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 ep = get_endpoint(alts, 0)->bEndpointAddress;
154 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
155 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
156 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
157 data, sizeof(data))) < 0) {
158 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
159 dev->devnum, iface, ep);
166 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
167 struct usb_host_interface *alts,
168 struct audioformat *fmt)
170 struct usb_device *dev = chip->dev;
171 unsigned char data[1];
175 ep = get_endpoint(alts, 0)->bEndpointAddress;
178 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
179 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
180 UAC2_EP_CS_PITCH << 8, 0,
181 data, sizeof(data))) < 0) {
182 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
183 dev->devnum, iface, fmt->altsetting);
191 * initialize the pitch control and sample rate
193 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
194 struct usb_host_interface *alts,
195 struct audioformat *fmt)
197 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
199 /* if endpoint doesn't have pitch control, bail out */
200 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
203 switch (altsd->bInterfaceProtocol) {
206 return init_pitch_v1(chip, iface, alts, fmt);
209 return init_pitch_v2(chip, iface, alts, fmt);
214 * find a matching format and set up the interface
216 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
218 struct usb_device *dev = subs->dev;
219 struct usb_host_interface *alts;
220 struct usb_interface_descriptor *altsd;
221 struct usb_interface *iface;
222 unsigned int ep, attr;
223 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
226 iface = usb_ifnum_to_if(dev, fmt->iface);
229 alts = &iface->altsetting[fmt->altset_idx];
230 altsd = get_iface_desc(alts);
231 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
234 if (fmt == subs->cur_audiofmt)
237 /* close the old interface */
238 if (subs->interface >= 0 && subs->interface != fmt->iface) {
239 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
240 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
241 dev->devnum, fmt->iface, fmt->altsetting);
244 subs->interface = -1;
245 subs->altset_idx = 0;
249 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
250 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
251 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
252 dev->devnum, fmt->iface, fmt->altsetting);
255 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
256 subs->interface = fmt->iface;
257 subs->altset_idx = fmt->altset_idx;
260 /* create a data pipe */
261 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
263 subs->datapipe = usb_sndisocpipe(dev, ep);
265 subs->datapipe = usb_rcvisocpipe(dev, ep);
266 subs->datainterval = fmt->datainterval;
267 subs->syncpipe = subs->syncinterval = 0;
268 subs->maxpacksize = fmt->maxpacksize;
269 subs->syncmaxsize = 0;
272 /* we need a sync pipe in async OUT or adaptive IN mode */
273 /* check the number of EP, since some devices have broken
274 * descriptors which fool us. if it has only one EP,
275 * assume it as adaptive-out or sync-in.
277 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
278 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
279 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
280 altsd->bNumEndpoints >= 2) {
281 /* check sync-pipe endpoint */
282 /* ... and check descriptor size before accessing bSynchAddress
283 because there is a version of the SB Audigy 2 NX firmware lacking
284 the audio fields in the endpoint descriptors */
285 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
286 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
287 get_endpoint(alts, 1)->bSynchAddress != 0)) {
288 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
289 dev->devnum, fmt->iface, fmt->altsetting);
292 ep = get_endpoint(alts, 1)->bEndpointAddress;
293 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
294 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
295 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
296 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
297 dev->devnum, fmt->iface, fmt->altsetting);
300 ep &= USB_ENDPOINT_NUMBER_MASK;
302 subs->syncpipe = usb_rcvisocpipe(dev, ep);
304 subs->syncpipe = usb_sndisocpipe(dev, ep);
305 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
306 get_endpoint(alts, 1)->bRefresh >= 1 &&
307 get_endpoint(alts, 1)->bRefresh <= 9)
308 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
309 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
310 subs->syncinterval = 1;
311 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
312 get_endpoint(alts, 1)->bInterval <= 16)
313 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
315 subs->syncinterval = 3;
316 subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
319 /* always fill max packet size */
320 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
323 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
326 subs->cur_audiofmt = fmt;
328 snd_usb_set_format_quirk(subs, fmt);
332 "setting done: format = %d, rate = %d..%d, channels = %d\n",
333 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
335 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
336 subs->datapipe, subs->syncpipe);
345 * allocate a buffer and set the given audio format.
347 * so far we use a physically linear buffer although packetize transfer
348 * doesn't need a continuous area.
349 * if sg buffer is supported on the later version of alsa, we'll follow
352 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
353 struct snd_pcm_hw_params *hw_params)
355 struct snd_usb_substream *subs = substream->runtime->private_data;
356 struct audioformat *fmt;
357 unsigned int channels, rate, format;
360 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
361 params_buffer_bytes(hw_params));
365 format = params_format(hw_params);
366 rate = params_rate(hw_params);
367 channels = params_channels(hw_params);
368 fmt = find_format(subs, format, rate, channels);
370 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
371 format, rate, channels);
375 changed = subs->cur_audiofmt != fmt ||
376 subs->period_bytes != params_period_bytes(hw_params) ||
377 subs->cur_rate != rate;
379 down_read(&subs->stream->chip->shutdown_rwsem);
380 if (subs->stream->chip->shutdown) {
384 if ((ret = set_format(subs, fmt)) < 0)
387 if (subs->cur_rate != rate) {
388 struct usb_host_interface *alts;
389 struct usb_interface *iface;
390 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
391 alts = &iface->altsetting[fmt->altset_idx];
392 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
395 subs->cur_rate = rate;
400 snd_usb_release_substream_urbs(subs, 0);
401 /* influenced: period_bytes, channels, rate, format, */
402 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
403 params_rate(hw_params),
404 snd_pcm_format_physical_width(params_format(hw_params)) *
405 params_channels(hw_params));
409 up_read(&subs->stream->chip->shutdown_rwsem);
416 * reset the audio format and release the buffer
418 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
420 struct snd_usb_substream *subs = substream->runtime->private_data;
422 subs->cur_audiofmt = NULL;
424 subs->period_bytes = 0;
425 down_read(&subs->stream->chip->shutdown_rwsem);
426 snd_usb_release_substream_urbs(subs, 0);
427 up_read(&subs->stream->chip->shutdown_rwsem);
428 return snd_pcm_lib_free_vmalloc_buffer(substream);
434 * only a few subtle things...
436 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
438 struct snd_pcm_runtime *runtime = substream->runtime;
439 struct snd_usb_substream *subs = runtime->private_data;
442 if (! subs->cur_audiofmt) {
443 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
447 down_read(&subs->stream->chip->shutdown_rwsem);
448 if (subs->stream->chip->shutdown) {
452 /* some unit conversions in runtime */
453 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
454 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
456 /* reset the pointer */
457 subs->hwptr_done = 0;
458 subs->transfer_done = 0;
460 subs->last_delay = 0;
461 subs->last_frame_number = 0;
464 ret = snd_usb_substream_prepare(subs, runtime);
466 up_read(&subs->stream->chip->shutdown_rwsem);
470 static struct snd_pcm_hardware snd_usb_hardware =
472 .info = SNDRV_PCM_INFO_MMAP |
473 SNDRV_PCM_INFO_MMAP_VALID |
474 SNDRV_PCM_INFO_BATCH |
475 SNDRV_PCM_INFO_INTERLEAVED |
476 SNDRV_PCM_INFO_BLOCK_TRANSFER |
477 SNDRV_PCM_INFO_PAUSE,
478 .buffer_bytes_max = 1024 * 1024,
479 .period_bytes_min = 64,
480 .period_bytes_max = 512 * 1024,
485 static int hw_check_valid_format(struct snd_usb_substream *subs,
486 struct snd_pcm_hw_params *params,
487 struct audioformat *fp)
489 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
490 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
491 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
492 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
493 struct snd_mask check_fmts;
496 /* check the format */
497 snd_mask_none(&check_fmts);
498 check_fmts.bits[0] = (u32)fp->formats;
499 check_fmts.bits[1] = (u32)(fp->formats >> 32);
500 snd_mask_intersect(&check_fmts, fmts);
501 if (snd_mask_empty(&check_fmts)) {
502 hwc_debug(" > check: no supported format %d\n", fp->format);
505 /* check the channels */
506 if (fp->channels < ct->min || fp->channels > ct->max) {
507 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
510 /* check the rate is within the range */
511 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
512 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
515 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
516 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
519 /* check whether the period time is >= the data packet interval */
520 if (subs->speed != USB_SPEED_FULL) {
521 ptime = 125 * (1 << fp->datainterval);
522 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
523 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
530 static int hw_rule_rate(struct snd_pcm_hw_params *params,
531 struct snd_pcm_hw_rule *rule)
533 struct snd_usb_substream *subs = rule->private;
535 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
536 unsigned int rmin, rmax;
539 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
542 list_for_each(p, &subs->fmt_list) {
543 struct audioformat *fp;
544 fp = list_entry(p, struct audioformat, list);
545 if (!hw_check_valid_format(subs, params, fp))
548 if (rmin > fp->rate_min)
550 if (rmax < fp->rate_max)
559 hwc_debug(" --> get empty\n");
565 if (it->min < rmin) {
570 if (it->max > rmax) {
575 if (snd_interval_checkempty(it)) {
579 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
584 static int hw_rule_channels(struct snd_pcm_hw_params *params,
585 struct snd_pcm_hw_rule *rule)
587 struct snd_usb_substream *subs = rule->private;
589 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
590 unsigned int rmin, rmax;
593 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
596 list_for_each(p, &subs->fmt_list) {
597 struct audioformat *fp;
598 fp = list_entry(p, struct audioformat, list);
599 if (!hw_check_valid_format(subs, params, fp))
602 if (rmin > fp->channels)
604 if (rmax < fp->channels)
613 hwc_debug(" --> get empty\n");
619 if (it->min < rmin) {
624 if (it->max > rmax) {
629 if (snd_interval_checkempty(it)) {
633 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
637 static int hw_rule_format(struct snd_pcm_hw_params *params,
638 struct snd_pcm_hw_rule *rule)
640 struct snd_usb_substream *subs = rule->private;
642 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
647 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
649 list_for_each(p, &subs->fmt_list) {
650 struct audioformat *fp;
651 fp = list_entry(p, struct audioformat, list);
652 if (!hw_check_valid_format(subs, params, fp))
654 fbits |= fp->formats;
657 oldbits[0] = fmt->bits[0];
658 oldbits[1] = fmt->bits[1];
659 fmt->bits[0] &= (u32)fbits;
660 fmt->bits[1] &= (u32)(fbits >> 32);
661 if (!fmt->bits[0] && !fmt->bits[1]) {
662 hwc_debug(" --> get empty\n");
665 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
666 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
670 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
671 struct snd_pcm_hw_rule *rule)
673 struct snd_usb_substream *subs = rule->private;
674 struct audioformat *fp;
675 struct snd_interval *it;
676 unsigned char min_datainterval;
680 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
681 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
682 min_datainterval = 0xff;
683 list_for_each_entry(fp, &subs->fmt_list, list) {
684 if (!hw_check_valid_format(subs, params, fp))
686 min_datainterval = min(min_datainterval, fp->datainterval);
688 if (min_datainterval == 0xff) {
689 hwc_debug(" --> get empty\n");
693 pmin = 125 * (1 << min_datainterval);
695 if (it->min < pmin) {
700 if (snd_interval_checkempty(it)) {
704 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
709 * If the device supports unusual bit rates, does the request meet these?
711 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
712 struct snd_usb_substream *subs)
714 struct audioformat *fp;
715 int count = 0, needs_knot = 0;
718 kfree(subs->rate_list.list);
719 subs->rate_list.list = NULL;
721 list_for_each_entry(fp, &subs->fmt_list, list) {
722 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
724 count += fp->nr_rates;
725 if (fp->rates & SNDRV_PCM_RATE_KNOT)
731 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
732 if (!subs->rate_list.list)
734 subs->rate_list.count = count;
735 subs->rate_list.mask = 0;
737 list_for_each_entry(fp, &subs->fmt_list, list) {
739 for (i = 0; i < fp->nr_rates; i++)
740 subs->rate_list.list[count++] = fp->rate_table[i];
742 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
752 * set up the runtime hardware information.
755 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
758 unsigned int pt, ptmin;
759 int param_period_time_if_needed;
762 runtime->hw.formats = subs->formats;
764 runtime->hw.rate_min = 0x7fffffff;
765 runtime->hw.rate_max = 0;
766 runtime->hw.channels_min = 256;
767 runtime->hw.channels_max = 0;
768 runtime->hw.rates = 0;
770 /* check min/max rates and channels */
771 list_for_each(p, &subs->fmt_list) {
772 struct audioformat *fp;
773 fp = list_entry(p, struct audioformat, list);
774 runtime->hw.rates |= fp->rates;
775 if (runtime->hw.rate_min > fp->rate_min)
776 runtime->hw.rate_min = fp->rate_min;
777 if (runtime->hw.rate_max < fp->rate_max)
778 runtime->hw.rate_max = fp->rate_max;
779 if (runtime->hw.channels_min > fp->channels)
780 runtime->hw.channels_min = fp->channels;
781 if (runtime->hw.channels_max < fp->channels)
782 runtime->hw.channels_max = fp->channels;
783 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
784 /* FIXME: there might be more than one audio formats... */
785 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
788 pt = 125 * (1 << fp->datainterval);
789 ptmin = min(ptmin, pt);
791 err = snd_usb_autoresume(subs->stream->chip);
795 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
796 if (subs->speed == USB_SPEED_FULL)
797 /* full speed devices have fixed data packet interval */
800 /* if period time doesn't go below 1 ms, no rules needed */
801 param_period_time_if_needed = -1;
802 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
805 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
807 SNDRV_PCM_HW_PARAM_FORMAT,
808 SNDRV_PCM_HW_PARAM_CHANNELS,
809 param_period_time_if_needed,
812 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
813 hw_rule_channels, subs,
814 SNDRV_PCM_HW_PARAM_FORMAT,
815 SNDRV_PCM_HW_PARAM_RATE,
816 param_period_time_if_needed,
819 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
820 hw_rule_format, subs,
821 SNDRV_PCM_HW_PARAM_RATE,
822 SNDRV_PCM_HW_PARAM_CHANNELS,
823 param_period_time_if_needed,
826 if (param_period_time_if_needed >= 0) {
827 err = snd_pcm_hw_rule_add(runtime, 0,
828 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
829 hw_rule_period_time, subs,
830 SNDRV_PCM_HW_PARAM_FORMAT,
831 SNDRV_PCM_HW_PARAM_CHANNELS,
832 SNDRV_PCM_HW_PARAM_RATE,
837 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
842 snd_usb_autosuspend(subs->stream->chip);
846 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
848 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
849 struct snd_pcm_runtime *runtime = substream->runtime;
850 struct snd_usb_substream *subs = &as->substream[direction];
852 subs->interface = -1;
853 subs->altset_idx = 0;
854 runtime->hw = snd_usb_hardware;
855 runtime->private_data = subs;
856 subs->pcm_substream = substream;
857 /* runtime PM is also done there */
858 return setup_hw_info(runtime, subs);
861 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
863 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
864 struct snd_usb_substream *subs = &as->substream[direction];
866 if (!as->chip->shutdown && subs->interface >= 0) {
867 usb_set_interface(subs->dev, subs->interface, 0);
868 subs->interface = -1;
870 subs->pcm_substream = NULL;
871 snd_usb_autosuspend(subs->stream->chip);
875 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
877 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
880 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
882 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
885 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
887 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
890 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
892 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
895 static struct snd_pcm_ops snd_usb_playback_ops = {
896 .open = snd_usb_playback_open,
897 .close = snd_usb_playback_close,
898 .ioctl = snd_pcm_lib_ioctl,
899 .hw_params = snd_usb_hw_params,
900 .hw_free = snd_usb_hw_free,
901 .prepare = snd_usb_pcm_prepare,
902 .trigger = snd_usb_substream_playback_trigger,
903 .pointer = snd_usb_pcm_pointer,
904 .page = snd_pcm_lib_get_vmalloc_page,
905 .mmap = snd_pcm_lib_mmap_vmalloc,
908 static struct snd_pcm_ops snd_usb_capture_ops = {
909 .open = snd_usb_capture_open,
910 .close = snd_usb_capture_close,
911 .ioctl = snd_pcm_lib_ioctl,
912 .hw_params = snd_usb_hw_params,
913 .hw_free = snd_usb_hw_free,
914 .prepare = snd_usb_pcm_prepare,
915 .trigger = snd_usb_substream_capture_trigger,
916 .pointer = snd_usb_pcm_pointer,
917 .page = snd_pcm_lib_get_vmalloc_page,
918 .mmap = snd_pcm_lib_mmap_vmalloc,
921 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
923 snd_pcm_set_ops(pcm, stream,
924 stream == SNDRV_PCM_STREAM_PLAYBACK ?
925 &snd_usb_playback_ops : &snd_usb_capture_ops);