blob: eeefbce3873c11dc35a6e174c4ace79daeeaa8c1 [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;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200119 struct mutex mutex;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100120 u32 usb_id;
121 int next_midi_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100124 struct snd_usb_midi_out_endpoint *out;
125 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 } endpoints[MIDI_MAX_ENDPOINTS];
127 unsigned long input_triggered;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200128 unsigned int opened;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100129 unsigned char disconnected;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200130
131 struct snd_kcontrol *roland_load_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100135 struct snd_usb_midi* umidi;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200136 struct out_urb_context {
137 struct urb *urb;
138 struct snd_usb_midi_out_endpoint *ep;
139 } urbs[OUTPUT_URBS];
140 unsigned int active_urbs;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200141 unsigned int drain_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int max_transfer; /* size of urb buffer */
143 struct tasklet_struct tasklet;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200144 unsigned int next_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 spinlock_t buffer_lock;
146
147 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100148 struct snd_usb_midi_out_endpoint* ep;
149 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int active;
Clemens Ladische99ddfd2012-10-31 16:35:30 +0100151 bool autopm_reference;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 uint8_t cable; /* cable number << 4 */
153 uint8_t state;
154#define STATE_UNKNOWN 0
155#define STATE_1PARAM 1
156#define STATE_2PARAM_1 2
157#define STATE_2PARAM_2 3
158#define STATE_SYSEX_0 4
159#define STATE_SYSEX_1 5
160#define STATE_SYSEX_2 6
161 uint8_t data[2];
162 } ports[0x10];
163 int current_port;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200164
165 wait_queue_head_t drain_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
168struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100169 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200170 struct urb* urbs[INPUT_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100172 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200173 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200175 u8 seen_f5;
176 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int current_port;
178};
179
Takashi Iwai86e07d32005-11-17 15:08:02 +0100180static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182static const uint8_t snd_usbmidi_cin_length[] = {
183 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
184};
185
186/*
187 * Submits the URB, with error handling.
188 */
Al Viro55016f12005-10-21 03:21:58 -0400189static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 int err = usb_submit_urb(urb, flags);
192 if (err < 0 && err != -ENODEV)
193 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
194 return err;
195}
196
197/*
198 * Error handling for URB completion functions.
199 */
200static int snd_usbmidi_urb_error(int status)
201{
Clemens Ladischc8846972005-08-02 15:26:52 +0200202 switch (status) {
203 /* manually unlinked, or device gone */
204 case -ENOENT:
205 case -ECONNRESET:
206 case -ESHUTDOWN:
207 case -ENODEV:
208 return -ENODEV;
209 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700210 case -EPROTO:
211 case -ETIME:
212 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200213 return -EIO;
214 default:
215 snd_printk(KERN_ERR "urb status %d\n", status);
216 return 0; /* continue */
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220/*
221 * Receives a chunk of MIDI data.
222 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100223static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 uint8_t* data, int length)
225{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100226 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (!port->substream) {
229 snd_printd("unexpected port %d!\n", portidx);
230 return;
231 }
232 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
233 return;
234 snd_rawmidi_receive(port->substream, data, length);
235}
236
237#ifdef DUMP_PACKETS
238static void dump_urb(const char *type, const u8 *data, int length)
239{
240 snd_printk(KERN_DEBUG "%s packet: [", type);
241 for (; length > 0; ++data, --length)
242 printk(" %02x", *data);
243 printk(" ]\n");
244}
245#else
246#define dump_urb(type, data, length) /* nothing */
247#endif
248
249/*
250 * Processes the data read from the device.
251 */
David Howells7d12e782006-10-05 14:55:46 +0100252static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100254 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if (urb->status == 0) {
257 dump_urb("received", urb->transfer_buffer, urb->actual_length);
258 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
259 urb->actual_length);
260 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200261 int err = snd_usbmidi_urb_error(urb->status);
262 if (err < 0) {
263 if (err != -ENODEV) {
264 ep->error_resubmit = 1;
265 mod_timer(&ep->umidi->error_timer,
266 jiffies + ERROR_DELAY_JIFFIES);
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100272 urb->dev = ep->umidi->dev;
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200273 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
David Howells7d12e782006-10-05 14:55:46 +0100276static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200278 struct out_urb_context *context = urb->context;
279 struct snd_usb_midi_out_endpoint* ep = context->ep;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200280 unsigned int urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 spin_lock(&ep->buffer_lock);
Clemens Ladischa65dd992009-07-13 13:48:36 +0200283 urb_index = context - ep->urbs;
284 ep->active_urbs &= ~(1 << urb_index);
285 if (unlikely(ep->drain_urbs)) {
286 ep->drain_urbs &= ~(1 << urb_index);
287 wake_up(&ep->drain_wait);
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 spin_unlock(&ep->buffer_lock);
290 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200291 int err = snd_usbmidi_urb_error(urb->status);
292 if (err < 0) {
293 if (err != -ENODEV)
294 mod_timer(&ep->umidi->error_timer,
295 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 snd_usbmidi_do_output(ep);
300}
301
302/*
303 * This is called when some data should be transferred to the device
304 * (from one or more substreams).
305 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100306static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200308 unsigned int urb_index;
309 struct urb* urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unsigned long flags;
311
312 spin_lock_irqsave(&ep->buffer_lock, flags);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100313 if (ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 spin_unlock_irqrestore(&ep->buffer_lock, flags);
315 return;
316 }
317
Clemens Ladischa65dd992009-07-13 13:48:36 +0200318 urb_index = ep->next_urb;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200319 for (;;) {
Clemens Ladischa65dd992009-07-13 13:48:36 +0200320 if (!(ep->active_urbs & (1 << urb_index))) {
321 urb = ep->urbs[urb_index].urb;
322 urb->transfer_buffer_length = 0;
323 ep->umidi->usb_protocol_ops->output(ep, urb);
324 if (urb->transfer_buffer_length == 0)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Clemens Ladischa65dd992009-07-13 13:48:36 +0200327 dump_urb("sending", urb->transfer_buffer,
328 urb->transfer_buffer_length);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100329 urb->dev = ep->umidi->dev;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200330 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
331 break;
332 ep->active_urbs |= 1 << urb_index;
333 }
334 if (++urb_index >= OUTPUT_URBS)
335 urb_index = 0;
336 if (urb_index == ep->next_urb)
Clemens Ladisched4affa2009-07-13 13:43:59 +0200337 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Clemens Ladischa65dd992009-07-13 13:48:36 +0200339 ep->next_urb = urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 spin_unlock_irqrestore(&ep->buffer_lock, flags);
341}
342
343static void snd_usbmidi_out_tasklet(unsigned long data)
344{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100345 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 snd_usbmidi_do_output(ep);
348}
349
Clemens Ladischc8846972005-08-02 15:26:52 +0200350/* called after transfers had been interrupted due to some USB error */
351static void snd_usbmidi_error_timer(unsigned long data)
352{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100353 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200354 unsigned int i, j;
Clemens Ladischc8846972005-08-02 15:26:52 +0200355
Takashi Iwaic0792e02008-02-22 18:34:44 +0100356 spin_lock(&umidi->disc_lock);
357 if (umidi->disconnected) {
358 spin_unlock(&umidi->disc_lock);
359 return;
360 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200361 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100362 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200363 if (in && in->error_resubmit) {
364 in->error_resubmit = 0;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200365 for (j = 0; j < INPUT_URBS; ++j) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100366 in->urbs[j]->dev = umidi->dev;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200367 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
368 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200369 }
370 if (umidi->endpoints[i].out)
371 snd_usbmidi_do_output(umidi->endpoints[i].out);
372 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100373 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100377static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 const void *data, int len)
379{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200380 int err = 0;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700381 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!buf)
383 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dump_urb("sending", buf, len);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200385 if (ep->urbs[0].urb)
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100386 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200387 buf, len, NULL, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 kfree(buf);
389 return err;
390}
391
392/*
393 * Standard USB MIDI protocol: see the spec.
394 * Midiman protocol: like the standard protocol, but the control byte is the
395 * fourth byte in each packet, and uses length instead of CIN.
396 */
397
Takashi Iwai86e07d32005-11-17 15:08:02 +0100398static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 uint8_t* buffer, int buffer_length)
400{
401 int i;
402
403 for (i = 0; i + 3 < buffer_length; i += 4)
404 if (buffer[i] != 0) {
405 int cable = buffer[i] >> 4;
406 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
407 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
408 }
409}
410
Takashi Iwai86e07d32005-11-17 15:08:02 +0100411static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 uint8_t* buffer, int buffer_length)
413{
414 int i;
415
416 for (i = 0; i + 3 < buffer_length; i += 4)
417 if (buffer[i + 3] != 0) {
418 int port = buffer[i + 3] >> 4;
419 int length = buffer[i + 3] & 3;
420 snd_usbmidi_input_data(ep, port, &buffer[i], length);
421 }
422}
423
424/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200425 * Buggy M-Audio device: running status on input results in a packet that has
426 * the data bytes but not the status byte and that is marked with CIN 4.
427 */
428static void snd_usbmidi_maudio_broken_running_status_input(
429 struct snd_usb_midi_in_endpoint* ep,
430 uint8_t* buffer, int buffer_length)
431{
432 int i;
433
434 for (i = 0; i + 3 < buffer_length; i += 4)
435 if (buffer[i] != 0) {
436 int cable = buffer[i] >> 4;
437 u8 cin = buffer[i] & 0x0f;
438 struct usbmidi_in_port *port = &ep->ports[cable];
439 int length;
Daniel Mack21af7d82010-06-16 17:57:29 +0200440
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200441 length = snd_usbmidi_cin_length[cin];
442 if (cin == 0xf && buffer[i + 1] >= 0xf8)
443 ; /* realtime msg: no running status change */
444 else if (cin >= 0x8 && cin <= 0xe)
445 /* channel msg */
446 port->running_status_length = length - 1;
447 else if (cin == 0x4 &&
448 port->running_status_length != 0 &&
449 buffer[i + 1] < 0x80)
450 /* CIN 4 that is not a SysEx */
451 length = port->running_status_length;
452 else
453 /*
454 * All other msgs cannot begin running status.
455 * (A channel msg sent as two or three CIN 0xF
456 * packets could in theory, but this device
457 * doesn't use this format.)
458 */
459 port->running_status_length = 0;
460 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
461 }
462}
463
464/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200465 * CME protocol: like the standard protocol, but SysEx commands are sent as a
466 * single USB packet preceded by a 0x0F byte.
467 */
468static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
469 uint8_t *buffer, int buffer_length)
470{
471 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
472 snd_usbmidi_standard_input(ep, buffer, buffer_length);
473 else
474 snd_usbmidi_input_data(ep, buffer[0] >> 4,
475 &buffer[1], buffer_length - 1);
476}
477
478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * Adds one USB MIDI packet to the output buffer.
480 */
481static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
482 uint8_t p1, uint8_t p2, uint8_t p3)
483{
484
485 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
486 buf[0] = p0;
487 buf[1] = p1;
488 buf[2] = p2;
489 buf[3] = p3;
490 urb->transfer_buffer_length += 4;
491}
492
493/*
494 * Adds one Midiman packet to the output buffer.
495 */
496static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
497 uint8_t p1, uint8_t p2, uint8_t p3)
498{
499
500 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
501 buf[0] = p1;
502 buf[1] = p2;
503 buf[2] = p3;
504 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
505 urb->transfer_buffer_length += 4;
506}
507
508/*
509 * Converts MIDI commands to USB MIDI packets.
510 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100511static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 uint8_t b, struct urb* urb)
513{
514 uint8_t p0 = port->cable;
515 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
516 port->ep->umidi->usb_protocol_ops->output_packet;
517
518 if (b >= 0xf8) {
519 output_packet(urb, p0 | 0x0f, b, 0, 0);
520 } else if (b >= 0xf0) {
521 switch (b) {
522 case 0xf0:
523 port->data[0] = b;
524 port->state = STATE_SYSEX_1;
525 break;
526 case 0xf1:
527 case 0xf3:
528 port->data[0] = b;
529 port->state = STATE_1PARAM;
530 break;
531 case 0xf2:
532 port->data[0] = b;
533 port->state = STATE_2PARAM_1;
534 break;
535 case 0xf4:
536 case 0xf5:
537 port->state = STATE_UNKNOWN;
538 break;
539 case 0xf6:
540 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
541 port->state = STATE_UNKNOWN;
542 break;
543 case 0xf7:
544 switch (port->state) {
545 case STATE_SYSEX_0:
546 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
547 break;
548 case STATE_SYSEX_1:
549 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
550 break;
551 case STATE_SYSEX_2:
552 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
553 break;
554 }
555 port->state = STATE_UNKNOWN;
556 break;
557 }
558 } else if (b >= 0x80) {
559 port->data[0] = b;
560 if (b >= 0xc0 && b <= 0xdf)
561 port->state = STATE_1PARAM;
562 else
563 port->state = STATE_2PARAM_1;
564 } else { /* b < 0x80 */
565 switch (port->state) {
566 case STATE_1PARAM:
567 if (port->data[0] < 0xf0) {
568 p0 |= port->data[0] >> 4;
569 } else {
570 p0 |= 0x02;
571 port->state = STATE_UNKNOWN;
572 }
573 output_packet(urb, p0, port->data[0], b, 0);
574 break;
575 case STATE_2PARAM_1:
576 port->data[1] = b;
577 port->state = STATE_2PARAM_2;
578 break;
579 case STATE_2PARAM_2:
580 if (port->data[0] < 0xf0) {
581 p0 |= port->data[0] >> 4;
582 port->state = STATE_2PARAM_1;
583 } else {
584 p0 |= 0x03;
585 port->state = STATE_UNKNOWN;
586 }
587 output_packet(urb, p0, port->data[0], port->data[1], b);
588 break;
589 case STATE_SYSEX_0:
590 port->data[0] = b;
591 port->state = STATE_SYSEX_1;
592 break;
593 case STATE_SYSEX_1:
594 port->data[1] = b;
595 port->state = STATE_SYSEX_2;
596 break;
597 case STATE_SYSEX_2:
598 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
599 port->state = STATE_SYSEX_0;
600 break;
601 }
602 }
603}
604
Clemens Ladisched4affa2009-07-13 13:43:59 +0200605static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
606 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int p;
609
610 /* FIXME: lower-numbered ports can starve higher-numbered ports */
611 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100612 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!port->active)
614 continue;
615 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
616 uint8_t b;
617 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
618 port->active = 0;
619 break;
620 }
621 snd_usbmidi_transmit_byte(port, b, urb);
622 }
623 }
624}
625
626static struct usb_protocol_ops snd_usbmidi_standard_ops = {
627 .input = snd_usbmidi_standard_input,
628 .output = snd_usbmidi_standard_output,
629 .output_packet = snd_usbmidi_output_standard_packet,
630};
631
632static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
633 .input = snd_usbmidi_midiman_input,
Daniel Mack21af7d82010-06-16 17:57:29 +0200634 .output = snd_usbmidi_standard_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 .output_packet = snd_usbmidi_output_midiman_packet,
636};
637
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200638static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
639 .input = snd_usbmidi_maudio_broken_running_status_input,
Daniel Mack21af7d82010-06-16 17:57:29 +0200640 .output = snd_usbmidi_standard_output,
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200641 .output_packet = snd_usbmidi_output_standard_packet,
642};
643
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200644static struct usb_protocol_ops snd_usbmidi_cme_ops = {
645 .input = snd_usbmidi_cme_input,
646 .output = snd_usbmidi_standard_output,
647 .output_packet = snd_usbmidi_output_standard_packet,
648};
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/*
Krzysztof Foltman4434ade2010-05-20 20:31:10 +0100651 * AKAI MPD16 protocol:
652 *
653 * For control port (endpoint 1):
654 * ==============================
655 * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
656 * SysEx message (msg_len=9 bytes long).
657 *
658 * For data port (endpoint 2):
659 * ===========================
660 * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
661 * MIDI message (msg_len bytes long)
662 *
663 * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
664 */
665static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
666 uint8_t *buffer, int buffer_length)
667{
668 unsigned int pos = 0;
669 unsigned int len = (unsigned int)buffer_length;
670 while (pos < len) {
671 unsigned int port = (buffer[pos] >> 4) - 1;
672 unsigned int msg_len = buffer[pos] & 0x0f;
673 pos++;
674 if (pos + msg_len <= len && port < 2)
675 snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
676 pos += msg_len;
677 }
678}
679
680#define MAX_AKAI_SYSEX_LEN 9
681
682static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
683 struct urb *urb)
684{
685 uint8_t *msg;
686 int pos, end, count, buf_end;
687 uint8_t tmp[MAX_AKAI_SYSEX_LEN];
688 struct snd_rawmidi_substream *substream = ep->ports[0].substream;
689
690 if (!ep->ports[0].active)
691 return;
692
693 msg = urb->transfer_buffer + urb->transfer_buffer_length;
694 buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
695
696 /* only try adding more data when there's space for at least 1 SysEx */
697 while (urb->transfer_buffer_length < buf_end) {
698 count = snd_rawmidi_transmit_peek(substream,
699 tmp, MAX_AKAI_SYSEX_LEN);
700 if (!count) {
701 ep->ports[0].active = 0;
702 return;
703 }
704 /* try to skip non-SysEx data */
705 for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
706 ;
707
708 if (pos > 0) {
709 snd_rawmidi_transmit_ack(substream, pos);
710 continue;
711 }
712
713 /* look for the start or end marker */
714 for (end = 1; end < count && tmp[end] < 0xF0; end++)
715 ;
716
717 /* next SysEx started before the end of current one */
718 if (end < count && tmp[end] == 0xF0) {
719 /* it's incomplete - drop it */
720 snd_rawmidi_transmit_ack(substream, end);
721 continue;
722 }
723 /* SysEx complete */
724 if (end < count && tmp[end] == 0xF7) {
725 /* queue it, ack it, and get the next one */
726 count = end + 1;
727 msg[0] = 0x10 | count;
728 memcpy(&msg[1], tmp, count);
729 snd_rawmidi_transmit_ack(substream, count);
730 urb->transfer_buffer_length += count + 1;
731 msg += count + 1;
732 continue;
733 }
734 /* less than 9 bytes and no end byte - wait for more */
735 if (count < MAX_AKAI_SYSEX_LEN) {
736 ep->ports[0].active = 0;
737 return;
738 }
739 /* 9 bytes and no end marker in sight - malformed, skip it */
740 snd_rawmidi_transmit_ack(substream, count);
741 }
742}
743
744static struct usb_protocol_ops snd_usbmidi_akai_ops = {
745 .input = snd_usbmidi_akai_input,
746 .output = snd_usbmidi_akai_output,
747};
748
749/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * Novation USB MIDI protocol: number of data bytes is in the first byte
751 * (when receiving) (+1!) or in the second byte (when sending); data begins
752 * at the third byte.
753 */
754
Takashi Iwai86e07d32005-11-17 15:08:02 +0100755static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 uint8_t* buffer, int buffer_length)
757{
758 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
759 return;
760 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
761}
762
Clemens Ladisched4affa2009-07-13 13:43:59 +0200763static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
764 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 uint8_t* transfer_buffer;
767 int count;
768
769 if (!ep->ports[0].active)
770 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200771 transfer_buffer = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 count = snd_rawmidi_transmit(ep->ports[0].substream,
773 &transfer_buffer[2],
774 ep->max_transfer - 2);
775 if (count < 1) {
776 ep->ports[0].active = 0;
777 return;
778 }
779 transfer_buffer[0] = 0;
780 transfer_buffer[1] = count;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200781 urb->transfer_buffer_length = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784static struct usb_protocol_ops snd_usbmidi_novation_ops = {
785 .input = snd_usbmidi_novation_input,
786 .output = snd_usbmidi_novation_output,
787};
788
789/*
Clemens Ladischc7f57212010-10-22 18:20:48 +0200790 * "raw" protocol: just move raw MIDI bytes from/to the endpoint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
792
Takashi Iwai86e07d32005-11-17 15:08:02 +0100793static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200794 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
797}
798
Clemens Ladisched4affa2009-07-13 13:43:59 +0200799static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
800 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 int count;
803
804 if (!ep->ports[0].active)
805 return;
806 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200807 urb->transfer_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ep->max_transfer);
809 if (count < 1) {
810 ep->ports[0].active = 0;
811 return;
812 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200813 urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200816static struct usb_protocol_ops snd_usbmidi_raw_ops = {
817 .input = snd_usbmidi_raw_input,
818 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819};
820
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +0200821/*
822 * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
823 */
824
825static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint* ep,
826 uint8_t* buffer, int buffer_length)
827{
828 if (buffer_length > 2)
829 snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
830}
831
832static struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
833 .input = snd_usbmidi_ftdi_input,
834 .output = snd_usbmidi_raw_output,
835};
836
Karsten Wiese030a07e2008-07-30 15:13:29 +0200837static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
838 uint8_t *buffer, int buffer_length)
839{
840 if (buffer_length != 9)
841 return;
842 buffer_length = 8;
843 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
844 buffer_length--;
845 if (buffer_length)
846 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
847}
848
Clemens Ladisched4affa2009-07-13 13:43:59 +0200849static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
850 struct urb *urb)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200851{
852 int count;
853
854 if (!ep->ports[0].active)
855 return;
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700856 switch (snd_usb_get_speed(ep->umidi->dev)) {
857 case USB_SPEED_HIGH:
858 case USB_SPEED_SUPER:
859 count = 1;
860 break;
861 default:
862 count = 2;
863 }
Karsten Wiese030a07e2008-07-30 15:13:29 +0200864 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200865 urb->transfer_buffer,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200866 count);
867 if (count < 1) {
868 ep->ports[0].active = 0;
869 return;
870 }
871
Karsten Wiese921eebd2011-01-03 02:41:58 +0100872 memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
873 urb->transfer_buffer_length = ep->max_transfer;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200874}
875
876static struct usb_protocol_ops snd_usbmidi_122l_ops = {
877 .input = snd_usbmidi_us122l_input,
878 .output = snd_usbmidi_us122l_output,
879};
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/*
882 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
883 */
884
Takashi Iwai86e07d32005-11-17 15:08:02 +0100885static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 static const u8 init_data[] = {
888 /* initialization magic: "get version" */
889 0xf0,
890 0x00, 0x20, 0x31, /* Emagic */
891 0x64, /* Unitor8 */
892 0x0b, /* version number request */
893 0x00, /* command version */
894 0x00, /* EEPROM, box 0 */
895 0xf7
896 };
897 send_bulk_static_data(ep, init_data, sizeof(init_data));
898 /* while we're at it, pour on more magic */
899 send_bulk_static_data(ep, init_data, sizeof(init_data));
900}
901
Takashi Iwai86e07d32005-11-17 15:08:02 +0100902static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 static const u8 finish_data[] = {
905 /* switch to patch mode with last preset */
906 0xf0,
907 0x00, 0x20, 0x31, /* Emagic */
908 0x64, /* Unitor8 */
909 0x10, /* patch switch command */
910 0x00, /* command version */
911 0x7f, /* to all boxes */
912 0x40, /* last preset in EEPROM */
913 0xf7
914 };
915 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
916}
917
Takashi Iwai86e07d32005-11-17 15:08:02 +0100918static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 uint8_t* buffer, int buffer_length)
920{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200921 int i;
922
923 /* FF indicates end of valid data */
924 for (i = 0; i < buffer_length; ++i)
925 if (buffer[i] == 0xff) {
926 buffer_length = i;
927 break;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 /* handle F5 at end of last buffer */
931 if (ep->seen_f5)
932 goto switch_port;
933
934 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* determine size of data until next F5 */
936 for (i = 0; i < buffer_length; ++i)
937 if (buffer[i] == 0xf5)
938 break;
939 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
940 buffer += i;
941 buffer_length -= i;
942
943 if (buffer_length <= 0)
944 break;
945 /* assert(buffer[0] == 0xf5); */
946 ep->seen_f5 = 1;
947 ++buffer;
948 --buffer_length;
949
950 switch_port:
951 if (buffer_length <= 0)
952 break;
953 if (buffer[0] < 0x80) {
954 ep->current_port = (buffer[0] - 1) & 15;
955 ++buffer;
956 --buffer_length;
957 }
958 ep->seen_f5 = 0;
959 }
960}
961
Clemens Ladisched4affa2009-07-13 13:43:59 +0200962static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
963 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 int port0 = ep->current_port;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200966 uint8_t* buf = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int buf_free = ep->max_transfer;
968 int length, i;
969
970 for (i = 0; i < 0x10; ++i) {
971 /* round-robin, starting at the last current port */
972 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100973 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (!port->active)
976 continue;
977 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
978 port->active = 0;
979 continue;
980 }
981
982 if (portnum != ep->current_port) {
983 if (buf_free < 2)
984 break;
985 ep->current_port = portnum;
986 buf[0] = 0xf5;
987 buf[1] = (portnum + 1) & 15;
988 buf += 2;
989 buf_free -= 2;
990 }
991
992 if (buf_free < 1)
993 break;
994 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
995 if (length > 0) {
996 buf += length;
997 buf_free -= length;
998 if (buf_free < 1)
999 break;
1000 }
1001 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +02001002 if (buf_free < ep->max_transfer && buf_free > 0) {
1003 *buf = 0xff;
1004 --buf_free;
1005 }
Clemens Ladisched4affa2009-07-13 13:43:59 +02001006 urb->transfer_buffer_length = ep->max_transfer - buf_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
1010 .input = snd_usbmidi_emagic_input,
1011 .output = snd_usbmidi_emagic_output,
1012 .init_out_endpoint = snd_usbmidi_emagic_init_out,
1013 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
1014};
1015
1016
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001017static void update_roland_altsetting(struct snd_usb_midi* umidi)
1018{
1019 struct usb_interface *intf;
1020 struct usb_host_interface *hostif;
1021 struct usb_interface_descriptor *intfd;
1022 int is_light_load;
1023
1024 intf = umidi->iface;
1025 is_light_load = intf->cur_altsetting != intf->altsetting;
1026 if (umidi->roland_load_ctl->private_value == is_light_load)
1027 return;
1028 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
1029 intfd = get_iface_desc(hostif);
1030 snd_usbmidi_input_stop(&umidi->list);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001031 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001032 intfd->bAlternateSetting);
1033 snd_usbmidi_input_start(&umidi->list);
1034}
1035
1036static void substream_open(struct snd_rawmidi_substream *substream, int open)
1037{
1038 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1039 struct snd_kcontrol *ctl;
1040
1041 mutex_lock(&umidi->mutex);
1042 if (open) {
1043 if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
1044 ctl = umidi->roland_load_ctl;
1045 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001046 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001047 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1048 update_roland_altsetting(umidi);
1049 }
1050 } else {
1051 if (--umidi->opened == 0 && umidi->roland_load_ctl) {
1052 ctl = umidi->roland_load_ctl;
1053 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001054 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001055 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1056 }
1057 }
1058 mutex_unlock(&umidi->mutex);
1059}
1060
Takashi Iwai86e07d32005-11-17 15:08:02 +01001061static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001063 struct snd_usb_midi* umidi = substream->rmidi->private_data;
1064 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 int i, j;
Oliver Neukum88a85162011-03-11 14:51:12 +01001066 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1069 if (umidi->endpoints[i].out)
1070 for (j = 0; j < 0x10; ++j)
1071 if (umidi->endpoints[i].out->ports[j].substream == substream) {
1072 port = &umidi->endpoints[i].out->ports[j];
1073 break;
1074 }
1075 if (!port) {
1076 snd_BUG();
1077 return -ENXIO;
1078 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001079 err = usb_autopm_get_interface(umidi->iface);
Clemens Ladische99ddfd2012-10-31 16:35:30 +01001080 port->autopm_reference = err >= 0;
1081 if (err < 0 && err != -EACCES)
Oliver Neukum88a85162011-03-11 14:51:12 +01001082 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 substream->runtime->private_data = port;
1084 port->state = STATE_UNKNOWN;
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001085 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return 0;
1087}
1088
Takashi Iwai86e07d32005-11-17 15:08:02 +01001089static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Oliver Neukum88a85162011-03-11 14:51:12 +01001091 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Clemens Ladische99ddfd2012-10-31 16:35:30 +01001092 struct usbmidi_out_port *port = substream->runtime->private_data;
Oliver Neukum88a85162011-03-11 14:51:12 +01001093
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001094 substream_open(substream, 0);
Clemens Ladische99ddfd2012-10-31 16:35:30 +01001095 if (port->autopm_reference)
1096 usb_autopm_put_interface(umidi->iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0;
1098}
1099
Takashi Iwai86e07d32005-11-17 15:08:02 +01001100static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001102 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 port->active = up;
1105 if (up) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001106 if (port->ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /* gobble up remaining bytes to prevent wait in
1108 * snd_rawmidi_drain_output */
1109 while (!snd_rawmidi_transmit_empty(substream))
1110 snd_rawmidi_transmit_ack(substream, 1);
1111 return;
1112 }
Takashi Iwai1f041282008-12-18 12:17:55 +01001113 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115}
1116
Clemens Ladischa65dd992009-07-13 13:48:36 +02001117static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
1118{
1119 struct usbmidi_out_port* port = substream->runtime->private_data;
1120 struct snd_usb_midi_out_endpoint *ep = port->ep;
1121 unsigned int drain_urbs;
1122 DEFINE_WAIT(wait);
1123 long timeout = msecs_to_jiffies(50);
1124
Takashi Iwai29aac002010-04-10 21:27:23 +02001125 if (ep->umidi->disconnected)
1126 return;
Clemens Ladischa65dd992009-07-13 13:48:36 +02001127 /*
1128 * The substream buffer is empty, but some data might still be in the
1129 * currently active URBs, so we have to wait for those to complete.
1130 */
1131 spin_lock_irq(&ep->buffer_lock);
1132 drain_urbs = ep->active_urbs;
1133 if (drain_urbs) {
1134 ep->drain_urbs |= drain_urbs;
1135 do {
1136 prepare_to_wait(&ep->drain_wait, &wait,
1137 TASK_UNINTERRUPTIBLE);
1138 spin_unlock_irq(&ep->buffer_lock);
1139 timeout = schedule_timeout(timeout);
1140 spin_lock_irq(&ep->buffer_lock);
1141 drain_urbs &= ep->drain_urbs;
1142 } while (drain_urbs && timeout);
1143 finish_wait(&ep->drain_wait, &wait);
1144 }
1145 spin_unlock_irq(&ep->buffer_lock);
1146}
1147
Takashi Iwai86e07d32005-11-17 15:08:02 +01001148static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001150 substream_open(substream, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 0;
1152}
1153
Takashi Iwai86e07d32005-11-17 15:08:02 +01001154static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001156 substream_open(substream, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return 0;
1158}
1159
Takashi Iwai86e07d32005-11-17 15:08:02 +01001160static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001162 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (up)
1165 set_bit(substream->number, &umidi->input_triggered);
1166 else
1167 clear_bit(substream->number, &umidi->input_triggered);
1168}
1169
Takashi Iwai86e07d32005-11-17 15:08:02 +01001170static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 .open = snd_usbmidi_output_open,
1172 .close = snd_usbmidi_output_close,
1173 .trigger = snd_usbmidi_output_trigger,
Clemens Ladischa65dd992009-07-13 13:48:36 +02001174 .drain = snd_usbmidi_output_drain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175};
1176
Takashi Iwai86e07d32005-11-17 15:08:02 +01001177static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 .open = snd_usbmidi_input_open,
1179 .close = snd_usbmidi_input_close,
1180 .trigger = snd_usbmidi_input_trigger
1181};
1182
Clemens Ladisched4affa2009-07-13 13:43:59 +02001183static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1184 unsigned int buffer_length)
1185{
Daniel Mack997ea582010-04-12 13:17:25 +02001186 usb_free_coherent(umidi->dev, buffer_length,
1187 urb->transfer_buffer, urb->transfer_dma);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001188 usb_free_urb(urb);
1189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191/*
1192 * Frees an input endpoint.
1193 * May be called when ep hasn't been initialized completely.
1194 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001195static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001197 unsigned int i;
1198
Clemens Ladisched4affa2009-07-13 13:43:59 +02001199 for (i = 0; i < INPUT_URBS; ++i)
1200 if (ep->urbs[i])
1201 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1202 ep->urbs[i]->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 kfree(ep);
1204}
1205
1206/*
1207 * Creates an input endpoint.
1208 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001209static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1210 struct snd_usb_midi_endpoint_info* ep_info,
1211 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001213 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 void* buffer;
1215 unsigned int pipe;
1216 int length;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001217 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001220 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (!ep)
1222 return -ENOMEM;
1223 ep->umidi = umidi;
1224
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001225 for (i = 0; i < INPUT_URBS; ++i) {
1226 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1227 if (!ep->urbs[i]) {
1228 snd_usbmidi_in_endpoint_delete(ep);
1229 return -ENOMEM;
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001233 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001235 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1236 length = usb_maxpacket(umidi->dev, pipe, 0);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001237 for (i = 0; i < INPUT_URBS; ++i) {
Daniel Mack997ea582010-04-12 13:17:25 +02001238 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1239 &ep->urbs[i]->transfer_dma);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001240 if (!buffer) {
1241 snd_usbmidi_in_endpoint_delete(ep);
1242 return -ENOMEM;
1243 }
1244 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001245 usb_fill_int_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001246 pipe, buffer, length,
1247 snd_usbmidi_in_urb_complete,
1248 ep, ep_info->in_interval);
1249 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001250 usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001251 pipe, buffer, length,
1252 snd_usbmidi_in_urb_complete, ep);
1253 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 rep->in = ep;
1257 return 0;
1258}
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260/*
1261 * Frees an output endpoint.
1262 * May be called when ep hasn't been initialized completely.
1263 */
Takashi Iwai29aac002010-04-10 21:27:23 +02001264static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Clemens Ladisched4affa2009-07-13 13:43:59 +02001266 unsigned int i;
1267
1268 for (i = 0; i < OUTPUT_URBS; ++i)
Takashi Iwai29aac002010-04-10 21:27:23 +02001269 if (ep->urbs[i].urb) {
Clemens Ladisched4affa2009-07-13 13:43:59 +02001270 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1271 ep->max_transfer);
Takashi Iwai29aac002010-04-10 21:27:23 +02001272 ep->urbs[i].urb = NULL;
1273 }
1274}
1275
1276static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1277{
1278 snd_usbmidi_out_endpoint_clear(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 kfree(ep);
1280}
1281
1282/*
1283 * Creates an output endpoint, and initializes output ports.
1284 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001285static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1286 struct snd_usb_midi_endpoint_info* ep_info,
Daniel Mack21af7d82010-06-16 17:57:29 +02001287 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001289 struct snd_usb_midi_out_endpoint* ep;
Clemens Ladisched4affa2009-07-13 13:43:59 +02001290 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 unsigned int pipe;
1292 void* buffer;
1293
1294 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001295 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (!ep)
1297 return -ENOMEM;
1298 ep->umidi = umidi;
1299
Clemens Ladisched4affa2009-07-13 13:43:59 +02001300 for (i = 0; i < OUTPUT_URBS; ++i) {
1301 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1302 if (!ep->urbs[i].urb) {
1303 snd_usbmidi_out_endpoint_delete(ep);
1304 return -ENOMEM;
1305 }
1306 ep->urbs[i].ep = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001308 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001309 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001310 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001311 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischf167e1d072010-02-15 08:55:28 +01001312 switch (umidi->usb_id) {
1313 default:
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001314 ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
Clemens Ladischf167e1d072010-02-15 08:55:28 +01001315 break;
1316 /*
1317 * Various chips declare a packet size larger than 4 bytes, but
1318 * do not actually work with larger packets:
1319 */
1320 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1321 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1322 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1323 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1324 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
Tarek Soliman49c039f2011-04-04 09:23:53 -05001325 case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
Clemens Ladischf167e1d072010-02-15 08:55:28 +01001326 ep->max_transfer = 4;
1327 break;
Karsten Wiese921eebd2011-01-03 02:41:58 +01001328 /*
1329 * Some devices only work with 9 bytes packet size:
1330 */
1331 case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
1332 case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
1333 ep->max_transfer = 9;
1334 break;
Clemens Ladischf167e1d072010-02-15 08:55:28 +01001335 }
Clemens Ladisched4affa2009-07-13 13:43:59 +02001336 for (i = 0; i < OUTPUT_URBS; ++i) {
Daniel Mack997ea582010-04-12 13:17:25 +02001337 buffer = usb_alloc_coherent(umidi->dev,
1338 ep->max_transfer, GFP_KERNEL,
1339 &ep->urbs[i].urb->transfer_dma);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001340 if (!buffer) {
1341 snd_usbmidi_out_endpoint_delete(ep);
1342 return -ENOMEM;
1343 }
1344 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001345 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001346 pipe, buffer, ep->max_transfer,
1347 snd_usbmidi_out_urb_complete,
1348 &ep->urbs[i], ep_info->out_interval);
1349 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001350 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001351 pipe, buffer, ep->max_transfer,
1352 snd_usbmidi_out_urb_complete,
1353 &ep->urbs[i]);
1354 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 spin_lock_init(&ep->buffer_lock);
1358 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
Clemens Ladischa65dd992009-07-13 13:48:36 +02001359 init_waitqueue_head(&ep->drain_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 for (i = 0; i < 0x10; ++i)
1362 if (ep_info->out_cables & (1 << i)) {
1363 ep->ports[i].ep = ep;
1364 ep->ports[i].cable = i << 4;
1365 }
1366
1367 if (umidi->usb_protocol_ops->init_out_endpoint)
1368 umidi->usb_protocol_ops->init_out_endpoint(ep);
1369
1370 rep->out = ep;
1371 return 0;
1372}
1373
1374/*
1375 * Frees everything.
1376 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001377static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 int i;
1380
1381 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001382 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (ep->out)
1384 snd_usbmidi_out_endpoint_delete(ep->out);
1385 if (ep->in)
1386 snd_usbmidi_in_endpoint_delete(ep->in);
1387 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001388 mutex_destroy(&umidi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 kfree(umidi);
1390}
1391
1392/*
1393 * Unlinks all URBs (must be done before the usb_device is deleted).
1394 */
Clemens Ladischee733392005-04-25 10:34:13 +02001395void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001397 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001398 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Takashi Iwai86e07d32005-11-17 15:08:02 +01001400 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001401 /*
1402 * an URB's completion handler may start the timer and
1403 * a timer may submit an URB. To reliably break the cycle
1404 * a flag under lock must be used
1405 */
1406 spin_lock_irq(&umidi->disc_lock);
1407 umidi->disconnected = 1;
1408 spin_unlock_irq(&umidi->disc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001410 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001411 if (ep->out)
1412 tasklet_kill(&ep->out->tasklet);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001413 if (ep->out) {
1414 for (j = 0; j < OUTPUT_URBS; ++j)
1415 usb_kill_urb(ep->out->urbs[j].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (umidi->usb_protocol_ops->finish_out_endpoint)
1417 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
Takashi Iwai29aac002010-04-10 21:27:23 +02001418 ep->out->active_urbs = 0;
1419 if (ep->out->drain_urbs) {
1420 ep->out->drain_urbs = 0;
1421 wake_up(&ep->out->drain_wait);
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001424 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001425 for (j = 0; j < INPUT_URBS; ++j)
1426 usb_kill_urb(ep->in->urbs[j]);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001427 /* free endpoints here; later call can result in Oops */
Takashi Iwai29aac002010-04-10 21:27:23 +02001428 if (ep->out)
1429 snd_usbmidi_out_endpoint_clear(ep->out);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001430 if (ep->in) {
1431 snd_usbmidi_in_endpoint_delete(ep->in);
1432 ep->in = NULL;
1433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001435 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Takashi Iwai86e07d32005-11-17 15:08:02 +01001438static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001440 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 snd_usbmidi_free(umidi);
1442}
1443
Takashi Iwai86e07d32005-11-17 15:08:02 +01001444static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Daniel Mack21af7d82010-06-16 17:57:29 +02001445 int stream, int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct list_head* list;
1448
1449 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001450 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (substream->number == number)
1452 return substream;
1453 }
1454 return NULL;
1455}
1456
1457/*
1458 * This list specifies names for ports that do not fit into the standard
1459 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1460 * such as internal control or synthesizer ports.
1461 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001462static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001463 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001464 short int port;
1465 short int voices;
1466 const char *name;
1467 unsigned int seq_flags;
1468} snd_usbmidi_port_info[] = {
1469#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1470 { .id = USB_ID(vendor, product), \
1471 .port = num, .voices = voices_, \
1472 .name = name_, .seq_flags = flags }
1473#define EXTERNAL_PORT(vendor, product, num, name) \
1474 PORT_INFO(vendor, product, num, name, 0, \
1475 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1476 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1477 SNDRV_SEQ_PORT_TYPE_PORT)
1478#define CONTROL_PORT(vendor, product, num, name) \
1479 PORT_INFO(vendor, product, num, name, 0, \
1480 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1481 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1482#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1483 PORT_INFO(vendor, product, num, name, voices, \
1484 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1485 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1486 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1487 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1488 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1489 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1490 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1491#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1492 PORT_INFO(vendor, product, num, name, voices, \
1493 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1494 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1495 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1496 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1497 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1498 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1499 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1500 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001502 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001504 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1505 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1506 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1507 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1508 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1509 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001511 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1512 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001514 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1515 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1516 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001518 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1519 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1520 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001522 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1523 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1524 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001526 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001528 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1529 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1530 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1531 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001533 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001535 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1536 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1537 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001539 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1540 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1541 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1542 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001544 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1545 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001547 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1548 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1549 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001551 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1552 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1553 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001555 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1556 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001558 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1559 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001561 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1562 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1563 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001565 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1566 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1567 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001568 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001569 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001571 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1572 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001574 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1575 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001577 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1578 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1579 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Krzysztof Foltman4434ade2010-05-20 20:31:10 +01001580 /* Akai MPD16 */
1581 CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1582 PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1583 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1584 SNDRV_SEQ_PORT_TYPE_HARDWARE),
Sebastien Alaiwand39e82d2010-02-16 08:55:08 +01001585 /* Access Music Virus TI */
1586 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1587 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1588 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1589 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1590 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591};
1592
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001593static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1594{
1595 int i;
1596
1597 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001598 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001599 snd_usbmidi_port_info[i].port == number)
1600 return &snd_usbmidi_port_info[i];
1601 }
1602 return NULL;
1603}
1604
1605static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1606 struct snd_seq_port_info *seq_port_info)
1607{
1608 struct snd_usb_midi *umidi = rmidi->private_data;
1609 struct port_info *port_info;
1610
1611 /* TODO: read port flags from descriptors */
1612 port_info = find_port_info(umidi, number);
1613 if (port_info) {
1614 seq_port_info->type = port_info->seq_flags;
1615 seq_port_info->midi_voices = port_info->voices;
1616 }
1617}
1618
Takashi Iwai86e07d32005-11-17 15:08:02 +01001619static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001621 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001623 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 const char *name_format;
1625
Takashi Iwai86e07d32005-11-17 15:08:02 +01001626 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (!substream) {
1628 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1629 return;
1630 }
1631
1632 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001633 port_info = find_port_info(umidi, number);
1634 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 snprintf(substream->name, sizeof(substream->name),
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001636 name_format, umidi->card->shortname, number + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 *rsubstream = substream;
1639}
1640
1641/*
1642 * Creates the endpoints and their ports.
1643 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001644static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1645 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 int i, j, err;
1648 int out_ports = 0, in_ports = 0;
1649
1650 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1651 if (endpoints[i].out_cables) {
1652 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1653 &umidi->endpoints[i]);
1654 if (err < 0)
1655 return err;
1656 }
1657 if (endpoints[i].in_cables) {
1658 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1659 &umidi->endpoints[i]);
1660 if (err < 0)
1661 return err;
1662 }
1663
1664 for (j = 0; j < 0x10; ++j) {
1665 if (endpoints[i].out_cables & (1 << j)) {
1666 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1667 &umidi->endpoints[i].out->ports[j].substream);
1668 ++out_ports;
1669 }
1670 if (endpoints[i].in_cables & (1 << j)) {
1671 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1672 &umidi->endpoints[i].in->ports[j].substream);
1673 ++in_ports;
1674 }
1675 }
1676 }
1677 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1678 out_ports, in_ports);
1679 return 0;
1680}
1681
1682/*
1683 * Returns MIDIStreaming device capabilities.
1684 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001685static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1686 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct usb_interface* intf;
1689 struct usb_host_interface *hostif;
1690 struct usb_interface_descriptor* intfd;
1691 struct usb_ms_header_descriptor* ms_header;
1692 struct usb_host_endpoint *hostep;
1693 struct usb_endpoint_descriptor* ep;
1694 struct usb_ms_endpoint_descriptor* ms_ep;
1695 int i, epidx;
1696
1697 intf = umidi->iface;
1698 if (!intf)
1699 return -ENXIO;
1700 hostif = &intf->altsetting[0];
1701 intfd = get_iface_desc(hostif);
1702 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1703 if (hostif->extralen >= 7 &&
1704 ms_header->bLength >= 7 &&
1705 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
Daniel Mackde48c7b2010-02-22 23:49:13 +01001706 ms_header->bDescriptorSubtype == UAC_HEADER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1708 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1709 else
1710 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1711
1712 epidx = 0;
1713 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1714 hostep = &hostif->endpoint[i];
1715 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001716 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 continue;
1718 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1719 if (hostep->extralen < 4 ||
1720 ms_ep->bLength < 4 ||
1721 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
Daniel Mackde48c7b2010-02-22 23:49:13 +01001722 ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001724 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (endpoints[epidx].out_ep) {
1726 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1727 snd_printk(KERN_WARNING "too many endpoints\n");
1728 break;
1729 }
1730 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001731 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1732 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001734 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001735 /*
1736 * Low speed bulk transfers don't exist, so
1737 * force interrupt transfers for devices like
1738 * ESI MIDI Mate that try to use them anyway.
1739 */
1740 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1742 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1743 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1744 } else {
1745 if (endpoints[epidx].in_ep) {
1746 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1747 snd_printk(KERN_WARNING "too many endpoints\n");
1748 break;
1749 }
1750 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001751 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1752 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001754 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001755 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1757 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1758 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1759 }
1760 }
1761 return 0;
1762}
1763
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001764static int roland_load_info(struct snd_kcontrol *kcontrol,
1765 struct snd_ctl_elem_info *info)
1766{
1767 static const char *const names[] = { "High Load", "Light Load" };
1768
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01001769 return snd_ctl_enum_info(info, 1, 2, names);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001770}
1771
1772static int roland_load_get(struct snd_kcontrol *kcontrol,
1773 struct snd_ctl_elem_value *value)
1774{
1775 value->value.enumerated.item[0] = kcontrol->private_value;
1776 return 0;
1777}
1778
1779static int roland_load_put(struct snd_kcontrol *kcontrol,
1780 struct snd_ctl_elem_value *value)
1781{
1782 struct snd_usb_midi* umidi = kcontrol->private_data;
1783 int changed;
1784
1785 if (value->value.enumerated.item[0] > 1)
1786 return -EINVAL;
1787 mutex_lock(&umidi->mutex);
1788 changed = value->value.enumerated.item[0] != kcontrol->private_value;
1789 if (changed)
1790 kcontrol->private_value = value->value.enumerated.item[0];
1791 mutex_unlock(&umidi->mutex);
1792 return changed;
1793}
1794
1795static struct snd_kcontrol_new roland_load_ctl = {
1796 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1797 .name = "MIDI Input Mode",
1798 .info = roland_load_info,
1799 .get = roland_load_get,
1800 .put = roland_load_put,
1801 .private_value = 1,
1802};
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804/*
1805 * On Roland devices, use the second alternate setting to be able to use
1806 * the interrupt input endpoint.
1807 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001808static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 struct usb_interface* intf;
1811 struct usb_host_interface *hostif;
1812 struct usb_interface_descriptor* intfd;
1813
1814 intf = umidi->iface;
1815 if (!intf || intf->num_altsetting != 2)
1816 return;
1817
1818 hostif = &intf->altsetting[1];
1819 intfd = get_iface_desc(hostif);
1820 if (intfd->bNumEndpoints != 2 ||
1821 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1822 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1823 return;
1824
1825 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1826 intfd->bAlternateSetting);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001827 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 intfd->bAlternateSetting);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001829
1830 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001831 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001832 umidi->roland_load_ctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
1835/*
1836 * Try to find any usable endpoints in the interface.
1837 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001838static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1839 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 int max_endpoints)
1841{
1842 struct usb_interface* intf;
1843 struct usb_host_interface *hostif;
1844 struct usb_interface_descriptor* intfd;
1845 struct usb_endpoint_descriptor* epd;
1846 int i, out_eps = 0, in_eps = 0;
1847
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001848 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 snd_usbmidi_switch_roland_altsetting(umidi);
1850
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001851 if (endpoint[0].out_ep || endpoint[0].in_ep)
Daniel Mack21af7d82010-06-16 17:57:29 +02001852 return 0;
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 intf = umidi->iface;
1855 if (!intf || intf->num_altsetting < 1)
1856 return -ENOENT;
1857 hostif = intf->cur_altsetting;
1858 intfd = get_iface_desc(hostif);
1859
1860 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1861 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001862 if (!usb_endpoint_xfer_bulk(epd) &&
1863 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 continue;
1865 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001866 usb_endpoint_dir_out(epd)) {
1867 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1868 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 endpoint[out_eps].out_interval = epd->bInterval;
1870 ++out_eps;
1871 }
1872 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001873 usb_endpoint_dir_in(epd)) {
1874 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1875 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 endpoint[in_eps].in_interval = epd->bInterval;
1877 ++in_eps;
1878 }
1879 }
1880 return (out_eps || in_eps) ? 0 : -ENOENT;
1881}
1882
1883/*
1884 * Detects the endpoints for one-port-per-endpoint protocols.
1885 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001886static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1887 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 int err, i;
Daniel Mack21af7d82010-06-16 17:57:29 +02001890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1892 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1893 if (endpoints[i].out_ep)
1894 endpoints[i].out_cables = 0x0001;
1895 if (endpoints[i].in_ep)
1896 endpoints[i].in_cables = 0x0001;
1897 }
1898 return err;
1899}
1900
1901/*
1902 * Detects the endpoints and ports of Yamaha devices.
1903 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001904static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1905 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 struct usb_interface* intf;
1908 struct usb_host_interface *hostif;
1909 struct usb_interface_descriptor* intfd;
1910 uint8_t* cs_desc;
1911
1912 intf = umidi->iface;
1913 if (!intf)
1914 return -ENOENT;
1915 hostif = intf->altsetting;
1916 intfd = get_iface_desc(hostif);
1917 if (intfd->bNumEndpoints < 1)
1918 return -ENOENT;
1919
1920 /*
1921 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1922 * necessarily with any useful contents. So simply count 'em.
1923 */
1924 for (cs_desc = hostif->extra;
1925 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1926 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001927 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01001928 if (cs_desc[2] == UAC_MIDI_IN_JACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
Daniel Mackde48c7b2010-02-22 23:49:13 +01001930 else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1932 }
1933 }
1934 if (!endpoint->in_cables && !endpoint->out_cables)
1935 return -ENOENT;
1936
1937 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1938}
1939
1940/*
1941 * Creates the endpoints and their ports for Midiman devices.
1942 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001943static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1944 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001946 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 struct usb_interface* intf;
1948 struct usb_host_interface *hostif;
1949 struct usb_interface_descriptor* intfd;
1950 struct usb_endpoint_descriptor* epd;
1951 int cable, err;
1952
1953 intf = umidi->iface;
1954 if (!intf)
1955 return -ENOENT;
1956 hostif = intf->altsetting;
1957 intfd = get_iface_desc(hostif);
1958 /*
1959 * The various MidiSport devices have more or less random endpoint
1960 * numbers, so we have to identify the endpoints by their index in
1961 * the descriptor array, like the driver for that other OS does.
1962 *
1963 * There is one interrupt input endpoint for all input ports, one
1964 * bulk output endpoint for even-numbered ports, and one for odd-
1965 * numbered ports. Both bulk output endpoints have corresponding
1966 * input bulk endpoints (at indices 1 and 3) which aren't used.
1967 */
1968 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1969 snd_printdd(KERN_ERR "not enough endpoints\n");
1970 return -ENOENT;
1971 }
1972
1973 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001974 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1976 return -ENXIO;
1977 }
1978 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001979 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1981 return -ENXIO;
1982 }
1983 if (endpoint->out_cables > 0x0001) {
1984 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001985 if (!usb_endpoint_dir_out(epd) ||
1986 !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1988 return -ENXIO;
1989 }
1990 }
1991
1992 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01001993 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 ep_info.out_cables = endpoint->out_cables & 0x5555;
1995 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1996 if (err < 0)
1997 return err;
1998
1999 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2000 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
2001 ep_info.in_cables = endpoint->in_cables;
2002 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
2003 if (err < 0)
2004 return err;
2005
2006 if (endpoint->out_cables > 0x0001) {
2007 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2008 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
2009 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
2010 if (err < 0)
2011 return err;
2012 }
2013
2014 for (cable = 0; cable < 0x10; ++cable) {
2015 if (endpoint->out_cables & (1 << cable))
2016 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
2017 &umidi->endpoints[cable & 1].out->ports[cable].substream);
2018 if (endpoint->in_cables & (1 << cable))
2019 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
2020 &umidi->endpoints[0].in->ports[cable].substream);
2021 }
2022 return 0;
2023}
2024
Clemens Ladischa7b928a2006-05-02 16:22:12 +02002025static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
2026 .get_port_info = snd_usbmidi_get_port_info,
2027};
2028
Takashi Iwai86e07d32005-11-17 15:08:02 +01002029static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 int out_ports, int in_ports)
2031{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002032 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int err;
2034
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002035 err = snd_rawmidi_new(umidi->card, "USB MIDI",
2036 umidi->next_midi_device++,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 out_ports, in_ports, &rmidi);
2038 if (err < 0)
2039 return err;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002040 strcpy(rmidi->name, umidi->card->shortname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2042 SNDRV_RAWMIDI_INFO_INPUT |
2043 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02002044 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 rmidi->private_data = umidi;
2046 rmidi->private_free = snd_usbmidi_rawmidi_free;
2047 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
2048 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
2049
2050 umidi->rmidi = rmidi;
2051 return 0;
2052}
2053
2054/*
2055 * Temporarily stop input.
2056 */
2057void snd_usbmidi_input_stop(struct list_head* p)
2058{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002059 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02002060 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Takashi Iwai86e07d32005-11-17 15:08:02 +01002062 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01002064 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02002066 for (j = 0; j < INPUT_URBS; ++j)
2067 usb_kill_urb(ep->in->urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069}
2070
Takashi Iwai86e07d32005-11-17 15:08:02 +01002071static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02002073 unsigned int i;
2074
2075 if (!ep)
2076 return;
2077 for (i = 0; i < INPUT_URBS; ++i) {
2078 struct urb* urb = ep->urbs[i];
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002079 urb->dev = ep->umidi->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
2081 }
2082}
2083
2084/*
2085 * Resume input after a call to snd_usbmidi_input_stop().
2086 */
2087void snd_usbmidi_input_start(struct list_head* p)
2088{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002089 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 int i;
2091
Takashi Iwai86e07d32005-11-17 15:08:02 +01002092 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2094 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2095}
2096
2097/*
2098 * Creates and registers everything needed for a MIDI streaming interface.
2099 */
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002100int snd_usbmidi_create(struct snd_card *card,
2101 struct usb_interface* iface,
2102 struct list_head *midi_list,
2103 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002105 struct snd_usb_midi* umidi;
2106 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 int out_ports, in_ports;
2108 int i, err;
2109
Takashi Iwai561b2202005-09-09 14:22:34 +02002110 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (!umidi)
2112 return -ENOMEM;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002113 umidi->dev = interface_to_usbdev(iface);
2114 umidi->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 umidi->iface = iface;
2116 umidi->quirk = quirk;
2117 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02002118 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01002119 spin_lock_init(&umidi->disc_lock);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02002120 mutex_init(&umidi->mutex);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002121 umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2122 le16_to_cpu(umidi->dev->descriptor.idProduct));
Clemens Ladischc8846972005-08-02 15:26:52 +02002123 umidi->error_timer.function = snd_usbmidi_error_timer;
2124 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 /* detect the endpoint(s) to use */
2127 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002128 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2129 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002131 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
Clemens Ladischd05cc10432007-05-07 09:28:53 +02002132 umidi->usb_protocol_ops =
2133 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002134 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02002135 case QUIRK_MIDI_US122L:
2136 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2137 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02002138 case QUIRK_MIDI_FIXED_ENDPOINT:
2139 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002140 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002141 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2142 break;
2143 case QUIRK_MIDI_YAMAHA:
2144 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2145 break;
2146 case QUIRK_MIDI_MIDIMAN:
2147 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2148 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002149 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002150 err = 0;
2151 break;
2152 case QUIRK_MIDI_NOVATION:
2153 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2154 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2155 break;
Clemens Ladischc7f57212010-10-22 18:20:48 +02002156 case QUIRK_MIDI_RAW_BYTES:
Clemens Ladischd1bda042005-09-14 08:36:03 +02002157 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02002158 /*
2159 * Interface 1 contains isochronous endpoints, but with the same
2160 * numbers as in interface 0. Since it is interface 1 that the
2161 * USB core has most recently seen, these descriptors are now
2162 * associated with the endpoint numbers. This will foul up our
2163 * attempts to submit bulk/interrupt URBs to the endpoints in
2164 * interface 0, so we have to make sure that the USB core looks
2165 * again at interface 0 by calling usb_set_interface() on it.
2166 */
Clemens Ladischc7f57212010-10-22 18:20:48 +02002167 if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2168 usb_set_interface(umidi->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002169 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2170 break;
2171 case QUIRK_MIDI_EMAGIC:
2172 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2173 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002174 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002175 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2176 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01002177 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02002178 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002179 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2180 break;
Krzysztof Foltman4434ade2010-05-20 20:31:10 +01002181 case QUIRK_MIDI_AKAI:
2182 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2183 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2184 /* endpoint 1 is input-only */
2185 endpoints[1].out_cables = 0;
2186 break;
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +02002187 case QUIRK_MIDI_FTDI:
2188 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2189
2190 /* set baud rate to 31250 (48 MHz / 16 / 96) */
2191 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2192 3, 0x40, 0x60, 0, NULL, 0, 1000);
2193 if (err < 0)
2194 break;
2195
2196 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2197 break;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002198 default:
2199 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2200 err = -ENXIO;
2201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203 if (err < 0) {
2204 kfree(umidi);
2205 return err;
2206 }
2207
2208 /* create rawmidi device */
2209 out_ports = 0;
2210 in_ports = 0;
2211 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Akinobu Mitafbc54392009-11-20 14:56:52 +09002212 out_ports += hweight16(endpoints[i].out_cables);
2213 in_ports += hweight16(endpoints[i].in_cables);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2216 if (err < 0) {
2217 kfree(umidi);
2218 return err;
2219 }
2220
2221 /* create endpoint/port structures */
2222 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2223 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2224 else
2225 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2226 if (err < 0) {
2227 snd_usbmidi_free(umidi);
2228 return err;
2229 }
2230
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002231 list_add_tail(&umidi->list, midi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2234 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2235 return 0;
2236}
2237
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002238EXPORT_SYMBOL(snd_usbmidi_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239EXPORT_SYMBOL(snd_usbmidi_input_stop);
2240EXPORT_SYMBOL(snd_usbmidi_input_start);
2241EXPORT_SYMBOL(snd_usbmidi_disconnect);