summaryrefslogtreecommitdiff
path: root/src/class/audio/audio_device.c
blob: 7e7fef19f4678cc64d83f00cbb7cdde499efcb3e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
/* 
 * The MIT License (MIT)
 *
 * Copyright (c) 2020 Reinhard Panhuber
 *
 * 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.
 *
 * This file is part of the TinyUSB stack.
 */

/*
 * This driver supports at most one out EP, one in EP, one control EP, and one feedback EP and one alternative interface other than zero. Hence, only one input terminal and one output terminal are support, if you need more adjust the driver!
 * It supports multiple TX and RX channels.
 *
 * In case you need more alternate interfaces, you need to define additional defines for this specific alternate interface. Just define them and set them in the set_interface function.
 *
 * */

#include "tusb_option.h"

#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_AUDIO)

//--------------------------------------------------------------------+
// INCLUDE
//--------------------------------------------------------------------+
#include "audio_device.h"
#include "class/audio/audio.h"
#include "device/usbd_pvt.h"

//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
typedef struct
{
	uint8_t const * p_desc; 	// Pointer pointing to Standard AC Interface Descriptor(4.7.1) - Audio Control descriptor defining audio function

#if CFG_TUD_AUDIO_EPSIZE_IN
	uint8_t ep_in; 	 			// Outgoing (out of uC) audio data EP.
	uint8_t ep_in_as_intf_num; 	// Corresponding Standard AS Interface Descriptor (4.9.1) belonging to output terminal to which this EP belongs - 0 is invalid (this fits to UAC2 specification since AS interfaces can not have interface number equal to zero)
#endif

#if CFG_TUD_AUDIO_EPSIZE_OUT
	uint8_t ep_out; 			// Incoming (into uC) audio data EP.
	uint8_t ep_out_as_intf_num; // Corresponding Standard AS Interface Descriptor (4.9.1) belonging to input terminal to which this EP belongs - 0 is invalid (this fits to UAC2 specification since AS interfaces can not have interface number equal to zero)

#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
	uint8_t ep_fb; 				// Feedback EP.
#endif

#endif

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN
	uint8_t ep_int_ctr; 		// Audio control interrupt EP.
#endif

#if CFG_TUD_AUDIO_N_AS_INT
	uint8_t altSetting[CFG_TUD_AUDIO_N_AS_INT];
#endif
	/*------------- From this point, data is not cleared by bus reset -------------*/

	// Buffer for control requests
	CFG_TUSB_MEM_ALIGN uint8_t ctrl_buf[CFG_TUD_AUDIO_CTRL_BUF_SIZE];

	// FIFO
#if CFG_TUD_AUDIO_EPSIZE_IN && CFG_TUD_AUDIO_TX_FIFO_SIZE
	tu_fifo_t tx_ff[CFG_TUD_AUDIO_N_CHANNELS_TX];
	CFG_TUSB_MEM_ALIGN uint8_t tx_ff_buf[CFG_TUD_AUDIO_N_CHANNELS_TX][CFG_TUD_AUDIO_TX_FIFO_SIZE];
#if CFG_FIFO_MUTEX
	osal_mutex_def_t tx_ff_mutex[CFG_TUD_AUDIO_N_CHANNELS_TX];
#endif
#endif

#if CFG_TUD_AUDIO_EPSIZE_OUT && CFG_TUD_AUDIO_RX_FIFO_SIZE
	tu_fifo_t rx_ff[CFG_TUD_AUDIO_N_CHANNELS_RX];
	CFG_TUSB_MEM_ALIGN uint8_t rx_ff_buf[CFG_TUD_AUDIO_N_CHANNELS_RX][CFG_TUD_AUDIO_RX_FIFO_SIZE];
#if CFG_FIFO_MUTEX
	osal_mutex_def_t rx_ff_mutex[CFG_TUD_AUDIO_N_CHANNELS_RX];
#endif
#endif

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN
	tu_fifo_t int_ctr_ff;
	CFG_TUSB_MEM_ALIGN uint8_t int_ctr_ff_buf[CFG_TUD_AUDIO_INT_CTR_BUFSIZE];
#if CFG_FIFO_MUTEX
	osal_mutex_def_t int_ctr_ff_mutex;
#endif
#endif

	// Endpoint Transfer buffers
#if CFG_TUD_AUDIO_EPSIZE_OUT
	CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_AUDIO_EPSIZE_OUT]; 	// Bigger makes no sense for isochronous EP's (but technically possible here)

	// TODO: required?
	//#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
	//	uint16_t fb_val; 													// Feedback value for asynchronous mode!
	//#endif

#endif

#if CFG_TUD_AUDIO_EPSIZE_IN
	CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_AUDIO_EPSIZE_IN]; 		// Bigger makes no sense for isochronous EP's (but technically possible here)
#endif

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN
	CFG_TUSB_MEM_ALIGN uint8_t ep_int_ctr_buf[CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN];
#endif

} audiod_interface_t;

#define ITF_MEM_RESET_SIZE   offsetof(audiod_interface_t, ctrl_buf)

//#if CFG_TUD_AUDIO_EPSIZE_IN && CFG_TUD_AUDIO_TX_FIFO_SIZE
//#define ITF_MEM_RESET_SIZE   offsetof(audiod_interface_t, tx_ff)
//#elif CFG_TUD_AUDIO_EPSIZE_OUT && CFG_TUD_AUDIO_RX_FIFO_SIZE
//#define ITF_MEM_RESET_SIZE   offsetof(audiod_interface_t, rx_ff)
//#elif CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN
//#define ITF_MEM_RESET_SIZE   offsetof(audiod_interface_t, int_ctr_ff)
//#elif CFG_TUD_AUDIO_EPSIZE_OUT
//#define ITF_MEM_RESET_SIZE   offsetof(audiod_interface_t, epout_buf)
//#elif CFG_TUD_AUDIO_EPSIZE_IN
//#define ITF_MEM_RESET_SIZE   offsetof(audiod_interface_t, epin_buf)
//#elif CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN
//#define ITF_MEM_RESET_SIZE   offsetof(audiod_interface_t, ep_int_ctr_buf)
//#endif

//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
CFG_TUSB_MEM_SECTION audiod_interface_t _audiod_itf[CFG_TUD_AUDIO];

extern const uint16_t tud_audio_desc_lengths[];

#if CFG_TUD_AUDIO_EPSIZE_OUT
static audio_rx_done_type_I_pcm_ff_cb(uint8_t rhport, audiod_interface_t* audio, uint8_t const* buffer, uint32_t bufsize);
#endif

#if CFG_TUD_AUDIO_EPSIZE_IN
static bool audio_tx_done_type_I_pcm_ff_cb(uint8_t rhport, audiod_interface_t* audio, uint16_t * n_bytes_copied);
#endif

static bool audiod_get_interface(uint8_t rhport, tusb_control_request_t const * p_request);
static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const * p_request);

static bool audiod_get_interface_index(uint8_t itf, uint8_t *idxDriver, uint8_t *idxItf, uint8_t const **pp_desc_int);
static bool audiod_verify_entity_exists(uint8_t itf, uint8_t entityID);
static bool audiod_verify_itf_exists(uint8_t itf);
static bool audiod_verify_ep_exists(uint8_t ep);

