xref: /linux/sound/usb/bcd2000/bcd2000.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Behringer BCD2000 driver
4  *
5  *   Copyright (C) 2014 Mario Kicherer (dev@kicherer.org)
6  */
7 
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>
19 
20 #define PREFIX "snd-bcd2000: "
21 #define BUFSIZE 64
22 
23 static const struct usb_device_id id_table[] = {
24 	{ USB_DEVICE(0x1397, 0x00bd) },
25 	{ },
26 };
27 
28 static const unsigned char device_cmd_prefix[] = {0x03, 0x00};
29 
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
38 };
39 
40 struct bcd2000 {
41 	struct usb_device *dev;
42 	struct snd_card *card;
43 	struct usb_interface *intf;
44 	int card_index;
45 
46 	spinlock_t midi_lock;
47 	int midi_out_active;
48 	struct snd_rawmidi *rmidi;
49 	struct snd_rawmidi_substream *midi_receive_substream;
50 	struct snd_rawmidi_substream *midi_out_substream;
51 
52 	unsigned char midi_in_buf[BUFSIZE];
53 	unsigned char midi_out_buf[BUFSIZE];
54 
55 	struct urb *midi_out_urb;
56 	struct urb *midi_in_urb;
57 
58 	struct usb_anchor anchor;
59 };
60 
61 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
62 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
63 
64 static DEFINE_MUTEX(devices_mutex);
65 static DECLARE_BITMAP(devices_used, SNDRV_CARDS);
66 static struct usb_driver bcd2000_driver;
67 
68 #ifdef CONFIG_SND_DEBUG
69 static void bcd2000_dump_buffer(const char *prefix, const char *buf, int len)
70 {
71 	print_hex_dump(KERN_DEBUG, prefix,
72 			DUMP_PREFIX_NONE, 16, 1,
73 			buf, len, false);
74 }
75 #else
76 static void bcd2000_dump_buffer(const char *prefix, const char *buf, int len) {}
77 #endif
78 
79 static int bcd2000_midi_input_open(struct snd_rawmidi_substream *substream)
80 {
81 	return 0;
82 }
83 
84 static int bcd2000_midi_input_close(struct snd_rawmidi_substream *substream)
85 {
86 	return 0;
87 }
88 
89 /* (de)register midi substream from client */
90 static void bcd2000_midi_input_trigger(struct snd_rawmidi_substream *substream,
91 						int up)
92 {
93 	struct bcd2000 *bcd2k = substream->rmidi->private_data;
94 
95 	guard(spinlock_irqsave)(&bcd2k->midi_lock);
96 	bcd2k->midi_receive_substream = up ? substream : NULL;
97 }
98 
99 static void bcd2000_midi_handle_input(struct bcd2000 *bcd2k,
100 				const unsigned char *buf, unsigned int buf_len)
101 {
102 	unsigned int payload_length, tocopy;
103 	struct snd_rawmidi_substream *midi_receive_substream;
104 
105 	midi_receive_substream = READ_ONCE(bcd2k->midi_receive_substream);
106 	if (!midi_receive_substream)
107 		return;
108 
109 	bcd2000_dump_buffer(PREFIX "received from device: ", buf, buf_len);
110 
111 	if (buf_len < 2)
112 		return;
113 
114 	payload_length = buf[0];
115 
116 	/* ignore packets without payload */
117 	if (payload_length == 0)
118 		return;
119 
120 	tocopy = min(payload_length, buf_len-1);
121 
122 	bcd2000_dump_buffer(PREFIX "sending to userspace: ",
123 					&buf[1], tocopy);
124 
125 	snd_rawmidi_receive(midi_receive_substream,
126 					&buf[1], tocopy);
127 }
128 
129 static void bcd2000_midi_send(struct bcd2000 *bcd2k)
130 {
131 	int len, ret;
132 	struct snd_rawmidi_substream *midi_out_substream;
133 
134 	BUILD_BUG_ON(sizeof(device_cmd_prefix) >= BUFSIZE);
135 
136 	midi_out_substream = READ_ONCE(bcd2k->midi_out_substream);
137 	if (!midi_out_substream)
138 		return;
139 
140 	if (!bcd2k->midi_out_urb)
141 		return;
142 
143 	/* copy command prefix bytes */
144 	memcpy(bcd2k->midi_out_buf, device_cmd_prefix,
145 		sizeof(device_cmd_prefix));
146 
147 	/*
148 	 * get MIDI packet and leave space for command prefix
149 	 * and payload length
150 	 */
151 	len = snd_rawmidi_transmit(midi_out_substream,
152 				bcd2k->midi_out_buf + 3, BUFSIZE - 3);
153 
154 	if (len < 0)
155 		dev_err(&bcd2k->dev->dev, "%s: snd_rawmidi_transmit error %d\n",
156 				__func__, len);
157 
158 	if (len <= 0)
159 		return;
160 
161 	/* set payload length */
162 	bcd2k->midi_out_buf[2] = len;
163 	bcd2k->midi_out_urb->transfer_buffer_length = BUFSIZE;
164 
165 	bcd2000_dump_buffer(PREFIX "sending to device: ",
166 			bcd2k->midi_out_buf, len+3);
167 
168 	/* send packet to the BCD2000 */
169 	ret = usb_submit_urb(bcd2k->midi_out_urb, GFP_ATOMIC);
170 	if (ret < 0)
171 		dev_err(&bcd2k->dev->dev, PREFIX
172 			"%s (%p): usb_submit_urb() failed, ret=%d, len=%d\n",
173 			__func__, midi_out_substream, ret, len);
174 	else
175 		bcd2k->midi_out_active = 1;
176 }
177 
178 static int bcd2000_midi_output_open(struct snd_rawmidi_substream *substream)
179 {
180 	return 0;
181 }
182 
183 static int bcd2000_midi_output_close(struct snd_rawmidi_substream *substream)
184 {
185 	struct bcd2000 *bcd2k = substream->rmidi->private_data;
186 	struct urb *urb = NULL;
187 
188 	/*
189 	 * Reference the URB under the lock so that it stays valid until
190 	 * usb_kill_urb() returns, even if a disconnect frees it in the
191 	 * meantime.  usb_kill_urb() cannot run under the lock, as the
192 	 * completion handler takes the same lock.
193 	 */
194 	scoped_guard(spinlock_irqsave, &bcd2k->midi_lock) {
195 		if (bcd2k->midi_out_active && bcd2k->midi_out_urb) {
196 			urb = usb_get_urb(bcd2k->midi_out_urb);
197 			bcd2k->midi_out_active = 0;
198 		}
199 	}
200 
201 	if (urb) {
202 		usb_kill_urb(urb);
203 		usb_put_urb(urb);
204 	}
205 
206 	return 0;
207 }
208 
209 /* (de)register midi substream from client */
210 static void bcd2000_midi_output_trigger(struct snd_rawmidi_substream *substream,
211 						int up)
212 {
213 	struct bcd2000 *bcd2k = substream->rmidi->private_data;
214 
215 	guard(spinlock_irqsave)(&bcd2k->midi_lock);
216 
217 	if (up) {
218 		bcd2k->midi_out_substream = substream;
219 		/* check if there is data userspace wants to send */
220 		if (!bcd2k->midi_out_active)
221 			bcd2000_midi_send(bcd2k);
222 	} else {
223 		bcd2k->midi_out_substream = NULL;
224 	}
225 }
226 
227 static void bcd2000_output_complete(struct urb *urb)
228 {
229 	struct bcd2000 *bcd2k = urb->context;
230 
231 	bcd2k->midi_out_active = 0;
232 
233 	if (urb->status)
234 		dev_warn(&urb->dev->dev,
235 			PREFIX "output urb->status: %d\n", urb->status);
236 
237 	if (urb->status == -ESHUTDOWN)
238 		return;
239 
240 	/* check if there is more data userspace wants to send */
241 	guard(spinlock_irqsave)(&bcd2k->midi_lock);
242 	bcd2000_midi_send(bcd2k);
243 }
244 
245 static void bcd2000_input_complete(struct urb *urb)
246 {
247 	int ret;
248 	struct bcd2000 *bcd2k = urb->context;
249 
250 	if (urb->status)
251 		dev_warn(&urb->dev->dev,
252 			PREFIX "input urb->status: %i\n", urb->status);
253 
254 	if (!bcd2k || urb->status == -ESHUTDOWN)
255 		return;
256 
257 	guard(spinlock_irqsave)(&bcd2k->midi_lock);
258 
259 	if (urb->actual_length > 0)
260 		bcd2000_midi_handle_input(bcd2k, urb->transfer_buffer,
261 					urb->actual_length);
262 
263 	/* return URB to device */
264 	ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_ATOMIC);
265 	if (ret < 0)
266 		dev_err(&bcd2k->dev->dev, PREFIX
267 			"%s: usb_submit_urb() failed, ret=%d\n",
268 			__func__, ret);
269 }
270 
271 static const struct snd_rawmidi_ops bcd2000_midi_output = {
272 	.open =    bcd2000_midi_output_open,
273 	.close =   bcd2000_midi_output_close,
274 	.trigger = bcd2000_midi_output_trigger,
275 };
276 
277 static const struct snd_rawmidi_ops bcd2000_midi_input = {
278 	.open =    bcd2000_midi_input_open,
279 	.close =   bcd2000_midi_input_close,
280 	.trigger = bcd2000_midi_input_trigger,
281 };
282 
283 static void bcd2000_init_device(struct bcd2000 *bcd2k)
284 {
285 	int ret;
286 
287 	init_usb_anchor(&bcd2k->anchor);
288 	usb_anchor_urb(bcd2k->midi_out_urb, &bcd2k->anchor);
289 	usb_anchor_urb(bcd2k->midi_in_urb, &bcd2k->anchor);
290 
291 	/* copy init sequence into buffer */
292 	memcpy(bcd2k->midi_out_buf, bcd2000_init_sequence, 52);
293 	bcd2k->midi_out_urb->transfer_buffer_length = 52;
294 
295 	/* submit sequence */
296 	ret = usb_submit_urb(bcd2k->midi_out_urb, GFP_KERNEL);
297 	if (ret < 0)
298 		dev_err(&bcd2k->dev->dev, PREFIX
299 			"%s: usb_submit_urb() out failed, ret=%d: ",
300 			__func__, ret);
301 	else
302 		bcd2k->midi_out_active = 1;
303 
304 	/* pass URB to device to enable button and controller events */
305 	ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_KERNEL);
306 	if (ret < 0)
307 		dev_err(&bcd2k->dev->dev, PREFIX
308 			"%s: usb_submit_urb() in failed, ret=%d: ",
309 			__func__, ret);
310 
311 	/* ensure initialization is finished */
312 	usb_wait_anchor_empty_timeout(&bcd2k->anchor, 1000);
313 }
314 
315 static int bcd2000_init_midi(struct bcd2000 *bcd2k)
316 {
317 	int ret;
318 	struct snd_rawmidi *rmidi;
319 
320 	ret = snd_rawmidi_new(bcd2k->card, bcd2k->card->shortname, 0,
321 					1, /* output */
322 					1, /* input */
323 					&rmidi);
324 
325 	if (ret < 0)
326 		return ret;
327 
328 	strscpy(rmidi->name, bcd2k->card->shortname, sizeof(rmidi->name));
329 
330 	rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX;
331 	rmidi->private_data = bcd2k;
332 
333 	rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
334 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
335 					&bcd2000_midi_output);
336 
337 	rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
338 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
339 					&bcd2000_midi_input);
340 
341 	bcd2k->rmidi = rmidi;
342 
343 	bcd2k->midi_in_urb = usb_alloc_urb(0, GFP_KERNEL);
344 	bcd2k->midi_out_urb = usb_alloc_urb(0, GFP_KERNEL);
345 
346 	if (!bcd2k->midi_in_urb || !bcd2k->midi_out_urb) {
347 		dev_err(&bcd2k->dev->dev, PREFIX "usb_alloc_urb failed\n");
348 		return -ENOMEM;
349 	}
350 
351 	usb_fill_int_urb(bcd2k->midi_in_urb, bcd2k->dev,
352 				usb_rcvintpipe(bcd2k->dev, 0x81),
353 				bcd2k->midi_in_buf, BUFSIZE,
354 				bcd2000_input_complete, bcd2k, 1);
355 
356 	usb_fill_int_urb(bcd2k->midi_out_urb, bcd2k->dev,
357 				usb_sndintpipe(bcd2k->dev, 0x1),
358 				bcd2k->midi_out_buf, BUFSIZE,
359 				bcd2000_output_complete, bcd2k, 1);
360 
361 	/* sanity checks of EPs before actually submitting */
362 	if (usb_urb_ep_type_check(bcd2k->midi_in_urb) ||
363 	    usb_urb_ep_type_check(bcd2k->midi_out_urb)) {
364 		dev_err(&bcd2k->dev->dev, "invalid MIDI EP\n");
365 		return -EINVAL;
366 	}
367 
368 	bcd2000_init_device(bcd2k);
369 
370 	return 0;
371 }
372 
373 static void bcd2000_midi_free(struct bcd2000 *bcd2k,
374 			      struct urb **urb_p)
375 {
376 	struct urb *urb = *urb_p;
377 
378 	if (!urb)
379 		return;
380 
381 	usb_poison_urb(urb);
382 	scoped_guard(spinlock_irq, &bcd2k->midi_lock)
383 		*urb_p = NULL;
384 
385 	usb_free_urb(urb);
386 }
387 
388 static void bcd2000_free_usb_related_resources(struct bcd2000 *bcd2k,
389 						struct usb_interface *interface)
390 {
391 	bcd2000_midi_free(bcd2k, &bcd2k->midi_out_urb);
392 	bcd2000_midi_free(bcd2k, &bcd2k->midi_in_urb);
393 
394 	if (bcd2k->intf) {
395 		usb_set_intfdata(bcd2k->intf, NULL);
396 		bcd2k->intf = NULL;
397 	}
398 }
399 
400 static int bcd2000_probe(struct usb_interface *interface,
401 				const struct usb_device_id *usb_id)
402 {
403 	struct snd_card *card;
404 	struct bcd2000 *bcd2k;
405 	unsigned int card_index;
406 	char usb_path[32];
407 	int err;
408 
409 	guard(mutex)(&devices_mutex);
410 
411 	for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
412 		if (!test_bit(card_index, devices_used))
413 			break;
414 
415 	if (card_index >= SNDRV_CARDS)
416 		return -ENOENT;
417 
418 	err = snd_card_new(&interface->dev, index[card_index], id[card_index],
419 			THIS_MODULE, sizeof(*bcd2k), &card);
420 	if (err < 0)
421 		return err;
422 
423 	bcd2k = card->private_data;
424 	bcd2k->dev = interface_to_usbdev(interface);
425 	bcd2k->card = card;
426 	bcd2k->card_index = card_index;
427 	bcd2k->intf = interface;
428 	spin_lock_init(&bcd2k->midi_lock);
429 
430 	snd_card_set_dev(card, &interface->dev);
431 
432 	strscpy(card->driver, "snd-bcd2000", sizeof(card->driver));
433 	strscpy(card->shortname, "BCD2000", sizeof(card->shortname));
434 	usb_make_path(bcd2k->dev, usb_path, sizeof(usb_path));
435 	snprintf(bcd2k->card->longname, sizeof(bcd2k->card->longname),
436 		    "Behringer BCD2000 at %s",
437 			usb_path);
438 
439 	err = bcd2000_init_midi(bcd2k);
440 	if (err < 0)
441 		goto probe_error;
442 
443 	err = snd_card_register(card);
444 	if (err < 0)
445 		goto probe_error;
446 
447 	usb_set_intfdata(interface, bcd2k);
448 	set_bit(card_index, devices_used);
449 
450 	return 0;
451 
452 probe_error:
453 	dev_info(&bcd2k->dev->dev, PREFIX "error during probing");
454 	bcd2000_free_usb_related_resources(bcd2k, interface);
455 	snd_card_free(card);
456 	return err;
457 }
458 
459 static void bcd2000_disconnect(struct usb_interface *interface)
460 {
461 	struct bcd2000 *bcd2k = usb_get_intfdata(interface);
462 
463 	if (!bcd2k)
464 		return;
465 
466 	guard(mutex)(&devices_mutex);
467 
468 	/* make sure that userspace cannot create new requests */
469 	snd_card_disconnect(bcd2k->card);
470 
471 	bcd2000_free_usb_related_resources(bcd2k, interface);
472 
473 	clear_bit(bcd2k->card_index, devices_used);
474 
475 	snd_card_free_when_closed(bcd2k->card);
476 }
477 
478 static struct usb_driver bcd2000_driver = {
479 	.name =		"snd-bcd2000",
480 	.probe =	bcd2000_probe,
481 	.disconnect =	bcd2000_disconnect,
482 	.id_table =	id_table,
483 };
484 
485 module_usb_driver(bcd2000_driver);
486 
487 MODULE_DEVICE_TABLE(usb, id_table);
488 MODULE_AUTHOR("Mario Kicherer, dev@kicherer.org");
489 MODULE_DESCRIPTION("Behringer BCD2000 driver");
490 MODULE_LICENSE("GPL");
491