blob: 32d70cc046e789d32d122051b5364564376f100e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
Clemens Ladischd05cc10432007-05-07 09:28:53 +02004 * Copyright (c) 2002-2007 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>
48#include <sound/core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <sound/rawmidi.h>
Clemens Ladischa7b928a2006-05-02 16:22:12 +020050#include <sound/asequencer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "usbaudio.h"
52
53
54/*
55 * define this to log all USB packets
56 */
57/* #define DUMP_PACKETS */
58
Clemens Ladischc8846972005-08-02 15:26:52 +020059/*
60 * how long to wait after some USB errors, so that khubd can disconnect() us
61 * without too many spurious errors
62 */
63#define ERROR_DELAY_JIFFIES (HZ / 10)
64
Clemens Ladisched4affa2009-07-13 13:43:59 +020065#define OUTPUT_URBS 7
Clemens Ladisch4773d1f2009-07-13 13:40:36 +020066#define INPUT_URBS 7
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
70MODULE_DESCRIPTION("USB Audio/MIDI helper module");
71MODULE_LICENSE("Dual BSD/GPL");
72
73
74struct usb_ms_header_descriptor {
75 __u8 bLength;
76 __u8 bDescriptorType;
77 __u8 bDescriptorSubtype;
78 __u8 bcdMSC[2];
79 __le16 wTotalLength;
80} __attribute__ ((packed));
81
82struct usb_ms_endpoint_descriptor {
83 __u8 bLength;
84 __u8 bDescriptorType;
85 __u8 bDescriptorSubtype;
86 __u8 bNumEmbMIDIJack;
87 __u8 baAssocJackID[0];
88} __attribute__ ((packed));
89
Takashi Iwai86e07d32005-11-17 15:08:02 +010090struct snd_usb_midi_in_endpoint;
91struct snd_usb_midi_out_endpoint;
92struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +010095 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
Clemens Ladisched4affa2009-07-13 13:43:59 +020096 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Takashi Iwai86e07d32005-11-17 15:08:02 +010098 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
99 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102struct snd_usb_midi {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100103 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100105 const struct snd_usb_audio_quirk *quirk;
106 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct usb_protocol_ops* usb_protocol_ops;
108 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200109 struct timer_list error_timer;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100110 spinlock_t disc_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100113 struct snd_usb_midi_out_endpoint *out;
114 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 } endpoints[MIDI_MAX_ENDPOINTS];
116 unsigned long input_triggered;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100117 unsigned char disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100121 struct snd_usb_midi* umidi;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200122 struct out_urb_context {
123 struct urb *urb;
124 struct snd_usb_midi_out_endpoint *ep;
125 } urbs[OUTPUT_URBS];
126 unsigned int active_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int max_transfer; /* size of urb buffer */
128 struct tasklet_struct tasklet;
129
130 spinlock_t buffer_lock;
131
132 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100133 struct snd_usb_midi_out_endpoint* ep;
134 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int active;
136 uint8_t cable; /* cable number << 4 */
137 uint8_t state;
138#define STATE_UNKNOWN 0
139#define STATE_1PARAM 1
140#define STATE_2PARAM_1 2
141#define STATE_2PARAM_2 3
142#define STATE_SYSEX_0 4
143#define STATE_SYSEX_1 5
144#define STATE_SYSEX_2 6
145 uint8_t data[2];
146 } ports[0x10];
147 int current_port;
148};
149
150struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100151 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200152 struct urb* urbs[INPUT_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100154 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200155 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200157 u8 seen_f5;
158 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int current_port;
160};
161
Takashi Iwai86e07d32005-11-17 15:08:02 +0100162static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164static const uint8_t snd_usbmidi_cin_length[] = {
165 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
166};
167
168/*
169 * Submits the URB, with error handling.
170 */
Al Viro55016f12005-10-21 03:21:58 -0400171static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int err = usb_submit_urb(urb, flags);
174 if (err < 0 && err != -ENODEV)
175 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
176 return err;
177}
178
179/*
180 * Error handling for URB completion functions.
181 */
182static int snd_usbmidi_urb_error(int status)
183{
Clemens Ladischc8846972005-08-02 15:26:52 +0200184 switch (status) {
185 /* manually unlinked, or device gone */
186 case -ENOENT:
187 case -ECONNRESET:
188 case -ESHUTDOWN:
189 case -ENODEV:
190 return -ENODEV;
191 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700192 case -EPROTO:
193 case -ETIME:
194 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200195 return -EIO;
196 default:
197 snd_printk(KERN_ERR "urb status %d\n", status);
198 return 0; /* continue */
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/*
203 * Receives a chunk of MIDI data.
204 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100205static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 uint8_t* data, int length)
207{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100208 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if (!port->substream) {
211 snd_printd("unexpected port %d!\n", portidx);
212 return;
213 }
214 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
215 return;
216 snd_rawmidi_receive(port->substream, data, length);
217}
218
219#ifdef DUMP_PACKETS
220static void dump_urb(const char *type, const u8 *data, int length)
221{
222 snd_printk(KERN_DEBUG "%s packet: [", type);
223 for (; length > 0; ++data, --length)
224 printk(" %02x", *data);
225 printk(" ]\n");
226}
227#else
228#define dump_urb(type, data, length) /* nothing */
229#endif
230
231/*
232 * Processes the data read from the device.
233 */
David Howells7d12e782006-10-05 14:55:46 +0100234static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100236 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (urb->status == 0) {
239 dump_urb("received", urb->transfer_buffer, urb->actual_length);
240 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
241 urb->actual_length);
242 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200243 int err = snd_usbmidi_urb_error(urb->status);
244 if (err < 0) {
245 if (err != -ENODEV) {
246 ep->error_resubmit = 1;
247 mod_timer(&ep->umidi->error_timer,
248 jiffies + ERROR_DELAY_JIFFIES);
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200254 urb->dev = ep->umidi->chip->dev;
255 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
David Howells7d12e782006-10-05 14:55:46 +0100258static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200260 struct out_urb_context *context = urb->context;
261 struct snd_usb_midi_out_endpoint* ep = context->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 spin_lock(&ep->buffer_lock);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200264 ep->active_urbs &= ~(1 << (context - ep->urbs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 spin_unlock(&ep->buffer_lock);
266 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200267 int err = snd_usbmidi_urb_error(urb->status);
268 if (err < 0) {
269 if (err != -ENODEV)
270 mod_timer(&ep->umidi->error_timer,
271 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 snd_usbmidi_do_output(ep);
276}
277
278/*
279 * This is called when some data should be transferred to the device
280 * (from one or more substreams).
281 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100282static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200284 unsigned int urb_index;
285 struct urb* urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unsigned long flags;
287
288 spin_lock_irqsave(&ep->buffer_lock, flags);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200289 if (ep->umidi->chip->shutdown) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 spin_unlock_irqrestore(&ep->buffer_lock, flags);
291 return;
292 }
293
Clemens Ladisched4affa2009-07-13 13:43:59 +0200294 for (;;) {
295 urb = NULL;
296 for (urb_index = 0; urb_index < OUTPUT_URBS; ++urb_index)
297 if (!(ep->active_urbs & (1 << urb_index))) {
298 urb = ep->urbs[urb_index].urb;
299 break;
300 }
301 if (!urb)
302 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Clemens Ladisched4affa2009-07-13 13:43:59 +0200304 urb->transfer_buffer_length = 0;
305 ep->umidi->usb_protocol_ops->output(ep, urb);
306 if (urb->transfer_buffer_length == 0)
307 break;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 dump_urb("sending", urb->transfer_buffer,
310 urb->transfer_buffer_length);
311 urb->dev = ep->umidi->chip->dev;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200312 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
313 break;
314 ep->active_urbs |= 1 << urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 spin_unlock_irqrestore(&ep->buffer_lock, flags);
317}
318
319static void snd_usbmidi_out_tasklet(unsigned long data)
320{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100321 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 snd_usbmidi_do_output(ep);
324}
325
Clemens Ladischc8846972005-08-02 15:26:52 +0200326/* called after transfers had been interrupted due to some USB error */
327static void snd_usbmidi_error_timer(unsigned long data)
328{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100329 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200330 unsigned int i, j;
Clemens Ladischc8846972005-08-02 15:26:52 +0200331
Takashi Iwaic0792e02008-02-22 18:34:44 +0100332 spin_lock(&umidi->disc_lock);
333 if (umidi->disconnected) {
334 spin_unlock(&umidi->disc_lock);
335 return;
336 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200337 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100338 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200339 if (in && in->error_resubmit) {
340 in->error_resubmit = 0;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200341 for (j = 0; j < INPUT_URBS; ++j) {
342 in->urbs[j]->dev = umidi->chip->dev;
343 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
344 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200345 }
346 if (umidi->endpoints[i].out)
347 snd_usbmidi_do_output(umidi->endpoints[i].out);
348 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100349 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100353static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 const void *data, int len)
355{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200356 int err = 0;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700357 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!buf)
359 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 dump_urb("sending", buf, len);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200361 if (ep->urbs[0].urb)
362 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urbs[0].urb->pipe,
363 buf, len, NULL, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 kfree(buf);
365 return err;
366}
367
368/*
369 * Standard USB MIDI protocol: see the spec.
370 * Midiman protocol: like the standard protocol, but the control byte is the
371 * fourth byte in each packet, and uses length instead of CIN.
372 */
373
Takashi Iwai86e07d32005-11-17 15:08:02 +0100374static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 uint8_t* buffer, int buffer_length)
376{
377 int i;
378
379 for (i = 0; i + 3 < buffer_length; i += 4)
380 if (buffer[i] != 0) {
381 int cable = buffer[i] >> 4;
382 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
383 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
384 }
385}
386
Takashi Iwai86e07d32005-11-17 15:08:02 +0100387static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 uint8_t* buffer, int buffer_length)
389{
390 int i;
391
392 for (i = 0; i + 3 < buffer_length; i += 4)
393 if (buffer[i + 3] != 0) {
394 int port = buffer[i + 3] >> 4;
395 int length = buffer[i + 3] & 3;
396 snd_usbmidi_input_data(ep, port, &buffer[i], length);
397 }
398}
399
400/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200401 * Buggy M-Audio device: running status on input results in a packet that has
402 * the data bytes but not the status byte and that is marked with CIN 4.
403 */
404static void snd_usbmidi_maudio_broken_running_status_input(
405 struct snd_usb_midi_in_endpoint* ep,
406 uint8_t* buffer, int buffer_length)
407{
408 int i;
409
410 for (i = 0; i + 3 < buffer_length; i += 4)
411 if (buffer[i] != 0) {
412 int cable = buffer[i] >> 4;
413 u8 cin = buffer[i] & 0x0f;
414 struct usbmidi_in_port *port = &ep->ports[cable];
415 int length;
416
417 length = snd_usbmidi_cin_length[cin];
418 if (cin == 0xf && buffer[i + 1] >= 0xf8)
419 ; /* realtime msg: no running status change */
420 else if (cin >= 0x8 && cin <= 0xe)
421 /* channel msg */
422 port->running_status_length = length - 1;
423 else if (cin == 0x4 &&
424 port->running_status_length != 0 &&
425 buffer[i + 1] < 0x80)
426 /* CIN 4 that is not a SysEx */
427 length = port->running_status_length;
428 else
429 /*
430 * All other msgs cannot begin running status.
431 * (A channel msg sent as two or three CIN 0xF
432 * packets could in theory, but this device
433 * doesn't use this format.)
434 */
435 port->running_status_length = 0;
436 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
437 }
438}
439
440/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200441 * CME protocol: like the standard protocol, but SysEx commands are sent as a
442 * single USB packet preceded by a 0x0F byte.
443 */
444static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
445 uint8_t *buffer, int buffer_length)
446{
447 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
448 snd_usbmidi_standard_input(ep, buffer, buffer_length);
449 else
450 snd_usbmidi_input_data(ep, buffer[0] >> 4,
451 &buffer[1], buffer_length - 1);
452}
453
454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * Adds one USB MIDI packet to the output buffer.
456 */
457static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
458 uint8_t p1, uint8_t p2, uint8_t p3)
459{
460
461 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
462 buf[0] = p0;
463 buf[1] = p1;
464 buf[2] = p2;
465 buf[3] = p3;
466 urb->transfer_buffer_length += 4;
467}
468
469/*
470 * Adds one Midiman packet to the output buffer.
471 */
472static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
473 uint8_t p1, uint8_t p2, uint8_t p3)
474{
475
476 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
477 buf[0] = p1;
478 buf[1] = p2;
479 buf[2] = p3;
480 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
481 urb->transfer_buffer_length += 4;
482}
483
484/*
485 * Converts MIDI commands to USB MIDI packets.
486 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100487static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 uint8_t b, struct urb* urb)
489{
490 uint8_t p0 = port->cable;
491 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
492 port->ep->umidi->usb_protocol_ops->output_packet;
493
494 if (b >= 0xf8) {
495 output_packet(urb, p0 | 0x0f, b, 0, 0);
496 } else if (b >= 0xf0) {
497 switch (b) {
498 case 0xf0:
499 port->data[0] = b;
500 port->state = STATE_SYSEX_1;
501 break;
502 case 0xf1:
503 case 0xf3:
504 port->data[0] = b;
505 port->state = STATE_1PARAM;
506 break;
507 case 0xf2:
508 port->data[0] = b;
509 port->state = STATE_2PARAM_1;
510 break;
511 case 0xf4:
512 case 0xf5:
513 port->state = STATE_UNKNOWN;
514 break;
515 case 0xf6:
516 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
517 port->state = STATE_UNKNOWN;
518 break;
519 case 0xf7:
520 switch (port->state) {
521 case STATE_SYSEX_0:
522 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
523 break;
524 case STATE_SYSEX_1:
525 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
526 break;
527 case STATE_SYSEX_2:
528 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
529 break;
530 }
531 port->state = STATE_UNKNOWN;
532 break;
533 }
534 } else if (b >= 0x80) {
535 port->data[0] = b;
536 if (b >= 0xc0 && b <= 0xdf)
537 port->state = STATE_1PARAM;
538 else
539 port->state = STATE_2PARAM_1;
540 } else { /* b < 0x80 */
541 switch (port->state) {
542 case STATE_1PARAM:
543 if (port->data[0] < 0xf0) {
544 p0 |= port->data[0] >> 4;
545 } else {
546 p0 |= 0x02;
547 port->state = STATE_UNKNOWN;
548 }
549 output_packet(urb, p0, port->data[0], b, 0);
550 break;
551 case STATE_2PARAM_1:
552 port->data[1] = b;
553 port->state = STATE_2PARAM_2;
554 break;
555 case STATE_2PARAM_2:
556 if (port->data[0] < 0xf0) {
557 p0 |= port->data[0] >> 4;
558 port->state = STATE_2PARAM_1;
559 } else {
560 p0 |= 0x03;
561 port->state = STATE_UNKNOWN;
562 }
563 output_packet(urb, p0, port->data[0], port->data[1], b);
564 break;
565 case STATE_SYSEX_0:
566 port->data[0] = b;
567 port->state = STATE_SYSEX_1;
568 break;
569 case STATE_SYSEX_1:
570 port->data[1] = b;
571 port->state = STATE_SYSEX_2;
572 break;
573 case STATE_SYSEX_2:
574 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
575 port->state = STATE_SYSEX_0;
576 break;
577 }
578 }
579}
580
Clemens Ladisched4affa2009-07-13 13:43:59 +0200581static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
582 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int p;
585
586 /* FIXME: lower-numbered ports can starve higher-numbered ports */
587 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100588 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!port->active)
590 continue;
591 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
592 uint8_t b;
593 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
594 port->active = 0;
595 break;
596 }
597 snd_usbmidi_transmit_byte(port, b, urb);
598 }
599 }
600}
601
602static struct usb_protocol_ops snd_usbmidi_standard_ops = {
603 .input = snd_usbmidi_standard_input,
604 .output = snd_usbmidi_standard_output,
605 .output_packet = snd_usbmidi_output_standard_packet,
606};
607
608static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
609 .input = snd_usbmidi_midiman_input,
610 .output = snd_usbmidi_standard_output,
611 .output_packet = snd_usbmidi_output_midiman_packet,
612};
613
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200614static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
615 .input = snd_usbmidi_maudio_broken_running_status_input,
616 .output = snd_usbmidi_standard_output,
617 .output_packet = snd_usbmidi_output_standard_packet,
618};
619
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200620static struct usb_protocol_ops snd_usbmidi_cme_ops = {
621 .input = snd_usbmidi_cme_input,
622 .output = snd_usbmidi_standard_output,
623 .output_packet = snd_usbmidi_output_standard_packet,
624};
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*
627 * Novation USB MIDI protocol: number of data bytes is in the first byte
628 * (when receiving) (+1!) or in the second byte (when sending); data begins
629 * at the third byte.
630 */
631
Takashi Iwai86e07d32005-11-17 15:08:02 +0100632static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 uint8_t* buffer, int buffer_length)
634{
635 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
636 return;
637 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
638}
639
Clemens Ladisched4affa2009-07-13 13:43:59 +0200640static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
641 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 uint8_t* transfer_buffer;
644 int count;
645
646 if (!ep->ports[0].active)
647 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200648 transfer_buffer = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 count = snd_rawmidi_transmit(ep->ports[0].substream,
650 &transfer_buffer[2],
651 ep->max_transfer - 2);
652 if (count < 1) {
653 ep->ports[0].active = 0;
654 return;
655 }
656 transfer_buffer[0] = 0;
657 transfer_buffer[1] = count;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200658 urb->transfer_buffer_length = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661static struct usb_protocol_ops snd_usbmidi_novation_ops = {
662 .input = snd_usbmidi_novation_input,
663 .output = snd_usbmidi_novation_output,
664};
665
666/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200667 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
669
Takashi Iwai86e07d32005-11-17 15:08:02 +0100670static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200671 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
674}
675
Clemens Ladisched4affa2009-07-13 13:43:59 +0200676static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
677 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 int count;
680
681 if (!ep->ports[0].active)
682 return;
683 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200684 urb->transfer_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ep->max_transfer);
686 if (count < 1) {
687 ep->ports[0].active = 0;
688 return;
689 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200690 urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200693static struct usb_protocol_ops snd_usbmidi_raw_ops = {
694 .input = snd_usbmidi_raw_input,
695 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696};
697
Karsten Wiese030a07e2008-07-30 15:13:29 +0200698static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
699 uint8_t *buffer, int buffer_length)
700{
701 if (buffer_length != 9)
702 return;
703 buffer_length = 8;
704 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
705 buffer_length--;
706 if (buffer_length)
707 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
708}
709
Clemens Ladisched4affa2009-07-13 13:43:59 +0200710static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
711 struct urb *urb)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200712{
713 int count;
714
715 if (!ep->ports[0].active)
716 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200717 count = snd_usb_get_speed(ep->umidi->chip->dev) == USB_SPEED_HIGH
718 ? 1 : 2;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200719 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200720 urb->transfer_buffer,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200721 count);
722 if (count < 1) {
723 ep->ports[0].active = 0;
724 return;
725 }
726
Clemens Ladisched4affa2009-07-13 13:43:59 +0200727 memset(urb->transfer_buffer + count, 0xFD, 9 - count);
728 urb->transfer_buffer_length = count;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200729}
730
731static struct usb_protocol_ops snd_usbmidi_122l_ops = {
732 .input = snd_usbmidi_us122l_input,
733 .output = snd_usbmidi_us122l_output,
734};
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/*
737 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
738 */
739
Takashi Iwai86e07d32005-11-17 15:08:02 +0100740static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 static const u8 init_data[] = {
743 /* initialization magic: "get version" */
744 0xf0,
745 0x00, 0x20, 0x31, /* Emagic */
746 0x64, /* Unitor8 */
747 0x0b, /* version number request */
748 0x00, /* command version */
749 0x00, /* EEPROM, box 0 */
750 0xf7
751 };
752 send_bulk_static_data(ep, init_data, sizeof(init_data));
753 /* while we're at it, pour on more magic */
754 send_bulk_static_data(ep, init_data, sizeof(init_data));
755}
756
Takashi Iwai86e07d32005-11-17 15:08:02 +0100757static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 static const u8 finish_data[] = {
760 /* switch to patch mode with last preset */
761 0xf0,
762 0x00, 0x20, 0x31, /* Emagic */
763 0x64, /* Unitor8 */
764 0x10, /* patch switch command */
765 0x00, /* command version */
766 0x7f, /* to all boxes */
767 0x40, /* last preset in EEPROM */
768 0xf7
769 };
770 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
771}
772
Takashi Iwai86e07d32005-11-17 15:08:02 +0100773static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 uint8_t* buffer, int buffer_length)
775{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200776 int i;
777
778 /* FF indicates end of valid data */
779 for (i = 0; i < buffer_length; ++i)
780 if (buffer[i] == 0xff) {
781 buffer_length = i;
782 break;
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* handle F5 at end of last buffer */
786 if (ep->seen_f5)
787 goto switch_port;
788
789 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /* determine size of data until next F5 */
791 for (i = 0; i < buffer_length; ++i)
792 if (buffer[i] == 0xf5)
793 break;
794 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
795 buffer += i;
796 buffer_length -= i;
797
798 if (buffer_length <= 0)
799 break;
800 /* assert(buffer[0] == 0xf5); */
801 ep->seen_f5 = 1;
802 ++buffer;
803 --buffer_length;
804
805 switch_port:
806 if (buffer_length <= 0)
807 break;
808 if (buffer[0] < 0x80) {
809 ep->current_port = (buffer[0] - 1) & 15;
810 ++buffer;
811 --buffer_length;
812 }
813 ep->seen_f5 = 0;
814 }
815}
816
Clemens Ladisched4affa2009-07-13 13:43:59 +0200817static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
818 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 int port0 = ep->current_port;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200821 uint8_t* buf = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int buf_free = ep->max_transfer;
823 int length, i;
824
825 for (i = 0; i < 0x10; ++i) {
826 /* round-robin, starting at the last current port */
827 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100828 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (!port->active)
831 continue;
832 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
833 port->active = 0;
834 continue;
835 }
836
837 if (portnum != ep->current_port) {
838 if (buf_free < 2)
839 break;
840 ep->current_port = portnum;
841 buf[0] = 0xf5;
842 buf[1] = (portnum + 1) & 15;
843 buf += 2;
844 buf_free -= 2;
845 }
846
847 if (buf_free < 1)
848 break;
849 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
850 if (length > 0) {
851 buf += length;
852 buf_free -= length;
853 if (buf_free < 1)
854 break;
855 }
856 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200857 if (buf_free < ep->max_transfer && buf_free > 0) {
858 *buf = 0xff;
859 --buf_free;
860 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200861 urb->transfer_buffer_length = ep->max_transfer - buf_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
865 .input = snd_usbmidi_emagic_input,
866 .output = snd_usbmidi_emagic_output,
867 .init_out_endpoint = snd_usbmidi_emagic_init_out,
868 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
869};
870
871
Takashi Iwai86e07d32005-11-17 15:08:02 +0100872static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100874 struct snd_usb_midi* umidi = substream->rmidi->private_data;
875 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int i, j;
877
878 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
879 if (umidi->endpoints[i].out)
880 for (j = 0; j < 0x10; ++j)
881 if (umidi->endpoints[i].out->ports[j].substream == substream) {
882 port = &umidi->endpoints[i].out->ports[j];
883 break;
884 }
885 if (!port) {
886 snd_BUG();
887 return -ENXIO;
888 }
889 substream->runtime->private_data = port;
890 port->state = STATE_UNKNOWN;
891 return 0;
892}
893
Takashi Iwai86e07d32005-11-17 15:08:02 +0100894static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 return 0;
897}
898
Takashi Iwai86e07d32005-11-17 15:08:02 +0100899static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100901 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 port->active = up;
904 if (up) {
905 if (port->ep->umidi->chip->shutdown) {
906 /* gobble up remaining bytes to prevent wait in
907 * snd_rawmidi_drain_output */
908 while (!snd_rawmidi_transmit_empty(substream))
909 snd_rawmidi_transmit_ack(substream, 1);
910 return;
911 }
Takashi Iwai1f041282008-12-18 12:17:55 +0100912 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914}
915
Takashi Iwai86e07d32005-11-17 15:08:02 +0100916static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 return 0;
919}
920
Takashi Iwai86e07d32005-11-17 15:08:02 +0100921static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 return 0;
924}
925
Takashi Iwai86e07d32005-11-17 15:08:02 +0100926static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100928 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (up)
931 set_bit(substream->number, &umidi->input_triggered);
932 else
933 clear_bit(substream->number, &umidi->input_triggered);
934}
935
Takashi Iwai86e07d32005-11-17 15:08:02 +0100936static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 .open = snd_usbmidi_output_open,
938 .close = snd_usbmidi_output_close,
939 .trigger = snd_usbmidi_output_trigger,
940};
941
Takashi Iwai86e07d32005-11-17 15:08:02 +0100942static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 .open = snd_usbmidi_input_open,
944 .close = snd_usbmidi_input_close,
945 .trigger = snd_usbmidi_input_trigger
946};
947
Clemens Ladisched4affa2009-07-13 13:43:59 +0200948static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
949 unsigned int buffer_length)
950{
951 usb_buffer_free(umidi->chip->dev, buffer_length,
952 urb->transfer_buffer, urb->transfer_dma);
953 usb_free_urb(urb);
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/*
957 * Frees an input endpoint.
958 * May be called when ep hasn't been initialized completely.
959 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100960static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200962 unsigned int i;
963
Clemens Ladisched4affa2009-07-13 13:43:59 +0200964 for (i = 0; i < INPUT_URBS; ++i)
965 if (ep->urbs[i])
966 free_urb_and_buffer(ep->umidi, ep->urbs[i],
967 ep->urbs[i]->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 kfree(ep);
969}
970
971/*
972 * Creates an input endpoint.
973 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100974static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
975 struct snd_usb_midi_endpoint_info* ep_info,
976 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100978 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 void* buffer;
980 unsigned int pipe;
981 int length;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200982 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200985 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (!ep)
987 return -ENOMEM;
988 ep->umidi = umidi;
989
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200990 for (i = 0; i < INPUT_URBS; ++i) {
991 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
992 if (!ep->urbs[i]) {
993 snd_usbmidi_in_endpoint_delete(ep);
994 return -ENOMEM;
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 if (ep_info->in_interval)
998 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
999 else
1000 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
1001 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001002 for (i = 0; i < INPUT_URBS; ++i) {
1003 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
1004 &ep->urbs[i]->transfer_dma);
1005 if (!buffer) {
1006 snd_usbmidi_in_endpoint_delete(ep);
1007 return -ENOMEM;
1008 }
1009 if (ep_info->in_interval)
1010 usb_fill_int_urb(ep->urbs[i], umidi->chip->dev,
1011 pipe, buffer, length,
1012 snd_usbmidi_in_urb_complete,
1013 ep, ep_info->in_interval);
1014 else
1015 usb_fill_bulk_urb(ep->urbs[i], umidi->chip->dev,
1016 pipe, buffer, length,
1017 snd_usbmidi_in_urb_complete, ep);
1018 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 rep->in = ep;
1022 return 0;
1023}
1024
1025static unsigned int snd_usbmidi_count_bits(unsigned int x)
1026{
Clemens Ladisch62f09c32006-02-27 09:53:03 +01001027 unsigned int bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Clemens Ladisch62f09c32006-02-27 09:53:03 +01001029 for (bits = 0; x; ++bits)
1030 x &= x - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return bits;
1032}
1033
1034/*
1035 * Frees an output endpoint.
1036 * May be called when ep hasn't been initialized completely.
1037 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001038static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Clemens Ladisched4affa2009-07-13 13:43:59 +02001040 unsigned int i;
1041
1042 for (i = 0; i < OUTPUT_URBS; ++i)
1043 if (ep->urbs[i].urb)
1044 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1045 ep->max_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 kfree(ep);
1047}
1048
1049/*
1050 * Creates an output endpoint, and initializes output ports.
1051 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001052static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1053 struct snd_usb_midi_endpoint_info* ep_info,
1054 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001056 struct snd_usb_midi_out_endpoint* ep;
Clemens Ladisched4affa2009-07-13 13:43:59 +02001057 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 unsigned int pipe;
1059 void* buffer;
1060
1061 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001062 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!ep)
1064 return -ENOMEM;
1065 ep->umidi = umidi;
1066
Clemens Ladisched4affa2009-07-13 13:43:59 +02001067 for (i = 0; i < OUTPUT_URBS; ++i) {
1068 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1069 if (!ep->urbs[i].urb) {
1070 snd_usbmidi_out_endpoint_delete(ep);
1071 return -ENOMEM;
1072 }
1073 ep->urbs[i].ep = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001075 if (ep_info->out_interval)
1076 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
1077 else
1078 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
Clemens Ladisch490cbd92007-05-07 09:29:32 +02001079 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
Clemens Ladisch490cbd92007-05-07 09:29:32 +02001080 ep->max_transfer = 4;
1081 else
1082 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001083 for (i = 0; i < OUTPUT_URBS; ++i) {
1084 buffer = usb_buffer_alloc(umidi->chip->dev,
1085 ep->max_transfer, GFP_KERNEL,
1086 &ep->urbs[i].urb->transfer_dma);
1087 if (!buffer) {
1088 snd_usbmidi_out_endpoint_delete(ep);
1089 return -ENOMEM;
1090 }
1091 if (ep_info->out_interval)
1092 usb_fill_int_urb(ep->urbs[i].urb, umidi->chip->dev,
1093 pipe, buffer, ep->max_transfer,
1094 snd_usbmidi_out_urb_complete,
1095 &ep->urbs[i], ep_info->out_interval);
1096 else
1097 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->chip->dev,
1098 pipe, buffer, ep->max_transfer,
1099 snd_usbmidi_out_urb_complete,
1100 &ep->urbs[i]);
1101 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 spin_lock_init(&ep->buffer_lock);
1105 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1106
1107 for (i = 0; i < 0x10; ++i)
1108 if (ep_info->out_cables & (1 << i)) {
1109 ep->ports[i].ep = ep;
1110 ep->ports[i].cable = i << 4;
1111 }
1112
1113 if (umidi->usb_protocol_ops->init_out_endpoint)
1114 umidi->usb_protocol_ops->init_out_endpoint(ep);
1115
1116 rep->out = ep;
1117 return 0;
1118}
1119
1120/*
1121 * Frees everything.
1122 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001123static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 int i;
1126
1127 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001128 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (ep->out)
1130 snd_usbmidi_out_endpoint_delete(ep->out);
1131 if (ep->in)
1132 snd_usbmidi_in_endpoint_delete(ep->in);
1133 }
1134 kfree(umidi);
1135}
1136
1137/*
1138 * Unlinks all URBs (must be done before the usb_device is deleted).
1139 */
Clemens Ladischee733392005-04-25 10:34:13 +02001140void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001142 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001143 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Takashi Iwai86e07d32005-11-17 15:08:02 +01001145 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001146 /*
1147 * an URB's completion handler may start the timer and
1148 * a timer may submit an URB. To reliably break the cycle
1149 * a flag under lock must be used
1150 */
1151 spin_lock_irq(&umidi->disc_lock);
1152 umidi->disconnected = 1;
1153 spin_unlock_irq(&umidi->disc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001155 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001156 if (ep->out)
1157 tasklet_kill(&ep->out->tasklet);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001158 if (ep->out) {
1159 for (j = 0; j < OUTPUT_URBS; ++j)
1160 usb_kill_urb(ep->out->urbs[j].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (umidi->usb_protocol_ops->finish_out_endpoint)
1162 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1163 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001164 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001165 for (j = 0; j < INPUT_URBS; ++j)
1166 usb_kill_urb(ep->in->urbs[j]);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001167 /* free endpoints here; later call can result in Oops */
1168 if (ep->out) {
1169 snd_usbmidi_out_endpoint_delete(ep->out);
1170 ep->out = NULL;
1171 }
1172 if (ep->in) {
1173 snd_usbmidi_in_endpoint_delete(ep->in);
1174 ep->in = NULL;
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001177 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Takashi Iwai86e07d32005-11-17 15:08:02 +01001180static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001182 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 snd_usbmidi_free(umidi);
1184}
1185
Takashi Iwai86e07d32005-11-17 15:08:02 +01001186static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 int stream, int number)
1188{
1189 struct list_head* list;
1190
1191 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001192 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (substream->number == number)
1194 return substream;
1195 }
1196 return NULL;
1197}
1198
1199/*
1200 * This list specifies names for ports that do not fit into the standard
1201 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1202 * such as internal control or synthesizer ports.
1203 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001204static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001205 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001206 short int port;
1207 short int voices;
1208 const char *name;
1209 unsigned int seq_flags;
1210} snd_usbmidi_port_info[] = {
1211#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1212 { .id = USB_ID(vendor, product), \
1213 .port = num, .voices = voices_, \
1214 .name = name_, .seq_flags = flags }
1215#define EXTERNAL_PORT(vendor, product, num, name) \
1216 PORT_INFO(vendor, product, num, name, 0, \
1217 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1218 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1219 SNDRV_SEQ_PORT_TYPE_PORT)
1220#define CONTROL_PORT(vendor, product, num, name) \
1221 PORT_INFO(vendor, product, num, name, 0, \
1222 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1223 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1224#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1225 PORT_INFO(vendor, product, num, name, voices, \
1226 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1227 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1228 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1229 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1230 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1231 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1232 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1233#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1234 PORT_INFO(vendor, product, num, name, voices, \
1235 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1236 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1237 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1238 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1239 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1240 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1241 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1242 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001244 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001246 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1247 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1248 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1249 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1250 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1251 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001253 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1254 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001256 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1257 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1258 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001260 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1261 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1262 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001264 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1265 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1266 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001268 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001270 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1271 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1272 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1273 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001275 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001277 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1278 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1279 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001281 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1282 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1283 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1284 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001286 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1287 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001289 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1290 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1291 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001293 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1294 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1295 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001297 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1298 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001300 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1301 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001303 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1304 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1305 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001307 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1308 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1309 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001310 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001311 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001313 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1314 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001316 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1317 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001319 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1320 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1321 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322};
1323
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001324static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1325{
1326 int i;
1327
1328 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1329 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1330 snd_usbmidi_port_info[i].port == number)
1331 return &snd_usbmidi_port_info[i];
1332 }
1333 return NULL;
1334}
1335
1336static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1337 struct snd_seq_port_info *seq_port_info)
1338{
1339 struct snd_usb_midi *umidi = rmidi->private_data;
1340 struct port_info *port_info;
1341
1342 /* TODO: read port flags from descriptors */
1343 port_info = find_port_info(umidi, number);
1344 if (port_info) {
1345 seq_port_info->type = port_info->seq_flags;
1346 seq_port_info->midi_voices = port_info->voices;
1347 }
1348}
1349
Takashi Iwai86e07d32005-11-17 15:08:02 +01001350static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001352 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001354 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 const char *name_format;
1356
Takashi Iwai86e07d32005-11-17 15:08:02 +01001357 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!substream) {
1359 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1360 return;
1361 }
1362
1363 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001364 port_info = find_port_info(umidi, number);
1365 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 snprintf(substream->name, sizeof(substream->name),
1367 name_format, umidi->chip->card->shortname, number + 1);
1368
1369 *rsubstream = substream;
1370}
1371
1372/*
1373 * Creates the endpoints and their ports.
1374 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001375static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1376 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 int i, j, err;
1379 int out_ports = 0, in_ports = 0;
1380
1381 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1382 if (endpoints[i].out_cables) {
1383 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1384 &umidi->endpoints[i]);
1385 if (err < 0)
1386 return err;
1387 }
1388 if (endpoints[i].in_cables) {
1389 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1390 &umidi->endpoints[i]);
1391 if (err < 0)
1392 return err;
1393 }
1394
1395 for (j = 0; j < 0x10; ++j) {
1396 if (endpoints[i].out_cables & (1 << j)) {
1397 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1398 &umidi->endpoints[i].out->ports[j].substream);
1399 ++out_ports;
1400 }
1401 if (endpoints[i].in_cables & (1 << j)) {
1402 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1403 &umidi->endpoints[i].in->ports[j].substream);
1404 ++in_ports;
1405 }
1406 }
1407 }
1408 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1409 out_ports, in_ports);
1410 return 0;
1411}
1412
1413/*
1414 * Returns MIDIStreaming device capabilities.
1415 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001416static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1417 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 struct usb_interface* intf;
1420 struct usb_host_interface *hostif;
1421 struct usb_interface_descriptor* intfd;
1422 struct usb_ms_header_descriptor* ms_header;
1423 struct usb_host_endpoint *hostep;
1424 struct usb_endpoint_descriptor* ep;
1425 struct usb_ms_endpoint_descriptor* ms_ep;
1426 int i, epidx;
1427
1428 intf = umidi->iface;
1429 if (!intf)
1430 return -ENXIO;
1431 hostif = &intf->altsetting[0];
1432 intfd = get_iface_desc(hostif);
1433 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1434 if (hostif->extralen >= 7 &&
1435 ms_header->bLength >= 7 &&
1436 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1437 ms_header->bDescriptorSubtype == HEADER)
1438 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1439 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1440 else
1441 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1442
1443 epidx = 0;
1444 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1445 hostep = &hostif->endpoint[i];
1446 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001447 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 continue;
1449 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1450 if (hostep->extralen < 4 ||
1451 ms_ep->bLength < 4 ||
1452 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1453 ms_ep->bDescriptorSubtype != MS_GENERAL)
1454 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001455 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (endpoints[epidx].out_ep) {
1457 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1458 snd_printk(KERN_WARNING "too many endpoints\n");
1459 break;
1460 }
1461 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001462 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1463 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001465 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1466 /*
1467 * Low speed bulk transfers don't exist, so
1468 * force interrupt transfers for devices like
1469 * ESI MIDI Mate that try to use them anyway.
1470 */
1471 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1473 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1474 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1475 } else {
1476 if (endpoints[epidx].in_ep) {
1477 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1478 snd_printk(KERN_WARNING "too many endpoints\n");
1479 break;
1480 }
1481 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001482 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1483 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001485 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1486 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1488 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1489 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1490 }
1491 }
1492 return 0;
1493}
1494
1495/*
1496 * On Roland devices, use the second alternate setting to be able to use
1497 * the interrupt input endpoint.
1498 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001499static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 struct usb_interface* intf;
1502 struct usb_host_interface *hostif;
1503 struct usb_interface_descriptor* intfd;
1504
1505 intf = umidi->iface;
1506 if (!intf || intf->num_altsetting != 2)
1507 return;
1508
1509 hostif = &intf->altsetting[1];
1510 intfd = get_iface_desc(hostif);
1511 if (intfd->bNumEndpoints != 2 ||
1512 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1513 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1514 return;
1515
1516 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1517 intfd->bAlternateSetting);
1518 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1519 intfd->bAlternateSetting);
1520}
1521
1522/*
1523 * Try to find any usable endpoints in the interface.
1524 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001525static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1526 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 int max_endpoints)
1528{
1529 struct usb_interface* intf;
1530 struct usb_host_interface *hostif;
1531 struct usb_interface_descriptor* intfd;
1532 struct usb_endpoint_descriptor* epd;
1533 int i, out_eps = 0, in_eps = 0;
1534
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001535 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 snd_usbmidi_switch_roland_altsetting(umidi);
1537
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001538 if (endpoint[0].out_ep || endpoint[0].in_ep)
1539 return 0;
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 intf = umidi->iface;
1542 if (!intf || intf->num_altsetting < 1)
1543 return -ENOENT;
1544 hostif = intf->cur_altsetting;
1545 intfd = get_iface_desc(hostif);
1546
1547 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1548 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001549 if (!usb_endpoint_xfer_bulk(epd) &&
1550 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 continue;
1552 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001553 usb_endpoint_dir_out(epd)) {
1554 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1555 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 endpoint[out_eps].out_interval = epd->bInterval;
1557 ++out_eps;
1558 }
1559 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001560 usb_endpoint_dir_in(epd)) {
1561 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1562 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 endpoint[in_eps].in_interval = epd->bInterval;
1564 ++in_eps;
1565 }
1566 }
1567 return (out_eps || in_eps) ? 0 : -ENOENT;
1568}
1569
1570/*
1571 * Detects the endpoints for one-port-per-endpoint protocols.
1572 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001573static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1574 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
1576 int err, i;
1577
1578 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1579 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1580 if (endpoints[i].out_ep)
1581 endpoints[i].out_cables = 0x0001;
1582 if (endpoints[i].in_ep)
1583 endpoints[i].in_cables = 0x0001;
1584 }
1585 return err;
1586}
1587
1588/*
1589 * Detects the endpoints and ports of Yamaha devices.
1590 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001591static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1592 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 struct usb_interface* intf;
1595 struct usb_host_interface *hostif;
1596 struct usb_interface_descriptor* intfd;
1597 uint8_t* cs_desc;
1598
1599 intf = umidi->iface;
1600 if (!intf)
1601 return -ENOENT;
1602 hostif = intf->altsetting;
1603 intfd = get_iface_desc(hostif);
1604 if (intfd->bNumEndpoints < 1)
1605 return -ENOENT;
1606
1607 /*
1608 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1609 * necessarily with any useful contents. So simply count 'em.
1610 */
1611 for (cs_desc = hostif->extra;
1612 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1613 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001614 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (cs_desc[2] == MIDI_IN_JACK)
1616 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1617 else if (cs_desc[2] == MIDI_OUT_JACK)
1618 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1619 }
1620 }
1621 if (!endpoint->in_cables && !endpoint->out_cables)
1622 return -ENOENT;
1623
1624 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1625}
1626
1627/*
1628 * Creates the endpoints and their ports for Midiman devices.
1629 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001630static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1631 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001633 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct usb_interface* intf;
1635 struct usb_host_interface *hostif;
1636 struct usb_interface_descriptor* intfd;
1637 struct usb_endpoint_descriptor* epd;
1638 int cable, err;
1639
1640 intf = umidi->iface;
1641 if (!intf)
1642 return -ENOENT;
1643 hostif = intf->altsetting;
1644 intfd = get_iface_desc(hostif);
1645 /*
1646 * The various MidiSport devices have more or less random endpoint
1647 * numbers, so we have to identify the endpoints by their index in
1648 * the descriptor array, like the driver for that other OS does.
1649 *
1650 * There is one interrupt input endpoint for all input ports, one
1651 * bulk output endpoint for even-numbered ports, and one for odd-
1652 * numbered ports. Both bulk output endpoints have corresponding
1653 * input bulk endpoints (at indices 1 and 3) which aren't used.
1654 */
1655 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1656 snd_printdd(KERN_ERR "not enough endpoints\n");
1657 return -ENOENT;
1658 }
1659
1660 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001661 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1663 return -ENXIO;
1664 }
1665 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001666 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1668 return -ENXIO;
1669 }
1670 if (endpoint->out_cables > 0x0001) {
1671 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001672 if (!usb_endpoint_dir_out(epd) ||
1673 !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1675 return -ENXIO;
1676 }
1677 }
1678
1679 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01001680 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 ep_info.out_cables = endpoint->out_cables & 0x5555;
1682 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1683 if (err < 0)
1684 return err;
1685
1686 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1687 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1688 ep_info.in_cables = endpoint->in_cables;
1689 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1690 if (err < 0)
1691 return err;
1692
1693 if (endpoint->out_cables > 0x0001) {
1694 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1695 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1696 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1697 if (err < 0)
1698 return err;
1699 }
1700
1701 for (cable = 0; cable < 0x10; ++cable) {
1702 if (endpoint->out_cables & (1 << cable))
1703 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1704 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1705 if (endpoint->in_cables & (1 << cable))
1706 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1707 &umidi->endpoints[0].in->ports[cable].substream);
1708 }
1709 return 0;
1710}
1711
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001712static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1713 .get_port_info = snd_usbmidi_get_port_info,
1714};
1715
Takashi Iwai86e07d32005-11-17 15:08:02 +01001716static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 int out_ports, int in_ports)
1718{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001719 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 int err;
1721
1722 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1723 umidi->chip->next_midi_device++,
1724 out_ports, in_ports, &rmidi);
1725 if (err < 0)
1726 return err;
1727 strcpy(rmidi->name, umidi->chip->card->shortname);
1728 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1729 SNDRV_RAWMIDI_INFO_INPUT |
1730 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001731 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 rmidi->private_data = umidi;
1733 rmidi->private_free = snd_usbmidi_rawmidi_free;
1734 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1735 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1736
1737 umidi->rmidi = rmidi;
1738 return 0;
1739}
1740
1741/*
1742 * Temporarily stop input.
1743 */
1744void snd_usbmidi_input_stop(struct list_head* p)
1745{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001746 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001747 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Takashi Iwai86e07d32005-11-17 15:08:02 +01001749 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001751 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001753 for (j = 0; j < INPUT_URBS; ++j)
1754 usb_kill_urb(ep->in->urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756}
1757
Takashi Iwai86e07d32005-11-17 15:08:02 +01001758static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001760 unsigned int i;
1761
1762 if (!ep)
1763 return;
1764 for (i = 0; i < INPUT_URBS; ++i) {
1765 struct urb* urb = ep->urbs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 urb->dev = ep->umidi->chip->dev;
1767 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1768 }
1769}
1770
1771/*
1772 * Resume input after a call to snd_usbmidi_input_stop().
1773 */
1774void snd_usbmidi_input_start(struct list_head* p)
1775{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001776 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int i;
1778
Takashi Iwai86e07d32005-11-17 15:08:02 +01001779 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1781 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1782}
1783
1784/*
1785 * Creates and registers everything needed for a MIDI streaming interface.
1786 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001787int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct usb_interface* iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001789 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001791 struct snd_usb_midi* umidi;
1792 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 int out_ports, in_ports;
1794 int i, err;
1795
Takashi Iwai561b2202005-09-09 14:22:34 +02001796 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (!umidi)
1798 return -ENOMEM;
1799 umidi->chip = chip;
1800 umidi->iface = iface;
1801 umidi->quirk = quirk;
1802 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001803 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001804 spin_lock_init(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +02001805 umidi->error_timer.function = snd_usbmidi_error_timer;
1806 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 /* detect the endpoint(s) to use */
1809 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001810 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1811 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd05cc10432007-05-07 09:28:53 +02001813 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1814 umidi->usb_protocol_ops =
1815 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001816 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02001817 case QUIRK_MIDI_US122L:
1818 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1819 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02001820 case QUIRK_MIDI_FIXED_ENDPOINT:
1821 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001822 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001823 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1824 break;
1825 case QUIRK_MIDI_YAMAHA:
1826 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1827 break;
1828 case QUIRK_MIDI_MIDIMAN:
1829 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1830 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001831 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001832 err = 0;
1833 break;
1834 case QUIRK_MIDI_NOVATION:
1835 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1836 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1837 break;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001838 case QUIRK_MIDI_FASTLANE:
Clemens Ladischd1bda042005-09-14 08:36:03 +02001839 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001840 /*
1841 * Interface 1 contains isochronous endpoints, but with the same
1842 * numbers as in interface 0. Since it is interface 1 that the
1843 * USB core has most recently seen, these descriptors are now
1844 * associated with the endpoint numbers. This will foul up our
1845 * attempts to submit bulk/interrupt URBs to the endpoints in
1846 * interface 0, so we have to make sure that the USB core looks
1847 * again at interface 0 by calling usb_set_interface() on it.
1848 */
1849 usb_set_interface(umidi->chip->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02001850 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1851 break;
1852 case QUIRK_MIDI_EMAGIC:
1853 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1854 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001855 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001856 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1857 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01001858 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02001859 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001860 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1861 break;
1862 default:
1863 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1864 err = -ENXIO;
1865 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867 if (err < 0) {
1868 kfree(umidi);
1869 return err;
1870 }
1871
1872 /* create rawmidi device */
1873 out_ports = 0;
1874 in_ports = 0;
1875 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1876 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1877 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1878 }
1879 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1880 if (err < 0) {
1881 kfree(umidi);
1882 return err;
1883 }
1884
1885 /* create endpoint/port structures */
1886 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1887 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1888 else
1889 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1890 if (err < 0) {
1891 snd_usbmidi_free(umidi);
1892 return err;
1893 }
1894
1895 list_add(&umidi->list, &umidi->chip->midi_list);
1896
1897 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1898 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1899 return 0;
1900}
1901
1902EXPORT_SYMBOL(snd_usb_create_midi_interface);
1903EXPORT_SYMBOL(snd_usbmidi_input_stop);
1904EXPORT_SYMBOL(snd_usbmidi_input_start);
1905EXPORT_SYMBOL(snd_usbmidi_disconnect);