bool tud_audio_n_mounted(uint8_t itf)
{
	audiod_interface_t* audio = &_audiod_itf[itf];

#if CFG_TUD_AUDIO_EPSIZE_OUT
	if (audio->ep_out == 0)
	{
		return false;
	}
#endif

#if CFG_TUD_AUDIO_EPSIZE_IN
	if (audio->ep_in == 0)
	{
		return false;
	}
#endif

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN
	if (audio->ep_int_ctr == 0)
	{
		return false;
	}
#endif

#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
	if (audio->ep_fb == 0)
	{
		return false;
	}
#endif

	return true;
}

//--------------------------------------------------------------------+
// READ API
//--------------------------------------------------------------------+

#if CFG_TUD_AUDIO_EPSIZE_OUT && CFG_TUD_AUDIO_RX_FIFO_SIZE

uint16_t tud_audio_n_available(uint8_t itf, uint8_t channelId)
{
	TU_VERIFY(channelId < CFG_TUD_AUDIO_N_CHANNELS_RX);
	return tu_fifo_count(&_audiod_itf[itf].rx_ff[channelId]);
}

uint16_t tud_audio_n_read(uint8_t itf, uint8_t channelId, void* buffer, uint16_t bufsize)
{
	TU_VERIFY(channelId < CFG_TUD_AUDIO_N_CHANNELS_RX);
	return tu_fifo_read_n(&_audiod_itf[itf].rx_ff[channelId], buffer, bufsize);
}

void tud_audio_n_read_flush (uint8_t itf, uint8_t channelId)
{
	TU_VERIFY(channelId < CFG_TUD_AUDIO_N_CHANNELS_RX);
	tu_fifo_clear(&_audiod_itf[itf].rx_ff[channelId]);
}

#endif

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN

uint16_t tud_audio_int_ctr_n_available(uint8_t itf)
{
	return tu_fifo_count(&_audiod_itf[itf].int_ctr_ff);
}

uint16_t tud_audio_int_ctr_n_read(uint8_t itf, void* buffer, uint16_t bufsize)
{
	return tu_fifo_read_n(&_audiod_itf[itf].int_ctr_ff, buffer, bufsize);
}

void tud_audio_int_ctr_n_read_flush (uint8_t itf)
{
	tu_fifo_clear(&_audiod_itf[itf].int_ctr_ff);
}

#endif

// This function is called once something is received by USB and is responsible for decoding received stream into audio channels.
// If you prefer your own (more efficient) implementation suiting your purpose set CFG_TUD_AUDIO_RX_FIFO_SIZE = 0.

#if CFG_TUD_AUDIO_EPSIZE_OUT

static bool audio_rx_done_cb(uint8_t rhport, audiod_interface_t* audio, uint8_t* buffer, uint16_t bufsize)
{
	switch (CFG_TUD_AUDIO_FORMAT_TYPE_RX)
	{
	case AUDIO_FORMAT_TYPE_UNDEFINED:
		// INDIVIDUAL DECODING PROCEDURE REQUIRED HERE!
		asm("nop");
		break;

	case AUDIO_FORMAT_TYPE_I:

		switch (CFG_TUD_AUDIO_FORMAT_TYPE_I_RX)
		{
		case AUDIO_DATA_FORMAT_TYPE_I_PCM:

#if CFG_TUD_AUDIO_RX_FIFO_SIZE
			TU_VERIFY(audio_rx_done_type_I_pcm_ff_cb(rhport, audio, buffer, bufsize));
#else
#error YOUR DECODING AND BUFFERING IS REQUIRED HERE!
#endif
			break;

		default:
			// DESIRED CFG_TUD_AUDIO_FORMAT_TYPE_I_RX NOT IMPLEMENTED!
			asm("nop");
			break;
		}
		break;

		default:
			// Desired CFG_TUD_AUDIO_FORMAT_TYPE_RX not implemented!
			asm("nop");
			break;
	}

	// Call a weak callback here - a possibility for user to get informed RX was completed
	TTU_VERIFY(tud_audio_rx_done_cb(rhport, buffer, bufsize));
	return true;
}

#endif //CFG_TUD_AUDIO_EPSIZE_OUT

// The following functions are used in case CFG_TUD_AUDIO_RX_FIFO_SIZE != 0
#if CFG_TUD_AUDIO_RX_FIFO_SIZE
static bool audio_rx_done_type_I_pcm_ff_cb(uint8_t rhport, audiod_interface_t* audio, uint8_t * buffer, uint16_t bufsize)
{
	(void) rhport;

	// We expect to get a multiple of CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_RX * CFG_TUD_AUDIO_N_CHANNELS_RX per channel
	if (bufsize % CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_RX*CFG_TUD_AUDIO_N_CHANNELS_RX != 0) {
		return false;
	}

	uint8_t chId = 0;
	uint16_t cnt;
#if CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_RX == 1
	uint8_t sample = 0;
#elif CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_RX == 2
	uint16_t sample = 0;
#else
	uint32_t sample = 0;
#endif

	for(cnt = 0; cnt < bufsize; cnt += CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_RX)
	{
		// Let alignment problems be handled by memcpy
		memcpy(&sample, &buffer[cnt], CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_RX);
		if(tu_fifo_write_n(&audio->rx_ff[chId++], &sample, CFG_TUD_AUDIO_RX_ITEMSIZE) != CFG_TUD_AUDIO_RX_ITEMSIZE)
		{
			// Buffer overflow
			return false;
		}

		if (chId == CFG_TUD_AUDIO_N_CHANNELS_RX)
		{
			chId = 0;
		}
	}
}																																														}
#endif //CFG_TUD_AUDIO_RX_FIFO_SIZE


#if CFG_TUD_AUDIO_EPSIZE_OUT
TU_ATTR_WEAK bool tud_audio_rx_done_cb(uint8_t rhport, uint8_t * buffer, uint16_t bufsize)
{
	(void) rhport;
	(void) buffer;
	(void) bufsize;

	/* NOTE: This function should not be modified, when the callback is needed,
           the tud_audio_rx_done_cb could be implemented in the user file
	 */

	return true;
}
#endif

//--------------------------------------------------------------------+
// WRITE API
//--------------------------------------------------------------------+

#if CFG_TUD_AUDIO_EPSIZE_IN && CFG_TUD_AUDIO_TX_FIFO_SIZE
uint16_t tud_audio_n_write(uint8_t itf, uint8_t channelId, uint8_t const* buffer, uint16_t bufsize)
{
	audiod_interface_t* audio = &_audiod_itf[itf];
	if (audio->p_desc == NULL) {
		return 0;
	}

	return tu_fifo_write_n(&audio->tx_ff[channelId], buffer, bufsize);
}
#endif

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN > 0

uint32_t tud_audio_int_ctr_n_write(uint8_t itf, uint8_t const* buffer, uint32_t bufsize)
{
	audiod_interface_t* audio = &_audiod_itf[itf];
	if (audio->itf_num == 0) {
		return 0;
	}

	return tu_fifo_write_n(&audio->int_ctr_ff, buffer, bufsize);
}

#endif


