blob: aae50df062326a2fd7b2f1a0957ec65d379b5af6 [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 {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100105 struct usb_device *dev;
106 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100108 const struct snd_usb_audio_quirk *quirk;
109 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct usb_protocol_ops* usb_protocol_ops;
111 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200112 struct timer_list error_timer;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100113 spinlock_t disc_lock;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200114 struct mutex mutex;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100115 u32 usb_id;
116 int next_midi_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100119 struct snd_usb_midi_out_endpoint *out;
120 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 } endpoints[MIDI_MAX_ENDPOINTS];
122 unsigned long input_triggered;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200123 unsigned int opened;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100124 unsigned char disconnected;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200125
126 struct snd_kcontrol *roland_load_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100130 struct snd_usb_midi* umidi;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200131 struct out_urb_context {
132 struct urb *urb;
133 struct snd_usb_midi_out_endpoint *ep;
134 } urbs[OUTPUT_URBS];
135 unsigned int active_urbs;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200136 unsigned int drain_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int max_transfer; /* size of urb buffer */
138 struct tasklet_struct tasklet;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200139 unsigned int next_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 spinlock_t buffer_lock;
141
142 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100143 struct snd_usb_midi_out_endpoint* ep;
144 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int active;
146 uint8_t cable; /* cable number << 4 */
147 uint8_t state;
148#define STATE_UNKNOWN 0
149#define STATE_1PARAM 1
150#define STATE_2PARAM_1 2
151#define STATE_2PARAM_2 3
152#define STATE_SYSEX_0 4
153#define STATE_SYSEX_1 5
154#define STATE_SYSEX_2 6
155 uint8_t data[2];
156 } ports[0x10];
157 int current_port;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200158
159 wait_queue_head_t drain_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100163 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200164 struct urb* urbs[INPUT_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100166 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200167 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200169 u8 seen_f5;
170 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int current_port;
172};
173
Takashi Iwai86e07d32005-11-17 15:08:02 +0100174static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176static const uint8_t snd_usbmidi_cin_length[] = {
177 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
178};
179
180/*
181 * Submits the URB, with error handling.
182 */
Al Viro55016f12005-10-21 03:21:58 -0400183static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int err = usb_submit_urb(urb, flags);
186 if (err < 0 && err != -ENODEV)
187 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
188 return err;
189}
190
191/*
192 * Error handling for URB completion functions.
193 */
194static int snd_usbmidi_urb_error(int status)
195{
Clemens Ladischc8846972005-08-02 15:26:52 +0200196 switch (status) {
197 /* manually unlinked, or device gone */
198 case -ENOENT:
199 case -ECONNRESET:
200 case -ESHUTDOWN:
201 case -ENODEV:
202 return -ENODEV;
203 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700204 case -EPROTO:
205 case -ETIME:
206 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200207 return -EIO;
208 default:
209 snd_printk(KERN_ERR "urb status %d\n", status);
210 return 0; /* continue */
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/*
215 * Receives a chunk of MIDI data.
216 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100217static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 uint8_t* data, int length)
219{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100220 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (!port->substream) {
223 snd_printd("unexpected port %d!\n", portidx);
224 return;
225 }
226 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
227 return;
228 snd_rawmidi_receive(port->substream, data, length);
229}
230
231#ifdef DUMP_PACKETS
232static void dump_urb(const char *type, const u8 *data, int length)
233{
234 snd_printk(KERN_DEBUG "%s packet: [", type);
235 for (; length > 0; ++data, --length)
236 printk(" %02x", *data);
237 printk(" ]\n");
238}
239#else
240#define dump_urb(type, data, length) /* nothing */
241#endif
242
243/*
244 * Processes the data read from the device.
245 */
David Howells7d12e782006-10-05 14:55:46 +0100246static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100248 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (urb->status == 0) {
251 dump_urb("received", urb->transfer_buffer, urb->actual_length);
252 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
253 urb->actual_length);
254 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200255 int err = snd_usbmidi_urb_error(urb->status);
256 if (err < 0) {
257 if (err != -ENODEV) {
258 ep->error_resubmit = 1;
259 mod_timer(&ep->umidi->error_timer,
260 jiffies + ERROR_DELAY_JIFFIES);
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100266 urb->dev = ep->umidi->dev;
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200267 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
David Howells7d12e782006-10-05 14:55:46 +0100270static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200272 struct out_urb_context *context = urb->context;
273 struct snd_usb_midi_out_endpoint* ep = context->ep;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200274 unsigned int urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 spin_lock(&ep->buffer_lock);
Clemens Ladischa65dd992009-07-13 13:48:36 +0200277 urb_index = context - ep->urbs;
278 ep->active_urbs &= ~(1 << urb_index);
279 if (unlikely(ep->drain_urbs)) {
280 ep->drain_urbs &= ~(1 << urb_index);
281 wake_up(&ep->drain_wait);
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 spin_unlock(&ep->buffer_lock);
284 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200285 int err = snd_usbmidi_urb_error(urb->status);
286 if (err < 0) {
287 if (err != -ENODEV)
288 mod_timer(&ep->umidi->error_timer,
289 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 snd_usbmidi_do_output(ep);
294}
295
296/*
297 * This is called when some data should be transferred to the device
298 * (from one or more substreams).
299 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100300static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200302 unsigned int urb_index;
303 struct urb* urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 unsigned long flags;
305
306 spin_lock_irqsave(&ep->buffer_lock, flags);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100307 if (ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_unlock_irqrestore(&ep->buffer_lock, flags);
309 return;
310 }
311
Clemens Ladischa65dd992009-07-13 13:48:36 +0200312 urb_index = ep->next_urb;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200313 for (;;) {
Clemens Ladischa65dd992009-07-13 13:48:36 +0200314 if (!(ep->active_urbs & (1 << urb_index))) {
315 urb = ep->urbs[urb_index].urb;
316 urb->transfer_buffer_length = 0;
317 ep->umidi->usb_protocol_ops->output(ep, urb);
318 if (urb->transfer_buffer_length == 0)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Clemens Ladischa65dd992009-07-13 13:48:36 +0200321 dump_urb("sending", urb->transfer_buffer,
322 urb->transfer_buffer_length);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100323 urb->dev = ep->umidi->dev;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200324 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
325 break;
326 ep->active_urbs |= 1 << urb_index;
327 }
328 if (++urb_index >= OUTPUT_URBS)
329 urb_index = 0;
330 if (urb_index == ep->next_urb)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Clemens Ladischa65dd992009-07-13 13:48:36 +0200333 ep->next_urb = urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 spin_unlock_irqrestore(&ep->buffer_lock, flags);
335}
336
337static void snd_usbmidi_out_tasklet(unsigned long data)
338{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100339 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 snd_usbmidi_do_output(ep);
342}
343
Clemens Ladischc8846972005-08-02 15:26:52 +0200344/* called after transfers had been interrupted due to some USB error */
345static void snd_usbmidi_error_timer(unsigned long data)
346{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100347 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200348 unsigned int i, j;
Clemens Ladischc8846972005-08-02 15:26:52 +0200349
Takashi Iwaic0792e02008-02-22 18:34:44 +0100350 spin_lock(&umidi->disc_lock);
351 if (umidi->disconnected) {
352 spin_unlock(&umidi->disc_lock);
353 return;
354 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200355 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100356 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200357 if (in && in->error_resubmit) {
358 in->error_resubmit = 0;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200359 for (j = 0; j < INPUT_URBS; ++j) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100360 in->urbs[j]->dev = umidi->dev;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200361 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
362 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200363 }
364 if (umidi->endpoints[i].out)
365 snd_usbmidi_do_output(umidi->endpoints[i].out);
366 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100367 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100371static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 const void *data, int len)
373{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200374 int err = 0;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700375 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!buf)
377 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 dump_urb("sending", buf, len);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200379 if (ep->urbs[0].urb)
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100380 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200381 buf, len, NULL, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kfree(buf);
383 return err;
384}
385
386/*
387 * Standard USB MIDI protocol: see the spec.
388 * Midiman protocol: like the standard protocol, but the control byte is the
389 * fourth byte in each packet, and uses length instead of CIN.
390 */
391
Takashi Iwai86e07d32005-11-17 15:08:02 +0100392static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 uint8_t* buffer, int buffer_length)
394{
395 int i;
396
397 for (i = 0; i + 3 < buffer_length; i += 4)
398 if (buffer[i] != 0) {
399 int cable = buffer[i] >> 4;
400 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
401 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
402 }
403}
404
Takashi Iwai86e07d32005-11-17 15:08:02 +0100405static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 uint8_t* buffer, int buffer_length)
407{
408 int i;
409
410 for (i = 0; i + 3 < buffer_length; i += 4)
411 if (buffer[i + 3] != 0) {
412 int port = buffer[i + 3] >> 4;
413 int length = buffer[i + 3] & 3;
414 snd_usbmidi_input_data(ep, port, &buffer[i], length);
415 }
416}
417
418/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200419 * Buggy M-Audio device: running status on input results in a packet that has
420 * the data bytes but not the status byte and that is marked with CIN 4.
421 */
422static void snd_usbmidi_maudio_broken_running_status_input(
423 struct snd_usb_midi_in_endpoint* ep,
424 uint8_t* buffer, int buffer_length)
425{
426 int i;
427
428 for (i = 0; i + 3 < buffer_length; i += 4)
429 if (buffer[i] != 0) {
430 int cable = buffer[i] >> 4;
431 u8 cin = buffer[i] & 0x0f;
432 struct usbmidi_in_port *port = &ep->ports[cable];
433 int length;
434
435 length = snd_usbmidi_cin_length[cin];
436 if (cin == 0xf && buffer[i + 1] >= 0xf8)
437 ; /* realtime msg: no running status change */
438 else if (cin >= 0x8 && cin <= 0xe)
439 /* channel msg */
440 port->running_status_length = length - 1;
441 else if (cin == 0x4 &&
442 port->running_status_length != 0 &&
443 buffer[i + 1] < 0x80)
444 /* CIN 4 that is not a SysEx */
445 length = port->running_status_length;
446 else
447 /*
448 * All other msgs cannot begin running status.
449 * (A channel msg sent as two or three CIN 0xF
450 * packets could in theory, but this device
451 * doesn't use this format.)
452 */
453 port->running_status_length = 0;
454 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
455 }
456}
457
458/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200459 * CME protocol: like the standard protocol, but SysEx commands are sent as a
460 * single USB packet preceded by a 0x0F byte.
461 */
462static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
463 uint8_t *buffer, int buffer_length)
464{
465 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
466 snd_usbmidi_standard_input(ep, buffer, buffer_length);
467 else
468 snd_usbmidi_input_data(ep, buffer[0] >> 4,
469 &buffer[1], buffer_length - 1);
470}
471
472/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * Adds one USB MIDI packet to the output buffer.
474 */
475static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
476 uint8_t p1, uint8_t p2, uint8_t p3)
477{
478
479 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
480 buf[0] = p0;
481 buf[1] = p1;
482 buf[2] = p2;
483 buf[3] = p3;
484 urb->transfer_buffer_length += 4;
485}
486
487/*
488 * Adds one Midiman packet to the output buffer.
489 */
490static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
491 uint8_t p1, uint8_t p2, uint8_t p3)
492{
493
494 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
495 buf[0] = p1;
496 buf[1] = p2;
497 buf[2] = p3;
498 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
499 urb->transfer_buffer_length += 4;
500}
501
502/*
503 * Converts MIDI commands to USB MIDI packets.
504 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100505static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 uint8_t b, struct urb* urb)
507{
508 uint8_t p0 = port->cable;
509 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
510 port->ep->umidi->usb_protocol_ops->output_packet;
511
512 if (b >= 0xf8) {
513 output_packet(urb, p0 | 0x0f, b, 0, 0);
514 } else if (b >= 0xf0) {
515 switch (b) {
516 case 0xf0:
517 port->data[0] = b;
518 port->state = STATE_SYSEX_1;
519 break;
520 case 0xf1:
521 case 0xf3:
522 port->data[0] = b;
523 port->state = STATE_1PARAM;
524 break;
525 case 0xf2:
526 port->data[0] = b;
527 port->state = STATE_2PARAM_1;
528 break;
529 case 0xf4:
530 case 0xf5:
531 port->state = STATE_UNKNOWN;
532 break;
533 case 0xf6:
534 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
535 port->state = STATE_UNKNOWN;
536 break;
537 case 0xf7:
538 switch (port->state) {
539 case STATE_SYSEX_0:
540 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
541 break;
542 case STATE_SYSEX_1:
543 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
544 break;
545 case STATE_SYSEX_2:
546 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
547 break;
548 }
549 port->state = STATE_UNKNOWN;
550 break;
551 }
552 } else if (b >= 0x80) {
553 port->data[0] = b;
554 if (b >= 0xc0 && b <= 0xdf)
555 port->state = STATE_1PARAM;
556 else
557 port->state = STATE_2PARAM_1;
558 } else { /* b < 0x80 */
559 switch (port->state) {
560 case STATE_1PARAM:
561 if (port->data[0] < 0xf0) {
562 p0 |= port->data[0] >> 4;
563 } else {
564 p0 |= 0x02;
565 port->state = STATE_UNKNOWN;
566 }
567 output_packet(urb, p0, port->data[0], b, 0);
568 break;
569 case STATE_2PARAM_1:
570 port->data[1] = b;
571 port->state = STATE_2PARAM_2;
572 break;
573 case STATE_2PARAM_2:
574 if (port->data[0] < 0xf0) {
575 p0 |= port->data[0] >> 4;
576 port->state = STATE_2PARAM_1;
577 } else {
578 p0 |= 0x03;
579 port->state = STATE_UNKNOWN;
580 }
581 output_packet(urb, p0, port->data[0], port->data[1], b);
582 break;
583 case STATE_SYSEX_0:
584 port->data[0] = b;
585 port->state = STATE_SYSEX_1;
586 break;
587 case STATE_SYSEX_1:
588 port->data[1] = b;
589 port->state = STATE_SYSEX_2;
590 break;
591 case STATE_SYSEX_2:
592 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
593 port->state = STATE_SYSEX_0;
594 break;
595 }
596 }
597}
598
Clemens Ladisched4affa2009-07-13 13:43:59 +0200599static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
600 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int p;
603
604 /* FIXME: lower-numbered ports can starve higher-numbered ports */
605 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100606 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!port->active)
608 continue;
609 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
610 uint8_t b;
611 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
612 port->active = 0;
613 break;
614 }
615 snd_usbmidi_transmit_byte(port, b, urb);
616 }
617 }
618}
619
620static struct usb_protocol_ops snd_usbmidi_standard_ops = {
621 .input = snd_usbmidi_standard_input,
622 .output = snd_usbmidi_standard_output,
623 .output_packet = snd_usbmidi_output_standard_packet,
624};
625
626static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
627 .input = snd_usbmidi_midiman_input,
628 .output = snd_usbmidi_standard_output,
629 .output_packet = snd_usbmidi_output_midiman_packet,
630};
631
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200632static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
633 .input = snd_usbmidi_maudio_broken_running_status_input,
634 .output = snd_usbmidi_standard_output,
635 .output_packet = snd_usbmidi_output_standard_packet,
636};
637
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200638static struct usb_protocol_ops snd_usbmidi_cme_ops = {
639 .input = snd_usbmidi_cme_input,
640 .output = snd_usbmidi_standard_output,
641 .output_packet = snd_usbmidi_output_standard_packet,
642};
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/*
645 * Novation USB MIDI protocol: number of data bytes is in the first byte
646 * (when receiving) (+1!) or in the second byte (when sending); data begins
647 * at the third byte.
648 */
649
Takashi Iwai86e07d32005-11-17 15:08:02 +0100650static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 uint8_t* buffer, int buffer_length)
652{
653 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
654 return;
655 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
656}
657
Clemens Ladisched4affa2009-07-13 13:43:59 +0200658static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
659 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 uint8_t* transfer_buffer;
662 int count;
663
664 if (!ep->ports[0].active)
665 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200666 transfer_buffer = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 count = snd_rawmidi_transmit(ep->ports[0].substream,
668 &transfer_buffer[2],
669 ep->max_transfer - 2);
670 if (count < 1) {
671 ep->ports[0].active = 0;
672 return;
673 }
674 transfer_buffer[0] = 0;
675 transfer_buffer[1] = count;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200676 urb->transfer_buffer_length = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static struct usb_protocol_ops snd_usbmidi_novation_ops = {
680 .input = snd_usbmidi_novation_input,
681 .output = snd_usbmidi_novation_output,
682};
683
684/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200685 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
687
Takashi Iwai86e07d32005-11-17 15:08:02 +0100688static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200689 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
692}
693
Clemens Ladisched4affa2009-07-13 13:43:59 +0200694static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
695 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 int count;
698
699 if (!ep->ports[0].active)
700 return;
701 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200702 urb->transfer_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ep->max_transfer);
704 if (count < 1) {
705 ep->ports[0].active = 0;
706 return;
707 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200708 urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200711static struct usb_protocol_ops snd_usbmidi_raw_ops = {
712 .input = snd_usbmidi_raw_input,
713 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714};
715
Karsten Wiese030a07e2008-07-30 15:13:29 +0200716static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
717 uint8_t *buffer, int buffer_length)
718{
719 if (buffer_length != 9)
720 return;
721 buffer_length = 8;
722 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
723 buffer_length--;
724 if (buffer_length)
725 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
726}
727
Clemens Ladisched4affa2009-07-13 13:43:59 +0200728static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
729 struct urb *urb)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200730{
731 int count;
732
733 if (!ep->ports[0].active)
734 return;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100735 count = snd_usb_get_speed(ep->umidi->dev) == USB_SPEED_HIGH ? 1 : 2;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200736 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200737 urb->transfer_buffer,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200738 count);
739 if (count < 1) {
740 ep->ports[0].active = 0;
741 return;
742 }
743
Clemens Ladisched4affa2009-07-13 13:43:59 +0200744 memset(urb->transfer_buffer + count, 0xFD, 9 - count);
745 urb->transfer_buffer_length = count;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200746}
747
748static struct usb_protocol_ops snd_usbmidi_122l_ops = {
749 .input = snd_usbmidi_us122l_input,
750 .output = snd_usbmidi_us122l_output,
751};
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/*
754 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
755 */
756
Takashi Iwai86e07d32005-11-17 15:08:02 +0100757static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 static const u8 init_data[] = {
760 /* initialization magic: "get version" */
761 0xf0,
762 0x00, 0x20, 0x31, /* Emagic */
763 0x64, /* Unitor8 */
764 0x0b, /* version number request */
765 0x00, /* command version */
766 0x00, /* EEPROM, box 0 */
767 0xf7
768 };
769 send_bulk_static_data(ep, init_data, sizeof(init_data));
770 /* while we're at it, pour on more magic */
771 send_bulk_static_data(ep, init_data, sizeof(init_data));
772}
773
Takashi Iwai86e07d32005-11-17 15:08:02 +0100774static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 static const u8 finish_data[] = {
777 /* switch to patch mode with last preset */
778 0xf0,
779 0x00, 0x20, 0x31, /* Emagic */
780 0x64, /* Unitor8 */
781 0x10, /* patch switch command */
782 0x00, /* command version */
783 0x7f, /* to all boxes */
784 0x40, /* last preset in EEPROM */
785 0xf7
786 };
787 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
788}
789
Takashi Iwai86e07d32005-11-17 15:08:02 +0100790static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 uint8_t* buffer, int buffer_length)
792{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200793 int i;
794
795 /* FF indicates end of valid data */
796 for (i = 0; i < buffer_length; ++i)
797 if (buffer[i] == 0xff) {
798 buffer_length = i;
799 break;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* handle F5 at end of last buffer */
803 if (ep->seen_f5)
804 goto switch_port;
805
806 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* determine size of data until next F5 */
808 for (i = 0; i < buffer_length; ++i)
809 if (buffer[i] == 0xf5)
810 break;
811 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
812 buffer += i;
813 buffer_length -= i;
814
815 if (buffer_length <= 0)
816 break;
817 /* assert(buffer[0] == 0xf5); */
818 ep->seen_f5 = 1;
819 ++buffer;
820 --buffer_length;
821
822 switch_port:
823 if (buffer_length <= 0)
824 break;
825 if (buffer[0] < 0x80) {
826 ep->current_port = (buffer[0] - 1) & 15;
827 ++buffer;
828 --buffer_length;
829 }
830 ep->seen_f5 = 0;
831 }
832}
833
Clemens Ladisched4affa2009-07-13 13:43:59 +0200834static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
835 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 int port0 = ep->current_port;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200838 uint8_t* buf = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int buf_free = ep->max_transfer;
840 int length, i;
841
842 for (i = 0; i < 0x10; ++i) {
843 /* round-robin, starting at the last current port */
844 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100845 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if (!port->active)
848 continue;
849 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
850 port->active = 0;
851 continue;
852 }
853
854 if (portnum != ep->current_port) {
855 if (buf_free < 2)
856 break;
857 ep->current_port = portnum;
858 buf[0] = 0xf5;
859 buf[1] = (portnum + 1) & 15;
860 buf += 2;
861 buf_free -= 2;
862 }
863
864 if (buf_free < 1)
865 break;
866 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
867 if (length > 0) {
868 buf += length;
869 buf_free -= length;
870 if (buf_free < 1)
871 break;
872 }
873 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200874 if (buf_free < ep->max_transfer && buf_free > 0) {
875 *buf = 0xff;
876 --buf_free;
877 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200878 urb->transfer_buffer_length = ep->max_transfer - buf_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
882 .input = snd_usbmidi_emagic_input,
883 .output = snd_usbmidi_emagic_output,
884 .init_out_endpoint = snd_usbmidi_emagic_init_out,
885 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
886};
887
888
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200889static void update_roland_altsetting(struct snd_usb_midi* umidi)
890{
891 struct usb_interface *intf;
892 struct usb_host_interface *hostif;
893 struct usb_interface_descriptor *intfd;
894 int is_light_load;
895
896 intf = umidi->iface;
897 is_light_load = intf->cur_altsetting != intf->altsetting;
898 if (umidi->roland_load_ctl->private_value == is_light_load)
899 return;
900 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
901 intfd = get_iface_desc(hostif);
902 snd_usbmidi_input_stop(&umidi->list);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100903 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200904 intfd->bAlternateSetting);
905 snd_usbmidi_input_start(&umidi->list);
906}
907
908static void substream_open(struct snd_rawmidi_substream *substream, int open)
909{
910 struct snd_usb_midi* umidi = substream->rmidi->private_data;
911 struct snd_kcontrol *ctl;
912
913 mutex_lock(&umidi->mutex);
914 if (open) {
915 if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
916 ctl = umidi->roland_load_ctl;
917 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100918 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200919 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
920 update_roland_altsetting(umidi);
921 }
922 } else {
923 if (--umidi->opened == 0 && umidi->roland_load_ctl) {
924 ctl = umidi->roland_load_ctl;
925 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100926 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200927 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
928 }
929 }
930 mutex_unlock(&umidi->mutex);
931}
932
Takashi Iwai86e07d32005-11-17 15:08:02 +0100933static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100935 struct snd_usb_midi* umidi = substream->rmidi->private_data;
936 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int i, j;
938
939 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
940 if (umidi->endpoints[i].out)
941 for (j = 0; j < 0x10; ++j)
942 if (umidi->endpoints[i].out->ports[j].substream == substream) {
943 port = &umidi->endpoints[i].out->ports[j];
944 break;
945 }
946 if (!port) {
947 snd_BUG();
948 return -ENXIO;
949 }
950 substream->runtime->private_data = port;
951 port->state = STATE_UNKNOWN;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200952 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return 0;
954}
955
Takashi Iwai86e07d32005-11-17 15:08:02 +0100956static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200958 substream_open(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
960}
961
Takashi Iwai86e07d32005-11-17 15:08:02 +0100962static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100964 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 port->active = up;
967 if (up) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100968 if (port->ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* gobble up remaining bytes to prevent wait in
970 * snd_rawmidi_drain_output */
971 while (!snd_rawmidi_transmit_empty(substream))
972 snd_rawmidi_transmit_ack(substream, 1);
973 return;
974 }
Takashi Iwai1f041282008-12-18 12:17:55 +0100975 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977}
978
Clemens Ladischa65dd992009-07-13 13:48:36 +0200979static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
980{
981 struct usbmidi_out_port* port = substream->runtime->private_data;
982 struct snd_usb_midi_out_endpoint *ep = port->ep;
983 unsigned int drain_urbs;
984 DEFINE_WAIT(wait);
985 long timeout = msecs_to_jiffies(50);
986
987 /*
988 * The substream buffer is empty, but some data might still be in the
989 * currently active URBs, so we have to wait for those to complete.
990 */
991 spin_lock_irq(&ep->buffer_lock);
992 drain_urbs = ep->active_urbs;
993 if (drain_urbs) {
994 ep->drain_urbs |= drain_urbs;
995 do {
996 prepare_to_wait(&ep->drain_wait, &wait,
997 TASK_UNINTERRUPTIBLE);
998 spin_unlock_irq(&ep->buffer_lock);
999 timeout = schedule_timeout(timeout);
1000 spin_lock_irq(&ep->buffer_lock);
1001 drain_urbs &= ep->drain_urbs;
1002 } while (drain_urbs && timeout);
1003 finish_wait(&ep->drain_wait, &wait);
1004 }
1005 spin_unlock_irq(&ep->buffer_lock);
1006}
1007
Takashi Iwai86e07d32005-11-17 15:08:02 +01001008static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001010 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return 0;
1012}
1013
Takashi Iwai86e07d32005-11-17 15:08:02 +01001014static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001016 substream_open(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
1018}
1019
Takashi Iwai86e07d32005-11-17 15:08:02 +01001020static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001022 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (up)
1025 set_bit(substream->number, &umidi->input_triggered);
1026 else
1027 clear_bit(substream->number, &umidi->input_triggered);
1028}
1029
Takashi Iwai86e07d32005-11-17 15:08:02 +01001030static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 .open = snd_usbmidi_output_open,
1032 .close = snd_usbmidi_output_close,
1033 .trigger = snd_usbmidi_output_trigger,
Clemens Ladischa65dd992009-07-13 13:48:36 +02001034 .drain = snd_usbmidi_output_drain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035};
1036
Takashi Iwai86e07d32005-11-17 15:08:02 +01001037static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 .open = snd_usbmidi_input_open,
1039 .close = snd_usbmidi_input_close,
1040 .trigger = snd_usbmidi_input_trigger
1041};
1042
Clemens Ladisched4affa2009-07-13 13:43:59 +02001043static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1044 unsigned int buffer_length)
1045{
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001046 usb_buffer_free(umidi->dev, buffer_length,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001047 urb->transfer_buffer, urb->transfer_dma);
1048 usb_free_urb(urb);
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
1052 * Frees an input endpoint.
1053 * May be called when ep hasn't been initialized completely.
1054 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001055static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001057 unsigned int i;
1058
Clemens Ladisched4affa2009-07-13 13:43:59 +02001059 for (i = 0; i < INPUT_URBS; ++i)
1060 if (ep->urbs[i])
1061 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1062 ep->urbs[i]->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 kfree(ep);
1064}
1065
1066/*
1067 * Creates an input endpoint.
1068 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001069static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1070 struct snd_usb_midi_endpoint_info* ep_info,
1071 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001073 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 void* buffer;
1075 unsigned int pipe;
1076 int length;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001077 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001080 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!ep)
1082 return -ENOMEM;
1083 ep->umidi = umidi;
1084
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001085 for (i = 0; i < INPUT_URBS; ++i) {
1086 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1087 if (!ep->urbs[i]) {
1088 snd_usbmidi_in_endpoint_delete(ep);
1089 return -ENOMEM;
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001093 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001095 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1096 length = usb_maxpacket(umidi->dev, pipe, 0);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001097 for (i = 0; i < INPUT_URBS; ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001098 buffer = usb_buffer_alloc(umidi->dev, length, GFP_KERNEL,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001099 &ep->urbs[i]->transfer_dma);
1100 if (!buffer) {
1101 snd_usbmidi_in_endpoint_delete(ep);
1102 return -ENOMEM;
1103 }
1104 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001105 usb_fill_int_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001106 pipe, buffer, length,
1107 snd_usbmidi_in_urb_complete,
1108 ep, ep_info->in_interval);
1109 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001110 usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001111 pipe, buffer, length,
1112 snd_usbmidi_in_urb_complete, ep);
1113 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 rep->in = ep;
1117 return 0;
1118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120/*
1121 * Frees an output endpoint.
1122 * May be called when ep hasn't been initialized completely.
1123 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001124static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Clemens Ladisched4affa2009-07-13 13:43:59 +02001126 unsigned int i;
1127
1128 for (i = 0; i < OUTPUT_URBS; ++i)
1129 if (ep->urbs[i].urb)
1130 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1131 ep->max_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 kfree(ep);
1133}
1134
1135/*
1136 * Creates an output endpoint, and initializes output ports.
1137 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001138static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1139 struct snd_usb_midi_endpoint_info* ep_info,
1140 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001142 struct snd_usb_midi_out_endpoint* ep;
Clemens Ladisched4affa2009-07-13 13:43:59 +02001143 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 unsigned int pipe;
1145 void* buffer;
1146
1147 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001148 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (!ep)
1150 return -ENOMEM;
1151 ep->umidi = umidi;
1152
Clemens Ladisched4affa2009-07-13 13:43:59 +02001153 for (i = 0; i < OUTPUT_URBS; ++i) {
1154 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1155 if (!ep->urbs[i].urb) {
1156 snd_usbmidi_out_endpoint_delete(ep);
1157 return -ENOMEM;
1158 }
1159 ep->urbs[i].ep = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001161 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001162 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001163 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001164 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001165 switch (umidi->usb_id) {
1166 default:
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001167 ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001168 break;
1169 /*
1170 * Various chips declare a packet size larger than 4 bytes, but
1171 * do not actually work with larger packets:
1172 */
1173 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1174 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1175 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1176 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1177 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1178 ep->max_transfer = 4;
1179 break;
1180 }
Clemens Ladisched4affa2009-07-13 13:43:59 +02001181 for (i = 0; i < OUTPUT_URBS; ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001182 buffer = usb_buffer_alloc(umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001183 ep->max_transfer, GFP_KERNEL,
1184 &ep->urbs[i].urb->transfer_dma);
1185 if (!buffer) {
1186 snd_usbmidi_out_endpoint_delete(ep);
1187 return -ENOMEM;
1188 }
1189 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001190 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001191 pipe, buffer, ep->max_transfer,
1192 snd_usbmidi_out_urb_complete,
1193 &ep->urbs[i], ep_info->out_interval);
1194 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001195 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001196 pipe, buffer, ep->max_transfer,
1197 snd_usbmidi_out_urb_complete,
1198 &ep->urbs[i]);
1199 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 spin_lock_init(&ep->buffer_lock);
1203 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
Clemens Ladischa65dd992009-07-13 13:48:36 +02001204 init_waitqueue_head(&ep->drain_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 for (i = 0; i < 0x10; ++i)
1207 if (ep_info->out_cables & (1 << i)) {
1208 ep->ports[i].ep = ep;
1209 ep->ports[i].cable = i << 4;
1210 }
1211
1212 if (umidi->usb_protocol_ops->init_out_endpoint)
1213 umidi->usb_protocol_ops->init_out_endpoint(ep);
1214
1215 rep->out = ep;
1216 return 0;
1217}
1218
1219/*
1220 * Frees everything.
1221 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001222static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 int i;
1225
1226 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001227 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (ep->out)
1229 snd_usbmidi_out_endpoint_delete(ep->out);
1230 if (ep->in)
1231 snd_usbmidi_in_endpoint_delete(ep->in);
1232 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001233 mutex_destroy(&umidi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 kfree(umidi);
1235}
1236
1237/*
1238 * Unlinks all URBs (must be done before the usb_device is deleted).
1239 */
Clemens Ladischee733392005-04-25 10:34:13 +02001240void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001242 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001243 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Takashi Iwai86e07d32005-11-17 15:08:02 +01001245 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001246 /*
1247 * an URB's completion handler may start the timer and
1248 * a timer may submit an URB. To reliably break the cycle
1249 * a flag under lock must be used
1250 */
1251 spin_lock_irq(&umidi->disc_lock);
1252 umidi->disconnected = 1;
1253 spin_unlock_irq(&umidi->disc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001255 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001256 if (ep->out)
1257 tasklet_kill(&ep->out->tasklet);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001258 if (ep->out) {
1259 for (j = 0; j < OUTPUT_URBS; ++j)
1260 usb_kill_urb(ep->out->urbs[j].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (umidi->usb_protocol_ops->finish_out_endpoint)
1262 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1263 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001264 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001265 for (j = 0; j < INPUT_URBS; ++j)
1266 usb_kill_urb(ep->in->urbs[j]);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001267 /* free endpoints here; later call can result in Oops */
1268 if (ep->out) {
1269 snd_usbmidi_out_endpoint_delete(ep->out);
1270 ep->out = NULL;
1271 }
1272 if (ep->in) {
1273 snd_usbmidi_in_endpoint_delete(ep->in);
1274 ep->in = NULL;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001277 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Takashi Iwai86e07d32005-11-17 15:08:02 +01001280static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001282 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 snd_usbmidi_free(umidi);
1284}
1285
Takashi Iwai86e07d32005-11-17 15:08:02 +01001286static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int stream, int number)
1288{
1289 struct list_head* list;
1290
1291 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001292 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (substream->number == number)
1294 return substream;
1295 }
1296 return NULL;
1297}
1298
1299/*
1300 * This list specifies names for ports that do not fit into the standard
1301 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1302 * such as internal control or synthesizer ports.
1303 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001304static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001305 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001306 short int port;
1307 short int voices;
1308 const char *name;
1309 unsigned int seq_flags;
1310} snd_usbmidi_port_info[] = {
1311#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1312 { .id = USB_ID(vendor, product), \
1313 .port = num, .voices = voices_, \
1314 .name = name_, .seq_flags = flags }
1315#define EXTERNAL_PORT(vendor, product, num, name) \
1316 PORT_INFO(vendor, product, num, name, 0, \
1317 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1318 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1319 SNDRV_SEQ_PORT_TYPE_PORT)
1320#define CONTROL_PORT(vendor, product, num, name) \
1321 PORT_INFO(vendor, product, num, name, 0, \
1322 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1323 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1324#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1325 PORT_INFO(vendor, product, num, name, voices, \
1326 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1327 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1328 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1329 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1330 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1331 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1332 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1333#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1334 PORT_INFO(vendor, product, num, name, voices, \
1335 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1336 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1337 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1338 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1339 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1340 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1341 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1342 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001344 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001346 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1347 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1348 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1349 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1350 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1351 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001353 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1354 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001356 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1357 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1358 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001360 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1361 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1362 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001364 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1365 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1366 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001368 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001370 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1371 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1372 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1373 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001375 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001377 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1378 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1379 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001381 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1382 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1383 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1384 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001386 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1387 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001389 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1390 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1391 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001393 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1394 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1395 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001397 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1398 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001400 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1401 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001403 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1404 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1405 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001407 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1408 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1409 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001410 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001411 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001413 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1414 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001416 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1417 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001419 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1420 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1421 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422};
1423
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001424static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1425{
1426 int i;
1427
1428 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001429 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001430 snd_usbmidi_port_info[i].port == number)
1431 return &snd_usbmidi_port_info[i];
1432 }
1433 return NULL;
1434}
1435
1436static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1437 struct snd_seq_port_info *seq_port_info)
1438{
1439 struct snd_usb_midi *umidi = rmidi->private_data;
1440 struct port_info *port_info;
1441
1442 /* TODO: read port flags from descriptors */
1443 port_info = find_port_info(umidi, number);
1444 if (port_info) {
1445 seq_port_info->type = port_info->seq_flags;
1446 seq_port_info->midi_voices = port_info->voices;
1447 }
1448}
1449
Takashi Iwai86e07d32005-11-17 15:08:02 +01001450static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001452 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001454 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 const char *name_format;
1456
Takashi Iwai86e07d32005-11-17 15:08:02 +01001457 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (!substream) {
1459 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1460 return;
1461 }
1462
1463 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001464 port_info = find_port_info(umidi, number);
1465 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 snprintf(substream->name, sizeof(substream->name),
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001467 name_format, umidi->card->shortname, number + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 *rsubstream = substream;
1470}
1471
1472/*
1473 * Creates the endpoints and their ports.
1474 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001475static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1476 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 int i, j, err;
1479 int out_ports = 0, in_ports = 0;
1480
1481 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1482 if (endpoints[i].out_cables) {
1483 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1484 &umidi->endpoints[i]);
1485 if (err < 0)
1486 return err;
1487 }
1488 if (endpoints[i].in_cables) {
1489 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1490 &umidi->endpoints[i]);
1491 if (err < 0)
1492 return err;
1493 }
1494
1495 for (j = 0; j < 0x10; ++j) {
1496 if (endpoints[i].out_cables & (1 << j)) {
1497 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1498 &umidi->endpoints[i].out->ports[j].substream);
1499 ++out_ports;
1500 }
1501 if (endpoints[i].in_cables & (1 << j)) {
1502 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1503 &umidi->endpoints[i].in->ports[j].substream);
1504 ++in_ports;
1505 }
1506 }
1507 }
1508 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1509 out_ports, in_ports);
1510 return 0;
1511}
1512
1513/*
1514 * Returns MIDIStreaming device capabilities.
1515 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001516static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1517 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 struct usb_interface* intf;
1520 struct usb_host_interface *hostif;
1521 struct usb_interface_descriptor* intfd;
1522 struct usb_ms_header_descriptor* ms_header;
1523 struct usb_host_endpoint *hostep;
1524 struct usb_endpoint_descriptor* ep;
1525 struct usb_ms_endpoint_descriptor* ms_ep;
1526 int i, epidx;
1527
1528 intf = umidi->iface;
1529 if (!intf)
1530 return -ENXIO;
1531 hostif = &intf->altsetting[0];
1532 intfd = get_iface_desc(hostif);
1533 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1534 if (hostif->extralen >= 7 &&
1535 ms_header->bLength >= 7 &&
1536 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1537 ms_header->bDescriptorSubtype == HEADER)
1538 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1539 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1540 else
1541 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1542
1543 epidx = 0;
1544 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1545 hostep = &hostif->endpoint[i];
1546 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001547 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 continue;
1549 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1550 if (hostep->extralen < 4 ||
1551 ms_ep->bLength < 4 ||
1552 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1553 ms_ep->bDescriptorSubtype != MS_GENERAL)
1554 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001555 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (endpoints[epidx].out_ep) {
1557 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1558 snd_printk(KERN_WARNING "too many endpoints\n");
1559 break;
1560 }
1561 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001562 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1563 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001565 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001566 /*
1567 * Low speed bulk transfers don't exist, so
1568 * force interrupt transfers for devices like
1569 * ESI MIDI Mate that try to use them anyway.
1570 */
1571 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1573 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1574 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1575 } else {
1576 if (endpoints[epidx].in_ep) {
1577 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1578 snd_printk(KERN_WARNING "too many endpoints\n");
1579 break;
1580 }
1581 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001582 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1583 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001585 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001586 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1588 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1589 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1590 }
1591 }
1592 return 0;
1593}
1594
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001595static int roland_load_info(struct snd_kcontrol *kcontrol,
1596 struct snd_ctl_elem_info *info)
1597{
1598 static const char *const names[] = { "High Load", "Light Load" };
1599
1600 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1601 info->count = 1;
1602 info->value.enumerated.items = 2;
1603 if (info->value.enumerated.item > 1)
1604 info->value.enumerated.item = 1;
1605 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
1606 return 0;
1607}
1608
1609static int roland_load_get(struct snd_kcontrol *kcontrol,
1610 struct snd_ctl_elem_value *value)
1611{
1612 value->value.enumerated.item[0] = kcontrol->private_value;
1613 return 0;
1614}
1615
1616static int roland_load_put(struct snd_kcontrol *kcontrol,
1617 struct snd_ctl_elem_value *value)
1618{
1619 struct snd_usb_midi* umidi = kcontrol->private_data;
1620 int changed;
1621
1622 if (value->value.enumerated.item[0] > 1)
1623 return -EINVAL;
1624 mutex_lock(&umidi->mutex);
1625 changed = value->value.enumerated.item[0] != kcontrol->private_value;
1626 if (changed)
1627 kcontrol->private_value = value->value.enumerated.item[0];
1628 mutex_unlock(&umidi->mutex);
1629 return changed;
1630}
1631
1632static struct snd_kcontrol_new roland_load_ctl = {
1633 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1634 .name = "MIDI Input Mode",
1635 .info = roland_load_info,
1636 .get = roland_load_get,
1637 .put = roland_load_put,
1638 .private_value = 1,
1639};
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641/*
1642 * On Roland devices, use the second alternate setting to be able to use
1643 * the interrupt input endpoint.
1644 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001645static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 struct usb_interface* intf;
1648 struct usb_host_interface *hostif;
1649 struct usb_interface_descriptor* intfd;
1650
1651 intf = umidi->iface;
1652 if (!intf || intf->num_altsetting != 2)
1653 return;
1654
1655 hostif = &intf->altsetting[1];
1656 intfd = get_iface_desc(hostif);
1657 if (intfd->bNumEndpoints != 2 ||
1658 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1659 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1660 return;
1661
1662 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1663 intfd->bAlternateSetting);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001664 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 intfd->bAlternateSetting);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001666
1667 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001668 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001669 umidi->roland_load_ctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672/*
1673 * Try to find any usable endpoints in the interface.
1674 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001675static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1676 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 int max_endpoints)
1678{
1679 struct usb_interface* intf;
1680 struct usb_host_interface *hostif;
1681 struct usb_interface_descriptor* intfd;
1682 struct usb_endpoint_descriptor* epd;
1683 int i, out_eps = 0, in_eps = 0;
1684
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001685 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 snd_usbmidi_switch_roland_altsetting(umidi);
1687
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001688 if (endpoint[0].out_ep || endpoint[0].in_ep)
1689 return 0;
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 intf = umidi->iface;
1692 if (!intf || intf->num_altsetting < 1)
1693 return -ENOENT;
1694 hostif = intf->cur_altsetting;
1695 intfd = get_iface_desc(hostif);
1696
1697 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1698 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001699 if (!usb_endpoint_xfer_bulk(epd) &&
1700 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 continue;
1702 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001703 usb_endpoint_dir_out(epd)) {
1704 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1705 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 endpoint[out_eps].out_interval = epd->bInterval;
1707 ++out_eps;
1708 }
1709 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001710 usb_endpoint_dir_in(epd)) {
1711 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1712 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 endpoint[in_eps].in_interval = epd->bInterval;
1714 ++in_eps;
1715 }
1716 }
1717 return (out_eps || in_eps) ? 0 : -ENOENT;
1718}
1719
1720/*
1721 * Detects the endpoints for one-port-per-endpoint protocols.
1722 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001723static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1724 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 int err, i;
1727
1728 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1729 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1730 if (endpoints[i].out_ep)
1731 endpoints[i].out_cables = 0x0001;
1732 if (endpoints[i].in_ep)
1733 endpoints[i].in_cables = 0x0001;
1734 }
1735 return err;
1736}
1737
1738/*
1739 * Detects the endpoints and ports of Yamaha devices.
1740 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001741static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1742 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 struct usb_interface* intf;
1745 struct usb_host_interface *hostif;
1746 struct usb_interface_descriptor* intfd;
1747 uint8_t* cs_desc;
1748
1749 intf = umidi->iface;
1750 if (!intf)
1751 return -ENOENT;
1752 hostif = intf->altsetting;
1753 intfd = get_iface_desc(hostif);
1754 if (intfd->bNumEndpoints < 1)
1755 return -ENOENT;
1756
1757 /*
1758 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1759 * necessarily with any useful contents. So simply count 'em.
1760 */
1761 for (cs_desc = hostif->extra;
1762 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1763 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001764 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (cs_desc[2] == MIDI_IN_JACK)
1766 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1767 else if (cs_desc[2] == MIDI_OUT_JACK)
1768 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1769 }
1770 }
1771 if (!endpoint->in_cables && !endpoint->out_cables)
1772 return -ENOENT;
1773
1774 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1775}
1776
1777/*
1778 * Creates the endpoints and their ports for Midiman devices.
1779 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001780static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1781 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001783 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct usb_interface* intf;
1785 struct usb_host_interface *hostif;
1786 struct usb_interface_descriptor* intfd;
1787 struct usb_endpoint_descriptor* epd;
1788 int cable, err;
1789
1790 intf = umidi->iface;
1791 if (!intf)
1792 return -ENOENT;
1793 hostif = intf->altsetting;
1794 intfd = get_iface_desc(hostif);
1795 /*
1796 * The various MidiSport devices have more or less random endpoint
1797 * numbers, so we have to identify the endpoints by their index in
1798 * the descriptor array, like the driver for that other OS does.
1799 *
1800 * There is one interrupt input endpoint for all input ports, one
1801 * bulk output endpoint for even-numbered ports, and one for odd-
1802 * numbered ports. Both bulk output endpoints have corresponding
1803 * input bulk endpoints (at indices 1 and 3) which aren't used.
1804 */
1805 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1806 snd_printdd(KERN_ERR "not enough endpoints\n");
1807 return -ENOENT;
1808 }
1809
1810 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001811 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1813 return -ENXIO;
1814 }
1815 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001816 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1818 return -ENXIO;
1819 }
1820 if (endpoint->out_cables > 0x0001) {
1821 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001822 if (!usb_endpoint_dir_out(epd) ||
1823 !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1825 return -ENXIO;
1826 }
1827 }
1828
1829 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01001830 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 ep_info.out_cables = endpoint->out_cables & 0x5555;
1832 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1833 if (err < 0)
1834 return err;
1835
1836 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1837 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1838 ep_info.in_cables = endpoint->in_cables;
1839 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1840 if (err < 0)
1841 return err;
1842
1843 if (endpoint->out_cables > 0x0001) {
1844 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1845 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1846 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1847 if (err < 0)
1848 return err;
1849 }
1850
1851 for (cable = 0; cable < 0x10; ++cable) {
1852 if (endpoint->out_cables & (1 << cable))
1853 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1854 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1855 if (endpoint->in_cables & (1 << cable))
1856 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1857 &umidi->endpoints[0].in->ports[cable].substream);
1858 }
1859 return 0;
1860}
1861
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001862static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1863 .get_port_info = snd_usbmidi_get_port_info,
1864};
1865
Takashi Iwai86e07d32005-11-17 15:08:02 +01001866static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 int out_ports, int in_ports)
1868{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001869 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 int err;
1871
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001872 err = snd_rawmidi_new(umidi->card, "USB MIDI",
1873 umidi->next_midi_device++,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 out_ports, in_ports, &rmidi);
1875 if (err < 0)
1876 return err;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001877 strcpy(rmidi->name, umidi->card->shortname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1879 SNDRV_RAWMIDI_INFO_INPUT |
1880 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001881 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 rmidi->private_data = umidi;
1883 rmidi->private_free = snd_usbmidi_rawmidi_free;
1884 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1885 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1886
1887 umidi->rmidi = rmidi;
1888 return 0;
1889}
1890
1891/*
1892 * Temporarily stop input.
1893 */
1894void snd_usbmidi_input_stop(struct list_head* p)
1895{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001896 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001897 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Takashi Iwai86e07d32005-11-17 15:08:02 +01001899 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001901 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001903 for (j = 0; j < INPUT_URBS; ++j)
1904 usb_kill_urb(ep->in->urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906}
1907
Takashi Iwai86e07d32005-11-17 15:08:02 +01001908static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001910 unsigned int i;
1911
1912 if (!ep)
1913 return;
1914 for (i = 0; i < INPUT_URBS; ++i) {
1915 struct urb* urb = ep->urbs[i];
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001916 urb->dev = ep->umidi->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1918 }
1919}
1920
1921/*
1922 * Resume input after a call to snd_usbmidi_input_stop().
1923 */
1924void snd_usbmidi_input_start(struct list_head* p)
1925{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001926 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int i;
1928
Takashi Iwai86e07d32005-11-17 15:08:02 +01001929 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1931 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1932}
1933
1934/*
1935 * Creates and registers everything needed for a MIDI streaming interface.
1936 */
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001937int snd_usbmidi_create(struct snd_card *card,
1938 struct usb_interface* iface,
1939 struct list_head *midi_list,
1940 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001942 struct snd_usb_midi* umidi;
1943 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 int out_ports, in_ports;
1945 int i, err;
1946
Takashi Iwai561b2202005-09-09 14:22:34 +02001947 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (!umidi)
1949 return -ENOMEM;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001950 umidi->dev = interface_to_usbdev(iface);
1951 umidi->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 umidi->iface = iface;
1953 umidi->quirk = quirk;
1954 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001955 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001956 spin_lock_init(&umidi->disc_lock);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001957 mutex_init(&umidi->mutex);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001958 umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
1959 le16_to_cpu(umidi->dev->descriptor.idProduct));
Clemens Ladischc8846972005-08-02 15:26:52 +02001960 umidi->error_timer.function = snd_usbmidi_error_timer;
1961 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /* detect the endpoint(s) to use */
1964 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001965 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1966 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001968 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
Clemens Ladischd05cc10432007-05-07 09:28:53 +02001969 umidi->usb_protocol_ops =
1970 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001971 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02001972 case QUIRK_MIDI_US122L:
1973 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1974 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02001975 case QUIRK_MIDI_FIXED_ENDPOINT:
1976 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001977 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001978 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1979 break;
1980 case QUIRK_MIDI_YAMAHA:
1981 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1982 break;
1983 case QUIRK_MIDI_MIDIMAN:
1984 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1985 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001986 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001987 err = 0;
1988 break;
1989 case QUIRK_MIDI_NOVATION:
1990 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1991 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1992 break;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001993 case QUIRK_MIDI_FASTLANE:
Clemens Ladischd1bda042005-09-14 08:36:03 +02001994 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001995 /*
1996 * Interface 1 contains isochronous endpoints, but with the same
1997 * numbers as in interface 0. Since it is interface 1 that the
1998 * USB core has most recently seen, these descriptors are now
1999 * associated with the endpoint numbers. This will foul up our
2000 * attempts to submit bulk/interrupt URBs to the endpoints in
2001 * interface 0, so we have to make sure that the USB core looks
2002 * again at interface 0 by calling usb_set_interface() on it.
2003 */
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002004 usb_set_interface(umidi->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002005 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2006 break;
2007 case QUIRK_MIDI_EMAGIC:
2008 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2009 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002010 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002011 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2012 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01002013 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02002014 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002015 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2016 break;
2017 default:
2018 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2019 err = -ENXIO;
2020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 if (err < 0) {
2023 kfree(umidi);
2024 return err;
2025 }
2026
2027 /* create rawmidi device */
2028 out_ports = 0;
2029 in_ports = 0;
2030 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Akinobu Mitafbc54392009-11-20 14:56:52 +09002031 out_ports += hweight16(endpoints[i].out_cables);
2032 in_ports += hweight16(endpoints[i].in_cables);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2035 if (err < 0) {
2036 kfree(umidi);
2037 return err;
2038 }
2039
2040 /* create endpoint/port structures */
2041 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2042 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2043 else
2044 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2045 if (err < 0) {
2046 snd_usbmidi_free(umidi);
2047 return err;
2048 }
2049
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002050 list_add_tail(&umidi->list, midi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2053 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2054 return 0;
2055}
2056
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002057EXPORT_SYMBOL(snd_usbmidi_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058EXPORT_SYMBOL(snd_usbmidi_input_stop);
2059EXPORT_SYMBOL(snd_usbmidi_input_start);
2060EXPORT_SYMBOL(snd_usbmidi_disconnect);