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;
124 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
125 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
126 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
127 data, sizeof(data), 1000)) < 0) {
128 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
129 dev->devnum, iface, ep);
136 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
137 struct usb_host_interface *alts,
138 struct audioformat *fmt)
140 struct usb_device *dev = chip->dev;
141 unsigned char data[1];
145 ep = get_endpoint(alts, 0)->bEndpointAddress;
148 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
149 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
150 UAC2_EP_CS_PITCH << 8, 0,
151 data, sizeof(data), 1000)) < 0) {
152 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
153 dev->devnum, iface, fmt->altsetting);
161 * initialize the pitch control and sample rate
163 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
164 struct usb_host_interface *alts,
165 struct audioformat *fmt)
167 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
169 /* if endpoint doesn't have pitch control, bail out */
170 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
173 switch (altsd->bInterfaceProtocol) {
175 return init_pitch_v1(chip, iface, alts, fmt);
178 return init_pitch_v2(chip, iface, alts, fmt);
184 static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
185 struct usb_host_interface *alts,
186 struct audioformat *fmt, int rate)
188 struct usb_device *dev = chip->dev;
190 unsigned char data[3];
193 ep = get_endpoint(alts, 0)->bEndpointAddress;
194 /* if endpoint doesn't have sampling rate control, bail out */
195 if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE)) {
196 snd_printk(KERN_WARNING "%d:%d:%d: endpoint lacks sample rate attribute bit, cannot set.\n",
197 dev->devnum, iface, fmt->altsetting);
203 data[2] = rate >> 16;
204 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
205 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
206 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
207 data, sizeof(data), 1000)) < 0) {
208 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
209 dev->devnum, iface, fmt->altsetting, rate, ep);
212 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
213 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
214 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
215 data, sizeof(data), 1000)) < 0) {
216 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
217 dev->devnum, iface, fmt->altsetting, ep);
218 return 0; /* some devices don't support reading */
220 crate = data[0] | (data[1] << 8) | (data[2] << 16);
222 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
223 // runtime->rate = crate;
229 static int set_sample_rate_v2(struct snd_usb_audio *chip, int iface,
230 struct usb_host_interface *alts,
231 struct audioformat *fmt, int rate)
233 struct usb_device *dev = chip->dev;
234 unsigned char data[4];
239 data[2] = rate >> 16;
240 data[3] = rate >> 24;
241 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
242 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
243 UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,
244 data, sizeof(data), 1000)) < 0) {
245 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d (v2)\n",
246 dev->devnum, iface, fmt->altsetting, rate);
249 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
250 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
251 UAC2_CS_CONTROL_SAM_FREQ << 8, chip->clock_id << 8,
252 data, sizeof(data), 1000)) < 0) {
253 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq (v2)\n",
254 dev->devnum, iface, fmt->altsetting);
257 crate = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
259 snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
264 int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
265 struct usb_host_interface *alts,
266 struct audioformat *fmt, int rate)
268 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
270 switch (altsd->bInterfaceProtocol) {
272 return set_sample_rate_v1(chip, iface, alts, fmt, rate);
275 return set_sample_rate_v2(chip, iface, alts, fmt, rate);
282 * find a matching format and set up the interface
284 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
286 struct usb_device *dev = subs->dev;
287 struct usb_host_interface *alts;
288 struct usb_interface_descriptor *altsd;
289 struct usb_interface *iface;
290 unsigned int ep, attr;
291 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
294 iface = usb_ifnum_to_if(dev, fmt->iface);
297 alts = &iface->altsetting[fmt->altset_idx];
298 altsd = get_iface_desc(alts);
299 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
302 if (fmt == subs->cur_audiofmt)
305 /* close the old interface */
306 if (subs->interface >= 0 && subs->interface != fmt->iface) {
307 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
308 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
309 dev->devnum, fmt->iface, fmt->altsetting);
312 subs->interface = -1;
313 subs->altset_idx = 0;
317 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
318 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
319 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
320 dev->devnum, fmt->iface, fmt->altsetting);
323 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
324 subs->interface = fmt->iface;
325 subs->altset_idx = fmt->altset_idx;
328 /* create a data pipe */
329 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
331 subs->datapipe = usb_sndisocpipe(dev, ep);
333 subs->datapipe = usb_rcvisocpipe(dev, ep);
334 subs->datainterval = fmt->datainterval;
335 subs->syncpipe = subs->syncinterval = 0;
336 subs->maxpacksize = fmt->maxpacksize;
339 /* we need a sync pipe in async OUT or adaptive IN mode */
340 /* check the number of EP, since some devices have broken
341 * descriptors which fool us. if it has only one EP,
342 * assume it as adaptive-out or sync-in.
344 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
345 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
346 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
347 altsd->bNumEndpoints >= 2) {
348 /* check sync-pipe endpoint */
349 /* ... and check descriptor size before accessing bSynchAddress
350 because there is a version of the SB Audigy 2 NX firmware lacking
351 the audio fields in the endpoint descriptors */
352 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
353 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
354 get_endpoint(alts, 1)->bSynchAddress != 0)) {
355 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
356 dev->devnum, fmt->iface, fmt->altsetting);
359 ep = get_endpoint(alts, 1)->bEndpointAddress;
360 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
361 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
362 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
363 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
364 dev->devnum, fmt->iface, fmt->altsetting);
367 ep &= USB_ENDPOINT_NUMBER_MASK;
369 subs->syncpipe = usb_rcvisocpipe(dev, ep);
371 subs->syncpipe = usb_sndisocpipe(dev, ep);
372 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
373 get_endpoint(alts, 1)->bRefresh >= 1 &&
374 get_endpoint(alts, 1)->bRefresh <= 9)
375 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
376 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
377 subs->syncinterval = 1;
378 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
379 get_endpoint(alts, 1)->bInterval <= 16)
380 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
382 subs->syncinterval = 3;
385 /* always fill max packet size */
386 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
389 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
392 subs->cur_audiofmt = fmt;
394 snd_usb_set_format_quirk(subs, fmt);
398 "setting done: format = %d, rate = %d..%d, channels = %d\n",
399 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
401 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
402 subs->datapipe, subs->syncpipe);
411 * allocate a buffer and set the given audio format.
413 * so far we use a physically linear buffer although packetize transfer
414 * doesn't need a continuous area.
415 * if sg buffer is supported on the later version of alsa, we'll follow
418 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
419 struct snd_pcm_hw_params *hw_params)
421 struct snd_usb_substream *subs = substream->runtime->private_data;
422 struct audioformat *fmt;
423 unsigned int channels, rate, format;
426 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
427 params_buffer_bytes(hw_params));
431 format = params_format(hw_params);
432 rate = params_rate(hw_params);
433 channels = params_channels(hw_params);
434 fmt = find_format(subs, format, rate, channels);
436 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
437 format, rate, channels);
441 changed = subs->cur_audiofmt != fmt ||
442 subs->period_bytes != params_period_bytes(hw_params) ||
443 subs->cur_rate != rate;
444 if ((ret = set_format(subs, fmt)) < 0)
447 if (subs->cur_rate != rate) {
448 struct usb_host_interface *alts;
449 struct usb_interface *iface;
450 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
451 alts = &iface->altsetting[fmt->altset_idx];
452 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
455 subs->cur_rate = rate;
460 snd_usb_release_substream_urbs(subs, 0);
461 /* influenced: period_bytes, channels, rate, format, */
462 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
463 params_rate(hw_params),
464 snd_pcm_format_physical_width(params_format(hw_params)) *
465 params_channels(hw_params));
474 * reset the audio format and release the buffer
476 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
478 struct snd_usb_substream *subs = substream->runtime->private_data;
480 subs->cur_audiofmt = NULL;
482 subs->period_bytes = 0;
483 if (!subs->stream->chip->shutdown)
484 snd_usb_release_substream_urbs(subs, 0);
485 return snd_pcm_lib_free_vmalloc_buffer(substream);
491 * only a few subtle things...
493 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
495 struct snd_pcm_runtime *runtime = substream->runtime;
496 struct snd_usb_substream *subs = runtime->private_data;
498 if (! subs->cur_audiofmt) {
499 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
503 /* some unit conversions in runtime */
504 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
505 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
507 /* reset the pointer */
508 subs->hwptr_done = 0;
509 subs->transfer_done = 0;
513 return snd_usb_substream_prepare(subs, runtime);
516 static struct snd_pcm_hardware snd_usb_hardware =
518 .info = SNDRV_PCM_INFO_MMAP |
519 SNDRV_PCM_INFO_MMAP_VALID |
520 SNDRV_PCM_INFO_BATCH |
521 SNDRV_PCM_INFO_INTERLEAVED |
522 SNDRV_PCM_INFO_BLOCK_TRANSFER |
523 SNDRV_PCM_INFO_PAUSE,
524 .buffer_bytes_max = 1024 * 1024,
525 .period_bytes_min = 64,
526 .period_bytes_max = 512 * 1024,
531 static int hw_check_valid_format(struct snd_usb_substream *subs,
532 struct snd_pcm_hw_params *params,
533 struct audioformat *fp)
535 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
536 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
537 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
538 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
539 struct snd_mask check_fmts;
542 /* check the format */
543 snd_mask_none(&check_fmts);
544 check_fmts.bits[0] = (u32)fp->formats;
545 check_fmts.bits[1] = (u32)(fp->formats >> 32);
546 snd_mask_intersect(&check_fmts, fmts);
547 if (snd_mask_empty(&check_fmts)) {
548 hwc_debug(" > check: no supported format %d\n", fp->format);
551 /* check the channels */
552 if (fp->channels < ct->min || fp->channels > ct->max) {
553 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
556 /* check the rate is within the range */
557 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
558 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
561 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
562 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
565 /* check whether the period time is >= the data packet interval */
566 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
567 ptime = 125 * (1 << fp->datainterval);
568 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
569 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
576 static int hw_rule_rate(struct snd_pcm_hw_params *params,
577 struct snd_pcm_hw_rule *rule)
579 struct snd_usb_substream *subs = rule->private;
581 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
582 unsigned int rmin, rmax;
585 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
588 list_for_each(p, &subs->fmt_list) {
589 struct audioformat *fp;
590 fp = list_entry(p, struct audioformat, list);
591 if (!hw_check_valid_format(subs, params, fp))
594 if (rmin > fp->rate_min)
596 if (rmax < fp->rate_max)
605 hwc_debug(" --> get empty\n");
611 if (it->min < rmin) {
616 if (it->max > rmax) {
621 if (snd_interval_checkempty(it)) {
625 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
630 static int hw_rule_channels(struct snd_pcm_hw_params *params,
631 struct snd_pcm_hw_rule *rule)
633 struct snd_usb_substream *subs = rule->private;
635 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
636 unsigned int rmin, rmax;
639 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
642 list_for_each(p, &subs->fmt_list) {
643 struct audioformat *fp;
644 fp = list_entry(p, struct audioformat, list);
645 if (!hw_check_valid_format(subs, params, fp))
648 if (rmin > fp->channels)
650 if (rmax < fp->channels)
659 hwc_debug(" --> get empty\n");
665 if (it->min < rmin) {
670 if (it->max > rmax) {
675 if (snd_interval_checkempty(it)) {
679 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
683 static int hw_rule_format(struct snd_pcm_hw_params *params,
684 struct snd_pcm_hw_rule *rule)
686 struct snd_usb_substream *subs = rule->private;
688 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
693 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
695 list_for_each(p, &subs->fmt_list) {
696 struct audioformat *fp;
697 fp = list_entry(p, struct audioformat, list);
698 if (!hw_check_valid_format(subs, params, fp))
700 fbits |= fp->formats;
703 oldbits[0] = fmt->bits[0];
704 oldbits[1] = fmt->bits[1];
705 fmt->bits[0] &= (u32)fbits;
706 fmt->bits[1] &= (u32)(fbits >> 32);
707 if (!fmt->bits[0] && !fmt->bits[1]) {
708 hwc_debug(" --> get empty\n");
711 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
712 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
716 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
717 struct snd_pcm_hw_rule *rule)
719 struct snd_usb_substream *subs = rule->private;
720 struct audioformat *fp;
721 struct snd_interval *it;
722 unsigned char min_datainterval;
726 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
727 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
728 min_datainterval = 0xff;
729 list_for_each_entry(fp, &subs->fmt_list, list) {
730 if (!hw_check_valid_format(subs, params, fp))
732 min_datainterval = min(min_datainterval, fp->datainterval);
734 if (min_datainterval == 0xff) {
735 hwc_debug(" --> get emtpy\n");
739 pmin = 125 * (1 << min_datainterval);
741 if (it->min < pmin) {
746 if (snd_interval_checkempty(it)) {
750 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
755 * If the device supports unusual bit rates, does the request meet these?
757 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
758 struct snd_usb_substream *subs)
760 struct audioformat *fp;
761 int count = 0, needs_knot = 0;
764 list_for_each_entry(fp, &subs->fmt_list, list) {
765 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
767 count += fp->nr_rates;
768 if (fp->rates & SNDRV_PCM_RATE_KNOT)
774 subs->rate_list.count = count;
775 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
776 subs->rate_list.mask = 0;
778 list_for_each_entry(fp, &subs->fmt_list, list) {
780 for (i = 0; i < fp->nr_rates; i++)
781 subs->rate_list.list[count++] = fp->rate_table[i];
783 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
793 * set up the runtime hardware information.
796 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
799 unsigned int pt, ptmin;
800 int param_period_time_if_needed;
803 runtime->hw.formats = subs->formats;
805 runtime->hw.rate_min = 0x7fffffff;
806 runtime->hw.rate_max = 0;
807 runtime->hw.channels_min = 256;
808 runtime->hw.channels_max = 0;
809 runtime->hw.rates = 0;
811 /* check min/max rates and channels */
812 list_for_each(p, &subs->fmt_list) {
813 struct audioformat *fp;
814 fp = list_entry(p, struct audioformat, list);
815 runtime->hw.rates |= fp->rates;
816 if (runtime->hw.rate_min > fp->rate_min)
817 runtime->hw.rate_min = fp->rate_min;
818 if (runtime->hw.rate_max < fp->rate_max)
819 runtime->hw.rate_max = fp->rate_max;
820 if (runtime->hw.channels_min > fp->channels)
821 runtime->hw.channels_min = fp->channels;
822 if (runtime->hw.channels_max < fp->channels)
823 runtime->hw.channels_max = fp->channels;
824 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
825 /* FIXME: there might be more than one audio formats... */
826 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
829 pt = 125 * (1 << fp->datainterval);
830 ptmin = min(ptmin, pt);
833 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
834 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
835 /* full speed devices have fixed data packet interval */
838 /* if period time doesn't go below 1 ms, no rules needed */
839 param_period_time_if_needed = -1;
840 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
843 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
845 SNDRV_PCM_HW_PARAM_FORMAT,
846 SNDRV_PCM_HW_PARAM_CHANNELS,
847 param_period_time_if_needed,
850 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
851 hw_rule_channels, subs,
852 SNDRV_PCM_HW_PARAM_FORMAT,
853 SNDRV_PCM_HW_PARAM_RATE,
854 param_period_time_if_needed,
857 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
858 hw_rule_format, subs,
859 SNDRV_PCM_HW_PARAM_RATE,
860 SNDRV_PCM_HW_PARAM_CHANNELS,
861 param_period_time_if_needed,
864 if (param_period_time_if_needed >= 0) {
865 err = snd_pcm_hw_rule_add(runtime, 0,
866 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
867 hw_rule_period_time, subs,
868 SNDRV_PCM_HW_PARAM_FORMAT,
869 SNDRV_PCM_HW_PARAM_CHANNELS,
870 SNDRV_PCM_HW_PARAM_RATE,
875 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
880 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
882 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
883 struct snd_pcm_runtime *runtime = substream->runtime;
884 struct snd_usb_substream *subs = &as->substream[direction];
886 subs->interface = -1;
887 subs->altset_idx = 0;
888 runtime->hw = snd_usb_hardware;
889 runtime->private_data = subs;
890 subs->pcm_substream = substream;
891 return setup_hw_info(runtime, subs);
894 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
896 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
897 struct snd_usb_substream *subs = &as->substream[direction];
899 if (!as->chip->shutdown && subs->interface >= 0) {
900 usb_set_interface(subs->dev, subs->interface, 0);
901 subs->interface = -1;
903 subs->pcm_substream = NULL;
907 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
909 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
912 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
914 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
917 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
919 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
922 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
924 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
927 static struct snd_pcm_ops snd_usb_playback_ops = {
928 .open = snd_usb_playback_open,
929 .close = snd_usb_playback_close,
930 .ioctl = snd_pcm_lib_ioctl,
931 .hw_params = snd_usb_hw_params,
932 .hw_free = snd_usb_hw_free,
933 .prepare = snd_usb_pcm_prepare,
934 .trigger = snd_usb_substream_playback_trigger,
935 .pointer = snd_usb_pcm_pointer,
936 .page = snd_pcm_lib_get_vmalloc_page,
937 .mmap = snd_pcm_lib_mmap_vmalloc,
940 static struct snd_pcm_ops snd_usb_capture_ops = {
941 .open = snd_usb_capture_open,
942 .close = snd_usb_capture_close,
943 .ioctl = snd_pcm_lib_ioctl,
944 .hw_params = snd_usb_hw_params,
945 .hw_free = snd_usb_hw_free,
946 .prepare = snd_usb_pcm_prepare,
947 .trigger = snd_usb_substream_capture_trigger,
948 .pointer = snd_usb_pcm_pointer,
949 .page = snd_pcm_lib_get_vmalloc_page,
950 .mmap = snd_pcm_lib_mmap_vmalloc,
953 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
955 snd_pcm_set_ops(pcm, stream,
956 stream == SNDRV_PCM_STREAM_PLAYBACK ?
957 &snd_usb_playback_ops : &snd_usb_capture_ops);