blob: 6330788c1c2b2d58ddf31c2802c682b5531f0004 [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/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200410 * CME protocol: like the standard protocol, but SysEx commands are sent as a
411 * single USB packet preceded by a 0x0F byte.
412 */
413static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
414 uint8_t *buffer, int buffer_length)
415{
416 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
417 snd_usbmidi_standard_input(ep, buffer, buffer_length);
418 else
419 snd_usbmidi_input_data(ep, buffer[0] >> 4,
420 &buffer[1], buffer_length - 1);
421}
422
423/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * Adds one USB MIDI packet to the output buffer.
425 */
426static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
427 uint8_t p1, uint8_t p2, uint8_t p3)
428{
429
430 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
431 buf[0] = p0;
432 buf[1] = p1;
433 buf[2] = p2;
434 buf[3] = p3;
435 urb->transfer_buffer_length += 4;
436}
437
438/*
439 * Adds one Midiman packet to the output buffer.
440 */
441static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
442 uint8_t p1, uint8_t p2, uint8_t p3)
443{
444
445 uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
446 buf[0] = p1;
447 buf[1] = p2;
448 buf[2] = p3;
449 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
450 urb->transfer_buffer_length += 4;
451}
452
453/*
454 * Converts MIDI commands to USB MIDI packets.
455 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100456static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 uint8_t b, struct urb* urb)
458{
459 uint8_t p0 = port->cable;
460 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
461 port->ep->umidi->usb_protocol_ops->output_packet;
462
463 if (b >= 0xf8) {
464 output_packet(urb, p0 | 0x0f, b, 0, 0);
465 } else if (b >= 0xf0) {
466 switch (b) {
467 case 0xf0:
468 port->data[0] = b;
469 port->state = STATE_SYSEX_1;
470 break;
471 case 0xf1:
472 case 0xf3:
473 port->data[0] = b;
474 port->state = STATE_1PARAM;
475 break;
476 case 0xf2:
477 port->data[0] = b;
478 port->state = STATE_2PARAM_1;
479 break;
480 case 0xf4:
481 case 0xf5:
482 port->state = STATE_UNKNOWN;
483 break;
484 case 0xf6:
485 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
486 port->state = STATE_UNKNOWN;
487 break;
488 case 0xf7:
489 switch (port->state) {
490 case STATE_SYSEX_0:
491 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
492 break;
493 case STATE_SYSEX_1:
494 output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
495 break;
496 case STATE_SYSEX_2:
497 output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
498 break;
499 }
500 port->state = STATE_UNKNOWN;
501 break;
502 }
503 } else if (b >= 0x80) {
504 port->data[0] = b;
505 if (b >= 0xc0 && b <= 0xdf)
506 port->state = STATE_1PARAM;
507 else
508 port->state = STATE_2PARAM_1;
509 } else { /* b < 0x80 */
510 switch (port->state) {
511 case STATE_1PARAM:
512 if (port->data[0] < 0xf0) {
513 p0 |= port->data[0] >> 4;
514 } else {
515 p0 |= 0x02;
516 port->state = STATE_UNKNOWN;
517 }
518 output_packet(urb, p0, port->data[0], b, 0);
519 break;
520 case STATE_2PARAM_1:
521 port->data[1] = b;
522 port->state = STATE_2PARAM_2;
523 break;
524 case STATE_2PARAM_2:
525 if (port->data[0] < 0xf0) {
526 p0 |= port->data[0] >> 4;
527 port->state = STATE_2PARAM_1;
528 } else {
529 p0 |= 0x03;
530 port->state = STATE_UNKNOWN;
531 }
532 output_packet(urb, p0, port->data[0], port->data[1], b);
533 break;
534 case STATE_SYSEX_0:
535 port->data[0] = b;
536 port->state = STATE_SYSEX_1;
537 break;
538 case STATE_SYSEX_1:
539 port->data[1] = b;
540 port->state = STATE_SYSEX_2;
541 break;
542 case STATE_SYSEX_2:
543 output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
544 port->state = STATE_SYSEX_0;
545 break;
546 }
547 }
548}
549
Takashi Iwai86e07d32005-11-17 15:08:02 +0100550static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct urb* urb = ep->urb;
553 int p;
554
555 /* FIXME: lower-numbered ports can starve higher-numbered ports */
556 for (p = 0; p < 0x10; ++p) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100557 struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (!port->active)
559 continue;
560 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
561 uint8_t b;
562 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
563 port->active = 0;
564 break;
565 }
566 snd_usbmidi_transmit_byte(port, b, urb);
567 }
568 }
569}
570
571static struct usb_protocol_ops snd_usbmidi_standard_ops = {
572 .input = snd_usbmidi_standard_input,
573 .output = snd_usbmidi_standard_output,
574 .output_packet = snd_usbmidi_output_standard_packet,
575};
576
577static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
578 .input = snd_usbmidi_midiman_input,
579 .output = snd_usbmidi_standard_output,
580 .output_packet = snd_usbmidi_output_midiman_packet,
581};
582
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200583static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
584 .input = snd_usbmidi_maudio_broken_running_status_input,
585 .output = snd_usbmidi_standard_output,
586 .output_packet = snd_usbmidi_output_standard_packet,
587};
588
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200589static struct usb_protocol_ops snd_usbmidi_cme_ops = {
590 .input = snd_usbmidi_cme_input,
591 .output = snd_usbmidi_standard_output,
592 .output_packet = snd_usbmidi_output_standard_packet,
593};
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
596 * Novation USB MIDI protocol: number of data bytes is in the first byte
597 * (when receiving) (+1!) or in the second byte (when sending); data begins
598 * at the third byte.
599 */
600
Takashi Iwai86e07d32005-11-17 15:08:02 +0100601static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 uint8_t* buffer, int buffer_length)
603{
604 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
605 return;
606 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
607}
608
Takashi Iwai86e07d32005-11-17 15:08:02 +0100609static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 uint8_t* transfer_buffer;
612 int count;
613
614 if (!ep->ports[0].active)
615 return;
616 transfer_buffer = ep->urb->transfer_buffer;
617 count = snd_rawmidi_transmit(ep->ports[0].substream,
618 &transfer_buffer[2],
619 ep->max_transfer - 2);
620 if (count < 1) {
621 ep->ports[0].active = 0;
622 return;
623 }
624 transfer_buffer[0] = 0;
625 transfer_buffer[1] = count;
626 ep->urb->transfer_buffer_length = 2 + count;
627}
628
629static struct usb_protocol_ops snd_usbmidi_novation_ops = {
630 .input = snd_usbmidi_novation_input,
631 .output = snd_usbmidi_novation_output,
632};
633
634/*
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200635 * "raw" protocol: used by the MOTU FastLane.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
637
Takashi Iwai86e07d32005-11-17 15:08:02 +0100638static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200639 uint8_t* buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
642}
643
Takashi Iwai86e07d32005-11-17 15:08:02 +0100644static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 int count;
647
648 if (!ep->ports[0].active)
649 return;
650 count = snd_rawmidi_transmit(ep->ports[0].substream,
651 ep->urb->transfer_buffer,
652 ep->max_transfer);
653 if (count < 1) {
654 ep->ports[0].active = 0;
655 return;
656 }
657 ep->urb->transfer_buffer_length = count;
658}
659
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200660static struct usb_protocol_ops snd_usbmidi_raw_ops = {
661 .input = snd_usbmidi_raw_input,
662 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663};
664
665/*
666 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
667 */
668
Takashi Iwai86e07d32005-11-17 15:08:02 +0100669static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 static const u8 init_data[] = {
672 /* initialization magic: "get version" */
673 0xf0,
674 0x00, 0x20, 0x31, /* Emagic */
675 0x64, /* Unitor8 */
676 0x0b, /* version number request */
677 0x00, /* command version */
678 0x00, /* EEPROM, box 0 */
679 0xf7
680 };
681 send_bulk_static_data(ep, init_data, sizeof(init_data));
682 /* while we're at it, pour on more magic */
683 send_bulk_static_data(ep, init_data, sizeof(init_data));
684}
685
Takashi Iwai86e07d32005-11-17 15:08:02 +0100686static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 static const u8 finish_data[] = {
689 /* switch to patch mode with last preset */
690 0xf0,
691 0x00, 0x20, 0x31, /* Emagic */
692 0x64, /* Unitor8 */
693 0x10, /* patch switch command */
694 0x00, /* command version */
695 0x7f, /* to all boxes */
696 0x40, /* last preset in EEPROM */
697 0xf7
698 };
699 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
700}
701
Takashi Iwai86e07d32005-11-17 15:08:02 +0100702static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 uint8_t* buffer, int buffer_length)
704{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200705 int i;
706
707 /* FF indicates end of valid data */
708 for (i = 0; i < buffer_length; ++i)
709 if (buffer[i] == 0xff) {
710 buffer_length = i;
711 break;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* handle F5 at end of last buffer */
715 if (ep->seen_f5)
716 goto switch_port;
717
718 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* determine size of data until next F5 */
720 for (i = 0; i < buffer_length; ++i)
721 if (buffer[i] == 0xf5)
722 break;
723 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
724 buffer += i;
725 buffer_length -= i;
726
727 if (buffer_length <= 0)
728 break;
729 /* assert(buffer[0] == 0xf5); */
730 ep->seen_f5 = 1;
731 ++buffer;
732 --buffer_length;
733
734 switch_port:
735 if (buffer_length <= 0)
736 break;
737 if (buffer[0] < 0x80) {
738 ep->current_port = (buffer[0] - 1) & 15;
739 ++buffer;
740 --buffer_length;
741 }
742 ep->seen_f5 = 0;
743 }
744}
745
Takashi Iwai86e07d32005-11-17 15:08:02 +0100746static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 int port0 = ep->current_port;
749 uint8_t* buf = ep->urb->transfer_buffer;
750 int buf_free = ep->max_transfer;
751 int length, i;
752
753 for (i = 0; i < 0x10; ++i) {
754 /* round-robin, starting at the last current port */
755 int portnum = (port0 + i) & 15;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100756 struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (!port->active)
759 continue;
760 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
761 port->active = 0;
762 continue;
763 }
764
765 if (portnum != ep->current_port) {
766 if (buf_free < 2)
767 break;
768 ep->current_port = portnum;
769 buf[0] = 0xf5;
770 buf[1] = (portnum + 1) & 15;
771 buf += 2;
772 buf_free -= 2;
773 }
774
775 if (buf_free < 1)
776 break;
777 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
778 if (length > 0) {
779 buf += length;
780 buf_free -= length;
781 if (buf_free < 1)
782 break;
783 }
784 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200785 if (buf_free < ep->max_transfer && buf_free > 0) {
786 *buf = 0xff;
787 --buf_free;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 ep->urb->transfer_buffer_length = ep->max_transfer - buf_free;
790}
791
792static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
793 .input = snd_usbmidi_emagic_input,
794 .output = snd_usbmidi_emagic_output,
795 .init_out_endpoint = snd_usbmidi_emagic_init_out,
796 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
797};
798
799
Takashi Iwai86e07d32005-11-17 15:08:02 +0100800static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100802 struct snd_usb_midi* umidi = substream->rmidi->private_data;
803 struct usbmidi_out_port* port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int i, j;
805
806 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
807 if (umidi->endpoints[i].out)
808 for (j = 0; j < 0x10; ++j)
809 if (umidi->endpoints[i].out->ports[j].substream == substream) {
810 port = &umidi->endpoints[i].out->ports[j];
811 break;
812 }
813 if (!port) {
814 snd_BUG();
815 return -ENXIO;
816 }
817 substream->runtime->private_data = port;
818 port->state = STATE_UNKNOWN;
819 return 0;
820}
821
Takashi Iwai86e07d32005-11-17 15:08:02 +0100822static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 return 0;
825}
826
Takashi Iwai86e07d32005-11-17 15:08:02 +0100827static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100829 struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 port->active = up;
832 if (up) {
833 if (port->ep->umidi->chip->shutdown) {
834 /* gobble up remaining bytes to prevent wait in
835 * snd_rawmidi_drain_output */
836 while (!snd_rawmidi_transmit_empty(substream))
837 snd_rawmidi_transmit_ack(substream, 1);
838 return;
839 }
840 tasklet_hi_schedule(&port->ep->tasklet);
841 }
842}
843
Takashi Iwai86e07d32005-11-17 15:08:02 +0100844static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 return 0;
847}
848
Takashi Iwai86e07d32005-11-17 15:08:02 +0100849static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 return 0;
852}
853
Takashi Iwai86e07d32005-11-17 15:08:02 +0100854static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100856 struct snd_usb_midi* umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (up)
859 set_bit(substream->number, &umidi->input_triggered);
860 else
861 clear_bit(substream->number, &umidi->input_triggered);
862}
863
Takashi Iwai86e07d32005-11-17 15:08:02 +0100864static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 .open = snd_usbmidi_output_open,
866 .close = snd_usbmidi_output_close,
867 .trigger = snd_usbmidi_output_trigger,
868};
869
Takashi Iwai86e07d32005-11-17 15:08:02 +0100870static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 .open = snd_usbmidi_input_open,
872 .close = snd_usbmidi_input_close,
873 .trigger = snd_usbmidi_input_trigger
874};
875
876/*
877 * Frees an input endpoint.
878 * May be called when ep hasn't been initialized completely.
879 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100880static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200883 usb_buffer_free(ep->umidi->chip->dev,
884 ep->urb->transfer_buffer_length,
885 ep->urb->transfer_buffer,
886 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 usb_free_urb(ep->urb);
888 }
889 kfree(ep);
890}
891
892/*
893 * Creates an input endpoint.
894 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100895static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
896 struct snd_usb_midi_endpoint_info* ep_info,
897 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100899 struct snd_usb_midi_in_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 void* buffer;
901 unsigned int pipe;
902 int length;
903
904 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200905 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (!ep)
907 return -ENOMEM;
908 ep->umidi = umidi;
909
910 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
911 if (!ep->urb) {
912 snd_usbmidi_in_endpoint_delete(ep);
913 return -ENOMEM;
914 }
915 if (ep_info->in_interval)
916 pipe = usb_rcvintpipe(umidi->chip->dev, ep_info->in_ep);
917 else
918 pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep);
919 length = usb_maxpacket(umidi->chip->dev, pipe, 0);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200920 buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL,
921 &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (!buffer) {
923 snd_usbmidi_in_endpoint_delete(ep);
924 return -ENOMEM;
925 }
926 if (ep_info->in_interval)
Clemens Ladisch3527a002005-09-26 10:03:09 +0200927 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
928 length, snd_usbmidi_in_urb_complete, ep,
929 ep_info->in_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 else
Clemens Ladisch3527a002005-09-26 10:03:09 +0200931 usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer,
932 length, snd_usbmidi_in_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200933 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 rep->in = ep;
936 return 0;
937}
938
939static unsigned int snd_usbmidi_count_bits(unsigned int x)
940{
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100941 unsigned int bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Clemens Ladisch62f09c32006-02-27 09:53:03 +0100943 for (bits = 0; x; ++bits)
944 x &= x - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return bits;
946}
947
948/*
949 * Frees an output endpoint.
950 * May be called when ep hasn't been initialized completely.
951 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100952static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (ep->urb) {
Clemens Ladisch55851f72005-08-15 08:34:16 +0200955 usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer,
956 ep->urb->transfer_buffer,
957 ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 usb_free_urb(ep->urb);
959 }
960 kfree(ep);
961}
962
963/*
964 * Creates an output endpoint, and initializes output ports.
965 */
Takashi Iwai86e07d32005-11-17 15:08:02 +0100966static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
967 struct snd_usb_midi_endpoint_info* ep_info,
968 struct snd_usb_midi_endpoint* rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100970 struct snd_usb_midi_out_endpoint* ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int i;
972 unsigned int pipe;
973 void* buffer;
974
975 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +0200976 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!ep)
978 return -ENOMEM;
979 ep->umidi = umidi;
980
981 ep->urb = usb_alloc_urb(0, GFP_KERNEL);
982 if (!ep->urb) {
983 snd_usbmidi_out_endpoint_delete(ep);
984 return -ENOMEM;
985 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +0200986 if (ep_info->out_interval)
987 pipe = usb_sndintpipe(umidi->chip->dev, ep_info->out_ep);
988 else
989 pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep);
Clemens Ladisch490cbd92007-05-07 09:29:32 +0200990 if (umidi->chip->usb_id == USB_ID(0x0a92, 0x1020)) /* ESI M4U */
991 /* FIXME: we need more URBs to get reasonable bandwidth here: */
992 ep->max_transfer = 4;
993 else
994 ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1);
Clemens Ladisch55851f72005-08-15 08:34:16 +0200995 buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer,
996 GFP_KERNEL, &ep->urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (!buffer) {
998 snd_usbmidi_out_endpoint_delete(ep);
999 return -ENOMEM;
1000 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001001 if (ep_info->out_interval)
1002 usb_fill_int_urb(ep->urb, umidi->chip->dev, pipe, buffer,
1003 ep->max_transfer, snd_usbmidi_out_urb_complete,
1004 ep, ep_info->out_interval);
1005 else
1006 usb_fill_bulk_urb(ep->urb, umidi->chip->dev,
1007 pipe, buffer, ep->max_transfer,
1008 snd_usbmidi_out_urb_complete, ep);
Clemens Ladisch55851f72005-08-15 08:34:16 +02001009 ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 spin_lock_init(&ep->buffer_lock);
1012 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1013
1014 for (i = 0; i < 0x10; ++i)
1015 if (ep_info->out_cables & (1 << i)) {
1016 ep->ports[i].ep = ep;
1017 ep->ports[i].cable = i << 4;
1018 }
1019
1020 if (umidi->usb_protocol_ops->init_out_endpoint)
1021 umidi->usb_protocol_ops->init_out_endpoint(ep);
1022
1023 rep->out = ep;
1024 return 0;
1025}
1026
1027/*
1028 * Frees everything.
1029 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001030static void snd_usbmidi_free(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 int i;
1033
1034 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001035 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (ep->out)
1037 snd_usbmidi_out_endpoint_delete(ep->out);
1038 if (ep->in)
1039 snd_usbmidi_in_endpoint_delete(ep->in);
1040 }
1041 kfree(umidi);
1042}
1043
1044/*
1045 * Unlinks all URBs (must be done before the usb_device is deleted).
1046 */
Clemens Ladischee733392005-04-25 10:34:13 +02001047void snd_usbmidi_disconnect(struct list_head* p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001049 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int i;
1051
Takashi Iwai86e07d32005-11-17 15:08:02 +01001052 umidi = list_entry(p, struct snd_usb_midi, list);
Clemens Ladischc8846972005-08-02 15:26:52 +02001053 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001055 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001056 if (ep->out)
1057 tasklet_kill(&ep->out->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (ep->out && ep->out->urb) {
1059 usb_kill_urb(ep->out->urb);
1060 if (umidi->usb_protocol_ops->finish_out_endpoint)
1061 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1062 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001063 if (ep->in)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 usb_kill_urb(ep->in->urb);
1065 }
1066}
1067
Takashi Iwai86e07d32005-11-17 15:08:02 +01001068static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001070 struct snd_usb_midi* umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 snd_usbmidi_free(umidi);
1072}
1073
Takashi Iwai86e07d32005-11-17 15:08:02 +01001074static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int stream, int number)
1076{
1077 struct list_head* list;
1078
1079 list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001080 struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (substream->number == number)
1082 return substream;
1083 }
1084 return NULL;
1085}
1086
1087/*
1088 * This list specifies names for ports that do not fit into the standard
1089 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1090 * such as internal control or synthesizer ports.
1091 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001092static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001093 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001094 short int port;
1095 short int voices;
1096 const char *name;
1097 unsigned int seq_flags;
1098} snd_usbmidi_port_info[] = {
1099#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1100 { .id = USB_ID(vendor, product), \
1101 .port = num, .voices = voices_, \
1102 .name = name_, .seq_flags = flags }
1103#define EXTERNAL_PORT(vendor, product, num, name) \
1104 PORT_INFO(vendor, product, num, name, 0, \
1105 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1106 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1107 SNDRV_SEQ_PORT_TYPE_PORT)
1108#define CONTROL_PORT(vendor, product, num, name) \
1109 PORT_INFO(vendor, product, num, name, 0, \
1110 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1111 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1112#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1113 PORT_INFO(vendor, product, num, name, voices, \
1114 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1115 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1116 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1117 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1118 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1119 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1120 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1121#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1122 PORT_INFO(vendor, product, num, name, voices, \
1123 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1124 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1125 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1126 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1127 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1128 SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1129 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1130 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001132 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001134 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1135 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1136 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1137 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1138 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1139 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001141 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1142 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001144 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1145 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1146 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001148 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1149 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1150 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001152 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1153 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1154 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001156 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001158 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1159 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1160 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1161 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001163 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001165 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1166 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1167 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001169 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1170 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1171 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1172 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001174 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1175 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001177 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1178 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1179 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001181 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1182 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1183 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001185 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1186 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001188 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1189 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001191 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1192 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1193 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001195 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1196 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1197 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001198 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001199 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001201 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1202 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001204 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1205 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001207 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1208 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1209 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210};
1211
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001212static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
1213{
1214 int i;
1215
1216 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1217 if (snd_usbmidi_port_info[i].id == umidi->chip->usb_id &&
1218 snd_usbmidi_port_info[i].port == number)
1219 return &snd_usbmidi_port_info[i];
1220 }
1221 return NULL;
1222}
1223
1224static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1225 struct snd_seq_port_info *seq_port_info)
1226{
1227 struct snd_usb_midi *umidi = rmidi->private_data;
1228 struct port_info *port_info;
1229
1230 /* TODO: read port flags from descriptors */
1231 port_info = find_port_info(umidi, number);
1232 if (port_info) {
1233 seq_port_info->type = port_info->seq_flags;
1234 seq_port_info->midi_voices = port_info->voices;
1235 }
1236}
1237
Takashi Iwai86e07d32005-11-17 15:08:02 +01001238static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int stream, int number,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001240 struct snd_rawmidi_substream ** rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001242 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 const char *name_format;
1244
Takashi Iwai86e07d32005-11-17 15:08:02 +01001245 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (!substream) {
1247 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1248 return;
1249 }
1250
1251 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001252 port_info = find_port_info(umidi, number);
1253 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 snprintf(substream->name, sizeof(substream->name),
1255 name_format, umidi->chip->card->shortname, number + 1);
1256
1257 *rsubstream = substream;
1258}
1259
1260/*
1261 * Creates the endpoints and their ports.
1262 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001263static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
1264 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 int i, j, err;
1267 int out_ports = 0, in_ports = 0;
1268
1269 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1270 if (endpoints[i].out_cables) {
1271 err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
1272 &umidi->endpoints[i]);
1273 if (err < 0)
1274 return err;
1275 }
1276 if (endpoints[i].in_cables) {
1277 err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
1278 &umidi->endpoints[i]);
1279 if (err < 0)
1280 return err;
1281 }
1282
1283 for (j = 0; j < 0x10; ++j) {
1284 if (endpoints[i].out_cables & (1 << j)) {
1285 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
1286 &umidi->endpoints[i].out->ports[j].substream);
1287 ++out_ports;
1288 }
1289 if (endpoints[i].in_cables & (1 << j)) {
1290 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1291 &umidi->endpoints[i].in->ports[j].substream);
1292 ++in_ports;
1293 }
1294 }
1295 }
1296 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1297 out_ports, in_ports);
1298 return 0;
1299}
1300
1301/*
1302 * Returns MIDIStreaming device capabilities.
1303 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001304static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
1305 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 struct usb_interface* intf;
1308 struct usb_host_interface *hostif;
1309 struct usb_interface_descriptor* intfd;
1310 struct usb_ms_header_descriptor* ms_header;
1311 struct usb_host_endpoint *hostep;
1312 struct usb_endpoint_descriptor* ep;
1313 struct usb_ms_endpoint_descriptor* ms_ep;
1314 int i, epidx;
1315
1316 intf = umidi->iface;
1317 if (!intf)
1318 return -ENXIO;
1319 hostif = &intf->altsetting[0];
1320 intfd = get_iface_desc(hostif);
1321 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1322 if (hostif->extralen >= 7 &&
1323 ms_header->bLength >= 7 &&
1324 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1325 ms_header->bDescriptorSubtype == HEADER)
1326 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1327 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1328 else
1329 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1330
1331 epidx = 0;
1332 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1333 hostep = &hostif->endpoint[i];
1334 ep = get_ep_desc(hostep);
1335 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1336 (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1337 continue;
1338 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1339 if (hostep->extralen < 4 ||
1340 ms_ep->bLength < 4 ||
1341 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1342 ms_ep->bDescriptorSubtype != MS_GENERAL)
1343 continue;
1344 if ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1345 if (endpoints[epidx].out_ep) {
1346 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1347 snd_printk(KERN_WARNING "too many endpoints\n");
1348 break;
1349 }
1350 }
1351 endpoints[epidx].out_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1352 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1353 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001354 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1355 /*
1356 * Low speed bulk transfers don't exist, so
1357 * force interrupt transfers for devices like
1358 * ESI MIDI Mate that try to use them anyway.
1359 */
1360 endpoints[epidx].out_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1362 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1363 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1364 } else {
1365 if (endpoints[epidx].in_ep) {
1366 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1367 snd_printk(KERN_WARNING "too many endpoints\n");
1368 break;
1369 }
1370 }
1371 endpoints[epidx].in_ep = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1372 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1373 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001374 else if (snd_usb_get_speed(umidi->chip->dev) == USB_SPEED_LOW)
1375 endpoints[epidx].in_interval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1377 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1378 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1379 }
1380 }
1381 return 0;
1382}
1383
1384/*
1385 * On Roland devices, use the second alternate setting to be able to use
1386 * the interrupt input endpoint.
1387 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001388static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 struct usb_interface* intf;
1391 struct usb_host_interface *hostif;
1392 struct usb_interface_descriptor* intfd;
1393
1394 intf = umidi->iface;
1395 if (!intf || intf->num_altsetting != 2)
1396 return;
1397
1398 hostif = &intf->altsetting[1];
1399 intfd = get_iface_desc(hostif);
1400 if (intfd->bNumEndpoints != 2 ||
1401 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1402 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1403 return;
1404
1405 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1406 intfd->bAlternateSetting);
1407 usb_set_interface(umidi->chip->dev, intfd->bInterfaceNumber,
1408 intfd->bAlternateSetting);
1409}
1410
1411/*
1412 * Try to find any usable endpoints in the interface.
1413 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001414static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
1415 struct snd_usb_midi_endpoint_info* endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int max_endpoints)
1417{
1418 struct usb_interface* intf;
1419 struct usb_host_interface *hostif;
1420 struct usb_interface_descriptor* intfd;
1421 struct usb_endpoint_descriptor* epd;
1422 int i, out_eps = 0, in_eps = 0;
1423
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001424 if (USB_ID_VENDOR(umidi->chip->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 snd_usbmidi_switch_roland_altsetting(umidi);
1426
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001427 if (endpoint[0].out_ep || endpoint[0].in_ep)
1428 return 0;
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 intf = umidi->iface;
1431 if (!intf || intf->num_altsetting < 1)
1432 return -ENOENT;
1433 hostif = intf->cur_altsetting;
1434 intfd = get_iface_desc(hostif);
1435
1436 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1437 epd = get_endpoint(hostif, i);
1438 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK &&
1439 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1440 continue;
1441 if (out_eps < max_endpoints &&
1442 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1443 endpoint[out_eps].out_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1444 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1445 endpoint[out_eps].out_interval = epd->bInterval;
1446 ++out_eps;
1447 }
1448 if (in_eps < max_endpoints &&
1449 (epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1450 endpoint[in_eps].in_ep = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1451 if ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
1452 endpoint[in_eps].in_interval = epd->bInterval;
1453 ++in_eps;
1454 }
1455 }
1456 return (out_eps || in_eps) ? 0 : -ENOENT;
1457}
1458
1459/*
1460 * Detects the endpoints for one-port-per-endpoint protocols.
1461 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001462static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
1463 struct snd_usb_midi_endpoint_info* endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 int err, i;
1466
1467 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1468 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1469 if (endpoints[i].out_ep)
1470 endpoints[i].out_cables = 0x0001;
1471 if (endpoints[i].in_ep)
1472 endpoints[i].in_cables = 0x0001;
1473 }
1474 return err;
1475}
1476
1477/*
1478 * Detects the endpoints and ports of Yamaha devices.
1479 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001480static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
1481 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 struct usb_interface* intf;
1484 struct usb_host_interface *hostif;
1485 struct usb_interface_descriptor* intfd;
1486 uint8_t* cs_desc;
1487
1488 intf = umidi->iface;
1489 if (!intf)
1490 return -ENOENT;
1491 hostif = intf->altsetting;
1492 intfd = get_iface_desc(hostif);
1493 if (intfd->bNumEndpoints < 1)
1494 return -ENOENT;
1495
1496 /*
1497 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1498 * necessarily with any useful contents. So simply count 'em.
1499 */
1500 for (cs_desc = hostif->extra;
1501 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
1502 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02001503 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (cs_desc[2] == MIDI_IN_JACK)
1505 endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1506 else if (cs_desc[2] == MIDI_OUT_JACK)
1507 endpoint->out_cables = (endpoint->out_cables << 1) | 1;
1508 }
1509 }
1510 if (!endpoint->in_cables && !endpoint->out_cables)
1511 return -ENOENT;
1512
1513 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
1514}
1515
1516/*
1517 * Creates the endpoints and their ports for Midiman devices.
1518 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001519static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
1520 struct snd_usb_midi_endpoint_info* endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001522 struct snd_usb_midi_endpoint_info ep_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct usb_interface* intf;
1524 struct usb_host_interface *hostif;
1525 struct usb_interface_descriptor* intfd;
1526 struct usb_endpoint_descriptor* epd;
1527 int cable, err;
1528
1529 intf = umidi->iface;
1530 if (!intf)
1531 return -ENOENT;
1532 hostif = intf->altsetting;
1533 intfd = get_iface_desc(hostif);
1534 /*
1535 * The various MidiSport devices have more or less random endpoint
1536 * numbers, so we have to identify the endpoints by their index in
1537 * the descriptor array, like the driver for that other OS does.
1538 *
1539 * There is one interrupt input endpoint for all input ports, one
1540 * bulk output endpoint for even-numbered ports, and one for odd-
1541 * numbered ports. Both bulk output endpoints have corresponding
1542 * input bulk endpoints (at indices 1 and 3) which aren't used.
1543 */
1544 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1545 snd_printdd(KERN_ERR "not enough endpoints\n");
1546 return -ENOENT;
1547 }
1548
1549 epd = get_endpoint(hostif, 0);
1550 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN ||
1551 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) {
1552 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
1553 return -ENXIO;
1554 }
1555 epd = get_endpoint(hostif, 2);
1556 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1557 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1558 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
1559 return -ENXIO;
1560 }
1561 if (endpoint->out_cables > 0x0001) {
1562 epd = get_endpoint(hostif, 4);
1563 if ((epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_OUT ||
1564 (epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK) {
1565 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
1566 return -ENXIO;
1567 }
1568 }
1569
1570 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1571 ep_info.out_cables = endpoint->out_cables & 0x5555;
1572 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1573 if (err < 0)
1574 return err;
1575
1576 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1577 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
1578 ep_info.in_cables = endpoint->in_cables;
1579 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
1580 if (err < 0)
1581 return err;
1582
1583 if (endpoint->out_cables > 0x0001) {
1584 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1585 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
1586 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
1587 if (err < 0)
1588 return err;
1589 }
1590
1591 for (cable = 0; cable < 0x10; ++cable) {
1592 if (endpoint->out_cables & (1 << cable))
1593 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
1594 &umidi->endpoints[cable & 1].out->ports[cable].substream);
1595 if (endpoint->in_cables & (1 << cable))
1596 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
1597 &umidi->endpoints[0].in->ports[cable].substream);
1598 }
1599 return 0;
1600}
1601
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001602static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
1603 .get_port_info = snd_usbmidi_get_port_info,
1604};
1605
Takashi Iwai86e07d32005-11-17 15:08:02 +01001606static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 int out_ports, int in_ports)
1608{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001609 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 int err;
1611
1612 err = snd_rawmidi_new(umidi->chip->card, "USB MIDI",
1613 umidi->chip->next_midi_device++,
1614 out_ports, in_ports, &rmidi);
1615 if (err < 0)
1616 return err;
1617 strcpy(rmidi->name, umidi->chip->card->shortname);
1618 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
1619 SNDRV_RAWMIDI_INFO_INPUT |
1620 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001621 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 rmidi->private_data = umidi;
1623 rmidi->private_free = snd_usbmidi_rawmidi_free;
1624 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
1625 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);
1626
1627 umidi->rmidi = rmidi;
1628 return 0;
1629}
1630
1631/*
1632 * Temporarily stop input.
1633 */
1634void snd_usbmidi_input_stop(struct list_head* p)
1635{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001636 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 int i;
1638
Takashi Iwai86e07d32005-11-17 15:08:02 +01001639 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +01001641 struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (ep->in)
1643 usb_kill_urb(ep->in->urb);
1644 }
1645}
1646
Takashi Iwai86e07d32005-11-17 15:08:02 +01001647static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 if (ep) {
1650 struct urb* urb = ep->urb;
1651 urb->dev = ep->umidi->chip->dev;
1652 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
1653 }
1654}
1655
1656/*
1657 * Resume input after a call to snd_usbmidi_input_stop().
1658 */
1659void snd_usbmidi_input_start(struct list_head* p)
1660{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001661 struct snd_usb_midi* umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 int i;
1663
Takashi Iwai86e07d32005-11-17 15:08:02 +01001664 umidi = list_entry(p, struct snd_usb_midi, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1666 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1667}
1668
1669/*
1670 * Creates and registers everything needed for a MIDI streaming interface.
1671 */
Takashi Iwai86e07d32005-11-17 15:08:02 +01001672int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 struct usb_interface* iface,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001674 const struct snd_usb_audio_quirk* quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Takashi Iwai86e07d32005-11-17 15:08:02 +01001676 struct snd_usb_midi* umidi;
1677 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 int out_ports, in_ports;
1679 int i, err;
1680
Takashi Iwai561b2202005-09-09 14:22:34 +02001681 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (!umidi)
1683 return -ENOMEM;
1684 umidi->chip = chip;
1685 umidi->iface = iface;
1686 umidi->quirk = quirk;
1687 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02001688 init_timer(&umidi->error_timer);
1689 umidi->error_timer.function = snd_usbmidi_error_timer;
1690 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /* detect the endpoint(s) to use */
1693 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001694 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
1695 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd05cc10432007-05-07 09:28:53 +02001697 if (chip->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
1698 umidi->usb_protocol_ops =
1699 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001700 break;
1701 case QUIRK_MIDI_FIXED_ENDPOINT:
1702 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001703 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001704 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1705 break;
1706 case QUIRK_MIDI_YAMAHA:
1707 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
1708 break;
1709 case QUIRK_MIDI_MIDIMAN:
1710 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
1711 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001712 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001713 err = 0;
1714 break;
1715 case QUIRK_MIDI_NOVATION:
1716 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
1717 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1718 break;
1719 case QUIRK_MIDI_RAW:
1720 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
1721 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1722 break;
1723 case QUIRK_MIDI_EMAGIC:
1724 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
1725 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01001726 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02001727 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
1728 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01001729 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02001730 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02001731 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
1732 break;
1733 default:
1734 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
1735 err = -ENXIO;
1736 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738 if (err < 0) {
1739 kfree(umidi);
1740 return err;
1741 }
1742
1743 /* create rawmidi device */
1744 out_ports = 0;
1745 in_ports = 0;
1746 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1747 out_ports += snd_usbmidi_count_bits(endpoints[i].out_cables);
1748 in_ports += snd_usbmidi_count_bits(endpoints[i].in_cables);
1749 }
1750 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
1751 if (err < 0) {
1752 kfree(umidi);
1753 return err;
1754 }
1755
1756 /* create endpoint/port structures */
1757 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
1758 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
1759 else
1760 err = snd_usbmidi_create_endpoints(umidi, endpoints);
1761 if (err < 0) {
1762 snd_usbmidi_free(umidi);
1763 return err;
1764 }
1765
1766 list_add(&umidi->list, &umidi->chip->midi_list);
1767
1768 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1769 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
1770 return 0;
1771}
1772
1773EXPORT_SYMBOL(snd_usb_create_midi_interface);
1774EXPORT_SYMBOL(snd_usbmidi_input_stop);
1775EXPORT_SYMBOL(snd_usbmidi_input_start);
1776EXPORT_SYMBOL(snd_usbmidi_disconnect);