eventfd/kaio integration fix
[linux-2.6.22.y-op.git] / sound / usb / usbmidi.c
blob99295f9b76910eb9610243170dab6acdfcd2e971
1 /*
2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2007 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 u8 running_status_length;
149 } ports[0x10];
150 u8 seen_f5;
151 u8 error_resubmit;
152 int current_port;
155 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
157 static const uint8_t snd_usbmidi_cin_length[] = {
158 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
162 * Submits the URB, with error handling.
164 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
166 int err = usb_submit_urb(urb, flags);
167 if (err < 0 && err != -ENODEV)
168 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
169 return err;
173 * Error handling for URB completion functions.
175 static int snd_usbmidi_urb_error(int status)
177 switch (status) {
178 /* manually unlinked, or device gone */
179 case -ENOENT:
180 case -ECONNRESET:
181 case -ESHUTDOWN:
182 case -ENODEV:
183 return -ENODEV;
184 /* errors that might occur during unplugging */
185 case -EPROTO:
186 case -ETIME:
187 case -EILSEQ:
188 return -EIO;
189 default:
190 snd_printk(KERN_ERR "urb status %d\n", status);
191 return 0; /* continue */
196 * Receives a chunk of MIDI data.
198 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
199 uint8_t* data, int length)
201 struct usbmidi_in_port* port = &ep->ports[portidx];
203 if (!port->substream) {
204 snd_printd("unexpected port %d!\n", portidx);
205 return;
207 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
208 return;
209 snd_rawmidi_receive(port->substream, data, length);
212 #ifdef DUMP_PACKETS
213 static void dump_urb(const char *type, const u8 *data, int length)
215 snd_printk(KERN_DEBUG "%s packet: [", type);
216 for (; length > 0; ++data, --length)
217 printk(" %02x", *data);
218 printk(" ]\n");
220 #else
221 #define dump_urb(type, data, length) /* nothing */
222 #endif
225 * Processes the data read from the device.
227 static void snd_usbmidi_in_urb_complete(struct urb* urb)
229 struct snd_usb_midi_in_endpoint* ep = urb->context;
231 if (urb->status == 0) {
232 dump_urb("received", urb->transfer_buffer, urb->actual_length);
233 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
234 urb->actual_length);
235 } else {
236 int err = snd_usbmidi_urb_error(urb->status);
237 if (err < 0) {
238 if (err != -ENODEV) {
239 ep->error_resubmit = 1;
240 mod_timer(&ep->umidi->error_timer,
241 jiffies + ERROR_DELAY_JIFFIES);
243 return;
247 urb->dev = ep->umidi->chip->dev;
248 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
251 static void snd_usbmidi_out_urb_complete(struct urb* urb)
253 struct snd_usb_midi_out_endpoint* ep = urb->context;
255 spin_lock(&ep->buffer_lock);
256 ep->urb_active = 0;
257 spin_unlock(&ep->buffer_lock);
258 if (urb->status < 0) {
259 int err = snd_usbmidi_urb_error(urb->status);
260 if (err < 0) {
261 if (err != -ENODEV)
262 mod_timer(&ep->umidi->error_timer,
263 jiffies + ERROR_DELAY_JIFFIES);
264 return;
267 snd_usbmidi_do_output(ep);
271 * This is called when some data should be transferred to the device
272 * (from one or more substreams).
274 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
276 struct urb* urb = ep->urb;
277 unsigned long flags;
279 spin_lock_irqsave(&ep->buffer_lock, flags);
280 if (ep->urb_active || ep->umidi->chip->shutdown) {
281 spin_unlock_irqrestore(&ep->buffer_lock, flags);
282 return;
285 urb->transfer_buffer_length = 0;
286 ep->umidi->usb_protocol_ops->output(ep);
288 if (urb->transfer_buffer_length > 0) {
289 dump_urb("sending", urb->transfer_buffer,
290 urb->transfer_buffer_length);
291 urb->dev = ep->umidi->chip->dev;
292 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
294 spin_unlock_irqrestore(&ep->buffer_lock, flags);
297 static void snd_usbmidi_out_tasklet(unsigned long data)
299 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
301 snd_usbmidi_do_output(ep);
304 /* called after transfers had been interrupted due to some USB error */
305 static void snd_usbmidi_error_timer(unsigned long data)
307 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
308 int i;
310 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
311 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
312 if (in && in->error_resubmit) {
313 in->error_resubmit = 0;
314 in->urb->dev = umidi->chip->dev;
315 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
317 if (umidi->endpoints[i].out)
318 snd_usbmidi_do_output(umidi->endpoints[i].out);
322 /* helper function to send static data that may not DMA-able */
323 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
324 const void *data, int len)
326 int err;
327 void *buf = kmemdup(data, len, GFP_KERNEL);
328 if (!buf)
329 return -ENOMEM;
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 * Buggy M-Audio device: running status on input results in a packet that has
371 * the data bytes but not the status byte and that is marked with CIN 4.
373 static void snd_usbmidi_maudio_broken_running_status_input(
374 struct snd_usb_midi_in_endpoint* ep,
375 uint8_t* buffer, int buffer_length)
377 int i;
379 for (i = 0; i + 3 < buffer_length; i += 4)
380 if (buffer[i] != 0) {
381 int cable = buffer[i] >> 4;
382 u8 cin = buffer[i] & 0x0f;
383 struct usbmidi_in_port *port = &ep->ports[cable];
384 int length;
386 length = snd_usbmidi_cin_length[cin];
387 if (cin == 0xf && buffer[i + 1] >= 0xf8)
388 ; /* realtime msg: no running status change */
389 else if (cin >= 0x8 && cin <= 0xe)
390 /* channel msg */
391 port->running_status_length = length - 1;
392 else if (cin == 0x4 &&
393 port->running_status_length != 0 &&
394 buffer[i + 1] < 0x80)
395 /* CIN 4 that is not a SysEx */
396 length = port->running_status_length;
397 else
399 * All other msgs cannot begin running status.
400 * (A channel msg sent as two or three CIN 0xF
401 * packets could in theory, but this device
402 * doesn't use this format.)
404 port->running_status_length = 0;
405 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
410 * Adds one USB MIDI packet to the output buffer.
412 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
413 uint8_t p1, uint8_t p2, uint8_t p3)
416 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
417 buf[0] = p0;
418 buf[1] = p1;
419 buf[2] = p2;
420 buf[3] = p3;
421 urb->transfer_buffer_length += 4;
425 * Adds one Midiman packet to the output buffer.
427 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
428 uint8_t p1, uint8_t p2, uint8_t p3)
431 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
432 buf[0] = p1;
433 buf[1] = p2;
434 buf[2] = p3;
435 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
436 urb->transfer_buffer_length += 4;
440 * Converts MIDI commands to USB MIDI packets.
442 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
443 uint8_t b, struct urb* urb)
445 uint8_t p0 = port->cable;
446 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
447 port->ep->umidi->usb_protocol_ops->output_packet;
449 if (b >= 0xf8) {
450 output_packet(urb, p0 | 0x0f, b, 0, 0);
451 } else if (b >= 0xf0) {
452 switch (b) {
453 case 0xf0:
454 port->data[0] = b;
455 port->state = STATE_SYSEX_1;
456 break;
457 case 0xf1:
458 case 0xf3:
459 port->data[0] = b;
460 port->state = STATE_1PARAM;
461 break;
462 case 0xf2:
463 port->data[0] = b;
464 port->state = STATE_2PARAM_1;
465 break;
466 case 0xf4:
467 case 0xf5:
468 port->state = STATE_UNKNOWN;
469 break;
470 case 0xf6:
471 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
472 port->state = STATE_UNKNOWN;
473 break;
474 case 0xf7:
475 switch (port->state) {
476 case STATE_SYSEX_0:
477 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
478 break;
479 case STATE_SYSEX_1:
480 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
481 break;
482 case STATE_SYSEX_2:
483 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
484 break;
486 port->state = STATE_UNKNOWN;
487 break;
489 } else if (b >= 0x80) {
490 port->data[0] = b;
491 if (b >= 0xc0 && b <= 0xdf)
492 port->state = STATE_1PARAM;
493 else
494 port->state = STATE_2PARAM_1;
495 } else { /* b < 0x80 */
496 switch (port->state) {
497 case STATE_1PARAM:
498 if (port->data[0] < 0xf0) {
499 p0 |= port->data[0] >> 4;
500 } else {
501 p0 |= 0x02;
502 port->state = STATE_UNKNOWN;
504 output_packet(urb, p0, port->data[0], b, 0);
505 break;
506 case STATE_2PARAM_1:
507 port->data[1] = b;
508 port->state = STATE_2PARAM_2;
509 break;
510 case STATE_2PARAM_2:
511 if (port->data[0] < 0xf0) {
512 p0 |= port->data[0] >> 4;
513 port->state = STATE_2PARAM_1;
514 } else {
515 p0 |= 0x03;
516 port->state = STATE_UNKNOWN;
518 output_packet(urb, p0, port->data[0], port->data[1], b);
519 break;
520 case STATE_SYSEX_0:
521 port->data[0] = b;
522 port->state = STATE_SYSEX_1;
523 break;
524 case STATE_SYSEX_1:
525 port->data[1] = b;
526 port->state = STATE_SYSEX_2;
527 break;
528 case STATE_SYSEX_2:
529 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
530 port->state = STATE_SYSEX_0;
531 break;
536 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
538 struct urb* urb = ep->urb;
539 int p;
541 /* FIXME: lower-numbered ports can starve higher-numbered ports */
542 for (p = 0; p < 0x10; ++p) {
543 struct usbmidi_out_port* port = &ep->ports[p];
544 if (!port->active)
545 continue;
546 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
547 uint8_t b;
548 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
549 port->active = 0;
550 break;
552 snd_usbmidi_transmit_byte(port, b, urb);
557 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
558 .input = snd_usbmidi_standard_input,
559 .output = snd_usbmidi_standard_output,
560 .output_packet = snd_usbmidi_output_standard_packet,
563 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
564 .input = snd_usbmidi_midiman_input,
565 .output = snd_usbmidi_standard_output,
566 .output_packet = snd_usbmidi_output_midiman_packet,
569 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
570 .input = snd_usbmidi_maudio_broken_running_status_input,
571 .output = snd_usbmidi_standard_output,
572 .output_packet = snd_usbmidi_output_standard_packet,
576 * Novation USB MIDI protocol: number of data bytes is in the first byte
577 * (when receiving) (+1!) or in the second byte (when sending); data begins
578 * at the third byte.
581 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
582 uint8_t* buffer, int buffer_length)
584 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
585 return;
586 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
589 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
591 uint8_t* transfer_buffer;
592 int count;
594 if (!ep->ports[0].active)
595 return;
596 transfer_buffer = ep->urb->transfer_buffer;
597 count = snd_rawmidi_transmit(ep->ports[0].substream,
598 &transfer_buffer[2],
599 ep->max_transfer - 2);
600 if (count < 1) {
601 ep->ports[0].active = 0;
602 return;
604 transfer_buffer[0] = 0;
605 transfer_buffer[1] = count;
606 ep->urb->transfer_buffer_length = 2 + count;
609 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
610 .input = snd_usbmidi_novation_input,
611 .output = snd_usbmidi_novation_output,
615 * "raw" protocol: used by the MOTU FastLane.
618 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
619 uint8_t* buffer, int buffer_length)
621 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
624 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
626 int count;
628 if (!ep->ports[0].active)
629 return;
630 count = snd_rawmidi_transmit(ep->ports[0].substream,
631 ep->urb->transfer_buffer,
632 ep->max_transfer);
633 if (count < 1) {
634 ep->ports[0].active = 0;
635 return;
637 ep->urb->transfer_buffer_length = count;
640 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
641 .input = snd_usbmidi_raw_input,
642 .output = snd_usbmidi_raw_output,
646 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
649 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
651 static const u8 init_data[] = {
652 /* initialization magic: "get version" */
653 0xf0,
654 0x00, 0x20, 0x31, /* Emagic */
655 0x64, /* Unitor8 */
656 0x0b, /* version number request */
657 0x00, /* command version */
658 0x00, /* EEPROM, box 0 */
659 0xf7
661 send_bulk_static_data(ep, init_data, sizeof(init_data));
662 /* while we're at it, pour on more magic */
663 send_bulk_static_data(ep, init_data, sizeof(init_data));
666 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
668 static const u8 finish_data[] = {
669 /* switch to patch mode with last preset */
670 0xf0,
671 0x00, 0x20, 0x31, /* Emagic */
672 0x64, /* Unitor8 */
673 0x10, /* patch switch command */
674 0x00, /* command version */
675 0x7f, /* to all boxes */
676 0x40, /* last preset in EEPROM */
677 0xf7
679 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
682 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
683 uint8_t* buffer, int buffer_length)
685 int i;
687 /* FF indicates end of valid data */
688 for (i = 0; i < buffer_length; ++i)
689 if (buffer[i] == 0xff) {
690 buffer_length = i;
691 break;
694 /* handle F5 at end of last buffer */
695 if (ep->seen_f5)
696 goto switch_port;
698 while (buffer_length > 0) {
699 /* determine size of data until next F5 */
700 for (i = 0; i < buffer_length; ++i)
701 if (buffer[i] == 0xf5)
702 break;
703 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
704 buffer += i;
705 buffer_length -= i;
707 if (buffer_length <= 0)
708 break;
709 /* assert(buffer[0] == 0xf5); */
710 ep->seen_f5 = 1;
711 ++buffer;
712 --buffer_length;
714 switch_port:
715 if (buffer_length <= 0)
716 break;
717 if (buffer[0] < 0x80) {
718 ep->current_port = (buffer[0] - 1) & 15;
719 ++buffer;
720 --buffer_length;
722 ep->seen_f5 = 0;
726 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
728 int port0 = ep->current_port;
729 uint8_t* buf = ep->urb->transfer_buffer;
730 int buf_free = ep->max_transfer;
731 int length, i;
733 for (i = 0; i < 0x10; ++i) {
734 /* round-robin, starting at the last current port */
735 int portnum = (port0 + i) & 15;
736 struct usbmidi_out_port* port = &ep->ports[portnum];
738 if (!port->active)
739 continue;
740 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
741 port->active = 0;
742 continue;
745 if (portnum != ep->current_port) {
746 if (buf_free < 2)
747 break;
748 ep->current_port = portnum;
749 buf[0] = 0xf5;
750 buf[1] = (portnum + 1) & 15;
751 buf += 2;
752 buf_free -= 2;
755 if (buf_free < 1)
756 break;
757 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
758 if (length > 0) {
759 buf += length;
760 buf_free -= length;
761 if (buf_free < 1)
762 break;
765 if (buf_free < ep->max_transfer && buf_free > 0) {
766 *buf = 0xff;
767 --buf_free;
769 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
772 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
773 .input = snd_usbmidi_emagic_input,
774 .output = snd_usbmidi_emagic_output,
775 .init_out_endpoint = snd_usbmidi_emagic_init_out,
776 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
780 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
782 struct snd_usb_midi* umidi = substream->rmidi->private_data;
783 struct usbmidi_out_port* port = NULL;
784 int i, j;
786 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
787 if (umidi->endpoints[i].out)
788 for (j = 0; j < 0x10; ++j)
789 if (umidi->endpoints[i].out->ports[j].substream == substream) {
790 port = &umidi->endpoints[i].out->ports[j];
791 break;
793 if (!port) {
794 snd_BUG();
795 return -ENXIO;
797 substream->runtime->private_data = port;
798 port->state = STATE_UNKNOWN;
799 return 0;
802 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
804 return 0;
807 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
809 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
811 port->active = up;
812 if (up) {
813 if (port->ep->umidi->chip->shutdown) {
814 /* gobble up remaining bytes to prevent wait in
815 * snd_rawmidi_drain_output */
816 while (!snd_rawmidi_transmit_empty(substream))
817 snd_rawmidi_transmit_ack(substream, 1);
818 return;
820 tasklet_hi_schedule(&port->ep->tasklet);
824 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
826 return 0;
829 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
831 return 0;
834 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
836 struct snd_usb_midi* umidi = substream->rmidi->private_data;
838 if (up)
839 set_bit(substream->number, &umidi->input_triggered);
840 else
841 clear_bit(substream->number, &umidi->input_triggered);
844 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
845 .open = snd_usbmidi_output_open,
846 .close = snd_usbmidi_output_close,
847 .trigger = snd_usbmidi_output_trigger,
850 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
851 .open = snd_usbmidi_input_open,
852 .close = snd_usbmidi_input_close,
853 .trigger = snd_usbmidi_input_trigger
857 * Frees an input endpoint.
858 * May be called when ep hasn't been initialized completely.
860 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
862 if (ep->urb) {
863 usb_buffer_free(ep->umidi->chip->dev,
864 ep->urb->transfer_buffer_length,
865 ep->urb->transfer_buffer,
866 ep->urb->transfer_dma);
867 usb_free_urb(ep->urb);
869 kfree(ep);
873 * Creates an input endpoint.
875 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
876 struct snd_usb_midi_endpoint_info* ep_info,
877 struct snd_usb_midi_endpoint* rep)
879 struct snd_usb_midi_in_endpoint* ep;
880 void* buffer;
881 unsigned int pipe;
882 int length;
884 rep->in = NULL;
885 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
886 if (!ep)
887 return -ENOMEM;
888 ep->umidi = umidi;
890 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
891 if (!ep->urb) {
892 snd_usbmidi_in_endpoint_delete(ep);
893 return -ENOMEM;
895 if (ep_info->in_interval)
896 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
897 else
898 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
899 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
900 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
901 &ep->urb->transfer_dma);
902 if (!buffer) {
903 snd_usbmidi_in_endpoint_delete(ep);
904 return -ENOMEM;
906 if (ep_info->in_interval)
907 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
908 length, snd_usbmidi_in_urb_complete, ep,
909 ep_info->in_interval);
910 else
911 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
912 length, snd_usbmidi_in_urb_complete, ep);
913 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
915 rep->in = ep;
916 return 0;
919 static unsigned int snd_usbmidi_count_bits(unsigned int x)
921 unsigned int bits;
923 for (bits = 0; x; ++bits)
924 x &= x - 1;
925 return bits;
929 * Frees an output endpoint.
930 * May be called when ep hasn't been initialized completely.
932 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
934 if (ep->urb) {
935 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
936 ep->urb->transfer_buffer,
937 ep->urb->transfer_dma);
938 usb_free_urb(ep->urb);
940 kfree(ep);
944 * Creates an output endpoint, and initializes output ports.
946 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
947 struct snd_usb_midi_endpoint_info* ep_info,
948 struct snd_usb_midi_endpoint* rep)
950 struct snd_usb_midi_out_endpoint* ep;
951 int i;
952 unsigned int pipe;
953 void* buffer;
955 rep->out = NULL;
956 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
957 if (!ep)
958 return -ENOMEM;
959 ep->umidi = umidi;
961 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
962 if (!ep->urb) {
963 snd_usbmidi_out_endpoint_delete(ep);
964 return -ENOMEM;
966 /* we never use interrupt output pipes */
967 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
968 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
969 /* FIXME: we need more URBs to get reasonable bandwidth here: */
970 ep->max_transfer = 4;
971 else
972 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
973 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
974 GFP_KERNEL, &ep->urb->transfer_dma);
975 if (!buffer) {
976 snd_usbmidi_out_endpoint_delete(ep);
977 return -ENOMEM;
979 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
980 ep->max_transfer, snd_usbmidi_out_urb_complete, ep);
981 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
983 spin_lock_init(&ep->buffer_lock);
984 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
986 for (i = 0; i < 0x10; ++i)
987 if (ep_info->out_cables & (1 << i)) {
988 ep->ports[i].ep = ep;
989 ep->ports[i].cable = i << 4;
992 if (umidi->usb_protocol_ops->init_out_endpoint)
993 umidi->usb_protocol_ops->init_out_endpoint(ep);
995 rep->out = ep;
996 return 0;
1000 * Frees everything.
1002 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1004 int i;
1006 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1007 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1008 if (ep->out)
1009 snd_usbmidi_out_endpoint_delete(ep->out);
1010 if (ep->in)
1011 snd_usbmidi_in_endpoint_delete(ep->in);
1013 kfree(umidi);
1017 * Unlinks all URBs (must be done before the usb_device is deleted).
1019 void snd_usbmidi_disconnect(struct list_head* p)
1021 struct snd_usb_midi* umidi;
1022 int i;
1024 umidi = list_entry(p, struct snd_usb_midi, list);
1025 del_timer_sync(&umidi->error_timer);
1026 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1027 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1028 if (ep->out)
1029 tasklet_kill(&ep->out->tasklet);
1030 if (ep->out && ep->out->urb) {
1031 usb_kill_urb(ep->out->urb);
1032 if (umidi->usb_protocol_ops->finish_out_endpoint)
1033 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1035 if (ep->in)
1036 usb_kill_urb(ep->in->urb);
1040 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1042 struct snd_usb_midi* umidi = rmidi->private_data;
1043 snd_usbmidi_free(umidi);
1046 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1047 int stream, int number)
1049 struct list_head* list;
1051 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1052 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1053 if (substream->number == number)
1054 return substream;
1056 return NULL;
1060 * This list specifies names for ports that do not fit into the standard
1061 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1062 * such as internal control or synthesizer ports.
1064 static struct port_info {
1065 u32 id;
1066 short int port;
1067 short int voices;
1068 const char *name;
1069 unsigned int seq_flags;
1070 } snd_usbmidi_port_info[] = {
1071 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1072 { .id = USB_ID(vendor, product), \
1073 .port = num, .voices = voices_, \
1074 .name = name_, .seq_flags = flags }
1075 #define EXTERNAL_PORT(vendor, product, num, name) \
1076 PORT_INFO(vendor, product, num, name, 0, \
1077 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1078 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1079 SNDRV_SEQ_PORT_TYPE_PORT)
1080 #define CONTROL_PORT(vendor, product, num, name) \
1081 PORT_INFO(vendor, product, num, name, 0, \
1082 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1083 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1084 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1085 PORT_INFO(vendor, product, num, name, voices, \
1086 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1087 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1088 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1089 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1090 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1091 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1092 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1093 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1094 PORT_INFO(vendor, product, num, name, voices, \
1095 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1096 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1097 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1098 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1099 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1100 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1101 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1102 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1103 /* Roland UA-100 */
1104 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1105 /* Roland SC-8850 */
1106 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1107 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1108 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1109 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1110 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1111 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1112 /* Roland U-8 */
1113 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1114 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1115 /* Roland SC-8820 */
1116 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1117 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1118 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1119 /* Roland SK-500 */
1120 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1121 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1122 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1123 /* Roland SC-D70 */
1124 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1125 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1126 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1127 /* Edirol UM-880 */
1128 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1129 /* Edirol SD-90 */
1130 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1131 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1132 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1133 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1134 /* Edirol UM-550 */
1135 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1136 /* Edirol SD-20 */
1137 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1138 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1139 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1140 /* Edirol SD-80 */
1141 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1142 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1143 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1144 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1145 /* Edirol UA-700 */
1146 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1147 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1148 /* Roland VariOS */
1149 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1150 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1151 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1152 /* Edirol PCR */
1153 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1154 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1155 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1156 /* BOSS GS-10 */
1157 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1158 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1159 /* Edirol UA-1000 */
1160 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1161 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1162 /* Edirol UR-80 */
1163 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1164 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1165 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1166 /* Edirol PCR-A */
1167 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1168 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1169 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1170 /* Edirol UM-3EX */
1171 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1172 /* M-Audio MidiSport 8x8 */
1173 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1174 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1175 /* MOTU Fastlane */
1176 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1177 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1178 /* Emagic Unitor8/AMT8/MT4 */
1179 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1180 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1181 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1184 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1186 int i;
1188 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1189 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1190 snd_usbmidi_port_info[i].port == number)
1191 return &snd_usbmidi_port_info[i];
1193 return NULL;
1196 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1197 struct snd_seq_port_info *seq_port_info)
1199 struct snd_usb_midi *umidi = rmidi->private_data;
1200 struct port_info *port_info;
1202 /* TODO: read port flags from descriptors */
1203 port_info = find_port_info(umidi, number);
1204 if (port_info) {
1205 seq_port_info->type = port_info->seq_flags;
1206 seq_port_info->midi_voices = port_info->voices;
1210 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1211 int stream, int number,
1212 struct snd_rawmidi_substream ** rsubstream)
1214 struct port_info *port_info;
1215 const char *name_format;
1217 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1218 if (!substream) {
1219 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1220 return;
1223 /* TODO: read port name from jack descriptor */
1224 port_info = find_port_info(umidi, number);
1225 name_format = port_info ? port_info->name : "%s MIDI %d";
1226 snprintf(substream->name, sizeof(substream->name),
1227 name_format, umidi->chip->card->shortname, number + 1);
1229 *rsubstream = substream;
1233 * Creates the endpoints and their ports.
1235 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1236 struct snd_usb_midi_endpoint_info* endpoints)
1238 int i, j, err;
1239 int out_ports = 0, in_ports = 0;
1241 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1242 if (endpoints[i].out_cables) {
1243 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1244 &umidi->endpoints[i]);
1245 if (err < 0)
1246 return err;
1248 if (endpoints[i].in_cables) {
1249 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1250 &umidi->endpoints[i]);
1251 if (err < 0)
1252 return err;
1255 for (j = 0; j < 0x10; ++j) {
1256 if (endpoints[i].out_cables & (1 << j)) {
1257 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1258 &umidi->endpoints[i].out->ports[j].substream);
1259 ++out_ports;
1261 if (endpoints[i].in_cables & (1 << j)) {
1262 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1263 &umidi->endpoints[i].in->ports[j].substream);
1264 ++in_ports;
1268 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1269 out_ports, in_ports);
1270 return 0;
1274 * Returns MIDIStreaming device capabilities.
1276 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1277 struct snd_usb_midi_endpoint_info* endpoints)
1279 struct usb_interface* intf;
1280 struct usb_host_interface *hostif;
1281 struct usb_interface_descriptor* intfd;
1282 struct usb_ms_header_descriptor* ms_header;
1283 struct usb_host_endpoint *hostep;
1284 struct usb_endpoint_descriptor* ep;
1285 struct usb_ms_endpoint_descriptor* ms_ep;
1286 int i, epidx;
1288 intf = umidi->iface;
1289 if (!intf)
1290 return -ENXIO;
1291 hostif = &intf->altsetting[0];
1292 intfd = get_iface_desc(hostif);
1293 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1294 if (hostif->extralen >= 7 &&
1295 ms_header->bLength >= 7 &&
1296 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1297 ms_header->bDescriptorSubtype == HEADER)
1298 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1299 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1300 else
1301 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1303 epidx = 0;
1304 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1305 hostep = &hostif->endpoint[i];
1306 ep = get_ep_desc(hostep);
1307 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1308 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1309 continue;
1310 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1311 if (hostep->extralen < 4 ||
1312 ms_ep->bLength < 4 ||
1313 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1314 ms_ep->bDescriptorSubtype != MS_GENERAL)
1315 continue;
1316 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1317 if (endpoints[epidx].out_ep) {
1318 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1319 snd_printk(KERN_WARNING "too many endpoints\n");
1320 break;
1323 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1324 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1325 endpoints[epidx].out_interval = ep->bInterval;
1326 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1327 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1328 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1329 } else {
1330 if (endpoints[epidx].in_ep) {
1331 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1332 snd_printk(KERN_WARNING "too many endpoints\n");
1333 break;
1336 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1337 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1338 endpoints[epidx].in_interval = ep->bInterval;
1339 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1340 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1341 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1344 return 0;
1348 * On Roland devices, use the second alternate setting to be able to use
1349 * the interrupt input endpoint.
1351 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1353 struct usb_interface* intf;
1354 struct usb_host_interface *hostif;
1355 struct usb_interface_descriptor* intfd;
1357 intf = umidi->iface;
1358 if (!intf || intf->num_altsetting != 2)
1359 return;
1361 hostif = &intf->altsetting[1];
1362 intfd = get_iface_desc(hostif);
1363 if (intfd->bNumEndpoints != 2 ||
1364 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1365 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1366 return;
1368 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1369 intfd->bAlternateSetting);
1370 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1371 intfd->bAlternateSetting);
1375 * Try to find any usable endpoints in the interface.
1377 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1378 struct snd_usb_midi_endpoint_info* endpoint,
1379 int max_endpoints)
1381 struct usb_interface* intf;
1382 struct usb_host_interface *hostif;
1383 struct usb_interface_descriptor* intfd;
1384 struct usb_endpoint_descriptor* epd;
1385 int i, out_eps = 0, in_eps = 0;
1387 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
1388 snd_usbmidi_switch_roland_altsetting(umidi);
1390 if (endpoint[0].out_ep || endpoint[0].in_ep)
1391 return 0;
1393 intf = umidi->iface;
1394 if (!intf || intf->num_altsetting < 1)
1395 return -ENOENT;
1396 hostif = intf->cur_altsetting;
1397 intfd = get_iface_desc(hostif);
1399 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1400 epd = get_endpoint(hostif, i);
1401 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1402 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1403 continue;
1404 if (out_eps < max_endpoints &&
1405 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1406 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1407 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1408 endpoint[out_eps].out_interval = epd->bInterval;
1409 ++out_eps;
1411 if (in_eps < max_endpoints &&
1412 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1413 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1414 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1415 endpoint[in_eps].in_interval = epd->bInterval;
1416 ++in_eps;
1419 return (out_eps || in_eps) ? 0 : -ENOENT;
1423 * Detects the endpoints for one-port-per-endpoint protocols.
1425 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1426 struct snd_usb_midi_endpoint_info* endpoints)
1428 int err, i;
1430 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1431 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1432 if (endpoints[i].out_ep)
1433 endpoints[i].out_cables = 0x0001;
1434 if (endpoints[i].in_ep)
1435 endpoints[i].in_cables = 0x0001;
1437 return err;
1441 * Detects the endpoints and ports of Yamaha devices.
1443 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1444 struct snd_usb_midi_endpoint_info* endpoint)
1446 struct usb_interface* intf;
1447 struct usb_host_interface *hostif;
1448 struct usb_interface_descriptor* intfd;
1449 uint8_t* cs_desc;
1451 intf = umidi->iface;
1452 if (!intf)
1453 return -ENOENT;
1454 hostif = intf->altsetting;
1455 intfd = get_iface_desc(hostif);
1456 if (intfd->bNumEndpoints < 1)
1457 return -ENOENT;
1460 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1461 * necessarily with any useful contents. So simply count 'em.
1463 for (cs_desc = hostif->extra;
1464 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1465 cs_desc += cs_desc[0]) {
1466 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1467 if (cs_desc[2] == MIDI_IN_JACK)
1468 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1469 else if (cs_desc[2] == MIDI_OUT_JACK)
1470 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1473 if (!endpoint->in_cables && !endpoint->out_cables)
1474 return -ENOENT;
1476 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1480 * Creates the endpoints and their ports for Midiman devices.
1482 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1483 struct snd_usb_midi_endpoint_info* endpoint)
1485 struct snd_usb_midi_endpoint_info ep_info;
1486 struct usb_interface* intf;
1487 struct usb_host_interface *hostif;
1488 struct usb_interface_descriptor* intfd;
1489 struct usb_endpoint_descriptor* epd;
1490 int cable, err;
1492 intf = umidi->iface;
1493 if (!intf)
1494 return -ENOENT;
1495 hostif = intf->altsetting;
1496 intfd = get_iface_desc(hostif);
1498 * The various MidiSport devices have more or less random endpoint
1499 * numbers, so we have to identify the endpoints by their index in
1500 * the descriptor array, like the driver for that other OS does.
1502 * There is one interrupt input endpoint for all input ports, one
1503 * bulk output endpoint for even-numbered ports, and one for odd-
1504 * numbered ports. Both bulk output endpoints have corresponding
1505 * input bulk endpoints (at indices 1 and 3) which aren't used.
1507 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1508 snd_printdd(KERN_ERR "not enough endpoints\n");
1509 return -ENOENT;
1512 epd = get_endpoint(hostif, 0);
1513 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1514 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1515 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1516 return -ENXIO;
1518 epd = get_endpoint(hostif, 2);
1519 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1520 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1521 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1522 return -ENXIO;
1524 if (endpoint->out_cables > 0x0001) {
1525 epd = get_endpoint(hostif, 4);
1526 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1527 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1528 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1529 return -ENXIO;
1533 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1534 ep_info.out_cables = endpoint->out_cables & 0x5555;
1535 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1536 if (err < 0)
1537 return err;
1539 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1540 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1541 ep_info.in_cables = endpoint->in_cables;
1542 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1543 if (err < 0)
1544 return err;
1546 if (endpoint->out_cables > 0x0001) {
1547 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1548 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1549 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1550 if (err < 0)
1551 return err;
1554 for (cable = 0; cable < 0x10; ++cable) {
1555 if (endpoint->out_cables & (1 << cable))
1556 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1557 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1558 if (endpoint->in_cables & (1 << cable))
1559 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1560 &umidi->endpoints[0].in->ports[cable].substream);
1562 return 0;
1565 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1566 .get_port_info = snd_usbmidi_get_port_info,
1569 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
1570 int out_ports, int in_ports)
1572 struct snd_rawmidi *rmidi;
1573 int err;
1575 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1576 umidi->chip->next_midi_device++,
1577 out_ports, in_ports, &rmidi);
1578 if (err < 0)
1579 return err;
1580 strcpy(rmidi->name, umidi->chip->card->shortname);
1581 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1582 SNDRV_RAWMIDI_INFO_INPUT |
1583 SNDRV_RAWMIDI_INFO_DUPLEX;
1584 rmidi->ops = &snd_usbmidi_ops;
1585 rmidi->private_data = umidi;
1586 rmidi->private_free = snd_usbmidi_rawmidi_free;
1587 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1588 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1590 umidi->rmidi = rmidi;
1591 return 0;
1595 * Temporarily stop input.
1597 void snd_usbmidi_input_stop(struct list_head* p)
1599 struct snd_usb_midi* umidi;
1600 int i;
1602 umidi = list_entry(p, struct snd_usb_midi, list);
1603 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1604 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1605 if (ep->in)
1606 usb_kill_urb(ep->in->urb);
1610 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
1612 if (ep) {
1613 struct urb* urb = ep->urb;
1614 urb->dev = ep->umidi->chip->dev;
1615 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1620 * Resume input after a call to snd_usbmidi_input_stop().
1622 void snd_usbmidi_input_start(struct list_head* p)
1624 struct snd_usb_midi* umidi;
1625 int i;
1627 umidi = list_entry(p, struct snd_usb_midi, list);
1628 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1629 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1633 * Creates and registers everything needed for a MIDI streaming interface.
1635 int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
1636 struct usb_interface* iface,
1637 const struct snd_usb_audio_quirk* quirk)
1639 struct snd_usb_midi* umidi;
1640 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
1641 int out_ports, in_ports;
1642 int i, err;
1644 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
1645 if (!umidi)
1646 return -ENOMEM;
1647 umidi->chip = chip;
1648 umidi->iface = iface;
1649 umidi->quirk = quirk;
1650 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
1651 init_timer(&umidi->error_timer);
1652 umidi->error_timer.function = snd_usbmidi_error_timer;
1653 umidi->error_timer.data = (unsigned long)umidi;
1655 /* detect the endpoint(s) to use */
1656 memset(endpoints, 0, sizeof(endpoints));
1657 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1658 case QUIRK_MIDI_STANDARD_INTERFACE:
1659 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1660 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1661 umidi->usb_protocol_ops =
1662 &snd_usbmidi_maudio_broken_running_status_ops;
1663 break;
1664 case QUIRK_MIDI_FIXED_ENDPOINT:
1665 memcpy(&endpoints[0], quirk->data,
1666 sizeof(struct snd_usb_midi_endpoint_info));
1667 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1668 break;
1669 case QUIRK_MIDI_YAMAHA:
1670 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1671 break;
1672 case QUIRK_MIDI_MIDIMAN:
1673 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1674 memcpy(&endpoints[0], quirk->data,
1675 sizeof(struct snd_usb_midi_endpoint_info));
1676 err = 0;
1677 break;
1678 case QUIRK_MIDI_NOVATION:
1679 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1680 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1681 break;
1682 case QUIRK_MIDI_RAW:
1683 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1684 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1685 break;
1686 case QUIRK_MIDI_EMAGIC:
1687 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1688 memcpy(&endpoints[0], quirk->data,
1689 sizeof(struct snd_usb_midi_endpoint_info));
1690 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1691 break;
1692 case QUIRK_MIDI_CME:
1693 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1694 break;
1695 default:
1696 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1697 err = -ENXIO;
1698 break;
1700 if (err < 0) {
1701 kfree(umidi);
1702 return err;
1705 /* create rawmidi device */
1706 out_ports = 0;
1707 in_ports = 0;
1708 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1709 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1710 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1712 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1713 if (err < 0) {
1714 kfree(umidi);
1715 return err;
1718 /* create endpoint/port structures */
1719 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1720 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1721 else
1722 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1723 if (err < 0) {
1724 snd_usbmidi_free(umidi);
1725 return err;
1728 list_add(&umidi->list, &umidi->chip->midi_list);
1730 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1731 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1732 return 0;
1735 EXPORT_SYMBOL(snd_usb_create_midi_interface);
1736 EXPORT_SYMBOL(snd_usbmidi_input_stop);
1737 EXPORT_SYMBOL(snd_usbmidi_input_start);
1738 EXPORT_SYMBOL(snd_usbmidi_disconnect);