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