blob: 8b1e4b124a9f65281fa3bf488c5f17f7c5c34a35 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
Clemens Ladisch96f61d92009-10-22 09:06:19 +02004 * Copyright (c) 2002-2009 Clemens Ladisch
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * All rights reserved.
6 *
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
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
19 *
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
23 * version.
24 *
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
35 * SUCH DAMAGE.
36 */
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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>
Clemens Ladischc8846972005-08-02 15:26:52 +020046#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/usb.h>
Clemens Ladischa65dd992009-07-13 13:48:36 +020048#include <linux/wait.h>
Daniel Mackde48c7b2010-02-22 23:49:13 +010049#include <linux/usb/audio.h>
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <sound/core.h>
Clemens Ladisch96f61d92009-10-22 09:06:19 +020052#include <sound/control.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <sound/rawmidi.h>
Clemens Ladischa7b928a2006-05-02 16:22:12 +020054#include <sound/asequencer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include "usbaudio.h"
Daniel Macke5779992010-03-04 19:46:13 +010056#include "midi.h"
57#include "helper.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
60 * define this to log all USB packets
61 */
62/* #define DUMP_PACKETS */
63
Clemens Ladischc8846972005-08-02 15:26:52 +020064/*
65 * how long to wait after some USB errors, so that khubd can disconnect() us
66 * without too many spurious errors
67 */
68#define ERROR_DELAY_JIFFIES (HZ / 10)
69
Clemens Ladisched4affa2009-07-13 13:43:59 +020070#define OUTPUT_URBS 7
Clemens Ladisch4773d1f2009-07-13 13:40:36 +020071#define INPUT_URBS 7
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
75MODULE_DESCRIPTION("USB Audio/MIDI helper module");
76MODULE_LICENSE("Dual BSD/GPL");
77
78
79struct usb_ms_header_descriptor {
80 __u8 bLength;
81 __u8 bDescriptorType;
82 __u8 bDescriptorSubtype;
83 __u8 bcdMSC[2];
84 __le16 wTotalLength;
85} __attribute__ ((packed));
86
87struct usb_ms_endpoint_descriptor {
88 __u8 bLength;
89 __u8 bDescriptorType;
90 __u8 bDescriptorSubtype;
91 __u8 bNumEmbMIDIJack;
92 __u8 baAssocJackID[0];
93} __attribute__ ((packed));
94
Takashi Iwai86e07d32005-11-17 15:08:02 +010095struct snd_usb_midi_in_endpoint;
96struct snd_usb_midi_out_endpoint;
97struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100100 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200101 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100103 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
104 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
107struct snd_usb_midi {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100108 struct usb_device *dev;
109 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100111 const struct snd_usb_audio_quirk *quirk;
112 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct usb_protocol_ops* usb_protocol_ops;
114 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200115 struct timer_list error_timer;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100116 spinlock_t disc_lock;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200117 struct mutex mutex;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100118 u32 usb_id;
119 int next_midi_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100122 struct snd_usb_midi_out_endpoint *out;
123 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 } endpoints[MIDI_MAX_ENDPOINTS];
125 unsigned long input_triggered;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200126 unsigned int opened;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100127 unsigned char disconnected;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200128
129 struct snd_kcontrol *roland_load_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100133 struct snd_usb_midi* umidi;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200134 struct out_urb_context {
135 struct urb *urb;
136 struct snd_usb_midi_out_endpoint *ep;
137 } urbs[OUTPUT_URBS];
138 unsigned int active_urbs;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200139 unsigned int drain_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int max_transfer; /* size of urb buffer */
141 struct tasklet_struct tasklet;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200142 unsigned int next_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 spinlock_t buffer_lock;
144
145 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100146 struct snd_usb_midi_out_endpoint* ep;
147 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int active;
149 uint8_t cable; /* cable number << 4 */
150 uint8_t state;
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
158 uint8_t data[2];
159 } ports[0x10];
160 int current_port;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200161
162 wait_queue_head_t drain_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
165struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100166 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200167 struct urb* urbs[INPUT_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100169 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200170 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200172 u8 seen_f5;
173 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 int current_port;
175};
176
Takashi Iwai86e07d32005-11-17 15:08:02 +0100177static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179static const uint8_t snd_usbmidi_cin_length[] = {
180 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
181};
182
183/*
184 * Submits the URB, with error handling.
185 */
Al Viro55016f12005-10-21 03:21:58 -0400186static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
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);
191 return err;
192}
193
194/*
195 * Error handling for URB completion functions.
196 */
197static int snd_usbmidi_urb_error(int status)
198{
Clemens Ladischc8846972005-08-02 15:26:52 +0200199 switch (status) {
200 /* manually unlinked, or device gone */
201 case -ENOENT:
202 case -ECONNRESET:
203 case -ESHUTDOWN:
204 case -ENODEV:
205 return -ENODEV;
206 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700207 case -EPROTO:
208 case -ETIME:
209 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200210 return -EIO;
211 default:
212 snd_printk(KERN_ERR "urb status %d\n", status);
213 return 0; /* continue */
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
218 * Receives a chunk of MIDI data.
219 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100220static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 uint8_t* data, int length)
222{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100223 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (!port->substream) {
226 snd_printd("unexpected port %d!\n", portidx);
227 return;
228 }
229 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
230 return;
231 snd_rawmidi_receive(port->substream, data, length);
232}
233
234#ifdef DUMP_PACKETS
235static void dump_urb(const char *type, const u8 *data, int length)
236{
237 snd_printk(KERN_DEBUG "%s packet: [", type);
238 for (; length > 0; ++data, --length)
239 printk(" %02x", *data);
240 printk(" ]\n");
241}
242#else
243#define dump_urb(type, data, length) /* nothing */
244#endif
245
246/*
247 * Processes the data read from the device.
248 */
David Howells7d12e782006-10-05 14:55:46 +0100249static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100251 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
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,
256 urb->actual_length);
257 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200258 int err = snd_usbmidi_urb_error(urb->status);
259 if (err < 0) {
260 if (err != -ENODEV) {
261 ep->error_resubmit = 1;
262 mod_timer(&ep->umidi->error_timer,
263 jiffies + ERROR_DELAY_JIFFIES);
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100269 urb->dev = ep->umidi->dev;
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200270 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
David Howells7d12e782006-10-05 14:55:46 +0100273static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200275 struct out_urb_context *context = urb->context;
276 struct snd_usb_midi_out_endpoint* ep = context->ep;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200277 unsigned int urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 spin_lock(&ep->buffer_lock);
Clemens Ladischa65dd992009-07-13 13:48:36 +0200280 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);
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 spin_unlock(&ep->buffer_lock);
287 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200288 int err = snd_usbmidi_urb_error(urb->status);
289 if (err < 0) {
290 if (err != -ENODEV)
291 mod_timer(&ep->umidi->error_timer,
292 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296 snd_usbmidi_do_output(ep);
297}
298
299/*
300 * This is called when some data should be transferred to the device
301 * (from one or more substreams).
302 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100303static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200305 unsigned int urb_index;
306 struct urb* urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned long flags;
308
309 spin_lock_irqsave(&ep->buffer_lock, flags);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100310 if (ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 spin_unlock_irqrestore(&ep->buffer_lock, flags);
312 return;
313 }
314
Clemens Ladischa65dd992009-07-13 13:48:36 +0200315 urb_index = ep->next_urb;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200316 for (;;) {
Clemens Ladischa65dd992009-07-13 13:48:36 +0200317 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)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Clemens Ladischa65dd992009-07-13 13:48:36 +0200324 dump_urb("sending", urb->transfer_buffer,
325 urb->transfer_buffer_length);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100326 urb->dev = ep->umidi->dev;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200327 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
328 break;
329 ep->active_urbs |= 1 << urb_index;
330 }
331 if (++urb_index >= OUTPUT_URBS)
332 urb_index = 0;
333 if (urb_index == ep->next_urb)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Clemens Ladischa65dd992009-07-13 13:48:36 +0200336 ep->next_urb = urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 spin_unlock_irqrestore(&ep->buffer_lock, flags);
338}
339
340static void snd_usbmidi_out_tasklet(unsigned long data)
341{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100342 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 snd_usbmidi_do_output(ep);
345}
346
Clemens Ladischc8846972005-08-02 15:26:52 +0200347/* called after transfers had been interrupted due to some USB error */
348static void snd_usbmidi_error_timer(unsigned long data)
349{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100350 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200351 unsigned int i, j;
Clemens Ladischc8846972005-08-02 15:26:52 +0200352
Takashi Iwaic0792e02008-02-22 18:34:44 +0100353 spin_lock(&umidi->disc_lock);
354 if (umidi->disconnected) {
355 spin_unlock(&umidi->disc_lock);
356 return;
357 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200358 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100359 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200360 if (in && in->error_resubmit) {
361 in->error_resubmit = 0;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200362 for (j = 0; j < INPUT_URBS; ++j) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100363 in->urbs[j]->dev = umidi->dev;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200364 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
365 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200366 }
367 if (umidi->endpoints[i].out)
368 snd_usbmidi_do_output(umidi->endpoints[i].out);
369 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100370 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100374static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 const void *data, int len)
376{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200377 int err = 0;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700378 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!buf)
380 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 dump_urb("sending", buf, len);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200382 if (ep->urbs[0].urb)
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100383 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200384 buf, len, NULL, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 kfree(buf);
386 return err;
387}
388
389/*
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.
393 */
394
Takashi Iwai86e07d32005-11-17 15:08:02 +0100395static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 uint8_t* buffer, int buffer_length)
397{
398 int i;
399
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);
405 }
406}
407
Takashi Iwai86e07d32005-11-17 15:08:02 +0100408static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 uint8_t* buffer, int buffer_length)
410{
411 int i;
412
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);
418 }
419}
420
421/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200422 * 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.
424 */
425static void snd_usbmidi_maudio_broken_running_status_input(
426 struct snd_usb_midi_in_endpoint* ep,
427 uint8_t* buffer, int buffer_length)
428{
429 int i;
430
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];
436 int length;
437
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)
442 /* channel msg */
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;
449 else
450 /*
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.)
455 */
456 port->running_status_length = 0;
457 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
458 }
459}
460
461/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200462 * CME protocol: like the standard protocol, but SysEx commands are sent as a
463 * single USB packet preceded by a 0x0F byte.
464 */
465static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
466 uint8_t *buffer, int buffer_length)
467{
468 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
469 snd_usbmidi_standard_input(ep, buffer, buffer_length);
470 else
471 snd_usbmidi_input_data(ep, buffer[0] >> 4,
472 &buffer[1], buffer_length - 1);
473}
474
475/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 * Adds one USB MIDI packet to the output buffer.
477 */
478static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
479 uint8_t p1, uint8_t p2, uint8_t p3)
480{
481
482 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
483 buf[0] = p0;
484 buf[1] = p1;
485 buf[2] = p2;
486 buf[3] = p3;
487 urb->transfer_buffer_length += 4;
488}
489
490/*
491 * Adds one Midiman packet to the output buffer.
492 */
493static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
494 uint8_t p1, uint8_t p2, uint8_t p3)
495{
496
497 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
498 buf[0] = p1;
499 buf[1] = p2;
500 buf[2] = p3;
501 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
502 urb->transfer_buffer_length += 4;
503}
504
505/*
506 * Converts MIDI commands to USB MIDI packets.
507 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100508static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 uint8_t b, struct urb* urb)
510{
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;
514
515 if (b >= 0xf8) {
516 output_packet(urb, p0 | 0x0f, b, 0, 0);
517 } else if (b >= 0xf0) {
518 switch (b) {
519 case 0xf0:
520 port->data[0] = b;
521 port->state = STATE_SYSEX_1;
522 break;
523 case 0xf1:
524 case 0xf3:
525 port->data[0] = b;
526 port->state = STATE_1PARAM;
527 break;
528 case 0xf2:
529 port->data[0] = b;
530 port->state = STATE_2PARAM_1;
531 break;
532 case 0xf4:
533 case 0xf5:
534 port->state = STATE_UNKNOWN;
535 break;
536 case 0xf6:
537 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
538 port->state = STATE_UNKNOWN;
539 break;
540 case 0xf7:
541 switch (port->state) {
542 case STATE_SYSEX_0:
543 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
544 break;
545 case STATE_SYSEX_1:
546 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
547 break;
548 case STATE_SYSEX_2:
549 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
550 break;
551 }
552 port->state = STATE_UNKNOWN;
553 break;
554 }
555 } else if (b >= 0x80) {
556 port->data[0] = b;
557 if (b >= 0xc0 && b <= 0xdf)
558 port->state = STATE_1PARAM;
559 else
560 port->state = STATE_2PARAM_1;
561 } else { /* b < 0x80 */
562 switch (port->state) {
563 case STATE_1PARAM:
564 if (port->data[0] < 0xf0) {
565 p0 |= port->data[0] >> 4;
566 } else {
567 p0 |= 0x02;
568 port->state = STATE_UNKNOWN;
569 }
570 output_packet(urb, p0, port->data[0], b, 0);
571 break;
572 case STATE_2PARAM_1:
573 port->data[1] = b;
574 port->state = STATE_2PARAM_2;
575 break;
576 case STATE_2PARAM_2:
577 if (port->data[0] < 0xf0) {
578 p0 |= port->data[0] >> 4;
579 port->state = STATE_2PARAM_1;
580 } else {
581 p0 |= 0x03;
582 port->state = STATE_UNKNOWN;
583 }
584 output_packet(urb, p0, port->data[0], port->data[1], b);
585 break;
586 case STATE_SYSEX_0:
587 port->data[0] = b;
588 port->state = STATE_SYSEX_1;
589 break;
590 case STATE_SYSEX_1:
591 port->data[1] = b;
592 port->state = STATE_SYSEX_2;
593 break;
594 case STATE_SYSEX_2:
595 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
596 port->state = STATE_SYSEX_0;
597 break;
598 }
599 }
600}
601
Clemens Ladisched4affa2009-07-13 13:43:59 +0200602static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
603 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int p;
606
607 /* FIXME: lower-numbered ports can starve higher-numbered ports */
608 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100609 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!port->active)
611 continue;
612 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
613 uint8_t b;
614 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
615 port->active = 0;
616 break;
617 }
618 snd_usbmidi_transmit_byte(port, b, urb);
619 }
620 }
621}
622
623static 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,
627};
628
629static 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,
633};
634
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200635static 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,
639};
640
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200641static 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,
645};
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/*
648 * Novation USB MIDI protocol: number of data bytes is in the first byte
649 * (when receiving) (+1!) or in the second byte (when sending); data begins
650 * at the third byte.
651 */
652
Takashi Iwai86e07d32005-11-17 15:08:02 +0100653static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 uint8_t* buffer, int buffer_length)
655{
656 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
657 return;
658 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
659}
660
Clemens Ladisched4affa2009-07-13 13:43:59 +0200661static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
662 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 uint8_t* transfer_buffer;
665 int count;
666
667 if (!ep->ports[0].active)
668 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200669 transfer_buffer = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 count = snd_rawmidi_transmit(ep->ports[0].substream,
671 &transfer_buffer[2],
672 ep->max_transfer - 2);
673 if (count < 1) {
674 ep->ports[0].active = 0;
675 return;
676 }
677 transfer_buffer[0] = 0;
678 transfer_buffer[1] = count;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200679 urb->transfer_buffer_length = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682static struct usb_protocol_ops snd_usbmidi_novation_ops = {
683 .input = snd_usbmidi_novation_input,
684 .output = snd_usbmidi_novation_output,
685};
686
687/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200688 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
690
Takashi Iwai86e07d32005-11-17 15:08:02 +0100691static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200692 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
695}
696
Clemens Ladisched4affa2009-07-13 13:43:59 +0200697static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
698 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 int count;
701
702 if (!ep->ports[0].active)
703 return;
704 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200705 urb->transfer_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ep->max_transfer);
707 if (count < 1) {
708 ep->ports[0].active = 0;
709 return;
710 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200711 urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200714static struct usb_protocol_ops snd_usbmidi_raw_ops = {
715 .input = snd_usbmidi_raw_input,
716 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717};
718
Karsten Wiese030a07e2008-07-30 15:13:29 +0200719static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
720 uint8_t *buffer, int buffer_length)
721{
722 if (buffer_length != 9)
723 return;
724 buffer_length = 8;
725 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
726 buffer_length--;
727 if (buffer_length)
728 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
729}
730
Clemens Ladisched4affa2009-07-13 13:43:59 +0200731static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
732 struct urb *urb)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200733{
734 int count;
735
736 if (!ep->ports[0].active)
737 return;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100738 count = snd_usb_get_speed(ep->umidi->dev) == USB_SPEED_HIGH ? 1 : 2;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200739 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200740 urb->transfer_buffer,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200741 count);
742 if (count < 1) {
743 ep->ports[0].active = 0;
744 return;
745 }
746
Clemens Ladisched4affa2009-07-13 13:43:59 +0200747 memset(urb->transfer_buffer + count, 0xFD, 9 - count);
748 urb->transfer_buffer_length = count;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200749}
750
751static struct usb_protocol_ops snd_usbmidi_122l_ops = {
752 .input = snd_usbmidi_us122l_input,
753 .output = snd_usbmidi_us122l_output,
754};
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*
757 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
758 */
759
Takashi Iwai86e07d32005-11-17 15:08:02 +0100760static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 static const u8 init_data[] = {
763 /* initialization magic: "get version" */
764 0xf0,
765 0x00, 0x20, 0x31, /* Emagic */
766 0x64, /* Unitor8 */
767 0x0b, /* version number request */
768 0x00, /* command version */
769 0x00, /* EEPROM, box 0 */
770 0xf7
771 };
772 send_bulk_static_data(ep, init_data, sizeof(init_data));
773 /* while we're at it, pour on more magic */
774 send_bulk_static_data(ep, init_data, sizeof(init_data));
775}
776
Takashi Iwai86e07d32005-11-17 15:08:02 +0100777static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 static const u8 finish_data[] = {
780 /* switch to patch mode with last preset */
781 0xf0,
782 0x00, 0x20, 0x31, /* Emagic */
783 0x64, /* Unitor8 */
784 0x10, /* patch switch command */
785 0x00, /* command version */
786 0x7f, /* to all boxes */
787 0x40, /* last preset in EEPROM */
788 0xf7
789 };
790 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
791}
792
Takashi Iwai86e07d32005-11-17 15:08:02 +0100793static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 uint8_t* buffer, int buffer_length)
795{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200796 int i;
797
798 /* FF indicates end of valid data */
799 for (i = 0; i < buffer_length; ++i)
800 if (buffer[i] == 0xff) {
801 buffer_length = i;
802 break;
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /* handle F5 at end of last buffer */
806 if (ep->seen_f5)
807 goto switch_port;
808
809 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /* determine size of data until next F5 */
811 for (i = 0; i < buffer_length; ++i)
812 if (buffer[i] == 0xf5)
813 break;
814 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
815 buffer += i;
816 buffer_length -= i;
817
818 if (buffer_length <= 0)
819 break;
820 /* assert(buffer[0] == 0xf5); */
821 ep->seen_f5 = 1;
822 ++buffer;
823 --buffer_length;
824
825 switch_port:
826 if (buffer_length <= 0)
827 break;
828 if (buffer[0] < 0x80) {
829 ep->current_port = (buffer[0] - 1) & 15;
830 ++buffer;
831 --buffer_length;
832 }
833 ep->seen_f5 = 0;
834 }
835}
836
Clemens Ladisched4affa2009-07-13 13:43:59 +0200837static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
838 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 int port0 = ep->current_port;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200841 uint8_t* buf = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int buf_free = ep->max_transfer;
843 int length, i;
844
845 for (i = 0; i < 0x10; ++i) {
846 /* round-robin, starting at the last current port */
847 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100848 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 if (!port->active)
851 continue;
852 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
853 port->active = 0;
854 continue;
855 }
856
857 if (portnum != ep->current_port) {
858 if (buf_free < 2)
859 break;
860 ep->current_port = portnum;
861 buf[0] = 0xf5;
862 buf[1] = (portnum + 1) & 15;
863 buf += 2;
864 buf_free -= 2;
865 }
866
867 if (buf_free < 1)
868 break;
869 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
870 if (length > 0) {
871 buf += length;
872 buf_free -= length;
873 if (buf_free < 1)
874 break;
875 }
876 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200877 if (buf_free < ep->max_transfer && buf_free > 0) {
878 *buf = 0xff;
879 --buf_free;
880 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200881 urb->transfer_buffer_length = ep->max_transfer - buf_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
885 .input = snd_usbmidi_emagic_input,
886 .output = snd_usbmidi_emagic_output,
887 .init_out_endpoint = snd_usbmidi_emagic_init_out,
888 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
889};
890
891
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200892static void update_roland_altsetting(struct snd_usb_midi* umidi)
893{
894 struct usb_interface *intf;
895 struct usb_host_interface *hostif;
896 struct usb_interface_descriptor *intfd;
897 int is_light_load;
898
899 intf = umidi->iface;
900 is_light_load = intf->cur_altsetting != intf->altsetting;
901 if (umidi->roland_load_ctl->private_value == is_light_load)
902 return;
903 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
904 intfd = get_iface_desc(hostif);
905 snd_usbmidi_input_stop(&umidi->list);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100906 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200907 intfd->bAlternateSetting);
908 snd_usbmidi_input_start(&umidi->list);
909}
910
911static void substream_open(struct snd_rawmidi_substream *substream, int open)
912{
913 struct snd_usb_midi* umidi = substream->rmidi->private_data;
914 struct snd_kcontrol *ctl;
915
916 mutex_lock(&umidi->mutex);
917 if (open) {
918 if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
919 ctl = umidi->roland_load_ctl;
920 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100921 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200922 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
923 update_roland_altsetting(umidi);
924 }
925 } else {
926 if (--umidi->opened == 0 && umidi->roland_load_ctl) {
927 ctl = umidi->roland_load_ctl;
928 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100929 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200930 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
931 }
932 }
933 mutex_unlock(&umidi->mutex);
934}
935
Takashi Iwai86e07d32005-11-17 15:08:02 +0100936static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100938 struct snd_usb_midi* umidi = substream->rmidi->private_data;
939 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 int i, j;
941
942 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
943 if (umidi->endpoints[i].out)
944 for (j = 0; j < 0x10; ++j)
945 if (umidi->endpoints[i].out->ports[j].substream == substream) {
946 port = &umidi->endpoints[i].out->ports[j];
947 break;
948 }
949 if (!port) {
950 snd_BUG();
951 return -ENXIO;
952 }
953 substream->runtime->private_data = port;
954 port->state = STATE_UNKNOWN;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200955 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957}
958
Takashi Iwai86e07d32005-11-17 15:08:02 +0100959static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200961 substream_open(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return 0;
963}
964
Takashi Iwai86e07d32005-11-17 15:08:02 +0100965static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100967 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 port->active = up;
970 if (up) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100971 if (port->ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* gobble up remaining bytes to prevent wait in
973 * snd_rawmidi_drain_output */
974 while (!snd_rawmidi_transmit_empty(substream))
975 snd_rawmidi_transmit_ack(substream, 1);
976 return;
977 }
Takashi Iwai1f041282008-12-18 12:17:55 +0100978 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980}
981
Clemens Ladischa65dd992009-07-13 13:48:36 +0200982static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
983{
984 struct usbmidi_out_port* port = substream->runtime->private_data;
985 struct snd_usb_midi_out_endpoint *ep = port->ep;
986 unsigned int drain_urbs;
987 DEFINE_WAIT(wait);
988 long timeout = msecs_to_jiffies(50);
989
Takashi Iwai29aac002010-04-10 21:27:23 +0200990 if (ep->umidi->disconnected)
991 return;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200992 /*
993 * The substream buffer is empty, but some data might still be in the
994 * currently active URBs, so we have to wait for those to complete.
995 */
996 spin_lock_irq(&ep->buffer_lock);
997 drain_urbs = ep->active_urbs;
998 if (drain_urbs) {
999 ep->drain_urbs |= drain_urbs;
1000 do {
1001 prepare_to_wait(&ep->drain_wait, &wait,
1002 TASK_UNINTERRUPTIBLE);
1003 spin_unlock_irq(&ep->buffer_lock);
1004 timeout = schedule_timeout(timeout);
1005 spin_lock_irq(&ep->buffer_lock);
1006 drain_urbs &= ep->drain_urbs;
1007 } while (drain_urbs && timeout);
1008 finish_wait(&ep->drain_wait, &wait);
1009 }
1010 spin_unlock_irq(&ep->buffer_lock);
1011}
1012
Takashi Iwai86e07d32005-11-17 15:08:02 +01001013static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001015 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return 0;
1017}
1018
Takashi Iwai86e07d32005-11-17 15:08:02 +01001019static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001021 substream_open(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023}
1024
Takashi Iwai86e07d32005-11-17 15:08:02 +01001025static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001027 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (up)
1030 set_bit(substream->number, &umidi->input_triggered);
1031 else
1032 clear_bit(substream->number, &umidi->input_triggered);
1033}
1034
Takashi Iwai86e07d32005-11-17 15:08:02 +01001035static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 .open = snd_usbmidi_output_open,
1037 .close = snd_usbmidi_output_close,
1038 .trigger = snd_usbmidi_output_trigger,
Clemens Ladischa65dd992009-07-13 13:48:36 +02001039 .drain = snd_usbmidi_output_drain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040};
1041
Takashi Iwai86e07d32005-11-17 15:08:02 +01001042static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .open = snd_usbmidi_input_open,
1044 .close = snd_usbmidi_input_close,
1045 .trigger = snd_usbmidi_input_trigger
1046};
1047
Clemens Ladisched4affa2009-07-13 13:43:59 +02001048static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1049 unsigned int buffer_length)
1050{
Linus Torvalds7a9b1492010-05-20 21:26:12 -07001051 usb_free_coherent(umidi->dev, buffer_length,
1052 urb->transfer_buffer, urb->transfer_dma);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001053 usb_free_urb(urb);
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/*
1057 * Frees an input endpoint.
1058 * May be called when ep hasn't been initialized completely.
1059 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001060static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001062 unsigned int i;
1063
Clemens Ladisched4affa2009-07-13 13:43:59 +02001064 for (i = 0; i < INPUT_URBS; ++i)
1065 if (ep->urbs[i])
1066 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1067 ep->urbs[i]->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 kfree(ep);
1069}
1070
1071/*
1072 * Creates an input endpoint.
1073 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001074static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1075 struct snd_usb_midi_endpoint_info* ep_info,
1076 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001078 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 void* buffer;
1080 unsigned int pipe;
1081 int length;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001082 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001085 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (!ep)
1087 return -ENOMEM;
1088 ep->umidi = umidi;
1089
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001090 for (i = 0; i < INPUT_URBS; ++i) {
1091 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1092 if (!ep->urbs[i]) {
1093 snd_usbmidi_in_endpoint_delete(ep);
1094 return -ENOMEM;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001098 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001100 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1101 length = usb_maxpacket(umidi->dev, pipe, 0);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001102 for (i = 0; i < INPUT_URBS; ++i) {
Linus Torvalds7a9b1492010-05-20 21:26:12 -07001103 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1104 &ep->urbs[i]->transfer_dma);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001105 if (!buffer) {
1106 snd_usbmidi_in_endpoint_delete(ep);
1107 return -ENOMEM;
1108 }
1109 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001110 usb_fill_int_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001111 pipe, buffer, length,
1112 snd_usbmidi_in_urb_complete,
1113 ep, ep_info->in_interval);
1114 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001115 usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001116 pipe, buffer, length,
1117 snd_usbmidi_in_urb_complete, ep);
1118 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 rep->in = ep;
1122 return 0;
1123}
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/*
1126 * Frees an output endpoint.
1127 * May be called when ep hasn't been initialized completely.
1128 */
Takashi Iwai29aac002010-04-10 21:27:23 +02001129static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Clemens Ladisched4affa2009-07-13 13:43:59 +02001131 unsigned int i;
1132
1133 for (i = 0; i < OUTPUT_URBS; ++i)
Takashi Iwai29aac002010-04-10 21:27:23 +02001134 if (ep->urbs[i].urb) {
Clemens Ladisched4affa2009-07-13 13:43:59 +02001135 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1136 ep->max_transfer);
Takashi Iwai29aac002010-04-10 21:27:23 +02001137 ep->urbs[i].urb = NULL;
1138 }
1139}
1140
1141static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1142{
1143 snd_usbmidi_out_endpoint_clear(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 kfree(ep);
1145}
1146
1147/*
1148 * Creates an output endpoint, and initializes output ports.
1149 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001150static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1151 struct snd_usb_midi_endpoint_info* ep_info,
1152 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001154 struct snd_usb_midi_out_endpoint* ep;
Clemens Ladisched4affa2009-07-13 13:43:59 +02001155 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 unsigned int pipe;
1157 void* buffer;
1158
1159 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001160 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (!ep)
1162 return -ENOMEM;
1163 ep->umidi = umidi;
1164
Clemens Ladisched4affa2009-07-13 13:43:59 +02001165 for (i = 0; i < OUTPUT_URBS; ++i) {
1166 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1167 if (!ep->urbs[i].urb) {
1168 snd_usbmidi_out_endpoint_delete(ep);
1169 return -ENOMEM;
1170 }
1171 ep->urbs[i].ep = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001173 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001174 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001175 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001176 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001177 switch (umidi->usb_id) {
1178 default:
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001179 ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001180 break;
1181 /*
1182 * Various chips declare a packet size larger than 4 bytes, but
1183 * do not actually work with larger packets:
1184 */
1185 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1186 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1187 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1188 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1189 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1190 ep->max_transfer = 4;
1191 break;
1192 }
Clemens Ladisched4affa2009-07-13 13:43:59 +02001193 for (i = 0; i < OUTPUT_URBS; ++i) {
Linus Torvalds7a9b1492010-05-20 21:26:12 -07001194 buffer = usb_alloc_coherent(umidi->dev,
1195 ep->max_transfer, GFP_KERNEL,
1196 &ep->urbs[i].urb->transfer_dma);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001197 if (!buffer) {
1198 snd_usbmidi_out_endpoint_delete(ep);
1199 return -ENOMEM;
1200 }
1201 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001202 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001203 pipe, buffer, ep->max_transfer,
1204 snd_usbmidi_out_urb_complete,
1205 &ep->urbs[i], ep_info->out_interval);
1206 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001207 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001208 pipe, buffer, ep->max_transfer,
1209 snd_usbmidi_out_urb_complete,
1210 &ep->urbs[i]);
1211 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 spin_lock_init(&ep->buffer_lock);
1215 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
Clemens Ladischa65dd992009-07-13 13:48:36 +02001216 init_waitqueue_head(&ep->drain_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 for (i = 0; i < 0x10; ++i)
1219 if (ep_info->out_cables & (1 << i)) {
1220 ep->ports[i].ep = ep;
1221 ep->ports[i].cable = i << 4;
1222 }
1223
1224 if (umidi->usb_protocol_ops->init_out_endpoint)
1225 umidi->usb_protocol_ops->init_out_endpoint(ep);
1226
1227 rep->out = ep;
1228 return 0;
1229}
1230
1231/*
1232 * Frees everything.
1233 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001234static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 int i;
1237
1238 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001239 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (ep->out)
1241 snd_usbmidi_out_endpoint_delete(ep->out);
1242 if (ep->in)
1243 snd_usbmidi_in_endpoint_delete(ep->in);
1244 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001245 mutex_destroy(&umidi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 kfree(umidi);
1247}
1248
1249/*
1250 * Unlinks all URBs (must be done before the usb_device is deleted).
1251 */
Clemens Ladischee733392005-04-25 10:34:13 +02001252void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001254 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001255 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Takashi Iwai86e07d32005-11-17 15:08:02 +01001257 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001258 /*
1259 * an URB's completion handler may start the timer and
1260 * a timer may submit an URB. To reliably break the cycle
1261 * a flag under lock must be used
1262 */
1263 spin_lock_irq(&umidi->disc_lock);
1264 umidi->disconnected = 1;
1265 spin_unlock_irq(&umidi->disc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001267 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001268 if (ep->out)
1269 tasklet_kill(&ep->out->tasklet);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001270 if (ep->out) {
1271 for (j = 0; j < OUTPUT_URBS; ++j)
1272 usb_kill_urb(ep->out->urbs[j].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (umidi->usb_protocol_ops->finish_out_endpoint)
1274 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
Takashi Iwai29aac002010-04-10 21:27:23 +02001275 ep->out->active_urbs = 0;
1276 if (ep->out->drain_urbs) {
1277 ep->out->drain_urbs = 0;
1278 wake_up(&ep->out->drain_wait);
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001281 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001282 for (j = 0; j < INPUT_URBS; ++j)
1283 usb_kill_urb(ep->in->urbs[j]);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001284 /* free endpoints here; later call can result in Oops */
Takashi Iwai29aac002010-04-10 21:27:23 +02001285 if (ep->out)
1286 snd_usbmidi_out_endpoint_clear(ep->out);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001287 if (ep->in) {
1288 snd_usbmidi_in_endpoint_delete(ep->in);
1289 ep->in = NULL;
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001292 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Takashi Iwai86e07d32005-11-17 15:08:02 +01001295static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001297 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 snd_usbmidi_free(umidi);
1299}
1300
Takashi Iwai86e07d32005-11-17 15:08:02 +01001301static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int stream, int number)
1303{
1304 struct list_head* list;
1305
1306 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001307 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (substream->number == number)
1309 return substream;
1310 }
1311 return NULL;
1312}
1313
1314/*
1315 * This list specifies names for ports that do not fit into the standard
1316 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1317 * such as internal control or synthesizer ports.
1318 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001319static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001320 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001321 short int port;
1322 short int voices;
1323 const char *name;
1324 unsigned int seq_flags;
1325} snd_usbmidi_port_info[] = {
1326#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1327 { .id = USB_ID(vendor, product), \
1328 .port = num, .voices = voices_, \
1329 .name = name_, .seq_flags = flags }
1330#define EXTERNAL_PORT(vendor, product, num, name) \
1331 PORT_INFO(vendor, product, num, name, 0, \
1332 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1333 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1334 SNDRV_SEQ_PORT_TYPE_PORT)
1335#define CONTROL_PORT(vendor, product, num, name) \
1336 PORT_INFO(vendor, product, num, name, 0, \
1337 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1338 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1339#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1340 PORT_INFO(vendor, product, num, name, voices, \
1341 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1342 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1343 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1344 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1345 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1346 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1347 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1348#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1349 PORT_INFO(vendor, product, num, name, voices, \
1350 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1351 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1352 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1353 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1354 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1355 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1356 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1357 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001359 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001361 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1362 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1363 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1364 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1365 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1366 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001368 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1369 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001371 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1372 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1373 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001375 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1376 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1377 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001379 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1380 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1381 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001383 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001385 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1386 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1387 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1388 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001390 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001392 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1393 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1394 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001396 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1397 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1398 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1399 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001401 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1402 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001404 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1405 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1406 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001408 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1409 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1410 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001412 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1413 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001415 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1416 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001418 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1419 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1420 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001422 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1423 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1424 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001425 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001426 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001428 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1429 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001431 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1432 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001434 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1435 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1436 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Sebastien Alaiwand39e82d2010-02-16 08:55:08 +01001437 /* Access Music Virus TI */
1438 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1439 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1440 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1441 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1442 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443};
1444
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001445static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1446{
1447 int i;
1448
1449 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001450 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001451 snd_usbmidi_port_info[i].port == number)
1452 return &snd_usbmidi_port_info[i];
1453 }
1454 return NULL;
1455}
1456
1457static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1458 struct snd_seq_port_info *seq_port_info)
1459{
1460 struct snd_usb_midi *umidi = rmidi->private_data;
1461 struct port_info *port_info;
1462
1463 /* TODO: read port flags from descriptors */
1464 port_info = find_port_info(umidi, number);
1465 if (port_info) {
1466 seq_port_info->type = port_info->seq_flags;
1467 seq_port_info->midi_voices = port_info->voices;
1468 }
1469}
1470
Takashi Iwai86e07d32005-11-17 15:08:02 +01001471static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001473 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001475 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 const char *name_format;
1477
Takashi Iwai86e07d32005-11-17 15:08:02 +01001478 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (!substream) {
1480 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1481 return;
1482 }
1483
1484 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001485 port_info = find_port_info(umidi, number);
1486 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 snprintf(substream->name, sizeof(substream->name),
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001488 name_format, umidi->card->shortname, number + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 *rsubstream = substream;
1491}
1492
1493/*
1494 * Creates the endpoints and their ports.
1495 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001496static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1497 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 int i, j, err;
1500 int out_ports = 0, in_ports = 0;
1501
1502 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1503 if (endpoints[i].out_cables) {
1504 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1505 &umidi->endpoints[i]);
1506 if (err < 0)
1507 return err;
1508 }
1509 if (endpoints[i].in_cables) {
1510 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1511 &umidi->endpoints[i]);
1512 if (err < 0)
1513 return err;
1514 }
1515
1516 for (j = 0; j < 0x10; ++j) {
1517 if (endpoints[i].out_cables & (1 << j)) {
1518 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1519 &umidi->endpoints[i].out->ports[j].substream);
1520 ++out_ports;
1521 }
1522 if (endpoints[i].in_cables & (1 << j)) {
1523 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1524 &umidi->endpoints[i].in->ports[j].substream);
1525 ++in_ports;
1526 }
1527 }
1528 }
1529 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1530 out_ports, in_ports);
1531 return 0;
1532}
1533
1534/*
1535 * Returns MIDIStreaming device capabilities.
1536 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001537static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1538 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 struct usb_interface* intf;
1541 struct usb_host_interface *hostif;
1542 struct usb_interface_descriptor* intfd;
1543 struct usb_ms_header_descriptor* ms_header;
1544 struct usb_host_endpoint *hostep;
1545 struct usb_endpoint_descriptor* ep;
1546 struct usb_ms_endpoint_descriptor* ms_ep;
1547 int i, epidx;
1548
1549 intf = umidi->iface;
1550 if (!intf)
1551 return -ENXIO;
1552 hostif = &intf->altsetting[0];
1553 intfd = get_iface_desc(hostif);
1554 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1555 if (hostif->extralen >= 7 &&
1556 ms_header->bLength >= 7 &&
1557 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
Daniel Mackde48c7b2010-02-22 23:49:13 +01001558 ms_header->bDescriptorSubtype == UAC_HEADER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1560 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1561 else
1562 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1563
1564 epidx = 0;
1565 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1566 hostep = &hostif->endpoint[i];
1567 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001568 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 continue;
1570 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1571 if (hostep->extralen < 4 ||
1572 ms_ep->bLength < 4 ||
1573 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
Daniel Mackde48c7b2010-02-22 23:49:13 +01001574 ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001576 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (endpoints[epidx].out_ep) {
1578 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1579 snd_printk(KERN_WARNING "too many endpoints\n");
1580 break;
1581 }
1582 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001583 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1584 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001586 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001587 /*
1588 * Low speed bulk transfers don't exist, so
1589 * force interrupt transfers for devices like
1590 * ESI MIDI Mate that try to use them anyway.
1591 */
1592 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1594 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1595 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1596 } else {
1597 if (endpoints[epidx].in_ep) {
1598 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1599 snd_printk(KERN_WARNING "too many endpoints\n");
1600 break;
1601 }
1602 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001603 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1604 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001606 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001607 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1609 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1610 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1611 }
1612 }
1613 return 0;
1614}
1615
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001616static int roland_load_info(struct snd_kcontrol *kcontrol,
1617 struct snd_ctl_elem_info *info)
1618{
1619 static const char *const names[] = { "High Load", "Light Load" };
1620
1621 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1622 info->count = 1;
1623 info->value.enumerated.items = 2;
1624 if (info->value.enumerated.item > 1)
1625 info->value.enumerated.item = 1;
1626 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
1627 return 0;
1628}
1629
1630static int roland_load_get(struct snd_kcontrol *kcontrol,
1631 struct snd_ctl_elem_value *value)
1632{
1633 value->value.enumerated.item[0] = kcontrol->private_value;
1634 return 0;
1635}
1636
1637static int roland_load_put(struct snd_kcontrol *kcontrol,
1638 struct snd_ctl_elem_value *value)
1639{
1640 struct snd_usb_midi* umidi = kcontrol->private_data;
1641 int changed;
1642
1643 if (value->value.enumerated.item[0] > 1)
1644 return -EINVAL;
1645 mutex_lock(&umidi->mutex);
1646 changed = value->value.enumerated.item[0] != kcontrol->private_value;
1647 if (changed)
1648 kcontrol->private_value = value->value.enumerated.item[0];
1649 mutex_unlock(&umidi->mutex);
1650 return changed;
1651}
1652
1653static struct snd_kcontrol_new roland_load_ctl = {
1654 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1655 .name = "MIDI Input Mode",
1656 .info = roland_load_info,
1657 .get = roland_load_get,
1658 .put = roland_load_put,
1659 .private_value = 1,
1660};
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662/*
1663 * On Roland devices, use the second alternate setting to be able to use
1664 * the interrupt input endpoint.
1665 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001666static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 struct usb_interface* intf;
1669 struct usb_host_interface *hostif;
1670 struct usb_interface_descriptor* intfd;
1671
1672 intf = umidi->iface;
1673 if (!intf || intf->num_altsetting != 2)
1674 return;
1675
1676 hostif = &intf->altsetting[1];
1677 intfd = get_iface_desc(hostif);
1678 if (intfd->bNumEndpoints != 2 ||
1679 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1680 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1681 return;
1682
1683 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1684 intfd->bAlternateSetting);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001685 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 intfd->bAlternateSetting);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001687
1688 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001689 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001690 umidi->roland_load_ctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
1693/*
1694 * Try to find any usable endpoints in the interface.
1695 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001696static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1697 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 int max_endpoints)
1699{
1700 struct usb_interface* intf;
1701 struct usb_host_interface *hostif;
1702 struct usb_interface_descriptor* intfd;
1703 struct usb_endpoint_descriptor* epd;
1704 int i, out_eps = 0, in_eps = 0;
1705
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001706 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 snd_usbmidi_switch_roland_altsetting(umidi);
1708
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001709 if (endpoint[0].out_ep || endpoint[0].in_ep)
1710 return 0;
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 intf = umidi->iface;
1713 if (!intf || intf->num_altsetting < 1)
1714 return -ENOENT;
1715 hostif = intf->cur_altsetting;
1716 intfd = get_iface_desc(hostif);
1717
1718 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1719 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001720 if (!usb_endpoint_xfer_bulk(epd) &&
1721 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 continue;
1723 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001724 usb_endpoint_dir_out(epd)) {
1725 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1726 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 endpoint[out_eps].out_interval = epd->bInterval;
1728 ++out_eps;
1729 }
1730 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001731 usb_endpoint_dir_in(epd)) {
1732 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1733 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 endpoint[in_eps].in_interval = epd->bInterval;
1735 ++in_eps;
1736 }
1737 }
1738 return (out_eps || in_eps) ? 0 : -ENOENT;
1739}
1740
1741/*
1742 * Detects the endpoints for one-port-per-endpoint protocols.
1743 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001744static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1745 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 int err, i;
1748
1749 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1750 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1751 if (endpoints[i].out_ep)
1752 endpoints[i].out_cables = 0x0001;
1753 if (endpoints[i].in_ep)
1754 endpoints[i].in_cables = 0x0001;
1755 }
1756 return err;
1757}
1758
1759/*
1760 * Detects the endpoints and ports of Yamaha devices.
1761 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001762static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1763 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 struct usb_interface* intf;
1766 struct usb_host_interface *hostif;
1767 struct usb_interface_descriptor* intfd;
1768 uint8_t* cs_desc;
1769
1770 intf = umidi->iface;
1771 if (!intf)
1772 return -ENOENT;
1773 hostif = intf->altsetting;
1774 intfd = get_iface_desc(hostif);
1775 if (intfd->bNumEndpoints < 1)
1776 return -ENOENT;
1777
1778 /*
1779 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1780 * necessarily with any useful contents. So simply count 'em.
1781 */
1782 for (cs_desc = hostif->extra;
1783 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1784 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001785 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001786 if (cs_desc[2] == UAC_MIDI_IN_JACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
Daniel Mackde48c7b2010-02-22 23:49:13 +01001788 else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1790 }
1791 }
1792 if (!endpoint->in_cables && !endpoint->out_cables)
1793 return -ENOENT;
1794
1795 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1796}
1797
1798/*
1799 * Creates the endpoints and their ports for Midiman devices.
1800 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001801static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1802 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001804 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 struct usb_interface* intf;
1806 struct usb_host_interface *hostif;
1807 struct usb_interface_descriptor* intfd;
1808 struct usb_endpoint_descriptor* epd;
1809 int cable, err;
1810
1811 intf = umidi->iface;
1812 if (!intf)
1813 return -ENOENT;
1814 hostif = intf->altsetting;
1815 intfd = get_iface_desc(hostif);
1816 /*
1817 * The various MidiSport devices have more or less random endpoint
1818 * numbers, so we have to identify the endpoints by their index in
1819 * the descriptor array, like the driver for that other OS does.
1820 *
1821 * There is one interrupt input endpoint for all input ports, one
1822 * bulk output endpoint for even-numbered ports, and one for odd-
1823 * numbered ports. Both bulk output endpoints have corresponding
1824 * input bulk endpoints (at indices 1 and 3) which aren't used.
1825 */
1826 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1827 snd_printdd(KERN_ERR "not enough endpoints\n");
1828 return -ENOENT;
1829 }
1830
1831 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001832 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1834 return -ENXIO;
1835 }
1836 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001837 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1839 return -ENXIO;
1840 }
1841 if (endpoint->out_cables > 0x0001) {
1842 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001843 if (!usb_endpoint_dir_out(epd) ||
1844 !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1846 return -ENXIO;
1847 }
1848 }
1849
1850 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01001851 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 ep_info.out_cables = endpoint->out_cables & 0x5555;
1853 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1854 if (err < 0)
1855 return err;
1856
1857 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1858 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1859 ep_info.in_cables = endpoint->in_cables;
1860 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1861 if (err < 0)
1862 return err;
1863
1864 if (endpoint->out_cables > 0x0001) {
1865 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1866 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1867 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1868 if (err < 0)
1869 return err;
1870 }
1871
1872 for (cable = 0; cable < 0x10; ++cable) {
1873 if (endpoint->out_cables & (1 << cable))
1874 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1875 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1876 if (endpoint->in_cables & (1 << cable))
1877 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1878 &umidi->endpoints[0].in->ports[cable].substream);
1879 }
1880 return 0;
1881}
1882
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001883static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1884 .get_port_info = snd_usbmidi_get_port_info,
1885};
1886
Takashi Iwai86e07d32005-11-17 15:08:02 +01001887static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 int out_ports, int in_ports)
1889{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001890 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 int err;
1892
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001893 err = snd_rawmidi_new(umidi->card, "USB MIDI",
1894 umidi->next_midi_device++,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 out_ports, in_ports, &rmidi);
1896 if (err < 0)
1897 return err;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001898 strcpy(rmidi->name, umidi->card->shortname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1900 SNDRV_RAWMIDI_INFO_INPUT |
1901 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001902 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 rmidi->private_data = umidi;
1904 rmidi->private_free = snd_usbmidi_rawmidi_free;
1905 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1906 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1907
1908 umidi->rmidi = rmidi;
1909 return 0;
1910}
1911
1912/*
1913 * Temporarily stop input.
1914 */
1915void snd_usbmidi_input_stop(struct list_head* p)
1916{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001917 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001918 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Takashi Iwai86e07d32005-11-17 15:08:02 +01001920 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001922 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001924 for (j = 0; j < INPUT_URBS; ++j)
1925 usb_kill_urb(ep->in->urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927}
1928
Takashi Iwai86e07d32005-11-17 15:08:02 +01001929static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001931 unsigned int i;
1932
1933 if (!ep)
1934 return;
1935 for (i = 0; i < INPUT_URBS; ++i) {
1936 struct urb* urb = ep->urbs[i];
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001937 urb->dev = ep->umidi->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1939 }
1940}
1941
1942/*
1943 * Resume input after a call to snd_usbmidi_input_stop().
1944 */
1945void snd_usbmidi_input_start(struct list_head* p)
1946{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001947 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 int i;
1949
Takashi Iwai86e07d32005-11-17 15:08:02 +01001950 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1952 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1953}
1954
1955/*
1956 * Creates and registers everything needed for a MIDI streaming interface.
1957 */
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001958int snd_usbmidi_create(struct snd_card *card,
1959 struct usb_interface* iface,
1960 struct list_head *midi_list,
1961 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001963 struct snd_usb_midi* umidi;
1964 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 int out_ports, in_ports;
1966 int i, err;
1967
Takashi Iwai561b2202005-09-09 14:22:34 +02001968 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (!umidi)
1970 return -ENOMEM;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001971 umidi->dev = interface_to_usbdev(iface);
1972 umidi->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 umidi->iface = iface;
1974 umidi->quirk = quirk;
1975 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001976 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001977 spin_lock_init(&umidi->disc_lock);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001978 mutex_init(&umidi->mutex);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001979 umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
1980 le16_to_cpu(umidi->dev->descriptor.idProduct));
Clemens Ladischc8846972005-08-02 15:26:52 +02001981 umidi->error_timer.function = snd_usbmidi_error_timer;
1982 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 /* detect the endpoint(s) to use */
1985 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001986 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1987 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001989 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
Clemens Ladischd05cc10432007-05-07 09:28:53 +02001990 umidi->usb_protocol_ops =
1991 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001992 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02001993 case QUIRK_MIDI_US122L:
1994 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1995 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02001996 case QUIRK_MIDI_FIXED_ENDPOINT:
1997 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001998 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001999 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2000 break;
2001 case QUIRK_MIDI_YAMAHA:
2002 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2003 break;
2004 case QUIRK_MIDI_MIDIMAN:
2005 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2006 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002007 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002008 err = 0;
2009 break;
2010 case QUIRK_MIDI_NOVATION:
2011 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2012 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2013 break;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02002014 case QUIRK_MIDI_FASTLANE:
Clemens Ladischd1bda042005-09-14 08:36:03 +02002015 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02002016 /*
2017 * Interface 1 contains isochronous endpoints, but with the same
2018 * numbers as in interface 0. Since it is interface 1 that the
2019 * USB core has most recently seen, these descriptors are now
2020 * associated with the endpoint numbers. This will foul up our
2021 * attempts to submit bulk/interrupt URBs to the endpoints in
2022 * interface 0, so we have to make sure that the USB core looks
2023 * again at interface 0 by calling usb_set_interface() on it.
2024 */
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002025 usb_set_interface(umidi->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002026 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2027 break;
2028 case QUIRK_MIDI_EMAGIC:
2029 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2030 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002031 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002032 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2033 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01002034 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02002035 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002036 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2037 break;
2038 default:
2039 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2040 err = -ENXIO;
2041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 if (err < 0) {
2044 kfree(umidi);
2045 return err;
2046 }
2047
2048 /* create rawmidi device */
2049 out_ports = 0;
2050 in_ports = 0;
2051 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Akinobu Mitafbc54392009-11-20 14:56:52 +09002052 out_ports += hweight16(endpoints[i].out_cables);
2053 in_ports += hweight16(endpoints[i].in_cables);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
2055 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2056 if (err < 0) {
2057 kfree(umidi);
2058 return err;
2059 }
2060
2061 /* create endpoint/port structures */
2062 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2063 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2064 else
2065 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2066 if (err < 0) {
2067 snd_usbmidi_free(umidi);
2068 return err;
2069 }
2070
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002071 list_add_tail(&umidi->list, midi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2074 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2075 return 0;
2076}
2077
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002078EXPORT_SYMBOL(snd_usbmidi_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079EXPORT_SYMBOL(snd_usbmidi_input_stop);
2080EXPORT_SYMBOL(snd_usbmidi_input_start);
2081EXPORT_SYMBOL(snd_usbmidi_disconnect);