Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 | * |
| 16 | */ |
| 17 | |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 18 | #include <linux/gfp.h> |
| 19 | #include <linux/init.h> |
Takashi Iwai | 80c8a2a | 2012-01-09 11:37:20 +0100 | [diff] [blame] | 20 | #include <linux/ratelimit.h> |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 21 | #include <linux/usb.h> |
| 22 | #include <linux/usb/audio.h> |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 23 | #include <linux/slab.h> |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 24 | |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/pcm.h> |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 27 | #include <sound/pcm_params.h> |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 28 | |
| 29 | #include "usbaudio.h" |
| 30 | #include "helper.h" |
| 31 | #include "card.h" |
| 32 | #include "endpoint.h" |
| 33 | #include "pcm.h" |
| 34 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 35 | #define EP_FLAG_ACTIVATED 0 |
| 36 | #define EP_FLAG_RUNNING 1 |
| 37 | |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 38 | /* |
| 39 | * convert a sampling rate into our full speed format (fs/1000 in Q16.16) |
| 40 | * this will overflow at approx 524 kHz |
| 41 | */ |
| 42 | static inline unsigned get_usb_full_speed_rate(unsigned int rate) |
| 43 | { |
| 44 | return ((rate << 13) + 62) / 125; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * convert a sampling rate into USB high speed format (fs/8000 in Q16.16) |
| 49 | * this will overflow at approx 4 MHz |
| 50 | */ |
| 51 | static inline unsigned get_usb_high_speed_rate(unsigned int rate) |
| 52 | { |
| 53 | return ((rate << 10) + 62) / 125; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * unlink active urbs. |
| 58 | */ |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 59 | static int deactivate_urbs_old(struct snd_usb_substream *subs, int force, int can_sleep) |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 60 | { |
| 61 | struct snd_usb_audio *chip = subs->stream->chip; |
| 62 | unsigned int i; |
| 63 | int async; |
| 64 | |
| 65 | subs->running = 0; |
| 66 | |
| 67 | if (!force && subs->stream->chip->shutdown) /* to be sure... */ |
| 68 | return -EBADFD; |
| 69 | |
| 70 | async = !can_sleep && chip->async_unlink; |
| 71 | |
| 72 | if (!async && in_interrupt()) |
| 73 | return 0; |
| 74 | |
| 75 | for (i = 0; i < subs->nurbs; i++) { |
| 76 | if (test_bit(i, &subs->active_mask)) { |
| 77 | if (!test_and_set_bit(i, &subs->unlink_mask)) { |
| 78 | struct urb *u = subs->dataurb[i].urb; |
| 79 | if (async) |
| 80 | usb_unlink_urb(u); |
| 81 | else |
| 82 | usb_kill_urb(u); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | if (subs->syncpipe) { |
| 87 | for (i = 0; i < SYNC_URBS; i++) { |
| 88 | if (test_bit(i+16, &subs->active_mask)) { |
| 89 | if (!test_and_set_bit(i+16, &subs->unlink_mask)) { |
| 90 | struct urb *u = subs->syncurb[i].urb; |
| 91 | if (async) |
| 92 | usb_unlink_urb(u); |
| 93 | else |
| 94 | usb_kill_urb(u); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /* |
| 104 | * release a urb data |
| 105 | */ |
| 106 | static void release_urb_ctx(struct snd_urb_ctx *u) |
| 107 | { |
| 108 | if (u->urb) { |
| 109 | if (u->buffer_size) |
| 110 | usb_free_coherent(u->subs->dev, u->buffer_size, |
| 111 | u->urb->transfer_buffer, |
| 112 | u->urb->transfer_dma); |
| 113 | usb_free_urb(u->urb); |
| 114 | u->urb = NULL; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * wait until all urbs are processed. |
| 120 | */ |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 121 | static int wait_clear_urbs_old(struct snd_usb_substream *subs) |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 122 | { |
| 123 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); |
| 124 | unsigned int i; |
| 125 | int alive; |
| 126 | |
| 127 | do { |
| 128 | alive = 0; |
| 129 | for (i = 0; i < subs->nurbs; i++) { |
| 130 | if (test_bit(i, &subs->active_mask)) |
| 131 | alive++; |
| 132 | } |
| 133 | if (subs->syncpipe) { |
| 134 | for (i = 0; i < SYNC_URBS; i++) { |
| 135 | if (test_bit(i + 16, &subs->active_mask)) |
| 136 | alive++; |
| 137 | } |
| 138 | } |
| 139 | if (! alive) |
| 140 | break; |
| 141 | schedule_timeout_uninterruptible(1); |
| 142 | } while (time_before(jiffies, end_time)); |
| 143 | if (alive) |
| 144 | snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * release a substream |
| 150 | */ |
| 151 | void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force) |
| 152 | { |
| 153 | int i; |
| 154 | |
| 155 | /* stop urbs (to be sure) */ |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 156 | deactivate_urbs_old(subs, force, 1); |
| 157 | wait_clear_urbs_old(subs); |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 158 | |
| 159 | for (i = 0; i < MAX_URBS; i++) |
| 160 | release_urb_ctx(&subs->dataurb[i]); |
| 161 | for (i = 0; i < SYNC_URBS; i++) |
| 162 | release_urb_ctx(&subs->syncurb[i]); |
| 163 | usb_free_coherent(subs->dev, SYNC_URBS * 4, |
| 164 | subs->syncbuf, subs->sync_dma); |
| 165 | subs->syncbuf = NULL; |
| 166 | subs->nurbs = 0; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * complete callback from data urb |
| 171 | */ |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 172 | static void snd_complete_urb_old(struct urb *urb) |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 173 | { |
| 174 | struct snd_urb_ctx *ctx = urb->context; |
| 175 | struct snd_usb_substream *subs = ctx->subs; |
| 176 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; |
| 177 | int err = 0; |
| 178 | |
| 179 | if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) || |
| 180 | !subs->running || /* can be stopped during retire callback */ |
| 181 | (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 || |
| 182 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 183 | clear_bit(ctx->index, &subs->active_mask); |
| 184 | if (err < 0) { |
| 185 | snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err); |
| 186 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /* |
| 193 | * complete callback from sync urb |
| 194 | */ |
| 195 | static void snd_complete_sync_urb(struct urb *urb) |
| 196 | { |
| 197 | struct snd_urb_ctx *ctx = urb->context; |
| 198 | struct snd_usb_substream *subs = ctx->subs; |
| 199 | struct snd_pcm_substream *substream = ctx->subs->pcm_substream; |
| 200 | int err = 0; |
| 201 | |
| 202 | if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) || |
| 203 | !subs->running || /* can be stopped during retire callback */ |
| 204 | (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 || |
| 205 | (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { |
| 206 | clear_bit(ctx->index + 16, &subs->active_mask); |
| 207 | if (err < 0) { |
| 208 | snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err); |
| 209 | snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* |
| 216 | * initialize a substream for plaback/capture |
| 217 | */ |
| 218 | int snd_usb_init_substream_urbs(struct snd_usb_substream *subs, |
| 219 | unsigned int period_bytes, |
| 220 | unsigned int rate, |
| 221 | unsigned int frame_bits) |
| 222 | { |
| 223 | unsigned int maxsize, i; |
| 224 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; |
| 225 | unsigned int urb_packs, total_packs, packs_per_ms; |
| 226 | struct snd_usb_audio *chip = subs->stream->chip; |
| 227 | |
| 228 | /* calculate the frequency in 16.16 format */ |
| 229 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) |
| 230 | subs->freqn = get_usb_full_speed_rate(rate); |
| 231 | else |
| 232 | subs->freqn = get_usb_high_speed_rate(rate); |
| 233 | subs->freqm = subs->freqn; |
| 234 | subs->freqshift = INT_MIN; |
| 235 | /* calculate max. frequency */ |
| 236 | if (subs->maxpacksize) { |
| 237 | /* whatever fits into a max. size packet */ |
| 238 | maxsize = subs->maxpacksize; |
| 239 | subs->freqmax = (maxsize / (frame_bits >> 3)) |
| 240 | << (16 - subs->datainterval); |
| 241 | } else { |
| 242 | /* no max. packet size: just take 25% higher than nominal */ |
| 243 | subs->freqmax = subs->freqn + (subs->freqn >> 2); |
| 244 | maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3)) |
| 245 | >> (16 - subs->datainterval); |
| 246 | } |
| 247 | subs->phase = 0; |
| 248 | |
| 249 | if (subs->fill_max) |
| 250 | subs->curpacksize = subs->maxpacksize; |
| 251 | else |
| 252 | subs->curpacksize = maxsize; |
| 253 | |
| 254 | if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) |
| 255 | packs_per_ms = 8 >> subs->datainterval; |
| 256 | else |
| 257 | packs_per_ms = 1; |
| 258 | |
| 259 | if (is_playback) { |
| 260 | urb_packs = max(chip->nrpacks, 1); |
| 261 | urb_packs = min(urb_packs, (unsigned int)MAX_PACKS); |
| 262 | } else |
| 263 | urb_packs = 1; |
| 264 | urb_packs *= packs_per_ms; |
| 265 | if (subs->syncpipe) |
| 266 | urb_packs = min(urb_packs, 1U << subs->syncinterval); |
| 267 | |
| 268 | /* decide how many packets to be used */ |
| 269 | if (is_playback) { |
| 270 | unsigned int minsize, maxpacks; |
| 271 | /* determine how small a packet can be */ |
| 272 | minsize = (subs->freqn >> (16 - subs->datainterval)) |
| 273 | * (frame_bits >> 3); |
| 274 | /* with sync from device, assume it can be 12% lower */ |
| 275 | if (subs->syncpipe) |
| 276 | minsize -= minsize >> 3; |
| 277 | minsize = max(minsize, 1u); |
| 278 | total_packs = (period_bytes + minsize - 1) / minsize; |
| 279 | /* we need at least two URBs for queueing */ |
| 280 | if (total_packs < 2) { |
| 281 | total_packs = 2; |
| 282 | } else { |
| 283 | /* and we don't want too long a queue either */ |
| 284 | maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2); |
| 285 | total_packs = min(total_packs, maxpacks); |
| 286 | } |
| 287 | } else { |
| 288 | while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) |
| 289 | urb_packs >>= 1; |
| 290 | total_packs = MAX_URBS * urb_packs; |
| 291 | } |
| 292 | subs->nurbs = (total_packs + urb_packs - 1) / urb_packs; |
| 293 | if (subs->nurbs > MAX_URBS) { |
| 294 | /* too much... */ |
| 295 | subs->nurbs = MAX_URBS; |
| 296 | total_packs = MAX_URBS * urb_packs; |
| 297 | } else if (subs->nurbs < 2) { |
| 298 | /* too little - we need at least two packets |
| 299 | * to ensure contiguous playback/capture |
| 300 | */ |
| 301 | subs->nurbs = 2; |
| 302 | } |
| 303 | |
| 304 | /* allocate and initialize data urbs */ |
| 305 | for (i = 0; i < subs->nurbs; i++) { |
| 306 | struct snd_urb_ctx *u = &subs->dataurb[i]; |
| 307 | u->index = i; |
| 308 | u->subs = subs; |
| 309 | u->packets = (i + 1) * total_packs / subs->nurbs |
| 310 | - i * total_packs / subs->nurbs; |
| 311 | u->buffer_size = maxsize * u->packets; |
| 312 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) |
| 313 | u->packets++; /* for transfer delimiter */ |
| 314 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); |
| 315 | if (!u->urb) |
| 316 | goto out_of_memory; |
| 317 | u->urb->transfer_buffer = |
| 318 | usb_alloc_coherent(subs->dev, u->buffer_size, |
| 319 | GFP_KERNEL, &u->urb->transfer_dma); |
| 320 | if (!u->urb->transfer_buffer) |
| 321 | goto out_of_memory; |
| 322 | u->urb->pipe = subs->datapipe; |
| 323 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
| 324 | u->urb->interval = 1 << subs->datainterval; |
| 325 | u->urb->context = u; |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 326 | u->urb->complete = snd_complete_urb_old; |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | if (subs->syncpipe) { |
| 330 | /* allocate and initialize sync urbs */ |
| 331 | subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4, |
| 332 | GFP_KERNEL, &subs->sync_dma); |
| 333 | if (!subs->syncbuf) |
| 334 | goto out_of_memory; |
| 335 | for (i = 0; i < SYNC_URBS; i++) { |
| 336 | struct snd_urb_ctx *u = &subs->syncurb[i]; |
| 337 | u->index = i; |
| 338 | u->subs = subs; |
| 339 | u->packets = 1; |
| 340 | u->urb = usb_alloc_urb(1, GFP_KERNEL); |
| 341 | if (!u->urb) |
| 342 | goto out_of_memory; |
| 343 | u->urb->transfer_buffer = subs->syncbuf + i * 4; |
| 344 | u->urb->transfer_dma = subs->sync_dma + i * 4; |
| 345 | u->urb->transfer_buffer_length = 4; |
| 346 | u->urb->pipe = subs->syncpipe; |
| 347 | u->urb->transfer_flags = URB_ISO_ASAP | |
| 348 | URB_NO_TRANSFER_DMA_MAP; |
| 349 | u->urb->number_of_packets = 1; |
| 350 | u->urb->interval = 1 << subs->syncinterval; |
| 351 | u->urb->context = u; |
| 352 | u->urb->complete = snd_complete_sync_urb; |
| 353 | } |
| 354 | } |
| 355 | return 0; |
| 356 | |
| 357 | out_of_memory: |
| 358 | snd_usb_release_substream_urbs(subs, 0); |
| 359 | return -ENOMEM; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * prepare urb for full speed capture sync pipe |
| 364 | * |
| 365 | * fill the length and offset of each urb descriptor. |
| 366 | * the fixed 10.14 frequency is passed through the pipe. |
| 367 | */ |
| 368 | static int prepare_capture_sync_urb(struct snd_usb_substream *subs, |
| 369 | struct snd_pcm_runtime *runtime, |
| 370 | struct urb *urb) |
| 371 | { |
| 372 | unsigned char *cp = urb->transfer_buffer; |
| 373 | struct snd_urb_ctx *ctx = urb->context; |
| 374 | |
| 375 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
| 376 | urb->iso_frame_desc[0].length = 3; |
| 377 | urb->iso_frame_desc[0].offset = 0; |
| 378 | cp[0] = subs->freqn >> 2; |
| 379 | cp[1] = subs->freqn >> 10; |
| 380 | cp[2] = subs->freqn >> 18; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * prepare urb for high speed capture sync pipe |
| 386 | * |
| 387 | * fill the length and offset of each urb descriptor. |
| 388 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. |
| 389 | */ |
| 390 | static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs, |
| 391 | struct snd_pcm_runtime *runtime, |
| 392 | struct urb *urb) |
| 393 | { |
| 394 | unsigned char *cp = urb->transfer_buffer; |
| 395 | struct snd_urb_ctx *ctx = urb->context; |
| 396 | |
| 397 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
| 398 | urb->iso_frame_desc[0].length = 4; |
| 399 | urb->iso_frame_desc[0].offset = 0; |
| 400 | cp[0] = subs->freqn; |
| 401 | cp[1] = subs->freqn >> 8; |
| 402 | cp[2] = subs->freqn >> 16; |
| 403 | cp[3] = subs->freqn >> 24; |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * process after capture sync complete |
| 409 | * - nothing to do |
| 410 | */ |
| 411 | static int retire_capture_sync_urb(struct snd_usb_substream *subs, |
| 412 | struct snd_pcm_runtime *runtime, |
| 413 | struct urb *urb) |
| 414 | { |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * prepare urb for capture data pipe |
| 420 | * |
| 421 | * fill the offset and length of each descriptor. |
| 422 | * |
| 423 | * we use a temporary buffer to write the captured data. |
| 424 | * since the length of written data is determined by host, we cannot |
| 425 | * write onto the pcm buffer directly... the data is thus copied |
| 426 | * later at complete callback to the global buffer. |
| 427 | */ |
| 428 | static int prepare_capture_urb(struct snd_usb_substream *subs, |
| 429 | struct snd_pcm_runtime *runtime, |
| 430 | struct urb *urb) |
| 431 | { |
| 432 | int i, offs; |
| 433 | struct snd_urb_ctx *ctx = urb->context; |
| 434 | |
| 435 | offs = 0; |
| 436 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
| 437 | for (i = 0; i < ctx->packets; i++) { |
| 438 | urb->iso_frame_desc[i].offset = offs; |
| 439 | urb->iso_frame_desc[i].length = subs->curpacksize; |
| 440 | offs += subs->curpacksize; |
| 441 | } |
| 442 | urb->transfer_buffer_length = offs; |
| 443 | urb->number_of_packets = ctx->packets; |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * process after capture complete |
| 449 | * |
| 450 | * copy the data from each desctiptor to the pcm buffer, and |
| 451 | * update the current position. |
| 452 | */ |
| 453 | static int retire_capture_urb(struct snd_usb_substream *subs, |
| 454 | struct snd_pcm_runtime *runtime, |
| 455 | struct urb *urb) |
| 456 | { |
| 457 | unsigned long flags; |
| 458 | unsigned char *cp; |
| 459 | int i; |
| 460 | unsigned int stride, frames, bytes, oldptr; |
| 461 | int period_elapsed = 0; |
| 462 | |
| 463 | stride = runtime->frame_bits >> 3; |
| 464 | |
| 465 | for (i = 0; i < urb->number_of_packets; i++) { |
| 466 | cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
Takashi Iwai | 80c8a2a | 2012-01-09 11:37:20 +0100 | [diff] [blame] | 467 | if (urb->iso_frame_desc[i].status && printk_ratelimit()) { |
| 468 | snd_printdd("frame %d active: %d\n", i, urb->iso_frame_desc[i].status); |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 469 | // continue; |
| 470 | } |
| 471 | bytes = urb->iso_frame_desc[i].actual_length; |
| 472 | frames = bytes / stride; |
| 473 | if (!subs->txfr_quirk) |
| 474 | bytes = frames * stride; |
| 475 | if (bytes % (runtime->sample_bits >> 3) != 0) { |
| 476 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
| 477 | int oldbytes = bytes; |
| 478 | #endif |
| 479 | bytes = frames * stride; |
| 480 | snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n", |
| 481 | oldbytes, bytes); |
| 482 | } |
| 483 | /* update the current pointer */ |
| 484 | spin_lock_irqsave(&subs->lock, flags); |
| 485 | oldptr = subs->hwptr_done; |
| 486 | subs->hwptr_done += bytes; |
| 487 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 488 | subs->hwptr_done -= runtime->buffer_size * stride; |
| 489 | frames = (bytes + (oldptr % stride)) / stride; |
| 490 | subs->transfer_done += frames; |
| 491 | if (subs->transfer_done >= runtime->period_size) { |
| 492 | subs->transfer_done -= runtime->period_size; |
| 493 | period_elapsed = 1; |
| 494 | } |
| 495 | spin_unlock_irqrestore(&subs->lock, flags); |
| 496 | /* copy a data chunk */ |
| 497 | if (oldptr + bytes > runtime->buffer_size * stride) { |
| 498 | unsigned int bytes1 = |
| 499 | runtime->buffer_size * stride - oldptr; |
| 500 | memcpy(runtime->dma_area + oldptr, cp, bytes1); |
| 501 | memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1); |
| 502 | } else { |
| 503 | memcpy(runtime->dma_area + oldptr, cp, bytes); |
| 504 | } |
| 505 | } |
| 506 | if (period_elapsed) |
| 507 | snd_pcm_period_elapsed(subs->pcm_substream); |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Process after capture complete when paused. Nothing to do. |
| 513 | */ |
| 514 | static int retire_paused_capture_urb(struct snd_usb_substream *subs, |
| 515 | struct snd_pcm_runtime *runtime, |
| 516 | struct urb *urb) |
| 517 | { |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | /* |
| 523 | * prepare urb for playback sync pipe |
| 524 | * |
| 525 | * set up the offset and length to receive the current frequency. |
| 526 | */ |
| 527 | static int prepare_playback_sync_urb(struct snd_usb_substream *subs, |
| 528 | struct snd_pcm_runtime *runtime, |
| 529 | struct urb *urb) |
| 530 | { |
| 531 | struct snd_urb_ctx *ctx = urb->context; |
| 532 | |
| 533 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
| 534 | urb->iso_frame_desc[0].length = min(4u, ctx->subs->syncmaxsize); |
| 535 | urb->iso_frame_desc[0].offset = 0; |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * process after playback sync complete |
| 541 | * |
| 542 | * Full speed devices report feedback values in 10.14 format as samples per |
| 543 | * frame, high speed devices in 16.16 format as samples per microframe. |
| 544 | * Because the Audio Class 1 spec was written before USB 2.0, many high speed |
| 545 | * devices use a wrong interpretation, some others use an entirely different |
| 546 | * format. Therefore, we cannot predict what format any particular device uses |
| 547 | * and must detect it automatically. |
| 548 | */ |
| 549 | static int retire_playback_sync_urb(struct snd_usb_substream *subs, |
| 550 | struct snd_pcm_runtime *runtime, |
| 551 | struct urb *urb) |
| 552 | { |
| 553 | unsigned int f; |
| 554 | int shift; |
| 555 | unsigned long flags; |
| 556 | |
| 557 | if (urb->iso_frame_desc[0].status != 0 || |
| 558 | urb->iso_frame_desc[0].actual_length < 3) |
| 559 | return 0; |
| 560 | |
| 561 | f = le32_to_cpup(urb->transfer_buffer); |
| 562 | if (urb->iso_frame_desc[0].actual_length == 3) |
| 563 | f &= 0x00ffffff; |
| 564 | else |
| 565 | f &= 0x0fffffff; |
| 566 | if (f == 0) |
| 567 | return 0; |
| 568 | |
| 569 | if (unlikely(subs->freqshift == INT_MIN)) { |
| 570 | /* |
| 571 | * The first time we see a feedback value, determine its format |
| 572 | * by shifting it left or right until it matches the nominal |
| 573 | * frequency value. This assumes that the feedback does not |
| 574 | * differ from the nominal value more than +50% or -25%. |
| 575 | */ |
| 576 | shift = 0; |
| 577 | while (f < subs->freqn - subs->freqn / 4) { |
| 578 | f <<= 1; |
| 579 | shift++; |
| 580 | } |
| 581 | while (f > subs->freqn + subs->freqn / 2) { |
| 582 | f >>= 1; |
| 583 | shift--; |
| 584 | } |
| 585 | subs->freqshift = shift; |
| 586 | } |
| 587 | else if (subs->freqshift >= 0) |
| 588 | f <<= subs->freqshift; |
| 589 | else |
| 590 | f >>= -subs->freqshift; |
| 591 | |
| 592 | if (likely(f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax)) { |
| 593 | /* |
| 594 | * If the frequency looks valid, set it. |
| 595 | * This value is referred to in prepare_playback_urb(). |
| 596 | */ |
| 597 | spin_lock_irqsave(&subs->lock, flags); |
| 598 | subs->freqm = f; |
| 599 | spin_unlock_irqrestore(&subs->lock, flags); |
| 600 | } else { |
| 601 | /* |
| 602 | * Out of range; maybe the shift value is wrong. |
| 603 | * Reset it so that we autodetect again the next time. |
| 604 | */ |
| 605 | subs->freqshift = INT_MIN; |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | /* determine the number of frames in the next packet */ |
| 612 | static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs) |
| 613 | { |
| 614 | if (subs->fill_max) |
| 615 | return subs->maxframesize; |
| 616 | else { |
| 617 | subs->phase = (subs->phase & 0xffff) |
| 618 | + (subs->freqm << subs->datainterval); |
| 619 | return min(subs->phase >> 16, subs->maxframesize); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Prepare urb for streaming before playback starts or when paused. |
| 625 | * |
| 626 | * We don't have any data, so we send silence. |
| 627 | */ |
| 628 | static int prepare_nodata_playback_urb(struct snd_usb_substream *subs, |
| 629 | struct snd_pcm_runtime *runtime, |
| 630 | struct urb *urb) |
| 631 | { |
| 632 | unsigned int i, offs, counts; |
| 633 | struct snd_urb_ctx *ctx = urb->context; |
| 634 | int stride = runtime->frame_bits >> 3; |
| 635 | |
| 636 | offs = 0; |
| 637 | urb->dev = ctx->subs->dev; |
| 638 | for (i = 0; i < ctx->packets; ++i) { |
| 639 | counts = snd_usb_audio_next_packet_size(subs); |
| 640 | urb->iso_frame_desc[i].offset = offs * stride; |
| 641 | urb->iso_frame_desc[i].length = counts * stride; |
| 642 | offs += counts; |
| 643 | } |
| 644 | urb->number_of_packets = ctx->packets; |
| 645 | urb->transfer_buffer_length = offs * stride; |
| 646 | memset(urb->transfer_buffer, |
| 647 | runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0, |
| 648 | offs * stride); |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * prepare urb for playback data pipe |
| 654 | * |
| 655 | * Since a URB can handle only a single linear buffer, we must use double |
| 656 | * buffering when the data to be transferred overflows the buffer boundary. |
| 657 | * To avoid inconsistencies when updating hwptr_done, we use double buffering |
| 658 | * for all URBs. |
| 659 | */ |
| 660 | static int prepare_playback_urb(struct snd_usb_substream *subs, |
| 661 | struct snd_pcm_runtime *runtime, |
| 662 | struct urb *urb) |
| 663 | { |
| 664 | int i, stride; |
| 665 | unsigned int counts, frames, bytes; |
| 666 | unsigned long flags; |
| 667 | int period_elapsed = 0; |
| 668 | struct snd_urb_ctx *ctx = urb->context; |
| 669 | |
| 670 | stride = runtime->frame_bits >> 3; |
| 671 | |
| 672 | frames = 0; |
| 673 | urb->dev = ctx->subs->dev; /* we need to set this at each time */ |
| 674 | urb->number_of_packets = 0; |
| 675 | spin_lock_irqsave(&subs->lock, flags); |
| 676 | for (i = 0; i < ctx->packets; i++) { |
| 677 | counts = snd_usb_audio_next_packet_size(subs); |
| 678 | /* set up descriptor */ |
| 679 | urb->iso_frame_desc[i].offset = frames * stride; |
| 680 | urb->iso_frame_desc[i].length = counts * stride; |
| 681 | frames += counts; |
| 682 | urb->number_of_packets++; |
| 683 | subs->transfer_done += counts; |
| 684 | if (subs->transfer_done >= runtime->period_size) { |
| 685 | subs->transfer_done -= runtime->period_size; |
| 686 | period_elapsed = 1; |
| 687 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) { |
| 688 | if (subs->transfer_done > 0) { |
| 689 | /* FIXME: fill-max mode is not |
| 690 | * supported yet */ |
| 691 | frames -= subs->transfer_done; |
| 692 | counts -= subs->transfer_done; |
| 693 | urb->iso_frame_desc[i].length = |
| 694 | counts * stride; |
| 695 | subs->transfer_done = 0; |
| 696 | } |
| 697 | i++; |
| 698 | if (i < ctx->packets) { |
| 699 | /* add a transfer delimiter */ |
| 700 | urb->iso_frame_desc[i].offset = |
| 701 | frames * stride; |
| 702 | urb->iso_frame_desc[i].length = 0; |
| 703 | urb->number_of_packets++; |
| 704 | } |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | if (period_elapsed) /* finish at the period boundary */ |
| 709 | break; |
| 710 | } |
| 711 | bytes = frames * stride; |
| 712 | if (subs->hwptr_done + bytes > runtime->buffer_size * stride) { |
| 713 | /* err, the transferred area goes over buffer boundary. */ |
| 714 | unsigned int bytes1 = |
| 715 | runtime->buffer_size * stride - subs->hwptr_done; |
| 716 | memcpy(urb->transfer_buffer, |
| 717 | runtime->dma_area + subs->hwptr_done, bytes1); |
| 718 | memcpy(urb->transfer_buffer + bytes1, |
| 719 | runtime->dma_area, bytes - bytes1); |
| 720 | } else { |
| 721 | memcpy(urb->transfer_buffer, |
| 722 | runtime->dma_area + subs->hwptr_done, bytes); |
| 723 | } |
| 724 | subs->hwptr_done += bytes; |
| 725 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 726 | subs->hwptr_done -= runtime->buffer_size * stride; |
| 727 | |
| 728 | /* update delay with exact number of samples queued */ |
| 729 | runtime->delay = subs->last_delay; |
| 730 | runtime->delay += frames; |
| 731 | subs->last_delay = runtime->delay; |
| 732 | |
| 733 | /* realign last_frame_number */ |
| 734 | subs->last_frame_number = usb_get_current_frame_number(subs->dev); |
| 735 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ |
| 736 | |
| 737 | spin_unlock_irqrestore(&subs->lock, flags); |
| 738 | urb->transfer_buffer_length = bytes; |
| 739 | if (period_elapsed) |
| 740 | snd_pcm_period_elapsed(subs->pcm_substream); |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * process after playback data complete |
| 746 | * - decrease the delay count again |
| 747 | */ |
| 748 | static int retire_playback_urb(struct snd_usb_substream *subs, |
| 749 | struct snd_pcm_runtime *runtime, |
| 750 | struct urb *urb) |
| 751 | { |
| 752 | unsigned long flags; |
| 753 | int stride = runtime->frame_bits >> 3; |
| 754 | int processed = urb->transfer_buffer_length / stride; |
| 755 | int est_delay; |
| 756 | |
| 757 | spin_lock_irqsave(&subs->lock, flags); |
| 758 | |
| 759 | est_delay = snd_usb_pcm_delay(subs, runtime->rate); |
| 760 | /* update delay with exact number of samples played */ |
| 761 | if (processed > subs->last_delay) |
| 762 | subs->last_delay = 0; |
| 763 | else |
| 764 | subs->last_delay -= processed; |
| 765 | runtime->delay = subs->last_delay; |
| 766 | |
| 767 | /* |
| 768 | * Report when delay estimate is off by more than 2ms. |
| 769 | * The error should be lower than 2ms since the estimate relies |
| 770 | * on two reads of a counter updated every ms. |
| 771 | */ |
| 772 | if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2) |
| 773 | snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n", |
| 774 | est_delay, subs->last_delay); |
| 775 | |
| 776 | spin_unlock_irqrestore(&subs->lock, flags); |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | static const char *usb_error_string(int err) |
| 781 | { |
| 782 | switch (err) { |
| 783 | case -ENODEV: |
| 784 | return "no device"; |
| 785 | case -ENOENT: |
| 786 | return "endpoint not enabled"; |
| 787 | case -EPIPE: |
| 788 | return "endpoint stalled"; |
| 789 | case -ENOSPC: |
| 790 | return "not enough bandwidth"; |
| 791 | case -ESHUTDOWN: |
| 792 | return "device disabled"; |
| 793 | case -EHOSTUNREACH: |
| 794 | return "device suspended"; |
| 795 | case -EINVAL: |
| 796 | case -EAGAIN: |
| 797 | case -EFBIG: |
| 798 | case -EMSGSIZE: |
| 799 | return "internal error"; |
| 800 | default: |
| 801 | return "unknown error"; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * set up and start data/sync urbs |
| 807 | */ |
| 808 | static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime) |
| 809 | { |
| 810 | unsigned int i; |
| 811 | int err; |
| 812 | |
| 813 | if (subs->stream->chip->shutdown) |
| 814 | return -EBADFD; |
| 815 | |
| 816 | for (i = 0; i < subs->nurbs; i++) { |
| 817 | if (snd_BUG_ON(!subs->dataurb[i].urb)) |
| 818 | return -EINVAL; |
| 819 | if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) { |
| 820 | snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i); |
| 821 | goto __error; |
| 822 | } |
| 823 | } |
| 824 | if (subs->syncpipe) { |
| 825 | for (i = 0; i < SYNC_URBS; i++) { |
| 826 | if (snd_BUG_ON(!subs->syncurb[i].urb)) |
| 827 | return -EINVAL; |
| 828 | if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) { |
| 829 | snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i); |
| 830 | goto __error; |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | subs->active_mask = 0; |
| 836 | subs->unlink_mask = 0; |
| 837 | subs->running = 1; |
| 838 | for (i = 0; i < subs->nurbs; i++) { |
| 839 | err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC); |
| 840 | if (err < 0) { |
| 841 | snd_printk(KERN_ERR "cannot submit datapipe " |
| 842 | "for urb %d, error %d: %s\n", |
| 843 | i, err, usb_error_string(err)); |
| 844 | goto __error; |
| 845 | } |
| 846 | set_bit(i, &subs->active_mask); |
| 847 | } |
| 848 | if (subs->syncpipe) { |
| 849 | for (i = 0; i < SYNC_URBS; i++) { |
| 850 | err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC); |
| 851 | if (err < 0) { |
| 852 | snd_printk(KERN_ERR "cannot submit syncpipe " |
| 853 | "for urb %d, error %d: %s\n", |
| 854 | i, err, usb_error_string(err)); |
| 855 | goto __error; |
| 856 | } |
| 857 | set_bit(i + 16, &subs->active_mask); |
| 858 | } |
| 859 | } |
| 860 | return 0; |
| 861 | |
| 862 | __error: |
| 863 | // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN); |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 864 | deactivate_urbs_old(subs, 0, 0); |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 865 | return -EPIPE; |
| 866 | } |
| 867 | |
| 868 | |
| 869 | /* |
| 870 | */ |
| 871 | static struct snd_urb_ops audio_urb_ops[2] = { |
| 872 | { |
| 873 | .prepare = prepare_nodata_playback_urb, |
| 874 | .retire = retire_playback_urb, |
| 875 | .prepare_sync = prepare_playback_sync_urb, |
| 876 | .retire_sync = retire_playback_sync_urb, |
| 877 | }, |
| 878 | { |
| 879 | .prepare = prepare_capture_urb, |
| 880 | .retire = retire_capture_urb, |
| 881 | .prepare_sync = prepare_capture_sync_urb, |
| 882 | .retire_sync = retire_capture_sync_urb, |
| 883 | }, |
| 884 | }; |
| 885 | |
| 886 | /* |
| 887 | * initialize the substream instance. |
| 888 | */ |
| 889 | |
| 890 | void snd_usb_init_substream(struct snd_usb_stream *as, |
| 891 | int stream, struct audioformat *fp) |
| 892 | { |
| 893 | struct snd_usb_substream *subs = &as->substream[stream]; |
| 894 | |
| 895 | INIT_LIST_HEAD(&subs->fmt_list); |
| 896 | spin_lock_init(&subs->lock); |
| 897 | |
| 898 | subs->stream = as; |
| 899 | subs->direction = stream; |
| 900 | subs->dev = as->chip->dev; |
| 901 | subs->txfr_quirk = as->chip->txfr_quirk; |
| 902 | subs->ops = audio_urb_ops[stream]; |
| 903 | if (snd_usb_get_speed(subs->dev) >= USB_SPEED_HIGH) |
| 904 | subs->ops.prepare_sync = prepare_capture_sync_urb_hs; |
| 905 | |
| 906 | snd_usb_set_pcm_ops(as->pcm, stream); |
| 907 | |
| 908 | list_add_tail(&fp->list, &subs->fmt_list); |
| 909 | subs->formats |= fp->formats; |
| 910 | subs->endpoint = fp->endpoint; |
| 911 | subs->num_formats++; |
| 912 | subs->fmt_type = fp->fmt_type; |
| 913 | } |
| 914 | |
| 915 | int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd) |
| 916 | { |
| 917 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 918 | |
| 919 | switch (cmd) { |
| 920 | case SNDRV_PCM_TRIGGER_START: |
| 921 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 922 | subs->ops.prepare = prepare_playback_urb; |
| 923 | return 0; |
| 924 | case SNDRV_PCM_TRIGGER_STOP: |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 925 | return deactivate_urbs_old(subs, 0, 0); |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 926 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 927 | subs->ops.prepare = prepare_nodata_playback_urb; |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | return -EINVAL; |
| 932 | } |
| 933 | |
| 934 | int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd) |
| 935 | { |
| 936 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 937 | |
| 938 | switch (cmd) { |
| 939 | case SNDRV_PCM_TRIGGER_START: |
| 940 | subs->ops.retire = retire_capture_urb; |
| 941 | return start_urbs(subs, substream->runtime); |
| 942 | case SNDRV_PCM_TRIGGER_STOP: |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 943 | return deactivate_urbs_old(subs, 0, 0); |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 944 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 945 | subs->ops.retire = retire_paused_capture_urb; |
| 946 | return 0; |
| 947 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 948 | subs->ops.retire = retire_capture_urb; |
| 949 | return 0; |
| 950 | } |
| 951 | |
| 952 | return -EINVAL; |
| 953 | } |
| 954 | |
| 955 | int snd_usb_substream_prepare(struct snd_usb_substream *subs, |
| 956 | struct snd_pcm_runtime *runtime) |
| 957 | { |
| 958 | /* clear urbs (to be sure) */ |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 959 | deactivate_urbs_old(subs, 0, 1); |
| 960 | wait_clear_urbs_old(subs); |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 961 | |
| 962 | /* for playback, submit the URBs now; otherwise, the first hwptr_done |
| 963 | * updates for all URBs would happen at the same time when starting */ |
| 964 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) { |
| 965 | subs->ops.prepare = prepare_nodata_playback_urb; |
| 966 | return start_urbs(subs, runtime); |
| 967 | } |
| 968 | |
| 969 | return 0; |
| 970 | } |
| 971 | |
Daniel Mack | 8fdff6a | 2012-04-12 13:51:11 +0200 | [diff] [blame^] | 972 | int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep) |
| 973 | { |
| 974 | return ep->sync_master && |
| 975 | ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA && |
| 976 | ep->type == SND_USB_ENDPOINT_TYPE_DATA && |
| 977 | usb_pipeout(ep->pipe); |
| 978 | } |
| 979 | |
| 980 | /* determine the number of frames in the next packet */ |
| 981 | static int next_packet_size(struct snd_usb_endpoint *ep) |
| 982 | { |
| 983 | unsigned long flags; |
| 984 | int ret; |
| 985 | |
| 986 | if (ep->fill_max) |
| 987 | return ep->maxframesize; |
| 988 | |
| 989 | spin_lock_irqsave(&ep->lock, flags); |
| 990 | ep->phase = (ep->phase & 0xffff) |
| 991 | + (ep->freqm << ep->datainterval); |
| 992 | ret = min(ep->phase >> 16, ep->maxframesize); |
| 993 | spin_unlock_irqrestore(&ep->lock, flags); |
| 994 | |
| 995 | return ret; |
| 996 | } |
| 997 | |
| 998 | static void retire_outbound_urb(struct snd_usb_endpoint *ep, |
| 999 | struct snd_urb_ctx *urb_ctx) |
| 1000 | { |
| 1001 | if (ep->retire_data_urb) |
| 1002 | ep->retire_data_urb(ep->data_subs, urb_ctx->urb); |
| 1003 | } |
| 1004 | |
| 1005 | static void retire_inbound_urb(struct snd_usb_endpoint *ep, |
| 1006 | struct snd_urb_ctx *urb_ctx) |
| 1007 | { |
| 1008 | struct urb *urb = urb_ctx->urb; |
| 1009 | |
| 1010 | if (ep->sync_slave) |
| 1011 | snd_usb_handle_sync_urb(ep->sync_slave, ep, urb); |
| 1012 | |
| 1013 | if (ep->retire_data_urb) |
| 1014 | ep->retire_data_urb(ep->data_subs, urb); |
| 1015 | } |
| 1016 | |
| 1017 | static void prepare_outbound_urb_sizes(struct snd_usb_endpoint *ep, |
| 1018 | struct snd_urb_ctx *ctx) |
| 1019 | { |
| 1020 | int i; |
| 1021 | |
| 1022 | for (i = 0; i < ctx->packets; ++i) |
| 1023 | ctx->packet_size[i] = next_packet_size(ep); |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * Prepare a PLAYBACK urb for submission to the bus. |
| 1028 | */ |
| 1029 | static void prepare_outbound_urb(struct snd_usb_endpoint *ep, |
| 1030 | struct snd_urb_ctx *ctx) |
| 1031 | { |
| 1032 | int i; |
| 1033 | struct urb *urb = ctx->urb; |
| 1034 | unsigned char *cp = urb->transfer_buffer; |
| 1035 | |
| 1036 | urb->dev = ep->chip->dev; /* we need to set this at each time */ |
| 1037 | |
| 1038 | switch (ep->type) { |
| 1039 | case SND_USB_ENDPOINT_TYPE_DATA: |
| 1040 | if (ep->prepare_data_urb) { |
| 1041 | ep->prepare_data_urb(ep->data_subs, urb); |
| 1042 | } else { |
| 1043 | /* no data provider, so send silence */ |
| 1044 | unsigned int offs = 0; |
| 1045 | for (i = 0; i < ctx->packets; ++i) { |
| 1046 | int counts = ctx->packet_size[i]; |
| 1047 | urb->iso_frame_desc[i].offset = offs * ep->stride; |
| 1048 | urb->iso_frame_desc[i].length = counts * ep->stride; |
| 1049 | offs += counts; |
| 1050 | } |
| 1051 | |
| 1052 | urb->number_of_packets = ctx->packets; |
| 1053 | urb->transfer_buffer_length = offs * ep->stride; |
| 1054 | memset(urb->transfer_buffer, ep->silence_value, |
| 1055 | offs * ep->stride); |
| 1056 | } |
| 1057 | break; |
| 1058 | |
| 1059 | case SND_USB_ENDPOINT_TYPE_SYNC: |
| 1060 | if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) { |
| 1061 | /* |
| 1062 | * fill the length and offset of each urb descriptor. |
| 1063 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. |
| 1064 | */ |
| 1065 | urb->iso_frame_desc[0].length = 4; |
| 1066 | urb->iso_frame_desc[0].offset = 0; |
| 1067 | cp[0] = ep->freqn; |
| 1068 | cp[1] = ep->freqn >> 8; |
| 1069 | cp[2] = ep->freqn >> 16; |
| 1070 | cp[3] = ep->freqn >> 24; |
| 1071 | } else { |
| 1072 | /* |
| 1073 | * fill the length and offset of each urb descriptor. |
| 1074 | * the fixed 10.14 frequency is passed through the pipe. |
| 1075 | */ |
| 1076 | urb->iso_frame_desc[0].length = 3; |
| 1077 | urb->iso_frame_desc[0].offset = 0; |
| 1078 | cp[0] = ep->freqn >> 2; |
| 1079 | cp[1] = ep->freqn >> 10; |
| 1080 | cp[2] = ep->freqn >> 18; |
| 1081 | } |
| 1082 | |
| 1083 | break; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * Prepare a CAPTURE or SYNC urb for submission to the bus. |
| 1089 | */ |
| 1090 | static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep, |
| 1091 | struct snd_urb_ctx *urb_ctx) |
| 1092 | { |
| 1093 | int i, offs; |
| 1094 | struct urb *urb = urb_ctx->urb; |
| 1095 | |
| 1096 | urb->dev = ep->chip->dev; /* we need to set this at each time */ |
| 1097 | |
| 1098 | switch (ep->type) { |
| 1099 | case SND_USB_ENDPOINT_TYPE_DATA: |
| 1100 | offs = 0; |
| 1101 | for (i = 0; i < urb_ctx->packets; i++) { |
| 1102 | urb->iso_frame_desc[i].offset = offs; |
| 1103 | urb->iso_frame_desc[i].length = ep->curpacksize; |
| 1104 | offs += ep->curpacksize; |
| 1105 | } |
| 1106 | |
| 1107 | urb->transfer_buffer_length = offs; |
| 1108 | urb->number_of_packets = urb_ctx->packets; |
| 1109 | break; |
| 1110 | |
| 1111 | case SND_USB_ENDPOINT_TYPE_SYNC: |
| 1112 | urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize); |
| 1113 | urb->iso_frame_desc[0].offset = 0; |
| 1114 | break; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | static void queue_pending_output_urbs(struct snd_usb_endpoint *ep) |
| 1119 | { |
| 1120 | while (test_bit(EP_FLAG_RUNNING, &ep->flags)) { |
| 1121 | |
| 1122 | unsigned long flags; |
| 1123 | struct snd_usb_packet_info *packet; |
| 1124 | struct snd_urb_ctx *ctx = NULL; |
| 1125 | struct urb *urb; |
| 1126 | int err, i; |
| 1127 | |
| 1128 | spin_lock_irqsave(&ep->lock, flags); |
| 1129 | if (ep->next_packet_read_pos != ep->next_packet_write_pos) { |
| 1130 | packet = ep->next_packet + ep->next_packet_read_pos; |
| 1131 | ep->next_packet_read_pos++; |
| 1132 | ep->next_packet_read_pos %= MAX_URBS; |
| 1133 | |
| 1134 | /* take URB out of FIFO */ |
| 1135 | if (!list_empty(&ep->ready_playback_urbs)) |
| 1136 | ctx = list_first_entry(&ep->ready_playback_urbs, |
| 1137 | struct snd_urb_ctx, ready_list); |
| 1138 | } |
| 1139 | spin_unlock_irqrestore(&ep->lock, flags); |
| 1140 | |
| 1141 | if (ctx == NULL) |
| 1142 | return; |
| 1143 | |
| 1144 | list_del_init(&ctx->ready_list); |
| 1145 | urb = ctx->urb; |
| 1146 | |
| 1147 | /* copy over the length information */ |
| 1148 | for (i = 0; i < packet->packets; i++) |
| 1149 | ctx->packet_size[i] = packet->packet_size[i]; |
| 1150 | |
| 1151 | prepare_outbound_urb(ep, ctx); |
| 1152 | |
| 1153 | err = usb_submit_urb(ctx->urb, GFP_ATOMIC); |
| 1154 | if (err < 0) |
| 1155 | snd_printk(KERN_ERR "Unable to submit urb #%d: %d (urb %p)\n", |
| 1156 | ctx->index, err, ctx->urb); |
| 1157 | else |
| 1158 | set_bit(ctx->index, &ep->active_mask); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * complete callback for urbs |
| 1164 | */ |
| 1165 | static void snd_complete_urb(struct urb *urb) |
| 1166 | { |
| 1167 | struct snd_urb_ctx *ctx = urb->context; |
| 1168 | struct snd_usb_endpoint *ep = ctx->ep; |
| 1169 | int err; |
| 1170 | |
| 1171 | if (unlikely(urb->status == -ENOENT || /* unlinked */ |
| 1172 | urb->status == -ENODEV || /* device removed */ |
| 1173 | urb->status == -ECONNRESET || /* unlinked */ |
| 1174 | urb->status == -ESHUTDOWN || /* device disabled */ |
| 1175 | ep->chip->shutdown)) /* device disconnected */ |
| 1176 | goto exit_clear; |
| 1177 | |
| 1178 | if (usb_pipeout(ep->pipe)) { |
| 1179 | retire_outbound_urb(ep, ctx); |
| 1180 | /* can be stopped during retire callback */ |
| 1181 | if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) |
| 1182 | goto exit_clear; |
| 1183 | |
| 1184 | if (snd_usb_endpoint_implict_feedback_sink(ep)) { |
| 1185 | unsigned long flags; |
| 1186 | |
| 1187 | spin_lock_irqsave(&ep->lock, flags); |
| 1188 | list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); |
| 1189 | spin_unlock_irqrestore(&ep->lock, flags); |
| 1190 | queue_pending_output_urbs(ep); |
| 1191 | |
| 1192 | goto exit_clear; |
| 1193 | } |
| 1194 | |
| 1195 | prepare_outbound_urb_sizes(ep, ctx); |
| 1196 | prepare_outbound_urb(ep, ctx); |
| 1197 | } else { |
| 1198 | retire_inbound_urb(ep, ctx); |
| 1199 | /* can be stopped during retire callback */ |
| 1200 | if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) |
| 1201 | goto exit_clear; |
| 1202 | |
| 1203 | prepare_inbound_urb(ep, ctx); |
| 1204 | } |
| 1205 | |
| 1206 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 1207 | if (err == 0) |
| 1208 | return; |
| 1209 | |
| 1210 | snd_printk(KERN_ERR "cannot submit urb (err = %d)\n", err); |
| 1211 | //snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); |
| 1212 | |
| 1213 | exit_clear: |
| 1214 | clear_bit(ctx->index, &ep->active_mask); |
| 1215 | } |
| 1216 | |
| 1217 | struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip, |
| 1218 | struct usb_host_interface *alts, |
| 1219 | int ep_num, int direction, int type) |
| 1220 | { |
| 1221 | struct list_head *p; |
| 1222 | struct snd_usb_endpoint *ep; |
| 1223 | int ret, is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK; |
| 1224 | |
| 1225 | mutex_lock(&chip->mutex); |
| 1226 | |
| 1227 | list_for_each(p, &chip->ep_list) { |
| 1228 | ep = list_entry(p, struct snd_usb_endpoint, list); |
| 1229 | if (ep->ep_num == ep_num && |
| 1230 | ep->iface == alts->desc.bInterfaceNumber && |
| 1231 | ep->alt_idx == alts->desc.bAlternateSetting) { |
| 1232 | snd_printdd(KERN_DEBUG "Re-using EP %x in iface %d,%d @%p\n", |
| 1233 | ep_num, ep->iface, ep->alt_idx, ep); |
| 1234 | goto __exit_unlock; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | snd_printdd(KERN_DEBUG "Creating new %s %s endpoint #%x\n", |
| 1239 | is_playback ? "playback" : "capture", |
| 1240 | type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync", |
| 1241 | ep_num); |
| 1242 | |
| 1243 | /* select the alt setting once so the endpoints become valid */ |
| 1244 | ret = usb_set_interface(chip->dev, alts->desc.bInterfaceNumber, |
| 1245 | alts->desc.bAlternateSetting); |
| 1246 | if (ret < 0) { |
| 1247 | snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", |
| 1248 | __func__, ret); |
| 1249 | ep = NULL; |
| 1250 | goto __exit_unlock; |
| 1251 | } |
| 1252 | |
| 1253 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
| 1254 | if (!ep) |
| 1255 | goto __exit_unlock; |
| 1256 | |
| 1257 | ep->chip = chip; |
| 1258 | spin_lock_init(&ep->lock); |
| 1259 | ep->type = type; |
| 1260 | ep->ep_num = ep_num; |
| 1261 | ep->iface = alts->desc.bInterfaceNumber; |
| 1262 | ep->alt_idx = alts->desc.bAlternateSetting; |
| 1263 | INIT_LIST_HEAD(&ep->ready_playback_urbs); |
| 1264 | ep_num &= USB_ENDPOINT_NUMBER_MASK; |
| 1265 | |
| 1266 | if (is_playback) |
| 1267 | ep->pipe = usb_sndisocpipe(chip->dev, ep_num); |
| 1268 | else |
| 1269 | ep->pipe = usb_rcvisocpipe(chip->dev, ep_num); |
| 1270 | |
| 1271 | if (type == SND_USB_ENDPOINT_TYPE_SYNC) { |
| 1272 | if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
| 1273 | get_endpoint(alts, 1)->bRefresh >= 1 && |
| 1274 | get_endpoint(alts, 1)->bRefresh <= 9) |
| 1275 | ep->syncinterval = get_endpoint(alts, 1)->bRefresh; |
| 1276 | else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) |
| 1277 | ep->syncinterval = 1; |
| 1278 | else if (get_endpoint(alts, 1)->bInterval >= 1 && |
| 1279 | get_endpoint(alts, 1)->bInterval <= 16) |
| 1280 | ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1; |
| 1281 | else |
| 1282 | ep->syncinterval = 3; |
| 1283 | |
| 1284 | ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize); |
| 1285 | } |
| 1286 | |
| 1287 | list_add_tail(&ep->list, &chip->ep_list); |
| 1288 | |
| 1289 | __exit_unlock: |
| 1290 | mutex_unlock(&chip->mutex); |
| 1291 | |
| 1292 | return ep; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * wait until all urbs are processed. |
| 1297 | */ |
| 1298 | static int wait_clear_urbs(struct snd_usb_endpoint *ep) |
| 1299 | { |
| 1300 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); |
| 1301 | unsigned int i; |
| 1302 | int alive; |
| 1303 | |
| 1304 | do { |
| 1305 | alive = 0; |
| 1306 | for (i = 0; i < ep->nurbs; i++) |
| 1307 | if (test_bit(i, &ep->active_mask)) |
| 1308 | alive++; |
| 1309 | |
| 1310 | if (!alive) |
| 1311 | break; |
| 1312 | |
| 1313 | schedule_timeout_uninterruptible(1); |
| 1314 | } while (time_before(jiffies, end_time)); |
| 1315 | |
| 1316 | if (alive) |
| 1317 | snd_printk(KERN_ERR "timeout: still %d active urbs on EP #%x\n", |
| 1318 | alive, ep->ep_num); |
| 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | * unlink active urbs. |
| 1325 | */ |
| 1326 | static int deactivate_urbs(struct snd_usb_endpoint *ep, int force, int can_sleep) |
| 1327 | { |
| 1328 | unsigned long flags; |
| 1329 | unsigned int i; |
| 1330 | int async; |
| 1331 | |
| 1332 | if (!force && ep->chip->shutdown) /* to be sure... */ |
| 1333 | return -EBADFD; |
| 1334 | |
| 1335 | async = !can_sleep && ep->chip->async_unlink; |
| 1336 | |
| 1337 | clear_bit(EP_FLAG_RUNNING, &ep->flags); |
| 1338 | |
| 1339 | INIT_LIST_HEAD(&ep->ready_playback_urbs); |
| 1340 | ep->next_packet_read_pos = 0; |
| 1341 | ep->next_packet_write_pos = 0; |
| 1342 | |
| 1343 | if (!async && in_interrupt()) |
| 1344 | return 0; |
| 1345 | |
| 1346 | for (i = 0; i < ep->nurbs; i++) { |
| 1347 | if (test_bit(i, &ep->active_mask)) { |
| 1348 | if (!test_and_set_bit(i, &ep->unlink_mask)) { |
| 1349 | struct urb *u = ep->urb[i].urb; |
| 1350 | if (async) |
| 1351 | usb_unlink_urb(u); |
| 1352 | else |
| 1353 | usb_kill_urb(u); |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * release an endpoint's urbs |
| 1363 | */ |
| 1364 | static void release_urbs(struct snd_usb_endpoint *ep, int force) |
| 1365 | { |
| 1366 | int i; |
| 1367 | |
| 1368 | /* route incoming urbs to nirvana */ |
| 1369 | ep->retire_data_urb = NULL; |
| 1370 | ep->prepare_data_urb = NULL; |
| 1371 | |
| 1372 | /* stop urbs */ |
| 1373 | deactivate_urbs(ep, force, 1); |
| 1374 | wait_clear_urbs(ep); |
| 1375 | |
| 1376 | for (i = 0; i < ep->nurbs; i++) |
| 1377 | release_urb_ctx(&ep->urb[i]); |
| 1378 | |
| 1379 | if (ep->syncbuf) |
| 1380 | usb_free_coherent(ep->chip->dev, SYNC_URBS * 4, |
| 1381 | ep->syncbuf, ep->sync_dma); |
| 1382 | |
| 1383 | ep->syncbuf = NULL; |
| 1384 | ep->nurbs = 0; |
| 1385 | } |
| 1386 | |
| 1387 | static int data_ep_set_params(struct snd_usb_endpoint *ep, |
| 1388 | struct snd_pcm_hw_params *hw_params, |
| 1389 | struct audioformat *fmt, |
| 1390 | struct snd_usb_endpoint *sync_ep) |
| 1391 | { |
| 1392 | unsigned int maxsize, i, urb_packs, total_packs, packs_per_ms; |
| 1393 | int period_bytes = params_period_bytes(hw_params); |
| 1394 | int format = params_format(hw_params); |
| 1395 | int is_playback = usb_pipeout(ep->pipe); |
| 1396 | int frame_bits = snd_pcm_format_physical_width(params_format(hw_params)) * |
| 1397 | params_channels(hw_params); |
| 1398 | |
| 1399 | ep->datainterval = fmt->datainterval; |
| 1400 | ep->stride = frame_bits >> 3; |
| 1401 | ep->silence_value = format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0; |
| 1402 | |
| 1403 | /* calculate max. frequency */ |
| 1404 | if (ep->maxpacksize) { |
| 1405 | /* whatever fits into a max. size packet */ |
| 1406 | maxsize = ep->maxpacksize; |
| 1407 | ep->freqmax = (maxsize / (frame_bits >> 3)) |
| 1408 | << (16 - ep->datainterval); |
| 1409 | } else { |
| 1410 | /* no max. packet size: just take 25% higher than nominal */ |
| 1411 | ep->freqmax = ep->freqn + (ep->freqn >> 2); |
| 1412 | maxsize = ((ep->freqmax + 0xffff) * (frame_bits >> 3)) |
| 1413 | >> (16 - ep->datainterval); |
| 1414 | } |
| 1415 | |
| 1416 | if (ep->fill_max) |
| 1417 | ep->curpacksize = ep->maxpacksize; |
| 1418 | else |
| 1419 | ep->curpacksize = maxsize; |
| 1420 | |
| 1421 | if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) |
| 1422 | packs_per_ms = 8 >> ep->datainterval; |
| 1423 | else |
| 1424 | packs_per_ms = 1; |
| 1425 | |
| 1426 | if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) { |
| 1427 | urb_packs = max(ep->chip->nrpacks, 1); |
| 1428 | urb_packs = min(urb_packs, (unsigned int) MAX_PACKS); |
| 1429 | } else { |
| 1430 | urb_packs = 1; |
| 1431 | } |
| 1432 | |
| 1433 | urb_packs *= packs_per_ms; |
| 1434 | |
| 1435 | if (sync_ep && !snd_usb_endpoint_implict_feedback_sink(ep)) |
| 1436 | urb_packs = min(urb_packs, 1U << sync_ep->syncinterval); |
| 1437 | |
| 1438 | /* decide how many packets to be used */ |
| 1439 | if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) { |
| 1440 | unsigned int minsize, maxpacks; |
| 1441 | /* determine how small a packet can be */ |
| 1442 | minsize = (ep->freqn >> (16 - ep->datainterval)) |
| 1443 | * (frame_bits >> 3); |
| 1444 | /* with sync from device, assume it can be 12% lower */ |
| 1445 | if (sync_ep) |
| 1446 | minsize -= minsize >> 3; |
| 1447 | minsize = max(minsize, 1u); |
| 1448 | total_packs = (period_bytes + minsize - 1) / minsize; |
| 1449 | /* we need at least two URBs for queueing */ |
| 1450 | if (total_packs < 2) { |
| 1451 | total_packs = 2; |
| 1452 | } else { |
| 1453 | /* and we don't want too long a queue either */ |
| 1454 | maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2); |
| 1455 | total_packs = min(total_packs, maxpacks); |
| 1456 | } |
| 1457 | } else { |
| 1458 | while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) |
| 1459 | urb_packs >>= 1; |
| 1460 | total_packs = MAX_URBS * urb_packs; |
| 1461 | } |
| 1462 | |
| 1463 | ep->nurbs = (total_packs + urb_packs - 1) / urb_packs; |
| 1464 | if (ep->nurbs > MAX_URBS) { |
| 1465 | /* too much... */ |
| 1466 | ep->nurbs = MAX_URBS; |
| 1467 | total_packs = MAX_URBS * urb_packs; |
| 1468 | } else if (ep->nurbs < 2) { |
| 1469 | /* too little - we need at least two packets |
| 1470 | * to ensure contiguous playback/capture |
| 1471 | */ |
| 1472 | ep->nurbs = 2; |
| 1473 | } |
| 1474 | |
| 1475 | /* allocate and initialize data urbs */ |
| 1476 | for (i = 0; i < ep->nurbs; i++) { |
| 1477 | struct snd_urb_ctx *u = &ep->urb[i]; |
| 1478 | u->index = i; |
| 1479 | u->ep = ep; |
| 1480 | u->packets = (i + 1) * total_packs / ep->nurbs |
| 1481 | - i * total_packs / ep->nurbs; |
| 1482 | u->buffer_size = maxsize * u->packets; |
| 1483 | |
| 1484 | if (fmt->fmt_type == UAC_FORMAT_TYPE_II) |
| 1485 | u->packets++; /* for transfer delimiter */ |
| 1486 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); |
| 1487 | if (!u->urb) |
| 1488 | goto out_of_memory; |
| 1489 | |
| 1490 | u->urb->transfer_buffer = |
| 1491 | usb_alloc_coherent(ep->chip->dev, u->buffer_size, |
| 1492 | GFP_KERNEL, &u->urb->transfer_dma); |
| 1493 | if (!u->urb->transfer_buffer) |
| 1494 | goto out_of_memory; |
| 1495 | u->urb->pipe = ep->pipe; |
| 1496 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
| 1497 | u->urb->interval = 1 << ep->datainterval; |
| 1498 | u->urb->context = u; |
| 1499 | u->urb->complete = snd_complete_urb; |
| 1500 | INIT_LIST_HEAD(&u->ready_list); |
| 1501 | } |
| 1502 | |
| 1503 | return 0; |
| 1504 | |
| 1505 | out_of_memory: |
| 1506 | release_urbs(ep, 0); |
| 1507 | return -ENOMEM; |
| 1508 | } |
| 1509 | |
| 1510 | static int sync_ep_set_params(struct snd_usb_endpoint *ep, |
| 1511 | struct snd_pcm_hw_params *hw_params, |
| 1512 | struct audioformat *fmt) |
| 1513 | { |
| 1514 | int i; |
| 1515 | |
| 1516 | ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4, |
| 1517 | GFP_KERNEL, &ep->sync_dma); |
| 1518 | if (!ep->syncbuf) |
| 1519 | return -ENOMEM; |
| 1520 | |
| 1521 | for (i = 0; i < SYNC_URBS; i++) { |
| 1522 | struct snd_urb_ctx *u = &ep->urb[i]; |
| 1523 | u->index = i; |
| 1524 | u->ep = ep; |
| 1525 | u->packets = 1; |
| 1526 | u->urb = usb_alloc_urb(1, GFP_KERNEL); |
| 1527 | if (!u->urb) |
| 1528 | goto out_of_memory; |
| 1529 | u->urb->transfer_buffer = ep->syncbuf + i * 4; |
| 1530 | u->urb->transfer_dma = ep->sync_dma + i * 4; |
| 1531 | u->urb->transfer_buffer_length = 4; |
| 1532 | u->urb->pipe = ep->pipe; |
| 1533 | u->urb->transfer_flags = URB_ISO_ASAP | |
| 1534 | URB_NO_TRANSFER_DMA_MAP; |
| 1535 | u->urb->number_of_packets = 1; |
| 1536 | u->urb->interval = 1 << ep->syncinterval; |
| 1537 | u->urb->context = u; |
| 1538 | u->urb->complete = snd_complete_urb; |
| 1539 | } |
| 1540 | |
| 1541 | ep->nurbs = SYNC_URBS; |
| 1542 | |
| 1543 | return 0; |
| 1544 | |
| 1545 | out_of_memory: |
| 1546 | release_urbs(ep, 0); |
| 1547 | return -ENOMEM; |
| 1548 | } |
| 1549 | |
| 1550 | int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, |
| 1551 | struct snd_pcm_hw_params *hw_params, |
| 1552 | struct audioformat *fmt, |
| 1553 | struct snd_usb_endpoint *sync_ep) |
| 1554 | { |
| 1555 | int err; |
| 1556 | |
| 1557 | if (ep->use_count != 0) { |
| 1558 | snd_printk(KERN_WARNING "Unable to change format on ep #%x: already in use\n", |
| 1559 | ep->ep_num); |
| 1560 | return -EBUSY; |
| 1561 | } |
| 1562 | |
| 1563 | /* release old buffers, if any */ |
| 1564 | release_urbs(ep, 0); |
| 1565 | |
| 1566 | ep->datainterval = fmt->datainterval; |
| 1567 | ep->maxpacksize = fmt->maxpacksize; |
| 1568 | ep->fill_max = fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX; |
| 1569 | |
| 1570 | if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) |
| 1571 | ep->freqn = get_usb_full_speed_rate(params_rate(hw_params)); |
| 1572 | else |
| 1573 | ep->freqn = get_usb_high_speed_rate(params_rate(hw_params)); |
| 1574 | |
| 1575 | /* calculate the frequency in 16.16 format */ |
| 1576 | ep->freqm = ep->freqn; |
| 1577 | ep->freqshift = INT_MIN; |
| 1578 | |
| 1579 | ep->phase = 0; |
| 1580 | |
| 1581 | switch (ep->type) { |
| 1582 | case SND_USB_ENDPOINT_TYPE_DATA: |
| 1583 | err = data_ep_set_params(ep, hw_params, fmt, sync_ep); |
| 1584 | break; |
| 1585 | case SND_USB_ENDPOINT_TYPE_SYNC: |
| 1586 | err = sync_ep_set_params(ep, hw_params, fmt); |
| 1587 | break; |
| 1588 | default: |
| 1589 | err = -EINVAL; |
| 1590 | } |
| 1591 | |
| 1592 | snd_printdd(KERN_DEBUG "Setting params for ep #%x (type %d, %d urbs), ret=%d\n", |
| 1593 | ep->ep_num, ep->type, ep->nurbs, err); |
| 1594 | |
| 1595 | return err; |
| 1596 | } |
| 1597 | |
| 1598 | int snd_usb_endpoint_start(struct snd_usb_endpoint *ep) |
| 1599 | { |
| 1600 | int err; |
| 1601 | unsigned int i; |
| 1602 | |
| 1603 | if (ep->chip->shutdown) |
| 1604 | return -EBADFD; |
| 1605 | |
| 1606 | /* already running? */ |
| 1607 | if (++ep->use_count != 1) |
| 1608 | return 0; |
| 1609 | |
| 1610 | if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) |
| 1611 | return -EINVAL; |
| 1612 | |
| 1613 | /* just to be sure */ |
| 1614 | deactivate_urbs(ep, 0, 1); |
| 1615 | wait_clear_urbs(ep); |
| 1616 | |
| 1617 | ep->active_mask = 0; |
| 1618 | ep->unlink_mask = 0; |
| 1619 | ep->phase = 0; |
| 1620 | |
| 1621 | /* |
| 1622 | * If this endpoint has a data endpoint as implicit feedback source, |
| 1623 | * don't start the urbs here. Instead, mark them all as available, |
| 1624 | * wait for the record urbs to arrive and queue from that context. |
| 1625 | */ |
| 1626 | |
| 1627 | set_bit(EP_FLAG_RUNNING, &ep->flags); |
| 1628 | |
| 1629 | if (snd_usb_endpoint_implict_feedback_sink(ep)) { |
| 1630 | for (i = 0; i < ep->nurbs; i++) { |
| 1631 | struct snd_urb_ctx *ctx = ep->urb + i; |
| 1632 | list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); |
| 1633 | } |
| 1634 | |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
| 1638 | for (i = 0; i < ep->nurbs; i++) { |
| 1639 | struct urb *urb = ep->urb[i].urb; |
| 1640 | |
| 1641 | if (snd_BUG_ON(!urb)) |
| 1642 | goto __error; |
| 1643 | |
| 1644 | if (usb_pipeout(ep->pipe)) { |
| 1645 | prepare_outbound_urb_sizes(ep, urb->context); |
| 1646 | prepare_outbound_urb(ep, urb->context); |
| 1647 | } else { |
| 1648 | prepare_inbound_urb(ep, urb->context); |
| 1649 | } |
| 1650 | |
| 1651 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 1652 | if (err < 0) { |
| 1653 | snd_printk(KERN_ERR "cannot submit urb %d, error %d: %s\n", |
| 1654 | i, err, usb_error_string(err)); |
| 1655 | goto __error; |
| 1656 | } |
| 1657 | set_bit(i, &ep->active_mask); |
| 1658 | } |
| 1659 | |
| 1660 | return 0; |
| 1661 | |
| 1662 | __error: |
| 1663 | clear_bit(EP_FLAG_RUNNING, &ep->flags); |
| 1664 | ep->use_count--; |
| 1665 | deactivate_urbs(ep, 0, 0); |
| 1666 | return -EPIPE; |
| 1667 | } |
| 1668 | |
| 1669 | void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, |
| 1670 | int force, int can_sleep, int wait) |
| 1671 | { |
| 1672 | if (!ep) |
| 1673 | return; |
| 1674 | |
| 1675 | if (snd_BUG_ON(ep->use_count == 0)) |
| 1676 | return; |
| 1677 | |
| 1678 | if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) |
| 1679 | return; |
| 1680 | |
| 1681 | if (--ep->use_count == 0) { |
| 1682 | deactivate_urbs(ep, force, can_sleep); |
| 1683 | ep->data_subs = NULL; |
| 1684 | ep->sync_slave = NULL; |
| 1685 | ep->retire_data_urb = NULL; |
| 1686 | ep->prepare_data_urb = NULL; |
| 1687 | |
| 1688 | if (wait) |
| 1689 | wait_clear_urbs(ep); |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep) |
| 1694 | { |
| 1695 | if (ep->use_count != 0) |
| 1696 | return 0; |
| 1697 | |
| 1698 | if (!ep->chip->shutdown && |
| 1699 | !test_and_set_bit(EP_FLAG_ACTIVATED, &ep->flags)) { |
| 1700 | int ret; |
| 1701 | |
| 1702 | ret = usb_set_interface(ep->chip->dev, ep->iface, ep->alt_idx); |
| 1703 | if (ret < 0) { |
| 1704 | snd_printk(KERN_ERR "%s() usb_set_interface() failed, ret = %d\n", |
| 1705 | __func__, ret); |
| 1706 | clear_bit(EP_FLAG_ACTIVATED, &ep->flags); |
| 1707 | return ret; |
| 1708 | } |
| 1709 | |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | return -EBUSY; |
| 1714 | } |
| 1715 | |
| 1716 | int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep) |
| 1717 | { |
| 1718 | if (!ep) |
| 1719 | return -EINVAL; |
| 1720 | |
| 1721 | if (ep->use_count != 0) |
| 1722 | return 0; |
| 1723 | |
| 1724 | if (!ep->chip->shutdown && |
| 1725 | test_and_clear_bit(EP_FLAG_ACTIVATED, &ep->flags)) { |
| 1726 | int ret; |
| 1727 | |
| 1728 | ret = usb_set_interface(ep->chip->dev, ep->iface, 0); |
| 1729 | if (ret < 0) { |
| 1730 | snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", |
| 1731 | __func__, ret); |
| 1732 | return ret; |
| 1733 | } |
| 1734 | |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | return -EBUSY; |
| 1739 | } |
| 1740 | |
| 1741 | void snd_usb_endpoint_free(struct list_head *head) |
| 1742 | { |
| 1743 | struct snd_usb_endpoint *ep; |
| 1744 | |
| 1745 | ep = list_entry(head, struct snd_usb_endpoint, list); |
| 1746 | release_urbs(ep, 1); |
| 1747 | kfree(ep); |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | * process after playback sync complete |
| 1752 | * |
| 1753 | * Full speed devices report feedback values in 10.14 format as samples per |
| 1754 | * frame, high speed devices in 16.16 format as samples per microframe. |
| 1755 | * Because the Audio Class 1 spec was written before USB 2.0, many high speed |
| 1756 | * devices use a wrong interpretation, some others use an entirely different |
| 1757 | * format. Therefore, we cannot predict what format any particular device uses |
| 1758 | * and must detect it automatically. |
| 1759 | */ |
| 1760 | void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, |
| 1761 | struct snd_usb_endpoint *sender, |
| 1762 | const struct urb *urb) |
| 1763 | { |
| 1764 | int shift; |
| 1765 | unsigned int f; |
| 1766 | unsigned long flags; |
| 1767 | |
| 1768 | snd_BUG_ON(ep == sender); |
| 1769 | |
| 1770 | if (snd_usb_endpoint_implict_feedback_sink(ep) && |
| 1771 | ep->use_count != 0) { |
| 1772 | |
| 1773 | /* implicit feedback case */ |
| 1774 | int i, bytes = 0; |
| 1775 | struct snd_urb_ctx *in_ctx; |
| 1776 | struct snd_usb_packet_info *out_packet; |
| 1777 | |
| 1778 | in_ctx = urb->context; |
| 1779 | |
| 1780 | /* Count overall packet size */ |
| 1781 | for (i = 0; i < in_ctx->packets; i++) |
| 1782 | if (urb->iso_frame_desc[i].status == 0) |
| 1783 | bytes += urb->iso_frame_desc[i].actual_length; |
| 1784 | |
| 1785 | /* |
| 1786 | * skip empty packets. At least M-Audio's Fast Track Ultra stops |
| 1787 | * streaming once it received a 0-byte OUT URB |
| 1788 | */ |
| 1789 | if (bytes == 0) |
| 1790 | return; |
| 1791 | |
| 1792 | spin_lock_irqsave(&ep->lock, flags); |
| 1793 | out_packet = ep->next_packet + ep->next_packet_write_pos; |
| 1794 | |
| 1795 | /* |
| 1796 | * Iterate through the inbound packet and prepare the lengths |
| 1797 | * for the output packet. The OUT packet we are about to send |
| 1798 | * will have the same amount of payload than the IN packet we |
| 1799 | * just received. |
| 1800 | */ |
| 1801 | |
| 1802 | out_packet->packets = in_ctx->packets; |
| 1803 | for (i = 0; i < in_ctx->packets; i++) { |
| 1804 | if (urb->iso_frame_desc[i].status == 0) |
| 1805 | out_packet->packet_size[i] = |
| 1806 | urb->iso_frame_desc[i].actual_length / ep->stride; |
| 1807 | else |
| 1808 | out_packet->packet_size[i] = 0; |
| 1809 | } |
| 1810 | |
| 1811 | ep->next_packet_write_pos++; |
| 1812 | ep->next_packet_write_pos %= MAX_URBS; |
| 1813 | spin_unlock_irqrestore(&ep->lock, flags); |
| 1814 | queue_pending_output_urbs(ep); |
| 1815 | |
| 1816 | return; |
| 1817 | } |
| 1818 | |
| 1819 | /* parse sync endpoint packet */ |
| 1820 | |
| 1821 | if (urb->iso_frame_desc[0].status != 0 || |
| 1822 | urb->iso_frame_desc[0].actual_length < 3) |
| 1823 | return; |
| 1824 | |
| 1825 | f = le32_to_cpup(urb->transfer_buffer); |
| 1826 | if (urb->iso_frame_desc[0].actual_length == 3) |
| 1827 | f &= 0x00ffffff; |
| 1828 | else |
| 1829 | f &= 0x0fffffff; |
| 1830 | |
| 1831 | if (f == 0) |
| 1832 | return; |
| 1833 | |
| 1834 | if (unlikely(ep->freqshift == INT_MIN)) { |
| 1835 | /* |
| 1836 | * The first time we see a feedback value, determine its format |
| 1837 | * by shifting it left or right until it matches the nominal |
| 1838 | * frequency value. This assumes that the feedback does not |
| 1839 | * differ from the nominal value more than +50% or -25%. |
| 1840 | */ |
| 1841 | shift = 0; |
| 1842 | while (f < ep->freqn - ep->freqn / 4) { |
| 1843 | f <<= 1; |
| 1844 | shift++; |
| 1845 | } |
| 1846 | while (f > ep->freqn + ep->freqn / 2) { |
| 1847 | f >>= 1; |
| 1848 | shift--; |
| 1849 | } |
| 1850 | ep->freqshift = shift; |
| 1851 | } else if (ep->freqshift >= 0) |
| 1852 | f <<= ep->freqshift; |
| 1853 | else |
| 1854 | f >>= -ep->freqshift; |
| 1855 | |
| 1856 | if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) { |
| 1857 | /* |
| 1858 | * If the frequency looks valid, set it. |
| 1859 | * This value is referred to in prepare_playback_urb(). |
| 1860 | */ |
| 1861 | spin_lock_irqsave(&ep->lock, flags); |
| 1862 | ep->freqm = f; |
| 1863 | spin_unlock_irqrestore(&ep->lock, flags); |
| 1864 | } else { |
| 1865 | /* |
| 1866 | * Out of range; maybe the shift value is wrong. |
| 1867 | * Reset it so that we autodetect again the next time. |
| 1868 | */ |
| 1869 | ep->freqshift = INT_MIN; |
| 1870 | } |
| 1871 | } |
| 1872 | |