blob: 2fb35cc22a3030b687d1623546196cd6e8e2de27 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
67MODULE_DESCRIPTION("USB Audio/MIDI helper module");
68MODULE_LICENSE("Dual BSD/GPL");
69
70
71struct usb_ms_header_descriptor {
72 __u8 bLength;
73 __u8 bDescriptorType;
74 __u8 bDescriptorSubtype;
75 __u8 bcdMSC[2];
76 __le16 wTotalLength;
77} __attribute__ ((packed));
78
79struct usb_ms_endpoint_descriptor {
80 __u8 bLength;
81 __u8 bDescriptorType;
82 __u8 bDescriptorSubtype;
83 __u8 bNumEmbMIDIJack;
84 __u8 baAssocJackID[0];
85} __attribute__ ((packed));
86
Takashi Iwai86e07d32005-11-17 15:08:02 +010087struct snd_usb_midi_in_endpoint;
88struct snd_usb_midi_out_endpoint;
89struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +010092 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
93 void (*output)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Takashi Iwai86e07d32005-11-17 15:08:02 +010095 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
96 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
99struct snd_usb_midi {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100100 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100102 const struct snd_usb_audio_quirk *quirk;
103 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct usb_protocol_ops* usb_protocol_ops;
105 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200106 struct timer_list error_timer;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100107 spinlock_t disc_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100110 struct snd_usb_midi_out_endpoint *out;
111 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 } endpoints[MIDI_MAX_ENDPOINTS];
113 unsigned long input_triggered;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100114 unsigned char disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100118 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct urb* urb;
120 int urb_active;
121 int max_transfer; /* size of urb buffer */
122 struct tasklet_struct tasklet;
123
124 spinlock_t buffer_lock;
125
126 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100127 struct snd_usb_midi_out_endpoint* ep;
128 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int active;
130 uint8_t cable; /* cable number << 4 */
131 uint8_t state;
132#define STATE_UNKNOWN 0
133#define STATE_1PARAM 1
134#define STATE_2PARAM_1 2
135#define STATE_2PARAM_2 3
136#define STATE_SYSEX_0 4
137#define STATE_SYSEX_1 5
138#define STATE_SYSEX_2 6
139 uint8_t data[2];
140 } ports[0x10];
141 int current_port;
142};
143
144struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100145 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct urb* urb;
147 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100148 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200149 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200151 u8 seen_f5;
152 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int current_port;
154};
155
Takashi Iwai86e07d32005-11-17 15:08:02 +0100156static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158static const uint8_t snd_usbmidi_cin_length[] = {
159 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
160};
161
162/*
163 * Submits the URB, with error handling.
164 */
Al Viro55016f12005-10-21 03:21:58 -0400165static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int err = usb_submit_urb(urb, flags);
168 if (err < 0 && err != -ENODEV)
169 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
170 return err;
171}
172
173/*
174 * Error handling for URB completion functions.
175 */
176static int snd_usbmidi_urb_error(int status)
177{
Clemens Ladischc8846972005-08-02 15:26:52 +0200178 switch (status) {
179 /* manually unlinked, or device gone */
180 case -ENOENT:
181 case -ECONNRESET:
182 case -ESHUTDOWN:
183 case -ENODEV:
184 return -ENODEV;
185 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700186 case -EPROTO:
187 case -ETIME:
188 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200189 return -EIO;
190 default:
191 snd_printk(KERN_ERR "urb status %d\n", status);
192 return 0; /* continue */
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/*
197 * Receives a chunk of MIDI data.
198 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100199static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 uint8_t* data, int length)
201{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100202 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (!port->substream) {
205 snd_printd("unexpected port %d!\n", portidx);
206 return;
207 }
208 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
209 return;
210 snd_rawmidi_receive(port->substream, data, length);
211}
212
213#ifdef DUMP_PACKETS
214static void dump_urb(const char *type, const u8 *data, int length)
215{
216 snd_printk(KERN_DEBUG "%s packet: [", type);
217 for (; length > 0; ++data, --length)
218 printk(" %02x", *data);
219 printk(" ]\n");
220}
221#else
222#define dump_urb(type, data, length) /* nothing */
223#endif
224
225/*
226 * Processes the data read from the device.
227 */
David Howells7d12e782006-10-05 14:55:46 +0100228static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100230 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (urb->status == 0) {
233 dump_urb("received", urb->transfer_buffer, urb->actual_length);
234 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
235 urb->actual_length);
236 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200237 int err = snd_usbmidi_urb_error(urb->status);
238 if (err < 0) {
239 if (err != -ENODEV) {
240 ep->error_resubmit = 1;
241 mod_timer(&ep->umidi->error_timer,
242 jiffies + ERROR_DELAY_JIFFIES);
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200248 urb->dev = ep->umidi->chip->dev;
249 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
David Howells7d12e782006-10-05 14:55:46 +0100252static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100254 struct snd_usb_midi_out_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 spin_lock(&ep->buffer_lock);
257 ep->urb_active = 0;
258 spin_unlock(&ep->buffer_lock);
259 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200260 int err = snd_usbmidi_urb_error(urb->status);
261 if (err < 0) {
262 if (err != -ENODEV)
263 mod_timer(&ep->umidi->error_timer,
264 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 snd_usbmidi_do_output(ep);
269}
270
271/*
272 * This is called when some data should be transferred to the device
273 * (from one or more substreams).
274 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100275static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct urb* urb = ep->urb;
278 unsigned long flags;
279
280 spin_lock_irqsave(&ep->buffer_lock, flags);
281 if (ep->urb_active || ep->umidi->chip->shutdown) {
282 spin_unlock_irqrestore(&ep->buffer_lock, flags);
283 return;
284 }
285
286 urb->transfer_buffer_length = 0;
287 ep->umidi->usb_protocol_ops->output(ep);
288
289 if (urb->transfer_buffer_length > 0) {
290 dump_urb("sending", urb->transfer_buffer,
291 urb->transfer_buffer_length);
292 urb->dev = ep->umidi->chip->dev;
293 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
294 }
295 spin_unlock_irqrestore(&ep->buffer_lock, flags);
296}
297
298static void snd_usbmidi_out_tasklet(unsigned long data)
299{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100300 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 snd_usbmidi_do_output(ep);
303}
304
Clemens Ladischc8846972005-08-02 15:26:52 +0200305/* called after transfers had been interrupted due to some USB error */
306static void snd_usbmidi_error_timer(unsigned long data)
307{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100308 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladischc8846972005-08-02 15:26:52 +0200309 int i;
310
Takashi Iwaic0792e02008-02-22 18:34:44 +0100311 spin_lock(&umidi->disc_lock);
312 if (umidi->disconnected) {
313 spin_unlock(&umidi->disc_lock);
314 return;
315 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200316 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100317 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200318 if (in && in->error_resubmit) {
319 in->error_resubmit = 0;
320 in->urb->dev = umidi->chip->dev;
321 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
322 }
323 if (umidi->endpoints[i].out)
324 snd_usbmidi_do_output(umidi->endpoints[i].out);
325 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100326 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100330static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 const void *data, int len)
332{
333 int err;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700334 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (!buf)
336 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 dump_urb("sending", buf, len);
338 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len,
339 NULL, 250);
340 kfree(buf);
341 return err;
342}
343
344/*
345 * Standard USB MIDI protocol: see the spec.
346 * Midiman protocol: like the standard protocol, but the control byte is the
347 * fourth byte in each packet, and uses length instead of CIN.
348 */
349
Takashi Iwai86e07d32005-11-17 15:08:02 +0100350static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 uint8_t* buffer, int buffer_length)
352{
353 int i;
354
355 for (i = 0; i + 3 < buffer_length; i += 4)
356 if (buffer[i] != 0) {
357 int cable = buffer[i] >> 4;
358 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
359 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
360 }
361}
362
Takashi Iwai86e07d32005-11-17 15:08:02 +0100363static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 uint8_t* buffer, int buffer_length)
365{
366 int i;
367
368 for (i = 0; i + 3 < buffer_length; i += 4)
369 if (buffer[i + 3] != 0) {
370 int port = buffer[i + 3] >> 4;
371 int length = buffer[i + 3] & 3;
372 snd_usbmidi_input_data(ep, port, &buffer[i], length);
373 }
374}
375
376/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200377 * Buggy M-Audio device: running status on input results in a packet that has
378 * the data bytes but not the status byte and that is marked with CIN 4.
379 */
380static void snd_usbmidi_maudio_broken_running_status_input(
381 struct snd_usb_midi_in_endpoint* ep,
382 uint8_t* buffer, int buffer_length)
383{
384 int i;
385
386 for (i = 0; i + 3 < buffer_length; i += 4)
387 if (buffer[i] != 0) {
388 int cable = buffer[i] >> 4;
389 u8 cin = buffer[i] & 0x0f;
390 struct usbmidi_in_port *port = &ep->ports[cable];
391 int length;
392
393 length = snd_usbmidi_cin_length[cin];
394 if (cin == 0xf && buffer[i + 1] >= 0xf8)
395 ; /* realtime msg: no running status change */
396 else if (cin >= 0x8 && cin <= 0xe)
397 /* channel msg */
398 port->running_status_length = length - 1;
399 else if (cin == 0x4 &&
400 port->running_status_length != 0 &&
401 buffer[i + 1] < 0x80)
402 /* CIN 4 that is not a SysEx */
403 length = port->running_status_length;
404 else
405 /*
406 * All other msgs cannot begin running status.
407 * (A channel msg sent as two or three CIN 0xF
408 * packets could in theory, but this device
409 * doesn't use this format.)
410 */
411 port->running_status_length = 0;
412 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
413 }
414}
415
416/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200417 * CME protocol: like the standard protocol, but SysEx commands are sent as a
418 * single USB packet preceded by a 0x0F byte.
419 */
420static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
421 uint8_t *buffer, int buffer_length)
422{
423 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
424 snd_usbmidi_standard_input(ep, buffer, buffer_length);
425 else
426 snd_usbmidi_input_data(ep, buffer[0] >> 4,
427 &buffer[1], buffer_length - 1);
428}
429
430/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * Adds one USB MIDI packet to the output buffer.
432 */
433static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
434 uint8_t p1, uint8_t p2, uint8_t p3)
435{
436
437 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
438 buf[0] = p0;
439 buf[1] = p1;
440 buf[2] = p2;
441 buf[3] = p3;
442 urb->transfer_buffer_length += 4;
443}
444
445/*
446 * Adds one Midiman packet to the output buffer.
447 */
448static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
449 uint8_t p1, uint8_t p2, uint8_t p3)
450{
451
452 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
453 buf[0] = p1;
454 buf[1] = p2;
455 buf[2] = p3;
456 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
457 urb->transfer_buffer_length += 4;
458}
459
460/*
461 * Converts MIDI commands to USB MIDI packets.
462 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100463static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 uint8_t b, struct urb* urb)
465{
466 uint8_t p0 = port->cable;
467 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
468 port->ep->umidi->usb_protocol_ops->output_packet;
469
470 if (b >= 0xf8) {
471 output_packet(urb, p0 | 0x0f, b, 0, 0);
472 } else if (b >= 0xf0) {
473 switch (b) {
474 case 0xf0:
475 port->data[0] = b;
476 port->state = STATE_SYSEX_1;
477 break;
478 case 0xf1:
479 case 0xf3:
480 port->data[0] = b;
481 port->state = STATE_1PARAM;
482 break;
483 case 0xf2:
484 port->data[0] = b;
485 port->state = STATE_2PARAM_1;
486 break;
487 case 0xf4:
488 case 0xf5:
489 port->state = STATE_UNKNOWN;
490 break;
491 case 0xf6:
492 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
493 port->state = STATE_UNKNOWN;
494 break;
495 case 0xf7:
496 switch (port->state) {
497 case STATE_SYSEX_0:
498 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
499 break;
500 case STATE_SYSEX_1:
501 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
502 break;
503 case STATE_SYSEX_2:
504 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
505 break;
506 }
507 port->state = STATE_UNKNOWN;
508 break;
509 }
510 } else if (b >= 0x80) {
511 port->data[0] = b;
512 if (b >= 0xc0 && b <= 0xdf)
513 port->state = STATE_1PARAM;
514 else
515 port->state = STATE_2PARAM_1;
516 } else { /* b < 0x80 */
517 switch (port->state) {
518 case STATE_1PARAM:
519 if (port->data[0] < 0xf0) {
520 p0 |= port->data[0] >> 4;
521 } else {
522 p0 |= 0x02;
523 port->state = STATE_UNKNOWN;
524 }
525 output_packet(urb, p0, port->data[0], b, 0);
526 break;
527 case STATE_2PARAM_1:
528 port->data[1] = b;
529 port->state = STATE_2PARAM_2;
530 break;
531 case STATE_2PARAM_2:
532 if (port->data[0] < 0xf0) {
533 p0 |= port->data[0] >> 4;
534 port->state = STATE_2PARAM_1;
535 } else {
536 p0 |= 0x03;
537 port->state = STATE_UNKNOWN;
538 }
539 output_packet(urb, p0, port->data[0], port->data[1], b);
540 break;
541 case STATE_SYSEX_0:
542 port->data[0] = b;
543 port->state = STATE_SYSEX_1;
544 break;
545 case STATE_SYSEX_1:
546 port->data[1] = b;
547 port->state = STATE_SYSEX_2;
548 break;
549 case STATE_SYSEX_2:
550 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
551 port->state = STATE_SYSEX_0;
552 break;
553 }
554 }
555}
556
Takashi Iwai86e07d32005-11-17 15:08:02 +0100557static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct urb* urb = ep->urb;
560 int p;
561
562 /* FIXME: lower-numbered ports can starve higher-numbered ports */
563 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100564 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!port->active)
566 continue;
567 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
568 uint8_t b;
569 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
570 port->active = 0;
571 break;
572 }
573 snd_usbmidi_transmit_byte(port, b, urb);
574 }
575 }
576}
577
578static struct usb_protocol_ops snd_usbmidi_standard_ops = {
579 .input = snd_usbmidi_standard_input,
580 .output = snd_usbmidi_standard_output,
581 .output_packet = snd_usbmidi_output_standard_packet,
582};
583
584static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
585 .input = snd_usbmidi_midiman_input,
586 .output = snd_usbmidi_standard_output,
587 .output_packet = snd_usbmidi_output_midiman_packet,
588};
589
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200590static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
591 .input = snd_usbmidi_maudio_broken_running_status_input,
592 .output = snd_usbmidi_standard_output,
593 .output_packet = snd_usbmidi_output_standard_packet,
594};
595
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200596static struct usb_protocol_ops snd_usbmidi_cme_ops = {
597 .input = snd_usbmidi_cme_input,
598 .output = snd_usbmidi_standard_output,
599 .output_packet = snd_usbmidi_output_standard_packet,
600};
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*
603 * Novation USB MIDI protocol: number of data bytes is in the first byte
604 * (when receiving) (+1!) or in the second byte (when sending); data begins
605 * at the third byte.
606 */
607
Takashi Iwai86e07d32005-11-17 15:08:02 +0100608static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 uint8_t* buffer, int buffer_length)
610{
611 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
612 return;
613 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
614}
615
Takashi Iwai86e07d32005-11-17 15:08:02 +0100616static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 uint8_t* transfer_buffer;
619 int count;
620
621 if (!ep->ports[0].active)
622 return;
623 transfer_buffer = ep->urb->transfer_buffer;
624 count = snd_rawmidi_transmit(ep->ports[0].substream,
625 &transfer_buffer[2],
626 ep->max_transfer - 2);
627 if (count < 1) {
628 ep->ports[0].active = 0;
629 return;
630 }
631 transfer_buffer[0] = 0;
632 transfer_buffer[1] = count;
633 ep->urb->transfer_buffer_length = 2 + count;
634}
635
636static struct usb_protocol_ops snd_usbmidi_novation_ops = {
637 .input = snd_usbmidi_novation_input,
638 .output = snd_usbmidi_novation_output,
639};
640
641/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200642 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
644
Takashi Iwai86e07d32005-11-17 15:08:02 +0100645static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200646 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
649}
650
Takashi Iwai86e07d32005-11-17 15:08:02 +0100651static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 int count;
654
655 if (!ep->ports[0].active)
656 return;
657 count = snd_rawmidi_transmit(ep->ports[0].substream,
658 ep->urb->transfer_buffer,
659 ep->max_transfer);
660 if (count < 1) {
661 ep->ports[0].active = 0;
662 return;
663 }
664 ep->urb->transfer_buffer_length = count;
665}
666
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200667static struct usb_protocol_ops snd_usbmidi_raw_ops = {
668 .input = snd_usbmidi_raw_input,
669 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670};
671
Karsten Wiese030a07e2008-07-30 15:13:29 +0200672static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
673 uint8_t *buffer, int buffer_length)
674{
675 if (buffer_length != 9)
676 return;
677 buffer_length = 8;
678 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
679 buffer_length--;
680 if (buffer_length)
681 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
682}
683
684static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep)
685{
686 int count;
687
688 if (!ep->ports[0].active)
689 return;
690 count = ep->urb->dev->speed == USB_SPEED_HIGH ? 1 : 2;
691 count = snd_rawmidi_transmit(ep->ports[0].substream,
692 ep->urb->transfer_buffer,
693 count);
694 if (count < 1) {
695 ep->ports[0].active = 0;
696 return;
697 }
698
699 memset(ep->urb->transfer_buffer + count, 0xFD, 9 - count);
700 ep->urb->transfer_buffer_length = count;
701}
702
703static struct usb_protocol_ops snd_usbmidi_122l_ops = {
704 .input = snd_usbmidi_us122l_input,
705 .output = snd_usbmidi_us122l_output,
706};
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
709 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
710 */
711
Takashi Iwai86e07d32005-11-17 15:08:02 +0100712static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 static const u8 init_data[] = {
715 /* initialization magic: "get version" */
716 0xf0,
717 0x00, 0x20, 0x31, /* Emagic */
718 0x64, /* Unitor8 */
719 0x0b, /* version number request */
720 0x00, /* command version */
721 0x00, /* EEPROM, box 0 */
722 0xf7
723 };
724 send_bulk_static_data(ep, init_data, sizeof(init_data));
725 /* while we're at it, pour on more magic */
726 send_bulk_static_data(ep, init_data, sizeof(init_data));
727}
728
Takashi Iwai86e07d32005-11-17 15:08:02 +0100729static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 static const u8 finish_data[] = {
732 /* switch to patch mode with last preset */
733 0xf0,
734 0x00, 0x20, 0x31, /* Emagic */
735 0x64, /* Unitor8 */
736 0x10, /* patch switch command */
737 0x00, /* command version */
738 0x7f, /* to all boxes */
739 0x40, /* last preset in EEPROM */
740 0xf7
741 };
742 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
743}
744
Takashi Iwai86e07d32005-11-17 15:08:02 +0100745static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 uint8_t* buffer, int buffer_length)
747{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200748 int i;
749
750 /* FF indicates end of valid data */
751 for (i = 0; i < buffer_length; ++i)
752 if (buffer[i] == 0xff) {
753 buffer_length = i;
754 break;
755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /* handle F5 at end of last buffer */
758 if (ep->seen_f5)
759 goto switch_port;
760
761 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /* determine size of data until next F5 */
763 for (i = 0; i < buffer_length; ++i)
764 if (buffer[i] == 0xf5)
765 break;
766 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
767 buffer += i;
768 buffer_length -= i;
769
770 if (buffer_length <= 0)
771 break;
772 /* assert(buffer[0] == 0xf5); */
773 ep->seen_f5 = 1;
774 ++buffer;
775 --buffer_length;
776
777 switch_port:
778 if (buffer_length <= 0)
779 break;
780 if (buffer[0] < 0x80) {
781 ep->current_port = (buffer[0] - 1) & 15;
782 ++buffer;
783 --buffer_length;
784 }
785 ep->seen_f5 = 0;
786 }
787}
788
Takashi Iwai86e07d32005-11-17 15:08:02 +0100789static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 int port0 = ep->current_port;
792 uint8_t* buf = ep->urb->transfer_buffer;
793 int buf_free = ep->max_transfer;
794 int length, i;
795
796 for (i = 0; i < 0x10; ++i) {
797 /* round-robin, starting at the last current port */
798 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100799 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (!port->active)
802 continue;
803 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
804 port->active = 0;
805 continue;
806 }
807
808 if (portnum != ep->current_port) {
809 if (buf_free < 2)
810 break;
811 ep->current_port = portnum;
812 buf[0] = 0xf5;
813 buf[1] = (portnum + 1) & 15;
814 buf += 2;
815 buf_free -= 2;
816 }
817
818 if (buf_free < 1)
819 break;
820 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
821 if (length > 0) {
822 buf += length;
823 buf_free -= length;
824 if (buf_free < 1)
825 break;
826 }
827 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200828 if (buf_free < ep->max_transfer && buf_free > 0) {
829 *buf = 0xff;
830 --buf_free;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
833}
834
835static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
836 .input = snd_usbmidi_emagic_input,
837 .output = snd_usbmidi_emagic_output,
838 .init_out_endpoint = snd_usbmidi_emagic_init_out,
839 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
840};
841
842
Takashi Iwai86e07d32005-11-17 15:08:02 +0100843static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100845 struct snd_usb_midi* umidi = substream->rmidi->private_data;
846 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int i, j;
848
849 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
850 if (umidi->endpoints[i].out)
851 for (j = 0; j < 0x10; ++j)
852 if (umidi->endpoints[i].out->ports[j].substream == substream) {
853 port = &umidi->endpoints[i].out->ports[j];
854 break;
855 }
856 if (!port) {
857 snd_BUG();
858 return -ENXIO;
859 }
860 substream->runtime->private_data = port;
861 port->state = STATE_UNKNOWN;
862 return 0;
863}
864
Takashi Iwai86e07d32005-11-17 15:08:02 +0100865static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 return 0;
868}
869
Takashi Iwai86e07d32005-11-17 15:08:02 +0100870static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100872 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 port->active = up;
875 if (up) {
876 if (port->ep->umidi->chip->shutdown) {
877 /* gobble up remaining bytes to prevent wait in
878 * snd_rawmidi_drain_output */
879 while (!snd_rawmidi_transmit_empty(substream))
880 snd_rawmidi_transmit_ack(substream, 1);
881 return;
882 }
Takashi Iwai1f041282008-12-18 12:17:55 +0100883 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885}
886
Takashi Iwai86e07d32005-11-17 15:08:02 +0100887static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 return 0;
890}
891
Takashi Iwai86e07d32005-11-17 15:08:02 +0100892static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 return 0;
895}
896
Takashi Iwai86e07d32005-11-17 15:08:02 +0100897static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100899 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (up)
902 set_bit(substream->number, &umidi->input_triggered);
903 else
904 clear_bit(substream->number, &umidi->input_triggered);
905}
906
Takashi Iwai86e07d32005-11-17 15:08:02 +0100907static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 .open = snd_usbmidi_output_open,
909 .close = snd_usbmidi_output_close,
910 .trigger = snd_usbmidi_output_trigger,
911};
912
Takashi Iwai86e07d32005-11-17 15:08:02 +0100913static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 .open = snd_usbmidi_input_open,
915 .close = snd_usbmidi_input_close,
916 .trigger = snd_usbmidi_input_trigger
917};
918
919/*
920 * Frees an input endpoint.
921 * May be called when ep hasn't been initialized completely.
922 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100923static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200926 usb_buffer_free(ep->umidi->chip->dev,
927 ep->urb->transfer_buffer_length,
928 ep->urb->transfer_buffer,
929 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 usb_free_urb(ep->urb);
931 }
932 kfree(ep);
933}
934
935/*
936 * Creates an input endpoint.
937 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100938static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
939 struct snd_usb_midi_endpoint_info* ep_info,
940 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100942 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 void* buffer;
944 unsigned int pipe;
945 int length;
946
947 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200948 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!ep)
950 return -ENOMEM;
951 ep->umidi = umidi;
952
953 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
954 if (!ep->urb) {
955 snd_usbmidi_in_endpoint_delete(ep);
956 return -ENOMEM;
957 }
958 if (ep_info->in_interval)
959 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
960 else
961 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
962 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200963 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
964 &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (!buffer) {
966 snd_usbmidi_in_endpoint_delete(ep);
967 return -ENOMEM;
968 }
969 if (ep_info->in_interval)
Clemens Ladisch3527a002005-09-26 10:03:09 +0200970 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
971 length, snd_usbmidi_in_urb_complete, ep,
972 ep_info->in_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 else
Clemens Ladisch3527a002005-09-26 10:03:09 +0200974 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
975 length, snd_usbmidi_in_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200976 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 rep->in = ep;
979 return 0;
980}
981
982static unsigned int snd_usbmidi_count_bits(unsigned int x)
983{
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100984 unsigned int bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100986 for (bits = 0; x; ++bits)
987 x &= x - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return bits;
989}
990
991/*
992 * Frees an output endpoint.
993 * May be called when ep hasn't been initialized completely.
994 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100995static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200998 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
999 ep->urb->transfer_buffer,
1000 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 usb_free_urb(ep->urb);
1002 }
1003 kfree(ep);
1004}
1005
1006/*
1007 * Creates an output endpoint, and initializes output ports.
1008 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001009static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1010 struct snd_usb_midi_endpoint_info* ep_info,
1011 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001013 struct snd_usb_midi_out_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int i;
1015 unsigned int pipe;
1016 void* buffer;
1017
1018 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001019 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (!ep)
1021 return -ENOMEM;
1022 ep->umidi = umidi;
1023
1024 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
1025 if (!ep->urb) {
1026 snd_usbmidi_out_endpoint_delete(ep);
1027 return -ENOMEM;
1028 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001029 if (ep_info->out_interval)
1030 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
1031 else
1032 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
Clemens Ladisch490cbd92007-05-07 09:29:32 +02001033 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
1034 /* FIXME: we need more URBs to get reasonable bandwidth here: */
1035 ep->max_transfer = 4;
1036 else
1037 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
Clemens Ladisch55851f72005-08-15 08:34:16 +02001038 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
1039 GFP_KERNEL, &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!buffer) {
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 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
1046 ep->max_transfer, snd_usbmidi_out_urb_complete,
1047 ep, ep_info->out_interval);
1048 else
1049 usb_fill_bulk_urb(ep->urb, umidi->chip->dev,
1050 pipe, buffer, ep->max_transfer,
1051 snd_usbmidi_out_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +02001052 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 spin_lock_init(&ep->buffer_lock);
1055 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1056
1057 for (i = 0; i < 0x10; ++i)
1058 if (ep_info->out_cables & (1 << i)) {
1059 ep->ports[i].ep = ep;
1060 ep->ports[i].cable = i << 4;
1061 }
1062
1063 if (umidi->usb_protocol_ops->init_out_endpoint)
1064 umidi->usb_protocol_ops->init_out_endpoint(ep);
1065
1066 rep->out = ep;
1067 return 0;
1068}
1069
1070/*
1071 * Frees everything.
1072 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001073static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 int i;
1076
1077 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001078 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (ep->out)
1080 snd_usbmidi_out_endpoint_delete(ep->out);
1081 if (ep->in)
1082 snd_usbmidi_in_endpoint_delete(ep->in);
1083 }
1084 kfree(umidi);
1085}
1086
1087/*
1088 * Unlinks all URBs (must be done before the usb_device is deleted).
1089 */
Clemens Ladischee733392005-04-25 10:34:13 +02001090void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001092 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int i;
1094
Takashi Iwai86e07d32005-11-17 15:08:02 +01001095 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001096 /*
1097 * an URB's completion handler may start the timer and
1098 * a timer may submit an URB. To reliably break the cycle
1099 * a flag under lock must be used
1100 */
1101 spin_lock_irq(&umidi->disc_lock);
1102 umidi->disconnected = 1;
1103 spin_unlock_irq(&umidi->disc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001105 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001106 if (ep->out)
1107 tasklet_kill(&ep->out->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (ep->out && ep->out->urb) {
1109 usb_kill_urb(ep->out->urb);
1110 if (umidi->usb_protocol_ops->finish_out_endpoint)
1111 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1112 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001113 if (ep->in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 usb_kill_urb(ep->in->urb);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001115 /* free endpoints here; later call can result in Oops */
1116 if (ep->out) {
1117 snd_usbmidi_out_endpoint_delete(ep->out);
1118 ep->out = NULL;
1119 }
1120 if (ep->in) {
1121 snd_usbmidi_in_endpoint_delete(ep->in);
1122 ep->in = NULL;
1123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001125 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Takashi Iwai86e07d32005-11-17 15:08:02 +01001128static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001130 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 snd_usbmidi_free(umidi);
1132}
1133
Takashi Iwai86e07d32005-11-17 15:08:02 +01001134static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 int stream, int number)
1136{
1137 struct list_head* list;
1138
1139 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001140 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (substream->number == number)
1142 return substream;
1143 }
1144 return NULL;
1145}
1146
1147/*
1148 * This list specifies names for ports that do not fit into the standard
1149 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1150 * such as internal control or synthesizer ports.
1151 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001152static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001153 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001154 short int port;
1155 short int voices;
1156 const char *name;
1157 unsigned int seq_flags;
1158} snd_usbmidi_port_info[] = {
1159#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1160 { .id = USB_ID(vendor, product), \
1161 .port = num, .voices = voices_, \
1162 .name = name_, .seq_flags = flags }
1163#define EXTERNAL_PORT(vendor, product, num, name) \
1164 PORT_INFO(vendor, product, num, name, 0, \
1165 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1166 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1167 SNDRV_SEQ_PORT_TYPE_PORT)
1168#define CONTROL_PORT(vendor, product, num, name) \
1169 PORT_INFO(vendor, product, num, name, 0, \
1170 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1171 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1172#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1173 PORT_INFO(vendor, product, num, name, voices, \
1174 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1175 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1176 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1177 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1178 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1179 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1180 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1181#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1182 PORT_INFO(vendor, product, num, name, voices, \
1183 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1184 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1185 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1186 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1187 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1188 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1189 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1190 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001192 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001194 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1195 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1196 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1197 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1198 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1199 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001201 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1202 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001204 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1205 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1206 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001208 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1209 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1210 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001212 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1213 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1214 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001216 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001218 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1219 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1220 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1221 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001223 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001225 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1226 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1227 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001229 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1230 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1231 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1232 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001234 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1235 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001237 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1238 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1239 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001241 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1242 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1243 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001245 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1246 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001248 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1249 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001251 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1252 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1253 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001255 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1256 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1257 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001258 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001259 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001261 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1262 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001264 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1265 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001267 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1268 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1269 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270};
1271
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001272static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1273{
1274 int i;
1275
1276 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1277 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1278 snd_usbmidi_port_info[i].port == number)
1279 return &snd_usbmidi_port_info[i];
1280 }
1281 return NULL;
1282}
1283
1284static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1285 struct snd_seq_port_info *seq_port_info)
1286{
1287 struct snd_usb_midi *umidi = rmidi->private_data;
1288 struct port_info *port_info;
1289
1290 /* TODO: read port flags from descriptors */
1291 port_info = find_port_info(umidi, number);
1292 if (port_info) {
1293 seq_port_info->type = port_info->seq_flags;
1294 seq_port_info->midi_voices = port_info->voices;
1295 }
1296}
1297
Takashi Iwai86e07d32005-11-17 15:08:02 +01001298static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001300 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001302 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 const char *name_format;
1304
Takashi Iwai86e07d32005-11-17 15:08:02 +01001305 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (!substream) {
1307 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1308 return;
1309 }
1310
1311 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001312 port_info = find_port_info(umidi, number);
1313 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 snprintf(substream->name, sizeof(substream->name),
1315 name_format, umidi->chip->card->shortname, number + 1);
1316
1317 *rsubstream = substream;
1318}
1319
1320/*
1321 * Creates the endpoints and their ports.
1322 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001323static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1324 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 int i, j, err;
1327 int out_ports = 0, in_ports = 0;
1328
1329 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1330 if (endpoints[i].out_cables) {
1331 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1332 &umidi->endpoints[i]);
1333 if (err < 0)
1334 return err;
1335 }
1336 if (endpoints[i].in_cables) {
1337 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1338 &umidi->endpoints[i]);
1339 if (err < 0)
1340 return err;
1341 }
1342
1343 for (j = 0; j < 0x10; ++j) {
1344 if (endpoints[i].out_cables & (1 << j)) {
1345 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1346 &umidi->endpoints[i].out->ports[j].substream);
1347 ++out_ports;
1348 }
1349 if (endpoints[i].in_cables & (1 << j)) {
1350 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1351 &umidi->endpoints[i].in->ports[j].substream);
1352 ++in_ports;
1353 }
1354 }
1355 }
1356 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1357 out_ports, in_ports);
1358 return 0;
1359}
1360
1361/*
1362 * Returns MIDIStreaming device capabilities.
1363 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001364static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1365 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 struct usb_interface* intf;
1368 struct usb_host_interface *hostif;
1369 struct usb_interface_descriptor* intfd;
1370 struct usb_ms_header_descriptor* ms_header;
1371 struct usb_host_endpoint *hostep;
1372 struct usb_endpoint_descriptor* ep;
1373 struct usb_ms_endpoint_descriptor* ms_ep;
1374 int i, epidx;
1375
1376 intf = umidi->iface;
1377 if (!intf)
1378 return -ENXIO;
1379 hostif = &intf->altsetting[0];
1380 intfd = get_iface_desc(hostif);
1381 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1382 if (hostif->extralen >= 7 &&
1383 ms_header->bLength >= 7 &&
1384 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1385 ms_header->bDescriptorSubtype == HEADER)
1386 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1387 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1388 else
1389 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1390
1391 epidx = 0;
1392 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1393 hostep = &hostif->endpoint[i];
1394 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001395 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 continue;
1397 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1398 if (hostep->extralen < 4 ||
1399 ms_ep->bLength < 4 ||
1400 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1401 ms_ep->bDescriptorSubtype != MS_GENERAL)
1402 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001403 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (endpoints[epidx].out_ep) {
1405 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1406 snd_printk(KERN_WARNING "too many endpoints\n");
1407 break;
1408 }
1409 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001410 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1411 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001413 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1414 /*
1415 * Low speed bulk transfers don't exist, so
1416 * force interrupt transfers for devices like
1417 * ESI MIDI Mate that try to use them anyway.
1418 */
1419 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1421 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1422 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1423 } else {
1424 if (endpoints[epidx].in_ep) {
1425 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1426 snd_printk(KERN_WARNING "too many endpoints\n");
1427 break;
1428 }
1429 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001430 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1431 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001433 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1434 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1436 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1437 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1438 }
1439 }
1440 return 0;
1441}
1442
1443/*
1444 * On Roland devices, use the second alternate setting to be able to use
1445 * the interrupt input endpoint.
1446 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001447static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 struct usb_interface* intf;
1450 struct usb_host_interface *hostif;
1451 struct usb_interface_descriptor* intfd;
1452
1453 intf = umidi->iface;
1454 if (!intf || intf->num_altsetting != 2)
1455 return;
1456
1457 hostif = &intf->altsetting[1];
1458 intfd = get_iface_desc(hostif);
1459 if (intfd->bNumEndpoints != 2 ||
1460 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1461 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1462 return;
1463
1464 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1465 intfd->bAlternateSetting);
1466 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1467 intfd->bAlternateSetting);
1468}
1469
1470/*
1471 * Try to find any usable endpoints in the interface.
1472 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001473static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1474 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 int max_endpoints)
1476{
1477 struct usb_interface* intf;
1478 struct usb_host_interface *hostif;
1479 struct usb_interface_descriptor* intfd;
1480 struct usb_endpoint_descriptor* epd;
1481 int i, out_eps = 0, in_eps = 0;
1482
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001483 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 snd_usbmidi_switch_roland_altsetting(umidi);
1485
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001486 if (endpoint[0].out_ep || endpoint[0].in_ep)
1487 return 0;
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 intf = umidi->iface;
1490 if (!intf || intf->num_altsetting < 1)
1491 return -ENOENT;
1492 hostif = intf->cur_altsetting;
1493 intfd = get_iface_desc(hostif);
1494
1495 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1496 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001497 if (!usb_endpoint_xfer_bulk(epd) &&
1498 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 continue;
1500 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001501 usb_endpoint_dir_out(epd)) {
1502 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1503 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 endpoint[out_eps].out_interval = epd->bInterval;
1505 ++out_eps;
1506 }
1507 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001508 usb_endpoint_dir_in(epd)) {
1509 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1510 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 endpoint[in_eps].in_interval = epd->bInterval;
1512 ++in_eps;
1513 }
1514 }
1515 return (out_eps || in_eps) ? 0 : -ENOENT;
1516}
1517
1518/*
1519 * Detects the endpoints for one-port-per-endpoint protocols.
1520 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001521static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1522 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 int err, i;
1525
1526 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1527 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1528 if (endpoints[i].out_ep)
1529 endpoints[i].out_cables = 0x0001;
1530 if (endpoints[i].in_ep)
1531 endpoints[i].in_cables = 0x0001;
1532 }
1533 return err;
1534}
1535
1536/*
1537 * Detects the endpoints and ports of Yamaha devices.
1538 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001539static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1540 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct usb_interface* intf;
1543 struct usb_host_interface *hostif;
1544 struct usb_interface_descriptor* intfd;
1545 uint8_t* cs_desc;
1546
1547 intf = umidi->iface;
1548 if (!intf)
1549 return -ENOENT;
1550 hostif = intf->altsetting;
1551 intfd = get_iface_desc(hostif);
1552 if (intfd->bNumEndpoints < 1)
1553 return -ENOENT;
1554
1555 /*
1556 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1557 * necessarily with any useful contents. So simply count 'em.
1558 */
1559 for (cs_desc = hostif->extra;
1560 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1561 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001562 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (cs_desc[2] == MIDI_IN_JACK)
1564 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1565 else if (cs_desc[2] == MIDI_OUT_JACK)
1566 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1567 }
1568 }
1569 if (!endpoint->in_cables && !endpoint->out_cables)
1570 return -ENOENT;
1571
1572 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1573}
1574
1575/*
1576 * Creates the endpoints and their ports for Midiman devices.
1577 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001578static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1579 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001581 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 struct usb_interface* intf;
1583 struct usb_host_interface *hostif;
1584 struct usb_interface_descriptor* intfd;
1585 struct usb_endpoint_descriptor* epd;
1586 int cable, err;
1587
1588 intf = umidi->iface;
1589 if (!intf)
1590 return -ENOENT;
1591 hostif = intf->altsetting;
1592 intfd = get_iface_desc(hostif);
1593 /*
1594 * The various MidiSport devices have more or less random endpoint
1595 * numbers, so we have to identify the endpoints by their index in
1596 * the descriptor array, like the driver for that other OS does.
1597 *
1598 * There is one interrupt input endpoint for all input ports, one
1599 * bulk output endpoint for even-numbered ports, and one for odd-
1600 * numbered ports. Both bulk output endpoints have corresponding
1601 * input bulk endpoints (at indices 1 and 3) which aren't used.
1602 */
1603 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1604 snd_printdd(KERN_ERR "not enough endpoints\n");
1605 return -ENOENT;
1606 }
1607
1608 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001609 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1611 return -ENXIO;
1612 }
1613 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001614 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1616 return -ENXIO;
1617 }
1618 if (endpoint->out_cables > 0x0001) {
1619 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001620 if (!usb_endpoint_dir_out(epd) ||
1621 !usb_endpoint_xfer_bulk(epd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1623 return -ENXIO;
1624 }
1625 }
1626
1627 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01001628 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 ep_info.out_cables = endpoint->out_cables & 0x5555;
1630 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1631 if (err < 0)
1632 return err;
1633
1634 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1635 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1636 ep_info.in_cables = endpoint->in_cables;
1637 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1638 if (err < 0)
1639 return err;
1640
1641 if (endpoint->out_cables > 0x0001) {
1642 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1643 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1644 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1645 if (err < 0)
1646 return err;
1647 }
1648
1649 for (cable = 0; cable < 0x10; ++cable) {
1650 if (endpoint->out_cables & (1 << cable))
1651 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1652 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1653 if (endpoint->in_cables & (1 << cable))
1654 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1655 &umidi->endpoints[0].in->ports[cable].substream);
1656 }
1657 return 0;
1658}
1659
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001660static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1661 .get_port_info = snd_usbmidi_get_port_info,
1662};
1663
Takashi Iwai86e07d32005-11-17 15:08:02 +01001664static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int out_ports, int in_ports)
1666{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001667 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int err;
1669
1670 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1671 umidi->chip->next_midi_device++,
1672 out_ports, in_ports, &rmidi);
1673 if (err < 0)
1674 return err;
1675 strcpy(rmidi->name, umidi->chip->card->shortname);
1676 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1677 SNDRV_RAWMIDI_INFO_INPUT |
1678 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001679 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 rmidi->private_data = umidi;
1681 rmidi->private_free = snd_usbmidi_rawmidi_free;
1682 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1683 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1684
1685 umidi->rmidi = rmidi;
1686 return 0;
1687}
1688
1689/*
1690 * Temporarily stop input.
1691 */
1692void snd_usbmidi_input_stop(struct list_head* p)
1693{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001694 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 int i;
1696
Takashi Iwai86e07d32005-11-17 15:08:02 +01001697 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001699 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (ep->in)
1701 usb_kill_urb(ep->in->urb);
1702 }
1703}
1704
Takashi Iwai86e07d32005-11-17 15:08:02 +01001705static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 if (ep) {
1708 struct urb* urb = ep->urb;
1709 urb->dev = ep->umidi->chip->dev;
1710 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1711 }
1712}
1713
1714/*
1715 * Resume input after a call to snd_usbmidi_input_stop().
1716 */
1717void snd_usbmidi_input_start(struct list_head* p)
1718{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001719 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 int i;
1721
Takashi Iwai86e07d32005-11-17 15:08:02 +01001722 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1724 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1725}
1726
1727/*
1728 * Creates and registers everything needed for a MIDI streaming interface.
1729 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001730int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct usb_interface* iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001732 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001734 struct snd_usb_midi* umidi;
1735 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 int out_ports, in_ports;
1737 int i, err;
1738
Takashi Iwai561b2202005-09-09 14:22:34 +02001739 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (!umidi)
1741 return -ENOMEM;
1742 umidi->chip = chip;
1743 umidi->iface = iface;
1744 umidi->quirk = quirk;
1745 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001746 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001747 spin_lock_init(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +02001748 umidi->error_timer.function = snd_usbmidi_error_timer;
1749 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 /* detect the endpoint(s) to use */
1752 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001753 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1754 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd05cc10432007-05-07 09:28:53 +02001756 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1757 umidi->usb_protocol_ops =
1758 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001759 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02001760 case QUIRK_MIDI_US122L:
1761 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1762 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02001763 case QUIRK_MIDI_FIXED_ENDPOINT:
1764 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001765 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001766 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1767 break;
1768 case QUIRK_MIDI_YAMAHA:
1769 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1770 break;
1771 case QUIRK_MIDI_MIDIMAN:
1772 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1773 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001774 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001775 err = 0;
1776 break;
1777 case QUIRK_MIDI_NOVATION:
1778 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1779 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1780 break;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001781 case QUIRK_MIDI_FASTLANE:
Clemens Ladischd1bda042005-09-14 08:36:03 +02001782 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02001783 /*
1784 * Interface 1 contains isochronous endpoints, but with the same
1785 * numbers as in interface 0. Since it is interface 1 that the
1786 * USB core has most recently seen, these descriptors are now
1787 * associated with the endpoint numbers. This will foul up our
1788 * attempts to submit bulk/interrupt URBs to the endpoints in
1789 * interface 0, so we have to make sure that the USB core looks
1790 * again at interface 0 by calling usb_set_interface() on it.
1791 */
1792 usb_set_interface(umidi->chip->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02001793 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1794 break;
1795 case QUIRK_MIDI_EMAGIC:
1796 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1797 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001798 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001799 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1800 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01001801 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02001802 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001803 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1804 break;
1805 default:
1806 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1807 err = -ENXIO;
1808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810 if (err < 0) {
1811 kfree(umidi);
1812 return err;
1813 }
1814
1815 /* create rawmidi device */
1816 out_ports = 0;
1817 in_ports = 0;
1818 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1819 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1820 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1821 }
1822 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1823 if (err < 0) {
1824 kfree(umidi);
1825 return err;
1826 }
1827
1828 /* create endpoint/port structures */
1829 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1830 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1831 else
1832 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1833 if (err < 0) {
1834 snd_usbmidi_free(umidi);
1835 return err;
1836 }
1837
1838 list_add(&umidi->list, &umidi->chip->midi_list);
1839
1840 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1841 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1842 return 0;
1843}
1844
1845EXPORT_SYMBOL(snd_usb_create_midi_interface);
1846EXPORT_SYMBOL(snd_usbmidi_input_stop);
1847EXPORT_SYMBOL(snd_usbmidi_input_start);
1848EXPORT_SYMBOL(snd_usbmidi_disconnect);