sound: usb-audio: use multiple output URBs
[pandora-kernel.git] / sound / usb / usbmidi.c
1 /*
2  * usbmidi.c - ALSA USB MIDI driver
3  *
4  * Copyright (c) 2002-2007 Clemens Ladisch
5  * 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
38 #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>
46 #include <linux/timer.h>
47 #include <linux/usb.h>
48 #include <sound/core.h>
49 #include <sound/rawmidi.h>
50 #include <sound/asequencer.h>
51 #include "usbaudio.h"
52
53
54 /*
55  * define this to log all USB packets
56  */
57 /* #define DUMP_PACKETS */
58
59 /*
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
65 #define OUTPUT_URBS 7
66 #define INPUT_URBS 7
67
68
69 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
70 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
71 MODULE_LICENSE("Dual BSD/GPL");
72
73
74 struct usb_ms_header_descriptor {
75         __u8  bLength;
76         __u8  bDescriptorType;
77         __u8  bDescriptorSubtype;
78         __u8  bcdMSC[2];
79         __le16 wTotalLength;
80 } __attribute__ ((packed));
81
82 struct usb_ms_endpoint_descriptor {
83         __u8  bLength;
84         __u8  bDescriptorType;
85         __u8  bDescriptorSubtype;
86         __u8  bNumEmbMIDIJack;
87         __u8  baAssocJackID[0];
88 } __attribute__ ((packed));
89
90 struct snd_usb_midi_in_endpoint;
91 struct snd_usb_midi_out_endpoint;
92 struct snd_usb_midi_endpoint;
93
94 struct usb_protocol_ops {
95         void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
96         void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
97         void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
98         void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
99         void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
100 };
101
102 struct snd_usb_midi {
103         struct snd_usb_audio *chip;
104         struct usb_interface *iface;
105         const struct snd_usb_audio_quirk *quirk;
106         struct snd_rawmidi *rmidi;
107         struct usb_protocol_ops* usb_protocol_ops;
108         struct list_head list;
109         struct timer_list error_timer;
110         spinlock_t disc_lock;
111
112         struct snd_usb_midi_endpoint {
113                 struct snd_usb_midi_out_endpoint *out;
114                 struct snd_usb_midi_in_endpoint *in;
115         } endpoints[MIDI_MAX_ENDPOINTS];
116         unsigned long input_triggered;
117         unsigned char disconnected;
118 };
119
120 struct snd_usb_midi_out_endpoint {
121         struct snd_usb_midi* umidi;
122         struct out_urb_context {
123                 struct urb *urb;
124                 struct snd_usb_midi_out_endpoint *ep;
125         } urbs[OUTPUT_URBS];
126         unsigned int active_urbs;
127         int max_transfer;               /* size of urb buffer */
128         struct tasklet_struct tasklet;
129
130         spinlock_t buffer_lock;
131
132         struct usbmidi_out_port {
133                 struct snd_usb_midi_out_endpoint* ep;
134                 struct snd_rawmidi_substream *substream;
135                 int active;
136                 uint8_t cable;          /* cable number << 4 */
137                 uint8_t state;
138 #define STATE_UNKNOWN   0
139 #define STATE_1PARAM    1
140 #define STATE_2PARAM_1  2
141 #define STATE_2PARAM_2  3
142 #define STATE_SYSEX_0   4
143 #define STATE_SYSEX_1   5
144 #define STATE_SYSEX_2   6
145                 uint8_t data[2];
146         } ports[0x10];
147         int current_port;
148 };
149
150 struct snd_usb_midi_in_endpoint {
151         struct snd_usb_midi* umidi;
152         struct urb* urbs[INPUT_URBS];
153         struct usbmidi_in_port {
154                 struct snd_rawmidi_substream *substream;
155                 u8 running_status_length;
156         } ports[0x10];
157         u8 seen_f5;
158         u8 error_resubmit;
159         int current_port;
160 };
161
162 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
163
164 static const uint8_t snd_usbmidi_cin_length[] = {
165         0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
166 };
167
168 /*
169  * Submits the URB, with error handling.
170  */
171 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
172 {
173         int err = usb_submit_urb(urb, flags);
174         if (err < 0 && err != -ENODEV)
175                 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
176         return err;
177 }
178
179 /*
180  * Error handling for URB completion functions.
181  */
182 static int snd_usbmidi_urb_error(int status)
183 {
184         switch (status) {
185         /* manually unlinked, or device gone */
186         case -ENOENT:
187         case -ECONNRESET:
188         case -ESHUTDOWN:
189         case -ENODEV:
190                 return -ENODEV;
191         /* errors that might occur during unplugging */
192         case -EPROTO:
193         case -ETIME:
194         case -EILSEQ:
195                 return -EIO;
196         default:
197                 snd_printk(KERN_ERR "urb status %d\n", status);
198                 return 0; /* continue */
199         }
200 }
201
202 /*
203  * Receives a chunk of MIDI data.
204  */
205 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
206                                    uint8_t* data, int length)
207 {
208         struct usbmidi_in_port* port = &ep->ports[portidx];
209
210         if (!port->substream) {
211                 snd_printd("unexpected port %d!\n", portidx);
212                 return;
213         }
214         if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
215                 return;
216         snd_rawmidi_receive(port->substream, data, length);
217 }
218
219 #ifdef DUMP_PACKETS
220 static void dump_urb(const char *type, const u8 *data, int length)
221 {
222         snd_printk(KERN_DEBUG "%s packet: [", type);
223         for (; length > 0; ++data, --length)
224                 printk(" %02x", *data);
225         printk(" ]\n");
226 }
227 #else
228 #define dump_urb(type, data, length) /* nothing */
229 #endif
230
231 /*
232  * Processes the data read from the device.
233  */
234 static void snd_usbmidi_in_urb_complete(struct urb* urb)
235 {
236         struct snd_usb_midi_in_endpoint* ep = urb->context;
237
238         if (urb->status == 0) {
239                 dump_urb("received", urb->transfer_buffer, urb->actual_length);
240                 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
241                                                    urb->actual_length);
242         } else {
243                 int err = snd_usbmidi_urb_error(urb->status);
244                 if (err < 0) {
245                         if (err != -ENODEV) {
246                                 ep->error_resubmit = 1;
247                                 mod_timer(&ep->umidi->error_timer,
248                                           jiffies + ERROR_DELAY_JIFFIES);
249                         }
250                         return;
251                 }
252         }
253
254         urb->dev = ep->umidi->chip->dev;
255         snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
256 }
257
258 static void snd_usbmidi_out_urb_complete(struct urb* urb)
259 {
260         struct out_urb_context *context = urb->context;
261         struct snd_usb_midi_out_endpoint* ep = context->ep;
262
263         spin_lock(&ep->buffer_lock);
264         ep->active_urbs &= ~(1 << (context - ep->urbs));
265         spin_unlock(&ep->buffer_lock);
266         if (urb->status < 0) {
267                 int err = snd_usbmidi_urb_error(urb->status);
268                 if (err < 0) {
269                         if (err != -ENODEV)
270                                 mod_timer(&ep->umidi->error_timer,
271                                           jiffies + ERROR_DELAY_JIFFIES);
272                         return;
273                 }
274         }
275         snd_usbmidi_do_output(ep);
276 }
277
278 /*
279  * This is called when some data should be transferred to the device
280  * (from one or more substreams).
281  */
282 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
283 {
284         unsigned int urb_index;
285         struct urb* urb;
286         unsigned long flags;
287
288         spin_lock_irqsave(&ep->buffer_lock, flags);
289         if (ep->umidi->chip->shutdown) {
290                 spin_unlock_irqrestore(&ep->buffer_lock, flags);
291                 return;
292         }
293
294         for (;;) {
295                 urb = NULL;
296                 for (urb_index = 0; urb_index < OUTPUT_URBS; ++urb_index)
297                         if (!(ep->active_urbs & (1 << urb_index))) {
298                                 urb = ep->urbs[urb_index].urb;
299                                 break;
300                         }
301                 if (!urb)
302                         break;
303
304                 urb->transfer_buffer_length = 0;
305                 ep->umidi->usb_protocol_ops->output(ep, urb);
306                 if (urb->transfer_buffer_length == 0)
307                         break;
308
309                 dump_urb("sending", urb->transfer_buffer,
310                          urb->transfer_buffer_length);
311                 urb->dev = ep->umidi->chip->dev;
312                 if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
313                         break;
314                 ep->active_urbs |= 1 << urb_index;
315         }
316         spin_unlock_irqrestore(&ep->buffer_lock, flags);
317 }
318
319 static void snd_usbmidi_out_tasklet(unsigned long data)
320 {
321         struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
322
323         snd_usbmidi_do_output(ep);
324 }
325
326 /* called after transfers had been interrupted due to some USB error */
327 static void snd_usbmidi_error_timer(unsigned long data)
328 {
329         struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
330         unsigned int i, j;
331
332         spin_lock(&umidi->disc_lock);
333         if (umidi->disconnected) {
334                 spin_unlock(&umidi->disc_lock);
335                 return;
336         }
337         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
338                 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
339                 if (in && in->error_resubmit) {
340                         in->error_resubmit = 0;
341                         for (j = 0; j < INPUT_URBS; ++j) {
342                                 in->urbs[j]->dev = umidi->chip->dev;
343                                 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
344                         }
345                 }
346                 if (umidi->endpoints[i].out)
347                         snd_usbmidi_do_output(umidi->endpoints[i].out);
348         }
349         spin_unlock(&umidi->disc_lock);
350 }
351
352 /* helper function to send static data that may not DMA-able */
353 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
354                                  const void *data, int len)
355 {
356         int err = 0;
357         void *buf = kmemdup(data, len, GFP_KERNEL);
358         if (!buf)
359                 return -ENOMEM;
360         dump_urb("sending", buf, len);
361         if (ep->urbs[0].urb)
362                 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urbs[0].urb->pipe,
363                                    buf, len, NULL, 250);
364         kfree(buf);
365         return err;
366 }
367
368 /*
369  * Standard USB MIDI protocol: see the spec.
370  * Midiman protocol: like the standard protocol, but the control byte is the
371  * fourth byte in each packet, and uses length instead of CIN.
372  */
373
374 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
375                                        uint8_t* buffer, int buffer_length)
376 {
377         int i;
378
379         for (i = 0; i + 3 < buffer_length; i += 4)
380                 if (buffer[i] != 0) {
381                         int cable = buffer[i] >> 4;
382                         int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
383                         snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
384                 }
385 }
386
387 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
388                                       uint8_t* buffer, int buffer_length)
389 {
390         int i;
391
392         for (i = 0; i + 3 < buffer_length; i += 4)
393                 if (buffer[i + 3] != 0) {
394                         int port = buffer[i + 3] >> 4;
395                         int length = buffer[i + 3] & 3;
396                         snd_usbmidi_input_data(ep, port, &buffer[i], length);
397                 }
398 }
399
400 /*
401  * Buggy M-Audio device: running status on input results in a packet that has
402  * the data bytes but not the status byte and that is marked with CIN 4.
403  */
404 static void snd_usbmidi_maudio_broken_running_status_input(
405                                         struct snd_usb_midi_in_endpoint* ep,
406                                         uint8_t* buffer, int buffer_length)
407 {
408         int i;
409
410         for (i = 0; i + 3 < buffer_length; i += 4)
411                 if (buffer[i] != 0) {
412                         int cable = buffer[i] >> 4;
413                         u8 cin = buffer[i] & 0x0f;
414                         struct usbmidi_in_port *port = &ep->ports[cable];
415                         int length;
416                         
417                         length = snd_usbmidi_cin_length[cin];
418                         if (cin == 0xf && buffer[i + 1] >= 0xf8)
419                                 ; /* realtime msg: no running status change */
420                         else if (cin >= 0x8 && cin <= 0xe)
421                                 /* channel msg */
422                                 port->running_status_length = length - 1;
423                         else if (cin == 0x4 &&
424                                  port->running_status_length != 0 &&
425                                  buffer[i + 1] < 0x80)
426                                 /* CIN 4 that is not a SysEx */
427                                 length = port->running_status_length;
428                         else
429                                 /*
430                                  * All other msgs cannot begin running status.
431                                  * (A channel msg sent as two or three CIN 0xF
432                                  * packets could in theory, but this device
433                                  * doesn't use this format.)
434                                  */
435                                 port->running_status_length = 0;
436                         snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
437                 }
438 }
439
440 /*
441  * CME protocol: like the standard protocol, but SysEx commands are sent as a
442  * single USB packet preceded by a 0x0F byte.
443  */
444 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
445                                   uint8_t *buffer, int buffer_length)
446 {
447         if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
448                 snd_usbmidi_standard_input(ep, buffer, buffer_length);
449         else
450                 snd_usbmidi_input_data(ep, buffer[0] >> 4,
451                                        &buffer[1], buffer_length - 1);
452 }
453
454 /*
455  * Adds one USB MIDI packet to the output buffer.
456  */
457 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
458                                                uint8_t p1, uint8_t p2, uint8_t p3)
459 {
460
461         uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
462         buf[0] = p0;
463         buf[1] = p1;
464         buf[2] = p2;
465         buf[3] = p3;
466         urb->transfer_buffer_length += 4;
467 }
468
469 /*
470  * Adds one Midiman packet to the output buffer.
471  */
472 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
473                                               uint8_t p1, uint8_t p2, uint8_t p3)
474 {
475
476         uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
477         buf[0] = p1;
478         buf[1] = p2;
479         buf[2] = p3;
480         buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
481         urb->transfer_buffer_length += 4;
482 }
483
484 /*
485  * Converts MIDI commands to USB MIDI packets.
486  */
487 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
488                                       uint8_t b, struct urb* urb)
489 {
490         uint8_t p0 = port->cable;
491         void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
492                 port->ep->umidi->usb_protocol_ops->output_packet;
493
494         if (b >= 0xf8) {
495                 output_packet(urb, p0 | 0x0f, b, 0, 0);
496         } else if (b >= 0xf0) {
497                 switch (b) {
498                 case 0xf0:
499                         port->data[0] = b;
500                         port->state = STATE_SYSEX_1;
501                         break;
502                 case 0xf1:
503                 case 0xf3:
504                         port->data[0] = b;
505                         port->state = STATE_1PARAM;
506                         break;
507                 case 0xf2:
508                         port->data[0] = b;
509                         port->state = STATE_2PARAM_1;
510                         break;
511                 case 0xf4:
512                 case 0xf5:
513                         port->state = STATE_UNKNOWN;
514                         break;
515                 case 0xf6:
516                         output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
517                         port->state = STATE_UNKNOWN;
518                         break;
519                 case 0xf7:
520                         switch (port->state) {
521                         case STATE_SYSEX_0:
522                                 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
523                                 break;
524                         case STATE_SYSEX_1:
525                                 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
526                                 break;
527                         case STATE_SYSEX_2:
528                                 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
529                                 break;
530                         }
531                         port->state = STATE_UNKNOWN;
532                         break;
533                 }
534         } else if (b >= 0x80) {
535                 port->data[0] = b;
536                 if (b >= 0xc0 && b <= 0xdf)
537                         port->state = STATE_1PARAM;
538                 else
539                         port->state = STATE_2PARAM_1;
540         } else { /* b < 0x80 */
541                 switch (port->state) {
542                 case STATE_1PARAM:
543                         if (port->data[0] < 0xf0) {
544                                 p0 |= port->data[0] >> 4;
545                         } else {
546                                 p0 |= 0x02;
547                                 port->state = STATE_UNKNOWN;
548                         }
549                         output_packet(urb, p0, port->data[0], b, 0);
550                         break;
551                 case STATE_2PARAM_1:
552                         port->data[1] = b;
553                         port->state = STATE_2PARAM_2;
554                         break;
555                 case STATE_2PARAM_2:
556                         if (port->data[0] < 0xf0) {
557                                 p0 |= port->data[0] >> 4;
558                                 port->state = STATE_2PARAM_1;
559                         } else {
560                                 p0 |= 0x03;
561                                 port->state = STATE_UNKNOWN;
562                         }
563                         output_packet(urb, p0, port->data[0], port->data[1], b);
564                         break;
565                 case STATE_SYSEX_0:
566                         port->data[0] = b;
567                         port->state = STATE_SYSEX_1;
568                         break;
569                 case STATE_SYSEX_1:
570                         port->data[1] = b;
571                         port->state = STATE_SYSEX_2;
572                         break;
573                 case STATE_SYSEX_2:
574                         output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
575                         port->state = STATE_SYSEX_0;
576                         break;
577                 }
578         }
579 }
580
581 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
582                                         struct urb *urb)
583 {
584         int p;
585
586         /* FIXME: lower-numbered ports can starve higher-numbered ports */
587         for (p = 0; p < 0x10; ++p) {
588                 struct usbmidi_out_port* port = &ep->ports[p];
589                 if (!port->active)
590                         continue;
591                 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
592                         uint8_t b;
593                         if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
594                                 port->active = 0;
595                                 break;
596                         }
597                         snd_usbmidi_transmit_byte(port, b, urb);
598                 }
599         }
600 }
601
602 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
603         .input = snd_usbmidi_standard_input,
604         .output = snd_usbmidi_standard_output,
605         .output_packet = snd_usbmidi_output_standard_packet,
606 };
607
608 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
609         .input = snd_usbmidi_midiman_input,
610         .output = snd_usbmidi_standard_output, 
611         .output_packet = snd_usbmidi_output_midiman_packet,
612 };
613
614 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
615         .input = snd_usbmidi_maudio_broken_running_status_input,
616         .output = snd_usbmidi_standard_output, 
617         .output_packet = snd_usbmidi_output_standard_packet,
618 };
619
620 static struct usb_protocol_ops snd_usbmidi_cme_ops = {
621         .input = snd_usbmidi_cme_input,
622         .output = snd_usbmidi_standard_output,
623         .output_packet = snd_usbmidi_output_standard_packet,
624 };
625
626 /*
627  * Novation USB MIDI protocol: number of data bytes is in the first byte
628  * (when receiving) (+1!) or in the second byte (when sending); data begins
629  * at the third byte.
630  */
631
632 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
633                                        uint8_t* buffer, int buffer_length)
634 {
635         if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
636                 return;
637         snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
638 }
639
640 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
641                                         struct urb *urb)
642 {
643         uint8_t* transfer_buffer;
644         int count;
645
646         if (!ep->ports[0].active)
647                 return;
648         transfer_buffer = urb->transfer_buffer;
649         count = snd_rawmidi_transmit(ep->ports[0].substream,
650                                      &transfer_buffer[2],
651                                      ep->max_transfer - 2);
652         if (count < 1) {
653                 ep->ports[0].active = 0;
654                 return;
655         }
656         transfer_buffer[0] = 0;
657         transfer_buffer[1] = count;
658         urb->transfer_buffer_length = 2 + count;
659 }
660
661 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
662         .input = snd_usbmidi_novation_input,
663         .output = snd_usbmidi_novation_output,
664 };
665
666 /*
667  * "raw" protocol: used by the MOTU FastLane.
668  */
669
670 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
671                                   uint8_t* buffer, int buffer_length)
672 {
673         snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
674 }
675
676 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
677                                    struct urb *urb)
678 {
679         int count;
680
681         if (!ep->ports[0].active)
682                 return;
683         count = snd_rawmidi_transmit(ep->ports[0].substream,
684                                      urb->transfer_buffer,
685                                      ep->max_transfer);
686         if (count < 1) {
687                 ep->ports[0].active = 0;
688                 return;
689         }
690         urb->transfer_buffer_length = count;
691 }
692
693 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
694         .input = snd_usbmidi_raw_input,
695         .output = snd_usbmidi_raw_output,
696 };
697
698 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
699                                      uint8_t *buffer, int buffer_length)
700 {
701         if (buffer_length != 9)
702                 return;
703         buffer_length = 8;
704         while (buffer_length && buffer[buffer_length - 1] == 0xFD)
705                 buffer_length--;
706         if (buffer_length)
707                 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
708 }
709
710 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
711                                       struct urb *urb)
712 {
713         int count;
714
715         if (!ep->ports[0].active)
716                 return;
717         count = snd_usb_get_speed(ep->umidi->chip->dev) == USB_SPEED_HIGH
718                 ? 1 : 2;
719         count = snd_rawmidi_transmit(ep->ports[0].substream,
720                                      urb->transfer_buffer,
721                                      count);
722         if (count < 1) {
723                 ep->ports[0].active = 0;
724                 return;
725         }
726
727         memset(urb->transfer_buffer + count, 0xFD, 9 - count);
728         urb->transfer_buffer_length = count;
729 }
730
731 static struct usb_protocol_ops snd_usbmidi_122l_ops = {
732         .input = snd_usbmidi_us122l_input,
733         .output = snd_usbmidi_us122l_output,
734 };
735
736 /*
737  * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
738  */
739
740 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
741 {
742         static const u8 init_data[] = {
743                 /* initialization magic: "get version" */
744                 0xf0,
745                 0x00, 0x20, 0x31,       /* Emagic */
746                 0x64,                   /* Unitor8 */
747                 0x0b,                   /* version number request */
748                 0x00,                   /* command version */
749                 0x00,                   /* EEPROM, box 0 */
750                 0xf7
751         };
752         send_bulk_static_data(ep, init_data, sizeof(init_data));
753         /* while we're at it, pour on more magic */
754         send_bulk_static_data(ep, init_data, sizeof(init_data));
755 }
756
757 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
758 {
759         static const u8 finish_data[] = {
760                 /* switch to patch mode with last preset */
761                 0xf0,
762                 0x00, 0x20, 0x31,       /* Emagic */
763                 0x64,                   /* Unitor8 */
764                 0x10,                   /* patch switch command */
765                 0x00,                   /* command version */
766                 0x7f,                   /* to all boxes */
767                 0x40,                   /* last preset in EEPROM */
768                 0xf7
769         };
770         send_bulk_static_data(ep, finish_data, sizeof(finish_data));
771 }
772
773 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
774                                      uint8_t* buffer, int buffer_length)
775 {
776         int i;
777
778         /* FF indicates end of valid data */
779         for (i = 0; i < buffer_length; ++i)
780                 if (buffer[i] == 0xff) {
781                         buffer_length = i;
782                         break;
783                 }
784
785         /* handle F5 at end of last buffer */
786         if (ep->seen_f5)
787                 goto switch_port;
788
789         while (buffer_length > 0) {
790                 /* determine size of data until next F5 */
791                 for (i = 0; i < buffer_length; ++i)
792                         if (buffer[i] == 0xf5)
793                                 break;
794                 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
795                 buffer += i;
796                 buffer_length -= i;
797
798                 if (buffer_length <= 0)
799                         break;
800                 /* assert(buffer[0] == 0xf5); */
801                 ep->seen_f5 = 1;
802                 ++buffer;
803                 --buffer_length;
804
805         switch_port:
806                 if (buffer_length <= 0)
807                         break;
808                 if (buffer[0] < 0x80) {
809                         ep->current_port = (buffer[0] - 1) & 15;
810                         ++buffer;
811                         --buffer_length;
812                 }
813                 ep->seen_f5 = 0;
814         }
815 }
816
817 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
818                                       struct urb *urb)
819 {
820         int port0 = ep->current_port;
821         uint8_t* buf = urb->transfer_buffer;
822         int buf_free = ep->max_transfer;
823         int length, i;
824
825         for (i = 0; i < 0x10; ++i) {
826                 /* round-robin, starting at the last current port */
827                 int portnum = (port0 + i) & 15;
828                 struct usbmidi_out_port* port = &ep->ports[portnum];
829
830                 if (!port->active)
831                         continue;
832                 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
833                         port->active = 0;
834                         continue;
835                 }
836
837                 if (portnum != ep->current_port) {
838                         if (buf_free < 2)
839                                 break;
840                         ep->current_port = portnum;
841                         buf[0] = 0xf5;
842                         buf[1] = (portnum + 1) & 15;
843                         buf += 2;
844                         buf_free -= 2;
845                 }
846
847                 if (buf_free < 1)
848                         break;
849                 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
850                 if (length > 0) {
851                         buf += length;
852                         buf_free -= length;
853                         if (buf_free < 1)
854                                 break;
855                 }
856         }
857         if (buf_free < ep->max_transfer && buf_free > 0) {
858                 *buf = 0xff;
859                 --buf_free;
860         }
861         urb->transfer_buffer_length = ep->max_transfer - buf_free;
862 }
863
864 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
865         .input = snd_usbmidi_emagic_input,
866         .output = snd_usbmidi_emagic_output,
867         .init_out_endpoint = snd_usbmidi_emagic_init_out,
868         .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
869 };
870
871
872 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
873 {
874         struct snd_usb_midi* umidi = substream->rmidi->private_data;
875         struct usbmidi_out_port* port = NULL;
876         int i, j;
877
878         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
879                 if (umidi->endpoints[i].out)
880                         for (j = 0; j < 0x10; ++j)
881                                 if (umidi->endpoints[i].out->ports[j].substream == substream) {
882                                         port = &umidi->endpoints[i].out->ports[j];
883                                         break;
884                                 }
885         if (!port) {
886                 snd_BUG();
887                 return -ENXIO;
888         }
889         substream->runtime->private_data = port;
890         port->state = STATE_UNKNOWN;
891         return 0;
892 }
893
894 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
895 {
896         return 0;
897 }
898
899 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
900 {
901         struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
902
903         port->active = up;
904         if (up) {
905                 if (port->ep->umidi->chip->shutdown) {
906                         /* gobble up remaining bytes to prevent wait in
907                          * snd_rawmidi_drain_output */
908                         while (!snd_rawmidi_transmit_empty(substream))
909                                 snd_rawmidi_transmit_ack(substream, 1);
910                         return;
911                 }
912                 tasklet_schedule(&port->ep->tasklet);
913         }
914 }
915
916 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
917 {
918         return 0;
919 }
920
921 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
922 {
923         return 0;
924 }
925
926 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
927 {
928         struct snd_usb_midi* umidi = substream->rmidi->private_data;
929
930         if (up)
931                 set_bit(substream->number, &umidi->input_triggered);
932         else
933                 clear_bit(substream->number, &umidi->input_triggered);
934 }
935
936 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
937         .open = snd_usbmidi_output_open,
938         .close = snd_usbmidi_output_close,
939         .trigger = snd_usbmidi_output_trigger,
940 };
941
942 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
943         .open = snd_usbmidi_input_open,
944         .close = snd_usbmidi_input_close,
945         .trigger = snd_usbmidi_input_trigger
946 };
947
948 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
949                                 unsigned int buffer_length)
950 {
951         usb_buffer_free(umidi->chip->dev, buffer_length,
952                         urb->transfer_buffer, urb->transfer_dma);
953         usb_free_urb(urb);
954 }
955
956 /*
957  * Frees an input endpoint.
958  * May be called when ep hasn't been initialized completely.
959  */
960 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
961 {
962         unsigned int i;
963
964         for (i = 0; i < INPUT_URBS; ++i)
965                 if (ep->urbs[i])
966                         free_urb_and_buffer(ep->umidi, ep->urbs[i],
967                                             ep->urbs[i]->transfer_buffer_length);
968         kfree(ep);
969 }
970
971 /*
972  * Creates an input endpoint.
973  */
974 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
975                                           struct snd_usb_midi_endpoint_info* ep_info,
976                                           struct snd_usb_midi_endpoint* rep)
977 {
978         struct snd_usb_midi_in_endpoint* ep;
979         void* buffer;
980         unsigned int pipe;
981         int length;
982         unsigned int i;
983
984         rep->in = NULL;
985         ep = kzalloc(sizeof(*ep), GFP_KERNEL);
986         if (!ep)
987                 return -ENOMEM;
988         ep->umidi = umidi;
989
990         for (i = 0; i < INPUT_URBS; ++i) {
991                 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
992                 if (!ep->urbs[i]) {
993                         snd_usbmidi_in_endpoint_delete(ep);
994                         return -ENOMEM;
995                 }
996         }
997         if (ep_info->in_interval)
998                 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
999         else
1000                 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
1001         length = usb_maxpacket(umidi->chip->dev, pipe, 0);
1002         for (i = 0; i < INPUT_URBS; ++i) {
1003                 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
1004                                           &ep->urbs[i]->transfer_dma);
1005                 if (!buffer) {
1006                         snd_usbmidi_in_endpoint_delete(ep);
1007                         return -ENOMEM;
1008                 }
1009                 if (ep_info->in_interval)
1010                         usb_fill_int_urb(ep->urbs[i], umidi->chip->dev,
1011                                          pipe, buffer, length,
1012                                          snd_usbmidi_in_urb_complete,
1013                                          ep, ep_info->in_interval);
1014                 else
1015                         usb_fill_bulk_urb(ep->urbs[i], umidi->chip->dev,
1016                                           pipe, buffer, length,
1017                                           snd_usbmidi_in_urb_complete, ep);
1018                 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1019         }
1020
1021         rep->in = ep;
1022         return 0;
1023 }
1024
1025 static unsigned int snd_usbmidi_count_bits(unsigned int x)
1026 {
1027         unsigned int bits;
1028
1029         for (bits = 0; x; ++bits)
1030                 x &= x - 1;
1031         return bits;
1032 }
1033
1034 /*
1035  * Frees an output endpoint.
1036  * May be called when ep hasn't been initialized completely.
1037  */
1038 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
1039 {
1040         unsigned int i;
1041
1042         for (i = 0; i < OUTPUT_URBS; ++i)
1043                 if (ep->urbs[i].urb)
1044                         free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1045                                             ep->max_transfer);
1046         kfree(ep);
1047 }
1048
1049 /*
1050  * Creates an output endpoint, and initializes output ports.
1051  */
1052 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1053                                            struct snd_usb_midi_endpoint_info* ep_info,
1054                                            struct snd_usb_midi_endpoint* rep)
1055 {
1056         struct snd_usb_midi_out_endpoint* ep;
1057         unsigned int i;
1058         unsigned int pipe;
1059         void* buffer;
1060
1061         rep->out = NULL;
1062         ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1063         if (!ep)
1064                 return -ENOMEM;
1065         ep->umidi = umidi;
1066
1067         for (i = 0; i < OUTPUT_URBS; ++i) {
1068                 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1069                 if (!ep->urbs[i].urb) {
1070                         snd_usbmidi_out_endpoint_delete(ep);
1071                         return -ENOMEM;
1072                 }
1073                 ep->urbs[i].ep = ep;
1074         }
1075         if (ep_info->out_interval)
1076                 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
1077         else
1078                 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
1079         if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
1080                 ep->max_transfer = 4;
1081         else
1082                 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
1083         for (i = 0; i < OUTPUT_URBS; ++i) {
1084                 buffer = usb_buffer_alloc(umidi->chip->dev,
1085                                           ep->max_transfer, GFP_KERNEL,
1086                                           &ep->urbs[i].urb->transfer_dma);
1087                 if (!buffer) {
1088                         snd_usbmidi_out_endpoint_delete(ep);
1089                         return -ENOMEM;
1090                 }
1091                 if (ep_info->out_interval)
1092                         usb_fill_int_urb(ep->urbs[i].urb, umidi->chip->dev,
1093                                          pipe, buffer, ep->max_transfer,
1094                                          snd_usbmidi_out_urb_complete,
1095                                          &ep->urbs[i], ep_info->out_interval);
1096                 else
1097                         usb_fill_bulk_urb(ep->urbs[i].urb, umidi->chip->dev,
1098                                           pipe, buffer, ep->max_transfer,
1099                                           snd_usbmidi_out_urb_complete,
1100                                           &ep->urbs[i]);
1101                 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1102         }
1103
1104         spin_lock_init(&ep->buffer_lock);
1105         tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1106
1107         for (i = 0; i < 0x10; ++i)
1108                 if (ep_info->out_cables & (1 << i)) {
1109                         ep->ports[i].ep = ep;
1110                         ep->ports[i].cable = i << 4;
1111                 }
1112
1113         if (umidi->usb_protocol_ops->init_out_endpoint)
1114                 umidi->usb_protocol_ops->init_out_endpoint(ep);
1115
1116         rep->out = ep;
1117         return 0;
1118 }
1119
1120 /*
1121  * Frees everything.
1122  */
1123 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1124 {
1125         int i;
1126
1127         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1128                 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1129                 if (ep->out)
1130                         snd_usbmidi_out_endpoint_delete(ep->out);
1131                 if (ep->in)
1132                         snd_usbmidi_in_endpoint_delete(ep->in);
1133         }
1134         kfree(umidi);
1135 }
1136
1137 /*
1138  * Unlinks all URBs (must be done before the usb_device is deleted).
1139  */
1140 void snd_usbmidi_disconnect(struct list_head* p)
1141 {
1142         struct snd_usb_midi* umidi;
1143         unsigned int i, j;
1144
1145         umidi = list_entry(p, struct snd_usb_midi, list);
1146         /*
1147          * an URB's completion handler may start the timer and
1148          * a timer may submit an URB. To reliably break the cycle
1149          * a flag under lock must be used
1150          */
1151         spin_lock_irq(&umidi->disc_lock);
1152         umidi->disconnected = 1;
1153         spin_unlock_irq(&umidi->disc_lock);
1154         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1155                 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1156                 if (ep->out)
1157                         tasklet_kill(&ep->out->tasklet);
1158                 if (ep->out) {
1159                         for (j = 0; j < OUTPUT_URBS; ++j)
1160                                 usb_kill_urb(ep->out->urbs[j].urb);
1161                         if (umidi->usb_protocol_ops->finish_out_endpoint)
1162                                 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1163                 }
1164                 if (ep->in)
1165                         for (j = 0; j < INPUT_URBS; ++j)
1166                                 usb_kill_urb(ep->in->urbs[j]);
1167                 /* free endpoints here; later call can result in Oops */
1168                 if (ep->out) {
1169                         snd_usbmidi_out_endpoint_delete(ep->out);
1170                         ep->out = NULL;
1171                 }
1172                 if (ep->in) {
1173                         snd_usbmidi_in_endpoint_delete(ep->in);
1174                         ep->in = NULL;
1175                 }
1176         }
1177         del_timer_sync(&umidi->error_timer);
1178 }
1179
1180 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1181 {
1182         struct snd_usb_midi* umidi = rmidi->private_data;
1183         snd_usbmidi_free(umidi);
1184 }
1185
1186 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1187                                                            int stream, int number)
1188 {
1189         struct list_head* list;
1190
1191         list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1192                 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1193                 if (substream->number == number)
1194                         return substream;
1195         }
1196         return NULL;
1197 }
1198
1199 /*
1200  * This list specifies names for ports that do not fit into the standard
1201  * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1202  * such as internal control or synthesizer ports.
1203  */
1204 static struct port_info {
1205         u32 id;
1206         short int port;
1207         short int voices;
1208         const char *name;
1209         unsigned int seq_flags;
1210 } snd_usbmidi_port_info[] = {
1211 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1212         { .id = USB_ID(vendor, product), \
1213           .port = num, .voices = voices_, \
1214           .name = name_, .seq_flags = flags }
1215 #define EXTERNAL_PORT(vendor, product, num, name) \
1216         PORT_INFO(vendor, product, num, name, 0, \
1217                   SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1218                   SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1219                   SNDRV_SEQ_PORT_TYPE_PORT)
1220 #define CONTROL_PORT(vendor, product, num, name) \
1221         PORT_INFO(vendor, product, num, name, 0, \
1222                   SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1223                   SNDRV_SEQ_PORT_TYPE_HARDWARE)
1224 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1225         PORT_INFO(vendor, product, num, name, voices, \
1226                   SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1227                   SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1228                   SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1229                   SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1230                   SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1231                   SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1232                   SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1233 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1234         PORT_INFO(vendor, product, num, name, voices, \
1235                   SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1236                   SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1237                   SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1238                   SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1239                   SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1240                   SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1241                   SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1242                   SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1243         /* Roland UA-100 */
1244         CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1245         /* Roland SC-8850 */
1246         SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1247         SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1248         SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1249         SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1250         EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1251         EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1252         /* Roland U-8 */
1253         EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1254         CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1255         /* Roland SC-8820 */
1256         SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1257         SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1258         EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1259         /* Roland SK-500 */
1260         SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1261         SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1262         EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1263         /* Roland SC-D70 */
1264         SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1265         SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1266         EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1267         /* Edirol UM-880 */
1268         CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1269         /* Edirol SD-90 */
1270         ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1271         ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1272         EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1273         EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1274         /* Edirol UM-550 */
1275         CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1276         /* Edirol SD-20 */
1277         ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1278         ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1279         EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1280         /* Edirol SD-80 */
1281         ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1282         ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1283         EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1284         EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1285         /* Edirol UA-700 */
1286         EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1287         CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1288         /* Roland VariOS */
1289         EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1290         EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1291         EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1292         /* Edirol PCR */
1293         EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1294         EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1295         EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1296         /* BOSS GS-10 */
1297         EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1298         CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1299         /* Edirol UA-1000 */
1300         EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1301         CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1302         /* Edirol UR-80 */
1303         EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1304         EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1305         EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1306         /* Edirol PCR-A */
1307         EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1308         EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1309         EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1310         /* Edirol UM-3EX */
1311         CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1312         /* M-Audio MidiSport 8x8 */
1313         CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1314         CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1315         /* MOTU Fastlane */
1316         EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1317         EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1318         /* Emagic Unitor8/AMT8/MT4 */
1319         EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1320         EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1321         EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1322 };
1323
1324 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1325 {
1326         int i;
1327
1328         for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1329                 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1330                     snd_usbmidi_port_info[i].port == number)
1331                         return &snd_usbmidi_port_info[i];
1332         }
1333         return NULL;
1334 }
1335
1336 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1337                                       struct snd_seq_port_info *seq_port_info)
1338 {
1339         struct snd_usb_midi *umidi = rmidi->private_data;
1340         struct port_info *port_info;
1341
1342         /* TODO: read port flags from descriptors */
1343         port_info = find_port_info(umidi, number);
1344         if (port_info) {
1345                 seq_port_info->type = port_info->seq_flags;
1346                 seq_port_info->midi_voices = port_info->voices;
1347         }
1348 }
1349
1350 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1351                                        int stream, int number,
1352                                        struct snd_rawmidi_substream ** rsubstream)
1353 {
1354         struct port_info *port_info;
1355         const char *name_format;
1356
1357         struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1358         if (!substream) {
1359                 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1360                 return;
1361         }
1362
1363         /* TODO: read port name from jack descriptor */
1364         port_info = find_port_info(umidi, number);
1365         name_format = port_info ? port_info->name : "%s MIDI %d";
1366         snprintf(substream->name, sizeof(substream->name),
1367                  name_format, umidi->chip->card->shortname, number + 1);
1368
1369         *rsubstream = substream;
1370 }
1371
1372 /*
1373  * Creates the endpoints and their ports.
1374  */
1375 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1376                                         struct snd_usb_midi_endpoint_info* endpoints)
1377 {
1378         int i, j, err;
1379         int out_ports = 0, in_ports = 0;
1380
1381         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1382                 if (endpoints[i].out_cables) {
1383                         err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1384                                                               &umidi->endpoints[i]);
1385                         if (err < 0)
1386                                 return err;
1387                 }
1388                 if (endpoints[i].in_cables) {
1389                         err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1390                                                              &umidi->endpoints[i]);
1391                         if (err < 0)
1392                                 return err;
1393                 }
1394
1395                 for (j = 0; j < 0x10; ++j) {
1396                         if (endpoints[i].out_cables & (1 << j)) {
1397                                 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1398                                                            &umidi->endpoints[i].out->ports[j].substream);
1399                                 ++out_ports;
1400                         }
1401                         if (endpoints[i].in_cables & (1 << j)) {
1402                                 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1403                                                            &umidi->endpoints[i].in->ports[j].substream);
1404                                 ++in_ports;
1405                         }
1406                 }
1407         }
1408         snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1409                     out_ports, in_ports);
1410         return 0;
1411 }
1412
1413 /*
1414  * Returns MIDIStreaming device capabilities.
1415  */
1416 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1417                                    struct snd_usb_midi_endpoint_info* endpoints)
1418 {
1419         struct usb_interface* intf;
1420         struct usb_host_interface *hostif;
1421         struct usb_interface_descriptor* intfd;
1422         struct usb_ms_header_descriptor* ms_header;
1423         struct usb_host_endpoint *hostep;
1424         struct usb_endpoint_descriptor* ep;
1425         struct usb_ms_endpoint_descriptor* ms_ep;
1426         int i, epidx;
1427
1428         intf = umidi->iface;
1429         if (!intf)
1430                 return -ENXIO;
1431         hostif = &intf->altsetting[0];
1432         intfd = get_iface_desc(hostif);
1433         ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1434         if (hostif->extralen >= 7 &&
1435             ms_header->bLength >= 7 &&
1436             ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1437             ms_header->bDescriptorSubtype == HEADER)
1438                 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1439                             ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1440         else
1441                 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1442
1443         epidx = 0;
1444         for (i = 0; i < intfd->bNumEndpoints; ++i) {
1445                 hostep = &hostif->endpoint[i];
1446                 ep = get_ep_desc(hostep);
1447                 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
1448                         continue;
1449                 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1450                 if (hostep->extralen < 4 ||
1451                     ms_ep->bLength < 4 ||
1452                     ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1453                     ms_ep->bDescriptorSubtype != MS_GENERAL)
1454                         continue;
1455                 if (usb_endpoint_dir_out(ep)) {
1456                         if (endpoints[epidx].out_ep) {
1457                                 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1458                                         snd_printk(KERN_WARNING "too many endpoints\n");
1459                                         break;
1460                                 }
1461                         }
1462                         endpoints[epidx].out_ep = usb_endpoint_num(ep);
1463                         if (usb_endpoint_xfer_int(ep))
1464                                 endpoints[epidx].out_interval = ep->bInterval;
1465                         else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1466                                 /*
1467                                  * Low speed bulk transfers don't exist, so
1468                                  * force interrupt transfers for devices like
1469                                  * ESI MIDI Mate that try to use them anyway.
1470                                  */
1471                                 endpoints[epidx].out_interval = 1;
1472                         endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1473                         snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1474                                     ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1475                 } else {
1476                         if (endpoints[epidx].in_ep) {
1477                                 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1478                                         snd_printk(KERN_WARNING "too many endpoints\n");
1479                                         break;
1480                                 }
1481                         }
1482                         endpoints[epidx].in_ep = usb_endpoint_num(ep);
1483                         if (usb_endpoint_xfer_int(ep))
1484                                 endpoints[epidx].in_interval = ep->bInterval;
1485                         else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1486                                 endpoints[epidx].in_interval = 1;
1487                         endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1488                         snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1489                                     ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1490                 }
1491         }
1492         return 0;
1493 }
1494
1495 /*
1496  * On Roland devices, use the second alternate setting to be able to use
1497  * the interrupt input endpoint.
1498  */
1499 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1500 {
1501         struct usb_interface* intf;
1502         struct usb_host_interface *hostif;
1503         struct usb_interface_descriptor* intfd;
1504
1505         intf = umidi->iface;
1506         if (!intf || intf->num_altsetting != 2)
1507                 return;
1508
1509         hostif = &intf->altsetting[1];
1510         intfd = get_iface_desc(hostif);
1511         if (intfd->bNumEndpoints != 2 ||
1512             (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1513             (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1514                 return;
1515
1516         snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1517                     intfd->bAlternateSetting);
1518         usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1519                           intfd->bAlternateSetting);
1520 }
1521
1522 /*
1523  * Try to find any usable endpoints in the interface.
1524  */
1525 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1526                                         struct snd_usb_midi_endpoint_info* endpoint,
1527                                         int max_endpoints)
1528 {
1529         struct usb_interface* intf;
1530         struct usb_host_interface *hostif;
1531         struct usb_interface_descriptor* intfd;
1532         struct usb_endpoint_descriptor* epd;
1533         int i, out_eps = 0, in_eps = 0;
1534
1535         if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
1536                 snd_usbmidi_switch_roland_altsetting(umidi);
1537
1538         if (endpoint[0].out_ep || endpoint[0].in_ep)
1539                 return 0;       
1540
1541         intf = umidi->iface;
1542         if (!intf || intf->num_altsetting < 1)
1543                 return -ENOENT;
1544         hostif = intf->cur_altsetting;
1545         intfd = get_iface_desc(hostif);
1546
1547         for (i = 0; i < intfd->bNumEndpoints; ++i) {
1548                 epd = get_endpoint(hostif, i);
1549                 if (!usb_endpoint_xfer_bulk(epd) &&
1550                     !usb_endpoint_xfer_int(epd))
1551                         continue;
1552                 if (out_eps < max_endpoints &&
1553                     usb_endpoint_dir_out(epd)) {
1554                         endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1555                         if (usb_endpoint_xfer_int(epd))
1556                                 endpoint[out_eps].out_interval = epd->bInterval;
1557                         ++out_eps;
1558                 }
1559                 if (in_eps < max_endpoints &&
1560                     usb_endpoint_dir_in(epd)) {
1561                         endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1562                         if (usb_endpoint_xfer_int(epd))
1563                                 endpoint[in_eps].in_interval = epd->bInterval;
1564                         ++in_eps;
1565                 }
1566         }
1567         return (out_eps || in_eps) ? 0 : -ENOENT;
1568 }
1569
1570 /*
1571  * Detects the endpoints for one-port-per-endpoint protocols.
1572  */
1573 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1574                                                  struct snd_usb_midi_endpoint_info* endpoints)
1575 {
1576         int err, i;
1577         
1578         err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1579         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1580                 if (endpoints[i].out_ep)
1581                         endpoints[i].out_cables = 0x0001;
1582                 if (endpoints[i].in_ep)
1583                         endpoints[i].in_cables = 0x0001;
1584         }
1585         return err;
1586 }
1587
1588 /*
1589  * Detects the endpoints and ports of Yamaha devices.
1590  */
1591 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1592                                      struct snd_usb_midi_endpoint_info* endpoint)
1593 {
1594         struct usb_interface* intf;
1595         struct usb_host_interface *hostif;
1596         struct usb_interface_descriptor* intfd;
1597         uint8_t* cs_desc;
1598
1599         intf = umidi->iface;
1600         if (!intf)
1601                 return -ENOENT;
1602         hostif = intf->altsetting;
1603         intfd = get_iface_desc(hostif);
1604         if (intfd->bNumEndpoints < 1)
1605                 return -ENOENT;
1606
1607         /*
1608          * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1609          * necessarily with any useful contents.  So simply count 'em.
1610          */
1611         for (cs_desc = hostif->extra;
1612              cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1613              cs_desc += cs_desc[0]) {
1614                 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1615                         if (cs_desc[2] == MIDI_IN_JACK)
1616                                 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1617                         else if (cs_desc[2] == MIDI_OUT_JACK)
1618                                 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1619                 }
1620         }
1621         if (!endpoint->in_cables && !endpoint->out_cables)
1622                 return -ENOENT;
1623
1624         return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1625 }
1626
1627 /*
1628  * Creates the endpoints and their ports for Midiman devices.
1629  */
1630 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1631                                                 struct snd_usb_midi_endpoint_info* endpoint)
1632 {
1633         struct snd_usb_midi_endpoint_info ep_info;
1634         struct usb_interface* intf;
1635         struct usb_host_interface *hostif;
1636         struct usb_interface_descriptor* intfd;
1637         struct usb_endpoint_descriptor* epd;
1638         int cable, err;
1639
1640         intf = umidi->iface;
1641         if (!intf)
1642                 return -ENOENT;
1643         hostif = intf->altsetting;
1644         intfd = get_iface_desc(hostif);
1645         /*
1646          * The various MidiSport devices have more or less random endpoint
1647          * numbers, so we have to identify the endpoints by their index in
1648          * the descriptor array, like the driver for that other OS does.
1649          *
1650          * There is one interrupt input endpoint for all input ports, one
1651          * bulk output endpoint for even-numbered ports, and one for odd-
1652          * numbered ports.  Both bulk output endpoints have corresponding
1653          * input bulk endpoints (at indices 1 and 3) which aren't used.
1654          */
1655         if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1656                 snd_printdd(KERN_ERR "not enough endpoints\n");
1657                 return -ENOENT;
1658         }
1659
1660         epd = get_endpoint(hostif, 0);
1661         if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
1662                 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1663                 return -ENXIO;
1664         }
1665         epd = get_endpoint(hostif, 2);
1666         if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
1667                 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1668                 return -ENXIO;
1669         }
1670         if (endpoint->out_cables > 0x0001) {
1671                 epd = get_endpoint(hostif, 4);
1672                 if (!usb_endpoint_dir_out(epd) ||
1673                     !usb_endpoint_xfer_bulk(epd)) {
1674                         snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1675                         return -ENXIO;
1676                 }
1677         }
1678
1679         ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1680         ep_info.out_interval = 0;
1681         ep_info.out_cables = endpoint->out_cables & 0x5555;
1682         err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1683         if (err < 0)
1684                 return err;
1685
1686         ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1687         ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1688         ep_info.in_cables = endpoint->in_cables;
1689         err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1690         if (err < 0)
1691                 return err;
1692
1693         if (endpoint->out_cables > 0x0001) {
1694                 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1695                 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1696                 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1697                 if (err < 0)
1698                         return err;
1699         }
1700
1701         for (cable = 0; cable < 0x10; ++cable) {
1702                 if (endpoint->out_cables & (1 << cable))
1703                         snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1704                                                    &umidi->endpoints[cable & 1].out->ports[cable].substream);
1705                 if (endpoint->in_cables & (1 << cable))
1706                         snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1707                                                    &umidi->endpoints[0].in->ports[cable].substream);
1708         }
1709         return 0;
1710 }
1711
1712 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1713         .get_port_info = snd_usbmidi_get_port_info,
1714 };
1715
1716 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
1717                                       int out_ports, int in_ports)
1718 {
1719         struct snd_rawmidi *rmidi;
1720         int err;
1721
1722         err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1723                               umidi->chip->next_midi_device++,
1724                               out_ports, in_ports, &rmidi);
1725         if (err < 0)
1726                 return err;
1727         strcpy(rmidi->name, umidi->chip->card->shortname);
1728         rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1729                             SNDRV_RAWMIDI_INFO_INPUT |
1730                             SNDRV_RAWMIDI_INFO_DUPLEX;
1731         rmidi->ops = &snd_usbmidi_ops;
1732         rmidi->private_data = umidi;
1733         rmidi->private_free = snd_usbmidi_rawmidi_free;
1734         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1735         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1736
1737         umidi->rmidi = rmidi;
1738         return 0;
1739 }
1740
1741 /*
1742  * Temporarily stop input.
1743  */
1744 void snd_usbmidi_input_stop(struct list_head* p)
1745 {
1746         struct snd_usb_midi* umidi;
1747         unsigned int i, j;
1748
1749         umidi = list_entry(p, struct snd_usb_midi, list);
1750         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1751                 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1752                 if (ep->in)
1753                         for (j = 0; j < INPUT_URBS; ++j)
1754                                 usb_kill_urb(ep->in->urbs[j]);
1755         }
1756 }
1757
1758 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
1759 {
1760         unsigned int i;
1761
1762         if (!ep)
1763                 return;
1764         for (i = 0; i < INPUT_URBS; ++i) {
1765                 struct urb* urb = ep->urbs[i];
1766                 urb->dev = ep->umidi->chip->dev;
1767                 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1768         }
1769 }
1770
1771 /*
1772  * Resume input after a call to snd_usbmidi_input_stop().
1773  */
1774 void snd_usbmidi_input_start(struct list_head* p)
1775 {
1776         struct snd_usb_midi* umidi;
1777         int i;
1778
1779         umidi = list_entry(p, struct snd_usb_midi, list);
1780         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1781                 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1782 }
1783
1784 /*
1785  * Creates and registers everything needed for a MIDI streaming interface.
1786  */
1787 int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
1788                                   struct usb_interface* iface,
1789                                   const struct snd_usb_audio_quirk* quirk)
1790 {
1791         struct snd_usb_midi* umidi;
1792         struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
1793         int out_ports, in_ports;
1794         int i, err;
1795
1796         umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
1797         if (!umidi)
1798                 return -ENOMEM;
1799         umidi->chip = chip;
1800         umidi->iface = iface;
1801         umidi->quirk = quirk;
1802         umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
1803         init_timer(&umidi->error_timer);
1804         spin_lock_init(&umidi->disc_lock);
1805         umidi->error_timer.function = snd_usbmidi_error_timer;
1806         umidi->error_timer.data = (unsigned long)umidi;
1807
1808         /* detect the endpoint(s) to use */
1809         memset(endpoints, 0, sizeof(endpoints));
1810         switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1811         case QUIRK_MIDI_STANDARD_INTERFACE:
1812                 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1813                 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1814                         umidi->usb_protocol_ops =
1815                                 &snd_usbmidi_maudio_broken_running_status_ops;
1816                 break;
1817         case QUIRK_MIDI_US122L:
1818                 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1819                 /* fall through */
1820         case QUIRK_MIDI_FIXED_ENDPOINT:
1821                 memcpy(&endpoints[0], quirk->data,
1822                        sizeof(struct snd_usb_midi_endpoint_info));
1823                 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1824                 break;
1825         case QUIRK_MIDI_YAMAHA:
1826                 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1827                 break;
1828         case QUIRK_MIDI_MIDIMAN:
1829                 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1830                 memcpy(&endpoints[0], quirk->data,
1831                        sizeof(struct snd_usb_midi_endpoint_info));
1832                 err = 0;
1833                 break;
1834         case QUIRK_MIDI_NOVATION:
1835                 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1836                 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1837                 break;
1838         case QUIRK_MIDI_FASTLANE:
1839                 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1840                 /*
1841                  * Interface 1 contains isochronous endpoints, but with the same
1842                  * numbers as in interface 0.  Since it is interface 1 that the
1843                  * USB core has most recently seen, these descriptors are now
1844                  * associated with the endpoint numbers.  This will foul up our
1845                  * attempts to submit bulk/interrupt URBs to the endpoints in
1846                  * interface 0, so we have to make sure that the USB core looks
1847                  * again at interface 0 by calling usb_set_interface() on it.
1848                  */
1849                 usb_set_interface(umidi->chip->dev, 0, 0);
1850                 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1851                 break;
1852         case QUIRK_MIDI_EMAGIC:
1853                 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1854                 memcpy(&endpoints[0], quirk->data,
1855                        sizeof(struct snd_usb_midi_endpoint_info));
1856                 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1857                 break;
1858         case QUIRK_MIDI_CME:
1859                 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
1860                 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1861                 break;
1862         default:
1863                 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1864                 err = -ENXIO;
1865                 break;
1866         }
1867         if (err < 0) {
1868                 kfree(umidi);
1869                 return err;
1870         }
1871
1872         /* create rawmidi device */
1873         out_ports = 0;
1874         in_ports = 0;
1875         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1876                 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1877                 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1878         }
1879         err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1880         if (err < 0) {
1881                 kfree(umidi);
1882                 return err;
1883         }
1884
1885         /* create endpoint/port structures */
1886         if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1887                 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1888         else
1889                 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1890         if (err < 0) {
1891                 snd_usbmidi_free(umidi);
1892                 return err;
1893         }
1894
1895         list_add(&umidi->list, &umidi->chip->midi_list);
1896
1897         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1898                 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1899         return 0;
1900 }
1901
1902 EXPORT_SYMBOL(snd_usb_create_midi_interface);
1903 EXPORT_SYMBOL(snd_usbmidi_input_stop);
1904 EXPORT_SYMBOL(snd_usbmidi_input_start);
1905 EXPORT_SYMBOL(snd_usbmidi_disconnect);