ccwgroup: move attributes to attribute group
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / usb / usbmidi.c
blob64d8d2e6026df684a1a0c298f5aabbeb361c57f1
1 /*
2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2007 Clemens Ladisch
5 * All rights reserved.
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
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.
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.
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.
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 <sound/core.h>
50 #include <sound/rawmidi.h>
51 #include <sound/asequencer.h>
52 #include "usbaudio.h"
56 * define this to log all USB packets
58 /* #define DUMP_PACKETS */
61 * how long to wait after some USB errors, so that khubd can disconnect() us
62 * without too many spurious errors
64 #define ERROR_DELAY_JIFFIES (HZ / 10)
66 #define OUTPUT_URBS 7
67 #define INPUT_URBS 7
70 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
71 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
72 MODULE_LICENSE("Dual BSD/GPL");
75 struct usb_ms_header_descriptor {
76 __u8 bLength;
77 __u8 bDescriptorType;
78 __u8 bDescriptorSubtype;
79 __u8 bcdMSC[2];
80 __le16 wTotalLength;
81 } __attribute__ ((packed));
83 struct usb_ms_endpoint_descriptor {
84 __u8 bLength;
85 __u8 bDescriptorType;
86 __u8 bDescriptorSubtype;
87 __u8 bNumEmbMIDIJack;
88 __u8 baAssocJackID[0];
89 } __attribute__ ((packed));
91 struct snd_usb_midi_in_endpoint;
92 struct snd_usb_midi_out_endpoint;
93 struct snd_usb_midi_endpoint;
95 struct usb_protocol_ops {
96 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
97 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
98 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
99 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
100 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
103 struct snd_usb_midi {
104 struct snd_usb_audio *chip;
105 struct usb_interface *iface;
106 const struct snd_usb_audio_quirk *quirk;
107 struct snd_rawmidi *rmidi;
108 struct usb_protocol_ops* usb_protocol_ops;
109 struct list_head list;
110 struct timer_list error_timer;
111 spinlock_t disc_lock;
113 struct snd_usb_midi_endpoint {
114 struct snd_usb_midi_out_endpoint *out;
115 struct snd_usb_midi_in_endpoint *in;
116 } endpoints[MIDI_MAX_ENDPOINTS];
117 unsigned long input_triggered;
118 unsigned char disconnected;
121 struct snd_usb_midi_out_endpoint {
122 struct snd_usb_midi* umidi;
123 struct out_urb_context {
124 struct urb *urb;
125 struct snd_usb_midi_out_endpoint *ep;
126 } urbs[OUTPUT_URBS];
127 unsigned int active_urbs;
128 unsigned int drain_urbs;
129 int max_transfer; /* size of urb buffer */
130 struct tasklet_struct tasklet;
131 unsigned int next_urb;
132 spinlock_t buffer_lock;
134 struct usbmidi_out_port {
135 struct snd_usb_midi_out_endpoint* ep;
136 struct snd_rawmidi_substream *substream;
137 int active;
138 uint8_t cable; /* cable number << 4 */
139 uint8_t state;
140 #define STATE_UNKNOWN 0
141 #define STATE_1PARAM 1
142 #define STATE_2PARAM_1 2
143 #define STATE_2PARAM_2 3
144 #define STATE_SYSEX_0 4
145 #define STATE_SYSEX_1 5
146 #define STATE_SYSEX_2 6
147 uint8_t data[2];
148 } ports[0x10];
149 int current_port;
151 wait_queue_head_t drain_wait;
154 struct snd_usb_midi_in_endpoint {
155 struct snd_usb_midi* umidi;
156 struct urb* urbs[INPUT_URBS];
157 struct usbmidi_in_port {
158 struct snd_rawmidi_substream *substream;
159 u8 running_status_length;
160 } ports[0x10];
161 u8 seen_f5;
162 u8 error_resubmit;
163 int current_port;
166 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
168 static const uint8_t snd_usbmidi_cin_length[] = {
169 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
173 * Submits the URB, with error handling.
175 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
177 int err = usb_submit_urb(urb, flags);
178 if (err < 0 && err != -ENODEV)
179 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
180 return err;
184 * Error handling for URB completion functions.
186 static int snd_usbmidi_urb_error(int status)
188 switch (status) {
189 /* manually unlinked, or device gone */
190 case -ENOENT:
191 case -ECONNRESET:
192 case -ESHUTDOWN:
193 case -ENODEV:
194 return -ENODEV;
195 /* errors that might occur during unplugging */
196 case -EPROTO:
197 case -ETIME:
198 case -EILSEQ:
199 return -EIO;
200 default:
201 snd_printk(KERN_ERR "urb status %d\n", status);
202 return 0; /* continue */
207 * Receives a chunk of MIDI data.
209 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
210 uint8_t* data, int length)
212 struct usbmidi_in_port* port = &ep->ports[portidx];
214 if (!port->substream) {
215 snd_printd("unexpected port %d!\n", portidx);
216 return;
218 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
219 return;
220 snd_rawmidi_receive(port->substream, data, length);
223 #ifdef DUMP_PACKETS
224 static void dump_urb(const char *type, const u8 *data, int length)
226 snd_printk(KERN_DEBUG "%s packet: [", type);
227 for (; length > 0; ++data, --length)
228 printk(" %02x", *data);
229 printk(" ]\n");
231 #else
232 #define dump_urb(type, data, length) /* nothing */
233 #endif
236 * Processes the data read from the device.
238 static void snd_usbmidi_in_urb_complete(struct urb* urb)
240 struct snd_usb_midi_in_endpoint* ep = urb->context;
242 if (urb->status == 0) {
243 dump_urb("received", urb->transfer_buffer, urb->actual_length);
244 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
245 urb->actual_length);
246 } else {
247 int err = snd_usbmidi_urb_error(urb->status);
248 if (err < 0) {
249 if (err != -ENODEV) {
250 ep->error_resubmit = 1;
251 mod_timer(&ep->umidi->error_timer,
252 jiffies + ERROR_DELAY_JIFFIES);
254 return;
258 urb->dev = ep->umidi->chip->dev;
259 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
262 static void snd_usbmidi_out_urb_complete(struct urb* urb)
264 struct out_urb_context *context = urb->context;
265 struct snd_usb_midi_out_endpoint* ep = context->ep;
266 unsigned int urb_index;
268 spin_lock(&ep->buffer_lock);
269 urb_index = context - ep->urbs;
270 ep->active_urbs &= ~(1 << urb_index);
271 if (unlikely(ep->drain_urbs)) {
272 ep->drain_urbs &= ~(1 << urb_index);
273 wake_up(&ep->drain_wait);
275 spin_unlock(&ep->buffer_lock);
276 if (urb->status < 0) {
277 int err = snd_usbmidi_urb_error(urb->status);
278 if (err < 0) {
279 if (err != -ENODEV)
280 mod_timer(&ep->umidi->error_timer,
281 jiffies + ERROR_DELAY_JIFFIES);
282 return;
285 snd_usbmidi_do_output(ep);
289 * This is called when some data should be transferred to the device
290 * (from one or more substreams).
292 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
294 unsigned int urb_index;
295 struct urb* urb;
296 unsigned long flags;
298 spin_lock_irqsave(&ep->buffer_lock, flags);
299 if (ep->umidi->chip->shutdown) {
300 spin_unlock_irqrestore(&ep->buffer_lock, flags);
301 return;
304 urb_index = ep->next_urb;
305 for (;;) {
306 if (!(ep->active_urbs & (1 << urb_index))) {
307 urb = ep->urbs[urb_index].urb;
308 urb->transfer_buffer_length = 0;
309 ep->umidi->usb_protocol_ops->output(ep, urb);
310 if (urb->transfer_buffer_length == 0)
311 break;
313 dump_urb("sending", urb->transfer_buffer,
314 urb->transfer_buffer_length);
315 urb->dev = ep->umidi->chip->dev;
316 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
317 break;
318 ep->active_urbs |= 1 << urb_index;
320 if (++urb_index >= OUTPUT_URBS)
321 urb_index = 0;
322 if (urb_index == ep->next_urb)
323 break;
325 ep->next_urb = urb_index;
326 spin_unlock_irqrestore(&ep->buffer_lock, flags);
329 static void snd_usbmidi_out_tasklet(unsigned long data)
331 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
333 snd_usbmidi_do_output(ep);
336 /* called after transfers had been interrupted due to some USB error */
337 static void snd_usbmidi_error_timer(unsigned long data)
339 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
340 unsigned int i, j;
342 spin_lock(&umidi->disc_lock);
343 if (umidi->disconnected) {
344 spin_unlock(&umidi->disc_lock);
345 return;
347 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
348 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
349 if (in && in->error_resubmit) {
350 in->error_resubmit = 0;
351 for (j = 0; j < INPUT_URBS; ++j) {
352 in->urbs[j]->dev = umidi->chip->dev;
353 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
356 if (umidi->endpoints[i].out)
357 snd_usbmidi_do_output(umidi->endpoints[i].out);
359 spin_unlock(&umidi->disc_lock);
362 /* helper function to send static data that may not DMA-able */
363 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
364 const void *data, int len)
366 int err = 0;
367 void *buf = kmemdup(data, len, GFP_KERNEL);
368 if (!buf)
369 return -ENOMEM;
370 dump_urb("sending", buf, len);
371 if (ep->urbs[0].urb)
372 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urbs[0].urb->pipe,
373 buf, len, NULL, 250);
374 kfree(buf);
375 return err;
379 * Standard USB MIDI protocol: see the spec.
380 * Midiman protocol: like the standard protocol, but the control byte is the
381 * fourth byte in each packet, and uses length instead of CIN.
384 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
385 uint8_t* buffer, int buffer_length)
387 int i;
389 for (i = 0; i + 3 < buffer_length; i += 4)
390 if (buffer[i] != 0) {
391 int cable = buffer[i] >> 4;
392 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
393 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
397 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
398 uint8_t* buffer, int buffer_length)
400 int i;
402 for (i = 0; i + 3 < buffer_length; i += 4)
403 if (buffer[i + 3] != 0) {
404 int port = buffer[i + 3] >> 4;
405 int length = buffer[i + 3] & 3;
406 snd_usbmidi_input_data(ep, port, &buffer[i], length);
411 * Buggy M-Audio device: running status on input results in a packet that has
412 * the data bytes but not the status byte and that is marked with CIN 4.
414 static void snd_usbmidi_maudio_broken_running_status_input(
415 struct snd_usb_midi_in_endpoint* ep,
416 uint8_t* buffer, int buffer_length)
418 int i;
420 for (i = 0; i + 3 < buffer_length; i += 4)
421 if (buffer[i] != 0) {
422 int cable = buffer[i] >> 4;
423 u8 cin = buffer[i] & 0x0f;
424 struct usbmidi_in_port *port = &ep->ports[cable];
425 int length;
427 length = snd_usbmidi_cin_length[cin];
428 if (cin == 0xf && buffer[i + 1] >= 0xf8)
429 ; /* realtime msg: no running status change */
430 else if (cin >= 0x8 && cin <= 0xe)
431 /* channel msg */
432 port->running_status_length = length - 1;
433 else if (cin == 0x4 &&
434 port->running_status_length != 0 &&
435 buffer[i + 1] < 0x80)
436 /* CIN 4 that is not a SysEx */
437 length = port->running_status_length;
438 else
440 * All other msgs cannot begin running status.
441 * (A channel msg sent as two or three CIN 0xF
442 * packets could in theory, but this device
443 * doesn't use this format.)
445 port->running_status_length = 0;
446 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
451 * CME protocol: like the standard protocol, but SysEx commands are sent as a
452 * single USB packet preceded by a 0x0F byte.
454 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
455 uint8_t *buffer, int buffer_length)
457 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
458 snd_usbmidi_standard_input(ep, buffer, buffer_length);
459 else
460 snd_usbmidi_input_data(ep, buffer[0] >> 4,
461 &buffer[1], buffer_length - 1);
465 * Adds one USB MIDI packet to the output buffer.
467 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
468 uint8_t p1, uint8_t p2, uint8_t p3)
471 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
472 buf[0] = p0;
473 buf[1] = p1;
474 buf[2] = p2;
475 buf[3] = p3;
476 urb->transfer_buffer_length += 4;
480 * Adds one Midiman packet to the output buffer.
482 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
483 uint8_t p1, uint8_t p2, uint8_t p3)
486 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
487 buf[0] = p1;
488 buf[1] = p2;
489 buf[2] = p3;
490 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
491 urb->transfer_buffer_length += 4;
495 * Converts MIDI commands to USB MIDI packets.
497 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
498 uint8_t b, struct urb* urb)
500 uint8_t p0 = port->cable;
501 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
502 port->ep->umidi->usb_protocol_ops->output_packet;
504 if (b >= 0xf8) {
505 output_packet(urb, p0 | 0x0f, b, 0, 0);
506 } else if (b >= 0xf0) {
507 switch (b) {
508 case 0xf0:
509 port->data[0] = b;
510 port->state = STATE_SYSEX_1;
511 break;
512 case 0xf1:
513 case 0xf3:
514 port->data[0] = b;
515 port->state = STATE_1PARAM;
516 break;
517 case 0xf2:
518 port->data[0] = b;
519 port->state = STATE_2PARAM_1;
520 break;
521 case 0xf4:
522 case 0xf5:
523 port->state = STATE_UNKNOWN;
524 break;
525 case 0xf6:
526 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
527 port->state = STATE_UNKNOWN;
528 break;
529 case 0xf7:
530 switch (port->state) {
531 case STATE_SYSEX_0:
532 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
533 break;
534 case STATE_SYSEX_1:
535 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
536 break;
537 case STATE_SYSEX_2:
538 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
539 break;
541 port->state = STATE_UNKNOWN;
542 break;
544 } else if (b >= 0x80) {
545 port->data[0] = b;
546 if (b >= 0xc0 && b <= 0xdf)
547 port->state = STATE_1PARAM;
548 else
549 port->state = STATE_2PARAM_1;
550 } else { /* b < 0x80 */
551 switch (port->state) {
552 case STATE_1PARAM:
553 if (port->data[0] < 0xf0) {
554 p0 |= port->data[0] >> 4;
555 } else {
556 p0 |= 0x02;
557 port->state = STATE_UNKNOWN;
559 output_packet(urb, p0, port->data[0], b, 0);
560 break;
561 case STATE_2PARAM_1:
562 port->data[1] = b;
563 port->state = STATE_2PARAM_2;
564 break;
565 case STATE_2PARAM_2:
566 if (port->data[0] < 0xf0) {
567 p0 |= port->data[0] >> 4;
568 port->state = STATE_2PARAM_1;
569 } else {
570 p0 |= 0x03;
571 port->state = STATE_UNKNOWN;
573 output_packet(urb, p0, port->data[0], port->data[1], b);
574 break;
575 case STATE_SYSEX_0:
576 port->data[0] = b;
577 port->state = STATE_SYSEX_1;
578 break;
579 case STATE_SYSEX_1:
580 port->data[1] = b;
581 port->state = STATE_SYSEX_2;
582 break;
583 case STATE_SYSEX_2:
584 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
585 port->state = STATE_SYSEX_0;
586 break;
591 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
592 struct urb *urb)
594 int p;
596 /* FIXME: lower-numbered ports can starve higher-numbered ports */
597 for (p = 0; p < 0x10; ++p) {
598 struct usbmidi_out_port* port = &ep->ports[p];
599 if (!port->active)
600 continue;
601 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
602 uint8_t b;
603 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
604 port->active = 0;
605 break;
607 snd_usbmidi_transmit_byte(port, b, urb);
612 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
613 .input = snd_usbmidi_standard_input,
614 .output = snd_usbmidi_standard_output,
615 .output_packet = snd_usbmidi_output_standard_packet,
618 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
619 .input = snd_usbmidi_midiman_input,
620 .output = snd_usbmidi_standard_output,
621 .output_packet = snd_usbmidi_output_midiman_packet,
624 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
625 .input = snd_usbmidi_maudio_broken_running_status_input,
626 .output = snd_usbmidi_standard_output,
627 .output_packet = snd_usbmidi_output_standard_packet,
630 static struct usb_protocol_ops snd_usbmidi_cme_ops = {
631 .input = snd_usbmidi_cme_input,
632 .output = snd_usbmidi_standard_output,
633 .output_packet = snd_usbmidi_output_standard_packet,
637 * Novation USB MIDI protocol: number of data bytes is in the first byte
638 * (when receiving) (+1!) or in the second byte (when sending); data begins
639 * at the third byte.
642 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
643 uint8_t* buffer, int buffer_length)
645 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
646 return;
647 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
650 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
651 struct urb *urb)
653 uint8_t* transfer_buffer;
654 int count;
656 if (!ep->ports[0].active)
657 return;
658 transfer_buffer = urb->transfer_buffer;
659 count = snd_rawmidi_transmit(ep->ports[0].substream,
660 &transfer_buffer[2],
661 ep->max_transfer - 2);
662 if (count < 1) {
663 ep->ports[0].active = 0;
664 return;
666 transfer_buffer[0] = 0;
667 transfer_buffer[1] = count;
668 urb->transfer_buffer_length = 2 + count;
671 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
672 .input = snd_usbmidi_novation_input,
673 .output = snd_usbmidi_novation_output,
677 * "raw" protocol: used by the MOTU FastLane.
680 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
681 uint8_t* buffer, int buffer_length)
683 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
686 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
687 struct urb *urb)
689 int count;
691 if (!ep->ports[0].active)
692 return;
693 count = snd_rawmidi_transmit(ep->ports[0].substream,
694 urb->transfer_buffer,
695 ep->max_transfer);
696 if (count < 1) {
697 ep->ports[0].active = 0;
698 return;
700 urb->transfer_buffer_length = count;
703 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
704 .input = snd_usbmidi_raw_input,
705 .output = snd_usbmidi_raw_output,
708 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
709 uint8_t *buffer, int buffer_length)
711 if (buffer_length != 9)
712 return;
713 buffer_length = 8;
714 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
715 buffer_length--;
716 if (buffer_length)
717 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
720 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
721 struct urb *urb)
723 int count;
725 if (!ep->ports[0].active)
726 return;
727 count = snd_usb_get_speed(ep->umidi->chip->dev) == USB_SPEED_HIGH
728 ? 1 : 2;
729 count = snd_rawmidi_transmit(ep->ports[0].substream,
730 urb->transfer_buffer,
731 count);
732 if (count < 1) {
733 ep->ports[0].active = 0;
734 return;
737 memset(urb->transfer_buffer + count, 0xFD, 9 - count);
738 urb->transfer_buffer_length = count;
741 static struct usb_protocol_ops snd_usbmidi_122l_ops = {
742 .input = snd_usbmidi_us122l_input,
743 .output = snd_usbmidi_us122l_output,
747 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
750 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
752 static const u8 init_data[] = {
753 /* initialization magic: "get version" */
754 0xf0,
755 0x00, 0x20, 0x31, /* Emagic */
756 0x64, /* Unitor8 */
757 0x0b, /* version number request */
758 0x00, /* command version */
759 0x00, /* EEPROM, box 0 */
760 0xf7
762 send_bulk_static_data(ep, init_data, sizeof(init_data));
763 /* while we're at it, pour on more magic */
764 send_bulk_static_data(ep, init_data, sizeof(init_data));
767 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
769 static const u8 finish_data[] = {
770 /* switch to patch mode with last preset */
771 0xf0,
772 0x00, 0x20, 0x31, /* Emagic */
773 0x64, /* Unitor8 */
774 0x10, /* patch switch command */
775 0x00, /* command version */
776 0x7f, /* to all boxes */
777 0x40, /* last preset in EEPROM */
778 0xf7
780 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
783 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
784 uint8_t* buffer, int buffer_length)
786 int i;
788 /* FF indicates end of valid data */
789 for (i = 0; i < buffer_length; ++i)
790 if (buffer[i] == 0xff) {
791 buffer_length = i;
792 break;
795 /* handle F5 at end of last buffer */
796 if (ep->seen_f5)
797 goto switch_port;
799 while (buffer_length > 0) {
800 /* determine size of data until next F5 */
801 for (i = 0; i < buffer_length; ++i)
802 if (buffer[i] == 0xf5)
803 break;
804 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
805 buffer += i;
806 buffer_length -= i;
808 if (buffer_length <= 0)
809 break;
810 /* assert(buffer[0] == 0xf5); */
811 ep->seen_f5 = 1;
812 ++buffer;
813 --buffer_length;
815 switch_port:
816 if (buffer_length <= 0)
817 break;
818 if (buffer[0] < 0x80) {
819 ep->current_port = (buffer[0] - 1) & 15;
820 ++buffer;
821 --buffer_length;
823 ep->seen_f5 = 0;
827 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
828 struct urb *urb)
830 int port0 = ep->current_port;
831 uint8_t* buf = urb->transfer_buffer;
832 int buf_free = ep->max_transfer;
833 int length, i;
835 for (i = 0; i < 0x10; ++i) {
836 /* round-robin, starting at the last current port */
837 int portnum = (port0 + i) & 15;
838 struct usbmidi_out_port* port = &ep->ports[portnum];
840 if (!port->active)
841 continue;
842 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
843 port->active = 0;
844 continue;
847 if (portnum != ep->current_port) {
848 if (buf_free < 2)
849 break;
850 ep->current_port = portnum;
851 buf[0] = 0xf5;
852 buf[1] = (portnum + 1) & 15;
853 buf += 2;
854 buf_free -= 2;
857 if (buf_free < 1)
858 break;
859 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
860 if (length > 0) {
861 buf += length;
862 buf_free -= length;
863 if (buf_free < 1)
864 break;
867 if (buf_free < ep->max_transfer && buf_free > 0) {
868 *buf = 0xff;
869 --buf_free;
871 urb->transfer_buffer_length = ep->max_transfer - buf_free;
874 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
875 .input = snd_usbmidi_emagic_input,
876 .output = snd_usbmidi_emagic_output,
877 .init_out_endpoint = snd_usbmidi_emagic_init_out,
878 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
882 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
884 struct snd_usb_midi* umidi = substream->rmidi->private_data;
885 struct usbmidi_out_port* port = NULL;
886 int i, j;
888 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
889 if (umidi->endpoints[i].out)
890 for (j = 0; j < 0x10; ++j)
891 if (umidi->endpoints[i].out->ports[j].substream == substream) {
892 port = &umidi->endpoints[i].out->ports[j];
893 break;
895 if (!port) {
896 snd_BUG();
897 return -ENXIO;
899 substream->runtime->private_data = port;
900 port->state = STATE_UNKNOWN;
901 return 0;
904 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
906 return 0;
909 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
911 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
913 port->active = up;
914 if (up) {
915 if (port->ep->umidi->chip->shutdown) {
916 /* gobble up remaining bytes to prevent wait in
917 * snd_rawmidi_drain_output */
918 while (!snd_rawmidi_transmit_empty(substream))
919 snd_rawmidi_transmit_ack(substream, 1);
920 return;
922 tasklet_schedule(&port->ep->tasklet);
926 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
928 struct usbmidi_out_port* port = substream->runtime->private_data;
929 struct snd_usb_midi_out_endpoint *ep = port->ep;
930 unsigned int drain_urbs;
931 DEFINE_WAIT(wait);
932 long timeout = msecs_to_jiffies(50);
934 if (ep->umidi->disconnected)
935 return;
937 * The substream buffer is empty, but some data might still be in the
938 * currently active URBs, so we have to wait for those to complete.
940 spin_lock_irq(&ep->buffer_lock);
941 drain_urbs = ep->active_urbs;
942 if (drain_urbs) {
943 ep->drain_urbs |= drain_urbs;
944 do {
945 prepare_to_wait(&ep->drain_wait, &wait,
946 TASK_UNINTERRUPTIBLE);
947 spin_unlock_irq(&ep->buffer_lock);
948 timeout = schedule_timeout(timeout);
949 spin_lock_irq(&ep->buffer_lock);
950 drain_urbs &= ep->drain_urbs;
951 } while (drain_urbs && timeout);
952 finish_wait(&ep->drain_wait, &wait);
954 spin_unlock_irq(&ep->buffer_lock);
957 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
959 return 0;
962 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
964 return 0;
967 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
969 struct snd_usb_midi* umidi = substream->rmidi->private_data;
971 if (up)
972 set_bit(substream->number, &umidi->input_triggered);
973 else
974 clear_bit(substream->number, &umidi->input_triggered);
977 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
978 .open = snd_usbmidi_output_open,
979 .close = snd_usbmidi_output_close,
980 .trigger = snd_usbmidi_output_trigger,
981 .drain = snd_usbmidi_output_drain,
984 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
985 .open = snd_usbmidi_input_open,
986 .close = snd_usbmidi_input_close,
987 .trigger = snd_usbmidi_input_trigger
990 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
991 unsigned int buffer_length)
993 usb_buffer_free(umidi->chip->dev, buffer_length,
994 urb->transfer_buffer, urb->transfer_dma);
995 usb_free_urb(urb);
999 * Frees an input endpoint.
1000 * May be called when ep hasn't been initialized completely.
1002 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
1004 unsigned int i;
1006 for (i = 0; i < INPUT_URBS; ++i)
1007 if (ep->urbs[i])
1008 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1009 ep->urbs[i]->transfer_buffer_length);
1010 kfree(ep);
1014 * Creates an input endpoint.
1016 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1017 struct snd_usb_midi_endpoint_info* ep_info,
1018 struct snd_usb_midi_endpoint* rep)
1020 struct snd_usb_midi_in_endpoint* ep;
1021 void* buffer;
1022 unsigned int pipe;
1023 int length;
1024 unsigned int i;
1026 rep->in = NULL;
1027 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1028 if (!ep)
1029 return -ENOMEM;
1030 ep->umidi = umidi;
1032 for (i = 0; i < INPUT_URBS; ++i) {
1033 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1034 if (!ep->urbs[i]) {
1035 snd_usbmidi_in_endpoint_delete(ep);
1036 return -ENOMEM;
1039 if (ep_info->in_interval)
1040 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
1041 else
1042 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
1043 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
1044 for (i = 0; i < INPUT_URBS; ++i) {
1045 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
1046 &ep->urbs[i]->transfer_dma);
1047 if (!buffer) {
1048 snd_usbmidi_in_endpoint_delete(ep);
1049 return -ENOMEM;
1051 if (ep_info->in_interval)
1052 usb_fill_int_urb(ep->urbs[i], umidi->chip->dev,
1053 pipe, buffer, length,
1054 snd_usbmidi_in_urb_complete,
1055 ep, ep_info->in_interval);
1056 else
1057 usb_fill_bulk_urb(ep->urbs[i], umidi->chip->dev,
1058 pipe, buffer, length,
1059 snd_usbmidi_in_urb_complete, ep);
1060 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1063 rep->in = ep;
1064 return 0;
1067 static unsigned int snd_usbmidi_count_bits(unsigned int x)
1069 unsigned int bits;
1071 for (bits = 0; x; ++bits)
1072 x &= x - 1;
1073 return bits;
1077 * Frees an output endpoint.
1078 * May be called when ep hasn't been initialized completely.
1080 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
1082 unsigned int i;
1084 for (i = 0; i < OUTPUT_URBS; ++i)
1085 if (ep->urbs[i].urb) {
1086 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1087 ep->max_transfer);
1088 ep->urbs[i].urb = NULL;
1092 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1094 snd_usbmidi_out_endpoint_clear(ep);
1095 kfree(ep);
1099 * Creates an output endpoint, and initializes output ports.
1101 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1102 struct snd_usb_midi_endpoint_info* ep_info,
1103 struct snd_usb_midi_endpoint* rep)
1105 struct snd_usb_midi_out_endpoint* ep;
1106 unsigned int i;
1107 unsigned int pipe;
1108 void* buffer;
1110 rep->out = NULL;
1111 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1112 if (!ep)
1113 return -ENOMEM;
1114 ep->umidi = umidi;
1116 for (i = 0; i < OUTPUT_URBS; ++i) {
1117 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1118 if (!ep->urbs[i].urb) {
1119 snd_usbmidi_out_endpoint_delete(ep);
1120 return -ENOMEM;
1122 ep->urbs[i].ep = ep;
1124 if (ep_info->out_interval)
1125 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
1126 else
1127 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
1128 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
1129 ep->max_transfer = 4;
1130 else
1131 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
1132 for (i = 0; i < OUTPUT_URBS; ++i) {
1133 buffer = usb_buffer_alloc(umidi->chip->dev,
1134 ep->max_transfer, GFP_KERNEL,
1135 &ep->urbs[i].urb->transfer_dma);
1136 if (!buffer) {
1137 snd_usbmidi_out_endpoint_delete(ep);
1138 return -ENOMEM;
1140 if (ep_info->out_interval)
1141 usb_fill_int_urb(ep->urbs[i].urb, umidi->chip->dev,
1142 pipe, buffer, ep->max_transfer,
1143 snd_usbmidi_out_urb_complete,
1144 &ep->urbs[i], ep_info->out_interval);
1145 else
1146 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->chip->dev,
1147 pipe, buffer, ep->max_transfer,
1148 snd_usbmidi_out_urb_complete,
1149 &ep->urbs[i]);
1150 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1153 spin_lock_init(&ep->buffer_lock);
1154 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1155 init_waitqueue_head(&ep->drain_wait);
1157 for (i = 0; i < 0x10; ++i)
1158 if (ep_info->out_cables & (1 << i)) {
1159 ep->ports[i].ep = ep;
1160 ep->ports[i].cable = i << 4;
1163 if (umidi->usb_protocol_ops->init_out_endpoint)
1164 umidi->usb_protocol_ops->init_out_endpoint(ep);
1166 rep->out = ep;
1167 return 0;
1171 * Frees everything.
1173 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1175 int i;
1177 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1178 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1179 if (ep->out)
1180 snd_usbmidi_out_endpoint_delete(ep->out);
1181 if (ep->in)
1182 snd_usbmidi_in_endpoint_delete(ep->in);
1184 kfree(umidi);
1188 * Unlinks all URBs (must be done before the usb_device is deleted).
1190 void snd_usbmidi_disconnect(struct list_head* p)
1192 struct snd_usb_midi* umidi;
1193 unsigned int i, j;
1195 umidi = list_entry(p, struct snd_usb_midi, list);
1197 * an URB's completion handler may start the timer and
1198 * a timer may submit an URB. To reliably break the cycle
1199 * a flag under lock must be used
1201 spin_lock_irq(&umidi->disc_lock);
1202 umidi->disconnected = 1;
1203 spin_unlock_irq(&umidi->disc_lock);
1204 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1205 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1206 if (ep->out)
1207 tasklet_kill(&ep->out->tasklet);
1208 if (ep->out) {
1209 for (j = 0; j < OUTPUT_URBS; ++j)
1210 usb_kill_urb(ep->out->urbs[j].urb);
1211 if (umidi->usb_protocol_ops->finish_out_endpoint)
1212 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1213 ep->out->active_urbs = 0;
1214 if (ep->out->drain_urbs) {
1215 ep->out->drain_urbs = 0;
1216 wake_up(&ep->out->drain_wait);
1219 if (ep->in)
1220 for (j = 0; j < INPUT_URBS; ++j)
1221 usb_kill_urb(ep->in->urbs[j]);
1222 /* free endpoints here; later call can result in Oops */
1223 if (ep->out)
1224 snd_usbmidi_out_endpoint_clear(ep->out);
1225 if (ep->in) {
1226 snd_usbmidi_in_endpoint_delete(ep->in);
1227 ep->in = NULL;
1230 del_timer_sync(&umidi->error_timer);
1233 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1235 struct snd_usb_midi* umidi = rmidi->private_data;
1236 snd_usbmidi_free(umidi);
1239 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1240 int stream, int number)
1242 struct list_head* list;
1244 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1245 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1246 if (substream->number == number)
1247 return substream;
1249 return NULL;
1253 * This list specifies names for ports that do not fit into the standard
1254 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1255 * such as internal control or synthesizer ports.
1257 static struct port_info {
1258 u32 id;
1259 short int port;
1260 short int voices;
1261 const char *name;
1262 unsigned int seq_flags;
1263 } snd_usbmidi_port_info[] = {
1264 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1265 { .id = USB_ID(vendor, product), \
1266 .port = num, .voices = voices_, \
1267 .name = name_, .seq_flags = flags }
1268 #define EXTERNAL_PORT(vendor, product, num, name) \
1269 PORT_INFO(vendor, product, num, name, 0, \
1270 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1271 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1272 SNDRV_SEQ_PORT_TYPE_PORT)
1273 #define CONTROL_PORT(vendor, product, num, name) \
1274 PORT_INFO(vendor, product, num, name, 0, \
1275 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1276 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1277 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1278 PORT_INFO(vendor, product, num, name, voices, \
1279 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1280 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1281 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1282 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1283 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1284 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1285 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1286 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1287 PORT_INFO(vendor, product, num, name, voices, \
1288 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1289 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1290 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1291 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1292 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1293 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1294 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1295 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1296 /* Roland UA-100 */
1297 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1298 /* Roland SC-8850 */
1299 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1300 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1301 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1302 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1303 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1304 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1305 /* Roland U-8 */
1306 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1307 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1308 /* Roland SC-8820 */
1309 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1310 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1311 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1312 /* Roland SK-500 */
1313 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1314 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1315 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1316 /* Roland SC-D70 */
1317 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1318 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1319 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1320 /* Edirol UM-880 */
1321 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1322 /* Edirol SD-90 */
1323 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1324 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1325 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1326 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1327 /* Edirol UM-550 */
1328 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1329 /* Edirol SD-20 */
1330 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1331 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1332 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1333 /* Edirol SD-80 */
1334 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1335 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1336 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1337 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1338 /* Edirol UA-700 */
1339 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1340 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1341 /* Roland VariOS */
1342 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1343 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1344 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1345 /* Edirol PCR */
1346 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1347 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1348 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1349 /* BOSS GS-10 */
1350 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1351 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1352 /* Edirol UA-1000 */
1353 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1354 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1355 /* Edirol UR-80 */
1356 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1357 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1358 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1359 /* Edirol PCR-A */
1360 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1361 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1362 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1363 /* Edirol UM-3EX */
1364 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1365 /* M-Audio MidiSport 8x8 */
1366 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1367 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1368 /* MOTU Fastlane */
1369 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1370 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1371 /* Emagic Unitor8/AMT8/MT4 */
1372 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1373 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1374 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1375 /* Access Music Virus TI */
1376 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1377 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1378 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1379 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1380 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
1383 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1385 int i;
1387 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1388 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1389 snd_usbmidi_port_info[i].port == number)
1390 return &snd_usbmidi_port_info[i];
1392 return NULL;
1395 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1396 struct snd_seq_port_info *seq_port_info)
1398 struct snd_usb_midi *umidi = rmidi->private_data;
1399 struct port_info *port_info;
1401 /* TODO: read port flags from descriptors */
1402 port_info = find_port_info(umidi, number);
1403 if (port_info) {
1404 seq_port_info->type = port_info->seq_flags;
1405 seq_port_info->midi_voices = port_info->voices;
1409 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1410 int stream, int number,
1411 struct snd_rawmidi_substream ** rsubstream)
1413 struct port_info *port_info;
1414 const char *name_format;
1416 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1417 if (!substream) {
1418 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1419 return;
1422 /* TODO: read port name from jack descriptor */
1423 port_info = find_port_info(umidi, number);
1424 name_format = port_info ? port_info->name : "%s MIDI %d";
1425 snprintf(substream->name, sizeof(substream->name),
1426 name_format, umidi->chip->card->shortname, number + 1);
1428 *rsubstream = substream;
1432 * Creates the endpoints and their ports.
1434 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1435 struct snd_usb_midi_endpoint_info* endpoints)
1437 int i, j, err;
1438 int out_ports = 0, in_ports = 0;
1440 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1441 if (endpoints[i].out_cables) {
1442 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1443 &umidi->endpoints[i]);
1444 if (err < 0)
1445 return err;
1447 if (endpoints[i].in_cables) {
1448 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1449 &umidi->endpoints[i]);
1450 if (err < 0)
1451 return err;
1454 for (j = 0; j < 0x10; ++j) {
1455 if (endpoints[i].out_cables & (1 << j)) {
1456 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1457 &umidi->endpoints[i].out->ports[j].substream);
1458 ++out_ports;
1460 if (endpoints[i].in_cables & (1 << j)) {
1461 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1462 &umidi->endpoints[i].in->ports[j].substream);
1463 ++in_ports;
1467 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1468 out_ports, in_ports);
1469 return 0;
1473 * Returns MIDIStreaming device capabilities.
1475 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1476 struct snd_usb_midi_endpoint_info* endpoints)
1478 struct usb_interface* intf;
1479 struct usb_host_interface *hostif;
1480 struct usb_interface_descriptor* intfd;
1481 struct usb_ms_header_descriptor* ms_header;
1482 struct usb_host_endpoint *hostep;
1483 struct usb_endpoint_descriptor* ep;
1484 struct usb_ms_endpoint_descriptor* ms_ep;
1485 int i, epidx;
1487 intf = umidi->iface;
1488 if (!intf)
1489 return -ENXIO;
1490 hostif = &intf->altsetting[0];
1491 intfd = get_iface_desc(hostif);
1492 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1493 if (hostif->extralen >= 7 &&
1494 ms_header->bLength >= 7 &&
1495 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1496 ms_header->bDescriptorSubtype == HEADER)
1497 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1498 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1499 else
1500 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1502 epidx = 0;
1503 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1504 hostep = &hostif->endpoint[i];
1505 ep = get_ep_desc(hostep);
1506 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
1507 continue;
1508 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1509 if (hostep->extralen < 4 ||
1510 ms_ep->bLength < 4 ||
1511 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1512 ms_ep->bDescriptorSubtype != MS_GENERAL)
1513 continue;
1514 if (usb_endpoint_dir_out(ep)) {
1515 if (endpoints[epidx].out_ep) {
1516 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1517 snd_printk(KERN_WARNING "too many endpoints\n");
1518 break;
1521 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1522 if (usb_endpoint_xfer_int(ep))
1523 endpoints[epidx].out_interval = ep->bInterval;
1524 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1526 * Low speed bulk transfers don't exist, so
1527 * force interrupt transfers for devices like
1528 * ESI MIDI Mate that try to use them anyway.
1530 endpoints[epidx].out_interval = 1;
1531 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1532 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1533 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1534 } else {
1535 if (endpoints[epidx].in_ep) {
1536 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1537 snd_printk(KERN_WARNING "too many endpoints\n");
1538 break;
1541 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1542 if (usb_endpoint_xfer_int(ep))
1543 endpoints[epidx].in_interval = ep->bInterval;
1544 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1545 endpoints[epidx].in_interval = 1;
1546 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1547 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1548 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1551 return 0;
1555 * On Roland devices, use the second alternate setting to be able to use
1556 * the interrupt input endpoint.
1558 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1560 struct usb_interface* intf;
1561 struct usb_host_interface *hostif;
1562 struct usb_interface_descriptor* intfd;
1564 intf = umidi->iface;
1565 if (!intf || intf->num_altsetting != 2)
1566 return;
1568 hostif = &intf->altsetting[1];
1569 intfd = get_iface_desc(hostif);
1570 if (intfd->bNumEndpoints != 2 ||
1571 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1572 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1573 return;
1575 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1576 intfd->bAlternateSetting);
1577 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1578 intfd->bAlternateSetting);
1582 * Try to find any usable endpoints in the interface.
1584 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1585 struct snd_usb_midi_endpoint_info* endpoint,
1586 int max_endpoints)
1588 struct usb_interface* intf;
1589 struct usb_host_interface *hostif;
1590 struct usb_interface_descriptor* intfd;
1591 struct usb_endpoint_descriptor* epd;
1592 int i, out_eps = 0, in_eps = 0;
1594 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
1595 snd_usbmidi_switch_roland_altsetting(umidi);
1597 if (endpoint[0].out_ep || endpoint[0].in_ep)
1598 return 0;
1600 intf = umidi->iface;
1601 if (!intf || intf->num_altsetting < 1)
1602 return -ENOENT;
1603 hostif = intf->cur_altsetting;
1604 intfd = get_iface_desc(hostif);
1606 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1607 epd = get_endpoint(hostif, i);
1608 if (!usb_endpoint_xfer_bulk(epd) &&
1609 !usb_endpoint_xfer_int(epd))
1610 continue;
1611 if (out_eps < max_endpoints &&
1612 usb_endpoint_dir_out(epd)) {
1613 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1614 if (usb_endpoint_xfer_int(epd))
1615 endpoint[out_eps].out_interval = epd->bInterval;
1616 ++out_eps;
1618 if (in_eps < max_endpoints &&
1619 usb_endpoint_dir_in(epd)) {
1620 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1621 if (usb_endpoint_xfer_int(epd))
1622 endpoint[in_eps].in_interval = epd->bInterval;
1623 ++in_eps;
1626 return (out_eps || in_eps) ? 0 : -ENOENT;
1630 * Detects the endpoints for one-port-per-endpoint protocols.
1632 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1633 struct snd_usb_midi_endpoint_info* endpoints)
1635 int err, i;
1637 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1638 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1639 if (endpoints[i].out_ep)
1640 endpoints[i].out_cables = 0x0001;
1641 if (endpoints[i].in_ep)
1642 endpoints[i].in_cables = 0x0001;
1644 return err;
1648 * Detects the endpoints and ports of Yamaha devices.
1650 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1651 struct snd_usb_midi_endpoint_info* endpoint)
1653 struct usb_interface* intf;
1654 struct usb_host_interface *hostif;
1655 struct usb_interface_descriptor* intfd;
1656 uint8_t* cs_desc;
1658 intf = umidi->iface;
1659 if (!intf)
1660 return -ENOENT;
1661 hostif = intf->altsetting;
1662 intfd = get_iface_desc(hostif);
1663 if (intfd->bNumEndpoints < 1)
1664 return -ENOENT;
1667 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1668 * necessarily with any useful contents. So simply count 'em.
1670 for (cs_desc = hostif->extra;
1671 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1672 cs_desc += cs_desc[0]) {
1673 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1674 if (cs_desc[2] == MIDI_IN_JACK)
1675 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1676 else if (cs_desc[2] == MIDI_OUT_JACK)
1677 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1680 if (!endpoint->in_cables && !endpoint->out_cables)
1681 return -ENOENT;
1683 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1687 * Creates the endpoints and their ports for Midiman devices.
1689 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1690 struct snd_usb_midi_endpoint_info* endpoint)
1692 struct snd_usb_midi_endpoint_info ep_info;
1693 struct usb_interface* intf;
1694 struct usb_host_interface *hostif;
1695 struct usb_interface_descriptor* intfd;
1696 struct usb_endpoint_descriptor* epd;
1697 int cable, err;
1699 intf = umidi->iface;
1700 if (!intf)
1701 return -ENOENT;
1702 hostif = intf->altsetting;
1703 intfd = get_iface_desc(hostif);
1705 * The various MidiSport devices have more or less random endpoint
1706 * numbers, so we have to identify the endpoints by their index in
1707 * the descriptor array, like the driver for that other OS does.
1709 * There is one interrupt input endpoint for all input ports, one
1710 * bulk output endpoint for even-numbered ports, and one for odd-
1711 * numbered ports. Both bulk output endpoints have corresponding
1712 * input bulk endpoints (at indices 1 and 3) which aren't used.
1714 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1715 snd_printdd(KERN_ERR "not enough endpoints\n");
1716 return -ENOENT;
1719 epd = get_endpoint(hostif, 0);
1720 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
1721 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1722 return -ENXIO;
1724 epd = get_endpoint(hostif, 2);
1725 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
1726 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1727 return -ENXIO;
1729 if (endpoint->out_cables > 0x0001) {
1730 epd = get_endpoint(hostif, 4);
1731 if (!usb_endpoint_dir_out(epd) ||
1732 !usb_endpoint_xfer_bulk(epd)) {
1733 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1734 return -ENXIO;
1738 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1739 ep_info.out_interval = 0;
1740 ep_info.out_cables = endpoint->out_cables & 0x5555;
1741 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1742 if (err < 0)
1743 return err;
1745 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1746 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1747 ep_info.in_cables = endpoint->in_cables;
1748 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1749 if (err < 0)
1750 return err;
1752 if (endpoint->out_cables > 0x0001) {
1753 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1754 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1755 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1756 if (err < 0)
1757 return err;
1760 for (cable = 0; cable < 0x10; ++cable) {
1761 if (endpoint->out_cables & (1 << cable))
1762 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1763 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1764 if (endpoint->in_cables & (1 << cable))
1765 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1766 &umidi->endpoints[0].in->ports[cable].substream);
1768 return 0;
1771 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1772 .get_port_info = snd_usbmidi_get_port_info,
1775 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
1776 int out_ports, int in_ports)
1778 struct snd_rawmidi *rmidi;
1779 int err;
1781 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1782 umidi->chip->next_midi_device++,
1783 out_ports, in_ports, &rmidi);
1784 if (err < 0)
1785 return err;
1786 strcpy(rmidi->name, umidi->chip->card->shortname);
1787 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1788 SNDRV_RAWMIDI_INFO_INPUT |
1789 SNDRV_RAWMIDI_INFO_DUPLEX;
1790 rmidi->ops = &snd_usbmidi_ops;
1791 rmidi->private_data = umidi;
1792 rmidi->private_free = snd_usbmidi_rawmidi_free;
1793 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1794 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1796 umidi->rmidi = rmidi;
1797 return 0;
1801 * Temporarily stop input.
1803 void snd_usbmidi_input_stop(struct list_head* p)
1805 struct snd_usb_midi* umidi;
1806 unsigned int i, j;
1808 umidi = list_entry(p, struct snd_usb_midi, list);
1809 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1810 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1811 if (ep->in)
1812 for (j = 0; j < INPUT_URBS; ++j)
1813 usb_kill_urb(ep->in->urbs[j]);
1817 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
1819 unsigned int i;
1821 if (!ep)
1822 return;
1823 for (i = 0; i < INPUT_URBS; ++i) {
1824 struct urb* urb = ep->urbs[i];
1825 urb->dev = ep->umidi->chip->dev;
1826 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1831 * Resume input after a call to snd_usbmidi_input_stop().
1833 void snd_usbmidi_input_start(struct list_head* p)
1835 struct snd_usb_midi* umidi;
1836 int i;
1838 umidi = list_entry(p, struct snd_usb_midi, list);
1839 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1840 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1844 * Creates and registers everything needed for a MIDI streaming interface.
1846 int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
1847 struct usb_interface* iface,
1848 const struct snd_usb_audio_quirk* quirk)
1850 struct snd_usb_midi* umidi;
1851 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
1852 int out_ports, in_ports;
1853 int i, err;
1855 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
1856 if (!umidi)
1857 return -ENOMEM;
1858 umidi->chip = chip;
1859 umidi->iface = iface;
1860 umidi->quirk = quirk;
1861 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
1862 init_timer(&umidi->error_timer);
1863 spin_lock_init(&umidi->disc_lock);
1864 umidi->error_timer.function = snd_usbmidi_error_timer;
1865 umidi->error_timer.data = (unsigned long)umidi;
1867 /* detect the endpoint(s) to use */
1868 memset(endpoints, 0, sizeof(endpoints));
1869 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1870 case QUIRK_MIDI_STANDARD_INTERFACE:
1871 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1872 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1873 umidi->usb_protocol_ops =
1874 &snd_usbmidi_maudio_broken_running_status_ops;
1875 break;
1876 case QUIRK_MIDI_US122L:
1877 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1878 /* fall through */
1879 case QUIRK_MIDI_FIXED_ENDPOINT:
1880 memcpy(&endpoints[0], quirk->data,
1881 sizeof(struct snd_usb_midi_endpoint_info));
1882 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1883 break;
1884 case QUIRK_MIDI_YAMAHA:
1885 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1886 break;
1887 case QUIRK_MIDI_MIDIMAN:
1888 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1889 memcpy(&endpoints[0], quirk->data,
1890 sizeof(struct snd_usb_midi_endpoint_info));
1891 err = 0;
1892 break;
1893 case QUIRK_MIDI_NOVATION:
1894 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1895 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1896 break;
1897 case QUIRK_MIDI_FASTLANE:
1898 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1900 * Interface 1 contains isochronous endpoints, but with the same
1901 * numbers as in interface 0. Since it is interface 1 that the
1902 * USB core has most recently seen, these descriptors are now
1903 * associated with the endpoint numbers. This will foul up our
1904 * attempts to submit bulk/interrupt URBs to the endpoints in
1905 * interface 0, so we have to make sure that the USB core looks
1906 * again at interface 0 by calling usb_set_interface() on it.
1908 usb_set_interface(umidi->chip->dev, 0, 0);
1909 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1910 break;
1911 case QUIRK_MIDI_EMAGIC:
1912 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1913 memcpy(&endpoints[0], quirk->data,
1914 sizeof(struct snd_usb_midi_endpoint_info));
1915 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1916 break;
1917 case QUIRK_MIDI_CME:
1918 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
1919 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1920 break;
1921 default:
1922 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1923 err = -ENXIO;
1924 break;
1926 if (err < 0) {
1927 kfree(umidi);
1928 return err;
1931 /* create rawmidi device */
1932 out_ports = 0;
1933 in_ports = 0;
1934 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1935 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1936 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1938 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1939 if (err < 0) {
1940 kfree(umidi);
1941 return err;
1944 /* create endpoint/port structures */
1945 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1946 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1947 else
1948 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1949 if (err < 0) {
1950 snd_usbmidi_free(umidi);
1951 return err;
1954 list_add(&umidi->list, &umidi->chip->midi_list);
1956 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1957 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1958 return 0;
1961 EXPORT_SYMBOL(snd_usb_create_midi_interface);
1962 EXPORT_SYMBOL(snd_usbmidi_input_stop);
1963 EXPORT_SYMBOL(snd_usbmidi_input_start);
1964 EXPORT_SYMBOL(snd_usbmidi_disconnect);