bcd2000.c 11 KB

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