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/slab.h>
19 #include <linux/usb.h>
20 #include <linux/usb/audio.h>
21 #include <linux/usb/audio-v2.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
37 * return the current pcm pointer. just based on the hwptr_done value.
39 static snd_pcm_uframes_t
snd_usb_pcm_pointer(struct snd_pcm_substream
*substream
)
41 struct snd_usb_substream
*subs
;
42 unsigned int hwptr_done
;
44 subs
= (struct snd_usb_substream
*)substream
->runtime
->private_data
;
45 spin_lock(&subs
->lock
);
46 hwptr_done
= subs
->hwptr_done
;
47 spin_unlock(&subs
->lock
);
48 return hwptr_done
/ (substream
->runtime
->frame_bits
>> 3);
52 * find a matching audio format
54 static struct audioformat
*find_format(struct snd_usb_substream
*subs
, unsigned int format
,
55 unsigned int rate
, unsigned int channels
)
58 struct audioformat
*found
= NULL
;
59 int cur_attr
= 0, attr
;
61 list_for_each(p
, &subs
->fmt_list
) {
62 struct audioformat
*fp
;
63 fp
= list_entry(p
, struct audioformat
, list
);
64 if (!(fp
->formats
& (1uLL << format
)))
66 if (fp
->channels
!= channels
)
68 if (rate
< fp
->rate_min
|| rate
> fp
->rate_max
)
70 if (! (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)) {
72 for (i
= 0; i
< fp
->nr_rates
; i
++)
73 if (fp
->rate_table
[i
] == rate
)
75 if (i
>= fp
->nr_rates
)
78 attr
= fp
->ep_attr
& USB_ENDPOINT_SYNCTYPE
;
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
) {
108 static int init_pitch_v1(struct snd_usb_audio
*chip
, int iface
,
109 struct usb_host_interface
*alts
,
110 struct audioformat
*fmt
)
112 struct usb_device
*dev
= chip
->dev
;
114 unsigned char data
[1];
117 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
120 if ((err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC_SET_CUR
,
121 USB_TYPE_CLASS
|USB_RECIP_ENDPOINT
|USB_DIR_OUT
,
122 UAC_EP_CS_ATTR_PITCH_CONTROL
<< 8, ep
,
123 data
, sizeof(data
), 1000)) < 0) {
124 snd_printk(KERN_ERR
"%d:%d:%d: cannot set enable PITCH\n",
125 dev
->devnum
, iface
, ep
);
132 static int init_pitch_v2(struct snd_usb_audio
*chip
, int iface
,
133 struct usb_host_interface
*alts
,
134 struct audioformat
*fmt
)
136 struct usb_device
*dev
= chip
->dev
;
137 unsigned char data
[1];
141 ep
= get_endpoint(alts
, 0)->bEndpointAddress
;
144 if ((err
= snd_usb_ctl_msg(dev
, usb_sndctrlpipe(dev
, 0), UAC2_CS_CUR
,
145 USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
| USB_DIR_OUT
,
146 UAC2_EP_CS_PITCH
<< 8, 0,
147 data
, sizeof(data
), 1000)) < 0) {
148 snd_printk(KERN_ERR
"%d:%d:%d: cannot set enable PITCH (v2)\n",
149 dev
->devnum
, iface
, fmt
->altsetting
);
157 * initialize the pitch control and sample rate
159 int snd_usb_init_pitch(struct snd_usb_audio
*chip
, int iface
,
160 struct usb_host_interface
*alts
,
161 struct audioformat
*fmt
)
163 struct usb_interface_descriptor
*altsd
= get_iface_desc(alts
);
165 /* if endpoint doesn't have pitch control, bail out */
166 if (!(fmt
->attributes
& UAC_EP_CS_ATTR_PITCH_CONTROL
))
169 switch (altsd
->bInterfaceProtocol
) {
172 return init_pitch_v1(chip
, iface
, alts
, fmt
);
175 return init_pitch_v2(chip
, iface
, alts
, fmt
);
180 * find a matching format and set up the interface
182 static int set_format(struct snd_usb_substream
*subs
, struct audioformat
*fmt
)
184 struct usb_device
*dev
= subs
->dev
;
185 struct usb_host_interface
*alts
;
186 struct usb_interface_descriptor
*altsd
;
187 struct usb_interface
*iface
;
188 unsigned int ep
, attr
;
189 int is_playback
= subs
->direction
== SNDRV_PCM_STREAM_PLAYBACK
;
192 iface
= usb_ifnum_to_if(dev
, fmt
->iface
);
195 alts
= &iface
->altsetting
[fmt
->altset_idx
];
196 altsd
= get_iface_desc(alts
);
197 if (WARN_ON(altsd
->bAlternateSetting
!= fmt
->altsetting
))
200 if (fmt
== subs
->cur_audiofmt
)
203 /* close the old interface */
204 if (subs
->interface
>= 0 && subs
->interface
!= fmt
->iface
) {
205 if (usb_set_interface(subs
->dev
, subs
->interface
, 0) < 0) {
206 snd_printk(KERN_ERR
"%d:%d:%d: return to setting 0 failed\n",
207 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
210 subs
->interface
= -1;
211 subs
->altset_idx
= 0;
215 if (subs
->interface
!= fmt
->iface
|| subs
->altset_idx
!= fmt
->altset_idx
) {
216 if (usb_set_interface(dev
, fmt
->iface
, fmt
->altsetting
) < 0) {
217 snd_printk(KERN_ERR
"%d:%d:%d: usb_set_interface failed\n",
218 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
221 snd_printdd(KERN_INFO
"setting usb interface %d:%d\n", fmt
->iface
, fmt
->altsetting
);
222 subs
->interface
= fmt
->iface
;
223 subs
->altset_idx
= fmt
->altset_idx
;
226 /* create a data pipe */
227 ep
= fmt
->endpoint
& USB_ENDPOINT_NUMBER_MASK
;
229 subs
->datapipe
= usb_sndisocpipe(dev
, ep
);
231 subs
->datapipe
= usb_rcvisocpipe(dev
, ep
);
232 subs
->datainterval
= fmt
->datainterval
;
233 subs
->syncpipe
= subs
->syncinterval
= 0;
234 subs
->maxpacksize
= fmt
->maxpacksize
;
237 /* we need a sync pipe in async OUT or adaptive IN mode */
238 /* check the number of EP, since some devices have broken
239 * descriptors which fool us. if it has only one EP,
240 * assume it as adaptive-out or sync-in.
242 attr
= fmt
->ep_attr
& USB_ENDPOINT_SYNCTYPE
;
243 if (((is_playback
&& attr
== USB_ENDPOINT_SYNC_ASYNC
) ||
244 (! is_playback
&& attr
== USB_ENDPOINT_SYNC_ADAPTIVE
)) &&
245 altsd
->bNumEndpoints
>= 2) {
246 /* check sync-pipe endpoint */
247 /* ... and check descriptor size before accessing bSynchAddress
248 because there is a version of the SB Audigy 2 NX firmware lacking
249 the audio fields in the endpoint descriptors */
250 if ((get_endpoint(alts
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != 0x01 ||
251 (get_endpoint(alts
, 1)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
252 get_endpoint(alts
, 1)->bSynchAddress
!= 0)) {
253 snd_printk(KERN_ERR
"%d:%d:%d : invalid synch pipe\n",
254 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
257 ep
= get_endpoint(alts
, 1)->bEndpointAddress
;
258 if (get_endpoint(alts
, 0)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
259 (( is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
| USB_DIR_IN
)) ||
260 (!is_playback
&& ep
!= (unsigned int)(get_endpoint(alts
, 0)->bSynchAddress
& ~USB_DIR_IN
)))) {
261 snd_printk(KERN_ERR
"%d:%d:%d : invalid synch pipe\n",
262 dev
->devnum
, fmt
->iface
, fmt
->altsetting
);
265 ep
&= USB_ENDPOINT_NUMBER_MASK
;
267 subs
->syncpipe
= usb_rcvisocpipe(dev
, ep
);
269 subs
->syncpipe
= usb_sndisocpipe(dev
, ep
);
270 if (get_endpoint(alts
, 1)->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
&&
271 get_endpoint(alts
, 1)->bRefresh
>= 1 &&
272 get_endpoint(alts
, 1)->bRefresh
<= 9)
273 subs
->syncinterval
= get_endpoint(alts
, 1)->bRefresh
;
274 else if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_FULL
)
275 subs
->syncinterval
= 1;
276 else if (get_endpoint(alts
, 1)->bInterval
>= 1 &&
277 get_endpoint(alts
, 1)->bInterval
<= 16)
278 subs
->syncinterval
= get_endpoint(alts
, 1)->bInterval
- 1;
280 subs
->syncinterval
= 3;
283 /* always fill max packet size */
284 if (fmt
->attributes
& UAC_EP_CS_ATTR_FILL_MAX
)
287 if ((err
= snd_usb_init_pitch(subs
->stream
->chip
, subs
->interface
, alts
, fmt
)) < 0)
290 subs
->cur_audiofmt
= fmt
;
292 snd_usb_set_format_quirk(subs
, fmt
);
301 * allocate a buffer and set the given audio format.
303 * so far we use a physically linear buffer although packetize transfer
304 * doesn't need a continuous area.
305 * if sg buffer is supported on the later version of alsa, we'll follow
308 static int snd_usb_hw_params(struct snd_pcm_substream
*substream
,
309 struct snd_pcm_hw_params
*hw_params
)
311 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
312 struct audioformat
*fmt
;
313 unsigned int channels
, rate
, format
;
316 ret
= snd_pcm_lib_alloc_vmalloc_buffer(substream
,
317 params_buffer_bytes(hw_params
));
321 format
= params_format(hw_params
);
322 rate
= params_rate(hw_params
);
323 channels
= params_channels(hw_params
);
324 fmt
= find_format(subs
, format
, rate
, channels
);
326 snd_printd(KERN_DEBUG
"cannot set format: format = %#x, rate = %d, channels = %d\n",
327 format
, rate
, channels
);
331 changed
= subs
->cur_audiofmt
!= fmt
||
332 subs
->period_bytes
!= params_period_bytes(hw_params
) ||
333 subs
->cur_rate
!= rate
;
334 if ((ret
= set_format(subs
, fmt
)) < 0)
337 if (subs
->cur_rate
!= rate
) {
338 struct usb_host_interface
*alts
;
339 struct usb_interface
*iface
;
340 iface
= usb_ifnum_to_if(subs
->dev
, fmt
->iface
);
341 alts
= &iface
->altsetting
[fmt
->altset_idx
];
342 ret
= snd_usb_init_sample_rate(subs
->stream
->chip
, subs
->interface
, alts
, fmt
, rate
);
345 subs
->cur_rate
= rate
;
350 snd_usb_release_substream_urbs(subs
, 0);
351 /* influenced: period_bytes, channels, rate, format, */
352 ret
= snd_usb_init_substream_urbs(subs
, params_period_bytes(hw_params
),
353 params_rate(hw_params
),
354 snd_pcm_format_physical_width(params_format(hw_params
)) *
355 params_channels(hw_params
));
364 * reset the audio format and release the buffer
366 static int snd_usb_hw_free(struct snd_pcm_substream
*substream
)
368 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
370 subs
->cur_audiofmt
= NULL
;
372 subs
->period_bytes
= 0;
373 if (!subs
->stream
->chip
->shutdown
)
374 snd_usb_release_substream_urbs(subs
, 0);
375 return snd_pcm_lib_free_vmalloc_buffer(substream
);
381 * only a few subtle things...
383 static int snd_usb_pcm_prepare(struct snd_pcm_substream
*substream
)
385 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
386 struct snd_usb_substream
*subs
= runtime
->private_data
;
388 if (! subs
->cur_audiofmt
) {
389 snd_printk(KERN_ERR
"usbaudio: no format is specified!\n");
393 /* some unit conversions in runtime */
394 subs
->maxframesize
= bytes_to_frames(runtime
, subs
->maxpacksize
);
395 subs
->curframesize
= bytes_to_frames(runtime
, subs
->curpacksize
);
397 /* reset the pointer */
398 subs
->hwptr_done
= 0;
399 subs
->transfer_done
= 0;
403 return snd_usb_substream_prepare(subs
, runtime
);
406 static struct snd_pcm_hardware snd_usb_hardware
=
408 .info
= SNDRV_PCM_INFO_MMAP
|
409 SNDRV_PCM_INFO_MMAP_VALID
|
410 SNDRV_PCM_INFO_BATCH
|
411 SNDRV_PCM_INFO_INTERLEAVED
|
412 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
413 SNDRV_PCM_INFO_PAUSE
,
414 .buffer_bytes_max
= 1024 * 1024,
415 .period_bytes_min
= 64,
416 .period_bytes_max
= 512 * 1024,
421 static int hw_check_valid_format(struct snd_usb_substream
*subs
,
422 struct snd_pcm_hw_params
*params
,
423 struct audioformat
*fp
)
425 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
426 struct snd_interval
*ct
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
427 struct snd_mask
*fmts
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
428 struct snd_interval
*pt
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
429 struct snd_mask check_fmts
;
432 /* check the format */
433 snd_mask_none(&check_fmts
);
434 check_fmts
.bits
[0] = (u32
)fp
->formats
;
435 check_fmts
.bits
[1] = (u32
)(fp
->formats
>> 32);
436 snd_mask_intersect(&check_fmts
, fmts
);
437 if (snd_mask_empty(&check_fmts
)) {
438 hwc_debug(" > check: no supported format %d\n", fp
->format
);
441 /* check the channels */
442 if (fp
->channels
< ct
->min
|| fp
->channels
> ct
->max
) {
443 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp
->channels
, ct
->min
, ct
->max
);
446 /* check the rate is within the range */
447 if (fp
->rate_min
> it
->max
|| (fp
->rate_min
== it
->max
&& it
->openmax
)) {
448 hwc_debug(" > check: rate_min %d > max %d\n", fp
->rate_min
, it
->max
);
451 if (fp
->rate_max
< it
->min
|| (fp
->rate_max
== it
->min
&& it
->openmin
)) {
452 hwc_debug(" > check: rate_max %d < min %d\n", fp
->rate_max
, it
->min
);
455 /* check whether the period time is >= the data packet interval */
456 if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_HIGH
) {
457 ptime
= 125 * (1 << fp
->datainterval
);
458 if (ptime
> pt
->max
|| (ptime
== pt
->max
&& pt
->openmax
)) {
459 hwc_debug(" > check: ptime %u > max %u\n", ptime
, pt
->max
);
466 static int hw_rule_rate(struct snd_pcm_hw_params
*params
,
467 struct snd_pcm_hw_rule
*rule
)
469 struct snd_usb_substream
*subs
= rule
->private;
471 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
472 unsigned int rmin
, rmax
;
475 hwc_debug("hw_rule_rate: (%d,%d)\n", it
->min
, it
->max
);
478 list_for_each(p
, &subs
->fmt_list
) {
479 struct audioformat
*fp
;
480 fp
= list_entry(p
, struct audioformat
, list
);
481 if (!hw_check_valid_format(subs
, params
, fp
))
484 if (rmin
> fp
->rate_min
)
486 if (rmax
< fp
->rate_max
)
495 hwc_debug(" --> get empty\n");
501 if (it
->min
< rmin
) {
506 if (it
->max
> rmax
) {
511 if (snd_interval_checkempty(it
)) {
515 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
520 static int hw_rule_channels(struct snd_pcm_hw_params
*params
,
521 struct snd_pcm_hw_rule
*rule
)
523 struct snd_usb_substream
*subs
= rule
->private;
525 struct snd_interval
*it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
526 unsigned int rmin
, rmax
;
529 hwc_debug("hw_rule_channels: (%d,%d)\n", it
->min
, it
->max
);
532 list_for_each(p
, &subs
->fmt_list
) {
533 struct audioformat
*fp
;
534 fp
= list_entry(p
, struct audioformat
, list
);
535 if (!hw_check_valid_format(subs
, params
, fp
))
538 if (rmin
> fp
->channels
)
540 if (rmax
< fp
->channels
)
549 hwc_debug(" --> get empty\n");
555 if (it
->min
< rmin
) {
560 if (it
->max
> rmax
) {
565 if (snd_interval_checkempty(it
)) {
569 hwc_debug(" --> (%d, %d) (changed = %d)\n", it
->min
, it
->max
, changed
);
573 static int hw_rule_format(struct snd_pcm_hw_params
*params
,
574 struct snd_pcm_hw_rule
*rule
)
576 struct snd_usb_substream
*subs
= rule
->private;
578 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
583 hwc_debug("hw_rule_format: %x:%x\n", fmt
->bits
[0], fmt
->bits
[1]);
585 list_for_each(p
, &subs
->fmt_list
) {
586 struct audioformat
*fp
;
587 fp
= list_entry(p
, struct audioformat
, list
);
588 if (!hw_check_valid_format(subs
, params
, fp
))
590 fbits
|= fp
->formats
;
593 oldbits
[0] = fmt
->bits
[0];
594 oldbits
[1] = fmt
->bits
[1];
595 fmt
->bits
[0] &= (u32
)fbits
;
596 fmt
->bits
[1] &= (u32
)(fbits
>> 32);
597 if (!fmt
->bits
[0] && !fmt
->bits
[1]) {
598 hwc_debug(" --> get empty\n");
601 changed
= (oldbits
[0] != fmt
->bits
[0] || oldbits
[1] != fmt
->bits
[1]);
602 hwc_debug(" --> %x:%x (changed = %d)\n", fmt
->bits
[0], fmt
->bits
[1], changed
);
606 static int hw_rule_period_time(struct snd_pcm_hw_params
*params
,
607 struct snd_pcm_hw_rule
*rule
)
609 struct snd_usb_substream
*subs
= rule
->private;
610 struct audioformat
*fp
;
611 struct snd_interval
*it
;
612 unsigned char min_datainterval
;
616 it
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
);
617 hwc_debug("hw_rule_period_time: (%u,%u)\n", it
->min
, it
->max
);
618 min_datainterval
= 0xff;
619 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
620 if (!hw_check_valid_format(subs
, params
, fp
))
622 min_datainterval
= min(min_datainterval
, fp
->datainterval
);
624 if (min_datainterval
== 0xff) {
625 hwc_debug(" --> get empty\n");
629 pmin
= 125 * (1 << min_datainterval
);
631 if (it
->min
< pmin
) {
636 if (snd_interval_checkempty(it
)) {
640 hwc_debug(" --> (%u,%u) (changed = %d)\n", it
->min
, it
->max
, changed
);
645 * If the device supports unusual bit rates, does the request meet these?
647 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime
*runtime
,
648 struct snd_usb_substream
*subs
)
650 struct audioformat
*fp
;
651 int count
= 0, needs_knot
= 0;
654 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
655 if (fp
->rates
& SNDRV_PCM_RATE_CONTINUOUS
)
657 count
+= fp
->nr_rates
;
658 if (fp
->rates
& SNDRV_PCM_RATE_KNOT
)
664 subs
->rate_list
.count
= count
;
665 subs
->rate_list
.list
= kmalloc(sizeof(int) * count
, GFP_KERNEL
);
666 subs
->rate_list
.mask
= 0;
668 list_for_each_entry(fp
, &subs
->fmt_list
, list
) {
670 for (i
= 0; i
< fp
->nr_rates
; i
++)
671 subs
->rate_list
.list
[count
++] = fp
->rate_table
[i
];
673 err
= snd_pcm_hw_constraint_list(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
683 * set up the runtime hardware information.
686 static int setup_hw_info(struct snd_pcm_runtime
*runtime
, struct snd_usb_substream
*subs
)
689 unsigned int pt
, ptmin
;
690 int param_period_time_if_needed
;
693 runtime
->hw
.formats
= subs
->formats
;
695 runtime
->hw
.rate_min
= 0x7fffffff;
696 runtime
->hw
.rate_max
= 0;
697 runtime
->hw
.channels_min
= 256;
698 runtime
->hw
.channels_max
= 0;
699 runtime
->hw
.rates
= 0;
701 /* check min/max rates and channels */
702 list_for_each(p
, &subs
->fmt_list
) {
703 struct audioformat
*fp
;
704 fp
= list_entry(p
, struct audioformat
, list
);
705 runtime
->hw
.rates
|= fp
->rates
;
706 if (runtime
->hw
.rate_min
> fp
->rate_min
)
707 runtime
->hw
.rate_min
= fp
->rate_min
;
708 if (runtime
->hw
.rate_max
< fp
->rate_max
)
709 runtime
->hw
.rate_max
= fp
->rate_max
;
710 if (runtime
->hw
.channels_min
> fp
->channels
)
711 runtime
->hw
.channels_min
= fp
->channels
;
712 if (runtime
->hw
.channels_max
< fp
->channels
)
713 runtime
->hw
.channels_max
= fp
->channels
;
714 if (fp
->fmt_type
== UAC_FORMAT_TYPE_II
&& fp
->frame_size
> 0) {
715 runtime
->hw
.period_bytes_min
= runtime
->hw
.period_bytes_max
=
718 pt
= 125 * (1 << fp
->datainterval
);
719 ptmin
= min(ptmin
, pt
);
722 param_period_time_if_needed
= SNDRV_PCM_HW_PARAM_PERIOD_TIME
;
723 if (snd_usb_get_speed(subs
->dev
) != USB_SPEED_HIGH
)
724 /* full speed devices have fixed data packet interval */
727 /* if period time doesn't go below 1 ms, no rules needed */
728 param_period_time_if_needed
= -1;
729 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
732 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
734 SNDRV_PCM_HW_PARAM_FORMAT
,
735 SNDRV_PCM_HW_PARAM_CHANNELS
,
736 param_period_time_if_needed
,
739 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
740 hw_rule_channels
, subs
,
741 SNDRV_PCM_HW_PARAM_FORMAT
,
742 SNDRV_PCM_HW_PARAM_RATE
,
743 param_period_time_if_needed
,
746 if ((err
= snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_FORMAT
,
747 hw_rule_format
, subs
,
748 SNDRV_PCM_HW_PARAM_RATE
,
749 SNDRV_PCM_HW_PARAM_CHANNELS
,
750 param_period_time_if_needed
,
753 if (param_period_time_if_needed
>= 0) {
754 err
= snd_pcm_hw_rule_add(runtime
, 0,
755 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
756 hw_rule_period_time
, subs
,
757 SNDRV_PCM_HW_PARAM_FORMAT
,
758 SNDRV_PCM_HW_PARAM_CHANNELS
,
759 SNDRV_PCM_HW_PARAM_RATE
,
764 if ((err
= snd_usb_pcm_check_knot(runtime
, subs
)) < 0)
769 static int snd_usb_pcm_open(struct snd_pcm_substream
*substream
, int direction
)
771 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
772 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
773 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
775 subs
->interface
= -1;
776 subs
->altset_idx
= 0;
777 runtime
->hw
= snd_usb_hardware
;
778 runtime
->private_data
= subs
;
779 subs
->pcm_substream
= substream
;
780 return setup_hw_info(runtime
, subs
);
783 static int snd_usb_pcm_close(struct snd_pcm_substream
*substream
, int direction
)
785 struct snd_usb_stream
*as
= snd_pcm_substream_chip(substream
);
786 struct snd_usb_substream
*subs
= &as
->substream
[direction
];
788 if (!as
->chip
->shutdown
&& subs
->interface
>= 0) {
789 usb_set_interface(subs
->dev
, subs
->interface
, 0);
790 subs
->interface
= -1;
792 subs
->pcm_substream
= NULL
;
796 static int snd_usb_playback_open(struct snd_pcm_substream
*substream
)
798 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
801 static int snd_usb_playback_close(struct snd_pcm_substream
*substream
)
803 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_PLAYBACK
);
806 static int snd_usb_capture_open(struct snd_pcm_substream
*substream
)
808 return snd_usb_pcm_open(substream
, SNDRV_PCM_STREAM_CAPTURE
);
811 static int snd_usb_capture_close(struct snd_pcm_substream
*substream
)
813 return snd_usb_pcm_close(substream
, SNDRV_PCM_STREAM_CAPTURE
);
816 static struct snd_pcm_ops snd_usb_playback_ops
= {
817 .open
= snd_usb_playback_open
,
818 .close
= snd_usb_playback_close
,
819 .ioctl
= snd_pcm_lib_ioctl
,
820 .hw_params
= snd_usb_hw_params
,
821 .hw_free
= snd_usb_hw_free
,
822 .prepare
= snd_usb_pcm_prepare
,
823 .trigger
= snd_usb_substream_playback_trigger
,
824 .pointer
= snd_usb_pcm_pointer
,
825 .page
= snd_pcm_lib_get_vmalloc_page
,
826 .mmap
= snd_pcm_lib_mmap_vmalloc
,
829 static struct snd_pcm_ops snd_usb_capture_ops
= {
830 .open
= snd_usb_capture_open
,
831 .close
= snd_usb_capture_close
,
832 .ioctl
= snd_pcm_lib_ioctl
,
833 .hw_params
= snd_usb_hw_params
,
834 .hw_free
= snd_usb_hw_free
,
835 .prepare
= snd_usb_pcm_prepare
,
836 .trigger
= snd_usb_substream_capture_trigger
,
837 .pointer
= snd_usb_pcm_pointer
,
838 .page
= snd_pcm_lib_get_vmalloc_page
,
839 .mmap
= snd_pcm_lib_mmap_vmalloc
,
842 void snd_usb_set_pcm_ops(struct snd_pcm
*pcm
, int stream
)
844 snd_pcm_set_ops(pcm
, stream
,
845 stream
== SNDRV_PCM_STREAM_PLAYBACK
?
846 &snd_usb_playback_ops
: &snd_usb_capture_ops
);