device.c 14 KB

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