Lines Matching refs:subs

32 static snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
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;
60 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
75 struct snd_usb_substream *subs = runtime->private_data;
78 if (atomic_read(&subs->stream->chip->shutdown))
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);
93 struct snd_usb_substream *subs)
127 if (subs && attr != cur_attr) {
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)) {
152 find_substream_format(struct snd_usb_substream *subs,
155 return find_format(&subs->fmt_list, params_format(params),
157 true, subs);
160 bool snd_usb_pcm_has_fixed_rate(struct snd_usb_substream *subs)
166 if (!subs)
168 chip = subs->stream->chip;
171 list_for_each_entry(fp, &subs->fmt_list, list) {
250 static bool stop_endpoints(struct snd_usb_substream *subs, bool keep_pending)
254 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
255 snd_usb_endpoint_stop(subs->sync_endpoint, keep_pending);
258 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
259 snd_usb_endpoint_stop(subs->data_endpoint, keep_pending);
265 static int start_endpoints(struct snd_usb_substream *subs)
269 if (!subs->data_endpoint)
272 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
273 err = snd_usb_endpoint_start(subs->data_endpoint);
275 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
280 if (subs->sync_endpoint &&
281 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
282 err = snd_usb_endpoint_start(subs->sync_endpoint);
284 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
292 stop_endpoints(subs, false);
296 static void sync_pending_stops(struct snd_usb_substream *subs)
298 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
299 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
305 struct snd_usb_substream *subs = substream->runtime->private_data;
307 sync_pending_stops(subs);
401 static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
405 if (!subs->str_pd)
408 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
410 dev_err(&subs->dev->dev,
412 subs->str_pd->pd_id, state, ret);
450 struct snd_usb_substream *subs)
452 if (subs->data_endpoint) {
453 snd_usb_endpoint_set_sync(chip, subs->data_endpoint, NULL);
454 snd_usb_endpoint_close(chip, subs->data_endpoint);
455 subs->data_endpoint = NULL;
458 if (subs->sync_endpoint) {
459 snd_usb_endpoint_close(chip, subs->sync_endpoint);
460 subs->sync_endpoint = NULL;
477 struct snd_usb_substream *subs = substream->runtime->private_data;
478 struct snd_usb_audio *chip = subs->stream->chip;
484 ret = snd_media_start_pipeline(subs);
488 fixed_rate = snd_usb_pcm_has_fixed_rate(subs);
489 fmt = find_substream_format(subs, hw_params);
523 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
527 if (subs->data_endpoint) {
528 if (snd_usb_endpoint_compatible(chip, subs->data_endpoint,
531 if (stop_endpoints(subs, false))
532 sync_pending_stops(subs);
533 close_endpoints(chip, subs);
536 subs->data_endpoint = snd_usb_endpoint_open(chip, fmt, hw_params, false, fixed_rate);
537 if (!subs->data_endpoint) {
543 subs->sync_endpoint = snd_usb_endpoint_open(chip, sync_fmt,
547 if (!subs->sync_endpoint) {
552 snd_usb_endpoint_set_sync(chip, subs->data_endpoint,
553 subs->sync_endpoint);
557 subs->cur_audiofmt = fmt;
560 if (!subs->data_endpoint->need_setup)
563 if (subs->sync_endpoint) {
564 ret = snd_usb_endpoint_set_params(chip, subs->sync_endpoint);
569 ret = snd_usb_endpoint_set_params(chip, subs->data_endpoint);
573 close_endpoints(chip, subs);
578 snd_media_stop_pipeline(subs);
590 struct snd_usb_substream *subs = substream->runtime->private_data;
591 struct snd_usb_audio *chip = subs->stream->chip;
593 snd_media_stop_pipeline(subs);
595 subs->cur_audiofmt = NULL;
598 if (stop_endpoints(subs, false))
599 sync_pending_stops(subs);
600 close_endpoints(chip, subs);
615 struct snd_usb_substream *subs)
617 struct snd_usb_audio *chip = subs->stream->chip;
619 if (subs->direction == SNDRV_PCM_STREAM_CAPTURE)
627 if (snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint))
640 struct snd_usb_substream *subs = runtime->private_data;
641 struct snd_usb_audio *chip = subs->stream->chip;
648 if (snd_BUG_ON(!subs->data_endpoint)) {
653 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
658 if (subs->sync_endpoint) {
659 ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint);
664 ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint);
668 snd_usb_set_format_quirk(subs, subs->cur_audiofmt);
672 subs->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
673 subs->inflight_bytes = 0;
674 subs->hwptr_done = 0;
675 subs->transfer_done = 0;
676 subs->last_frame_number = 0;
677 subs->period_elapsed_pending = 0;
680 subs->lowlatency_playback = lowlatency_playback_available(runtime, subs);
682 !subs->lowlatency_playback) {
683 ret = start_endpoints(subs);
688 sync_pending_stops(subs);
724 static int hw_check_valid_format(struct snd_usb_substream *subs,
759 if (subs->speed != USB_SPEED_FULL) {
817 struct snd_usb_substream *subs = rule->private;
818 struct snd_usb_audio *chip = subs->stream->chip;
828 list_for_each_entry(fp, &subs->fmt_list, list) {
829 if (!hw_check_valid_format(subs, params, fp))
833 subs->data_endpoint);
844 subs->sync_endpoint);
883 struct snd_usb_substream *subs = rule->private;
891 list_for_each_entry(fp, &subs->fmt_list, list) {
892 if (!hw_check_valid_format(subs, params, fp))
922 struct snd_usb_substream *subs = rule->private;
923 struct snd_usb_audio *chip = subs->stream->chip;
931 list_for_each_entry(fp, &subs->fmt_list, list) {
932 if (!hw_check_valid_format(subs, params, fp))
936 subs->data_endpoint);
946 subs->sync_endpoint);
963 struct snd_usb_substream *subs = rule->private;
972 list_for_each_entry(fp, &subs->fmt_list, list) {
973 if (!hw_check_valid_format(subs, params, fp))
991 struct snd_usb_substream *subs = rule->private;
992 struct snd_usb_audio *chip = subs->stream->chip;
1002 list_for_each_entry(fp, &subs->fmt_list, list) {
1003 if (!hw_check_valid_format(subs, params, fp))
1006 subs->data_endpoint);
1017 subs->sync_endpoint);
1036 struct snd_usb_substream *subs = rule->private;
1037 struct snd_usb_audio *chip = subs->stream->chip;
1047 list_for_each_entry(fp, &subs->fmt_list, list) {
1048 if (!hw_check_valid_format(subs, params, fp))
1051 subs->data_endpoint);
1062 subs->sync_endpoint);
1082 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1089 runtime->hw.formats = subs->formats;
1098 list_for_each_entry(fp, &subs->fmt_list, list) {
1118 if (subs->speed == USB_SPEED_FULL)
1132 hw_rule_rate, subs,
1142 hw_rule_channels, subs,
1151 hw_rule_format, subs,
1162 hw_rule_period_time, subs,
1185 hw_rule_period_size_implicit_fb, subs,
1190 hw_rule_periods_implicit_fb, subs,
1195 list_for_each_entry(fp, &subs->fmt_list, list) {
1210 struct snd_usb_substream *subs = &as->substream[direction];
1218 runtime->private_data = subs;
1219 subs->pcm_substream = substream;
1223 subs->dsd_dop.byte_idx = 0;
1224 subs->dsd_dop.channel = 0;
1225 subs->dsd_dop.marker = 1;
1227 ret = setup_hw_info(runtime, subs);
1230 ret = snd_usb_autoresume(subs->stream->chip);
1233 ret = snd_media_stream_init(subs, as->pcm, direction);
1235 snd_usb_autosuspend(subs->stream->chip);
1243 struct snd_usb_substream *subs = &as->substream[direction];
1246 snd_media_stop_pipeline(subs);
1248 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
1249 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
1250 snd_usb_unlock_shutdown(subs->stream->chip);
1255 subs->pcm_substream = NULL;
1256 snd_usb_autosuspend(subs->stream->chip);
1266 static void retire_capture_urb(struct snd_usb_substream *subs,
1269 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1277 current_frame_number = usb_get_current_frame_number(subs->dev);
1282 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1284 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1289 if (subs->stream_offset_adj > 0) {
1290 unsigned int adj = min(subs->stream_offset_adj, bytes);
1293 subs->stream_offset_adj -= adj;
1296 if (!subs->txfr_quirk)
1301 dev_warn_ratelimited(&subs->dev->dev,
1306 spin_lock_irqsave(&subs->lock, flags);
1307 oldptr = subs->hwptr_done;
1308 subs->hwptr_done += bytes;
1309 if (subs->hwptr_done >= subs->buffer_bytes)
1310 subs->hwptr_done -= subs->buffer_bytes;
1312 subs->transfer_done += frames;
1313 if (subs->transfer_done >= runtime->period_size) {
1314 subs->transfer_done -= runtime->period_size;
1319 subs->last_frame_number = current_frame_number;
1321 spin_unlock_irqrestore(&subs->lock, flags);
1323 if (oldptr + bytes > subs->buffer_bytes) {
1324 unsigned int bytes1 = subs->buffer_bytes - oldptr;
1334 snd_pcm_period_elapsed(subs->pcm_substream);
1337 static void urb_ctx_queue_advance(struct snd_usb_substream *subs,
1343 subs->inflight_bytes += bytes;
1344 subs->hwptr_done += bytes;
1345 if (subs->hwptr_done >= subs->buffer_bytes)
1346 subs->hwptr_done -= subs->buffer_bytes;
1349 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1352 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1354 unsigned int src_idx = subs->hwptr_done;
1355 unsigned int wrap = subs->buffer_bytes;
1378 if (++subs->dsd_dop.byte_idx == 3) {
1380 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1382 subs->dsd_dop.byte_idx = 0;
1384 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1386 subs->dsd_dop.marker++;
1387 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1388 subs->dsd_dop.channel = 0;
1392 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1394 if (subs->cur_audiofmt->dsd_bitrev)
1402 urb_ctx_queue_advance(subs, urb, queued);
1406 static void fill_playback_urb_dsd_bitrev(struct snd_usb_substream *subs,
1409 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1412 int i, ofs = subs->hwptr_done;
1416 if (++ofs >= subs->buffer_bytes)
1420 urb_ctx_queue_advance(subs, urb, bytes);
1423 static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1426 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1428 if (subs->hwptr_done + bytes > subs->buffer_bytes) {
1430 unsigned int bytes1 = subs->buffer_bytes - subs->hwptr_done;
1433 runtime->dma_area + subs->hwptr_done, bytes1);
1438 runtime->dma_area + subs->hwptr_done, bytes);
1441 urb_ctx_queue_advance(subs, urb, bytes);
1444 static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1462 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1470 static int prepare_playback_urb(struct snd_usb_substream *subs,
1474 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1475 struct snd_usb_endpoint *ep = subs->data_endpoint;
1490 spin_lock_irqsave(&subs->lock, flags);
1491 frame_limit = subs->frame_limit + ep->max_urb_frames;
1492 transfer_done = subs->transfer_done;
1494 if (subs->lowlatency_playback &&
1496 unsigned int hwptr = subs->hwptr_done / stride;
1521 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1554 subs->transfer_done = transfer_done;
1555 subs->frame_limit = frame_limit;
1557 subs->cur_audiofmt->dsd_dop)) {
1558 fill_playback_urb_dsd_dop(subs, urb, bytes);
1560 subs->cur_audiofmt->dsd_bitrev)) {
1561 fill_playback_urb_dsd_bitrev(subs, urb, bytes);
1564 if (!subs->tx_length_quirk)
1565 copy_to_urb(subs, urb, 0, stride, bytes);
1567 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1571 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1573 if (subs->trigger_tstamp_pending_update) {
1578 subs->trigger_tstamp_pending_update = false;
1581 if (period_elapsed && !subs->running && subs->lowlatency_playback) {
1582 subs->period_elapsed_pending = 1;
1587 spin_unlock_irqrestore(&subs->lock, flags);
1593 snd_pcm_period_elapsed_under_stream_lock(subs->pcm_substream);
1595 snd_pcm_period_elapsed(subs->pcm_substream);
1604 static void retire_playback_urb(struct snd_usb_substream *subs,
1611 spin_lock_irqsave(&subs->lock, flags);
1613 if (subs->inflight_bytes >= ctx->queued)
1614 subs->inflight_bytes -= ctx->queued;
1616 subs->inflight_bytes = 0;
1619 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1620 if (subs->running) {
1621 period_elapsed = subs->period_elapsed_pending;
1622 subs->period_elapsed_pending = 0;
1624 spin_unlock_irqrestore(&subs->lock, flags);
1626 snd_pcm_period_elapsed(subs->pcm_substream);
1634 struct snd_usb_substream *subs = substream->runtime->private_data;
1637 if (!subs->lowlatency_playback || !subs->running)
1639 ep = subs->data_endpoint;
1653 struct snd_usb_substream *subs = substream->runtime->private_data;
1658 subs->trigger_tstamp_pending_update = true;
1661 snd_usb_endpoint_set_callback(subs->data_endpoint,
1664 subs);
1665 if (subs->lowlatency_playback &&
1668 subs->lowlatency_playback = false;
1669 err = start_endpoints(subs);
1671 snd_usb_endpoint_set_callback(subs->data_endpoint,
1676 subs->running = 1;
1677 dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n",
1678 subs->cur_audiofmt->iface,
1679 subs->cur_audiofmt->altsetting);
1683 stop_endpoints(subs, substream->runtime->state == SNDRV_PCM_STATE_DRAINING);
1684 snd_usb_endpoint_set_callback(subs->data_endpoint,
1686 subs->running = 0;
1687 dev_dbg(&subs->dev->dev, "%d:%d Stop Playback PCM\n",
1688 subs->cur_audiofmt->iface,
1689 subs->cur_audiofmt->altsetting);
1693 snd_usb_endpoint_set_callback(subs->data_endpoint,
1696 subs);
1697 subs->running = 0;
1698 dev_dbg(&subs->dev->dev, "%d:%d Pause Playback PCM\n",
1699 subs->cur_audiofmt->iface,
1700 subs->cur_audiofmt->altsetting);
1711 struct snd_usb_substream *subs = substream->runtime->private_data;
1715 err = start_endpoints(subs);
1720 snd_usb_endpoint_set_callback(subs->data_endpoint,
1722 subs);
1723 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1724 subs->running = 1;
1725 dev_dbg(&subs->dev->dev, "%d:%d Start Capture PCM\n",
1726 subs->cur_audiofmt->iface,
1727 subs->cur_audiofmt->altsetting);
1731 stop_endpoints(subs, false);
1734 snd_usb_endpoint_set_callback(subs->data_endpoint,
1736 subs->running = 0;
1737 dev_dbg(&subs->dev->dev, "%d:%d Stop Capture PCM\n",
1738 subs->cur_audiofmt->iface,
1739 subs->cur_audiofmt->altsetting);
1778 void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1780 struct snd_pcm *pcm = subs->stream->pcm;
1781 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1782 struct device *dev = subs->dev->bus->sysdev;