Lines Matching refs:subs
32 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
39 if (!subs->last_delay)
42 current_frame_number = usb_get_current_frame_number(subs->dev);
48 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
53 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
54 est_delay = subs->last_delay - est_delay;
56 est_delay = subs->last_delay + est_delay;
68 struct snd_usb_substream *subs = substream->runtime->private_data;
71 if (atomic_read(&subs->stream->chip->shutdown))
73 spin_lock(&subs->lock);
74 hwptr_done = subs->hwptr_done;
75 substream->runtime->delay = snd_usb_pcm_delay(subs,
77 spin_unlock(&subs->lock);
84 static struct audioformat *find_format(struct snd_usb_substream *subs)
90 list_for_each_entry(fp, &subs->fmt_list, list) {
91 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
93 if (fp->channels != subs->channels)
95 if (subs->cur_rate < fp->rate_min ||
96 subs->cur_rate > fp->rate_max)
101 if (fp->rate_table[i] == subs->cur_rate)
119 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
121 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
124 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
126 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
211 static int start_endpoints(struct snd_usb_substream *subs)
215 if (!subs->data_endpoint)
218 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
219 struct snd_usb_endpoint *ep = subs->data_endpoint;
221 dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);
223 ep->data_subs = subs;
226 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
231 if (subs->sync_endpoint &&
232 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
233 struct snd_usb_endpoint *ep = subs->sync_endpoint;
235 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
236 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
237 err = usb_set_interface(subs->dev,
238 subs->sync_endpoint->iface,
239 subs->sync_endpoint->altsetting);
241 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
242 dev_err(&subs->dev->dev,
244 subs->sync_endpoint->iface,
245 subs->sync_endpoint->altsetting, err);
250 dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);
252 ep->sync_slave = subs->data_endpoint;
255 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
263 static void sync_pending_stops(struct snd_usb_substream *subs)
265 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
266 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
269 static void stop_endpoints(struct snd_usb_substream *subs)
271 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
272 snd_usb_endpoint_stop(subs->sync_endpoint);
274 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
275 snd_usb_endpoint_stop(subs->data_endpoint);
281 struct snd_usb_substream *subs = substream->runtime->private_data;
283 sync_pending_stops(subs);
319 static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
330 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
333 switch (subs->stream->chip->usb_id) {
394 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
413 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
414 alts, ep, !subs->direction,
416 if (!subs->sync_endpoint)
419 subs->sync_endpoint->is_implicit_feedback = 1;
421 subs->data_endpoint->sync_master = subs->sync_endpoint;
426 static int set_sync_endpoint(struct snd_usb_substream *subs,
432 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
454 subs->sync_endpoint = NULL;
455 subs->data_endpoint->sync_master = NULL;
458 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
514 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
515 alts, ep, !subs->direction,
520 if (!subs->sync_endpoint) {
526 subs->sync_endpoint->is_implicit_feedback = implicit_fb;
528 subs->data_endpoint->sync_master = subs->sync_endpoint;
536 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
538 struct usb_device *dev = subs->dev;
552 if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
556 if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
557 if (!subs->stream->chip->keep_iface) {
558 err = usb_set_interface(subs->dev, subs->interface, 0);
566 subs->interface = -1;
567 subs->altset_idx = 0;
570 if (subs->need_setup_fmt)
571 subs->need_setup_fmt = false;
575 err = snd_usb_select_mode_quirk(subs, fmt);
591 subs->interface = fmt->iface;
592 subs->altset_idx = fmt->altset_idx;
593 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
594 alts, fmt->endpoint, subs->direction,
597 if (!subs->data_endpoint)
600 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
604 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
608 subs->cur_audiofmt = fmt;
610 snd_usb_set_format_quirk(subs, fmt);
622 static int match_endpoint_audioformats(struct snd_usb_substream *subs,
631 dev_dbg(&subs->dev->dev,
637 dev_dbg(&subs->dev->dev,
650 dev_dbg(&subs->dev->dev,
659 dev_dbg(&subs->dev->dev,
668 static int configure_sync_endpoint(struct snd_usb_substream *subs)
674 int sync_period_bytes = subs->period_bytes;
676 &subs->stream->substream[subs->direction ^ 1];
678 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
679 !subs->stream)
680 return snd_usb_endpoint_set_params(subs->sync_endpoint,
681 subs->pcm_format,
682 subs->channels,
683 subs->period_bytes,
685 subs->cur_rate,
686 subs->cur_audiofmt,
691 int score = match_endpoint_audioformats(subs,
692 fp, subs->cur_audiofmt,
693 subs->cur_rate, subs->pcm_format);
702 dev_err(&subs->dev->dev,
712 if (sync_fp->channels != subs->channels) {
713 sync_period_bytes = (subs->period_bytes / subs->channels) *
715 dev_dbg(&subs->dev->dev,
717 __func__, subs->period_bytes, sync_period_bytes);
720 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
721 subs->pcm_format,
725 subs->cur_rate,
737 static int configure_endpoint(struct snd_usb_substream *subs)
742 stop_endpoints(subs);
743 sync_pending_stops(subs);
744 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
745 subs->pcm_format,
746 subs->channels,
747 subs->period_bytes,
748 subs->period_frames,
749 subs->buffer_periods,
750 subs->cur_rate,
751 subs->cur_audiofmt,
752 subs->sync_endpoint);
756 if (subs->sync_endpoint)
757 ret = configure_sync_endpoint(subs);
762 static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
766 if (!subs->str_pd)
769 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
771 dev_err(&subs->dev->dev,
773 subs->str_pd->pd_id, state, ret);
823 struct snd_usb_substream *subs = substream->runtime->private_data;
827 ret = snd_media_start_pipeline(subs);
831 subs->pcm_format = params_format(hw_params);
832 subs->period_bytes = params_period_bytes(hw_params);
833 subs->period_frames = params_period_size(hw_params);
834 subs->buffer_periods = params_periods(hw_params);
835 subs->channels = params_channels(hw_params);
836 subs->cur_rate = params_rate(hw_params);
838 fmt = find_format(subs);
840 dev_dbg(&subs->dev->dev,
842 subs->pcm_format, subs->cur_rate, subs->channels);
847 ret = snd_usb_lock_shutdown(subs->stream->chip);
851 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
855 ret = set_format(subs, fmt);
859 subs->interface = fmt->iface;
860 subs->altset_idx = fmt->altset_idx;
861 subs->need_setup_ep = true;
864 snd_usb_unlock_shutdown(subs->stream->chip);
870 snd_media_stop_pipeline(subs);
881 struct snd_usb_substream *subs = substream->runtime->private_data;
883 snd_media_stop_pipeline(subs);
884 subs->cur_audiofmt = NULL;
885 subs->cur_rate = 0;
886 subs->period_bytes = 0;
887 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
888 stop_endpoints(subs);
889 sync_pending_stops(subs);
890 snd_usb_endpoint_deactivate(subs->sync_endpoint);
891 snd_usb_endpoint_deactivate(subs->data_endpoint);
892 snd_usb_unlock_shutdown(subs->stream->chip);
906 struct snd_usb_substream *subs = runtime->private_data;
911 if (! subs->cur_audiofmt) {
912 dev_err(&subs->dev->dev, "no format is specified!\n");
916 ret = snd_usb_lock_shutdown(subs->stream->chip);
919 if (snd_BUG_ON(!subs->data_endpoint)) {
924 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
928 ret = set_format(subs, subs->cur_audiofmt);
932 if (subs->need_setup_ep) {
934 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
935 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
936 ret = snd_usb_init_sample_rate(subs->stream->chip,
937 subs->cur_audiofmt->iface,
939 subs->cur_audiofmt,
940 subs->cur_rate);
944 ret = configure_endpoint(subs);
947 subs->need_setup_ep = false;
951 subs->data_endpoint->maxframesize =
952 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
953 subs->data_endpoint->curframesize =
954 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
957 subs->hwptr_done = 0;
958 subs->transfer_done = 0;
959 subs->last_delay = 0;
960 subs->last_frame_number = 0;
965 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
966 ret = start_endpoints(subs);
969 snd_usb_unlock_shutdown(subs->stream->chip);
988 static int hw_check_valid_format(struct snd_usb_substream *subs,
1023 if (subs->speed != USB_SPEED_FULL) {
1036 struct snd_usb_substream *subs = rule->private;
1045 list_for_each_entry(fp, &subs->fmt_list, list) {
1046 if (!hw_check_valid_format(subs, params, fp))
1088 struct snd_usb_substream *subs = rule->private;
1097 list_for_each_entry(fp, &subs->fmt_list, list) {
1098 if (!hw_check_valid_format(subs, params, fp))
1139 struct snd_usb_substream *subs = rule->private;
1148 list_for_each_entry(fp, &subs->fmt_list, list) {
1149 if (!hw_check_valid_format(subs, params, fp))
1170 struct snd_usb_substream *subs = rule->private;
1180 list_for_each_entry(fp, &subs->fmt_list, list) {
1181 if (!hw_check_valid_format(subs, params, fp))
1209 struct snd_usb_substream *subs)
1216 kfree(subs->rate_list.list);
1217 subs->rate_list.list = NULL;
1219 list_for_each_entry(fp, &subs->fmt_list, list) {
1229 subs->rate_list.list = rate_list =
1231 if (!subs->rate_list.list)
1233 subs->rate_list.count = count;
1234 subs->rate_list.mask = 0;
1236 list_for_each_entry(fp, &subs->fmt_list, list) {
1242 &subs->rate_list);
1254 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1261 runtime->hw.formats = subs->formats;
1270 list_for_each_entry(fp, &subs->fmt_list, list) {
1290 if (subs->speed == USB_SPEED_FULL)
1304 hw_rule_rate, subs,
1312 hw_rule_channels, subs,
1320 hw_rule_format, subs,
1330 hw_rule_period_time, subs,
1338 err = snd_usb_pcm_check_knot(runtime, subs);
1342 return snd_usb_autoresume(subs->stream->chip);
1350 struct snd_usb_substream *subs = &as->substream[direction];
1353 subs->interface = -1;
1354 subs->altset_idx = 0;
1356 runtime->private_data = subs;
1357 subs->pcm_substream = substream;
1361 subs->dsd_dop.byte_idx = 0;
1362 subs->dsd_dop.channel = 0;
1363 subs->dsd_dop.marker = 1;
1365 ret = setup_hw_info(runtime, subs);
1367 ret = snd_media_stream_init(subs, as->pcm, direction);
1369 snd_usb_autosuspend(subs->stream->chip);
1378 struct snd_usb_substream *subs = &as->substream[direction];
1381 snd_media_stop_pipeline(subs);
1384 subs->interface >= 0 &&
1385 !snd_usb_lock_shutdown(subs->stream->chip)) {
1386 usb_set_interface(subs->dev, subs->interface, 0);
1387 subs->interface = -1;
1388 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
1389 snd_usb_unlock_shutdown(subs->stream->chip);
1394 subs->pcm_substream = NULL;
1395 snd_usb_autosuspend(subs->stream->chip);
1405 static void retire_capture_urb(struct snd_usb_substream *subs,
1408 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1416 current_frame_number = usb_get_current_frame_number(subs->dev);
1421 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1423 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1428 if (subs->stream_offset_adj > 0) {
1429 unsigned int adj = min(subs->stream_offset_adj, bytes);
1432 subs->stream_offset_adj -= adj;
1435 if (!subs->txfr_quirk)
1440 dev_warn_ratelimited(&subs->dev->dev,
1445 spin_lock_irqsave(&subs->lock, flags);
1446 oldptr = subs->hwptr_done;
1447 subs->hwptr_done += bytes;
1448 if (subs->hwptr_done >= runtime->buffer_size * stride)
1449 subs->hwptr_done -= runtime->buffer_size * stride;
1451 subs->transfer_done += frames;
1452 if (subs->transfer_done >= runtime->period_size) {
1453 subs->transfer_done -= runtime->period_size;
1459 runtime->delay = subs->last_delay = 0;
1462 subs->last_frame_number = current_frame_number;
1463 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1465 spin_unlock_irqrestore(&subs->lock, flags);
1478 snd_pcm_period_elapsed(subs->pcm_substream);
1481 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1484 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1487 unsigned int src_idx = subs->hwptr_done;
1510 if (++subs->dsd_dop.byte_idx == 3) {
1512 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1514 subs->dsd_dop.byte_idx = 0;
1516 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1518 subs->dsd_dop.marker++;
1519 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1520 subs->dsd_dop.channel = 0;
1524 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1526 if (subs->cur_audiofmt->dsd_bitrev)
1531 subs->hwptr_done++;
1534 if (subs->hwptr_done >= runtime->buffer_size * stride)
1535 subs->hwptr_done -= runtime->buffer_size * stride;
1538 static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1541 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1543 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1546 runtime->buffer_size * stride - subs->hwptr_done;
1548 runtime->dma_area + subs->hwptr_done, bytes1);
1553 runtime->dma_area + subs->hwptr_done, bytes);
1555 subs->hwptr_done += bytes;
1556 if (subs->hwptr_done >= runtime->buffer_size * stride)
1557 subs->hwptr_done -= runtime->buffer_size * stride;
1560 static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1578 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1586 static void prepare_playback_urb(struct snd_usb_substream *subs,
1589 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1590 struct snd_usb_endpoint *ep = subs->data_endpoint;
1600 spin_lock_irqsave(&subs->lock, flags);
1601 subs->frame_limit += ep->max_urb_frames;
1615 subs->transfer_done += counts;
1616 if (subs->transfer_done >= runtime->period_size) {
1617 subs->transfer_done -= runtime->period_size;
1618 subs->frame_limit = 0;
1620 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1621 if (subs->transfer_done > 0) {
1624 frames -= subs->transfer_done;
1625 counts -= subs->transfer_done;
1628 subs->transfer_done = 0;
1643 subs->transfer_done >= subs->frame_limit) &&
1649 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1650 subs->cur_audiofmt->dsd_dop)) {
1651 fill_playback_urb_dsd_dop(subs, urb, bytes);
1652 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1653 subs->cur_audiofmt->dsd_bitrev)) {
1657 int idx = (subs->hwptr_done + i)
1662 subs->hwptr_done += bytes;
1663 if (subs->hwptr_done >= runtime->buffer_size * stride)
1664 subs->hwptr_done -= runtime->buffer_size * stride;
1667 if (!subs->tx_length_quirk)
1668 copy_to_urb(subs, urb, 0, stride, bytes);
1670 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1675 runtime->delay = subs->last_delay;
1677 subs->last_delay = runtime->delay;
1680 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1681 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1683 if (subs->trigger_tstamp_pending_update) {
1688 subs->trigger_tstamp_pending_update = false;
1691 spin_unlock_irqrestore(&subs->lock, flags);
1694 snd_pcm_period_elapsed(subs->pcm_substream);
1701 static void retire_playback_urb(struct snd_usb_substream *subs,
1705 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1706 struct snd_usb_endpoint *ep = subs->data_endpoint;
1716 spin_lock_irqsave(&subs->lock, flags);
1717 if (!subs->last_delay)
1720 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1722 if (processed > subs->last_delay)
1723 subs->last_delay = 0;
1725 subs->last_delay -= processed;
1726 runtime->delay = subs->last_delay;
1733 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1734 dev_dbg_ratelimited(&subs->dev->dev,
1736 est_delay, subs->last_delay);
1738 if (!subs->running) {
1742 subs->last_frame_number =
1743 usb_get_current_frame_number(subs->dev) & 0xff;
1747 spin_unlock_irqrestore(&subs->lock, flags);
1753 struct snd_usb_substream *subs = substream->runtime->private_data;
1757 subs->trigger_tstamp_pending_update = true;
1760 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1761 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1762 subs->running = 1;
1765 stop_endpoints(subs);
1766 subs->running = 0;
1769 subs->data_endpoint->prepare_data_urb = NULL;
1771 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1772 subs->running = 0;
1775 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
1776 stop_endpoints(subs);
1777 subs->need_setup_fmt = true;
1790 struct snd_usb_substream *subs = substream->runtime->private_data;
1794 err = start_endpoints(subs);
1798 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1799 subs->running = 1;
1802 stop_endpoints(subs);
1803 subs->data_endpoint->retire_data_urb = NULL;
1804 subs->running = 0;
1807 subs->data_endpoint->retire_data_urb = NULL;
1808 subs->running = 0;
1811 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1812 subs->running = 1;
1815 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
1816 stop_endpoints(subs);
1817 subs->need_setup_fmt = true;
1857 void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1859 struct snd_pcm *pcm = subs->stream->pcm;
1860 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1861 struct device *dev = subs->dev->bus->sysdev;