Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/usb/midi.c
26378 views
1
/*
2
* usbmidi.c - ALSA USB MIDI driver
3
*
4
* Copyright (c) 2002-2009 Clemens Ladisch
5
* All rights reserved.
6
*
7
* Based on the OSS usb-midi driver by NAGANO Daisuke,
8
* NetBSD's umidi driver by Takuya SHIOZAKI,
9
* the "USB Device Class Definition for MIDI Devices" by Roland
10
*
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions
13
* are met:
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions, and the following disclaimer,
16
* without modification.
17
* 2. The name of the author may not be used to endorse or promote products
18
* derived from this software without specific prior written permission.
19
*
20
* Alternatively, this software may be distributed and/or modified under the
21
* terms of the GNU General Public License as published by the Free Software
22
* Foundation; either version 2 of the License, or (at your option) any later
23
* version.
24
*
25
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35
* SUCH DAMAGE.
36
*/
37
38
#include <linux/kernel.h>
39
#include <linux/types.h>
40
#include <linux/bitops.h>
41
#include <linux/interrupt.h>
42
#include <linux/spinlock.h>
43
#include <linux/string.h>
44
#include <linux/init.h>
45
#include <linux/slab.h>
46
#include <linux/timer.h>
47
#include <linux/usb.h>
48
#include <linux/wait.h>
49
#include <linux/usb/audio.h>
50
#include <linux/usb/midi.h>
51
#include <linux/module.h>
52
53
#include <sound/core.h>
54
#include <sound/control.h>
55
#include <sound/rawmidi.h>
56
#include <sound/asequencer.h>
57
#include "usbaudio.h"
58
#include "midi.h"
59
#include "power.h"
60
#include "helper.h"
61
62
/*
63
* define this to log all USB packets
64
*/
65
/* #define DUMP_PACKETS */
66
67
/*
68
* how long to wait after some USB errors, so that hub_wq can disconnect() us
69
* without too many spurious errors
70
*/
71
#define ERROR_DELAY_JIFFIES (HZ / 10)
72
73
#define OUTPUT_URBS 7
74
#define INPUT_URBS 7
75
76
77
MODULE_AUTHOR("Clemens Ladisch <[email protected]>");
78
MODULE_DESCRIPTION("USB Audio/MIDI helper module");
79
MODULE_LICENSE("Dual BSD/GPL");
80
81
struct snd_usb_midi_in_endpoint;
82
struct snd_usb_midi_out_endpoint;
83
struct snd_usb_midi_endpoint;
84
85
struct usb_protocol_ops {
86
void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
87
void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
88
void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
89
void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint *);
90
void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint *);
91
};
92
93
struct snd_usb_midi {
94
struct usb_device *dev;
95
struct snd_card *card;
96
struct usb_interface *iface;
97
const struct snd_usb_audio_quirk *quirk;
98
struct snd_rawmidi *rmidi;
99
const struct usb_protocol_ops *usb_protocol_ops;
100
struct list_head list;
101
struct timer_list error_timer;
102
spinlock_t disc_lock;
103
struct rw_semaphore disc_rwsem;
104
struct mutex mutex;
105
u32 usb_id;
106
int next_midi_device;
107
108
struct snd_usb_midi_endpoint {
109
struct snd_usb_midi_out_endpoint *out;
110
struct snd_usb_midi_in_endpoint *in;
111
} endpoints[MIDI_MAX_ENDPOINTS];
112
unsigned long input_triggered;
113
unsigned int opened[2];
114
unsigned char disconnected;
115
unsigned char input_running;
116
117
struct snd_kcontrol *roland_load_ctl;
118
};
119
120
struct snd_usb_midi_out_endpoint {
121
struct snd_usb_midi *umidi;
122
struct out_urb_context {
123
struct urb *urb;
124
struct snd_usb_midi_out_endpoint *ep;
125
} urbs[OUTPUT_URBS];
126
unsigned int active_urbs;
127
unsigned int drain_urbs;
128
int max_transfer; /* size of urb buffer */
129
struct work_struct work;
130
unsigned int next_urb;
131
spinlock_t buffer_lock;
132
133
struct usbmidi_out_port {
134
struct snd_usb_midi_out_endpoint *ep;
135
struct snd_rawmidi_substream *substream;
136
int active;
137
uint8_t cable; /* cable number << 4 */
138
uint8_t state;
139
#define STATE_UNKNOWN 0
140
#define STATE_1PARAM 1
141
#define STATE_2PARAM_1 2
142
#define STATE_2PARAM_2 3
143
#define STATE_SYSEX_0 4
144
#define STATE_SYSEX_1 5
145
#define STATE_SYSEX_2 6
146
uint8_t data[2];
147
} ports[0x10];
148
int current_port;
149
150
wait_queue_head_t drain_wait;
151
};
152
153
struct snd_usb_midi_in_endpoint {
154
struct snd_usb_midi *umidi;
155
struct urb *urbs[INPUT_URBS];
156
struct usbmidi_in_port {
157
struct snd_rawmidi_substream *substream;
158
u8 running_status_length;
159
} ports[0x10];
160
u8 seen_f5;
161
bool in_sysex;
162
u8 last_cin;
163
u8 error_resubmit;
164
int current_port;
165
};
166
167
static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep);
168
169
static const uint8_t snd_usbmidi_cin_length[] = {
170
0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
171
};
172
173
/*
174
* Submits the URB, with error handling.
175
*/
176
static int snd_usbmidi_submit_urb(struct urb *urb, gfp_t flags)
177
{
178
int err = usb_submit_urb(urb, flags);
179
if (err < 0 && err != -ENODEV)
180
dev_err(&urb->dev->dev, "usb_submit_urb: %d\n", err);
181
return err;
182
}
183
184
/*
185
* Error handling for URB completion functions.
186
*/
187
static int snd_usbmidi_urb_error(const struct urb *urb)
188
{
189
switch (urb->status) {
190
/* manually unlinked, or device gone */
191
case -ENOENT:
192
case -ECONNRESET:
193
case -ESHUTDOWN:
194
case -ENODEV:
195
return -ENODEV;
196
/* errors that might occur during unplugging */
197
case -EPROTO:
198
case -ETIME:
199
case -EILSEQ:
200
return -EIO;
201
default:
202
dev_err(&urb->dev->dev, "urb status %d\n", urb->status);
203
return 0; /* continue */
204
}
205
}
206
207
/*
208
* Receives a chunk of MIDI data.
209
*/
210
static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint *ep,
211
int portidx, uint8_t *data, int length)
212
{
213
struct usbmidi_in_port *port = &ep->ports[portidx];
214
215
if (!port->substream) {
216
dev_dbg(&ep->umidi->dev->dev, "unexpected port %d!\n", portidx);
217
return;
218
}
219
if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
220
return;
221
snd_rawmidi_receive(port->substream, data, length);
222
}
223
224
#ifdef DUMP_PACKETS
225
static void dump_urb(const char *type, const u8 *data, int length)
226
{
227
pr_debug("%s packet: [", type);
228
for (; length > 0; ++data, --length)
229
pr_cont(" %02x", *data);
230
pr_cont(" ]\n");
231
}
232
#else
233
#define dump_urb(type, data, length) /* nothing */
234
#endif
235
236
/*
237
* Processes the data read from the device.
238
*/
239
static void snd_usbmidi_in_urb_complete(struct urb *urb)
240
{
241
struct snd_usb_midi_in_endpoint *ep = urb->context;
242
243
if (urb->status == 0) {
244
dump_urb("received", urb->transfer_buffer, urb->actual_length);
245
ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
246
urb->actual_length);
247
} else {
248
int err = snd_usbmidi_urb_error(urb);
249
if (err < 0) {
250
if (err != -ENODEV) {
251
ep->error_resubmit = 1;
252
mod_timer(&ep->umidi->error_timer,
253
jiffies + ERROR_DELAY_JIFFIES);
254
}
255
return;
256
}
257
}
258
259
urb->dev = ep->umidi->dev;
260
snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
261
}
262
263
static void snd_usbmidi_out_urb_complete(struct urb *urb)
264
{
265
struct out_urb_context *context = urb->context;
266
struct snd_usb_midi_out_endpoint *ep = context->ep;
267
unsigned int urb_index;
268
unsigned long flags;
269
270
spin_lock_irqsave(&ep->buffer_lock, flags);
271
urb_index = context - ep->urbs;
272
ep->active_urbs &= ~(1 << urb_index);
273
if (unlikely(ep->drain_urbs)) {
274
ep->drain_urbs &= ~(1 << urb_index);
275
wake_up(&ep->drain_wait);
276
}
277
spin_unlock_irqrestore(&ep->buffer_lock, flags);
278
if (urb->status < 0) {
279
int err = snd_usbmidi_urb_error(urb);
280
if (err < 0) {
281
if (err != -ENODEV)
282
mod_timer(&ep->umidi->error_timer,
283
jiffies + ERROR_DELAY_JIFFIES);
284
return;
285
}
286
}
287
snd_usbmidi_do_output(ep);
288
}
289
290
/*
291
* This is called when some data should be transferred to the device
292
* (from one or more substreams).
293
*/
294
static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep)
295
{
296
unsigned int urb_index;
297
struct urb *urb;
298
unsigned long flags;
299
300
spin_lock_irqsave(&ep->buffer_lock, flags);
301
if (ep->umidi->disconnected) {
302
spin_unlock_irqrestore(&ep->buffer_lock, flags);
303
return;
304
}
305
306
urb_index = ep->next_urb;
307
for (;;) {
308
if (!(ep->active_urbs & (1 << urb_index))) {
309
urb = ep->urbs[urb_index].urb;
310
urb->transfer_buffer_length = 0;
311
ep->umidi->usb_protocol_ops->output(ep, urb);
312
if (urb->transfer_buffer_length == 0)
313
break;
314
315
dump_urb("sending", urb->transfer_buffer,
316
urb->transfer_buffer_length);
317
urb->dev = ep->umidi->dev;
318
if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0)
319
break;
320
ep->active_urbs |= 1 << urb_index;
321
}
322
if (++urb_index >= OUTPUT_URBS)
323
urb_index = 0;
324
if (urb_index == ep->next_urb)
325
break;
326
}
327
ep->next_urb = urb_index;
328
spin_unlock_irqrestore(&ep->buffer_lock, flags);
329
}
330
331
static void snd_usbmidi_out_work(struct work_struct *work)
332
{
333
struct snd_usb_midi_out_endpoint *ep =
334
container_of(work, struct snd_usb_midi_out_endpoint, work);
335
336
snd_usbmidi_do_output(ep);
337
}
338
339
/* called after transfers had been interrupted due to some USB error */
340
static void snd_usbmidi_error_timer(struct timer_list *t)
341
{
342
struct snd_usb_midi *umidi = timer_container_of(umidi, t, error_timer);
343
unsigned int i, j;
344
345
spin_lock(&umidi->disc_lock);
346
if (umidi->disconnected) {
347
spin_unlock(&umidi->disc_lock);
348
return;
349
}
350
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
351
struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
352
if (in && in->error_resubmit) {
353
in->error_resubmit = 0;
354
for (j = 0; j < INPUT_URBS; ++j) {
355
if (atomic_read(&in->urbs[j]->use_count))
356
continue;
357
in->urbs[j]->dev = umidi->dev;
358
snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
359
}
360
}
361
if (umidi->endpoints[i].out)
362
snd_usbmidi_do_output(umidi->endpoints[i].out);
363
}
364
spin_unlock(&umidi->disc_lock);
365
}
366
367
/* helper function to send static data that may not DMA-able */
368
static int send_bulk_static_data(struct snd_usb_midi_out_endpoint *ep,
369
const void *data, int len)
370
{
371
int err = 0;
372
void *buf = kmemdup(data, len, GFP_KERNEL);
373
if (!buf)
374
return -ENOMEM;
375
dump_urb("sending", buf, len);
376
if (ep->urbs[0].urb)
377
err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
378
buf, len, NULL, 250);
379
kfree(buf);
380
return err;
381
}
382
383
/*
384
* Standard USB MIDI protocol: see the spec.
385
* Midiman protocol: like the standard protocol, but the control byte is the
386
* fourth byte in each packet, and uses length instead of CIN.
387
*/
388
389
static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint *ep,
390
uint8_t *buffer, int buffer_length)
391
{
392
int i;
393
394
for (i = 0; i + 3 < buffer_length; i += 4)
395
if (buffer[i] != 0) {
396
int cable = buffer[i] >> 4;
397
int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
398
snd_usbmidi_input_data(ep, cable, &buffer[i + 1],
399
length);
400
}
401
}
402
403
static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint *ep,
404
uint8_t *buffer, int buffer_length)
405
{
406
int i;
407
408
for (i = 0; i + 3 < buffer_length; i += 4)
409
if (buffer[i + 3] != 0) {
410
int port = buffer[i + 3] >> 4;
411
int length = buffer[i + 3] & 3;
412
snd_usbmidi_input_data(ep, port, &buffer[i], length);
413
}
414
}
415
416
/*
417
* Buggy M-Audio device: running status on input results in a packet that has
418
* the data bytes but not the status byte and that is marked with CIN 4.
419
*/
420
static void snd_usbmidi_maudio_broken_running_status_input(
421
struct snd_usb_midi_in_endpoint *ep,
422
uint8_t *buffer, int buffer_length)
423
{
424
int i;
425
426
for (i = 0; i + 3 < buffer_length; i += 4)
427
if (buffer[i] != 0) {
428
int cable = buffer[i] >> 4;
429
u8 cin = buffer[i] & 0x0f;
430
struct usbmidi_in_port *port = &ep->ports[cable];
431
int length;
432
433
length = snd_usbmidi_cin_length[cin];
434
if (cin == 0xf && buffer[i + 1] >= 0xf8)
435
; /* realtime msg: no running status change */
436
else if (cin >= 0x8 && cin <= 0xe)
437
/* channel msg */
438
port->running_status_length = length - 1;
439
else if (cin == 0x4 &&
440
port->running_status_length != 0 &&
441
buffer[i + 1] < 0x80)
442
/* CIN 4 that is not a SysEx */
443
length = port->running_status_length;
444
else
445
/*
446
* All other msgs cannot begin running status.
447
* (A channel msg sent as two or three CIN 0xF
448
* packets could in theory, but this device
449
* doesn't use this format.)
450
*/
451
port->running_status_length = 0;
452
snd_usbmidi_input_data(ep, cable, &buffer[i + 1],
453
length);
454
}
455
}
456
457
/*
458
* QinHeng CH345 is buggy: every second packet inside a SysEx has not CIN 4
459
* but the previously seen CIN, but still with three data bytes.
460
*/
461
static void ch345_broken_sysex_input(struct snd_usb_midi_in_endpoint *ep,
462
uint8_t *buffer, int buffer_length)
463
{
464
unsigned int i, cin, length;
465
466
for (i = 0; i + 3 < buffer_length; i += 4) {
467
if (buffer[i] == 0 && i > 0)
468
break;
469
cin = buffer[i] & 0x0f;
470
if (ep->in_sysex &&
471
cin == ep->last_cin &&
472
(buffer[i + 1 + (cin == 0x6)] & 0x80) == 0)
473
cin = 0x4;
474
#if 0
475
if (buffer[i + 1] == 0x90) {
476
/*
477
* Either a corrupted running status or a real note-on
478
* message; impossible to detect reliably.
479
*/
480
}
481
#endif
482
length = snd_usbmidi_cin_length[cin];
483
snd_usbmidi_input_data(ep, 0, &buffer[i + 1], length);
484
ep->in_sysex = cin == 0x4;
485
if (!ep->in_sysex)
486
ep->last_cin = cin;
487
}
488
}
489
490
/*
491
* CME protocol: like the standard protocol, but SysEx commands are sent as a
492
* single USB packet preceded by a 0x0F byte, as are system realtime
493
* messages and MIDI Active Sensing.
494
* Also, multiple messages can be sent in the same packet.
495
*/
496
static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
497
uint8_t *buffer, int buffer_length)
498
{
499
int remaining = buffer_length;
500
501
/*
502
* CME send sysex, song position pointer, system realtime
503
* and active sensing using CIN 0x0f, which in the standard
504
* is only intended for single byte unparsed data.
505
* So we need to interpret these here before sending them on.
506
* By default, we assume single byte data, which is true
507
* for system realtime (midi clock, start, stop and continue)
508
* and active sensing, and handle the other (known) cases
509
* separately.
510
* In contrast to the standard, CME does not split sysex
511
* into multiple 4-byte packets, but lumps everything together
512
* into one. In addition, CME can string multiple messages
513
* together in the same packet; pressing the Record button
514
* on an UF6 sends a sysex message directly followed
515
* by a song position pointer in the same packet.
516
* For it to have any reasonable meaning, a sysex message
517
* needs to be at least 3 bytes in length (0xf0, id, 0xf7),
518
* corresponding to a packet size of 4 bytes, and the ones sent
519
* by CME devices are 6 or 7 bytes, making the packet fragments
520
* 7 or 8 bytes long (six or seven bytes plus preceding CN+CIN byte).
521
* For the other types, the packet size is always 4 bytes,
522
* as per the standard, with the data size being 3 for SPP
523
* and 1 for the others.
524
* Thus all packet fragments are at least 4 bytes long, so we can
525
* skip anything that is shorter; this also conveniantly skips
526
* packets with size 0, which CME devices continuously send when
527
* they have nothing better to do.
528
* Another quirk is that sometimes multiple messages are sent
529
* in the same packet. This has been observed for midi clock
530
* and active sensing i.e. 0x0f 0xf8 0x00 0x00 0x0f 0xfe 0x00 0x00,
531
* but also multiple note ons/offs, and control change together
532
* with MIDI clock. Similarly, some sysex messages are followed by
533
* the song position pointer in the same packet, and occasionally
534
* additionally by a midi clock or active sensing.
535
* We handle this by looping over all data and parsing it along the way.
536
*/
537
while (remaining >= 4) {
538
int source_length = 4; /* default */
539
540
if ((buffer[0] & 0x0f) == 0x0f) {
541
int data_length = 1; /* default */
542
543
if (buffer[1] == 0xf0) {
544
/* Sysex: Find EOX and send on whole message. */
545
/* To kick off the search, skip the first
546
* two bytes (CN+CIN and SYSEX (0xf0).
547
*/
548
uint8_t *tmp_buf = buffer + 2;
549
int tmp_length = remaining - 2;
550
551
while (tmp_length > 1 && *tmp_buf != 0xf7) {
552
tmp_buf++;
553
tmp_length--;
554
}
555
data_length = tmp_buf - buffer;
556
source_length = data_length + 1;
557
} else if (buffer[1] == 0xf2) {
558
/* Three byte song position pointer */
559
data_length = 3;
560
}
561
snd_usbmidi_input_data(ep, buffer[0] >> 4,
562
&buffer[1], data_length);
563
} else {
564
/* normal channel events */
565
snd_usbmidi_standard_input(ep, buffer, source_length);
566
}
567
buffer += source_length;
568
remaining -= source_length;
569
}
570
}
571
572
/*
573
* Adds one USB MIDI packet to the output buffer.
574
*/
575
static void snd_usbmidi_output_standard_packet(struct urb *urb, uint8_t p0,
576
uint8_t p1, uint8_t p2,
577
uint8_t p3)
578
{
579
580
uint8_t *buf =
581
(uint8_t *)urb->transfer_buffer + urb->transfer_buffer_length;
582
buf[0] = p0;
583
buf[1] = p1;
584
buf[2] = p2;
585
buf[3] = p3;
586
urb->transfer_buffer_length += 4;
587
}
588
589
/*
590
* Adds one Midiman packet to the output buffer.
591
*/
592
static void snd_usbmidi_output_midiman_packet(struct urb *urb, uint8_t p0,
593
uint8_t p1, uint8_t p2,
594
uint8_t p3)
595
{
596
597
uint8_t *buf =
598
(uint8_t *)urb->transfer_buffer + urb->transfer_buffer_length;
599
buf[0] = p1;
600
buf[1] = p2;
601
buf[2] = p3;
602
buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
603
urb->transfer_buffer_length += 4;
604
}
605
606
/*
607
* Converts MIDI commands to USB MIDI packets.
608
*/
609
static void snd_usbmidi_transmit_byte(struct usbmidi_out_port *port,
610
uint8_t b, struct urb *urb)
611
{
612
uint8_t p0 = port->cable;
613
void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
614
port->ep->umidi->usb_protocol_ops->output_packet;
615
616
if (b >= 0xf8) {
617
output_packet(urb, p0 | 0x0f, b, 0, 0);
618
} else if (b >= 0xf0) {
619
switch (b) {
620
case 0xf0:
621
port->data[0] = b;
622
port->state = STATE_SYSEX_1;
623
break;
624
case 0xf1:
625
case 0xf3:
626
port->data[0] = b;
627
port->state = STATE_1PARAM;
628
break;
629
case 0xf2:
630
port->data[0] = b;
631
port->state = STATE_2PARAM_1;
632
break;
633
case 0xf4:
634
case 0xf5:
635
port->state = STATE_UNKNOWN;
636
break;
637
case 0xf6:
638
output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
639
port->state = STATE_UNKNOWN;
640
break;
641
case 0xf7:
642
switch (port->state) {
643
case STATE_SYSEX_0:
644
output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
645
break;
646
case STATE_SYSEX_1:
647
output_packet(urb, p0 | 0x06, port->data[0],
648
0xf7, 0);
649
break;
650
case STATE_SYSEX_2:
651
output_packet(urb, p0 | 0x07, port->data[0],
652
port->data[1], 0xf7);
653
break;
654
}
655
port->state = STATE_UNKNOWN;
656
break;
657
}
658
} else if (b >= 0x80) {
659
port->data[0] = b;
660
if (b >= 0xc0 && b <= 0xdf)
661
port->state = STATE_1PARAM;
662
else
663
port->state = STATE_2PARAM_1;
664
} else { /* b < 0x80 */
665
switch (port->state) {
666
case STATE_1PARAM:
667
if (port->data[0] < 0xf0) {
668
p0 |= port->data[0] >> 4;
669
} else {
670
p0 |= 0x02;
671
port->state = STATE_UNKNOWN;
672
}
673
output_packet(urb, p0, port->data[0], b, 0);
674
break;
675
case STATE_2PARAM_1:
676
port->data[1] = b;
677
port->state = STATE_2PARAM_2;
678
break;
679
case STATE_2PARAM_2:
680
if (port->data[0] < 0xf0) {
681
p0 |= port->data[0] >> 4;
682
port->state = STATE_2PARAM_1;
683
} else {
684
p0 |= 0x03;
685
port->state = STATE_UNKNOWN;
686
}
687
output_packet(urb, p0, port->data[0], port->data[1], b);
688
break;
689
case STATE_SYSEX_0:
690
port->data[0] = b;
691
port->state = STATE_SYSEX_1;
692
break;
693
case STATE_SYSEX_1:
694
port->data[1] = b;
695
port->state = STATE_SYSEX_2;
696
break;
697
case STATE_SYSEX_2:
698
output_packet(urb, p0 | 0x04, port->data[0],
699
port->data[1], b);
700
port->state = STATE_SYSEX_0;
701
break;
702
}
703
}
704
}
705
706
static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint *ep,
707
struct urb *urb)
708
{
709
int p;
710
711
/* FIXME: lower-numbered ports can starve higher-numbered ports */
712
for (p = 0; p < 0x10; ++p) {
713
struct usbmidi_out_port *port = &ep->ports[p];
714
if (!port->active)
715
continue;
716
while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
717
uint8_t b;
718
if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
719
port->active = 0;
720
break;
721
}
722
snd_usbmidi_transmit_byte(port, b, urb);
723
}
724
}
725
}
726
727
static const struct usb_protocol_ops snd_usbmidi_standard_ops = {
728
.input = snd_usbmidi_standard_input,
729
.output = snd_usbmidi_standard_output,
730
.output_packet = snd_usbmidi_output_standard_packet,
731
};
732
733
static const struct usb_protocol_ops snd_usbmidi_midiman_ops = {
734
.input = snd_usbmidi_midiman_input,
735
.output = snd_usbmidi_standard_output,
736
.output_packet = snd_usbmidi_output_midiman_packet,
737
};
738
739
static const
740
struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
741
.input = snd_usbmidi_maudio_broken_running_status_input,
742
.output = snd_usbmidi_standard_output,
743
.output_packet = snd_usbmidi_output_standard_packet,
744
};
745
746
static const struct usb_protocol_ops snd_usbmidi_cme_ops = {
747
.input = snd_usbmidi_cme_input,
748
.output = snd_usbmidi_standard_output,
749
.output_packet = snd_usbmidi_output_standard_packet,
750
};
751
752
static const struct usb_protocol_ops snd_usbmidi_ch345_broken_sysex_ops = {
753
.input = ch345_broken_sysex_input,
754
.output = snd_usbmidi_standard_output,
755
.output_packet = snd_usbmidi_output_standard_packet,
756
};
757
758
/*
759
* AKAI MPD16 protocol:
760
*
761
* For control port (endpoint 1):
762
* ==============================
763
* One or more chunks consisting of first byte of (0x10 | msg_len) and then a
764
* SysEx message (msg_len=9 bytes long).
765
*
766
* For data port (endpoint 2):
767
* ===========================
768
* One or more chunks consisting of first byte of (0x20 | msg_len) and then a
769
* MIDI message (msg_len bytes long)
770
*
771
* Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
772
*/
773
static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
774
uint8_t *buffer, int buffer_length)
775
{
776
unsigned int pos = 0;
777
unsigned int len = (unsigned int)buffer_length;
778
while (pos < len) {
779
unsigned int port = (buffer[pos] >> 4) - 1;
780
unsigned int msg_len = buffer[pos] & 0x0f;
781
pos++;
782
if (pos + msg_len <= len && port < 2)
783
snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
784
pos += msg_len;
785
}
786
}
787
788
#define MAX_AKAI_SYSEX_LEN 9
789
790
static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
791
struct urb *urb)
792
{
793
uint8_t *msg;
794
int pos, end, count, buf_end;
795
uint8_t tmp[MAX_AKAI_SYSEX_LEN];
796
struct snd_rawmidi_substream *substream = ep->ports[0].substream;
797
798
if (!ep->ports[0].active)
799
return;
800
801
msg = urb->transfer_buffer + urb->transfer_buffer_length;
802
buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
803
804
/* only try adding more data when there's space for at least 1 SysEx */
805
while (urb->transfer_buffer_length < buf_end) {
806
count = snd_rawmidi_transmit_peek(substream,
807
tmp, MAX_AKAI_SYSEX_LEN);
808
if (!count) {
809
ep->ports[0].active = 0;
810
return;
811
}
812
/* try to skip non-SysEx data */
813
for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
814
;
815
816
if (pos > 0) {
817
snd_rawmidi_transmit_ack(substream, pos);
818
continue;
819
}
820
821
/* look for the start or end marker */
822
for (end = 1; end < count && tmp[end] < 0xF0; end++)
823
;
824
825
/* next SysEx started before the end of current one */
826
if (end < count && tmp[end] == 0xF0) {
827
/* it's incomplete - drop it */
828
snd_rawmidi_transmit_ack(substream, end);
829
continue;
830
}
831
/* SysEx complete */
832
if (end < count && tmp[end] == 0xF7) {
833
/* queue it, ack it, and get the next one */
834
count = end + 1;
835
msg[0] = 0x10 | count;
836
memcpy(&msg[1], tmp, count);
837
snd_rawmidi_transmit_ack(substream, count);
838
urb->transfer_buffer_length += count + 1;
839
msg += count + 1;
840
continue;
841
}
842
/* less than 9 bytes and no end byte - wait for more */
843
if (count < MAX_AKAI_SYSEX_LEN) {
844
ep->ports[0].active = 0;
845
return;
846
}
847
/* 9 bytes and no end marker in sight - malformed, skip it */
848
snd_rawmidi_transmit_ack(substream, count);
849
}
850
}
851
852
static const struct usb_protocol_ops snd_usbmidi_akai_ops = {
853
.input = snd_usbmidi_akai_input,
854
.output = snd_usbmidi_akai_output,
855
};
856
857
/*
858
* Novation USB MIDI protocol: number of data bytes is in the first byte
859
* (when receiving) (+1!) or in the second byte (when sending); data begins
860
* at the third byte.
861
*/
862
863
static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint *ep,
864
uint8_t *buffer, int buffer_length)
865
{
866
if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
867
return;
868
snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
869
}
870
871
static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint *ep,
872
struct urb *urb)
873
{
874
uint8_t *transfer_buffer;
875
int count;
876
877
if (!ep->ports[0].active)
878
return;
879
transfer_buffer = urb->transfer_buffer;
880
count = snd_rawmidi_transmit(ep->ports[0].substream,
881
&transfer_buffer[2],
882
ep->max_transfer - 2);
883
if (count < 1) {
884
ep->ports[0].active = 0;
885
return;
886
}
887
transfer_buffer[0] = 0;
888
transfer_buffer[1] = count;
889
urb->transfer_buffer_length = 2 + count;
890
}
891
892
static const struct usb_protocol_ops snd_usbmidi_novation_ops = {
893
.input = snd_usbmidi_novation_input,
894
.output = snd_usbmidi_novation_output,
895
};
896
897
/*
898
* "raw" protocol: just move raw MIDI bytes from/to the endpoint
899
*/
900
901
static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint *ep,
902
uint8_t *buffer, int buffer_length)
903
{
904
snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
905
}
906
907
static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint *ep,
908
struct urb *urb)
909
{
910
int count;
911
912
if (!ep->ports[0].active)
913
return;
914
count = snd_rawmidi_transmit(ep->ports[0].substream,
915
urb->transfer_buffer,
916
ep->max_transfer);
917
if (count < 1) {
918
ep->ports[0].active = 0;
919
return;
920
}
921
urb->transfer_buffer_length = count;
922
}
923
924
static const struct usb_protocol_ops snd_usbmidi_raw_ops = {
925
.input = snd_usbmidi_raw_input,
926
.output = snd_usbmidi_raw_output,
927
};
928
929
/*
930
* FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
931
*/
932
933
static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint *ep,
934
uint8_t *buffer, int buffer_length)
935
{
936
if (buffer_length > 2)
937
snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
938
}
939
940
static const struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
941
.input = snd_usbmidi_ftdi_input,
942
.output = snd_usbmidi_raw_output,
943
};
944
945
static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
946
uint8_t *buffer, int buffer_length)
947
{
948
if (buffer_length != 9)
949
return;
950
buffer_length = 8;
951
while (buffer_length && buffer[buffer_length - 1] == 0xFD)
952
buffer_length--;
953
if (buffer_length)
954
snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
955
}
956
957
static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
958
struct urb *urb)
959
{
960
int count;
961
962
if (!ep->ports[0].active)
963
return;
964
switch (snd_usb_get_speed(ep->umidi->dev)) {
965
case USB_SPEED_HIGH:
966
case USB_SPEED_SUPER:
967
case USB_SPEED_SUPER_PLUS:
968
count = 1;
969
break;
970
default:
971
count = 2;
972
}
973
count = snd_rawmidi_transmit(ep->ports[0].substream,
974
urb->transfer_buffer,
975
count);
976
if (count < 1) {
977
ep->ports[0].active = 0;
978
return;
979
}
980
981
memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
982
urb->transfer_buffer_length = ep->max_transfer;
983
}
984
985
static const struct usb_protocol_ops snd_usbmidi_122l_ops = {
986
.input = snd_usbmidi_us122l_input,
987
.output = snd_usbmidi_us122l_output,
988
};
989
990
/*
991
* Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
992
*/
993
994
static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint *ep)
995
{
996
static const u8 init_data[] = {
997
/* initialization magic: "get version" */
998
0xf0,
999
0x00, 0x20, 0x31, /* Emagic */
1000
0x64, /* Unitor8 */
1001
0x0b, /* version number request */
1002
0x00, /* command version */
1003
0x00, /* EEPROM, box 0 */
1004
0xf7
1005
};
1006
send_bulk_static_data(ep, init_data, sizeof(init_data));
1007
/* while we're at it, pour on more magic */
1008
send_bulk_static_data(ep, init_data, sizeof(init_data));
1009
}
1010
1011
static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint *ep)
1012
{
1013
static const u8 finish_data[] = {
1014
/* switch to patch mode with last preset */
1015
0xf0,
1016
0x00, 0x20, 0x31, /* Emagic */
1017
0x64, /* Unitor8 */
1018
0x10, /* patch switch command */
1019
0x00, /* command version */
1020
0x7f, /* to all boxes */
1021
0x40, /* last preset in EEPROM */
1022
0xf7
1023
};
1024
send_bulk_static_data(ep, finish_data, sizeof(finish_data));
1025
}
1026
1027
static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint *ep,
1028
uint8_t *buffer, int buffer_length)
1029
{
1030
int i;
1031
1032
/* FF indicates end of valid data */
1033
for (i = 0; i < buffer_length; ++i)
1034
if (buffer[i] == 0xff) {
1035
buffer_length = i;
1036
break;
1037
}
1038
1039
/* handle F5 at end of last buffer */
1040
if (ep->seen_f5)
1041
goto switch_port;
1042
1043
while (buffer_length > 0) {
1044
/* determine size of data until next F5 */
1045
for (i = 0; i < buffer_length; ++i)
1046
if (buffer[i] == 0xf5)
1047
break;
1048
snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
1049
buffer += i;
1050
buffer_length -= i;
1051
1052
if (buffer_length <= 0)
1053
break;
1054
/* assert(buffer[0] == 0xf5); */
1055
ep->seen_f5 = 1;
1056
++buffer;
1057
--buffer_length;
1058
1059
switch_port:
1060
if (buffer_length <= 0)
1061
break;
1062
if (buffer[0] < 0x80) {
1063
ep->current_port = (buffer[0] - 1) & 15;
1064
++buffer;
1065
--buffer_length;
1066
}
1067
ep->seen_f5 = 0;
1068
}
1069
}
1070
1071
static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint *ep,
1072
struct urb *urb)
1073
{
1074
int port0 = ep->current_port;
1075
uint8_t *buf = urb->transfer_buffer;
1076
int buf_free = ep->max_transfer;
1077
int length, i;
1078
1079
for (i = 0; i < 0x10; ++i) {
1080
/* round-robin, starting at the last current port */
1081
int portnum = (port0 + i) & 15;
1082
struct usbmidi_out_port *port = &ep->ports[portnum];
1083
1084
if (!port->active)
1085
continue;
1086
if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
1087
port->active = 0;
1088
continue;
1089
}
1090
1091
if (portnum != ep->current_port) {
1092
if (buf_free < 2)
1093
break;
1094
ep->current_port = portnum;
1095
buf[0] = 0xf5;
1096
buf[1] = (portnum + 1) & 15;
1097
buf += 2;
1098
buf_free -= 2;
1099
}
1100
1101
if (buf_free < 1)
1102
break;
1103
length = snd_rawmidi_transmit(port->substream, buf, buf_free);
1104
if (length > 0) {
1105
buf += length;
1106
buf_free -= length;
1107
if (buf_free < 1)
1108
break;
1109
}
1110
}
1111
if (buf_free < ep->max_transfer && buf_free > 0) {
1112
*buf = 0xff;
1113
--buf_free;
1114
}
1115
urb->transfer_buffer_length = ep->max_transfer - buf_free;
1116
}
1117
1118
static const struct usb_protocol_ops snd_usbmidi_emagic_ops = {
1119
.input = snd_usbmidi_emagic_input,
1120
.output = snd_usbmidi_emagic_output,
1121
.init_out_endpoint = snd_usbmidi_emagic_init_out,
1122
.finish_out_endpoint = snd_usbmidi_emagic_finish_out,
1123
};
1124
1125
1126
static void update_roland_altsetting(struct snd_usb_midi *umidi)
1127
{
1128
struct usb_interface *intf;
1129
struct usb_host_interface *hostif;
1130
struct usb_interface_descriptor *intfd;
1131
int is_light_load;
1132
1133
intf = umidi->iface;
1134
is_light_load = intf->cur_altsetting != intf->altsetting;
1135
if (umidi->roland_load_ctl->private_value == is_light_load)
1136
return;
1137
hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
1138
intfd = get_iface_desc(hostif);
1139
snd_usbmidi_input_stop(&umidi->list);
1140
usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1141
intfd->bAlternateSetting);
1142
snd_usbmidi_input_start(&umidi->list);
1143
}
1144
1145
static int substream_open(struct snd_rawmidi_substream *substream, int dir,
1146
int open)
1147
{
1148
struct snd_usb_midi *umidi = substream->rmidi->private_data;
1149
struct snd_kcontrol *ctl;
1150
1151
down_read(&umidi->disc_rwsem);
1152
if (umidi->disconnected) {
1153
up_read(&umidi->disc_rwsem);
1154
return open ? -ENODEV : 0;
1155
}
1156
1157
mutex_lock(&umidi->mutex);
1158
if (open) {
1159
if (!umidi->opened[0] && !umidi->opened[1]) {
1160
if (umidi->roland_load_ctl) {
1161
ctl = umidi->roland_load_ctl;
1162
ctl->vd[0].access |=
1163
SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1164
snd_ctl_notify(umidi->card,
1165
SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1166
update_roland_altsetting(umidi);
1167
}
1168
}
1169
umidi->opened[dir]++;
1170
if (umidi->opened[1])
1171
snd_usbmidi_input_start(&umidi->list);
1172
} else {
1173
umidi->opened[dir]--;
1174
if (!umidi->opened[1])
1175
snd_usbmidi_input_stop(&umidi->list);
1176
if (!umidi->opened[0] && !umidi->opened[1]) {
1177
if (umidi->roland_load_ctl) {
1178
ctl = umidi->roland_load_ctl;
1179
ctl->vd[0].access &=
1180
~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1181
snd_ctl_notify(umidi->card,
1182
SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
1183
}
1184
}
1185
}
1186
mutex_unlock(&umidi->mutex);
1187
up_read(&umidi->disc_rwsem);
1188
return 0;
1189
}
1190
1191
static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
1192
{
1193
struct snd_usb_midi *umidi = substream->rmidi->private_data;
1194
struct usbmidi_out_port *port = NULL;
1195
int i, j;
1196
1197
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1198
if (umidi->endpoints[i].out)
1199
for (j = 0; j < 0x10; ++j)
1200
if (umidi->endpoints[i].out->ports[j].substream == substream) {
1201
port = &umidi->endpoints[i].out->ports[j];
1202
break;
1203
}
1204
if (!port)
1205
return -ENXIO;
1206
1207
substream->runtime->private_data = port;
1208
port->state = STATE_UNKNOWN;
1209
return substream_open(substream, 0, 1);
1210
}
1211
1212
static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
1213
{
1214
struct usbmidi_out_port *port = substream->runtime->private_data;
1215
1216
flush_work(&port->ep->work);
1217
return substream_open(substream, 0, 0);
1218
}
1219
1220
static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream,
1221
int up)
1222
{
1223
struct usbmidi_out_port *port =
1224
(struct usbmidi_out_port *)substream->runtime->private_data;
1225
1226
port->active = up;
1227
if (up) {
1228
if (port->ep->umidi->disconnected) {
1229
/* gobble up remaining bytes to prevent wait in
1230
* snd_rawmidi_drain_output */
1231
snd_rawmidi_proceed(substream);
1232
return;
1233
}
1234
queue_work(system_highpri_wq, &port->ep->work);
1235
}
1236
}
1237
1238
static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
1239
{
1240
struct usbmidi_out_port *port = substream->runtime->private_data;
1241
struct snd_usb_midi_out_endpoint *ep = port->ep;
1242
unsigned int drain_urbs;
1243
DEFINE_WAIT(wait);
1244
long timeout = msecs_to_jiffies(50);
1245
1246
if (ep->umidi->disconnected)
1247
return;
1248
/*
1249
* The substream buffer is empty, but some data might still be in the
1250
* currently active URBs, so we have to wait for those to complete.
1251
*/
1252
spin_lock_irq(&ep->buffer_lock);
1253
drain_urbs = ep->active_urbs;
1254
if (drain_urbs) {
1255
ep->drain_urbs |= drain_urbs;
1256
do {
1257
prepare_to_wait(&ep->drain_wait, &wait,
1258
TASK_UNINTERRUPTIBLE);
1259
spin_unlock_irq(&ep->buffer_lock);
1260
timeout = schedule_timeout(timeout);
1261
spin_lock_irq(&ep->buffer_lock);
1262
drain_urbs &= ep->drain_urbs;
1263
} while (drain_urbs && timeout);
1264
finish_wait(&ep->drain_wait, &wait);
1265
}
1266
port->active = 0;
1267
spin_unlock_irq(&ep->buffer_lock);
1268
}
1269
1270
static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
1271
{
1272
return substream_open(substream, 1, 1);
1273
}
1274
1275
static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
1276
{
1277
return substream_open(substream, 1, 0);
1278
}
1279
1280
static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream,
1281
int up)
1282
{
1283
struct snd_usb_midi *umidi = substream->rmidi->private_data;
1284
1285
if (up)
1286
set_bit(substream->number, &umidi->input_triggered);
1287
else
1288
clear_bit(substream->number, &umidi->input_triggered);
1289
}
1290
1291
static const struct snd_rawmidi_ops snd_usbmidi_output_ops = {
1292
.open = snd_usbmidi_output_open,
1293
.close = snd_usbmidi_output_close,
1294
.trigger = snd_usbmidi_output_trigger,
1295
.drain = snd_usbmidi_output_drain,
1296
};
1297
1298
static const struct snd_rawmidi_ops snd_usbmidi_input_ops = {
1299
.open = snd_usbmidi_input_open,
1300
.close = snd_usbmidi_input_close,
1301
.trigger = snd_usbmidi_input_trigger
1302
};
1303
1304
static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1305
unsigned int buffer_length)
1306
{
1307
usb_free_coherent(umidi->dev, buffer_length,
1308
urb->transfer_buffer, urb->transfer_dma);
1309
usb_free_urb(urb);
1310
}
1311
1312
/*
1313
* Frees an input endpoint.
1314
* May be called when ep hasn't been initialized completely.
1315
*/
1316
static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint *ep)
1317
{
1318
unsigned int i;
1319
1320
for (i = 0; i < INPUT_URBS; ++i)
1321
if (ep->urbs[i])
1322
free_urb_and_buffer(ep->umidi, ep->urbs[i],
1323
ep->urbs[i]->transfer_buffer_length);
1324
kfree(ep);
1325
}
1326
1327
/*
1328
* Creates an input endpoint.
1329
*/
1330
static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi *umidi,
1331
struct snd_usb_midi_endpoint_info *ep_info,
1332
struct snd_usb_midi_endpoint *rep)
1333
{
1334
struct snd_usb_midi_in_endpoint *ep;
1335
void *buffer;
1336
unsigned int pipe;
1337
int length;
1338
unsigned int i;
1339
int err;
1340
1341
rep->in = NULL;
1342
ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1343
if (!ep)
1344
return -ENOMEM;
1345
ep->umidi = umidi;
1346
1347
for (i = 0; i < INPUT_URBS; ++i) {
1348
ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1349
if (!ep->urbs[i]) {
1350
err = -ENOMEM;
1351
goto error;
1352
}
1353
}
1354
if (ep_info->in_interval)
1355
pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
1356
else
1357
pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1358
length = usb_maxpacket(umidi->dev, pipe);
1359
for (i = 0; i < INPUT_URBS; ++i) {
1360
buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1361
&ep->urbs[i]->transfer_dma);
1362
if (!buffer) {
1363
err = -ENOMEM;
1364
goto error;
1365
}
1366
if (ep_info->in_interval)
1367
usb_fill_int_urb(ep->urbs[i], umidi->dev,
1368
pipe, buffer, length,
1369
snd_usbmidi_in_urb_complete,
1370
ep, ep_info->in_interval);
1371
else
1372
usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
1373
pipe, buffer, length,
1374
snd_usbmidi_in_urb_complete, ep);
1375
ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1376
err = usb_urb_ep_type_check(ep->urbs[i]);
1377
if (err < 0) {
1378
dev_err(&umidi->dev->dev, "invalid MIDI in EP %x\n",
1379
ep_info->in_ep);
1380
goto error;
1381
}
1382
}
1383
1384
rep->in = ep;
1385
return 0;
1386
1387
error:
1388
snd_usbmidi_in_endpoint_delete(ep);
1389
return err;
1390
}
1391
1392
/*
1393
* Frees an output endpoint.
1394
* May be called when ep hasn't been initialized completely.
1395
*/
1396
static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
1397
{
1398
unsigned int i;
1399
1400
for (i = 0; i < OUTPUT_URBS; ++i)
1401
if (ep->urbs[i].urb) {
1402
free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1403
ep->max_transfer);
1404
ep->urbs[i].urb = NULL;
1405
}
1406
}
1407
1408
static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1409
{
1410
snd_usbmidi_out_endpoint_clear(ep);
1411
kfree(ep);
1412
}
1413
1414
/*
1415
* Creates an output endpoint, and initializes output ports.
1416
*/
1417
static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi *umidi,
1418
struct snd_usb_midi_endpoint_info *ep_info,
1419
struct snd_usb_midi_endpoint *rep)
1420
{
1421
struct snd_usb_midi_out_endpoint *ep;
1422
unsigned int i;
1423
unsigned int pipe;
1424
void *buffer;
1425
int err;
1426
1427
rep->out = NULL;
1428
ep = kzalloc(sizeof(*ep), GFP_KERNEL);
1429
if (!ep)
1430
return -ENOMEM;
1431
ep->umidi = umidi;
1432
1433
for (i = 0; i < OUTPUT_URBS; ++i) {
1434
ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1435
if (!ep->urbs[i].urb) {
1436
err = -ENOMEM;
1437
goto error;
1438
}
1439
ep->urbs[i].ep = ep;
1440
}
1441
if (ep_info->out_interval)
1442
pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
1443
else
1444
pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
1445
switch (umidi->usb_id) {
1446
default:
1447
ep->max_transfer = usb_maxpacket(umidi->dev, pipe);
1448
break;
1449
/*
1450
* Various chips declare a packet size larger than 4 bytes, but
1451
* do not actually work with larger packets:
1452
*/
1453
case USB_ID(0x0a67, 0x5011): /* Medeli DD305 */
1454
case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1455
case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1456
case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1457
case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1458
case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
1459
case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
1460
ep->max_transfer = 4;
1461
break;
1462
/*
1463
* Some devices only work with 9 bytes packet size:
1464
*/
1465
case USB_ID(0x0644, 0x800e): /* Tascam US-122L */
1466
case USB_ID(0x0644, 0x800f): /* Tascam US-144 */
1467
ep->max_transfer = 9;
1468
break;
1469
}
1470
for (i = 0; i < OUTPUT_URBS; ++i) {
1471
buffer = usb_alloc_coherent(umidi->dev,
1472
ep->max_transfer, GFP_KERNEL,
1473
&ep->urbs[i].urb->transfer_dma);
1474
if (!buffer) {
1475
err = -ENOMEM;
1476
goto error;
1477
}
1478
if (ep_info->out_interval)
1479
usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
1480
pipe, buffer, ep->max_transfer,
1481
snd_usbmidi_out_urb_complete,
1482
&ep->urbs[i], ep_info->out_interval);
1483
else
1484
usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
1485
pipe, buffer, ep->max_transfer,
1486
snd_usbmidi_out_urb_complete,
1487
&ep->urbs[i]);
1488
err = usb_urb_ep_type_check(ep->urbs[i].urb);
1489
if (err < 0) {
1490
dev_err(&umidi->dev->dev, "invalid MIDI out EP %x\n",
1491
ep_info->out_ep);
1492
goto error;
1493
}
1494
ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1495
}
1496
1497
spin_lock_init(&ep->buffer_lock);
1498
INIT_WORK(&ep->work, snd_usbmidi_out_work);
1499
init_waitqueue_head(&ep->drain_wait);
1500
1501
for (i = 0; i < 0x10; ++i)
1502
if (ep_info->out_cables & (1 << i)) {
1503
ep->ports[i].ep = ep;
1504
ep->ports[i].cable = i << 4;
1505
}
1506
1507
if (umidi->usb_protocol_ops->init_out_endpoint)
1508
umidi->usb_protocol_ops->init_out_endpoint(ep);
1509
1510
rep->out = ep;
1511
return 0;
1512
1513
error:
1514
snd_usbmidi_out_endpoint_delete(ep);
1515
return err;
1516
}
1517
1518
/*
1519
* Frees everything.
1520
*/
1521
static void snd_usbmidi_free(struct snd_usb_midi *umidi)
1522
{
1523
int i;
1524
1525
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1526
struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
1527
if (ep->out)
1528
snd_usbmidi_out_endpoint_delete(ep->out);
1529
if (ep->in)
1530
snd_usbmidi_in_endpoint_delete(ep->in);
1531
}
1532
mutex_destroy(&umidi->mutex);
1533
timer_shutdown_sync(&umidi->error_timer);
1534
kfree(umidi);
1535
}
1536
1537
/*
1538
* Unlinks all URBs (must be done before the usb_device is deleted).
1539
*/
1540
void snd_usbmidi_disconnect(struct list_head *p)
1541
{
1542
struct snd_usb_midi *umidi;
1543
unsigned int i, j;
1544
1545
umidi = list_entry(p, struct snd_usb_midi, list);
1546
/*
1547
* an URB's completion handler may start the timer and
1548
* a timer may submit an URB. To reliably break the cycle
1549
* a flag under lock must be used
1550
*/
1551
down_write(&umidi->disc_rwsem);
1552
spin_lock_irq(&umidi->disc_lock);
1553
umidi->disconnected = 1;
1554
spin_unlock_irq(&umidi->disc_lock);
1555
up_write(&umidi->disc_rwsem);
1556
1557
timer_shutdown_sync(&umidi->error_timer);
1558
1559
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1560
struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
1561
if (ep->out)
1562
cancel_work_sync(&ep->out->work);
1563
if (ep->out) {
1564
for (j = 0; j < OUTPUT_URBS; ++j)
1565
usb_kill_urb(ep->out->urbs[j].urb);
1566
if (umidi->usb_protocol_ops->finish_out_endpoint)
1567
umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
1568
ep->out->active_urbs = 0;
1569
if (ep->out->drain_urbs) {
1570
ep->out->drain_urbs = 0;
1571
wake_up(&ep->out->drain_wait);
1572
}
1573
}
1574
if (ep->in)
1575
for (j = 0; j < INPUT_URBS; ++j)
1576
usb_kill_urb(ep->in->urbs[j]);
1577
/* free endpoints here; later call can result in Oops */
1578
if (ep->out)
1579
snd_usbmidi_out_endpoint_clear(ep->out);
1580
if (ep->in) {
1581
snd_usbmidi_in_endpoint_delete(ep->in);
1582
ep->in = NULL;
1583
}
1584
}
1585
}
1586
EXPORT_SYMBOL(snd_usbmidi_disconnect);
1587
1588
static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
1589
{
1590
struct snd_usb_midi *umidi = rmidi->private_data;
1591
snd_usbmidi_free(umidi);
1592
}
1593
1594
static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi *umidi,
1595
int stream,
1596
int number)
1597
{
1598
struct snd_rawmidi_substream *substream;
1599
1600
list_for_each_entry(substream, &umidi->rmidi->streams[stream].substreams,
1601
list) {
1602
if (substream->number == number)
1603
return substream;
1604
}
1605
return NULL;
1606
}
1607
1608
/*
1609
* This list specifies names for ports that do not fit into the standard
1610
* "(product) MIDI (n)" schema because they aren't external MIDI ports,
1611
* such as internal control or synthesizer ports.
1612
*/
1613
static struct port_info {
1614
u32 id;
1615
short int port;
1616
short int voices;
1617
const char *name;
1618
unsigned int seq_flags;
1619
} snd_usbmidi_port_info[] = {
1620
#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1621
{ .id = USB_ID(vendor, product), \
1622
.port = num, .voices = voices_, \
1623
.name = name_, .seq_flags = flags }
1624
#define EXTERNAL_PORT(vendor, product, num, name) \
1625
PORT_INFO(vendor, product, num, name, 0, \
1626
SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1627
SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1628
SNDRV_SEQ_PORT_TYPE_PORT)
1629
#define CONTROL_PORT(vendor, product, num, name) \
1630
PORT_INFO(vendor, product, num, name, 0, \
1631
SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1632
SNDRV_SEQ_PORT_TYPE_HARDWARE)
1633
#define GM_SYNTH_PORT(vendor, product, num, name, voices) \
1634
PORT_INFO(vendor, product, num, name, voices, \
1635
SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1636
SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1637
SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1638
SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1639
#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1640
PORT_INFO(vendor, product, num, name, voices, \
1641
SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1642
SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1643
SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1644
SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1645
SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1646
SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1647
SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1648
#define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \
1649
PORT_INFO(vendor, product, num, name, voices, \
1650
SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1651
SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1652
SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1653
SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1654
SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1655
SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \
1656
SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1657
SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1658
/* Yamaha MOTIF XF */
1659
GM_SYNTH_PORT(0x0499, 0x105c, 0, "%s Tone Generator", 128),
1660
CONTROL_PORT(0x0499, 0x105c, 1, "%s Remote Control"),
1661
EXTERNAL_PORT(0x0499, 0x105c, 2, "%s Thru"),
1662
CONTROL_PORT(0x0499, 0x105c, 3, "%s Editor"),
1663
/* Roland UA-100 */
1664
CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
1665
/* Roland SC-8850 */
1666
SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1667
SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1668
SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1669
SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1670
EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1671
EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
1672
/* Roland U-8 */
1673
EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1674
CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
1675
/* Roland SC-8820 */
1676
SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1677
SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1678
EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
1679
/* Roland SK-500 */
1680
SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1681
SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1682
EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
1683
/* Roland SC-D70 */
1684
SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1685
SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1686
EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
1687
/* Edirol UM-880 */
1688
CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
1689
/* Edirol SD-90 */
1690
ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1691
ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1692
EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1693
EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
1694
/* Edirol UM-550 */
1695
CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
1696
/* Edirol SD-20 */
1697
ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1698
ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1699
EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
1700
/* Edirol SD-80 */
1701
ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1702
ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1703
EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1704
EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
1705
/* Edirol UA-700 */
1706
EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1707
CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
1708
/* Roland VariOS */
1709
EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1710
EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1711
EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
1712
/* Edirol PCR */
1713
EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1714
EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1715
EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
1716
/* BOSS GS-10 */
1717
EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1718
CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
1719
/* Edirol UA-1000 */
1720
EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1721
CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
1722
/* Edirol UR-80 */
1723
EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1724
EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1725
EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
1726
/* Edirol PCR-A */
1727
EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1728
EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1729
EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
1730
/* BOSS GT-PRO */
1731
CONTROL_PORT(0x0582, 0x0089, 0, "%s Control"),
1732
/* Edirol UM-3EX */
1733
CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
1734
/* Roland VG-99 */
1735
CONTROL_PORT(0x0582, 0x00b2, 0, "%s Control"),
1736
EXTERNAL_PORT(0x0582, 0x00b2, 1, "%s MIDI"),
1737
/* Cakewalk Sonar V-Studio 100 */
1738
EXTERNAL_PORT(0x0582, 0x00eb, 0, "%s MIDI"),
1739
CONTROL_PORT(0x0582, 0x00eb, 1, "%s Control"),
1740
/* Roland VB-99 */
1741
CONTROL_PORT(0x0582, 0x0102, 0, "%s Control"),
1742
EXTERNAL_PORT(0x0582, 0x0102, 1, "%s MIDI"),
1743
/* Roland A-PRO */
1744
EXTERNAL_PORT(0x0582, 0x010f, 0, "%s MIDI"),
1745
CONTROL_PORT(0x0582, 0x010f, 1, "%s 1"),
1746
CONTROL_PORT(0x0582, 0x010f, 2, "%s 2"),
1747
/* Roland SD-50 */
1748
ROLAND_SYNTH_PORT(0x0582, 0x0114, 0, "%s Synth", 128),
1749
EXTERNAL_PORT(0x0582, 0x0114, 1, "%s MIDI"),
1750
CONTROL_PORT(0x0582, 0x0114, 2, "%s Control"),
1751
/* Roland OCTA-CAPTURE */
1752
EXTERNAL_PORT(0x0582, 0x0120, 0, "%s MIDI"),
1753
CONTROL_PORT(0x0582, 0x0120, 1, "%s Control"),
1754
EXTERNAL_PORT(0x0582, 0x0121, 0, "%s MIDI"),
1755
CONTROL_PORT(0x0582, 0x0121, 1, "%s Control"),
1756
/* Roland SPD-SX */
1757
CONTROL_PORT(0x0582, 0x0145, 0, "%s Control"),
1758
EXTERNAL_PORT(0x0582, 0x0145, 1, "%s MIDI"),
1759
/* Roland A-Series */
1760
CONTROL_PORT(0x0582, 0x0156, 0, "%s Keyboard"),
1761
EXTERNAL_PORT(0x0582, 0x0156, 1, "%s MIDI"),
1762
/* Roland INTEGRA-7 */
1763
ROLAND_SYNTH_PORT(0x0582, 0x015b, 0, "%s Synth", 128),
1764
CONTROL_PORT(0x0582, 0x015b, 1, "%s Control"),
1765
/* M-Audio MidiSport 8x8 */
1766
CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1767
CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
1768
/* MOTU Fastlane */
1769
EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1770
EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
1771
/* Emagic Unitor8/AMT8/MT4 */
1772
EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1773
EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1774
EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
1775
/* Akai MPD16 */
1776
CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1777
PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1778
SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1779
SNDRV_SEQ_PORT_TYPE_HARDWARE),
1780
/* Access Music Virus TI */
1781
EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1782
PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1783
SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1784
SNDRV_SEQ_PORT_TYPE_HARDWARE |
1785
SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
1786
};
1787
1788
static struct port_info *find_port_info(struct snd_usb_midi *umidi, int number)
1789
{
1790
int i;
1791
1792
for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
1793
if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
1794
snd_usbmidi_port_info[i].port == number)
1795
return &snd_usbmidi_port_info[i];
1796
}
1797
return NULL;
1798
}
1799
1800
static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1801
struct snd_seq_port_info *seq_port_info)
1802
{
1803
struct snd_usb_midi *umidi = rmidi->private_data;
1804
struct port_info *port_info;
1805
1806
/* TODO: read port flags from descriptors */
1807
port_info = find_port_info(umidi, number);
1808
if (port_info) {
1809
seq_port_info->type = port_info->seq_flags;
1810
seq_port_info->midi_voices = port_info->voices;
1811
}
1812
}
1813
1814
/* return iJack for the corresponding jackID */
1815
static int find_usb_ijack(struct usb_host_interface *hostif, uint8_t jack_id)
1816
{
1817
unsigned char *extra = hostif->extra;
1818
int extralen = hostif->extralen;
1819
struct usb_descriptor_header *h;
1820
struct usb_midi_out_jack_descriptor *outjd;
1821
struct usb_midi_in_jack_descriptor *injd;
1822
size_t sz;
1823
1824
while (extralen > 4) {
1825
h = (struct usb_descriptor_header *)extra;
1826
if (h->bDescriptorType != USB_DT_CS_INTERFACE)
1827
goto next;
1828
1829
outjd = (struct usb_midi_out_jack_descriptor *)h;
1830
if (h->bLength >= sizeof(*outjd) &&
1831
outjd->bDescriptorSubtype == UAC_MIDI_OUT_JACK &&
1832
outjd->bJackID == jack_id) {
1833
sz = USB_DT_MIDI_OUT_SIZE(outjd->bNrInputPins);
1834
if (outjd->bLength < sz)
1835
goto next;
1836
return *(extra + sz - 1);
1837
}
1838
1839
injd = (struct usb_midi_in_jack_descriptor *)h;
1840
if (injd->bLength >= sizeof(*injd) &&
1841
injd->bDescriptorSubtype == UAC_MIDI_IN_JACK &&
1842
injd->bJackID == jack_id)
1843
return injd->iJack;
1844
1845
next:
1846
if (!extra[0])
1847
break;
1848
extralen -= extra[0];
1849
extra += extra[0];
1850
}
1851
return 0;
1852
}
1853
1854
static void snd_usbmidi_init_substream(struct snd_usb_midi *umidi,
1855
int stream, int number, int jack_id,
1856
struct snd_rawmidi_substream **rsubstream)
1857
{
1858
struct port_info *port_info;
1859
const char *name_format;
1860
struct usb_interface *intf;
1861
struct usb_host_interface *hostif;
1862
uint8_t jack_name_buf[32];
1863
uint8_t *default_jack_name = "MIDI";
1864
uint8_t *jack_name = default_jack_name;
1865
uint8_t iJack;
1866
int res;
1867
1868
struct snd_rawmidi_substream *substream =
1869
snd_usbmidi_find_substream(umidi, stream, number);
1870
if (!substream) {
1871
dev_err(&umidi->dev->dev, "substream %d:%d not found\n", stream,
1872
number);
1873
return;
1874
}
1875
1876
intf = umidi->iface;
1877
if (intf && jack_id >= 0) {
1878
hostif = intf->cur_altsetting;
1879
iJack = find_usb_ijack(hostif, jack_id);
1880
if (iJack != 0) {
1881
res = usb_string(umidi->dev, iJack, jack_name_buf,
1882
ARRAY_SIZE(jack_name_buf));
1883
if (res)
1884
jack_name = jack_name_buf;
1885
}
1886
}
1887
1888
port_info = find_port_info(umidi, number);
1889
if (port_info || jack_name == default_jack_name ||
1890
strncmp(umidi->card->shortname, jack_name, strlen(umidi->card->shortname)) != 0) {
1891
name_format = port_info ? port_info->name :
1892
(jack_name != default_jack_name ? "%s %s" : "%s %s %d");
1893
snprintf(substream->name, sizeof(substream->name),
1894
name_format, umidi->card->shortname, jack_name, number + 1);
1895
} else {
1896
/* The manufacturer included the iProduct name in the jack
1897
* name, do not use both
1898
*/
1899
strscpy(substream->name, jack_name);
1900
}
1901
1902
*rsubstream = substream;
1903
}
1904
1905
/*
1906
* Creates the endpoints and their ports.
1907
*/
1908
static int snd_usbmidi_create_endpoints(struct snd_usb_midi *umidi,
1909
struct snd_usb_midi_endpoint_info *endpoints)
1910
{
1911
int i, j, err;
1912
int out_ports = 0, in_ports = 0;
1913
1914
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1915
if (endpoints[i].out_cables) {
1916
err = snd_usbmidi_out_endpoint_create(umidi,
1917
&endpoints[i],
1918
&umidi->endpoints[i]);
1919
if (err < 0)
1920
return err;
1921
}
1922
if (endpoints[i].in_cables) {
1923
err = snd_usbmidi_in_endpoint_create(umidi,
1924
&endpoints[i],
1925
&umidi->endpoints[i]);
1926
if (err < 0)
1927
return err;
1928
}
1929
1930
for (j = 0; j < 0x10; ++j) {
1931
if (endpoints[i].out_cables & (1 << j)) {
1932
snd_usbmidi_init_substream(umidi,
1933
SNDRV_RAWMIDI_STREAM_OUTPUT,
1934
out_ports,
1935
endpoints[i].assoc_out_jacks[j],
1936
&umidi->endpoints[i].out->ports[j].substream);
1937
++out_ports;
1938
}
1939
if (endpoints[i].in_cables & (1 << j)) {
1940
snd_usbmidi_init_substream(umidi,
1941
SNDRV_RAWMIDI_STREAM_INPUT,
1942
in_ports,
1943
endpoints[i].assoc_in_jacks[j],
1944
&umidi->endpoints[i].in->ports[j].substream);
1945
++in_ports;
1946
}
1947
}
1948
}
1949
dev_dbg(&umidi->dev->dev, "created %d output and %d input ports\n",
1950
out_ports, in_ports);
1951
return 0;
1952
}
1953
1954
static struct usb_ms_endpoint_descriptor *find_usb_ms_endpoint_descriptor(
1955
struct usb_host_endpoint *hostep)
1956
{
1957
unsigned char *extra = hostep->extra;
1958
int extralen = hostep->extralen;
1959
1960
while (extralen > 3) {
1961
struct usb_ms_endpoint_descriptor *ms_ep =
1962
(struct usb_ms_endpoint_descriptor *)extra;
1963
1964
if (ms_ep->bLength > 3 &&
1965
ms_ep->bDescriptorType == USB_DT_CS_ENDPOINT &&
1966
ms_ep->bDescriptorSubtype == UAC_MS_GENERAL)
1967
return ms_ep;
1968
if (!extra[0])
1969
break;
1970
extralen -= extra[0];
1971
extra += extra[0];
1972
}
1973
return NULL;
1974
}
1975
1976
/*
1977
* Returns MIDIStreaming device capabilities.
1978
*/
1979
static int snd_usbmidi_get_ms_info(struct snd_usb_midi *umidi,
1980
struct snd_usb_midi_endpoint_info *endpoints)
1981
{
1982
struct usb_interface *intf;
1983
struct usb_host_interface *hostif;
1984
struct usb_interface_descriptor *intfd;
1985
struct usb_ms_header_descriptor *ms_header;
1986
struct usb_host_endpoint *hostep;
1987
struct usb_endpoint_descriptor *ep;
1988
struct usb_ms_endpoint_descriptor *ms_ep;
1989
int i, j, epidx;
1990
1991
intf = umidi->iface;
1992
if (!intf)
1993
return -ENXIO;
1994
hostif = &intf->altsetting[0];
1995
intfd = get_iface_desc(hostif);
1996
ms_header = (struct usb_ms_header_descriptor *)hostif->extra;
1997
if (hostif->extralen >= 7 &&
1998
ms_header->bLength >= 7 &&
1999
ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
2000
ms_header->bDescriptorSubtype == UAC_HEADER)
2001
dev_dbg(&umidi->dev->dev, "MIDIStreaming version %02x.%02x\n",
2002
((uint8_t *)&ms_header->bcdMSC)[1], ((uint8_t *)&ms_header->bcdMSC)[0]);
2003
else
2004
dev_warn(&umidi->dev->dev,
2005
"MIDIStreaming interface descriptor not found\n");
2006
2007
epidx = 0;
2008
for (i = 0; i < intfd->bNumEndpoints; ++i) {
2009
hostep = &hostif->endpoint[i];
2010
ep = get_ep_desc(hostep);
2011
if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
2012
continue;
2013
ms_ep = find_usb_ms_endpoint_descriptor(hostep);
2014
if (!ms_ep)
2015
continue;
2016
if (ms_ep->bLength <= sizeof(*ms_ep))
2017
continue;
2018
if (ms_ep->bNumEmbMIDIJack > 0x10)
2019
continue;
2020
if (ms_ep->bLength < sizeof(*ms_ep) + ms_ep->bNumEmbMIDIJack)
2021
continue;
2022
if (usb_endpoint_dir_out(ep)) {
2023
if (endpoints[epidx].out_ep) {
2024
if (++epidx >= MIDI_MAX_ENDPOINTS) {
2025
dev_warn(&umidi->dev->dev,
2026
"too many endpoints\n");
2027
break;
2028
}
2029
}
2030
endpoints[epidx].out_ep = usb_endpoint_num(ep);
2031
if (usb_endpoint_xfer_int(ep))
2032
endpoints[epidx].out_interval = ep->bInterval;
2033
else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
2034
/*
2035
* Low speed bulk transfers don't exist, so
2036
* force interrupt transfers for devices like
2037
* ESI MIDI Mate that try to use them anyway.
2038
*/
2039
endpoints[epidx].out_interval = 1;
2040
endpoints[epidx].out_cables =
2041
(1 << ms_ep->bNumEmbMIDIJack) - 1;
2042
for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j)
2043
endpoints[epidx].assoc_out_jacks[j] = ms_ep->baAssocJackID[j];
2044
for (; j < ARRAY_SIZE(endpoints[epidx].assoc_out_jacks); ++j)
2045
endpoints[epidx].assoc_out_jacks[j] = -1;
2046
dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
2047
ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
2048
} else {
2049
if (endpoints[epidx].in_ep) {
2050
if (++epidx >= MIDI_MAX_ENDPOINTS) {
2051
dev_warn(&umidi->dev->dev,
2052
"too many endpoints\n");
2053
break;
2054
}
2055
}
2056
endpoints[epidx].in_ep = usb_endpoint_num(ep);
2057
if (usb_endpoint_xfer_int(ep))
2058
endpoints[epidx].in_interval = ep->bInterval;
2059
else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
2060
endpoints[epidx].in_interval = 1;
2061
endpoints[epidx].in_cables =
2062
(1 << ms_ep->bNumEmbMIDIJack) - 1;
2063
for (j = 0; j < ms_ep->bNumEmbMIDIJack; ++j)
2064
endpoints[epidx].assoc_in_jacks[j] = ms_ep->baAssocJackID[j];
2065
for (; j < ARRAY_SIZE(endpoints[epidx].assoc_in_jacks); ++j)
2066
endpoints[epidx].assoc_in_jacks[j] = -1;
2067
dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
2068
ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
2069
}
2070
}
2071
return 0;
2072
}
2073
2074
static int roland_load_info(struct snd_kcontrol *kcontrol,
2075
struct snd_ctl_elem_info *info)
2076
{
2077
static const char *const names[] = { "High Load", "Light Load" };
2078
2079
return snd_ctl_enum_info(info, 1, 2, names);
2080
}
2081
2082
static int roland_load_get(struct snd_kcontrol *kcontrol,
2083
struct snd_ctl_elem_value *value)
2084
{
2085
value->value.enumerated.item[0] = kcontrol->private_value;
2086
return 0;
2087
}
2088
2089
static int roland_load_put(struct snd_kcontrol *kcontrol,
2090
struct snd_ctl_elem_value *value)
2091
{
2092
struct snd_usb_midi *umidi = snd_kcontrol_chip(kcontrol);
2093
int changed;
2094
2095
if (value->value.enumerated.item[0] > 1)
2096
return -EINVAL;
2097
mutex_lock(&umidi->mutex);
2098
changed = value->value.enumerated.item[0] != kcontrol->private_value;
2099
if (changed)
2100
kcontrol->private_value = value->value.enumerated.item[0];
2101
mutex_unlock(&umidi->mutex);
2102
return changed;
2103
}
2104
2105
static const struct snd_kcontrol_new roland_load_ctl = {
2106
.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2107
.name = "MIDI Input Mode",
2108
.info = roland_load_info,
2109
.get = roland_load_get,
2110
.put = roland_load_put,
2111
.private_value = 1,
2112
};
2113
2114
/*
2115
* On Roland devices, use the second alternate setting to be able to use
2116
* the interrupt input endpoint.
2117
*/
2118
static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi *umidi)
2119
{
2120
struct usb_interface *intf;
2121
struct usb_host_interface *hostif;
2122
struct usb_interface_descriptor *intfd;
2123
2124
intf = umidi->iface;
2125
if (!intf || intf->num_altsetting != 2)
2126
return;
2127
2128
hostif = &intf->altsetting[1];
2129
intfd = get_iface_desc(hostif);
2130
/* If either or both of the endpoints support interrupt transfer,
2131
* then use the alternate setting
2132
*/
2133
if (intfd->bNumEndpoints != 2 ||
2134
!((get_endpoint(hostif, 0)->bmAttributes &
2135
USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT ||
2136
(get_endpoint(hostif, 1)->bmAttributes &
2137
USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT))
2138
return;
2139
2140
dev_dbg(&umidi->dev->dev, "switching to altsetting %d with int ep\n",
2141
intfd->bAlternateSetting);
2142
usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
2143
intfd->bAlternateSetting);
2144
2145
umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
2146
if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
2147
umidi->roland_load_ctl = NULL;
2148
}
2149
2150
/*
2151
* Try to find any usable endpoints in the interface.
2152
*/
2153
static int snd_usbmidi_detect_endpoints(struct snd_usb_midi *umidi,
2154
struct snd_usb_midi_endpoint_info *endpoint,
2155
int max_endpoints)
2156
{
2157
struct usb_interface *intf;
2158
struct usb_host_interface *hostif;
2159
struct usb_interface_descriptor *intfd;
2160
struct usb_endpoint_descriptor *epd;
2161
int i, out_eps = 0, in_eps = 0;
2162
2163
if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
2164
snd_usbmidi_switch_roland_altsetting(umidi);
2165
2166
if (endpoint[0].out_ep || endpoint[0].in_ep)
2167
return 0;
2168
2169
intf = umidi->iface;
2170
if (!intf || intf->num_altsetting < 1)
2171
return -ENOENT;
2172
hostif = intf->cur_altsetting;
2173
intfd = get_iface_desc(hostif);
2174
2175
for (i = 0; i < intfd->bNumEndpoints; ++i) {
2176
epd = get_endpoint(hostif, i);
2177
if (!usb_endpoint_xfer_bulk(epd) &&
2178
!usb_endpoint_xfer_int(epd))
2179
continue;
2180
if (out_eps < max_endpoints &&
2181
usb_endpoint_dir_out(epd)) {
2182
endpoint[out_eps].out_ep = usb_endpoint_num(epd);
2183
if (usb_endpoint_xfer_int(epd))
2184
endpoint[out_eps].out_interval = epd->bInterval;
2185
++out_eps;
2186
}
2187
if (in_eps < max_endpoints &&
2188
usb_endpoint_dir_in(epd)) {
2189
endpoint[in_eps].in_ep = usb_endpoint_num(epd);
2190
if (usb_endpoint_xfer_int(epd))
2191
endpoint[in_eps].in_interval = epd->bInterval;
2192
++in_eps;
2193
}
2194
}
2195
return (out_eps || in_eps) ? 0 : -ENOENT;
2196
}
2197
2198
/*
2199
* Detects the endpoints for one-port-per-endpoint protocols.
2200
*/
2201
static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi *umidi,
2202
struct snd_usb_midi_endpoint_info *endpoints)
2203
{
2204
int err, i;
2205
2206
err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
2207
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2208
if (endpoints[i].out_ep)
2209
endpoints[i].out_cables = 0x0001;
2210
if (endpoints[i].in_ep)
2211
endpoints[i].in_cables = 0x0001;
2212
}
2213
return err;
2214
}
2215
2216
/*
2217
* Detects the endpoints and ports of Yamaha devices.
2218
*/
2219
static int snd_usbmidi_detect_yamaha(struct snd_usb_midi *umidi,
2220
struct snd_usb_midi_endpoint_info *endpoint)
2221
{
2222
struct usb_interface *intf;
2223
struct usb_host_interface *hostif;
2224
struct usb_interface_descriptor *intfd;
2225
uint8_t *cs_desc;
2226
2227
intf = umidi->iface;
2228
if (!intf)
2229
return -ENOENT;
2230
hostif = intf->altsetting;
2231
intfd = get_iface_desc(hostif);
2232
if (intfd->bNumEndpoints < 1)
2233
return -ENOENT;
2234
2235
/*
2236
* For each port there is one MIDI_IN/OUT_JACK descriptor, not
2237
* necessarily with any useful contents. So simply count 'em.
2238
*/
2239
for (cs_desc = hostif->extra;
2240
cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
2241
cs_desc += cs_desc[0]) {
2242
if (cs_desc[1] == USB_DT_CS_INTERFACE) {
2243
if (cs_desc[2] == UAC_MIDI_IN_JACK)
2244
endpoint->in_cables =
2245
(endpoint->in_cables << 1) | 1;
2246
else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
2247
endpoint->out_cables =
2248
(endpoint->out_cables << 1) | 1;
2249
}
2250
}
2251
if (!endpoint->in_cables && !endpoint->out_cables)
2252
return -ENOENT;
2253
2254
return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
2255
}
2256
2257
/*
2258
* Detects the endpoints and ports of Roland devices.
2259
*/
2260
static int snd_usbmidi_detect_roland(struct snd_usb_midi *umidi,
2261
struct snd_usb_midi_endpoint_info *endpoint)
2262
{
2263
struct usb_interface *intf;
2264
struct usb_host_interface *hostif;
2265
u8 *cs_desc;
2266
2267
intf = umidi->iface;
2268
if (!intf)
2269
return -ENOENT;
2270
hostif = intf->altsetting;
2271
/*
2272
* Some devices have a descriptor <06 24 F1 02 <inputs> <outputs>>,
2273
* some have standard class descriptors, or both kinds, or neither.
2274
*/
2275
for (cs_desc = hostif->extra;
2276
cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
2277
cs_desc += cs_desc[0]) {
2278
if (cs_desc[0] >= 6 &&
2279
cs_desc[1] == USB_DT_CS_INTERFACE &&
2280
cs_desc[2] == 0xf1 &&
2281
cs_desc[3] == 0x02) {
2282
if (cs_desc[4] > 0x10 || cs_desc[5] > 0x10)
2283
continue;
2284
endpoint->in_cables = (1 << cs_desc[4]) - 1;
2285
endpoint->out_cables = (1 << cs_desc[5]) - 1;
2286
return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
2287
} else if (cs_desc[0] >= 7 &&
2288
cs_desc[1] == USB_DT_CS_INTERFACE &&
2289
cs_desc[2] == UAC_HEADER) {
2290
return snd_usbmidi_get_ms_info(umidi, endpoint);
2291
}
2292
}
2293
2294
return -ENODEV;
2295
}
2296
2297
/*
2298
* Creates the endpoints and their ports for Midiman devices.
2299
*/
2300
static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi *umidi,
2301
struct snd_usb_midi_endpoint_info *endpoint)
2302
{
2303
struct snd_usb_midi_endpoint_info ep_info;
2304
struct usb_interface *intf;
2305
struct usb_host_interface *hostif;
2306
struct usb_interface_descriptor *intfd;
2307
struct usb_endpoint_descriptor *epd;
2308
int cable, err;
2309
2310
intf = umidi->iface;
2311
if (!intf)
2312
return -ENOENT;
2313
hostif = intf->altsetting;
2314
intfd = get_iface_desc(hostif);
2315
/*
2316
* The various MidiSport devices have more or less random endpoint
2317
* numbers, so we have to identify the endpoints by their index in
2318
* the descriptor array, like the driver for that other OS does.
2319
*
2320
* There is one interrupt input endpoint for all input ports, one
2321
* bulk output endpoint for even-numbered ports, and one for odd-
2322
* numbered ports. Both bulk output endpoints have corresponding
2323
* input bulk endpoints (at indices 1 and 3) which aren't used.
2324
*/
2325
if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
2326
dev_dbg(&umidi->dev->dev, "not enough endpoints\n");
2327
return -ENOENT;
2328
}
2329
2330
epd = get_endpoint(hostif, 0);
2331
if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
2332
dev_dbg(&umidi->dev->dev, "endpoint[0] isn't interrupt\n");
2333
return -ENXIO;
2334
}
2335
epd = get_endpoint(hostif, 2);
2336
if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
2337
dev_dbg(&umidi->dev->dev, "endpoint[2] isn't bulk output\n");
2338
return -ENXIO;
2339
}
2340
if (endpoint->out_cables > 0x0001) {
2341
epd = get_endpoint(hostif, 4);
2342
if (!usb_endpoint_dir_out(epd) ||
2343
!usb_endpoint_xfer_bulk(epd)) {
2344
dev_dbg(&umidi->dev->dev,
2345
"endpoint[4] isn't bulk output\n");
2346
return -ENXIO;
2347
}
2348
}
2349
2350
ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress &
2351
USB_ENDPOINT_NUMBER_MASK;
2352
ep_info.out_interval = 0;
2353
ep_info.out_cables = endpoint->out_cables & 0x5555;
2354
err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
2355
&umidi->endpoints[0]);
2356
if (err < 0)
2357
return err;
2358
2359
ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress &
2360
USB_ENDPOINT_NUMBER_MASK;
2361
ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
2362
ep_info.in_cables = endpoint->in_cables;
2363
err = snd_usbmidi_in_endpoint_create(umidi, &ep_info,
2364
&umidi->endpoints[0]);
2365
if (err < 0)
2366
return err;
2367
2368
if (endpoint->out_cables > 0x0001) {
2369
ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress &
2370
USB_ENDPOINT_NUMBER_MASK;
2371
ep_info.out_cables = endpoint->out_cables & 0xaaaa;
2372
err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
2373
&umidi->endpoints[1]);
2374
if (err < 0)
2375
return err;
2376
}
2377
2378
for (cable = 0; cable < 0x10; ++cable) {
2379
if (endpoint->out_cables & (1 << cable))
2380
snd_usbmidi_init_substream(umidi,
2381
SNDRV_RAWMIDI_STREAM_OUTPUT,
2382
cable,
2383
-1 /* prevent trying to find jack */,
2384
&umidi->endpoints[cable & 1].out->ports[cable].substream);
2385
if (endpoint->in_cables & (1 << cable))
2386
snd_usbmidi_init_substream(umidi,
2387
SNDRV_RAWMIDI_STREAM_INPUT,
2388
cable,
2389
-1 /* prevent trying to find jack */,
2390
&umidi->endpoints[0].in->ports[cable].substream);
2391
}
2392
return 0;
2393
}
2394
2395
static const struct snd_rawmidi_global_ops snd_usbmidi_ops = {
2396
.get_port_info = snd_usbmidi_get_port_info,
2397
};
2398
2399
static int snd_usbmidi_create_rawmidi(struct snd_usb_midi *umidi,
2400
int out_ports, int in_ports)
2401
{
2402
struct snd_rawmidi *rmidi;
2403
int err;
2404
2405
err = snd_rawmidi_new(umidi->card, "USB MIDI",
2406
umidi->next_midi_device++,
2407
out_ports, in_ports, &rmidi);
2408
if (err < 0)
2409
return err;
2410
strscpy(rmidi->name, umidi->card->shortname);
2411
rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2412
SNDRV_RAWMIDI_INFO_INPUT |
2413
SNDRV_RAWMIDI_INFO_DUPLEX;
2414
rmidi->ops = &snd_usbmidi_ops;
2415
rmidi->private_data = umidi;
2416
rmidi->private_free = snd_usbmidi_rawmidi_free;
2417
snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
2418
&snd_usbmidi_output_ops);
2419
snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
2420
&snd_usbmidi_input_ops);
2421
2422
umidi->rmidi = rmidi;
2423
return 0;
2424
}
2425
2426
/*
2427
* Temporarily stop input.
2428
*/
2429
void snd_usbmidi_input_stop(struct list_head *p)
2430
{
2431
struct snd_usb_midi *umidi;
2432
unsigned int i, j;
2433
2434
umidi = list_entry(p, struct snd_usb_midi, list);
2435
if (!umidi->input_running)
2436
return;
2437
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2438
struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
2439
if (ep->in)
2440
for (j = 0; j < INPUT_URBS; ++j)
2441
usb_kill_urb(ep->in->urbs[j]);
2442
}
2443
umidi->input_running = 0;
2444
}
2445
EXPORT_SYMBOL(snd_usbmidi_input_stop);
2446
2447
static void snd_usbmidi_input_start_ep(struct snd_usb_midi *umidi,
2448
struct snd_usb_midi_in_endpoint *ep)
2449
{
2450
unsigned int i;
2451
unsigned long flags;
2452
2453
if (!ep)
2454
return;
2455
for (i = 0; i < INPUT_URBS; ++i) {
2456
struct urb *urb = ep->urbs[i];
2457
spin_lock_irqsave(&umidi->disc_lock, flags);
2458
if (!atomic_read(&urb->use_count)) {
2459
urb->dev = ep->umidi->dev;
2460
snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
2461
}
2462
spin_unlock_irqrestore(&umidi->disc_lock, flags);
2463
}
2464
}
2465
2466
/*
2467
* Resume input after a call to snd_usbmidi_input_stop().
2468
*/
2469
void snd_usbmidi_input_start(struct list_head *p)
2470
{
2471
struct snd_usb_midi *umidi;
2472
int i;
2473
2474
umidi = list_entry(p, struct snd_usb_midi, list);
2475
if (umidi->input_running || !umidi->opened[1])
2476
return;
2477
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2478
snd_usbmidi_input_start_ep(umidi, umidi->endpoints[i].in);
2479
umidi->input_running = 1;
2480
}
2481
EXPORT_SYMBOL(snd_usbmidi_input_start);
2482
2483
/*
2484
* Prepare for suspend. Typically called from the USB suspend callback.
2485
*/
2486
void snd_usbmidi_suspend(struct list_head *p)
2487
{
2488
struct snd_usb_midi *umidi;
2489
2490
umidi = list_entry(p, struct snd_usb_midi, list);
2491
mutex_lock(&umidi->mutex);
2492
snd_usbmidi_input_stop(p);
2493
mutex_unlock(&umidi->mutex);
2494
}
2495
EXPORT_SYMBOL(snd_usbmidi_suspend);
2496
2497
/*
2498
* Resume. Typically called from the USB resume callback.
2499
*/
2500
void snd_usbmidi_resume(struct list_head *p)
2501
{
2502
struct snd_usb_midi *umidi;
2503
2504
umidi = list_entry(p, struct snd_usb_midi, list);
2505
mutex_lock(&umidi->mutex);
2506
snd_usbmidi_input_start(p);
2507
mutex_unlock(&umidi->mutex);
2508
}
2509
EXPORT_SYMBOL(snd_usbmidi_resume);
2510
2511
/*
2512
* Creates and registers everything needed for a MIDI streaming interface.
2513
*/
2514
int __snd_usbmidi_create(struct snd_card *card,
2515
struct usb_interface *iface,
2516
struct list_head *midi_list,
2517
const struct snd_usb_audio_quirk *quirk,
2518
unsigned int usb_id,
2519
unsigned int *num_rawmidis)
2520
{
2521
struct snd_usb_midi *umidi;
2522
struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
2523
int out_ports, in_ports;
2524
int i, err;
2525
2526
umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
2527
if (!umidi)
2528
return -ENOMEM;
2529
umidi->dev = interface_to_usbdev(iface);
2530
umidi->card = card;
2531
umidi->iface = iface;
2532
umidi->quirk = quirk;
2533
umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
2534
if (num_rawmidis)
2535
umidi->next_midi_device = *num_rawmidis;
2536
spin_lock_init(&umidi->disc_lock);
2537
init_rwsem(&umidi->disc_rwsem);
2538
mutex_init(&umidi->mutex);
2539
if (!usb_id)
2540
usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2541
le16_to_cpu(umidi->dev->descriptor.idProduct));
2542
umidi->usb_id = usb_id;
2543
timer_setup(&umidi->error_timer, snd_usbmidi_error_timer, 0);
2544
2545
/* detect the endpoint(s) to use */
2546
memset(endpoints, 0, sizeof(endpoints));
2547
switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2548
case QUIRK_MIDI_STANDARD_INTERFACE:
2549
err = snd_usbmidi_get_ms_info(umidi, endpoints);
2550
if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
2551
umidi->usb_protocol_ops =
2552
&snd_usbmidi_maudio_broken_running_status_ops;
2553
break;
2554
case QUIRK_MIDI_US122L:
2555
umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2556
fallthrough;
2557
case QUIRK_MIDI_FIXED_ENDPOINT:
2558
memcpy(&endpoints[0], quirk->data,
2559
sizeof(struct snd_usb_midi_endpoint_info));
2560
err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2561
break;
2562
case QUIRK_MIDI_YAMAHA:
2563
err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2564
break;
2565
case QUIRK_MIDI_ROLAND:
2566
err = snd_usbmidi_detect_roland(umidi, &endpoints[0]);
2567
break;
2568
case QUIRK_MIDI_MIDIMAN:
2569
umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2570
memcpy(&endpoints[0], quirk->data,
2571
sizeof(struct snd_usb_midi_endpoint_info));
2572
err = 0;
2573
break;
2574
case QUIRK_MIDI_NOVATION:
2575
umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2576
err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2577
break;
2578
case QUIRK_MIDI_RAW_BYTES:
2579
umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
2580
/*
2581
* Interface 1 contains isochronous endpoints, but with the same
2582
* numbers as in interface 0. Since it is interface 1 that the
2583
* USB core has most recently seen, these descriptors are now
2584
* associated with the endpoint numbers. This will foul up our
2585
* attempts to submit bulk/interrupt URBs to the endpoints in
2586
* interface 0, so we have to make sure that the USB core looks
2587
* again at interface 0 by calling usb_set_interface() on it.
2588
*/
2589
if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2590
usb_set_interface(umidi->dev, 0, 0);
2591
err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2592
break;
2593
case QUIRK_MIDI_EMAGIC:
2594
umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2595
memcpy(&endpoints[0], quirk->data,
2596
sizeof(struct snd_usb_midi_endpoint_info));
2597
err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2598
break;
2599
case QUIRK_MIDI_CME:
2600
umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
2601
err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2602
break;
2603
case QUIRK_MIDI_AKAI:
2604
umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2605
err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2606
/* endpoint 1 is input-only */
2607
endpoints[1].out_cables = 0;
2608
break;
2609
case QUIRK_MIDI_FTDI:
2610
umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2611
2612
/* set baud rate to 31250 (48 MHz / 16 / 96) */
2613
err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2614
3, 0x40, 0x60, 0, NULL, 0, 1000);
2615
if (err < 0)
2616
break;
2617
2618
err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2619
break;
2620
case QUIRK_MIDI_CH345:
2621
umidi->usb_protocol_ops = &snd_usbmidi_ch345_broken_sysex_ops;
2622
err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2623
break;
2624
default:
2625
dev_err(&umidi->dev->dev, "invalid quirk type %d\n",
2626
quirk->type);
2627
err = -ENXIO;
2628
break;
2629
}
2630
if (err < 0)
2631
goto free_midi;
2632
2633
/* create rawmidi device */
2634
out_ports = 0;
2635
in_ports = 0;
2636
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
2637
out_ports += hweight16(endpoints[i].out_cables);
2638
in_ports += hweight16(endpoints[i].in_cables);
2639
}
2640
err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2641
if (err < 0)
2642
goto free_midi;
2643
2644
/* create endpoint/port structures */
2645
if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2646
err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2647
else
2648
err = snd_usbmidi_create_endpoints(umidi, endpoints);
2649
if (err < 0)
2650
goto exit;
2651
2652
usb_autopm_get_interface_no_resume(umidi->iface);
2653
2654
list_add_tail(&umidi->list, midi_list);
2655
if (num_rawmidis)
2656
*num_rawmidis = umidi->next_midi_device;
2657
return 0;
2658
2659
free_midi:
2660
kfree(umidi);
2661
exit:
2662
return err;
2663
}
2664
EXPORT_SYMBOL(__snd_usbmidi_create);
2665
2666