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 #ifndef USB_DDK_API_H 16 #define USB_DDK_API_H 17 18 /** 19 * @addtogroup UsbDdk 20 * @{ 21 * 22 * @brief Provides USB DDK APIs to open and close USB interfaces, perform non-isochronous and isochronous\n 23 * data transfer over USB pipes, and implement control transfer and interrupt transfer, etc. 24 * 25 * @syscap SystemCapability.Driver.USB.Extension 26 * @since 10 27 * @version 1.0 28 */ 29 30 /** 31 * @file usb_ddk_api.h 32 * 33 * @brief Declares the USB DDK APIs used by the USB host to access USB devices. 34 * 35 * @since 10 36 * @version 1.0 37 */ 38 39 #include <stdint.h> 40 41 #include "usb_ddk_types.h" 42 43 #ifdef __cplusplus 44 extern "C" { 45 #endif /* __cplusplus */ 46 47 /** 48 * @brief Initializes the DDK. 49 * 50 * @permission ohos.permission.ACCESS_DDK_USB 51 * @return <b>0</b> if the operation is successful; a negative value otherwise. 52 * @since 10 53 * @version 1.0 54 */ 55 int32_t OH_Usb_Init(void); 56 57 /** 58 * @brief Releases the DDK. 59 * 60 * @permission ohos.permission.ACCESS_DDK_USB 61 * @since 10 62 * @version 1.0 63 */ 64 void OH_Usb_Release(void); 65 66 /** 67 * @brief Obtains the USB device descriptor. 68 * 69 * @permission ohos.permission.ACCESS_DDK_USB 70 * @param deviceId ID of the device whose descriptor is to be obtained. 71 * @param desc Standard device descriptor defined in the USB protocol. 72 * @return <b>0</b> if the operation is successful; a negative value otherwise. 73 * @since 10 74 * @version 1.0 75 */ 76 int32_t OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc); 77 78 /** 79 * @brief Obtains the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n 80 * to release a descriptor after use. 81 * 82 * @permission ohos.permission.ACCESS_DDK_USB 83 * @param deviceId ID of the device whose configuration descriptor is to be obtained. 84 * @param configIndex Configuration index, which corresponds to <b>bConfigurationValue</b> in the USB protocol. 85 * @param config Configuration descriptor, which includes the standard configuration descriptor defined in the\n 86 * USB protocol and the associated interface descriptor and endpoint descriptor. 87 * @return <b>0</b> if the operation is successful; a negative value otherwise. 88 * @since 10 89 * @version 1.0 90 */ 91 int32_t OH_Usb_GetConfigDescriptor( 92 uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor ** const config); 93 94 /** 95 * @brief Releases the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n 96 * to release a descriptor after use. 97 * 98 * @permission ohos.permission.ACCESS_DDK_USB 99 * @param config Configuration descriptor obtained by calling <b>OH_Usb_GetConfigDescriptor</b>. 100 * @since 10 101 * @version 1.0 102 */ 103 void OH_Usb_FreeConfigDescriptor(struct UsbDdkConfigDescriptor * const config); 104 105 /** 106 * @brief Claims a USB interface. 107 * 108 * @permission ohos.permission.ACCESS_DDK_USB 109 * @param deviceId ID of the device to be operated. 110 * @param interfaceIndex Interface index, which corresponds to <b>bInterfaceNumber</b> in the USB protocol. 111 * @param interfaceHandle Interface operation handle. After the interface is claimed successfully, a value will be\n 112 * assigned to this parameter. 113 * @return <b>0</b> if the operation is successful; a negative value otherwise. 114 * @since 10 115 * @version 1.0 116 */ 117 int32_t OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle); 118 119 /** 120 * @brief Releases a USB interface. 121 * 122 * @permission ohos.permission.ACCESS_DDK_USB 123 * @param interfaceHandle Interface operation handle. 124 * @return <b>0</b> if the operation is successful; a negative value otherwise. 125 * @since 10 126 * @version 1.0 127 */ 128 int32_t OH_Usb_ReleaseInterface(uint64_t interfaceHandle); 129 130 /** 131 * @brief Activates the alternate setting of the USB interface. 132 * 133 * @permission ohos.permission.ACCESS_DDK_USB 134 * @param interfaceHandle Interface operation handle. 135 * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n 136 * in the USB protocol. 137 * @return <b>0</b> if the operation is successful; a negative value otherwise. 138 * @since 10 139 * @version 1.0 140 */ 141 int32_t OH_Usb_SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex); 142 143 /** 144 * @brief Obtains the activated alternate setting of the USB interface. 145 * 146 * @permission ohos.permission.ACCESS_DDK_USB 147 * @param interfaceHandle Interface operation handle. 148 * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n 149 * in the USB protocol. 150 * @return <b>0</b> if the operation is successful; a negative value otherwise. 151 * @since 10 152 * @version 1.0 153 */ 154 int32_t OH_Usb_GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t *settingIndex); 155 156 /** 157 * @brief Sends a control read transfer request. This API works in a synchronous manner. 158 * 159 * @permission ohos.permission.ACCESS_DDK_USB 160 * @param interfaceHandle Interface operation handle. 161 * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol. 162 * @param timeout Timeout duration, in milliseconds. 163 * @param data Data to be transferred. 164 * @param dataLen Data length. The return value indicates the length of the actually read data. 165 * @return <b>0</b> if the operation is successful; a negative value otherwise. 166 * @since 10 167 * @version 1.0 168 */ 169 int32_t OH_Usb_SendControlReadRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup, 170 uint32_t timeout, uint8_t *data, uint32_t *dataLen); 171 172 /** 173 * @brief Sends a control write transfer request. This API works in a synchronous manner. 174 * 175 * @permission ohos.permission.ACCESS_DDK_USB 176 * @param interfaceHandle Interface operation handle. 177 * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol. 178 * @param timeout Timeout duration, in milliseconds. 179 * @param data Data to be transferred. 180 * @param dataLen Data length. 181 * @return <b>0</b> if the operation is successful; a negative value otherwise. 182 * @since 10 183 * @version 1.0 184 */ 185 int32_t OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup, 186 uint32_t timeout, const uint8_t *data, uint32_t dataLen); 187 188 /** 189 * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n 190 * and bulk transfer. 191 * 192 * @permission ohos.permission.ACCESS_DDK_USB 193 * @param pipe Pipe used to transfer data. 194 * @param devMmap Device memory map, which can be obtained by calling <b>OH_Usb_CreateDeviceMemMap</b>. 195 * @return <b>0</b> if the operation is successful; a negative value otherwise. 196 * @since 10 197 * @version 1.0 198 */ 199 int32_t OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap); 200 201 /** 202 * @brief Creates a buffer. To avoid resource leakage, destroy a buffer by calling\n 203 * <b>OH_Usb_DestroyDeviceMemMap</b> after use. 204 * 205 * @permission ohos.permission.ACCESS_DDK_USB 206 * @param deviceId ID of the device for which the buffer is to be created. 207 * @param size Buffer size. 208 * @param devMmap Data memory map, through which the created buffer is returned to the caller. 209 * @return <b>0</b> if the operation is successful; a negative value otherwise. 210 * @since 10 211 * @version 1.0 212 */ 213 int32_t OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap); 214 215 /** 216 * @brief Destroys a buffer. To avoid resource leakage, destroy a buffer in time after use. 217 * 218 * @permission ohos.permission.ACCESS_DDK_USB 219 * @param devMmap Device memory map created by calling <b>OH_Usb_CreateDeviceMemMap</b>. 220 * @since 10 221 * @version 1.0 222 */ 223 void OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap); 224 /** @} */ 225 #ifdef __cplusplus 226 } 227 #endif /* __cplusplus */ 228 229 #endif // USB_DDK_API_H