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 if (buf_end <= 0) 801 return; 802 803 /* only try adding more data when there's space for at least 1 SysEx */ 804 while (urb->transfer_buffer_length < buf_end) { 805 count = snd_rawmidi_transmit_peek(substream, 806 tmp, MAX_AKAI_SYSEX_LEN); 807 if (!count) { 808 ep->ports[0].active = 0; 809 return; 810 } 811 /* try to skip non-SysEx data */ 812 for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++) 813 ; 814 815 if (pos > 0) { 816 snd_rawmidi_transmit_ack(substream, pos); 817 continue; 818 } 819 820 /* look for the start or end marker */ 821 for (end = 1; end < count && tmp[end] < 0xF0; end++) 822 ; 823 824 /* next SysEx started before the end of current one */ 825 if (end < count && tmp[end] == 0xF0) { 826 /* it's incomplete - drop it */ 827 snd_rawmidi_transmit_ack(substream, end); 828 continue; 829 } 830 /* SysEx complete */ 831 if (end < count && tmp[end] == 0xF7) { 832 /* queue it, ack it, and get the next one */ 833 count = end + 1; 834 msg[0] = 0x10 | count; 835 memcpy(&msg[1], tmp, count); 836 snd_rawmidi_transmit_ack(substream, count); 837 urb->transfer_buffer_length += count + 1; 838 msg += count + 1; 839 continue; 840 } 841 /* less than 9 bytes and no end byte - wait for more */ 842 if (count < MAX_AKAI_SYSEX_LEN) { 843 ep->ports[0].active = 0; 844 return; 845 } 846 /* 9 bytes and no end marker in sight - malformed, skip it */ 847 snd_rawmidi_transmit_ack(substream, count); 848 } 849 } 850 851 static const struct usb_protocol_ops snd_usbmidi_akai_ops = { 852 .input = snd_usbmidi_akai_input, 853 .output = snd_usbmidi_akai_output, 854 }; 855 856 /* 857 * Novation USB MIDI protocol: number of data bytes is in the first byte 858 * (when receiving) (+1!) or in the second byte (when sending); data begins 859 * at the third byte. 860 */ 861 862 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint *ep, 863 uint8_t *buffer, int buffer_length) 864 { 865 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1) 866 return; 867 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1); 868 } 869 870 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint *ep, 871 struct urb *urb) 872 { 873 uint8_t *transfer_buffer; 874 int count; 875 876 if (!ep->ports[0].active) 877 return; 878 transfer_buffer = urb->transfer_buffer; 879 count = snd_rawmidi_transmit(ep->ports[0].substream, 880 &transfer_buffer[2], 881 ep->max_transfer - 2); 882 if (count < 1) { 883 ep->ports[0].active = 0; 884 return; 885 } 886 transfer_buffer[0] = 0; 887 transfer_buffer[1] = count; 888 urb->transfer_buffer_length = 2 + count; 889 } 890 891 static const struct usb_protocol_ops snd_usbmidi_novation_ops = { 892 .input = snd_usbmidi_novation_input, 893 .output = snd_usbmidi_novation_output, 894 }; 895 896 /* 897 * "raw" protocol: just move raw MIDI bytes from/to the endpoint 898 */ 899 900 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint *ep, 901 uint8_t *buffer, int buffer_length) 902 { 903 snd_usbmidi_input_data(ep, 0, buffer, buffer_length); 904 } 905 906 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint *ep, 907 struct urb *urb) 908 { 909 int count; 910 911 if (!ep->ports[0].active) 912 return; 913 count = snd_rawmidi_transmit(ep->ports[0].substream, 914 urb->transfer_buffer, 915 ep->max_transfer); 916 if (count < 1) { 917 ep->ports[0].active = 0; 918 return; 919 } 920 urb->transfer_buffer_length = count; 921 } 922 923 static const struct usb_protocol_ops snd_usbmidi_raw_ops = { 924 .input = snd_usbmidi_raw_input, 925 .output = snd_usbmidi_raw_output, 926 }; 927 928 /* 929 * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes. 930 */ 931 932 static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint *ep, 933 uint8_t *buffer, int buffer_length) 934 { 935 if (buffer_length > 2) 936 snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2); 937 } 938 939 static const struct usb_protocol_ops snd_usbmidi_ftdi_ops = { 940 .input = snd_usbmidi_ftdi_input, 941 .output = snd_usbmidi_raw_output, 942 }; 943 944 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep, 945 uint8_t *buffer, int buffer_length) 946 { 947 if (buffer_length != 9) 948 return; 949 buffer_length = 8; 950 while (buffer_length && buffer[buffer_length - 1] == 0xFD) 951 buffer_length--; 952 if (buffer_length) 953 snd_usbmidi_input_data(ep, 0, buffer, buffer_length); 954 } 955 956 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep, 957 struct urb *urb) 958 { 959 int count; 960 961 if (!ep->ports[0].active) 962 return; 963 switch (snd_usb_get_speed(ep->umidi->dev)) { 964 case USB_SPEED_HIGH: 965 case USB_SPEED_SUPER: 966 case USB_SPEED_SUPER_PLUS: 967 count = 1; 968 break; 969 default: 970 count = 2; 971 } 972 count = snd_rawmidi_transmit(ep->ports[0].substream, 973 urb->transfer_buffer, 974 count); 975 if (count < 1) { 976 ep->ports[0].active = 0; 977 return; 978 } 979 980 memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count); 981 urb->transfer_buffer_length = ep->max_transfer; 982 } 983 984 static const struct usb_protocol_ops snd_usbmidi_122l_ops = { 985 .input = snd_usbmidi_us122l_input, 986 .output = snd_usbmidi_us122l_output, 987 }; 988 989 /* 990 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching. 991 */ 992 993 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint *ep) 994 { 995 static const u8 init_data[] = { 996 /* initialization magic: "get version" */ 997 0xf0, 998 0x00, 0x20, 0x31, /* Emagic */ 999 0x64, /* Unitor8 */ 1000 0x0b, /* version number request */ 1001 0x00, /* command version */ 1002 0x00, /* EEPROM, box 0 */ 1003 0xf7 1004 }; 1005 send_bulk_static_data(ep, init_data, sizeof(init_data)); 1006 /* while we're at it, pour on more magic */ 1007 send_bulk_static_data(ep, init_data, sizeof(init_data)); 1008 } 1009 1010 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint *ep) 1011 { 1012 static const u8 finish_data[] = { 1013 /* switch to patch mode with last preset */ 1014 0xf0, 1015 0x00, 0x20, 0x31, /* Emagic */ 1016 0x64, /* Unitor8 */ 1017 0x10, /* patch switch command */ 1018 0x00, /* command version */ 1019 0x7f, /* to all boxes */ 1020 0x40, /* last preset in EEPROM */ 1021 0xf7 1022 }; 1023 send_bulk_static_data(ep, finish_data, sizeof(finish_data)); 1024 } 1025 1026 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint *ep, 1027 uint8_t *buffer, int buffer_length) 1028 { 1029 int i; 1030 1031 /* FF indicates end of valid data */ 1032 for (i = 0; i < buffer_length; ++i) 1033 if (buffer[i] == 0xff) { 1034 buffer_length = i; 1035 break; 1036 } 1037 1038 /* handle F5 at end of last buffer */ 1039 if (ep->seen_f5) 1040 goto switch_port; 1041 1042 while (buffer_length > 0) { 1043 /* determine size of data until next F5 */ 1044 for (i = 0; i < buffer_length; ++i) 1045 if (buffer[i] == 0xf5) 1046 break; 1047 snd_usbmidi_input_data(ep, ep->current_port, buffer, i); 1048 buffer += i; 1049 buffer_length -= i; 1050 1051 if (buffer_length <= 0) 1052 break; 1053 /* assert(buffer[0] == 0xf5); */ 1054 ep->seen_f5 = 1; 1055 ++buffer; 1056 --buffer_length; 1057 1058 switch_port: 1059 if (buffer_length <= 0) 1060 break; 1061 if (buffer[0] < 0x80) { 1062 ep->current_port = (buffer[0] - 1) & 15; 1063 ++buffer; 1064 --buffer_length; 1065 } 1066 ep->seen_f5 = 0; 1067 } 1068 } 1069 1070 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint *ep, 1071 struct urb *urb) 1072 { 1073 int port0 = ep->current_port; 1074 uint8_t *buf = urb->transfer_buffer; 1075 int buf_free = ep->max_transfer; 1076 int length, i; 1077 1078 for (i = 0; i < 0x10; ++i) { 1079 /* round-robin, starting at the last current port */ 1080 int portnum = (port0 + i) & 15; 1081 struct usbmidi_out_port *port = &ep->ports[portnum]; 1082 1083 if (!port->active) 1084 continue; 1085 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) { 1086 port->active = 0; 1087 continue; 1088 } 1089 1090 if (portnum != ep->current_port) { 1091 if (buf_free < 2) 1092 break; 1093 ep->current_port = portnum; 1094 buf[0] = 0xf5; 1095 buf[1] = (portnum + 1) & 15; 1096 buf += 2; 1097 buf_free -= 2; 1098 } 1099 1100 if (buf_free < 1) 1101 break; 1102 length = snd_rawmidi_transmit(port->substream, buf, buf_free); 1103 if (length > 0) { 1104 buf += length; 1105 buf_free -= length; 1106 if (buf_free < 1) 1107 break; 1108 } 1109 } 1110 if (buf_free < ep->max_transfer && buf_free > 0) { 1111 *buf = 0xff; 1112 --buf_free; 1113 } 1114 urb->transfer_buffer_length = ep->max_transfer - buf_free; 1115 } 1116 1117 static const struct usb_protocol_ops snd_usbmidi_emagic_ops = { 1118 .input = snd_usbmidi_emagic_input, 1119 .output = snd_usbmidi_emagic_output, 1120 .init_out_endpoint = snd_usbmidi_emagic_init_out, 1121 .finish_out_endpoint = snd_usbmidi_emagic_finish_out, 1122 }; 1123 1124 1125 static void update_roland_altsetting(struct snd_usb_midi *umidi) 1126 { 1127 struct usb_interface *intf; 1128 struct usb_host_interface *hostif; 1129 struct usb_interface_descriptor *intfd; 1130 int is_light_load; 1131 1132 intf = umidi->iface; 1133 is_light_load = intf->cur_altsetting != intf->altsetting; 1134 if (umidi->roland_load_ctl->private_value == is_light_load) 1135 return; 1136 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value]; 1137 intfd = get_iface_desc(hostif); 1138 snd_usbmidi_input_stop(&umidi->list); 1139 usb_set_interface(umidi->dev, intfd->bInterfaceNumber, 1140 intfd->bAlternateSetting); 1141 snd_usbmidi_input_start(&umidi->list); 1142 } 1143 1144 static int substream_open(struct snd_rawmidi_substream *substream, int dir, 1145 int open) 1146 { 1147 struct snd_usb_midi *umidi = substream->rmidi->private_data; 1148 struct snd_kcontrol *ctl; 1149 1150 guard(rwsem_read)(&umidi->disc_rwsem); 1151 if (umidi->disconnected) 1152 return open ? -ENODEV : 0; 1153 1154 guard(mutex)(&umidi->mutex); 1155 if (open) { 1156 if (!umidi->opened[0] && !umidi->opened[1]) { 1157 if (umidi->roland_load_ctl) { 1158 ctl = umidi->roland_load_ctl; 1159 ctl->vd[0].access |= 1160 SNDRV_CTL_ELEM_ACCESS_INACTIVE; 1161 snd_ctl_notify(umidi->card, 1162 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); 1163 update_roland_altsetting(umidi); 1164 } 1165 } 1166 umidi->opened[dir]++; 1167 if (umidi->opened[1]) 1168 snd_usbmidi_input_start(&umidi->list); 1169 } else { 1170 umidi->opened[dir]--; 1171 if (!umidi->opened[1]) 1172 snd_usbmidi_input_stop(&umidi->list); 1173 if (!umidi->opened[0] && !umidi->opened[1]) { 1174 if (umidi->roland_load_ctl) { 1175 ctl = umidi->roland_load_ctl; 1176 ctl->vd[0].access &= 1177 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; 1178 snd_ctl_notify(umidi->card, 1179 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); 1180 } 1181 } 1182 } 1183 return 0; 1184 } 1185 1186 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream) 1187 { 1188 struct snd_usb_midi *umidi = substream->rmidi->private_data; 1189 struct usbmidi_out_port *port = NULL; 1190 int i, j; 1191 1192 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) 1193 if (umidi->endpoints[i].out) 1194 for (j = 0; j < 0x10; ++j) 1195 if (umidi->endpoints[i].out->ports[j].substream == substream) { 1196 port = &umidi->endpoints[i].out->ports[j]; 1197 break; 1198 } 1199 if (!port) 1200 return -ENXIO; 1201 1202 substream->runtime->private_data = port; 1203 port->state = STATE_UNKNOWN; 1204 return substream_open(substream, 0, 1); 1205 } 1206 1207 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream) 1208 { 1209 struct usbmidi_out_port *port = substream->runtime->private_data; 1210 1211 flush_work(&port->ep->work); 1212 return substream_open(substream, 0, 0); 1213 } 1214 1215 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, 1216 int up) 1217 { 1218 struct usbmidi_out_port *port = 1219 (struct usbmidi_out_port *)substream->runtime->private_data; 1220 1221 port->active = up; 1222 if (up) { 1223 if (port->ep->umidi->disconnected) { 1224 /* gobble up remaining bytes to prevent wait in 1225 * snd_rawmidi_drain_output */ 1226 snd_rawmidi_proceed(substream); 1227 return; 1228 } 1229 queue_work(system_highpri_wq, &port->ep->work); 1230 } 1231 } 1232 1233 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream) 1234 { 1235 struct usbmidi_out_port *port = substream->runtime->private_data; 1236 struct snd_usb_midi_out_endpoint *ep = port->ep; 1237 unsigned int drain_urbs; 1238 DEFINE_WAIT(wait); 1239 long timeout = msecs_to_jiffies(50); 1240 1241 if (ep->umidi->disconnected) 1242 return; 1243 /* 1244 * The substream buffer is empty, but some data might still be in the 1245 * currently active URBs, so we have to wait for those to complete. 1246 */ 1247 spin_lock_irq(&ep->buffer_lock); 1248 drain_urbs = ep->active_urbs; 1249 if (drain_urbs) { 1250 ep->drain_urbs |= drain_urbs; 1251 do { 1252 prepare_to_wait(&ep->drain_wait, &wait, 1253 TASK_UNINTERRUPTIBLE); 1254 spin_unlock_irq(&ep->buffer_lock); 1255 timeout = schedule_timeout(timeout); 1256 spin_lock_irq(&ep->buffer_lock); 1257 drain_urbs &= ep->drain_urbs; 1258 } while (drain_urbs && timeout); 1259 finish_wait(&ep->drain_wait, &wait); 1260 } 1261 port->active = 0; 1262 spin_unlock_irq(&ep->buffer_lock); 1263 } 1264 1265 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream) 1266 { 1267 return substream_open(substream, 1, 1); 1268 } 1269 1270 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream) 1271 { 1272 return substream_open(substream, 1, 0); 1273 } 1274 1275 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, 1276 int up) 1277 { 1278 struct snd_usb_midi *umidi = substream->rmidi->private_data; 1279 1280 if (up) 1281 set_bit(substream->number, &umidi->input_triggered); 1282 else 1283 clear_bit(substream->number, &umidi->input_triggered); 1284 } 1285 1286 static const struct snd_rawmidi_ops snd_usbmidi_output_ops = { 1287 .open = snd_usbmidi_output_open, 1288 .close = snd_usbmidi_output_close, 1289 .trigger = snd_usbmidi_output_trigger, 1290 .drain = snd_usbmidi_output_drain, 1291 }; 1292 1293 static const struct snd_rawmidi_ops snd_usbmidi_input_ops = { 1294 .open = snd_usbmidi_input_open, 1295 .close = snd_usbmidi_input_close, 1296 .trigger = snd_usbmidi_input_trigger 1297 }; 1298 1299 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb, 1300 unsigned int buffer_length) 1301 { 1302 usb_free_coherent(umidi->dev, buffer_length, 1303 urb->transfer_buffer, urb->transfer_dma); 1304 usb_free_urb(urb); 1305 } 1306 1307 /* 1308 * Frees an input endpoint. 1309 * May be called when ep hasn't been initialized completely. 1310 */ 1311 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint *ep) 1312 { 1313 unsigned int i; 1314 1315 for (i = 0; i < INPUT_URBS; ++i) 1316 if (ep->urbs[i]) 1317 free_urb_and_buffer(ep->umidi, ep->urbs[i], 1318 ep->urbs[i]->transfer_buffer_length); 1319 kfree(ep); 1320 } 1321 1322 /* 1323 * Creates an input endpoint. 1324 */ 1325 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi *umidi, 1326 struct snd_usb_midi_endpoint_info *ep_info, 1327 struct snd_usb_midi_endpoint *rep) 1328 { 1329 struct snd_usb_midi_in_endpoint *ep; 1330 void *buffer; 1331 unsigned int pipe; 1332 int length; 1333 unsigned int i; 1334 int err; 1335 1336 rep->in = NULL; 1337 ep = kzalloc_obj(*ep); 1338 if (!ep) 1339 return -ENOMEM; 1340 ep->umidi = umidi; 1341 1342 for (i = 0; i < INPUT_URBS; ++i) { 1343 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL); 1344 if (!ep->urbs[i]) { 1345 err = -ENOMEM; 1346 goto error; 1347 } 1348 } 1349 if (ep_info->in_interval) 1350 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep); 1351 else 1352 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep); 1353 length = usb_maxpacket(umidi->dev, pipe); 1354 for (i = 0; i < INPUT_URBS; ++i) { 1355 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL, 1356 &ep->urbs[i]->transfer_dma); 1357 if (!buffer) { 1358 err = -ENOMEM; 1359 goto error; 1360 } 1361 if (ep_info->in_interval) 1362 usb_fill_int_urb(ep->urbs[i], umidi->dev, 1363 pipe, buffer, length, 1364 snd_usbmidi_in_urb_complete, 1365 ep, ep_info->in_interval); 1366 else 1367 usb_fill_bulk_urb(ep->urbs[i], umidi->dev, 1368 pipe, buffer, length, 1369 snd_usbmidi_in_urb_complete, ep); 1370 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 1371 err = usb_urb_ep_type_check(ep->urbs[i]); 1372 if (err < 0) { 1373 dev_err(&umidi->dev->dev, "invalid MIDI in EP %x\n", 1374 ep_info->in_ep); 1375 goto error; 1376 } 1377 } 1378 1379 rep->in = ep; 1380 return 0; 1381 1382 error: 1383 snd_usbmidi_in_endpoint_delete(ep); 1384 return err; 1385 } 1386 1387 /* 1388 * Frees an output endpoint. 1389 * May be called when ep hasn't been initialized completely. 1390 */ 1391 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep) 1392 { 1393 unsigned int i; 1394 1395 for (i = 0; i < OUTPUT_URBS; ++i) 1396 if (ep->urbs[i].urb) { 1397 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb, 1398 ep->max_transfer); 1399 ep->urbs[i].urb = NULL; 1400 } 1401 } 1402 1403 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep) 1404 { 1405 snd_usbmidi_out_endpoint_clear(ep); 1406 kfree(ep); 1407 } 1408 1409 /* 1410 * Creates an output endpoint, and initializes output ports. 1411 */ 1412 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi *umidi, 1413 struct snd_usb_midi_endpoint_info *ep_info, 1414 struct snd_usb_midi_endpoint *rep) 1415 { 1416 struct snd_usb_midi_out_endpoint *ep; 1417 unsigned int i; 1418 unsigned int pipe; 1419 void *buffer; 1420 int err; 1421 1422 rep->out = NULL; 1423 ep = kzalloc_obj(*ep); 1424 if (!ep) 1425 return -ENOMEM; 1426 ep->umidi = umidi; 1427 1428 for (i = 0; i < OUTPUT_URBS; ++i) { 1429 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL); 1430 if (!ep->urbs[i].urb) { 1431 err = -ENOMEM; 1432 goto error; 1433 } 1434 ep->urbs[i].ep = ep; 1435 } 1436 if (ep_info->out_interval) 1437 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep); 1438 else 1439 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep); 1440 switch (umidi->usb_id) { 1441 default: 1442 ep->max_transfer = usb_maxpacket(umidi->dev, pipe); 1443 break; 1444 /* 1445 * Various chips declare a packet size larger than 4 bytes, but 1446 * do not actually work with larger packets: 1447 */ 1448 case USB_ID(0x0a67, 0x5011): /* Medeli DD305 */ 1449 case USB_ID(0x0a92, 0x1020): /* ESI M4U */ 1450 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */ 1451 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */ 1452 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */ 1453 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */ 1454 case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */ 1455 ep->max_transfer = 4; 1456 break; 1457 /* 1458 * Some devices only work with 9 bytes packet size: 1459 */ 1460 case USB_ID(0x0644, 0x800e): /* Tascam US-122L */ 1461 case USB_ID(0x0644, 0x800f): /* Tascam US-144 */ 1462 ep->max_transfer = 9; 1463 break; 1464 } 1465 for (i = 0; i < OUTPUT_URBS; ++i) { 1466 buffer = usb_alloc_coherent(umidi->dev, 1467 ep->max_transfer, GFP_KERNEL, 1468 &ep->urbs[i].urb->transfer_dma); 1469 if (!buffer) { 1470 err = -ENOMEM; 1471 goto error; 1472 } 1473 if (ep_info->out_interval) 1474 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev, 1475 pipe, buffer, ep->max_transfer, 1476 snd_usbmidi_out_urb_complete, 1477 &ep->urbs[i], ep_info->out_interval); 1478 else 1479 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev, 1480 pipe, buffer, ep->max_transfer, 1481 snd_usbmidi_out_urb_complete, 1482 &ep->urbs[i]); 1483 err = usb_urb_ep_type_check(ep->urbs[i].urb); 1484 if (err < 0) { 1485 dev_err(&umidi->dev->dev, "invalid MIDI out EP %x\n", 1486 ep_info->out_ep); 1487 goto error; 1488 } 1489 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 1490 } 1491 1492 spin_lock_init(&ep->buffer_lock); 1493 INIT_WORK(&ep->work, snd_usbmidi_out_work); 1494 init_waitqueue_head(&ep->drain_wait); 1495 1496 for (i = 0; i < 0x10; ++i) 1497 if (ep_info->out_cables & (1 << i)) { 1498 ep->ports[i].ep = ep; 1499 ep->ports[i].cable = i << 4; 1500 } 1501 1502 if (umidi->usb_protocol_ops->init_out_endpoint) 1503 umidi->usb_protocol_ops->init_out_endpoint(ep); 1504 1505 rep->out = ep; 1506 return 0; 1507 1508 error: 1509 snd_usbmidi_out_endpoint_delete(ep); 1510 return err; 1511 } 1512 1513 /* 1514 * Frees everything. 1515 */ 1516 static void snd_usbmidi_free(struct snd_usb_midi *umidi) 1517 { 1518 int i; 1519 1520 if (!umidi->disconnected) 1521 snd_usbmidi_disconnect(&umidi->list); 1522 1523 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1524 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i]; 1525 kfree(ep->out); 1526 } 1527 mutex_destroy(&umidi->mutex); 1528 kfree(umidi); 1529 } 1530 1531 /* 1532 * Unlinks all URBs (must be done before the usb_device is deleted). 1533 */ 1534 void snd_usbmidi_disconnect(struct list_head *p) 1535 { 1536 struct snd_usb_midi *umidi; 1537 unsigned int i, j; 1538 1539 umidi = list_entry(p, struct snd_usb_midi, list); 1540 /* 1541 * an URB's completion handler may start the timer and 1542 * a timer may submit an URB. To reliably break the cycle 1543 * a flag under lock must be used 1544 */ 1545 scoped_guard(rwsem_write, &umidi->disc_rwsem) { 1546 guard(spinlock_irq)(&umidi->disc_lock); 1547 umidi->disconnected = 1; 1548 } 1549 1550 timer_shutdown_sync(&umidi->error_timer); 1551 1552 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1553 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i]; 1554 if (ep->out) 1555 cancel_work_sync(&ep->out->work); 1556 if (ep->out) { 1557 for (j = 0; j < OUTPUT_URBS; ++j) 1558 usb_kill_urb(ep->out->urbs[j].urb); 1559 if (umidi->usb_protocol_ops->finish_out_endpoint) 1560 umidi->usb_protocol_ops->finish_out_endpoint(ep->out); 1561 ep->out->active_urbs = 0; 1562 if (ep->out->drain_urbs) { 1563 ep->out->drain_urbs = 0; 1564 wake_up(&ep->out->drain_wait); 1565 } 1566 } 1567 if (ep->in) 1568 for (j = 0; j < INPUT_URBS; ++j) 1569 usb_kill_urb(ep->in->urbs[j]); 1570 /* free endpoints here; later call can result in Oops */ 1571 if (ep->out) 1572 snd_usbmidi_out_endpoint_clear(ep->out); 1573 if (ep->in) { 1574 snd_usbmidi_in_endpoint_delete(ep->in); 1575 ep->in = NULL; 1576 } 1577 } 1578 } 1579 EXPORT_SYMBOL(snd_usbmidi_disconnect); 1580 1581 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi) 1582 { 1583 struct snd_usb_midi *umidi = rmidi->private_data; 1584 snd_usbmidi_free(umidi); 1585 } 1586 1587 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi *umidi, 1588 int stream, 1589 int number) 1590 { 1591 struct snd_rawmidi_substream *substream; 1592 1593 list_for_each_entry(substream, &umidi->rmidi->streams[stream].substreams, 1594 list) { 1595 if (substream->number == number) 1596 return substream; 1597 } 1598 return NULL; 1599 } 1600 1601 /* 1602 * This list specifies names for ports that do not fit into the standard 1603 * "(product) MIDI (n)" schema because they aren't external MIDI ports, 1604 * such as internal control or synthesizer ports. 1605 */ 1606 static struct port_info { 1607 u32 id; 1608 short int port; 1609 short int voices; 1610 const char *name; 1611 unsigned int seq_flags; 1612 } snd_usbmidi_port_info[] = { 1613 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \ 1614 { .id = USB_ID(vendor, product), \ 1615 .port = num, .voices = voices_, \ 1616 .name = name_, .seq_flags = flags } 1617 #define EXTERNAL_PORT(vendor, product, num, name) \ 1618 PORT_INFO(vendor, product, num, name, 0, \ 1619 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1620 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1621 SNDRV_SEQ_PORT_TYPE_PORT) 1622 #define CONTROL_PORT(vendor, product, num, name) \ 1623 PORT_INFO(vendor, product, num, name, 0, \ 1624 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1625 SNDRV_SEQ_PORT_TYPE_HARDWARE) 1626 #define GM_SYNTH_PORT(vendor, product, num, name, voices) \ 1627 PORT_INFO(vendor, product, num, name, voices, \ 1628 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1629 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ 1630 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1631 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) 1632 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \ 1633 PORT_INFO(vendor, product, num, name, voices, \ 1634 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1635 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ 1636 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ 1637 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ 1638 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ 1639 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1640 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) 1641 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \ 1642 PORT_INFO(vendor, product, num, name, voices, \ 1643 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1644 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ 1645 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ 1646 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ 1647 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ 1648 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \ 1649 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1650 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) 1651 /* Yamaha MOTIF XF */ 1652 GM_SYNTH_PORT(0x0499, 0x105c, 0, "%s Tone Generator", 128), 1653 CONTROL_PORT(0x0499, 0x105c, 1, "%s Remote Control"), 1654 EXTERNAL_PORT(0x0499, 0x105c, 2, "%s Thru"), 1655 CONTROL_PORT(0x0499, 0x105c, 3, "%s Editor"), 1656 /* Roland UA-100 */ 1657 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"), 1658 /* Roland SC-8850 */ 1659 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128), 1660 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128), 1661 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128), 1662 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128), 1663 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"), 1664 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"), 1665 /* Roland U-8 */ 1666 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"), 1667 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"), 1668 /* Roland SC-8820 */ 1669 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64), 1670 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64), 1671 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"), 1672 /* Roland SK-500 */ 1673 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64), 1674 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64), 1675 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"), 1676 /* Roland SC-D70 */ 1677 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64), 1678 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64), 1679 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"), 1680 /* Edirol UM-880 */ 1681 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"), 1682 /* Edirol SD-90 */ 1683 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128), 1684 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128), 1685 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"), 1686 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"), 1687 /* Edirol UM-550 */ 1688 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"), 1689 /* Edirol SD-20 */ 1690 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64), 1691 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64), 1692 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"), 1693 /* Edirol SD-80 */ 1694 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128), 1695 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128), 1696 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"), 1697 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"), 1698 /* Edirol UA-700 */ 1699 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"), 1700 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"), 1701 /* Roland VariOS */ 1702 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"), 1703 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"), 1704 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"), 1705 /* Edirol PCR */ 1706 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"), 1707 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"), 1708 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"), 1709 /* BOSS GS-10 */ 1710 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"), 1711 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"), 1712 /* Edirol UA-1000 */ 1713 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"), 1714 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"), 1715 /* Edirol UR-80 */ 1716 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"), 1717 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"), 1718 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"), 1719 /* Edirol PCR-A */ 1720 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"), 1721 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"), 1722 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"), 1723 /* BOSS GT-PRO */ 1724 CONTROL_PORT(0x0582, 0x0089, 0, "%s Control"), 1725 /* Edirol UM-3EX */ 1726 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"), 1727 /* Roland VG-99 */ 1728 CONTROL_PORT(0x0582, 0x00b2, 0, "%s Control"), 1729 EXTERNAL_PORT(0x0582, 0x00b2, 1, "%s MIDI"), 1730 /* Cakewalk Sonar V-Studio 100 */ 1731 EXTERNAL_PORT(0x0582, 0x00eb, 0, "%s MIDI"), 1732 CONTROL_PORT(0x0582, 0x00eb, 1, "%s Control"), 1733 /* Roland VB-99 */ 1734 CONTROL_PORT(0x0582, 0x0102, 0, "%s Control"), 1735 EXTERNAL_PORT(0x0582, 0x0102, 1, "%s MIDI"), 1736 /* Roland A-PRO */ 1737 EXTERNAL_PORT(0x0582, 0x010f, 0, "%s MIDI"), 1738 CONTROL_PORT(0x0582, 0x010f, 1, "%s 1"), 1739 CONTROL_PORT(0x0582, 0x010f, 2, "%s 2"), 1740 /* Roland SD-50 */ 1741 ROLAND_SYNTH_PORT(0x0582, 0x0114, 0, "%s Synth", 128), 1742 EXTERNAL_PORT(0x0582, 0x0114, 1, "%s MIDI"), 1743 CONTROL_PORT(0x0582, 0x0114, 2, "%s Control"), 1744 /* Roland OCTA-CAPTURE */ 1745 EXTERNAL_PORT(0x0582, 0x0120, 0, "%s MIDI"), 1746 CONTROL_PORT(0x0582, 0x0120, 1, "%s Control"), 1747 EXTERNAL_PORT(0x0582, 0x0121, 0, "%s MIDI"), 1748 CONTROL_PORT(0x0582, 0x0121, 1, "%s Control"), 1749 /* Roland SPD-SX */ 1750 CONTROL_PORT(0x0582, 0x0145, 0, "%s Control"), 1751 EXTERNAL_PORT(0x0582, 0x0145, 1, "%s MIDI"), 1752 /* Roland A-Series */ 1753 CONTROL_PORT(0x0582, 0x0156, 0, "%s Keyboard"), 1754 EXTERNAL_PORT(0x0582, 0x0156, 1, "%s MIDI"), 1755 /* Roland INTEGRA-7 */ 1756 ROLAND_SYNTH_PORT(0x0582, 0x015b, 0, "%s Synth", 128), 1757 CONTROL_PORT(0x0582, 0x015b, 1, "%s Control"), 1758 /* M-Audio MidiSport 8x8 */ 1759 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"), 1760 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"), 1761 /* MOTU Fastlane */ 1762 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"), 1763 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"), 1764 /* Emagic Unitor8/AMT8/MT4 */ 1765 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"), 1766 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"), 1767 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"), 1768 /* Akai MPD16 */ 1769 CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"), 1770 PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0, 1771 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | 1772 SNDRV_SEQ_PORT_TYPE_HARDWARE), 1773 /* Access Music Virus TI */ 1774 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"), 1775 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0, 1776 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | 1777 SNDRV_SEQ_PORT_TYPE_HARDWARE | 1778 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER), 1779 }; 1780 1781 static struct port_info *find_port_info(struct snd_usb_midi *umidi, int number) 1782 { 1783 int i; 1784 1785 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) { 1786 if (snd_usbmidi_port_info[i].id == umidi->usb_id && 1787 snd_usbmidi_port_info[i].port == number) 1788 return &snd_usbmidi_port_info[i]; 1789 } 1790 return NULL; 1791 } 1792 1793 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number, 1794 struct snd_seq_port_info *seq_port_info) 1795 { 1796 struct snd_usb_midi *umidi = rmidi->private_data; 1797 struct port_info *port_info; 1798 1799 /* TODO: read port flags from descriptors */ 1800 port_info = find_port_info(umidi, number); 1801 if (port_info) { 1802 seq_port_info->type = port_info->seq_flags; 1803 seq_port_info->midi_voices = port_info->voices; 1804 } 1805 } 1806 1807 /* return iJack for the corresponding jackID */ 1808 static int find_usb_ijack(struct usb_host_interface *hostif, uint8_t jack_id) 1809 { 1810 unsigned char *extra = hostif->extra; 1811 int extralen = hostif->extralen; 1812 struct usb_descriptor_header *h; 1813 struct usb_midi_out_jack_descriptor *outjd; 1814 struct usb_midi_in_jack_descriptor *injd; 1815 size_t sz; 1816 1817 while (extralen > 4) { 1818 h = (struct usb_descriptor_header *)extra; 1819 if (h->bDescriptorType != USB_DT_CS_INTERFACE) 1820 goto next; 1821 1822 outjd = (struct usb_midi_out_jack_descriptor *)h; 1823 if (h->bLength >= sizeof(*outjd) && 1824 outjd->bDescriptorSubtype == UAC_MIDI_OUT_JACK && 1825 outjd->bJackID == jack_id) { 1826 sz = USB_DT_MIDI_OUT_SIZE(outjd->bNrInputPins); 1827 if (outjd->bLength < sz) 1828 goto next; 1829 return *(extra + sz - 1); 1830 } 1831 1832 injd = (struct usb_midi_in_jack_descriptor *)h; 1833 if (injd->bLength >= sizeof(*injd) && 1834 injd->bDescriptorSubtype == UAC_MIDI_IN_JACK && 1835 injd->bJackID == jack_id) 1836 return injd->iJack; 1837 1838 next: 1839 if (!extra[0]) 1840 break; 1841 extralen -= extra[0]; 1842 extra += extra[0]; 1843 } 1844 return 0; 1845 } 1846 1847 static void snd_usbmidi_init_substream(struct snd_usb_midi *umidi, 1848 int stream, int number, int jack_id, 1849 struct snd_rawmidi_substream **rsubstream) 1850 { 1851 struct port_info *port_info; 1852 const char *name_format; 1853 struct usb_interface *intf; 1854 struct usb_host_interface *hostif; 1855 uint8_t jack_name_buf[32]; 1856 uint8_t *default_jack_name = "MIDI"; 1857 uint8_t *jack_name = default_jack_name; 1858 uint8_t iJack; 1859 int res; 1860 1861 struct snd_rawmidi_substream *substream = 1862 snd_usbmidi_find_substream(umidi, stream, number); 1863 if (!substream) { 1864 dev_err(&umidi->dev->dev, "substream %d:%d not found\n", stream, 1865 number); 1866 return; 1867 } 1868 1869 intf = umidi->iface; 1870 if (intf && jack_id >= 0) { 1871 hostif = intf->cur_altsetting; 1872 iJack = find_usb_ijack(hostif, jack_id); 1873 if (iJack != 0) { 1874 res = usb_string(umidi->dev, iJack, jack_name_buf, 1875 ARRAY_SIZE(jack_name_buf)); 1876 if (res) 1877 jack_name = jack_name_buf; 1878 } 1879 } 1880 1881 port_info = find_port_info(umidi, number); 1882 if (port_info || jack_name == default_jack_name || 1883 strncmp(umidi->card->shortname, jack_name, strlen(umidi->card->shortname)) != 0) { 1884 name_format = port_info ? port_info->name : 1885 (jack_name != default_jack_name ? "%s %s" : "%s %s %d"); 1886 snprintf(substream->name, sizeof(substream->name), 1887 name_format, umidi->card->shortname, jack_name, number + 1); 1888 } else { 1889 /* The manufacturer included the iProduct name in the jack 1890 * name, do not use both 1891 */ 1892 strscpy(substream->name, jack_name); 1893 } 1894 1895 *rsubstream = substream; 1896 } 1897 1898 /* 1899 * Creates the endpoints and their ports. 1900 */ 1901 static int snd_usbmidi_create_endpoints(struct snd_usb_midi *umidi, 1902 struct snd_usb_midi_endpoint_info *endpoints) 1903 { 1904 int i, j, err; 1905 int out_ports = 0, in_ports = 0; 1906 1907 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1908 if (endpoints[i].out_cables) { 1909 err = snd_usbmidi_out_endpoint_create(umidi, 1910 &endpoints[i], 1911 &umidi->endpoints[i]); 1912 if (err < 0) 1913 return err; 1914 } 1915 if (endpoints[i].in_cables) { 1916 err = snd_usbmidi_in_endpoint_create(umidi, 1917 &endpoints[i], 1918 &umidi->endpoints[i]); 1919 if (err < 0) 1920 return err; 1921 } 1922 1923 for (j = 0; j < 0x10; ++j) { 1924 if (endpoints[i].out_cables & (1 << j)) { 1925 snd_usbmidi_init_substream(umidi, 1926 SNDRV_RAWMIDI_STREAM_OUTPUT, 1927 out_ports, 1928 endpoints[i].assoc_out_jacks[j], 1929 &umidi->endpoints[i].out->ports[j].substream); 1930 ++out_ports; 1931 } 1932 if (endpoints[i].in_cables & (1 << j)) { 1933 snd_usbmidi_init_substream(umidi, 1934 SNDRV_RAWMIDI_STREAM_INPUT, 1935 in_ports, 1936 endpoints[i].assoc_in_jacks[j], 1937 &umidi->endpoints[i].in->ports[j].substream); 1938 ++in_ports; 1939 } 1940 } 1941 } 1942 dev_dbg(&umidi->dev->dev, "created %d output and %d input ports\n", 1943 out_ports, in_ports); 1944 return 0; 1945 } 1946 1947 static struct usb_ms_endpoint_descriptor *find_usb_ms_endpoint_descriptor( 1948 struct usb_host_endpoint *hostep) 1949 { 1950 unsigned char *extra = hostep->extra; 1951 int extralen = hostep->extralen; 1952 1953 while (extralen > 3) { 1954 struct usb_ms_endpoint_descriptor *ms_ep = 1955 (struct usb_ms_endpoint_descriptor *)extra; 1956 int length = ms_ep->bLength; 1957 1958 if (!length || length > extralen) 1959 break; 1960 1961 if (length > 3 && 1962 ms_ep->bDescriptorType == USB_DT_CS_ENDPOINT && 1963 ms_ep->bDescriptorSubtype == UAC_MS_GENERAL) 1964 return ms_ep; 1965 extralen -= length; 1966 extra += length; 1967 } 1968 return NULL; 1969 } 1970 1971 /* 1972 * Returns MIDIStreaming device capabilities. 1973 */ 1974 static int snd_usbmidi_get_ms_info(struct snd_usb_midi *umidi, 1975 struct snd_usb_midi_endpoint_info *endpoints) 1976 { 1977 struct usb_interface *intf; 1978 struct usb_host_interface *hostif; 1979 struct usb_interface_descriptor *intfd; 1980 struct usb_ms_header_descriptor *ms_header; 1981 struct usb_host_endpoint *hostep; 1982 struct usb_endpoint_descriptor *ep; 1983 struct usb_ms_endpoint_descriptor *ms_ep; 1984 int i, j, epidx; 1985 1986 intf = umidi->iface; 1987 if (!intf) 1988 return -ENXIO; 1989 hostif = &intf->altsetting[0]; 1990 intfd = get_iface_desc(hostif); 1991 ms_header = (struct usb_ms_header_descriptor *)hostif->extra; 1992 if (hostif->extralen >= 7 && 1993 ms_header->bLength >= 7 && 1994 ms_header->bDescriptorType == USB_DT_CS_INTERFACE && 1995 ms_header->bDescriptorSubtype == UAC_HEADER) 1996 dev_dbg(&umidi->dev->dev, "MIDIStreaming version %02x.%02x\n", 1997 ((uint8_t *)&ms_header->bcdMSC)[1], ((uint8_t *)&ms_header->bcdMSC)[0]); 1998 else 1999 dev_warn(&umidi->dev->dev, 2000 "MIDIStreaming interface descriptor not found\n"); 2001 2002 epidx = 0; 2003 for (i = 0; i < intfd->bNumEndpoints; ++i) { 2004 hostep = &hostif->endpoint[i]; 2005 ep = get_ep_desc(hostep); 2006 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep)) 2007 continue; 2008 ms_ep = find_usb_ms_endpoint_descriptor(hostep); 2009 if (!ms_ep) 2010 continue; 2011 if (ms_ep->bLength <= sizeof(*ms_ep)) 2012 continue; 2013 if (ms_ep->bNumEmbMIDIJack > 0x10) 2014 continue; 2015 if (ms_ep->bLength < sizeof(*ms_ep) + ms_ep->bNumEmbMIDIJack) 2016 continue; 2017 if (usb_endpoint_dir_out(ep)) { 2018 if (endpoints[epidx].out_ep) { 2019 if (++epidx >= MIDI_MAX_ENDPOINTS) { 2020 dev_warn(&umidi->dev->dev, 2021 "too many endpoints\n"); 2022 break; 2023 } 2024 } 2025 endpoints[epidx].out_ep = usb_endpoint_num(ep); 2026 if (usb_endpoint_xfer_int(ep)) 2027 endpoints[epidx].out_interval = ep->bInterval; 2028 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) 2029 /* 2030 * Low speed bulk transfers don't exist, so 2031 * force interrupt transfers for devices like 2032 * ESI MIDI Mate that try to use them anyway. 2033 */ 2034 endpoints[epidx].out_interval = 1; 2035 endpoints[epidx].out_cables = 2036 (1 << ms_ep->bNumEmbMIDIJack) - 1; 2037 for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j) 2038 endpoints[epidx].assoc_out_jacks[j] = ms_ep->baAssocJackID[j]; 2039 for (; j < ARRAY_SIZE(endpoints[epidx].assoc_out_jacks); ++j) 2040 endpoints[epidx].assoc_out_jacks[j] = -1; 2041 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n", 2042 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); 2043 } else { 2044 if (endpoints[epidx].in_ep) { 2045 if (++epidx >= MIDI_MAX_ENDPOINTS) { 2046 dev_warn(&umidi->dev->dev, 2047 "too many endpoints\n"); 2048 break; 2049 } 2050 } 2051 endpoints[epidx].in_ep = usb_endpoint_num(ep); 2052 if (usb_endpoint_xfer_int(ep)) 2053 endpoints[epidx].in_interval = ep->bInterval; 2054 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) 2055 endpoints[epidx].in_interval = 1; 2056 endpoints[epidx].in_cables = 2057 (1 << ms_ep->bNumEmbMIDIJack) - 1; 2058 for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j) 2059 endpoints[epidx].assoc_in_jacks[j] = ms_ep->baAssocJackID[j]; 2060 for (; j < ARRAY_SIZE(endpoints[epidx].assoc_in_jacks); ++j) 2061 endpoints[epidx].assoc_in_jacks[j] = -1; 2062 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n", 2063 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); 2064 } 2065 } 2066 return 0; 2067 } 2068 2069 static int roland_load_info(struct snd_kcontrol *kcontrol, 2070 struct snd_ctl_elem_info *info) 2071 { 2072 static const char *const names[] = { "High Load", "Light Load" }; 2073 2074 return snd_ctl_enum_info(info, 1, 2, names); 2075 } 2076 2077 static int roland_load_get(struct snd_kcontrol *kcontrol, 2078 struct snd_ctl_elem_value *value) 2079 { 2080 value->value.enumerated.item[0] = kcontrol->private_value; 2081 return 0; 2082 } 2083 2084 static int roland_load_put(struct snd_kcontrol *kcontrol, 2085 struct snd_ctl_elem_value *value) 2086 { 2087 struct snd_usb_midi *umidi = snd_kcontrol_chip(kcontrol); 2088 int changed; 2089 2090 if (value->value.enumerated.item[0] > 1) 2091 return -EINVAL; 2092 guard(mutex)(&umidi->mutex); 2093 changed = value->value.enumerated.item[0] != kcontrol->private_value; 2094 if (changed) 2095 kcontrol->private_value = value->value.enumerated.item[0]; 2096 return changed; 2097 } 2098 2099 static const struct snd_kcontrol_new roland_load_ctl = { 2100 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2101 .name = "MIDI Input Mode", 2102 .info = roland_load_info, 2103 .get = roland_load_get, 2104 .put = roland_load_put, 2105 .private_value = 1, 2106 }; 2107 2108 /* 2109 * On Roland devices, use the second alternate setting to be able to use 2110 * the interrupt input endpoint. 2111 */ 2112 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi *umidi) 2113 { 2114 struct usb_interface *intf; 2115 struct usb_host_interface *hostif; 2116 struct usb_interface_descriptor *intfd; 2117 2118 intf = umidi->iface; 2119 if (!intf || intf->num_altsetting != 2) 2120 return; 2121 2122 hostif = &intf->altsetting[1]; 2123 intfd = get_iface_desc(hostif); 2124 /* If either or both of the endpoints support interrupt transfer, 2125 * then use the alternate setting 2126 */ 2127 if (intfd->bNumEndpoints != 2 || 2128 !((get_endpoint(hostif, 0)->bmAttributes & 2129 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT || 2130 (get_endpoint(hostif, 1)->bmAttributes & 2131 USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) 2132 return; 2133 2134 dev_dbg(&umidi->dev->dev, "switching to altsetting %d with int ep\n", 2135 intfd->bAlternateSetting); 2136 usb_set_interface(umidi->dev, intfd->bInterfaceNumber, 2137 intfd->bAlternateSetting); 2138 2139 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi); 2140 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0) 2141 umidi->roland_load_ctl = NULL; 2142 } 2143 2144 /* 2145 * Try to find any usable endpoints in the interface. 2146 */ 2147 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi *umidi, 2148 struct snd_usb_midi_endpoint_info *endpoint, 2149 int max_endpoints) 2150 { 2151 struct usb_interface *intf; 2152 struct usb_host_interface *hostif; 2153 struct usb_interface_descriptor *intfd; 2154 struct usb_endpoint_descriptor *epd; 2155 int i, out_eps = 0, in_eps = 0; 2156 2157 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582) 2158 snd_usbmidi_switch_roland_altsetting(umidi); 2159 2160 if (endpoint[0].out_ep || endpoint[0].in_ep) 2161 return 0; 2162 2163 intf = umidi->iface; 2164 if (!intf || intf->num_altsetting < 1) 2165 return -ENOENT; 2166 hostif = intf->cur_altsetting; 2167 intfd = get_iface_desc(hostif); 2168 2169 for (i = 0; i < intfd->bNumEndpoints; ++i) { 2170 epd = get_endpoint(hostif, i); 2171 if (!usb_endpoint_xfer_bulk(epd) && 2172 !usb_endpoint_xfer_int(epd)) 2173 continue; 2174 if (out_eps < max_endpoints && 2175 usb_endpoint_dir_out(epd)) { 2176 endpoint[out_eps].out_ep = usb_endpoint_num(epd); 2177 if (usb_endpoint_xfer_int(epd)) 2178 endpoint[out_eps].out_interval = epd->bInterval; 2179 ++out_eps; 2180 } 2181 if (in_eps < max_endpoints && 2182 usb_endpoint_dir_in(epd)) { 2183 endpoint[in_eps].in_ep = usb_endpoint_num(epd); 2184 if (usb_endpoint_xfer_int(epd)) 2185 endpoint[in_eps].in_interval = epd->bInterval; 2186 ++in_eps; 2187 } 2188 } 2189 return (out_eps || in_eps) ? 0 : -ENOENT; 2190 } 2191 2192 /* 2193 * Detects the endpoints for one-port-per-endpoint protocols. 2194 */ 2195 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi *umidi, 2196 struct snd_usb_midi_endpoint_info *endpoints) 2197 { 2198 int err, i; 2199 2200 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS); 2201 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 2202 if (endpoints[i].out_ep) 2203 endpoints[i].out_cables = 0x0001; 2204 if (endpoints[i].in_ep) 2205 endpoints[i].in_cables = 0x0001; 2206 } 2207 return err; 2208 } 2209 2210 /* 2211 * Detects the endpoints and ports of Yamaha devices. 2212 */ 2213 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi *umidi, 2214 struct snd_usb_midi_endpoint_info *endpoint) 2215 { 2216 struct usb_interface *intf; 2217 struct usb_host_interface *hostif; 2218 struct usb_interface_descriptor *intfd; 2219 uint8_t *cs_desc; 2220 2221 intf = umidi->iface; 2222 if (!intf) 2223 return -ENOENT; 2224 hostif = intf->altsetting; 2225 intfd = get_iface_desc(hostif); 2226 if (intfd->bNumEndpoints < 1) 2227 return -ENOENT; 2228 2229 /* 2230 * For each port there is one MIDI_IN/OUT_JACK descriptor, not 2231 * necessarily with any useful contents. So simply count 'em. 2232 */ 2233 for (cs_desc = hostif->extra; 2234 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2; 2235 cs_desc += cs_desc[0]) { 2236 if (cs_desc[1] == USB_DT_CS_INTERFACE) { 2237 if (cs_desc[2] == UAC_MIDI_IN_JACK) 2238 endpoint->in_cables = 2239 (endpoint->in_cables << 1) | 1; 2240 else if (cs_desc[2] == UAC_MIDI_OUT_JACK) 2241 endpoint->out_cables = 2242 (endpoint->out_cables << 1) | 1; 2243 } 2244 } 2245 if (!endpoint->in_cables && !endpoint->out_cables) 2246 return -ENOENT; 2247 2248 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1); 2249 } 2250 2251 /* 2252 * Detects the endpoints and ports of Roland devices. 2253 */ 2254 static int snd_usbmidi_detect_roland(struct snd_usb_midi *umidi, 2255 struct snd_usb_midi_endpoint_info *endpoint) 2256 { 2257 struct usb_interface *intf; 2258 struct usb_host_interface *hostif; 2259 u8 *cs_desc; 2260 2261 intf = umidi->iface; 2262 if (!intf) 2263 return -ENOENT; 2264 hostif = intf->altsetting; 2265 /* 2266 * Some devices have a descriptor <06 24 F1 02 <inputs> <outputs>>, 2267 * some have standard class descriptors, or both kinds, or neither. 2268 */ 2269 for (cs_desc = hostif->extra; 2270 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2; 2271 cs_desc += cs_desc[0]) { 2272 if (cs_desc[0] >= 6 && 2273 cs_desc[1] == USB_DT_CS_INTERFACE && 2274 cs_desc[2] == 0xf1 && 2275 cs_desc[3] == 0x02) { 2276 if (cs_desc[4] > 0x10 || cs_desc[5] > 0x10) 2277 continue; 2278 endpoint->in_cables = (1 << cs_desc[4]) - 1; 2279 endpoint->out_cables = (1 << cs_desc[5]) - 1; 2280 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1); 2281 } else if (cs_desc[0] >= 7 && 2282 cs_desc[1] == USB_DT_CS_INTERFACE && 2283 cs_desc[2] == UAC_HEADER) { 2284 return snd_usbmidi_get_ms_info(umidi, endpoint); 2285 } 2286 } 2287 2288 return -ENODEV; 2289 } 2290 2291 /* 2292 * Creates the endpoints and their ports for Midiman devices. 2293 */ 2294 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi *umidi, 2295 struct snd_usb_midi_endpoint_info *endpoint) 2296 { 2297 struct snd_usb_midi_endpoint_info ep_info; 2298 struct usb_interface *intf; 2299 struct usb_host_interface *hostif; 2300 struct usb_interface_descriptor *intfd; 2301 struct usb_endpoint_descriptor *epd; 2302 int cable, err; 2303 2304 intf = umidi->iface; 2305 if (!intf) 2306 return -ENOENT; 2307 hostif = intf->altsetting; 2308 intfd = get_iface_desc(hostif); 2309 /* 2310 * The various MidiSport devices have more or less random endpoint 2311 * numbers, so we have to identify the endpoints by their index in 2312 * the descriptor array, like the driver for that other OS does. 2313 * 2314 * There is one interrupt input endpoint for all input ports, one 2315 * bulk output endpoint for even-numbered ports, and one for odd- 2316 * numbered ports. Both bulk output endpoints have corresponding 2317 * input bulk endpoints (at indices 1 and 3) which aren't used. 2318 */ 2319 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) { 2320 dev_dbg(&umidi->dev->dev, "not enough endpoints\n"); 2321 return -ENOENT; 2322 } 2323 2324 epd = get_endpoint(hostif, 0); 2325 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) { 2326 dev_dbg(&umidi->dev->dev, "endpoint[0] isn't interrupt\n"); 2327 return -ENXIO; 2328 } 2329 epd = get_endpoint(hostif, 2); 2330 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) { 2331 dev_dbg(&umidi->dev->dev, "endpoint[2] isn't bulk output\n"); 2332 return -ENXIO; 2333 } 2334 if (endpoint->out_cables > 0x0001) { 2335 epd = get_endpoint(hostif, 4); 2336 if (!usb_endpoint_dir_out(epd) || 2337 !usb_endpoint_xfer_bulk(epd)) { 2338 dev_dbg(&umidi->dev->dev, 2339 "endpoint[4] isn't bulk output\n"); 2340 return -ENXIO; 2341 } 2342 } 2343 2344 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & 2345 USB_ENDPOINT_NUMBER_MASK; 2346 ep_info.out_interval = 0; 2347 ep_info.out_cables = endpoint->out_cables & 0x5555; 2348 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, 2349 &umidi->endpoints[0]); 2350 if (err < 0) 2351 return err; 2352 2353 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & 2354 USB_ENDPOINT_NUMBER_MASK; 2355 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval; 2356 ep_info.in_cables = endpoint->in_cables; 2357 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, 2358 &umidi->endpoints[0]); 2359 if (err < 0) 2360 return err; 2361 2362 if (endpoint->out_cables > 0x0001) { 2363 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & 2364 USB_ENDPOINT_NUMBER_MASK; 2365 ep_info.out_cables = endpoint->out_cables & 0xaaaa; 2366 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, 2367 &umidi->endpoints[1]); 2368 if (err < 0) 2369 return err; 2370 } 2371 2372 for (cable = 0; cable < 0x10; ++cable) { 2373 if (endpoint->out_cables & (1 << cable)) 2374 snd_usbmidi_init_substream(umidi, 2375 SNDRV_RAWMIDI_STREAM_OUTPUT, 2376 cable, 2377 -1 /* prevent trying to find jack */, 2378 &umidi->endpoints[cable & 1].out->ports[cable].substream); 2379 if (endpoint->in_cables & (1 << cable)) 2380 snd_usbmidi_init_substream(umidi, 2381 SNDRV_RAWMIDI_STREAM_INPUT, 2382 cable, 2383 -1 /* prevent trying to find jack */, 2384 &umidi->endpoints[0].in->ports[cable].substream); 2385 } 2386 return 0; 2387 } 2388 2389 static const struct snd_rawmidi_global_ops snd_usbmidi_ops = { 2390 .get_port_info = snd_usbmidi_get_port_info, 2391 }; 2392 2393 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi *umidi, 2394 int out_ports, int in_ports) 2395 { 2396 struct snd_rawmidi *rmidi; 2397 int err; 2398 2399 err = snd_rawmidi_new(umidi->card, "USB MIDI", 2400 umidi->next_midi_device++, 2401 out_ports, in_ports, &rmidi); 2402 if (err < 0) 2403 return err; 2404 strscpy(rmidi->name, umidi->card->shortname); 2405 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | 2406 SNDRV_RAWMIDI_INFO_INPUT | 2407 SNDRV_RAWMIDI_INFO_DUPLEX; 2408 rmidi->ops = &snd_usbmidi_ops; 2409 rmidi->private_data = umidi; 2410 rmidi->private_free = snd_usbmidi_rawmidi_free; 2411 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, 2412 &snd_usbmidi_output_ops); 2413 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, 2414 &snd_usbmidi_input_ops); 2415 2416 umidi->rmidi = rmidi; 2417 return 0; 2418 } 2419 2420 /* 2421 * Temporarily stop input. 2422 */ 2423 void snd_usbmidi_input_stop(struct list_head *p) 2424 { 2425 struct snd_usb_midi *umidi; 2426 unsigned int i, j; 2427 2428 umidi = list_entry(p, struct snd_usb_midi, list); 2429 if (!umidi->input_running) 2430 return; 2431 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 2432 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i]; 2433 if (ep->in) 2434 for (j = 0; j < INPUT_URBS; ++j) 2435 usb_kill_urb(ep->in->urbs[j]); 2436 } 2437 umidi->input_running = 0; 2438 } 2439 EXPORT_SYMBOL(snd_usbmidi_input_stop); 2440 2441 static void snd_usbmidi_input_start_ep(struct snd_usb_midi *umidi, 2442 struct snd_usb_midi_in_endpoint *ep) 2443 { 2444 unsigned int i; 2445 2446 if (!ep) 2447 return; 2448 for (i = 0; i < INPUT_URBS; ++i) { 2449 struct urb *urb = ep->urbs[i]; 2450 scoped_guard(spinlock_irqsave, &umidi->disc_lock) { 2451 if (!atomic_read(&urb->use_count)) { 2452 urb->dev = ep->umidi->dev; 2453 snd_usbmidi_submit_urb(urb, GFP_ATOMIC); 2454 } 2455 } 2456 } 2457 } 2458 2459 /* 2460 * Resume input after a call to snd_usbmidi_input_stop(). 2461 */ 2462 void snd_usbmidi_input_start(struct list_head *p) 2463 { 2464 struct snd_usb_midi *umidi; 2465 int i; 2466 2467 umidi = list_entry(p, struct snd_usb_midi, list); 2468 if (umidi->input_running || !umidi->opened[1]) 2469 return; 2470 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) 2471 snd_usbmidi_input_start_ep(umidi, umidi->endpoints[i].in); 2472 umidi->input_running = 1; 2473 } 2474 EXPORT_SYMBOL(snd_usbmidi_input_start); 2475 2476 /* 2477 * Prepare for suspend. Typically called from the USB suspend callback. 2478 */ 2479 void snd_usbmidi_suspend(struct list_head *p) 2480 { 2481 struct snd_usb_midi *umidi; 2482 2483 umidi = list_entry(p, struct snd_usb_midi, list); 2484 guard(mutex)(&umidi->mutex); 2485 snd_usbmidi_input_stop(p); 2486 } 2487 EXPORT_SYMBOL(snd_usbmidi_suspend); 2488 2489 /* 2490 * Resume. Typically called from the USB resume callback. 2491 */ 2492 void snd_usbmidi_resume(struct list_head *p) 2493 { 2494 struct snd_usb_midi *umidi; 2495 2496 umidi = list_entry(p, struct snd_usb_midi, list); 2497 guard(mutex)(&umidi->mutex); 2498 snd_usbmidi_input_start(p); 2499 } 2500 EXPORT_SYMBOL(snd_usbmidi_resume); 2501 2502 /* 2503 * Creates and registers everything needed for a MIDI streaming interface. 2504 */ 2505 int __snd_usbmidi_create(struct snd_card *card, 2506 struct usb_interface *iface, 2507 struct list_head *midi_list, 2508 const struct snd_usb_audio_quirk *quirk, 2509 unsigned int usb_id, 2510 unsigned int *num_rawmidis) 2511 { 2512 struct snd_usb_midi *umidi; 2513 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS]; 2514 int out_ports, in_ports; 2515 int i, err; 2516 2517 umidi = kzalloc_obj(*umidi); 2518 if (!umidi) 2519 return -ENOMEM; 2520 umidi->dev = interface_to_usbdev(iface); 2521 umidi->card = card; 2522 umidi->iface = iface; 2523 umidi->quirk = quirk; 2524 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops; 2525 if (num_rawmidis) 2526 umidi->next_midi_device = *num_rawmidis; 2527 spin_lock_init(&umidi->disc_lock); 2528 init_rwsem(&umidi->disc_rwsem); 2529 mutex_init(&umidi->mutex); 2530 if (!usb_id) 2531 usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor), 2532 le16_to_cpu(umidi->dev->descriptor.idProduct)); 2533 umidi->usb_id = usb_id; 2534 timer_setup(&umidi->error_timer, snd_usbmidi_error_timer, 0); 2535 2536 /* detect the endpoint(s) to use */ 2537 memset(endpoints, 0, sizeof(endpoints)); 2538 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) { 2539 case QUIRK_MIDI_STANDARD_INTERFACE: 2540 err = snd_usbmidi_get_ms_info(umidi, endpoints); 2541 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */ 2542 umidi->usb_protocol_ops = 2543 &snd_usbmidi_maudio_broken_running_status_ops; 2544 break; 2545 case QUIRK_MIDI_US122L: 2546 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops; 2547 fallthrough; 2548 case QUIRK_MIDI_FIXED_ENDPOINT: 2549 memcpy(&endpoints[0], quirk->data, 2550 sizeof(struct snd_usb_midi_endpoint_info)); 2551 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); 2552 break; 2553 case QUIRK_MIDI_YAMAHA: 2554 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]); 2555 break; 2556 case QUIRK_MIDI_ROLAND: 2557 err = snd_usbmidi_detect_roland(umidi, &endpoints[0]); 2558 break; 2559 case QUIRK_MIDI_MIDIMAN: 2560 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops; 2561 memcpy(&endpoints[0], quirk->data, 2562 sizeof(struct snd_usb_midi_endpoint_info)); 2563 err = 0; 2564 break; 2565 case QUIRK_MIDI_NOVATION: 2566 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops; 2567 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2568 break; 2569 case QUIRK_MIDI_RAW_BYTES: 2570 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops; 2571 /* 2572 * Interface 1 contains isochronous endpoints, but with the same 2573 * numbers as in interface 0. Since it is interface 1 that the 2574 * USB core has most recently seen, these descriptors are now 2575 * associated with the endpoint numbers. This will foul up our 2576 * attempts to submit bulk/interrupt URBs to the endpoints in 2577 * interface 0, so we have to make sure that the USB core looks 2578 * again at interface 0 by calling usb_set_interface() on it. 2579 */ 2580 if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */ 2581 usb_set_interface(umidi->dev, 0, 0); 2582 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2583 break; 2584 case QUIRK_MIDI_EMAGIC: 2585 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops; 2586 memcpy(&endpoints[0], quirk->data, 2587 sizeof(struct snd_usb_midi_endpoint_info)); 2588 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); 2589 break; 2590 case QUIRK_MIDI_CME: 2591 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops; 2592 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2593 break; 2594 case QUIRK_MIDI_AKAI: 2595 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops; 2596 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2597 /* endpoint 1 is input-only */ 2598 endpoints[1].out_cables = 0; 2599 break; 2600 case QUIRK_MIDI_FTDI: 2601 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops; 2602 2603 /* set baud rate to 31250 (48 MHz / 16 / 96) */ 2604 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0), 2605 3, 0x40, 0x60, 0, NULL, 0, 1000); 2606 if (err < 0) 2607 break; 2608 2609 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2610 break; 2611 case QUIRK_MIDI_CH345: 2612 umidi->usb_protocol_ops = &snd_usbmidi_ch345_broken_sysex_ops; 2613 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 2614 break; 2615 default: 2616 dev_err(&umidi->dev->dev, "invalid quirk type %d\n", 2617 quirk->type); 2618 err = -ENXIO; 2619 break; 2620 } 2621 if (err < 0) 2622 goto free_midi; 2623 2624 /* create rawmidi device */ 2625 out_ports = 0; 2626 in_ports = 0; 2627 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 2628 out_ports += hweight16(endpoints[i].out_cables); 2629 in_ports += hweight16(endpoints[i].in_cables); 2630 } 2631 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports); 2632 if (err < 0) 2633 goto free_midi; 2634 2635 /* create endpoint/port structures */ 2636 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN) 2637 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]); 2638 else 2639 err = snd_usbmidi_create_endpoints(umidi, endpoints); 2640 if (err < 0) 2641 goto exit; 2642 2643 usb_autopm_get_interface_no_resume(umidi->iface); 2644 2645 list_add_tail(&umidi->list, midi_list); 2646 if (num_rawmidis) 2647 *num_rawmidis = umidi->next_midi_device; 2648 return 0; 2649 2650 free_midi: 2651 kfree(umidi); 2652 exit: 2653 return err; 2654 } 2655 EXPORT_SYMBOL(__snd_usbmidi_create); 2656