2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2009 Clemens Ladisch
7 * Based on the OSS usb-midi driver by NAGANO Daisuke,
8 * NetBSD's umidi driver by Takuya SHIOZAKI,
9 * the "USB Device Class Definition for MIDI Devices" by Roland
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * Alternatively, this software may be distributed and/or modified under the
21 * terms of the GNU General Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at your option) any later
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <linux/kernel.h>
39 #include <linux/types.h>
40 #include <linux/bitops.h>
41 #include <linux/interrupt.h>
42 #include <linux/spinlock.h>
43 #include <linux/string.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/timer.h>
47 #include <linux/usb.h>
48 #include <linux/wait.h>
49 #include <sound/core.h>
50 #include <sound/control.h>
51 #include <sound/rawmidi.h>
52 #include <sound/asequencer.h>
57 * define this to log all USB packets
59 /* #define DUMP_PACKETS */
62 * how long to wait after some USB errors, so that khubd can disconnect() us
63 * without too many spurious errors
65 #define ERROR_DELAY_JIFFIES (HZ / 10)
71 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
72 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
73 MODULE_LICENSE("Dual BSD/GPL");
76 struct usb_ms_header_descriptor
{
79 __u8 bDescriptorSubtype
;
82 } __attribute__ ((packed
));
84 struct usb_ms_endpoint_descriptor
{
87 __u8 bDescriptorSubtype
;
89 __u8 baAssocJackID
[0];
90 } __attribute__ ((packed
));
92 struct snd_usb_midi_in_endpoint
;
93 struct snd_usb_midi_out_endpoint
;
94 struct snd_usb_midi_endpoint
;
96 struct usb_protocol_ops
{
97 void (*input
)(struct snd_usb_midi_in_endpoint
*, uint8_t*, int);
98 void (*output
)(struct snd_usb_midi_out_endpoint
*ep
, struct urb
*urb
);
99 void (*output_packet
)(struct urb
*, uint8_t, uint8_t, uint8_t, uint8_t);
100 void (*init_out_endpoint
)(struct snd_usb_midi_out_endpoint
*);
101 void (*finish_out_endpoint
)(struct snd_usb_midi_out_endpoint
*);
104 struct snd_usb_midi
{
105 struct usb_device
*dev
;
106 struct snd_card
*card
;
107 struct usb_interface
*iface
;
108 const struct snd_usb_audio_quirk
*quirk
;
109 struct snd_rawmidi
*rmidi
;
110 struct usb_protocol_ops
* usb_protocol_ops
;
111 struct list_head list
;
112 struct timer_list error_timer
;
113 spinlock_t disc_lock
;
116 int next_midi_device
;
118 struct snd_usb_midi_endpoint
{
119 struct snd_usb_midi_out_endpoint
*out
;
120 struct snd_usb_midi_in_endpoint
*in
;
121 } endpoints
[MIDI_MAX_ENDPOINTS
];
122 unsigned long input_triggered
;
124 unsigned char disconnected
;
126 struct snd_kcontrol
*roland_load_ctl
;
129 struct snd_usb_midi_out_endpoint
{
130 struct snd_usb_midi
* umidi
;
131 struct out_urb_context
{
133 struct snd_usb_midi_out_endpoint
*ep
;
135 unsigned int active_urbs
;
136 unsigned int drain_urbs
;
137 int max_transfer
; /* size of urb buffer */
138 struct tasklet_struct tasklet
;
139 unsigned int next_urb
;
140 spinlock_t buffer_lock
;
142 struct usbmidi_out_port
{
143 struct snd_usb_midi_out_endpoint
* ep
;
144 struct snd_rawmidi_substream
*substream
;
146 uint8_t cable
; /* cable number << 4 */
148 #define STATE_UNKNOWN 0
149 #define STATE_1PARAM 1
150 #define STATE_2PARAM_1 2
151 #define STATE_2PARAM_2 3
152 #define STATE_SYSEX_0 4
153 #define STATE_SYSEX_1 5
154 #define STATE_SYSEX_2 6
159 wait_queue_head_t drain_wait
;
162 struct snd_usb_midi_in_endpoint
{
163 struct snd_usb_midi
* umidi
;
164 struct urb
* urbs
[INPUT_URBS
];
165 struct usbmidi_in_port
{
166 struct snd_rawmidi_substream
*substream
;
167 u8 running_status_length
;
174 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint
* ep
);
176 static const uint8_t snd_usbmidi_cin_length
[] = {
177 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
181 * Submits the URB, with error handling.
183 static int snd_usbmidi_submit_urb(struct urb
* urb
, gfp_t flags
)
185 int err
= usb_submit_urb(urb
, flags
);
186 if (err
< 0 && err
!= -ENODEV
)
187 snd_printk(KERN_ERR
"usb_submit_urb: %d\n", err
);
192 * Error handling for URB completion functions.
194 static int snd_usbmidi_urb_error(int status
)
197 /* manually unlinked, or device gone */
203 /* errors that might occur during unplugging */
209 snd_printk(KERN_ERR
"urb status %d\n", status
);
210 return 0; /* continue */
215 * Receives a chunk of MIDI data.
217 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint
* ep
, int portidx
,
218 uint8_t* data
, int length
)
220 struct usbmidi_in_port
* port
= &ep
->ports
[portidx
];
222 if (!port
->substream
) {
223 snd_printd("unexpected port %d!\n", portidx
);
226 if (!test_bit(port
->substream
->number
, &ep
->umidi
->input_triggered
))
228 snd_rawmidi_receive(port
->substream
, data
, length
);
232 static void dump_urb(const char *type
, const u8
*data
, int length
)
234 snd_printk(KERN_DEBUG
"%s packet: [", type
);
235 for (; length
> 0; ++data
, --length
)
236 printk(" %02x", *data
);
240 #define dump_urb(type, data, length) /* nothing */
244 * Processes the data read from the device.
246 static void snd_usbmidi_in_urb_complete(struct urb
* urb
)
248 struct snd_usb_midi_in_endpoint
* ep
= urb
->context
;
250 if (urb
->status
== 0) {
251 dump_urb("received", urb
->transfer_buffer
, urb
->actual_length
);
252 ep
->umidi
->usb_protocol_ops
->input(ep
, urb
->transfer_buffer
,
255 int err
= snd_usbmidi_urb_error(urb
->status
);
257 if (err
!= -ENODEV
) {
258 ep
->error_resubmit
= 1;
259 mod_timer(&ep
->umidi
->error_timer
,
260 jiffies
+ ERROR_DELAY_JIFFIES
);
266 urb
->dev
= ep
->umidi
->dev
;
267 snd_usbmidi_submit_urb(urb
, GFP_ATOMIC
);
270 static void snd_usbmidi_out_urb_complete(struct urb
* urb
)
272 struct out_urb_context
*context
= urb
->context
;
273 struct snd_usb_midi_out_endpoint
* ep
= context
->ep
;
274 unsigned int urb_index
;
276 spin_lock(&ep
->buffer_lock
);
277 urb_index
= context
- ep
->urbs
;
278 ep
->active_urbs
&= ~(1 << urb_index
);
279 if (unlikely(ep
->drain_urbs
)) {
280 ep
->drain_urbs
&= ~(1 << urb_index
);
281 wake_up(&ep
->drain_wait
);
283 spin_unlock(&ep
->buffer_lock
);
284 if (urb
->status
< 0) {
285 int err
= snd_usbmidi_urb_error(urb
->status
);
288 mod_timer(&ep
->umidi
->error_timer
,
289 jiffies
+ ERROR_DELAY_JIFFIES
);
293 snd_usbmidi_do_output(ep
);
297 * This is called when some data should be transferred to the device
298 * (from one or more substreams).
300 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint
* ep
)
302 unsigned int urb_index
;
306 spin_lock_irqsave(&ep
->buffer_lock
, flags
);
307 if (ep
->umidi
->disconnected
) {
308 spin_unlock_irqrestore(&ep
->buffer_lock
, flags
);
312 urb_index
= ep
->next_urb
;
314 if (!(ep
->active_urbs
& (1 << urb_index
))) {
315 urb
= ep
->urbs
[urb_index
].urb
;
316 urb
->transfer_buffer_length
= 0;
317 ep
->umidi
->usb_protocol_ops
->output(ep
, urb
);
318 if (urb
->transfer_buffer_length
== 0)
321 dump_urb("sending", urb
->transfer_buffer
,
322 urb
->transfer_buffer_length
);
323 urb
->dev
= ep
->umidi
->dev
;
324 if (snd_usbmidi_submit_urb(urb
, GFP_ATOMIC
) < 0)
326 ep
->active_urbs
|= 1 << urb_index
;
328 if (++urb_index
>= OUTPUT_URBS
)
330 if (urb_index
== ep
->next_urb
)
333 ep
->next_urb
= urb_index
;
334 spin_unlock_irqrestore(&ep
->buffer_lock
, flags
);
337 static void snd_usbmidi_out_tasklet(unsigned long data
)
339 struct snd_usb_midi_out_endpoint
* ep
= (struct snd_usb_midi_out_endpoint
*) data
;
341 snd_usbmidi_do_output(ep
);
344 /* called after transfers had been interrupted due to some USB error */
345 static void snd_usbmidi_error_timer(unsigned long data
)
347 struct snd_usb_midi
*umidi
= (struct snd_usb_midi
*)data
;
350 spin_lock(&umidi
->disc_lock
);
351 if (umidi
->disconnected
) {
352 spin_unlock(&umidi
->disc_lock
);
355 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
356 struct snd_usb_midi_in_endpoint
*in
= umidi
->endpoints
[i
].in
;
357 if (in
&& in
->error_resubmit
) {
358 in
->error_resubmit
= 0;
359 for (j
= 0; j
< INPUT_URBS
; ++j
) {
360 in
->urbs
[j
]->dev
= umidi
->dev
;
361 snd_usbmidi_submit_urb(in
->urbs
[j
], GFP_ATOMIC
);
364 if (umidi
->endpoints
[i
].out
)
365 snd_usbmidi_do_output(umidi
->endpoints
[i
].out
);
367 spin_unlock(&umidi
->disc_lock
);
370 /* helper function to send static data that may not DMA-able */
371 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint
* ep
,
372 const void *data
, int len
)
375 void *buf
= kmemdup(data
, len
, GFP_KERNEL
);
378 dump_urb("sending", buf
, len
);
380 err
= usb_bulk_msg(ep
->umidi
->dev
, ep
->urbs
[0].urb
->pipe
,
381 buf
, len
, NULL
, 250);
387 * Standard USB MIDI protocol: see the spec.
388 * Midiman protocol: like the standard protocol, but the control byte is the
389 * fourth byte in each packet, and uses length instead of CIN.
392 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint
* ep
,
393 uint8_t* buffer
, int buffer_length
)
397 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
398 if (buffer
[i
] != 0) {
399 int cable
= buffer
[i
] >> 4;
400 int length
= snd_usbmidi_cin_length
[buffer
[i
] & 0x0f];
401 snd_usbmidi_input_data(ep
, cable
, &buffer
[i
+ 1], length
);
405 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint
* ep
,
406 uint8_t* buffer
, int buffer_length
)
410 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
411 if (buffer
[i
+ 3] != 0) {
412 int port
= buffer
[i
+ 3] >> 4;
413 int length
= buffer
[i
+ 3] & 3;
414 snd_usbmidi_input_data(ep
, port
, &buffer
[i
], length
);
419 * Buggy M-Audio device: running status on input results in a packet that has
420 * the data bytes but not the status byte and that is marked with CIN 4.
422 static void snd_usbmidi_maudio_broken_running_status_input(
423 struct snd_usb_midi_in_endpoint
* ep
,
424 uint8_t* buffer
, int buffer_length
)
428 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
429 if (buffer
[i
] != 0) {
430 int cable
= buffer
[i
] >> 4;
431 u8 cin
= buffer
[i
] & 0x0f;
432 struct usbmidi_in_port
*port
= &ep
->ports
[cable
];
435 length
= snd_usbmidi_cin_length
[cin
];
436 if (cin
== 0xf && buffer
[i
+ 1] >= 0xf8)
437 ; /* realtime msg: no running status change */
438 else if (cin
>= 0x8 && cin
<= 0xe)
440 port
->running_status_length
= length
- 1;
441 else if (cin
== 0x4 &&
442 port
->running_status_length
!= 0 &&
443 buffer
[i
+ 1] < 0x80)
444 /* CIN 4 that is not a SysEx */
445 length
= port
->running_status_length
;
448 * All other msgs cannot begin running status.
449 * (A channel msg sent as two or three CIN 0xF
450 * packets could in theory, but this device
451 * doesn't use this format.)
453 port
->running_status_length
= 0;
454 snd_usbmidi_input_data(ep
, cable
, &buffer
[i
+ 1], length
);
459 * CME protocol: like the standard protocol, but SysEx commands are sent as a
460 * single USB packet preceded by a 0x0F byte.
462 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint
*ep
,
463 uint8_t *buffer
, int buffer_length
)
465 if (buffer_length
< 2 || (buffer
[0] & 0x0f) != 0x0f)
466 snd_usbmidi_standard_input(ep
, buffer
, buffer_length
);
468 snd_usbmidi_input_data(ep
, buffer
[0] >> 4,
469 &buffer
[1], buffer_length
- 1);
473 * Adds one USB MIDI packet to the output buffer.
475 static void snd_usbmidi_output_standard_packet(struct urb
* urb
, uint8_t p0
,
476 uint8_t p1
, uint8_t p2
, uint8_t p3
)
479 uint8_t* buf
= (uint8_t*)urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
484 urb
->transfer_buffer_length
+= 4;
488 * Adds one Midiman packet to the output buffer.
490 static void snd_usbmidi_output_midiman_packet(struct urb
* urb
, uint8_t p0
,
491 uint8_t p1
, uint8_t p2
, uint8_t p3
)
494 uint8_t* buf
= (uint8_t*)urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
498 buf
[3] = (p0
& 0xf0) | snd_usbmidi_cin_length
[p0
& 0x0f];
499 urb
->transfer_buffer_length
+= 4;
503 * Converts MIDI commands to USB MIDI packets.
505 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port
* port
,
506 uint8_t b
, struct urb
* urb
)
508 uint8_t p0
= port
->cable
;
509 void (*output_packet
)(struct urb
*, uint8_t, uint8_t, uint8_t, uint8_t) =
510 port
->ep
->umidi
->usb_protocol_ops
->output_packet
;
513 output_packet(urb
, p0
| 0x0f, b
, 0, 0);
514 } else if (b
>= 0xf0) {
518 port
->state
= STATE_SYSEX_1
;
523 port
->state
= STATE_1PARAM
;
527 port
->state
= STATE_2PARAM_1
;
531 port
->state
= STATE_UNKNOWN
;
534 output_packet(urb
, p0
| 0x05, 0xf6, 0, 0);
535 port
->state
= STATE_UNKNOWN
;
538 switch (port
->state
) {
540 output_packet(urb
, p0
| 0x05, 0xf7, 0, 0);
543 output_packet(urb
, p0
| 0x06, port
->data
[0], 0xf7, 0);
546 output_packet(urb
, p0
| 0x07, port
->data
[0], port
->data
[1], 0xf7);
549 port
->state
= STATE_UNKNOWN
;
552 } else if (b
>= 0x80) {
554 if (b
>= 0xc0 && b
<= 0xdf)
555 port
->state
= STATE_1PARAM
;
557 port
->state
= STATE_2PARAM_1
;
558 } else { /* b < 0x80 */
559 switch (port
->state
) {
561 if (port
->data
[0] < 0xf0) {
562 p0
|= port
->data
[0] >> 4;
565 port
->state
= STATE_UNKNOWN
;
567 output_packet(urb
, p0
, port
->data
[0], b
, 0);
571 port
->state
= STATE_2PARAM_2
;
574 if (port
->data
[0] < 0xf0) {
575 p0
|= port
->data
[0] >> 4;
576 port
->state
= STATE_2PARAM_1
;
579 port
->state
= STATE_UNKNOWN
;
581 output_packet(urb
, p0
, port
->data
[0], port
->data
[1], b
);
585 port
->state
= STATE_SYSEX_1
;
589 port
->state
= STATE_SYSEX_2
;
592 output_packet(urb
, p0
| 0x04, port
->data
[0], port
->data
[1], b
);
593 port
->state
= STATE_SYSEX_0
;
599 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint
* ep
,
604 /* FIXME: lower-numbered ports can starve higher-numbered ports */
605 for (p
= 0; p
< 0x10; ++p
) {
606 struct usbmidi_out_port
* port
= &ep
->ports
[p
];
609 while (urb
->transfer_buffer_length
+ 3 < ep
->max_transfer
) {
611 if (snd_rawmidi_transmit(port
->substream
, &b
, 1) != 1) {
615 snd_usbmidi_transmit_byte(port
, b
, urb
);
620 static struct usb_protocol_ops snd_usbmidi_standard_ops
= {
621 .input
= snd_usbmidi_standard_input
,
622 .output
= snd_usbmidi_standard_output
,
623 .output_packet
= snd_usbmidi_output_standard_packet
,
626 static struct usb_protocol_ops snd_usbmidi_midiman_ops
= {
627 .input
= snd_usbmidi_midiman_input
,
628 .output
= snd_usbmidi_standard_output
,
629 .output_packet
= snd_usbmidi_output_midiman_packet
,
632 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops
= {
633 .input
= snd_usbmidi_maudio_broken_running_status_input
,
634 .output
= snd_usbmidi_standard_output
,
635 .output_packet
= snd_usbmidi_output_standard_packet
,
638 static struct usb_protocol_ops snd_usbmidi_cme_ops
= {
639 .input
= snd_usbmidi_cme_input
,
640 .output
= snd_usbmidi_standard_output
,
641 .output_packet
= snd_usbmidi_output_standard_packet
,
645 * Novation USB MIDI protocol: number of data bytes is in the first byte
646 * (when receiving) (+1!) or in the second byte (when sending); data begins
650 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint
* ep
,
651 uint8_t* buffer
, int buffer_length
)
653 if (buffer_length
< 2 || !buffer
[0] || buffer_length
< buffer
[0] + 1)
655 snd_usbmidi_input_data(ep
, 0, &buffer
[2], buffer
[0] - 1);
658 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint
* ep
,
661 uint8_t* transfer_buffer
;
664 if (!ep
->ports
[0].active
)
666 transfer_buffer
= urb
->transfer_buffer
;
667 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
669 ep
->max_transfer
- 2);
671 ep
->ports
[0].active
= 0;
674 transfer_buffer
[0] = 0;
675 transfer_buffer
[1] = count
;
676 urb
->transfer_buffer_length
= 2 + count
;
679 static struct usb_protocol_ops snd_usbmidi_novation_ops
= {
680 .input
= snd_usbmidi_novation_input
,
681 .output
= snd_usbmidi_novation_output
,
685 * "raw" protocol: used by the MOTU FastLane.
688 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint
* ep
,
689 uint8_t* buffer
, int buffer_length
)
691 snd_usbmidi_input_data(ep
, 0, buffer
, buffer_length
);
694 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint
* ep
,
699 if (!ep
->ports
[0].active
)
701 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
702 urb
->transfer_buffer
,
705 ep
->ports
[0].active
= 0;
708 urb
->transfer_buffer_length
= count
;
711 static struct usb_protocol_ops snd_usbmidi_raw_ops
= {
712 .input
= snd_usbmidi_raw_input
,
713 .output
= snd_usbmidi_raw_output
,
716 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint
*ep
,
717 uint8_t *buffer
, int buffer_length
)
719 if (buffer_length
!= 9)
722 while (buffer_length
&& buffer
[buffer_length
- 1] == 0xFD)
725 snd_usbmidi_input_data(ep
, 0, buffer
, buffer_length
);
728 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint
*ep
,
733 if (!ep
->ports
[0].active
)
735 count
= snd_usb_get_speed(ep
->umidi
->dev
) == USB_SPEED_HIGH
? 1 : 2;
736 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
737 urb
->transfer_buffer
,
740 ep
->ports
[0].active
= 0;
744 memset(urb
->transfer_buffer
+ count
, 0xFD, 9 - count
);
745 urb
->transfer_buffer_length
= count
;
748 static struct usb_protocol_ops snd_usbmidi_122l_ops
= {
749 .input
= snd_usbmidi_us122l_input
,
750 .output
= snd_usbmidi_us122l_output
,
754 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
757 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint
* ep
)
759 static const u8 init_data
[] = {
760 /* initialization magic: "get version" */
762 0x00, 0x20, 0x31, /* Emagic */
764 0x0b, /* version number request */
765 0x00, /* command version */
766 0x00, /* EEPROM, box 0 */
769 send_bulk_static_data(ep
, init_data
, sizeof(init_data
));
770 /* while we're at it, pour on more magic */
771 send_bulk_static_data(ep
, init_data
, sizeof(init_data
));
774 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint
* ep
)
776 static const u8 finish_data
[] = {
777 /* switch to patch mode with last preset */
779 0x00, 0x20, 0x31, /* Emagic */
781 0x10, /* patch switch command */
782 0x00, /* command version */
783 0x7f, /* to all boxes */
784 0x40, /* last preset in EEPROM */
787 send_bulk_static_data(ep
, finish_data
, sizeof(finish_data
));
790 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint
* ep
,
791 uint8_t* buffer
, int buffer_length
)
795 /* FF indicates end of valid data */
796 for (i
= 0; i
< buffer_length
; ++i
)
797 if (buffer
[i
] == 0xff) {
802 /* handle F5 at end of last buffer */
806 while (buffer_length
> 0) {
807 /* determine size of data until next F5 */
808 for (i
= 0; i
< buffer_length
; ++i
)
809 if (buffer
[i
] == 0xf5)
811 snd_usbmidi_input_data(ep
, ep
->current_port
, buffer
, i
);
815 if (buffer_length
<= 0)
817 /* assert(buffer[0] == 0xf5); */
823 if (buffer_length
<= 0)
825 if (buffer
[0] < 0x80) {
826 ep
->current_port
= (buffer
[0] - 1) & 15;
834 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint
* ep
,
837 int port0
= ep
->current_port
;
838 uint8_t* buf
= urb
->transfer_buffer
;
839 int buf_free
= ep
->max_transfer
;
842 for (i
= 0; i
< 0x10; ++i
) {
843 /* round-robin, starting at the last current port */
844 int portnum
= (port0
+ i
) & 15;
845 struct usbmidi_out_port
* port
= &ep
->ports
[portnum
];
849 if (snd_rawmidi_transmit_peek(port
->substream
, buf
, 1) != 1) {
854 if (portnum
!= ep
->current_port
) {
857 ep
->current_port
= portnum
;
859 buf
[1] = (portnum
+ 1) & 15;
866 length
= snd_rawmidi_transmit(port
->substream
, buf
, buf_free
);
874 if (buf_free
< ep
->max_transfer
&& buf_free
> 0) {
878 urb
->transfer_buffer_length
= ep
->max_transfer
- buf_free
;
881 static struct usb_protocol_ops snd_usbmidi_emagic_ops
= {
882 .input
= snd_usbmidi_emagic_input
,
883 .output
= snd_usbmidi_emagic_output
,
884 .init_out_endpoint
= snd_usbmidi_emagic_init_out
,
885 .finish_out_endpoint
= snd_usbmidi_emagic_finish_out
,
889 static void update_roland_altsetting(struct snd_usb_midi
* umidi
)
891 struct usb_interface
*intf
;
892 struct usb_host_interface
*hostif
;
893 struct usb_interface_descriptor
*intfd
;
897 is_light_load
= intf
->cur_altsetting
!= intf
->altsetting
;
898 if (umidi
->roland_load_ctl
->private_value
== is_light_load
)
900 hostif
= &intf
->altsetting
[umidi
->roland_load_ctl
->private_value
];
901 intfd
= get_iface_desc(hostif
);
902 snd_usbmidi_input_stop(&umidi
->list
);
903 usb_set_interface(umidi
->dev
, intfd
->bInterfaceNumber
,
904 intfd
->bAlternateSetting
);
905 snd_usbmidi_input_start(&umidi
->list
);
908 static void substream_open(struct snd_rawmidi_substream
*substream
, int open
)
910 struct snd_usb_midi
* umidi
= substream
->rmidi
->private_data
;
911 struct snd_kcontrol
*ctl
;
913 mutex_lock(&umidi
->mutex
);
915 if (umidi
->opened
++ == 0 && umidi
->roland_load_ctl
) {
916 ctl
= umidi
->roland_load_ctl
;
917 ctl
->vd
[0].access
|= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
918 snd_ctl_notify(umidi
->card
,
919 SNDRV_CTL_EVENT_MASK_INFO
, &ctl
->id
);
920 update_roland_altsetting(umidi
);
923 if (--umidi
->opened
== 0 && umidi
->roland_load_ctl
) {
924 ctl
= umidi
->roland_load_ctl
;
925 ctl
->vd
[0].access
&= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
926 snd_ctl_notify(umidi
->card
,
927 SNDRV_CTL_EVENT_MASK_INFO
, &ctl
->id
);
930 mutex_unlock(&umidi
->mutex
);
933 static int snd_usbmidi_output_open(struct snd_rawmidi_substream
*substream
)
935 struct snd_usb_midi
* umidi
= substream
->rmidi
->private_data
;
936 struct usbmidi_out_port
* port
= NULL
;
939 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
940 if (umidi
->endpoints
[i
].out
)
941 for (j
= 0; j
< 0x10; ++j
)
942 if (umidi
->endpoints
[i
].out
->ports
[j
].substream
== substream
) {
943 port
= &umidi
->endpoints
[i
].out
->ports
[j
];
950 substream
->runtime
->private_data
= port
;
951 port
->state
= STATE_UNKNOWN
;
952 substream_open(substream
, 1);
956 static int snd_usbmidi_output_close(struct snd_rawmidi_substream
*substream
)
958 substream_open(substream
, 0);
962 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
964 struct usbmidi_out_port
* port
= (struct usbmidi_out_port
*)substream
->runtime
->private_data
;
968 if (port
->ep
->umidi
->disconnected
) {
969 /* gobble up remaining bytes to prevent wait in
970 * snd_rawmidi_drain_output */
971 while (!snd_rawmidi_transmit_empty(substream
))
972 snd_rawmidi_transmit_ack(substream
, 1);
975 tasklet_schedule(&port
->ep
->tasklet
);
979 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream
*substream
)
981 struct usbmidi_out_port
* port
= substream
->runtime
->private_data
;
982 struct snd_usb_midi_out_endpoint
*ep
= port
->ep
;
983 unsigned int drain_urbs
;
985 long timeout
= msecs_to_jiffies(50);
987 if (ep
->umidi
->disconnected
)
990 * The substream buffer is empty, but some data might still be in the
991 * currently active URBs, so we have to wait for those to complete.
993 spin_lock_irq(&ep
->buffer_lock
);
994 drain_urbs
= ep
->active_urbs
;
996 ep
->drain_urbs
|= drain_urbs
;
998 prepare_to_wait(&ep
->drain_wait
, &wait
,
999 TASK_UNINTERRUPTIBLE
);
1000 spin_unlock_irq(&ep
->buffer_lock
);
1001 timeout
= schedule_timeout(timeout
);
1002 spin_lock_irq(&ep
->buffer_lock
);
1003 drain_urbs
&= ep
->drain_urbs
;
1004 } while (drain_urbs
&& timeout
);
1005 finish_wait(&ep
->drain_wait
, &wait
);
1007 spin_unlock_irq(&ep
->buffer_lock
);
1010 static int snd_usbmidi_input_open(struct snd_rawmidi_substream
*substream
)
1012 substream_open(substream
, 1);
1016 static int snd_usbmidi_input_close(struct snd_rawmidi_substream
*substream
)
1018 substream_open(substream
, 0);
1022 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1024 struct snd_usb_midi
* umidi
= substream
->rmidi
->private_data
;
1027 set_bit(substream
->number
, &umidi
->input_triggered
);
1029 clear_bit(substream
->number
, &umidi
->input_triggered
);
1032 static struct snd_rawmidi_ops snd_usbmidi_output_ops
= {
1033 .open
= snd_usbmidi_output_open
,
1034 .close
= snd_usbmidi_output_close
,
1035 .trigger
= snd_usbmidi_output_trigger
,
1036 .drain
= snd_usbmidi_output_drain
,
1039 static struct snd_rawmidi_ops snd_usbmidi_input_ops
= {
1040 .open
= snd_usbmidi_input_open
,
1041 .close
= snd_usbmidi_input_close
,
1042 .trigger
= snd_usbmidi_input_trigger
1045 static void free_urb_and_buffer(struct snd_usb_midi
*umidi
, struct urb
*urb
,
1046 unsigned int buffer_length
)
1048 usb_buffer_free(umidi
->dev
, buffer_length
,
1049 urb
->transfer_buffer
, urb
->transfer_dma
);
1054 * Frees an input endpoint.
1055 * May be called when ep hasn't been initialized completely.
1057 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint
* ep
)
1061 for (i
= 0; i
< INPUT_URBS
; ++i
)
1063 free_urb_and_buffer(ep
->umidi
, ep
->urbs
[i
],
1064 ep
->urbs
[i
]->transfer_buffer_length
);
1069 * Creates an input endpoint.
1071 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi
* umidi
,
1072 struct snd_usb_midi_endpoint_info
* ep_info
,
1073 struct snd_usb_midi_endpoint
* rep
)
1075 struct snd_usb_midi_in_endpoint
* ep
;
1082 ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
1087 for (i
= 0; i
< INPUT_URBS
; ++i
) {
1088 ep
->urbs
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
1090 snd_usbmidi_in_endpoint_delete(ep
);
1094 if (ep_info
->in_interval
)
1095 pipe
= usb_rcvintpipe(umidi
->dev
, ep_info
->in_ep
);
1097 pipe
= usb_rcvbulkpipe(umidi
->dev
, ep_info
->in_ep
);
1098 length
= usb_maxpacket(umidi
->dev
, pipe
, 0);
1099 for (i
= 0; i
< INPUT_URBS
; ++i
) {
1100 buffer
= usb_buffer_alloc(umidi
->dev
, length
, GFP_KERNEL
,
1101 &ep
->urbs
[i
]->transfer_dma
);
1103 snd_usbmidi_in_endpoint_delete(ep
);
1106 if (ep_info
->in_interval
)
1107 usb_fill_int_urb(ep
->urbs
[i
], umidi
->dev
,
1108 pipe
, buffer
, length
,
1109 snd_usbmidi_in_urb_complete
,
1110 ep
, ep_info
->in_interval
);
1112 usb_fill_bulk_urb(ep
->urbs
[i
], umidi
->dev
,
1113 pipe
, buffer
, length
,
1114 snd_usbmidi_in_urb_complete
, ep
);
1115 ep
->urbs
[i
]->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
1123 * Frees an output endpoint.
1124 * May be called when ep hasn't been initialized completely.
1126 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint
*ep
)
1130 for (i
= 0; i
< OUTPUT_URBS
; ++i
)
1131 if (ep
->urbs
[i
].urb
) {
1132 free_urb_and_buffer(ep
->umidi
, ep
->urbs
[i
].urb
,
1134 ep
->urbs
[i
].urb
= NULL
;
1138 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint
*ep
)
1140 snd_usbmidi_out_endpoint_clear(ep
);
1145 * Creates an output endpoint, and initializes output ports.
1147 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi
* umidi
,
1148 struct snd_usb_midi_endpoint_info
* ep_info
,
1149 struct snd_usb_midi_endpoint
* rep
)
1151 struct snd_usb_midi_out_endpoint
* ep
;
1157 ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
1162 for (i
= 0; i
< OUTPUT_URBS
; ++i
) {
1163 ep
->urbs
[i
].urb
= usb_alloc_urb(0, GFP_KERNEL
);
1164 if (!ep
->urbs
[i
].urb
) {
1165 snd_usbmidi_out_endpoint_delete(ep
);
1168 ep
->urbs
[i
].ep
= ep
;
1170 if (ep_info
->out_interval
)
1171 pipe
= usb_sndintpipe(umidi
->dev
, ep_info
->out_ep
);
1173 pipe
= usb_sndbulkpipe(umidi
->dev
, ep_info
->out_ep
);
1174 switch (umidi
->usb_id
) {
1176 ep
->max_transfer
= usb_maxpacket(umidi
->dev
, pipe
, 1);
1179 * Various chips declare a packet size larger than 4 bytes, but
1180 * do not actually work with larger packets:
1182 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1183 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1184 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1185 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1186 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1187 ep
->max_transfer
= 4;
1190 for (i
= 0; i
< OUTPUT_URBS
; ++i
) {
1191 buffer
= usb_buffer_alloc(umidi
->dev
,
1192 ep
->max_transfer
, GFP_KERNEL
,
1193 &ep
->urbs
[i
].urb
->transfer_dma
);
1195 snd_usbmidi_out_endpoint_delete(ep
);
1198 if (ep_info
->out_interval
)
1199 usb_fill_int_urb(ep
->urbs
[i
].urb
, umidi
->dev
,
1200 pipe
, buffer
, ep
->max_transfer
,
1201 snd_usbmidi_out_urb_complete
,
1202 &ep
->urbs
[i
], ep_info
->out_interval
);
1204 usb_fill_bulk_urb(ep
->urbs
[i
].urb
, umidi
->dev
,
1205 pipe
, buffer
, ep
->max_transfer
,
1206 snd_usbmidi_out_urb_complete
,
1208 ep
->urbs
[i
].urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
1211 spin_lock_init(&ep
->buffer_lock
);
1212 tasklet_init(&ep
->tasklet
, snd_usbmidi_out_tasklet
, (unsigned long)ep
);
1213 init_waitqueue_head(&ep
->drain_wait
);
1215 for (i
= 0; i
< 0x10; ++i
)
1216 if (ep_info
->out_cables
& (1 << i
)) {
1217 ep
->ports
[i
].ep
= ep
;
1218 ep
->ports
[i
].cable
= i
<< 4;
1221 if (umidi
->usb_protocol_ops
->init_out_endpoint
)
1222 umidi
->usb_protocol_ops
->init_out_endpoint(ep
);
1231 static void snd_usbmidi_free(struct snd_usb_midi
* umidi
)
1235 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1236 struct snd_usb_midi_endpoint
* ep
= &umidi
->endpoints
[i
];
1238 snd_usbmidi_out_endpoint_delete(ep
->out
);
1240 snd_usbmidi_in_endpoint_delete(ep
->in
);
1242 mutex_destroy(&umidi
->mutex
);
1247 * Unlinks all URBs (must be done before the usb_device is deleted).
1249 void snd_usbmidi_disconnect(struct list_head
* p
)
1251 struct snd_usb_midi
* umidi
;
1254 umidi
= list_entry(p
, struct snd_usb_midi
, list
);
1256 * an URB's completion handler may start the timer and
1257 * a timer may submit an URB. To reliably break the cycle
1258 * a flag under lock must be used
1260 spin_lock_irq(&umidi
->disc_lock
);
1261 umidi
->disconnected
= 1;
1262 spin_unlock_irq(&umidi
->disc_lock
);
1263 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1264 struct snd_usb_midi_endpoint
* ep
= &umidi
->endpoints
[i
];
1266 tasklet_kill(&ep
->out
->tasklet
);
1268 for (j
= 0; j
< OUTPUT_URBS
; ++j
)
1269 usb_kill_urb(ep
->out
->urbs
[j
].urb
);
1270 if (umidi
->usb_protocol_ops
->finish_out_endpoint
)
1271 umidi
->usb_protocol_ops
->finish_out_endpoint(ep
->out
);
1272 ep
->out
->active_urbs
= 0;
1273 if (ep
->out
->drain_urbs
) {
1274 ep
->out
->drain_urbs
= 0;
1275 wake_up(&ep
->out
->drain_wait
);
1279 for (j
= 0; j
< INPUT_URBS
; ++j
)
1280 usb_kill_urb(ep
->in
->urbs
[j
]);
1281 /* free endpoints here; later call can result in Oops */
1283 snd_usbmidi_out_endpoint_clear(ep
->out
);
1285 snd_usbmidi_in_endpoint_delete(ep
->in
);
1289 del_timer_sync(&umidi
->error_timer
);
1292 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi
*rmidi
)
1294 struct snd_usb_midi
* umidi
= rmidi
->private_data
;
1295 snd_usbmidi_free(umidi
);
1298 static struct snd_rawmidi_substream
*snd_usbmidi_find_substream(struct snd_usb_midi
* umidi
,
1299 int stream
, int number
)
1301 struct list_head
* list
;
1303 list_for_each(list
, &umidi
->rmidi
->streams
[stream
].substreams
) {
1304 struct snd_rawmidi_substream
*substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
1305 if (substream
->number
== number
)
1312 * This list specifies names for ports that do not fit into the standard
1313 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1314 * such as internal control or synthesizer ports.
1316 static struct port_info
{
1321 unsigned int seq_flags
;
1322 } snd_usbmidi_port_info
[] = {
1323 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1324 { .id = USB_ID(vendor, product), \
1325 .port = num, .voices = voices_, \
1326 .name = name_, .seq_flags = flags }
1327 #define EXTERNAL_PORT(vendor, product, num, name) \
1328 PORT_INFO(vendor, product, num, name, 0, \
1329 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1330 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1331 SNDRV_SEQ_PORT_TYPE_PORT)
1332 #define CONTROL_PORT(vendor, product, num, name) \
1333 PORT_INFO(vendor, product, num, name, 0, \
1334 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1335 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1336 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1337 PORT_INFO(vendor, product, num, name, voices, \
1338 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1339 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1340 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1341 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1342 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1343 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1344 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1345 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1346 PORT_INFO(vendor, product, num, name, voices, \
1347 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1348 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1349 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1350 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1351 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1352 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1353 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1354 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1356 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1357 /* Roland SC-8850 */
1358 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1359 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1360 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1361 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1362 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1363 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1365 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1366 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1367 /* Roland SC-8820 */
1368 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1369 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1370 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1372 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1373 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1374 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1376 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1377 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1378 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1380 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1382 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1383 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1384 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1385 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1387 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1389 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1390 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1391 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1393 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1394 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1395 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1396 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1398 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1399 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1401 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1402 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1403 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1405 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1406 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1407 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1409 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1410 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1411 /* Edirol UA-1000 */
1412 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1413 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1415 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1416 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1417 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1419 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1420 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1421 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1423 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1424 /* M-Audio MidiSport 8x8 */
1425 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1426 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1428 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1429 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1430 /* Emagic Unitor8/AMT8/MT4 */
1431 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1432 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1433 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1434 /* Access Music Virus TI */
1435 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1436 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1437 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
|
1438 SNDRV_SEQ_PORT_TYPE_HARDWARE
|
1439 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER
),
1442 static struct port_info
*find_port_info(struct snd_usb_midi
* umidi
, int number
)
1446 for (i
= 0; i
< ARRAY_SIZE(snd_usbmidi_port_info
); ++i
) {
1447 if (snd_usbmidi_port_info
[i
].id
== umidi
->usb_id
&&
1448 snd_usbmidi_port_info
[i
].port
== number
)
1449 return &snd_usbmidi_port_info
[i
];
1454 static void snd_usbmidi_get_port_info(struct snd_rawmidi
*rmidi
, int number
,
1455 struct snd_seq_port_info
*seq_port_info
)
1457 struct snd_usb_midi
*umidi
= rmidi
->private_data
;
1458 struct port_info
*port_info
;
1460 /* TODO: read port flags from descriptors */
1461 port_info
= find_port_info(umidi
, number
);
1463 seq_port_info
->type
= port_info
->seq_flags
;
1464 seq_port_info
->midi_voices
= port_info
->voices
;
1468 static void snd_usbmidi_init_substream(struct snd_usb_midi
* umidi
,
1469 int stream
, int number
,
1470 struct snd_rawmidi_substream
** rsubstream
)
1472 struct port_info
*port_info
;
1473 const char *name_format
;
1475 struct snd_rawmidi_substream
*substream
= snd_usbmidi_find_substream(umidi
, stream
, number
);
1477 snd_printd(KERN_ERR
"substream %d:%d not found\n", stream
, number
);
1481 /* TODO: read port name from jack descriptor */
1482 port_info
= find_port_info(umidi
, number
);
1483 name_format
= port_info
? port_info
->name
: "%s MIDI %d";
1484 snprintf(substream
->name
, sizeof(substream
->name
),
1485 name_format
, umidi
->card
->shortname
, number
+ 1);
1487 *rsubstream
= substream
;
1491 * Creates the endpoints and their ports.
1493 static int snd_usbmidi_create_endpoints(struct snd_usb_midi
* umidi
,
1494 struct snd_usb_midi_endpoint_info
* endpoints
)
1497 int out_ports
= 0, in_ports
= 0;
1499 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1500 if (endpoints
[i
].out_cables
) {
1501 err
= snd_usbmidi_out_endpoint_create(umidi
, &endpoints
[i
],
1502 &umidi
->endpoints
[i
]);
1506 if (endpoints
[i
].in_cables
) {
1507 err
= snd_usbmidi_in_endpoint_create(umidi
, &endpoints
[i
],
1508 &umidi
->endpoints
[i
]);
1513 for (j
= 0; j
< 0x10; ++j
) {
1514 if (endpoints
[i
].out_cables
& (1 << j
)) {
1515 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, out_ports
,
1516 &umidi
->endpoints
[i
].out
->ports
[j
].substream
);
1519 if (endpoints
[i
].in_cables
& (1 << j
)) {
1520 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, in_ports
,
1521 &umidi
->endpoints
[i
].in
->ports
[j
].substream
);
1526 snd_printdd(KERN_INFO
"created %d output and %d input ports\n",
1527 out_ports
, in_ports
);
1532 * Returns MIDIStreaming device capabilities.
1534 static int snd_usbmidi_get_ms_info(struct snd_usb_midi
* umidi
,
1535 struct snd_usb_midi_endpoint_info
* endpoints
)
1537 struct usb_interface
* intf
;
1538 struct usb_host_interface
*hostif
;
1539 struct usb_interface_descriptor
* intfd
;
1540 struct usb_ms_header_descriptor
* ms_header
;
1541 struct usb_host_endpoint
*hostep
;
1542 struct usb_endpoint_descriptor
* ep
;
1543 struct usb_ms_endpoint_descriptor
* ms_ep
;
1546 intf
= umidi
->iface
;
1549 hostif
= &intf
->altsetting
[0];
1550 intfd
= get_iface_desc(hostif
);
1551 ms_header
= (struct usb_ms_header_descriptor
*)hostif
->extra
;
1552 if (hostif
->extralen
>= 7 &&
1553 ms_header
->bLength
>= 7 &&
1554 ms_header
->bDescriptorType
== USB_DT_CS_INTERFACE
&&
1555 ms_header
->bDescriptorSubtype
== HEADER
)
1556 snd_printdd(KERN_INFO
"MIDIStreaming version %02x.%02x\n",
1557 ms_header
->bcdMSC
[1], ms_header
->bcdMSC
[0]);
1559 snd_printk(KERN_WARNING
"MIDIStreaming interface descriptor not found\n");
1562 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
1563 hostep
= &hostif
->endpoint
[i
];
1564 ep
= get_ep_desc(hostep
);
1565 if (!usb_endpoint_xfer_bulk(ep
) && !usb_endpoint_xfer_int(ep
))
1567 ms_ep
= (struct usb_ms_endpoint_descriptor
*)hostep
->extra
;
1568 if (hostep
->extralen
< 4 ||
1569 ms_ep
->bLength
< 4 ||
1570 ms_ep
->bDescriptorType
!= USB_DT_CS_ENDPOINT
||
1571 ms_ep
->bDescriptorSubtype
!= MS_GENERAL
)
1573 if (usb_endpoint_dir_out(ep
)) {
1574 if (endpoints
[epidx
].out_ep
) {
1575 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
1576 snd_printk(KERN_WARNING
"too many endpoints\n");
1580 endpoints
[epidx
].out_ep
= usb_endpoint_num(ep
);
1581 if (usb_endpoint_xfer_int(ep
))
1582 endpoints
[epidx
].out_interval
= ep
->bInterval
;
1583 else if (snd_usb_get_speed(umidi
->dev
) == USB_SPEED_LOW
)
1585 * Low speed bulk transfers don't exist, so
1586 * force interrupt transfers for devices like
1587 * ESI MIDI Mate that try to use them anyway.
1589 endpoints
[epidx
].out_interval
= 1;
1590 endpoints
[epidx
].out_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
1591 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
1592 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
1594 if (endpoints
[epidx
].in_ep
) {
1595 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
1596 snd_printk(KERN_WARNING
"too many endpoints\n");
1600 endpoints
[epidx
].in_ep
= usb_endpoint_num(ep
);
1601 if (usb_endpoint_xfer_int(ep
))
1602 endpoints
[epidx
].in_interval
= ep
->bInterval
;
1603 else if (snd_usb_get_speed(umidi
->dev
) == USB_SPEED_LOW
)
1604 endpoints
[epidx
].in_interval
= 1;
1605 endpoints
[epidx
].in_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
1606 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
1607 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
1613 static int roland_load_info(struct snd_kcontrol
*kcontrol
,
1614 struct snd_ctl_elem_info
*info
)
1616 static const char *const names
[] = { "High Load", "Light Load" };
1618 info
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1620 info
->value
.enumerated
.items
= 2;
1621 if (info
->value
.enumerated
.item
> 1)
1622 info
->value
.enumerated
.item
= 1;
1623 strcpy(info
->value
.enumerated
.name
, names
[info
->value
.enumerated
.item
]);
1627 static int roland_load_get(struct snd_kcontrol
*kcontrol
,
1628 struct snd_ctl_elem_value
*value
)
1630 value
->value
.enumerated
.item
[0] = kcontrol
->private_value
;
1634 static int roland_load_put(struct snd_kcontrol
*kcontrol
,
1635 struct snd_ctl_elem_value
*value
)
1637 struct snd_usb_midi
* umidi
= kcontrol
->private_data
;
1640 if (value
->value
.enumerated
.item
[0] > 1)
1642 mutex_lock(&umidi
->mutex
);
1643 changed
= value
->value
.enumerated
.item
[0] != kcontrol
->private_value
;
1645 kcontrol
->private_value
= value
->value
.enumerated
.item
[0];
1646 mutex_unlock(&umidi
->mutex
);
1650 static struct snd_kcontrol_new roland_load_ctl
= {
1651 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1652 .name
= "MIDI Input Mode",
1653 .info
= roland_load_info
,
1654 .get
= roland_load_get
,
1655 .put
= roland_load_put
,
1660 * On Roland devices, use the second alternate setting to be able to use
1661 * the interrupt input endpoint.
1663 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi
* umidi
)
1665 struct usb_interface
* intf
;
1666 struct usb_host_interface
*hostif
;
1667 struct usb_interface_descriptor
* intfd
;
1669 intf
= umidi
->iface
;
1670 if (!intf
|| intf
->num_altsetting
!= 2)
1673 hostif
= &intf
->altsetting
[1];
1674 intfd
= get_iface_desc(hostif
);
1675 if (intfd
->bNumEndpoints
!= 2 ||
1676 (get_endpoint(hostif
, 0)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
||
1677 (get_endpoint(hostif
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1680 snd_printdd(KERN_INFO
"switching to altsetting %d with int ep\n",
1681 intfd
->bAlternateSetting
);
1682 usb_set_interface(umidi
->dev
, intfd
->bInterfaceNumber
,
1683 intfd
->bAlternateSetting
);
1685 umidi
->roland_load_ctl
= snd_ctl_new1(&roland_load_ctl
, umidi
);
1686 if (snd_ctl_add(umidi
->card
, umidi
->roland_load_ctl
) < 0)
1687 umidi
->roland_load_ctl
= NULL
;
1691 * Try to find any usable endpoints in the interface.
1693 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi
* umidi
,
1694 struct snd_usb_midi_endpoint_info
* endpoint
,
1697 struct usb_interface
* intf
;
1698 struct usb_host_interface
*hostif
;
1699 struct usb_interface_descriptor
* intfd
;
1700 struct usb_endpoint_descriptor
* epd
;
1701 int i
, out_eps
= 0, in_eps
= 0;
1703 if (USB_ID_VENDOR(umidi
->usb_id
) == 0x0582)
1704 snd_usbmidi_switch_roland_altsetting(umidi
);
1706 if (endpoint
[0].out_ep
|| endpoint
[0].in_ep
)
1709 intf
= umidi
->iface
;
1710 if (!intf
|| intf
->num_altsetting
< 1)
1712 hostif
= intf
->cur_altsetting
;
1713 intfd
= get_iface_desc(hostif
);
1715 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
1716 epd
= get_endpoint(hostif
, i
);
1717 if (!usb_endpoint_xfer_bulk(epd
) &&
1718 !usb_endpoint_xfer_int(epd
))
1720 if (out_eps
< max_endpoints
&&
1721 usb_endpoint_dir_out(epd
)) {
1722 endpoint
[out_eps
].out_ep
= usb_endpoint_num(epd
);
1723 if (usb_endpoint_xfer_int(epd
))
1724 endpoint
[out_eps
].out_interval
= epd
->bInterval
;
1727 if (in_eps
< max_endpoints
&&
1728 usb_endpoint_dir_in(epd
)) {
1729 endpoint
[in_eps
].in_ep
= usb_endpoint_num(epd
);
1730 if (usb_endpoint_xfer_int(epd
))
1731 endpoint
[in_eps
].in_interval
= epd
->bInterval
;
1735 return (out_eps
|| in_eps
) ? 0 : -ENOENT
;
1739 * Detects the endpoints for one-port-per-endpoint protocols.
1741 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi
* umidi
,
1742 struct snd_usb_midi_endpoint_info
* endpoints
)
1746 err
= snd_usbmidi_detect_endpoints(umidi
, endpoints
, MIDI_MAX_ENDPOINTS
);
1747 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1748 if (endpoints
[i
].out_ep
)
1749 endpoints
[i
].out_cables
= 0x0001;
1750 if (endpoints
[i
].in_ep
)
1751 endpoints
[i
].in_cables
= 0x0001;
1757 * Detects the endpoints and ports of Yamaha devices.
1759 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi
* umidi
,
1760 struct snd_usb_midi_endpoint_info
* endpoint
)
1762 struct usb_interface
* intf
;
1763 struct usb_host_interface
*hostif
;
1764 struct usb_interface_descriptor
* intfd
;
1767 intf
= umidi
->iface
;
1770 hostif
= intf
->altsetting
;
1771 intfd
= get_iface_desc(hostif
);
1772 if (intfd
->bNumEndpoints
< 1)
1776 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1777 * necessarily with any useful contents. So simply count 'em.
1779 for (cs_desc
= hostif
->extra
;
1780 cs_desc
< hostif
->extra
+ hostif
->extralen
&& cs_desc
[0] >= 2;
1781 cs_desc
+= cs_desc
[0]) {
1782 if (cs_desc
[1] == USB_DT_CS_INTERFACE
) {
1783 if (cs_desc
[2] == MIDI_IN_JACK
)
1784 endpoint
->in_cables
= (endpoint
->in_cables
<< 1) | 1;
1785 else if (cs_desc
[2] == MIDI_OUT_JACK
)
1786 endpoint
->out_cables
= (endpoint
->out_cables
<< 1) | 1;
1789 if (!endpoint
->in_cables
&& !endpoint
->out_cables
)
1792 return snd_usbmidi_detect_endpoints(umidi
, endpoint
, 1);
1796 * Creates the endpoints and their ports for Midiman devices.
1798 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi
* umidi
,
1799 struct snd_usb_midi_endpoint_info
* endpoint
)
1801 struct snd_usb_midi_endpoint_info ep_info
;
1802 struct usb_interface
* intf
;
1803 struct usb_host_interface
*hostif
;
1804 struct usb_interface_descriptor
* intfd
;
1805 struct usb_endpoint_descriptor
* epd
;
1808 intf
= umidi
->iface
;
1811 hostif
= intf
->altsetting
;
1812 intfd
= get_iface_desc(hostif
);
1814 * The various MidiSport devices have more or less random endpoint
1815 * numbers, so we have to identify the endpoints by their index in
1816 * the descriptor array, like the driver for that other OS does.
1818 * There is one interrupt input endpoint for all input ports, one
1819 * bulk output endpoint for even-numbered ports, and one for odd-
1820 * numbered ports. Both bulk output endpoints have corresponding
1821 * input bulk endpoints (at indices 1 and 3) which aren't used.
1823 if (intfd
->bNumEndpoints
< (endpoint
->out_cables
> 0x0001 ? 5 : 3)) {
1824 snd_printdd(KERN_ERR
"not enough endpoints\n");
1828 epd
= get_endpoint(hostif
, 0);
1829 if (!usb_endpoint_dir_in(epd
) || !usb_endpoint_xfer_int(epd
)) {
1830 snd_printdd(KERN_ERR
"endpoint[0] isn't interrupt\n");
1833 epd
= get_endpoint(hostif
, 2);
1834 if (!usb_endpoint_dir_out(epd
) || !usb_endpoint_xfer_bulk(epd
)) {
1835 snd_printdd(KERN_ERR
"endpoint[2] isn't bulk output\n");
1838 if (endpoint
->out_cables
> 0x0001) {
1839 epd
= get_endpoint(hostif
, 4);
1840 if (!usb_endpoint_dir_out(epd
) ||
1841 !usb_endpoint_xfer_bulk(epd
)) {
1842 snd_printdd(KERN_ERR
"endpoint[4] isn't bulk output\n");
1847 ep_info
.out_ep
= get_endpoint(hostif
, 2)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1848 ep_info
.out_interval
= 0;
1849 ep_info
.out_cables
= endpoint
->out_cables
& 0x5555;
1850 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1854 ep_info
.in_ep
= get_endpoint(hostif
, 0)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1855 ep_info
.in_interval
= get_endpoint(hostif
, 0)->bInterval
;
1856 ep_info
.in_cables
= endpoint
->in_cables
;
1857 err
= snd_usbmidi_in_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1861 if (endpoint
->out_cables
> 0x0001) {
1862 ep_info
.out_ep
= get_endpoint(hostif
, 4)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1863 ep_info
.out_cables
= endpoint
->out_cables
& 0xaaaa;
1864 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[1]);
1869 for (cable
= 0; cable
< 0x10; ++cable
) {
1870 if (endpoint
->out_cables
& (1 << cable
))
1871 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, cable
,
1872 &umidi
->endpoints
[cable
& 1].out
->ports
[cable
].substream
);
1873 if (endpoint
->in_cables
& (1 << cable
))
1874 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, cable
,
1875 &umidi
->endpoints
[0].in
->ports
[cable
].substream
);
1880 static struct snd_rawmidi_global_ops snd_usbmidi_ops
= {
1881 .get_port_info
= snd_usbmidi_get_port_info
,
1884 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi
* umidi
,
1885 int out_ports
, int in_ports
)
1887 struct snd_rawmidi
*rmidi
;
1890 err
= snd_rawmidi_new(umidi
->card
, "USB MIDI",
1891 umidi
->next_midi_device
++,
1892 out_ports
, in_ports
, &rmidi
);
1895 strcpy(rmidi
->name
, umidi
->card
->shortname
);
1896 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
1897 SNDRV_RAWMIDI_INFO_INPUT
|
1898 SNDRV_RAWMIDI_INFO_DUPLEX
;
1899 rmidi
->ops
= &snd_usbmidi_ops
;
1900 rmidi
->private_data
= umidi
;
1901 rmidi
->private_free
= snd_usbmidi_rawmidi_free
;
1902 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_usbmidi_output_ops
);
1903 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_usbmidi_input_ops
);
1905 umidi
->rmidi
= rmidi
;
1910 * Temporarily stop input.
1912 void snd_usbmidi_input_stop(struct list_head
* p
)
1914 struct snd_usb_midi
* umidi
;
1917 umidi
= list_entry(p
, struct snd_usb_midi
, list
);
1918 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1919 struct snd_usb_midi_endpoint
* ep
= &umidi
->endpoints
[i
];
1921 for (j
= 0; j
< INPUT_URBS
; ++j
)
1922 usb_kill_urb(ep
->in
->urbs
[j
]);
1926 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint
* ep
)
1932 for (i
= 0; i
< INPUT_URBS
; ++i
) {
1933 struct urb
* urb
= ep
->urbs
[i
];
1934 urb
->dev
= ep
->umidi
->dev
;
1935 snd_usbmidi_submit_urb(urb
, GFP_KERNEL
);
1940 * Resume input after a call to snd_usbmidi_input_stop().
1942 void snd_usbmidi_input_start(struct list_head
* p
)
1944 struct snd_usb_midi
* umidi
;
1947 umidi
= list_entry(p
, struct snd_usb_midi
, list
);
1948 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
1949 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
1953 * Creates and registers everything needed for a MIDI streaming interface.
1955 int snd_usbmidi_create(struct snd_card
*card
,
1956 struct usb_interface
* iface
,
1957 struct list_head
*midi_list
,
1958 const struct snd_usb_audio_quirk
* quirk
)
1960 struct snd_usb_midi
* umidi
;
1961 struct snd_usb_midi_endpoint_info endpoints
[MIDI_MAX_ENDPOINTS
];
1962 int out_ports
, in_ports
;
1965 umidi
= kzalloc(sizeof(*umidi
), GFP_KERNEL
);
1968 umidi
->dev
= interface_to_usbdev(iface
);
1970 umidi
->iface
= iface
;
1971 umidi
->quirk
= quirk
;
1972 umidi
->usb_protocol_ops
= &snd_usbmidi_standard_ops
;
1973 init_timer(&umidi
->error_timer
);
1974 spin_lock_init(&umidi
->disc_lock
);
1975 mutex_init(&umidi
->mutex
);
1976 umidi
->usb_id
= USB_ID(le16_to_cpu(umidi
->dev
->descriptor
.idVendor
),
1977 le16_to_cpu(umidi
->dev
->descriptor
.idProduct
));
1978 umidi
->error_timer
.function
= snd_usbmidi_error_timer
;
1979 umidi
->error_timer
.data
= (unsigned long)umidi
;
1981 /* detect the endpoint(s) to use */
1982 memset(endpoints
, 0, sizeof(endpoints
));
1983 switch (quirk
? quirk
->type
: QUIRK_MIDI_STANDARD_INTERFACE
) {
1984 case QUIRK_MIDI_STANDARD_INTERFACE
:
1985 err
= snd_usbmidi_get_ms_info(umidi
, endpoints
);
1986 if (umidi
->usb_id
== USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1987 umidi
->usb_protocol_ops
=
1988 &snd_usbmidi_maudio_broken_running_status_ops
;
1990 case QUIRK_MIDI_US122L
:
1991 umidi
->usb_protocol_ops
= &snd_usbmidi_122l_ops
;
1993 case QUIRK_MIDI_FIXED_ENDPOINT
:
1994 memcpy(&endpoints
[0], quirk
->data
,
1995 sizeof(struct snd_usb_midi_endpoint_info
));
1996 err
= snd_usbmidi_detect_endpoints(umidi
, &endpoints
[0], 1);
1998 case QUIRK_MIDI_YAMAHA
:
1999 err
= snd_usbmidi_detect_yamaha(umidi
, &endpoints
[0]);
2001 case QUIRK_MIDI_MIDIMAN
:
2002 umidi
->usb_protocol_ops
= &snd_usbmidi_midiman_ops
;
2003 memcpy(&endpoints
[0], quirk
->data
,
2004 sizeof(struct snd_usb_midi_endpoint_info
));
2007 case QUIRK_MIDI_NOVATION
:
2008 umidi
->usb_protocol_ops
= &snd_usbmidi_novation_ops
;
2009 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
2011 case QUIRK_MIDI_FASTLANE
:
2012 umidi
->usb_protocol_ops
= &snd_usbmidi_raw_ops
;
2014 * Interface 1 contains isochronous endpoints, but with the same
2015 * numbers as in interface 0. Since it is interface 1 that the
2016 * USB core has most recently seen, these descriptors are now
2017 * associated with the endpoint numbers. This will foul up our
2018 * attempts to submit bulk/interrupt URBs to the endpoints in
2019 * interface 0, so we have to make sure that the USB core looks
2020 * again at interface 0 by calling usb_set_interface() on it.
2022 usb_set_interface(umidi
->dev
, 0, 0);
2023 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
2025 case QUIRK_MIDI_EMAGIC
:
2026 umidi
->usb_protocol_ops
= &snd_usbmidi_emagic_ops
;
2027 memcpy(&endpoints
[0], quirk
->data
,
2028 sizeof(struct snd_usb_midi_endpoint_info
));
2029 err
= snd_usbmidi_detect_endpoints(umidi
, &endpoints
[0], 1);
2031 case QUIRK_MIDI_CME
:
2032 umidi
->usb_protocol_ops
= &snd_usbmidi_cme_ops
;
2033 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
2036 snd_printd(KERN_ERR
"invalid quirk type %d\n", quirk
->type
);
2045 /* create rawmidi device */
2048 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
2049 out_ports
+= hweight16(endpoints
[i
].out_cables
);
2050 in_ports
+= hweight16(endpoints
[i
].in_cables
);
2052 err
= snd_usbmidi_create_rawmidi(umidi
, out_ports
, in_ports
);
2058 /* create endpoint/port structures */
2059 if (quirk
&& quirk
->type
== QUIRK_MIDI_MIDIMAN
)
2060 err
= snd_usbmidi_create_endpoints_midiman(umidi
, &endpoints
[0]);
2062 err
= snd_usbmidi_create_endpoints(umidi
, endpoints
);
2064 snd_usbmidi_free(umidi
);
2068 list_add_tail(&umidi
->list
, midi_list
);
2070 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
2071 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
2075 EXPORT_SYMBOL(snd_usbmidi_create
);
2076 EXPORT_SYMBOL(snd_usbmidi_input_stop
);
2077 EXPORT_SYMBOL(snd_usbmidi_input_start
);
2078 EXPORT_SYMBOL(snd_usbmidi_disconnect
);