midi.c 62.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * usbmidi.c - ALSA USB MIDI driver
 *
4
 * Copyright (c) 2002-2009 Clemens Ladisch
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 * All rights reserved.
 *
 * Based on the OSS usb-midi driver by NAGANO Daisuke,
 *          NetBSD's umidi driver by Takuya SHIOZAKI,
 *          the "USB Device Class Definition for MIDI Devices" by Roland
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed and/or modified under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/bitops.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/slab.h>
46
#include <linux/timer.h>
L
Linus Torvalds 已提交
47
#include <linux/usb.h>
48
#include <linux/wait.h>
49
#include <linux/usb/audio.h>
50
#include <linux/module.h>
51

L
Linus Torvalds 已提交
52
#include <sound/core.h>
53
#include <sound/control.h>
L
Linus Torvalds 已提交
54
#include <sound/rawmidi.h>
55
#include <sound/asequencer.h>
L
Linus Torvalds 已提交
56
#include "usbaudio.h"
D
Daniel Mack 已提交
57
#include "midi.h"
58
#include "power.h"
D
Daniel Mack 已提交
59
#include "helper.h"
L
Linus Torvalds 已提交
60 61 62 63 64 65

/*
 * define this to log all USB packets
 */
/* #define DUMP_PACKETS */

66 67 68 69 70 71
/*
 * how long to wait after some USB errors, so that khubd can disconnect() us
 * without too many spurious errors
 */
#define ERROR_DELAY_JIFFIES (HZ / 10)

72
#define OUTPUT_URBS 7
73 74
#define INPUT_URBS 7

L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
MODULE_DESCRIPTION("USB Audio/MIDI helper module");
MODULE_LICENSE("Dual BSD/GPL");


struct usb_ms_header_descriptor {
	__u8  bLength;
	__u8  bDescriptorType;
	__u8  bDescriptorSubtype;
	__u8  bcdMSC[2];
	__le16 wTotalLength;
} __attribute__ ((packed));

struct usb_ms_endpoint_descriptor {
	__u8  bLength;
	__u8  bDescriptorType;
	__u8  bDescriptorSubtype;
	__u8  bNumEmbMIDIJack;
	__u8  baAssocJackID[0];
} __attribute__ ((packed));

97 98 99
struct snd_usb_midi_in_endpoint;
struct snd_usb_midi_out_endpoint;
struct snd_usb_midi_endpoint;
L
Linus Torvalds 已提交
100 101

struct usb_protocol_ops {
102
	void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
103
	void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
L
Linus Torvalds 已提交
104
	void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
105 106
	void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*);
	void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*);
L
Linus Torvalds 已提交
107 108 109
};

struct snd_usb_midi {
110 111
	struct usb_device *dev;
	struct snd_card *card;
L
Linus Torvalds 已提交
112
	struct usb_interface *iface;
113 114
	const struct snd_usb_audio_quirk *quirk;
	struct snd_rawmidi *rmidi;
L
Linus Torvalds 已提交
115 116
	struct usb_protocol_ops* usb_protocol_ops;
	struct list_head list;
117
	struct timer_list error_timer;
118
	spinlock_t disc_lock;
119
	struct mutex mutex;
120 121
	u32 usb_id;
	int next_midi_device;
L
Linus Torvalds 已提交
122 123

	struct snd_usb_midi_endpoint {
124 125
		struct snd_usb_midi_out_endpoint *out;
		struct snd_usb_midi_in_endpoint *in;
L
Linus Torvalds 已提交
126 127
	} endpoints[MIDI_MAX_ENDPOINTS];
	unsigned long input_triggered;
128
	unsigned int opened;
129
	unsigned char disconnected;
130 131

	struct snd_kcontrol *roland_load_ctl;
L
Linus Torvalds 已提交
132 133 134
};

struct snd_usb_midi_out_endpoint {
135
	struct snd_usb_midi* umidi;
136 137 138 139 140
	struct out_urb_context {
		struct urb *urb;
		struct snd_usb_midi_out_endpoint *ep;
	} urbs[OUTPUT_URBS];
	unsigned int active_urbs;
141
	unsigned int drain_urbs;
L
Linus Torvalds 已提交
142 143
	int max_transfer;		/* size of urb buffer */
	struct tasklet_struct tasklet;
144
	unsigned int next_urb;
L
Linus Torvalds 已提交
145 146 147
	spinlock_t buffer_lock;

	struct usbmidi_out_port {
148 149
		struct snd_usb_midi_out_endpoint* ep;
		struct snd_rawmidi_substream *substream;
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162
		int active;
		uint8_t cable;		/* cable number << 4 */
		uint8_t state;
#define STATE_UNKNOWN	0
#define STATE_1PARAM	1
#define STATE_2PARAM_1	2
#define STATE_2PARAM_2	3
#define STATE_SYSEX_0	4
#define STATE_SYSEX_1	5
#define STATE_SYSEX_2	6
		uint8_t data[2];
	} ports[0x10];
	int current_port;
163 164

	wait_queue_head_t drain_wait;
L
Linus Torvalds 已提交
165 166 167
};

struct snd_usb_midi_in_endpoint {
168
	struct snd_usb_midi* umidi;
169
	struct urb* urbs[INPUT_URBS];
L
Linus Torvalds 已提交
170
	struct usbmidi_in_port {
171
		struct snd_rawmidi_substream *substream;
172
		u8 running_status_length;
L
Linus Torvalds 已提交
173
	} ports[0x10];
174 175
	u8 seen_f5;
	u8 error_resubmit;
L
Linus Torvalds 已提交
176 177 178
	int current_port;
};

179
static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187

static const uint8_t snd_usbmidi_cin_length[] = {
	0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
};

/*
 * Submits the URB, with error handling.
 */
A
Al Viro 已提交
188
static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196 197 198 199 200
{
	int err = usb_submit_urb(urb, flags);
	if (err < 0 && err != -ENODEV)
		snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
	return err;
}

/*
 * Error handling for URB completion functions.
 */
static int snd_usbmidi_urb_error(int status)
{
201 202 203 204 205 206 207 208
	switch (status) {
	/* manually unlinked, or device gone */
	case -ENOENT:
	case -ECONNRESET:
	case -ESHUTDOWN:
	case -ENODEV:
		return -ENODEV;
	/* errors that might occur during unplugging */
209 210 211
	case -EPROTO:
	case -ETIME:
	case -EILSEQ:
212 213 214 215 216
		return -EIO;
	default:
		snd_printk(KERN_ERR "urb status %d\n", status);
		return 0; /* continue */
	}
L
Linus Torvalds 已提交
217 218 219 220 221
}

/*
 * Receives a chunk of MIDI data.
 */
222
static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
L
Linus Torvalds 已提交
223 224
				   uint8_t* data, int length)
{
225
	struct usbmidi_in_port* port = &ep->ports[portidx];
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

	if (!port->substream) {
		snd_printd("unexpected port %d!\n", portidx);
		return;
	}
	if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
		return;
	snd_rawmidi_receive(port->substream, data, length);
}

#ifdef DUMP_PACKETS
static void dump_urb(const char *type, const u8 *data, int length)
{
	snd_printk(KERN_DEBUG "%s packet: [", type);
	for (; length > 0; ++data, --length)
		printk(" %02x", *data);
	printk(" ]\n");
}
#else
#define dump_urb(type, data, length) /* nothing */
#endif

/*
 * Processes the data read from the device.
 */
251
static void snd_usbmidi_in_urb_complete(struct urb* urb)
L
Linus Torvalds 已提交
252
{
253
	struct snd_usb_midi_in_endpoint* ep = urb->context;
L
Linus Torvalds 已提交
254 255 256 257 258 259

	if (urb->status == 0) {
		dump_urb("received", urb->transfer_buffer, urb->actual_length);
		ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
						   urb->actual_length);
	} else {
260 261 262 263 264 265 266
		int err = snd_usbmidi_urb_error(urb->status);
		if (err < 0) {
			if (err != -ENODEV) {
				ep->error_resubmit = 1;
				mod_timer(&ep->umidi->error_timer,
					  jiffies + ERROR_DELAY_JIFFIES);
			}
L
Linus Torvalds 已提交
267
			return;
268
		}
L
Linus Torvalds 已提交
269 270
	}

271
	urb->dev = ep->umidi->dev;
272
	snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
273 274
}

275
static void snd_usbmidi_out_urb_complete(struct urb* urb)
L
Linus Torvalds 已提交
276
{
277 278
	struct out_urb_context *context = urb->context;
	struct snd_usb_midi_out_endpoint* ep = context->ep;
279
	unsigned int urb_index;
L
Linus Torvalds 已提交
280 281

	spin_lock(&ep->buffer_lock);
282 283 284 285 286 287
	urb_index = context - ep->urbs;
	ep->active_urbs &= ~(1 << urb_index);
	if (unlikely(ep->drain_urbs)) {
		ep->drain_urbs &= ~(1 << urb_index);
		wake_up(&ep->drain_wait);
	}
L
Linus Torvalds 已提交
288 289
	spin_unlock(&ep->buffer_lock);
	if (urb->status < 0) {
290 291 292 293 294
		int err = snd_usbmidi_urb_error(urb->status);
		if (err < 0) {
			if (err != -ENODEV)
				mod_timer(&ep->umidi->error_timer,
					  jiffies + ERROR_DELAY_JIFFIES);
L
Linus Torvalds 已提交
295
			return;
296
		}
L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304
	}
	snd_usbmidi_do_output(ep);
}

