Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 2 | /* |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 3 | */ |
| 4 | |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 5 | #include <linux/gfp.h> |
| 6 | #include <linux/init.h> |
Takashi Iwai | 80c8a2a | 2012-01-09 11:37:20 +0100 | [diff] [blame] | 7 | #include <linux/ratelimit.h> |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 8 | #include <linux/usb.h> |
| 9 | #include <linux/usb/audio.h> |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 10 | #include <linux/slab.h> |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 11 | |
| 12 | #include <sound/core.h> |
| 13 | #include <sound/pcm.h> |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 14 | #include <sound/pcm_params.h> |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 15 | |
| 16 | #include "usbaudio.h" |
| 17 | #include "helper.h" |
| 18 | #include "card.h" |
| 19 | #include "endpoint.h" |
| 20 | #include "pcm.h" |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 21 | #include "clock.h" |
Daniel Mack | 2b58fd5 | 2012-09-04 10:23:07 +0200 | [diff] [blame] | 22 | #include "quirks.h" |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 23 | |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 24 | enum { |
| 25 | EP_STATE_STOPPED, |
| 26 | EP_STATE_RUNNING, |
| 27 | EP_STATE_STOPPING, |
| 28 | }; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 29 | |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 30 | /* interface refcounting */ |
| 31 | struct snd_usb_iface_ref { |
| 32 | unsigned char iface; |
| 33 | bool need_setup; |
| 34 | int opened; |
| 35 | struct list_head list; |
| 36 | }; |
| 37 | |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 38 | /* |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 39 | * snd_usb_endpoint is a model that abstracts everything related to an |
| 40 | * USB endpoint and its streaming. |
| 41 | * |
| 42 | * There are functions to activate and deactivate the streaming URBs and |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 43 | * optional callbacks to let the pcm logic handle the actual content of the |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 44 | * packets for playback and record. Thus, the bus streaming and the audio |
| 45 | * handlers are fully decoupled. |
| 46 | * |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 47 | * There are two different types of endpoints in audio applications. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 48 | * |
| 49 | * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both |
| 50 | * inbound and outbound traffic. |
| 51 | * |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 52 | * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and |
| 53 | * expect the payload to carry Q10.14 / Q16.16 formatted sync information |
| 54 | * (3 or 4 bytes). |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 55 | * |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 56 | * Each endpoint has to be configured prior to being used by calling |
| 57 | * snd_usb_endpoint_set_params(). |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 58 | * |
| 59 | * The model incorporates a reference counting, so that multiple users |
| 60 | * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and |
| 61 | * only the first user will effectively start the URBs, and only the last |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 62 | * one to stop it will tear the URBs down again. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 63 | */ |
| 64 | |
| 65 | /* |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 66 | * convert a sampling rate into our full speed format (fs/1000 in Q16.16) |
| 67 | * this will overflow at approx 524 kHz |
| 68 | */ |
| 69 | static inline unsigned get_usb_full_speed_rate(unsigned int rate) |
| 70 | { |
| 71 | return ((rate << 13) + 62) / 125; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * convert a sampling rate into USB high speed format (fs/8000 in Q16.16) |
| 76 | * this will overflow at approx 4 MHz |
| 77 | */ |
| 78 | static inline unsigned get_usb_high_speed_rate(unsigned int rate) |
| 79 | { |
| 80 | return ((rate << 10) + 62) / 125; |
| 81 | } |
| 82 | |
| 83 | /* |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 84 | * release a urb data |
| 85 | */ |
| 86 | static void release_urb_ctx(struct snd_urb_ctx *u) |
| 87 | { |
Daniel Mack | d399ff9 | 2012-04-12 13:51:13 +0200 | [diff] [blame] | 88 | if (u->buffer_size) |
| 89 | usb_free_coherent(u->ep->chip->dev, u->buffer_size, |
| 90 | u->urb->transfer_buffer, |
| 91 | u->urb->transfer_dma); |
| 92 | usb_free_urb(u->urb); |
| 93 | u->urb = NULL; |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static const char *usb_error_string(int err) |
| 97 | { |
| 98 | switch (err) { |
| 99 | case -ENODEV: |
| 100 | return "no device"; |
| 101 | case -ENOENT: |
| 102 | return "endpoint not enabled"; |
| 103 | case -EPIPE: |
| 104 | return "endpoint stalled"; |
| 105 | case -ENOSPC: |
| 106 | return "not enough bandwidth"; |
| 107 | case -ESHUTDOWN: |
| 108 | return "device disabled"; |
| 109 | case -EHOSTUNREACH: |
| 110 | return "device suspended"; |
| 111 | case -EINVAL: |
| 112 | case -EAGAIN: |
| 113 | case -EFBIG: |
| 114 | case -EMSGSIZE: |
| 115 | return "internal error"; |
| 116 | default: |
| 117 | return "unknown error"; |
| 118 | } |
| 119 | } |
| 120 | |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 121 | static inline bool ep_state_running(struct snd_usb_endpoint *ep) |
| 122 | { |
| 123 | return atomic_read(&ep->state) == EP_STATE_RUNNING; |
| 124 | } |
| 125 | |
| 126 | static inline bool ep_state_update(struct snd_usb_endpoint *ep, int old, int new) |
| 127 | { |
| 128 | return atomic_cmpxchg(&ep->state, old, new) == old; |
| 129 | } |
| 130 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 131 | /** |
| 132 | * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type |
| 133 | * |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 134 | * @ep: The snd_usb_endpoint |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 135 | * |
| 136 | * Determine whether an endpoint is driven by an implicit feedback |
| 137 | * data endpoint source. |
| 138 | */ |
Eldad Zack | 98ae472 | 2013-04-03 23:18:52 +0200 | [diff] [blame] | 139 | int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 140 | { |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 141 | return ep->implicit_fb_sync && usb_pipeout(ep->pipe); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 144 | /* |
Takashi Iwai | 3d58760 | 2020-11-23 09:53:37 +0100 | [diff] [blame] | 145 | * Return the number of samples to be sent in the next packet |
| 146 | * for streaming based on information derived from sync endpoints |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 147 | * |
Takashi Iwai | 3d58760 | 2020-11-23 09:53:37 +0100 | [diff] [blame] | 148 | * This won't be used for implicit feedback which takes the packet size |
| 149 | * returned from the sync source |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 150 | */ |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 151 | static int slave_next_packet_size(struct snd_usb_endpoint *ep, |
| 152 | unsigned int avail) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 153 | { |
| 154 | unsigned long flags; |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 155 | unsigned int phase; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 156 | int ret; |
| 157 | |
| 158 | if (ep->fill_max) |
| 159 | return ep->maxframesize; |
| 160 | |
| 161 | spin_lock_irqsave(&ep->lock, flags); |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 162 | phase = (ep->phase & 0xffff) + (ep->freqm << ep->datainterval); |
| 163 | ret = min(phase >> 16, ep->maxframesize); |
| 164 | if (avail && ret >= avail) |
| 165 | ret = -EAGAIN; |
| 166 | else |
| 167 | ep->phase = phase; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 168 | spin_unlock_irqrestore(&ep->lock, flags); |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 173 | /* |
Takashi Iwai | 3d58760 | 2020-11-23 09:53:37 +0100 | [diff] [blame] | 174 | * Return the number of samples to be sent in the next packet |
| 175 | * for adaptive and synchronous endpoints |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 176 | */ |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 177 | static int next_packet_size(struct snd_usb_endpoint *ep, unsigned int avail) |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 178 | { |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 179 | unsigned int sample_accum; |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 180 | int ret; |
| 181 | |
| 182 | if (ep->fill_max) |
| 183 | return ep->maxframesize; |
| 184 | |
Takashi Iwai | 2393911 | 2021-10-01 12:54:25 +0200 | [diff] [blame] | 185 | sample_accum = ep->sample_accum + ep->sample_rem; |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 186 | if (sample_accum >= ep->pps) { |
| 187 | sample_accum -= ep->pps; |
Alexander Tsoy | b9fd200 | 2020-06-29 05:59:34 +0300 | [diff] [blame] | 188 | ret = ep->packsize[1]; |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 189 | } else { |
Alexander Tsoy | b9fd200 | 2020-06-29 05:59:34 +0300 | [diff] [blame] | 190 | ret = ep->packsize[0]; |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 191 | } |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 192 | if (avail && ret >= avail) |
| 193 | ret = -EAGAIN; |
| 194 | else |
| 195 | ep->sample_accum = sample_accum; |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
Takashi Iwai | 3d58760 | 2020-11-23 09:53:37 +0100 | [diff] [blame] | 200 | /* |
| 201 | * snd_usb_endpoint_next_packet_size: Return the number of samples to be sent |
| 202 | * in the next packet |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 203 | * |
| 204 | * If the size is equal or exceeds @avail, don't proceed but return -EAGAIN |
| 205 | * Exception: @avail = 0 for skipping the check. |
Takashi Iwai | 3d58760 | 2020-11-23 09:53:37 +0100 | [diff] [blame] | 206 | */ |
| 207 | int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep, |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 208 | struct snd_urb_ctx *ctx, int idx, |
| 209 | unsigned int avail) |
Takashi Iwai | 3d58760 | 2020-11-23 09:53:37 +0100 | [diff] [blame] | 210 | { |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 211 | unsigned int packet; |
| 212 | |
| 213 | packet = ctx->packet_size[idx]; |
| 214 | if (packet) { |
| 215 | if (avail && packet >= avail) |
| 216 | return -EAGAIN; |
| 217 | return packet; |
| 218 | } |
| 219 | |
| 220 | if (ep->sync_source) |
| 221 | return slave_next_packet_size(ep, avail); |
Takashi Iwai | 3d58760 | 2020-11-23 09:53:37 +0100 | [diff] [blame] | 222 | else |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 223 | return next_packet_size(ep, avail); |
Takashi Iwai | 3d58760 | 2020-11-23 09:53:37 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 226 | static void call_retire_callback(struct snd_usb_endpoint *ep, |
| 227 | struct urb *urb) |
| 228 | { |
| 229 | struct snd_usb_substream *data_subs; |
| 230 | |
| 231 | data_subs = READ_ONCE(ep->data_subs); |
| 232 | if (data_subs && ep->retire_data_urb) |
| 233 | ep->retire_data_urb(data_subs, urb); |
| 234 | } |
| 235 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 236 | static void retire_outbound_urb(struct snd_usb_endpoint *ep, |
| 237 | struct snd_urb_ctx *urb_ctx) |
| 238 | { |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 239 | call_retire_callback(ep, urb_ctx->urb); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 242 | static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, |
| 243 | struct snd_usb_endpoint *sender, |
| 244 | const struct urb *urb); |
| 245 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 246 | static void retire_inbound_urb(struct snd_usb_endpoint *ep, |
| 247 | struct snd_urb_ctx *urb_ctx) |
| 248 | { |
| 249 | struct urb *urb = urb_ctx->urb; |
Takashi Iwai | 53837b4 | 2020-11-23 09:53:39 +0100 | [diff] [blame] | 250 | struct snd_usb_endpoint *sync_sink; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 251 | |
Daniel Mack | 2b58fd5 | 2012-09-04 10:23:07 +0200 | [diff] [blame] | 252 | if (unlikely(ep->skip_packets > 0)) { |
| 253 | ep->skip_packets--; |
| 254 | return; |
| 255 | } |
| 256 | |
Takashi Iwai | 53837b4 | 2020-11-23 09:53:39 +0100 | [diff] [blame] | 257 | sync_sink = READ_ONCE(ep->sync_sink); |
| 258 | if (sync_sink) |
| 259 | snd_usb_handle_sync_urb(sync_sink, ep, urb); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 260 | |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 261 | call_retire_callback(ep, urb); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Takashi Iwai | c1b034a | 2021-07-29 09:38:50 +0200 | [diff] [blame] | 264 | static inline bool has_tx_length_quirk(struct snd_usb_audio *chip) |
| 265 | { |
| 266 | return chip->quirk_flags & QUIRK_FLAG_TX_LENGTH; |
| 267 | } |
| 268 | |
Ricard Wanderlof | 5cf310e | 2015-10-19 08:52:51 +0200 | [diff] [blame] | 269 | static void prepare_silent_urb(struct snd_usb_endpoint *ep, |
| 270 | struct snd_urb_ctx *ctx) |
| 271 | { |
| 272 | struct urb *urb = ctx->urb; |
| 273 | unsigned int offs = 0; |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 274 | unsigned int extra = 0; |
| 275 | __le32 packet_length; |
Ricard Wanderlof | 5cf310e | 2015-10-19 08:52:51 +0200 | [diff] [blame] | 276 | int i; |
| 277 | |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 278 | /* For tx_length_quirk, put packet length at start of packet */ |
Takashi Iwai | c1b034a | 2021-07-29 09:38:50 +0200 | [diff] [blame] | 279 | if (has_tx_length_quirk(ep->chip)) |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 280 | extra = sizeof(packet_length); |
| 281 | |
Ricard Wanderlof | 5cf310e | 2015-10-19 08:52:51 +0200 | [diff] [blame] | 282 | for (i = 0; i < ctx->packets; ++i) { |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 283 | unsigned int offset; |
| 284 | unsigned int length; |
Ricard Wanderlof | 5cf310e | 2015-10-19 08:52:51 +0200 | [diff] [blame] | 285 | int counts; |
| 286 | |
Takashi Iwai | d215f63 | 2021-09-29 10:08:41 +0200 | [diff] [blame] | 287 | counts = snd_usb_endpoint_next_packet_size(ep, ctx, i, 0); |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 288 | length = counts * ep->stride; /* number of silent bytes */ |
| 289 | offset = offs * ep->stride + extra * i; |
| 290 | urb->iso_frame_desc[i].offset = offset; |
| 291 | urb->iso_frame_desc[i].length = length + extra; |
| 292 | if (extra) { |
| 293 | packet_length = cpu_to_le32(length); |
| 294 | memcpy(urb->transfer_buffer + offset, |
| 295 | &packet_length, sizeof(packet_length)); |
| 296 | } |
| 297 | memset(urb->transfer_buffer + offset + extra, |
| 298 | ep->silence_value, length); |
Ricard Wanderlof | 5cf310e | 2015-10-19 08:52:51 +0200 | [diff] [blame] | 299 | offs += counts; |
| 300 | } |
| 301 | |
| 302 | urb->number_of_packets = ctx->packets; |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 303 | urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra; |
Takashi Iwai | e8a8f09 | 2021-06-01 18:24:55 +0200 | [diff] [blame] | 304 | ctx->queued = 0; |
Ricard Wanderlof | 5cf310e | 2015-10-19 08:52:51 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 307 | /* |
| 308 | * Prepare a PLAYBACK urb for submission to the bus. |
| 309 | */ |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 310 | static int prepare_outbound_urb(struct snd_usb_endpoint *ep, |
| 311 | struct snd_urb_ctx *ctx, |
| 312 | bool in_stream_lock) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 313 | { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 314 | struct urb *urb = ctx->urb; |
| 315 | unsigned char *cp = urb->transfer_buffer; |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 316 | struct snd_usb_substream *data_subs; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 317 | |
| 318 | urb->dev = ep->chip->dev; /* we need to set this at each time */ |
| 319 | |
| 320 | switch (ep->type) { |
| 321 | case SND_USB_ENDPOINT_TYPE_DATA: |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 322 | data_subs = READ_ONCE(ep->data_subs); |
| 323 | if (data_subs && ep->prepare_data_urb) |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 324 | return ep->prepare_data_urb(data_subs, urb, in_stream_lock); |
| 325 | /* no data provider, so send silence */ |
| 326 | prepare_silent_urb(ep, ctx); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 327 | break; |
| 328 | |
| 329 | case SND_USB_ENDPOINT_TYPE_SYNC: |
| 330 | if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) { |
| 331 | /* |
| 332 | * fill the length and offset of each urb descriptor. |
| 333 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. |
| 334 | */ |
| 335 | urb->iso_frame_desc[0].length = 4; |
| 336 | urb->iso_frame_desc[0].offset = 0; |
| 337 | cp[0] = ep->freqn; |
| 338 | cp[1] = ep->freqn >> 8; |
| 339 | cp[2] = ep->freqn >> 16; |
| 340 | cp[3] = ep->freqn >> 24; |
| 341 | } else { |
| 342 | /* |
| 343 | * fill the length and offset of each urb descriptor. |
| 344 | * the fixed 10.14 frequency is passed through the pipe. |
| 345 | */ |
| 346 | urb->iso_frame_desc[0].length = 3; |
| 347 | urb->iso_frame_desc[0].offset = 0; |
| 348 | cp[0] = ep->freqn >> 2; |
| 349 | cp[1] = ep->freqn >> 10; |
| 350 | cp[2] = ep->freqn >> 18; |
| 351 | } |
| 352 | |
| 353 | break; |
| 354 | } |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 355 | return 0; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Prepare a CAPTURE or SYNC urb for submission to the bus. |
| 360 | */ |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 361 | static int prepare_inbound_urb(struct snd_usb_endpoint *ep, |
| 362 | struct snd_urb_ctx *urb_ctx) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 363 | { |
| 364 | int i, offs; |
| 365 | struct urb *urb = urb_ctx->urb; |
| 366 | |
| 367 | urb->dev = ep->chip->dev; /* we need to set this at each time */ |
| 368 | |
| 369 | switch (ep->type) { |
| 370 | case SND_USB_ENDPOINT_TYPE_DATA: |
| 371 | offs = 0; |
| 372 | for (i = 0; i < urb_ctx->packets; i++) { |
| 373 | urb->iso_frame_desc[i].offset = offs; |
| 374 | urb->iso_frame_desc[i].length = ep->curpacksize; |
| 375 | offs += ep->curpacksize; |
| 376 | } |
| 377 | |
| 378 | urb->transfer_buffer_length = offs; |
| 379 | urb->number_of_packets = urb_ctx->packets; |
| 380 | break; |
| 381 | |
| 382 | case SND_USB_ENDPOINT_TYPE_SYNC: |
| 383 | urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize); |
| 384 | urb->iso_frame_desc[0].offset = 0; |
| 385 | break; |
| 386 | } |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 387 | return 0; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 390 | /* notify an error as XRUN to the assigned PCM data substream */ |
| 391 | static void notify_xrun(struct snd_usb_endpoint *ep) |
| 392 | { |
| 393 | struct snd_usb_substream *data_subs; |
| 394 | |
| 395 | data_subs = READ_ONCE(ep->data_subs); |
| 396 | if (data_subs && data_subs->pcm_substream) |
| 397 | snd_pcm_stop_xrun(data_subs->pcm_substream); |
| 398 | } |
| 399 | |
| 400 | static struct snd_usb_packet_info * |
| 401 | next_packet_fifo_enqueue(struct snd_usb_endpoint *ep) |
| 402 | { |
| 403 | struct snd_usb_packet_info *p; |
| 404 | |
| 405 | p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) % |
| 406 | ARRAY_SIZE(ep->next_packet); |
| 407 | ep->next_packet_queued++; |
| 408 | return p; |
| 409 | } |
| 410 | |
| 411 | static struct snd_usb_packet_info * |
| 412 | next_packet_fifo_dequeue(struct snd_usb_endpoint *ep) |
| 413 | { |
| 414 | struct snd_usb_packet_info *p; |
| 415 | |
| 416 | p = ep->next_packet + ep->next_packet_head; |
| 417 | ep->next_packet_head++; |
| 418 | ep->next_packet_head %= ARRAY_SIZE(ep->next_packet); |
| 419 | ep->next_packet_queued--; |
| 420 | return p; |
| 421 | } |
| 422 | |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 423 | static void push_back_to_ready_list(struct snd_usb_endpoint *ep, |
| 424 | struct snd_urb_ctx *ctx) |
| 425 | { |
| 426 | unsigned long flags; |
| 427 | |
| 428 | spin_lock_irqsave(&ep->lock, flags); |
| 429 | list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); |
| 430 | spin_unlock_irqrestore(&ep->lock, flags); |
| 431 | } |
| 432 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 433 | /* |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 434 | * Send output urbs that have been prepared previously. URBs are dequeued |
Randy Dunlap | 0569b3d | 2020-10-05 12:12:44 -0700 | [diff] [blame] | 435 | * from ep->ready_playback_urbs and in case there aren't any available |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 436 | * or there are no packets that have been prepared, this function does |
| 437 | * nothing. |
| 438 | * |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 439 | * The reason why the functionality of sending and preparing URBs is separated |
| 440 | * is that host controllers don't guarantee the order in which they return |
| 441 | * inbound and outbound packets to their submitters. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 442 | * |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 443 | * This function is used both for implicit feedback endpoints and in low- |
| 444 | * latency playback mode. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 445 | */ |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 446 | void snd_usb_queue_pending_output_urbs(struct snd_usb_endpoint *ep, |
| 447 | bool in_stream_lock) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 448 | { |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 449 | bool implicit_fb = snd_usb_endpoint_implicit_feedback_sink(ep); |
| 450 | |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 451 | while (ep_state_running(ep)) { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 452 | |
| 453 | unsigned long flags; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 454 | struct snd_usb_packet_info *packet; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 455 | struct snd_urb_ctx *ctx = NULL; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 456 | int err, i; |
| 457 | |
| 458 | spin_lock_irqsave(&ep->lock, flags); |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 459 | if ((!implicit_fb || ep->next_packet_queued > 0) && |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 460 | !list_empty(&ep->ready_playback_urbs)) { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 461 | /* take URB out of FIFO */ |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 462 | ctx = list_first_entry(&ep->ready_playback_urbs, |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 463 | struct snd_urb_ctx, ready_list); |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 464 | list_del_init(&ctx->ready_list); |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 465 | if (implicit_fb) |
| 466 | packet = next_packet_fifo_dequeue(ep); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 467 | } |
| 468 | spin_unlock_irqrestore(&ep->lock, flags); |
| 469 | |
| 470 | if (ctx == NULL) |
| 471 | return; |
| 472 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 473 | /* copy over the length information */ |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 474 | if (implicit_fb) { |
| 475 | for (i = 0; i < packet->packets; i++) |
| 476 | ctx->packet_size[i] = packet->packet_size[i]; |
| 477 | } |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 478 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 479 | /* call the data handler to fill in playback data */ |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 480 | err = prepare_outbound_urb(ep, ctx, in_stream_lock); |
| 481 | /* can be stopped during prepare callback */ |
| 482 | if (unlikely(!ep_state_running(ep))) |
| 483 | break; |
| 484 | if (err < 0) { |
| 485 | /* push back to ready list again for -EAGAIN */ |
| 486 | if (err == -EAGAIN) |
| 487 | push_back_to_ready_list(ep, ctx); |
| 488 | else |
| 489 | notify_xrun(ep); |
| 490 | return; |
| 491 | } |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 492 | |
| 493 | err = usb_submit_urb(ctx->urb, GFP_ATOMIC); |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 494 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 495 | usb_audio_err(ep->chip, |
Takashi Iwai | e93e890 | 2020-11-23 09:53:13 +0100 | [diff] [blame] | 496 | "Unable to submit urb #%d: %d at %s\n", |
| 497 | ctx->index, err, __func__); |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 498 | notify_xrun(ep); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | set_bit(ctx->index, &ep->active_mask); |
Takashi Iwai | 86a42ad | 2021-09-29 10:08:37 +0200 | [diff] [blame] | 503 | atomic_inc(&ep->submitted_urbs); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * complete callback for urbs |
| 509 | */ |
| 510 | static void snd_complete_urb(struct urb *urb) |
| 511 | { |
| 512 | struct snd_urb_ctx *ctx = urb->context; |
| 513 | struct snd_usb_endpoint *ep = ctx->ep; |
| 514 | int err; |
| 515 | |
| 516 | if (unlikely(urb->status == -ENOENT || /* unlinked */ |
| 517 | urb->status == -ENODEV || /* device removed */ |
| 518 | urb->status == -ECONNRESET || /* unlinked */ |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 519 | urb->status == -ESHUTDOWN)) /* device disabled */ |
| 520 | goto exit_clear; |
| 521 | /* device disconnected */ |
| 522 | if (unlikely(atomic_read(&ep->chip->shutdown))) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 523 | goto exit_clear; |
| 524 | |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 525 | if (unlikely(!ep_state_running(ep))) |
Ioan-Adrian Ratiu | 13a6c83 | 2017-01-05 00:37:47 +0200 | [diff] [blame] | 526 | goto exit_clear; |
| 527 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 528 | if (usb_pipeout(ep->pipe)) { |
| 529 | retire_outbound_urb(ep, ctx); |
| 530 | /* can be stopped during retire callback */ |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 531 | if (unlikely(!ep_state_running(ep))) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 532 | goto exit_clear; |
| 533 | |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 534 | /* in low-latency and implicit-feedback modes, push back the |
| 535 | * URB to ready list at first, then process as much as possible |
| 536 | */ |
| 537 | if (ep->lowlatency_playback || |
| 538 | snd_usb_endpoint_implicit_feedback_sink(ep)) { |
| 539 | push_back_to_ready_list(ep, ctx); |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 540 | clear_bit(ctx->index, &ep->active_mask); |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 541 | snd_usb_queue_pending_output_urbs(ep, false); |
Takashi Iwai | 86a42ad | 2021-09-29 10:08:37 +0200 | [diff] [blame] | 542 | atomic_dec(&ep->submitted_urbs); /* decrement at last */ |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 543 | return; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 546 | /* in non-lowlatency mode, no error handling for prepare */ |
| 547 | prepare_outbound_urb(ep, ctx, false); |
Henry Lin | 5286993 | 2019-11-13 10:14:19 +0800 | [diff] [blame] | 548 | /* can be stopped during prepare callback */ |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 549 | if (unlikely(!ep_state_running(ep))) |
Henry Lin | 5286993 | 2019-11-13 10:14:19 +0800 | [diff] [blame] | 550 | goto exit_clear; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 551 | } else { |
| 552 | retire_inbound_urb(ep, ctx); |
| 553 | /* can be stopped during retire callback */ |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 554 | if (unlikely(!ep_state_running(ep))) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 555 | goto exit_clear; |
| 556 | |
| 557 | prepare_inbound_urb(ep, ctx); |
| 558 | } |
| 559 | |
| 560 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 561 | if (err == 0) |
| 562 | return; |
| 563 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 564 | usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err); |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 565 | notify_xrun(ep); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 566 | |
| 567 | exit_clear: |
| 568 | clear_bit(ctx->index, &ep->active_mask); |
Takashi Iwai | 86a42ad | 2021-09-29 10:08:37 +0200 | [diff] [blame] | 569 | atomic_dec(&ep->submitted_urbs); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Takashi Iwai | c7474d0 | 2020-11-23 09:53:11 +0100 | [diff] [blame] | 572 | /* |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 573 | * Find or create a refcount object for the given interface |
| 574 | * |
| 575 | * The objects are released altogether in snd_usb_endpoint_free_all() |
| 576 | */ |
| 577 | static struct snd_usb_iface_ref * |
| 578 | iface_ref_find(struct snd_usb_audio *chip, int iface) |
| 579 | { |
| 580 | struct snd_usb_iface_ref *ip; |
| 581 | |
| 582 | list_for_each_entry(ip, &chip->iface_ref_list, list) |
| 583 | if (ip->iface == iface) |
| 584 | return ip; |
| 585 | |
| 586 | ip = kzalloc(sizeof(*ip), GFP_KERNEL); |
| 587 | if (!ip) |
| 588 | return NULL; |
| 589 | ip->iface = iface; |
| 590 | list_add_tail(&ip->list, &chip->iface_ref_list); |
| 591 | return ip; |
| 592 | } |
| 593 | |
| 594 | /* |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 595 | * Get the existing endpoint object corresponding EP |
Takashi Iwai | c7474d0 | 2020-11-23 09:53:11 +0100 | [diff] [blame] | 596 | * Returns NULL if not present. |
Takashi Iwai | c7474d0 | 2020-11-23 09:53:11 +0100 | [diff] [blame] | 597 | */ |
| 598 | struct snd_usb_endpoint * |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 599 | snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num) |
Takashi Iwai | c7474d0 | 2020-11-23 09:53:11 +0100 | [diff] [blame] | 600 | { |
| 601 | struct snd_usb_endpoint *ep; |
| 602 | |
| 603 | list_for_each_entry(ep, &chip->ep_list, list) { |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 604 | if (ep->ep_num == ep_num) |
Takashi Iwai | c7474d0 | 2020-11-23 09:53:11 +0100 | [diff] [blame] | 605 | return ep; |
| 606 | } |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 607 | |
Takashi Iwai | c7474d0 | 2020-11-23 09:53:11 +0100 | [diff] [blame] | 608 | return NULL; |
| 609 | } |
| 610 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 611 | #define ep_type_name(type) \ |
| 612 | (type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync") |
| 613 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 614 | /** |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 615 | * snd_usb_add_endpoint: Add an endpoint to an USB audio chip |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 616 | * |
| 617 | * @chip: The chip |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 618 | * @ep_num: The number of the endpoint to use |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 619 | * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC |
| 620 | * |
| 621 | * If the requested endpoint has not been added to the given chip before, |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 622 | * a new instance is created. |
| 623 | * |
| 624 | * Returns zero on success or a negative error code. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 625 | * |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 626 | * New endpoints will be added to chip->ep_list and freed by |
| 627 | * calling snd_usb_endpoint_free_all(). |
Takashi Iwai | 447d627 | 2016-03-15 15:20:58 +0100 | [diff] [blame] | 628 | * |
| 629 | * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that |
| 630 | * bNumEndpoints > 1 beforehand. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 631 | */ |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 632 | int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 633 | { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 634 | struct snd_usb_endpoint *ep; |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 635 | bool is_playback; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 636 | |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 637 | ep = snd_usb_get_endpoint(chip, ep_num); |
| 638 | if (ep) |
| 639 | return 0; |
Eldad Zack | e7e58df | 2013-08-03 10:51:15 +0200 | [diff] [blame] | 640 | |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 641 | usb_audio_dbg(chip, "Creating new %s endpoint #%x\n", |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 642 | ep_type_name(type), |
| 643 | ep_num); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 644 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
| 645 | if (!ep) |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 646 | return -ENOMEM; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 647 | |
| 648 | ep->chip = chip; |
| 649 | spin_lock_init(&ep->lock); |
| 650 | ep->type = type; |
| 651 | ep->ep_num = ep_num; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 652 | INIT_LIST_HEAD(&ep->ready_playback_urbs); |
Takashi Iwai | 86a42ad | 2021-09-29 10:08:37 +0200 | [diff] [blame] | 653 | atomic_set(&ep->submitted_urbs, 0); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 654 | |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 655 | is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT); |
| 656 | ep_num &= USB_ENDPOINT_NUMBER_MASK; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 657 | if (is_playback) |
| 658 | ep->pipe = usb_sndisocpipe(chip->dev, ep_num); |
| 659 | else |
| 660 | ep->pipe = usb_rcvisocpipe(chip->dev, ep_num); |
| 661 | |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 662 | list_add_tail(&ep->list, &chip->ep_list); |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | /* Set up syncinterval and maxsyncsize for a sync EP */ |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 667 | static void endpoint_set_syncinterval(struct snd_usb_audio *chip, |
| 668 | struct snd_usb_endpoint *ep) |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 669 | { |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 670 | struct usb_host_interface *alts; |
| 671 | struct usb_endpoint_descriptor *desc; |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 672 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 673 | alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting); |
| 674 | if (!alts) |
| 675 | return; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 676 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 677 | desc = get_endpoint(alts, ep->ep_idx); |
| 678 | if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
| 679 | desc->bRefresh >= 1 && desc->bRefresh <= 9) |
| 680 | ep->syncinterval = desc->bRefresh; |
| 681 | else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) |
| 682 | ep->syncinterval = 1; |
| 683 | else if (desc->bInterval >= 1 && desc->bInterval <= 16) |
| 684 | ep->syncinterval = desc->bInterval - 1; |
| 685 | else |
| 686 | ep->syncinterval = 3; |
| 687 | |
| 688 | ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize); |
| 689 | } |
| 690 | |
| 691 | static bool endpoint_compatible(struct snd_usb_endpoint *ep, |
| 692 | const struct audioformat *fp, |
| 693 | const struct snd_pcm_hw_params *params) |
| 694 | { |
| 695 | if (!ep->opened) |
| 696 | return false; |
| 697 | if (ep->cur_audiofmt != fp) |
| 698 | return false; |
| 699 | if (ep->cur_rate != params_rate(params) || |
| 700 | ep->cur_format != params_format(params) || |
| 701 | ep->cur_period_frames != params_period_size(params) || |
| 702 | ep->cur_buffer_periods != params_periods(params)) |
| 703 | return false; |
| 704 | return true; |
| 705 | } |
| 706 | |
| 707 | /* |
gushengxian | ff630b6 | 2021-07-05 05:00:52 -0700 | [diff] [blame] | 708 | * Check whether the given fp and hw params are compatible with the current |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 709 | * setup of the target EP for implicit feedback sync |
| 710 | */ |
| 711 | bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip, |
| 712 | struct snd_usb_endpoint *ep, |
| 713 | const struct audioformat *fp, |
| 714 | const struct snd_pcm_hw_params *params) |
| 715 | { |
| 716 | bool ret; |
| 717 | |
| 718 | mutex_lock(&chip->mutex); |
| 719 | ret = endpoint_compatible(ep, fp, params); |
| 720 | mutex_unlock(&chip->mutex); |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * snd_usb_endpoint_open: Open the endpoint |
| 726 | * |
| 727 | * Called from hw_params to assign the endpoint to the substream. |
| 728 | * It's reference-counted, and only the first opener is allowed to set up |
| 729 | * arbitrary parameters. The later opener must be compatible with the |
| 730 | * former opened parameters. |
| 731 | * The endpoint needs to be closed via snd_usb_endpoint_close() later. |
| 732 | * |
| 733 | * Note that this function doesn't configure the endpoint. The substream |
| 734 | * needs to set it up later via snd_usb_endpoint_configure(). |
| 735 | */ |
| 736 | struct snd_usb_endpoint * |
| 737 | snd_usb_endpoint_open(struct snd_usb_audio *chip, |
Takashi Iwai | cab941b | 2020-11-23 09:53:33 +0100 | [diff] [blame] | 738 | const struct audioformat *fp, |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 739 | const struct snd_pcm_hw_params *params, |
| 740 | bool is_sync_ep) |
| 741 | { |
| 742 | struct snd_usb_endpoint *ep; |
| 743 | int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint; |
| 744 | |
| 745 | mutex_lock(&chip->mutex); |
| 746 | ep = snd_usb_get_endpoint(chip, ep_num); |
| 747 | if (!ep) { |
| 748 | usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num); |
| 749 | goto unlock; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 750 | } |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 751 | |
| 752 | if (!ep->opened) { |
| 753 | if (is_sync_ep) { |
| 754 | ep->iface = fp->sync_iface; |
| 755 | ep->altsetting = fp->sync_altsetting; |
| 756 | ep->ep_idx = fp->sync_ep_idx; |
| 757 | } else { |
| 758 | ep->iface = fp->iface; |
| 759 | ep->altsetting = fp->altsetting; |
Takashi Iwai | eae4d05 | 2021-01-08 08:52:18 +0100 | [diff] [blame] | 760 | ep->ep_idx = fp->ep_idx; |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 761 | } |
| 762 | usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n", |
| 763 | ep_num, ep->iface, ep->altsetting, ep->ep_idx); |
| 764 | |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 765 | ep->iface_ref = iface_ref_find(chip, ep->iface); |
| 766 | if (!ep->iface_ref) { |
| 767 | ep = NULL; |
| 768 | goto unlock; |
| 769 | } |
| 770 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 771 | ep->cur_audiofmt = fp; |
| 772 | ep->cur_channels = fp->channels; |
| 773 | ep->cur_rate = params_rate(params); |
| 774 | ep->cur_format = params_format(params); |
| 775 | ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) * |
| 776 | ep->cur_channels / 8; |
| 777 | ep->cur_period_frames = params_period_size(params); |
| 778 | ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes; |
| 779 | ep->cur_buffer_periods = params_periods(params); |
Takashi Iwai | 4e7cf1f | 2021-09-29 10:08:36 +0200 | [diff] [blame] | 780 | ep->cur_clock = fp->clock; |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 781 | |
| 782 | if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC) |
| 783 | endpoint_set_syncinterval(chip, ep); |
| 784 | |
| 785 | ep->implicit_fb_sync = fp->implicit_fb; |
| 786 | ep->need_setup = true; |
| 787 | |
| 788 | usb_audio_dbg(chip, " channels=%d, rate=%d, format=%s, period_bytes=%d, periods=%d, implicit_fb=%d\n", |
| 789 | ep->cur_channels, ep->cur_rate, |
| 790 | snd_pcm_format_name(ep->cur_format), |
| 791 | ep->cur_period_bytes, ep->cur_buffer_periods, |
| 792 | ep->implicit_fb_sync); |
| 793 | |
| 794 | } else { |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 795 | if (WARN_ON(!ep->iface_ref)) { |
| 796 | ep = NULL; |
| 797 | goto unlock; |
| 798 | } |
| 799 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 800 | if (!endpoint_compatible(ep, fp, params)) { |
| 801 | usb_audio_err(chip, "Incompatible EP setup for 0x%x\n", |
| 802 | ep_num); |
| 803 | ep = NULL; |
| 804 | goto unlock; |
| 805 | } |
| 806 | |
| 807 | usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n", |
| 808 | ep_num, ep->opened); |
| 809 | } |
| 810 | |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 811 | if (!ep->iface_ref->opened++) |
| 812 | ep->iface_ref->need_setup = true; |
| 813 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 814 | ep->opened++; |
| 815 | |
| 816 | unlock: |
| 817 | mutex_unlock(&chip->mutex); |
| 818 | return ep; |
| 819 | } |
| 820 | |
| 821 | /* |
| 822 | * snd_usb_endpoint_set_sync: Link data and sync endpoints |
| 823 | * |
| 824 | * Pass NULL to sync_ep to unlink again |
| 825 | */ |
| 826 | void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip, |
| 827 | struct snd_usb_endpoint *data_ep, |
| 828 | struct snd_usb_endpoint *sync_ep) |
| 829 | { |
Takashi Iwai | 53837b4 | 2020-11-23 09:53:39 +0100 | [diff] [blame] | 830 | data_ep->sync_source = sync_ep; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /* |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 834 | * Set data endpoint callbacks and the assigned data stream |
| 835 | * |
| 836 | * Called at PCM trigger and cleanups. |
| 837 | * Pass NULL to deactivate each callback. |
| 838 | */ |
| 839 | void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep, |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 840 | int (*prepare)(struct snd_usb_substream *subs, |
| 841 | struct urb *urb, |
| 842 | bool in_stream_lock), |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 843 | void (*retire)(struct snd_usb_substream *subs, |
| 844 | struct urb *urb), |
| 845 | struct snd_usb_substream *data_subs) |
| 846 | { |
| 847 | ep->prepare_data_urb = prepare; |
| 848 | ep->retire_data_urb = retire; |
Takashi Iwai | 9c9a3b9 | 2021-09-29 10:08:38 +0200 | [diff] [blame] | 849 | if (data_subs) |
| 850 | ep->lowlatency_playback = data_subs->lowlatency_playback; |
| 851 | else |
| 852 | ep->lowlatency_playback = false; |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 853 | WRITE_ONCE(ep->data_subs, data_subs); |
| 854 | } |
| 855 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 856 | static int endpoint_set_interface(struct snd_usb_audio *chip, |
| 857 | struct snd_usb_endpoint *ep, |
| 858 | bool set) |
| 859 | { |
| 860 | int altset = set ? ep->altsetting : 0; |
| 861 | int err; |
| 862 | |
| 863 | usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n", |
| 864 | ep->iface, altset, ep->ep_num); |
| 865 | err = usb_set_interface(chip->dev, ep->iface, altset); |
| 866 | if (err < 0) { |
| 867 | usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n", |
| 868 | ep->iface, altset, err); |
| 869 | return err; |
| 870 | } |
| 871 | |
Takashi Iwai | 1f074fe | 2021-07-29 09:38:55 +0200 | [diff] [blame] | 872 | if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY) |
| 873 | msleep(50); |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * snd_usb_endpoint_close: Close the endpoint |
| 879 | * |
| 880 | * Unreference the already opened endpoint via snd_usb_endpoint_open(). |
| 881 | */ |
| 882 | void snd_usb_endpoint_close(struct snd_usb_audio *chip, |
| 883 | struct snd_usb_endpoint *ep) |
| 884 | { |
| 885 | mutex_lock(&chip->mutex); |
| 886 | usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n", |
| 887 | ep->ep_num, ep->opened); |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 888 | |
| 889 | if (!--ep->iface_ref->opened) |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 890 | endpoint_set_interface(chip, ep, false); |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 891 | |
| 892 | if (!--ep->opened) { |
Takashi Iwai | 89fa3f6 | 2020-11-23 09:53:40 +0100 | [diff] [blame] | 893 | ep->iface = 0; |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 894 | ep->altsetting = 0; |
| 895 | ep->cur_audiofmt = NULL; |
| 896 | ep->cur_rate = 0; |
Takashi Iwai | 4e7cf1f | 2021-09-29 10:08:36 +0200 | [diff] [blame] | 897 | ep->cur_clock = 0; |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 898 | ep->iface_ref = NULL; |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 899 | usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num); |
| 900 | } |
| 901 | mutex_unlock(&chip->mutex); |
| 902 | } |
| 903 | |
| 904 | /* Prepare for suspening EP, called from the main suspend handler */ |
| 905 | void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep) |
| 906 | { |
| 907 | ep->need_setup = true; |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 908 | if (ep->iface_ref) |
| 909 | ep->iface_ref->need_setup = true; |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 910 | } |
| 911 | |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 912 | /* |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 913 | * wait until all urbs are processed. |
| 914 | */ |
| 915 | static int wait_clear_urbs(struct snd_usb_endpoint *ep) |
| 916 | { |
| 917 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 918 | int alive; |
| 919 | |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 920 | if (atomic_read(&ep->state) != EP_STATE_STOPPING) |
Takashi Iwai | d0f09d1 | 2020-11-23 09:53:35 +0100 | [diff] [blame] | 921 | return 0; |
| 922 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 923 | do { |
Takashi Iwai | 86a42ad | 2021-09-29 10:08:37 +0200 | [diff] [blame] | 924 | alive = atomic_read(&ep->submitted_urbs); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 925 | if (!alive) |
| 926 | break; |
| 927 | |
| 928 | schedule_timeout_uninterruptible(1); |
| 929 | } while (time_before(jiffies, end_time)); |
| 930 | |
| 931 | if (alive) |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 932 | usb_audio_err(ep->chip, |
| 933 | "timeout: still %d active urbs on EP #%x\n", |
| 934 | alive, ep->ep_num); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 935 | |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 936 | if (ep_state_update(ep, EP_STATE_STOPPING, EP_STATE_STOPPED)) { |
| 937 | ep->sync_sink = NULL; |
| 938 | snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL); |
| 939 | } |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 940 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 941 | return 0; |
| 942 | } |
| 943 | |
Takashi Iwai | f58161b | 2012-11-08 08:52:45 +0100 | [diff] [blame] | 944 | /* sync the pending stop operation; |
| 945 | * this function itself doesn't trigger the stop operation |
| 946 | */ |
| 947 | void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep) |
| 948 | { |
Takashi Iwai | d0f09d1 | 2020-11-23 09:53:35 +0100 | [diff] [blame] | 949 | if (ep) |
Takashi Iwai | f58161b | 2012-11-08 08:52:45 +0100 | [diff] [blame] | 950 | wait_clear_urbs(ep); |
| 951 | } |
| 952 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 953 | /* |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 954 | * Stop active urbs |
Takashi Iwai | d0f09d1 | 2020-11-23 09:53:35 +0100 | [diff] [blame] | 955 | * |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 956 | * This function moves the EP to STOPPING state if it's being RUNNING. |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 957 | */ |
Takashi Iwai | 813a17c | 2021-09-29 10:08:44 +0200 | [diff] [blame] | 958 | static int stop_urbs(struct snd_usb_endpoint *ep, bool force, bool keep_pending) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 959 | { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 960 | unsigned int i; |
Takashi Iwai | 0ef7436 | 2021-09-29 10:08:42 +0200 | [diff] [blame] | 961 | unsigned long flags; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 962 | |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 963 | if (!force && atomic_read(&ep->running)) |
Takashi Iwai | d0f09d1 | 2020-11-23 09:53:35 +0100 | [diff] [blame] | 964 | return -EBUSY; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 965 | |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 966 | if (!ep_state_update(ep, EP_STATE_RUNNING, EP_STATE_STOPPING)) |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 967 | return 0; |
Takashi Iwai | d0f09d1 | 2020-11-23 09:53:35 +0100 | [diff] [blame] | 968 | |
Takashi Iwai | 0ef7436 | 2021-09-29 10:08:42 +0200 | [diff] [blame] | 969 | spin_lock_irqsave(&ep->lock, flags); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 970 | INIT_LIST_HEAD(&ep->ready_playback_urbs); |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 971 | ep->next_packet_head = 0; |
| 972 | ep->next_packet_queued = 0; |
Takashi Iwai | 0ef7436 | 2021-09-29 10:08:42 +0200 | [diff] [blame] | 973 | spin_unlock_irqrestore(&ep->lock, flags); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 974 | |
Takashi Iwai | 813a17c | 2021-09-29 10:08:44 +0200 | [diff] [blame] | 975 | if (keep_pending) |
| 976 | return 0; |
| 977 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 978 | for (i = 0; i < ep->nurbs; i++) { |
| 979 | if (test_bit(i, &ep->active_mask)) { |
| 980 | if (!test_and_set_bit(i, &ep->unlink_mask)) { |
| 981 | struct urb *u = ep->urb[i].urb; |
Takashi Iwai | ccc1696 | 2012-11-21 08:22:52 +0100 | [diff] [blame] | 982 | usb_unlink_urb(u); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * release an endpoint's urbs |
| 992 | */ |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 993 | static int release_urbs(struct snd_usb_endpoint *ep, bool force) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 994 | { |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 995 | int i, err; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 996 | |
| 997 | /* route incoming urbs to nirvana */ |
Takashi Iwai | 96e221f | 2020-11-23 09:53:28 +0100 | [diff] [blame] | 998 | snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 999 | |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 1000 | /* stop and unlink urbs */ |
Takashi Iwai | 813a17c | 2021-09-29 10:08:44 +0200 | [diff] [blame] | 1001 | err = stop_urbs(ep, force, false); |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 1002 | if (err) |
| 1003 | return err; |
| 1004 | |
| 1005 | wait_clear_urbs(ep); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1006 | |
| 1007 | for (i = 0; i < ep->nurbs; i++) |
| 1008 | release_urb_ctx(&ep->urb[i]); |
| 1009 | |
Xu Wang | 2e5a8e1 | 2020-07-27 02:52:08 +0000 | [diff] [blame] | 1010 | usb_free_coherent(ep->chip->dev, SYNC_URBS * 4, |
| 1011 | ep->syncbuf, ep->sync_dma); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1012 | |
| 1013 | ep->syncbuf = NULL; |
| 1014 | ep->nurbs = 0; |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 1015 | return 0; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1018 | /* |
| 1019 | * configure a data endpoint |
| 1020 | */ |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1021 | static int data_ep_set_params(struct snd_usb_endpoint *ep) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1022 | { |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1023 | struct snd_usb_audio *chip = ep->chip; |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1024 | unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb; |
| 1025 | unsigned int max_packs_per_period, urbs_per_period, urb_packs; |
| 1026 | unsigned int max_urbs, i; |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1027 | const struct audioformat *fmt = ep->cur_audiofmt; |
| 1028 | int frame_bits = ep->cur_frame_bytes * 8; |
Takashi Iwai | c1b034a | 2021-07-29 09:38:50 +0200 | [diff] [blame] | 1029 | int tx_length_quirk = (has_tx_length_quirk(chip) && |
Ricard Wanderlof | 759c90f | 2015-10-19 08:52:54 +0200 | [diff] [blame] | 1030 | usb_pipeout(ep->pipe)); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1031 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1032 | usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n", |
| 1033 | ep->ep_num, ep->pipe); |
| 1034 | |
| 1035 | if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) { |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1036 | /* |
| 1037 | * When operating in DSD DOP mode, the size of a sample frame |
| 1038 | * in hardware differs from the actual physical format width |
| 1039 | * because we need to make room for the DOP markers. |
| 1040 | */ |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1041 | frame_bits += ep->cur_channels << 3; |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1044 | ep->datainterval = fmt->datainterval; |
| 1045 | ep->stride = frame_bits >> 3; |
Nobutaka Okabe | 0120073 | 2016-12-13 02:52:58 +0900 | [diff] [blame] | 1046 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1047 | switch (ep->cur_format) { |
Nobutaka Okabe | 0120073 | 2016-12-13 02:52:58 +0900 | [diff] [blame] | 1048 | case SNDRV_PCM_FORMAT_U8: |
| 1049 | ep->silence_value = 0x80; |
| 1050 | break; |
| 1051 | case SNDRV_PCM_FORMAT_DSD_U8: |
| 1052 | case SNDRV_PCM_FORMAT_DSD_U16_LE: |
| 1053 | case SNDRV_PCM_FORMAT_DSD_U32_LE: |
| 1054 | case SNDRV_PCM_FORMAT_DSD_U16_BE: |
| 1055 | case SNDRV_PCM_FORMAT_DSD_U32_BE: |
| 1056 | ep->silence_value = 0x69; |
| 1057 | break; |
| 1058 | default: |
| 1059 | ep->silence_value = 0; |
| 1060 | } |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1061 | |
Andreas Pape | fd1a505 | 2016-12-06 14:46:14 +0900 | [diff] [blame] | 1062 | /* assume max. frequency is 50% higher than nominal */ |
| 1063 | ep->freqmax = ep->freqn + (ep->freqn >> 1); |
Ricard Wanderlof | ab30965 | 2015-10-11 20:54:51 +0200 | [diff] [blame] | 1064 | /* Round up freqmax to nearest integer in order to calculate maximum |
| 1065 | * packet size, which must represent a whole number of frames. |
| 1066 | * This is accomplished by adding 0x0.ffff before converting the |
| 1067 | * Q16.16 format into integer. |
| 1068 | * In order to accurately calculate the maximum packet size when |
| 1069 | * the data interval is more than 1 (i.e. ep->datainterval > 0), |
| 1070 | * multiply by the data interval prior to rounding. For instance, |
| 1071 | * a freqmax of 41 kHz will result in a max packet size of 6 (5.125) |
| 1072 | * frames with a data interval of 1, but 11 (10.25) frames with a |
| 1073 | * data interval of 2. |
| 1074 | * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the |
| 1075 | * maximum datainterval value of 3, at USB full speed, higher for |
| 1076 | * USB high speed, noting that ep->freqmax is in units of |
| 1077 | * frames per packet in Q16.16 format.) |
| 1078 | */ |
| 1079 | maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) * |
| 1080 | (frame_bits >> 3); |
Ricard Wanderlof | 759c90f | 2015-10-19 08:52:54 +0200 | [diff] [blame] | 1081 | if (tx_length_quirk) |
| 1082 | maxsize += sizeof(__le32); /* Space for length descriptor */ |
Clemens Ladisch | 57e6dae | 2013-08-08 11:24:55 +0200 | [diff] [blame] | 1083 | /* but wMaxPacketSize might reduce this */ |
| 1084 | if (ep->maxpacksize && ep->maxpacksize < maxsize) { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1085 | /* whatever fits into a max. size packet */ |
Ricard Wanderlof | 759c90f | 2015-10-19 08:52:54 +0200 | [diff] [blame] | 1086 | unsigned int data_maxsize = maxsize = ep->maxpacksize; |
| 1087 | |
| 1088 | if (tx_length_quirk) |
| 1089 | /* Need to remove the length descriptor to calc freq */ |
| 1090 | data_maxsize -= sizeof(__le32); |
| 1091 | ep->freqmax = (data_maxsize / (frame_bits >> 3)) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1092 | << (16 - ep->datainterval); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | if (ep->fill_max) |
| 1096 | ep->curpacksize = ep->maxpacksize; |
| 1097 | else |
| 1098 | ep->curpacksize = maxsize; |
| 1099 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1100 | if (snd_usb_get_speed(chip->dev) != USB_SPEED_FULL) { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1101 | packs_per_ms = 8 >> ep->datainterval; |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1102 | max_packs_per_urb = MAX_PACKS_HS; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1103 | } else { |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1104 | packs_per_ms = 1; |
| 1105 | max_packs_per_urb = MAX_PACKS; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1106 | } |
Takashi Iwai | 53837b4 | 2020-11-23 09:53:39 +0100 | [diff] [blame] | 1107 | if (ep->sync_source && !ep->implicit_fb_sync) |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1108 | max_packs_per_urb = min(max_packs_per_urb, |
Takashi Iwai | 53837b4 | 2020-11-23 09:53:39 +0100 | [diff] [blame] | 1109 | 1U << ep->sync_source->syncinterval); |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1110 | max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1111 | |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1112 | /* |
| 1113 | * Capture endpoints need to use small URBs because there's no way |
| 1114 | * to tell in advance where the next period will end, and we don't |
| 1115 | * want the next URB to complete much after the period ends. |
| 1116 | * |
| 1117 | * Playback endpoints with implicit sync much use the same parameters |
| 1118 | * as their corresponding capture endpoint. |
| 1119 | */ |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1120 | if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) { |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1121 | |
Thomas Pugliese | a93455e | 2013-11-26 13:58:15 -0600 | [diff] [blame] | 1122 | urb_packs = packs_per_ms; |
| 1123 | /* |
| 1124 | * Wireless devices can poll at a max rate of once per 4ms. |
| 1125 | * For dataintervals less than 5, increase the packet count to |
| 1126 | * allow the host controller to use bursting to fill in the |
| 1127 | * gaps. |
| 1128 | */ |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1129 | if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) { |
Thomas Pugliese | a93455e | 2013-11-26 13:58:15 -0600 | [diff] [blame] | 1130 | int interval = ep->datainterval; |
| 1131 | while (interval < 5) { |
| 1132 | urb_packs <<= 1; |
| 1133 | ++interval; |
| 1134 | } |
| 1135 | } |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1136 | /* make capture URBs <= 1 ms and smaller than a period */ |
Thomas Pugliese | a93455e | 2013-11-26 13:58:15 -0600 | [diff] [blame] | 1137 | urb_packs = min(max_packs_per_urb, urb_packs); |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1138 | while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes) |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1139 | urb_packs >>= 1; |
| 1140 | ep->nurbs = MAX_URBS; |
| 1141 | |
| 1142 | /* |
| 1143 | * Playback endpoints without implicit sync are adjusted so that |
| 1144 | * a period fits as evenly as possible in the smallest number of |
| 1145 | * URBs. The total number of URBs is adjusted to the size of the |
| 1146 | * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits. |
| 1147 | */ |
| 1148 | } else { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1149 | /* determine how small a packet can be */ |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1150 | minsize = (ep->freqn >> (16 - ep->datainterval)) * |
| 1151 | (frame_bits >> 3); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1152 | /* with sync from device, assume it can be 12% lower */ |
Takashi Iwai | 53837b4 | 2020-11-23 09:53:39 +0100 | [diff] [blame] | 1153 | if (ep->sync_source) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1154 | minsize -= minsize >> 3; |
| 1155 | minsize = max(minsize, 1u); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1156 | |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1157 | /* how many packets will contain an entire ALSA period? */ |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1158 | max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize); |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1159 | |
| 1160 | /* how many URBs will contain a period? */ |
| 1161 | urbs_per_period = DIV_ROUND_UP(max_packs_per_period, |
| 1162 | max_packs_per_urb); |
| 1163 | /* how many packets are needed in each URB? */ |
| 1164 | urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period); |
| 1165 | |
| 1166 | /* limit the number of frames in a single URB */ |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1167 | ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames, |
| 1168 | urbs_per_period); |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1169 | |
| 1170 | /* try to use enough URBs to contain an entire ALSA buffer */ |
| 1171 | max_urbs = min((unsigned) MAX_URBS, |
| 1172 | MAX_QUEUE * packs_per_ms / urb_packs); |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1173 | ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /* allocate and initialize data urbs */ |
| 1177 | for (i = 0; i < ep->nurbs; i++) { |
| 1178 | struct snd_urb_ctx *u = &ep->urb[i]; |
| 1179 | u->index = i; |
| 1180 | u->ep = ep; |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1181 | u->packets = urb_packs; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1182 | u->buffer_size = maxsize * u->packets; |
| 1183 | |
| 1184 | if (fmt->fmt_type == UAC_FORMAT_TYPE_II) |
| 1185 | u->packets++; /* for transfer delimiter */ |
| 1186 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); |
| 1187 | if (!u->urb) |
| 1188 | goto out_of_memory; |
| 1189 | |
| 1190 | u->urb->transfer_buffer = |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1191 | usb_alloc_coherent(chip->dev, u->buffer_size, |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1192 | GFP_KERNEL, &u->urb->transfer_dma); |
| 1193 | if (!u->urb->transfer_buffer) |
| 1194 | goto out_of_memory; |
| 1195 | u->urb->pipe = ep->pipe; |
Clemens Ladisch | c75c5ab | 2013-04-27 12:10:32 +0200 | [diff] [blame] | 1196 | u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1197 | u->urb->interval = 1 << ep->datainterval; |
| 1198 | u->urb->context = u; |
| 1199 | u->urb->complete = snd_complete_urb; |
| 1200 | INIT_LIST_HEAD(&u->ready_list); |
| 1201 | } |
| 1202 | |
| 1203 | return 0; |
| 1204 | |
| 1205 | out_of_memory: |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 1206 | release_urbs(ep, false); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1207 | return -ENOMEM; |
| 1208 | } |
| 1209 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1210 | /* |
| 1211 | * configure a sync endpoint |
| 1212 | */ |
Eldad Zack | 9372103 | 2013-10-06 22:31:06 +0200 | [diff] [blame] | 1213 | static int sync_ep_set_params(struct snd_usb_endpoint *ep) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1214 | { |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1215 | struct snd_usb_audio *chip = ep->chip; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1216 | int i; |
| 1217 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1218 | usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n", |
| 1219 | ep->ep_num, ep->pipe); |
| 1220 | |
| 1221 | ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4, |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1222 | GFP_KERNEL, &ep->sync_dma); |
| 1223 | if (!ep->syncbuf) |
| 1224 | return -ENOMEM; |
| 1225 | |
| 1226 | for (i = 0; i < SYNC_URBS; i++) { |
| 1227 | struct snd_urb_ctx *u = &ep->urb[i]; |
| 1228 | u->index = i; |
| 1229 | u->ep = ep; |
| 1230 | u->packets = 1; |
| 1231 | u->urb = usb_alloc_urb(1, GFP_KERNEL); |
| 1232 | if (!u->urb) |
| 1233 | goto out_of_memory; |
| 1234 | u->urb->transfer_buffer = ep->syncbuf + i * 4; |
| 1235 | u->urb->transfer_dma = ep->sync_dma + i * 4; |
| 1236 | u->urb->transfer_buffer_length = 4; |
| 1237 | u->urb->pipe = ep->pipe; |
Clemens Ladisch | c75c5ab | 2013-04-27 12:10:32 +0200 | [diff] [blame] | 1238 | u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1239 | u->urb->number_of_packets = 1; |
| 1240 | u->urb->interval = 1 << ep->syncinterval; |
| 1241 | u->urb->context = u; |
| 1242 | u->urb->complete = snd_complete_urb; |
| 1243 | } |
| 1244 | |
| 1245 | ep->nurbs = SYNC_URBS; |
| 1246 | |
| 1247 | return 0; |
| 1248 | |
| 1249 | out_of_memory: |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 1250 | release_urbs(ep, false); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1251 | return -ENOMEM; |
| 1252 | } |
| 1253 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1254 | /* |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 1255 | * snd_usb_endpoint_set_params: configure an snd_usb_endpoint |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1256 | * |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 1257 | * Determine the number of URBs to be used on this endpoint. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1258 | * An endpoint must be configured before it can be started. |
| 1259 | * An endpoint that is already running can not be reconfigured. |
| 1260 | */ |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1261 | static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip, |
| 1262 | struct snd_usb_endpoint *ep) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1263 | { |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1264 | const struct audioformat *fmt = ep->cur_audiofmt; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1265 | int err; |
| 1266 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1267 | /* release old buffers, if any */ |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 1268 | err = release_urbs(ep, false); |
| 1269 | if (err < 0) |
| 1270 | return err; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1271 | |
| 1272 | ep->datainterval = fmt->datainterval; |
| 1273 | ep->maxpacksize = fmt->maxpacksize; |
Takashi Iwai | 85f7193 | 2012-04-13 12:41:54 +0200 | [diff] [blame] | 1274 | ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1275 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1276 | if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) { |
| 1277 | ep->freqn = get_usb_full_speed_rate(ep->cur_rate); |
Alexander Tsoy | b9fd200 | 2020-06-29 05:59:34 +0300 | [diff] [blame] | 1278 | ep->pps = 1000 >> ep->datainterval; |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 1279 | } else { |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1280 | ep->freqn = get_usb_high_speed_rate(ep->cur_rate); |
Alexander Tsoy | b9fd200 | 2020-06-29 05:59:34 +0300 | [diff] [blame] | 1281 | ep->pps = 8000 >> ep->datainterval; |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 1282 | } |
| 1283 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1284 | ep->sample_rem = ep->cur_rate % ep->pps; |
| 1285 | ep->packsize[0] = ep->cur_rate / ep->pps; |
| 1286 | ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1287 | |
| 1288 | /* calculate the frequency in 16.16 format */ |
| 1289 | ep->freqm = ep->freqn; |
| 1290 | ep->freqshift = INT_MIN; |
| 1291 | |
| 1292 | ep->phase = 0; |
| 1293 | |
| 1294 | switch (ep->type) { |
| 1295 | case SND_USB_ENDPOINT_TYPE_DATA: |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1296 | err = data_ep_set_params(ep); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1297 | break; |
| 1298 | case SND_USB_ENDPOINT_TYPE_SYNC: |
Eldad Zack | 9372103 | 2013-10-06 22:31:06 +0200 | [diff] [blame] | 1299 | err = sync_ep_set_params(ep); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1300 | break; |
| 1301 | default: |
| 1302 | err = -EINVAL; |
| 1303 | } |
| 1304 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1305 | usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1306 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1307 | if (err < 0) |
| 1308 | return err; |
| 1309 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1310 | /* some unit conversions in runtime */ |
| 1311 | ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes; |
| 1312 | ep->curframesize = ep->curpacksize / ep->cur_frame_bytes; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1313 | |
| 1314 | return 0; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1317 | /* |
| 1318 | * snd_usb_endpoint_configure: Configure the endpoint |
| 1319 | * |
| 1320 | * This function sets up the EP to be fully usable state. |
| 1321 | * It's called either from hw_params or prepare callback. |
gushengxian | ff630b6 | 2021-07-05 05:00:52 -0700 | [diff] [blame] | 1322 | * The function checks need_setup flag, and performs nothing unless needed, |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1323 | * so it's safe to call this multiple times. |
| 1324 | * |
| 1325 | * This returns zero if unchanged, 1 if the configuration has changed, |
| 1326 | * or a negative error code. |
| 1327 | */ |
| 1328 | int snd_usb_endpoint_configure(struct snd_usb_audio *chip, |
| 1329 | struct snd_usb_endpoint *ep) |
| 1330 | { |
| 1331 | bool iface_first; |
| 1332 | int err = 0; |
| 1333 | |
| 1334 | mutex_lock(&chip->mutex); |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 1335 | if (WARN_ON(!ep->iface_ref)) |
| 1336 | goto unlock; |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1337 | if (!ep->need_setup) |
| 1338 | goto unlock; |
| 1339 | |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 1340 | /* If the interface has been already set up, just set EP parameters */ |
| 1341 | if (!ep->iface_ref->need_setup) { |
Takashi Iwai | 3784d44 | 2021-01-18 08:58:15 +0100 | [diff] [blame] | 1342 | /* sample rate setup of UAC1 is per endpoint, and we need |
| 1343 | * to update at each EP configuration |
| 1344 | */ |
| 1345 | if (ep->cur_audiofmt->protocol == UAC_VERSION_1) { |
| 1346 | err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, |
| 1347 | ep->cur_rate); |
| 1348 | if (err < 0) |
| 1349 | goto unlock; |
| 1350 | } |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1351 | err = snd_usb_endpoint_set_params(chip, ep); |
| 1352 | if (err < 0) |
| 1353 | goto unlock; |
| 1354 | goto done; |
| 1355 | } |
| 1356 | |
| 1357 | /* Need to deselect altsetting at first */ |
| 1358 | endpoint_set_interface(chip, ep, false); |
| 1359 | |
| 1360 | /* Some UAC1 devices (e.g. Yamaha THR10) need the host interface |
| 1361 | * to be set up before parameter setups |
| 1362 | */ |
| 1363 | iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1; |
Takashi Iwai | 6e41340 | 2021-08-24 07:57:20 +0200 | [diff] [blame] | 1364 | /* Workaround for devices that require the interface setup at first like UAC1 */ |
| 1365 | if (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST) |
Takashi Iwai | 7af5a14 | 2021-08-24 07:47:00 +0200 | [diff] [blame] | 1366 | iface_first = true; |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1367 | if (iface_first) { |
| 1368 | err = endpoint_set_interface(chip, ep, true); |
| 1369 | if (err < 0) |
| 1370 | goto unlock; |
| 1371 | } |
| 1372 | |
| 1373 | err = snd_usb_init_pitch(chip, ep->cur_audiofmt); |
| 1374 | if (err < 0) |
| 1375 | goto unlock; |
| 1376 | |
| 1377 | err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate); |
| 1378 | if (err < 0) |
| 1379 | goto unlock; |
| 1380 | |
| 1381 | err = snd_usb_endpoint_set_params(chip, ep); |
| 1382 | if (err < 0) |
| 1383 | goto unlock; |
| 1384 | |
| 1385 | err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt); |
| 1386 | if (err < 0) |
| 1387 | goto unlock; |
| 1388 | |
| 1389 | /* for UAC2/3, enable the interface altset here at last */ |
| 1390 | if (!iface_first) { |
| 1391 | err = endpoint_set_interface(chip, ep, true); |
| 1392 | if (err < 0) |
| 1393 | goto unlock; |
| 1394 | } |
| 1395 | |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 1396 | ep->iface_ref->need_setup = false; |
| 1397 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1398 | done: |
| 1399 | ep->need_setup = false; |
| 1400 | err = 1; |
| 1401 | |
| 1402 | unlock: |
| 1403 | mutex_unlock(&chip->mutex); |
| 1404 | return err; |
| 1405 | } |
| 1406 | |
Takashi Iwai | 4e7cf1f | 2021-09-29 10:08:36 +0200 | [diff] [blame] | 1407 | /* get the current rate set to the given clock by any endpoint */ |
| 1408 | int snd_usb_endpoint_get_clock_rate(struct snd_usb_audio *chip, int clock) |
| 1409 | { |
| 1410 | struct snd_usb_endpoint *ep; |
| 1411 | int rate = 0; |
| 1412 | |
| 1413 | if (!clock) |
| 1414 | return 0; |
| 1415 | mutex_lock(&chip->mutex); |
| 1416 | list_for_each_entry(ep, &chip->ep_list, list) { |
| 1417 | if (ep->cur_clock == clock && ep->cur_rate) { |
| 1418 | rate = ep->cur_rate; |
| 1419 | break; |
| 1420 | } |
| 1421 | } |
| 1422 | mutex_unlock(&chip->mutex); |
| 1423 | return rate; |
| 1424 | } |
| 1425 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1426 | /** |
| 1427 | * snd_usb_endpoint_start: start an snd_usb_endpoint |
| 1428 | * |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 1429 | * @ep: the endpoint to start |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1430 | * |
Takashi Iwai | 43b81e8 | 2020-11-23 09:53:34 +0100 | [diff] [blame] | 1431 | * A call to this function will increment the running count of the endpoint. |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 1432 | * In case it is not already running, the URBs for this endpoint will be |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1433 | * submitted. Otherwise, this function does nothing. |
| 1434 | * |
| 1435 | * Must be balanced to calls of snd_usb_endpoint_stop(). |
| 1436 | * |
| 1437 | * Returns an error if the URB submission failed, 0 in all other cases. |
| 1438 | */ |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 1439 | int snd_usb_endpoint_start(struct snd_usb_endpoint *ep) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1440 | { |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 1441 | bool is_playback = usb_pipeout(ep->pipe); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1442 | int err; |
| 1443 | unsigned int i; |
| 1444 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 1445 | if (atomic_read(&ep->chip->shutdown)) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1446 | return -EBADFD; |
| 1447 | |
Takashi Iwai | 53837b4 | 2020-11-23 09:53:39 +0100 | [diff] [blame] | 1448 | if (ep->sync_source) |
| 1449 | WRITE_ONCE(ep->sync_source->sync_sink, ep); |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1450 | |
Takashi Iwai | 43b81e8 | 2020-11-23 09:53:34 +0100 | [diff] [blame] | 1451 | usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (running %d)\n", |
| 1452 | ep_type_name(ep->type), ep->ep_num, |
| 1453 | atomic_read(&ep->running)); |
Takashi Iwai | 57234bc | 2020-11-23 09:53:27 +0100 | [diff] [blame] | 1454 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1455 | /* already running? */ |
Takashi Iwai | 43b81e8 | 2020-11-23 09:53:34 +0100 | [diff] [blame] | 1456 | if (atomic_inc_return(&ep->running) != 1) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1457 | return 0; |
| 1458 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1459 | ep->active_mask = 0; |
| 1460 | ep->unlink_mask = 0; |
| 1461 | ep->phase = 0; |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 1462 | ep->sample_accum = 0; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1463 | |
Daniel Mack | 2b58fd5 | 2012-09-04 10:23:07 +0200 | [diff] [blame] | 1464 | snd_usb_endpoint_start_quirk(ep); |
| 1465 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1466 | /* |
| 1467 | * If this endpoint has a data endpoint as implicit feedback source, |
| 1468 | * don't start the urbs here. Instead, mark them all as available, |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 1469 | * wait for the record urbs to return and queue the playback urbs |
| 1470 | * from that context. |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1471 | */ |
| 1472 | |
Takashi Iwai | 5c2b301 | 2021-02-06 21:30:51 +0100 | [diff] [blame] | 1473 | if (!ep_state_update(ep, EP_STATE_STOPPED, EP_STATE_RUNNING)) |
| 1474 | goto __error; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1475 | |
Takashi Iwai | ebe8dc5 | 2021-04-14 10:32:55 +0200 | [diff] [blame] | 1476 | if (snd_usb_endpoint_implicit_feedback_sink(ep) && |
Takashi Iwai | 019c7f9 | 2021-07-29 09:38:51 +0200 | [diff] [blame] | 1477 | !(ep->chip->quirk_flags & QUIRK_FLAG_PLAYBACK_FIRST)) { |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1478 | usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n"); |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 1479 | i = 0; |
| 1480 | goto fill_rest; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | for (i = 0; i < ep->nurbs; i++) { |
| 1484 | struct urb *urb = ep->urb[i].urb; |
| 1485 | |
| 1486 | if (snd_BUG_ON(!urb)) |
| 1487 | goto __error; |
| 1488 | |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 1489 | if (is_playback) |
| 1490 | err = prepare_outbound_urb(ep, urb->context, true); |
| 1491 | else |
| 1492 | err = prepare_inbound_urb(ep, urb->context); |
| 1493 | if (err < 0) { |
| 1494 | /* stop filling at applptr */ |
| 1495 | if (err == -EAGAIN) |
| 1496 | break; |
| 1497 | usb_audio_dbg(ep->chip, |
| 1498 | "EP 0x%x: failed to prepare urb: %d\n", |
| 1499 | ep->ep_num, err); |
| 1500 | goto __error; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 1504 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1505 | usb_audio_err(ep->chip, |
| 1506 | "cannot submit urb %d, error %d: %s\n", |
| 1507 | i, err, usb_error_string(err)); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1508 | goto __error; |
| 1509 | } |
| 1510 | set_bit(i, &ep->active_mask); |
Takashi Iwai | 86a42ad | 2021-09-29 10:08:37 +0200 | [diff] [blame] | 1511 | atomic_inc(&ep->submitted_urbs); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1512 | } |
| 1513 | |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 1514 | if (!i) { |
| 1515 | usb_audio_dbg(ep->chip, "XRUN at starting EP 0x%x\n", |
| 1516 | ep->ep_num); |
| 1517 | goto __error; |
| 1518 | } |
| 1519 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1520 | usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n", |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 1521 | i, ep->ep_num); |
| 1522 | |
| 1523 | fill_rest: |
| 1524 | /* put the remaining URBs to ready list */ |
| 1525 | if (is_playback) { |
| 1526 | for (; i < ep->nurbs; i++) |
| 1527 | push_back_to_ready_list(ep, ep->urb + i); |
| 1528 | } |
| 1529 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1530 | return 0; |
| 1531 | |
| 1532 | __error: |
Takashi Iwai | 813a17c | 2021-09-29 10:08:44 +0200 | [diff] [blame] | 1533 | snd_usb_endpoint_stop(ep, false); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1534 | return -EPIPE; |
| 1535 | } |
| 1536 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1537 | /** |
| 1538 | * snd_usb_endpoint_stop: stop an snd_usb_endpoint |
| 1539 | * |
| 1540 | * @ep: the endpoint to stop (may be NULL) |
Takashi Iwai | 813a17c | 2021-09-29 10:08:44 +0200 | [diff] [blame] | 1541 | * @keep_pending: keep in-flight URBs |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1542 | * |
Takashi Iwai | 43b81e8 | 2020-11-23 09:53:34 +0100 | [diff] [blame] | 1543 | * A call to this function will decrement the running count of the endpoint. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1544 | * In case the last user has requested the endpoint stop, the URBs will |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 1545 | * actually be deactivated. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1546 | * |
| 1547 | * Must be balanced to calls of snd_usb_endpoint_start(). |
Takashi Iwai | b2eb950 | 2012-11-21 08:30:48 +0100 | [diff] [blame] | 1548 | * |
| 1549 | * The caller needs to synchronize the pending stop operation via |
| 1550 | * snd_usb_endpoint_sync_pending_stop(). |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1551 | */ |
Takashi Iwai | 813a17c | 2021-09-29 10:08:44 +0200 | [diff] [blame] | 1552 | void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, bool keep_pending) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1553 | { |
| 1554 | if (!ep) |
| 1555 | return; |
| 1556 | |
Takashi Iwai | 43b81e8 | 2020-11-23 09:53:34 +0100 | [diff] [blame] | 1557 | usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (running %d)\n", |
| 1558 | ep_type_name(ep->type), ep->ep_num, |
| 1559 | atomic_read(&ep->running)); |
Takashi Iwai | 57234bc | 2020-11-23 09:53:27 +0100 | [diff] [blame] | 1560 | |
Takashi Iwai | 43b81e8 | 2020-11-23 09:53:34 +0100 | [diff] [blame] | 1561 | if (snd_BUG_ON(!atomic_read(&ep->running))) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1562 | return; |
| 1563 | |
Takashi Iwai | 988cc17 | 2021-04-26 08:33:49 +0200 | [diff] [blame] | 1564 | if (!atomic_dec_return(&ep->running)) { |
| 1565 | if (ep->sync_source) |
| 1566 | WRITE_ONCE(ep->sync_source->sync_sink, NULL); |
Takashi Iwai | 813a17c | 2021-09-29 10:08:44 +0200 | [diff] [blame] | 1567 | stop_urbs(ep, false, keep_pending); |
Takashi Iwai | 988cc17 | 2021-04-26 08:33:49 +0200 | [diff] [blame] | 1568 | } |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1571 | /** |
Takashi Iwai | 92a586b | 2014-06-25 14:24:47 +0200 | [diff] [blame] | 1572 | * snd_usb_endpoint_release: Tear down an snd_usb_endpoint |
| 1573 | * |
| 1574 | * @ep: the endpoint to release |
| 1575 | * |
Takashi Iwai | 43b81e8 | 2020-11-23 09:53:34 +0100 | [diff] [blame] | 1576 | * This function does not care for the endpoint's running count but will tear |
Takashi Iwai | 92a586b | 2014-06-25 14:24:47 +0200 | [diff] [blame] | 1577 | * down all the streaming URBs immediately. |
| 1578 | */ |
| 1579 | void snd_usb_endpoint_release(struct snd_usb_endpoint *ep) |
| 1580 | { |
Takashi Iwai | d6cda46 | 2021-02-06 21:30:50 +0100 | [diff] [blame] | 1581 | release_urbs(ep, true); |
Takashi Iwai | 92a586b | 2014-06-25 14:24:47 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /** |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 1585 | * snd_usb_endpoint_free_all: Free the resources of an snd_usb_endpoint |
Takashi Iwai | 036f90d | 2021-02-05 09:28:37 +0100 | [diff] [blame] | 1586 | * @chip: The chip |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1587 | * |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 1588 | * This free all endpoints and those resources |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1589 | */ |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 1590 | void snd_usb_endpoint_free_all(struct snd_usb_audio *chip) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1591 | { |
Takashi Iwai | 00272c6 | 2021-01-08 08:52:17 +0100 | [diff] [blame] | 1592 | struct snd_usb_endpoint *ep, *en; |
| 1593 | struct snd_usb_iface_ref *ip, *in; |
| 1594 | |
| 1595 | list_for_each_entry_safe(ep, en, &chip->ep_list, list) |
| 1596 | kfree(ep); |
| 1597 | |
| 1598 | list_for_each_entry_safe(ip, in, &chip->iface_ref_list, list) |
| 1599 | kfree(ip); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1602 | /* |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1603 | * snd_usb_handle_sync_urb: parse an USB sync packet |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1604 | * |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1605 | * @ep: the endpoint to handle the packet |
| 1606 | * @sender: the sending endpoint |
| 1607 | * @urb: the received packet |
| 1608 | * |
| 1609 | * This function is called from the context of an endpoint that received |
| 1610 | * the packet and is used to let another endpoint object handle the payload. |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1611 | */ |
Takashi Iwai | bf6313a | 2020-11-23 09:53:31 +0100 | [diff] [blame] | 1612 | static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, |
| 1613 | struct snd_usb_endpoint *sender, |
| 1614 | const struct urb *urb) |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1615 | { |
| 1616 | int shift; |
| 1617 | unsigned int f; |
| 1618 | unsigned long flags; |
| 1619 | |
| 1620 | snd_BUG_ON(ep == sender); |
| 1621 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1622 | /* |
| 1623 | * In case the endpoint is operating in implicit feedback mode, prepare |
Daniel Mack | 07a5e9d | 2012-04-24 19:31:24 +0200 | [diff] [blame] | 1624 | * a new outbound URB that has the same layout as the received packet |
| 1625 | * and add it to the list of pending urbs. queue_pending_output_urbs() |
| 1626 | * will take care of them later. |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1627 | */ |
Eldad Zack | 98ae472 | 2013-04-03 23:18:52 +0200 | [diff] [blame] | 1628 | if (snd_usb_endpoint_implicit_feedback_sink(ep) && |
Takashi Iwai | 43b81e8 | 2020-11-23 09:53:34 +0100 | [diff] [blame] | 1629 | atomic_read(&ep->running)) { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1630 | |
| 1631 | /* implicit feedback case */ |
| 1632 | int i, bytes = 0; |
| 1633 | struct snd_urb_ctx *in_ctx; |
| 1634 | struct snd_usb_packet_info *out_packet; |
| 1635 | |
| 1636 | in_ctx = urb->context; |
| 1637 | |
| 1638 | /* Count overall packet size */ |
| 1639 | for (i = 0; i < in_ctx->packets; i++) |
| 1640 | if (urb->iso_frame_desc[i].status == 0) |
| 1641 | bytes += urb->iso_frame_desc[i].actual_length; |
| 1642 | |
| 1643 | /* |
| 1644 | * skip empty packets. At least M-Audio's Fast Track Ultra stops |
| 1645 | * streaming once it received a 0-byte OUT URB |
| 1646 | */ |
| 1647 | if (bytes == 0) |
| 1648 | return; |
| 1649 | |
| 1650 | spin_lock_irqsave(&ep->lock, flags); |
Takashi Iwai | c15871e | 2020-11-23 09:53:32 +0100 | [diff] [blame] | 1651 | if (ep->next_packet_queued >= ARRAY_SIZE(ep->next_packet)) { |
| 1652 | spin_unlock_irqrestore(&ep->lock, flags); |
| 1653 | usb_audio_err(ep->chip, |
| 1654 | "next package FIFO overflow EP 0x%x\n", |
| 1655 | ep->ep_num); |
| 1656 | notify_xrun(ep); |
| 1657 | return; |
| 1658 | } |
| 1659 | |
| 1660 | out_packet = next_packet_fifo_enqueue(ep); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1661 | |
| 1662 | /* |
| 1663 | * Iterate through the inbound packet and prepare the lengths |
| 1664 | * for the output packet. The OUT packet we are about to send |
Eldad Zack | 28acb12 | 2012-11-28 23:55:34 +0100 | [diff] [blame] | 1665 | * will have the same amount of payload bytes per stride as the |
| 1666 | * IN packet we just received. Since the actual size is scaled |
| 1667 | * by the stride, use the sender stride to calculate the length |
| 1668 | * in case the number of channels differ between the implicitly |
| 1669 | * fed-back endpoint and the synchronizing endpoint. |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1670 | */ |
| 1671 | |
| 1672 | out_packet->packets = in_ctx->packets; |
| 1673 | for (i = 0; i < in_ctx->packets; i++) { |
| 1674 | if (urb->iso_frame_desc[i].status == 0) |
| 1675 | out_packet->packet_size[i] = |
Eldad Zack | 28acb12 | 2012-11-28 23:55:34 +0100 | [diff] [blame] | 1676 | urb->iso_frame_desc[i].actual_length / sender->stride; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1677 | else |
| 1678 | out_packet->packet_size[i] = 0; |
| 1679 | } |
| 1680 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1681 | spin_unlock_irqrestore(&ep->lock, flags); |
Takashi Iwai | d5f871f | 2021-09-29 10:08:43 +0200 | [diff] [blame] | 1682 | snd_usb_queue_pending_output_urbs(ep, false); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1683 | |
| 1684 | return; |
| 1685 | } |
| 1686 | |
Daniel Mack | 94c2721 | 2012-04-12 13:51:15 +0200 | [diff] [blame] | 1687 | /* |
| 1688 | * process after playback sync complete |
| 1689 | * |
| 1690 | * Full speed devices report feedback values in 10.14 format as samples |
| 1691 | * per frame, high speed devices in 16.16 format as samples per |
| 1692 | * microframe. |
| 1693 | * |
| 1694 | * Because the Audio Class 1 spec was written before USB 2.0, many high |
| 1695 | * speed devices use a wrong interpretation, some others use an |
| 1696 | * entirely different format. |
| 1697 | * |
| 1698 | * Therefore, we cannot predict what format any particular device uses |
| 1699 | * and must detect it automatically. |
| 1700 | */ |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1701 | |
| 1702 | if (urb->iso_frame_desc[0].status != 0 || |
| 1703 | urb->iso_frame_desc[0].actual_length < 3) |
| 1704 | return; |
| 1705 | |
| 1706 | f = le32_to_cpup(urb->transfer_buffer); |
| 1707 | if (urb->iso_frame_desc[0].actual_length == 3) |
| 1708 | f &= 0x00ffffff; |
| 1709 | else |
| 1710 | f &= 0x0fffffff; |
| 1711 | |
| 1712 | if (f == 0) |
| 1713 | return; |
| 1714 | |
Daniel Mack | ca0dd27 | 2016-08-22 08:53:37 +0200 | [diff] [blame] | 1715 | if (unlikely(sender->tenor_fb_quirk)) { |
Clemens Ladisch | 7040b6d | 2014-05-01 12:20:22 +0200 | [diff] [blame] | 1716 | /* |
Daniel Mack | ca0dd27 | 2016-08-22 08:53:37 +0200 | [diff] [blame] | 1717 | * Devices based on Tenor 8802 chipsets (TEAC UD-H01 |
| 1718 | * and others) sometimes change the feedback value |
Clemens Ladisch | 7040b6d | 2014-05-01 12:20:22 +0200 | [diff] [blame] | 1719 | * by +/- 0x1.0000. |
| 1720 | */ |
| 1721 | if (f < ep->freqn - 0x8000) |
Daniel Mack | 36e1ac3 | 2016-08-22 08:53:38 +0200 | [diff] [blame] | 1722 | f += 0xf000; |
Clemens Ladisch | 7040b6d | 2014-05-01 12:20:22 +0200 | [diff] [blame] | 1723 | else if (f > ep->freqn + 0x8000) |
Daniel Mack | 36e1ac3 | 2016-08-22 08:53:38 +0200 | [diff] [blame] | 1724 | f -= 0xf000; |
Clemens Ladisch | 7040b6d | 2014-05-01 12:20:22 +0200 | [diff] [blame] | 1725 | } else if (unlikely(ep->freqshift == INT_MIN)) { |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame] | 1726 | /* |
| 1727 | * The first time we see a feedback value, determine its format |
| 1728 | * by shifting it left or right until it matches the nominal |
| 1729 | * frequency value. This assumes that the feedback does not |
| 1730 | * differ from the nominal value more than +50% or -25%. |
| 1731 | */ |
| 1732 | shift = 0; |
| 1733 | while (f < ep->freqn - ep->freqn / 4) { |
| 1734 | f <<= 1; |
| 1735 | shift++; |
| 1736 | } |
| 1737 | while (f > ep->freqn + ep->freqn / 2) { |
| 1738 | f >>= 1; |
| 1739 | shift--; |
| 1740 | } |
| 1741 | ep->freqshift = shift; |
| 1742 | } else if (ep->freqshift >= 0) |
| 1743 | f <<= ep->freqshift; |
| 1744 | else |
| 1745 | f >>= -ep->freqshift; |
| 1746 | |
| 1747 | if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) { |
| 1748 | /* |
| 1749 | * If the frequency looks valid, set it. |
| 1750 | * This value is referred to in prepare_playback_urb(). |
| 1751 | */ |
| 1752 | spin_lock_irqsave(&ep->lock, flags); |
| 1753 | ep->freqm = f; |
| 1754 | spin_unlock_irqrestore(&ep->lock, flags); |
| 1755 | } else { |
| 1756 | /* |
| 1757 | * Out of range; maybe the shift value is wrong. |
| 1758 | * Reset it so that we autodetect again the next time. |
| 1759 | */ |
| 1760 | ep->freqshift = INT_MIN; |
| 1761 | } |
| 1762 | } |
| 1763 | |