• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 Releases the DDK.
75  *
76  * @permission ohos.permission.ACCESS_DDK_USB
77  * @return {@link USB_DDK_SUCCESS} the operation is successful.
78  *         {@link USB_DDK_NO_PERM} permission check failed.
79  * @since 18
80  * @version 1.0
81  */
82 int32_t OH_Usb_ReleaseResource(void);
83 
84 /**
85  * @brief Obtains the USB device descriptor.
86  *
87  * @permission ohos.permission.ACCESS_DDK_USB
88  * @param deviceId ID of the device whose descriptor is to be obtained.
89  * @param desc Standard device descriptor defined in the USB protocol.
90  * @return {@link USB_DDK_SUCCESS} the operation is successful.
91  *         {@link USB_DDK_NO_PERM} permission check failed.
92  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
93  *         {@link USB_DDK_INVALID_PARAMETER} desc is null.
94  * @since 10
95  * @version 1.0
96  */
97 int32_t OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc);
98 
99 /**
100  * @brief Obtains the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n
101  * to release a descriptor after use.
102  *
103  * @permission ohos.permission.ACCESS_DDK_USB
104  * @param deviceId ID of the device whose configuration descriptor is to be obtained.
105  * @param configIndex Configuration index, which corresponds to <b>bConfigurationValue</b> in the USB protocol.
106  * @param config Configuration descriptor, which includes the standard configuration descriptor defined in the\n
107  * USB protocol and the associated interface descriptor and endpoint descriptor.
108  * @return {@link USB_DDK_SUCCESS} the operation is successful.
109  *         {@link USB_DDK_NO_PERM} permission check failed.
110  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
111  *         {@link USB_DDK_INVALID_PARAMETER} config is null.
112  * @since 10
113  * @version 1.0
114  */
115 int32_t OH_Usb_GetConfigDescriptor(
116     uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor ** const config);
117 
118 /**
119  * @brief Releases the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n
120  * to release a descriptor after use.
121  *
122  * @permission ohos.permission.ACCESS_DDK_USB
123  * @param config Configuration descriptor obtained by calling <b>OH_Usb_GetConfigDescriptor</b>.
124  * @since 10
125  * @version 1.0
126  */
127 void OH_Usb_FreeConfigDescriptor(struct UsbDdkConfigDescriptor * const config);
128 
129 /**
130  * @brief Claims a USB interface.
131  *
132  * @permission ohos.permission.ACCESS_DDK_USB
133  * @param deviceId ID of the device to be operated.
134  * @param interfaceIndex Interface index, which corresponds to <b>bInterfaceNumber</b> in the USB protocol.
135  * @param interfaceHandle Interface operation handle. After the interface is claimed successfully, a value will be\n
136  * assigned to this parameter.
137  * @return {@link USB_DDK_SUCCESS} the operation is successful.
138  *         {@link USB_DDK_NO_PERM} permission check failed.
139  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
140  *         {@link USB_DDK_INVALID_PARAMETER} interfaceHandle is null.
141  * @since 10
142  * @version 1.0
143  */
144 int32_t OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle);
145 
146 /**
147  * @brief Releases a USB interface.
148  *
149  * @permission ohos.permission.ACCESS_DDK_USB
150  * @param interfaceHandle Interface operation handle.
151  * @return {@link USB_DDK_SUCCESS} the operation is successful.
152  *         {@link USB_DDK_NO_PERM} permission check failed.
153  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
154  * @since 10
155  * @version 1.0
156  */
157 int32_t OH_Usb_ReleaseInterface(uint64_t interfaceHandle);
158 
159 /**
160  * @brief Activates the alternate setting of the USB interface.
161  *
162  * @permission ohos.permission.ACCESS_DDK_USB
163  * @param interfaceHandle Interface operation handle.
164  * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n
165  * in the USB protocol.
166  * @return {@link USB_DDK_SUCCESS} the operation is successful.
167  *         {@link USB_DDK_NO_PERM} permission check failed.
168  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
169  * @since 10
170  * @version 1.0
171  */
172 int32_t OH_Usb_SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex);
173 
174 /**
175  * @brief Obtains the activated alternate setting of the USB interface.
176  *
177  * @permission ohos.permission.ACCESS_DDK_USB
178  * @param interfaceHandle Interface operation handle.
179  * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n
180  * in the USB protocol.
181  * @return {@link USB_DDK_SUCCESS} the operation is successful.
182  *         {@link USB_DDK_NO_PERM} permission check failed.
183  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
184  *         {@link USB_DDK_INVALID_PARAMETER} settingIndex is null.
185  * @since 10
186  * @version 1.0
187  */
188 int32_t OH_Usb_GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t *settingIndex);
189 
190 /**
191  * @brief Sends a control read transfer request. This API works in a synchronous manner.
192  *
193  * @permission ohos.permission.ACCESS_DDK_USB
194  * @param interfaceHandle Interface operation handle.
195  * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol.
196  * @param timeout Timeout duration, in milliseconds.
197  * @param data Data to be transferred.
198  * @param dataLen Data length. The return value indicates the length of the actually read data.
199  * @return {@link USB_DDK_SUCCESS} the operation is successful.
200  *         {@link USB_DDK_NO_PERM} permission check failed.
201  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
202  *         {@link USB_DDK_INVALID_PARAMETER} setup is null or data is null or dataLen is null or dataLen is less than\n
203  *         size of the read data.
204  *         {@link USB_DDK_MEMORY_ERROR} the memory of read data copies failed.
205  * @since 10
206  * @version 1.0
207  */
208 int32_t OH_Usb_SendControlReadRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,
209     uint32_t timeout, uint8_t *data, uint32_t *dataLen);
210 
211 /**
212  * @brief Sends a control write transfer request. This API works in a synchronous manner.
213  *
214  * @permission ohos.permission.ACCESS_DDK_USB
215  * @param interfaceHandle Interface operation handle.
216  * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol.
217  * @param timeout Timeout duration, in milliseconds.
218  * @param data Data to be transferred.
219  * @param dataLen Data length.
220  * @return {@link USB_DDK_SUCCESS} the operation is successful.
221  *         {@link USB_DDK_NO_PERM} permission check failed.
222  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
223  *         {@link USB_DDK_INVALID_PARAMETER} setup is null or data is null.
224  * @since 10
225  * @version 1.0
226  */
227 int32_t OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,
228     uint32_t timeout, const uint8_t *data, uint32_t dataLen);
229 
230 /**
231  * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n
232  * and bulk transfer.
233  *
234  * @permission ohos.permission.ACCESS_DDK_USB
235  * @param pipe Pipe used to transfer data.
236  * @param devMmap Device memory map, which can be obtained by calling <b>OH_Usb_CreateDeviceMemMap</b>.
237  * @return {@link USB_DDK_SUCCESS} the operation is successful.
238  *         {@link USB_DDK_NO_PERM} permission check failed.
239  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
240  *         {@link USB_DDK_INVALID_PARAMETER} pipe is null or devMmap is null or address of devMmap is null.
241  * @since 10
242  * @version 1.0
243  */
244 int32_t OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap);
245 
246 /**
247  * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n
248  * and bulk transfer.
249  *
250  * @permission ohos.permission.ACCESS_DDK_USB
251  * @param pipe Pipe used to transfer data.
252  * @param ashmem Shared memory, which can be obtained by calling <b>OH_DDK_CreateAshmem</b>.
253  * @return {@link USB_DDK_SUCCESS} the operation is successful.
254  *         {@link USB_DDK_NO_PERM} permission check failed.
255  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
256  *         {@link USB_DDK_INVALID_PARAMETER} pipe is null or ashmem is null or address of ashmem is null.
257  * @since 12
258  */
259 int32_t OH_Usb_SendPipeRequestWithAshmem(const struct UsbRequestPipe *pipe, DDK_Ashmem *ashmem);
260 
261 /**
262  * @brief Creates a buffer. To avoid resource leakage, destroy a buffer by calling\n
263  * <b>OH_Usb_DestroyDeviceMemMap</b> after use.
264  *
265  * @permission ohos.permission.ACCESS_DDK_USB
266  * @param deviceId ID of the device for which the buffer is to be created.
267  * @param size Buffer size.
268  * @param devMmap Data memory map, through which the created buffer is returned to the caller.
269  * @return {@link USB_DDK_SUCCESS} the operation is successful.
270  *         {@link USB_DDK_NO_PERM} permission check failed.
271  *         {@link USB_DDK_INVALID_PARAMETER} devMmap is null.
272  *         {@link USB_DDK_MEMORY_ERROR} mmap failed or alloc memory of devMmap failed.
273  * @since 10
274  * @version 1.0
275  */
276 int32_t OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap);
277 
278 /**
279  * @brief Destroys a buffer. To avoid resource leakage, destroy a buffer in time after use.
280  *
281  * @permission ohos.permission.ACCESS_DDK_USB
282  * @param devMmap Device memory map created by calling <b>OH_Usb_CreateDeviceMemMap</b>.
283  * @since 10
284  * @version 1.0
285  */
286 void OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap);
287 
288 /**
289  * @brief Obtain USB devices.
290  *
291  * @permission ohos.permission.ACCESS_DDK_USB
292  * @param devices USB device array.
293  * @return {@link USB_DDK_SUCCESS} the operation is successful.
294  *         {@link USB_DDK_NO_PERM} permission check failed.
295  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
296  *         {@link USB_DDK_INVALID_PARAMETER} devices is null.
297  * @since 18
298  */
299 int32_t OH_Usb_GetDevices(struct Usb_DeviceArray *devices);
300 /** @} */
301 #ifdef __cplusplus
302 }
303 #endif /* __cplusplus */
304 
305 #endif // USB_DDK_API_H