2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include <linux/init.h>
18 #include <linux/usb.h>
19 #include <linux/usb/audio.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
34 * return the current pcm pointer. just based on the hwptr_done value.
36 static snd_pcm_uframes_t
snd_usb_pcm_pointer(struct snd_pcm_substream
*substream
)
38 struct snd_usb_substream
*subs
;
39 unsigned int hwptr_done
;
41 subs
= (struct snd_usb_substream
*)substream
->runtime
->private_data
;
42 spin_lock(&subs
->lock
);
43 hwptr_done
= subs
->hwptr_done
;
44 spin_unlock(&subs
->lock
);
45 return hwptr_done
/ (substream
->runtime
->frame_bits
>> 3);
49 * find a matching audio format
51 static struct audioformat
*find_format(struct snd_usb_substream
*subs
, unsigned int format
,
52 unsigned int rate
, unsigned int channels
)
55 struct audioformat
*found
= NULL
;
56 int cur_attr
= 0, attr
;
58 list_for_each(p
, &subs
->fmt_list
) {
59 struct audioformat
*fp
;
60 fp
= list_entry(p
, struct audioformat
, list
);
61 if (fp
->format
!= format
|| fp
->channels
!= channels
)
63 if (rate
< fp
->rate_min
|| rate
> fp
->rate_max
)
65 if (! (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)) {
67 for (i
= 0; i
< fp
->nr_rates
; i
++)
68 if (fp
->rate_table
[i
] == rate
)
70 if (i
>= fp
->nr_rates
)
73 attr
= fp
->ep_attr
& USB_ENDPOINT_SYNCTYPE
;
79 /* avoid async out and adaptive in if the other method
80 * supports the same format.
81 * this is a workaround for the case like
82 * M-audio audiophile USB.
84 if (attr
!= cur_attr
) {
85 if ((attr
== USB_ENDPOINT_SYNC_ASYNC
&&
86 subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) ||
87 (attr
== USB_ENDPOINT_SYNC_ADAPTIVE
&&
88 subs
->direction
== SNDRV_PCM_STREAM_CAPTURE
))
90 if ((cur_attr
== USB_ENDPOINT_SYNC_ASYNC
&&
91 subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
) ||
92 (cur_attr
== USB_ENDPOINT_SYNC_ADAPTIVE
&&
93 subs
->direction
== SNDRV_PCM_STREAM_CAPTURE
)) {
99 /* find the format with the largest max. packet size */
100 if (fp
->maxpacksize
> found
->maxpacksize
) {
110 * initialize the picth control and sample rate
112 int snd_usb_init_pitch(struct usb_device
*dev
, int iface
,
113 struct usb_host_interface
*alts
,
114 struct audioformat
*fmt
)
117 unsigned char data
[1];
120 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
121 /* if endpoint has pitch control, enable it */
122 if (fmt
->attributes
& UAC_EP_CS_ATTR_PITCH_CONTROL
) {
124 if ((err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
125 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
126 UAC_EP_CS_ATTR_PITCH_CONTROL
<< 8, ep
, data
, 1, 1000)) < 0) {
127 snd_printk(KERN_ERR
"%d:%d:%d: cannot set enable PITCH\n",
128 dev
->devnum
, iface
, ep
);
135 int snd_usb_init_sample_rate(struct usb_device
*dev
, int iface
,
136 struct usb_host_interface
*alts
,
137 struct audioformat
*fmt
, int rate
)
140 unsigned char data
[3];
143 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
144 /* if endpoint has sampling rate control, set it */
145 if (fmt
->attributes
& UAC_EP_CS_ATTR_SAMPLE_RATE
) {
149 data
[2] = rate
>> 16;
150 if ((err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
151 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
152 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
, data
, 3, 1000)) < 0) {
153 snd_printk(KERN_ERR
"%d:%d:%d: cannot set freq %d to ep %#x\n",
154 dev
->devnum
, iface
, fmt
->altsetting
, rate
, ep
);
157 if ((err
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), UAC_GET_CUR
,
158 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_IN
,
159 UAC_EP_CS_ATTR_SAMPLE_RATE
<< 8, ep
, data
, 3, 1000)) < 0) {
160 snd_printk(KERN_WARNING
"%d:%d:%d: cannot get freq at ep %#x\n",
161 dev
->devnum
, iface
, fmt
->altsetting
, ep
);
162 return 0; /* some devices don't support reading */
164 crate
= data
[0] | (data
[1] << 8) | (data
[2] << 16);
166 snd_printd(KERN_WARNING
"current rate %d is different from the runtime rate %d\n", crate
, rate
);
167 // runtime->rate = crate;
174 * find a matching format and set up the interface
176 static int set_format(struct snd_usb_substream
*subs
, struct audioformat
*fmt
)
178 struct usb_device
*dev
= subs
->dev
;
179 struct usb_host_interface
*alts
;
180 struct usb_interface_descriptor
*altsd
;
181 struct usb_interface
*iface
;
182 unsigned int ep
, attr
;
183 int is_playback
= subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
;
186 iface
= usb_ifnum_to_if(dev
, fmt
->iface
);
189 alts
= &iface
->altsetting
[fmt
->altset_idx
];
190 altsd
= get_iface_desc(alts
);
191 if (WARN_ON(altsd
->bAlternateSetting
!= fmt
->altsetting
))
194 if (fmt
== subs
->cur_audiofmt
)
197 /* close the old interface */
198 if (subs
->interface
>= 0 && subs
->interface
!= fmt
->iface
) {
199 if (usb_set_interface(subs
->dev
, subs
->interface
, 0) < 0) {
200 snd_printk(KERN_ERR
"%d:%d:%d: return to setting 0 failed\n",
201 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
204 subs
->interface
= -1;
209 if (subs
->interface
!= fmt
->iface
|| subs
->format
!= fmt
->altset_idx
) {
210 if (usb_set_interface(dev
, fmt
->iface
, fmt
->altsetting
) < 0) {
211 snd_printk(KERN_ERR
"%d:%d:%d: usb_set_interface failed\n",
212 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
215 snd_printdd(KERN_INFO
"setting usb interface %d:%d\n", fmt
->iface
, fmt
->altsetting
);
216 subs
->interface
= fmt
->iface
;
217 subs
->format
= fmt
->altset_idx
;
220 /* create a data pipe */
221 ep
= fmt
->endpoint
& USB_ENDPOINT_NUMBER_MASK
;
223 subs
->datapipe
= usb_sndisocpipe(dev
, ep
);
225 subs
->datapipe
= usb_rcvisocpipe(dev
, ep
);
226 subs
->datainterval
= fmt
->datainterval
;
227 subs
->syncpipe
= subs
->syncinterval
= 0;
228 subs
->maxpacksize
= fmt
->maxpacksize
;
231 /* we need a sync pipe in async OUT or adaptive IN mode */
232 /* check the number of EP, since some devices have broken
233 * descriptors which fool us. if it has only one EP,
234 * assume it as adaptive-out or sync-in.
236 attr
= fmt
->ep_attr
& USB_ENDPOINT_SYNCTYPE
;
237 if (((is_playback
&& attr
== USB_ENDPOINT_SYNC_ASYNC
) ||
238 (! is_playback
&& attr
== USB_ENDPOINT_SYNC_ADAPTIVE
)) &&
239 altsd
->bNumEndpoints
>= 2) {
240 /* check sync-pipe endpoint */
241 /* ... and check descriptor size before accessing bSynchAddress
242 because there is a version of the SB Audigy 2 NX firmware lacking
243 the audio fields in the endpoint descriptors */
244 if ((get_endpoint(alts
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != 0x01 ||
245 (get_endpoint(alts
, 1)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
246 get_endpoint(alts
, 1)->bSynchAddress
!= 0)) {
247 snd_printk(KERN_ERR
"%d:%d:%d : invalid synch pipe\n",
248 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
251 ep
= get_endpoint(alts
, 1)->bEndpointAddress
;
252 if (get_endpoint(alts
, 0)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
253 (( is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
| USB_DIR_IN
)) ||
254 (!is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
& ~USB_DIR_IN
)))) {
255 snd_printk(KERN_ERR
"%d:%d:%d : invalid synch pipe\n",
256 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
259 ep
&= USB_ENDPOINT_NUMBER_MASK
;
261 subs
->syncpipe
= usb_rcvisocpipe(dev
, ep
);
263 subs
->syncpipe
= usb_sndisocpipe(dev
, ep
);
264 if (get_endpoint(alts
, 1)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
265 get_endpoint(alts
, 1)->bRefresh
>= 1 &&
266 get_endpoint(alts
, 1)->bRefresh
<= 9)
267 subs
->syncinterval
= get_endpoint(alts
, 1)->bRefresh
;
268 else if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_FULL
)
269 subs
->syncinterval
= 1;
270 else if (get_endpoint(alts
, 1)->bInterval
>= 1 &&
271 get_endpoint(alts
, 1)->bInterval
<= 16)
272 subs
->syncinterval
= get_endpoint(alts
, 1)->bInterval
- 1;
274 subs
->syncinterval
= 3;
277 /* always fill max packet size */
278 if (fmt
->attributes
& UAC_EP_CS_ATTR_FILL_MAX
)
281 if ((err
= snd_usb_init_pitch(dev
, subs
->interface
, alts
, fmt
)) < 0)
284 subs
->cur_audiofmt
= fmt
;
286 snd_usb_set_format_quirk(subs
, fmt
);
290 "setting done: format = %d, rate = %d..%d, channels = %d\n",
291 fmt
->format
, fmt
->rate_min
, fmt
->rate_max
, fmt
->channels
);
293 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
294 subs
->datapipe
, subs
->syncpipe
);
303 * allocate a buffer and set the given audio format.
305 * so far we use a physically linear buffer although packetize transfer
306 * doesn't need a continuous area.
307 * if sg buffer is supported on the later version of alsa, we'll follow
310 static int snd_usb_hw_params(struct snd_pcm_substream
*substream
,
311 struct snd_pcm_hw_params
*hw_params
)
313 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
314 struct audioformat
*fmt
;
315 unsigned int channels
, rate
, format
;
318 ret
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
319 params_buffer_bytes(hw_params
));
323 format
= params_format(hw_params
);
324 rate
= params_rate(hw_params
);
325 channels
= params_channels(hw_params
);
326 fmt
= find_format(subs
, format
, rate
, channels
);
328 snd_printd(KERN_DEBUG
"cannot set format: format = %#x, rate = %d, channels = %d\n",
329 format
, rate
, channels
);
333 changed
= subs
->cur_audiofmt
!= fmt
||
334 subs
->period_bytes
!= params_period_bytes(hw_params
) ||
335 subs
->cur_rate
!= rate
;
336 if ((ret
= set_format(subs
, fmt
)) < 0)
339 if (subs
->cur_rate
!= rate
) {
340 struct usb_host_interface
*alts
;
341 struct usb_interface
*iface
;
342 iface
= usb_ifnum_to_if(subs
->dev
, fmt
->iface
);
343 alts
= &iface
->altsetting
[fmt
->altset_idx
];
344 ret
= snd_usb_init_sample_rate(subs
->dev
, subs
->interface
, alts
, fmt
, rate
);
347 subs
->cur_rate
= rate
;
352 snd_usb_release_substream_urbs(subs
, 0);
353 /* influenced: period_bytes, channels, rate, format, */
354 ret
= snd_usb_init_substream_urbs(subs
, params_period_bytes(hw_params
),
355 params_rate(hw_params
),
356 snd_pcm_format_physical_width(params_format(hw_params
)) *
357 params_channels(hw_params
));
366 * reset the audio format and release the buffer
368 static int snd_usb_hw_free(struct snd_pcm_substream
*substream
)
370 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
372 subs
->cur_audiofmt
= NULL
;
374 subs
->period_bytes
= 0;
375 if (!subs
->stream
->chip
->shutdown
)
376 snd_usb_release_substream_urbs(subs
, 0);
377 return snd_pcm_lib_free_vmalloc_buffer(substream
);
383 * only a few subtle things...
385 static int snd_usb_pcm_prepare(struct snd_pcm_substream
*substream
)
387 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
388 struct snd_usb_substream
*subs
= runtime
->private_data
;
390 if (! subs
->cur_audiofmt
) {
391 snd_printk(KERN_ERR
"usbaudio: no format is specified!\n");
395 /* some unit conversions in runtime */
396 subs
->maxframesize
= bytes_to_frames(runtime
, subs
->maxpacksize
);
397 subs
->curframesize
= bytes_to_frames(runtime
, subs
->curpacksize
);
399 /* reset the pointer */
400 subs
->hwptr_done
= 0;
401 subs
->transfer_done
= 0;
405 return snd_usb_substream_prepare(subs
, runtime
);
408 static struct snd_pcm_hardware snd_usb_hardware
=
410 .info
= SNDRV_PCM_INFO_MMAP
|
411 SNDRV_PCM_INFO_MMAP_VALID
|
412 SNDRV_PCM_INFO_BATCH
|
413 SNDRV_PCM_INFO_INTERLEAVED
|
414 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
415 SNDRV_PCM_INFO_PAUSE
,
416 .buffer_bytes_max
= 1024 * 1024,
417 .period_bytes_min
= 64,
418 .period_bytes_max
= 512 * 1024,
423 static int hw_check_valid_format(struct snd_usb_substream
*subs
,
424 struct snd_pcm_hw_params
*params
,
425 struct audioformat
*fp
)
427 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
428 struct snd_interval
*ct
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
429 struct snd_mask
*fmts
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
430 struct snd_interval
*pt
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
433 /* check the format */
434 if (!snd_mask_test(fmts
, fp
->format
)) {
435 hwc_debug(" > check: no supported format %d\n", fp
->format
);
438 /* check the channels */
439 if (fp
->channels
< ct
->min
|| fp
->channels
> ct
->max
) {
440 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp
->channels
, ct
->min
, ct
->max
);
443 /* check the rate is within the range */
444 if (fp
->rate_min
> it
->max
|| (fp
->rate_min
== it
->max
&& it
->openmax
)) {
445 hwc_debug(" > check: rate_min %d > max %d\n", fp
->rate_min
, it
->max
);
448 if (fp
->rate_max
< it
->min
|| (fp
->rate_max
== it
->min
&& it
->openmin
)) {
449 hwc_debug(" > check: rate_max %d < min %d\n", fp
->rate_max
, it
->min
);
452 /* check whether the period time is >= the data packet interval */
453 if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_HIGH
) {
454 ptime
= 125 * (1 << fp
->datainterval
);
455 if (ptime
> pt
->max
|| (ptime
== pt
->max
&& pt
->openmax
)) {
456 hwc_debug(" > check: ptime %u > max %u\n", ptime
, pt
->max
);
463 static int hw_rule_rate(struct snd_pcm_hw_params
*params
,
464 struct snd_pcm_hw_rule
*rule
)
466 struct snd_usb_substream
*subs
= rule
->private;
468 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
469 unsigned int rmin
, rmax
;
472 hwc_debug("hw_rule_rate: (%d,%d)\n", it
->min
, it
->max
);
475 list_for_each(p
, &subs
->fmt_list
) {
476 struct audioformat
*fp
;
477 fp
= list_entry(p
, struct audioformat
, list
);
478 if (!hw_check_valid_format(subs
, params
, fp
))
481 if (rmin
> fp
->rate_min
)
483 if (rmax
< fp
->rate_max
)
492 hwc_debug(" --> get empty\n");
498 if (it
->min
< rmin
) {
503 if (it
->max
> rmax
) {
508 if (snd_interval_checkempty(it
)) {
512 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
517 static int hw_rule_channels(struct snd_pcm_hw_params
*params
,
518 struct snd_pcm_hw_rule
*rule
)
520 struct snd_usb_substream
*subs
= rule
->private;
522 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
523 unsigned int rmin
, rmax
;
526 hwc_debug("hw_rule_channels: (%d,%d)\n", it
->min
, it
->max
);
529 list_for_each(p
, &subs
->fmt_list
) {
530 struct audioformat
*fp
;
531 fp
= list_entry(p
, struct audioformat
, list
);
532 if (!hw_check_valid_format(subs
, params
, fp
))
535 if (rmin
> fp
->channels
)
537 if (rmax
< fp
->channels
)
546 hwc_debug(" --> get empty\n");
552 if (it
->min
< rmin
) {
557 if (it
->max
> rmax
) {
562 if (snd_interval_checkempty(it
)) {
566 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
570 static int hw_rule_format(struct snd_pcm_hw_params
*params
,
571 struct snd_pcm_hw_rule
*rule
)
573 struct snd_usb_substream
*subs
= rule
->private;
575 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
580 hwc_debug("hw_rule_format: %x:%x\n", fmt
->bits
[0], fmt
->bits
[1]);
582 list_for_each(p
, &subs
->fmt_list
) {
583 struct audioformat
*fp
;
584 fp
= list_entry(p
, struct audioformat
, list
);
585 if (!hw_check_valid_format(subs
, params
, fp
))
587 fbits
|= (1ULL << fp
->format
);
590 oldbits
[0] = fmt
->bits
[0];
591 oldbits
[1] = fmt
->bits
[1];
592 fmt
->bits
[0] &= (u32
)fbits
;
593 fmt
->bits
[1] &= (u32
)(fbits
>> 32);
594 if (!fmt
->bits
[0] && !fmt
->bits
[1]) {
595 hwc_debug(" --> get empty\n");
598 changed
= (oldbits
[0] != fmt
->bits
[0] || oldbits
[1] != fmt
->bits
[1]);
599 hwc_debug(" --> %x:%x (changed = %d)\n", fmt
->bits
[0], fmt
->bits
[1], changed
);
603 static int hw_rule_period_time(struct snd_pcm_hw_params
*params
,
604 struct snd_pcm_hw_rule
*rule
)
606 struct snd_usb_substream
*subs
= rule
->private;
607 struct audioformat
*fp
;
608 struct snd_interval
*it
;
609 unsigned char min_datainterval
;
613 it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
614 hwc_debug("hw_rule_period_time: (%u,%u)\n", it
->min
, it
->max
);
615 min_datainterval
= 0xff;
616 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
617 if (!hw_check_valid_format(subs
, params
, fp
))
619 min_datainterval
= min(min_datainterval
, fp
->datainterval
);
621 if (min_datainterval
== 0xff) {
622 hwc_debug(" --> get emtpy\n");
626 pmin
= 125 * (1 << min_datainterval
);
628 if (it
->min
< pmin
) {
633 if (snd_interval_checkempty(it
)) {
637 hwc_debug(" --> (%u,%u) (changed = %d)\n", it
->min
, it
->max
, changed
);
642 * If the device supports unusual bit rates, does the request meet these?
644 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime
*runtime
,
645 struct snd_usb_substream
*subs
)
647 struct audioformat
*fp
;
648 int count
= 0, needs_knot
= 0;
651 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
652 if (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)
654 count
+= fp
->nr_rates
;
655 if (fp
->rates
& SNDRV_PCM_RATE_KNOT
)
661 subs
->rate_list
.count
= count
;
662 subs
->rate_list
.list
= kmalloc(sizeof(int) * count
, GFP_KERNEL
);
663 subs
->rate_list
.mask
= 0;
665 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
667 for (i
= 0; i
< fp
->nr_rates
; i
++)
668 subs
->rate_list
.list
[count
++] = fp
->rate_table
[i
];
670 err
= snd_pcm_hw_constraint_list(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
680 * set up the runtime hardware information.
683 static int setup_hw_info(struct snd_pcm_runtime
*runtime
, struct snd_usb_substream
*subs
)
686 unsigned int pt
, ptmin
;
687 int param_period_time_if_needed
;
690 runtime
->hw
.formats
= subs
->formats
;
692 runtime
->hw
.rate_min
= 0x7fffffff;
693 runtime
->hw
.rate_max
= 0;
694 runtime
->hw
.channels_min
= 256;
695 runtime
->hw
.channels_max
= 0;
696 runtime
->hw
.rates
= 0;
698 /* check min/max rates and channels */
699 list_for_each(p
, &subs
->fmt_list
) {
700 struct audioformat
*fp
;
701 fp
= list_entry(p
, struct audioformat
, list
);
702 runtime
->hw
.rates
|= fp
->rates
;
703 if (runtime
->hw
.rate_min
> fp
->rate_min
)
704 runtime
->hw
.rate_min
= fp
->rate_min
;
705 if (runtime
->hw
.rate_max
< fp
->rate_max
)
706 runtime
->hw
.rate_max
= fp
->rate_max
;
707 if (runtime
->hw
.channels_min
> fp
->channels
)
708 runtime
->hw
.channels_min
= fp
->channels
;
709 if (runtime
->hw
.channels_max
< fp
->channels
)
710 runtime
->hw
.channels_max
= fp
->channels
;
711 if (fp
->fmt_type
== UAC_FORMAT_TYPE_II
&& fp
->frame_size
> 0) {
712 /* FIXME: there might be more than one audio formats... */
713 runtime
->hw
.period_bytes_min
= runtime
->hw
.period_bytes_max
=
716 pt
= 125 * (1 << fp
->datainterval
);
717 ptmin
= min(ptmin
, pt
);
720 param_period_time_if_needed
= SNDRV_PCM_HW_PARAM_PERIOD_TIME
;
721 if (snd_usb_get_speed(subs
->dev
) != USB_SPEED_HIGH
)
722 /* full speed devices have fixed data packet interval */
725 /* if period time doesn't go below 1 ms, no rules needed */
726 param_period_time_if_needed
= -1;
727 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
730 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
732 SNDRV_PCM_HW_PARAM_FORMAT
,
733 SNDRV_PCM_HW_PARAM_CHANNELS
,
734 param_period_time_if_needed
,
737 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
738 hw_rule_channels
, subs
,
739 SNDRV_PCM_HW_PARAM_FORMAT
,
740 SNDRV_PCM_HW_PARAM_RATE
,
741 param_period_time_if_needed
,
744 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
745 hw_rule_format
, subs
,
746 SNDRV_PCM_HW_PARAM_RATE
,
747 SNDRV_PCM_HW_PARAM_CHANNELS
,
748 param_period_time_if_needed
,
751 if (param_period_time_if_needed
>= 0) {
752 err
= snd_pcm_hw_rule_add(runtime
, 0,
753 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
754 hw_rule_period_time
, subs
,
755 SNDRV_PCM_HW_PARAM_FORMAT
,
756 SNDRV_PCM_HW_PARAM_CHANNELS
,
757 SNDRV_PCM_HW_PARAM_RATE
,
762 if ((err
= snd_usb_pcm_check_knot(runtime
, subs
)) < 0)
767 static int snd_usb_pcm_open(struct snd_pcm_substream
*substream
, int direction
)
769 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
770 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
771 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
773 subs
->interface
= -1;
775 runtime
->hw
= snd_usb_hardware
;
776 runtime
->private_data
= subs
;
777 subs
->pcm_substream
= substream
;
778 return setup_hw_info(runtime
, subs
);
781 static int snd_usb_pcm_close(struct snd_pcm_substream
*substream
, int direction
)
783 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
784 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
786 if (!as
->chip
->shutdown
&& subs
->interface
>= 0) {
787 usb_set_interface(subs
->dev
, subs
->interface
, 0);
788 subs
->interface
= -1;
790 subs
->pcm_substream
= NULL
;
794 static int snd_usb_playback_open(struct snd_pcm_substream
*substream
)
796 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
799 static int snd_usb_playback_close(struct snd_pcm_substream
*substream
)
801 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
804 static int snd_usb_capture_open(struct snd_pcm_substream
*substream
)
806 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_CAPTURE
);
809 static int snd_usb_capture_close(struct snd_pcm_substream
*substream
)
811 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_CAPTURE
);
814 static struct snd_pcm_ops snd_usb_playback_ops
= {
815 .open
= snd_usb_playback_open
,
816 .close
= snd_usb_playback_close
,
817 .ioctl
= snd_pcm_lib_ioctl
,
818 .hw_params
= snd_usb_hw_params
,
819 .hw_free
= snd_usb_hw_free
,
820 .prepare
= snd_usb_pcm_prepare
,
821 .trigger
= snd_usb_substream_playback_trigger
,
822 .pointer
= snd_usb_pcm_pointer
,
823 .page
= snd_pcm_lib_get_vmalloc_page
,
824 .mmap
= snd_pcm_lib_mmap_vmalloc
,
827 static struct snd_pcm_ops snd_usb_capture_ops
= {
828 .open
= snd_usb_capture_open
,
829 .close
= snd_usb_capture_close
,
830 .ioctl
= snd_pcm_lib_ioctl
,
831 .hw_params
= snd_usb_hw_params
,
832 .hw_free
= snd_usb_hw_free
,
833 .prepare
= snd_usb_pcm_prepare
,
834 .trigger
= snd_usb_substream_capture_trigger
,
835 .pointer
= snd_usb_pcm_pointer
,
836 .page
= snd_pcm_lib_get_vmalloc_page
,
837 .mmap
= snd_pcm_lib_mmap_vmalloc
,
840 void snd_usb_set_pcm_ops(struct snd_pcm
*pcm
, int stream
)
842 snd_pcm_set_ops(pcm
, stream
,
843 stream
== SNDRV_PCM_STREAM_PLAYBACK
?
844 &snd_usb_playback_ops
: &snd_usb_capture_ops
);