// This function is called once a transmit of an audio packet was successfully completed. Here, we encode samples and place it in IN EP's buffer for next transmission.
// If you prefer your own (more efficient) implementation suiting your purpose set CFG_TUD_AUDIO_TX_FIFO_SIZE = 0.
#if CFG_TUD_AUDIO_EPSIZE_IN
static bool audio_tx_done_cb(uint8_t rhport, audiod_interface_t* audio, uint16_t * n_bytes_copied)
{
	switch (CFG_TUD_AUDIO_FORMAT_TYPE_TX)
	{
	case AUDIO_FORMAT_TYPE_UNDEFINED:
		// INDIVIDUAL ENCODING PROCEDURE REQUIRED HERE!
		asm("nop");
		break;

	case AUDIO_FORMAT_TYPE_I:

		switch (CFG_TUD_AUDIO_FORMAT_TYPE_I_TX)
		{
		case AUDIO_DATA_FORMAT_TYPE_I_PCM:

#if CFG_TUD_AUDIO_TX_FIFO_SIZE
			TU_VERIFY(audio_tx_done_type_I_pcm_ff_cb(rhport, audio, n_bytes_copied));
#else
#error YOUR ENCODING AND BUFFERING IS REQUIRED HERE!
#endif
			break;

		default:
			// YOUR ENCODING AND SENDING IS REQUIRED HERE!
			asm("nop");
			break;
		}
		break;

		default:
			// Desired CFG_TUD_AUDIO_FORMAT_TYPE_TX not implemented!
			asm("nop");
			break;
	}

	// Call a weak callback here - a possibility for user to get informed TX was completed
	TU_VERIFY(tud_audio_tx_done_cb(rhport, n_bytes_copied));
	return true;
}

#endif //CFG_TUD_AUDIO_EPSIZE_IN

#if CFG_TUD_AUDIO_TX_FIFO_SIZE
static bool audio_tx_done_type_I_pcm_ff_cb(uint8_t rhport, audiod_interface_t* audio, uint16_t * n_bytes_copied)
{
	// We encode directly into IN EP's buffer - abort if previous transfer not complete
	TU_VERIFY(!usbd_edpt_busy(rhport, audio->ep_in));

	// Determine amount of samples
	uint16_t nSamplesPerChannelToSend = 0xFFFF;
	uint8_t cntChannel;

	for (cntChannel = 0; cntChannel < CFG_TUD_AUDIO_N_CHANNELS_TX; cntChannel++)
	{
		if (audio->tx_ff[cntChannel].count < nSamplesPerChannelToSend)
		{
			nSamplesPerChannelToSend = audio->tx_ff[cntChannel].count;
		}
	}

	// Check if there is enough
	if (nSamplesPerChannelToSend == 0)
	{
		*n_bytes_copied = 0;
		return true;
	}

	// Limit to maximum sample number - THIS IS A POSSIBLE ERROR SOURCE IF TOO MANY SAMPLE WOULD NEED TO BE SENT BUT CAN NOT!
	if (nSamplesPerChannelToSend * CFG_TUD_AUDIO_N_CHANNELS_TX * CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_TX > CFG_TUD_AUDIO_EPSIZE_IN)
	{
		nSamplesPerChannelToSend = CFG_TUD_AUDIO_EPSIZE_IN / CFG_TUD_AUDIO_N_CHANNELS_TX / CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_TX;
	}

	// Encode
	uint16_t cntSample;
	uint8_t * pBuff = audio->epin_buf;
#if CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_TX == 1
	uint8_t sample;
#elif CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_TX == 2
	uint16_t sample;
#else
	uint32_t sample;
#endif

	// TODO: Big endianess handling
	for (cntSample = 0; cntSample < nSamplesPerChannelToSend; cntSample++)
	{
		for (cntChannel = 0; cntChannel < CFG_TUD_AUDIO_N_CHANNELS_TX; cntChannel++)
		{
			// Get sample from buffer
			tu_fifo_read(&audio->tx_ff[cntChannel], &sample);

			// Put it into EP's buffer - Let alignment problems be handled by memcpy
			memcpy(pBuff, &sample, CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_TX);

			// Advance pointer
			pBuff += CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_TX;
		}
	}

	// Schedule transmit
	TU_VERIFY(usbd_edpt_xfer(rhport, audio->ep_in, audio->epin_buf, nSamplesPerChannelToSend*CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_TX));
	*n_bytes_copied = nSamplesPerChannelToSend*CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_TX;
	return true;
}

#endif //CFG_TUD_AUDIO_TX_FIFO_SIZE

// This function is called once a transmit of an feedback packet was successfully completed. Here, we get the next feedback value to be sent

#if CFG_TUD_AUDIO_EPSIZE_OUT && CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
static uint16_t audio_fb_done_cb(uint8_t rhport, audiod_interface_t* audio)
{
	(void) rhport;
	(void) audio;

	// Here we need to return the feedback value
#error RETURN YOUR FEEDBACK VALUE HERE!

	TU_VERIFY(tud_audio_fb_done_cb(rhport));
	return 0;
}

#endif

// This function is called once a transmit of an interrupt control packet was successfully completed. Here, we get the remaining bytes to send

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN
static bool audio_int_ctr_done_cb(uint8_t rhport, audiod_interface_t* audio, uint16_t * n_bytes_copied)
{
	// We write directly into the EP's buffer - abort if previous transfer not complete
	TU_VERIFY(!usbd_edpt_busy(rhport, audio->ep_int_ctr));

	// TODO: Big endianess handling
	uint16_t cnt = tu_fifo_read_n(audio->int_ctr_ff, audio->ep_int_ctr_buf, CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN);

	if (cnt > 0)
	{
		// Schedule transmit
		TU_VERIFY(usbd_edpt_xfer(rhport, audio->ep_int_ctr, audio->ep_int_ctr_buf, cnt));
	}

	*n_bytes_copied = cnt;

	TU_VERIFY(tud_audio_int_ctr_done_cb(rhport, n_bytes_copied));

	return true;
}
#endif

// Callback functions
// Currently the return value has to effect so far. The return value finally is discarded in tud_task(void) in usbd.c - we just incorporate that for later use

#if CFG_TUD_AUDIO_EPSIZE_IN
TU_ATTR_WEAK bool tud_audio_tx_done_cb(uint8_t rhport, uint16_t * n_bytes_copied)
{
	(void) rhport;
	(void) n_bytes_copied;

	/* NOTE: This function should not be modified, when the callback is needed,
           the tud_audio_tx_done_cb could be implemented in the user file
	 */

	return true;
}
#endif

#if CFG_TUD_AUDIO_EPSIZE_OUT && CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
TU_ATTR_WEAK bool tud_audio_fb_done_cb(uint8_t rhport)
{
	(void) rhport;

	/* NOTE: This function should not be modified, when the callback is needed,
           the tud_audio_fb_done_cb could be implemented in the user file
	 */

	return true;
}
#endif

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN
TU_ATTR_WEAK bool tud_audio_int_ctr_done_cb(uint8_t rhport, uint16_t * n_bytes_copied)
{
	(void) rhport;
	(void) n_bytes_copied;

	/* NOTE: This function should not be modified, when the callback is needed,
	           the tud_audio_int_ctr_done_cb could be implemented in the user file
	 */

	return true;
}

#endif

