bcd2000.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Behringer BCD2000 driver
  3. *
  4. * Copyright (C) 2014 Mario Kicherer (dev@kicherer.org)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/audio.h>
  24. #include <sound/core.h>
  25. #include <sound/initval.h>
  26. #include <sound/rawmidi.h>
  27. #define PREFIX "snd-bcd2000: "
  28. #define BUFSIZE 64
  29. static struct usb_device_id id_table[] = {
  30. { USB_DEVICE(0x1397, 0x00bd) },
  31. { },
  32. };
  33. static unsigned char device_cmd_prefix[] = {0x03, 0x00};
  34. static unsigned char bcd2000_init_sequence[] = {
  35. 0x07, 0x00, 0x00, 0x00, 0x78, 0x48, 0x1c, 0x81,
  36. 0xc4, 0x00, 0x00, 0x00, 0x5e, 0x53, 0x4a, 0xf7,
  37. 0x18, 0xfa, 0x11, 0xff, 0x6c, 0xf3, 0x90, 0xff,
  38. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  39. 0x18, 0xfa, 0x11, 0xff, 0x14, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0xf2, 0x34, 0x4a, 0xf7,
  41. 0x18, 0xfa, 0x11, 0xff
  42. };
  43. struct bcd2000 {
  44. struct usb_device *dev;
  45. struct snd_card *card;
  46. struct usb_interface *intf;
  47. int card_index;
  48. int midi_out_active;
  49. struct snd_rawmidi *rmidi;
  50. struct snd_rawmidi_substream *midi_receive_substream;
  51. struct snd_rawmidi_substream *midi_out_substream;
  52. unsigned char midi_in_buf[BUFSIZE];
  53. unsigned char midi_out_buf[BUFSIZE];
  54. struct urb *midi_out_urb;
  55. struct urb *midi_in_urb;
  56. struct usb_anchor anchor;
  57. };
  58. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  59. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  60. static DEFINE_MUTEX(devices_mutex);
  61. static DECLARE_BITMAP(devices_used, SNDRV_CARDS);
  62. static struct usb_driver bcd2000_driver;
  63. #ifdef CONFIG_SND_DEBUG
  64. static void bcd2000_dump_buffer(const char *prefix, const char *buf, int len)
  65. {
  66. print_hex_dump(KERN_DEBUG, prefix,
  67. DUMP_PREFIX_NONE, 16, 1,
  68. buf, len, false);
  69. }
  70. #else
  71. static void bcd2000_dump_buffer(const char *prefix, const char *buf, int len) {}
  72. #endif
  73. static int bcd2000_midi_input_open(struct snd_rawmidi_substream *substream)
  74. {
  75. return 0;
  76. }
  77. static int bcd2000_midi_input_close(struct snd_rawmidi_substream *substream)
  78. {
  79. return 0;
  80. }
  81. /* (de)register midi substream from client */
  82. static void bcd2000_midi_input_trigger(struct snd_rawmidi_substream *substream,
  83. int up)
  84. {
  85. struct bcd2000 *bcd2k = substream->rmidi->private_data;
  86. bcd2k->midi_receive_substream = up ? substream : NULL;
  87. }
  88. static void bcd2000_midi_handle_input(struct bcd2000 *bcd2k,
  89. const unsigned char *buf, unsigned int buf_len)
  90. {
  91. unsigned int payload_length, tocopy;
  92. struct snd_rawmidi_substream *midi_receive_substream;
  93. midi_receive_substream = ACCESS_ONCE(bcd2k->midi_receive_substream);
  94. if (!midi_receive_substream)
  95. return;
  96. bcd2000_dump_buffer(PREFIX "received from device: ", buf, buf_len);
  97. if (buf_len < 2)
  98. return;
  99. payload_length = buf[0];
  100. /* ignore packets without payload */
  101. if (payload_length == 0)
  102. return;
  103. tocopy = min(payload_length, buf_len-1);
  104. bcd2000_dump_buffer(PREFIX "sending to userspace: ",
  105. &buf[1], tocopy);
  106. snd_rawmidi_receive(midi_receive_substream,
  107. &buf[1], tocopy);
  108. }
  109. static void bcd2000_midi_send(struct bcd2000 *bcd2k)
  110. {
  111. int len, ret;
  112. struct snd_rawmidi_substream *midi_out_substream;
  113. BUILD_BUG_ON(sizeof(device_cmd_prefix) >= BUFSIZE);
  114. midi_out_substream = ACCESS_ONCE(bcd2k->midi_out_substream);
  115. if (!midi_out_substream)
  116. return;
  117. /* copy command prefix bytes */
  118. memcpy(bcd2k->midi_out_buf, device_cmd_prefix,
  119. sizeof(device_cmd_prefix));
  120. /*
  121. * get MIDI packet and leave space for command prefix
  122. * and payload length
  123. */
  124. len = snd_rawmidi_transmit(midi_out_substream,
  125. bcd2k->midi_out_buf + 3, BUFSIZE - 3);
  126. if (len < 0)
  127. dev_err(&bcd2k->dev->dev, "%s: snd_rawmidi_transmit error %d\n",
  128. __func__, len);
  129. if (len <= 0)
  130. return;
  131. /* set payload length */
  132. bcd2k->midi_out_buf[2] = len;
  133. bcd2k->midi_out_urb->transfer_buffer_length = BUFSIZE;
  134. bcd2000_dump_buffer(PREFIX "sending to device: ",
  135. bcd2k->midi_out_buf, len+3);
  136. /* send packet to the BCD2000 */
  137. ret = usb_submit_urb(bcd2k->midi_out_urb, GFP_ATOMIC);
  138. if (ret < 0)
  139. dev_err(&bcd2k->dev->dev, PREFIX
  140. "%s (%p): usb_submit_urb() failed, ret=%d, len=%d\n",
  141. __func__, midi_out_substream, ret, len);
  142. else
  143. bcd2k->midi_out_active = 1;
  144. }
  145. static int bcd2000_midi_output_open(struct snd_rawmidi_substream *substream)
  146. {
  147. return 0;
  148. }
  149. static int bcd2000_midi_output_close(struct snd_rawmidi_substream *substream)
  150. {
  151. struct bcd2000 *bcd2k = substream->rmidi->private_data;
  152. if (bcd2k->midi_out_active) {
  153. usb_kill_urb(bcd2k->midi_out_urb);
  154. bcd2k->midi_out_active = 0;
  155. }
  156. return 0;
  157. }
  158. /* (de)register midi substream from client */
  159. static void bcd2000_midi_output_trigger(struct snd_rawmidi_substream *substream,
  160. int up)
  161. {
  162. struct bcd2000 *bcd2k = substream->rmidi->private_data;
  163. if (up) {
  164. bcd2k->midi_out_substream = substream;
  165. /* check if there is data userspace wants to send */
  166. if (!bcd2k->midi_out_active)
  167. bcd2000_midi_send(bcd2k);
  168. } else {
  169. bcd2k->midi_out_substream = NULL;
  170. }
  171. }
  172. static void bcd2000_output_complete(struct urb *urb)
  173. {
  174. struct bcd2000 *bcd2k = urb->context;
  175. bcd2k->midi_out_active = 0;
  176. if (urb->status)
  177. dev_warn(&urb->dev->dev,
  178. PREFIX "output urb->status: %d\n", urb->status);
  179. if (urb->status == -ESHUTDOWN)
  180. return;
  181. /* check if there is more data userspace wants to send */
  182. bcd2000_midi_send(bcd2k);
  183. }
  184. static void bcd2000_input_complete(struct urb *urb)
  185. {
  186. int ret;
  187. struct bcd2000 *bcd2k = urb->context;
  188. if (urb->status)
  189. dev_warn(&urb->dev->dev,
  190. PREFIX "input urb->status: %i\n", urb->status);
  191. if (!bcd2k || urb->status == -ESHUTDOWN)
  192. return;
  193. if (urb->actual_length > 0)
  194. bcd2000_midi_handle_input(bcd2k, urb->transfer_buffer,
  195. urb->actual_length);
  196. /* return URB to device */
  197. ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_ATOMIC);
  198. if (ret < 0)
  199. dev_err(&bcd2k->dev->dev, PREFIX
  200. "%s: usb_submit_urb() failed, ret=%d\n",
  201. __func__, ret);
  202. }
  203. static struct snd_rawmidi_ops bcd2000_midi_output = {
  204. .open = bcd2000_midi_output_open,
  205. .close = bcd2000_midi_output_close,
  206. .trigger = bcd2000_midi_output_trigger,
  207. };
  208. static struct snd_rawmidi_ops bcd2000_midi_input = {
  209. .open = bcd2000_midi_input_open,
  210. .close = bcd2000_midi_input_close,
  211. .trigger = bcd2000_midi_input_trigger,
  212. };
  213. static void bcd2000_init_device(struct bcd2000 *bcd2k)
  214. {
  215. int ret;
  216. init_usb_anchor(&bcd2k->anchor);
  217. usb_anchor_urb(bcd2k->midi_out_urb, &bcd2k->anchor);
  218. usb_anchor_urb(bcd2k->midi_in_urb, &bcd2k->anchor);
  219. /* copy init sequence into buffer */
  220. memcpy(bcd2k->midi_out_buf, bcd2000_init_sequence, 52);
  221. bcd2k->midi_out_urb->transfer_buffer_length = 52;
  222. /* submit sequence */
  223. ret = usb_submit_urb(bcd2k->midi_out_urb, GFP_KERNEL);
  224. if (ret < 0)
  225. dev_err(&bcd2k->dev->dev, PREFIX
  226. "%s: usb_submit_urb() out failed, ret=%d: ",
  227. __func__, ret);
  228. else
  229. bcd2k->midi_out_active = 1;
  230. /* pass URB to device to enable button and controller events */
  231. ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_KERNEL);
  232. if (ret < 0)
  233. dev_err(&bcd2k->dev->dev, PREFIX
  234. "%s: usb_submit_urb() in failed, ret=%d: ",
  235. __func__, ret);
  236. /* ensure initialization is finished */
  237. usb_wait_anchor_empty_timeout(&bcd2k->anchor, 1000);
  238. }
  239. static int bcd2000_init_midi(struct bcd2000 *bcd2k)
  240. {
  241. int ret;
  242. struct snd_rawmidi *rmidi;
  243. ret = snd_rawmidi_new(bcd2k->card, bcd2k->card->shortname, 0,
  244. 1, /* output */
  245. 1, /* input */
  246. &rmidi);
  247. if (ret < 0)
  248. return ret;
  249. strlcpy(rmidi->name, bcd2k->card->shortname, sizeof(rmidi->name));
  250. rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX;
  251. rmidi->private_data = bcd2k;
  252. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
  253. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  254. &bcd2000_midi_output);
  255. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  256. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  257. &bcd2000_midi_input);
  258. bcd2k->rmidi = rmidi;
  259. bcd2k->midi_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  260. bcd2k->midi_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  261. if (!bcd2k->midi_in_urb || !bcd2k->midi_out_urb) {
  262. dev_err(&bcd2k->dev->dev, PREFIX "usb_alloc_urb failed\n");
  263. return -ENOMEM;
  264. }
  265. usb_fill_int_urb(bcd2k->midi_in_urb, bcd2k->dev,
  266. usb_rcvintpipe(bcd2k->dev, 0x81),
  267. bcd2k->midi_in_buf, BUFSIZE,
  268. bcd2000_input_complete, bcd2k, 1);
  269. usb_fill_int_urb(bcd2k->midi_out_urb, bcd2k->dev,
  270. usb_sndintpipe(bcd2k->dev, 0x1),
  271. bcd2k->midi_out_buf, BUFSIZE,
  272. bcd2000_output_complete, bcd2k, 1);
  273. bcd2000_init_device(bcd2k);
  274. return 0;
  275. }
  276. static void bcd2000_free_usb_related_resources(struct bcd2000 *bcd2k,
  277. struct usb_interface *interface)
  278. {
  279. /* usb_kill_urb not necessary, urb is aborted automatically */
  280. usb_free_urb(bcd2k->midi_out_urb);
  281. usb_free_urb(bcd2k->midi_in_urb);
  282. if (bcd2k->intf) {
  283. usb_set_intfdata(bcd2k->intf, NULL);
  284. bcd2k->intf = NULL;
  285. }
  286. }
  287. static int bcd2000_probe(struct usb_interface *interface,
  288. const struct usb_device_id *usb_id)
  289. {
  290. struct snd_card *card;
  291. struct bcd2000 *bcd2k;
  292. unsigned int card_index;
  293. char usb_path[32];
  294. int err;
  295. mutex_lock(&devices_mutex);
  296. for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
  297. if (!test_bit(card_index, devices_used))
  298. break;
  299. if (card_index >= SNDRV_CARDS) {
  300. mutex_unlock(&devices_mutex);
  301. return -ENOENT;
  302. }
  303. err = snd_card_new(&interface->dev, index[card_index], id[card_index],
  304. THIS_MODULE, sizeof(*bcd2k), &card);
  305. if (err < 0) {
  306. mutex_unlock(&devices_mutex);
  307. return err;
  308. }
  309. bcd2k = card->private_data;
  310. bcd2k->dev = interface_to_usbdev(interface);
  311. bcd2k->card = card;
  312. bcd2k->card_index = card_index;
  313. bcd2k->intf = interface;
  314. snd_card_set_dev(card, &interface->dev);
  315. strncpy(card->driver, "snd-bcd2000", sizeof(card->driver));
  316. strncpy(card->shortname, "BCD2000", sizeof(card->shortname));
  317. usb_make_path(bcd2k->dev, usb_path, sizeof(usb_path));
  318. snprintf(bcd2k->card->longname, sizeof(bcd2k->card->longname),
  319. "Behringer BCD2000 at %s",
  320. usb_path);
  321. err = bcd2000_init_midi(bcd2k);
  322. if (err < 0)
  323. goto probe_error;
  324. err = snd_card_register(card);
  325. if (err < 0)
  326. goto probe_error;
  327. usb_set_intfdata(interface, bcd2k);
  328. set_bit(card_index, devices_used);
  329. mutex_unlock(&devices_mutex);
  330. return 0;
  331. probe_error:
  332. dev_info(&bcd2k->dev->dev, PREFIX "error during probing");
  333. bcd2000_free_usb_related_resources(bcd2k, interface);
  334. snd_card_free(card);
  335. mutex_unlock(&devices_mutex);
  336. return err;
  337. }
  338. static void bcd2000_disconnect(struct usb_interface *interface)
  339. {
  340. struct bcd2000 *bcd2k = usb_get_intfdata(interface);
  341. if (!bcd2k)
  342. return;
  343. mutex_lock(&devices_mutex);
  344. /* make sure that userspace cannot create new requests */
  345. snd_card_disconnect(bcd2k->card);
  346. bcd2000_free_usb_related_resources(bcd2k, interface);
  347. clear_bit(bcd2k->card_index, devices_used);
  348. snd_card_free_when_closed(bcd2k->card);
  349. mutex_unlock(&devices_mutex);
  350. }
  351. static struct usb_driver bcd2000_driver = {
  352. .name = "snd-bcd2000",
  353. .probe = bcd2000_probe,
  354. .disconnect = bcd2000_disconnect,
  355. .id_table = id_table,
  356. };
  357. module_usb_driver(bcd2000_driver);
  358. MODULE_DEVICE_TABLE(usb, id_table);
  359. MODULE_AUTHOR("Mario Kicherer, dev@kicherer.org");
  360. MODULE_DESCRIPTION("Behringer BCD2000 driver");
  361. MODULE_LICENSE("GPL");