Input: db9 - fix potential buffer overrun
[linux-2.6/mini2440.git] / sound / usb / usbmidi.c
blob5105b6b057484ffb0408e509e90f70a45e3d799e
1 /*
2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2005 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 <sound/driver.h>
39 #include <linux/kernel.h>
40 #include <linux/types.h>
41 #include <linux/bitops.h>
42 #include <linux/interrupt.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/init.h>
46 #include <linux/slab.h>
47 #include <linux/timer.h>
48 #include <linux/usb.h>
49 #include <sound/core.h>
50 #include <sound/rawmidi.h>
51 #include <sound/asequencer.h>
52 #include "usbaudio.h"
56 * define this to log all USB packets
58 /* #define DUMP_PACKETS */
61 * how long to wait after some USB errors, so that khubd can disconnect() us
62 * without too many spurious errors
64 #define ERROR_DELAY_JIFFIES (HZ / 10)
67 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
68 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
69 MODULE_LICENSE("Dual BSD/GPL");
72 struct usb_ms_header_descriptor {
73 __u8 bLength;
74 __u8 bDescriptorType;
75 __u8 bDescriptorSubtype;
76 __u8 bcdMSC[2];
77 __le16 wTotalLength;
78 } __attribute__ ((packed));
80 struct usb_ms_endpoint_descriptor {
81 __u8 bLength;
82 __u8 bDescriptorType;
83 __u8 bDescriptorSubtype;
84 __u8 bNumEmbMIDIJack;
85 __u8 baAssocJackID[0];
86 } __attribute__ ((packed));
88 struct snd_usb_midi_in_endpoint;
89 struct snd_usb_midi_out_endpoint;
90 struct snd_usb_midi_endpoint;
92 struct usb_protocol_ops {
93 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
94 void (*output)(struct snd_usb_midi_out_endpoint*);
95 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
96 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
97 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
100 struct snd_usb_midi {
101 struct snd_usb_audio *chip;
102 struct usb_interface *iface;
103 const struct snd_usb_audio_quirk *quirk;
104 struct snd_rawmidi *rmidi;
105 struct usb_protocol_ops* usb_protocol_ops;
106 struct list_head list;
107 struct timer_list error_timer;
109 struct snd_usb_midi_endpoint {
110 struct snd_usb_midi_out_endpoint *out;
111 struct snd_usb_midi_in_endpoint *in;
112 } endpoints[MIDI_MAX_ENDPOINTS];
113 unsigned long input_triggered;
116 struct snd_usb_midi_out_endpoint {
117 struct snd_usb_midi* umidi;
118 struct urb* urb;
119 int urb_active;
120 int max_transfer; /* size of urb buffer */
121 struct tasklet_struct tasklet;
123 spinlock_t buffer_lock;
125 struct usbmidi_out_port {
126 struct snd_usb_midi_out_endpoint* ep;
127 struct snd_rawmidi_substream *substream;
128 int active;
129 uint8_t cable; /* cable number << 4 */
130 uint8_t state;
131 #define STATE_UNKNOWN 0
132 #define STATE_1PARAM 1
133 #define STATE_2PARAM_1 2
134 #define STATE_2PARAM_2 3
135 #define STATE_SYSEX_0 4
136 #define STATE_SYSEX_1 5
137 #define STATE_SYSEX_2 6
138 uint8_t data[2];
139 } ports[0x10];
140 int current_port;
143 struct snd_usb_midi_in_endpoint {
144 struct snd_usb_midi* umidi;
145 struct urb* urb;
146 struct usbmidi_in_port {
147 struct snd_rawmidi_substream *substream;
148 } ports[0x10];
149 u8 seen_f5;
150 u8 error_resubmit;
151 int current_port;
154 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
156 static const uint8_t snd_usbmidi_cin_length[] = {
157 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
161 * Submits the URB, with error handling.
163 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
165 int err = usb_submit_urb(urb, flags);
166 if (err < 0 && err != -ENODEV)
167 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
168 return err;
172 * Error handling for URB completion functions.
174 static int snd_usbmidi_urb_error(int status)
176 switch (status) {
177 /* manually unlinked, or device gone */
178 case -ENOENT:
179 case -ECONNRESET:
180 case -ESHUTDOWN:
181 case -ENODEV:
182 return -ENODEV;
183 /* errors that might occur during unplugging */
184 case -EPROTO: /* EHCI */
185 case -ETIMEDOUT: /* OHCI */
186 case -EILSEQ: /* UHCI */
187 return -EIO;
188 default:
189 snd_printk(KERN_ERR "urb status %d\n", status);
190 return 0; /* continue */
195 * Receives a chunk of MIDI data.
197 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
198 uint8_t* data, int length)
200 struct usbmidi_in_port* port = &ep->ports[portidx];
202 if (!port->substream) {
203 snd_printd("unexpected port %d!\n", portidx);
204 return;
206 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
207 return;
208 snd_rawmidi_receive(port->substream, data, length);
211 #ifdef DUMP_PACKETS
212 static void dump_urb(const char *type, const u8 *data, int length)
214 snd_printk(KERN_DEBUG "%s packet: [", type);
215 for (; length > 0; ++data, --length)
216 printk(" %02x", *data);
217 printk(" ]\n");
219 #else
220 #define dump_urb(type, data, length) /* nothing */
221 #endif
224 * Processes the data read from the device.
226 static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs)
228 struct snd_usb_midi_in_endpoint* ep = urb->context;
230 if (urb->status == 0) {
231 dump_urb("received", urb->transfer_buffer, urb->actual_length);
232 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
233 urb->actual_length);
234 } else {
235 int err = snd_usbmidi_urb_error(urb->status);
236 if (err < 0) {
237 if (err != -ENODEV) {
238 ep->error_resubmit = 1;
239 mod_timer(&ep->umidi->error_timer,
240 jiffies + ERROR_DELAY_JIFFIES);
242 return;
246 urb->dev = ep->umidi->chip->dev;
247 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
250 static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs)
252 struct snd_usb_midi_out_endpoint* ep = urb->context;
254 spin_lock(&ep->buffer_lock);
255 ep->urb_active = 0;
256 spin_unlock(&ep->buffer_lock);
257 if (urb->status < 0) {
258 int err = snd_usbmidi_urb_error(urb->status);
259 if (err < 0) {
260 if (err != -ENODEV)
261 mod_timer(&ep->umidi->error_timer,
262 jiffies + ERROR_DELAY_JIFFIES);
263 return;
266 snd_usbmidi_do_output(ep);
270 * This is called when some data should be transferred to the device
271 * (from one or more substreams).
273 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
275 struct urb* urb = ep->urb;
276 unsigned long flags;
278 spin_lock_irqsave(&ep->buffer_lock, flags);
279 if (ep->urb_active || ep->umidi->chip->shutdown) {
280 spin_unlock_irqrestore(&ep->buffer_lock, flags);
281 return;
284 urb->transfer_buffer_length = 0;
285 ep->umidi->usb_protocol_ops->output(ep);
287 if (urb->transfer_buffer_length > 0) {
288 dump_urb("sending", urb->transfer_buffer,
289 urb->transfer_buffer_length);
290 urb->dev = ep->umidi->chip->dev;
291 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
293 spin_unlock_irqrestore(&ep->buffer_lock, flags);
296 static void snd_usbmidi_out_tasklet(unsigned long data)
298 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
300 snd_usbmidi_do_output(ep);
303 /* called after transfers had been interrupted due to some USB error */
304 static void snd_usbmidi_error_timer(unsigned long data)
306 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
307 int i;
309 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
310 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
311 if (in && in->error_resubmit) {
312 in->error_resubmit = 0;
313 in->urb->dev = umidi->chip->dev;
314 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
316 if (umidi->endpoints[i].out)
317 snd_usbmidi_do_output(umidi->endpoints[i].out);
321 /* helper function to send static data that may not DMA-able */
322 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
323 const void *data, int len)
325 int err;
326 void *buf = kmalloc(len, GFP_KERNEL);
327 if (!buf)
328 return -ENOMEM;
329 memcpy(buf, data, len);
330 dump_urb("sending", buf, len);
331 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len,
332 NULL, 250);
333 kfree(buf);
334 return err;
338 * Standard USB MIDI protocol: see the spec.
339 * Midiman protocol: like the standard protocol, but the control byte is the
340 * fourth byte in each packet, and uses length instead of CIN.
343 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
344 uint8_t* buffer, int buffer_length)
346 int i;
348 for (i = 0; i + 3 < buffer_length; i += 4)
349 if (buffer[i] != 0) {
350 int cable = buffer[i] >> 4;
351 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
352 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
356 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
357 uint8_t* buffer, int buffer_length)
359 int i;
361 for (i = 0; i + 3 < buffer_length; i += 4)
362 if (buffer[i + 3] != 0) {
363 int port = buffer[i + 3] >> 4;
364 int length = buffer[i + 3] & 3;
365 snd_usbmidi_input_data(ep, port, &buffer[i], length);
370 * Adds one USB MIDI packet to the output buffer.
372 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
373 uint8_t p1, uint8_t p2, uint8_t p3)
376 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
377 buf[0] = p0;
378 buf[1] = p1;
379 buf[2] = p2;
380 buf[3] = p3;
381 urb->transfer_buffer_length += 4;
385 * Adds one Midiman packet to the output buffer.
387 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
388 uint8_t p1, uint8_t p2, uint8_t p3)
391 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
392 buf[0] = p1;
393 buf[1] = p2;
394 buf[2] = p3;
395 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
396 urb->transfer_buffer_length += 4;
400 * Converts MIDI commands to USB MIDI packets.
402 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
403 uint8_t b, struct urb* urb)
405 uint8_t p0 = port->cable;
406 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
407 port->ep->umidi->usb_protocol_ops->output_packet;
409 if (b >= 0xf8) {
410 output_packet(urb, p0 | 0x0f, b, 0, 0);
411 } else if (b >= 0xf0) {
412 switch (b) {
413 case 0xf0:
414 port->data[0] = b;
415 port->state = STATE_SYSEX_1;
416 break;
417 case 0xf1:
418 case 0xf3:
419 port->data[0] = b;
420 port->state = STATE_1PARAM;
421 break;
422 case 0xf2:
423 port->data[0] = b;
424 port->state = STATE_2PARAM_1;
425 break;
426 case 0xf4:
427 case 0xf5:
428 port->state = STATE_UNKNOWN;
429 break;
430 case 0xf6:
431 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
432 port->state = STATE_UNKNOWN;
433 break;
434 case 0xf7:
435 switch (port->state) {
436 case STATE_SYSEX_0:
437 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
438 break;
439 case STATE_SYSEX_1:
440 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
441 break;
442 case STATE_SYSEX_2:
443 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
444 break;
446 port->state = STATE_UNKNOWN;
447 break;
449 } else if (b >= 0x80) {
450 port->data[0] = b;
451 if (b >= 0xc0 && b <= 0xdf)
452 port->state = STATE_1PARAM;
453 else
454 port->state = STATE_2PARAM_1;
455 } else { /* b < 0x80 */
456 switch (port->state) {
457 case STATE_1PARAM:
458 if (port->data[0] < 0xf0) {
459 p0 |= port->data[0] >> 4;
460 } else {
461 p0 |= 0x02;
462 port->state = STATE_UNKNOWN;
464 output_packet(urb, p0, port->data[0], b, 0);
465 break;
466 case STATE_2PARAM_1:
467 port->data[1] = b;
468 port->state = STATE_2PARAM_2;
469 break;
470 case STATE_2PARAM_2:
471 if (port->data[0] < 0xf0) {
472 p0 |= port->data[0] >> 4;
473 port->state = STATE_2PARAM_1;
474 } else {
475 p0 |= 0x03;
476 port->state = STATE_UNKNOWN;
478 output_packet(urb, p0, port->data[0], port->data[1], b);
479 break;
480 case STATE_SYSEX_0:
481 port->data[0] = b;
482 port->state = STATE_SYSEX_1;
483 break;
484 case STATE_SYSEX_1:
485 port->data[1] = b;
486 port->state = STATE_SYSEX_2;
487 break;
488 case STATE_SYSEX_2:
489 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
490 port->state = STATE_SYSEX_0;
491 break;
496 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
498 struct urb* urb = ep->urb;
499 int p;
501 /* FIXME: lower-numbered ports can starve higher-numbered ports */
502 for (p = 0; p < 0x10; ++p) {
503 struct usbmidi_out_port* port = &ep->ports[p];
504 if (!port->active)
505 continue;
506 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
507 uint8_t b;
508 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
509 port->active = 0;
510 break;
512 snd_usbmidi_transmit_byte(port, b, urb);
517 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
518 .input = snd_usbmidi_standard_input,
519 .output = snd_usbmidi_standard_output,
520 .output_packet = snd_usbmidi_output_standard_packet,
523 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
524 .input = snd_usbmidi_midiman_input,
525 .output = snd_usbmidi_standard_output,
526 .output_packet = snd_usbmidi_output_midiman_packet,
530 * Novation USB MIDI protocol: number of data bytes is in the first byte
531 * (when receiving) (+1!) or in the second byte (when sending); data begins
532 * at the third byte.
535 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
536 uint8_t* buffer, int buffer_length)
538 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
539 return;
540 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
543 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
545 uint8_t* transfer_buffer;
546 int count;
548 if (!ep->ports[0].active)
549 return;
550 transfer_buffer = ep->urb->transfer_buffer;
551 count = snd_rawmidi_transmit(ep->ports[0].substream,
552 &transfer_buffer[2],
553 ep->max_transfer - 2);
554 if (count < 1) {
555 ep->ports[0].active = 0;
556 return;
558 transfer_buffer[0] = 0;
559 transfer_buffer[1] = count;
560 ep->urb->transfer_buffer_length = 2 + count;
563 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
564 .input = snd_usbmidi_novation_input,
565 .output = snd_usbmidi_novation_output,
569 * "raw" protocol: used by the MOTU FastLane.
572 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
573 uint8_t* buffer, int buffer_length)
575 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
578 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
580 int count;
582 if (!ep->ports[0].active)
583 return;
584 count = snd_rawmidi_transmit(ep->ports[0].substream,
585 ep->urb->transfer_buffer,
586 ep->max_transfer);
587 if (count < 1) {
588 ep->ports[0].active = 0;
589 return;
591 ep->urb->transfer_buffer_length = count;
594 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
595 .input = snd_usbmidi_raw_input,
596 .output = snd_usbmidi_raw_output,
600 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
603 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
605 static const u8 init_data[] = {
606 /* initialization magic: "get version" */
607 0xf0,
608 0x00, 0x20, 0x31, /* Emagic */
609 0x64, /* Unitor8 */
610 0x0b, /* version number request */
611 0x00, /* command version */
612 0x00, /* EEPROM, box 0 */
613 0xf7
615 send_bulk_static_data(ep, init_data, sizeof(init_data));
616 /* while we're at it, pour on more magic */
617 send_bulk_static_data(ep, init_data, sizeof(init_data));
620 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
622 static const u8 finish_data[] = {
623 /* switch to patch mode with last preset */
624 0xf0,
625 0x00, 0x20, 0x31, /* Emagic */
626 0x64, /* Unitor8 */
627 0x10, /* patch switch command */
628 0x00, /* command version */
629 0x7f, /* to all boxes */
630 0x40, /* last preset in EEPROM */
631 0xf7
633 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
636 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
637 uint8_t* buffer, int buffer_length)
639 int i;
641 /* FF indicates end of valid data */
642 for (i = 0; i < buffer_length; ++i)
643 if (buffer[i] == 0xff) {
644 buffer_length = i;
645 break;
648 /* handle F5 at end of last buffer */
649 if (ep->seen_f5)
650 goto switch_port;
652 while (buffer_length > 0) {
653 /* determine size of data until next F5 */
654 for (i = 0; i < buffer_length; ++i)
655 if (buffer[i] == 0xf5)
656 break;
657 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
658 buffer += i;
659 buffer_length -= i;
661 if (buffer_length <= 0)
662 break;
663 /* assert(buffer[0] == 0xf5); */
664 ep->seen_f5 = 1;
665 ++buffer;
666 --buffer_length;
668 switch_port:
669 if (buffer_length <= 0)
670 break;
671 if (buffer[0] < 0x80) {
672 ep->current_port = (buffer[0] - 1) & 15;
673 ++buffer;
674 --buffer_length;
676 ep->seen_f5 = 0;
680 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
682 int port0 = ep->current_port;
683 uint8_t* buf = ep->urb->transfer_buffer;
684 int buf_free = ep->max_transfer;
685 int length, i;
687 for (i = 0; i < 0x10; ++i) {
688 /* round-robin, starting at the last current port */
689 int portnum = (port0 + i) & 15;
690 struct usbmidi_out_port* port = &ep->ports[portnum];
692 if (!port->active)
693 continue;
694 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
695 port->active = 0;
696 continue;
699 if (portnum != ep->current_port) {
700 if (buf_free < 2)
701 break;
702 ep->current_port = portnum;
703 buf[0] = 0xf5;
704 buf[1] = (portnum + 1) & 15;
705 buf += 2;
706 buf_free -= 2;
709 if (buf_free < 1)
710 break;
711 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
712 if (length > 0) {
713 buf += length;
714 buf_free -= length;
715 if (buf_free < 1)
716 break;
719 if (buf_free < ep->max_transfer && buf_free > 0) {
720 *buf = 0xff;
721 --buf_free;
723 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
726 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
727 .input = snd_usbmidi_emagic_input,
728 .output = snd_usbmidi_emagic_output,
729 .init_out_endpoint = snd_usbmidi_emagic_init_out,
730 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
734 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
736 struct snd_usb_midi* umidi = substream->rmidi->private_data;
737 struct usbmidi_out_port* port = NULL;
738 int i, j;
740 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
741 if (umidi->endpoints[i].out)
742 for (j = 0; j < 0x10; ++j)
743 if (umidi->endpoints[i].out->ports[j].substream == substream) {
744 port = &umidi->endpoints[i].out->ports[j];
745 break;
747 if (!port) {
748 snd_BUG();
749 return -ENXIO;
751 substream->runtime->private_data = port;
752 port->state = STATE_UNKNOWN;
753 return 0;
756 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
758 return 0;
761 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
763 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
765 port->active = up;
766 if (up) {
767 if (port->ep->umidi->chip->shutdown) {
768 /* gobble up remaining bytes to prevent wait in
769 * snd_rawmidi_drain_output */
770 while (!snd_rawmidi_transmit_empty(substream))
771 snd_rawmidi_transmit_ack(substream, 1);
772 return;
774 tasklet_hi_schedule(&port->ep->tasklet);
778 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
780 return 0;
783 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
785 return 0;
788 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
790 struct snd_usb_midi* umidi = substream->rmidi->private_data;
792 if (up)
793 set_bit(substream->number, &umidi->input_triggered);
794 else
795 clear_bit(substream->number, &umidi->input_triggered);
798 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
799 .open = snd_usbmidi_output_open,
800 .close = snd_usbmidi_output_close,
801 .trigger = snd_usbmidi_output_trigger,
804 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
805 .open = snd_usbmidi_input_open,
806 .close = snd_usbmidi_input_close,
807 .trigger = snd_usbmidi_input_trigger
811 * Frees an input endpoint.
812 * May be called when ep hasn't been initialized completely.
814 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
816 if (ep->urb) {
817 usb_buffer_free(ep->umidi->chip->dev,
818 ep->urb->transfer_buffer_length,
819 ep->urb->transfer_buffer,
820 ep->urb->transfer_dma);
821 usb_free_urb(ep->urb);
823 kfree(ep);
827 * Creates an input endpoint.
829 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
830 struct snd_usb_midi_endpoint_info* ep_info,
831 struct snd_usb_midi_endpoint* rep)
833 struct snd_usb_midi_in_endpoint* ep;
834 void* buffer;
835 unsigned int pipe;
836 int length;
838 rep->in = NULL;
839 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
840 if (!ep)
841 return -ENOMEM;
842 ep->umidi = umidi;
844 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
845 if (!ep->urb) {
846 snd_usbmidi_in_endpoint_delete(ep);
847 return -ENOMEM;
849 if (ep_info->in_interval)
850 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
851 else
852 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
853 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
854 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
855 &ep->urb->transfer_dma);
856 if (!buffer) {
857 snd_usbmidi_in_endpoint_delete(ep);
858 return -ENOMEM;
860 if (ep_info->in_interval)
861 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
862 length, snd_usbmidi_in_urb_complete, ep,
863 ep_info->in_interval);
864 else
865 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
866 length, snd_usbmidi_in_urb_complete, ep);
867 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
869 rep->in = ep;
870 return 0;
873 static unsigned int snd_usbmidi_count_bits(unsigned int x)
875 unsigned int bits;
877 for (bits = 0; x; ++bits)
878 x &= x - 1;
879 return bits;
883 * Frees an output endpoint.
884 * May be called when ep hasn't been initialized completely.
886 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
888 if (ep->urb) {
889 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
890 ep->urb->transfer_buffer,
891 ep->urb->transfer_dma);
892 usb_free_urb(ep->urb);
894 kfree(ep);
898 * Creates an output endpoint, and initializes output ports.
900 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
901 struct snd_usb_midi_endpoint_info* ep_info,
902 struct snd_usb_midi_endpoint* rep)
904 struct snd_usb_midi_out_endpoint* ep;
905 int i;
906 unsigned int pipe;
907 void* buffer;
909 rep->out = NULL;
910 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
911 if (!ep)
912 return -ENOMEM;
913 ep->umidi = umidi;
915 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
916 if (!ep->urb) {
917 snd_usbmidi_out_endpoint_delete(ep);
918 return -ENOMEM;
920 /* we never use interrupt output pipes */
921 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
922 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
923 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
924 GFP_KERNEL, &ep->urb->transfer_dma);
925 if (!buffer) {
926 snd_usbmidi_out_endpoint_delete(ep);
927 return -ENOMEM;
929 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
930 ep->max_transfer, snd_usbmidi_out_urb_complete, ep);
931 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
933 spin_lock_init(&ep->buffer_lock);
934 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
936 for (i = 0; i < 0x10; ++i)
937 if (ep_info->out_cables & (1 << i)) {
938 ep->ports[i].ep = ep;
939 ep->ports[i].cable = i << 4;
942 if (umidi->usb_protocol_ops->init_out_endpoint)
943 umidi->usb_protocol_ops->init_out_endpoint(ep);
945 rep->out = ep;
946 return 0;
950 * Frees everything.
952 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
954 int i;
956 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
957 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
958 if (ep->out)
959 snd_usbmidi_out_endpoint_delete(ep->out);
960 if (ep->in)
961 snd_usbmidi_in_endpoint_delete(ep->in);
963 kfree(umidi);
967 * Unlinks all URBs (must be done before the usb_device is deleted).
969 void snd_usbmidi_disconnect(struct list_head* p)
971 struct snd_usb_midi* umidi;
972 int i;
974 umidi = list_entry(p, struct snd_usb_midi, list);
975 del_timer_sync(&umidi->error_timer);
976 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
977 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
978 if (ep->out)
979 tasklet_kill(&ep->out->tasklet);
980 if (ep->out && ep->out->urb) {
981 usb_kill_urb(ep->out->urb);
982 if (umidi->usb_protocol_ops->finish_out_endpoint)
983 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
985 if (ep->in && ep->in->urb)
986 usb_kill_urb(ep->in->urb);
990 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
992 struct snd_usb_midi* umidi = rmidi->private_data;
993 snd_usbmidi_free(umidi);
996 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
997 int stream, int number)
999 struct list_head* list;
1001 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1002 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1003 if (substream->number == number)
1004 return substream;
1006 return NULL;
1010 * This list specifies names for ports that do not fit into the standard
1011 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1012 * such as internal control or synthesizer ports.
1014 static struct port_info {
1015 u32 id;
1016 short int port;
1017 short int voices;
1018 const char *name;
1019 unsigned int seq_flags;
1020 } snd_usbmidi_port_info[] = {
1021 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1022 { .id = USB_ID(vendor, product), \
1023 .port = num, .voices = voices_, \
1024 .name = name_, .seq_flags = flags }
1025 #define EXTERNAL_PORT(vendor, product, num, name) \
1026 PORT_INFO(vendor, product, num, name, 0, \
1027 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1028 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1029 SNDRV_SEQ_PORT_TYPE_PORT)
1030 #define CONTROL_PORT(vendor, product, num, name) \
1031 PORT_INFO(vendor, product, num, name, 0, \
1032 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1033 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1034 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1035 PORT_INFO(vendor, product, num, name, voices, \
1036 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1037 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1038 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1039 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1040 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1041 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1042 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1043 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1044 PORT_INFO(vendor, product, num, name, voices, \
1045 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1046 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1047 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1048 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1049 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1050 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1051 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1052 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1053 /* Roland UA-100 */
1054 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1055 /* Roland SC-8850 */
1056 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1057 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1058 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1059 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1060 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1061 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1062 /* Roland U-8 */
1063 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1064 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1065 /* Roland SC-8820 */
1066 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1067 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1068 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1069 /* Roland SK-500 */
1070 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1071 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1072 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1073 /* Roland SC-D70 */
1074 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1075 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1076 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1077 /* Edirol UM-880 */
1078 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1079 /* Edirol SD-90 */
1080 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1081 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1082 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1083 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1084 /* Edirol UM-550 */
1085 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1086 /* Edirol SD-20 */
1087 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1088 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1089 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1090 /* Edirol SD-80 */
1091 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1092 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1093 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1094 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1095 /* Edirol UA-700 */
1096 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1097 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1098 /* Roland VariOS */
1099 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1100 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1101 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1102 /* Edirol PCR */
1103 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1104 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1105 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1106 /* BOSS GS-10 */
1107 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1108 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1109 /* Edirol UA-1000 */
1110 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1111 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1112 /* Edirol UR-80 */
1113 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1114 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1115 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1116 /* Edirol PCR-A */
1117 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1118 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1119 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1120 /* Edirol UM-3EX */
1121 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1122 /* M-Audio MidiSport 8x8 */
1123 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1124 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1125 /* MOTU Fastlane */
1126 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1127 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1128 /* Emagic Unitor8/AMT8/MT4 */
1129 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1130 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1131 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1134 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1136 int i;
1138 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1139 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1140 snd_usbmidi_port_info[i].port == number)
1141 return &snd_usbmidi_port_info[i];
1143 return NULL;
1146 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1147 struct snd_seq_port_info *seq_port_info)
1149 struct snd_usb_midi *umidi = rmidi->private_data;
1150 struct port_info *port_info;
1152 /* TODO: read port flags from descriptors */
1153 port_info = find_port_info(umidi, number);
1154 if (port_info) {
1155 seq_port_info->type = port_info->seq_flags;
1156 seq_port_info->midi_voices = port_info->voices;
1160 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1161 int stream, int number,
1162 struct snd_rawmidi_substream ** rsubstream)
1164 struct port_info *port_info;
1165 const char *name_format;
1167 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1168 if (!substream) {
1169 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1170 return;
1173 /* TODO: read port name from jack descriptor */
1174 port_info = find_port_info(umidi, number);
1175 name_format = port_info ? port_info->name : "%s MIDI %d";
1176 snprintf(substream->name, sizeof(substream->name),
1177 name_format, umidi->chip->card->shortname, number + 1);
1179 *rsubstream = substream;
1183 * Creates the endpoints and their ports.
1185 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1186 struct snd_usb_midi_endpoint_info* endpoints)
1188 int i, j, err;
1189 int out_ports = 0, in_ports = 0;
1191 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1192 if (endpoints[i].out_cables) {
1193 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1194 &umidi->endpoints[i]);
1195 if (err < 0)
1196 return err;
1198 if (endpoints[i].in_cables) {
1199 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1200 &umidi->endpoints[i]);
1201 if (err < 0)
1202 return err;
1205 for (j = 0; j < 0x10; ++j) {
1206 if (endpoints[i].out_cables & (1 << j)) {
1207 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1208 &umidi->endpoints[i].out->ports[j].substream);
1209 ++out_ports;
1211 if (endpoints[i].in_cables & (1 << j)) {
1212 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1213 &umidi->endpoints[i].in->ports[j].substream);
1214 ++in_ports;
1218 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1219 out_ports, in_ports);
1220 return 0;
1224 * Returns MIDIStreaming device capabilities.
1226 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1227 struct snd_usb_midi_endpoint_info* endpoints)
1229 struct usb_interface* intf;
1230 struct usb_host_interface *hostif;
1231 struct usb_interface_descriptor* intfd;
1232 struct usb_ms_header_descriptor* ms_header;
1233 struct usb_host_endpoint *hostep;
1234 struct usb_endpoint_descriptor* ep;
1235 struct usb_ms_endpoint_descriptor* ms_ep;
1236 int i, epidx;
1238 intf = umidi->iface;
1239 if (!intf)
1240 return -ENXIO;
1241 hostif = &intf->altsetting[0];
1242 intfd = get_iface_desc(hostif);
1243 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1244 if (hostif->extralen >= 7 &&
1245 ms_header->bLength >= 7 &&
1246 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1247 ms_header->bDescriptorSubtype == HEADER)
1248 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1249 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1250 else
1251 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1253 epidx = 0;
1254 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1255 hostep = &hostif->endpoint[i];
1256 ep = get_ep_desc(hostep);
1257 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1258 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1259 continue;
1260 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1261 if (hostep->extralen < 4 ||
1262 ms_ep->bLength < 4 ||
1263 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1264 ms_ep->bDescriptorSubtype != MS_GENERAL)
1265 continue;
1266 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1267 if (endpoints[epidx].out_ep) {
1268 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1269 snd_printk(KERN_WARNING "too many endpoints\n");
1270 break;
1273 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1274 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1275 endpoints[epidx].out_interval = ep->bInterval;
1276 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1277 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1278 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1279 } else {
1280 if (endpoints[epidx].in_ep) {
1281 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1282 snd_printk(KERN_WARNING "too many endpoints\n");
1283 break;
1286 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1287 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1288 endpoints[epidx].in_interval = ep->bInterval;
1289 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1290 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1291 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1294 return 0;
1298 * On Roland devices, use the second alternate setting to be able to use
1299 * the interrupt input endpoint.
1301 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1303 struct usb_interface* intf;
1304 struct usb_host_interface *hostif;
1305 struct usb_interface_descriptor* intfd;
1307 intf = umidi->iface;
1308 if (!intf || intf->num_altsetting != 2)
1309 return;
1311 hostif = &intf->altsetting[1];
1312 intfd = get_iface_desc(hostif);
1313 if (intfd->bNumEndpoints != 2 ||
1314 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1315 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1316 return;
1318 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1319 intfd->bAlternateSetting);
1320 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1321 intfd->bAlternateSetting);
1325 * Try to find any usable endpoints in the interface.
1327 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1328 struct snd_usb_midi_endpoint_info* endpoint,
1329 int max_endpoints)
1331 struct usb_interface* intf;
1332 struct usb_host_interface *hostif;
1333 struct usb_interface_descriptor* intfd;
1334 struct usb_endpoint_descriptor* epd;
1335 int i, out_eps = 0, in_eps = 0;
1337 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
1338 snd_usbmidi_switch_roland_altsetting(umidi);
1340 if (endpoint[0].out_ep || endpoint[0].in_ep)
1341 return 0;
1343 intf = umidi->iface;
1344 if (!intf || intf->num_altsetting < 1)
1345 return -ENOENT;
1346 hostif = intf->cur_altsetting;
1347 intfd = get_iface_desc(hostif);
1349 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1350 epd = get_endpoint(hostif, i);
1351 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1352 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1353 continue;
1354 if (out_eps < max_endpoints &&
1355 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1356 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1357 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1358 endpoint[out_eps].out_interval = epd->bInterval;
1359 ++out_eps;
1361 if (in_eps < max_endpoints &&
1362 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1363 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1364 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1365 endpoint[in_eps].in_interval = epd->bInterval;
1366 ++in_eps;
1369 return (out_eps || in_eps) ? 0 : -ENOENT;
1373 * Detects the endpoints for one-port-per-endpoint protocols.
1375 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1376 struct snd_usb_midi_endpoint_info* endpoints)
1378 int err, i;
1380 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1381 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1382 if (endpoints[i].out_ep)
1383 endpoints[i].out_cables = 0x0001;
1384 if (endpoints[i].in_ep)
1385 endpoints[i].in_cables = 0x0001;
1387 return err;
1391 * Detects the endpoints and ports of Yamaha devices.
1393 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1394 struct snd_usb_midi_endpoint_info* endpoint)
1396 struct usb_interface* intf;
1397 struct usb_host_interface *hostif;
1398 struct usb_interface_descriptor* intfd;
1399 uint8_t* cs_desc;
1401 intf = umidi->iface;
1402 if (!intf)
1403 return -ENOENT;
1404 hostif = intf->altsetting;
1405 intfd = get_iface_desc(hostif);
1406 if (intfd->bNumEndpoints < 1)
1407 return -ENOENT;
1410 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1411 * necessarily with any useful contents. So simply count 'em.
1413 for (cs_desc = hostif->extra;
1414 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1415 cs_desc += cs_desc[0]) {
1416 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1417 if (cs_desc[2] == MIDI_IN_JACK)
1418 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1419 else if (cs_desc[2] == MIDI_OUT_JACK)
1420 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1423 if (!endpoint->in_cables && !endpoint->out_cables)
1424 return -ENOENT;
1426 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1430 * Creates the endpoints and their ports for Midiman devices.
1432 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1433 struct snd_usb_midi_endpoint_info* endpoint)
1435 struct snd_usb_midi_endpoint_info ep_info;
1436 struct usb_interface* intf;
1437 struct usb_host_interface *hostif;
1438 struct usb_interface_descriptor* intfd;
1439 struct usb_endpoint_descriptor* epd;
1440 int cable, err;
1442 intf = umidi->iface;
1443 if (!intf)
1444 return -ENOENT;
1445 hostif = intf->altsetting;
1446 intfd = get_iface_desc(hostif);
1448 * The various MidiSport devices have more or less random endpoint
1449 * numbers, so we have to identify the endpoints by their index in
1450 * the descriptor array, like the driver for that other OS does.
1452 * There is one interrupt input endpoint for all input ports, one
1453 * bulk output endpoint for even-numbered ports, and one for odd-
1454 * numbered ports. Both bulk output endpoints have corresponding
1455 * input bulk endpoints (at indices 1 and 3) which aren't used.
1457 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1458 snd_printdd(KERN_ERR "not enough endpoints\n");
1459 return -ENOENT;
1462 epd = get_endpoint(hostif, 0);
1463 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1464 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1465 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1466 return -ENXIO;
1468 epd = get_endpoint(hostif, 2);
1469 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1470 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1471 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1472 return -ENXIO;
1474 if (endpoint->out_cables > 0x0001) {
1475 epd = get_endpoint(hostif, 4);
1476 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1477 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1478 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1479 return -ENXIO;
1483 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1484 ep_info.out_cables = endpoint->out_cables & 0x5555;
1485 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1486 if (err < 0)
1487 return err;
1489 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1490 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1491 ep_info.in_cables = endpoint->in_cables;
1492 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1493 if (err < 0)
1494 return err;
1496 if (endpoint->out_cables > 0x0001) {
1497 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1498 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1499 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1500 if (err < 0)
1501 return err;
1504 for (cable = 0; cable < 0x10; ++cable) {
1505 if (endpoint->out_cables & (1 << cable))
1506 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1507 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1508 if (endpoint->in_cables & (1 << cable))
1509 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1510 &umidi->endpoints[0].in->ports[cable].substream);
1512 return 0;
1515 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1516 .get_port_info = snd_usbmidi_get_port_info,
1519 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
1520 int out_ports, int in_ports)
1522 struct snd_rawmidi *rmidi;
1523 int err;
1525 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1526 umidi->chip->next_midi_device++,
1527 out_ports, in_ports, &rmidi);
1528 if (err < 0)
1529 return err;
1530 strcpy(rmidi->name, umidi->chip->card->shortname);
1531 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1532 SNDRV_RAWMIDI_INFO_INPUT |
1533 SNDRV_RAWMIDI_INFO_DUPLEX;
1534 rmidi->ops = &snd_usbmidi_ops;
1535 rmidi->private_data = umidi;
1536 rmidi->private_free = snd_usbmidi_rawmidi_free;
1537 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1538 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1540 umidi->rmidi = rmidi;
1541 return 0;
1545 * Temporarily stop input.
1547 void snd_usbmidi_input_stop(struct list_head* p)
1549 struct snd_usb_midi* umidi;
1550 int i;
1552 umidi = list_entry(p, struct snd_usb_midi, list);
1553 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1554 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1555 if (ep->in)
1556 usb_kill_urb(ep->in->urb);
1560 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
1562 if (ep) {
1563 struct urb* urb = ep->urb;
1564 urb->dev = ep->umidi->chip->dev;
1565 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1570 * Resume input after a call to snd_usbmidi_input_stop().
1572 void snd_usbmidi_input_start(struct list_head* p)
1574 struct snd_usb_midi* umidi;
1575 int i;
1577 umidi = list_entry(p, struct snd_usb_midi, list);
1578 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1579 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1583 * Creates and registers everything needed for a MIDI streaming interface.
1585 int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
1586 struct usb_interface* iface,
1587 const struct snd_usb_audio_quirk* quirk)
1589 struct snd_usb_midi* umidi;
1590 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
1591 int out_ports, in_ports;
1592 int i, err;
1594 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
1595 if (!umidi)
1596 return -ENOMEM;
1597 umidi->chip = chip;
1598 umidi->iface = iface;
1599 umidi->quirk = quirk;
1600 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
1601 init_timer(&umidi->error_timer);
1602 umidi->error_timer.function = snd_usbmidi_error_timer;
1603 umidi->error_timer.data = (unsigned long)umidi;
1605 /* detect the endpoint(s) to use */
1606 memset(endpoints, 0, sizeof(endpoints));
1607 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1608 case QUIRK_MIDI_STANDARD_INTERFACE:
1609 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1610 break;
1611 case QUIRK_MIDI_FIXED_ENDPOINT:
1612 memcpy(&endpoints[0], quirk->data,
1613 sizeof(struct snd_usb_midi_endpoint_info));
1614 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1615 break;
1616 case QUIRK_MIDI_YAMAHA:
1617 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1618 break;
1619 case QUIRK_MIDI_MIDIMAN:
1620 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1621 memcpy(&endpoints[0], quirk->data,
1622 sizeof(struct snd_usb_midi_endpoint_info));
1623 err = 0;
1624 break;
1625 case QUIRK_MIDI_NOVATION:
1626 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1627 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1628 break;
1629 case QUIRK_MIDI_RAW:
1630 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1631 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1632 break;
1633 case QUIRK_MIDI_EMAGIC:
1634 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1635 memcpy(&endpoints[0], quirk->data,
1636 sizeof(struct snd_usb_midi_endpoint_info));
1637 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1638 break;
1639 case QUIRK_MIDI_CME:
1640 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1641 break;
1642 default:
1643 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1644 err = -ENXIO;
1645 break;
1647 if (err < 0) {
1648 kfree(umidi);
1649 return err;
1652 /* create rawmidi device */
1653 out_ports = 0;
1654 in_ports = 0;
1655 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1656 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1657 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1659 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1660 if (err < 0) {
1661 kfree(umidi);
1662 return err;
1665 /* create endpoint/port structures */
1666 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1667 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1668 else
1669 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1670 if (err < 0) {
1671 snd_usbmidi_free(umidi);
1672 return err;
1675 list_add(&umidi->list, &umidi->chip->midi_list);
1677 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1678 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1679 return 0;
1682 EXPORT_SYMBOL(snd_usb_create_midi_interface);
1683 EXPORT_SYMBOL(snd_usbmidi_input_stop);
1684 EXPORT_SYMBOL(snd_usbmidi_input_start);
1685 EXPORT_SYMBOL(snd_usbmidi_disconnect);