/*
 * This is called when some data should be transferred to the device
 * (from one or more substreams).
 */
305
static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
L
Linus Torvalds 已提交
306
{
307 308
	unsigned int urb_index;
	struct urb* urb;
L
Linus Torvalds 已提交
309 310 311
	unsigned long flags;

	spin_lock_irqsave(&ep->buffer_lock, flags);
312
	if (ep->umidi->disconnected) {
L
Linus Torvalds 已提交
313 314 315 316
		spin_unlock_irqrestore(&ep->buffer_lock, flags);
		return;
	}

317
	urb_index = ep->next_urb;
318
	for (;;) {
319 320 321 322 323
		if (!(ep->active_urbs & (1 << urb_index))) {
			urb = ep->urbs[urb_index].urb;
			urb->transfer_buffer_length = 0;
			ep->umidi->usb_protocol_ops->output(ep, urb);
			if (urb->transfer_buffer_length == 0)
324
				break;
L
Linus Torvalds 已提交
325

326 327
			dump_urb("sending", urb->transfer_buffer,
				 urb->transfer_buffer_length);
328
			urb->dev = ep->umidi->dev;
329 330 331 332 333 334 335
			if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
				break;
			ep->active_urbs |= 1 << urb_index;
		}
		if (++urb_index >= OUTPUT_URBS)
			urb_index = 0;
		if (urb_index == ep->next_urb)
336
			break;
L
Linus Torvalds 已提交
337
	}
338
	ep->next_urb = urb_index;
L
Linus Torvalds 已提交
339 340 341 342 343
	spin_unlock_irqrestore(&ep->buffer_lock, flags);
}

static void snd_usbmidi_out_tasklet(unsigned long data)
{
344
	struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
L
Linus Torvalds 已提交
345 346 347 348

	snd_usbmidi_do_output(ep);
}

349 350 351
/* called after transfers had been interrupted due to some USB error */
static void snd_usbmidi_error_timer(unsigned long data)
{
352
	struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
353
	unsigned int i, j;
354

355 356 357 358 359
	spin_lock(&umidi->disc_lock);
	if (umidi->disconnected) {
		spin_unlock(&umidi->disc_lock);
		return;
	}
360
	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
361
		struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
362 363
		if (in && in->error_resubmit) {
			in->error_resubmit = 0;
364
			for (j = 0; j < INPUT_URBS; ++j) {
365
				in->urbs[j]->dev = umidi->dev;
366 367
				snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
			}
368 369 370 371
		}
		if (umidi->endpoints[i].out)
			snd_usbmidi_do_output(umidi->endpoints[i].out);
	}
372
	spin_unlock(&umidi->disc_lock);
373 374
}

L
Linus Torvalds 已提交
375
/* helper function to send static data that may not DMA-able */
376
static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
L
Linus Torvalds 已提交
377 378
				 const void *data, int len)
{
379
	int err = 0;
A
Alexey Dobriyan 已提交
380
	void *buf = kmemdup(data, len, GFP_KERNEL);
L
Linus Torvalds 已提交
381 382 383
	if (!buf)
		return -ENOMEM;
	dump_urb("sending", buf, len);
384
	if (ep->urbs[0].urb)
385
		err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
386
				   buf, len, NULL, 250);
L
Linus Torvalds 已提交
387 388 389 390 391 392 393 394 395 396
	kfree(buf);
	return err;
}

/*
 * Standard USB MIDI protocol: see the spec.
 * Midiman protocol: like the standard protocol, but the control byte is the
 * fourth byte in each packet, and uses length instead of CIN.
 */

397
static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
L
Linus Torvalds 已提交
398 399 400 401 402 403 404 405 406 407 408 409
				       uint8_t* buffer, int buffer_length)
{
	int i;

	for (i = 0; i + 3 < buffer_length; i += 4)
		if (buffer[i] != 0) {
			int cable = buffer[i] >> 4;
			int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
			snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
		}
}

410
static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
L
Linus Torvalds 已提交
411 412 413 414 415 416 417 418 419 420 421 422
				      uint8_t* buffer, int buffer_length)
{
	int i;

	for (i = 0; i + 3 < buffer_length; i += 4)
		if (buffer[i + 3] != 0) {
			int port = buffer[i + 3] >> 4;
			int length = buffer[i + 3] & 3;
			snd_usbmidi_input_data(ep, port, &buffer[i], length);
		}
}

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
/*
 * Buggy M-Audio device: running status on input results in a packet that has
 * the data bytes but not the status byte and that is marked with CIN 4.
 */
static void snd_usbmidi_maudio_broken_running_status_input(
					struct snd_usb_midi_in_endpoint* ep,
					uint8_t* buffer, int buffer_length)
{
	int i;

	for (i = 0; i + 3 < buffer_length; i += 4)
		if (buffer[i] != 0) {
			int cable = buffer[i] >> 4;
			u8 cin = buffer[i] & 0x0f;
			struct usbmidi_in_port *port = &ep->ports[cable];
			int length;
D
Daniel Mack 已提交
439

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
			length = snd_usbmidi_cin_length[cin];
			if (cin == 0xf && buffer[i + 1] >= 0xf8)
				; /* realtime msg: no running status change */
			else if (cin >= 0x8 && cin <= 0xe)
				/* channel msg */
				port->running_status_length = length - 1;
			else if (cin == 0x4 &&
				 port->running_status_length != 0 &&
				 buffer[i + 1] < 0x80)
				/* CIN 4 that is not a SysEx */
				length = port->running_status_length;
			else
				/*
				 * All other msgs cannot begin running status.
				 * (A channel msg sent as two or three CIN 0xF
				 * packets could in theory, but this device
				 * doesn't use this format.)
				 */
				port->running_status_length = 0;
			snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length);
		}
}

463 464 465 466 467 468 469 470 471 472 473 474 475 476
/*
 * CME protocol: like the standard protocol, but SysEx commands are sent as a
 * single USB packet preceded by a 0x0F byte.
 */
static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
				  uint8_t *buffer, int buffer_length)
{
	if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
		snd_usbmidi_standard_input(ep, buffer, buffer_length);
	else
		snd_usbmidi_input_data(ep, buffer[0] >> 4,
				       &buffer[1], buffer_length - 1);
}

L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
/*
 * Adds one USB MIDI packet to the output buffer.
 */
static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0,
					       uint8_t p1, uint8_t p2, uint8_t p3)
{

	uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
	buf[0] = p0;
	buf[1] = p1;
	buf[2] = p2;
	buf[3] = p3;
	urb->transfer_buffer_length += 4;
}

/*
 * Adds one Midiman packet to the output buffer.
 */
static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0,
					      uint8_t p1, uint8_t p2, uint8_t p3)
{

	uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length;
	buf[0] = p1;
	buf[1] = p2;
	buf[2] = p3;
	buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
	urb->transfer_buffer_length += 4;
}

/*
 * Converts MIDI commands to USB MIDI packets.
 */
510
static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
L
Linus Torvalds 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
				      uint8_t b, struct urb* urb)
{
	uint8_t p0 = port->cable;
	void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
		port->ep->umidi->usb_protocol_ops->output_packet;

	if (b >= 0xf8) {
		output_packet(urb, p0 | 0x0f, b, 0, 0);
	} else if (b >= 0xf0) {
		switch (b) {
		case 0xf0:
			port->data[0] = b;
			port->state = STATE_SYSEX_1;
			break;
		case 0xf1:
		case 0xf3:
			port->data[0] = b;
			port->state = STATE_1PARAM;
			break;
		case 0xf2:
			port->data[0] = b;
			port->state = STATE_2PARAM_1;
			break;
		case 0xf4:
		case 0xf5:
			port->state = STATE_UNKNOWN;
			break;
		case 0xf6:
			output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
			port->state = STATE_UNKNOWN;
			break;
		case 0xf7:
			switch (port->state) {
			case STATE_SYSEX_0:
				output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
				break;
			case STATE_SYSEX_1:
				output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
				break;
			case STATE_SYSEX_2:
				output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
				break;
			}
			port->state = STATE_UNKNOWN;
			break;
		}
	} else if (b >= 0x80) {
		port->data[0] = b;
		if (b >= 0xc0 && b <= 0xdf)
			port->state = STATE_1PARAM;
		else
			port->state = STATE_2PARAM_1;
	} else { /* b < 0x80 */
		switch (port->state) {
		case STATE_1PARAM:
			if (port->data[0] < 0xf0) {
				p0 |= port->data[0] >> 4;
			} else {
				p0 |= 0x02;
				port->state = STATE_UNKNOWN;
			}
			output_packet(urb, p0, port->data[0], b, 0);
			break;
		case STATE_2PARAM_1:
			port->data[1] = b;
			port->state = STATE_2PARAM_2;
			break;
		case STATE_2PARAM_2:
			if (port->data[0] < 0xf0) {
				p0 |= port->data[0] >> 4;
				port->state = STATE_2PARAM_1;
			} else {
				p0 |= 0x03;
				port->state = STATE_UNKNOWN;
			}
			output_packet(urb, p0, port->data[0], port->data[1], b);
			break;
		case STATE_SYSEX_0:
			port->data[0] = b;
			port->state = STATE_SYSEX_1;
			break;
		case STATE_SYSEX_1:
			port->data[1] = b;
			port->state = STATE_SYSEX_2;
			break;
		case STATE_SYSEX_2:
			output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b);
			port->state = STATE_SYSEX_0;
			break;
		}
	}
}

