device.c 15 KB

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