-
Notifications
You must be signed in to change notification settings - Fork 10
/
usb_midi_host.c
1020 lines (939 loc) · 34.6 KB
/
usb_midi_host.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2023 rppicomidi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "tusb_option.h"
#if (TUSB_OPT_HOST_ENABLED)
#include "host/usbh.h"
#include "host/usbh_pvt.h"
#include "usb_midi_host.h"
#include <stdlib.h>
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
#ifndef CFG_TUH_MAX_CABLES
#define CFG_TUH_MAX_CABLES 16
#endif
#ifndef CFG_TUH_MIDI_RX_BUFSIZE
#define CFG_TUH_MIDI_RX_BUFSIZE USBH_EPSIZE_BULK_MAX
#endif
#ifndef CFG_TUH_MIDI_TX_BUFSIZE
#define CFG_TUH_MIDI_TX_BUFSIZE USBH_EPSIZE_BULK_MAX
#endif
#ifndef CFG_TUH_MIDI_EP_BUFSIZE
#define CFG_TUH_MIDI_EP_BUFSIZE USBH_EPSIZE_BULK_MAX
#endif
#define MIDI_MAX_DATA_VAL 0x7f
static struct midih_limits_s {
size_t midi_rx_buf;
size_t midi_tx_buf;
uint8_t max_cables;
} midih_limits = {CFG_TUH_MIDI_RX_BUFSIZE, CFG_TUH_MIDI_TX_BUFSIZE, CFG_TUH_MAX_CABLES};
// This descriptor follows the standard bulk data endpoint descriptor
typedef struct
{
uint8_t bLength ; ///< Size of this descriptor in bytes (4+bNumEmbMIDIJack)
uint8_t bDescriptorType ; ///< Descriptor Type, must be CS_ENDPOINT
uint8_t bDescriptorSubType ; ///< Descriptor SubType, must be MS_GENERAL
uint8_t bNumEmbMIDIJack; ; ///< Number of embedded MIDI jacks associated with this endpoint
uint8_t baAssocJackID[]; ; ///< A list of associated jacks
} midi_cs_desc_endpoint_t;
typedef struct
{
uint8_t buffer[4];
uint8_t index;
uint8_t total;
}midi_stream_t;
typedef struct
{
uint8_t dev_addr;
uint8_t itf_num;
uint8_t ep_in; // IN endpoint address
uint8_t ep_out; // OUT endpoint address
uint16_t ep_in_max; // min( midih_limits.midi_rx_buf, wMaxPacketSize of the IN endpoint)
uint16_t ep_out_max; // min( midih_limits.midi_tx_buf, wMaxPacketSize of the OUT endpoint)
uint8_t num_cables_rx; // IN endpoint CS descriptor bNumEmbMIDIJack value
uint8_t num_cables_tx; // OUT endpoint CS descriptor bNumEmbMIDIJack value
// For Stream read()/write() API
// Messages are always 4 bytes long, queue them for reading and writing so the
// callers can use the Stream interface with single-byte read/write calls.
midi_stream_t *stream_write;
midi_stream_t stream_read;
/*------------- From this point, data is not cleared by bus reset -------------*/
// Endpoint FIFOs
tu_fifo_t rx_ff;
tu_fifo_t tx_ff;
uint8_t *rx_ff_buf;
uint8_t *tx_ff_buf;
#if CFG_FIFO_MUTEX
osal_mutex_def_t rx_ff_mutex;
osal_mutex_def_t tx_ff_mutex;
#endif
// Endpoint Transfer buffer
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUH_MIDI_EP_BUFSIZE];
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUH_MIDI_EP_BUFSIZE];
bool configured;
#if CFG_MIDI_HOST_DEVSTRINGS
#define MAX_STRING_INDICES 32
uint8_t all_string_indices[MAX_STRING_INDICES];
uint8_t num_string_indices;
#define MAX_IN_JACKS 8
#define MAX_OUT_JACKS 8
struct {
uint8_t jack_id;
uint8_t jack_type;
uint8_t string_index;
} in_jack_info[MAX_IN_JACKS];
uint8_t next_in_jack;
struct {
uint8_t jack_id;
uint8_t jack_type;
uint8_t num_source_ids;
uint8_t source_ids[MAX_IN_JACKS/4];
uint8_t string_index;
} out_jack_info[MAX_OUT_JACKS];
uint8_t next_out_jack;
uint8_t ep_in_associated_jacks[MAX_OUT_JACKS/2];
uint8_t ep_out_associated_jacks[MAX_IN_JACKS/2];
#endif
}midih_interface_t;
static midih_interface_t _midi_host[CFG_TUH_DEVICE_MAX];
static midih_interface_t *get_midi_host(uint8_t dev_addr)
{
TU_VERIFY(dev_addr >0 && dev_addr <= CFG_TUH_DEVICE_MAX);
return (_midi_host + dev_addr - 1);
}
//------------- Internal prototypes -------------//
static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi);
static void midih_freeall(void)
{
// free memory allocated by midih_init()
for (int inst = 0; inst < CFG_TUH_DEVICE_MAX; inst++)
{
midih_interface_t *p_midi_host = &_midi_host[inst];
if (p_midi_host->rx_ff_buf != NULL)
{
free (p_midi_host->rx_ff_buf);
p_midi_host->rx_ff_buf = NULL;
}
if (p_midi_host->tx_ff_buf != NULL)
{
free (p_midi_host->tx_ff_buf);
p_midi_host->tx_ff_buf = NULL;
}
if (p_midi_host->stream_write != NULL)
{
free(p_midi_host->stream_write);
p_midi_host->stream_write = NULL;
}
}
}
//--------------------------------------------------------------------+
// USBH API
//--------------------------------------------------------------------+
void tuh_midih_define_limits(size_t midi_rx_buffer_bytes, size_t midi_tx_buffer_bytes, uint8_t max_cables)
{
// prevent memory leak in case midih_init() was called before this function
midih_freeall();
midih_limits.midi_rx_buf = midi_rx_buffer_bytes;
midih_limits.midi_tx_buf = midi_tx_buffer_bytes;
midih_limits.max_cables = max_cables;
}
bool midih_init(void)
{
tu_memclr(&_midi_host, sizeof(_midi_host));
// config fifos
for (int inst = 0; inst < CFG_TUH_DEVICE_MAX; inst++)
{
midih_interface_t *p_midi_host = &_midi_host[inst];
p_midi_host->rx_ff_buf = malloc(midih_limits.midi_rx_buf);
p_midi_host->tx_ff_buf = malloc(midih_limits.midi_tx_buf);
p_midi_host->stream_write = malloc(midih_limits.max_cables * sizeof(midi_stream_t));
TU_ASSERT((p_midi_host->rx_ff_buf != NULL && p_midi_host->tx_ff_buf != NULL && p_midi_host->stream_write != NULL), 0);
tu_memclr(p_midi_host->stream_write, sizeof(*(p_midi_host->stream_write))*midih_limits.max_cables);
tu_fifo_config(&p_midi_host->rx_ff, p_midi_host->rx_ff_buf, midih_limits.midi_rx_buf, 1, false); // true, true
tu_fifo_config(&p_midi_host->tx_ff, p_midi_host->tx_ff_buf, midih_limits.midi_tx_buf, 1, false); // OBVS.
#if CFG_FIFO_MUTEX
tu_fifo_config_mutex(&p_midi_host->rx_ff, NULL, osal_mutex_create(&p_midi_host->rx_ff_mutex));
tu_fifo_config_mutex(&p_midi_host->tx_ff, osal_mutex_create(&p_midi_host->tx_ff_mutex), NULL);
#endif
}
return true;
}
bool midih_deinit()
{
midih_freeall();
return true;
}
bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
{
(void)result;
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
if ( ep_addr == p_midi_host->ep_in)
{
// receive new data if available
uint32_t packets_queued = 0;
if (xferred_bytes)
{
// put in the RX FIFO only non-zero MIDI IN 4-byte packets
uint8_t* buf = p_midi_host->epin_buf;
uint32_t npackets = xferred_bytes / 4;
uint32_t packet_num;
for (packet_num = 0; packet_num < npackets; packet_num++)
{
// some devices send back all zero packets even if there is no data ready
uint32_t packet = (uint32_t)((*buf)<<24) | ((uint32_t)(*(buf+1))<<16) | ((uint32_t)(*(buf+2))<<8) | ((uint32_t)(*(buf+3)));
if (packet != 0)
{
tu_fifo_write_n(&p_midi_host->rx_ff, buf, 4);
++packets_queued;
TU_LOG3("MIDI RX=%08lx\r\n", packet);
}
buf += 4;
}
// invoke receive callback if available
if (tuh_midi_rx_cb && packets_queued)
{
tuh_midi_rx_cb(dev_addr, packets_queued);
}
}
TU_LOG2("Requesting poll IN endpoint %d\r\n", p_midi_host->ep_in);
TU_ASSERT(usbh_edpt_xfer(p_midi_host->dev_addr, p_midi_host->ep_in, p_midi_host->epin_buf, p_midi_host->ep_in_max), 0);
}
else if ( ep_addr == p_midi_host->ep_out )
{
if (0 == write_flush(dev_addr, p_midi_host))
{
// If there is no data left, a ZLP should be sent if
// xferred_bytes is multiple of EP size and not zero
if ( !tu_fifo_count(&p_midi_host->tx_ff) && xferred_bytes && (0 == (xferred_bytes % p_midi_host->ep_out_max)) )
{
if ( usbh_edpt_claim(dev_addr, p_midi_host->ep_out) )
{
TU_ASSERT(usbh_edpt_xfer(dev_addr, p_midi_host->ep_out, XFER_RESULT_SUCCESS, 0));
}
}
}
if (tuh_midi_tx_cb)
{
tuh_midi_tx_cb(dev_addr);
}
}
return true;
}
void midih_close(uint8_t dev_addr)
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
if (p_midi_host == NULL)
return;
if (tuh_midi_umount_cb)
tuh_midi_umount_cb(dev_addr, 0);
tu_fifo_clear(&p_midi_host->rx_ff);
tu_fifo_clear(&p_midi_host->tx_ff);
p_midi_host->ep_in = 0;
p_midi_host->ep_in_max = 0;
p_midi_host->ep_out = 0;
p_midi_host->ep_out_max = 0;
p_midi_host->itf_num = 0;
p_midi_host->num_cables_rx = 0;
p_midi_host->num_cables_tx = 0;
p_midi_host->dev_addr = 255; // invalid
p_midi_host->configured = false;
tu_memclr(&p_midi_host->stream_read, sizeof(p_midi_host->stream_read));
tu_memclr(p_midi_host->stream_write, sizeof(p_midi_host->stream_write)*midih_limits.max_cables);
}
//--------------------------------------------------------------------+
// Enumeration
//--------------------------------------------------------------------+
bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len)
{
(void) rhport;
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
#if CFG_MIDI_HOST_DEVSTRINGS
p_midi_host->num_string_indices = 0;
#endif
TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass);
// There can be just a MIDI interface or an audio and a MIDI interface. Only open the MIDI interface
uint8_t const *p_desc = (uint8_t const *) desc_itf;
uint16_t len_parsed = 0;
if (AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass)
{
#if CFG_MIDI_HOST_DEVSTRINGS
// Keep track of any string descriptor that might be here
if (desc_itf->iInterface != 0)
p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface;
#endif
// This driver does not support audio streaming. However, if this is the audio control interface
// there might be a MIDI interface following it. Search through every descriptor until a MIDI
// interface is found or the end of the descriptor is found
while (len_parsed < max_len && (desc_itf->bInterfaceClass != TUSB_CLASS_AUDIO || desc_itf->bInterfaceSubClass != AUDIO_SUBCLASS_MIDI_STREAMING))
{
len_parsed += desc_itf->bLength;
p_desc = tu_desc_next(p_desc);
desc_itf = (tusb_desc_interface_t const *)p_desc;
}
TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass);
}
TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf->bInterfaceSubClass);
len_parsed += desc_itf->bLength;
#if CFG_MIDI_HOST_DEVSTRINGS
// Keep track of any string descriptor that might be here
if (desc_itf->iInterface != 0)
p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface;
#endif
p_desc = tu_desc_next(p_desc);
TU_LOG1("MIDI opening Interface %u (addr = %u)\r\n", desc_itf->bInterfaceNumber, dev_addr);
// Find out if getting the MIDI class specific interface header or an endpoint descriptor
// or a class-specific endpoint descriptor
// Jack descriptors or element descriptors must follow the cs interface header,
// but this driver does not support devices that contain element descriptors
// assume it is an interface header
midi_desc_header_t const *p_mdh = (midi_desc_header_t const *)p_desc;
TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE && p_mdh->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) ||
(p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) ||
p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT);
uint8_t prev_ep_addr = 0; // the CS endpoint descriptor is associated with the previous endpoint descrptor
p_midi_host->itf_num = desc_itf->bInterfaceNumber;
tusb_desc_endpoint_t const* in_desc = NULL;
tusb_desc_endpoint_t const* out_desc = NULL;
while (len_parsed < max_len)
{
TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) ||
(p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) ||
p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT);
if (p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) {
// The USB host doesn't really need this information unless it uses
// the string descriptor for a jack or Element
// assume it is an input jack
midi_desc_in_jack_t const *p_mdij = (midi_desc_in_jack_t const *)p_desc;
if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER)
{
TU_LOG2("Found MIDI Interface Header\r\b");
}
else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_IN_JACK)
{
// Then it is an in jack.
TU_LOG2("Found in jack\r\n");
#if CFG_MIDI_HOST_DEVSTRINGS
if (p_midi_host->next_in_jack < MAX_IN_JACKS)
{
p_midi_host->in_jack_info[p_midi_host->next_in_jack].jack_id = p_mdij->bJackID;
p_midi_host->in_jack_info[p_midi_host->next_in_jack].jack_type = p_mdij->bJackType;
p_midi_host->in_jack_info[p_midi_host->next_in_jack].string_index = p_mdij->iJack;
++p_midi_host->next_in_jack;
// Keep track of any string descriptor that might be here
if (p_mdij->iJack != 0)
p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = p_mdij->iJack;
}
#endif
}
else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_OUT_JACK)
{
// then it is an out jack
TU_LOG2("Found out jack\r\n");
#if CFG_MIDI_HOST_DEVSTRINGS
if (p_midi_host->next_out_jack < MAX_OUT_JACKS)
{
midi_desc_out_jack_t const *p_mdoj = (midi_desc_out_jack_t const *)p_desc;
p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_id = p_mdoj->bJackID;
p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_type = p_mdoj->bJackType;
p_midi_host->out_jack_info[p_midi_host->next_out_jack].num_source_ids = p_mdoj->bNrInputPins;
const struct associated_jack_s {
uint8_t id;
uint8_t pin;
} *associated_jack = (const struct associated_jack_s *)(p_desc+6);
int jack;
for (jack = 0; jack < p_mdoj->bNrInputPins; jack++)
{
p_midi_host->out_jack_info[p_midi_host->next_out_jack].source_ids[jack] = associated_jack->id;
}
p_midi_host->out_jack_info[p_midi_host->next_out_jack].string_index = *(p_desc+6+p_mdoj->bNrInputPins*2);
++p_midi_host->next_out_jack;
if (p_mdoj->iJack != 0)
p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = p_mdoj->iJack;
}
#endif
}
else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_ELEMENT)
{
// the it is an element;
#if CFG_MIDI_HOST_DEVSTRINGS
TU_LOG1("Found element; strings not supported\r\n");
#else
TU_LOG2("Found element\r\n");
#endif
}
else
{
TU_LOG2("Unknown CS Interface sub-type %u\r\n", p_mdij->bDescriptorSubType);
TU_VERIFY(false); // unknown CS Interface sub-type
}
len_parsed += p_mdij->bLength;
}
else if (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT)
{
TU_LOG2("found CS_ENDPOINT Descriptor for %02x\r\n", prev_ep_addr);
TU_VERIFY(prev_ep_addr != 0);
// parse out the mapping between the device's embedded jacks and the endpoints
// Each embedded IN jack is assocated with an OUT endpoint
midi_cs_desc_endpoint_t const* p_csep = (midi_cs_desc_endpoint_t const*)p_mdh;
if (tu_edpt_dir(prev_ep_addr) == TUSB_DIR_OUT)
{
TU_VERIFY(p_midi_host->ep_out == prev_ep_addr);
TU_VERIFY(p_midi_host->num_cables_tx == 0);
p_midi_host->num_cables_tx = p_csep->bNumEmbMIDIJack;
#if CFG_MIDI_HOST_DEVSTRINGS
uint8_t jack;
uint8_t max_jack = p_midi_host->num_cables_tx;
if (max_jack > sizeof(p_midi_host->ep_out_associated_jacks))
{
max_jack = sizeof(p_midi_host->ep_out_associated_jacks);
}
for (jack = 0; jack < max_jack; jack++)
{
p_midi_host->ep_out_associated_jacks[jack] = p_csep->baAssocJackID[jack];
}
#endif
}
else
{
TU_VERIFY(p_midi_host->ep_in == prev_ep_addr);
TU_VERIFY(p_midi_host->num_cables_rx == 0);
p_midi_host->num_cables_rx = p_csep->bNumEmbMIDIJack;
#if CFG_MIDI_HOST_DEVSTRINGS
uint8_t jack;
uint8_t max_jack = p_midi_host->num_cables_rx;
if (max_jack > sizeof(p_midi_host->ep_in_associated_jacks))
{
max_jack = sizeof(p_midi_host->ep_in_associated_jacks);
}
for (jack = 0; jack < max_jack; jack++)
{
p_midi_host->ep_in_associated_jacks[jack] = p_csep->baAssocJackID[jack];
}
#endif
}
len_parsed += p_csep->bLength;
prev_ep_addr = 0;
}
else if (p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT) {
// parse out the bulk endpoint info
tusb_desc_endpoint_t *p_ep = (tusb_desc_endpoint_t *)p_mdh;
TU_LOG2("found ENDPOINT Descriptor %02x\r\n", p_ep->bEndpointAddress);
if (p_ep->wMaxPacketSize > USBH_EPSIZE_BULK_MAX) {
TU_LOG2("ENDPOINT %02x wMaxPacketSize shorted from %u to %u\r\n", p_ep->bEndpointAddress, p_ep->wMaxPacketSize, USBH_EPSIZE_BULK_MAX);
p_ep->wMaxPacketSize = USBH_EPSIZE_BULK_MAX;
}
if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT)
{
TU_VERIFY(p_midi_host->ep_out == 0);
TU_VERIFY(p_midi_host->num_cables_tx == 0);
p_midi_host->ep_out = p_ep->bEndpointAddress;
p_midi_host->ep_out_max = p_ep->wMaxPacketSize;
if (p_midi_host->ep_out_max > midih_limits.midi_tx_buf)
p_midi_host->ep_out_max = midih_limits.midi_tx_buf;
prev_ep_addr = p_midi_host->ep_out;
out_desc = p_ep;
}
else
{
TU_VERIFY(p_midi_host->ep_in == 0);
TU_VERIFY(p_midi_host->num_cables_rx == 0);
p_midi_host->ep_in = p_ep->bEndpointAddress;
p_midi_host->ep_in_max = p_ep->wMaxPacketSize;
if (p_midi_host->ep_in_max > midih_limits.midi_rx_buf)
p_midi_host->ep_in_max = midih_limits.midi_rx_buf;
prev_ep_addr = p_midi_host->ep_in;
in_desc = p_ep;
}
len_parsed += p_mdh->bLength;
}
p_desc = tu_desc_next(p_desc);
p_mdh = (midi_desc_header_t const *)p_desc;
}
TU_VERIFY((p_midi_host->ep_out != 0 && p_midi_host->num_cables_tx != 0) ||
(p_midi_host->ep_in != 0 && p_midi_host->num_cables_rx != 0));
TU_LOG1("MIDI descriptor parsed successfully\r\n");
#if CFG_MIDI_HOST_DEVSTRINGS
// remove duplicate string indices
for (int idx=0; idx < p_midi_host->num_string_indices; idx++) {
for (int jdx = idx+1; jdx < p_midi_host->num_string_indices; jdx++) {
while (jdx < p_midi_host->num_string_indices && p_midi_host->all_string_indices[idx] == p_midi_host->all_string_indices[jdx]) {
// delete the duplicate by overwriting it with the last entry and reducing the number of entries by 1
p_midi_host->all_string_indices[jdx] = p_midi_host->all_string_indices[p_midi_host->num_string_indices-1];
--p_midi_host->num_string_indices;
}
}
}
#endif
if (in_desc)
{
TU_ASSERT(tuh_edpt_open(dev_addr, in_desc));
// Some devices always return exactly the request length so transfers won't complete
// unless you assume every transfer is the last one.
// TODO usbh_edpt_force_last_buffer(dev_addr, p_midi_host->ep_in, true);
}
if (out_desc)
{
TU_ASSERT(tuh_edpt_open(dev_addr, out_desc));
}
p_midi_host->dev_addr = dev_addr;
return true;
}
bool tuh_midi_configured(uint8_t dev_addr)
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
return p_midi_host->configured;
}
bool midih_set_config(uint8_t dev_addr, uint8_t itf_num)
{
(void) itf_num;
TU_LOG2("Set config dev_addr=%u\r\n", dev_addr);
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
p_midi_host->configured = true;
TU_LOG2("Requesting poll IN endpoint %d\r\n", p_midi_host->ep_in);
TU_ASSERT(usbh_edpt_xfer(p_midi_host->dev_addr, p_midi_host->ep_in, p_midi_host->epin_buf, p_midi_host->ep_in_max), 0);
if (tuh_midi_mount_cb)
{
tuh_midi_mount_cb(dev_addr, p_midi_host->ep_in, p_midi_host->ep_out, p_midi_host->num_cables_rx, p_midi_host->num_cables_tx);
}
usbh_driver_set_config_complete(dev_addr, itf_num);
return true;
}
//--------------------------------------------------------------------+
// Stream API
//--------------------------------------------------------------------+
static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi)
{
// No data to send
if ( !tu_fifo_count(&midi->tx_ff) ) return 0;
// skip if previous transfer not complete
TU_VERIFY( usbh_edpt_claim(dev_addr, midi->ep_out) );
uint16_t count = tu_fifo_read_n(&midi->tx_ff, midi->epout_buf, midi->ep_out_max);
if (count)
{
TU_ASSERT( usbh_edpt_xfer(dev_addr, midi->ep_out, midi->epout_buf, count), 0 );
return count;
}else
{
// Release endpoint since we don't make any transfer
usbh_edpt_release(dev_addr, midi->ep_out);
return 0;
}
}
bool tuh_midi_can_write_stream (uint8_t dev_addr)
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
return (tu_fifo_remaining(&p_midi_host->tx_ff) >= 4);
}
uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t const* buffer, uint32_t bufsize)
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
TU_VERIFY(cable_num < p_midi_host->num_cables_tx);
TU_VERIFY(cable_num < midih_limits.max_cables);
midi_stream_t *stream = &p_midi_host->stream_write[cable_num];
uint32_t i = 0;
uint8_t const CN_ = cable_num << 4;
while ( (i < bufsize) && (tu_fifo_remaining(&p_midi_host->tx_ff) >= 4) )
{
uint8_t const data = buffer[i];
i++;
if (data >= MIDI_STATUS_SYSREAL_TIMING_CLOCK)
{
// real-time messages need to be sent right away
midi_stream_t streamrt;
streamrt.buffer[0] = CN_ + MIDI_CIN_1BYTE_DATA;
streamrt.buffer[1] = data;
streamrt.buffer[2] = 0;
streamrt.buffer[3] = 0;
uint16_t const count = tu_fifo_write_n(&p_midi_host->tx_ff, streamrt.buffer, 4);
// FIFO overflown, since we already check fifo remaining. It is probably race condition
TU_ASSERT(count == 4, i);
}
else if ( stream->index == 0 )
{
//------------- New event packet -------------//
uint8_t const msg = data >> 4;
uint8_t const _msg = (stream->buffer[0]) & 0x0F;
stream->index = 2;
//stream->buffer[1] = data; //first check if its a running status byte, then update
stream->total = 4;
// Check to see if we're still in a SysEx transmit.
if ( _msg == MIDI_CIN_SYSEX_START)
{
stream->buffer[1] = data;
if ( data == MIDI_STATUS_SYSEX_END )
{
stream->buffer[0] = CN_ + MIDI_CIN_SYSEX_END_1BYTE;
stream->total = 2;
}
}
else if (msg < 0x8 && _msg >= 0x8 && _msg < 0xF) //Running Status ?
{
//stream->buffer[0] leave;
//stream->buffer[1] leave;
stream->buffer[2] = data;
if (_msg < 0xC || _msg == 0xE)
{
stream->index = 3;
}
else //if (_msg < 0xF)
{
stream->index = 3;
stream->total = 3;
}
}
else if ( (msg >= 0x8 && msg <= 0xB) || msg == 0xE )
{
// Channel Voice Messages
stream->buffer[1] = data;
stream->buffer[0] = CN_ + msg;
}
else if ( msg == 0xC || msg == 0xD)
{
// Channel Voice Messages, two-byte variants (Program Change and Channel Pressure)
stream->buffer[1] = data;
stream->buffer[0] = CN_ + msg;
stream->total = 3;
}
else if ( msg == 0xf )
{
// System message
stream->buffer[1] = data;
if ( data == MIDI_STATUS_SYSEX_START )
{
stream->buffer[0] = CN_ + MIDI_CIN_SYSEX_START;
}
else if ( data == MIDI_STATUS_SYSCOM_TIME_CODE_QUARTER_FRAME || data == MIDI_STATUS_SYSCOM_SONG_SELECT )
{
stream->buffer[0] = CN_ + MIDI_CIN_SYSCOM_2BYTE;
stream->total = 3;
}
else if ( data == MIDI_STATUS_SYSCOM_SONG_POSITION_POINTER )
{
stream->buffer[0] = CN_ + MIDI_CIN_SYSCOM_3BYTE;
}
else //for example, MIDI_STATUS_SYSCOM_TUNE_REQUEST
{
stream->buffer[0] = CN_ + MIDI_CIN_1BYTE_DATA;
stream->total = 2;
}
}
else
{
// Pack individual bytes if we don't support packing them into words.
stream->buffer[1] = data;
stream->buffer[0] = CN_ + 0xF;
stream->index = 2;
stream->total = 2;
}
} //End of: if (stream->index == 0)
else
{
//------------- On-going (buffering) packet -------------//
TU_ASSERT(stream->index < 4, i);
stream->buffer[stream->index] = data;
stream->index++;
// See if this byte ends a SysEx.
if ( stream->buffer[0] == CN_ + MIDI_CIN_SYSEX_START && data == MIDI_STATUS_SYSEX_END )
{
stream->buffer[0] = CN_ + MIDI_CIN_SYSEX_START + (stream->index - 1); //END +1/+2/+3 Bytes
stream->total = stream->index;
}
}
// Send out packet
if ( stream->index >= 2 && stream->index >= stream->total )
{
//zeroes unused bytes
for(uint8_t idx = stream->total; idx < 4; idx++) stream->buffer[idx] = 0;
TU_LOG3_MEM(stream->buffer, 4, 2);
uint16_t const count = tu_fifo_write_n(&p_midi_host->tx_ff, stream->buffer, 4);
stream->index = 0;
// FIFO overflown, since we already check fifo remaining. It is probably race condition
TU_ASSERT(count == 4, i);
}
}
return i;
}
bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4])
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
if (tu_fifo_remaining(&p_midi_host->tx_ff) < 4)
{
return false;
}
tu_fifo_write_n(&p_midi_host->tx_ff, packet, 4);
return true;
}
uint32_t tuh_midi_stream_flush( uint8_t dev_addr )
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
uint32_t bytes_flushed = 0;
if (!usbh_edpt_busy(p_midi_host->dev_addr, p_midi_host->ep_out))
{
bytes_flushed = write_flush(dev_addr, p_midi_host);
}
return bytes_flushed;
}
//--------------------------------------------------------------------+
// Helper
//--------------------------------------------------------------------+
uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr)
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
TU_VERIFY(p_midi_host->ep_out != 0); // returns 0 if fails
return p_midi_host->num_cables_tx;
}
uint8_t tuh_midih_get_num_rx_cables (uint8_t dev_addr)
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
TU_VERIFY(p_midi_host->ep_in != 0); // returns 0 if fails
return p_midi_host->num_cables_rx;
}
bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4])
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
TU_VERIFY(tu_fifo_count(&p_midi_host->rx_ff) >= 4);
return tu_fifo_read_n(&p_midi_host->rx_ff, packet, 4) == 4;
}
uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize)
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
uint32_t bytes_buffered = 0;
TU_ASSERT(p_cable_num);
TU_ASSERT(p_buffer);
TU_ASSERT(bufsize);
uint8_t one_byte;
if (!tu_fifo_peek(&p_midi_host->rx_ff, &one_byte))
{
return 0;
}
*p_cable_num = (one_byte >> 4) & 0xf;
uint32_t nread = tu_fifo_read_n(&p_midi_host->rx_ff, p_midi_host->stream_read.buffer, 4);
static uint16_t cable_sysex_in_progress; // bit i is set if received MIDI_STATUS_SYSEX_START but not MIDI_STATUS_SYSEX_END
while (nread == 4 && bytes_buffered < bufsize)
{
*p_cable_num=(p_midi_host->stream_read.buffer[0] >> 4) & 0x0f;
uint8_t bytes_to_add_to_stream = 0;
if (*p_cable_num < p_midi_host->num_cables_rx)
{
// ignore the CIN field; too many devices out there encode this wrong
uint8_t status = p_midi_host->stream_read.buffer[1];
uint16_t cable_mask = (uint16_t) (1 << *p_cable_num);
if (status <= MIDI_MAX_DATA_VAL || status == MIDI_STATUS_SYSEX_START)
{
if (status == MIDI_STATUS_SYSEX_START)
{
cable_sysex_in_progress |= cable_mask;
}
// only add the packet if a sysex message is in progress
if (cable_sysex_in_progress & cable_mask)
{
++bytes_to_add_to_stream;
uint8_t idx;
for (idx = 2; idx < 4; idx++)
{
if (p_midi_host->stream_read.buffer[idx] <= MIDI_MAX_DATA_VAL)
{
++bytes_to_add_to_stream;
}
else if (p_midi_host->stream_read.buffer[idx] == MIDI_STATUS_SYSEX_END)
{
++bytes_to_add_to_stream;
cable_sysex_in_progress &= (uint16_t) ~cable_mask;
idx = 4; // force the loop to exit; I hate break statements in loops
}
}
}
}
else if (status < MIDI_STATUS_SYSEX_START)
{
// then it is a channel message either three bytes or two
uint8_t fake_cin = (status & 0xf0) >> 4;
switch (fake_cin)
{
case MIDI_CIN_NOTE_OFF:
case MIDI_CIN_NOTE_ON:
case MIDI_CIN_POLY_KEYPRESS:
case MIDI_CIN_CONTROL_CHANGE:
case MIDI_CIN_PITCH_BEND_CHANGE:
bytes_to_add_to_stream = 3;
break;
case MIDI_CIN_PROGRAM_CHANGE:
case MIDI_CIN_CHANNEL_PRESSURE:
bytes_to_add_to_stream = 2;
break;
default:
break; // Should not get this
}
cable_sysex_in_progress &= (uint16_t)~cable_mask;
}
else if (status < MIDI_STATUS_SYSREAL_TIMING_CLOCK)
{
switch (status)
{
case MIDI_STATUS_SYSCOM_TIME_CODE_QUARTER_FRAME:
case MIDI_STATUS_SYSCOM_SONG_SELECT:
bytes_to_add_to_stream = 2;
break;
case MIDI_STATUS_SYSCOM_SONG_POSITION_POINTER:
bytes_to_add_to_stream = 3;
break;
case MIDI_STATUS_SYSCOM_TUNE_REQUEST:
case MIDI_STATUS_SYSEX_END:
bytes_to_add_to_stream = 1;
break;
default:
break;
cable_sysex_in_progress &= (uint16_t)~cable_mask;
}
}
else
{
// Real-time message: can be inserted into a sysex message,
// so do don't clear cable_sysex_in_progress bit
bytes_to_add_to_stream = 1;
}
}
uint8_t idx;
for (idx = 1; idx <= bytes_to_add_to_stream; idx++)
{
*p_buffer++ = p_midi_host->stream_read.buffer[idx];
}
bytes_buffered += bytes_to_add_to_stream;
nread = 0;
if (tu_fifo_peek(&p_midi_host->rx_ff, &one_byte))
{
uint8_t new_cable = (one_byte >> 4) & 0xf;
if (new_cable == *p_cable_num)
{
// still on the same cable. Continue reading the stream
nread = tu_fifo_read_n(&p_midi_host->rx_ff, p_midi_host->stream_read.buffer, 4);
}
}
}
return bytes_buffered;
}
uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr)
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
uint8_t num_cables = 0;
if (p_midi_host)
{
num_cables = p_midi_host->num_cables_rx;
}
return num_cables;
}
uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr)
{
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
uint8_t num_cables = 0;
if (p_midi_host)
{
num_cables = p_midi_host->num_cables_tx;
}
return num_cables;
}
#if CFG_MIDI_HOST_DEVSTRINGS
static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id)
{
uint8_t index = 0;
uint8_t assoc;
for (assoc = 0; index == 0 && assoc < ptr->next_in_jack; assoc++)
{
if (jack_id == ptr->in_jack_info[assoc].jack_id)
{
index = ptr->in_jack_info[assoc].string_index;
}
}
for (assoc = 0; index == 0 && assoc < ptr->next_out_jack; assoc++)
{
if (jack_id == ptr->out_jack_info[assoc].jack_id)
{
index = ptr->out_jack_info[assoc].string_index;
}
}
return index;
}
#endif
#if CFG_MIDI_HOST_DEVSTRINGS
uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings)
{
uint8_t nstrings = 0;
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
nstrings = p_midi_host->num_cables_rx;
if (nstrings > max_istrings)
{
nstrings = max_istrings;
}
uint8_t jack;
for (jack=0; jack<nstrings; jack++)
{
uint8_t jack_id = p_midi_host->ep_in_associated_jacks[jack];
istrings[jack] = find_string_index(p_midi_host, jack_id);
}
return nstrings;
}
uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings)
{
uint8_t nstrings = 0;
midih_interface_t *p_midi_host = get_midi_host(dev_addr);
TU_VERIFY(p_midi_host != NULL);
nstrings = p_midi_host->num_cables_tx;
if (nstrings > max_istrings)
{
nstrings = max_istrings;
}