blob: 7b8532453c4fe1bc9f5b7c9a8fd4a33b405d1e72 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040050#include <linux/module.h>
Daniel Mackde48c7b2010-02-22 23:49:13 +010051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <sound/core.h>
Clemens Ladisch96f61d92009-10-22 09:06:19 +020053#include <sound/control.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <sound/rawmidi.h>
Clemens Ladischa7b928a2006-05-02 16:22:12 +020055#include <sound/asequencer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "usbaudio.h"
Daniel Macke5779992010-03-04 19:46:13 +010057#include "midi.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010058#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010059#include "helper.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
62 * define this to log all USB packets
63 */
64/* #define DUMP_PACKETS */
65
Clemens Ladischc8846972005-08-02 15:26:52 +020066/*
67 * how long to wait after some USB errors, so that khubd can disconnect() us
68 * without too many spurious errors
69 */
70#define ERROR_DELAY_JIFFIES (HZ / 10)
71
Clemens Ladisched4affa2009-07-13 13:43:59 +020072#define OUTPUT_URBS 7
Clemens Ladisch4773d1f2009-07-13 13:40:36 +020073#define INPUT_URBS 7
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
77MODULE_DESCRIPTION("USB Audio/MIDI helper module");
78MODULE_LICENSE("Dual BSD/GPL");
79
80
81struct usb_ms_header_descriptor {
82 __u8 bLength;
83 __u8 bDescriptorType;
84 __u8 bDescriptorSubtype;
85 __u8 bcdMSC[2];
86 __le16 wTotalLength;
87} __attribute__ ((packed));
88
89struct usb_ms_endpoint_descriptor {
90 __u8 bLength;
91 __u8 bDescriptorType;
92 __u8 bDescriptorSubtype;
93 __u8 bNumEmbMIDIJack;
94 __u8 baAssocJackID[0];
95} __attribute__ ((packed));
96
Takashi Iwai86e07d32005-11-17 15:08:02 +010097struct snd_usb_midi_in_endpoint;
98struct snd_usb_midi_out_endpoint;
99struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100102 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200103 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Takashi Iwai86e07d32005-11-17 15:08:02 +0100105 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
106 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109struct snd_usb_midi {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100110 struct usb_device *dev;
111 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100113 const struct snd_usb_audio_quirk *quirk;
114 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct usb_protocol_ops* usb_protocol_ops;
116 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200117 struct timer_list error_timer;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100118 spinlock_t disc_lock;
Takashi Iwai2ca77192012-12-03 11:12:46 +0100119 struct rw_semaphore disc_rwsem;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200120 struct mutex mutex;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100121 u32 usb_id;
122 int next_midi_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100125 struct snd_usb_midi_out_endpoint *out;
126 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 } endpoints[MIDI_MAX_ENDPOINTS];
128 unsigned long input_triggered;
Takashi Iwai394c9d82012-12-03 11:30:50 +0100129 unsigned int opened[2];
Takashi Iwaic0792e02008-02-22 18:34:44 +0100130 unsigned char disconnected;
Takashi Iwai394c9d82012-12-03 11:30:50 +0100131 unsigned char input_running;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200132
133 struct snd_kcontrol *roland_load_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100137 struct snd_usb_midi* umidi;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200138 struct out_urb_context {
139 struct urb *urb;
140 struct snd_usb_midi_out_endpoint *ep;
141 } urbs[OUTPUT_URBS];
142 unsigned int active_urbs;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200143 unsigned int drain_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int max_transfer; /* size of urb buffer */
145 struct tasklet_struct tasklet;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200146 unsigned int next_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 spinlock_t buffer_lock;
148
149 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100150 struct snd_usb_midi_out_endpoint* ep;
151 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int active;
153 uint8_t cable; /* cable number << 4 */
154 uint8_t state;
155#define STATE_UNKNOWN 0
156#define STATE_1PARAM 1
157#define STATE_2PARAM_1 2
158#define STATE_2PARAM_2 3
159#define STATE_SYSEX_0 4
160#define STATE_SYSEX_1 5
161#define STATE_SYSEX_2 6
162 uint8_t data[2];
163 } ports[0x10];
164 int current_port;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200165
166 wait_queue_head_t drain_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
169struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100170 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200171 struct urb* urbs[INPUT_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100173 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200174 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200176 u8 seen_f5;
Clemens Ladisch3d282cb2015-11-15 22:39:08 +0100177 bool in_sysex;
178 u8 last_cin;
Clemens Ladischc8846972005-08-02 15:26:52 +0200179 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int current_port;
181};
182
Takashi Iwai86e07d32005-11-17 15:08:02 +0100183static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185static const uint8_t snd_usbmidi_cin_length[] = {
186 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
187};
188
189/*
190 * Submits the URB, with error handling.
191 */
Al Viro55016f12005-10-21 03:21:58 -0400192static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 int err = usb_submit_urb(urb, flags);
195 if (err < 0 && err != -ENODEV)
196 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
197 return err;
198}
199
200/*
201 * Error handling for URB completion functions.
202 */
203static int snd_usbmidi_urb_error(int status)
204{
Clemens Ladischc8846972005-08-02 15:26:52 +0200205 switch (status) {
206 /* manually unlinked, or device gone */
207 case -ENOENT:
208 case -ECONNRESET:
209 case -ESHUTDOWN:
210 case -ENODEV:
211 return -ENODEV;
212 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700213 case -EPROTO:
214 case -ETIME:
215 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200216 return -EIO;
217 default:
218 snd_printk(KERN_ERR "urb status %d\n", status);
219 return 0; /* continue */
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/*
224 * Receives a chunk of MIDI data.
225 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100226static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 uint8_t* data, int length)
228{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100229 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (!port->substream) {
232 snd_printd("unexpected port %d!\n", portidx);
233 return;
234 }
235 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
236 return;
237 snd_rawmidi_receive(port->substream, data, length);
238}
239
240#ifdef DUMP_PACKETS
241static void dump_urb(const char *type, const u8 *data, int length)
242{
243 snd_printk(KERN_DEBUG "%s packet: [", type);
244 for (; length > 0; ++data, --length)
245 printk(" %02x", *data);
246 printk(" ]\n");
247}
248#else
249#define dump_urb(type, data, length) /* nothing */
250#endif
251
252/*
253 * Processes the data read from the device.
254 */
David Howells7d12e782006-10-05 14:55:46 +0100255static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100257 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (urb->status == 0) {
260 dump_urb("received", urb->transfer_buffer, urb->actual_length);
261 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
262 urb->actual_length);
263 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200264 int err = snd_usbmidi_urb_error(urb->status);
265 if (err < 0) {
266 if (err != -ENODEV) {
267 ep->error_resubmit = 1;
268 mod_timer(&ep->umidi->error_timer,
269 jiffies + ERROR_DELAY_JIFFIES);
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100275 urb->dev = ep->umidi->dev;
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200276 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
David Howells7d12e782006-10-05 14:55:46 +0100279static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200281 struct out_urb_context *context = urb->context;
282 struct snd_usb_midi_out_endpoint* ep = context->ep;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200283 unsigned int urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 spin_lock(&ep->buffer_lock);
Clemens Ladischa65dd992009-07-13 13:48:36 +0200286 urb_index = context - ep->urbs;
287 ep->active_urbs &= ~(1 << urb_index);
288 if (unlikely(ep->drain_urbs)) {
289 ep->drain_urbs &= ~(1 << urb_index);
290 wake_up(&ep->drain_wait);
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 spin_unlock(&ep->buffer_lock);
293 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200294 int err = snd_usbmidi_urb_error(urb->status);
295 if (err < 0) {
296 if (err != -ENODEV)
297 mod_timer(&ep->umidi->error_timer,
298 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 snd_usbmidi_do_output(ep);
303}
304
305/*
306 * This is called when some data should be transferred to the device
307 * (from one or more substreams).
308 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100309static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200311 unsigned int urb_index;
312 struct urb* urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 unsigned long flags;
314
315 spin_lock_irqsave(&ep->buffer_lock, flags);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100316 if (ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 spin_unlock_irqrestore(&ep->buffer_lock, flags);
318 return;
319 }
320
Clemens Ladischa65dd992009-07-13 13:48:36 +0200321 urb_index = ep->next_urb;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200322 for (;;) {
Clemens Ladischa65dd992009-07-13 13:48:36 +0200323 if (!(ep->active_urbs & (1 << urb_index))) {
324 urb = ep->urbs[urb_index].urb;
325 urb->transfer_buffer_length = 0;
326 ep->umidi->usb_protocol_ops->output(ep, urb);
327 if (urb->transfer_buffer_length == 0)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Clemens Ladischa65dd992009-07-13 13:48:36 +0200330 dump_urb("sending", urb->transfer_buffer,
331 urb->transfer_buffer_length);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100332 urb->dev = ep->umidi->dev;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200333 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
334 break;
335 ep->active_urbs |= 1 << urb_index;
336 }
337 if (++urb_index >= OUTPUT_URBS)
338 urb_index = 0;
339 if (urb_index == ep->next_urb)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Clemens Ladischa65dd992009-07-13 13:48:36 +0200342 ep->next_urb = urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 spin_unlock_irqrestore(&ep->buffer_lock, flags);
344}
345
346static void snd_usbmidi_out_tasklet(unsigned long data)
347{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100348 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 snd_usbmidi_do_output(ep);
351}
352
Clemens Ladischc8846972005-08-02 15:26:52 +0200353/* called after transfers had been interrupted due to some USB error */
354static void snd_usbmidi_error_timer(unsigned long data)
355{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100356 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200357 unsigned int i, j;
Clemens Ladischc8846972005-08-02 15:26:52 +0200358
Takashi Iwaic0792e02008-02-22 18:34:44 +0100359 spin_lock(&umidi->disc_lock);
360 if (umidi->disconnected) {
361 spin_unlock(&umidi->disc_lock);
362 return;
363 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200364 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100365 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200366 if (in && in->error_resubmit) {
367 in->error_resubmit = 0;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200368 for (j = 0; j < INPUT_URBS; ++j) {
Takashi Iwaia28db4c2014-12-06 18:02:55 +0100369 if (atomic_read(&in->urbs[j]->use_count))
370 continue;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100371 in->urbs[j]->dev = umidi->dev;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200372 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
373 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200374 }
375 if (umidi->endpoints[i].out)
376 snd_usbmidi_do_output(umidi->endpoints[i].out);
377 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100378 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100382static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 const void *data, int len)
384{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200385 int err = 0;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700386 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!buf)
388 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 dump_urb("sending", buf, len);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200390 if (ep->urbs[0].urb)
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100391 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200392 buf, len, NULL, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 kfree(buf);
394 return err;
395}
396
397/*
398 * Standard USB MIDI protocol: see the spec.
399 * Midiman protocol: like the standard protocol, but the control byte is the
400 * fourth byte in each packet, and uses length instead of CIN.
401 */
402
Takashi Iwai86e07d32005-11-17 15:08:02 +0100403static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 uint8_t* buffer, int buffer_length)
405{
406 int i;
407
408 for (i = 0; i + 3 < buffer_length; i += 4)
409 if (buffer[i] != 0) {
410 int cable = buffer[i] >> 4;
411 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
412 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
413 }
414}
415
Takashi Iwai86e07d32005-11-17 15:08:02 +0100416static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 uint8_t* buffer, int buffer_length)
418{
419 int i;
420
421 for (i = 0; i + 3 < buffer_length; i += 4)
422 if (buffer[i + 3] != 0) {
423 int port = buffer[i + 3] >> 4;
424 int length = buffer[i + 3] & 3;
425 snd_usbmidi_input_data(ep, port, &buffer[i], length);
426 }
427}
428
429/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200430 * Buggy M-Audio device: running status on input results in a packet that has
431 * the data bytes but not the status byte and that is marked with CIN 4.
432 */
433static void snd_usbmidi_maudio_broken_running_status_input(
434 struct snd_usb_midi_in_endpoint* ep,
435 uint8_t* buffer, int buffer_length)
436{
437 int i;
438
439 for (i = 0; i + 3 < buffer_length; i += 4)
440 if (buffer[i] != 0) {
441 int cable = buffer[i] >> 4;
442 u8 cin = buffer[i] & 0x0f;
443 struct usbmidi_in_port *port = &ep->ports[cable];
444 int length;
Daniel Mack21af7d82010-06-16 17:57:29 +0200445
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200446 length = snd_usbmidi_cin_length[cin];
447 if (cin == 0xf && buffer[i + 1] >= 0xf8)
448 ; /* realtime msg: no running status change */
449 else if (cin >= 0x8 && cin <= 0xe)
450 /* channel msg */
451 port->running_status_length = length - 1;
452 else if (cin == 0x4 &&
453 port->running_status_length != 0 &&
454 buffer[i + 1] < 0x80)
455 /* CIN 4 that is not a SysEx */
456 length = port->running_status_length;
457 else
458 /*
459 * All other msgs cannot begin running status.
460 * (A channel msg sent as two or three CIN 0xF
461 * packets could in theory, but this device
462 * doesn't use this format.)
463 */
464 port->running_status_length = 0;
465 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
466 }
467}
468
469/*
Clemens Ladisch3d282cb2015-11-15 22:39:08 +0100470 * QinHeng CH345 is buggy: every second packet inside a SysEx has not CIN 4
471 * but the previously seen CIN, but still with three data bytes.
472 */
473static void ch345_broken_sysex_input(struct snd_usb_midi_in_endpoint *ep,
474 uint8_t *buffer, int buffer_length)
475{
476 unsigned int i, cin, length;
477
478 for (i = 0; i + 3 < buffer_length; i += 4) {
479 if (buffer[i] == 0 && i > 0)
480 break;
481 cin = buffer[i] & 0x0f;
482 if (ep->in_sysex &&
483 cin == ep->last_cin &&
484 (buffer[i + 1 + (cin == 0x6)] & 0x80) == 0)
485 cin = 0x4;
486#if 0
487 if (buffer[i + 1] == 0x90) {
488 /*
489 * Either a corrupted running status or a real note-on
490 * message; impossible to detect reliably.
491 */
492 }
493#endif
494 length = snd_usbmidi_cin_length[cin];
495 snd_usbmidi_input_data(ep, 0, &buffer[i + 1], length);
496 ep->in_sysex = cin == 0x4;
497 if (!ep->in_sysex)
498 ep->last_cin = cin;
499 }
500}
501
502/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200503 * CME protocol: like the standard protocol, but SysEx commands are sent as a
504 * single USB packet preceded by a 0x0F byte.
505 */
506static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
507 uint8_t *buffer, int buffer_length)
508{
509 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
510 snd_usbmidi_standard_input(ep, buffer, buffer_length);
511 else
512 snd_usbmidi_input_data(ep, buffer[0] >> 4,
513 &buffer[1], buffer_length - 1);
514}
515
516/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * Adds one USB MIDI packet to the output buffer.
518 */
519static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
520 uint8_t p1, uint8_t p2, uint8_t p3)
521{
522
523 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
524 buf[0] = p0;
525 buf[1] = p1;
526 buf[2] = p2;
527 buf[3] = p3;
528 urb->transfer_buffer_length += 4;
529}
530
531/*
532 * Adds one Midiman packet to the output buffer.
533 */
534static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
535 uint8_t p1, uint8_t p2, uint8_t p3)
536{
537
538 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
539 buf[0] = p1;
540 buf[1] = p2;
541 buf[2] = p3;
542 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
543 urb->transfer_buffer_length += 4;
544}
545
546/*
547 * Converts MIDI commands to USB MIDI packets.
548 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100549static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 uint8_t b, struct urb* urb)
551{
552 uint8_t p0 = port->cable;
553 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
554 port->ep->umidi->usb_protocol_ops->output_packet;
555
556 if (b >= 0xf8) {
557 output_packet(urb, p0 | 0x0f, b, 0, 0);
558 } else if (b >= 0xf0) {
559 switch (b) {
560 case 0xf0:
561 port->data[0] = b;
562 port->state = STATE_SYSEX_1;
563 break;
564 case 0xf1:
565 case 0xf3:
566 port->data[0] = b;
567 port->state = STATE_1PARAM;
568 break;
569 case 0xf2:
570 port->data[0] = b;
571 port->state = STATE_2PARAM_1;
572 break;
573 case 0xf4:
574 case 0xf5:
575 port->state = STATE_UNKNOWN;
576 break;
577 case 0xf6:
578 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
579 port->state = STATE_UNKNOWN;
580 break;
581 case 0xf7:
582 switch (port->state) {
583 case STATE_SYSEX_0:
584 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
585 break;
586 case STATE_SYSEX_1:
587 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
588 break;
589 case STATE_SYSEX_2:
590 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
591 break;
592 }
593 port->state = STATE_UNKNOWN;
594 break;
595 }
596 } else if (b >= 0x80) {
597 port->data[0] = b;
598 if (b >= 0xc0 && b <= 0xdf)
599 port->state = STATE_1PARAM;
600 else
601 port->state = STATE_2PARAM_1;
602 } else { /* b < 0x80 */
603 switch (port->state) {
604 case STATE_1PARAM:
605 if (port->data[0] < 0xf0) {
606 p0 |= port->data[0] >> 4;
607 } else {
608 p0 |= 0x02;
609 port->state = STATE_UNKNOWN;
610 }
611 output_packet(urb, p0, port->data[0], b, 0);
612 break;
613 case STATE_2PARAM_1:
614 port->data[1] = b;
615 port->state = STATE_2PARAM_2;
616 break;
617 case STATE_2PARAM_2:
618 if (port->data[0] < 0xf0) {
619 p0 |= port->data[0] >> 4;
620 port->state = STATE_2PARAM_1;
621 } else {
622 p0 |= 0x03;
623 port->state = STATE_UNKNOWN;
624 }
625 output_packet(urb, p0, port->data[0], port->data[1], b);
626 break;
627 case STATE_SYSEX_0:
628 port->data[0] = b;
629 port->state = STATE_SYSEX_1;
630 break;
631 case STATE_SYSEX_1:
632 port->data[1] = b;
633 port->state = STATE_SYSEX_2;
634 break;
635 case STATE_SYSEX_2:
636 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
637 port->state = STATE_SYSEX_0;
638 break;
639 }
640 }
641}
642
Clemens Ladisched4affa2009-07-13 13:43:59 +0200643static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
644 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int p;
647
648 /* FIXME: lower-numbered ports can starve higher-numbered ports */
649 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100650 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!port->active)
652 continue;
653 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
654 uint8_t b;
655 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
656 port->active = 0;
657 break;
658 }
659 snd_usbmidi_transmit_byte(port, b, urb);
660 }
661 }
662}
663
664static struct usb_protocol_ops snd_usbmidi_standard_ops = {
665 .input = snd_usbmidi_standard_input,
666 .output = snd_usbmidi_standard_output,
667 .output_packet = snd_usbmidi_output_standard_packet,
668};
669
670static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
671 .input = snd_usbmidi_midiman_input,
Daniel Mack21af7d82010-06-16 17:57:29 +0200672 .output = snd_usbmidi_standard_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 .output_packet = snd_usbmidi_output_midiman_packet,
674};
675
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200676static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
677 .input = snd_usbmidi_maudio_broken_running_status_input,
Daniel Mack21af7d82010-06-16 17:57:29 +0200678 .output = snd_usbmidi_standard_output,
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200679 .output_packet = snd_usbmidi_output_standard_packet,
680};
681
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200682static struct usb_protocol_ops snd_usbmidi_cme_ops = {
683 .input = snd_usbmidi_cme_input,
684 .output = snd_usbmidi_standard_output,
685 .output_packet = snd_usbmidi_output_standard_packet,
686};
687
Clemens Ladisch3d282cb2015-11-15 22:39:08 +0100688static struct usb_protocol_ops snd_usbmidi_ch345_broken_sysex_ops = {
689 .input = ch345_broken_sysex_input,
690 .output = snd_usbmidi_standard_output,
691 .output_packet = snd_usbmidi_output_standard_packet,
692};
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/*
Krzysztof Foltman4434ade2010-05-20 20:31:10 +0100695 * AKAI MPD16 protocol:
696 *
697 * For control port (endpoint 1):
698 * ==============================
699 * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
700 * SysEx message (msg_len=9 bytes long).
701 *
702 * For data port (endpoint 2):
703 * ===========================
704 * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
705 * MIDI message (msg_len bytes long)
706 *
707 * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
708 */
709static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
710 uint8_t *buffer, int buffer_length)
711{
712 unsigned int pos = 0;
713 unsigned int len = (unsigned int)buffer_length;
714 while (pos < len) {
715 unsigned int port = (buffer[pos] >> 4) - 1;
716 unsigned int msg_len = buffer[pos] & 0x0f;
717 pos++;
718 if (pos + msg_len <= len && port < 2)
719 snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
720 pos += msg_len;
721 }
722}
723
724#define MAX_AKAI_SYSEX_LEN 9
725
726static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
727 struct urb *urb)
728{
729 uint8_t *msg;
730 int pos, end, count, buf_end;
731 uint8_t tmp[MAX_AKAI_SYSEX_LEN];
732 struct snd_rawmidi_substream *substream = ep->ports[0].substream;
733
734 if (!ep->ports[0].active)
735 return;
736
737 msg = urb->transfer_buffer + urb->transfer_buffer_length;
738 buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
739
740 /* only try adding more data when there's space for at least 1 SysEx */
741 while (urb->transfer_buffer_length < buf_end) {
742 count = snd_rawmidi_transmit_peek(substream,
743 tmp, MAX_AKAI_SYSEX_LEN);
744 if (!count) {
745 ep->ports[0].active = 0;
746 return;
747 }
748 /* try to skip non-SysEx data */
749 for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
750 ;
751
752 if (pos > 0) {
753 snd_rawmidi_transmit_ack(substream, pos);
754 continue;
755 }
756
757 /* look for the start or end marker */
758 for (end = 1; end < count && tmp[end] < 0xF0; end++)
759 ;
760
761 /* next SysEx started before the end of current one */
762 if (end < count && tmp[end] == 0xF0) {
763 /* it's incomplete - drop it */
764 snd_rawmidi_transmit_ack(substream, end);
765 continue;
766 }
767 /* SysEx complete */
768 if (end < count && tmp[end] == 0xF7) {
769 /* queue it, ack it, and get the next one */
770 count = end + 1;
771 msg[0] = 0x10 | count;
772 memcpy(&msg[1], tmp, count);
773 snd_rawmidi_transmit_ack(substream, count);
774 urb->transfer_buffer_length += count + 1;
775 msg += count + 1;
776 continue;
777 }
778 /* less than 9 bytes and no end byte - wait for more */
779 if (count < MAX_AKAI_SYSEX_LEN) {
780 ep->ports[0].active = 0;
781 return;
782 }
783 /* 9 bytes and no end marker in sight - malformed, skip it */
784 snd_rawmidi_transmit_ack(substream, count);
785 }
786}
787
788static struct usb_protocol_ops snd_usbmidi_akai_ops = {
789 .input = snd_usbmidi_akai_input,
790 .output = snd_usbmidi_akai_output,
791};
792
793/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 * Novation USB MIDI protocol: number of data bytes is in the first byte
795 * (when receiving) (+1!) or in the second byte (when sending); data begins
796 * at the third byte.
797 */
798
Takashi Iwai86e07d32005-11-17 15:08:02 +0100799static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 uint8_t* buffer, int buffer_length)
801{
802 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
803 return;
804 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
805}
806
Clemens Ladisched4affa2009-07-13 13:43:59 +0200807static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
808 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 uint8_t* transfer_buffer;
811 int count;
812
813 if (!ep->ports[0].active)
814 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200815 transfer_buffer = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 count = snd_rawmidi_transmit(ep->ports[0].substream,
817 &transfer_buffer[2],
818 ep->max_transfer - 2);
819 if (count < 1) {
820 ep->ports[0].active = 0;
821 return;
822 }
823 transfer_buffer[0] = 0;
824 transfer_buffer[1] = count;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200825 urb->transfer_buffer_length = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828static struct usb_protocol_ops snd_usbmidi_novation_ops = {
829 .input = snd_usbmidi_novation_input,
830 .output = snd_usbmidi_novation_output,
831};
832
833/*
Clemens Ladischc7f57212010-10-22 18:20:48 +0200834 * "raw" protocol: just move raw MIDI bytes from/to the endpoint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
836
Takashi Iwai86e07d32005-11-17 15:08:02 +0100837static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200838 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
841}
842
Clemens Ladisched4affa2009-07-13 13:43:59 +0200843static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
844 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 int count;
847
848 if (!ep->ports[0].active)
849 return;
850 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200851 urb->transfer_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ep->max_transfer);
853 if (count < 1) {
854 ep->ports[0].active = 0;
855 return;
856 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200857 urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200860static struct usb_protocol_ops snd_usbmidi_raw_ops = {
861 .input = snd_usbmidi_raw_input,
862 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863};
864
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +0200865/*
866 * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
867 */
868
869static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint* ep,
870 uint8_t* buffer, int buffer_length)
871{
872 if (buffer_length > 2)
873 snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
874}
875
876static struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
877 .input = snd_usbmidi_ftdi_input,
878 .output = snd_usbmidi_raw_output,
879};
880
Karsten Wiese030a07e2008-07-30 15:13:29 +0200881static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
882 uint8_t *buffer, int buffer_length)
883{
884 if (buffer_length != 9)
885 return;
886 buffer_length = 8;
887 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
888 buffer_length--;
889 if (buffer_length)
890 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
891}
892
Clemens Ladisched4affa2009-07-13 13:43:59 +0200893static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
894 struct urb *urb)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200895{
896 int count;
897
898 if (!ep->ports[0].active)
899 return;
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700900 switch (snd_usb_get_speed(ep->umidi->dev)) {
901 case USB_SPEED_HIGH:
902 case USB_SPEED_SUPER:
903 count = 1;
904 break;
905 default:
906 count = 2;
907 }
Karsten Wiese030a07e2008-07-30 15:13:29 +0200908 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200909 urb->transfer_buffer,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200910 count);
911 if (count < 1) {
912 ep->ports[0].active = 0;
913 return;
914 }
915
Karsten Wiese921eebd2011-01-03 02:41:58 +0100916 memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
917 urb->transfer_buffer_length = ep->max_transfer;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200918}
919
920static struct usb_protocol_ops snd_usbmidi_122l_ops = {
921 .input = snd_usbmidi_us122l_input,
922 .output = snd_usbmidi_us122l_output,
923};
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
927 */
928
Takashi Iwai86e07d32005-11-17 15:08:02 +0100929static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 static const u8 init_data[] = {
932 /* initialization magic: "get version" */
933 0xf0,
934 0x00, 0x20, 0x31, /* Emagic */
935 0x64, /* Unitor8 */
936 0x0b, /* version number request */
937 0x00, /* command version */
938 0x00, /* EEPROM, box 0 */
939 0xf7
940 };
941 send_bulk_static_data(ep, init_data, sizeof(init_data));
942 /* while we're at it, pour on more magic */
943 send_bulk_static_data(ep, init_data, sizeof(init_data));
944}
945
Takashi Iwai86e07d32005-11-17 15:08:02 +0100946static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 static const u8 finish_data[] = {
949 /* switch to patch mode with last preset */
950 0xf0,
951 0x00, 0x20, 0x31, /* Emagic */
952 0x64, /* Unitor8 */
953 0x10, /* patch switch command */
954 0x00, /* command version */
955 0x7f, /* to all boxes */
956 0x40, /* last preset in EEPROM */
957 0xf7
958 };
959 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
960}
961
Takashi Iwai86e07d32005-11-17 15:08:02 +0100962static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 uint8_t* buffer, int buffer_length)
964{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200965 int i;
966
967 /* FF indicates end of valid data */
968 for (i = 0; i < buffer_length; ++i)
969 if (buffer[i] == 0xff) {
970 buffer_length = i;
971 break;
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /* handle F5 at end of last buffer */
975 if (ep->seen_f5)
976 goto switch_port;
977
978 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* determine size of data until next F5 */
980 for (i = 0; i < buffer_length; ++i)
981 if (buffer[i] == 0xf5)
982 break;
983 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
984 buffer += i;
985 buffer_length -= i;
986
987 if (buffer_length <= 0)
988 break;
989 /* assert(buffer[0] == 0xf5); */
990 ep->seen_f5 = 1;
991 ++buffer;
992 --buffer_length;
993
994 switch_port:
995 if (buffer_length <= 0)
996 break;
997 if (buffer[0] < 0x80) {
998 ep->current_port = (buffer[0] - 1) & 15;
999 ++buffer;
1000 --buffer_length;
1001 }
1002 ep->seen_f5 = 0;
1003 }
1004}
1005
Clemens Ladisched4affa2009-07-13 13:43:59 +02001006static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
1007 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 int port0 = ep->current_port;
Clemens Ladisched4affa2009-07-13 13:43:59 +02001010 uint8_t* buf = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int buf_free = ep->max_transfer;
1012 int length, i;
1013
1014 for (i = 0; i < 0x10; ++i) {
1015 /* round-robin, starting at the last current port */
1016 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +01001017 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (!port->active)
1020 continue;
1021 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
1022 port->active = 0;
1023 continue;
1024 }
1025
1026 if (portnum != ep->current_port) {
1027 if (buf_free < 2)
1028 break;
1029 ep->current_port = portnum;
1030 buf[0] = 0xf5;
1031 buf[1] = (portnum + 1) & 15;
1032 buf += 2;
1033 buf_free -= 2;
1034 }
1035
1036 if (buf_free < 1)
1037 break;
1038 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
1039 if (length > 0) {
1040 buf += length;
1041 buf_free -= length;
1042 if (buf_free < 1)
1043 break;
1044 }
1045 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +02001046 if (buf_free < ep->max_transfer && buf_free > 0) {
1047 *buf = 0xff;
1048 --buf_free;
1049 }
Clemens Ladisched4affa2009-07-13 13:43:59 +02001050 urb->transfer_buffer_length = ep->max_transfer - buf_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
1054 .input = snd_usbmidi_emagic_input,
1055 .output = snd_usbmidi_emagic_output,
1056 .init_out_endpoint = snd_usbmidi_emagic_init_out,
1057 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
1058};
1059
1060
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001061static void update_roland_altsetting(struct snd_usb_midi* umidi)
1062{
1063 struct usb_interface *intf;
1064 struct usb_host_interface *hostif;
1065 struct usb_interface_descriptor *intfd;
1066 int is_light_load;
1067
1068 intf = umidi->iface;
1069 is_light_load = intf->cur_altsetting != intf->altsetting;
1070 if (umidi->roland_load_ctl->private_value == is_light_load)
1071 return;
1072 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
1073 intfd = get_iface_desc(hostif);
1074 snd_usbmidi_input_stop(&umidi->list);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001075 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001076 intfd->bAlternateSetting);
1077 snd_usbmidi_input_start(&umidi->list);
1078}
1079
Takashi Iwai394c9d82012-12-03 11:30:50 +01001080static int substream_open(struct snd_rawmidi_substream *substream, int dir,
1081 int open)
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001082{
1083 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1084 struct snd_kcontrol *ctl;
1085
Takashi Iwai2ca77192012-12-03 11:12:46 +01001086 down_read(&umidi->disc_rwsem);
1087 if (umidi->disconnected) {
1088 up_read(&umidi->disc_rwsem);
Takashi Iwai394c9d82012-12-03 11:30:50 +01001089 return open ? -ENODEV : 0;
Takashi Iwai2ca77192012-12-03 11:12:46 +01001090 }
1091
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001092 mutex_lock(&umidi->mutex);
1093 if (open) {
Takashi Iwai394c9d82012-12-03 11:30:50 +01001094 if (!umidi->opened[0] && !umidi->opened[1]) {
Takashi Iwai394c9d82012-12-03 11:30:50 +01001095 if (umidi->roland_load_ctl) {
1096 ctl = umidi->roland_load_ctl;
1097 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1098 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001099 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
Takashi Iwai394c9d82012-12-03 11:30:50 +01001100 update_roland_altsetting(umidi);
1101 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001102 }
Takashi Iwai394c9d82012-12-03 11:30:50 +01001103 umidi->opened[dir]++;
1104 if (umidi->opened[1])
1105 snd_usbmidi_input_start(&umidi->list);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001106 } else {
Takashi Iwai394c9d82012-12-03 11:30:50 +01001107 umidi->opened[dir]--;
1108 if (!umidi->opened[1])
1109 snd_usbmidi_input_stop(&umidi->list);
1110 if (!umidi->opened[0] && !umidi->opened[1]) {
1111 if (umidi->roland_load_ctl) {
1112 ctl = umidi->roland_load_ctl;
1113 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1114 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001115 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
Takashi Iwai394c9d82012-12-03 11:30:50 +01001116 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001117 }
1118 }
1119 mutex_unlock(&umidi->mutex);
Takashi Iwai2ca77192012-12-03 11:12:46 +01001120 up_read(&umidi->disc_rwsem);
Takashi Iwai394c9d82012-12-03 11:30:50 +01001121 return 0;
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001122}
1123
Takashi Iwai86e07d32005-11-17 15:08:02 +01001124static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001126 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1127 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int i, j;
1129
1130 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1131 if (umidi->endpoints[i].out)
1132 for (j = 0; j < 0x10; ++j)
1133 if (umidi->endpoints[i].out->ports[j].substream == substream) {
1134 port = &umidi->endpoints[i].out->ports[j];
1135 break;
1136 }
1137 if (!port) {
1138 snd_BUG();
1139 return -ENXIO;
1140 }
Takashi Iwai2ca77192012-12-03 11:12:46 +01001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 substream->runtime->private_data = port;
1143 port->state = STATE_UNKNOWN;
Takashi Iwai394c9d82012-12-03 11:30:50 +01001144 return substream_open(substream, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Takashi Iwai86e07d32005-11-17 15:08:02 +01001147static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Takashi Iwai394c9d82012-12-03 11:30:50 +01001149 return substream_open(substream, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Takashi Iwai86e07d32005-11-17 15:08:02 +01001152static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001154 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 port->active = up;
1157 if (up) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001158 if (port->ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* gobble up remaining bytes to prevent wait in
1160 * snd_rawmidi_drain_output */
1161 while (!snd_rawmidi_transmit_empty(substream))
1162 snd_rawmidi_transmit_ack(substream, 1);
1163 return;
1164 }
Takashi Iwai1f041282008-12-18 12:17:55 +01001165 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167}
1168
Clemens Ladischa65dd992009-07-13 13:48:36 +02001169static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
1170{
1171 struct usbmidi_out_port* port = substream->runtime->private_data;
1172 struct snd_usb_midi_out_endpoint *ep = port->ep;
1173 unsigned int drain_urbs;
1174 DEFINE_WAIT(wait);
1175 long timeout = msecs_to_jiffies(50);
1176
Takashi Iwai29aac002010-04-10 21:27:23 +02001177 if (ep->umidi->disconnected)
1178 return;
Clemens Ladischa65dd992009-07-13 13:48:36 +02001179 /*
1180 * The substream buffer is empty, but some data might still be in the
1181 * currently active URBs, so we have to wait for those to complete.
1182 */
1183 spin_lock_irq(&ep->buffer_lock);
1184 drain_urbs = ep->active_urbs;
1185 if (drain_urbs) {
1186 ep->drain_urbs |= drain_urbs;
1187 do {
1188 prepare_to_wait(&ep->drain_wait, &wait,
1189 TASK_UNINTERRUPTIBLE);
1190 spin_unlock_irq(&ep->buffer_lock);
1191 timeout = schedule_timeout(timeout);
1192 spin_lock_irq(&ep->buffer_lock);
1193 drain_urbs &= ep->drain_urbs;
1194 } while (drain_urbs && timeout);
1195 finish_wait(&ep->drain_wait, &wait);
1196 }
1197 spin_unlock_irq(&ep->buffer_lock);
1198}
1199
Takashi Iwai86e07d32005-11-17 15:08:02 +01001200static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Takashi Iwai394c9d82012-12-03 11:30:50 +01001202 return substream_open(substream, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Takashi Iwai86e07d32005-11-17 15:08:02 +01001205static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Takashi Iwai394c9d82012-12-03 11:30:50 +01001207 return substream_open(substream, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Takashi Iwai86e07d32005-11-17 15:08:02 +01001210static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001212 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if (up)
1215 set_bit(substream->number, &umidi->input_triggered);
1216 else
1217 clear_bit(substream->number, &umidi->input_triggered);
1218}
1219
Takashi Iwai86e07d32005-11-17 15:08:02 +01001220static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 .open = snd_usbmidi_output_open,
1222 .close = snd_usbmidi_output_close,
1223 .trigger = snd_usbmidi_output_trigger,
Clemens Ladischa65dd992009-07-13 13:48:36 +02001224 .drain = snd_usbmidi_output_drain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225};
1226
Takashi Iwai86e07d32005-11-17 15:08:02 +01001227static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 .open = snd_usbmidi_input_open,
1229 .close = snd_usbmidi_input_close,
1230 .trigger = snd_usbmidi_input_trigger
1231};
1232
Clemens Ladisched4affa2009-07-13 13:43:59 +02001233static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1234 unsigned int buffer_length)
1235{
Daniel Mack997ea582010-04-12 13:17:25 +02001236 usb_free_coherent(umidi->dev, buffer_length,
1237 urb->transfer_buffer, urb->transfer_dma);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001238 usb_free_urb(urb);
1239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
1242 * Frees an input endpoint.
1243 * May be called when ep hasn't been initialized completely.
1244 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001245static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001247 unsigned int i;
1248
Clemens Ladisched4affa2009-07-13 13:43:59 +02001249 for (i = 0; i < INPUT_URBS; ++i)
1250 if (ep->urbs[i])
1251 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1252 ep->urbs[i]->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 kfree(ep);
1254}
1255
1256/*
1257 * Creates an input endpoint.
1258 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001259static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1260 struct snd_usb_midi_endpoint_info* ep_info,
1261 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001263 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 void* buffer;
1265 unsigned int pipe;
1266 int length;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001267 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001270 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!ep)
1272 return -ENOMEM;
1273 ep->umidi = umidi;
1274
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001275 for (i = 0; i < INPUT_URBS; ++i) {
1276 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1277 if (!ep->urbs[i]) {
1278 snd_usbmidi_in_endpoint_delete(ep);
1279 return -ENOMEM;
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
1282 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001283 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001285 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1286 length = usb_maxpacket(umidi->dev, pipe, 0);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001287 for (i = 0; i < INPUT_URBS; ++i) {
Daniel Mack997ea582010-04-12 13:17:25 +02001288 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1289 &ep->urbs[i]->transfer_dma);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001290 if (!buffer) {
1291 snd_usbmidi_in_endpoint_delete(ep);
1292 return -ENOMEM;
1293 }
1294 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001295 usb_fill_int_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001296 pipe, buffer, length,
1297 snd_usbmidi_in_urb_complete,
1298 ep, ep_info->in_interval);
1299 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001300 usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001301 pipe, buffer, length,
1302 snd_usbmidi_in_urb_complete, ep);
1303 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 rep->in = ep;
1307 return 0;
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/*
1311 * Frees an output endpoint.
1312 * May be called when ep hasn't been initialized completely.
1313 */
Takashi Iwai29aac002010-04-10 21:27:23 +02001314static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Clemens Ladisched4affa2009-07-13 13:43:59 +02001316 unsigned int i;
1317
1318 for (i = 0; i < OUTPUT_URBS; ++i)
Takashi Iwai29aac002010-04-10 21:27:23 +02001319 if (ep->urbs[i].urb) {
Clemens Ladisched4affa2009-07-13 13:43:59 +02001320 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1321 ep->max_transfer);
Takashi Iwai29aac002010-04-10 21:27:23 +02001322 ep->urbs[i].urb = NULL;
1323 }
1324}
1325
1326static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1327{
1328 snd_usbmidi_out_endpoint_clear(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 kfree(ep);
1330}
1331
1332/*
1333 * Creates an output endpoint, and initializes output ports.
1334 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001335static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1336 struct snd_usb_midi_endpoint_info* ep_info,
Daniel Mack21af7d82010-06-16 17:57:29 +02001337 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001339 struct snd_usb_midi_out_endpoint* ep;
Clemens Ladisched4affa2009-07-13 13:43:59 +02001340 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 unsigned int pipe;
1342 void* buffer;
1343
1344 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001345 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (!ep)
1347 return -ENOMEM;
1348 ep->umidi = umidi;
1349
Clemens Ladisched4affa2009-07-13 13:43:59 +02001350 for (i = 0; i < OUTPUT_URBS; ++i) {
1351 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1352 if (!ep->urbs[i].urb) {
1353 snd_usbmidi_out_endpoint_delete(ep);
1354 return -ENOMEM;
1355 }
1356 ep->urbs[i].ep = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001358 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001359 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001360 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001361 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001362 switch (umidi->usb_id) {
1363 default:
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001364 ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001365 break;
1366 /*
1367 * Various chips declare a packet size larger than 4 bytes, but
1368 * do not actually work with larger packets:
1369 */
Clemens Ladischb7be03b2015-11-15 22:37:44 +01001370 case USB_ID(0x0a67, 0x5011): /* Medeli DD305 */
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001371 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1372 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1373 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1374 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1375 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
Tarek Soliman49c039f2011-04-04 09:23:53 -05001376 case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001377 ep->max_transfer = 4;
1378 break;
Karsten Wiese921eebd2011-01-03 02:41:58 +01001379 /*
1380 * Some devices only work with 9 bytes packet size:
1381 */
1382 case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
1383 case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
1384 ep->max_transfer = 9;
1385 break;
Clemens Ladischf167e1d2010-02-15 08:55:28 +01001386 }
Clemens Ladisched4affa2009-07-13 13:43:59 +02001387 for (i = 0; i < OUTPUT_URBS; ++i) {
Daniel Mack997ea582010-04-12 13:17:25 +02001388 buffer = usb_alloc_coherent(umidi->dev,
1389 ep->max_transfer, GFP_KERNEL,
1390 &ep->urbs[i].urb->transfer_dma);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001391 if (!buffer) {
1392 snd_usbmidi_out_endpoint_delete(ep);
1393 return -ENOMEM;
1394 }
1395 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001396 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001397 pipe, buffer, ep->max_transfer,
1398 snd_usbmidi_out_urb_complete,
1399 &ep->urbs[i], ep_info->out_interval);
1400 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001401 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001402 pipe, buffer, ep->max_transfer,
1403 snd_usbmidi_out_urb_complete,
1404 &ep->urbs[i]);
1405 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 spin_lock_init(&ep->buffer_lock);
1409 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
Clemens Ladischa65dd992009-07-13 13:48:36 +02001410 init_waitqueue_head(&ep->drain_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 for (i = 0; i < 0x10; ++i)
1413 if (ep_info->out_cables & (1 << i)) {
1414 ep->ports[i].ep = ep;
1415 ep->ports[i].cable = i << 4;
1416 }
1417
1418 if (umidi->usb_protocol_ops->init_out_endpoint)
1419 umidi->usb_protocol_ops->init_out_endpoint(ep);
1420
1421 rep->out = ep;
1422 return 0;
1423}
1424
1425/*
1426 * Frees everything.
1427 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001428static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 int i;
1431
1432 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001433 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (ep->out)
1435 snd_usbmidi_out_endpoint_delete(ep->out);
1436 if (ep->in)
1437 snd_usbmidi_in_endpoint_delete(ep->in);
1438 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001439 mutex_destroy(&umidi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 kfree(umidi);
1441}
1442
1443/*
1444 * Unlinks all URBs (must be done before the usb_device is deleted).
1445 */
Clemens Ladischee733392005-04-25 10:34:13 +02001446void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001448 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001449 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Takashi Iwai86e07d32005-11-17 15:08:02 +01001451 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001452 /*
1453 * an URB's completion handler may start the timer and
1454 * a timer may submit an URB. To reliably break the cycle
1455 * a flag under lock must be used
1456 */
Takashi Iwai2ca77192012-12-03 11:12:46 +01001457 down_write(&umidi->disc_rwsem);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001458 spin_lock_irq(&umidi->disc_lock);
1459 umidi->disconnected = 1;
1460 spin_unlock_irq(&umidi->disc_lock);
Takashi Iwai2ca77192012-12-03 11:12:46 +01001461 up_write(&umidi->disc_rwsem);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001464 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001465 if (ep->out)
1466 tasklet_kill(&ep->out->tasklet);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001467 if (ep->out) {
1468 for (j = 0; j < OUTPUT_URBS; ++j)
1469 usb_kill_urb(ep->out->urbs[j].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (umidi->usb_protocol_ops->finish_out_endpoint)
1471 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
Takashi Iwai29aac002010-04-10 21:27:23 +02001472 ep->out->active_urbs = 0;
1473 if (ep->out->drain_urbs) {
1474 ep->out->drain_urbs = 0;
1475 wake_up(&ep->out->drain_wait);
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001478 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001479 for (j = 0; j < INPUT_URBS; ++j)
1480 usb_kill_urb(ep->in->urbs[j]);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001481 /* free endpoints here; later call can result in Oops */
Takashi Iwai29aac002010-04-10 21:27:23 +02001482 if (ep->out)
1483 snd_usbmidi_out_endpoint_clear(ep->out);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001484 if (ep->in) {
1485 snd_usbmidi_in_endpoint_delete(ep->in);
1486 ep->in = NULL;
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001489 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Takashi Iwai86e07d32005-11-17 15:08:02 +01001492static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001494 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 snd_usbmidi_free(umidi);
1496}
1497
Takashi Iwai86e07d32005-11-17 15:08:02 +01001498static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Daniel Mack21af7d82010-06-16 17:57:29 +02001499 int stream, int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 struct list_head* list;
1502
1503 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001504 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (substream->number == number)
1506 return substream;
1507 }
1508 return NULL;
1509}
1510
1511/*
1512 * This list specifies names for ports that do not fit into the standard
1513 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1514 * such as internal control or synthesizer ports.
1515 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001516static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001517 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001518 short int port;
1519 short int voices;
1520 const char *name;
1521 unsigned int seq_flags;
1522} snd_usbmidi_port_info[] = {
1523#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1524 { .id = USB_ID(vendor, product), \
1525 .port = num, .voices = voices_, \
1526 .name = name_, .seq_flags = flags }
1527#define EXTERNAL_PORT(vendor, product, num, name) \
1528 PORT_INFO(vendor, product, num, name, 0, \
1529 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1530 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1531 SNDRV_SEQ_PORT_TYPE_PORT)
1532#define CONTROL_PORT(vendor, product, num, name) \
1533 PORT_INFO(vendor, product, num, name, 0, \
1534 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1535 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1536#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1537 PORT_INFO(vendor, product, num, name, voices, \
1538 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1539 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1540 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1541 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1542 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1543 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1544 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1545#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1546 PORT_INFO(vendor, product, num, name, voices, \
1547 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1548 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1549 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1550 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1551 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1552 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1553 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1554 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001556 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001558 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1559 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1560 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1561 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1562 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1563 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001565 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1566 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001568 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1569 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1570 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001572 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1573 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1574 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001576 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1577 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1578 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001580 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001582 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1583 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1584 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1585 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001587 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001589 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1590 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1591 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001593 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1594 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1595 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1596 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001598 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1599 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001601 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1602 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1603 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001605 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1606 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1607 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001609 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1610 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001612 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1613 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001615 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1616 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1617 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001619 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1620 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1621 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001622 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001623 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001625 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1626 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001628 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1629 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001631 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1632 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1633 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Krzysztof Foltman4434ade2010-05-20 20:31:10 +01001634 /* Akai MPD16 */
1635 CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1636 PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1637 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1638 SNDRV_SEQ_PORT_TYPE_HARDWARE),
Sebastien Alaiwand39e82d2010-02-16 08:55:08 +01001639 /* Access Music Virus TI */
1640 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1641 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1642 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1643 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1644 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645};
1646
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001647static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1648{
1649 int i;
1650
1651 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001652 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001653 snd_usbmidi_port_info[i].port == number)
1654 return &snd_usbmidi_port_info[i];
1655 }
1656 return NULL;
1657}
1658
1659static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1660 struct snd_seq_port_info *seq_port_info)
1661{
1662 struct snd_usb_midi *umidi = rmidi->private_data;
1663 struct port_info *port_info;
1664
1665 /* TODO: read port flags from descriptors */
1666 port_info = find_port_info(umidi, number);
1667 if (port_info) {
1668 seq_port_info->type = port_info->seq_flags;
1669 seq_port_info->midi_voices = port_info->voices;
1670 }
1671}
1672
Takashi Iwai86e07d32005-11-17 15:08:02 +01001673static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001675 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001677 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 const char *name_format;
1679
Takashi Iwai86e07d32005-11-17 15:08:02 +01001680 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (!substream) {
1682 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1683 return;
1684 }
1685
1686 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001687 port_info = find_port_info(umidi, number);
1688 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 snprintf(substream->name, sizeof(substream->name),
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001690 name_format, umidi->card->shortname, number + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 *rsubstream = substream;
1693}
1694
1695/*
1696 * Creates the endpoints and their ports.
1697 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001698static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1699 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 int i, j, err;
1702 int out_ports = 0, in_ports = 0;
1703
1704 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1705 if (endpoints[i].out_cables) {
1706 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1707 &umidi->endpoints[i]);
1708 if (err < 0)
1709 return err;
1710 }
1711 if (endpoints[i].in_cables) {
1712 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1713 &umidi->endpoints[i]);
1714 if (err < 0)
1715 return err;
1716 }
1717
1718 for (j = 0; j < 0x10; ++j) {
1719 if (endpoints[i].out_cables & (1 << j)) {
1720 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1721 &umidi->endpoints[i].out->ports[j].substream);
1722 ++out_ports;
1723 }
1724 if (endpoints[i].in_cables & (1 << j)) {
1725 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1726 &umidi->endpoints[i].in->ports[j].substream);
1727 ++in_ports;
1728 }
1729 }
1730 }
1731 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1732 out_ports, in_ports);
1733 return 0;
1734}
1735
1736/*
1737 * Returns MIDIStreaming device capabilities.
1738 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001739static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1740 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
1742 struct usb_interface* intf;
1743 struct usb_host_interface *hostif;
1744 struct usb_interface_descriptor* intfd;
1745 struct usb_ms_header_descriptor* ms_header;
1746 struct usb_host_endpoint *hostep;
1747 struct usb_endpoint_descriptor* ep;
1748 struct usb_ms_endpoint_descriptor* ms_ep;
1749 int i, epidx;
1750
1751 intf = umidi->iface;
1752 if (!intf)
1753 return -ENXIO;
1754 hostif = &intf->altsetting[0];
1755 intfd = get_iface_desc(hostif);
1756 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1757 if (hostif->extralen >= 7 &&
1758 ms_header->bLength >= 7 &&
1759 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
Daniel Mackde48c7b2010-02-22 23:49:13 +01001760 ms_header->bDescriptorSubtype == UAC_HEADER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1762 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1763 else
1764 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1765
1766 epidx = 0;
1767 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1768 hostep = &hostif->endpoint[i];
1769 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001770 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 continue;
1772 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1773 if (hostep->extralen < 4 ||
1774 ms_ep->bLength < 4 ||
1775 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
Daniel Mackde48c7b2010-02-22 23:49:13 +01001776 ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001778 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (endpoints[epidx].out_ep) {
1780 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1781 snd_printk(KERN_WARNING "too many endpoints\n");
1782 break;
1783 }
1784 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001785 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1786 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001788 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001789 /*
1790 * Low speed bulk transfers don't exist, so
1791 * force interrupt transfers for devices like
1792 * ESI MIDI Mate that try to use them anyway.
1793 */
1794 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1796 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1797 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1798 } else {
1799 if (endpoints[epidx].in_ep) {
1800 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1801 snd_printk(KERN_WARNING "too many endpoints\n");
1802 break;
1803 }
1804 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001805 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1806 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001808 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001809 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1811 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1812 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1813 }
1814 }
1815 return 0;
1816}
1817
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001818static int roland_load_info(struct snd_kcontrol *kcontrol,
1819 struct snd_ctl_elem_info *info)
1820{
1821 static const char *const names[] = { "High Load", "Light Load" };
1822
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01001823 return snd_ctl_enum_info(info, 1, 2, names);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001824}
1825
1826static int roland_load_get(struct snd_kcontrol *kcontrol,
1827 struct snd_ctl_elem_value *value)
1828{
1829 value->value.enumerated.item[0] = kcontrol->private_value;
1830 return 0;
1831}
1832
1833static int roland_load_put(struct snd_kcontrol *kcontrol,
1834 struct snd_ctl_elem_value *value)
1835{
1836 struct snd_usb_midi* umidi = kcontrol->private_data;
1837 int changed;
1838
1839 if (value->value.enumerated.item[0] > 1)
1840 return -EINVAL;
1841 mutex_lock(&umidi->mutex);
1842 changed = value->value.enumerated.item[0] != kcontrol->private_value;
1843 if (changed)
1844 kcontrol->private_value = value->value.enumerated.item[0];
1845 mutex_unlock(&umidi->mutex);
1846 return changed;
1847}
1848
1849static struct snd_kcontrol_new roland_load_ctl = {
1850 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1851 .name = "MIDI Input Mode",
1852 .info = roland_load_info,
1853 .get = roland_load_get,
1854 .put = roland_load_put,
1855 .private_value = 1,
1856};
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/*
1859 * On Roland devices, use the second alternate setting to be able to use
1860 * the interrupt input endpoint.
1861 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001862static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 struct usb_interface* intf;
1865 struct usb_host_interface *hostif;
1866 struct usb_interface_descriptor* intfd;
1867
1868 intf = umidi->iface;
1869 if (!intf || intf->num_altsetting != 2)
1870 return;
1871
1872 hostif = &intf->altsetting[1];
1873 intfd = get_iface_desc(hostif);
1874 if (intfd->bNumEndpoints != 2 ||
1875 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1876 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1877 return;
1878
1879 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1880 intfd->bAlternateSetting);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001881 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 intfd->bAlternateSetting);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001883
1884 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001885 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001886 umidi->roland_load_ctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889/*
1890 * Try to find any usable endpoints in the interface.
1891 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001892static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1893 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 int max_endpoints)
1895{
1896 struct usb_interface* intf;
1897 struct usb_host_interface *hostif;
1898 struct usb_interface_descriptor* intfd;
1899 struct usb_endpoint_descriptor* epd;
1900 int i, out_eps = 0, in_eps = 0;
1901
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001902 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 snd_usbmidi_switch_roland_altsetting(umidi);
1904
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001905 if (endpoint[0].out_ep || endpoint[0].in_ep)
Daniel Mack21af7d82010-06-16 17:57:29 +02001906 return 0;
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 intf = umidi->iface;
1909 if (!intf || intf->num_altsetting < 1)
1910 return -ENOENT;
1911 hostif = intf->cur_altsetting;
1912 intfd = get_iface_desc(hostif);
1913
1914 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1915 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001916 if (!usb_endpoint_xfer_bulk(epd) &&
1917 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 continue;
1919 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001920 usb_endpoint_dir_out(epd)) {
1921 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1922 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 endpoint[out_eps].out_interval = epd->bInterval;
1924 ++out_eps;
1925 }
1926 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001927 usb_endpoint_dir_in(epd)) {
1928 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1929 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 endpoint[in_eps].in_interval = epd->bInterval;
1931 ++in_eps;
1932 }
1933 }
1934 return (out_eps || in_eps) ? 0 : -ENOENT;
1935}
1936
1937/*
1938 * Detects the endpoints for one-port-per-endpoint protocols.
1939 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001940static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1941 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 int err, i;
Daniel Mack21af7d82010-06-16 17:57:29 +02001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1946 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1947 if (endpoints[i].out_ep)
1948 endpoints[i].out_cables = 0x0001;
1949 if (endpoints[i].in_ep)
1950 endpoints[i].in_cables = 0x0001;
1951 }
1952 return err;
1953}
1954
1955/*
1956 * Detects the endpoints and ports of Yamaha devices.
1957 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001958static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1959 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 struct usb_interface* intf;
1962 struct usb_host_interface *hostif;
1963 struct usb_interface_descriptor* intfd;
1964 uint8_t* cs_desc;
1965
1966 intf = umidi->iface;
1967 if (!intf)
1968 return -ENOENT;
1969 hostif = intf->altsetting;
1970 intfd = get_iface_desc(hostif);
1971 if (intfd->bNumEndpoints < 1)
1972 return -ENOENT;
1973
1974 /*
1975 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1976 * necessarily with any useful contents. So simply count 'em.
1977 */
1978 for (cs_desc = hostif->extra;
1979 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1980 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001981 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001982 if (cs_desc[2] == UAC_MIDI_IN_JACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
Daniel Mackde48c7b2010-02-22 23:49:13 +01001984 else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1986 }
1987 }
1988 if (!endpoint->in_cables && !endpoint->out_cables)
1989 return -ENOENT;
1990
1991 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1992}
1993
1994/*
1995 * Creates the endpoints and their ports for Midiman devices.
1996 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001997static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1998 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002000 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 struct usb_interface* intf;
2002 struct usb_host_interface *hostif;
2003 struct usb_interface_descriptor* intfd;
2004 struct usb_endpoint_descriptor* epd;
2005 int cable, err;
2006
2007 intf = umidi->iface;
2008 if (!intf)
2009 return -ENOENT;
2010 hostif = intf->altsetting;
2011 intfd = get_iface_desc(hostif);
2012 /*
2013 * The various MidiSport devices have more or less random endpoint
2014 * numbers, so we have to identify the endpoints by their index in
2015 * the descriptor array, like the driver for that other OS does.
2016 *
2017 * There is one interrupt input endpoint for all input ports, one
2018 * bulk output endpoint for even-numbered ports, and one for odd-
2019 * numbered ports. Both bulk output endpoints have corresponding
2020 * input bulk endpoints (at indices 1 and 3) which aren't used.
2021 */
2022 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
2023 snd_printdd(KERN_ERR "not enough endpoints\n");
2024 return -ENOENT;
2025 }
2026
2027 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002028 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
2030 return -ENXIO;
2031 }
2032 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002033 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
2035 return -ENXIO;
2036 }
2037 if (endpoint->out_cables > 0x0001) {
2038 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002039 if (!usb_endpoint_dir_out(epd) ||
2040 !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
2042 return -ENXIO;
2043 }
2044 }
2045
2046 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01002047 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 ep_info.out_cables = endpoint->out_cables & 0x5555;
2049 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
2050 if (err < 0)
2051 return err;
2052
2053 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2054 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
2055 ep_info.in_cables = endpoint->in_cables;
2056 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
2057 if (err < 0)
2058 return err;
2059
2060 if (endpoint->out_cables > 0x0001) {
2061 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2062 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
2063 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
2064 if (err < 0)
2065 return err;
2066 }
2067
2068 for (cable = 0; cable < 0x10; ++cable) {
2069 if (endpoint->out_cables & (1 << cable))
2070 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
2071 &umidi->endpoints[cable & 1].out->ports[cable].substream);
2072 if (endpoint->in_cables & (1 << cable))
2073 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
2074 &umidi->endpoints[0].in->ports[cable].substream);
2075 }
2076 return 0;
2077}
2078
Clemens Ladischa7b928a2006-05-02 16:22:12 +02002079static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
2080 .get_port_info = snd_usbmidi_get_port_info,
2081};
2082
Takashi Iwai86e07d32005-11-17 15:08:02 +01002083static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 int out_ports, int in_ports)
2085{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002086 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 int err;
2088
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002089 err = snd_rawmidi_new(umidi->card, "USB MIDI",
2090 umidi->next_midi_device++,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 out_ports, in_ports, &rmidi);
2092 if (err < 0)
2093 return err;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002094 strcpy(rmidi->name, umidi->card->shortname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2096 SNDRV_RAWMIDI_INFO_INPUT |
2097 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02002098 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 rmidi->private_data = umidi;
2100 rmidi->private_free = snd_usbmidi_rawmidi_free;
2101 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
2102 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
2103
2104 umidi->rmidi = rmidi;
2105 return 0;
2106}
2107
2108/*
2109 * Temporarily stop input.
2110 */
2111void snd_usbmidi_input_stop(struct list_head* p)
2112{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002113 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02002114 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Takashi Iwai86e07d32005-11-17 15:08:02 +01002116 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwai394c9d82012-12-03 11:30:50 +01002117 if (!umidi->input_running)
2118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002120 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02002122 for (j = 0; j < INPUT_URBS; ++j)
2123 usb_kill_urb(ep->in->urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
Takashi Iwai394c9d82012-12-03 11:30:50 +01002125 umidi->input_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Takashi Iwai86e07d32005-11-17 15:08:02 +01002128static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02002130 unsigned int i;
2131
2132 if (!ep)
2133 return;
2134 for (i = 0; i < INPUT_URBS; ++i) {
2135 struct urb* urb = ep->urbs[i];
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002136 urb->dev = ep->umidi->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
2138 }
2139}
2140
2141/*
2142 * Resume input after a call to snd_usbmidi_input_stop().
2143 */
2144void snd_usbmidi_input_start(struct list_head* p)
2145{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002146 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int i;
2148
Takashi Iwai86e07d32005-11-17 15:08:02 +01002149 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwai394c9d82012-12-03 11:30:50 +01002150 if (umidi->input_running || !umidi->opened[1])
2151 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2153 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
Takashi Iwai394c9d82012-12-03 11:30:50 +01002154 umidi->input_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
2157/*
2158 * Creates and registers everything needed for a MIDI streaming interface.
2159 */
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002160int snd_usbmidi_create(struct snd_card *card,
2161 struct usb_interface* iface,
2162 struct list_head *midi_list,
2163 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002165 struct snd_usb_midi* umidi;
2166 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int out_ports, in_ports;
2168 int i, err;
2169
Takashi Iwai561b2202005-09-09 14:22:34 +02002170 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (!umidi)
2172 return -ENOMEM;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002173 umidi->dev = interface_to_usbdev(iface);
2174 umidi->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 umidi->iface = iface;
2176 umidi->quirk = quirk;
2177 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02002178 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01002179 spin_lock_init(&umidi->disc_lock);
Takashi Iwai2ca77192012-12-03 11:12:46 +01002180 init_rwsem(&umidi->disc_rwsem);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02002181 mutex_init(&umidi->mutex);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002182 umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2183 le16_to_cpu(umidi->dev->descriptor.idProduct));
Clemens Ladischc8846972005-08-02 15:26:52 +02002184 umidi->error_timer.function = snd_usbmidi_error_timer;
2185 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 /* detect the endpoint(s) to use */
2188 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002189 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2190 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002192 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
Clemens Ladischd05cc10432007-05-07 09:28:53 +02002193 umidi->usb_protocol_ops =
2194 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002195 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02002196 case QUIRK_MIDI_US122L:
2197 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2198 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02002199 case QUIRK_MIDI_FIXED_ENDPOINT:
2200 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002201 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002202 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2203 break;
2204 case QUIRK_MIDI_YAMAHA:
2205 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2206 break;
2207 case QUIRK_MIDI_MIDIMAN:
2208 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2209 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002210 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002211 err = 0;
2212 break;
2213 case QUIRK_MIDI_NOVATION:
2214 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2215 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2216 break;
Clemens Ladischc7f57212010-10-22 18:20:48 +02002217 case QUIRK_MIDI_RAW_BYTES:
Clemens Ladischd1bda042005-09-14 08:36:03 +02002218 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02002219 /*
2220 * Interface 1 contains isochronous endpoints, but with the same
2221 * numbers as in interface 0. Since it is interface 1 that the
2222 * USB core has most recently seen, these descriptors are now
2223 * associated with the endpoint numbers. This will foul up our
2224 * attempts to submit bulk/interrupt URBs to the endpoints in
2225 * interface 0, so we have to make sure that the USB core looks
2226 * again at interface 0 by calling usb_set_interface() on it.
2227 */
Clemens Ladischc7f57212010-10-22 18:20:48 +02002228 if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2229 usb_set_interface(umidi->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002230 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2231 break;
2232 case QUIRK_MIDI_EMAGIC:
2233 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2234 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002235 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002236 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2237 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01002238 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02002239 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002240 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2241 break;
Krzysztof Foltman4434ade2010-05-20 20:31:10 +01002242 case QUIRK_MIDI_AKAI:
2243 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2244 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2245 /* endpoint 1 is input-only */
2246 endpoints[1].out_cables = 0;
2247 break;
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +02002248 case QUIRK_MIDI_FTDI:
2249 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2250
2251 /* set baud rate to 31250 (48 MHz / 16 / 96) */
2252 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2253 3, 0x40, 0x60, 0, NULL, 0, 1000);
2254 if (err < 0)
2255 break;
2256
2257 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2258 break;
Clemens Ladischd05e96b2015-11-15 22:38:29 +01002259 case QUIRK_MIDI_CH345:
Clemens Ladisch3d282cb2015-11-15 22:39:08 +01002260 umidi->usb_protocol_ops = &snd_usbmidi_ch345_broken_sysex_ops;
Clemens Ladischd05e96b2015-11-15 22:38:29 +01002261 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2262 break;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002263 default:
2264 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2265 err = -ENXIO;
2266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268 if (err < 0) {
2269 kfree(umidi);
2270 return err;
2271 }
2272
2273 /* create rawmidi device */
2274 out_ports = 0;
2275 in_ports = 0;
2276 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Akinobu Mitafbc54392009-11-20 14:56:52 +09002277 out_ports += hweight16(endpoints[i].out_cables);
2278 in_ports += hweight16(endpoints[i].in_cables);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2281 if (err < 0) {
2282 kfree(umidi);
2283 return err;
2284 }
2285
2286 /* create endpoint/port structures */
2287 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2288 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2289 else
2290 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2291 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return err;
2293 }
2294
Clemens Ladischd5928a12013-04-15 15:59:51 +02002295 usb_autopm_get_interface_no_resume(umidi->iface);
2296
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002297 list_add_tail(&umidi->list, midi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return 0;
2299}
2300
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002301EXPORT_SYMBOL(snd_usbmidi_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302EXPORT_SYMBOL(snd_usbmidi_input_stop);
2303EXPORT_SYMBOL(snd_usbmidi_input_start);
2304EXPORT_SYMBOL(snd_usbmidi_disconnect);