blob: 2b9d940c8064e705cf7bfd27dd674f2e4780dc71 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Clemens Ladischc8846972005-08-02 15:26:52 +020047#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/usb.h>
49#include <sound/core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#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
Clemens Ladischc8846972005-08-02 15:26:52 +020059/*
60 * how long to wait after some USB errors, so that khubd can disconnect() us
61 * without too many spurious errors
62 */
63#define ERROR_DELAY_JIFFIES (HZ / 10)
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
67MODULE_DESCRIPTION("USB Audio/MIDI helper module");
68MODULE_LICENSE("Dual BSD/GPL");
69
70
71struct usb_ms_header_descriptor {
72 __u8 bLength;
73 __u8 bDescriptorType;
74 __u8 bDescriptorSubtype;
75 __u8 bcdMSC[2];
76 __le16 wTotalLength;
77} __attribute__ ((packed));
78
79struct usb_ms_endpoint_descriptor {
80 __u8 bLength;
81 __u8 bDescriptorType;
82 __u8 bDescriptorSubtype;
83 __u8 bNumEmbMIDIJack;
84 __u8 baAssocJackID[0];
85} __attribute__ ((packed));
86
Takashi Iwai86e07d32005-11-17 15:08:02 +010087struct snd_usb_midi_in_endpoint;
88struct snd_usb_midi_out_endpoint;
89struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +010092 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
93 void (*output)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Takashi Iwai86e07d32005-11-17 15:08:02 +010095 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
96 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
99struct snd_usb_midi {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100100 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100102 const struct snd_usb_audio_quirk *quirk;
103 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct usb_protocol_ops* usb_protocol_ops;
105 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200106 struct timer_list error_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100109 struct snd_usb_midi_out_endpoint *out;
110 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 } endpoints[MIDI_MAX_ENDPOINTS];
112 unsigned long input_triggered;
113};
114
115struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100116 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct urb* urb;
118 int urb_active;
119 int max_transfer; /* size of urb buffer */
120 struct tasklet_struct tasklet;
121
122 spinlock_t buffer_lock;
123
124 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100125 struct snd_usb_midi_out_endpoint* ep;
126 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int active;
128 uint8_t cable; /* cable number << 4 */
129 uint8_t state;
130#define STATE_UNKNOWN 0
131#define STATE_1PARAM 1
132#define STATE_2PARAM_1 2
133#define STATE_2PARAM_2 3
134#define STATE_SYSEX_0 4
135#define STATE_SYSEX_1 5
136#define STATE_SYSEX_2 6
137 uint8_t data[2];
138 } ports[0x10];
139 int current_port;
140};
141
142struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100143 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct urb* urb;
145 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100146 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200148 u8 seen_f5;
149 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int current_port;
151};
152
Takashi Iwai86e07d32005-11-17 15:08:02 +0100153static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155static const uint8_t snd_usbmidi_cin_length[] = {
156 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
157};
158
159/*
160 * Submits the URB, with error handling.
161 */
Al Viro55016f12005-10-21 03:21:58 -0400162static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 int err = usb_submit_urb(urb, flags);
165 if (err < 0 && err != -ENODEV)
166 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
167 return err;
168}
169
170/*
171 * Error handling for URB completion functions.
172 */
173static int snd_usbmidi_urb_error(int status)
174{
Clemens Ladischc8846972005-08-02 15:26:52 +0200175 switch (status) {
176 /* manually unlinked, or device gone */
177 case -ENOENT:
178 case -ECONNRESET:
179 case -ESHUTDOWN:
180 case -ENODEV:
181 return -ENODEV;
182 /* errors that might occur during unplugging */
183 case -EPROTO: /* EHCI */
184 case -ETIMEDOUT: /* OHCI */
185 case -EILSEQ: /* UHCI */
186 return -EIO;
187 default:
188 snd_printk(KERN_ERR "urb status %d\n", status);
189 return 0; /* continue */
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193/*
194 * Receives a chunk of MIDI data.
195 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100196static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 uint8_t* data, int length)
198{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100199 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (!port->substream) {
202 snd_printd("unexpected port %d!\n", portidx);
203 return;
204 }
205 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
206 return;
207 snd_rawmidi_receive(port->substream, data, length);
208}
209
210#ifdef DUMP_PACKETS
211static void dump_urb(const char *type, const u8 *data, int length)
212{
213 snd_printk(KERN_DEBUG "%s packet: [", type);
214 for (; length > 0; ++data, --length)
215 printk(" %02x", *data);
216 printk(" ]\n");
217}
218#else
219#define dump_urb(type, data, length) /* nothing */
220#endif
221
222/*
223 * Processes the data read from the device.
224 */
225static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs)
226{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100227 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (urb->status == 0) {
230 dump_urb("received", urb->transfer_buffer, urb->actual_length);
231 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
232 urb->actual_length);
233 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200234 int err = snd_usbmidi_urb_error(urb->status);
235 if (err < 0) {
236 if (err != -ENODEV) {
237 ep->error_resubmit = 1;
238 mod_timer(&ep->umidi->error_timer,
239 jiffies + ERROR_DELAY_JIFFIES);
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200245 urb->dev = ep->umidi->chip->dev;
246 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs)
250{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100251 struct snd_usb_midi_out_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 spin_lock(&ep->buffer_lock);
254 ep->urb_active = 0;
255 spin_unlock(&ep->buffer_lock);
256 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200257 int err = snd_usbmidi_urb_error(urb->status);
258 if (err < 0) {
259 if (err != -ENODEV)
260 mod_timer(&ep->umidi->error_timer,
261 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 snd_usbmidi_do_output(ep);
266}
267
268/*
269 * This is called when some data should be transferred to the device
270 * (from one or more substreams).
271 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100272static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct urb* urb = ep->urb;
275 unsigned long flags;
276
277 spin_lock_irqsave(&ep->buffer_lock, flags);
278 if (ep->urb_active || ep->umidi->chip->shutdown) {
279 spin_unlock_irqrestore(&ep->buffer_lock, flags);
280 return;
281 }
282
283 urb->transfer_buffer_length = 0;
284 ep->umidi->usb_protocol_ops->output(ep);
285
286 if (urb->transfer_buffer_length > 0) {
287 dump_urb("sending", urb->transfer_buffer,
288 urb->transfer_buffer_length);
289 urb->dev = ep->umidi->chip->dev;
290 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
291 }
292 spin_unlock_irqrestore(&ep->buffer_lock, flags);
293}
294
295static void snd_usbmidi_out_tasklet(unsigned long data)
296{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100297 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 snd_usbmidi_do_output(ep);
300}
301
Clemens Ladischc8846972005-08-02 15:26:52 +0200302/* called after transfers had been interrupted due to some USB error */
303static void snd_usbmidi_error_timer(unsigned long data)
304{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100305 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladischc8846972005-08-02 15:26:52 +0200306 int i;
307
308 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100309 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200310 if (in && in->error_resubmit) {
311 in->error_resubmit = 0;
312 in->urb->dev = umidi->chip->dev;
313 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
314 }
315 if (umidi->endpoints[i].out)
316 snd_usbmidi_do_output(umidi->endpoints[i].out);
317 }
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100321static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 const void *data, int len)
323{
324 int err;
325 void *buf = kmalloc(len, GFP_KERNEL);
326 if (!buf)
327 return -ENOMEM;
328 memcpy(buf, data, len);
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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100342static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100355static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 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 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100401static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100495static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
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) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100502 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100534static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100542static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
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/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200568 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
570
Takashi Iwai86e07d32005-11-17 15:08:02 +0100571static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200572 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
575}
576
Takashi Iwai86e07d32005-11-17 15:08:02 +0100577static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
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
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200593static struct usb_protocol_ops snd_usbmidi_raw_ops = {
594 .input = snd_usbmidi_raw_input,
595 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596};
597
598/*
599 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
600 */
601
Takashi Iwai86e07d32005-11-17 15:08:02 +0100602static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100619static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100635static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 uint8_t* buffer, int buffer_length)
637{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200638 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* handle F5 at end of last buffer */
648 if (ep->seen_f5)
649 goto switch_port;
650
651 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* 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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100679static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
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;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100689 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
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 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200718 if (buf_free < ep->max_transfer && buf_free > 0) {
719 *buf = 0xff;
720 --buf_free;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100733static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100735 struct snd_usb_midi* umidi = substream->rmidi->private_data;
736 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100755static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 return 0;
758}
759
Takashi Iwai86e07d32005-11-17 15:08:02 +0100760static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100762 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100777static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 return 0;
780}
781
Takashi Iwai86e07d32005-11-17 15:08:02 +0100782static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 return 0;
785}
786
Takashi Iwai86e07d32005-11-17 15:08:02 +0100787static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100789 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (up)
792 set_bit(substream->number, &umidi->input_triggered);
793 else
794 clear_bit(substream->number, &umidi->input_triggered);
795}
796
Takashi Iwai86e07d32005-11-17 15:08:02 +0100797static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 .open = snd_usbmidi_output_open,
799 .close = snd_usbmidi_output_close,
800 .trigger = snd_usbmidi_output_trigger,
801};
802
Takashi Iwai86e07d32005-11-17 15:08:02 +0100803static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 .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 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100813static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200816 usb_buffer_free(ep->umidi->chip->dev,
817 ep->urb->transfer_buffer_length,
818 ep->urb->transfer_buffer,
819 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 usb_free_urb(ep->urb);
821 }
822 kfree(ep);
823}
824
825/*
826 * Creates an input endpoint.
827 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100828static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100832 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 void* buffer;
834 unsigned int pipe;
835 int length;
836
837 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200838 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 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);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200853 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
854 &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!buffer) {
856 snd_usbmidi_in_endpoint_delete(ep);
857 return -ENOMEM;
858 }
859 if (ep_info->in_interval)
Clemens Ladisch3527a002005-09-26 10:03:09 +0200860 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
861 length, snd_usbmidi_in_urb_complete, ep,
862 ep_info->in_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 else
Clemens Ladisch3527a002005-09-26 10:03:09 +0200864 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
865 length, snd_usbmidi_in_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200866 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 rep->in = ep;
869 return 0;
870}
871
872static unsigned int snd_usbmidi_count_bits(unsigned int x)
873{
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100874 unsigned int bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100876 for (bits = 0; x; ++bits)
877 x &= x - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return bits;
879}
880
881/*
882 * Frees an output endpoint.
883 * May be called when ep hasn't been initialized completely.
884 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100885static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200888 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
889 ep->urb->transfer_buffer,
890 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 usb_free_urb(ep->urb);
892 }
893 kfree(ep);
894}
895
896/*
897 * Creates an output endpoint, and initializes output ports.
898 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100899static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100903 struct snd_usb_midi_out_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 int i;
905 unsigned int pipe;
906 void* buffer;
907
908 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200909 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 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);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200922 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
923 GFP_KERNEL, &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 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,
Clemens Ladisch3527a002005-09-26 10:03:09 +0200929 ep->max_transfer, snd_usbmidi_out_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200930 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
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 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100951static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 int i;
954
955 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100956 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 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 */
Clemens Ladischee733392005-04-25 10:34:13 +0200968void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100970 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int i;
972
Takashi Iwai86e07d32005-11-17 15:08:02 +0100973 umidi = list_entry(p, struct snd_usb_midi, list);
Clemens Ladischc8846972005-08-02 15:26:52 +0200974 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100976 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +0200977 if (ep->out)
978 tasklet_kill(&ep->out->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 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
Takashi Iwai86e07d32005-11-17 15:08:02 +0100989static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100991 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 snd_usbmidi_free(umidi);
993}
994
Takashi Iwai86e07d32005-11-17 15:08:02 +0100995static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 int stream, int number)
997{
998 struct list_head* list;
999
1000 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001001 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 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 */
1013static struct {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001014 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int port;
1016 const char *name_format;
1017} snd_usbmidi_port_names[] = {
1018 /* Roland UA-100 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001019 { USB_ID(0x0582, 0x0000), 2, "%s Control" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /* Roland SC-8850 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001021 { USB_ID(0x0582, 0x0003), 0, "%s Part A" },
1022 { USB_ID(0x0582, 0x0003), 1, "%s Part B" },
1023 { USB_ID(0x0582, 0x0003), 2, "%s Part C" },
1024 { USB_ID(0x0582, 0x0003), 3, "%s Part D" },
1025 { USB_ID(0x0582, 0x0003), 4, "%s MIDI 1" },
1026 { USB_ID(0x0582, 0x0003), 5, "%s MIDI 2" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* Roland U-8 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001028 { USB_ID(0x0582, 0x0004), 0, "%s MIDI" },
1029 { USB_ID(0x0582, 0x0004), 1, "%s Control" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* Roland SC-8820 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001031 { USB_ID(0x0582, 0x0007), 0, "%s Part A" },
1032 { USB_ID(0x0582, 0x0007), 1, "%s Part B" },
1033 { USB_ID(0x0582, 0x0007), 2, "%s MIDI" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* Roland SK-500 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001035 { USB_ID(0x0582, 0x000b), 0, "%s Part A" },
1036 { USB_ID(0x0582, 0x000b), 1, "%s Part B" },
1037 { USB_ID(0x0582, 0x000b), 2, "%s MIDI" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Roland SC-D70 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001039 { USB_ID(0x0582, 0x000c), 0, "%s Part A" },
1040 { USB_ID(0x0582, 0x000c), 1, "%s Part B" },
1041 { USB_ID(0x0582, 0x000c), 2, "%s MIDI" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* Edirol UM-880 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001043 { USB_ID(0x0582, 0x0014), 8, "%s Control" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* Edirol SD-90 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001045 { USB_ID(0x0582, 0x0016), 0, "%s Part A" },
1046 { USB_ID(0x0582, 0x0016), 1, "%s Part B" },
1047 { USB_ID(0x0582, 0x0016), 2, "%s MIDI 1" },
1048 { USB_ID(0x0582, 0x0016), 3, "%s MIDI 2" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* Edirol UM-550 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001050 { USB_ID(0x0582, 0x0023), 5, "%s Control" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* Edirol SD-20 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001052 { USB_ID(0x0582, 0x0027), 0, "%s Part A" },
1053 { USB_ID(0x0582, 0x0027), 1, "%s Part B" },
1054 { USB_ID(0x0582, 0x0027), 2, "%s MIDI" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* Edirol SD-80 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001056 { USB_ID(0x0582, 0x0029), 0, "%s Part A" },
1057 { USB_ID(0x0582, 0x0029), 1, "%s Part B" },
1058 { USB_ID(0x0582, 0x0029), 2, "%s MIDI 1" },
1059 { USB_ID(0x0582, 0x0029), 3, "%s MIDI 2" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* Edirol UA-700 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001061 { USB_ID(0x0582, 0x002b), 0, "%s MIDI" },
1062 { USB_ID(0x0582, 0x002b), 1, "%s Control" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* Roland VariOS */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001064 { USB_ID(0x0582, 0x002f), 0, "%s MIDI" },
1065 { USB_ID(0x0582, 0x002f), 1, "%s External MIDI" },
1066 { USB_ID(0x0582, 0x002f), 2, "%s Sync" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* Edirol PCR */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001068 { USB_ID(0x0582, 0x0033), 0, "%s MIDI" },
1069 { USB_ID(0x0582, 0x0033), 1, "%s 1" },
1070 { USB_ID(0x0582, 0x0033), 2, "%s 2" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* BOSS GS-10 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001072 { USB_ID(0x0582, 0x003b), 0, "%s MIDI" },
1073 { USB_ID(0x0582, 0x003b), 1, "%s Control" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* Edirol UA-1000 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001075 { USB_ID(0x0582, 0x0044), 0, "%s MIDI" },
1076 { USB_ID(0x0582, 0x0044), 1, "%s Control" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* Edirol UR-80 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001078 { USB_ID(0x0582, 0x0048), 0, "%s MIDI" },
1079 { USB_ID(0x0582, 0x0048), 1, "%s 1" },
1080 { USB_ID(0x0582, 0x0048), 2, "%s 2" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /* Edirol PCR-A */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001082 { USB_ID(0x0582, 0x004d), 0, "%s MIDI" },
1083 { USB_ID(0x0582, 0x004d), 1, "%s 1" },
1084 { USB_ID(0x0582, 0x004d), 2, "%s 2" },
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001085 /* Edirol UM-3EX */
1086 { USB_ID(0x0582, 0x009a), 3, "%s Control" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 /* M-Audio MidiSport 8x8 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001088 { USB_ID(0x0763, 0x1031), 8, "%s Control" },
1089 { USB_ID(0x0763, 0x1033), 8, "%s Control" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /* MOTU Fastlane */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001091 { USB_ID(0x07fd, 0x0001), 0, "%s MIDI A" },
1092 { USB_ID(0x07fd, 0x0001), 1, "%s MIDI B" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001094 { USB_ID(0x086a, 0x0001), 8, "%s Broadcast" },
1095 { USB_ID(0x086a, 0x0002), 8, "%s Broadcast" },
1096 { USB_ID(0x086a, 0x0003), 4, "%s Broadcast" },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097};
1098
Takashi Iwai86e07d32005-11-17 15:08:02 +01001099static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001101 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 const char *name_format;
1105
Takashi Iwai86e07d32005-11-17 15:08:02 +01001106 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!substream) {
1108 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1109 return;
1110 }
1111
1112 /* TODO: read port name from jack descriptor */
1113 name_format = "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_names); ++i) {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001115 if (snd_usbmidi_port_names[i].id == umidi->chip->usb_id &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 snd_usbmidi_port_names[i].port == number) {
1117 name_format = snd_usbmidi_port_names[i].name_format;
1118 break;
1119 }
1120 }
1121 snprintf(substream->name, sizeof(substream->name),
1122 name_format, umidi->chip->card->shortname, number + 1);
1123
1124 *rsubstream = substream;
1125}
1126
1127/*
1128 * Creates the endpoints and their ports.
1129 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001130static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1131 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 int i, j, err;
1134 int out_ports = 0, in_ports = 0;
1135
1136 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1137 if (endpoints[i].out_cables) {
1138 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1139 &umidi->endpoints[i]);
1140 if (err < 0)
1141 return err;
1142 }
1143 if (endpoints[i].in_cables) {
1144 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1145 &umidi->endpoints[i]);
1146 if (err < 0)
1147 return err;
1148 }
1149
1150 for (j = 0; j < 0x10; ++j) {
1151 if (endpoints[i].out_cables & (1 << j)) {
1152 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1153 &umidi->endpoints[i].out->ports[j].substream);
1154 ++out_ports;
1155 }
1156 if (endpoints[i].in_cables & (1 << j)) {
1157 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1158 &umidi->endpoints[i].in->ports[j].substream);
1159 ++in_ports;
1160 }
1161 }
1162 }
1163 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1164 out_ports, in_ports);
1165 return 0;
1166}
1167
1168/*
1169 * Returns MIDIStreaming device capabilities.
1170 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001171static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1172 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 struct usb_interface* intf;
1175 struct usb_host_interface *hostif;
1176 struct usb_interface_descriptor* intfd;
1177 struct usb_ms_header_descriptor* ms_header;
1178 struct usb_host_endpoint *hostep;
1179 struct usb_endpoint_descriptor* ep;
1180 struct usb_ms_endpoint_descriptor* ms_ep;
1181 int i, epidx;
1182
1183 intf = umidi->iface;
1184 if (!intf)
1185 return -ENXIO;
1186 hostif = &intf->altsetting[0];
1187 intfd = get_iface_desc(hostif);
1188 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1189 if (hostif->extralen >= 7 &&
1190 ms_header->bLength >= 7 &&
1191 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1192 ms_header->bDescriptorSubtype == HEADER)
1193 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1194 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1195 else
1196 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1197
1198 epidx = 0;
1199 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1200 hostep = &hostif->endpoint[i];
1201 ep = get_ep_desc(hostep);
1202 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1203 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1204 continue;
1205 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1206 if (hostep->extralen < 4 ||
1207 ms_ep->bLength < 4 ||
1208 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1209 ms_ep->bDescriptorSubtype != MS_GENERAL)
1210 continue;
1211 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1212 if (endpoints[epidx].out_ep) {
1213 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1214 snd_printk(KERN_WARNING "too many endpoints\n");
1215 break;
1216 }
1217 }
1218 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1219 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1220 endpoints[epidx].out_interval = ep->bInterval;
1221 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1222 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1223 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1224 } else {
1225 if (endpoints[epidx].in_ep) {
1226 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1227 snd_printk(KERN_WARNING "too many endpoints\n");
1228 break;
1229 }
1230 }
1231 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1232 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1233 endpoints[epidx].in_interval = ep->bInterval;
1234 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1235 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1236 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1237 }
1238 }
1239 return 0;
1240}
1241
1242/*
1243 * On Roland devices, use the second alternate setting to be able to use
1244 * the interrupt input endpoint.
1245 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001246static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct usb_interface* intf;
1249 struct usb_host_interface *hostif;
1250 struct usb_interface_descriptor* intfd;
1251
1252 intf = umidi->iface;
1253 if (!intf || intf->num_altsetting != 2)
1254 return;
1255
1256 hostif = &intf->altsetting[1];
1257 intfd = get_iface_desc(hostif);
1258 if (intfd->bNumEndpoints != 2 ||
1259 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1260 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1261 return;
1262
1263 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1264 intfd->bAlternateSetting);
1265 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1266 intfd->bAlternateSetting);
1267}
1268
1269/*
1270 * Try to find any usable endpoints in the interface.
1271 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001272static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1273 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int max_endpoints)
1275{
1276 struct usb_interface* intf;
1277 struct usb_host_interface *hostif;
1278 struct usb_interface_descriptor* intfd;
1279 struct usb_endpoint_descriptor* epd;
1280 int i, out_eps = 0, in_eps = 0;
1281
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001282 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 snd_usbmidi_switch_roland_altsetting(umidi);
1284
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001285 if (endpoint[0].out_ep || endpoint[0].in_ep)
1286 return 0;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 intf = umidi->iface;
1289 if (!intf || intf->num_altsetting < 1)
1290 return -ENOENT;
1291 hostif = intf->cur_altsetting;
1292 intfd = get_iface_desc(hostif);
1293
1294 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1295 epd = get_endpoint(hostif, i);
1296 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1297 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1298 continue;
1299 if (out_eps < max_endpoints &&
1300 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1301 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1302 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1303 endpoint[out_eps].out_interval = epd->bInterval;
1304 ++out_eps;
1305 }
1306 if (in_eps < max_endpoints &&
1307 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1308 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1309 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1310 endpoint[in_eps].in_interval = epd->bInterval;
1311 ++in_eps;
1312 }
1313 }
1314 return (out_eps || in_eps) ? 0 : -ENOENT;
1315}
1316
1317/*
1318 * Detects the endpoints for one-port-per-endpoint protocols.
1319 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001320static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1321 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 int err, i;
1324
1325 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1326 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1327 if (endpoints[i].out_ep)
1328 endpoints[i].out_cables = 0x0001;
1329 if (endpoints[i].in_ep)
1330 endpoints[i].in_cables = 0x0001;
1331 }
1332 return err;
1333}
1334
1335/*
1336 * Detects the endpoints and ports of Yamaha devices.
1337 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001338static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1339 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct usb_interface* intf;
1342 struct usb_host_interface *hostif;
1343 struct usb_interface_descriptor* intfd;
1344 uint8_t* cs_desc;
1345
1346 intf = umidi->iface;
1347 if (!intf)
1348 return -ENOENT;
1349 hostif = intf->altsetting;
1350 intfd = get_iface_desc(hostif);
1351 if (intfd->bNumEndpoints < 1)
1352 return -ENOENT;
1353
1354 /*
1355 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1356 * necessarily with any useful contents. So simply count 'em.
1357 */
1358 for (cs_desc = hostif->extra;
1359 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1360 cs_desc += cs_desc[0]) {
1361 if (cs_desc[1] == CS_AUDIO_INTERFACE) {
1362 if (cs_desc[2] == MIDI_IN_JACK)
1363 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1364 else if (cs_desc[2] == MIDI_OUT_JACK)
1365 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1366 }
1367 }
1368 if (!endpoint->in_cables && !endpoint->out_cables)
1369 return -ENOENT;
1370
1371 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1372}
1373
1374/*
1375 * Creates the endpoints and their ports for Midiman devices.
1376 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001377static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1378 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001380 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 struct usb_interface* intf;
1382 struct usb_host_interface *hostif;
1383 struct usb_interface_descriptor* intfd;
1384 struct usb_endpoint_descriptor* epd;
1385 int cable, err;
1386
1387 intf = umidi->iface;
1388 if (!intf)
1389 return -ENOENT;
1390 hostif = intf->altsetting;
1391 intfd = get_iface_desc(hostif);
1392 /*
1393 * The various MidiSport devices have more or less random endpoint
1394 * numbers, so we have to identify the endpoints by their index in
1395 * the descriptor array, like the driver for that other OS does.
1396 *
1397 * There is one interrupt input endpoint for all input ports, one
1398 * bulk output endpoint for even-numbered ports, and one for odd-
1399 * numbered ports. Both bulk output endpoints have corresponding
1400 * input bulk endpoints (at indices 1 and 3) which aren't used.
1401 */
1402 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1403 snd_printdd(KERN_ERR "not enough endpoints\n");
1404 return -ENOENT;
1405 }
1406
1407 epd = get_endpoint(hostif, 0);
1408 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1409 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1410 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1411 return -ENXIO;
1412 }
1413 epd = get_endpoint(hostif, 2);
1414 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1415 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1416 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1417 return -ENXIO;
1418 }
1419 if (endpoint->out_cables > 0x0001) {
1420 epd = get_endpoint(hostif, 4);
1421 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1422 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1423 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1424 return -ENXIO;
1425 }
1426 }
1427
1428 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1429 ep_info.out_cables = endpoint->out_cables & 0x5555;
1430 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1431 if (err < 0)
1432 return err;
1433
1434 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1435 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1436 ep_info.in_cables = endpoint->in_cables;
1437 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1438 if (err < 0)
1439 return err;
1440
1441 if (endpoint->out_cables > 0x0001) {
1442 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1443 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1444 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1445 if (err < 0)
1446 return err;
1447 }
1448
1449 for (cable = 0; cable < 0x10; ++cable) {
1450 if (endpoint->out_cables & (1 << cable))
1451 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1452 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1453 if (endpoint->in_cables & (1 << cable))
1454 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1455 &umidi->endpoints[0].in->ports[cable].substream);
1456 }
1457 return 0;
1458}
1459
Takashi Iwai86e07d32005-11-17 15:08:02 +01001460static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int out_ports, int in_ports)
1462{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001463 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 int err;
1465
1466 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1467 umidi->chip->next_midi_device++,
1468 out_ports, in_ports, &rmidi);
1469 if (err < 0)
1470 return err;
1471 strcpy(rmidi->name, umidi->chip->card->shortname);
1472 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1473 SNDRV_RAWMIDI_INFO_INPUT |
1474 SNDRV_RAWMIDI_INFO_DUPLEX;
1475 rmidi->private_data = umidi;
1476 rmidi->private_free = snd_usbmidi_rawmidi_free;
1477 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1478 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1479
1480 umidi->rmidi = rmidi;
1481 return 0;
1482}
1483
1484/*
1485 * Temporarily stop input.
1486 */
1487void snd_usbmidi_input_stop(struct list_head* p)
1488{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001489 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 int i;
1491
Takashi Iwai86e07d32005-11-17 15:08:02 +01001492 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001494 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (ep->in)
1496 usb_kill_urb(ep->in->urb);
1497 }
1498}
1499
Takashi Iwai86e07d32005-11-17 15:08:02 +01001500static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 if (ep) {
1503 struct urb* urb = ep->urb;
1504 urb->dev = ep->umidi->chip->dev;
1505 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1506 }
1507}
1508
1509/*
1510 * Resume input after a call to snd_usbmidi_input_stop().
1511 */
1512void snd_usbmidi_input_start(struct list_head* p)
1513{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001514 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 int i;
1516
Takashi Iwai86e07d32005-11-17 15:08:02 +01001517 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1519 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1520}
1521
1522/*
1523 * Creates and registers everything needed for a MIDI streaming interface.
1524 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001525int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 struct usb_interface* iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001527 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001529 struct snd_usb_midi* umidi;
1530 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 int out_ports, in_ports;
1532 int i, err;
1533
Takashi Iwai561b2202005-09-09 14:22:34 +02001534 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (!umidi)
1536 return -ENOMEM;
1537 umidi->chip = chip;
1538 umidi->iface = iface;
1539 umidi->quirk = quirk;
1540 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001541 init_timer(&umidi->error_timer);
1542 umidi->error_timer.function = snd_usbmidi_error_timer;
1543 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 /* detect the endpoint(s) to use */
1546 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001547 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1548 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd1bda042005-09-14 08:36:03 +02001550 break;
1551 case QUIRK_MIDI_FIXED_ENDPOINT:
1552 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001553 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001554 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1555 break;
1556 case QUIRK_MIDI_YAMAHA:
1557 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1558 break;
1559 case QUIRK_MIDI_MIDIMAN:
1560 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1561 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001562 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001563 err = 0;
1564 break;
1565 case QUIRK_MIDI_NOVATION:
1566 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1567 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1568 break;
1569 case QUIRK_MIDI_RAW:
1570 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1571 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1572 break;
1573 case QUIRK_MIDI_EMAGIC:
1574 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1575 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001576 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001577 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1578 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01001579 case QUIRK_MIDI_CME:
Clemens Ladischd1bda042005-09-14 08:36:03 +02001580 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1581 break;
1582 default:
1583 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1584 err = -ENXIO;
1585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 if (err < 0) {
1588 kfree(umidi);
1589 return err;
1590 }
1591
1592 /* create rawmidi device */
1593 out_ports = 0;
1594 in_ports = 0;
1595 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1596 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1597 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1598 }
1599 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1600 if (err < 0) {
1601 kfree(umidi);
1602 return err;
1603 }
1604
1605 /* create endpoint/port structures */
1606 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1607 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1608 else
1609 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1610 if (err < 0) {
1611 snd_usbmidi_free(umidi);
1612 return err;
1613 }
1614
1615 list_add(&umidi->list, &umidi->chip->midi_list);
1616
1617 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1618 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1619 return 0;
1620}
1621
1622EXPORT_SYMBOL(snd_usb_create_midi_interface);
1623EXPORT_SYMBOL(snd_usbmidi_input_stop);
1624EXPORT_SYMBOL(snd_usbmidi_input_start);
1625EXPORT_SYMBOL(snd_usbmidi_disconnect);