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