604 605
static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
					struct urb *urb)
L
Linus Torvalds 已提交
606 607 608 609 610
{
	int p;

	/* FIXME: lower-numbered ports can starve higher-numbered ports */
	for (p = 0; p < 0x10; ++p) {
611
		struct usbmidi_out_port* port = &ep->ports[p];
L
Linus Torvalds 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
		if (!port->active)
			continue;
		while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
			uint8_t b;
			if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
				port->active = 0;
				break;
			}
			snd_usbmidi_transmit_byte(port, b, urb);
		}
	}
}

static struct usb_protocol_ops snd_usbmidi_standard_ops = {
	.input = snd_usbmidi_standard_input,
	.output = snd_usbmidi_standard_output,
	.output_packet = snd_usbmidi_output_standard_packet,
};

static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
	.input = snd_usbmidi_midiman_input,
D
Daniel Mack 已提交
633
	.output = snd_usbmidi_standard_output,
L
Linus Torvalds 已提交
634 635 636
	.output_packet = snd_usbmidi_output_midiman_packet,
};

637 638
static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
	.input = snd_usbmidi_maudio_broken_running_status_input,
D
Daniel Mack 已提交
639
	.output = snd_usbmidi_standard_output,
640 641 642
	.output_packet = snd_usbmidi_output_standard_packet,
};

643 644 645 646 647 648
static struct usb_protocol_ops snd_usbmidi_cme_ops = {
	.input = snd_usbmidi_cme_input,
	.output = snd_usbmidi_standard_output,
	.output_packet = snd_usbmidi_output_standard_packet,
};

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
/*
 * AKAI MPD16 protocol:
 *
 * For control port (endpoint 1):
 * ==============================
 * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
 * SysEx message (msg_len=9 bytes long).
 *
 * For data port (endpoint 2):
 * ===========================
 * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
 * MIDI message (msg_len bytes long)
 *
 * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
 */
static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
				   uint8_t *buffer, int buffer_length)
{
	unsigned int pos = 0;
	unsigned int len = (unsigned int)buffer_length;
	while (pos < len) {
		unsigned int port = (buffer[pos] >> 4) - 1;
		unsigned int msg_len = buffer[pos] & 0x0f;
		pos++;
		if (pos + msg_len <= len && port < 2)
			snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
		pos += msg_len;
	}
}

#define MAX_AKAI_SYSEX_LEN 9

static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
				    struct urb *urb)
{
	uint8_t *msg;
	int pos, end, count, buf_end;
	uint8_t tmp[MAX_AKAI_SYSEX_LEN];
	struct snd_rawmidi_substream *substream = ep->ports[0].substream;

	if (!ep->ports[0].active)
		return;

	msg = urb->transfer_buffer + urb->transfer_buffer_length;
	buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;

	/* only try adding more data when there's space for at least 1 SysEx */
	while (urb->transfer_buffer_length < buf_end) {
		count = snd_rawmidi_transmit_peek(substream,
						  tmp, MAX_AKAI_SYSEX_LEN);
		if (!count) {
			ep->ports[0].active = 0;
			return;
		}
		/* try to skip non-SysEx data */
		for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
			;

		if (pos > 0) {
			snd_rawmidi_transmit_ack(substream, pos);
			continue;
		}

		/* look for the start or end marker */
		for (end = 1; end < count && tmp[end] < 0xF0; end++)
			;

		/* next SysEx started before the end of current one */
		if (end < count && tmp[end] == 0xF0) {
			/* it's incomplete - drop it */
			snd_rawmidi_transmit_ack(substream, end);
			continue;
		}
		/* SysEx complete */
		if (end < count && tmp[end] == 0xF7) {
			/* queue it, ack it, and get the next one */
			count = end + 1;
			msg[0] = 0x10 | count;
			memcpy(&msg[1], tmp, count);
			snd_rawmidi_transmit_ack(substream, count);
			urb->transfer_buffer_length += count + 1;
			msg += count + 1;
			continue;
		}
		/* less than 9 bytes and no end byte - wait for more */
		if (count < MAX_AKAI_SYSEX_LEN) {
			ep->ports[0].active = 0;
			return;
		}
		/* 9 bytes and no end marker in sight - malformed, skip it */
		snd_rawmidi_transmit_ack(substream, count);
	}
}

static struct usb_protocol_ops snd_usbmidi_akai_ops = {
	.input = snd_usbmidi_akai_input,
	.output = snd_usbmidi_akai_output,
};

L
Linus Torvalds 已提交
748 749 750 751 752 753
/*
 * Novation USB MIDI protocol: number of data bytes is in the first byte
 * (when receiving) (+1!) or in the second byte (when sending); data begins
 * at the third byte.
 */

754
static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
L
Linus Torvalds 已提交
755 756 757 758 759 760 761
				       uint8_t* buffer, int buffer_length)
{
	if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
		return;
	snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
}

762 763
static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
					struct urb *urb)
L
Linus Torvalds 已提交
764 765 766 767 768 769
{
	uint8_t* transfer_buffer;
	int count;

	if (!ep->ports[0].active)
		return;
770
	transfer_buffer = urb->transfer_buffer;
L
Linus Torvalds 已提交
771 772 773 774 775 776 777 778 779
	count = snd_rawmidi_transmit(ep->ports[0].substream,
				     &transfer_buffer[2],
				     ep->max_transfer - 2);
	if (count < 1) {
		ep->ports[0].active = 0;
		return;
	}
	transfer_buffer[0] = 0;
	transfer_buffer[1] = count;
780
	urb->transfer_buffer_length = 2 + count;
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788
}

static struct usb_protocol_ops snd_usbmidi_novation_ops = {
	.input = snd_usbmidi_novation_input,
	.output = snd_usbmidi_novation_output,
};

/*
789
 * "raw" protocol: just move raw MIDI bytes from/to the endpoint
L
Linus Torvalds 已提交
790 791
 */

792
static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
793
				  uint8_t* buffer, int buffer_length)
L
Linus Torvalds 已提交
794 795 796 797
{
	snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
}

798 799
static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
				   struct urb *urb)
L
Linus Torvalds 已提交
800 801 802 803 804 805
{
	int count;

	if (!ep->ports[0].active)
		return;
	count = snd_rawmidi_transmit(ep->ports[0].substream,
806
				     urb->transfer_buffer,
L
Linus Torvalds 已提交
807 808 809 810 811
				     ep->max_transfer);
	if (count < 1) {
		ep->ports[0].active = 0;
		return;
	}
812
	urb->transfer_buffer_length = count;
L
Linus Torvalds 已提交
813 814
}

815 816 817
static struct usb_protocol_ops snd_usbmidi_raw_ops = {
	.input = snd_usbmidi_raw_input,
	.output = snd_usbmidi_raw_output,
L
Linus Torvalds 已提交
818 819
};

820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
/*
 * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
 */

static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint* ep,
				   uint8_t* buffer, int buffer_length)
{
	if (buffer_length > 2)
		snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
}

static struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
	.input = snd_usbmidi_ftdi_input,
	.output = snd_usbmidi_raw_output,
};

K
Karsten Wiese 已提交
836 837 838 839 840 841 842 843 844 845 846 847
static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
				     uint8_t *buffer, int buffer_length)
{
	if (buffer_length != 9)
		return;
	buffer_length = 8;
	while (buffer_length && buffer[buffer_length - 1] == 0xFD)
		buffer_length--;
	if (buffer_length)
		snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
}

848 849
static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
				      struct urb *urb)
K
Karsten Wiese 已提交
850 851 852 853 854
{
	int count;

	if (!ep->ports[0].active)
		return;
855 856 857 858 859 860 861 862
	switch (snd_usb_get_speed(ep->umidi->dev)) {
	case USB_SPEED_HIGH:
	case USB_SPEED_SUPER:
		count = 1;
		break;
	default:
		count = 2;
	}
K
Karsten Wiese 已提交
863
	count = snd_rawmidi_transmit(ep->ports[0].substream,
864
				     urb->transfer_buffer,
K
Karsten Wiese 已提交
865 866 867 868 869 870
				     count);
	if (count < 1) {
		ep->ports[0].active = 0;
		return;
	}

871 872
	memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
	urb->transfer_buffer_length = ep->max_transfer;
K
Karsten Wiese 已提交
873 874 875 876 877 878 879
}

static struct usb_protocol_ops snd_usbmidi_122l_ops = {
	.input = snd_usbmidi_us122l_input,
	.output = snd_usbmidi_us122l_output,
};

L
Linus Torvalds 已提交
880 881 882 883
/*
 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
 */

884
static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
L
Linus Torvalds 已提交
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
{
	static const u8 init_data[] = {
		/* initialization magic: "get version" */
		0xf0,
		0x00, 0x20, 0x31,	/* Emagic */
		0x64,			/* Unitor8 */
		0x0b,			/* version number request */
		0x00,			/* command version */
		0x00,			/* EEPROM, box 0 */
		0xf7
	};
	send_bulk_static_data(ep, init_data, sizeof(init_data));
	/* while we're at it, pour on more magic */
	send_bulk_static_data(ep, init_data, sizeof(init_data));
}