//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void audiod_init(void)
{
	uint8_t cnt;
	tu_memclr(_audiod_itf, sizeof(_audiod_itf));

	for(uint8_t i=0; i<CFG_TUD_AUDIO; i++)
	{
		audiod_interface_t* audio = &_audiod_itf[i];

		// Initialize TX FIFOs if required
#if CFG_TUD_AUDIO_EPSIZE_IN && CFG_TUD_AUDIO_TX_FIFO_SIZE
		for (cnt = 0; cnt < CFG_TUD_AUDIO_N_CHANNELS_TX; cnt++)
		{
			tu_fifo_config(&audio->tx_ff[cnt], &audio->tx_ff_buf[cnt], CFG_TUD_AUDIO_TX_FIFO_SIZE, CFG_TUD_AUDIO_TX_ITEMSIZE, true);
#if CFG_FIFO_MUTEX
			tu_fifo_config_mutex(&audio->tx_ff[cnt], osal_mutex_create(&audio->tx_ff_mutex[cnt]));
#endif
		}
#endif

#if CFG_TUD_AUDIO_EPSIZE_OUT && CFG_TUD_AUDIO_RX_FIFO_SIZE
		for (cnt = 0; cnt < CFG_TUD_AUDIO_N_CHANNELS_RX; cnt++)
		{
			tu_fifo_config(&audio->rx_ff[cnt], &audio->rx_ff_buf[cnt], CFG_TUD_AUDIO_RX_FIFO_SIZE, CFG_TUD_AUDIO_RX_ITEMSIZE, true);
#if CFG_FIFO_MUTEX
			tu_fifo_config_mutex(&audio->rx_ff[cnt], osal_mutex_create(&audio->rx_ff_mutex[cnt]));
#endif
		}
#endif

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN > 0
		tu_fifo_config(&audio->int_ctr_ff, &audio->int_ctr_ff_buf, CFG_TUD_AUDIO_INT_CTR_BUFSIZE, 1, true);
#if CFG_FIFO_MUTEX
		tu_fifo_config_mutex(&audio->int_ctr_ff, osal_mutex_create(&audio->int_ctr_ff_mutex));
#endif
#endif
	}
}

void audiod_reset(uint8_t rhport)
{
	(void) rhport;

	for(uint8_t i=0; i<CFG_TUD_AUDIO; i++)
	{
		audiod_interface_t* audio = &_audiod_itf[i];
		tu_memclr(audio, ITF_MEM_RESET_SIZE);

		uint8_t cnt;
#if CFG_TUD_AUDIO_EPSIZE_IN && CFG_TUD_AUDIO_TX_FIFO_SIZE
		for (cnt = 0; cnt < CFG_TUD_AUDIO_N_CHANNELS_TX; cnt++)
		{
			tu_fifo_clear(&audio->tx_ff[cnt]);
		}
#endif

#if CFG_TUD_AUDIO_EPSIZE_OUT && CFG_TUD_AUDIO_RX_FIFO_SIZE
		for (cnt = 0; cnt < CFG_TUD_AUDIO_N_CHANNELS_RX; cnt++)
		{
			tu_fifo_clear(&audio->rx_ff[cnt]);
		}
#endif
	}
}

uint16_t audiod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
{
	TU_VERIFY ( TUSB_CLASS_AUDIO 	== itf_desc->bInterfaceClass &&
			AUDIO_SUBCLASS_CONTROL	== itf_desc->bInterfaceSubClass);

	// Verify version is correct - this check can be omitted
	TU_VERIFY(itf_desc->bInterfaceProtocol == AUDIO_INT_PROTOCOL_CODE_V2);

	// Verify interrupt control EP is enabled if demanded by descriptor - this should be best some static check however - this check can be omitted
	if (itf_desc->bNumEndpoints == 1) // 0 or 1 EPs are allowed
	{
		TU_VERIFY(CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN > 0);
	}

	// Alternate setting MUST be zero - this check can be omitted
	TU_VERIFY(itf_desc->bAlternateSetting == 0);

	// Find available audio driver interface
	uint8_t i;
	for (i = 0; i < CFG_TUD_AUDIO; i++)
	{
		if (!_audiod_itf[i].p_desc)
		{
			_audiod_itf[i].p_desc = (uint8_t const *)itf_desc; 	// Save pointer to AC descriptor which is by specification always the first one
			break;
		}
	}

	// Verify we found a free one
	TU_ASSERT( i < CFG_TUD_AUDIO );

	// This is all we need so far - the EPs are setup by a later set_interface request (as per UAC2 specification)

	// Notify caller we read complete descriptor
	//	(*p_length) += tud_audio_desc_lengths[i];
	// TODO: Find a way to find end of current audio function and avoid necessity of tud_audio_desc_lengths - since now max_length is available we could do this surely somehow
	uint16_t drv_len = tud_audio_desc_lengths[i] - TUD_AUDIO_DESC_IAD_LEN; 	// - TUD_AUDIO_DESC_IAD_LEN since tinyUSB already handles the IAD descriptor

	return drv_len;
}

static bool audiod_get_interface(uint8_t rhport, tusb_control_request_t const * p_request)
{
#if CFG_TUD_AUDIO_N_AS_INT > 0
	uint8_t const itf = tu_u16_low(p_request->wIndex);

	// Find index of audio streaming interface
	uint8_t idxDriver, idxItf;
	uint8_t const *dummy;

	TU_VERIFY(audiod_get_interface_index(itf, &idxDriver, &idxItf, &dummy));
	TU_VERIFY(tud_control_xfer(rhport, p_request, &_audiod_itf[idxDriver].altSetting[idxItf], 1));

	return true;

#else
	(void) rhport;
	(void) p_request;
	return false;
#endif
}

static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const * p_request)
{
	(void) rhport;

	// Here we need to do the following:

	// 1. Find the audio driver interface which was assigned to the given interface which is to be set
	// Since one audio driver interface has to be able to cover an unknown number of interfaces (AC, AS + its alternate settings), the best memory efficient way to solve this is to always search through the descriptors.
	// The audio driver interface is mapped to an audio function by a reference pointer to the corresponding AC interface of this audio function which serves as a starting point for searching

	// 2. Close EPs which are currently open
	// To do so it is not necessary to know the current active alternate interface since we already save the current EP addresses - we simply close them

	// 3. Open new EP

	uint8_t const itf = tu_u16_low(p_request->wIndex);
	uint8_t const alt = tu_u16_low(p_request->wValue);

	// Find index of audio streaming interface and index of interface
	uint8_t idxDriver, idxItf;
	uint8_t const *p_desc;
	TU_VERIFY(audiod_get_interface_index(itf, &idxDriver, &idxItf, &p_desc));

	// Look if there is an EP to be closed - for this driver, there are only 3 possible EPs which may be closed (only AS related EPs can be closed, AC EP (if present) is always open)
#if CFG_TUD_AUDIO_EPSIZE_IN > 0
	if (_audiod_itf[idxDriver].ep_in_as_intf_num == itf)
	{
		_audiod_itf[idxDriver].ep_in_as_intf_num = 0;
		usbd_edpt_close(rhport, _audiod_itf[idxDriver].ep_in);
		_audiod_itf[idxDriver].ep_in = 0; 							// Necessary?
	}
#endif

#if CFG_TUD_AUDIO_EPSIZE_OUT
	if (_audiod_itf[idxDriver].ep_out_as_intf_num == itf)
	{
		_audiod_itf[idxDriver].ep_out_as_intf_num = 0;
		usbd_edpt_close(rhport, _audiod_itf[idxDriver].ep_out);
		_audiod_itf[idxDriver].ep_out = 0; 							// Necessary?

		// Close corresponding feedback EP
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
		usbd_edpt_close(rhport, _audiod_itf[idxDriver].ep_fb);
		_audiod_itf[idxDriver].ep_fb = 0; 							// Necessary?
#endif
	}
#endif


	// Open new EP if necessary - EPs are only to be closed or opened for AS interfaces - Look for AS interface with correct alternate interface
	// Get pointer at end
	uint8_t const *p_desc_end = _audiod_itf[idxDriver].p_desc + tud_audio_desc_lengths[idxDriver];

	// p_desc starts at required interface with alternate setting zero
	while (p_desc < p_desc_end)
	{
		// Find correct interface
		if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE && ((tusb_desc_interface_t const * )p_desc)->bInterfaceNumber == itf && ((tusb_desc_interface_t const * )p_desc)->bAlternateSetting == alt)
		{
			// Open EPs
			uint8_t foundEPs = 0;
			while (foundEPs < ((tusb_desc_interface_t const * )p_desc)->bNumEndpoints && p_desc < p_desc_end)
			{
				if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT)
				{
					TU_ASSERT(dcd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), false);
					uint8_t ep_addr = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;

#if CFG_TUD_AUDIO_EPSIZE_IN > 0
					if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN && ((tusb_desc_endpoint_t const *) p_desc)->bmAttributes.usage == 0x00) 	// Check if usage is data EP
					{
						_audiod_itf[idxDriver].ep_in = ep_addr;
					}
#endif

#if CFG_TUD_AUDIO_EPSIZE_OUT

					if (tu_edpt_dir(ep_addr) == TUSB_DIR_OUT) 	// Checking usage not necessary
					{
						// Save address
						_audiod_itf[idxDriver].ep_out = ep_addr;

						// Prepare for incoming data
						TU_ASSERT(usbd_edpt_xfer(rhport, ep_addr, _audiod_itf[idxDriver].epout_buf, CFG_TUD_AUDIO_EPSIZE_OUT), false);
					}

#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
					if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN && ((tusb_desc_endpoint_t const *) p_desc)->bmAttributes.usage == 0x10) 	// Check if usage is implicit data feedback
					{
						_audiod_itf[idxDriver].ep_fb = ep_addr;
					}
