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