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