#endif

#endif
					foundEPs += 1;
				}
				p_desc = tu_desc_next(p_desc);
			}

			// We are done - abort loop
			break;
		}

		// Increase index, bytes read, and pointer
		p_desc = tu_desc_next(p_desc);
	}

	// Check for nothing found - we can rely on this since EP descriptors are never the last descriptors, there are always also class specific EP descriptors following!
	TU_VERIFY(p_desc < p_desc_end);

	// Invoke callback
	if (tud_audio_set_itf_cb)
	{
		if (!tud_audio_set_itf_cb(rhport, p_request))
		{
			return false;
		}
	}

	// Save current alternative interface setting
	_audiod_itf[idxDriver].altSetting[idxItf] = alt;

	tud_control_status(rhport, p_request);

	return true;
}

// Invoked when class request DATA stage is finished.
// return false to stall control EP (e.g Host send non-sense DATA)
bool audiod_control_complete(uint8_t rhport, tusb_control_request_t const * p_request)
{
	// Handle audio class specific set requests
	if(p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS && p_request->bmRequestType_bit.direction == TUSB_DIR_OUT)
	{
		switch (p_request->bmRequestType_bit.recipient)
		{
		case TUSB_REQ_RCPT_INTERFACE: ;		// The semicolon is there to enable a declaration right after the label

		uint8_t itf = TU_U16_LOW(p_request->wIndex);
		uint8_t entityID = TU_U16_HIGH(p_request->wIndex);

		// Verify if entity is present - This check may be omitted if we trust the host not to send rubbish
		if (entityID != 0)
		{
			// Invoke callback
			if (tud_audio_set_req_entity_cb && tud_audio_set_req_entity_cb(rhport, p_request))
			{
				tud_control_status(rhport, p_request);
			}
			else
			{
				return false; 	// In case no callback function is present or request can not be conducted we stall it
			}
		}
		else
		{
			// Invoke callback
			if (tud_audio_set_req_itf_cb && tud_audio_set_req_itf_cb(rhport, p_request))
			{
				tud_control_status(rhport, p_request);
			}
			else
			{
				return false; 	// In case no callback function is present or request can not be conducted we stall it
			}
		}

		break;

		case TUSB_REQ_RCPT_ENDPOINT: ;		// The semicolon is there to enable a declaration right after the label

		uint8_t ep = TU_U16_LOW(p_request->wIndex);

		// Invoke callback
		if (tud_audio_set_req_ep_cb && tud_audio_set_req_ep_cb(rhport, p_request))
		{
			tud_control_status(rhport, p_request);
		}
		else
		{
			return false; 	// In case no callback function is present or request can not be conducted we stall it
		}

		break;

		// Unknown/Unsupported recipient
		default: TU_BREAKPOINT(); return false;
		}
	}
	return true;
}

// Handle class control request
// return false to stall control endpoint (e.g unsupported request)
bool audiod_control_request(uint8_t rhport, tusb_control_request_t const * p_request)
{
	(void) rhport;

	// Handle standard requests - standard set requests usually have no data stage so we also handle set requests here
	if (p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD)
	{
		switch (p_request->bRequest)
		{
		case TUSB_REQ_GET_INTERFACE:
			return audiod_get_interface(rhport, p_request);

		case TUSB_REQ_SET_INTERFACE:
			return audiod_set_interface(rhport, p_request);

			// Unknown/Unsupported request
		default: TU_BREAKPOINT(); return false;
		}
	}

	// Handle class requests
	if (p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS)
	{
		// Conduct checks which depend on the recipient
		switch (p_request->bmRequestType_bit.recipient)
		{
		case TUSB_REQ_RCPT_INTERFACE: ;		// The semicolon is there to enable a declaration right after the label

		uint8_t itf = TU_U16_LOW(p_request->wIndex);
		uint8_t entityID = TU_U16_HIGH(p_request->wIndex);

		// Verify if entity is present - This check may be omitted if we trust the host not to send rubbish
		if (entityID != 0)
		{
			TU_VERIFY(audiod_verify_entity_exists(itf, entityID));

			// If request is a set request we return true here and handle the rest later in audiod_control_complete() once the data stage was finished
			if (p_request->bmRequestType_bit.direction == TUSB_DIR_OUT) return true;

			// Invoke callback - callback needs to answer as defined in UAC2 specification page 89 - 5. Requests
			if (tud_audio_get_req_entity_cb)
			{
				return tud_audio_get_req_entity_cb(rhport, p_request);
			}
			else
			{
				TU_LOG2("  No entity get request callback available!\r\n");
			}
		}
		else
		{
			TU_VERIFY(audiod_verify_itf_exists(itf));

			// If request is a set request we return true here and handle the rest later in audiod_control_complete() once the data stage was finished
			if (p_request->bmRequestType_bit.direction == TUSB_DIR_OUT) return true;

			// Invoke callback - callback needs to answer as defined in UAC2 specification page 89 - 5. Requests
			if (tud_audio_get_req_itf_cb)
			{
				return tud_audio_get_req_itf_cb(rhport, p_request);
			}
			else
			{
				TU_LOG2("  No interface get request callback available!\r\n");
			}
		}

		break;

		case TUSB_REQ_RCPT_ENDPOINT: ;		// The semicolon is there to enable a declaration right after the label

		uint8_t ep = TU_U16_LOW(p_request->wIndex);

		// Verify if EP is present - This check may be omitted if we trust the host not to send rubbish
		TU_VERIFY(audiod_verify_ep_exists(ep));

		// If request is a set request we return true here and handle the rest later in audiod_control_complete() once the data stage was finished
		if (p_request->bmRequestType_bit.direction == TUSB_DIR_OUT) return true;

		if (tud_audio_get_req_ep_cb)
		{
			return tud_audio_get_req_ep_cb(rhport, p_request);
		}
		else
		{
			TU_LOG2("  No EP get request callback available!\r\n");
		}

		break;

		// Unknown/Unsupported recipient
		default: TU_LOG2("  Unsupported recipient: %d\r\n", p_request->bmRequestType_bit.recipient); TU_BREAKPOINT(); return false;
		}

		// Host expects an answer - in case no callback function is present we stall the request
		return false;
	}

	// There went something wrong
	TU_BREAKPOINT();
	return false;
}

