GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / usb / pcm.c
blobc7f73331e4e13963948bda6a2f090b2005aecb1e
1 /*
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>
27 #include "usbaudio.h"
28 #include "card.h"
29 #include "quirks.h"
30 #include "debug.h"
31 #include "urb.h"
32 #include "helper.h"
33 #include "pcm.h"
34 #include "clock.h"
37 * return the current pcm pointer. just based on the hwptr_done value.
39 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
41 struct snd_usb_substream *subs;
42 unsigned int hwptr_done;
44 subs = (struct snd_usb_substream *)substream->runtime->private_data;
45 spin_lock(&subs->lock);
46 hwptr_done = subs->hwptr_done;
47 spin_unlock(&subs->lock);
48 return hwptr_done / (substream->runtime->frame_bits >> 3);
52 * find a matching audio format
54 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
55 unsigned int rate, unsigned int channels)
57 struct list_head *p;
58 struct audioformat *found = NULL;
59 int cur_attr = 0, attr;
61 list_for_each(p, &subs->fmt_list) {
62 struct audioformat *fp;
63 fp = list_entry(p, struct audioformat, list);
64 if (!(fp->formats & (1uLL << format)))
65 continue;
66 if (fp->channels != channels)
67 continue;
68 if (rate < fp->rate_min || rate > fp->rate_max)
69 continue;
70 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
71 unsigned int i;
72 for (i = 0; i < fp->nr_rates; i++)
73 if (fp->rate_table[i] == rate)
74 break;
75 if (i >= fp->nr_rates)
76 continue;
78 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
79 if (! found) {
80 found = fp;
81 cur_attr = attr;
82 continue;
84 if (attr != cur_attr) {
85 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
86 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
87 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
88 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
89 continue;
90 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
91 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
92 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
93 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
94 found = fp;
95 cur_attr = attr;
96 continue;
99 /* find the format with the largest max. packet size */
100 if (fp->maxpacksize > found->maxpacksize) {
101 found = fp;
102 cur_attr = attr;
105 return found;
108 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
109 struct usb_host_interface *alts,
110 struct audioformat *fmt)
112 struct usb_device *dev = chip->dev;
113 unsigned int ep;
114 unsigned char data[1];
115 int err;
117 ep = get_endpoint(alts, 0)->bEndpointAddress;
119 data[0] = 1;
120 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
121 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
122 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
123 data, sizeof(data), 1000)) < 0) {
124 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
125 dev->devnum, iface, ep);
126 return err;
129 return 0;
132 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
133 struct usb_host_interface *alts,
134 struct audioformat *fmt)
136 struct usb_device *dev = chip->dev;
137 unsigned char data[1];
138 unsigned int ep;
139 int err;
141 ep = get_endpoint(alts, 0)->bEndpointAddress;
143 data[0] = 1;
144 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
145 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
146 UAC2_EP_CS_PITCH << 8, 0,
147 data, sizeof(data), 1000)) < 0) {
148 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
149 dev->devnum, iface, fmt->altsetting);
150 return err;
153 return 0;
157 * initialize the pitch control and sample rate
159 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
160 struct usb_host_interface *alts,
161 struct audioformat *fmt)
163 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
165 /* if endpoint doesn't have pitch control, bail out */
166 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
167 return 0;
169 switch (altsd->bInterfaceProtocol) {
170 case UAC_VERSION_1:
171 default:
172 return init_pitch_v1(chip, iface, alts, fmt);
174 case UAC_VERSION_2:
175 return init_pitch_v2(chip, iface, alts, fmt);
180 * find a matching format and set up the interface
182 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
184 struct usb_device *dev = subs->dev;
185 struct usb_host_interface *alts;
186 struct usb_interface_descriptor *altsd;
187 struct usb_interface *iface;
188 unsigned int ep, attr;
189 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
190 int err;
192 iface = usb_ifnum_to_if(dev, fmt->iface);
193 if (WARN_ON(!iface))
194 return -EINVAL;
195 alts = &iface->altsetting[fmt->altset_idx];
196 altsd = get_iface_desc(alts);
197 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
198 return -EINVAL;
200 if (fmt == subs->cur_audiofmt)
201 return 0;
203 /* close the old interface */
204 if (subs->interface >= 0 && subs->interface != fmt->iface) {
205 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
206 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
207 dev->devnum, fmt->iface, fmt->altsetting);
208 return -EIO;
210 subs->interface = -1;
211 subs->altset_idx = 0;
214 /* set interface */
215 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
216 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
217 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
218 dev->devnum, fmt->iface, fmt->altsetting);
219 return -EIO;
221 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
222 subs->interface = fmt->iface;
223 subs->altset_idx = fmt->altset_idx;
226 /* create a data pipe */
227 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
228 if (is_playback)
229 subs->datapipe = usb_sndisocpipe(dev, ep);
230 else
231 subs->datapipe = usb_rcvisocpipe(dev, ep);
232 subs->datainterval = fmt->datainterval;
233 subs->syncpipe = subs->syncinterval = 0;
234 subs->maxpacksize = fmt->maxpacksize;
235 subs->fill_max = 0;
237 /* we need a sync pipe in async OUT or adaptive IN mode */
238 /* check the number of EP, since some devices have broken
239 * descriptors which fool us. if it has only one EP,
240 * assume it as adaptive-out or sync-in.
242 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
243 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
244 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
245 altsd->bNumEndpoints >= 2) {
246 /* check sync-pipe endpoint */
247 /* ... and check descriptor size before accessing bSynchAddress
248 because there is a version of the SB Audigy 2 NX firmware lacking
249 the audio fields in the endpoint descriptors */
250 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
251 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
252 get_endpoint(alts, 1)->bSynchAddress != 0)) {
253 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
254 dev->devnum, fmt->iface, fmt->altsetting);
255 return -EINVAL;
257 ep = get_endpoint(alts, 1)->bEndpointAddress;
258 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
259 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
260 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
261 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
262 dev->devnum, fmt->iface, fmt->altsetting);
263 return -EINVAL;
265 ep &= USB_ENDPOINT_NUMBER_MASK;
266 if (is_playback)
267 subs->syncpipe = usb_rcvisocpipe(dev, ep);
268 else
269 subs->syncpipe = usb_sndisocpipe(dev, ep);
270 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
271 get_endpoint(alts, 1)->bRefresh >= 1 &&
272 get_endpoint(alts, 1)->bRefresh <= 9)
273 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
274 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
275 subs->syncinterval = 1;
276 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
277 get_endpoint(alts, 1)->bInterval <= 16)
278 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
279 else
280 subs->syncinterval = 3;
283 /* always fill max packet size */
284 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
285 subs->fill_max = 1;
287 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
288 return err;
290 subs->cur_audiofmt = fmt;
292 snd_usb_set_format_quirk(subs, fmt);
295 return 0;
299 * hw_params callback
301 * allocate a buffer and set the given audio format.
303 * so far we use a physically linear buffer although packetize transfer
304 * doesn't need a continuous area.
305 * if sg buffer is supported on the later version of alsa, we'll follow
306 * that.
308 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
309 struct snd_pcm_hw_params *hw_params)
311 struct snd_usb_substream *subs = substream->runtime->private_data;
312 struct audioformat *fmt;
313 unsigned int channels, rate, format;
314 int ret, changed;
316 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
317 params_buffer_bytes(hw_params));
318 if (ret < 0)
319 return ret;
321 format = params_format(hw_params);
322 rate = params_rate(hw_params);
323 channels = params_channels(hw_params);
324 fmt = find_format(subs, format, rate, channels);
325 if (!fmt) {
326 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
327 format, rate, channels);
328 return -EINVAL;
331 changed = subs->cur_audiofmt != fmt ||
332 subs->period_bytes != params_period_bytes(hw_params) ||
333 subs->cur_rate != rate;
334 if ((ret = set_format(subs, fmt)) < 0)
335 return ret;
337 if (subs->cur_rate != rate) {
338 struct usb_host_interface *alts;
339 struct usb_interface *iface;
340 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
341 alts = &iface->altsetting[fmt->altset_idx];
342 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
343 if (ret < 0)
344 return ret;
345 subs->cur_rate = rate;
348 if (changed) {
349 /* format changed */
350 snd_usb_release_substream_urbs(subs, 0);
351 /* influenced: period_bytes, channels, rate, format, */
352 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
353 params_rate(hw_params),
354 snd_pcm_format_physical_width(params_format(hw_params)) *
355 params_channels(hw_params));
358 return ret;
362 * hw_free callback
364 * reset the audio format and release the buffer
366 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
368 struct snd_usb_substream *subs = substream->runtime->private_data;
370 subs->cur_audiofmt = NULL;
371 subs->cur_rate = 0;
372 subs->period_bytes = 0;
373 if (!subs->stream->chip->shutdown)
374 snd_usb_release_substream_urbs(subs, 0);
375 return snd_pcm_lib_free_vmalloc_buffer(substream);
379 * prepare callback
381 * only a few subtle things...
383 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
385 struct snd_pcm_runtime *runtime = substream->runtime;
386 struct snd_usb_substream *subs = runtime->private_data;
388 if (! subs->cur_audiofmt) {
389 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
390 return -ENXIO;
393 /* some unit conversions in runtime */
394 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
395 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
397 /* reset the pointer */
398 subs->hwptr_done = 0;
399 subs->transfer_done = 0;
400 subs->phase = 0;
401 runtime->delay = 0;
403 return snd_usb_substream_prepare(subs, runtime);
406 static struct snd_pcm_hardware snd_usb_hardware =
408 .info = SNDRV_PCM_INFO_MMAP |
409 SNDRV_PCM_INFO_MMAP_VALID |
410 SNDRV_PCM_INFO_BATCH |
411 SNDRV_PCM_INFO_INTERLEAVED |
412 SNDRV_PCM_INFO_BLOCK_TRANSFER |
413 SNDRV_PCM_INFO_PAUSE,
414 .buffer_bytes_max = 1024 * 1024,
415 .period_bytes_min = 64,
416 .period_bytes_max = 512 * 1024,
417 .periods_min = 2,
418 .periods_max = 1024,
421 static int hw_check_valid_format(struct snd_usb_substream *subs,
422 struct snd_pcm_hw_params *params,
423 struct audioformat *fp)
425 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
426 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
427 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
428 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
429 struct snd_mask check_fmts;
430 unsigned int ptime;
432 /* check the format */
433 snd_mask_none(&check_fmts);
434 check_fmts.bits[0] = (u32)fp->formats;
435 check_fmts.bits[1] = (u32)(fp->formats >> 32);
436 snd_mask_intersect(&check_fmts, fmts);
437 if (snd_mask_empty(&check_fmts)) {
438 hwc_debug(" > check: no supported format %d\n", fp->format);
439 return 0;
441 /* check the channels */
442 if (fp->channels < ct->min || fp->channels > ct->max) {
443 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
444 return 0;
446 /* check the rate is within the range */
447 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
448 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
449 return 0;
451 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
452 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
453 return 0;
455 /* check whether the period time is >= the data packet interval */
456 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
457 ptime = 125 * (1 << fp->datainterval);
458 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
459 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
460 return 0;
463 return 1;
466 static int hw_rule_rate(struct snd_pcm_hw_params *params,
467 struct snd_pcm_hw_rule *rule)
469 struct snd_usb_substream *subs = rule->private;
470 struct list_head *p;
471 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
472 unsigned int rmin, rmax;
473 int changed;
475 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
476 changed = 0;
477 rmin = rmax = 0;
478 list_for_each(p, &subs->fmt_list) {
479 struct audioformat *fp;
480 fp = list_entry(p, struct audioformat, list);
481 if (!hw_check_valid_format(subs, params, fp))
482 continue;
483 if (changed++) {
484 if (rmin > fp->rate_min)
485 rmin = fp->rate_min;
486 if (rmax < fp->rate_max)
487 rmax = fp->rate_max;
488 } else {
489 rmin = fp->rate_min;
490 rmax = fp->rate_max;
494 if (!changed) {
495 hwc_debug(" --> get empty\n");
496 it->empty = 1;
497 return -EINVAL;
500 changed = 0;
501 if (it->min < rmin) {
502 it->min = rmin;
503 it->openmin = 0;
504 changed = 1;
506 if (it->max > rmax) {
507 it->max = rmax;
508 it->openmax = 0;
509 changed = 1;
511 if (snd_interval_checkempty(it)) {
512 it->empty = 1;
513 return -EINVAL;
515 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
516 return changed;
520 static int hw_rule_channels(struct snd_pcm_hw_params *params,
521 struct snd_pcm_hw_rule *rule)
523 struct snd_usb_substream *subs = rule->private;
524 struct list_head *p;
525 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
526 unsigned int rmin, rmax;
527 int changed;
529 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
530 changed = 0;
531 rmin = rmax = 0;
532 list_for_each(p, &subs->fmt_list) {
533 struct audioformat *fp;
534 fp = list_entry(p, struct audioformat, list);
535 if (!hw_check_valid_format(subs, params, fp))
536 continue;
537 if (changed++) {
538 if (rmin > fp->channels)
539 rmin = fp->channels;
540 if (rmax < fp->channels)
541 rmax = fp->channels;
542 } else {
543 rmin = fp->channels;
544 rmax = fp->channels;
548 if (!changed) {
549 hwc_debug(" --> get empty\n");
550 it->empty = 1;
551 return -EINVAL;
554 changed = 0;
555 if (it->min < rmin) {
556 it->min = rmin;
557 it->openmin = 0;
558 changed = 1;
560 if (it->max > rmax) {
561 it->max = rmax;
562 it->openmax = 0;
563 changed = 1;
565 if (snd_interval_checkempty(it)) {
566 it->empty = 1;
567 return -EINVAL;
569 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
570 return changed;
573 static int hw_rule_format(struct snd_pcm_hw_params *params,
574 struct snd_pcm_hw_rule *rule)
576 struct snd_usb_substream *subs = rule->private;
577 struct list_head *p;
578 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
579 u64 fbits;
580 u32 oldbits[2];
581 int changed;
583 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
584 fbits = 0;
585 list_for_each(p, &subs->fmt_list) {
586 struct audioformat *fp;
587 fp = list_entry(p, struct audioformat, list);
588 if (!hw_check_valid_format(subs, params, fp))
589 continue;
590 fbits |= fp->formats;
593 oldbits[0] = fmt->bits[0];
594 oldbits[1] = fmt->bits[1];
595 fmt->bits[0] &= (u32)fbits;
596 fmt->bits[1] &= (u32)(fbits >> 32);
597 if (!fmt->bits[0] && !fmt->bits[1]) {
598 hwc_debug(" --> get empty\n");
599 return -EINVAL;
601 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
602 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
603 return changed;
606 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
607 struct snd_pcm_hw_rule *rule)
609 struct snd_usb_substream *subs = rule->private;
610 struct audioformat *fp;
611 struct snd_interval *it;
612 unsigned char min_datainterval;
613 unsigned int pmin;
614 int changed;
616 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
617 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
618 min_datainterval = 0xff;
619 list_for_each_entry(fp, &subs->fmt_list, list) {
620 if (!hw_check_valid_format(subs, params, fp))
621 continue;
622 min_datainterval = min(min_datainterval, fp->datainterval);
624 if (min_datainterval == 0xff) {
625 hwc_debug(" --> get empty\n");
626 it->empty = 1;
627 return -EINVAL;
629 pmin = 125 * (1 << min_datainterval);
630 changed = 0;
631 if (it->min < pmin) {
632 it->min = pmin;
633 it->openmin = 0;
634 changed = 1;
636 if (snd_interval_checkempty(it)) {
637 it->empty = 1;
638 return -EINVAL;
640 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
641 return changed;
645 * If the device supports unusual bit rates, does the request meet these?
647 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
648 struct snd_usb_substream *subs)
650 struct audioformat *fp;
651 int count = 0, needs_knot = 0;
652 int err;
654 list_for_each_entry(fp, &subs->fmt_list, list) {
655 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
656 return 0;
657 count += fp->nr_rates;
658 if (fp->rates & SNDRV_PCM_RATE_KNOT)
659 needs_knot = 1;
661 if (!needs_knot)
662 return 0;
664 subs->rate_list.count = count;
665 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
666 subs->rate_list.mask = 0;
667 count = 0;
668 list_for_each_entry(fp, &subs->fmt_list, list) {
669 int i;
670 for (i = 0; i < fp->nr_rates; i++)
671 subs->rate_list.list[count++] = fp->rate_table[i];
673 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
674 &subs->rate_list);
675 if (err < 0)
676 return err;
678 return 0;
683 * set up the runtime hardware information.
686 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
688 struct list_head *p;
689 unsigned int pt, ptmin;
690 int param_period_time_if_needed;
691 int err;
693 runtime->hw.formats = subs->formats;
695 runtime->hw.rate_min = 0x7fffffff;
696 runtime->hw.rate_max = 0;
697 runtime->hw.channels_min = 256;
698 runtime->hw.channels_max = 0;
699 runtime->hw.rates = 0;
700 ptmin = UINT_MAX;
701 /* check min/max rates and channels */
702 list_for_each(p, &subs->fmt_list) {
703 struct audioformat *fp;
704 fp = list_entry(p, struct audioformat, list);
705 runtime->hw.rates |= fp->rates;
706 if (runtime->hw.rate_min > fp->rate_min)
707 runtime->hw.rate_min = fp->rate_min;
708 if (runtime->hw.rate_max < fp->rate_max)
709 runtime->hw.rate_max = fp->rate_max;
710 if (runtime->hw.channels_min > fp->channels)
711 runtime->hw.channels_min = fp->channels;
712 if (runtime->hw.channels_max < fp->channels)
713 runtime->hw.channels_max = fp->channels;
714 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
715 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
716 fp->frame_size;
718 pt = 125 * (1 << fp->datainterval);
719 ptmin = min(ptmin, pt);
722 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
723 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
724 /* full speed devices have fixed data packet interval */
725 ptmin = 1000;
726 if (ptmin == 1000)
727 /* if period time doesn't go below 1 ms, no rules needed */
728 param_period_time_if_needed = -1;
729 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
730 ptmin, UINT_MAX);
732 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
733 hw_rule_rate, subs,
734 SNDRV_PCM_HW_PARAM_FORMAT,
735 SNDRV_PCM_HW_PARAM_CHANNELS,
736 param_period_time_if_needed,
737 -1)) < 0)
738 return err;
739 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
740 hw_rule_channels, subs,
741 SNDRV_PCM_HW_PARAM_FORMAT,
742 SNDRV_PCM_HW_PARAM_RATE,
743 param_period_time_if_needed,
744 -1)) < 0)
745 return err;
746 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
747 hw_rule_format, subs,
748 SNDRV_PCM_HW_PARAM_RATE,
749 SNDRV_PCM_HW_PARAM_CHANNELS,
750 param_period_time_if_needed,
751 -1)) < 0)
752 return err;
753 if (param_period_time_if_needed >= 0) {
754 err = snd_pcm_hw_rule_add(runtime, 0,
755 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
756 hw_rule_period_time, subs,
757 SNDRV_PCM_HW_PARAM_FORMAT,
758 SNDRV_PCM_HW_PARAM_CHANNELS,
759 SNDRV_PCM_HW_PARAM_RATE,
760 -1);
761 if (err < 0)
762 return err;
764 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
765 return err;
766 return 0;
769 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
771 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
772 struct snd_pcm_runtime *runtime = substream->runtime;
773 struct snd_usb_substream *subs = &as->substream[direction];
775 subs->interface = -1;
776 subs->altset_idx = 0;
777 runtime->hw = snd_usb_hardware;
778 runtime->private_data = subs;
779 subs->pcm_substream = substream;
780 return setup_hw_info(runtime, subs);
783 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
785 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
786 struct snd_usb_substream *subs = &as->substream[direction];
788 if (!as->chip->shutdown && subs->interface >= 0) {
789 usb_set_interface(subs->dev, subs->interface, 0);
790 subs->interface = -1;
792 subs->pcm_substream = NULL;
793 return 0;
796 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
798 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
801 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
803 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
806 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
808 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
811 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
813 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
816 static struct snd_pcm_ops snd_usb_playback_ops = {
817 .open = snd_usb_playback_open,
818 .close = snd_usb_playback_close,
819 .ioctl = snd_pcm_lib_ioctl,
820 .hw_params = snd_usb_hw_params,
821 .hw_free = snd_usb_hw_free,
822 .prepare = snd_usb_pcm_prepare,
823 .trigger = snd_usb_substream_playback_trigger,
824 .pointer = snd_usb_pcm_pointer,
825 .page = snd_pcm_lib_get_vmalloc_page,
826 .mmap = snd_pcm_lib_mmap_vmalloc,
829 static struct snd_pcm_ops snd_usb_capture_ops = {
830 .open = snd_usb_capture_open,
831 .close = snd_usb_capture_close,
832 .ioctl = snd_pcm_lib_ioctl,
833 .hw_params = snd_usb_hw_params,
834 .hw_free = snd_usb_hw_free,
835 .prepare = snd_usb_pcm_prepare,
836 .trigger = snd_usb_substream_capture_trigger,
837 .pointer = snd_usb_pcm_pointer,
838 .page = snd_pcm_lib_get_vmalloc_page,
839 .mmap = snd_pcm_lib_mmap_vmalloc,
842 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
844 snd_pcm_set_ops(pcm, stream,
845 stream == SNDRV_PCM_STREAM_PLAYBACK ?
846 &snd_usb_playback_ops : &snd_usb_capture_ops);