1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Behringer BCD2000 driver
5 * Copyright (C) 2014 Mario Kicherer (dev@kicherer.org)
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/bitmap.h>
14 #include <linux/usb.h>
15 #include <linux/usb/audio.h>
16 #include <sound/core.h>
17 #include <sound/initval.h>
18 #include <sound/rawmidi.h>
20 #define PREFIX "snd-bcd2000: "
23 static const struct usb_device_id id_table[] = {
24 { USB_DEVICE(0x1397, 0x00bd) },
28 static unsigned char device_cmd_prefix[] = {0x03, 0x00};
30 static unsigned char bcd2000_init_sequence[] = {
31 0x07, 0x00, 0x00, 0x00, 0x78, 0x48, 0x1c, 0x81,
32 0xc4, 0x00, 0x00, 0x00, 0x5e, 0x53, 0x4a, 0xf7,
33 0x18, 0xfa, 0x11, 0xff, 0x6c, 0xf3, 0x90, 0xff,
34 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
35 0x18, 0xfa, 0x11, 0xff, 0x14, 0x00, 0x00, 0x00,
36 0x00, 0x00, 0x00, 0x00, 0xf2, 0x34, 0x4a, 0xf7,
37 0x18, 0xfa, 0x11, 0xff
41 struct usb_device *dev;
42 struct snd_card *card;
43 struct usb_interface *intf;
47 struct snd_rawmidi *rmidi;
48 struct snd_rawmidi_substream *midi_receive_substream;
49 struct snd_rawmidi_substream *midi_out_substream;
51 unsigned char midi_in_buf[BUFSIZE];
52 unsigned char midi_out_buf[BUFSIZE];
54 struct urb *midi_out_urb;
55 struct urb *midi_in_urb;
57 struct usb_anchor anchor;
60 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
61 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
63 static DEFINE_MUTEX(devices_mutex);
64 static DECLARE_BITMAP(devices_used, SNDRV_CARDS);
65 static struct usb_driver bcd2000_driver;
67 #ifdef CONFIG_SND_DEBUG
68 static void bcd2000_dump_buffer(const char *prefix, const char *buf, int len)
70 print_hex_dump(KERN_DEBUG, prefix,
71 DUMP_PREFIX_NONE, 16, 1,
75 static void bcd2000_dump_buffer(const char *prefix, const char *buf, int len) {}
78 static int bcd2000_midi_input_open(struct snd_rawmidi_substream *substream)
83 static int bcd2000_midi_input_close(struct snd_rawmidi_substream *substream)
88 /* (de)register midi substream from client */
89 static void bcd2000_midi_input_trigger(struct snd_rawmidi_substream *substream,
92 struct bcd2000 *bcd2k = substream->rmidi->private_data;
93 bcd2k->midi_receive_substream = up ? substream : NULL;
96 static void bcd2000_midi_handle_input(struct bcd2000 *bcd2k,
97 const unsigned char *buf, unsigned int buf_len)
99 unsigned int payload_length, tocopy;
100 struct snd_rawmidi_substream *midi_receive_substream;
102 midi_receive_substream = READ_ONCE(bcd2k->midi_receive_substream);
103 if (!midi_receive_substream)
106 bcd2000_dump_buffer(PREFIX "received from device: ", buf, buf_len);
111 payload_length = buf[0];
113 /* ignore packets without payload */
114 if (payload_length == 0)
117 tocopy = min(payload_length, buf_len-1);
119 bcd2000_dump_buffer(PREFIX "sending to userspace: ",
122 snd_rawmidi_receive(midi_receive_substream,
126 static void bcd2000_midi_send(struct bcd2000 *bcd2k)
129 struct snd_rawmidi_substream *midi_out_substream;
131 BUILD_BUG_ON(sizeof(device_cmd_prefix) >= BUFSIZE);
133 midi_out_substream = READ_ONCE(bcd2k->midi_out_substream);
134 if (!midi_out_substream)
137 /* copy command prefix bytes */
138 memcpy(bcd2k->midi_out_buf, device_cmd_prefix,
139 sizeof(device_cmd_prefix));
142 * get MIDI packet and leave space for command prefix
145 len = snd_rawmidi_transmit(midi_out_substream,
146 bcd2k->midi_out_buf + 3, BUFSIZE - 3);
149 dev_err(&bcd2k->dev->dev, "%s: snd_rawmidi_transmit error %d\n",
155 /* set payload length */
156 bcd2k->midi_out_buf[2] = len;
157 bcd2k->midi_out_urb->transfer_buffer_length = BUFSIZE;
159 bcd2000_dump_buffer(PREFIX "sending to device: ",
160 bcd2k->midi_out_buf, len+3);
162 /* send packet to the BCD2000 */
163 ret = usb_submit_urb(bcd2k->midi_out_urb, GFP_ATOMIC);
165 dev_err(&bcd2k->dev->dev, PREFIX
166 "%s (%p): usb_submit_urb() failed, ret=%d, len=%d\n",
167 __func__, midi_out_substream, ret, len);
169 bcd2k->midi_out_active = 1;
172 static int bcd2000_midi_output_open(struct snd_rawmidi_substream *substream)
177 static int bcd2000_midi_output_close(struct snd_rawmidi_substream *substream)
179 struct bcd2000 *bcd2k = substream->rmidi->private_data;
181 if (bcd2k->midi_out_active) {
182 usb_kill_urb(bcd2k->midi_out_urb);
183 bcd2k->midi_out_active = 0;
189 /* (de)register midi substream from client */
190 static void bcd2000_midi_output_trigger(struct snd_rawmidi_substream *substream,
193 struct bcd2000 *bcd2k = substream->rmidi->private_data;
196 bcd2k->midi_out_substream = substream;
197 /* check if there is data userspace wants to send */
198 if (!bcd2k->midi_out_active)
199 bcd2000_midi_send(bcd2k);
201 bcd2k->midi_out_substream = NULL;
205 static void bcd2000_output_complete(struct urb *urb)
207 struct bcd2000 *bcd2k = urb->context;
209 bcd2k->midi_out_active = 0;
212 dev_warn(&urb->dev->dev,
213 PREFIX "output urb->status: %d\n", urb->status);
215 if (urb->status == -ESHUTDOWN)
218 /* check if there is more data userspace wants to send */
219 bcd2000_midi_send(bcd2k);
222 static void bcd2000_input_complete(struct urb *urb)
225 struct bcd2000 *bcd2k = urb->context;
228 dev_warn(&urb->dev->dev,
229 PREFIX "input urb->status: %i\n", urb->status);
231 if (!bcd2k || urb->status == -ESHUTDOWN)
234 if (urb->actual_length > 0)
235 bcd2000_midi_handle_input(bcd2k, urb->transfer_buffer,
238 /* return URB to device */
239 ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_ATOMIC);
241 dev_err(&bcd2k->dev->dev, PREFIX
242 "%s: usb_submit_urb() failed, ret=%d\n",
246 static const struct snd_rawmidi_ops bcd2000_midi_output = {
247 .open = bcd2000_midi_output_open,
248 .close = bcd2000_midi_output_close,
249 .trigger = bcd2000_midi_output_trigger,
252 static const struct snd_rawmidi_ops bcd2000_midi_input = {
253 .open = bcd2000_midi_input_open,
254 .close = bcd2000_midi_input_close,
255 .trigger = bcd2000_midi_input_trigger,
258 static void bcd2000_init_device(struct bcd2000 *bcd2k)
262 init_usb_anchor(&bcd2k->anchor);
263 usb_anchor_urb(bcd2k->midi_out_urb, &bcd2k->anchor);
264 usb_anchor_urb(bcd2k->midi_in_urb, &bcd2k->anchor);
266 /* copy init sequence into buffer */
267 memcpy(bcd2k->midi_out_buf, bcd2000_init_sequence, 52);
268 bcd2k->midi_out_urb->transfer_buffer_length = 52;
270 /* submit sequence */
271 ret = usb_submit_urb(bcd2k->midi_out_urb, GFP_KERNEL);
273 dev_err(&bcd2k->dev->dev, PREFIX
274 "%s: usb_submit_urb() out failed, ret=%d: ",
277 bcd2k->midi_out_active = 1;
279 /* pass URB to device to enable button and controller events */
280 ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_KERNEL);
282 dev_err(&bcd2k->dev->dev, PREFIX
283 "%s: usb_submit_urb() in failed, ret=%d: ",
286 /* ensure initialization is finished */
287 usb_wait_anchor_empty_timeout(&bcd2k->anchor, 1000);
290 static int bcd2000_init_midi(struct bcd2000 *bcd2k)
293 struct snd_rawmidi *rmidi;
295 ret = snd_rawmidi_new(bcd2k->card, bcd2k->card->shortname, 0,
303 strlcpy(rmidi->name, bcd2k->card->shortname, sizeof(rmidi->name));
305 rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX;
306 rmidi->private_data = bcd2k;
308 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
309 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
310 &bcd2000_midi_output);
312 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
313 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
314 &bcd2000_midi_input);
316 bcd2k->rmidi = rmidi;
318 bcd2k->midi_in_urb = usb_alloc_urb(0, GFP_KERNEL);
319 bcd2k->midi_out_urb = usb_alloc_urb(0, GFP_KERNEL);
321 if (!bcd2k->midi_in_urb || !bcd2k->midi_out_urb) {
322 dev_err(&bcd2k->dev->dev, PREFIX "usb_alloc_urb failed\n");
326 usb_fill_int_urb(bcd2k->midi_in_urb, bcd2k->dev,
327 usb_rcvintpipe(bcd2k->dev, 0x81),
328 bcd2k->midi_in_buf, BUFSIZE,
329 bcd2000_input_complete, bcd2k, 1);
331 usb_fill_int_urb(bcd2k->midi_out_urb, bcd2k->dev,
332 usb_sndintpipe(bcd2k->dev, 0x1),
333 bcd2k->midi_out_buf, BUFSIZE,
334 bcd2000_output_complete, bcd2k, 1);
336 /* sanity checks of EPs before actually submitting */
337 if (usb_urb_ep_type_check(bcd2k->midi_in_urb) ||
338 usb_urb_ep_type_check(bcd2k->midi_out_urb)) {
339 dev_err(&bcd2k->dev->dev, "invalid MIDI EP\n");
343 bcd2000_init_device(bcd2k);
348 static void bcd2000_free_usb_related_resources(struct bcd2000 *bcd2k,
349 struct usb_interface *interface)
351 /* usb_kill_urb not necessary, urb is aborted automatically */
353 usb_free_urb(bcd2k->midi_out_urb);
354 usb_free_urb(bcd2k->midi_in_urb);
357 usb_set_intfdata(bcd2k->intf, NULL);
362 static int bcd2000_probe(struct usb_interface *interface,
363 const struct usb_device_id *usb_id)
365 struct snd_card *card;
366 struct bcd2000 *bcd2k;
367 unsigned int card_index;
371 mutex_lock(&devices_mutex);
373 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
374 if (!test_bit(card_index, devices_used))
377 if (card_index >= SNDRV_CARDS) {
378 mutex_unlock(&devices_mutex);
382 err = snd_card_new(&interface->dev, index[card_index], id[card_index],
383 THIS_MODULE, sizeof(*bcd2k), &card);
385 mutex_unlock(&devices_mutex);
389 bcd2k = card->private_data;
390 bcd2k->dev = interface_to_usbdev(interface);
392 bcd2k->card_index = card_index;
393 bcd2k->intf = interface;
395 snd_card_set_dev(card, &interface->dev);
397 strncpy(card->driver, "snd-bcd2000", sizeof(card->driver));
398 strncpy(card->shortname, "BCD2000", sizeof(card->shortname));
399 usb_make_path(bcd2k->dev, usb_path, sizeof(usb_path));
400 snprintf(bcd2k->card->longname, sizeof(bcd2k->card->longname),
401 "Behringer BCD2000 at %s",
404 err = bcd2000_init_midi(bcd2k);
408 err = snd_card_register(card);
412 usb_set_intfdata(interface, bcd2k);
413 set_bit(card_index, devices_used);
415 mutex_unlock(&devices_mutex);
419 dev_info(&bcd2k->dev->dev, PREFIX "error during probing");
420 bcd2000_free_usb_related_resources(bcd2k, interface);
422 mutex_unlock(&devices_mutex);
426 static void bcd2000_disconnect(struct usb_interface *interface)
428 struct bcd2000 *bcd2k = usb_get_intfdata(interface);
433 mutex_lock(&devices_mutex);
435 /* make sure that userspace cannot create new requests */
436 snd_card_disconnect(bcd2k->card);
438 bcd2000_free_usb_related_resources(bcd2k, interface);
440 clear_bit(bcd2k->card_index, devices_used);
442 snd_card_free_when_closed(bcd2k->card);
444 mutex_unlock(&devices_mutex);
447 static struct usb_driver bcd2000_driver = {
448 .name = "snd-bcd2000",
449 .probe = bcd2000_probe,
450 .disconnect = bcd2000_disconnect,
451 .id_table = id_table,
454 module_usb_driver(bcd2000_driver);
456 MODULE_DEVICE_TABLE(usb, id_table);
457 MODULE_AUTHOR("Mario Kicherer, dev@kicherer.org");
458 MODULE_DESCRIPTION("Behringer BCD2000 driver");
459 MODULE_LICENSE("GPL");