bool audiod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
{
	(void) result;

	// Search for interface belonging to given end point address and proceed as required
	uint8_t idxDriver;
	for (idxDriver = 0; idxDriver < CFG_TUD_AUDIO; idxDriver++)
	{

#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN

		// Data transmission of control interrupt finished
		if (_audiod_itf[idxDriver].ep_int_ctr == ep_addr)
		{
			// According to USB2 specification, maximum payload of interrupt EP is 8 bytes on low speed, 64 bytes on full speed, and 1024 bytes on high speed (but only if an alternate interface other than 0 is used - see specification p. 49)
			// In case there is nothing to send we have to return a NAK - this is taken care of by PHY ???
			// In case of an erroneous transmission a retransmission is conducted - this is taken care of by PHY ???

			// Load new data
			uint16 *n_bytes_copied;
			TU_VERIFY(audio_int_ctr_done_cb(rhport, &_audiod_itf[idxDriver], n_bytes_copied));

			if (*n_bytes_copied == 0 && xferred_bytes && (0 == (xferred_bytes % CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN)))
			{
				// There is no data left to send, a ZLP should be sent if
				// xferred_bytes is multiple of EP size and not zero
				return usbd_edpt_xfer(rhport, ep_addr, NULL, 0);
			}
		}

#endif

#if CFG_TUD_AUDIO_EPSIZE_IN

		// Data transmission of audio packet finished
		if (_audiod_itf[idxDriver].ep_in == ep_addr)
		{
			// USB 2.0, section 5.6.4, third paragraph, states "An isochronous endpoint must specify its required bus access period. However, an isochronous endpoint must be prepared to handle poll rates faster than the one specified."
			// That paragraph goes on to say "An isochronous IN endpoint must return a zero-length packet whenever data is requested at a faster interval than the specified interval and data is not available."
			// This can only be solved reliably if we load a ZLP after every IN transmission since we can not say if the host requests samples earlier than we declared! Once all samples are collected we overwrite the loaded ZLP.

			// Check if there is data to load into EPs buffer - if not load it with ZLP
			// Be aware - we as a device are not able to know if the host polls for data with a faster rate as we stated this in the descriptors. Therefore we always have to put something into the EPs buffer. However, once we did that, there is no way of aborting this or replacing what we put into the buffer before!
			// This is the only place where we can fill something into the EPs buffer!

			// Load new data
			uint16_t *n_bytes_copied = NULL;
			TU_VERIFY(audio_tx_done_cb(rhport, &_audiod_itf[idxDriver], n_bytes_copied));

			if (*n_bytes_copied == 0)
			{
				// Load with ZLP
				return usbd_edpt_xfer(rhport, ep_addr, NULL, 0);
			}

			return true;
		}
#endif

#if CFG_TUD_AUDIO_EPSIZE_OUT

		// New audio packet received
		if (_audiod_itf[idxDriver].ep_out == ep_addr)
		{
			// Save into buffer - do whatever has to be done
			TU_VERIFY(audio_rx_done_cb(rhport, &_audiod_itf[idxDriver], _audiod_itf[idxDriver].epout_buf, xferred_bytes));

			// prepare for next transmission
			TU_ASSERT(usbd_edpt_xfer(rhport, ep_addr, _audiod_itf[idxDriver].epout_buf, CFG_TUD_AUDIO_EPSIZE_OUT), false);

			return true;
		}


#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
		// Transmission of feedback EP finished
		if (_audiod_itf[idxDriver].ep_fb == ep_addr)
		{
			if (!audio_fb_done_cb(rhport, &_audiod_itf[idxDriver]))
			{
				// Load with ZLP
				return usbd_edpt_xfer(rhport, ep_addr, NULL, 0);
			}

			return true;
		}
#endif
#endif
	}

	return false;

}

// This helper function finds for a given interface number the index of the attached driver interface, the index of the interface in the audio function
// (e.g. the std. AS interface with interface number 15 is the first AS interface for the given audio function and thus gets index zero), and
// finally a pointer to the std. AS interface, where the pointer always points to the start i.e. alternate interface zero.
static bool audiod_get_interface_index(uint8_t itf, uint8_t *idxDriver, uint8_t *idxItf, uint8_t const **pp_desc_int)
{
	// Loop over audio driver interfaces
	uint8_t i;
	for (i = 0; i < CFG_TUD_AUDIO; i++)
	{
		if (_audiod_itf[i].p_desc)
		{
			// Get pointer at end
			uint8_t const *p_desc_end = _audiod_itf[i].p_desc + tud_audio_desc_lengths[i];

			// Advance past AC descriptors
			uint8_t const *p_desc = tu_desc_next(_audiod_itf[i].p_desc);
			p_desc += ((audio_desc_cs_ac_interface_t const *)p_desc)->wTotalLength;

			uint8_t tmp = 0;
			while (p_desc < p_desc_end)
			{
				// We assume the number of alternate settings is increasing thus we return the index of alternate setting zero!
				if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE && ((tusb_desc_interface_t const * )p_desc)->bInterfaceNumber == itf)
				{
					*idxItf = tmp;
					*idxDriver = i;
					*pp_desc_int = p_desc;
					return true;
				}

				// Increase index, bytes read, and pointer
				tmp++;
				p_desc = tu_desc_next(p_desc);
			}
		}
	}

	return false;
}

static bool audiod_verify_entity_exists(uint8_t itf, uint8_t entityID)
{
	uint8_t i;
	for (i = 0; i < CFG_TUD_AUDIO; i++)
	{
		// Look for the correct driver by checking if the unique standard AC interface number fits
		if (_audiod_itf[i].p_desc && ((tusb_desc_interface_t const *)_audiod_itf[i].p_desc)->bInterfaceNumber == itf)
		{
			// Get pointers after class specific AC descriptors and end of AC descriptors - entities are defined in between
			uint8_t const *p_desc = tu_desc_next(_audiod_itf[i].p_desc); 											// Points to CS AC descriptor
			uint8_t const *p_desc_end = ((audio_desc_cs_ac_interface_t const *)p_desc)->wTotalLength + p_desc;
			p_desc = tu_desc_next(p_desc); 																			// Get past CS AC descriptor

			while (p_desc < p_desc_end)
			{
				if (p_desc[3] == entityID) 	// Entity IDs are always at offset 3
				{
					return true;
				}
				p_desc = tu_desc_next(p_desc);
			}
		}
	}
	return false;
}

