1beacf11bSopenharmony_ci/* ----------------------------------------------------------------------------
2beacf11bSopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
3beacf11bSopenharmony_ci * Description: LiteOS USB Driver UAC Protocol HeadFile
4beacf11bSopenharmony_ci * Author: huangjieliang
5beacf11bSopenharmony_ci * Create: 2017-12-12
6beacf11bSopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
7beacf11bSopenharmony_ci * are permitted provided that the following conditions are met:
8beacf11bSopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
9beacf11bSopenharmony_ci * conditions and the following disclaimer.
10beacf11bSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11beacf11bSopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials
12beacf11bSopenharmony_ci * provided with the distribution.
13beacf11bSopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14beacf11bSopenharmony_ci * to endorse or promote products derived from this software without specific prior written
15beacf11bSopenharmony_ci * permission.
16beacf11bSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17beacf11bSopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18beacf11bSopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19beacf11bSopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20beacf11bSopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21beacf11bSopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22beacf11bSopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23beacf11bSopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24beacf11bSopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25beacf11bSopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26beacf11bSopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27beacf11bSopenharmony_ci * --------------------------------------------------------------------------- */
28beacf11bSopenharmony_ci/* ----------------------------------------------------------------------------
29beacf11bSopenharmony_ci * Notice of Export Control Law
30beacf11bSopenharmony_ci * ===============================================
31beacf11bSopenharmony_ci * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
32beacf11bSopenharmony_ci * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
33beacf11bSopenharmony_ci * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
34beacf11bSopenharmony_ci * applicable export control laws and regulations.
35beacf11bSopenharmony_ci * --------------------------------------------------------------------------- */
36beacf11bSopenharmony_ci
37beacf11bSopenharmony_ci#ifndef _F_UAC_GADGET_H
38beacf11bSopenharmony_ci#define _F_UAC_GADGET_H
39beacf11bSopenharmony_ci
40beacf11bSopenharmony_ci#include "gadget/usbdev.h"
41beacf11bSopenharmony_ci#include "gadget/composite.h"
42beacf11bSopenharmony_ci
43beacf11bSopenharmony_ci#ifdef __cplusplus
44beacf11bSopenharmony_ci#if __cplusplus
45beacf11bSopenharmony_ciextern "C" {
46beacf11bSopenharmony_ci#endif /* __cplusplus */
47beacf11bSopenharmony_ci#endif /* __cplusplus */
48beacf11bSopenharmony_ci
49beacf11bSopenharmony_cistruct uac_dev_s
50beacf11bSopenharmony_ci{
51beacf11bSopenharmony_ci  uint8_t            *descs;   /* pointer to the concatenated descriptors */
52beacf11bSopenharmony_ci  uint32_t            desl;    /* length of all descriptors */
53beacf11bSopenharmony_ci  uint32_t            control; /* Class-specific control request value */
54beacf11bSopenharmony_ci  struct usbdev_ep_s *out_ep;  /* control transfer endpoint */
55beacf11bSopenharmony_ci  struct usbdev_ep_s *in_ep;   /* Audio Streaming endpoint */
56beacf11bSopenharmony_ci  bool out_ep_enabled;
57beacf11bSopenharmony_ci  bool in_ep_enabled;
58beacf11bSopenharmony_ci
59beacf11bSopenharmony_ci  struct usbdev_req_s ctrlreq;   /* control request */
60beacf11bSopenharmony_ci  struct usbdev_req_s outputreq; /* Audio Control request */
61beacf11bSopenharmony_ci  struct usbdev_req_s inputreq;  /* Audio Streaming request */
62beacf11bSopenharmony_ci  uint32_t            dyn_fc;    /* dynamic format change, non-zero indicates that format change occurred */
63beacf11bSopenharmony_ci
64beacf11bSopenharmony_ci  volatile int connected;        /* Connected to USB host ? */
65beacf11bSopenharmony_ci  int          count;
66beacf11bSopenharmony_ci  uint32_t     fid;              /* Frame Identifier */
67beacf11bSopenharmony_ci
68beacf11bSopenharmony_ci  struct list_head       uac_queue;
69beacf11bSopenharmony_ci  int                    uac_queue_len;
70beacf11bSopenharmony_ci  volatile atomic_t      busy_flag;
71beacf11bSopenharmony_ci  struct uac_queue_node *cur_node;
72beacf11bSopenharmony_ci  spinlock_t             lock;
73beacf11bSopenharmony_ci};
74beacf11bSopenharmony_ci
75beacf11bSopenharmony_cistruct uac_driver_s
76beacf11bSopenharmony_ci{
77beacf11bSopenharmony_ci  struct usbdevclass_driver_s  drvr;
78beacf11bSopenharmony_ci  struct uac_dev_s            *dev;
79beacf11bSopenharmony_ci};
80beacf11bSopenharmony_ci
81beacf11bSopenharmony_ci/* private UAC device driver instance structure */
82beacf11bSopenharmony_ci
83beacf11bSopenharmony_cistruct uac_softc
84beacf11bSopenharmony_ci{
85beacf11bSopenharmony_ci  struct uac_dev_s    dev;
86beacf11bSopenharmony_ci  struct uac_driver_s drvr;
87beacf11bSopenharmony_ci};
88beacf11bSopenharmony_ci
89beacf11bSopenharmony_cistruct uac_ac_header_descriptor
90beacf11bSopenharmony_ci{
91beacf11bSopenharmony_ci  uint8_t  bLength;
92beacf11bSopenharmony_ci  uint8_t  bDescriptorType;
93beacf11bSopenharmony_ci  uint8_t  bDescriptorSubtype;
94beacf11bSopenharmony_ci
95beacf11bSopenharmony_ci  uint16_t bcdADC;
96beacf11bSopenharmony_ci  uint16_t wTotalLength;
97beacf11bSopenharmony_ci
98beacf11bSopenharmony_ci  uint8_t  bInCollection;
99beacf11bSopenharmony_ci  uint8_t  baInterfaceNr[2];
100beacf11bSopenharmony_ci} __attribute__((packed));
101beacf11bSopenharmony_ci
102beacf11bSopenharmony_cistruct uac1_input_terminal_descriptor
103beacf11bSopenharmony_ci{
104beacf11bSopenharmony_ci  uint8_t  bLength;            /* in bytes: 12 */
105beacf11bSopenharmony_ci  uint8_t  bDescriptorType;    /* CS_INTERFACE descriptor type */
106beacf11bSopenharmony_ci  uint8_t  bDescriptorSubtype; /* INPUT_TERMINAL descriptor subtype */
107beacf11bSopenharmony_ci  uint8_t  bTerminalID;        /* Constant uniquely terminal ID */
108beacf11bSopenharmony_ci  uint16_t wTerminalType;      /* USB Audio Terminal Types */
109beacf11bSopenharmony_ci  uint8_t  bAssocTerminal;     /* ID of the Output Terminal associated */
110beacf11bSopenharmony_ci  uint8_t  bNrChannels;        /* Number of logical output channels */
111beacf11bSopenharmony_ci  uint16_t wChannelConfig;
112beacf11bSopenharmony_ci  uint8_t  iChannelNames;
113beacf11bSopenharmony_ci  uint8_t  iTerminal;
114beacf11bSopenharmony_ci} __attribute__ ((packed));
115beacf11bSopenharmony_ci
116beacf11bSopenharmony_ci#define UAC_DT_INPUT_TERMINAL_SIZE     12
117beacf11bSopenharmony_ci#define UAC_DT_OUTPUT_TERMINAL_SIZE    9
118beacf11bSopenharmony_ci#define UAC_INPUT_TERMINAL_MICROPHONE  0x201
119beacf11bSopenharmony_ci
120beacf11bSopenharmony_cistruct uac_output_terminal_descriptor
121beacf11bSopenharmony_ci{
122beacf11bSopenharmony_ci  uint8_t  bLength;            /* in bytes: 9 */
123beacf11bSopenharmony_ci  uint8_t  bDescriptorType;    /* CS_INTERFACE descriptor type */
124beacf11bSopenharmony_ci  uint8_t  bDescriptorSubtype; /* OUTPUT_TERMINAL descriptor subtype */
125beacf11bSopenharmony_ci  uint8_t  bTerminalID;        /* Constant uniquely terminal ID */
126beacf11bSopenharmony_ci  uint16_t wTerminalType;      /* USB Audio Terminal Types */
127beacf11bSopenharmony_ci  uint8_t  bAssocTerminal;     /* ID of the Input Terminal associated */
128beacf11bSopenharmony_ci  uint8_t  bSourceID;          /* ID of the connected Unit or Terminal*/
129beacf11bSopenharmony_ci  uint8_t  iTerminal;
130beacf11bSopenharmony_ci} __attribute__ ((packed));
131beacf11bSopenharmony_ci
132beacf11bSopenharmony_ci/* 4.5.2 Class-Specific AS Interface Descriptor */
133beacf11bSopenharmony_ci
134beacf11bSopenharmony_cistruct uac_as_header_descriptor
135beacf11bSopenharmony_ci{
136beacf11bSopenharmony_ci  uint8_t  bLength;                   /* in bytes: 7 */
137beacf11bSopenharmony_ci  uint8_t  bDescriptorType;           /* USB_DT_CS_INTERFACE */
138beacf11bSopenharmony_ci  uint8_t  bDescriptorSubtype;        /* AS_GENERAL */
139beacf11bSopenharmony_ci  uint8_t  bTerminalLink;             /* Terminal ID of connected Terminal */
140beacf11bSopenharmony_ci  uint8_t  bDelay;                    /* Delay introduced by the data path */
141beacf11bSopenharmony_ci  uint16_t wFormatTag;                /* The Audio Data Format */
142beacf11bSopenharmony_ci} __attribute__((packed));
143beacf11bSopenharmony_ci
144beacf11bSopenharmony_ci/* Audio Class-Specific AS Interface Descriptor Subtypes */
145beacf11bSopenharmony_ci
146beacf11bSopenharmony_ci#define UAC_AS_GENERAL                     0x01
147beacf11bSopenharmony_ci#define UAC_FORMAT_TYPE                    0x02
148beacf11bSopenharmony_ci#define UAC_FORMAT_SPECIFIC                0x03
149beacf11bSopenharmony_ci
150beacf11bSopenharmony_cistruct uac_format_type_i_discrete_descriptor
151beacf11bSopenharmony_ci{
152beacf11bSopenharmony_ci  uint8_t  bLength;
153beacf11bSopenharmony_ci  uint8_t  bDescriptorType;
154beacf11bSopenharmony_ci  uint8_t  bDescriptorSubtype;
155beacf11bSopenharmony_ci  uint8_t  bFormatType;
156beacf11bSopenharmony_ci  uint8_t  bNrChannels;
157beacf11bSopenharmony_ci  uint8_t  bSubframeSize;
158beacf11bSopenharmony_ci  uint8_t  bBitResolution;
159beacf11bSopenharmony_ci  uint8_t  bSamFreqType;
160beacf11bSopenharmony_ci  uint8_t  tSamFreq[4][3];
161beacf11bSopenharmony_ci} __attribute__ ((packed));
162beacf11bSopenharmony_ci
163beacf11bSopenharmony_cistruct uac_endpoint_descriptor
164beacf11bSopenharmony_ci{
165beacf11bSopenharmony_ci  uByte    bLength;
166beacf11bSopenharmony_ci  uByte    bDescriptorType;
167beacf11bSopenharmony_ci  uByte    bEndpointAddress;
168beacf11bSopenharmony_ci  uByte    bmAttributes;
169beacf11bSopenharmony_ci  uWord    wMaxPacketSize;
170beacf11bSopenharmony_ci  uByte    bInterval;
171beacf11bSopenharmony_ci  uByte    bRefresh;
172beacf11bSopenharmony_ci  uByte    bSynchAddress;
173beacf11bSopenharmony_ci} __attribute__((packed));
174beacf11bSopenharmony_ci
175beacf11bSopenharmony_cistruct uac2_iso_endpoint_descriptor
176beacf11bSopenharmony_ci{
177beacf11bSopenharmony_ci  uint8_t  bLength;            /* in bytes: 8 */
178beacf11bSopenharmony_ci  uint8_t  bDescriptorType;    /* USB_DT_CS_ENDPOINT */
179beacf11bSopenharmony_ci  uint8_t  bDescriptorSubtype; /* EP_GENERAL */
180beacf11bSopenharmony_ci  uint8_t  bmAttributes;
181beacf11bSopenharmony_ci  uint8_t  bmControls;
182beacf11bSopenharmony_ci  uint8_t  bLockDelayUnits;
183beacf11bSopenharmony_ci  uint16_t wLockDelay;
184beacf11bSopenharmony_ci} __attribute__((packed));
185beacf11bSopenharmony_ci
186beacf11bSopenharmony_ci/* 4.3.2.5 Feature Unit Descriptor */
187beacf11bSopenharmony_ci
188beacf11bSopenharmony_cistruct uac_feature_unit_descriptor
189beacf11bSopenharmony_ci{
190beacf11bSopenharmony_ci  uint8_t  bLength;
191beacf11bSopenharmony_ci  uint8_t  bDescriptorType;
192beacf11bSopenharmony_ci  uint8_t  bDescriptorSubtype;
193beacf11bSopenharmony_ci  uint8_t  bUnitID;
194beacf11bSopenharmony_ci  uint8_t  bSourceID;
195beacf11bSopenharmony_ci  uint8_t  bControlSize;
196beacf11bSopenharmony_ci  uint16_t bmaControls[1]; /* variable length */
197beacf11bSopenharmony_ci  uint8_t  iFeature;
198beacf11bSopenharmony_ci} __attribute__((packed));
199beacf11bSopenharmony_ci
200beacf11bSopenharmony_ci/*
201beacf11bSopenharmony_ci * UAC Audio Class-specific Descriptor Types, UAC spec. 1.0
202beacf11bSopenharmony_ci */
203beacf11bSopenharmony_ci
204beacf11bSopenharmony_ci#define UAC_CS_UNDEFINED            0x20
205beacf11bSopenharmony_ci#define UAC_CS_DEVICE               0x21
206beacf11bSopenharmony_ci#define UAC_CS_CONFIGURATION        0x22
207beacf11bSopenharmony_ci#define UAC_CS_STRING               0x23
208beacf11bSopenharmony_ci#define UAC_CS_INTERFACE            0x24
209beacf11bSopenharmony_ci#define UAC_CS_ENDPOINT             0x25
210beacf11bSopenharmony_ci
211beacf11bSopenharmony_ci/*
212beacf11bSopenharmony_ci * VideoStreaming Interface Control Selectors, UAC spec. 1.0
213beacf11bSopenharmony_ci */
214beacf11bSopenharmony_ci
215beacf11bSopenharmony_ci#define UAC_VS_CONTROL_UNDEFINED                   0x00
216beacf11bSopenharmony_ci#define UAC_VS_PROBE_CONTROL                       0x01
217beacf11bSopenharmony_ci#define UAC_VS_COMMIT_CONTROL                      0x02
218beacf11bSopenharmony_ci#define UAC_VS_STILL_PROBE_CONTROL                 0x03
219beacf11bSopenharmony_ci#define UAC_VS_STILL_COMMIT_CONTROL                0x04
220beacf11bSopenharmony_ci#define UAC_VS_STILL_IMAGE_TRIGGER_CONTROL         0x05
221beacf11bSopenharmony_ci#define UAC_VS_STREAM_ERROR_CODE_CONTROL           0x06
222beacf11bSopenharmony_ci#define UAC_VS_GENERATE_KEY_FRAME_CONTROL          0x07
223beacf11bSopenharmony_ci#define UAC_VS_UPDATE_FRAME_SEGMENT_CONTROL        0x08
224beacf11bSopenharmony_ci#define UAC_VS_SYNCH_DELAY_CONTROL                 0x09
225beacf11bSopenharmony_ci
226beacf11bSopenharmony_ci#define UAC_SETCUR_COMPLETE 0xfe
227beacf11bSopenharmony_ci
228beacf11bSopenharmony_ci#define USB_ENDPOINT_XFER_SYNC        4
229beacf11bSopenharmony_ci
230beacf11bSopenharmony_ci/* Audio Class-Specific AC Interface Descriptor Subtypes */
231beacf11bSopenharmony_ci
232beacf11bSopenharmony_ci#define UAC_HEADER                      0x01
233beacf11bSopenharmony_ci#define UAC_INPUT_TERMINAL              0x02
234beacf11bSopenharmony_ci#define UAC_OUTPUT_TERMINAL             0x03
235beacf11bSopenharmony_ci#define UAC_MIXER_UNIT                  0x04
236beacf11bSopenharmony_ci#define UAC_SELECTOR_UNIT               0x05
237beacf11bSopenharmony_ci#define UAC_FEATURE_UNIT                0x06
238beacf11bSopenharmony_ci#define UAC1_PROCESSING_UNIT            0x07
239beacf11bSopenharmony_ci#define UAC1_EXTENSION_UNIT             0x08
240beacf11bSopenharmony_ci
241beacf11bSopenharmony_ci/* Audio Class-Specific Request Codes */
242beacf11bSopenharmony_ci
243beacf11bSopenharmony_ci#define SET_CUR_UAC                     0x1
244beacf11bSopenharmony_ci#define GET_CUR_UAC                     0x81
245beacf11bSopenharmony_ci#define SET_MIN_UAC                     0x2
246beacf11bSopenharmony_ci#define GET_MIN_UAC                     0x82
247beacf11bSopenharmony_ci#define SET_MAX_UAC                     0x3
248beacf11bSopenharmony_ci#define GET_MAX_UAC                     0x83
249beacf11bSopenharmony_ci#define SET_RES_UAC                     0x4
250beacf11bSopenharmony_ci#define GET_RES_UAC                     0x84
251beacf11bSopenharmony_ci#define SET_MEM_UAC                     0x5
252beacf11bSopenharmony_ci#define GET_MEM_UAC                     0x85
253beacf11bSopenharmony_ci
254beacf11bSopenharmony_cistruct uac_opts
255beacf11bSopenharmony_ci{
256beacf11bSopenharmony_ci  int c_chmask; /* Capture channel mask. */
257beacf11bSopenharmony_ci  int c_srate;  /* Capture default sample rate. */
258beacf11bSopenharmony_ci  int c_ssize;  /* Capture default sample size. */
259beacf11bSopenharmony_ci  int p_chmask; /* Playback channel mask. */
260beacf11bSopenharmony_ci  int p_srate;  /* Playback default sample rate. */
261beacf11bSopenharmony_ci  int p_ssize;  /* Playback default sample size. */
262beacf11bSopenharmony_ci};
263beacf11bSopenharmony_ci
264beacf11bSopenharmony_ci#define UAC_CS_INTERFACE                0x24
265beacf11bSopenharmony_ci
266beacf11bSopenharmony_ci#define HSETM(_var, _val) _var = { (uint8_t)(_val), (uint8_t)(_val >> 0x8), (uint8_t)(_val >> 16) }
267beacf11bSopenharmony_ci
268beacf11bSopenharmony_ciextern void usbdev_uac_initialize_sub(struct composite_devdesc_s *dev, int ifnobase, int minor);
269beacf11bSopenharmony_ciextern struct uac_endpoint_descriptor g_fuac_as_in_ep_desc;
270beacf11bSopenharmony_ciextern size_t g_uac_iso_data_size;
271beacf11bSopenharmony_ci
272beacf11bSopenharmony_ciextern int fuac_opts_set(struct uac_opts *opts);
273beacf11bSopenharmony_ciextern uint32_t fuac_rate_get(void);
274beacf11bSopenharmony_ci
275beacf11bSopenharmony_ci#ifdef __cplusplus
276beacf11bSopenharmony_ci#if __cplusplus
277beacf11bSopenharmony_ci}
278beacf11bSopenharmony_ci#endif /* __cplusplus */
279beacf11bSopenharmony_ci#endif /* __cplusplus */
280beacf11bSopenharmony_ci
281beacf11bSopenharmony_ci#endif