Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 2 | /* |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | #include <linux/init.h> |
Stephen Rothwell | 9966dda | 2010-03-29 19:01:48 +1100 | [diff] [blame] | 6 | #include <linux/slab.h> |
Daniel Mack | 44dcbbb | 2013-04-17 00:01:39 +0800 | [diff] [blame] | 7 | #include <linux/bitrev.h> |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 8 | #include <linux/ratelimit.h> |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 9 | #include <linux/usb.h> |
| 10 | #include <linux/usb/audio.h> |
Daniel Mack | 7e84789 | 2010-03-11 21:13:20 +0100 | [diff] [blame] | 11 | #include <linux/usb/audio-v2.h> |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 12 | |
| 13 | #include <sound/core.h> |
| 14 | #include <sound/pcm.h> |
| 15 | #include <sound/pcm_params.h> |
| 16 | |
| 17 | #include "usbaudio.h" |
| 18 | #include "card.h" |
| 19 | #include "quirks.h" |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 20 | #include "endpoint.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 21 | #include "helper.h" |
| 22 | #include "pcm.h" |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 23 | #include "clock.h" |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 24 | #include "power.h" |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 25 | #include "media.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 26 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 27 | #define SUBSTREAM_FLAG_DATA_EP_STARTED 0 |
| 28 | #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1 |
| 29 | |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 30 | /* return the estimated delay based on USB frame counters */ |
| 31 | snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs, |
| 32 | unsigned int rate) |
| 33 | { |
| 34 | int current_frame_number; |
| 35 | int frame_diff; |
| 36 | int est_delay; |
| 37 | |
Takashi Iwai | 48779a0 | 2012-11-23 16:00:37 +0100 | [diff] [blame] | 38 | if (!subs->last_delay) |
| 39 | return 0; /* short path */ |
| 40 | |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 41 | current_frame_number = usb_get_current_frame_number(subs->dev); |
| 42 | /* |
| 43 | * HCD implementations use different widths, use lower 8 bits. |
| 44 | * The delay will be managed up to 256ms, which is more than |
| 45 | * enough |
| 46 | */ |
| 47 | frame_diff = (current_frame_number - subs->last_frame_number) & 0xff; |
| 48 | |
| 49 | /* Approximation based on number of samples per USB frame (ms), |
| 50 | some truncation for 44.1 but the estimate is good enough */ |
Pierre-Louis Bossart | e4cc615 | 2012-12-19 11:39:05 -0600 | [diff] [blame] | 51 | est_delay = frame_diff * rate / 1000; |
| 52 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) |
| 53 | est_delay = subs->last_delay - est_delay; |
| 54 | else |
| 55 | est_delay = subs->last_delay + est_delay; |
| 56 | |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 57 | if (est_delay < 0) |
| 58 | est_delay = 0; |
| 59 | return est_delay; |
| 60 | } |
| 61 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 62 | /* |
| 63 | * return the current pcm pointer. just based on the hwptr_done value. |
| 64 | */ |
| 65 | static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream) |
| 66 | { |
Takashi Iwai | e92be81 | 2018-05-27 15:09:15 +0200 | [diff] [blame] | 67 | struct snd_usb_substream *subs = substream->runtime->private_data; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 68 | unsigned int hwptr_done; |
| 69 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 70 | if (atomic_read(&subs->stream->chip->shutdown)) |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 71 | return SNDRV_PCM_POS_XRUN; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 72 | spin_lock(&subs->lock); |
| 73 | hwptr_done = subs->hwptr_done; |
Pierre-Louis Bossart | e4cc615 | 2012-12-19 11:39:05 -0600 | [diff] [blame] | 74 | substream->runtime->delay = snd_usb_pcm_delay(subs, |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 75 | substream->runtime->rate); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 76 | spin_unlock(&subs->lock); |
| 77 | return hwptr_done / (substream->runtime->frame_bits >> 3); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * find a matching audio format |
| 82 | */ |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 83 | static struct audioformat *find_format(struct list_head *fmt_list_head, |
| 84 | snd_pcm_format_t format, |
| 85 | unsigned int rate, |
| 86 | unsigned int channels, |
| 87 | struct snd_usb_substream *subs) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 88 | { |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 89 | struct audioformat *fp; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 90 | struct audioformat *found = NULL; |
| 91 | int cur_attr = 0, attr; |
| 92 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 93 | list_for_each_entry(fp, fmt_list_head, list) { |
| 94 | if (!(fp->formats & pcm_format_to_bits(format))) |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame] | 95 | continue; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 96 | if (fp->channels != channels) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 97 | continue; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 98 | if (rate < fp->rate_min || rate > fp->rate_max) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 99 | continue; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 100 | if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 101 | unsigned int i; |
| 102 | for (i = 0; i < fp->nr_rates; i++) |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 103 | if (fp->rate_table[i] == rate) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 104 | break; |
| 105 | if (i >= fp->nr_rates) |
| 106 | continue; |
| 107 | } |
| 108 | attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 109 | if (!found) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 110 | found = fp; |
| 111 | cur_attr = attr; |
| 112 | continue; |
| 113 | } |
| 114 | /* avoid async out and adaptive in if the other method |
| 115 | * supports the same format. |
| 116 | * this is a workaround for the case like |
| 117 | * M-audio audiophile USB. |
| 118 | */ |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 119 | if (subs && attr != cur_attr) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 120 | if ((attr == USB_ENDPOINT_SYNC_ASYNC && |
| 121 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || |
| 122 | (attr == USB_ENDPOINT_SYNC_ADAPTIVE && |
| 123 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) |
| 124 | continue; |
| 125 | if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC && |
| 126 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || |
| 127 | (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE && |
| 128 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) { |
| 129 | found = fp; |
| 130 | cur_attr = attr; |
| 131 | continue; |
| 132 | } |
| 133 | } |
| 134 | /* find the format with the largest max. packet size */ |
| 135 | if (fp->maxpacksize > found->maxpacksize) { |
| 136 | found = fp; |
| 137 | cur_attr = attr; |
| 138 | } |
| 139 | } |
| 140 | return found; |
| 141 | } |
| 142 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 143 | static struct audioformat *find_substream_format(struct snd_usb_substream *subs) |
| 144 | { |
| 145 | return find_format(&subs->fmt_list, subs->pcm_format, subs->cur_rate, |
| 146 | subs->channels, subs); |
| 147 | } |
| 148 | |
Takashi Iwai | 73037c8 | 2020-11-23 09:53:26 +0100 | [diff] [blame] | 149 | static int init_pitch_v1(struct snd_usb_audio *chip, |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 150 | struct audioformat *fmt) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 151 | { |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 152 | struct usb_device *dev = chip->dev; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 153 | unsigned int ep; |
| 154 | unsigned char data[1]; |
| 155 | int err; |
| 156 | |
Takashi Iwai | 73037c8 | 2020-11-23 09:53:26 +0100 | [diff] [blame] | 157 | ep = fmt->endpoint; |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 158 | |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 159 | data[0] = 1; |
Takashi Iwai | f25ecf8 | 2018-05-27 15:18:22 +0200 | [diff] [blame] | 160 | err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, |
| 161 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, |
| 162 | UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, |
| 163 | data, sizeof(data)); |
| 164 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 165 | usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n", |
Takashi Iwai | 73037c8 | 2020-11-23 09:53:26 +0100 | [diff] [blame] | 166 | fmt->iface, ep); |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 167 | return err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 168 | } |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 169 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Takashi Iwai | 73037c8 | 2020-11-23 09:53:26 +0100 | [diff] [blame] | 173 | static int init_pitch_v2(struct snd_usb_audio *chip, |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 174 | struct audioformat *fmt) |
| 175 | { |
| 176 | struct usb_device *dev = chip->dev; |
| 177 | unsigned char data[1]; |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 178 | int err; |
| 179 | |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 180 | data[0] = 1; |
Takashi Iwai | f25ecf8 | 2018-05-27 15:18:22 +0200 | [diff] [blame] | 181 | err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR, |
| 182 | USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, |
| 183 | UAC2_EP_CS_PITCH << 8, 0, |
| 184 | data, sizeof(data)); |
| 185 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 186 | usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n", |
Takashi Iwai | 73037c8 | 2020-11-23 09:53:26 +0100 | [diff] [blame] | 187 | fmt->iface, fmt->altsetting); |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 188 | return err; |
| 189 | } |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 194 | /* |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 195 | * initialize the pitch control and sample rate |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 196 | */ |
Takashi Iwai | 73037c8 | 2020-11-23 09:53:26 +0100 | [diff] [blame] | 197 | int snd_usb_init_pitch(struct snd_usb_audio *chip, |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 198 | struct audioformat *fmt) |
| 199 | { |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 200 | /* if endpoint doesn't have pitch control, bail out */ |
| 201 | if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL)) |
| 202 | return 0; |
| 203 | |
Clemens Ladisch | 8f898e9 | 2013-01-31 21:39:17 +0100 | [diff] [blame] | 204 | switch (fmt->protocol) { |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 205 | case UAC_VERSION_1: |
Clemens Ladisch | a2acad8 | 2010-09-03 10:53:11 +0200 | [diff] [blame] | 206 | default: |
Takashi Iwai | 73037c8 | 2020-11-23 09:53:26 +0100 | [diff] [blame] | 207 | return init_pitch_v1(chip, fmt); |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 208 | |
| 209 | case UAC_VERSION_2: |
Takashi Iwai | 73037c8 | 2020-11-23 09:53:26 +0100 | [diff] [blame] | 210 | return init_pitch_v2(chip, fmt); |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 211 | } |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Takashi Iwai | 57234bc | 2020-11-23 09:53:27 +0100 | [diff] [blame] | 214 | static void stop_endpoints(struct snd_usb_substream *subs) |
| 215 | { |
| 216 | if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { |
| 217 | snd_usb_endpoint_stop(subs->sync_endpoint); |
| 218 | subs->sync_endpoint->sync_slave = NULL; |
| 219 | } |
| 220 | |
| 221 | if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) |
| 222 | snd_usb_endpoint_stop(subs->data_endpoint); |
| 223 | } |
| 224 | |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 225 | static int start_endpoints(struct snd_usb_substream *subs) |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 226 | { |
| 227 | int err; |
| 228 | |
| 229 | if (!subs->data_endpoint) |
| 230 | return -EINVAL; |
| 231 | |
| 232 | if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) { |
| 233 | struct snd_usb_endpoint *ep = subs->data_endpoint; |
| 234 | |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 235 | err = snd_usb_endpoint_start(ep); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 236 | if (err < 0) { |
| 237 | clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags); |
Takashi Iwai | 57234bc | 2020-11-23 09:53:27 +0100 | [diff] [blame] | 238 | goto error; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | if (subs->sync_endpoint && |
| 243 | !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { |
| 244 | struct snd_usb_endpoint *ep = subs->sync_endpoint; |
| 245 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 246 | ep->sync_slave = subs->data_endpoint; |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 247 | err = snd_usb_endpoint_start(ep); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 248 | if (err < 0) { |
| 249 | clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags); |
Takashi Iwai | 1803503 | 2020-11-23 09:53:12 +0100 | [diff] [blame] | 250 | ep->sync_slave = NULL; |
Takashi Iwai | 57234bc | 2020-11-23 09:53:27 +0100 | [diff] [blame] | 251 | goto error; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | return 0; |
Takashi Iwai | 57234bc | 2020-11-23 09:53:27 +0100 | [diff] [blame] | 256 | |
| 257 | error: |
| 258 | stop_endpoints(subs); |
| 259 | return err; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 262 | static void sync_pending_stops(struct snd_usb_substream *subs) |
| 263 | { |
| 264 | snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint); |
| 265 | snd_usb_endpoint_sync_pending_stop(subs->data_endpoint); |
| 266 | } |
| 267 | |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 268 | /* PCM sync_stop callback */ |
| 269 | static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream) |
| 270 | { |
| 271 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 272 | |
| 273 | if (!snd_usb_lock_shutdown(subs->stream->chip)) { |
| 274 | sync_pending_stops(subs); |
| 275 | snd_usb_unlock_shutdown(subs->stream->chip); |
Takashi Iwai | b2eb950 | 2012-11-21 08:30:48 +0100 | [diff] [blame] | 276 | } |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 277 | return 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 280 | /* Check whether the given iface:altsetting points to an implicit fb source */ |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 281 | static bool search_generic_implicit_fb(struct snd_usb_audio *chip, int ifnum, |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 282 | unsigned int altsetting, |
| 283 | struct usb_host_interface **altsp, |
| 284 | unsigned int *ep) |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 285 | { |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 286 | struct usb_host_interface *alts; |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 287 | struct usb_interface_descriptor *altsd; |
| 288 | struct usb_endpoint_descriptor *epd; |
| 289 | |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 290 | alts = snd_usb_get_host_interface(chip, ifnum, altsetting); |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 291 | if (!alts) |
| 292 | return false; |
| 293 | altsd = get_iface_desc(alts); |
| 294 | if (altsd->bInterfaceClass != USB_CLASS_AUDIO || |
| 295 | altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING || |
| 296 | altsd->bInterfaceProtocol != UAC_VERSION_2 || |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 297 | altsd->bNumEndpoints < 1) |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 298 | return false; |
| 299 | epd = get_endpoint(alts, 0); |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 300 | if (!usb_endpoint_is_isoc_in(epd) || |
| 301 | (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) != |
| 302 | USB_ENDPOINT_USAGE_IMPLICIT_FB) |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 303 | return false; |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 304 | *ep = epd->bEndpointAddress; |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 305 | *altsp = alts; |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | /* Like the function above, but specific to Roland with vendor class and hack */ |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 310 | static bool search_roland_implicit_fb(struct snd_usb_audio *chip, int ifnum, |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 311 | unsigned int altsetting, |
| 312 | struct usb_host_interface **altsp, |
| 313 | unsigned int *ep) |
| 314 | { |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 315 | struct usb_host_interface *alts; |
| 316 | struct usb_interface_descriptor *altsd; |
| 317 | struct usb_endpoint_descriptor *epd; |
| 318 | |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 319 | alts = snd_usb_get_host_interface(chip, ifnum, altsetting); |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 320 | if (!alts) |
| 321 | return false; |
| 322 | altsd = get_iface_desc(alts); |
| 323 | if (altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC || |
| 324 | (altsd->bInterfaceSubClass != 2 && |
| 325 | altsd->bInterfaceProtocol != 2) || |
| 326 | altsd->bNumEndpoints < 1) |
| 327 | return false; |
| 328 | epd = get_endpoint(alts, 0); |
| 329 | if (!usb_endpoint_is_isoc_in(epd) || |
| 330 | (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) != |
| 331 | USB_ENDPOINT_USAGE_IMPLICIT_FB) |
| 332 | return false; |
| 333 | *ep = epd->bEndpointAddress; |
| 334 | *altsp = alts; |
| 335 | return true; |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Manuel Reinhardt | 2bc16b9 | 2019-01-31 15:32:35 +0100 | [diff] [blame] | 338 | /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk |
| 339 | * applies. Returns 1 if a quirk was found. |
| 340 | */ |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 341 | static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip, |
| 342 | struct audioformat *fmt, |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 343 | struct usb_host_interface *alts) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 344 | { |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 345 | struct usb_device *dev = chip->dev; |
| 346 | struct usb_interface_descriptor *altsd = get_iface_desc(alts); |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 347 | struct usb_interface *iface; |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 348 | unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 349 | unsigned int ep; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 350 | unsigned int ifnum; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 351 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 352 | switch (chip->usb_id) { |
Eldad Zack | ca10a7e | 2012-11-28 23:55:41 +0100 | [diff] [blame] | 353 | case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */ |
Matt Gruskin | e9a25e0 | 2013-02-09 12:56:35 -0500 | [diff] [blame] | 354 | case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */ |
Geoffrey D. Bennett | 0938eca | 2020-11-04 22:27:17 +1030 | [diff] [blame] | 355 | case USB_ID(0x22f0, 0x0006): /* Allen&Heath Qu-16 */ |
Eldad Zack | 914273c | 2013-08-03 10:50:21 +0200 | [diff] [blame] | 356 | ep = 0x81; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 357 | ifnum = 3; |
| 358 | goto add_sync_ep_from_ifnum; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 359 | case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */ |
| 360 | case USB_ID(0x0763, 0x2081): |
Eldad Zack | 914273c | 2013-08-03 10:50:21 +0200 | [diff] [blame] | 361 | ep = 0x81; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 362 | ifnum = 2; |
| 363 | goto add_sync_ep_from_ifnum; |
| 364 | case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */ |
Geoffrey D. Bennett | 26201dd | 2020-11-04 22:37:05 +1030 | [diff] [blame] | 365 | case USB_ID(0x0499, 0x172a): /* Yamaha MODX */ |
Alberto Aguirre | 17f08b0 | 2016-12-08 00:36:48 -0600 | [diff] [blame] | 366 | ep = 0x86; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 367 | ifnum = 2; |
| 368 | goto add_sync_ep_from_ifnum; |
Alberto Aguirre | 91a8561 | 2018-04-18 09:35:35 -0500 | [diff] [blame] | 369 | case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */ |
| 370 | ep = 0x81; |
| 371 | ifnum = 2; |
| 372 | goto add_sync_ep_from_ifnum; |
Keith Winstein | f15cfca | 2020-10-25 22:05:47 -0700 | [diff] [blame] | 373 | case USB_ID(0x1686, 0xf029): /* Zoom UAC-2 */ |
| 374 | ep = 0x82; |
| 375 | ifnum = 2; |
| 376 | goto add_sync_ep_from_ifnum; |
Takashi Iwai | 1a15718 | 2019-08-20 08:58:12 +0200 | [diff] [blame] | 377 | case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */ |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 378 | case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */ |
Lassi Ylikojola | 5e35dc0 | 2018-02-09 16:51:36 +0200 | [diff] [blame] | 379 | ep = 0x81; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 380 | ifnum = 1; |
| 381 | goto add_sync_ep_from_ifnum; |
Alexander Tsoy | 2edb84e | 2020-02-29 18:18:15 +0300 | [diff] [blame] | 382 | case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */ |
| 383 | /* MicroBook IIc */ |
| 384 | if (altsd->bInterfaceClass == USB_CLASS_AUDIO) |
| 385 | return 0; |
| 386 | |
| 387 | /* MicroBook II */ |
Manuel Reinhardt | a634090 | 2019-02-28 20:34:04 +0100 | [diff] [blame] | 388 | ep = 0x84; |
| 389 | ifnum = 0; |
| 390 | goto add_sync_ep_from_ifnum; |
Alexander Tsoy | c249177 | 2020-01-15 18:13:58 +0300 | [diff] [blame] | 391 | case USB_ID(0x07fd, 0x0008): /* MOTU M Series */ |
Laurence Tratt | 3da87ec | 2020-06-21 08:50:05 +0100 | [diff] [blame] | 392 | case USB_ID(0x31e9, 0x0001): /* Solid State Logic SSL2 */ |
Laurence Tratt | e7585db | 2020-06-12 12:18:07 +0100 | [diff] [blame] | 393 | case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */ |
Joshua Sivec | 7c5b892 | 2020-08-25 18:55:18 +0200 | [diff] [blame] | 394 | case USB_ID(0x0499, 0x172f): /* Steinberg UR22C */ |
Pavel Hofman | b6a1e78 | 2020-07-03 12:04:33 +0200 | [diff] [blame] | 395 | case USB_ID(0x0d9a, 0x00df): /* RTX6001 */ |
Alexander Tsoy | c249177 | 2020-01-15 18:13:58 +0300 | [diff] [blame] | 396 | ep = 0x81; |
| 397 | ifnum = 2; |
| 398 | goto add_sync_ep_from_ifnum; |
Dmitry Panchenko | 7fccfec | 2020-06-01 13:22:24 +0300 | [diff] [blame] | 399 | case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */ |
František Kučera | 14335d8 | 2020-08-25 17:31:13 +0200 | [diff] [blame] | 400 | case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */ |
Dmitry Panchenko | 7fccfec | 2020-06-01 13:22:24 +0300 | [diff] [blame] | 401 | ep = 0x82; |
| 402 | ifnum = 0; |
| 403 | goto add_sync_ep_from_ifnum; |
Szabolcs Szőke | 7571b6a | 2019-10-11 19:19:36 +0200 | [diff] [blame] | 404 | case USB_ID(0x0582, 0x01d8): /* BOSS Katana */ |
| 405 | /* BOSS Katana amplifiers do not need quirks */ |
| 406 | return 0; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 407 | } |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 408 | |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 409 | /* Generic UAC2 implicit feedback */ |
| 410 | if (attr == USB_ENDPOINT_SYNC_ASYNC && |
| 411 | altsd->bInterfaceClass == USB_CLASS_AUDIO && |
| 412 | altsd->bInterfaceProtocol == UAC_VERSION_2 && |
| 413 | altsd->bNumEndpoints == 1) { |
| 414 | ifnum = altsd->bInterfaceNumber + 1; |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 415 | if (search_generic_implicit_fb(chip, ifnum, |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 416 | altsd->bAlternateSetting, |
| 417 | &alts, &ep)) |
| 418 | goto add_sync_ep; |
| 419 | } |
| 420 | |
| 421 | /* Roland/BOSS implicit feedback with vendor spec class */ |
Eldad Zack | 914273c | 2013-08-03 10:50:21 +0200 | [diff] [blame] | 422 | if (attr == USB_ENDPOINT_SYNC_ASYNC && |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 423 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && |
| 424 | altsd->bInterfaceProtocol == 2 && |
| 425 | altsd->bNumEndpoints == 1 && |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 426 | USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) { |
| 427 | ifnum = altsd->bInterfaceNumber + 1; |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 428 | if (search_roland_implicit_fb(chip, ifnum, |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 429 | altsd->bAlternateSetting, |
| 430 | &alts, &ep)) |
| 431 | goto add_sync_ep; |
| 432 | } |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 433 | |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 434 | /* No quirk */ |
| 435 | return 0; |
| 436 | |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 437 | add_sync_ep_from_ifnum: |
| 438 | iface = usb_ifnum_to_if(dev, ifnum); |
| 439 | |
Johan Hovold | 5d1b712 | 2020-01-14 09:39:53 +0100 | [diff] [blame] | 440 | if (!iface || iface->num_altsetting < 2) |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 441 | return 0; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 442 | |
| 443 | alts = &iface->altsetting[1]; |
| 444 | |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 445 | add_sync_ep: |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 446 | fmt->sync_ep = ep; |
| 447 | fmt->sync_iface = ifnum; |
| 448 | fmt->sync_altsetting = alts->desc.bAlternateSetting; |
| 449 | fmt->implicit_fb = 1; |
| 450 | dev_dbg(&dev->dev, "%d:%d: found implicit_fb sync_ep=%x, iface=%d, alt=%d\n", |
| 451 | fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface, |
| 452 | fmt->sync_altsetting); |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 453 | |
Manuel Reinhardt | 2bc16b9 | 2019-01-31 15:32:35 +0100 | [diff] [blame] | 454 | return 1; |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 457 | int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip, |
| 458 | struct audioformat *fmt) |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 459 | { |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 460 | struct usb_device *dev = chip->dev; |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 461 | struct usb_host_interface *alts; |
| 462 | struct usb_interface_descriptor *altsd; |
| 463 | unsigned int ep, attr, sync_attr; |
| 464 | bool is_playback; |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 465 | int err; |
| 466 | |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 467 | alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting); |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 468 | if (!alts) |
| 469 | return 0; |
| 470 | altsd = get_iface_desc(alts); |
| 471 | |
| 472 | is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN); |
| 473 | if (is_playback) { |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 474 | err = audioformat_implicit_fb_quirk(chip, fmt, alts); |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 475 | if (err > 0) |
| 476 | return 0; |
| 477 | } |
Manuel Reinhardt | 2bc16b9 | 2019-01-31 15:32:35 +0100 | [diff] [blame] | 478 | |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 479 | if (altsd->bNumEndpoints < 2) |
| 480 | return 0; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 481 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 482 | attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 483 | if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC || |
| 484 | attr == USB_ENDPOINT_SYNC_ADAPTIVE)) || |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 485 | (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE)) |
| 486 | return 0; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 487 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 488 | sync_attr = get_endpoint(alts, 1)->bmAttributes; |
| 489 | |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 490 | /* |
| 491 | * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see |
| 492 | * if we don't find a sync endpoint, as on M-Audio Transit. In case of |
| 493 | * error fall back to SYNC mode and don't create sync endpoint |
| 494 | */ |
| 495 | |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 496 | /* check sync-pipe endpoint */ |
| 497 | /* ... and check descriptor size before accessing bSynchAddress |
| 498 | because there is a version of the SB Audigy 2 NX firmware lacking |
| 499 | the audio fields in the endpoint descriptors */ |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 500 | if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC || |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 501 | (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
Eldad Zack | 95fec88 | 2013-08-03 10:50:20 +0200 | [diff] [blame] | 502 | get_endpoint(alts, 1)->bSynchAddress != 0)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 503 | dev_err(&dev->dev, |
| 504 | "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n", |
| 505 | fmt->iface, fmt->altsetting, |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 506 | get_endpoint(alts, 1)->bmAttributes, |
| 507 | get_endpoint(alts, 1)->bLength, |
| 508 | get_endpoint(alts, 1)->bSynchAddress); |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 509 | if (is_playback && attr == USB_ENDPOINT_SYNC_NONE) |
| 510 | return 0; |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 511 | return -EINVAL; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 512 | } |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 513 | ep = get_endpoint(alts, 1)->bEndpointAddress; |
Eldad Zack | 95fec88 | 2013-08-03 10:50:20 +0200 | [diff] [blame] | 514 | if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
Ard van Breemen | 1b34121 | 2019-08-02 13:52:14 +0200 | [diff] [blame] | 515 | get_endpoint(alts, 0)->bSynchAddress != 0 && |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 516 | ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) || |
| 517 | (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 518 | dev_err(&dev->dev, |
| 519 | "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n", |
| 520 | fmt->iface, fmt->altsetting, |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 521 | is_playback, ep, get_endpoint(alts, 0)->bSynchAddress); |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 522 | if (is_playback && attr == USB_ENDPOINT_SYNC_NONE) |
| 523 | return 0; |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 524 | return -EINVAL; |
| 525 | } |
| 526 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 527 | fmt->sync_ep = ep; |
| 528 | fmt->sync_iface = altsd->bInterfaceNumber; |
| 529 | fmt->sync_altsetting = altsd->bAlternateSetting; |
| 530 | if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB) |
| 531 | fmt->implicit_fb = 1; |
| 532 | |
| 533 | dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n", |
| 534 | fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface, |
| 535 | fmt->sync_altsetting, fmt->implicit_fb); |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | static int set_sync_endpoint(struct snd_usb_substream *subs, |
| 541 | struct audioformat *fmt) |
| 542 | { |
| 543 | struct usb_device *dev = subs->dev; |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 544 | struct usb_host_interface *alts; |
Takashi Iwai | d767aba | 2020-11-23 09:53:24 +0100 | [diff] [blame] | 545 | struct snd_usb_audio *chip = subs->stream->chip; |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 546 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; |
| 547 | unsigned int ep; |
| 548 | int err; |
| 549 | |
| 550 | subs->sync_endpoint = NULL; |
| 551 | subs->data_endpoint->sync_master = NULL; |
| 552 | |
| 553 | ep = fmt->sync_ep; |
| 554 | if (!ep) |
| 555 | return 0; |
| 556 | |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame] | 557 | alts = snd_usb_get_host_interface(subs->stream->chip, fmt->sync_iface, |
| 558 | fmt->altsetting); |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 559 | if (!alts) |
| 560 | return 0; |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 561 | |
Takashi Iwai | d767aba | 2020-11-23 09:53:24 +0100 | [diff] [blame] | 562 | subs->sync_endpoint = snd_usb_get_endpoint(chip, ep); |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 563 | if (!subs->sync_endpoint) { |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 564 | if (is_playback && |
| 565 | (fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_NONE) |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 566 | return 0; |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 567 | return -EINVAL; |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 568 | } |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 569 | |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 570 | subs->sync_endpoint->iface = fmt->sync_iface; |
| 571 | subs->sync_endpoint->altsetting = fmt->sync_altsetting; |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 572 | subs->sync_endpoint->is_implicit_feedback = fmt->implicit_fb; |
Erwin Burema | 10ce77e | 2020-05-10 20:29:11 +0200 | [diff] [blame] | 573 | |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 574 | subs->data_endpoint->sync_master = subs->sync_endpoint; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 575 | |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 576 | snd_usb_endpoint_set_syncinterval(subs->stream->chip, subs->sync_endpoint, alts); |
| 577 | |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 578 | if (!subs->sync_endpoint->use_count && |
| 579 | (subs->data_endpoint->iface != subs->sync_endpoint->iface || |
| 580 | subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting)) { |
Takashi Iwai | 4974b79 | 2020-11-23 09:53:08 +0100 | [diff] [blame] | 581 | err = usb_set_interface(subs->dev, |
| 582 | subs->sync_endpoint->iface, |
| 583 | subs->sync_endpoint->altsetting); |
| 584 | if (err < 0) |
| 585 | return err; |
| 586 | dev_dbg(&dev->dev, "setting usb interface %d:%d\n", |
| 587 | subs->sync_endpoint->iface, |
| 588 | subs->sync_endpoint->altsetting); |
Takashi Iwai | d767aba | 2020-11-23 09:53:24 +0100 | [diff] [blame] | 589 | snd_usb_set_interface_quirk(chip); |
Takashi Iwai | 4974b79 | 2020-11-23 09:53:08 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * find a matching format and set up the interface |
| 597 | */ |
| 598 | static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) |
| 599 | { |
| 600 | struct usb_device *dev = subs->dev; |
Takashi Iwai | d767aba | 2020-11-23 09:53:24 +0100 | [diff] [blame] | 601 | struct snd_usb_audio *chip = subs->stream->chip; |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 602 | struct usb_host_interface *alts; |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 603 | struct usb_interface *iface; |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 604 | struct snd_usb_endpoint *ep; |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 605 | int err; |
| 606 | |
| 607 | iface = usb_ifnum_to_if(dev, fmt->iface); |
| 608 | if (WARN_ON(!iface)) |
| 609 | return -EINVAL; |
Takashi Iwai | b099b96 | 2018-05-02 09:36:28 +0200 | [diff] [blame] | 610 | alts = usb_altnum_to_altsetting(iface, fmt->altsetting); |
Johan Hovold | 0141254 | 2019-12-20 10:31:34 +0100 | [diff] [blame] | 611 | if (WARN_ON(!alts)) |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 612 | return -EINVAL; |
| 613 | |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 614 | if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt) |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 615 | return 0; |
| 616 | |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 617 | /* shared EP with implicit fb */ |
| 618 | if (fmt->implicit_fb && !subs->need_setup_fmt) { |
Takashi Iwai | d767aba | 2020-11-23 09:53:24 +0100 | [diff] [blame] | 619 | ep = snd_usb_get_endpoint(chip, fmt->endpoint); |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 620 | if (ep && ep->use_count > 0) |
| 621 | goto add_data_ep; |
| 622 | } |
| 623 | |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 624 | /* close the old interface */ |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 625 | if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) { |
Takashi Iwai | 9821505 | 2020-11-23 09:53:21 +0100 | [diff] [blame] | 626 | err = usb_set_interface(subs->dev, subs->interface, 0); |
| 627 | if (err < 0) { |
| 628 | dev_err(&dev->dev, |
| 629 | "%d:%d: return to setting 0 failed (%d)\n", |
| 630 | fmt->iface, fmt->altsetting, err); |
| 631 | return -EIO; |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 632 | } |
| 633 | subs->interface = -1; |
| 634 | subs->altset_idx = 0; |
| 635 | } |
| 636 | |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 637 | if (subs->need_setup_fmt) |
| 638 | subs->need_setup_fmt = false; |
| 639 | |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 640 | /* set interface */ |
Takashi Iwai | b099b96 | 2018-05-02 09:36:28 +0200 | [diff] [blame] | 641 | if (iface->cur_altsetting != alts) { |
Takashi Iwai | d767aba | 2020-11-23 09:53:24 +0100 | [diff] [blame] | 642 | err = snd_usb_select_mode_quirk(chip, fmt); |
Jurgen Kramer | 6874daa | 2014-11-28 17:32:54 +0100 | [diff] [blame] | 643 | if (err < 0) |
| 644 | return -EIO; |
| 645 | |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 646 | err = usb_set_interface(dev, fmt->iface, fmt->altsetting); |
| 647 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 648 | dev_err(&dev->dev, |
| 649 | "%d:%d: usb_set_interface failed (%d)\n", |
| 650 | fmt->iface, fmt->altsetting, err); |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 651 | return -EIO; |
| 652 | } |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 653 | dev_dbg(&dev->dev, "setting usb interface %d:%d\n", |
| 654 | fmt->iface, fmt->altsetting); |
Takashi Iwai | d767aba | 2020-11-23 09:53:24 +0100 | [diff] [blame] | 655 | snd_usb_set_interface_quirk(chip); |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 656 | } |
| 657 | |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 658 | subs->need_setup_ep = true; |
| 659 | |
| 660 | add_data_ep: |
Takashi Iwai | b099b96 | 2018-05-02 09:36:28 +0200 | [diff] [blame] | 661 | subs->interface = fmt->iface; |
| 662 | subs->altset_idx = fmt->altset_idx; |
Takashi Iwai | d767aba | 2020-11-23 09:53:24 +0100 | [diff] [blame] | 663 | subs->data_endpoint = snd_usb_get_endpoint(chip, fmt->endpoint); |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 664 | if (!subs->data_endpoint) |
| 665 | return -EINVAL; |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 666 | subs->data_endpoint->iface = fmt->iface; |
| 667 | subs->data_endpoint->altsetting = fmt->altsetting; |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 668 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 669 | err = set_sync_endpoint(subs, fmt); |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 670 | if (err < 0) |
| 671 | return err; |
| 672 | |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 673 | if (subs->need_setup_ep) { |
Takashi Iwai | 73037c8 | 2020-11-23 09:53:26 +0100 | [diff] [blame] | 674 | err = snd_usb_init_pitch(chip, fmt); |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 675 | if (err < 0) |
| 676 | return err; |
| 677 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 678 | |
| 679 | subs->cur_audiofmt = fmt; |
| 680 | |
| 681 | snd_usb_set_format_quirk(subs, fmt); |
| 682 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | /* |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 687 | * Return the score of matching two audioformats. |
| 688 | * Veto the audioformat if: |
| 689 | * - It has no channels for some reason. |
| 690 | * - Requested PCM format is not supported. |
| 691 | * - Requested sample rate is not supported. |
| 692 | */ |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 693 | static int match_endpoint_audioformats(struct snd_usb_substream *subs, |
| 694 | struct audioformat *fp, |
| 695 | struct audioformat *match, int rate, |
| 696 | snd_pcm_format_t pcm_format) |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 697 | { |
| 698 | int i; |
| 699 | int score = 0; |
| 700 | |
| 701 | if (fp->channels < 1) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 702 | dev_dbg(&subs->dev->dev, |
| 703 | "%s: (fmt @%p) no channels\n", __func__, fp); |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
Eldad Zack | 74c34ca | 2013-04-23 01:00:41 +0200 | [diff] [blame] | 707 | if (!(fp->formats & pcm_format_to_bits(pcm_format))) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 708 | dev_dbg(&subs->dev->dev, |
| 709 | "%s: (fmt @%p) no match for format %d\n", __func__, |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 710 | fp, pcm_format); |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | for (i = 0; i < fp->nr_rates; i++) { |
| 715 | if (fp->rate_table[i] == rate) { |
| 716 | score++; |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | if (!score) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 721 | dev_dbg(&subs->dev->dev, |
| 722 | "%s: (fmt @%p) no match for rate %d\n", __func__, |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 723 | fp, rate); |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | if (fp->channels == match->channels) |
| 728 | score++; |
| 729 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 730 | dev_dbg(&subs->dev->dev, |
| 731 | "%s: (fmt @%p) score %d\n", __func__, fp, score); |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 732 | |
| 733 | return score; |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Configure the sync ep using the rate and pcm format of the data ep. |
| 738 | */ |
| 739 | static int configure_sync_endpoint(struct snd_usb_substream *subs) |
| 740 | { |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 741 | struct audioformat *fp; |
| 742 | struct audioformat *sync_fp = NULL; |
| 743 | int cur_score = 0; |
| 744 | int sync_period_bytes = subs->period_bytes; |
| 745 | struct snd_usb_substream *sync_subs = |
| 746 | &subs->stream->substream[subs->direction ^ 1]; |
| 747 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 748 | if (subs->fixed_hw || |
| 749 | !subs->sync_endpoint->is_implicit_feedback) { |
| 750 | sync_fp = subs->cur_audiofmt; |
| 751 | goto configure; |
| 752 | } |
| 753 | |
| 754 | sync_fp = find_format(&sync_subs->fmt_list, subs->pcm_format, |
| 755 | subs->cur_rate, subs->channels, NULL); |
| 756 | if (sync_fp) |
| 757 | goto configure; |
Takashi Iwai | 31be542 | 2013-01-10 14:06:38 +0100 | [diff] [blame] | 758 | |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 759 | /* Try to find the best matching audioformat. */ |
| 760 | list_for_each_entry(fp, &sync_subs->fmt_list, list) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 761 | int score = match_endpoint_audioformats(subs, |
| 762 | fp, subs->cur_audiofmt, |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 763 | subs->cur_rate, subs->pcm_format); |
| 764 | |
| 765 | if (score > cur_score) { |
| 766 | sync_fp = fp; |
| 767 | cur_score = score; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (unlikely(sync_fp == NULL)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 772 | dev_err(&subs->dev->dev, |
| 773 | "%s: no valid audioformat for sync ep %x found\n", |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 774 | __func__, sync_subs->ep_num); |
| 775 | return -EINVAL; |
| 776 | } |
| 777 | |
| 778 | /* |
| 779 | * Recalculate the period bytes if channel number differ between |
| 780 | * data and sync ep audioformat. |
| 781 | */ |
| 782 | if (sync_fp->channels != subs->channels) { |
| 783 | sync_period_bytes = (subs->period_bytes / subs->channels) * |
| 784 | sync_fp->channels; |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 785 | dev_dbg(&subs->dev->dev, |
| 786 | "%s: adjusted sync ep period bytes (%d -> %d)\n", |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 787 | __func__, subs->period_bytes, sync_period_bytes); |
| 788 | } |
| 789 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 790 | configure: |
| 791 | return snd_usb_endpoint_set_params(subs->sync_endpoint, |
| 792 | subs->pcm_format, |
| 793 | sync_fp->channels, |
| 794 | sync_period_bytes, |
| 795 | subs->period_frames, |
| 796 | subs->buffer_periods, |
| 797 | subs->cur_rate, |
| 798 | sync_fp, |
| 799 | NULL); |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /* |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 803 | * configure endpoint params |
| 804 | * |
| 805 | * called during initial setup and upon resume |
| 806 | */ |
| 807 | static int configure_endpoint(struct snd_usb_substream *subs) |
| 808 | { |
| 809 | int ret; |
| 810 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 811 | /* format changed */ |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 812 | stop_endpoints(subs); |
| 813 | sync_pending_stops(subs); |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 814 | ret = snd_usb_endpoint_set_params(subs->data_endpoint, |
| 815 | subs->pcm_format, |
| 816 | subs->channels, |
| 817 | subs->period_bytes, |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 818 | subs->period_frames, |
| 819 | subs->buffer_periods, |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 820 | subs->cur_rate, |
| 821 | subs->cur_audiofmt, |
| 822 | subs->sync_endpoint); |
| 823 | if (ret < 0) |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 824 | return ret; |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 825 | |
| 826 | if (subs->sync_endpoint) |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 827 | ret = configure_sync_endpoint(subs); |
| 828 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 829 | return ret; |
| 830 | } |
| 831 | |
Jorge Sanjuan | 3f59aa1 | 2018-07-31 13:28:44 +0100 | [diff] [blame] | 832 | static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state) |
| 833 | { |
| 834 | int ret; |
| 835 | |
| 836 | if (!subs->str_pd) |
| 837 | return 0; |
| 838 | |
| 839 | ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state); |
| 840 | if (ret < 0) { |
| 841 | dev_err(&subs->dev->dev, |
| 842 | "Cannot change Power Domain ID: %d to state: %d. Err: %d\n", |
| 843 | subs->str_pd->pd_id, state, ret); |
| 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | int snd_usb_pcm_suspend(struct snd_usb_stream *as) |
| 851 | { |
| 852 | int ret; |
| 853 | |
| 854 | ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2); |
| 855 | if (ret < 0) |
| 856 | return ret; |
| 857 | |
| 858 | ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2); |
| 859 | if (ret < 0) |
| 860 | return ret; |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | int snd_usb_pcm_resume(struct snd_usb_stream *as) |
| 866 | { |
| 867 | int ret; |
| 868 | |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 869 | ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1); |
Jorge Sanjuan | 3f59aa1 | 2018-07-31 13:28:44 +0100 | [diff] [blame] | 870 | if (ret < 0) |
| 871 | return ret; |
| 872 | |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 873 | ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1); |
Jorge Sanjuan | 3f59aa1 | 2018-07-31 13:28:44 +0100 | [diff] [blame] | 874 | if (ret < 0) |
| 875 | return ret; |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 880 | /* |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 881 | * hw_params callback |
| 882 | * |
| 883 | * allocate a buffer and set the given audio format. |
| 884 | * |
| 885 | * so far we use a physically linear buffer although packetize transfer |
| 886 | * doesn't need a continuous area. |
| 887 | * if sg buffer is supported on the later version of alsa, we'll follow |
| 888 | * that. |
| 889 | */ |
| 890 | static int snd_usb_hw_params(struct snd_pcm_substream *substream, |
| 891 | struct snd_pcm_hw_params *hw_params) |
| 892 | { |
| 893 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 894 | struct audioformat *fmt; |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 895 | int ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 896 | |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 897 | ret = snd_media_start_pipeline(subs); |
| 898 | if (ret) |
| 899 | return ret; |
| 900 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 901 | subs->pcm_format = params_format(hw_params); |
| 902 | subs->period_bytes = params_period_bytes(hw_params); |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 903 | subs->period_frames = params_period_size(hw_params); |
| 904 | subs->buffer_periods = params_periods(hw_params); |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 905 | subs->channels = params_channels(hw_params); |
| 906 | subs->cur_rate = params_rate(hw_params); |
| 907 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 908 | fmt = find_substream_format(subs); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 909 | if (!fmt) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 910 | dev_dbg(&subs->dev->dev, |
| 911 | "cannot set format: format = %#x, rate = %d, channels = %d\n", |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 912 | subs->pcm_format, subs->cur_rate, subs->channels); |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 913 | ret = -EINVAL; |
| 914 | goto stop_pipeline; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 915 | } |
| 916 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 917 | ret = snd_usb_lock_shutdown(subs->stream->chip); |
| 918 | if (ret < 0) |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 919 | goto stop_pipeline; |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 920 | |
| 921 | ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 922 | if (ret < 0) |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 923 | goto unlock; |
| 924 | |
| 925 | ret = set_format(subs, fmt); |
| 926 | if (ret < 0) |
| 927 | goto unlock; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 928 | |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 929 | unlock: |
| 930 | snd_usb_unlock_shutdown(subs->stream->chip); |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 931 | if (ret < 0) |
| 932 | goto stop_pipeline; |
| 933 | return ret; |
| 934 | |
| 935 | stop_pipeline: |
| 936 | snd_media_stop_pipeline(subs); |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 937 | return ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | /* |
| 941 | * hw_free callback |
| 942 | * |
| 943 | * reset the audio format and release the buffer |
| 944 | */ |
| 945 | static int snd_usb_hw_free(struct snd_pcm_substream *substream) |
| 946 | { |
| 947 | struct snd_usb_substream *subs = substream->runtime->private_data; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 948 | struct snd_usb_audio *chip = subs->stream->chip; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 949 | |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 950 | snd_media_stop_pipeline(subs); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 951 | subs->cur_audiofmt = NULL; |
| 952 | subs->cur_rate = 0; |
| 953 | subs->period_bytes = 0; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 954 | if (!snd_usb_lock_shutdown(chip)) { |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 955 | stop_endpoints(subs); |
| 956 | sync_pending_stops(subs); |
Eldad Zack | 26de5d0 | 2013-10-06 22:31:07 +0200 | [diff] [blame] | 957 | snd_usb_endpoint_deactivate(subs->sync_endpoint); |
| 958 | snd_usb_endpoint_deactivate(subs->data_endpoint); |
Takashi Iwai | 1803503 | 2020-11-23 09:53:12 +0100 | [diff] [blame] | 959 | if (subs->data_endpoint) { |
| 960 | subs->data_endpoint->sync_master = NULL; |
| 961 | subs->data_endpoint = NULL; |
| 962 | } |
| 963 | subs->sync_endpoint = NULL; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 964 | snd_usb_unlock_shutdown(chip); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 965 | } |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 966 | |
Takashi Iwai | 6dd9486ca | 2019-12-09 10:49:42 +0100 | [diff] [blame] | 967 | return 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /* |
| 971 | * prepare callback |
| 972 | * |
| 973 | * only a few subtle things... |
| 974 | */ |
| 975 | static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) |
| 976 | { |
| 977 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 978 | struct snd_usb_substream *subs = runtime->private_data; |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 979 | struct usb_host_interface *alts; |
| 980 | struct usb_interface *iface; |
| 981 | int ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 982 | |
| 983 | if (! subs->cur_audiofmt) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 984 | dev_err(&subs->dev->dev, "no format is specified!\n"); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 985 | return -ENXIO; |
| 986 | } |
| 987 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 988 | ret = snd_usb_lock_shutdown(subs->stream->chip); |
| 989 | if (ret < 0) |
| 990 | return ret; |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 991 | if (snd_BUG_ON(!subs->data_endpoint)) { |
| 992 | ret = -EIO; |
| 993 | goto unlock; |
| 994 | } |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 995 | |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 996 | ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0); |
| 997 | if (ret < 0) |
| 998 | goto unlock; |
| 999 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 1000 | ret = set_format(subs, subs->cur_audiofmt); |
| 1001 | if (ret < 0) |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1002 | goto unlock; |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 1003 | |
Takashi Iwai | 384dc085 | 2012-09-18 14:49:31 +0200 | [diff] [blame] | 1004 | if (subs->need_setup_ep) { |
Daniel Girnus | 1e2e3fe | 2016-12-06 14:46:15 +0900 | [diff] [blame] | 1005 | |
| 1006 | iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface); |
| 1007 | alts = &iface->altsetting[subs->cur_audiofmt->altset_idx]; |
| 1008 | ret = snd_usb_init_sample_rate(subs->stream->chip, |
Daniel Girnus | 1e2e3fe | 2016-12-06 14:46:15 +0900 | [diff] [blame] | 1009 | subs->cur_audiofmt, |
| 1010 | subs->cur_rate); |
| 1011 | if (ret < 0) |
| 1012 | goto unlock; |
| 1013 | |
Takashi Iwai | 384dc085 | 2012-09-18 14:49:31 +0200 | [diff] [blame] | 1014 | ret = configure_endpoint(subs); |
| 1015 | if (ret < 0) |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1016 | goto unlock; |
Takashi Iwai | 384dc085 | 2012-09-18 14:49:31 +0200 | [diff] [blame] | 1017 | subs->need_setup_ep = false; |
| 1018 | } |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 1019 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1020 | /* some unit conversions in runtime */ |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1021 | subs->data_endpoint->maxframesize = |
| 1022 | bytes_to_frames(runtime, subs->data_endpoint->maxpacksize); |
| 1023 | subs->data_endpoint->curframesize = |
| 1024 | bytes_to_frames(runtime, subs->data_endpoint->curpacksize); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1025 | |
| 1026 | /* reset the pointer */ |
| 1027 | subs->hwptr_done = 0; |
| 1028 | subs->transfer_done = 0; |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 1029 | subs->last_delay = 0; |
| 1030 | subs->last_frame_number = 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1031 | runtime->delay = 0; |
| 1032 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1033 | /* for playback, submit the URBs now; otherwise, the first hwptr_done |
| 1034 | * updates for all URBs would happen at the same time when starting */ |
| 1035 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 1036 | ret = start_endpoints(subs); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1037 | |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1038 | unlock: |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 1039 | snd_usb_unlock_shutdown(subs->stream->chip); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1040 | return ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
Takashi Iwai | 7ec827b | 2020-11-23 09:53:18 +0100 | [diff] [blame] | 1043 | /* |
| 1044 | * h/w constraints |
| 1045 | */ |
| 1046 | |
| 1047 | #ifdef HW_CONST_DEBUG |
| 1048 | #define hwc_debug(fmt, args...) pr_debug(fmt, ##args) |
| 1049 | #else |
| 1050 | #define hwc_debug(fmt, args...) do { } while(0) |
| 1051 | #endif |
| 1052 | |
Bhumika Goyal | aaffbf7 | 2017-08-17 14:45:59 +0530 | [diff] [blame] | 1053 | static const struct snd_pcm_hardware snd_usb_hardware = |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1054 | { |
| 1055 | .info = SNDRV_PCM_INFO_MMAP | |
| 1056 | SNDRV_PCM_INFO_MMAP_VALID | |
| 1057 | SNDRV_PCM_INFO_BATCH | |
| 1058 | SNDRV_PCM_INFO_INTERLEAVED | |
| 1059 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 1060 | SNDRV_PCM_INFO_PAUSE, |
| 1061 | .buffer_bytes_max = 1024 * 1024, |
| 1062 | .period_bytes_min = 64, |
| 1063 | .period_bytes_max = 512 * 1024, |
| 1064 | .periods_min = 2, |
| 1065 | .periods_max = 1024, |
| 1066 | }; |
| 1067 | |
| 1068 | static int hw_check_valid_format(struct snd_usb_substream *subs, |
| 1069 | struct snd_pcm_hw_params *params, |
| 1070 | struct audioformat *fp) |
| 1071 | { |
| 1072 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 1073 | struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 1074 | struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 1075 | struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame] | 1076 | struct snd_mask check_fmts; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1077 | unsigned int ptime; |
| 1078 | |
| 1079 | /* check the format */ |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame] | 1080 | snd_mask_none(&check_fmts); |
| 1081 | check_fmts.bits[0] = (u32)fp->formats; |
| 1082 | check_fmts.bits[1] = (u32)(fp->formats >> 32); |
| 1083 | snd_mask_intersect(&check_fmts, fmts); |
| 1084 | if (snd_mask_empty(&check_fmts)) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1085 | hwc_debug(" > check: no supported format %d\n", fp->format); |
| 1086 | return 0; |
| 1087 | } |
| 1088 | /* check the channels */ |
| 1089 | if (fp->channels < ct->min || fp->channels > ct->max) { |
| 1090 | hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max); |
| 1091 | return 0; |
| 1092 | } |
| 1093 | /* check the rate is within the range */ |
| 1094 | if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) { |
| 1095 | hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max); |
| 1096 | return 0; |
| 1097 | } |
| 1098 | if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) { |
| 1099 | hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); |
| 1100 | return 0; |
| 1101 | } |
| 1102 | /* check whether the period time is >= the data packet interval */ |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1103 | if (subs->speed != USB_SPEED_FULL) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1104 | ptime = 125 * (1 << fp->datainterval); |
| 1105 | if (ptime > pt->max || (ptime == pt->max && pt->openmax)) { |
| 1106 | hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max); |
| 1107 | return 0; |
| 1108 | } |
| 1109 | } |
| 1110 | return 1; |
| 1111 | } |
| 1112 | |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1113 | static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin, |
| 1114 | unsigned int rmax) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1115 | { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1116 | int changed; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1117 | |
Takashi Iwai | bc4e94a | 2020-11-23 09:53:07 +0100 | [diff] [blame] | 1118 | if (rmin > rmax) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1119 | hwc_debug(" --> get empty\n"); |
| 1120 | it->empty = 1; |
| 1121 | return -EINVAL; |
| 1122 | } |
| 1123 | |
| 1124 | changed = 0; |
| 1125 | if (it->min < rmin) { |
| 1126 | it->min = rmin; |
| 1127 | it->openmin = 0; |
| 1128 | changed = 1; |
| 1129 | } |
| 1130 | if (it->max > rmax) { |
| 1131 | it->max = rmax; |
| 1132 | it->openmax = 0; |
| 1133 | changed = 1; |
| 1134 | } |
| 1135 | if (snd_interval_checkempty(it)) { |
| 1136 | it->empty = 1; |
| 1137 | return -EINVAL; |
| 1138 | } |
| 1139 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); |
| 1140 | return changed; |
| 1141 | } |
| 1142 | |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1143 | static int hw_rule_rate(struct snd_pcm_hw_params *params, |
| 1144 | struct snd_pcm_hw_rule *rule) |
| 1145 | { |
| 1146 | struct snd_usb_substream *subs = rule->private; |
| 1147 | struct audioformat *fp; |
| 1148 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 1149 | unsigned int rmin, rmax, r; |
| 1150 | int i; |
| 1151 | |
| 1152 | hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max); |
| 1153 | rmin = UINT_MAX; |
| 1154 | rmax = 0; |
| 1155 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 1156 | if (!hw_check_valid_format(subs, params, fp)) |
| 1157 | continue; |
| 1158 | if (fp->rate_table && fp->nr_rates) { |
| 1159 | for (i = 0; i < fp->nr_rates; i++) { |
| 1160 | r = fp->rate_table[i]; |
| 1161 | if (!snd_interval_test(it, r)) |
| 1162 | continue; |
| 1163 | rmin = min(rmin, r); |
| 1164 | rmax = max(rmax, r); |
| 1165 | } |
| 1166 | } else { |
| 1167 | rmin = min(rmin, fp->rate_min); |
| 1168 | rmax = max(rmax, fp->rate_max); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | return apply_hw_params_minmax(it, rmin, rmax); |
| 1173 | } |
| 1174 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1175 | |
| 1176 | static int hw_rule_channels(struct snd_pcm_hw_params *params, |
| 1177 | struct snd_pcm_hw_rule *rule) |
| 1178 | { |
| 1179 | struct snd_usb_substream *subs = rule->private; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1180 | struct audioformat *fp; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1181 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 1182 | unsigned int rmin, rmax; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1183 | |
| 1184 | hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max); |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1185 | rmin = UINT_MAX; |
| 1186 | rmax = 0; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1187 | list_for_each_entry(fp, &subs->fmt_list, list) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1188 | if (!hw_check_valid_format(subs, params, fp)) |
| 1189 | continue; |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1190 | rmin = min(rmin, fp->channels); |
| 1191 | rmax = max(rmax, fp->channels); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1194 | return apply_hw_params_minmax(it, rmin, rmax); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | static int hw_rule_format(struct snd_pcm_hw_params *params, |
| 1198 | struct snd_pcm_hw_rule *rule) |
| 1199 | { |
| 1200 | struct snd_usb_substream *subs = rule->private; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1201 | struct audioformat *fp; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1202 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 1203 | u64 fbits; |
| 1204 | u32 oldbits[2]; |
| 1205 | int changed; |
| 1206 | |
| 1207 | hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]); |
| 1208 | fbits = 0; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1209 | list_for_each_entry(fp, &subs->fmt_list, list) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1210 | if (!hw_check_valid_format(subs, params, fp)) |
| 1211 | continue; |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame] | 1212 | fbits |= fp->formats; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | oldbits[0] = fmt->bits[0]; |
| 1216 | oldbits[1] = fmt->bits[1]; |
| 1217 | fmt->bits[0] &= (u32)fbits; |
| 1218 | fmt->bits[1] &= (u32)(fbits >> 32); |
| 1219 | if (!fmt->bits[0] && !fmt->bits[1]) { |
| 1220 | hwc_debug(" --> get empty\n"); |
| 1221 | return -EINVAL; |
| 1222 | } |
| 1223 | changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]); |
| 1224 | hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed); |
| 1225 | return changed; |
| 1226 | } |
| 1227 | |
| 1228 | static int hw_rule_period_time(struct snd_pcm_hw_params *params, |
| 1229 | struct snd_pcm_hw_rule *rule) |
| 1230 | { |
| 1231 | struct snd_usb_substream *subs = rule->private; |
| 1232 | struct audioformat *fp; |
| 1233 | struct snd_interval *it; |
| 1234 | unsigned char min_datainterval; |
| 1235 | unsigned int pmin; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1236 | |
| 1237 | it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); |
| 1238 | hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max); |
| 1239 | min_datainterval = 0xff; |
| 1240 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 1241 | if (!hw_check_valid_format(subs, params, fp)) |
| 1242 | continue; |
| 1243 | min_datainterval = min(min_datainterval, fp->datainterval); |
| 1244 | } |
| 1245 | if (min_datainterval == 0xff) { |
Uwe Kleine-König | a7ce2e0 | 2010-07-12 17:15:44 +0200 | [diff] [blame] | 1246 | hwc_debug(" --> get empty\n"); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1247 | it->empty = 1; |
| 1248 | return -EINVAL; |
| 1249 | } |
| 1250 | pmin = 125 * (1 << min_datainterval); |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1251 | |
| 1252 | return apply_hw_params_minmax(it, pmin, UINT_MAX); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1253 | } |
| 1254 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1255 | /* apply PCM hw constraints from the concurrent sync EP */ |
| 1256 | static int apply_hw_constraint_from_sync(struct snd_pcm_runtime *runtime, |
| 1257 | struct snd_usb_substream *subs) |
| 1258 | { |
| 1259 | struct snd_usb_audio *chip = subs->stream->chip; |
| 1260 | struct snd_usb_endpoint *ep; |
| 1261 | struct audioformat *fp; |
| 1262 | int err; |
| 1263 | |
| 1264 | subs->fixed_hw = 0; |
| 1265 | list_for_each_entry(fp, &subs->fmt_list, list) { |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 1266 | ep = snd_usb_get_endpoint(chip, fp->endpoint); |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1267 | if (ep && ep->cur_rate) |
| 1268 | goto found; |
| 1269 | if (!fp->implicit_fb) |
| 1270 | continue; |
| 1271 | /* for the implicit fb, check the sync ep as well */ |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 1272 | ep = snd_usb_get_endpoint(chip, fp->sync_ep); |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1273 | if (ep && ep->cur_rate) |
| 1274 | goto found; |
| 1275 | } |
| 1276 | return 0; |
| 1277 | |
| 1278 | found: |
| 1279 | if (!find_format(&subs->fmt_list, ep->cur_format, ep->cur_rate, |
| 1280 | ep->cur_channels, NULL)) { |
| 1281 | usb_audio_dbg(chip, "EP 0x%x being used, but not applicable\n", |
| 1282 | ep->ep_num); |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | usb_audio_dbg(chip, "EP 0x%x being used, using fixed params:\n", |
| 1287 | ep->ep_num); |
| 1288 | usb_audio_dbg(chip, "rate=%d, format=%s, channels=%d, period_size=%d, periods=%d\n", |
| 1289 | ep->cur_rate, snd_pcm_format_name(ep->cur_format), |
| 1290 | ep->cur_channels, ep->cur_period_frames, |
| 1291 | ep->cur_buffer_periods); |
| 1292 | |
| 1293 | runtime->hw.formats = pcm_format_to_bits(ep->cur_format); |
| 1294 | runtime->hw.rate_min = runtime->hw.rate_max = ep->cur_rate; |
| 1295 | runtime->hw.channels_min = runtime->hw.channels_max = |
| 1296 | ep->cur_channels; |
| 1297 | runtime->hw.rates = SNDRV_PCM_RATE_KNOT; |
| 1298 | runtime->hw.periods_min = runtime->hw.periods_max = |
| 1299 | ep->cur_buffer_periods; |
| 1300 | subs->fixed_hw = 1; |
| 1301 | |
| 1302 | err = snd_pcm_hw_constraint_minmax(runtime, |
| 1303 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 1304 | ep->cur_period_frames, |
| 1305 | ep->cur_period_frames); |
| 1306 | if (err < 0) |
| 1307 | return err; |
| 1308 | |
| 1309 | return 1; /* notify the finding */ |
| 1310 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1311 | |
| 1312 | /* |
| 1313 | * set up the runtime hardware information. |
| 1314 | */ |
| 1315 | |
| 1316 | static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) |
| 1317 | { |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1318 | struct snd_usb_audio *chip = subs->stream->chip; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1319 | struct audioformat *fp; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1320 | unsigned int pt, ptmin; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1321 | int param_period_time_if_needed = -1; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1322 | int err; |
| 1323 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1324 | mutex_lock(&chip->mutex); |
| 1325 | err = apply_hw_constraint_from_sync(runtime, subs); |
| 1326 | mutex_unlock(&chip->mutex); |
| 1327 | if (err < 0) |
| 1328 | return err; |
| 1329 | if (err > 0) /* found the matching? */ |
| 1330 | goto add_extra_rules; |
| 1331 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1332 | runtime->hw.formats = subs->formats; |
| 1333 | |
| 1334 | runtime->hw.rate_min = 0x7fffffff; |
| 1335 | runtime->hw.rate_max = 0; |
| 1336 | runtime->hw.channels_min = 256; |
| 1337 | runtime->hw.channels_max = 0; |
| 1338 | runtime->hw.rates = 0; |
| 1339 | ptmin = UINT_MAX; |
| 1340 | /* check min/max rates and channels */ |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1341 | list_for_each_entry(fp, &subs->fmt_list, list) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1342 | runtime->hw.rates |= fp->rates; |
| 1343 | if (runtime->hw.rate_min > fp->rate_min) |
| 1344 | runtime->hw.rate_min = fp->rate_min; |
| 1345 | if (runtime->hw.rate_max < fp->rate_max) |
| 1346 | runtime->hw.rate_max = fp->rate_max; |
| 1347 | if (runtime->hw.channels_min > fp->channels) |
| 1348 | runtime->hw.channels_min = fp->channels; |
| 1349 | if (runtime->hw.channels_max < fp->channels) |
| 1350 | runtime->hw.channels_max = fp->channels; |
| 1351 | if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) { |
| 1352 | /* FIXME: there might be more than one audio formats... */ |
| 1353 | runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = |
| 1354 | fp->frame_size; |
| 1355 | } |
| 1356 | pt = 125 * (1 << fp->datainterval); |
| 1357 | ptmin = min(ptmin, pt); |
| 1358 | } |
| 1359 | |
| 1360 | param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME; |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1361 | if (subs->speed == USB_SPEED_FULL) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1362 | /* full speed devices have fixed data packet interval */ |
| 1363 | ptmin = 1000; |
| 1364 | if (ptmin == 1000) |
| 1365 | /* if period time doesn't go below 1 ms, no rules needed */ |
| 1366 | param_period_time_if_needed = -1; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1367 | |
Takashi Iwai | e92be81 | 2018-05-27 15:09:15 +0200 | [diff] [blame] | 1368 | err = snd_pcm_hw_constraint_minmax(runtime, |
| 1369 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 1370 | ptmin, UINT_MAX); |
| 1371 | if (err < 0) |
| 1372 | return err; |
| 1373 | |
| 1374 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 1375 | hw_rule_rate, subs, |
| 1376 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1377 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1378 | param_period_time_if_needed, |
| 1379 | -1); |
| 1380 | if (err < 0) |
| 1381 | return err; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1382 | |
| 1383 | add_extra_rules: |
Takashi Iwai | e92be81 | 2018-05-27 15:09:15 +0200 | [diff] [blame] | 1384 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1385 | hw_rule_channels, subs, |
| 1386 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1387 | SNDRV_PCM_HW_PARAM_RATE, |
| 1388 | param_period_time_if_needed, |
| 1389 | -1); |
| 1390 | if (err < 0) |
| 1391 | return err; |
| 1392 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, |
| 1393 | hw_rule_format, subs, |
| 1394 | SNDRV_PCM_HW_PARAM_RATE, |
| 1395 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1396 | param_period_time_if_needed, |
| 1397 | -1); |
| 1398 | if (err < 0) |
| 1399 | return err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1400 | if (param_period_time_if_needed >= 0) { |
| 1401 | err = snd_pcm_hw_rule_add(runtime, 0, |
| 1402 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 1403 | hw_rule_period_time, subs, |
| 1404 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1405 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1406 | SNDRV_PCM_HW_PARAM_RATE, |
| 1407 | -1); |
| 1408 | if (err < 0) |
Takashi Iwai | e92be81 | 2018-05-27 15:09:15 +0200 | [diff] [blame] | 1409 | return err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1410 | } |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 1411 | |
Takashi Iwai | 1865211 | 2020-11-23 09:53:15 +0100 | [diff] [blame] | 1412 | return 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1413 | } |
| 1414 | |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1415 | static int snd_usb_pcm_open(struct snd_pcm_substream *substream) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1416 | { |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1417 | int direction = substream->stream; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1418 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
| 1419 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1420 | struct snd_usb_substream *subs = &as->substream[direction]; |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 1421 | int ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1422 | |
| 1423 | subs->interface = -1; |
Clemens Ladisch | e11b4e0 | 2010-03-04 19:46:14 +0100 | [diff] [blame] | 1424 | subs->altset_idx = 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1425 | runtime->hw = snd_usb_hardware; |
| 1426 | runtime->private_data = subs; |
| 1427 | subs->pcm_substream = substream; |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 1428 | /* runtime PM is also done there */ |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1429 | |
| 1430 | /* initialize DSD/DOP context */ |
| 1431 | subs->dsd_dop.byte_idx = 0; |
| 1432 | subs->dsd_dop.channel = 0; |
| 1433 | subs->dsd_dop.marker = 1; |
| 1434 | |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 1435 | ret = setup_hw_info(runtime, subs); |
Takashi Iwai | 1865211 | 2020-11-23 09:53:15 +0100 | [diff] [blame] | 1436 | if (ret < 0) |
| 1437 | return ret; |
| 1438 | ret = snd_usb_autoresume(subs->stream->chip); |
| 1439 | if (ret < 0) |
| 1440 | return ret; |
| 1441 | ret = snd_media_stream_init(subs, as->pcm, direction); |
| 1442 | if (ret < 0) |
| 1443 | snd_usb_autosuspend(subs->stream->chip); |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 1444 | return ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1445 | } |
| 1446 | |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1447 | static int snd_usb_pcm_close(struct snd_pcm_substream *substream) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1448 | { |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1449 | int direction = substream->stream; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1450 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
| 1451 | struct snd_usb_substream *subs = &as->substream[direction]; |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 1452 | int ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1453 | |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 1454 | snd_media_stop_pipeline(subs); |
Daniel Mack | 68e67f4 | 2012-07-12 13:08:40 +0200 | [diff] [blame] | 1455 | |
Takashi Iwai | 9821505 | 2020-11-23 09:53:21 +0100 | [diff] [blame] | 1456 | if (subs->interface >= 0 && |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 1457 | !snd_usb_lock_shutdown(subs->stream->chip)) { |
Daniel Mack | 68e67f4 | 2012-07-12 13:08:40 +0200 | [diff] [blame] | 1458 | usb_set_interface(subs->dev, subs->interface, 0); |
| 1459 | subs->interface = -1; |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 1460 | ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1); |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 1461 | snd_usb_unlock_shutdown(subs->stream->chip); |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 1462 | if (ret < 0) |
| 1463 | return ret; |
Daniel Mack | 68e67f4 | 2012-07-12 13:08:40 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1466 | subs->pcm_substream = NULL; |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 1467 | snd_usb_autosuspend(subs->stream->chip); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1468 | |
Daniel Mack | 68e67f4 | 2012-07-12 13:08:40 +0200 | [diff] [blame] | 1469 | return 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /* Since a URB can handle only a single linear buffer, we must use double |
| 1473 | * buffering when the data to be transferred overflows the buffer boundary. |
| 1474 | * To avoid inconsistencies when updating hwptr_done, we use double buffering |
| 1475 | * for all URBs. |
| 1476 | */ |
| 1477 | static void retire_capture_urb(struct snd_usb_substream *subs, |
| 1478 | struct urb *urb) |
| 1479 | { |
| 1480 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
| 1481 | unsigned int stride, frames, bytes, oldptr; |
| 1482 | int i, period_elapsed = 0; |
| 1483 | unsigned long flags; |
| 1484 | unsigned char *cp; |
Pierre-Louis Bossart | e4cc615 | 2012-12-19 11:39:05 -0600 | [diff] [blame] | 1485 | int current_frame_number; |
| 1486 | |
| 1487 | /* read frame number here, update pointer in critical section */ |
| 1488 | current_frame_number = usb_get_current_frame_number(subs->dev); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1489 | |
| 1490 | stride = runtime->frame_bits >> 3; |
| 1491 | |
| 1492 | for (i = 0; i < urb->number_of_packets; i++) { |
Calvin Owens | 1539d4f | 2013-04-12 22:33:59 -0500 | [diff] [blame] | 1493 | cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1494 | if (urb->iso_frame_desc[i].status && printk_ratelimit()) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1495 | dev_dbg(&subs->dev->dev, "frame %d active: %d\n", |
| 1496 | i, urb->iso_frame_desc[i].status); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1497 | // continue; |
| 1498 | } |
| 1499 | bytes = urb->iso_frame_desc[i].actual_length; |
Hector Martin | 1b7ecc2 | 2020-08-10 17:24:00 +0900 | [diff] [blame] | 1500 | if (subs->stream_offset_adj > 0) { |
| 1501 | unsigned int adj = min(subs->stream_offset_adj, bytes); |
| 1502 | cp += adj; |
| 1503 | bytes -= adj; |
| 1504 | subs->stream_offset_adj -= adj; |
| 1505 | } |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1506 | frames = bytes / stride; |
| 1507 | if (!subs->txfr_quirk) |
| 1508 | bytes = frames * stride; |
| 1509 | if (bytes % (runtime->sample_bits >> 3) != 0) { |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1510 | int oldbytes = bytes; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1511 | bytes = frames * stride; |
Takashi Iwai | 377a879 | 2018-05-16 20:07:18 +0200 | [diff] [blame] | 1512 | dev_warn_ratelimited(&subs->dev->dev, |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1513 | "Corrected urb data len. %d->%d\n", |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1514 | oldbytes, bytes); |
| 1515 | } |
| 1516 | /* update the current pointer */ |
| 1517 | spin_lock_irqsave(&subs->lock, flags); |
| 1518 | oldptr = subs->hwptr_done; |
| 1519 | subs->hwptr_done += bytes; |
| 1520 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 1521 | subs->hwptr_done -= runtime->buffer_size * stride; |
| 1522 | frames = (bytes + (oldptr % stride)) / stride; |
| 1523 | subs->transfer_done += frames; |
| 1524 | if (subs->transfer_done >= runtime->period_size) { |
| 1525 | subs->transfer_done -= runtime->period_size; |
| 1526 | period_elapsed = 1; |
| 1527 | } |
Pierre-Louis Bossart | e4cc615 | 2012-12-19 11:39:05 -0600 | [diff] [blame] | 1528 | /* capture delay is by construction limited to one URB, |
| 1529 | * reset delays here |
| 1530 | */ |
| 1531 | runtime->delay = subs->last_delay = 0; |
| 1532 | |
| 1533 | /* realign last_frame_number */ |
| 1534 | subs->last_frame_number = current_frame_number; |
| 1535 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ |
| 1536 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1537 | spin_unlock_irqrestore(&subs->lock, flags); |
| 1538 | /* copy a data chunk */ |
| 1539 | if (oldptr + bytes > runtime->buffer_size * stride) { |
| 1540 | unsigned int bytes1 = |
| 1541 | runtime->buffer_size * stride - oldptr; |
| 1542 | memcpy(runtime->dma_area + oldptr, cp, bytes1); |
| 1543 | memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1); |
| 1544 | } else { |
| 1545 | memcpy(runtime->dma_area + oldptr, cp, bytes); |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | if (period_elapsed) |
| 1550 | snd_pcm_period_elapsed(subs->pcm_substream); |
| 1551 | } |
| 1552 | |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1553 | static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs, |
| 1554 | struct urb *urb, unsigned int bytes) |
| 1555 | { |
| 1556 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
| 1557 | unsigned int stride = runtime->frame_bits >> 3; |
| 1558 | unsigned int dst_idx = 0; |
| 1559 | unsigned int src_idx = subs->hwptr_done; |
| 1560 | unsigned int wrap = runtime->buffer_size * stride; |
| 1561 | u8 *dst = urb->transfer_buffer; |
| 1562 | u8 *src = runtime->dma_area; |
| 1563 | u8 marker[] = { 0x05, 0xfa }; |
| 1564 | |
| 1565 | /* |
| 1566 | * The DSP DOP format defines a way to transport DSD samples over |
| 1567 | * normal PCM data endpoints. It requires stuffing of marker bytes |
| 1568 | * (0x05 and 0xfa, alternating per sample frame), and then expects |
| 1569 | * 2 additional bytes of actual payload. The whole frame is stored |
| 1570 | * LSB. |
| 1571 | * |
| 1572 | * Hence, for a stereo transport, the buffer layout looks like this, |
| 1573 | * where L refers to left channel samples and R to right. |
| 1574 | * |
| 1575 | * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa |
| 1576 | * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa |
| 1577 | * ..... |
| 1578 | * |
| 1579 | */ |
| 1580 | |
| 1581 | while (bytes--) { |
| 1582 | if (++subs->dsd_dop.byte_idx == 3) { |
| 1583 | /* frame boundary? */ |
| 1584 | dst[dst_idx++] = marker[subs->dsd_dop.marker]; |
| 1585 | src_idx += 2; |
| 1586 | subs->dsd_dop.byte_idx = 0; |
| 1587 | |
| 1588 | if (++subs->dsd_dop.channel % runtime->channels == 0) { |
| 1589 | /* alternate the marker */ |
| 1590 | subs->dsd_dop.marker++; |
| 1591 | subs->dsd_dop.marker %= ARRAY_SIZE(marker); |
| 1592 | subs->dsd_dop.channel = 0; |
| 1593 | } |
| 1594 | } else { |
| 1595 | /* stuff the DSD payload */ |
| 1596 | int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap; |
Daniel Mack | 44dcbbb | 2013-04-17 00:01:39 +0800 | [diff] [blame] | 1597 | |
| 1598 | if (subs->cur_audiofmt->dsd_bitrev) |
| 1599 | dst[dst_idx++] = bitrev8(src[idx]); |
| 1600 | else |
| 1601 | dst[dst_idx++] = src[idx]; |
| 1602 | |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1603 | subs->hwptr_done++; |
| 1604 | } |
| 1605 | } |
Ricard Wanderlof | 4c4e439 | 2015-10-19 08:52:50 +0200 | [diff] [blame] | 1606 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 1607 | subs->hwptr_done -= runtime->buffer_size * stride; |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Ricard Wanderlof | b97a936 | 2015-10-19 08:52:52 +0200 | [diff] [blame] | 1610 | static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb, |
| 1611 | int offset, int stride, unsigned int bytes) |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1612 | { |
| 1613 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
| 1614 | |
| 1615 | if (subs->hwptr_done + bytes > runtime->buffer_size * stride) { |
| 1616 | /* err, the transferred area goes over buffer boundary. */ |
| 1617 | unsigned int bytes1 = |
| 1618 | runtime->buffer_size * stride - subs->hwptr_done; |
Ricard Wanderlof | b97a936 | 2015-10-19 08:52:52 +0200 | [diff] [blame] | 1619 | memcpy(urb->transfer_buffer + offset, |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1620 | runtime->dma_area + subs->hwptr_done, bytes1); |
Ricard Wanderlof | b97a936 | 2015-10-19 08:52:52 +0200 | [diff] [blame] | 1621 | memcpy(urb->transfer_buffer + offset + bytes1, |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1622 | runtime->dma_area, bytes - bytes1); |
| 1623 | } else { |
Ricard Wanderlof | b97a936 | 2015-10-19 08:52:52 +0200 | [diff] [blame] | 1624 | memcpy(urb->transfer_buffer + offset, |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1625 | runtime->dma_area + subs->hwptr_done, bytes); |
| 1626 | } |
| 1627 | subs->hwptr_done += bytes; |
Ricard Wanderlof | 4c4e439 | 2015-10-19 08:52:50 +0200 | [diff] [blame] | 1628 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 1629 | subs->hwptr_done -= runtime->buffer_size * stride; |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1630 | } |
| 1631 | |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 1632 | static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs, |
| 1633 | struct urb *urb, int stride, |
| 1634 | unsigned int bytes) |
| 1635 | { |
| 1636 | __le32 packet_length; |
| 1637 | int i; |
| 1638 | |
| 1639 | /* Put __le32 length descriptor at start of each packet. */ |
| 1640 | for (i = 0; i < urb->number_of_packets; i++) { |
| 1641 | unsigned int length = urb->iso_frame_desc[i].length; |
| 1642 | unsigned int offset = urb->iso_frame_desc[i].offset; |
| 1643 | |
| 1644 | packet_length = cpu_to_le32(length); |
| 1645 | offset += i * sizeof(packet_length); |
| 1646 | urb->iso_frame_desc[i].offset = offset; |
| 1647 | urb->iso_frame_desc[i].length += sizeof(packet_length); |
| 1648 | memcpy(urb->transfer_buffer + offset, |
| 1649 | &packet_length, sizeof(packet_length)); |
| 1650 | copy_to_urb(subs, urb, offset + sizeof(packet_length), |
| 1651 | stride, length); |
| 1652 | } |
| 1653 | /* Adjust transfer size accordingly. */ |
| 1654 | bytes += urb->number_of_packets * sizeof(packet_length); |
| 1655 | return bytes; |
| 1656 | } |
| 1657 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1658 | static void prepare_playback_urb(struct snd_usb_substream *subs, |
| 1659 | struct urb *urb) |
| 1660 | { |
| 1661 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
Daniel Mack | 245baf9 | 2012-08-30 18:52:30 +0200 | [diff] [blame] | 1662 | struct snd_usb_endpoint *ep = subs->data_endpoint; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1663 | struct snd_urb_ctx *ctx = urb->context; |
| 1664 | unsigned int counts, frames, bytes; |
| 1665 | int i, stride, period_elapsed = 0; |
| 1666 | unsigned long flags; |
| 1667 | |
| 1668 | stride = runtime->frame_bits >> 3; |
| 1669 | |
| 1670 | frames = 0; |
| 1671 | urb->number_of_packets = 0; |
| 1672 | spin_lock_irqsave(&subs->lock, flags); |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1673 | subs->frame_limit += ep->max_urb_frames; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1674 | for (i = 0; i < ctx->packets; i++) { |
Daniel Mack | 245baf9 | 2012-08-30 18:52:30 +0200 | [diff] [blame] | 1675 | if (ctx->packet_size[i]) |
| 1676 | counts = ctx->packet_size[i]; |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 1677 | else if (ep->sync_master) |
| 1678 | counts = snd_usb_endpoint_slave_next_packet_size(ep); |
Daniel Mack | 245baf9 | 2012-08-30 18:52:30 +0200 | [diff] [blame] | 1679 | else |
| 1680 | counts = snd_usb_endpoint_next_packet_size(ep); |
| 1681 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1682 | /* set up descriptor */ |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1683 | urb->iso_frame_desc[i].offset = frames * ep->stride; |
| 1684 | urb->iso_frame_desc[i].length = counts * ep->stride; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1685 | frames += counts; |
| 1686 | urb->number_of_packets++; |
| 1687 | subs->transfer_done += counts; |
| 1688 | if (subs->transfer_done >= runtime->period_size) { |
| 1689 | subs->transfer_done -= runtime->period_size; |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1690 | subs->frame_limit = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1691 | period_elapsed = 1; |
| 1692 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) { |
| 1693 | if (subs->transfer_done > 0) { |
| 1694 | /* FIXME: fill-max mode is not |
| 1695 | * supported yet */ |
| 1696 | frames -= subs->transfer_done; |
| 1697 | counts -= subs->transfer_done; |
| 1698 | urb->iso_frame_desc[i].length = |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1699 | counts * ep->stride; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1700 | subs->transfer_done = 0; |
| 1701 | } |
| 1702 | i++; |
| 1703 | if (i < ctx->packets) { |
| 1704 | /* add a transfer delimiter */ |
| 1705 | urb->iso_frame_desc[i].offset = |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1706 | frames * ep->stride; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1707 | urb->iso_frame_desc[i].length = 0; |
| 1708 | urb->number_of_packets++; |
| 1709 | } |
| 1710 | break; |
| 1711 | } |
| 1712 | } |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1713 | /* finish at the period boundary or after enough frames */ |
| 1714 | if ((period_elapsed || |
| 1715 | subs->transfer_done >= subs->frame_limit) && |
| 1716 | !snd_usb_endpoint_implicit_feedback_sink(ep)) |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1717 | break; |
| 1718 | } |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1719 | bytes = frames * ep->stride; |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1720 | |
| 1721 | if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && |
| 1722 | subs->cur_audiofmt->dsd_dop)) { |
| 1723 | fill_playback_urb_dsd_dop(subs, urb, bytes); |
Daniel Mack | 44dcbbb | 2013-04-17 00:01:39 +0800 | [diff] [blame] | 1724 | } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 && |
| 1725 | subs->cur_audiofmt->dsd_bitrev)) { |
| 1726 | /* bit-reverse the bytes */ |
| 1727 | u8 *buf = urb->transfer_buffer; |
| 1728 | for (i = 0; i < bytes; i++) { |
| 1729 | int idx = (subs->hwptr_done + i) |
| 1730 | % (runtime->buffer_size * stride); |
| 1731 | buf[i] = bitrev8(runtime->dma_area[idx]); |
| 1732 | } |
| 1733 | |
| 1734 | subs->hwptr_done += bytes; |
Ricard Wanderlof | 4c4e439 | 2015-10-19 08:52:50 +0200 | [diff] [blame] | 1735 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 1736 | subs->hwptr_done -= runtime->buffer_size * stride; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1737 | } else { |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1738 | /* usual PCM */ |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 1739 | if (!subs->tx_length_quirk) |
| 1740 | copy_to_urb(subs, urb, 0, stride, bytes); |
| 1741 | else |
| 1742 | bytes = copy_to_urb_quirk(subs, urb, stride, bytes); |
| 1743 | /* bytes is now amount of outgoing data */ |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1744 | } |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1745 | |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1746 | /* update delay with exact number of samples queued */ |
| 1747 | runtime->delay = subs->last_delay; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1748 | runtime->delay += frames; |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1749 | subs->last_delay = runtime->delay; |
| 1750 | |
| 1751 | /* realign last_frame_number */ |
| 1752 | subs->last_frame_number = usb_get_current_frame_number(subs->dev); |
| 1753 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ |
| 1754 | |
Pierre-Louis Bossart | ea33d35 | 2015-02-06 15:55:53 -0600 | [diff] [blame] | 1755 | if (subs->trigger_tstamp_pending_update) { |
| 1756 | /* this is the first actual URB submitted, |
| 1757 | * update trigger timestamp to reflect actual start time |
| 1758 | */ |
| 1759 | snd_pcm_gettime(runtime, &runtime->trigger_tstamp); |
| 1760 | subs->trigger_tstamp_pending_update = false; |
| 1761 | } |
| 1762 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1763 | spin_unlock_irqrestore(&subs->lock, flags); |
| 1764 | urb->transfer_buffer_length = bytes; |
| 1765 | if (period_elapsed) |
| 1766 | snd_pcm_period_elapsed(subs->pcm_substream); |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * process after playback data complete |
| 1771 | * - decrease the delay count again |
| 1772 | */ |
| 1773 | static void retire_playback_urb(struct snd_usb_substream *subs, |
| 1774 | struct urb *urb) |
| 1775 | { |
| 1776 | unsigned long flags; |
| 1777 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1778 | struct snd_usb_endpoint *ep = subs->data_endpoint; |
| 1779 | int processed = urb->transfer_buffer_length / ep->stride; |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1780 | int est_delay; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1781 | |
Alexander Tsoy | 5ff40e6 | 2020-06-29 06:26:07 +0300 | [diff] [blame] | 1782 | /* ignore the delay accounting when processed=0 is given, i.e. |
| 1783 | * silent payloads are processed before handling the actual data |
Takashi Iwai | 1213a20 | 2012-09-06 14:58:00 +0200 | [diff] [blame] | 1784 | */ |
| 1785 | if (!processed) |
| 1786 | return; |
| 1787 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1788 | spin_lock_irqsave(&subs->lock, flags); |
Takashi Iwai | 48779a0 | 2012-11-23 16:00:37 +0100 | [diff] [blame] | 1789 | if (!subs->last_delay) |
| 1790 | goto out; /* short path */ |
| 1791 | |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1792 | est_delay = snd_usb_pcm_delay(subs, runtime->rate); |
| 1793 | /* update delay with exact number of samples played */ |
| 1794 | if (processed > subs->last_delay) |
| 1795 | subs->last_delay = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1796 | else |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1797 | subs->last_delay -= processed; |
| 1798 | runtime->delay = subs->last_delay; |
| 1799 | |
| 1800 | /* |
| 1801 | * Report when delay estimate is off by more than 2ms. |
| 1802 | * The error should be lower than 2ms since the estimate relies |
| 1803 | * on two reads of a counter updated every ms. |
| 1804 | */ |
Sander Eikelenboom | b7a7723 | 2014-05-02 15:09:27 +0200 | [diff] [blame] | 1805 | if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2) |
| 1806 | dev_dbg_ratelimited(&subs->dev->dev, |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1807 | "delay: estimated %d, actual %d\n", |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1808 | est_delay, subs->last_delay); |
| 1809 | |
Takashi Iwai | 48779a0 | 2012-11-23 16:00:37 +0100 | [diff] [blame] | 1810 | if (!subs->running) { |
| 1811 | /* update last_frame_number for delay counting here since |
| 1812 | * prepare_playback_urb won't be called during pause |
| 1813 | */ |
| 1814 | subs->last_frame_number = |
| 1815 | usb_get_current_frame_number(subs->dev) & 0xff; |
| 1816 | } |
| 1817 | |
| 1818 | out: |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1819 | spin_unlock_irqrestore(&subs->lock, flags); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1822 | static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, |
| 1823 | int cmd) |
| 1824 | { |
| 1825 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 1826 | |
| 1827 | switch (cmd) { |
| 1828 | case SNDRV_PCM_TRIGGER_START: |
Pierre-Louis Bossart | ea33d35 | 2015-02-06 15:55:53 -0600 | [diff] [blame] | 1829 | subs->trigger_tstamp_pending_update = true; |
Gustavo A. R. Silva | c0dbbda | 2020-07-08 15:32:36 -0500 | [diff] [blame] | 1830 | fallthrough; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1831 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 1832 | snd_usb_endpoint_set_callback(subs->data_endpoint, |
| 1833 | prepare_playback_urb, |
| 1834 | retire_playback_urb, |
| 1835 | subs); |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1836 | subs->running = 1; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1837 | return 0; |
Takashi Iwai | 75c16b5 | 2020-11-23 09:53:29 +0100 | [diff] [blame^] | 1838 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 1839 | subs->need_setup_fmt = true; |
| 1840 | fallthrough; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1841 | case SNDRV_PCM_TRIGGER_STOP: |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 1842 | stop_endpoints(subs); |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 1843 | snd_usb_endpoint_set_callback(subs->data_endpoint, |
| 1844 | NULL, NULL, NULL); |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1845 | subs->running = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1846 | return 0; |
| 1847 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Takashi Iwai | 48779a0 | 2012-11-23 16:00:37 +0100 | [diff] [blame] | 1848 | /* keep retire_data_urb for delay calculation */ |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 1849 | snd_usb_endpoint_set_callback(subs->data_endpoint, |
| 1850 | NULL, |
| 1851 | retire_playback_urb, |
| 1852 | subs); |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1853 | subs->running = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1854 | return 0; |
| 1855 | } |
| 1856 | |
| 1857 | return -EINVAL; |
| 1858 | } |
| 1859 | |
Daniel Mack | afe2596 | 2012-06-16 16:58:04 +0200 | [diff] [blame] | 1860 | static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, |
| 1861 | int cmd) |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1862 | { |
| 1863 | int err; |
| 1864 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 1865 | |
| 1866 | switch (cmd) { |
| 1867 | case SNDRV_PCM_TRIGGER_START: |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 1868 | err = start_endpoints(subs); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1869 | if (err < 0) |
| 1870 | return err; |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 1871 | fallthrough; |
| 1872 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 1873 | snd_usb_endpoint_set_callback(subs->data_endpoint, |
| 1874 | NULL, retire_capture_urb, |
| 1875 | subs); |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1876 | subs->running = 1; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1877 | return 0; |
Takashi Iwai | 75c16b5 | 2020-11-23 09:53:29 +0100 | [diff] [blame^] | 1878 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 1879 | subs->need_setup_fmt = true; |
| 1880 | fallthrough; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1881 | case SNDRV_PCM_TRIGGER_STOP: |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 1882 | stop_endpoints(subs); |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 1883 | fallthrough; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1884 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 1885 | snd_usb_endpoint_set_callback(subs->data_endpoint, |
| 1886 | NULL, NULL, NULL); |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1887 | subs->running = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1888 | return 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | return -EINVAL; |
| 1892 | } |
| 1893 | |
Arvind Yadav | 31cb1fb | 2017-08-18 13:15:21 +0530 | [diff] [blame] | 1894 | static const struct snd_pcm_ops snd_usb_playback_ops = { |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1895 | .open = snd_usb_pcm_open, |
| 1896 | .close = snd_usb_pcm_close, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1897 | .hw_params = snd_usb_hw_params, |
| 1898 | .hw_free = snd_usb_hw_free, |
| 1899 | .prepare = snd_usb_pcm_prepare, |
| 1900 | .trigger = snd_usb_substream_playback_trigger, |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 1901 | .sync_stop = snd_usb_pcm_sync_stop, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1902 | .pointer = snd_usb_pcm_pointer, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1903 | }; |
| 1904 | |
Arvind Yadav | 31cb1fb | 2017-08-18 13:15:21 +0530 | [diff] [blame] | 1905 | static const struct snd_pcm_ops snd_usb_capture_ops = { |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1906 | .open = snd_usb_pcm_open, |
| 1907 | .close = snd_usb_pcm_close, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1908 | .hw_params = snd_usb_hw_params, |
| 1909 | .hw_free = snd_usb_hw_free, |
| 1910 | .prepare = snd_usb_pcm_prepare, |
| 1911 | .trigger = snd_usb_substream_capture_trigger, |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 1912 | .sync_stop = snd_usb_pcm_sync_stop, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1913 | .pointer = snd_usb_pcm_pointer, |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 1914 | }; |
| 1915 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1916 | void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream) |
| 1917 | { |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 1918 | const struct snd_pcm_ops *ops; |
| 1919 | |
Takashi Iwai | b315997d | 2019-11-05 16:18:40 +0100 | [diff] [blame] | 1920 | ops = stream == SNDRV_PCM_STREAM_PLAYBACK ? |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 1921 | &snd_usb_playback_ops : &snd_usb_capture_ops; |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 1922 | snd_pcm_set_ops(pcm, stream, ops); |
| 1923 | } |
| 1924 | |
| 1925 | void snd_usb_preallocate_buffer(struct snd_usb_substream *subs) |
| 1926 | { |
| 1927 | struct snd_pcm *pcm = subs->stream->pcm; |
| 1928 | struct snd_pcm_substream *s = pcm->streams[subs->direction].substream; |
| 1929 | struct device *dev = subs->dev->bus->controller; |
| 1930 | |
Takashi Iwai | b315997d | 2019-11-05 16:18:40 +0100 | [diff] [blame] | 1931 | if (snd_usb_use_vmalloc) |
Takashi Iwai | 6dd9486ca | 2019-12-09 10:49:42 +0100 | [diff] [blame] | 1932 | snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC, |
| 1933 | NULL, 0, 0); |
Takashi Iwai | b315997d | 2019-11-05 16:18:40 +0100 | [diff] [blame] | 1934 | else |
Takashi Iwai | 6dd9486ca | 2019-12-09 10:49:42 +0100 | [diff] [blame] | 1935 | snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG, |
| 1936 | dev, 64*1024, 512*1024); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1937 | } |