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