1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2019-09-19     flybreak     the first version
9  */
10 
11 #include <rthw.h>
12 #include <rtdevice.h>
13 #include "drivers/usb_device.h"
14 
15 #define AUFMT_MAX_FREQUENCIES       1
16 #include "uaudioreg.h"
17 
18 #define DBG_TAG              "usbd.audio.speaker"
19 #define DBG_LVL              DBG_INFO
20 #include <rtdbg.h>
21 
22 #define AUDIO_SAMPLERATE   16000
23 #define AUDIO_CHANNEL      1
24 #define RESOLUTION_BITS    16
25 
26 #define RESOLUTION_BYTE     (RESOLUTION_BITS / 8)
27 #define AUDIO_PER_MS_SZ    ((AUDIO_SAMPLERATE * AUDIO_CHANNEL * RESOLUTION_BYTE) / 1000)
28 #define AUDIO_BUFFER_SZ    (AUDIO_PER_MS_SZ * 20)  /* 20ms */
29 
30 #if defined(RT_USBD_SPEAKER_DEVICE_NAME)
31     #define SPEAKER_DEVICE_NAME    RT_USBD_SPEAKER_DEVICE_NAME
32 #else
33     #define SPEAKER_DEVICE_NAME    "sound0"
34 #endif
35 
36 #define EVENT_AUDIO_START   (1 << 0)
37 #define EVENT_AUDIO_STOP    (1 << 1)
38 #define EVENT_AUDIO_DATA    (1 << 2)
39 
40 #define SPK_INTF_STR_INDEX 9
41 /*
42  * uac speaker descriptor define
43  */
44 
45 #define UAC_CS_INTERFACE            0x24
46 #define UAC_CS_ENDPOINT             0x25
47 
48 #define UAC_MAX_PACKET_SIZE         64
49 #define UAC_EP_MAX_PACKET_SIZE      32
50 #define UAC_CHANNEL_NUM             AUDIO_CHANNEL
51 
52 struct uac_ac_descriptor
53 {
54 #ifdef RT_USB_DEVICE_COMPOSITE
55     struct uiad_descriptor iad_desc;
56 #endif
57     struct uinterface_descriptor intf_desc;
58     struct usb_audio_control_descriptor hdr_desc;
59     struct usb_audio_input_terminal it_desc;
60     struct usb_audio_output_terminal ot_desc;
61 #if UAC_USE_FEATURE_UNIT
62     struct usb_audio_feature_unit feature_unit_desc;
63 #endif
64 };
65 
66 struct uac_as_descriptor
67 {
68     struct uinterface_descriptor intf_desc;
69     struct usb_audio_streaming_interface_descriptor hdr_desc;
70     struct usb_audio_streaming_type1_descriptor format_type_desc;
71     struct uendpoint_descriptor ep_desc;
72     struct usb_audio_streaming_endpoint_descriptor as_ep_desc;
73 };
74 
75 /*
76  * uac speaker device type
77  */
78 
79 struct uac_audio_speaker
80 {
81     rt_device_t  dev;
82     rt_event_t   event;
83     rt_uint8_t   open_count;
84 
85     rt_uint8_t  *buffer;
86     rt_uint32_t  buffer_index;
87 
88     uep_t        ep;
89 };
90 static struct uac_audio_speaker speaker;
91 
92 rt_align(4)
93 static struct udevice_descriptor dev_desc =
94 {
95     USB_DESC_LENGTH_DEVICE,     //bLength;
96     USB_DESC_TYPE_DEVICE,       //type;
97     USB_BCD_VERSION,            //bcdUSB;
98     USB_CLASS_DEVICE,           //bDeviceClass;
99     0x00,                       //bDeviceSubClass;
100     0x00,                       //bDeviceProtocol;
101     UAC_MAX_PACKET_SIZE,        //bMaxPacketSize0;
102     _VENDOR_ID,                 //idVendor;
103     _PRODUCT_ID,                //idProduct;
104     USB_BCD_DEVICE,             //bcdDevice;
105     USB_STRING_MANU_INDEX,      //iManufacturer;
106     USB_STRING_PRODUCT_INDEX,   //iProduct;
107     USB_STRING_SERIAL_INDEX,    //iSerialNumber;Unused.
108     USB_DYNAMIC,                //bNumConfigurations;
109 };
110 
111 //FS and HS needed
112 rt_align(4)
113 static struct usb_qualifier_descriptor dev_qualifier =
114 {
115     sizeof(dev_qualifier),          //bLength
116     USB_DESC_TYPE_DEVICEQUALIFIER,  //bDescriptorType
117     0x0200,                         //bcdUSB
118     USB_CLASS_AUDIO,                //bDeviceClass
119     0x00,                           //bDeviceSubClass
120     0x00,                           //bDeviceProtocol
121     64,                             //bMaxPacketSize0
122     0x01,                           //bNumConfigurations
123     0,
124 };
125 
126 rt_align(4)
127 const static char *_ustring[] =
128 {
129     "Language",
130     "RT-Thread Team.",
131     "RT-Thread Audio Speaker",
132     "32021919830108",
133     "Configuration",
134     "Interface",
135 };
136 
137 rt_align(4)
138 static struct uac_ac_descriptor ac_desc =
139 {
140 #ifdef RT_USB_DEVICE_COMPOSITE
141     /* Interface Association Descriptor */
142     {
143         USB_DESC_LENGTH_IAD,
144         USB_DESC_TYPE_IAD,
145         USB_DYNAMIC,
146         0x02,
147         USB_CLASS_AUDIO,
148         USB_SUBCLASS_AUDIOSTREAMING,
149         0x00,
150         0x00,
151     },
152 #endif
153     /* Interface Descriptor */
154     {
155         USB_DESC_LENGTH_INTERFACE,
156         USB_DESC_TYPE_INTERFACE,
157         USB_DYNAMIC,
158         0x00,
159         0x00,
160         USB_CLASS_AUDIO,
161         USB_SUBCLASS_AUDIOCONTROL,
162         0x00,
163 #ifdef RT_USB_DEVICE_COMPOSITE
164         SPK_INTF_STR_INDEX,
165 #else
166         0x00,
167 #endif
168     },
169     /* Header Descriptor */
170     {
171         sizeof(struct usb_audio_control_descriptor),
172         UAC_CS_INTERFACE,
173         UDESCSUB_AC_HEADER,
174         0x0100,    /* Version: 1.00 */
175         0x0027,    /* Total length: 39 */
176         0x01,      /* Total number of interfaces: 1 */
177         {0x01},    /* Interface number: 1 */
178     },
179     /*  Input Terminal Descriptor */
180     {
181         sizeof(struct usb_audio_input_terminal),
182         UAC_CS_INTERFACE,
183         UDESCSUB_AC_INPUT,
184         0x01,      /* Terminal ID: 1 */
185         0x0101,    /* Terminal Type: USB Streaming (0x0101) */
186         0x00,      /* Assoc Terminal: 0 */
187         0x01,      /* Number Channels: 1 */
188         0x0000,    /* Channel Config: 0x0000 */
189         0x00,      /* Channel Names: 0 */
190         0x00,      /* Terminal: 0 */
191     },
192     /*  Output Terminal Descriptor */
193     {
194         sizeof(struct usb_audio_output_terminal),
195         UAC_CS_INTERFACE,
196         UDESCSUB_AC_OUTPUT,
197         0x02,      /* Terminal ID: 2 */
198         0x0302,    /* Terminal Type: Headphones (0x0302) */
199         0x00,      /* Assoc Terminal: 0 */
200         0x01,      /* Source ID: 1 */
201         0x00,      /* Terminal: 0 */
202     },
203 #if UAC_USE_FEATURE_UNIT
204     /*  Feature unit Descriptor */
205     {
206         UAC_DT_FEATURE_UNIT_SIZE(UAC_CH_NUM),
207         UAC_CS_INTERFACE,
208         UAC_FEATURE_UNIT,
209         0x02,
210         0x0101,
211         0x00,
212         0x01,
213     },
214 #endif
215 };
216 
217 rt_align(4)
218 static struct uinterface_descriptor as_desc0 =
219 {
220     USB_DESC_LENGTH_INTERFACE,
221     USB_DESC_TYPE_INTERFACE,
222     USB_DYNAMIC,
223     0x00,
224     0x00,
225     USB_CLASS_AUDIO,
226     USB_SUBCLASS_AUDIOSTREAMING,
227     0x00,
228     0x00,
229 };
230 
231 rt_align(4)
232 static struct uac_as_descriptor as_desc =
233 {
234     /* Interface Descriptor */
235     {
236         USB_DESC_LENGTH_INTERFACE,
237         USB_DESC_TYPE_INTERFACE,
238         USB_DYNAMIC,
239         0x01,
240         0x01,
241         USB_CLASS_AUDIO,
242         USB_SUBCLASS_AUDIOSTREAMING,
243         0x00,
244         0x00,
245     },
246     /* General AS Descriptor */
247     {
248         sizeof(struct usb_audio_streaming_interface_descriptor),
249         UAC_CS_INTERFACE,
250         AS_GENERAL,
251         0x01,      /* Terminal ID: 1 */
252         0x01,      /* Interface delay in frames: 1 */
253         UA_FMT_PCM,
254     },
255     /* Format type i Descriptor */
256     {
257         sizeof(struct usb_audio_streaming_type1_descriptor),
258         UAC_CS_INTERFACE,
259         FORMAT_TYPE,
260         FORMAT_TYPE_I,
261         UAC_CHANNEL_NUM,
262         2,         /* Subframe Size: 2 */
263         RESOLUTION_BITS,
264         0x01,      /* Samples Frequence Type: 1 */
265         {0},       /* Samples Frequence */
266     },
267     /* Endpoint Descriptor */
268     {
269         USB_DESC_LENGTH_ENDPOINT,
270         USB_DESC_TYPE_ENDPOINT,
271         USB_DYNAMIC | USB_DIR_OUT,
272         USB_EP_ATTR_ISOC,
273         UAC_EP_MAX_PACKET_SIZE,
274         0x01,
275     },
276     /* AS Endpoint Descriptor */
277     {
278         sizeof(struct usb_audio_streaming_endpoint_descriptor),
279         UAC_CS_ENDPOINT,
280         AS_GENERAL,
281     },
282 };
283 
speaker_entry(void * parameter)284 void speaker_entry(void *parameter)
285 {
286     struct rt_audio_caps caps = {0};
287     rt_uint32_t e, index;
288 
289     speaker.buffer = rt_malloc(AUDIO_BUFFER_SZ);
290     if (speaker.buffer == RT_NULL)
291     {
292         LOG_E("malloc failed");
293         goto __exit;
294     }
295 
296     speaker.dev = rt_device_find(SPEAKER_DEVICE_NAME);
297     if (speaker.dev == RT_NULL)
298     {
299         LOG_E("can't find device:%s", SPEAKER_DEVICE_NAME);
300         goto __exit;
301     }
302 
303     while (1)
304     {
305         if (rt_event_recv(speaker.event, EVENT_AUDIO_START | EVENT_AUDIO_STOP,
306                           RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
307                           1000, &e) != RT_EOK)
308         {
309             continue;
310         }
311         if (speaker.open_count == 0)
312         {
313             continue;
314         }
315         LOG_D("play start");
316 
317         rt_device_open(speaker.dev, RT_DEVICE_OFLAG_WRONLY);
318 
319         caps.main_type               = AUDIO_TYPE_OUTPUT;
320         caps.sub_type                = AUDIO_DSP_PARAM;
321         caps.udata.config.samplerate = AUDIO_SAMPLERATE;
322         caps.udata.config.channels   = AUDIO_CHANNEL;
323         caps.udata.config.samplebits = RESOLUTION_BITS;
324         rt_device_control(speaker.dev, AUDIO_CTL_CONFIGURE, &caps);
325 
326         while (1)
327         {
328             if (rt_event_recv(speaker.event, EVENT_AUDIO_DATA | EVENT_AUDIO_STOP,
329                               RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
330                               1000, &e) != RT_EOK)
331             {
332                 if (speaker.open_count > 0)
333                     continue;
334                 else
335                     break;
336             }
337             if (e & EVENT_AUDIO_DATA)
338             {
339                 index = (speaker.buffer_index >= AUDIO_BUFFER_SZ / 2) ? 0 : (AUDIO_BUFFER_SZ / 2);
340                 rt_device_write(speaker.dev, 0, speaker.buffer + index, AUDIO_BUFFER_SZ / 2);
341             }
342             else if (e & EVENT_AUDIO_STOP)
343             {
344                 break;
345             }
346         }
347         LOG_D("play stop");
348         rt_device_close(speaker.dev);
349     }
350 
351 __exit:
352     if (speaker.buffer)
353         rt_free(speaker.buffer);
354 }
355 
_audio_start(ufunction_t func)356 static rt_err_t _audio_start(ufunction_t func)
357 {
358     speaker.ep->request.buffer = speaker.buffer;
359     speaker.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
360     speaker.ep->request.req_type = UIO_REQUEST_READ_FULL;
361     rt_usbd_io_request(func->device, speaker.ep, &speaker.ep->request);
362 
363     speaker.open_count ++;
364     rt_event_send(speaker.event, EVENT_AUDIO_START);
365 
366     return 0;
367 }
368 
_audio_stop(ufunction_t func)369 static rt_err_t _audio_stop(ufunction_t func)
370 {
371     speaker.open_count --;
372     rt_event_send(speaker.event, EVENT_AUDIO_STOP);
373     return 0;
374 }
375 
_ep_data_handler(ufunction_t func,rt_size_t size)376 static rt_err_t _ep_data_handler(ufunction_t func, rt_size_t size)
377 {
378     RT_ASSERT(func != RT_NULL);
379     LOG_D("_ep_data_handler");
380 
381     speaker.ep->request.buffer = speaker.buffer + speaker.buffer_index;
382     speaker.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
383     speaker.ep->request.req_type = UIO_REQUEST_READ_FULL;
384     rt_usbd_io_request(func->device, speaker.ep, &speaker.ep->request);
385 
386     speaker.buffer_index += UAC_EP_MAX_PACKET_SIZE;
387     if (speaker.buffer_index >= AUDIO_BUFFER_SZ)
388     {
389         speaker.buffer_index = 0;
390         rt_event_send(speaker.event, EVENT_AUDIO_DATA);
391     }
392     else if (speaker.buffer_index == AUDIO_BUFFER_SZ / 2)
393     {
394         rt_event_send(speaker.event, EVENT_AUDIO_DATA);
395     }
396 
397     return RT_EOK;
398 }
399 
_interface_as_handler(ufunction_t func,ureq_t setup)400 static rt_err_t _interface_as_handler(ufunction_t func, ureq_t setup)
401 {
402     RT_ASSERT(func != RT_NULL);
403     RT_ASSERT(func->device != RT_NULL);
404     RT_ASSERT(setup != RT_NULL);
405 
406     LOG_D("_interface_as_handler");
407 
408     if ((setup->request_type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD)
409     {
410         switch (setup->bRequest)
411         {
412         case USB_REQ_GET_INTERFACE:
413             break;
414         case USB_REQ_SET_INTERFACE:
415             LOG_D("set interface handler");
416             if (setup->wValue == 1)
417             {
418                 _audio_start(func);
419             }
420             else if (setup->wValue == 0)
421             {
422                 _audio_stop(func);
423             }
424             break;
425         default:
426             LOG_D("unknown uac request 0x%x", setup->bRequest);
427             return -RT_ERROR;
428         }
429     }
430 
431     return RT_EOK;
432 }
433 
_function_enable(ufunction_t func)434 static rt_err_t _function_enable(ufunction_t func)
435 {
436     RT_ASSERT(func != RT_NULL);
437 
438     LOG_D("uac function enable");
439 
440     return RT_EOK;
441 }
442 
_function_disable(ufunction_t func)443 static rt_err_t _function_disable(ufunction_t func)
444 {
445     RT_ASSERT(func != RT_NULL);
446 
447     LOG_D("uac function disable");
448     _audio_stop(func);
449     return RT_EOK;
450 }
451 
452 static struct ufunction_ops ops =
453 {
454     _function_enable,
455     _function_disable,
456     RT_NULL,
457 };
458 /**
459  * This function will configure uac descriptor.
460  *
461  * @param comm the communication interface number.
462  * @param data the data interface number.
463  *
464  * @return RT_EOK on successful.
465  */
_uac_descriptor_config(struct uac_ac_descriptor * ac,rt_uint8_t cintf_nr,struct uac_as_descriptor * as,rt_uint8_t sintf_nr)466 static rt_err_t _uac_descriptor_config(struct uac_ac_descriptor *ac,
467                                        rt_uint8_t cintf_nr, struct uac_as_descriptor *as, rt_uint8_t sintf_nr)
468 {
469     ac->hdr_desc.baInterfaceNr[0] = sintf_nr;
470 #ifdef RT_USB_DEVICE_COMPOSITE
471     ac->iad_desc.bFirstInterface = cintf_nr;
472 #endif
473 
474     return RT_EOK;
475 }
476 
_uac_samplerate_config(struct uac_as_descriptor * as,rt_uint32_t samplerate)477 static rt_err_t _uac_samplerate_config(struct uac_as_descriptor *as, rt_uint32_t samplerate)
478 {
479     as->format_type_desc.tSamFreq[0 * 3 + 2] = samplerate >> 16 & 0xff;
480     as->format_type_desc.tSamFreq[0 * 3 + 1] = samplerate >> 8 & 0xff;
481     as->format_type_desc.tSamFreq[0 * 3 + 0] = samplerate & 0xff;
482     return RT_EOK;
483 }
484 
485 /**
486  * This function will create a uac function instance.
487  *
488  * @param device the usb device object.
489  *
490  * @return RT_EOK on successful.
491  */
rt_usbd_function_uac_speaker_create(udevice_t device)492 ufunction_t rt_usbd_function_uac_speaker_create(udevice_t device)
493 {
494     ufunction_t func;
495     uintf_t intf_ac, intf_as;
496     ualtsetting_t setting_as0;
497     ualtsetting_t setting_ac, setting_as;
498     struct uac_as_descriptor *as_desc_t;
499 
500     /* parameter check */
501     RT_ASSERT(device != RT_NULL);
502 
503 #ifdef RT_USB_DEVICE_COMPOSITE
504     rt_usbd_device_set_interface_string(device, SPK_INTF_STR_INDEX, _ustring[2]);
505 #else
506     /* set usb device string description */
507     rt_usbd_device_set_string(device, _ustring);
508 #endif
509     /* create a uac function */
510     func = rt_usbd_function_new(device, &dev_desc, &ops);
511     //not support HS
512     //rt_usbd_device_set_qualifier(device, &dev_qualifier);
513 
514     /* create interface */
515     intf_ac = rt_usbd_interface_new(device, RT_NULL);
516     intf_as = rt_usbd_interface_new(device, _interface_as_handler);
517 
518     /* create alternate setting */
519     setting_ac = rt_usbd_altsetting_new(sizeof(struct uac_ac_descriptor));
520     setting_as0 = rt_usbd_altsetting_new(sizeof(struct uinterface_descriptor));
521     setting_as = rt_usbd_altsetting_new(sizeof(struct uac_as_descriptor));
522     /* config desc in alternate setting */
523     rt_usbd_altsetting_config_descriptor(setting_ac, &ac_desc,
524                                          (rt_off_t) & ((struct uac_ac_descriptor *)0)->intf_desc);
525     rt_usbd_altsetting_config_descriptor(setting_as0, &as_desc0, 0);
526     rt_usbd_altsetting_config_descriptor(setting_as, &as_desc,
527                                          (rt_off_t) & ((struct uac_as_descriptor *)0)->intf_desc);
528     /* configure the uac interface descriptor */
529     _uac_descriptor_config(setting_ac->desc, intf_ac->intf_num, setting_as->desc, intf_as->intf_num);
530     _uac_samplerate_config(setting_as->desc, AUDIO_SAMPLERATE);
531 
532     /* create endpoint */
533     as_desc_t = (struct uac_as_descriptor *)setting_as->desc;
534     speaker.ep = rt_usbd_endpoint_new(&as_desc_t->ep_desc, _ep_data_handler);
535 
536     /* add the endpoint to the alternate setting */
537     rt_usbd_altsetting_add_endpoint(setting_as, speaker.ep);
538 
539     /* add the alternate setting to the interface, then set default setting of the interface */
540     rt_usbd_interface_add_altsetting(intf_ac, setting_ac);
541     rt_usbd_set_altsetting(intf_ac, 0);
542     rt_usbd_interface_add_altsetting(intf_as, setting_as0);
543     rt_usbd_interface_add_altsetting(intf_as, setting_as);
544     rt_usbd_set_altsetting(intf_as, 0);
545 
546     /* add the interface to the uac function */
547     rt_usbd_function_add_interface(func, intf_ac);
548     rt_usbd_function_add_interface(func, intf_as);
549 
550     return func;
551 }
552 
audio_speaker_init(void)553 int audio_speaker_init(void)
554 {
555     rt_thread_t speaker_tid;
556     speaker.event = rt_event_create("speaker_event", RT_IPC_FLAG_FIFO);
557 
558     speaker_tid = rt_thread_create("speaker_thread",
559                                    speaker_entry, RT_NULL,
560                                    1024,
561                                    5, 10);
562 
563     if (speaker_tid != RT_NULL)
564         rt_thread_startup(speaker_tid);
565     return RT_EOK;
566 }
567 INIT_COMPONENT_EXPORT(audio_speaker_init);
568 
569 /*
570  *  register uac class
571  */
572 static struct udclass uac_speaker_class =
573 {
574     .rt_usbd_function_create = rt_usbd_function_uac_speaker_create
575 };
576 
rt_usbd_uac_speaker_class_register(void)577 int rt_usbd_uac_speaker_class_register(void)
578 {
579     rt_usbd_class_register(&uac_speaker_class);
580     return 0;
581 }
582 INIT_PREV_EXPORT(rt_usbd_uac_speaker_class_register);
583