2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2005 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>
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)
66 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
67 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
68 MODULE_LICENSE("Dual BSD/GPL");
71 struct usb_ms_header_descriptor {
74 __u8 bDescriptorSubtype;
77 } __attribute__ ((packed));
79 struct usb_ms_endpoint_descriptor {
82 __u8 bDescriptorSubtype;
84 __u8 baAssocJackID[0];
85 } __attribute__ ((packed));
87 typedef struct snd_usb_midi snd_usb_midi_t;
88 typedef struct snd_usb_midi_endpoint snd_usb_midi_endpoint_t;
89 typedef struct snd_usb_midi_out_endpoint snd_usb_midi_out_endpoint_t;
90 typedef struct snd_usb_midi_in_endpoint snd_usb_midi_in_endpoint_t;
91 typedef struct usbmidi_out_port usbmidi_out_port_t;
92 typedef struct usbmidi_in_port usbmidi_in_port_t;
94 struct usb_protocol_ops {
95 void (*input)(snd_usb_midi_in_endpoint_t*, uint8_t*, int);
96 void (*output)(snd_usb_midi_out_endpoint_t*);
97 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
98 void (*init_out_endpoint)(snd_usb_midi_out_endpoint_t*);
99 void (*finish_out_endpoint)(snd_usb_midi_out_endpoint_t*);
102 struct snd_usb_midi {
103 snd_usb_audio_t *chip;
104 struct usb_interface *iface;
105 const snd_usb_audio_quirk_t *quirk;
106 snd_rawmidi_t* rmidi;
107 struct usb_protocol_ops* usb_protocol_ops;
108 struct list_head list;
109 struct timer_list error_timer;
111 struct snd_usb_midi_endpoint {
112 snd_usb_midi_out_endpoint_t *out;
113 snd_usb_midi_in_endpoint_t *in;
114 } endpoints[MIDI_MAX_ENDPOINTS];
115 unsigned long input_triggered;
118 struct snd_usb_midi_out_endpoint {
119 snd_usb_midi_t* umidi;
122 int max_transfer; /* size of urb buffer */
123 struct tasklet_struct tasklet;
125 spinlock_t buffer_lock;
127 struct usbmidi_out_port {
128 snd_usb_midi_out_endpoint_t* ep;
129 snd_rawmidi_substream_t* substream;
131 uint8_t cable; /* cable number << 4 */
133 #define STATE_UNKNOWN 0
134 #define STATE_1PARAM 1
135 #define STATE_2PARAM_1 2
136 #define STATE_2PARAM_2 3
137 #define STATE_SYSEX_0 4
138 #define STATE_SYSEX_1 5
139 #define STATE_SYSEX_2 6
145 struct snd_usb_midi_in_endpoint {
146 snd_usb_midi_t* umidi;
148 struct usbmidi_in_port {
149 snd_rawmidi_substream_t* substream;
156 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep);
158 static const uint8_t snd_usbmidi_cin_length[] = {
159 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
163 * Submits the URB, with error handling.
165 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
167 int err = usb_submit_urb(urb, flags);
168 if (err < 0 && err != -ENODEV)
169 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
174 * Error handling for URB completion functions.
176 static int snd_usbmidi_urb_error(int status)
179 /* manually unlinked, or device gone */
185 /* errors that might occur during unplugging */
186 case -EPROTO: /* EHCI */
187 case -ETIMEDOUT: /* OHCI */
188 case -EILSEQ: /* UHCI */
191 snd_printk(KERN_ERR "urb status %d\n", status);
192 return 0; /* continue */
197 * Receives a chunk of MIDI data.
199 static void snd_usbmidi_input_data(snd_usb_midi_in_endpoint_t* ep, int portidx,
200 uint8_t* data, int length)
202 usbmidi_in_port_t* port = &ep->ports[portidx];
204 if (!port->substream) {
205 snd_printd("unexpected port %d!\n", portidx);
208 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
210 snd_rawmidi_receive(port->substream, data, length);
214 static void dump_urb(const char *type, const u8 *data, int length)
216 snd_printk(KERN_DEBUG "%s packet: [", type);
217 for (; length > 0; ++data, --length)
218 printk(" %02x", *data);
222 #define dump_urb(type, data, length) /* nothing */
226 * Processes the data read from the device.
228 static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs)
230 snd_usb_midi_in_endpoint_t* ep = urb->context;
232 if (urb->status == 0) {
233 dump_urb("received", urb->transfer_buffer, urb->actual_length);
234 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
237 int err = snd_usbmidi_urb_error(urb->status);
239 if (err != -ENODEV) {
240 ep->error_resubmit = 1;
241 mod_timer(&ep->umidi->error_timer,
242 jiffies + ERROR_DELAY_JIFFIES);
248 urb->dev = ep->umidi->chip->dev;
249 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
252 static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs)
254 snd_usb_midi_out_endpoint_t* ep = urb->context;
256 spin_lock(&ep->buffer_lock);
258 spin_unlock(&ep->buffer_lock);
259 if (urb->status < 0) {
260 int err = snd_usbmidi_urb_error(urb->status);
263 mod_timer(&ep->umidi->error_timer,
264 jiffies + ERROR_DELAY_JIFFIES);
268 snd_usbmidi_do_output(ep);
272 * This is called when some data should be transferred to the device
273 * (from one or more substreams).
275 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep)
277 struct urb* urb = ep->urb;
280 spin_lock_irqsave(&ep->buffer_lock, flags);
281 if (ep->urb_active || ep->umidi->chip->shutdown) {
282 spin_unlock_irqrestore(&ep->buffer_lock, flags);
286 urb->transfer_buffer_length = 0;
287 ep->umidi->usb_protocol_ops->output(ep);
289 if (urb->transfer_buffer_length > 0) {
290 dump_urb("sending", urb->transfer_buffer,
291 urb->transfer_buffer_length);
292 urb->dev = ep->umidi->chip->dev;
293 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
295 spin_unlock_irqrestore(&ep->buffer_lock, flags);
298 static void snd_usbmidi_out_tasklet(unsigned long data)
300 snd_usb_midi_out_endpoint_t* ep = (snd_usb_midi_out_endpoint_t *) data;
302 snd_usbmidi_do_output(ep);
305 /* called after transfers had been interrupted due to some USB error */
306 static void snd_usbmidi_error_timer(unsigned long data)
308 snd_usb_midi_t *umidi = (snd_usb_midi_t *)data;
311 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
312 snd_usb_midi_in_endpoint_t *in = umidi->endpoints[i].in;
313 if (in && in->error_resubmit) {
314 in->error_resubmit = 0;
315 in->urb->dev = umidi->chip->dev;
316 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
318 if (umidi->endpoints[i].out)
319 snd_usbmidi_do_output(umidi->endpoints[i].out);
323 /* helper function to send static data that may not DMA-able */
324 static int send_bulk_static_data(snd_usb_midi_out_endpoint_t* ep,
325 const void *data, int len)
328 void *buf = kmalloc(len, GFP_KERNEL);
331 memcpy(buf, data, len);
332 dump_urb("sending", buf, len);
333 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len,
340 * Standard USB MIDI protocol: see the spec.
341 * Midiman protocol: like the standard protocol, but the control byte is the
342 * fourth byte in each packet, and uses length instead of CIN.
345 static void snd_usbmidi_standard_input(snd_usb_midi_in_endpoint_t* ep,
346 uint8_t* buffer, int buffer_length)
350 for (i = 0; i + 3 < buffer_length; i += 4)
351 if (buffer[i] != 0) {
352 int cable = buffer[i] >> 4;
353 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
354 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
358 static void snd_usbmidi_midiman_input(snd_usb_midi_in_endpoint_t* ep,
359 uint8_t* buffer, int buffer_length)
363 for (i = 0; i + 3 < buffer_length; i += 4)
364 if (buffer[i + 3] != 0) {
365 int port = buffer[i + 3] >> 4;
366 int length = buffer[i + 3] & 3;
367 snd_usbmidi_input_data(ep, port, &buffer[i], length);
372 * Adds one USB MIDI packet to the output buffer.
374 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
375 uint8_t p1, uint8_t p2, uint8_t p3)
378 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
383 urb->transfer_buffer_length += 4;
387 * Adds one Midiman packet to the output buffer.
389 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
390 uint8_t p1, uint8_t p2, uint8_t p3)
393 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
397 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
398 urb->transfer_buffer_length += 4;
402 * Converts MIDI commands to USB MIDI packets.
404 static void snd_usbmidi_transmit_byte(usbmidi_out_port_t* port,
405 uint8_t b, struct urb* urb)
407 uint8_t p0 = port->cable;
408 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
409 port->ep->umidi->usb_protocol_ops->output_packet;
412 output_packet(urb, p0 | 0x0f, b, 0, 0);
413 } else if (b >= 0xf0) {
417 port->state = STATE_SYSEX_1;
422 port->state = STATE_1PARAM;
426 port->state = STATE_2PARAM_1;
430 port->state = STATE_UNKNOWN;
433 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
434 port->state = STATE_UNKNOWN;
437 switch (port->state) {
439 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
442 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
445 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
448 port->state = STATE_UNKNOWN;
451 } else if (b >= 0x80) {
453 if (b >= 0xc0 && b <= 0xdf)
454 port->state = STATE_1PARAM;
456 port->state = STATE_2PARAM_1;
457 } else { /* b < 0x80 */
458 switch (port->state) {
460 if (port->data[0] < 0xf0) {
461 p0 |= port->data[0] >> 4;
464 port->state = STATE_UNKNOWN;
466 output_packet(urb, p0, port->data[0], b, 0);
470 port->state = STATE_2PARAM_2;
473 if (port->data[0] < 0xf0) {
474 p0 |= port->data[0] >> 4;
475 port->state = STATE_2PARAM_1;
478 port->state = STATE_UNKNOWN;
480 output_packet(urb, p0, port->data[0], port->data[1], b);
484 port->state = STATE_SYSEX_1;
488 port->state = STATE_SYSEX_2;
491 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
492 port->state = STATE_SYSEX_0;
498 static void snd_usbmidi_standard_output(snd_usb_midi_out_endpoint_t* ep)
500 struct urb* urb = ep->urb;
503 /* FIXME: lower-numbered ports can starve higher-numbered ports */
504 for (p = 0; p < 0x10; ++p) {
505 usbmidi_out_port_t* port = &ep->ports[p];
508 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
510 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
514 snd_usbmidi_transmit_byte(port, b, urb);
519 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
520 .input = snd_usbmidi_standard_input,
521 .output = snd_usbmidi_standard_output,
522 .output_packet = snd_usbmidi_output_standard_packet,
525 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
526 .input = snd_usbmidi_midiman_input,
527 .output = snd_usbmidi_standard_output,
528 .output_packet = snd_usbmidi_output_midiman_packet,
532 * Novation USB MIDI protocol: number of data bytes is in the first byte
533 * (when receiving) (+1!) or in the second byte (when sending); data begins
537 static void snd_usbmidi_novation_input(snd_usb_midi_in_endpoint_t* ep,
538 uint8_t* buffer, int buffer_length)
540 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
542 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
545 static void snd_usbmidi_novation_output(snd_usb_midi_out_endpoint_t* ep)
547 uint8_t* transfer_buffer;
550 if (!ep->ports[0].active)
552 transfer_buffer = ep->urb->transfer_buffer;
553 count = snd_rawmidi_transmit(ep->ports[0].substream,
555 ep->max_transfer - 2);
557 ep->ports[0].active = 0;
560 transfer_buffer[0] = 0;
561 transfer_buffer[1] = count;
562 ep->urb->transfer_buffer_length = 2 + count;
565 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
566 .input = snd_usbmidi_novation_input,
567 .output = snd_usbmidi_novation_output,
571 * "raw" protocol: used by the MOTU FastLane.
574 static void snd_usbmidi_raw_input(snd_usb_midi_in_endpoint_t* ep,
575 uint8_t* buffer, int buffer_length)
577 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
580 static void snd_usbmidi_raw_output(snd_usb_midi_out_endpoint_t* ep)
584 if (!ep->ports[0].active)
586 count = snd_rawmidi_transmit(ep->ports[0].substream,
587 ep->urb->transfer_buffer,
590 ep->ports[0].active = 0;
593 ep->urb->transfer_buffer_length = count;
596 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
597 .input = snd_usbmidi_raw_input,
598 .output = snd_usbmidi_raw_output,
602 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
605 static void snd_usbmidi_emagic_init_out(snd_usb_midi_out_endpoint_t* ep)
607 static const u8 init_data[] = {
608 /* initialization magic: "get version" */
610 0x00, 0x20, 0x31, /* Emagic */
612 0x0b, /* version number request */
613 0x00, /* command version */
614 0x00, /* EEPROM, box 0 */
617 send_bulk_static_data(ep, init_data, sizeof(init_data));
618 /* while we're at it, pour on more magic */
619 send_bulk_static_data(ep, init_data, sizeof(init_data));
622 static void snd_usbmidi_emagic_finish_out(snd_usb_midi_out_endpoint_t* ep)
624 static const u8 finish_data[] = {
625 /* switch to patch mode with last preset */
627 0x00, 0x20, 0x31, /* Emagic */
629 0x10, /* patch switch command */
630 0x00, /* command version */
631 0x7f, /* to all boxes */
632 0x40, /* last preset in EEPROM */
635 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
638 static void snd_usbmidi_emagic_input(snd_usb_midi_in_endpoint_t* ep,
639 uint8_t* buffer, int buffer_length)
643 /* FF indicates end of valid data */
644 for (i = 0; i < buffer_length; ++i)
645 if (buffer[i] == 0xff) {
650 /* handle F5 at end of last buffer */
654 while (buffer_length > 0) {
655 /* determine size of data until next F5 */
656 for (i = 0; i < buffer_length; ++i)
657 if (buffer[i] == 0xf5)
659 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
663 if (buffer_length <= 0)
665 /* assert(buffer[0] == 0xf5); */
671 if (buffer_length <= 0)
673 if (buffer[0] < 0x80) {
674 ep->current_port = (buffer[0] - 1) & 15;
682 static void snd_usbmidi_emagic_output(snd_usb_midi_out_endpoint_t* ep)
684 int port0 = ep->current_port;
685 uint8_t* buf = ep->urb->transfer_buffer;
686 int buf_free = ep->max_transfer;
689 for (i = 0; i < 0x10; ++i) {
690 /* round-robin, starting at the last current port */
691 int portnum = (port0 + i) & 15;
692 usbmidi_out_port_t* port = &ep->ports[portnum];
696 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
701 if (portnum != ep->current_port) {
704 ep->current_port = portnum;
706 buf[1] = (portnum + 1) & 15;
713 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
721 if (buf_free < ep->max_transfer && buf_free > 0) {
725 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
728 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
729 .input = snd_usbmidi_emagic_input,
730 .output = snd_usbmidi_emagic_output,
731 .init_out_endpoint = snd_usbmidi_emagic_init_out,
732 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
736 static int snd_usbmidi_output_open(snd_rawmidi_substream_t* substream)
738 snd_usb_midi_t* umidi = substream->rmidi->private_data;
739 usbmidi_out_port_t* port = NULL;
742 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
743 if (umidi->endpoints[i].out)
744 for (j = 0; j < 0x10; ++j)
745 if (umidi->endpoints[i].out->ports[j].substream == substream) {
746 port = &umidi->endpoints[i].out->ports[j];
753 substream->runtime->private_data = port;
754 port->state = STATE_UNKNOWN;
758 static int snd_usbmidi_output_close(snd_rawmidi_substream_t* substream)
763 static void snd_usbmidi_output_trigger(snd_rawmidi_substream_t* substream, int up)
765 usbmidi_out_port_t* port = (usbmidi_out_port_t*)substream->runtime->private_data;
769 if (port->ep->umidi->chip->shutdown) {
770 /* gobble up remaining bytes to prevent wait in
771 * snd_rawmidi_drain_output */
772 while (!snd_rawmidi_transmit_empty(substream))
773 snd_rawmidi_transmit_ack(substream, 1);
776 tasklet_hi_schedule(&port->ep->tasklet);
780 static int snd_usbmidi_input_open(snd_rawmidi_substream_t* substream)
785 static int snd_usbmidi_input_close(snd_rawmidi_substream_t* substream)
790 static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t* substream, int up)
792 snd_usb_midi_t* umidi = substream->rmidi->private_data;
795 set_bit(substream->number, &umidi->input_triggered);
797 clear_bit(substream->number, &umidi->input_triggered);
800 static snd_rawmidi_ops_t snd_usbmidi_output_ops = {
801 .open = snd_usbmidi_output_open,
802 .close = snd_usbmidi_output_close,
803 .trigger = snd_usbmidi_output_trigger,
806 static snd_rawmidi_ops_t snd_usbmidi_input_ops = {
807 .open = snd_usbmidi_input_open,
808 .close = snd_usbmidi_input_close,
809 .trigger = snd_usbmidi_input_trigger
813 * Frees an input endpoint.
814 * May be called when ep hasn't been initialized completely.
816 static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t* ep)
819 usb_buffer_free(ep->umidi->chip->dev,
820 ep->urb->transfer_buffer_length,
821 ep->urb->transfer_buffer,
822 ep->urb->transfer_dma);
823 usb_free_urb(ep->urb);
829 * Creates an input endpoint.
831 static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t* umidi,
832 snd_usb_midi_endpoint_info_t* ep_info,
833 snd_usb_midi_endpoint_t* rep)
835 snd_usb_midi_in_endpoint_t* ep;
841 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
846 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
848 snd_usbmidi_in_endpoint_delete(ep);
851 if (ep_info->in_interval)
852 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
854 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
855 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
856 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
857 &ep->urb->transfer_dma);
859 snd_usbmidi_in_endpoint_delete(ep);
862 if (ep_info->in_interval)
863 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
864 length, snd_usbmidi_in_urb_complete, ep,
865 ep_info->in_interval);
867 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
868 length, snd_usbmidi_in_urb_complete, ep);
869 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
875 static unsigned int snd_usbmidi_count_bits(unsigned int x)
877 unsigned int bits = 0;
885 * Frees an output endpoint.
886 * May be called when ep hasn't been initialized completely.
888 static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t* ep)
891 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
892 ep->urb->transfer_buffer,
893 ep->urb->transfer_dma);
894 usb_free_urb(ep->urb);
900 * Creates an output endpoint, and initializes output ports.
902 static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t* umidi,
903 snd_usb_midi_endpoint_info_t* ep_info,
904 snd_usb_midi_endpoint_t* rep)
906 snd_usb_midi_out_endpoint_t* ep;
912 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
917 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
919 snd_usbmidi_out_endpoint_delete(ep);
922 /* we never use interrupt output pipes */
923 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
924 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
925 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
926 GFP_KERNEL, &ep->urb->transfer_dma);
928 snd_usbmidi_out_endpoint_delete(ep);
931 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
932 ep->max_transfer, snd_usbmidi_out_urb_complete, ep);
933 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
935 spin_lock_init(&ep->buffer_lock);
936 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
938 for (i = 0; i < 0x10; ++i)
939 if (ep_info->out_cables & (1 << i)) {
940 ep->ports[i].ep = ep;
941 ep->ports[i].cable = i << 4;
944 if (umidi->usb_protocol_ops->init_out_endpoint)
945 umidi->usb_protocol_ops->init_out_endpoint(ep);
954 static void snd_usbmidi_free(snd_usb_midi_t* umidi)
958 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
959 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
961 snd_usbmidi_out_endpoint_delete(ep->out);
963 snd_usbmidi_in_endpoint_delete(ep->in);
969 * Unlinks all URBs (must be done before the usb_device is deleted).
971 void snd_usbmidi_disconnect(struct list_head* p)
973 snd_usb_midi_t* umidi;
976 umidi = list_entry(p, snd_usb_midi_t, list);
977 del_timer_sync(&umidi->error_timer);
978 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
979 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
981 tasklet_kill(&ep->out->tasklet);
982 if (ep->out && ep->out->urb) {
983 usb_kill_urb(ep->out->urb);
984 if (umidi->usb_protocol_ops->finish_out_endpoint)
985 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
987 if (ep->in && ep->in->urb)
988 usb_kill_urb(ep->in->urb);
992 static void snd_usbmidi_rawmidi_free(snd_rawmidi_t* rmidi)
994 snd_usb_midi_t* umidi = rmidi->private_data;
995 snd_usbmidi_free(umidi);
998 static snd_rawmidi_substream_t* snd_usbmidi_find_substream(snd_usb_midi_t* umidi,
999 int stream, int number)
1001 struct list_head* list;
1003 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1004 snd_rawmidi_substream_t* substream = list_entry(list, snd_rawmidi_substream_t, list);
1005 if (substream->number == number)
1012 * This list specifies names for ports that do not fit into the standard
1013 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1014 * such as internal control or synthesizer ports.
1019 const char *name_format;
1020 } snd_usbmidi_port_names[] = {
1022 { USB_ID(0x0582, 0x0000), 2, "%s Control" },
1023 /* Roland SC-8850 */
1024 { USB_ID(0x0582, 0x0003), 0, "%s Part A" },
1025 { USB_ID(0x0582, 0x0003), 1, "%s Part B" },
1026 { USB_ID(0x0582, 0x0003), 2, "%s Part C" },
1027 { USB_ID(0x0582, 0x0003), 3, "%s Part D" },
1028 { USB_ID(0x0582, 0x0003), 4, "%s MIDI 1" },
1029 { USB_ID(0x0582, 0x0003), 5, "%s MIDI 2" },
1031 { USB_ID(0x0582, 0x0004), 0, "%s MIDI" },
1032 { USB_ID(0x0582, 0x0004), 1, "%s Control" },
1033 /* Roland SC-8820 */
1034 { USB_ID(0x0582, 0x0007), 0, "%s Part A" },
1035 { USB_ID(0x0582, 0x0007), 1, "%s Part B" },
1036 { USB_ID(0x0582, 0x0007), 2, "%s MIDI" },
1038 { USB_ID(0x0582, 0x000b), 0, "%s Part A" },
1039 { USB_ID(0x0582, 0x000b), 1, "%s Part B" },
1040 { USB_ID(0x0582, 0x000b), 2, "%s MIDI" },
1042 { USB_ID(0x0582, 0x000c), 0, "%s Part A" },
1043 { USB_ID(0x0582, 0x000c), 1, "%s Part B" },
1044 { USB_ID(0x0582, 0x000c), 2, "%s MIDI" },
1046 { USB_ID(0x0582, 0x0014), 8, "%s Control" },
1048 { USB_ID(0x0582, 0x0016), 0, "%s Part A" },
1049 { USB_ID(0x0582, 0x0016), 1, "%s Part B" },
1050 { USB_ID(0x0582, 0x0016), 2, "%s MIDI 1" },
1051 { USB_ID(0x0582, 0x0016), 3, "%s MIDI 2" },
1053 { USB_ID(0x0582, 0x0023), 5, "%s Control" },
1055 { USB_ID(0x0582, 0x0027), 0, "%s Part A" },
1056 { USB_ID(0x0582, 0x0027), 1, "%s Part B" },
1057 { USB_ID(0x0582, 0x0027), 2, "%s MIDI" },
1059 { USB_ID(0x0582, 0x0029), 0, "%s Part A" },
1060 { USB_ID(0x0582, 0x0029), 1, "%s Part B" },
1061 { USB_ID(0x0582, 0x0029), 2, "%s MIDI 1" },
1062 { USB_ID(0x0582, 0x0029), 3, "%s MIDI 2" },
1064 { USB_ID(0x0582, 0x002b), 0, "%s MIDI" },
1065 { USB_ID(0x0582, 0x002b), 1, "%s Control" },
1067 { USB_ID(0x0582, 0x002f), 0, "%s MIDI" },
1068 { USB_ID(0x0582, 0x002f), 1, "%s External MIDI" },
1069 { USB_ID(0x0582, 0x002f), 2, "%s Sync" },
1071 { USB_ID(0x0582, 0x0033), 0, "%s MIDI" },
1072 { USB_ID(0x0582, 0x0033), 1, "%s 1" },
1073 { USB_ID(0x0582, 0x0033), 2, "%s 2" },
1075 { USB_ID(0x0582, 0x003b), 0, "%s MIDI" },
1076 { USB_ID(0x0582, 0x003b), 1, "%s Control" },
1077 /* Edirol UA-1000 */
1078 { USB_ID(0x0582, 0x0044), 0, "%s MIDI" },
1079 { USB_ID(0x0582, 0x0044), 1, "%s Control" },
1081 { USB_ID(0x0582, 0x0048), 0, "%s MIDI" },
1082 { USB_ID(0x0582, 0x0048), 1, "%s 1" },
1083 { USB_ID(0x0582, 0x0048), 2, "%s 2" },
1085 { USB_ID(0x0582, 0x004d), 0, "%s MIDI" },
1086 { USB_ID(0x0582, 0x004d), 1, "%s 1" },
1087 { USB_ID(0x0582, 0x004d), 2, "%s 2" },
1088 /* M-Audio MidiSport 8x8 */
1089 { USB_ID(0x0763, 0x1031), 8, "%s Control" },
1090 { USB_ID(0x0763, 0x1033), 8, "%s Control" },
1092 { USB_ID(0x07fd, 0x0001), 0, "%s MIDI A" },
1093 { USB_ID(0x07fd, 0x0001), 1, "%s MIDI B" },
1094 /* Emagic Unitor8/AMT8/MT4 */
1095 { USB_ID(0x086a, 0x0001), 8, "%s Broadcast" },
1096 { USB_ID(0x086a, 0x0002), 8, "%s Broadcast" },
1097 { USB_ID(0x086a, 0x0003), 4, "%s Broadcast" },
1100 static void snd_usbmidi_init_substream(snd_usb_midi_t* umidi,
1101 int stream, int number,
1102 snd_rawmidi_substream_t** rsubstream)
1105 const char *name_format;
1107 snd_rawmidi_substream_t* substream = snd_usbmidi_find_substream(umidi, stream, number);
1109 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1113 /* TODO: read port name from jack descriptor */
1114 name_format = "%s MIDI %d";
1115 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_names); ++i) {
1116 if (snd_usbmidi_port_names[i].id == umidi->chip->usb_id &&
1117 snd_usbmidi_port_names[i].port == number) {
1118 name_format = snd_usbmidi_port_names[i].name_format;
1122 snprintf(substream->name, sizeof(substream->name),
1123 name_format, umidi->chip->card->shortname, number + 1);
1125 *rsubstream = substream;
1129 * Creates the endpoints and their ports.
1131 static int snd_usbmidi_create_endpoints(snd_usb_midi_t* umidi,
1132 snd_usb_midi_endpoint_info_t* endpoints)
1135 int out_ports = 0, in_ports = 0;
1137 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1138 if (endpoints[i].out_cables) {
1139 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1140 &umidi->endpoints[i]);
1144 if (endpoints[i].in_cables) {
1145 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1146 &umidi->endpoints[i]);
1151 for (j = 0; j < 0x10; ++j) {
1152 if (endpoints[i].out_cables & (1 << j)) {
1153 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1154 &umidi->endpoints[i].out->ports[j].substream);
1157 if (endpoints[i].in_cables & (1 << j)) {
1158 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1159 &umidi->endpoints[i].in->ports[j].substream);
1164 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1165 out_ports, in_ports);
1170 * Returns MIDIStreaming device capabilities.
1172 static int snd_usbmidi_get_ms_info(snd_usb_midi_t* umidi,
1173 snd_usb_midi_endpoint_info_t* endpoints)
1175 struct usb_interface* intf;
1176 struct usb_host_interface *hostif;
1177 struct usb_interface_descriptor* intfd;
1178 struct usb_ms_header_descriptor* ms_header;
1179 struct usb_host_endpoint *hostep;
1180 struct usb_endpoint_descriptor* ep;
1181 struct usb_ms_endpoint_descriptor* ms_ep;
1184 intf = umidi->iface;
1187 hostif = &intf->altsetting[0];
1188 intfd = get_iface_desc(hostif);
1189 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1190 if (hostif->extralen >= 7 &&
1191 ms_header->bLength >= 7 &&
1192 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1193 ms_header->bDescriptorSubtype == HEADER)
1194 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1195 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1197 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1200 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1201 hostep = &hostif->endpoint[i];
1202 ep = get_ep_desc(hostep);
1203 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1204 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1206 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1207 if (hostep->extralen < 4 ||
1208 ms_ep->bLength < 4 ||
1209 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1210 ms_ep->bDescriptorSubtype != MS_GENERAL)
1212 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1213 if (endpoints[epidx].out_ep) {
1214 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1215 snd_printk(KERN_WARNING "too many endpoints\n");
1219 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1220 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1221 endpoints[epidx].out_interval = ep->bInterval;
1222 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1223 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1224 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1226 if (endpoints[epidx].in_ep) {
1227 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1228 snd_printk(KERN_WARNING "too many endpoints\n");
1232 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1233 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1234 endpoints[epidx].in_interval = ep->bInterval;
1235 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1236 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1237 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1244 * On Roland devices, use the second alternate setting to be able to use
1245 * the interrupt input endpoint.
1247 static void snd_usbmidi_switch_roland_altsetting(snd_usb_midi_t* umidi)
1249 struct usb_interface* intf;
1250 struct usb_host_interface *hostif;
1251 struct usb_interface_descriptor* intfd;
1253 intf = umidi->iface;
1254 if (!intf || intf->num_altsetting != 2)
1257 hostif = &intf->altsetting[1];
1258 intfd = get_iface_desc(hostif);
1259 if (intfd->bNumEndpoints != 2 ||
1260 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1261 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1264 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1265 intfd->bAlternateSetting);
1266 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1267 intfd->bAlternateSetting);
1271 * Try to find any usable endpoints in the interface.
1273 static int snd_usbmidi_detect_endpoints(snd_usb_midi_t* umidi,
1274 snd_usb_midi_endpoint_info_t* endpoint,
1277 struct usb_interface* intf;
1278 struct usb_host_interface *hostif;
1279 struct usb_interface_descriptor* intfd;
1280 struct usb_endpoint_descriptor* epd;
1281 int i, out_eps = 0, in_eps = 0;
1283 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
1284 snd_usbmidi_switch_roland_altsetting(umidi);
1286 if (endpoint[0].out_ep || endpoint[0].in_ep)
1289 intf = umidi->iface;
1290 if (!intf || intf->num_altsetting < 1)
1292 hostif = intf->cur_altsetting;
1293 intfd = get_iface_desc(hostif);
1295 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1296 epd = get_endpoint(hostif, i);
1297 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1298 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1300 if (out_eps < max_endpoints &&
1301 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1302 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1303 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1304 endpoint[out_eps].out_interval = epd->bInterval;
1307 if (in_eps < max_endpoints &&
1308 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1309 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1310 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1311 endpoint[in_eps].in_interval = epd->bInterval;
1315 return (out_eps || in_eps) ? 0 : -ENOENT;
1319 * Detects the endpoints for one-port-per-endpoint protocols.
1321 static int snd_usbmidi_detect_per_port_endpoints(snd_usb_midi_t* umidi,
1322 snd_usb_midi_endpoint_info_t* endpoints)
1326 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1327 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1328 if (endpoints[i].out_ep)
1329 endpoints[i].out_cables = 0x0001;
1330 if (endpoints[i].in_ep)
1331 endpoints[i].in_cables = 0x0001;
1337 * Detects the endpoints and ports of Yamaha devices.
1339 static int snd_usbmidi_detect_yamaha(snd_usb_midi_t* umidi,
1340 snd_usb_midi_endpoint_info_t* endpoint)
1342 struct usb_interface* intf;
1343 struct usb_host_interface *hostif;
1344 struct usb_interface_descriptor* intfd;
1347 intf = umidi->iface;
1350 hostif = intf->altsetting;
1351 intfd = get_iface_desc(hostif);
1352 if (intfd->bNumEndpoints < 1)
1356 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1357 * necessarily with any useful contents. So simply count 'em.
1359 for (cs_desc = hostif->extra;
1360 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1361 cs_desc += cs_desc[0]) {
1362 if (cs_desc[1] == CS_AUDIO_INTERFACE) {
1363 if (cs_desc[2] == MIDI_IN_JACK)
1364 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1365 else if (cs_desc[2] == MIDI_OUT_JACK)
1366 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1369 if (!endpoint->in_cables && !endpoint->out_cables)
1372 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1376 * Creates the endpoints and their ports for Midiman devices.
1378 static int snd_usbmidi_create_endpoints_midiman(snd_usb_midi_t* umidi,
1379 snd_usb_midi_endpoint_info_t* endpoint)
1381 snd_usb_midi_endpoint_info_t ep_info;
1382 struct usb_interface* intf;
1383 struct usb_host_interface *hostif;
1384 struct usb_interface_descriptor* intfd;
1385 struct usb_endpoint_descriptor* epd;
1388 intf = umidi->iface;
1391 hostif = intf->altsetting;
1392 intfd = get_iface_desc(hostif);
1394 * The various MidiSport devices have more or less random endpoint
1395 * numbers, so we have to identify the endpoints by their index in
1396 * the descriptor array, like the driver for that other OS does.
1398 * There is one interrupt input endpoint for all input ports, one
1399 * bulk output endpoint for even-numbered ports, and one for odd-
1400 * numbered ports. Both bulk output endpoints have corresponding
1401 * input bulk endpoints (at indices 1 and 3) which aren't used.
1403 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1404 snd_printdd(KERN_ERR "not enough endpoints\n");
1408 epd = get_endpoint(hostif, 0);
1409 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1410 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1411 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1414 epd = get_endpoint(hostif, 2);
1415 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1416 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1417 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1420 if (endpoint->out_cables > 0x0001) {
1421 epd = get_endpoint(hostif, 4);
1422 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1423 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1424 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1429 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1430 ep_info.out_cables = endpoint->out_cables & 0x5555;
1431 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1435 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1436 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1437 ep_info.in_cables = endpoint->in_cables;
1438 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1442 if (endpoint->out_cables > 0x0001) {
1443 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1444 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1445 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1450 for (cable = 0; cable < 0x10; ++cable) {
1451 if (endpoint->out_cables & (1 << cable))
1452 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1453 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1454 if (endpoint->in_cables & (1 << cable))
1455 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1456 &umidi->endpoints[0].in->ports[cable].substream);
1461 static int snd_usbmidi_create_rawmidi(snd_usb_midi_t* umidi,
1462 int out_ports, int in_ports)
1464 snd_rawmidi_t* rmidi;
1467 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1468 umidi->chip->next_midi_device++,
1469 out_ports, in_ports, &rmidi);
1472 strcpy(rmidi->name, umidi->chip->card->shortname);
1473 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1474 SNDRV_RAWMIDI_INFO_INPUT |
1475 SNDRV_RAWMIDI_INFO_DUPLEX;
1476 rmidi->private_data = umidi;
1477 rmidi->private_free = snd_usbmidi_rawmidi_free;
1478 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1479 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1481 umidi->rmidi = rmidi;
1486 * Temporarily stop input.
1488 void snd_usbmidi_input_stop(struct list_head* p)
1490 snd_usb_midi_t* umidi;
1493 umidi = list_entry(p, snd_usb_midi_t, list);
1494 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1495 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
1497 usb_kill_urb(ep->in->urb);
1501 static void snd_usbmidi_input_start_ep(snd_usb_midi_in_endpoint_t* ep)
1504 struct urb* urb = ep->urb;
1505 urb->dev = ep->umidi->chip->dev;
1506 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1511 * Resume input after a call to snd_usbmidi_input_stop().
1513 void snd_usbmidi_input_start(struct list_head* p)
1515 snd_usb_midi_t* umidi;
1518 umidi = list_entry(p, snd_usb_midi_t, list);
1519 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1520 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1524 * Creates and registers everything needed for a MIDI streaming interface.
1526 int snd_usb_create_midi_interface(snd_usb_audio_t* chip,
1527 struct usb_interface* iface,
1528 const snd_usb_audio_quirk_t* quirk)
1530 snd_usb_midi_t* umidi;
1531 snd_usb_midi_endpoint_info_t endpoints[MIDI_MAX_ENDPOINTS];
1532 int out_ports, in_ports;
1535 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
1539 umidi->iface = iface;
1540 umidi->quirk = quirk;
1541 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
1542 init_timer(&umidi->error_timer);
1543 umidi->error_timer.function = snd_usbmidi_error_timer;
1544 umidi->error_timer.data = (unsigned long)umidi;
1546 /* detect the endpoint(s) to use */
1547 memset(endpoints, 0, sizeof(endpoints));
1548 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1549 case QUIRK_MIDI_STANDARD_INTERFACE:
1550 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1552 case QUIRK_MIDI_FIXED_ENDPOINT:
1553 memcpy(&endpoints[0], quirk->data,
1554 sizeof(snd_usb_midi_endpoint_info_t));
1555 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1557 case QUIRK_MIDI_YAMAHA:
1558 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1560 case QUIRK_MIDI_MIDIMAN:
1561 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1562 memcpy(&endpoints[0], quirk->data,
1563 sizeof(snd_usb_midi_endpoint_info_t));
1566 case QUIRK_MIDI_NOVATION:
1567 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1568 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1570 case QUIRK_MIDI_RAW:
1571 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1572 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1574 case QUIRK_MIDI_EMAGIC:
1575 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1576 memcpy(&endpoints[0], quirk->data,
1577 sizeof(snd_usb_midi_endpoint_info_t));
1578 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1580 case QUIRK_MIDI_MIDITECH:
1581 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1584 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1593 /* create rawmidi device */
1596 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1597 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1598 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1600 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1606 /* create endpoint/port structures */
1607 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1608 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1610 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1612 snd_usbmidi_free(umidi);
1616 list_add(&umidi->list, &umidi->chip->midi_list);
1618 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1619 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1623 EXPORT_SYMBOL(snd_usb_create_midi_interface);
1624 EXPORT_SYMBOL(snd_usbmidi_input_stop);
1625 EXPORT_SYMBOL(snd_usbmidi_input_start);
1626 EXPORT_SYMBOL(snd_usbmidi_disconnect);