2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2009 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 <linux/wait.h>
49 #include <linux/usb/audio.h>
50 #include <linux/module.h>
52 #include <sound/core.h>
53 #include <sound/control.h>
54 #include <sound/rawmidi.h>
55 #include <sound/asequencer.h>
62 * define this to log all USB packets
64 /* #define DUMP_PACKETS */
67 * how long to wait after some USB errors, so that khubd can disconnect() us
68 * without too many spurious errors
70 #define ERROR_DELAY_JIFFIES (HZ / 10)
76 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
77 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
78 MODULE_LICENSE("Dual BSD/GPL");
81 struct usb_ms_header_descriptor {
84 __u8 bDescriptorSubtype;
87 } __attribute__ ((packed));
89 struct usb_ms_endpoint_descriptor {
92 __u8 bDescriptorSubtype;
94 __u8 baAssocJackID[0];
95 } __attribute__ ((packed));
97 struct snd_usb_midi_in_endpoint;
98 struct snd_usb_midi_out_endpoint;
99 struct snd_usb_midi_endpoint;
101 struct usb_protocol_ops {
102 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
103 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
104 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
105 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
106 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
109 struct snd_usb_midi {
110 struct usb_device *dev;
111 struct snd_card *card;
112 struct usb_interface *iface;
113 const struct snd_usb_audio_quirk *quirk;
114 struct snd_rawmidi *rmidi;
115 struct usb_protocol_ops* usb_protocol_ops;
116 struct list_head list;
117 struct timer_list error_timer;
118 spinlock_t disc_lock;
119 struct rw_semaphore disc_rwsem;
122 int next_midi_device;
124 struct snd_usb_midi_endpoint {
125 struct snd_usb_midi_out_endpoint *out;
126 struct snd_usb_midi_in_endpoint *in;
127 } endpoints[MIDI_MAX_ENDPOINTS];
128 unsigned long input_triggered;
129 unsigned int opened[2];
130 unsigned char disconnected;
131 unsigned char input_running;
133 struct snd_kcontrol *roland_load_ctl;
136 struct snd_usb_midi_out_endpoint {
137 struct snd_usb_midi* umidi;
138 struct out_urb_context {
140 struct snd_usb_midi_out_endpoint *ep;
142 unsigned int active_urbs;
143 unsigned int drain_urbs;
144 int max_transfer; /* size of urb buffer */
145 struct tasklet_struct tasklet;
146 unsigned int next_urb;
147 spinlock_t buffer_lock;
149 struct usbmidi_out_port {
150 struct snd_usb_midi_out_endpoint* ep;
151 struct snd_rawmidi_substream *substream;
153 uint8_t cable; /* cable number << 4 */
155 #define STATE_UNKNOWN 0
156 #define STATE_1PARAM 1
157 #define STATE_2PARAM_1 2
158 #define STATE_2PARAM_2 3
159 #define STATE_SYSEX_0 4
160 #define STATE_SYSEX_1 5
161 #define STATE_SYSEX_2 6
166 wait_queue_head_t drain_wait;
169 struct snd_usb_midi_in_endpoint {
170 struct snd_usb_midi* umidi;
171 struct urb* urbs[INPUT_URBS];
172 struct usbmidi_in_port {
173 struct snd_rawmidi_substream *substream;
174 u8 running_status_length;
183 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
185 static const uint8_t snd_usbmidi_cin_length[] = {
186 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
190 * Submits the URB, with error handling.
192 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
194 int err = usb_submit_urb(urb, flags);
195 if (err < 0 && err != -ENODEV)
196 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
201 * Error handling for URB completion functions.
203 static int snd_usbmidi_urb_error(int status)
206 /* manually unlinked, or device gone */
212 /* errors that might occur during unplugging */
218 snd_printk(KERN_ERR "urb status %d\n", status);
219 return 0; /* continue */
224 * Receives a chunk of MIDI data.
226 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
227 uint8_t* data, int length)
229 struct usbmidi_in_port* port = &ep->ports[portidx];
231 if (!port->substream) {
232 snd_printd("unexpected port %d!\n", portidx);
235 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
237 snd_rawmidi_receive(port->substream, data, length);
241 static void dump_urb(const char *type, const u8 *data, int length)
243 snd_printk(KERN_DEBUG "%s packet: [", type);
244 for (; length > 0; ++data, --length)
245 printk(" %02x", *data);
249 #define dump_urb(type, data, length) /* nothing */
253 * Processes the data read from the device.
255 static void snd_usbmidi_in_urb_complete(struct urb* urb)
257 struct snd_usb_midi_in_endpoint* ep = urb->context;
259 if (urb->status == 0) {
260 dump_urb("received", urb->transfer_buffer, urb->actual_length);
261 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
264 int err = snd_usbmidi_urb_error(urb->status);
266 if (err != -ENODEV) {
267 ep->error_resubmit = 1;
268 mod_timer(&ep->umidi->error_timer,
269 jiffies + ERROR_DELAY_JIFFIES);
275 urb->dev = ep->umidi->dev;
276 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
279 static void snd_usbmidi_out_urb_complete(struct urb* urb)
281 struct out_urb_context *context = urb->context;
282 struct snd_usb_midi_out_endpoint* ep = context->ep;
283 unsigned int urb_index;
285 spin_lock(&ep->buffer_lock);
286 urb_index = context - ep->urbs;
287 ep->active_urbs &= ~(1 << urb_index);
288 if (unlikely(ep->drain_urbs)) {
289 ep->drain_urbs &= ~(1 << urb_index);
290 wake_up(&ep->drain_wait);
292 spin_unlock(&ep->buffer_lock);
293 if (urb->status < 0) {
294 int err = snd_usbmidi_urb_error(urb->status);
297 mod_timer(&ep->umidi->error_timer,
298 jiffies + ERROR_DELAY_JIFFIES);
302 snd_usbmidi_do_output(ep);
306 * This is called when some data should be transferred to the device
307 * (from one or more substreams).
309 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
311 unsigned int urb_index;
315 spin_lock_irqsave(&ep->buffer_lock, flags);
316 if (ep->umidi->disconnected) {
317 spin_unlock_irqrestore(&ep->buffer_lock, flags);
321 urb_index = ep->next_urb;
323 if (!(ep->active_urbs & (1 << urb_index))) {
324 urb = ep->urbs[urb_index].urb;
325 urb->transfer_buffer_length = 0;
326 ep->umidi->usb_protocol_ops->output(ep, urb);
327 if (urb->transfer_buffer_length == 0)
330 dump_urb("sending", urb->transfer_buffer,
331 urb->transfer_buffer_length);
332 urb->dev = ep->umidi->dev;
333 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
335 ep->active_urbs |= 1 << urb_index;
337 if (++urb_index >= OUTPUT_URBS)
339 if (urb_index == ep->next_urb)
342 ep->next_urb = urb_index;
343 spin_unlock_irqrestore(&ep->buffer_lock, flags);
346 static void snd_usbmidi_out_tasklet(unsigned long data)
348 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
350 snd_usbmidi_do_output(ep);
353 /* called after transfers had been interrupted due to some USB error */
354 static void snd_usbmidi_error_timer(unsigned long data)
356 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
359 spin_lock(&umidi->disc_lock);
360 if (umidi->disconnected) {
361 spin_unlock(&umidi->disc_lock);
364 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
365 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
366 if (in && in->error_resubmit) {
367 in->error_resubmit = 0;
368 for (j = 0; j < INPUT_URBS; ++j) {
369 if (atomic_read(&in->urbs[j]->use_count))
371 in->urbs[j]->dev = umidi->dev;
372 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
375 if (umidi->endpoints[i].out)
376 snd_usbmidi_do_output(umidi->endpoints[i].out);
378 spin_unlock(&umidi->disc_lock);
381 /* helper function to send static data that may not DMA-able */
382 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
383 const void *data, int len)
386 void *buf = kmemdup(data, len, GFP_KERNEL);
389 dump_urb("sending", buf, len);
391 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
392 buf, len, NULL, 250);
398 * Standard USB MIDI protocol: see the spec.
399 * Midiman protocol: like the standard protocol, but the control byte is the
400 * fourth byte in each packet, and uses length instead of CIN.
403 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
404 uint8_t* buffer, int buffer_length)
408 for (i = 0; i + 3 < buffer_length; i += 4)
409 if (buffer[i] != 0) {
410 int cable = buffer[i] >> 4;
411 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
412 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
416 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
417 uint8_t* buffer, int buffer_length)
421 for (i = 0; i + 3 < buffer_length; i += 4)
422 if (buffer[i + 3] != 0) {
423 int port = buffer[i + 3] >> 4;
424 int length = buffer[i + 3] & 3;
425 snd_usbmidi_input_data(ep, port, &buffer[i], length);
430 * Buggy M-Audio device: running status on input results in a packet that has
431 * the data bytes but not the status byte and that is marked with CIN 4.
433 static void snd_usbmidi_maudio_broken_running_status_input(
434 struct snd_usb_midi_in_endpoint* ep,
435 uint8_t* buffer, int buffer_length)
439 for (i = 0; i + 3 < buffer_length; i += 4)
440 if (buffer[i] != 0) {
441 int cable = buffer[i] >> 4;
442 u8 cin = buffer[i] & 0x0f;
443 struct usbmidi_in_port *port = &ep->ports[cable];
446 length = snd_usbmidi_cin_length[cin];
447 if (cin == 0xf && buffer[i + 1] >= 0xf8)
448 ; /* realtime msg: no running status change */
449 else if (cin >= 0x8 && cin <= 0xe)
451 port->running_status_length = length - 1;
452 else if (cin == 0x4 &&
453 port->running_status_length != 0 &&
454 buffer[i + 1] < 0x80)
455 /* CIN 4 that is not a SysEx */
456 length = port->running_status_length;
459 * All other msgs cannot begin running status.
460 * (A channel msg sent as two or three CIN 0xF
461 * packets could in theory, but this device
462 * doesn't use this format.)
464 port->running_status_length = 0;
465 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
470 * QinHeng CH345 is buggy: every second packet inside a SysEx has not CIN 4
471 * but the previously seen CIN, but still with three data bytes.
473 static void ch345_broken_sysex_input(struct snd_usb_midi_in_endpoint *ep,
474 uint8_t *buffer, int buffer_length)
476 unsigned int i, cin, length;
478 for (i = 0; i + 3 < buffer_length; i += 4) {
479 if (buffer[i] == 0 && i > 0)
481 cin = buffer[i] & 0x0f;
483 cin == ep->last_cin &&
484 (buffer[i + 1 + (cin == 0x6)] & 0x80) == 0)
487 if (buffer[i + 1] == 0x90) {
489 * Either a corrupted running status or a real note-on
490 * message; impossible to detect reliably.
494 length = snd_usbmidi_cin_length[cin];
495 snd_usbmidi_input_data(ep, 0, &buffer[i + 1], length);
496 ep->in_sysex = cin == 0x4;
503 * CME protocol: like the standard protocol, but SysEx commands are sent as a
504 * single USB packet preceded by a 0x0F byte.
506 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
507 uint8_t *buffer, int buffer_length)
509 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
510 snd_usbmidi_standard_input(ep, buffer, buffer_length);
512 snd_usbmidi_input_data(ep, buffer[0] >> 4,
513 &buffer[1], buffer_length - 1);
517 * Adds one USB MIDI packet to the output buffer.
519 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
520 uint8_t p1, uint8_t p2, uint8_t p3)
523 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
528 urb->transfer_buffer_length += 4;
532 * Adds one Midiman packet to the output buffer.
534 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
535 uint8_t p1, uint8_t p2, uint8_t p3)
538 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
542 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
543 urb->transfer_buffer_length += 4;
547 * Converts MIDI commands to USB MIDI packets.
549 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
550 uint8_t b, struct urb* urb)
552 uint8_t p0 = port->cable;
553 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
554 port->ep->umidi->usb_protocol_ops->output_packet;
557 output_packet(urb, p0 | 0x0f, b, 0, 0);
558 } else if (b >= 0xf0) {
562 port->state = STATE_SYSEX_1;
567 port->state = STATE_1PARAM;
571 port->state = STATE_2PARAM_1;
575 port->state = STATE_UNKNOWN;
578 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
579 port->state = STATE_UNKNOWN;
582 switch (port->state) {
584 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
587 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
590 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
593 port->state = STATE_UNKNOWN;
596 } else if (b >= 0x80) {
598 if (b >= 0xc0 && b <= 0xdf)
599 port->state = STATE_1PARAM;
601 port->state = STATE_2PARAM_1;
602 } else { /* b < 0x80 */
603 switch (port->state) {
605 if (port->data[0] < 0xf0) {
606 p0 |= port->data[0] >> 4;
609 port->state = STATE_UNKNOWN;
611 output_packet(urb, p0, port->data[0], b, 0);
615 port->state = STATE_2PARAM_2;
618 if (port->data[0] < 0xf0) {
619 p0 |= port->data[0] >> 4;
620 port->state = STATE_2PARAM_1;
623 port->state = STATE_UNKNOWN;
625 output_packet(urb, p0, port->data[0], port->data[1], b);
629 port->state = STATE_SYSEX_1;
633 port->state = STATE_SYSEX_2;
636 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
637 port->state = STATE_SYSEX_0;
643 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
648 /* FIXME: lower-numbered ports can starve higher-numbered ports */
649 for (p = 0; p < 0x10; ++p) {
650 struct usbmidi_out_port* port = &ep->ports[p];
653 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
655 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
659 snd_usbmidi_transmit_byte(port, b, urb);
664 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
665 .input = snd_usbmidi_standard_input,
666 .output = snd_usbmidi_standard_output,
667 .output_packet = snd_usbmidi_output_standard_packet,
670 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
671 .input = snd_usbmidi_midiman_input,
672 .output = snd_usbmidi_standard_output,
673 .output_packet = snd_usbmidi_output_midiman_packet,
676 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
677 .input = snd_usbmidi_maudio_broken_running_status_input,
678 .output = snd_usbmidi_standard_output,
679 .output_packet = snd_usbmidi_output_standard_packet,
682 static struct usb_protocol_ops snd_usbmidi_cme_ops = {
683 .input = snd_usbmidi_cme_input,
684 .output = snd_usbmidi_standard_output,
685 .output_packet = snd_usbmidi_output_standard_packet,
688 static struct usb_protocol_ops snd_usbmidi_ch345_broken_sysex_ops = {
689 .input = ch345_broken_sysex_input,
690 .output = snd_usbmidi_standard_output,
691 .output_packet = snd_usbmidi_output_standard_packet,
695 * AKAI MPD16 protocol:
697 * For control port (endpoint 1):
698 * ==============================
699 * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
700 * SysEx message (msg_len=9 bytes long).
702 * For data port (endpoint 2):
703 * ===========================
704 * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
705 * MIDI message (msg_len bytes long)
707 * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
709 static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
710 uint8_t *buffer, int buffer_length)
712 unsigned int pos = 0;
713 unsigned int len = (unsigned int)buffer_length;
715 unsigned int port = (buffer[pos] >> 4) - 1;
716 unsigned int msg_len = buffer[pos] & 0x0f;
718 if (pos + msg_len <= len && port < 2)
719 snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
724 #define MAX_AKAI_SYSEX_LEN 9
726 static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
730 int pos, end, count, buf_end;
731 uint8_t tmp[MAX_AKAI_SYSEX_LEN];
732 struct snd_rawmidi_substream *substream = ep->ports[0].substream;
734 if (!ep->ports[0].active)
737 msg = urb->transfer_buffer + urb->transfer_buffer_length;
738 buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
740 /* only try adding more data when there's space for at least 1 SysEx */
741 while (urb->transfer_buffer_length < buf_end) {
742 count = snd_rawmidi_transmit_peek(substream,
743 tmp, MAX_AKAI_SYSEX_LEN);
745 ep->ports[0].active = 0;
748 /* try to skip non-SysEx data */
749 for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
753 snd_rawmidi_transmit_ack(substream, pos);
757 /* look for the start or end marker */
758 for (end = 1; end < count && tmp[end] < 0xF0; end++)
761 /* next SysEx started before the end of current one */
762 if (end < count && tmp[end] == 0xF0) {
763 /* it's incomplete - drop it */
764 snd_rawmidi_transmit_ack(substream, end);
768 if (end < count && tmp[end] == 0xF7) {
769 /* queue it, ack it, and get the next one */
771 msg[0] = 0x10 | count;
772 memcpy(&msg[1], tmp, count);
773 snd_rawmidi_transmit_ack(substream, count);
774 urb->transfer_buffer_length += count + 1;
778 /* less than 9 bytes and no end byte - wait for more */
779 if (count < MAX_AKAI_SYSEX_LEN) {
780 ep->ports[0].active = 0;
783 /* 9 bytes and no end marker in sight - malformed, skip it */
784 snd_rawmidi_transmit_ack(substream, count);
788 static struct usb_protocol_ops snd_usbmidi_akai_ops = {
789 .input = snd_usbmidi_akai_input,
790 .output = snd_usbmidi_akai_output,
794 * Novation USB MIDI protocol: number of data bytes is in the first byte
795 * (when receiving) (+1!) or in the second byte (when sending); data begins
799 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
800 uint8_t* buffer, int buffer_length)
802 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
804 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
807 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
810 uint8_t* transfer_buffer;
813 if (!ep->ports[0].active)
815 transfer_buffer = urb->transfer_buffer;
816 count = snd_rawmidi_transmit(ep->ports[0].substream,
818 ep->max_transfer - 2);
820 ep->ports[0].active = 0;
823 transfer_buffer[0] = 0;
824 transfer_buffer[1] = count;
825 urb->transfer_buffer_length = 2 + count;
828 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
829 .input = snd_usbmidi_novation_input,
830 .output = snd_usbmidi_novation_output,
834 * "raw" protocol: just move raw MIDI bytes from/to the endpoint
837 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
838 uint8_t* buffer, int buffer_length)
840 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
843 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
848 if (!ep->ports[0].active)
850 count = snd_rawmidi_transmit(ep->ports[0].substream,
851 urb->transfer_buffer,
854 ep->ports[0].active = 0;
857 urb->transfer_buffer_length = count;
860 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
861 .input = snd_usbmidi_raw_input,
862 .output = snd_usbmidi_raw_output,
866 * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
869 static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint* ep,
870 uint8_t* buffer, int buffer_length)
872 if (buffer_length > 2)
873 snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
876 static struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
877 .input = snd_usbmidi_ftdi_input,
878 .output = snd_usbmidi_raw_output,
881 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
882 uint8_t *buffer, int buffer_length)
884 if (buffer_length != 9)
887 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
890 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
893 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
898 if (!ep->ports[0].active)
900 switch (snd_usb_get_speed(ep->umidi->dev)) {
902 case USB_SPEED_SUPER:
908 count = snd_rawmidi_transmit(ep->ports[0].substream,
909 urb->transfer_buffer,
912 ep->ports[0].active = 0;
916 memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
917 urb->transfer_buffer_length = ep->max_transfer;
920 static struct usb_protocol_ops snd_usbmidi_122l_ops = {
921 .input = snd_usbmidi_us122l_input,
922 .output = snd_usbmidi_us122l_output,
926 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
929 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
931 static const u8 init_data[] = {
932 /* initialization magic: "get version" */
934 0x00, 0x20, 0x31, /* Emagic */
936 0x0b, /* version number request */
937 0x00, /* command version */
938 0x00, /* EEPROM, box 0 */
941 send_bulk_static_data(ep, init_data, sizeof(init_data));
942 /* while we're at it, pour on more magic */
943 send_bulk_static_data(ep, init_data, sizeof(init_data));
946 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
948 static const u8 finish_data[] = {
949 /* switch to patch mode with last preset */
951 0x00, 0x20, 0x31, /* Emagic */
953 0x10, /* patch switch command */
954 0x00, /* command version */
955 0x7f, /* to all boxes */
956 0x40, /* last preset in EEPROM */
959 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
962 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
963 uint8_t* buffer, int buffer_length)
967 /* FF indicates end of valid data */
968 for (i = 0; i < buffer_length; ++i)
969 if (buffer[i] == 0xff) {
974 /* handle F5 at end of last buffer */
978 while (buffer_length > 0) {
979 /* determine size of data until next F5 */
980 for (i = 0; i < buffer_length; ++i)
981 if (buffer[i] == 0xf5)
983 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
987 if (buffer_length <= 0)
989 /* assert(buffer[0] == 0xf5); */
995 if (buffer_length <= 0)
997 if (buffer[0] < 0x80) {
998 ep->current_port = (buffer[0] - 1) & 15;
1006 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
1009 int port0 = ep->current_port;
1010 uint8_t* buf = urb->transfer_buffer;
1011 int buf_free = ep->max_transfer;
1014 for (i = 0; i < 0x10; ++i) {
1015 /* round-robin, starting at the last current port */
1016 int portnum = (port0 + i) & 15;
1017 struct usbmidi_out_port* port = &ep->ports[portnum];
1021 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
1026 if (portnum != ep->current_port) {
1029 ep->current_port = portnum;
1031 buf[1] = (portnum + 1) & 15;
1038 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
1046 if (buf_free < ep->max_transfer && buf_free > 0) {
1050 urb->transfer_buffer_length = ep->max_transfer - buf_free;
1053 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
1054 .input = snd_usbmidi_emagic_input,
1055 .output = snd_usbmidi_emagic_output,
1056 .init_out_endpoint = snd_usbmidi_emagic_init_out,
1057 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
1061 static void update_roland_altsetting(struct snd_usb_midi* umidi)
1063 struct usb_interface *intf;
1064 struct usb_host_interface *hostif;
1065 struct usb_interface_descriptor *intfd;
1068 intf = umidi->iface;
1069 is_light_load = intf->cur_altsetting != intf->altsetting;
1070 if (umidi->roland_load_ctl->private_value == is_light_load)
1072 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
1073 intfd = get_iface_desc(hostif);
1074 snd_usbmidi_input_stop(&umidi->list);
1075 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1076 intfd->bAlternateSetting);
1077 snd_usbmidi_input_start(&umidi->list);
1080 static int substream_open(struct snd_rawmidi_substream *substream, int dir,
1083 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1084 struct snd_kcontrol *ctl;
1086 down_read(&umidi->disc_rwsem);
1087 if (umidi->disconnected) {
1088 up_read(&umidi->disc_rwsem);
1089 return open ? -ENODEV : 0;
1092 mutex_lock(&umidi->mutex);
1094 if (!umidi->opened[0] && !umidi->opened[1]) {
1095 if (umidi->roland_load_ctl) {
1096 ctl = umidi->roland_load_ctl;
1097 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1098 snd_ctl_notify(umidi->card,
1099 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1100 update_roland_altsetting(umidi);
1103 umidi->opened[dir]++;
1104 if (umidi->opened[1])
1105 snd_usbmidi_input_start(&umidi->list);
1107 umidi->opened[dir]--;
1108 if (!umidi->opened[1])
1109 snd_usbmidi_input_stop(&umidi->list);
1110 if (!umidi->opened[0] && !umidi->opened[1]) {
1111 if (umidi->roland_load_ctl) {
1112 ctl = umidi->roland_load_ctl;
1113 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1114 snd_ctl_notify(umidi->card,
1115 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1119 mutex_unlock(&umidi->mutex);
1120 up_read(&umidi->disc_rwsem);
1124 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
1126 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1127 struct usbmidi_out_port* port = NULL;
1130 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1131 if (umidi->endpoints[i].out)
1132 for (j = 0; j < 0x10; ++j)
1133 if (umidi->endpoints[i].out->ports[j].substream == substream) {
1134 port = &umidi->endpoints[i].out->ports[j];
1142 substream->runtime->private_data = port;
1143 port->state = STATE_UNKNOWN;
1144 return substream_open(substream, 0, 1);
1147 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
1149 return substream_open(substream, 0, 0);
1152 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1154 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
1158 if (port->ep->umidi->disconnected) {
1159 /* gobble up remaining bytes to prevent wait in
1160 * snd_rawmidi_drain_output */
1161 while (!snd_rawmidi_transmit_empty(substream))
1162 snd_rawmidi_transmit_ack(substream, 1);
1165 tasklet_schedule(&port->ep->tasklet);
1169 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
1171 struct usbmidi_out_port* port = substream->runtime->private_data;
1172 struct snd_usb_midi_out_endpoint *ep = port->ep;
1173 unsigned int drain_urbs;
1175 long timeout = msecs_to_jiffies(50);
1177 if (ep->umidi->disconnected)
1180 * The substream buffer is empty, but some data might still be in the
1181 * currently active URBs, so we have to wait for those to complete.
1183 spin_lock_irq(&ep->buffer_lock);
1184 drain_urbs = ep->active_urbs;
1186 ep->drain_urbs |= drain_urbs;
1188 prepare_to_wait(&ep->drain_wait, &wait,
1189 TASK_UNINTERRUPTIBLE);
1190 spin_unlock_irq(&ep->buffer_lock);
1191 timeout = schedule_timeout(timeout);
1192 spin_lock_irq(&ep->buffer_lock);
1193 drain_urbs &= ep->drain_urbs;
1194 } while (drain_urbs && timeout);
1195 finish_wait(&ep->drain_wait, &wait);
1197 spin_unlock_irq(&ep->buffer_lock);
1200 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
1202 return substream_open(substream, 1, 1);
1205 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
1207 return substream_open(substream, 1, 0);
1210 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1212 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1215 set_bit(substream->number, &umidi->input_triggered);
1217 clear_bit(substream->number, &umidi->input_triggered);
1220 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
1221 .open = snd_usbmidi_output_open,
1222 .close = snd_usbmidi_output_close,
1223 .trigger = snd_usbmidi_output_trigger,
1224 .drain = snd_usbmidi_output_drain,
1227 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
1228 .open = snd_usbmidi_input_open,
1229 .close = snd_usbmidi_input_close,
1230 .trigger = snd_usbmidi_input_trigger
1233 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1234 unsigned int buffer_length)
1236 usb_free_coherent(umidi->dev, buffer_length,
1237 urb->transfer_buffer, urb->transfer_dma);
1242 * Frees an input endpoint.
1243 * May be called when ep hasn't been initialized completely.
1245 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
1249 for (i = 0; i < INPUT_URBS; ++i)
1251 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1252 ep->urbs[i]->transfer_buffer_length);
1257 * Creates an input endpoint.
1259 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1260 struct snd_usb_midi_endpoint_info* ep_info,
1261 struct snd_usb_midi_endpoint* rep)
1263 struct snd_usb_midi_in_endpoint* ep;
1270 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1275 for (i = 0; i < INPUT_URBS; ++i) {
1276 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1278 snd_usbmidi_in_endpoint_delete(ep);
1282 if (ep_info->in_interval)
1283 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
1285 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1286 length = usb_maxpacket(umidi->dev, pipe, 0);
1287 for (i = 0; i < INPUT_URBS; ++i) {
1288 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1289 &ep->urbs[i]->transfer_dma);
1291 snd_usbmidi_in_endpoint_delete(ep);
1294 if (ep_info->in_interval)
1295 usb_fill_int_urb(ep->urbs[i], umidi->dev,
1296 pipe, buffer, length,
1297 snd_usbmidi_in_urb_complete,
1298 ep, ep_info->in_interval);
1300 usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
1301 pipe, buffer, length,
1302 snd_usbmidi_in_urb_complete, ep);
1303 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1311 * Frees an output endpoint.
1312 * May be called when ep hasn't been initialized completely.
1314 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
1318 for (i = 0; i < OUTPUT_URBS; ++i)
1319 if (ep->urbs[i].urb) {
1320 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1322 ep->urbs[i].urb = NULL;
1326 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1328 snd_usbmidi_out_endpoint_clear(ep);
1333 * Creates an output endpoint, and initializes output ports.
1335 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1336 struct snd_usb_midi_endpoint_info* ep_info,
1337 struct snd_usb_midi_endpoint* rep)
1339 struct snd_usb_midi_out_endpoint* ep;
1345 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1350 for (i = 0; i < OUTPUT_URBS; ++i) {
1351 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1352 if (!ep->urbs[i].urb) {
1353 snd_usbmidi_out_endpoint_delete(ep);
1356 ep->urbs[i].ep = ep;
1358 if (ep_info->out_interval)
1359 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
1361 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
1362 switch (umidi->usb_id) {
1364 ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
1367 * Various chips declare a packet size larger than 4 bytes, but
1368 * do not actually work with larger packets:
1370 case USB_ID(0x0a67, 0x5011): /* Medeli DD305 */
1371 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1372 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1373 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1374 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1375 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1376 case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
1377 ep->max_transfer = 4;
1380 * Some devices only work with 9 bytes packet size:
1382 case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
1383 case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
1384 ep->max_transfer = 9;
1387 for (i = 0; i < OUTPUT_URBS; ++i) {
1388 buffer = usb_alloc_coherent(umidi->dev,
1389 ep->max_transfer, GFP_KERNEL,
1390 &ep->urbs[i].urb->transfer_dma);
1392 snd_usbmidi_out_endpoint_delete(ep);
1395 if (ep_info->out_interval)
1396 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
1397 pipe, buffer, ep->max_transfer,
1398 snd_usbmidi_out_urb_complete,
1399 &ep->urbs[i], ep_info->out_interval);
1401 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
1402 pipe, buffer, ep->max_transfer,
1403 snd_usbmidi_out_urb_complete,
1405 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1408 spin_lock_init(&ep->buffer_lock);
1409 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1410 init_waitqueue_head(&ep->drain_wait);
1412 for (i = 0; i < 0x10; ++i)
1413 if (ep_info->out_cables & (1 << i)) {
1414 ep->ports[i].ep = ep;
1415 ep->ports[i].cable = i << 4;
1418 if (umidi->usb_protocol_ops->init_out_endpoint)
1419 umidi->usb_protocol_ops->init_out_endpoint(ep);
1428 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1432 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1433 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1435 snd_usbmidi_out_endpoint_delete(ep->out);
1437 snd_usbmidi_in_endpoint_delete(ep->in);
1439 mutex_destroy(&umidi->mutex);
1444 * Unlinks all URBs (must be done before the usb_device is deleted).
1446 void snd_usbmidi_disconnect(struct list_head* p)
1448 struct snd_usb_midi* umidi;
1451 umidi = list_entry(p, struct snd_usb_midi, list);
1453 * an URB's completion handler may start the timer and
1454 * a timer may submit an URB. To reliably break the cycle
1455 * a flag under lock must be used
1457 down_write(&umidi->disc_rwsem);
1458 spin_lock_irq(&umidi->disc_lock);
1459 umidi->disconnected = 1;
1460 spin_unlock_irq(&umidi->disc_lock);
1461 up_write(&umidi->disc_rwsem);
1463 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1464 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1466 tasklet_kill(&ep->out->tasklet);
1468 for (j = 0; j < OUTPUT_URBS; ++j)
1469 usb_kill_urb(ep->out->urbs[j].urb);
1470 if (umidi->usb_protocol_ops->finish_out_endpoint)
1471 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1472 ep->out->active_urbs = 0;
1473 if (ep->out->drain_urbs) {
1474 ep->out->drain_urbs = 0;
1475 wake_up(&ep->out->drain_wait);
1479 for (j = 0; j < INPUT_URBS; ++j)
1480 usb_kill_urb(ep->in->urbs[j]);
1481 /* free endpoints here; later call can result in Oops */
1483 snd_usbmidi_out_endpoint_clear(ep->out);
1485 snd_usbmidi_in_endpoint_delete(ep->in);
1489 del_timer_sync(&umidi->error_timer);
1492 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1494 struct snd_usb_midi* umidi = rmidi->private_data;
1495 snd_usbmidi_free(umidi);
1498 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1499 int stream, int number)
1501 struct list_head* list;
1503 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1504 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1505 if (substream->number == number)
1512 * This list specifies names for ports that do not fit into the standard
1513 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1514 * such as internal control or synthesizer ports.
1516 static struct port_info {
1521 unsigned int seq_flags;
1522 } snd_usbmidi_port_info[] = {
1523 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1524 { .id = USB_ID(vendor, product), \
1525 .port = num, .voices = voices_, \
1526 .name = name_, .seq_flags = flags }
1527 #define EXTERNAL_PORT(vendor, product, num, name) \
1528 PORT_INFO(vendor, product, num, name, 0, \
1529 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1530 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1531 SNDRV_SEQ_PORT_TYPE_PORT)
1532 #define CONTROL_PORT(vendor, product, num, name) \
1533 PORT_INFO(vendor, product, num, name, 0, \
1534 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1535 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1536 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1537 PORT_INFO(vendor, product, num, name, voices, \
1538 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1539 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1540 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1541 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1542 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1543 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1544 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1545 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1546 PORT_INFO(vendor, product, num, name, voices, \
1547 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1548 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1549 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1550 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1551 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1552 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1553 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1554 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1556 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1557 /* Roland SC-8850 */
1558 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1559 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1560 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1561 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1562 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1563 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1565 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1566 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1567 /* Roland SC-8820 */
1568 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1569 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1570 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1572 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1573 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1574 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1576 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1577 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1578 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1580 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1582 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1583 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1584 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1585 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1587 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1589 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1590 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1591 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1593 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1594 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1595 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1596 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1598 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1599 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1601 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1602 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1603 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1605 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1606 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1607 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1609 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1610 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1611 /* Edirol UA-1000 */
1612 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1613 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1615 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1616 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1617 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1619 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1620 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1621 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1623 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1624 /* M-Audio MidiSport 8x8 */
1625 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1626 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1628 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1629 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1630 /* Emagic Unitor8/AMT8/MT4 */
1631 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1632 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1633 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1635 CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1636 PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1637 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1638 SNDRV_SEQ_PORT_TYPE_HARDWARE),
1639 /* Access Music Virus TI */
1640 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1641 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1642 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1643 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1644 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
1647 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1651 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1652 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
1653 snd_usbmidi_port_info[i].port == number)
1654 return &snd_usbmidi_port_info[i];
1659 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1660 struct snd_seq_port_info *seq_port_info)
1662 struct snd_usb_midi *umidi = rmidi->private_data;
1663 struct port_info *port_info;
1665 /* TODO: read port flags from descriptors */
1666 port_info = find_port_info(umidi, number);
1668 seq_port_info->type = port_info->seq_flags;
1669 seq_port_info->midi_voices = port_info->voices;
1673 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1674 int stream, int number,
1675 struct snd_rawmidi_substream ** rsubstream)
1677 struct port_info *port_info;
1678 const char *name_format;
1680 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1682 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1686 /* TODO: read port name from jack descriptor */
1687 port_info = find_port_info(umidi, number);
1688 name_format = port_info ? port_info->name : "%s MIDI %d";
1689 snprintf(substream->name, sizeof(substream->name),
1690 name_format, umidi->card->shortname, number + 1);
1692 *rsubstream = substream;
1696 * Creates the endpoints and their ports.
1698 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1699 struct snd_usb_midi_endpoint_info* endpoints)
1702 int out_ports = 0, in_ports = 0;
1704 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1705 if (endpoints[i].out_cables) {
1706 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1707 &umidi->endpoints[i]);
1711 if (endpoints[i].in_cables) {
1712 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1713 &umidi->endpoints[i]);
1718 for (j = 0; j < 0x10; ++j) {
1719 if (endpoints[i].out_cables & (1 << j)) {
1720 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1721 &umidi->endpoints[i].out->ports[j].substream);
1724 if (endpoints[i].in_cables & (1 << j)) {
1725 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1726 &umidi->endpoints[i].in->ports[j].substream);
1731 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1732 out_ports, in_ports);
1737 * Returns MIDIStreaming device capabilities.
1739 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1740 struct snd_usb_midi_endpoint_info* endpoints)
1742 struct usb_interface* intf;
1743 struct usb_host_interface *hostif;
1744 struct usb_interface_descriptor* intfd;
1745 struct usb_ms_header_descriptor* ms_header;
1746 struct usb_host_endpoint *hostep;
1747 struct usb_endpoint_descriptor* ep;
1748 struct usb_ms_endpoint_descriptor* ms_ep;
1751 intf = umidi->iface;
1754 hostif = &intf->altsetting[0];
1755 intfd = get_iface_desc(hostif);
1756 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1757 if (hostif->extralen >= 7 &&
1758 ms_header->bLength >= 7 &&
1759 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1760 ms_header->bDescriptorSubtype == UAC_HEADER)
1761 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1762 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1764 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1767 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1768 hostep = &hostif->endpoint[i];
1769 ep = get_ep_desc(hostep);
1770 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
1772 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1773 if (hostep->extralen < 4 ||
1774 ms_ep->bLength < 4 ||
1775 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1776 ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
1778 if (usb_endpoint_dir_out(ep)) {
1779 if (endpoints[epidx].out_ep) {
1780 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1781 snd_printk(KERN_WARNING "too many endpoints\n");
1785 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1786 if (usb_endpoint_xfer_int(ep))
1787 endpoints[epidx].out_interval = ep->bInterval;
1788 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1790 * Low speed bulk transfers don't exist, so
1791 * force interrupt transfers for devices like
1792 * ESI MIDI Mate that try to use them anyway.
1794 endpoints[epidx].out_interval = 1;
1795 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1796 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1797 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1799 if (endpoints[epidx].in_ep) {
1800 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1801 snd_printk(KERN_WARNING "too many endpoints\n");
1805 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1806 if (usb_endpoint_xfer_int(ep))
1807 endpoints[epidx].in_interval = ep->bInterval;
1808 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1809 endpoints[epidx].in_interval = 1;
1810 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1811 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1812 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1818 static int roland_load_info(struct snd_kcontrol *kcontrol,
1819 struct snd_ctl_elem_info *info)
1821 static const char *const names[] = { "High Load", "Light Load" };
1823 return snd_ctl_enum_info(info, 1, 2, names);
1826 static int roland_load_get(struct snd_kcontrol *kcontrol,
1827 struct snd_ctl_elem_value *value)
1829 value->value.enumerated.item[0] = kcontrol->private_value;
1833 static int roland_load_put(struct snd_kcontrol *kcontrol,
1834 struct snd_ctl_elem_value *value)
1836 struct snd_usb_midi* umidi = kcontrol->private_data;
1839 if (value->value.enumerated.item[0] > 1)
1841 mutex_lock(&umidi->mutex);
1842 changed = value->value.enumerated.item[0] != kcontrol->private_value;
1844 kcontrol->private_value = value->value.enumerated.item[0];
1845 mutex_unlock(&umidi->mutex);
1849 static struct snd_kcontrol_new roland_load_ctl = {
1850 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1851 .name = "MIDI Input Mode",
1852 .info = roland_load_info,
1853 .get = roland_load_get,
1854 .put = roland_load_put,
1859 * On Roland devices, use the second alternate setting to be able to use
1860 * the interrupt input endpoint.
1862 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1864 struct usb_interface* intf;
1865 struct usb_host_interface *hostif;
1866 struct usb_interface_descriptor* intfd;
1868 intf = umidi->iface;
1869 if (!intf || intf->num_altsetting != 2)
1872 hostif = &intf->altsetting[1];
1873 intfd = get_iface_desc(hostif);
1874 if (intfd->bNumEndpoints != 2 ||
1875 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1876 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1879 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1880 intfd->bAlternateSetting);
1881 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1882 intfd->bAlternateSetting);
1884 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
1885 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
1886 umidi->roland_load_ctl = NULL;
1890 * Try to find any usable endpoints in the interface.
1892 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1893 struct snd_usb_midi_endpoint_info* endpoint,
1896 struct usb_interface* intf;
1897 struct usb_host_interface *hostif;
1898 struct usb_interface_descriptor* intfd;
1899 struct usb_endpoint_descriptor* epd;
1900 int i, out_eps = 0, in_eps = 0;
1902 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
1903 snd_usbmidi_switch_roland_altsetting(umidi);
1905 if (endpoint[0].out_ep || endpoint[0].in_ep)
1908 intf = umidi->iface;
1909 if (!intf || intf->num_altsetting < 1)
1911 hostif = intf->cur_altsetting;
1912 intfd = get_iface_desc(hostif);
1914 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1915 epd = get_endpoint(hostif, i);
1916 if (!usb_endpoint_xfer_bulk(epd) &&
1917 !usb_endpoint_xfer_int(epd))
1919 if (out_eps < max_endpoints &&
1920 usb_endpoint_dir_out(epd)) {
1921 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1922 if (usb_endpoint_xfer_int(epd))
1923 endpoint[out_eps].out_interval = epd->bInterval;
1926 if (in_eps < max_endpoints &&
1927 usb_endpoint_dir_in(epd)) {
1928 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1929 if (usb_endpoint_xfer_int(epd))
1930 endpoint[in_eps].in_interval = epd->bInterval;
1934 return (out_eps || in_eps) ? 0 : -ENOENT;
1938 * Detects the endpoints for one-port-per-endpoint protocols.
1940 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1941 struct snd_usb_midi_endpoint_info* endpoints)
1945 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1946 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1947 if (endpoints[i].out_ep)
1948 endpoints[i].out_cables = 0x0001;
1949 if (endpoints[i].in_ep)
1950 endpoints[i].in_cables = 0x0001;
1956 * Detects the endpoints and ports of Yamaha devices.
1958 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1959 struct snd_usb_midi_endpoint_info* endpoint)
1961 struct usb_interface* intf;
1962 struct usb_host_interface *hostif;
1963 struct usb_interface_descriptor* intfd;
1966 intf = umidi->iface;
1969 hostif = intf->altsetting;
1970 intfd = get_iface_desc(hostif);
1971 if (intfd->bNumEndpoints < 1)
1975 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1976 * necessarily with any useful contents. So simply count 'em.
1978 for (cs_desc = hostif->extra;
1979 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1980 cs_desc += cs_desc[0]) {
1981 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1982 if (cs_desc[2] == UAC_MIDI_IN_JACK)
1983 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1984 else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
1985 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1988 if (!endpoint->in_cables && !endpoint->out_cables)
1991 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1995 * Creates the endpoints and their ports for Midiman devices.
1997 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1998 struct snd_usb_midi_endpoint_info* endpoint)
2000 struct snd_usb_midi_endpoint_info ep_info;
2001 struct usb_interface* intf;
2002 struct usb_host_interface *hostif;
2003 struct usb_interface_descriptor* intfd;
2004 struct usb_endpoint_descriptor* epd;
2007 intf = umidi->iface;
2010 hostif = intf->altsetting;
2011 intfd = get_iface_desc(hostif);
2013 * The various MidiSport devices have more or less random endpoint
2014 * numbers, so we have to identify the endpoints by their index in
2015 * the descriptor array, like the driver for that other OS does.
2017 * There is one interrupt input endpoint for all input ports, one
2018 * bulk output endpoint for even-numbered ports, and one for odd-
2019 * numbered ports. Both bulk output endpoints have corresponding
2020 * input bulk endpoints (at indices 1 and 3) which aren't used.
2022 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
2023 snd_printdd(KERN_ERR "not enough endpoints\n");
2027 epd = get_endpoint(hostif, 0);
2028 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
2029 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
2032 epd = get_endpoint(hostif, 2);
2033 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
2034 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
2037 if (endpoint->out_cables > 0x0001) {
2038 epd = get_endpoint(hostif, 4);
2039 if (!usb_endpoint_dir_out(epd) ||
2040 !usb_endpoint_xfer_bulk(epd)) {
2041 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
2046 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2047 ep_info.out_interval = 0;
2048 ep_info.out_cables = endpoint->out_cables & 0x5555;
2049 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
2053 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2054 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
2055 ep_info.in_cables = endpoint->in_cables;
2056 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
2060 if (endpoint->out_cables > 0x0001) {
2061 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2062 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
2063 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
2068 for (cable = 0; cable < 0x10; ++cable) {
2069 if (endpoint->out_cables & (1 << cable))
2070 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
2071 &umidi->endpoints[cable & 1].out->ports[cable].substream);
2072 if (endpoint->in_cables & (1 << cable))
2073 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
2074 &umidi->endpoints[0].in->ports[cable].substream);
2079 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
2080 .get_port_info = snd_usbmidi_get_port_info,
2083 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
2084 int out_ports, int in_ports)
2086 struct snd_rawmidi *rmidi;
2089 err = snd_rawmidi_new(umidi->card, "USB MIDI",
2090 umidi->next_midi_device++,
2091 out_ports, in_ports, &rmidi);
2094 strcpy(rmidi->name, umidi->card->shortname);
2095 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2096 SNDRV_RAWMIDI_INFO_INPUT |
2097 SNDRV_RAWMIDI_INFO_DUPLEX;
2098 rmidi->ops = &snd_usbmidi_ops;
2099 rmidi->private_data = umidi;
2100 rmidi->private_free = snd_usbmidi_rawmidi_free;
2101 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
2102 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
2104 umidi->rmidi = rmidi;
2109 * Temporarily stop input.
2111 void snd_usbmidi_input_stop(struct list_head* p)
2113 struct snd_usb_midi* umidi;
2116 umidi = list_entry(p, struct snd_usb_midi, list);
2117 if (!umidi->input_running)
2119 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2120 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
2122 for (j = 0; j < INPUT_URBS; ++j)
2123 usb_kill_urb(ep->in->urbs[j]);
2125 umidi->input_running = 0;
2128 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
2134 for (i = 0; i < INPUT_URBS; ++i) {
2135 struct urb* urb = ep->urbs[i];
2136 urb->dev = ep->umidi->dev;
2137 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
2142 * Resume input after a call to snd_usbmidi_input_stop().
2144 void snd_usbmidi_input_start(struct list_head* p)
2146 struct snd_usb_midi* umidi;
2149 umidi = list_entry(p, struct snd_usb_midi, list);
2150 if (umidi->input_running || !umidi->opened[1])
2152 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2153 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2154 umidi->input_running = 1;
2158 * Creates and registers everything needed for a MIDI streaming interface.
2160 int snd_usbmidi_create(struct snd_card *card,
2161 struct usb_interface* iface,
2162 struct list_head *midi_list,
2163 const struct snd_usb_audio_quirk* quirk)
2165 struct snd_usb_midi* umidi;
2166 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
2167 int out_ports, in_ports;
2170 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
2173 umidi->dev = interface_to_usbdev(iface);
2175 umidi->iface = iface;
2176 umidi->quirk = quirk;
2177 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
2178 init_timer(&umidi->error_timer);
2179 spin_lock_init(&umidi->disc_lock);
2180 init_rwsem(&umidi->disc_rwsem);
2181 mutex_init(&umidi->mutex);
2182 umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2183 le16_to_cpu(umidi->dev->descriptor.idProduct));
2184 umidi->error_timer.function = snd_usbmidi_error_timer;
2185 umidi->error_timer.data = (unsigned long)umidi;
2187 /* detect the endpoint(s) to use */
2188 memset(endpoints, 0, sizeof(endpoints));
2189 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2190 case QUIRK_MIDI_STANDARD_INTERFACE:
2191 err = snd_usbmidi_get_ms_info(umidi, endpoints);
2192 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
2193 umidi->usb_protocol_ops =
2194 &snd_usbmidi_maudio_broken_running_status_ops;
2196 case QUIRK_MIDI_US122L:
2197 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2199 case QUIRK_MIDI_FIXED_ENDPOINT:
2200 memcpy(&endpoints[0], quirk->data,
2201 sizeof(struct snd_usb_midi_endpoint_info));
2202 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2204 case QUIRK_MIDI_YAMAHA:
2205 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2207 case QUIRK_MIDI_MIDIMAN:
2208 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2209 memcpy(&endpoints[0], quirk->data,
2210 sizeof(struct snd_usb_midi_endpoint_info));
2213 case QUIRK_MIDI_NOVATION:
2214 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2215 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2217 case QUIRK_MIDI_RAW_BYTES:
2218 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
2220 * Interface 1 contains isochronous endpoints, but with the same
2221 * numbers as in interface 0. Since it is interface 1 that the
2222 * USB core has most recently seen, these descriptors are now
2223 * associated with the endpoint numbers. This will foul up our
2224 * attempts to submit bulk/interrupt URBs to the endpoints in
2225 * interface 0, so we have to make sure that the USB core looks
2226 * again at interface 0 by calling usb_set_interface() on it.
2228 if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2229 usb_set_interface(umidi->dev, 0, 0);
2230 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2232 case QUIRK_MIDI_EMAGIC:
2233 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2234 memcpy(&endpoints[0], quirk->data,
2235 sizeof(struct snd_usb_midi_endpoint_info));
2236 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2238 case QUIRK_MIDI_CME:
2239 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
2240 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2242 case QUIRK_MIDI_AKAI:
2243 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2244 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2245 /* endpoint 1 is input-only */
2246 endpoints[1].out_cables = 0;
2248 case QUIRK_MIDI_FTDI:
2249 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2251 /* set baud rate to 31250 (48 MHz / 16 / 96) */
2252 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2253 3, 0x40, 0x60, 0, NULL, 0, 1000);
2257 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2259 case QUIRK_MIDI_CH345:
2260 umidi->usb_protocol_ops = &snd_usbmidi_ch345_broken_sysex_ops;
2261 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2264 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2273 /* create rawmidi device */
2276 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2277 out_ports += hweight16(endpoints[i].out_cables);
2278 in_ports += hweight16(endpoints[i].in_cables);
2280 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2286 /* create endpoint/port structures */
2287 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2288 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2290 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2295 usb_autopm_get_interface_no_resume(umidi->iface);
2297 list_add_tail(&umidi->list, midi_list);
2301 EXPORT_SYMBOL(snd_usbmidi_create);
2302 EXPORT_SYMBOL(snd_usbmidi_input_stop);
2303 EXPORT_SYMBOL(snd_usbmidi_input_start);
2304 EXPORT_SYMBOL(snd_usbmidi_disconnect);