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