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>
38 * return the current pcm pointer. just based on the hwptr_done value.
40 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
42 struct snd_usb_substream *subs;
43 unsigned int hwptr_done;
45 subs = (struct snd_usb_substream *)substream->runtime->private_data;
46 if (subs->stream->chip->shutdown)
47 return SNDRV_PCM_POS_XRUN;
48 spin_lock(&subs->lock);
49 hwptr_done = subs->hwptr_done;
50 spin_unlock(&subs->lock);
51 return hwptr_done / (substream->runtime->frame_bits >> 3);
55 * find a matching audio format
57 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
58 unsigned int rate, unsigned int channels)
61 struct audioformat *found = NULL;
62 int cur_attr = 0, attr;
64 list_for_each(p, &subs->fmt_list) {
65 struct audioformat *fp;
66 fp = list_entry(p, struct audioformat, list);
67 if (!(fp->formats & (1uLL << format)))
69 if (fp->channels != channels)
71 if (rate < fp->rate_min || rate > fp->rate_max)
73 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
75 for (i = 0; i < fp->nr_rates; i++)
76 if (fp->rate_table[i] == rate)
78 if (i >= fp->nr_rates)
81 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
87 /* avoid async out and adaptive in if the other method
88 * supports the same format.
89 * this is a workaround for the case like
90 * M-audio audiophile USB.
92 if (attr != cur_attr) {
93 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
94 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
95 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
96 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
98 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
99 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
100 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
101 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
107 /* find the format with the largest max. packet size */
108 if (fp->maxpacksize > found->maxpacksize) {
116 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
117 struct usb_host_interface *alts,
118 struct audioformat *fmt)
120 struct usb_device *dev = chip->dev;
122 unsigned char data[1];
125 ep = get_endpoint(alts, 0)->bEndpointAddress;
128 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
129 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
130 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
131 data, sizeof(data), 1000)) < 0) {
132 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
133 dev->devnum, iface, ep);
140 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
141 struct usb_host_interface *alts,
142 struct audioformat *fmt)
144 struct usb_device *dev = chip->dev;
145 unsigned char data[1];
149 ep = get_endpoint(alts, 0)->bEndpointAddress;
152 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
153 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
154 UAC2_EP_CS_PITCH << 8, 0,
155 data, sizeof(data), 1000)) < 0) {
156 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
157 dev->devnum, iface, fmt->altsetting);
165 * initialize the pitch control and sample rate
167 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
168 struct usb_host_interface *alts,
169 struct audioformat *fmt)
171 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
173 /* if endpoint doesn't have pitch control, bail out */
174 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
177 switch (altsd->bInterfaceProtocol) {
180 return init_pitch_v1(chip, iface, alts, fmt);
183 return init_pitch_v2(chip, iface, alts, fmt);
188 * find a matching format and set up the interface
190 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
192 struct usb_device *dev = subs->dev;
193 struct usb_host_interface *alts;
194 struct usb_interface_descriptor *altsd;
195 struct usb_interface *iface;
196 unsigned int ep, attr;
197 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
200 iface = usb_ifnum_to_if(dev, fmt->iface);
203 alts = &iface->altsetting[fmt->altset_idx];
204 altsd = get_iface_desc(alts);
205 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
208 if (fmt == subs->cur_audiofmt)
211 /* close the old interface */
212 if (subs->interface >= 0 && subs->interface != fmt->iface) {
213 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
214 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
215 dev->devnum, fmt->iface, fmt->altsetting);
218 subs->interface = -1;
219 subs->altset_idx = 0;
223 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
224 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
225 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
226 dev->devnum, fmt->iface, fmt->altsetting);
229 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
230 subs->interface = fmt->iface;
231 subs->altset_idx = fmt->altset_idx;
234 /* create a data pipe */
235 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
237 subs->datapipe = usb_sndisocpipe(dev, ep);
239 subs->datapipe = usb_rcvisocpipe(dev, ep);
240 subs->datainterval = fmt->datainterval;
241 subs->syncpipe = subs->syncinterval = 0;
242 subs->maxpacksize = fmt->maxpacksize;
243 subs->syncmaxsize = 0;
246 /* we need a sync pipe in async OUT or adaptive IN mode */
247 /* check the number of EP, since some devices have broken
248 * descriptors which fool us. if it has only one EP,
249 * assume it as adaptive-out or sync-in.
251 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
252 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
253 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
254 altsd->bNumEndpoints >= 2) {
255 /* check sync-pipe endpoint */
256 /* ... and check descriptor size before accessing bSynchAddress
257 because there is a version of the SB Audigy 2 NX firmware lacking
258 the audio fields in the endpoint descriptors */
259 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
260 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
261 get_endpoint(alts, 1)->bSynchAddress != 0)) {
262 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
263 dev->devnum, fmt->iface, fmt->altsetting);
266 ep = get_endpoint(alts, 1)->bEndpointAddress;
267 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
268 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
269 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
270 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
271 dev->devnum, fmt->iface, fmt->altsetting);
274 ep &= USB_ENDPOINT_NUMBER_MASK;
276 subs->syncpipe = usb_rcvisocpipe(dev, ep);
278 subs->syncpipe = usb_sndisocpipe(dev, ep);
279 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
280 get_endpoint(alts, 1)->bRefresh >= 1 &&
281 get_endpoint(alts, 1)->bRefresh <= 9)
282 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
283 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
284 subs->syncinterval = 1;
285 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
286 get_endpoint(alts, 1)->bInterval <= 16)
287 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
289 subs->syncinterval = 3;
290 subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
293 /* always fill max packet size */
294 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
297 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
300 subs->cur_audiofmt = fmt;
302 snd_usb_set_format_quirk(subs, fmt);
306 "setting done: format = %d, rate = %d..%d, channels = %d\n",
307 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
309 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
310 subs->datapipe, subs->syncpipe);
319 * allocate a buffer and set the given audio format.
321 * so far we use a physically linear buffer although packetize transfer
322 * doesn't need a continuous area.
323 * if sg buffer is supported on the later version of alsa, we'll follow
326 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
327 struct snd_pcm_hw_params *hw_params)
329 struct snd_usb_substream *subs = substream->runtime->private_data;
330 struct audioformat *fmt;
331 unsigned int channels, rate, format;
334 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
335 params_buffer_bytes(hw_params));
339 format = params_format(hw_params);
340 rate = params_rate(hw_params);
341 channels = params_channels(hw_params);
342 fmt = find_format(subs, format, rate, channels);
344 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
345 format, rate, channels);
349 changed = subs->cur_audiofmt != fmt ||
350 subs->period_bytes != params_period_bytes(hw_params) ||
351 subs->cur_rate != rate;
353 mutex_lock(&subs->stream->chip->shutdown_mutex);
354 if (subs->stream->chip->shutdown) {
358 if ((ret = set_format(subs, fmt)) < 0)
361 if (subs->cur_rate != rate) {
362 struct usb_host_interface *alts;
363 struct usb_interface *iface;
364 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
365 alts = &iface->altsetting[fmt->altset_idx];
366 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
369 subs->cur_rate = rate;
374 snd_usb_release_substream_urbs(subs, 0);
375 /* influenced: period_bytes, channels, rate, format, */
376 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
377 params_rate(hw_params),
378 snd_pcm_format_physical_width(params_format(hw_params)) *
379 params_channels(hw_params));
383 mutex_unlock(&subs->stream->chip->shutdown_mutex);
390 * reset the audio format and release the buffer
392 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
394 struct snd_usb_substream *subs = substream->runtime->private_data;
396 subs->cur_audiofmt = NULL;
398 subs->period_bytes = 0;
399 mutex_lock(&subs->stream->chip->shutdown_mutex);
400 snd_usb_release_substream_urbs(subs, 0);
401 mutex_unlock(&subs->stream->chip->shutdown_mutex);
402 return snd_pcm_lib_free_vmalloc_buffer(substream);
408 * only a few subtle things...
410 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
412 struct snd_pcm_runtime *runtime = substream->runtime;
413 struct snd_usb_substream *subs = runtime->private_data;
416 if (! subs->cur_audiofmt) {
417 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
421 mutex_lock(&subs->stream->chip->shutdown_mutex);
422 if (subs->stream->chip->shutdown) {
426 /* some unit conversions in runtime */
427 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
428 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
430 /* reset the pointer */
431 subs->hwptr_done = 0;
432 subs->transfer_done = 0;
436 ret = snd_usb_substream_prepare(subs, runtime);
438 mutex_unlock(&subs->stream->chip->shutdown_mutex);
442 static struct snd_pcm_hardware snd_usb_hardware =
444 .info = SNDRV_PCM_INFO_MMAP |
445 SNDRV_PCM_INFO_MMAP_VALID |
446 SNDRV_PCM_INFO_BATCH |
447 SNDRV_PCM_INFO_INTERLEAVED |
448 SNDRV_PCM_INFO_BLOCK_TRANSFER |
449 SNDRV_PCM_INFO_PAUSE,
450 .buffer_bytes_max = 1024 * 1024,
451 .period_bytes_min = 64,
452 .period_bytes_max = 512 * 1024,
457 static int hw_check_valid_format(struct snd_usb_substream *subs,
458 struct snd_pcm_hw_params *params,
459 struct audioformat *fp)
461 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
462 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
463 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
464 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
465 struct snd_mask check_fmts;
468 /* check the format */
469 snd_mask_none(&check_fmts);
470 check_fmts.bits[0] = (u32)fp->formats;
471 check_fmts.bits[1] = (u32)(fp->formats >> 32);
472 snd_mask_intersect(&check_fmts, fmts);
473 if (snd_mask_empty(&check_fmts)) {
474 hwc_debug(" > check: no supported format %d\n", fp->format);
477 /* check the channels */
478 if (fp->channels < ct->min || fp->channels > ct->max) {
479 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
482 /* check the rate is within the range */
483 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
484 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
487 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
488 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
491 /* check whether the period time is >= the data packet interval */
492 if (subs->speed != USB_SPEED_FULL) {
493 ptime = 125 * (1 << fp->datainterval);
494 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
495 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
502 static int hw_rule_rate(struct snd_pcm_hw_params *params,
503 struct snd_pcm_hw_rule *rule)
505 struct snd_usb_substream *subs = rule->private;
507 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
508 unsigned int rmin, rmax;
511 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
514 list_for_each(p, &subs->fmt_list) {
515 struct audioformat *fp;
516 fp = list_entry(p, struct audioformat, list);
517 if (!hw_check_valid_format(subs, params, fp))
520 if (rmin > fp->rate_min)
522 if (rmax < fp->rate_max)
531 hwc_debug(" --> get empty\n");
537 if (it->min < rmin) {
542 if (it->max > rmax) {
547 if (snd_interval_checkempty(it)) {
551 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
556 static int hw_rule_channels(struct snd_pcm_hw_params *params,
557 struct snd_pcm_hw_rule *rule)
559 struct snd_usb_substream *subs = rule->private;
561 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
562 unsigned int rmin, rmax;
565 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
568 list_for_each(p, &subs->fmt_list) {
569 struct audioformat *fp;
570 fp = list_entry(p, struct audioformat, list);
571 if (!hw_check_valid_format(subs, params, fp))
574 if (rmin > fp->channels)
576 if (rmax < fp->channels)
585 hwc_debug(" --> get empty\n");
591 if (it->min < rmin) {
596 if (it->max > rmax) {
601 if (snd_interval_checkempty(it)) {
605 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
609 static int hw_rule_format(struct snd_pcm_hw_params *params,
610 struct snd_pcm_hw_rule *rule)
612 struct snd_usb_substream *subs = rule->private;
614 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
619 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
621 list_for_each(p, &subs->fmt_list) {
622 struct audioformat *fp;
623 fp = list_entry(p, struct audioformat, list);
624 if (!hw_check_valid_format(subs, params, fp))
626 fbits |= fp->formats;
629 oldbits[0] = fmt->bits[0];
630 oldbits[1] = fmt->bits[1];
631 fmt->bits[0] &= (u32)fbits;
632 fmt->bits[1] &= (u32)(fbits >> 32);
633 if (!fmt->bits[0] && !fmt->bits[1]) {
634 hwc_debug(" --> get empty\n");
637 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
638 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
642 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
643 struct snd_pcm_hw_rule *rule)
645 struct snd_usb_substream *subs = rule->private;
646 struct audioformat *fp;
647 struct snd_interval *it;
648 unsigned char min_datainterval;
652 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
653 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
654 min_datainterval = 0xff;
655 list_for_each_entry(fp, &subs->fmt_list, list) {
656 if (!hw_check_valid_format(subs, params, fp))
658 min_datainterval = min(min_datainterval, fp->datainterval);
660 if (min_datainterval == 0xff) {
661 hwc_debug(" --> get empty\n");
665 pmin = 125 * (1 << min_datainterval);
667 if (it->min < pmin) {
672 if (snd_interval_checkempty(it)) {
676 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
681 * If the device supports unusual bit rates, does the request meet these?
683 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
684 struct snd_usb_substream *subs)
686 struct audioformat *fp;
687 int count = 0, needs_knot = 0;
690 kfree(subs->rate_list.list);
691 subs->rate_list.list = NULL;
693 list_for_each_entry(fp, &subs->fmt_list, list) {
694 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
696 count += fp->nr_rates;
697 if (fp->rates & SNDRV_PCM_RATE_KNOT)
703 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
704 if (!subs->rate_list.list)
706 subs->rate_list.count = count;
707 subs->rate_list.mask = 0;
709 list_for_each_entry(fp, &subs->fmt_list, list) {
711 for (i = 0; i < fp->nr_rates; i++)
712 subs->rate_list.list[count++] = fp->rate_table[i];
714 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
724 * set up the runtime hardware information.
727 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
730 unsigned int pt, ptmin;
731 int param_period_time_if_needed;
734 runtime->hw.formats = subs->formats;
736 runtime->hw.rate_min = 0x7fffffff;
737 runtime->hw.rate_max = 0;
738 runtime->hw.channels_min = 256;
739 runtime->hw.channels_max = 0;
740 runtime->hw.rates = 0;
742 /* check min/max rates and channels */
743 list_for_each(p, &subs->fmt_list) {
744 struct audioformat *fp;
745 fp = list_entry(p, struct audioformat, list);
746 runtime->hw.rates |= fp->rates;
747 if (runtime->hw.rate_min > fp->rate_min)
748 runtime->hw.rate_min = fp->rate_min;
749 if (runtime->hw.rate_max < fp->rate_max)
750 runtime->hw.rate_max = fp->rate_max;
751 if (runtime->hw.channels_min > fp->channels)
752 runtime->hw.channels_min = fp->channels;
753 if (runtime->hw.channels_max < fp->channels)
754 runtime->hw.channels_max = fp->channels;
755 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
756 /* FIXME: there might be more than one audio formats... */
757 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
760 pt = 125 * (1 << fp->datainterval);
761 ptmin = min(ptmin, pt);
763 err = snd_usb_autoresume(subs->stream->chip);
767 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
768 if (subs->speed == USB_SPEED_FULL)
769 /* full speed devices have fixed data packet interval */
772 /* if period time doesn't go below 1 ms, no rules needed */
773 param_period_time_if_needed = -1;
774 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
777 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
779 SNDRV_PCM_HW_PARAM_FORMAT,
780 SNDRV_PCM_HW_PARAM_CHANNELS,
781 param_period_time_if_needed,
784 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
785 hw_rule_channels, subs,
786 SNDRV_PCM_HW_PARAM_FORMAT,
787 SNDRV_PCM_HW_PARAM_RATE,
788 param_period_time_if_needed,
791 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
792 hw_rule_format, subs,
793 SNDRV_PCM_HW_PARAM_RATE,
794 SNDRV_PCM_HW_PARAM_CHANNELS,
795 param_period_time_if_needed,
798 if (param_period_time_if_needed >= 0) {
799 err = snd_pcm_hw_rule_add(runtime, 0,
800 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
801 hw_rule_period_time, subs,
802 SNDRV_PCM_HW_PARAM_FORMAT,
803 SNDRV_PCM_HW_PARAM_CHANNELS,
804 SNDRV_PCM_HW_PARAM_RATE,
809 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
814 snd_usb_autosuspend(subs->stream->chip);
818 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
820 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
821 struct snd_pcm_runtime *runtime = substream->runtime;
822 struct snd_usb_substream *subs = &as->substream[direction];
824 subs->interface = -1;
825 subs->altset_idx = 0;
826 runtime->hw = snd_usb_hardware;
827 runtime->private_data = subs;
828 subs->pcm_substream = substream;
829 /* runtime PM is also done there */
830 return setup_hw_info(runtime, subs);
833 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
835 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
836 struct snd_usb_substream *subs = &as->substream[direction];
838 if (!as->chip->shutdown && subs->interface >= 0) {
839 usb_set_interface(subs->dev, subs->interface, 0);
840 subs->interface = -1;
842 subs->pcm_substream = NULL;
843 snd_usb_autosuspend(subs->stream->chip);
847 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
849 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
852 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
854 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
857 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
859 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
862 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
864 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
867 static struct snd_pcm_ops snd_usb_playback_ops = {
868 .open = snd_usb_playback_open,
869 .close = snd_usb_playback_close,
870 .ioctl = snd_pcm_lib_ioctl,
871 .hw_params = snd_usb_hw_params,
872 .hw_free = snd_usb_hw_free,
873 .prepare = snd_usb_pcm_prepare,
874 .trigger = snd_usb_substream_playback_trigger,
875 .pointer = snd_usb_pcm_pointer,
876 .page = snd_pcm_lib_get_vmalloc_page,
877 .mmap = snd_pcm_lib_mmap_vmalloc,
880 static struct snd_pcm_ops snd_usb_capture_ops = {
881 .open = snd_usb_capture_open,
882 .close = snd_usb_capture_close,
883 .ioctl = snd_pcm_lib_ioctl,
884 .hw_params = snd_usb_hw_params,
885 .hw_free = snd_usb_hw_free,
886 .prepare = snd_usb_pcm_prepare,
887 .trigger = snd_usb_substream_capture_trigger,
888 .pointer = snd_usb_pcm_pointer,
889 .page = snd_pcm_lib_get_vmalloc_page,
890 .mmap = snd_pcm_lib_mmap_vmalloc,
893 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
895 snd_pcm_set_ops(pcm, stream,
896 stream == SNDRV_PCM_STREAM_PLAYBACK ?
897 &snd_usb_playback_ops : &snd_usb_capture_ops);