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