device.c 14 KB

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