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