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