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