ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / usb / usbmidi.c
1 /*
2  * usbmidi.c - ALSA USB MIDI driver
3  *
4  * Copyright (c) 2002-2004 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/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/usb.h>
47 #include <sound/core.h>
48 #include <sound/minors.h>
49 #include <sound/rawmidi.h>
50 #include "usbaudio.h"
51
52 struct usb_ms_header_descriptor {
53         __u8  bLength;
54         __u8  bDescriptorType;
55         __u8  bDescriptorSubtype;
56         __u8  bcdMSC[2];
57         __u16 wTotalLength;
58 } __attribute__ ((packed));
59
60 struct usb_ms_endpoint_descriptor {
61         __u8  bLength;
62         __u8  bDescriptorType;
63         __u8  bDescriptorSubtype;
64         __u8  bNumEmbMIDIJack;
65         __u8  baAssocJackID[0];
66 } __attribute__ ((packed));
67
68 typedef struct snd_usb_midi snd_usb_midi_t;
69 typedef struct snd_usb_midi_endpoint snd_usb_midi_endpoint_t;
70 typedef struct snd_usb_midi_out_endpoint snd_usb_midi_out_endpoint_t;
71 typedef struct snd_usb_midi_in_endpoint snd_usb_midi_in_endpoint_t;
72 typedef struct usbmidi_out_port usbmidi_out_port_t;
73 typedef struct usbmidi_in_port usbmidi_in_port_t;
74
75 struct snd_usb_midi {
76         snd_usb_audio_t *chip;
77         struct usb_interface *iface;
78         const snd_usb_audio_quirk_t *quirk;
79         snd_rawmidi_t* rmidi;
80         struct list_head list;
81
82         struct snd_usb_midi_endpoint {
83                 snd_usb_midi_out_endpoint_t *out;
84                 snd_usb_midi_in_endpoint_t *in;
85         } endpoints[MIDI_MAX_ENDPOINTS];
86 };
87
88 struct snd_usb_midi_out_endpoint {
89         snd_usb_midi_t* umidi;
90         struct urb* urb;
91         int max_transfer;               /* size of urb buffer */
92         struct tasklet_struct tasklet;
93
94         spinlock_t buffer_lock;
95
96         struct usbmidi_out_port {
97                 snd_usb_midi_out_endpoint_t* ep;
98                 snd_rawmidi_substream_t* substream;
99                 int active;
100                 uint8_t cable;          /* cable number << 4 */
101                 uint8_t state;
102 #define STATE_UNKNOWN   0
103 #define STATE_1PARAM    1
104 #define STATE_2PARAM_1  2
105 #define STATE_2PARAM_2  3
106 #define STATE_SYSEX_0   4
107 #define STATE_SYSEX_1   5
108 #define STATE_SYSEX_2   6
109                 uint8_t data[2];
110         } ports[0x10];
111 };
112
113 struct snd_usb_midi_in_endpoint {
114         snd_usb_midi_t* umidi;
115         struct urb* urb;
116         struct usbmidi_in_port {
117                 snd_rawmidi_substream_t* substream;
118         } ports[0x10];
119 };
120
121 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep);
122
123 static const uint8_t snd_usbmidi_cin_length[] = {
124         0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
125 };
126
127 /*
128  * Submits the URB, with error handling.
129  */
130 static int snd_usbmidi_submit_urb(struct urb* urb, int flags)
131 {
132         int err = usb_submit_urb(urb, flags);
133         if (err < 0 && err != -ENODEV)
134                 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
135         return err;
136 }
137
138 /*
139  * Error handling for URB completion functions.
140  */
141 static int snd_usbmidi_urb_error(int status)
142 {
143         if (status == -ENOENT)
144                 return status; /* killed */
145         if (status == -EILSEQ ||
146             status == -ECONNRESET ||
147             status == -ETIMEDOUT)
148                 return -ENODEV; /* device removed/shutdown */
149         snd_printk(KERN_ERR "urb status %d\n", status);
150         return 0; /* continue */
151 }
152
153 /*
154  * Receives a USB MIDI packet.
155  */
156 static void snd_usbmidi_input_packet(snd_usb_midi_in_endpoint_t* ep,
157                                      uint8_t packet[4])
158 {
159         int cable = packet[0] >> 4;
160         usbmidi_in_port_t* port = &ep->ports[cable];
161
162         if (!port->substream) {
163                 snd_printd("unexpected port %d!\n", cable);
164                 return;
165         }
166         if (!port->substream->runtime ||
167             !port->substream->runtime->trigger)
168                 return;
169         snd_rawmidi_receive(port->substream, &packet[1],
170                             snd_usbmidi_cin_length[packet[0] & 0x0f]);
171 }
172
173 /*
174  * Processes the data read from the device.
175  */
176 static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs)
177 {
178         snd_usb_midi_in_endpoint_t* ep = snd_magic_cast(snd_usb_midi_in_endpoint_t, urb->context, return);
179
180         if (urb->status == 0) {
181                 uint8_t* buffer = (uint8_t*)ep->urb->transfer_buffer;
182                 int i;
183
184                 for (i = 0; i + 4 <= urb->actual_length; i += 4)
185                         if (buffer[i] != 0)
186                                 snd_usbmidi_input_packet(ep, &buffer[i]);
187         } else {
188                 if (snd_usbmidi_urb_error(urb->status) < 0)
189                         return;
190         }
191
192         if (usb_pipe_needs_resubmit(urb->pipe)) {
193                 urb->dev = ep->umidi->chip->dev;
194                 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
195         }
196 }
197
198 /*
199  * Converts the data read from a Midiman device to standard USB MIDI packets.
200  */
201 static void snd_usbmidi_in_midiman_complete(struct urb* urb, struct pt_regs *regs)
202 {
203         if (urb->status == 0) {
204                 uint8_t* buffer = (uint8_t*)urb->transfer_buffer;
205                 int i;
206
207                 for (i = 0; i + 4 <= urb->actual_length; i += 4) {
208                         if (buffer[i + 3] != 0) {
209                                 /*
210                                  * snd_usbmidi_input_packet() doesn't check the
211                                  * contents of the message, so we simply use
212                                  * some random CIN with the desired length.
213                                  */
214                                 static const uint8_t cin[4] = {
215                                         0x0, 0xf, 0x2, 0x3
216                                 };
217                                 uint8_t ctl = buffer[i + 3];
218                                 buffer[i + 3] = buffer[i + 2];
219                                 buffer[i + 2] = buffer[i + 1];
220                                 buffer[i + 1] = buffer[i + 0];
221                                 buffer[i + 0] = (ctl & 0xf0) | cin[ctl & 3];
222                         } else {
223                                 buffer[i + 0] = 0;
224                         }
225                 }
226         }
227         snd_usbmidi_in_urb_complete(urb, regs);
228 }
229
230 static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs)
231 {
232         snd_usb_midi_out_endpoint_t* ep = snd_magic_cast(snd_usb_midi_out_endpoint_t, urb->context, return);
233
234         if (urb->status < 0) {
235                 if (snd_usbmidi_urb_error(urb->status) < 0)
236                         return;
237         }
238         snd_usbmidi_do_output(ep);
239 }
240
241 /*
242  * Converts standard USB MIDI packets to what Midman devices expect.
243  */
244 static void snd_usbmidi_convert_to_midiman(struct urb* urb)
245 {
246         uint8_t* buffer = (uint8_t*)urb->transfer_buffer;
247         int i;
248
249         for (i = 0; i + 4 <= urb->transfer_buffer_length; i += 4) {
250                 uint8_t cin = buffer[i];
251                 buffer[i + 0] = buffer[i + 1];
252                 buffer[i + 1] = buffer[i + 2];
253                 buffer[i + 2] = buffer[i + 3];
254                 buffer[i + 3] = (cin & 0xf0) | snd_usbmidi_cin_length[cin & 0x0f];
255         }
256 }
257
258 /*
259  * Adds one USB MIDI packet to the output buffer.
260  */
261 static inline void output_packet(struct urb* urb,
262                                  uint8_t p0, uint8_t p1, uint8_t p2, uint8_t p3)
263 {
264
265         uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
266         buf[0] = p0;
267         buf[1] = p1;
268         buf[2] = p2;
269         buf[3] = p3;
270         urb->transfer_buffer_length += 4;
271 }
272
273 /*
274  * Converts MIDI commands to USB MIDI packets.
275  */
276 static void snd_usbmidi_transmit_byte(usbmidi_out_port_t* port,
277                                       uint8_t b, struct urb* urb)
278 {
279         uint8_t p0 = port->cable;
280
281         if (b >= 0xf8) {
282                 output_packet(urb, p0 | 0x0f, b, 0, 0);
283         } else if (b >= 0xf0) {
284                 switch (b) {
285                 case 0xf0:
286                         port->data[0] = b;
287                         port->state = STATE_SYSEX_1;
288                         break;
289                 case 0xf1:
290                 case 0xf3:
291                         port->data[0] = b;
292                         port->state = STATE_1PARAM;
293                         break;
294                 case 0xf2:
295                         port->data[0] = b;
296                         port->state = STATE_2PARAM_1;
297                         break;
298                 case 0xf4:
299                 case 0xf5:
300                         port->state = STATE_UNKNOWN;
301                         break;
302                 case 0xf6:
303                         output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
304                         port->state = STATE_UNKNOWN;
305                         break;
306                 case 0xf7:
307                         switch (port->state) {
308                         case STATE_SYSEX_0:
309                                 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
310                                 break;
311                         case STATE_SYSEX_1:
312                                 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
313                                 break;
314                         case STATE_SYSEX_2:
315                                 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
316                                 break;
317                         }
318                         port->state = STATE_UNKNOWN;
319                         break;
320                 }
321         } else if (b >= 0x80) {
322                 port->data[0] = b;
323                 if (b >= 0xc0 && b <= 0xdf)
324                         port->state = STATE_1PARAM;
325                 else
326                         port->state = STATE_2PARAM_1;
327         } else { /* b < 0x80 */
328                 switch (port->state) {
329                 case STATE_1PARAM:
330                         if (port->data[0] < 0xf0) {
331                                 p0 |= port->data[0] >> 4;
332                         } else {
333                                 p0 |= 0x02;
334                                 port->state = STATE_UNKNOWN;
335                         }
336                         output_packet(urb, p0, port->data[0], b, 0);
337                         break;
338                 case STATE_2PARAM_1:
339                         port->data[1] = b;
340                         port->state = STATE_2PARAM_2;
341                         break;
342                 case STATE_2PARAM_2:
343                         if (port->data[0] < 0xf0) {
344                                 p0 |= port->data[0] >> 4;
345                                 port->state = STATE_2PARAM_1;
346                         } else {
347                                 p0 |= 0x03;
348                                 port->state = STATE_UNKNOWN;
349                         }
350                         output_packet(urb, p0, port->data[0], port->data[1], b);
351                         break;
352                 case STATE_SYSEX_0:
353                         port->data[0] = b;
354                         port->state = STATE_SYSEX_1;
355                         break;
356                 case STATE_SYSEX_1:
357                         port->data[1] = b;
358                         port->state = STATE_SYSEX_2;
359                         break;
360                 case STATE_SYSEX_2:
361                         output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
362                         port->state = STATE_SYSEX_0;
363                         break;
364                 }
365         }
366 }
367
368 /*
369  * Moves data from one substream buffer to the URB transfer buffer.
370  */
371 static void snd_usbmidi_transmit(snd_usb_midi_out_endpoint_t* ep, int port_idx)
372 {
373         struct urb* urb = ep->urb;
374         usbmidi_out_port_t* port = &ep->ports[port_idx];
375
376         while (urb->transfer_buffer_length < ep->max_transfer) {
377                 uint8_t b;
378                 if (snd_rawmidi_transmit_peek(port->substream, &b, 1) != 1) {
379                         port->active = 0;
380                         break;
381                 }
382                 snd_usbmidi_transmit_byte(port, b, urb);
383                 snd_rawmidi_transmit_ack(port->substream, 1);
384         }
385 }
386
387 /*
388  * This is called when some data should be transferred to the device
389  * (from one or more substreams).
390  */
391 static void snd_usbmidi_do_output(snd_usb_midi_out_endpoint_t* ep)
392 {
393         int p;
394         struct urb* urb = ep->urb;
395         unsigned long flags;
396         
397         spin_lock_irqsave(&ep->buffer_lock, flags);
398         if (urb->status == -EINPROGRESS || ep->umidi->chip->shutdown) {
399                 spin_unlock_irqrestore(&ep->buffer_lock, flags);
400                 return;
401         }
402
403         urb->transfer_buffer_length = 0;
404         for (p= 0; p < 0x10; ++p)
405                 if (ep->ports[p].active)
406                         snd_usbmidi_transmit(ep, p);
407
408         if (urb->transfer_buffer_length > 0) {
409                 if (ep->umidi->quirk && ep->umidi->quirk->type == QUIRK_MIDI_MIDIMAN)
410                         snd_usbmidi_convert_to_midiman(urb);
411
412                 urb->dev = ep->umidi->chip->dev;
413                 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
414         }
415         spin_unlock_irqrestore(&ep->buffer_lock, flags);
416 }
417
418 static void snd_usbmidi_out_tasklet(unsigned long data)
419 {
420         snd_usb_midi_out_endpoint_t* ep = snd_magic_cast(snd_usb_midi_out_endpoint_t, (void*)data, return);
421         
422         snd_usbmidi_do_output(ep);
423 }
424
425 static int snd_usbmidi_output_open(snd_rawmidi_substream_t* substream)
426 {
427         snd_usb_midi_t* umidi = snd_magic_cast(snd_usb_midi_t, substream->rmidi->private_data, return -ENXIO);
428         usbmidi_out_port_t* port = NULL;
429         int i, j;
430
431         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
432                 if (umidi->endpoints[i].out)
433                         for (j = 0; j < 0x10; ++j)
434                                 if (umidi->endpoints[i].out->ports[j].substream == substream) {
435                                         port = &umidi->endpoints[i].out->ports[j];
436                                         break;
437                                 }
438         if (!port) {
439                 snd_BUG();
440                 return -ENXIO;
441         }
442         substream->runtime->private_data = port;
443         port->state = STATE_UNKNOWN;
444         return 0;
445 }
446
447 static int snd_usbmidi_output_close(snd_rawmidi_substream_t* substream)
448 {
449         return 0;
450 }
451
452 static void snd_usbmidi_output_trigger(snd_rawmidi_substream_t* substream, int up)
453 {
454         usbmidi_out_port_t* port = (usbmidi_out_port_t*)substream->runtime->private_data;
455
456         port->active = up;
457         if (up) {
458                 if (port->ep->umidi->chip->shutdown) {
459                         /* gobble up remaining bytes to prevent wait in
460                          * snd_rawmidi_drain_output */
461                         while (!snd_rawmidi_transmit_empty(substream))
462                                 snd_rawmidi_transmit_ack(substream, 1);
463                         return;
464                 }
465                 tasklet_hi_schedule(&port->ep->tasklet);
466         }
467 }
468
469 static int snd_usbmidi_input_open(snd_rawmidi_substream_t* substream)
470 {
471         return 0;
472 }
473
474 static int snd_usbmidi_input_close(snd_rawmidi_substream_t* substream)
475 {
476         return 0;
477 }
478
479 static void snd_usbmidi_input_trigger(snd_rawmidi_substream_t* substream, int up)
480 {
481 }
482
483 static snd_rawmidi_ops_t snd_usbmidi_output_ops = {
484         .open = snd_usbmidi_output_open,
485         .close = snd_usbmidi_output_close,
486         .trigger = snd_usbmidi_output_trigger,
487 };
488
489 static snd_rawmidi_ops_t snd_usbmidi_input_ops = {
490         .open = snd_usbmidi_input_open,
491         .close = snd_usbmidi_input_close,
492         .trigger = snd_usbmidi_input_trigger
493 };
494
495 /*
496  * Frees an input endpoint.
497  * May be called when ep hasn't been initialized completely.
498  */
499 static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t* ep)
500 {
501         if (ep->urb) {
502                 if (ep->urb->transfer_buffer)
503                         kfree(ep->urb->transfer_buffer);
504                 usb_free_urb(ep->urb);
505         }
506         snd_magic_kfree(ep);
507 }
508
509 /*
510  * For Roland devices, use the alternate setting which uses interrupt
511  * transfers for input.
512  */
513 static struct usb_endpoint_descriptor* snd_usbmidi_get_int_epd(snd_usb_midi_t* umidi)
514 {
515         struct usb_interface* intf;
516         struct usb_host_interface *hostif;
517         struct usb_interface_descriptor* intfd;
518
519         if (umidi->chip->dev->descriptor.idVendor != 0x0582)
520                 return NULL;
521         intf = umidi->iface;
522         if (!intf || intf->num_altsetting != 2)
523                 return NULL;
524
525         hostif = &intf->altsetting[0];
526         intfd = get_iface_desc(hostif);
527         if (intfd->bNumEndpoints != 2 ||
528             (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
529             (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
530                 return NULL;
531
532         hostif = &intf->altsetting[1];
533         intfd = get_iface_desc(hostif);
534         if (intfd->bNumEndpoints != 2 ||
535             (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
536             (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
537                 return NULL;
538
539         snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
540                     intfd->bAlternateSetting);
541         usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
542                           intfd->bAlternateSetting);
543         return get_endpoint(hostif, 1);
544 }
545
546 static struct usb_endpoint_descriptor* snd_usbmidi_get_midiman_int_epd(snd_usb_midi_t* umidi)
547 {
548         struct usb_interface* intf = umidi->iface;
549         struct usb_host_interface *hostif;
550         struct usb_interface_descriptor *intfd;
551         if (!intf)
552                 return NULL;
553         hostif = &intf->altsetting[0];
554         intfd = get_iface_desc(hostif);
555         if (intfd->bNumEndpoints < 1)
556                 return NULL;
557         return get_endpoint(hostif, 0);
558 }
559
560 /*
561  * Creates an input endpoint.
562  */
563 static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t* umidi,
564                                           snd_usb_midi_endpoint_info_t* ep_info,
565                                           snd_usb_midi_endpoint_t* rep)
566 {
567         snd_usb_midi_in_endpoint_t* ep;
568         struct usb_endpoint_descriptor* int_epd;
569         void* buffer;
570         unsigned int pipe;
571         int length;
572
573         rep->in = NULL;
574         ep = snd_magic_kcalloc(snd_usb_midi_in_endpoint_t, 0, GFP_KERNEL);
575         if (!ep)
576                 return -ENOMEM;
577         ep->umidi = umidi;
578
579         if (umidi->quirk && umidi->quirk->type == QUIRK_MIDI_MIDIMAN)
580                 int_epd = snd_usbmidi_get_midiman_int_epd(umidi);
581         else
582                 int_epd = snd_usbmidi_get_int_epd(umidi);
583
584         ep->urb = usb_alloc_urb(0, GFP_KERNEL);
585         if (!ep->urb) {
586                 snd_usbmidi_in_endpoint_delete(ep);
587                 return -ENOMEM;
588         }
589         if (int_epd)
590                 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
591         else
592                 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
593         length = usb_maxpacket(umidi->chip->dev, pipe, 0);
594         buffer = kmalloc(length, GFP_KERNEL);
595         if (!buffer) {
596                 snd_usbmidi_in_endpoint_delete(ep);
597                 return -ENOMEM;
598         }
599         if (int_epd)
600                 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer, length,
601                                  snd_usb_complete_callback(snd_usbmidi_in_urb_complete),
602                                  ep, int_epd->bInterval);
603         else
604                 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, length,
605                                   snd_usb_complete_callback(snd_usbmidi_in_urb_complete),
606                                   ep);
607
608         rep->in = ep;
609         return 0;
610 }
611
612 static int snd_usbmidi_count_bits(uint16_t x)
613 {
614         int i, bits = 0;
615
616         for (i = 0; i < 16; ++i)
617                 bits += (x & (1 << i)) != 0;
618         return bits;
619 }
620
621 /*
622  * Frees an output endpoint.
623  * May be called when ep hasn't been initialized completely.
624  */
625 static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t* ep)
626 {
627         if (ep->tasklet.func)
628                 tasklet_kill(&ep->tasklet);
629         if (ep->urb) {
630                 if (ep->urb->transfer_buffer)
631                         kfree(ep->urb->transfer_buffer);
632                 usb_free_urb(ep->urb);
633         }
634         snd_magic_kfree(ep);
635 }
636
637 /*
638  * Creates an output endpoint, and initializes output ports.
639  */
640 static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t* umidi,
641                                            snd_usb_midi_endpoint_info_t* ep_info,
642                                            snd_usb_midi_endpoint_t* rep)
643 {
644         snd_usb_midi_out_endpoint_t* ep;
645         int i;
646         unsigned int pipe;
647         void* buffer;
648
649         rep->out = NULL;
650         ep = snd_magic_kcalloc(snd_usb_midi_out_endpoint_t, 0, GFP_KERNEL);
651         if (!ep)
652                 return -ENOMEM;
653         ep->umidi = umidi;
654
655         ep->urb = usb_alloc_urb(0, GFP_KERNEL);
656         if (!ep->urb) {
657                 snd_usbmidi_out_endpoint_delete(ep);
658                 return -ENOMEM;
659         }
660         pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
661         ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1) & ~3;
662         buffer = kmalloc(ep->max_transfer, GFP_KERNEL);
663         if (!buffer) {
664                 snd_usbmidi_out_endpoint_delete(ep);
665                 return -ENOMEM;
666         }
667         usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
668                           ep->max_transfer,
669                           snd_usb_complete_callback(snd_usbmidi_out_urb_complete), ep);
670
671         spin_lock_init(&ep->buffer_lock);
672         tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
673
674         for (i = 0; i < 0x10; ++i)
675                 if (ep_info->out_cables & (1 << i)) {
676                         ep->ports[i].ep = ep;
677                         ep->ports[i].cable = i << 4;
678                 }
679
680         rep->out = ep;
681         return 0;
682 }
683
684 /*
685  * Frees everything.
686  */
687 static void snd_usbmidi_free(snd_usb_midi_t* umidi)
688 {
689         int i;
690
691         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
692                 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
693                 if (ep->out)
694                         snd_usbmidi_out_endpoint_delete(ep->out);
695                 if (ep->in)
696                         snd_usbmidi_in_endpoint_delete(ep->in);
697         }
698         snd_magic_kfree(umidi);
699 }
700
701 /*
702  * Unlinks all URBs (must be done before the usb_device is deleted).
703  */
704 void snd_usbmidi_disconnect(struct list_head* p, struct usb_driver *driver)
705 {
706         snd_usb_midi_t* umidi;
707         int i;
708
709         umidi = list_entry(p, snd_usb_midi_t, list);
710         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
711                 snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i];
712                 if (ep->out && ep->out->urb)
713                         usb_unlink_urb(ep->out->urb);
714                 if (ep->in && ep->in->urb)
715                         usb_unlink_urb(ep->in->urb);
716         }
717 }
718
719 static void snd_usbmidi_rawmidi_free(snd_rawmidi_t* rmidi)
720 {
721         snd_usb_midi_t* umidi = snd_magic_cast(snd_usb_midi_t, rmidi->private_data, return);
722         snd_usbmidi_free(umidi);
723 }
724
725 static snd_rawmidi_substream_t* snd_usbmidi_find_substream(snd_usb_midi_t* umidi,
726                                                            int stream, int number)
727 {
728         struct list_head* list;
729
730         list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
731                 snd_rawmidi_substream_t* substream = list_entry(list, snd_rawmidi_substream_t, list);
732                 if (substream->number == number)
733                         return substream;
734         }
735         return NULL;
736 }
737
738 /*
739  * This list specifies names for ports that do not fit into the standard
740  * "(product) MIDI (n)" schema because they aren't external MIDI ports,
741  * such as internal control or synthesizer ports.
742  */
743 static struct {
744         __u16 vendor;
745         __u16 product;
746         int port;
747         const char *name_format;
748 } snd_usbmidi_port_names[] = {
749         /* Roland UA-100 */
750         {0x0582, 0x0000, 2, "%s Control"},
751         /* Roland SC-8850 */
752         {0x0582, 0x0003, 0, "%s Part A"},
753         {0x0582, 0x0003, 1, "%s Part B"},
754         {0x0582, 0x0003, 2, "%s Part C"},
755         {0x0582, 0x0003, 3, "%s Part D"},
756         {0x0582, 0x0003, 4, "%s MIDI 1"},
757         {0x0582, 0x0003, 5, "%s MIDI 2"},
758         /* Roland U-8 */
759         {0x0582, 0x0004, 0, "%s MIDI"},
760         {0x0582, 0x0004, 1, "%s Control"},
761         /* Roland SC-8820 */
762         {0x0582, 0x0007, 0, "%s Part A"},
763         {0x0582, 0x0007, 1, "%s Part B"},
764         {0x0582, 0x0007, 2, "%s MIDI"},
765         /* Roland SK-500 */
766         {0x0582, 0x000b, 0, "%s Part A"},
767         {0x0582, 0x000b, 1, "%s Part B"},
768         {0x0582, 0x000b, 2, "%s MIDI"},
769         /* Roland SC-D70 */
770         {0x0582, 0x000c, 0, "%s Part A"},
771         {0x0582, 0x000c, 1, "%s Part B"},
772         {0x0582, 0x000c, 2, "%s MIDI"},
773         /* Edirol UM-880 */
774         {0x0582, 0x0014, 8, "%s Control"},
775         /* Edirol SD-90 */
776         {0x0582, 0x0016, 0, "%s Part A"},
777         {0x0582, 0x0016, 1, "%s Part B"},
778         {0x0582, 0x0016, 2, "%s MIDI 1"},
779         {0x0582, 0x0016, 3, "%s MIDI 2"},
780         /* Edirol UM-550 */
781         {0x0582, 0x0023, 5, "%s Control"},
782         /* Edirol SD-20 */
783         {0x0582, 0x0027, 0, "%s Part A"},
784         {0x0582, 0x0027, 1, "%s Part B"},
785         {0x0582, 0x0027, 2, "%s MIDI"},
786         /* Edirol SD-80 */
787         {0x0582, 0x0029, 0, "%s Part A"},
788         {0x0582, 0x0029, 1, "%s Part B"},
789         {0x0582, 0x0029, 2, "%s MIDI 1"},
790         {0x0582, 0x0029, 3, "%s MIDI 2"},
791         /* Edirol UA-700 */
792         {0x0582, 0x002b, 0, "%s MIDI"},
793         {0x0582, 0x002b, 1, "%s Control"},
794         /* Roland VariOS */
795         {0x0582, 0x002f, 0, "%s MIDI"},
796         {0x0582, 0x002f, 1, "%s External MIDI"},
797         {0x0582, 0x002f, 2, "%s Sync"},
798         /* Edirol PCR */
799         {0x0582, 0x0033, 0, "%s MIDI"},
800         {0x0582, 0x0033, 1, "%s 1"},
801         {0x0582, 0x0033, 2, "%s 2"},
802         /* BOSS GS-10 */
803         {0x0582, 0x003b, 0, "%s MIDI"},
804         {0x0582, 0x003b, 1, "%s Control"},
805         /* Edirol UA-1000 */
806         {0x0582, 0x0044, 0, "%s MIDI"},
807         {0x0582, 0x0044, 1, "%s Control"},
808         /* Edirol UR-80 */
809         {0x0582, 0x0048, 0, "%s MIDI"},
810         {0x0582, 0x0048, 1, "%s 1"},
811         {0x0582, 0x0048, 2, "%s 2"},
812         /* Edirol PCR-A */
813         {0x0582, 0x004d, 0, "%s MIDI"},
814         {0x0582, 0x004d, 1, "%s 1"},
815         {0x0582, 0x004d, 2, "%s 2"},
816         /* M-Audio MidiSport 8x8 */
817         {0x0763, 0x1031, 8, "%s Control"},
818         {0x0763, 0x1033, 8, "%s Control"},
819 };
820
821 static void snd_usbmidi_init_substream(snd_usb_midi_t* umidi,
822                                        int stream, int number,
823                                        snd_rawmidi_substream_t** rsubstream)
824 {
825         int i;
826         __u16 vendor, product;
827         const char *name_format;
828
829         snd_rawmidi_substream_t* substream = snd_usbmidi_find_substream(umidi, stream, number);
830         if (!substream) {
831                 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
832                 return;
833         }
834
835         /* TODO: read port name from jack descriptor */
836         name_format = "%s MIDI %d";
837         vendor = umidi->chip->dev->descriptor.idVendor;
838         product = umidi->chip->dev->descriptor.idProduct;
839         for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_names); ++i) {
840                 if (snd_usbmidi_port_names[i].vendor == vendor &&
841                     snd_usbmidi_port_names[i].product == product &&
842                     snd_usbmidi_port_names[i].port == number) {
843                         name_format = snd_usbmidi_port_names[i].name_format;
844                         break;
845                 }
846         }
847         snprintf(substream->name, sizeof(substream->name),
848                  name_format, umidi->chip->card->shortname, number + 1);
849
850         *rsubstream = substream;
851 }
852
853 /*
854  * Creates the endpoints and their ports.
855  */
856 static int snd_usbmidi_create_endpoints(snd_usb_midi_t* umidi,
857                                         snd_usb_midi_endpoint_info_t* endpoints)
858 {
859         int i, j, err;
860         int out_ports = 0, in_ports = 0;
861
862         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
863                 if (endpoints[i].out_cables) {
864                         err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
865                                                               &umidi->endpoints[i]);
866                         if (err < 0)
867                                 return err;
868                 }
869                 if (endpoints[i].in_cables) {
870                         err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
871                                                              &umidi->endpoints[i]);
872                         if (err < 0)
873                                 return err;
874                 }
875
876                 for (j = 0; j < 0x10; ++j) {
877                         if (endpoints[i].out_cables & (1 << j)) {
878                                 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
879                                                            &umidi->endpoints[i].out->ports[j].substream);
880                                 ++out_ports;
881                         }
882                         if (endpoints[i].in_cables & (1 << j)) {
883                                 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
884                                                            &umidi->endpoints[i].in->ports[j].substream);
885                                 ++in_ports;
886                         }
887                 }
888         }
889         snd_printdd(KERN_INFO "created %d output and %d input ports\n",
890                     out_ports, in_ports);
891         return 0;
892 }
893
894 /*
895  * Returns MIDIStreaming device capabilities.
896  */
897 static int snd_usbmidi_get_ms_info(snd_usb_midi_t* umidi,
898                                    snd_usb_midi_endpoint_info_t* endpoints)
899 {
900         struct usb_interface* intf;
901         struct usb_host_interface *hostif;
902         struct usb_interface_descriptor* intfd;
903         struct usb_ms_header_descriptor* ms_header;
904         struct usb_host_endpoint *hostep;
905         struct usb_endpoint_descriptor* ep;
906         struct usb_ms_endpoint_descriptor* ms_ep;
907         int i, epidx;
908
909         intf = umidi->iface;
910         if (!intf)
911                 return -ENXIO;
912         hostif = &intf->altsetting[0];
913         intfd = get_iface_desc(hostif);
914         ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
915         if (hostif->extralen >= 7 &&
916             ms_header->bLength >= 7 &&
917             ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
918             ms_header->bDescriptorSubtype == HEADER)
919                 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
920                             ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
921         else
922                 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
923
924         epidx = 0;
925         for (i = 0; i < intfd->bNumEndpoints; ++i) {
926                 hostep = &hostif->endpoint[i];
927                 ep = get_ep_desc(hostep);
928                 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
929                         continue;
930                 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
931                 if (hostep->extralen < 4 ||
932                     ms_ep->bLength < 4 ||
933                     ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
934                     ms_ep->bDescriptorSubtype != MS_GENERAL)
935                         continue;
936                 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
937                         if (endpoints[epidx].out_ep) {
938                                 if (++epidx >= MIDI_MAX_ENDPOINTS) {
939                                         snd_printk(KERN_WARNING "too many endpoints\n");
940                                         break;
941                                 }
942                         }
943                         endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
944                         endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
945                         snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
946                                     ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
947                 } else {
948                         if (endpoints[epidx].in_ep) {
949                                 if (++epidx >= MIDI_MAX_ENDPOINTS) {
950                                         snd_printk(KERN_WARNING "too many endpoints\n");
951                                         break;
952                                 }
953                         }
954                         endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
955                         endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
956                         snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
957                                     ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
958                 }
959         }
960         return 0;
961 }
962
963 /*
964  * If the endpoints aren't specified, use the first bulk endpoints in the
965  * first alternate setting of the interface.
966  */
967 static int snd_usbmidi_detect_endpoint(snd_usb_midi_t* umidi, 
968                                        snd_usb_midi_endpoint_info_t* endpoint)
969 {
970         struct usb_interface* intf;
971         struct usb_host_interface *hostif;
972         struct usb_interface_descriptor* intfd;
973         struct usb_endpoint_descriptor* epd;
974         int i;
975
976         intf = umidi->iface;
977         if (!intf || intf->num_altsetting < 1)
978                 return -ENOENT;
979         hostif = intf->altsetting;
980         intfd = get_iface_desc(hostif);
981         if (intfd->bNumEndpoints < 1)
982                 return -ENOENT;
983
984         for (i = 0; i < intfd->bNumEndpoints; ++i) {
985                 epd = get_endpoint(hostif, i);
986                 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK)
987                         continue;
988                 if (!endpoint->out_ep && endpoint->out_cables &&
989                     (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
990                         endpoint->out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
991                 if (!endpoint->in_ep && endpoint->in_cables &&
992                     (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
993                         endpoint->in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
994         }
995         return 0;
996 }
997
998 /*
999  * Detects the endpoints and ports of Yamaha devices.
1000  */
1001 static int snd_usbmidi_detect_yamaha(snd_usb_midi_t* umidi, 
1002                                      snd_usb_midi_endpoint_info_t* endpoint)
1003 {
1004         struct usb_interface* intf;
1005         struct usb_host_interface *hostif;
1006         struct usb_interface_descriptor* intfd;
1007         uint8_t* cs_desc;
1008
1009         intf = umidi->iface;
1010         if (!intf)
1011                 return -ENOENT;
1012         hostif = intf->altsetting;
1013         intfd = get_iface_desc(hostif);
1014         if (intfd->bNumEndpoints < 1)
1015                 return -ENOENT;
1016
1017         /*
1018          * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1019          * necessarily with any useful contents.  So simply count 'em.
1020          */
1021         for (cs_desc = hostif->extra;
1022              cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1023              cs_desc += cs_desc[0]) {
1024                 if (cs_desc[1] == CS_AUDIO_INTERFACE) {
1025                         if (cs_desc[2] == MIDI_IN_JACK)
1026                                 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1027                         else if (cs_desc[2] == MIDI_OUT_JACK)
1028                                 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1029                 }
1030         }
1031         if (!endpoint->in_cables && !endpoint->out_cables)
1032                 return -ENOENT;
1033
1034         return snd_usbmidi_detect_endpoint(umidi, endpoint);
1035 }
1036
1037 /*
1038  * Creates the endpoints and their ports for Midiman devices.
1039  */
1040 static int snd_usbmidi_create_endpoints_midiman(snd_usb_midi_t* umidi,
1041                                                 snd_usb_midi_endpoint_info_t* endpoint)
1042 {
1043         snd_usb_midi_endpoint_info_t ep_info;
1044         struct usb_interface* intf;
1045         struct usb_host_interface *hostif;
1046         struct usb_interface_descriptor* intfd;
1047         struct usb_endpoint_descriptor* epd;
1048         int cable, err;
1049
1050         intf = umidi->iface;
1051         if (!intf)
1052                 return -ENOENT;
1053         hostif = intf->altsetting;
1054         intfd = get_iface_desc(hostif);
1055         /*
1056          * The various MidiSport devices have more or less random endpoint
1057          * numbers, so we have to identify the endpoints by their index in
1058          * the descriptor array, like the driver for that other OS does.
1059          *
1060          * There is one interrupt input endpoint for all input ports, one
1061          * bulk output endpoint for even-numbered ports, and one for odd-
1062          * numbered ports.  Both bulk output endpoints have corresponding
1063          * input bulk endpoints (at indices 1 and 3) which aren't used.
1064          */
1065         if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1066                 snd_printdd(KERN_ERR "not enough endpoints\n");
1067                 return -ENOENT;
1068         }
1069
1070         epd = get_endpoint(hostif, 0);
1071         if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1072             (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1073                 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1074                 return -ENXIO;
1075         }
1076         epd = get_endpoint(hostif, 2);
1077         if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1078             (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1079                 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1080                 return -ENXIO;
1081         }
1082         if (endpoint->out_cables > 0x0001) {
1083                 epd = get_endpoint(hostif, 4);
1084                 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1085                     (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1086                         snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1087                         return -ENXIO;
1088                 }
1089         }
1090
1091         ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1092         ep_info.out_cables = endpoint->out_cables & 0x5555;
1093         err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1094         if (err < 0)
1095                 return err;
1096
1097         ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1098         ep_info.in_cables = endpoint->in_cables;
1099         err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1100         if (err < 0)
1101                 return err;
1102         umidi->endpoints[0].in->urb->complete = snd_usb_complete_callback(snd_usbmidi_in_midiman_complete);
1103
1104         if (endpoint->out_cables > 0x0001) {
1105                 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1106                 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1107                 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1108                 if (err < 0)
1109                         return err;
1110         }
1111
1112         for (cable = 0; cable < 0x10; ++cable) {
1113                 if (endpoint->out_cables & (1 << cable))
1114                         snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1115                                                    &umidi->endpoints[cable & 1].out->ports[cable].substream);
1116                 if (endpoint->in_cables & (1 << cable))
1117                         snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1118                                                    &umidi->endpoints[0].in->ports[cable].substream);
1119         }
1120         return 0;
1121 }
1122
1123 static int snd_usbmidi_create_rawmidi(snd_usb_midi_t* umidi,
1124                                       int out_ports, int in_ports)
1125 {
1126         snd_rawmidi_t* rmidi;
1127         int err;
1128
1129         err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1130                               umidi->chip->next_midi_device++,
1131                               out_ports, in_ports, &rmidi);
1132         if (err < 0)
1133                 return err;
1134         strcpy(rmidi->name, umidi->chip->card->shortname);
1135         rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1136                             SNDRV_RAWMIDI_INFO_INPUT |
1137                             SNDRV_RAWMIDI_INFO_DUPLEX;
1138         rmidi->private_data = umidi;
1139         rmidi->private_free = snd_usbmidi_rawmidi_free;
1140         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1141         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1142
1143         umidi->rmidi = rmidi;
1144         return 0;
1145 }
1146
1147 /*
1148  * Creates and registers everything needed for a MIDI streaming interface.
1149  */
1150 int snd_usb_create_midi_interface(snd_usb_audio_t* chip,
1151                                   struct usb_interface* iface,
1152                                   const snd_usb_audio_quirk_t* quirk)
1153 {
1154         snd_usb_midi_t* umidi;
1155         snd_usb_midi_endpoint_info_t endpoints[MIDI_MAX_ENDPOINTS];
1156         int out_ports, in_ports;
1157         int i, err;
1158
1159         umidi = snd_magic_kcalloc(snd_usb_midi_t, 0, GFP_KERNEL);
1160         if (!umidi)
1161                 return -ENOMEM;
1162         umidi->chip = chip;
1163         umidi->iface = iface;
1164         umidi->quirk = quirk;
1165
1166         /* detect the endpoint(s) to use */
1167         memset(endpoints, 0, sizeof(endpoints));
1168         if (!quirk) {
1169                 err = snd_usbmidi_get_ms_info(umidi, endpoints);
1170         } else {
1171                 switch (quirk->type) {
1172                 case QUIRK_MIDI_FIXED_ENDPOINT:
1173                         memcpy(&endpoints[0], quirk->data,
1174                                sizeof(snd_usb_midi_endpoint_info_t));
1175                         err = snd_usbmidi_detect_endpoint(umidi, &endpoints[0]);
1176                         break;
1177                 case QUIRK_MIDI_YAMAHA:
1178                         err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1179                         break;
1180                 case QUIRK_MIDI_MIDIMAN:
1181                         memcpy(&endpoints[0], quirk->data,
1182                                sizeof(snd_usb_midi_endpoint_info_t));
1183                         err = 0;
1184                         break;
1185                 default:
1186                         snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1187                         err = -ENXIO;
1188                         break;
1189                 }
1190         }
1191         if (err < 0) {
1192                 snd_magic_kfree(umidi);
1193                 return err;
1194         }
1195
1196         /* create rawmidi device */
1197         out_ports = 0;
1198         in_ports = 0;
1199         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1200                 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1201                 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1202         }
1203         err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1204         if (err < 0) {
1205                 snd_magic_kfree(umidi);
1206                 return err;
1207         }
1208
1209         /* create endpoint/port structures */
1210         if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1211                 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1212         else
1213                 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1214         if (err < 0) {
1215                 snd_usbmidi_free(umidi);
1216                 return err;
1217         }
1218
1219         list_add(&umidi->list, &umidi->chip->midi_list);
1220
1221         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1222                 if (umidi->endpoints[i].in)
1223                         snd_usbmidi_submit_urb(umidi->endpoints[i].in->urb,
1224                                                GFP_KERNEL);
1225         return 0;
1226 }