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 <sound/driver.h>
39 #include <linux/kernel.h>
40 #include <linux/types.h>
41 #include <linux/bitops.h>
42 #include <linux/interrupt.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/init.h>
46 #include <linux/slab.h>
47 #include <linux/timer.h>
48 #include <linux/usb.h>
49 #include <sound/core.h>
50 #include <sound/rawmidi.h>
51 #include <sound/asequencer.h>
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)
67 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
68 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
69 MODULE_LICENSE("Dual BSD/GPL");
72 struct usb_ms_header_descriptor {
75 __u8 bDescriptorSubtype;
78 } __attribute__ ((packed));
80 struct usb_ms_endpoint_descriptor {
83 __u8 bDescriptorSubtype;
85 __u8 baAssocJackID[0];
86 } __attribute__ ((packed));
88 struct snd_usb_midi_in_endpoint;
89 struct snd_usb_midi_out_endpoint;
90 struct snd_usb_midi_endpoint;
92 struct usb_protocol_ops {
93 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
94 void (*output)(struct snd_usb_midi_out_endpoint*);
95 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
96 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
97 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
100 struct snd_usb_midi {
101 struct snd_usb_audio *chip;
102 struct usb_interface *iface;
103 const struct snd_usb_audio_quirk *quirk;
104 struct snd_rawmidi *rmidi;
105 struct usb_protocol_ops* usb_protocol_ops;
106 struct list_head list;
107 struct timer_list error_timer;
109 struct snd_usb_midi_endpoint {
110 struct snd_usb_midi_out_endpoint *out;
111 struct snd_usb_midi_in_endpoint *in;
112 } endpoints[MIDI_MAX_ENDPOINTS];
113 unsigned long input_triggered;
116 struct snd_usb_midi_out_endpoint {
117 struct snd_usb_midi* umidi;
120 int max_transfer; /* size of urb buffer */
121 struct tasklet_struct tasklet;
123 spinlock_t buffer_lock;
125 struct usbmidi_out_port {
126 struct snd_usb_midi_out_endpoint* ep;
127 struct snd_rawmidi_substream *substream;
129 uint8_t cable; /* cable number << 4 */
131 #define STATE_UNKNOWN 0
132 #define STATE_1PARAM 1
133 #define STATE_2PARAM_1 2
134 #define STATE_2PARAM_2 3
135 #define STATE_SYSEX_0 4
136 #define STATE_SYSEX_1 5
137 #define STATE_SYSEX_2 6
143 struct snd_usb_midi_in_endpoint {
144 struct snd_usb_midi* umidi;
146 struct usbmidi_in_port {
147 struct snd_rawmidi_substream *substream;
148 u8 running_status_length;
155 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
157 static const uint8_t snd_usbmidi_cin_length[] = {
158 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
162 * Submits the URB, with error handling.
164 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
166 int err = usb_submit_urb(urb, flags);
167 if (err < 0 && err != -ENODEV)
168 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
173 * Error handling for URB completion functions.
175 static int snd_usbmidi_urb_error(int status)
178 /* manually unlinked, or device gone */
184 /* errors that might occur during unplugging */
190 snd_printk(KERN_ERR "urb status %d\n", status);
191 return 0; /* continue */
196 * Receives a chunk of MIDI data.
198 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
199 uint8_t* data, int length)
201 struct usbmidi_in_port* port = &ep->ports[portidx];
203 if (!port->substream) {
204 snd_printd("unexpected port %d!\n", portidx);
207 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
209 snd_rawmidi_receive(port->substream, data, length);
213 static void dump_urb(const char *type, const u8 *data, int length)
215 snd_printk(KERN_DEBUG "%s packet: [", type);
216 for (; length > 0; ++data, --length)
217 printk(" %02x", *data);
221 #define dump_urb(type, data, length) /* nothing */
225 * Processes the data read from the device.
227 static void snd_usbmidi_in_urb_complete(struct urb* urb)
229 struct snd_usb_midi_in_endpoint* ep = urb->context;
231 if (urb->status == 0) {
232 dump_urb("received", urb->transfer_buffer, urb->actual_length);
233 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
236 int err = snd_usbmidi_urb_error(urb->status);
238 if (err != -ENODEV) {
239 ep->error_resubmit = 1;
240 mod_timer(&ep->umidi->error_timer,
241 jiffies + ERROR_DELAY_JIFFIES);
247 urb->dev = ep->umidi->chip->dev;
248 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
251 static void snd_usbmidi_out_urb_complete(struct urb* urb)
253 struct snd_usb_midi_out_endpoint* ep = urb->context;
255 spin_lock(&ep->buffer_lock);
257 spin_unlock(&ep->buffer_lock);
258 if (urb->status < 0) {
259 int err = snd_usbmidi_urb_error(urb->status);
262 mod_timer(&ep->umidi->error_timer,
263 jiffies + ERROR_DELAY_JIFFIES);
267 snd_usbmidi_do_output(ep);
271 * This is called when some data should be transferred to the device
272 * (from one or more substreams).
274 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
276 struct urb* urb = ep->urb;
279 spin_lock_irqsave(&ep->buffer_lock, flags);
280 if (ep->urb_active || ep->umidi->chip->shutdown) {
281 spin_unlock_irqrestore(&ep->buffer_lock, flags);
285 urb->transfer_buffer_length = 0;
286 ep->umidi->usb_protocol_ops->output(ep);
288 if (urb->transfer_buffer_length > 0) {
289 dump_urb("sending", urb->transfer_buffer,
290 urb->transfer_buffer_length);
291 urb->dev = ep->umidi->chip->dev;
292 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
294 spin_unlock_irqrestore(&ep->buffer_lock, flags);
297 static void snd_usbmidi_out_tasklet(unsigned long data)
299 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
301 snd_usbmidi_do_output(ep);
304 /* called after transfers had been interrupted due to some USB error */
305 static void snd_usbmidi_error_timer(unsigned long data)
307 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
310 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
311 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
312 if (in && in->error_resubmit) {
313 in->error_resubmit = 0;
314 in->urb->dev = umidi->chip->dev;
315 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
317 if (umidi->endpoints[i].out)
318 snd_usbmidi_do_output(umidi->endpoints[i].out);
322 /* helper function to send static data that may not DMA-able */
323 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
324 const void *data, int len)
327 void *buf = kmemdup(data, len, GFP_KERNEL);
330 dump_urb("sending", buf, len);
331 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len,
338 * Standard USB MIDI protocol: see the spec.
339 * Midiman protocol: like the standard protocol, but the control byte is the
340 * fourth byte in each packet, and uses length instead of CIN.
343 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
344 uint8_t* buffer, int buffer_length)
348 for (i = 0; i + 3 < buffer_length; i += 4)
349 if (buffer[i] != 0) {
350 int cable = buffer[i] >> 4;
351 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
352 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
356 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
357 uint8_t* buffer, int buffer_length)
361 for (i = 0; i + 3 < buffer_length; i += 4)
362 if (buffer[i + 3] != 0) {
363 int port = buffer[i + 3] >> 4;
364 int length = buffer[i + 3] & 3;
365 snd_usbmidi_input_data(ep, port, &buffer[i], length);
370 * Buggy M-Audio device: running status on input results in a packet that has
371 * the data bytes but not the status byte and that is marked with CIN 4.
373 static void snd_usbmidi_maudio_broken_running_status_input(
374 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 u8 cin = buffer[i] & 0x0f;
383 struct usbmidi_in_port *port = &ep->ports[cable];
386 length = snd_usbmidi_cin_length[cin];
387 if (cin == 0xf && buffer[i + 1] >= 0xf8)
388 ; /* realtime msg: no running status change */
389 else if (cin >= 0x8 && cin <= 0xe)
391 port->running_status_length = length - 1;
392 else if (cin == 0x4 &&
393 port->running_status_length != 0 &&
394 buffer[i + 1] < 0x80)
395 /* CIN 4 that is not a SysEx */
396 length = port->running_status_length;
399 * All other msgs cannot begin running status.
400 * (A channel msg sent as two or three CIN 0xF
401 * packets could in theory, but this device
402 * doesn't use this format.)
404 port->running_status_length = 0;
405 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
410 * CME protocol: like the standard protocol, but SysEx commands are sent as a
411 * single USB packet preceded by a 0x0F byte.
413 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
414 uint8_t *buffer, int buffer_length)
416 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
417 snd_usbmidi_standard_input(ep, buffer, buffer_length);
419 snd_usbmidi_input_data(ep, buffer[0] >> 4,
420 &buffer[1], buffer_length - 1);
424 * Adds one USB MIDI packet to the output buffer.
426 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
427 uint8_t p1, uint8_t p2, uint8_t p3)
430 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
435 urb->transfer_buffer_length += 4;
439 * Adds one Midiman packet to the output buffer.
441 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
442 uint8_t p1, uint8_t p2, uint8_t p3)
445 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
449 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
450 urb->transfer_buffer_length += 4;
454 * Converts MIDI commands to USB MIDI packets.
456 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
457 uint8_t b, struct urb* urb)
459 uint8_t p0 = port->cable;
460 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
461 port->ep->umidi->usb_protocol_ops->output_packet;
464 output_packet(urb, p0 | 0x0f, b, 0, 0);
465 } else if (b >= 0xf0) {
469 port->state = STATE_SYSEX_1;
474 port->state = STATE_1PARAM;
478 port->state = STATE_2PARAM_1;
482 port->state = STATE_UNKNOWN;
485 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
486 port->state = STATE_UNKNOWN;
489 switch (port->state) {
491 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
494 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
497 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
500 port->state = STATE_UNKNOWN;
503 } else if (b >= 0x80) {
505 if (b >= 0xc0 && b <= 0xdf)
506 port->state = STATE_1PARAM;
508 port->state = STATE_2PARAM_1;
509 } else { /* b < 0x80 */
510 switch (port->state) {
512 if (port->data[0] < 0xf0) {
513 p0 |= port->data[0] >> 4;
516 port->state = STATE_UNKNOWN;
518 output_packet(urb, p0, port->data[0], b, 0);
522 port->state = STATE_2PARAM_2;
525 if (port->data[0] < 0xf0) {
526 p0 |= port->data[0] >> 4;
527 port->state = STATE_2PARAM_1;
530 port->state = STATE_UNKNOWN;
532 output_packet(urb, p0, port->data[0], port->data[1], b);
536 port->state = STATE_SYSEX_1;
540 port->state = STATE_SYSEX_2;
543 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
544 port->state = STATE_SYSEX_0;
550 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
552 struct urb* urb = ep->urb;
555 /* FIXME: lower-numbered ports can starve higher-numbered ports */
556 for (p = 0; p < 0x10; ++p) {
557 struct usbmidi_out_port* port = &ep->ports[p];
560 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
562 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
566 snd_usbmidi_transmit_byte(port, b, urb);
571 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
572 .input = snd_usbmidi_standard_input,
573 .output = snd_usbmidi_standard_output,
574 .output_packet = snd_usbmidi_output_standard_packet,
577 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
578 .input = snd_usbmidi_midiman_input,
579 .output = snd_usbmidi_standard_output,
580 .output_packet = snd_usbmidi_output_midiman_packet,
583 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
584 .input = snd_usbmidi_maudio_broken_running_status_input,
585 .output = snd_usbmidi_standard_output,
586 .output_packet = snd_usbmidi_output_standard_packet,
589 static struct usb_protocol_ops snd_usbmidi_cme_ops = {
590 .input = snd_usbmidi_cme_input,
591 .output = snd_usbmidi_standard_output,
592 .output_packet = snd_usbmidi_output_standard_packet,
596 * Novation USB MIDI protocol: number of data bytes is in the first byte
597 * (when receiving) (+1!) or in the second byte (when sending); data begins
601 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
602 uint8_t* buffer, int buffer_length)
604 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
606 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
609 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
611 uint8_t* transfer_buffer;
614 if (!ep->ports[0].active)
616 transfer_buffer = ep->urb->transfer_buffer;
617 count = snd_rawmidi_transmit(ep->ports[0].substream,
619 ep->max_transfer - 2);
621 ep->ports[0].active = 0;
624 transfer_buffer[0] = 0;
625 transfer_buffer[1] = count;
626 ep->urb->transfer_buffer_length = 2 + count;
629 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
630 .input = snd_usbmidi_novation_input,
631 .output = snd_usbmidi_novation_output,
635 * "raw" protocol: used by the MOTU FastLane.
638 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
639 uint8_t* buffer, int buffer_length)
641 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
644 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
648 if (!ep->ports[0].active)
650 count = snd_rawmidi_transmit(ep->ports[0].substream,
651 ep->urb->transfer_buffer,
654 ep->ports[0].active = 0;
657 ep->urb->transfer_buffer_length = count;
660 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
661 .input = snd_usbmidi_raw_input,
662 .output = snd_usbmidi_raw_output,
666 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
669 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
671 static const u8 init_data[] = {
672 /* initialization magic: "get version" */
674 0x00, 0x20, 0x31, /* Emagic */
676 0x0b, /* version number request */
677 0x00, /* command version */
678 0x00, /* EEPROM, box 0 */
681 send_bulk_static_data(ep, init_data, sizeof(init_data));
682 /* while we're at it, pour on more magic */
683 send_bulk_static_data(ep, init_data, sizeof(init_data));
686 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
688 static const u8 finish_data[] = {
689 /* switch to patch mode with last preset */
691 0x00, 0x20, 0x31, /* Emagic */
693 0x10, /* patch switch command */
694 0x00, /* command version */
695 0x7f, /* to all boxes */
696 0x40, /* last preset in EEPROM */
699 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
702 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
703 uint8_t* buffer, int buffer_length)
707 /* FF indicates end of valid data */
708 for (i = 0; i < buffer_length; ++i)
709 if (buffer[i] == 0xff) {
714 /* handle F5 at end of last buffer */
718 while (buffer_length > 0) {
719 /* determine size of data until next F5 */
720 for (i = 0; i < buffer_length; ++i)
721 if (buffer[i] == 0xf5)
723 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
727 if (buffer_length <= 0)
729 /* assert(buffer[0] == 0xf5); */
735 if (buffer_length <= 0)
737 if (buffer[0] < 0x80) {
738 ep->current_port = (buffer[0] - 1) & 15;
746 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
748 int port0 = ep->current_port;
749 uint8_t* buf = ep->urb->transfer_buffer;
750 int buf_free = ep->max_transfer;
753 for (i = 0; i < 0x10; ++i) {
754 /* round-robin, starting at the last current port */
755 int portnum = (port0 + i) & 15;
756 struct usbmidi_out_port* port = &ep->ports[portnum];
760 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
765 if (portnum != ep->current_port) {
768 ep->current_port = portnum;
770 buf[1] = (portnum + 1) & 15;
777 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
785 if (buf_free < ep->max_transfer && buf_free > 0) {
789 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
792 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
793 .input = snd_usbmidi_emagic_input,
794 .output = snd_usbmidi_emagic_output,
795 .init_out_endpoint = snd_usbmidi_emagic_init_out,
796 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
800 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
802 struct snd_usb_midi* umidi = substream->rmidi->private_data;
803 struct usbmidi_out_port* port = NULL;
806 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
807 if (umidi->endpoints[i].out)
808 for (j = 0; j < 0x10; ++j)
809 if (umidi->endpoints[i].out->ports[j].substream == substream) {
810 port = &umidi->endpoints[i].out->ports[j];
817 substream->runtime->private_data = port;
818 port->state = STATE_UNKNOWN;
822 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
827 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
829 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
833 if (port->ep->umidi->chip->shutdown) {
834 /* gobble up remaining bytes to prevent wait in
835 * snd_rawmidi_drain_output */
836 while (!snd_rawmidi_transmit_empty(substream))
837 snd_rawmidi_transmit_ack(substream, 1);
840 tasklet_hi_schedule(&port->ep->tasklet);
844 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
849 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
854 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
856 struct snd_usb_midi* umidi = substream->rmidi->private_data;
859 set_bit(substream->number, &umidi->input_triggered);
861 clear_bit(substream->number, &umidi->input_triggered);
864 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
865 .open = snd_usbmidi_output_open,
866 .close = snd_usbmidi_output_close,
867 .trigger = snd_usbmidi_output_trigger,
870 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
871 .open = snd_usbmidi_input_open,
872 .close = snd_usbmidi_input_close,
873 .trigger = snd_usbmidi_input_trigger
877 * Frees an input endpoint.
878 * May be called when ep hasn't been initialized completely.
880 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
883 usb_buffer_free(ep->umidi->chip->dev,
884 ep->urb->transfer_buffer_length,
885 ep->urb->transfer_buffer,
886 ep->urb->transfer_dma);
887 usb_free_urb(ep->urb);
893 * Creates an input endpoint.
895 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
896 struct snd_usb_midi_endpoint_info* ep_info,
897 struct snd_usb_midi_endpoint* rep)
899 struct snd_usb_midi_in_endpoint* ep;
905 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
910 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
912 snd_usbmidi_in_endpoint_delete(ep);
915 if (ep_info->in_interval)
916 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
918 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
919 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
920 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
921 &ep->urb->transfer_dma);
923 snd_usbmidi_in_endpoint_delete(ep);
926 if (ep_info->in_interval)
927 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
928 length, snd_usbmidi_in_urb_complete, ep,
929 ep_info->in_interval);
931 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
932 length, snd_usbmidi_in_urb_complete, ep);
933 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
939 static unsigned int snd_usbmidi_count_bits(unsigned int x)
943 for (bits = 0; x; ++bits)
949 * Frees an output endpoint.
950 * May be called when ep hasn't been initialized completely.
952 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
955 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
956 ep->urb->transfer_buffer,
957 ep->urb->transfer_dma);
958 usb_free_urb(ep->urb);
964 * Creates an output endpoint, and initializes output ports.
966 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
967 struct snd_usb_midi_endpoint_info* ep_info,
968 struct snd_usb_midi_endpoint* rep)
970 struct snd_usb_midi_out_endpoint* ep;
976 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
981 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
983 snd_usbmidi_out_endpoint_delete(ep);
986 if (ep_info->out_interval)
987 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
989 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
990 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
991 /* FIXME: we need more URBs to get reasonable bandwidth here: */
992 ep->max_transfer = 4;
994 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
995 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
996 GFP_KERNEL, &ep->urb->transfer_dma);
998 snd_usbmidi_out_endpoint_delete(ep);
1001 if (ep_info->out_interval)
1002 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
1003 ep->max_transfer, snd_usbmidi_out_urb_complete,
1004 ep, ep_info->out_interval);
1006 usb_fill_bulk_urb(ep->urb, umidi->chip->dev,
1007 pipe, buffer, ep->max_transfer,
1008 snd_usbmidi_out_urb_complete, ep);
1009 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1011 spin_lock_init(&ep->buffer_lock);
1012 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1014 for (i = 0; i < 0x10; ++i)
1015 if (ep_info->out_cables & (1 << i)) {
1016 ep->ports[i].ep = ep;
1017 ep->ports[i].cable = i << 4;
1020 if (umidi->usb_protocol_ops->init_out_endpoint)
1021 umidi->usb_protocol_ops->init_out_endpoint(ep);
1030 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1034 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1035 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1037 snd_usbmidi_out_endpoint_delete(ep->out);
1039 snd_usbmidi_in_endpoint_delete(ep->in);
1045 * Unlinks all URBs (must be done before the usb_device is deleted).
1047 void snd_usbmidi_disconnect(struct list_head* p)
1049 struct snd_usb_midi* umidi;
1052 umidi = list_entry(p, struct snd_usb_midi, list);
1053 del_timer_sync(&umidi->error_timer);
1054 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1055 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1057 tasklet_kill(&ep->out->tasklet);
1058 if (ep->out && ep->out->urb) {
1059 usb_kill_urb(ep->out->urb);
1060 if (umidi->usb_protocol_ops->finish_out_endpoint)
1061 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1064 usb_kill_urb(ep->in->urb);
1068 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1070 struct snd_usb_midi* umidi = rmidi->private_data;
1071 snd_usbmidi_free(umidi);
1074 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1075 int stream, int number)
1077 struct list_head* list;
1079 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1080 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1081 if (substream->number == number)
1088 * This list specifies names for ports that do not fit into the standard
1089 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1090 * such as internal control or synthesizer ports.
1092 static struct port_info {
1097 unsigned int seq_flags;
1098 } snd_usbmidi_port_info[] = {
1099 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1100 { .id = USB_ID(vendor, product), \
1101 .port = num, .voices = voices_, \
1102 .name = name_, .seq_flags = flags }
1103 #define EXTERNAL_PORT(vendor, product, num, name) \
1104 PORT_INFO(vendor, product, num, name, 0, \
1105 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1106 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1107 SNDRV_SEQ_PORT_TYPE_PORT)
1108 #define CONTROL_PORT(vendor, product, num, name) \
1109 PORT_INFO(vendor, product, num, name, 0, \
1110 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1111 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1112 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1113 PORT_INFO(vendor, product, num, name, voices, \
1114 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1115 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1116 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1117 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1118 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1119 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1120 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1121 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1122 PORT_INFO(vendor, product, num, name, voices, \
1123 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1124 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1125 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1126 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1127 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1128 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1129 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1130 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1132 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1133 /* Roland SC-8850 */
1134 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1135 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1136 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1137 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1138 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1139 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1141 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1142 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1143 /* Roland SC-8820 */
1144 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1145 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1146 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1148 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1149 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1150 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1152 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1153 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1154 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1156 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1158 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1159 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1160 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1161 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1163 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1165 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1166 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1167 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1169 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1170 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1171 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1172 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1174 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1175 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1177 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1178 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1179 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1181 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1182 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1183 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1185 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1186 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1187 /* Edirol UA-1000 */
1188 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1189 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1191 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1192 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1193 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1195 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1196 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1197 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1199 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1200 /* M-Audio MidiSport 8x8 */
1201 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1202 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1204 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1205 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1206 /* Emagic Unitor8/AMT8/MT4 */
1207 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1208 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1209 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1212 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1216 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1217 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1218 snd_usbmidi_port_info[i].port == number)
1219 return &snd_usbmidi_port_info[i];
1224 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1225 struct snd_seq_port_info *seq_port_info)
1227 struct snd_usb_midi *umidi = rmidi->private_data;
1228 struct port_info *port_info;
1230 /* TODO: read port flags from descriptors */
1231 port_info = find_port_info(umidi, number);
1233 seq_port_info->type = port_info->seq_flags;
1234 seq_port_info->midi_voices = port_info->voices;
1238 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1239 int stream, int number,
1240 struct snd_rawmidi_substream ** rsubstream)
1242 struct port_info *port_info;
1243 const char *name_format;
1245 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1247 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1251 /* TODO: read port name from jack descriptor */
1252 port_info = find_port_info(umidi, number);
1253 name_format = port_info ? port_info->name : "%s MIDI %d";
1254 snprintf(substream->name, sizeof(substream->name),
1255 name_format, umidi->chip->card->shortname, number + 1);
1257 *rsubstream = substream;
1261 * Creates the endpoints and their ports.
1263 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1264 struct snd_usb_midi_endpoint_info* endpoints)
1267 int out_ports = 0, in_ports = 0;
1269 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1270 if (endpoints[i].out_cables) {
1271 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1272 &umidi->endpoints[i]);
1276 if (endpoints[i].in_cables) {
1277 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1278 &umidi->endpoints[i]);
1283 for (j = 0; j < 0x10; ++j) {
1284 if (endpoints[i].out_cables & (1 << j)) {
1285 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1286 &umidi->endpoints[i].out->ports[j].substream);
1289 if (endpoints[i].in_cables & (1 << j)) {
1290 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1291 &umidi->endpoints[i].in->ports[j].substream);
1296 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1297 out_ports, in_ports);
1302 * Returns MIDIStreaming device capabilities.
1304 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1305 struct snd_usb_midi_endpoint_info* endpoints)
1307 struct usb_interface* intf;
1308 struct usb_host_interface *hostif;
1309 struct usb_interface_descriptor* intfd;
1310 struct usb_ms_header_descriptor* ms_header;
1311 struct usb_host_endpoint *hostep;
1312 struct usb_endpoint_descriptor* ep;
1313 struct usb_ms_endpoint_descriptor* ms_ep;
1316 intf = umidi->iface;
1319 hostif = &intf->altsetting[0];
1320 intfd = get_iface_desc(hostif);
1321 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1322 if (hostif->extralen >= 7 &&
1323 ms_header->bLength >= 7 &&
1324 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1325 ms_header->bDescriptorSubtype == HEADER)
1326 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1327 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1329 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1332 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1333 hostep = &hostif->endpoint[i];
1334 ep = get_ep_desc(hostep);
1335 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1336 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1338 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1339 if (hostep->extralen < 4 ||
1340 ms_ep->bLength < 4 ||
1341 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1342 ms_ep->bDescriptorSubtype != MS_GENERAL)
1344 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1345 if (endpoints[epidx].out_ep) {
1346 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1347 snd_printk(KERN_WARNING "too many endpoints\n");
1351 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1352 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1353 endpoints[epidx].out_interval = ep->bInterval;
1354 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1355 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1356 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1358 if (endpoints[epidx].in_ep) {
1359 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1360 snd_printk(KERN_WARNING "too many endpoints\n");
1364 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1365 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1366 endpoints[epidx].in_interval = ep->bInterval;
1367 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1368 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1369 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1376 * On Roland devices, use the second alternate setting to be able to use
1377 * the interrupt input endpoint.
1379 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1381 struct usb_interface* intf;
1382 struct usb_host_interface *hostif;
1383 struct usb_interface_descriptor* intfd;
1385 intf = umidi->iface;
1386 if (!intf || intf->num_altsetting != 2)
1389 hostif = &intf->altsetting[1];
1390 intfd = get_iface_desc(hostif);
1391 if (intfd->bNumEndpoints != 2 ||
1392 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1393 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1396 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1397 intfd->bAlternateSetting);
1398 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1399 intfd->bAlternateSetting);
1403 * Try to find any usable endpoints in the interface.
1405 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1406 struct snd_usb_midi_endpoint_info* endpoint,
1409 struct usb_interface* intf;
1410 struct usb_host_interface *hostif;
1411 struct usb_interface_descriptor* intfd;
1412 struct usb_endpoint_descriptor* epd;
1413 int i, out_eps = 0, in_eps = 0;
1415 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
1416 snd_usbmidi_switch_roland_altsetting(umidi);
1418 if (endpoint[0].out_ep || endpoint[0].in_ep)
1421 intf = umidi->iface;
1422 if (!intf || intf->num_altsetting < 1)
1424 hostif = intf->cur_altsetting;
1425 intfd = get_iface_desc(hostif);
1427 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1428 epd = get_endpoint(hostif, i);
1429 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1430 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1432 if (out_eps < max_endpoints &&
1433 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1434 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1435 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1436 endpoint[out_eps].out_interval = epd->bInterval;
1439 if (in_eps < max_endpoints &&
1440 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1441 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1442 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1443 endpoint[in_eps].in_interval = epd->bInterval;
1447 return (out_eps || in_eps) ? 0 : -ENOENT;
1451 * Detects the endpoints for one-port-per-endpoint protocols.
1453 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1454 struct snd_usb_midi_endpoint_info* endpoints)
1458 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1459 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1460 if (endpoints[i].out_ep)
1461 endpoints[i].out_cables = 0x0001;
1462 if (endpoints[i].in_ep)
1463 endpoints[i].in_cables = 0x0001;
1469 * Detects the endpoints and ports of Yamaha devices.
1471 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1472 struct snd_usb_midi_endpoint_info* endpoint)
1474 struct usb_interface* intf;
1475 struct usb_host_interface *hostif;
1476 struct usb_interface_descriptor* intfd;
1479 intf = umidi->iface;
1482 hostif = intf->altsetting;
1483 intfd = get_iface_desc(hostif);
1484 if (intfd->bNumEndpoints < 1)
1488 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1489 * necessarily with any useful contents. So simply count 'em.
1491 for (cs_desc = hostif->extra;
1492 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1493 cs_desc += cs_desc[0]) {
1494 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1495 if (cs_desc[2] == MIDI_IN_JACK)
1496 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1497 else if (cs_desc[2] == MIDI_OUT_JACK)
1498 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1501 if (!endpoint->in_cables && !endpoint->out_cables)
1504 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1508 * Creates the endpoints and their ports for Midiman devices.
1510 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1511 struct snd_usb_midi_endpoint_info* endpoint)
1513 struct snd_usb_midi_endpoint_info ep_info;
1514 struct usb_interface* intf;
1515 struct usb_host_interface *hostif;
1516 struct usb_interface_descriptor* intfd;
1517 struct usb_endpoint_descriptor* epd;
1520 intf = umidi->iface;
1523 hostif = intf->altsetting;
1524 intfd = get_iface_desc(hostif);
1526 * The various MidiSport devices have more or less random endpoint
1527 * numbers, so we have to identify the endpoints by their index in
1528 * the descriptor array, like the driver for that other OS does.
1530 * There is one interrupt input endpoint for all input ports, one
1531 * bulk output endpoint for even-numbered ports, and one for odd-
1532 * numbered ports. Both bulk output endpoints have corresponding
1533 * input bulk endpoints (at indices 1 and 3) which aren't used.
1535 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1536 snd_printdd(KERN_ERR "not enough endpoints\n");
1540 epd = get_endpoint(hostif, 0);
1541 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1542 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1543 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1546 epd = get_endpoint(hostif, 2);
1547 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1548 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1549 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1552 if (endpoint->out_cables > 0x0001) {
1553 epd = get_endpoint(hostif, 4);
1554 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1555 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1556 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1561 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1562 ep_info.out_cables = endpoint->out_cables & 0x5555;
1563 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1567 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1568 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1569 ep_info.in_cables = endpoint->in_cables;
1570 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1574 if (endpoint->out_cables > 0x0001) {
1575 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1576 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1577 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1582 for (cable = 0; cable < 0x10; ++cable) {
1583 if (endpoint->out_cables & (1 << cable))
1584 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1585 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1586 if (endpoint->in_cables & (1 << cable))
1587 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1588 &umidi->endpoints[0].in->ports[cable].substream);
1593 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1594 .get_port_info = snd_usbmidi_get_port_info,
1597 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
1598 int out_ports, int in_ports)
1600 struct snd_rawmidi *rmidi;
1603 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1604 umidi->chip->next_midi_device++,
1605 out_ports, in_ports, &rmidi);
1608 strcpy(rmidi->name, umidi->chip->card->shortname);
1609 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1610 SNDRV_RAWMIDI_INFO_INPUT |
1611 SNDRV_RAWMIDI_INFO_DUPLEX;
1612 rmidi->ops = &snd_usbmidi_ops;
1613 rmidi->private_data = umidi;
1614 rmidi->private_free = snd_usbmidi_rawmidi_free;
1615 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1616 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1618 umidi->rmidi = rmidi;
1623 * Temporarily stop input.
1625 void snd_usbmidi_input_stop(struct list_head* p)
1627 struct snd_usb_midi* umidi;
1630 umidi = list_entry(p, struct snd_usb_midi, list);
1631 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1632 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1634 usb_kill_urb(ep->in->urb);
1638 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
1641 struct urb* urb = ep->urb;
1642 urb->dev = ep->umidi->chip->dev;
1643 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1648 * Resume input after a call to snd_usbmidi_input_stop().
1650 void snd_usbmidi_input_start(struct list_head* p)
1652 struct snd_usb_midi* umidi;
1655 umidi = list_entry(p, struct snd_usb_midi, list);
1656 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1657 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1661 * Creates and registers everything needed for a MIDI streaming interface.
1663 int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
1664 struct usb_interface* iface,
1665 const struct snd_usb_audio_quirk* quirk)
1667 struct snd_usb_midi* umidi;
1668 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
1669 int out_ports, in_ports;
1672 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
1676 umidi->iface = iface;
1677 umidi->quirk = quirk;
1678 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
1679 init_timer(&umidi->error_timer);
1680 umidi->error_timer.function = snd_usbmidi_error_timer;
1681 umidi->error_timer.data = (unsigned long)umidi;
1683 /* detect the endpoint(s) to use */
1684 memset(endpoints, 0, sizeof(endpoints));
1685 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1686 case QUIRK_MIDI_STANDARD_INTERFACE:
1687 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1688 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1689 umidi->usb_protocol_ops =
1690 &snd_usbmidi_maudio_broken_running_status_ops;
1692 case QUIRK_MIDI_FIXED_ENDPOINT:
1693 memcpy(&endpoints[0], quirk->data,
1694 sizeof(struct snd_usb_midi_endpoint_info));
1695 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1697 case QUIRK_MIDI_YAMAHA:
1698 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1700 case QUIRK_MIDI_MIDIMAN:
1701 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1702 memcpy(&endpoints[0], quirk->data,
1703 sizeof(struct snd_usb_midi_endpoint_info));
1706 case QUIRK_MIDI_NOVATION:
1707 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1708 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1710 case QUIRK_MIDI_RAW:
1711 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1712 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1714 case QUIRK_MIDI_EMAGIC:
1715 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1716 memcpy(&endpoints[0], quirk->data,
1717 sizeof(struct snd_usb_midi_endpoint_info));
1718 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1720 case QUIRK_MIDI_CME:
1721 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
1722 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1725 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1734 /* create rawmidi device */
1737 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1738 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1739 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1741 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1747 /* create endpoint/port structures */
1748 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1749 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1751 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1753 snd_usbmidi_free(umidi);
1757 list_add(&umidi->list, &umidi->chip->midi_list);
1759 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1760 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1764 EXPORT_SYMBOL(snd_usb_create_midi_interface);
1765 EXPORT_SYMBOL(snd_usbmidi_input_stop);
1766 EXPORT_SYMBOL(snd_usbmidi_input_start);
1767 EXPORT_SYMBOL(snd_usbmidi_disconnect);