2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2007 Clemens Ladisch
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
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
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
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 <sound/core.h>
49 #include <sound/rawmidi.h>
50 #include <sound/asequencer.h>
55 * define this to log all USB packets
57 /* #define DUMP_PACKETS */
60 * how long to wait after some USB errors, so that khubd can disconnect() us
61 * without too many spurious errors
63 #define ERROR_DELAY_JIFFIES (HZ / 10)
69 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
70 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
71 MODULE_LICENSE("Dual BSD/GPL");
74 struct usb_ms_header_descriptor {
77 __u8 bDescriptorSubtype;
80 } __attribute__ ((packed));
82 struct usb_ms_endpoint_descriptor {
85 __u8 bDescriptorSubtype;
87 __u8 baAssocJackID[0];
88 } __attribute__ ((packed));
90 struct snd_usb_midi_in_endpoint;
91 struct snd_usb_midi_out_endpoint;
92 struct snd_usb_midi_endpoint;
94 struct usb_protocol_ops {
95 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
96 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
97 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
98 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
99 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
102 struct snd_usb_midi {
103 struct snd_usb_audio *chip;
104 struct usb_interface *iface;
105 const struct snd_usb_audio_quirk *quirk;
106 struct snd_rawmidi *rmidi;
107 struct usb_protocol_ops* usb_protocol_ops;
108 struct list_head list;
109 struct timer_list error_timer;
110 spinlock_t disc_lock;
112 struct snd_usb_midi_endpoint {
113 struct snd_usb_midi_out_endpoint *out;
114 struct snd_usb_midi_in_endpoint *in;
115 } endpoints[MIDI_MAX_ENDPOINTS];
116 unsigned long input_triggered;
117 unsigned char disconnected;
120 struct snd_usb_midi_out_endpoint {
121 struct snd_usb_midi* umidi;
122 struct out_urb_context {
124 struct snd_usb_midi_out_endpoint *ep;
126 unsigned int active_urbs;
127 int max_transfer; /* size of urb buffer */
128 struct tasklet_struct tasklet;
130 spinlock_t buffer_lock;
132 struct usbmidi_out_port {
133 struct snd_usb_midi_out_endpoint* ep;
134 struct snd_rawmidi_substream *substream;
136 uint8_t cable; /* cable number << 4 */
138 #define STATE_UNKNOWN 0
139 #define STATE_1PARAM 1
140 #define STATE_2PARAM_1 2
141 #define STATE_2PARAM_2 3
142 #define STATE_SYSEX_0 4
143 #define STATE_SYSEX_1 5
144 #define STATE_SYSEX_2 6
150 struct snd_usb_midi_in_endpoint {
151 struct snd_usb_midi* umidi;
152 struct urb* urbs[INPUT_URBS];
153 struct usbmidi_in_port {
154 struct snd_rawmidi_substream *substream;
155 u8 running_status_length;
162 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
164 static const uint8_t snd_usbmidi_cin_length[] = {
165 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
169 * Submits the URB, with error handling.
171 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
173 int err = usb_submit_urb(urb, flags);
174 if (err < 0 && err != -ENODEV)
175 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
180 * Error handling for URB completion functions.
182 static int snd_usbmidi_urb_error(int status)
185 /* manually unlinked, or device gone */
191 /* errors that might occur during unplugging */
197 snd_printk(KERN_ERR "urb status %d\n", status);
198 return 0; /* continue */
203 * Receives a chunk of MIDI data.
205 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
206 uint8_t* data, int length)
208 struct usbmidi_in_port* port = &ep->ports[portidx];
210 if (!port->substream) {
211 snd_printd("unexpected port %d!\n", portidx);
214 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
216 snd_rawmidi_receive(port->substream, data, length);
220 static void dump_urb(const char *type, const u8 *data, int length)
222 snd_printk(KERN_DEBUG "%s packet: [", type);
223 for (; length > 0; ++data, --length)
224 printk(" %02x", *data);
228 #define dump_urb(type, data, length) /* nothing */
232 * Processes the data read from the device.
234 static void snd_usbmidi_in_urb_complete(struct urb* urb)
236 struct snd_usb_midi_in_endpoint* ep = urb->context;
238 if (urb->status == 0) {
239 dump_urb("received", urb->transfer_buffer, urb->actual_length);
240 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
243 int err = snd_usbmidi_urb_error(urb->status);
245 if (err != -ENODEV) {
246 ep->error_resubmit = 1;
247 mod_timer(&ep->umidi->error_timer,
248 jiffies + ERROR_DELAY_JIFFIES);
254 urb->dev = ep->umidi->chip->dev;
255 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
258 static void snd_usbmidi_out_urb_complete(struct urb* urb)
260 struct out_urb_context *context = urb->context;
261 struct snd_usb_midi_out_endpoint* ep = context->ep;
263 spin_lock(&ep->buffer_lock);
264 ep->active_urbs &= ~(1 << (context - ep->urbs));
265 spin_unlock(&ep->buffer_lock);
266 if (urb->status < 0) {
267 int err = snd_usbmidi_urb_error(urb->status);
270 mod_timer(&ep->umidi->error_timer,
271 jiffies + ERROR_DELAY_JIFFIES);
275 snd_usbmidi_do_output(ep);
279 * This is called when some data should be transferred to the device
280 * (from one or more substreams).
282 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
284 unsigned int urb_index;
288 spin_lock_irqsave(&ep->buffer_lock, flags);
289 if (ep->umidi->chip->shutdown) {
290 spin_unlock_irqrestore(&ep->buffer_lock, flags);
296 for (urb_index = 0; urb_index < OUTPUT_URBS; ++urb_index)
297 if (!(ep->active_urbs & (1 << urb_index))) {
298 urb = ep->urbs[urb_index].urb;
304 urb->transfer_buffer_length = 0;
305 ep->umidi->usb_protocol_ops->output(ep, urb);
306 if (urb->transfer_buffer_length == 0)
309 dump_urb("sending", urb->transfer_buffer,
310 urb->transfer_buffer_length);
311 urb->dev = ep->umidi->chip->dev;
312 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
314 ep->active_urbs |= 1 << urb_index;
316 spin_unlock_irqrestore(&ep->buffer_lock, flags);
319 static void snd_usbmidi_out_tasklet(unsigned long data)
321 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
323 snd_usbmidi_do_output(ep);
326 /* called after transfers had been interrupted due to some USB error */
327 static void snd_usbmidi_error_timer(unsigned long data)
329 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
332 spin_lock(&umidi->disc_lock);
333 if (umidi->disconnected) {
334 spin_unlock(&umidi->disc_lock);
337 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
338 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
339 if (in && in->error_resubmit) {
340 in->error_resubmit = 0;
341 for (j = 0; j < INPUT_URBS; ++j) {
342 in->urbs[j]->dev = umidi->chip->dev;
343 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
346 if (umidi->endpoints[i].out)
347 snd_usbmidi_do_output(umidi->endpoints[i].out);
349 spin_unlock(&umidi->disc_lock);
352 /* helper function to send static data that may not DMA-able */
353 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
354 const void *data, int len)
357 void *buf = kmemdup(data, len, GFP_KERNEL);
360 dump_urb("sending", buf, len);
362 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urbs[0].urb->pipe,
363 buf, len, NULL, 250);
369 * Standard USB MIDI protocol: see the spec.
370 * Midiman protocol: like the standard protocol, but the control byte is the
371 * fourth byte in each packet, and uses length instead of CIN.
374 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
375 uint8_t* buffer, int buffer_length)
379 for (i = 0; i + 3 < buffer_length; i += 4)
380 if (buffer[i] != 0) {
381 int cable = buffer[i] >> 4;
382 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
383 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
387 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
388 uint8_t* buffer, int buffer_length)
392 for (i = 0; i + 3 < buffer_length; i += 4)
393 if (buffer[i + 3] != 0) {
394 int port = buffer[i + 3] >> 4;
395 int length = buffer[i + 3] & 3;
396 snd_usbmidi_input_data(ep, port, &buffer[i], length);
401 * Buggy M-Audio device: running status on input results in a packet that has
402 * the data bytes but not the status byte and that is marked with CIN 4.
404 static void snd_usbmidi_maudio_broken_running_status_input(
405 struct snd_usb_midi_in_endpoint* ep,
406 uint8_t* buffer, int buffer_length)
410 for (i = 0; i + 3 < buffer_length; i += 4)
411 if (buffer[i] != 0) {
412 int cable = buffer[i] >> 4;
413 u8 cin = buffer[i] & 0x0f;
414 struct usbmidi_in_port *port = &ep->ports[cable];
417 length = snd_usbmidi_cin_length[cin];
418 if (cin == 0xf && buffer[i + 1] >= 0xf8)
419 ; /* realtime msg: no running status change */
420 else if (cin >= 0x8 && cin <= 0xe)
422 port->running_status_length = length - 1;
423 else if (cin == 0x4 &&
424 port->running_status_length != 0 &&
425 buffer[i + 1] < 0x80)
426 /* CIN 4 that is not a SysEx */
427 length = port->running_status_length;
430 * All other msgs cannot begin running status.
431 * (A channel msg sent as two or three CIN 0xF
432 * packets could in theory, but this device
433 * doesn't use this format.)
435 port->running_status_length = 0;
436 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
441 * CME protocol: like the standard protocol, but SysEx commands are sent as a
442 * single USB packet preceded by a 0x0F byte.
444 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
445 uint8_t *buffer, int buffer_length)
447 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
448 snd_usbmidi_standard_input(ep, buffer, buffer_length);
450 snd_usbmidi_input_data(ep, buffer[0] >> 4,
451 &buffer[1], buffer_length - 1);
455 * Adds one USB MIDI packet to the output buffer.
457 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
458 uint8_t p1, uint8_t p2, uint8_t p3)
461 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
466 urb->transfer_buffer_length += 4;
470 * Adds one Midiman packet to the output buffer.
472 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
473 uint8_t p1, uint8_t p2, uint8_t p3)
476 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
480 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
481 urb->transfer_buffer_length += 4;
485 * Converts MIDI commands to USB MIDI packets.
487 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
488 uint8_t b, struct urb* urb)
490 uint8_t p0 = port->cable;
491 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
492 port->ep->umidi->usb_protocol_ops->output_packet;
495 output_packet(urb, p0 | 0x0f, b, 0, 0);
496 } else if (b >= 0xf0) {
500 port->state = STATE_SYSEX_1;
505 port->state = STATE_1PARAM;
509 port->state = STATE_2PARAM_1;
513 port->state = STATE_UNKNOWN;
516 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
517 port->state = STATE_UNKNOWN;
520 switch (port->state) {
522 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
525 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
528 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
531 port->state = STATE_UNKNOWN;
534 } else if (b >= 0x80) {
536 if (b >= 0xc0 && b <= 0xdf)
537 port->state = STATE_1PARAM;
539 port->state = STATE_2PARAM_1;
540 } else { /* b < 0x80 */
541 switch (port->state) {
543 if (port->data[0] < 0xf0) {
544 p0 |= port->data[0] >> 4;
547 port->state = STATE_UNKNOWN;
549 output_packet(urb, p0, port->data[0], b, 0);
553 port->state = STATE_2PARAM_2;
556 if (port->data[0] < 0xf0) {
557 p0 |= port->data[0] >> 4;
558 port->state = STATE_2PARAM_1;
561 port->state = STATE_UNKNOWN;
563 output_packet(urb, p0, port->data[0], port->data[1], b);
567 port->state = STATE_SYSEX_1;
571 port->state = STATE_SYSEX_2;
574 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
575 port->state = STATE_SYSEX_0;
581 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
586 /* FIXME: lower-numbered ports can starve higher-numbered ports */
587 for (p = 0; p < 0x10; ++p) {
588 struct usbmidi_out_port* port = &ep->ports[p];
591 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
593 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
597 snd_usbmidi_transmit_byte(port, b, urb);
602 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
603 .input = snd_usbmidi_standard_input,
604 .output = snd_usbmidi_standard_output,
605 .output_packet = snd_usbmidi_output_standard_packet,
608 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
609 .input = snd_usbmidi_midiman_input,
610 .output = snd_usbmidi_standard_output,
611 .output_packet = snd_usbmidi_output_midiman_packet,
614 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
615 .input = snd_usbmidi_maudio_broken_running_status_input,
616 .output = snd_usbmidi_standard_output,
617 .output_packet = snd_usbmidi_output_standard_packet,
620 static struct usb_protocol_ops snd_usbmidi_cme_ops = {
621 .input = snd_usbmidi_cme_input,
622 .output = snd_usbmidi_standard_output,
623 .output_packet = snd_usbmidi_output_standard_packet,
627 * Novation USB MIDI protocol: number of data bytes is in the first byte
628 * (when receiving) (+1!) or in the second byte (when sending); data begins
632 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
633 uint8_t* buffer, int buffer_length)
635 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
637 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
640 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
643 uint8_t* transfer_buffer;
646 if (!ep->ports[0].active)
648 transfer_buffer = urb->transfer_buffer;
649 count = snd_rawmidi_transmit(ep->ports[0].substream,
651 ep->max_transfer - 2);
653 ep->ports[0].active = 0;
656 transfer_buffer[0] = 0;
657 transfer_buffer[1] = count;
658 urb->transfer_buffer_length = 2 + count;
661 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
662 .input = snd_usbmidi_novation_input,
663 .output = snd_usbmidi_novation_output,
667 * "raw" protocol: used by the MOTU FastLane.
670 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
671 uint8_t* buffer, int buffer_length)
673 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
676 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
681 if (!ep->ports[0].active)
683 count = snd_rawmidi_transmit(ep->ports[0].substream,
684 urb->transfer_buffer,
687 ep->ports[0].active = 0;
690 urb->transfer_buffer_length = count;
693 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
694 .input = snd_usbmidi_raw_input,
695 .output = snd_usbmidi_raw_output,
698 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
699 uint8_t *buffer, int buffer_length)
701 if (buffer_length != 9)
704 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
707 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
710 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
715 if (!ep->ports[0].active)
717 count = snd_usb_get_speed(ep->umidi->chip->dev) == USB_SPEED_HIGH
719 count = snd_rawmidi_transmit(ep->ports[0].substream,
720 urb->transfer_buffer,
723 ep->ports[0].active = 0;
727 memset(urb->transfer_buffer + count, 0xFD, 9 - count);
728 urb->transfer_buffer_length = count;
731 static struct usb_protocol_ops snd_usbmidi_122l_ops = {
732 .input = snd_usbmidi_us122l_input,
733 .output = snd_usbmidi_us122l_output,
737 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
740 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
742 static const u8 init_data[] = {
743 /* initialization magic: "get version" */
745 0x00, 0x20, 0x31, /* Emagic */
747 0x0b, /* version number request */
748 0x00, /* command version */
749 0x00, /* EEPROM, box 0 */
752 send_bulk_static_data(ep, init_data, sizeof(init_data));
753 /* while we're at it, pour on more magic */
754 send_bulk_static_data(ep, init_data, sizeof(init_data));
757 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
759 static const u8 finish_data[] = {
760 /* switch to patch mode with last preset */
762 0x00, 0x20, 0x31, /* Emagic */
764 0x10, /* patch switch command */
765 0x00, /* command version */
766 0x7f, /* to all boxes */
767 0x40, /* last preset in EEPROM */
770 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
773 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
774 uint8_t* buffer, int buffer_length)
778 /* FF indicates end of valid data */
779 for (i = 0; i < buffer_length; ++i)
780 if (buffer[i] == 0xff) {
785 /* handle F5 at end of last buffer */
789 while (buffer_length > 0) {
790 /* determine size of data until next F5 */
791 for (i = 0; i < buffer_length; ++i)
792 if (buffer[i] == 0xf5)
794 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
798 if (buffer_length <= 0)
800 /* assert(buffer[0] == 0xf5); */
806 if (buffer_length <= 0)
808 if (buffer[0] < 0x80) {
809 ep->current_port = (buffer[0] - 1) & 15;
817 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
820 int port0 = ep->current_port;
821 uint8_t* buf = urb->transfer_buffer;
822 int buf_free = ep->max_transfer;
825 for (i = 0; i < 0x10; ++i) {
826 /* round-robin, starting at the last current port */
827 int portnum = (port0 + i) & 15;
828 struct usbmidi_out_port* port = &ep->ports[portnum];
832 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
837 if (portnum != ep->current_port) {
840 ep->current_port = portnum;
842 buf[1] = (portnum + 1) & 15;
849 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
857 if (buf_free < ep->max_transfer && buf_free > 0) {
861 urb->transfer_buffer_length = ep->max_transfer - buf_free;
864 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
865 .input = snd_usbmidi_emagic_input,
866 .output = snd_usbmidi_emagic_output,
867 .init_out_endpoint = snd_usbmidi_emagic_init_out,
868 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
872 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
874 struct snd_usb_midi* umidi = substream->rmidi->private_data;
875 struct usbmidi_out_port* port = NULL;
878 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
879 if (umidi->endpoints[i].out)
880 for (j = 0; j < 0x10; ++j)
881 if (umidi->endpoints[i].out->ports[j].substream == substream) {
882 port = &umidi->endpoints[i].out->ports[j];
889 substream->runtime->private_data = port;
890 port->state = STATE_UNKNOWN;
894 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
899 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
901 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
905 if (port->ep->umidi->chip->shutdown) {
906 /* gobble up remaining bytes to prevent wait in
907 * snd_rawmidi_drain_output */
908 while (!snd_rawmidi_transmit_empty(substream))
909 snd_rawmidi_transmit_ack(substream, 1);
912 tasklet_schedule(&port->ep->tasklet);
916 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
921 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
926 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
928 struct snd_usb_midi* umidi = substream->rmidi->private_data;
931 set_bit(substream->number, &umidi->input_triggered);
933 clear_bit(substream->number, &umidi->input_triggered);
936 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
937 .open = snd_usbmidi_output_open,
938 .close = snd_usbmidi_output_close,
939 .trigger = snd_usbmidi_output_trigger,
942 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
943 .open = snd_usbmidi_input_open,
944 .close = snd_usbmidi_input_close,
945 .trigger = snd_usbmidi_input_trigger
948 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
949 unsigned int buffer_length)
951 usb_buffer_free(umidi->chip->dev, buffer_length,
952 urb->transfer_buffer, urb->transfer_dma);
957 * Frees an input endpoint.
958 * May be called when ep hasn't been initialized completely.
960 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
964 for (i = 0; i < INPUT_URBS; ++i)
966 free_urb_and_buffer(ep->umidi, ep->urbs[i],
967 ep->urbs[i]->transfer_buffer_length);
972 * Creates an input endpoint.
974 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
975 struct snd_usb_midi_endpoint_info* ep_info,
976 struct snd_usb_midi_endpoint* rep)
978 struct snd_usb_midi_in_endpoint* ep;
985 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
990 for (i = 0; i < INPUT_URBS; ++i) {
991 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
993 snd_usbmidi_in_endpoint_delete(ep);
997 if (ep_info->in_interval)
998 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
1000 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
1001 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
1002 for (i = 0; i < INPUT_URBS; ++i) {
1003 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
1004 &ep->urbs[i]->transfer_dma);
1006 snd_usbmidi_in_endpoint_delete(ep);
1009 if (ep_info->in_interval)
1010 usb_fill_int_urb(ep->urbs[i], umidi->chip->dev,
1011 pipe, buffer, length,
1012 snd_usbmidi_in_urb_complete,
1013 ep, ep_info->in_interval);
1015 usb_fill_bulk_urb(ep->urbs[i], umidi->chip->dev,
1016 pipe, buffer, length,
1017 snd_usbmidi_in_urb_complete, ep);
1018 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1025 static unsigned int snd_usbmidi_count_bits(unsigned int x)
1029 for (bits = 0; x; ++bits)
1035 * Frees an output endpoint.
1036 * May be called when ep hasn't been initialized completely.
1038 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
1042 for (i = 0; i < OUTPUT_URBS; ++i)
1043 if (ep->urbs[i].urb)
1044 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1050 * Creates an output endpoint, and initializes output ports.
1052 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1053 struct snd_usb_midi_endpoint_info* ep_info,
1054 struct snd_usb_midi_endpoint* rep)
1056 struct snd_usb_midi_out_endpoint* ep;
1062 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1067 for (i = 0; i < OUTPUT_URBS; ++i) {
1068 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1069 if (!ep->urbs[i].urb) {
1070 snd_usbmidi_out_endpoint_delete(ep);
1073 ep->urbs[i].ep = ep;
1075 if (ep_info->out_interval)
1076 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
1078 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
1079 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
1080 ep->max_transfer = 4;
1082 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
1083 for (i = 0; i < OUTPUT_URBS; ++i) {
1084 buffer = usb_buffer_alloc(umidi->chip->dev,
1085 ep->max_transfer, GFP_KERNEL,
1086 &ep->urbs[i].urb->transfer_dma);
1088 snd_usbmidi_out_endpoint_delete(ep);
1091 if (ep_info->out_interval)
1092 usb_fill_int_urb(ep->urbs[i].urb, umidi->chip->dev,
1093 pipe, buffer, ep->max_transfer,
1094 snd_usbmidi_out_urb_complete,
1095 &ep->urbs[i], ep_info->out_interval);
1097 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->chip->dev,
1098 pipe, buffer, ep->max_transfer,
1099 snd_usbmidi_out_urb_complete,
1101 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1104 spin_lock_init(&ep->buffer_lock);
1105 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1107 for (i = 0; i < 0x10; ++i)
1108 if (ep_info->out_cables & (1 << i)) {
1109 ep->ports[i].ep = ep;
1110 ep->ports[i].cable = i << 4;
1113 if (umidi->usb_protocol_ops->init_out_endpoint)
1114 umidi->usb_protocol_ops->init_out_endpoint(ep);
1123 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1127 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1128 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1130 snd_usbmidi_out_endpoint_delete(ep->out);
1132 snd_usbmidi_in_endpoint_delete(ep->in);
1138 * Unlinks all URBs (must be done before the usb_device is deleted).
1140 void snd_usbmidi_disconnect(struct list_head* p)
1142 struct snd_usb_midi* umidi;
1145 umidi = list_entry(p, struct snd_usb_midi, list);
1147 * an URB's completion handler may start the timer and
1148 * a timer may submit an URB. To reliably break the cycle
1149 * a flag under lock must be used
1151 spin_lock_irq(&umidi->disc_lock);
1152 umidi->disconnected = 1;
1153 spin_unlock_irq(&umidi->disc_lock);
1154 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1155 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1157 tasklet_kill(&ep->out->tasklet);
1159 for (j = 0; j < OUTPUT_URBS; ++j)
1160 usb_kill_urb(ep->out->urbs[j].urb);
1161 if (umidi->usb_protocol_ops->finish_out_endpoint)
1162 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1165 for (j = 0; j < INPUT_URBS; ++j)
1166 usb_kill_urb(ep->in->urbs[j]);
1167 /* free endpoints here; later call can result in Oops */
1169 snd_usbmidi_out_endpoint_delete(ep->out);
1173 snd_usbmidi_in_endpoint_delete(ep->in);
1177 del_timer_sync(&umidi->error_timer);
1180 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1182 struct snd_usb_midi* umidi = rmidi->private_data;
1183 snd_usbmidi_free(umidi);
1186 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1187 int stream, int number)
1189 struct list_head* list;
1191 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1192 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1193 if (substream->number == number)
1200 * This list specifies names for ports that do not fit into the standard
1201 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1202 * such as internal control or synthesizer ports.
1204 static struct port_info {
1209 unsigned int seq_flags;
1210 } snd_usbmidi_port_info[] = {
1211 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1212 { .id = USB_ID(vendor, product), \
1213 .port = num, .voices = voices_, \
1214 .name = name_, .seq_flags = flags }
1215 #define EXTERNAL_PORT(vendor, product, num, name) \
1216 PORT_INFO(vendor, product, num, name, 0, \
1217 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1218 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1219 SNDRV_SEQ_PORT_TYPE_PORT)
1220 #define CONTROL_PORT(vendor, product, num, name) \
1221 PORT_INFO(vendor, product, num, name, 0, \
1222 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1223 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1224 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1225 PORT_INFO(vendor, product, num, name, voices, \
1226 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1227 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1228 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1229 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1230 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1231 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1232 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1233 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1234 PORT_INFO(vendor, product, num, name, voices, \
1235 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1236 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1237 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1238 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1239 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1240 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1241 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1242 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1244 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1245 /* Roland SC-8850 */
1246 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1247 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1248 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1249 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1250 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1251 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1253 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1254 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1255 /* Roland SC-8820 */
1256 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1257 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1258 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1260 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1261 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1262 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1264 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1265 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1266 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1268 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1270 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1271 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1272 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1273 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1275 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1277 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1278 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1279 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1281 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1282 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1283 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1284 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1286 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1287 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1289 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1290 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1291 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1293 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1294 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1295 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1297 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1298 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1299 /* Edirol UA-1000 */
1300 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1301 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1303 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1304 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1305 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1307 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1308 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1309 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1311 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1312 /* M-Audio MidiSport 8x8 */
1313 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1314 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1316 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1317 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1318 /* Emagic Unitor8/AMT8/MT4 */
1319 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1320 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1321 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1324 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1328 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1329 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1330 snd_usbmidi_port_info[i].port == number)
1331 return &snd_usbmidi_port_info[i];
1336 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1337 struct snd_seq_port_info *seq_port_info)
1339 struct snd_usb_midi *umidi = rmidi->private_data;
1340 struct port_info *port_info;
1342 /* TODO: read port flags from descriptors */
1343 port_info = find_port_info(umidi, number);
1345 seq_port_info->type = port_info->seq_flags;
1346 seq_port_info->midi_voices = port_info->voices;
1350 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1351 int stream, int number,
1352 struct snd_rawmidi_substream ** rsubstream)
1354 struct port_info *port_info;
1355 const char *name_format;
1357 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1359 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1363 /* TODO: read port name from jack descriptor */
1364 port_info = find_port_info(umidi, number);
1365 name_format = port_info ? port_info->name : "%s MIDI %d";
1366 snprintf(substream->name, sizeof(substream->name),
1367 name_format, umidi->chip->card->shortname, number + 1);
1369 *rsubstream = substream;
1373 * Creates the endpoints and their ports.
1375 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1376 struct snd_usb_midi_endpoint_info* endpoints)
1379 int out_ports = 0, in_ports = 0;
1381 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1382 if (endpoints[i].out_cables) {
1383 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1384 &umidi->endpoints[i]);
1388 if (endpoints[i].in_cables) {
1389 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1390 &umidi->endpoints[i]);
1395 for (j = 0; j < 0x10; ++j) {
1396 if (endpoints[i].out_cables & (1 << j)) {
1397 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1398 &umidi->endpoints[i].out->ports[j].substream);
1401 if (endpoints[i].in_cables & (1 << j)) {
1402 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1403 &umidi->endpoints[i].in->ports[j].substream);
1408 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1409 out_ports, in_ports);
1414 * Returns MIDIStreaming device capabilities.
1416 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1417 struct snd_usb_midi_endpoint_info* endpoints)
1419 struct usb_interface* intf;
1420 struct usb_host_interface *hostif;
1421 struct usb_interface_descriptor* intfd;
1422 struct usb_ms_header_descriptor* ms_header;
1423 struct usb_host_endpoint *hostep;
1424 struct usb_endpoint_descriptor* ep;
1425 struct usb_ms_endpoint_descriptor* ms_ep;
1428 intf = umidi->iface;
1431 hostif = &intf->altsetting[0];
1432 intfd = get_iface_desc(hostif);
1433 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1434 if (hostif->extralen >= 7 &&
1435 ms_header->bLength >= 7 &&
1436 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1437 ms_header->bDescriptorSubtype == HEADER)
1438 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1439 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1441 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1444 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1445 hostep = &hostif->endpoint[i];
1446 ep = get_ep_desc(hostep);
1447 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
1449 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1450 if (hostep->extralen < 4 ||
1451 ms_ep->bLength < 4 ||
1452 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1453 ms_ep->bDescriptorSubtype != MS_GENERAL)
1455 if (usb_endpoint_dir_out(ep)) {
1456 if (endpoints[epidx].out_ep) {
1457 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1458 snd_printk(KERN_WARNING "too many endpoints\n");
1462 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1463 if (usb_endpoint_xfer_int(ep))
1464 endpoints[epidx].out_interval = ep->bInterval;
1465 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1467 * Low speed bulk transfers don't exist, so
1468 * force interrupt transfers for devices like
1469 * ESI MIDI Mate that try to use them anyway.
1471 endpoints[epidx].out_interval = 1;
1472 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1473 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1474 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1476 if (endpoints[epidx].in_ep) {
1477 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1478 snd_printk(KERN_WARNING "too many endpoints\n");
1482 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1483 if (usb_endpoint_xfer_int(ep))
1484 endpoints[epidx].in_interval = ep->bInterval;
1485 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1486 endpoints[epidx].in_interval = 1;
1487 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1488 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1489 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1496 * On Roland devices, use the second alternate setting to be able to use
1497 * the interrupt input endpoint.
1499 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1501 struct usb_interface* intf;
1502 struct usb_host_interface *hostif;
1503 struct usb_interface_descriptor* intfd;
1505 intf = umidi->iface;
1506 if (!intf || intf->num_altsetting != 2)
1509 hostif = &intf->altsetting[1];
1510 intfd = get_iface_desc(hostif);
1511 if (intfd->bNumEndpoints != 2 ||
1512 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1513 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1516 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1517 intfd->bAlternateSetting);
1518 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1519 intfd->bAlternateSetting);
1523 * Try to find any usable endpoints in the interface.
1525 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1526 struct snd_usb_midi_endpoint_info* endpoint,
1529 struct usb_interface* intf;
1530 struct usb_host_interface *hostif;
1531 struct usb_interface_descriptor* intfd;
1532 struct usb_endpoint_descriptor* epd;
1533 int i, out_eps = 0, in_eps = 0;
1535 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
1536 snd_usbmidi_switch_roland_altsetting(umidi);
1538 if (endpoint[0].out_ep || endpoint[0].in_ep)
1541 intf = umidi->iface;
1542 if (!intf || intf->num_altsetting < 1)
1544 hostif = intf->cur_altsetting;
1545 intfd = get_iface_desc(hostif);
1547 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1548 epd = get_endpoint(hostif, i);
1549 if (!usb_endpoint_xfer_bulk(epd) &&
1550 !usb_endpoint_xfer_int(epd))
1552 if (out_eps < max_endpoints &&
1553 usb_endpoint_dir_out(epd)) {
1554 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1555 if (usb_endpoint_xfer_int(epd))
1556 endpoint[out_eps].out_interval = epd->bInterval;
1559 if (in_eps < max_endpoints &&
1560 usb_endpoint_dir_in(epd)) {
1561 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1562 if (usb_endpoint_xfer_int(epd))
1563 endpoint[in_eps].in_interval = epd->bInterval;
1567 return (out_eps || in_eps) ? 0 : -ENOENT;
1571 * Detects the endpoints for one-port-per-endpoint protocols.
1573 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1574 struct snd_usb_midi_endpoint_info* endpoints)
1578 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1579 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1580 if (endpoints[i].out_ep)
1581 endpoints[i].out_cables = 0x0001;
1582 if (endpoints[i].in_ep)
1583 endpoints[i].in_cables = 0x0001;
1589 * Detects the endpoints and ports of Yamaha devices.
1591 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1592 struct snd_usb_midi_endpoint_info* endpoint)
1594 struct usb_interface* intf;
1595 struct usb_host_interface *hostif;
1596 struct usb_interface_descriptor* intfd;
1599 intf = umidi->iface;
1602 hostif = intf->altsetting;
1603 intfd = get_iface_desc(hostif);
1604 if (intfd->bNumEndpoints < 1)
1608 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1609 * necessarily with any useful contents. So simply count 'em.
1611 for (cs_desc = hostif->extra;
1612 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1613 cs_desc += cs_desc[0]) {
1614 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1615 if (cs_desc[2] == MIDI_IN_JACK)
1616 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1617 else if (cs_desc[2] == MIDI_OUT_JACK)
1618 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1621 if (!endpoint->in_cables && !endpoint->out_cables)
1624 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1628 * Creates the endpoints and their ports for Midiman devices.
1630 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1631 struct snd_usb_midi_endpoint_info* endpoint)
1633 struct snd_usb_midi_endpoint_info ep_info;
1634 struct usb_interface* intf;
1635 struct usb_host_interface *hostif;
1636 struct usb_interface_descriptor* intfd;
1637 struct usb_endpoint_descriptor* epd;
1640 intf = umidi->iface;
1643 hostif = intf->altsetting;
1644 intfd = get_iface_desc(hostif);
1646 * The various MidiSport devices have more or less random endpoint
1647 * numbers, so we have to identify the endpoints by their index in
1648 * the descriptor array, like the driver for that other OS does.
1650 * There is one interrupt input endpoint for all input ports, one
1651 * bulk output endpoint for even-numbered ports, and one for odd-
1652 * numbered ports. Both bulk output endpoints have corresponding
1653 * input bulk endpoints (at indices 1 and 3) which aren't used.
1655 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1656 snd_printdd(KERN_ERR "not enough endpoints\n");
1660 epd = get_endpoint(hostif, 0);
1661 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
1662 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1665 epd = get_endpoint(hostif, 2);
1666 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
1667 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1670 if (endpoint->out_cables > 0x0001) {
1671 epd = get_endpoint(hostif, 4);
1672 if (!usb_endpoint_dir_out(epd) ||
1673 !usb_endpoint_xfer_bulk(epd)) {
1674 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1679 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1680 ep_info.out_interval = 0;
1681 ep_info.out_cables = endpoint->out_cables & 0x5555;
1682 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1686 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1687 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1688 ep_info.in_cables = endpoint->in_cables;
1689 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1693 if (endpoint->out_cables > 0x0001) {
1694 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1695 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1696 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1701 for (cable = 0; cable < 0x10; ++cable) {
1702 if (endpoint->out_cables & (1 << cable))
1703 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1704 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1705 if (endpoint->in_cables & (1 << cable))
1706 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1707 &umidi->endpoints[0].in->ports[cable].substream);
1712 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1713 .get_port_info = snd_usbmidi_get_port_info,
1716 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
1717 int out_ports, int in_ports)
1719 struct snd_rawmidi *rmidi;
1722 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1723 umidi->chip->next_midi_device++,
1724 out_ports, in_ports, &rmidi);
1727 strcpy(rmidi->name, umidi->chip->card->shortname);
1728 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1729 SNDRV_RAWMIDI_INFO_INPUT |
1730 SNDRV_RAWMIDI_INFO_DUPLEX;
1731 rmidi->ops = &snd_usbmidi_ops;
1732 rmidi->private_data = umidi;
1733 rmidi->private_free = snd_usbmidi_rawmidi_free;
1734 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1735 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1737 umidi->rmidi = rmidi;
1742 * Temporarily stop input.
1744 void snd_usbmidi_input_stop(struct list_head* p)
1746 struct snd_usb_midi* umidi;
1749 umidi = list_entry(p, struct snd_usb_midi, list);
1750 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1751 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1753 for (j = 0; j < INPUT_URBS; ++j)
1754 usb_kill_urb(ep->in->urbs[j]);
1758 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
1764 for (i = 0; i < INPUT_URBS; ++i) {
1765 struct urb* urb = ep->urbs[i];
1766 urb->dev = ep->umidi->chip->dev;
1767 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1772 * Resume input after a call to snd_usbmidi_input_stop().
1774 void snd_usbmidi_input_start(struct list_head* p)
1776 struct snd_usb_midi* umidi;
1779 umidi = list_entry(p, struct snd_usb_midi, list);
1780 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1781 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1785 * Creates and registers everything needed for a MIDI streaming interface.
1787 int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
1788 struct usb_interface* iface,
1789 const struct snd_usb_audio_quirk* quirk)
1791 struct snd_usb_midi* umidi;
1792 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
1793 int out_ports, in_ports;
1796 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
1800 umidi->iface = iface;
1801 umidi->quirk = quirk;
1802 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
1803 init_timer(&umidi->error_timer);
1804 spin_lock_init(&umidi->disc_lock);
1805 umidi->error_timer.function = snd_usbmidi_error_timer;
1806 umidi->error_timer.data = (unsigned long)umidi;
1808 /* detect the endpoint(s) to use */
1809 memset(endpoints, 0, sizeof(endpoints));
1810 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1811 case QUIRK_MIDI_STANDARD_INTERFACE:
1812 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1813 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1814 umidi->usb_protocol_ops =
1815 &snd_usbmidi_maudio_broken_running_status_ops;
1817 case QUIRK_MIDI_US122L:
1818 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1820 case QUIRK_MIDI_FIXED_ENDPOINT:
1821 memcpy(&endpoints[0], quirk->data,
1822 sizeof(struct snd_usb_midi_endpoint_info));
1823 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1825 case QUIRK_MIDI_YAMAHA:
1826 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1828 case QUIRK_MIDI_MIDIMAN:
1829 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1830 memcpy(&endpoints[0], quirk->data,
1831 sizeof(struct snd_usb_midi_endpoint_info));
1834 case QUIRK_MIDI_NOVATION:
1835 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1836 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1838 case QUIRK_MIDI_FASTLANE:
1839 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1841 * Interface 1 contains isochronous endpoints, but with the same
1842 * numbers as in interface 0. Since it is interface 1 that the
1843 * USB core has most recently seen, these descriptors are now
1844 * associated with the endpoint numbers. This will foul up our
1845 * attempts to submit bulk/interrupt URBs to the endpoints in
1846 * interface 0, so we have to make sure that the USB core looks
1847 * again at interface 0 by calling usb_set_interface() on it.
1849 usb_set_interface(umidi->chip->dev, 0, 0);
1850 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1852 case QUIRK_MIDI_EMAGIC:
1853 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1854 memcpy(&endpoints[0], quirk->data,
1855 sizeof(struct snd_usb_midi_endpoint_info));
1856 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1858 case QUIRK_MIDI_CME:
1859 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
1860 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1863 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1872 /* create rawmidi device */
1875 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1876 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1877 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1879 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1885 /* create endpoint/port structures */
1886 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1887 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1889 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1891 snd_usbmidi_free(umidi);
1895 list_add(&umidi->list, &umidi->chip->midi_list);
1897 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1898 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1902 EXPORT_SYMBOL(snd_usb_create_midi_interface);
1903 EXPORT_SYMBOL(snd_usbmidi_input_stop);
1904 EXPORT_SYMBOL(snd_usbmidi_input_start);
1905 EXPORT_SYMBOL(snd_usbmidi_disconnect);