device.c 14 KB

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