1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * US-X2Y AUDIO 4 * Copyright (c) 2002-2004 by Karsten Wiese 5 * 6 * based on 7 * 8 * (Tentative) USB Audio Driver for ALSA 9 * 10 * Main and PCM part 11 * 12 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> 13 * 14 * Many codes borrowed from audio.c by 15 * Alan Cox (alan@lxorguk.ukuu.org.uk) 16 * Thomas Sailer (sailer@ife.ee.ethz.ch) 17 */ 18 19 20 #include <linux/interrupt.h> 21 #include <linux/slab.h> 22 #include <linux/usb.h> 23 #include <linux/moduleparam.h> 24 #include <sound/core.h> 25 #include <sound/info.h> 26 #include <sound/pcm.h> 27 #include <sound/pcm_params.h> 28 #include "usx2y.h" 29 #include "usbusx2y.h" 30 31 static int usx2y_urb_capt_retire(struct snd_usx2y_substream *subs) 32 { 33 struct urb *urb = subs->completed_urb; 34 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 35 unsigned char *cp; 36 int i, len, lens = 0, hwptr_done = subs->hwptr_done; 37 int cnt, blen; 38 struct usx2ydev *usx2y = subs->usx2y; 39 40 for (i = 0; i < nr_of_packs(); i++) { 41 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset; 42 if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */ 43 dev_err(&usx2y->dev->dev, 44 "%s: active frame status %i. Most probably some hardware problem.\n", 45 __func__, 46 urb->iso_frame_desc[i].status); 47 return urb->iso_frame_desc[i].status; 48 } 49 len = urb->iso_frame_desc[i].actual_length / usx2y->stride; 50 if (!len) { 51 dev_dbg(&usx2y->dev->dev, "%s: 0 == len ERROR!\n", __func__); 52 continue; 53 } 54 55 /* copy a data chunk */ 56 if ((hwptr_done + len) > runtime->buffer_size) { 57 cnt = runtime->buffer_size - hwptr_done; 58 blen = cnt * usx2y->stride; 59 memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp, blen); 60 memcpy(runtime->dma_area, cp + blen, len * usx2y->stride - blen); 61 } else { 62 memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp, 63 len * usx2y->stride); 64 } 65 lens += len; 66 hwptr_done += len; 67 if (hwptr_done >= runtime->buffer_size) 68 hwptr_done -= runtime->buffer_size; 69 } 70 71 subs->hwptr_done = hwptr_done; 72 subs->transfer_done += lens; 73 /* update the pointer, call callback if necessary */ 74 if (subs->transfer_done >= runtime->period_size) { 75 subs->transfer_done -= runtime->period_size; 76 snd_pcm_period_elapsed(subs->pcm_substream); 77 } 78 return 0; 79 } 80 81 /* 82 * prepare urb for playback data pipe 83 * 84 * we copy the data directly from the pcm buffer. 85 * the current position to be copied is held in hwptr field. 86 * since a urb can handle only a single linear buffer, if the total 87 * transferred area overflows the buffer boundary, we cannot send 88 * it directly from the buffer. thus the data is once copied to 89 * a temporary buffer and urb points to that. 90 */ 91 static int usx2y_urb_play_prepare(struct snd_usx2y_substream *subs, 92 struct urb *cap_urb, 93 struct urb *urb) 94 { 95 struct usx2ydev *usx2y = subs->usx2y; 96 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 97 int count, counts, pack, len; 98 99 count = 0; 100 for (pack = 0; pack < nr_of_packs(); pack++) { 101 /* calculate the size of a packet */ 102 counts = cap_urb->iso_frame_desc[pack].actual_length / usx2y->stride; 103 count += counts; 104 if (counts < 43 || counts > 50) { 105 dev_err(&usx2y->dev->dev, "%s: should not be here with counts=%i\n", 106 __func__, counts); 107 return -EPIPE; 108 } 109 /* set up descriptor */ 110 urb->iso_frame_desc[pack].offset = pack ? 111 urb->iso_frame_desc[pack - 1].offset + 112 urb->iso_frame_desc[pack - 1].length : 113 0; 114 urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length; 115 } 116 if (atomic_read(&subs->state) >= STATE_PRERUNNING) { 117 if (subs->hwptr + count > runtime->buffer_size) { 118 /* err, the transferred area goes over buffer boundary. 119 * copy the data to the temp buffer. 120 */ 121 len = runtime->buffer_size - subs->hwptr; 122 urb->transfer_buffer = subs->tmpbuf; 123 memcpy(subs->tmpbuf, runtime->dma_area + 124 subs->hwptr * usx2y->stride, len * usx2y->stride); 125 memcpy(subs->tmpbuf + len * usx2y->stride, 126 runtime->dma_area, (count - len) * usx2y->stride); 127 subs->hwptr += count; 128 subs->hwptr -= runtime->buffer_size; 129 } else { 130 /* set the buffer pointer */ 131 urb->transfer_buffer = runtime->dma_area + subs->hwptr * usx2y->stride; 132 subs->hwptr += count; 133 if (subs->hwptr >= runtime->buffer_size) 134 subs->hwptr -= runtime->buffer_size; 135 } 136 } else { 137 urb->transfer_buffer = subs->tmpbuf; 138 } 139 urb->transfer_buffer_length = count * usx2y->stride; 140 return 0; 141 } 142 143 /* 144 * process after playback data complete 145 * 146 * update the current position and call callback if a period is processed. 147 */ 148 static void usx2y_urb_play_retire(struct snd_usx2y_substream *subs, struct urb *urb) 149 { 150 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 151 int len = urb->actual_length / subs->usx2y->stride; 152 153 subs->transfer_done += len; 154 subs->hwptr_done += len; 155 if (subs->hwptr_done >= runtime->buffer_size) 156 subs->hwptr_done -= runtime->buffer_size; 157 if (subs->transfer_done >= runtime->period_size) { 158 subs->transfer_done -= runtime->period_size; 159 snd_pcm_period_elapsed(subs->pcm_substream); 160 } 161 } 162 163 static int usx2y_urb_submit(struct snd_usx2y_substream *subs, struct urb *urb, int frame) 164 { 165 int err; 166 167 if (!urb) 168 return -ENODEV; 169 urb->start_frame = frame + NRURBS * nr_of_packs(); // let hcd do rollover sanity checks 170 urb->dev = subs->usx2y->dev; /* we need to set this at each time */ 171 err = usb_submit_urb(urb, GFP_ATOMIC); 172 if (err < 0) { 173 dev_err(&urb->dev->dev, "%s: usb_submit_urb() returned %i\n", 174 __func__, err); 175 return err; 176 } 177 return 0; 178 } 179 180 static int usx2y_usbframe_complete(struct snd_usx2y_substream *capsubs, 181 struct snd_usx2y_substream *playbacksubs, 182 int frame) 183 { 184 int err, state; 185 struct urb *urb = playbacksubs->completed_urb; 186 187 state = atomic_read(&playbacksubs->state); 188 if (urb) { 189 if (state == STATE_RUNNING) 190 usx2y_urb_play_retire(playbacksubs, urb); 191 else if (state >= STATE_PRERUNNING) 192 atomic_inc(&playbacksubs->state); 193 } else { 194 switch (state) { 195 case STATE_STARTING1: 196 urb = playbacksubs->urb[0]; 197 atomic_inc(&playbacksubs->state); 198 break; 199 case STATE_STARTING2: 200 urb = playbacksubs->urb[1]; 201 atomic_inc(&playbacksubs->state); 202 break; 203 } 204 } 205 if (urb) { 206 err = usx2y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb); 207 if (err) 208 return err; 209 err = usx2y_urb_submit(playbacksubs, urb, frame); 210 if (err) 211 return err; 212 } 213 214 playbacksubs->completed_urb = NULL; 215 216 state = atomic_read(&capsubs->state); 217 if (state >= STATE_PREPARED) { 218 if (state == STATE_RUNNING) { 219 err = usx2y_urb_capt_retire(capsubs); 220 if (err) 221 return err; 222 } else if (state >= STATE_PRERUNNING) { 223 atomic_inc(&capsubs->state); 224 } 225 err = usx2y_urb_submit(capsubs, capsubs->completed_urb, frame); 226 if (err) 227 return err; 228 } 229 capsubs->completed_urb = NULL; 230 return 0; 231 } 232 233 static void usx2y_clients_stop(struct usx2ydev *usx2y) 234 { 235 struct snd_usx2y_substream *subs; 236 struct urb *urb; 237 int s, u; 238 239 for (s = 0; s < 4; s++) { 240 subs = usx2y->subs[s]; 241 if (subs) { 242 dev_dbg(&usx2y->dev->dev, "%s: %i %p state=%i\n", 243 __func__, s, subs, atomic_read(&subs->state)); 244 atomic_set(&subs->state, STATE_STOPPED); 245 } 246 } 247 for (s = 0; s < 4; s++) { 248 subs = usx2y->subs[s]; 249 if (subs) { 250 if (atomic_read(&subs->state) >= STATE_PRERUNNING) 251 snd_pcm_stop_xrun(subs->pcm_substream); 252 for (u = 0; u < NRURBS; u++) { 253 urb = subs->urb[u]; 254 if (urb) 255 dev_dbg(&usx2y->dev->dev, 256 "%s: %i status=%i start_frame=%i\n", 257 __func__, u, urb->status, urb->start_frame); 258 } 259 } 260 } 261 usx2y->prepare_subs = NULL; 262 wake_up(&usx2y->prepare_wait_queue); 263 } 264 265 static void usx2y_error_urb_status(struct usx2ydev *usx2y, 266 struct snd_usx2y_substream *subs, struct urb *urb) 267 { 268 dev_err(&usx2y->dev->dev, "%s: ep=%i stalled with status=%i\n", 269 __func__, subs->endpoint, urb->status); 270 urb->status = 0; 271 usx2y_clients_stop(usx2y); 272 } 273 274 static void i_usx2y_urb_complete(struct urb *urb) 275 { 276 struct snd_usx2y_substream *subs = urb->context; 277 struct usx2ydev *usx2y = subs->usx2y; 278 struct snd_usx2y_substream *capsubs, *playbacksubs; 279 280 if (unlikely(atomic_read(&subs->state) < STATE_PREPARED)) { 281 dev_dbg(&usx2y->dev->dev, 282 "%s: hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", 283 __func__, 284 usb_get_current_frame_number(usx2y->dev), 285 subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", 286 urb->status, urb->start_frame); 287 return; 288 } 289 if (unlikely(urb->status)) { 290 usx2y_error_urb_status(usx2y, subs, urb); 291 return; 292 } 293 294 subs->completed_urb = urb; 295 296 capsubs = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; 297 playbacksubs = usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; 298 299 if (capsubs->completed_urb && 300 atomic_read(&capsubs->state) >= STATE_PREPARED && 301 (playbacksubs->completed_urb || 302 atomic_read(&playbacksubs->state) < STATE_PREPARED)) { 303 if (!usx2y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) { 304 usx2y->wait_iso_frame += nr_of_packs(); 305 } else { 306 usx2y_clients_stop(usx2y); 307 } 308 } 309 } 310 311 static void usx2y_urbs_set_complete(struct usx2ydev *usx2y, 312 void (*complete)(struct urb *)) 313 { 314 struct snd_usx2y_substream *subs; 315 struct urb *urb; 316 int s, u; 317 318 for (s = 0; s < 4; s++) { 319 subs = usx2y->subs[s]; 320 if (subs) { 321 for (u = 0; u < NRURBS; u++) { 322 urb = subs->urb[u]; 323 if (urb) 324 urb->complete = complete; 325 } 326 } 327 } 328 } 329 330 static void usx2y_subs_startup_finish(struct usx2ydev *usx2y) 331 { 332 usx2y_urbs_set_complete(usx2y, i_usx2y_urb_complete); 333 usx2y->prepare_subs = NULL; 334 } 335 336 static void i_usx2y_subs_startup(struct urb *urb) 337 { 338 struct snd_usx2y_substream *subs = urb->context; 339 struct usx2ydev *usx2y = subs->usx2y; 340 struct snd_usx2y_substream *prepare_subs = usx2y->prepare_subs; 341 342 if (prepare_subs) { 343 if (urb->start_frame == prepare_subs->urb[0]->start_frame) { 344 usx2y_subs_startup_finish(usx2y); 345 atomic_inc(&prepare_subs->state); 346 wake_up(&usx2y->prepare_wait_queue); 347 } 348 } 349 350 i_usx2y_urb_complete(urb); 351 } 352 353 static void usx2y_subs_prepare(struct snd_usx2y_substream *subs) 354 { 355 dev_dbg(&subs->usx2y->dev->dev, 356 "%s(%p) ep=%i urb0=%p urb1=%p\n", 357 __func__, subs, subs->endpoint, subs->urb[0], subs->urb[1]); 358 /* reset the pointer */ 359 subs->hwptr = 0; 360 subs->hwptr_done = 0; 361 subs->transfer_done = 0; 362 } 363 364 static void usx2y_urb_release(struct urb **urb, int free_tb) 365 { 366 if (*urb) { 367 usb_kill_urb(*urb); 368 if (free_tb) 369 kfree((*urb)->transfer_buffer); 370 usb_free_urb(*urb); 371 *urb = NULL; 372 } 373 } 374 375 /* 376 * release a substreams urbs 377 */ 378 static void usx2y_urbs_release(struct snd_usx2y_substream *subs) 379 { 380 int i; 381 382 dev_dbg(&subs->usx2y->dev->dev, "%s %i\n", __func__, subs->endpoint); 383 for (i = 0; i < NRURBS; i++) 384 usx2y_urb_release(subs->urb + i, 385 subs != subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]); 386 387 kfree(subs->tmpbuf); 388 subs->tmpbuf = NULL; 389 } 390 391 /* 392 * initialize a substream's urbs 393 */ 394 static int usx2y_urbs_allocate(struct snd_usx2y_substream *subs) 395 { 396 int i; 397 unsigned int pipe; 398 int is_playback = subs == subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; 399 struct usb_device *dev = subs->usx2y->dev; 400 struct urb **purb; 401 402 pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) : 403 usb_rcvisocpipe(dev, subs->endpoint); 404 subs->maxpacksize = usb_maxpacket(dev, pipe); 405 if (!subs->maxpacksize) 406 return -EINVAL; 407 408 if (is_playback && !subs->tmpbuf) { /* allocate a temporary buffer for playback */ 409 subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL); 410 if (!subs->tmpbuf) 411 return -ENOMEM; 412 } 413 /* allocate and initialize data urbs */ 414 for (i = 0; i < NRURBS; i++) { 415 purb = subs->urb + i; 416 if (*purb) { 417 usb_kill_urb(*purb); 418 continue; 419 } 420 *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL); 421 if (!*purb) { 422 usx2y_urbs_release(subs); 423 return -ENOMEM; 424 } 425 if (!is_playback && !(*purb)->transfer_buffer) { 426 /* allocate a capture buffer per urb */ 427 (*purb)->transfer_buffer = 428 kmalloc_array(subs->maxpacksize, 429 nr_of_packs(), GFP_KERNEL); 430 if (!(*purb)->transfer_buffer) { 431 usx2y_urbs_release(subs); 432 return -ENOMEM; 433 } 434 } 435 (*purb)->dev = dev; 436 (*purb)->pipe = pipe; 437 (*purb)->number_of_packets = nr_of_packs(); 438 (*purb)->context = subs; 439 (*purb)->interval = 1; 440 (*purb)->complete = i_usx2y_subs_startup; 441 } 442 return 0; 443 } 444 445 static void usx2y_subs_startup(struct snd_usx2y_substream *subs) 446 { 447 struct usx2ydev *usx2y = subs->usx2y; 448 449 usx2y->prepare_subs = subs; 450 subs->urb[0]->start_frame = -1; 451 wmb(); 452 usx2y_urbs_set_complete(usx2y, i_usx2y_subs_startup); 453 } 454 455 static int usx2y_urbs_start(struct snd_usx2y_substream *subs) 456 { 457 int i, err; 458 struct usx2ydev *usx2y = subs->usx2y; 459 struct urb *urb; 460 unsigned long pack; 461 462 err = usx2y_urbs_allocate(subs); 463 if (err < 0) 464 return err; 465 subs->completed_urb = NULL; 466 for (i = 0; i < 4; i++) { 467 struct snd_usx2y_substream *subs = usx2y->subs[i]; 468 469 if (subs && atomic_read(&subs->state) >= STATE_PREPARED) 470 goto start; 471 } 472 473 start: 474 usx2y_subs_startup(subs); 475 for (i = 0; i < NRURBS; i++) { 476 urb = subs->urb[i]; 477 if (usb_pipein(urb->pipe)) { 478 if (!i) 479 atomic_set(&subs->state, STATE_STARTING3); 480 urb->dev = usx2y->dev; 481 for (pack = 0; pack < nr_of_packs(); pack++) { 482 urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack; 483 urb->iso_frame_desc[pack].length = subs->maxpacksize; 484 } 485 urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); 486 err = usb_submit_urb(urb, GFP_ATOMIC); 487 if (err < 0) { 488 dev_err(&urb->dev->dev, "%s: cannot submit datapipe for urb %d, err = %d\n", 489 __func__, i, err); 490 err = -EPIPE; 491 goto cleanup; 492 } else { 493 if (!i) 494 usx2y->wait_iso_frame = urb->start_frame; 495 } 496 urb->transfer_flags = 0; 497 } else { 498 atomic_set(&subs->state, STATE_STARTING1); 499 break; 500 } 501 } 502 err = 0; 503 wait_event(usx2y->prepare_wait_queue, !usx2y->prepare_subs); 504 if (atomic_read(&subs->state) != STATE_PREPARED) 505 err = -EPIPE; 506 507 cleanup: 508 if (err) { 509 usx2y_subs_startup_finish(usx2y); 510 usx2y_clients_stop(usx2y); // something is completely wrong > stop everything 511 } 512 return err; 513 } 514 515 /* 516 * return the current pcm pointer. just return the hwptr_done value. 517 */ 518 static snd_pcm_uframes_t snd_usx2y_pcm_pointer(struct snd_pcm_substream *substream) 519 { 520 struct snd_usx2y_substream *subs = substream->runtime->private_data; 521 522 return subs->hwptr_done; 523 } 524 525 /* 526 * start/stop substream 527 */ 528 static int snd_usx2y_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 529 { 530 struct snd_usx2y_substream *subs = substream->runtime->private_data; 531 532 switch (cmd) { 533 case SNDRV_PCM_TRIGGER_START: 534 dev_dbg(&subs->usx2y->dev->dev, "%s(START)\n", __func__); 535 if (atomic_read(&subs->state) == STATE_PREPARED && 536 atomic_read(&subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= STATE_PREPARED) { 537 atomic_set(&subs->state, STATE_PRERUNNING); 538 } else { 539 return -EPIPE; 540 } 541 break; 542 case SNDRV_PCM_TRIGGER_STOP: 543 dev_dbg(&subs->usx2y->dev->dev, "%s(STOP)\n", __func__); 544 if (atomic_read(&subs->state) >= STATE_PRERUNNING) 545 atomic_set(&subs->state, STATE_PREPARED); 546 break; 547 default: 548 return -EINVAL; 549 } 550 return 0; 551 } 552 553 /* 554 * allocate a buffer, setup samplerate 555 * 556 * so far we use a physically linear buffer although packetize transfer 557 * doesn't need a continuous area. 558 * if sg buffer is supported on the later version of alsa, we'll follow 559 * that. 560 */ 561 struct s_c2 { 562 char c1, c2; 563 }; 564 565 static const struct s_c2 setrate_44100[] = { 566 { 0x14, 0x08}, // this line sets 44100, well actually a little less 567 { 0x18, 0x40}, // only tascam / frontier design knows the further lines ....... 568 { 0x18, 0x42}, 569 { 0x18, 0x45}, 570 { 0x18, 0x46}, 571 { 0x18, 0x48}, 572 { 0x18, 0x4A}, 573 { 0x18, 0x4C}, 574 { 0x18, 0x4E}, 575 { 0x18, 0x50}, 576 { 0x18, 0x52}, 577 { 0x18, 0x54}, 578 { 0x18, 0x56}, 579 { 0x18, 0x58}, 580 { 0x18, 0x5A}, 581 { 0x18, 0x5C}, 582 { 0x18, 0x5E}, 583 { 0x18, 0x60}, 584 { 0x18, 0x62}, 585 { 0x18, 0x64}, 586 { 0x18, 0x66}, 587 { 0x18, 0x68}, 588 { 0x18, 0x6A}, 589 { 0x18, 0x6C}, 590 { 0x18, 0x6E}, 591 { 0x18, 0x70}, 592 { 0x18, 0x72}, 593 { 0x18, 0x74}, 594 { 0x18, 0x76}, 595 { 0x18, 0x78}, 596 { 0x18, 0x7A}, 597 { 0x18, 0x7C}, 598 { 0x18, 0x7E} 599 }; 600 601 static const struct s_c2 setrate_48000[] = { 602 { 0x14, 0x09}, // this line sets 48000, well actually a little less 603 { 0x18, 0x40}, // only tascam / frontier design knows the further lines ....... 604 { 0x18, 0x42}, 605 { 0x18, 0x45}, 606 { 0x18, 0x46}, 607 { 0x18, 0x48}, 608 { 0x18, 0x4A}, 609 { 0x18, 0x4C}, 610 { 0x18, 0x4E}, 611 { 0x18, 0x50}, 612 { 0x18, 0x52}, 613 { 0x18, 0x54}, 614 { 0x18, 0x56}, 615 { 0x18, 0x58}, 616 { 0x18, 0x5A}, 617 { 0x18, 0x5C}, 618 { 0x18, 0x5E}, 619 { 0x18, 0x60}, 620 { 0x18, 0x62}, 621 { 0x18, 0x64}, 622 { 0x18, 0x66}, 623 { 0x18, 0x68}, 624 { 0x18, 0x6A}, 625 { 0x18, 0x6C}, 626 { 0x18, 0x6E}, 627 { 0x18, 0x70}, 628 { 0x18, 0x73}, 629 { 0x18, 0x74}, 630 { 0x18, 0x76}, 631 { 0x18, 0x78}, 632 { 0x18, 0x7A}, 633 { 0x18, 0x7C}, 634 { 0x18, 0x7E} 635 }; 636 637 #define NOOF_SETRATE_URBS ARRAY_SIZE(setrate_48000) 638 639 static void i_usx2y_04int(struct urb *urb) 640 { 641 struct usx2ydev *usx2y = urb->context; 642 643 if (urb->status) 644 dev_err(&urb->dev->dev, "%s() urb->status=%i\n", 645 __func__, urb->status); 646 if (!--usx2y->us04->len) 647 wake_up(&usx2y->in04_wait_queue); 648 } 649 650 static int usx2y_rate_set(struct usx2ydev *usx2y, int rate) 651 { 652 int err = 0, i; 653 struct snd_usx2y_urb_seq *us = NULL; 654 int *usbdata = NULL; 655 const struct s_c2 *ra = rate == 48000 ? setrate_48000 : setrate_44100; 656 struct urb *urb; 657 658 if (usx2y->rate != rate) { 659 us = kzalloc_flex(*us, urb, NOOF_SETRATE_URBS); 660 if (!us) { 661 err = -ENOMEM; 662 goto cleanup; 663 } 664 us->len = NOOF_SETRATE_URBS; 665 usbdata = kmalloc_objs(int, NOOF_SETRATE_URBS); 666 if (!usbdata) { 667 err = -ENOMEM; 668 goto cleanup; 669 } 670 for (i = 0; i < NOOF_SETRATE_URBS; ++i) { 671 us->urb[i] = usb_alloc_urb(0, GFP_KERNEL); 672 if (!us->urb[i]) { 673 err = -ENOMEM; 674 goto cleanup; 675 } 676 ((char *)(usbdata + i))[0] = ra[i].c1; 677 ((char *)(usbdata + i))[1] = ra[i].c2; 678 usb_fill_bulk_urb(us->urb[i], usx2y->dev, usb_sndbulkpipe(usx2y->dev, 4), 679 usbdata + i, 2, i_usx2y_04int, usx2y); 680 } 681 err = usb_urb_ep_type_check(us->urb[0]); 682 if (err < 0) 683 goto cleanup; 684 us->submitted = 0; 685 usx2y->us04 = us; 686 wait_event_timeout(usx2y->in04_wait_queue, !us->len, HZ); 687 usx2y->us04 = NULL; 688 if (us->len) 689 err = -ENODEV; 690 cleanup: 691 if (us) { 692 us->submitted = 2*NOOF_SETRATE_URBS; 693 for (i = 0; i < NOOF_SETRATE_URBS; ++i) { 694 urb = us->urb[i]; 695 if (!urb) 696 continue; 697 if (urb->status) { 698 if (!err) 699 err = -ENODEV; 700 usb_kill_urb(urb); 701 } 702 usb_free_urb(urb); 703 } 704 usx2y->us04 = NULL; 705 kfree(usbdata); 706 kfree(us); 707 if (!err) 708 usx2y->rate = rate; 709 } 710 } 711 712 return err; 713 } 714 715 static int usx2y_format_set(struct usx2ydev *usx2y, snd_pcm_format_t format) 716 { 717 int alternate, err; 718 struct list_head *p; 719 720 if (format == SNDRV_PCM_FORMAT_S24_3LE) { 721 alternate = 2; 722 usx2y->stride = 6; 723 } else { 724 alternate = 1; 725 usx2y->stride = 4; 726 } 727 list_for_each(p, &usx2y->midi_list) { 728 snd_usbmidi_input_stop(p); 729 } 730 usb_kill_urb(usx2y->in04_urb); 731 err = usb_set_interface(usx2y->dev, 0, alternate); 732 if (err) { 733 dev_err(&usx2y->dev->dev, "%s: usb_set_interface error\n", 734 __func__); 735 return err; 736 } 737 usx2y->in04_urb->dev = usx2y->dev; 738 err = usb_submit_urb(usx2y->in04_urb, GFP_KERNEL); 739 list_for_each(p, &usx2y->midi_list) { 740 snd_usbmidi_input_start(p); 741 } 742 usx2y->format = format; 743 usx2y->rate = 0; 744 return err; 745 } 746 747 748 static int snd_usx2y_pcm_hw_params(struct snd_pcm_substream *substream, 749 struct snd_pcm_hw_params *hw_params) 750 { 751 unsigned int rate = params_rate(hw_params); 752 snd_pcm_format_t format = params_format(hw_params); 753 struct snd_card *card = substream->pstr->pcm->card; 754 struct usx2ydev *dev = usx2y(card); 755 struct snd_usx2y_substream *subs; 756 struct snd_pcm_substream *test_substream; 757 int i; 758 759 guard(mutex)(&usx2y(card)->pcm_mutex); 760 dev_dbg(&dev->dev->dev, "%s(%p, %p)\n", __func__, substream, hw_params); 761 /* all pcm substreams off one usx2y have to operate at the same 762 * rate & format 763 */ 764 for (i = 0; i < dev->pcm_devs * 2; i++) { 765 subs = dev->subs[i]; 766 if (!subs) 767 continue; 768 test_substream = subs->pcm_substream; 769 if (!test_substream || test_substream == substream || 770 !test_substream->runtime) 771 continue; 772 if ((test_substream->runtime->format && 773 test_substream->runtime->format != format) || 774 (test_substream->runtime->rate && 775 test_substream->runtime->rate != rate)) { 776 return -EINVAL; 777 } 778 } 779 780 return 0; 781 } 782 783 /* 784 * free the buffer 785 */ 786 static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream) 787 { 788 struct snd_pcm_runtime *runtime = substream->runtime; 789 struct snd_usx2y_substream *subs = runtime->private_data; 790 struct snd_usx2y_substream *cap_subs, *playback_subs; 791 792 guard(mutex)(&subs->usx2y->pcm_mutex); 793 dev_dbg(&subs->usx2y->dev->dev, "%s(%p)\n", __func__, substream); 794 795 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 796 cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; 797 atomic_set(&subs->state, STATE_STOPPED); 798 usx2y_urbs_release(subs); 799 if (!cap_subs->pcm_substream || 800 !cap_subs->pcm_substream->runtime || 801 cap_subs->pcm_substream->runtime->state < SNDRV_PCM_STATE_PREPARED) { 802 atomic_set(&cap_subs->state, STATE_STOPPED); 803 usx2y_urbs_release(cap_subs); 804 } 805 } else { 806 playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]; 807 if (atomic_read(&playback_subs->state) < STATE_PREPARED) { 808 atomic_set(&subs->state, STATE_STOPPED); 809 usx2y_urbs_release(subs); 810 } 811 } 812 return 0; 813 } 814 815 /* 816 * prepare callback 817 * 818 * set format and initialize urbs 819 */ 820 static int snd_usx2y_pcm_prepare(struct snd_pcm_substream *substream) 821 { 822 struct snd_pcm_runtime *runtime = substream->runtime; 823 struct snd_usx2y_substream *subs = runtime->private_data; 824 struct usx2ydev *usx2y = subs->usx2y; 825 struct snd_usx2y_substream *capsubs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]; 826 int err = 0; 827 828 dev_dbg(&usx2y->dev->dev, "%s(%p)\n", __func__, substream); 829 830 guard(mutex)(&usx2y->pcm_mutex); 831 usx2y_subs_prepare(subs); 832 // Start hardware streams 833 // SyncStream first.... 834 if (atomic_read(&capsubs->state) < STATE_PREPARED) { 835 if (usx2y->format != runtime->format) { 836 err = usx2y_format_set(usx2y, runtime->format); 837 if (err < 0) 838 return err; 839 } 840 if (usx2y->rate != runtime->rate) { 841 err = usx2y_rate_set(usx2y, runtime->rate); 842 if (err < 0) 843 return err; 844 } 845 dev_dbg(&usx2y->dev->dev, "%s: starting capture pipe for %s\n", 846 __func__, subs == capsubs ? "self" : "playpipe"); 847 err = usx2y_urbs_start(capsubs); 848 if (err < 0) 849 return err; 850 } 851 852 if (subs != capsubs && atomic_read(&subs->state) < STATE_PREPARED) 853 err = usx2y_urbs_start(subs); 854 855 return err; 856 } 857 858 static const struct snd_pcm_hardware snd_usx2y_2c = { 859 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 860 SNDRV_PCM_INFO_BLOCK_TRANSFER | 861 SNDRV_PCM_INFO_MMAP_VALID | 862 SNDRV_PCM_INFO_BATCH), 863 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE, 864 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, 865 .rate_min = 44100, 866 .rate_max = 48000, 867 .channels_min = 2, 868 .channels_max = 2, 869 .buffer_bytes_max = (2*128*1024), 870 .period_bytes_min = 64, 871 .period_bytes_max = (128*1024), 872 .periods_min = 2, 873 .periods_max = 1024, 874 .fifo_size = 0 875 }; 876 877 static int snd_usx2y_pcm_open(struct snd_pcm_substream *substream) 878 { 879 struct snd_usx2y_substream *subs = 880 ((struct snd_usx2y_substream **) 881 snd_pcm_substream_chip(substream))[substream->stream]; 882 struct snd_pcm_runtime *runtime = substream->runtime; 883 884 if (subs->usx2y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS) 885 return -EBUSY; 886 887 runtime->hw = snd_usx2y_2c; 888 runtime->private_data = subs; 889 subs->pcm_substream = substream; 890 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000); 891 return 0; 892 } 893 894 static int snd_usx2y_pcm_close(struct snd_pcm_substream *substream) 895 { 896 struct snd_pcm_runtime *runtime = substream->runtime; 897 struct snd_usx2y_substream *subs = runtime->private_data; 898 899 subs->pcm_substream = NULL; 900 901 return 0; 902 } 903 904 static const struct snd_pcm_ops snd_usx2y_pcm_ops = { 905 .open = snd_usx2y_pcm_open, 906 .close = snd_usx2y_pcm_close, 907 .hw_params = snd_usx2y_pcm_hw_params, 908 .hw_free = snd_usx2y_pcm_hw_free, 909 .prepare = snd_usx2y_pcm_prepare, 910 .trigger = snd_usx2y_pcm_trigger, 911 .pointer = snd_usx2y_pcm_pointer, 912 }; 913 914 /* 915 * free a usb stream instance 916 */ 917 static void usx2y_audio_stream_free(struct snd_usx2y_substream **usx2y_substream) 918 { 919 int stream; 920 921 for_each_pcm_streams(stream) { 922 kfree(usx2y_substream[stream]); 923 usx2y_substream[stream] = NULL; 924 } 925 } 926 927 static void snd_usx2y_pcm_private_free(struct snd_pcm *pcm) 928 { 929 struct snd_usx2y_substream **usx2y_stream = pcm->private_data; 930 931 if (usx2y_stream) 932 usx2y_audio_stream_free(usx2y_stream); 933 } 934 935 static int usx2y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint) 936 { 937 struct snd_pcm *pcm; 938 int err, i; 939 struct snd_usx2y_substream **usx2y_substream = 940 usx2y(card)->subs + 2 * usx2y(card)->pcm_devs; 941 942 for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; 943 i <= SNDRV_PCM_STREAM_CAPTURE; ++i) { 944 usx2y_substream[i] = kzalloc_obj(struct snd_usx2y_substream); 945 if (!usx2y_substream[i]) 946 return -ENOMEM; 947 948 usx2y_substream[i]->usx2y = usx2y(card); 949 } 950 951 if (playback_endpoint) 952 usx2y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint; 953 usx2y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint; 954 955 err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usx2y(card)->pcm_devs, 956 playback_endpoint ? 1 : 0, 1, 957 &pcm); 958 if (err < 0) { 959 usx2y_audio_stream_free(usx2y_substream); 960 return err; 961 } 962 963 if (playback_endpoint) 964 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usx2y_pcm_ops); 965 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usx2y_pcm_ops); 966 967 pcm->private_data = usx2y_substream; 968 pcm->private_free = snd_usx2y_pcm_private_free; 969 pcm->info_flags = 0; 970 971 sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usx2y(card)->pcm_devs); 972 973 if (playback_endpoint) { 974 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream, 975 SNDRV_DMA_TYPE_CONTINUOUS, 976 NULL, 977 64*1024, 128*1024); 978 } 979 980 snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, 981 SNDRV_DMA_TYPE_CONTINUOUS, 982 NULL, 983 64*1024, 128*1024); 984 usx2y(card)->pcm_devs++; 985 986 return 0; 987 } 988 989 /* 990 * create a chip instance and set its names. 991 */ 992 int usx2y_audio_create(struct snd_card *card) 993 { 994 int err; 995 996 err = usx2y_audio_stream_new(card, 0xA, 0x8); 997 if (err < 0) 998 return err; 999 if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) == USB_ID_US428) { 1000 err = usx2y_audio_stream_new(card, 0, 0xA); 1001 if (err < 0) 1002 return err; 1003 } 1004 if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) != USB_ID_US122) 1005 err = usx2y_rate_set(usx2y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122. 1006 return err; 1007 } 1008