1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup UsbDdk 18 * @{ 19 * 20 * @brief Provides USB DDK APIs to open and close USB interfaces, perform non-isochronous and isochronous\n 21 * data transfer over USB pipes, and implement control transfer and interrupt transfer, etc. 22 * 23 * @kit DriverDevelopmentKit 24 * @syscap SystemCapability.Driver.USB.Extension 25 * @since 10 26 * @version 1.0 27 */ 28 29 /** 30 * @file usb_ddk_api.h 31 * 32 * @brief Declares the USB DDK APIs used by the USB host to access USB devices. 33 * 34 * @kit DriverDevelopmentKit 35 * @library libusb_ndk.z.so 36 * @syscap SystemCapability.Driver.USB.Extension 37 * @since 10 38 * @version 1.0 39 */ 40 41 #ifndef USB_DDK_API_H 42 #define USB_DDK_API_H 43 44 #include <stdint.h> 45 46 #include "ddk_types.h" 47 #include "usb_ddk_types.h" 48 49 #ifdef __cplusplus 50 extern "C" { 51 #endif /* __cplusplus */ 52 53 /** 54 * @brief Initializes the DDK. 55 * 56 * @permission ohos.permission.ACCESS_DDK_USB 57 * @return {@link USB_DDK_SUCCESS} the operation is successful. 58 * {@link USB_DDK_FAILED} permission check failed or connect usb ddk service failed or internal error failed. 59 * @since 10 60 * @version 1.0 61 */ 62 int32_t OH_Usb_Init(void); 63 64 /** 65 * @brief Releases the DDK. 66 * 67 * @permission ohos.permission.ACCESS_DDK_USB 68 * @since 10 69 * @version 1.0 70 */ 71 void OH_Usb_Release(void); 72 73 /** 74 * @brief Obtains the USB device descriptor. 75 * 76 * @permission ohos.permission.ACCESS_DDK_USB 77 * @param deviceId ID of the device whose descriptor is to be obtained. 78 * @param desc Standard device descriptor defined in the USB protocol. 79 * @return {@link USB_DDK_SUCCESS} the operation is successful. 80 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 81 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 82 * {@link USB_DDK_INVALID_PARAMETER} desc is null. 83 * @since 10 84 * @version 1.0 85 */ 86 int32_t OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc); 87 88 /** 89 * @brief Obtains the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n 90 * to release a descriptor after use. 91 * 92 * @permission ohos.permission.ACCESS_DDK_USB 93 * @param deviceId ID of the device whose configuration descriptor is to be obtained. 94 * @param configIndex Configuration index, which corresponds to <b>bConfigurationValue</b> in the USB protocol. 95 * @param config Configuration descriptor, which includes the standard configuration descriptor defined in the\n 96 * USB protocol and the associated interface descriptor and endpoint descriptor. 97 * @return {@link USB_DDK_SUCCESS} the operation is successful. 98 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 99 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 100 * {@link USB_DDK_INVALID_PARAMETER} config is null. 101 * @since 10 102 * @version 1.0 103 */ 104 int32_t OH_Usb_GetConfigDescriptor( 105 uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor ** const config); 106 107 /** 108 * @brief Releases the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n 109 * to release a descriptor after use. 110 * 111 * @permission ohos.permission.ACCESS_DDK_USB 112 * @param config Configuration descriptor obtained by calling <b>OH_Usb_GetConfigDescriptor</b>. 113 * @since 10 114 * @version 1.0 115 */ 116 void OH_Usb_FreeConfigDescriptor(struct UsbDdkConfigDescriptor * const config); 117 118 /** 119 * @brief Claims a USB interface. 120 * 121 * @permission ohos.permission.ACCESS_DDK_USB 122 * @param deviceId ID of the device to be operated. 123 * @param interfaceIndex Interface index, which corresponds to <b>bInterfaceNumber</b> in the USB protocol. 124 * @param interfaceHandle Interface operation handle. After the interface is claimed successfully, a value will be\n 125 * assigned to this parameter. 126 * @return {@link USB_DDK_SUCCESS} the operation is successful. 127 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 128 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 129 * {@link USB_DDK_INVALID_PARAMETER} interfaceHandle is null. 130 * @since 10 131 * @version 1.0 132 */ 133 int32_t OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle); 134 135 /** 136 * @brief Releases a USB interface. 137 * 138 * @permission ohos.permission.ACCESS_DDK_USB 139 * @param interfaceHandle Interface operation handle. 140 * @return {@link USB_DDK_SUCCESS} the operation is successful. 141 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 142 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 143 * @since 10 144 * @version 1.0 145 */ 146 int32_t OH_Usb_ReleaseInterface(uint64_t interfaceHandle); 147 148 /** 149 * @brief Activates the alternate setting of the USB interface. 150 * 151 * @permission ohos.permission.ACCESS_DDK_USB 152 * @param interfaceHandle Interface operation handle. 153 * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n 154 * in the USB protocol. 155 * @return {@link USB_DDK_SUCCESS} the operation is successful. 156 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 157 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 158 * @since 10 159 * @version 1.0 160 */ 161 int32_t OH_Usb_SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex); 162 163 /** 164 * @brief Obtains the activated alternate setting of the USB interface. 165 * 166 * @permission ohos.permission.ACCESS_DDK_USB 167 * @param interfaceHandle Interface operation handle. 168 * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n 169 * in the USB protocol. 170 * @return {@link USB_DDK_SUCCESS} the operation is successful. 171 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 172 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 173 * {@link USB_DDK_INVALID_PARAMETER} settingIndex is null. 174 * @since 10 175 * @version 1.0 176 */ 177 int32_t OH_Usb_GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t *settingIndex); 178 179 /** 180 * @brief Sends a control read transfer request. This API works in a synchronous manner. 181 * 182 * @permission ohos.permission.ACCESS_DDK_USB 183 * @param interfaceHandle Interface operation handle. 184 * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol. 185 * @param timeout Timeout duration, in milliseconds. 186 * @param data Data to be transferred. 187 * @param dataLen Data length. The return value indicates the length of the actually read data. 188 * @return {@link USB_DDK_SUCCESS} the operation is successful. 189 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 190 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 191 * {@link USB_DDK_INVALID_PARAMETER} setup is null or data is null or dataLen is null or dataLen is less than\n 192 * size of the read data. 193 * {@link USB_DDK_MEMORY_ERROR} the memory of read data copies failed. 194 * @since 10 195 * @version 1.0 196 */ 197 int32_t OH_Usb_SendControlReadRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup, 198 uint32_t timeout, uint8_t *data, uint32_t *dataLen); 199 200 /** 201 * @brief Sends a control write transfer request. This API works in a synchronous manner. 202 * 203 * @permission ohos.permission.ACCESS_DDK_USB 204 * @param interfaceHandle Interface operation handle. 205 * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol. 206 * @param timeout Timeout duration, in milliseconds. 207 * @param data Data to be transferred. 208 * @param dataLen Data length. 209 * @return {@link USB_DDK_SUCCESS} the operation is successful. 210 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 211 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 212 * {@link USB_DDK_INVALID_PARAMETER} setup is null or data is null. 213 * @since 10 214 * @version 1.0 215 */ 216 int32_t OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup, 217 uint32_t timeout, const uint8_t *data, uint32_t dataLen); 218 219 /** 220 * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n 221 * and bulk transfer. 222 * 223 * @permission ohos.permission.ACCESS_DDK_USB 224 * @param pipe Pipe used to transfer data. 225 * @param devMmap Device memory map, which can be obtained by calling <b>OH_Usb_CreateDeviceMemMap</b>. 226 * @return {@link USB_DDK_SUCCESS} the operation is successful. 227 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 228 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 229 * {@link USB_DDK_INVALID_PARAMETER} pipe is null or devMmap is null or address of devMmap is null. 230 * @since 10 231 * @version 1.0 232 */ 233 int32_t OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap); 234 235 /** 236 * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n 237 * and bulk transfer. 238 * 239 * @permission ohos.permission.ACCESS_DDK_USB 240 * @param pipe Pipe used to transfer data. 241 * @param ashmem Shared memory, which can be obtained by calling <b>OH_DDK_CreateAshmem</b>. 242 * @return {@link USB_DDK_SUCCESS} the operation is successful. 243 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 244 * {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed. 245 * {@link USB_DDK_INVALID_PARAMETER} pipe is null or ashmem is null or address of ashmem is null. 246 * @since 12 247 */ 248 int32_t OH_Usb_SendPipeRequestWithAshmem(const struct UsbRequestPipe *pipe, DDK_Ashmem *ashmem); 249 250 /** 251 * @brief Creates a buffer. To avoid resource leakage, destroy a buffer by calling\n 252 * <b>OH_Usb_DestroyDeviceMemMap</b> after use. 253 * 254 * @permission ohos.permission.ACCESS_DDK_USB 255 * @param deviceId ID of the device for which the buffer is to be created. 256 * @param size Buffer size. 257 * @param devMmap Data memory map, through which the created buffer is returned to the caller. 258 * @return {@link USB_DDK_SUCCESS} the operation is successful. 259 * {@link USB_DDK_FAILED} permission check failed or internal error failed. 260 * {@link USB_DDK_INVALID_PARAMETER} devMmap is null. 261 * {@link USB_DDK_MEMORY_ERROR} mmap failed or alloc memory of devMmap failed. 262 * @since 10 263 * @version 1.0 264 */ 265 int32_t OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap); 266 267 /** 268 * @brief Destroys a buffer. To avoid resource leakage, destroy a buffer in time after use. 269 * 270 * @permission ohos.permission.ACCESS_DDK_USB 271 * @param devMmap Device memory map created by calling <b>OH_Usb_CreateDeviceMemMap</b>. 272 * @since 10 273 * @version 1.0 274 */ 275 void OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap); 276 /** @} */ 277 #ifdef __cplusplus 278 } 279 #endif /* __cplusplus */ 280 281 #endif // USB_DDK_API_H