forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.c
2510 lines (2282 loc) · 69.3 KB
/
midi.c
1
2
3
4
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* usbmidi.c - ALSA USB MIDI driver
*
* Copyright (c) 2002-2009 Clemens Ladisch
* 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>
#include <linux/timer.h>
#include <linux/usb.h>
#include <linux/wait.h>
#include <linux/usb/audio.h>
#include <linux/module.h>
#include <sound/core.h>
#include <sound/control.h>
#include <sound/rawmidi.h>
#include <sound/asequencer.h>
#include "usbaudio.h"
#include "midi.h"
#include "power.h"
#include "helper.h"
/*
* define this to log all USB packets
*/
/* #define DUMP_PACKETS */
/*
* how long to wait after some USB errors, so that hub_wq can disconnect() us
* without too many spurious errors
*/
#define ERROR_DELAY_JIFFIES (HZ / 10)
#define OUTPUT_URBS 7
#define INPUT_URBS 7
MODULE_AUTHOR("Clemens Ladisch <[email protected]>");
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[];
} __attribute__ ((packed));
struct snd_usb_midi_in_endpoint;
struct snd_usb_midi_out_endpoint;
struct snd_usb_midi_endpoint;
struct usb_protocol_ops {
void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint *);
void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint *);
};
struct snd_usb_midi {
struct usb_device *dev;
struct snd_card *card;
struct usb_interface *iface;
const struct snd_usb_audio_quirk *quirk;
struct snd_rawmidi *rmidi;
const struct usb_protocol_ops *usb_protocol_ops;
struct list_head list;
struct timer_list error_timer;
spinlock_t disc_lock;
struct rw_semaphore disc_rwsem;
struct mutex mutex;
u32 usb_id;
int next_midi_device;
struct snd_usb_midi_endpoint {
struct snd_usb_midi_out_endpoint *out;
struct snd_usb_midi_in_endpoint *in;
} endpoints[MIDI_MAX_ENDPOINTS];
unsigned long input_triggered;
unsigned int opened[2];
unsigned char disconnected;
unsigned char input_running;
struct snd_kcontrol *roland_load_ctl;
};
struct snd_usb_midi_out_endpoint {
struct snd_usb_midi *umidi;
struct out_urb_context {
struct urb *urb;
struct snd_usb_midi_out_endpoint *ep;
} urbs[OUTPUT_URBS];
unsigned int active_urbs;
unsigned int drain_urbs;
int max_transfer; /* size of urb buffer */
struct tasklet_struct tasklet;
unsigned int next_urb;
spinlock_t buffer_lock;
struct usbmidi_out_port {
struct snd_usb_midi_out_endpoint *ep;
struct snd_rawmidi_substream *substream;
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;
wait_queue_head_t drain_wait;
};
struct snd_usb_midi_in_endpoint {
struct snd_usb_midi *umidi;
struct urb *urbs[INPUT_URBS];
struct usbmidi_in_port {
struct snd_rawmidi_substream *substream;
u8 running_status_length;
} ports[0x10];
u8 seen_f5;
bool in_sysex;
u8 last_cin;
u8 error_resubmit;
int current_port;
};
static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep);
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.
*/
static int snd_usbmidi_submit_urb(struct urb *urb, gfp_t flags)
{
int err = usb_submit_urb(urb, flags);
if (err < 0 && err != -ENODEV)
dev_err(&urb->dev->dev, "usb_submit_urb: %d\n", err);
return err;
}
/*
* Error handling for URB completion functions.
*/
static int snd_usbmidi_urb_error(const struct urb *urb)
{
switch (urb->status) {
/* manually unlinked, or device gone */
case -ENOENT:
case -ECONNRESET:
case -ESHUTDOWN:
case -ENODEV:
return -ENODEV;
/* errors that might occur during unplugging */
case -EPROTO:
case -ETIME:
case -EILSEQ:
return -EIO;
default:
dev_err(&urb->dev->dev, "urb status %d\n", urb->status);
return 0; /* continue */
}
}
/*
* Receives a chunk of MIDI data.
*/
static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint *ep,
int portidx, uint8_t *data, int length)
{
struct usbmidi_in_port *port = &ep->ports[portidx];
if (!port->substream) {
dev_dbg(&ep->umidi->dev->dev, "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(KERN_CONT " %02x", *data);
printk(KERN_CONT " ]\n");
}
#else
#define dump_urb(type, data, length) /* nothing */
#endif
/*
* Processes the data read from the device.
*/
static void snd_usbmidi_in_urb_complete(struct urb *urb)
{
struct snd_usb_midi_in_endpoint *ep = urb->context;
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 {
int err = snd_usbmidi_urb_error(urb);
if (err < 0) {
if (err != -ENODEV) {
ep->error_resubmit = 1;
mod_timer(&ep->umidi->error_timer,
jiffies + ERROR_DELAY_JIFFIES);
}
return;
}
}
urb->dev = ep->umidi->dev;
snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
}
static void snd_usbmidi_out_urb_complete(struct urb *urb)
{
struct out_urb_context *context = urb->context;
struct snd_usb_midi_out_endpoint *ep = context->ep;
unsigned int urb_index;
unsigned long flags;
spin_lock_irqsave(&ep->buffer_lock, flags);
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);
}
spin_unlock_irqrestore(&ep->buffer_lock, flags);
if (urb->status < 0) {
int err = snd_usbmidi_urb_error(urb);
if (err < 0) {
if (err != -ENODEV)
mod_timer(&ep->umidi->error_timer,
jiffies + ERROR_DELAY_JIFFIES);
return;
}
}
snd_usbmidi_do_output(ep);
}
/*
* This is called when some data should be transferred to the device
* (from one or more substreams).
*/
static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep)
{
unsigned int urb_index;
struct urb *urb;
unsigned long flags;
spin_lock_irqsave(&ep->buffer_lock, flags);
if (ep->umidi->disconnected) {
spin_unlock_irqrestore(&ep->buffer_lock, flags);
return;
}
urb_index = ep->next_urb;
for (;;) {
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)
break;
dump_urb("sending", urb->transfer_buffer,
urb->transfer_buffer_length);
urb->dev = ep->umidi->dev;
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)
break;
}
ep->next_urb = urb_index;
spin_unlock_irqrestore(&ep->buffer_lock, flags);
}
static void snd_usbmidi_out_tasklet(unsigned long data)
{
struct snd_usb_midi_out_endpoint *ep =
(struct snd_usb_midi_out_endpoint *) data;
snd_usbmidi_do_output(ep);
}
/* called after transfers had been interrupted due to some USB error */
static void snd_usbmidi_error_timer(struct timer_list *t)
{
struct snd_usb_midi *umidi = from_timer(umidi, t, error_timer);
unsigned int i, j;
spin_lock(&umidi->disc_lock);
if (umidi->disconnected) {
spin_unlock(&umidi->disc_lock);
return;
}
for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
if (in && in->error_resubmit) {
in->error_resubmit = 0;
for (j = 0; j < INPUT_URBS; ++j) {
if (atomic_read(&in->urbs[j]->use_count))
continue;
in->urbs[j]->dev = umidi->dev;
snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
}
}
if (umidi->endpoints[i].out)
snd_usbmidi_do_output(umidi->endpoints[i].out);
}
spin_unlock(&umidi->disc_lock);
}
/* helper function to send static data that may not DMA-able */
static int send_bulk_static_data(struct snd_usb_midi_out_endpoint *ep,
const void *data, int len)
{
int err = 0;
void *buf = kmemdup(data, len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
dump_urb("sending", buf, len);
if (ep->urbs[0].urb)
err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
buf, len, NULL, 250);
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.
*/
static void snd_usbmidi_standard_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;
int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
snd_usbmidi_input_data(ep, cable, &buffer[i + 1],
length);
}
}
static void snd_usbmidi_midiman_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 + 3] != 0) {
int port = buffer[i + 3] >> 4;
int length = buffer[i + 3] & 3;
snd_usbmidi_input_data(ep, port, &buffer[i], length);
}
}
/*
* 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;
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);
}
}
/*
* QinHeng CH345 is buggy: every second packet inside a SysEx has not CIN 4
* but the previously seen CIN, but still with three data bytes.
*/
static void ch345_broken_sysex_input(struct snd_usb_midi_in_endpoint *ep,
uint8_t *buffer, int buffer_length)
{
unsigned int i, cin, length;
for (i = 0; i + 3 < buffer_length; i += 4) {
if (buffer[i] == 0 && i > 0)
break;
cin = buffer[i] & 0x0f;
if (ep->in_sysex &&
cin == ep->last_cin &&
(buffer[i + 1 + (cin == 0x6)] & 0x80) == 0)
cin = 0x4;
#if 0
if (buffer[i + 1] == 0x90) {
/*
* Either a corrupted running status or a real note-on
* message; impossible to detect reliably.
*/
}
#endif
length = snd_usbmidi_cin_length[cin];
snd_usbmidi_input_data(ep, 0, &buffer[i + 1], length);
ep->in_sysex = cin == 0x4;
if (!ep->in_sysex)
ep->last_cin = cin;
}
}
/*
* 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);
}
/*
* 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.
*/
static void snd_usbmidi_transmit_byte(struct usbmidi_out_port *port,
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;
}
}
}
static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint *ep,
struct urb *urb)
{
int p;
/* FIXME: lower-numbered ports can starve higher-numbered ports */
for (p = 0; p < 0x10; ++p) {
struct usbmidi_out_port *port = &ep->ports[p];
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 const 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 const struct usb_protocol_ops snd_usbmidi_midiman_ops = {
.input = snd_usbmidi_midiman_input,
.output = snd_usbmidi_standard_output,
.output_packet = snd_usbmidi_output_midiman_packet,
};
static const
struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
.input = snd_usbmidi_maudio_broken_running_status_input,
.output = snd_usbmidi_standard_output,
.output_packet = snd_usbmidi_output_standard_packet,
};
static const 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,
};
static const struct usb_protocol_ops snd_usbmidi_ch345_broken_sysex_ops = {
.input = ch345_broken_sysex_input,
.output = snd_usbmidi_standard_output,
.output_packet = snd_usbmidi_output_standard_packet,
};
/*
* 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 const struct usb_protocol_ops snd_usbmidi_akai_ops = {
.input = snd_usbmidi_akai_input,
.output = snd_usbmidi_akai_output,
};
/*
* 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.
*/
static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint *ep,
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);
}
static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint *ep,
struct urb *urb)
{
uint8_t *transfer_buffer;
int count;
if (!ep->ports[0].active)
return;
transfer_buffer = urb->transfer_buffer;
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;
urb->transfer_buffer_length = 2 + count;
}
static const struct usb_protocol_ops snd_usbmidi_novation_ops = {
.input = snd_usbmidi_novation_input,
.output = snd_usbmidi_novation_output,
};
/*
* "raw" protocol: just move raw MIDI bytes from/to the endpoint
*/
static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint *ep,
uint8_t *buffer, int buffer_length)
{
snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
}
static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint *ep,
struct urb *urb)
{
int count;
if (!ep->ports[0].active)
return;
count = snd_rawmidi_transmit(ep->ports[0].substream,
urb->transfer_buffer,
ep->max_transfer);
if (count < 1) {
ep->ports[0].active = 0;
return;
}
urb->transfer_buffer_length = count;
}
static const struct usb_protocol_ops snd_usbmidi_raw_ops = {
.input = snd_usbmidi_raw_input,
.output = snd_usbmidi_raw_output,
};
/*
* 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 const struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
.input = snd_usbmidi_ftdi_input,
.output = snd_usbmidi_raw_output,
};
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);
}
static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
struct urb *urb)
{
int count;
if (!ep->ports[0].active)
return;
switch (snd_usb_get_speed(ep->umidi->dev)) {
case USB_SPEED_HIGH:
case USB_SPEED_SUPER:
case USB_SPEED_SUPER_PLUS:
count = 1;
break;
default:
count = 2;
}
count = snd_rawmidi_transmit(ep->ports[0].substream,
urb->transfer_buffer,
count);
if (count < 1) {
ep->ports[0].active = 0;
return;
}
memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
urb->transfer_buffer_length = ep->max_transfer;
}
static const struct usb_protocol_ops snd_usbmidi_122l_ops = {
.input = snd_usbmidi_us122l_input,
.output = snd_usbmidi_us122l_output,
};
/*
* Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
*/
static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint *ep)
{
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));
}
static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint *ep)
{
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));
}
static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint *ep,
uint8_t *buffer, int buffer_length)
{
int i;
/* FF indicates end of valid data */
for (i = 0; i < buffer_length; ++i)
if (buffer[i] == 0xff) {
buffer_length = i;
break;
}
/* 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)