1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * caiaq.c: ALSA driver for caiaq/NativeInstruments devices 4 * 5 * Copyright (c) 2007 Daniel Mack <daniel@caiaq.de> 6 * Karsten Wiese <fzu@wemgehoertderstaat.de> 7 */ 8 9 #include <linux/moduleparam.h> 10 #include <linux/device.h> 11 #include <linux/interrupt.h> 12 #include <linux/module.h> 13 #include <linux/init.h> 14 #include <linux/gfp.h> 15 #include <linux/usb.h> 16 #include <sound/initval.h> 17 #include <sound/core.h> 18 #include <sound/pcm.h> 19 20 #include "device.h" 21 #include "audio.h" 22 #include "midi.h" 23 #include "control.h" 24 #include "input.h" 25 #include "lcd.h" 26 27 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); 28 MODULE_DESCRIPTION("caiaq USB audio"); 29 MODULE_LICENSE("GPL"); 30 31 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */ 32 static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */ 33 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */ 34 35 module_param_array(index, int, NULL, 0444); 36 MODULE_PARM_DESC(index, "Index value for the caiaq sound device"); 37 module_param_array(id, charp, NULL, 0444); 38 MODULE_PARM_DESC(id, "ID string for the caiaq soundcard."); 39 module_param_array(enable, bool, NULL, 0444); 40 MODULE_PARM_DESC(enable, "Enable the caiaq soundcard."); 41 42 enum { 43 SAMPLERATE_44100 = 0, 44 SAMPLERATE_48000 = 1, 45 SAMPLERATE_96000 = 2, 46 SAMPLERATE_192000 = 3, 47 SAMPLERATE_88200 = 4, 48 SAMPLERATE_INVALID = 0xff 49 }; 50 51 enum { 52 DEPTH_NONE = 0, 53 DEPTH_16 = 1, 54 DEPTH_24 = 2, 55 DEPTH_32 = 3 56 }; 57 58 static const struct usb_device_id snd_usb_id_table[] = { 59 { 60 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 61 .idVendor = USB_VID_NATIVEINSTRUMENTS, 62 .idProduct = USB_PID_RIGKONTROL2 63 }, 64 { 65 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 66 .idVendor = USB_VID_NATIVEINSTRUMENTS, 67 .idProduct = USB_PID_RIGKONTROL3 68 }, 69 { 70 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 71 .idVendor = USB_VID_NATIVEINSTRUMENTS, 72 .idProduct = USB_PID_KORECONTROLLER 73 }, 74 { 75 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 76 .idVendor = USB_VID_NATIVEINSTRUMENTS, 77 .idProduct = USB_PID_KORECONTROLLER2 78 }, 79 { 80 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 81 .idVendor = USB_VID_NATIVEINSTRUMENTS, 82 .idProduct = USB_PID_AK1 83 }, 84 { 85 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 86 .idVendor = USB_VID_NATIVEINSTRUMENTS, 87 .idProduct = USB_PID_AUDIO8DJ 88 }, 89 { 90 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 91 .idVendor = USB_VID_NATIVEINSTRUMENTS, 92 .idProduct = USB_PID_SESSIONIO 93 }, 94 { 95 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 96 .idVendor = USB_VID_NATIVEINSTRUMENTS, 97 .idProduct = USB_PID_GUITARRIGMOBILE 98 }, 99 { 100 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 101 .idVendor = USB_VID_NATIVEINSTRUMENTS, 102 .idProduct = USB_PID_AUDIO4DJ 103 }, 104 { 105 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 106 .idVendor = USB_VID_NATIVEINSTRUMENTS, 107 .idProduct = USB_PID_AUDIO2DJ 108 }, 109 { 110 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 111 .idVendor = USB_VID_NATIVEINSTRUMENTS, 112 .idProduct = USB_PID_TRAKTORKONTROLX1 113 }, 114 { 115 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 116 .idVendor = USB_VID_NATIVEINSTRUMENTS, 117 .idProduct = USB_PID_TRAKTORKONTROLS4 118 }, 119 { 120 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 121 .idVendor = USB_VID_NATIVEINSTRUMENTS, 122 .idProduct = USB_PID_TRAKTORAUDIO2 123 }, 124 { 125 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 126 .idVendor = USB_VID_NATIVEINSTRUMENTS, 127 .idProduct = USB_PID_MASCHINECONTROLLER 128 }, 129 { /* terminator */ } 130 }; 131 132 static void usb_ep1_command_reply_dispatch (struct urb* urb) 133 { 134 int ret; 135 struct device *dev = &urb->dev->dev; 136 struct snd_usb_caiaqdev *cdev = urb->context; 137 unsigned char *buf = urb->transfer_buffer; 138 unsigned int payload_len; 139 unsigned int copy_len; 140 141 if (urb->status || !cdev) { 142 dev_warn(dev, "received EP1 urb->status = %i\n", urb->status); 143 return; 144 } 145 if (urb->actual_length < 1) 146 goto resubmit; 147 148 payload_len = urb->actual_length - 1; 149 150 switch(buf[0]) { 151 case EP1_CMD_GET_DEVICE_INFO: 152 if (payload_len < sizeof(struct caiaq_device_spec)) 153 break; 154 memcpy(&cdev->spec, buf+1, sizeof(struct caiaq_device_spec)); 155 cdev->spec.fw_version = le16_to_cpu(cdev->spec.fw_version); 156 dev_dbg(dev, "device spec (firmware %d): audio: %d in, %d out, " 157 "MIDI: %d in, %d out, data alignment %d\n", 158 cdev->spec.fw_version, 159 cdev->spec.num_analog_audio_in, 160 cdev->spec.num_analog_audio_out, 161 cdev->spec.num_midi_in, 162 cdev->spec.num_midi_out, 163 cdev->spec.data_alignment); 164 165 cdev->spec_received++; 166 wake_up(&cdev->ep1_wait_queue); 167 break; 168 case EP1_CMD_AUDIO_PARAMS: 169 if (payload_len < 1) 170 break; 171 cdev->audio_parm_answer = buf[1]; 172 wake_up(&cdev->ep1_wait_queue); 173 break; 174 case EP1_CMD_MIDI_READ: 175 if (urb->actual_length < 3 || urb->actual_length - 3 < buf[2]) 176 break; 177 snd_usb_caiaq_midi_handle_input(cdev, buf[1], buf + 3, buf[2]); 178 break; 179 case EP1_CMD_READ_IO: 180 if (cdev->chip.usb_id == 181 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) { 182 copy_len = min_t(unsigned int, payload_len, sizeof(cdev->control_state)); 183 memcpy(cdev->control_state, buf + 1, copy_len); 184 wake_up(&cdev->ep1_wait_queue); 185 break; 186 } 187 #ifdef CONFIG_SND_USB_CAIAQ_INPUT 188 fallthrough; 189 case EP1_CMD_READ_ERP: 190 case EP1_CMD_READ_ANALOG: 191 snd_usb_caiaq_input_dispatch(cdev, buf, urb->actual_length); 192 #endif 193 break; 194 } 195 196 resubmit: 197 cdev->ep1_in_urb->actual_length = 0; 198 ret = usb_submit_urb(cdev->ep1_in_urb, GFP_ATOMIC); 199 if (ret < 0) 200 dev_err(dev, "unable to submit urb. OOM!?\n"); 201 } 202 203 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *cdev, 204 unsigned char command, 205 const unsigned char *buffer, 206 int len) 207 { 208 int actual_len; 209 struct usb_device *usb_dev = cdev->chip.dev; 210 211 if (!usb_dev) 212 return -EIO; 213 214 if (len > EP1_BUFSIZE - 1) 215 len = EP1_BUFSIZE - 1; 216 217 guard(mutex)(&cdev->ep1_out_mutex); 218 219 if (buffer && len > 0) 220 memcpy(cdev->ep1_out_buf+1, buffer, len); 221 222 cdev->ep1_out_buf[0] = command; 223 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1), 224 cdev->ep1_out_buf, len+1, &actual_len, 200); 225 } 226 227 int snd_usb_caiaq_send_command_bank(struct snd_usb_caiaqdev *cdev, 228 unsigned char command, 229 unsigned char bank, 230 const unsigned char *buffer, 231 int len) 232 { 233 int actual_len; 234 struct usb_device *usb_dev = cdev->chip.dev; 235 236 if (!usb_dev) 237 return -EIO; 238 239 if (len > EP1_BUFSIZE - 2) 240 len = EP1_BUFSIZE - 2; 241 242 guard(mutex)(&cdev->ep1_out_mutex); 243 244 if (buffer && len > 0) 245 memcpy(cdev->ep1_out_buf+2, buffer, len); 246 247 cdev->ep1_out_buf[0] = command; 248 cdev->ep1_out_buf[1] = bank; 249 250 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1), 251 cdev->ep1_out_buf, len+2, &actual_len, 200); 252 } 253 254 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *cdev, 255 int rate, int depth, int bpp) 256 { 257 int ret; 258 char tmp[5]; 259 struct device *dev = caiaqdev_to_dev(cdev); 260 261 switch (rate) { 262 case 44100: tmp[0] = SAMPLERATE_44100; break; 263 case 48000: tmp[0] = SAMPLERATE_48000; break; 264 case 88200: tmp[0] = SAMPLERATE_88200; break; 265 case 96000: tmp[0] = SAMPLERATE_96000; break; 266 case 192000: tmp[0] = SAMPLERATE_192000; break; 267 default: return -EINVAL; 268 } 269 270 switch (depth) { 271 case 16: tmp[1] = DEPTH_16; break; 272 case 24: tmp[1] = DEPTH_24; break; 273 default: return -EINVAL; 274 } 275 276 tmp[2] = bpp & 0xff; 277 tmp[3] = bpp >> 8; 278 tmp[4] = 1; /* packets per microframe */ 279 280 dev_dbg(dev, "setting audio params: %d Hz, %d bits, %d bpp\n", 281 rate, depth, bpp); 282 283 cdev->audio_parm_answer = -1; 284 ret = snd_usb_caiaq_send_command(cdev, EP1_CMD_AUDIO_PARAMS, 285 tmp, sizeof(tmp)); 286 287 if (ret) 288 return ret; 289 290 if (!wait_event_timeout(cdev->ep1_wait_queue, 291 cdev->audio_parm_answer >= 0, HZ)) 292 return -EPIPE; 293 294 if (cdev->audio_parm_answer != 1) 295 dev_dbg(dev, "unable to set the device's audio params\n"); 296 else 297 cdev->bpp = bpp; 298 299 return cdev->audio_parm_answer == 1 ? 0 : -EINVAL; 300 } 301 302 int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *cdev, 303 int digital, int analog, int erp) 304 { 305 char tmp[3] = { digital, analog, erp }; 306 return snd_usb_caiaq_send_command(cdev, EP1_CMD_AUTO_MSG, 307 tmp, sizeof(tmp)); 308 } 309 310 static int setup_card(struct snd_usb_caiaqdev *cdev) 311 { 312 int ret; 313 char val[4]; 314 struct device *dev = caiaqdev_to_dev(cdev); 315 316 /* device-specific startup specials */ 317 switch (cdev->chip.usb_id) { 318 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2): 319 /* RigKontrol2 - display centered dash ('-') */ 320 val[0] = 0x00; 321 val[1] = 0x00; 322 val[2] = 0x01; 323 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 3); 324 break; 325 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3): 326 /* RigKontrol2 - display two centered dashes ('--') */ 327 val[0] = 0x00; 328 val[1] = 0x40; 329 val[2] = 0x40; 330 val[3] = 0x00; 331 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 4); 332 break; 333 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1): 334 /* Audio Kontrol 1 - make USB-LED stop blinking */ 335 val[0] = 0x00; 336 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 1); 337 break; 338 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ): 339 /* Audio 8 DJ - trigger read of current settings */ 340 cdev->control_state[0] = 0xff; 341 snd_usb_caiaq_set_auto_msg(cdev, 1, 0, 0); 342 snd_usb_caiaq_send_command(cdev, EP1_CMD_READ_IO, NULL, 0); 343 344 if (!wait_event_timeout(cdev->ep1_wait_queue, 345 cdev->control_state[0] != 0xff, HZ)) { 346 dev_err(dev, "Read timeout for control state\n"); 347 return -EINVAL; 348 } 349 350 /* fix up some defaults */ 351 if ((cdev->control_state[1] != 2) || 352 (cdev->control_state[2] != 3) || 353 (cdev->control_state[4] != 2)) { 354 cdev->control_state[1] = 2; 355 cdev->control_state[2] = 3; 356 cdev->control_state[4] = 2; 357 snd_usb_caiaq_send_command(cdev, 358 EP1_CMD_WRITE_IO, cdev->control_state, 6); 359 } 360 361 break; 362 } 363 364 if (cdev->spec.num_analog_audio_out + 365 cdev->spec.num_analog_audio_in + 366 cdev->spec.num_digital_audio_out + 367 cdev->spec.num_digital_audio_in > 0) { 368 ret = snd_usb_caiaq_audio_init(cdev); 369 if (ret < 0) { 370 dev_err(dev, "Unable to set up audio system (ret=%d)\n", ret); 371 return ret; 372 } 373 } 374 375 if (cdev->spec.num_midi_in + 376 cdev->spec.num_midi_out > 0) { 377 ret = snd_usb_caiaq_midi_init(cdev); 378 if (ret < 0) { 379 dev_err(dev, "Unable to set up MIDI system (ret=%d)\n", ret); 380 return ret; 381 } 382 } 383 384 #ifdef CONFIG_SND_USB_CAIAQ_INPUT 385 ret = snd_usb_caiaq_input_init(cdev); 386 if (ret < 0 && ret != -ENODEV) { 387 dev_err(dev, "Unable to set up input system (ret=%d)\n", ret); 388 return ret; 389 } 390 #endif 391 392 ret = snd_usb_caiaq_lcd_init(cdev); 393 if (ret < 0) { 394 dev_err(dev, "Unable to set up LCD (ret=%d)\n", ret); 395 return ret; 396 } 397 398 ret = snd_usb_caiaq_control_init(cdev); 399 if (ret < 0) { 400 dev_err(dev, "Unable to set up control system (ret=%d)\n", ret); 401 return ret; 402 } 403 404 /* finally, register the card and all its sub-instances */ 405 ret = snd_card_register(cdev->chip.card); 406 if (ret < 0) { 407 dev_err(dev, "snd_card_register() returned %d\n", ret); 408 return ret; 409 } 410 411 return 0; 412 } 413 414 static void card_free(struct snd_card *card) 415 { 416 struct snd_usb_caiaqdev *cdev = caiaqdev(card); 417 418 #ifdef CONFIG_SND_USB_CAIAQ_INPUT 419 snd_usb_caiaq_input_free(cdev); 420 #endif 421 snd_usb_caiaq_audio_free(cdev); 422 usb_put_dev(cdev->chip.dev); 423 usb_free_urb(cdev->ep1_in_urb); 424 cdev->ep1_in_urb = NULL; 425 usb_free_urb(cdev->midi_out_urb); 426 cdev->midi_out_urb = NULL; 427 } 428 429 static int create_card(struct usb_device *usb_dev, 430 struct usb_interface *intf, 431 struct snd_card **cardp) 432 { 433 int devnum; 434 int err; 435 struct snd_card *card; 436 struct snd_usb_caiaqdev *cdev; 437 438 for (devnum = 0; devnum < SNDRV_CARDS; devnum++) 439 if (enable[devnum]) 440 break; 441 442 if (devnum >= SNDRV_CARDS) 443 return -ENODEV; 444 445 err = snd_card_new(&intf->dev, 446 index[devnum], id[devnum], THIS_MODULE, 447 sizeof(struct snd_usb_caiaqdev), &card); 448 if (err < 0) 449 return err; 450 451 cdev = caiaqdev(card); 452 cdev->chip.dev = usb_get_dev(usb_dev); 453 card->private_free = card_free; 454 cdev->chip.card = card; 455 cdev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor), 456 le16_to_cpu(usb_dev->descriptor.idProduct)); 457 spin_lock_init(&cdev->spinlock); 458 spin_lock_init(&cdev->midi_lock); 459 mutex_init(&cdev->ep1_out_mutex); 460 461 *cardp = card; 462 return 0; 463 } 464 465 static int init_card(struct snd_usb_caiaqdev *cdev) 466 { 467 char *c, usbpath[32]; 468 struct usb_device *usb_dev = cdev->chip.dev; 469 struct snd_card *card = cdev->chip.card; 470 struct device *dev = caiaqdev_to_dev(cdev); 471 int err, len; 472 473 if (usb_set_interface(usb_dev, 0, 1) != 0) { 474 dev_err(dev, "can't set alt interface.\n"); 475 return -EIO; 476 } 477 478 cdev->ep1_in_urb = usb_alloc_urb(0, GFP_KERNEL); 479 if (!cdev->ep1_in_urb) 480 return -ENOMEM; 481 482 cdev->midi_out_urb = usb_alloc_urb(0, GFP_KERNEL); 483 if (!cdev->midi_out_urb) { 484 usb_free_urb(cdev->ep1_in_urb); 485 cdev->ep1_in_urb = NULL; 486 return -ENOMEM; 487 } 488 489 usb_fill_bulk_urb(cdev->ep1_in_urb, usb_dev, 490 usb_rcvbulkpipe(usb_dev, 0x1), 491 cdev->ep1_in_buf, EP1_BUFSIZE, 492 usb_ep1_command_reply_dispatch, cdev); 493 494 usb_fill_bulk_urb(cdev->midi_out_urb, usb_dev, 495 usb_sndbulkpipe(usb_dev, 0x1), 496 cdev->midi_out_buf, EP1_BUFSIZE, 497 snd_usb_caiaq_midi_output_done, cdev); 498 499 /* sanity checks of EPs before actually submitting */ 500 if (usb_urb_ep_type_check(cdev->ep1_in_urb) || 501 usb_urb_ep_type_check(cdev->midi_out_urb)) { 502 dev_err(dev, "invalid EPs\n"); 503 return -EINVAL; 504 } 505 506 init_waitqueue_head(&cdev->ep1_wait_queue); 507 init_waitqueue_head(&cdev->prepare_wait_queue); 508 509 if (usb_submit_urb(cdev->ep1_in_urb, GFP_KERNEL) != 0) 510 return -EIO; 511 512 err = snd_usb_caiaq_send_command(cdev, EP1_CMD_GET_DEVICE_INFO, NULL, 0); 513 if (err) 514 goto err_kill_urb; 515 516 if (!wait_event_timeout(cdev->ep1_wait_queue, cdev->spec_received, HZ)) { 517 err = -ENODEV; 518 goto err_kill_urb; 519 } 520 521 usb_string(usb_dev, usb_dev->descriptor.iManufacturer, 522 cdev->vendor_name, CAIAQ_USB_STR_LEN); 523 524 usb_string(usb_dev, usb_dev->descriptor.iProduct, 525 cdev->product_name, CAIAQ_USB_STR_LEN); 526 527 strscpy(card->driver, MODNAME, sizeof(card->driver)); 528 strscpy(card->shortname, cdev->product_name, sizeof(card->shortname)); 529 strscpy(card->mixername, cdev->product_name, sizeof(card->mixername)); 530 531 /* if the id was not passed as module option, fill it with a shortened 532 * version of the product string which does not contain any 533 * whitespaces */ 534 535 if (*card->id == '\0') { 536 char id[sizeof(card->id)]; 537 538 memset(id, 0, sizeof(id)); 539 540 for (c = card->shortname, len = 0; 541 *c && len < sizeof(card->id) - 1; c++) 542 if (*c != ' ') 543 id[len++] = *c; 544 545 snd_card_set_id(card, id); 546 } 547 548 usb_make_path(usb_dev, usbpath, sizeof(usbpath)); 549 scnprintf(card->longname, sizeof(card->longname), "%s %s (%s)", 550 cdev->vendor_name, cdev->product_name, usbpath); 551 552 err = setup_card(cdev); 553 if (err < 0) 554 goto err_kill_urb; 555 556 return 0; 557 558 err_kill_urb: 559 usb_kill_urb(cdev->ep1_in_urb); 560 return err; 561 } 562 563 static int snd_probe(struct usb_interface *intf, 564 const struct usb_device_id *id) 565 { 566 int ret; 567 struct snd_card *card = NULL; 568 struct usb_device *usb_dev = interface_to_usbdev(intf); 569 570 ret = create_card(usb_dev, intf, &card); 571 572 if (ret < 0) 573 return ret; 574 575 usb_set_intfdata(intf, card); 576 ret = init_card(caiaqdev(card)); 577 if (ret < 0) { 578 dev_err(&usb_dev->dev, "unable to init card! (ret=%d)\n", ret); 579 #ifdef CONFIG_SND_USB_CAIAQ_INPUT 580 snd_usb_caiaq_input_disconnect(caiaqdev(card)); 581 #endif 582 snd_card_free(card); 583 return ret; 584 } 585 586 return 0; 587 } 588 589 static void snd_disconnect(struct usb_interface *intf) 590 { 591 struct snd_card *card = usb_get_intfdata(intf); 592 struct device *dev = intf->usb_dev; 593 struct snd_usb_caiaqdev *cdev; 594 595 if (!card) 596 return; 597 598 cdev = caiaqdev(card); 599 dev_dbg(dev, "%s(%p)\n", __func__, intf); 600 601 snd_card_disconnect(card); 602 603 #ifdef CONFIG_SND_USB_CAIAQ_INPUT 604 snd_usb_caiaq_input_disconnect(cdev); 605 #endif 606 snd_usb_caiaq_audio_disconnect(cdev); 607 608 usb_kill_urb(cdev->ep1_in_urb); 609 usb_kill_urb(cdev->midi_out_urb); 610 611 snd_card_free_when_closed(card); 612 } 613 614 615 MODULE_DEVICE_TABLE(usb, snd_usb_id_table); 616 static struct usb_driver snd_usb_driver = { 617 .name = MODNAME, 618 .probe = snd_probe, 619 .disconnect = snd_disconnect, 620 .id_table = snd_usb_id_table, 621 }; 622 623 module_usb_driver(snd_usb_driver); 624