901
static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
L
Linus Torvalds 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
{
	static const u8 finish_data[] = {
		/* switch to patch mode with last preset */
		0xf0,
		0x00, 0x20, 0x31,	/* Emagic */
		0x64,			/* Unitor8 */
		0x10,			/* patch switch command */
		0x00,			/* command version */
		0x7f,			/* to all boxes */
		0x40,			/* last preset in EEPROM */
		0xf7
	};
	send_bulk_static_data(ep, finish_data, sizeof(finish_data));
}

917
static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
L
Linus Torvalds 已提交
918 919
				     uint8_t* buffer, int buffer_length)
{
920 921 922 923 924 925 926 927
	int i;

	/* FF indicates end of valid data */
	for (i = 0; i < buffer_length; ++i)
		if (buffer[i] == 0xff) {
			buffer_length = i;
			break;
		}
L
Linus Torvalds 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960

	/* handle F5 at end of last buffer */
	if (ep->seen_f5)
		goto switch_port;

	while (buffer_length > 0) {
		/* determine size of data until next F5 */
		for (i = 0; i < buffer_length; ++i)
			if (buffer[i] == 0xf5)
				break;
		snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
		buffer += i;
		buffer_length -= i;

		if (buffer_length <= 0)
			break;
		/* assert(buffer[0] == 0xf5); */
		ep->seen_f5 = 1;
		++buffer;
		--buffer_length;

	switch_port:
		if (buffer_length <= 0)
			break;
		if (buffer[0] < 0x80) {
			ep->current_port = (buffer[0] - 1) & 15;
			++buffer;
			--buffer_length;
		}
		ep->seen_f5 = 0;
	}
}

961 962
static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
				      struct urb *urb)
L
Linus Torvalds 已提交
963 964
{
	int port0 = ep->current_port;
965
	uint8_t* buf = urb->transfer_buffer;
L
Linus Torvalds 已提交
966 967 968 969 970 971
	int buf_free = ep->max_transfer;
	int length, i;

	for (i = 0; i < 0x10; ++i) {
		/* round-robin, starting at the last current port */
		int portnum = (port0 + i) & 15;
972
		struct usbmidi_out_port* port = &ep->ports[portnum];
L
Linus Torvalds 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000

		if (!port->active)
			continue;
		if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
			port->active = 0;
			continue;
		}

		if (portnum != ep->current_port) {
			if (buf_free < 2)
				break;
			ep->current_port = portnum;
			buf[0] = 0xf5;
			buf[1] = (portnum + 1) & 15;
			buf += 2;
			buf_free -= 2;
		}

		if (buf_free < 1)
			break;
		length = snd_rawmidi_transmit(port->substream, buf, buf_free);
		if (length > 0) {
			buf += length;
			buf_free -= length;
			if (buf_free < 1)
				break;
		}
	}
1001 1002 1003 1004
	if (buf_free < ep->max_transfer && buf_free > 0) {
		*buf = 0xff;
		--buf_free;
	}
1005
	urb->transfer_buffer_length = ep->max_transfer - buf_free;
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
}

static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
	.input = snd_usbmidi_emagic_input,
	.output = snd_usbmidi_emagic_output,
	.init_out_endpoint = snd_usbmidi_emagic_init_out,
	.finish_out_endpoint = snd_usbmidi_emagic_finish_out,
};


1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
static void update_roland_altsetting(struct snd_usb_midi* umidi)
{
	struct usb_interface *intf;
	struct usb_host_interface *hostif;
	struct usb_interface_descriptor *intfd;
	int is_light_load;

	intf = umidi->iface;
	is_light_load = intf->cur_altsetting != intf->altsetting;
	if (umidi->roland_load_ctl->private_value == is_light_load)
		return;
	hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
	intfd = get_iface_desc(hostif);
	snd_usbmidi_input_stop(&umidi->list);
1030
	usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
			  intfd->bAlternateSetting);
	snd_usbmidi_input_start(&umidi->list);
}

static void substream_open(struct snd_rawmidi_substream *substream, int open)
{
	struct snd_usb_midi* umidi = substream->rmidi->private_data;
	struct snd_kcontrol *ctl;

	mutex_lock(&umidi->mutex);
	if (open) {
		if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
			ctl = umidi->roland_load_ctl;
			ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1045
			snd_ctl_notify(umidi->card,
1046 1047 1048 1049 1050 1051 1052
				       SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
			update_roland_altsetting(umidi);
		}
	} else {
		if (--umidi->opened == 0 && umidi->roland_load_ctl) {
			ctl = umidi->roland_load_ctl;
			ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1053
			snd_ctl_notify(umidi->card,
1054 1055 1056 1057 1058 1059
				       SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
		}
	}
	mutex_unlock(&umidi->mutex);
}

1060
static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
1061
{
1062 1063
	struct snd_usb_midi* umidi = substream->rmidi->private_data;
	struct usbmidi_out_port* port = NULL;
L
Linus Torvalds 已提交
1064
	int i, j;
1065
	int err;
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
		if (umidi->endpoints[i].out)
			for (j = 0; j < 0x10; ++j)
				if (umidi->endpoints[i].out->ports[j].substream == substream) {
					port = &umidi->endpoints[i].out->ports[j];
					break;
				}
	if (!port) {
		snd_BUG();
		return -ENXIO;
	}
1078 1079 1080
	err = usb_autopm_get_interface(umidi->iface);
	if (err < 0)
		return -EIO;
L
Linus Torvalds 已提交
1081 1082
	substream->runtime->private_data = port;
	port->state = STATE_UNKNOWN;
1083
	substream_open(substream, 1);
L
Linus Torvalds 已提交
1084 1085 1086
	return 0;
}

1087
static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
1088
{
1089 1090
	struct snd_usb_midi* umidi = substream->rmidi->private_data;

1091
	substream_open(substream, 0);
1092
	usb_autopm_put_interface(umidi->iface);
L
Linus Torvalds 已提交
1093 1094 1095
	return 0;
}

1096
static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
L
Linus Torvalds 已提交
1097
{
1098
	struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data;
L
Linus Torvalds 已提交
1099 1100 1101

	port->active = up;
	if (up) {
1102
		if (port->ep->umidi->disconnected) {
L
Linus Torvalds 已提交
1103 1104 1105 1106 1107 1108
			/* gobble up remaining bytes to prevent wait in
			 * snd_rawmidi_drain_output */
			while (!snd_rawmidi_transmit_empty(substream))
				snd_rawmidi_transmit_ack(substream, 1);
			return;
		}
1109
		tasklet_schedule(&port->ep->tasklet);
L
Linus Torvalds 已提交
1110 1111 1112
	}
}

1113 1114 1115 1116 1117 1118 1119 1120
static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
{
	struct usbmidi_out_port* port = substream->runtime->private_data;
	struct snd_usb_midi_out_endpoint *ep = port->ep;
	unsigned int drain_urbs;
	DEFINE_WAIT(wait);
	long timeout = msecs_to_jiffies(50);

1121 1122
	if (ep->umidi->disconnected)
		return;
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	/*
	 * The substream buffer is empty, but some data might still be in the
	 * currently active URBs, so we have to wait for those to complete.
	 */
	spin_lock_irq(&ep->buffer_lock);
	drain_urbs = ep->active_urbs;
	if (drain_urbs) {
		ep->drain_urbs |= drain_urbs;
		do {
			prepare_to_wait(&ep->drain_wait, &wait,
					TASK_UNINTERRUPTIBLE);
			spin_unlock_irq(&ep->buffer_lock);
			timeout = schedule_timeout(timeout);
			spin_lock_irq(&ep->buffer_lock);
			drain_urbs &= ep->drain_urbs;
		} while (drain_urbs && timeout);
		finish_wait(&ep->drain_wait, &wait);
	}
	spin_unlock_irq(&ep->buffer_lock);
}

1144
static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
1145
{
1146
	substream_open(substream, 1);
L
Linus Torvalds 已提交
1147 1148 1149
	return 0;
}

1150
static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
L
Linus Torvalds 已提交
1151
{
1152
	substream_open(substream, 0);
L
Linus Torvalds 已提交
1153 1154 1155
	return 0;
}

1156
static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
L
Linus Torvalds 已提交
1157
{
1158
	struct snd_usb_midi* umidi = substream->rmidi->private_data;
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163 1164 1165

	if (up)
		set_bit(substream->number, &umidi->input_triggered);
	else
		clear_bit(substream->number, &umidi->input_triggered);
}

1166
static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
L
Linus Torvalds 已提交
1167 1168 1169
	.open = snd_usbmidi_output_open,
	.close = snd_usbmidi_output_close,
	.trigger = snd_usbmidi_output_trigger,
1170
	.drain = snd_usbmidi_output_drain,
L
Linus Torvalds 已提交
1171 1172
};

1173
static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178
	.open = snd_usbmidi_input_open,
	.close = snd_usbmidi_input_close,
	.trigger = snd_usbmidi_input_trigger
};

1179 1180 1181
static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
				unsigned int buffer_length)
{
1182 1183
	usb_free_coherent(umidi->dev, buffer_length,
			  urb->transfer_buffer, urb->transfer_dma);
1184 1185 1186
	usb_free_urb(urb);
}

