blob: 2c59afd99611b363651d77afe0e0f2ef26be0ebb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
Clemens Ladisch96f61d92009-10-22 09:06:19 +02004 * Copyright (c) 2002-2009 Clemens Ladisch
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * All rights reserved.
6 *
7 * Based on the OSS usb-midi driver by NAGANO Daisuke,
8 * NetBSD's umidi driver by Takuya SHIOZAKI,
9 * the "USB Device Class Definition for MIDI Devices" by Roland
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed and/or modified under the
21 * terms of the GNU General Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at your option) any later
23 * version.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/kernel.h>
39#include <linux/types.h>
40#include <linux/bitops.h>
41#include <linux/interrupt.h>
42#include <linux/spinlock.h>
43#include <linux/string.h>
44#include <linux/init.h>
45#include <linux/slab.h>
Clemens Ladischc8846972005-08-02 15:26:52 +020046#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/usb.h>
Clemens Ladischa65dd992009-07-13 13:48:36 +020048#include <linux/wait.h>
Daniel Mackde48c7b2010-02-22 23:49:13 +010049#include <linux/usb/audio.h>
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <sound/core.h>
Clemens Ladisch96f61d92009-10-22 09:06:19 +020052#include <sound/control.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <sound/rawmidi.h>
Clemens Ladischa7b928a2006-05-02 16:22:12 +020054#include <sound/asequencer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include "usbaudio.h"
56
57
58/*
59 * define this to log all USB packets
60 */
61/* #define DUMP_PACKETS */
62
Clemens Ladischc8846972005-08-02 15:26:52 +020063/*
64 * how long to wait after some USB errors, so that khubd can disconnect() us
65 * without too many spurious errors
66 */
67#define ERROR_DELAY_JIFFIES (HZ / 10)
68
Clemens Ladisched4affa2009-07-13 13:43:59 +020069#define OUTPUT_URBS 7
Clemens Ladisch4773d1f2009-07-13 13:40:36 +020070#define INPUT_URBS 7
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
74MODULE_DESCRIPTION("USB Audio/MIDI helper module");
75MODULE_LICENSE("Dual BSD/GPL");
76
77
78struct usb_ms_header_descriptor {
79 __u8 bLength;
80 __u8 bDescriptorType;
81 __u8 bDescriptorSubtype;
82 __u8 bcdMSC[2];
83 __le16 wTotalLength;
84} __attribute__ ((packed));
85
86struct usb_ms_endpoint_descriptor {
87 __u8 bLength;
88 __u8 bDescriptorType;
89 __u8 bDescriptorSubtype;
90 __u8 bNumEmbMIDIJack;
91 __u8 baAssocJackID[0];
92} __attribute__ ((packed));
93
Takashi Iwai86e07d32005-11-17 15:08:02 +010094struct snd_usb_midi_in_endpoint;
95struct snd_usb_midi_out_endpoint;
96struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +010099 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200100 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100102 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
103 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106struct snd_usb_midi {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100107 struct usb_device *dev;
108 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100110 const struct snd_usb_audio_quirk *quirk;
111 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct usb_protocol_ops* usb_protocol_ops;
113 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200114 struct timer_list error_timer;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100115 spinlock_t disc_lock;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200116 struct mutex mutex;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100117 u32 usb_id;
118 int next_midi_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100121 struct snd_usb_midi_out_endpoint *out;
122 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } endpoints[MIDI_MAX_ENDPOINTS];
124 unsigned long input_triggered;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200125 unsigned int opened;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100126 unsigned char disconnected;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200127
128 struct snd_kcontrol *roland_load_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100132 struct snd_usb_midi* umidi;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200133 struct out_urb_context {
134 struct urb *urb;
135 struct snd_usb_midi_out_endpoint *ep;
136 } urbs[OUTPUT_URBS];
137 unsigned int active_urbs;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200138 unsigned int drain_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int max_transfer; /* size of urb buffer */
140 struct tasklet_struct tasklet;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200141 unsigned int next_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spinlock_t buffer_lock;
143
144 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100145 struct snd_usb_midi_out_endpoint* ep;
146 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int active;
148 uint8_t cable; /* cable number << 4 */
149 uint8_t state;
150#define STATE_UNKNOWN 0
151#define STATE_1PARAM 1
152#define STATE_2PARAM_1 2
153#define STATE_2PARAM_2 3
154#define STATE_SYSEX_0 4
155#define STATE_SYSEX_1 5
156#define STATE_SYSEX_2 6
157 uint8_t data[2];
158 } ports[0x10];
159 int current_port;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200160
161 wait_queue_head_t drain_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
164struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100165 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200166 struct urb* urbs[INPUT_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100168 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200169 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200171 u8 seen_f5;
172 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 int current_port;
174};
175
Takashi Iwai86e07d32005-11-17 15:08:02 +0100176static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178static const uint8_t snd_usbmidi_cin_length[] = {
179 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
180};
181
182/*
183 * Submits the URB, with error handling.
184 */
Al Viro55016f12005-10-21 03:21:58 -0400185static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 int err = usb_submit_urb(urb, flags);
188 if (err < 0 && err != -ENODEV)
189 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
190 return err;
191}
192
193/*
194 * Error handling for URB completion functions.
195 */
196static int snd_usbmidi_urb_error(int status)
197{
Clemens Ladischc8846972005-08-02 15:26:52 +0200198 switch (status) {
199 /* manually unlinked, or device gone */
200 case -ENOENT:
201 case -ECONNRESET:
202 case -ESHUTDOWN:
203 case -ENODEV:
204 return -ENODEV;
205 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700206 case -EPROTO:
207 case -ETIME:
208 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200209 return -EIO;
210 default:
211 snd_printk(KERN_ERR "urb status %d\n", status);
212 return 0; /* continue */
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/*
217 * Receives a chunk of MIDI data.
218 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100219static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 uint8_t* data, int length)
221{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100222 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (!port->substream) {
225 snd_printd("unexpected port %d!\n", portidx);
226 return;
227 }
228 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
229 return;
230 snd_rawmidi_receive(port->substream, data, length);
231}
232
233#ifdef DUMP_PACKETS
234static void dump_urb(const char *type, const u8 *data, int length)
235{
236 snd_printk(KERN_DEBUG "%s packet: [", type);
237 for (; length > 0; ++data, --length)
238 printk(" %02x", *data);
239 printk(" ]\n");
240}
241#else
242#define dump_urb(type, data, length) /* nothing */
243#endif
244
245/*
246 * Processes the data read from the device.
247 */
David Howells7d12e782006-10-05 14:55:46 +0100248static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100250 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (urb->status == 0) {
253 dump_urb("received", urb->transfer_buffer, urb->actual_length);
254 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
255 urb->actual_length);
256 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200257 int err = snd_usbmidi_urb_error(urb->status);
258 if (err < 0) {
259 if (err != -ENODEV) {
260 ep->error_resubmit = 1;
261 mod_timer(&ep->umidi->error_timer,
262 jiffies + ERROR_DELAY_JIFFIES);
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100268 urb->dev = ep->umidi->dev;
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200269 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
David Howells7d12e782006-10-05 14:55:46 +0100272static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200274 struct out_urb_context *context = urb->context;
275 struct snd_usb_midi_out_endpoint* ep = context->ep;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200276 unsigned int urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 spin_lock(&ep->buffer_lock);
Clemens Ladischa65dd992009-07-13 13:48:36 +0200279 urb_index = context - ep->urbs;
280 ep->active_urbs &= ~(1 << urb_index);
281 if (unlikely(ep->drain_urbs)) {
282 ep->drain_urbs &= ~(1 << urb_index);
283 wake_up(&ep->drain_wait);
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 spin_unlock(&ep->buffer_lock);
286 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200287 int err = snd_usbmidi_urb_error(urb->status);
288 if (err < 0) {
289 if (err != -ENODEV)
290 mod_timer(&ep->umidi->error_timer,
291 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 snd_usbmidi_do_output(ep);
296}
297
298/*
299 * This is called when some data should be transferred to the device
300 * (from one or more substreams).
301 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100302static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200304 unsigned int urb_index;
305 struct urb* urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned long flags;
307
308 spin_lock_irqsave(&ep->buffer_lock, flags);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100309 if (ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 spin_unlock_irqrestore(&ep->buffer_lock, flags);
311 return;
312 }
313
Clemens Ladischa65dd992009-07-13 13:48:36 +0200314 urb_index = ep->next_urb;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200315 for (;;) {
Clemens Ladischa65dd992009-07-13 13:48:36 +0200316 if (!(ep->active_urbs & (1 << urb_index))) {
317 urb = ep->urbs[urb_index].urb;
318 urb->transfer_buffer_length = 0;
319 ep->umidi->usb_protocol_ops->output(ep, urb);
320 if (urb->transfer_buffer_length == 0)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Clemens Ladischa65dd992009-07-13 13:48:36 +0200323 dump_urb("sending", urb->transfer_buffer,
324 urb->transfer_buffer_length);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100325 urb->dev = ep->umidi->dev;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200326 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
327 break;
328 ep->active_urbs |= 1 << urb_index;
329 }
330 if (++urb_index >= OUTPUT_URBS)
331 urb_index = 0;
332 if (urb_index == ep->next_urb)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200333 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Clemens Ladischa65dd992009-07-13 13:48:36 +0200335 ep->next_urb = urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_unlock_irqrestore(&ep->buffer_lock, flags);
337}
338
339static void snd_usbmidi_out_tasklet(unsigned long data)
340{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100341 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 snd_usbmidi_do_output(ep);
344}
345
Clemens Ladischc8846972005-08-02 15:26:52 +0200346/* called after transfers had been interrupted due to some USB error */
347static void snd_usbmidi_error_timer(unsigned long data)
348{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100349 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200350 unsigned int i, j;
Clemens Ladischc8846972005-08-02 15:26:52 +0200351
Takashi Iwaic0792e02008-02-22 18:34:44 +0100352 spin_lock(&umidi->disc_lock);
353 if (umidi->disconnected) {
354 spin_unlock(&umidi->disc_lock);
355 return;
356 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200357 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100358 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200359 if (in && in->error_resubmit) {
360 in->error_resubmit = 0;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200361 for (j = 0; j < INPUT_URBS; ++j) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100362 in->urbs[j]->dev = umidi->dev;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200363 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
364 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200365 }
366 if (umidi->endpoints[i].out)
367 snd_usbmidi_do_output(umidi->endpoints[i].out);
368 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100369 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100373static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 const void *data, int len)
375{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200376 int err = 0;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700377 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (!buf)
379 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 dump_urb("sending", buf, len);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200381 if (ep->urbs[0].urb)
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100382 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200383 buf, len, NULL, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 kfree(buf);
385 return err;
386}
387
388/*
389 * Standard USB MIDI protocol: see the spec.
390 * Midiman protocol: like the standard protocol, but the control byte is the
391 * fourth byte in each packet, and uses length instead of CIN.
392 */
393
Takashi Iwai86e07d32005-11-17 15:08:02 +0100394static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 uint8_t* buffer, int buffer_length)
396{
397 int i;
398
399 for (i = 0; i + 3 < buffer_length; i += 4)
400 if (buffer[i] != 0) {
401 int cable = buffer[i] >> 4;
402 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
403 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
404 }
405}
406
Takashi Iwai86e07d32005-11-17 15:08:02 +0100407static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 uint8_t* buffer, int buffer_length)
409{
410 int i;
411
412 for (i = 0; i + 3 < buffer_length; i += 4)
413 if (buffer[i + 3] != 0) {
414 int port = buffer[i + 3] >> 4;
415 int length = buffer[i + 3] & 3;
416 snd_usbmidi_input_data(ep, port, &buffer[i], length);
417 }
418}
419
420/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200421 * Buggy M-Audio device: running status on input results in a packet that has
422 * the data bytes but not the status byte and that is marked with CIN 4.
423 */
424static void snd_usbmidi_maudio_broken_running_status_input(
425 struct snd_usb_midi_in_endpoint* ep,
426 uint8_t* buffer, int buffer_length)
427{
428 int i;
429
430 for (i = 0; i + 3 < buffer_length; i += 4)
431 if (buffer[i] != 0) {
432 int cable = buffer[i] >> 4;
433 u8 cin = buffer[i] & 0x0f;
434 struct usbmidi_in_port *port = &ep->ports[cable];
435 int length;
436
437 length = snd_usbmidi_cin_length[cin];
438 if (cin == 0xf && buffer[i + 1] >= 0xf8)
439 ; /* realtime msg: no running status change */
440 else if (cin >= 0x8 && cin <= 0xe)
441 /* channel msg */
442 port->running_status_length = length - 1;
443 else if (cin == 0x4 &&
444 port->running_status_length != 0 &&
445 buffer[i + 1] < 0x80)
446 /* CIN 4 that is not a SysEx */
447 length = port->running_status_length;
448 else
449 /*
450 * All other msgs cannot begin running status.
451 * (A channel msg sent as two or three CIN 0xF
452 * packets could in theory, but this device
453 * doesn't use this format.)
454 */
455 port->running_status_length = 0;
456 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
457 }
458}
459
460/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200461 * CME protocol: like the standard protocol, but SysEx commands are sent as a
462 * single USB packet preceded by a 0x0F byte.
463 */
464static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
465 uint8_t *buffer, int buffer_length)
466{
467 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
468 snd_usbmidi_standard_input(ep, buffer, buffer_length);
469 else
470 snd_usbmidi_input_data(ep, buffer[0] >> 4,
471 &buffer[1], buffer_length - 1);
472}
473
474/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * Adds one USB MIDI packet to the output buffer.
476 */
477static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
478 uint8_t p1, uint8_t p2, uint8_t p3)
479{
480
481 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
482 buf[0] = p0;
483 buf[1] = p1;
484 buf[2] = p2;
485 buf[3] = p3;
486 urb->transfer_buffer_length += 4;
487}
488
489/*
490 * Adds one Midiman packet to the output buffer.
491 */
492static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
493 uint8_t p1, uint8_t p2, uint8_t p3)
494{
495
496 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
497 buf[0] = p1;
498 buf[1] = p2;
499 buf[2] = p3;
500 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
501 urb->transfer_buffer_length += 4;
502}
503
504/*
505 * Converts MIDI commands to USB MIDI packets.
506 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100507static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 uint8_t b, struct urb* urb)
509{
510 uint8_t p0 = port->cable;
511 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
512 port->ep->umidi->usb_protocol_ops->output_packet;
513
514 if (b >= 0xf8) {
515 output_packet(urb, p0 | 0x0f, b, 0, 0);
516 } else if (b >= 0xf0) {
517 switch (b) {
518 case 0xf0:
519 port->data[0] = b;
520 port->state = STATE_SYSEX_1;
521 break;
522 case 0xf1:
523 case 0xf3:
524 port->data[0] = b;
525 port->state = STATE_1PARAM;
526 break;
527 case 0xf2:
528 port->data[0] = b;
529 port->state = STATE_2PARAM_1;
530 break;
531 case 0xf4:
532 case 0xf5:
533 port->state = STATE_UNKNOWN;
534 break;
535 case 0xf6:
536 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
537 port->state = STATE_UNKNOWN;
538 break;
539 case 0xf7:
540 switch (port->state) {
541 case STATE_SYSEX_0:
542 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
543 break;
544 case STATE_SYSEX_1:
545 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
546 break;
547 case STATE_SYSEX_2:
548 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
549 break;
550 }
551 port->state = STATE_UNKNOWN;
552 break;
553 }
554 } else if (b >= 0x80) {
555 port->data[0] = b;
556 if (b >= 0xc0 && b <= 0xdf)
557 port->state = STATE_1PARAM;
558 else
559 port->state = STATE_2PARAM_1;
560 } else { /* b < 0x80 */
561 switch (port->state) {
562 case STATE_1PARAM:
563 if (port->data[0] < 0xf0) {
564 p0 |= port->data[0] >> 4;
565 } else {
566 p0 |= 0x02;
567 port->state = STATE_UNKNOWN;
568 }
569 output_packet(urb, p0, port->data[0], b, 0);
570 break;
571 case STATE_2PARAM_1:
572 port->data[1] = b;
573 port->state = STATE_2PARAM_2;
574 break;
575 case STATE_2PARAM_2:
576 if (port->data[0] < 0xf0) {
577 p0 |= port->data[0] >> 4;
578 port->state = STATE_2PARAM_1;
579 } else {
580 p0 |= 0x03;
581 port->state = STATE_UNKNOWN;
582 }
583 output_packet(urb, p0, port->data[0], port->data[1], b);
584 break;
585 case STATE_SYSEX_0:
586 port->data[0] = b;
587 port->state = STATE_SYSEX_1;
588 break;
589 case STATE_SYSEX_1:
590 port->data[1] = b;
591 port->state = STATE_SYSEX_2;
592 break;
593 case STATE_SYSEX_2:
594 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
595 port->state = STATE_SYSEX_0;
596 break;
597 }
598 }
599}
600
Clemens Ladisched4affa2009-07-13 13:43:59 +0200601static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
602 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int p;
605
606 /* FIXME: lower-numbered ports can starve higher-numbered ports */
607 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100608 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!port->active)
610 continue;
611 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
612 uint8_t b;
613 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
614 port->active = 0;
615 break;
616 }
617 snd_usbmidi_transmit_byte(port, b, urb);
618 }
619 }
620}
621
622static struct usb_protocol_ops snd_usbmidi_standard_ops = {
623 .input = snd_usbmidi_standard_input,
624 .output = snd_usbmidi_standard_output,
625 .output_packet = snd_usbmidi_output_standard_packet,
626};
627
628static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
629 .input = snd_usbmidi_midiman_input,
630 .output = snd_usbmidi_standard_output,
631 .output_packet = snd_usbmidi_output_midiman_packet,
632};
633
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200634static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
635 .input = snd_usbmidi_maudio_broken_running_status_input,
636 .output = snd_usbmidi_standard_output,
637 .output_packet = snd_usbmidi_output_standard_packet,
638};
639
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200640static struct usb_protocol_ops snd_usbmidi_cme_ops = {
641 .input = snd_usbmidi_cme_input,
642 .output = snd_usbmidi_standard_output,
643 .output_packet = snd_usbmidi_output_standard_packet,
644};
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/*
647 * Novation USB MIDI protocol: number of data bytes is in the first byte
648 * (when receiving) (+1!) or in the second byte (when sending); data begins
649 * at the third byte.
650 */
651
Takashi Iwai86e07d32005-11-17 15:08:02 +0100652static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 uint8_t* buffer, int buffer_length)
654{
655 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
656 return;
657 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
658}
659
Clemens Ladisched4affa2009-07-13 13:43:59 +0200660static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
661 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 uint8_t* transfer_buffer;
664 int count;
665
666 if (!ep->ports[0].active)
667 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200668 transfer_buffer = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 count = snd_rawmidi_transmit(ep->ports[0].substream,
670 &transfer_buffer[2],
671 ep->max_transfer - 2);
672 if (count < 1) {
673 ep->ports[0].active = 0;
674 return;
675 }
676 transfer_buffer[0] = 0;
677 transfer_buffer[1] = count;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200678 urb->transfer_buffer_length = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681static struct usb_protocol_ops snd_usbmidi_novation_ops = {
682 .input = snd_usbmidi_novation_input,
683 .output = snd_usbmidi_novation_output,
684};
685
686/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200687 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
689
Takashi Iwai86e07d32005-11-17 15:08:02 +0100690static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200691 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
694}
695
Clemens Ladisched4affa2009-07-13 13:43:59 +0200696static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
697 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 int count;
700
701 if (!ep->ports[0].active)
702 return;
703 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200704 urb->transfer_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 ep->max_transfer);
706 if (count < 1) {
707 ep->ports[0].active = 0;
708 return;
709 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200710 urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200713static struct usb_protocol_ops snd_usbmidi_raw_ops = {
714 .input = snd_usbmidi_raw_input,
715 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716};
717
Karsten Wiese030a07e2008-07-30 15:13:29 +0200718static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
719 uint8_t *buffer, int buffer_length)
720{
721 if (buffer_length != 9)
722 return;
723 buffer_length = 8;
724 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
725 buffer_length--;
726 if (buffer_length)
727 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
728}
729
Clemens Ladisched4affa2009-07-13 13:43:59 +0200730static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
731 struct urb *urb)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200732{
733 int count;
734
735 if (!ep->ports[0].active)
736 return;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100737 count = snd_usb_get_speed(ep->umidi->dev) == USB_SPEED_HIGH ? 1 : 2;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200738 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200739 urb->transfer_buffer,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200740 count);
741 if (count < 1) {
742 ep->ports[0].active = 0;
743 return;
744 }
745
Clemens Ladisched4affa2009-07-13 13:43:59 +0200746 memset(urb->transfer_buffer + count, 0xFD, 9 - count);
747 urb->transfer_buffer_length = count;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200748}
749
750static struct usb_protocol_ops snd_usbmidi_122l_ops = {
751 .input = snd_usbmidi_us122l_input,
752 .output = snd_usbmidi_us122l_output,
753};
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/*
756 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
757 */
758
Takashi Iwai86e07d32005-11-17 15:08:02 +0100759static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 static const u8 init_data[] = {
762 /* initialization magic: "get version" */
763 0xf0,
764 0x00, 0x20, 0x31, /* Emagic */
765 0x64, /* Unitor8 */
766 0x0b, /* version number request */
767 0x00, /* command version */
768 0x00, /* EEPROM, box 0 */
769 0xf7
770 };
771 send_bulk_static_data(ep, init_data, sizeof(init_data));
772 /* while we're at it, pour on more magic */
773 send_bulk_static_data(ep, init_data, sizeof(init_data));
774}
775
Takashi Iwai86e07d32005-11-17 15:08:02 +0100776static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 static const u8 finish_data[] = {
779 /* switch to patch mode with last preset */
780 0xf0,
781 0x00, 0x20, 0x31, /* Emagic */
782 0x64, /* Unitor8 */
783 0x10, /* patch switch command */
784 0x00, /* command version */
785 0x7f, /* to all boxes */
786 0x40, /* last preset in EEPROM */
787 0xf7
788 };
789 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
790}
791
Takashi Iwai86e07d32005-11-17 15:08:02 +0100792static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 uint8_t* buffer, int buffer_length)
794{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200795 int i;
796
797 /* FF indicates end of valid data */
798 for (i = 0; i < buffer_length; ++i)
799 if (buffer[i] == 0xff) {
800 buffer_length = i;
801 break;
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* handle F5 at end of last buffer */
805 if (ep->seen_f5)
806 goto switch_port;
807
808 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /* determine size of data until next F5 */
810 for (i = 0; i < buffer_length; ++i)
811 if (buffer[i] == 0xf5)
812 break;
813 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
814 buffer += i;
815 buffer_length -= i;
816
817 if (buffer_length <= 0)
818 break;
819 /* assert(buffer[0] == 0xf5); */
820 ep->seen_f5 = 1;
821 ++buffer;
822 --buffer_length;
823
824 switch_port:
825 if (buffer_length <= 0)
826 break;
827 if (buffer[0] < 0x80) {
828 ep->current_port = (buffer[0] - 1) & 15;
829 ++buffer;
830 --buffer_length;
831 }
832 ep->seen_f5 = 0;
833 }
834}
835
Clemens Ladisched4affa2009-07-13 13:43:59 +0200836static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
837 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 int port0 = ep->current_port;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200840 uint8_t* buf = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 int buf_free = ep->max_transfer;
842 int length, i;
843
844 for (i = 0; i < 0x10; ++i) {
845 /* round-robin, starting at the last current port */
846 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100847 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (!port->active)
850 continue;
851 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
852 port->active = 0;
853 continue;
854 }
855
856 if (portnum != ep->current_port) {
857 if (buf_free < 2)
858 break;
859 ep->current_port = portnum;
860 buf[0] = 0xf5;
861 buf[1] = (portnum + 1) & 15;
862 buf += 2;
863 buf_free -= 2;
864 }
865
866 if (buf_free < 1)
867 break;
868 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
869 if (length > 0) {
870 buf += length;
871 buf_free -= length;
872 if (buf_free < 1)
873 break;
874 }
875 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200876 if (buf_free < ep->max_transfer && buf_free > 0) {
877 *buf = 0xff;
878 --buf_free;
879 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200880 urb->transfer_buffer_length = ep->max_transfer - buf_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
884 .input = snd_usbmidi_emagic_input,
885 .output = snd_usbmidi_emagic_output,
886 .init_out_endpoint = snd_usbmidi_emagic_init_out,
887 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
888};
889
890
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200891static void update_roland_altsetting(struct snd_usb_midi* umidi)
892{
893 struct usb_interface *intf;
894 struct usb_host_interface *hostif;
895 struct usb_interface_descriptor *intfd;
896 int is_light_load;
897
898 intf = umidi->iface;
899 is_light_load = intf->cur_altsetting != intf->altsetting;
900 if (umidi->roland_load_ctl->private_value == is_light_load)
901 return;
902 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
903 intfd = get_iface_desc(hostif);
904 snd_usbmidi_input_stop(&umidi->list);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100905 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200906 intfd->bAlternateSetting);
907 snd_usbmidi_input_start(&umidi->list);
908}
909
910static void substream_open(struct snd_rawmidi_substream *substream, int open)
911{
912 struct snd_usb_midi* umidi = substream->rmidi->private_data;
913 struct snd_kcontrol *ctl;
914
915 mutex_lock(&umidi->mutex);
916 if (open) {
917 if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
918 ctl = umidi->roland_load_ctl;
919 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100920 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200921 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
922 update_roland_altsetting(umidi);
923 }
924 } else {
925 if (--umidi->opened == 0 && umidi->roland_load_ctl) {
926 ctl = umidi->roland_load_ctl;
927 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100928 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200929 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
930 }
931 }
932 mutex_unlock(&umidi->mutex);
933}
934
Takashi Iwai86e07d32005-11-17 15:08:02 +0100935static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100937 struct snd_usb_midi* umidi = substream->rmidi->private_data;
938 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int i, j;
940
941 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
942 if (umidi->endpoints[i].out)
943 for (j = 0; j < 0x10; ++j)
944 if (umidi->endpoints[i].out->ports[j].substream == substream) {
945 port = &umidi->endpoints[i].out->ports[j];
946 break;
947 }
948 if (!port) {
949 snd_BUG();
950 return -ENXIO;
951 }
952 substream->runtime->private_data = port;
953 port->state = STATE_UNKNOWN;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200954 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return 0;
956}
957
Takashi Iwai86e07d32005-11-17 15:08:02 +0100958static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200960 substream_open(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962}
963
Takashi Iwai86e07d32005-11-17 15:08:02 +0100964static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100966 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 port->active = up;
969 if (up) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100970 if (port->ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* gobble up remaining bytes to prevent wait in
972 * snd_rawmidi_drain_output */
973 while (!snd_rawmidi_transmit_empty(substream))
974 snd_rawmidi_transmit_ack(substream, 1);
975 return;
976 }
Takashi Iwai1f041282008-12-18 12:17:55 +0100977 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979}
980
Clemens Ladischa65dd992009-07-13 13:48:36 +0200981static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
982{
983 struct usbmidi_out_port* port = substream->runtime->private_data;
984 struct snd_usb_midi_out_endpoint *ep = port->ep;
985 unsigned int drain_urbs;
986 DEFINE_WAIT(wait);
987 long timeout = msecs_to_jiffies(50);
988
989 /*
990 * The substream buffer is empty, but some data might still be in the
991 * currently active URBs, so we have to wait for those to complete.
992 */
993 spin_lock_irq(&ep->buffer_lock);
994 drain_urbs = ep->active_urbs;
995 if (drain_urbs) {
996 ep->drain_urbs |= drain_urbs;
997 do {
998 prepare_to_wait(&ep->drain_wait, &wait,
999 TASK_UNINTERRUPTIBLE);
1000 spin_unlock_irq(&ep->buffer_lock);
1001 timeout = schedule_timeout(timeout);
1002 spin_lock_irq(&ep->buffer_lock);
1003 drain_urbs &= ep->drain_urbs;
1004 } while (drain_urbs && timeout);
1005 finish_wait(&ep->drain_wait, &wait);
1006 }
1007 spin_unlock_irq(&ep->buffer_lock);
1008}
1009
Takashi Iwai86e07d32005-11-17 15:08:02 +01001010static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001012 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014}
1015
Takashi Iwai86e07d32005-11-17 15:08:02 +01001016static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001018 substream_open(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020}
1021
Takashi Iwai86e07d32005-11-17 15:08:02 +01001022static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001024 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (up)
1027 set_bit(substream->number, &umidi->input_triggered);
1028 else
1029 clear_bit(substream->number, &umidi->input_triggered);
1030}
1031
Takashi Iwai86e07d32005-11-17 15:08:02 +01001032static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 .open = snd_usbmidi_output_open,
1034 .close = snd_usbmidi_output_close,
1035 .trigger = snd_usbmidi_output_trigger,
Clemens Ladischa65dd992009-07-13 13:48:36 +02001036 .drain = snd_usbmidi_output_drain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037};
1038
Takashi Iwai86e07d32005-11-17 15:08:02 +01001039static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 .open = snd_usbmidi_input_open,
1041 .close = snd_usbmidi_input_close,
1042 .trigger = snd_usbmidi_input_trigger
1043};
1044
Clemens Ladisched4affa2009-07-13 13:43:59 +02001045static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1046 unsigned int buffer_length)
1047{
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001048 usb_buffer_free(umidi->dev, buffer_length,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001049 urb->transfer_buffer, urb->transfer_dma);
1050 usb_free_urb(urb);
1051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/*
1054 * Frees an input endpoint.
1055 * May be called when ep hasn't been initialized completely.
1056 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001057static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001059 unsigned int i;
1060
Clemens Ladisched4affa2009-07-13 13:43:59 +02001061 for (i = 0; i < INPUT_URBS; ++i)
1062 if (ep->urbs[i])
1063 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1064 ep->urbs[i]->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 kfree(ep);
1066}
1067
1068/*
1069 * Creates an input endpoint.
1070 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001071static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1072 struct snd_usb_midi_endpoint_info* ep_info,
1073 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001075 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 void* buffer;
1077 unsigned int pipe;
1078 int length;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001079 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001082 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (!ep)
1084 return -ENOMEM;
1085 ep->umidi = umidi;
1086
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001087 for (i = 0; i < INPUT_URBS; ++i) {
1088 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1089 if (!ep->urbs[i]) {
1090 snd_usbmidi_in_endpoint_delete(ep);
1091 return -ENOMEM;
1092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001095 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001097 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1098 length = usb_maxpacket(umidi->dev, pipe, 0);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001099 for (i = 0; i < INPUT_URBS; ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001100 buffer = usb_buffer_alloc(umidi->dev, length, GFP_KERNEL,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001101 &ep->urbs[i]->transfer_dma);
1102 if (!buffer) {
1103 snd_usbmidi_in_endpoint_delete(ep);
1104 return -ENOMEM;
1105 }
1106 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001107 usb_fill_int_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001108 pipe, buffer, length,
1109 snd_usbmidi_in_urb_complete,
1110 ep, ep_info->in_interval);
1111 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001112 usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001113 pipe, buffer, length,
1114 snd_usbmidi_in_urb_complete, ep);
1115 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 rep->in = ep;
1119 return 0;
1120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122/*
1123 * Frees an output endpoint.
1124 * May be called when ep hasn't been initialized completely.
1125 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001126static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Clemens Ladisched4affa2009-07-13 13:43:59 +02001128 unsigned int i;
1129
1130 for (i = 0; i < OUTPUT_URBS; ++i)
1131 if (ep->urbs[i].urb)
1132 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1133 ep->max_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 kfree(ep);
1135}
1136
1137/*
1138 * Creates an output endpoint, and initializes output ports.
1139 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001140static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1141 struct snd_usb_midi_endpoint_info* ep_info,
1142 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001144 struct snd_usb_midi_out_endpoint* ep;
Clemens Ladisched4affa2009-07-13 13:43:59 +02001145 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 unsigned int pipe;
1147 void* buffer;
1148
1149 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001150 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (!ep)
1152 return -ENOMEM;
1153 ep->umidi = umidi;
1154
Clemens Ladisched4affa2009-07-13 13:43:59 +02001155 for (i = 0; i < OUTPUT_URBS; ++i) {
1156 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1157 if (!ep->urbs[i].urb) {
1158 snd_usbmidi_out_endpoint_delete(ep);
1159 return -ENOMEM;
1160 }
1161 ep->urbs[i].ep = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001163 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001164 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001165 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001166 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001167 switch (umidi->usb_id) {
1168 default:
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001169 ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001170 break;
1171 /*
1172 * Various chips declare a packet size larger than 4 bytes, but
1173 * do not actually work with larger packets:
1174 */
1175 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1176 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1177 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1178 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1179 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1180 ep->max_transfer = 4;
1181 break;
1182 }
Clemens Ladisched4affa2009-07-13 13:43:59 +02001183 for (i = 0; i < OUTPUT_URBS; ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001184 buffer = usb_buffer_alloc(umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001185 ep->max_transfer, GFP_KERNEL,
1186 &ep->urbs[i].urb->transfer_dma);
1187 if (!buffer) {
1188 snd_usbmidi_out_endpoint_delete(ep);
1189 return -ENOMEM;
1190 }
1191 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001192 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001193 pipe, buffer, ep->max_transfer,
1194 snd_usbmidi_out_urb_complete,
1195 &ep->urbs[i], ep_info->out_interval);
1196 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001197 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001198 pipe, buffer, ep->max_transfer,
1199 snd_usbmidi_out_urb_complete,
1200 &ep->urbs[i]);
1201 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 spin_lock_init(&ep->buffer_lock);
1205 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
Clemens Ladischa65dd992009-07-13 13:48:36 +02001206 init_waitqueue_head(&ep->drain_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 for (i = 0; i < 0x10; ++i)
1209 if (ep_info->out_cables & (1 << i)) {
1210 ep->ports[i].ep = ep;
1211 ep->ports[i].cable = i << 4;
1212 }
1213
1214 if (umidi->usb_protocol_ops->init_out_endpoint)
1215 umidi->usb_protocol_ops->init_out_endpoint(ep);
1216
1217 rep->out = ep;
1218 return 0;
1219}
1220
1221/*
1222 * Frees everything.
1223 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001224static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 int i;
1227
1228 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001229 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (ep->out)
1231 snd_usbmidi_out_endpoint_delete(ep->out);
1232 if (ep->in)
1233 snd_usbmidi_in_endpoint_delete(ep->in);
1234 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001235 mutex_destroy(&umidi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 kfree(umidi);
1237}
1238
1239/*
1240 * Unlinks all URBs (must be done before the usb_device is deleted).
1241 */
Clemens Ladischee733392005-04-25 10:34:13 +02001242void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001244 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001245 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Takashi Iwai86e07d32005-11-17 15:08:02 +01001247 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001248 /*
1249 * an URB's completion handler may start the timer and
1250 * a timer may submit an URB. To reliably break the cycle
1251 * a flag under lock must be used
1252 */
1253 spin_lock_irq(&umidi->disc_lock);
1254 umidi->disconnected = 1;
1255 spin_unlock_irq(&umidi->disc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001257 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001258 if (ep->out)
1259 tasklet_kill(&ep->out->tasklet);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001260 if (ep->out) {
1261 for (j = 0; j < OUTPUT_URBS; ++j)
1262 usb_kill_urb(ep->out->urbs[j].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (umidi->usb_protocol_ops->finish_out_endpoint)
1264 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1265 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001266 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001267 for (j = 0; j < INPUT_URBS; ++j)
1268 usb_kill_urb(ep->in->urbs[j]);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001269 /* free endpoints here; later call can result in Oops */
1270 if (ep->out) {
1271 snd_usbmidi_out_endpoint_delete(ep->out);
1272 ep->out = NULL;
1273 }
1274 if (ep->in) {
1275 snd_usbmidi_in_endpoint_delete(ep->in);
1276 ep->in = NULL;
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001279 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Takashi Iwai86e07d32005-11-17 15:08:02 +01001282static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001284 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 snd_usbmidi_free(umidi);
1286}
1287
Takashi Iwai86e07d32005-11-17 15:08:02 +01001288static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int stream, int number)
1290{
1291 struct list_head* list;
1292
1293 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001294 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (substream->number == number)
1296 return substream;
1297 }
1298 return NULL;
1299}
1300
1301/*
1302 * This list specifies names for ports that do not fit into the standard
1303 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1304 * such as internal control or synthesizer ports.
1305 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001306static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001307 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001308 short int port;
1309 short int voices;
1310 const char *name;
1311 unsigned int seq_flags;
1312} snd_usbmidi_port_info[] = {
1313#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1314 { .id = USB_ID(vendor, product), \
1315 .port = num, .voices = voices_, \
1316 .name = name_, .seq_flags = flags }
1317#define EXTERNAL_PORT(vendor, product, num, name) \
1318 PORT_INFO(vendor, product, num, name, 0, \
1319 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1320 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1321 SNDRV_SEQ_PORT_TYPE_PORT)
1322#define CONTROL_PORT(vendor, product, num, name) \
1323 PORT_INFO(vendor, product, num, name, 0, \
1324 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1325 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1326#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1327 PORT_INFO(vendor, product, num, name, voices, \
1328 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1329 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1330 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1331 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1332 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1333 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1334 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1335#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1336 PORT_INFO(vendor, product, num, name, voices, \
1337 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1338 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1339 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1340 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1341 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1342 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1343 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1344 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001346 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001348 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1349 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1350 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1351 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1352 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1353 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001355 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1356 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001358 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1359 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1360 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001362 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1363 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1364 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001366 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1367 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1368 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001370 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001372 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1373 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1374 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1375 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001377 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001379 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1380 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1381 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001383 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1384 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1385 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1386 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001388 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1389 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001391 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1392 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1393 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001395 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1396 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1397 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001399 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1400 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001402 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1403 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001405 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1406 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1407 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001409 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1410 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1411 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001412 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001413 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001415 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1416 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001418 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1419 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001421 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1422 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1423 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Sebastien Alaiwand39e82d2010-02-16 08:55:08 +01001424 /* Access Music Virus TI */
1425 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1426 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1427 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1428 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1429 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430};
1431
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001432static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1433{
1434 int i;
1435
1436 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001437 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001438 snd_usbmidi_port_info[i].port == number)
1439 return &snd_usbmidi_port_info[i];
1440 }
1441 return NULL;
1442}
1443
1444static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1445 struct snd_seq_port_info *seq_port_info)
1446{
1447 struct snd_usb_midi *umidi = rmidi->private_data;
1448 struct port_info *port_info;
1449
1450 /* TODO: read port flags from descriptors */
1451 port_info = find_port_info(umidi, number);
1452 if (port_info) {
1453 seq_port_info->type = port_info->seq_flags;
1454 seq_port_info->midi_voices = port_info->voices;
1455 }
1456}
1457
Takashi Iwai86e07d32005-11-17 15:08:02 +01001458static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001460 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001462 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 const char *name_format;
1464
Takashi Iwai86e07d32005-11-17 15:08:02 +01001465 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (!substream) {
1467 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1468 return;
1469 }
1470
1471 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001472 port_info = find_port_info(umidi, number);
1473 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 snprintf(substream->name, sizeof(substream->name),
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001475 name_format, umidi->card->shortname, number + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 *rsubstream = substream;
1478}
1479
1480/*
1481 * Creates the endpoints and their ports.
1482 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001483static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1484 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 int i, j, err;
1487 int out_ports = 0, in_ports = 0;
1488
1489 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1490 if (endpoints[i].out_cables) {
1491 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1492 &umidi->endpoints[i]);
1493 if (err < 0)
1494 return err;
1495 }
1496 if (endpoints[i].in_cables) {
1497 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1498 &umidi->endpoints[i]);
1499 if (err < 0)
1500 return err;
1501 }
1502
1503 for (j = 0; j < 0x10; ++j) {
1504 if (endpoints[i].out_cables & (1 << j)) {
1505 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1506 &umidi->endpoints[i].out->ports[j].substream);
1507 ++out_ports;
1508 }
1509 if (endpoints[i].in_cables & (1 << j)) {
1510 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1511 &umidi->endpoints[i].in->ports[j].substream);
1512 ++in_ports;
1513 }
1514 }
1515 }
1516 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1517 out_ports, in_ports);
1518 return 0;
1519}
1520
1521/*
1522 * Returns MIDIStreaming device capabilities.
1523 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001524static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1525 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 struct usb_interface* intf;
1528 struct usb_host_interface *hostif;
1529 struct usb_interface_descriptor* intfd;
1530 struct usb_ms_header_descriptor* ms_header;
1531 struct usb_host_endpoint *hostep;
1532 struct usb_endpoint_descriptor* ep;
1533 struct usb_ms_endpoint_descriptor* ms_ep;
1534 int i, epidx;
1535
1536 intf = umidi->iface;
1537 if (!intf)
1538 return -ENXIO;
1539 hostif = &intf->altsetting[0];
1540 intfd = get_iface_desc(hostif);
1541 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1542 if (hostif->extralen >= 7 &&
1543 ms_header->bLength >= 7 &&
1544 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
Daniel Mackde48c7b2010-02-22 23:49:13 +01001545 ms_header->bDescriptorSubtype == UAC_HEADER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1547 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1548 else
1549 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1550
1551 epidx = 0;
1552 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1553 hostep = &hostif->endpoint[i];
1554 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001555 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 continue;
1557 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1558 if (hostep->extralen < 4 ||
1559 ms_ep->bLength < 4 ||
1560 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
Daniel Mackde48c7b2010-02-22 23:49:13 +01001561 ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001563 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (endpoints[epidx].out_ep) {
1565 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1566 snd_printk(KERN_WARNING "too many endpoints\n");
1567 break;
1568 }
1569 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001570 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1571 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001573 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001574 /*
1575 * Low speed bulk transfers don't exist, so
1576 * force interrupt transfers for devices like
1577 * ESI MIDI Mate that try to use them anyway.
1578 */
1579 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1581 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1582 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1583 } else {
1584 if (endpoints[epidx].in_ep) {
1585 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1586 snd_printk(KERN_WARNING "too many endpoints\n");
1587 break;
1588 }
1589 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001590 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1591 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001593 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001594 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1596 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1597 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1598 }
1599 }
1600 return 0;
1601}
1602
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001603static int roland_load_info(struct snd_kcontrol *kcontrol,
1604 struct snd_ctl_elem_info *info)
1605{
1606 static const char *const names[] = { "High Load", "Light Load" };
1607
1608 info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1609 info->count = 1;
1610 info->value.enumerated.items = 2;
1611 if (info->value.enumerated.item > 1)
1612 info->value.enumerated.item = 1;
1613 strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
1614 return 0;
1615}
1616
1617static int roland_load_get(struct snd_kcontrol *kcontrol,
1618 struct snd_ctl_elem_value *value)
1619{
1620 value->value.enumerated.item[0] = kcontrol->private_value;
1621 return 0;
1622}
1623
1624static int roland_load_put(struct snd_kcontrol *kcontrol,
1625 struct snd_ctl_elem_value *value)
1626{
1627 struct snd_usb_midi* umidi = kcontrol->private_data;
1628 int changed;
1629
1630 if (value->value.enumerated.item[0] > 1)
1631 return -EINVAL;
1632 mutex_lock(&umidi->mutex);
1633 changed = value->value.enumerated.item[0] != kcontrol->private_value;
1634 if (changed)
1635 kcontrol->private_value = value->value.enumerated.item[0];
1636 mutex_unlock(&umidi->mutex);
1637 return changed;
1638}
1639
1640static struct snd_kcontrol_new roland_load_ctl = {
1641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1642 .name = "MIDI Input Mode",
1643 .info = roland_load_info,
1644 .get = roland_load_get,
1645 .put = roland_load_put,
1646 .private_value = 1,
1647};
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/*
1650 * On Roland devices, use the second alternate setting to be able to use
1651 * the interrupt input endpoint.
1652 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001653static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 struct usb_interface* intf;
1656 struct usb_host_interface *hostif;
1657 struct usb_interface_descriptor* intfd;
1658
1659 intf = umidi->iface;
1660 if (!intf || intf->num_altsetting != 2)
1661 return;
1662
1663 hostif = &intf->altsetting[1];
1664 intfd = get_iface_desc(hostif);
1665 if (intfd->bNumEndpoints != 2 ||
1666 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1667 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1668 return;
1669
1670 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1671 intfd->bAlternateSetting);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001672 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 intfd->bAlternateSetting);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001674
1675 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001676 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001677 umidi->roland_load_ctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
1680/*
1681 * Try to find any usable endpoints in the interface.
1682 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001683static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1684 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 int max_endpoints)
1686{
1687 struct usb_interface* intf;
1688 struct usb_host_interface *hostif;
1689 struct usb_interface_descriptor* intfd;
1690 struct usb_endpoint_descriptor* epd;
1691 int i, out_eps = 0, in_eps = 0;
1692
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001693 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 snd_usbmidi_switch_roland_altsetting(umidi);
1695
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001696 if (endpoint[0].out_ep || endpoint[0].in_ep)
1697 return 0;
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 intf = umidi->iface;
1700 if (!intf || intf->num_altsetting < 1)
1701 return -ENOENT;
1702 hostif = intf->cur_altsetting;
1703 intfd = get_iface_desc(hostif);
1704
1705 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1706 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001707 if (!usb_endpoint_xfer_bulk(epd) &&
1708 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 continue;
1710 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001711 usb_endpoint_dir_out(epd)) {
1712 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1713 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 endpoint[out_eps].out_interval = epd->bInterval;
1715 ++out_eps;
1716 }
1717 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001718 usb_endpoint_dir_in(epd)) {
1719 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1720 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 endpoint[in_eps].in_interval = epd->bInterval;
1722 ++in_eps;
1723 }
1724 }
1725 return (out_eps || in_eps) ? 0 : -ENOENT;
1726}
1727
1728/*
1729 * Detects the endpoints for one-port-per-endpoint protocols.
1730 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001731static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1732 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 int err, i;
1735
1736 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1737 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1738 if (endpoints[i].out_ep)
1739 endpoints[i].out_cables = 0x0001;
1740 if (endpoints[i].in_ep)
1741 endpoints[i].in_cables = 0x0001;
1742 }
1743 return err;
1744}
1745
1746/*
1747 * Detects the endpoints and ports of Yamaha devices.
1748 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001749static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1750 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
1752 struct usb_interface* intf;
1753 struct usb_host_interface *hostif;
1754 struct usb_interface_descriptor* intfd;
1755 uint8_t* cs_desc;
1756
1757 intf = umidi->iface;
1758 if (!intf)
1759 return -ENOENT;
1760 hostif = intf->altsetting;
1761 intfd = get_iface_desc(hostif);
1762 if (intfd->bNumEndpoints < 1)
1763 return -ENOENT;
1764
1765 /*
1766 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1767 * necessarily with any useful contents. So simply count 'em.
1768 */
1769 for (cs_desc = hostif->extra;
1770 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1771 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001772 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001773 if (cs_desc[2] == UAC_MIDI_IN_JACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
Daniel Mackde48c7b2010-02-22 23:49:13 +01001775 else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1777 }
1778 }
1779 if (!endpoint->in_cables && !endpoint->out_cables)
1780 return -ENOENT;
1781
1782 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1783}
1784
1785/*
1786 * Creates the endpoints and their ports for Midiman devices.
1787 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001788static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1789 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001791 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct usb_interface* intf;
1793 struct usb_host_interface *hostif;
1794 struct usb_interface_descriptor* intfd;
1795 struct usb_endpoint_descriptor* epd;
1796 int cable, err;
1797
1798 intf = umidi->iface;
1799 if (!intf)
1800 return -ENOENT;
1801 hostif = intf->altsetting;
1802 intfd = get_iface_desc(hostif);
1803 /*
1804 * The various MidiSport devices have more or less random endpoint
1805 * numbers, so we have to identify the endpoints by their index in
1806 * the descriptor array, like the driver for that other OS does.
1807 *
1808 * There is one interrupt input endpoint for all input ports, one
1809 * bulk output endpoint for even-numbered ports, and one for odd-
1810 * numbered ports. Both bulk output endpoints have corresponding
1811 * input bulk endpoints (at indices 1 and 3) which aren't used.
1812 */
1813 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1814 snd_printdd(KERN_ERR "not enough endpoints\n");
1815 return -ENOENT;
1816 }
1817
1818 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001819 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1821 return -ENXIO;
1822 }
1823 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001824 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1826 return -ENXIO;
1827 }
1828 if (endpoint->out_cables > 0x0001) {
1829 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001830 if (!usb_endpoint_dir_out(epd) ||
1831 !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1833 return -ENXIO;
1834 }
1835 }
1836
1837 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01001838 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 ep_info.out_cables = endpoint->out_cables & 0x5555;
1840 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1841 if (err < 0)
1842 return err;
1843
1844 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1845 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1846 ep_info.in_cables = endpoint->in_cables;
1847 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1848 if (err < 0)
1849 return err;
1850
1851 if (endpoint->out_cables > 0x0001) {
1852 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1853 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1854 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1855 if (err < 0)
1856 return err;
1857 }
1858
1859 for (cable = 0; cable < 0x10; ++cable) {
1860 if (endpoint->out_cables & (1 << cable))
1861 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1862 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1863 if (endpoint->in_cables & (1 << cable))
1864 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1865 &umidi->endpoints[0].in->ports[cable].substream);
1866 }
1867 return 0;
1868}
1869
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001870static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1871 .get_port_info = snd_usbmidi_get_port_info,
1872};
1873
Takashi Iwai86e07d32005-11-17 15:08:02 +01001874static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 int out_ports, int in_ports)
1876{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001877 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 int err;
1879
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001880 err = snd_rawmidi_new(umidi->card, "USB MIDI",
1881 umidi->next_midi_device++,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 out_ports, in_ports, &rmidi);
1883 if (err < 0)
1884 return err;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001885 strcpy(rmidi->name, umidi->card->shortname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1887 SNDRV_RAWMIDI_INFO_INPUT |
1888 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001889 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 rmidi->private_data = umidi;
1891 rmidi->private_free = snd_usbmidi_rawmidi_free;
1892 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1893 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1894
1895 umidi->rmidi = rmidi;
1896 return 0;
1897}
1898
1899/*
1900 * Temporarily stop input.
1901 */
1902void snd_usbmidi_input_stop(struct list_head* p)
1903{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001904 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001905 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Takashi Iwai86e07d32005-11-17 15:08:02 +01001907 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001909 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001911 for (j = 0; j < INPUT_URBS; ++j)
1912 usb_kill_urb(ep->in->urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914}
1915
Takashi Iwai86e07d32005-11-17 15:08:02 +01001916static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001918 unsigned int i;
1919
1920 if (!ep)
1921 return;
1922 for (i = 0; i < INPUT_URBS; ++i) {
1923 struct urb* urb = ep->urbs[i];
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001924 urb->dev = ep->umidi->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1926 }
1927}
1928
1929/*
1930 * Resume input after a call to snd_usbmidi_input_stop().
1931 */
1932void snd_usbmidi_input_start(struct list_head* p)
1933{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001934 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 int i;
1936
Takashi Iwai86e07d32005-11-17 15:08:02 +01001937 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1939 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1940}
1941
1942/*
1943 * Creates and registers everything needed for a MIDI streaming interface.
1944 */
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001945int snd_usbmidi_create(struct snd_card *card,
1946 struct usb_interface* iface,
1947 struct list_head *midi_list,
1948 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001950 struct snd_usb_midi* umidi;
1951 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 int out_ports, in_ports;
1953 int i, err;
1954
Takashi Iwai561b2202005-09-09 14:22:34 +02001955 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (!umidi)
1957 return -ENOMEM;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001958 umidi->dev = interface_to_usbdev(iface);
1959 umidi->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 umidi->iface = iface;
1961 umidi->quirk = quirk;
1962 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001963 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001964 spin_lock_init(&umidi->disc_lock);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001965 mutex_init(&umidi->mutex);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001966 umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
1967 le16_to_cpu(umidi->dev->descriptor.idProduct));
Clemens Ladischc8846972005-08-02 15:26:52 +02001968 umidi->error_timer.function = snd_usbmidi_error_timer;
1969 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 /* detect the endpoint(s) to use */
1972 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001973 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1974 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001976 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
Clemens Ladischd05cc10432007-05-07 09:28:53 +02001977 umidi->usb_protocol_ops =
1978 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001979 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02001980 case QUIRK_MIDI_US122L:
1981 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1982 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02001983 case QUIRK_MIDI_FIXED_ENDPOINT:
1984 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001985 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001986 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1987 break;
1988 case QUIRK_MIDI_YAMAHA:
1989 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1990 break;
1991 case QUIRK_MIDI_MIDIMAN:
1992 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1993 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001994 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001995 err = 0;
1996 break;
1997 case QUIRK_MIDI_NOVATION:
1998 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1999 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2000 break;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02002001 case QUIRK_MIDI_FASTLANE:
Clemens Ladischd1bda042005-09-14 08:36:03 +02002002 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02002003 /*
2004 * Interface 1 contains isochronous endpoints, but with the same
2005 * numbers as in interface 0. Since it is interface 1 that the
2006 * USB core has most recently seen, these descriptors are now
2007 * associated with the endpoint numbers. This will foul up our
2008 * attempts to submit bulk/interrupt URBs to the endpoints in
2009 * interface 0, so we have to make sure that the USB core looks
2010 * again at interface 0 by calling usb_set_interface() on it.
2011 */
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002012 usb_set_interface(umidi->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002013 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2014 break;
2015 case QUIRK_MIDI_EMAGIC:
2016 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2017 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002018 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002019 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2020 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01002021 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02002022 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002023 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2024 break;
2025 default:
2026 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2027 err = -ENXIO;
2028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030 if (err < 0) {
2031 kfree(umidi);
2032 return err;
2033 }
2034
2035 /* create rawmidi device */
2036 out_ports = 0;
2037 in_ports = 0;
2038 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Akinobu Mitafbc54392009-11-20 14:56:52 +09002039 out_ports += hweight16(endpoints[i].out_cables);
2040 in_ports += hweight16(endpoints[i].in_cables);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2043 if (err < 0) {
2044 kfree(umidi);
2045 return err;
2046 }
2047
2048 /* create endpoint/port structures */
2049 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2050 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2051 else
2052 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2053 if (err < 0) {
2054 snd_usbmidi_free(umidi);
2055 return err;
2056 }
2057
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002058 list_add_tail(&umidi->list, midi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2061 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2062 return 0;
2063}
2064
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002065EXPORT_SYMBOL(snd_usbmidi_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066EXPORT_SYMBOL(snd_usbmidi_input_stop);
2067EXPORT_SYMBOL(snd_usbmidi_input_start);
2068EXPORT_SYMBOL(snd_usbmidi_disconnect);