2 * usbmidi.c - ALSA USB MIDI driver
4 * Copyright (c) 2002-2005 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/bitops.h>
42 #include <linux/interrupt.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/init.h>
46 #include <linux/slab.h>
47 #include <linux/usb.h>
48 #include <sound/core.h>
49 #include <sound/minors.h>
50 #include <sound/rawmidi.h>
55 * define this to log all USB packets
57 /* #define DUMP_PACKETS */
60 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
61 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
62 MODULE_LICENSE("Dual BSD/GPL");
65 struct usb_ms_header_descriptor
{
68 __u8 bDescriptorSubtype
;
71 } __attribute__ ((packed
));
73 struct usb_ms_endpoint_descriptor
{
76 __u8 bDescriptorSubtype
;
78 __u8 baAssocJackID
[0];
79 } __attribute__ ((packed
));
81 typedef struct snd_usb_midi snd_usb_midi_t
;
82 typedef struct snd_usb_midi_endpoint snd_usb_midi_endpoint_t
;
83 typedef struct snd_usb_midi_out_endpoint snd_usb_midi_out_endpoint_t
;
84 typedef struct snd_usb_midi_in_endpoint snd_usb_midi_in_endpoint_t
;
85 typedef struct usbmidi_out_port usbmidi_out_port_t
;
86 typedef struct usbmidi_in_port usbmidi_in_port_t
;
88 struct usb_protocol_ops
{
89 void (*input
)(snd_usb_midi_in_endpoint_t
*, uint8_t*, int);
90 void (*output
)(snd_usb_midi_out_endpoint_t
*);
91 void (*output_packet
)(struct urb
*, uint8_t, uint8_t, uint8_t, uint8_t);
92 void (*init_out_endpoint
)(snd_usb_midi_out_endpoint_t
*);
93 void (*finish_out_endpoint
)(snd_usb_midi_out_endpoint_t
*);
97 snd_usb_audio_t
*chip
;
98 struct usb_interface
*iface
;
99 const snd_usb_audio_quirk_t
*quirk
;
100 snd_rawmidi_t
* rmidi
;
101 struct usb_protocol_ops
* usb_protocol_ops
;
102 struct list_head list
;
104 struct snd_usb_midi_endpoint
{
105 snd_usb_midi_out_endpoint_t
*out
;
106 snd_usb_midi_in_endpoint_t
*in
;
107 } endpoints
[MIDI_MAX_ENDPOINTS
];
108 unsigned long input_triggered
;
111 struct snd_usb_midi_out_endpoint
{
112 snd_usb_midi_t
* umidi
;
115 int max_transfer
; /* size of urb buffer */
116 struct tasklet_struct tasklet
;
118 spinlock_t buffer_lock
;
120 struct usbmidi_out_port
{
121 snd_usb_midi_out_endpoint_t
* ep
;
122 snd_rawmidi_substream_t
* substream
;
124 uint8_t cable
; /* cable number << 4 */
126 #define STATE_UNKNOWN 0
127 #define STATE_1PARAM 1
128 #define STATE_2PARAM_1 2
129 #define STATE_2PARAM_2 3
130 #define STATE_SYSEX_0 4
131 #define STATE_SYSEX_1 5
132 #define STATE_SYSEX_2 6
138 struct snd_usb_midi_in_endpoint
{
139 snd_usb_midi_t
* umidi
;
141 struct usbmidi_in_port
{
142 snd_rawmidi_substream_t
* substream
;
148 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t
* ep
);
150 static const uint8_t snd_usbmidi_cin_length
[] = {
151 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
155 * Submits the URB, with error handling.
157 static int snd_usbmidi_submit_urb(struct urb
* urb
, int flags
)
159 int err
= usb_submit_urb(urb
, flags
);
160 if (err
< 0 && err
!= -ENODEV
)
161 snd_printk(KERN_ERR
"usb_submit_urb: %d\n", err
);
166 * Error handling for URB completion functions.
168 static int snd_usbmidi_urb_error(int status
)
170 if (status
== -ENOENT
)
171 return status
; /* killed */
172 if (status
== -EILSEQ
||
173 status
== -ECONNRESET
||
174 status
== -ETIMEDOUT
)
175 return -ENODEV
; /* device removed/shutdown */
176 snd_printk(KERN_ERR
"urb status %d\n", status
);
177 return 0; /* continue */
181 * Receives a chunk of MIDI data.
183 static void snd_usbmidi_input_data(snd_usb_midi_in_endpoint_t
* ep
, int portidx
,
184 uint8_t* data
, int length
)
186 usbmidi_in_port_t
* port
= &ep
->ports
[portidx
];
188 if (!port
->substream
) {
189 snd_printd("unexpected port %d!\n", portidx
);
192 if (!test_bit(port
->substream
->number
, &ep
->umidi
->input_triggered
))
194 snd_rawmidi_receive(port
->substream
, data
, length
);
198 static void dump_urb(const char *type
, const u8
*data
, int length
)
200 snd_printk(KERN_DEBUG
"%s packet: [", type
);
201 for (; length
> 0; ++data
, --length
)
202 printk(" %02x", *data
);
206 #define dump_urb(type, data, length) /* nothing */
210 * Processes the data read from the device.
212 static void snd_usbmidi_in_urb_complete(struct urb
* urb
, struct pt_regs
*regs
)
214 snd_usb_midi_in_endpoint_t
* ep
= urb
->context
;
216 if (urb
->status
== 0) {
217 dump_urb("received", urb
->transfer_buffer
, urb
->actual_length
);
218 ep
->umidi
->usb_protocol_ops
->input(ep
, urb
->transfer_buffer
,
221 if (snd_usbmidi_urb_error(urb
->status
) < 0)
225 if (usb_pipe_needs_resubmit(urb
->pipe
)) {
226 urb
->dev
= ep
->umidi
->chip
->dev
;
227 snd_usbmidi_submit_urb(urb
, GFP_ATOMIC
);
231 static void snd_usbmidi_out_urb_complete(struct urb
* urb
, struct pt_regs
*regs
)
233 snd_usb_midi_out_endpoint_t
* ep
= urb
->context
;
235 spin_lock(&ep
->buffer_lock
);
237 spin_unlock(&ep
->buffer_lock
);
238 if (urb
->status
< 0) {
239 if (snd_usbmidi_urb_error(urb
->status
) < 0)
242 snd_usbmidi_do_output(ep
);
246 * This is called when some data should be transferred to the device
247 * (from one or more substreams).
249 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t
* ep
)
251 struct urb
* urb
= ep
->urb
;
254 spin_lock_irqsave(&ep
->buffer_lock
, flags
);
255 if (ep
->urb_active
|| ep
->umidi
->chip
->shutdown
) {
256 spin_unlock_irqrestore(&ep
->buffer_lock
, flags
);
260 urb
->transfer_buffer_length
= 0;
261 ep
->umidi
->usb_protocol_ops
->output(ep
);
263 if (urb
->transfer_buffer_length
> 0) {
264 dump_urb("sending", urb
->transfer_buffer
,
265 urb
->transfer_buffer_length
);
266 urb
->dev
= ep
->umidi
->chip
->dev
;
267 ep
->urb_active
= snd_usbmidi_submit_urb(urb
, GFP_ATOMIC
) >= 0;
269 spin_unlock_irqrestore(&ep
->buffer_lock
, flags
);
272 static void snd_usbmidi_out_tasklet(unsigned long data
)
274 snd_usb_midi_out_endpoint_t
* ep
= (snd_usb_midi_out_endpoint_t
*) data
;
276 snd_usbmidi_do_output(ep
);
279 /* helper function to send static data that may not DMA-able */
280 static int send_bulk_static_data(snd_usb_midi_out_endpoint_t
* ep
,
281 const void *data
, int len
)
284 void *buf
= kmalloc(len
, GFP_KERNEL
);
287 memcpy(buf
, data
, len
);
288 dump_urb("sending", buf
, len
);
289 err
= usb_bulk_msg(ep
->umidi
->chip
->dev
, ep
->urb
->pipe
, buf
, len
,
296 * Standard USB MIDI protocol: see the spec.
297 * Midiman protocol: like the standard protocol, but the control byte is the
298 * fourth byte in each packet, and uses length instead of CIN.
301 static void snd_usbmidi_standard_input(snd_usb_midi_in_endpoint_t
* ep
,
302 uint8_t* buffer
, int buffer_length
)
306 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
307 if (buffer
[i
] != 0) {
308 int cable
= buffer
[i
] >> 4;
309 int length
= snd_usbmidi_cin_length
[buffer
[i
] & 0x0f];
310 snd_usbmidi_input_data(ep
, cable
, &buffer
[i
+ 1], length
);
314 static void snd_usbmidi_midiman_input(snd_usb_midi_in_endpoint_t
* ep
,
315 uint8_t* buffer
, int buffer_length
)
319 for (i
= 0; i
+ 3 < buffer_length
; i
+= 4)
320 if (buffer
[i
+ 3] != 0) {
321 int port
= buffer
[i
+ 3] >> 4;
322 int length
= buffer
[i
+ 3] & 3;
323 snd_usbmidi_input_data(ep
, port
, &buffer
[i
], length
);
328 * Adds one USB MIDI packet to the output buffer.
330 static void snd_usbmidi_output_standard_packet(struct urb
* urb
, uint8_t p0
,
331 uint8_t p1
, uint8_t p2
, uint8_t p3
)
334 uint8_t* buf
= (uint8_t*)urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
339 urb
->transfer_buffer_length
+= 4;
343 * Adds one Midiman packet to the output buffer.
345 static void snd_usbmidi_output_midiman_packet(struct urb
* urb
, uint8_t p0
,
346 uint8_t p1
, uint8_t p2
, uint8_t p3
)
349 uint8_t* buf
= (uint8_t*)urb
->transfer_buffer
+ urb
->transfer_buffer_length
;
353 buf
[3] = (p0
& 0xf0) | snd_usbmidi_cin_length
[p0
& 0x0f];
354 urb
->transfer_buffer_length
+= 4;
358 * Converts MIDI commands to USB MIDI packets.
360 static void snd_usbmidi_transmit_byte(usbmidi_out_port_t
* port
,
361 uint8_t b
, struct urb
* urb
)
363 uint8_t p0
= port
->cable
;
364 void (*output_packet
)(struct urb
*, uint8_t, uint8_t, uint8_t, uint8_t) =
365 port
->ep
->umidi
->usb_protocol_ops
->output_packet
;
368 output_packet(urb
, p0
| 0x0f, b
, 0, 0);
369 } else if (b
>= 0xf0) {
373 port
->state
= STATE_SYSEX_1
;
378 port
->state
= STATE_1PARAM
;
382 port
->state
= STATE_2PARAM_1
;
386 port
->state
= STATE_UNKNOWN
;
389 output_packet(urb
, p0
| 0x05, 0xf6, 0, 0);
390 port
->state
= STATE_UNKNOWN
;
393 switch (port
->state
) {
395 output_packet(urb
, p0
| 0x05, 0xf7, 0, 0);
398 output_packet(urb
, p0
| 0x06, port
->data
[0], 0xf7, 0);
401 output_packet(urb
, p0
| 0x07, port
->data
[0], port
->data
[1], 0xf7);
404 port
->state
= STATE_UNKNOWN
;
407 } else if (b
>= 0x80) {
409 if (b
>= 0xc0 && b
<= 0xdf)
410 port
->state
= STATE_1PARAM
;
412 port
->state
= STATE_2PARAM_1
;
413 } else { /* b < 0x80 */
414 switch (port
->state
) {
416 if (port
->data
[0] < 0xf0) {
417 p0
|= port
->data
[0] >> 4;
420 port
->state
= STATE_UNKNOWN
;
422 output_packet(urb
, p0
, port
->data
[0], b
, 0);
426 port
->state
= STATE_2PARAM_2
;
429 if (port
->data
[0] < 0xf0) {
430 p0
|= port
->data
[0] >> 4;
431 port
->state
= STATE_2PARAM_1
;
434 port
->state
= STATE_UNKNOWN
;
436 output_packet(urb
, p0
, port
->data
[0], port
->data
[1], b
);
440 port
->state
= STATE_SYSEX_1
;
444 port
->state
= STATE_SYSEX_2
;
447 output_packet(urb
, p0
| 0x04, port
->data
[0], port
->data
[1], b
);
448 port
->state
= STATE_SYSEX_0
;
454 static void snd_usbmidi_standard_output(snd_usb_midi_out_endpoint_t
* ep
)
456 struct urb
* urb
= ep
->urb
;
459 /* FIXME: lower-numbered ports can starve higher-numbered ports */
460 for (p
= 0; p
< 0x10; ++p
) {
461 usbmidi_out_port_t
* port
= &ep
->ports
[p
];
464 while (urb
->transfer_buffer_length
+ 3 < ep
->max_transfer
) {
466 if (snd_rawmidi_transmit(port
->substream
, &b
, 1) != 1) {
470 snd_usbmidi_transmit_byte(port
, b
, urb
);
475 static struct usb_protocol_ops snd_usbmidi_standard_ops
= {
476 .input
= snd_usbmidi_standard_input
,
477 .output
= snd_usbmidi_standard_output
,
478 .output_packet
= snd_usbmidi_output_standard_packet
,
481 static struct usb_protocol_ops snd_usbmidi_midiman_ops
= {
482 .input
= snd_usbmidi_midiman_input
,
483 .output
= snd_usbmidi_standard_output
,
484 .output_packet
= snd_usbmidi_output_midiman_packet
,
488 * Novation USB MIDI protocol: number of data bytes is in the first byte
489 * (when receiving) (+1!) or in the second byte (when sending); data begins
493 static void snd_usbmidi_novation_input(snd_usb_midi_in_endpoint_t
* ep
,
494 uint8_t* buffer
, int buffer_length
)
496 if (buffer_length
< 2 || !buffer
[0] || buffer_length
< buffer
[0] + 1)
498 snd_usbmidi_input_data(ep
, 0, &buffer
[2], buffer
[0] - 1);
501 static void snd_usbmidi_novation_output(snd_usb_midi_out_endpoint_t
* ep
)
503 uint8_t* transfer_buffer
;
506 if (!ep
->ports
[0].active
)
508 transfer_buffer
= ep
->urb
->transfer_buffer
;
509 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
511 ep
->max_transfer
- 2);
513 ep
->ports
[0].active
= 0;
516 transfer_buffer
[0] = 0;
517 transfer_buffer
[1] = count
;
518 ep
->urb
->transfer_buffer_length
= 2 + count
;
521 static struct usb_protocol_ops snd_usbmidi_novation_ops
= {
522 .input
= snd_usbmidi_novation_input
,
523 .output
= snd_usbmidi_novation_output
,
527 * "raw" protocol: used by the MOTU FastLane.
530 static void snd_usbmidi_raw_input(snd_usb_midi_in_endpoint_t
* ep
,
531 uint8_t* buffer
, int buffer_length
)
533 snd_usbmidi_input_data(ep
, 0, buffer
, buffer_length
);
536 static void snd_usbmidi_raw_output(snd_usb_midi_out_endpoint_t
* ep
)
540 if (!ep
->ports
[0].active
)
542 count
= snd_rawmidi_transmit(ep
->ports
[0].substream
,
543 ep
->urb
->transfer_buffer
,
546 ep
->ports
[0].active
= 0;
549 ep
->urb
->transfer_buffer_length
= count
;
552 static struct usb_protocol_ops snd_usbmidi_raw_ops
= {
553 .input
= snd_usbmidi_raw_input
,
554 .output
= snd_usbmidi_raw_output
,
558 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
561 static void snd_usbmidi_emagic_init_out(snd_usb_midi_out_endpoint_t
* ep
)
563 static const u8 init_data
[] = {
564 /* initialization magic: "get version" */
566 0x00, 0x20, 0x31, /* Emagic */
568 0x0b, /* version number request */
569 0x00, /* command version */
570 0x00, /* EEPROM, box 0 */
573 send_bulk_static_data(ep
, init_data
, sizeof(init_data
));
574 /* while we're at it, pour on more magic */
575 send_bulk_static_data(ep
, init_data
, sizeof(init_data
));
578 static void snd_usbmidi_emagic_finish_out(snd_usb_midi_out_endpoint_t
* ep
)
580 static const u8 finish_data
[] = {
581 /* switch to patch mode with last preset */
583 0x00, 0x20, 0x31, /* Emagic */
585 0x10, /* patch switch command */
586 0x00, /* command version */
587 0x7f, /* to all boxes */
588 0x40, /* last preset in EEPROM */
591 send_bulk_static_data(ep
, finish_data
, sizeof(finish_data
));
594 static void snd_usbmidi_emagic_input(snd_usb_midi_in_endpoint_t
* ep
,
595 uint8_t* buffer
, int buffer_length
)
597 /* ignore padding bytes at end of buffer */
598 while (buffer_length
> 0 && buffer
[buffer_length
- 1] == 0xff)
601 /* handle F5 at end of last buffer */
605 while (buffer_length
> 0) {
608 /* determine size of data until next F5 */
609 for (i
= 0; i
< buffer_length
; ++i
)
610 if (buffer
[i
] == 0xf5)
612 snd_usbmidi_input_data(ep
, ep
->current_port
, buffer
, i
);
616 if (buffer_length
<= 0)
618 /* assert(buffer[0] == 0xf5); */
624 if (buffer_length
<= 0)
626 if (buffer
[0] < 0x80) {
627 ep
->current_port
= (buffer
[0] - 1) & 15;
635 static void snd_usbmidi_emagic_output(snd_usb_midi_out_endpoint_t
* ep
)
637 int port0
= ep
->current_port
;
638 uint8_t* buf
= ep
->urb
->transfer_buffer
;
639 int buf_free
= ep
->max_transfer
;
642 for (i
= 0; i
< 0x10; ++i
) {
643 /* round-robin, starting at the last current port */
644 int portnum
= (port0
+ i
) & 15;
645 usbmidi_out_port_t
* port
= &ep
->ports
[portnum
];
649 if (snd_rawmidi_transmit_peek(port
->substream
, buf
, 1) != 1) {
654 if (portnum
!= ep
->current_port
) {
657 ep
->current_port
= portnum
;
659 buf
[1] = (portnum
+ 1) & 15;
666 length
= snd_rawmidi_transmit(port
->substream
, buf
, buf_free
);
674 ep
->urb
->transfer_buffer_length
= ep
->max_transfer
- buf_free
;
677 static struct usb_protocol_ops snd_usbmidi_emagic_ops
= {
678 .input
= snd_usbmidi_emagic_input
,
679 .output
= snd_usbmidi_emagic_output
,
680 .init_out_endpoint
= snd_usbmidi_emagic_init_out
,
681 .finish_out_endpoint
= snd_usbmidi_emagic_finish_out
,
685 static int snd_usbmidi_output_open(snd_rawmidi_substream_t
* substream
)
687 snd_usb_midi_t
* umidi
= substream
->rmidi
->private_data
;
688 usbmidi_out_port_t
* port
= NULL
;
691 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
692 if (umidi
->endpoints
[i
].out
)
693 for (j
= 0; j
< 0x10; ++j
)
694 if (umidi
->endpoints
[i
].out
->ports
[j
].substream
== substream
) {
695 port
= &umidi
->endpoints
[i
].out
->ports
[j
];
702 substream
->runtime
->private_data
= port
;
703 port
->state
= STATE_UNKNOWN
;
707 static int snd_usbmidi_output_close(snd_rawmidi_substream_t
* substream
)
712 static void snd_usbmidi_output_trigger(snd_rawmidi_substream_t
* substream
, int up
)
714 usbmidi_out_port_t
* port
= (usbmidi_out_port_t
*)substream
->runtime
->private_data
;
718 if (port
->ep
->umidi
->chip
->shutdown
) {
719 /* gobble up remaining bytes to prevent wait in
720 * snd_rawmidi_drain_output */
721 while (!snd_rawmidi_transmit_empty(substream
))
722 snd_rawmidi_transmit_ack(substream
, 1);
725 tasklet_hi_schedule(&port
->ep
->tasklet
);
729 static int snd_usbmidi_input_open(snd_rawmidi_substream_t
* substream
)
734 static int snd_usbmidi_input_close(snd_rawmidi_substream_t
* substream
)
739 static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t
* substream
, int up
)
741 snd_usb_midi_t
* umidi
= substream
->rmidi
->private_data
;
744 set_bit(substream
->number
, &umidi
->input_triggered
);
746 clear_bit(substream
->number
, &umidi
->input_triggered
);
749 static snd_rawmidi_ops_t snd_usbmidi_output_ops
= {
750 .open
= snd_usbmidi_output_open
,
751 .close
= snd_usbmidi_output_close
,
752 .trigger
= snd_usbmidi_output_trigger
,
755 static snd_rawmidi_ops_t snd_usbmidi_input_ops
= {
756 .open
= snd_usbmidi_input_open
,
757 .close
= snd_usbmidi_input_close
,
758 .trigger
= snd_usbmidi_input_trigger
762 * Frees an input endpoint.
763 * May be called when ep hasn't been initialized completely.
765 static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t
* ep
)
768 kfree(ep
->urb
->transfer_buffer
);
769 usb_free_urb(ep
->urb
);
775 * Creates an input endpoint.
777 static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t
* umidi
,
778 snd_usb_midi_endpoint_info_t
* ep_info
,
779 snd_usb_midi_endpoint_t
* rep
)
781 snd_usb_midi_in_endpoint_t
* ep
;
787 ep
= kcalloc(1, sizeof(*ep
), GFP_KERNEL
);
792 ep
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
794 snd_usbmidi_in_endpoint_delete(ep
);
797 if (ep_info
->in_interval
)
798 pipe
= usb_rcvintpipe(umidi
->chip
->dev
, ep_info
->in_ep
);
800 pipe
= usb_rcvbulkpipe(umidi
->chip
->dev
, ep_info
->in_ep
);
801 length
= usb_maxpacket(umidi
->chip
->dev
, pipe
, 0);
802 buffer
= kmalloc(length
, GFP_KERNEL
);
804 snd_usbmidi_in_endpoint_delete(ep
);
807 if (ep_info
->in_interval
)
808 usb_fill_int_urb(ep
->urb
, umidi
->chip
->dev
, pipe
, buffer
, length
,
809 snd_usb_complete_callback(snd_usbmidi_in_urb_complete
),
810 ep
, ep_info
->in_interval
);
812 usb_fill_bulk_urb(ep
->urb
, umidi
->chip
->dev
, pipe
, buffer
, length
,
813 snd_usb_complete_callback(snd_usbmidi_in_urb_complete
),
820 static unsigned int snd_usbmidi_count_bits(unsigned int x
)
822 unsigned int bits
= 0;
830 * Frees an output endpoint.
831 * May be called when ep hasn't been initialized completely.
833 static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t
* ep
)
835 if (ep
->tasklet
.func
)
836 tasklet_kill(&ep
->tasklet
);
838 kfree(ep
->urb
->transfer_buffer
);
839 usb_free_urb(ep
->urb
);
845 * Creates an output endpoint, and initializes output ports.
847 static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t
* umidi
,
848 snd_usb_midi_endpoint_info_t
* ep_info
,
849 snd_usb_midi_endpoint_t
* rep
)
851 snd_usb_midi_out_endpoint_t
* ep
;
857 ep
= kcalloc(1, sizeof(*ep
), GFP_KERNEL
);
862 ep
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
864 snd_usbmidi_out_endpoint_delete(ep
);
867 /* we never use interrupt output pipes */
868 pipe
= usb_sndbulkpipe(umidi
->chip
->dev
, ep_info
->out_ep
);
869 ep
->max_transfer
= usb_maxpacket(umidi
->chip
->dev
, pipe
, 1);
870 buffer
= kmalloc(ep
->max_transfer
, GFP_KERNEL
);
872 snd_usbmidi_out_endpoint_delete(ep
);
875 usb_fill_bulk_urb(ep
->urb
, umidi
->chip
->dev
, pipe
, buffer
,
877 snd_usb_complete_callback(snd_usbmidi_out_urb_complete
), ep
);
879 spin_lock_init(&ep
->buffer_lock
);
880 tasklet_init(&ep
->tasklet
, snd_usbmidi_out_tasklet
, (unsigned long)ep
);
882 for (i
= 0; i
< 0x10; ++i
)
883 if (ep_info
->out_cables
& (1 << i
)) {
884 ep
->ports
[i
].ep
= ep
;
885 ep
->ports
[i
].cable
= i
<< 4;
888 if (umidi
->usb_protocol_ops
->init_out_endpoint
)
889 umidi
->usb_protocol_ops
->init_out_endpoint(ep
);
898 static void snd_usbmidi_free(snd_usb_midi_t
* umidi
)
902 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
903 snd_usb_midi_endpoint_t
* ep
= &umidi
->endpoints
[i
];
905 snd_usbmidi_out_endpoint_delete(ep
->out
);
907 snd_usbmidi_in_endpoint_delete(ep
->in
);
913 * Unlinks all URBs (must be done before the usb_device is deleted).
915 void snd_usbmidi_disconnect(struct list_head
* p
)
917 snd_usb_midi_t
* umidi
;
920 umidi
= list_entry(p
, snd_usb_midi_t
, list
);
921 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
922 snd_usb_midi_endpoint_t
* ep
= &umidi
->endpoints
[i
];
923 if (ep
->out
&& ep
->out
->urb
) {
924 usb_kill_urb(ep
->out
->urb
);
925 if (umidi
->usb_protocol_ops
->finish_out_endpoint
)
926 umidi
->usb_protocol_ops
->finish_out_endpoint(ep
->out
);
928 if (ep
->in
&& ep
->in
->urb
)
929 usb_kill_urb(ep
->in
->urb
);
933 static void snd_usbmidi_rawmidi_free(snd_rawmidi_t
* rmidi
)
935 snd_usb_midi_t
* umidi
= rmidi
->private_data
;
936 snd_usbmidi_free(umidi
);
939 static snd_rawmidi_substream_t
* snd_usbmidi_find_substream(snd_usb_midi_t
* umidi
,
940 int stream
, int number
)
942 struct list_head
* list
;
944 list_for_each(list
, &umidi
->rmidi
->streams
[stream
].substreams
) {
945 snd_rawmidi_substream_t
* substream
= list_entry(list
, snd_rawmidi_substream_t
, list
);
946 if (substream
->number
== number
)
953 * This list specifies names for ports that do not fit into the standard
954 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
955 * such as internal control or synthesizer ports.
960 const char *name_format
;
961 } snd_usbmidi_port_names
[] = {
963 { USB_ID(0x0582, 0x0000), 2, "%s Control" },
965 { USB_ID(0x0582, 0x0003), 0, "%s Part A" },
966 { USB_ID(0x0582, 0x0003), 1, "%s Part B" },
967 { USB_ID(0x0582, 0x0003), 2, "%s Part C" },
968 { USB_ID(0x0582, 0x0003), 3, "%s Part D" },
969 { USB_ID(0x0582, 0x0003), 4, "%s MIDI 1" },
970 { USB_ID(0x0582, 0x0003), 5, "%s MIDI 2" },
972 { USB_ID(0x0582, 0x0004), 0, "%s MIDI" },
973 { USB_ID(0x0582, 0x0004), 1, "%s Control" },
975 { USB_ID(0x0582, 0x0007), 0, "%s Part A" },
976 { USB_ID(0x0582, 0x0007), 1, "%s Part B" },
977 { USB_ID(0x0582, 0x0007), 2, "%s MIDI" },
979 { USB_ID(0x0582, 0x000b), 0, "%s Part A" },
980 { USB_ID(0x0582, 0x000b), 1, "%s Part B" },
981 { USB_ID(0x0582, 0x000b), 2, "%s MIDI" },
983 { USB_ID(0x0582, 0x000c), 0, "%s Part A" },
984 { USB_ID(0x0582, 0x000c), 1, "%s Part B" },
985 { USB_ID(0x0582, 0x000c), 2, "%s MIDI" },
987 { USB_ID(0x0582, 0x0014), 8, "%s Control" },
989 { USB_ID(0x0582, 0x0016), 0, "%s Part A" },
990 { USB_ID(0x0582, 0x0016), 1, "%s Part B" },
991 { USB_ID(0x0582, 0x0016), 2, "%s MIDI 1" },
992 { USB_ID(0x0582, 0x0016), 3, "%s MIDI 2" },
994 { USB_ID(0x0582, 0x0023), 5, "%s Control" },
996 { USB_ID(0x0582, 0x0027), 0, "%s Part A" },
997 { USB_ID(0x0582, 0x0027), 1, "%s Part B" },
998 { USB_ID(0x0582, 0x0027), 2, "%s MIDI" },
1000 { USB_ID(0x0582, 0x0029), 0, "%s Part A" },
1001 { USB_ID(0x0582, 0x0029), 1, "%s Part B" },
1002 { USB_ID(0x0582, 0x0029), 2, "%s MIDI 1" },
1003 { USB_ID(0x0582, 0x0029), 3, "%s MIDI 2" },
1005 { USB_ID(0x0582, 0x002b), 0, "%s MIDI" },
1006 { USB_ID(0x0582, 0x002b), 1, "%s Control" },
1008 { USB_ID(0x0582, 0x002f), 0, "%s MIDI" },
1009 { USB_ID(0x0582, 0x002f), 1, "%s External MIDI" },
1010 { USB_ID(0x0582, 0x002f), 2, "%s Sync" },
1012 { USB_ID(0x0582, 0x0033), 0, "%s MIDI" },
1013 { USB_ID(0x0582, 0x0033), 1, "%s 1" },
1014 { USB_ID(0x0582, 0x0033), 2, "%s 2" },
1016 { USB_ID(0x0582, 0x003b), 0, "%s MIDI" },
1017 { USB_ID(0x0582, 0x003b), 1, "%s Control" },
1018 /* Edirol UA-1000 */
1019 { USB_ID(0x0582, 0x0044), 0, "%s MIDI" },
1020 { USB_ID(0x0582, 0x0044), 1, "%s Control" },
1022 { USB_ID(0x0582, 0x0048), 0, "%s MIDI" },
1023 { USB_ID(0x0582, 0x0048), 1, "%s 1" },
1024 { USB_ID(0x0582, 0x0048), 2, "%s 2" },
1026 { USB_ID(0x0582, 0x004d), 0, "%s MIDI" },
1027 { USB_ID(0x0582, 0x004d), 1, "%s 1" },
1028 { USB_ID(0x0582, 0x004d), 2, "%s 2" },
1029 /* M-Audio MidiSport 8x8 */
1030 { USB_ID(0x0763, 0x1031), 8, "%s Control" },
1031 { USB_ID(0x0763, 0x1033), 8, "%s Control" },
1033 { USB_ID(0x07fd, 0x0001), 0, "%s MIDI A" },
1034 { USB_ID(0x07fd, 0x0001), 1, "%s MIDI B" },
1035 /* Emagic Unitor8/AMT8/MT4 */
1036 { USB_ID(0x086a, 0x0001), 8, "%s Broadcast" },
1037 { USB_ID(0x086a, 0x0002), 8, "%s Broadcast" },
1038 { USB_ID(0x086a, 0x0003), 4, "%s Broadcast" },
1041 static void snd_usbmidi_init_substream(snd_usb_midi_t
* umidi
,
1042 int stream
, int number
,
1043 snd_rawmidi_substream_t
** rsubstream
)
1046 const char *name_format
;
1048 snd_rawmidi_substream_t
* substream
= snd_usbmidi_find_substream(umidi
, stream
, number
);
1050 snd_printd(KERN_ERR
"substream %d:%d not found\n", stream
, number
);
1054 /* TODO: read port name from jack descriptor */
1055 name_format
= "%s MIDI %d";
1056 for (i
= 0; i
< ARRAY_SIZE(snd_usbmidi_port_names
); ++i
) {
1057 if (snd_usbmidi_port_names
[i
].id
== umidi
->chip
->usb_id
&&
1058 snd_usbmidi_port_names
[i
].port
== number
) {
1059 name_format
= snd_usbmidi_port_names
[i
].name_format
;
1063 snprintf(substream
->name
, sizeof(substream
->name
),
1064 name_format
, umidi
->chip
->card
->shortname
, number
+ 1);
1066 *rsubstream
= substream
;
1070 * Creates the endpoints and their ports.
1072 static int snd_usbmidi_create_endpoints(snd_usb_midi_t
* umidi
,
1073 snd_usb_midi_endpoint_info_t
* endpoints
)
1076 int out_ports
= 0, in_ports
= 0;
1078 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1079 if (endpoints
[i
].out_cables
) {
1080 err
= snd_usbmidi_out_endpoint_create(umidi
, &endpoints
[i
],
1081 &umidi
->endpoints
[i
]);
1085 if (endpoints
[i
].in_cables
) {
1086 err
= snd_usbmidi_in_endpoint_create(umidi
, &endpoints
[i
],
1087 &umidi
->endpoints
[i
]);
1092 for (j
= 0; j
< 0x10; ++j
) {
1093 if (endpoints
[i
].out_cables
& (1 << j
)) {
1094 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, out_ports
,
1095 &umidi
->endpoints
[i
].out
->ports
[j
].substream
);
1098 if (endpoints
[i
].in_cables
& (1 << j
)) {
1099 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, in_ports
,
1100 &umidi
->endpoints
[i
].in
->ports
[j
].substream
);
1105 snd_printdd(KERN_INFO
"created %d output and %d input ports\n",
1106 out_ports
, in_ports
);
1111 * Returns MIDIStreaming device capabilities.
1113 static int snd_usbmidi_get_ms_info(snd_usb_midi_t
* umidi
,
1114 snd_usb_midi_endpoint_info_t
* endpoints
)
1116 struct usb_interface
* intf
;
1117 struct usb_host_interface
*hostif
;
1118 struct usb_interface_descriptor
* intfd
;
1119 struct usb_ms_header_descriptor
* ms_header
;
1120 struct usb_host_endpoint
*hostep
;
1121 struct usb_endpoint_descriptor
* ep
;
1122 struct usb_ms_endpoint_descriptor
* ms_ep
;
1125 intf
= umidi
->iface
;
1128 hostif
= &intf
->altsetting
[0];
1129 intfd
= get_iface_desc(hostif
);
1130 ms_header
= (struct usb_ms_header_descriptor
*)hostif
->extra
;
1131 if (hostif
->extralen
>= 7 &&
1132 ms_header
->bLength
>= 7 &&
1133 ms_header
->bDescriptorType
== USB_DT_CS_INTERFACE
&&
1134 ms_header
->bDescriptorSubtype
== HEADER
)
1135 snd_printdd(KERN_INFO
"MIDIStreaming version %02x.%02x\n",
1136 ms_header
->bcdMSC
[1], ms_header
->bcdMSC
[0]);
1138 snd_printk(KERN_WARNING
"MIDIStreaming interface descriptor not found\n");
1141 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
1142 hostep
= &hostif
->endpoint
[i
];
1143 ep
= get_ep_desc(hostep
);
1144 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
&&
1145 (ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1147 ms_ep
= (struct usb_ms_endpoint_descriptor
*)hostep
->extra
;
1148 if (hostep
->extralen
< 4 ||
1149 ms_ep
->bLength
< 4 ||
1150 ms_ep
->bDescriptorType
!= USB_DT_CS_ENDPOINT
||
1151 ms_ep
->bDescriptorSubtype
!= MS_GENERAL
)
1153 if ((ep
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1154 if (endpoints
[epidx
].out_ep
) {
1155 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
1156 snd_printk(KERN_WARNING
"too many endpoints\n");
1160 endpoints
[epidx
].out_ep
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1161 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
1162 endpoints
[epidx
].out_interval
= ep
->bInterval
;
1163 endpoints
[epidx
].out_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
1164 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
1165 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
1167 if (endpoints
[epidx
].in_ep
) {
1168 if (++epidx
>= MIDI_MAX_ENDPOINTS
) {
1169 snd_printk(KERN_WARNING
"too many endpoints\n");
1173 endpoints
[epidx
].in_ep
= ep
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1174 if ((ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
1175 endpoints
[epidx
].in_interval
= ep
->bInterval
;
1176 endpoints
[epidx
].in_cables
= (1 << ms_ep
->bNumEmbMIDIJack
) - 1;
1177 snd_printdd(KERN_INFO
"EP %02X: %d jack(s)\n",
1178 ep
->bEndpointAddress
, ms_ep
->bNumEmbMIDIJack
);
1185 * On Roland devices, use the second alternate setting to be able to use
1186 * the interrupt input endpoint.
1188 static void snd_usbmidi_switch_roland_altsetting(snd_usb_midi_t
* umidi
)
1190 struct usb_interface
* intf
;
1191 struct usb_host_interface
*hostif
;
1192 struct usb_interface_descriptor
* intfd
;
1194 intf
= umidi
->iface
;
1195 if (!intf
|| intf
->num_altsetting
!= 2)
1198 hostif
= &intf
->altsetting
[1];
1199 intfd
= get_iface_desc(hostif
);
1200 if (intfd
->bNumEndpoints
!= 2 ||
1201 (get_endpoint(hostif
, 0)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
||
1202 (get_endpoint(hostif
, 1)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1205 snd_printdd(KERN_INFO
"switching to altsetting %d with int ep\n",
1206 intfd
->bAlternateSetting
);
1207 usb_set_interface(umidi
->chip
->dev
, intfd
->bInterfaceNumber
,
1208 intfd
->bAlternateSetting
);
1212 * Try to find any usable endpoints in the interface.
1214 static int snd_usbmidi_detect_endpoints(snd_usb_midi_t
* umidi
,
1215 snd_usb_midi_endpoint_info_t
* endpoint
,
1218 struct usb_interface
* intf
;
1219 struct usb_host_interface
*hostif
;
1220 struct usb_interface_descriptor
* intfd
;
1221 struct usb_endpoint_descriptor
* epd
;
1222 int i
, out_eps
= 0, in_eps
= 0;
1224 if (USB_ID_VENDOR(umidi
->chip
->usb_id
) == 0x0582)
1225 snd_usbmidi_switch_roland_altsetting(umidi
);
1227 if (endpoint
[0].out_ep
|| endpoint
[0].in_ep
)
1230 intf
= umidi
->iface
;
1231 if (!intf
|| intf
->num_altsetting
< 1)
1233 hostif
= intf
->cur_altsetting
;
1234 intfd
= get_iface_desc(hostif
);
1236 for (i
= 0; i
< intfd
->bNumEndpoints
; ++i
) {
1237 epd
= get_endpoint(hostif
, i
);
1238 if ((epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
&&
1239 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
)
1241 if (out_eps
< max_endpoints
&&
1242 (epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1243 endpoint
[out_eps
].out_ep
= epd
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1244 if ((epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
1245 endpoint
[out_eps
].out_interval
= epd
->bInterval
;
1248 if (in_eps
< max_endpoints
&&
1249 (epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
) {
1250 endpoint
[in_eps
].in_ep
= epd
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1251 if ((epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) == USB_ENDPOINT_XFER_INT
)
1252 endpoint
[in_eps
].in_interval
= epd
->bInterval
;
1256 return (out_eps
|| in_eps
) ? 0 : -ENOENT
;
1260 * Detects the endpoints for one-port-per-endpoint protocols.
1262 static int snd_usbmidi_detect_per_port_endpoints(snd_usb_midi_t
* umidi
,
1263 snd_usb_midi_endpoint_info_t
* endpoints
)
1267 err
= snd_usbmidi_detect_endpoints(umidi
, endpoints
, MIDI_MAX_ENDPOINTS
);
1268 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1269 if (endpoints
[i
].out_ep
)
1270 endpoints
[i
].out_cables
= 0x0001;
1271 if (endpoints
[i
].in_ep
)
1272 endpoints
[i
].in_cables
= 0x0001;
1278 * Detects the endpoints and ports of Yamaha devices.
1280 static int snd_usbmidi_detect_yamaha(snd_usb_midi_t
* umidi
,
1281 snd_usb_midi_endpoint_info_t
* endpoint
)
1283 struct usb_interface
* intf
;
1284 struct usb_host_interface
*hostif
;
1285 struct usb_interface_descriptor
* intfd
;
1288 intf
= umidi
->iface
;
1291 hostif
= intf
->altsetting
;
1292 intfd
= get_iface_desc(hostif
);
1293 if (intfd
->bNumEndpoints
< 1)
1297 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1298 * necessarily with any useful contents. So simply count 'em.
1300 for (cs_desc
= hostif
->extra
;
1301 cs_desc
< hostif
->extra
+ hostif
->extralen
&& cs_desc
[0] >= 2;
1302 cs_desc
+= cs_desc
[0]) {
1303 if (cs_desc
[1] == CS_AUDIO_INTERFACE
) {
1304 if (cs_desc
[2] == MIDI_IN_JACK
)
1305 endpoint
->in_cables
= (endpoint
->in_cables
<< 1) | 1;
1306 else if (cs_desc
[2] == MIDI_OUT_JACK
)
1307 endpoint
->out_cables
= (endpoint
->out_cables
<< 1) | 1;
1310 if (!endpoint
->in_cables
&& !endpoint
->out_cables
)
1313 return snd_usbmidi_detect_endpoints(umidi
, endpoint
, 1);
1317 * Creates the endpoints and their ports for Midiman devices.
1319 static int snd_usbmidi_create_endpoints_midiman(snd_usb_midi_t
* umidi
,
1320 snd_usb_midi_endpoint_info_t
* endpoint
)
1322 snd_usb_midi_endpoint_info_t ep_info
;
1323 struct usb_interface
* intf
;
1324 struct usb_host_interface
*hostif
;
1325 struct usb_interface_descriptor
* intfd
;
1326 struct usb_endpoint_descriptor
* epd
;
1329 intf
= umidi
->iface
;
1332 hostif
= intf
->altsetting
;
1333 intfd
= get_iface_desc(hostif
);
1335 * The various MidiSport devices have more or less random endpoint
1336 * numbers, so we have to identify the endpoints by their index in
1337 * the descriptor array, like the driver for that other OS does.
1339 * There is one interrupt input endpoint for all input ports, one
1340 * bulk output endpoint for even-numbered ports, and one for odd-
1341 * numbered ports. Both bulk output endpoints have corresponding
1342 * input bulk endpoints (at indices 1 and 3) which aren't used.
1344 if (intfd
->bNumEndpoints
< (endpoint
->out_cables
> 0x0001 ? 5 : 3)) {
1345 snd_printdd(KERN_ERR
"not enough endpoints\n");
1349 epd
= get_endpoint(hostif
, 0);
1350 if ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_IN
||
1351 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_INT
) {
1352 snd_printdd(KERN_ERR
"endpoint[0] isn't interrupt\n");
1355 epd
= get_endpoint(hostif
, 2);
1356 if ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_OUT
||
1357 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
) {
1358 snd_printdd(KERN_ERR
"endpoint[2] isn't bulk output\n");
1361 if (endpoint
->out_cables
> 0x0001) {
1362 epd
= get_endpoint(hostif
, 4);
1363 if ((epd
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) != USB_DIR_OUT
||
1364 (epd
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_BULK
) {
1365 snd_printdd(KERN_ERR
"endpoint[4] isn't bulk output\n");
1370 ep_info
.out_ep
= get_endpoint(hostif
, 2)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1371 ep_info
.out_cables
= endpoint
->out_cables
& 0x5555;
1372 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1376 ep_info
.in_ep
= get_endpoint(hostif
, 0)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1377 ep_info
.in_interval
= get_endpoint(hostif
, 0)->bInterval
;
1378 ep_info
.in_cables
= endpoint
->in_cables
;
1379 err
= snd_usbmidi_in_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[0]);
1383 if (endpoint
->out_cables
> 0x0001) {
1384 ep_info
.out_ep
= get_endpoint(hostif
, 4)->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1385 ep_info
.out_cables
= endpoint
->out_cables
& 0xaaaa;
1386 err
= snd_usbmidi_out_endpoint_create(umidi
, &ep_info
, &umidi
->endpoints
[1]);
1391 for (cable
= 0; cable
< 0x10; ++cable
) {
1392 if (endpoint
->out_cables
& (1 << cable
))
1393 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, cable
,
1394 &umidi
->endpoints
[cable
& 1].out
->ports
[cable
].substream
);
1395 if (endpoint
->in_cables
& (1 << cable
))
1396 snd_usbmidi_init_substream(umidi
, SNDRV_RAWMIDI_STREAM_INPUT
, cable
,
1397 &umidi
->endpoints
[0].in
->ports
[cable
].substream
);
1402 static int snd_usbmidi_create_rawmidi(snd_usb_midi_t
* umidi
,
1403 int out_ports
, int in_ports
)
1405 snd_rawmidi_t
* rmidi
;
1408 err
= snd_rawmidi_new(umidi
->chip
->card
, "USB MIDI",
1409 umidi
->chip
->next_midi_device
++,
1410 out_ports
, in_ports
, &rmidi
);
1413 strcpy(rmidi
->name
, umidi
->chip
->card
->shortname
);
1414 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
1415 SNDRV_RAWMIDI_INFO_INPUT
|
1416 SNDRV_RAWMIDI_INFO_DUPLEX
;
1417 rmidi
->private_data
= umidi
;
1418 rmidi
->private_free
= snd_usbmidi_rawmidi_free
;
1419 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_usbmidi_output_ops
);
1420 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_usbmidi_input_ops
);
1422 umidi
->rmidi
= rmidi
;
1427 * Temporarily stop input.
1429 void snd_usbmidi_input_stop(struct list_head
* p
)
1431 snd_usb_midi_t
* umidi
;
1434 umidi
= list_entry(p
, snd_usb_midi_t
, list
);
1435 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1436 snd_usb_midi_endpoint_t
* ep
= &umidi
->endpoints
[i
];
1438 usb_kill_urb(ep
->in
->urb
);
1442 static void snd_usbmidi_input_start_ep(snd_usb_midi_in_endpoint_t
* ep
)
1445 struct urb
* urb
= ep
->urb
;
1446 urb
->dev
= ep
->umidi
->chip
->dev
;
1447 snd_usbmidi_submit_urb(urb
, GFP_KERNEL
);
1452 * Resume input after a call to snd_usbmidi_input_stop().
1454 void snd_usbmidi_input_start(struct list_head
* p
)
1456 snd_usb_midi_t
* umidi
;
1459 umidi
= list_entry(p
, snd_usb_midi_t
, list
);
1460 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
1461 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
1465 * Creates and registers everything needed for a MIDI streaming interface.
1467 int snd_usb_create_midi_interface(snd_usb_audio_t
* chip
,
1468 struct usb_interface
* iface
,
1469 const snd_usb_audio_quirk_t
* quirk
)
1471 snd_usb_midi_t
* umidi
;
1472 snd_usb_midi_endpoint_info_t endpoints
[MIDI_MAX_ENDPOINTS
];
1473 int out_ports
, in_ports
;
1476 umidi
= kcalloc(1, sizeof(*umidi
), GFP_KERNEL
);
1480 umidi
->iface
= iface
;
1481 umidi
->quirk
= quirk
;
1482 umidi
->usb_protocol_ops
= &snd_usbmidi_standard_ops
;
1484 /* detect the endpoint(s) to use */
1485 memset(endpoints
, 0, sizeof(endpoints
));
1487 err
= snd_usbmidi_get_ms_info(umidi
, endpoints
);
1489 switch (quirk
->type
) {
1490 case QUIRK_MIDI_FIXED_ENDPOINT
:
1491 memcpy(&endpoints
[0], quirk
->data
,
1492 sizeof(snd_usb_midi_endpoint_info_t
));
1493 err
= snd_usbmidi_detect_endpoints(umidi
, &endpoints
[0], 1);
1495 case QUIRK_MIDI_YAMAHA
:
1496 err
= snd_usbmidi_detect_yamaha(umidi
, &endpoints
[0]);
1498 case QUIRK_MIDI_MIDIMAN
:
1499 umidi
->usb_protocol_ops
= &snd_usbmidi_midiman_ops
;
1500 memcpy(&endpoints
[0], quirk
->data
,
1501 sizeof(snd_usb_midi_endpoint_info_t
));
1504 case QUIRK_MIDI_NOVATION
:
1505 umidi
->usb_protocol_ops
= &snd_usbmidi_novation_ops
;
1506 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
1508 case QUIRK_MIDI_RAW
:
1509 umidi
->usb_protocol_ops
= &snd_usbmidi_raw_ops
;
1510 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
1512 case QUIRK_MIDI_EMAGIC
:
1513 umidi
->usb_protocol_ops
= &snd_usbmidi_emagic_ops
;
1514 memcpy(&endpoints
[0], quirk
->data
,
1515 sizeof(snd_usb_midi_endpoint_info_t
));
1516 err
= snd_usbmidi_detect_endpoints(umidi
, &endpoints
[0], 1);
1518 case QUIRK_MIDI_MIDITECH
:
1519 err
= snd_usbmidi_detect_per_port_endpoints(umidi
, endpoints
);
1522 snd_printd(KERN_ERR
"invalid quirk type %d\n", quirk
->type
);
1532 /* create rawmidi device */
1535 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
) {
1536 out_ports
+= snd_usbmidi_count_bits(endpoints
[i
].out_cables
);
1537 in_ports
+= snd_usbmidi_count_bits(endpoints
[i
].in_cables
);
1539 err
= snd_usbmidi_create_rawmidi(umidi
, out_ports
, in_ports
);
1545 /* create endpoint/port structures */
1546 if (quirk
&& quirk
->type
== QUIRK_MIDI_MIDIMAN
)
1547 err
= snd_usbmidi_create_endpoints_midiman(umidi
, &endpoints
[0]);
1549 err
= snd_usbmidi_create_endpoints(umidi
, endpoints
);
1551 snd_usbmidi_free(umidi
);
1555 list_add(&umidi
->list
, &umidi
->chip
->midi_list
);
1557 for (i
= 0; i
< MIDI_MAX_ENDPOINTS
; ++i
)
1558 snd_usbmidi_input_start_ep(umidi
->endpoints
[i
].in
);
1562 EXPORT_SYMBOL(snd_usb_create_midi_interface
);
1563 EXPORT_SYMBOL(snd_usbmidi_input_stop
);
1564 EXPORT_SYMBOL(snd_usbmidi_input_start
);
1565 EXPORT_SYMBOL(snd_usbmidi_disconnect
);