static bool audiod_verify_itf_exists(uint8_t itf)
{
	uint8_t i;
	for (i = 0; i < CFG_TUD_AUDIO; i++)
	{
		if (_audiod_itf[i].p_desc)
		{
			// Get pointer at beginning and end
			uint8_t const *p_desc = _audiod_itf[i].p_desc;
			uint8_t const *p_desc_end = _audiod_itf[i].p_desc + tud_audio_desc_lengths[i];

			while (p_desc < p_desc_end)
			{
				if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE && ((tusb_desc_interface_t const *)_audiod_itf[i].p_desc)->bInterfaceNumber == itf)
				{
					return true;
				}
				p_desc = tu_desc_next(p_desc);
			}
		}
	}
	return false;
}

static bool audiod_verify_ep_exists(uint8_t ep)
{
	uint8_t i;
	for (i = 0; i < CFG_TUD_AUDIO; i++)
	{
		if (_audiod_itf[i].p_desc)
		{
			// Get pointer at end
			uint8_t const *p_desc_end = _audiod_itf[i].p_desc + tud_audio_desc_lengths[i];

			// Advance past AC descriptors - EP we look for are streaming EPs
			uint8_t const *p_desc = tu_desc_next(_audiod_itf[i].p_desc);
			p_desc += ((audio_desc_cs_ac_interface_t const *)p_desc)->wTotalLength;

			while (p_desc < p_desc_end)
			{
				if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT && ((tusb_desc_endpoint_t const * )p_desc)->bEndpointAddress == ep)
				{
					return true;
				}
				p_desc = tu_desc_next(p_desc);
			}
		}
	}
	return false;
}

#endif //TUSB_OPT_DEVICE_ENABLED && CFG_TUD_AUDIO

// OLD
//// In order that this function works the following is mandatory:
//	// - An IAD descriptor is required and has to be placed directly before the Standard AC Interface Descriptor (4.7.1) ALSO if no Audio Streaming interfaces are used!
//	// - The Standard AC Interface Descriptor (4.7.1) has to be the first interface listed (required by UAC2 specification)
//	// - The alternate interfaces of the Standard AS Interface Descriptor(4.9.1) must be listed in increasing order and alternate 0 must have zero EPs
//
//	TU_VERIFY ( TUSB_CLASS_AUDIO 	== itf_desc->bInterfaceClass &&
//			AUDIO_SUBCLASS_CONTROL	== itf_desc->bInterfaceSubClass);
//
//	// Verify version is correct - this check can be omitted
//	TU_VERIFY(itf_desc->bInterfaceProtocol == AUDIO_PROTOCOL_V2);
//
//	// Verify interrupt control EP is enabled if demanded by descriptor - this should be best some static check however - this check can be omitted
//	if (itf_desc->bNumEndpoints == 1) // 0 or 1 EPs are allowed
//	{
//		TU_VERIFY(CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN > 0);
//	}
//
//	// Alternate setting MUST be zero - this check can be omitted
//	TU_VERIFY(itf_desc->bAlternateSetting == 0);
//
//	// Since checks are successful we get the number of interfaces we have to process by exploiting the fact that an IAD descriptor has to be provided directly before this AC descriptor
//	// We do not get this value delivered by the stack so we need to hack a little bit - the number of interfaces to be processed is listed in the IAD descriptor with an offset of exactly 5
//	uint8_t nItfs = *(((uint8_t const *) itf_desc) - 5) - 1; 	// -1 since we already process the (included) AC interface
//
//	// Notify caller we have read Standard AC Interface Descriptor (4.7.1)
//	(*p_length) = sizeof(tusb_desc_interface_t);
//
//	// Go to next descriptor which has to be a Class-Specific AC Interface Header Descriptor(4.7.2) - always present
//	audio_desc_cs_ac_interface_t const * p_desc_cs_ac = (audio_desc_cs_ac_interface_t const *) tu_desc_next ( (uint8_t const *) itf_desc );
//
//	// Verify its the correct descriptor - this check can be omitted
//	TU_VERIFY(p_desc_cs_ac->bDescriptorType == TUSB_DESC_CS_INTERFACE
//			&& p_desc_cs_ac->bDescriptorSubType == AUDIO_CS_AC_INTERFACE_HEADER
//			&& p_desc_cs_ac->bcdADC == 0x0200);
//
//	// We do not check the Audio Function Category Codes
//
//	// We do not check all the following Clock Source, Unit and Terminal descriptors
//
//	// We do not check the latency control
//
//	// We jump over all the following descriptor to the next following interface (which should be a Standard AS Interface Descriptor(4.9.1) if nItfs > 0)
//	(*p_length) += p_desc_cs_ac->wTotalLength; 																		// Notify caller of read bytes
//	itf_desc = (tusb_desc_interface_t const * )(((uint8_t const *)p_desc_cs_ac ) + p_desc_cs_ac->wTotalLength); 	// Move pointer
//
//	// From this point on we have nItfs packs of audio streaming interfaces (consisting of multiple descriptors always starting with a Standard AS Interface Descriptor(4.9.1))
//
//	uint8_t cnt;
//	uint8_t nEPs;
//	audio_format_type_t formatType;
//	uint8_t intNum;
//	uint8_t altInt;
//
//	for (cnt = 0; cnt < nItfs; cnt++)
//	{
//
//		// Here starts an unknown number of alternate interfaces - the art here is to find the end of this interface pack. We do not have the total number of descriptor bytes available (not provided by tinyUSB stack for .open() function) so we try to find the end of this interface pack by deducing it from the order and content of the descriptor pack
//		while(1)
//		{
//			// At first this should be a Standard AS Interface Descriptor(4.9.1) - this check can be omitted
//			TU_VERIFY(itf_desc->bDescriptorType == TUSB_DESC_INTERFACE &&
//					itf_desc->bInterfaceClass == TUSB_CLASS_AUDIO &&
//					itf_desc->bInterfaceSubClass == AUDIO_SUBCLASS_STREAMING &&
//					itf_desc->bInterfaceProtocol == AUDIO_PROTOCOL_V2);
//
//			// Remember number of EPs this interface has - required for later
//			nEPs = itf_desc->bNumEndpoints;
//			intNum = itf_desc->bInterfaceNumber;
//			altInt = itf_desc->bAlternateSetting;
//
//			// Notify caller we have read bytes
//			(*p_length) += tu_desc_len((uint8_t const *) itf_desc);
//
//			// Advance pointer
//			itf_desc = (tusb_desc_interface_t const * )(tu_desc_next((uint8_t const *)itf_desc));
//
//			// It is possible now that the following interface is again a Standard AS Interface Descriptor(4.9.1) (in case the related terminal has more than zero EPs at all and this is the following alternative interface > 0 - the alternative (that this is not a Standard AS Interface Descriptor(4.9.1) but a Class-Specific AS Interface Descriptor(4.9.2)) could be in case the related terminal has no EPs at all e.g. SPDIF connector
//			if (tu_desc_type((uint8_t const *) itf_desc) == TUSB_DESC_INTERFACE)
//			{
//				// It is not possible, that a Standard AS Interface Descriptor(4.9.1) ends without a Class-Specific AS Interface Descriptor(4.9.2)
//				// hence the next interface has to be an alternative interface with regard to the one we started above - continue
//				continue;
//			}
//
//			// Second, this interface has to be a Class-Specific AS Interface Descriptor(4.9.2) - this check can be omitted
//			TU_VERIFY(tu_desc_type((uint8_t const *) itf_desc) == TUSB_DESC_CS_INTERFACE &&
//					((audio_desc_cs_as_interface_t *) itf_desc)->bDescriptorSubType ==  AUDIO_CS_AS_INTERFACE_AS_GENERAL);
//
//			// Remember format type of this interface - required for later
//			formatType = ((audio_desc_cs_as_interface_t *) itf_desc)->bFormatType;
//
//			// Notify caller we have read bytes
//			(*p_length) += tu_desc_len((uint8_t const *) itf_desc);
//
//			// Advance pointer
//			itf_desc = (tusb_desc_interface_t const * )(tu_desc_next((uint8_t const *)itf_desc));
//
//			// Every Class-Specific AS Interface Descriptor(4.9.2) defines a format type and thus a format type descriptor e.g. Type I Format Type Descriptor(2.3.1.6 - Audio Formats) should follow - only if the format type is undefined no format type descriptor follows
//			if (formatType != AUDIO_FORMAT_TYPE_UNDEFINED)
//			{
//				// We do not need any information from this descriptor - we simply advance
//				// Notify caller we have read bytes
//				(*p_length) += tu_desc_len((uint8_t const *) itf_desc);
//
//				// Advance pointer
//				itf_desc = (tusb_desc_interface_t const * )(tu_desc_next((uint8_t const *)itf_desc));
//			}
//
//			// Now there might be an encoder/decoder interface - i found no clue if this interface is mandatory (although examples found by google never use this descriptor) or if there is any way to determine if this descriptor is reliably present or not. Problem here: Since we need to find the end of this interface pack and we do not have the total amount of descriptor bytes available here (not given by the tinyUSB stack for the .open() function), we have to make a speculative check if the following is an encoder/decoder interface or not.
//			// It could be possible that we ready over the total length of all descriptor bytes here - we simply hope that the combination of descriptor length, type, and sub type does not occur randomly in memory here. The only way to make this check waterproof is to know how many bytes we can read at all but this would be an information we need to be given by tinyUSB!
//			if (tu_desc_len((uint8_t const *) itf_desc) == 21 && tu_desc_type((uint8_t const *) itf_desc) == TUSB_DESC_CS_INTERFACE && (((audio_desc_cs_as_interface_t *) itf_desc)->bDescriptorSubType ==  AUDIO_CS_AS_INTERFACE_ENCODER || ((audio_desc_cs_as_interface_t *) itf_desc)->bDescriptorSubType ==  AUDIO_CS_AS_INTERFACE_DECODER))
//			{
//				// We do not need any information from this descriptor - we simply advance
//				// Notify caller we have read bytes
//				(*p_length) += tu_desc_len((uint8_t const *) itf_desc);
//
//				// Advance pointer
//				itf_desc = (tusb_desc_interface_t const * )(tu_desc_next((uint8_t const *)itf_desc));
//			}
//
//			// Here may be some EP descriptors
//			for (uint8_t cntEP = 0; cntEP < nEPs; cntEP++)
//			{
//				// This check can be omitted
//				TU_VERIFY(tu_desc_type((uint8_t const *) itf_desc) == TUSB_DESC_ENDPOINT);
//
//				// Here we can find information about our EPs address etc. but in the initialization process we do not set anything, we have to wait for the host to send a setInterface request where the EPs etc. are set up according
//
//				// Notify caller we have read bytes
//				(*p_length) += tu_desc_len((uint8_t const *) itf_desc);
//
//				// If the usage type is something else than feedback there is a Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) following up
//				if (((tusb_desc_endpoint_t *) itf_desc)->bmAttributes.usage != 0x10)
//				{
//					// Advance pointer
//					itf_desc = (tusb_desc_interface_t const * )(tu_desc_next((uint8_t const *)itf_desc));
//
//					// We do not need any information from this descriptor - we simply advance
//					// Notify caller we have read bytes
//					(*p_length) += tu_desc_len((uint8_t const *) itf_desc);
//
//				}
//
//				// Advance pointer
//				itf_desc = (tusb_desc_interface_t const * )(tu_desc_next((uint8_t const *)itf_desc));
//			}
//
//			// Okay we processed a complete pack - now we have to make a speculative check if the next interface is an alternate interface belonging to the ones processed before - since it is speculative we check a lot to reduce the chances we go wrong and hope for the best
//			if (!(tu_desc_len((uint8_t const *) itf_desc) == 9 && tu_desc_type((uint8_t const *) itf_desc) == TUSB_DESC_INTERFACE && itf_desc->bInterfaceNumber ==  intNum && itf_desc->bAlternateSetting == altInt + 1))
//			{
//				// The current interface is not a consecutive alternate interface to the one processed above
//				break;
//			}
//		}
//
//	}


