WIP FPC-III support
[linux/fpc-iii.git] / sound / usb / bcd2000 / bcd2000.c
blob010976d9ceb25fbc5b09552783dbc0a3c0e00451
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Behringer BCD2000 driver
5 * Copyright (C) 2014 Mario Kicherer (dev@kicherer.org)
6 */
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: "
21 #define BUFSIZE 64
23 static const struct usb_device_id id_table[] = {
24 { USB_DEVICE(0x1397, 0x00bd) },
25 { },
28 static const unsigned char device_cmd_prefix[] = {0x03, 0x00};
30 static const 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
40 struct bcd2000 {
41 struct usb_device *dev;
42 struct snd_card *card;
43 struct usb_interface *intf;
44 int card_index;
46 int midi_out_active;
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,
72 buf, len, false);
74 #else
75 static void bcd2000_dump_buffer(const char *prefix, const char *buf, int len) {}
76 #endif
78 static int bcd2000_midi_input_open(struct snd_rawmidi_substream *substream)
80 return 0;
83 static int bcd2000_midi_input_close(struct snd_rawmidi_substream *substream)
85 return 0;
88 /* (de)register midi substream from client */
89 static void bcd2000_midi_input_trigger(struct snd_rawmidi_substream *substream,
90 int up)
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)
104 return;
106 bcd2000_dump_buffer(PREFIX "received from device: ", buf, buf_len);
108 if (buf_len < 2)
109 return;
111 payload_length = buf[0];
113 /* ignore packets without payload */
114 if (payload_length == 0)
115 return;
117 tocopy = min(payload_length, buf_len-1);
119 bcd2000_dump_buffer(PREFIX "sending to userspace: ",
120 &buf[1], tocopy);
122 snd_rawmidi_receive(midi_receive_substream,
123 &buf[1], tocopy);
126 static void bcd2000_midi_send(struct bcd2000 *bcd2k)
128 int len, ret;
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)
135 return;
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
143 * and payload length
145 len = snd_rawmidi_transmit(midi_out_substream,
146 bcd2k->midi_out_buf + 3, BUFSIZE - 3);
148 if (len < 0)
149 dev_err(&bcd2k->dev->dev, "%s: snd_rawmidi_transmit error %d\n",
150 __func__, len);
152 if (len <= 0)
153 return;
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);
164 if (ret < 0)
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);
168 else
169 bcd2k->midi_out_active = 1;
172 static int bcd2000_midi_output_open(struct snd_rawmidi_substream *substream)
174 return 0;
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;
186 return 0;
189 /* (de)register midi substream from client */
190 static void bcd2000_midi_output_trigger(struct snd_rawmidi_substream *substream,
191 int up)
193 struct bcd2000 *bcd2k = substream->rmidi->private_data;
195 if (up) {
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);
200 } else {
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;
211 if (urb->status)
212 dev_warn(&urb->dev->dev,
213 PREFIX "output urb->status: %d\n", urb->status);
215 if (urb->status == -ESHUTDOWN)
216 return;
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)
224 int ret;
225 struct bcd2000 *bcd2k = urb->context;
227 if (urb->status)
228 dev_warn(&urb->dev->dev,
229 PREFIX "input urb->status: %i\n", urb->status);
231 if (!bcd2k || urb->status == -ESHUTDOWN)
232 return;
234 if (urb->actual_length > 0)
235 bcd2000_midi_handle_input(bcd2k, urb->transfer_buffer,
236 urb->actual_length);
238 /* return URB to device */
239 ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_ATOMIC);
240 if (ret < 0)
241 dev_err(&bcd2k->dev->dev, PREFIX
242 "%s: usb_submit_urb() failed, ret=%d\n",
243 __func__, ret);
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)
260 int ret;
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);
272 if (ret < 0)
273 dev_err(&bcd2k->dev->dev, PREFIX
274 "%s: usb_submit_urb() out failed, ret=%d: ",
275 __func__, ret);
276 else
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);
281 if (ret < 0)
282 dev_err(&bcd2k->dev->dev, PREFIX
283 "%s: usb_submit_urb() in failed, ret=%d: ",
284 __func__, ret);
286 /* ensure initialization is finished */
287 usb_wait_anchor_empty_timeout(&bcd2k->anchor, 1000);
290 static int bcd2000_init_midi(struct bcd2000 *bcd2k)
292 int ret;
293 struct snd_rawmidi *rmidi;
295 ret = snd_rawmidi_new(bcd2k->card, bcd2k->card->shortname, 0,
296 1, /* output */
297 1, /* input */
298 &rmidi);
300 if (ret < 0)
301 return ret;
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");
323 return -ENOMEM;
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");
340 return -EINVAL;
343 bcd2000_init_device(bcd2k);
345 return 0;
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);
356 if (bcd2k->intf) {
357 usb_set_intfdata(bcd2k->intf, NULL);
358 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;
368 char usb_path[32];
369 int err;
371 mutex_lock(&devices_mutex);
373 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
374 if (!test_bit(card_index, devices_used))
375 break;
377 if (card_index >= SNDRV_CARDS) {
378 mutex_unlock(&devices_mutex);
379 return -ENOENT;
382 err = snd_card_new(&interface->dev, index[card_index], id[card_index],
383 THIS_MODULE, sizeof(*bcd2k), &card);
384 if (err < 0) {
385 mutex_unlock(&devices_mutex);
386 return err;
389 bcd2k = card->private_data;
390 bcd2k->dev = interface_to_usbdev(interface);
391 bcd2k->card = card;
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",
402 usb_path);
404 err = bcd2000_init_midi(bcd2k);
405 if (err < 0)
406 goto probe_error;
408 err = snd_card_register(card);
409 if (err < 0)
410 goto probe_error;
412 usb_set_intfdata(interface, bcd2k);
413 set_bit(card_index, devices_used);
415 mutex_unlock(&devices_mutex);
416 return 0;
418 probe_error:
419 dev_info(&bcd2k->dev->dev, PREFIX "error during probing");
420 bcd2000_free_usb_related_resources(bcd2k, interface);
421 snd_card_free(card);
422 mutex_unlock(&devices_mutex);
423 return err;
426 static void bcd2000_disconnect(struct usb_interface *interface)
428 struct bcd2000 *bcd2k = usb_get_intfdata(interface);
430 if (!bcd2k)
431 return;
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");