blob: 5105b6b057484ffb0408e509e90f70a45e3d799e [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>
Clemens Ladischa7b928a2006-05-02 16:22:12 +020051#include <sound/asequencer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "usbaudio.h"
53
54
55/*
56 * define this to log all USB packets
57 */
58/* #define DUMP_PACKETS */
59
Clemens Ladischc8846972005-08-02 15:26:52 +020060/*
61 * how long to wait after some USB errors, so that khubd can disconnect() us
62 * without too many spurious errors
63 */
64#define ERROR_DELAY_JIFFIES (HZ / 10)
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
68MODULE_DESCRIPTION("USB Audio/MIDI helper module");
69MODULE_LICENSE("Dual BSD/GPL");
70
71
72struct usb_ms_header_descriptor {
73 __u8 bLength;
74 __u8 bDescriptorType;
75 __u8 bDescriptorSubtype;
76 __u8 bcdMSC[2];
77 __le16 wTotalLength;
78} __attribute__ ((packed));
79
80struct usb_ms_endpoint_descriptor {
81 __u8 bLength;
82 __u8 bDescriptorType;
83 __u8 bDescriptorSubtype;
84 __u8 bNumEmbMIDIJack;
85 __u8 baAssocJackID[0];
86} __attribute__ ((packed));
87
Takashi Iwai86e07d32005-11-17 15:08:02 +010088struct snd_usb_midi_in_endpoint;
89struct snd_usb_midi_out_endpoint;
90struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +010093 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
94 void (*output)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Takashi Iwai86e07d32005-11-17 15:08:02 +010096 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
97 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
100struct snd_usb_midi {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100101 struct snd_usb_audio *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100103 const struct snd_usb_audio_quirk *quirk;
104 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct usb_protocol_ops* usb_protocol_ops;
106 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200107 struct timer_list error_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100110 struct snd_usb_midi_out_endpoint *out;
111 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 } endpoints[MIDI_MAX_ENDPOINTS];
113 unsigned long input_triggered;
114};
115
116struct snd_usb_midi_out_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100117 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct urb* urb;
119 int urb_active;
120 int max_transfer; /* size of urb buffer */
121 struct tasklet_struct tasklet;
122
123 spinlock_t buffer_lock;
124
125 struct usbmidi_out_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100126 struct snd_usb_midi_out_endpoint* ep;
127 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int active;
129 uint8_t cable; /* cable number << 4 */
130 uint8_t state;
131#define STATE_UNKNOWN 0
132#define STATE_1PARAM 1
133#define STATE_2PARAM_1 2
134#define STATE_2PARAM_2 3
135#define STATE_SYSEX_0 4
136#define STATE_SYSEX_1 5
137#define STATE_SYSEX_2 6
138 uint8_t data[2];
139 } ports[0x10];
140 int current_port;
141};
142
143struct snd_usb_midi_in_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100144 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct urb* urb;
146 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100147 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200149 u8 seen_f5;
150 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int current_port;
152};
153
Takashi Iwai86e07d32005-11-17 15:08:02 +0100154static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156static const uint8_t snd_usbmidi_cin_length[] = {
157 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
158};
159
160/*
161 * Submits the URB, with error handling.
162 */
Al Viro55016f12005-10-21 03:21:58 -0400163static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 int err = usb_submit_urb(urb, flags);
166 if (err < 0 && err != -ENODEV)
167 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
168 return err;
169}
170
171/*
172 * Error handling for URB completion functions.
173 */
174static int snd_usbmidi_urb_error(int status)
175{
Clemens Ladischc8846972005-08-02 15:26:52 +0200176 switch (status) {
177 /* manually unlinked, or device gone */
178 case -ENOENT:
179 case -ECONNRESET:
180 case -ESHUTDOWN:
181 case -ENODEV:
182 return -ENODEV;
183 /* errors that might occur during unplugging */
184 case -EPROTO: /* EHCI */
185 case -ETIMEDOUT: /* OHCI */
186 case -EILSEQ: /* UHCI */
187 return -EIO;
188 default:
189 snd_printk(KERN_ERR "urb status %d\n", status);
190 return 0; /* continue */
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * Receives a chunk of MIDI data.
196 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100197static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 uint8_t* data, int length)
199{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100200 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (!port->substream) {
203 snd_printd("unexpected port %d!\n", portidx);
204 return;
205 }
206 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
207 return;
208 snd_rawmidi_receive(port->substream, data, length);
209}
210
211#ifdef DUMP_PACKETS
212static void dump_urb(const char *type, const u8 *data, int length)
213{
214 snd_printk(KERN_DEBUG "%s packet: [", type);
215 for (; length > 0; ++data, --length)
216 printk(" %02x", *data);
217 printk(" ]\n");
218}
219#else
220#define dump_urb(type, data, length) /* nothing */
221#endif
222
223/*
224 * Processes the data read from the device.
225 */
226static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs)
227{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100228 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (urb->status == 0) {
231 dump_urb("received", urb->transfer_buffer, urb->actual_length);
232 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
233 urb->actual_length);
234 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200235 int err = snd_usbmidi_urb_error(urb->status);
236 if (err < 0) {
237 if (err != -ENODEV) {
238 ep->error_resubmit = 1;
239 mod_timer(&ep->umidi->error_timer,
240 jiffies + ERROR_DELAY_JIFFIES);
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200246 urb->dev = ep->umidi->chip->dev;
247 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs)
251{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100252 struct snd_usb_midi_out_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 spin_lock(&ep->buffer_lock);
255 ep->urb_active = 0;
256 spin_unlock(&ep->buffer_lock);
257 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200258 int err = snd_usbmidi_urb_error(urb->status);
259 if (err < 0) {
260 if (err != -ENODEV)
261 mod_timer(&ep->umidi->error_timer,
262 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 snd_usbmidi_do_output(ep);
267}
268
269/*
270 * This is called when some data should be transferred to the device
271 * (from one or more substreams).
272 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100273static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct urb* urb = ep->urb;
276 unsigned long flags;
277
278 spin_lock_irqsave(&ep->buffer_lock, flags);
279 if (ep->urb_active || ep->umidi->chip->shutdown) {
280 spin_unlock_irqrestore(&ep->buffer_lock, flags);
281 return;
282 }
283
284 urb->transfer_buffer_length = 0;
285 ep->umidi->usb_protocol_ops->output(ep);
286
287 if (urb->transfer_buffer_length > 0) {
288 dump_urb("sending", urb->transfer_buffer,
289 urb->transfer_buffer_length);
290 urb->dev = ep->umidi->chip->dev;
291 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
292 }
293 spin_unlock_irqrestore(&ep->buffer_lock, flags);
294}
295
296static void snd_usbmidi_out_tasklet(unsigned long data)
297{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100298 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 snd_usbmidi_do_output(ep);
301}
302
Clemens Ladischc8846972005-08-02 15:26:52 +0200303/* called after transfers had been interrupted due to some USB error */
304static void snd_usbmidi_error_timer(unsigned long data)
305{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100306 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladischc8846972005-08-02 15:26:52 +0200307 int i;
308
309 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100310 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200311 if (in && in->error_resubmit) {
312 in->error_resubmit = 0;
313 in->urb->dev = umidi->chip->dev;
314 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
315 }
316 if (umidi->endpoints[i].out)
317 snd_usbmidi_do_output(umidi->endpoints[i].out);
318 }
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100322static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 const void *data, int len)
324{
325 int err;
326 void *buf = kmalloc(len, GFP_KERNEL);
327 if (!buf)
328 return -ENOMEM;
329 memcpy(buf, data, len);
330 dump_urb("sending", buf, len);
331 err = usb_bulk_msg(ep->umidi->chip->dev, ep->urb->pipe, buf, len,
332 NULL, 250);
333 kfree(buf);
334 return err;
335}
336
337/*
338 * Standard USB MIDI protocol: see the spec.
339 * Midiman protocol: like the standard protocol, but the control byte is the
340 * fourth byte in each packet, and uses length instead of CIN.
341 */
342
Takashi Iwai86e07d32005-11-17 15:08:02 +0100343static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 uint8_t* buffer, int buffer_length)
345{
346 int i;
347
348 for (i = 0; i + 3 < buffer_length; i += 4)
349 if (buffer[i] != 0) {
350 int cable = buffer[i] >> 4;
351 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
352 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
353 }
354}
355
Takashi Iwai86e07d32005-11-17 15:08:02 +0100356static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 uint8_t* buffer, int buffer_length)
358{
359 int i;
360
361 for (i = 0; i + 3 < buffer_length; i += 4)
362 if (buffer[i + 3] != 0) {
363 int port = buffer[i + 3] >> 4;
364 int length = buffer[i + 3] & 3;
365 snd_usbmidi_input_data(ep, port, &buffer[i], length);
366 }
367}
368
369/*
370 * Adds one USB MIDI packet to the output buffer.
371 */
372static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
373 uint8_t p1, uint8_t p2, uint8_t p3)
374{
375
376 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
377 buf[0] = p0;
378 buf[1] = p1;
379 buf[2] = p2;
380 buf[3] = p3;
381 urb->transfer_buffer_length += 4;
382}
383
384/*
385 * Adds one Midiman packet to the output buffer.
386 */
387static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
388 uint8_t p1, uint8_t p2, uint8_t p3)
389{
390
391 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
392 buf[0] = p1;
393 buf[1] = p2;
394 buf[2] = p3;
395 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
396 urb->transfer_buffer_length += 4;
397}
398
399/*
400 * Converts MIDI commands to USB MIDI packets.
401 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100402static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 uint8_t b, struct urb* urb)
404{
405 uint8_t p0 = port->cable;
406 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
407 port->ep->umidi->usb_protocol_ops->output_packet;
408
409 if (b >= 0xf8) {
410 output_packet(urb, p0 | 0x0f, b, 0, 0);
411 } else if (b >= 0xf0) {
412 switch (b) {
413 case 0xf0:
414 port->data[0] = b;
415 port->state = STATE_SYSEX_1;
416 break;
417 case 0xf1:
418 case 0xf3:
419 port->data[0] = b;
420 port->state = STATE_1PARAM;
421 break;
422 case 0xf2:
423 port->data[0] = b;
424 port->state = STATE_2PARAM_1;
425 break;
426 case 0xf4:
427 case 0xf5:
428 port->state = STATE_UNKNOWN;
429 break;
430 case 0xf6:
431 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
432 port->state = STATE_UNKNOWN;
433 break;
434 case 0xf7:
435 switch (port->state) {
436 case STATE_SYSEX_0:
437 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
438 break;
439 case STATE_SYSEX_1:
440 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
441 break;
442 case STATE_SYSEX_2:
443 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
444 break;
445 }
446 port->state = STATE_UNKNOWN;
447 break;
448 }
449 } else if (b >= 0x80) {
450 port->data[0] = b;
451 if (b >= 0xc0 && b <= 0xdf)
452 port->state = STATE_1PARAM;
453 else
454 port->state = STATE_2PARAM_1;
455 } else { /* b < 0x80 */
456 switch (port->state) {
457 case STATE_1PARAM:
458 if (port->data[0] < 0xf0) {
459 p0 |= port->data[0] >> 4;
460 } else {
461 p0 |= 0x02;
462 port->state = STATE_UNKNOWN;
463 }
464 output_packet(urb, p0, port->data[0], b, 0);
465 break;
466 case STATE_2PARAM_1:
467 port->data[1] = b;
468 port->state = STATE_2PARAM_2;
469 break;
470 case STATE_2PARAM_2:
471 if (port->data[0] < 0xf0) {
472 p0 |= port->data[0] >> 4;
473 port->state = STATE_2PARAM_1;
474 } else {
475 p0 |= 0x03;
476 port->state = STATE_UNKNOWN;
477 }
478 output_packet(urb, p0, port->data[0], port->data[1], b);
479 break;
480 case STATE_SYSEX_0:
481 port->data[0] = b;
482 port->state = STATE_SYSEX_1;
483 break;
484 case STATE_SYSEX_1:
485 port->data[1] = b;
486 port->state = STATE_SYSEX_2;
487 break;
488 case STATE_SYSEX_2:
489 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
490 port->state = STATE_SYSEX_0;
491 break;
492 }
493 }
494}
495
Takashi Iwai86e07d32005-11-17 15:08:02 +0100496static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct urb* urb = ep->urb;
499 int p;
500
501 /* FIXME: lower-numbered ports can starve higher-numbered ports */
502 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100503 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!port->active)
505 continue;
506 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
507 uint8_t b;
508 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
509 port->active = 0;
510 break;
511 }
512 snd_usbmidi_transmit_byte(port, b, urb);
513 }
514 }
515}
516
517static struct usb_protocol_ops snd_usbmidi_standard_ops = {
518 .input = snd_usbmidi_standard_input,
519 .output = snd_usbmidi_standard_output,
520 .output_packet = snd_usbmidi_output_standard_packet,
521};
522
523static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
524 .input = snd_usbmidi_midiman_input,
525 .output = snd_usbmidi_standard_output,
526 .output_packet = snd_usbmidi_output_midiman_packet,
527};
528
529/*
530 * Novation USB MIDI protocol: number of data bytes is in the first byte
531 * (when receiving) (+1!) or in the second byte (when sending); data begins
532 * at the third byte.
533 */
534
Takashi Iwai86e07d32005-11-17 15:08:02 +0100535static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 uint8_t* buffer, int buffer_length)
537{
538 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
539 return;
540 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
541}
542
Takashi Iwai86e07d32005-11-17 15:08:02 +0100543static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 uint8_t* transfer_buffer;
546 int count;
547
548 if (!ep->ports[0].active)
549 return;
550 transfer_buffer = ep->urb->transfer_buffer;
551 count = snd_rawmidi_transmit(ep->ports[0].substream,
552 &transfer_buffer[2],
553 ep->max_transfer - 2);
554 if (count < 1) {
555 ep->ports[0].active = 0;
556 return;
557 }
558 transfer_buffer[0] = 0;
559 transfer_buffer[1] = count;
560 ep->urb->transfer_buffer_length = 2 + count;
561}
562
563static struct usb_protocol_ops snd_usbmidi_novation_ops = {
564 .input = snd_usbmidi_novation_input,
565 .output = snd_usbmidi_novation_output,
566};
567
568/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200569 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
571
Takashi Iwai86e07d32005-11-17 15:08:02 +0100572static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200573 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
576}
577
Takashi Iwai86e07d32005-11-17 15:08:02 +0100578static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 int count;
581
582 if (!ep->ports[0].active)
583 return;
584 count = snd_rawmidi_transmit(ep->ports[0].substream,
585 ep->urb->transfer_buffer,
586 ep->max_transfer);
587 if (count < 1) {
588 ep->ports[0].active = 0;
589 return;
590 }
591 ep->urb->transfer_buffer_length = count;
592}
593
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200594static struct usb_protocol_ops snd_usbmidi_raw_ops = {
595 .input = snd_usbmidi_raw_input,
596 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597};
598
599/*
600 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
601 */
602
Takashi Iwai86e07d32005-11-17 15:08:02 +0100603static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 static const u8 init_data[] = {
606 /* initialization magic: "get version" */
607 0xf0,
608 0x00, 0x20, 0x31, /* Emagic */
609 0x64, /* Unitor8 */
610 0x0b, /* version number request */
611 0x00, /* command version */
612 0x00, /* EEPROM, box 0 */
613 0xf7
614 };
615 send_bulk_static_data(ep, init_data, sizeof(init_data));
616 /* while we're at it, pour on more magic */
617 send_bulk_static_data(ep, init_data, sizeof(init_data));
618}
619
Takashi Iwai86e07d32005-11-17 15:08:02 +0100620static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 static const u8 finish_data[] = {
623 /* switch to patch mode with last preset */
624 0xf0,
625 0x00, 0x20, 0x31, /* Emagic */
626 0x64, /* Unitor8 */
627 0x10, /* patch switch command */
628 0x00, /* command version */
629 0x7f, /* to all boxes */
630 0x40, /* last preset in EEPROM */
631 0xf7
632 };
633 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
634}
635
Takashi Iwai86e07d32005-11-17 15:08:02 +0100636static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 uint8_t* buffer, int buffer_length)
638{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200639 int i;
640
641 /* FF indicates end of valid data */
642 for (i = 0; i < buffer_length; ++i)
643 if (buffer[i] == 0xff) {
644 buffer_length = i;
645 break;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* handle F5 at end of last buffer */
649 if (ep->seen_f5)
650 goto switch_port;
651
652 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* determine size of data until next F5 */
654 for (i = 0; i < buffer_length; ++i)
655 if (buffer[i] == 0xf5)
656 break;
657 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
658 buffer += i;
659 buffer_length -= i;
660
661 if (buffer_length <= 0)
662 break;
663 /* assert(buffer[0] == 0xf5); */
664 ep->seen_f5 = 1;
665 ++buffer;
666 --buffer_length;
667
668 switch_port:
669 if (buffer_length <= 0)
670 break;
671 if (buffer[0] < 0x80) {
672 ep->current_port = (buffer[0] - 1) & 15;
673 ++buffer;
674 --buffer_length;
675 }
676 ep->seen_f5 = 0;
677 }
678}
679
Takashi Iwai86e07d32005-11-17 15:08:02 +0100680static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 int port0 = ep->current_port;
683 uint8_t* buf = ep->urb->transfer_buffer;
684 int buf_free = ep->max_transfer;
685 int length, i;
686
687 for (i = 0; i < 0x10; ++i) {
688 /* round-robin, starting at the last current port */
689 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100690 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (!port->active)
693 continue;
694 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
695 port->active = 0;
696 continue;
697 }
698
699 if (portnum != ep->current_port) {
700 if (buf_free < 2)
701 break;
702 ep->current_port = portnum;
703 buf[0] = 0xf5;
704 buf[1] = (portnum + 1) & 15;
705 buf += 2;
706 buf_free -= 2;
707 }
708
709 if (buf_free < 1)
710 break;
711 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
712 if (length > 0) {
713 buf += length;
714 buf_free -= length;
715 if (buf_free < 1)
716 break;
717 }
718 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200719 if (buf_free < ep->max_transfer && buf_free > 0) {
720 *buf = 0xff;
721 --buf_free;
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
724}
725
726static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
727 .input = snd_usbmidi_emagic_input,
728 .output = snd_usbmidi_emagic_output,
729 .init_out_endpoint = snd_usbmidi_emagic_init_out,
730 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
731};
732
733
Takashi Iwai86e07d32005-11-17 15:08:02 +0100734static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100736 struct snd_usb_midi* umidi = substream->rmidi->private_data;
737 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int i, j;
739
740 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
741 if (umidi->endpoints[i].out)
742 for (j = 0; j < 0x10; ++j)
743 if (umidi->endpoints[i].out->ports[j].substream == substream) {
744 port = &umidi->endpoints[i].out->ports[j];
745 break;
746 }
747 if (!port) {
748 snd_BUG();
749 return -ENXIO;
750 }
751 substream->runtime->private_data = port;
752 port->state = STATE_UNKNOWN;
753 return 0;
754}
755
Takashi Iwai86e07d32005-11-17 15:08:02 +0100756static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 return 0;
759}
760
Takashi Iwai86e07d32005-11-17 15:08:02 +0100761static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100763 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 port->active = up;
766 if (up) {
767 if (port->ep->umidi->chip->shutdown) {
768 /* gobble up remaining bytes to prevent wait in
769 * snd_rawmidi_drain_output */
770 while (!snd_rawmidi_transmit_empty(substream))
771 snd_rawmidi_transmit_ack(substream, 1);
772 return;
773 }
774 tasklet_hi_schedule(&port->ep->tasklet);
775 }
776}
777
Takashi Iwai86e07d32005-11-17 15:08:02 +0100778static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 return 0;
781}
782
Takashi Iwai86e07d32005-11-17 15:08:02 +0100783static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 return 0;
786}
787
Takashi Iwai86e07d32005-11-17 15:08:02 +0100788static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100790 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (up)
793 set_bit(substream->number, &umidi->input_triggered);
794 else
795 clear_bit(substream->number, &umidi->input_triggered);
796}
797
Takashi Iwai86e07d32005-11-17 15:08:02 +0100798static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .open = snd_usbmidi_output_open,
800 .close = snd_usbmidi_output_close,
801 .trigger = snd_usbmidi_output_trigger,
802};
803
Takashi Iwai86e07d32005-11-17 15:08:02 +0100804static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 .open = snd_usbmidi_input_open,
806 .close = snd_usbmidi_input_close,
807 .trigger = snd_usbmidi_input_trigger
808};
809
810/*
811 * Frees an input endpoint.
812 * May be called when ep hasn't been initialized completely.
813 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100814static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200817 usb_buffer_free(ep->umidi->chip->dev,
818 ep->urb->transfer_buffer_length,
819 ep->urb->transfer_buffer,
820 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 usb_free_urb(ep->urb);
822 }
823 kfree(ep);
824}
825
826/*
827 * Creates an input endpoint.
828 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100829static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
830 struct snd_usb_midi_endpoint_info* ep_info,
831 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100833 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 void* buffer;
835 unsigned int pipe;
836 int length;
837
838 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200839 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (!ep)
841 return -ENOMEM;
842 ep->umidi = umidi;
843
844 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
845 if (!ep->urb) {
846 snd_usbmidi_in_endpoint_delete(ep);
847 return -ENOMEM;
848 }
849 if (ep_info->in_interval)
850 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
851 else
852 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
853 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200854 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
855 &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!buffer) {
857 snd_usbmidi_in_endpoint_delete(ep);
858 return -ENOMEM;
859 }
860 if (ep_info->in_interval)
Clemens Ladisch3527a002005-09-26 10:03:09 +0200861 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
862 length, snd_usbmidi_in_urb_complete, ep,
863 ep_info->in_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 else
Clemens Ladisch3527a002005-09-26 10:03:09 +0200865 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
866 length, snd_usbmidi_in_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200867 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 rep->in = ep;
870 return 0;
871}
872
873static unsigned int snd_usbmidi_count_bits(unsigned int x)
874{
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100875 unsigned int bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100877 for (bits = 0; x; ++bits)
878 x &= x - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return bits;
880}
881
882/*
883 * Frees an output endpoint.
884 * May be called when ep hasn't been initialized completely.
885 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100886static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200889 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
890 ep->urb->transfer_buffer,
891 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 usb_free_urb(ep->urb);
893 }
894 kfree(ep);
895}
896
897/*
898 * Creates an output endpoint, and initializes output ports.
899 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100900static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
901 struct snd_usb_midi_endpoint_info* ep_info,
902 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100904 struct snd_usb_midi_out_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 int i;
906 unsigned int pipe;
907 void* buffer;
908
909 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200910 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (!ep)
912 return -ENOMEM;
913 ep->umidi = umidi;
914
915 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
916 if (!ep->urb) {
917 snd_usbmidi_out_endpoint_delete(ep);
918 return -ENOMEM;
919 }
920 /* we never use interrupt output pipes */
921 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
922 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200923 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
924 GFP_KERNEL, &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (!buffer) {
926 snd_usbmidi_out_endpoint_delete(ep);
927 return -ENOMEM;
928 }
929 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
Clemens Ladisch3527a002005-09-26 10:03:09 +0200930 ep->max_transfer, snd_usbmidi_out_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200931 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 spin_lock_init(&ep->buffer_lock);
934 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
935
936 for (i = 0; i < 0x10; ++i)
937 if (ep_info->out_cables & (1 << i)) {
938 ep->ports[i].ep = ep;
939 ep->ports[i].cable = i << 4;
940 }
941
942 if (umidi->usb_protocol_ops->init_out_endpoint)
943 umidi->usb_protocol_ops->init_out_endpoint(ep);
944
945 rep->out = ep;
946 return 0;
947}
948
949/*
950 * Frees everything.
951 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100952static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 int i;
955
956 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100957 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (ep->out)
959 snd_usbmidi_out_endpoint_delete(ep->out);
960 if (ep->in)
961 snd_usbmidi_in_endpoint_delete(ep->in);
962 }
963 kfree(umidi);
964}
965
966/*
967 * Unlinks all URBs (must be done before the usb_device is deleted).
968 */
Clemens Ladischee733392005-04-25 10:34:13 +0200969void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100971 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int i;
973
Takashi Iwai86e07d32005-11-17 15:08:02 +0100974 umidi = list_entry(p, struct snd_usb_midi, list);
Clemens Ladischc8846972005-08-02 15:26:52 +0200975 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100977 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +0200978 if (ep->out)
979 tasklet_kill(&ep->out->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (ep->out && ep->out->urb) {
981 usb_kill_urb(ep->out->urb);
982 if (umidi->usb_protocol_ops->finish_out_endpoint)
983 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
984 }
985 if (ep->in && ep->in->urb)
986 usb_kill_urb(ep->in->urb);
987 }
988}
989
Takashi Iwai86e07d32005-11-17 15:08:02 +0100990static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100992 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 snd_usbmidi_free(umidi);
994}
995
Takashi Iwai86e07d32005-11-17 15:08:02 +0100996static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int stream, int number)
998{
999 struct list_head* list;
1000
1001 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001002 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (substream->number == number)
1004 return substream;
1005 }
1006 return NULL;
1007}
1008
1009/*
1010 * This list specifies names for ports that do not fit into the standard
1011 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1012 * such as internal control or synthesizer ports.
1013 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001014static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001015 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001016 short int port;
1017 short int voices;
1018 const char *name;
1019 unsigned int seq_flags;
1020} snd_usbmidi_port_info[] = {
1021#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1022 { .id = USB_ID(vendor, product), \
1023 .port = num, .voices = voices_, \
1024 .name = name_, .seq_flags = flags }
1025#define EXTERNAL_PORT(vendor, product, num, name) \
1026 PORT_INFO(vendor, product, num, name, 0, \
1027 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1028 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1029 SNDRV_SEQ_PORT_TYPE_PORT)
1030#define CONTROL_PORT(vendor, product, num, name) \
1031 PORT_INFO(vendor, product, num, name, 0, \
1032 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1033 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1034#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1035 PORT_INFO(vendor, product, num, name, voices, \
1036 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1037 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1038 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1039 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1040 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1041 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1042 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1043#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1044 PORT_INFO(vendor, product, num, name, voices, \
1045 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1046 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1047 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1048 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1049 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1050 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1051 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1052 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001054 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001056 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1057 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1058 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1059 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1060 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1061 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001063 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1064 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001066 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1067 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1068 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001070 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1071 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1072 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001074 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1075 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1076 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001078 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001080 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1081 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1082 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1083 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001085 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001087 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1088 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1089 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001091 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1092 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1093 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1094 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001096 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1097 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001099 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1100 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1101 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001103 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1104 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1105 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001107 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1108 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001110 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1111 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001113 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1114 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1115 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001117 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1118 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1119 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001120 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001121 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001123 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1124 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001126 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1127 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001129 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1130 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1131 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132};
1133
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001134static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1135{
1136 int i;
1137
1138 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1139 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1140 snd_usbmidi_port_info[i].port == number)
1141 return &snd_usbmidi_port_info[i];
1142 }
1143 return NULL;
1144}
1145
1146static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1147 struct snd_seq_port_info *seq_port_info)
1148{
1149 struct snd_usb_midi *umidi = rmidi->private_data;
1150 struct port_info *port_info;
1151
1152 /* TODO: read port flags from descriptors */
1153 port_info = find_port_info(umidi, number);
1154 if (port_info) {
1155 seq_port_info->type = port_info->seq_flags;
1156 seq_port_info->midi_voices = port_info->voices;
1157 }
1158}
1159
Takashi Iwai86e07d32005-11-17 15:08:02 +01001160static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001162 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001164 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 const char *name_format;
1166
Takashi Iwai86e07d32005-11-17 15:08:02 +01001167 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!substream) {
1169 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1170 return;
1171 }
1172
1173 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001174 port_info = find_port_info(umidi, number);
1175 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 snprintf(substream->name, sizeof(substream->name),
1177 name_format, umidi->chip->card->shortname, number + 1);
1178
1179 *rsubstream = substream;
1180}
1181
1182/*
1183 * Creates the endpoints and their ports.
1184 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001185static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1186 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 int i, j, err;
1189 int out_ports = 0, in_ports = 0;
1190
1191 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1192 if (endpoints[i].out_cables) {
1193 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1194 &umidi->endpoints[i]);
1195 if (err < 0)
1196 return err;
1197 }
1198 if (endpoints[i].in_cables) {
1199 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1200 &umidi->endpoints[i]);
1201 if (err < 0)
1202 return err;
1203 }
1204
1205 for (j = 0; j < 0x10; ++j) {
1206 if (endpoints[i].out_cables & (1 << j)) {
1207 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1208 &umidi->endpoints[i].out->ports[j].substream);
1209 ++out_ports;
1210 }
1211 if (endpoints[i].in_cables & (1 << j)) {
1212 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1213 &umidi->endpoints[i].in->ports[j].substream);
1214 ++in_ports;
1215 }
1216 }
1217 }
1218 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1219 out_ports, in_ports);
1220 return 0;
1221}
1222
1223/*
1224 * Returns MIDIStreaming device capabilities.
1225 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001226static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1227 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct usb_interface* intf;
1230 struct usb_host_interface *hostif;
1231 struct usb_interface_descriptor* intfd;
1232 struct usb_ms_header_descriptor* ms_header;
1233 struct usb_host_endpoint *hostep;
1234 struct usb_endpoint_descriptor* ep;
1235 struct usb_ms_endpoint_descriptor* ms_ep;
1236 int i, epidx;
1237
1238 intf = umidi->iface;
1239 if (!intf)
1240 return -ENXIO;
1241 hostif = &intf->altsetting[0];
1242 intfd = get_iface_desc(hostif);
1243 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1244 if (hostif->extralen >= 7 &&
1245 ms_header->bLength >= 7 &&
1246 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1247 ms_header->bDescriptorSubtype == HEADER)
1248 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1249 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1250 else
1251 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1252
1253 epidx = 0;
1254 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1255 hostep = &hostif->endpoint[i];
1256 ep = get_ep_desc(hostep);
1257 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1258 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1259 continue;
1260 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1261 if (hostep->extralen < 4 ||
1262 ms_ep->bLength < 4 ||
1263 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1264 ms_ep->bDescriptorSubtype != MS_GENERAL)
1265 continue;
1266 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1267 if (endpoints[epidx].out_ep) {
1268 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1269 snd_printk(KERN_WARNING "too many endpoints\n");
1270 break;
1271 }
1272 }
1273 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1274 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1275 endpoints[epidx].out_interval = ep->bInterval;
1276 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1277 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1278 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1279 } else {
1280 if (endpoints[epidx].in_ep) {
1281 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1282 snd_printk(KERN_WARNING "too many endpoints\n");
1283 break;
1284 }
1285 }
1286 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1287 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1288 endpoints[epidx].in_interval = ep->bInterval;
1289 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1290 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1291 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1292 }
1293 }
1294 return 0;
1295}
1296
1297/*
1298 * On Roland devices, use the second alternate setting to be able to use
1299 * the interrupt input endpoint.
1300 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001301static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct usb_interface* intf;
1304 struct usb_host_interface *hostif;
1305 struct usb_interface_descriptor* intfd;
1306
1307 intf = umidi->iface;
1308 if (!intf || intf->num_altsetting != 2)
1309 return;
1310
1311 hostif = &intf->altsetting[1];
1312 intfd = get_iface_desc(hostif);
1313 if (intfd->bNumEndpoints != 2 ||
1314 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1315 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1316 return;
1317
1318 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1319 intfd->bAlternateSetting);
1320 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1321 intfd->bAlternateSetting);
1322}
1323
1324/*
1325 * Try to find any usable endpoints in the interface.
1326 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001327static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1328 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int max_endpoints)
1330{
1331 struct usb_interface* intf;
1332 struct usb_host_interface *hostif;
1333 struct usb_interface_descriptor* intfd;
1334 struct usb_endpoint_descriptor* epd;
1335 int i, out_eps = 0, in_eps = 0;
1336
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001337 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 snd_usbmidi_switch_roland_altsetting(umidi);
1339
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001340 if (endpoint[0].out_ep || endpoint[0].in_ep)
1341 return 0;
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 intf = umidi->iface;
1344 if (!intf || intf->num_altsetting < 1)
1345 return -ENOENT;
1346 hostif = intf->cur_altsetting;
1347 intfd = get_iface_desc(hostif);
1348
1349 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1350 epd = get_endpoint(hostif, i);
1351 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1352 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1353 continue;
1354 if (out_eps < max_endpoints &&
1355 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1356 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1357 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1358 endpoint[out_eps].out_interval = epd->bInterval;
1359 ++out_eps;
1360 }
1361 if (in_eps < max_endpoints &&
1362 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1363 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1364 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1365 endpoint[in_eps].in_interval = epd->bInterval;
1366 ++in_eps;
1367 }
1368 }
1369 return (out_eps || in_eps) ? 0 : -ENOENT;
1370}
1371
1372/*
1373 * Detects the endpoints for one-port-per-endpoint protocols.
1374 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001375static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1376 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 int err, i;
1379
1380 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1381 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1382 if (endpoints[i].out_ep)
1383 endpoints[i].out_cables = 0x0001;
1384 if (endpoints[i].in_ep)
1385 endpoints[i].in_cables = 0x0001;
1386 }
1387 return err;
1388}
1389
1390/*
1391 * Detects the endpoints and ports of Yamaha devices.
1392 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001393static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1394 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct usb_interface* intf;
1397 struct usb_host_interface *hostif;
1398 struct usb_interface_descriptor* intfd;
1399 uint8_t* cs_desc;
1400
1401 intf = umidi->iface;
1402 if (!intf)
1403 return -ENOENT;
1404 hostif = intf->altsetting;
1405 intfd = get_iface_desc(hostif);
1406 if (intfd->bNumEndpoints < 1)
1407 return -ENOENT;
1408
1409 /*
1410 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1411 * necessarily with any useful contents. So simply count 'em.
1412 */
1413 for (cs_desc = hostif->extra;
1414 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1415 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001416 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (cs_desc[2] == MIDI_IN_JACK)
1418 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1419 else if (cs_desc[2] == MIDI_OUT_JACK)
1420 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1421 }
1422 }
1423 if (!endpoint->in_cables && !endpoint->out_cables)
1424 return -ENOENT;
1425
1426 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1427}
1428
1429/*
1430 * Creates the endpoints and their ports for Midiman devices.
1431 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001432static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1433 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001435 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 struct usb_interface* intf;
1437 struct usb_host_interface *hostif;
1438 struct usb_interface_descriptor* intfd;
1439 struct usb_endpoint_descriptor* epd;
1440 int cable, err;
1441
1442 intf = umidi->iface;
1443 if (!intf)
1444 return -ENOENT;
1445 hostif = intf->altsetting;
1446 intfd = get_iface_desc(hostif);
1447 /*
1448 * The various MidiSport devices have more or less random endpoint
1449 * numbers, so we have to identify the endpoints by their index in
1450 * the descriptor array, like the driver for that other OS does.
1451 *
1452 * There is one interrupt input endpoint for all input ports, one
1453 * bulk output endpoint for even-numbered ports, and one for odd-
1454 * numbered ports. Both bulk output endpoints have corresponding
1455 * input bulk endpoints (at indices 1 and 3) which aren't used.
1456 */
1457 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1458 snd_printdd(KERN_ERR "not enough endpoints\n");
1459 return -ENOENT;
1460 }
1461
1462 epd = get_endpoint(hostif, 0);
1463 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1464 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1465 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1466 return -ENXIO;
1467 }
1468 epd = get_endpoint(hostif, 2);
1469 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1470 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1471 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1472 return -ENXIO;
1473 }
1474 if (endpoint->out_cables > 0x0001) {
1475 epd = get_endpoint(hostif, 4);
1476 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1477 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1478 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1479 return -ENXIO;
1480 }
1481 }
1482
1483 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1484 ep_info.out_cables = endpoint->out_cables & 0x5555;
1485 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1486 if (err < 0)
1487 return err;
1488
1489 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1490 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1491 ep_info.in_cables = endpoint->in_cables;
1492 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1493 if (err < 0)
1494 return err;
1495
1496 if (endpoint->out_cables > 0x0001) {
1497 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1498 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1499 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1500 if (err < 0)
1501 return err;
1502 }
1503
1504 for (cable = 0; cable < 0x10; ++cable) {
1505 if (endpoint->out_cables & (1 << cable))
1506 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1507 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1508 if (endpoint->in_cables & (1 << cable))
1509 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1510 &umidi->endpoints[0].in->ports[cable].substream);
1511 }
1512 return 0;
1513}
1514
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001515static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1516 .get_port_info = snd_usbmidi_get_port_info,
1517};
1518
Takashi Iwai86e07d32005-11-17 15:08:02 +01001519static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int out_ports, int in_ports)
1521{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001522 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 int err;
1524
1525 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1526 umidi->chip->next_midi_device++,
1527 out_ports, in_ports, &rmidi);
1528 if (err < 0)
1529 return err;
1530 strcpy(rmidi->name, umidi->chip->card->shortname);
1531 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1532 SNDRV_RAWMIDI_INFO_INPUT |
1533 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001534 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 rmidi->private_data = umidi;
1536 rmidi->private_free = snd_usbmidi_rawmidi_free;
1537 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1538 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1539
1540 umidi->rmidi = rmidi;
1541 return 0;
1542}
1543
1544/*
1545 * Temporarily stop input.
1546 */
1547void snd_usbmidi_input_stop(struct list_head* p)
1548{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001549 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 int i;
1551
Takashi Iwai86e07d32005-11-17 15:08:02 +01001552 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001554 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (ep->in)
1556 usb_kill_urb(ep->in->urb);
1557 }
1558}
1559
Takashi Iwai86e07d32005-11-17 15:08:02 +01001560static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 if (ep) {
1563 struct urb* urb = ep->urb;
1564 urb->dev = ep->umidi->chip->dev;
1565 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1566 }
1567}
1568
1569/*
1570 * Resume input after a call to snd_usbmidi_input_stop().
1571 */
1572void snd_usbmidi_input_start(struct list_head* p)
1573{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001574 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int i;
1576
Takashi Iwai86e07d32005-11-17 15:08:02 +01001577 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1579 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1580}
1581
1582/*
1583 * Creates and registers everything needed for a MIDI streaming interface.
1584 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001585int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 struct usb_interface* iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001587 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001589 struct snd_usb_midi* umidi;
1590 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int out_ports, in_ports;
1592 int i, err;
1593
Takashi Iwai561b2202005-09-09 14:22:34 +02001594 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (!umidi)
1596 return -ENOMEM;
1597 umidi->chip = chip;
1598 umidi->iface = iface;
1599 umidi->quirk = quirk;
1600 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001601 init_timer(&umidi->error_timer);
1602 umidi->error_timer.function = snd_usbmidi_error_timer;
1603 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 /* detect the endpoint(s) to use */
1606 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001607 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1608 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd1bda042005-09-14 08:36:03 +02001610 break;
1611 case QUIRK_MIDI_FIXED_ENDPOINT:
1612 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001613 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001614 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1615 break;
1616 case QUIRK_MIDI_YAMAHA:
1617 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1618 break;
1619 case QUIRK_MIDI_MIDIMAN:
1620 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1621 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001622 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001623 err = 0;
1624 break;
1625 case QUIRK_MIDI_NOVATION:
1626 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1627 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1628 break;
1629 case QUIRK_MIDI_RAW:
1630 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1631 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1632 break;
1633 case QUIRK_MIDI_EMAGIC:
1634 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1635 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001636 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001637 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1638 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01001639 case QUIRK_MIDI_CME:
Clemens Ladischd1bda042005-09-14 08:36:03 +02001640 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1641 break;
1642 default:
1643 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1644 err = -ENXIO;
1645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647 if (err < 0) {
1648 kfree(umidi);
1649 return err;
1650 }
1651
1652 /* create rawmidi device */
1653 out_ports = 0;
1654 in_ports = 0;
1655 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1656 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1657 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1658 }
1659 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1660 if (err < 0) {
1661 kfree(umidi);
1662 return err;
1663 }
1664
1665 /* create endpoint/port structures */
1666 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1667 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1668 else
1669 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1670 if (err < 0) {
1671 snd_usbmidi_free(umidi);
1672 return err;
1673 }
1674
1675 list_add(&umidi->list, &umidi->chip->midi_list);
1676
1677 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1678 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1679 return 0;
1680}
1681
1682EXPORT_SYMBOL(snd_usb_create_midi_interface);
1683EXPORT_SYMBOL(snd_usbmidi_input_stop);
1684EXPORT_SYMBOL(snd_usbmidi_input_start);
1685EXPORT_SYMBOL(snd_usbmidi_disconnect);