midi.c 63.2 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
/*
 * usbmidi.c - ALSA USB MIDI driver
 *
4
 * Copyright (c) 2002-2009 Clemens Ladisch
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
47
#include <linux/usb.h>
48
#include <linux/wait.h>
49
#include <linux/usb/audio.h>
50
#include <linux/module.h>
51

Linus Torvalds's avatar
Linus Torvalds committed
52
#include <sound/core.h>
53
#include <sound/control.h>
Linus Torvalds's avatar
Linus Torvalds committed
54
#include <sound/rawmidi.h>
55
#include <sound/asequencer.h>
Linus Torvalds's avatar
Linus Torvalds committed
56
#include "usbaudio.h"
Daniel Mack's avatar
Daniel Mack committed
57
#include "midi.h"
58
#include "power.h"
Daniel Mack's avatar
Daniel Mack committed
59
#include "helper.h"
Linus Torvalds's avatar
Linus Torvalds committed
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

Linus Torvalds's avatar
Linus Torvalds committed
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;
Linus Torvalds's avatar
Linus Torvalds committed
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);
Linus Torvalds's avatar
Linus Torvalds committed
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*);
Linus Torvalds's avatar
Linus Torvalds committed
107
108
109
};

struct snd_usb_midi {
110
111
	struct usb_device *dev;
	struct snd_card *card;
Linus Torvalds's avatar
Linus Torvalds committed
112
	struct usb_interface *iface;
113
114
	const struct snd_usb_audio_quirk *quirk;
	struct snd_rawmidi *rmidi;
Linus Torvalds's avatar
Linus Torvalds committed
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 rw_semaphore disc_rwsem;
120
	struct mutex mutex;
121
122
	u32 usb_id;
	int next_midi_device;
Linus Torvalds's avatar
Linus Torvalds committed
123
124

	struct snd_usb_midi_endpoint {
125
126
		struct snd_usb_midi_out_endpoint *out;
		struct snd_usb_midi_in_endpoint *in;
Linus Torvalds's avatar
Linus Torvalds committed
127
128
	} endpoints[MIDI_MAX_ENDPOINTS];
	unsigned long input_triggered;
129
130
	bool autopm_reference;
	unsigned int opened[2];
131
	unsigned char disconnected;
132
	unsigned char input_running;
133
134

	struct snd_kcontrol *roland_load_ctl;
Linus Torvalds's avatar
Linus Torvalds committed
135
136
137
};

struct snd_usb_midi_out_endpoint {
138
	struct snd_usb_midi* umidi;
139
140
141
142
143
	struct out_urb_context {
		struct urb *urb;
		struct snd_usb_midi_out_endpoint *ep;
	} urbs[OUTPUT_URBS];
	unsigned int active_urbs;
144
	unsigned int drain_urbs;
Linus Torvalds's avatar
Linus Torvalds committed
145
146
	int max_transfer;		/* size of urb buffer */
	struct tasklet_struct tasklet;
147
	unsigned int next_urb;
Linus Torvalds's avatar
Linus Torvalds committed
148
149
150
	spinlock_t buffer_lock;

	struct usbmidi_out_port {
151
152
		struct snd_usb_midi_out_endpoint* ep;
		struct snd_rawmidi_substream *substream;
Linus Torvalds's avatar
Linus Torvalds committed
153
154
155
156
157
158
159
160
161
162
163
164
165
		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;
166
167

	wait_queue_head_t drain_wait;
Linus Torvalds's avatar
Linus Torvalds committed
168
169
170
};

struct snd_usb_midi_in_endpoint {
171
	struct snd_usb_midi* umidi;
172
	struct urb* urbs[INPUT_URBS];
Linus Torvalds's avatar
Linus Torvalds committed
173
	struct usbmidi_in_port {
174
		struct snd_rawmidi_substream *substream;
175
		u8 running_status_length;
Linus Torvalds's avatar
Linus Torvalds committed
176
	} ports[0x10];
177
178
	u8 seen_f5;
	u8 error_resubmit;
Linus Torvalds's avatar
Linus Torvalds committed
179
180
181
	int current_port;
};

182
static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep);
Linus Torvalds's avatar
Linus Torvalds committed
183
184
185
186
187
188
189
190

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.
 */
Al Viro's avatar
Al Viro committed
191
static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
Linus Torvalds's avatar
Linus Torvalds committed
192
193
194
195
196
197
198
199
200
201
202
203
{
	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)
{
204
205
206
207
208
209
210
211
	switch (status) {
	/* manually unlinked, or device gone */
	case -ENOENT:
	case -ECONNRESET:
	case -ESHUTDOWN:
	case -ENODEV:
		return -ENODEV;
	/* errors that might occur during unplugging */
212
213
214
	case -EPROTO:
	case -ETIME:
	case -EILSEQ:
215
216
217
218
219
		return -EIO;
	default:
		snd_printk(KERN_ERR "urb status %d\n", status);
		return 0; /* continue */
	}
Linus Torvalds's avatar
Linus Torvalds committed
220
221
222
223
224
}

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

	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.
 */
