device.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. #include <linux/moduleparam.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/gfp.h>
  26. #include <linux/usb.h>
  27. #include <sound/initval.h>
  28. #include <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include "device.h"
  31. #include "audio.h"
  32. #include "midi.h"
  33. #include "control.h"
  34. #include "input.h"
  35. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  36. MODULE_DESCRIPTION("caiaq USB audio");
  37. MODULE_LICENSE("GPL");
  38. MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
  39. "{Native Instruments, RigKontrol3},"
  40. "{Native Instruments, Kore Controller},"
  41. "{Native Instruments, Kore Controller 2},"
  42. "{Native Instruments, Audio Kontrol 1},"
  43. "{Native Instruments, Audio 2 DJ},"
  44. "{Native Instruments, Audio 4 DJ},"
  45. "{Native Instruments, Audio 8 DJ},"
  46. "{Native Instruments, Traktor Audio 2},"
  47. "{Native Instruments, Session I/O},"
  48. "{Native Instruments, GuitarRig mobile}"
  49. "{Native Instruments, Traktor Kontrol X1}"
  50. "{Native Instruments, Traktor Kontrol S4}");
  51. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
  52. static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
  53. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  54. static int snd_card_used[SNDRV_CARDS];
  55. module_param_array(index, int, NULL, 0444);
  56. MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
  57. module_param_array(id, charp, NULL, 0444);
  58. MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
  59. module_param_array(enable, bool, NULL, 0444);
  60. MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
  61. enum {
  62. SAMPLERATE_44100 = 0,
  63. SAMPLERATE_48000 = 1,
  64. SAMPLERATE_96000 = 2,
  65. SAMPLERATE_192000 = 3,
  66. SAMPLERATE_88200 = 4,
  67. SAMPLERATE_INVALID = 0xff
  68. };
  69. enum {
  70. DEPTH_NONE = 0,
  71. DEPTH_16 = 1,
  72. DEPTH_24 = 2,
  73. DEPTH_32 = 3
  74. };
  75. static struct usb_device_id snd_usb_id_table[] = {
  76. {
  77. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  78. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  79. .idProduct = USB_PID_RIGKONTROL2
  80. },
  81. {
  82. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  83. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  84. .idProduct = USB_PID_RIGKONTROL3
  85. },
  86. {
  87. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  88. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  89. .idProduct = USB_PID_KORECONTROLLER
  90. },
  91. {
  92. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  93. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  94. .idProduct = USB_PID_KORECONTROLLER2
  95. },
  96. {
  97. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  98. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  99. .idProduct = USB_PID_AK1
  100. },
  101. {
  102. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  103. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  104. .idProduct = USB_PID_AUDIO8DJ
  105. },
  106. {
  107. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  108. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  109. .idProduct = USB_PID_SESSIONIO
  110. },
  111. {
  112. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  113. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  114. .idProduct = USB_PID_GUITARRIGMOBILE
  115. },
  116. {
  117. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  118. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  119. .idProduct = USB_PID_AUDIO4DJ
  120. },
  121. {
  122. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  123. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  124. .idProduct = USB_PID_AUDIO2DJ
  125. },
  126. {
  127. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  128. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  129. .idProduct = USB_PID_TRAKTORKONTROLX1
  130. },
  131. {
  132. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  133. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  134. .idProduct = USB_PID_TRAKTORKONTROLS4
  135. },
  136. {
  137. .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
  138. .idVendor = USB_VID_NATIVEINSTRUMENTS,
  139. .idProduct = USB_PID_TRAKTORAUDIO2
  140. },
  141. { /* terminator */ }
  142. };
  143. static void usb_ep1_command_reply_dispatch (struct urb* urb)
  144. {
  145. int ret;
  146. struct snd_usb_caiaqdev *dev = urb->context;
  147. unsigned char *buf = urb->transfer_buffer;
  148. if (urb->status || !dev) {
  149. log("received EP1 urb->status = %i\n", urb->status);
  150. return;
  151. }
  152. switch(buf[0]) {
  153. case EP1_CMD_GET_DEVICE_INFO:
  154. memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
  155. dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
  156. debug("device spec (firmware %d): audio: %d in, %d out, "
  157. "MIDI: %d in, %d out, data alignment %d\n",
  158. dev->spec.fw_version,
  159. dev->spec.num_analog_audio_in,
  160. dev->spec.num_analog_audio_out,
  161. dev->spec.num_midi_in,
  162. dev->spec.num_midi_out,
  163. dev->spec.data_alignment);
  164. dev->spec_received++;
  165. wake_up(&dev->ep1_wait_queue);
  166. break;
  167. case EP1_CMD_AUDIO_PARAMS:
  168. dev->audio_parm_answer = buf[1];
  169. wake_up(&dev->ep1_wait_queue);
  170. break;
  171. case EP1_CMD_MIDI_READ:
  172. snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
  173. break;
  174. case EP1_CMD_READ_IO:
  175. if (dev->chip.usb_id ==
  176. USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
  177. if (urb->actual_length > sizeof(dev->control_state))
  178. urb->actual_length = sizeof(dev->control_state);
  179. memcpy(dev->control_state, buf + 1, urb->actual_length);
  180. wake_up(&dev->ep1_wait_queue);
  181. break;
  182. }
  183. #ifdef CONFIG_SND_USB_CAIAQ_INPUT
  184. case EP1_CMD_READ_ERP:
  185. case EP1_CMD_READ_ANALOG:
  186. snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
  187. #endif
  188. break;
  189. }
  190. dev->ep1_in_urb.actual_length = 0;
  191. ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
  192. if (ret < 0)
  193. log("unable to submit urb. OOM!?\n");
  194. }
  195. int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
  196. unsigned char command,
  197. const unsigned char *buffer,
  198. int len)
  199. {
  200. int actual_len;
  201. struct usb_device *usb_dev = dev->chip.dev;
  202. if (!usb_dev)
  203. return -EIO;
  204. if (len > EP1_BUFSIZE - 1)
  205. len = EP1_BUFSIZE - 1;
  206. if (buffer && len > 0)
  207. memcpy(dev->ep1_out_buf+1, buffer, len);
  208. dev->ep1_out_buf[0] = command;
  209. return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
  210. dev->ep1_out_buf, len+1, &actual_len, 200);
  211. }
  212. int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
  213. int rate, int depth, int bpp)
  214. {
  215. int ret;
  216. char tmp[5];
  217. switch (rate) {
  218. case 44100: tmp[0] = SAMPLERATE_44100; break;
  219. case 48000: tmp[0] = SAMPLERATE_48000; break;
  220. case 88200: tmp[0] = SAMPLERATE_88200; break;
  221. case 96000: tmp[0] = SAMPLERATE_96000; break;
  222. case 192000: tmp[0] = SAMPLERATE_192000; break;
  223. default: return -EINVAL;
  224. }
  225. switch (depth) {
  226. case 16: tmp[1] = DEPTH_16; break;
  227. case 24: tmp[1] = DEPTH_24; break;
  228. default: return -EINVAL;
  229. }
  230. tmp[2] = bpp & 0xff;
  231. tmp[3] = bpp >> 8;
  232. tmp[4] = 1; /* packets per microframe */
  233. debug("setting audio params: %d Hz, %d bits, %d bpp\n",
  234. rate, depth, bpp);
  235. dev->audio_parm_answer = -1;
  236. ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
  237. tmp, sizeof(tmp));
  238. if (ret)
  239. return ret;
  240. if (!wait_event_timeout(dev->ep1_wait_queue,
  241. dev->audio_parm_answer >= 0, HZ))
  242. return -EPIPE;
  243. if (dev->audio_parm_answer != 1)
  244. debug("unable to set the device's audio params\n");
  245. else
  246. dev->bpp = bpp;
  247. return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
  248. }
  249. int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *dev,
  250. int digital, int analog, int erp)
  251. {
  252. char tmp[3] = { digital, analog, erp };
  253. return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
  254. tmp, sizeof(tmp));
  255. }
  256. static void __devinit setup_card(struct snd_usb_caiaqdev *dev)
  257. {
  258. int ret;
  259. char val[4];
  260. /* device-specific startup specials */
  261. switch (dev->chip.usb_id) {
  262. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
  263. /* RigKontrol2 - display centered dash ('-') */
  264. val[0] = 0x00;
  265. val[1] = 0x00;
  266. val[2] = 0x01;
  267. snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
  268. break;
  269. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
  270. /* RigKontrol2 - display two centered dashes ('--') */
  271. val[0] = 0x00;
  272. val[1] = 0x40;
  273. val[2] = 0x40;
  274. val[3] = 0x00;
  275. snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
  276. break;
  277. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
  278. /* Audio Kontrol 1 - make USB-LED stop blinking */
  279. val[0] = 0x00;
  280. snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1);
  281. break;
  282. case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
  283. /* Audio 8 DJ - trigger read of current settings */
  284. dev->control_state[0] = 0xff;
  285. snd_usb_caiaq_set_auto_msg(dev, 1, 0, 0);
  286. snd_usb_caiaq_send_command(dev, EP1_CMD_READ_IO, NULL, 0);
  287. if (!wait_event_timeout(dev->ep1_wait_queue,
  288. dev->control_state[0] != 0xff, HZ))
  289. return;
  290. /* fix up some defaults */
  291. if ((dev->control_state[1] != 2) ||
  292. (dev->control_state[2] != 3) ||
  293. (dev->control_state[4] != 2)) {
  294. dev->control_state[1] = 2;
  295. dev->control_state[2] = 3;
  296. dev->control_state[4] = 2;
  297. snd_usb_caiaq_send_command(dev,
  298. EP1_CMD_WRITE_IO, dev->control_state, 6);
  299. }
  300. break;
  301. }
  302. if (dev->spec.num_analog_audio_out +
  303. dev->spec.num_analog_audio_in +
  304. dev->spec.num_digital_audio_out +
  305. dev->spec.num_digital_audio_in > 0) {
  306. ret = snd_usb_caiaq_audio_init(dev);
  307. if (ret < 0)
  308. log("Unable to set up audio system (ret=%d)\n", ret);
  309. }
  310. if (dev->spec.num_midi_in +
  311. dev->spec.num_midi_out > 0) {
  312. ret = snd_usb_caiaq_midi_init(dev);
  313. if (ret < 0)
  314. log("Unable to set up MIDI system (ret=%d)\n", ret);
  315. }
  316. #ifdef CONFIG_SND_USB_CAIAQ_INPUT
  317. ret = snd_usb_caiaq_input_init(dev);
  318. if (ret < 0)
  319. log("Unable to set up input system (ret=%d)\n", ret);
  320. #endif
  321. /* finally, register the card and all its sub-instances */
  322. ret = snd_card_register(dev->chip.card);
  323. if (ret < 0) {
  324. log("snd_card_register() returned %d\n", ret);
  325. snd_card_free(dev->chip.card);
  326. }
  327. ret = snd_usb_caiaq_control_init(dev);
  328. if (ret < 0)
  329. log("Unable to set up control system (ret=%d)\n", ret);
  330. }
  331. static int create_card(struct usb_device *usb_dev,
  332. struct usb_interface *intf,
  333. struct snd_card **cardp)
  334. {
  335. int devnum;
  336. int err;
  337. struct snd_card *card;
  338. struct snd_usb_caiaqdev *dev;
  339. for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
  340. if (enable[devnum] && !snd_card_used[devnum])
  341. break;
  342. if (devnum >= SNDRV_CARDS)
  343. return -ENODEV;
  344. err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
  345. sizeof(struct snd_usb_caiaqdev), &card);
  346. if (err < 0)
  347. return err;
  348. dev = caiaqdev(card);
  349. dev->chip.dev = usb_dev;
  350. dev->chip.card = card;
  351. dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
  352. le16_to_cpu(usb_dev->descriptor.idProduct));
  353. spin_lock_init(&dev->spinlock);
  354. snd_card_set_dev(card, &intf->dev);
  355. *cardp = card;
  356. return 0;
  357. }
  358. static int __devinit init_card(struct snd_usb_caiaqdev *dev)
  359. {
  360. char *c, usbpath[32];
  361. struct usb_device *usb_dev = dev->chip.dev;
  362. struct snd_card *card = dev->chip.card;
  363. int err, len;
  364. if (usb_set_interface(usb_dev, 0, 1) != 0) {
  365. log("can't set alt interface.\n");
  366. return -EIO;
  367. }
  368. usb_init_urb(&dev->ep1_in_urb);
  369. usb_init_urb(&dev->midi_out_urb);
  370. usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
  371. usb_rcvbulkpipe(usb_dev, 0x1),
  372. dev->ep1_in_buf, EP1_BUFSIZE,
  373. usb_ep1_command_reply_dispatch, dev);
  374. usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
  375. usb_sndbulkpipe(usb_dev, 0x1),
  376. dev->midi_out_buf, EP1_BUFSIZE,
  377. snd_usb_caiaq_midi_output_done, dev);
  378. init_waitqueue_head(&dev->ep1_wait_queue);
  379. init_waitqueue_head(&dev->prepare_wait_queue);
  380. if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
  381. return -EIO;
  382. err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
  383. if (err)
  384. return err;
  385. if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
  386. return -ENODEV;
  387. usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
  388. dev->vendor_name, CAIAQ_USB_STR_LEN);
  389. usb_string(usb_dev, usb_dev->descriptor.iProduct,
  390. dev->product_name, CAIAQ_USB_STR_LEN);
  391. strlcpy(card->driver, MODNAME, sizeof(card->driver));
  392. strlcpy(card->shortname, dev->product_name, sizeof(card->shortname));
  393. strlcpy(card->mixername, dev->product_name, sizeof(card->mixername));
  394. /* if the id was not passed as module option, fill it with a shortened
  395. * version of the product string which does not contain any
  396. * whitespaces */
  397. if (*card->id == '\0') {
  398. char id[sizeof(card->id)];
  399. memset(id, 0, sizeof(id));
  400. for (c = card->shortname, len = 0;
  401. *c && len < sizeof(card->id); c++)
  402. if (*c != ' ')
  403. id[len++] = *c;
  404. snd_card_set_id(card, id);
  405. }
  406. usb_make_path(usb_dev, usbpath, sizeof(usbpath));
  407. snprintf(card->longname, sizeof(card->longname),
  408. "%s %s (%s)",
  409. dev->vendor_name, dev->product_name, usbpath);
  410. setup_card(dev);
  411. return 0;
  412. }
  413. static int __devinit snd_probe(struct usb_interface *intf,
  414. const struct usb_device_id *id)
  415. {
  416. int ret;
  417. struct snd_card *card;
  418. struct usb_device *device = interface_to_usbdev(intf);
  419. ret = create_card(device, intf, &card);
  420. if (ret < 0)
  421. return ret;
  422. usb_set_intfdata(intf, card);
  423. ret = init_card(caiaqdev(card));
  424. if (ret < 0) {
  425. log("unable to init card! (ret=%d)\n", ret);
  426. snd_card_free(card);
  427. return ret;
  428. }
  429. return 0;
  430. }
  431. static void snd_disconnect(struct usb_interface *intf)
  432. {
  433. struct snd_usb_caiaqdev *dev;
  434. struct snd_card *card = usb_get_intfdata(intf);
  435. debug("%s(%p)\n", __func__, intf);
  436. if (!card)
  437. return;
  438. dev = caiaqdev(card);
  439. snd_card_disconnect(card);
  440. #ifdef CONFIG_SND_USB_CAIAQ_INPUT
  441. snd_usb_caiaq_input_free(dev);
  442. #endif
  443. snd_usb_caiaq_audio_free(dev);
  444. usb_kill_urb(&dev->ep1_in_urb);
  445. usb_kill_urb(&dev->midi_out_urb);
  446. snd_card_free(card);
  447. usb_reset_device(interface_to_usbdev(intf));
  448. }
  449. MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
  450. static struct usb_driver snd_usb_driver = {
  451. .name = MODNAME,
  452. .probe = snd_probe,
  453. .disconnect = snd_disconnect,
  454. .id_table = snd_usb_id_table,
  455. };
  456. static int __init snd_module_init(void)
  457. {
  458. return usb_register(&snd_usb_driver);
  459. }
  460. static void __exit snd_module_exit(void)
  461. {
  462. usb_deregister(&snd_usb_driver);
  463. }
  464. module_init(snd_module_init)
  465. module_exit(snd_module_exit)