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