254
static void snd_usbmidi_in_urb_complete(struct urb* urb)
Linus Torvalds's avatar
Linus Torvalds committed
255
{
256
	struct snd_usb_midi_in_endpoint* ep = urb->context;
Linus Torvalds's avatar
Linus Torvalds committed
257
258
259
260
261
262

	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 {
263
264
265
266
267
268
269
		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);
			}
Linus Torvalds's avatar
Linus Torvalds committed
270
			return;
271
		}
Linus Torvalds's avatar
Linus Torvalds committed
272
273
	}

274
	urb->dev = ep->umidi->dev;
275
	snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
276
277
}

278
static void snd_usbmidi_out_urb_complete(struct urb* urb)
Linus Torvalds's avatar
Linus Torvalds committed
279
{
280
281
	struct out_urb_context *context = urb->context;
	struct snd_usb_midi_out_endpoint* ep = context->ep;
282
	unsigned int urb_index;
Linus Torvalds's avatar
Linus Torvalds committed
283
284

	spin_lock(&ep->buffer_lock);
285
286
287
288
289
290
	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);
	}
Linus Torvalds's avatar
Linus Torvalds committed
291
292
	spin_unlock(&ep->buffer_lock);
	if (urb->status < 0) {
293
294
295
296
297
		int err = snd_usbmidi_urb_error(urb->status);
		if (err < 0) {
			if (err != -ENODEV)
				mod_timer(&ep->umidi->error_timer,
					  jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds's avatar
Linus Torvalds committed
298
			return;
299
		}
Linus Torvalds's avatar
Linus Torvalds committed
300
301
302
303
304
305
306
307
	}
	snd_usbmidi_do_output(ep);
}

/*
 * This is called when some data should be transferred to the device
 * (from one or more substreams).
 */
308
static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds's avatar
Linus Torvalds committed
309
{
310
311
	unsigned int urb_index;
	struct urb* urb;
Linus Torvalds's avatar
Linus Torvalds committed
312
313
314
	unsigned long flags;

	spin_lock_irqsave(&ep->buffer_lock, flags);
315
	if (ep->umidi->disconnected) {
Linus Torvalds's avatar
Linus Torvalds committed
316
317
318
319
		spin_unlock_irqrestore(&ep->buffer_lock, flags);
		return;
	}

320
	urb_index = ep->next_urb;
321
	for (;;) {
322
323
324
325
326
		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)
327
				break;
Linus Torvalds's avatar
Linus Torvalds committed
328

329
330
			dump_urb("sending", urb->transfer_buffer,
				 urb->transfer_buffer_length);
331
			urb->dev = ep->umidi->dev;
332
333
334
335
336
337
338
			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)
339
			break;
Linus Torvalds's avatar
Linus Torvalds committed
340
	}
341
	ep->next_urb = urb_index;
Linus Torvalds's avatar
Linus Torvalds committed
342
343
344
345
346
	spin_unlock_irqrestore(&ep->buffer_lock, flags);
}

static void snd_usbmidi_out_tasklet(unsigned long data)
{
347
	struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds's avatar
Linus Torvalds committed
348
349
350
351

	snd_usbmidi_do_output(ep);
}

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

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

Linus Torvalds's avatar
Linus Torvalds committed
378
/* helper function to send static data that may not DMA-able */
379
static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep,
Linus Torvalds's avatar
Linus Torvalds committed
380
381
				 const void *data, int len)
{
382
	int err = 0;
Alexey Dobriyan's avatar
Alexey Dobriyan committed
383
	void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
384
385
386
	if (!buf)
		return -ENOMEM;
	dump_urb("sending", buf, len);
387
	if (ep->urbs[0].urb)
388
		err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
389
				   buf, len, NULL, 250);
Linus Torvalds's avatar
Linus Torvalds committed
390
391
392
393
394
395
396
397
398
399
	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.
 */

400
static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds's avatar
Linus Torvalds committed
401
402
403
404
405
406
407
408
409
410
411
412
				       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);
		}
}

413
static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds's avatar
Linus Torvalds committed
414
415
416
417
418
419
420
421
422
423
424
425
				      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);
		}
}

426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * 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;
Daniel Mack's avatar
Daniel Mack committed
442

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
			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);
		}
}

466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 * 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);
}

Linus Torvalds's avatar
Linus Torvalds committed
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
510
511
512
/*
 * 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.
 */
513
static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port,
Linus Torvalds's avatar
Linus Torvalds committed
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
604
605
606
				      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;
		}
	}
}

607
608
static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep,
					struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
