pandora: defconfig: update
[pandora-kernel.git] / sound / usb / midi.c
1 /*
2  * usbmidi.c - ALSA USB MIDI driver
3  *
4  * Copyright (c) 2002-2009 Clemens Ladisch
5  * All rights reserved.
6  *
7  * Based on the OSS usb-midi driver by NAGANO Daisuke,
8  *          NetBSD's umidi driver by Takuya SHIOZAKI,
9  *          the "USB Device Class Definition for MIDI Devices" by Roland
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed and/or modified under the
21  * terms of the GNU General Public License as published by the Free Software
22  * Foundation; either version 2 of the License, or (at your option) any later
23  * version.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 #include <linux/kernel.h>
39 #include <linux/types.h>
40 #include <linux/bitops.h>
41 #include <linux/interrupt.h>
42 #include <linux/spinlock.h>
43 #include <linux/string.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/timer.h>
47 #include <linux/usb.h>
48 #include <linux/wait.h>
49 #include <linux/usb/audio.h>
50 #include <linux/module.h>
51
52 #include <sound/core.h>
53 #include <sound/control.h>
54 #include <sound/rawmidi.h>
55 #include <sound/asequencer.h>
56 #include "usbaudio.h"
57 #include "midi.h"
58 #include "power.h"
59 #include "helper.h"
60
61 /*
62  * define this to log all USB packets
63  */
64 /* #define DUMP_PACKETS */
65
66 /*
67  * how long to wait after some USB errors, so that khubd can disconnect() us
68  * without too many spurious errors
69  */
70 #define ERROR_DELAY_JIFFIES (HZ / 10)
71
72 #define OUTPUT_URBS 7
73 #define INPUT_URBS 7
74
75
76 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
77 MODULE_DESCRIPTION("USB Audio/MIDI helper module");
78 MODULE_LICENSE("Dual BSD/GPL");
79
80
81 struct usb_ms_header_descriptor {
82         __u8  bLength;
83         __u8  bDescriptorType;
84         __u8  bDescriptorSubtype;
85         __u8  bcdMSC[2];
86         __le16 wTotalLength;
87 } __attribute__ ((packed));
88
89 struct usb_ms_endpoint_descriptor {
90         __u8  bLength;
91         __u8  bDescriptorType;
92         __u8  bDescriptorSubtype;
93         __u8  bNumEmbMIDIJack;
94         __u8  baAssocJackID[0];
95 } __attribute__ ((packed));
96
97 struct snd_usb_midi_in_endpoint;
98 struct snd_usb_midi_out_endpoint;
99 struct snd_usb_midi_endpoint;
100
101 struct usb_protocol_ops {
102         void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
103         void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
104         void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
105         void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
106         void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
107 };
108
109 struct snd_usb_midi {
110         struct usb_device *dev;
111         struct snd_card *card;
112         struct usb_interface *iface;
113         const struct snd_usb_audio_quirk *quirk;
114         struct snd_rawmidi *rmidi;
115         struct usb_protocol_ops* usb_protocol_ops;
116         struct list_head list;
117         struct timer_list error_timer;
118         spinlock_t disc_lock;
119         struct rw_semaphore disc_rwsem;
120         struct mutex mutex;
121         u32 usb_id;
122         int next_midi_device;
123
124         struct snd_usb_midi_endpoint {
125                 struct snd_usb_midi_out_endpoint *out;
126                 struct snd_usb_midi_in_endpoint *in;
127         } endpoints[MIDI_MAX_ENDPOINTS];
128         unsigned long input_triggered;
129         unsigned int opened[2];
130         unsigned char disconnected;
131         unsigned char input_running;
132
133         struct snd_kcontrol *roland_load_ctl;
134 };
135
136 struct snd_usb_midi_out_endpoint {
137         struct snd_usb_midi* umidi;
138         struct out_urb_context {
139                 struct urb *urb;
140                 struct snd_usb_midi_out_endpoint *ep;
141         } urbs[OUTPUT_URBS];
142         unsigned int active_urbs;
143         unsigned int drain_urbs;
144         int max_transfer;               /* size of urb buffer */
145         struct tasklet_struct tasklet;
146         unsigned int next_urb;
147         spinlock_t buffer_lock;
148
149         struct usbmidi_out_port {
150                 struct snd_usb_midi_out_endpoint* ep;
151                 struct snd_rawmidi_substream *substream;
152                 int active;
153                 uint8_t cable;          /* cable number << 4 */
154                 uint8_t state;
155 #define STATE_UNKNOWN   0
156 #define STATE_1PARAM    1
157 #define STATE_2PARAM_1  2
158 #define STATE_2PARAM_2  3
159 #define STATE_SYSEX_0   4
160 #define STATE_SYSEX_1   5
161 #define STATE_SYSEX_2   6
162                 uint8_t data[2];
163         } ports[0x10];
164         int current_port;
165
166         wait_queue_head_t drain_wait;
167 };
168
169 struct snd_usb_midi_in_endpoint {
170         struct snd_usb_midi* umidi;
171         struct urb* urbs[INPUT_URBS];
172         struct usbmidi_in_port {
173                 struct snd_rawmidi_substream *substream;
174                 u8 running_status_length;
175         } ports[0x10];
176         u8 seen_f5;
177         bool in_sysex;
178         u8 last_cin;
179         u8 error_resubmit;
180         int current_port;
181 };
182
183 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
184
185 static const uint8_t snd_usbmidi_cin_length[] = {
186         0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
187 };
188
189 /*
190  * Submits the URB, with error handling.
191  */
192 static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
193 {
194         int err = usb_submit_urb(urb, flags);
195         if (err < 0 && err != -ENODEV)
196                 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
197         return err;
198 }
199
200 /*
201  * Error handling for URB completion functions.
202  */
203 static int snd_usbmidi_urb_error(int status)
204 {
205         switch (status) {
206         /* manually unlinked, or device gone */
207         case -ENOENT:
208         case -ECONNRESET:
209         case -ESHUTDOWN:
210         case -ENODEV:
211                 return -ENODEV;
212         /* errors that might occur during unplugging */
213         case -EPROTO:
214         case -ETIME:
215         case -EILSEQ:
216                 return -EIO;
217         default:
218                 snd_printk(KERN_ERR "urb status %d\n", status);
219                 return 0; /* continue */
220         }
221 }
222
223 /*
224  * Receives a chunk of MIDI data.
225  */
226 static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
227                                    uint8_t* data, int length)
228 {
229         struct usbmidi_in_port* port = &ep->ports[portidx];
230
231         if (!port->substream) {
232                 snd_printd("unexpected port %d!\n", portidx);
233                 return;
234         }
235         if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
236                 return;
237         snd_rawmidi_receive(port->substream, data, length);
238 }
239
240 #ifdef DUMP_PACKETS
241 static void dump_urb(const char *type, const u8 *data, int length)
242 {
243         snd_printk(KERN_DEBUG "%s packet: [", type);
244         for (; length > 0; ++data, --length)
245                 printk(" %02x", *data);
246         printk(" ]\n");
247 }
248 #else
249 #define dump_urb(type, data, length) /* nothing */
250 #endif
251
252 /*
253  * Processes the data read from the device.
254  */
255 static void snd_usbmidi_in_urb_complete(struct urb* urb)
256 {
257         struct snd_usb_midi_in_endpoint* ep = urb->context;
258
259         if (urb->status == 0) {
260                 dump_urb("received", urb->transfer_buffer, urb->actual_length);
261                 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
262                                                    urb->actual_length);
263         } else {
264                 int err = snd_usbmidi_urb_error(urb->status);
265                 if (err < 0) {
266                         if (err != -ENODEV) {
267                                 ep->error_resubmit = 1;
268                                 mod_timer(&ep->umidi->error_timer,
269                                           jiffies + ERROR_DELAY_JIFFIES);
270                         }
271                         return;
272                 }
273         }
274
275         urb->dev = ep->umidi->dev;
276         snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
277 }
278
279 static void snd_usbmidi_out_urb_complete(struct urb* urb)
280 {
281         struct out_urb_context *context = urb->context;
282         struct snd_usb_midi_out_endpoint* ep = context->ep;
283         unsigned int urb_index;
284
285         spin_lock(&ep->buffer_lock);
286         urb_index = context - ep->urbs;
287         ep->active_urbs &= ~(1 << urb_index);
288         if (unlikely(ep->drain_urbs)) {
289                 ep->drain_urbs &= ~(1 << urb_index);
290                 wake_up(&ep->drain_wait);
291         }
292         spin_unlock(&ep->buffer_lock);
293         if (urb->status < 0) {
294                 int err = snd_usbmidi_urb_error(urb->status);
295                 if (err < 0) {
296                         if (err != -ENODEV)
297                                 mod_timer(&ep->umidi->error_timer,
298                                           jiffies + ERROR_DELAY_JIFFIES);
299                         return;
300                 }
301         }
302         snd_usbmidi_do_output(ep);
303 }
304
305 /*
306  * This is called when some data should be transferred to the device
307  * (from one or more substreams).
308  */
309 static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
310 {
311         unsigned int urb_index;
312         struct urb* urb;
313         unsigned long flags;
314
315         spin_lock_irqsave(&ep->buffer_lock, flags);
316         if (ep->umidi->disconnected) {
317                 spin_unlock_irqrestore(&ep->buffer_lock, flags);
318                 return;
319         }
320
321         urb_index = ep->next_urb;
322         for (;;) {
323                 if (!(ep->active_urbs & (1 << urb_index))) {
324                         urb = ep->urbs[urb_index].urb;
325                         urb->transfer_buffer_length = 0;
326                         ep->umidi->usb_protocol_ops->output(ep, urb);
327                         if (urb->transfer_buffer_length == 0)
328                                 break;
329
330                         dump_urb("sending", urb->transfer_buffer,
331                                  urb->transfer_buffer_length);
332                         urb->dev = ep->umidi->dev;
333                         if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
334                                 break;
335                         ep->active_urbs |= 1 << urb_index;
336                 }
337                 if (++urb_index >= OUTPUT_URBS)
338                         urb_index = 0;
339                 if (urb_index == ep->next_urb)
340                         break;
341         }
342         ep->next_urb = urb_index;
343         spin_unlock_irqrestore(&ep->buffer_lock, flags);
344 }
345
346 static void snd_usbmidi_out_tasklet(unsigned long data)
347 {
348         struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
349
350         snd_usbmidi_do_output(ep);
351 }
352
353 /* called after transfers had been interrupted due to some USB error */
354 static void snd_usbmidi_error_timer(unsigned long data)
355 {
356         struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
357         unsigned int i, j;
358
359         spin_lock(&umidi->disc_lock);
360         if (umidi->disconnected) {
361                 spin_unlock(&umidi->disc_lock);
362                 return;
363         }
364         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
365                 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
366                 if (in && in->error_resubmit) {
367                         in->error_resubmit = 0;
368                         for (j = 0; j < INPUT_URBS; ++j) {
369                                 if (atomic_read(&in->urbs[j]->use_count))
370                                         continue;
371                                 in->urbs[j]->dev = umidi->dev;
372                                 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
373                         }
374                 }
375                 if (umidi->endpoints[i].out)
376                         snd_usbmidi_do_output(umidi->endpoints[i].out);
377         }
378         spin_unlock(&umidi->disc_lock);
379 }
380
381 /* helper function to send static data that may not DMA-able */
382 static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
383                                  const void *data, int len)
384 {
385         int err = 0;
386         void *buf = kmemdup(data, len, GFP_KERNEL);
387         if (!buf)
388                 return -ENOMEM;
389         dump_urb("sending", buf, len);
390         if (ep->urbs[0].urb)
391                 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
392                                    buf, len, NULL, 250);
393         kfree(buf);
394         return err;
395 }
396
397 /*
398  * Standard USB MIDI protocol: see the spec.
399  * Midiman protocol: like the standard protocol, but the control byte is the
400  * fourth byte in each packet, and uses length instead of CIN.
401  */
402
403 static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
404                                        uint8_t* buffer, int buffer_length)
405 {
406         int i;
407
408         for (i = 0; i + 3 < buffer_length; i += 4)
409                 if (buffer[i] != 0) {
410                         int cable = buffer[i] >> 4;
411                         int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
412                         snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
413                 }
414 }
415
416 static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
417                                       uint8_t* buffer, int buffer_length)
418 {
419         int i;
420
421         for (i = 0; i + 3 < buffer_length; i += 4)
422                 if (buffer[i + 3] != 0) {
423                         int port = buffer[i + 3] >> 4;
424                         int length = buffer[i + 3] & 3;
425                         snd_usbmidi_input_data(ep, port, &buffer[i], length);
426                 }
427 }
428
429 /*
430  * Buggy M-Audio device: running status on input results in a packet that has
431  * the data bytes but not the status byte and that is marked with CIN 4.
432  */
433 static void snd_usbmidi_maudio_broken_running_status_input(
434                                         struct snd_usb_midi_in_endpoint* ep,
435                                         uint8_t* buffer, int buffer_length)
436 {
437         int i;
438
439         for (i = 0; i + 3 < buffer_length; i += 4)
440                 if (buffer[i] != 0) {
441                         int cable = buffer[i] >> 4;
442                         u8 cin = buffer[i] & 0x0f;
443                         struct usbmidi_in_port *port = &ep->ports[cable];
444                         int length;
445
446                         length = snd_usbmidi_cin_length[cin];
447                         if (cin == 0xf && buffer[i + 1] >= 0xf8)
448                                 ; /* realtime msg: no running status change */
449                         else if (cin >= 0x8 && cin <= 0xe)
450                                 /* channel msg */
451                                 port->running_status_length = length - 1;
452                         else if (cin == 0x4 &&
453                                  port->running_status_length != 0 &&
454                                  buffer[i + 1] < 0x80)
455                                 /* CIN 4 that is not a SysEx */
456                                 length = port->running_status_length;
457                         else
458                                 /*
459                                  * All other msgs cannot begin running status.
460                                  * (A channel msg sent as two or three CIN 0xF
461                                  * packets could in theory, but this device
462                                  * doesn't use this format.)
463                                  */
464                                 port->running_status_length = 0;
465                         snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
466                 }
467 }
468
469 /*
470  * QinHeng CH345 is buggy: every second packet inside a SysEx has not CIN 4
471  * but the previously seen CIN, but still with three data bytes.
472  */
473 static void ch345_broken_sysex_input(struct snd_usb_midi_in_endpoint *ep,
474                                      uint8_t *buffer, int buffer_length)
475 {
476         unsigned int i, cin, length;
477
478         for (i = 0; i + 3 < buffer_length; i += 4) {
479                 if (buffer[i] == 0 && i > 0)
480                         break;
481                 cin = buffer[i] & 0x0f;
482                 if (ep->in_sysex &&
483                     cin == ep->last_cin &&
484                     (buffer[i + 1 + (cin == 0x6)] & 0x80) == 0)
485                         cin = 0x4;
486 #if 0
487                 if (buffer[i + 1] == 0x90) {
488                         /*
489                          * Either a corrupted running status or a real note-on
490                          * message; impossible to detect reliably.
491                          */
492                 }
493 #endif
494                 length = snd_usbmidi_cin_length[cin];
495                 snd_usbmidi_input_data(ep, 0, &buffer[i + 1], length);
496                 ep->in_sysex = cin == 0x4;
497                 if (!ep->in_sysex)
498                         ep->last_cin = cin;
499         }
500 }
501
502 /*
503  * CME protocol: like the standard protocol, but SysEx commands are sent as a
504  * single USB packet preceded by a 0x0F byte.
505  */
506 static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
507                                   uint8_t *buffer, int buffer_length)
508 {
509         if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
510                 snd_usbmidi_standard_input(ep, buffer, buffer_length);
511         else
512                 snd_usbmidi_input_data(ep, buffer[0] >> 4,
513                                        &buffer[1], buffer_length - 1);
514 }
515
516 /*
517  * Adds one USB MIDI packet to the output buffer.
518  */
519 static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
520                                                uint8_t p1, uint8_t p2, uint8_t p3)
521 {
522
523         uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
524         buf[0] = p0;
525         buf[1] = p1;
526         buf[2] = p2;
527         buf[3] = p3;
528         urb->transfer_buffer_length += 4;
529 }
530
531 /*
532  * Adds one Midiman packet to the output buffer.
533  */
534 static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
535                                               uint8_t p1, uint8_t p2, uint8_t p3)
536 {
537
538         uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
539         buf[0] = p1;
540         buf[1] = p2;
541         buf[2] = p3;
542         buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
543         urb->transfer_buffer_length += 4;
544 }
545
546 /*
547  * Converts MIDI commands to USB MIDI packets.
548  */
549 static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
550                                       uint8_t b, struct urb* urb)
551 {
552         uint8_t p0 = port->cable;
553         void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
554                 port->ep->umidi->usb_protocol_ops->output_packet;
555
556         if (b >= 0xf8) {
557                 output_packet(urb, p0 | 0x0f, b, 0, 0);
558         } else if (b >= 0xf0) {
559                 switch (b) {
560                 case 0xf0:
561                         port->data[0] = b;
562                         port->state = STATE_SYSEX_1;
563                         break;
564                 case 0xf1:
565                 case 0xf3:
566                         port->data[0] = b;
567                         port->state = STATE_1PARAM;
568                         break;
569                 case 0xf2:
570                         port->data[0] = b;
571                         port->state = STATE_2PARAM_1;
572                         break;
573                 case 0xf4:
574                 case 0xf5:
575                         port->state = STATE_UNKNOWN;
576                         break;
577                 case 0xf6:
578                         output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
579                         port->state = STATE_UNKNOWN;
580                         break;
581                 case 0xf7:
582                         switch (port->state) {
583                         case STATE_SYSEX_0:
584                                 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
585                                 break;
586                         case STATE_SYSEX_1:
587                                 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
588                                 break;
589                         case STATE_SYSEX_2:
590                                 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
591                                 break;
592                         }
593                         port->state = STATE_UNKNOWN;
594                         break;
595                 }
596         } else if (b >= 0x80) {
597                 port->data[0] = b;
598                 if (b >= 0xc0 && b <= 0xdf)
599                         port->state = STATE_1PARAM;
600                 else
601                         port->state = STATE_2PARAM_1;
602         } else { /* b < 0x80 */
603                 switch (port->state) {
604                 case STATE_1PARAM:
605                         if (port->data[0] < 0xf0) {
606                                 p0 |= port->data[0] >> 4;
607                         } else {
608                                 p0 |= 0x02;
609                                 port->state = STATE_UNKNOWN;
610                         }
611                         output_packet(urb, p0, port->data[0], b, 0);
612                         break;
613                 case STATE_2PARAM_1:
614                         port->data[1] = b;
615                         port->state = STATE_2PARAM_2;
616                         break;
617                 case STATE_2PARAM_2:
618                         if (port->data[0] < 0xf0) {
619                                 p0 |= port->data[0] >> 4;
620                                 port->state = STATE_2PARAM_1;
621                         } else {
622                                 p0 |= 0x03;
623                                 port->state = STATE_UNKNOWN;
624                         }
625                         output_packet(urb, p0, port->data[0], port->data[1], b);
626                         break;
627                 case STATE_SYSEX_0:
628                         port->data[0] = b;
629                         port->state = STATE_SYSEX_1;
630                         break;
631                 case STATE_SYSEX_1:
632                         port->data[1] = b;
633                         port->state = STATE_SYSEX_2;
634                         break;
635                 case STATE_SYSEX_2:
636                         output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
637                         port->state = STATE_SYSEX_0;
638                         break;
639                 }
640         }
641 }
642
643 static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
644                                         struct urb *urb)
645 {
646         int p;
647
648         /* FIXME: lower-numbered ports can starve higher-numbered ports */
649         for (p = 0; p < 0x10; ++p) {
650                 struct usbmidi_out_port* port = &ep->ports[p];
651                 if (!port->active)
652                         continue;
653                 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
654                         uint8_t b;
655                         if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
656                                 port->active = 0;
657                                 break;
658                         }
659                         snd_usbmidi_transmit_byte(port, b, urb);
660                 }
661         }
662 }
663
664 static struct usb_protocol_ops snd_usbmidi_standard_ops = {
665         .input = snd_usbmidi_standard_input,
666         .output = snd_usbmidi_standard_output,
667         .output_packet = snd_usbmidi_output_standard_packet,
668 };
669
670 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
671         .input = snd_usbmidi_midiman_input,
672         .output = snd_usbmidi_standard_output,
673         .output_packet = snd_usbmidi_output_midiman_packet,
674 };
675
676 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
677         .input = snd_usbmidi_maudio_broken_running_status_input,
678         .output = snd_usbmidi_standard_output,
679         .output_packet = snd_usbmidi_output_standard_packet,
680 };
681
682 static struct usb_protocol_ops snd_usbmidi_cme_ops = {
683         .input = snd_usbmidi_cme_input,
684         .output = snd_usbmidi_standard_output,
685         .output_packet = snd_usbmidi_output_standard_packet,
686 };
687
688 static struct usb_protocol_ops snd_usbmidi_ch345_broken_sysex_ops = {
689         .input = ch345_broken_sysex_input,
690         .output = snd_usbmidi_standard_output,
691         .output_packet = snd_usbmidi_output_standard_packet,
692 };
693
694 /*
695  * AKAI MPD16 protocol:
696  *
697  * For control port (endpoint 1):
698  * ==============================
699  * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
700  * SysEx message (msg_len=9 bytes long).
701  *
702  * For data port (endpoint 2):
703  * ===========================
704  * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
705  * MIDI message (msg_len bytes long)
706  *
707  * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
708  */
709 static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
710                                    uint8_t *buffer, int buffer_length)
711 {
712         unsigned int pos = 0;
713         unsigned int len = (unsigned int)buffer_length;
714         while (pos < len) {
715                 unsigned int port = (buffer[pos] >> 4) - 1;
716                 unsigned int msg_len = buffer[pos] & 0x0f;
717                 pos++;
718                 if (pos + msg_len <= len && port < 2)
719                         snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
720                 pos += msg_len;
721         }
722 }
723
724 #define MAX_AKAI_SYSEX_LEN 9
725
726 static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
727                                     struct urb *urb)
728 {
729         uint8_t *msg;
730         int pos, end, count, buf_end;
731         uint8_t tmp[MAX_AKAI_SYSEX_LEN];
732         struct snd_rawmidi_substream *substream = ep->ports[0].substream;
733
734         if (!ep->ports[0].active)
735                 return;
736
737         msg = urb->transfer_buffer + urb->transfer_buffer_length;
738         buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
739
740         /* only try adding more data when there's space for at least 1 SysEx */
741         while (urb->transfer_buffer_length < buf_end) {
742                 count = snd_rawmidi_transmit_peek(substream,
743                                                   tmp, MAX_AKAI_SYSEX_LEN);
744                 if (!count) {
745                         ep->ports[0].active = 0;
746                         return;
747                 }
748                 /* try to skip non-SysEx data */
749                 for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
750                         ;
751
752                 if (pos > 0) {
753                         snd_rawmidi_transmit_ack(substream, pos);
754                         continue;
755                 }
756
757                 /* look for the start or end marker */
758                 for (end = 1; end < count && tmp[end] < 0xF0; end++)
759                         ;
760
761                 /* next SysEx started before the end of current one */
762                 if (end < count && tmp[end] == 0xF0) {
763                         /* it's incomplete - drop it */
764                         snd_rawmidi_transmit_ack(substream, end);
765                         continue;
766                 }
767                 /* SysEx complete */
768                 if (end < count && tmp[end] == 0xF7) {
769                         /* queue it, ack it, and get the next one */
770                         count = end + 1;
771                         msg[0] = 0x10 | count;
772                         memcpy(&msg[1], tmp, count);
773                         snd_rawmidi_transmit_ack(substream, count);
774                         urb->transfer_buffer_length += count + 1;
775                         msg += count + 1;
776                         continue;
777                 }
778                 /* less than 9 bytes and no end byte - wait for more */
779                 if (count < MAX_AKAI_SYSEX_LEN) {
780                         ep->ports[0].active = 0;
781                         return;
782                 }
783                 /* 9 bytes and no end marker in sight - malformed, skip it */
784                 snd_rawmidi_transmit_ack(substream, count);
785         }
786 }
787
788 static struct usb_protocol_ops snd_usbmidi_akai_ops = {
789         .input = snd_usbmidi_akai_input,
790         .output = snd_usbmidi_akai_output,
791 };
792
793 /*
794  * Novation USB MIDI protocol: number of data bytes is in the first byte
795  * (when receiving) (+1!) or in the second byte (when sending); data begins
796  * at the third byte.
797  */
798
799 static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
800                                        uint8_t* buffer, int buffer_length)
801 {
802         if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
803                 return;
804         snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
805 }
806
807 static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
808                                         struct urb *urb)
809 {
810         uint8_t* transfer_buffer;
811         int count;
812
813         if (!ep->ports[0].active)
814                 return;
815         transfer_buffer = urb->transfer_buffer;
816         count = snd_rawmidi_transmit(ep->ports[0].substream,
817                                      &transfer_buffer[2],
818                                      ep->max_transfer - 2);
819         if (count < 1) {
820                 ep->ports[0].active = 0;
821                 return;
822         }
823         transfer_buffer[0] = 0;
824         transfer_buffer[1] = count;
825         urb->transfer_buffer_length = 2 + count;
826 }
827
828 static struct usb_protocol_ops snd_usbmidi_novation_ops = {
829         .input = snd_usbmidi_novation_input,
830         .output = snd_usbmidi_novation_output,
831 };
832
833 /*
834  * "raw" protocol: just move raw MIDI bytes from/to the endpoint
835  */
836
837 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
838                                   uint8_t* buffer, int buffer_length)
839 {
840         snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
841 }
842
843 static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
844                                    struct urb *urb)
845 {
846         int count;
847
848         if (!ep->ports[0].active)
849                 return;
850         count = snd_rawmidi_transmit(ep->ports[0].substream,
851                                      urb->transfer_buffer,
852                                      ep->max_transfer);
853         if (count < 1) {
854                 ep->ports[0].active = 0;
855                 return;
856         }
857         urb->transfer_buffer_length = count;
858 }
859
860 static struct usb_protocol_ops snd_usbmidi_raw_ops = {
861         .input = snd_usbmidi_raw_input,
862         .output = snd_usbmidi_raw_output,
863 };
864
865 /*
866  * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
867  */
868
869 static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint* ep,
870                                    uint8_t* buffer, int buffer_length)
871 {
872         if (buffer_length > 2)
873                 snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
874 }
875
876 static struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
877         .input = snd_usbmidi_ftdi_input,
878         .output = snd_usbmidi_raw_output,
879 };
880
881 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
882                                      uint8_t *buffer, int buffer_length)
883 {
884         if (buffer_length != 9)
885                 return;
886         buffer_length = 8;
887         while (buffer_length && buffer[buffer_length - 1] == 0xFD)
888                 buffer_length--;
889         if (buffer_length)
890                 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
891 }
892
893 static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
894                                       struct urb *urb)
895 {
896         int count;
897
898         if (!ep->ports[0].active)
899                 return;
900         switch (snd_usb_get_speed(ep->umidi->dev)) {
901         case USB_SPEED_HIGH:
902         case USB_SPEED_SUPER:
903                 count = 1;
904                 break;
905         default:
906                 count = 2;
907         }
908         count = snd_rawmidi_transmit(ep->ports[0].substream,
909                                      urb->transfer_buffer,
910                                      count);
911         if (count < 1) {
912                 ep->ports[0].active = 0;
913                 return;
914         }
915
916         memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
917         urb->transfer_buffer_length = ep->max_transfer;
918 }
919
920 static struct usb_protocol_ops snd_usbmidi_122l_ops = {
921         .input = snd_usbmidi_us122l_input,
922         .output = snd_usbmidi_us122l_output,
923 };
924
925 /*
926  * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
927  */
928
929 static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
930 {
931         static const u8 init_data[] = {
932                 /* initialization magic: "get version" */
933                 0xf0,
934                 0x00, 0x20, 0x31,       /* Emagic */
935                 0x64,                   /* Unitor8 */
936                 0x0b,                   /* version number request */
937                 0x00,                   /* command version */
938                 0x00,                   /* EEPROM, box 0 */
939                 0xf7
940         };
941         send_bulk_static_data(ep, init_data, sizeof(init_data));
942         /* while we're at it, pour on more magic */
943         send_bulk_static_data(ep, init_data, sizeof(init_data));
944 }
945
946 static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
947 {
948         static const u8 finish_data[] = {
949                 /* switch to patch mode with last preset */
950                 0xf0,
951                 0x00, 0x20, 0x31,       /* Emagic */
952                 0x64,                   /* Unitor8 */
953                 0x10,                   /* patch switch command */
954                 0x00,                   /* command version */
955                 0x7f,                   /* to all boxes */
956                 0x40,                   /* last preset in EEPROM */
957                 0xf7
958         };
959         send_bulk_static_data(ep, finish_data, sizeof(finish_data));
960 }
961
962 static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
963                                      uint8_t* buffer, int buffer_length)
964 {
965         int i;
966
967         /* FF indicates end of valid data */
968         for (i = 0; i < buffer_length; ++i)
969                 if (buffer[i] == 0xff) {
970                         buffer_length = i;
971                         break;
972                 }
973
974         /* handle F5 at end of last buffer */
975         if (ep->seen_f5)
976                 goto switch_port;
977
978         while (buffer_length > 0) {
979                 /* determine size of data until next F5 */
980                 for (i = 0; i < buffer_length; ++i)
981                         if (buffer[i] == 0xf5)
982                                 break;
983                 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
984                 buffer += i;
985                 buffer_length -= i;
986
987                 if (buffer_length <= 0)
988                         break;
989                 /* assert(buffer[0] == 0xf5); */
990                 ep->seen_f5 = 1;
991                 ++buffer;
992                 --buffer_length;
993
994         switch_port:
995                 if (buffer_length <= 0)
996                         break;
997                 if (buffer[0] < 0x80) {
998                         ep->current_port = (buffer[0] - 1) & 15;
999                         ++buffer;
1000                         --buffer_length;
1001                 }
1002                 ep->seen_f5 = 0;
1003         }
1004 }
1005
1006 static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
1007                                       struct urb *urb)
1008 {
1009         int port0 = ep->current_port;
1010         uint8_t* buf = urb->transfer_buffer;
1011         int buf_free = ep->max_transfer;
1012         int length, i;
1013
1014         for (i = 0; i < 0x10; ++i) {
1015                 /* round-robin, starting at the last current port */
1016                 int portnum = (port0 + i) & 15;
1017                 struct usbmidi_out_port* port = &ep->ports[portnum];
1018
1019                 if (!port->active)
1020                         continue;
1021                 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
1022                         port->active = 0;
1023                         continue;
1024                 }
1025
1026                 if (portnum != ep->current_port) {
1027                         if (buf_free < 2)
1028                                 break;
1029                         ep->current_port = portnum;
1030                         buf[0] = 0xf5;
1031                         buf[1] = (portnum + 1) & 15;
1032                         buf += 2;
1033                         buf_free -= 2;
1034                 }
1035
1036                 if (buf_free < 1)
1037                         break;
1038                 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
1039                 if (length > 0) {
1040                         buf += length;
1041                         buf_free -= length;
1042                         if (buf_free < 1)
1043                                 break;
1044                 }
1045         }
1046         if (buf_free < ep->max_transfer && buf_free > 0) {
1047                 *buf = 0xff;
1048                 --buf_free;
1049         }
1050         urb->transfer_buffer_length = ep->max_transfer - buf_free;
1051 }
1052
1053 static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
1054         .input = snd_usbmidi_emagic_input,
1055         .output = snd_usbmidi_emagic_output,
1056         .init_out_endpoint = snd_usbmidi_emagic_init_out,
1057         .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
1058 };
1059
1060
1061 static void update_roland_altsetting(struct snd_usb_midi* umidi)
1062 {
1063         struct usb_interface *intf;
1064         struct usb_host_interface *hostif;
1065         struct usb_interface_descriptor *intfd;
1066         int is_light_load;
1067
1068         intf = umidi->iface;
1069         is_light_load = intf->cur_altsetting != intf->altsetting;
1070         if (umidi->roland_load_ctl->private_value == is_light_load)
1071                 return;
1072         hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
1073         intfd = get_iface_desc(hostif);
1074         snd_usbmidi_input_stop(&umidi->list);
1075         usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1076                           intfd->bAlternateSetting);
1077         snd_usbmidi_input_start(&umidi->list);
1078 }
1079
1080 static int substream_open(struct snd_rawmidi_substream *substream, int dir,
1081                           int open)
1082 {
1083         struct snd_usb_midi* umidi = substream->rmidi->private_data;
1084         struct snd_kcontrol *ctl;
1085
1086         down_read(&umidi->disc_rwsem);
1087         if (umidi->disconnected) {
1088                 up_read(&umidi->disc_rwsem);
1089                 return open ? -ENODEV : 0;
1090         }
1091
1092         mutex_lock(&umidi->mutex);
1093         if (open) {
1094                 if (!umidi->opened[0] && !umidi->opened[1]) {
1095                         if (umidi->roland_load_ctl) {
1096                                 ctl = umidi->roland_load_ctl;
1097                                 ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1098                                 snd_ctl_notify(umidi->card,
1099                                        SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1100                                 update_roland_altsetting(umidi);
1101                         }
1102                 }
1103                 umidi->opened[dir]++;
1104                 if (umidi->opened[1])
1105                         snd_usbmidi_input_start(&umidi->list);
1106         } else {
1107                 umidi->opened[dir]--;
1108                 if (!umidi->opened[1])
1109                         snd_usbmidi_input_stop(&umidi->list);
1110                 if (!umidi->opened[0] && !umidi->opened[1]) {
1111                         if (umidi->roland_load_ctl) {
1112                                 ctl = umidi->roland_load_ctl;
1113                                 ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1114                                 snd_ctl_notify(umidi->card,
1115                                        SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1116                         }
1117                 }
1118         }
1119         mutex_unlock(&umidi->mutex);
1120         up_read(&umidi->disc_rwsem);
1121         return 0;
1122 }
1123
1124 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
1125 {
1126         struct snd_usb_midi* umidi = substream->rmidi->private_data;
1127         struct usbmidi_out_port* port = NULL;
1128         int i, j;
1129
1130         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1131                 if (umidi->endpoints[i].out)
1132                         for (j = 0; j < 0x10; ++j)
1133                                 if (umidi->endpoints[i].out->ports[j].substream == substream) {
1134                                         port = &umidi->endpoints[i].out->ports[j];
1135                                         break;
1136                                 }
1137         if (!port) {
1138                 snd_BUG();
1139                 return -ENXIO;
1140         }
1141
1142         substream->runtime->private_data = port;
1143         port->state = STATE_UNKNOWN;
1144         return substream_open(substream, 0, 1);
1145 }
1146
1147 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
1148 {
1149         return substream_open(substream, 0, 0);
1150 }
1151
1152 static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1153 {
1154         struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
1155
1156         port->active = up;
1157         if (up) {
1158                 if (port->ep->umidi->disconnected) {
1159                         /* gobble up remaining bytes to prevent wait in
1160                          * snd_rawmidi_drain_output */
1161                         while (!snd_rawmidi_transmit_empty(substream))
1162                                 snd_rawmidi_transmit_ack(substream, 1);
1163                         return;
1164                 }
1165                 tasklet_schedule(&port->ep->tasklet);
1166         }
1167 }
1168
1169 static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
1170 {
1171         struct usbmidi_out_port* port = substream->runtime->private_data;
1172         struct snd_usb_midi_out_endpoint *ep = port->ep;
1173         unsigned int drain_urbs;
1174         DEFINE_WAIT(wait);
1175         long timeout = msecs_to_jiffies(50);
1176
1177         if (ep->umidi->disconnected)
1178                 return;
1179         /*
1180          * The substream buffer is empty, but some data might still be in the
1181          * currently active URBs, so we have to wait for those to complete.
1182          */
1183         spin_lock_irq(&ep->buffer_lock);
1184         drain_urbs = ep->active_urbs;
1185         if (drain_urbs) {
1186                 ep->drain_urbs |= drain_urbs;
1187                 do {
1188                         prepare_to_wait(&ep->drain_wait, &wait,
1189                                         TASK_UNINTERRUPTIBLE);
1190                         spin_unlock_irq(&ep->buffer_lock);
1191                         timeout = schedule_timeout(timeout);
1192                         spin_lock_irq(&ep->buffer_lock);
1193                         drain_urbs &= ep->drain_urbs;
1194                 } while (drain_urbs && timeout);
1195                 finish_wait(&ep->drain_wait, &wait);
1196         }
1197         spin_unlock_irq(&ep->buffer_lock);
1198 }
1199
1200 static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
1201 {
1202         return substream_open(substream, 1, 1);
1203 }
1204
1205 static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
1206 {
1207         return substream_open(substream, 1, 0);
1208 }
1209
1210 static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1211 {
1212         struct snd_usb_midi* umidi = substream->rmidi->private_data;
1213
1214         if (up)
1215                 set_bit(substream->number, &umidi->input_triggered);
1216         else
1217                 clear_bit(substream->number, &umidi->input_triggered);
1218 }
1219
1220 static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
1221         .open = snd_usbmidi_output_open,
1222         .close = snd_usbmidi_output_close,
1223         .trigger = snd_usbmidi_output_trigger,
1224         .drain = snd_usbmidi_output_drain,
1225 };
1226
1227 static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
1228         .open = snd_usbmidi_input_open,
1229         .close = snd_usbmidi_input_close,
1230         .trigger = snd_usbmidi_input_trigger
1231 };
1232
1233 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1234                                 unsigned int buffer_length)
1235 {
1236         usb_free_coherent(umidi->dev, buffer_length,
1237                           urb->transfer_buffer, urb->transfer_dma);
1238         usb_free_urb(urb);
1239 }
1240
1241 /*
1242  * Frees an input endpoint.
1243  * May be called when ep hasn't been initialized completely.
1244  */
1245 static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
1246 {
1247         unsigned int i;
1248
1249         for (i = 0; i < INPUT_URBS; ++i)
1250                 if (ep->urbs[i])
1251                         free_urb_and_buffer(ep->umidi, ep->urbs[i],
1252                                             ep->urbs[i]->transfer_buffer_length);
1253         kfree(ep);
1254 }
1255
1256 /*
1257  * Creates an input endpoint.
1258  */
1259 static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
1260                                           struct snd_usb_midi_endpoint_info* ep_info,
1261                                           struct snd_usb_midi_endpoint* rep)
1262 {
1263         struct snd_usb_midi_in_endpoint* ep;
1264         void* buffer;
1265         unsigned int pipe;
1266         int length;
1267         unsigned int i;
1268
1269         rep->in = NULL;
1270         ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1271         if (!ep)
1272                 return -ENOMEM;
1273         ep->umidi = umidi;
1274
1275         for (i = 0; i < INPUT_URBS; ++i) {
1276                 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1277                 if (!ep->urbs[i]) {
1278                         snd_usbmidi_in_endpoint_delete(ep);
1279                         return -ENOMEM;
1280                 }
1281         }
1282         if (ep_info->in_interval)
1283                 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
1284         else
1285                 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1286         length = usb_maxpacket(umidi->dev, pipe, 0);
1287         for (i = 0; i < INPUT_URBS; ++i) {
1288                 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1289                                             &ep->urbs[i]->transfer_dma);
1290                 if (!buffer) {
1291                         snd_usbmidi_in_endpoint_delete(ep);
1292                         return -ENOMEM;
1293                 }
1294                 if (ep_info->in_interval)
1295                         usb_fill_int_urb(ep->urbs[i], umidi->dev,
1296                                          pipe, buffer, length,
1297                                          snd_usbmidi_in_urb_complete,
1298                                          ep, ep_info->in_interval);
1299                 else
1300                         usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
1301                                           pipe, buffer, length,
1302                                           snd_usbmidi_in_urb_complete, ep);
1303                 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1304         }
1305
1306         rep->in = ep;
1307         return 0;
1308 }
1309
1310 /*
1311  * Frees an output endpoint.
1312  * May be called when ep hasn't been initialized completely.
1313  */
1314 static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
1315 {
1316         unsigned int i;
1317
1318         for (i = 0; i < OUTPUT_URBS; ++i)
1319                 if (ep->urbs[i].urb) {
1320                         free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1321                                             ep->max_transfer);
1322                         ep->urbs[i].urb = NULL;
1323                 }
1324 }
1325
1326 static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1327 {
1328         snd_usbmidi_out_endpoint_clear(ep);
1329         kfree(ep);
1330 }
1331
1332 /*
1333  * Creates an output endpoint, and initializes output ports.
1334  */
1335 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
1336                                            struct snd_usb_midi_endpoint_info* ep_info,
1337                                            struct snd_usb_midi_endpoint* rep)
1338 {
1339         struct snd_usb_midi_out_endpoint* ep;
1340         unsigned int i;
1341         unsigned int pipe;
1342         void* buffer;
1343
1344         rep->out = NULL;
1345         ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1346         if (!ep)
1347                 return -ENOMEM;
1348         ep->umidi = umidi;
1349
1350         for (i = 0; i < OUTPUT_URBS; ++i) {
1351                 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1352                 if (!ep->urbs[i].urb) {
1353                         snd_usbmidi_out_endpoint_delete(ep);
1354                         return -ENOMEM;
1355                 }
1356                 ep->urbs[i].ep = ep;
1357         }
1358         if (ep_info->out_interval)
1359                 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
1360         else
1361                 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
1362         switch (umidi->usb_id) {
1363         default:
1364                 ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
1365                 break;
1366                 /*
1367                  * Various chips declare a packet size larger than 4 bytes, but
1368                  * do not actually work with larger packets:
1369                  */
1370         case USB_ID(0x0a67, 0x5011): /* Medeli DD305 */
1371         case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1372         case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1373         case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1374         case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1375         case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1376         case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
1377                 ep->max_transfer = 4;
1378                 break;
1379                 /*
1380                  * Some devices only work with 9 bytes packet size:
1381                  */
1382         case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
1383         case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
1384                 ep->max_transfer = 9;
1385                 break;
1386         }
1387         for (i = 0; i < OUTPUT_URBS; ++i) {
1388                 buffer = usb_alloc_coherent(umidi->dev,
1389                                             ep->max_transfer, GFP_KERNEL,
1390                                             &ep->urbs[i].urb->transfer_dma);
1391                 if (!buffer) {
1392                         snd_usbmidi_out_endpoint_delete(ep);
1393                         return -ENOMEM;
1394                 }
1395                 if (ep_info->out_interval)
1396                         usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
1397                                          pipe, buffer, ep->max_transfer,
1398                                          snd_usbmidi_out_urb_complete,
1399                                          &ep->urbs[i], ep_info->out_interval);
1400                 else
1401                         usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
1402                                           pipe, buffer, ep->max_transfer,
1403                                           snd_usbmidi_out_urb_complete,
1404                                           &ep->urbs[i]);
1405                 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1406         }
1407
1408         spin_lock_init(&ep->buffer_lock);
1409         tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1410         init_waitqueue_head(&ep->drain_wait);
1411
1412         for (i = 0; i < 0x10; ++i)
1413                 if (ep_info->out_cables & (1 << i)) {
1414                         ep->ports[i].ep = ep;
1415                         ep->ports[i].cable = i << 4;
1416                 }
1417
1418         if (umidi->usb_protocol_ops->init_out_endpoint)
1419                 umidi->usb_protocol_ops->init_out_endpoint(ep);
1420
1421         rep->out = ep;
1422         return 0;
1423 }
1424
1425 /*
1426  * Frees everything.
1427  */
1428 static void snd_usbmidi_free(struct snd_usb_midi* umidi)
1429 {
1430         int i;
1431
1432         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1433                 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1434                 if (ep->out)
1435                         snd_usbmidi_out_endpoint_delete(ep->out);
1436                 if (ep->in)
1437                         snd_usbmidi_in_endpoint_delete(ep->in);
1438         }
1439         mutex_destroy(&umidi->mutex);
1440         kfree(umidi);
1441 }
1442
1443 /*
1444  * Unlinks all URBs (must be done before the usb_device is deleted).
1445  */
1446 void snd_usbmidi_disconnect(struct list_head* p)
1447 {
1448         struct snd_usb_midi* umidi;
1449         unsigned int i, j;
1450
1451         umidi = list_entry(p, struct snd_usb_midi, list);
1452         /*
1453          * an URB's completion handler may start the timer and
1454          * a timer may submit an URB. To reliably break the cycle
1455          * a flag under lock must be used
1456          */
1457         down_write(&umidi->disc_rwsem);
1458         spin_lock_irq(&umidi->disc_lock);
1459         umidi->disconnected = 1;
1460         spin_unlock_irq(&umidi->disc_lock);
1461         up_write(&umidi->disc_rwsem);
1462
1463         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1464                 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1465                 if (ep->out)
1466                         tasklet_kill(&ep->out->tasklet);
1467                 if (ep->out) {
1468                         for (j = 0; j < OUTPUT_URBS; ++j)
1469                                 usb_kill_urb(ep->out->urbs[j].urb);
1470                         if (umidi->usb_protocol_ops->finish_out_endpoint)
1471                                 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1472                         ep->out->active_urbs = 0;
1473                         if (ep->out->drain_urbs) {
1474                                 ep->out->drain_urbs = 0;
1475                                 wake_up(&ep->out->drain_wait);
1476                         }
1477                 }
1478                 if (ep->in)
1479                         for (j = 0; j < INPUT_URBS; ++j)
1480                                 usb_kill_urb(ep->in->urbs[j]);
1481                 /* free endpoints here; later call can result in Oops */
1482                 if (ep->out)
1483                         snd_usbmidi_out_endpoint_clear(ep->out);
1484                 if (ep->in) {
1485                         snd_usbmidi_in_endpoint_delete(ep->in);
1486                         ep->in = NULL;
1487                 }
1488         }
1489         del_timer_sync(&umidi->error_timer);
1490 }
1491
1492 static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1493 {
1494         struct snd_usb_midi* umidi = rmidi->private_data;
1495         snd_usbmidi_free(umidi);
1496 }
1497
1498 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
1499                                                                 int stream, int number)
1500 {
1501         struct list_head* list;
1502
1503         list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1504                 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
1505                 if (substream->number == number)
1506                         return substream;
1507         }
1508         return NULL;
1509 }
1510
1511 /*
1512  * This list specifies names for ports that do not fit into the standard
1513  * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1514  * such as internal control or synthesizer ports.
1515  */
1516 static struct port_info {
1517         u32 id;
1518         short int port;
1519         short int voices;
1520         const char *name;
1521         unsigned int seq_flags;
1522 } snd_usbmidi_port_info[] = {
1523 #define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1524         { .id = USB_ID(vendor, product), \
1525           .port = num, .voices = voices_, \
1526           .name = name_, .seq_flags = flags }
1527 #define EXTERNAL_PORT(vendor, product, num, name) \
1528         PORT_INFO(vendor, product, num, name, 0, \
1529                   SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1530                   SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1531                   SNDRV_SEQ_PORT_TYPE_PORT)
1532 #define CONTROL_PORT(vendor, product, num, name) \
1533         PORT_INFO(vendor, product, num, name, 0, \
1534                   SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1535                   SNDRV_SEQ_PORT_TYPE_HARDWARE)
1536 #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1537         PORT_INFO(vendor, product, num, name, voices, \
1538                   SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1539                   SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1540                   SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1541                   SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1542                   SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1543                   SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1544                   SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1545 #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1546         PORT_INFO(vendor, product, num, name, voices, \
1547                   SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1548                   SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1549                   SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1550                   SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1551                   SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1552                   SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1553                   SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1554                   SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1555         /* Roland UA-100 */
1556         CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1557         /* Roland SC-8850 */
1558         SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1559         SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1560         SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1561         SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1562         EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1563         EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1564         /* Roland U-8 */
1565         EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1566         CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1567         /* Roland SC-8820 */
1568         SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1569         SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1570         EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1571         /* Roland SK-500 */
1572         SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1573         SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1574         EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1575         /* Roland SC-D70 */
1576         SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1577         SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1578         EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1579         /* Edirol UM-880 */
1580         CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1581         /* Edirol SD-90 */
1582         ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1583         ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1584         EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1585         EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1586         /* Edirol UM-550 */
1587         CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1588         /* Edirol SD-20 */
1589         ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1590         ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1591         EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1592         /* Edirol SD-80 */
1593         ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1594         ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1595         EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1596         EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1597         /* Edirol UA-700 */
1598         EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1599         CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1600         /* Roland VariOS */
1601         EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1602         EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1603         EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1604         /* Edirol PCR */
1605         EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1606         EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1607         EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1608         /* BOSS GS-10 */
1609         EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1610         CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1611         /* Edirol UA-1000 */
1612         EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1613         CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1614         /* Edirol UR-80 */
1615         EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1616         EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1617         EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1618         /* Edirol PCR-A */
1619         EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1620         EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1621         EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1622         /* Edirol UM-3EX */
1623         CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1624         /* M-Audio MidiSport 8x8 */
1625         CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1626         CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1627         /* MOTU Fastlane */
1628         EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1629         EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1630         /* Emagic Unitor8/AMT8/MT4 */
1631         EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1632         EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1633         EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1634         /* Akai MPD16 */
1635         CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1636         PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1637                 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1638                 SNDRV_SEQ_PORT_TYPE_HARDWARE),
1639         /* Access Music Virus TI */
1640         EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1641         PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1642                 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1643                 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1644                 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
1645 };
1646
1647 static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1648 {
1649         int i;
1650
1651         for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1652                 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
1653                     snd_usbmidi_port_info[i].port == number)
1654                         return &snd_usbmidi_port_info[i];
1655         }
1656         return NULL;
1657 }
1658
1659 static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1660                                       struct snd_seq_port_info *seq_port_info)
1661 {
1662         struct snd_usb_midi *umidi = rmidi->private_data;
1663         struct port_info *port_info;
1664
1665         /* TODO: read port flags from descriptors */
1666         port_info = find_port_info(umidi, number);
1667         if (port_info) {
1668                 seq_port_info->type = port_info->seq_flags;
1669                 seq_port_info->midi_voices = port_info->voices;
1670         }
1671 }
1672
1673 static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
1674                                        int stream, int number,
1675                                        struct snd_rawmidi_substream ** rsubstream)
1676 {
1677         struct port_info *port_info;
1678         const char *name_format;
1679
1680         struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1681         if (!substream) {
1682                 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1683                 return;
1684         }
1685
1686         /* TODO: read port name from jack descriptor */
1687         port_info = find_port_info(umidi, number);
1688         name_format = port_info ? port_info->name : "%s MIDI %d";
1689         snprintf(substream->name, sizeof(substream->name),
1690                  name_format, umidi->card->shortname, number + 1);
1691
1692         *rsubstream = substream;
1693 }
1694
1695 /*
1696  * Creates the endpoints and their ports.
1697  */
1698 static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1699                                         struct snd_usb_midi_endpoint_info* endpoints)
1700 {
1701         int i, j, err;
1702         int out_ports = 0, in_ports = 0;
1703
1704         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1705                 if (endpoints[i].out_cables) {
1706                         err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1707                                                               &umidi->endpoints[i]);
1708                         if (err < 0)
1709                                 return err;
1710                 }
1711                 if (endpoints[i].in_cables) {
1712                         err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1713                                                              &umidi->endpoints[i]);
1714                         if (err < 0)
1715                                 return err;
1716                 }
1717
1718                 for (j = 0; j < 0x10; ++j) {
1719                         if (endpoints[i].out_cables & (1 << j)) {
1720                                 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1721                                                            &umidi->endpoints[i].out->ports[j].substream);
1722                                 ++out_ports;
1723                         }
1724                         if (endpoints[i].in_cables & (1 << j)) {
1725                                 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1726                                                            &umidi->endpoints[i].in->ports[j].substream);
1727                                 ++in_ports;
1728                         }
1729                 }
1730         }
1731         snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1732                     out_ports, in_ports);
1733         return 0;
1734 }
1735
1736 /*
1737  * Returns MIDIStreaming device capabilities.
1738  */
1739 static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1740                                    struct snd_usb_midi_endpoint_info* endpoints)
1741 {
1742         struct usb_interface* intf;
1743         struct usb_host_interface *hostif;
1744         struct usb_interface_descriptor* intfd;
1745         struct usb_ms_header_descriptor* ms_header;
1746         struct usb_host_endpoint *hostep;
1747         struct usb_endpoint_descriptor* ep;
1748         struct usb_ms_endpoint_descriptor* ms_ep;
1749         int i, epidx;
1750
1751         intf = umidi->iface;
1752         if (!intf)
1753                 return -ENXIO;
1754         hostif = &intf->altsetting[0];
1755         intfd = get_iface_desc(hostif);
1756         ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1757         if (hostif->extralen >= 7 &&
1758             ms_header->bLength >= 7 &&
1759             ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1760             ms_header->bDescriptorSubtype == UAC_HEADER)
1761                 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1762                             ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1763         else
1764                 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1765
1766         epidx = 0;
1767         for (i = 0; i < intfd->bNumEndpoints; ++i) {
1768                 hostep = &hostif->endpoint[i];
1769                 ep = get_ep_desc(hostep);
1770                 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
1771                         continue;
1772                 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1773                 if (hostep->extralen < 4 ||
1774                     ms_ep->bLength < 4 ||
1775                     ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1776                     ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
1777                         continue;
1778                 if (usb_endpoint_dir_out(ep)) {
1779                         if (endpoints[epidx].out_ep) {
1780                                 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1781                                         snd_printk(KERN_WARNING "too many endpoints\n");
1782                                         break;
1783                                 }
1784                         }
1785                         endpoints[epidx].out_ep = usb_endpoint_num(ep);
1786                         if (usb_endpoint_xfer_int(ep))
1787                                 endpoints[epidx].out_interval = ep->bInterval;
1788                         else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1789                                 /*
1790                                  * Low speed bulk transfers don't exist, so
1791                                  * force interrupt transfers for devices like
1792                                  * ESI MIDI Mate that try to use them anyway.
1793                                  */
1794                                 endpoints[epidx].out_interval = 1;
1795                         endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1796                         snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1797                                     ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1798                 } else {
1799                         if (endpoints[epidx].in_ep) {
1800                                 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1801                                         snd_printk(KERN_WARNING "too many endpoints\n");
1802                                         break;
1803                                 }
1804                         }
1805                         endpoints[epidx].in_ep = usb_endpoint_num(ep);
1806                         if (usb_endpoint_xfer_int(ep))
1807                                 endpoints[epidx].in_interval = ep->bInterval;
1808                         else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1809                                 endpoints[epidx].in_interval = 1;
1810                         endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1811                         snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1812                                     ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1813                 }
1814         }
1815         return 0;
1816 }
1817
1818 static int roland_load_info(struct snd_kcontrol *kcontrol,
1819                             struct snd_ctl_elem_info *info)
1820 {
1821         static const char *const names[] = { "High Load", "Light Load" };
1822
1823         return snd_ctl_enum_info(info, 1, 2, names);
1824 }
1825
1826 static int roland_load_get(struct snd_kcontrol *kcontrol,
1827                            struct snd_ctl_elem_value *value)
1828 {
1829         value->value.enumerated.item[0] = kcontrol->private_value;
1830         return 0;
1831 }
1832
1833 static int roland_load_put(struct snd_kcontrol *kcontrol,
1834                            struct snd_ctl_elem_value *value)
1835 {
1836         struct snd_usb_midi* umidi = kcontrol->private_data;
1837         int changed;
1838
1839         if (value->value.enumerated.item[0] > 1)
1840                 return -EINVAL;
1841         mutex_lock(&umidi->mutex);
1842         changed = value->value.enumerated.item[0] != kcontrol->private_value;
1843         if (changed)
1844                 kcontrol->private_value = value->value.enumerated.item[0];
1845         mutex_unlock(&umidi->mutex);
1846         return changed;
1847 }
1848
1849 static struct snd_kcontrol_new roland_load_ctl = {
1850         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1851         .name = "MIDI Input Mode",
1852         .info = roland_load_info,
1853         .get = roland_load_get,
1854         .put = roland_load_put,
1855         .private_value = 1,
1856 };
1857
1858 /*
1859  * On Roland devices, use the second alternate setting to be able to use
1860  * the interrupt input endpoint.
1861  */
1862 static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
1863 {
1864         struct usb_interface* intf;
1865         struct usb_host_interface *hostif;
1866         struct usb_interface_descriptor* intfd;
1867
1868         intf = umidi->iface;
1869         if (!intf || intf->num_altsetting != 2)
1870                 return;
1871
1872         hostif = &intf->altsetting[1];
1873         intfd = get_iface_desc(hostif);
1874         if (intfd->bNumEndpoints != 2 ||
1875             (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1876             (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1877                 return;
1878
1879         snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1880                     intfd->bAlternateSetting);
1881         usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1882                           intfd->bAlternateSetting);
1883
1884         umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
1885         if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
1886                 umidi->roland_load_ctl = NULL;
1887 }
1888
1889 /*
1890  * Try to find any usable endpoints in the interface.
1891  */
1892 static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1893                                         struct snd_usb_midi_endpoint_info* endpoint,
1894                                         int max_endpoints)
1895 {
1896         struct usb_interface* intf;
1897         struct usb_host_interface *hostif;
1898         struct usb_interface_descriptor* intfd;
1899         struct usb_endpoint_descriptor* epd;
1900         int i, out_eps = 0, in_eps = 0;
1901
1902         if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
1903                 snd_usbmidi_switch_roland_altsetting(umidi);
1904
1905         if (endpoint[0].out_ep || endpoint[0].in_ep)
1906                 return 0;
1907
1908         intf = umidi->iface;
1909         if (!intf || intf->num_altsetting < 1)
1910                 return -ENOENT;
1911         hostif = intf->cur_altsetting;
1912         intfd = get_iface_desc(hostif);
1913
1914         for (i = 0; i < intfd->bNumEndpoints; ++i) {
1915                 epd = get_endpoint(hostif, i);
1916                 if (!usb_endpoint_xfer_bulk(epd) &&
1917                     !usb_endpoint_xfer_int(epd))
1918                         continue;
1919                 if (out_eps < max_endpoints &&
1920                     usb_endpoint_dir_out(epd)) {
1921                         endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1922                         if (usb_endpoint_xfer_int(epd))
1923                                 endpoint[out_eps].out_interval = epd->bInterval;
1924                         ++out_eps;
1925                 }
1926                 if (in_eps < max_endpoints &&
1927                     usb_endpoint_dir_in(epd)) {
1928                         endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1929                         if (usb_endpoint_xfer_int(epd))
1930                                 endpoint[in_eps].in_interval = epd->bInterval;
1931                         ++in_eps;
1932                 }
1933         }
1934         return (out_eps || in_eps) ? 0 : -ENOENT;
1935 }
1936
1937 /*
1938  * Detects the endpoints for one-port-per-endpoint protocols.
1939  */
1940 static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1941                                                  struct snd_usb_midi_endpoint_info* endpoints)
1942 {
1943         int err, i;
1944
1945         err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1946         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1947                 if (endpoints[i].out_ep)
1948                         endpoints[i].out_cables = 0x0001;
1949                 if (endpoints[i].in_ep)
1950                         endpoints[i].in_cables = 0x0001;
1951         }
1952         return err;
1953 }
1954
1955 /*
1956  * Detects the endpoints and ports of Yamaha devices.
1957  */
1958 static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1959                                      struct snd_usb_midi_endpoint_info* endpoint)
1960 {
1961         struct usb_interface* intf;
1962         struct usb_host_interface *hostif;
1963         struct usb_interface_descriptor* intfd;
1964         uint8_t* cs_desc;
1965
1966         intf = umidi->iface;
1967         if (!intf)
1968                 return -ENOENT;
1969         hostif = intf->altsetting;
1970         intfd = get_iface_desc(hostif);
1971         if (intfd->bNumEndpoints < 1)
1972                 return -ENOENT;
1973
1974         /*
1975          * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1976          * necessarily with any useful contents.  So simply count 'em.
1977          */
1978         for (cs_desc = hostif->extra;
1979              cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1980              cs_desc += cs_desc[0]) {
1981                 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1982                         if (cs_desc[2] == UAC_MIDI_IN_JACK)
1983                                 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1984                         else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
1985                                 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1986                 }
1987         }
1988         if (!endpoint->in_cables && !endpoint->out_cables)
1989                 return -ENOENT;
1990
1991         return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1992 }
1993
1994 /*
1995  * Creates the endpoints and their ports for Midiman devices.
1996  */
1997 static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1998                                                 struct snd_usb_midi_endpoint_info* endpoint)
1999 {
2000         struct snd_usb_midi_endpoint_info ep_info;
2001         struct usb_interface* intf;
2002         struct usb_host_interface *hostif;
2003         struct usb_interface_descriptor* intfd;
2004         struct usb_endpoint_descriptor* epd;
2005         int cable, err;
2006
2007         intf = umidi->iface;
2008         if (!intf)
2009                 return -ENOENT;
2010         hostif = intf->altsetting;
2011         intfd = get_iface_desc(hostif);
2012         /*
2013          * The various MidiSport devices have more or less random endpoint
2014          * numbers, so we have to identify the endpoints by their index in
2015          * the descriptor array, like the driver for that other OS does.
2016          *
2017          * There is one interrupt input endpoint for all input ports, one
2018          * bulk output endpoint for even-numbered ports, and one for odd-
2019          * numbered ports.  Both bulk output endpoints have corresponding
2020          * input bulk endpoints (at indices 1 and 3) which aren't used.
2021          */
2022         if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
2023                 snd_printdd(KERN_ERR "not enough endpoints\n");
2024                 return -ENOENT;
2025         }
2026
2027         epd = get_endpoint(hostif, 0);
2028         if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
2029                 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
2030                 return -ENXIO;
2031         }
2032         epd = get_endpoint(hostif, 2);
2033         if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
2034                 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
2035                 return -ENXIO;
2036         }
2037         if (endpoint->out_cables > 0x0001) {
2038                 epd = get_endpoint(hostif, 4);
2039                 if (!usb_endpoint_dir_out(epd) ||
2040                     !usb_endpoint_xfer_bulk(epd)) {
2041                         snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
2042                         return -ENXIO;
2043                 }
2044         }
2045
2046         ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2047         ep_info.out_interval = 0;
2048         ep_info.out_cables = endpoint->out_cables & 0x5555;
2049         err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
2050         if (err < 0)
2051                 return err;
2052
2053         ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2054         ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
2055         ep_info.in_cables = endpoint->in_cables;
2056         err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
2057         if (err < 0)
2058                 return err;
2059
2060         if (endpoint->out_cables > 0x0001) {
2061                 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2062                 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
2063                 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
2064                 if (err < 0)
2065                         return err;
2066         }
2067
2068         for (cable = 0; cable < 0x10; ++cable) {
2069                 if (endpoint->out_cables & (1 << cable))
2070                         snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
2071                                                    &umidi->endpoints[cable & 1].out->ports[cable].substream);
2072                 if (endpoint->in_cables & (1 << cable))
2073                         snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
2074                                                    &umidi->endpoints[0].in->ports[cable].substream);
2075         }
2076         return 0;
2077 }
2078
2079 static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
2080         .get_port_info = snd_usbmidi_get_port_info,
2081 };
2082
2083 static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
2084                                       int out_ports, int in_ports)
2085 {
2086         struct snd_rawmidi *rmidi;
2087         int err;
2088
2089         err = snd_rawmidi_new(umidi->card, "USB MIDI",
2090                               umidi->next_midi_device++,
2091                               out_ports, in_ports, &rmidi);
2092         if (err < 0)
2093                 return err;
2094         strcpy(rmidi->name, umidi->card->shortname);
2095         rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2096                             SNDRV_RAWMIDI_INFO_INPUT |
2097                             SNDRV_RAWMIDI_INFO_DUPLEX;
2098         rmidi->ops = &snd_usbmidi_ops;
2099         rmidi->private_data = umidi;
2100         rmidi->private_free = snd_usbmidi_rawmidi_free;
2101         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
2102         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
2103
2104         umidi->rmidi = rmidi;
2105         return 0;
2106 }
2107
2108 /*
2109  * Temporarily stop input.
2110  */
2111 void snd_usbmidi_input_stop(struct list_head* p)
2112 {
2113         struct snd_usb_midi* umidi;
2114         unsigned int i, j;
2115
2116         umidi = list_entry(p, struct snd_usb_midi, list);
2117         if (!umidi->input_running)
2118                 return;
2119         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2120                 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
2121                 if (ep->in)
2122                         for (j = 0; j < INPUT_URBS; ++j)
2123                                 usb_kill_urb(ep->in->urbs[j]);
2124         }
2125         umidi->input_running = 0;
2126 }
2127
2128 static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
2129 {
2130         unsigned int i;
2131
2132         if (!ep)
2133                 return;
2134         for (i = 0; i < INPUT_URBS; ++i) {
2135                 struct urb* urb = ep->urbs[i];
2136                 urb->dev = ep->umidi->dev;
2137                 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
2138         }
2139 }
2140
2141 /*
2142  * Resume input after a call to snd_usbmidi_input_stop().
2143  */
2144 void snd_usbmidi_input_start(struct list_head* p)
2145 {
2146         struct snd_usb_midi* umidi;
2147         int i;
2148
2149         umidi = list_entry(p, struct snd_usb_midi, list);
2150         if (umidi->input_running || !umidi->opened[1])
2151                 return;
2152         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2153                 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
2154         umidi->input_running = 1;
2155 }
2156
2157 /*
2158  * Creates and registers everything needed for a MIDI streaming interface.
2159  */
2160 int snd_usbmidi_create(struct snd_card *card,
2161                        struct usb_interface* iface,
2162                        struct list_head *midi_list,
2163                        const struct snd_usb_audio_quirk* quirk)
2164 {
2165         struct snd_usb_midi* umidi;
2166         struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
2167         int out_ports, in_ports;
2168         int i, err;
2169
2170         umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
2171         if (!umidi)
2172                 return -ENOMEM;
2173         umidi->dev = interface_to_usbdev(iface);
2174         umidi->card = card;
2175         umidi->iface = iface;
2176         umidi->quirk = quirk;
2177         umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
2178         init_timer(&umidi->error_timer);
2179         spin_lock_init(&umidi->disc_lock);
2180         init_rwsem(&umidi->disc_rwsem);
2181         mutex_init(&umidi->mutex);
2182         umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2183                                le16_to_cpu(umidi->dev->descriptor.idProduct));
2184         umidi->error_timer.function = snd_usbmidi_error_timer;
2185         umidi->error_timer.data = (unsigned long)umidi;
2186
2187         /* detect the endpoint(s) to use */
2188         memset(endpoints, 0, sizeof(endpoints));
2189         switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2190         case QUIRK_MIDI_STANDARD_INTERFACE:
2191                 err = snd_usbmidi_get_ms_info(umidi, endpoints);
2192                 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
2193                         umidi->usb_protocol_ops =
2194                                 &snd_usbmidi_maudio_broken_running_status_ops;
2195                 break;
2196         case QUIRK_MIDI_US122L:
2197                 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2198                 /* fall through */
2199         case QUIRK_MIDI_FIXED_ENDPOINT:
2200                 memcpy(&endpoints[0], quirk->data,
2201                        sizeof(struct snd_usb_midi_endpoint_info));
2202                 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2203                 break;
2204         case QUIRK_MIDI_YAMAHA:
2205                 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2206                 break;
2207         case QUIRK_MIDI_MIDIMAN:
2208                 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2209                 memcpy(&endpoints[0], quirk->data,
2210                        sizeof(struct snd_usb_midi_endpoint_info));
2211                 err = 0;
2212                 break;
2213         case QUIRK_MIDI_NOVATION:
2214                 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2215                 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2216                 break;
2217         case QUIRK_MIDI_RAW_BYTES:
2218                 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
2219                 /*
2220                  * Interface 1 contains isochronous endpoints, but with the same
2221                  * numbers as in interface 0.  Since it is interface 1 that the
2222                  * USB core has most recently seen, these descriptors are now
2223                  * associated with the endpoint numbers.  This will foul up our
2224                  * attempts to submit bulk/interrupt URBs to the endpoints in
2225                  * interface 0, so we have to make sure that the USB core looks
2226                  * again at interface 0 by calling usb_set_interface() on it.
2227                  */
2228                 if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2229                         usb_set_interface(umidi->dev, 0, 0);
2230                 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2231                 break;
2232         case QUIRK_MIDI_EMAGIC:
2233                 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2234                 memcpy(&endpoints[0], quirk->data,
2235                        sizeof(struct snd_usb_midi_endpoint_info));
2236                 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2237                 break;
2238         case QUIRK_MIDI_CME:
2239                 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
2240                 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2241                 break;
2242         case QUIRK_MIDI_AKAI:
2243                 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2244                 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2245                 /* endpoint 1 is input-only */
2246                 endpoints[1].out_cables = 0;
2247                 break;
2248         case QUIRK_MIDI_FTDI:
2249                 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2250
2251                 /* set baud rate to 31250 (48 MHz / 16 / 96) */
2252                 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2253                                       3, 0x40, 0x60, 0, NULL, 0, 1000);
2254                 if (err < 0)
2255                         break;
2256
2257                 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2258                 break;
2259         case QUIRK_MIDI_CH345:
2260                 umidi->usb_protocol_ops = &snd_usbmidi_ch345_broken_sysex_ops;
2261                 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2262                 break;
2263         default:
2264                 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2265                 err = -ENXIO;
2266                 break;
2267         }
2268         if (err < 0) {
2269                 kfree(umidi);
2270                 return err;
2271         }
2272
2273         /* create rawmidi device */
2274         out_ports = 0;
2275         in_ports = 0;
2276         for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2277                 out_ports += hweight16(endpoints[i].out_cables);
2278                 in_ports += hweight16(endpoints[i].in_cables);
2279         }
2280         err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2281         if (err < 0) {
2282                 kfree(umidi);
2283                 return err;
2284         }
2285
2286         /* create endpoint/port structures */
2287         if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2288                 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2289         else
2290                 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2291         if (err < 0) {
2292                 return err;
2293         }
2294
2295         usb_autopm_get_interface_no_resume(umidi->iface);
2296
2297         list_add_tail(&umidi->list, midi_list);
2298         return 0;
2299 }
2300
2301 EXPORT_SYMBOL(snd_usbmidi_create);
2302 EXPORT_SYMBOL(snd_usbmidi_input_stop);
2303 EXPORT_SYMBOL(snd_usbmidi_input_start);
2304 EXPORT_SYMBOL(snd_usbmidi_disconnect);