L
Linus Torvalds 已提交
1187 1188 1189 1190
/*
 * Frees an input endpoint.
 * May be called when ep hasn't been initialized completely.
 */
1191
static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep)
L
Linus Torvalds 已提交
1192
{
1193 1194
	unsigned int i;

1195 1196 1197 1198
	for (i = 0; i < INPUT_URBS; ++i)
		if (ep->urbs[i])
			free_urb_and_buffer(ep->umidi, ep->urbs[i],
					    ep->urbs[i]->transfer_buffer_length);
L
Linus Torvalds 已提交
1199 1200 1201 1202 1203 1204
	kfree(ep);
}

/*
 * Creates an input endpoint.
 */
1205 1206 1207
static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
					  struct snd_usb_midi_endpoint_info* ep_info,
					  struct snd_usb_midi_endpoint* rep)
L
Linus Torvalds 已提交
1208
{
1209
	struct snd_usb_midi_in_endpoint* ep;
L
Linus Torvalds 已提交
1210 1211 1212
	void* buffer;
	unsigned int pipe;
	int length;
1213
	unsigned int i;
L
Linus Torvalds 已提交
1214 1215

	rep->in = NULL;
1216
	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
L
Linus Torvalds 已提交
1217 1218 1219 1220
	if (!ep)
		return -ENOMEM;
	ep->umidi = umidi;

1221 1222 1223 1224 1225 1226
	for (i = 0; i < INPUT_URBS; ++i) {
		ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
		if (!ep->urbs[i]) {
			snd_usbmidi_in_endpoint_delete(ep);
			return -ENOMEM;
		}
L
Linus Torvalds 已提交
1227 1228
	}
	if (ep_info->in_interval)
1229
		pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
L
Linus Torvalds 已提交
1230
	else
1231 1232
		pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
	length = usb_maxpacket(umidi->dev, pipe, 0);
1233
	for (i = 0; i < INPUT_URBS; ++i) {
1234 1235
		buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
					    &ep->urbs[i]->transfer_dma);
1236 1237 1238 1239 1240
		if (!buffer) {
			snd_usbmidi_in_endpoint_delete(ep);
			return -ENOMEM;
		}
		if (ep_info->in_interval)
1241
			usb_fill_int_urb(ep->urbs[i], umidi->dev,
1242 1243 1244 1245
					 pipe, buffer, length,
					 snd_usbmidi_in_urb_complete,
					 ep, ep_info->in_interval);
		else
1246
			usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
1247 1248 1249
					  pipe, buffer, length,
					  snd_usbmidi_in_urb_complete, ep);
		ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
L
Linus Torvalds 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
	}

	rep->in = ep;
	return 0;
}

/*
 * Frees an output endpoint.
 * May be called when ep hasn't been initialized completely.
 */
1260
static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
L
Linus Torvalds 已提交
1261
{
1262 1263 1264
	unsigned int i;

	for (i = 0; i < OUTPUT_URBS; ++i)
1265
		if (ep->urbs[i].urb) {
1266 1267
			free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
					    ep->max_transfer);
1268 1269 1270 1271 1272 1273 1274
			ep->urbs[i].urb = NULL;
		}
}

static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
{
	snd_usbmidi_out_endpoint_clear(ep);
L
Linus Torvalds 已提交
1275 1276 1277 1278 1279 1280
	kfree(ep);
}

/*
 * Creates an output endpoint, and initializes output ports.
 */
1281 1282
static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
					   struct snd_usb_midi_endpoint_info* ep_info,
D
Daniel Mack 已提交
1283
					   struct snd_usb_midi_endpoint* rep)
L
Linus Torvalds 已提交
1284
{
1285
	struct snd_usb_midi_out_endpoint* ep;
1286
	unsigned int i;
L
Linus Torvalds 已提交
1287 1288 1289 1290
	unsigned int pipe;
	void* buffer;

	rep->out = NULL;
1291
	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
L
Linus Torvalds 已提交
1292 1293 1294 1295
	if (!ep)
		return -ENOMEM;
	ep->umidi = umidi;

1296 1297 1298 1299 1300 1301 1302
	for (i = 0; i < OUTPUT_URBS; ++i) {
		ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
		if (!ep->urbs[i].urb) {
			snd_usbmidi_out_endpoint_delete(ep);
			return -ENOMEM;
		}
		ep->urbs[i].ep = ep;
L
Linus Torvalds 已提交
1303
	}
1304
	if (ep_info->out_interval)
1305
		pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
1306
	else
1307
		pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
1308 1309
	switch (umidi->usb_id) {
	default:
1310
		ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
		break;
		/*
		 * Various chips declare a packet size larger than 4 bytes, but
		 * do not actually work with larger packets:
		 */
	case USB_ID(0x0a92, 0x1020): /* ESI M4U */
	case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
	case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
	case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
	case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1321
	case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
1322 1323
		ep->max_transfer = 4;
		break;
1324 1325 1326 1327 1328 1329 1330
		/*
		 * Some devices only work with 9 bytes packet size:
		 */
	case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
	case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
		ep->max_transfer = 9;
		break;
1331
	}
1332
	for (i = 0; i < OUTPUT_URBS; ++i) {
1333 1334 1335
		buffer = usb_alloc_coherent(umidi->dev,
					    ep->max_transfer, GFP_KERNEL,
					    &ep->urbs[i].urb->transfer_dma);
1336 1337 1338 1339 1340
		if (!buffer) {
			snd_usbmidi_out_endpoint_delete(ep);
			return -ENOMEM;
		}
		if (ep_info->out_interval)
1341
			usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
1342 1343 1344 1345
					 pipe, buffer, ep->max_transfer,
					 snd_usbmidi_out_urb_complete,
					 &ep->urbs[i], ep_info->out_interval);
		else
1346
			usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
1347 1348 1349 1350
					  pipe, buffer, ep->max_transfer,
					  snd_usbmidi_out_urb_complete,
					  &ep->urbs[i]);
		ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
L
Linus Torvalds 已提交
1351 1352 1353 1354
	}

	spin_lock_init(&ep->buffer_lock);
	tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
1355
	init_waitqueue_head(&ep->drain_wait);
L
Linus Torvalds 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

	for (i = 0; i < 0x10; ++i)
		if (ep_info->out_cables & (1 << i)) {
			ep->ports[i].ep = ep;
			ep->ports[i].cable = i << 4;
		}

	if (umidi->usb_protocol_ops->init_out_endpoint)
		umidi->usb_protocol_ops->init_out_endpoint(ep);

	rep->out = ep;
	return 0;
}

/*
 * Frees everything.
 */
1373
static void snd_usbmidi_free(struct snd_usb_midi* umidi)
L
Linus Torvalds 已提交
1374 1375 1376 1377
{
	int i;

	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1378
		struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
L
Linus Torvalds 已提交
1379 1380 1381 1382 1383
		if (ep->out)
			snd_usbmidi_out_endpoint_delete(ep->out);
		if (ep->in)
			snd_usbmidi_in_endpoint_delete(ep->in);
	}
1384
	mutex_destroy(&umidi->mutex);
L
Linus Torvalds 已提交
1385 1386 1387 1388 1389 1390
	kfree(umidi);
}

/*
 * Unlinks all URBs (must be done before the usb_device is deleted).
 */
1391
void snd_usbmidi_disconnect(struct list_head* p)
L
Linus Torvalds 已提交
1392
{
1393
	struct snd_usb_midi* umidi;
1394
	unsigned int i, j;
L
Linus Torvalds 已提交
1395

1396
	umidi = list_entry(p, struct snd_usb_midi, list);
1397 1398 1399 1400 1401 1402 1403 1404
	/*
	 * an URB's completion handler may start the timer and
	 * a timer may submit an URB. To reliably break the cycle
	 * a flag under lock must be used
	 */
	spin_lock_irq(&umidi->disc_lock);
	umidi->disconnected = 1;
	spin_unlock_irq(&umidi->disc_lock);
L
Linus Torvalds 已提交
1405
	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1406
		struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
1407 1408
		if (ep->out)
			tasklet_kill(&ep->out->tasklet);
1409 1410 1411
		if (ep->out) {
			for (j = 0; j < OUTPUT_URBS; ++j)
				usb_kill_urb(ep->out->urbs[j].urb);
L
Linus Torvalds 已提交
1412 1413
			if (umidi->usb_protocol_ops->finish_out_endpoint)
				umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1414 1415 1416 1417 1418
			ep->out->active_urbs = 0;
			if (ep->out->drain_urbs) {
				ep->out->drain_urbs = 0;
				wake_up(&ep->out->drain_wait);
			}
L
Linus Torvalds 已提交
1419
		}
M
Mariusz Kozlowski 已提交
1420
		if (ep->in)
1421 1422
			for (j = 0; j < INPUT_URBS; ++j)
				usb_kill_urb(ep->in->urbs[j]);
1423
		/* free endpoints here; later call can result in Oops */
1424 1425
		if (ep->out)
			snd_usbmidi_out_endpoint_clear(ep->out);
1426 1427 1428 1429
		if (ep->in) {
			snd_usbmidi_in_endpoint_delete(ep->in);
			ep->in = NULL;
		}
L
Linus Torvalds 已提交
1430
	}