// Here follow the encoding steps

//#if CFG_TUD_AUDIO_FORMAT_TYPE_TX == AUDIO_FORMAT_TYPE_UNDEFINED
//
//#error Individual encoding procedure required!
//
//#elif CFG_TUD_AUDIO_FORMAT_TYPE_TX == AUDIO_FORMAT_TYPE_I
//
//#if CFG_TUD_AUDIO_FORMAT_TYPE_I_TX == AUDIO_DATA_FORMAT_TYPE_I_PCM
//
//#if CFG_TUD_AUDIO_TX_FIFO_SIZE
//	TU_VERIFY(audio_tx_done_type_I_pcm_cb(rhport, audio, n_bytes_copied));
//#else
//#error YOUR ENCODING AND SENDING IS REQUIRED HERE!
//#endif
//
//#else
//#error Desired CFG_TUD_AUDIO_FORMAT_TYPE_I_TX not implemented!
//#endif
//
//#else
//#error Desired CFG_TUD_AUDIO_FORMAT_TYPE_TX not implemented!
//#endif
//
//	// Call a weak callback here - a possibility for user to get informed TX was completed
//	TU_VERIFY(tud_audio_tx_done_cb(rhport, n_bytes_copied));
//	return true;

//#if CFG_TUD_AUDIO_EPSIZE_OUT
//static bool audio_rx_done_cb(uint8_t rhport, audiod_interface_t* audio, uint8_t* buffer, uint16_t bufsize) {
//
//	// Here follow the decoding steps
//
//#if CFG_TUD_AUDIO_FORMAT_TYPE_RX == AUDIO_FORMAT_TYPE_UNDEFINED
//
//#error Individual decoding procedure required!
//
//#elif CFG_TUD_AUDIO_FORMAT_TYPE_RX == AUDIO_FORMAT_TYPE_I
//
//#if CFG_TUD_AUDIO_FORMAT_TYPE_I_RX == AUDIO_DATA_FORMAT_TYPE_I_PCM
//
//#if CFG_TUD_AUDIO_RX_FIFO_SIZE
//	TU_VERIFY(audio_rx_done_type_I_pcm_ff_cb(rhport, audio, buffer, bufsize));
//#else
//#error YOUR DECODING AND BUFFERING IS REQUIRED HERE!
//#endif
//
//#else
//#error Desired CFG_TUD_AUDIO_FORMAT_TYPE_I_RX not implemented!
//#endif
//
//#else
//#error Desired CFG_TUD_AUDIO_FORMAT_TYPE_RX not implemented!
//#endif
//
//	TU_VERIFY(tud_audio_rx_done_cb(rhport, buffer, bufsize));
//	return true;
//}
//#endif