1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2006-2008 Daniel Mack, Karsten Wiese 4 */ 5 6 #include <linux/device.h> 7 #include <linux/spinlock.h> 8 #include <linux/slab.h> 9 #include <linux/init.h> 10 #include <linux/usb.h> 11 #include <sound/core.h> 12 #include <sound/pcm.h> 13 14 #include "device.h" 15 #include "audio.h" 16 17 #define N_URBS 32 18 #define CLOCK_DRIFT_TOLERANCE 5 19 #define FRAMES_PER_URB 8 20 #define BYTES_PER_FRAME 512 21 #define CHANNELS_PER_STREAM 2 22 #define BYTES_PER_SAMPLE 3 23 #define BYTES_PER_SAMPLE_USB 4 24 #define MAX_BUFFER_SIZE (128*1024) 25 #define MAX_ENDPOINT_SIZE 512 26 27 #define ENDPOINT_CAPTURE 2 28 #define ENDPOINT_PLAYBACK 6 29 30 #define MAKE_CHECKBYTE(cdev,stream,i) \ 31 (stream << 1) | (~(i / (cdev->n_streams * BYTES_PER_SAMPLE_USB)) & 1) 32 33 static const struct snd_pcm_hardware snd_usb_caiaq_pcm_hardware = { 34 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 35 SNDRV_PCM_INFO_BLOCK_TRANSFER), 36 .formats = SNDRV_PCM_FMTBIT_S24_3BE, 37 .rates = (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | 38 SNDRV_PCM_RATE_96000), 39 .rate_min = 44100, 40 .rate_max = 0, /* will overwrite later */ 41 .channels_min = CHANNELS_PER_STREAM, 42 .channels_max = CHANNELS_PER_STREAM, 43 .buffer_bytes_max = MAX_BUFFER_SIZE, 44 .period_bytes_min = 128, 45 .period_bytes_max = MAX_BUFFER_SIZE, 46 .periods_min = 1, 47 .periods_max = 1024, 48 }; 49 50 static void 51 activate_substream(struct snd_usb_caiaqdev *cdev, 52 struct snd_pcm_substream *sub) 53 { 54 guard(spinlock)(&cdev->spinlock); 55 56 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) 57 cdev->sub_playback[sub->number] = sub; 58 else 59 cdev->sub_capture[sub->number] = sub; 60 } 61 62 static void 63 deactivate_substream(struct snd_usb_caiaqdev *cdev, 64 struct snd_pcm_substream *sub) 65 { 66 guard(spinlock_irqsave)(&cdev->spinlock); 67 68 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) 69 cdev->sub_playback[sub->number] = NULL; 70 else 71 cdev->sub_capture[sub->number] = NULL; 72 } 73 74 static int 75 all_substreams_zero(struct snd_pcm_substream **subs) 76 { 77 int i; 78 for (i = 0; i < MAX_STREAMS; i++) 79 if (subs[i] != NULL) 80 return 0; 81 return 1; 82 } 83 84 static int stream_start(struct snd_usb_caiaqdev *cdev) 85 { 86 int i, ret; 87 struct device *dev = caiaqdev_to_dev(cdev); 88 89 dev_dbg(dev, "%s(%p)\n", __func__, cdev); 90 91 if (cdev->streaming) 92 return -EINVAL; 93 94 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback)); 95 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture)); 96 cdev->input_panic = 0; 97 cdev->output_panic = 0; 98 cdev->first_packet = 4; 99 cdev->streaming = 1; 100 cdev->warned = 0; 101 102 for (i = 0; i < N_URBS; i++) { 103 ret = usb_submit_urb(cdev->data_urbs_in[i], GFP_ATOMIC); 104 if (ret) { 105 dev_err(dev, "unable to trigger read #%d! (ret %d)\n", 106 i, ret); 107 cdev->streaming = 0; 108 return -EPIPE; 109 } 110 } 111 112 return 0; 113 } 114 115 static void stream_stop(struct snd_usb_caiaqdev *cdev) 116 { 117 int i; 118 struct device *dev = caiaqdev_to_dev(cdev); 119 120 dev_dbg(dev, "%s(%p)\n", __func__, cdev); 121 if (!cdev->streaming) 122 return; 123 124 cdev->streaming = 0; 125 126 for (i = 0; i < N_URBS; i++) { 127 usb_kill_urb(cdev->data_urbs_in[i]); 128 129 if (test_bit(i, &cdev->outurb_active_mask)) 130 usb_kill_urb(cdev->data_urbs_out[i]); 131 } 132 133 cdev->outurb_active_mask = 0; 134 } 135 136 static int snd_usb_caiaq_substream_open(struct snd_pcm_substream *substream) 137 { 138 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream); 139 struct device *dev = caiaqdev_to_dev(cdev); 140 141 dev_dbg(dev, "%s(%p)\n", __func__, substream); 142 substream->runtime->hw = cdev->pcm_info; 143 snd_pcm_limit_hw_rates(substream->runtime); 144 145 return 0; 146 } 147 148 static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream) 149 { 150 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream); 151 struct device *dev = caiaqdev_to_dev(cdev); 152 153 dev_dbg(dev, "%s(%p)\n", __func__, substream); 154 if (all_substreams_zero(cdev->sub_playback) && 155 all_substreams_zero(cdev->sub_capture)) { 156 /* when the last client has stopped streaming, 157 * all sample rates are allowed again */ 158 stream_stop(cdev); 159 cdev->pcm_info.rates = cdev->samplerates; 160 } 161 162 return 0; 163 } 164 165 static int snd_usb_caiaq_pcm_hw_free(struct snd_pcm_substream *sub) 166 { 167 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub); 168 deactivate_substream(cdev, sub); 169 return 0; 170 } 171 172 static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream) 173 { 174 int bytes_per_sample, bpp, ret; 175 int index = substream->number; 176 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(substream); 177 struct snd_pcm_runtime *runtime = substream->runtime; 178 struct device *dev = caiaqdev_to_dev(cdev); 179 180 dev_dbg(dev, "%s(%p)\n", __func__, substream); 181 182 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 183 int out_pos; 184 185 switch (cdev->spec.data_alignment) { 186 case 0: 187 case 2: 188 out_pos = BYTES_PER_SAMPLE + 1; 189 break; 190 case 3: 191 default: 192 out_pos = 0; 193 break; 194 } 195 196 cdev->period_out_count[index] = out_pos; 197 cdev->audio_out_buf_pos[index] = out_pos; 198 } else { 199 int in_pos; 200 201 switch (cdev->spec.data_alignment) { 202 case 0: 203 in_pos = BYTES_PER_SAMPLE + 2; 204 break; 205 case 2: 206 in_pos = BYTES_PER_SAMPLE; 207 break; 208 case 3: 209 default: 210 in_pos = 0; 211 break; 212 } 213 214 cdev->period_in_count[index] = in_pos; 215 cdev->audio_in_buf_pos[index] = in_pos; 216 } 217 218 if (cdev->streaming) 219 return 0; 220 221 /* the first client that opens a stream defines the sample rate 222 * setting for all subsequent calls, until the last client closed. */ 223 cdev->pcm_info.rates = snd_pcm_rate_to_rate_bit(runtime->rate); 224 snd_pcm_limit_hw_rates(runtime); 225 226 bytes_per_sample = BYTES_PER_SAMPLE; 227 if (cdev->spec.data_alignment >= 2) 228 bytes_per_sample++; 229 230 bpp = ((runtime->rate / 8000) + CLOCK_DRIFT_TOLERANCE) 231 * bytes_per_sample * CHANNELS_PER_STREAM * cdev->n_streams; 232 233 if (bpp > MAX_ENDPOINT_SIZE) 234 bpp = MAX_ENDPOINT_SIZE; 235 236 ret = snd_usb_caiaq_set_audio_params(cdev, runtime->rate, 237 runtime->sample_bits, bpp); 238 if (ret) 239 return ret; 240 241 ret = stream_start(cdev); 242 if (ret) 243 return ret; 244 245 cdev->output_running = 0; 246 wait_event_timeout(cdev->prepare_wait_queue, cdev->output_running, HZ); 247 if (!cdev->output_running) { 248 stream_stop(cdev); 249 return -EPIPE; 250 } 251 252 return 0; 253 } 254 255 static int snd_usb_caiaq_pcm_trigger(struct snd_pcm_substream *sub, int cmd) 256 { 257 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub); 258 struct device *dev = caiaqdev_to_dev(cdev); 259 260 dev_dbg(dev, "%s(%p) cmd %d\n", __func__, sub, cmd); 261 262 switch (cmd) { 263 case SNDRV_PCM_TRIGGER_START: 264 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 265 activate_substream(cdev, sub); 266 break; 267 case SNDRV_PCM_TRIGGER_STOP: 268 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 269 deactivate_substream(cdev, sub); 270 break; 271 default: 272 return -EINVAL; 273 } 274 275 return 0; 276 } 277 278 static snd_pcm_uframes_t 279 snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub) 280 { 281 int index = sub->number; 282 struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub); 283 284 guard(spinlock)(&cdev->spinlock); 285 286 if (cdev->input_panic || cdev->output_panic) 287 return SNDRV_PCM_POS_XRUN; 288 289 if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) 290 return bytes_to_frames(sub->runtime, 291 cdev->audio_out_buf_pos[index]); 292 else 293 return bytes_to_frames(sub->runtime, 294 cdev->audio_in_buf_pos[index]); 295 } 296 297 /* operators for both playback and capture */ 298 static const struct snd_pcm_ops snd_usb_caiaq_ops = { 299 .open = snd_usb_caiaq_substream_open, 300 .close = snd_usb_caiaq_substream_close, 301 .hw_free = snd_usb_caiaq_pcm_hw_free, 302 .prepare = snd_usb_caiaq_pcm_prepare, 303 .trigger = snd_usb_caiaq_pcm_trigger, 304 .pointer = snd_usb_caiaq_pcm_pointer, 305 }; 306 307 static void check_for_elapsed_periods(struct snd_usb_caiaqdev *cdev, 308 struct snd_pcm_substream **subs) 309 { 310 int stream, pb, *cnt; 311 struct snd_pcm_substream *sub; 312 313 for (stream = 0; stream < cdev->n_streams; stream++) { 314 sub = subs[stream]; 315 if (!sub) 316 continue; 317 318 pb = snd_pcm_lib_period_bytes(sub); 319 cnt = (sub->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 320 &cdev->period_out_count[stream] : 321 &cdev->period_in_count[stream]; 322 323 if (*cnt >= pb) { 324 snd_pcm_period_elapsed(sub); 325 *cnt %= pb; 326 } 327 } 328 } 329 330 static void read_in_urb_mode0(struct snd_usb_caiaqdev *cdev, 331 const struct urb *urb, 332 const struct usb_iso_packet_descriptor *iso) 333 { 334 unsigned char *usb_buf = urb->transfer_buffer + iso->offset; 335 struct snd_pcm_substream *sub; 336 int stream, i; 337 338 if (all_substreams_zero(cdev->sub_capture)) 339 return; 340 341 for (i = 0; i < iso->actual_length;) { 342 for (stream = 0; stream < cdev->n_streams; stream++, i++) { 343 sub = cdev->sub_capture[stream]; 344 if (sub) { 345 struct snd_pcm_runtime *rt = sub->runtime; 346 char *audio_buf = rt->dma_area; 347 int sz = frames_to_bytes(rt, rt->buffer_size); 348 audio_buf[cdev->audio_in_buf_pos[stream]++] 349 = usb_buf[i]; 350 cdev->period_in_count[stream]++; 351 if (cdev->audio_in_buf_pos[stream] == sz) 352 cdev->audio_in_buf_pos[stream] = 0; 353 } 354 } 355 } 356 } 357 358 static void read_in_urb_mode2(struct snd_usb_caiaqdev *cdev, 359 const struct urb *urb, 360 const struct usb_iso_packet_descriptor *iso) 361 { 362 unsigned char *usb_buf = urb->transfer_buffer + iso->offset; 363 unsigned char check_byte; 364 struct snd_pcm_substream *sub; 365 int stream, i; 366 367 for (i = 0; i < iso->actual_length;) { 368 if (i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 0) { 369 for (stream = 0; 370 stream < cdev->n_streams; 371 stream++, i++) { 372 if (cdev->first_packet) 373 continue; 374 375 check_byte = MAKE_CHECKBYTE(cdev, stream, i); 376 377 if ((usb_buf[i] & 0x3f) != check_byte) 378 cdev->input_panic = 1; 379 380 if (usb_buf[i] & 0x80) 381 cdev->output_panic = 1; 382 } 383 } 384 cdev->first_packet = 0; 385 386 for (stream = 0; stream < cdev->n_streams; stream++, i++) { 387 sub = cdev->sub_capture[stream]; 388 if (cdev->input_panic) 389 usb_buf[i] = 0; 390 391 if (sub) { 392 struct snd_pcm_runtime *rt = sub->runtime; 393 char *audio_buf = rt->dma_area; 394 int sz = frames_to_bytes(rt, rt->buffer_size); 395 audio_buf[cdev->audio_in_buf_pos[stream]++] = 396 usb_buf[i]; 397 cdev->period_in_count[stream]++; 398 if (cdev->audio_in_buf_pos[stream] == sz) 399 cdev->audio_in_buf_pos[stream] = 0; 400 } 401 } 402 } 403 } 404 405 static void read_in_urb_mode3(struct snd_usb_caiaqdev *cdev, 406 const struct urb *urb, 407 const struct usb_iso_packet_descriptor *iso) 408 { 409 unsigned char *usb_buf = urb->transfer_buffer + iso->offset; 410 struct device *dev = caiaqdev_to_dev(cdev); 411 int stream, i; 412 413 /* paranoia check */ 414 if (iso->actual_length % (BYTES_PER_SAMPLE_USB * CHANNELS_PER_STREAM)) 415 return; 416 417 for (i = 0; i < iso->actual_length;) { 418 for (stream = 0; stream < cdev->n_streams; stream++) { 419 struct snd_pcm_substream *sub = cdev->sub_capture[stream]; 420 char *audio_buf = NULL; 421 int c, n, sz = 0; 422 423 if (sub && !cdev->input_panic) { 424 struct snd_pcm_runtime *rt = sub->runtime; 425 audio_buf = rt->dma_area; 426 sz = frames_to_bytes(rt, rt->buffer_size); 427 } 428 429 for (c = 0; c < CHANNELS_PER_STREAM; c++) { 430 /* 3 audio data bytes, followed by 1 check byte */ 431 if (audio_buf) { 432 for (n = 0; n < BYTES_PER_SAMPLE; n++) { 433 audio_buf[cdev->audio_in_buf_pos[stream]++] = usb_buf[i+n]; 434 435 if (cdev->audio_in_buf_pos[stream] == sz) 436 cdev->audio_in_buf_pos[stream] = 0; 437 } 438 439 cdev->period_in_count[stream] += BYTES_PER_SAMPLE; 440 } 441 442 i += BYTES_PER_SAMPLE; 443 444 if (usb_buf[i] != ((stream << 1) | c) && 445 !cdev->first_packet) { 446 if (!cdev->input_panic) 447 dev_warn(dev, " EXPECTED: %02x got %02x, c %d, stream %d, i %d\n", 448 ((stream << 1) | c), usb_buf[i], c, stream, i); 449 cdev->input_panic = 1; 450 } 451 452 i++; 453 } 454 } 455 } 456 457 if (cdev->first_packet > 0) 458 cdev->first_packet--; 459 } 460 461 static void read_in_urb(struct snd_usb_caiaqdev *cdev, 462 const struct urb *urb, 463 const struct usb_iso_packet_descriptor *iso) 464 { 465 struct device *dev = caiaqdev_to_dev(cdev); 466 467 if (!cdev->streaming) 468 return; 469 470 if (iso->actual_length < cdev->bpp) 471 return; 472 473 switch (cdev->spec.data_alignment) { 474 case 0: 475 read_in_urb_mode0(cdev, urb, iso); 476 break; 477 case 2: 478 read_in_urb_mode2(cdev, urb, iso); 479 break; 480 case 3: 481 read_in_urb_mode3(cdev, urb, iso); 482 break; 483 } 484 485 if ((cdev->input_panic || cdev->output_panic) && !cdev->warned) { 486 dev_warn(dev, "streaming error detected %s %s\n", 487 cdev->input_panic ? "(input)" : "", 488 cdev->output_panic ? "(output)" : ""); 489 cdev->warned = 1; 490 } 491 } 492 493 static void fill_out_urb_mode_0(struct snd_usb_caiaqdev *cdev, 494 struct urb *urb, 495 const struct usb_iso_packet_descriptor *iso) 496 { 497 unsigned char *usb_buf = urb->transfer_buffer + iso->offset; 498 struct snd_pcm_substream *sub; 499 int stream, i; 500 501 for (i = 0; i < iso->length;) { 502 for (stream = 0; stream < cdev->n_streams; stream++, i++) { 503 sub = cdev->sub_playback[stream]; 504 if (sub) { 505 struct snd_pcm_runtime *rt = sub->runtime; 506 char *audio_buf = rt->dma_area; 507 int sz = frames_to_bytes(rt, rt->buffer_size); 508 usb_buf[i] = 509 audio_buf[cdev->audio_out_buf_pos[stream]]; 510 cdev->period_out_count[stream]++; 511 cdev->audio_out_buf_pos[stream]++; 512 if (cdev->audio_out_buf_pos[stream] == sz) 513 cdev->audio_out_buf_pos[stream] = 0; 514 } else 515 usb_buf[i] = 0; 516 } 517 518 /* fill in the check bytes */ 519 if (cdev->spec.data_alignment == 2 && 520 i % (cdev->n_streams * BYTES_PER_SAMPLE_USB) == 521 (cdev->n_streams * CHANNELS_PER_STREAM)) 522 for (stream = 0; stream < cdev->n_streams; stream++, i++) 523 usb_buf[i] = MAKE_CHECKBYTE(cdev, stream, i); 524 } 525 } 526 527 static void fill_out_urb_mode_3(struct snd_usb_caiaqdev *cdev, 528 struct urb *urb, 529 const struct usb_iso_packet_descriptor *iso) 530 { 531 unsigned char *usb_buf = urb->transfer_buffer + iso->offset; 532 int stream, i; 533 534 for (i = 0; i < iso->length;) { 535 for (stream = 0; stream < cdev->n_streams; stream++) { 536 struct snd_pcm_substream *sub = cdev->sub_playback[stream]; 537 char *audio_buf = NULL; 538 int c, n, sz = 0; 539 540 if (sub) { 541 struct snd_pcm_runtime *rt = sub->runtime; 542 audio_buf = rt->dma_area; 543 sz = frames_to_bytes(rt, rt->buffer_size); 544 } 545 546 for (c = 0; c < CHANNELS_PER_STREAM; c++) { 547 for (n = 0; n < BYTES_PER_SAMPLE; n++) { 548 if (audio_buf) { 549 usb_buf[i+n] = audio_buf[cdev->audio_out_buf_pos[stream]++]; 550 551 if (cdev->audio_out_buf_pos[stream] == sz) 552 cdev->audio_out_buf_pos[stream] = 0; 553 } else { 554 usb_buf[i+n] = 0; 555 } 556 } 557 558 if (audio_buf) 559 cdev->period_out_count[stream] += BYTES_PER_SAMPLE; 560 561 i += BYTES_PER_SAMPLE; 562 563 /* fill in the check byte pattern */ 564 usb_buf[i++] = (stream << 1) | c; 565 } 566 } 567 } 568 } 569 570 static inline void fill_out_urb(struct snd_usb_caiaqdev *cdev, 571 struct urb *urb, 572 const struct usb_iso_packet_descriptor *iso) 573 { 574 switch (cdev->spec.data_alignment) { 575 case 0: 576 case 2: 577 fill_out_urb_mode_0(cdev, urb, iso); 578 break; 579 case 3: 580 fill_out_urb_mode_3(cdev, urb, iso); 581 break; 582 } 583 } 584 585 static void read_completed(struct urb *urb) 586 { 587 struct snd_usb_caiaq_cb_info *info = urb->context; 588 struct snd_usb_caiaqdev *cdev; 589 struct device *dev; 590 struct urb *out = NULL; 591 int i, frame, len, send_it = 0, outframe = 0; 592 size_t offset = 0; 593 594 if (urb->status || !info) 595 return; 596 597 cdev = info->cdev; 598 dev = caiaqdev_to_dev(cdev); 599 600 if (!cdev->streaming) 601 return; 602 603 /* find an unused output urb that is unused */ 604 for (i = 0; i < N_URBS; i++) 605 if (test_and_set_bit(i, &cdev->outurb_active_mask) == 0) { 606 out = cdev->data_urbs_out[i]; 607 break; 608 } 609 610 if (!out) { 611 dev_err(dev, "Unable to find an output urb to use\n"); 612 goto requeue; 613 } 614 615 /* read the recently received packet and send back one which has 616 * the same layout */ 617 for (frame = 0; frame < FRAMES_PER_URB; frame++) { 618 if (urb->iso_frame_desc[frame].status) 619 continue; 620 621 len = urb->iso_frame_desc[outframe].actual_length; 622 out->iso_frame_desc[outframe].length = len; 623 out->iso_frame_desc[outframe].actual_length = 0; 624 out->iso_frame_desc[outframe].offset = offset; 625 offset += len; 626 627 if (len > 0) { 628 scoped_guard(spinlock_irqsave, &cdev->spinlock) { 629 fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]); 630 read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]); 631 } 632 check_for_elapsed_periods(cdev, cdev->sub_playback); 633 check_for_elapsed_periods(cdev, cdev->sub_capture); 634 send_it = 1; 635 } 636 637 outframe++; 638 } 639 640 if (send_it) { 641 out->number_of_packets = outframe; 642 usb_submit_urb(out, GFP_ATOMIC); 643 } else { 644 struct snd_usb_caiaq_cb_info *oinfo = out->context; 645 clear_bit(oinfo->index, &cdev->outurb_active_mask); 646 } 647 648 requeue: 649 /* re-submit inbound urb */ 650 for (frame = 0; frame < FRAMES_PER_URB; frame++) { 651 urb->iso_frame_desc[frame].offset = BYTES_PER_FRAME * frame; 652 urb->iso_frame_desc[frame].length = BYTES_PER_FRAME; 653 urb->iso_frame_desc[frame].actual_length = 0; 654 } 655 656 urb->number_of_packets = FRAMES_PER_URB; 657 usb_submit_urb(urb, GFP_ATOMIC); 658 } 659 660 static void write_completed(struct urb *urb) 661 { 662 struct snd_usb_caiaq_cb_info *info = urb->context; 663 struct snd_usb_caiaqdev *cdev = info->cdev; 664 665 if (!cdev->output_running) { 666 cdev->output_running = 1; 667 wake_up(&cdev->prepare_wait_queue); 668 } 669 670 clear_bit(info->index, &cdev->outurb_active_mask); 671 } 672 673 static struct urb **alloc_urbs(struct snd_usb_caiaqdev *cdev, int dir, int *ret) 674 { 675 int i, frame; 676 struct urb **urbs; 677 struct usb_device *usb_dev = cdev->chip.dev; 678 unsigned int pipe; 679 680 pipe = (dir == SNDRV_PCM_STREAM_PLAYBACK) ? 681 usb_sndisocpipe(usb_dev, ENDPOINT_PLAYBACK) : 682 usb_rcvisocpipe(usb_dev, ENDPOINT_CAPTURE); 683 684 urbs = kmalloc_array(N_URBS, sizeof(*urbs), GFP_KERNEL); 685 if (!urbs) { 686 *ret = -ENOMEM; 687 return NULL; 688 } 689 690 for (i = 0; i < N_URBS; i++) { 691 urbs[i] = usb_alloc_urb(FRAMES_PER_URB, GFP_KERNEL); 692 if (!urbs[i]) { 693 *ret = -ENOMEM; 694 return urbs; 695 } 696 697 urbs[i]->transfer_buffer = 698 kmalloc_array(BYTES_PER_FRAME, FRAMES_PER_URB, 699 GFP_KERNEL); 700 if (!urbs[i]->transfer_buffer) { 701 *ret = -ENOMEM; 702 return urbs; 703 } 704 705 for (frame = 0; frame < FRAMES_PER_URB; frame++) { 706 struct usb_iso_packet_descriptor *iso = 707 &urbs[i]->iso_frame_desc[frame]; 708 709 iso->offset = BYTES_PER_FRAME * frame; 710 iso->length = BYTES_PER_FRAME; 711 } 712 713 urbs[i]->dev = usb_dev; 714 urbs[i]->pipe = pipe; 715 urbs[i]->transfer_buffer_length = FRAMES_PER_URB 716 * BYTES_PER_FRAME; 717 urbs[i]->context = &cdev->data_cb_info[i]; 718 urbs[i]->interval = 1; 719 urbs[i]->number_of_packets = FRAMES_PER_URB; 720 urbs[i]->complete = (dir == SNDRV_PCM_STREAM_CAPTURE) ? 721 read_completed : write_completed; 722 } 723 724 *ret = 0; 725 return urbs; 726 } 727 728 static void free_urbs(struct urb **urbs) 729 { 730 int i; 731 732 if (!urbs) 733 return; 734 735 for (i = 0; i < N_URBS; i++) { 736 if (!urbs[i]) 737 continue; 738 739 usb_kill_urb(urbs[i]); 740 kfree(urbs[i]->transfer_buffer); 741 usb_free_urb(urbs[i]); 742 } 743 744 kfree(urbs); 745 } 746 747 int snd_usb_caiaq_audio_init(struct snd_usb_caiaqdev *cdev) 748 { 749 int i, ret; 750 struct device *dev = caiaqdev_to_dev(cdev); 751 752 cdev->n_audio_in = max(cdev->spec.num_analog_audio_in, 753 cdev->spec.num_digital_audio_in) / 754 CHANNELS_PER_STREAM; 755 cdev->n_audio_out = max(cdev->spec.num_analog_audio_out, 756 cdev->spec.num_digital_audio_out) / 757 CHANNELS_PER_STREAM; 758 cdev->n_streams = max(cdev->n_audio_in, cdev->n_audio_out); 759 760 dev_dbg(dev, "cdev->n_audio_in = %d\n", cdev->n_audio_in); 761 dev_dbg(dev, "cdev->n_audio_out = %d\n", cdev->n_audio_out); 762 dev_dbg(dev, "cdev->n_streams = %d\n", cdev->n_streams); 763 764 if (cdev->n_streams > MAX_STREAMS) { 765 dev_err(dev, "unable to initialize device, too many streams.\n"); 766 return -EINVAL; 767 } 768 769 if (cdev->n_streams < 1) { 770 dev_err(dev, "bogus number of streams: %d\n", cdev->n_streams); 771 return -EINVAL; 772 } 773 774 ret = snd_pcm_new(cdev->chip.card, cdev->product_name, 0, 775 cdev->n_audio_out, cdev->n_audio_in, &cdev->pcm); 776 777 if (ret < 0) { 778 dev_err(dev, "snd_pcm_new() returned %d\n", ret); 779 return ret; 780 } 781 782 cdev->pcm->private_data = cdev; 783 strscpy(cdev->pcm->name, cdev->product_name, sizeof(cdev->pcm->name)); 784 785 memset(cdev->sub_playback, 0, sizeof(cdev->sub_playback)); 786 memset(cdev->sub_capture, 0, sizeof(cdev->sub_capture)); 787 788 memcpy(&cdev->pcm_info, &snd_usb_caiaq_pcm_hardware, 789 sizeof(snd_usb_caiaq_pcm_hardware)); 790 791 /* setup samplerates */ 792 cdev->samplerates = cdev->pcm_info.rates; 793 switch (cdev->chip.usb_id) { 794 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1): 795 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3): 796 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_SESSIONIO): 797 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_GUITARRIGMOBILE): 798 cdev->samplerates |= SNDRV_PCM_RATE_192000; 799 fallthrough; 800 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO2DJ): 801 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ): 802 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ): 803 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_TRAKTORAUDIO2): 804 cdev->samplerates |= SNDRV_PCM_RATE_88200; 805 break; 806 } 807 808 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_PLAYBACK, 809 &snd_usb_caiaq_ops); 810 snd_pcm_set_ops(cdev->pcm, SNDRV_PCM_STREAM_CAPTURE, 811 &snd_usb_caiaq_ops); 812 snd_pcm_set_managed_buffer_all(cdev->pcm, SNDRV_DMA_TYPE_VMALLOC, 813 NULL, 0, 0); 814 815 cdev->data_cb_info = 816 kmalloc_array(N_URBS, sizeof(struct snd_usb_caiaq_cb_info), 817 GFP_KERNEL); 818 819 if (!cdev->data_cb_info) 820 return -ENOMEM; 821 822 cdev->outurb_active_mask = 0; 823 BUILD_BUG_ON(N_URBS > (sizeof(cdev->outurb_active_mask) * 8)); 824 825 for (i = 0; i < N_URBS; i++) { 826 cdev->data_cb_info[i].cdev = cdev; 827 cdev->data_cb_info[i].index = i; 828 } 829 830 cdev->data_urbs_in = alloc_urbs(cdev, SNDRV_PCM_STREAM_CAPTURE, &ret); 831 if (ret < 0) { 832 kfree(cdev->data_cb_info); 833 free_urbs(cdev->data_urbs_in); 834 return ret; 835 } 836 837 cdev->data_urbs_out = alloc_urbs(cdev, SNDRV_PCM_STREAM_PLAYBACK, &ret); 838 if (ret < 0) { 839 kfree(cdev->data_cb_info); 840 free_urbs(cdev->data_urbs_in); 841 free_urbs(cdev->data_urbs_out); 842 return ret; 843 } 844 845 return 0; 846 } 847 848 void snd_usb_caiaq_audio_disconnect(struct snd_usb_caiaqdev *cdev) 849 { 850 struct device *dev = caiaqdev_to_dev(cdev); 851 852 dev_dbg(dev, "%s(%p)\n", __func__, cdev); 853 stream_stop(cdev); 854 } 855 856 void snd_usb_caiaq_audio_free(struct snd_usb_caiaqdev *cdev) 857 { 858 struct device *dev = caiaqdev_to_dev(cdev); 859 860 dev_dbg(dev, "%s(%p)\n", __func__, cdev); 861 free_urbs(cdev->data_urbs_in); 862 free_urbs(cdev->data_urbs_out); 863 kfree(cdev->data_cb_info); 864 } 865