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/usb.h>
19 #include <linux/usb/audio.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
34 * return the current pcm pointer. just based on the hwptr_done value.
36 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
38 struct snd_usb_substream *subs;
39 unsigned int hwptr_done;
41 subs = (struct snd_usb_substream *)substream->runtime->private_data;
42 spin_lock(&subs->lock);
43 hwptr_done = subs->hwptr_done;
44 spin_unlock(&subs->lock);
45 return hwptr_done / (substream->runtime->frame_bits >> 3);
49 * find a matching audio format
51 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
52 unsigned int rate, unsigned int channels)
55 struct audioformat *found = NULL;
56 int cur_attr = 0, attr;
58 list_for_each(p, &subs->fmt_list) {
59 struct audioformat *fp;
60 fp = list_entry(p, struct audioformat, list);
61 if (!(fp->formats & (1uLL << format)))
63 if (fp->channels != channels)
65 if (rate < fp->rate_min || rate > fp->rate_max)
67 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
69 for (i = 0; i < fp->nr_rates; i++)
70 if (fp->rate_table[i] == rate)
72 if (i >= fp->nr_rates)
75 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
81 /* avoid async out and adaptive in if the other method
82 * supports the same format.
83 * this is a workaround for the case like
84 * M-audio audiophile USB.
86 if (attr != cur_attr) {
87 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
88 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
89 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
90 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
92 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
93 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
94 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
95 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
101 /* find the format with the largest max. packet size */
102 if (fp->maxpacksize > found->maxpacksize) {
112 * initialize the picth control and sample rate
114 int snd_usb_init_pitch(struct usb_device *dev, int iface,
115 struct usb_host_interface *alts,
116 struct audioformat *fmt)
119 unsigned char data[1];
122 ep = get_endpoint(alts, 0)->bEndpointAddress;
123 /* if endpoint has pitch control, enable it */
124 if (fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL) {
126 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
127 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
128 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
129 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
130 dev->devnum, iface, ep);
137 int snd_usb_init_sample_rate(struct usb_device *dev, int iface,
138 struct usb_host_interface *alts,
139 struct audioformat *fmt, int rate)
142 unsigned char data[3];
145 ep = get_endpoint(alts, 0)->bEndpointAddress;
146 /* if endpoint has sampling rate control, set it */
147 if (fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE) {
151 data[2] = rate >> 16;
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_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
155 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
156 dev->devnum, iface, fmt->altsetting, rate, ep);
159 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
160 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
161 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
162 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
163 dev->devnum, iface, fmt->altsetting, ep);
164 return 0; /* some devices don't support reading */
166 crate = data[0] | (data[1] << 8) | (data[2] << 16);
168 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
169 // runtime->rate = crate;
176 * find a matching format and set up the interface
178 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
180 struct usb_device *dev = subs->dev;
181 struct usb_host_interface *alts;
182 struct usb_interface_descriptor *altsd;
183 struct usb_interface *iface;
184 unsigned int ep, attr;
185 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
188 iface = usb_ifnum_to_if(dev, fmt->iface);
191 alts = &iface->altsetting[fmt->altset_idx];
192 altsd = get_iface_desc(alts);
193 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
196 if (fmt == subs->cur_audiofmt)
199 /* close the old interface */
200 if (subs->interface >= 0 && subs->interface != fmt->iface) {
201 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
202 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
203 dev->devnum, fmt->iface, fmt->altsetting);
206 subs->interface = -1;
207 subs->altset_idx = 0;
211 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
212 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
213 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
214 dev->devnum, fmt->iface, fmt->altsetting);
217 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
218 subs->interface = fmt->iface;
219 subs->altset_idx = fmt->altset_idx;
222 /* create a data pipe */
223 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
225 subs->datapipe = usb_sndisocpipe(dev, ep);
227 subs->datapipe = usb_rcvisocpipe(dev, ep);
228 subs->datainterval = fmt->datainterval;
229 subs->syncpipe = subs->syncinterval = 0;
230 subs->maxpacksize = fmt->maxpacksize;
233 /* we need a sync pipe in async OUT or adaptive IN mode */
234 /* check the number of EP, since some devices have broken
235 * descriptors which fool us. if it has only one EP,
236 * assume it as adaptive-out or sync-in.
238 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
239 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
240 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
241 altsd->bNumEndpoints >= 2) {
242 /* check sync-pipe endpoint */
243 /* ... and check descriptor size before accessing bSynchAddress
244 because there is a version of the SB Audigy 2 NX firmware lacking
245 the audio fields in the endpoint descriptors */
246 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
247 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
248 get_endpoint(alts, 1)->bSynchAddress != 0)) {
249 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
250 dev->devnum, fmt->iface, fmt->altsetting);
253 ep = get_endpoint(alts, 1)->bEndpointAddress;
254 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
255 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
256 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
257 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
258 dev->devnum, fmt->iface, fmt->altsetting);
261 ep &= USB_ENDPOINT_NUMBER_MASK;
263 subs->syncpipe = usb_rcvisocpipe(dev, ep);
265 subs->syncpipe = usb_sndisocpipe(dev, ep);
266 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
267 get_endpoint(alts, 1)->bRefresh >= 1 &&
268 get_endpoint(alts, 1)->bRefresh <= 9)
269 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
270 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
271 subs->syncinterval = 1;
272 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
273 get_endpoint(alts, 1)->bInterval <= 16)
274 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
276 subs->syncinterval = 3;
279 /* always fill max packet size */
280 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
283 if ((err = snd_usb_init_pitch(dev, subs->interface, alts, fmt)) < 0)
286 subs->cur_audiofmt = fmt;
288 snd_usb_set_format_quirk(subs, fmt);
292 "setting done: format = %d, rate = %d..%d, channels = %d\n",
293 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
295 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
296 subs->datapipe, subs->syncpipe);
305 * allocate a buffer and set the given audio format.
307 * so far we use a physically linear buffer although packetize transfer
308 * doesn't need a continuous area.
309 * if sg buffer is supported on the later version of alsa, we'll follow
312 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
313 struct snd_pcm_hw_params *hw_params)
315 struct snd_usb_substream *subs = substream->runtime->private_data;
316 struct audioformat *fmt;
317 unsigned int channels, rate, format;
320 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
321 params_buffer_bytes(hw_params));
325 format = params_format(hw_params);
326 rate = params_rate(hw_params);
327 channels = params_channels(hw_params);
328 fmt = find_format(subs, format, rate, channels);
330 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
331 format, rate, channels);
335 changed = subs->cur_audiofmt != fmt ||
336 subs->period_bytes != params_period_bytes(hw_params) ||
337 subs->cur_rate != rate;
338 if ((ret = set_format(subs, fmt)) < 0)
341 if (subs->cur_rate != rate) {
342 struct usb_host_interface *alts;
343 struct usb_interface *iface;
344 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
345 alts = &iface->altsetting[fmt->altset_idx];
346 ret = snd_usb_init_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
349 subs->cur_rate = rate;
354 snd_usb_release_substream_urbs(subs, 0);
355 /* influenced: period_bytes, channels, rate, format, */
356 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
357 params_rate(hw_params),
358 snd_pcm_format_physical_width(params_format(hw_params)) *
359 params_channels(hw_params));
368 * reset the audio format and release the buffer
370 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
372 struct snd_usb_substream *subs = substream->runtime->private_data;
374 subs->cur_audiofmt = NULL;
376 subs->period_bytes = 0;
377 if (!subs->stream->chip->shutdown)
378 snd_usb_release_substream_urbs(subs, 0);
379 return snd_pcm_lib_free_vmalloc_buffer(substream);
385 * only a few subtle things...
387 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
389 struct snd_pcm_runtime *runtime = substream->runtime;
390 struct snd_usb_substream *subs = runtime->private_data;
392 if (! subs->cur_audiofmt) {
393 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
397 /* some unit conversions in runtime */
398 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
399 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
401 /* reset the pointer */
402 subs->hwptr_done = 0;
403 subs->transfer_done = 0;
407 return snd_usb_substream_prepare(subs, runtime);
410 static struct snd_pcm_hardware snd_usb_hardware =
412 .info = SNDRV_PCM_INFO_MMAP |
413 SNDRV_PCM_INFO_MMAP_VALID |
414 SNDRV_PCM_INFO_BATCH |
415 SNDRV_PCM_INFO_INTERLEAVED |
416 SNDRV_PCM_INFO_BLOCK_TRANSFER |
417 SNDRV_PCM_INFO_PAUSE,
418 .buffer_bytes_max = 1024 * 1024,
419 .period_bytes_min = 64,
420 .period_bytes_max = 512 * 1024,
425 static int hw_check_valid_format(struct snd_usb_substream *subs,
426 struct snd_pcm_hw_params *params,
427 struct audioformat *fp)
429 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
430 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
431 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
432 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
433 struct snd_mask check_fmts;
436 /* check the format */
437 snd_mask_none(&check_fmts);
438 check_fmts.bits[0] = (u32)fp->formats;
439 check_fmts.bits[1] = (u32)(fp->formats >> 32);
440 snd_mask_intersect(&check_fmts, fmts);
441 if (snd_mask_empty(&check_fmts)) {
442 hwc_debug(" > check: no supported format %d\n", fp->format);
445 /* check the channels */
446 if (fp->channels < ct->min || fp->channels > ct->max) {
447 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
450 /* check the rate is within the range */
451 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
452 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
455 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
456 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
459 /* check whether the period time is >= the data packet interval */
460 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
461 ptime = 125 * (1 << fp->datainterval);
462 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
463 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
470 static int hw_rule_rate(struct snd_pcm_hw_params *params,
471 struct snd_pcm_hw_rule *rule)
473 struct snd_usb_substream *subs = rule->private;
475 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
476 unsigned int rmin, rmax;
479 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
482 list_for_each(p, &subs->fmt_list) {
483 struct audioformat *fp;
484 fp = list_entry(p, struct audioformat, list);
485 if (!hw_check_valid_format(subs, params, fp))
488 if (rmin > fp->rate_min)
490 if (rmax < fp->rate_max)
499 hwc_debug(" --> get empty\n");
505 if (it->min < rmin) {
510 if (it->max > rmax) {
515 if (snd_interval_checkempty(it)) {
519 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
524 static int hw_rule_channels(struct snd_pcm_hw_params *params,
525 struct snd_pcm_hw_rule *rule)
527 struct snd_usb_substream *subs = rule->private;
529 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
530 unsigned int rmin, rmax;
533 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
536 list_for_each(p, &subs->fmt_list) {
537 struct audioformat *fp;
538 fp = list_entry(p, struct audioformat, list);
539 if (!hw_check_valid_format(subs, params, fp))
542 if (rmin > fp->channels)
544 if (rmax < fp->channels)
553 hwc_debug(" --> get empty\n");
559 if (it->min < rmin) {
564 if (it->max > rmax) {
569 if (snd_interval_checkempty(it)) {
573 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
577 static int hw_rule_format(struct snd_pcm_hw_params *params,
578 struct snd_pcm_hw_rule *rule)
580 struct snd_usb_substream *subs = rule->private;
582 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
587 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
589 list_for_each(p, &subs->fmt_list) {
590 struct audioformat *fp;
591 fp = list_entry(p, struct audioformat, list);
592 if (!hw_check_valid_format(subs, params, fp))
594 fbits |= fp->formats;
597 oldbits[0] = fmt->bits[0];
598 oldbits[1] = fmt->bits[1];
599 fmt->bits[0] &= (u32)fbits;
600 fmt->bits[1] &= (u32)(fbits >> 32);
601 if (!fmt->bits[0] && !fmt->bits[1]) {
602 hwc_debug(" --> get empty\n");
605 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
606 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
610 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
611 struct snd_pcm_hw_rule *rule)
613 struct snd_usb_substream *subs = rule->private;
614 struct audioformat *fp;
615 struct snd_interval *it;
616 unsigned char min_datainterval;
620 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
621 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
622 min_datainterval = 0xff;
623 list_for_each_entry(fp, &subs->fmt_list, list) {
624 if (!hw_check_valid_format(subs, params, fp))
626 min_datainterval = min(min_datainterval, fp->datainterval);
628 if (min_datainterval == 0xff) {
629 hwc_debug(" --> get emtpy\n");
633 pmin = 125 * (1 << min_datainterval);
635 if (it->min < pmin) {
640 if (snd_interval_checkempty(it)) {
644 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
649 * If the device supports unusual bit rates, does the request meet these?
651 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
652 struct snd_usb_substream *subs)
654 struct audioformat *fp;
655 int count = 0, needs_knot = 0;
658 list_for_each_entry(fp, &subs->fmt_list, list) {
659 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
661 count += fp->nr_rates;
662 if (fp->rates & SNDRV_PCM_RATE_KNOT)
668 subs->rate_list.count = count;
669 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
670 subs->rate_list.mask = 0;
672 list_for_each_entry(fp, &subs->fmt_list, list) {
674 for (i = 0; i < fp->nr_rates; i++)
675 subs->rate_list.list[count++] = fp->rate_table[i];
677 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
687 * set up the runtime hardware information.
690 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
693 unsigned int pt, ptmin;
694 int param_period_time_if_needed;
697 runtime->hw.formats = subs->formats;
699 runtime->hw.rate_min = 0x7fffffff;
700 runtime->hw.rate_max = 0;
701 runtime->hw.channels_min = 256;
702 runtime->hw.channels_max = 0;
703 runtime->hw.rates = 0;
705 /* check min/max rates and channels */
706 list_for_each(p, &subs->fmt_list) {
707 struct audioformat *fp;
708 fp = list_entry(p, struct audioformat, list);
709 runtime->hw.rates |= fp->rates;
710 if (runtime->hw.rate_min > fp->rate_min)
711 runtime->hw.rate_min = fp->rate_min;
712 if (runtime->hw.rate_max < fp->rate_max)
713 runtime->hw.rate_max = fp->rate_max;
714 if (runtime->hw.channels_min > fp->channels)
715 runtime->hw.channels_min = fp->channels;
716 if (runtime->hw.channels_max < fp->channels)
717 runtime->hw.channels_max = fp->channels;
718 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
719 /* FIXME: there might be more than one audio formats... */
720 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
723 pt = 125 * (1 << fp->datainterval);
724 ptmin = min(ptmin, pt);
727 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
728 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
729 /* full speed devices have fixed data packet interval */
732 /* if period time doesn't go below 1 ms, no rules needed */
733 param_period_time_if_needed = -1;
734 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
737 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
739 SNDRV_PCM_HW_PARAM_FORMAT,
740 SNDRV_PCM_HW_PARAM_CHANNELS,
741 param_period_time_if_needed,
744 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
745 hw_rule_channels, subs,
746 SNDRV_PCM_HW_PARAM_FORMAT,
747 SNDRV_PCM_HW_PARAM_RATE,
748 param_period_time_if_needed,
751 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
752 hw_rule_format, subs,
753 SNDRV_PCM_HW_PARAM_RATE,
754 SNDRV_PCM_HW_PARAM_CHANNELS,
755 param_period_time_if_needed,
758 if (param_period_time_if_needed >= 0) {
759 err = snd_pcm_hw_rule_add(runtime, 0,
760 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
761 hw_rule_period_time, subs,
762 SNDRV_PCM_HW_PARAM_FORMAT,
763 SNDRV_PCM_HW_PARAM_CHANNELS,
764 SNDRV_PCM_HW_PARAM_RATE,
769 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
774 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
776 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
777 struct snd_pcm_runtime *runtime = substream->runtime;
778 struct snd_usb_substream *subs = &as->substream[direction];
780 subs->interface = -1;
781 subs->altset_idx = 0;
782 runtime->hw = snd_usb_hardware;
783 runtime->private_data = subs;
784 subs->pcm_substream = substream;
785 return setup_hw_info(runtime, subs);
788 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
790 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
791 struct snd_usb_substream *subs = &as->substream[direction];
793 if (!as->chip->shutdown && subs->interface >= 0) {
794 usb_set_interface(subs->dev, subs->interface, 0);
795 subs->interface = -1;
797 subs->pcm_substream = NULL;
801 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
803 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
806 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
808 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
811 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
813 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
816 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
818 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
821 static struct snd_pcm_ops snd_usb_playback_ops = {
822 .open = snd_usb_playback_open,
823 .close = snd_usb_playback_close,
824 .ioctl = snd_pcm_lib_ioctl,
825 .hw_params = snd_usb_hw_params,
826 .hw_free = snd_usb_hw_free,
827 .prepare = snd_usb_pcm_prepare,
828 .trigger = snd_usb_substream_playback_trigger,
829 .pointer = snd_usb_pcm_pointer,
830 .page = snd_pcm_lib_get_vmalloc_page,
831 .mmap = snd_pcm_lib_mmap_vmalloc,
834 static struct snd_pcm_ops snd_usb_capture_ops = {
835 .open = snd_usb_capture_open,
836 .close = snd_usb_capture_close,
837 .ioctl = snd_pcm_lib_ioctl,
838 .hw_params = snd_usb_hw_params,
839 .hw_free = snd_usb_hw_free,
840 .prepare = snd_usb_pcm_prepare,
841 .trigger = snd_usb_substream_capture_trigger,
842 .pointer = snd_usb_pcm_pointer,
843 .page = snd_pcm_lib_get_vmalloc_page,
844 .mmap = snd_pcm_lib_mmap_vmalloc,
847 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
849 snd_pcm_set_ops(pcm, stream,
850 stream == SNDRV_PCM_STREAM_PLAYBACK ?
851 &snd_usb_playback_ops : &snd_usb_capture_ops);