1/* 2 * usbmidi.c - ALSA USB MIDI driver 3 * 4 * Copyright (c) 2002-2007 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 <sound/driver.h> 39#include <linux/kernel.h> 40#include <linux/types.h> 41#include <linux/bitops.h> 42#include <linux/interrupt.h> 43#include <linux/spinlock.h> 44#include <linux/string.h> 45#include <linux/init.h> 46#include <linux/slab.h> 47#include <linux/timer.h> 48#include <linux/usb.h> 49#include <sound/core.h> 50#include <sound/rawmidi.h> 51#include <sound/asequencer.h> 52#include "usbaudio.h" 53 54 55/* 56 * define this to log all USB packets 57 */ 58/* #define DUMP_PACKETS */ 59 60/* 61 * how long to wait after some USB errors, so that khubd can disconnect() us 62 * without too many spurious errors 63 */ 64#define ERROR_DELAY_JIFFIES (HZ / 10) 65 66 67MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 68MODULE_DESCRIPTION("USB Audio/MIDI helper module"); 69MODULE_LICENSE("Dual BSD/GPL"); 70 71 72struct usb_ms_header_descriptor { 73 __u8 bLength; 74 __u8 bDescriptorType; 75 __u8 bDescriptorSubtype; 76 __u8 bcdMSC[2]; 77 __le16 wTotalLength; 78} __attribute__ ((packed)); 79 80struct usb_ms_endpoint_descriptor { 81 __u8 bLength; 82 __u8 bDescriptorType; 83 __u8 bDescriptorSubtype; 84 __u8 bNumEmbMIDIJack; 85 __u8 baAssocJackID[0]; 86} __attribute__ ((packed)); 87 88struct snd_usb_midi_in_endpoint; 89struct snd_usb_midi_out_endpoint; 90struct snd_usb_midi_endpoint; 91 92struct usb_protocol_ops { 93 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int); 94 void (*output)(struct snd_usb_midi_out_endpoint*); 95 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t); 96 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*); 97 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*); 98}; 99 100struct snd_usb_midi { 101 struct snd_usb_audio *chip; 102 struct usb_interface *iface; 103 const struct snd_usb_audio_quirk *quirk; 104 struct snd_rawmidi *rmidi; 105 struct usb_protocol_ops* usb_protocol_ops; 106 struct list_head list; 107 struct timer_list error_timer; 108 109 struct snd_usb_midi_endpoint { 110 struct snd_usb_midi_out_endpoint *out; 111 struct snd_usb_midi_in_endpoint *in; 112 } endpoints[MIDI_MAX_ENDPOINTS]; 113 unsigned long input_triggered; 114}; 115 116struct snd_usb_midi_out_endpoint { 117 struct snd_usb_midi* umidi; 118 struct urb* urb; 119 int urb_active; 120 int max_transfer; /* size of urb buffer */ 121 struct tasklet_struct tasklet; 122 123 spinlock_t buffer_lock; 124 125 struct usbmidi_out_port { 126 struct snd_usb_midi_out_endpoint* ep; 127 struct snd_rawmidi_substream *substream; 128 int active; 129 uint8_t cable; /* cable number << 4 */ 130 uint8_t state; 131#define STATE_UNKNOWN 0 132#define STATE_1PARAM 1 133#define STATE_2PARAM_1 2 134#define STATE_2PARAM_2 3 135#define STATE_SYSEX_0 4 136#define STATE_SYSEX_1 5 137#define STATE_SYSEX_2 6 138 uint8_t data[2]; 139 } ports[0x10]; 140 int current_port; 141}; 142 143struct snd_usb_midi_in_endpoint { 144 struct snd_usb_midi* umidi; 145 struct urb* urb; 146 struct usbmidi_in_port { 147 struct snd_rawmidi_substream *substream; 148 u8 running_status_length; 149 } ports[0x10]; 150 u8 seen_f5; 151 u8 error_resubmit; 152 int current_port; 153}; 154 155static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep); 156 157static const uint8_t snd_usbmidi_cin_length[] = { 158 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1 159}; 160 161/* 162 * Submits the URB, with error handling. 163 */ 164static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags) 165{ 166 int err = usb_submit_urb(urb, flags); 167 if (err < 0 && err != -ENODEV) 168 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err); 169 return err; 170} 171 172/* 173 * Error handling for URB completion functions. 174 */ 175static int snd_usbmidi_urb_error(int status) 176{ 177 switch (status) { 178 /* manually unlinked, or device gone */ 179 case -ENOENT: 180 case -ECONNRESET: 181 case -ESHUTDOWN: 182 case -ENODEV: 183 return -ENODEV; 184 /* errors that might occur during unplugging */ 185 case -EPROTO: 186 case -ETIME: 187 case -EILSEQ: 188 return -EIO; 189 default: 190 snd_printk(KERN_ERR "urb status %d\n", status); 191 return 0; /* continue */ 192 } 193} 194 195/* 196 * Receives a chunk of MIDI data. 197 */ 198static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx, 199 uint8_t* data, int length) 200{ 201 struct usbmidi_in_port* port = &ep->ports[portidx]; 202 203 if (!port->substream) { 204 snd_printd("unexpected port %d!\n", portidx); 205 return; 206 } 207 if (!test_bit(port->substream->number, &ep->umidi->input_triggered)) 208 return; 209 snd_rawmidi_receive(port->substream, data, length); 210} 211 212#ifdef DUMP_PACKETS 213static void dump_urb(const char *type, const u8 *data, int length) 214{ 215 snd_printk(KERN_DEBUG "%s packet: [", type); 216 for (; length > 0; ++data, --length) 217 printk(" %02x", *data); 218 printk(" ]\n"); 219} 220#else 221#define dump_urb(type, data, length) /* nothing */ 222#endif 223 224/* 225 * Processes the data read from the device. 226 */ 227static void snd_usbmidi_in_urb_complete(struct urb* urb) 228{ 229 struct snd_usb_midi_in_endpoint* ep = urb->context; 230 231 if (urb->status == 0) { 232 dump_urb("received", urb->transfer_buffer, urb->actual_length); 233 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer, 234 urb->actual_length); 235 } else { 236 int err = snd_usbmidi_urb_error(urb->status); 237 if (err < 0) { 238 if (err != -ENODEV) { 239 ep->error_resubmit = 1; 240 mod_timer(&ep->umidi->error_timer, 241 jiffies + ERROR_DELAY_JIFFIES); 242 } 243 return; 244 } 245 } 246 247 urb->dev = ep->umidi->chip->dev; 248 snd_usbmidi_submit_urb(urb, GFP_ATOMIC); 249} 250 251static void snd_usbmidi_out_urb_complete(struct urb* urb) 252{ 253 struct snd_usb_midi_out_endpoint* ep = urb->context; 254 255 spin_lock(&ep->buffer_lock); 256 ep->urb_active = 0; 257 spin_unlock(&ep->buffer_lock); 258 if (urb->status < 0) { 259 int err = snd_usbmidi_urb_error(urb->status); 260 if (err < 0) { 261 if (err != -ENODEV) 262 mod_timer(&ep->umidi->error_timer, 263 jiffies + ERROR_DELAY_JIFFIES); 264 return; 265 } 266 } 267 snd_usbmidi_do_output(ep); 268} 269 270/* 271 * This is called when some data should be transferred to the device 272 * (from one or more substreams). 273 */ 274static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep) 275{ 276 struct urb* urb = ep->urb; 277 unsigned long flags; 278 279 spin_lock_irqsave(&ep->buffer_lock, flags); 280 if (ep->urb_active || ep->umidi->chip->shutdown) { 281 spin_unlock_irqrestore(&ep->buffer_lock, flags); 282 return; 283 } 284 285 urb->transfer_buffer_length = 0; 286 ep->umidi->usb_protocol_ops->output(ep); 287 288 if (urb->transfer_buffer_length > 0) { 289 dump_urb("sending", urb->transfer_buffer, 290 urb->transfer_buffer_length); 291 urb->dev = ep->umidi->chip->dev; 292 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0; 293 } 294 spin_unlock_irqrestore(&ep->buffer_lock, flags); 295} 296 297static void snd_usbmidi_out_tasklet(unsigned long data) 298{ 299 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data; 300 301 snd_usbmidi_do_output(ep); 302} 303 304/* called after transfers had been interrupted due to some USB error */ 305static void snd_usbmidi_error_timer(unsigned long data) 306{ 307 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data; 308 int i; 309 310 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 311 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in; 312 if (in && in->error_resubmit) { 313 in->error_resubmit = 0; 314 in->urb->dev = umidi->chip->dev; 315 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC); 316 } 317 if (umidi->endpoints[i].out) 318 snd_usbmidi_do_output(umidi->endpoints[i].out); 319 } 320} 321 322/* helper function to send static data that may not DMA-able */ 323static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep, 324 const void *data, int len) 325{ 326 int err; 327 void *buf = kmemdup(data, len, GFP_KERNEL); 328 if (!buf) 329 return -ENOMEM; 330 dump_urb("sending", buf, len); 331 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len, 332 NULL, 250); 333 kfree(buf); 334 return err; 335} 336 337/* 338 * Standard USB MIDI protocol: see the spec. 339 * Midiman protocol: like the standard protocol, but the control byte is the 340 * fourth byte in each packet, and uses length instead of CIN. 341 */ 342 343static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep, 344 uint8_t* buffer, int buffer_length) 345{ 346 int i; 347 348 for (i = 0; i + 3 < buffer_length; i += 4) 349 if (buffer[i] != 0) { 350 int cable = buffer[i] >> 4; 351 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f]; 352 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length); 353 } 354} 355 356static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep, 357 uint8_t* buffer, int buffer_length) 358{ 359 int i; 360 361 for (i = 0; i + 3 < buffer_length; i += 4) 362 if (buffer[i + 3] != 0) { 363 int port = buffer[i + 3] >> 4; 364 int length = buffer[i + 3] & 3; 365 snd_usbmidi_input_data(ep, port, &buffer[i], length); 366 } 367} 368 369/* 370 * Buggy M-Audio device: running status on input results in a packet that has 371 * the data bytes but not the status byte and that is marked with CIN 4. 372 */ 373static void snd_usbmidi_maudio_broken_running_status_input( 374 struct snd_usb_midi_in_endpoint* ep, 375 uint8_t* buffer, int buffer_length) 376{ 377 int i; 378 379 for (i = 0; i + 3 < buffer_length; i += 4) 380 if (buffer[i] != 0) { 381 int cable = buffer[i] >> 4; 382 u8 cin = buffer[i] & 0x0f; 383 struct usbmidi_in_port *port = &ep->ports[cable]; 384 int length; 385 386 length = snd_usbmidi_cin_length[cin]; 387 if (cin == 0xf && buffer[i + 1] >= 0xf8) 388 ; /* realtime msg: no running status change */ 389 else if (cin >= 0x8 && cin <= 0xe) 390 /* channel msg */ 391 port->running_status_length = length - 1; 392 else if (cin == 0x4 && 393 port->running_status_length != 0 && 394 buffer[i + 1] < 0x80) 395 /* CIN 4 that is not a SysEx */ 396 length = port->running_status_length; 397 else 398 /* 399 * All other msgs cannot begin running status. 400 * (A channel msg sent as two or three CIN 0xF 401 * packets could in theory, but this device 402 * doesn't use this format.) 403 */ 404 port->running_status_length = 0; 405 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length); 406 } 407} 408 409/* 410 * Adds one USB MIDI packet to the output buffer. 411 */ 412static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0, 413 uint8_t p1, uint8_t p2, uint8_t p3) 414{ 415 416 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length; 417 buf[0] = p0; 418 buf[1] = p1; 419 buf[2] = p2; 420 buf[3] = p3; 421 urb->transfer_buffer_length += 4; 422} 423 424/* 425 * Adds one Midiman packet to the output buffer. 426 */ 427static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0, 428 uint8_t p1, uint8_t p2, uint8_t p3) 429{ 430 431 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length; 432 buf[0] = p1; 433 buf[1] = p2; 434 buf[2] = p3; 435 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f]; 436 urb->transfer_buffer_length += 4; 437} 438 439/* 440 * Converts MIDI commands to USB MIDI packets. 441 */ 442static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port, 443 uint8_t b, struct urb* urb) 444{ 445 uint8_t p0 = port->cable; 446 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) = 447 port->ep->umidi->usb_protocol_ops->output_packet; 448 449 if (b >= 0xf8) { 450 output_packet(urb, p0 | 0x0f, b, 0, 0); 451 } else if (b >= 0xf0) { 452 switch (b) { 453 case 0xf0: 454 port->data[0] = b; 455 port->state = STATE_SYSEX_1; 456 break; 457 case 0xf1: 458 case 0xf3: 459 port->data[0] = b; 460 port->state = STATE_1PARAM; 461 break; 462 case 0xf2: 463 port->data[0] = b; 464 port->state = STATE_2PARAM_1; 465 break; 466 case 0xf4: 467 case 0xf5: 468 port->state = STATE_UNKNOWN; 469 break; 470 case 0xf6: 471 output_packet(urb, p0 | 0x05, 0xf6, 0, 0); 472 port->state = STATE_UNKNOWN; 473 break; 474 case 0xf7: 475 switch (port->state) { 476 case STATE_SYSEX_0: 477 output_packet(urb, p0 | 0x05, 0xf7, 0, 0); 478 break; 479 case STATE_SYSEX_1: 480 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0); 481 break; 482 case STATE_SYSEX_2: 483 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7); 484 break; 485 } 486 port->state = STATE_UNKNOWN; 487 break; 488 } 489 } else if (b >= 0x80) { 490 port->data[0] = b; 491 if (b >= 0xc0 && b <= 0xdf) 492 port->state = STATE_1PARAM; 493 else 494 port->state = STATE_2PARAM_1; 495 } else { /* b < 0x80 */ 496 switch (port->state) { 497 case STATE_1PARAM: 498 if (port->data[0] < 0xf0) { 499 p0 |= port->data[0] >> 4; 500 } else { 501 p0 |= 0x02; 502 port->state = STATE_UNKNOWN; 503 } 504 output_packet(urb, p0, port->data[0], b, 0); 505 break; 506 case STATE_2PARAM_1: 507 port->data[1] = b; 508 port->state = STATE_2PARAM_2; 509 break; 510 case STATE_2PARAM_2: 511 if (port->data[0] < 0xf0) { 512 p0 |= port->data[0] >> 4; 513 port->state = STATE_2PARAM_1; 514 } else { 515 p0 |= 0x03; 516 port->state = STATE_UNKNOWN; 517 } 518 output_packet(urb, p0, port->data[0], port->data[1], b); 519 break; 520 case STATE_SYSEX_0: 521 port->data[0] = b; 522 port->state = STATE_SYSEX_1; 523 break; 524 case STATE_SYSEX_1: 525 port->data[1] = b; 526 port->state = STATE_SYSEX_2; 527 break; 528 case STATE_SYSEX_2: 529 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b); 530 port->state = STATE_SYSEX_0; 531 break; 532 } 533 } 534} 535 536static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep) 537{ 538 struct urb* urb = ep->urb; 539 int p; 540 541 for (p = 0; p < 0x10; ++p) { 542 struct usbmidi_out_port* port = &ep->ports[p]; 543 if (!port->active) 544 continue; 545 while (urb->transfer_buffer_length + 3 < ep->max_transfer) { 546 uint8_t b; 547 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) { 548 port->active = 0; 549 break; 550 } 551 snd_usbmidi_transmit_byte(port, b, urb); 552 } 553 } 554} 555 556static struct usb_protocol_ops snd_usbmidi_standard_ops = { 557 .input = snd_usbmidi_standard_input, 558 .output = snd_usbmidi_standard_output, 559 .output_packet = snd_usbmidi_output_standard_packet, 560}; 561 562static struct usb_protocol_ops snd_usbmidi_midiman_ops = { 563 .input = snd_usbmidi_midiman_input, 564 .output = snd_usbmidi_standard_output, 565 .output_packet = snd_usbmidi_output_midiman_packet, 566}; 567 568static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = { 569 .input = snd_usbmidi_maudio_broken_running_status_input, 570 .output = snd_usbmidi_standard_output, 571 .output_packet = snd_usbmidi_output_standard_packet, 572}; 573 574/* 575 * Novation USB MIDI protocol: number of data bytes is in the first byte 576 * (when receiving) (+1!) or in the second byte (when sending); data begins 577 * at the third byte. 578 */ 579 580static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep, 581 uint8_t* buffer, int buffer_length) 582{ 583 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1) 584 return; 585 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1); 586} 587 588static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep) 589{ 590 uint8_t* transfer_buffer; 591 int count; 592 593 if (!ep->ports[0].active) 594 return; 595 transfer_buffer = ep->urb->transfer_buffer; 596 count = snd_rawmidi_transmit(ep->ports[0].substream, 597 &transfer_buffer[2], 598 ep->max_transfer - 2); 599 if (count < 1) { 600 ep->ports[0].active = 0; 601 return; 602 } 603 transfer_buffer[0] = 0; 604 transfer_buffer[1] = count; 605 ep->urb->transfer_buffer_length = 2 + count; 606} 607 608static struct usb_protocol_ops snd_usbmidi_novation_ops = { 609 .input = snd_usbmidi_novation_input, 610 .output = snd_usbmidi_novation_output, 611}; 612 613/* 614 * "raw" protocol: used by the MOTU FastLane. 615 */ 616 617static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep, 618 uint8_t* buffer, int buffer_length) 619{ 620 snd_usbmidi_input_data(ep, 0, buffer, buffer_length); 621} 622 623static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep) 624{ 625 int count; 626 627 if (!ep->ports[0].active) 628 return; 629 count = snd_rawmidi_transmit(ep->ports[0].substream, 630 ep->urb->transfer_buffer, 631 ep->max_transfer); 632 if (count < 1) { 633 ep->ports[0].active = 0; 634 return; 635 } 636 ep->urb->transfer_buffer_length = count; 637} 638 639static struct usb_protocol_ops snd_usbmidi_raw_ops = { 640 .input = snd_usbmidi_raw_input, 641 .output = snd_usbmidi_raw_output, 642}; 643 644/* 645 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching. 646 */ 647 648static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep) 649{ 650 static const u8 init_data[] = { 651 /* initialization magic: "get version" */ 652 0xf0, 653 0x00, 0x20, 0x31, /* Emagic */ 654 0x64, /* Unitor8 */ 655 0x0b, /* version number request */ 656 0x00, /* command version */ 657 0x00, /* EEPROM, box 0 */ 658 0xf7 659 }; 660 send_bulk_static_data(ep, init_data, sizeof(init_data)); 661 /* while we're at it, pour on more magic */ 662 send_bulk_static_data(ep, init_data, sizeof(init_data)); 663} 664 665static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep) 666{ 667 static const u8 finish_data[] = { 668 /* switch to patch mode with last preset */ 669 0xf0, 670 0x00, 0x20, 0x31, /* Emagic */ 671 0x64, /* Unitor8 */ 672 0x10, /* patch switch command */ 673 0x00, /* command version */ 674 0x7f, /* to all boxes */ 675 0x40, /* last preset in EEPROM */ 676 0xf7 677 }; 678 send_bulk_static_data(ep, finish_data, sizeof(finish_data)); 679} 680 681static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep, 682 uint8_t* buffer, int buffer_length) 683{ 684 int i; 685 686 /* FF indicates end of valid data */ 687 for (i = 0; i < buffer_length; ++i) 688 if (buffer[i] == 0xff) { 689 buffer_length = i; 690 break; 691 } 692 693 /* handle F5 at end of last buffer */ 694 if (ep->seen_f5) 695 goto switch_port; 696 697 while (buffer_length > 0) { 698 /* determine size of data until next F5 */ 699 for (i = 0; i < buffer_length; ++i) 700 if (buffer[i] == 0xf5) 701 break; 702 snd_usbmidi_input_data(ep, ep->current_port, buffer, i); 703 buffer += i; 704 buffer_length -= i; 705 706 if (buffer_length <= 0) 707 break; 708 /* assert(buffer[0] == 0xf5); */ 709 ep->seen_f5 = 1; 710 ++buffer; 711 --buffer_length; 712 713 switch_port: 714 if (buffer_length <= 0) 715 break; 716 if (buffer[0] < 0x80) { 717 ep->current_port = (buffer[0] - 1) & 15; 718 ++buffer; 719 --buffer_length; 720 } 721 ep->seen_f5 = 0; 722 } 723} 724 725static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep) 726{ 727 int port0 = ep->current_port; 728 uint8_t* buf = ep->urb->transfer_buffer; 729 int buf_free = ep->max_transfer; 730 int length, i; 731 732 for (i = 0; i < 0x10; ++i) { 733 /* round-robin, starting at the last current port */ 734 int portnum = (port0 + i) & 15; 735 struct usbmidi_out_port* port = &ep->ports[portnum]; 736 737 if (!port->active) 738 continue; 739 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) { 740 port->active = 0; 741 continue; 742 } 743 744 if (portnum != ep->current_port) { 745 if (buf_free < 2) 746 break; 747 ep->current_port = portnum; 748 buf[0] = 0xf5; 749 buf[1] = (portnum + 1) & 15; 750 buf += 2; 751 buf_free -= 2; 752 } 753 754 if (buf_free < 1) 755 break; 756 length = snd_rawmidi_transmit(port->substream, buf, buf_free); 757 if (length > 0) { 758 buf += length; 759 buf_free -= length; 760 if (buf_free < 1) 761 break; 762 } 763 } 764 if (buf_free < ep->max_transfer && buf_free > 0) { 765 *buf = 0xff; 766 --buf_free; 767 } 768 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free; 769} 770 771static struct usb_protocol_ops snd_usbmidi_emagic_ops = { 772 .input = snd_usbmidi_emagic_input, 773 .output = snd_usbmidi_emagic_output, 774 .init_out_endpoint = snd_usbmidi_emagic_init_out, 775 .finish_out_endpoint = snd_usbmidi_emagic_finish_out, 776}; 777 778 779static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream) 780{ 781 struct snd_usb_midi* umidi = substream->rmidi->private_data; 782 struct usbmidi_out_port* port = NULL; 783 int i, j; 784 785 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) 786 if (umidi->endpoints[i].out) 787 for (j = 0; j < 0x10; ++j) 788 if (umidi->endpoints[i].out->ports[j].substream == substream) { 789 port = &umidi->endpoints[i].out->ports[j]; 790 break; 791 } 792 if (!port) { 793 snd_BUG(); 794 return -ENXIO; 795 } 796 substream->runtime->private_data = port; 797 port->state = STATE_UNKNOWN; 798 return 0; 799} 800 801static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream) 802{ 803 return 0; 804} 805 806static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up) 807{ 808 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data; 809 810 port->active = up; 811 if (up) { 812 if (port->ep->umidi->chip->shutdown) { 813 /* gobble up remaining bytes to prevent wait in 814 * snd_rawmidi_drain_output */ 815 while (!snd_rawmidi_transmit_empty(substream)) 816 snd_rawmidi_transmit_ack(substream, 1); 817 return; 818 } 819 tasklet_hi_schedule(&port->ep->tasklet); 820 } 821} 822 823static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream) 824{ 825 return 0; 826} 827 828static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream) 829{ 830 return 0; 831} 832 833static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up) 834{ 835 struct snd_usb_midi* umidi = substream->rmidi->private_data; 836 837 if (up) 838 set_bit(substream->number, &umidi->input_triggered); 839 else 840 clear_bit(substream->number, &umidi->input_triggered); 841} 842 843static struct snd_rawmidi_ops snd_usbmidi_output_ops = { 844 .open = snd_usbmidi_output_open, 845 .close = snd_usbmidi_output_close, 846 .trigger = snd_usbmidi_output_trigger, 847}; 848 849static struct snd_rawmidi_ops snd_usbmidi_input_ops = { 850 .open = snd_usbmidi_input_open, 851 .close = snd_usbmidi_input_close, 852 .trigger = snd_usbmidi_input_trigger 853}; 854 855/* 856 * Frees an input endpoint. 857 * May be called when ep hasn't been initialized completely. 858 */ 859static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep) 860{ 861 if (ep->urb) { 862 usb_buffer_free(ep->umidi->chip->dev, 863 ep->urb->transfer_buffer_length, 864 ep->urb->transfer_buffer, 865 ep->urb->transfer_dma); 866 usb_free_urb(ep->urb); 867 } 868 kfree(ep); 869} 870 871/* 872 * Creates an input endpoint. 873 */ 874static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi, 875 struct snd_usb_midi_endpoint_info* ep_info, 876 struct snd_usb_midi_endpoint* rep) 877{ 878 struct snd_usb_midi_in_endpoint* ep; 879 void* buffer; 880 unsigned int pipe; 881 int length; 882 883 rep->in = NULL; 884 ep = kzalloc(sizeof(*ep), GFP_KERNEL); 885 if (!ep) 886 return -ENOMEM; 887 ep->umidi = umidi; 888 889 ep->urb = usb_alloc_urb(0, GFP_KERNEL); 890 if (!ep->urb) { 891 snd_usbmidi_in_endpoint_delete(ep); 892 return -ENOMEM; 893 } 894 if (ep_info->in_interval) 895 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep); 896 else 897 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep); 898 length = usb_maxpacket(umidi->chip->dev, pipe, 0); 899 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL, 900 &ep->urb->transfer_dma); 901 if (!buffer) { 902 snd_usbmidi_in_endpoint_delete(ep); 903 return -ENOMEM; 904 } 905 if (ep_info->in_interval) 906 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer, 907 length, snd_usbmidi_in_urb_complete, ep, 908 ep_info->in_interval); 909 else 910 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, 911 length, snd_usbmidi_in_urb_complete, ep); 912 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 913 914 rep->in = ep; 915 return 0; 916} 917 918static unsigned int snd_usbmidi_count_bits(unsigned int x) 919{ 920 unsigned int bits; 921 922 for (bits = 0; x; ++bits) 923 x &= x - 1; 924 return bits; 925} 926 927/* 928 * Frees an output endpoint. 929 * May be called when ep hasn't been initialized completely. 930 */ 931static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep) 932{ 933 if (ep->urb) { 934 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer, 935 ep->urb->transfer_buffer, 936 ep->urb->transfer_dma); 937 usb_free_urb(ep->urb); 938 } 939 kfree(ep); 940} 941 942/* 943 * Creates an output endpoint, and initializes output ports. 944 */ 945static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi, 946 struct snd_usb_midi_endpoint_info* ep_info, 947 struct snd_usb_midi_endpoint* rep) 948{ 949 struct snd_usb_midi_out_endpoint* ep; 950 int i; 951 unsigned int pipe; 952 void* buffer; 953 954 rep->out = NULL; 955 ep = kzalloc(sizeof(*ep), GFP_KERNEL); 956 if (!ep) 957 return -ENOMEM; 958 ep->umidi = umidi; 959 960 ep->urb = usb_alloc_urb(0, GFP_KERNEL); 961 if (!ep->urb) { 962 snd_usbmidi_out_endpoint_delete(ep); 963 return -ENOMEM; 964 } 965 /* we never use interrupt output pipes */ 966 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep); 967 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */ 968 ep->max_transfer = 4; 969 else 970 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1); 971 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer, 972 GFP_KERNEL, &ep->urb->transfer_dma); 973 if (!buffer) { 974 snd_usbmidi_out_endpoint_delete(ep); 975 return -ENOMEM; 976 } 977 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, 978 ep->max_transfer, snd_usbmidi_out_urb_complete, ep); 979 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 980 981 spin_lock_init(&ep->buffer_lock); 982 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep); 983 984 for (i = 0; i < 0x10; ++i) 985 if (ep_info->out_cables & (1 << i)) { 986 ep->ports[i].ep = ep; 987 ep->ports[i].cable = i << 4; 988 } 989 990 if (umidi->usb_protocol_ops->init_out_endpoint) 991 umidi->usb_protocol_ops->init_out_endpoint(ep); 992 993 rep->out = ep; 994 return 0; 995} 996 997/* 998 * Frees everything. 999 */ 1000static void snd_usbmidi_free(struct snd_usb_midi* umidi) 1001{ 1002 int i; 1003 1004 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1005 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; 1006 if (ep->out) 1007 snd_usbmidi_out_endpoint_delete(ep->out); 1008 if (ep->in) 1009 snd_usbmidi_in_endpoint_delete(ep->in); 1010 } 1011 kfree(umidi); 1012} 1013 1014/* 1015 * Unlinks all URBs (must be done before the usb_device is deleted). 1016 */ 1017void snd_usbmidi_disconnect(struct list_head* p) 1018{ 1019 struct snd_usb_midi* umidi; 1020 int i; 1021 1022 umidi = list_entry(p, struct snd_usb_midi, list); 1023 del_timer_sync(&umidi->error_timer); 1024 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1025 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; 1026 if (ep->out) 1027 tasklet_kill(&ep->out->tasklet); 1028 if (ep->out && ep->out->urb) { 1029 usb_kill_urb(ep->out->urb); 1030 if (umidi->usb_protocol_ops->finish_out_endpoint) 1031 umidi->usb_protocol_ops->finish_out_endpoint(ep->out); 1032 } 1033 if (ep->in) 1034 usb_kill_urb(ep->in->urb); 1035 } 1036} 1037 1038static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi) 1039{ 1040 struct snd_usb_midi* umidi = rmidi->private_data; 1041 snd_usbmidi_free(umidi); 1042} 1043 1044static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi, 1045 int stream, int number) 1046{ 1047 struct list_head* list; 1048 1049 list_for_each(list, &umidi->rmidi->streams[stream].substreams) { 1050 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list); 1051 if (substream->number == number) 1052 return substream; 1053 } 1054 return NULL; 1055} 1056 1057/* 1058 * This list specifies names for ports that do not fit into the standard 1059 * "(product) MIDI (n)" schema because they aren't external MIDI ports, 1060 * such as internal control or synthesizer ports. 1061 */ 1062static struct port_info { 1063 u32 id; 1064 short int port; 1065 short int voices; 1066 const char *name; 1067 unsigned int seq_flags; 1068} snd_usbmidi_port_info[] = { 1069#define PORT_INFO(vendor, product, num, name_, voices_, flags) \ 1070 { .id = USB_ID(vendor, product), \ 1071 .port = num, .voices = voices_, \ 1072 .name = name_, .seq_flags = flags } 1073#define EXTERNAL_PORT(vendor, product, num, name) \ 1074 PORT_INFO(vendor, product, num, name, 0, \ 1075 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1076 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1077 SNDRV_SEQ_PORT_TYPE_PORT) 1078#define CONTROL_PORT(vendor, product, num, name) \ 1079 PORT_INFO(vendor, product, num, name, 0, \ 1080 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1081 SNDRV_SEQ_PORT_TYPE_HARDWARE) 1082#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \ 1083 PORT_INFO(vendor, product, num, name, voices, \ 1084 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1085 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ 1086 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ 1087 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ 1088 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ 1089 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1090 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) 1091#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \ 1092 PORT_INFO(vendor, product, num, name, voices, \ 1093 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ 1094 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ 1095 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ 1096 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ 1097 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ 1098 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \ 1099 SNDRV_SEQ_PORT_TYPE_HARDWARE | \ 1100 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) 1101 /* Roland UA-100 */ 1102 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"), 1103 /* Roland SC-8850 */ 1104 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128), 1105 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128), 1106 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128), 1107 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128), 1108 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"), 1109 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"), 1110 /* Roland U-8 */ 1111 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"), 1112 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"), 1113 /* Roland SC-8820 */ 1114 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64), 1115 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64), 1116 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"), 1117 /* Roland SK-500 */ 1118 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64), 1119 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64), 1120 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"), 1121 /* Roland SC-D70 */ 1122 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64), 1123 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64), 1124 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"), 1125 /* Edirol UM-880 */ 1126 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"), 1127 /* Edirol SD-90 */ 1128 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128), 1129 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128), 1130 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"), 1131 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"), 1132 /* Edirol UM-550 */ 1133 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"), 1134 /* Edirol SD-20 */ 1135 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64), 1136 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64), 1137 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"), 1138 /* Edirol SD-80 */ 1139 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128), 1140 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128), 1141 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"), 1142 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"), 1143 /* Edirol UA-700 */ 1144 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"), 1145 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"), 1146 /* Roland VariOS */ 1147 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"), 1148 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"), 1149 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"), 1150 /* Edirol PCR */ 1151 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"), 1152 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"), 1153 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"), 1154 /* BOSS GS-10 */ 1155 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"), 1156 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"), 1157 /* Edirol UA-1000 */ 1158 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"), 1159 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"), 1160 /* Edirol UR-80 */ 1161 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"), 1162 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"), 1163 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"), 1164 /* Edirol PCR-A */ 1165 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"), 1166 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"), 1167 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"), 1168 /* Edirol UM-3EX */ 1169 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"), 1170 /* M-Audio MidiSport 8x8 */ 1171 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"), 1172 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"), 1173 /* MOTU Fastlane */ 1174 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"), 1175 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"), 1176 /* Emagic Unitor8/AMT8/MT4 */ 1177 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"), 1178 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"), 1179 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"), 1180}; 1181 1182static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number) 1183{ 1184 int i; 1185 1186 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) { 1187 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id && 1188 snd_usbmidi_port_info[i].port == number) 1189 return &snd_usbmidi_port_info[i]; 1190 } 1191 return NULL; 1192} 1193 1194static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number, 1195 struct snd_seq_port_info *seq_port_info) 1196{ 1197 struct snd_usb_midi *umidi = rmidi->private_data; 1198 struct port_info *port_info; 1199 1200 /* TODO: read port flags from descriptors */ 1201 port_info = find_port_info(umidi, number); 1202 if (port_info) { 1203 seq_port_info->type = port_info->seq_flags; 1204 seq_port_info->midi_voices = port_info->voices; 1205 } 1206} 1207 1208static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi, 1209 int stream, int number, 1210 struct snd_rawmidi_substream ** rsubstream) 1211{ 1212 struct port_info *port_info; 1213 const char *name_format; 1214 1215 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number); 1216 if (!substream) { 1217 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number); 1218 return; 1219 } 1220 1221 /* TODO: read port name from jack descriptor */ 1222 port_info = find_port_info(umidi, number); 1223 name_format = port_info ? port_info->name : "%s MIDI %d"; 1224 snprintf(substream->name, sizeof(substream->name), 1225 name_format, umidi->chip->card->shortname, number + 1); 1226 1227 *rsubstream = substream; 1228} 1229 1230/* 1231 * Creates the endpoints and their ports. 1232 */ 1233static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi, 1234 struct snd_usb_midi_endpoint_info* endpoints) 1235{ 1236 int i, j, err; 1237 int out_ports = 0, in_ports = 0; 1238 1239 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1240 if (endpoints[i].out_cables) { 1241 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i], 1242 &umidi->endpoints[i]); 1243 if (err < 0) 1244 return err; 1245 } 1246 if (endpoints[i].in_cables) { 1247 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i], 1248 &umidi->endpoints[i]); 1249 if (err < 0) 1250 return err; 1251 } 1252 1253 for (j = 0; j < 0x10; ++j) { 1254 if (endpoints[i].out_cables & (1 << j)) { 1255 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports, 1256 &umidi->endpoints[i].out->ports[j].substream); 1257 ++out_ports; 1258 } 1259 if (endpoints[i].in_cables & (1 << j)) { 1260 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports, 1261 &umidi->endpoints[i].in->ports[j].substream); 1262 ++in_ports; 1263 } 1264 } 1265 } 1266 snd_printdd(KERN_INFO "created %d output and %d input ports\n", 1267 out_ports, in_ports); 1268 return 0; 1269} 1270 1271/* 1272 * Returns MIDIStreaming device capabilities. 1273 */ 1274static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi, 1275 struct snd_usb_midi_endpoint_info* endpoints) 1276{ 1277 struct usb_interface* intf; 1278 struct usb_host_interface *hostif; 1279 struct usb_interface_descriptor* intfd; 1280 struct usb_ms_header_descriptor* ms_header; 1281 struct usb_host_endpoint *hostep; 1282 struct usb_endpoint_descriptor* ep; 1283 struct usb_ms_endpoint_descriptor* ms_ep; 1284 int i, epidx; 1285 1286 intf = umidi->iface; 1287 if (!intf) 1288 return -ENXIO; 1289 hostif = &intf->altsetting[0]; 1290 intfd = get_iface_desc(hostif); 1291 ms_header = (struct usb_ms_header_descriptor*)hostif->extra; 1292 if (hostif->extralen >= 7 && 1293 ms_header->bLength >= 7 && 1294 ms_header->bDescriptorType == USB_DT_CS_INTERFACE && 1295 ms_header->bDescriptorSubtype == HEADER) 1296 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n", 1297 ms_header->bcdMSC[1], ms_header->bcdMSC[0]); 1298 else 1299 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n"); 1300 1301 epidx = 0; 1302 for (i = 0; i < intfd->bNumEndpoints; ++i) { 1303 hostep = &hostif->endpoint[i]; 1304 ep = get_ep_desc(hostep); 1305 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK && 1306 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) 1307 continue; 1308 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra; 1309 if (hostep->extralen < 4 || 1310 ms_ep->bLength < 4 || 1311 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT || 1312 ms_ep->bDescriptorSubtype != MS_GENERAL) 1313 continue; 1314 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { 1315 if (endpoints[epidx].out_ep) { 1316 if (++epidx >= MIDI_MAX_ENDPOINTS) { 1317 snd_printk(KERN_WARNING "too many endpoints\n"); 1318 break; 1319 } 1320 } 1321 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 1322 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) 1323 endpoints[epidx].out_interval = ep->bInterval; 1324 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; 1325 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", 1326 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); 1327 } else { 1328 if (endpoints[epidx].in_ep) { 1329 if (++epidx >= MIDI_MAX_ENDPOINTS) { 1330 snd_printk(KERN_WARNING "too many endpoints\n"); 1331 break; 1332 } 1333 } 1334 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 1335 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) 1336 endpoints[epidx].in_interval = ep->bInterval; 1337 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; 1338 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", 1339 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); 1340 } 1341 } 1342 return 0; 1343} 1344 1345/* 1346 * On Roland devices, use the second alternate setting to be able to use 1347 * the interrupt input endpoint. 1348 */ 1349static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi) 1350{ 1351 struct usb_interface* intf; 1352 struct usb_host_interface *hostif; 1353 struct usb_interface_descriptor* intfd; 1354 1355 intf = umidi->iface; 1356 if (!intf || intf->num_altsetting != 2) 1357 return; 1358 1359 hostif = &intf->altsetting[1]; 1360 intfd = get_iface_desc(hostif); 1361 if (intfd->bNumEndpoints != 2 || 1362 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK || 1363 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) 1364 return; 1365 1366 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n", 1367 intfd->bAlternateSetting); 1368 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber, 1369 intfd->bAlternateSetting); 1370} 1371 1372/* 1373 * Try to find any usable endpoints in the interface. 1374 */ 1375static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi, 1376 struct snd_usb_midi_endpoint_info* endpoint, 1377 int max_endpoints) 1378{ 1379 struct usb_interface* intf; 1380 struct usb_host_interface *hostif; 1381 struct usb_interface_descriptor* intfd; 1382 struct usb_endpoint_descriptor* epd; 1383 int i, out_eps = 0, in_eps = 0; 1384 1385 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582) 1386 snd_usbmidi_switch_roland_altsetting(umidi); 1387 1388 if (endpoint[0].out_ep || endpoint[0].in_ep) 1389 return 0; 1390 1391 intf = umidi->iface; 1392 if (!intf || intf->num_altsetting < 1) 1393 return -ENOENT; 1394 hostif = intf->cur_altsetting; 1395 intfd = get_iface_desc(hostif); 1396 1397 for (i = 0; i < intfd->bNumEndpoints; ++i) { 1398 epd = get_endpoint(hostif, i); 1399 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK && 1400 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) 1401 continue; 1402 if (out_eps < max_endpoints && 1403 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { 1404 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 1405 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) 1406 endpoint[out_eps].out_interval = epd->bInterval; 1407 ++out_eps; 1408 } 1409 if (in_eps < max_endpoints && 1410 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) { 1411 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 1412 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) 1413 endpoint[in_eps].in_interval = epd->bInterval; 1414 ++in_eps; 1415 } 1416 } 1417 return (out_eps || in_eps) ? 0 : -ENOENT; 1418} 1419 1420/* 1421 * Detects the endpoints for one-port-per-endpoint protocols. 1422 */ 1423static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi, 1424 struct snd_usb_midi_endpoint_info* endpoints) 1425{ 1426 int err, i; 1427 1428 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS); 1429 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1430 if (endpoints[i].out_ep) 1431 endpoints[i].out_cables = 0x0001; 1432 if (endpoints[i].in_ep) 1433 endpoints[i].in_cables = 0x0001; 1434 } 1435 return err; 1436} 1437 1438/* 1439 * Detects the endpoints and ports of Yamaha devices. 1440 */ 1441static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi, 1442 struct snd_usb_midi_endpoint_info* endpoint) 1443{ 1444 struct usb_interface* intf; 1445 struct usb_host_interface *hostif; 1446 struct usb_interface_descriptor* intfd; 1447 uint8_t* cs_desc; 1448 1449 intf = umidi->iface; 1450 if (!intf) 1451 return -ENOENT; 1452 hostif = intf->altsetting; 1453 intfd = get_iface_desc(hostif); 1454 if (intfd->bNumEndpoints < 1) 1455 return -ENOENT; 1456 1457 /* 1458 * For each port there is one MIDI_IN/OUT_JACK descriptor, not 1459 * necessarily with any useful contents. So simply count 'em. 1460 */ 1461 for (cs_desc = hostif->extra; 1462 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2; 1463 cs_desc += cs_desc[0]) { 1464 if (cs_desc[1] == USB_DT_CS_INTERFACE) { 1465 if (cs_desc[2] == MIDI_IN_JACK) 1466 endpoint->in_cables = (endpoint->in_cables << 1) | 1; 1467 else if (cs_desc[2] == MIDI_OUT_JACK) 1468 endpoint->out_cables = (endpoint->out_cables << 1) | 1; 1469 } 1470 } 1471 if (!endpoint->in_cables && !endpoint->out_cables) 1472 return -ENOENT; 1473 1474 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1); 1475} 1476 1477/* 1478 * Creates the endpoints and their ports for Midiman devices. 1479 */ 1480static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi, 1481 struct snd_usb_midi_endpoint_info* endpoint) 1482{ 1483 struct snd_usb_midi_endpoint_info ep_info; 1484 struct usb_interface* intf; 1485 struct usb_host_interface *hostif; 1486 struct usb_interface_descriptor* intfd; 1487 struct usb_endpoint_descriptor* epd; 1488 int cable, err; 1489 1490 intf = umidi->iface; 1491 if (!intf) 1492 return -ENOENT; 1493 hostif = intf->altsetting; 1494 intfd = get_iface_desc(hostif); 1495 /* 1496 * The various MidiSport devices have more or less random endpoint 1497 * numbers, so we have to identify the endpoints by their index in 1498 * the descriptor array, like the driver for that other OS does. 1499 * 1500 * There is one interrupt input endpoint for all input ports, one 1501 * bulk output endpoint for even-numbered ports, and one for odd- 1502 * numbered ports. Both bulk output endpoints have corresponding 1503 * input bulk endpoints (at indices 1 and 3) which aren't used. 1504 */ 1505 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) { 1506 snd_printdd(KERN_ERR "not enough endpoints\n"); 1507 return -ENOENT; 1508 } 1509 1510 epd = get_endpoint(hostif, 0); 1511 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN || 1512 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) { 1513 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n"); 1514 return -ENXIO; 1515 } 1516 epd = get_endpoint(hostif, 2); 1517 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT || 1518 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) { 1519 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n"); 1520 return -ENXIO; 1521 } 1522 if (endpoint->out_cables > 0x0001) { 1523 epd = get_endpoint(hostif, 4); 1524 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT || 1525 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) { 1526 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n"); 1527 return -ENXIO; 1528 } 1529 } 1530 1531 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 1532 ep_info.out_cables = endpoint->out_cables & 0x5555; 1533 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); 1534 if (err < 0) 1535 return err; 1536 1537 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 1538 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval; 1539 ep_info.in_cables = endpoint->in_cables; 1540 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); 1541 if (err < 0) 1542 return err; 1543 1544 if (endpoint->out_cables > 0x0001) { 1545 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 1546 ep_info.out_cables = endpoint->out_cables & 0xaaaa; 1547 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]); 1548 if (err < 0) 1549 return err; 1550 } 1551 1552 for (cable = 0; cable < 0x10; ++cable) { 1553 if (endpoint->out_cables & (1 << cable)) 1554 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable, 1555 &umidi->endpoints[cable & 1].out->ports[cable].substream); 1556 if (endpoint->in_cables & (1 << cable)) 1557 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable, 1558 &umidi->endpoints[0].in->ports[cable].substream); 1559 } 1560 return 0; 1561} 1562 1563static struct snd_rawmidi_global_ops snd_usbmidi_ops = { 1564 .get_port_info = snd_usbmidi_get_port_info, 1565}; 1566 1567static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi, 1568 int out_ports, int in_ports) 1569{ 1570 struct snd_rawmidi *rmidi; 1571 int err; 1572 1573 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI", 1574 umidi->chip->next_midi_device++, 1575 out_ports, in_ports, &rmidi); 1576 if (err < 0) 1577 return err; 1578 strcpy(rmidi->name, umidi->chip->card->shortname); 1579 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | 1580 SNDRV_RAWMIDI_INFO_INPUT | 1581 SNDRV_RAWMIDI_INFO_DUPLEX; 1582 rmidi->ops = &snd_usbmidi_ops; 1583 rmidi->private_data = umidi; 1584 rmidi->private_free = snd_usbmidi_rawmidi_free; 1585 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops); 1586 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops); 1587 1588 umidi->rmidi = rmidi; 1589 return 0; 1590} 1591 1592/* 1593 * Temporarily stop input. 1594 */ 1595void snd_usbmidi_input_stop(struct list_head* p) 1596{ 1597 struct snd_usb_midi* umidi; 1598 int i; 1599 1600 umidi = list_entry(p, struct snd_usb_midi, list); 1601 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1602 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; 1603 if (ep->in) 1604 usb_kill_urb(ep->in->urb); 1605 } 1606} 1607 1608static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep) 1609{ 1610 if (ep) { 1611 struct urb* urb = ep->urb; 1612 urb->dev = ep->umidi->chip->dev; 1613 snd_usbmidi_submit_urb(urb, GFP_KERNEL); 1614 } 1615} 1616 1617/* 1618 * Resume input after a call to snd_usbmidi_input_stop(). 1619 */ 1620void snd_usbmidi_input_start(struct list_head* p) 1621{ 1622 struct snd_usb_midi* umidi; 1623 int i; 1624 1625 umidi = list_entry(p, struct snd_usb_midi, list); 1626 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) 1627 snd_usbmidi_input_start_ep(umidi->endpoints[i].in); 1628} 1629 1630/* 1631 * Creates and registers everything needed for a MIDI streaming interface. 1632 */ 1633int snd_usb_create_midi_interface(struct snd_usb_audio* chip, 1634 struct usb_interface* iface, 1635 const struct snd_usb_audio_quirk* quirk) 1636{ 1637 struct snd_usb_midi* umidi; 1638 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS]; 1639 int out_ports, in_ports; 1640 int i, err; 1641 1642 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL); 1643 if (!umidi) 1644 return -ENOMEM; 1645 umidi->chip = chip; 1646 umidi->iface = iface; 1647 umidi->quirk = quirk; 1648 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops; 1649 init_timer(&umidi->error_timer); 1650 umidi->error_timer.function = snd_usbmidi_error_timer; 1651 umidi->error_timer.data = (unsigned long)umidi; 1652 1653 /* detect the endpoint(s) to use */ 1654 memset(endpoints, 0, sizeof(endpoints)); 1655 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) { 1656 case QUIRK_MIDI_STANDARD_INTERFACE: 1657 err = snd_usbmidi_get_ms_info(umidi, endpoints); 1658 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */ 1659 umidi->usb_protocol_ops = 1660 &snd_usbmidi_maudio_broken_running_status_ops; 1661 break; 1662 case QUIRK_MIDI_FIXED_ENDPOINT: 1663 memcpy(&endpoints[0], quirk->data, 1664 sizeof(struct snd_usb_midi_endpoint_info)); 1665 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); 1666 break; 1667 case QUIRK_MIDI_YAMAHA: 1668 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]); 1669 break; 1670 case QUIRK_MIDI_MIDIMAN: 1671 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops; 1672 memcpy(&endpoints[0], quirk->data, 1673 sizeof(struct snd_usb_midi_endpoint_info)); 1674 err = 0; 1675 break; 1676 case QUIRK_MIDI_NOVATION: 1677 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops; 1678 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 1679 break; 1680 case QUIRK_MIDI_RAW: 1681 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops; 1682 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 1683 break; 1684 case QUIRK_MIDI_EMAGIC: 1685 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops; 1686 memcpy(&endpoints[0], quirk->data, 1687 sizeof(struct snd_usb_midi_endpoint_info)); 1688 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); 1689 break; 1690 case QUIRK_MIDI_CME: 1691 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); 1692 break; 1693 default: 1694 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type); 1695 err = -ENXIO; 1696 break; 1697 } 1698 if (err < 0) { 1699 kfree(umidi); 1700 return err; 1701 } 1702 1703 /* create rawmidi device */ 1704 out_ports = 0; 1705 in_ports = 0; 1706 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { 1707 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables); 1708 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables); 1709 } 1710 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports); 1711 if (err < 0) { 1712 kfree(umidi); 1713 return err; 1714 } 1715 1716 /* create endpoint/port structures */ 1717 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN) 1718 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]); 1719 else 1720 err = snd_usbmidi_create_endpoints(umidi, endpoints); 1721 if (err < 0) { 1722 snd_usbmidi_free(umidi); 1723 return err; 1724 } 1725 1726 list_add(&umidi->list, &umidi->chip->midi_list); 1727 1728 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) 1729 snd_usbmidi_input_start_ep(umidi->endpoints[i].in); 1730 return 0; 1731} 1732 1733EXPORT_SYMBOL(snd_usb_create_midi_interface); 1734EXPORT_SYMBOL(snd_usbmidi_input_stop); 1735EXPORT_SYMBOL(snd_usbmidi_input_start); 1736EXPORT_SYMBOL(snd_usbmidi_disconnect); 1737