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 if (!subs->last_delay)
50 return 0; /* short path */
52 current_frame_number = usb_get_current_frame_number(subs->dev);
54 * HCD implementations use different widths, use lower 8 bits.
55 * The delay will be managed up to 256ms, which is more than
58 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
60 /* Approximation based on number of samples per USB frame (ms),
61 some truncation for 44.1 but the estimate is good enough */
62 est_delay = subs->last_delay - (frame_diff * rate / 1000);
69 * return the current pcm pointer. just based on the hwptr_done value.
71 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
73 struct snd_usb_substream *subs;
74 unsigned int hwptr_done;
76 subs = (struct snd_usb_substream *)substream->runtime->private_data;
77 if (subs->stream->chip->shutdown)
78 return SNDRV_PCM_POS_XRUN;
79 spin_lock(&subs->lock);
80 hwptr_done = subs->hwptr_done;
81 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
82 substream->runtime->delay = snd_usb_pcm_delay(subs,
83 substream->runtime->rate);
84 spin_unlock(&subs->lock);
85 return hwptr_done / (substream->runtime->frame_bits >> 3);
89 * find a matching audio format
91 static struct audioformat *find_format(struct snd_usb_substream *subs)
94 struct audioformat *found = NULL;
95 int cur_attr = 0, attr;
97 list_for_each(p, &subs->fmt_list) {
98 struct audioformat *fp;
99 fp = list_entry(p, struct audioformat, list);
100 if (!(fp->formats & (1uLL << subs->pcm_format)))
102 if (fp->channels != subs->channels)
104 if (subs->cur_rate < fp->rate_min ||
105 subs->cur_rate > fp->rate_max)
107 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
109 for (i = 0; i < fp->nr_rates; i++)
110 if (fp->rate_table[i] == subs->cur_rate)
112 if (i >= fp->nr_rates)
115 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
121 /* avoid async out and adaptive in if the other method
122 * supports the same format.
123 * this is a workaround for the case like
124 * M-audio audiophile USB.
126 if (attr != cur_attr) {
127 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
128 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
129 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
130 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
132 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
133 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
134 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
135 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
141 /* find the format with the largest max. packet size */
142 if (fp->maxpacksize > found->maxpacksize) {
150 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
151 struct usb_host_interface *alts,
152 struct audioformat *fmt)
154 struct usb_device *dev = chip->dev;
156 unsigned char data[1];
159 ep = get_endpoint(alts, 0)->bEndpointAddress;
162 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
163 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
164 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
165 data, sizeof(data))) < 0) {
166 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
167 dev->devnum, iface, ep);
174 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
175 struct usb_host_interface *alts,
176 struct audioformat *fmt)
178 struct usb_device *dev = chip->dev;
179 unsigned char data[1];
183 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
184 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
185 UAC2_EP_CS_PITCH << 8, 0,
186 data, sizeof(data))) < 0) {
187 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
188 dev->devnum, iface, fmt->altsetting);
196 * initialize the pitch control and sample rate
198 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
199 struct usb_host_interface *alts,
200 struct audioformat *fmt)
202 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
204 /* if endpoint doesn't have pitch control, bail out */
205 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
208 switch (altsd->bInterfaceProtocol) {
211 return init_pitch_v1(chip, iface, alts, fmt);
214 return init_pitch_v2(chip, iface, alts, fmt);
218 static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
222 if (!subs->data_endpoint)
225 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
226 struct snd_usb_endpoint *ep = subs->data_endpoint;
228 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
230 ep->data_subs = subs;
231 err = snd_usb_endpoint_start(ep, can_sleep);
233 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
238 if (subs->sync_endpoint &&
239 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
240 struct snd_usb_endpoint *ep = subs->sync_endpoint;
242 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
243 subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
244 err = usb_set_interface(subs->dev,
245 subs->sync_endpoint->iface,
246 subs->sync_endpoint->alt_idx);
249 "%d:%d:%d: cannot set interface (%d)\n",
251 subs->sync_endpoint->iface,
252 subs->sync_endpoint->alt_idx, err);
257 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
259 ep->sync_slave = subs->data_endpoint;
260 err = snd_usb_endpoint_start(ep, can_sleep);
262 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
270 static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
272 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
273 snd_usb_endpoint_stop(subs->sync_endpoint);
275 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
276 snd_usb_endpoint_stop(subs->data_endpoint);
279 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
280 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
284 static int deactivate_endpoints(struct snd_usb_substream *subs)
288 reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
289 retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
301 * find a matching format and set up the interface
303 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
305 struct usb_device *dev = subs->dev;
306 struct usb_host_interface *alts;
307 struct usb_interface_descriptor *altsd;
308 struct usb_interface *iface;
309 unsigned int ep, attr;
310 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
311 int err, implicit_fb = 0;
313 iface = usb_ifnum_to_if(dev, fmt->iface);
316 alts = &iface->altsetting[fmt->altset_idx];
317 altsd = get_iface_desc(alts);
318 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
321 if (fmt == subs->cur_audiofmt)
324 /* close the old interface */
325 if (subs->interface >= 0 && subs->interface != fmt->iface) {
326 err = usb_set_interface(subs->dev, subs->interface, 0);
328 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
329 dev->devnum, fmt->iface, fmt->altsetting, err);
332 subs->interface = -1;
333 subs->altset_idx = 0;
337 if (subs->interface != fmt->iface ||
338 subs->altset_idx != fmt->altset_idx) {
339 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
341 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
342 dev->devnum, fmt->iface, fmt->altsetting, err);
345 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
346 fmt->iface, fmt->altsetting);
347 subs->interface = fmt->iface;
348 subs->altset_idx = fmt->altset_idx;
351 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
352 alts, fmt->endpoint, subs->direction,
353 SND_USB_ENDPOINT_TYPE_DATA);
354 if (!subs->data_endpoint)
357 /* we need a sync pipe in async OUT or adaptive IN mode */
358 /* check the number of EP, since some devices have broken
359 * descriptors which fool us. if it has only one EP,
360 * assume it as adaptive-out or sync-in.
362 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
364 switch (subs->stream->chip->usb_id) {
365 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
369 iface = usb_ifnum_to_if(dev, 3);
371 if (!iface || iface->num_altsetting == 0)
374 alts = &iface->altsetting[1];
378 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
379 case USB_ID(0x0763, 0x2081):
383 iface = usb_ifnum_to_if(dev, 2);
385 if (!iface || iface->num_altsetting == 0)
388 alts = &iface->altsetting[1];
393 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
394 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
395 altsd->bNumEndpoints >= 2) {
396 /* check sync-pipe endpoint */
397 /* ... and check descriptor size before accessing bSynchAddress
398 because there is a version of the SB Audigy 2 NX firmware lacking
399 the audio fields in the endpoint descriptors */
400 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
401 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
402 get_endpoint(alts, 1)->bSynchAddress != 0 &&
404 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
405 dev->devnum, fmt->iface, fmt->altsetting,
406 get_endpoint(alts, 1)->bmAttributes,
407 get_endpoint(alts, 1)->bLength,
408 get_endpoint(alts, 1)->bSynchAddress);
411 ep = get_endpoint(alts, 1)->bEndpointAddress;
413 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
414 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
415 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
416 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
417 dev->devnum, fmt->iface, fmt->altsetting,
418 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
422 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
423 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
426 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
427 alts, ep, !subs->direction,
429 SND_USB_ENDPOINT_TYPE_DATA :
430 SND_USB_ENDPOINT_TYPE_SYNC);
431 if (!subs->sync_endpoint)
434 subs->data_endpoint->sync_master = subs->sync_endpoint;
437 if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
440 subs->cur_audiofmt = fmt;
442 snd_usb_set_format_quirk(subs, fmt);
446 "setting done: format = %d, rate = %d..%d, channels = %d\n",
447 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
449 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
450 subs->datapipe, subs->syncpipe);
457 * Return the score of matching two audioformats.
458 * Veto the audioformat if:
459 * - It has no channels for some reason.
460 * - Requested PCM format is not supported.
461 * - Requested sample rate is not supported.
463 static int match_endpoint_audioformats(struct audioformat *fp,
464 struct audioformat *match, int rate,
465 snd_pcm_format_t pcm_format)
470 if (fp->channels < 1) {
471 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
475 if (!(fp->formats & (1ULL << pcm_format))) {
476 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
481 for (i = 0; i < fp->nr_rates; i++) {
482 if (fp->rate_table[i] == rate) {
488 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
493 if (fp->channels == match->channels)
496 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
502 * Configure the sync ep using the rate and pcm format of the data ep.
504 static int configure_sync_endpoint(struct snd_usb_substream *subs)
507 struct audioformat *fp;
508 struct audioformat *sync_fp = NULL;
510 int sync_period_bytes = subs->period_bytes;
511 struct snd_usb_substream *sync_subs =
512 &subs->stream->substream[subs->direction ^ 1];
514 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
516 return snd_usb_endpoint_set_params(subs->sync_endpoint,
524 /* Try to find the best matching audioformat. */
525 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
526 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
527 subs->cur_rate, subs->pcm_format);
529 if (score > cur_score) {
535 if (unlikely(sync_fp == NULL)) {
536 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
537 __func__, sync_subs->ep_num);
542 * Recalculate the period bytes if channel number differ between
543 * data and sync ep audioformat.
545 if (sync_fp->channels != subs->channels) {
546 sync_period_bytes = (subs->period_bytes / subs->channels) *
548 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
549 __func__, subs->period_bytes, sync_period_bytes);
552 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
564 * configure endpoint params
566 * called during initial setup and upon resume
568 static int configure_endpoint(struct snd_usb_substream *subs)
573 stop_endpoints(subs, true);
574 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
580 subs->sync_endpoint);
584 if (subs->sync_endpoint)
585 ret = configure_sync_endpoint(subs);
593 * allocate a buffer and set the given audio format.
595 * so far we use a physically linear buffer although packetize transfer
596 * doesn't need a continuous area.
597 * if sg buffer is supported on the later version of alsa, we'll follow
600 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
601 struct snd_pcm_hw_params *hw_params)
603 struct snd_usb_substream *subs = substream->runtime->private_data;
604 struct audioformat *fmt;
607 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
608 params_buffer_bytes(hw_params));
612 subs->pcm_format = params_format(hw_params);
613 subs->period_bytes = params_period_bytes(hw_params);
614 subs->channels = params_channels(hw_params);
615 subs->cur_rate = params_rate(hw_params);
617 fmt = find_format(subs);
619 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
620 subs->pcm_format, subs->cur_rate, subs->channels);
624 down_read(&subs->stream->chip->shutdown_rwsem);
625 if (subs->stream->chip->shutdown)
628 ret = set_format(subs, fmt);
629 up_read(&subs->stream->chip->shutdown_rwsem);
633 subs->interface = fmt->iface;
634 subs->altset_idx = fmt->altset_idx;
635 subs->need_setup_ep = true;
643 * reset the audio format and release the buffer
645 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
647 struct snd_usb_substream *subs = substream->runtime->private_data;
649 subs->cur_audiofmt = NULL;
651 subs->period_bytes = 0;
652 down_read(&subs->stream->chip->shutdown_rwsem);
653 if (!subs->stream->chip->shutdown) {
654 stop_endpoints(subs, true);
655 deactivate_endpoints(subs);
657 up_read(&subs->stream->chip->shutdown_rwsem);
658 return snd_pcm_lib_free_vmalloc_buffer(substream);
664 * only a few subtle things...
666 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
668 struct snd_pcm_runtime *runtime = substream->runtime;
669 struct snd_usb_substream *subs = runtime->private_data;
670 struct usb_host_interface *alts;
671 struct usb_interface *iface;
674 if (! subs->cur_audiofmt) {
675 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
679 down_read(&subs->stream->chip->shutdown_rwsem);
680 if (subs->stream->chip->shutdown) {
684 if (snd_BUG_ON(!subs->data_endpoint)) {
689 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
690 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
692 ret = set_format(subs, subs->cur_audiofmt);
696 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
697 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
698 ret = snd_usb_init_sample_rate(subs->stream->chip,
699 subs->cur_audiofmt->iface,
706 if (subs->need_setup_ep) {
707 ret = configure_endpoint(subs);
710 subs->need_setup_ep = false;
713 /* some unit conversions in runtime */
714 subs->data_endpoint->maxframesize =
715 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
716 subs->data_endpoint->curframesize =
717 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
719 /* reset the pointer */
720 subs->hwptr_done = 0;
721 subs->transfer_done = 0;
722 subs->last_delay = 0;
723 subs->last_frame_number = 0;
726 /* for playback, submit the URBs now; otherwise, the first hwptr_done
727 * updates for all URBs would happen at the same time when starting */
728 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
729 ret = start_endpoints(subs, true);
732 up_read(&subs->stream->chip->shutdown_rwsem);
736 static struct snd_pcm_hardware snd_usb_hardware =
738 .info = SNDRV_PCM_INFO_MMAP |
739 SNDRV_PCM_INFO_MMAP_VALID |
740 SNDRV_PCM_INFO_BATCH |
741 SNDRV_PCM_INFO_INTERLEAVED |
742 SNDRV_PCM_INFO_BLOCK_TRANSFER |
743 SNDRV_PCM_INFO_PAUSE,
744 .buffer_bytes_max = 1024 * 1024,
745 .period_bytes_min = 64,
746 .period_bytes_max = 512 * 1024,
751 static int hw_check_valid_format(struct snd_usb_substream *subs,
752 struct snd_pcm_hw_params *params,
753 struct audioformat *fp)
755 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
756 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
757 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
758 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
759 struct snd_mask check_fmts;
762 /* check the format */
763 snd_mask_none(&check_fmts);
764 check_fmts.bits[0] = (u32)fp->formats;
765 check_fmts.bits[1] = (u32)(fp->formats >> 32);
766 snd_mask_intersect(&check_fmts, fmts);
767 if (snd_mask_empty(&check_fmts)) {
768 hwc_debug(" > check: no supported format %d\n", fp->format);
771 /* check the channels */
772 if (fp->channels < ct->min || fp->channels > ct->max) {
773 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
776 /* check the rate is within the range */
777 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
778 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
781 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
782 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
785 /* check whether the period time is >= the data packet interval */
786 if (subs->speed != USB_SPEED_FULL) {
787 ptime = 125 * (1 << fp->datainterval);
788 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
789 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
796 static int hw_rule_rate(struct snd_pcm_hw_params *params,
797 struct snd_pcm_hw_rule *rule)
799 struct snd_usb_substream *subs = rule->private;
801 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
802 unsigned int rmin, rmax;
805 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
808 list_for_each(p, &subs->fmt_list) {
809 struct audioformat *fp;
810 fp = list_entry(p, struct audioformat, list);
811 if (!hw_check_valid_format(subs, params, fp))
814 if (rmin > fp->rate_min)
816 if (rmax < fp->rate_max)
825 hwc_debug(" --> get empty\n");
831 if (it->min < rmin) {
836 if (it->max > rmax) {
841 if (snd_interval_checkempty(it)) {
845 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
850 static int hw_rule_channels(struct snd_pcm_hw_params *params,
851 struct snd_pcm_hw_rule *rule)
853 struct snd_usb_substream *subs = rule->private;
855 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
856 unsigned int rmin, rmax;
859 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
862 list_for_each(p, &subs->fmt_list) {
863 struct audioformat *fp;
864 fp = list_entry(p, struct audioformat, list);
865 if (!hw_check_valid_format(subs, params, fp))
868 if (rmin > fp->channels)
870 if (rmax < fp->channels)
879 hwc_debug(" --> get empty\n");
885 if (it->min < rmin) {
890 if (it->max > rmax) {
895 if (snd_interval_checkempty(it)) {
899 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
903 static int hw_rule_format(struct snd_pcm_hw_params *params,
904 struct snd_pcm_hw_rule *rule)
906 struct snd_usb_substream *subs = rule->private;
908 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
913 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
915 list_for_each(p, &subs->fmt_list) {
916 struct audioformat *fp;
917 fp = list_entry(p, struct audioformat, list);
918 if (!hw_check_valid_format(subs, params, fp))
920 fbits |= fp->formats;
923 oldbits[0] = fmt->bits[0];
924 oldbits[1] = fmt->bits[1];
925 fmt->bits[0] &= (u32)fbits;
926 fmt->bits[1] &= (u32)(fbits >> 32);
927 if (!fmt->bits[0] && !fmt->bits[1]) {
928 hwc_debug(" --> get empty\n");
931 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
932 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
936 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
937 struct snd_pcm_hw_rule *rule)
939 struct snd_usb_substream *subs = rule->private;
940 struct audioformat *fp;
941 struct snd_interval *it;
942 unsigned char min_datainterval;
946 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
947 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
948 min_datainterval = 0xff;
949 list_for_each_entry(fp, &subs->fmt_list, list) {
950 if (!hw_check_valid_format(subs, params, fp))
952 min_datainterval = min(min_datainterval, fp->datainterval);
954 if (min_datainterval == 0xff) {
955 hwc_debug(" --> get empty\n");
959 pmin = 125 * (1 << min_datainterval);
961 if (it->min < pmin) {
966 if (snd_interval_checkempty(it)) {
970 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
975 * If the device supports unusual bit rates, does the request meet these?
977 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
978 struct snd_usb_substream *subs)
980 struct audioformat *fp;
982 int count = 0, needs_knot = 0;
985 kfree(subs->rate_list.list);
986 subs->rate_list.list = NULL;
988 list_for_each_entry(fp, &subs->fmt_list, list) {
989 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
991 count += fp->nr_rates;
992 if (fp->rates & SNDRV_PCM_RATE_KNOT)
998 subs->rate_list.list = rate_list =
999 kmalloc(sizeof(int) * count, GFP_KERNEL);
1000 if (!subs->rate_list.list)
1002 subs->rate_list.count = count;
1003 subs->rate_list.mask = 0;
1005 list_for_each_entry(fp, &subs->fmt_list, list) {
1007 for (i = 0; i < fp->nr_rates; i++)
1008 rate_list[count++] = fp->rate_table[i];
1010 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1020 * set up the runtime hardware information.
1023 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1025 struct list_head *p;
1026 unsigned int pt, ptmin;
1027 int param_period_time_if_needed;
1030 runtime->hw.formats = subs->formats;
1032 runtime->hw.rate_min = 0x7fffffff;
1033 runtime->hw.rate_max = 0;
1034 runtime->hw.channels_min = 256;
1035 runtime->hw.channels_max = 0;
1036 runtime->hw.rates = 0;
1038 /* check min/max rates and channels */
1039 list_for_each(p, &subs->fmt_list) {
1040 struct audioformat *fp;
1041 fp = list_entry(p, struct audioformat, list);
1042 runtime->hw.rates |= fp->rates;
1043 if (runtime->hw.rate_min > fp->rate_min)
1044 runtime->hw.rate_min = fp->rate_min;
1045 if (runtime->hw.rate_max < fp->rate_max)
1046 runtime->hw.rate_max = fp->rate_max;
1047 if (runtime->hw.channels_min > fp->channels)
1048 runtime->hw.channels_min = fp->channels;
1049 if (runtime->hw.channels_max < fp->channels)
1050 runtime->hw.channels_max = fp->channels;
1051 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1052 /* FIXME: there might be more than one audio formats... */
1053 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1056 pt = 125 * (1 << fp->datainterval);
1057 ptmin = min(ptmin, pt);
1059 err = snd_usb_autoresume(subs->stream->chip);
1063 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1064 if (subs->speed == USB_SPEED_FULL)
1065 /* full speed devices have fixed data packet interval */
1068 /* if period time doesn't go below 1 ms, no rules needed */
1069 param_period_time_if_needed = -1;
1070 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1073 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1075 SNDRV_PCM_HW_PARAM_FORMAT,
1076 SNDRV_PCM_HW_PARAM_CHANNELS,
1077 param_period_time_if_needed,
1080 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1081 hw_rule_channels, subs,
1082 SNDRV_PCM_HW_PARAM_FORMAT,
1083 SNDRV_PCM_HW_PARAM_RATE,
1084 param_period_time_if_needed,
1087 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1088 hw_rule_format, subs,
1089 SNDRV_PCM_HW_PARAM_RATE,
1090 SNDRV_PCM_HW_PARAM_CHANNELS,
1091 param_period_time_if_needed,
1094 if (param_period_time_if_needed >= 0) {
1095 err = snd_pcm_hw_rule_add(runtime, 0,
1096 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1097 hw_rule_period_time, subs,
1098 SNDRV_PCM_HW_PARAM_FORMAT,
1099 SNDRV_PCM_HW_PARAM_CHANNELS,
1100 SNDRV_PCM_HW_PARAM_RATE,
1105 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1110 snd_usb_autosuspend(subs->stream->chip);
1114 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1116 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1117 struct snd_pcm_runtime *runtime = substream->runtime;
1118 struct snd_usb_substream *subs = &as->substream[direction];
1120 subs->interface = -1;
1121 subs->altset_idx = 0;
1122 runtime->hw = snd_usb_hardware;
1123 runtime->private_data = subs;
1124 subs->pcm_substream = substream;
1125 /* runtime PM is also done there */
1126 return setup_hw_info(runtime, subs);
1129 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1131 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1132 struct snd_usb_substream *subs = &as->substream[direction];
1134 stop_endpoints(subs, true);
1136 if (!as->chip->shutdown && subs->interface >= 0) {
1137 usb_set_interface(subs->dev, subs->interface, 0);
1138 subs->interface = -1;
1141 subs->pcm_substream = NULL;
1142 snd_usb_autosuspend(subs->stream->chip);
1147 /* Since a URB can handle only a single linear buffer, we must use double
1148 * buffering when the data to be transferred overflows the buffer boundary.
1149 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1152 static void retire_capture_urb(struct snd_usb_substream *subs,
1155 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1156 unsigned int stride, frames, bytes, oldptr;
1157 int i, period_elapsed = 0;
1158 unsigned long flags;
1161 stride = runtime->frame_bits >> 3;
1163 for (i = 0; i < urb->number_of_packets; i++) {
1164 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1165 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1166 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1169 bytes = urb->iso_frame_desc[i].actual_length;
1170 frames = bytes / stride;
1171 if (!subs->txfr_quirk)
1172 bytes = frames * stride;
1173 if (bytes % (runtime->sample_bits >> 3) != 0) {
1174 #ifdef CONFIG_SND_DEBUG_VERBOSE
1175 int oldbytes = bytes;
1177 bytes = frames * stride;
1178 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1181 /* update the current pointer */
1182 spin_lock_irqsave(&subs->lock, flags);
1183 oldptr = subs->hwptr_done;
1184 subs->hwptr_done += bytes;
1185 if (subs->hwptr_done >= runtime->buffer_size * stride)
1186 subs->hwptr_done -= runtime->buffer_size * stride;
1187 frames = (bytes + (oldptr % stride)) / stride;
1188 subs->transfer_done += frames;
1189 if (subs->transfer_done >= runtime->period_size) {
1190 subs->transfer_done -= runtime->period_size;
1193 spin_unlock_irqrestore(&subs->lock, flags);
1194 /* copy a data chunk */
1195 if (oldptr + bytes > runtime->buffer_size * stride) {
1196 unsigned int bytes1 =
1197 runtime->buffer_size * stride - oldptr;
1198 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1199 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1201 memcpy(runtime->dma_area + oldptr, cp, bytes);
1206 snd_pcm_period_elapsed(subs->pcm_substream);
1209 static void prepare_playback_urb(struct snd_usb_substream *subs,
1212 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1213 struct snd_usb_endpoint *ep = subs->data_endpoint;
1214 struct snd_urb_ctx *ctx = urb->context;
1215 unsigned int counts, frames, bytes;
1216 int i, stride, period_elapsed = 0;
1217 unsigned long flags;
1219 stride = runtime->frame_bits >> 3;
1222 urb->number_of_packets = 0;
1223 spin_lock_irqsave(&subs->lock, flags);
1224 for (i = 0; i < ctx->packets; i++) {
1225 if (ctx->packet_size[i])
1226 counts = ctx->packet_size[i];
1228 counts = snd_usb_endpoint_next_packet_size(ep);
1230 /* set up descriptor */
1231 urb->iso_frame_desc[i].offset = frames * stride;
1232 urb->iso_frame_desc[i].length = counts * stride;
1234 urb->number_of_packets++;
1235 subs->transfer_done += counts;
1236 if (subs->transfer_done >= runtime->period_size) {
1237 subs->transfer_done -= runtime->period_size;
1239 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1240 if (subs->transfer_done > 0) {
1241 /* FIXME: fill-max mode is not
1243 frames -= subs->transfer_done;
1244 counts -= subs->transfer_done;
1245 urb->iso_frame_desc[i].length =
1247 subs->transfer_done = 0;
1250 if (i < ctx->packets) {
1251 /* add a transfer delimiter */
1252 urb->iso_frame_desc[i].offset =
1254 urb->iso_frame_desc[i].length = 0;
1255 urb->number_of_packets++;
1260 if (period_elapsed &&
1261 !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1264 bytes = frames * stride;
1265 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1266 /* err, the transferred area goes over buffer boundary. */
1267 unsigned int bytes1 =
1268 runtime->buffer_size * stride - subs->hwptr_done;
1269 memcpy(urb->transfer_buffer,
1270 runtime->dma_area + subs->hwptr_done, bytes1);
1271 memcpy(urb->transfer_buffer + bytes1,
1272 runtime->dma_area, bytes - bytes1);
1274 memcpy(urb->transfer_buffer,
1275 runtime->dma_area + subs->hwptr_done, bytes);
1277 subs->hwptr_done += bytes;
1278 if (subs->hwptr_done >= runtime->buffer_size * stride)
1279 subs->hwptr_done -= runtime->buffer_size * stride;
1281 /* update delay with exact number of samples queued */
1282 runtime->delay = subs->last_delay;
1283 runtime->delay += frames;
1284 subs->last_delay = runtime->delay;
1286 /* realign last_frame_number */
1287 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1288 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1290 spin_unlock_irqrestore(&subs->lock, flags);
1291 urb->transfer_buffer_length = bytes;
1293 snd_pcm_period_elapsed(subs->pcm_substream);
1297 * process after playback data complete
1298 * - decrease the delay count again
1300 static void retire_playback_urb(struct snd_usb_substream *subs,
1303 unsigned long flags;
1304 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1305 int stride = runtime->frame_bits >> 3;
1306 int processed = urb->transfer_buffer_length / stride;
1309 /* ignore the delay accounting when procssed=0 is given, i.e.
1310 * silent payloads are procssed before handling the actual data
1315 spin_lock_irqsave(&subs->lock, flags);
1316 if (!subs->last_delay)
1317 goto out; /* short path */
1319 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1320 /* update delay with exact number of samples played */
1321 if (processed > subs->last_delay)
1322 subs->last_delay = 0;
1324 subs->last_delay -= processed;
1325 runtime->delay = subs->last_delay;
1328 * Report when delay estimate is off by more than 2ms.
1329 * The error should be lower than 2ms since the estimate relies
1330 * on two reads of a counter updated every ms.
1332 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1333 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1334 est_delay, subs->last_delay);
1336 if (!subs->running) {
1337 /* update last_frame_number for delay counting here since
1338 * prepare_playback_urb won't be called during pause
1340 subs->last_frame_number =
1341 usb_get_current_frame_number(subs->dev) & 0xff;
1345 spin_unlock_irqrestore(&subs->lock, flags);
1348 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1350 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1353 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1355 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1358 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1360 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1363 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1365 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1368 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1371 struct snd_usb_substream *subs = substream->runtime->private_data;
1374 case SNDRV_PCM_TRIGGER_START:
1375 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1376 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1377 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1380 case SNDRV_PCM_TRIGGER_STOP:
1381 stop_endpoints(subs, false);
1384 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1385 subs->data_endpoint->prepare_data_urb = NULL;
1386 /* keep retire_data_urb for delay calculation */
1387 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1395 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1399 struct snd_usb_substream *subs = substream->runtime->private_data;
1402 case SNDRV_PCM_TRIGGER_START:
1403 err = start_endpoints(subs, false);
1407 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1410 case SNDRV_PCM_TRIGGER_STOP:
1411 stop_endpoints(subs, false);
1414 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1415 subs->data_endpoint->retire_data_urb = NULL;
1418 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1419 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1427 static struct snd_pcm_ops snd_usb_playback_ops = {
1428 .open = snd_usb_playback_open,
1429 .close = snd_usb_playback_close,
1430 .ioctl = snd_pcm_lib_ioctl,
1431 .hw_params = snd_usb_hw_params,
1432 .hw_free = snd_usb_hw_free,
1433 .prepare = snd_usb_pcm_prepare,
1434 .trigger = snd_usb_substream_playback_trigger,
1435 .pointer = snd_usb_pcm_pointer,
1436 .page = snd_pcm_lib_get_vmalloc_page,
1437 .mmap = snd_pcm_lib_mmap_vmalloc,
1440 static struct snd_pcm_ops snd_usb_capture_ops = {
1441 .open = snd_usb_capture_open,
1442 .close = snd_usb_capture_close,
1443 .ioctl = snd_pcm_lib_ioctl,
1444 .hw_params = snd_usb_hw_params,
1445 .hw_free = snd_usb_hw_free,
1446 .prepare = snd_usb_pcm_prepare,
1447 .trigger = snd_usb_substream_capture_trigger,
1448 .pointer = snd_usb_pcm_pointer,
1449 .page = snd_pcm_lib_get_vmalloc_page,
1450 .mmap = snd_pcm_lib_mmap_vmalloc,
1453 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1455 snd_pcm_set_ops(pcm, stream,
1456 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1457 &snd_usb_playback_ops : &snd_usb_capture_ops);