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