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