device.c 14 KB

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