Lines Matching +full:pcm +full:- +full:sync +full:- +full:mode

1 // SPDX-License-Identifier: GPL-2.0-or-later
11 #include <linux/usb/audio-v2.h>
14 #include <sound/pcm.h>
22 #include "pcm.h"
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);
54 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
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.
74 struct snd_pcm_runtime *runtime = substream->runtime;
75 struct snd_usb_substream *subs = runtime->private_data;
78 if (atomic_read(&subs->stream->chip->shutdown))
80 scoped_guard(spinlock, &subs->lock) {
81 hwptr_done = subs->hwptr_done;
82 runtime->delay = snd_usb_pcm_delay(subs, runtime);
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;
125 * M-audio audiophile USB.
129 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
131 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
134 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
136 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
143 if (fp->maxpacksize > found->maxpacksize) {
165 return find_format(&subs->fmt_list, params_format(params),
182 int rate = -1;
186 chip = subs->stream->chip;
187 if (!(chip->quirk_flags & QUIRK_FLAG_FIXED_RATE))
189 list_for_each_entry(fp, &subs->fmt_list, list) {
190 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
192 if (fp->nr_rates < 1)
194 if (fp->nr_rates > 1)
197 rate = fp->rate_table[0];
200 if (rate != fp->rate_table[0])
208 struct usb_device *dev = chip->dev;
222 struct usb_device *dev = chip->dev;
243 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
246 usb_audio_dbg(chip, "enable PITCH for EP 0x%x\n", fmt->endpoint);
248 switch (fmt->protocol) {
250 err = init_pitch_v1(chip, fmt->endpoint);
253 err = init_pitch_v2(chip, fmt->endpoint);
261 fmt->endpoint);
272 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
273 snd_usb_endpoint_stop(subs->sync_endpoint, keep_pending);
276 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
277 snd_usb_endpoint_stop(subs->data_endpoint, keep_pending);
287 if (!subs->data_endpoint)
288 return -EINVAL;
290 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
291 err = snd_usb_endpoint_start(subs->data_endpoint);
293 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
298 if (subs->sync_endpoint &&
299 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
300 err = snd_usb_endpoint_start(subs->sync_endpoint);
302 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
316 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
317 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
320 /* PCM sync_stop callback */
323 struct snd_usb_substream *subs = substream->runtime->private_data;
329 /* Set up sync endpoint */
333 struct usb_device *dev = chip->dev;
340 if (fmt->sync_ep)
343 alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting);
353 * Generic sync EP handling
356 if (fmt->ep_idx > 0 || altsd->bNumEndpoints < 2)
359 is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
360 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
366 sync_attr = get_endpoint(alts, 1)->bmAttributes;
370 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
371 * error fall back to SYNC mode and don't create sync endpoint
374 /* check sync-pipe endpoint */
379 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
380 get_endpoint(alts, 1)->bSynchAddress != 0)) {
381 dev_err(&dev->dev,
382 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
383 fmt->iface, fmt->altsetting,
384 get_endpoint(alts, 1)->bmAttributes,
385 get_endpoint(alts, 1)->bLength,
386 get_endpoint(alts, 1)->bSynchAddress);
389 return -EINVAL;
391 ep = get_endpoint(alts, 1)->bEndpointAddress;
392 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
393 get_endpoint(alts, 0)->bSynchAddress != 0 &&
394 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
395 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
396 dev_err(&dev->dev,
397 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
398 fmt->iface, fmt->altsetting,
399 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
402 return -EINVAL;
405 fmt->sync_ep = ep;
406 fmt->sync_iface = altsd->bInterfaceNumber;
407 fmt->sync_altsetting = altsd->bAlternateSetting;
408 fmt->sync_ep_idx = 1;
410 fmt->implicit_fb = 1;
412 dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
413 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
414 fmt->sync_altsetting, fmt->implicit_fb);
423 if (!subs->str_pd)
426 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
428 dev_err(&subs->dev->dev,
430 subs->str_pd->pd_id, state, ret);
441 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
445 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
456 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
460 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
470 if (subs->data_endpoint) {
471 snd_usb_endpoint_set_sync(chip, subs->data_endpoint, NULL);
472 snd_usb_endpoint_close(chip, subs->data_endpoint);
473 subs->data_endpoint = NULL;
476 if (subs->sync_endpoint) {
477 snd_usb_endpoint_close(chip, subs->sync_endpoint);
478 subs->sync_endpoint = NULL;
485 struct snd_usb_audio *chip = subs->stream->chip;
502 ret = -EINVAL;
506 if (fmt->implicit_fb) {
509 !subs->direction,
513 "cannot find sync format: ep=0x%x, iface=%d:%d, format=%s, rate=%d, channels=%d\n",
514 fmt->sync_ep, fmt->sync_iface,
515 fmt->sync_altsetting,
518 ret = -EINVAL;
534 if (subs->data_endpoint) {
535 if (snd_usb_endpoint_compatible(chip, subs->data_endpoint,
543 subs->data_endpoint = snd_usb_endpoint_open(chip, fmt, hw_params, false, fixed_rate);
544 if (!subs->data_endpoint) {
545 ret = -EINVAL;
549 if (fmt->sync_ep) {
550 subs->sync_endpoint = snd_usb_endpoint_open(chip, sync_fmt,
554 if (!subs->sync_endpoint) {
555 ret = -EINVAL;
559 snd_usb_endpoint_set_sync(chip, subs->data_endpoint,
560 subs->sync_endpoint);
563 scoped_guard(mutex, &chip->mutex) {
564 subs->cur_audiofmt = fmt;
567 if (!subs->data_endpoint->need_setup)
570 if (subs->sync_endpoint) {
571 ret = snd_usb_endpoint_set_params(chip, subs->sync_endpoint);
576 ret = snd_usb_endpoint_set_params(chip, subs->data_endpoint);
604 struct snd_usb_substream *subs = substream->runtime->private_data;
611 struct snd_usb_audio *chip = subs->stream->chip;
614 scoped_guard(mutex, &chip->mutex) {
615 subs->cur_audiofmt = NULL;
635 struct snd_usb_substream *subs = substream->runtime->private_data;
640 /* free-wheeling mode? (e.g. dmix) */
643 return runtime->stop_threshold > runtime->buffer_size;
650 struct snd_usb_audio *chip = subs->stream->chip;
652 if (subs->direction == SNDRV_PCM_STREAM_CAPTURE)
655 if (!chip->lowlatency)
659 /* implicit feedback mode has own operation mode */
660 if (snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint))
672 struct snd_pcm_runtime *runtime = substream->runtime;
673 struct snd_usb_substream *subs = runtime->private_data;
674 struct snd_usb_audio *chip = subs->stream->chip;
681 if (snd_BUG_ON(!subs->data_endpoint))
682 return -EIO;
689 if (subs->sync_endpoint) {
690 ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint);
695 ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint);
699 snd_usb_set_format_quirk(subs, subs->cur_audiofmt);
703 subs->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
704 subs->inflight_bytes = 0;
705 subs->hwptr_done = 0;
706 subs->transfer_done = 0;
707 subs->last_frame_number = 0;
708 subs->period_elapsed_pending = 0;
709 runtime->delay = 0;
711 subs->lowlatency_playback = lowlatency_playback_available(runtime, subs);
712 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
713 !subs->lowlatency_playback) {
718 if (ret == -EPIPE && !retry++) {
767 check_fmts.bits[0] = (u32)fp->formats;
768 check_fmts.bits[1] = (u32)(fp->formats >> 32);
771 hwc_debug(" > check: no supported format 0x%llx\n", fp->formats);
775 if (fp->channels < ct->min || fp->channels > ct->max) {
776 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
780 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
781 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
784 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
785 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
789 if (subs->speed != USB_SPEED_FULL) {
790 ptime = 125 * (1 << fp->datainterval);
791 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
792 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
805 hwc_debug(" --> get empty\n");
806 it->empty = 1;
807 return -EINVAL;
811 if (it->min < rmin) {
812 it->min = rmin;
813 it->openmin = 0;
816 if (it->max > rmax) {
817 it->max = rmax;
818 it->openmax = 0;
822 it->empty = 1;
823 return -EINVAL;
825 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
839 if (ep && ep->cur_audiofmt && (ep != ref_ep || ep->opened > 1))
847 struct snd_usb_substream *subs = rule->private;
848 struct snd_usb_audio *chip = subs->stream->chip;
855 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
858 list_for_each_entry(fp, &subs->fmt_list, list) {
862 ep = get_endpoint_in_use(chip, fp->endpoint,
863 subs->data_endpoint);
866 ep->cur_rate, fp->endpoint);
867 rmin = min(rmin, ep->cur_rate);
868 rmax = max(rmax, ep->cur_rate);
872 if (fp->implicit_fb) {
873 ep = get_endpoint_in_use(chip, fp->sync_ep,
874 subs->sync_endpoint);
877 ep->cur_rate, fp->sync_ep);
878 rmin = min(rmin, ep->cur_rate);
879 rmax = max(rmax, ep->cur_rate);
884 r = snd_usb_endpoint_get_clock_rate(chip, fp->clock);
892 if (fp->rate_table && fp->nr_rates) {
893 for (i = 0; i < fp->nr_rates; i++) {
894 r = fp->rate_table[i];
901 rmin = min(rmin, fp->rate_min);
902 rmax = max(rmax, fp->rate_max);
913 struct snd_usb_substream *subs = rule->private;
918 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
921 list_for_each_entry(fp, &subs->fmt_list, list) {
924 rmin = min(rmin, fp->channels);
925 rmax = max(rmax, fp->channels);
936 oldbits[0] = fmt->bits[0];
937 oldbits[1] = fmt->bits[1];
938 fmt->bits[0] &= (u32)fbits;
939 fmt->bits[1] &= (u32)(fbits >> 32);
940 if (!fmt->bits[0] && !fmt->bits[1]) {
941 hwc_debug(" --> get empty\n");
942 return -EINVAL;
944 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
945 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
952 struct snd_usb_substream *subs = rule->private;
953 struct snd_usb_audio *chip = subs->stream->chip;
959 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
961 list_for_each_entry(fp, &subs->fmt_list, list) {
965 ep = get_endpoint_in_use(chip, fp->endpoint,
966 subs->data_endpoint);
969 ep->cur_format, fp->endpoint);
970 fbits |= pcm_format_to_bits(ep->cur_format);
974 if (fp->implicit_fb) {
975 ep = get_endpoint_in_use(chip, fp->sync_ep,
976 subs->sync_endpoint);
979 ep->cur_format, fp->sync_ep);
980 fbits |= pcm_format_to_bits(ep->cur_format);
985 fbits |= fp->formats;
993 struct snd_usb_substream *subs = rule->private;
1000 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1002 list_for_each_entry(fp, &subs->fmt_list, list) {
1005 min_datainterval = min(min_datainterval, fp->datainterval);
1008 hwc_debug(" --> get empty\n");
1009 it->empty = 1;
1010 return -EINVAL;
1017 /* additional hw constraints for implicit feedback mode */
1021 struct snd_usb_substream *subs = rule->private;
1022 struct snd_usb_audio *chip = subs->stream->chip;
1029 hwc_debug("hw_rule_period_size: (%u,%u)\n", it->min, it->max);
1032 list_for_each_entry(fp, &subs->fmt_list, list) {
1035 ep = get_endpoint_in_use(chip, fp->endpoint,
1036 subs->data_endpoint);
1039 ep->cur_period_frames, fp->endpoint);
1040 rmin = min(rmin, ep->cur_period_frames);
1041 rmax = max(rmax, ep->cur_period_frames);
1045 if (fp->implicit_fb) {
1046 ep = get_endpoint_in_use(chip, fp->sync_ep,
1047 subs->sync_endpoint);
1050 ep->cur_period_frames, fp->sync_ep);
1051 rmin = min(rmin, ep->cur_period_frames);
1052 rmax = max(rmax, ep->cur_period_frames);
1066 struct snd_usb_substream *subs = rule->private;
1067 struct snd_usb_audio *chip = subs->stream->chip;
1074 hwc_debug("hw_rule_periods: (%u,%u)\n", it->min, it->max);
1077 list_for_each_entry(fp, &subs->fmt_list, list) {
1080 ep = get_endpoint_in_use(chip, fp->endpoint,
1081 subs->data_endpoint);
1084 ep->cur_buffer_periods, fp->endpoint);
1085 rmin = min(rmin, ep->cur_buffer_periods);
1086 rmax = max(rmax, ep->cur_buffer_periods);
1090 if (fp->implicit_fb) {
1091 ep = get_endpoint_in_use(chip, fp->sync_ep,
1092 subs->sync_endpoint);
1095 ep->cur_buffer_periods, fp->sync_ep);
1096 rmin = min(rmin, ep->cur_buffer_periods);
1097 rmax = max(rmax, ep->cur_buffer_periods);
1116 int param_period_time_if_needed = -1;
1119 runtime->hw.formats = subs->formats;
1121 runtime->hw.rate_min = 0x7fffffff;
1122 runtime->hw.rate_max = 0;
1123 runtime->hw.channels_min = 256;
1124 runtime->hw.channels_max = 0;
1125 runtime->hw.rates = 0;
1128 list_for_each_entry(fp, &subs->fmt_list, list) {
1129 runtime->hw.rates |= fp->rates;
1130 if (runtime->hw.rate_min > fp->rate_min)
1131 runtime->hw.rate_min = fp->rate_min;
1132 if (runtime->hw.rate_max < fp->rate_max)
1133 runtime->hw.rate_max = fp->rate_max;
1134 if (runtime->hw.channels_min > fp->channels)
1135 runtime->hw.channels_min = fp->channels;
1136 if (runtime->hw.channels_max < fp->channels)
1137 runtime->hw.channels_max = fp->channels;
1138 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1140 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1141 fp->frame_size;
1143 pt = 125 * (1 << fp->datainterval);
1148 if (subs->speed == USB_SPEED_FULL)
1153 param_period_time_if_needed = -1;
1167 -1);
1177 -1);
1186 -1);
1196 -1);
1216 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1221 SNDRV_PCM_HW_PARAM_PERIODS, -1);
1225 list_for_each_entry(fp, &subs->fmt_list, list) {
1226 if (fp->implicit_fb) {
1227 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1237 int direction = substream->stream;
1239 struct snd_pcm_runtime *runtime = substream->runtime;
1240 struct snd_usb_substream *subs = &as->substream[direction];
1241 struct snd_usb_audio *chip = subs->stream->chip;
1244 scoped_guard(mutex, &chip->mutex) {
1245 if (subs->opened)
1246 return -EBUSY;
1247 subs->opened = 1;
1250 runtime->hw = snd_usb_hardware;
1251 /* need an explicit sync to catch applptr update in low-latency mode */
1253 as->chip->lowlatency)
1254 runtime->hw.info |= SNDRV_PCM_INFO_SYNC_APPLPTR;
1255 runtime->private_data = subs;
1256 subs->pcm_substream = substream;
1260 subs->dsd_dop.byte_idx = 0;
1261 subs->dsd_dop.channel = 0;
1262 subs->dsd_dop.marker = 1;
1267 ret = snd_usb_autoresume(subs->stream->chip);
1270 ret = snd_media_stream_init(subs, as->pcm, direction);
1277 snd_usb_autosuspend(subs->stream->chip);
1279 scoped_guard(mutex, &chip->mutex) {
1280 subs->opened = 0;
1288 int direction = substream->stream;
1290 struct snd_usb_substream *subs = &as->substream[direction];
1291 struct snd_usb_audio *chip = subs->stream->chip;
1297 CLASS(snd_usb_lock, pm)(subs->stream->chip);
1305 subs->pcm_substream = NULL;
1306 snd_usb_autosuspend(subs->stream->chip);
1307 scoped_guard(mutex, &chip->mutex) {
1308 subs->opened = 0;
1322 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1329 current_frame_number = usb_get_current_frame_number(subs->dev);
1331 stride = runtime->frame_bits >> 3;
1333 for (i = 0; i < urb->number_of_packets; i++) {
1334 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1335 if (urb->iso_frame_desc[i].status)
1336 dev_dbg_ratelimited(&subs->dev->dev,
1338 urb->iso_frame_desc[i].status);
1339 bytes = urb->iso_frame_desc[i].actual_length;
1340 if (subs->stream_offset_adj > 0) {
1341 unsigned int adj = min(subs->stream_offset_adj, bytes);
1343 bytes -= adj;
1344 subs->stream_offset_adj -= adj;
1347 if (!subs->txfr_quirk)
1349 if (bytes % (runtime->sample_bits >> 3) != 0) {
1352 dev_warn_ratelimited(&subs->dev->dev,
1353 "Corrected urb data len. %d->%d\n",
1357 scoped_guard(spinlock_irqsave, &subs->lock) {
1358 oldptr = subs->hwptr_done;
1359 subs->hwptr_done += bytes;
1360 if (subs->hwptr_done >= subs->buffer_bytes)
1361 subs->hwptr_done -= subs->buffer_bytes;
1363 subs->transfer_done += frames;
1364 if (subs->transfer_done >= runtime->period_size) {
1365 subs->transfer_done -= runtime->period_size;
1370 subs->last_frame_number = current_frame_number;
1373 if (oldptr + bytes > subs->buffer_bytes) {
1374 unsigned int bytes1 = subs->buffer_bytes - oldptr;
1376 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1377 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1379 memcpy(runtime->dma_area + oldptr, cp, bytes);
1384 snd_pcm_period_elapsed(subs->pcm_substream);
1390 struct snd_urb_ctx *ctx = urb->context;
1392 ctx->queued += bytes;
1393 subs->inflight_bytes += bytes;
1394 subs->hwptr_done += bytes;
1395 if (subs->hwptr_done >= subs->buffer_bytes)
1396 subs->hwptr_done -= subs->buffer_bytes;
1402 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1404 unsigned int src_idx = subs->hwptr_done;
1405 unsigned int wrap = subs->buffer_bytes;
1406 u8 *dst = urb->transfer_buffer;
1407 u8 *src = runtime->dma_area;
1413 * normal PCM data endpoints. It requires stuffing of marker bytes
1427 while (bytes--) {
1428 if (++subs->dsd_dop.byte_idx == 3) {
1430 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1432 subs->dsd_dop.byte_idx = 0;
1434 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1436 subs->dsd_dop.marker++;
1437 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1438 subs->dsd_dop.channel = 0;
1442 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1444 if (subs->cur_audiofmt->dsd_bitrev)
1455 /* copy bit-reversed bytes onto transfer buffer */
1459 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1460 const u8 *src = runtime->dma_area;
1461 u8 *buf = urb->transfer_buffer;
1462 int i, ofs = subs->hwptr_done;
1466 if (++ofs >= subs->buffer_bytes)
1476 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1478 if (subs->hwptr_done + bytes > subs->buffer_bytes) {
1480 unsigned int bytes1 = subs->buffer_bytes - subs->hwptr_done;
1482 memcpy(urb->transfer_buffer + offset,
1483 runtime->dma_area + subs->hwptr_done, bytes1);
1484 memcpy(urb->transfer_buffer + offset + bytes1,
1485 runtime->dma_area, bytes - bytes1);
1487 memcpy(urb->transfer_buffer + offset,
1488 runtime->dma_area + subs->hwptr_done, bytes);
1502 for (i = 0; i < urb->number_of_packets; i++) {
1503 unsigned int length = urb->iso_frame_desc[i].length;
1504 unsigned int offset = urb->iso_frame_desc[i].offset;
1508 urb->iso_frame_desc[i].offset = offset;
1509 urb->iso_frame_desc[i].length += sizeof(packet_length);
1510 memcpy(urb->transfer_buffer + offset,
1516 bytes += urb->number_of_packets * sizeof(packet_length);
1524 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1525 struct snd_usb_endpoint *ep = subs->data_endpoint;
1526 struct snd_urb_ctx *ctx = urb->context;
1532 stride = ep->stride;
1535 ctx->queued = 0;
1536 urb->number_of_packets = 0;
1538 scoped_guard(spinlock_irqsave, &subs->lock) {
1539 frame_limit = subs->frame_limit + ep->max_urb_frames;
1540 transfer_done = subs->transfer_done;
1542 if (subs->lowlatency_playback &&
1543 runtime->state != SNDRV_PCM_STATE_DRAINING) {
1544 unsigned int hwptr = subs->hwptr_done / stride;
1546 /* calculate the byte offset-in-buffer of the appl_ptr */
1547 avail = (runtime->control->appl_ptr - runtime->hw_ptr_base)
1548 % runtime->buffer_size;
1550 avail += runtime->buffer_size;
1551 avail -= hwptr;
1554 for (i = 0; i < ctx->packets; i++) {
1559 urb->iso_frame_desc[i].offset = frames * stride;
1560 urb->iso_frame_desc[i].length = counts * stride;
1562 avail -= counts;
1563 urb->number_of_packets++;
1565 if (transfer_done >= runtime->period_size) {
1566 transfer_done -= runtime->period_size;
1569 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1571 /* FIXME: fill-max mode is not
1573 frames -= transfer_done;
1574 counts -= transfer_done;
1575 urb->iso_frame_desc[i].length =
1580 if (i < ctx->packets) {
1582 urb->iso_frame_desc[i].offset =
1584 urb->iso_frame_desc[i].length = 0;
1585 urb->number_of_packets++;
1597 return -EAGAIN;
1600 subs->transfer_done = transfer_done;
1601 subs->frame_limit = frame_limit;
1602 if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1603 subs->cur_audiofmt->dsd_dop)) {
1605 } else if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1606 subs->cur_audiofmt->dsd_bitrev)) {
1609 /* usual PCM */
1610 if (!subs->tx_length_quirk)
1617 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1619 if (subs->trigger_tstamp_pending_update) {
1623 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1624 subs->trigger_tstamp_pending_update = false;
1627 if (period_elapsed && !subs->running && subs->lowlatency_playback) {
1628 subs->period_elapsed_pending = 1;
1633 urb->transfer_buffer_length = bytes;
1636 snd_pcm_period_elapsed_under_stream_lock(subs->pcm_substream);
1638 snd_pcm_period_elapsed(subs->pcm_substream);
1645 * - decrease the delay count again
1650 struct snd_urb_ctx *ctx = urb->context;
1653 scoped_guard(spinlock_irqsave, &subs->lock) {
1654 if (ctx->queued) {
1655 if (subs->inflight_bytes >= ctx->queued)
1656 subs->inflight_bytes -= ctx->queued;
1658 subs->inflight_bytes = 0;
1661 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1662 if (subs->running) {
1663 period_elapsed = subs->period_elapsed_pending;
1664 subs->period_elapsed_pending = 0;
1668 snd_pcm_period_elapsed(subs->pcm_substream);
1671 /* PCM ack callback for the playback stream;
1672 * this plays a role only when the stream is running in low-latency mode.
1676 struct snd_usb_substream *subs = substream->runtime->private_data;
1679 if (!subs->lowlatency_playback || !subs->running)
1681 ep = subs->data_endpoint;
1684 /* When no more in-flight URBs available, try to process the pending
1687 if (!ep->active_mask)
1695 struct snd_usb_substream *subs = substream->runtime->private_data;
1700 subs->trigger_tstamp_pending_update = true;
1703 snd_usb_endpoint_set_callback(subs->data_endpoint,
1707 if (subs->lowlatency_playback &&
1709 if (in_free_wheeling_mode(substream->runtime))
1710 subs->lowlatency_playback = false;
1713 snd_usb_endpoint_set_callback(subs->data_endpoint,
1718 subs->running = 1;
1719 dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n",
1720 subs->cur_audiofmt->iface,
1721 subs->cur_audiofmt->altsetting);
1725 stop_endpoints(subs, substream->runtime->state == SNDRV_PCM_STATE_DRAINING);
1726 snd_usb_endpoint_set_callback(subs->data_endpoint,
1728 subs->running = 0;
1729 dev_dbg(&subs->dev->dev, "%d:%d Stop Playback PCM\n",
1730 subs->cur_audiofmt->iface,
1731 subs->cur_audiofmt->altsetting);
1735 snd_usb_endpoint_set_callback(subs->data_endpoint,
1739 subs->running = 0;
1740 dev_dbg(&subs->dev->dev, "%d:%d Pause Playback PCM\n",
1741 subs->cur_audiofmt->iface,
1742 subs->cur_audiofmt->altsetting);
1746 return -EINVAL;
1753 struct snd_usb_substream *subs = substream->runtime->private_data;
1762 snd_usb_endpoint_set_callback(subs->data_endpoint,
1765 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1766 subs->running = 1;
1767 dev_dbg(&subs->dev->dev, "%d:%d Start Capture PCM\n",
1768 subs->cur_audiofmt->iface,
1769 subs->cur_audiofmt->altsetting);
1776 snd_usb_endpoint_set_callback(subs->data_endpoint,
1778 subs->running = 0;
1779 dev_dbg(&subs->dev->dev, "%d:%d Stop Capture PCM\n",
1780 subs->cur_audiofmt->iface,
1781 subs->cur_audiofmt->altsetting);
1785 return -EINVAL;
1811 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1817 snd_pcm_set_ops(pcm, stream, ops);
1822 struct snd_pcm *pcm = subs->stream->pcm;
1823 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1824 struct device *dev = subs->dev->bus->sysdev;