blob: 80b2845bc4860df6c8efb4d885c70684ca82e8bb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <sound/core.h>
Clemens Ladisch96f61d92009-10-22 09:06:19 +020050#include <sound/control.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <sound/rawmidi.h>
Clemens Ladischa7b928a2006-05-02 16:22:12 +020052#include <sound/asequencer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "usbaudio.h"
54
55
56/*
57 * define this to log all USB packets
58 */
59/* #define DUMP_PACKETS */
60
Clemens Ladischc8846972005-08-02 15:26:52 +020061/*
62 * how long to wait after some USB errors, so that khubd can disconnect() us
63 * without too many spurious errors
64 */
65#define ERROR_DELAY_JIFFIES (HZ / 10)
66
Clemens Ladisched4affa2009-07-13 13:43:59 +020067#define OUTPUT_URBS 7
Clemens Ladisch4773d1f2009-07-13 13:40:36 +020068#define INPUT_URBS 7
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
72MODULE_DESCRIPTION("USB Audio/MIDI helper module");
73MODULE_LICENSE("Dual BSD/GPL");
74
75
76struct usb_ms_header_descriptor {
77 __u8 bLength;
78 __u8 bDescriptorType;
79 __u8 bDescriptorSubtype;
80 __u8 bcdMSC[2];
81 __le16 wTotalLength;
82} __attribute__ ((packed));
83
84struct usb_ms_endpoint_descriptor {
85 __u8 bLength;
86 __u8 bDescriptorType;
87 __u8 bDescriptorSubtype;
88 __u8 bNumEmbMIDIJack;
89 __u8 baAssocJackID[0];
90} __attribute__ ((packed));
91
Takashi Iwai86e07d32005-11-17 15:08:02 +010092struct snd_usb_midi_in_endpoint;
93struct snd_usb_midi_out_endpoint;
94struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +010097 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
Clemens Ladisched4affa2009-07-13 13:43:59 +020098 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100100 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
101 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104struct snd_usb_midi {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100105 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100107 const struct snd_usb_audio_quirk *quirk;
108 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct usb_protocol_ops* usb_protocol_ops;
110 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200111 struct timer_list error_timer;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100112 spinlock_t disc_lock;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200113 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100116 struct snd_usb_midi_out_endpoint *out;
117 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 } endpoints[MIDI_MAX_ENDPOINTS];
119 unsigned long input_triggered;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200120 unsigned int opened;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100121 unsigned char disconnected;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200122
123 struct snd_kcontrol *roland_load_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100127 struct snd_usb_midi* umidi;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200128 struct out_urb_context {
129 struct urb *urb;
130 struct snd_usb_midi_out_endpoint *ep;
131 } urbs[OUTPUT_URBS];
132 unsigned int active_urbs;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200133 unsigned int drain_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int max_transfer; /* size of urb buffer */
135 struct tasklet_struct tasklet;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200136 unsigned int next_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 spinlock_t buffer_lock;
138
139 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100140 struct snd_usb_midi_out_endpoint* ep;
141 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int active;
143 uint8_t cable; /* cable number << 4 */
144 uint8_t state;
145#define STATE_UNKNOWN 0
146#define STATE_1PARAM 1
147#define STATE_2PARAM_1 2
148#define STATE_2PARAM_2 3
149#define STATE_SYSEX_0 4
150#define STATE_SYSEX_1 5
151#define STATE_SYSEX_2 6
152 uint8_t data[2];
153 } ports[0x10];
154 int current_port;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200155
156 wait_queue_head_t drain_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
159struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100160 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200161 struct urb* urbs[INPUT_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100163 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc102007-05-07 09:28:53 +0200164 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200166 u8 seen_f5;
167 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int current_port;
169};
170
Takashi Iwai86e07d32005-11-17 15:08:02 +0100171static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static const uint8_t snd_usbmidi_cin_length[] = {
174 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
175};
176
177/*
178 * Submits the URB, with error handling.
179 */
Al Viro55016f12005-10-21 03:21:58 -0400180static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 int err = usb_submit_urb(urb, flags);
183 if (err < 0 && err != -ENODEV)
184 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
185 return err;
186}
187
188/*
189 * Error handling for URB completion functions.
190 */
191static int snd_usbmidi_urb_error(int status)
192{
Clemens Ladischc8846972005-08-02 15:26:52 +0200193 switch (status) {
194 /* manually unlinked, or device gone */
195 case -ENOENT:
196 case -ECONNRESET:
197 case -ESHUTDOWN:
198 case -ENODEV:
199 return -ENODEV;
200 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700201 case -EPROTO:
202 case -ETIME:
203 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200204 return -EIO;
205 default:
206 snd_printk(KERN_ERR "urb status %d\n", status);
207 return 0; /* continue */
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211/*
212 * Receives a chunk of MIDI data.
213 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100214static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 uint8_t* data, int length)
216{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100217 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 if (!port->substream) {
220 snd_printd("unexpected port %d!\n", portidx);
221 return;
222 }
223 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
224 return;
225 snd_rawmidi_receive(port->substream, data, length);
226}
227
228#ifdef DUMP_PACKETS
229static void dump_urb(const char *type, const u8 *data, int length)
230{
231 snd_printk(KERN_DEBUG "%s packet: [", type);
232 for (; length > 0; ++data, --length)
233 printk(" %02x", *data);
234 printk(" ]\n");
235}
236#else
237#define dump_urb(type, data, length) /* nothing */
238#endif
239
240/*
241 * Processes the data read from the device.
242 */
David Howells7d12e782006-10-05 14:55:46 +0100243static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100245 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (urb->status == 0) {
248 dump_urb("received", urb->transfer_buffer, urb->actual_length);
249 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
250 urb->actual_length);
251 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200252 int err = snd_usbmidi_urb_error(urb->status);
253 if (err < 0) {
254 if (err != -ENODEV) {
255 ep->error_resubmit = 1;
256 mod_timer(&ep->umidi->error_timer,
257 jiffies + ERROR_DELAY_JIFFIES);
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200263 urb->dev = ep->umidi->chip->dev;
264 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
David Howells7d12e782006-10-05 14:55:46 +0100267static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200269 struct out_urb_context *context = urb->context;
270 struct snd_usb_midi_out_endpoint* ep = context->ep;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200271 unsigned int urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 spin_lock(&ep->buffer_lock);
Clemens Ladischa65dd992009-07-13 13:48:36 +0200274 urb_index = context - ep->urbs;
275 ep->active_urbs &= ~(1 << urb_index);
276 if (unlikely(ep->drain_urbs)) {
277 ep->drain_urbs &= ~(1 << urb_index);
278 wake_up(&ep->drain_wait);
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 spin_unlock(&ep->buffer_lock);
281 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200282 int err = snd_usbmidi_urb_error(urb->status);
283 if (err < 0) {
284 if (err != -ENODEV)
285 mod_timer(&ep->umidi->error_timer,
286 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 snd_usbmidi_do_output(ep);
291}
292
293/*
294 * This is called when some data should be transferred to the device
295 * (from one or more substreams).
296 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100297static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200299 unsigned int urb_index;
300 struct urb* urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned long flags;
302
303 spin_lock_irqsave(&ep->buffer_lock, flags);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200304 if (ep->umidi->chip->shutdown) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 spin_unlock_irqrestore(&ep->buffer_lock, flags);
306 return;
307 }
308
Clemens Ladischa65dd992009-07-13 13:48:36 +0200309 urb_index = ep->next_urb;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200310 for (;;) {
Clemens Ladischa65dd992009-07-13 13:48:36 +0200311 if (!(ep->active_urbs & (1 << urb_index))) {
312 urb = ep->urbs[urb_index].urb;
313 urb->transfer_buffer_length = 0;
314 ep->umidi->usb_protocol_ops->output(ep, urb);
315 if (urb->transfer_buffer_length == 0)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200316 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Clemens Ladischa65dd992009-07-13 13:48:36 +0200318 dump_urb("sending", urb->transfer_buffer,
319 urb->transfer_buffer_length);
320 urb->dev = ep->umidi->chip->dev;
321 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
322 break;
323 ep->active_urbs |= 1 << urb_index;
324 }
325 if (++urb_index >= OUTPUT_URBS)
326 urb_index = 0;
327 if (urb_index == ep->next_urb)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Clemens Ladischa65dd992009-07-13 13:48:36 +0200330 ep->next_urb = urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 spin_unlock_irqrestore(&ep->buffer_lock, flags);
332}
333
334static void snd_usbmidi_out_tasklet(unsigned long data)
335{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100336 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 snd_usbmidi_do_output(ep);
339}
340
Clemens Ladischc8846972005-08-02 15:26:52 +0200341/* called after transfers had been interrupted due to some USB error */
342static void snd_usbmidi_error_timer(unsigned long data)
343{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100344 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200345 unsigned int i, j;
Clemens Ladischc8846972005-08-02 15:26:52 +0200346
Takashi Iwaic0792e02008-02-22 18:34:44 +0100347 spin_lock(&umidi->disc_lock);
348 if (umidi->disconnected) {
349 spin_unlock(&umidi->disc_lock);
350 return;
351 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200352 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100353 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200354 if (in && in->error_resubmit) {
355 in->error_resubmit = 0;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200356 for (j = 0; j < INPUT_URBS; ++j) {
357 in->urbs[j]->dev = umidi->chip->dev;
358 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
359 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200360 }
361 if (umidi->endpoints[i].out)
362 snd_usbmidi_do_output(umidi->endpoints[i].out);
363 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100364 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100368static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 const void *data, int len)
370{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200371 int err = 0;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700372 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (!buf)
374 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 dump_urb("sending", buf, len);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200376 if (ep->urbs[0].urb)
377 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urbs[0].urb->pipe,
378 buf, len, NULL, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 kfree(buf);
380 return err;
381}
382
383/*
384 * Standard USB MIDI protocol: see the spec.
385 * Midiman protocol: like the standard protocol, but the control byte is the
386 * fourth byte in each packet, and uses length instead of CIN.
387 */
388
Takashi Iwai86e07d32005-11-17 15:08:02 +0100389static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 uint8_t* buffer, int buffer_length)
391{
392 int i;
393
394 for (i = 0; i + 3 < buffer_length; i += 4)
395 if (buffer[i] != 0) {
396 int cable = buffer[i] >> 4;
397 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
398 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
399 }
400}
401
Takashi Iwai86e07d32005-11-17 15:08:02 +0100402static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 uint8_t* buffer, int buffer_length)
404{
405 int i;
406
407 for (i = 0; i + 3 < buffer_length; i += 4)
408 if (buffer[i + 3] != 0) {
409 int port = buffer[i + 3] >> 4;
410 int length = buffer[i + 3] & 3;
411 snd_usbmidi_input_data(ep, port, &buffer[i], length);
412 }
413}
414
415/*
Clemens Ladischd05cc102007-05-07 09:28:53 +0200416 * Buggy M-Audio device: running status on input results in a packet that has
417 * the data bytes but not the status byte and that is marked with CIN 4.
418 */
419static void snd_usbmidi_maudio_broken_running_status_input(
420 struct snd_usb_midi_in_endpoint* ep,
421 uint8_t* buffer, int buffer_length)
422{
423 int i;
424
425 for (i = 0; i + 3 < buffer_length; i += 4)
426 if (buffer[i] != 0) {
427 int cable = buffer[i] >> 4;
428 u8 cin = buffer[i] & 0x0f;
429 struct usbmidi_in_port *port = &ep->ports[cable];
430 int length;
431
432 length = snd_usbmidi_cin_length[cin];
433 if (cin == 0xf && buffer[i + 1] >= 0xf8)
434 ; /* realtime msg: no running status change */
435 else if (cin >= 0x8 && cin <= 0xe)
436 /* channel msg */
437 port->running_status_length = length - 1;
438 else if (cin == 0x4 &&
439 port->running_status_length != 0 &&
440 buffer[i + 1] < 0x80)
441 /* CIN 4 that is not a SysEx */
442 length = port->running_status_length;
443 else
444 /*
445 * All other msgs cannot begin running status.
446 * (A channel msg sent as two or three CIN 0xF
447 * packets could in theory, but this device
448 * doesn't use this format.)
449 */
450 port->running_status_length = 0;
451 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
452 }
453}
454
455/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200456 * CME protocol: like the standard protocol, but SysEx commands are sent as a
457 * single USB packet preceded by a 0x0F byte.
458 */
459static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
460 uint8_t *buffer, int buffer_length)
461{
462 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
463 snd_usbmidi_standard_input(ep, buffer, buffer_length);
464 else
465 snd_usbmidi_input_data(ep, buffer[0] >> 4,
466 &buffer[1], buffer_length - 1);
467}
468
469/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * Adds one USB MIDI packet to the output buffer.
471 */
472static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
473 uint8_t p1, uint8_t p2, uint8_t p3)
474{
475
476 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
477 buf[0] = p0;
478 buf[1] = p1;
479 buf[2] = p2;
480 buf[3] = p3;
481 urb->transfer_buffer_length += 4;
482}
483
484/*
485 * Adds one Midiman packet to the output buffer.
486 */
487static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
488 uint8_t p1, uint8_t p2, uint8_t p3)
489{
490
491 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
492 buf[0] = p1;
493 buf[1] = p2;
494 buf[2] = p3;
495 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
496 urb->transfer_buffer_length += 4;
497}
498
499/*
500 * Converts MIDI commands to USB MIDI packets.
501 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100502static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 uint8_t b, struct urb* urb)
504{
505 uint8_t p0 = port->cable;
506 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
507 port->ep->umidi->usb_protocol_ops->output_packet;
508
509 if (b >= 0xf8) {
510 output_packet(urb, p0 | 0x0f, b, 0, 0);
511 } else if (b >= 0xf0) {
512 switch (b) {
513 case 0xf0:
514 port->data[0] = b;
515 port->state = STATE_SYSEX_1;
516 break;
517 case 0xf1:
518 case 0xf3:
519 port->data[0] = b;
520 port->state = STATE_1PARAM;
521 break;
522 case 0xf2:
523 port->data[0] = b;
524 port->state = STATE_2PARAM_1;
525 break;
526 case 0xf4:
527 case 0xf5:
528 port->state = STATE_UNKNOWN;
529 break;
530 case 0xf6:
531 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
532 port->state = STATE_UNKNOWN;
533 break;
534 case 0xf7:
535 switch (port->state) {
536 case STATE_SYSEX_0:
537 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
538 break;
539 case STATE_SYSEX_1:
540 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
541 break;
542 case STATE_SYSEX_2:
543 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
544 break;
545 }
546 port->state = STATE_UNKNOWN;
547 break;
548 }
549 } else if (b >= 0x80) {
550 port->data[0] = b;
551 if (b >= 0xc0 && b <= 0xdf)
552 port->state = STATE_1PARAM;
553 else
554 port->state = STATE_2PARAM_1;
555 } else { /* b < 0x80 */
556 switch (port->state) {
557 case STATE_1PARAM:
558 if (port->data[0] < 0xf0) {
559 p0 |= port->data[0] >> 4;
560 } else {
561 p0 |= 0x02;
562 port->state = STATE_UNKNOWN;
563 }
564 output_packet(urb, p0, port->data[0], b, 0);
565 break;
566 case STATE_2PARAM_1:
567 port->data[1] = b;
568 port->state = STATE_2PARAM_2;
569 break;
570 case STATE_2PARAM_2:
571 if (port->data[0] < 0xf0) {
572 p0 |= port->data[0] >> 4;
573 port->state = STATE_2PARAM_1;
574 } else {
575 p0 |= 0x03;
576 port->state = STATE_UNKNOWN;
577 }
578 output_packet(urb, p0, port->data[0], port->data[1], b);
579 break;
580 case STATE_SYSEX_0:
581 port->data[0] = b;
582 port->state = STATE_SYSEX_1;
583 break;
584 case STATE_SYSEX_1:
585 port->data[1] = b;
586 port->state = STATE_SYSEX_2;
587 break;
588 case STATE_SYSEX_2:
589 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
590 port->state = STATE_SYSEX_0;
591 break;
592 }
593 }
594}
595
Clemens Ladisched4affa2009-07-13 13:43:59 +0200596static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
597 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int p;
600
601 /* FIXME: lower-numbered ports can starve higher-numbered ports */
602 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100603 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!port->active)
605 continue;
606 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
607 uint8_t b;
608 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
609 port->active = 0;
610 break;
611 }
612 snd_usbmidi_transmit_byte(port, b, urb);
613 }
614 }
615}
616
617static struct usb_protocol_ops snd_usbmidi_standard_ops = {
618 .input = snd_usbmidi_standard_input,
619 .output = snd_usbmidi_standard_output,
620 .output_packet = snd_usbmidi_output_standard_packet,
621};
622
623static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
624 .input = snd_usbmidi_midiman_input,
625 .output = snd_usbmidi_standard_output,
626 .output_packet = snd_usbmidi_output_midiman_packet,
627};
628
Clemens Ladischd05cc102007-05-07 09:28:53 +0200629static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
630 .input = snd_usbmidi_maudio_broken_running_status_input,
631 .output = snd_usbmidi_standard_output,
632 .output_packet = snd_usbmidi_output_standard_packet,
633};
634
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200635static struct usb_protocol_ops snd_usbmidi_cme_ops = {
636 .input = snd_usbmidi_cme_input,
637 .output = snd_usbmidi_standard_output,
638 .output_packet = snd_usbmidi_output_standard_packet,
639};
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/*
642 * Novation USB MIDI protocol: number of data bytes is in the first byte
643 * (when receiving) (+1!) or in the second byte (when sending); data begins
644 * at the third byte.
645 */
646
Takashi Iwai86e07d32005-11-17 15:08:02 +0100647static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 uint8_t* buffer, int buffer_length)
649{
650 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
651 return;
652 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
653}
654
Clemens Ladisched4affa2009-07-13 13:43:59 +0200655static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
656 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 uint8_t* transfer_buffer;
659 int count;
660
661 if (!ep->ports[0].active)
662 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200663 transfer_buffer = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 count = snd_rawmidi_transmit(ep->ports[0].substream,
665 &transfer_buffer[2],
666 ep->max_transfer - 2);
667 if (count < 1) {
668 ep->ports[0].active = 0;
669 return;
670 }
671 transfer_buffer[0] = 0;
672 transfer_buffer[1] = count;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200673 urb->transfer_buffer_length = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
676static struct usb_protocol_ops snd_usbmidi_novation_ops = {
677 .input = snd_usbmidi_novation_input,
678 .output = snd_usbmidi_novation_output,
679};
680
681/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200682 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
684
Takashi Iwai86e07d32005-11-17 15:08:02 +0100685static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200686 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
689}
690
Clemens Ladisched4affa2009-07-13 13:43:59 +0200691static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
692 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 int count;
695
696 if (!ep->ports[0].active)
697 return;
698 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200699 urb->transfer_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ep->max_transfer);
701 if (count < 1) {
702 ep->ports[0].active = 0;
703 return;
704 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200705 urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200708static struct usb_protocol_ops snd_usbmidi_raw_ops = {
709 .input = snd_usbmidi_raw_input,
710 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711};
712
Karsten Wiese030a07e2008-07-30 15:13:29 +0200713static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
714 uint8_t *buffer, int buffer_length)
715{
716 if (buffer_length != 9)
717 return;
718 buffer_length = 8;
719 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
720 buffer_length--;
721 if (buffer_length)
722 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
723}
724
Clemens Ladisched4affa2009-07-13 13:43:59 +0200725static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
726 struct urb *urb)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200727{
728 int count;
729
730 if (!ep->ports[0].active)
731 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200732 count = snd_usb_get_speed(ep->umidi->chip->dev) == USB_SPEED_HIGH
733 ? 1 : 2;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200734 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200735 urb->transfer_buffer,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200736 count);
737 if (count < 1) {
738 ep->ports[0].active = 0;
739 return;
740 }
741
Clemens Ladisched4affa2009-07-13 13:43:59 +0200742 memset(urb->transfer_buffer + count, 0xFD, 9 - count);
743 urb->transfer_buffer_length = count;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200744}
745
746static struct usb_protocol_ops snd_usbmidi_122l_ops = {
747 .input = snd_usbmidi_us122l_input,
748 .output = snd_usbmidi_us122l_output,
749};
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/*
752 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
753 */
754
Takashi Iwai86e07d32005-11-17 15:08:02 +0100755static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 static const u8 init_data[] = {
758 /* initialization magic: "get version" */
759 0xf0,
760 0x00, 0x20, 0x31, /* Emagic */
761 0x64, /* Unitor8 */
762 0x0b, /* version number request */
763 0x00, /* command version */
764 0x00, /* EEPROM, box 0 */
765 0xf7
766 };
767 send_bulk_static_data(ep, init_data, sizeof(init_data));
768 /* while we're at it, pour on more magic */
769 send_bulk_static_data(ep, init_data, sizeof(init_data));
770}
771
Takashi Iwai86e07d32005-11-17 15:08:02 +0100772static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 static const u8 finish_data[] = {
775 /* switch to patch mode with last preset */
776 0xf0,
777 0x00, 0x20, 0x31, /* Emagic */
778 0x64, /* Unitor8 */
779 0x10, /* patch switch command */
780 0x00, /* command version */
781 0x7f, /* to all boxes */
782 0x40, /* last preset in EEPROM */
783 0xf7
784 };
785 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
786}
787
Takashi Iwai86e07d32005-11-17 15:08:02 +0100788static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 uint8_t* buffer, int buffer_length)
790{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200791 int i;
792
793 /* FF indicates end of valid data */
794 for (i = 0; i < buffer_length; ++i)
795 if (buffer[i] == 0xff) {
796 buffer_length = i;
797 break;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /* handle F5 at end of last buffer */
801 if (ep->seen_f5)
802 goto switch_port;
803
804 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* determine size of data until next F5 */
806 for (i = 0; i < buffer_length; ++i)
807 if (buffer[i] == 0xf5)
808 break;
809 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
810 buffer += i;
811 buffer_length -= i;
812
813 if (buffer_length <= 0)
814 break;
815 /* assert(buffer[0] == 0xf5); */
816 ep->seen_f5 = 1;
817 ++buffer;
818 --buffer_length;
819
820 switch_port:
821 if (buffer_length <= 0)
822 break;
823 if (buffer[0] < 0x80) {
824 ep->current_port = (buffer[0] - 1) & 15;
825 ++buffer;
826 --buffer_length;
827 }
828 ep->seen_f5 = 0;
829 }
830}
831
Clemens Ladisched4affa2009-07-13 13:43:59 +0200832static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
833 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 int port0 = ep->current_port;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200836 uint8_t* buf = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int buf_free = ep->max_transfer;
838 int length, i;
839
840 for (i = 0; i < 0x10; ++i) {
841 /* round-robin, starting at the last current port */
842 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100843 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if (!port->active)
846 continue;
847 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
848 port->active = 0;
849 continue;
850 }
851
852 if (portnum != ep->current_port) {
853 if (buf_free < 2)
854 break;
855 ep->current_port = portnum;
856 buf[0] = 0xf5;
857 buf[1] = (portnum + 1) & 15;
858 buf += 2;
859 buf_free -= 2;
860 }
861
862 if (buf_free < 1)
863 break;
864 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
865 if (length > 0) {
866 buf += length;
867 buf_free -= length;
868 if (buf_free < 1)
869 break;
870 }
871 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200872 if (buf_free < ep->max_transfer && buf_free > 0) {
873 *buf = 0xff;
874 --buf_free;
875 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200876 urb->transfer_buffer_length = ep->max_transfer - buf_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
879static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
880 .input = snd_usbmidi_emagic_input,
881 .output = snd_usbmidi_emagic_output,
882 .init_out_endpoint = snd_usbmidi_emagic_init_out,
883 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
884};
885
886
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200887static void update_roland_altsetting(struct snd_usb_midi* umidi)
888{
889 struct usb_interface *intf;
890 struct usb_host_interface *hostif;
891 struct usb_interface_descriptor *intfd;
892 int is_light_load;
893
894 intf = umidi->iface;
895 is_light_load = intf->cur_altsetting != intf->altsetting;
896 if (umidi->roland_load_ctl->private_value == is_light_load)
897 return;
898 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
899 intfd = get_iface_desc(hostif);
900 snd_usbmidi_input_stop(&umidi->list);
901 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
902 intfd->bAlternateSetting);
903 snd_usbmidi_input_start(&umidi->list);
904}
905
906static void substream_open(struct snd_rawmidi_substream *substream, int open)
907{
908 struct snd_usb_midi* umidi = substream->rmidi->private_data;
909 struct snd_kcontrol *ctl;
910
911 mutex_lock(&umidi->mutex);
912 if (open) {
913 if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
914 ctl = umidi->roland_load_ctl;
915 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
916 snd_ctl_notify(umidi->chip->card,
917 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
918 update_roland_altsetting(umidi);
919 }
920 } else {
921 if (--umidi->opened == 0 && umidi->roland_load_ctl) {
922 ctl = umidi->roland_load_ctl;
923 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
924 snd_ctl_notify(umidi->chip->card,
925 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
926 }
927 }
928 mutex_unlock(&umidi->mutex);
929}
930
Takashi Iwai86e07d32005-11-17 15:08:02 +0100931static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100933 struct snd_usb_midi* umidi = substream->rmidi->private_data;
934 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int i, j;
936
937 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
938 if (umidi->endpoints[i].out)
939 for (j = 0; j < 0x10; ++j)
940 if (umidi->endpoints[i].out->ports[j].substream == substream) {
941 port = &umidi->endpoints[i].out->ports[j];
942 break;
943 }
944 if (!port) {
945 snd_BUG();
946 return -ENXIO;
947 }
948 substream->runtime->private_data = port;
949 port->state = STATE_UNKNOWN;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200950 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return 0;
952}
953
Takashi Iwai86e07d32005-11-17 15:08:02 +0100954static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200956 substream_open(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return 0;
958}
959
Takashi Iwai86e07d32005-11-17 15:08:02 +0100960static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100962 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 port->active = up;
965 if (up) {
966 if (port->ep->umidi->chip->shutdown) {
967 /* gobble up remaining bytes to prevent wait in
968 * snd_rawmidi_drain_output */
969 while (!snd_rawmidi_transmit_empty(substream))
970 snd_rawmidi_transmit_ack(substream, 1);
971 return;
972 }
Takashi Iwai1f041282008-12-18 12:17:55 +0100973 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975}
976
Clemens Ladischa65dd992009-07-13 13:48:36 +0200977static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
978{
979 struct usbmidi_out_port* port = substream->runtime->private_data;
980 struct snd_usb_midi_out_endpoint *ep = port->ep;
981 unsigned int drain_urbs;
982 DEFINE_WAIT(wait);
983 long timeout = msecs_to_jiffies(50);
984
985 /*
986 * The substream buffer is empty, but some data might still be in the
987 * currently active URBs, so we have to wait for those to complete.
988 */
989 spin_lock_irq(&ep->buffer_lock);
990 drain_urbs = ep->active_urbs;
991 if (drain_urbs) {
992 ep->drain_urbs |= drain_urbs;
993 do {
994 prepare_to_wait(&ep->drain_wait, &wait,
995 TASK_UNINTERRUPTIBLE);
996 spin_unlock_irq(&ep->buffer_lock);
997 timeout = schedule_timeout(timeout);
998 spin_lock_irq(&ep->buffer_lock);
999 drain_urbs &= ep->drain_urbs;
1000 } while (drain_urbs && timeout);
1001 finish_wait(&ep->drain_wait, &wait);
1002 }
1003 spin_unlock_irq(&ep->buffer_lock);
1004}
1005
Takashi Iwai86e07d32005-11-17 15:08:02 +01001006static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001008 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
1010}
1011
Takashi Iwai86e07d32005-11-17 15:08:02 +01001012static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001014 substream_open(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016}
1017
Takashi Iwai86e07d32005-11-17 15:08:02 +01001018static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001020 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (up)
1023 set_bit(substream->number, &umidi->input_triggered);
1024 else
1025 clear_bit(substream->number, &umidi->input_triggered);
1026}
1027
Takashi Iwai86e07d32005-11-17 15:08:02 +01001028static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 .open = snd_usbmidi_output_open,
1030 .close = snd_usbmidi_output_close,
1031 .trigger = snd_usbmidi_output_trigger,
Clemens Ladischa65dd992009-07-13 13:48:36 +02001032 .drain = snd_usbmidi_output_drain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033};
1034
Takashi Iwai86e07d32005-11-17 15:08:02 +01001035static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 .open = snd_usbmidi_input_open,
1037 .close = snd_usbmidi_input_close,
1038 .trigger = snd_usbmidi_input_trigger
1039};
1040
Clemens Ladisched4affa2009-07-13 13:43:59 +02001041static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1042 unsigned int buffer_length)
1043{
1044 usb_buffer_free(umidi->chip->dev, buffer_length,
1045 urb->transfer_buffer, urb->transfer_dma);
1046 usb_free_urb(urb);
1047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/*
1050 * Frees an input endpoint.
1051 * May be called when ep hasn't been initialized completely.
1052 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001053static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001055 unsigned int i;
1056
Clemens Ladisched4affa2009-07-13 13:43:59 +02001057 for (i = 0; i < INPUT_URBS; ++i)
1058 if (ep->urbs[i])
1059 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1060 ep->urbs[i]->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 kfree(ep);
1062}
1063
1064/*
1065 * Creates an input endpoint.
1066 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001067static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1068 struct snd_usb_midi_endpoint_info* ep_info,
1069 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001071 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 void* buffer;
1073 unsigned int pipe;
1074 int length;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001075 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001078 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (!ep)
1080 return -ENOMEM;
1081 ep->umidi = umidi;
1082
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001083 for (i = 0; i < INPUT_URBS; ++i) {
1084 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1085 if (!ep->urbs[i]) {
1086 snd_usbmidi_in_endpoint_delete(ep);
1087 return -ENOMEM;
1088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 if (ep_info->in_interval)
1091 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
1092 else
1093 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
1094 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001095 for (i = 0; i < INPUT_URBS; ++i) {
1096 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
1097 &ep->urbs[i]->transfer_dma);
1098 if (!buffer) {
1099 snd_usbmidi_in_endpoint_delete(ep);
1100 return -ENOMEM;
1101 }
1102 if (ep_info->in_interval)
1103 usb_fill_int_urb(ep->urbs[i], umidi->chip->dev,
1104 pipe, buffer, length,
1105 snd_usbmidi_in_urb_complete,
1106 ep, ep_info->in_interval);
1107 else
1108 usb_fill_bulk_urb(ep->urbs[i], umidi->chip->dev,
1109 pipe, buffer, length,
1110 snd_usbmidi_in_urb_complete, ep);
1111 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 rep->in = ep;
1115 return 0;
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/*
1119 * Frees an output endpoint.
1120 * May be called when ep hasn't been initialized completely.
1121 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001122static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Clemens Ladisched4affa2009-07-13 13:43:59 +02001124 unsigned int i;
1125
1126 for (i = 0; i < OUTPUT_URBS; ++i)
1127 if (ep->urbs[i].urb)
1128 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1129 ep->max_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 kfree(ep);
1131}
1132
1133/*
1134 * Creates an output endpoint, and initializes output ports.
1135 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001136static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1137 struct snd_usb_midi_endpoint_info* ep_info,
1138 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001140 struct snd_usb_midi_out_endpoint* ep;
Clemens Ladisched4affa2009-07-13 13:43:59 +02001141 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 unsigned int pipe;
1143 void* buffer;
1144
1145 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001146 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!ep)
1148 return -ENOMEM;
1149 ep->umidi = umidi;
1150
Clemens Ladisched4affa2009-07-13 13:43:59 +02001151 for (i = 0; i < OUTPUT_URBS; ++i) {
1152 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1153 if (!ep->urbs[i].urb) {
1154 snd_usbmidi_out_endpoint_delete(ep);
1155 return -ENOMEM;
1156 }
1157 ep->urbs[i].ep = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001159 if (ep_info->out_interval)
1160 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
1161 else
1162 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
Clemens Ladisch490cbd92007-05-07 09:29:32 +02001163 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
Clemens Ladisch490cbd92007-05-07 09:29:32 +02001164 ep->max_transfer = 4;
1165 else
1166 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001167 for (i = 0; i < OUTPUT_URBS; ++i) {
1168 buffer = usb_buffer_alloc(umidi->chip->dev,
1169 ep->max_transfer, GFP_KERNEL,
1170 &ep->urbs[i].urb->transfer_dma);
1171 if (!buffer) {
1172 snd_usbmidi_out_endpoint_delete(ep);
1173 return -ENOMEM;
1174 }
1175 if (ep_info->out_interval)
1176 usb_fill_int_urb(ep->urbs[i].urb, umidi->chip->dev,
1177 pipe, buffer, ep->max_transfer,
1178 snd_usbmidi_out_urb_complete,
1179 &ep->urbs[i], ep_info->out_interval);
1180 else
1181 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->chip->dev,
1182 pipe, buffer, ep->max_transfer,
1183 snd_usbmidi_out_urb_complete,
1184 &ep->urbs[i]);
1185 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 spin_lock_init(&ep->buffer_lock);
1189 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
Clemens Ladischa65dd992009-07-13 13:48:36 +02001190 init_waitqueue_head(&ep->drain_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 for (i = 0; i < 0x10; ++i)
1193 if (ep_info->out_cables & (1 << i)) {
1194 ep->ports[i].ep = ep;
1195 ep->ports[i].cable = i << 4;
1196 }
1197
1198 if (umidi->usb_protocol_ops->init_out_endpoint)
1199 umidi->usb_protocol_ops->init_out_endpoint(ep);
1200
1201 rep->out = ep;
1202 return 0;
1203}
1204
1205/*
1206 * Frees everything.
1207 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001208static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 int i;
1211
1212 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001213 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (ep->out)
1215 snd_usbmidi_out_endpoint_delete(ep->out);
1216 if (ep->in)
1217 snd_usbmidi_in_endpoint_delete(ep->in);
1218 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001219 mutex_destroy(&umidi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 kfree(umidi);
1221}
1222
1223/*
1224 * Unlinks all URBs (must be done before the usb_device is deleted).
1225 */
Clemens Ladischee733392005-04-25 10:34:13 +02001226void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001228 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001229 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Takashi Iwai86e07d32005-11-17 15:08:02 +01001231 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001232 /*
1233 * an URB's completion handler may start the timer and
1234 * a timer may submit an URB. To reliably break the cycle
1235 * a flag under lock must be used
1236 */
1237 spin_lock_irq(&umidi->disc_lock);
1238 umidi->disconnected = 1;
1239 spin_unlock_irq(&umidi->disc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001241 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001242 if (ep->out)
1243 tasklet_kill(&ep->out->tasklet);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001244 if (ep->out) {
1245 for (j = 0; j < OUTPUT_URBS; ++j)
1246 usb_kill_urb(ep->out->urbs[j].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (umidi->usb_protocol_ops->finish_out_endpoint)
1248 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1249 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001250 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001251 for (j = 0; j < INPUT_URBS; ++j)
1252 usb_kill_urb(ep->in->urbs[j]);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001253 /* free endpoints here; later call can result in Oops */
1254 if (ep->out) {
1255 snd_usbmidi_out_endpoint_delete(ep->out);
1256 ep->out = NULL;
1257 }
1258 if (ep->in) {
1259 snd_usbmidi_in_endpoint_delete(ep->in);
1260 ep->in = NULL;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001263 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Takashi Iwai86e07d32005-11-17 15:08:02 +01001266static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001268 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 snd_usbmidi_free(umidi);
1270}
1271
Takashi Iwai86e07d32005-11-17 15:08:02 +01001272static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int stream, int number)
1274{
1275 struct list_head* list;
1276
1277 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001278 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (substream->number == number)
1280 return substream;
1281 }
1282 return NULL;
1283}
1284
1285/*
1286 * This list specifies names for ports that do not fit into the standard
1287 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1288 * such as internal control or synthesizer ports.
1289 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001290static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001291 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001292 short int port;
1293 short int voices;
1294 const char *name;
1295 unsigned int seq_flags;
1296} snd_usbmidi_port_info[] = {
1297#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1298 { .id = USB_ID(vendor, product), \
1299 .port = num, .voices = voices_, \
1300 .name = name_, .seq_flags = flags }
1301#define EXTERNAL_PORT(vendor, product, num, name) \
1302 PORT_INFO(vendor, product, num, name, 0, \
1303 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1304 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1305 SNDRV_SEQ_PORT_TYPE_PORT)
1306#define CONTROL_PORT(vendor, product, num, name) \
1307 PORT_INFO(vendor, product, num, name, 0, \
1308 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1309 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1310#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1311 PORT_INFO(vendor, product, num, name, voices, \
1312 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1313 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1314 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1315 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1316 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1317 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1318 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1319#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1320 PORT_INFO(vendor, product, num, name, voices, \
1321 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1322 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1323 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1324 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1325 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1326 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1327 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1328 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001330 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001332 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1333 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1334 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1335 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1336 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1337 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001339 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1340 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001342 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1343 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1344 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001346 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1347 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1348 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001350 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1351 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1352 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001354 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001356 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1357 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1358 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1359 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001361 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001363 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1364 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1365 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001367 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1368 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1369 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1370 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001372 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1373 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001375 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1376 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1377 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001379 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1380 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1381 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001383 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1384 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001386 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1387 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001389 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1390 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1391 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001393 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1394 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1395 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001396 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001397 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001399 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1400 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001402 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1403 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001405 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1406 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1407 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408};
1409
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001410static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1411{
1412 int i;
1413
1414 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1415 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1416 snd_usbmidi_port_info[i].port == number)
1417 return &snd_usbmidi_port_info[i];
1418 }
1419 return NULL;
1420}
1421
1422static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1423 struct snd_seq_port_info *seq_port_info)
1424{
1425 struct snd_usb_midi *umidi = rmidi->private_data;
1426 struct port_info *port_info;
1427
1428 /* TODO: read port flags from descriptors */
1429 port_info = find_port_info(umidi, number);
1430 if (port_info) {
1431 seq_port_info->type = port_info->seq_flags;
1432 seq_port_info->midi_voices = port_info->voices;
1433 }
1434}
1435
Takashi Iwai86e07d32005-11-17 15:08:02 +01001436static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001438 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001440 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 const char *name_format;
1442
Takashi Iwai86e07d32005-11-17 15:08:02 +01001443 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (!substream) {
1445 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1446 return;
1447 }
1448
1449 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001450 port_info = find_port_info(umidi, number);
1451 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 snprintf(substream->name, sizeof(substream->name),
1453 name_format, umidi->chip->card->shortname, number + 1);
1454
1455 *rsubstream = substream;
1456}
1457
1458/*
1459 * Creates the endpoints and their ports.
1460 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001461static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1462 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 int i, j, err;
1465 int out_ports = 0, in_ports = 0;
1466
1467 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1468 if (endpoints[i].out_cables) {
1469 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1470 &umidi->endpoints[i]);
1471 if (err < 0)
1472 return err;
1473 }
1474 if (endpoints[i].in_cables) {
1475 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1476 &umidi->endpoints[i]);
1477 if (err < 0)
1478 return err;
1479 }
1480
1481 for (j = 0; j < 0x10; ++j) {
1482 if (endpoints[i].out_cables & (1 << j)) {
1483 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1484 &umidi->endpoints[i].out->ports[j].substream);
1485 ++out_ports;
1486 }
1487 if (endpoints[i].in_cables & (1 << j)) {
1488 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1489 &umidi->endpoints[i].in->ports[j].substream);
1490 ++in_ports;
1491 }
1492 }
1493 }
1494 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1495 out_ports, in_ports);
1496 return 0;
1497}
1498
1499/*
1500 * Returns MIDIStreaming device capabilities.
1501 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001502static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1503 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 struct usb_interface* intf;
1506 struct usb_host_interface *hostif;
1507 struct usb_interface_descriptor* intfd;
1508 struct usb_ms_header_descriptor* ms_header;
1509 struct usb_host_endpoint *hostep;
1510 struct usb_endpoint_descriptor* ep;
1511 struct usb_ms_endpoint_descriptor* ms_ep;
1512 int i, epidx;
1513
1514 intf = umidi->iface;
1515 if (!intf)
1516 return -ENXIO;
1517 hostif = &intf->altsetting[0];
1518 intfd = get_iface_desc(hostif);
1519 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1520 if (hostif->extralen >= 7 &&
1521 ms_header->bLength >= 7 &&
1522 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1523 ms_header->bDescriptorSubtype == HEADER)
1524 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1525 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1526 else
1527 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1528
1529 epidx = 0;
1530 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1531 hostep = &hostif->endpoint[i];
1532 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001533 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 continue;
1535 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1536 if (hostep->extralen < 4 ||
1537 ms_ep->bLength < 4 ||
1538 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1539 ms_ep->bDescriptorSubtype != MS_GENERAL)
1540 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001541 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (endpoints[epidx].out_ep) {
1543 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1544 snd_printk(KERN_WARNING "too many endpoints\n");
1545 break;
1546 }
1547 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001548 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1549 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001551 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1552 /*
1553 * Low speed bulk transfers don't exist, so
1554 * force interrupt transfers for devices like
1555 * ESI MIDI Mate that try to use them anyway.
1556 */
1557 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1559 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1560 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1561 } else {
1562 if (endpoints[epidx].in_ep) {
1563 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1564 snd_printk(KERN_WARNING "too many endpoints\n");
1565 break;
1566 }
1567 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001568 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1569 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001571 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1572 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1574 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1575 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1576 }
1577 }
1578 return 0;
1579}
1580
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001581static int roland_load_info(struct snd_kcontrol *kcontrol,
1582 struct snd_ctl_elem_info *info)
1583{
1584 static const char *const names[] = { "High Load", "Light Load" };
1585
1586 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1587 info->count = 1;
1588 info->value.enumerated.items = 2;
1589 if (info->value.enumerated.item > 1)
1590 info->value.enumerated.item = 1;
1591 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
1592 return 0;
1593}
1594
1595static int roland_load_get(struct snd_kcontrol *kcontrol,
1596 struct snd_ctl_elem_value *value)
1597{
1598 value->value.enumerated.item[0] = kcontrol->private_value;
1599 return 0;
1600}
1601
1602static int roland_load_put(struct snd_kcontrol *kcontrol,
1603 struct snd_ctl_elem_value *value)
1604{
1605 struct snd_usb_midi* umidi = kcontrol->private_data;
1606 int changed;
1607
1608 if (value->value.enumerated.item[0] > 1)
1609 return -EINVAL;
1610 mutex_lock(&umidi->mutex);
1611 changed = value->value.enumerated.item[0] != kcontrol->private_value;
1612 if (changed)
1613 kcontrol->private_value = value->value.enumerated.item[0];
1614 mutex_unlock(&umidi->mutex);
1615 return changed;
1616}
1617
1618static struct snd_kcontrol_new roland_load_ctl = {
1619 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1620 .name = "MIDI Input Mode",
1621 .info = roland_load_info,
1622 .get = roland_load_get,
1623 .put = roland_load_put,
1624 .private_value = 1,
1625};
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627/*
1628 * On Roland devices, use the second alternate setting to be able to use
1629 * the interrupt input endpoint.
1630 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001631static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 struct usb_interface* intf;
1634 struct usb_host_interface *hostif;
1635 struct usb_interface_descriptor* intfd;
1636
1637 intf = umidi->iface;
1638 if (!intf || intf->num_altsetting != 2)
1639 return;
1640
1641 hostif = &intf->altsetting[1];
1642 intfd = get_iface_desc(hostif);
1643 if (intfd->bNumEndpoints != 2 ||
1644 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1645 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1646 return;
1647
1648 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1649 intfd->bAlternateSetting);
1650 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1651 intfd->bAlternateSetting);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001652
1653 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
1654 if (snd_ctl_add(umidi->chip->card, umidi->roland_load_ctl) < 0)
1655 umidi->roland_load_ctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658/*
1659 * Try to find any usable endpoints in the interface.
1660 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001661static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1662 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 int max_endpoints)
1664{
1665 struct usb_interface* intf;
1666 struct usb_host_interface *hostif;
1667 struct usb_interface_descriptor* intfd;
1668 struct usb_endpoint_descriptor* epd;
1669 int i, out_eps = 0, in_eps = 0;
1670
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001671 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 snd_usbmidi_switch_roland_altsetting(umidi);
1673
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001674 if (endpoint[0].out_ep || endpoint[0].in_ep)
1675 return 0;
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 intf = umidi->iface;
1678 if (!intf || intf->num_altsetting < 1)
1679 return -ENOENT;
1680 hostif = intf->cur_altsetting;
1681 intfd = get_iface_desc(hostif);
1682
1683 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1684 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001685 if (!usb_endpoint_xfer_bulk(epd) &&
1686 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 continue;
1688 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001689 usb_endpoint_dir_out(epd)) {
1690 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1691 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 endpoint[out_eps].out_interval = epd->bInterval;
1693 ++out_eps;
1694 }
1695 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001696 usb_endpoint_dir_in(epd)) {
1697 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1698 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 endpoint[in_eps].in_interval = epd->bInterval;
1700 ++in_eps;
1701 }
1702 }
1703 return (out_eps || in_eps) ? 0 : -ENOENT;
1704}
1705
1706/*
1707 * Detects the endpoints for one-port-per-endpoint protocols.
1708 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001709static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1710 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 int err, i;
1713
1714 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1715 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1716 if (endpoints[i].out_ep)
1717 endpoints[i].out_cables = 0x0001;
1718 if (endpoints[i].in_ep)
1719 endpoints[i].in_cables = 0x0001;
1720 }
1721 return err;
1722}
1723
1724/*
1725 * Detects the endpoints and ports of Yamaha devices.
1726 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001727static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1728 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 struct usb_interface* intf;
1731 struct usb_host_interface *hostif;
1732 struct usb_interface_descriptor* intfd;
1733 uint8_t* cs_desc;
1734
1735 intf = umidi->iface;
1736 if (!intf)
1737 return -ENOENT;
1738 hostif = intf->altsetting;
1739 intfd = get_iface_desc(hostif);
1740 if (intfd->bNumEndpoints < 1)
1741 return -ENOENT;
1742
1743 /*
1744 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1745 * necessarily with any useful contents. So simply count 'em.
1746 */
1747 for (cs_desc = hostif->extra;
1748 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1749 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001750 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (cs_desc[2] == MIDI_IN_JACK)
1752 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1753 else if (cs_desc[2] == MIDI_OUT_JACK)
1754 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1755 }
1756 }
1757 if (!endpoint->in_cables && !endpoint->out_cables)
1758 return -ENOENT;
1759
1760 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1761}
1762
1763/*
1764 * Creates the endpoints and their ports for Midiman devices.
1765 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001766static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1767 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001769 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 struct usb_interface* intf;
1771 struct usb_host_interface *hostif;
1772 struct usb_interface_descriptor* intfd;
1773 struct usb_endpoint_descriptor* epd;
1774 int cable, err;
1775
1776 intf = umidi->iface;
1777 if (!intf)
1778 return -ENOENT;
1779 hostif = intf->altsetting;
1780 intfd = get_iface_desc(hostif);
1781 /*
1782 * The various MidiSport devices have more or less random endpoint
1783 * numbers, so we have to identify the endpoints by their index in
1784 * the descriptor array, like the driver for that other OS does.
1785 *
1786 * There is one interrupt input endpoint for all input ports, one
1787 * bulk output endpoint for even-numbered ports, and one for odd-
1788 * numbered ports. Both bulk output endpoints have corresponding
1789 * input bulk endpoints (at indices 1 and 3) which aren't used.
1790 */
1791 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1792 snd_printdd(KERN_ERR "not enough endpoints\n");
1793 return -ENOENT;
1794 }
1795
1796 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001797 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1799 return -ENXIO;
1800 }
1801 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001802 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1804 return -ENXIO;
1805 }
1806 if (endpoint->out_cables > 0x0001) {
1807 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001808 if (!usb_endpoint_dir_out(epd) ||
1809 !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1811 return -ENXIO;
1812 }
1813 }
1814
1815 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01001816 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 ep_info.out_cables = endpoint->out_cables & 0x5555;
1818 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1819 if (err < 0)
1820 return err;
1821
1822 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1823 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1824 ep_info.in_cables = endpoint->in_cables;
1825 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1826 if (err < 0)
1827 return err;
1828
1829 if (endpoint->out_cables > 0x0001) {
1830 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1831 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1832 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1833 if (err < 0)
1834 return err;
1835 }
1836
1837 for (cable = 0; cable < 0x10; ++cable) {
1838 if (endpoint->out_cables & (1 << cable))
1839 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1840 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1841 if (endpoint->in_cables & (1 << cable))
1842 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1843 &umidi->endpoints[0].in->ports[cable].substream);
1844 }
1845 return 0;
1846}
1847
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001848static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1849 .get_port_info = snd_usbmidi_get_port_info,
1850};
1851
Takashi Iwai86e07d32005-11-17 15:08:02 +01001852static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 int out_ports, int in_ports)
1854{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001855 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 int err;
1857
1858 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1859 umidi->chip->next_midi_device++,
1860 out_ports, in_ports, &rmidi);
1861 if (err < 0)
1862 return err;
1863 strcpy(rmidi->name, umidi->chip->card->shortname);
1864 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1865 SNDRV_RAWMIDI_INFO_INPUT |
1866 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001867 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 rmidi->private_data = umidi;
1869 rmidi->private_free = snd_usbmidi_rawmidi_free;
1870 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1871 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1872
1873 umidi->rmidi = rmidi;
1874 return 0;
1875}
1876
1877/*
1878 * Temporarily stop input.
1879 */
1880void snd_usbmidi_input_stop(struct list_head* p)
1881{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001882 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001883 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Takashi Iwai86e07d32005-11-17 15:08:02 +01001885 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001887 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001889 for (j = 0; j < INPUT_URBS; ++j)
1890 usb_kill_urb(ep->in->urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892}
1893
Takashi Iwai86e07d32005-11-17 15:08:02 +01001894static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001896 unsigned int i;
1897
1898 if (!ep)
1899 return;
1900 for (i = 0; i < INPUT_URBS; ++i) {
1901 struct urb* urb = ep->urbs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 urb->dev = ep->umidi->chip->dev;
1903 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1904 }
1905}
1906
1907/*
1908 * Resume input after a call to snd_usbmidi_input_stop().
1909 */
1910void snd_usbmidi_input_start(struct list_head* p)
1911{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001912 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 int i;
1914
Takashi Iwai86e07d32005-11-17 15:08:02 +01001915 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1917 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1918}
1919
1920/*
1921 * Creates and registers everything needed for a MIDI streaming interface.
1922 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001923int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 struct usb_interface* iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001925 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001927 struct snd_usb_midi* umidi;
1928 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 int out_ports, in_ports;
1930 int i, err;
1931
Takashi Iwai561b2202005-09-09 14:22:34 +02001932 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (!umidi)
1934 return -ENOMEM;
1935 umidi->chip = chip;
1936 umidi->iface = iface;
1937 umidi->quirk = quirk;
1938 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001939 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001940 spin_lock_init(&umidi->disc_lock);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001941 mutex_init(&umidi->mutex);
Clemens Ladischc8846972005-08-02 15:26:52 +02001942 umidi->error_timer.function = snd_usbmidi_error_timer;
1943 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 /* detect the endpoint(s) to use */
1946 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001947 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1948 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd05cc102007-05-07 09:28:53 +02001950 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1951 umidi->usb_protocol_ops =
1952 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001953 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02001954 case QUIRK_MIDI_US122L:
1955 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1956 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02001957 case QUIRK_MIDI_FIXED_ENDPOINT:
1958 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001959 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001960 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1961 break;
1962 case QUIRK_MIDI_YAMAHA:
1963 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1964 break;
1965 case QUIRK_MIDI_MIDIMAN:
1966 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1967 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001968 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001969 err = 0;
1970 break;
1971 case QUIRK_MIDI_NOVATION:
1972 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1973 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1974 break;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001975 case QUIRK_MIDI_FASTLANE:
Clemens Ladischd1bda042005-09-14 08:36:03 +02001976 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001977 /*
1978 * Interface 1 contains isochronous endpoints, but with the same
1979 * numbers as in interface 0. Since it is interface 1 that the
1980 * USB core has most recently seen, these descriptors are now
1981 * associated with the endpoint numbers. This will foul up our
1982 * attempts to submit bulk/interrupt URBs to the endpoints in
1983 * interface 0, so we have to make sure that the USB core looks
1984 * again at interface 0 by calling usb_set_interface() on it.
1985 */
1986 usb_set_interface(umidi->chip->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02001987 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1988 break;
1989 case QUIRK_MIDI_EMAGIC:
1990 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1991 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001992 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001993 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1994 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01001995 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02001996 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001997 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1998 break;
1999 default:
2000 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2001 err = -ENXIO;
2002 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004 if (err < 0) {
2005 kfree(umidi);
2006 return err;
2007 }
2008
2009 /* create rawmidi device */
2010 out_ports = 0;
2011 in_ports = 0;
2012 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Akinobu Mitafbc54392009-11-20 14:56:52 +09002013 out_ports += hweight16(endpoints[i].out_cables);
2014 in_ports += hweight16(endpoints[i].in_cables);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2017 if (err < 0) {
2018 kfree(umidi);
2019 return err;
2020 }
2021
2022 /* create endpoint/port structures */
2023 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2024 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2025 else
2026 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2027 if (err < 0) {
2028 snd_usbmidi_free(umidi);
2029 return err;
2030 }
2031
2032 list_add(&umidi->list, &umidi->chip->midi_list);
2033
2034 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2035 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2036 return 0;
2037}
2038
2039EXPORT_SYMBOL(snd_usb_create_midi_interface);
2040EXPORT_SYMBOL(snd_usbmidi_input_stop);
2041EXPORT_SYMBOL(snd_usbmidi_input_start);
2042EXPORT_SYMBOL(snd_usbmidi_disconnect);