1 /*
2  * Copyright (c) 2024, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "usbd_core.h"
7 #include "usbd_audio.h"
8 
9 #define USING_FEEDBACK 0
10 
11 #define USBD_VID           0xffff
12 #define USBD_PID           0xffff
13 #define USBD_MAX_POWER     100
14 #define USBD_LANGID_STRING 1033
15 
16 #ifdef CONFIG_USB_HS
17 #define EP_INTERVAL               0x04
18 #define FEEDBACK_ENDP_PACKET_SIZE 0x04
19 #else
20 #define EP_INTERVAL               0x01
21 #define FEEDBACK_ENDP_PACKET_SIZE 0x03
22 #endif
23 
24 #define AUDIO_IN_EP  0x81
25 #define AUDIO_OUT_EP 0x02
26 #define AUDIO_OUT_FEEDBACK_EP 0x83
27 
28 #define AUDIO_IN_FU_ID  0x02
29 #define AUDIO_OUT_FU_ID 0x05
30 
31 /* AUDIO Class Config */
32 #define AUDIO_SPEAKER_FREQ            16000U
33 #define AUDIO_SPEAKER_FRAME_SIZE_BYTE 2u
34 #define AUDIO_SPEAKER_RESOLUTION_BIT  16u
35 #define AUDIO_MIC_FREQ                16000U
36 #define AUDIO_MIC_FRAME_SIZE_BYTE     2u
37 #define AUDIO_MIC_RESOLUTION_BIT      16u
38 
39 #define AUDIO_SAMPLE_FREQ(frq) (uint8_t)(frq), (uint8_t)((frq >> 8)), (uint8_t)((frq >> 16))
40 
41 /* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 2) */
42 #define AUDIO_OUT_PACKET ((uint32_t)((AUDIO_SPEAKER_FREQ * AUDIO_SPEAKER_FRAME_SIZE_BYTE * 2) / 1000))
43 /* 16bit(2 Bytes) 双声道(Mono:2) */
44 #define AUDIO_IN_PACKET ((uint32_t)((AUDIO_MIC_FREQ * AUDIO_MIC_FRAME_SIZE_BYTE * 2) / 1000))
45 
46 #if USING_FEEDBACK == 0
47 #define USB_AUDIO_CONFIG_DESC_SIZ (unsigned long)(9 +                                       \
48                                                   AUDIO_AC_DESCRIPTOR_INIT_LEN(2) +         \
49                                                   AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +     \
50                                                   AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
51                                                   AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC +    \
52                                                   AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +     \
53                                                   AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
54                                                   AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC +    \
55                                                   AUDIO_AS_DESCRIPTOR_INIT_LEN(1) +         \
56                                                   AUDIO_AS_DESCRIPTOR_INIT_LEN(1))
57 #else
58 #define USB_AUDIO_CONFIG_DESC_SIZ (unsigned long)(9 +                                       \
59                                                   AUDIO_AC_DESCRIPTOR_INIT_LEN(2) +         \
60                                                   AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +     \
61                                                   AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
62                                                   AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC +    \
63                                                   AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +     \
64                                                   AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
65                                                   AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC +    \
66                                                   AUDIO_AS_DESCRIPTOR_INIT_LEN(1) +         \
67                                                   AUDIO_AS_FEEDBACK_DESCRIPTOR_INIT_LEN(1))
68 #endif
69 
70 #define AUDIO_AC_SIZ (AUDIO_SIZEOF_AC_HEADER_DESC(2) +          \
71                       AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +     \
72                       AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
73                       AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC +    \
74                       AUDIO_SIZEOF_AC_INPUT_TERMINAL_DESC +     \
75                       AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(2, 1) + \
76                       AUDIO_SIZEOF_AC_OUTPUT_TERMINAL_DESC)
77 
78 #ifdef CONFIG_USBDEV_ADVANCE_DESC
79 static const uint8_t device_descriptor[] = {
80     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01)
81 };
82 
83 static const uint8_t config_descriptor[] = {
84     USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x03, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
85     AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x03, AUDIO_AC_SIZ, 0x00, 0x01, 0x02),
86     AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, 0x02, 0x0003),
87     AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00, 0x00),
88     AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, 0x02),
89     AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x02, 0x0003),
90     AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x05, 0x04, 0x01, 0x03, 0x00, 0x00),
91     AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x06, AUDIO_OUTTERM_SPEAKER, 0x05),
92 #if USING_FEEDBACK == 0
93     AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET,
94                              EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
95 #else
96     AUDIO_AS_FEEDBACK_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, AUDIO_OUT_PACKET,
97                              EP_INTERVAL, AUDIO_OUT_FEEDBACK_EP, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
98 #endif
99     AUDIO_AS_DESCRIPTOR_INIT(0x02, 0x03, 0x02, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET,
100                              EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ))
101 };
102 
103 static const uint8_t device_quality_descriptor[] = {
104     ///////////////////////////////////////
105     /// device qualifier descriptor
106     ///////////////////////////////////////
107     0x0a,
108     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
109     0x00,
110     0x02,
111     0x00,
112     0x00,
113     0x00,
114     0x40,
115     0x00,
116     0x00,
117 };
118 
119 static const char *string_descriptors[] = {
120     (const char[]){ 0x09, 0x04 }, /* Langid */
121     "CherryUSB",                  /* Manufacturer */
122     "CherryUSB UAC DEMO",         /* Product */
123     "2022123456",                 /* Serial Number */
124 };
125 
device_descriptor_callback(uint8_t speed)126 static const uint8_t *device_descriptor_callback(uint8_t speed)
127 {
128     return device_descriptor;
129 }
130 
config_descriptor_callback(uint8_t speed)131 static const uint8_t *config_descriptor_callback(uint8_t speed)
132 {
133     return config_descriptor;
134 }
135 
device_quality_descriptor_callback(uint8_t speed)136 static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
137 {
138     return device_quality_descriptor;
139 }
140 
string_descriptor_callback(uint8_t speed,uint8_t index)141 static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
142 {
143     if (index > 3) {
144         return NULL;
145     }
146     return string_descriptors[index];
147 }
148 
149 const struct usb_descriptor audio_v1_descriptor = {
150     .device_descriptor_callback = device_descriptor_callback,
151     .config_descriptor_callback = config_descriptor_callback,
152     .device_quality_descriptor_callback = device_quality_descriptor_callback,
153     .string_descriptor_callback = string_descriptor_callback
154 };
155 #else
156 const uint8_t audio_v1_descriptor[] = {
157     USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xef, 0x02, 0x01, USBD_VID, USBD_PID, 0x0001, 0x01),
158     USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x03, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
159     AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x03, AUDIO_AC_SIZ, 0x00, 0x01, 0x02),
160     AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, 0x02, 0x0003),
161     AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00, 0x00),
162     AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, 0x02),
163     AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x02, 0x0003),
164     AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x05, 0x04, 0x01, 0x03, 0x00, 0x00),
165     AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x06, AUDIO_OUTTERM_SPEAKER, 0x05),
166 #if USING_FEEDBACK == 0
167     AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, 0x09, AUDIO_OUT_PACKET,
168                              EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
169 #else
170     AUDIO_AS_FEEDBACK_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, AUDIO_OUT_PACKET,
171                              EP_INTERVAL, AUDIO_OUT_FEEDBACK_EP, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)),
172 #endif
173     AUDIO_AS_DESCRIPTOR_INIT(0x02, 0x03, 0x02, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, 0x05, AUDIO_IN_PACKET,
174                              EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ)),
175     ///////////////////////////////////////
176     /// string0 descriptor
177     ///////////////////////////////////////
178     USB_LANGID_INIT(USBD_LANGID_STRING),
179     ///////////////////////////////////////
180     /// string1 descriptor
181     ///////////////////////////////////////
182     0x14,                       /* bLength */
183     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
184     'C', 0x00,                  /* wcChar0 */
185     'h', 0x00,                  /* wcChar1 */
186     'e', 0x00,                  /* wcChar2 */
187     'r', 0x00,                  /* wcChar3 */
188     'r', 0x00,                  /* wcChar4 */
189     'y', 0x00,                  /* wcChar5 */
190     'U', 0x00,                  /* wcChar6 */
191     'S', 0x00,                  /* wcChar7 */
192     'B', 0x00,                  /* wcChar8 */
193     ///////////////////////////////////////
194     /// string2 descriptor
195     ///////////////////////////////////////
196     0x26,                       /* bLength */
197     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
198     'C', 0x00,                  /* wcChar0 */
199     'h', 0x00,                  /* wcChar1 */
200     'e', 0x00,                  /* wcChar2 */
201     'r', 0x00,                  /* wcChar3 */
202     'r', 0x00,                  /* wcChar4 */
203     'y', 0x00,                  /* wcChar5 */
204     'U', 0x00,                  /* wcChar6 */
205     'S', 0x00,                  /* wcChar7 */
206     'B', 0x00,                  /* wcChar8 */
207     ' ', 0x00,                  /* wcChar9 */
208     'U', 0x00,                  /* wcChar10 */
209     'A', 0x00,                  /* wcChar11 */
210     'C', 0x00,                  /* wcChar12 */
211     ' ', 0x00,                  /* wcChar13 */
212     'D', 0x00,                  /* wcChar14 */
213     'E', 0x00,                  /* wcChar15 */
214     'M', 0x00,                  /* wcChar16 */
215     'O', 0x00,                  /* wcChar17 */
216     ///////////////////////////////////////
217     /// string3 descriptor
218     ///////////////////////////////////////
219     0x16,                       /* bLength */
220     USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
221     '2', 0x00,                  /* wcChar0 */
222     '0', 0x00,                  /* wcChar1 */
223     '2', 0x00,                  /* wcChar2 */
224     '2', 0x00,                  /* wcChar3 */
225     '1', 0x00,                  /* wcChar4 */
226     '2', 0x00,                  /* wcChar5 */
227     '3', 0x00,                  /* wcChar6 */
228     '4', 0x00,                  /* wcChar7 */
229     '5', 0x00,                  /* wcChar8 */
230 #if USING_FEEDBACK == 0
231     '1', 0x00,                  /* wcChar9 */
232 #else
233     '2', 0x00,                  /* wcChar9 */
234 #endif
235 #ifdef CONFIG_USB_HS
236     ///////////////////////////////////////
237     /// device qualifier descriptor
238     ///////////////////////////////////////
239     0x0a,
240     USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
241     0x00,
242     0x02,
243     0x00,
244     0x00,
245     0x00,
246     0x40,
247     0x00,
248     0x00,
249 #endif
250     0x00
251 };
252 #endif
253 
254 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[AUDIO_OUT_PACKET];
255 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[AUDIO_IN_PACKET];
256 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t s_speaker_feedback_buffer[4];
257 
258 volatile bool tx_flag = 0;
259 volatile bool rx_flag = 0;
260 volatile bool ep_tx_busy_flag = false;
261 volatile uint32_t s_mic_sample_rate;
262 volatile uint32_t s_speaker_sample_rate;
263 
usbd_event_handler(uint8_t busid,uint8_t event)264 static void usbd_event_handler(uint8_t busid, uint8_t event)
265 {
266     switch (event) {
267         case USBD_EVENT_RESET:
268             break;
269         case USBD_EVENT_CONNECTED:
270             break;
271         case USBD_EVENT_DISCONNECTED:
272             break;
273         case USBD_EVENT_RESUME:
274             break;
275         case USBD_EVENT_SUSPEND:
276             break;
277         case USBD_EVENT_CONFIGURED:
278             break;
279         case USBD_EVENT_SET_REMOTE_WAKEUP:
280             break;
281         case USBD_EVENT_CLR_REMOTE_WAKEUP:
282             break;
283 
284         default:
285             break;
286     }
287 }
288 
usbd_audio_open(uint8_t busid,uint8_t intf)289 void usbd_audio_open(uint8_t busid, uint8_t intf)
290 {
291     if (intf == 1) {
292         rx_flag = 1;
293         /* setup first out ep read transfer */
294         usbd_ep_start_read(busid, AUDIO_OUT_EP, read_buffer, AUDIO_OUT_PACKET);
295 #if USING_FEEDBACK == 1
296         uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_FS(s_speaker_sample_rate);
297         AUDIO_FEEDBACK_TO_BUF_FS(s_speaker_feedback_buffer, feedback_value); /* uac1 can only use 10.14 */
298         usbd_ep_start_write(busid, AUDIO_OUT_FEEDBACK_EP, s_speaker_feedback_buffer, FEEDBACK_ENDP_PACKET_SIZE);
299 #endif
300         printf("OPEN1\r\n");
301     } else {
302         tx_flag = 1;
303         ep_tx_busy_flag = false;
304         printf("OPEN2\r\n");
305     }
306 }
307 
usbd_audio_close(uint8_t busid,uint8_t intf)308 void usbd_audio_close(uint8_t busid, uint8_t intf)
309 {
310     if (intf == 1) {
311         rx_flag = 0;
312         printf("CLOSE1\r\n");
313     } else {
314         tx_flag = 0;
315         ep_tx_busy_flag = false;
316         printf("CLOSE2\r\n");
317     }
318 }
319 
usbd_audio_set_sampling_freq(uint8_t busid,uint8_t ep,uint32_t sampling_freq)320 void usbd_audio_set_sampling_freq(uint8_t busid, uint8_t ep, uint32_t sampling_freq)
321 {
322     if (ep == AUDIO_OUT_EP) {
323         s_speaker_sample_rate = sampling_freq;
324     } else if (ep == AUDIO_IN_EP) {
325         s_mic_sample_rate = sampling_freq;
326     }
327 }
328 
usbd_audio_get_sampling_freq(uint8_t busid,uint8_t ep)329 uint32_t usbd_audio_get_sampling_freq(uint8_t busid, uint8_t ep)
330 {
331     (void)busid;
332 
333     uint32_t freq = 0;
334 
335     if (ep == AUDIO_OUT_EP) {
336         freq = s_speaker_sample_rate;
337     } else if (ep == AUDIO_IN_EP) {
338         freq = s_mic_sample_rate;
339     }
340 
341     return freq;
342 }
343 
usbd_audio_out_callback(uint8_t busid,uint8_t ep,uint32_t nbytes)344 void usbd_audio_out_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
345 {
346     USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes);
347     usbd_ep_start_read(busid, AUDIO_OUT_EP, read_buffer, AUDIO_OUT_PACKET);
348 }
349 
usbd_audio_in_callback(uint8_t busid,uint8_t ep,uint32_t nbytes)350 void usbd_audio_in_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
351 {
352     USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes);
353     ep_tx_busy_flag = false;
354 }
355 
356 #if USING_FEEDBACK == 1
usbd_audio_iso_out_feedback_callback(uint8_t busid,uint8_t ep,uint32_t nbytes)357 void usbd_audio_iso_out_feedback_callback(uint8_t busid, uint8_t ep, uint32_t nbytes)
358 {
359     USB_LOG_RAW("actual feedback len:%d\r\n", (unsigned int)nbytes);
360     uint32_t feedback_value = AUDIO_FREQ_TO_FEEDBACK_FS(s_speaker_sample_rate);
361     AUDIO_FEEDBACK_TO_BUF_FS(s_speaker_feedback_buffer, feedback_value);
362     usbd_ep_start_write(busid, AUDIO_OUT_FEEDBACK_EP, s_speaker_feedback_buffer, FEEDBACK_ENDP_PACKET_SIZE);
363 }
364 #endif
365 
366 static struct usbd_endpoint audio_in_ep = {
367     .ep_cb = usbd_audio_in_callback,
368     .ep_addr = AUDIO_IN_EP
369 };
370 
371 static struct usbd_endpoint audio_out_ep = {
372     .ep_cb = usbd_audio_out_callback,
373     .ep_addr = AUDIO_OUT_EP
374 };
375 
376 #if USING_FEEDBACK == 1
377 static struct usbd_endpoint audio_out_feedback_ep = {
378     .ep_cb = usbd_audio_iso_out_feedback_callback,
379     .ep_addr = AUDIO_OUT_FEEDBACK_EP
380 };
381 #endif
382 
383 struct usbd_interface intf0;
384 struct usbd_interface intf1;
385 struct usbd_interface intf2;
386 
387 struct audio_entity_info audio_entity_table[] = {
388     { .bEntityId = AUDIO_IN_FU_ID,
389       .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
390       .ep = AUDIO_IN_EP },
391     { .bEntityId = AUDIO_OUT_FU_ID,
392       .bDescriptorSubtype = AUDIO_CONTROL_FEATURE_UNIT,
393       .ep = AUDIO_OUT_EP },
394 };
395 
audio_v1_init(uint8_t busid,uintptr_t reg_base)396 void audio_v1_init(uint8_t busid, uintptr_t reg_base)
397 {
398 #ifdef CONFIG_USBDEV_ADVANCE_DESC
399     usbd_desc_register(busid, &audio_v1_descriptor);
400 #else
401     usbd_desc_register(busid, audio_v1_descriptor);
402 #endif
403     usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf0, 0x0100, audio_entity_table, 2));
404     usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf1, 0x0100, audio_entity_table, 2));
405     usbd_add_interface(busid, usbd_audio_init_intf(busid, &intf2, 0x0100, audio_entity_table, 2));
406     usbd_add_endpoint(busid, &audio_in_ep);
407     usbd_add_endpoint(busid, &audio_out_ep);
408 #if USING_FEEDBACK == 1
409     usbd_add_endpoint(busid, &audio_out_feedback_ep);
410 #endif
411     usbd_initialize(busid, reg_base, usbd_event_handler);
412 }
413 
audio_v1_test(uint8_t busid)414 void audio_v1_test(uint8_t busid)
415 {
416     if (tx_flag) {
417         memset(write_buffer, 'a', AUDIO_IN_PACKET);
418         ep_tx_busy_flag = true;
419         usbd_ep_start_write(busid, AUDIO_IN_EP, write_buffer, AUDIO_IN_PACKET);
420         while (ep_tx_busy_flag) {
421             if (tx_flag == false) {
422                 break;
423             }
424         }
425     }
426 }
427