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