blob: cc17401de95eb15cfefcfa7678337bf77b544e3e [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 Ladisch4773d1f2009-07-13 13:40:36 +020065#define INPUT_URBS 7
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
69MODULE_DESCRIPTION("USB Audio/MIDI helper module");
70MODULE_LICENSE("Dual BSD/GPL");
71
72
73struct usb_ms_header_descriptor {
74 __u8 bLength;
75 __u8 bDescriptorType;
76 __u8 bDescriptorSubtype;
77 __u8 bcdMSC[2];
78 __le16 wTotalLength;
79} __attribute__ ((packed));
80
81struct usb_ms_endpoint_descriptor {
82 __u8 bLength;
83 __u8 bDescriptorType;
84 __u8 bDescriptorSubtype;
85 __u8 bNumEmbMIDIJack;
86 __u8 baAssocJackID[0];
87} __attribute__ ((packed));
88
Takashi Iwai86e07d32005-11-17 15:08:02 +010089struct snd_usb_midi_in_endpoint;
90struct snd_usb_midi_out_endpoint;
91struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +010094 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
95 void (*output)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Takashi Iwai86e07d32005-11-17 15:08:02 +010097 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
98 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101struct snd_usb_midi {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100102 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100104 const struct snd_usb_audio_quirk *quirk;
105 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct usb_protocol_ops* usb_protocol_ops;
107 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200108 struct timer_list error_timer;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100109 spinlock_t disc_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100112 struct snd_usb_midi_out_endpoint *out;
113 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 } endpoints[MIDI_MAX_ENDPOINTS];
115 unsigned long input_triggered;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100116 unsigned char disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100120 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct urb* urb;
122 int urb_active;
123 int max_transfer; /* size of urb buffer */
124 struct tasklet_struct tasklet;
125
126 spinlock_t buffer_lock;
127
128 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100129 struct snd_usb_midi_out_endpoint* ep;
130 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int active;
132 uint8_t cable; /* cable number << 4 */
133 uint8_t state;
134#define STATE_UNKNOWN 0
135#define STATE_1PARAM 1
136#define STATE_2PARAM_1 2
137#define STATE_2PARAM_2 3
138#define STATE_SYSEX_0 4
139#define STATE_SYSEX_1 5
140#define STATE_SYSEX_2 6
141 uint8_t data[2];
142 } ports[0x10];
143 int current_port;
144};
145
146struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100147 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200148 struct urb* urbs[INPUT_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100150 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200151 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200153 u8 seen_f5;
154 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 int current_port;
156};
157
Takashi Iwai86e07d32005-11-17 15:08:02 +0100158static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160static const uint8_t snd_usbmidi_cin_length[] = {
161 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
162};
163
164/*
165 * Submits the URB, with error handling.
166 */
Al Viro55016f12005-10-21 03:21:58 -0400167static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 int err = usb_submit_urb(urb, flags);
170 if (err < 0 && err != -ENODEV)
171 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
172 return err;
173}
174
175/*
176 * Error handling for URB completion functions.
177 */
178static int snd_usbmidi_urb_error(int status)
179{
Clemens Ladischc8846972005-08-02 15:26:52 +0200180 switch (status) {
181 /* manually unlinked, or device gone */
182 case -ENOENT:
183 case -ECONNRESET:
184 case -ESHUTDOWN:
185 case -ENODEV:
186 return -ENODEV;
187 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700188 case -EPROTO:
189 case -ETIME:
190 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200191 return -EIO;
192 default:
193 snd_printk(KERN_ERR "urb status %d\n", status);
194 return 0; /* continue */
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/*
199 * Receives a chunk of MIDI data.
200 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100201static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 uint8_t* data, int length)
203{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100204 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (!port->substream) {
207 snd_printd("unexpected port %d!\n", portidx);
208 return;
209 }
210 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
211 return;
212 snd_rawmidi_receive(port->substream, data, length);
213}
214
215#ifdef DUMP_PACKETS
216static void dump_urb(const char *type, const u8 *data, int length)
217{
218 snd_printk(KERN_DEBUG "%s packet: [", type);
219 for (; length > 0; ++data, --length)
220 printk(" %02x", *data);
221 printk(" ]\n");
222}
223#else
224#define dump_urb(type, data, length) /* nothing */
225#endif
226
227/*
228 * Processes the data read from the device.
229 */
David Howells7d12e782006-10-05 14:55:46 +0100230static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100232 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (urb->status == 0) {
235 dump_urb("received", urb->transfer_buffer, urb->actual_length);
236 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
237 urb->actual_length);
238 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200239 int err = snd_usbmidi_urb_error(urb->status);
240 if (err < 0) {
241 if (err != -ENODEV) {
242 ep->error_resubmit = 1;
243 mod_timer(&ep->umidi->error_timer,
244 jiffies + ERROR_DELAY_JIFFIES);
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200250 urb->dev = ep->umidi->chip->dev;
251 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
David Howells7d12e782006-10-05 14:55:46 +0100254static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100256 struct snd_usb_midi_out_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 spin_lock(&ep->buffer_lock);
259 ep->urb_active = 0;
260 spin_unlock(&ep->buffer_lock);
261 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200262 int err = snd_usbmidi_urb_error(urb->status);
263 if (err < 0) {
264 if (err != -ENODEV)
265 mod_timer(&ep->umidi->error_timer,
266 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 snd_usbmidi_do_output(ep);
271}
272
273/*
274 * This is called when some data should be transferred to the device
275 * (from one or more substreams).
276 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100277static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct urb* urb = ep->urb;
280 unsigned long flags;
281
282 spin_lock_irqsave(&ep->buffer_lock, flags);
283 if (ep->urb_active || ep->umidi->chip->shutdown) {
284 spin_unlock_irqrestore(&ep->buffer_lock, flags);
285 return;
286 }
287
288 urb->transfer_buffer_length = 0;
289 ep->umidi->usb_protocol_ops->output(ep);
290
291 if (urb->transfer_buffer_length > 0) {
292 dump_urb("sending", urb->transfer_buffer,
293 urb->transfer_buffer_length);
294 urb->dev = ep->umidi->chip->dev;
295 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
296 }
297 spin_unlock_irqrestore(&ep->buffer_lock, flags);
298}
299
300static void snd_usbmidi_out_tasklet(unsigned long data)
301{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100302 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 snd_usbmidi_do_output(ep);
305}
306
Clemens Ladischc8846972005-08-02 15:26:52 +0200307/* called after transfers had been interrupted due to some USB error */
308static void snd_usbmidi_error_timer(unsigned long data)
309{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100310 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200311 unsigned int i, j;
Clemens Ladischc8846972005-08-02 15:26:52 +0200312
Takashi Iwaic0792e02008-02-22 18:34:44 +0100313 spin_lock(&umidi->disc_lock);
314 if (umidi->disconnected) {
315 spin_unlock(&umidi->disc_lock);
316 return;
317 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200318 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100319 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200320 if (in && in->error_resubmit) {
321 in->error_resubmit = 0;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200322 for (j = 0; j < INPUT_URBS; ++j) {
323 in->urbs[j]->dev = umidi->chip->dev;
324 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
325 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200326 }
327 if (umidi->endpoints[i].out)
328 snd_usbmidi_do_output(umidi->endpoints[i].out);
329 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100330 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100334static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 const void *data, int len)
336{
337 int err;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700338 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!buf)
340 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 dump_urb("sending", buf, len);
342 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len,
343 NULL, 250);
344 kfree(buf);
345 return err;
346}
347
348/*
349 * Standard USB MIDI protocol: see the spec.
350 * Midiman protocol: like the standard protocol, but the control byte is the
351 * fourth byte in each packet, and uses length instead of CIN.
352 */
353
Takashi Iwai86e07d32005-11-17 15:08:02 +0100354static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 uint8_t* buffer, int buffer_length)
356{
357 int i;
358
359 for (i = 0; i + 3 < buffer_length; i += 4)
360 if (buffer[i] != 0) {
361 int cable = buffer[i] >> 4;
362 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
363 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
364 }
365}
366
Takashi Iwai86e07d32005-11-17 15:08:02 +0100367static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 uint8_t* buffer, int buffer_length)
369{
370 int i;
371
372 for (i = 0; i + 3 < buffer_length; i += 4)
373 if (buffer[i + 3] != 0) {
374 int port = buffer[i + 3] >> 4;
375 int length = buffer[i + 3] & 3;
376 snd_usbmidi_input_data(ep, port, &buffer[i], length);
377 }
378}
379
380/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200381 * Buggy M-Audio device: running status on input results in a packet that has
382 * the data bytes but not the status byte and that is marked with CIN 4.
383 */
384static void snd_usbmidi_maudio_broken_running_status_input(
385 struct snd_usb_midi_in_endpoint* ep,
386 uint8_t* buffer, int buffer_length)
387{
388 int i;
389
390 for (i = 0; i + 3 < buffer_length; i += 4)
391 if (buffer[i] != 0) {
392 int cable = buffer[i] >> 4;
393 u8 cin = buffer[i] & 0x0f;
394 struct usbmidi_in_port *port = &ep->ports[cable];
395 int length;
396
397 length = snd_usbmidi_cin_length[cin];
398 if (cin == 0xf && buffer[i + 1] >= 0xf8)
399 ; /* realtime msg: no running status change */
400 else if (cin >= 0x8 && cin <= 0xe)
401 /* channel msg */
402 port->running_status_length = length - 1;
403 else if (cin == 0x4 &&
404 port->running_status_length != 0 &&
405 buffer[i + 1] < 0x80)
406 /* CIN 4 that is not a SysEx */
407 length = port->running_status_length;
408 else
409 /*
410 * All other msgs cannot begin running status.
411 * (A channel msg sent as two or three CIN 0xF
412 * packets could in theory, but this device
413 * doesn't use this format.)
414 */
415 port->running_status_length = 0;
416 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
417 }
418}
419
420/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200421 * CME protocol: like the standard protocol, but SysEx commands are sent as a
422 * single USB packet preceded by a 0x0F byte.
423 */
424static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
425 uint8_t *buffer, int buffer_length)
426{
427 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
428 snd_usbmidi_standard_input(ep, buffer, buffer_length);
429 else
430 snd_usbmidi_input_data(ep, buffer[0] >> 4,
431 &buffer[1], buffer_length - 1);
432}
433
434/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * Adds one USB MIDI packet to the output buffer.
436 */
437static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
438 uint8_t p1, uint8_t p2, uint8_t p3)
439{
440
441 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
442 buf[0] = p0;
443 buf[1] = p1;
444 buf[2] = p2;
445 buf[3] = p3;
446 urb->transfer_buffer_length += 4;
447}
448
449/*
450 * Adds one Midiman packet to the output buffer.
451 */
452static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
453 uint8_t p1, uint8_t p2, uint8_t p3)
454{
455
456 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
457 buf[0] = p1;
458 buf[1] = p2;
459 buf[2] = p3;
460 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
461 urb->transfer_buffer_length += 4;
462}
463
464/*
465 * Converts MIDI commands to USB MIDI packets.
466 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100467static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 uint8_t b, struct urb* urb)
469{
470 uint8_t p0 = port->cable;
471 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
472 port->ep->umidi->usb_protocol_ops->output_packet;
473
474 if (b >= 0xf8) {
475 output_packet(urb, p0 | 0x0f, b, 0, 0);
476 } else if (b >= 0xf0) {
477 switch (b) {
478 case 0xf0:
479 port->data[0] = b;
480 port->state = STATE_SYSEX_1;
481 break;
482 case 0xf1:
483 case 0xf3:
484 port->data[0] = b;
485 port->state = STATE_1PARAM;
486 break;
487 case 0xf2:
488 port->data[0] = b;
489 port->state = STATE_2PARAM_1;
490 break;
491 case 0xf4:
492 case 0xf5:
493 port->state = STATE_UNKNOWN;
494 break;
495 case 0xf6:
496 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
497 port->state = STATE_UNKNOWN;
498 break;
499 case 0xf7:
500 switch (port->state) {
501 case STATE_SYSEX_0:
502 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
503 break;
504 case STATE_SYSEX_1:
505 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
506 break;
507 case STATE_SYSEX_2:
508 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
509 break;
510 }
511 port->state = STATE_UNKNOWN;
512 break;
513 }
514 } else if (b >= 0x80) {
515 port->data[0] = b;
516 if (b >= 0xc0 && b <= 0xdf)
517 port->state = STATE_1PARAM;
518 else
519 port->state = STATE_2PARAM_1;
520 } else { /* b < 0x80 */
521 switch (port->state) {
522 case STATE_1PARAM:
523 if (port->data[0] < 0xf0) {
524 p0 |= port->data[0] >> 4;
525 } else {
526 p0 |= 0x02;
527 port->state = STATE_UNKNOWN;
528 }
529 output_packet(urb, p0, port->data[0], b, 0);
530 break;
531 case STATE_2PARAM_1:
532 port->data[1] = b;
533 port->state = STATE_2PARAM_2;
534 break;
535 case STATE_2PARAM_2:
536 if (port->data[0] < 0xf0) {
537 p0 |= port->data[0] >> 4;
538 port->state = STATE_2PARAM_1;
539 } else {
540 p0 |= 0x03;
541 port->state = STATE_UNKNOWN;
542 }
543 output_packet(urb, p0, port->data[0], port->data[1], b);
544 break;
545 case STATE_SYSEX_0:
546 port->data[0] = b;
547 port->state = STATE_SYSEX_1;
548 break;
549 case STATE_SYSEX_1:
550 port->data[1] = b;
551 port->state = STATE_SYSEX_2;
552 break;
553 case STATE_SYSEX_2:
554 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
555 port->state = STATE_SYSEX_0;
556 break;
557 }
558 }
559}
560
Takashi Iwai86e07d32005-11-17 15:08:02 +0100561static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct urb* urb = ep->urb;
564 int p;
565
566 /* FIXME: lower-numbered ports can starve higher-numbered ports */
567 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100568 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!port->active)
570 continue;
571 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
572 uint8_t b;
573 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
574 port->active = 0;
575 break;
576 }
577 snd_usbmidi_transmit_byte(port, b, urb);
578 }
579 }
580}
581
582static struct usb_protocol_ops snd_usbmidi_standard_ops = {
583 .input = snd_usbmidi_standard_input,
584 .output = snd_usbmidi_standard_output,
585 .output_packet = snd_usbmidi_output_standard_packet,
586};
587
588static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
589 .input = snd_usbmidi_midiman_input,
590 .output = snd_usbmidi_standard_output,
591 .output_packet = snd_usbmidi_output_midiman_packet,
592};
593
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200594static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
595 .input = snd_usbmidi_maudio_broken_running_status_input,
596 .output = snd_usbmidi_standard_output,
597 .output_packet = snd_usbmidi_output_standard_packet,
598};
599
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200600static struct usb_protocol_ops snd_usbmidi_cme_ops = {
601 .input = snd_usbmidi_cme_input,
602 .output = snd_usbmidi_standard_output,
603 .output_packet = snd_usbmidi_output_standard_packet,
604};
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/*
607 * Novation USB MIDI protocol: number of data bytes is in the first byte
608 * (when receiving) (+1!) or in the second byte (when sending); data begins
609 * at the third byte.
610 */
611
Takashi Iwai86e07d32005-11-17 15:08:02 +0100612static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 uint8_t* buffer, int buffer_length)
614{
615 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
616 return;
617 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
618}
619
Takashi Iwai86e07d32005-11-17 15:08:02 +0100620static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 uint8_t* transfer_buffer;
623 int count;
624
625 if (!ep->ports[0].active)
626 return;
627 transfer_buffer = ep->urb->transfer_buffer;
628 count = snd_rawmidi_transmit(ep->ports[0].substream,
629 &transfer_buffer[2],
630 ep->max_transfer - 2);
631 if (count < 1) {
632 ep->ports[0].active = 0;
633 return;
634 }
635 transfer_buffer[0] = 0;
636 transfer_buffer[1] = count;
637 ep->urb->transfer_buffer_length = 2 + count;
638}
639
640static struct usb_protocol_ops snd_usbmidi_novation_ops = {
641 .input = snd_usbmidi_novation_input,
642 .output = snd_usbmidi_novation_output,
643};
644
645/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200646 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
648
Takashi Iwai86e07d32005-11-17 15:08:02 +0100649static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200650 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
653}
654
Takashi Iwai86e07d32005-11-17 15:08:02 +0100655static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 int count;
658
659 if (!ep->ports[0].active)
660 return;
661 count = snd_rawmidi_transmit(ep->ports[0].substream,
662 ep->urb->transfer_buffer,
663 ep->max_transfer);
664 if (count < 1) {
665 ep->ports[0].active = 0;
666 return;
667 }
668 ep->urb->transfer_buffer_length = count;
669}
670
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200671static struct usb_protocol_ops snd_usbmidi_raw_ops = {
672 .input = snd_usbmidi_raw_input,
673 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674};
675
Karsten Wiese030a07e2008-07-30 15:13:29 +0200676static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
677 uint8_t *buffer, int buffer_length)
678{
679 if (buffer_length != 9)
680 return;
681 buffer_length = 8;
682 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
683 buffer_length--;
684 if (buffer_length)
685 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
686}
687
688static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep)
689{
690 int count;
691
692 if (!ep->ports[0].active)
693 return;
694 count = ep->urb->dev->speed == USB_SPEED_HIGH ? 1 : 2;
695 count = snd_rawmidi_transmit(ep->ports[0].substream,
696 ep->urb->transfer_buffer,
697 count);
698 if (count < 1) {
699 ep->ports[0].active = 0;
700 return;
701 }
702
703 memset(ep->urb->transfer_buffer + count, 0xFD, 9 - count);
704 ep->urb->transfer_buffer_length = count;
705}
706
707static struct usb_protocol_ops snd_usbmidi_122l_ops = {
708 .input = snd_usbmidi_us122l_input,
709 .output = snd_usbmidi_us122l_output,
710};
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/*
713 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
714 */
715
Takashi Iwai86e07d32005-11-17 15:08:02 +0100716static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 static const u8 init_data[] = {
719 /* initialization magic: "get version" */
720 0xf0,
721 0x00, 0x20, 0x31, /* Emagic */
722 0x64, /* Unitor8 */
723 0x0b, /* version number request */
724 0x00, /* command version */
725 0x00, /* EEPROM, box 0 */
726 0xf7
727 };
728 send_bulk_static_data(ep, init_data, sizeof(init_data));
729 /* while we're at it, pour on more magic */
730 send_bulk_static_data(ep, init_data, sizeof(init_data));
731}
732
Takashi Iwai86e07d32005-11-17 15:08:02 +0100733static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 static const u8 finish_data[] = {
736 /* switch to patch mode with last preset */
737 0xf0,
738 0x00, 0x20, 0x31, /* Emagic */
739 0x64, /* Unitor8 */
740 0x10, /* patch switch command */
741 0x00, /* command version */
742 0x7f, /* to all boxes */
743 0x40, /* last preset in EEPROM */
744 0xf7
745 };
746 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
747}
748
Takashi Iwai86e07d32005-11-17 15:08:02 +0100749static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 uint8_t* buffer, int buffer_length)
751{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200752 int i;
753
754 /* FF indicates end of valid data */
755 for (i = 0; i < buffer_length; ++i)
756 if (buffer[i] == 0xff) {
757 buffer_length = i;
758 break;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /* handle F5 at end of last buffer */
762 if (ep->seen_f5)
763 goto switch_port;
764
765 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /* determine size of data until next F5 */
767 for (i = 0; i < buffer_length; ++i)
768 if (buffer[i] == 0xf5)
769 break;
770 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
771 buffer += i;
772 buffer_length -= i;
773
774 if (buffer_length <= 0)
775 break;
776 /* assert(buffer[0] == 0xf5); */
777 ep->seen_f5 = 1;
778 ++buffer;
779 --buffer_length;
780
781 switch_port:
782 if (buffer_length <= 0)
783 break;
784 if (buffer[0] < 0x80) {
785 ep->current_port = (buffer[0] - 1) & 15;
786 ++buffer;
787 --buffer_length;
788 }
789 ep->seen_f5 = 0;
790 }
791}
792
Takashi Iwai86e07d32005-11-17 15:08:02 +0100793static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 int port0 = ep->current_port;
796 uint8_t* buf = ep->urb->transfer_buffer;
797 int buf_free = ep->max_transfer;
798 int length, i;
799
800 for (i = 0; i < 0x10; ++i) {
801 /* round-robin, starting at the last current port */
802 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100803 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (!port->active)
806 continue;
807 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
808 port->active = 0;
809 continue;
810 }
811
812 if (portnum != ep->current_port) {
813 if (buf_free < 2)
814 break;
815 ep->current_port = portnum;
816 buf[0] = 0xf5;
817 buf[1] = (portnum + 1) & 15;
818 buf += 2;
819 buf_free -= 2;
820 }
821
822 if (buf_free < 1)
823 break;
824 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
825 if (length > 0) {
826 buf += length;
827 buf_free -= length;
828 if (buf_free < 1)
829 break;
830 }
831 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200832 if (buf_free < ep->max_transfer && buf_free > 0) {
833 *buf = 0xff;
834 --buf_free;
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
837}
838
839static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
840 .input = snd_usbmidi_emagic_input,
841 .output = snd_usbmidi_emagic_output,
842 .init_out_endpoint = snd_usbmidi_emagic_init_out,
843 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
844};
845
846
Takashi Iwai86e07d32005-11-17 15:08:02 +0100847static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100849 struct snd_usb_midi* umidi = substream->rmidi->private_data;
850 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int i, j;
852
853 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
854 if (umidi->endpoints[i].out)
855 for (j = 0; j < 0x10; ++j)
856 if (umidi->endpoints[i].out->ports[j].substream == substream) {
857 port = &umidi->endpoints[i].out->ports[j];
858 break;
859 }
860 if (!port) {
861 snd_BUG();
862 return -ENXIO;
863 }
864 substream->runtime->private_data = port;
865 port->state = STATE_UNKNOWN;
866 return 0;
867}
868
Takashi Iwai86e07d32005-11-17 15:08:02 +0100869static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 return 0;
872}
873
Takashi Iwai86e07d32005-11-17 15:08:02 +0100874static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100876 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 port->active = up;
879 if (up) {
880 if (port->ep->umidi->chip->shutdown) {
881 /* gobble up remaining bytes to prevent wait in
882 * snd_rawmidi_drain_output */
883 while (!snd_rawmidi_transmit_empty(substream))
884 snd_rawmidi_transmit_ack(substream, 1);
885 return;
886 }
Takashi Iwai1f041282008-12-18 12:17:55 +0100887 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889}
890
Takashi Iwai86e07d32005-11-17 15:08:02 +0100891static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 return 0;
894}
895
Takashi Iwai86e07d32005-11-17 15:08:02 +0100896static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 return 0;
899}
900
Takashi Iwai86e07d32005-11-17 15:08:02 +0100901static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100903 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (up)
906 set_bit(substream->number, &umidi->input_triggered);
907 else
908 clear_bit(substream->number, &umidi->input_triggered);
909}
910
Takashi Iwai86e07d32005-11-17 15:08:02 +0100911static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 .open = snd_usbmidi_output_open,
913 .close = snd_usbmidi_output_close,
914 .trigger = snd_usbmidi_output_trigger,
915};
916
Takashi Iwai86e07d32005-11-17 15:08:02 +0100917static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 .open = snd_usbmidi_input_open,
919 .close = snd_usbmidi_input_close,
920 .trigger = snd_usbmidi_input_trigger
921};
922
923/*
924 * Frees an input endpoint.
925 * May be called when ep hasn't been initialized completely.
926 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100927static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200929 unsigned int i;
930
931 for (i = 0; i < INPUT_URBS; ++i) {
932 if (ep->urbs[i]) {
933 usb_buffer_free(ep->umidi->chip->dev,
934 ep->urbs[i]->transfer_buffer_length,
935 ep->urbs[i]->transfer_buffer,
936 ep->urbs[i]->transfer_dma);
937 usb_free_urb(ep->urbs[i]);
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 kfree(ep);
941}
942
943/*
944 * Creates an input endpoint.
945 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100946static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
947 struct snd_usb_midi_endpoint_info* ep_info,
948 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100950 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 void* buffer;
952 unsigned int pipe;
953 int length;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200954 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200957 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (!ep)
959 return -ENOMEM;
960 ep->umidi = umidi;
961
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200962 for (i = 0; i < INPUT_URBS; ++i) {
963 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
964 if (!ep->urbs[i]) {
965 snd_usbmidi_in_endpoint_delete(ep);
966 return -ENOMEM;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 if (ep_info->in_interval)
970 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
971 else
972 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
973 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200974 for (i = 0; i < INPUT_URBS; ++i) {
975 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
976 &ep->urbs[i]->transfer_dma);
977 if (!buffer) {
978 snd_usbmidi_in_endpoint_delete(ep);
979 return -ENOMEM;
980 }
981 if (ep_info->in_interval)
982 usb_fill_int_urb(ep->urbs[i], umidi->chip->dev,
983 pipe, buffer, length,
984 snd_usbmidi_in_urb_complete,
985 ep, ep_info->in_interval);
986 else
987 usb_fill_bulk_urb(ep->urbs[i], umidi->chip->dev,
988 pipe, buffer, length,
989 snd_usbmidi_in_urb_complete, ep);
990 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 rep->in = ep;
994 return 0;
995}
996
997static unsigned int snd_usbmidi_count_bits(unsigned int x)
998{
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100999 unsigned int bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Clemens Ladisch62f09c32006-02-27 09:53:03 +01001001 for (bits = 0; x; ++bits)
1002 x &= x - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return bits;
1004}
1005
1006/*
1007 * Frees an output endpoint.
1008 * May be called when ep hasn't been initialized completely.
1009 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001010static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +02001013 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
1014 ep->urb->transfer_buffer,
1015 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 usb_free_urb(ep->urb);
1017 }
1018 kfree(ep);
1019}
1020
1021/*
1022 * Creates an output endpoint, and initializes output ports.
1023 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001024static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1025 struct snd_usb_midi_endpoint_info* ep_info,
1026 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001028 struct snd_usb_midi_out_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 int i;
1030 unsigned int pipe;
1031 void* buffer;
1032
1033 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001034 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (!ep)
1036 return -ENOMEM;
1037 ep->umidi = umidi;
1038
1039 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
1040 if (!ep->urb) {
1041 snd_usbmidi_out_endpoint_delete(ep);
1042 return -ENOMEM;
1043 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001044 if (ep_info->out_interval)
1045 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
1046 else
1047 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
Clemens Ladisch490cbd92007-05-07 09:29:32 +02001048 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
1049 /* FIXME: we need more URBs to get reasonable bandwidth here: */
1050 ep->max_transfer = 4;
1051 else
1052 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
Clemens Ladisch55851f72005-08-15 08:34:16 +02001053 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
1054 GFP_KERNEL, &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (!buffer) {
1056 snd_usbmidi_out_endpoint_delete(ep);
1057 return -ENOMEM;
1058 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001059 if (ep_info->out_interval)
1060 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
1061 ep->max_transfer, snd_usbmidi_out_urb_complete,
1062 ep, ep_info->out_interval);
1063 else
1064 usb_fill_bulk_urb(ep->urb, umidi->chip->dev,
1065 pipe, buffer, ep->max_transfer,
1066 snd_usbmidi_out_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +02001067 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 spin_lock_init(&ep->buffer_lock);
1070 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1071
1072 for (i = 0; i < 0x10; ++i)
1073 if (ep_info->out_cables & (1 << i)) {
1074 ep->ports[i].ep = ep;
1075 ep->ports[i].cable = i << 4;
1076 }
1077
1078 if (umidi->usb_protocol_ops->init_out_endpoint)
1079 umidi->usb_protocol_ops->init_out_endpoint(ep);
1080
1081 rep->out = ep;
1082 return 0;
1083}
1084
1085/*
1086 * Frees everything.
1087 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001088static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 int i;
1091
1092 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001093 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (ep->out)
1095 snd_usbmidi_out_endpoint_delete(ep->out);
1096 if (ep->in)
1097 snd_usbmidi_in_endpoint_delete(ep->in);
1098 }
1099 kfree(umidi);
1100}
1101
1102/*
1103 * Unlinks all URBs (must be done before the usb_device is deleted).
1104 */
Clemens Ladischee733392005-04-25 10:34:13 +02001105void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001107 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001108 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Takashi Iwai86e07d32005-11-17 15:08:02 +01001110 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001111 /*
1112 * an URB's completion handler may start the timer and
1113 * a timer may submit an URB. To reliably break the cycle
1114 * a flag under lock must be used
1115 */
1116 spin_lock_irq(&umidi->disc_lock);
1117 umidi->disconnected = 1;
1118 spin_unlock_irq(&umidi->disc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001120 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001121 if (ep->out)
1122 tasklet_kill(&ep->out->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (ep->out && ep->out->urb) {
1124 usb_kill_urb(ep->out->urb);
1125 if (umidi->usb_protocol_ops->finish_out_endpoint)
1126 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1127 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001128 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001129 for (j = 0; j < INPUT_URBS; ++j)
1130 usb_kill_urb(ep->in->urbs[j]);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001131 /* free endpoints here; later call can result in Oops */
1132 if (ep->out) {
1133 snd_usbmidi_out_endpoint_delete(ep->out);
1134 ep->out = NULL;
1135 }
1136 if (ep->in) {
1137 snd_usbmidi_in_endpoint_delete(ep->in);
1138 ep->in = NULL;
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001141 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Takashi Iwai86e07d32005-11-17 15:08:02 +01001144static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001146 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 snd_usbmidi_free(umidi);
1148}
1149
Takashi Iwai86e07d32005-11-17 15:08:02 +01001150static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int stream, int number)
1152{
1153 struct list_head* list;
1154
1155 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001156 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (substream->number == number)
1158 return substream;
1159 }
1160 return NULL;
1161}
1162
1163/*
1164 * This list specifies names for ports that do not fit into the standard
1165 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1166 * such as internal control or synthesizer ports.
1167 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001168static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001169 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001170 short int port;
1171 short int voices;
1172 const char *name;
1173 unsigned int seq_flags;
1174} snd_usbmidi_port_info[] = {
1175#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1176 { .id = USB_ID(vendor, product), \
1177 .port = num, .voices = voices_, \
1178 .name = name_, .seq_flags = flags }
1179#define EXTERNAL_PORT(vendor, product, num, name) \
1180 PORT_INFO(vendor, product, num, name, 0, \
1181 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1182 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1183 SNDRV_SEQ_PORT_TYPE_PORT)
1184#define CONTROL_PORT(vendor, product, num, name) \
1185 PORT_INFO(vendor, product, num, name, 0, \
1186 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1187 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1188#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1189 PORT_INFO(vendor, product, num, name, voices, \
1190 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1191 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1192 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1193 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1194 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1195 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1196 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1197#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1198 PORT_INFO(vendor, product, num, name, voices, \
1199 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1200 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1201 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1202 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1203 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1204 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1205 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1206 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001208 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001210 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1211 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1212 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1213 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1214 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1215 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001217 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1218 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001220 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1221 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1222 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001224 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1225 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1226 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001228 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1229 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1230 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001232 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001234 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1235 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1236 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1237 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001239 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001241 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1242 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1243 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001245 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1246 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1247 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1248 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001250 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1251 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001253 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1254 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1255 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001257 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1258 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1259 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001261 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1262 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001264 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1265 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001267 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1268 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1269 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001271 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1272 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1273 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001274 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001275 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001277 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1278 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001280 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1281 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001283 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1284 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1285 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286};
1287
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001288static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1289{
1290 int i;
1291
1292 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1293 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1294 snd_usbmidi_port_info[i].port == number)
1295 return &snd_usbmidi_port_info[i];
1296 }
1297 return NULL;
1298}
1299
1300static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1301 struct snd_seq_port_info *seq_port_info)
1302{
1303 struct snd_usb_midi *umidi = rmidi->private_data;
1304 struct port_info *port_info;
1305
1306 /* TODO: read port flags from descriptors */
1307 port_info = find_port_info(umidi, number);
1308 if (port_info) {
1309 seq_port_info->type = port_info->seq_flags;
1310 seq_port_info->midi_voices = port_info->voices;
1311 }
1312}
1313
Takashi Iwai86e07d32005-11-17 15:08:02 +01001314static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001316 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001318 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 const char *name_format;
1320
Takashi Iwai86e07d32005-11-17 15:08:02 +01001321 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (!substream) {
1323 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1324 return;
1325 }
1326
1327 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001328 port_info = find_port_info(umidi, number);
1329 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 snprintf(substream->name, sizeof(substream->name),
1331 name_format, umidi->chip->card->shortname, number + 1);
1332
1333 *rsubstream = substream;
1334}
1335
1336/*
1337 * Creates the endpoints and their ports.
1338 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001339static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1340 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 int i, j, err;
1343 int out_ports = 0, in_ports = 0;
1344
1345 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1346 if (endpoints[i].out_cables) {
1347 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1348 &umidi->endpoints[i]);
1349 if (err < 0)
1350 return err;
1351 }
1352 if (endpoints[i].in_cables) {
1353 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1354 &umidi->endpoints[i]);
1355 if (err < 0)
1356 return err;
1357 }
1358
1359 for (j = 0; j < 0x10; ++j) {
1360 if (endpoints[i].out_cables & (1 << j)) {
1361 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1362 &umidi->endpoints[i].out->ports[j].substream);
1363 ++out_ports;
1364 }
1365 if (endpoints[i].in_cables & (1 << j)) {
1366 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1367 &umidi->endpoints[i].in->ports[j].substream);
1368 ++in_ports;
1369 }
1370 }
1371 }
1372 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1373 out_ports, in_ports);
1374 return 0;
1375}
1376
1377/*
1378 * Returns MIDIStreaming device capabilities.
1379 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001380static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1381 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 struct usb_interface* intf;
1384 struct usb_host_interface *hostif;
1385 struct usb_interface_descriptor* intfd;
1386 struct usb_ms_header_descriptor* ms_header;
1387 struct usb_host_endpoint *hostep;
1388 struct usb_endpoint_descriptor* ep;
1389 struct usb_ms_endpoint_descriptor* ms_ep;
1390 int i, epidx;
1391
1392 intf = umidi->iface;
1393 if (!intf)
1394 return -ENXIO;
1395 hostif = &intf->altsetting[0];
1396 intfd = get_iface_desc(hostif);
1397 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1398 if (hostif->extralen >= 7 &&
1399 ms_header->bLength >= 7 &&
1400 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1401 ms_header->bDescriptorSubtype == HEADER)
1402 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1403 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1404 else
1405 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1406
1407 epidx = 0;
1408 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1409 hostep = &hostif->endpoint[i];
1410 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001411 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 continue;
1413 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1414 if (hostep->extralen < 4 ||
1415 ms_ep->bLength < 4 ||
1416 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1417 ms_ep->bDescriptorSubtype != MS_GENERAL)
1418 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001419 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (endpoints[epidx].out_ep) {
1421 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1422 snd_printk(KERN_WARNING "too many endpoints\n");
1423 break;
1424 }
1425 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001426 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1427 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001429 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1430 /*
1431 * Low speed bulk transfers don't exist, so
1432 * force interrupt transfers for devices like
1433 * ESI MIDI Mate that try to use them anyway.
1434 */
1435 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1437 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1438 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1439 } else {
1440 if (endpoints[epidx].in_ep) {
1441 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1442 snd_printk(KERN_WARNING "too many endpoints\n");
1443 break;
1444 }
1445 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001446 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1447 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001449 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1450 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1452 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1453 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1454 }
1455 }
1456 return 0;
1457}
1458
1459/*
1460 * On Roland devices, use the second alternate setting to be able to use
1461 * the interrupt input endpoint.
1462 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001463static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct usb_interface* intf;
1466 struct usb_host_interface *hostif;
1467 struct usb_interface_descriptor* intfd;
1468
1469 intf = umidi->iface;
1470 if (!intf || intf->num_altsetting != 2)
1471 return;
1472
1473 hostif = &intf->altsetting[1];
1474 intfd = get_iface_desc(hostif);
1475 if (intfd->bNumEndpoints != 2 ||
1476 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1477 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1478 return;
1479
1480 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1481 intfd->bAlternateSetting);
1482 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1483 intfd->bAlternateSetting);
1484}
1485
1486/*
1487 * Try to find any usable endpoints in the interface.
1488 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001489static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1490 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 int max_endpoints)
1492{
1493 struct usb_interface* intf;
1494 struct usb_host_interface *hostif;
1495 struct usb_interface_descriptor* intfd;
1496 struct usb_endpoint_descriptor* epd;
1497 int i, out_eps = 0, in_eps = 0;
1498
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001499 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 snd_usbmidi_switch_roland_altsetting(umidi);
1501
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001502 if (endpoint[0].out_ep || endpoint[0].in_ep)
1503 return 0;
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 intf = umidi->iface;
1506 if (!intf || intf->num_altsetting < 1)
1507 return -ENOENT;
1508 hostif = intf->cur_altsetting;
1509 intfd = get_iface_desc(hostif);
1510
1511 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1512 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001513 if (!usb_endpoint_xfer_bulk(epd) &&
1514 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 continue;
1516 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001517 usb_endpoint_dir_out(epd)) {
1518 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1519 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 endpoint[out_eps].out_interval = epd->bInterval;
1521 ++out_eps;
1522 }
1523 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001524 usb_endpoint_dir_in(epd)) {
1525 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1526 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 endpoint[in_eps].in_interval = epd->bInterval;
1528 ++in_eps;
1529 }
1530 }
1531 return (out_eps || in_eps) ? 0 : -ENOENT;
1532}
1533
1534/*
1535 * Detects the endpoints for one-port-per-endpoint protocols.
1536 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001537static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1538 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 int err, i;
1541
1542 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1543 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1544 if (endpoints[i].out_ep)
1545 endpoints[i].out_cables = 0x0001;
1546 if (endpoints[i].in_ep)
1547 endpoints[i].in_cables = 0x0001;
1548 }
1549 return err;
1550}
1551
1552/*
1553 * Detects the endpoints and ports of Yamaha devices.
1554 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001555static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1556 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 struct usb_interface* intf;
1559 struct usb_host_interface *hostif;
1560 struct usb_interface_descriptor* intfd;
1561 uint8_t* cs_desc;
1562
1563 intf = umidi->iface;
1564 if (!intf)
1565 return -ENOENT;
1566 hostif = intf->altsetting;
1567 intfd = get_iface_desc(hostif);
1568 if (intfd->bNumEndpoints < 1)
1569 return -ENOENT;
1570
1571 /*
1572 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1573 * necessarily with any useful contents. So simply count 'em.
1574 */
1575 for (cs_desc = hostif->extra;
1576 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1577 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001578 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (cs_desc[2] == MIDI_IN_JACK)
1580 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1581 else if (cs_desc[2] == MIDI_OUT_JACK)
1582 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1583 }
1584 }
1585 if (!endpoint->in_cables && !endpoint->out_cables)
1586 return -ENOENT;
1587
1588 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1589}
1590
1591/*
1592 * Creates the endpoints and their ports for Midiman devices.
1593 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001594static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1595 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001597 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 struct usb_interface* intf;
1599 struct usb_host_interface *hostif;
1600 struct usb_interface_descriptor* intfd;
1601 struct usb_endpoint_descriptor* epd;
1602 int cable, err;
1603
1604 intf = umidi->iface;
1605 if (!intf)
1606 return -ENOENT;
1607 hostif = intf->altsetting;
1608 intfd = get_iface_desc(hostif);
1609 /*
1610 * The various MidiSport devices have more or less random endpoint
1611 * numbers, so we have to identify the endpoints by their index in
1612 * the descriptor array, like the driver for that other OS does.
1613 *
1614 * There is one interrupt input endpoint for all input ports, one
1615 * bulk output endpoint for even-numbered ports, and one for odd-
1616 * numbered ports. Both bulk output endpoints have corresponding
1617 * input bulk endpoints (at indices 1 and 3) which aren't used.
1618 */
1619 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1620 snd_printdd(KERN_ERR "not enough endpoints\n");
1621 return -ENOENT;
1622 }
1623
1624 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001625 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1627 return -ENXIO;
1628 }
1629 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001630 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1632 return -ENXIO;
1633 }
1634 if (endpoint->out_cables > 0x0001) {
1635 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001636 if (!usb_endpoint_dir_out(epd) ||
1637 !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1639 return -ENXIO;
1640 }
1641 }
1642
1643 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01001644 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 ep_info.out_cables = endpoint->out_cables & 0x5555;
1646 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1647 if (err < 0)
1648 return err;
1649
1650 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1651 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1652 ep_info.in_cables = endpoint->in_cables;
1653 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1654 if (err < 0)
1655 return err;
1656
1657 if (endpoint->out_cables > 0x0001) {
1658 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1659 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1660 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1661 if (err < 0)
1662 return err;
1663 }
1664
1665 for (cable = 0; cable < 0x10; ++cable) {
1666 if (endpoint->out_cables & (1 << cable))
1667 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1668 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1669 if (endpoint->in_cables & (1 << cable))
1670 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1671 &umidi->endpoints[0].in->ports[cable].substream);
1672 }
1673 return 0;
1674}
1675
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001676static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1677 .get_port_info = snd_usbmidi_get_port_info,
1678};
1679
Takashi Iwai86e07d32005-11-17 15:08:02 +01001680static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int out_ports, int in_ports)
1682{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001683 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 int err;
1685
1686 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1687 umidi->chip->next_midi_device++,
1688 out_ports, in_ports, &rmidi);
1689 if (err < 0)
1690 return err;
1691 strcpy(rmidi->name, umidi->chip->card->shortname);
1692 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1693 SNDRV_RAWMIDI_INFO_INPUT |
1694 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001695 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 rmidi->private_data = umidi;
1697 rmidi->private_free = snd_usbmidi_rawmidi_free;
1698 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1699 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1700
1701 umidi->rmidi = rmidi;
1702 return 0;
1703}
1704
1705/*
1706 * Temporarily stop input.
1707 */
1708void snd_usbmidi_input_stop(struct list_head* p)
1709{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001710 struct snd_usb_midi* umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001711 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Takashi Iwai86e07d32005-11-17 15:08:02 +01001713 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001715 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001717 for (j = 0; j < INPUT_URBS; ++j)
1718 usb_kill_urb(ep->in->urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720}
1721
Takashi Iwai86e07d32005-11-17 15:08:02 +01001722static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001724 unsigned int i;
1725
1726 if (!ep)
1727 return;
1728 for (i = 0; i < INPUT_URBS; ++i) {
1729 struct urb* urb = ep->urbs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 urb->dev = ep->umidi->chip->dev;
1731 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1732 }
1733}
1734
1735/*
1736 * Resume input after a call to snd_usbmidi_input_stop().
1737 */
1738void snd_usbmidi_input_start(struct list_head* p)
1739{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001740 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int i;
1742
Takashi Iwai86e07d32005-11-17 15:08:02 +01001743 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1745 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1746}
1747
1748/*
1749 * Creates and registers everything needed for a MIDI streaming interface.
1750 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001751int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct usb_interface* iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001753 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001755 struct snd_usb_midi* umidi;
1756 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 int out_ports, in_ports;
1758 int i, err;
1759
Takashi Iwai561b2202005-09-09 14:22:34 +02001760 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (!umidi)
1762 return -ENOMEM;
1763 umidi->chip = chip;
1764 umidi->iface = iface;
1765 umidi->quirk = quirk;
1766 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001767 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001768 spin_lock_init(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +02001769 umidi->error_timer.function = snd_usbmidi_error_timer;
1770 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /* detect the endpoint(s) to use */
1773 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001774 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1775 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd05cc10432007-05-07 09:28:53 +02001777 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1778 umidi->usb_protocol_ops =
1779 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001780 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02001781 case QUIRK_MIDI_US122L:
1782 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1783 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02001784 case QUIRK_MIDI_FIXED_ENDPOINT:
1785 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001786 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001787 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1788 break;
1789 case QUIRK_MIDI_YAMAHA:
1790 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1791 break;
1792 case QUIRK_MIDI_MIDIMAN:
1793 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1794 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001795 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001796 err = 0;
1797 break;
1798 case QUIRK_MIDI_NOVATION:
1799 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1800 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1801 break;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001802 case QUIRK_MIDI_FASTLANE:
Clemens Ladischd1bda042005-09-14 08:36:03 +02001803 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001804 /*
1805 * Interface 1 contains isochronous endpoints, but with the same
1806 * numbers as in interface 0. Since it is interface 1 that the
1807 * USB core has most recently seen, these descriptors are now
1808 * associated with the endpoint numbers. This will foul up our
1809 * attempts to submit bulk/interrupt URBs to the endpoints in
1810 * interface 0, so we have to make sure that the USB core looks
1811 * again at interface 0 by calling usb_set_interface() on it.
1812 */
1813 usb_set_interface(umidi->chip->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02001814 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1815 break;
1816 case QUIRK_MIDI_EMAGIC:
1817 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1818 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001819 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001820 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1821 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01001822 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02001823 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001824 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1825 break;
1826 default:
1827 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1828 err = -ENXIO;
1829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
1831 if (err < 0) {
1832 kfree(umidi);
1833 return err;
1834 }
1835
1836 /* create rawmidi device */
1837 out_ports = 0;
1838 in_ports = 0;
1839 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1840 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1841 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1842 }
1843 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1844 if (err < 0) {
1845 kfree(umidi);
1846 return err;
1847 }
1848
1849 /* create endpoint/port structures */
1850 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1851 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1852 else
1853 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1854 if (err < 0) {
1855 snd_usbmidi_free(umidi);
1856 return err;
1857 }
1858
1859 list_add(&umidi->list, &umidi->chip->midi_list);
1860
1861 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1862 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1863 return 0;
1864}
1865
1866EXPORT_SYMBOL(snd_usb_create_midi_interface);
1867EXPORT_SYMBOL(snd_usbmidi_input_stop);
1868EXPORT_SYMBOL(snd_usbmidi_input_start);
1869EXPORT_SYMBOL(snd_usbmidi_disconnect);