1 /* 2 * usbmidi.c - ALSA USB MIDI driver 3 * 4 * Copyright (c) 2002-2009 Clemens Ladisch 5 * All rights reserved. 6 * 7 * Based on the OSS usb-midi driver by NAGANO Daisuke, 8 * NetBSD's umidi driver by Takuya SHIOZAKI, 9 * the "USB Device Class Definition for MIDI Devices" by Roland 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions, and the following disclaimer, 16 * without modification. 17 * 2. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * Alternatively, this software may be distributed and/or modified under the 21 * terms of the GNU General Public License as published by the Free Software 22 * Foundation; either version 2 of the License, or (at your option) any later 23 * version. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <linux/kernel.h> 39 #include <linux/types.h> 40 #include <linux/bitops.h> 41 #include <linux/interrupt.h> 42 #include <linux/spinlock.h> 43 #include <linux/string.h> 44 #include <linux/init.h> 45 #include <linux/slab.h> 46 #include <linux/timer.h> 47 #include <linux/usb.h> 48 #include <linux/wait.h> 49 #include <linux/usb/audio.h> 50 #include <linux/usb/midi.h> 51 #include <linux/module.h> 52 53 #include <sound/core.h> 54 #include <sound/control.h> 55 #include <sound/rawmidi.h> 56 #include <sound/asequencer.h> 57 #include "usbaudio.h" 58 #include "midi.h" 59 #include "power.h" 60 #include "helper.h" 61 62 /* 63 * define this to log all USB packets 64 */ 65 /* #define DUMP_PACKETS */ 66 67 /* 68 * how long to wait after some USB errors, so that hub_wq can disconnect() us 69 * without too many spurious errors 70 */ 71 #define ERROR_DELAY_JIFFIES (HZ / 10) 72 73 #define OUTPUT_URBS 7 74 #define INPUT_URBS 7 75 76 77 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 78 MODULE_DESCRIPTION("USB Audio/MIDI helper module"); 79 MODULE_LICENSE("Dual BSD/GPL"); 80 81 struct snd_usb_midi_in_endpoint; 82 struct snd_usb_midi_out_endpoint; 83 struct snd_usb_midi_endpoint; 84 85 struct usb_protocol_ops { 86 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int); 87 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb); 88 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t); 89 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint *); 90 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint *); 91 }; 92 93 struct snd_usb_midi { 94 struct usb_device *dev; 95 struct snd_card *card; 96 struct usb_interface *iface; 97 const struct snd_usb_audio_quirk *quirk; 98 struct snd_rawmidi *rmidi; 99 const struct usb_protocol_ops *usb_protocol_ops; 100 struct list_head list; 101 struct timer_list error_timer; 102 spinlock_t disc_lock; 103 struct rw_semaphore disc_rwsem; 104 struct mutex mutex; 105 u32 usb_id; 106 int next_midi_device; 107 108 struct snd_usb_midi_endpoint { 109 struct snd_usb_midi_out_endpoint *out; 110 struct snd_usb_midi_in_endpoint *in; 111 } endpoints[MIDI_MAX_ENDPOINTS]; 112 unsigned long input_triggered; 113 unsigned int opened[2]; 114 unsigned char disconnected; 115 unsigned char input_running; 116 117 struct snd_kcontrol *roland_load_ctl; 118 }; 119 120 struct snd_usb_midi_out_endpoint { 121 struct snd_usb_midi *umidi; 122 struct out_urb_context { 123 struct urb *urb; 124 struct snd_usb_midi_out_endpoint *ep; 125 } urbs[OUTPUT_URBS]; 126 unsigned int active_urbs; 127 unsigned int drain_urbs; 128 int max_transfer; /* size of urb buffer */ 129 struct work_struct work; 130 unsigned int next_urb; 131 spinlock_t buffer_lock; 132 133 struct usbmidi_out_port { 134 struct snd_usb_midi_out_endpoint *ep; 135 struct snd_rawmidi_substream *substream; 136 int active; 137 uint8_t cable; /* cable number << 4 */ 138 uint8_t state; 139 #define STATE_UNKNOWN 0 140 #define STATE_1PARAM 1 141 #define STATE_2PARAM_1 2 142 #define STATE_2PARAM_2 3 143 #define STATE_SYSEX_0 4 144 #define STATE_SYSEX_1 5 145 #define STATE_SYSEX_2 6 146 uint8_t data[2]; 147 } ports[0x10]; 148 int current_port; 149 150 wait_queue_head_t drain_wait; 151 }; 152 153 struct snd_usb_midi_in_endpoint { 154 struct snd_usb_midi *umidi; 155 struct urb *urbs[INPUT_URBS]; 156 struct usbmidi_in_port { 157 struct snd_rawmidi_substream *substream; 158 u8 running_status_length; 159 } ports[0x10]; 160 u8 seen_f5; 161 bool in_sysex; 162 u8 last_cin; 163 u8 error_resubmit; 164 int current_port; 165 }; 166 167 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep); 168 169 static const uint8_t snd_usbmidi_cin_length[] = { 170 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1 171 }; 172 173 /* 174 * Submits the URB, with error handling. 175 */ 176 static int snd_usbmidi_submit_urb(struct urb *urb, gfp_t flags) 177 { 178 int err = usb_submit_urb(urb, flags); 179 if (err < 0 && err != -ENODEV) 180 dev_err(&urb->dev->dev, "usb_submit_urb: %d\n", err); 181 return err; 182 } 183 184 /* 185 * Error handling for URB completion functions. 186 */ 187 static int snd_usbmidi_urb_error(const struct urb *urb) 188 { 189 switch (urb->status) { 190 /* manually unlinked, or device gone */ 191 case -ENOENT: 192 case -ECONNRESET: 193 case -ESHUTDOWN: 194 case -ENODEV: 195 return -ENODEV; 196 /* errors that might occur during unplugging */ 197 case -EPROTO: 198 case -ETIME: 199 case -EILSEQ: 200 return -EIO; 201 default: 202 dev_err(&urb->dev->dev, "urb status %d\n", urb->status); 203 return 0; /* continue */ 204 } 205 } 206 207 /* 208 * Receives a chunk of MIDI data. 209 */ 210 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint *ep, 211 int portidx, uint8_t *data, int length) 212 { 213 struct usbmidi_in_port *port = &ep->ports[portidx]; 214 215 if (!port->substream) { 216 dev_dbg(&ep->umidi->dev->dev, "unexpected port %d!\n", portidx); 217 return; 218 } 219 if (!test_bit(port->substream->number, &ep->umidi->input_triggered)) 220 return; 221 snd_rawmidi_receive(port->substream, data, length); 222 } 223 224 #ifdef DUMP_PACKETS 225 static void dump_urb(const char *type, const u8 *data, int length) 226 { 227 pr_debug("%s packet: [", type); 228 for (; length > 0; ++data, --length) 229 pr_cont(" %02x", *data); 230 pr_cont(" ]\n"); 231 } 232 #else 233 #define dump_urb(type, data, length) /* nothing */ 234 #endif 235 236 /* 237 * Processes the data read from the device. 238 */ 239 static void snd_usbmidi_in_urb_complete(struct urb *urb) 240 { 241 struct snd_usb_midi_in_endpoint *ep = urb->context; 242 243 if (urb->status == 0) { 244 dump_urb("received", urb->transfer_buffer, urb->actual_length); 245 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer, 246 urb->actual_length); 247 } else { 248 int err = snd_usbmidi_urb_error(urb); 249 if (err < 0) { 250 if (err != -ENODEV) { 251 ep->error_resubmit = 1; 252 mod_timer(&ep->umidi->error_timer, 253 jiffies + ERROR_DELAY_JIFFIES); 254 } 255 return; 256 } 257 } 258 259 urb->dev = ep->umidi->dev; 260 snd_usbmidi_submit_urb(urb, GFP_ATOMIC); 261 } 262 263 static void snd_usbmidi_out_urb_complete(struct urb *urb) 264 { 265 struct out_urb_context *context = urb->context; 266 struct snd_usb_midi_out_endpoint *ep = context->ep; 267 unsigned int urb_index; 268 269 scoped_guard(spinlock_irqsave, &ep->buffer_lock) { 270 urb_index = context - ep->urbs; 271 ep->active_urbs &= ~(1 << urb_index); 272 if (unlikely(ep->drain_urbs)) { 273 ep->drain_urbs &= ~(1 << urb_index); 274 wake_up(&ep->drain_wait); 275 } 276 } 277 if (urb->status < 0) { 278 int err = snd_usbmidi_urb_error(urb); 279 if (err < 0) { 280 if (err != -ENODEV) 281 mod_timer(&ep->umidi->error_timer, 282 jiffies + ERROR_DELAY_JIFFIES); 283 return; 284 } 285 } 286 snd_usbmidi_do_output(ep); 287 } 288 289 /* 290 * This is called when some data should be transferred to the device 291 * (from one or more substreams). 292 */ 293 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep) 294 { 295 unsigned int urb_index; 296 struct urb *urb; 297 298 guard(spinlock_irqsave)(&ep->buffer_lock); 299 if (ep->umidi->disconnected) 300 return; 301 302 urb_index = ep->next_urb; 303 for (;;) { 304 if (!(ep->active_urbs & (1 << urb_index))) { 305 urb = ep->urbs[urb_index].urb; 306 urb->transfer_buffer_length = 0; 307 ep->umidi->usb_protocol_ops->output(ep, urb); 308 if (urb->transfer_buffer_length == 0) 309 break; 310 311 dump_urb("sending", urb->transfer_buffer, 312 urb->transfer_buffer_length); 313 urb->dev = ep->umidi->dev; 314 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0) 315 break; 316 ep->active_urbs |= 1 << urb_index; 317 } 318 if (++urb_index >= OUTPUT_URBS) 319 urb_index = 0; 320 if (urb_index == ep->next_urb) 321 break; 322 } 323 ep->next_urb = urb_index; 324 } 325 326 static void snd_usbmidi_out_work(struct work_struct *work) 327 { 328 struct snd_usb_midi_out_endpoint *ep = 329 container_of(work, struct snd_usb_midi_out_endpoint, work); 330 331 snd_usbmidi_do_output(ep); 332 } 333 334 /* called after transfers had been interrupted due to some USB error */ 335 static void snd_usbmidi_error_timer(struct timer_list *t) 336 { 337 struct snd_usb_midi *umidi = timer_container_of(umidi, t, error_timer); 338 unsigned int i, j; 339 340 guard(spinlock)(&umidi->disc_lock); 341 if (umidi->disconnected) { 342 return; 343 } 344 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 345 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in; 346 if (in && in->error_resubmit) { 347 in->error_resubmit = 0; 348 for (j = 0; j < INPUT_URBS; ++j) { 349 if (atomic_read(&in->urbs[j]->use_count)) 350 continue; 351 in->urbs[j]->dev = umidi->dev; 352 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC); 353 } 354 } 355 if (umidi->endpoints[i].out) 356 snd_usbmidi_do_output(umidi->endpoints[i].out); 357 } 358 } 359 360 /* helper function to send static data that may not DMA-able */ 361 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint *ep, 362 const void *data, int len) 363 { 364 int err = 0; 365 void *buf = kmemdup(data, len, GFP_KERNEL); 366 if (!buf) 367 return -ENOMEM; 368 dump_urb("sending", buf, len); 369 if (ep->urbs[0].urb) 370 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe, 371 buf, len, NULL, 250); 372 kfree(buf); 373 return err; 374 } 375 376 /* 377 * Standard USB MIDI protocol: see the spec. 378 * Midiman protocol: like the standard protocol, but the control byte is the 379 * fourth byte in each packet, and uses length instead of CIN. 380 */ 381 382 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint *ep, 383 uint8_t *buffer, int buffer_length) 384 { 385 int i; 386 387 for (i = 0; i + 3 < buffer_length; i += 4) 388 if (buffer[i] != 0) { 389 int cable = buffer[i] >> 4; 390 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f]; 391 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], 392 length); 393 } 394 } 395 396 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint *ep, 397 uint8_t *buffer, int buffer_length) 398 { 399 int i; 400 401 for (i = 0; i + 3 < buffer_length; i += 4) 402 if (buffer[i + 3] != 0) { 403 int port = buffer[i + 3] >> 4; 404 int length = buffer[i + 3] & 3; 405 snd_usbmidi_input_data(ep, port, &buffer[i], length); 406 } 407 } 408 409 /* 410 * Buggy M-Audio device: running status on input results in a packet that has 411 * the data bytes but not the status byte and that is marked with CIN 4. 412 */ 413 static void snd_usbmidi_maudio_broken_running_status_input( 414 struct snd_usb_midi_in_endpoint *ep, 415 uint8_t *buffer, int buffer_length) 416 { 417 int i; 418 419 for (i = 0; i + 3 < buffer_length; i += 4) 420 if (buffer[i] != 0) { 421 int cable = buffer[i] >> 4; 422 u8 cin = buffer[i] & 0x0f; 423 struct usbmidi_in_port *port = &ep->ports[cable]; 424 int length; 425 426 length = snd_usbmidi_cin_length[cin]; 427 if (cin == 0xf && buffer[i + 1] >= 0xf8) 428 ; /* realtime msg: no running status change */ 429 else if (cin >= 0x8 && cin <= 0xe) 430 /* channel msg */ 431 port->running_status_length = length - 1; 432 else if (cin == 0x4 && 433 port->running_status_length != 0 && 434 buffer[i + 1] < 0x80) 435 /* CIN 4 that is not a SysEx */ 436 length = port->running_status_length; 437 else 438 /* 439 * All other msgs cannot begin running status. 440 * (A channel msg sent as two or three CIN 0xF 441 * packets could in theory, but this device 442 * doesn't use this format.) 443 */ 444 port->running_status_length = 0; 445 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], 446 length); 447 } 448 } 449 450 /* 451 * QinHeng CH345 is buggy: every second packet inside a SysEx has not CIN 4 452 * but the previously seen CIN, but still with three data bytes. 453 */ 454 static void ch345_broken_sysex_input(struct snd_usb_midi_in_endpoint *ep, 455 uint8_t *buffer, int buffer_length) 456 { 457 unsigned int i, cin, length; 458 459 for (i = 0; i + 3 < buffer_length; i += 4) { 460 if (buffer[i] == 0 && i > 0) 461 break; 462 cin = buffer[i] & 0x0f; 463 if (ep->in_sysex && 464 cin == ep->last_cin && 465 (buffer[i + 1 + (cin == 0x6)] & 0x80) == 0) 466 cin = 0x4; 467 #if 0 468 if (buffer[i + 1] == 0x90) { 469 /* 470 * Either a corrupted running status or a real note-on 471 * message; impossible to detect reliably. 472 */ 473 } 474 #endif 475 length = snd_usbmidi_cin_length[cin]; 476 snd_usbmidi_input_data(ep, 0, &buffer[i + 1], length); 477 ep->in_sysex = cin == 0x4; 478 if (!ep->in_sysex) 479 ep->last_cin = cin; 480 } 481 } 482 483 /* 484 * CME protocol: like the standard protocol, but SysEx commands are sent as a 485 * single USB packet preceded by a 0x0F byte, as are system realtime 486 * messages and MIDI Active Sensing. 487 * Also, multiple messages can be sent in the same packet. 488 */ 489 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep, 490 uint8_t *buffer, int buffer_length) 491 { 492 int remaining = buffer_length; 493 494 /* 495 * CME send sysex, song position pointer, system realtime 496 * and active sensing using CIN 0x0f, which in the standard 497 * is only intended for single byte unparsed data. 498 * So we need to interpret these here before sending them on. 499 * By default, we assume single byte data, which is true 500 * for system realtime (midi clock, start, stop and continue) 501 * and active sensing, and handle the other (known) cases 502 * separately. 503 * In contrast to the standard, CME does not split sysex 504 * into multiple 4-byte packets, but lumps everything together 505 * into one. In addition, CME can string multiple messages 506 * together in the same packet; pressing the Record button 507 * on an UF6 sends a sysex message directly followed 508 * by a song position pointer in the same packet. 509 * For it to have any reasonable meaning, a sysex message 510 * needs to be at least 3 bytes in length (0xf0, id, 0xf7), 511 * corresponding to a packet size of 4 bytes, and the ones sent 512 * by CME devices are 6 or 7 bytes, making the packet fragments 513 * 7 or 8 bytes long (six or seven bytes plus preceding CN+CIN byte). 514 * For the other types, the packet size is always 4 bytes, 515 * as per the standard, with the data size being 3 for SPP 516 * and 1 for the others. 517 * Thus all packet fragments are at least 4 bytes long, so we can 518 * skip anything that is shorter; this also conveniantly skips 519 * packets with size 0, which CME devices continuously send when 520 * they have nothing better to do. 521 * Another quirk is that sometimes multiple messages are sent 522 * in the same packet. This has been observed for midi clock 523 * and active sensing i.e. 0x0f 0xf8 0x00 0x00 0x0f 0xfe 0x00 0x00, 524 * but also multiple note ons/offs, and control change together 525 * with MIDI clock. Similarly, some sysex messages are followed by 526 * the song position pointer in the same packet, and occasionally 527 * additionally by a midi clock or active sensing. 528 * We handle this by looping over all data and parsing it along the way. 529 */ 530 while (remaining >= 4) { 531 int source_length = 4; /* default */ 532 533 if ((buffer[0] & 0x0f) == 0x0f) { 534 int data_length = 1; /* default */ 535 536 if (buffer[1] == 0xf0) { 537 /* Sysex: Find EOX and send on whole message. */ 538 /* To kick off the search, skip the first 539 * two bytes (CN+CIN and SYSEX (0xf0). 540 */ 541 uint8_t *tmp_buf = buffer + 2; 542 int tmp_length = remaining - 2; 543 544 while (tmp_length > 1 && *tmp_buf != 0xf7) { 545 tmp_buf++; 546 tmp_length--; 547 } 548 data_length = tmp_buf - buffer; 549 source_length = data_length + 1; 550 } else if (buffer[1] == 0xf2) { 551 /* Three byte song position pointer */ 552 data_length = 3; 553 } 554 snd_usbmidi_input_data(ep, buffer[0] >> 4, 555 &buffer[1], data_length); 556 } else { 557 /* normal channel events */ 558 snd_usbmidi_standard_input(ep, buffer, source_length); 559 } 560 buffer += source_length; 561 remaining -= source_length; 562 } 563 } 564 565 /* 566 * Adds one USB MIDI packet to the output buffer. 567 */ 568 static void snd_usbmidi_output_standard_packet(struct urb *urb, uint8_t p0, 569 uint8_t p1, uint8_t p2, 570 uint8_t p3) 571 { 572 573 uint8_t *buf = 574 (uint8_t *)urb->transfer_buffer + urb->transfer_buffer_length; 575 buf[0] = p0; 576 buf[1] = p1; 577 buf[2] = p2; 578 buf[3] = p3; 579 urb->transfer_buffer_length += 4; 580 } 581 582 /* 583 * Adds one Midiman packet to the output buffer. 584 */ 585 static void snd_usbmidi_output_midiman_packet(struct urb *urb, uint8_t p0, 586 uint8_t p1, uint8_t p2, 587 uint8_t p3) 588 { 589 590 uint8_t *buf = 591 (uint8_t *)urb->transfer_buffer + urb->transfer_buffer_length; 592 buf[0] = p1; 593 buf[1] = p2; 594 buf[2] = p3; 595 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f]; 596 urb->transfer_buffer_length += 4; 597 } 598 599 /* 600 * Converts MIDI commands to USB MIDI packets. 601 */ 602 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port *port, 603 uint8_t b, struct urb *urb) 604 { 605 uint8_t p0 = port->cable; 606 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) = 607 port->ep->umidi->usb_protocol_ops->output_packet; 608 609 if (b >= 0xf8) { 610 output_packet(urb, p0 | 0x0f, b, 0, 0); 611 } else if (b >= 0xf0) { 612 switch (b) { 613 case 0xf0: 614 port->data[0] = b; 615 port->state = STATE_SYSEX_1; 616 break; 617 case 0xf1: 618 case 0xf3: 619 port->data[0] = b; 620 port->state = STATE_1PARAM; 621 break; 622 case 0xf2: 623 port->data[0] = b; 624 port->state = STATE_2PARAM_1; 625 break; 626 case 0xf4: 627 case 0xf5: 628 port->state = STATE_UNKNOWN; 629 break; 630 case 0xf6: 631 output_packet(urb, p0 | 0x05, 0xf6, 0, 0); 632 port->state = STATE_UNKNOWN; 633 break; 634 case 0xf7: 635 switch (port->state) { 636 case STATE_SYSEX_0: 637 output_packet(urb, p0 | 0x05, 0xf7, 0, 0); 638 break; 639 case STATE_SYSEX_1: 640 output_packet(urb, p0 | 0x06, port->data[0], 641 0xf7, 0); 642 break; 643 case STATE_SYSEX_2: 644 output_packet(urb, p0 | 0x07, port->data[0], 645 port->data[1], 0xf7); 646 break; 647 } 648 port->state = STATE_UNKNOWN; 649 break; 650 } 651 } else if (b >= 0x80) { 652 port->data[0] = b; 653 if (b >= 0xc0 && b <= 0xdf) 654 port->state = STATE_1PARAM; 655 else 656 port->state = STATE_2PARAM_1; 657 } else { /* b < 0x80 */ 658 switch (port->state) { 659 case STATE_1PARAM: 660 if (port->data[0] < 0xf0) { 661 p0 |= port->data[0] >> 4; 662 } else { 663 p0 |= 0x02; 664 port->state = STATE_UNKNOWN; 665 } 666 output_packet(urb, p0, port->data[0], b, 0); 667 break; 668 case STATE_2PARAM_1: 669 port->data[1] = b; 670 port->state = STATE_2PARAM_2; 671 break; 672 case STATE_2PARAM_2: 673 if (port->data[0] < 0xf0) { 674 p0 |= port->data[0] >> 4; 675 port->state = STATE_2PARAM_1; 676 } else { 677 p0 |= 0x03; 678 port->state = STATE_UNKNOWN; 679 } 680 output_packet(urb, p0, port->data[0], port->data[1], b); 681 break; 682 case STATE_SYSEX_0: 683 port->data[0] = b; 684 port->state = STATE_SYSEX_1; 685 break; 686 case STATE_SYSEX_1: 687 port->data[1] = b; 688 port->state = STATE_SYSEX_2; 689 break; 690 case STATE_SYSEX_2: 691 output_packet(urb, p0 | 0x04, port->data[0], 692 port->data[1], b); 693 port->state = STATE_SYSEX_0; 694 break; 695 } 696 } 697 } 698 699 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint *ep, 700 struct urb *urb) 701 { 702 int port0 = ep->current_port; 703 int i; 704 705 for (i = 0; i < 0x10; ++i) { 706 int portnum = (port0 + i) & 15; 707 struct usbmidi_out_port *port = &ep->ports[portnum]; 708 709 if (!port->active) 710 continue; 711 while (urb->transfer_buffer_length + 3 < ep->max_transfer) { 712 uint8_t b; 713 714 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) { 715 port->active = 0; 716 break; 717 } 718 snd_usbmidi_transmit_byte(port, b, urb); 719 } 720 } 721 ep->current_port = (port0 + 1) & 15; 722 } 723 724 static const struct usb_protocol_ops snd_usbmidi_standard_ops = { 725 .input = snd_usbmidi_standard_input, 726 .output = snd_usbmidi_standard_output, 727 .output_packet = snd_usbmidi_output_standard_packet, 728 }; 729 730 static const struct usb_protocol_ops snd_usbmidi_midiman_ops = { 731 .input = snd_usbmidi_midiman_input, 732 .output = snd_usbmidi_standard_output, 733 .output_packet = snd_usbmidi_output_midiman_packet, 734 }; 735 736 static const 737 struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = { 738 .input = snd_usbmidi_maudio_broken_running_status_input, 739 .output = snd_usbmidi_standard_output, 740 .output_packet = snd_usbmidi_output_standard_packet, 741 }; 742 743 static const struct usb_protocol_ops snd_usbmidi_cme_ops = { 744 .input = snd_usbmidi_cme_input, 745 .output = snd_usbmidi_standard_output, 746 .output_packet = snd_usbmidi_output_standard_packet, 747 }; 748 749 static const struct usb_protocol_ops snd_usbmidi_ch345_broken_sysex_ops = { 750 .input = ch345_broken_sysex_input, 751 .output = snd_usbmidi_standard_output, 752 .output_packet = snd_usbmidi_output_standard_packet, 753 }; 754 755 /* 756 * AKAI MPD16 protocol: 757 * 758 * For control port (endpoint 1): 759 * ============================== 760 * One or more chunks consisting of first byte of (0x10 | msg_len) and then a 761 * SysEx message (msg_len=9 bytes long). 762 * 763 * For data port (endpoint 2): 764 * =========================== 765 * One or more chunks consisting of first byte of (0x20 | msg_len) and then a 766 * MIDI message (msg_len bytes long) 767 * 768 * Messages sent: Active Sense, Note On, Poly Pressure, Control Change. 769 */ 770 static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep, 771 uint8_t *buffer, int buffer_length) 772 { 773 unsigned int pos = 0; 774 unsigned int len = (unsigned int)buffer_length; 775 while (pos < len) { 776 unsigned int port = (buffer[pos] >> 4) - 1; 777 unsigned int msg_len = buffer[pos] & 0x0f; 778 pos++; 779 if (pos + msg_len <= len && port < 2) 780 snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len); 781 pos += msg_len; 782 } 783 } 784 785 #define MAX_AKAI_SYSEX_LEN 9 786 787 static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep, 788 struct urb *urb) 789 { 790 uint8_t *msg; 791 int pos, end, count, buf_end; 792 uint8_t tmp[MAX_AKAI_SYSEX_LEN]; 793 struct snd_rawmidi_substream *substream = ep->ports[0].substream; 794 795 if (!ep->ports[0].active) 796 return; 797 798 msg = urb->transfer_buffer + urb->transfer_buffer_length; 799 buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1; 800 801 /* only try adding more data when there's space for at least 1 SysEx */ 802 while (urb->transfer_buffer_length < buf_end) { 803 count = snd_rawmidi_transmit_peek(substream, 804 tmp, MAX_AKAI_SYSEX_LEN); 805 if (!count) { 806 ep->ports[0].active = 0; 807 return; 808 } 809 /* try to skip non-SysEx data */ 810 for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++) 811 ; 812 813 if (pos > 0) { 814 snd_rawmidi_transmit_ack(substream, pos); 815 continue; 816 } 817 818 /* look for the start or end marker */ 819 for (end = 1; end < count && tmp[end] < 0xF0; end++) 820 ; 821 822 /* next SysEx started before the end of current one */ 823 if (end < count && tmp[end] == 0xF0) { 824 /* it's incomplete - drop it */ 825 snd_rawmidi_transmit_ack(substream, end); 826 continue; 827 } 828 /* SysEx complete */ 829 if (end < count && tmp[end] == 0xF7) { 830 /* queue it, ack it, and get the next one */ 831 count = end + 1; 832 msg[0] = 0x10 | count; 833 memcpy(&msg[1], tmp, count); 834 snd_rawmidi_transmit_ack(substream, count); 835 urb->transfer_buffer_length += count + 1; 836 msg += count + 1; 837 continue; 838 } 839 /* less than 9 bytes and no end byte - wait for more */ 840 if (count < MAX_AKAI_SYSEX_LEN) { 841 ep->ports[0].active = 0; 842 return; 843 } 844 /* 9 bytes and no end marker in sight - malformed, skip it */ 845 snd_rawmidi_transmit_ack(substream, count); 846 } 847 } 848 849 static const struct usb_protocol_ops snd_usbmidi_akai_ops = { 850 .input = snd_usbmidi_akai_input, 851 .output = snd_usbmidi_akai_output, 852 }; 853 854 /* 855 * Novation USB MIDI protocol: number of data bytes is in the first byte 856 * (when receiving) (+1!) or in the second byte (when sending); data begins 857 * at the third byte. 858 */ 859 860 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint *ep, 861 uint8_t *buffer, int buffer_length) 862 { 863 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1) 864 return; 865 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1); 866 } 867 868 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint *ep, 869 struct urb *urb) 870 { 871 uint8_t *transfer_buffer; 872 int count; 873 874 if (!ep->ports[0].active) 875 return; 876 transfer_buffer = urb->transfer_buffer; 877 count = snd_rawmidi_transmit(ep->ports[0].substream, 878 &transfer_buffer[2], 879 ep->max_transfer - 2); 880 if (count < 1) { 881 ep->ports[0].active = 0; 882 return; 883 } 884 transfer_buffer[0] = 0; 885 transfer_buffer[1] = count; 886 urb->transfer_buffer_length = 2 + count; 887 } 888 889 static const struct usb_protocol_ops snd_usbmidi_novation_ops = { 890 .input = snd_usbmidi_novation_input, 891 .output = snd_usbmidi_novation_output, 892 }; 893 894 /* 895 * "raw" protocol: just move raw MIDI bytes from/to the endpoint 896 */ 897 898 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint *ep, 899 uint8_t *buffer, int buffer_length) 900 { 901 snd_usbmidi_input_data(ep, 0, buffer, buffer_length); 902 } 903 904 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint *ep, 905 struct urb *urb) 906 { 907 int count; 908 909 if (!ep->ports[0].active) 910 return; 911 count = snd_rawmidi_transmit(ep->ports[0].substream, 912 urb->transfer_buffer, 913 ep->max_transfer); 914 if (count < 1) { 915 ep->ports[0].active = 0; 916 return; 917 } 918 urb->transfer_buffer_length = count; 919 } 920 921 static const struct usb_protocol_ops snd_usbmidi_raw_ops = { 922 .input = snd_usbmidi_raw_input, 923 .output = snd_usbmidi_raw_output, 924 }; 925 926 /* 927 * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes. 928 */ 929 930 static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint *ep, 931 uint8_t *buffer, int buffer_length) 932 { 933 if (buffer_length > 2) 934 snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2); 935 } 936 937 static const struct usb_protocol_ops snd_usbmidi_ftdi_ops = { 938 .input = snd_usbmidi_ftdi_input, 939 .output = snd_usbmidi_raw_output, 940 }; 941 942 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep, 943 uint8_t *buffer, int buffer_length) 944 { 945 if (buffer_length != 9) 946 return; 947 buffer_length = 8; 948 while (buffer_length && buffer[buffer_length - 1] == 0xFD) 949 buffer_length--; 950 if (buffer_length) 951 snd_usbmidi_input_data(ep, 0, buffer, buffer_length); 952 } 953 954 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep, 955 struct urb *urb) 956 { 957 int count; 958 959 if (!ep->ports[0].active) 960 return; 961 switch (snd_usb_get_speed(ep->umidi->dev)) { 962 case USB_SPEED_HIGH: 963 case USB_SPEED_SUPER: 964 case USB_SPEED_SUPER_PLUS: 965 count = 1; 966 break; 967 default: 968 count = 2; 969 } 970 count = snd_rawmidi_transmit(ep->ports[0].substream, 971 urb->transfer_buffer, 972 count); 973 if (count < 1) { 974 ep->ports[0].active = 0; 975 return; 976 } 977 978 memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count); 979 urb->transfer_buffer_length = ep->max_transfer; 980 } 981 982 static const struct usb_protocol_ops snd_usbmidi_122l_ops = { 983 .input = snd_usbmidi_us122l_input, 984 .output = snd_usbmidi_us122l_output, 985 }; 986 987 /* 988 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching. 989 */ 990 991 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint *ep) 992 { 993 static const u8 init_data[] = { 994 /* initialization magic: "get version" */ 995 0xf0, 996 0x00, 0x20, 0x31, /* Emagic */ 997 0x64, /* Unitor8 */ 998 0x0b, /* version number request */ 999 0x00, /* command version */ 1000 0x00, /* EEPROM, box 0 */ 1001 0xf7 1002 }; 1003 send_bulk_static_data(ep, init_data, sizeof(init_data)); 1004 /* while we're at it, pour on more magic */ 1005 send_bulk_static_data(ep, init_data, sizeof(init_data)); 1006 } 1007 1008 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint *ep) 1009 { 1010 static const u8 finish_data[] = { 1011 /* switch to patch mode with last preset */ 1012 0xf0, 1013 0x00, 0x20, 0x31, /* Emagic */ 1014 0x64, /* Unitor8 */ 1015 0x10, /* patch switch command */ 1016 0x00, /* command version */ 1017 0x7f, /* to all boxes */ 1018 0x40, /* last preset in EEPROM */ 1019 0xf7 1020 }; 1021 send_bulk_static_data(ep, finish_data, sizeof(finish_data)); 1022 } 1023 1024 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint *ep, 1025 uint8_t *buffer, int buffer_length) 1026 { 1027 int i; 1028 1029 /* FF indicates end of valid data */ 1030 for (i = 0; i < buffer_length; ++i) 1031 if (buffer[i] == 0xff) { 1032 buffer_length = i; 1033 break; 1034 } 1035 1036 /* handle F5 at end of last buffer */ 1037 if (ep->seen_f5) 1038 goto switch_port; 1039 1040 while (buffer_length > 0) { 1041 /* determine size of data until next F5 */ 1042 for (i = 0; i < buffer_length; ++i) 1043 if (buffer[i] == 0xf5) 1044 break; 1045 snd_usbmidi_input_data(ep, ep->current_port, buffer, i); 1046 buffer += i; 1047 buffer_length -= i; 1048 1049 if (buffer_length <= 0) 1050 break; 1051 /* assert(buffer[0] == 0xf5); */ 1052 ep->seen_f5 = 1; 1053 ++buffer; 1054 --buffer_length; 1055 1056 switch_port: 1057 if (buffer_length <= 0) 1058 break; 1059 if (buffer[0] < 0x80) { 1060 ep->current_port = (buffer[0] - 1) & 15; 1061 ++buffer; 1062 --buffer_length; 1063 } 1064 ep->seen_f5 = 0; 1065 } 1066 } 1067 1068 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint *ep, 1069 struct urb *urb) 1070 { 1071 int port0 = ep->current_port; 1072 uint8_t *buf = urb->transfer_buffer; 1073 int buf_free = ep->max_transfer; 1074 int length, i; 1075 1076 for (i = 0; i < 0x10; ++i) { 1077 /* round-robin, starting at the last current port */ 1078 int portnum = (port0 + i) & 15; 1079 struct usbmidi_out_port *port = &ep->ports[portnum]; 1080 1081 if (!port->active) 1082 continue; 1083 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) { 1084 port->active = 0; 1085 continue; 1086 } 1087 1088 if (portnum != ep->current_port) { 1089 if (buf_free < 2) 1090 break; 1091 ep->current_port = portnum; 1092 buf[0] = 0xf5; 1093 buf[1] = (portnum + 1) & 15; 1094 buf += 2; 1095 buf_free -= 2; 1096 } 1097 1098 if (buf_free < 1) 1099 break; 1100 length = snd_rawmidi_transmit(port->substream, buf, buf_free); 1101 if (length > 0) { 1102 buf += length; 1103 buf_free -= length; 1104 if (buf_free < 1) 1105 break; 1106 } 1107 } 1108 if (buf_free < ep->max_transfer && buf_free > 0) { 1109 *buf = 0xff; 1110 --buf_free; 1111 } 1112 urb->transfer_buffer_length = ep->max_transfer - buf_free; 1113 } 1114 1115 static const struct usb_protocol_ops snd_usbmidi_emagic_ops = { 1116 .input = snd_usbmidi_emagic_input, 1117 .output = snd_usbmidi_emagic_output, 1118 .init_out_endpoint = snd_usbmidi_emagic_init_out, 1119 .finish_out_endpoint = snd_usbmidi_emagic_finish_out, 1120 }; 1121 1122 1123 static void update_roland_altsetting(struct snd_usb_midi *umidi) 1124 { 1125 struct usb_interface *intf; 1126 struct usb_host_interface *hostif; 1127 struct usb_interface_descriptor *intfd; 1128 int is_light_load; 1129 1130 intf = umidi->iface; 1131 is_light_load = intf->cur_altsetting != intf->altsetting; 1132 if (umidi->roland_load_ctl->private_value == is_light_load) 1133 return; 1134 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value]; 1135 intfd = get_iface_desc(hostif); 1136 snd_usbmidi_input_stop(&umidi->list); 1137 usb_set_interface(umidi->dev, intfd->bInterfaceNumber, 1138 intfd->bAlternateSetting); 1139 snd_usbmidi_input_start(&umidi->list); 1140 } 1141 1142 static int substream_open(struct snd_rawmidi_substream *substream, int dir, 1143 int open) 1144 { 1145 struct snd_usb_midi *umidi = substream->rmidi->private_data; 1146 struct snd_kcontrol *ctl; 1147 1148 guard(rwsem_read)(&umidi->disc_rwsem); 1149 if (umidi->disconnected) 1150 return open ? -ENODEV : 0; 1151 1152 guard(mutex)(&umidi->mutex); 1153 if (open) { 1154 if (!umidi->opened[0] && !umidi->opened[1]) { 1155 if (umidi->roland_load_ctl) { 1156 ctl = umidi->roland_load_ctl; 1157 ctl->vd[0].access |= 1158 SNDRV_CTL_ELEM_ACCESS_INACTIVE; 1159 snd_ctl_notify(umidi->card, 1160 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); 1161 update_roland_altsetting(umidi); 1162 } 1163 } 1164 umidi->opened[dir]++; 1165 if (umidi->opened[1]) 1166 snd_usbmidi_input_start(&umidi->list); 1167 } else { 1168 umidi->opened[dir]--; 1169 if (!umidi->opened[1]) 1170 snd_usbmidi_input_stop(&umidi->list); 1171 if (!umidi->opened[0] && !umidi->opened[1]) { 1172 if (umidi->roland_load_ctl) { 1173 ctl = umidi->roland_load_ctl; 1174 ctl->vd[0].access &= 1175 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; 1176 snd_ctl_notify(umidi->card, 1177 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); 1178 } 1179 } 1180 } 1181 return 0; 1182 } 1183 1184 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream) 1185 { 1186 struct snd_usb_midi *umidi = substream->rmidi->private_data; 1187 struct usbmidi_out_port *port = NULL; 1188 int i, j; 1189 1190 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) 1191 if (umidi->endpoints[i].out) 1192 for (j = 0; j < 0x10; ++j) 1193 if (umidi->endpoints[i].out->ports[j].substream == substream) { 1194 port = &umidi->endpoints[i].out->ports[j]; 1195 break; 1196 } 1197 if (!port) 1198 return -ENXIO; 1199 1200 substream->runtime->private_data = port; 1201 port->state = STATE_UNKNOWN; 1202 return substream_open(substream, 0, 1); 1203 } 1204 1205 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream) 1206 { 1207 struct usbmidi_out_port *port = substream->runtime->private_data; 1208 1209 flush_work(&port->ep->work); 1210 return substream_open(substream, 0, 0); 1211 } 1212 1213 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, 1214 int up) 1215 { 1216 struct usbmidi_out_port *port = 1217 (struct usbmidi_out_port *)substream->runtime->private_data; 1218 1219 port->active = up; 1220 if (up) { 1221 if (port->ep->umidi->disconnected) { 1222 /* gobble up remaining bytes to prevent wait in 1223 * snd_rawmidi_drain_output */ 1224 snd_rawmidi_proceed(substream); 1225 return; 1226 } 1227 queue_work(system_highpri_wq, &port->ep->work); 1228 } 1229 } 1230 1231 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream) 1232 { 1233 struct usbmidi_out_port *port = substream->runtime->private_data; 1234 struct snd_usb_midi_out_endpoint *ep = port->ep; 1235 unsigned int drain_urbs; 1236 DEFINE_WAIT(wait); 1237 long timeout = msecs_to_jiffies(50); 1238 1239 if (ep->umidi->disconnected) 1240 return; 1241 /* 1242 * The substream buffer is empty, but some data might still be in the 1243 * currently active URBs, so we have to wait for those to complete. 1244 */ 1245 spin_lock_irq(&ep->buffer_lock); 1246 drain_urbs = ep->active_urbs; 1247 if (drain_urbs) { 1248 ep->drain_urbs |= drain_urbs; 1249 do { 1250 prepare_to_wait(&ep->drain_wait, &wait, 1251 TASK_UNINTERRUPTIBLE); 1252 spin_unlock_irq(&ep->buffer_lock); 1253 timeout = schedule_timeout(timeout); 1254 spin_lock_irq(&ep->buffer_lock); 1255 drain_urbs &= ep->drain_urbs; 1256 } while (drain_urbs && timeout); 1257 finish_wait(&ep->drain_wait, &wait); 1258 } 1259 port->active = 0; 1260 spin_unlock_irq(&ep->buffer_lock); 1261 } 1262 1263 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream) 1264 { 1265 return substream_open(substream, 1, 1); 1266 } 1267 1268 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream) 1269 { 1270 return substream_open(substream, 1, 0); 1271 } 1272 1273 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, 1274 int up) 1275 { 1276 struct snd_usb_midi *umidi = substream->rmidi->private_data; 1277 1278 if (up) 1279 set_bit(substream->number, &umidi->input_triggered); 1280 else 1281 clear_bit(substream->number, &umidi->input_triggered); 1282 } 1283 1284 static const struct snd_rawmidi_ops snd_usbmidi_output_ops = { 1285 .open = snd_usbmidi_output_open, 1286 .close = snd_usbmidi_output_close, 1287 .trigger = snd_usbmidi_output_trigger, 1288 .drain = snd_usbmidi_output_drain, 1289 }; 1290 1291 static const struct snd_rawmidi_ops snd_usbmidi_input_ops = { 1292 .open = snd_usbmidi_input_open, 1293 .close = snd_usbmidi_input_close, 1294 .trigger = snd_usbmidi_input_trigger 1295 }; 1296 1297 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb, 1298 unsigned int buffer_length) 1299 { 1300 usb_free_coherent(umidi->dev, buffer_length, 1301 urb->transfer_buffer, urb->transfer_dma); 1302 usb_free_urb(urb); 1303 } 1304 1305 /* 1306 * Frees an input endpoint. 1307 * May be called when ep hasn't been initialized completely. 1308 */ 1309 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint *ep) 1310 { 1311 unsigned int i; 1312 1313 for (i = 0; i < INPUT_URBS; ++i) 1314 if (ep->urbs[i]) 1315 free_urb_and_buffer(ep->umidi, ep->urbs[i], 1316 ep->urbs[i]->transfer_buffer_length); 1317 kfree(ep); 1318 } 1319 1320 /* 1321 * Creates an input endpoint. 1322 */ 1323 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi *umidi, 1324 struct snd_usb_midi_endpoint_info *ep_info, 1325 struct snd_usb_midi_endpoint *rep) 1326 { 1327 struct snd_usb_midi_in_endpoint *ep; 1328 void *buffer; 1329 unsigned int pipe; 1330 int length; 1331 unsigned int i; 1332 int err; 1333 1334 rep->in = NULL; 1335 ep = kzalloc_obj(*ep); 1336 if (!ep) 1337 return -ENOMEM; 1338 ep->umidi = umidi; 1339 1340 for (i = 0; i < INPUT_URBS; ++i) { 1341 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL); 1342 if (!ep->urbs[i]) { 1343 err = -ENOMEM; 1344 goto error; 1345 } 1346 } 1347 if (ep_info->in_interval) 1348 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep); 1349 else 1350 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep); 1351 length = usb_maxpacket(umidi->dev, pipe); 1352 for (i = 0; i < INPUT_URBS; ++i) { 1353 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL, 1354 &ep->urbs[i]->transfer_dma); 1355 if (!buffer) { 1356 err = -ENOMEM; 1357 goto error; 1358 } 1359 if (ep_info->in_interval) 1360 usb_fill_int_urb(ep->urbs[i], umidi->dev, 1361 pipe, buffer, length, 1362 snd_usbmidi_in_urb_complete, 1363 ep, ep_info->in_interval); 1364 else 1365 usb_fill_bulk_urb(ep->urbs[i], umidi->dev, 1366 pipe, buffer, length, 1367 snd_usbmidi_in_urb_complete, ep); 1368 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 1369 err = usb_urb_ep_type_check(ep->urbs[i]); 1370 if (err < 0) { 1371 dev_err(&umidi->dev->dev, "invalid MIDI in EP %x\n", 1372 ep_info->in_ep); 1373 goto error; 1374 } 1375 } 1376 1377 rep->in = ep; 1378 return 0; 1379 1380 error: 1381 snd_usbmidi_in_endpoint_delete(ep); 1382 return err; 1383 } 1384 1385 /* 1386 * Frees an output endpoint. 1387 * May be called when ep hasn't been initialized completely. 1388 */ 1389 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep) 1390 { 1391 unsigned int i; 1392 1393 for (i = 0; i < OUTPUT_URBS; ++i) 1394 if (ep->urbs[i].urb) { 1395 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb, 1396 ep->max_transfer); 1397 ep->urbs[i].urb = NULL; 1398 } 1399 } 1400 1401 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep) 1402 { 1403 snd_usbmidi_out_endpoint_clear(ep); 1404 kfree(ep); 1405 } 1406 1407 /* 1408 * Creates an output endpoint, and initializes output ports. 1409 */ 1410 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi *umidi, 1411 struct snd_usb_midi_endpoint_info *ep_info, 1412 struct snd_usb_midi_endpoint *rep) 1413 { 1414 struct snd_usb_midi_out_endpoint *ep; 1415 unsigned int i; 1416 unsigned int pipe; 1417 void *buffer; 1418 int err; 1419 1420 rep->out = NULL; 1421 ep = kzalloc_obj(*ep); 1422 if (!ep) 1423 return -ENOMEM; 1424 ep->umidi = umidi; 1425 1426 for (i = 0; i < OUTPUT_URBS; ++i) { 1427 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL); 1428 if (!ep->urbs[i].urb) { 1429 err = -ENOMEM; 1430 goto error; 1431 } 1432 ep->urbs[i].ep = ep; 1433 } 1434 if (ep_info->out_interval) 1435 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep); 1436 else 1437 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep); 1438 switch (umidi->usb_id) { 1439 default: 1440 ep->max_transfer = usb_maxpacket(umidi->dev, pipe); 1441 break; 1442 /* 1443 * Various chips declare a packet size larger than 4 bytes, but 1444 * do not actually work with larger packets: 1445 */ 1446 case USB_ID(0x0a67, 0x5011): /* Medeli DD305 */ 1447 case USB_ID(0x0a92, 0x1020): /* ESI M4U */ 1448 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */ 1449 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */ 1450 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */ 1451 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */ 1452 case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */ 1453 ep->max_transfer = 4; 1454 break; 1455 /* 1456 * Some devices only work with 9 bytes packet size: 1457 */ 1458 case USB_ID(0x0644, 0x800e): /* Tascam US-122L */ 1459 case USB_ID(0x0644, 0x800f): /* Tascam US-144 */ 1460 ep->max_transfer = 9; 1461 break; 1462 } 1463 for (i = 0; i < OUTPUT_URBS; ++i) { 1464 buffer = usb_alloc_coherent(umidi->dev, 1465 ep->max_transfer, GFP_KERNEL, 1466 &ep->urbs[i].urb->transfer_dma); 1467 if (!buffer) { 1468 err = -ENOMEM; 1469 goto error; 1470 } 1471 if (ep_info->out_interval) 1472 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev, 1473 pipe, buffer, ep->max_transfer, 1474 snd_usbmidi_out_urb_complete, 1475 &ep->urbs[i], ep_info->out_interval); 1476 else 1477 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev, 1478 pipe, buffer, ep->max_transfer, 1479 snd_usbmidi_out_urb_complete, 1480 &ep->urbs[i]); 1481 err = usb_urb_ep_type_check(ep->urbs[i].urb); 1482 if (err < 0) { 1483 dev_err(&umidi->dev->dev, "invalid MIDI out EP %x\n", 1484 ep_info->out_ep); 1485 goto error; 1486 } 1487 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 1488 } 1489 1490 spin_lock_init(&ep->buffer_lock); 1491 INIT_WORK(&ep->work, snd_usbmidi_out_work); 1492 init_waitqueue_head(&ep->drain_wait); 1493 1494 for (i = 0; i < 0x10; ++i) 1495 if (ep_info->out_cables & (1 << i)) { 1496 ep->ports[i].ep = ep; 1497 ep->ports[i].cable = i << 4; 1498 } 1499 1500 if (umidi->usb_protocol_ops->init_out_endpoint) 1501 umidi->usb_protocol_ops->init_out_endpoint(ep); 1502 1503 rep->out = ep; 1504 return 0; 1505 1506 error: 1507 snd_usbmidi_out_endpoint_delete(ep); 1508 return err; 1509 } 1510 1511 /* 1512 * Frees everything. 1513 */ 1514 static void snd_usbmidi_free(struct snd_usb_midi *umidi) 1515 { 1516 int i; 1517 1518 if (!umidi->disconnected) 1519 snd_usbmidi_disconnect(&umidi->list); 1520 1521 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1522 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i]; 1523 kfree(ep->out); 1524 } 1525 mutex_destroy(&umidi->mutex); 1526 kfree(umidi); 1527 } 1528 1529 /* 1530 * Unlinks all URBs (must be done before the usb_device is deleted). 1531 */ 1532 void snd_usbmidi_disconnect(struct list_head *p) 1533 { 1534 struct snd_usb_midi *umidi; 1535 unsigned int i, j; 1536 1537 umidi = list_entry(p, struct snd_usb_midi, list); 1538 /* 1539 * an URB's completion handler may start the timer and 1540 * a timer may submit an URB. To reliably break the cycle 1541 * a flag under lock must be used 1542 */ 1543 scoped_guard(rwsem_write, &umidi->disc_rwsem) { 1544 guard(spinlock_irq)(&umidi->disc_lock); 1545 umidi->disconnected = 1; 1546 } 1547 1548 timer_shutdown_sync(&umidi->error_timer); 1549 1550 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1551 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i]; 1552 if (ep->out) 1553 cancel_work_sync(&ep->out->work); 1554 if (ep->out) { 1555 for (j = 0; j < OUTPUT_URBS; ++j) 1556 usb_kill_urb(ep->out->urbs[j].urb); 1557 if (umidi->usb_protocol_ops->finish_out_endpoint) 1558 umidi->usb_protocol_ops->finish_out_endpoint(ep->out); 1559 ep->out->active_urbs = 0; 1560 if (ep->out->drain_urbs) { 1561 ep->out->drain_urbs = 0; 1562 wake_up(&ep->out->drain_wait); 1563 } 1564 } 1565 if (ep->in) 1566 for (j = 0; j < INPUT_URBS; ++j) 1567 usb_kill_urb(ep->in->urbs[j]); 1568 /* free endpoints here; later call can result in Oops */ 1569 if (ep->out) 1570 snd_usbmidi_out_endpoint_clear(ep->out); 1571 if (ep->in) { 1572 snd_usbmidi_in_endpoint_delete(ep->in); 1573 ep->in = NULL; 1574 } 1575 } 1576 } 1577 EXPORT_SYMBOL(snd_usbmidi_disconnect); 1578 1579 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi) 1580 { 1581 struct snd_usb_midi *umidi = rmidi->private_data; 1582 snd_usbmidi_free(umidi); 1583 } 1584 1585 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi *umidi, 1586 int stream, 1587 int number) 1588 { 1589 struct snd_rawmidi_substream *substream; 1590 1591 list_for_each_entry(substream, &umidi->rmidi->streams[stream].substreams, 1592 list) { 1593 if (substream->number == number) 1594 return substream; 1595 } 1596 return NULL; 1597 } 1598 1599 /* 1600 * This list specifies names for ports that do not fit into the standard 1601 * "(product) MIDI (n)" schema because they aren't external MIDI ports, 1602 * such as internal control or synthesizer ports. 1603 */ 1604 static struct port_info { 1605 u32 id; 1606 short int port; 1607 short int voices; 1608 const char *name; 1609 unsigned int seq_flags; 1610 } snd_usbmidi_port_info[] = { 1611 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \ 1612 { .id = USB_ID(vendor, product), \ 1613 .port = num, .voices = voices_, \ 1614 .name = name_, .seq_flags = flags } 1615 #define EXTERNAL_PORT(vendor, product, num, name) \ 1616 PORT_INFO(vendor, product, num, name, 0, \ 1617 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1618 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1619 SNDRV_SEQ_PORT_TYPE_PORT) 1620 #define CONTROL_PORT(vendor, product, num, name) \ 1621 PORT_INFO(vendor, product, num, name, 0, \ 1622 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1623 SNDRV_SEQ_PORT_TYPE_HARDWARE) 1624 #define GM_SYNTH_PORT(vendor, product, num, name, voices) \ 1625 PORT_INFO(vendor, product, num, name, voices, \ 1626 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1627 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ 1628 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1629 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) 1630 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \ 1631 PORT_INFO(vendor, product, num, name, voices, \ 1632 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1633 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ 1634 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ 1635 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ 1636 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ 1637 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1638 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) 1639 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \ 1640 PORT_INFO(vendor, product, num, name, voices, \ 1641 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1642 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ 1643 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ 1644 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ 1645 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ 1646 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \ 1647 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1648 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) 1649 /* Yamaha MOTIF XF */ 1650 GM_SYNTH_PORT(0x0499, 0x105c, 0, "%s Tone Generator", 128), 1651 CONTROL_PORT(0x0499, 0x105c, 1, "%s Remote Control"), 1652 EXTERNAL_PORT(0x0499, 0x105c, 2, "%s Thru"), 1653 CONTROL_PORT(0x0499, 0x105c, 3, "%s Editor"), 1654 /* Roland UA-100 */ 1655 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"), 1656 /* Roland SC-8850 */ 1657 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128), 1658 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128), 1659 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128), 1660 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128), 1661 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"), 1662 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"), 1663 /* Roland U-8 */ 1664 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"), 1665 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"), 1666 /* Roland SC-8820 */ 1667 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64), 1668 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64), 1669 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"), 1670 /* Roland SK-500 */ 1671 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64), 1672 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64), 1673 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"), 1674 /* Roland SC-D70 */ 1675 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64), 1676 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64), 1677 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"), 1678 /* Edirol UM-880 */ 1679 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"), 1680 /* Edirol SD-90 */ 1681 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128), 1682 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128), 1683 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"), 1684 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"), 1685 /* Edirol UM-550 */ 1686 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"), 1687 /* Edirol SD-20 */ 1688 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64), 1689 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64), 1690 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"), 1691 /* Edirol SD-80 */ 1692 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128), 1693 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128), 1694 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"), 1695 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"), 1696 /* Edirol UA-700 */ 1697 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"), 1698 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"), 1699 /* Roland VariOS */ 1700 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"), 1701 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"), 1702 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"), 1703 /* Edirol PCR */ 1704 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"), 1705 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"), 1706 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"), 1707 /* BOSS GS-10 */ 1708 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"), 1709 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"), 1710 /* Edirol UA-1000 */ 1711 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"), 1712 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"), 1713 /* Edirol UR-80 */ 1714 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"), 1715 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"), 1716 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"), 1717 /* Edirol PCR-A */ 1718 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"), 1719 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"), 1720 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"), 1721 /* BOSS GT-PRO */ 1722 CONTROL_PORT(0x0582, 0x0089, 0, "%s Control"), 1723 /* Edirol UM-3EX */ 1724 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"), 1725 /* Roland VG-99 */ 1726 CONTROL_PORT(0x0582, 0x00b2, 0, "%s Control"), 1727 EXTERNAL_PORT(0x0582, 0x00b2, 1, "%s MIDI"), 1728 /* Cakewalk Sonar V-Studio 100 */ 1729 EXTERNAL_PORT(0x0582, 0x00eb, 0, "%s MIDI"), 1730 CONTROL_PORT(0x0582, 0x00eb, 1, "%s Control"), 1731 /* Roland VB-99 */ 1732 CONTROL_PORT(0x0582, 0x0102, 0, "%s Control"), 1733 EXTERNAL_PORT(0x0582, 0x0102, 1, "%s MIDI"), 1734 /* Roland A-PRO */ 1735 EXTERNAL_PORT(0x0582, 0x010f, 0, "%s MIDI"), 1736 CONTROL_PORT(0x0582, 0x010f, 1, "%s 1"), 1737 CONTROL_PORT(0x0582, 0x010f, 2, "%s 2"), 1738 /* Roland SD-50 */ 1739 ROLAND_SYNTH_PORT(0x0582, 0x0114, 0, "%s Synth", 128), 1740 EXTERNAL_PORT(0x0582, 0x0114, 1, "%s MIDI"), 1741 CONTROL_PORT(0x0582, 0x0114, 2, "%s Control"), 1742 /* Roland OCTA-CAPTURE */ 1743 EXTERNAL_PORT(0x0582, 0x0120, 0, "%s MIDI"), 1744 CONTROL_PORT(0x0582, 0x0120, 1, "%s Control"), 1745 EXTERNAL_PORT(0x0582, 0x0121, 0, "%s MIDI"), 1746 CONTROL_PORT(0x0582, 0x0121, 1, "%s Control"), 1747 /* Roland SPD-SX */ 1748 CONTROL_PORT(0x0582, 0x0145, 0, "%s Control"), 1749 EXTERNAL_PORT(0x0582, 0x0145, 1, "%s MIDI"), 1750 /* Roland A-Series */ 1751 CONTROL_PORT(0x0582, 0x0156, 0, "%s Keyboard"), 1752 EXTERNAL_PORT(0x0582, 0x0156, 1, "%s MIDI"), 1753 /* Roland INTEGRA-7 */ 1754 ROLAND_SYNTH_PORT(0x0582, 0x015b, 0, "%s Synth", 128), 1755 CONTROL_PORT(0x0582, 0x015b, 1, "%s Control"), 1756 /* M-Audio MidiSport 8x8 */ 1757 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"), 1758 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"), 1759 /* MOTU Fastlane */ 1760 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"), 1761 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"), 1762 /* Emagic Unitor8/AMT8/MT4 */ 1763 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"), 1764 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"), 1765 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"), 1766 /* Akai MPD16 */ 1767 CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"), 1768 PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0, 1769 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | 1770 SNDRV_SEQ_PORT_TYPE_HARDWARE), 1771 /* Access Music Virus TI */ 1772 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"), 1773 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0, 1774 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | 1775 SNDRV_SEQ_PORT_TYPE_HARDWARE | 1776 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER), 1777 }; 1778 1779 static struct port_info *find_port_info(struct snd_usb_midi *umidi, int number) 1780 { 1781 int i; 1782 1783 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) { 1784 if (snd_usbmidi_port_info[i].id == umidi->usb_id && 1785 snd_usbmidi_port_info[i].port == number) 1786 return &snd_usbmidi_port_info[i]; 1787 } 1788 return NULL; 1789 } 1790 1791 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number, 1792 struct snd_seq_port_info *seq_port_info) 1793 { 1794 struct snd_usb_midi *umidi = rmidi->private_data; 1795 struct port_info *port_info; 1796 1797 /* TODO: read port flags from descriptors */ 1798 port_info = find_port_info(umidi, number); 1799 if (port_info) { 1800 seq_port_info->type = port_info->seq_flags; 1801 seq_port_info->midi_voices = port_info->voices; 1802 } 1803 } 1804 1805 /* return iJack for the corresponding jackID */ 1806 static int find_usb_ijack(struct usb_host_interface *hostif, uint8_t jack_id) 1807 { 1808 unsigned char *extra = hostif->extra; 1809 int extralen = hostif->extralen; 1810 struct usb_descriptor_header *h; 1811 struct usb_midi_out_jack_descriptor *outjd; 1812 struct usb_midi_in_jack_descriptor *injd; 1813 size_t sz; 1814 1815 while (extralen > 4) { 1816 h = (struct usb_descriptor_header *)extra; 1817 if (h->bDescriptorType != USB_DT_CS_INTERFACE) 1818 goto next; 1819 1820 outjd = (struct usb_midi_out_jack_descriptor *)h; 1821 if (h->bLength >= sizeof(*outjd) && 1822 outjd->bDescriptorSubtype == UAC_MIDI_OUT_JACK && 1823 outjd->bJackID == jack_id) { 1824 sz = USB_DT_MIDI_OUT_SIZE(outjd->bNrInputPins); 1825 if (outjd->bLength < sz) 1826 goto next; 1827 return *(extra + sz - 1); 1828 } 1829 1830 injd = (struct usb_midi_in_jack_descriptor *)h; 1831 if (injd->bLength >= sizeof(*injd) && 1832 injd->bDescriptorSubtype == UAC_MIDI_IN_JACK && 1833 injd->bJackID == jack_id) 1834 return injd->iJack; 1835 1836 next: 1837 if (!extra[0]) 1838 break; 1839 extralen -= extra[0]; 1840 extra += extra[0]; 1841 } 1842 return 0; 1843 } 1844 1845 static void snd_usbmidi_init_substream(struct snd_usb_midi *umidi, 1846 int stream, int number, int jack_id, 1847 struct snd_rawmidi_substream **rsubstream) 1848 { 1849 struct port_info *port_info; 1850 const char *name_format; 1851 struct usb_interface *intf; 1852 struct usb_host_interface *hostif; 1853 uint8_t jack_name_buf[32]; 1854 uint8_t *default_jack_name = "MIDI"; 1855 uint8_t *jack_name = default_jack_name; 1856 uint8_t iJack; 1857 int res; 1858 1859 struct snd_rawmidi_substream *substream = 1860 snd_usbmidi_find_substream(umidi, stream, number); 1861 if (!substream) { 1862 dev_err(&umidi->dev->dev, "substream %d:%d not found\n", stream, 1863 number); 1864 return; 1865 } 1866 1867 intf = umidi->iface; 1868 if (intf && jack_id >= 0) { 1869 hostif = intf->cur_altsetting; 1870 iJack = find_usb_ijack(hostif, jack_id); 1871 if (iJack != 0) { 1872 res = usb_string(umidi->dev, iJack, jack_name_buf, 1873 ARRAY_SIZE(jack_name_buf)); 1874 if (res) 1875 jack_name = jack_name_buf; 1876 } 1877 } 1878 1879 port_info = find_port_info(umidi, number); 1880 if (port_info || jack_name == default_jack_name || 1881 strncmp(umidi->card->shortname, jack_name, strlen(umidi->card->shortname)) != 0) { 1882 name_format = port_info ? port_info->name : 1883 (jack_name != default_jack_name ? "%s %s" : "%s %s %d"); 1884 snprintf(substream->name, sizeof(substream->name), 1885 name_format, umidi->card->shortname, jack_name, number + 1); 1886 } else { 1887 /* The manufacturer included the iProduct name in the jack 1888 * name, do not use both 1889 */ 1890 strscpy(substream->name, jack_name); 1891 } 1892 1893 *rsubstream = substream; 1894 } 1895 1896 /* 1897 * Creates the endpoints and their ports. 1898 */ 1899 static int snd_usbmidi_create_endpoints(struct snd_usb_midi *umidi, 1900 struct snd_usb_midi_endpoint_info *endpoints) 1901 { 1902 int i, j, err; 1903 int out_ports = 0, in_ports = 0; 1904 1905 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1906 if (endpoints[i].out_cables) { 1907 err = snd_usbmidi_out_endpoint_create(umidi, 1908 &endpoints[i], 1909 &umidi->endpoints[i]); 1910 if (err < 0) 1911 return err; 1912 } 1913 if (endpoints[i].in_cables) { 1914 err = snd_usbmidi_in_endpoint_create(umidi, 1915 &endpoints[i], 1916 &umidi->endpoints[i]); 1917 if (err < 0) 1918 return err; 1919 } 1920 1921 for (j = 0; j < 0x10; ++j) { 1922 if (endpoints[i].out_cables & (1 << j)) { 1923 snd_usbmidi_init_substream(umidi, 1924 SNDRV_RAWMIDI_STREAM_OUTPUT, 1925 out_ports, 1926 endpoints[i].assoc_out_jacks[j], 1927 &umidi->endpoints[i].out->ports[j].substream); 1928 ++out_ports; 1929 } 1930 if (endpoints[i].in_cables & (1 << j)) { 1931 snd_usbmidi_init_substream(umidi, 1932 SNDRV_RAWMIDI_STREAM_INPUT, 1933 in_ports, 1934 endpoints[i].assoc_in_jacks[j], 1935 &umidi->endpoints[i].in->ports[j].substream); 1936 ++in_ports; 1937 } 1938 } 1939 } 1940 dev_dbg(&umidi->dev->dev, "created %d output and %d input ports\n", 1941 out_ports, in_ports); 1942 return 0; 1943 } 1944 1945 static struct usb_ms_endpoint_descriptor *find_usb_ms_endpoint_descriptor( 1946 struct usb_host_endpoint *hostep) 1947 { 1948 unsigned char *extra = hostep->extra; 1949 int extralen = hostep->extralen; 1950 1951 while (extralen > 3) { 1952 struct usb_ms_endpoint_descriptor *ms_ep = 1953 (struct usb_ms_endpoint_descriptor *)extra; 1954 1955 if (ms_ep->bLength > 3 && 1956 ms_ep->bDescriptorType == USB_DT_CS_ENDPOINT && 1957 ms_ep->bDescriptorSubtype == UAC_MS_GENERAL) 1958 return ms_ep; 1959 if (!extra[0]) 1960 break; 1961 extralen -= extra[0]; 1962 extra += extra[0]; 1963 } 1964 return NULL; 1965 } 1966 1967 /* 1968 * Returns MIDIStreaming device capabilities. 1969 */ 1970 static int snd_usbmidi_get_ms_info(struct snd_usb_midi *umidi, 1971 struct snd_usb_midi_endpoint_info *endpoints) 1972 { 1973 struct usb_interface *intf; 1974 struct usb_host_interface *hostif; 1975 struct usb_interface_descriptor *intfd; 1976 struct usb_ms_header_descriptor *ms_header; 1977 struct usb_host_endpoint *hostep; 1978 struct usb_endpoint_descriptor *ep; 1979 struct usb_ms_endpoint_descriptor *ms_ep; 1980 int i, j, epidx; 1981 1982 intf = umidi->iface; 1983 if (!intf) 1984 return -ENXIO; 1985 hostif = &intf->altsetting[0]; 1986 intfd = get_iface_desc(hostif); 1987 ms_header = (struct usb_ms_header_descriptor *)hostif->extra; 1988 if (hostif->extralen >= 7 && 1989 ms_header->bLength >= 7 && 1990 ms_header->bDescriptorType == USB_DT_CS_INTERFACE && 1991 ms_header->bDescriptorSubtype == UAC_HEADER) 1992 dev_dbg(&umidi->dev->dev, "MIDIStreaming version %02x.%02x\n", 1993 ((uint8_t *)&ms_header->bcdMSC)[1], ((uint8_t *)&ms_header->bcdMSC)[0]); 1994 else 1995 dev_warn(&umidi->dev->dev, 1996 "MIDIStreaming interface descriptor not found\n"); 1997 1998 epidx = 0; 1999 for (i = 0; i < intfd->bNumEndpoints; ++i) { 2000 hostep = &hostif->endpoint[i]; 2001 ep = get_ep_desc(hostep); 2002 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep)) 2003 continue; 2004 ms_ep = find_usb_ms_endpoint_descriptor(hostep); 2005 if (!ms_ep) 2006 continue; 2007 if (ms_ep->bLength <= sizeof(*ms_ep)) 2008 continue; 2009 if (ms_ep->bNumEmbMIDIJack > 0x10) 2010 continue; 2011 if (ms_ep->bLength < sizeof(*ms_ep) + ms_ep->bNumEmbMIDIJack) 2012 continue; 2013 if (usb_endpoint_dir_out(ep)) { 2014 if (endpoints[epidx].out_ep) { 2015 if (++epidx >= MIDI_MAX_ENDPOINTS) { 2016 dev_warn(&umidi->dev->dev, 2017 "too many endpoints\n"); 2018 break; 2019 } 2020 } 2021 endpoints[epidx].out_ep = usb_endpoint_num(ep); 2022 if (usb_endpoint_xfer_int(ep)) 2023 endpoints[epidx].out_interval = ep->bInterval; 2024 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) 2025 /* 2026 * Low speed bulk transfers don't exist, so 2027 * force interrupt transfers for devices like 2028 * ESI MIDI Mate that try to use them anyway. 2029 */ 2030 endpoints[epidx].out_interval = 1; 2031 endpoints[epidx].out_cables = 2032 (1 << ms_ep->bNumEmbMIDIJack) - 1; 2033 for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j) 2034 endpoints[epidx].assoc_out_jacks[j] = ms_ep->baAssocJackID[j]; 2035 for (; j < ARRAY_SIZE(endpoints[epidx].assoc_out_jacks); ++j) 2036 endpoints[epidx].assoc_out_jacks[j] = -1; 2037 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n", 2038 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); 2039 } else { 2040 if (endpoints[epidx].in_ep) { 2041 if (++epidx >= MIDI_MAX_ENDPOINTS) { 2042 dev_warn(&umidi->dev->dev, 2043 "too many endpoints\n"); 2044 break; 2045 } 2046 } 2047 endpoints[epidx].in_ep = usb_endpoint_num(ep); 2048 if (usb_endpoint_xfer_int(ep)) 2049 endpoints[epidx].in_interval = ep->bInterval; 2050 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) 2051 endpoints[epidx].in_interval = 1; 2052 endpoints[epidx].in_cables = 2053 (1 << ms_ep->bNumEmbMIDIJack) - 1; 2054 for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j) 2055 endpoints[epidx].assoc_in_jacks[j] = ms_ep->baAssocJackID[j]; 2056 for (; j < ARRAY_SIZE(endpoints[epidx].assoc_in_jacks); ++j) 2057 endpoints[epidx].assoc_in_jacks[j] = -1; 2058 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n", 2059 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); 2060 } 2061 } 2062 return 0; 2063 } 2064 2065 static int roland_load_info(struct snd_kcontrol *kcontrol, 2066 struct snd_ctl_elem_info *info) 2067 { 2068 static const char *const names[] = { "High Load", "Light Load" }; 2069 2070 return snd_ctl_enum_info(info, 1, 2, names); 2071 } 2072 2073 static int roland_load_get(struct snd_kcontrol *kcontrol, 2074 struct snd_ctl_elem_value *value) 2075 { 2076 value->value.enumerated.item[0] = kcontrol->private_value; 2077 return 0; 2078 } 2079 2080 static int roland_load_put(struct snd_kcontrol *kcontrol, 2081 struct snd_ctl_elem_value *value) 2082 { 2083 struct snd_usb_midi *umidi = snd_kcontrol_chip(kcontrol); 2084 int changed; 2085 2086 if (value->value.enumerated.item[0] > 1) 2087 return -EINVAL; 2088 guard(mutex)(&umidi->mutex); 2089 changed = value->value.enumerated.item[0] != kcontrol->private_value; 2090 if (changed) 2091 kcontrol->private_value = value->value.enumerated.item[0]; 2092 return changed; 2093 } 2094 2095 static const struct snd_kcontrol_new roland_load_ctl = { 2096 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2097 .name = "MIDI Input Mode", 2098 .info = roland_load_info, 2099 .get = roland_load_get, 2100 .put = roland_load_put, 2101 .private_value = 1, 2102 }; 2103 2104 /* 2105 * On Roland devices, use the second alternate setting to be able to use 2106 * the interrupt input endpoint. 2107 */ 2108 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi *umidi) 2109 { 2110 struct usb_interface *intf; 2111 struct usb_host_interface *hostif; 2112 struct usb_interface_descriptor *intfd; 2113 2114 intf = umidi->iface; 2115 if (!intf || intf->num_altsetting != 2) 2116 return; 2117 2118 hostif = &intf->altsetting[1]; 2119 intfd = get_iface_desc(hostif); 2120 /* If either or both of the endpoints support interrupt transfer, 2121 * then use the alternate setting 2122 */ 2123 if (intfd->bNumEndpoints != 2 || 2124 !((get_endpoint(hostif, 0)->bmAttributes & 2125 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT || 2126 (get_endpoint(hostif, 1)->bmAttributes & 2127 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) 2128 return; 2129 2130 dev_dbg(&umidi->dev->dev, "switching to altsetting %d with int ep\n", 2131 intfd->bAlternateSetting); 2132 usb_set_interface(umidi->dev, intfd->bInterfaceNumber, 2133 intfd->bAlternateSetting); 2134 2135 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi); 2136 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0) 2137 umidi->roland_load_ctl = NULL; 2138 } 2139 2140 /* 2141 * Try to find any usable endpoints in the interface. 2142 */ 2143 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi *umidi, 2144 struct snd_usb_midi_endpoint_info *endpoint, 2145 int max_endpoints) 2146 { 2147 struct usb_interface *intf; 2148 struct usb_host_interface *hostif; 2149 struct usb_interface_descriptor *intfd; 2150 struct usb_endpoint_descriptor *epd; 2151 int i, out_eps = 0, in_eps = 0; 2152 2153 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582) 2154 snd_usbmidi_switch_roland_altsetting(umidi); 2155 2156 if (endpoint[0].out_ep || endpoint[0].in_ep) 2157 return 0; 2158 2159 intf = umidi->iface; 2160 if (!intf || intf->num_altsetting < 1) 2161 return -ENOENT; 2162 hostif = intf->cur_altsetting; 2163 intfd = get_iface_desc(hostif); 2164 2165 for (i = 0; i < intfd->bNumEndpoints; ++i) { 2166 epd = get_endpoint(hostif, i); 2167 if (!usb_endpoint_xfer_bulk(epd) && 2168 !usb_endpoint_xfer_int(epd)) 2169 continue; 2170 if (out_eps < max_endpoints && 2171 usb_endpoint_dir_out(epd)) { 2172 endpoint[out_eps].out_ep = usb_endpoint_num(epd); 2173 if (usb_endpoint_xfer_int(epd)) 2174 endpoint[out_eps].out_interval = epd->bInterval; 2175 ++out_eps; 2176 } 2177 if (in_eps < max_endpoints && 2178 usb_endpoint_dir_in(epd)) { 2179 endpoint[in_eps].in_ep = usb_endpoint_num(epd); 2180 if (usb_endpoint_xfer_int(epd)) 2181 endpoint[in_eps].in_interval = epd->bInterval; 2182 ++in_eps; 2183 } 2184 } 2185 return (out_eps || in_eps) ? 0 : -ENOENT; 2186 } 2187 2188 /* 2189 * Detects the endpoints for one-port-per-endpoint protocols. 2190 */ 2191 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi *umidi, 2192 struct snd_usb_midi_endpoint_info *endpoints) 2193 { 2194 int err, i; 2195 2196 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS); 2197 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 2198 if (endpoints[i].out_ep) 2199 endpoints[i].out_cables = 0x0001; 2200 if (endpoints[i].in_ep) 2201 endpoints[i].in_cables = 0x0001; 2202 } 2203 return err; 2204 } 2205 2206 /* 2207 * Detects the endpoints and ports of Yamaha devices. 2208 */ 2209 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi *umidi, 2210 struct snd_usb_midi_endpoint_info *endpoint) 2211 { 2212 struct usb_interface *intf; 2213 struct usb_host_interface *hostif; 2214 struct usb_interface_descriptor *intfd; 2215 uint8_t *cs_desc; 2216 2217 intf = umidi->iface; 2218 if (!intf) 2219 return -ENOENT; 2220 hostif = intf->altsetting; 2221 intfd = get_iface_desc(hostif); 2222 if (intfd->bNumEndpoints < 1) 2223 return -ENOENT; 2224 2225 /* 2226 * For each port there is one MIDI_IN/OUT_JACK descriptor, not 2227 * necessarily with any useful contents. So simply count 'em. 2228 */ 2229 for (cs_desc = hostif->extra; 2230 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2; 2231 cs_desc += cs_desc[0]) { 2232 if (cs_desc[1] == USB_DT_CS_INTERFACE) { 2233 if (cs_desc[2] == UAC_MIDI_IN_JACK) 2234 endpoint->in_cables = 2235 (endpoint->in_cables << 1) | 1; 2236 else if (cs_desc[2] == UAC_MIDI_OUT_JACK) 2237 endpoint->out_cables = 2238 (endpoint->out_cables << 1) | 1; 2239 } 2240 } 2241 if (!endpoint->in_cables && !endpoint->out_cables) 2242 return -ENOENT; 2243 2244 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1); 2245 } 2246 2247 /* 2248 * Detects the endpoints and ports of Roland devices. 2249 */ 2250 static int snd_usbmidi_detect_roland(struct snd_usb_midi *umidi, 2251 struct snd_usb_midi_endpoint_info *endpoint) 2252 { 2253 struct usb_interface *intf; 2254 struct usb_host_interface *hostif; 2255 u8 *cs_desc; 2256 2257 intf = umidi->iface; 2258 if (!intf) 2259 return -ENOENT; 2260 hostif = intf->altsetting; 2261 /* 2262 * Some devices have a descriptor <06 24 F1 02 <inputs> <outputs>>, 2263 * some have standard class descriptors, or both kinds, or neither. 2264 */ 2265 for (cs_desc = hostif->extra; 2266 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2; 2267 cs_desc += cs_desc[0]) { 2268 if (cs_desc[0] >= 6 && 2269 cs_desc[1] == USB_DT_CS_INTERFACE && 2270 cs_desc[2] == 0xf1 && 2271 cs_desc[3] == 0x02) { 2272 if (cs_desc[4] > 0x10 || cs_desc[5] > 0x10) 2273 continue; 2274 endpoint->in_cables = (1 << cs_desc[4]) - 1; 2275 endpoint->out_cables = (1 << cs_desc[5]) - 1; 2276 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1); 2277 } else if (cs_desc[0] >= 7 && 2278 cs_desc[1] == USB_DT_CS_INTERFACE && 2279 cs_desc[2] == UAC_HEADER) { 2280 return snd_usbmidi_get_ms_info(umidi, endpoint); 2281 } 2282 } 2283 2284 return -ENODEV; 2285 } 2286 2287 /* 2288 * Creates the endpoints and their ports for Midiman devices. 2289 */ 2290 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi *umidi, 2291 struct snd_usb_midi_endpoint_info *endpoint) 2292 { 2293 struct snd_usb_midi_endpoint_info ep_info; 2294 struct usb_interface *intf; 2295 struct usb_host_interface *hostif; 2296 struct usb_interface_descriptor *intfd; 2297 struct usb_endpoint_descriptor *epd; 2298 int cable, err; 2299 2300 intf = umidi->iface; 2301 if (!intf) 2302 return -ENOENT; 2303 hostif = intf->altsetting; 2304 intfd = get_iface_desc(hostif); 2305 /* 2306 * The various MidiSport devices have more or less random endpoint 2307 * numbers, so we have to identify the endpoints by their index in 2308 * the descriptor array, like the driver for that other OS does. 2309 * 2310 * There is one interrupt input endpoint for all input ports, one 2311 * bulk output endpoint for even-numbered ports, and one for odd- 2312 * numbered ports. Both bulk output endpoints have corresponding 2313 * input bulk endpoints (at indices 1 and 3) which aren't used. 2314 */ 2315 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) { 2316 dev_dbg(&umidi->dev->dev, "not enough endpoints\n"); 2317 return -ENOENT; 2318 } 2319 2320 epd = get_endpoint(hostif, 0); 2321 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) { 2322 dev_dbg(&umidi->dev->dev, "endpoint[0] isn't interrupt\n"); 2323 return -ENXIO; 2324 } 2325 epd = get_endpoint(hostif, 2); 2326 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) { 2327 dev_dbg(&umidi->dev->dev, "endpoint[2] isn't bulk output\n"); 2328 return -ENXIO; 2329 } 2330 if (endpoint->out_cables > 0x0001) { 2331 epd = get_endpoint(hostif, 4); 2332 if (!usb_endpoint_dir_out(epd) || 2333 !usb_endpoint_xfer_bulk(epd)) { 2334 dev_dbg(&umidi->dev->dev, 2335 "endpoint[4] isn't bulk output\n"); 2336 return -ENXIO; 2337 } 2338 } 2339 2340 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & 2341 USB_ENDPOINT_NUMBER_MASK; 2342 ep_info.out_interval = 0; 2343 ep_info.out_cables = endpoint->out_cables & 0x5555; 2344 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, 2345 &umidi->endpoints[0]); 2346 if (err < 0) 2347 return err; 2348 2349 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & 2350 USB_ENDPOINT_NUMBER_MASK; 2351 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval; 2352 ep_info.in_cables = endpoint->in_cables; 2353 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, 2354 &umidi->endpoints[0]); 2355 if (err < 0) 2356 return err; 2357 2358 if (endpoint->out_cables > 0x0001) { 2359 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & 2360 USB_ENDPOINT_NUMBER_MASK; 2361 ep_info.out_cables = endpoint->out_cables & 0xaaaa; 2362 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, 2363 &umidi->endpoints[1]); 2364 if (err < 0) 2365 return err; 2366 } 2367 2368 for (cable = 0; cable < 0x10; ++cable) { 2369 if (endpoint->out_cables & (1 << cable)) 2370 snd_usbmidi_init_substream(umidi, 2371 SNDRV_RAWMIDI_STREAM_OUTPUT, 2372 cable, 2373 -1 /* prevent trying to find jack */, 2374 &umidi->endpoints[cable & 1].out->ports[cable].substream); 2375 if (endpoint->in_cables & (1 << cable)) 2376 snd_usbmidi_init_substream(umidi, 2377 SNDRV_RAWMIDI_STREAM_INPUT, 2378 cable, 2379 -1 /* prevent trying to find jack */, 2380 &umidi->endpoints[0].in->ports[cable].substream); 2381 } 2382 return 0; 2383 } 2384 2385 static const struct snd_rawmidi_global_ops snd_usbmidi_ops = { 2386 .get_port_info = snd_usbmidi_get_port_info, 2387 }; 2388 2389 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi *umidi, 2390 int out_ports, int in_ports) 2391 { 2392 struct snd_rawmidi *rmidi; 2393 int err; 2394 2395 err = snd_rawmidi_new(umidi->card, "USB MIDI", 2396 umidi->next_midi_device++, 2397 out_ports, in_ports, &rmidi); 2398 if (err < 0) 2399 return err; 2400 strscpy(rmidi->name, umidi->card->shortname); 2401 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | 2402 SNDRV_RAWMIDI_INFO_INPUT | 2403 SNDRV_RAWMIDI_INFO_DUPLEX; 2404 rmidi->ops = &snd_usbmidi_ops; 2405 rmidi->private_data = umidi; 2406 rmidi->private_free = snd_usbmidi_rawmidi_free; 2407 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, 2408 &snd_usbmidi_output_ops); 2409 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, 2410 &snd_usbmidi_input_ops); 2411 2412 umidi->rmidi = rmidi; 2413 return 0; 2414 } 2415 2416 /* 2417 * Temporarily stop input. 2418 */ 2419 void snd_usbmidi_input_stop(struct list_head *p) 2420 { 2421 struct snd_usb_midi *umidi; 2422 unsigned int i, j; 2423 2424 umidi = list_entry(p, struct snd_usb_midi, list); 2425 if (!umidi->input_running) 2426 return; 2427 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 2428 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i]; 2429 if (ep->in) 2430 for (j = 0; j < INPUT_URBS; ++j) 2431 usb_kill_urb(ep->in->urbs[j]); 2432 } 2433 umidi->input_running = 0; 2434 } 2435 EXPORT_SYMBOL(snd_usbmidi_input_stop); 2436 2437 static void snd_usbmidi_input_start_ep(struct snd_usb_midi *umidi, 2438 struct snd_usb_midi_in_endpoint *ep) 2439 { 2440 unsigned int i; 2441 2442 if (!ep) 2443 return; 2444 for (i = 0; i < INPUT_URBS; ++i) { 2445 struct urb *urb = ep->urbs[i]; 2446 scoped_guard(spinlock_irqsave, &umidi->disc_lock) { 2447 if (!atomic_read(&urb->use_count)) { 2448 urb->dev = ep->umidi->dev; 2449 snd_usbmidi_submit_urb(urb, GFP_ATOMIC); 2450 } 2451 } 2452 } 2453 } 2454 2455 /* 2456 * Resume input after a call to snd_usbmidi_input_stop(). 2457 */ 2458 void snd_usbmidi_input_start(struct list_head *p) 2459 { 2460 struct snd_usb_midi *umidi; 2461 int i; 2462 2463 umidi = list_entry(p, struct snd_usb_midi, list); 2464 if (umidi->input_running || !umidi->opened[1]) 2465 return; 2466 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) 2467 snd_usbmidi_input_start_ep(umidi, umidi->endpoints[i].in); 2468 umidi->input_running = 1; 2469 } 2470 EXPORT_SYMBOL(snd_usbmidi_input_start); 2471 2472 /* 2473 * Prepare for suspend. Typically called from the USB suspend callback. 2474 */ 2475 void snd_usbmidi_suspend(struct list_head *p) 2476 { 2477 struct snd_usb_midi *umidi; 2478 2479 umidi = list_entry(p, struct snd_usb_midi, list); 2480 guard(mutex)(&umidi->mutex); 2481 snd_usbmidi_input_stop(p); 2482 } 2483 EXPORT_SYMBOL(snd_usbmidi_suspend); 2484 2485 /* 2486 * Resume. Typically called from the USB resume callback. 2487 */ 2488 void snd_usbmidi_resume(struct list_head *p) 2489 { 2490 struct snd_usb_midi *umidi; 2491 2492 umidi = list_entry(p, struct snd_usb_midi, list); 2493 guard(mutex)(&umidi->mutex); 2494 snd_usbmidi_input_start(p); 2495 } 2496 EXPORT_SYMBOL(snd_usbmidi_resume); 2497 2498 /* 2499 * Creates and registers everything needed for a MIDI streaming interface. 2500 */ 2501 int __snd_usbmidi_create(struct snd_card *card, 2502 struct usb_interface *iface, 2503 struct list_head *midi_list, 2504 const struct snd_usb_audio_quirk *quirk, 2505 unsigned int usb_id, 2506 unsigned int *num_rawmidis) 2507 { 2508 struct snd_usb_midi *umidi; 2509 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS]; 2510 int out_ports, in_ports; 2511 int i, err; 2512 2513 umidi = kzalloc_obj(*umidi); 2514 if (!umidi) 2515 return -ENOMEM; 2516 umidi->dev = interface_to_usbdev(iface); 2517 umidi->card = card; 2518 umidi->iface = iface; 2519 umidi->quirk = quirk; 2520 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops; 2521 if (num_rawmidis) 2522 umidi->next_midi_device = *num_rawmidis; 2523 spin_lock_init(&umidi->disc_lock); 2524 init_rwsem(&umidi->disc_rwsem); 2525 mutex_init(&umidi->mutex); 2526 if (!usb_id) 2527 usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor), 2528 le16_to_cpu(umidi->dev->descriptor.idProduct)); 2529 umidi->usb_id = usb_id; 2530 timer_setup(&umidi->error_timer, snd_usbmidi_error_timer, 0); 2531 2532 /* detect the endpoint(s) to use */ 2533 memset(endpoints, 0, sizeof(endpoints)); 2534 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) { 2535 case QUIRK_MIDI_STANDARD_INTERFACE: 2536 err = snd_usbmidi_get_ms_info(umidi, endpoints); 2537 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */ 2538 umidi->usb_protocol_ops = 2539 &snd_usbmidi_maudio_broken_running_status_ops; 2540 break; 2541 case QUIRK_MIDI_US122L: 2542 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops; 2543 fallthrough; 2544 case QUIRK_MIDI_FIXED_ENDPOINT: 2545 memcpy(&endpoints[0], quirk->data, 2546 sizeof(struct snd_usb_midi_endpoint_info)); 2547 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); 2548 break; 2549 case QUIRK_MIDI_YAMAHA: 2550 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]); 2551 break; 2552 case QUIRK_MIDI_ROLAND: 2553 err = snd_usbmidi_detect_roland(umidi, &endpoints[0]); 2554 break; 2555 case QUIRK_MIDI_MIDIMAN: 2556 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops; 2557 memcpy(&endpoints[0], quirk->data, 2558 sizeof(struct snd_usb_midi_endpoint_info)); 2559 err = 0; 2560 break; 2561 case QUIRK_MIDI_NOVATION: 2562 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops; 2563 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2564 break; 2565 case QUIRK_MIDI_RAW_BYTES: 2566 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops; 2567 /* 2568 * Interface 1 contains isochronous endpoints, but with the same 2569 * numbers as in interface 0. Since it is interface 1 that the 2570 * USB core has most recently seen, these descriptors are now 2571 * associated with the endpoint numbers. This will foul up our 2572 * attempts to submit bulk/interrupt URBs to the endpoints in 2573 * interface 0, so we have to make sure that the USB core looks 2574 * again at interface 0 by calling usb_set_interface() on it. 2575 */ 2576 if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */ 2577 usb_set_interface(umidi->dev, 0, 0); 2578 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2579 break; 2580 case QUIRK_MIDI_EMAGIC: 2581 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops; 2582 memcpy(&endpoints[0], quirk->data, 2583 sizeof(struct snd_usb_midi_endpoint_info)); 2584 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); 2585 break; 2586 case QUIRK_MIDI_CME: 2587 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops; 2588 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2589 break; 2590 case QUIRK_MIDI_AKAI: 2591 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops; 2592 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2593 /* endpoint 1 is input-only */ 2594 endpoints[1].out_cables = 0; 2595 break; 2596 case QUIRK_MIDI_FTDI: 2597 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops; 2598 2599 /* set baud rate to 31250 (48 MHz / 16 / 96) */ 2600 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0), 2601 3, 0x40, 0x60, 0, NULL, 0, 1000); 2602 if (err < 0) 2603 break; 2604 2605 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2606 break; 2607 case QUIRK_MIDI_CH345: 2608 umidi->usb_protocol_ops = &snd_usbmidi_ch345_broken_sysex_ops; 2609 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2610 break; 2611 default: 2612 dev_err(&umidi->dev->dev, "invalid quirk type %d\n", 2613 quirk->type); 2614 err = -ENXIO; 2615 break; 2616 } 2617 if (err < 0) 2618 goto free_midi; 2619 2620 /* create rawmidi device */ 2621 out_ports = 0; 2622 in_ports = 0; 2623 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 2624 out_ports += hweight16(endpoints[i].out_cables); 2625 in_ports += hweight16(endpoints[i].in_cables); 2626 } 2627 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports); 2628 if (err < 0) 2629 goto free_midi; 2630 2631 /* create endpoint/port structures */ 2632 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN) 2633 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]); 2634 else 2635 err = snd_usbmidi_create_endpoints(umidi, endpoints); 2636 if (err < 0) 2637 goto exit; 2638 2639 usb_autopm_get_interface_no_resume(umidi->iface); 2640 2641 list_add_tail(&umidi->list, midi_list); 2642 if (num_rawmidis) 2643 *num_rawmidis = umidi->next_midi_device; 2644 return 0; 2645 2646 free_midi: 2647 kfree(umidi); 2648 exit: 2649 return err; 2650 } 2651 EXPORT_SYMBOL(__snd_usbmidi_create); 2652