GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / usb / midi.c
bloba14b312caaa8bcb414cac152a274b814db33183b
1 /*
2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2009 Clemens Ladisch
5 * All rights reserved.
7 * Based on the OSS usb-midi driver by NAGANO Daisuke,
8 * NetBSD's umidi driver by Takuya SHIOZAKI,
9 * the "USB Device Class Definition for MIDI Devices" by Roland
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * Alternatively, this software may be distributed and/or modified under the
21 * terms of the GNU General Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at your option) any later
23 * version.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
38 #include <linux/kernel.h>
39 #include <linux/types.h>
40 #include <linux/bitops.h>
41 #include <linux/interrupt.h>
42 #include <linux/spinlock.h>
43 #include <linux/string.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/timer.h>
47 #include <linux/usb.h>
48 #include <linux/wait.h>
49 #include <linux/usb/audio.h>
51 #include <sound/core.h>
52 #include <sound/control.h>
53 #include <sound/rawmidi.h>
54 #include <sound/asequencer.h>
55 #include "usbaudio.h"
56 #include "midi.h"
57 #include "helper.h"
60 * define this to log all USB packets
62 /* #define DUMP_PACKETS */
65 * how long to wait after some USB errors, so that khubd can disconnect() us
66 * without too many spurious errors
68 #define ERROR_DELAY_JIFFIES (HZ / 10)
70 #define OUTPUT_URBS 7
71 #define INPUT_URBS 7
74 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
75 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
76 MODULE_LICENSE("Dual BSD/GPL");
79 struct usb_ms_header_descriptor {
80 __u8 bLength;
81 __u8 bDescriptorType;
82 __u8 bDescriptorSubtype;
83 __u8 bcdMSC[2];
84 __le16 wTotalLength;
85 } __attribute__ ((packed));
87 struct usb_ms_endpoint_descriptor {
88 __u8 bLength;
89 __u8 bDescriptorType;
90 __u8 bDescriptorSubtype;
91 __u8 bNumEmbMIDIJack;
92 __u8 baAssocJackID[0];
93 } __attribute__ ((packed));
95 struct snd_usb_midi_in_endpoint;
96 struct snd_usb_midi_out_endpoint;
97 struct snd_usb_midi_endpoint;
99 struct usb_protocol_ops {
100 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
101 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
102 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
103 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
104 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
107 struct snd_usb_midi {
108 struct usb_device *dev;
109 struct snd_card *card;
110 struct usb_interface *iface;
111 const struct snd_usb_audio_quirk *quirk;
112 struct snd_rawmidi *rmidi;
113 struct usb_protocol_ops* usb_protocol_ops;
114 struct list_head list;
115 struct timer_list error_timer;
116 spinlock_t disc_lock;
117 struct mutex mutex;
118 u32 usb_id;
119 int next_midi_device;
121 struct snd_usb_midi_endpoint {
122 struct snd_usb_midi_out_endpoint *out;
123 struct snd_usb_midi_in_endpoint *in;
124 } endpoints[MIDI_MAX_ENDPOINTS];
125 unsigned long input_triggered;
126 unsigned int opened;
127 unsigned char disconnected;
129 struct snd_kcontrol *roland_load_ctl;
132 struct snd_usb_midi_out_endpoint {
133 struct snd_usb_midi* umidi;
134 struct out_urb_context {
135 struct urb *urb;
136 struct snd_usb_midi_out_endpoint *ep;
137 } urbs[OUTPUT_URBS];
138 unsigned int active_urbs;
139 unsigned int drain_urbs;
140 int max_transfer; /* size of urb buffer */
141 struct tasklet_struct tasklet;
142 unsigned int next_urb;
143 spinlock_t buffer_lock;
145 struct usbmidi_out_port {
146 struct snd_usb_midi_out_endpoint* ep;
147 struct snd_rawmidi_substream *substream;
148 int active;
149 uint8_t cable; /* cable number << 4 */
150 uint8_t state;
151 #define STATE_UNKNOWN 0
152 #define STATE_1PARAM 1
153 #define STATE_2PARAM_1 2
154 #define STATE_2PARAM_2 3
155 #define STATE_SYSEX_0 4
156 #define STATE_SYSEX_1 5
157 #define STATE_SYSEX_2 6
158 uint8_t data[2];
159 } ports[0x10];
160 int current_port;
162 wait_queue_head_t drain_wait;
165 struct snd_usb_midi_in_endpoint {
166 struct snd_usb_midi* umidi;
167 struct urb* urbs[INPUT_URBS];
168 struct usbmidi_in_port {
169 struct snd_rawmidi_substream *substream;
170 u8 running_status_length;
171 } ports[0x10];
172 u8 seen_f5;
173 u8 error_resubmit;
174 int current_port;
177 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
179 static const uint8_t snd_usbmidi_cin_length[] = {
180 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
184 * Submits the URB, with error handling.
186 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
188 int err = usb_submit_urb(urb, flags);
189 if (err < 0 && err != -ENODEV)
190 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
191 return err;
195 * Error handling for URB completion functions.
197 static int snd_usbmidi_urb_error(int status)
199 switch (status) {
200 /* manually unlinked, or device gone */
201 case -ENOENT:
202 case -ECONNRESET:
203 case -ESHUTDOWN:
204 case -ENODEV:
205 return -ENODEV;
206 /* errors that might occur during unplugging */
207 case -EPROTO:
208 case -ETIME:
209 case -EILSEQ:
210 return -EIO;
211 default:
212 snd_printk(KERN_ERR "urb status %d\n", status);
213 return 0; /* continue */
218 * Receives a chunk of MIDI data.
220 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
221 uint8_t* data, int length)
223 struct usbmidi_in_port* port = &ep->ports[portidx];
225 if (!port->substream) {
226 snd_printd("unexpected port %d!\n", portidx);
227 return;
229 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
230 return;
231 snd_rawmidi_receive(port->substream, data, length);
234 #ifdef DUMP_PACKETS
235 static void dump_urb(const char *type, const u8 *data, int length)
237 snd_printk(KERN_DEBUG "%s packet: [", type);
238 for (; length > 0; ++data, --length)
239 printk(" %02x", *data);
240 printk(" ]\n");
242 #else
243 #define dump_urb(type, data, length) /* nothing */
244 #endif
247 * Processes the data read from the device.
249 static void snd_usbmidi_in_urb_complete(struct urb* urb)
251 struct snd_usb_midi_in_endpoint* ep = urb->context;
253 if (urb->status == 0) {
254 dump_urb("received", urb->transfer_buffer, urb->actual_length);
255 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
256 urb->actual_length);
257 } else {
258 int err = snd_usbmidi_urb_error(urb->status);
259 if (err < 0) {
260 if (err != -ENODEV) {
261 ep->error_resubmit = 1;
262 mod_timer(&ep->umidi->error_timer,
263 jiffies + ERROR_DELAY_JIFFIES);
265 return;
269 urb->dev = ep->umidi->dev;
270 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
273 static void snd_usbmidi_out_urb_complete(struct urb* urb)
275 struct out_urb_context *context = urb->context;
276 struct snd_usb_midi_out_endpoint* ep = context->ep;
277 unsigned int urb_index;
279 spin_lock(&ep->buffer_lock);
280 urb_index = context - ep->urbs;
281 ep->active_urbs &= ~(1 << urb_index);
282 if (unlikely(ep->drain_urbs)) {
283 ep->drain_urbs &= ~(1 << urb_index);
284 wake_up(&ep->drain_wait);
286 spin_unlock(&ep->buffer_lock);
287 if (urb->status < 0) {
288 int err = snd_usbmidi_urb_error(urb->status);
289 if (err < 0) {
290 if (err != -ENODEV)
291 mod_timer(&ep->umidi->error_timer,
292 jiffies + ERROR_DELAY_JIFFIES);
293 return;
296 snd_usbmidi_do_output(ep);
300 * This is called when some data should be transferred to the device
301 * (from one or more substreams).
303 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
305 unsigned int urb_index;
306 struct urb* urb;
307 unsigned long flags;
309 spin_lock_irqsave(&ep->buffer_lock, flags);
310 if (ep->umidi->disconnected) {
311 spin_unlock_irqrestore(&ep->buffer_lock, flags);
312 return;
315 urb_index = ep->next_urb;
316 for (;;) {
317 if (!(ep->active_urbs & (1 << urb_index))) {
318 urb = ep->urbs[urb_index].urb;
319 urb->transfer_buffer_length = 0;
320 ep->umidi->usb_protocol_ops->output(ep, urb);
321 if (urb->transfer_buffer_length == 0)
322 break;
324 dump_urb("sending", urb->transfer_buffer,
325 urb->transfer_buffer_length);
326 urb->dev = ep->umidi->dev;
327 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
328 break;
329 ep->active_urbs |= 1 << urb_index;
331 if (++urb_index >= OUTPUT_URBS)
332 urb_index = 0;
333 if (urb_index == ep->next_urb)
334 break;
336 ep->next_urb = urb_index;
337 spin_unlock_irqrestore(&ep->buffer_lock, flags);
340 static void snd_usbmidi_out_tasklet(unsigned long data)
342 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
344 snd_usbmidi_do_output(ep);
347 /* called after transfers had been interrupted due to some USB error */
348 static void snd_usbmidi_error_timer(unsigned long data)
350 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
351 unsigned int i, j;
353 spin_lock(&umidi->disc_lock);
354 if (umidi->disconnected) {
355 spin_unlock(&umidi->disc_lock);
356 return;
358 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
359 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
360 if (in && in->error_resubmit) {
361 in->error_resubmit = 0;
362 for (j = 0; j < INPUT_URBS; ++j) {
363 in->urbs[j]->dev = umidi->dev;
364 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
367 if (umidi->endpoints[i].out)
368 snd_usbmidi_do_output(umidi->endpoints[i].out);
370 spin_unlock(&umidi->disc_lock);
373 /* helper function to send static data that may not DMA-able */
374 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
375 const void *data, int len)
377 int err = 0;
378 void *buf = kmemdup(data, len, GFP_KERNEL);
379 if (!buf)
380 return -ENOMEM;
381 dump_urb("sending", buf, len);
382 if (ep->urbs[0].urb)
383 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
384 buf, len, NULL, 250);
385 kfree(buf);
386 return err;
390 * Standard USB MIDI protocol: see the spec.
391 * Midiman protocol: like the standard protocol, but the control byte is the
392 * fourth byte in each packet, and uses length instead of CIN.
395 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
396 uint8_t* buffer, int buffer_length)
398 int i;
400 for (i = 0; i + 3 < buffer_length; i += 4)
401 if (buffer[i] != 0) {
402 int cable = buffer[i] >> 4;
403 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
404 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
408 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
409 uint8_t* buffer, int buffer_length)
411 int i;
413 for (i = 0; i + 3 < buffer_length; i += 4)
414 if (buffer[i + 3] != 0) {
415 int port = buffer[i + 3] >> 4;
416 int length = buffer[i + 3] & 3;
417 snd_usbmidi_input_data(ep, port, &buffer[i], length);
422 * Buggy M-Audio device: running status on input results in a packet that has
423 * the data bytes but not the status byte and that is marked with CIN 4.
425 static void snd_usbmidi_maudio_broken_running_status_input(
426 struct snd_usb_midi_in_endpoint* ep,
427 uint8_t* buffer, int buffer_length)
429 int i;
431 for (i = 0; i + 3 < buffer_length; i += 4)
432 if (buffer[i] != 0) {
433 int cable = buffer[i] >> 4;
434 u8 cin = buffer[i] & 0x0f;
435 struct usbmidi_in_port *port = &ep->ports[cable];
436 int length;
438 length = snd_usbmidi_cin_length[cin];
439 if (cin == 0xf && buffer[i + 1] >= 0xf8)
440 ; /* realtime msg: no running status change */
441 else if (cin >= 0x8 && cin <= 0xe)
442 /* channel msg */
443 port->running_status_length = length - 1;
444 else if (cin == 0x4 &&
445 port->running_status_length != 0 &&
446 buffer[i + 1] < 0x80)
447 /* CIN 4 that is not a SysEx */
448 length = port->running_status_length;
449 else
451 * All other msgs cannot begin running status.
452 * (A channel msg sent as two or three CIN 0xF
453 * packets could in theory, but this device
454 * doesn't use this format.)
456 port->running_status_length = 0;
457 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
462 * CME protocol: like the standard protocol, but SysEx commands are sent as a
463 * single USB packet preceded by a 0x0F byte.
465 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
466 uint8_t *buffer, int buffer_length)
468 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
469 snd_usbmidi_standard_input(ep, buffer, buffer_length);
470 else
471 snd_usbmidi_input_data(ep, buffer[0] >> 4,
472 &buffer[1], buffer_length - 1);
476 * Adds one USB MIDI packet to the output buffer.
478 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
479 uint8_t p1, uint8_t p2, uint8_t p3)
482 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
483 buf[0] = p0;
484 buf[1] = p1;
485 buf[2] = p2;
486 buf[3] = p3;
487 urb->transfer_buffer_length += 4;
491 * Adds one Midiman packet to the output buffer.
493 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
494 uint8_t p1, uint8_t p2, uint8_t p3)
497 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
498 buf[0] = p1;
499 buf[1] = p2;
500 buf[2] = p3;
501 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
502 urb->transfer_buffer_length += 4;
506 * Converts MIDI commands to USB MIDI packets.
508 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
509 uint8_t b, struct urb* urb)
511 uint8_t p0 = port->cable;
512 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
513 port->ep->umidi->usb_protocol_ops->output_packet;
515 if (b >= 0xf8) {
516 output_packet(urb, p0 | 0x0f, b, 0, 0);
517 } else if (b >= 0xf0) {
518 switch (b) {
519 case 0xf0:
520 port->data[0] = b;
521 port->state = STATE_SYSEX_1;
522 break;
523 case 0xf1:
524 case 0xf3:
525 port->data[0] = b;
526 port->state = STATE_1PARAM;
527 break;
528 case 0xf2:
529 port->data[0] = b;
530 port->state = STATE_2PARAM_1;
531 break;
532 case 0xf4:
533 case 0xf5:
534 port->state = STATE_UNKNOWN;
535 break;
536 case 0xf6:
537 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
538 port->state = STATE_UNKNOWN;
539 break;
540 case 0xf7:
541 switch (port->state) {
542 case STATE_SYSEX_0:
543 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
544 break;
545 case STATE_SYSEX_1:
546 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
547 break;
548 case STATE_SYSEX_2:
549 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
550 break;
552 port->state = STATE_UNKNOWN;
553 break;
555 } else if (b >= 0x80) {
556 port->data[0] = b;
557 if (b >= 0xc0 && b <= 0xdf)
558 port->state = STATE_1PARAM;
559 else
560 port->state = STATE_2PARAM_1;
561 } else { /* b < 0x80 */
562 switch (port->state) {
563 case STATE_1PARAM:
564 if (port->data[0] < 0xf0) {
565 p0 |= port->data[0] >> 4;
566 } else {
567 p0 |= 0x02;
568 port->state = STATE_UNKNOWN;
570 output_packet(urb, p0, port->data[0], b, 0);
571 break;
572 case STATE_2PARAM_1:
573 port->data[1] = b;
574 port->state = STATE_2PARAM_2;
575 break;
576 case STATE_2PARAM_2:
577 if (port->data[0] < 0xf0) {
578 p0 |= port->data[0] >> 4;
579 port->state = STATE_2PARAM_1;
580 } else {
581 p0 |= 0x03;
582 port->state = STATE_UNKNOWN;
584 output_packet(urb, p0, port->data[0], port->data[1], b);
585 break;
586 case STATE_SYSEX_0:
587 port->data[0] = b;
588 port->state = STATE_SYSEX_1;
589 break;
590 case STATE_SYSEX_1:
591 port->data[1] = b;
592 port->state = STATE_SYSEX_2;
593 break;
594 case STATE_SYSEX_2:
595 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
596 port->state = STATE_SYSEX_0;
597 break;
602 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
603 struct urb *urb)
605 int p;
607 for (p = 0; p < 0x10; ++p) {
608 struct usbmidi_out_port* port = &ep->ports[p];
609 if (!port->active)
610 continue;
611 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
612 uint8_t b;
613 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
614 port->active = 0;
615 break;
617 snd_usbmidi_transmit_byte(port, b, urb);
622 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
623 .input = snd_usbmidi_standard_input,
624 .output = snd_usbmidi_standard_output,
625 .output_packet = snd_usbmidi_output_standard_packet,
628 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
629 .input = snd_usbmidi_midiman_input,
630 .output = snd_usbmidi_standard_output,
631 .output_packet = snd_usbmidi_output_midiman_packet,
634 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
635 .input = snd_usbmidi_maudio_broken_running_status_input,
636 .output = snd_usbmidi_standard_output,
637 .output_packet = snd_usbmidi_output_standard_packet,
640 static struct usb_protocol_ops snd_usbmidi_cme_ops = {
641 .input = snd_usbmidi_cme_input,
642 .output = snd_usbmidi_standard_output,
643 .output_packet = snd_usbmidi_output_standard_packet,
647 * AKAI MPD16 protocol:
649 * For control port (endpoint 1):
650 * ==============================
651 * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
652 * SysEx message (msg_len=9 bytes long).
654 * For data port (endpoint 2):
655 * ===========================
656 * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
657 * MIDI message (msg_len bytes long)
659 * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
661 static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
662 uint8_t *buffer, int buffer_length)
664 unsigned int pos = 0;
665 unsigned int len = (unsigned int)buffer_length;
666 while (pos < len) {
667 unsigned int port = (buffer[pos] >> 4) - 1;
668 unsigned int msg_len = buffer[pos] & 0x0f;
669 pos++;
670 if (pos + msg_len <= len && port < 2)
671 snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
672 pos += msg_len;
676 #define MAX_AKAI_SYSEX_LEN 9
678 static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
679 struct urb *urb)
681 uint8_t *msg;
682 int pos, end, count, buf_end;
683 uint8_t tmp[MAX_AKAI_SYSEX_LEN];
684 struct snd_rawmidi_substream *substream = ep->ports[0].substream;
686 if (!ep->ports[0].active)
687 return;
689 msg = urb->transfer_buffer + urb->transfer_buffer_length;
690 buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
692 /* only try adding more data when there's space for at least 1 SysEx */
693 while (urb->transfer_buffer_length < buf_end) {
694 count = snd_rawmidi_transmit_peek(substream,
695 tmp, MAX_AKAI_SYSEX_LEN);
696 if (!count) {
697 ep->ports[0].active = 0;
698 return;
700 /* try to skip non-SysEx data */
701 for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
704 if (pos > 0) {
705 snd_rawmidi_transmit_ack(substream, pos);
706 continue;
709 /* look for the start or end marker */
710 for (end = 1; end < count && tmp[end] < 0xF0; end++)
713 /* next SysEx started before the end of current one */
714 if (end < count && tmp[end] == 0xF0) {
715 /* it's incomplete - drop it */
716 snd_rawmidi_transmit_ack(substream, end);
717 continue;
719 /* SysEx complete */
720 if (end < count && tmp[end] == 0xF7) {
721 /* queue it, ack it, and get the next one */
722 count = end + 1;
723 msg[0] = 0x10 | count;
724 memcpy(&msg[1], tmp, count);
725 snd_rawmidi_transmit_ack(substream, count);
726 urb->transfer_buffer_length += count + 1;
727 msg += count + 1;
728 continue;
730 /* less than 9 bytes and no end byte - wait for more */
731 if (count < MAX_AKAI_SYSEX_LEN) {
732 ep->ports[0].active = 0;
733 return;
735 /* 9 bytes and no end marker in sight - malformed, skip it */
736 snd_rawmidi_transmit_ack(substream, count);
740 static struct usb_protocol_ops snd_usbmidi_akai_ops = {
741 .input = snd_usbmidi_akai_input,
742 .output = snd_usbmidi_akai_output,
746 * Novation USB MIDI protocol: number of data bytes is in the first byte
747 * (when receiving) (+1!) or in the second byte (when sending); data begins
748 * at the third byte.
751 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
752 uint8_t* buffer, int buffer_length)
754 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
755 return;
756 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
759 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
760 struct urb *urb)
762 uint8_t* transfer_buffer;
763 int count;
765 if (!ep->ports[0].active)
766 return;
767 transfer_buffer = urb->transfer_buffer;
768 count = snd_rawmidi_transmit(ep->ports[0].substream,
769 &transfer_buffer[2],
770 ep->max_transfer - 2);
771 if (count < 1) {
772 ep->ports[0].active = 0;
773 return;
775 transfer_buffer[0] = 0;
776 transfer_buffer[1] = count;
777 urb->transfer_buffer_length = 2 + count;
780 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
781 .input = snd_usbmidi_novation_input,
782 .output = snd_usbmidi_novation_output,
786 * "raw" protocol: used by the MOTU FastLane.
789 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
790 uint8_t* buffer, int buffer_length)
792 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
795 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
796 struct urb *urb)
798 int count;
800 if (!ep->ports[0].active)
801 return;
802 count = snd_rawmidi_transmit(ep->ports[0].substream,
803 urb->transfer_buffer,
804 ep->max_transfer);
805 if (count < 1) {
806 ep->ports[0].active = 0;
807 return;
809 urb->transfer_buffer_length = count;
812 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
813 .input = snd_usbmidi_raw_input,
814 .output = snd_usbmidi_raw_output,
817 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
818 uint8_t *buffer, int buffer_length)
820 if (buffer_length != 9)
821 return;
822 buffer_length = 8;
823 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
824 buffer_length--;
825 if (buffer_length)
826 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
829 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
830 struct urb *urb)
832 int count;
834 if (!ep->ports[0].active)
835 return;
836 count = snd_usb_get_speed(ep->umidi->dev) == USB_SPEED_HIGH ? 1 : 2;
837 count = snd_rawmidi_transmit(ep->ports[0].substream,
838 urb->transfer_buffer,
839 count);
840 if (count < 1) {
841 ep->ports[0].active = 0;
842 return;
845 memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
846 urb->transfer_buffer_length = ep->max_transfer;
849 static struct usb_protocol_ops snd_usbmidi_122l_ops = {
850 .input = snd_usbmidi_us122l_input,
851 .output = snd_usbmidi_us122l_output,
855 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
858 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
860 static const u8 init_data[] = {
861 /* initialization magic: "get version" */
862 0xf0,
863 0x00, 0x20, 0x31, /* Emagic */
864 0x64, /* Unitor8 */
865 0x0b, /* version number request */
866 0x00, /* command version */
867 0x00, /* EEPROM, box 0 */
868 0xf7
870 send_bulk_static_data(ep, init_data, sizeof(init_data));
871 /* while we're at it, pour on more magic */
872 send_bulk_static_data(ep, init_data, sizeof(init_data));
875 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
877 static const u8 finish_data[] = {
878 /* switch to patch mode with last preset */
879 0xf0,
880 0x00, 0x20, 0x31, /* Emagic */
881 0x64, /* Unitor8 */
882 0x10, /* patch switch command */
883 0x00, /* command version */
884 0x7f, /* to all boxes */
885 0x40, /* last preset in EEPROM */
886 0xf7
888 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
891 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
892 uint8_t* buffer, int buffer_length)
894 int i;
896 /* FF indicates end of valid data */
897 for (i = 0; i < buffer_length; ++i)
898 if (buffer[i] == 0xff) {
899 buffer_length = i;
900 break;
903 /* handle F5 at end of last buffer */
904 if (ep->seen_f5)
905 goto switch_port;
907 while (buffer_length > 0) {
908 /* determine size of data until next F5 */
909 for (i = 0; i < buffer_length; ++i)
910 if (buffer[i] == 0xf5)
911 break;
912 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
913 buffer += i;
914 buffer_length -= i;
916 if (buffer_length <= 0)
917 break;
918 /* assert(buffer[0] == 0xf5); */
919 ep->seen_f5 = 1;
920 ++buffer;
921 --buffer_length;
923 switch_port:
924 if (buffer_length <= 0)
925 break;
926 if (buffer[0] < 0x80) {
927 ep->current_port = (buffer[0] - 1) & 15;
928 ++buffer;
929 --buffer_length;
931 ep->seen_f5 = 0;
935 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
936 struct urb *urb)
938 int port0 = ep->current_port;
939 uint8_t* buf = urb->transfer_buffer;
940 int buf_free = ep->max_transfer;
941 int length, i;
943 for (i = 0; i < 0x10; ++i) {
944 /* round-robin, starting at the last current port */
945 int portnum = (port0 + i) & 15;
946 struct usbmidi_out_port* port = &ep->ports[portnum];
948 if (!port->active)
949 continue;
950 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
951 port->active = 0;
952 continue;
955 if (portnum != ep->current_port) {
956 if (buf_free < 2)
957 break;
958 ep->current_port = portnum;
959 buf[0] = 0xf5;
960 buf[1] = (portnum + 1) & 15;
961 buf += 2;
962 buf_free -= 2;
965 if (buf_free < 1)
966 break;
967 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
968 if (length > 0) {
969 buf += length;
970 buf_free -= length;
971 if (buf_free < 1)
972 break;
975 if (buf_free < ep->max_transfer && buf_free > 0) {
976 *buf = 0xff;
977 --buf_free;
979 urb->transfer_buffer_length = ep->max_transfer - buf_free;
982 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
983 .input = snd_usbmidi_emagic_input,
984 .output = snd_usbmidi_emagic_output,
985 .init_out_endpoint = snd_usbmidi_emagic_init_out,
986 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
990 static void update_roland_altsetting(struct snd_usb_midi* umidi)
992 struct usb_interface *intf;
993 struct usb_host_interface *hostif;
994 struct usb_interface_descriptor *intfd;
995 int is_light_load;
997 intf = umidi->iface;
998 is_light_load = intf->cur_altsetting != intf->altsetting;
999 if (umidi->roland_load_ctl->private_value == is_light_load)
1000 return;
1001 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
1002 intfd = get_iface_desc(hostif);
1003 snd_usbmidi_input_stop(&umidi->list);
1004 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1005 intfd->bAlternateSetting);
1006 snd_usbmidi_input_start(&umidi->list);
1009 static void substream_open(struct snd_rawmidi_substream *substream, int open)
1011 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1012 struct snd_kcontrol *ctl;
1014 mutex_lock(&umidi->mutex);
1015 if (open) {
1016 if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
1017 ctl = umidi->roland_load_ctl;
1018 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1019 snd_ctl_notify(umidi->card,
1020 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1021 update_roland_altsetting(umidi);
1023 } else {
1024 if (--umidi->opened == 0 && umidi->roland_load_ctl) {
1025 ctl = umidi->roland_load_ctl;
1026 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1027 snd_ctl_notify(umidi->card,
1028 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1031 mutex_unlock(&umidi->mutex);
1034 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
1036 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1037 struct usbmidi_out_port* port = NULL;
1038 int i, j;
1040 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1041 if (umidi->endpoints[i].out)
1042 for (j = 0; j < 0x10; ++j)
1043 if (umidi->endpoints[i].out->ports[j].substream == substream) {
1044 port = &umidi->endpoints[i].out->ports[j];
1045 break;
1047 if (!port) {
1048 snd_BUG();
1049 return -ENXIO;
1051 substream->runtime->private_data = port;
1052 port->state = STATE_UNKNOWN;
1053 substream_open(substream, 1);
1054 return 0;
1057 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
1059 substream_open(substream, 0);
1060 return 0;
1063 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1065 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
1067 port->active = up;
1068 if (up) {
1069 if (port->ep->umidi->disconnected) {
1070 /* gobble up remaining bytes to prevent wait in
1071 * snd_rawmidi_drain_output */
1072 while (!snd_rawmidi_transmit_empty(substream))
1073 snd_rawmidi_transmit_ack(substream, 1);
1074 return;
1076 tasklet_schedule(&port->ep->tasklet);
1080 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
1082 struct usbmidi_out_port* port = substream->runtime->private_data;
1083 struct snd_usb_midi_out_endpoint *ep = port->ep;
1084 unsigned int drain_urbs;
1085 DEFINE_WAIT(wait);
1086 long timeout = msecs_to_jiffies(50);
1088 if (ep->umidi->disconnected)
1089 return;
1091 * The substream buffer is empty, but some data might still be in the
1092 * currently active URBs, so we have to wait for those to complete.
1094 spin_lock_irq(&ep->buffer_lock);
1095 drain_urbs = ep->active_urbs;
1096 if (drain_urbs) {
1097 ep->drain_urbs |= drain_urbs;
1098 do {
1099 prepare_to_wait(&ep->drain_wait, &wait,
1100 TASK_UNINTERRUPTIBLE);
1101 spin_unlock_irq(&ep->buffer_lock);
1102 timeout = schedule_timeout(timeout);
1103 spin_lock_irq(&ep->buffer_lock);
1104 drain_urbs &= ep->drain_urbs;
1105 } while (drain_urbs && timeout);
1106 finish_wait(&ep->drain_wait, &wait);
1108 spin_unlock_irq(&ep->buffer_lock);
1111 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
1113 substream_open(substream, 1);
1114 return 0;
1117 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
1119 substream_open(substream, 0);
1120 return 0;
1123 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1125 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1127 if (up)
1128 set_bit(substream->number, &umidi->input_triggered);
1129 else
1130 clear_bit(substream->number, &umidi->input_triggered);
1133 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
1134 .open = snd_usbmidi_output_open,
1135 .close = snd_usbmidi_output_close,
1136 .trigger = snd_usbmidi_output_trigger,
1137 .drain = snd_usbmidi_output_drain,
1140 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
1141 .open = snd_usbmidi_input_open,
1142 .close = snd_usbmidi_input_close,
1143 .trigger = snd_usbmidi_input_trigger
1146 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1147 unsigned int buffer_length)
1149 usb_free_coherent(umidi->dev, buffer_length,
1150 urb->transfer_buffer, urb->transfer_dma);
1151 usb_free_urb(urb);
1155 * Frees an input endpoint.
1156 * May be called when ep hasn't been initialized completely.
1158 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
1160 unsigned int i;
1162 for (i = 0; i < INPUT_URBS; ++i)
1163 if (ep->urbs[i])
1164 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1165 ep->urbs[i]->transfer_buffer_length);
1166 kfree(ep);
1170 * Creates an input endpoint.
1172 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1173 struct snd_usb_midi_endpoint_info* ep_info,
1174 struct snd_usb_midi_endpoint* rep)
1176 struct snd_usb_midi_in_endpoint* ep;
1177 void* buffer;
1178 unsigned int pipe;
1179 int length;
1180 unsigned int i;
1182 rep->in = NULL;
1183 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1184 if (!ep)
1185 return -ENOMEM;
1186 ep->umidi = umidi;
1188 for (i = 0; i < INPUT_URBS; ++i) {
1189 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1190 if (!ep->urbs[i]) {
1191 snd_usbmidi_in_endpoint_delete(ep);
1192 return -ENOMEM;
1195 if (ep_info->in_interval)
1196 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
1197 else
1198 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1199 length = usb_maxpacket(umidi->dev, pipe, 0);
1200 for (i = 0; i < INPUT_URBS; ++i) {
1201 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1202 &ep->urbs[i]->transfer_dma);
1203 if (!buffer) {
1204 snd_usbmidi_in_endpoint_delete(ep);
1205 return -ENOMEM;
1207 if (ep_info->in_interval)
1208 usb_fill_int_urb(ep->urbs[i], umidi->dev,
1209 pipe, buffer, length,
1210 snd_usbmidi_in_urb_complete,
1211 ep, ep_info->in_interval);
1212 else
1213 usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
1214 pipe, buffer, length,
1215 snd_usbmidi_in_urb_complete, ep);
1216 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1219 rep->in = ep;
1220 return 0;
1224 * Frees an output endpoint.
1225 * May be called when ep hasn't been initialized completely.
1227 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
1229 unsigned int i;
1231 for (i = 0; i < OUTPUT_URBS; ++i)
1232 if (ep->urbs[i].urb) {
1233 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1234 ep->max_transfer);
1235 ep->urbs[i].urb = NULL;
1239 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1241 snd_usbmidi_out_endpoint_clear(ep);
1242 kfree(ep);
1246 * Creates an output endpoint, and initializes output ports.
1248 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1249 struct snd_usb_midi_endpoint_info* ep_info,
1250 struct snd_usb_midi_endpoint* rep)
1252 struct snd_usb_midi_out_endpoint* ep;
1253 unsigned int i;
1254 unsigned int pipe;
1255 void* buffer;
1257 rep->out = NULL;
1258 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1259 if (!ep)
1260 return -ENOMEM;
1261 ep->umidi = umidi;
1263 for (i = 0; i < OUTPUT_URBS; ++i) {
1264 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1265 if (!ep->urbs[i].urb) {
1266 snd_usbmidi_out_endpoint_delete(ep);
1267 return -ENOMEM;
1269 ep->urbs[i].ep = ep;
1271 if (ep_info->out_interval)
1272 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
1273 else
1274 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
1275 switch (umidi->usb_id) {
1276 default:
1277 ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
1278 break;
1280 * Various chips declare a packet size larger than 4 bytes, but
1281 * do not actually work with larger packets:
1283 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1284 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1285 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1286 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1287 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1288 ep->max_transfer = 4;
1289 break;
1291 * Some devices only work with 9 bytes packet size:
1293 case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
1294 case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
1295 ep->max_transfer = 9;
1296 break;
1298 for (i = 0; i < OUTPUT_URBS; ++i) {
1299 buffer = usb_alloc_coherent(umidi->dev,
1300 ep->max_transfer, GFP_KERNEL,
1301 &ep->urbs[i].urb->transfer_dma);
1302 if (!buffer) {
1303 snd_usbmidi_out_endpoint_delete(ep);
1304 return -ENOMEM;
1306 if (ep_info->out_interval)
1307 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
1308 pipe, buffer, ep->max_transfer,
1309 snd_usbmidi_out_urb_complete,
1310 &ep->urbs[i], ep_info->out_interval);
1311 else
1312 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
1313 pipe, buffer, ep->max_transfer,
1314 snd_usbmidi_out_urb_complete,
1315 &ep->urbs[i]);
1316 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1319 spin_lock_init(&ep->buffer_lock);
1320 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1321 init_waitqueue_head(&ep->drain_wait);
1323 for (i = 0; i < 0x10; ++i)
1324 if (ep_info->out_cables & (1 << i)) {
1325 ep->ports[i].ep = ep;
1326 ep->ports[i].cable = i << 4;
1329 if (umidi->usb_protocol_ops->init_out_endpoint)
1330 umidi->usb_protocol_ops->init_out_endpoint(ep);
1332 rep->out = ep;
1333 return 0;
1337 * Frees everything.
1339 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1341 int i;
1343 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1344 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1345 if (ep->out)
1346 snd_usbmidi_out_endpoint_delete(ep->out);
1347 if (ep->in)
1348 snd_usbmidi_in_endpoint_delete(ep->in);
1350 mutex_destroy(&umidi->mutex);
1351 kfree(umidi);
1355 * Unlinks all URBs (must be done before the usb_device is deleted).
1357 void snd_usbmidi_disconnect(struct list_head* p)
1359 struct snd_usb_midi* umidi;
1360 unsigned int i, j;
1362 umidi = list_entry(p, struct snd_usb_midi, list);
1364 * an URB's completion handler may start the timer and
1365 * a timer may submit an URB. To reliably break the cycle
1366 * a flag under lock must be used
1368 spin_lock_irq(&umidi->disc_lock);
1369 umidi->disconnected = 1;
1370 spin_unlock_irq(&umidi->disc_lock);
1371 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1372 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1373 if (ep->out)
1374 tasklet_kill(&ep->out->tasklet);
1375 if (ep->out) {
1376 for (j = 0; j < OUTPUT_URBS; ++j)
1377 usb_kill_urb(ep->out->urbs[j].urb);
1378 if (umidi->usb_protocol_ops->finish_out_endpoint)
1379 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1380 ep->out->active_urbs = 0;
1381 if (ep->out->drain_urbs) {
1382 ep->out->drain_urbs = 0;
1383 wake_up(&ep->out->drain_wait);
1386 if (ep->in)
1387 for (j = 0; j < INPUT_URBS; ++j)
1388 usb_kill_urb(ep->in->urbs[j]);
1389 /* free endpoints here; later call can result in Oops */
1390 if (ep->out)
1391 snd_usbmidi_out_endpoint_clear(ep->out);
1392 if (ep->in) {
1393 snd_usbmidi_in_endpoint_delete(ep->in);
1394 ep->in = NULL;
1397 del_timer_sync(&umidi->error_timer);
1400 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1402 struct snd_usb_midi* umidi = rmidi->private_data;
1403 snd_usbmidi_free(umidi);
1406 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1407 int stream, int number)
1409 struct list_head* list;
1411 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1412 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1413 if (substream->number == number)
1414 return substream;
1416 return NULL;
1420 * This list specifies names for ports that do not fit into the standard
1421 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1422 * such as internal control or synthesizer ports.
1424 static struct port_info {
1425 u32 id;
1426 short int port;
1427 short int voices;
1428 const char *name;
1429 unsigned int seq_flags;
1430 } snd_usbmidi_port_info[] = {
1431 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1432 { .id = USB_ID(vendor, product), \
1433 .port = num, .voices = voices_, \
1434 .name = name_, .seq_flags = flags }
1435 #define EXTERNAL_PORT(vendor, product, num, name) \
1436 PORT_INFO(vendor, product, num, name, 0, \
1437 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1438 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1439 SNDRV_SEQ_PORT_TYPE_PORT)
1440 #define CONTROL_PORT(vendor, product, num, name) \
1441 PORT_INFO(vendor, product, num, name, 0, \
1442 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1443 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1444 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1445 PORT_INFO(vendor, product, num, name, voices, \
1446 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1447 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1448 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1449 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1450 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1451 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1452 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1453 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1454 PORT_INFO(vendor, product, num, name, voices, \
1455 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1456 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1457 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1458 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1459 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1460 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1461 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1462 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1463 /* Roland UA-100 */
1464 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1465 /* Roland SC-8850 */
1466 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1467 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1468 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1469 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1470 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1471 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1472 /* Roland U-8 */
1473 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1474 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1475 /* Roland SC-8820 */
1476 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1477 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1478 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1479 /* Roland SK-500 */
1480 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1481 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1482 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1483 /* Roland SC-D70 */
1484 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1485 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1486 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1487 /* Edirol UM-880 */
1488 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1489 /* Edirol SD-90 */
1490 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1491 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1492 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1493 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1494 /* Edirol UM-550 */
1495 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1496 /* Edirol SD-20 */
1497 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1498 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1499 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1500 /* Edirol SD-80 */
1501 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1502 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1503 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1504 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1505 /* Edirol UA-700 */
1506 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1507 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1508 /* Roland VariOS */
1509 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1510 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1511 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1512 /* Edirol PCR */
1513 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1514 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1515 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1516 /* BOSS GS-10 */
1517 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1518 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1519 /* Edirol UA-1000 */
1520 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1521 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1522 /* Edirol UR-80 */
1523 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1524 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1525 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1526 /* Edirol PCR-A */
1527 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1528 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1529 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1530 /* Edirol UM-3EX */
1531 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1532 /* M-Audio MidiSport 8x8 */
1533 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1534 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1535 /* MOTU Fastlane */
1536 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1537 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1538 /* Emagic Unitor8/AMT8/MT4 */
1539 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1540 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1541 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1542 /* Akai MPD16 */
1543 CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1544 PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1545 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1546 SNDRV_SEQ_PORT_TYPE_HARDWARE),
1547 /* Access Music Virus TI */
1548 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1549 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1550 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1551 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1552 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
1555 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1557 int i;
1559 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1560 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
1561 snd_usbmidi_port_info[i].port == number)
1562 return &snd_usbmidi_port_info[i];
1564 return NULL;
1567 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1568 struct snd_seq_port_info *seq_port_info)
1570 struct snd_usb_midi *umidi = rmidi->private_data;
1571 struct port_info *port_info;
1573 /* TODO: read port flags from descriptors */
1574 port_info = find_port_info(umidi, number);
1575 if (port_info) {
1576 seq_port_info->type = port_info->seq_flags;
1577 seq_port_info->midi_voices = port_info->voices;
1581 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1582 int stream, int number,
1583 struct snd_rawmidi_substream ** rsubstream)
1585 struct port_info *port_info;
1586 const char *name_format;
1588 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1589 if (!substream) {
1590 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1591 return;
1594 /* TODO: read port name from jack descriptor */
1595 port_info = find_port_info(umidi, number);
1596 name_format = port_info ? port_info->name : "%s MIDI %d";
1597 snprintf(substream->name, sizeof(substream->name),
1598 name_format, umidi->card->shortname, number + 1);
1600 *rsubstream = substream;
1604 * Creates the endpoints and their ports.
1606 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1607 struct snd_usb_midi_endpoint_info* endpoints)
1609 int i, j, err;
1610 int out_ports = 0, in_ports = 0;
1612 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1613 if (endpoints[i].out_cables) {
1614 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1615 &umidi->endpoints[i]);
1616 if (err < 0)
1617 return err;
1619 if (endpoints[i].in_cables) {
1620 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1621 &umidi->endpoints[i]);
1622 if (err < 0)
1623 return err;
1626 for (j = 0; j < 0x10; ++j) {
1627 if (endpoints[i].out_cables & (1 << j)) {
1628 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1629 &umidi->endpoints[i].out->ports[j].substream);
1630 ++out_ports;
1632 if (endpoints[i].in_cables & (1 << j)) {
1633 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1634 &umidi->endpoints[i].in->ports[j].substream);
1635 ++in_ports;
1639 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1640 out_ports, in_ports);
1641 return 0;
1645 * Returns MIDIStreaming device capabilities.
1647 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1648 struct snd_usb_midi_endpoint_info* endpoints)
1650 struct usb_interface* intf;
1651 struct usb_host_interface *hostif;
1652 struct usb_interface_descriptor* intfd;
1653 struct usb_ms_header_descriptor* ms_header;
1654 struct usb_host_endpoint *hostep;
1655 struct usb_endpoint_descriptor* ep;
1656 struct usb_ms_endpoint_descriptor* ms_ep;
1657 int i, epidx;
1659 intf = umidi->iface;
1660 if (!intf)
1661 return -ENXIO;
1662 hostif = &intf->altsetting[0];
1663 intfd = get_iface_desc(hostif);
1664 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1665 if (hostif->extralen >= 7 &&
1666 ms_header->bLength >= 7 &&
1667 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1668 ms_header->bDescriptorSubtype == UAC_HEADER)
1669 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1670 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1671 else
1672 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1674 epidx = 0;
1675 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1676 hostep = &hostif->endpoint[i];
1677 ep = get_ep_desc(hostep);
1678 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
1679 continue;
1680 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1681 if (hostep->extralen < 4 ||
1682 ms_ep->bLength < 4 ||
1683 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1684 ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
1685 continue;
1686 if (usb_endpoint_dir_out(ep)) {
1687 if (endpoints[epidx].out_ep) {
1688 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1689 snd_printk(KERN_WARNING "too many endpoints\n");
1690 break;
1693 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1694 if (usb_endpoint_xfer_int(ep))
1695 endpoints[epidx].out_interval = ep->bInterval;
1696 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1698 * Low speed bulk transfers don't exist, so
1699 * force interrupt transfers for devices like
1700 * ESI MIDI Mate that try to use them anyway.
1702 endpoints[epidx].out_interval = 1;
1703 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1704 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1705 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1706 } else {
1707 if (endpoints[epidx].in_ep) {
1708 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1709 snd_printk(KERN_WARNING "too many endpoints\n");
1710 break;
1713 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1714 if (usb_endpoint_xfer_int(ep))
1715 endpoints[epidx].in_interval = ep->bInterval;
1716 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1717 endpoints[epidx].in_interval = 1;
1718 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1719 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1720 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1723 return 0;
1726 static int roland_load_info(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_info *info)
1729 static const char *const names[] = { "High Load", "Light Load" };
1731 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1732 info->count = 1;
1733 info->value.enumerated.items = 2;
1734 if (info->value.enumerated.item > 1)
1735 info->value.enumerated.item = 1;
1736 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
1737 return 0;
1740 static int roland_load_get(struct snd_kcontrol *kcontrol,
1741 struct snd_ctl_elem_value *value)
1743 value->value.enumerated.item[0] = kcontrol->private_value;
1744 return 0;
1747 static int roland_load_put(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *value)
1750 struct snd_usb_midi* umidi = kcontrol->private_data;
1751 int changed;
1753 if (value->value.enumerated.item[0] > 1)
1754 return -EINVAL;
1755 mutex_lock(&umidi->mutex);
1756 changed = value->value.enumerated.item[0] != kcontrol->private_value;
1757 if (changed)
1758 kcontrol->private_value = value->value.enumerated.item[0];
1759 mutex_unlock(&umidi->mutex);
1760 return changed;
1763 static struct snd_kcontrol_new roland_load_ctl = {
1764 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1765 .name = "MIDI Input Mode",
1766 .info = roland_load_info,
1767 .get = roland_load_get,
1768 .put = roland_load_put,
1769 .private_value = 1,
1773 * On Roland devices, use the second alternate setting to be able to use
1774 * the interrupt input endpoint.
1776 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1778 struct usb_interface* intf;
1779 struct usb_host_interface *hostif;
1780 struct usb_interface_descriptor* intfd;
1782 intf = umidi->iface;
1783 if (!intf || intf->num_altsetting != 2)
1784 return;
1786 hostif = &intf->altsetting[1];
1787 intfd = get_iface_desc(hostif);
1788 if (intfd->bNumEndpoints != 2 ||
1789 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1790 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1791 return;
1793 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1794 intfd->bAlternateSetting);
1795 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1796 intfd->bAlternateSetting);
1798 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
1799 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
1800 umidi->roland_load_ctl = NULL;
1804 * Try to find any usable endpoints in the interface.
1806 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1807 struct snd_usb_midi_endpoint_info* endpoint,
1808 int max_endpoints)
1810 struct usb_interface* intf;
1811 struct usb_host_interface *hostif;
1812 struct usb_interface_descriptor* intfd;
1813 struct usb_endpoint_descriptor* epd;
1814 int i, out_eps = 0, in_eps = 0;
1816 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
1817 snd_usbmidi_switch_roland_altsetting(umidi);
1819 if (endpoint[0].out_ep || endpoint[0].in_ep)
1820 return 0;
1822 intf = umidi->iface;
1823 if (!intf || intf->num_altsetting < 1)
1824 return -ENOENT;
1825 hostif = intf->cur_altsetting;
1826 intfd = get_iface_desc(hostif);
1828 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1829 epd = get_endpoint(hostif, i);
1830 if (!usb_endpoint_xfer_bulk(epd) &&
1831 !usb_endpoint_xfer_int(epd))
1832 continue;
1833 if (out_eps < max_endpoints &&
1834 usb_endpoint_dir_out(epd)) {
1835 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1836 if (usb_endpoint_xfer_int(epd))
1837 endpoint[out_eps].out_interval = epd->bInterval;
1838 ++out_eps;
1840 if (in_eps < max_endpoints &&
1841 usb_endpoint_dir_in(epd)) {
1842 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1843 if (usb_endpoint_xfer_int(epd))
1844 endpoint[in_eps].in_interval = epd->bInterval;
1845 ++in_eps;
1848 return (out_eps || in_eps) ? 0 : -ENOENT;
1852 * Detects the endpoints for one-port-per-endpoint protocols.
1854 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1855 struct snd_usb_midi_endpoint_info* endpoints)
1857 int err, i;
1859 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1860 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1861 if (endpoints[i].out_ep)
1862 endpoints[i].out_cables = 0x0001;
1863 if (endpoints[i].in_ep)
1864 endpoints[i].in_cables = 0x0001;
1866 return err;
1870 * Detects the endpoints and ports of Yamaha devices.
1872 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1873 struct snd_usb_midi_endpoint_info* endpoint)
1875 struct usb_interface* intf;
1876 struct usb_host_interface *hostif;
1877 struct usb_interface_descriptor* intfd;
1878 uint8_t* cs_desc;
1880 intf = umidi->iface;
1881 if (!intf)
1882 return -ENOENT;
1883 hostif = intf->altsetting;
1884 intfd = get_iface_desc(hostif);
1885 if (intfd->bNumEndpoints < 1)
1886 return -ENOENT;
1889 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1890 * necessarily with any useful contents. So simply count 'em.
1892 for (cs_desc = hostif->extra;
1893 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1894 cs_desc += cs_desc[0]) {
1895 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1896 if (cs_desc[2] == UAC_MIDI_IN_JACK)
1897 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1898 else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
1899 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1902 if (!endpoint->in_cables && !endpoint->out_cables)
1903 return -ENOENT;
1905 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1909 * Creates the endpoints and their ports for Midiman devices.
1911 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1912 struct snd_usb_midi_endpoint_info* endpoint)
1914 struct snd_usb_midi_endpoint_info ep_info;
1915 struct usb_interface* intf;
1916 struct usb_host_interface *hostif;
1917 struct usb_interface_descriptor* intfd;
1918 struct usb_endpoint_descriptor* epd;
1919 int cable, err;
1921 intf = umidi->iface;
1922 if (!intf)
1923 return -ENOENT;
1924 hostif = intf->altsetting;
1925 intfd = get_iface_desc(hostif);
1927 * The various MidiSport devices have more or less random endpoint
1928 * numbers, so we have to identify the endpoints by their index in
1929 * the descriptor array, like the driver for that other OS does.
1931 * There is one interrupt input endpoint for all input ports, one
1932 * bulk output endpoint for even-numbered ports, and one for odd-
1933 * numbered ports. Both bulk output endpoints have corresponding
1934 * input bulk endpoints (at indices 1 and 3) which aren't used.
1936 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1937 snd_printdd(KERN_ERR "not enough endpoints\n");
1938 return -ENOENT;
1941 epd = get_endpoint(hostif, 0);
1942 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
1943 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1944 return -ENXIO;
1946 epd = get_endpoint(hostif, 2);
1947 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
1948 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1949 return -ENXIO;
1951 if (endpoint->out_cables > 0x0001) {
1952 epd = get_endpoint(hostif, 4);
1953 if (!usb_endpoint_dir_out(epd) ||
1954 !usb_endpoint_xfer_bulk(epd)) {
1955 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1956 return -ENXIO;
1960 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1961 ep_info.out_interval = 0;
1962 ep_info.out_cables = endpoint->out_cables & 0x5555;
1963 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1964 if (err < 0)
1965 return err;
1967 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1968 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1969 ep_info.in_cables = endpoint->in_cables;
1970 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1971 if (err < 0)
1972 return err;
1974 if (endpoint->out_cables > 0x0001) {
1975 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1976 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1977 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1978 if (err < 0)
1979 return err;
1982 for (cable = 0; cable < 0x10; ++cable) {
1983 if (endpoint->out_cables & (1 << cable))
1984 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1985 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1986 if (endpoint->in_cables & (1 << cable))
1987 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1988 &umidi->endpoints[0].in->ports[cable].substream);
1990 return 0;
1993 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1994 .get_port_info = snd_usbmidi_get_port_info,
1997 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
1998 int out_ports, int in_ports)
2000 struct snd_rawmidi *rmidi;
2001 int err;
2003 err = snd_rawmidi_new(umidi->card, "USB MIDI",
2004 umidi->next_midi_device++,
2005 out_ports, in_ports, &rmidi);
2006 if (err < 0)
2007 return err;
2008 strcpy(rmidi->name, umidi->card->shortname);
2009 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2010 SNDRV_RAWMIDI_INFO_INPUT |
2011 SNDRV_RAWMIDI_INFO_DUPLEX;
2012 rmidi->ops = &snd_usbmidi_ops;
2013 rmidi->private_data = umidi;
2014 rmidi->private_free = snd_usbmidi_rawmidi_free;
2015 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
2016 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
2018 umidi->rmidi = rmidi;
2019 return 0;
2023 * Temporarily stop input.
2025 void snd_usbmidi_input_stop(struct list_head* p)
2027 struct snd_usb_midi* umidi;
2028 unsigned int i, j;
2030 umidi = list_entry(p, struct snd_usb_midi, list);
2031 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2032 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
2033 if (ep->in)
2034 for (j = 0; j < INPUT_URBS; ++j)
2035 usb_kill_urb(ep->in->urbs[j]);
2039 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
2041 unsigned int i;
2043 if (!ep)
2044 return;
2045 for (i = 0; i < INPUT_URBS; ++i) {
2046 struct urb* urb = ep->urbs[i];
2047 urb->dev = ep->umidi->dev;
2048 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
2053 * Resume input after a call to snd_usbmidi_input_stop().
2055 void snd_usbmidi_input_start(struct list_head* p)
2057 struct snd_usb_midi* umidi;
2058 int i;
2060 umidi = list_entry(p, struct snd_usb_midi, list);
2061 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2062 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2066 * Creates and registers everything needed for a MIDI streaming interface.
2068 int snd_usbmidi_create(struct snd_card *card,
2069 struct usb_interface* iface,
2070 struct list_head *midi_list,
2071 const struct snd_usb_audio_quirk* quirk)
2073 struct snd_usb_midi* umidi;
2074 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
2075 int out_ports, in_ports;
2076 int i, err;
2078 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
2079 if (!umidi)
2080 return -ENOMEM;
2081 umidi->dev = interface_to_usbdev(iface);
2082 umidi->card = card;
2083 umidi->iface = iface;
2084 umidi->quirk = quirk;
2085 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
2086 init_timer(&umidi->error_timer);
2087 spin_lock_init(&umidi->disc_lock);
2088 mutex_init(&umidi->mutex);
2089 umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2090 le16_to_cpu(umidi->dev->descriptor.idProduct));
2091 umidi->error_timer.function = snd_usbmidi_error_timer;
2092 umidi->error_timer.data = (unsigned long)umidi;
2094 /* detect the endpoint(s) to use */
2095 memset(endpoints, 0, sizeof(endpoints));
2096 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2097 case QUIRK_MIDI_STANDARD_INTERFACE:
2098 err = snd_usbmidi_get_ms_info(umidi, endpoints);
2099 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
2100 umidi->usb_protocol_ops =
2101 &snd_usbmidi_maudio_broken_running_status_ops;
2102 break;
2103 case QUIRK_MIDI_US122L:
2104 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2105 /* fall through */
2106 case QUIRK_MIDI_FIXED_ENDPOINT:
2107 memcpy(&endpoints[0], quirk->data,
2108 sizeof(struct snd_usb_midi_endpoint_info));
2109 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2110 break;
2111 case QUIRK_MIDI_YAMAHA:
2112 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2113 break;
2114 case QUIRK_MIDI_MIDIMAN:
2115 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2116 memcpy(&endpoints[0], quirk->data,
2117 sizeof(struct snd_usb_midi_endpoint_info));
2118 err = 0;
2119 break;
2120 case QUIRK_MIDI_NOVATION:
2121 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2122 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2123 break;
2124 case QUIRK_MIDI_FASTLANE:
2125 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
2127 * Interface 1 contains isochronous endpoints, but with the same
2128 * numbers as in interface 0. Since it is interface 1 that the
2129 * USB core has most recently seen, these descriptors are now
2130 * associated with the endpoint numbers. This will foul up our
2131 * attempts to submit bulk/interrupt URBs to the endpoints in
2132 * interface 0, so we have to make sure that the USB core looks
2133 * again at interface 0 by calling usb_set_interface() on it.
2135 usb_set_interface(umidi->dev, 0, 0);
2136 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2137 break;
2138 case QUIRK_MIDI_EMAGIC:
2139 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2140 memcpy(&endpoints[0], quirk->data,
2141 sizeof(struct snd_usb_midi_endpoint_info));
2142 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2143 break;
2144 case QUIRK_MIDI_CME:
2145 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
2146 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2147 break;
2148 case QUIRK_MIDI_AKAI:
2149 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2150 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2151 /* endpoint 1 is input-only */
2152 endpoints[1].out_cables = 0;
2153 break;
2154 default:
2155 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2156 err = -ENXIO;
2157 break;
2159 if (err < 0) {
2160 kfree(umidi);
2161 return err;
2164 /* create rawmidi device */
2165 out_ports = 0;
2166 in_ports = 0;
2167 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2168 out_ports += hweight16(endpoints[i].out_cables);
2169 in_ports += hweight16(endpoints[i].in_cables);
2171 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2172 if (err < 0) {
2173 kfree(umidi);
2174 return err;
2177 /* create endpoint/port structures */
2178 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2179 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2180 else
2181 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2182 if (err < 0) {
2183 snd_usbmidi_free(umidi);
2184 return err;
2187 list_add_tail(&umidi->list, midi_list);
2189 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2190 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2191 return 0;
2194 EXPORT_SYMBOL(snd_usbmidi_create);
2195 EXPORT_SYMBOL(snd_usbmidi_input_stop);
2196 EXPORT_SYMBOL(snd_usbmidi_input_start);
2197 EXPORT_SYMBOL(snd_usbmidi_disconnect);