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 187 if (bcd2k->midi_out_active && bcd2k->midi_out_urb) { 188 usb_kill_urb(bcd2k->midi_out_urb); 189 bcd2k->midi_out_active = 0; 190 } 191 192 return 0; 193 } 194 195 /* (de)register midi substream from client */ 196 static void bcd2000_midi_output_trigger(struct snd_rawmidi_substream *substream, 197 int up) 198 { 199 struct bcd2000 *bcd2k = substream->rmidi->private_data; 200 201 guard(spinlock_irqsave)(&bcd2k->midi_lock); 202 203 if (up) { 204 bcd2k->midi_out_substream = substream; 205 /* check if there is data userspace wants to send */ 206 if (!bcd2k->midi_out_active) 207 bcd2000_midi_send(bcd2k); 208 } else { 209 bcd2k->midi_out_substream = NULL; 210 } 211 } 212 213 static void bcd2000_output_complete(struct urb *urb) 214 { 215 struct bcd2000 *bcd2k = urb->context; 216 217 bcd2k->midi_out_active = 0; 218 219 if (urb->status) 220 dev_warn(&urb->dev->dev, 221 PREFIX "output urb->status: %d\n", urb->status); 222 223 if (urb->status == -ESHUTDOWN) 224 return; 225 226 /* check if there is more data userspace wants to send */ 227 guard(spinlock_irqsave)(&bcd2k->midi_lock); 228 bcd2000_midi_send(bcd2k); 229 } 230 231 static void bcd2000_input_complete(struct urb *urb) 232 { 233 int ret; 234 struct bcd2000 *bcd2k = urb->context; 235 236 if (urb->status) 237 dev_warn(&urb->dev->dev, 238 PREFIX "input urb->status: %i\n", urb->status); 239 240 if (!bcd2k || urb->status == -ESHUTDOWN) 241 return; 242 243 guard(spinlock_irqsave)(&bcd2k->midi_lock); 244 245 if (urb->actual_length > 0) 246 bcd2000_midi_handle_input(bcd2k, urb->transfer_buffer, 247 urb->actual_length); 248 249 /* return URB to device */ 250 ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_ATOMIC); 251 if (ret < 0) 252 dev_err(&bcd2k->dev->dev, PREFIX 253 "%s: usb_submit_urb() failed, ret=%d\n", 254 __func__, ret); 255 } 256 257 static const struct snd_rawmidi_ops bcd2000_midi_output = { 258 .open = bcd2000_midi_output_open, 259 .close = bcd2000_midi_output_close, 260 .trigger = bcd2000_midi_output_trigger, 261 }; 262 263 static const struct snd_rawmidi_ops bcd2000_midi_input = { 264 .open = bcd2000_midi_input_open, 265 .close = bcd2000_midi_input_close, 266 .trigger = bcd2000_midi_input_trigger, 267 }; 268 269 static void bcd2000_init_device(struct bcd2000 *bcd2k) 270 { 271 int ret; 272 273 init_usb_anchor(&bcd2k->anchor); 274 usb_anchor_urb(bcd2k->midi_out_urb, &bcd2k->anchor); 275 usb_anchor_urb(bcd2k->midi_in_urb, &bcd2k->anchor); 276 277 /* copy init sequence into buffer */ 278 memcpy(bcd2k->midi_out_buf, bcd2000_init_sequence, 52); 279 bcd2k->midi_out_urb->transfer_buffer_length = 52; 280 281 /* submit sequence */ 282 ret = usb_submit_urb(bcd2k->midi_out_urb, GFP_KERNEL); 283 if (ret < 0) 284 dev_err(&bcd2k->dev->dev, PREFIX 285 "%s: usb_submit_urb() out failed, ret=%d: ", 286 __func__, ret); 287 else 288 bcd2k->midi_out_active = 1; 289 290 /* pass URB to device to enable button and controller events */ 291 ret = usb_submit_urb(bcd2k->midi_in_urb, GFP_KERNEL); 292 if (ret < 0) 293 dev_err(&bcd2k->dev->dev, PREFIX 294 "%s: usb_submit_urb() in failed, ret=%d: ", 295 __func__, ret); 296 297 /* ensure initialization is finished */ 298 usb_wait_anchor_empty_timeout(&bcd2k->anchor, 1000); 299 } 300 301 static int bcd2000_init_midi(struct bcd2000 *bcd2k) 302 { 303 int ret; 304 struct snd_rawmidi *rmidi; 305 306 ret = snd_rawmidi_new(bcd2k->card, bcd2k->card->shortname, 0, 307 1, /* output */ 308 1, /* input */ 309 &rmidi); 310 311 if (ret < 0) 312 return ret; 313 314 strscpy(rmidi->name, bcd2k->card->shortname, sizeof(rmidi->name)); 315 316 rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX; 317 rmidi->private_data = bcd2k; 318 319 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT; 320 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, 321 &bcd2000_midi_output); 322 323 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT; 324 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, 325 &bcd2000_midi_input); 326 327 bcd2k->rmidi = rmidi; 328 329 bcd2k->midi_in_urb = usb_alloc_urb(0, GFP_KERNEL); 330 bcd2k->midi_out_urb = usb_alloc_urb(0, GFP_KERNEL); 331 332 if (!bcd2k->midi_in_urb || !bcd2k->midi_out_urb) { 333 dev_err(&bcd2k->dev->dev, PREFIX "usb_alloc_urb failed\n"); 334 return -ENOMEM; 335 } 336 337 usb_fill_int_urb(bcd2k->midi_in_urb, bcd2k->dev, 338 usb_rcvintpipe(bcd2k->dev, 0x81), 339 bcd2k->midi_in_buf, BUFSIZE, 340 bcd2000_input_complete, bcd2k, 1); 341 342 usb_fill_int_urb(bcd2k->midi_out_urb, bcd2k->dev, 343 usb_sndintpipe(bcd2k->dev, 0x1), 344 bcd2k->midi_out_buf, BUFSIZE, 345 bcd2000_output_complete, bcd2k, 1); 346 347 /* sanity checks of EPs before actually submitting */ 348 if (usb_urb_ep_type_check(bcd2k->midi_in_urb) || 349 usb_urb_ep_type_check(bcd2k->midi_out_urb)) { 350 dev_err(&bcd2k->dev->dev, "invalid MIDI EP\n"); 351 return -EINVAL; 352 } 353 354 bcd2000_init_device(bcd2k); 355 356 return 0; 357 } 358 359 static void bcd2000_midi_free(struct bcd2000 *bcd2k, 360 struct urb **urb_p) 361 { 362 struct urb *urb = *urb_p; 363 364 if (!urb) 365 return; 366 367 usb_poison_urb(urb); 368 scoped_guard(spinlock_irq, &bcd2k->midi_lock) 369 *urb_p = NULL; 370 371 usb_free_urb(urb); 372 } 373 374 static void bcd2000_free_usb_related_resources(struct bcd2000 *bcd2k, 375 struct usb_interface *interface) 376 { 377 bcd2000_midi_free(bcd2k, &bcd2k->midi_out_urb); 378 bcd2000_midi_free(bcd2k, &bcd2k->midi_in_urb); 379 380 if (bcd2k->intf) { 381 usb_set_intfdata(bcd2k->intf, NULL); 382 bcd2k->intf = NULL; 383 } 384 } 385 386 static int bcd2000_probe(struct usb_interface *interface, 387 const struct usb_device_id *usb_id) 388 { 389 struct snd_card *card; 390 struct bcd2000 *bcd2k; 391 unsigned int card_index; 392 char usb_path[32]; 393 int err; 394 395 guard(mutex)(&devices_mutex); 396 397 for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) 398 if (!test_bit(card_index, devices_used)) 399 break; 400 401 if (card_index >= SNDRV_CARDS) 402 return -ENOENT; 403 404 err = snd_card_new(&interface->dev, index[card_index], id[card_index], 405 THIS_MODULE, sizeof(*bcd2k), &card); 406 if (err < 0) 407 return err; 408 409 bcd2k = card->private_data; 410 bcd2k->dev = interface_to_usbdev(interface); 411 bcd2k->card = card; 412 bcd2k->card_index = card_index; 413 bcd2k->intf = interface; 414 spin_lock_init(&bcd2k->midi_lock); 415 416 snd_card_set_dev(card, &interface->dev); 417 418 strscpy(card->driver, "snd-bcd2000", sizeof(card->driver)); 419 strscpy(card->shortname, "BCD2000", sizeof(card->shortname)); 420 usb_make_path(bcd2k->dev, usb_path, sizeof(usb_path)); 421 snprintf(bcd2k->card->longname, sizeof(bcd2k->card->longname), 422 "Behringer BCD2000 at %s", 423 usb_path); 424 425 err = bcd2000_init_midi(bcd2k); 426 if (err < 0) 427 goto probe_error; 428 429 err = snd_card_register(card); 430 if (err < 0) 431 goto probe_error; 432 433 usb_set_intfdata(interface, bcd2k); 434 set_bit(card_index, devices_used); 435 436 return 0; 437 438 probe_error: 439 dev_info(&bcd2k->dev->dev, PREFIX "error during probing"); 440 bcd2000_free_usb_related_resources(bcd2k, interface); 441 snd_card_free(card); 442 return err; 443 } 444 445 static void bcd2000_disconnect(struct usb_interface *interface) 446 { 447 struct bcd2000 *bcd2k = usb_get_intfdata(interface); 448 449 if (!bcd2k) 450 return; 451 452 guard(mutex)(&devices_mutex); 453 454 /* make sure that userspace cannot create new requests */ 455 snd_card_disconnect(bcd2k->card); 456 457 bcd2000_free_usb_related_resources(bcd2k, interface); 458 459 clear_bit(bcd2k->card_index, devices_used); 460 461 snd_card_free_when_closed(bcd2k->card); 462 } 463 464 static struct usb_driver bcd2000_driver = { 465 .name = "snd-bcd2000", 466 .probe = bcd2000_probe, 467 .disconnect = bcd2000_disconnect, 468 .id_table = id_table, 469 }; 470 471 module_usb_driver(bcd2000_driver); 472 473 MODULE_DEVICE_TABLE(usb, id_table); 474 MODULE_AUTHOR("Mario Kicherer, dev@kicherer.org"); 475 MODULE_DESCRIPTION("Behringer BCD2000 driver"); 476 MODULE_LICENSE("GPL"); 477