1431
	del_timer_sync(&umidi->error_timer);
L
Linus Torvalds 已提交
1432 1433
}

1434
static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
L
Linus Torvalds 已提交
1435
{
1436
	struct snd_usb_midi* umidi = rmidi->private_data;
L
Linus Torvalds 已提交
1437 1438 1439
	snd_usbmidi_free(umidi);
}

1440
static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
D
Daniel Mack 已提交
1441
								int stream, int number)
L
Linus Torvalds 已提交
1442 1443 1444 1445
{
	struct list_head* list;

	list_for_each(list, &umidi->rmidi->streams[stream].substreams) {
1446
		struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list);
L
Linus Torvalds 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
		if (substream->number == number)
			return substream;
	}
	return NULL;
}

/*
 * This list specifies names for ports that do not fit into the standard
 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
 * such as internal control or synthesizer ports.
 */
1458
static struct port_info {
1459
	u32 id;
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
	short int port;
	short int voices;
	const char *name;
	unsigned int seq_flags;
} snd_usbmidi_port_info[] = {
#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
	{ .id = USB_ID(vendor, product), \
	  .port = num, .voices = voices_, \
	  .name = name_, .seq_flags = flags }
#define EXTERNAL_PORT(vendor, product, num, name) \
	PORT_INFO(vendor, product, num, name, 0, \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
		  SNDRV_SEQ_PORT_TYPE_HARDWARE | \
		  SNDRV_SEQ_PORT_TYPE_PORT)
#define CONTROL_PORT(vendor, product, num, name) \
	PORT_INFO(vendor, product, num, name, 0, \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
		  SNDRV_SEQ_PORT_TYPE_HARDWARE)
#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
	PORT_INFO(vendor, product, num, name, voices, \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
		  SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
		  SNDRV_SEQ_PORT_TYPE_HARDWARE | \
		  SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
	PORT_INFO(vendor, product, num, name, voices, \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
		  SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
		  SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
		  SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
		  SNDRV_SEQ_PORT_TYPE_HARDWARE | \
		  SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
L
Linus Torvalds 已提交
1497
	/* Roland UA-100 */
1498
	CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
L
Linus Torvalds 已提交
1499
	/* Roland SC-8850 */
1500 1501 1502 1503 1504 1505
	SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
	SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
	SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
	SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
	EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
	EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
L
Linus Torvalds 已提交
1506
	/* Roland U-8 */
1507 1508
	EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
	CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
L
Linus Torvalds 已提交
1509
	/* Roland SC-8820 */
1510 1511 1512
	SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
	SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
	EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
L
Linus Torvalds 已提交
1513
	/* Roland SK-500 */
1514 1515 1516
	SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
	SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
	EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
L
Linus Torvalds 已提交
1517
	/* Roland SC-D70 */
1518 1519 1520
	SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
	SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
	EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
L
Linus Torvalds 已提交
1521
	/* Edirol UM-880 */
1522
	CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
L
Linus Torvalds 已提交
1523
	/* Edirol SD-90 */
1524 1525 1526 1527
	ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
	ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
	EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
	EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
L
Linus Torvalds 已提交
1528
	/* Edirol UM-550 */
1529
	CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
L
Linus Torvalds 已提交
1530
	/* Edirol SD-20 */
1531 1532 1533
	ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
	ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
	EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
L
Linus Torvalds 已提交
1534
	/* Edirol SD-80 */
1535 1536 1537 1538
	ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
	ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
	EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
	EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
L
Linus Torvalds 已提交
1539
	/* Edirol UA-700 */
1540 1541
	EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
	CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
L
Linus Torvalds 已提交
1542
	/* Roland VariOS */
1543 1544 1545
	EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
	EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
	EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
L
Linus Torvalds 已提交
1546
	/* Edirol PCR */
1547 1548 1549
	EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
	EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
	EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
L
Linus Torvalds 已提交
1550
	/* BOSS GS-10 */
1551 1552
	EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
	CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
L
Linus Torvalds 已提交
1553
	/* Edirol UA-1000 */
1554 1555
	EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
	CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
L
Linus Torvalds 已提交
1556
	/* Edirol UR-80 */
1557 1558 1559
	EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
	EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
	EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
L
Linus Torvalds 已提交
1560
	/* Edirol PCR-A */
1561 1562 1563
	EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
	EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
	EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1564
	/* Edirol UM-3EX */
1565
	CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
L
Linus Torvalds 已提交
1566
	/* M-Audio MidiSport 8x8 */
1567 1568
	CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
	CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
L
Linus Torvalds 已提交
1569
	/* MOTU Fastlane */
1570 1571
	EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
	EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
L
Linus Torvalds 已提交
1572
	/* Emagic Unitor8/AMT8/MT4 */
1573 1574 1575
	EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
	EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
	EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1576 1577 1578 1579 1580
	/* Akai MPD16 */
	CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
	PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
		SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
		SNDRV_SEQ_PORT_TYPE_HARDWARE),
1581 1582 1583 1584 1585 1586
	/* Access Music Virus TI */
	EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
	PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
		SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
		SNDRV_SEQ_PORT_TYPE_HARDWARE |
		SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
L
Linus Torvalds 已提交
1587 1588
};

1589 1590 1591 1592 1593
static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1594
		if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
		    snd_usbmidi_port_info[i].port == number)
			return &snd_usbmidi_port_info[i];
	}
	return NULL;
}

static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
				      struct snd_seq_port_info *seq_port_info)
{
	struct snd_usb_midi *umidi = rmidi->private_data;
	struct port_info *port_info;

	/* TODO: read port flags from descriptors */
	port_info = find_port_info(umidi, number);
	if (port_info) {
		seq_port_info->type = port_info->seq_flags;
		seq_port_info->midi_voices = port_info->voices;
	}
}

1615
static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi,
L
Linus Torvalds 已提交
1616
				       int stream, int number,
1617
				       struct snd_rawmidi_substream ** rsubstream)
L
Linus Torvalds 已提交
1618
{
1619
	struct port_info *port_info;
L
Linus Torvalds 已提交
1620 1621
	const char *name_format;

1622
	struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
L
Linus Torvalds 已提交
1623 1624 1625 1626 1627 1628
	if (!substream) {
		snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
		return;
	}

	/* TODO: read port name from jack descriptor */
1629 1630
	port_info = find_port_info(umidi, number);
	name_format = port_info ? port_info->name : "%s MIDI %d";
L
Linus Torvalds 已提交
1631
	snprintf(substream->name, sizeof(substream->name),
1632
		 name_format, umidi->card->shortname, number + 1);
L
Linus Torvalds 已提交
1633 1634 1635 1636 1637 1638 1639

	*rsubstream = substream;
}

/*
 * Creates the endpoints and their ports.
 */
1640 1641
static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi,
					struct snd_usb_midi_endpoint_info* endpoints)
L
Linus Torvalds 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
{
	int i, j, err;
	int out_ports = 0, in_ports = 0;

	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
		if (endpoints[i].out_cables) {
			err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i],
							      &umidi->endpoints[i]);
			if (err < 0)
				return err;
		}
		if (endpoints[i].in_cables) {
			err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i],
							     &umidi->endpoints[i]);
			if (err < 0)
				return err;
		}

		for (j = 0; j < 0x10; ++j) {
			if (endpoints[i].out_cables & (1 << j)) {
				snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports,
							   &umidi->endpoints[i].out->ports[j].substream);
				++out_ports;
			}
			if (endpoints[i].in_cables & (1 << j)) {
				snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
							   &umidi->endpoints[i].in->ports[j].substream);
				++in_ports;
			}
		}
	}
	snd_printdd(KERN_INFO "created %d output and %d input ports\n",
		    out_ports, in_ports);
	return 0;
}

/*
 * Returns MIDIStreaming device capabilities.
 */
1681 1682
static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,
			   	   struct snd_usb_midi_endpoint_info* endpoints)
