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/ratelimit.h>
20 #include <linux/usb.h>
21 #include <linux/usb/audio.h>
22 #include <linux/usb/audio-v2.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
38 #define SUBSTREAM_FLAG_DATA_EP_STARTED 0
39 #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
41 /* return the estimated delay based on USB frame counters */
42 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
45 int current_frame_number;
49 current_frame_number = usb_get_current_frame_number(subs->dev);
51 * HCD implementations use different widths, use lower 8 bits.
52 * The delay will be managed up to 256ms, which is more than
55 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
57 /* Approximation based on number of samples per USB frame (ms),
58 some truncation for 44.1 but the estimate is good enough */
59 est_delay = subs->last_delay - (frame_diff * rate / 1000);
66 * return the current pcm pointer. just based on the hwptr_done value.
68 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
70 struct snd_usb_substream *subs;
71 unsigned int hwptr_done;
73 subs = (struct snd_usb_substream *)substream->runtime->private_data;
74 if (subs->stream->chip->shutdown)
75 return SNDRV_PCM_POS_XRUN;
76 spin_lock(&subs->lock);
77 hwptr_done = subs->hwptr_done;
78 substream->runtime->delay = snd_usb_pcm_delay(subs,
79 substream->runtime->rate);
80 spin_unlock(&subs->lock);
81 return hwptr_done / (substream->runtime->frame_bits >> 3);
85 * find a matching audio format
87 static struct audioformat *find_format(struct snd_usb_substream *subs)
90 struct audioformat *found = NULL;
91 int cur_attr = 0, attr;
93 list_for_each(p, &subs->fmt_list) {
94 struct audioformat *fp;
95 fp = list_entry(p, struct audioformat, list);
96 if (!(fp->formats & (1uLL << subs->pcm_format)))
98 if (fp->channels != subs->channels)
100 if (subs->cur_rate < fp->rate_min ||
101 subs->cur_rate > fp->rate_max)
103 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
105 for (i = 0; i < fp->nr_rates; i++)
106 if (fp->rate_table[i] == subs->cur_rate)
108 if (i >= fp->nr_rates)
111 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
117 /* avoid async out and adaptive in if the other method
118 * supports the same format.
119 * this is a workaround for the case like
120 * M-audio audiophile USB.
122 if (attr != cur_attr) {
123 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
124 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
125 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
126 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
128 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
129 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
130 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
131 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
137 /* find the format with the largest max. packet size */
138 if (fp->maxpacksize > found->maxpacksize) {
146 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
147 struct usb_host_interface *alts,
148 struct audioformat *fmt)
150 struct usb_device *dev = chip->dev;
152 unsigned char data[1];
155 ep = get_endpoint(alts, 0)->bEndpointAddress;
158 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
159 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
160 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
161 data, sizeof(data))) < 0) {
162 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
163 dev->devnum, iface, ep);
170 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
171 struct usb_host_interface *alts,
172 struct audioformat *fmt)
174 struct usb_device *dev = chip->dev;
175 unsigned char data[1];
179 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
180 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
181 UAC2_EP_CS_PITCH << 8, 0,
182 data, sizeof(data))) < 0) {
183 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
184 dev->devnum, iface, fmt->altsetting);
192 * initialize the pitch control and sample rate
194 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
195 struct usb_host_interface *alts,
196 struct audioformat *fmt)
198 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
200 /* if endpoint doesn't have pitch control, bail out */
201 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
204 switch (altsd->bInterfaceProtocol) {
207 return init_pitch_v1(chip, iface, alts, fmt);
210 return init_pitch_v2(chip, iface, alts, fmt);
214 static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
218 if (!subs->data_endpoint)
221 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
222 struct snd_usb_endpoint *ep = subs->data_endpoint;
224 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
226 ep->data_subs = subs;
227 err = snd_usb_endpoint_start(ep, can_sleep);
229 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
234 if (subs->sync_endpoint &&
235 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
236 struct snd_usb_endpoint *ep = subs->sync_endpoint;
238 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
239 subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
240 err = usb_set_interface(subs->dev,
241 subs->sync_endpoint->iface,
242 subs->sync_endpoint->alt_idx);
245 "%d:%d:%d: cannot set interface (%d)\n",
247 subs->sync_endpoint->iface,
248 subs->sync_endpoint->alt_idx, err);
253 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
255 ep->sync_slave = subs->data_endpoint;
256 err = snd_usb_endpoint_start(ep, can_sleep);
258 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
266 static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
268 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
269 snd_usb_endpoint_stop(subs->sync_endpoint, wait);
271 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
272 snd_usb_endpoint_stop(subs->data_endpoint, wait);
275 static int deactivate_endpoints(struct snd_usb_substream *subs)
279 reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
280 retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
292 * find a matching format and set up the interface
294 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
296 struct usb_device *dev = subs->dev;
297 struct usb_host_interface *alts;
298 struct usb_interface_descriptor *altsd;
299 struct usb_interface *iface;
300 unsigned int ep, attr;
301 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
302 int err, implicit_fb = 0;
304 iface = usb_ifnum_to_if(dev, fmt->iface);
307 alts = &iface->altsetting[fmt->altset_idx];
308 altsd = get_iface_desc(alts);
309 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
312 if (fmt == subs->cur_audiofmt)
315 /* close the old interface */
316 if (subs->interface >= 0 && subs->interface != fmt->iface) {
317 err = usb_set_interface(subs->dev, subs->interface, 0);
319 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
320 dev->devnum, fmt->iface, fmt->altsetting, err);
323 subs->interface = -1;
324 subs->altset_idx = 0;
328 if (subs->interface != fmt->iface ||
329 subs->altset_idx != fmt->altset_idx) {
330 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
332 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
333 dev->devnum, fmt->iface, fmt->altsetting, err);
336 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
337 fmt->iface, fmt->altsetting);
338 subs->interface = fmt->iface;
339 subs->altset_idx = fmt->altset_idx;
342 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
343 alts, fmt->endpoint, subs->direction,
344 SND_USB_ENDPOINT_TYPE_DATA);
345 if (!subs->data_endpoint)
348 /* we need a sync pipe in async OUT or adaptive IN mode */
349 /* check the number of EP, since some devices have broken
350 * descriptors which fool us. if it has only one EP,
351 * assume it as adaptive-out or sync-in.
353 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
355 switch (subs->stream->chip->usb_id) {
356 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
357 case USB_ID(0x0763, 0x2081):
361 iface = usb_ifnum_to_if(dev, 2);
363 if (!iface || iface->num_altsetting == 0)
366 alts = &iface->altsetting[1];
371 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
372 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
373 altsd->bNumEndpoints >= 2) {
374 /* check sync-pipe endpoint */
375 /* ... and check descriptor size before accessing bSynchAddress
376 because there is a version of the SB Audigy 2 NX firmware lacking
377 the audio fields in the endpoint descriptors */
378 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
379 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
380 get_endpoint(alts, 1)->bSynchAddress != 0 &&
382 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
383 dev->devnum, fmt->iface, fmt->altsetting,
384 get_endpoint(alts, 1)->bmAttributes,
385 get_endpoint(alts, 1)->bLength,
386 get_endpoint(alts, 1)->bSynchAddress);
389 ep = get_endpoint(alts, 1)->bEndpointAddress;
391 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
392 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
393 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
394 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
395 dev->devnum, fmt->iface, fmt->altsetting,
396 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
400 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
401 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
404 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
405 alts, ep, !subs->direction,
407 SND_USB_ENDPOINT_TYPE_DATA :
408 SND_USB_ENDPOINT_TYPE_SYNC);
409 if (!subs->sync_endpoint)
412 subs->data_endpoint->sync_master = subs->sync_endpoint;
415 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
418 subs->cur_audiofmt = fmt;
420 snd_usb_set_format_quirk(subs, fmt);
424 "setting done: format = %d, rate = %d..%d, channels = %d\n",
425 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
427 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
428 subs->datapipe, subs->syncpipe);
435 * configure endpoint params
437 * called during initial setup and upon resume
439 static int configure_endpoint(struct snd_usb_substream *subs)
444 stop_endpoints(subs, false);
445 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
451 subs->sync_endpoint);
455 if (subs->sync_endpoint)
456 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
469 * allocate a buffer and set the given audio format.
471 * so far we use a physically linear buffer although packetize transfer
472 * doesn't need a continuous area.
473 * if sg buffer is supported on the later version of alsa, we'll follow
476 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
477 struct snd_pcm_hw_params *hw_params)
479 struct snd_usb_substream *subs = substream->runtime->private_data;
480 struct audioformat *fmt;
483 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
484 params_buffer_bytes(hw_params));
488 subs->pcm_format = params_format(hw_params);
489 subs->period_bytes = params_period_bytes(hw_params);
490 subs->channels = params_channels(hw_params);
491 subs->cur_rate = params_rate(hw_params);
493 fmt = find_format(subs);
495 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
496 subs->pcm_format, subs->cur_rate, subs->channels);
500 down_read(&subs->stream->chip->shutdown_rwsem);
501 if (subs->stream->chip->shutdown)
504 ret = set_format(subs, fmt);
505 up_read(&subs->stream->chip->shutdown_rwsem);
509 subs->interface = fmt->iface;
510 subs->altset_idx = fmt->altset_idx;
511 subs->need_setup_ep = true;
519 * reset the audio format and release the buffer
521 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
523 struct snd_usb_substream *subs = substream->runtime->private_data;
525 subs->cur_audiofmt = NULL;
527 subs->period_bytes = 0;
528 down_read(&subs->stream->chip->shutdown_rwsem);
529 if (!subs->stream->chip->shutdown) {
530 stop_endpoints(subs, true);
531 deactivate_endpoints(subs);
533 up_read(&subs->stream->chip->shutdown_rwsem);
534 return snd_pcm_lib_free_vmalloc_buffer(substream);
540 * only a few subtle things...
542 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
544 struct snd_pcm_runtime *runtime = substream->runtime;
545 struct snd_usb_substream *subs = runtime->private_data;
546 struct usb_host_interface *alts;
547 struct usb_interface *iface;
550 if (! subs->cur_audiofmt) {
551 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
555 down_read(&subs->stream->chip->shutdown_rwsem);
556 if (subs->stream->chip->shutdown) {
560 if (snd_BUG_ON(!subs->data_endpoint)) {
565 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
566 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
568 ret = set_format(subs, subs->cur_audiofmt);
572 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
573 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
574 ret = snd_usb_init_sample_rate(subs->stream->chip,
575 subs->cur_audiofmt->iface,
582 if (subs->need_setup_ep) {
583 ret = configure_endpoint(subs);
586 subs->need_setup_ep = false;
589 /* some unit conversions in runtime */
590 subs->data_endpoint->maxframesize =
591 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
592 subs->data_endpoint->curframesize =
593 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
595 /* reset the pointer */
596 subs->hwptr_done = 0;
597 subs->transfer_done = 0;
598 subs->last_delay = 0;
599 subs->last_frame_number = 0;
602 /* for playback, submit the URBs now; otherwise, the first hwptr_done
603 * updates for all URBs would happen at the same time when starting */
604 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
605 ret = start_endpoints(subs, true);
608 up_read(&subs->stream->chip->shutdown_rwsem);
612 static struct snd_pcm_hardware snd_usb_hardware =
614 .info = SNDRV_PCM_INFO_MMAP |
615 SNDRV_PCM_INFO_MMAP_VALID |
616 SNDRV_PCM_INFO_BATCH |
617 SNDRV_PCM_INFO_INTERLEAVED |
618 SNDRV_PCM_INFO_BLOCK_TRANSFER |
619 SNDRV_PCM_INFO_PAUSE,
620 .buffer_bytes_max = 1024 * 1024,
621 .period_bytes_min = 64,
622 .period_bytes_max = 512 * 1024,
627 static int hw_check_valid_format(struct snd_usb_substream *subs,
628 struct snd_pcm_hw_params *params,
629 struct audioformat *fp)
631 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
632 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
633 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
634 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
635 struct snd_mask check_fmts;
638 /* check the format */
639 snd_mask_none(&check_fmts);
640 check_fmts.bits[0] = (u32)fp->formats;
641 check_fmts.bits[1] = (u32)(fp->formats >> 32);
642 snd_mask_intersect(&check_fmts, fmts);
643 if (snd_mask_empty(&check_fmts)) {
644 hwc_debug(" > check: no supported format %d\n", fp->format);
647 /* check the channels */
648 if (fp->channels < ct->min || fp->channels > ct->max) {
649 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
652 /* check the rate is within the range */
653 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
654 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
657 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
658 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
661 /* check whether the period time is >= the data packet interval */
662 if (subs->speed != USB_SPEED_FULL) {
663 ptime = 125 * (1 << fp->datainterval);
664 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
665 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
672 static int hw_rule_rate(struct snd_pcm_hw_params *params,
673 struct snd_pcm_hw_rule *rule)
675 struct snd_usb_substream *subs = rule->private;
677 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
678 unsigned int rmin, rmax;
681 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
684 list_for_each(p, &subs->fmt_list) {
685 struct audioformat *fp;
686 fp = list_entry(p, struct audioformat, list);
687 if (!hw_check_valid_format(subs, params, fp))
690 if (rmin > fp->rate_min)
692 if (rmax < fp->rate_max)
701 hwc_debug(" --> get empty\n");
707 if (it->min < rmin) {
712 if (it->max > rmax) {
717 if (snd_interval_checkempty(it)) {
721 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
726 static int hw_rule_channels(struct snd_pcm_hw_params *params,
727 struct snd_pcm_hw_rule *rule)
729 struct snd_usb_substream *subs = rule->private;
731 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
732 unsigned int rmin, rmax;
735 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
738 list_for_each(p, &subs->fmt_list) {
739 struct audioformat *fp;
740 fp = list_entry(p, struct audioformat, list);
741 if (!hw_check_valid_format(subs, params, fp))
744 if (rmin > fp->channels)
746 if (rmax < fp->channels)
755 hwc_debug(" --> get empty\n");
761 if (it->min < rmin) {
766 if (it->max > rmax) {
771 if (snd_interval_checkempty(it)) {
775 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
779 static int hw_rule_format(struct snd_pcm_hw_params *params,
780 struct snd_pcm_hw_rule *rule)
782 struct snd_usb_substream *subs = rule->private;
784 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
789 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
791 list_for_each(p, &subs->fmt_list) {
792 struct audioformat *fp;
793 fp = list_entry(p, struct audioformat, list);
794 if (!hw_check_valid_format(subs, params, fp))
796 fbits |= fp->formats;
799 oldbits[0] = fmt->bits[0];
800 oldbits[1] = fmt->bits[1];
801 fmt->bits[0] &= (u32)fbits;
802 fmt->bits[1] &= (u32)(fbits >> 32);
803 if (!fmt->bits[0] && !fmt->bits[1]) {
804 hwc_debug(" --> get empty\n");
807 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
808 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
812 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
813 struct snd_pcm_hw_rule *rule)
815 struct snd_usb_substream *subs = rule->private;
816 struct audioformat *fp;
817 struct snd_interval *it;
818 unsigned char min_datainterval;
822 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
823 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
824 min_datainterval = 0xff;
825 list_for_each_entry(fp, &subs->fmt_list, list) {
826 if (!hw_check_valid_format(subs, params, fp))
828 min_datainterval = min(min_datainterval, fp->datainterval);
830 if (min_datainterval == 0xff) {
831 hwc_debug(" --> get empty\n");
835 pmin = 125 * (1 << min_datainterval);
837 if (it->min < pmin) {
842 if (snd_interval_checkempty(it)) {
846 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
851 * If the device supports unusual bit rates, does the request meet these?
853 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
854 struct snd_usb_substream *subs)
856 struct audioformat *fp;
858 int count = 0, needs_knot = 0;
861 kfree(subs->rate_list.list);
862 subs->rate_list.list = NULL;
864 list_for_each_entry(fp, &subs->fmt_list, list) {
865 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
867 count += fp->nr_rates;
868 if (fp->rates & SNDRV_PCM_RATE_KNOT)
874 subs->rate_list.list = rate_list =
875 kmalloc(sizeof(int) * count, GFP_KERNEL);
876 if (!subs->rate_list.list)
878 subs->rate_list.count = count;
879 subs->rate_list.mask = 0;
881 list_for_each_entry(fp, &subs->fmt_list, list) {
883 for (i = 0; i < fp->nr_rates; i++)
884 rate_list[count++] = fp->rate_table[i];
886 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
896 * set up the runtime hardware information.
899 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
902 unsigned int pt, ptmin;
903 int param_period_time_if_needed;
906 runtime->hw.formats = subs->formats;
908 runtime->hw.rate_min = 0x7fffffff;
909 runtime->hw.rate_max = 0;
910 runtime->hw.channels_min = 256;
911 runtime->hw.channels_max = 0;
912 runtime->hw.rates = 0;
914 /* check min/max rates and channels */
915 list_for_each(p, &subs->fmt_list) {
916 struct audioformat *fp;
917 fp = list_entry(p, struct audioformat, list);
918 runtime->hw.rates |= fp->rates;
919 if (runtime->hw.rate_min > fp->rate_min)
920 runtime->hw.rate_min = fp->rate_min;
921 if (runtime->hw.rate_max < fp->rate_max)
922 runtime->hw.rate_max = fp->rate_max;
923 if (runtime->hw.channels_min > fp->channels)
924 runtime->hw.channels_min = fp->channels;
925 if (runtime->hw.channels_max < fp->channels)
926 runtime->hw.channels_max = fp->channels;
927 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
928 /* FIXME: there might be more than one audio formats... */
929 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
932 pt = 125 * (1 << fp->datainterval);
933 ptmin = min(ptmin, pt);
935 err = snd_usb_autoresume(subs->stream->chip);
939 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
940 if (subs->speed == USB_SPEED_FULL)
941 /* full speed devices have fixed data packet interval */
944 /* if period time doesn't go below 1 ms, no rules needed */
945 param_period_time_if_needed = -1;
946 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
949 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
951 SNDRV_PCM_HW_PARAM_FORMAT,
952 SNDRV_PCM_HW_PARAM_CHANNELS,
953 param_period_time_if_needed,
956 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
957 hw_rule_channels, subs,
958 SNDRV_PCM_HW_PARAM_FORMAT,
959 SNDRV_PCM_HW_PARAM_RATE,
960 param_period_time_if_needed,
963 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
964 hw_rule_format, subs,
965 SNDRV_PCM_HW_PARAM_RATE,
966 SNDRV_PCM_HW_PARAM_CHANNELS,
967 param_period_time_if_needed,
970 if (param_period_time_if_needed >= 0) {
971 err = snd_pcm_hw_rule_add(runtime, 0,
972 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
973 hw_rule_period_time, subs,
974 SNDRV_PCM_HW_PARAM_FORMAT,
975 SNDRV_PCM_HW_PARAM_CHANNELS,
976 SNDRV_PCM_HW_PARAM_RATE,
981 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
986 snd_usb_autosuspend(subs->stream->chip);
990 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
992 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
993 struct snd_pcm_runtime *runtime = substream->runtime;
994 struct snd_usb_substream *subs = &as->substream[direction];
996 subs->interface = -1;
997 subs->altset_idx = 0;
998 runtime->hw = snd_usb_hardware;
999 runtime->private_data = subs;
1000 subs->pcm_substream = substream;
1001 /* runtime PM is also done there */
1002 return setup_hw_info(runtime, subs);
1005 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1007 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1008 struct snd_usb_substream *subs = &as->substream[direction];
1010 stop_endpoints(subs, false);
1012 if (!as->chip->shutdown && subs->interface >= 0) {
1013 usb_set_interface(subs->dev, subs->interface, 0);
1014 subs->interface = -1;
1017 subs->pcm_substream = NULL;
1018 snd_usb_autosuspend(subs->stream->chip);
1023 /* Since a URB can handle only a single linear buffer, we must use double
1024 * buffering when the data to be transferred overflows the buffer boundary.
1025 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1028 static void retire_capture_urb(struct snd_usb_substream *subs,
1031 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1032 unsigned int stride, frames, bytes, oldptr;
1033 int i, period_elapsed = 0;
1034 unsigned long flags;
1037 stride = runtime->frame_bits >> 3;
1039 for (i = 0; i < urb->number_of_packets; i++) {
1040 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1041 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1042 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1045 bytes = urb->iso_frame_desc[i].actual_length;
1046 frames = bytes / stride;
1047 if (!subs->txfr_quirk)
1048 bytes = frames * stride;
1049 if (bytes % (runtime->sample_bits >> 3) != 0) {
1050 #ifdef CONFIG_SND_DEBUG_VERBOSE
1051 int oldbytes = bytes;
1053 bytes = frames * stride;
1054 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1057 /* update the current pointer */
1058 spin_lock_irqsave(&subs->lock, flags);
1059 oldptr = subs->hwptr_done;
1060 subs->hwptr_done += bytes;
1061 if (subs->hwptr_done >= runtime->buffer_size * stride)
1062 subs->hwptr_done -= runtime->buffer_size * stride;
1063 frames = (bytes + (oldptr % stride)) / stride;
1064 subs->transfer_done += frames;
1065 if (subs->transfer_done >= runtime->period_size) {
1066 subs->transfer_done -= runtime->period_size;
1069 spin_unlock_irqrestore(&subs->lock, flags);
1070 /* copy a data chunk */
1071 if (oldptr + bytes > runtime->buffer_size * stride) {
1072 unsigned int bytes1 =
1073 runtime->buffer_size * stride - oldptr;
1074 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1075 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1077 memcpy(runtime->dma_area + oldptr, cp, bytes);
1082 snd_pcm_period_elapsed(subs->pcm_substream);
1085 static void prepare_playback_urb(struct snd_usb_substream *subs,
1088 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1089 struct snd_usb_endpoint *ep = subs->data_endpoint;
1090 struct snd_urb_ctx *ctx = urb->context;
1091 unsigned int counts, frames, bytes;
1092 int i, stride, period_elapsed = 0;
1093 unsigned long flags;
1095 stride = runtime->frame_bits >> 3;
1098 urb->number_of_packets = 0;
1099 spin_lock_irqsave(&subs->lock, flags);
1100 for (i = 0; i < ctx->packets; i++) {
1101 if (ctx->packet_size[i])
1102 counts = ctx->packet_size[i];
1104 counts = snd_usb_endpoint_next_packet_size(ep);
1106 /* set up descriptor */
1107 urb->iso_frame_desc[i].offset = frames * stride;
1108 urb->iso_frame_desc[i].length = counts * stride;
1110 urb->number_of_packets++;
1111 subs->transfer_done += counts;
1112 if (subs->transfer_done >= runtime->period_size) {
1113 subs->transfer_done -= runtime->period_size;
1115 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1116 if (subs->transfer_done > 0) {
1117 /* FIXME: fill-max mode is not
1119 frames -= subs->transfer_done;
1120 counts -= subs->transfer_done;
1121 urb->iso_frame_desc[i].length =
1123 subs->transfer_done = 0;
1126 if (i < ctx->packets) {
1127 /* add a transfer delimiter */
1128 urb->iso_frame_desc[i].offset =
1130 urb->iso_frame_desc[i].length = 0;
1131 urb->number_of_packets++;
1136 if (period_elapsed &&
1137 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1140 bytes = frames * stride;
1141 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1142 /* err, the transferred area goes over buffer boundary. */
1143 unsigned int bytes1 =
1144 runtime->buffer_size * stride - subs->hwptr_done;
1145 memcpy(urb->transfer_buffer,
1146 runtime->dma_area + subs->hwptr_done, bytes1);
1147 memcpy(urb->transfer_buffer + bytes1,
1148 runtime->dma_area, bytes - bytes1);
1150 memcpy(urb->transfer_buffer,
1151 runtime->dma_area + subs->hwptr_done, bytes);
1153 subs->hwptr_done += bytes;
1154 if (subs->hwptr_done >= runtime->buffer_size * stride)
1155 subs->hwptr_done -= runtime->buffer_size * stride;
1157 /* update delay with exact number of samples queued */
1158 runtime->delay = subs->last_delay;
1159 runtime->delay += frames;
1160 subs->last_delay = runtime->delay;
1162 /* realign last_frame_number */
1163 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1164 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1166 spin_unlock_irqrestore(&subs->lock, flags);
1167 urb->transfer_buffer_length = bytes;
1169 snd_pcm_period_elapsed(subs->pcm_substream);
1173 * process after playback data complete
1174 * - decrease the delay count again
1176 static void retire_playback_urb(struct snd_usb_substream *subs,
1179 unsigned long flags;
1180 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1181 int stride = runtime->frame_bits >> 3;
1182 int processed = urb->transfer_buffer_length / stride;
1185 /* ignore the delay accounting when procssed=0 is given, i.e.
1186 * silent payloads are procssed before handling the actual data
1191 spin_lock_irqsave(&subs->lock, flags);
1192 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1193 /* update delay with exact number of samples played */
1194 if (processed > subs->last_delay)
1195 subs->last_delay = 0;
1197 subs->last_delay -= processed;
1198 runtime->delay = subs->last_delay;
1201 * Report when delay estimate is off by more than 2ms.
1202 * The error should be lower than 2ms since the estimate relies
1203 * on two reads of a counter updated every ms.
1205 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1206 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1207 est_delay, subs->last_delay);
1209 spin_unlock_irqrestore(&subs->lock, flags);
1212 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1214 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1217 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1219 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1222 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1224 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1227 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1229 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1232 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1235 struct snd_usb_substream *subs = substream->runtime->private_data;
1238 case SNDRV_PCM_TRIGGER_START:
1239 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1240 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1241 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1244 case SNDRV_PCM_TRIGGER_STOP:
1245 stop_endpoints(subs, false);
1248 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1249 subs->data_endpoint->prepare_data_urb = NULL;
1250 subs->data_endpoint->retire_data_urb = NULL;
1258 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1262 struct snd_usb_substream *subs = substream->runtime->private_data;
1265 case SNDRV_PCM_TRIGGER_START:
1266 err = start_endpoints(subs, false);
1270 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1273 case SNDRV_PCM_TRIGGER_STOP:
1274 stop_endpoints(subs, false);
1277 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1278 subs->data_endpoint->retire_data_urb = NULL;
1281 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1282 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1290 static struct snd_pcm_ops snd_usb_playback_ops = {
1291 .open = snd_usb_playback_open,
1292 .close = snd_usb_playback_close,
1293 .ioctl = snd_pcm_lib_ioctl,
1294 .hw_params = snd_usb_hw_params,
1295 .hw_free = snd_usb_hw_free,
1296 .prepare = snd_usb_pcm_prepare,
1297 .trigger = snd_usb_substream_playback_trigger,
1298 .pointer = snd_usb_pcm_pointer,
1299 .page = snd_pcm_lib_get_vmalloc_page,
1300 .mmap = snd_pcm_lib_mmap_vmalloc,
1303 static struct snd_pcm_ops snd_usb_capture_ops = {
1304 .open = snd_usb_capture_open,
1305 .close = snd_usb_capture_close,
1306 .ioctl = snd_pcm_lib_ioctl,
1307 .hw_params = snd_usb_hw_params,
1308 .hw_free = snd_usb_hw_free,
1309 .prepare = snd_usb_pcm_prepare,
1310 .trigger = snd_usb_substream_capture_trigger,
1311 .pointer = snd_usb_pcm_pointer,
1312 .page = snd_pcm_lib_get_vmalloc_page,
1313 .mmap = snd_pcm_lib_mmap_vmalloc,
1316 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1318 snd_pcm_set_ops(pcm, stream,
1319 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1320 &snd_usb_playback_ops : &snd_usb_capture_ops);