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>
36 * return the current pcm pointer. just based on the hwptr_done value.
38 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
40 struct snd_usb_substream *subs;
41 unsigned int hwptr_done;
43 subs = (struct snd_usb_substream *)substream->runtime->private_data;
44 spin_lock(&subs->lock);
45 hwptr_done = subs->hwptr_done;
46 spin_unlock(&subs->lock);
47 return hwptr_done / (substream->runtime->frame_bits >> 3);
51 * find a matching audio format
53 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
54 unsigned int rate, unsigned int channels)
57 struct audioformat *found = NULL;
58 int cur_attr = 0, attr;
60 list_for_each(p, &subs->fmt_list) {
61 struct audioformat *fp;
62 fp = list_entry(p, struct audioformat, list);
63 if (!(fp->formats & (1uLL << format)))
65 if (fp->channels != channels)
67 if (rate < fp->rate_min || rate > fp->rate_max)
69 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
71 for (i = 0; i < fp->nr_rates; i++)
72 if (fp->rate_table[i] == rate)
74 if (i >= fp->nr_rates)
77 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
83 /* avoid async out and adaptive in if the other method
84 * supports the same format.
85 * this is a workaround for the case like
86 * M-audio audiophile USB.
88 if (attr != cur_attr) {
89 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
90 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
91 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
92 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
94 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
95 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
96 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
97 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
103 /* find the format with the largest max. packet size */
104 if (fp->maxpacksize > found->maxpacksize) {
112 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
113 struct usb_host_interface *alts,
114 struct audioformat *fmt)
116 struct usb_device *dev = chip->dev;
118 unsigned char data[1];
121 ep = get_endpoint(alts, 0)->bEndpointAddress;
123 /* if endpoint doesn't have pitch control, bail out */
124 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
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);
141 * initialize the picth control and sample rate
143 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
144 struct usb_host_interface *alts,
145 struct audioformat *fmt)
147 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
149 switch (altsd->bInterfaceProtocol) {
151 return init_pitch_v1(chip, iface, alts, fmt);
154 /* not implemented yet */
161 static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
162 struct usb_host_interface *alts,
163 struct audioformat *fmt, int rate)
165 struct usb_device *dev = chip->dev;
167 unsigned char data[3];
170 ep = get_endpoint(alts, 0)->bEndpointAddress;
171 /* if endpoint doesn't have sampling rate control, bail out */
172 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE)) {
173 snd_printk(KERN_WARNING "%d:%d:%d: endpoint lacks sample rate attribute bit, cannot set.\n",
174 dev->devnum, iface, fmt->altsetting);
180 data[2] = rate >> 16;
181 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
182 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
183 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
184 data, sizeof(data), 1000)) < 0) {
185 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
186 dev->devnum, iface, fmt->altsetting, rate, ep);
189 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
190 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
191 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
192 data, sizeof(data), 1000)) < 0) {
193 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
194 dev->devnum, iface, fmt->altsetting, ep);
195 return 0; /* some devices don't support reading */
197 crate = data[0] | (data[1] << 8) | (data[2] << 16);
199 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
200 // runtime->rate = crate;
206 static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
207 struct usb_host_interface *alts,
208 struct audioformat *fmt, int rate)
210 struct usb_device *dev = chip->dev;
211 unsigned char data[4];
216 data[2] = rate >> 16;
217 data[3] = rate >> 24;
218 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
219 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
220 UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,
221 data, sizeof(data), 1000)) < 0) {
222 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
223 dev->devnum, iface, fmt->altsetting, rate);
226 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
227 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
228 UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,
229 data, sizeof(data), 1000)) < 0) {
230 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
231 dev->devnum, iface, fmt->altsetting);
234 crate = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
236 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
241 int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
242 struct usb_host_interface *alts,
243 struct audioformat *fmt, int rate)
245 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
247 switch (altsd->bInterfaceProtocol) {
249 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
252 return set_sample_rate_v2(chip, iface, alts, fmt, rate);
259 * find a matching format and set up the interface
261 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
263 struct usb_device *dev = subs->dev;
264 struct usb_host_interface *alts;
265 struct usb_interface_descriptor *altsd;
266 struct usb_interface *iface;
267 unsigned int ep, attr;
268 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
271 iface = usb_ifnum_to_if(dev, fmt->iface);
274 alts = &iface->altsetting[fmt->altset_idx];
275 altsd = get_iface_desc(alts);
276 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
279 if (fmt == subs->cur_audiofmt)
282 /* close the old interface */
283 if (subs->interface >= 0 && subs->interface != fmt->iface) {
284 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
285 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
286 dev->devnum, fmt->iface, fmt->altsetting);
289 subs->interface = -1;
290 subs->altset_idx = 0;
294 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
295 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
296 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
297 dev->devnum, fmt->iface, fmt->altsetting);
300 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
301 subs->interface = fmt->iface;
302 subs->altset_idx = fmt->altset_idx;
305 /* create a data pipe */
306 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
308 subs->datapipe = usb_sndisocpipe(dev, ep);
310 subs->datapipe = usb_rcvisocpipe(dev, ep);
311 subs->datainterval = fmt->datainterval;
312 subs->syncpipe = subs->syncinterval = 0;
313 subs->maxpacksize = fmt->maxpacksize;
316 /* we need a sync pipe in async OUT or adaptive IN mode */
317 /* check the number of EP, since some devices have broken
318 * descriptors which fool us. if it has only one EP,
319 * assume it as adaptive-out or sync-in.
321 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
322 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
323 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
324 altsd->bNumEndpoints >= 2) {
325 /* check sync-pipe endpoint */
326 /* ... and check descriptor size before accessing bSynchAddress
327 because there is a version of the SB Audigy 2 NX firmware lacking
328 the audio fields in the endpoint descriptors */
329 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
330 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
331 get_endpoint(alts, 1)->bSynchAddress != 0)) {
332 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
333 dev->devnum, fmt->iface, fmt->altsetting);
336 ep = get_endpoint(alts, 1)->bEndpointAddress;
337 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
338 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
339 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
340 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
341 dev->devnum, fmt->iface, fmt->altsetting);
344 ep &= USB_ENDPOINT_NUMBER_MASK;
346 subs->syncpipe = usb_rcvisocpipe(dev, ep);
348 subs->syncpipe = usb_sndisocpipe(dev, ep);
349 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
350 get_endpoint(alts, 1)->bRefresh >= 1 &&
351 get_endpoint(alts, 1)->bRefresh <= 9)
352 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
353 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
354 subs->syncinterval = 1;
355 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
356 get_endpoint(alts, 1)->bInterval <= 16)
357 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
359 subs->syncinterval = 3;
362 /* always fill max packet size */
363 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
366 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
369 subs->cur_audiofmt = fmt;
371 snd_usb_set_format_quirk(subs, fmt);
375 "setting done: format = %d, rate = %d..%d, channels = %d\n",
376 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
378 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
379 subs->datapipe, subs->syncpipe);
388 * allocate a buffer and set the given audio format.
390 * so far we use a physically linear buffer although packetize transfer
391 * doesn't need a continuous area.
392 * if sg buffer is supported on the later version of alsa, we'll follow
395 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
396 struct snd_pcm_hw_params *hw_params)
398 struct snd_usb_substream *subs = substream->runtime->private_data;
399 struct audioformat *fmt;
400 unsigned int channels, rate, format;
403 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
404 params_buffer_bytes(hw_params));
408 format = params_format(hw_params);
409 rate = params_rate(hw_params);
410 channels = params_channels(hw_params);
411 fmt = find_format(subs, format, rate, channels);
413 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
414 format, rate, channels);
418 changed = subs->cur_audiofmt != fmt ||
419 subs->period_bytes != params_period_bytes(hw_params) ||
420 subs->cur_rate != rate;
421 if ((ret = set_format(subs, fmt)) < 0)
424 if (subs->cur_rate != rate) {
425 struct usb_host_interface *alts;
426 struct usb_interface *iface;
427 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
428 alts = &iface->altsetting[fmt->altset_idx];
429 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
432 subs->cur_rate = rate;
437 snd_usb_release_substream_urbs(subs, 0);
438 /* influenced: period_bytes, channels, rate, format, */
439 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
440 params_rate(hw_params),
441 snd_pcm_format_physical_width(params_format(hw_params)) *
442 params_channels(hw_params));
451 * reset the audio format and release the buffer
453 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
455 struct snd_usb_substream *subs = substream->runtime->private_data;
457 subs->cur_audiofmt = NULL;
459 subs->period_bytes = 0;
460 if (!subs->stream->chip->shutdown)
461 snd_usb_release_substream_urbs(subs, 0);
462 return snd_pcm_lib_free_vmalloc_buffer(substream);
468 * only a few subtle things...
470 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
472 struct snd_pcm_runtime *runtime = substream->runtime;
473 struct snd_usb_substream *subs = runtime->private_data;
475 if (! subs->cur_audiofmt) {
476 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
480 /* some unit conversions in runtime */
481 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
482 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
484 /* reset the pointer */
485 subs->hwptr_done = 0;
486 subs->transfer_done = 0;
490 return snd_usb_substream_prepare(subs, runtime);
493 static struct snd_pcm_hardware snd_usb_hardware =
495 .info = SNDRV_PCM_INFO_MMAP |
496 SNDRV_PCM_INFO_MMAP_VALID |
497 SNDRV_PCM_INFO_BATCH |
498 SNDRV_PCM_INFO_INTERLEAVED |
499 SNDRV_PCM_INFO_BLOCK_TRANSFER |
500 SNDRV_PCM_INFO_PAUSE,
501 .buffer_bytes_max = 1024 * 1024,
502 .period_bytes_min = 64,
503 .period_bytes_max = 512 * 1024,
508 static int hw_check_valid_format(struct snd_usb_substream *subs,
509 struct snd_pcm_hw_params *params,
510 struct audioformat *fp)
512 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
513 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
514 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
515 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
516 struct snd_mask check_fmts;
519 /* check the format */
520 snd_mask_none(&check_fmts);
521 check_fmts.bits[0] = (u32)fp->formats;
522 check_fmts.bits[1] = (u32)(fp->formats >> 32);
523 snd_mask_intersect(&check_fmts, fmts);
524 if (snd_mask_empty(&check_fmts)) {
525 hwc_debug(" > check: no supported format %d\n", fp->format);
528 /* check the channels */
529 if (fp->channels < ct->min || fp->channels > ct->max) {
530 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
533 /* check the rate is within the range */
534 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
535 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
538 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
539 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
542 /* check whether the period time is >= the data packet interval */
543 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
544 ptime = 125 * (1 << fp->datainterval);
545 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
546 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
553 static int hw_rule_rate(struct snd_pcm_hw_params *params,
554 struct snd_pcm_hw_rule *rule)
556 struct snd_usb_substream *subs = rule->private;
558 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
559 unsigned int rmin, rmax;
562 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
565 list_for_each(p, &subs->fmt_list) {
566 struct audioformat *fp;
567 fp = list_entry(p, struct audioformat, list);
568 if (!hw_check_valid_format(subs, params, fp))
571 if (rmin > fp->rate_min)
573 if (rmax < fp->rate_max)
582 hwc_debug(" --> get empty\n");
588 if (it->min < rmin) {
593 if (it->max > rmax) {
598 if (snd_interval_checkempty(it)) {
602 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
607 static int hw_rule_channels(struct snd_pcm_hw_params *params,
608 struct snd_pcm_hw_rule *rule)
610 struct snd_usb_substream *subs = rule->private;
612 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
613 unsigned int rmin, rmax;
616 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
619 list_for_each(p, &subs->fmt_list) {
620 struct audioformat *fp;
621 fp = list_entry(p, struct audioformat, list);
622 if (!hw_check_valid_format(subs, params, fp))
625 if (rmin > fp->channels)
627 if (rmax < fp->channels)
636 hwc_debug(" --> get empty\n");
642 if (it->min < rmin) {
647 if (it->max > rmax) {
652 if (snd_interval_checkempty(it)) {
656 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
660 static int hw_rule_format(struct snd_pcm_hw_params *params,
661 struct snd_pcm_hw_rule *rule)
663 struct snd_usb_substream *subs = rule->private;
665 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
670 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
672 list_for_each(p, &subs->fmt_list) {
673 struct audioformat *fp;
674 fp = list_entry(p, struct audioformat, list);
675 if (!hw_check_valid_format(subs, params, fp))
677 fbits |= fp->formats;
680 oldbits[0] = fmt->bits[0];
681 oldbits[1] = fmt->bits[1];
682 fmt->bits[0] &= (u32)fbits;
683 fmt->bits[1] &= (u32)(fbits >> 32);
684 if (!fmt->bits[0] && !fmt->bits[1]) {
685 hwc_debug(" --> get empty\n");
688 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
689 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
693 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
694 struct snd_pcm_hw_rule *rule)
696 struct snd_usb_substream *subs = rule->private;
697 struct audioformat *fp;
698 struct snd_interval *it;
699 unsigned char min_datainterval;
703 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
704 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
705 min_datainterval = 0xff;
706 list_for_each_entry(fp, &subs->fmt_list, list) {
707 if (!hw_check_valid_format(subs, params, fp))
709 min_datainterval = min(min_datainterval, fp->datainterval);
711 if (min_datainterval == 0xff) {
712 hwc_debug(" --> get emtpy\n");
716 pmin = 125 * (1 << min_datainterval);
718 if (it->min < pmin) {
723 if (snd_interval_checkempty(it)) {
727 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
732 * If the device supports unusual bit rates, does the request meet these?
734 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
735 struct snd_usb_substream *subs)
737 struct audioformat *fp;
738 int count = 0, needs_knot = 0;
741 list_for_each_entry(fp, &subs->fmt_list, list) {
742 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
744 count += fp->nr_rates;
745 if (fp->rates & SNDRV_PCM_RATE_KNOT)
751 subs->rate_list.count = count;
752 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
753 subs->rate_list.mask = 0;
755 list_for_each_entry(fp, &subs->fmt_list, list) {
757 for (i = 0; i < fp->nr_rates; i++)
758 subs->rate_list.list[count++] = fp->rate_table[i];
760 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
770 * set up the runtime hardware information.
773 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
776 unsigned int pt, ptmin;
777 int param_period_time_if_needed;
780 runtime->hw.formats = subs->formats;
782 runtime->hw.rate_min = 0x7fffffff;
783 runtime->hw.rate_max = 0;
784 runtime->hw.channels_min = 256;
785 runtime->hw.channels_max = 0;
786 runtime->hw.rates = 0;
788 /* check min/max rates and channels */
789 list_for_each(p, &subs->fmt_list) {
790 struct audioformat *fp;
791 fp = list_entry(p, struct audioformat, list);
792 runtime->hw.rates |= fp->rates;
793 if (runtime->hw.rate_min > fp->rate_min)
794 runtime->hw.rate_min = fp->rate_min;
795 if (runtime->hw.rate_max < fp->rate_max)
796 runtime->hw.rate_max = fp->rate_max;
797 if (runtime->hw.channels_min > fp->channels)
798 runtime->hw.channels_min = fp->channels;
799 if (runtime->hw.channels_max < fp->channels)
800 runtime->hw.channels_max = fp->channels;
801 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
802 /* FIXME: there might be more than one audio formats... */
803 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
806 pt = 125 * (1 << fp->datainterval);
807 ptmin = min(ptmin, pt);
810 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
811 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
812 /* full speed devices have fixed data packet interval */
815 /* if period time doesn't go below 1 ms, no rules needed */
816 param_period_time_if_needed = -1;
817 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
820 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
822 SNDRV_PCM_HW_PARAM_FORMAT,
823 SNDRV_PCM_HW_PARAM_CHANNELS,
824 param_period_time_if_needed,
827 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
828 hw_rule_channels, subs,
829 SNDRV_PCM_HW_PARAM_FORMAT,
830 SNDRV_PCM_HW_PARAM_RATE,
831 param_period_time_if_needed,
834 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
835 hw_rule_format, subs,
836 SNDRV_PCM_HW_PARAM_RATE,
837 SNDRV_PCM_HW_PARAM_CHANNELS,
838 param_period_time_if_needed,
841 if (param_period_time_if_needed >= 0) {
842 err = snd_pcm_hw_rule_add(runtime, 0,
843 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
844 hw_rule_period_time, subs,
845 SNDRV_PCM_HW_PARAM_FORMAT,
846 SNDRV_PCM_HW_PARAM_CHANNELS,
847 SNDRV_PCM_HW_PARAM_RATE,
852 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
857 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
859 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
860 struct snd_pcm_runtime *runtime = substream->runtime;
861 struct snd_usb_substream *subs = &as->substream[direction];
863 subs->interface = -1;
864 subs->altset_idx = 0;
865 runtime->hw = snd_usb_hardware;
866 runtime->private_data = subs;
867 subs->pcm_substream = substream;
868 return setup_hw_info(runtime, subs);
871 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
873 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
874 struct snd_usb_substream *subs = &as->substream[direction];
876 if (!as->chip->shutdown && subs->interface >= 0) {
877 usb_set_interface(subs->dev, subs->interface, 0);
878 subs->interface = -1;
880 subs->pcm_substream = NULL;
884 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
886 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
889 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
891 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
894 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
896 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
899 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
901 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
904 static struct snd_pcm_ops snd_usb_playback_ops = {
905 .open = snd_usb_playback_open,
906 .close = snd_usb_playback_close,
907 .ioctl = snd_pcm_lib_ioctl,
908 .hw_params = snd_usb_hw_params,
909 .hw_free = snd_usb_hw_free,
910 .prepare = snd_usb_pcm_prepare,
911 .trigger = snd_usb_substream_playback_trigger,
912 .pointer = snd_usb_pcm_pointer,
913 .page = snd_pcm_lib_get_vmalloc_page,
914 .mmap = snd_pcm_lib_mmap_vmalloc,
917 static struct snd_pcm_ops snd_usb_capture_ops = {
918 .open = snd_usb_capture_open,
919 .close = snd_usb_capture_close,
920 .ioctl = snd_pcm_lib_ioctl,
921 .hw_params = snd_usb_hw_params,
922 .hw_free = snd_usb_hw_free,
923 .prepare = snd_usb_pcm_prepare,
924 .trigger = snd_usb_substream_capture_trigger,
925 .pointer = snd_usb_pcm_pointer,
926 .page = snd_pcm_lib_get_vmalloc_page,
927 .mmap = snd_pcm_lib_mmap_vmalloc,
930 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
932 snd_pcm_set_ops(pcm, stream,
933 stream == SNDRV_PCM_STREAM_PLAYBACK ?
934 &snd_usb_playback_ops : &snd_usb_capture_ops);