2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2007 Clemens Ladisch
7 * Based on the OSS usb-midi driver by NAGANO Daisuke,
8 * NetBSD's umidi driver by Takuya SHIOZAKI,
9 * the "USB Device Class Definition for MIDI Devices" by Roland
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * Alternatively, this software may be distributed and/or modified under the
21 * terms of the GNU General Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at your option) any later
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <linux/kernel.h>
39 #include <linux/types.h>
40 #include <linux/bitops.h>
41 #include <linux/interrupt.h>
42 #include <linux/spinlock.h>
43 #include <linux/string.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/timer.h>
47 #include <linux/usb.h>
48 #include <sound/core.h>
49 #include <sound/rawmidi.h>
50 #include <sound/asequencer.h>
55 * define this to log all USB packets
57 /* #define DUMP_PACKETS */
60 * how long to wait after some USB errors, so that khubd can disconnect() us
61 * without too many spurious errors
63 #define ERROR_DELAY_JIFFIES (HZ / 10)
66 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
67 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
68 MODULE_LICENSE("Dual BSD/GPL");
71 struct usb_ms_header_descriptor
{
74 __u8 bDescriptorSubtype
;
77 } __attribute__ ((packed
));
79 struct usb_ms_endpoint_descriptor
{
82 __u8 bDescriptorSubtype
;
84 __u8 baAssocJackID
[0];
85 } __attribute__ ((packed
));
87 struct snd_usb_midi_in_endpoint
;
88 struct snd_usb_midi_out_endpoint
;
89 struct snd_usb_midi_endpoint
;
91 struct usb_protocol_ops
{
92 void (*input
)(struct snd_usb_midi_in_endpoint
*, uint8_t*, int);
93 void (*output
)(struct snd_usb_midi_out_endpoint
*);
94 void (*output_packet
)(struct urb
*, uint8_t, uint8_t, uint8_t, uint8_t);
95 void (*init_out_endpoint
)(struct snd_usb_midi_out_endpoint
*);
96 void (*finish_out_endpoint
)(struct snd_usb_midi_out_endpoint
*);
100 struct snd_usb_audio
*chip
;
101 struct usb_interface
*iface
;
102 const struct snd_usb_audio_quirk
*quirk
;
103 struct snd_rawmidi
*rmidi
;
104 struct usb_protocol_ops
* usb_protocol_ops
;
105 struct list_head list
;
106 struct timer_list error_timer
;
108 struct snd_usb_midi_endpoint
{
109 struct snd_usb_midi_out_endpoint
*out
;
110 struct snd_usb_midi_in_endpoint
*in
;
111 } endpoints
[MIDI_MAX_ENDPOINTS
];
112 unsigned long input_triggered
;
115 struct snd_usb_midi_out_endpoint
{
116 struct snd_usb_midi
* umidi
;
119 int max_transfer
; /* size of urb buffer */
120 struct tasklet_struct tasklet
;
122 spinlock_t buffer_lock
;
124 struct usbmidi_out_port
{
125 struct snd_usb_midi_out_endpoint
* ep
;
126 struct snd_rawmidi_substream
*substream
;
128 uint8_t cable
; /* cable number << 4 */
130 #define STATE_UNKNOWN 0
131 #define STATE_1PARAM 1
132 #define STATE_2PARAM_1 2
133 #define STATE_2PARAM_2 3
134 #define STATE_SYSEX_0 4
135 #define STATE_SYSEX_1 5
136 #define STATE_SYSEX_2 6
142 struct snd_usb_midi_in_endpoint
{
143 struct snd_usb_midi
* umidi
;
145 struct usbmidi_in_port
{
146 struct snd_rawmidi_substream
*substream
;
147 u8 running_status_length
;
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
);
172 * Error handling for URB completion functions.
174 static int snd_usbmidi_urb_error(int status
)
177 /* manually unlinked, or device gone */
183 /* errors that might occur during unplugging */
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
);
206 if (!test_bit(port
->substream
->number
, &ep
->umidi
->input_triggered
))
208 snd_rawmidi_receive(port
->substream
, data
, length
);
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
);
220 #define dump_urb(type, data, length) /* nothing */
224 * Processes the data read from the device.
226 static void snd_usbmidi_in_urb_complete(struct urb
* urb
)
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
,
235 int err
= snd_usbmidi_urb_error(urb
->status
);
237 if (err
!= -ENODEV
) {
238 ep
->error_resubmit
= 1;
239 mod_timer(&ep
->umidi
->error_timer
,
240 jiffies
+ ERROR_DELAY_JIFFIES
);
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
)
252 struct snd_usb_midi_out_endpoint
* ep
= urb
->context
;
254 spin_lock(&ep
->buffer_lock
);
256 spin_unlock(&ep
->buffer_lock
);
257 if (urb
->status
< 0) {
258 int err
= snd_usbmidi_urb_error(urb
->status
);
261 mod_timer(&ep
->umidi
->error_timer
,
262 jiffies
+ ERROR_DELAY_JIFFIES
);
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
;
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
);
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
;
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
)
326 void *buf
= kmemdup(data
, len
, GFP_KERNEL
);
329 dump_urb("sending", buf
, len
);
330 err
= usb_bulk_msg(ep
->umidi
->chip
->dev
, ep
->urb
->pipe
, buf
, len
,
337 * Standard USB MIDI protocol: see the spec.
338 * Midiman protocol: like the standard protocol, but the control byte is the
339 * fourth byte in each packet, and uses length instead of CIN.
342 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint
* ep
,
343 uint8_t* buffer
, int buffer_length
)
347 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
348 if (buffer
[i
] != 0) {
349 int cable
= buffer
[i
] >> 4;
350 int length
= snd_usbmidi_cin_length
[buffer
[i
] & 0x0f];
351 snd_usbmidi_input_data(ep
, cable
, &buffer
[i
+ 1], length
);
355 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint
* ep
,
356 uint8_t* buffer
, int buffer_length
)
360 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
361 if (buffer
[i
+ 3] != 0) {
362 int port
= buffer
[i
+ 3] >> 4;
363 int length
= buffer
[i
+ 3] & 3;
364 snd_usbmidi_input_data(ep
, port
, &buffer
[i
], length
);
369 * Buggy M-Audio device: running status on input results in a packet that has
370 * the data bytes but not the status byte and that is marked with CIN 4.
372 static void snd_usbmidi_maudio_broken_running_status_input(
373 struct snd_usb_midi_in_endpoint
* ep
,
374 uint8_t* buffer
, int buffer_length
)
378 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
379 if (buffer
[i
] != 0) {
380 int cable
= buffer
[i
] >> 4;
381 u8 cin
= buffer
[i
] & 0x0f;
382 struct usbmidi_in_port
*port
= &ep
->ports
[cable
];
385 length
= snd_usbmidi_cin_length
[cin
];
386 if (cin
== 0xf && buffer
[i
+ 1] >= 0xf8)
387 ; /* realtime msg: no running status change */
388 else if (cin
>= 0x8 && cin
<= 0xe)
390 port
->running_status_length
= length
- 1;
391 else if (cin
== 0x4 &&
392 port
->running_status_length
!= 0 &&
393 buffer
[i
+ 1] < 0x80)
394 /* CIN 4 that is not a SysEx */
395 length
= port
->running_status_length
;
398 * All other msgs cannot begin running status.
399 * (A channel msg sent as two or three CIN 0xF
400 * packets could in theory, but this device
401 * doesn't use this format.)
403 port
->running_status_length
= 0;
404 snd_usbmidi_input_data(ep
, cable
, &buffer
[i
+ 1], length
);
409 * CME protocol: like the standard protocol, but SysEx commands are sent as a
410 * single USB packet preceded by a 0x0F byte.
412 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint
*ep
,
413 uint8_t *buffer
, int buffer_length
)
415 if (buffer_length
< 2 || (buffer
[0] & 0x0f) != 0x0f)
416 snd_usbmidi_standard_input(ep
, buffer
, buffer_length
);
418 snd_usbmidi_input_data(ep
, buffer
[0] >> 4,
419 &buffer
[1], buffer_length
- 1);
423 * Adds one USB MIDI packet to the output buffer.
425 static void snd_usbmidi_output_standard_packet(struct urb
* urb
, uint8_t p0
,
426 uint8_t p1
, uint8_t p2
, uint8_t p3
)
429 uint8_t* buf
= (uint8_t*)urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
434 urb
->transfer_buffer_length
+= 4;
438 * Adds one Midiman packet to the output buffer.
440 static void snd_usbmidi_output_midiman_packet(struct urb
* urb
, uint8_t p0
,
441 uint8_t p1
, uint8_t p2
, uint8_t p3
)
444 uint8_t* buf
= (uint8_t*)urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
448 buf
[3] = (p0
& 0xf0) | snd_usbmidi_cin_length
[p0
& 0x0f];
449 urb
->transfer_buffer_length
+= 4;
453 * Converts MIDI commands to USB MIDI packets.
455 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port
* port
,
456 uint8_t b
, struct urb
* urb
)
458 uint8_t p0
= port
->cable
;
459 void (*output_packet
)(struct urb
*, uint8_t, uint8_t, uint8_t, uint8_t) =
460 port
->ep
->umidi
->usb_protocol_ops
->output_packet
;
463 output_packet(urb
, p0
| 0x0f, b
, 0, 0);
464 } else if (b
>= 0xf0) {
468 port
->state
= STATE_SYSEX_1
;
473 port
->state
= STATE_1PARAM
;
477 port
->state
= STATE_2PARAM_1
;
481 port
->state
= STATE_UNKNOWN
;
484 output_packet(urb
, p0
| 0x05, 0xf6, 0, 0);
485 port
->state
= STATE_UNKNOWN
;
488 switch (port
->state
) {
490 output_packet(urb
, p0
| 0x05, 0xf7, 0, 0);
493 output_packet(urb
, p0
| 0x06, port
->data
[0], 0xf7, 0);
496 output_packet(urb
, p0
| 0x07, port
->data
[0], port
->data
[1], 0xf7);
499 port
->state
= STATE_UNKNOWN
;
502 } else if (b
>= 0x80) {
504 if (b
>= 0xc0 && b
<= 0xdf)
505 port
->state
= STATE_1PARAM
;
507 port
->state
= STATE_2PARAM_1
;
508 } else { /* b < 0x80 */
509 switch (port
->state
) {
511 if (port
->data
[0] < 0xf0) {
512 p0
|= port
->data
[0] >> 4;
515 port
->state
= STATE_UNKNOWN
;
517 output_packet(urb
, p0
, port
->data
[0], b
, 0);
521 port
->state
= STATE_2PARAM_2
;
524 if (port
->data
[0] < 0xf0) {
525 p0
|= port
->data
[0] >> 4;
526 port
->state
= STATE_2PARAM_1
;
529 port
->state
= STATE_UNKNOWN
;
531 output_packet(urb
, p0
, port
->data
[0], port
->data
[1], b
);
535 port
->state
= STATE_SYSEX_1
;
539 port
->state
= STATE_SYSEX_2
;
542 output_packet(urb
, p0
| 0x04, port
->data
[0], port
->data
[1], b
);
543 port
->state
= STATE_SYSEX_0
;
549 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint
* ep
)
551 struct urb
* urb
= ep
->urb
;
554 /* FIXME: lower-numbered ports can starve higher-numbered ports */
555 for (p
= 0; p
< 0x10; ++p
) {
556 struct usbmidi_out_port
* port
= &ep
->ports
[p
];
559 while (urb
->transfer_buffer_length
+ 3 < ep
->max_transfer
) {
561 if (snd_rawmidi_transmit(port
->substream
, &b
, 1) != 1) {
565 snd_usbmidi_transmit_byte(port
, b
, urb
);
570 static struct usb_protocol_ops snd_usbmidi_standard_ops
= {
571 .input
= snd_usbmidi_standard_input
,
572 .output
= snd_usbmidi_standard_output
,
573 .output_packet
= snd_usbmidi_output_standard_packet
,
576 static struct usb_protocol_ops snd_usbmidi_midiman_ops
= {
577 .input
= snd_usbmidi_midiman_input
,
578 .output
= snd_usbmidi_standard_output
,
579 .output_packet
= snd_usbmidi_output_midiman_packet
,
582 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops
= {
583 .input
= snd_usbmidi_maudio_broken_running_status_input
,
584 .output
= snd_usbmidi_standard_output
,
585 .output_packet
= snd_usbmidi_output_standard_packet
,
588 static struct usb_protocol_ops snd_usbmidi_cme_ops
= {
589 .input
= snd_usbmidi_cme_input
,
590 .output
= snd_usbmidi_standard_output
,
591 .output_packet
= snd_usbmidi_output_standard_packet
,
595 * Novation USB MIDI protocol: number of data bytes is in the first byte
596 * (when receiving) (+1!) or in the second byte (when sending); data begins
600 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint
* ep
,
601 uint8_t* buffer
, int buffer_length
)
603 if (buffer_length
< 2 || !buffer
[0] || buffer_length
< buffer
[0] + 1)
605 snd_usbmidi_input_data(ep
, 0, &buffer
[2], buffer
[0] - 1);
608 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint
* ep
)
610 uint8_t* transfer_buffer
;
613 if (!ep
->ports
[0].active
)
615 transfer_buffer
= ep
->urb
->transfer_buffer
;
616 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
618 ep
->max_transfer
- 2);
620 ep
->ports
[0].active
= 0;
623 transfer_buffer
[0] = 0;
624 transfer_buffer
[1] = count
;
625 ep
->urb
->transfer_buffer_length
= 2 + count
;
628 static struct usb_protocol_ops snd_usbmidi_novation_ops
= {
629 .input
= snd_usbmidi_novation_input
,
630 .output
= snd_usbmidi_novation_output
,
634 * "raw" protocol: used by the MOTU FastLane.
637 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint
* ep
,
638 uint8_t* buffer
, int buffer_length
)
640 snd_usbmidi_input_data(ep
, 0, buffer
, buffer_length
);
643 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint
* ep
)
647 if (!ep
->ports
[0].active
)
649 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
650 ep
->urb
->transfer_buffer
,
653 ep
->ports
[0].active
= 0;
656 ep
->urb
->transfer_buffer_length
= count
;
659 static struct usb_protocol_ops snd_usbmidi_raw_ops
= {
660 .input
= snd_usbmidi_raw_input
,
661 .output
= snd_usbmidi_raw_output
,
665 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
668 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint
* ep
)
670 static const u8 init_data
[] = {
671 /* initialization magic: "get version" */
673 0x00, 0x20, 0x31, /* Emagic */
675 0x0b, /* version number request */
676 0x00, /* command version */
677 0x00, /* EEPROM, box 0 */
680 send_bulk_static_data(ep
, init_data
, sizeof(init_data
));
681 /* while we're at it, pour on more magic */
682 send_bulk_static_data(ep
, init_data
, sizeof(init_data
));
685 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint
* ep
)
687 static const u8 finish_data
[] = {
688 /* switch to patch mode with last preset */
690 0x00, 0x20, 0x31, /* Emagic */
692 0x10, /* patch switch command */
693 0x00, /* command version */
694 0x7f, /* to all boxes */
695 0x40, /* last preset in EEPROM */
698 send_bulk_static_data(ep
, finish_data
, sizeof(finish_data
));
701 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint
* ep
,
702 uint8_t* buffer
, int buffer_length
)
706 /* FF indicates end of valid data */
707 for (i
= 0; i
< buffer_length
; ++i
)
708 if (buffer
[i
] == 0xff) {
713 /* handle F5 at end of last buffer */
717 while (buffer_length
> 0) {
718 /* determine size of data until next F5 */
719 for (i
= 0; i
< buffer_length
; ++i
)
720 if (buffer
[i
] == 0xf5)
722 snd_usbmidi_input_data(ep
, ep
->current_port
, buffer
, i
);
726 if (buffer_length
<= 0)
728 /* assert(buffer[0] == 0xf5); */
734 if (buffer_length
<= 0)
736 if (buffer
[0] < 0x80) {
737 ep
->current_port
= (buffer
[0] - 1) & 15;
745 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint
* ep
)
747 int port0
= ep
->current_port
;
748 uint8_t* buf
= ep
->urb
->transfer_buffer
;
749 int buf_free
= ep
->max_transfer
;
752 for (i
= 0; i
< 0x10; ++i
) {
753 /* round-robin, starting at the last current port */
754 int portnum
= (port0
+ i
) & 15;
755 struct usbmidi_out_port
* port
= &ep
->ports
[portnum
];
759 if (snd_rawmidi_transmit_peek(port
->substream
, buf
, 1) != 1) {
764 if (portnum
!= ep
->current_port
) {
767 ep
->current_port
= portnum
;
769 buf
[1] = (portnum
+ 1) & 15;
776 length
= snd_rawmidi_transmit(port
->substream
, buf
, buf_free
);
784 if (buf_free
< ep
->max_transfer
&& buf_free
> 0) {
788 ep
->urb
->transfer_buffer_length
= ep
->max_transfer
- buf_free
;
791 static struct usb_protocol_ops snd_usbmidi_emagic_ops
= {
792 .input
= snd_usbmidi_emagic_input
,
793 .output
= snd_usbmidi_emagic_output
,
794 .init_out_endpoint
= snd_usbmidi_emagic_init_out
,
795 .finish_out_endpoint
= snd_usbmidi_emagic_finish_out
,
799 static int snd_usbmidi_output_open(struct snd_rawmidi_substream
*substream
)
801 struct snd_usb_midi
* umidi
= substream
->rmidi
->private_data
;
802 struct usbmidi_out_port
* port
= NULL
;
805 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
806 if (umidi
->endpoints
[i
].out
)
807 for (j
= 0; j
< 0x10; ++j
)
808 if (umidi
->endpoints
[i
].out
->ports
[j
].substream
== substream
) {
809 port
= &umidi
->endpoints
[i
].out
->ports
[j
];
816 substream
->runtime
->private_data
= port
;
817 port
->state
= STATE_UNKNOWN
;
821 static int snd_usbmidi_output_close(struct snd_rawmidi_substream
*substream
)
826 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
828 struct usbmidi_out_port
* port
= (struct usbmidi_out_port
*)substream
->runtime
->private_data
;
832 if (port
->ep
->umidi
->chip
->shutdown
) {
833 /* gobble up remaining bytes to prevent wait in
834 * snd_rawmidi_drain_output */
835 while (!snd_rawmidi_transmit_empty(substream
))
836 snd_rawmidi_transmit_ack(substream
, 1);
839 tasklet_hi_schedule(&port
->ep
->tasklet
);
843 static int snd_usbmidi_input_open(struct snd_rawmidi_substream
*substream
)
848 static int snd_usbmidi_input_close(struct snd_rawmidi_substream
*substream
)
853 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
855 struct snd_usb_midi
* umidi
= substream
->rmidi
->private_data
;
858 set_bit(substream
->number
, &umidi
->input_triggered
);
860 clear_bit(substream
->number
, &umidi
->input_triggered
);
863 static struct snd_rawmidi_ops snd_usbmidi_output_ops
= {
864 .open
= snd_usbmidi_output_open
,
865 .close
= snd_usbmidi_output_close
,
866 .trigger
= snd_usbmidi_output_trigger
,
869 static struct snd_rawmidi_ops snd_usbmidi_input_ops
= {
870 .open
= snd_usbmidi_input_open
,
871 .close
= snd_usbmidi_input_close
,
872 .trigger
= snd_usbmidi_input_trigger
876 * Frees an input endpoint.
877 * May be called when ep hasn't been initialized completely.
879 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint
* ep
)
882 usb_buffer_free(ep
->umidi
->chip
->dev
,
883 ep
->urb
->transfer_buffer_length
,
884 ep
->urb
->transfer_buffer
,
885 ep
->urb
->transfer_dma
);
886 usb_free_urb(ep
->urb
);
892 * Creates an input endpoint.
894 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi
* umidi
,
895 struct snd_usb_midi_endpoint_info
* ep_info
,
896 struct snd_usb_midi_endpoint
* rep
)
898 struct snd_usb_midi_in_endpoint
* ep
;
904 ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
909 ep
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
911 snd_usbmidi_in_endpoint_delete(ep
);
914 if (ep_info
->in_interval
)
915 pipe
= usb_rcvintpipe(umidi
->chip
->dev
, ep_info
->in_ep
);
917 pipe
= usb_rcvbulkpipe(umidi
->chip
->dev
, ep_info
->in_ep
);
918 length
= usb_maxpacket(umidi
->chip
->dev
, pipe
, 0);
919 buffer
= usb_buffer_alloc(umidi
->chip
->dev
, length
, GFP_KERNEL
,
920 &ep
->urb
->transfer_dma
);
922 snd_usbmidi_in_endpoint_delete(ep
);
925 if (ep_info
->in_interval
)
926 usb_fill_int_urb(ep
->urb
, umidi
->chip
->dev
, pipe
, buffer
,
927 length
, snd_usbmidi_in_urb_complete
, ep
,
928 ep_info
->in_interval
);
930 usb_fill_bulk_urb(ep
->urb
, umidi
->chip
->dev
, pipe
, buffer
,
931 length
, snd_usbmidi_in_urb_complete
, ep
);
932 ep
->urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
938 static unsigned int snd_usbmidi_count_bits(unsigned int x
)
942 for (bits
= 0; x
; ++bits
)
948 * Frees an output endpoint.
949 * May be called when ep hasn't been initialized completely.
951 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint
* ep
)
954 usb_buffer_free(ep
->umidi
->chip
->dev
, ep
->max_transfer
,
955 ep
->urb
->transfer_buffer
,
956 ep
->urb
->transfer_dma
);
957 usb_free_urb(ep
->urb
);
963 * Creates an output endpoint, and initializes output ports.
965 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi
* umidi
,
966 struct snd_usb_midi_endpoint_info
* ep_info
,
967 struct snd_usb_midi_endpoint
* rep
)
969 struct snd_usb_midi_out_endpoint
* ep
;
975 ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
980 ep
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
982 snd_usbmidi_out_endpoint_delete(ep
);
985 if (ep_info
->out_interval
)
986 pipe
= usb_sndintpipe(umidi
->chip
->dev
, ep_info
->out_ep
);
988 pipe
= usb_sndbulkpipe(umidi
->chip
->dev
, ep_info
->out_ep
);
989 if (umidi
->chip
->usb_id
== USB_ID(0x0a92, 0x1020)) /* ESI M4U */
990 /* FIXME: we need more URBs to get reasonable bandwidth here: */
991 ep
->max_transfer
= 4;
993 ep
->max_transfer
= usb_maxpacket(umidi
->chip
->dev
, pipe
, 1);
994 buffer
= usb_buffer_alloc(umidi
->chip
->dev
, ep
->max_transfer
,
995 GFP_KERNEL
, &ep
->urb
->transfer_dma
);
997 snd_usbmidi_out_endpoint_delete(ep
);
1000 if (ep_info
->out_interval
)
1001 usb_fill_int_urb(ep
->urb
, umidi
->chip
->dev
, pipe
, buffer
,
1002 ep
->max_transfer
, snd_usbmidi_out_urb_complete
,
1003 ep
, ep_info
->out_interval
);
1005 usb_fill_bulk_urb(ep
->urb
, umidi
->chip
->dev
,
1006 pipe
, buffer
, ep
->max_transfer
,
1007 snd_usbmidi_out_urb_complete
, ep
);
1008 ep
->urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
1010 spin_lock_init(&ep
->buffer_lock
);
1011 tasklet_init(&ep
->tasklet
, snd_usbmidi_out_tasklet
, (unsigned long)ep
);
1013 for (i
= 0; i
< 0x10; ++i
)
1014 if (ep_info
->out_cables
& (1 << i
)) {
1015 ep
->ports
[i
].ep
= ep
;
1016 ep
->ports
[i
].cable
= i
<< 4;
1019 if (umidi
->usb_protocol_ops
->init_out_endpoint
)
1020 umidi
->usb_protocol_ops
->init_out_endpoint(ep
);
1029 static void snd_usbmidi_free(struct snd_usb_midi
* umidi
)
1033 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1034 struct snd_usb_midi_endpoint
* ep
= &umidi
->endpoints
[i
];
1036 snd_usbmidi_out_endpoint_delete(ep
->out
);
1038 snd_usbmidi_in_endpoint_delete(ep
->in
);
1044 * Unlinks all URBs (must be done before the usb_device is deleted).
1046 void snd_usbmidi_disconnect(struct list_head
* p
)
1048 struct snd_usb_midi
* umidi
;
1051 umidi
= list_entry(p
, struct snd_usb_midi
, list
);
1052 del_timer_sync(&umidi
->error_timer
);
1053 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1054 struct snd_usb_midi_endpoint
* ep
= &umidi
->endpoints
[i
];
1056 tasklet_kill(&ep
->out
->tasklet
);
1057 if (ep
->out
&& ep
->out
->urb
) {
1058 usb_kill_urb(ep
->out
->urb
);
1059 if (umidi
->usb_protocol_ops
->finish_out_endpoint
)
1060 umidi
->usb_protocol_ops
->finish_out_endpoint(ep
->out
);
1063 usb_kill_urb(ep
->in
->urb
);
1067 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi
*rmidi
)
1069 struct snd_usb_midi
* umidi
= rmidi
->private_data
;
1070 snd_usbmidi_free(umidi
);
1073 static struct snd_rawmidi_substream
*snd_usbmidi_find_substream(struct snd_usb_midi
* umidi
,
1074 int stream
, int number
)
1076 struct list_head
* list
;
1078 list_for_each(list
, &umidi
->rmidi
->streams
[stream
].substreams
) {
1079 struct snd_rawmidi_substream
*substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
1080 if (substream
->number
== number
)
1087 * This list specifies names for ports that do not fit into the standard
1088 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1089 * such as internal control or synthesizer ports.
1091 static struct port_info
{
1096 unsigned int seq_flags
;
1097 } snd_usbmidi_port_info
[] = {
1098 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1099 { .id = USB_ID(vendor, product), \
1100 .port = num, .voices = voices_, \
1101 .name = name_, .seq_flags = flags }
1102 #define EXTERNAL_PORT(vendor, product, num, name) \
1103 PORT_INFO(vendor, product, num, name, 0, \
1104 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1105 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1106 SNDRV_SEQ_PORT_TYPE_PORT)
1107 #define CONTROL_PORT(vendor, product, num, name) \
1108 PORT_INFO(vendor, product, num, name, 0, \
1109 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1110 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1111 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1112 PORT_INFO(vendor, product, num, name, voices, \
1113 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1114 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1115 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1116 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1117 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1118 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1119 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1120 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1121 PORT_INFO(vendor, product, num, name, voices, \
1122 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1123 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1124 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1125 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1126 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1127 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1128 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1129 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1131 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1132 /* Roland SC-8850 */
1133 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1134 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1135 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1136 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1137 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1138 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1140 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1141 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1142 /* Roland SC-8820 */
1143 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1144 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1145 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1147 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1148 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1149 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1151 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1152 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1153 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1155 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1157 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1158 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1159 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1160 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1162 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1164 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1165 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1166 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1168 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1169 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1170 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1171 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1173 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1174 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1176 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1177 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1178 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1180 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1181 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1182 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1184 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1185 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1186 /* Edirol UA-1000 */
1187 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1188 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1190 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1191 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1192 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1194 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1195 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1196 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1198 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1199 /* M-Audio MidiSport 8x8 */
1200 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1201 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1203 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1204 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1205 /* Emagic Unitor8/AMT8/MT4 */
1206 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1207 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1208 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1211 static struct port_info
*find_port_info(struct snd_usb_midi
* umidi
, int number
)
1215 for (i
= 0; i
< ARRAY_SIZE(snd_usbmidi_port_info
); ++i
) {
1216 if (snd_usbmidi_port_info
[i
].id
== umidi
->chip
->usb_id
&&
1217 snd_usbmidi_port_info
[i
].port
== number
)
1218 return &snd_usbmidi_port_info
[i
];
1223 static void snd_usbmidi_get_port_info(struct snd_rawmidi
*rmidi
, int number
,
1224 struct snd_seq_port_info
*seq_port_info
)
1226 struct snd_usb_midi
*umidi
= rmidi
->private_data
;
1227 struct port_info
*port_info
;
1229 /* TODO: read port flags from descriptors */
1230 port_info
= find_port_info(umidi
, number
);
1232 seq_port_info
->type
= port_info
->seq_flags
;
1233 seq_port_info
->midi_voices
= port_info
->voices
;
1237 static void snd_usbmidi_init_substream(struct snd_usb_midi
* umidi
,
1238 int stream
, int number
,
1239 struct snd_rawmidi_substream
** rsubstream
)
1241 struct port_info
*port_info
;
1242 const char *name_format
;
1244 struct snd_rawmidi_substream
*substream
= snd_usbmidi_find_substream(umidi
, stream
, number
);
1246 snd_printd(KERN_ERR
"substream %d:%d not found\n", stream
, number
);
1250 /* TODO: read port name from jack descriptor */
1251 port_info
= find_port_info(umidi
, number
);
1252 name_format
= port_info
? port_info
->name
: "%s MIDI %d";
1253 snprintf(substream
->name
, sizeof(substream
->name
),
1254 name_format
, umidi
->chip
->card
->shortname
, number
+ 1);
1256 *rsubstream
= substream
;
1260 * Creates the endpoints and their ports.
1262 static int snd_usbmidi_create_endpoints(struct snd_usb_midi
* umidi
,
1263 struct snd_usb_midi_endpoint_info
* endpoints
)
1266 int out_ports
= 0, in_ports
= 0;
1268 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1269 if (endpoints
[i
].out_cables
) {
1270 err
= snd_usbmidi_out_endpoint_create(umidi
, &endpoints
[i
],
1271 &umidi
->endpoints
[i
]);
1275 if (endpoints
[i
].in_cables
) {
1276 err
= snd_usbmidi_in_endpoint_create(umidi
, &endpoints
[i
],
1277 &umidi
->endpoints
[i
]);
1282 for (j
= 0; j
< 0x10; ++j
) {
1283 if (endpoints
[i
].out_cables
& (1 << j
)) {
1284 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, out_ports
,
1285 &umidi
->endpoints
[i
].out
->ports
[j
].substream
);
1288 if (endpoints
[i
].in_cables
& (1 << j
)) {
1289 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, in_ports
,
1290 &umidi
->endpoints
[i
].in
->ports
[j
].substream
);
1295 snd_printdd(KERN_INFO
"created %d output and %d input ports\n",
1296 out_ports
, in_ports
);
1301 * Returns MIDIStreaming device capabilities.
1303 static int snd_usbmidi_get_ms_info(struct snd_usb_midi
* umidi
,
1304 struct snd_usb_midi_endpoint_info
* endpoints
)
1306 struct usb_interface
* intf
;
1307 struct usb_host_interface
*hostif
;
1308 struct usb_interface_descriptor
* intfd
;
1309 struct usb_ms_header_descriptor
* ms_header
;
1310 struct usb_host_endpoint
*hostep
;
1311 struct usb_endpoint_descriptor
* ep
;
1312 struct usb_ms_endpoint_descriptor
* ms_ep
;
1315 intf
= umidi
->iface
;
1318 hostif
= &intf
->altsetting
[0];
1319 intfd
= get_iface_desc(hostif
);
1320 ms_header
= (struct usb_ms_header_descriptor
*)hostif
->extra
;
1321 if (hostif
->extralen
>= 7 &&
1322 ms_header
->bLength
>= 7 &&
1323 ms_header
->bDescriptorType
== USB_DT_CS_INTERFACE
&&
1324 ms_header
->bDescriptorSubtype
== HEADER
)
1325 snd_printdd(KERN_INFO
"MIDIStreaming version %02x.%02x\n",
1326 ms_header
->bcdMSC
[1], ms_header
->bcdMSC
[0]);
1328 snd_printk(KERN_WARNING
"MIDIStreaming interface descriptor not found\n");
1331 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
1332 hostep
= &hostif
->endpoint
[i
];
1333 ep
= get_ep_desc(hostep
);
1334 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
&&
1335 (ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1337 ms_ep
= (struct usb_ms_endpoint_descriptor
*)hostep
->extra
;
1338 if (hostep
->extralen
< 4 ||
1339 ms_ep
->bLength
< 4 ||
1340 ms_ep
->bDescriptorType
!= USB_DT_CS_ENDPOINT
||
1341 ms_ep
->bDescriptorSubtype
!= MS_GENERAL
)
1343 if ((ep
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1344 if (endpoints
[epidx
].out_ep
) {
1345 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
1346 snd_printk(KERN_WARNING
"too many endpoints\n");
1350 endpoints
[epidx
].out_ep
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1351 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
1352 endpoints
[epidx
].out_interval
= ep
->bInterval
;
1353 else if (snd_usb_get_speed(umidi
->chip
->dev
) == USB_SPEED_LOW
)
1355 * Low speed bulk transfers don't exist, so
1356 * force interrupt transfers for devices like
1357 * ESI MIDI Mate that try to use them anyway.
1359 endpoints
[epidx
].out_interval
= 1;
1360 endpoints
[epidx
].out_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
1361 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
1362 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
1364 if (endpoints
[epidx
].in_ep
) {
1365 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
1366 snd_printk(KERN_WARNING
"too many endpoints\n");
1370 endpoints
[epidx
].in_ep
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1371 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
1372 endpoints
[epidx
].in_interval
= ep
->bInterval
;
1373 else if (snd_usb_get_speed(umidi
->chip
->dev
) == USB_SPEED_LOW
)
1374 endpoints
[epidx
].in_interval
= 1;
1375 endpoints
[epidx
].in_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
1376 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
1377 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
1384 * On Roland devices, use the second alternate setting to be able to use
1385 * the interrupt input endpoint.
1387 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi
* umidi
)
1389 struct usb_interface
* intf
;
1390 struct usb_host_interface
*hostif
;
1391 struct usb_interface_descriptor
* intfd
;
1393 intf
= umidi
->iface
;
1394 if (!intf
|| intf
->num_altsetting
!= 2)
1397 hostif
= &intf
->altsetting
[1];
1398 intfd
= get_iface_desc(hostif
);
1399 if (intfd
->bNumEndpoints
!= 2 ||
1400 (get_endpoint(hostif
, 0)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
||
1401 (get_endpoint(hostif
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1404 snd_printdd(KERN_INFO
"switching to altsetting %d with int ep\n",
1405 intfd
->bAlternateSetting
);
1406 usb_set_interface(umidi
->chip
->dev
, intfd
->bInterfaceNumber
,
1407 intfd
->bAlternateSetting
);
1411 * Try to find any usable endpoints in the interface.
1413 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi
* umidi
,
1414 struct snd_usb_midi_endpoint_info
* endpoint
,
1417 struct usb_interface
* intf
;
1418 struct usb_host_interface
*hostif
;
1419 struct usb_interface_descriptor
* intfd
;
1420 struct usb_endpoint_descriptor
* epd
;
1421 int i
, out_eps
= 0, in_eps
= 0;
1423 if (USB_ID_VENDOR(umidi
->chip
->usb_id
) == 0x0582)
1424 snd_usbmidi_switch_roland_altsetting(umidi
);
1426 if (endpoint
[0].out_ep
|| endpoint
[0].in_ep
)
1429 intf
= umidi
->iface
;
1430 if (!intf
|| intf
->num_altsetting
< 1)
1432 hostif
= intf
->cur_altsetting
;
1433 intfd
= get_iface_desc(hostif
);
1435 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
1436 epd
= get_endpoint(hostif
, i
);
1437 if ((epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
&&
1438 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1440 if (out_eps
< max_endpoints
&&
1441 (epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1442 endpoint
[out_eps
].out_ep
= epd
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1443 if ((epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
1444 endpoint
[out_eps
].out_interval
= epd
->bInterval
;
1447 if (in_eps
< max_endpoints
&&
1448 (epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
) {
1449 endpoint
[in_eps
].in_ep
= epd
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1450 if ((epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
1451 endpoint
[in_eps
].in_interval
= epd
->bInterval
;
1455 return (out_eps
|| in_eps
) ? 0 : -ENOENT
;
1459 * Detects the endpoints for one-port-per-endpoint protocols.
1461 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi
* umidi
,
1462 struct snd_usb_midi_endpoint_info
* endpoints
)
1466 err
= snd_usbmidi_detect_endpoints(umidi
, endpoints
, MIDI_MAX_ENDPOINTS
);
1467 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1468 if (endpoints
[i
].out_ep
)
1469 endpoints
[i
].out_cables
= 0x0001;
1470 if (endpoints
[i
].in_ep
)
1471 endpoints
[i
].in_cables
= 0x0001;
1477 * Detects the endpoints and ports of Yamaha devices.
1479 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi
* umidi
,
1480 struct snd_usb_midi_endpoint_info
* endpoint
)
1482 struct usb_interface
* intf
;
1483 struct usb_host_interface
*hostif
;
1484 struct usb_interface_descriptor
* intfd
;
1487 intf
= umidi
->iface
;
1490 hostif
= intf
->altsetting
;
1491 intfd
= get_iface_desc(hostif
);
1492 if (intfd
->bNumEndpoints
< 1)
1496 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1497 * necessarily with any useful contents. So simply count 'em.
1499 for (cs_desc
= hostif
->extra
;
1500 cs_desc
< hostif
->extra
+ hostif
->extralen
&& cs_desc
[0] >= 2;
1501 cs_desc
+= cs_desc
[0]) {
1502 if (cs_desc
[1] == USB_DT_CS_INTERFACE
) {
1503 if (cs_desc
[2] == MIDI_IN_JACK
)
1504 endpoint
->in_cables
= (endpoint
->in_cables
<< 1) | 1;
1505 else if (cs_desc
[2] == MIDI_OUT_JACK
)
1506 endpoint
->out_cables
= (endpoint
->out_cables
<< 1) | 1;
1509 if (!endpoint
->in_cables
&& !endpoint
->out_cables
)
1512 return snd_usbmidi_detect_endpoints(umidi
, endpoint
, 1);
1516 * Creates the endpoints and their ports for Midiman devices.
1518 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi
* umidi
,
1519 struct snd_usb_midi_endpoint_info
* endpoint
)
1521 struct snd_usb_midi_endpoint_info ep_info
;
1522 struct usb_interface
* intf
;
1523 struct usb_host_interface
*hostif
;
1524 struct usb_interface_descriptor
* intfd
;
1525 struct usb_endpoint_descriptor
* epd
;
1528 intf
= umidi
->iface
;
1531 hostif
= intf
->altsetting
;
1532 intfd
= get_iface_desc(hostif
);
1534 * The various MidiSport devices have more or less random endpoint
1535 * numbers, so we have to identify the endpoints by their index in
1536 * the descriptor array, like the driver for that other OS does.
1538 * There is one interrupt input endpoint for all input ports, one
1539 * bulk output endpoint for even-numbered ports, and one for odd-
1540 * numbered ports. Both bulk output endpoints have corresponding
1541 * input bulk endpoints (at indices 1 and 3) which aren't used.
1543 if (intfd
->bNumEndpoints
< (endpoint
->out_cables
> 0x0001 ? 5 : 3)) {
1544 snd_printdd(KERN_ERR
"not enough endpoints\n");
1548 epd
= get_endpoint(hostif
, 0);
1549 if ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_IN
||
1550 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
) {
1551 snd_printdd(KERN_ERR
"endpoint[0] isn't interrupt\n");
1554 epd
= get_endpoint(hostif
, 2);
1555 if ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_OUT
||
1556 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
) {
1557 snd_printdd(KERN_ERR
"endpoint[2] isn't bulk output\n");
1560 if (endpoint
->out_cables
> 0x0001) {
1561 epd
= get_endpoint(hostif
, 4);
1562 if ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_OUT
||
1563 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
) {
1564 snd_printdd(KERN_ERR
"endpoint[4] isn't bulk output\n");
1569 ep_info
.out_ep
= get_endpoint(hostif
, 2)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1570 ep_info
.out_cables
= endpoint
->out_cables
& 0x5555;
1571 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1575 ep_info
.in_ep
= get_endpoint(hostif
, 0)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1576 ep_info
.in_interval
= get_endpoint(hostif
, 0)->bInterval
;
1577 ep_info
.in_cables
= endpoint
->in_cables
;
1578 err
= snd_usbmidi_in_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1582 if (endpoint
->out_cables
> 0x0001) {
1583 ep_info
.out_ep
= get_endpoint(hostif
, 4)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1584 ep_info
.out_cables
= endpoint
->out_cables
& 0xaaaa;
1585 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[1]);
1590 for (cable
= 0; cable
< 0x10; ++cable
) {
1591 if (endpoint
->out_cables
& (1 << cable
))
1592 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, cable
,
1593 &umidi
->endpoints
[cable
& 1].out
->ports
[cable
].substream
);
1594 if (endpoint
->in_cables
& (1 << cable
))
1595 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, cable
,
1596 &umidi
->endpoints
[0].in
->ports
[cable
].substream
);
1601 static struct snd_rawmidi_global_ops snd_usbmidi_ops
= {
1602 .get_port_info
= snd_usbmidi_get_port_info
,
1605 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi
* umidi
,
1606 int out_ports
, int in_ports
)
1608 struct snd_rawmidi
*rmidi
;
1611 err
= snd_rawmidi_new(umidi
->chip
->card
, "USB MIDI",
1612 umidi
->chip
->next_midi_device
++,
1613 out_ports
, in_ports
, &rmidi
);
1616 strcpy(rmidi
->name
, umidi
->chip
->card
->shortname
);
1617 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
1618 SNDRV_RAWMIDI_INFO_INPUT
|
1619 SNDRV_RAWMIDI_INFO_DUPLEX
;
1620 rmidi
->ops
= &snd_usbmidi_ops
;
1621 rmidi
->private_data
= umidi
;
1622 rmidi
->private_free
= snd_usbmidi_rawmidi_free
;
1623 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_usbmidi_output_ops
);
1624 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_usbmidi_input_ops
);
1626 umidi
->rmidi
= rmidi
;
1631 * Temporarily stop input.
1633 void snd_usbmidi_input_stop(struct list_head
* p
)
1635 struct snd_usb_midi
* umidi
;
1638 umidi
= list_entry(p
, struct snd_usb_midi
, list
);
1639 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1640 struct snd_usb_midi_endpoint
* ep
= &umidi
->endpoints
[i
];
1642 usb_kill_urb(ep
->in
->urb
);
1646 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint
* ep
)
1649 struct urb
* urb
= ep
->urb
;
1650 urb
->dev
= ep
->umidi
->chip
->dev
;
1651 snd_usbmidi_submit_urb(urb
, GFP_KERNEL
);
1656 * Resume input after a call to snd_usbmidi_input_stop().
1658 void snd_usbmidi_input_start(struct list_head
* p
)
1660 struct snd_usb_midi
* umidi
;
1663 umidi
= list_entry(p
, struct snd_usb_midi
, list
);
1664 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
1665 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
1669 * Creates and registers everything needed for a MIDI streaming interface.
1671 int snd_usb_create_midi_interface(struct snd_usb_audio
* chip
,
1672 struct usb_interface
* iface
,
1673 const struct snd_usb_audio_quirk
* quirk
)
1675 struct snd_usb_midi
* umidi
;
1676 struct snd_usb_midi_endpoint_info endpoints
[MIDI_MAX_ENDPOINTS
];
1677 int out_ports
, in_ports
;
1680 umidi
= kzalloc(sizeof(*umidi
), GFP_KERNEL
);
1684 umidi
->iface
= iface
;
1685 umidi
->quirk
= quirk
;
1686 umidi
->usb_protocol_ops
= &snd_usbmidi_standard_ops
;
1687 init_timer(&umidi
->error_timer
);
1688 umidi
->error_timer
.function
= snd_usbmidi_error_timer
;
1689 umidi
->error_timer
.data
= (unsigned long)umidi
;
1691 /* detect the endpoint(s) to use */
1692 memset(endpoints
, 0, sizeof(endpoints
));
1693 switch (quirk
? quirk
->type
: QUIRK_MIDI_STANDARD_INTERFACE
) {
1694 case QUIRK_MIDI_STANDARD_INTERFACE
:
1695 err
= snd_usbmidi_get_ms_info(umidi
, endpoints
);
1696 if (chip
->usb_id
== USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1697 umidi
->usb_protocol_ops
=
1698 &snd_usbmidi_maudio_broken_running_status_ops
;
1700 case QUIRK_MIDI_FIXED_ENDPOINT
:
1701 memcpy(&endpoints
[0], quirk
->data
,
1702 sizeof(struct snd_usb_midi_endpoint_info
));
1703 err
= snd_usbmidi_detect_endpoints(umidi
, &endpoints
[0], 1);
1705 case QUIRK_MIDI_YAMAHA
:
1706 err
= snd_usbmidi_detect_yamaha(umidi
, &endpoints
[0]);
1708 case QUIRK_MIDI_MIDIMAN
:
1709 umidi
->usb_protocol_ops
= &snd_usbmidi_midiman_ops
;
1710 memcpy(&endpoints
[0], quirk
->data
,
1711 sizeof(struct snd_usb_midi_endpoint_info
));
1714 case QUIRK_MIDI_NOVATION
:
1715 umidi
->usb_protocol_ops
= &snd_usbmidi_novation_ops
;
1716 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
1718 case QUIRK_MIDI_RAW
:
1719 umidi
->usb_protocol_ops
= &snd_usbmidi_raw_ops
;
1720 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
1722 case QUIRK_MIDI_EMAGIC
:
1723 umidi
->usb_protocol_ops
= &snd_usbmidi_emagic_ops
;
1724 memcpy(&endpoints
[0], quirk
->data
,
1725 sizeof(struct snd_usb_midi_endpoint_info
));
1726 err
= snd_usbmidi_detect_endpoints(umidi
, &endpoints
[0], 1);
1728 case QUIRK_MIDI_CME
:
1729 umidi
->usb_protocol_ops
= &snd_usbmidi_cme_ops
;
1730 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
1733 snd_printd(KERN_ERR
"invalid quirk type %d\n", quirk
->type
);
1742 /* create rawmidi device */
1745 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1746 out_ports
+= snd_usbmidi_count_bits(endpoints
[i
].out_cables
);
1747 in_ports
+= snd_usbmidi_count_bits(endpoints
[i
].in_cables
);
1749 err
= snd_usbmidi_create_rawmidi(umidi
, out_ports
, in_ports
);
1755 /* create endpoint/port structures */
1756 if (quirk
&& quirk
->type
== QUIRK_MIDI_MIDIMAN
)
1757 err
= snd_usbmidi_create_endpoints_midiman(umidi
, &endpoints
[0]);
1759 err
= snd_usbmidi_create_endpoints(umidi
, endpoints
);
1761 snd_usbmidi_free(umidi
);
1765 list_add(&umidi
->list
, &umidi
->chip
->midi_list
);
1767 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
1768 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
1772 EXPORT_SYMBOL(snd_usb_create_midi_interface
);
1773 EXPORT_SYMBOL(snd_usbmidi_input_stop
);
1774 EXPORT_SYMBOL(snd_usbmidi_input_start
);
1775 EXPORT_SYMBOL(snd_usbmidi_disconnect
);