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 estimated delay based on USB frame counters */
38 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
41 int current_frame_number;
45 current_frame_number = usb_get_current_frame_number(subs->dev);
47 * HCD implementations use different widths, use lower 8 bits.
48 * The delay will be managed up to 256ms, which is more than
51 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
53 /* Approximation based on number of samples per USB frame (ms),
54 some truncation for 44.1 but the estimate is good enough */
55 est_delay = subs->last_delay - (frame_diff * rate / 1000);
62 * return the current pcm pointer. just based on the hwptr_done value.
64 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
66 struct snd_usb_substream *subs;
67 unsigned int hwptr_done;
69 subs = (struct snd_usb_substream *)substream->runtime->private_data;
70 if (subs->stream->chip->shutdown)
71 return SNDRV_PCM_POS_XRUN;
72 spin_lock(&subs->lock);
73 hwptr_done = subs->hwptr_done;
74 substream->runtime->delay = snd_usb_pcm_delay(subs,
75 substream->runtime->rate);
76 spin_unlock(&subs->lock);
77 return hwptr_done / (substream->runtime->frame_bits >> 3);
81 * find a matching audio format
83 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
84 unsigned int rate, unsigned int channels)
87 struct audioformat *found = NULL;
88 int cur_attr = 0, attr;
90 list_for_each(p, &subs->fmt_list) {
91 struct audioformat *fp;
92 fp = list_entry(p, struct audioformat, list);
93 if (!(fp->formats & (1uLL << format)))
95 if (fp->channels != channels)
97 if (rate < fp->rate_min || rate > fp->rate_max)
99 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
101 for (i = 0; i < fp->nr_rates; i++)
102 if (fp->rate_table[i] == rate)
104 if (i >= fp->nr_rates)
107 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
113 /* avoid async out and adaptive in if the other method
114 * supports the same format.
115 * this is a workaround for the case like
116 * M-audio audiophile USB.
118 if (attr != cur_attr) {
119 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
120 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
121 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
122 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
124 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
125 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
126 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
127 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
133 /* find the format with the largest max. packet size */
134 if (fp->maxpacksize > found->maxpacksize) {
142 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
143 struct usb_host_interface *alts,
144 struct audioformat *fmt)
146 struct usb_device *dev = chip->dev;
148 unsigned char data[1];
151 if (get_iface_desc(alts)->bNumEndpoints < 1)
153 ep = get_endpoint(alts, 0)->bEndpointAddress;
156 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
157 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
158 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
159 data, sizeof(data))) < 0) {
160 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
161 dev->devnum, iface, ep);
168 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
169 struct usb_host_interface *alts,
170 struct audioformat *fmt)
172 struct usb_device *dev = chip->dev;
173 unsigned char data[1];
177 ep = get_endpoint(alts, 0)->bEndpointAddress;
180 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
181 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
182 UAC2_EP_CS_PITCH << 8, 0,
183 data, sizeof(data))) < 0) {
184 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
185 dev->devnum, iface, fmt->altsetting);
193 * initialize the pitch control and sample rate
195 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
196 struct usb_host_interface *alts,
197 struct audioformat *fmt)
199 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
201 /* if endpoint doesn't have pitch control, bail out */
202 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
205 switch (altsd->bInterfaceProtocol) {
208 return init_pitch_v1(chip, iface, alts, fmt);
211 return init_pitch_v2(chip, iface, alts, fmt);
216 * find a matching format and set up the interface
218 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
220 struct usb_device *dev = subs->dev;
221 struct usb_host_interface *alts;
222 struct usb_interface_descriptor *altsd;
223 struct usb_interface *iface;
224 unsigned int ep, attr;
225 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
228 iface = usb_ifnum_to_if(dev, fmt->iface);
231 alts = &iface->altsetting[fmt->altset_idx];
232 altsd = get_iface_desc(alts);
233 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
236 if (fmt == subs->cur_audiofmt)
239 /* close the old interface */
240 if (subs->interface >= 0 && subs->interface != fmt->iface) {
241 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
242 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
243 dev->devnum, fmt->iface, fmt->altsetting);
246 subs->interface = -1;
247 subs->altset_idx = 0;
251 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
252 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
253 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
254 dev->devnum, fmt->iface, fmt->altsetting);
257 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
258 subs->interface = fmt->iface;
259 subs->altset_idx = fmt->altset_idx;
262 /* create a data pipe */
263 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
265 subs->datapipe = usb_sndisocpipe(dev, ep);
267 subs->datapipe = usb_rcvisocpipe(dev, ep);
268 subs->datainterval = fmt->datainterval;
269 subs->syncpipe = subs->syncinterval = 0;
270 subs->maxpacksize = fmt->maxpacksize;
271 subs->syncmaxsize = 0;
274 /* we need a sync pipe in async OUT or adaptive IN mode */
275 /* check the number of EP, since some devices have broken
276 * descriptors which fool us. if it has only one EP,
277 * assume it as adaptive-out or sync-in.
279 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
280 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
281 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
282 altsd->bNumEndpoints >= 2) {
283 /* check sync-pipe endpoint */
284 /* ... and check descriptor size before accessing bSynchAddress
285 because there is a version of the SB Audigy 2 NX firmware lacking
286 the audio fields in the endpoint descriptors */
287 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
288 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
289 get_endpoint(alts, 1)->bSynchAddress != 0)) {
290 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
291 dev->devnum, fmt->iface, fmt->altsetting);
294 ep = get_endpoint(alts, 1)->bEndpointAddress;
295 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
296 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
297 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
298 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
299 dev->devnum, fmt->iface, fmt->altsetting);
302 ep &= USB_ENDPOINT_NUMBER_MASK;
304 subs->syncpipe = usb_rcvisocpipe(dev, ep);
306 subs->syncpipe = usb_sndisocpipe(dev, ep);
307 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
308 get_endpoint(alts, 1)->bRefresh >= 1 &&
309 get_endpoint(alts, 1)->bRefresh <= 9)
310 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
311 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
312 subs->syncinterval = 1;
313 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
314 get_endpoint(alts, 1)->bInterval <= 16)
315 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
317 subs->syncinterval = 3;
318 subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
321 /* always fill max packet size */
322 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
325 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
328 subs->cur_audiofmt = fmt;
330 snd_usb_set_format_quirk(subs, fmt);
334 "setting done: format = %d, rate = %d..%d, channels = %d\n",
335 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
337 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
338 subs->datapipe, subs->syncpipe);
347 * allocate a buffer and set the given audio format.
349 * so far we use a physically linear buffer although packetize transfer
350 * doesn't need a continuous area.
351 * if sg buffer is supported on the later version of alsa, we'll follow
354 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
355 struct snd_pcm_hw_params *hw_params)
357 struct snd_usb_substream *subs = substream->runtime->private_data;
358 struct audioformat *fmt;
359 unsigned int channels, rate, format;
362 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
363 params_buffer_bytes(hw_params));
367 format = params_format(hw_params);
368 rate = params_rate(hw_params);
369 channels = params_channels(hw_params);
370 fmt = find_format(subs, format, rate, channels);
372 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
373 format, rate, channels);
377 changed = subs->cur_audiofmt != fmt ||
378 subs->period_bytes != params_period_bytes(hw_params) ||
379 subs->cur_rate != rate;
381 down_read(&subs->stream->chip->shutdown_rwsem);
382 if (subs->stream->chip->shutdown) {
386 if ((ret = set_format(subs, fmt)) < 0)
389 if (subs->cur_rate != rate) {
390 struct usb_host_interface *alts;
391 struct usb_interface *iface;
392 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
393 alts = &iface->altsetting[fmt->altset_idx];
394 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
397 subs->cur_rate = rate;
402 snd_usb_release_substream_urbs(subs, 0);
403 /* influenced: period_bytes, channels, rate, format, */
404 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
405 params_rate(hw_params),
406 snd_pcm_format_physical_width(params_format(hw_params)) *
407 params_channels(hw_params));
411 up_read(&subs->stream->chip->shutdown_rwsem);
418 * reset the audio format and release the buffer
420 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
422 struct snd_usb_substream *subs = substream->runtime->private_data;
424 subs->cur_audiofmt = NULL;
426 subs->period_bytes = 0;
427 down_read(&subs->stream->chip->shutdown_rwsem);
428 snd_usb_release_substream_urbs(subs, 0);
429 up_read(&subs->stream->chip->shutdown_rwsem);
430 return snd_pcm_lib_free_vmalloc_buffer(substream);
436 * only a few subtle things...
438 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
440 struct snd_pcm_runtime *runtime = substream->runtime;
441 struct snd_usb_substream *subs = runtime->private_data;
444 if (! subs->cur_audiofmt) {
445 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
449 down_read(&subs->stream->chip->shutdown_rwsem);
450 if (subs->stream->chip->shutdown) {
454 /* some unit conversions in runtime */
455 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
456 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
458 /* reset the pointer */
459 subs->hwptr_done = 0;
460 subs->transfer_done = 0;
462 subs->last_delay = 0;
463 subs->last_frame_number = 0;
466 ret = snd_usb_substream_prepare(subs, runtime);
468 up_read(&subs->stream->chip->shutdown_rwsem);
472 static struct snd_pcm_hardware snd_usb_hardware =
474 .info = SNDRV_PCM_INFO_MMAP |
475 SNDRV_PCM_INFO_MMAP_VALID |
476 SNDRV_PCM_INFO_BATCH |
477 SNDRV_PCM_INFO_INTERLEAVED |
478 SNDRV_PCM_INFO_BLOCK_TRANSFER |
479 SNDRV_PCM_INFO_PAUSE,
480 .buffer_bytes_max = 1024 * 1024,
481 .period_bytes_min = 64,
482 .period_bytes_max = 512 * 1024,
487 static int hw_check_valid_format(struct snd_usb_substream *subs,
488 struct snd_pcm_hw_params *params,
489 struct audioformat *fp)
491 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
492 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
493 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
494 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
495 struct snd_mask check_fmts;
498 /* check the format */
499 snd_mask_none(&check_fmts);
500 check_fmts.bits[0] = (u32)fp->formats;
501 check_fmts.bits[1] = (u32)(fp->formats >> 32);
502 snd_mask_intersect(&check_fmts, fmts);
503 if (snd_mask_empty(&check_fmts)) {
504 hwc_debug(" > check: no supported format %d\n", fp->format);
507 /* check the channels */
508 if (fp->channels < ct->min || fp->channels > ct->max) {
509 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
512 /* check the rate is within the range */
513 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
514 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
517 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
518 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
521 /* check whether the period time is >= the data packet interval */
522 if (subs->speed != USB_SPEED_FULL) {
523 ptime = 125 * (1 << fp->datainterval);
524 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
525 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
532 static int hw_rule_rate(struct snd_pcm_hw_params *params,
533 struct snd_pcm_hw_rule *rule)
535 struct snd_usb_substream *subs = rule->private;
537 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
538 unsigned int rmin, rmax;
541 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
544 list_for_each(p, &subs->fmt_list) {
545 struct audioformat *fp;
546 fp = list_entry(p, struct audioformat, list);
547 if (!hw_check_valid_format(subs, params, fp))
550 if (rmin > fp->rate_min)
552 if (rmax < fp->rate_max)
561 hwc_debug(" --> get empty\n");
567 if (it->min < rmin) {
572 if (it->max > rmax) {
577 if (snd_interval_checkempty(it)) {
581 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
586 static int hw_rule_channels(struct snd_pcm_hw_params *params,
587 struct snd_pcm_hw_rule *rule)
589 struct snd_usb_substream *subs = rule->private;
591 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
592 unsigned int rmin, rmax;
595 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
598 list_for_each(p, &subs->fmt_list) {
599 struct audioformat *fp;
600 fp = list_entry(p, struct audioformat, list);
601 if (!hw_check_valid_format(subs, params, fp))
604 if (rmin > fp->channels)
606 if (rmax < fp->channels)
615 hwc_debug(" --> get empty\n");
621 if (it->min < rmin) {
626 if (it->max > rmax) {
631 if (snd_interval_checkempty(it)) {
635 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
639 static int hw_rule_format(struct snd_pcm_hw_params *params,
640 struct snd_pcm_hw_rule *rule)
642 struct snd_usb_substream *subs = rule->private;
644 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
649 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
651 list_for_each(p, &subs->fmt_list) {
652 struct audioformat *fp;
653 fp = list_entry(p, struct audioformat, list);
654 if (!hw_check_valid_format(subs, params, fp))
656 fbits |= fp->formats;
659 oldbits[0] = fmt->bits[0];
660 oldbits[1] = fmt->bits[1];
661 fmt->bits[0] &= (u32)fbits;
662 fmt->bits[1] &= (u32)(fbits >> 32);
663 if (!fmt->bits[0] && !fmt->bits[1]) {
664 hwc_debug(" --> get empty\n");
667 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
668 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
672 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
673 struct snd_pcm_hw_rule *rule)
675 struct snd_usb_substream *subs = rule->private;
676 struct audioformat *fp;
677 struct snd_interval *it;
678 unsigned char min_datainterval;
682 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
683 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
684 min_datainterval = 0xff;
685 list_for_each_entry(fp, &subs->fmt_list, list) {
686 if (!hw_check_valid_format(subs, params, fp))
688 min_datainterval = min(min_datainterval, fp->datainterval);
690 if (min_datainterval == 0xff) {
691 hwc_debug(" --> get empty\n");
695 pmin = 125 * (1 << min_datainterval);
697 if (it->min < pmin) {
702 if (snd_interval_checkempty(it)) {
706 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
711 * If the device supports unusual bit rates, does the request meet these?
713 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
714 struct snd_usb_substream *subs)
716 struct audioformat *fp;
717 int count = 0, needs_knot = 0;
720 kfree(subs->rate_list.list);
721 subs->rate_list.list = NULL;
723 list_for_each_entry(fp, &subs->fmt_list, list) {
724 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
726 count += fp->nr_rates;
727 if (fp->rates & SNDRV_PCM_RATE_KNOT)
733 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
734 if (!subs->rate_list.list)
736 subs->rate_list.count = count;
737 subs->rate_list.mask = 0;
739 list_for_each_entry(fp, &subs->fmt_list, list) {
741 for (i = 0; i < fp->nr_rates; i++)
742 subs->rate_list.list[count++] = fp->rate_table[i];
744 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
754 * set up the runtime hardware information.
757 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
760 unsigned int pt, ptmin;
761 int param_period_time_if_needed;
764 runtime->hw.formats = subs->formats;
766 runtime->hw.rate_min = 0x7fffffff;
767 runtime->hw.rate_max = 0;
768 runtime->hw.channels_min = 256;
769 runtime->hw.channels_max = 0;
770 runtime->hw.rates = 0;
772 /* check min/max rates and channels */
773 list_for_each(p, &subs->fmt_list) {
774 struct audioformat *fp;
775 fp = list_entry(p, struct audioformat, list);
776 runtime->hw.rates |= fp->rates;
777 if (runtime->hw.rate_min > fp->rate_min)
778 runtime->hw.rate_min = fp->rate_min;
779 if (runtime->hw.rate_max < fp->rate_max)
780 runtime->hw.rate_max = fp->rate_max;
781 if (runtime->hw.channels_min > fp->channels)
782 runtime->hw.channels_min = fp->channels;
783 if (runtime->hw.channels_max < fp->channels)
784 runtime->hw.channels_max = fp->channels;
785 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
786 /* FIXME: there might be more than one audio formats... */
787 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
790 pt = 125 * (1 << fp->datainterval);
791 ptmin = min(ptmin, pt);
793 err = snd_usb_autoresume(subs->stream->chip);
797 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
798 if (subs->speed == USB_SPEED_FULL)
799 /* full speed devices have fixed data packet interval */
802 /* if period time doesn't go below 1 ms, no rules needed */
803 param_period_time_if_needed = -1;
804 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
807 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
809 SNDRV_PCM_HW_PARAM_FORMAT,
810 SNDRV_PCM_HW_PARAM_CHANNELS,
811 param_period_time_if_needed,
814 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
815 hw_rule_channels, subs,
816 SNDRV_PCM_HW_PARAM_FORMAT,
817 SNDRV_PCM_HW_PARAM_RATE,
818 param_period_time_if_needed,
821 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
822 hw_rule_format, subs,
823 SNDRV_PCM_HW_PARAM_RATE,
824 SNDRV_PCM_HW_PARAM_CHANNELS,
825 param_period_time_if_needed,
828 if (param_period_time_if_needed >= 0) {
829 err = snd_pcm_hw_rule_add(runtime, 0,
830 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
831 hw_rule_period_time, subs,
832 SNDRV_PCM_HW_PARAM_FORMAT,
833 SNDRV_PCM_HW_PARAM_CHANNELS,
834 SNDRV_PCM_HW_PARAM_RATE,
839 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
844 snd_usb_autosuspend(subs->stream->chip);
848 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
850 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
851 struct snd_pcm_runtime *runtime = substream->runtime;
852 struct snd_usb_substream *subs = &as->substream[direction];
854 subs->interface = -1;
855 subs->altset_idx = 0;
856 runtime->hw = snd_usb_hardware;
857 runtime->private_data = subs;
858 subs->pcm_substream = substream;
859 /* runtime PM is also done there */
860 return setup_hw_info(runtime, subs);
863 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
865 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
866 struct snd_usb_substream *subs = &as->substream[direction];
868 if (!as->chip->shutdown && subs->interface >= 0) {
869 usb_set_interface(subs->dev, subs->interface, 0);
870 subs->interface = -1;
872 subs->pcm_substream = NULL;
873 snd_usb_autosuspend(subs->stream->chip);
877 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
879 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
882 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
884 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
887 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
889 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
892 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
894 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
897 static struct snd_pcm_ops snd_usb_playback_ops = {
898 .open = snd_usb_playback_open,
899 .close = snd_usb_playback_close,
900 .ioctl = snd_pcm_lib_ioctl,
901 .hw_params = snd_usb_hw_params,
902 .hw_free = snd_usb_hw_free,
903 .prepare = snd_usb_pcm_prepare,
904 .trigger = snd_usb_substream_playback_trigger,
905 .pointer = snd_usb_pcm_pointer,
906 .page = snd_pcm_lib_get_vmalloc_page,
907 .mmap = snd_pcm_lib_mmap_vmalloc,
910 static struct snd_pcm_ops snd_usb_capture_ops = {
911 .open = snd_usb_capture_open,
912 .close = snd_usb_capture_close,
913 .ioctl = snd_pcm_lib_ioctl,
914 .hw_params = snd_usb_hw_params,
915 .hw_free = snd_usb_hw_free,
916 .prepare = snd_usb_pcm_prepare,
917 .trigger = snd_usb_substream_capture_trigger,
918 .pointer = snd_usb_pcm_pointer,
919 .page = snd_pcm_lib_get_vmalloc_page,
920 .mmap = snd_pcm_lib_mmap_vmalloc,
923 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
925 snd_pcm_set_ops(pcm, stream,
926 stream == SNDRV_PCM_STREAM_PLAYBACK ?
927 &snd_usb_playback_ops : &snd_usb_capture_ops);