609
610
611
612
613
{
	int p;

	/* FIXME: lower-numbered ports can starve higher-numbered ports */
	for (p = 0; p < 0x10; ++p) {
614
		struct usbmidi_out_port* port = &ep->ports[p];
Linus Torvalds's avatar
Linus Torvalds committed
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
		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,
Daniel Mack's avatar
Daniel Mack committed
636
	.output = snd_usbmidi_standard_output,
Linus Torvalds's avatar
Linus Torvalds committed
637
638
639
	.output_packet = snd_usbmidi_output_midiman_packet,
};

640
641
static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
	.input = snd_usbmidi_maudio_broken_running_status_input,
Daniel Mack's avatar
Daniel Mack committed
642
	.output = snd_usbmidi_standard_output,
643
644
645
	.output_packet = snd_usbmidi_output_standard_packet,
};

646
647
648
649
650
651
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,
};

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
748
749
750
/*
 * 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,
};

Linus Torvalds's avatar
Linus Torvalds committed
751
752
753
754
755
756
/*
 * 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.
 */

757
static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds's avatar
Linus Torvalds committed
758
759
760
761
762
763
764
				       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);
}

765
766
static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep,
					struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
767
768
769
770
771
772
{
	uint8_t* transfer_buffer;
	int count;

	if (!ep->ports[0].active)
		return;
773
	transfer_buffer = urb->transfer_buffer;
Linus Torvalds's avatar
Linus Torvalds committed
774
775
776
777
778
779
780
781
782
	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;
783
	urb->transfer_buffer_length = 2 + count;
Linus Torvalds's avatar
Linus Torvalds committed
784
785
786
787
788
789
790
791
}

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

/*
792
 * "raw" protocol: just move raw MIDI bytes from/to the endpoint
Linus Torvalds's avatar
Linus Torvalds committed
793
794
 */

795
static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
796
				  uint8_t* buffer, int buffer_length)
Linus Torvalds's avatar
Linus Torvalds committed
797
798
799
800
{
	snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
}

801
802
static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep,
				   struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
803
804
805
806
807
808
{
	int count;

	if (!ep->ports[0].active)
		return;
	count = snd_rawmidi_transmit(ep->ports[0].substream,
809
				     urb->transfer_buffer,
Linus Torvalds's avatar
Linus Torvalds committed
810
811
812
813
814
				     ep->max_transfer);
	if (count < 1) {
		ep->ports[0].active = 0;
		return;
	}
815
	urb->transfer_buffer_length = count;
Linus Torvalds's avatar
Linus Torvalds committed
816
817
}

818
819
820
static struct usb_protocol_ops snd_usbmidi_raw_ops = {
	.input = snd_usbmidi_raw_input,
	.output = snd_usbmidi_raw_output,
Linus Torvalds's avatar
Linus Torvalds committed
821
822
};

823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/*
 * 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,
};

Karsten Wiese's avatar
Karsten Wiese committed
839
840
841
842
843
844
845
846
847
848
849
850
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);
}

851
852
static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
				      struct urb *urb)
Karsten Wiese's avatar
Karsten Wiese committed
853
854
855
856
857
{
	int count;

	if (!ep->ports[0].active)
		return;
858
859
860
861
862
863
864
865
	switch (snd_usb_get_speed(ep->umidi->dev)) {
	case USB_SPEED_HIGH:
	case USB_SPEED_SUPER:
		count = 1;
		break;
	default:
		count = 2;
	}
Karsten Wiese's avatar
Karsten Wiese committed
866
	count = snd_rawmidi_transmit(ep->ports[0].substream,
867
				     urb->transfer_buffer,
Karsten Wiese's avatar
Karsten Wiese committed
868
869
870
871
872
873
				     count);
	if (count < 1) {
		ep->ports[0].active = 0;
		return;
	}

874
875
	memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
	urb->transfer_buffer_length = ep->max_transfer;
Karsten Wiese's avatar
Karsten Wiese committed
876
877
878
879
880
881
882
}

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

Linus Torvalds's avatar
Linus Torvalds committed
883
884
885
886
/*
 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
 */

887
static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds's avatar
Linus Torvalds committed
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
{
	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));
}

904
static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep)
Linus Torvalds's avatar
Linus Torvalds committed
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
{
	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));
}

920
static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep,
Linus Torvalds's avatar
Linus Torvalds committed
921
922
				     uint8_t* buffer, int buffer_length)
{
923
924
925
926
927
928
929
930
	int i;

	/* FF indicates end of valid data */
	for (i = 0; i < buffer_length; ++i)
		if (buffer[i] == 0xff) {
			buffer_length = i;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
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
961
962
963

	/* 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;
	}
}

964
965
static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep,
				      struct urb *urb)
Linus Torvalds's avatar
Linus Torvalds committed
966
967
{
	int port0 = ep->current_port;
968
	uint8_t* buf = urb->transfer_buffer;
Linus Torvalds's avatar
Linus Torvalds committed
969
970
971
972
973
974
	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;
975
		struct usbmidi_out_port* port = &ep->ports[portnum];
Linus Torvalds's avatar
Linus Torvalds committed
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)
For faster browsing, not all history is shown. View entire blame