L
Linus Torvalds 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
{
	struct usb_interface* intf;
	struct usb_host_interface *hostif;
	struct usb_interface_descriptor* intfd;
	struct usb_ms_header_descriptor* ms_header;
	struct usb_host_endpoint *hostep;
	struct usb_endpoint_descriptor* ep;
	struct usb_ms_endpoint_descriptor* ms_ep;
	int i, epidx;

	intf = umidi->iface;
	if (!intf)
		return -ENXIO;
	hostif = &intf->altsetting[0];
	intfd = get_iface_desc(hostif);
	ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
	if (hostif->extralen >= 7 &&
	    ms_header->bLength >= 7 &&
	    ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1702
	    ms_header->bDescriptorSubtype == UAC_HEADER)
L
Linus Torvalds 已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711
		snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
			    ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
	else
		snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");

	epidx = 0;
	for (i = 0; i < intfd->bNumEndpoints; ++i) {
		hostep = &hostif->endpoint[i];
		ep = get_ep_desc(hostep);
1712
		if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
L
Linus Torvalds 已提交
1713 1714 1715 1716 1717
			continue;
		ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
		if (hostep->extralen < 4 ||
		    ms_ep->bLength < 4 ||
		    ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1718
		    ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
L
Linus Torvalds 已提交
1719
			continue;
1720
		if (usb_endpoint_dir_out(ep)) {
L
Linus Torvalds 已提交
1721 1722 1723 1724 1725 1726
			if (endpoints[epidx].out_ep) {
				if (++epidx >= MIDI_MAX_ENDPOINTS) {
					snd_printk(KERN_WARNING "too many endpoints\n");
					break;
				}
			}
1727 1728
			endpoints[epidx].out_ep = usb_endpoint_num(ep);
			if (usb_endpoint_xfer_int(ep))
L
Linus Torvalds 已提交
1729
				endpoints[epidx].out_interval = ep->bInterval;
1730
			else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1731 1732 1733 1734 1735 1736
				/*
				 * Low speed bulk transfers don't exist, so
				 * force interrupt transfers for devices like
				 * ESI MIDI Mate that try to use them anyway.
				 */
				endpoints[epidx].out_interval = 1;
L
Linus Torvalds 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
			endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
			snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
				    ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
		} else {
			if (endpoints[epidx].in_ep) {
				if (++epidx >= MIDI_MAX_ENDPOINTS) {
					snd_printk(KERN_WARNING "too many endpoints\n");
					break;
				}
			}
1747 1748
			endpoints[epidx].in_ep = usb_endpoint_num(ep);
			if (usb_endpoint_xfer_int(ep))
L
Linus Torvalds 已提交
1749
				endpoints[epidx].in_interval = ep->bInterval;
1750
			else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1751
				endpoints[epidx].in_interval = 1;
L
Linus Torvalds 已提交
1752 1753 1754 1755 1756 1757 1758 1759
			endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
			snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
				    ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
		}
	}
	return 0;
}

1760 1761 1762 1763 1764
static int roland_load_info(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_info *info)
{
	static const char *const names[] = { "High Load", "Light Load" };

1765
	return snd_ctl_enum_info(info, 1, 2, names);
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
}

static int roland_load_get(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *value)
{
	value->value.enumerated.item[0] = kcontrol->private_value;
	return 0;
}

static int roland_load_put(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *value)
{
	struct snd_usb_midi* umidi = kcontrol->private_data;
	int changed;

	if (value->value.enumerated.item[0] > 1)
		return -EINVAL;
	mutex_lock(&umidi->mutex);
	changed = value->value.enumerated.item[0] != kcontrol->private_value;
	if (changed)
		kcontrol->private_value = value->value.enumerated.item[0];
	mutex_unlock(&umidi->mutex);
	return changed;
}

static struct snd_kcontrol_new roland_load_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "MIDI Input Mode",
	.info = roland_load_info,
	.get = roland_load_get,
	.put = roland_load_put,
	.private_value = 1,
};

L
Linus Torvalds 已提交
1800 1801 1802 1803
/*
 * On Roland devices, use the second alternate setting to be able to use
 * the interrupt input endpoint.
 */
1804
static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi)
L
Linus Torvalds 已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
{
	struct usb_interface* intf;
	struct usb_host_interface *hostif;
	struct usb_interface_descriptor* intfd;

	intf = umidi->iface;
	if (!intf || intf->num_altsetting != 2)
		return;

	hostif = &intf->altsetting[1];
	intfd = get_iface_desc(hostif);
	if (intfd->bNumEndpoints != 2 ||
	    (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
	    (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
		return;

	snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
		    intfd->bAlternateSetting);
1823
	usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
L
Linus Torvalds 已提交
1824
			  intfd->bAlternateSetting);
1825 1826

	umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
1827
	if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
1828
		umidi->roland_load_ctl = NULL;
L
Linus Torvalds 已提交
1829 1830 1831 1832 1833
}

/*
 * Try to find any usable endpoints in the interface.
 */
1834 1835
static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
					struct snd_usb_midi_endpoint_info* endpoint,
L
Linus Torvalds 已提交
1836 1837 1838 1839 1840 1841 1842 1843
					int max_endpoints)
{
	struct usb_interface* intf;
	struct usb_host_interface *hostif;
	struct usb_interface_descriptor* intfd;
	struct usb_endpoint_descriptor* epd;
	int i, out_eps = 0, in_eps = 0;

1844
	if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
L
Linus Torvalds 已提交
1845 1846
		snd_usbmidi_switch_roland_altsetting(umidi);

1847
	if (endpoint[0].out_ep || endpoint[0].in_ep)
D
Daniel Mack 已提交
1848
		return 0;
1849

L
Linus Torvalds 已提交
1850 1851 1852 1853 1854 1855 1856 1857
	intf = umidi->iface;
	if (!intf || intf->num_altsetting < 1)
		return -ENOENT;
	hostif = intf->cur_altsetting;
	intfd = get_iface_desc(hostif);

	for (i = 0; i < intfd->bNumEndpoints; ++i) {
		epd = get_endpoint(hostif, i);
1858 1859
		if (!usb_endpoint_xfer_bulk(epd) &&
		    !usb_endpoint_xfer_int(epd))
L
Linus Torvalds 已提交
1860 1861
			continue;
		if (out_eps < max_endpoints &&
1862 1863 1864
		    usb_endpoint_dir_out(epd)) {
			endpoint[out_eps].out_ep = usb_endpoint_num(epd);
			if (usb_endpoint_xfer_int(epd))
L
Linus Torvalds 已提交
1865 1866 1867 1868
				endpoint[out_eps].out_interval = epd->bInterval;
			++out_eps;
		}
		if (in_eps < max_endpoints &&
1869 1870 1871
		    usb_endpoint_dir_in(epd)) {
			endpoint[in_eps].in_ep = usb_endpoint_num(epd);
			if (usb_endpoint_xfer_int(epd))
L
Linus Torvalds 已提交
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
				endpoint[in_eps].in_interval = epd->bInterval;
			++in_eps;
		}
	}
	return (out_eps || in_eps) ? 0 : -ENOENT;
}

/*
 * Detects the endpoints for one-port-per-endpoint protocols.
 */
1882 1883
static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
						 struct snd_usb_midi_endpoint_info* endpoints)
L
Linus Torvalds 已提交
1884 1885
{
	int err, i;
D
Daniel Mack 已提交
1886

L
Linus Torvalds 已提交
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
	err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
		if (endpoints[i].out_ep)
			endpoints[i].out_cables = 0x0001;
		if (endpoints[i].in_ep)
			endpoints[i].in_cables = 0x0001;
	}
	return err;
}

/*
 * Detects the endpoints and ports of Yamaha devices.
 */
1900 1901
static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi,
				     struct snd_usb_midi_endpoint_info* endpoint)
L
Linus Torvalds 已提交
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
{
	struct usb_interface* intf;
	struct usb_host_interface *hostif;
	struct usb_interface_descriptor* intfd;
	uint8_t* cs_desc;

	intf = umidi->iface;
	if (!intf)
		return -ENOENT;
	hostif = intf->altsetting;
	intfd = get_iface_desc(hostif);
	if (intfd->bNumEndpoints < 1)
		return -ENOENT;

	/*
	 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
	 * necessarily with any useful contents.  So simply count 'em.
	 */
	for (cs_desc = hostif->extra;
	     cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
	     cs_desc += cs_desc[0]) {
1923
		if (cs_desc[1] == USB_DT_CS_INTERFACE) {
1924
			if (cs_desc[2] == UAC_MIDI_IN_JACK)
L
Linus Torvalds 已提交
1925
				endpoint->in_cables = (endpoint->in_cables << 1) | 1;
1926
			else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
L
Linus Torvalds 已提交
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
				endpoint->out_cables = (endpoint->out_cables << 1) | 1;
		}
	}
	if (!endpoint->in_cables && !endpoint->out_cables)
		return -ENOENT;

	return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
}

/*
 * Creates the endpoints and their ports for Midiman devices.
 */
1939 1940
static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi,
						struct snd_usb_midi_endpoint_info* endpoint)
L
Linus Torvalds 已提交
1941
{
1942
	struct snd_usb_midi_endpoint_info ep_info;
L
Linus Torvalds 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
	struct usb_interface* intf;
	struct usb_host_interface *hostif;
	struct usb_interface_descriptor* intfd;
	struct usb_endpoint_descriptor* epd;
	int cable, err;

	intf = umidi->iface;
	if (!intf)
		return -ENOENT;
	hostif = intf->altsetting;
	intfd = get_iface_desc(hostif);
	/*
	 * The various MidiSport devices have more or less random endpoint
	 * numbers, so we have to identify the endpoints by their index in
	 * the descriptor array, like the driver for that other OS does.
	 *
	 * There is one interrupt input endpoint for all input ports, one
	 * bulk output endpoint for even-numbered ports, and one for odd-
	 * numbered ports.  Both bulk output endpoints have corresponding
	 * input bulk endpoints (at indices 1 and 3) which aren't used.
	 */
	if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
		snd_printdd(KERN_ERR "not enough endpoints\n");
		return -ENOENT;
	}

	epd = get_endpoint(hostif, 0);
1970
	if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
L
Linus Torvalds 已提交
1971 1972 1973 1974
		snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
		return -ENXIO;
	}
	epd = get_endpoint(hostif, 2);
1975
	if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
L
Linus Torvalds 已提交
1976 1977 1978 1979 1980
		snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
		return -ENXIO;
	}
	if (endpoint->out_cables > 0x0001) {
		epd = get_endpoint(hostif, 4);
1981 1982
		if (!usb_endpoint_dir_out(epd) ||
		    !usb_endpoint_xfer_bulk(epd)) {
L
Linus Torvalds 已提交
1983 1984 1985 1986 1987 1988
			snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
			return -ENXIO;
		}
	}

	ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1989
	ep_info.out_interval = 0;
