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 spin_lock(&subs->lock);
71 hwptr_done = subs->hwptr_done;
72 substream->runtime->delay = snd_usb_pcm_delay(subs,
73 substream->runtime->rate);
74 spin_unlock(&subs->lock);
75 return hwptr_done / (substream->runtime->frame_bits >> 3);
79 * find a matching audio format
81 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
82 unsigned int rate, unsigned int channels)
85 struct audioformat *found = NULL;
86 int cur_attr = 0, attr;
88 list_for_each(p, &subs->fmt_list) {
89 struct audioformat *fp;
90 fp = list_entry(p, struct audioformat, list);
91 if (!(fp->formats & (1uLL << format)))
93 if (fp->channels != channels)
95 if (rate < fp->rate_min || rate > fp->rate_max)
97 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
99 for (i = 0; i < fp->nr_rates; i++)
100 if (fp->rate_table[i] == rate)
102 if (i >= fp->nr_rates)
105 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
111 /* avoid async out and adaptive in if the other method
112 * supports the same format.
113 * this is a workaround for the case like
114 * M-audio audiophile USB.
116 if (attr != cur_attr) {
117 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
118 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
119 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
120 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
122 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
123 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
124 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
125 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
131 /* find the format with the largest max. packet size */
132 if (fp->maxpacksize > found->maxpacksize) {
140 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
141 struct usb_host_interface *alts,
142 struct audioformat *fmt)
144 struct usb_device *dev = chip->dev;
146 unsigned char data[1];
149 ep = get_endpoint(alts, 0)->bEndpointAddress;
152 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
153 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
154 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
155 data, sizeof(data))) < 0) {
156 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
157 dev->devnum, iface, ep);
164 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
165 struct usb_host_interface *alts,
166 struct audioformat *fmt)
168 struct usb_device *dev = chip->dev;
169 unsigned char data[1];
173 ep = get_endpoint(alts, 0)->bEndpointAddress;
176 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
177 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
178 UAC2_EP_CS_PITCH << 8, 0,
179 data, sizeof(data))) < 0) {
180 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
181 dev->devnum, iface, fmt->altsetting);
189 * initialize the pitch control and sample rate
191 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
192 struct usb_host_interface *alts,
193 struct audioformat *fmt)
195 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
197 /* if endpoint doesn't have pitch control, bail out */
198 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
201 switch (altsd->bInterfaceProtocol) {
204 return init_pitch_v1(chip, iface, alts, fmt);
207 return init_pitch_v2(chip, iface, alts, fmt);
212 * find a matching format and set up the interface
214 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
216 struct usb_device *dev = subs->dev;
217 struct usb_host_interface *alts;
218 struct usb_interface_descriptor *altsd;
219 struct usb_interface *iface;
220 unsigned int ep, attr;
221 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
224 iface = usb_ifnum_to_if(dev, fmt->iface);
227 alts = &iface->altsetting[fmt->altset_idx];
228 altsd = get_iface_desc(alts);
229 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
232 if (fmt == subs->cur_audiofmt)
235 /* close the old interface */
236 if (subs->interface >= 0 && subs->interface != fmt->iface) {
237 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
238 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
239 dev->devnum, fmt->iface, fmt->altsetting);
242 subs->interface = -1;
243 subs->altset_idx = 0;
247 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
248 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
249 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
250 dev->devnum, fmt->iface, fmt->altsetting);
253 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
254 subs->interface = fmt->iface;
255 subs->altset_idx = fmt->altset_idx;
258 /* create a data pipe */
259 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
261 subs->datapipe = usb_sndisocpipe(dev, ep);
263 subs->datapipe = usb_rcvisocpipe(dev, ep);
264 subs->datainterval = fmt->datainterval;
265 subs->syncpipe = subs->syncinterval = 0;
266 subs->maxpacksize = fmt->maxpacksize;
267 subs->syncmaxsize = 0;
270 /* we need a sync pipe in async OUT or adaptive IN mode */
271 /* check the number of EP, since some devices have broken
272 * descriptors which fool us. if it has only one EP,
273 * assume it as adaptive-out or sync-in.
275 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
276 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
277 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
278 altsd->bNumEndpoints >= 2) {
279 /* check sync-pipe endpoint */
280 /* ... and check descriptor size before accessing bSynchAddress
281 because there is a version of the SB Audigy 2 NX firmware lacking
282 the audio fields in the endpoint descriptors */
283 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
284 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
285 get_endpoint(alts, 1)->bSynchAddress != 0)) {
286 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
287 dev->devnum, fmt->iface, fmt->altsetting);
290 ep = get_endpoint(alts, 1)->bEndpointAddress;
291 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
292 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
293 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
294 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
295 dev->devnum, fmt->iface, fmt->altsetting);
298 ep &= USB_ENDPOINT_NUMBER_MASK;
300 subs->syncpipe = usb_rcvisocpipe(dev, ep);
302 subs->syncpipe = usb_sndisocpipe(dev, ep);
303 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
304 get_endpoint(alts, 1)->bRefresh >= 1 &&
305 get_endpoint(alts, 1)->bRefresh <= 9)
306 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
307 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
308 subs->syncinterval = 1;
309 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
310 get_endpoint(alts, 1)->bInterval <= 16)
311 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
313 subs->syncinterval = 3;
314 subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
317 /* always fill max packet size */
318 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
321 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
324 subs->cur_audiofmt = fmt;
326 snd_usb_set_format_quirk(subs, fmt);
330 "setting done: format = %d, rate = %d..%d, channels = %d\n",
331 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
333 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
334 subs->datapipe, subs->syncpipe);
343 * allocate a buffer and set the given audio format.
345 * so far we use a physically linear buffer although packetize transfer
346 * doesn't need a continuous area.
347 * if sg buffer is supported on the later version of alsa, we'll follow
350 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
351 struct snd_pcm_hw_params *hw_params)
353 struct snd_usb_substream *subs = substream->runtime->private_data;
354 struct audioformat *fmt;
355 unsigned int channels, rate, format;
358 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
359 params_buffer_bytes(hw_params));
363 format = params_format(hw_params);
364 rate = params_rate(hw_params);
365 channels = params_channels(hw_params);
366 fmt = find_format(subs, format, rate, channels);
368 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
369 format, rate, channels);
373 changed = subs->cur_audiofmt != fmt ||
374 subs->period_bytes != params_period_bytes(hw_params) ||
375 subs->cur_rate != rate;
376 if ((ret = set_format(subs, fmt)) < 0)
379 if (subs->cur_rate != rate) {
380 struct usb_host_interface *alts;
381 struct usb_interface *iface;
382 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
383 alts = &iface->altsetting[fmt->altset_idx];
384 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
387 subs->cur_rate = rate;
391 mutex_lock(&subs->stream->chip->shutdown_mutex);
393 snd_usb_release_substream_urbs(subs, 0);
394 /* influenced: period_bytes, channels, rate, format, */
395 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
396 params_rate(hw_params),
397 snd_pcm_format_physical_width(params_format(hw_params)) *
398 params_channels(hw_params));
399 mutex_unlock(&subs->stream->chip->shutdown_mutex);
408 * reset the audio format and release the buffer
410 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
412 struct snd_usb_substream *subs = substream->runtime->private_data;
414 subs->cur_audiofmt = NULL;
416 subs->period_bytes = 0;
417 mutex_lock(&subs->stream->chip->shutdown_mutex);
418 snd_usb_release_substream_urbs(subs, 0);
419 mutex_unlock(&subs->stream->chip->shutdown_mutex);
420 return snd_pcm_lib_free_vmalloc_buffer(substream);
426 * only a few subtle things...
428 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
430 struct snd_pcm_runtime *runtime = substream->runtime;
431 struct snd_usb_substream *subs = runtime->private_data;
433 if (! subs->cur_audiofmt) {
434 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
438 /* some unit conversions in runtime */
439 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
440 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
442 /* reset the pointer */
443 subs->hwptr_done = 0;
444 subs->transfer_done = 0;
446 subs->last_delay = 0;
447 subs->last_frame_number = 0;
450 return snd_usb_substream_prepare(subs, runtime);
453 static struct snd_pcm_hardware snd_usb_hardware =
455 .info = SNDRV_PCM_INFO_MMAP |
456 SNDRV_PCM_INFO_MMAP_VALID |
457 SNDRV_PCM_INFO_BATCH |
458 SNDRV_PCM_INFO_INTERLEAVED |
459 SNDRV_PCM_INFO_BLOCK_TRANSFER |
460 SNDRV_PCM_INFO_PAUSE,
461 .buffer_bytes_max = 1024 * 1024,
462 .period_bytes_min = 64,
463 .period_bytes_max = 512 * 1024,
468 static int hw_check_valid_format(struct snd_usb_substream *subs,
469 struct snd_pcm_hw_params *params,
470 struct audioformat *fp)
472 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
473 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
474 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
475 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
476 struct snd_mask check_fmts;
479 /* check the format */
480 snd_mask_none(&check_fmts);
481 check_fmts.bits[0] = (u32)fp->formats;
482 check_fmts.bits[1] = (u32)(fp->formats >> 32);
483 snd_mask_intersect(&check_fmts, fmts);
484 if (snd_mask_empty(&check_fmts)) {
485 hwc_debug(" > check: no supported format %d\n", fp->format);
488 /* check the channels */
489 if (fp->channels < ct->min || fp->channels > ct->max) {
490 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
493 /* check the rate is within the range */
494 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
495 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
498 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
499 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
502 /* check whether the period time is >= the data packet interval */
503 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) {
504 ptime = 125 * (1 << fp->datainterval);
505 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
506 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
513 static int hw_rule_rate(struct snd_pcm_hw_params *params,
514 struct snd_pcm_hw_rule *rule)
516 struct snd_usb_substream *subs = rule->private;
518 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
519 unsigned int rmin, rmax;
522 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
525 list_for_each(p, &subs->fmt_list) {
526 struct audioformat *fp;
527 fp = list_entry(p, struct audioformat, list);
528 if (!hw_check_valid_format(subs, params, fp))
531 if (rmin > fp->rate_min)
533 if (rmax < fp->rate_max)
542 hwc_debug(" --> get empty\n");
548 if (it->min < rmin) {
553 if (it->max > rmax) {
558 if (snd_interval_checkempty(it)) {
562 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
567 static int hw_rule_channels(struct snd_pcm_hw_params *params,
568 struct snd_pcm_hw_rule *rule)
570 struct snd_usb_substream *subs = rule->private;
572 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
573 unsigned int rmin, rmax;
576 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
579 list_for_each(p, &subs->fmt_list) {
580 struct audioformat *fp;
581 fp = list_entry(p, struct audioformat, list);
582 if (!hw_check_valid_format(subs, params, fp))
585 if (rmin > fp->channels)
587 if (rmax < fp->channels)
596 hwc_debug(" --> get empty\n");
602 if (it->min < rmin) {
607 if (it->max > rmax) {
612 if (snd_interval_checkempty(it)) {
616 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
620 static int hw_rule_format(struct snd_pcm_hw_params *params,
621 struct snd_pcm_hw_rule *rule)
623 struct snd_usb_substream *subs = rule->private;
625 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
630 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
632 list_for_each(p, &subs->fmt_list) {
633 struct audioformat *fp;
634 fp = list_entry(p, struct audioformat, list);
635 if (!hw_check_valid_format(subs, params, fp))
637 fbits |= fp->formats;
640 oldbits[0] = fmt->bits[0];
641 oldbits[1] = fmt->bits[1];
642 fmt->bits[0] &= (u32)fbits;
643 fmt->bits[1] &= (u32)(fbits >> 32);
644 if (!fmt->bits[0] && !fmt->bits[1]) {
645 hwc_debug(" --> get empty\n");
648 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
649 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
653 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
654 struct snd_pcm_hw_rule *rule)
656 struct snd_usb_substream *subs = rule->private;
657 struct audioformat *fp;
658 struct snd_interval *it;
659 unsigned char min_datainterval;
663 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
664 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
665 min_datainterval = 0xff;
666 list_for_each_entry(fp, &subs->fmt_list, list) {
667 if (!hw_check_valid_format(subs, params, fp))
669 min_datainterval = min(min_datainterval, fp->datainterval);
671 if (min_datainterval == 0xff) {
672 hwc_debug(" --> get empty\n");
676 pmin = 125 * (1 << min_datainterval);
678 if (it->min < pmin) {
683 if (snd_interval_checkempty(it)) {
687 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
692 * If the device supports unusual bit rates, does the request meet these?
694 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
695 struct snd_usb_substream *subs)
697 struct audioformat *fp;
698 int count = 0, needs_knot = 0;
701 list_for_each_entry(fp, &subs->fmt_list, list) {
702 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
704 count += fp->nr_rates;
705 if (fp->rates & SNDRV_PCM_RATE_KNOT)
711 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
712 if (!subs->rate_list.list)
714 subs->rate_list.count = count;
715 subs->rate_list.mask = 0;
717 list_for_each_entry(fp, &subs->fmt_list, list) {
719 for (i = 0; i < fp->nr_rates; i++)
720 subs->rate_list.list[count++] = fp->rate_table[i];
722 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
732 * set up the runtime hardware information.
735 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
738 unsigned int pt, ptmin;
739 int param_period_time_if_needed;
742 runtime->hw.formats = subs->formats;
744 runtime->hw.rate_min = 0x7fffffff;
745 runtime->hw.rate_max = 0;
746 runtime->hw.channels_min = 256;
747 runtime->hw.channels_max = 0;
748 runtime->hw.rates = 0;
750 /* check min/max rates and channels */
751 list_for_each(p, &subs->fmt_list) {
752 struct audioformat *fp;
753 fp = list_entry(p, struct audioformat, list);
754 runtime->hw.rates |= fp->rates;
755 if (runtime->hw.rate_min > fp->rate_min)
756 runtime->hw.rate_min = fp->rate_min;
757 if (runtime->hw.rate_max < fp->rate_max)
758 runtime->hw.rate_max = fp->rate_max;
759 if (runtime->hw.channels_min > fp->channels)
760 runtime->hw.channels_min = fp->channels;
761 if (runtime->hw.channels_max < fp->channels)
762 runtime->hw.channels_max = fp->channels;
763 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
764 /* FIXME: there might be more than one audio formats... */
765 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
768 pt = 125 * (1 << fp->datainterval);
769 ptmin = min(ptmin, pt);
771 err = snd_usb_autoresume(subs->stream->chip);
775 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
776 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
777 /* full speed devices have fixed data packet interval */
780 /* if period time doesn't go below 1 ms, no rules needed */
781 param_period_time_if_needed = -1;
782 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
785 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
787 SNDRV_PCM_HW_PARAM_FORMAT,
788 SNDRV_PCM_HW_PARAM_CHANNELS,
789 param_period_time_if_needed,
792 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
793 hw_rule_channels, subs,
794 SNDRV_PCM_HW_PARAM_FORMAT,
795 SNDRV_PCM_HW_PARAM_RATE,
796 param_period_time_if_needed,
799 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
800 hw_rule_format, subs,
801 SNDRV_PCM_HW_PARAM_RATE,
802 SNDRV_PCM_HW_PARAM_CHANNELS,
803 param_period_time_if_needed,
806 if (param_period_time_if_needed >= 0) {
807 err = snd_pcm_hw_rule_add(runtime, 0,
808 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
809 hw_rule_period_time, subs,
810 SNDRV_PCM_HW_PARAM_FORMAT,
811 SNDRV_PCM_HW_PARAM_CHANNELS,
812 SNDRV_PCM_HW_PARAM_RATE,
817 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
822 snd_usb_autosuspend(subs->stream->chip);
826 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
828 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
829 struct snd_pcm_runtime *runtime = substream->runtime;
830 struct snd_usb_substream *subs = &as->substream[direction];
832 subs->interface = -1;
833 subs->altset_idx = 0;
834 runtime->hw = snd_usb_hardware;
835 runtime->private_data = subs;
836 subs->pcm_substream = substream;
837 /* runtime PM is also done there */
838 return setup_hw_info(runtime, subs);
841 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
843 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
844 struct snd_usb_substream *subs = &as->substream[direction];
846 if (!as->chip->shutdown && subs->interface >= 0) {
847 usb_set_interface(subs->dev, subs->interface, 0);
848 subs->interface = -1;
850 subs->pcm_substream = NULL;
851 snd_usb_autosuspend(subs->stream->chip);
855 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
857 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
860 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
862 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
865 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
867 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
870 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
872 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
875 static struct snd_pcm_ops snd_usb_playback_ops = {
876 .open = snd_usb_playback_open,
877 .close = snd_usb_playback_close,
878 .ioctl = snd_pcm_lib_ioctl,
879 .hw_params = snd_usb_hw_params,
880 .hw_free = snd_usb_hw_free,
881 .prepare = snd_usb_pcm_prepare,
882 .trigger = snd_usb_substream_playback_trigger,
883 .pointer = snd_usb_pcm_pointer,
884 .page = snd_pcm_lib_get_vmalloc_page,
885 .mmap = snd_pcm_lib_mmap_vmalloc,
888 static struct snd_pcm_ops snd_usb_capture_ops = {
889 .open = snd_usb_capture_open,
890 .close = snd_usb_capture_close,
891 .ioctl = snd_pcm_lib_ioctl,
892 .hw_params = snd_usb_hw_params,
893 .hw_free = snd_usb_hw_free,
894 .prepare = snd_usb_pcm_prepare,
895 .trigger = snd_usb_substream_capture_trigger,
896 .pointer = snd_usb_pcm_pointer,
897 .page = snd_pcm_lib_get_vmalloc_page,
898 .mmap = snd_pcm_lib_mmap_vmalloc,
901 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
903 snd_pcm_set_ops(pcm, stream,
904 stream == SNDRV_PCM_STREAM_PLAYBACK ?
905 &snd_usb_playback_ops : &snd_usb_capture_ops);