usb: ch9.h: usb_endpoint_maxp() uses __le16_to_cpu()
[zen-stable.git] / sound / usb / pcm.c
blob0220b0f335b9ad230ebcb954ed78ab25731ca58e
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 "endpoint.h"
32 #include "helper.h"
33 #include "pcm.h"
34 #include "clock.h"
35 #include "power.h"
37 /* return the estimated delay based on USB frame counters */
38 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
39 unsigned int rate)
41 int current_frame_number;
42 int frame_diff;
43 int est_delay;
45 current_frame_number = usb_get_current_frame_number(subs->dev);
47 * HCD implementations use different widths, use lower 8 bits.
48 * The delay will be managed up to 256ms, which is more than
49 * enough
51 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
53 /* Approximation based on number of samples per USB frame (ms),
54 some truncation for 44.1 but the estimate is good enough */
55 est_delay = subs->last_delay - (frame_diff * rate / 1000);
56 if (est_delay < 0)
57 est_delay = 0;
58 return est_delay;
62 * return the current pcm pointer. just based on the hwptr_done value.
64 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
66 struct snd_usb_substream *subs;
67 unsigned int hwptr_done;
69 subs = (struct snd_usb_substream *)substream->runtime->private_data;
70 spin_lock(&subs->lock);
71 hwptr_done = subs->hwptr_done;
72 substream->runtime->delay = snd_usb_pcm_delay(subs,
73 substream->runtime->rate);
74 spin_unlock(&subs->lock);
75 return hwptr_done / (substream->runtime->frame_bits >> 3);
79 * find a matching audio format
81 static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
82 unsigned int rate, unsigned int channels)
84 struct list_head *p;
85 struct audioformat *found = NULL;
86 int cur_attr = 0, attr;
88 list_for_each(p, &subs->fmt_list) {
89 struct audioformat *fp;
90 fp = list_entry(p, struct audioformat, list);
91 if (!(fp->formats & (1uLL << format)))
92 continue;
93 if (fp->channels != channels)
94 continue;
95 if (rate < fp->rate_min || rate > fp->rate_max)
96 continue;
97 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
98 unsigned int i;
99 for (i = 0; i < fp->nr_rates; i++)
100 if (fp->rate_table[i] == rate)
101 break;
102 if (i >= fp->nr_rates)
103 continue;
105 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
106 if (! found) {
107 found = fp;
108 cur_attr = attr;
109 continue;
111 /* avoid async out and adaptive in if the other method
112 * supports the same format.
113 * this is a workaround for the case like
114 * M-audio audiophile USB.
116 if (attr != cur_attr) {
117 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
118 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
119 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
120 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
121 continue;
122 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
123 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
124 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
125 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
126 found = fp;
127 cur_attr = attr;
128 continue;
131 /* find the format with the largest max. packet size */
132 if (fp->maxpacksize > found->maxpacksize) {
133 found = fp;
134 cur_attr = attr;
137 return found;
140 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
141 struct usb_host_interface *alts,
142 struct audioformat *fmt)
144 struct usb_device *dev = chip->dev;
145 unsigned int ep;
146 unsigned char data[1];
147 int err;
149 ep = get_endpoint(alts, 0)->bEndpointAddress;
151 data[0] = 1;
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_PITCH_CONTROL << 8, ep,
155 data, sizeof(data))) < 0) {
156 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
157 dev->devnum, iface, ep);
158 return err;
161 return 0;
164 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
165 struct usb_host_interface *alts,
166 struct audioformat *fmt)
168 struct usb_device *dev = chip->dev;
169 unsigned char data[1];
170 unsigned int ep;
171 int err;
173 ep = get_endpoint(alts, 0)->bEndpointAddress;
175 data[0] = 1;
176 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
177 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
178 UAC2_EP_CS_PITCH << 8, 0,
179 data, sizeof(data))) < 0) {
180 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
181 dev->devnum, iface, fmt->altsetting);
182 return err;
185 return 0;
189 * initialize the pitch control and sample rate
191 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
192 struct usb_host_interface *alts,
193 struct audioformat *fmt)
195 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
197 /* if endpoint doesn't have pitch control, bail out */
198 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
199 return 0;
201 switch (altsd->bInterfaceProtocol) {
202 case UAC_VERSION_1:
203 default:
204 return init_pitch_v1(chip, iface, alts, fmt);
206 case UAC_VERSION_2:
207 return init_pitch_v2(chip, iface, alts, fmt);
212 * find a matching format and set up the interface
214 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
216 struct usb_device *dev = subs->dev;
217 struct usb_host_interface *alts;
218 struct usb_interface_descriptor *altsd;
219 struct usb_interface *iface;
220 unsigned int ep, attr;
221 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
222 int err;
224 iface = usb_ifnum_to_if(dev, fmt->iface);
225 if (WARN_ON(!iface))
226 return -EINVAL;
227 alts = &iface->altsetting[fmt->altset_idx];
228 altsd = get_iface_desc(alts);
229 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
230 return -EINVAL;
232 if (fmt == subs->cur_audiofmt)
233 return 0;
235 /* close the old interface */
236 if (subs->interface >= 0 && subs->interface != fmt->iface) {
237 if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
238 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
239 dev->devnum, fmt->iface, fmt->altsetting);
240 return -EIO;
242 subs->interface = -1;
243 subs->altset_idx = 0;
246 /* set interface */
247 if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
248 if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
249 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
250 dev->devnum, fmt->iface, fmt->altsetting);
251 return -EIO;
253 snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
254 subs->interface = fmt->iface;
255 subs->altset_idx = fmt->altset_idx;
258 /* create a data pipe */
259 ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
260 if (is_playback)
261 subs->datapipe = usb_sndisocpipe(dev, ep);
262 else
263 subs->datapipe = usb_rcvisocpipe(dev, ep);
264 subs->datainterval = fmt->datainterval;
265 subs->syncpipe = subs->syncinterval = 0;
266 subs->maxpacksize = fmt->maxpacksize;
267 subs->syncmaxsize = 0;
268 subs->fill_max = 0;
270 /* we need a sync pipe in async OUT or adaptive IN mode */
271 /* check the number of EP, since some devices have broken
272 * descriptors which fool us. if it has only one EP,
273 * assume it as adaptive-out or sync-in.
275 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
276 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
277 (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
278 altsd->bNumEndpoints >= 2) {
279 /* check sync-pipe endpoint */
280 /* ... and check descriptor size before accessing bSynchAddress
281 because there is a version of the SB Audigy 2 NX firmware lacking
282 the audio fields in the endpoint descriptors */
283 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
284 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
285 get_endpoint(alts, 1)->bSynchAddress != 0)) {
286 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
287 dev->devnum, fmt->iface, fmt->altsetting);
288 return -EINVAL;
290 ep = get_endpoint(alts, 1)->bEndpointAddress;
291 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
292 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
293 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
294 snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
295 dev->devnum, fmt->iface, fmt->altsetting);
296 return -EINVAL;
298 ep &= USB_ENDPOINT_NUMBER_MASK;
299 if (is_playback)
300 subs->syncpipe = usb_rcvisocpipe(dev, ep);
301 else
302 subs->syncpipe = usb_sndisocpipe(dev, ep);
303 if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
304 get_endpoint(alts, 1)->bRefresh >= 1 &&
305 get_endpoint(alts, 1)->bRefresh <= 9)
306 subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
307 else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
308 subs->syncinterval = 1;
309 else if (get_endpoint(alts, 1)->bInterval >= 1 &&
310 get_endpoint(alts, 1)->bInterval <= 16)
311 subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
312 else
313 subs->syncinterval = 3;
314 subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
317 /* always fill max packet size */
318 if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
319 subs->fill_max = 1;
321 if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
322 return err;
324 subs->cur_audiofmt = fmt;
326 snd_usb_set_format_quirk(subs, fmt);
328 #if 0
329 printk(KERN_DEBUG
330 "setting done: format = %d, rate = %d..%d, channels = %d\n",
331 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
332 printk(KERN_DEBUG
333 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
334 subs->datapipe, subs->syncpipe);
335 #endif
337 return 0;
341 * hw_params callback
343 * allocate a buffer and set the given audio format.
345 * so far we use a physically linear buffer although packetize transfer
346 * doesn't need a continuous area.
347 * if sg buffer is supported on the later version of alsa, we'll follow
348 * that.
350 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
351 struct snd_pcm_hw_params *hw_params)
353 struct snd_usb_substream *subs = substream->runtime->private_data;
354 struct audioformat *fmt;
355 unsigned int channels, rate, format;
356 int ret, changed;
358 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
359 params_buffer_bytes(hw_params));
360 if (ret < 0)
361 return ret;
363 format = params_format(hw_params);
364 rate = params_rate(hw_params);
365 channels = params_channels(hw_params);
366 fmt = find_format(subs, format, rate, channels);
367 if (!fmt) {
368 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
369 format, rate, channels);
370 return -EINVAL;
373 changed = subs->cur_audiofmt != fmt ||
374 subs->period_bytes != params_period_bytes(hw_params) ||
375 subs->cur_rate != rate;
376 if ((ret = set_format(subs, fmt)) < 0)
377 return ret;
379 if (subs->cur_rate != rate) {
380 struct usb_host_interface *alts;
381 struct usb_interface *iface;
382 iface = usb_ifnum_to_if(subs->dev, fmt->iface);
383 alts = &iface->altsetting[fmt->altset_idx];
384 ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
385 if (ret < 0)
386 return ret;
387 subs->cur_rate = rate;
390 if (changed) {
391 mutex_lock(&subs->stream->chip->shutdown_mutex);
392 /* format changed */
393 snd_usb_release_substream_urbs(subs, 0);
394 /* influenced: period_bytes, channels, rate, format, */
395 ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
396 params_rate(hw_params),
397 snd_pcm_format_physical_width(params_format(hw_params)) *
398 params_channels(hw_params));
399 mutex_unlock(&subs->stream->chip->shutdown_mutex);
402 return ret;
406 * hw_free callback
408 * reset the audio format and release the buffer
410 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
412 struct snd_usb_substream *subs = substream->runtime->private_data;
414 subs->cur_audiofmt = NULL;
415 subs->cur_rate = 0;
416 subs->period_bytes = 0;
417 mutex_lock(&subs->stream->chip->shutdown_mutex);
418 snd_usb_release_substream_urbs(subs, 0);
419 mutex_unlock(&subs->stream->chip->shutdown_mutex);
420 return snd_pcm_lib_free_vmalloc_buffer(substream);
424 * prepare callback
426 * only a few subtle things...
428 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
430 struct snd_pcm_runtime *runtime = substream->runtime;
431 struct snd_usb_substream *subs = runtime->private_data;
433 if (! subs->cur_audiofmt) {
434 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
435 return -ENXIO;
438 /* some unit conversions in runtime */
439 subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
440 subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
442 /* reset the pointer */
443 subs->hwptr_done = 0;
444 subs->transfer_done = 0;
445 subs->phase = 0;
446 subs->last_delay = 0;
447 subs->last_frame_number = 0;
448 runtime->delay = 0;
450 return snd_usb_substream_prepare(subs, runtime);
453 static struct snd_pcm_hardware snd_usb_hardware =
455 .info = SNDRV_PCM_INFO_MMAP |
456 SNDRV_PCM_INFO_MMAP_VALID |
457 SNDRV_PCM_INFO_BATCH |
458 SNDRV_PCM_INFO_INTERLEAVED |
459 SNDRV_PCM_INFO_BLOCK_TRANSFER |
460 SNDRV_PCM_INFO_PAUSE,
461 .buffer_bytes_max = 1024 * 1024,
462 .period_bytes_min = 64,
463 .period_bytes_max = 512 * 1024,
464 .periods_min = 2,
465 .periods_max = 1024,
468 static int hw_check_valid_format(struct snd_usb_substream *subs,
469 struct snd_pcm_hw_params *params,
470 struct audioformat *fp)
472 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
473 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
474 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
475 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
476 struct snd_mask check_fmts;
477 unsigned int ptime;
479 /* check the format */
480 snd_mask_none(&check_fmts);
481 check_fmts.bits[0] = (u32)fp->formats;
482 check_fmts.bits[1] = (u32)(fp->formats >> 32);
483 snd_mask_intersect(&check_fmts, fmts);
484 if (snd_mask_empty(&check_fmts)) {
485 hwc_debug(" > check: no supported format %d\n", fp->format);
486 return 0;
488 /* check the channels */
489 if (fp->channels < ct->min || fp->channels > ct->max) {
490 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
491 return 0;
493 /* check the rate is within the range */
494 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
495 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
496 return 0;
498 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
499 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
500 return 0;
502 /* check whether the period time is >= the data packet interval */
503 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) {
504 ptime = 125 * (1 << fp->datainterval);
505 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
506 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
507 return 0;
510 return 1;
513 static int hw_rule_rate(struct snd_pcm_hw_params *params,
514 struct snd_pcm_hw_rule *rule)
516 struct snd_usb_substream *subs = rule->private;
517 struct list_head *p;
518 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
519 unsigned int rmin, rmax;
520 int changed;
522 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
523 changed = 0;
524 rmin = rmax = 0;
525 list_for_each(p, &subs->fmt_list) {
526 struct audioformat *fp;
527 fp = list_entry(p, struct audioformat, list);
528 if (!hw_check_valid_format(subs, params, fp))
529 continue;
530 if (changed++) {
531 if (rmin > fp->rate_min)
532 rmin = fp->rate_min;
533 if (rmax < fp->rate_max)
534 rmax = fp->rate_max;
535 } else {
536 rmin = fp->rate_min;
537 rmax = fp->rate_max;
541 if (!changed) {
542 hwc_debug(" --> get empty\n");
543 it->empty = 1;
544 return -EINVAL;
547 changed = 0;
548 if (it->min < rmin) {
549 it->min = rmin;
550 it->openmin = 0;
551 changed = 1;
553 if (it->max > rmax) {
554 it->max = rmax;
555 it->openmax = 0;
556 changed = 1;
558 if (snd_interval_checkempty(it)) {
559 it->empty = 1;
560 return -EINVAL;
562 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
563 return changed;
567 static int hw_rule_channels(struct snd_pcm_hw_params *params,
568 struct snd_pcm_hw_rule *rule)
570 struct snd_usb_substream *subs = rule->private;
571 struct list_head *p;
572 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
573 unsigned int rmin, rmax;
574 int changed;
576 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
577 changed = 0;
578 rmin = rmax = 0;
579 list_for_each(p, &subs->fmt_list) {
580 struct audioformat *fp;
581 fp = list_entry(p, struct audioformat, list);
582 if (!hw_check_valid_format(subs, params, fp))
583 continue;
584 if (changed++) {
585 if (rmin > fp->channels)
586 rmin = fp->channels;
587 if (rmax < fp->channels)
588 rmax = fp->channels;
589 } else {
590 rmin = fp->channels;
591 rmax = fp->channels;
595 if (!changed) {
596 hwc_debug(" --> get empty\n");
597 it->empty = 1;
598 return -EINVAL;
601 changed = 0;
602 if (it->min < rmin) {
603 it->min = rmin;
604 it->openmin = 0;
605 changed = 1;
607 if (it->max > rmax) {
608 it->max = rmax;
609 it->openmax = 0;
610 changed = 1;
612 if (snd_interval_checkempty(it)) {
613 it->empty = 1;
614 return -EINVAL;
616 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
617 return changed;
620 static int hw_rule_format(struct snd_pcm_hw_params *params,
621 struct snd_pcm_hw_rule *rule)
623 struct snd_usb_substream *subs = rule->private;
624 struct list_head *p;
625 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
626 u64 fbits;
627 u32 oldbits[2];
628 int changed;
630 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
631 fbits = 0;
632 list_for_each(p, &subs->fmt_list) {
633 struct audioformat *fp;
634 fp = list_entry(p, struct audioformat, list);
635 if (!hw_check_valid_format(subs, params, fp))
636 continue;
637 fbits |= fp->formats;
640 oldbits[0] = fmt->bits[0];
641 oldbits[1] = fmt->bits[1];
642 fmt->bits[0] &= (u32)fbits;
643 fmt->bits[1] &= (u32)(fbits >> 32);
644 if (!fmt->bits[0] && !fmt->bits[1]) {
645 hwc_debug(" --> get empty\n");
646 return -EINVAL;
648 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
649 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
650 return changed;
653 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
654 struct snd_pcm_hw_rule *rule)
656 struct snd_usb_substream *subs = rule->private;
657 struct audioformat *fp;
658 struct snd_interval *it;
659 unsigned char min_datainterval;
660 unsigned int pmin;
661 int changed;
663 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
664 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
665 min_datainterval = 0xff;
666 list_for_each_entry(fp, &subs->fmt_list, list) {
667 if (!hw_check_valid_format(subs, params, fp))
668 continue;
669 min_datainterval = min(min_datainterval, fp->datainterval);
671 if (min_datainterval == 0xff) {
672 hwc_debug(" --> get empty\n");
673 it->empty = 1;
674 return -EINVAL;
676 pmin = 125 * (1 << min_datainterval);
677 changed = 0;
678 if (it->min < pmin) {
679 it->min = pmin;
680 it->openmin = 0;
681 changed = 1;
683 if (snd_interval_checkempty(it)) {
684 it->empty = 1;
685 return -EINVAL;
687 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
688 return changed;
692 * If the device supports unusual bit rates, does the request meet these?
694 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
695 struct snd_usb_substream *subs)
697 struct audioformat *fp;
698 int count = 0, needs_knot = 0;
699 int err;
701 list_for_each_entry(fp, &subs->fmt_list, list) {
702 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
703 return 0;
704 count += fp->nr_rates;
705 if (fp->rates & SNDRV_PCM_RATE_KNOT)
706 needs_knot = 1;
708 if (!needs_knot)
709 return 0;
711 subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
712 if (!subs->rate_list.list)
713 return -ENOMEM;
714 subs->rate_list.count = count;
715 subs->rate_list.mask = 0;
716 count = 0;
717 list_for_each_entry(fp, &subs->fmt_list, list) {
718 int i;
719 for (i = 0; i < fp->nr_rates; i++)
720 subs->rate_list.list[count++] = fp->rate_table[i];
722 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
723 &subs->rate_list);
724 if (err < 0)
725 return err;
727 return 0;
732 * set up the runtime hardware information.
735 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
737 struct list_head *p;
738 unsigned int pt, ptmin;
739 int param_period_time_if_needed;
740 int err;
742 runtime->hw.formats = subs->formats;
744 runtime->hw.rate_min = 0x7fffffff;
745 runtime->hw.rate_max = 0;
746 runtime->hw.channels_min = 256;
747 runtime->hw.channels_max = 0;
748 runtime->hw.rates = 0;
749 ptmin = UINT_MAX;
750 /* check min/max rates and channels */
751 list_for_each(p, &subs->fmt_list) {
752 struct audioformat *fp;
753 fp = list_entry(p, struct audioformat, list);
754 runtime->hw.rates |= fp->rates;
755 if (runtime->hw.rate_min > fp->rate_min)
756 runtime->hw.rate_min = fp->rate_min;
757 if (runtime->hw.rate_max < fp->rate_max)
758 runtime->hw.rate_max = fp->rate_max;
759 if (runtime->hw.channels_min > fp->channels)
760 runtime->hw.channels_min = fp->channels;
761 if (runtime->hw.channels_max < fp->channels)
762 runtime->hw.channels_max = fp->channels;
763 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
764 /* FIXME: there might be more than one audio formats... */
765 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
766 fp->frame_size;
768 pt = 125 * (1 << fp->datainterval);
769 ptmin = min(ptmin, pt);
771 err = snd_usb_autoresume(subs->stream->chip);
772 if (err < 0)
773 return err;
775 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
776 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
777 /* full speed devices have fixed data packet interval */
778 ptmin = 1000;
779 if (ptmin == 1000)
780 /* if period time doesn't go below 1 ms, no rules needed */
781 param_period_time_if_needed = -1;
782 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
783 ptmin, UINT_MAX);
785 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
786 hw_rule_rate, subs,
787 SNDRV_PCM_HW_PARAM_FORMAT,
788 SNDRV_PCM_HW_PARAM_CHANNELS,
789 param_period_time_if_needed,
790 -1)) < 0)
791 goto rep_err;
792 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
793 hw_rule_channels, subs,
794 SNDRV_PCM_HW_PARAM_FORMAT,
795 SNDRV_PCM_HW_PARAM_RATE,
796 param_period_time_if_needed,
797 -1)) < 0)
798 goto rep_err;
799 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
800 hw_rule_format, subs,
801 SNDRV_PCM_HW_PARAM_RATE,
802 SNDRV_PCM_HW_PARAM_CHANNELS,
803 param_period_time_if_needed,
804 -1)) < 0)
805 goto rep_err;
806 if (param_period_time_if_needed >= 0) {
807 err = snd_pcm_hw_rule_add(runtime, 0,
808 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
809 hw_rule_period_time, subs,
810 SNDRV_PCM_HW_PARAM_FORMAT,
811 SNDRV_PCM_HW_PARAM_CHANNELS,
812 SNDRV_PCM_HW_PARAM_RATE,
813 -1);
814 if (err < 0)
815 goto rep_err;
817 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
818 goto rep_err;
819 return 0;
821 rep_err:
822 snd_usb_autosuspend(subs->stream->chip);
823 return err;
826 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
828 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
829 struct snd_pcm_runtime *runtime = substream->runtime;
830 struct snd_usb_substream *subs = &as->substream[direction];
832 subs->interface = -1;
833 subs->altset_idx = 0;
834 runtime->hw = snd_usb_hardware;
835 runtime->private_data = subs;
836 subs->pcm_substream = substream;
837 /* runtime PM is also done there */
838 return setup_hw_info(runtime, subs);
841 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
843 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
844 struct snd_usb_substream *subs = &as->substream[direction];
846 if (!as->chip->shutdown && subs->interface >= 0) {
847 usb_set_interface(subs->dev, subs->interface, 0);
848 subs->interface = -1;
850 subs->pcm_substream = NULL;
851 snd_usb_autosuspend(subs->stream->chip);
852 return 0;
855 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
857 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
860 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
862 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
865 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
867 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
870 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
872 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
875 static struct snd_pcm_ops snd_usb_playback_ops = {
876 .open = snd_usb_playback_open,
877 .close = snd_usb_playback_close,
878 .ioctl = snd_pcm_lib_ioctl,
879 .hw_params = snd_usb_hw_params,
880 .hw_free = snd_usb_hw_free,
881 .prepare = snd_usb_pcm_prepare,
882 .trigger = snd_usb_substream_playback_trigger,
883 .pointer = snd_usb_pcm_pointer,
884 .page = snd_pcm_lib_get_vmalloc_page,
885 .mmap = snd_pcm_lib_mmap_vmalloc,
888 static struct snd_pcm_ops snd_usb_capture_ops = {
889 .open = snd_usb_capture_open,
890 .close = snd_usb_capture_close,
891 .ioctl = snd_pcm_lib_ioctl,
892 .hw_params = snd_usb_hw_params,
893 .hw_free = snd_usb_hw_free,
894 .prepare = snd_usb_pcm_prepare,
895 .trigger = snd_usb_substream_capture_trigger,
896 .pointer = snd_usb_pcm_pointer,
897 .page = snd_pcm_lib_get_vmalloc_page,
898 .mmap = snd_pcm_lib_mmap_vmalloc,
901 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
903 snd_pcm_set_ops(pcm, stream,
904 stream == SNDRV_PCM_STREAM_PLAYBACK ?
905 &snd_usb_playback_ops : &snd_usb_capture_ops);