L
Linus Torvalds 已提交
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
	ep_info.out_cables = endpoint->out_cables & 0x5555;
	err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
	if (err < 0)
		return err;

	ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
	ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
	ep_info.in_cables = endpoint->in_cables;
	err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);
	if (err < 0)
		return err;

	if (endpoint->out_cables > 0x0001) {
		ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
		ep_info.out_cables = endpoint->out_cables & 0xaaaa;
		err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]);
		if (err < 0)
			return err;
	}

	for (cable = 0; cable < 0x10; ++cable) {
		if (endpoint->out_cables & (1 << cable))
			snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable,
						   &umidi->endpoints[cable & 1].out->ports[cable].substream);
		if (endpoint->in_cables & (1 << cable))
			snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable,
						   &umidi->endpoints[0].in->ports[cable].substream);
	}
	return 0;
}

2021 2022 2023 2024
static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
	.get_port_info = snd_usbmidi_get_port_info,
};

2025
static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi,
L
Linus Torvalds 已提交
2026 2027
				      int out_ports, int in_ports)
{
2028
	struct snd_rawmidi *rmidi;
L
Linus Torvalds 已提交
2029 2030
	int err;

2031 2032
	err = snd_rawmidi_new(umidi->card, "USB MIDI",
			      umidi->next_midi_device++,
L
Linus Torvalds 已提交
2033 2034 2035
			      out_ports, in_ports, &rmidi);
	if (err < 0)
		return err;
2036
	strcpy(rmidi->name, umidi->card->shortname);
L
Linus Torvalds 已提交
2037 2038 2039
	rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
			    SNDRV_RAWMIDI_INFO_INPUT |
			    SNDRV_RAWMIDI_INFO_DUPLEX;
2040
	rmidi->ops = &snd_usbmidi_ops;
L
Linus Torvalds 已提交
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
	rmidi->private_data = umidi;
	rmidi->private_free = snd_usbmidi_rawmidi_free;
	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops);
	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops);

	umidi->rmidi = rmidi;
	return 0;
}

/*
 * Temporarily stop input.
 */
void snd_usbmidi_input_stop(struct list_head* p)
{
2055
	struct snd_usb_midi* umidi;
2056
	unsigned int i, j;
L
Linus Torvalds 已提交
2057

2058
	umidi = list_entry(p, struct snd_usb_midi, list);
L
Linus Torvalds 已提交
2059
	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2060
		struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
L
Linus Torvalds 已提交
2061
		if (ep->in)
2062 2063
			for (j = 0; j < INPUT_URBS; ++j)
				usb_kill_urb(ep->in->urbs[j]);
L
Linus Torvalds 已提交
2064 2065 2066
	}
}

2067
static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep)
L
Linus Torvalds 已提交
2068
{
2069 2070 2071 2072 2073 2074
	unsigned int i;

	if (!ep)
		return;
	for (i = 0; i < INPUT_URBS; ++i) {
		struct urb* urb = ep->urbs[i];
2075
		urb->dev = ep->umidi->dev;
L
Linus Torvalds 已提交
2076 2077 2078 2079 2080 2081 2082 2083 2084
		snd_usbmidi_submit_urb(urb, GFP_KERNEL);
	}
}

/*
 * Resume input after a call to snd_usbmidi_input_stop().
 */
void snd_usbmidi_input_start(struct list_head* p)
{
2085
	struct snd_usb_midi* umidi;
L
Linus Torvalds 已提交
2086 2087
	int i;

2088
	umidi = list_entry(p, struct snd_usb_midi, list);
L
Linus Torvalds 已提交
2089 2090 2091 2092 2093 2094 2095
	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
		snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
}

/*
 * Creates and registers everything needed for a MIDI streaming interface.
 */
2096 2097 2098 2099
int snd_usbmidi_create(struct snd_card *card,
		       struct usb_interface* iface,
		       struct list_head *midi_list,
		       const struct snd_usb_audio_quirk* quirk)
L
Linus Torvalds 已提交
2100
{
2101 2102
	struct snd_usb_midi* umidi;
	struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
L
Linus Torvalds 已提交
2103 2104 2105
	int out_ports, in_ports;
	int i, err;

2106
	umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
L
Linus Torvalds 已提交
2107 2108
	if (!umidi)
		return -ENOMEM;
2109 2110
	umidi->dev = interface_to_usbdev(iface);
	umidi->card = card;
L
Linus Torvalds 已提交
2111 2112 2113
	umidi->iface = iface;
	umidi->quirk = quirk;
	umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
2114
	init_timer(&umidi->error_timer);
2115
	spin_lock_init(&umidi->disc_lock);
2116
	mutex_init(&umidi->mutex);
2117 2118
	umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
			       le16_to_cpu(umidi->dev->descriptor.idProduct));
2119 2120
	umidi->error_timer.function = snd_usbmidi_error_timer;
	umidi->error_timer.data = (unsigned long)umidi;
L
Linus Torvalds 已提交
2121 2122 2123

	/* detect the endpoint(s) to use */
	memset(endpoints, 0, sizeof(endpoints));
2124 2125
	switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
	case QUIRK_MIDI_STANDARD_INTERFACE:
L
Linus Torvalds 已提交
2126
		err = snd_usbmidi_get_ms_info(umidi, endpoints);
2127
		if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
2128 2129
			umidi->usb_protocol_ops =
				&snd_usbmidi_maudio_broken_running_status_ops;
2130
		break;
K
Karsten Wiese 已提交
2131 2132 2133
	case QUIRK_MIDI_US122L:
		umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
		/* fall through */
2134 2135
	case QUIRK_MIDI_FIXED_ENDPOINT:
		memcpy(&endpoints[0], quirk->data,
2136
		       sizeof(struct snd_usb_midi_endpoint_info));
2137 2138 2139 2140 2141 2142 2143 2144
		err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
		break;
	case QUIRK_MIDI_YAMAHA:
		err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
		break;
	case QUIRK_MIDI_MIDIMAN:
		umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
		memcpy(&endpoints[0], quirk->data,
2145
		       sizeof(struct snd_usb_midi_endpoint_info));
2146 2147 2148 2149 2150 2151
		err = 0;
		break;
	case QUIRK_MIDI_NOVATION:
		umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
		err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
		break;
2152
	case QUIRK_MIDI_RAW_BYTES:
2153
		umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
2154 2155 2156 2157 2158 2159 2160 2161 2162
		/*
		 * Interface 1 contains isochronous endpoints, but with the same
		 * numbers as in interface 0.  Since it is interface 1 that the
		 * USB core has most recently seen, these descriptors are now
		 * associated with the endpoint numbers.  This will foul up our
		 * attempts to submit bulk/interrupt URBs to the endpoints in
		 * interface 0, so we have to make sure that the USB core looks
		 * again at interface 0 by calling usb_set_interface() on it.
		 */
2163 2164
		if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
			usb_set_interface(umidi->dev, 0, 0);
2165 2166 2167 2168 2169
		err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
		break;
	case QUIRK_MIDI_EMAGIC:
		umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
		memcpy(&endpoints[0], quirk->data,
2170
		       sizeof(struct snd_usb_midi_endpoint_info));
2171 2172
		err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
		break;
2173
	case QUIRK_MIDI_CME:
2174
		umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
2175 2176
		err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
		break;
2177 2178 2179 2180 2181 2182
	case QUIRK_MIDI_AKAI:
		umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
		err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
		/* endpoint 1 is input-only */
		endpoints[1].out_cables = 0;
		break;
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
	case QUIRK_MIDI_FTDI:
		umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;

		/* set baud rate to 31250 (48 MHz / 16 / 96) */
		err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
				      3, 0x40, 0x60, 0, NULL, 0, 1000);
		if (err < 0)
			break;

		err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
		break;
2194 2195 2196 2197
	default:
		snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
		err = -ENXIO;
		break;
L
Linus Torvalds 已提交
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
	}
	if (err < 0) {
		kfree(umidi);
		return err;
	}

	/* create rawmidi device */
	out_ports = 0;
	in_ports = 0;
	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
A
Akinobu Mita 已提交
2208 2209
		out_ports += hweight16(endpoints[i].out_cables);
		in_ports += hweight16(endpoints[i].in_cables);
L
Linus Torvalds 已提交
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
	}
	err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
	if (err < 0) {
		kfree(umidi);
		return err;
	}

	/* create endpoint/port structures */
	if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
		err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
	else
		err = snd_usbmidi_create_endpoints(umidi, endpoints);
	if (err < 0) {
		snd_usbmidi_free(umidi);
		return err;
	}

2227
	list_add_tail(&umidi->list, midi_list);
L
Linus Torvalds 已提交
2228 2229 2230 2231 2232 2233

	for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
		snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
	return 0;
}

2234
EXPORT_SYMBOL(snd_usbmidi_create);
L
Linus Torvalds 已提交
2235 2236 2237
EXPORT_SYMBOL(snd_usbmidi_input_stop);
EXPORT_SYMBOL(snd_usbmidi_input_start);
EXPORT_SYMBOL(snd_usbmidi_disconnect);