device.c 14 KB

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