2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2004 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 <sound/driver.h>
39 #include <linux/kernel.h>
40 #include <linux/types.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/usb.h>
47 #include <sound/core.h>
48 #include <sound/minors.h>
49 #include <sound/rawmidi.h>
52 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
53 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
54 MODULE_LICENSE("Dual BSD/GPL");
57 struct usb_ms_header_descriptor
{
60 __u8 bDescriptorSubtype
;
63 } __attribute__ ((packed
));
65 struct usb_ms_endpoint_descriptor
{
68 __u8 bDescriptorSubtype
;
70 __u8 baAssocJackID
[0];
71 } __attribute__ ((packed
));
73 typedef struct snd_usb_midi snd_usb_midi_t
;
74 typedef struct snd_usb_midi_endpoint snd_usb_midi_endpoint_t
;
75 typedef struct snd_usb_midi_out_endpoint snd_usb_midi_out_endpoint_t
;
76 typedef struct snd_usb_midi_in_endpoint snd_usb_midi_in_endpoint_t
;
77 typedef struct usbmidi_out_port usbmidi_out_port_t
;
78 typedef struct usbmidi_in_port usbmidi_in_port_t
;
81 snd_usb_audio_t
*chip
;
82 struct usb_interface
*iface
;
83 const snd_usb_audio_quirk_t
*quirk
;
85 struct list_head list
;
87 struct snd_usb_midi_endpoint
{
88 snd_usb_midi_out_endpoint_t
*out
;
89 snd_usb_midi_in_endpoint_t
*in
;
90 } endpoints
[MIDI_MAX_ENDPOINTS
];
93 struct snd_usb_midi_out_endpoint
{
94 snd_usb_midi_t
* umidi
;
96 int max_transfer
; /* size of urb buffer */
97 struct tasklet_struct tasklet
;
99 spinlock_t buffer_lock
;
101 struct usbmidi_out_port
{
102 snd_usb_midi_out_endpoint_t
* ep
;
103 snd_rawmidi_substream_t
* substream
;
105 uint8_t cable
; /* cable number << 4 */
107 #define STATE_UNKNOWN 0
108 #define STATE_1PARAM 1
109 #define STATE_2PARAM_1 2
110 #define STATE_2PARAM_2 3
111 #define STATE_SYSEX_0 4
112 #define STATE_SYSEX_1 5
113 #define STATE_SYSEX_2 6
118 struct snd_usb_midi_in_endpoint
{
119 snd_usb_midi_t
* umidi
;
121 struct usbmidi_in_port
{
122 snd_rawmidi_substream_t
* substream
;
126 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t
* ep
);
128 static const uint8_t snd_usbmidi_cin_length
[] = {
129 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
133 * Submits the URB, with error handling.
135 static int snd_usbmidi_submit_urb(struct urb
* urb
, int flags
)
137 int err
= usb_submit_urb(urb
, flags
);
138 if (err
< 0 && err
!= -ENODEV
)
139 snd_printk(KERN_ERR
"usb_submit_urb: %d\n", err
);
144 * Error handling for URB completion functions.
146 static int snd_usbmidi_urb_error(int status
)
148 if (status
== -ENOENT
)
149 return status
; /* killed */
150 if (status
== -EILSEQ
||
151 status
== -ECONNRESET
||
152 status
== -ETIMEDOUT
)
153 return -ENODEV
; /* device removed/shutdown */
154 snd_printk(KERN_ERR
"urb status %d\n", status
);
155 return 0; /* continue */
159 * Receives a USB MIDI packet.
161 static void snd_usbmidi_input_packet(snd_usb_midi_in_endpoint_t
* ep
,
164 int cable
= packet
[0] >> 4;
165 usbmidi_in_port_t
* port
= &ep
->ports
[cable
];
167 if (!port
->substream
) {
168 snd_printd("unexpected port %d!\n", cable
);
171 if (!port
->substream
->runtime
||
172 !port
->substream
->runtime
->trigger
)
174 snd_rawmidi_receive(port
->substream
, &packet
[1],
175 snd_usbmidi_cin_length
[packet
[0] & 0x0f]);
179 * Processes the data read from the device.
181 static void snd_usbmidi_in_urb_complete(struct urb
* urb
, struct pt_regs
*regs
)
183 snd_usb_midi_in_endpoint_t
* ep
= urb
->context
;
185 if (urb
->status
== 0) {
186 uint8_t* buffer
= (uint8_t*)ep
->urb
->transfer_buffer
;
189 for (i
= 0; i
+ 4 <= urb
->actual_length
; i
+= 4)
191 snd_usbmidi_input_packet(ep
, &buffer
[i
]);
193 if (snd_usbmidi_urb_error(urb
->status
) < 0)
197 if (usb_pipe_needs_resubmit(urb
->pipe
)) {
198 urb
->dev
= ep
->umidi
->chip
->dev
;
199 snd_usbmidi_submit_urb(urb
, GFP_ATOMIC
);
204 * Converts the data read from a Midiman device to standard USB MIDI packets.
206 static void snd_usbmidi_in_midiman_complete(struct urb
* urb
, struct pt_regs
*regs
)
208 if (urb
->status
== 0) {
209 uint8_t* buffer
= (uint8_t*)urb
->transfer_buffer
;
212 for (i
= 0; i
+ 4 <= urb
->actual_length
; i
+= 4) {
213 if (buffer
[i
+ 3] != 0) {
215 * snd_usbmidi_input_packet() doesn't check the
216 * contents of the message, so we simply use
217 * some random CIN with the desired length.
219 static const uint8_t cin
[4] = {
222 uint8_t ctl
= buffer
[i
+ 3];
223 buffer
[i
+ 3] = buffer
[i
+ 2];
224 buffer
[i
+ 2] = buffer
[i
+ 1];
225 buffer
[i
+ 1] = buffer
[i
+ 0];
226 buffer
[i
+ 0] = (ctl
& 0xf0) | cin
[ctl
& 3];
232 snd_usbmidi_in_urb_complete(urb
, regs
);
235 static void snd_usbmidi_out_urb_complete(struct urb
* urb
, struct pt_regs
*regs
)
237 snd_usb_midi_out_endpoint_t
* ep
= urb
->context
;
239 if (urb
->status
< 0) {
240 if (snd_usbmidi_urb_error(urb
->status
) < 0)
243 snd_usbmidi_do_output(ep
);
247 * Converts standard USB MIDI packets to what Midman devices expect.
249 static void snd_usbmidi_convert_to_midiman(struct urb
* urb
)
251 uint8_t* buffer
= (uint8_t*)urb
->transfer_buffer
;
254 for (i
= 0; i
+ 4 <= urb
->transfer_buffer_length
; i
+= 4) {
255 uint8_t cin
= buffer
[i
];
256 buffer
[i
+ 0] = buffer
[i
+ 1];
257 buffer
[i
+ 1] = buffer
[i
+ 2];
258 buffer
[i
+ 2] = buffer
[i
+ 3];
259 buffer
[i
+ 3] = (cin
& 0xf0) | snd_usbmidi_cin_length
[cin
& 0x0f];
264 * Adds one USB MIDI packet to the output buffer.
266 static inline void output_packet(struct urb
* urb
,
267 uint8_t p0
, uint8_t p1
, uint8_t p2
, uint8_t p3
)
270 uint8_t* buf
= (uint8_t*)urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
275 urb
->transfer_buffer_length
+= 4;
279 * Converts MIDI commands to USB MIDI packets.
281 static void snd_usbmidi_transmit_byte(usbmidi_out_port_t
* port
,
282 uint8_t b
, struct urb
* urb
)
284 uint8_t p0
= port
->cable
;
287 output_packet(urb
, p0
| 0x0f, b
, 0, 0);
288 } else if (b
>= 0xf0) {
292 port
->state
= STATE_SYSEX_1
;
297 port
->state
= STATE_1PARAM
;
301 port
->state
= STATE_2PARAM_1
;
305 port
->state
= STATE_UNKNOWN
;
308 output_packet(urb
, p0
| 0x05, 0xf6, 0, 0);
309 port
->state
= STATE_UNKNOWN
;
312 switch (port
->state
) {
314 output_packet(urb
, p0
| 0x05, 0xf7, 0, 0);
317 output_packet(urb
, p0
| 0x06, port
->data
[0], 0xf7, 0);
320 output_packet(urb
, p0
| 0x07, port
->data
[0], port
->data
[1], 0xf7);
323 port
->state
= STATE_UNKNOWN
;
326 } else if (b
>= 0x80) {
328 if (b
>= 0xc0 && b
<= 0xdf)
329 port
->state
= STATE_1PARAM
;
331 port
->state
= STATE_2PARAM_1
;
332 } else { /* b < 0x80 */
333 switch (port
->state
) {
335 if (port
->data
[0] < 0xf0) {
336 p0
|= port
->data
[0] >> 4;
339 port
->state
= STATE_UNKNOWN
;
341 output_packet(urb
, p0
, port
->data
[0], b
, 0);
345 port
->state
= STATE_2PARAM_2
;
348 if (port
->data
[0] < 0xf0) {
349 p0
|= port
->data
[0] >> 4;
350 port
->state
= STATE_2PARAM_1
;
353 port
->state
= STATE_UNKNOWN
;
355 output_packet(urb
, p0
, port
->data
[0], port
->data
[1], b
);
359 port
->state
= STATE_SYSEX_1
;
363 port
->state
= STATE_SYSEX_2
;
366 output_packet(urb
, p0
| 0x04, port
->data
[0], port
->data
[1], b
);
367 port
->state
= STATE_SYSEX_0
;
374 * Moves data from one substream buffer to the URB transfer buffer.
376 static void snd_usbmidi_transmit(snd_usb_midi_out_endpoint_t
* ep
, int port_idx
)
378 struct urb
* urb
= ep
->urb
;
379 usbmidi_out_port_t
* port
= &ep
->ports
[port_idx
];
381 while (urb
->transfer_buffer_length
< ep
->max_transfer
) {
383 if (snd_rawmidi_transmit_peek(port
->substream
, &b
, 1) != 1) {
387 snd_usbmidi_transmit_byte(port
, b
, urb
);
388 snd_rawmidi_transmit_ack(port
->substream
, 1);
393 * This is called when some data should be transferred to the device
394 * (from one or more substreams).
396 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t
* ep
)
399 struct urb
* urb
= ep
->urb
;
402 spin_lock_irqsave(&ep
->buffer_lock
, flags
);
403 if (urb
->status
== -EINPROGRESS
|| ep
->umidi
->chip
->shutdown
) {
404 spin_unlock_irqrestore(&ep
->buffer_lock
, flags
);
408 urb
->transfer_buffer_length
= 0;
409 for (p
= 0; p
< 0x10; ++p
)
410 if (ep
->ports
[p
].active
)
411 snd_usbmidi_transmit(ep
, p
);
413 if (urb
->transfer_buffer_length
> 0) {
414 if (ep
->umidi
->quirk
&& ep
->umidi
->quirk
->type
== QUIRK_MIDI_MIDIMAN
)
415 snd_usbmidi_convert_to_midiman(urb
);
417 urb
->dev
= ep
->umidi
->chip
->dev
;
418 snd_usbmidi_submit_urb(urb
, GFP_ATOMIC
);
420 spin_unlock_irqrestore(&ep
->buffer_lock
, flags
);
423 static void snd_usbmidi_out_tasklet(unsigned long data
)
425 snd_usb_midi_out_endpoint_t
* ep
= (snd_usb_midi_out_endpoint_t
*) data
;
427 snd_usbmidi_do_output(ep
);
430 static int snd_usbmidi_output_open(snd_rawmidi_substream_t
* substream
)
432 snd_usb_midi_t
* umidi
= substream
->rmidi
->private_data
;
433 usbmidi_out_port_t
* port
= NULL
;
436 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
437 if (umidi
->endpoints
[i
].out
)
438 for (j
= 0; j
< 0x10; ++j
)
439 if (umidi
->endpoints
[i
].out
->ports
[j
].substream
== substream
) {
440 port
= &umidi
->endpoints
[i
].out
->ports
[j
];
447 substream
->runtime
->private_data
= port
;
448 port
->state
= STATE_UNKNOWN
;
452 static int snd_usbmidi_output_close(snd_rawmidi_substream_t
* substream
)
457 static void snd_usbmidi_output_trigger(snd_rawmidi_substream_t
* substream
, int up
)
459 usbmidi_out_port_t
* port
= (usbmidi_out_port_t
*)substream
->runtime
->private_data
;
463 if (port
->ep
->umidi
->chip
->shutdown
) {
464 /* gobble up remaining bytes to prevent wait in
465 * snd_rawmidi_drain_output */
466 while (!snd_rawmidi_transmit_empty(substream
))
467 snd_rawmidi_transmit_ack(substream
, 1);
470 tasklet_hi_schedule(&port
->ep
->tasklet
);
474 static int snd_usbmidi_input_open(snd_rawmidi_substream_t
* substream
)
479 static int snd_usbmidi_input_close(snd_rawmidi_substream_t
* substream
)
484 static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t
* substream
, int up
)
488 static snd_rawmidi_ops_t snd_usbmidi_output_ops
= {
489 .open
= snd_usbmidi_output_open
,
490 .close
= snd_usbmidi_output_close
,
491 .trigger
= snd_usbmidi_output_trigger
,
494 static snd_rawmidi_ops_t snd_usbmidi_input_ops
= {
495 .open
= snd_usbmidi_input_open
,
496 .close
= snd_usbmidi_input_close
,
497 .trigger
= snd_usbmidi_input_trigger
501 * Frees an input endpoint.
502 * May be called when ep hasn't been initialized completely.
504 static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t
* ep
)
507 if (ep
->urb
->transfer_buffer
)
508 kfree(ep
->urb
->transfer_buffer
);
509 usb_free_urb(ep
->urb
);
515 * For Roland devices, use the alternate setting which uses interrupt
516 * transfers for input.
518 static struct usb_endpoint_descriptor
* snd_usbmidi_get_int_epd(snd_usb_midi_t
* umidi
)
520 struct usb_interface
* intf
;
521 struct usb_host_interface
*hostif
;
522 struct usb_interface_descriptor
* intfd
;
524 if (umidi
->chip
->dev
->descriptor
.idVendor
!= 0x0582)
527 if (!intf
|| intf
->num_altsetting
!= 2)
530 hostif
= &intf
->altsetting
[0];
531 intfd
= get_iface_desc(hostif
);
532 if (intfd
->bNumEndpoints
!= 2 ||
533 (get_endpoint(hostif
, 0)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
||
534 (get_endpoint(hostif
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
)
537 hostif
= &intf
->altsetting
[1];
538 intfd
= get_iface_desc(hostif
);
539 if (intfd
->bNumEndpoints
!= 2 ||
540 (get_endpoint(hostif
, 0)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
||
541 (get_endpoint(hostif
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
544 snd_printdd(KERN_INFO
"switching to altsetting %d with int ep\n",
545 intfd
->bAlternateSetting
);
546 usb_set_interface(umidi
->chip
->dev
, intfd
->bInterfaceNumber
,
547 intfd
->bAlternateSetting
);
548 return get_endpoint(hostif
, 1);
551 static struct usb_endpoint_descriptor
* snd_usbmidi_get_midiman_int_epd(snd_usb_midi_t
* umidi
)
553 struct usb_interface
* intf
= umidi
->iface
;
554 struct usb_host_interface
*hostif
;
555 struct usb_interface_descriptor
*intfd
;
558 hostif
= &intf
->altsetting
[0];
559 intfd
= get_iface_desc(hostif
);
560 if (intfd
->bNumEndpoints
< 1)
562 return get_endpoint(hostif
, 0);
566 * Creates an input endpoint.
568 static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t
* umidi
,
569 snd_usb_midi_endpoint_info_t
* ep_info
,
570 snd_usb_midi_endpoint_t
* rep
)
572 snd_usb_midi_in_endpoint_t
* ep
;
573 struct usb_endpoint_descriptor
* int_epd
;
579 ep
= kcalloc(1, sizeof(*ep
), GFP_KERNEL
);
584 if (umidi
->quirk
&& umidi
->quirk
->type
== QUIRK_MIDI_MIDIMAN
)
585 int_epd
= snd_usbmidi_get_midiman_int_epd(umidi
);
587 int_epd
= snd_usbmidi_get_int_epd(umidi
);
589 ep
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
591 snd_usbmidi_in_endpoint_delete(ep
);
595 pipe
= usb_rcvintpipe(umidi
->chip
->dev
, ep_info
->in_ep
);
597 pipe
= usb_rcvbulkpipe(umidi
->chip
->dev
, ep_info
->in_ep
);
598 length
= usb_maxpacket(umidi
->chip
->dev
, pipe
, 0);
599 buffer
= kmalloc(length
, GFP_KERNEL
);
601 snd_usbmidi_in_endpoint_delete(ep
);
605 usb_fill_int_urb(ep
->urb
, umidi
->chip
->dev
, pipe
, buffer
, length
,
606 snd_usb_complete_callback(snd_usbmidi_in_urb_complete
),
607 ep
, int_epd
->bInterval
);
609 usb_fill_bulk_urb(ep
->urb
, umidi
->chip
->dev
, pipe
, buffer
, length
,
610 snd_usb_complete_callback(snd_usbmidi_in_urb_complete
),
617 static int snd_usbmidi_count_bits(uint16_t x
)
621 for (i
= 0; i
< 16; ++i
)
622 bits
+= (x
& (1 << i
)) != 0;
627 * Frees an output endpoint.
628 * May be called when ep hasn't been initialized completely.
630 static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t
* ep
)
632 if (ep
->tasklet
.func
)
633 tasklet_kill(&ep
->tasklet
);
635 if (ep
->urb
->transfer_buffer
)
636 kfree(ep
->urb
->transfer_buffer
);
637 usb_free_urb(ep
->urb
);
643 * Creates an output endpoint, and initializes output ports.
645 static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t
* umidi
,
646 snd_usb_midi_endpoint_info_t
* ep_info
,
647 snd_usb_midi_endpoint_t
* rep
)
649 snd_usb_midi_out_endpoint_t
* ep
;
655 ep
= kcalloc(1, sizeof(*ep
), GFP_KERNEL
);
660 ep
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
662 snd_usbmidi_out_endpoint_delete(ep
);
665 pipe
= usb_sndbulkpipe(umidi
->chip
->dev
, ep_info
->out_ep
);
666 ep
->max_transfer
= usb_maxpacket(umidi
->chip
->dev
, pipe
, 1) & ~3;
667 buffer
= kmalloc(ep
->max_transfer
, GFP_KERNEL
);
669 snd_usbmidi_out_endpoint_delete(ep
);
672 usb_fill_bulk_urb(ep
->urb
, umidi
->chip
->dev
, pipe
, buffer
,
674 snd_usb_complete_callback(snd_usbmidi_out_urb_complete
), ep
);
676 spin_lock_init(&ep
->buffer_lock
);
677 tasklet_init(&ep
->tasklet
, snd_usbmidi_out_tasklet
, (unsigned long)ep
);
679 for (i
= 0; i
< 0x10; ++i
)
680 if (ep_info
->out_cables
& (1 << i
)) {
681 ep
->ports
[i
].ep
= ep
;
682 ep
->ports
[i
].cable
= i
<< 4;
692 static void snd_usbmidi_free(snd_usb_midi_t
* umidi
)
696 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
697 snd_usb_midi_endpoint_t
* ep
= &umidi
->endpoints
[i
];
699 snd_usbmidi_out_endpoint_delete(ep
->out
);
701 snd_usbmidi_in_endpoint_delete(ep
->in
);
707 * Unlinks all URBs (must be done before the usb_device is deleted).
709 void snd_usbmidi_disconnect(struct list_head
* p
, struct usb_driver
*driver
)
711 snd_usb_midi_t
* umidi
;
714 umidi
= list_entry(p
, snd_usb_midi_t
, list
);
715 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
716 snd_usb_midi_endpoint_t
* ep
= &umidi
->endpoints
[i
];
717 if (ep
->out
&& ep
->out
->urb
)
718 usb_unlink_urb(ep
->out
->urb
);
719 if (ep
->in
&& ep
->in
->urb
)
720 usb_unlink_urb(ep
->in
->urb
);
724 static void snd_usbmidi_rawmidi_free(snd_rawmidi_t
* rmidi
)
726 snd_usb_midi_t
* umidi
= rmidi
->private_data
;
727 snd_usbmidi_free(umidi
);
730 static snd_rawmidi_substream_t
* snd_usbmidi_find_substream(snd_usb_midi_t
* umidi
,
731 int stream
, int number
)
733 struct list_head
* list
;
735 list_for_each(list
, &umidi
->rmidi
->streams
[stream
].substreams
) {
736 snd_rawmidi_substream_t
* substream
= list_entry(list
, snd_rawmidi_substream_t
, list
);
737 if (substream
->number
== number
)
744 * This list specifies names for ports that do not fit into the standard
745 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
746 * such as internal control or synthesizer ports.
752 const char *name_format
;
753 } snd_usbmidi_port_names
[] = {
755 {0x0582, 0x0000, 2, "%s Control"},
757 {0x0582, 0x0003, 0, "%s Part A"},
758 {0x0582, 0x0003, 1, "%s Part B"},
759 {0x0582, 0x0003, 2, "%s Part C"},
760 {0x0582, 0x0003, 3, "%s Part D"},
761 {0x0582, 0x0003, 4, "%s MIDI 1"},
762 {0x0582, 0x0003, 5, "%s MIDI 2"},
764 {0x0582, 0x0004, 0, "%s MIDI"},
765 {0x0582, 0x0004, 1, "%s Control"},
767 {0x0582, 0x0007, 0, "%s Part A"},
768 {0x0582, 0x0007, 1, "%s Part B"},
769 {0x0582, 0x0007, 2, "%s MIDI"},
771 {0x0582, 0x000b, 0, "%s Part A"},
772 {0x0582, 0x000b, 1, "%s Part B"},
773 {0x0582, 0x000b, 2, "%s MIDI"},
775 {0x0582, 0x000c, 0, "%s Part A"},
776 {0x0582, 0x000c, 1, "%s Part B"},
777 {0x0582, 0x000c, 2, "%s MIDI"},
779 {0x0582, 0x0014, 8, "%s Control"},
781 {0x0582, 0x0016, 0, "%s Part A"},
782 {0x0582, 0x0016, 1, "%s Part B"},
783 {0x0582, 0x0016, 2, "%s MIDI 1"},
784 {0x0582, 0x0016, 3, "%s MIDI 2"},
786 {0x0582, 0x0023, 5, "%s Control"},
788 {0x0582, 0x0027, 0, "%s Part A"},
789 {0x0582, 0x0027, 1, "%s Part B"},
790 {0x0582, 0x0027, 2, "%s MIDI"},
792 {0x0582, 0x0029, 0, "%s Part A"},
793 {0x0582, 0x0029, 1, "%s Part B"},
794 {0x0582, 0x0029, 2, "%s MIDI 1"},
795 {0x0582, 0x0029, 3, "%s MIDI 2"},
797 {0x0582, 0x002b, 0, "%s MIDI"},
798 {0x0582, 0x002b, 1, "%s Control"},
800 {0x0582, 0x002f, 0, "%s MIDI"},
801 {0x0582, 0x002f, 1, "%s External MIDI"},
802 {0x0582, 0x002f, 2, "%s Sync"},
804 {0x0582, 0x0033, 0, "%s MIDI"},
805 {0x0582, 0x0033, 1, "%s 1"},
806 {0x0582, 0x0033, 2, "%s 2"},
808 {0x0582, 0x003b, 0, "%s MIDI"},
809 {0x0582, 0x003b, 1, "%s Control"},
811 {0x0582, 0x0044, 0, "%s MIDI"},
812 {0x0582, 0x0044, 1, "%s Control"},
814 {0x0582, 0x0048, 0, "%s MIDI"},
815 {0x0582, 0x0048, 1, "%s 1"},
816 {0x0582, 0x0048, 2, "%s 2"},
818 {0x0582, 0x004d, 0, "%s MIDI"},
819 {0x0582, 0x004d, 1, "%s 1"},
820 {0x0582, 0x004d, 2, "%s 2"},
821 /* M-Audio MidiSport 8x8 */
822 {0x0763, 0x1031, 8, "%s Control"},
823 {0x0763, 0x1033, 8, "%s Control"},
826 static void snd_usbmidi_init_substream(snd_usb_midi_t
* umidi
,
827 int stream
, int number
,
828 snd_rawmidi_substream_t
** rsubstream
)
831 __u16 vendor
, product
;
832 const char *name_format
;
834 snd_rawmidi_substream_t
* substream
= snd_usbmidi_find_substream(umidi
, stream
, number
);
836 snd_printd(KERN_ERR
"substream %d:%d not found\n", stream
, number
);
840 /* TODO: read port name from jack descriptor */
841 name_format
= "%s MIDI %d";
842 vendor
= umidi
->chip
->dev
->descriptor
.idVendor
;
843 product
= umidi
->chip
->dev
->descriptor
.idProduct
;
844 for (i
= 0; i
< ARRAY_SIZE(snd_usbmidi_port_names
); ++i
) {
845 if (snd_usbmidi_port_names
[i
].vendor
== vendor
&&
846 snd_usbmidi_port_names
[i
].product
== product
&&
847 snd_usbmidi_port_names
[i
].port
== number
) {
848 name_format
= snd_usbmidi_port_names
[i
].name_format
;
852 snprintf(substream
->name
, sizeof(substream
->name
),
853 name_format
, umidi
->chip
->card
->shortname
, number
+ 1);
855 *rsubstream
= substream
;
859 * Creates the endpoints and their ports.
861 static int snd_usbmidi_create_endpoints(snd_usb_midi_t
* umidi
,
862 snd_usb_midi_endpoint_info_t
* endpoints
)
865 int out_ports
= 0, in_ports
= 0;
867 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
868 if (endpoints
[i
].out_cables
) {
869 err
= snd_usbmidi_out_endpoint_create(umidi
, &endpoints
[i
],
870 &umidi
->endpoints
[i
]);
874 if (endpoints
[i
].in_cables
) {
875 err
= snd_usbmidi_in_endpoint_create(umidi
, &endpoints
[i
],
876 &umidi
->endpoints
[i
]);
881 for (j
= 0; j
< 0x10; ++j
) {
882 if (endpoints
[i
].out_cables
& (1 << j
)) {
883 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, out_ports
,
884 &umidi
->endpoints
[i
].out
->ports
[j
].substream
);
887 if (endpoints
[i
].in_cables
& (1 << j
)) {
888 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, in_ports
,
889 &umidi
->endpoints
[i
].in
->ports
[j
].substream
);
894 snd_printdd(KERN_INFO
"created %d output and %d input ports\n",
895 out_ports
, in_ports
);
900 * Returns MIDIStreaming device capabilities.
902 static int snd_usbmidi_get_ms_info(snd_usb_midi_t
* umidi
,
903 snd_usb_midi_endpoint_info_t
* endpoints
)
905 struct usb_interface
* intf
;
906 struct usb_host_interface
*hostif
;
907 struct usb_interface_descriptor
* intfd
;
908 struct usb_ms_header_descriptor
* ms_header
;
909 struct usb_host_endpoint
*hostep
;
910 struct usb_endpoint_descriptor
* ep
;
911 struct usb_ms_endpoint_descriptor
* ms_ep
;
917 hostif
= &intf
->altsetting
[0];
918 intfd
= get_iface_desc(hostif
);
919 ms_header
= (struct usb_ms_header_descriptor
*)hostif
->extra
;
920 if (hostif
->extralen
>= 7 &&
921 ms_header
->bLength
>= 7 &&
922 ms_header
->bDescriptorType
== USB_DT_CS_INTERFACE
&&
923 ms_header
->bDescriptorSubtype
== HEADER
)
924 snd_printdd(KERN_INFO
"MIDIStreaming version %02x.%02x\n",
925 ms_header
->bcdMSC
[1], ms_header
->bcdMSC
[0]);
927 snd_printk(KERN_WARNING
"MIDIStreaming interface descriptor not found\n");
930 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
931 hostep
= &hostif
->endpoint
[i
];
932 ep
= get_ep_desc(hostep
);
933 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
)
935 ms_ep
= (struct usb_ms_endpoint_descriptor
*)hostep
->extra
;
936 if (hostep
->extralen
< 4 ||
937 ms_ep
->bLength
< 4 ||
938 ms_ep
->bDescriptorType
!= USB_DT_CS_ENDPOINT
||
939 ms_ep
->bDescriptorSubtype
!= MS_GENERAL
)
941 if ((ep
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
942 if (endpoints
[epidx
].out_ep
) {
943 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
944 snd_printk(KERN_WARNING
"too many endpoints\n");
948 endpoints
[epidx
].out_ep
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
949 endpoints
[epidx
].out_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
950 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
951 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
953 if (endpoints
[epidx
].in_ep
) {
954 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
955 snd_printk(KERN_WARNING
"too many endpoints\n");
959 endpoints
[epidx
].in_ep
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
960 endpoints
[epidx
].in_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
961 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
962 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
969 * If the endpoints aren't specified, use the first bulk endpoints in the
970 * first alternate setting of the interface.
972 static int snd_usbmidi_detect_endpoint(snd_usb_midi_t
* umidi
,
973 snd_usb_midi_endpoint_info_t
* endpoint
)
975 struct usb_interface
* intf
;
976 struct usb_host_interface
*hostif
;
977 struct usb_interface_descriptor
* intfd
;
978 struct usb_endpoint_descriptor
* epd
;
982 if (!intf
|| intf
->num_altsetting
< 1)
984 hostif
= intf
->altsetting
;
985 intfd
= get_iface_desc(hostif
);
986 if (intfd
->bNumEndpoints
< 1)
989 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
990 epd
= get_endpoint(hostif
, i
);
991 if ((epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
)
993 if (!endpoint
->out_ep
&& endpoint
->out_cables
&&
994 (epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
)
995 endpoint
->out_ep
= epd
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
996 if (!endpoint
->in_ep
&& endpoint
->in_cables
&&
997 (epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
)
998 endpoint
->in_ep
= epd
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1004 * Detects the endpoints and ports of Yamaha devices.
1006 static int snd_usbmidi_detect_yamaha(snd_usb_midi_t
* umidi
,
1007 snd_usb_midi_endpoint_info_t
* endpoint
)
1009 struct usb_interface
* intf
;
1010 struct usb_host_interface
*hostif
;
1011 struct usb_interface_descriptor
* intfd
;
1014 intf
= umidi
->iface
;
1017 hostif
= intf
->altsetting
;
1018 intfd
= get_iface_desc(hostif
);
1019 if (intfd
->bNumEndpoints
< 1)
1023 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1024 * necessarily with any useful contents. So simply count 'em.
1026 for (cs_desc
= hostif
->extra
;
1027 cs_desc
< hostif
->extra
+ hostif
->extralen
&& cs_desc
[0] >= 2;
1028 cs_desc
+= cs_desc
[0]) {
1029 if (cs_desc
[1] == CS_AUDIO_INTERFACE
) {
1030 if (cs_desc
[2] == MIDI_IN_JACK
)
1031 endpoint
->in_cables
= (endpoint
->in_cables
<< 1) | 1;
1032 else if (cs_desc
[2] == MIDI_OUT_JACK
)
1033 endpoint
->out_cables
= (endpoint
->out_cables
<< 1) | 1;
1036 if (!endpoint
->in_cables
&& !endpoint
->out_cables
)
1039 return snd_usbmidi_detect_endpoint(umidi
, endpoint
);
1043 * Creates the endpoints and their ports for Midiman devices.
1045 static int snd_usbmidi_create_endpoints_midiman(snd_usb_midi_t
* umidi
,
1046 snd_usb_midi_endpoint_info_t
* endpoint
)
1048 snd_usb_midi_endpoint_info_t ep_info
;
1049 struct usb_interface
* intf
;
1050 struct usb_host_interface
*hostif
;
1051 struct usb_interface_descriptor
* intfd
;
1052 struct usb_endpoint_descriptor
* epd
;
1055 intf
= umidi
->iface
;
1058 hostif
= intf
->altsetting
;
1059 intfd
= get_iface_desc(hostif
);
1061 * The various MidiSport devices have more or less random endpoint
1062 * numbers, so we have to identify the endpoints by their index in
1063 * the descriptor array, like the driver for that other OS does.
1065 * There is one interrupt input endpoint for all input ports, one
1066 * bulk output endpoint for even-numbered ports, and one for odd-
1067 * numbered ports. Both bulk output endpoints have corresponding
1068 * input bulk endpoints (at indices 1 and 3) which aren't used.
1070 if (intfd
->bNumEndpoints
< (endpoint
->out_cables
> 0x0001 ? 5 : 3)) {
1071 snd_printdd(KERN_ERR
"not enough endpoints\n");
1075 epd
= get_endpoint(hostif
, 0);
1076 if ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_IN
||
1077 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
) {
1078 snd_printdd(KERN_ERR
"endpoint[0] isn't interrupt\n");
1081 epd
= get_endpoint(hostif
, 2);
1082 if ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_OUT
||
1083 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
) {
1084 snd_printdd(KERN_ERR
"endpoint[2] isn't bulk output\n");
1087 if (endpoint
->out_cables
> 0x0001) {
1088 epd
= get_endpoint(hostif
, 4);
1089 if ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_OUT
||
1090 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
) {
1091 snd_printdd(KERN_ERR
"endpoint[4] isn't bulk output\n");
1096 ep_info
.out_ep
= get_endpoint(hostif
, 2)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1097 ep_info
.out_cables
= endpoint
->out_cables
& 0x5555;
1098 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1102 ep_info
.in_ep
= get_endpoint(hostif
, 0)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1103 ep_info
.in_cables
= endpoint
->in_cables
;
1104 err
= snd_usbmidi_in_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1107 umidi
->endpoints
[0].in
->urb
->complete
= snd_usb_complete_callback(snd_usbmidi_in_midiman_complete
);
1109 if (endpoint
->out_cables
> 0x0001) {
1110 ep_info
.out_ep
= get_endpoint(hostif
, 4)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1111 ep_info
.out_cables
= endpoint
->out_cables
& 0xaaaa;
1112 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[1]);
1117 for (cable
= 0; cable
< 0x10; ++cable
) {
1118 if (endpoint
->out_cables
& (1 << cable
))
1119 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, cable
,
1120 &umidi
->endpoints
[cable
& 1].out
->ports
[cable
].substream
);
1121 if (endpoint
->in_cables
& (1 << cable
))
1122 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, cable
,
1123 &umidi
->endpoints
[0].in
->ports
[cable
].substream
);
1128 static int snd_usbmidi_create_rawmidi(snd_usb_midi_t
* umidi
,
1129 int out_ports
, int in_ports
)
1131 snd_rawmidi_t
* rmidi
;
1134 err
= snd_rawmidi_new(umidi
->chip
->card
, "USB MIDI",
1135 umidi
->chip
->next_midi_device
++,
1136 out_ports
, in_ports
, &rmidi
);
1139 strcpy(rmidi
->name
, umidi
->chip
->card
->shortname
);
1140 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
1141 SNDRV_RAWMIDI_INFO_INPUT
|
1142 SNDRV_RAWMIDI_INFO_DUPLEX
;
1143 rmidi
->private_data
= umidi
;
1144 rmidi
->private_free
= snd_usbmidi_rawmidi_free
;
1145 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_usbmidi_output_ops
);
1146 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_usbmidi_input_ops
);
1148 umidi
->rmidi
= rmidi
;
1153 * Temporarily stop input.
1155 void snd_usbmidi_input_stop(struct list_head
* p
)
1157 snd_usb_midi_t
* umidi
;
1160 umidi
= list_entry(p
, snd_usb_midi_t
, list
);
1161 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1162 snd_usb_midi_endpoint_t
* ep
= &umidi
->endpoints
[i
];
1164 usb_unlink_urb(ep
->in
->urb
);
1168 static void snd_usbmidi_input_start_ep(snd_usb_midi_in_endpoint_t
* ep
)
1171 struct urb
* urb
= ep
->urb
;
1172 urb
->dev
= ep
->umidi
->chip
->dev
;
1173 snd_usbmidi_submit_urb(urb
, GFP_KERNEL
);
1178 * Resume input after a call to snd_usbmidi_input_stop().
1180 void snd_usbmidi_input_start(struct list_head
* p
)
1182 snd_usb_midi_t
* umidi
;
1185 umidi
= list_entry(p
, snd_usb_midi_t
, list
);
1186 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
1187 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
1191 * Creates and registers everything needed for a MIDI streaming interface.
1193 int snd_usb_create_midi_interface(snd_usb_audio_t
* chip
,
1194 struct usb_interface
* iface
,
1195 const snd_usb_audio_quirk_t
* quirk
)
1197 snd_usb_midi_t
* umidi
;
1198 snd_usb_midi_endpoint_info_t endpoints
[MIDI_MAX_ENDPOINTS
];
1199 int out_ports
, in_ports
;
1202 umidi
= kcalloc(1, sizeof(*umidi
), GFP_KERNEL
);
1206 umidi
->iface
= iface
;
1207 umidi
->quirk
= quirk
;
1209 /* detect the endpoint(s) to use */
1210 memset(endpoints
, 0, sizeof(endpoints
));
1212 err
= snd_usbmidi_get_ms_info(umidi
, endpoints
);
1214 switch (quirk
->type
) {
1215 case QUIRK_MIDI_FIXED_ENDPOINT
:
1216 memcpy(&endpoints
[0], quirk
->data
,
1217 sizeof(snd_usb_midi_endpoint_info_t
));
1218 err
= snd_usbmidi_detect_endpoint(umidi
, &endpoints
[0]);
1220 case QUIRK_MIDI_YAMAHA
:
1221 err
= snd_usbmidi_detect_yamaha(umidi
, &endpoints
[0]);
1223 case QUIRK_MIDI_MIDIMAN
:
1224 memcpy(&endpoints
[0], quirk
->data
,
1225 sizeof(snd_usb_midi_endpoint_info_t
));
1229 snd_printd(KERN_ERR
"invalid quirk type %d\n", quirk
->type
);
1239 /* create rawmidi device */
1242 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1243 out_ports
+= snd_usbmidi_count_bits(endpoints
[i
].out_cables
);
1244 in_ports
+= snd_usbmidi_count_bits(endpoints
[i
].in_cables
);
1246 err
= snd_usbmidi_create_rawmidi(umidi
, out_ports
, in_ports
);
1252 /* create endpoint/port structures */
1253 if (quirk
&& quirk
->type
== QUIRK_MIDI_MIDIMAN
)
1254 err
= snd_usbmidi_create_endpoints_midiman(umidi
, &endpoints
[0]);
1256 err
= snd_usbmidi_create_endpoints(umidi
, endpoints
);
1258 snd_usbmidi_free(umidi
);
1262 list_add(&umidi
->list
, &umidi
->chip
->midi_list
);
1264 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
1265 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
1269 EXPORT_SYMBOL(snd_usb_create_midi_interface
);
1270 EXPORT_SYMBOL(snd_usbmidi_input_stop
);
1271 EXPORT_SYMBOL(snd_usbmidi_input_start
);
1272 EXPORT_SYMBOL(snd_usbmidi_disconnect
);