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 <linux/usb/audio.h>
51 #include <sound/core.h>
52 #include <sound/control.h>
53 #include <sound/rawmidi.h>
54 #include <sound/asequencer.h>
60 * define this to log all USB packets
62 /* #define DUMP_PACKETS */
65 * how long to wait after some USB errors, so that khubd can disconnect() us
66 * without too many spurious errors
68 #define ERROR_DELAY_JIFFIES (HZ / 10)
74 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
75 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
76 MODULE_LICENSE("Dual BSD/GPL");
79 struct usb_ms_header_descriptor
{
82 __u8 bDescriptorSubtype
;
85 } __attribute__ ((packed
));
87 struct usb_ms_endpoint_descriptor
{
90 __u8 bDescriptorSubtype
;
92 __u8 baAssocJackID
[0];
93 } __attribute__ ((packed
));
95 struct snd_usb_midi_in_endpoint
;
96 struct snd_usb_midi_out_endpoint
;
97 struct snd_usb_midi_endpoint
;
99 struct usb_protocol_ops
{
100 void (*input
)(struct snd_usb_midi_in_endpoint
*, uint8_t*, int);
101 void (*output
)(struct snd_usb_midi_out_endpoint
*ep
, struct urb
*urb
);
102 void (*output_packet
)(struct urb
*, uint8_t, uint8_t, uint8_t, uint8_t);
103 void (*init_out_endpoint
)(struct snd_usb_midi_out_endpoint
*);
104 void (*finish_out_endpoint
)(struct snd_usb_midi_out_endpoint
*);
107 struct snd_usb_midi
{
108 struct usb_device
*dev
;
109 struct snd_card
*card
;
110 struct usb_interface
*iface
;
111 const struct snd_usb_audio_quirk
*quirk
;
112 struct snd_rawmidi
*rmidi
;
113 struct usb_protocol_ops
* usb_protocol_ops
;
114 struct list_head list
;
115 struct timer_list error_timer
;
116 spinlock_t disc_lock
;
119 int next_midi_device
;
121 struct snd_usb_midi_endpoint
{
122 struct snd_usb_midi_out_endpoint
*out
;
123 struct snd_usb_midi_in_endpoint
*in
;
124 } endpoints
[MIDI_MAX_ENDPOINTS
];
125 unsigned long input_triggered
;
127 unsigned char disconnected
;
129 struct snd_kcontrol
*roland_load_ctl
;
132 struct snd_usb_midi_out_endpoint
{
133 struct snd_usb_midi
* umidi
;
134 struct out_urb_context
{
136 struct snd_usb_midi_out_endpoint
*ep
;
138 unsigned int active_urbs
;
139 unsigned int drain_urbs
;
140 int max_transfer
; /* size of urb buffer */
141 struct tasklet_struct tasklet
;
142 unsigned int next_urb
;
143 spinlock_t buffer_lock
;
145 struct usbmidi_out_port
{
146 struct snd_usb_midi_out_endpoint
* ep
;
147 struct snd_rawmidi_substream
*substream
;
149 uint8_t cable
; /* cable number << 4 */
151 #define STATE_UNKNOWN 0
152 #define STATE_1PARAM 1
153 #define STATE_2PARAM_1 2
154 #define STATE_2PARAM_2 3
155 #define STATE_SYSEX_0 4
156 #define STATE_SYSEX_1 5
157 #define STATE_SYSEX_2 6
162 wait_queue_head_t drain_wait
;
165 struct snd_usb_midi_in_endpoint
{
166 struct snd_usb_midi
* umidi
;
167 struct urb
* urbs
[INPUT_URBS
];
168 struct usbmidi_in_port
{
169 struct snd_rawmidi_substream
*substream
;
170 u8 running_status_length
;
177 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint
* ep
);
179 static const uint8_t snd_usbmidi_cin_length
[] = {
180 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
184 * Submits the URB, with error handling.
186 static int snd_usbmidi_submit_urb(struct urb
* urb
, gfp_t flags
)
188 int err
= usb_submit_urb(urb
, flags
);
189 if (err
< 0 && err
!= -ENODEV
)
190 snd_printk(KERN_ERR
"usb_submit_urb: %d\n", err
);
195 * Error handling for URB completion functions.
197 static int snd_usbmidi_urb_error(int status
)
200 /* manually unlinked, or device gone */
206 /* errors that might occur during unplugging */
212 snd_printk(KERN_ERR
"urb status %d\n", status
);
213 return 0; /* continue */
218 * Receives a chunk of MIDI data.
220 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint
* ep
, int portidx
,
221 uint8_t* data
, int length
)
223 struct usbmidi_in_port
* port
= &ep
->ports
[portidx
];
225 if (!port
->substream
) {
226 snd_printd("unexpected port %d!\n", portidx
);
229 if (!test_bit(port
->substream
->number
, &ep
->umidi
->input_triggered
))
231 snd_rawmidi_receive(port
->substream
, data
, length
);
235 static void dump_urb(const char *type
, const u8
*data
, int length
)
237 snd_printk(KERN_DEBUG
"%s packet: [", type
);
238 for (; length
> 0; ++data
, --length
)
239 printk(" %02x", *data
);
243 #define dump_urb(type, data, length) /* nothing */
247 * Processes the data read from the device.
249 static void snd_usbmidi_in_urb_complete(struct urb
* urb
)
251 struct snd_usb_midi_in_endpoint
* ep
= urb
->context
;
253 if (urb
->status
== 0) {
254 dump_urb("received", urb
->transfer_buffer
, urb
->actual_length
);
255 ep
->umidi
->usb_protocol_ops
->input(ep
, urb
->transfer_buffer
,
258 int err
= snd_usbmidi_urb_error(urb
->status
);
260 if (err
!= -ENODEV
) {
261 ep
->error_resubmit
= 1;
262 mod_timer(&ep
->umidi
->error_timer
,
263 jiffies
+ ERROR_DELAY_JIFFIES
);
269 urb
->dev
= ep
->umidi
->dev
;
270 snd_usbmidi_submit_urb(urb
, GFP_ATOMIC
);
273 static void snd_usbmidi_out_urb_complete(struct urb
* urb
)
275 struct out_urb_context
*context
= urb
->context
;
276 struct snd_usb_midi_out_endpoint
* ep
= context
->ep
;
277 unsigned int urb_index
;
279 spin_lock(&ep
->buffer_lock
);
280 urb_index
= context
- ep
->urbs
;
281 ep
->active_urbs
&= ~(1 << urb_index
);
282 if (unlikely(ep
->drain_urbs
)) {
283 ep
->drain_urbs
&= ~(1 << urb_index
);
284 wake_up(&ep
->drain_wait
);
286 spin_unlock(&ep
->buffer_lock
);
287 if (urb
->status
< 0) {
288 int err
= snd_usbmidi_urb_error(urb
->status
);
291 mod_timer(&ep
->umidi
->error_timer
,
292 jiffies
+ ERROR_DELAY_JIFFIES
);
296 snd_usbmidi_do_output(ep
);
300 * This is called when some data should be transferred to the device
301 * (from one or more substreams).
303 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint
* ep
)
305 unsigned int urb_index
;
309 spin_lock_irqsave(&ep
->buffer_lock
, flags
);
310 if (ep
->umidi
->disconnected
) {
311 spin_unlock_irqrestore(&ep
->buffer_lock
, flags
);
315 urb_index
= ep
->next_urb
;
317 if (!(ep
->active_urbs
& (1 << urb_index
))) {
318 urb
= ep
->urbs
[urb_index
].urb
;
319 urb
->transfer_buffer_length
= 0;
320 ep
->umidi
->usb_protocol_ops
->output(ep
, urb
);
321 if (urb
->transfer_buffer_length
== 0)
324 dump_urb("sending", urb
->transfer_buffer
,
325 urb
->transfer_buffer_length
);
326 urb
->dev
= ep
->umidi
->dev
;
327 if (snd_usbmidi_submit_urb(urb
, GFP_ATOMIC
) < 0)
329 ep
->active_urbs
|= 1 << urb_index
;
331 if (++urb_index
>= OUTPUT_URBS
)
333 if (urb_index
== ep
->next_urb
)
336 ep
->next_urb
= urb_index
;
337 spin_unlock_irqrestore(&ep
->buffer_lock
, flags
);
340 static void snd_usbmidi_out_tasklet(unsigned long data
)
342 struct snd_usb_midi_out_endpoint
* ep
= (struct snd_usb_midi_out_endpoint
*) data
;
344 snd_usbmidi_do_output(ep
);
347 /* called after transfers had been interrupted due to some USB error */
348 static void snd_usbmidi_error_timer(unsigned long data
)
350 struct snd_usb_midi
*umidi
= (struct snd_usb_midi
*)data
;
353 spin_lock(&umidi
->disc_lock
);
354 if (umidi
->disconnected
) {
355 spin_unlock(&umidi
->disc_lock
);
358 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
359 struct snd_usb_midi_in_endpoint
*in
= umidi
->endpoints
[i
].in
;
360 if (in
&& in
->error_resubmit
) {
361 in
->error_resubmit
= 0;
362 for (j
= 0; j
< INPUT_URBS
; ++j
) {
363 in
->urbs
[j
]->dev
= umidi
->dev
;
364 snd_usbmidi_submit_urb(in
->urbs
[j
], GFP_ATOMIC
);
367 if (umidi
->endpoints
[i
].out
)
368 snd_usbmidi_do_output(umidi
->endpoints
[i
].out
);
370 spin_unlock(&umidi
->disc_lock
);
373 /* helper function to send static data that may not DMA-able */
374 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint
* ep
,
375 const void *data
, int len
)
378 void *buf
= kmemdup(data
, len
, GFP_KERNEL
);
381 dump_urb("sending", buf
, len
);
383 err
= usb_bulk_msg(ep
->umidi
->dev
, ep
->urbs
[0].urb
->pipe
,
384 buf
, len
, NULL
, 250);
390 * Standard USB MIDI protocol: see the spec.
391 * Midiman protocol: like the standard protocol, but the control byte is the
392 * fourth byte in each packet, and uses length instead of CIN.
395 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint
* ep
,
396 uint8_t* buffer
, int buffer_length
)
400 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
401 if (buffer
[i
] != 0) {
402 int cable
= buffer
[i
] >> 4;
403 int length
= snd_usbmidi_cin_length
[buffer
[i
] & 0x0f];
404 snd_usbmidi_input_data(ep
, cable
, &buffer
[i
+ 1], length
);
408 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint
* ep
,
409 uint8_t* buffer
, int buffer_length
)
413 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
414 if (buffer
[i
+ 3] != 0) {
415 int port
= buffer
[i
+ 3] >> 4;
416 int length
= buffer
[i
+ 3] & 3;
417 snd_usbmidi_input_data(ep
, port
, &buffer
[i
], length
);
422 * Buggy M-Audio device: running status on input results in a packet that has
423 * the data bytes but not the status byte and that is marked with CIN 4.
425 static void snd_usbmidi_maudio_broken_running_status_input(
426 struct snd_usb_midi_in_endpoint
* ep
,
427 uint8_t* buffer
, int buffer_length
)
431 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
432 if (buffer
[i
] != 0) {
433 int cable
= buffer
[i
] >> 4;
434 u8 cin
= buffer
[i
] & 0x0f;
435 struct usbmidi_in_port
*port
= &ep
->ports
[cable
];
438 length
= snd_usbmidi_cin_length
[cin
];
439 if (cin
== 0xf && buffer
[i
+ 1] >= 0xf8)
440 ; /* realtime msg: no running status change */
441 else if (cin
>= 0x8 && cin
<= 0xe)
443 port
->running_status_length
= length
- 1;
444 else if (cin
== 0x4 &&
445 port
->running_status_length
!= 0 &&
446 buffer
[i
+ 1] < 0x80)
447 /* CIN 4 that is not a SysEx */
448 length
= port
->running_status_length
;
451 * All other msgs cannot begin running status.
452 * (A channel msg sent as two or three CIN 0xF
453 * packets could in theory, but this device
454 * doesn't use this format.)
456 port
->running_status_length
= 0;
457 snd_usbmidi_input_data(ep
, cable
, &buffer
[i
+ 1], length
);
462 * CME protocol: like the standard protocol, but SysEx commands are sent as a
463 * single USB packet preceded by a 0x0F byte.
465 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint
*ep
,
466 uint8_t *buffer
, int buffer_length
)
468 if (buffer_length
< 2 || (buffer
[0] & 0x0f) != 0x0f)
469 snd_usbmidi_standard_input(ep
, buffer
, buffer_length
);
471 snd_usbmidi_input_data(ep
, buffer
[0] >> 4,
472 &buffer
[1], buffer_length
- 1);
476 * Adds one USB MIDI packet to the output buffer.
478 static void snd_usbmidi_output_standard_packet(struct urb
* urb
, uint8_t p0
,
479 uint8_t p1
, uint8_t p2
, uint8_t p3
)
482 uint8_t* buf
= (uint8_t*)urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
487 urb
->transfer_buffer_length
+= 4;
491 * Adds one Midiman packet to the output buffer.
493 static void snd_usbmidi_output_midiman_packet(struct urb
* urb
, uint8_t p0
,
494 uint8_t p1
, uint8_t p2
, uint8_t p3
)
497 uint8_t* buf
= (uint8_t*)urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
501 buf
[3] = (p0
& 0xf0) | snd_usbmidi_cin_length
[p0
& 0x0f];
502 urb
->transfer_buffer_length
+= 4;
506 * Converts MIDI commands to USB MIDI packets.
508 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port
* port
,
509 uint8_t b
, struct urb
* urb
)
511 uint8_t p0
= port
->cable
;
512 void (*output_packet
)(struct urb
*, uint8_t, uint8_t, uint8_t, uint8_t) =
513 port
->ep
->umidi
->usb_protocol_ops
->output_packet
;
516 output_packet(urb
, p0
| 0x0f, b
, 0, 0);
517 } else if (b
>= 0xf0) {
521 port
->state
= STATE_SYSEX_1
;
526 port
->state
= STATE_1PARAM
;
530 port
->state
= STATE_2PARAM_1
;
534 port
->state
= STATE_UNKNOWN
;
537 output_packet(urb
, p0
| 0x05, 0xf6, 0, 0);
538 port
->state
= STATE_UNKNOWN
;
541 switch (port
->state
) {
543 output_packet(urb
, p0
| 0x05, 0xf7, 0, 0);
546 output_packet(urb
, p0
| 0x06, port
->data
[0], 0xf7, 0);
549 output_packet(urb
, p0
| 0x07, port
->data
[0], port
->data
[1], 0xf7);
552 port
->state
= STATE_UNKNOWN
;
555 } else if (b
>= 0x80) {
557 if (b
>= 0xc0 && b
<= 0xdf)
558 port
->state
= STATE_1PARAM
;
560 port
->state
= STATE_2PARAM_1
;
561 } else { /* b < 0x80 */
562 switch (port
->state
) {
564 if (port
->data
[0] < 0xf0) {
565 p0
|= port
->data
[0] >> 4;
568 port
->state
= STATE_UNKNOWN
;
570 output_packet(urb
, p0
, port
->data
[0], b
, 0);
574 port
->state
= STATE_2PARAM_2
;
577 if (port
->data
[0] < 0xf0) {
578 p0
|= port
->data
[0] >> 4;
579 port
->state
= STATE_2PARAM_1
;
582 port
->state
= STATE_UNKNOWN
;
584 output_packet(urb
, p0
, port
->data
[0], port
->data
[1], b
);
588 port
->state
= STATE_SYSEX_1
;
592 port
->state
= STATE_SYSEX_2
;
595 output_packet(urb
, p0
| 0x04, port
->data
[0], port
->data
[1], b
);
596 port
->state
= STATE_SYSEX_0
;
602 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint
* ep
,
607 /* FIXME: lower-numbered ports can starve higher-numbered ports */
608 for (p
= 0; p
< 0x10; ++p
) {
609 struct usbmidi_out_port
* port
= &ep
->ports
[p
];
612 while (urb
->transfer_buffer_length
+ 3 < ep
->max_transfer
) {
614 if (snd_rawmidi_transmit(port
->substream
, &b
, 1) != 1) {
618 snd_usbmidi_transmit_byte(port
, b
, urb
);
623 static struct usb_protocol_ops snd_usbmidi_standard_ops
= {
624 .input
= snd_usbmidi_standard_input
,
625 .output
= snd_usbmidi_standard_output
,
626 .output_packet
= snd_usbmidi_output_standard_packet
,
629 static struct usb_protocol_ops snd_usbmidi_midiman_ops
= {
630 .input
= snd_usbmidi_midiman_input
,
631 .output
= snd_usbmidi_standard_output
,
632 .output_packet
= snd_usbmidi_output_midiman_packet
,
635 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops
= {
636 .input
= snd_usbmidi_maudio_broken_running_status_input
,
637 .output
= snd_usbmidi_standard_output
,
638 .output_packet
= snd_usbmidi_output_standard_packet
,
641 static struct usb_protocol_ops snd_usbmidi_cme_ops
= {
642 .input
= snd_usbmidi_cme_input
,
643 .output
= snd_usbmidi_standard_output
,
644 .output_packet
= snd_usbmidi_output_standard_packet
,
648 * AKAI MPD16 protocol:
650 * For control port (endpoint 1):
651 * ==============================
652 * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
653 * SysEx message (msg_len=9 bytes long).
655 * For data port (endpoint 2):
656 * ===========================
657 * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
658 * MIDI message (msg_len bytes long)
660 * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
662 static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint
*ep
,
663 uint8_t *buffer
, int buffer_length
)
665 unsigned int pos
= 0;
666 unsigned int len
= (unsigned int)buffer_length
;
668 unsigned int port
= (buffer
[pos
] >> 4) - 1;
669 unsigned int msg_len
= buffer
[pos
] & 0x0f;
671 if (pos
+ msg_len
<= len
&& port
< 2)
672 snd_usbmidi_input_data(ep
, 0, &buffer
[pos
], msg_len
);
677 #define MAX_AKAI_SYSEX_LEN 9
679 static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint
*ep
,
683 int pos
, end
, count
, buf_end
;
684 uint8_t tmp
[MAX_AKAI_SYSEX_LEN
];
685 struct snd_rawmidi_substream
*substream
= ep
->ports
[0].substream
;
687 if (!ep
->ports
[0].active
)
690 msg
= urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
691 buf_end
= ep
->max_transfer
- MAX_AKAI_SYSEX_LEN
- 1;
693 /* only try adding more data when there's space for at least 1 SysEx */
694 while (urb
->transfer_buffer_length
< buf_end
) {
695 count
= snd_rawmidi_transmit_peek(substream
,
696 tmp
, MAX_AKAI_SYSEX_LEN
);
698 ep
->ports
[0].active
= 0;
701 /* try to skip non-SysEx data */
702 for (pos
= 0; pos
< count
&& tmp
[pos
] != 0xF0; pos
++)
706 snd_rawmidi_transmit_ack(substream
, pos
);
710 /* look for the start or end marker */
711 for (end
= 1; end
< count
&& tmp
[end
] < 0xF0; end
++)
714 /* next SysEx started before the end of current one */
715 if (end
< count
&& tmp
[end
] == 0xF0) {
716 /* it's incomplete - drop it */
717 snd_rawmidi_transmit_ack(substream
, end
);
721 if (end
< count
&& tmp
[end
] == 0xF7) {
722 /* queue it, ack it, and get the next one */
724 msg
[0] = 0x10 | count
;
725 memcpy(&msg
[1], tmp
, count
);
726 snd_rawmidi_transmit_ack(substream
, count
);
727 urb
->transfer_buffer_length
+= count
+ 1;
731 /* less than 9 bytes and no end byte - wait for more */
732 if (count
< MAX_AKAI_SYSEX_LEN
) {
733 ep
->ports
[0].active
= 0;
736 /* 9 bytes and no end marker in sight - malformed, skip it */
737 snd_rawmidi_transmit_ack(substream
, count
);
741 static struct usb_protocol_ops snd_usbmidi_akai_ops
= {
742 .input
= snd_usbmidi_akai_input
,
743 .output
= snd_usbmidi_akai_output
,
747 * Novation USB MIDI protocol: number of data bytes is in the first byte
748 * (when receiving) (+1!) or in the second byte (when sending); data begins
752 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint
* ep
,
753 uint8_t* buffer
, int buffer_length
)
755 if (buffer_length
< 2 || !buffer
[0] || buffer_length
< buffer
[0] + 1)
757 snd_usbmidi_input_data(ep
, 0, &buffer
[2], buffer
[0] - 1);
760 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint
* ep
,
763 uint8_t* transfer_buffer
;
766 if (!ep
->ports
[0].active
)
768 transfer_buffer
= urb
->transfer_buffer
;
769 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
771 ep
->max_transfer
- 2);
773 ep
->ports
[0].active
= 0;
776 transfer_buffer
[0] = 0;
777 transfer_buffer
[1] = count
;
778 urb
->transfer_buffer_length
= 2 + count
;
781 static struct usb_protocol_ops snd_usbmidi_novation_ops
= {
782 .input
= snd_usbmidi_novation_input
,
783 .output
= snd_usbmidi_novation_output
,
787 * "raw" protocol: just move raw MIDI bytes from/to the endpoint
790 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint
* ep
,
791 uint8_t* buffer
, int buffer_length
)
793 snd_usbmidi_input_data(ep
, 0, buffer
, buffer_length
);
796 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint
* ep
,
801 if (!ep
->ports
[0].active
)
803 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
804 urb
->transfer_buffer
,
807 ep
->ports
[0].active
= 0;
810 urb
->transfer_buffer_length
= count
;
813 static struct usb_protocol_ops snd_usbmidi_raw_ops
= {
814 .input
= snd_usbmidi_raw_input
,
815 .output
= snd_usbmidi_raw_output
,
818 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint
*ep
,
819 uint8_t *buffer
, int buffer_length
)
821 if (buffer_length
!= 9)
824 while (buffer_length
&& buffer
[buffer_length
- 1] == 0xFD)
827 snd_usbmidi_input_data(ep
, 0, buffer
, buffer_length
);
830 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint
*ep
,
835 if (!ep
->ports
[0].active
)
837 switch (snd_usb_get_speed(ep
->umidi
->dev
)) {
839 case USB_SPEED_SUPER
:
845 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
846 urb
->transfer_buffer
,
849 ep
->ports
[0].active
= 0;
853 memset(urb
->transfer_buffer
+ count
, 0xFD, 9 - count
);
854 urb
->transfer_buffer_length
= count
;
857 static struct usb_protocol_ops snd_usbmidi_122l_ops
= {
858 .input
= snd_usbmidi_us122l_input
,
859 .output
= snd_usbmidi_us122l_output
,
863 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
866 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint
* ep
)
868 static const u8 init_data
[] = {
869 /* initialization magic: "get version" */
871 0x00, 0x20, 0x31, /* Emagic */
873 0x0b, /* version number request */
874 0x00, /* command version */
875 0x00, /* EEPROM, box 0 */
878 send_bulk_static_data(ep
, init_data
, sizeof(init_data
));
879 /* while we're at it, pour on more magic */
880 send_bulk_static_data(ep
, init_data
, sizeof(init_data
));
883 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint
* ep
)
885 static const u8 finish_data
[] = {
886 /* switch to patch mode with last preset */
888 0x00, 0x20, 0x31, /* Emagic */
890 0x10, /* patch switch command */
891 0x00, /* command version */
892 0x7f, /* to all boxes */
893 0x40, /* last preset in EEPROM */
896 send_bulk_static_data(ep
, finish_data
, sizeof(finish_data
));
899 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint
* ep
,
900 uint8_t* buffer
, int buffer_length
)
904 /* FF indicates end of valid data */
905 for (i
= 0; i
< buffer_length
; ++i
)
906 if (buffer
[i
] == 0xff) {
911 /* handle F5 at end of last buffer */
915 while (buffer_length
> 0) {
916 /* determine size of data until next F5 */
917 for (i
= 0; i
< buffer_length
; ++i
)
918 if (buffer
[i
] == 0xf5)
920 snd_usbmidi_input_data(ep
, ep
->current_port
, buffer
, i
);
924 if (buffer_length
<= 0)
926 /* assert(buffer[0] == 0xf5); */
932 if (buffer_length
<= 0)
934 if (buffer
[0] < 0x80) {
935 ep
->current_port
= (buffer
[0] - 1) & 15;
943 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint
* ep
,
946 int port0
= ep
->current_port
;
947 uint8_t* buf
= urb
->transfer_buffer
;
948 int buf_free
= ep
->max_transfer
;
951 for (i
= 0; i
< 0x10; ++i
) {
952 /* round-robin, starting at the last current port */
953 int portnum
= (port0
+ i
) & 15;
954 struct usbmidi_out_port
* port
= &ep
->ports
[portnum
];
958 if (snd_rawmidi_transmit_peek(port
->substream
, buf
, 1) != 1) {
963 if (portnum
!= ep
->current_port
) {
966 ep
->current_port
= portnum
;
968 buf
[1] = (portnum
+ 1) & 15;
975 length
= snd_rawmidi_transmit(port
->substream
, buf
, buf_free
);
983 if (buf_free
< ep
->max_transfer
&& buf_free
> 0) {
987 urb
->transfer_buffer_length
= ep
->max_transfer
- buf_free
;
990 static struct usb_protocol_ops snd_usbmidi_emagic_ops
= {
991 .input
= snd_usbmidi_emagic_input
,
992 .output
= snd_usbmidi_emagic_output
,
993 .init_out_endpoint
= snd_usbmidi_emagic_init_out
,
994 .finish_out_endpoint
= snd_usbmidi_emagic_finish_out
,
998 static void update_roland_altsetting(struct snd_usb_midi
* umidi
)
1000 struct usb_interface
*intf
;
1001 struct usb_host_interface
*hostif
;
1002 struct usb_interface_descriptor
*intfd
;
1005 intf
= umidi
->iface
;
1006 is_light_load
= intf
->cur_altsetting
!= intf
->altsetting
;
1007 if (umidi
->roland_load_ctl
->private_value
== is_light_load
)
1009 hostif
= &intf
->altsetting
[umidi
->roland_load_ctl
->private_value
];
1010 intfd
= get_iface_desc(hostif
);
1011 snd_usbmidi_input_stop(&umidi
->list
);
1012 usb_set_interface(umidi
->dev
, intfd
->bInterfaceNumber
,
1013 intfd
->bAlternateSetting
);
1014 snd_usbmidi_input_start(&umidi
->list
);
1017 static void substream_open(struct snd_rawmidi_substream
*substream
, int open
)
1019 struct snd_usb_midi
* umidi
= substream
->rmidi
->private_data
;
1020 struct snd_kcontrol
*ctl
;
1022 mutex_lock(&umidi
->mutex
);
1024 if (umidi
->opened
++ == 0 && umidi
->roland_load_ctl
) {
1025 ctl
= umidi
->roland_load_ctl
;
1026 ctl
->vd
[0].access
|= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
1027 snd_ctl_notify(umidi
->card
,
1028 SNDRV_CTL_EVENT_MASK_INFO
, &ctl
->id
);
1029 update_roland_altsetting(umidi
);
1032 if (--umidi
->opened
== 0 && umidi
->roland_load_ctl
) {
1033 ctl
= umidi
->roland_load_ctl
;
1034 ctl
->vd
[0].access
&= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
1035 snd_ctl_notify(umidi
->card
,
1036 SNDRV_CTL_EVENT_MASK_INFO
, &ctl
->id
);
1039 mutex_unlock(&umidi
->mutex
);
1042 static int snd_usbmidi_output_open(struct snd_rawmidi_substream
*substream
)
1044 struct snd_usb_midi
* umidi
= substream
->rmidi
->private_data
;
1045 struct usbmidi_out_port
* port
= NULL
;
1048 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
1049 if (umidi
->endpoints
[i
].out
)
1050 for (j
= 0; j
< 0x10; ++j
)
1051 if (umidi
->endpoints
[i
].out
->ports
[j
].substream
== substream
) {
1052 port
= &umidi
->endpoints
[i
].out
->ports
[j
];
1059 substream
->runtime
->private_data
= port
;
1060 port
->state
= STATE_UNKNOWN
;
1061 substream_open(substream
, 1);
1065 static int snd_usbmidi_output_close(struct snd_rawmidi_substream
*substream
)
1067 substream_open(substream
, 0);
1071 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1073 struct usbmidi_out_port
* port
= (struct usbmidi_out_port
*)substream
->runtime
->private_data
;
1077 if (port
->ep
->umidi
->disconnected
) {
1078 /* gobble up remaining bytes to prevent wait in
1079 * snd_rawmidi_drain_output */
1080 while (!snd_rawmidi_transmit_empty(substream
))
1081 snd_rawmidi_transmit_ack(substream
, 1);
1084 tasklet_schedule(&port
->ep
->tasklet
);
1088 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream
*substream
)
1090 struct usbmidi_out_port
* port
= substream
->runtime
->private_data
;
1091 struct snd_usb_midi_out_endpoint
*ep
= port
->ep
;
1092 unsigned int drain_urbs
;
1094 long timeout
= msecs_to_jiffies(50);
1096 if (ep
->umidi
->disconnected
)
1099 * The substream buffer is empty, but some data might still be in the
1100 * currently active URBs, so we have to wait for those to complete.
1102 spin_lock_irq(&ep
->buffer_lock
);
1103 drain_urbs
= ep
->active_urbs
;
1105 ep
->drain_urbs
|= drain_urbs
;
1107 prepare_to_wait(&ep
->drain_wait
, &wait
,
1108 TASK_UNINTERRUPTIBLE
);
1109 spin_unlock_irq(&ep
->buffer_lock
);
1110 timeout
= schedule_timeout(timeout
);
1111 spin_lock_irq(&ep
->buffer_lock
);
1112 drain_urbs
&= ep
->drain_urbs
;
1113 } while (drain_urbs
&& timeout
);
1114 finish_wait(&ep
->drain_wait
, &wait
);
1116 spin_unlock_irq(&ep
->buffer_lock
);
1119 static int snd_usbmidi_input_open(struct snd_rawmidi_substream
*substream
)
1121 substream_open(substream
, 1);
1125 static int snd_usbmidi_input_close(struct snd_rawmidi_substream
*substream
)
1127 substream_open(substream
, 0);
1131 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1133 struct snd_usb_midi
* umidi
= substream
->rmidi
->private_data
;
1136 set_bit(substream
->number
, &umidi
->input_triggered
);
1138 clear_bit(substream
->number
, &umidi
->input_triggered
);
1141 static struct snd_rawmidi_ops snd_usbmidi_output_ops
= {
1142 .open
= snd_usbmidi_output_open
,
1143 .close
= snd_usbmidi_output_close
,
1144 .trigger
= snd_usbmidi_output_trigger
,
1145 .drain
= snd_usbmidi_output_drain
,
1148 static struct snd_rawmidi_ops snd_usbmidi_input_ops
= {
1149 .open
= snd_usbmidi_input_open
,
1150 .close
= snd_usbmidi_input_close
,
1151 .trigger
= snd_usbmidi_input_trigger
1154 static void free_urb_and_buffer(struct snd_usb_midi
*umidi
, struct urb
*urb
,
1155 unsigned int buffer_length
)
1157 usb_free_coherent(umidi
->dev
, buffer_length
,
1158 urb
->transfer_buffer
, urb
->transfer_dma
);
1163 * Frees an input endpoint.
1164 * May be called when ep hasn't been initialized completely.
1166 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint
* ep
)
1170 for (i
= 0; i
< INPUT_URBS
; ++i
)
1172 free_urb_and_buffer(ep
->umidi
, ep
->urbs
[i
],
1173 ep
->urbs
[i
]->transfer_buffer_length
);
1178 * Creates an input endpoint.
1180 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi
* umidi
,
1181 struct snd_usb_midi_endpoint_info
* ep_info
,
1182 struct snd_usb_midi_endpoint
* rep
)
1184 struct snd_usb_midi_in_endpoint
* ep
;
1191 ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
1196 for (i
= 0; i
< INPUT_URBS
; ++i
) {
1197 ep
->urbs
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
1199 snd_usbmidi_in_endpoint_delete(ep
);
1203 if (ep_info
->in_interval
)
1204 pipe
= usb_rcvintpipe(umidi
->dev
, ep_info
->in_ep
);
1206 pipe
= usb_rcvbulkpipe(umidi
->dev
, ep_info
->in_ep
);
1207 length
= usb_maxpacket(umidi
->dev
, pipe
, 0);
1208 for (i
= 0; i
< INPUT_URBS
; ++i
) {
1209 buffer
= usb_alloc_coherent(umidi
->dev
, length
, GFP_KERNEL
,
1210 &ep
->urbs
[i
]->transfer_dma
);
1212 snd_usbmidi_in_endpoint_delete(ep
);
1215 if (ep_info
->in_interval
)
1216 usb_fill_int_urb(ep
->urbs
[i
], umidi
->dev
,
1217 pipe
, buffer
, length
,
1218 snd_usbmidi_in_urb_complete
,
1219 ep
, ep_info
->in_interval
);
1221 usb_fill_bulk_urb(ep
->urbs
[i
], umidi
->dev
,
1222 pipe
, buffer
, length
,
1223 snd_usbmidi_in_urb_complete
, ep
);
1224 ep
->urbs
[i
]->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
1232 * Frees an output endpoint.
1233 * May be called when ep hasn't been initialized completely.
1235 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint
*ep
)
1239 for (i
= 0; i
< OUTPUT_URBS
; ++i
)
1240 if (ep
->urbs
[i
].urb
) {
1241 free_urb_and_buffer(ep
->umidi
, ep
->urbs
[i
].urb
,
1243 ep
->urbs
[i
].urb
= NULL
;
1247 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint
*ep
)
1249 snd_usbmidi_out_endpoint_clear(ep
);
1254 * Creates an output endpoint, and initializes output ports.
1256 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi
* umidi
,
1257 struct snd_usb_midi_endpoint_info
* ep_info
,
1258 struct snd_usb_midi_endpoint
* rep
)
1260 struct snd_usb_midi_out_endpoint
* ep
;
1266 ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
1271 for (i
= 0; i
< OUTPUT_URBS
; ++i
) {
1272 ep
->urbs
[i
].urb
= usb_alloc_urb(0, GFP_KERNEL
);
1273 if (!ep
->urbs
[i
].urb
) {
1274 snd_usbmidi_out_endpoint_delete(ep
);
1277 ep
->urbs
[i
].ep
= ep
;
1279 if (ep_info
->out_interval
)
1280 pipe
= usb_sndintpipe(umidi
->dev
, ep_info
->out_ep
);
1282 pipe
= usb_sndbulkpipe(umidi
->dev
, ep_info
->out_ep
);
1283 switch (umidi
->usb_id
) {
1285 ep
->max_transfer
= usb_maxpacket(umidi
->dev
, pipe
, 1);
1288 * Various chips declare a packet size larger than 4 bytes, but
1289 * do not actually work with larger packets:
1291 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1292 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1293 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1294 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1295 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1296 ep
->max_transfer
= 4;
1299 for (i
= 0; i
< OUTPUT_URBS
; ++i
) {
1300 buffer
= usb_alloc_coherent(umidi
->dev
,
1301 ep
->max_transfer
, GFP_KERNEL
,
1302 &ep
->urbs
[i
].urb
->transfer_dma
);
1304 snd_usbmidi_out_endpoint_delete(ep
);
1307 if (ep_info
->out_interval
)
1308 usb_fill_int_urb(ep
->urbs
[i
].urb
, umidi
->dev
,
1309 pipe
, buffer
, ep
->max_transfer
,
1310 snd_usbmidi_out_urb_complete
,
1311 &ep
->urbs
[i
], ep_info
->out_interval
);
1313 usb_fill_bulk_urb(ep
->urbs
[i
].urb
, umidi
->dev
,
1314 pipe
, buffer
, ep
->max_transfer
,
1315 snd_usbmidi_out_urb_complete
,
1317 ep
->urbs
[i
].urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
1320 spin_lock_init(&ep
->buffer_lock
);
1321 tasklet_init(&ep
->tasklet
, snd_usbmidi_out_tasklet
, (unsigned long)ep
);
1322 init_waitqueue_head(&ep
->drain_wait
);
1324 for (i
= 0; i
< 0x10; ++i
)
1325 if (ep_info
->out_cables
& (1 << i
)) {
1326 ep
->ports
[i
].ep
= ep
;
1327 ep
->ports
[i
].cable
= i
<< 4;
1330 if (umidi
->usb_protocol_ops
->init_out_endpoint
)
1331 umidi
->usb_protocol_ops
->init_out_endpoint(ep
);
1340 static void snd_usbmidi_free(struct snd_usb_midi
* umidi
)
1344 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1345 struct snd_usb_midi_endpoint
* ep
= &umidi
->endpoints
[i
];
1347 snd_usbmidi_out_endpoint_delete(ep
->out
);
1349 snd_usbmidi_in_endpoint_delete(ep
->in
);
1351 mutex_destroy(&umidi
->mutex
);
1356 * Unlinks all URBs (must be done before the usb_device is deleted).
1358 void snd_usbmidi_disconnect(struct list_head
* p
)
1360 struct snd_usb_midi
* umidi
;
1363 umidi
= list_entry(p
, struct snd_usb_midi
, list
);
1365 * an URB's completion handler may start the timer and
1366 * a timer may submit an URB. To reliably break the cycle
1367 * a flag under lock must be used
1369 spin_lock_irq(&umidi
->disc_lock
);
1370 umidi
->disconnected
= 1;
1371 spin_unlock_irq(&umidi
->disc_lock
);
1372 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1373 struct snd_usb_midi_endpoint
* ep
= &umidi
->endpoints
[i
];
1375 tasklet_kill(&ep
->out
->tasklet
);
1377 for (j
= 0; j
< OUTPUT_URBS
; ++j
)
1378 usb_kill_urb(ep
->out
->urbs
[j
].urb
);
1379 if (umidi
->usb_protocol_ops
->finish_out_endpoint
)
1380 umidi
->usb_protocol_ops
->finish_out_endpoint(ep
->out
);
1381 ep
->out
->active_urbs
= 0;
1382 if (ep
->out
->drain_urbs
) {
1383 ep
->out
->drain_urbs
= 0;
1384 wake_up(&ep
->out
->drain_wait
);
1388 for (j
= 0; j
< INPUT_URBS
; ++j
)
1389 usb_kill_urb(ep
->in
->urbs
[j
]);
1390 /* free endpoints here; later call can result in Oops */
1392 snd_usbmidi_out_endpoint_clear(ep
->out
);
1394 snd_usbmidi_in_endpoint_delete(ep
->in
);
1398 del_timer_sync(&umidi
->error_timer
);
1401 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi
*rmidi
)
1403 struct snd_usb_midi
* umidi
= rmidi
->private_data
;
1404 snd_usbmidi_free(umidi
);
1407 static struct snd_rawmidi_substream
*snd_usbmidi_find_substream(struct snd_usb_midi
* umidi
,
1408 int stream
, int number
)
1410 struct list_head
* list
;
1412 list_for_each(list
, &umidi
->rmidi
->streams
[stream
].substreams
) {
1413 struct snd_rawmidi_substream
*substream
= list_entry(list
, struct snd_rawmidi_substream
, list
);
1414 if (substream
->number
== number
)
1421 * This list specifies names for ports that do not fit into the standard
1422 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1423 * such as internal control or synthesizer ports.
1425 static struct port_info
{
1430 unsigned int seq_flags
;
1431 } snd_usbmidi_port_info
[] = {
1432 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1433 { .id = USB_ID(vendor, product), \
1434 .port = num, .voices = voices_, \
1435 .name = name_, .seq_flags = flags }
1436 #define EXTERNAL_PORT(vendor, product, num, name) \
1437 PORT_INFO(vendor, product, num, name, 0, \
1438 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1439 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1440 SNDRV_SEQ_PORT_TYPE_PORT)
1441 #define CONTROL_PORT(vendor, product, num, name) \
1442 PORT_INFO(vendor, product, num, name, 0, \
1443 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1444 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1445 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1446 PORT_INFO(vendor, product, num, name, voices, \
1447 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1448 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1449 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1450 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1451 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1452 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1453 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1454 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1455 PORT_INFO(vendor, product, num, name, voices, \
1456 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1457 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1458 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1459 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1460 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1461 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1462 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1463 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1465 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1466 /* Roland SC-8850 */
1467 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1468 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1469 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1470 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1471 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1472 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1474 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1475 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1476 /* Roland SC-8820 */
1477 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1478 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1479 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1481 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1482 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1483 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1485 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1486 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1487 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1489 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1491 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1492 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1493 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1494 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1496 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1498 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1499 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1500 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1502 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1503 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1504 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1505 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1507 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1508 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1510 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1511 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1512 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1514 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1515 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1516 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1518 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1519 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1520 /* Edirol UA-1000 */
1521 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1522 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1524 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1525 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1526 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1528 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1529 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1530 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1532 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1533 /* M-Audio MidiSport 8x8 */
1534 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1535 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1537 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1538 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1539 /* Emagic Unitor8/AMT8/MT4 */
1540 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1541 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1542 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1544 CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1545 PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1546 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
|
1547 SNDRV_SEQ_PORT_TYPE_HARDWARE
),
1548 /* Access Music Virus TI */
1549 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1550 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1551 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
|
1552 SNDRV_SEQ_PORT_TYPE_HARDWARE
|
1553 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER
),
1556 static struct port_info
*find_port_info(struct snd_usb_midi
* umidi
, int number
)
1560 for (i
= 0; i
< ARRAY_SIZE(snd_usbmidi_port_info
); ++i
) {
1561 if (snd_usbmidi_port_info
[i
].id
== umidi
->usb_id
&&
1562 snd_usbmidi_port_info
[i
].port
== number
)
1563 return &snd_usbmidi_port_info
[i
];
1568 static void snd_usbmidi_get_port_info(struct snd_rawmidi
*rmidi
, int number
,
1569 struct snd_seq_port_info
*seq_port_info
)
1571 struct snd_usb_midi
*umidi
= rmidi
->private_data
;
1572 struct port_info
*port_info
;
1574 /* TODO: read port flags from descriptors */
1575 port_info
= find_port_info(umidi
, number
);
1577 seq_port_info
->type
= port_info
->seq_flags
;
1578 seq_port_info
->midi_voices
= port_info
->voices
;
1582 static void snd_usbmidi_init_substream(struct snd_usb_midi
* umidi
,
1583 int stream
, int number
,
1584 struct snd_rawmidi_substream
** rsubstream
)
1586 struct port_info
*port_info
;
1587 const char *name_format
;
1589 struct snd_rawmidi_substream
*substream
= snd_usbmidi_find_substream(umidi
, stream
, number
);
1591 snd_printd(KERN_ERR
"substream %d:%d not found\n", stream
, number
);
1595 /* TODO: read port name from jack descriptor */
1596 port_info
= find_port_info(umidi
, number
);
1597 name_format
= port_info
? port_info
->name
: "%s MIDI %d";
1598 snprintf(substream
->name
, sizeof(substream
->name
),
1599 name_format
, umidi
->card
->shortname
, number
+ 1);
1601 *rsubstream
= substream
;
1605 * Creates the endpoints and their ports.
1607 static int snd_usbmidi_create_endpoints(struct snd_usb_midi
* umidi
,
1608 struct snd_usb_midi_endpoint_info
* endpoints
)
1611 int out_ports
= 0, in_ports
= 0;
1613 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1614 if (endpoints
[i
].out_cables
) {
1615 err
= snd_usbmidi_out_endpoint_create(umidi
, &endpoints
[i
],
1616 &umidi
->endpoints
[i
]);
1620 if (endpoints
[i
].in_cables
) {
1621 err
= snd_usbmidi_in_endpoint_create(umidi
, &endpoints
[i
],
1622 &umidi
->endpoints
[i
]);
1627 for (j
= 0; j
< 0x10; ++j
) {
1628 if (endpoints
[i
].out_cables
& (1 << j
)) {
1629 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, out_ports
,
1630 &umidi
->endpoints
[i
].out
->ports
[j
].substream
);
1633 if (endpoints
[i
].in_cables
& (1 << j
)) {
1634 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, in_ports
,
1635 &umidi
->endpoints
[i
].in
->ports
[j
].substream
);
1640 snd_printdd(KERN_INFO
"created %d output and %d input ports\n",
1641 out_ports
, in_ports
);
1646 * Returns MIDIStreaming device capabilities.
1648 static int snd_usbmidi_get_ms_info(struct snd_usb_midi
* umidi
,
1649 struct snd_usb_midi_endpoint_info
* endpoints
)
1651 struct usb_interface
* intf
;
1652 struct usb_host_interface
*hostif
;
1653 struct usb_interface_descriptor
* intfd
;
1654 struct usb_ms_header_descriptor
* ms_header
;
1655 struct usb_host_endpoint
*hostep
;
1656 struct usb_endpoint_descriptor
* ep
;
1657 struct usb_ms_endpoint_descriptor
* ms_ep
;
1660 intf
= umidi
->iface
;
1663 hostif
= &intf
->altsetting
[0];
1664 intfd
= get_iface_desc(hostif
);
1665 ms_header
= (struct usb_ms_header_descriptor
*)hostif
->extra
;
1666 if (hostif
->extralen
>= 7 &&
1667 ms_header
->bLength
>= 7 &&
1668 ms_header
->bDescriptorType
== USB_DT_CS_INTERFACE
&&
1669 ms_header
->bDescriptorSubtype
== UAC_HEADER
)
1670 snd_printdd(KERN_INFO
"MIDIStreaming version %02x.%02x\n",
1671 ms_header
->bcdMSC
[1], ms_header
->bcdMSC
[0]);
1673 snd_printk(KERN_WARNING
"MIDIStreaming interface descriptor not found\n");
1676 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
1677 hostep
= &hostif
->endpoint
[i
];
1678 ep
= get_ep_desc(hostep
);
1679 if (!usb_endpoint_xfer_bulk(ep
) && !usb_endpoint_xfer_int(ep
))
1681 ms_ep
= (struct usb_ms_endpoint_descriptor
*)hostep
->extra
;
1682 if (hostep
->extralen
< 4 ||
1683 ms_ep
->bLength
< 4 ||
1684 ms_ep
->bDescriptorType
!= USB_DT_CS_ENDPOINT
||
1685 ms_ep
->bDescriptorSubtype
!= UAC_MS_GENERAL
)
1687 if (usb_endpoint_dir_out(ep
)) {
1688 if (endpoints
[epidx
].out_ep
) {
1689 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
1690 snd_printk(KERN_WARNING
"too many endpoints\n");
1694 endpoints
[epidx
].out_ep
= usb_endpoint_num(ep
);
1695 if (usb_endpoint_xfer_int(ep
))
1696 endpoints
[epidx
].out_interval
= ep
->bInterval
;
1697 else if (snd_usb_get_speed(umidi
->dev
) == USB_SPEED_LOW
)
1699 * Low speed bulk transfers don't exist, so
1700 * force interrupt transfers for devices like
1701 * ESI MIDI Mate that try to use them anyway.
1703 endpoints
[epidx
].out_interval
= 1;
1704 endpoints
[epidx
].out_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
1705 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
1706 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
1708 if (endpoints
[epidx
].in_ep
) {
1709 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
1710 snd_printk(KERN_WARNING
"too many endpoints\n");
1714 endpoints
[epidx
].in_ep
= usb_endpoint_num(ep
);
1715 if (usb_endpoint_xfer_int(ep
))
1716 endpoints
[epidx
].in_interval
= ep
->bInterval
;
1717 else if (snd_usb_get_speed(umidi
->dev
) == USB_SPEED_LOW
)
1718 endpoints
[epidx
].in_interval
= 1;
1719 endpoints
[epidx
].in_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
1720 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
1721 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
1727 static int roland_load_info(struct snd_kcontrol
*kcontrol
,
1728 struct snd_ctl_elem_info
*info
)
1730 static const char *const names
[] = { "High Load", "Light Load" };
1732 info
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1734 info
->value
.enumerated
.items
= 2;
1735 if (info
->value
.enumerated
.item
> 1)
1736 info
->value
.enumerated
.item
= 1;
1737 strcpy(info
->value
.enumerated
.name
, names
[info
->value
.enumerated
.item
]);
1741 static int roland_load_get(struct snd_kcontrol
*kcontrol
,
1742 struct snd_ctl_elem_value
*value
)
1744 value
->value
.enumerated
.item
[0] = kcontrol
->private_value
;
1748 static int roland_load_put(struct snd_kcontrol
*kcontrol
,
1749 struct snd_ctl_elem_value
*value
)
1751 struct snd_usb_midi
* umidi
= kcontrol
->private_data
;
1754 if (value
->value
.enumerated
.item
[0] > 1)
1756 mutex_lock(&umidi
->mutex
);
1757 changed
= value
->value
.enumerated
.item
[0] != kcontrol
->private_value
;
1759 kcontrol
->private_value
= value
->value
.enumerated
.item
[0];
1760 mutex_unlock(&umidi
->mutex
);
1764 static struct snd_kcontrol_new roland_load_ctl
= {
1765 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1766 .name
= "MIDI Input Mode",
1767 .info
= roland_load_info
,
1768 .get
= roland_load_get
,
1769 .put
= roland_load_put
,
1774 * On Roland devices, use the second alternate setting to be able to use
1775 * the interrupt input endpoint.
1777 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi
* umidi
)
1779 struct usb_interface
* intf
;
1780 struct usb_host_interface
*hostif
;
1781 struct usb_interface_descriptor
* intfd
;
1783 intf
= umidi
->iface
;
1784 if (!intf
|| intf
->num_altsetting
!= 2)
1787 hostif
= &intf
->altsetting
[1];
1788 intfd
= get_iface_desc(hostif
);
1789 if (intfd
->bNumEndpoints
!= 2 ||
1790 (get_endpoint(hostif
, 0)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
||
1791 (get_endpoint(hostif
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1794 snd_printdd(KERN_INFO
"switching to altsetting %d with int ep\n",
1795 intfd
->bAlternateSetting
);
1796 usb_set_interface(umidi
->dev
, intfd
->bInterfaceNumber
,
1797 intfd
->bAlternateSetting
);
1799 umidi
->roland_load_ctl
= snd_ctl_new1(&roland_load_ctl
, umidi
);
1800 if (snd_ctl_add(umidi
->card
, umidi
->roland_load_ctl
) < 0)
1801 umidi
->roland_load_ctl
= NULL
;
1805 * Try to find any usable endpoints in the interface.
1807 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi
* umidi
,
1808 struct snd_usb_midi_endpoint_info
* endpoint
,
1811 struct usb_interface
* intf
;
1812 struct usb_host_interface
*hostif
;
1813 struct usb_interface_descriptor
* intfd
;
1814 struct usb_endpoint_descriptor
* epd
;
1815 int i
, out_eps
= 0, in_eps
= 0;
1817 if (USB_ID_VENDOR(umidi
->usb_id
) == 0x0582)
1818 snd_usbmidi_switch_roland_altsetting(umidi
);
1820 if (endpoint
[0].out_ep
|| endpoint
[0].in_ep
)
1823 intf
= umidi
->iface
;
1824 if (!intf
|| intf
->num_altsetting
< 1)
1826 hostif
= intf
->cur_altsetting
;
1827 intfd
= get_iface_desc(hostif
);
1829 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
1830 epd
= get_endpoint(hostif
, i
);
1831 if (!usb_endpoint_xfer_bulk(epd
) &&
1832 !usb_endpoint_xfer_int(epd
))
1834 if (out_eps
< max_endpoints
&&
1835 usb_endpoint_dir_out(epd
)) {
1836 endpoint
[out_eps
].out_ep
= usb_endpoint_num(epd
);
1837 if (usb_endpoint_xfer_int(epd
))
1838 endpoint
[out_eps
].out_interval
= epd
->bInterval
;
1841 if (in_eps
< max_endpoints
&&
1842 usb_endpoint_dir_in(epd
)) {
1843 endpoint
[in_eps
].in_ep
= usb_endpoint_num(epd
);
1844 if (usb_endpoint_xfer_int(epd
))
1845 endpoint
[in_eps
].in_interval
= epd
->bInterval
;
1849 return (out_eps
|| in_eps
) ? 0 : -ENOENT
;
1853 * Detects the endpoints for one-port-per-endpoint protocols.
1855 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi
* umidi
,
1856 struct snd_usb_midi_endpoint_info
* endpoints
)
1860 err
= snd_usbmidi_detect_endpoints(umidi
, endpoints
, MIDI_MAX_ENDPOINTS
);
1861 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1862 if (endpoints
[i
].out_ep
)
1863 endpoints
[i
].out_cables
= 0x0001;
1864 if (endpoints
[i
].in_ep
)
1865 endpoints
[i
].in_cables
= 0x0001;
1871 * Detects the endpoints and ports of Yamaha devices.
1873 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi
* umidi
,
1874 struct snd_usb_midi_endpoint_info
* endpoint
)
1876 struct usb_interface
* intf
;
1877 struct usb_host_interface
*hostif
;
1878 struct usb_interface_descriptor
* intfd
;
1881 intf
= umidi
->iface
;
1884 hostif
= intf
->altsetting
;
1885 intfd
= get_iface_desc(hostif
);
1886 if (intfd
->bNumEndpoints
< 1)
1890 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1891 * necessarily with any useful contents. So simply count 'em.
1893 for (cs_desc
= hostif
->extra
;
1894 cs_desc
< hostif
->extra
+ hostif
->extralen
&& cs_desc
[0] >= 2;
1895 cs_desc
+= cs_desc
[0]) {
1896 if (cs_desc
[1] == USB_DT_CS_INTERFACE
) {
1897 if (cs_desc
[2] == UAC_MIDI_IN_JACK
)
1898 endpoint
->in_cables
= (endpoint
->in_cables
<< 1) | 1;
1899 else if (cs_desc
[2] == UAC_MIDI_OUT_JACK
)
1900 endpoint
->out_cables
= (endpoint
->out_cables
<< 1) | 1;
1903 if (!endpoint
->in_cables
&& !endpoint
->out_cables
)
1906 return snd_usbmidi_detect_endpoints(umidi
, endpoint
, 1);
1910 * Creates the endpoints and their ports for Midiman devices.
1912 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi
* umidi
,
1913 struct snd_usb_midi_endpoint_info
* endpoint
)
1915 struct snd_usb_midi_endpoint_info ep_info
;
1916 struct usb_interface
* intf
;
1917 struct usb_host_interface
*hostif
;
1918 struct usb_interface_descriptor
* intfd
;
1919 struct usb_endpoint_descriptor
* epd
;
1922 intf
= umidi
->iface
;
1925 hostif
= intf
->altsetting
;
1926 intfd
= get_iface_desc(hostif
);
1928 * The various MidiSport devices have more or less random endpoint
1929 * numbers, so we have to identify the endpoints by their index in
1930 * the descriptor array, like the driver for that other OS does.
1932 * There is one interrupt input endpoint for all input ports, one
1933 * bulk output endpoint for even-numbered ports, and one for odd-
1934 * numbered ports. Both bulk output endpoints have corresponding
1935 * input bulk endpoints (at indices 1 and 3) which aren't used.
1937 if (intfd
->bNumEndpoints
< (endpoint
->out_cables
> 0x0001 ? 5 : 3)) {
1938 snd_printdd(KERN_ERR
"not enough endpoints\n");
1942 epd
= get_endpoint(hostif
, 0);
1943 if (!usb_endpoint_dir_in(epd
) || !usb_endpoint_xfer_int(epd
)) {
1944 snd_printdd(KERN_ERR
"endpoint[0] isn't interrupt\n");
1947 epd
= get_endpoint(hostif
, 2);
1948 if (!usb_endpoint_dir_out(epd
) || !usb_endpoint_xfer_bulk(epd
)) {
1949 snd_printdd(KERN_ERR
"endpoint[2] isn't bulk output\n");
1952 if (endpoint
->out_cables
> 0x0001) {
1953 epd
= get_endpoint(hostif
, 4);
1954 if (!usb_endpoint_dir_out(epd
) ||
1955 !usb_endpoint_xfer_bulk(epd
)) {
1956 snd_printdd(KERN_ERR
"endpoint[4] isn't bulk output\n");
1961 ep_info
.out_ep
= get_endpoint(hostif
, 2)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1962 ep_info
.out_interval
= 0;
1963 ep_info
.out_cables
= endpoint
->out_cables
& 0x5555;
1964 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1968 ep_info
.in_ep
= get_endpoint(hostif
, 0)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1969 ep_info
.in_interval
= get_endpoint(hostif
, 0)->bInterval
;
1970 ep_info
.in_cables
= endpoint
->in_cables
;
1971 err
= snd_usbmidi_in_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1975 if (endpoint
->out_cables
> 0x0001) {
1976 ep_info
.out_ep
= get_endpoint(hostif
, 4)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1977 ep_info
.out_cables
= endpoint
->out_cables
& 0xaaaa;
1978 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[1]);
1983 for (cable
= 0; cable
< 0x10; ++cable
) {
1984 if (endpoint
->out_cables
& (1 << cable
))
1985 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, cable
,
1986 &umidi
->endpoints
[cable
& 1].out
->ports
[cable
].substream
);
1987 if (endpoint
->in_cables
& (1 << cable
))
1988 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, cable
,
1989 &umidi
->endpoints
[0].in
->ports
[cable
].substream
);
1994 static struct snd_rawmidi_global_ops snd_usbmidi_ops
= {
1995 .get_port_info
= snd_usbmidi_get_port_info
,
1998 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi
* umidi
,
1999 int out_ports
, int in_ports
)
2001 struct snd_rawmidi
*rmidi
;
2004 err
= snd_rawmidi_new(umidi
->card
, "USB MIDI",
2005 umidi
->next_midi_device
++,
2006 out_ports
, in_ports
, &rmidi
);
2009 strcpy(rmidi
->name
, umidi
->card
->shortname
);
2010 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
2011 SNDRV_RAWMIDI_INFO_INPUT
|
2012 SNDRV_RAWMIDI_INFO_DUPLEX
;
2013 rmidi
->ops
= &snd_usbmidi_ops
;
2014 rmidi
->private_data
= umidi
;
2015 rmidi
->private_free
= snd_usbmidi_rawmidi_free
;
2016 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_usbmidi_output_ops
);
2017 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_usbmidi_input_ops
);
2019 umidi
->rmidi
= rmidi
;
2024 * Temporarily stop input.
2026 void snd_usbmidi_input_stop(struct list_head
* p
)
2028 struct snd_usb_midi
* umidi
;
2031 umidi
= list_entry(p
, struct snd_usb_midi
, list
);
2032 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
2033 struct snd_usb_midi_endpoint
* ep
= &umidi
->endpoints
[i
];
2035 for (j
= 0; j
< INPUT_URBS
; ++j
)
2036 usb_kill_urb(ep
->in
->urbs
[j
]);
2040 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint
* ep
)
2046 for (i
= 0; i
< INPUT_URBS
; ++i
) {
2047 struct urb
* urb
= ep
->urbs
[i
];
2048 urb
->dev
= ep
->umidi
->dev
;
2049 snd_usbmidi_submit_urb(urb
, GFP_KERNEL
);
2054 * Resume input after a call to snd_usbmidi_input_stop().
2056 void snd_usbmidi_input_start(struct list_head
* p
)
2058 struct snd_usb_midi
* umidi
;
2061 umidi
= list_entry(p
, struct snd_usb_midi
, list
);
2062 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
2063 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
2067 * Creates and registers everything needed for a MIDI streaming interface.
2069 int snd_usbmidi_create(struct snd_card
*card
,
2070 struct usb_interface
* iface
,
2071 struct list_head
*midi_list
,
2072 const struct snd_usb_audio_quirk
* quirk
)
2074 struct snd_usb_midi
* umidi
;
2075 struct snd_usb_midi_endpoint_info endpoints
[MIDI_MAX_ENDPOINTS
];
2076 int out_ports
, in_ports
;
2079 umidi
= kzalloc(sizeof(*umidi
), GFP_KERNEL
);
2082 umidi
->dev
= interface_to_usbdev(iface
);
2084 umidi
->iface
= iface
;
2085 umidi
->quirk
= quirk
;
2086 umidi
->usb_protocol_ops
= &snd_usbmidi_standard_ops
;
2087 init_timer(&umidi
->error_timer
);
2088 spin_lock_init(&umidi
->disc_lock
);
2089 mutex_init(&umidi
->mutex
);
2090 umidi
->usb_id
= USB_ID(le16_to_cpu(umidi
->dev
->descriptor
.idVendor
),
2091 le16_to_cpu(umidi
->dev
->descriptor
.idProduct
));
2092 umidi
->error_timer
.function
= snd_usbmidi_error_timer
;
2093 umidi
->error_timer
.data
= (unsigned long)umidi
;
2095 /* detect the endpoint(s) to use */
2096 memset(endpoints
, 0, sizeof(endpoints
));
2097 switch (quirk
? quirk
->type
: QUIRK_MIDI_STANDARD_INTERFACE
) {
2098 case QUIRK_MIDI_STANDARD_INTERFACE
:
2099 err
= snd_usbmidi_get_ms_info(umidi
, endpoints
);
2100 if (umidi
->usb_id
== USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
2101 umidi
->usb_protocol_ops
=
2102 &snd_usbmidi_maudio_broken_running_status_ops
;
2104 case QUIRK_MIDI_US122L
:
2105 umidi
->usb_protocol_ops
= &snd_usbmidi_122l_ops
;
2107 case QUIRK_MIDI_FIXED_ENDPOINT
:
2108 memcpy(&endpoints
[0], quirk
->data
,
2109 sizeof(struct snd_usb_midi_endpoint_info
));
2110 err
= snd_usbmidi_detect_endpoints(umidi
, &endpoints
[0], 1);
2112 case QUIRK_MIDI_YAMAHA
:
2113 err
= snd_usbmidi_detect_yamaha(umidi
, &endpoints
[0]);
2115 case QUIRK_MIDI_MIDIMAN
:
2116 umidi
->usb_protocol_ops
= &snd_usbmidi_midiman_ops
;
2117 memcpy(&endpoints
[0], quirk
->data
,
2118 sizeof(struct snd_usb_midi_endpoint_info
));
2121 case QUIRK_MIDI_NOVATION
:
2122 umidi
->usb_protocol_ops
= &snd_usbmidi_novation_ops
;
2123 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
2125 case QUIRK_MIDI_RAW_BYTES
:
2126 umidi
->usb_protocol_ops
= &snd_usbmidi_raw_ops
;
2128 * Interface 1 contains isochronous endpoints, but with the same
2129 * numbers as in interface 0. Since it is interface 1 that the
2130 * USB core has most recently seen, these descriptors are now
2131 * associated with the endpoint numbers. This will foul up our
2132 * attempts to submit bulk/interrupt URBs to the endpoints in
2133 * interface 0, so we have to make sure that the USB core looks
2134 * again at interface 0 by calling usb_set_interface() on it.
2136 if (umidi
->usb_id
== USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2137 usb_set_interface(umidi
->dev
, 0, 0);
2138 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
2140 case QUIRK_MIDI_EMAGIC
:
2141 umidi
->usb_protocol_ops
= &snd_usbmidi_emagic_ops
;
2142 memcpy(&endpoints
[0], quirk
->data
,
2143 sizeof(struct snd_usb_midi_endpoint_info
));
2144 err
= snd_usbmidi_detect_endpoints(umidi
, &endpoints
[0], 1);
2146 case QUIRK_MIDI_CME
:
2147 umidi
->usb_protocol_ops
= &snd_usbmidi_cme_ops
;
2148 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
2150 case QUIRK_MIDI_AKAI
:
2151 umidi
->usb_protocol_ops
= &snd_usbmidi_akai_ops
;
2152 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
2153 /* endpoint 1 is input-only */
2154 endpoints
[1].out_cables
= 0;
2157 snd_printd(KERN_ERR
"invalid quirk type %d\n", quirk
->type
);
2166 /* create rawmidi device */
2169 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
2170 out_ports
+= hweight16(endpoints
[i
].out_cables
);
2171 in_ports
+= hweight16(endpoints
[i
].in_cables
);
2173 err
= snd_usbmidi_create_rawmidi(umidi
, out_ports
, in_ports
);
2179 /* create endpoint/port structures */
2180 if (quirk
&& quirk
->type
== QUIRK_MIDI_MIDIMAN
)
2181 err
= snd_usbmidi_create_endpoints_midiman(umidi
, &endpoints
[0]);
2183 err
= snd_usbmidi_create_endpoints(umidi
, endpoints
);
2185 snd_usbmidi_free(umidi
);
2189 list_add_tail(&umidi
->list
, midi_list
);
2191 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
2192 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
2196 EXPORT_SYMBOL(snd_usbmidi_create
);
2197 EXPORT_SYMBOL(snd_usbmidi_input_stop
);
2198 EXPORT_SYMBOL(snd_usbmidi_input_start
);
2199 EXPORT_SYMBOL(snd_usbmidi_disconnect
);