blob: 99295f9b76910eb9610243170dab6acdfcd2e971 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
Clemens Ladischd05cc10432007-05-07 09:28:53 +02004 * Copyright (c) 2002-2007 Clemens Ladisch
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200148 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200150 u8 seen_f5;
151 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int current_port;
153};
154
Takashi Iwai86e07d32005-11-17 15:08:02 +0100155static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157static const uint8_t snd_usbmidi_cin_length[] = {
158 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
159};
160
161/*
162 * Submits the URB, with error handling.
163 */
Al Viro55016f12005-10-21 03:21:58 -0400164static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 int err = usb_submit_urb(urb, flags);
167 if (err < 0 && err != -ENODEV)
168 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
169 return err;
170}
171
172/*
173 * Error handling for URB completion functions.
174 */
175static int snd_usbmidi_urb_error(int status)
176{
Clemens Ladischc8846972005-08-02 15:26:52 +0200177 switch (status) {
178 /* manually unlinked, or device gone */
179 case -ENOENT:
180 case -ECONNRESET:
181 case -ESHUTDOWN:
182 case -ENODEV:
183 return -ENODEV;
184 /* errors that might occur during unplugging */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700185 case -EPROTO:
186 case -ETIME:
187 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200188 return -EIO;
189 default:
190 snd_printk(KERN_ERR "urb status %d\n", status);
191 return 0; /* continue */
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/*
196 * Receives a chunk of MIDI data.
197 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100198static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 uint8_t* data, int length)
200{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100201 struct usbmidi_in_port* port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (!port->substream) {
204 snd_printd("unexpected port %d!\n", portidx);
205 return;
206 }
207 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
208 return;
209 snd_rawmidi_receive(port->substream, data, length);
210}
211
212#ifdef DUMP_PACKETS
213static void dump_urb(const char *type, const u8 *data, int length)
214{
215 snd_printk(KERN_DEBUG "%s packet: [", type);
216 for (; length > 0; ++data, --length)
217 printk(" %02x", *data);
218 printk(" ]\n");
219}
220#else
221#define dump_urb(type, data, length) /* nothing */
222#endif
223
224/*
225 * Processes the data read from the device.
226 */
David Howells7d12e782006-10-05 14:55:46 +0100227static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100229 struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (urb->status == 0) {
232 dump_urb("received", urb->transfer_buffer, urb->actual_length);
233 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
234 urb->actual_length);
235 } else {
Clemens Ladischc8846972005-08-02 15:26:52 +0200236 int err = snd_usbmidi_urb_error(urb->status);
237 if (err < 0) {
238 if (err != -ENODEV) {
239 ep->error_resubmit = 1;
240 mod_timer(&ep->umidi->error_timer,
241 jiffies + ERROR_DELAY_JIFFIES);
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200247 urb->dev = ep->umidi->chip->dev;
248 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
David Howells7d12e782006-10-05 14:55:46 +0100251static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100253 struct snd_usb_midi_out_endpoint* ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 spin_lock(&ep->buffer_lock);
256 ep->urb_active = 0;
257 spin_unlock(&ep->buffer_lock);
258 if (urb->status < 0) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200259 int err = snd_usbmidi_urb_error(urb->status);
260 if (err < 0) {
261 if (err != -ENODEV)
262 mod_timer(&ep->umidi->error_timer,
263 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 snd_usbmidi_do_output(ep);
268}
269
270/*
271 * This is called when some data should be transferred to the device
272 * (from one or more substreams).
273 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100274static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct urb* urb = ep->urb;
277 unsigned long flags;
278
279 spin_lock_irqsave(&ep->buffer_lock, flags);
280 if (ep->urb_active || ep->umidi->chip->shutdown) {
281 spin_unlock_irqrestore(&ep->buffer_lock, flags);
282 return;
283 }
284
285 urb->transfer_buffer_length = 0;
286 ep->umidi->usb_protocol_ops->output(ep);
287
288 if (urb->transfer_buffer_length > 0) {
289 dump_urb("sending", urb->transfer_buffer,
290 urb->transfer_buffer_length);
291 urb->dev = ep->umidi->chip->dev;
292 ep->urb_active = snd_usbmidi_submit_urb(urb, GFP_ATOMIC) >= 0;
293 }
294 spin_unlock_irqrestore(&ep->buffer_lock, flags);
295}
296
297static void snd_usbmidi_out_tasklet(unsigned long data)
298{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100299 struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 snd_usbmidi_do_output(ep);
302}
303
Clemens Ladischc8846972005-08-02 15:26:52 +0200304/* called after transfers had been interrupted due to some USB error */
305static void snd_usbmidi_error_timer(unsigned long data)
306{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100307 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladischc8846972005-08-02 15:26:52 +0200308 int i;
309
310 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100311 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200312 if (in && in->error_resubmit) {
313 in->error_resubmit = 0;
314 in->urb->dev = umidi->chip->dev;
315 snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC);
316 }
317 if (umidi->endpoints[i].out)
318 snd_usbmidi_do_output(umidi->endpoints[i].out);
319 }
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/* helper function to send static data that may not DMA-able */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100323static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 const void *data, int len)
325{
326 int err;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700327 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (!buf)
329 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 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/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200370 * Buggy M-Audio device: running status on input results in a packet that has
371 * the data bytes but not the status byte and that is marked with CIN 4.
372 */
373static void snd_usbmidi_maudio_broken_running_status_input(
374 struct snd_usb_midi_in_endpoint* ep,
375 uint8_t* buffer, int buffer_length)
376{
377 int i;
378
379 for (i = 0; i + 3 < buffer_length; i += 4)
380 if (buffer[i] != 0) {
381 int cable = buffer[i] >> 4;
382 u8 cin = buffer[i] & 0x0f;
383 struct usbmidi_in_port *port = &ep->ports[cable];
384 int length;
385
386 length = snd_usbmidi_cin_length[cin];
387 if (cin == 0xf && buffer[i + 1] >= 0xf8)
388 ; /* realtime msg: no running status change */
389 else if (cin >= 0x8 && cin <= 0xe)
390 /* channel msg */
391 port->running_status_length = length - 1;
392 else if (cin == 0x4 &&
393 port->running_status_length != 0 &&
394 buffer[i + 1] < 0x80)
395 /* CIN 4 that is not a SysEx */
396 length = port->running_status_length;
397 else
398 /*
399 * All other msgs cannot begin running status.
400 * (A channel msg sent as two or three CIN 0xF
401 * packets could in theory, but this device
402 * doesn't use this format.)
403 */
404 port->running_status_length = 0;
405 snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
406 }
407}
408
409/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * Adds one USB MIDI packet to the output buffer.
411 */
412static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
413 uint8_t p1, uint8_t p2, uint8_t p3)
414{
415
416 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
417 buf[0] = p0;
418 buf[1] = p1;
419 buf[2] = p2;
420 buf[3] = p3;
421 urb->transfer_buffer_length += 4;
422}
423
424/*
425 * Adds one Midiman packet to the output buffer.
426 */
427static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
428 uint8_t p1, uint8_t p2, uint8_t p3)
429{
430
431 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
432 buf[0] = p1;
433 buf[1] = p2;
434 buf[2] = p3;
435 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
436 urb->transfer_buffer_length += 4;
437}
438
439/*
440 * Converts MIDI commands to USB MIDI packets.
441 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100442static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 uint8_t b, struct urb* urb)
444{
445 uint8_t p0 = port->cable;
446 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
447 port->ep->umidi->usb_protocol_ops->output_packet;
448
449 if (b >= 0xf8) {
450 output_packet(urb, p0 | 0x0f, b, 0, 0);
451 } else if (b >= 0xf0) {
452 switch (b) {
453 case 0xf0:
454 port->data[0] = b;
455 port->state = STATE_SYSEX_1;
456 break;
457 case 0xf1:
458 case 0xf3:
459 port->data[0] = b;
460 port->state = STATE_1PARAM;
461 break;
462 case 0xf2:
463 port->data[0] = b;
464 port->state = STATE_2PARAM_1;
465 break;
466 case 0xf4:
467 case 0xf5:
468 port->state = STATE_UNKNOWN;
469 break;
470 case 0xf6:
471 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
472 port->state = STATE_UNKNOWN;
473 break;
474 case 0xf7:
475 switch (port->state) {
476 case STATE_SYSEX_0:
477 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
478 break;
479 case STATE_SYSEX_1:
480 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
481 break;
482 case STATE_SYSEX_2:
483 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
484 break;
485 }
486 port->state = STATE_UNKNOWN;
487 break;
488 }
489 } else if (b >= 0x80) {
490 port->data[0] = b;
491 if (b >= 0xc0 && b <= 0xdf)
492 port->state = STATE_1PARAM;
493 else
494 port->state = STATE_2PARAM_1;
495 } else { /* b < 0x80 */
496 switch (port->state) {
497 case STATE_1PARAM:
498 if (port->data[0] < 0xf0) {
499 p0 |= port->data[0] >> 4;
500 } else {
501 p0 |= 0x02;
502 port->state = STATE_UNKNOWN;
503 }
504 output_packet(urb, p0, port->data[0], b, 0);
505 break;
506 case STATE_2PARAM_1:
507 port->data[1] = b;
508 port->state = STATE_2PARAM_2;
509 break;
510 case STATE_2PARAM_2:
511 if (port->data[0] < 0xf0) {
512 p0 |= port->data[0] >> 4;
513 port->state = STATE_2PARAM_1;
514 } else {
515 p0 |= 0x03;
516 port->state = STATE_UNKNOWN;
517 }
518 output_packet(urb, p0, port->data[0], port->data[1], b);
519 break;
520 case STATE_SYSEX_0:
521 port->data[0] = b;
522 port->state = STATE_SYSEX_1;
523 break;
524 case STATE_SYSEX_1:
525 port->data[1] = b;
526 port->state = STATE_SYSEX_2;
527 break;
528 case STATE_SYSEX_2:
529 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
530 port->state = STATE_SYSEX_0;
531 break;
532 }
533 }
534}
535
Takashi Iwai86e07d32005-11-17 15:08:02 +0100536static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct urb* urb = ep->urb;
539 int p;
540
541 /* FIXME: lower-numbered ports can starve higher-numbered ports */
542 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100543 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (!port->active)
545 continue;
546 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
547 uint8_t b;
548 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
549 port->active = 0;
550 break;
551 }
552 snd_usbmidi_transmit_byte(port, b, urb);
553 }
554 }
555}
556
557static struct usb_protocol_ops snd_usbmidi_standard_ops = {
558 .input = snd_usbmidi_standard_input,
559 .output = snd_usbmidi_standard_output,
560 .output_packet = snd_usbmidi_output_standard_packet,
561};
562
563static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
564 .input = snd_usbmidi_midiman_input,
565 .output = snd_usbmidi_standard_output,
566 .output_packet = snd_usbmidi_output_midiman_packet,
567};
568
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200569static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
570 .input = snd_usbmidi_maudio_broken_running_status_input,
571 .output = snd_usbmidi_standard_output,
572 .output_packet = snd_usbmidi_output_standard_packet,
573};
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*
576 * Novation USB MIDI protocol: number of data bytes is in the first byte
577 * (when receiving) (+1!) or in the second byte (when sending); data begins
578 * at the third byte.
579 */
580
Takashi Iwai86e07d32005-11-17 15:08:02 +0100581static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 uint8_t* buffer, int buffer_length)
583{
584 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
585 return;
586 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
587}
588
Takashi Iwai86e07d32005-11-17 15:08:02 +0100589static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 uint8_t* transfer_buffer;
592 int count;
593
594 if (!ep->ports[0].active)
595 return;
596 transfer_buffer = ep->urb->transfer_buffer;
597 count = snd_rawmidi_transmit(ep->ports[0].substream,
598 &transfer_buffer[2],
599 ep->max_transfer - 2);
600 if (count < 1) {
601 ep->ports[0].active = 0;
602 return;
603 }
604 transfer_buffer[0] = 0;
605 transfer_buffer[1] = count;
606 ep->urb->transfer_buffer_length = 2 + count;
607}
608
609static struct usb_protocol_ops snd_usbmidi_novation_ops = {
610 .input = snd_usbmidi_novation_input,
611 .output = snd_usbmidi_novation_output,
612};
613
614/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200615 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
617
Takashi Iwai86e07d32005-11-17 15:08:02 +0100618static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200619 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
622}
623
Takashi Iwai86e07d32005-11-17 15:08:02 +0100624static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 int count;
627
628 if (!ep->ports[0].active)
629 return;
630 count = snd_rawmidi_transmit(ep->ports[0].substream,
631 ep->urb->transfer_buffer,
632 ep->max_transfer);
633 if (count < 1) {
634 ep->ports[0].active = 0;
635 return;
636 }
637 ep->urb->transfer_buffer_length = count;
638}
639
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200640static struct usb_protocol_ops snd_usbmidi_raw_ops = {
641 .input = snd_usbmidi_raw_input,
642 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643};
644
645/*
646 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
647 */
648
Takashi Iwai86e07d32005-11-17 15:08:02 +0100649static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 static const u8 init_data[] = {
652 /* initialization magic: "get version" */
653 0xf0,
654 0x00, 0x20, 0x31, /* Emagic */
655 0x64, /* Unitor8 */
656 0x0b, /* version number request */
657 0x00, /* command version */
658 0x00, /* EEPROM, box 0 */
659 0xf7
660 };
661 send_bulk_static_data(ep, init_data, sizeof(init_data));
662 /* while we're at it, pour on more magic */
663 send_bulk_static_data(ep, init_data, sizeof(init_data));
664}
665
Takashi Iwai86e07d32005-11-17 15:08:02 +0100666static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 static const u8 finish_data[] = {
669 /* switch to patch mode with last preset */
670 0xf0,
671 0x00, 0x20, 0x31, /* Emagic */
672 0x64, /* Unitor8 */
673 0x10, /* patch switch command */
674 0x00, /* command version */
675 0x7f, /* to all boxes */
676 0x40, /* last preset in EEPROM */
677 0xf7
678 };
679 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
680}
681
Takashi Iwai86e07d32005-11-17 15:08:02 +0100682static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 uint8_t* buffer, int buffer_length)
684{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200685 int i;
686
687 /* FF indicates end of valid data */
688 for (i = 0; i < buffer_length; ++i)
689 if (buffer[i] == 0xff) {
690 buffer_length = i;
691 break;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* handle F5 at end of last buffer */
695 if (ep->seen_f5)
696 goto switch_port;
697
698 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* determine size of data until next F5 */
700 for (i = 0; i < buffer_length; ++i)
701 if (buffer[i] == 0xf5)
702 break;
703 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
704 buffer += i;
705 buffer_length -= i;
706
707 if (buffer_length <= 0)
708 break;
709 /* assert(buffer[0] == 0xf5); */
710 ep->seen_f5 = 1;
711 ++buffer;
712 --buffer_length;
713
714 switch_port:
715 if (buffer_length <= 0)
716 break;
717 if (buffer[0] < 0x80) {
718 ep->current_port = (buffer[0] - 1) & 15;
719 ++buffer;
720 --buffer_length;
721 }
722 ep->seen_f5 = 0;
723 }
724}
725
Takashi Iwai86e07d32005-11-17 15:08:02 +0100726static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 int port0 = ep->current_port;
729 uint8_t* buf = ep->urb->transfer_buffer;
730 int buf_free = ep->max_transfer;
731 int length, i;
732
733 for (i = 0; i < 0x10; ++i) {
734 /* round-robin, starting at the last current port */
735 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100736 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (!port->active)
739 continue;
740 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
741 port->active = 0;
742 continue;
743 }
744
745 if (portnum != ep->current_port) {
746 if (buf_free < 2)
747 break;
748 ep->current_port = portnum;
749 buf[0] = 0xf5;
750 buf[1] = (portnum + 1) & 15;
751 buf += 2;
752 buf_free -= 2;
753 }
754
755 if (buf_free < 1)
756 break;
757 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
758 if (length > 0) {
759 buf += length;
760 buf_free -= length;
761 if (buf_free < 1)
762 break;
763 }
764 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200765 if (buf_free < ep->max_transfer && buf_free > 0) {
766 *buf = 0xff;
767 --buf_free;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
770}
771
772static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
773 .input = snd_usbmidi_emagic_input,
774 .output = snd_usbmidi_emagic_output,
775 .init_out_endpoint = snd_usbmidi_emagic_init_out,
776 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
777};
778
779
Takashi Iwai86e07d32005-11-17 15:08:02 +0100780static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100782 struct snd_usb_midi* umidi = substream->rmidi->private_data;
783 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int i, j;
785
786 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
787 if (umidi->endpoints[i].out)
788 for (j = 0; j < 0x10; ++j)
789 if (umidi->endpoints[i].out->ports[j].substream == substream) {
790 port = &umidi->endpoints[i].out->ports[j];
791 break;
792 }
793 if (!port) {
794 snd_BUG();
795 return -ENXIO;
796 }
797 substream->runtime->private_data = port;
798 port->state = STATE_UNKNOWN;
799 return 0;
800}
801
Takashi Iwai86e07d32005-11-17 15:08:02 +0100802static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 return 0;
805}
806
Takashi Iwai86e07d32005-11-17 15:08:02 +0100807static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100809 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 port->active = up;
812 if (up) {
813 if (port->ep->umidi->chip->shutdown) {
814 /* gobble up remaining bytes to prevent wait in
815 * snd_rawmidi_drain_output */
816 while (!snd_rawmidi_transmit_empty(substream))
817 snd_rawmidi_transmit_ack(substream, 1);
818 return;
819 }
820 tasklet_hi_schedule(&port->ep->tasklet);
821 }
822}
823
Takashi Iwai86e07d32005-11-17 15:08:02 +0100824static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 return 0;
827}
828
Takashi Iwai86e07d32005-11-17 15:08:02 +0100829static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 return 0;
832}
833
Takashi Iwai86e07d32005-11-17 15:08:02 +0100834static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100836 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (up)
839 set_bit(substream->number, &umidi->input_triggered);
840 else
841 clear_bit(substream->number, &umidi->input_triggered);
842}
843
Takashi Iwai86e07d32005-11-17 15:08:02 +0100844static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 .open = snd_usbmidi_output_open,
846 .close = snd_usbmidi_output_close,
847 .trigger = snd_usbmidi_output_trigger,
848};
849
Takashi Iwai86e07d32005-11-17 15:08:02 +0100850static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 .open = snd_usbmidi_input_open,
852 .close = snd_usbmidi_input_close,
853 .trigger = snd_usbmidi_input_trigger
854};
855
856/*
857 * Frees an input endpoint.
858 * May be called when ep hasn't been initialized completely.
859 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100860static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200863 usb_buffer_free(ep->umidi->chip->dev,
864 ep->urb->transfer_buffer_length,
865 ep->urb->transfer_buffer,
866 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 usb_free_urb(ep->urb);
868 }
869 kfree(ep);
870}
871
872/*
873 * Creates an input endpoint.
874 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100875static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
876 struct snd_usb_midi_endpoint_info* ep_info,
877 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100879 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 void* buffer;
881 unsigned int pipe;
882 int length;
883
884 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200885 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!ep)
887 return -ENOMEM;
888 ep->umidi = umidi;
889
890 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
891 if (!ep->urb) {
892 snd_usbmidi_in_endpoint_delete(ep);
893 return -ENOMEM;
894 }
895 if (ep_info->in_interval)
896 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
897 else
898 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
899 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200900 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
901 &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (!buffer) {
903 snd_usbmidi_in_endpoint_delete(ep);
904 return -ENOMEM;
905 }
906 if (ep_info->in_interval)
Clemens Ladisch3527a002005-09-26 10:03:09 +0200907 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
908 length, snd_usbmidi_in_urb_complete, ep,
909 ep_info->in_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 else
Clemens Ladisch3527a002005-09-26 10:03:09 +0200911 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
912 length, snd_usbmidi_in_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200913 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 rep->in = ep;
916 return 0;
917}
918
919static unsigned int snd_usbmidi_count_bits(unsigned int x)
920{
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100921 unsigned int bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100923 for (bits = 0; x; ++bits)
924 x &= x - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return bits;
926}
927
928/*
929 * Frees an output endpoint.
930 * May be called when ep hasn't been initialized completely.
931 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100932static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200935 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
936 ep->urb->transfer_buffer,
937 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 usb_free_urb(ep->urb);
939 }
940 kfree(ep);
941}
942
943/*
944 * Creates an output endpoint, and initializes output ports.
945 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100946static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
947 struct snd_usb_midi_endpoint_info* ep_info,
948 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100950 struct snd_usb_midi_out_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 int i;
952 unsigned int pipe;
953 void* buffer;
954
955 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200956 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (!ep)
958 return -ENOMEM;
959 ep->umidi = umidi;
960
961 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
962 if (!ep->urb) {
963 snd_usbmidi_out_endpoint_delete(ep);
964 return -ENOMEM;
965 }
966 /* we never use interrupt output pipes */
967 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
Clemens Ladisch490cbd92007-05-07 09:29:32 +0200968 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
969 /* FIXME: we need more URBs to get reasonable bandwidth here: */
970 ep->max_transfer = 4;
971 else
972 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200973 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
974 GFP_KERNEL, &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!buffer) {
976 snd_usbmidi_out_endpoint_delete(ep);
977 return -ENOMEM;
978 }
979 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
Clemens Ladisch3527a002005-09-26 10:03:09 +0200980 ep->max_transfer, snd_usbmidi_out_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200981 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 spin_lock_init(&ep->buffer_lock);
984 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
985
986 for (i = 0; i < 0x10; ++i)
987 if (ep_info->out_cables & (1 << i)) {
988 ep->ports[i].ep = ep;
989 ep->ports[i].cable = i << 4;
990 }
991
992 if (umidi->usb_protocol_ops->init_out_endpoint)
993 umidi->usb_protocol_ops->init_out_endpoint(ep);
994
995 rep->out = ep;
996 return 0;
997}
998
999/*
1000 * Frees everything.
1001 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001002static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 int i;
1005
1006 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001007 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (ep->out)
1009 snd_usbmidi_out_endpoint_delete(ep->out);
1010 if (ep->in)
1011 snd_usbmidi_in_endpoint_delete(ep->in);
1012 }
1013 kfree(umidi);
1014}
1015
1016/*
1017 * Unlinks all URBs (must be done before the usb_device is deleted).
1018 */
Clemens Ladischee733392005-04-25 10:34:13 +02001019void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001021 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int i;
1023
Takashi Iwai86e07d32005-11-17 15:08:02 +01001024 umidi = list_entry(p, struct snd_usb_midi, list);
Clemens Ladischc8846972005-08-02 15:26:52 +02001025 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001027 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001028 if (ep->out)
1029 tasklet_kill(&ep->out->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (ep->out && ep->out->urb) {
1031 usb_kill_urb(ep->out->urb);
1032 if (umidi->usb_protocol_ops->finish_out_endpoint)
1033 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1034 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001035 if (ep->in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 usb_kill_urb(ep->in->urb);
1037 }
1038}
1039
Takashi Iwai86e07d32005-11-17 15:08:02 +01001040static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001042 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 snd_usbmidi_free(umidi);
1044}
1045
Takashi Iwai86e07d32005-11-17 15:08:02 +01001046static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 int stream, int number)
1048{
1049 struct list_head* list;
1050
1051 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001052 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (substream->number == number)
1054 return substream;
1055 }
1056 return NULL;
1057}
1058
1059/*
1060 * This list specifies names for ports that do not fit into the standard
1061 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1062 * such as internal control or synthesizer ports.
1063 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001064static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001065 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001066 short int port;
1067 short int voices;
1068 const char *name;
1069 unsigned int seq_flags;
1070} snd_usbmidi_port_info[] = {
1071#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1072 { .id = USB_ID(vendor, product), \
1073 .port = num, .voices = voices_, \
1074 .name = name_, .seq_flags = flags }
1075#define EXTERNAL_PORT(vendor, product, num, name) \
1076 PORT_INFO(vendor, product, num, name, 0, \
1077 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1078 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1079 SNDRV_SEQ_PORT_TYPE_PORT)
1080#define CONTROL_PORT(vendor, product, num, name) \
1081 PORT_INFO(vendor, product, num, name, 0, \
1082 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1083 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1084#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1085 PORT_INFO(vendor, product, num, name, voices, \
1086 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1087 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1088 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1089 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1090 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1091 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1092 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1093#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1094 PORT_INFO(vendor, product, num, name, voices, \
1095 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1096 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1097 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1098 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1099 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1100 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1101 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1102 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001104 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001106 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1107 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1108 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1109 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1110 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1111 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001113 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1114 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001116 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1117 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1118 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001120 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1121 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1122 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001124 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1125 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1126 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001128 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001130 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1131 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1132 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1133 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001135 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001137 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1138 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1139 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001141 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1142 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1143 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1144 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001146 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1147 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001149 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1150 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1151 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001153 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1154 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1155 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001157 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1158 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001160 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1161 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001163 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1164 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1165 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001167 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1168 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1169 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001170 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001171 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001173 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1174 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001176 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1177 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001179 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1180 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1181 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182};
1183
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001184static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1185{
1186 int i;
1187
1188 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1189 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1190 snd_usbmidi_port_info[i].port == number)
1191 return &snd_usbmidi_port_info[i];
1192 }
1193 return NULL;
1194}
1195
1196static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1197 struct snd_seq_port_info *seq_port_info)
1198{
1199 struct snd_usb_midi *umidi = rmidi->private_data;
1200 struct port_info *port_info;
1201
1202 /* TODO: read port flags from descriptors */
1203 port_info = find_port_info(umidi, number);
1204 if (port_info) {
1205 seq_port_info->type = port_info->seq_flags;
1206 seq_port_info->midi_voices = port_info->voices;
1207 }
1208}
1209
Takashi Iwai86e07d32005-11-17 15:08:02 +01001210static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001212 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001214 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 const char *name_format;
1216
Takashi Iwai86e07d32005-11-17 15:08:02 +01001217 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (!substream) {
1219 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1220 return;
1221 }
1222
1223 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001224 port_info = find_port_info(umidi, number);
1225 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 snprintf(substream->name, sizeof(substream->name),
1227 name_format, umidi->chip->card->shortname, number + 1);
1228
1229 *rsubstream = substream;
1230}
1231
1232/*
1233 * Creates the endpoints and their ports.
1234 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001235static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1236 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 int i, j, err;
1239 int out_ports = 0, in_ports = 0;
1240
1241 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1242 if (endpoints[i].out_cables) {
1243 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1244 &umidi->endpoints[i]);
1245 if (err < 0)
1246 return err;
1247 }
1248 if (endpoints[i].in_cables) {
1249 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1250 &umidi->endpoints[i]);
1251 if (err < 0)
1252 return err;
1253 }
1254
1255 for (j = 0; j < 0x10; ++j) {
1256 if (endpoints[i].out_cables & (1 << j)) {
1257 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1258 &umidi->endpoints[i].out->ports[j].substream);
1259 ++out_ports;
1260 }
1261 if (endpoints[i].in_cables & (1 << j)) {
1262 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1263 &umidi->endpoints[i].in->ports[j].substream);
1264 ++in_ports;
1265 }
1266 }
1267 }
1268 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1269 out_ports, in_ports);
1270 return 0;
1271}
1272
1273/*
1274 * Returns MIDIStreaming device capabilities.
1275 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001276static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1277 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 struct usb_interface* intf;
1280 struct usb_host_interface *hostif;
1281 struct usb_interface_descriptor* intfd;
1282 struct usb_ms_header_descriptor* ms_header;
1283 struct usb_host_endpoint *hostep;
1284 struct usb_endpoint_descriptor* ep;
1285 struct usb_ms_endpoint_descriptor* ms_ep;
1286 int i, epidx;
1287
1288 intf = umidi->iface;
1289 if (!intf)
1290 return -ENXIO;
1291 hostif = &intf->altsetting[0];
1292 intfd = get_iface_desc(hostif);
1293 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1294 if (hostif->extralen >= 7 &&
1295 ms_header->bLength >= 7 &&
1296 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1297 ms_header->bDescriptorSubtype == HEADER)
1298 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1299 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1300 else
1301 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1302
1303 epidx = 0;
1304 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1305 hostep = &hostif->endpoint[i];
1306 ep = get_ep_desc(hostep);
1307 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1308 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1309 continue;
1310 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1311 if (hostep->extralen < 4 ||
1312 ms_ep->bLength < 4 ||
1313 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1314 ms_ep->bDescriptorSubtype != MS_GENERAL)
1315 continue;
1316 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1317 if (endpoints[epidx].out_ep) {
1318 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1319 snd_printk(KERN_WARNING "too many endpoints\n");
1320 break;
1321 }
1322 }
1323 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1324 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1325 endpoints[epidx].out_interval = ep->bInterval;
1326 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1327 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1328 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1329 } else {
1330 if (endpoints[epidx].in_ep) {
1331 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1332 snd_printk(KERN_WARNING "too many endpoints\n");
1333 break;
1334 }
1335 }
1336 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1337 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1338 endpoints[epidx].in_interval = ep->bInterval;
1339 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1340 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1341 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1342 }
1343 }
1344 return 0;
1345}
1346
1347/*
1348 * On Roland devices, use the second alternate setting to be able to use
1349 * the interrupt input endpoint.
1350 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001351static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct usb_interface* intf;
1354 struct usb_host_interface *hostif;
1355 struct usb_interface_descriptor* intfd;
1356
1357 intf = umidi->iface;
1358 if (!intf || intf->num_altsetting != 2)
1359 return;
1360
1361 hostif = &intf->altsetting[1];
1362 intfd = get_iface_desc(hostif);
1363 if (intfd->bNumEndpoints != 2 ||
1364 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1365 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1366 return;
1367
1368 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1369 intfd->bAlternateSetting);
1370 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1371 intfd->bAlternateSetting);
1372}
1373
1374/*
1375 * Try to find any usable endpoints in the interface.
1376 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001377static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1378 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int max_endpoints)
1380{
1381 struct usb_interface* intf;
1382 struct usb_host_interface *hostif;
1383 struct usb_interface_descriptor* intfd;
1384 struct usb_endpoint_descriptor* epd;
1385 int i, out_eps = 0, in_eps = 0;
1386
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001387 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 snd_usbmidi_switch_roland_altsetting(umidi);
1389
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001390 if (endpoint[0].out_ep || endpoint[0].in_ep)
1391 return 0;
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 intf = umidi->iface;
1394 if (!intf || intf->num_altsetting < 1)
1395 return -ENOENT;
1396 hostif = intf->cur_altsetting;
1397 intfd = get_iface_desc(hostif);
1398
1399 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1400 epd = get_endpoint(hostif, i);
1401 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1402 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1403 continue;
1404 if (out_eps < max_endpoints &&
1405 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1406 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1407 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1408 endpoint[out_eps].out_interval = epd->bInterval;
1409 ++out_eps;
1410 }
1411 if (in_eps < max_endpoints &&
1412 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1413 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1414 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1415 endpoint[in_eps].in_interval = epd->bInterval;
1416 ++in_eps;
1417 }
1418 }
1419 return (out_eps || in_eps) ? 0 : -ENOENT;
1420}
1421
1422/*
1423 * Detects the endpoints for one-port-per-endpoint protocols.
1424 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001425static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1426 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 int err, i;
1429
1430 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1431 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1432 if (endpoints[i].out_ep)
1433 endpoints[i].out_cables = 0x0001;
1434 if (endpoints[i].in_ep)
1435 endpoints[i].in_cables = 0x0001;
1436 }
1437 return err;
1438}
1439
1440/*
1441 * Detects the endpoints and ports of Yamaha devices.
1442 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001443static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1444 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct usb_interface* intf;
1447 struct usb_host_interface *hostif;
1448 struct usb_interface_descriptor* intfd;
1449 uint8_t* cs_desc;
1450
1451 intf = umidi->iface;
1452 if (!intf)
1453 return -ENOENT;
1454 hostif = intf->altsetting;
1455 intfd = get_iface_desc(hostif);
1456 if (intfd->bNumEndpoints < 1)
1457 return -ENOENT;
1458
1459 /*
1460 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1461 * necessarily with any useful contents. So simply count 'em.
1462 */
1463 for (cs_desc = hostif->extra;
1464 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1465 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001466 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (cs_desc[2] == MIDI_IN_JACK)
1468 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1469 else if (cs_desc[2] == MIDI_OUT_JACK)
1470 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1471 }
1472 }
1473 if (!endpoint->in_cables && !endpoint->out_cables)
1474 return -ENOENT;
1475
1476 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1477}
1478
1479/*
1480 * Creates the endpoints and their ports for Midiman devices.
1481 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001482static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1483 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001485 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 struct usb_interface* intf;
1487 struct usb_host_interface *hostif;
1488 struct usb_interface_descriptor* intfd;
1489 struct usb_endpoint_descriptor* epd;
1490 int cable, err;
1491
1492 intf = umidi->iface;
1493 if (!intf)
1494 return -ENOENT;
1495 hostif = intf->altsetting;
1496 intfd = get_iface_desc(hostif);
1497 /*
1498 * The various MidiSport devices have more or less random endpoint
1499 * numbers, so we have to identify the endpoints by their index in
1500 * the descriptor array, like the driver for that other OS does.
1501 *
1502 * There is one interrupt input endpoint for all input ports, one
1503 * bulk output endpoint for even-numbered ports, and one for odd-
1504 * numbered ports. Both bulk output endpoints have corresponding
1505 * input bulk endpoints (at indices 1 and 3) which aren't used.
1506 */
1507 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1508 snd_printdd(KERN_ERR "not enough endpoints\n");
1509 return -ENOENT;
1510 }
1511
1512 epd = get_endpoint(hostif, 0);
1513 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1514 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1515 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1516 return -ENXIO;
1517 }
1518 epd = get_endpoint(hostif, 2);
1519 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1520 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1521 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1522 return -ENXIO;
1523 }
1524 if (endpoint->out_cables > 0x0001) {
1525 epd = get_endpoint(hostif, 4);
1526 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1527 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1528 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1529 return -ENXIO;
1530 }
1531 }
1532
1533 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1534 ep_info.out_cables = endpoint->out_cables & 0x5555;
1535 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1536 if (err < 0)
1537 return err;
1538
1539 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1540 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1541 ep_info.in_cables = endpoint->in_cables;
1542 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1543 if (err < 0)
1544 return err;
1545
1546 if (endpoint->out_cables > 0x0001) {
1547 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1548 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1549 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1550 if (err < 0)
1551 return err;
1552 }
1553
1554 for (cable = 0; cable < 0x10; ++cable) {
1555 if (endpoint->out_cables & (1 << cable))
1556 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1557 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1558 if (endpoint->in_cables & (1 << cable))
1559 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1560 &umidi->endpoints[0].in->ports[cable].substream);
1561 }
1562 return 0;
1563}
1564
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001565static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1566 .get_port_info = snd_usbmidi_get_port_info,
1567};
1568
Takashi Iwai86e07d32005-11-17 15:08:02 +01001569static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 int out_ports, int in_ports)
1571{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001572 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int err;
1574
1575 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1576 umidi->chip->next_midi_device++,
1577 out_ports, in_ports, &rmidi);
1578 if (err < 0)
1579 return err;
1580 strcpy(rmidi->name, umidi->chip->card->shortname);
1581 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1582 SNDRV_RAWMIDI_INFO_INPUT |
1583 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001584 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 rmidi->private_data = umidi;
1586 rmidi->private_free = snd_usbmidi_rawmidi_free;
1587 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1588 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1589
1590 umidi->rmidi = rmidi;
1591 return 0;
1592}
1593
1594/*
1595 * Temporarily stop input.
1596 */
1597void snd_usbmidi_input_stop(struct list_head* p)
1598{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001599 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int i;
1601
Takashi Iwai86e07d32005-11-17 15:08:02 +01001602 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001604 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (ep->in)
1606 usb_kill_urb(ep->in->urb);
1607 }
1608}
1609
Takashi Iwai86e07d32005-11-17 15:08:02 +01001610static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 if (ep) {
1613 struct urb* urb = ep->urb;
1614 urb->dev = ep->umidi->chip->dev;
1615 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1616 }
1617}
1618
1619/*
1620 * Resume input after a call to snd_usbmidi_input_stop().
1621 */
1622void snd_usbmidi_input_start(struct list_head* p)
1623{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001624 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 int i;
1626
Takashi Iwai86e07d32005-11-17 15:08:02 +01001627 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1629 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1630}
1631
1632/*
1633 * Creates and registers everything needed for a MIDI streaming interface.
1634 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001635int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct usb_interface* iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001637 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001639 struct snd_usb_midi* umidi;
1640 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int out_ports, in_ports;
1642 int i, err;
1643
Takashi Iwai561b2202005-09-09 14:22:34 +02001644 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (!umidi)
1646 return -ENOMEM;
1647 umidi->chip = chip;
1648 umidi->iface = iface;
1649 umidi->quirk = quirk;
1650 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001651 init_timer(&umidi->error_timer);
1652 umidi->error_timer.function = snd_usbmidi_error_timer;
1653 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 /* detect the endpoint(s) to use */
1656 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001657 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1658 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd05cc10432007-05-07 09:28:53 +02001660 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1661 umidi->usb_protocol_ops =
1662 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001663 break;
1664 case QUIRK_MIDI_FIXED_ENDPOINT:
1665 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001666 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001667 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1668 break;
1669 case QUIRK_MIDI_YAMAHA:
1670 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1671 break;
1672 case QUIRK_MIDI_MIDIMAN:
1673 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1674 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001675 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001676 err = 0;
1677 break;
1678 case QUIRK_MIDI_NOVATION:
1679 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1680 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1681 break;
1682 case QUIRK_MIDI_RAW:
1683 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1684 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1685 break;
1686 case QUIRK_MIDI_EMAGIC:
1687 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1688 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001689 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001690 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1691 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01001692 case QUIRK_MIDI_CME:
Clemens Ladischd1bda042005-09-14 08:36:03 +02001693 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1694 break;
1695 default:
1696 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1697 err = -ENXIO;
1698 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 if (err < 0) {
1701 kfree(umidi);
1702 return err;
1703 }
1704
1705 /* create rawmidi device */
1706 out_ports = 0;
1707 in_ports = 0;
1708 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1709 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1710 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1711 }
1712 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1713 if (err < 0) {
1714 kfree(umidi);
1715 return err;
1716 }
1717
1718 /* create endpoint/port structures */
1719 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1720 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1721 else
1722 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1723 if (err < 0) {
1724 snd_usbmidi_free(umidi);
1725 return err;
1726 }
1727
1728 list_add(&umidi->list, &umidi->chip->midi_list);
1729
1730 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1731 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1732 return 0;
1733}
1734
1735EXPORT_SYMBOL(snd_usb_create_midi_interface);
1736EXPORT_SYMBOL(snd_usbmidi_input_stop);
1737EXPORT_SYMBOL(snd_usbmidi_input_start);
1738EXPORT_SYMBOL(snd_usbmidi_disconnect);