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