blob: 911f4482b5e11c3f95b21aa81c13b7c21a245f2f [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);
968 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200969 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
970 GFP_KERNEL, &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!buffer) {
972 snd_usbmidi_out_endpoint_delete(ep);
973 return -ENOMEM;
974 }
975 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
Clemens Ladisch3527a002005-09-26 10:03:09 +0200976 ep->max_transfer, snd_usbmidi_out_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200977 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 spin_lock_init(&ep->buffer_lock);
980 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
981
982 for (i = 0; i < 0x10; ++i)
983 if (ep_info->out_cables & (1 << i)) {
984 ep->ports[i].ep = ep;
985 ep->ports[i].cable = i << 4;
986 }
987
988 if (umidi->usb_protocol_ops->init_out_endpoint)
989 umidi->usb_protocol_ops->init_out_endpoint(ep);
990
991 rep->out = ep;
992 return 0;
993}
994
995/*
996 * Frees everything.
997 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100998static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 int i;
1001
1002 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001003 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (ep->out)
1005 snd_usbmidi_out_endpoint_delete(ep->out);
1006 if (ep->in)
1007 snd_usbmidi_in_endpoint_delete(ep->in);
1008 }
1009 kfree(umidi);
1010}
1011
1012/*
1013 * Unlinks all URBs (must be done before the usb_device is deleted).
1014 */
Clemens Ladischee733392005-04-25 10:34:13 +02001015void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001017 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int i;
1019
Takashi Iwai86e07d32005-11-17 15:08:02 +01001020 umidi = list_entry(p, struct snd_usb_midi, list);
Clemens Ladischc8846972005-08-02 15:26:52 +02001021 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001023 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001024 if (ep->out)
1025 tasklet_kill(&ep->out->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (ep->out && ep->out->urb) {
1027 usb_kill_urb(ep->out->urb);
1028 if (umidi->usb_protocol_ops->finish_out_endpoint)
1029 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1030 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001031 if (ep->in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 usb_kill_urb(ep->in->urb);
1033 }
1034}
1035
Takashi Iwai86e07d32005-11-17 15:08:02 +01001036static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001038 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 snd_usbmidi_free(umidi);
1040}
1041
Takashi Iwai86e07d32005-11-17 15:08:02 +01001042static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int stream, int number)
1044{
1045 struct list_head* list;
1046
1047 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001048 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (substream->number == number)
1050 return substream;
1051 }
1052 return NULL;
1053}
1054
1055/*
1056 * This list specifies names for ports that do not fit into the standard
1057 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1058 * such as internal control or synthesizer ports.
1059 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001060static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001061 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001062 short int port;
1063 short int voices;
1064 const char *name;
1065 unsigned int seq_flags;
1066} snd_usbmidi_port_info[] = {
1067#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1068 { .id = USB_ID(vendor, product), \
1069 .port = num, .voices = voices_, \
1070 .name = name_, .seq_flags = flags }
1071#define EXTERNAL_PORT(vendor, product, num, name) \
1072 PORT_INFO(vendor, product, num, name, 0, \
1073 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1074 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1075 SNDRV_SEQ_PORT_TYPE_PORT)
1076#define CONTROL_PORT(vendor, product, num, name) \
1077 PORT_INFO(vendor, product, num, name, 0, \
1078 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1079 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1080#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1081 PORT_INFO(vendor, product, num, name, voices, \
1082 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1083 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1084 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1085 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1086 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1087 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1088 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1089#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1090 PORT_INFO(vendor, product, num, name, voices, \
1091 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1092 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1093 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1094 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1095 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1096 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1097 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1098 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001100 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001102 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1103 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1104 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1105 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1106 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1107 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001109 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1110 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001112 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1113 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1114 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001116 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1117 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1118 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001120 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1121 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1122 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001124 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001126 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1127 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1128 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1129 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001131 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001133 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1134 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1135 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001137 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1138 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1139 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1140 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001142 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1143 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001145 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1146 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1147 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001149 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1150 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1151 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001153 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1154 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001156 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1157 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001159 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1160 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1161 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001163 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1164 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1165 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001166 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001167 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001169 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1170 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001172 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1173 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001175 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1176 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1177 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178};
1179
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001180static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1181{
1182 int i;
1183
1184 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1185 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1186 snd_usbmidi_port_info[i].port == number)
1187 return &snd_usbmidi_port_info[i];
1188 }
1189 return NULL;
1190}
1191
1192static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1193 struct snd_seq_port_info *seq_port_info)
1194{
1195 struct snd_usb_midi *umidi = rmidi->private_data;
1196 struct port_info *port_info;
1197
1198 /* TODO: read port flags from descriptors */
1199 port_info = find_port_info(umidi, number);
1200 if (port_info) {
1201 seq_port_info->type = port_info->seq_flags;
1202 seq_port_info->midi_voices = port_info->voices;
1203 }
1204}
1205
Takashi Iwai86e07d32005-11-17 15:08:02 +01001206static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001208 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001210 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 const char *name_format;
1212
Takashi Iwai86e07d32005-11-17 15:08:02 +01001213 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!substream) {
1215 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1216 return;
1217 }
1218
1219 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001220 port_info = find_port_info(umidi, number);
1221 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 snprintf(substream->name, sizeof(substream->name),
1223 name_format, umidi->chip->card->shortname, number + 1);
1224
1225 *rsubstream = substream;
1226}
1227
1228/*
1229 * Creates the endpoints and their ports.
1230 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001231static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1232 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 int i, j, err;
1235 int out_ports = 0, in_ports = 0;
1236
1237 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1238 if (endpoints[i].out_cables) {
1239 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1240 &umidi->endpoints[i]);
1241 if (err < 0)
1242 return err;
1243 }
1244 if (endpoints[i].in_cables) {
1245 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1246 &umidi->endpoints[i]);
1247 if (err < 0)
1248 return err;
1249 }
1250
1251 for (j = 0; j < 0x10; ++j) {
1252 if (endpoints[i].out_cables & (1 << j)) {
1253 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1254 &umidi->endpoints[i].out->ports[j].substream);
1255 ++out_ports;
1256 }
1257 if (endpoints[i].in_cables & (1 << j)) {
1258 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1259 &umidi->endpoints[i].in->ports[j].substream);
1260 ++in_ports;
1261 }
1262 }
1263 }
1264 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1265 out_ports, in_ports);
1266 return 0;
1267}
1268
1269/*
1270 * Returns MIDIStreaming device capabilities.
1271 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001272static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1273 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 struct usb_interface* intf;
1276 struct usb_host_interface *hostif;
1277 struct usb_interface_descriptor* intfd;
1278 struct usb_ms_header_descriptor* ms_header;
1279 struct usb_host_endpoint *hostep;
1280 struct usb_endpoint_descriptor* ep;
1281 struct usb_ms_endpoint_descriptor* ms_ep;
1282 int i, epidx;
1283
1284 intf = umidi->iface;
1285 if (!intf)
1286 return -ENXIO;
1287 hostif = &intf->altsetting[0];
1288 intfd = get_iface_desc(hostif);
1289 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1290 if (hostif->extralen >= 7 &&
1291 ms_header->bLength >= 7 &&
1292 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1293 ms_header->bDescriptorSubtype == HEADER)
1294 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1295 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1296 else
1297 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1298
1299 epidx = 0;
1300 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1301 hostep = &hostif->endpoint[i];
1302 ep = get_ep_desc(hostep);
1303 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1304 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1305 continue;
1306 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1307 if (hostep->extralen < 4 ||
1308 ms_ep->bLength < 4 ||
1309 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1310 ms_ep->bDescriptorSubtype != MS_GENERAL)
1311 continue;
1312 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1313 if (endpoints[epidx].out_ep) {
1314 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1315 snd_printk(KERN_WARNING "too many endpoints\n");
1316 break;
1317 }
1318 }
1319 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1320 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1321 endpoints[epidx].out_interval = ep->bInterval;
1322 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1323 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1324 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1325 } else {
1326 if (endpoints[epidx].in_ep) {
1327 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1328 snd_printk(KERN_WARNING "too many endpoints\n");
1329 break;
1330 }
1331 }
1332 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1333 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1334 endpoints[epidx].in_interval = ep->bInterval;
1335 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1336 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1337 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1338 }
1339 }
1340 return 0;
1341}
1342
1343/*
1344 * On Roland devices, use the second alternate setting to be able to use
1345 * the interrupt input endpoint.
1346 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001347static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct usb_interface* intf;
1350 struct usb_host_interface *hostif;
1351 struct usb_interface_descriptor* intfd;
1352
1353 intf = umidi->iface;
1354 if (!intf || intf->num_altsetting != 2)
1355 return;
1356
1357 hostif = &intf->altsetting[1];
1358 intfd = get_iface_desc(hostif);
1359 if (intfd->bNumEndpoints != 2 ||
1360 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1361 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1362 return;
1363
1364 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1365 intfd->bAlternateSetting);
1366 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1367 intfd->bAlternateSetting);
1368}
1369
1370/*
1371 * Try to find any usable endpoints in the interface.
1372 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001373static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1374 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 int max_endpoints)
1376{
1377 struct usb_interface* intf;
1378 struct usb_host_interface *hostif;
1379 struct usb_interface_descriptor* intfd;
1380 struct usb_endpoint_descriptor* epd;
1381 int i, out_eps = 0, in_eps = 0;
1382
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001383 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 snd_usbmidi_switch_roland_altsetting(umidi);
1385
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001386 if (endpoint[0].out_ep || endpoint[0].in_ep)
1387 return 0;
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 intf = umidi->iface;
1390 if (!intf || intf->num_altsetting < 1)
1391 return -ENOENT;
1392 hostif = intf->cur_altsetting;
1393 intfd = get_iface_desc(hostif);
1394
1395 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1396 epd = get_endpoint(hostif, i);
1397 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1398 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1399 continue;
1400 if (out_eps < max_endpoints &&
1401 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1402 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1403 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1404 endpoint[out_eps].out_interval = epd->bInterval;
1405 ++out_eps;
1406 }
1407 if (in_eps < max_endpoints &&
1408 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1409 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1410 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1411 endpoint[in_eps].in_interval = epd->bInterval;
1412 ++in_eps;
1413 }
1414 }
1415 return (out_eps || in_eps) ? 0 : -ENOENT;
1416}
1417
1418/*
1419 * Detects the endpoints for one-port-per-endpoint protocols.
1420 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001421static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1422 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 int err, i;
1425
1426 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1427 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1428 if (endpoints[i].out_ep)
1429 endpoints[i].out_cables = 0x0001;
1430 if (endpoints[i].in_ep)
1431 endpoints[i].in_cables = 0x0001;
1432 }
1433 return err;
1434}
1435
1436/*
1437 * Detects the endpoints and ports of Yamaha devices.
1438 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001439static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1440 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct usb_interface* intf;
1443 struct usb_host_interface *hostif;
1444 struct usb_interface_descriptor* intfd;
1445 uint8_t* cs_desc;
1446
1447 intf = umidi->iface;
1448 if (!intf)
1449 return -ENOENT;
1450 hostif = intf->altsetting;
1451 intfd = get_iface_desc(hostif);
1452 if (intfd->bNumEndpoints < 1)
1453 return -ENOENT;
1454
1455 /*
1456 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1457 * necessarily with any useful contents. So simply count 'em.
1458 */
1459 for (cs_desc = hostif->extra;
1460 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1461 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001462 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (cs_desc[2] == MIDI_IN_JACK)
1464 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1465 else if (cs_desc[2] == MIDI_OUT_JACK)
1466 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1467 }
1468 }
1469 if (!endpoint->in_cables && !endpoint->out_cables)
1470 return -ENOENT;
1471
1472 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1473}
1474
1475/*
1476 * Creates the endpoints and their ports for Midiman devices.
1477 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001478static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1479 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001481 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct usb_interface* intf;
1483 struct usb_host_interface *hostif;
1484 struct usb_interface_descriptor* intfd;
1485 struct usb_endpoint_descriptor* epd;
1486 int cable, err;
1487
1488 intf = umidi->iface;
1489 if (!intf)
1490 return -ENOENT;
1491 hostif = intf->altsetting;
1492 intfd = get_iface_desc(hostif);
1493 /*
1494 * The various MidiSport devices have more or less random endpoint
1495 * numbers, so we have to identify the endpoints by their index in
1496 * the descriptor array, like the driver for that other OS does.
1497 *
1498 * There is one interrupt input endpoint for all input ports, one
1499 * bulk output endpoint for even-numbered ports, and one for odd-
1500 * numbered ports. Both bulk output endpoints have corresponding
1501 * input bulk endpoints (at indices 1 and 3) which aren't used.
1502 */
1503 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1504 snd_printdd(KERN_ERR "not enough endpoints\n");
1505 return -ENOENT;
1506 }
1507
1508 epd = get_endpoint(hostif, 0);
1509 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1510 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1511 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1512 return -ENXIO;
1513 }
1514 epd = get_endpoint(hostif, 2);
1515 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1516 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1517 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1518 return -ENXIO;
1519 }
1520 if (endpoint->out_cables > 0x0001) {
1521 epd = get_endpoint(hostif, 4);
1522 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1523 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1524 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1525 return -ENXIO;
1526 }
1527 }
1528
1529 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1530 ep_info.out_cables = endpoint->out_cables & 0x5555;
1531 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1532 if (err < 0)
1533 return err;
1534
1535 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1536 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1537 ep_info.in_cables = endpoint->in_cables;
1538 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1539 if (err < 0)
1540 return err;
1541
1542 if (endpoint->out_cables > 0x0001) {
1543 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1544 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1545 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1546 if (err < 0)
1547 return err;
1548 }
1549
1550 for (cable = 0; cable < 0x10; ++cable) {
1551 if (endpoint->out_cables & (1 << cable))
1552 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1553 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1554 if (endpoint->in_cables & (1 << cable))
1555 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1556 &umidi->endpoints[0].in->ports[cable].substream);
1557 }
1558 return 0;
1559}
1560
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001561static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1562 .get_port_info = snd_usbmidi_get_port_info,
1563};
1564
Takashi Iwai86e07d32005-11-17 15:08:02 +01001565static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 int out_ports, int in_ports)
1567{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001568 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 int err;
1570
1571 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1572 umidi->chip->next_midi_device++,
1573 out_ports, in_ports, &rmidi);
1574 if (err < 0)
1575 return err;
1576 strcpy(rmidi->name, umidi->chip->card->shortname);
1577 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1578 SNDRV_RAWMIDI_INFO_INPUT |
1579 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001580 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 rmidi->private_data = umidi;
1582 rmidi->private_free = snd_usbmidi_rawmidi_free;
1583 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1584 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1585
1586 umidi->rmidi = rmidi;
1587 return 0;
1588}
1589
1590/*
1591 * Temporarily stop input.
1592 */
1593void snd_usbmidi_input_stop(struct list_head* p)
1594{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001595 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 int i;
1597
Takashi Iwai86e07d32005-11-17 15:08:02 +01001598 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001600 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (ep->in)
1602 usb_kill_urb(ep->in->urb);
1603 }
1604}
1605
Takashi Iwai86e07d32005-11-17 15:08:02 +01001606static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 if (ep) {
1609 struct urb* urb = ep->urb;
1610 urb->dev = ep->umidi->chip->dev;
1611 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1612 }
1613}
1614
1615/*
1616 * Resume input after a call to snd_usbmidi_input_stop().
1617 */
1618void snd_usbmidi_input_start(struct list_head* p)
1619{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001620 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 int i;
1622
Takashi Iwai86e07d32005-11-17 15:08:02 +01001623 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1625 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1626}
1627
1628/*
1629 * Creates and registers everything needed for a MIDI streaming interface.
1630 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001631int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct usb_interface* iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001633 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001635 struct snd_usb_midi* umidi;
1636 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 int out_ports, in_ports;
1638 int i, err;
1639
Takashi Iwai561b2202005-09-09 14:22:34 +02001640 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (!umidi)
1642 return -ENOMEM;
1643 umidi->chip = chip;
1644 umidi->iface = iface;
1645 umidi->quirk = quirk;
1646 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001647 init_timer(&umidi->error_timer);
1648 umidi->error_timer.function = snd_usbmidi_error_timer;
1649 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 /* detect the endpoint(s) to use */
1652 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001653 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1654 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd05cc10432007-05-07 09:28:53 +02001656 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1657 umidi->usb_protocol_ops =
1658 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001659 break;
1660 case QUIRK_MIDI_FIXED_ENDPOINT:
1661 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001662 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001663 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1664 break;
1665 case QUIRK_MIDI_YAMAHA:
1666 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1667 break;
1668 case QUIRK_MIDI_MIDIMAN:
1669 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1670 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001671 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001672 err = 0;
1673 break;
1674 case QUIRK_MIDI_NOVATION:
1675 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1676 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1677 break;
1678 case QUIRK_MIDI_RAW:
1679 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1680 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1681 break;
1682 case QUIRK_MIDI_EMAGIC:
1683 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1684 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001685 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001686 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1687 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01001688 case QUIRK_MIDI_CME:
Clemens Ladischd1bda042005-09-14 08:36:03 +02001689 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1690 break;
1691 default:
1692 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1693 err = -ENXIO;
1694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696 if (err < 0) {
1697 kfree(umidi);
1698 return err;
1699 }
1700
1701 /* create rawmidi device */
1702 out_ports = 0;
1703 in_ports = 0;
1704 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1705 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1706 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1707 }
1708 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1709 if (err < 0) {
1710 kfree(umidi);
1711 return err;
1712 }
1713
1714 /* create endpoint/port structures */
1715 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1716 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1717 else
1718 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1719 if (err < 0) {
1720 snd_usbmidi_free(umidi);
1721 return err;
1722 }
1723
1724 list_add(&umidi->list, &umidi->chip->midi_list);
1725
1726 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1727 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1728 return 0;
1729}
1730
1731EXPORT_SYMBOL(snd_usb_create_midi_interface);
1732EXPORT_SYMBOL(snd_usbmidi_input_stop);
1733EXPORT_SYMBOL(snd_usbmidi_input_start);
1734EXPORT_SYMBOL(snd_usbmidi_disconnect);