1 // SPDX-License-Identifier: GPL-2.0-or-later
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/bitrev.h>
8 #include <linux/ratelimit.h>
10 #include <linux/usb/audio.h>
11 #include <linux/usb/audio-v2.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
28 #define SUBSTREAM_FLAG_DATA_EP_STARTED 0
29 #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
31 /* return the estimated delay based on USB frame counters */
32 static snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
33 struct snd_pcm_runtime *runtime)
35 unsigned int current_frame_number;
36 unsigned int frame_diff;
40 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
41 queued = bytes_to_frames(runtime, subs->inflight_bytes);
44 } else if (!subs->running) {
48 current_frame_number = usb_get_current_frame_number(subs->dev);
50 * HCD implementations use different widths, use lower 8 bits.
51 * The delay will be managed up to 256ms, which is more than
54 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
56 /* Approximation based on number of samples per USB frame (ms),
57 some truncation for 44.1 but the estimate is good enough */
58 est_delay = frame_diff * runtime->rate / 1000;
60 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
61 est_delay = queued - est_delay;
70 * return the current pcm pointer. just based on the hwptr_done value.
72 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
74 struct snd_pcm_runtime *runtime = substream->runtime;
75 struct snd_usb_substream *subs = runtime->private_data;
76 unsigned int hwptr_done;
78 if (atomic_read(&subs->stream->chip->shutdown))
79 return SNDRV_PCM_POS_XRUN;
80 spin_lock(&subs->lock);
81 hwptr_done = subs->hwptr_done;
82 runtime->delay = snd_usb_pcm_delay(subs, runtime);
83 spin_unlock(&subs->lock);
84 return bytes_to_frames(runtime, hwptr_done);
88 * find a matching audio format
90 static const struct audioformat *
91 find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
92 unsigned int rate, unsigned int channels, bool strict_match,
93 struct snd_usb_substream *subs)
95 const struct audioformat *fp;
96 const struct audioformat *found = NULL;
97 int cur_attr = 0, attr;
99 list_for_each_entry(fp, fmt_list_head, list) {
101 if (!(fp->formats & pcm_format_to_bits(format)))
103 if (fp->channels != channels)
106 if (rate < fp->rate_min || rate > fp->rate_max)
108 if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
110 for (i = 0; i < fp->nr_rates; i++)
111 if (fp->rate_table[i] == rate)
113 if (i >= fp->nr_rates)
116 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
122 /* avoid async out and adaptive in if the other method
123 * supports the same format.
124 * this is a workaround for the case like
125 * M-audio audiophile USB.
127 if (subs && attr != cur_attr) {
128 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
129 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
130 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
131 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
133 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
134 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
135 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
136 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
142 /* find the format with the largest max. packet size */
143 if (fp->maxpacksize > found->maxpacksize) {
151 static const struct audioformat *
152 find_substream_format(struct snd_usb_substream *subs,
153 const struct snd_pcm_hw_params *params)
155 return find_format(&subs->fmt_list, params_format(params),
156 params_rate(params), params_channels(params),
160 bool snd_usb_pcm_has_fixed_rate(struct snd_usb_substream *subs)
162 const struct audioformat *fp;
163 struct snd_usb_audio *chip = subs->stream->chip;
166 if (!(chip->quirk_flags & QUIRK_FLAG_FIXED_RATE))
168 list_for_each_entry(fp, &subs->fmt_list, list) {
169 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
171 if (fp->nr_rates < 1)
173 if (fp->nr_rates > 1)
176 rate = fp->rate_table[0];
179 if (rate != fp->rate_table[0])
185 static int init_pitch_v1(struct snd_usb_audio *chip, int ep)
187 struct usb_device *dev = chip->dev;
188 unsigned char data[1];
192 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
193 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
194 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
199 static int init_pitch_v2(struct snd_usb_audio *chip, int ep)
201 struct usb_device *dev = chip->dev;
202 unsigned char data[1];
206 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
207 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
208 UAC2_EP_CS_PITCH << 8, 0,
214 * initialize the pitch control and sample rate
216 int snd_usb_init_pitch(struct snd_usb_audio *chip,
217 const struct audioformat *fmt)
221 /* if endpoint doesn't have pitch control, bail out */
222 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
225 usb_audio_dbg(chip, "enable PITCH for EP 0x%x\n", fmt->endpoint);
227 switch (fmt->protocol) {
229 err = init_pitch_v1(chip, fmt->endpoint);
232 err = init_pitch_v2(chip, fmt->endpoint);
239 usb_audio_err(chip, "failed to enable PITCH for EP 0x%x\n",
247 static bool stop_endpoints(struct snd_usb_substream *subs, bool keep_pending)
251 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
252 snd_usb_endpoint_stop(subs->sync_endpoint, keep_pending);
255 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
256 snd_usb_endpoint_stop(subs->data_endpoint, keep_pending);
262 static int start_endpoints(struct snd_usb_substream *subs)
266 if (!subs->data_endpoint)
269 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
270 err = snd_usb_endpoint_start(subs->data_endpoint);
272 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
277 if (subs->sync_endpoint &&
278 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
279 err = snd_usb_endpoint_start(subs->sync_endpoint);
281 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
289 stop_endpoints(subs, false);
293 static void sync_pending_stops(struct snd_usb_substream *subs)
295 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
296 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
299 /* PCM sync_stop callback */
300 static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
302 struct snd_usb_substream *subs = substream->runtime->private_data;
304 sync_pending_stops(subs);
308 /* Set up sync endpoint */
309 int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
310 struct audioformat *fmt)
312 struct usb_device *dev = chip->dev;
313 struct usb_host_interface *alts;
314 struct usb_interface_descriptor *altsd;
315 unsigned int ep, attr, sync_attr;
320 return 0; /* already set up */
322 alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting);
325 altsd = get_iface_desc(alts);
327 err = snd_usb_parse_implicit_fb_quirk(chip, fmt, alts);
329 return 0; /* matched */
332 * Generic sync EP handling
335 if (fmt->ep_idx > 0 || altsd->bNumEndpoints < 2)
338 is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
339 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
340 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
341 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
342 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
345 sync_attr = get_endpoint(alts, 1)->bmAttributes;
348 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
349 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
350 * error fall back to SYNC mode and don't create sync endpoint
353 /* check sync-pipe endpoint */
354 /* ... and check descriptor size before accessing bSynchAddress
355 because there is a version of the SB Audigy 2 NX firmware lacking
356 the audio fields in the endpoint descriptors */
357 if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
358 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
359 get_endpoint(alts, 1)->bSynchAddress != 0)) {
361 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
362 fmt->iface, fmt->altsetting,
363 get_endpoint(alts, 1)->bmAttributes,
364 get_endpoint(alts, 1)->bLength,
365 get_endpoint(alts, 1)->bSynchAddress);
366 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
370 ep = get_endpoint(alts, 1)->bEndpointAddress;
371 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
372 get_endpoint(alts, 0)->bSynchAddress != 0 &&
373 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
374 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
376 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
377 fmt->iface, fmt->altsetting,
378 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
379 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
385 fmt->sync_iface = altsd->bInterfaceNumber;
386 fmt->sync_altsetting = altsd->bAlternateSetting;
387 fmt->sync_ep_idx = 1;
388 if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB)
389 fmt->implicit_fb = 1;
391 dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
392 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
393 fmt->sync_altsetting, fmt->implicit_fb);
398 static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
405 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
407 dev_err(&subs->dev->dev,
408 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
409 subs->str_pd->pd_id, state, ret);
416 int snd_usb_pcm_suspend(struct snd_usb_stream *as)
420 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
424 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
431 int snd_usb_pcm_resume(struct snd_usb_stream *as)
435 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
439 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
446 static void close_endpoints(struct snd_usb_audio *chip,
447 struct snd_usb_substream *subs)
449 if (subs->data_endpoint) {
450 snd_usb_endpoint_set_sync(chip, subs->data_endpoint, NULL);
451 snd_usb_endpoint_close(chip, subs->data_endpoint);
452 subs->data_endpoint = NULL;
455 if (subs->sync_endpoint) {
456 snd_usb_endpoint_close(chip, subs->sync_endpoint);
457 subs->sync_endpoint = NULL;
464 * allocate a buffer and set the given audio format.
466 * so far we use a physically linear buffer although packetize transfer
467 * doesn't need a continuous area.
468 * if sg buffer is supported on the later version of alsa, we'll follow
471 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
472 struct snd_pcm_hw_params *hw_params)
474 struct snd_usb_substream *subs = substream->runtime->private_data;
475 struct snd_usb_audio *chip = subs->stream->chip;
476 const struct audioformat *fmt;
477 const struct audioformat *sync_fmt;
478 bool fixed_rate, sync_fixed_rate;
481 ret = snd_media_start_pipeline(subs);
485 fixed_rate = snd_usb_pcm_has_fixed_rate(subs);
486 fmt = find_substream_format(subs, hw_params);
489 "cannot find format: format=%s, rate=%d, channels=%d\n",
490 snd_pcm_format_name(params_format(hw_params)),
491 params_rate(hw_params), params_channels(hw_params));
496 if (fmt->implicit_fb) {
497 sync_fmt = snd_usb_find_implicit_fb_sync_format(chip, fmt,
503 "cannot find sync format: ep=0x%x, iface=%d:%d, format=%s, rate=%d, channels=%d\n",
504 fmt->sync_ep, fmt->sync_iface,
505 fmt->sync_altsetting,
506 snd_pcm_format_name(params_format(hw_params)),
507 params_rate(hw_params), params_channels(hw_params));
513 sync_fixed_rate = fixed_rate;
516 ret = snd_usb_lock_shutdown(chip);
520 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
524 if (subs->data_endpoint) {
525 if (snd_usb_endpoint_compatible(chip, subs->data_endpoint,
528 close_endpoints(chip, subs);
531 subs->data_endpoint = snd_usb_endpoint_open(chip, fmt, hw_params, false, fixed_rate);
532 if (!subs->data_endpoint) {
538 subs->sync_endpoint = snd_usb_endpoint_open(chip, sync_fmt,
542 if (!subs->sync_endpoint) {
547 snd_usb_endpoint_set_sync(chip, subs->data_endpoint,
548 subs->sync_endpoint);
551 mutex_lock(&chip->mutex);
552 subs->cur_audiofmt = fmt;
553 mutex_unlock(&chip->mutex);
555 if (!subs->data_endpoint->need_setup)
558 if (subs->sync_endpoint) {
559 ret = snd_usb_endpoint_set_params(chip, subs->sync_endpoint);
564 ret = snd_usb_endpoint_set_params(chip, subs->data_endpoint);
568 close_endpoints(chip, subs);
570 snd_usb_unlock_shutdown(chip);
573 snd_media_stop_pipeline(subs);
581 * reset the audio format and release the buffer
583 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
585 struct snd_usb_substream *subs = substream->runtime->private_data;
586 struct snd_usb_audio *chip = subs->stream->chip;
588 snd_media_stop_pipeline(subs);
589 mutex_lock(&chip->mutex);
590 subs->cur_audiofmt = NULL;
591 mutex_unlock(&chip->mutex);
592 if (!snd_usb_lock_shutdown(chip)) {
593 if (stop_endpoints(subs, false))
594 sync_pending_stops(subs);
595 close_endpoints(chip, subs);
596 snd_usb_unlock_shutdown(chip);
602 /* free-wheeling mode? (e.g. dmix) */
603 static int in_free_wheeling_mode(struct snd_pcm_runtime *runtime)
605 return runtime->stop_threshold > runtime->buffer_size;
608 /* check whether early start is needed for playback stream */
609 static int lowlatency_playback_available(struct snd_pcm_runtime *runtime,
610 struct snd_usb_substream *subs)
612 struct snd_usb_audio *chip = subs->stream->chip;
614 if (subs->direction == SNDRV_PCM_STREAM_CAPTURE)
616 /* disabled via module option? */
617 if (!chip->lowlatency)
619 if (in_free_wheeling_mode(runtime))
621 /* implicit feedback mode has own operation mode */
622 if (snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint))
630 * only a few subtle things...
632 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
634 struct snd_pcm_runtime *runtime = substream->runtime;
635 struct snd_usb_substream *subs = runtime->private_data;
636 struct snd_usb_audio *chip = subs->stream->chip;
640 ret = snd_usb_lock_shutdown(chip);
643 if (snd_BUG_ON(!subs->data_endpoint)) {
649 if (subs->sync_endpoint) {
650 ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint);
655 ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint);
659 snd_usb_set_format_quirk(subs, subs->cur_audiofmt);
662 /* reset the pointer */
663 subs->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
664 subs->inflight_bytes = 0;
665 subs->hwptr_done = 0;
666 subs->transfer_done = 0;
667 subs->last_frame_number = 0;
668 subs->period_elapsed_pending = 0;
671 subs->lowlatency_playback = lowlatency_playback_available(runtime, subs);
672 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
673 !subs->lowlatency_playback) {
674 ret = start_endpoints(subs);
675 /* if XRUN happens at starting streams (possibly with implicit
676 * fb case), restart again, but only try once.
678 if (ret == -EPIPE && !retry++) {
679 sync_pending_stops(subs);
684 snd_usb_unlock_shutdown(chip);
692 #ifdef HW_CONST_DEBUG
693 #define hwc_debug(fmt, args...) pr_debug(fmt, ##args)
695 #define hwc_debug(fmt, args...) do { } while(0)
698 static const struct snd_pcm_hardware snd_usb_hardware =
700 .info = SNDRV_PCM_INFO_MMAP |
701 SNDRV_PCM_INFO_MMAP_VALID |
702 SNDRV_PCM_INFO_BATCH |
703 SNDRV_PCM_INFO_INTERLEAVED |
704 SNDRV_PCM_INFO_BLOCK_TRANSFER |
705 SNDRV_PCM_INFO_PAUSE,
708 .buffer_bytes_max = INT_MAX, /* limited by BUFFER_TIME later */
709 .period_bytes_min = 64,
710 .period_bytes_max = INT_MAX, /* limited by PERIOD_TIME later */
715 static int hw_check_valid_format(struct snd_usb_substream *subs,
716 struct snd_pcm_hw_params *params,
717 const struct audioformat *fp)
719 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
720 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
721 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
722 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
723 struct snd_mask check_fmts;
726 /* check the format */
727 snd_mask_none(&check_fmts);
728 check_fmts.bits[0] = (u32)fp->formats;
729 check_fmts.bits[1] = (u32)(fp->formats >> 32);
730 snd_mask_intersect(&check_fmts, fmts);
731 if (snd_mask_empty(&check_fmts)) {
732 hwc_debug(" > check: no supported format 0x%llx\n", fp->formats);
735 /* check the channels */
736 if (fp->channels < ct->min || fp->channels > ct->max) {
737 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
740 /* check the rate is within the range */
741 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
742 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
745 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
746 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
749 /* check whether the period time is >= the data packet interval */
750 if (subs->speed != USB_SPEED_FULL) {
751 ptime = 125 * (1 << fp->datainterval);
752 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
753 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
760 static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin,
766 hwc_debug(" --> get empty\n");
772 if (it->min < rmin) {
777 if (it->max > rmax) {
782 if (snd_interval_checkempty(it)) {
786 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
790 static int hw_rule_rate(struct snd_pcm_hw_params *params,
791 struct snd_pcm_hw_rule *rule)
793 struct snd_usb_substream *subs = rule->private;
794 struct snd_usb_audio *chip = subs->stream->chip;
795 const struct audioformat *fp;
796 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
797 unsigned int rmin, rmax, r;
800 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
803 list_for_each_entry(fp, &subs->fmt_list, list) {
804 if (!hw_check_valid_format(subs, params, fp))
806 r = snd_usb_endpoint_get_clock_rate(chip, fp->clock);
808 if (!snd_interval_test(it, r))
814 if (fp->rate_table && fp->nr_rates) {
815 for (i = 0; i < fp->nr_rates; i++) {
816 r = fp->rate_table[i];
817 if (!snd_interval_test(it, r))
823 rmin = min(rmin, fp->rate_min);
824 rmax = max(rmax, fp->rate_max);
828 return apply_hw_params_minmax(it, rmin, rmax);
832 static int hw_rule_channels(struct snd_pcm_hw_params *params,
833 struct snd_pcm_hw_rule *rule)
835 struct snd_usb_substream *subs = rule->private;
836 const struct audioformat *fp;
837 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
838 unsigned int rmin, rmax;
840 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
843 list_for_each_entry(fp, &subs->fmt_list, list) {
844 if (!hw_check_valid_format(subs, params, fp))
846 rmin = min(rmin, fp->channels);
847 rmax = max(rmax, fp->channels);
850 return apply_hw_params_minmax(it, rmin, rmax);
853 static int apply_hw_params_format_bits(struct snd_mask *fmt, u64 fbits)
858 oldbits[0] = fmt->bits[0];
859 oldbits[1] = fmt->bits[1];
860 fmt->bits[0] &= (u32)fbits;
861 fmt->bits[1] &= (u32)(fbits >> 32);
862 if (!fmt->bits[0] && !fmt->bits[1]) {
863 hwc_debug(" --> get empty\n");
866 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
867 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
871 static int hw_rule_format(struct snd_pcm_hw_params *params,
872 struct snd_pcm_hw_rule *rule)
874 struct snd_usb_substream *subs = rule->private;
875 const struct audioformat *fp;
876 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
879 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
881 list_for_each_entry(fp, &subs->fmt_list, list) {
882 if (!hw_check_valid_format(subs, params, fp))
884 fbits |= fp->formats;
886 return apply_hw_params_format_bits(fmt, fbits);
889 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
890 struct snd_pcm_hw_rule *rule)
892 struct snd_usb_substream *subs = rule->private;
893 const struct audioformat *fp;
894 struct snd_interval *it;
895 unsigned char min_datainterval;
898 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
899 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
900 min_datainterval = 0xff;
901 list_for_each_entry(fp, &subs->fmt_list, list) {
902 if (!hw_check_valid_format(subs, params, fp))
904 min_datainterval = min(min_datainterval, fp->datainterval);
906 if (min_datainterval == 0xff) {
907 hwc_debug(" --> get empty\n");
911 pmin = 125 * (1 << min_datainterval);
913 return apply_hw_params_minmax(it, pmin, UINT_MAX);
916 /* get the EP or the sync EP for implicit fb when it's already set up */
917 static const struct snd_usb_endpoint *
918 get_sync_ep_from_substream(struct snd_usb_substream *subs)
920 struct snd_usb_audio *chip = subs->stream->chip;
921 const struct audioformat *fp;
922 const struct snd_usb_endpoint *ep;
924 list_for_each_entry(fp, &subs->fmt_list, list) {
925 ep = snd_usb_get_endpoint(chip, fp->endpoint);
926 if (ep && ep->cur_audiofmt) {
927 /* if EP is already opened solely for this substream,
928 * we still allow us to change the parameter; otherwise
929 * this substream has to follow the existing parameter
931 if (ep->cur_audiofmt != subs->cur_audiofmt || ep->opened > 1)
934 if (!fp->implicit_fb)
936 /* for the implicit fb, check the sync ep as well */
937 ep = snd_usb_get_endpoint(chip, fp->sync_ep);
938 if (ep && ep->cur_audiofmt)
944 /* additional hw constraints for implicit feedback mode */
945 static int hw_rule_format_implicit_fb(struct snd_pcm_hw_params *params,
946 struct snd_pcm_hw_rule *rule)
948 struct snd_usb_substream *subs = rule->private;
949 const struct snd_usb_endpoint *ep;
950 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
952 ep = get_sync_ep_from_substream(subs);
956 hwc_debug("applying %s\n", __func__);
957 return apply_hw_params_format_bits(fmt, pcm_format_to_bits(ep->cur_format));
960 static int hw_rule_rate_implicit_fb(struct snd_pcm_hw_params *params,
961 struct snd_pcm_hw_rule *rule)
963 struct snd_usb_substream *subs = rule->private;
964 const struct snd_usb_endpoint *ep;
965 struct snd_interval *it;
967 ep = get_sync_ep_from_substream(subs);
971 hwc_debug("applying %s\n", __func__);
972 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
973 return apply_hw_params_minmax(it, ep->cur_rate, ep->cur_rate);
976 static int hw_rule_period_size_implicit_fb(struct snd_pcm_hw_params *params,
977 struct snd_pcm_hw_rule *rule)
979 struct snd_usb_substream *subs = rule->private;
980 const struct snd_usb_endpoint *ep;
981 struct snd_interval *it;
983 ep = get_sync_ep_from_substream(subs);
987 hwc_debug("applying %s\n", __func__);
988 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
989 return apply_hw_params_minmax(it, ep->cur_period_frames,
990 ep->cur_period_frames);
993 static int hw_rule_periods_implicit_fb(struct snd_pcm_hw_params *params,
994 struct snd_pcm_hw_rule *rule)
996 struct snd_usb_substream *subs = rule->private;
997 const struct snd_usb_endpoint *ep;
998 struct snd_interval *it;
1000 ep = get_sync_ep_from_substream(subs);
1004 hwc_debug("applying %s\n", __func__);
1005 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIODS);
1006 return apply_hw_params_minmax(it, ep->cur_buffer_periods,
1007 ep->cur_buffer_periods);
1011 * set up the runtime hardware information.
1014 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1016 const struct audioformat *fp;
1017 unsigned int pt, ptmin;
1018 int param_period_time_if_needed = -1;
1021 runtime->hw.formats = subs->formats;
1023 runtime->hw.rate_min = 0x7fffffff;
1024 runtime->hw.rate_max = 0;
1025 runtime->hw.channels_min = 256;
1026 runtime->hw.channels_max = 0;
1027 runtime->hw.rates = 0;
1029 /* check min/max rates and channels */
1030 list_for_each_entry(fp, &subs->fmt_list, list) {
1031 runtime->hw.rates |= fp->rates;
1032 if (runtime->hw.rate_min > fp->rate_min)
1033 runtime->hw.rate_min = fp->rate_min;
1034 if (runtime->hw.rate_max < fp->rate_max)
1035 runtime->hw.rate_max = fp->rate_max;
1036 if (runtime->hw.channels_min > fp->channels)
1037 runtime->hw.channels_min = fp->channels;
1038 if (runtime->hw.channels_max < fp->channels)
1039 runtime->hw.channels_max = fp->channels;
1040 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1041 /* FIXME: there might be more than one audio formats... */
1042 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1045 pt = 125 * (1 << fp->datainterval);
1046 ptmin = min(ptmin, pt);
1049 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1050 if (subs->speed == USB_SPEED_FULL)
1051 /* full speed devices have fixed data packet interval */
1054 /* if period time doesn't go below 1 ms, no rules needed */
1055 param_period_time_if_needed = -1;
1057 err = snd_pcm_hw_constraint_minmax(runtime,
1058 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1063 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1065 SNDRV_PCM_HW_PARAM_RATE,
1066 SNDRV_PCM_HW_PARAM_FORMAT,
1067 SNDRV_PCM_HW_PARAM_CHANNELS,
1068 param_period_time_if_needed,
1073 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1074 hw_rule_channels, subs,
1075 SNDRV_PCM_HW_PARAM_CHANNELS,
1076 SNDRV_PCM_HW_PARAM_FORMAT,
1077 SNDRV_PCM_HW_PARAM_RATE,
1078 param_period_time_if_needed,
1082 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1083 hw_rule_format, subs,
1084 SNDRV_PCM_HW_PARAM_FORMAT,
1085 SNDRV_PCM_HW_PARAM_RATE,
1086 SNDRV_PCM_HW_PARAM_CHANNELS,
1087 param_period_time_if_needed,
1091 if (param_period_time_if_needed >= 0) {
1092 err = snd_pcm_hw_rule_add(runtime, 0,
1093 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1094 hw_rule_period_time, subs,
1095 SNDRV_PCM_HW_PARAM_FORMAT,
1096 SNDRV_PCM_HW_PARAM_CHANNELS,
1097 SNDRV_PCM_HW_PARAM_RATE,
1103 /* set max period and buffer sizes for 1 and 2 seconds, respectively */
1104 err = snd_pcm_hw_constraint_minmax(runtime,
1105 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1109 err = snd_pcm_hw_constraint_minmax(runtime,
1110 SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1115 /* additional hw constraints for implicit fb */
1116 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1117 hw_rule_format_implicit_fb, subs,
1118 SNDRV_PCM_HW_PARAM_FORMAT, -1);
1121 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1122 hw_rule_rate_implicit_fb, subs,
1123 SNDRV_PCM_HW_PARAM_RATE, -1);
1126 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1127 hw_rule_period_size_implicit_fb, subs,
1128 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1131 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1132 hw_rule_periods_implicit_fb, subs,
1133 SNDRV_PCM_HW_PARAM_PERIODS, -1);
1137 list_for_each_entry(fp, &subs->fmt_list, list) {
1138 if (fp->implicit_fb) {
1139 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1147 static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
1149 int direction = substream->stream;
1150 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1151 struct snd_pcm_runtime *runtime = substream->runtime;
1152 struct snd_usb_substream *subs = &as->substream[direction];
1155 runtime->hw = snd_usb_hardware;
1156 /* need an explicit sync to catch applptr update in low-latency mode */
1157 if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
1158 as->chip->lowlatency)
1159 runtime->hw.info |= SNDRV_PCM_INFO_SYNC_APPLPTR;
1160 runtime->private_data = subs;
1161 subs->pcm_substream = substream;
1162 /* runtime PM is also done there */
1164 /* initialize DSD/DOP context */
1165 subs->dsd_dop.byte_idx = 0;
1166 subs->dsd_dop.channel = 0;
1167 subs->dsd_dop.marker = 1;
1169 ret = setup_hw_info(runtime, subs);
1172 ret = snd_usb_autoresume(subs->stream->chip);
1175 ret = snd_media_stream_init(subs, as->pcm, direction);
1177 snd_usb_autosuspend(subs->stream->chip);
1181 static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
1183 int direction = substream->stream;
1184 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1185 struct snd_usb_substream *subs = &as->substream[direction];
1188 snd_media_stop_pipeline(subs);
1190 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
1191 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
1192 snd_usb_unlock_shutdown(subs->stream->chip);
1197 subs->pcm_substream = NULL;
1198 snd_usb_autosuspend(subs->stream->chip);
1203 /* Since a URB can handle only a single linear buffer, we must use double
1204 * buffering when the data to be transferred overflows the buffer boundary.
1205 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1208 static void retire_capture_urb(struct snd_usb_substream *subs,
1211 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1212 unsigned int stride, frames, bytes, oldptr;
1213 int i, period_elapsed = 0;
1214 unsigned long flags;
1216 int current_frame_number;
1218 /* read frame number here, update pointer in critical section */
1219 current_frame_number = usb_get_current_frame_number(subs->dev);
1221 stride = runtime->frame_bits >> 3;
1223 for (i = 0; i < urb->number_of_packets; i++) {
1224 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1225 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1226 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1227 i, urb->iso_frame_desc[i].status);
1230 bytes = urb->iso_frame_desc[i].actual_length;
1231 if (subs->stream_offset_adj > 0) {
1232 unsigned int adj = min(subs->stream_offset_adj, bytes);
1235 subs->stream_offset_adj -= adj;
1237 frames = bytes / stride;
1238 if (!subs->txfr_quirk)
1239 bytes = frames * stride;
1240 if (bytes % (runtime->sample_bits >> 3) != 0) {
1241 int oldbytes = bytes;
1242 bytes = frames * stride;
1243 dev_warn_ratelimited(&subs->dev->dev,
1244 "Corrected urb data len. %d->%d\n",
1247 /* update the current pointer */
1248 spin_lock_irqsave(&subs->lock, flags);
1249 oldptr = subs->hwptr_done;
1250 subs->hwptr_done += bytes;
1251 if (subs->hwptr_done >= subs->buffer_bytes)
1252 subs->hwptr_done -= subs->buffer_bytes;
1253 frames = (bytes + (oldptr % stride)) / stride;
1254 subs->transfer_done += frames;
1255 if (subs->transfer_done >= runtime->period_size) {
1256 subs->transfer_done -= runtime->period_size;
1260 /* realign last_frame_number */
1261 subs->last_frame_number = current_frame_number;
1263 spin_unlock_irqrestore(&subs->lock, flags);
1264 /* copy a data chunk */
1265 if (oldptr + bytes > subs->buffer_bytes) {
1266 unsigned int bytes1 = subs->buffer_bytes - oldptr;
1268 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1269 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1271 memcpy(runtime->dma_area + oldptr, cp, bytes);
1276 snd_pcm_period_elapsed(subs->pcm_substream);
1279 static void urb_ctx_queue_advance(struct snd_usb_substream *subs,
1280 struct urb *urb, unsigned int bytes)
1282 struct snd_urb_ctx *ctx = urb->context;
1284 ctx->queued += bytes;
1285 subs->inflight_bytes += bytes;
1286 subs->hwptr_done += bytes;
1287 if (subs->hwptr_done >= subs->buffer_bytes)
1288 subs->hwptr_done -= subs->buffer_bytes;
1291 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1292 struct urb *urb, unsigned int bytes)
1294 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1295 unsigned int dst_idx = 0;
1296 unsigned int src_idx = subs->hwptr_done;
1297 unsigned int wrap = subs->buffer_bytes;
1298 u8 *dst = urb->transfer_buffer;
1299 u8 *src = runtime->dma_area;
1300 static const u8 marker[] = { 0x05, 0xfa };
1301 unsigned int queued = 0;
1304 * The DSP DOP format defines a way to transport DSD samples over
1305 * normal PCM data endpoints. It requires stuffing of marker bytes
1306 * (0x05 and 0xfa, alternating per sample frame), and then expects
1307 * 2 additional bytes of actual payload. The whole frame is stored
1310 * Hence, for a stereo transport, the buffer layout looks like this,
1311 * where L refers to left channel samples and R to right.
1313 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1314 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1320 if (++subs->dsd_dop.byte_idx == 3) {
1321 /* frame boundary? */
1322 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1324 subs->dsd_dop.byte_idx = 0;
1326 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1327 /* alternate the marker */
1328 subs->dsd_dop.marker++;
1329 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1330 subs->dsd_dop.channel = 0;
1333 /* stuff the DSD payload */
1334 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1336 if (subs->cur_audiofmt->dsd_bitrev)
1337 dst[dst_idx++] = bitrev8(src[idx]);
1339 dst[dst_idx++] = src[idx];
1344 urb_ctx_queue_advance(subs, urb, queued);
1347 /* copy bit-reversed bytes onto transfer buffer */
1348 static void fill_playback_urb_dsd_bitrev(struct snd_usb_substream *subs,
1349 struct urb *urb, unsigned int bytes)
1351 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1352 const u8 *src = runtime->dma_area;
1353 u8 *buf = urb->transfer_buffer;
1354 int i, ofs = subs->hwptr_done;
1356 for (i = 0; i < bytes; i++) {
1357 *buf++ = bitrev8(src[ofs]);
1358 if (++ofs >= subs->buffer_bytes)
1362 urb_ctx_queue_advance(subs, urb, bytes);
1365 static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1366 int offset, int stride, unsigned int bytes)
1368 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1370 if (subs->hwptr_done + bytes > subs->buffer_bytes) {
1371 /* err, the transferred area goes over buffer boundary. */
1372 unsigned int bytes1 = subs->buffer_bytes - subs->hwptr_done;
1374 memcpy(urb->transfer_buffer + offset,
1375 runtime->dma_area + subs->hwptr_done, bytes1);
1376 memcpy(urb->transfer_buffer + offset + bytes1,
1377 runtime->dma_area, bytes - bytes1);
1379 memcpy(urb->transfer_buffer + offset,
1380 runtime->dma_area + subs->hwptr_done, bytes);
1383 urb_ctx_queue_advance(subs, urb, bytes);
1386 static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1387 struct urb *urb, int stride,
1390 __le32 packet_length;
1393 /* Put __le32 length descriptor at start of each packet. */
1394 for (i = 0; i < urb->number_of_packets; i++) {
1395 unsigned int length = urb->iso_frame_desc[i].length;
1396 unsigned int offset = urb->iso_frame_desc[i].offset;
1398 packet_length = cpu_to_le32(length);
1399 offset += i * sizeof(packet_length);
1400 urb->iso_frame_desc[i].offset = offset;
1401 urb->iso_frame_desc[i].length += sizeof(packet_length);
1402 memcpy(urb->transfer_buffer + offset,
1403 &packet_length, sizeof(packet_length));
1404 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1407 /* Adjust transfer size accordingly. */
1408 bytes += urb->number_of_packets * sizeof(packet_length);
1412 static int prepare_playback_urb(struct snd_usb_substream *subs,
1414 bool in_stream_lock)
1416 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1417 struct snd_usb_endpoint *ep = subs->data_endpoint;
1418 struct snd_urb_ctx *ctx = urb->context;
1419 unsigned int frames, bytes;
1421 unsigned int transfer_done, frame_limit, avail = 0;
1422 int i, stride, period_elapsed = 0;
1423 unsigned long flags;
1426 stride = ep->stride;
1430 urb->number_of_packets = 0;
1432 spin_lock_irqsave(&subs->lock, flags);
1433 frame_limit = subs->frame_limit + ep->max_urb_frames;
1434 transfer_done = subs->transfer_done;
1436 if (subs->lowlatency_playback &&
1437 runtime->state != SNDRV_PCM_STATE_DRAINING) {
1438 unsigned int hwptr = subs->hwptr_done / stride;
1440 /* calculate the byte offset-in-buffer of the appl_ptr */
1441 avail = (runtime->control->appl_ptr - runtime->hw_ptr_base)
1442 % runtime->buffer_size;
1444 avail += runtime->buffer_size;
1448 for (i = 0; i < ctx->packets; i++) {
1449 counts = snd_usb_endpoint_next_packet_size(ep, ctx, i, avail);
1452 /* set up descriptor */
1453 urb->iso_frame_desc[i].offset = frames * stride;
1454 urb->iso_frame_desc[i].length = counts * stride;
1457 urb->number_of_packets++;
1458 transfer_done += counts;
1459 if (transfer_done >= runtime->period_size) {
1460 transfer_done -= runtime->period_size;
1463 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1464 if (transfer_done > 0) {
1465 /* FIXME: fill-max mode is not
1467 frames -= transfer_done;
1468 counts -= transfer_done;
1469 urb->iso_frame_desc[i].length =
1474 if (i < ctx->packets) {
1475 /* add a transfer delimiter */
1476 urb->iso_frame_desc[i].offset =
1478 urb->iso_frame_desc[i].length = 0;
1479 urb->number_of_packets++;
1484 /* finish at the period boundary or after enough frames */
1485 if ((period_elapsed || transfer_done >= frame_limit) &&
1486 !snd_usb_endpoint_implicit_feedback_sink(ep))
1495 bytes = frames * stride;
1496 subs->transfer_done = transfer_done;
1497 subs->frame_limit = frame_limit;
1498 if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1499 subs->cur_audiofmt->dsd_dop)) {
1500 fill_playback_urb_dsd_dop(subs, urb, bytes);
1501 } else if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1502 subs->cur_audiofmt->dsd_bitrev)) {
1503 fill_playback_urb_dsd_bitrev(subs, urb, bytes);
1506 if (!subs->tx_length_quirk)
1507 copy_to_urb(subs, urb, 0, stride, bytes);
1509 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1510 /* bytes is now amount of outgoing data */
1513 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1515 if (subs->trigger_tstamp_pending_update) {
1516 /* this is the first actual URB submitted,
1517 * update trigger timestamp to reflect actual start time
1519 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1520 subs->trigger_tstamp_pending_update = false;
1523 if (period_elapsed && !subs->running && subs->lowlatency_playback) {
1524 subs->period_elapsed_pending = 1;
1529 spin_unlock_irqrestore(&subs->lock, flags);
1532 urb->transfer_buffer_length = bytes;
1533 if (period_elapsed) {
1535 snd_pcm_period_elapsed_under_stream_lock(subs->pcm_substream);
1537 snd_pcm_period_elapsed(subs->pcm_substream);
1543 * process after playback data complete
1544 * - decrease the delay count again
1546 static void retire_playback_urb(struct snd_usb_substream *subs,
1549 unsigned long flags;
1550 struct snd_urb_ctx *ctx = urb->context;
1551 bool period_elapsed = false;
1553 spin_lock_irqsave(&subs->lock, flags);
1555 if (subs->inflight_bytes >= ctx->queued)
1556 subs->inflight_bytes -= ctx->queued;
1558 subs->inflight_bytes = 0;
1561 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1562 if (subs->running) {
1563 period_elapsed = subs->period_elapsed_pending;
1564 subs->period_elapsed_pending = 0;
1566 spin_unlock_irqrestore(&subs->lock, flags);
1568 snd_pcm_period_elapsed(subs->pcm_substream);
1571 /* PCM ack callback for the playback stream;
1572 * this plays a role only when the stream is running in low-latency mode.
1574 static int snd_usb_pcm_playback_ack(struct snd_pcm_substream *substream)
1576 struct snd_usb_substream *subs = substream->runtime->private_data;
1577 struct snd_usb_endpoint *ep;
1579 if (!subs->lowlatency_playback || !subs->running)
1581 ep = subs->data_endpoint;
1584 /* When no more in-flight URBs available, try to process the pending
1587 if (!ep->active_mask)
1588 snd_usb_queue_pending_output_urbs(ep, true);
1592 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1595 struct snd_usb_substream *subs = substream->runtime->private_data;
1599 case SNDRV_PCM_TRIGGER_START:
1600 subs->trigger_tstamp_pending_update = true;
1602 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1603 snd_usb_endpoint_set_callback(subs->data_endpoint,
1604 prepare_playback_urb,
1605 retire_playback_urb,
1607 if (subs->lowlatency_playback &&
1608 cmd == SNDRV_PCM_TRIGGER_START) {
1609 if (in_free_wheeling_mode(substream->runtime))
1610 subs->lowlatency_playback = false;
1611 err = start_endpoints(subs);
1613 snd_usb_endpoint_set_callback(subs->data_endpoint,
1619 dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n",
1620 subs->cur_audiofmt->iface,
1621 subs->cur_audiofmt->altsetting);
1623 case SNDRV_PCM_TRIGGER_SUSPEND:
1624 case SNDRV_PCM_TRIGGER_STOP:
1625 stop_endpoints(subs, substream->runtime->state == SNDRV_PCM_STATE_DRAINING);
1626 snd_usb_endpoint_set_callback(subs->data_endpoint,
1629 dev_dbg(&subs->dev->dev, "%d:%d Stop Playback PCM\n",
1630 subs->cur_audiofmt->iface,
1631 subs->cur_audiofmt->altsetting);
1633 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1634 /* keep retire_data_urb for delay calculation */
1635 snd_usb_endpoint_set_callback(subs->data_endpoint,
1637 retire_playback_urb,
1640 dev_dbg(&subs->dev->dev, "%d:%d Pause Playback PCM\n",
1641 subs->cur_audiofmt->iface,
1642 subs->cur_audiofmt->altsetting);
1649 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1653 struct snd_usb_substream *subs = substream->runtime->private_data;
1656 case SNDRV_PCM_TRIGGER_START:
1657 err = start_endpoints(subs);
1661 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1662 snd_usb_endpoint_set_callback(subs->data_endpoint,
1663 NULL, retire_capture_urb,
1665 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1667 dev_dbg(&subs->dev->dev, "%d:%d Start Capture PCM\n",
1668 subs->cur_audiofmt->iface,
1669 subs->cur_audiofmt->altsetting);
1671 case SNDRV_PCM_TRIGGER_SUSPEND:
1672 case SNDRV_PCM_TRIGGER_STOP:
1673 stop_endpoints(subs, false);
1675 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1676 snd_usb_endpoint_set_callback(subs->data_endpoint,
1679 dev_dbg(&subs->dev->dev, "%d:%d Stop Capture PCM\n",
1680 subs->cur_audiofmt->iface,
1681 subs->cur_audiofmt->altsetting);
1688 static const struct snd_pcm_ops snd_usb_playback_ops = {
1689 .open = snd_usb_pcm_open,
1690 .close = snd_usb_pcm_close,
1691 .hw_params = snd_usb_hw_params,
1692 .hw_free = snd_usb_hw_free,
1693 .prepare = snd_usb_pcm_prepare,
1694 .trigger = snd_usb_substream_playback_trigger,
1695 .sync_stop = snd_usb_pcm_sync_stop,
1696 .pointer = snd_usb_pcm_pointer,
1697 .ack = snd_usb_pcm_playback_ack,
1700 static const struct snd_pcm_ops snd_usb_capture_ops = {
1701 .open = snd_usb_pcm_open,
1702 .close = snd_usb_pcm_close,
1703 .hw_params = snd_usb_hw_params,
1704 .hw_free = snd_usb_hw_free,
1705 .prepare = snd_usb_pcm_prepare,
1706 .trigger = snd_usb_substream_capture_trigger,
1707 .sync_stop = snd_usb_pcm_sync_stop,
1708 .pointer = snd_usb_pcm_pointer,
1711 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1713 const struct snd_pcm_ops *ops;
1715 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
1716 &snd_usb_playback_ops : &snd_usb_capture_ops;
1717 snd_pcm_set_ops(pcm, stream, ops);
1720 void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1722 struct snd_pcm *pcm = subs->stream->pcm;
1723 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1724 struct device *dev = subs->dev->bus->sysdev;
1726 if (snd_usb_use_vmalloc)
1727 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
1730 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
1731 dev, 64*1024, 512*1024);