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/bitrev.h>
20 #include <linux/ratelimit.h>
21 #include <linux/usb.h>
22 #include <linux/usb/audio.h>
23 #include <linux/usb/audio-v2.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
39 #define SUBSTREAM_FLAG_DATA_EP_STARTED 0
40 #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
42 /* return the estimated delay based on USB frame counters */
43 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
46 int current_frame_number;
50 if (!subs->last_delay)
51 return 0; /* short path */
53 current_frame_number = usb_get_current_frame_number(subs->dev);
55 * HCD implementations use different widths, use lower 8 bits.
56 * The delay will be managed up to 256ms, which is more than
59 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
61 /* Approximation based on number of samples per USB frame (ms),
62 some truncation for 44.1 but the estimate is good enough */
63 est_delay = frame_diff * rate / 1000;
64 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
65 est_delay = subs->last_delay - est_delay;
67 est_delay = subs->last_delay + est_delay;
75 * return the current pcm pointer. just based on the hwptr_done value.
77 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
79 struct snd_usb_substream *subs = substream->runtime->private_data;
80 unsigned int hwptr_done;
82 if (atomic_read(&subs->stream->chip->shutdown))
83 return SNDRV_PCM_POS_XRUN;
84 spin_lock(&subs->lock);
85 hwptr_done = subs->hwptr_done;
86 substream->runtime->delay = snd_usb_pcm_delay(subs,
87 substream->runtime->rate);
88 spin_unlock(&subs->lock);
89 return hwptr_done / (substream->runtime->frame_bits >> 3);
93 * find a matching audio format
95 static struct audioformat *find_format(struct snd_usb_substream *subs)
97 struct audioformat *fp;
98 struct audioformat *found = NULL;
99 int cur_attr = 0, attr;
101 list_for_each_entry(fp, &subs->fmt_list, list) {
102 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
104 if (fp->channels != subs->channels)
106 if (subs->cur_rate < fp->rate_min ||
107 subs->cur_rate > fp->rate_max)
109 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
111 for (i = 0; i < fp->nr_rates; i++)
112 if (fp->rate_table[i] == subs->cur_rate)
114 if (i >= fp->nr_rates)
117 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
123 /* avoid async out and adaptive in if the other method
124 * supports the same format.
125 * this is a workaround for the case like
126 * M-audio audiophile USB.
128 if (attr != cur_attr) {
129 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
130 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
131 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
132 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
134 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
135 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
136 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
137 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
143 /* find the format with the largest max. packet size */
144 if (fp->maxpacksize > found->maxpacksize) {
152 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
153 struct usb_host_interface *alts,
154 struct audioformat *fmt)
156 struct usb_device *dev = chip->dev;
158 unsigned char data[1];
161 if (get_iface_desc(alts)->bNumEndpoints < 1)
163 ep = get_endpoint(alts, 0)->bEndpointAddress;
166 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
167 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
168 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
171 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
179 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
180 struct usb_host_interface *alts,
181 struct audioformat *fmt)
183 struct usb_device *dev = chip->dev;
184 unsigned char data[1];
188 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
189 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
190 UAC2_EP_CS_PITCH << 8, 0,
193 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
194 iface, fmt->altsetting);
202 * initialize the pitch control and sample rate
204 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
205 struct usb_host_interface *alts,
206 struct audioformat *fmt)
208 /* if endpoint doesn't have pitch control, bail out */
209 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
212 switch (fmt->protocol) {
215 return init_pitch_v1(chip, iface, alts, fmt);
218 return init_pitch_v2(chip, iface, alts, fmt);
222 static int start_endpoints(struct snd_usb_substream *subs)
226 if (!subs->data_endpoint)
229 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
230 struct snd_usb_endpoint *ep = subs->data_endpoint;
232 dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);
234 ep->data_subs = subs;
235 err = snd_usb_endpoint_start(ep);
237 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
242 if (subs->sync_endpoint &&
243 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
244 struct snd_usb_endpoint *ep = subs->sync_endpoint;
246 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
247 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
248 err = usb_set_interface(subs->dev,
249 subs->sync_endpoint->iface,
250 subs->sync_endpoint->altsetting);
252 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
253 dev_err(&subs->dev->dev,
254 "%d:%d: cannot set interface (%d)\n",
255 subs->sync_endpoint->iface,
256 subs->sync_endpoint->altsetting, err);
261 dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);
263 ep->sync_slave = subs->data_endpoint;
264 err = snd_usb_endpoint_start(ep);
266 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
274 static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
276 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
277 snd_usb_endpoint_stop(subs->sync_endpoint);
279 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
280 snd_usb_endpoint_stop(subs->data_endpoint);
283 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
284 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
288 static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
289 unsigned int altsetting,
290 struct usb_host_interface **alts,
293 struct usb_interface *iface;
294 struct usb_interface_descriptor *altsd;
295 struct usb_endpoint_descriptor *epd;
297 iface = usb_ifnum_to_if(dev, ifnum);
298 if (!iface || iface->num_altsetting < altsetting + 1)
300 *alts = &iface->altsetting[altsetting];
301 altsd = get_iface_desc(*alts);
302 if (altsd->bAlternateSetting != altsetting ||
303 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
304 (altsd->bInterfaceSubClass != 2 &&
305 altsd->bInterfaceProtocol != 2 ) ||
306 altsd->bNumEndpoints < 1)
308 epd = get_endpoint(*alts, 0);
309 if (!usb_endpoint_is_isoc_in(epd) ||
310 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
311 USB_ENDPOINT_USAGE_IMPLICIT_FB)
313 *ep = epd->bEndpointAddress;
317 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
318 * applies. Returns 1 if a quirk was found.
320 static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
321 struct usb_device *dev,
322 struct usb_interface_descriptor *altsd,
325 struct usb_host_interface *alts;
326 struct usb_interface *iface;
330 /* Implicit feedback sync EPs consumers are always playback EPs */
331 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
334 switch (subs->stream->chip->usb_id) {
335 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
336 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
339 goto add_sync_ep_from_ifnum;
340 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
341 case USB_ID(0x0763, 0x2081):
344 goto add_sync_ep_from_ifnum;
345 case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
348 goto add_sync_ep_from_ifnum;
349 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
352 goto add_sync_ep_from_ifnum;
353 case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
356 goto add_sync_ep_from_ifnum;
359 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
360 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
361 altsd->bInterfaceProtocol == 2 &&
362 altsd->bNumEndpoints == 1 &&
363 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
364 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
365 altsd->bAlternateSetting,
373 add_sync_ep_from_ifnum:
374 iface = usb_ifnum_to_if(dev, ifnum);
376 if (!iface || iface->num_altsetting == 0)
379 alts = &iface->altsetting[1];
382 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
383 alts, ep, !subs->direction,
384 SND_USB_ENDPOINT_TYPE_DATA);
385 if (!subs->sync_endpoint)
388 subs->data_endpoint->sync_master = subs->sync_endpoint;
393 static int set_sync_endpoint(struct snd_usb_substream *subs,
394 struct audioformat *fmt,
395 struct usb_device *dev,
396 struct usb_host_interface *alts,
397 struct usb_interface_descriptor *altsd)
399 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
400 unsigned int ep, attr;
404 /* we need a sync pipe in async OUT or adaptive IN mode */
405 /* check the number of EP, since some devices have broken
406 * descriptors which fool us. if it has only one EP,
407 * assume it as adaptive-out or sync-in.
409 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
411 if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
412 (!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
415 * In these modes the notion of sync_endpoint is irrelevant.
416 * Reset pointers to avoid using stale data from previously
417 * used settings, e.g. when configuration and endpoints were
421 subs->sync_endpoint = NULL;
422 subs->data_endpoint->sync_master = NULL;
425 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
429 /* endpoint set by quirk */
433 if (altsd->bNumEndpoints < 2)
436 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
437 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
438 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
442 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
443 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
444 * error fall back to SYNC mode and don't create sync endpoint
447 /* check sync-pipe endpoint */
448 /* ... and check descriptor size before accessing bSynchAddress
449 because there is a version of the SB Audigy 2 NX firmware lacking
450 the audio fields in the endpoint descriptors */
451 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
452 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
453 get_endpoint(alts, 1)->bSynchAddress != 0)) {
455 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
456 fmt->iface, fmt->altsetting,
457 get_endpoint(alts, 1)->bmAttributes,
458 get_endpoint(alts, 1)->bLength,
459 get_endpoint(alts, 1)->bSynchAddress);
460 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
464 ep = get_endpoint(alts, 1)->bEndpointAddress;
465 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
466 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
467 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
469 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
470 fmt->iface, fmt->altsetting,
471 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
472 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
477 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
478 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
480 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
481 alts, ep, !subs->direction,
483 SND_USB_ENDPOINT_TYPE_DATA :
484 SND_USB_ENDPOINT_TYPE_SYNC);
485 if (!subs->sync_endpoint) {
486 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
491 subs->data_endpoint->sync_master = subs->sync_endpoint;
497 * find a matching format and set up the interface
499 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
501 struct usb_device *dev = subs->dev;
502 struct usb_host_interface *alts;
503 struct usb_interface_descriptor *altsd;
504 struct usb_interface *iface;
507 iface = usb_ifnum_to_if(dev, fmt->iface);
510 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
511 altsd = get_iface_desc(alts);
512 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
515 if (fmt == subs->cur_audiofmt)
518 /* close the old interface */
519 if (subs->interface >= 0 && subs->interface != fmt->iface) {
520 if (!subs->stream->chip->keep_iface) {
521 err = usb_set_interface(subs->dev, subs->interface, 0);
524 "%d:%d: return to setting 0 failed (%d)\n",
525 fmt->iface, fmt->altsetting, err);
529 subs->interface = -1;
530 subs->altset_idx = 0;
534 if (iface->cur_altsetting != alts) {
535 err = snd_usb_select_mode_quirk(subs, fmt);
539 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
542 "%d:%d: usb_set_interface failed (%d)\n",
543 fmt->iface, fmt->altsetting, err);
546 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
547 fmt->iface, fmt->altsetting);
548 snd_usb_set_interface_quirk(dev);
551 subs->interface = fmt->iface;
552 subs->altset_idx = fmt->altset_idx;
553 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
554 alts, fmt->endpoint, subs->direction,
555 SND_USB_ENDPOINT_TYPE_DATA);
557 if (!subs->data_endpoint)
560 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
564 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
568 subs->cur_audiofmt = fmt;
570 snd_usb_set_format_quirk(subs, fmt);
576 * Return the score of matching two audioformats.
577 * Veto the audioformat if:
578 * - It has no channels for some reason.
579 * - Requested PCM format is not supported.
580 * - Requested sample rate is not supported.
582 static int match_endpoint_audioformats(struct snd_usb_substream *subs,
583 struct audioformat *fp,
584 struct audioformat *match, int rate,
585 snd_pcm_format_t pcm_format)
590 if (fp->channels < 1) {
591 dev_dbg(&subs->dev->dev,
592 "%s: (fmt @%p) no channels\n", __func__, fp);
596 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
597 dev_dbg(&subs->dev->dev,
598 "%s: (fmt @%p) no match for format %d\n", __func__,
603 for (i = 0; i < fp->nr_rates; i++) {
604 if (fp->rate_table[i] == rate) {
610 dev_dbg(&subs->dev->dev,
611 "%s: (fmt @%p) no match for rate %d\n", __func__,
616 if (fp->channels == match->channels)
619 dev_dbg(&subs->dev->dev,
620 "%s: (fmt @%p) score %d\n", __func__, fp, score);
626 * Configure the sync ep using the rate and pcm format of the data ep.
628 static int configure_sync_endpoint(struct snd_usb_substream *subs)
631 struct audioformat *fp;
632 struct audioformat *sync_fp = NULL;
634 int sync_period_bytes = subs->period_bytes;
635 struct snd_usb_substream *sync_subs =
636 &subs->stream->substream[subs->direction ^ 1];
638 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
640 return snd_usb_endpoint_set_params(subs->sync_endpoint,
649 /* Try to find the best matching audioformat. */
650 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
651 int score = match_endpoint_audioformats(subs,
652 fp, subs->cur_audiofmt,
653 subs->cur_rate, subs->pcm_format);
655 if (score > cur_score) {
661 if (unlikely(sync_fp == NULL)) {
662 dev_err(&subs->dev->dev,
663 "%s: no valid audioformat for sync ep %x found\n",
664 __func__, sync_subs->ep_num);
669 * Recalculate the period bytes if channel number differ between
670 * data and sync ep audioformat.
672 if (sync_fp->channels != subs->channels) {
673 sync_period_bytes = (subs->period_bytes / subs->channels) *
675 dev_dbg(&subs->dev->dev,
676 "%s: adjusted sync ep period bytes (%d -> %d)\n",
677 __func__, subs->period_bytes, sync_period_bytes);
680 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
693 * configure endpoint params
695 * called during initial setup and upon resume
697 static int configure_endpoint(struct snd_usb_substream *subs)
702 stop_endpoints(subs, true);
703 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
708 subs->buffer_periods,
711 subs->sync_endpoint);
715 if (subs->sync_endpoint)
716 ret = configure_sync_endpoint(subs);
721 static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
728 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
730 dev_err(&subs->dev->dev,
731 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
732 subs->str_pd->pd_id, state, ret);
739 int snd_usb_pcm_suspend(struct snd_usb_stream *as)
743 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
747 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
754 int snd_usb_pcm_resume(struct snd_usb_stream *as)
758 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
762 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
772 * allocate a buffer and set the given audio format.
774 * so far we use a physically linear buffer although packetize transfer
775 * doesn't need a continuous area.
776 * if sg buffer is supported on the later version of alsa, we'll follow
779 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
780 struct snd_pcm_hw_params *hw_params)
782 struct snd_usb_substream *subs = substream->runtime->private_data;
783 struct audioformat *fmt;
786 if (snd_usb_use_vmalloc)
787 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
788 params_buffer_bytes(hw_params));
790 ret = snd_pcm_lib_malloc_pages(substream,
791 params_buffer_bytes(hw_params));
795 subs->pcm_format = params_format(hw_params);
796 subs->period_bytes = params_period_bytes(hw_params);
797 subs->period_frames = params_period_size(hw_params);
798 subs->buffer_periods = params_periods(hw_params);
799 subs->channels = params_channels(hw_params);
800 subs->cur_rate = params_rate(hw_params);
802 fmt = find_format(subs);
804 dev_dbg(&subs->dev->dev,
805 "cannot set format: format = %#x, rate = %d, channels = %d\n",
806 subs->pcm_format, subs->cur_rate, subs->channels);
810 ret = snd_usb_lock_shutdown(subs->stream->chip);
814 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
818 ret = set_format(subs, fmt);
822 subs->interface = fmt->iface;
823 subs->altset_idx = fmt->altset_idx;
824 subs->need_setup_ep = true;
827 snd_usb_unlock_shutdown(subs->stream->chip);
834 * reset the audio format and release the buffer
836 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
838 struct snd_usb_substream *subs = substream->runtime->private_data;
840 subs->cur_audiofmt = NULL;
842 subs->period_bytes = 0;
843 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
844 stop_endpoints(subs, true);
845 snd_usb_endpoint_deactivate(subs->sync_endpoint);
846 snd_usb_endpoint_deactivate(subs->data_endpoint);
847 snd_usb_unlock_shutdown(subs->stream->chip);
850 if (snd_usb_use_vmalloc)
851 return snd_pcm_lib_free_vmalloc_buffer(substream);
853 return snd_pcm_lib_free_pages(substream);
859 * only a few subtle things...
861 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
863 struct snd_pcm_runtime *runtime = substream->runtime;
864 struct snd_usb_substream *subs = runtime->private_data;
865 struct usb_host_interface *alts;
866 struct usb_interface *iface;
869 if (! subs->cur_audiofmt) {
870 dev_err(&subs->dev->dev, "no format is specified!\n");
874 ret = snd_usb_lock_shutdown(subs->stream->chip);
877 if (snd_BUG_ON(!subs->data_endpoint)) {
882 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
883 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
885 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
889 ret = set_format(subs, subs->cur_audiofmt);
893 if (subs->need_setup_ep) {
895 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
896 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
897 ret = snd_usb_init_sample_rate(subs->stream->chip,
898 subs->cur_audiofmt->iface,
905 ret = configure_endpoint(subs);
908 subs->need_setup_ep = false;
911 /* some unit conversions in runtime */
912 subs->data_endpoint->maxframesize =
913 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
914 subs->data_endpoint->curframesize =
915 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
917 /* reset the pointer */
918 subs->hwptr_done = 0;
919 subs->transfer_done = 0;
920 subs->last_delay = 0;
921 subs->last_frame_number = 0;
924 /* for playback, submit the URBs now; otherwise, the first hwptr_done
925 * updates for all URBs would happen at the same time when starting */
926 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
927 ret = start_endpoints(subs);
930 snd_usb_unlock_shutdown(subs->stream->chip);
934 static const struct snd_pcm_hardware snd_usb_hardware =
936 .info = SNDRV_PCM_INFO_MMAP |
937 SNDRV_PCM_INFO_MMAP_VALID |
938 SNDRV_PCM_INFO_BATCH |
939 SNDRV_PCM_INFO_INTERLEAVED |
940 SNDRV_PCM_INFO_BLOCK_TRANSFER |
941 SNDRV_PCM_INFO_PAUSE,
942 .buffer_bytes_max = 1024 * 1024,
943 .period_bytes_min = 64,
944 .period_bytes_max = 512 * 1024,
949 static int hw_check_valid_format(struct snd_usb_substream *subs,
950 struct snd_pcm_hw_params *params,
951 struct audioformat *fp)
953 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
954 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
955 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
956 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
957 struct snd_mask check_fmts;
960 /* check the format */
961 snd_mask_none(&check_fmts);
962 check_fmts.bits[0] = (u32)fp->formats;
963 check_fmts.bits[1] = (u32)(fp->formats >> 32);
964 snd_mask_intersect(&check_fmts, fmts);
965 if (snd_mask_empty(&check_fmts)) {
966 hwc_debug(" > check: no supported format %d\n", fp->format);
969 /* check the channels */
970 if (fp->channels < ct->min || fp->channels > ct->max) {
971 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
974 /* check the rate is within the range */
975 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
976 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
979 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
980 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
983 /* check whether the period time is >= the data packet interval */
984 if (subs->speed != USB_SPEED_FULL) {
985 ptime = 125 * (1 << fp->datainterval);
986 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
987 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
994 static int hw_rule_rate(struct snd_pcm_hw_params *params,
995 struct snd_pcm_hw_rule *rule)
997 struct snd_usb_substream *subs = rule->private;
998 struct audioformat *fp;
999 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1000 unsigned int rmin, rmax;
1003 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1006 list_for_each_entry(fp, &subs->fmt_list, list) {
1007 if (!hw_check_valid_format(subs, params, fp))
1010 if (rmin > fp->rate_min)
1011 rmin = fp->rate_min;
1012 if (rmax < fp->rate_max)
1013 rmax = fp->rate_max;
1015 rmin = fp->rate_min;
1016 rmax = fp->rate_max;
1021 hwc_debug(" --> get empty\n");
1027 if (it->min < rmin) {
1032 if (it->max > rmax) {
1037 if (snd_interval_checkempty(it)) {
1041 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1046 static int hw_rule_channels(struct snd_pcm_hw_params *params,
1047 struct snd_pcm_hw_rule *rule)
1049 struct snd_usb_substream *subs = rule->private;
1050 struct audioformat *fp;
1051 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1052 unsigned int rmin, rmax;
1055 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1058 list_for_each_entry(fp, &subs->fmt_list, list) {
1059 if (!hw_check_valid_format(subs, params, fp))
1062 if (rmin > fp->channels)
1063 rmin = fp->channels;
1064 if (rmax < fp->channels)
1065 rmax = fp->channels;
1067 rmin = fp->channels;
1068 rmax = fp->channels;
1073 hwc_debug(" --> get empty\n");
1079 if (it->min < rmin) {
1084 if (it->max > rmax) {
1089 if (snd_interval_checkempty(it)) {
1093 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1097 static int hw_rule_format(struct snd_pcm_hw_params *params,
1098 struct snd_pcm_hw_rule *rule)
1100 struct snd_usb_substream *subs = rule->private;
1101 struct audioformat *fp;
1102 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1107 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1109 list_for_each_entry(fp, &subs->fmt_list, list) {
1110 if (!hw_check_valid_format(subs, params, fp))
1112 fbits |= fp->formats;
1115 oldbits[0] = fmt->bits[0];
1116 oldbits[1] = fmt->bits[1];
1117 fmt->bits[0] &= (u32)fbits;
1118 fmt->bits[1] &= (u32)(fbits >> 32);
1119 if (!fmt->bits[0] && !fmt->bits[1]) {
1120 hwc_debug(" --> get empty\n");
1123 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1124 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1128 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1129 struct snd_pcm_hw_rule *rule)
1131 struct snd_usb_substream *subs = rule->private;
1132 struct audioformat *fp;
1133 struct snd_interval *it;
1134 unsigned char min_datainterval;
1138 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1139 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1140 min_datainterval = 0xff;
1141 list_for_each_entry(fp, &subs->fmt_list, list) {
1142 if (!hw_check_valid_format(subs, params, fp))
1144 min_datainterval = min(min_datainterval, fp->datainterval);
1146 if (min_datainterval == 0xff) {
1147 hwc_debug(" --> get empty\n");
1151 pmin = 125 * (1 << min_datainterval);
1153 if (it->min < pmin) {
1158 if (snd_interval_checkempty(it)) {
1162 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1167 * If the device supports unusual bit rates, does the request meet these?
1169 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1170 struct snd_usb_substream *subs)
1172 struct audioformat *fp;
1174 int count = 0, needs_knot = 0;
1177 kfree(subs->rate_list.list);
1178 subs->rate_list.list = NULL;
1180 list_for_each_entry(fp, &subs->fmt_list, list) {
1181 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1183 count += fp->nr_rates;
1184 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1190 subs->rate_list.list = rate_list =
1191 kmalloc_array(count, sizeof(int), GFP_KERNEL);
1192 if (!subs->rate_list.list)
1194 subs->rate_list.count = count;
1195 subs->rate_list.mask = 0;
1197 list_for_each_entry(fp, &subs->fmt_list, list) {
1199 for (i = 0; i < fp->nr_rates; i++)
1200 rate_list[count++] = fp->rate_table[i];
1202 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1212 * set up the runtime hardware information.
1215 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1217 struct audioformat *fp;
1218 unsigned int pt, ptmin;
1219 int param_period_time_if_needed;
1222 runtime->hw.formats = subs->formats;
1224 runtime->hw.rate_min = 0x7fffffff;
1225 runtime->hw.rate_max = 0;
1226 runtime->hw.channels_min = 256;
1227 runtime->hw.channels_max = 0;
1228 runtime->hw.rates = 0;
1230 /* check min/max rates and channels */
1231 list_for_each_entry(fp, &subs->fmt_list, list) {
1232 runtime->hw.rates |= fp->rates;
1233 if (runtime->hw.rate_min > fp->rate_min)
1234 runtime->hw.rate_min = fp->rate_min;
1235 if (runtime->hw.rate_max < fp->rate_max)
1236 runtime->hw.rate_max = fp->rate_max;
1237 if (runtime->hw.channels_min > fp->channels)
1238 runtime->hw.channels_min = fp->channels;
1239 if (runtime->hw.channels_max < fp->channels)
1240 runtime->hw.channels_max = fp->channels;
1241 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1242 /* FIXME: there might be more than one audio formats... */
1243 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1246 pt = 125 * (1 << fp->datainterval);
1247 ptmin = min(ptmin, pt);
1250 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1251 if (subs->speed == USB_SPEED_FULL)
1252 /* full speed devices have fixed data packet interval */
1255 /* if period time doesn't go below 1 ms, no rules needed */
1256 param_period_time_if_needed = -1;
1258 err = snd_pcm_hw_constraint_minmax(runtime,
1259 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1264 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1266 SNDRV_PCM_HW_PARAM_FORMAT,
1267 SNDRV_PCM_HW_PARAM_CHANNELS,
1268 param_period_time_if_needed,
1272 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1273 hw_rule_channels, subs,
1274 SNDRV_PCM_HW_PARAM_FORMAT,
1275 SNDRV_PCM_HW_PARAM_RATE,
1276 param_period_time_if_needed,
1280 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1281 hw_rule_format, subs,
1282 SNDRV_PCM_HW_PARAM_RATE,
1283 SNDRV_PCM_HW_PARAM_CHANNELS,
1284 param_period_time_if_needed,
1288 if (param_period_time_if_needed >= 0) {
1289 err = snd_pcm_hw_rule_add(runtime, 0,
1290 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1291 hw_rule_period_time, subs,
1292 SNDRV_PCM_HW_PARAM_FORMAT,
1293 SNDRV_PCM_HW_PARAM_CHANNELS,
1294 SNDRV_PCM_HW_PARAM_RATE,
1299 err = snd_usb_pcm_check_knot(runtime, subs);
1303 return snd_usb_autoresume(subs->stream->chip);
1306 static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
1308 int direction = substream->stream;
1309 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1310 struct snd_pcm_runtime *runtime = substream->runtime;
1311 struct snd_usb_substream *subs = &as->substream[direction];
1313 subs->interface = -1;
1314 subs->altset_idx = 0;
1315 runtime->hw = snd_usb_hardware;
1316 runtime->private_data = subs;
1317 subs->pcm_substream = substream;
1318 /* runtime PM is also done there */
1320 /* initialize DSD/DOP context */
1321 subs->dsd_dop.byte_idx = 0;
1322 subs->dsd_dop.channel = 0;
1323 subs->dsd_dop.marker = 1;
1325 return setup_hw_info(runtime, subs);
1328 static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
1330 int direction = substream->stream;
1331 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1332 struct snd_usb_substream *subs = &as->substream[direction];
1335 stop_endpoints(subs, true);
1337 if (!as->chip->keep_iface &&
1338 subs->interface >= 0 &&
1339 !snd_usb_lock_shutdown(subs->stream->chip)) {
1340 usb_set_interface(subs->dev, subs->interface, 0);
1341 subs->interface = -1;
1342 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
1343 snd_usb_unlock_shutdown(subs->stream->chip);
1348 subs->pcm_substream = NULL;
1349 snd_usb_autosuspend(subs->stream->chip);
1354 /* Since a URB can handle only a single linear buffer, we must use double
1355 * buffering when the data to be transferred overflows the buffer boundary.
1356 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1359 static void retire_capture_urb(struct snd_usb_substream *subs,
1362 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1363 unsigned int stride, frames, bytes, oldptr;
1364 int i, period_elapsed = 0;
1365 unsigned long flags;
1367 int current_frame_number;
1369 /* read frame number here, update pointer in critical section */
1370 current_frame_number = usb_get_current_frame_number(subs->dev);
1372 stride = runtime->frame_bits >> 3;
1374 for (i = 0; i < urb->number_of_packets; i++) {
1375 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1376 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1377 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1378 i, urb->iso_frame_desc[i].status);
1381 bytes = urb->iso_frame_desc[i].actual_length;
1382 frames = bytes / stride;
1383 if (!subs->txfr_quirk)
1384 bytes = frames * stride;
1385 if (bytes % (runtime->sample_bits >> 3) != 0) {
1386 int oldbytes = bytes;
1387 bytes = frames * stride;
1388 dev_warn_ratelimited(&subs->dev->dev,
1389 "Corrected urb data len. %d->%d\n",
1392 /* update the current pointer */
1393 spin_lock_irqsave(&subs->lock, flags);
1394 oldptr = subs->hwptr_done;
1395 subs->hwptr_done += bytes;
1396 if (subs->hwptr_done >= runtime->buffer_size * stride)
1397 subs->hwptr_done -= runtime->buffer_size * stride;
1398 frames = (bytes + (oldptr % stride)) / stride;
1399 subs->transfer_done += frames;
1400 if (subs->transfer_done >= runtime->period_size) {
1401 subs->transfer_done -= runtime->period_size;
1404 /* capture delay is by construction limited to one URB,
1407 runtime->delay = subs->last_delay = 0;
1409 /* realign last_frame_number */
1410 subs->last_frame_number = current_frame_number;
1411 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1413 spin_unlock_irqrestore(&subs->lock, flags);
1414 /* copy a data chunk */
1415 if (oldptr + bytes > runtime->buffer_size * stride) {
1416 unsigned int bytes1 =
1417 runtime->buffer_size * stride - oldptr;
1418 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1419 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1421 memcpy(runtime->dma_area + oldptr, cp, bytes);
1426 snd_pcm_period_elapsed(subs->pcm_substream);
1429 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1430 struct urb *urb, unsigned int bytes)
1432 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1433 unsigned int stride = runtime->frame_bits >> 3;
1434 unsigned int dst_idx = 0;
1435 unsigned int src_idx = subs->hwptr_done;
1436 unsigned int wrap = runtime->buffer_size * stride;
1437 u8 *dst = urb->transfer_buffer;
1438 u8 *src = runtime->dma_area;
1439 u8 marker[] = { 0x05, 0xfa };
1442 * The DSP DOP format defines a way to transport DSD samples over
1443 * normal PCM data endpoints. It requires stuffing of marker bytes
1444 * (0x05 and 0xfa, alternating per sample frame), and then expects
1445 * 2 additional bytes of actual payload. The whole frame is stored
1448 * Hence, for a stereo transport, the buffer layout looks like this,
1449 * where L refers to left channel samples and R to right.
1451 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1452 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1458 if (++subs->dsd_dop.byte_idx == 3) {
1459 /* frame boundary? */
1460 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1462 subs->dsd_dop.byte_idx = 0;
1464 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1465 /* alternate the marker */
1466 subs->dsd_dop.marker++;
1467 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1468 subs->dsd_dop.channel = 0;
1471 /* stuff the DSD payload */
1472 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1474 if (subs->cur_audiofmt->dsd_bitrev)
1475 dst[dst_idx++] = bitrev8(src[idx]);
1477 dst[dst_idx++] = src[idx];
1482 if (subs->hwptr_done >= runtime->buffer_size * stride)
1483 subs->hwptr_done -= runtime->buffer_size * stride;
1486 static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1487 int offset, int stride, unsigned int bytes)
1489 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1491 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1492 /* err, the transferred area goes over buffer boundary. */
1493 unsigned int bytes1 =
1494 runtime->buffer_size * stride - subs->hwptr_done;
1495 memcpy(urb->transfer_buffer + offset,
1496 runtime->dma_area + subs->hwptr_done, bytes1);
1497 memcpy(urb->transfer_buffer + offset + bytes1,
1498 runtime->dma_area, bytes - bytes1);
1500 memcpy(urb->transfer_buffer + offset,
1501 runtime->dma_area + subs->hwptr_done, bytes);
1503 subs->hwptr_done += bytes;
1504 if (subs->hwptr_done >= runtime->buffer_size * stride)
1505 subs->hwptr_done -= runtime->buffer_size * stride;
1508 static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1509 struct urb *urb, int stride,
1512 __le32 packet_length;
1515 /* Put __le32 length descriptor at start of each packet. */
1516 for (i = 0; i < urb->number_of_packets; i++) {
1517 unsigned int length = urb->iso_frame_desc[i].length;
1518 unsigned int offset = urb->iso_frame_desc[i].offset;
1520 packet_length = cpu_to_le32(length);
1521 offset += i * sizeof(packet_length);
1522 urb->iso_frame_desc[i].offset = offset;
1523 urb->iso_frame_desc[i].length += sizeof(packet_length);
1524 memcpy(urb->transfer_buffer + offset,
1525 &packet_length, sizeof(packet_length));
1526 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1529 /* Adjust transfer size accordingly. */
1530 bytes += urb->number_of_packets * sizeof(packet_length);
1534 static void prepare_playback_urb(struct snd_usb_substream *subs,
1537 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1538 struct snd_usb_endpoint *ep = subs->data_endpoint;
1539 struct snd_urb_ctx *ctx = urb->context;
1540 unsigned int counts, frames, bytes;
1541 int i, stride, period_elapsed = 0;
1542 unsigned long flags;
1544 stride = runtime->frame_bits >> 3;
1547 urb->number_of_packets = 0;
1548 spin_lock_irqsave(&subs->lock, flags);
1549 subs->frame_limit += ep->max_urb_frames;
1550 for (i = 0; i < ctx->packets; i++) {
1551 if (ctx->packet_size[i])
1552 counts = ctx->packet_size[i];
1554 counts = snd_usb_endpoint_next_packet_size(ep);
1556 /* set up descriptor */
1557 urb->iso_frame_desc[i].offset = frames * ep->stride;
1558 urb->iso_frame_desc[i].length = counts * ep->stride;
1560 urb->number_of_packets++;
1561 subs->transfer_done += counts;
1562 if (subs->transfer_done >= runtime->period_size) {
1563 subs->transfer_done -= runtime->period_size;
1564 subs->frame_limit = 0;
1566 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1567 if (subs->transfer_done > 0) {
1568 /* FIXME: fill-max mode is not
1570 frames -= subs->transfer_done;
1571 counts -= subs->transfer_done;
1572 urb->iso_frame_desc[i].length =
1573 counts * ep->stride;
1574 subs->transfer_done = 0;
1577 if (i < ctx->packets) {
1578 /* add a transfer delimiter */
1579 urb->iso_frame_desc[i].offset =
1580 frames * ep->stride;
1581 urb->iso_frame_desc[i].length = 0;
1582 urb->number_of_packets++;
1587 /* finish at the period boundary or after enough frames */
1588 if ((period_elapsed ||
1589 subs->transfer_done >= subs->frame_limit) &&
1590 !snd_usb_endpoint_implicit_feedback_sink(ep))
1593 bytes = frames * ep->stride;
1595 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1596 subs->cur_audiofmt->dsd_dop)) {
1597 fill_playback_urb_dsd_dop(subs, urb, bytes);
1598 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1599 subs->cur_audiofmt->dsd_bitrev)) {
1600 /* bit-reverse the bytes */
1601 u8 *buf = urb->transfer_buffer;
1602 for (i = 0; i < bytes; i++) {
1603 int idx = (subs->hwptr_done + i)
1604 % (runtime->buffer_size * stride);
1605 buf[i] = bitrev8(runtime->dma_area[idx]);
1608 subs->hwptr_done += bytes;
1609 if (subs->hwptr_done >= runtime->buffer_size * stride)
1610 subs->hwptr_done -= runtime->buffer_size * stride;
1613 if (!subs->tx_length_quirk)
1614 copy_to_urb(subs, urb, 0, stride, bytes);
1616 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1617 /* bytes is now amount of outgoing data */
1620 /* update delay with exact number of samples queued */
1621 runtime->delay = subs->last_delay;
1622 runtime->delay += frames;
1623 subs->last_delay = runtime->delay;
1625 /* realign last_frame_number */
1626 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1627 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1629 if (subs->trigger_tstamp_pending_update) {
1630 /* this is the first actual URB submitted,
1631 * update trigger timestamp to reflect actual start time
1633 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1634 subs->trigger_tstamp_pending_update = false;
1637 spin_unlock_irqrestore(&subs->lock, flags);
1638 urb->transfer_buffer_length = bytes;
1640 snd_pcm_period_elapsed(subs->pcm_substream);
1644 * process after playback data complete
1645 * - decrease the delay count again
1647 static void retire_playback_urb(struct snd_usb_substream *subs,
1650 unsigned long flags;
1651 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1652 struct snd_usb_endpoint *ep = subs->data_endpoint;
1653 int processed = urb->transfer_buffer_length / ep->stride;
1656 /* ignore the delay accounting when procssed=0 is given, i.e.
1657 * silent payloads are procssed before handling the actual data
1662 spin_lock_irqsave(&subs->lock, flags);
1663 if (!subs->last_delay)
1664 goto out; /* short path */
1666 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1667 /* update delay with exact number of samples played */
1668 if (processed > subs->last_delay)
1669 subs->last_delay = 0;
1671 subs->last_delay -= processed;
1672 runtime->delay = subs->last_delay;
1675 * Report when delay estimate is off by more than 2ms.
1676 * The error should be lower than 2ms since the estimate relies
1677 * on two reads of a counter updated every ms.
1679 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1680 dev_dbg_ratelimited(&subs->dev->dev,
1681 "delay: estimated %d, actual %d\n",
1682 est_delay, subs->last_delay);
1684 if (!subs->running) {
1685 /* update last_frame_number for delay counting here since
1686 * prepare_playback_urb won't be called during pause
1688 subs->last_frame_number =
1689 usb_get_current_frame_number(subs->dev) & 0xff;
1693 spin_unlock_irqrestore(&subs->lock, flags);
1696 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1699 struct snd_usb_substream *subs = substream->runtime->private_data;
1702 case SNDRV_PCM_TRIGGER_START:
1703 subs->trigger_tstamp_pending_update = true;
1705 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1706 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1707 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1710 case SNDRV_PCM_TRIGGER_STOP:
1711 stop_endpoints(subs, false);
1714 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1715 subs->data_endpoint->prepare_data_urb = NULL;
1716 /* keep retire_data_urb for delay calculation */
1717 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1725 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1729 struct snd_usb_substream *subs = substream->runtime->private_data;
1732 case SNDRV_PCM_TRIGGER_START:
1733 err = start_endpoints(subs);
1737 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1740 case SNDRV_PCM_TRIGGER_STOP:
1741 stop_endpoints(subs, false);
1744 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1745 subs->data_endpoint->retire_data_urb = NULL;
1748 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1749 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1757 static const struct snd_pcm_ops snd_usb_playback_ops = {
1758 .open = snd_usb_pcm_open,
1759 .close = snd_usb_pcm_close,
1760 .ioctl = snd_pcm_lib_ioctl,
1761 .hw_params = snd_usb_hw_params,
1762 .hw_free = snd_usb_hw_free,
1763 .prepare = snd_usb_pcm_prepare,
1764 .trigger = snd_usb_substream_playback_trigger,
1765 .pointer = snd_usb_pcm_pointer,
1766 .page = snd_pcm_lib_get_vmalloc_page,
1769 static const struct snd_pcm_ops snd_usb_capture_ops = {
1770 .open = snd_usb_pcm_open,
1771 .close = snd_usb_pcm_close,
1772 .ioctl = snd_pcm_lib_ioctl,
1773 .hw_params = snd_usb_hw_params,
1774 .hw_free = snd_usb_hw_free,
1775 .prepare = snd_usb_pcm_prepare,
1776 .trigger = snd_usb_substream_capture_trigger,
1777 .pointer = snd_usb_pcm_pointer,
1778 .page = snd_pcm_lib_get_vmalloc_page,
1781 static const struct snd_pcm_ops snd_usb_playback_dev_ops = {
1782 .open = snd_usb_pcm_open,
1783 .close = snd_usb_pcm_close,
1784 .ioctl = snd_pcm_lib_ioctl,
1785 .hw_params = snd_usb_hw_params,
1786 .hw_free = snd_usb_hw_free,
1787 .prepare = snd_usb_pcm_prepare,
1788 .trigger = snd_usb_substream_playback_trigger,
1789 .pointer = snd_usb_pcm_pointer,
1790 .page = snd_pcm_sgbuf_ops_page,
1793 static const struct snd_pcm_ops snd_usb_capture_dev_ops = {
1794 .open = snd_usb_pcm_open,
1795 .close = snd_usb_pcm_close,
1796 .ioctl = snd_pcm_lib_ioctl,
1797 .hw_params = snd_usb_hw_params,
1798 .hw_free = snd_usb_hw_free,
1799 .prepare = snd_usb_pcm_prepare,
1800 .trigger = snd_usb_substream_capture_trigger,
1801 .pointer = snd_usb_pcm_pointer,
1802 .page = snd_pcm_sgbuf_ops_page,
1805 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1807 const struct snd_pcm_ops *ops;
1809 if (snd_usb_use_vmalloc)
1810 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
1811 &snd_usb_playback_ops : &snd_usb_capture_ops;
1813 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
1814 &snd_usb_playback_dev_ops : &snd_usb_capture_dev_ops;
1815 snd_pcm_set_ops(pcm, stream, ops);
1818 void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1820 struct snd_pcm *pcm = subs->stream->pcm;
1821 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1822 struct device *dev = subs->dev->bus->controller;
1824 if (!snd_usb_use_vmalloc)
1825 snd_pcm_lib_preallocate_pages(s, SNDRV_DMA_TYPE_DEV_SG,
1826 dev, 64*1024, 512*1024);