Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux/fpc-iii.git] / sound / usb / bcd2000 / bcd2000.c
blobd6c8b29fe430b659a7079566eeea91e76f48cc9f
1 /*
2 * Behringer BCD2000 driver
4 * Copyright (C) 2014 Mario Kicherer (dev@kicherer.org)
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/bitmap.h>
23 #include <linux/usb.h>
24 #include <linux/usb/audio.h>
25 #include <sound/core.h>
26 #include <sound/initval.h>
27 #include <sound/rawmidi.h>
29 #define PREFIX "snd-bcd2000: "
30 #define BUFSIZE 64
32 static const struct usb_device_id id_table[] = {
33 { USB_DEVICE(0x1397, 0x00bd) },
34 { },
37 static unsigned char device_cmd_prefix[] = {0x03, 0x00};
39 static unsigned char bcd2000_init_sequence[] = {
40 0x07, 0x00, 0x00, 0x00, 0x78, 0x48, 0x1c, 0x81,
41 0xc4, 0x00, 0x00, 0x00, 0x5e, 0x53, 0x4a, 0xf7,
42 0x18, 0xfa, 0x11, 0xff, 0x6c, 0xf3, 0x90, 0xff,
43 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
44 0x18, 0xfa, 0x11, 0xff, 0x14, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0xf2, 0x34, 0x4a, 0xf7,
46 0x18, 0xfa, 0x11, 0xff
49 struct bcd2000 {
50 struct usb_device *dev;
51 struct snd_card *card;
52 struct usb_interface *intf;
53 int card_index;
55 int midi_out_active;
56 struct snd_rawmidi *rmidi;
57 struct snd_rawmidi_substream *midi_receive_substream;
58 struct snd_rawmidi_substream *midi_out_substream;
60 unsigned char midi_in_buf[BUFSIZE];
61 unsigned char midi_out_buf[BUFSIZE];
63 struct urb *midi_out_urb;
64 struct urb *midi_in_urb;
66 struct usb_anchor anchor;
69 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
70 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
72 static DEFINE_MUTEX(devices_mutex);
73 static DECLARE_BITMAP(devices_used, SNDRV_CARDS);
74 static struct usb_driver bcd2000_driver;
76 #ifdef CONFIG_SND_DEBUG
77 static void bcd2000_dump_buffer(const char *prefix, const char *buf, int len)
79 print_hex_dump(KERN_DEBUG, prefix,
80 DUMP_PREFIX_NONE, 16, 1,
81 buf, len, false);
83 #else
84 static void bcd2000_dump_buffer(const char *prefix, const char *buf, int len) {}
85 #endif
87 static int bcd2000_midi_input_open(struct snd_rawmidi_substream *substream)
89 return 0;
92 static int bcd2000_midi_input_close(struct snd_rawmidi_substream *substream)
94 return 0;
97 /* (de)register midi substream from client */
98 static void bcd2000_midi_input_trigger(struct snd_rawmidi_substream *substream,
99 int up)
101 struct bcd2000 *bcd2k = substream->rmidi->private_data;
102 bcd2k->midi_receive_substream = up ? substream : NULL;
105 static void bcd2000_midi_handle_input(struct bcd2000 *bcd2k,
106 const unsigned char *buf, unsigned int buf_len)
108 unsigned int payload_length, tocopy;
109 struct snd_rawmidi_substream *midi_receive_substream;
111 midi_receive_substream = READ_ONCE(bcd2k->midi_receive_substream);
112 if (!midi_receive_substream)
113 return;
115 bcd2000_dump_buffer(PREFIX "received from device: ", buf, buf_len);
117 if (buf_len < 2)
118 return;
120 payload_length = buf[0];
122 /* ignore packets without payload */
123 if (payload_length == 0)
124 return;
126 tocopy = min(payload_length, buf_len-1);
128 bcd2000_dump_buffer(PREFIX "sending to userspace: ",
129 &buf[1], tocopy);
131 snd_rawmidi_receive(midi_receive_substream,
132 &buf[1], tocopy);
135 static void bcd2000_midi_send(struct bcd2000 *bcd2k)
137 int len, ret;
138 struct snd_rawmidi_substream *midi_out_substream;
140 BUILD_BUG_ON(sizeof(device_cmd_prefix) >= BUFSIZE);
142 midi_out_substream = READ_ONCE(bcd2k->midi_out_substream);
143 if (!midi_out_substream)
144 return;
146 /* copy command prefix bytes */
147 memcpy(bcd2k->midi_out_buf, device_cmd_prefix,
148 sizeof(device_cmd_prefix));
151 * get MIDI packet and leave space for command prefix
152 * and payload length
154 len = snd_rawmidi_transmit(midi_out_substream,
155 bcd2k->midi_out_buf + 3, BUFSIZE - 3);
157 if (len < 0)
158 dev_err(&bcd2k->dev->dev, "%s: snd_rawmidi_transmit error %d\n",
159 __func__, len);
161 if (len <= 0)
162 return;
164 /* set payload length */
165 bcd2k->midi_out_buf[2] = len;
166 bcd2k->midi_out_urb->transfer_buffer_length = BUFSIZE;
168 bcd2000_dump_buffer(PREFIX "sending to device: ",
169 bcd2k->midi_out_buf, len+3);
171 /* send packet to the BCD2000 */
172 ret = usb_submit_urb(bcd2k->midi_out_urb, GFP_ATOMIC);
173 if (ret < 0)
174 dev_err(&bcd2k->dev->dev, PREFIX
175 "%s (%p): usb_submit_urb() failed, ret=%d, len=%d\n",
176 __func__, midi_out_substream, ret, len);
177 else
178 bcd2k->midi_out_active = 1;
181 static int bcd2000_midi_output_open(struct snd_rawmidi_substream *substream)
183 return 0;
186 static int bcd2000_midi_output_close(struct snd_rawmidi_substream *substream)
188 struct bcd2000 *bcd2k = substream->rmidi->private_data;
190 if (bcd2k->midi_out_active) {
191 usb_kill_urb(bcd2k->midi_out_urb);
192 bcd2k->midi_out_active = 0;
195 return 0;
198 /* (de)register midi substream from client */
199 static void bcd2000_midi_output_trigger(struct snd_rawmidi_substream *substream,
200 int up)
202 struct bcd2000 *bcd2k = substream->rmidi->private_data;
204 if (up) {
205 bcd2k->midi_out_substream = substream;
206 /* check if there is data userspace wants to send */
207 if (!bcd2k->midi_out_active)
208 bcd2000_midi_send(bcd2k);
209 } else {
210 bcd2k->midi_out_substream = NULL;
214 static void bcd2000_output_complete(struct urb *urb)
216 struct bcd2000 *bcd2k = urb->context;
218 bcd2k->midi_out_active = 0;
220 if (urb->status)
221 dev_warn(&urb->dev->dev,
222 PREFIX "output urb->status: %d\n", urb->status);
224 if (urb->status == -ESHUTDOWN)
225 return;
227 /* check if there is more data userspace wants to send */
228 bcd2000_midi_send(bcd2k);
231 static void bcd2000_input_complete(struct urb *urb)
233 int ret;
234 struct bcd2000 *bcd2k = urb->context;
236 if (urb->status)
237 dev_warn(&urb->dev->dev,
238 PREFIX "input urb->status: %i\n", urb->status);
240 if (!bcd2k || urb->status == -ESHUTDOWN)
241 return;
243 if (urb->actual_length > 0)
244 bcd2000_midi_handle_input(bcd2k, urb->transfer_buffer,
245 urb->actual_length);
247 /* return URB to device */
248 ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_ATOMIC);
249 if (ret < 0)
250 dev_err(&bcd2k->dev->dev, PREFIX
251 "%s: usb_submit_urb() failed, ret=%d\n",
252 __func__, ret);
255 static const struct snd_rawmidi_ops bcd2000_midi_output = {
256 .open = bcd2000_midi_output_open,
257 .close = bcd2000_midi_output_close,
258 .trigger = bcd2000_midi_output_trigger,
261 static const struct snd_rawmidi_ops bcd2000_midi_input = {
262 .open = bcd2000_midi_input_open,
263 .close = bcd2000_midi_input_close,
264 .trigger = bcd2000_midi_input_trigger,
267 static void bcd2000_init_device(struct bcd2000 *bcd2k)
269 int ret;
271 init_usb_anchor(&bcd2k->anchor);
272 usb_anchor_urb(bcd2k->midi_out_urb, &bcd2k->anchor);
273 usb_anchor_urb(bcd2k->midi_in_urb, &bcd2k->anchor);
275 /* copy init sequence into buffer */
276 memcpy(bcd2k->midi_out_buf, bcd2000_init_sequence, 52);
277 bcd2k->midi_out_urb->transfer_buffer_length = 52;
279 /* submit sequence */
280 ret = usb_submit_urb(bcd2k->midi_out_urb, GFP_KERNEL);
281 if (ret < 0)
282 dev_err(&bcd2k->dev->dev, PREFIX
283 "%s: usb_submit_urb() out failed, ret=%d: ",
284 __func__, ret);
285 else
286 bcd2k->midi_out_active = 1;
288 /* pass URB to device to enable button and controller events */
289 ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_KERNEL);
290 if (ret < 0)
291 dev_err(&bcd2k->dev->dev, PREFIX
292 "%s: usb_submit_urb() in failed, ret=%d: ",
293 __func__, ret);
295 /* ensure initialization is finished */
296 usb_wait_anchor_empty_timeout(&bcd2k->anchor, 1000);
299 static int bcd2000_init_midi(struct bcd2000 *bcd2k)
301 int ret;
302 struct snd_rawmidi *rmidi;
304 ret = snd_rawmidi_new(bcd2k->card, bcd2k->card->shortname, 0,
305 1, /* output */
306 1, /* input */
307 &rmidi);
309 if (ret < 0)
310 return ret;
312 strlcpy(rmidi->name, bcd2k->card->shortname, sizeof(rmidi->name));
314 rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX;
315 rmidi->private_data = bcd2k;
317 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
318 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
319 &bcd2000_midi_output);
321 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
322 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
323 &bcd2000_midi_input);
325 bcd2k->rmidi = rmidi;
327 bcd2k->midi_in_urb = usb_alloc_urb(0, GFP_KERNEL);
328 bcd2k->midi_out_urb = usb_alloc_urb(0, GFP_KERNEL);
330 if (!bcd2k->midi_in_urb || !bcd2k->midi_out_urb) {
331 dev_err(&bcd2k->dev->dev, PREFIX "usb_alloc_urb failed\n");
332 return -ENOMEM;
335 usb_fill_int_urb(bcd2k->midi_in_urb, bcd2k->dev,
336 usb_rcvintpipe(bcd2k->dev, 0x81),
337 bcd2k->midi_in_buf, BUFSIZE,
338 bcd2000_input_complete, bcd2k, 1);
340 usb_fill_int_urb(bcd2k->midi_out_urb, bcd2k->dev,
341 usb_sndintpipe(bcd2k->dev, 0x1),
342 bcd2k->midi_out_buf, BUFSIZE,
343 bcd2000_output_complete, bcd2k, 1);
345 /* sanity checks of EPs before actually submitting */
346 if (usb_urb_ep_type_check(bcd2k->midi_in_urb) ||
347 usb_urb_ep_type_check(bcd2k->midi_out_urb)) {
348 dev_err(&bcd2k->dev->dev, "invalid MIDI EP\n");
349 return -EINVAL;
352 bcd2000_init_device(bcd2k);
354 return 0;
357 static void bcd2000_free_usb_related_resources(struct bcd2000 *bcd2k,
358 struct usb_interface *interface)
360 /* usb_kill_urb not necessary, urb is aborted automatically */
362 usb_free_urb(bcd2k->midi_out_urb);
363 usb_free_urb(bcd2k->midi_in_urb);
365 if (bcd2k->intf) {
366 usb_set_intfdata(bcd2k->intf, NULL);
367 bcd2k->intf = NULL;
371 static int bcd2000_probe(struct usb_interface *interface,
372 const struct usb_device_id *usb_id)
374 struct snd_card *card;
375 struct bcd2000 *bcd2k;
376 unsigned int card_index;
377 char usb_path[32];
378 int err;
380 mutex_lock(&devices_mutex);
382 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
383 if (!test_bit(card_index, devices_used))
384 break;
386 if (card_index >= SNDRV_CARDS) {
387 mutex_unlock(&devices_mutex);
388 return -ENOENT;
391 err = snd_card_new(&interface->dev, index[card_index], id[card_index],
392 THIS_MODULE, sizeof(*bcd2k), &card);
393 if (err < 0) {
394 mutex_unlock(&devices_mutex);
395 return err;
398 bcd2k = card->private_data;
399 bcd2k->dev = interface_to_usbdev(interface);
400 bcd2k->card = card;
401 bcd2k->card_index = card_index;
402 bcd2k->intf = interface;
404 snd_card_set_dev(card, &interface->dev);
406 strncpy(card->driver, "snd-bcd2000", sizeof(card->driver));
407 strncpy(card->shortname, "BCD2000", sizeof(card->shortname));
408 usb_make_path(bcd2k->dev, usb_path, sizeof(usb_path));
409 snprintf(bcd2k->card->longname, sizeof(bcd2k->card->longname),
410 "Behringer BCD2000 at %s",
411 usb_path);
413 err = bcd2000_init_midi(bcd2k);
414 if (err < 0)
415 goto probe_error;
417 err = snd_card_register(card);
418 if (err < 0)
419 goto probe_error;
421 usb_set_intfdata(interface, bcd2k);
422 set_bit(card_index, devices_used);
424 mutex_unlock(&devices_mutex);
425 return 0;
427 probe_error:
428 dev_info(&bcd2k->dev->dev, PREFIX "error during probing");
429 bcd2000_free_usb_related_resources(bcd2k, interface);
430 snd_card_free(card);
431 mutex_unlock(&devices_mutex);
432 return err;
435 static void bcd2000_disconnect(struct usb_interface *interface)
437 struct bcd2000 *bcd2k = usb_get_intfdata(interface);
439 if (!bcd2k)
440 return;
442 mutex_lock(&devices_mutex);
444 /* make sure that userspace cannot create new requests */
445 snd_card_disconnect(bcd2k->card);
447 bcd2000_free_usb_related_resources(bcd2k, interface);
449 clear_bit(bcd2k->card_index, devices_used);
451 snd_card_free_when_closed(bcd2k->card);
453 mutex_unlock(&devices_mutex);
456 static struct usb_driver bcd2000_driver = {
457 .name = "snd-bcd2000",
458 .probe = bcd2000_probe,
459 .disconnect = bcd2000_disconnect,
460 .id_table = id_table,
463 module_usb_driver(bcd2000_driver);
465 MODULE_DEVICE_TABLE(usb, id_table);
466 MODULE_AUTHOR("Mario Kicherer, dev@kicherer.org");
467 MODULE_DESCRIPTION("Behringer BCD2000 driver");
468 MODULE_LICENSE("GPL");