• 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 #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 "ddk_types.h"
42 #include "usb_ddk_types.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif /* __cplusplus */
47 
48 /**
49  * @brief Initializes the DDK.
50  *
51  * @permission ohos.permission.ACCESS_DDK_USB
52  * @return <b>0</b> if the operation is successful; a negative value otherwise.
53  * @since 10
54  * @version 1.0
55  */
56 int32_t OH_Usb_Init(void);
57 
58 /**
59  * @brief Releases the DDK.
60  *
61  * @permission ohos.permission.ACCESS_DDK_USB
62  * @since 10
63  * @version 1.0
64  */
65 void OH_Usb_Release(void);
66 
67 /**
68  * @brief Releases the DDK.
69  *
70  * @permission ohos.permission.ACCESS_DDK_USB
71  * @return <b>0</b> if the operation is successful; a negative value otherwise.
72  * @since 14
73  * @version 1.0
74  */
75 int32_t OH_Usb_ReleaseResource(void);
76 
77 /**
78  * @brief Obtains the USB device descriptor.
79  *
80  * @permission ohos.permission.ACCESS_DDK_USB
81  * @param deviceId ID of the device whose descriptor is to be obtained.
82  * @param desc Standard device descriptor defined in the USB protocol.
83  * @return <b>0</b> if the operation is successful; a negative value otherwise.
84  * @since 10
85  * @version 1.0
86  */
87 int32_t OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc);
88 
89 /**
90  * @brief Obtains the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n
91  * to release a descriptor after use.
92  *
93  * @permission ohos.permission.ACCESS_DDK_USB
94  * @param deviceId ID of the device whose configuration descriptor is to be obtained.
95  * @param configIndex Configuration index, which corresponds to <b>bConfigurationValue</b> in the USB protocol.
96  * @param config Configuration descriptor, which includes the standard configuration descriptor defined in the\n
97  * USB protocol and the associated interface descriptor and endpoint descriptor.
98  * @return <b>0</b> if the operation is successful; a negative value otherwise.
99  * @since 10
100  * @version 1.0
101  */
102 int32_t OH_Usb_GetConfigDescriptor(
103     uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor ** const config);
104 
105 /**
106  * @brief Releases the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n
107  * to release a descriptor after use.
108  *
109  * @permission ohos.permission.ACCESS_DDK_USB
110  * @param config Configuration descriptor obtained by calling <b>OH_Usb_GetConfigDescriptor</b>.
111  * @since 10
112  * @version 1.0
113  */
114 void OH_Usb_FreeConfigDescriptor(struct UsbDdkConfigDescriptor * const config);
115 
116 /**
117  * @brief Claims a USB interface.
118  *
119  * @permission ohos.permission.ACCESS_DDK_USB
120  * @param deviceId ID of the device to be operated.
121  * @param interfaceIndex Interface index, which corresponds to <b>bInterfaceNumber</b> in the USB protocol.
122  * @param interfaceHandle Interface operation handle. After the interface is claimed successfully, a value will be\n
123  * assigned to this parameter.
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_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle);
129 
130 /**
131  * @brief Releases a USB interface.
132  *
133  * @permission ohos.permission.ACCESS_DDK_USB
134  * @param interfaceHandle Interface operation handle.
135  * @return <b>0</b> if the operation is successful; a negative value otherwise.
136  * @since 10
137  * @version 1.0
138  */
139 int32_t OH_Usb_ReleaseInterface(uint64_t interfaceHandle);
140 
141 /**
142  * @brief Activates the alternate setting of the USB interface.
143  *
144  * @permission ohos.permission.ACCESS_DDK_USB
145  * @param interfaceHandle Interface operation handle.
146  * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n
147  * in the USB protocol.
148  * @return <b>0</b> if the operation is successful; a negative value otherwise.
149  * @since 10
150  * @version 1.0
151  */
152 int32_t OH_Usb_SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex);
153 
154 /**
155  * @brief Obtains the activated alternate setting of the USB interface.
156  *
157  * @permission ohos.permission.ACCESS_DDK_USB
158  * @param interfaceHandle Interface operation handle.
159  * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n
160  * in the USB protocol.
161  * @return <b>0</b> if the operation is successful; a negative value otherwise.
162  * @since 10
163  * @version 1.0
164  */
165 int32_t OH_Usb_GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t *settingIndex);
166 
167 /**
168  * @brief Sends a control read transfer request. This API works in a synchronous manner.
169  *
170  * @permission ohos.permission.ACCESS_DDK_USB
171  * @param interfaceHandle Interface operation handle.
172  * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol.
173  * @param timeout Timeout duration, in milliseconds.
174  * @param data Data to be transferred.
175  * @param dataLen Data length. The return value indicates the length of the actually read data.
176  * @return <b>0</b> if the operation is successful; a negative value otherwise.
177  * @since 10
178  * @version 1.0
179  */
180 int32_t OH_Usb_SendControlReadRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,
181     uint32_t timeout, uint8_t *data, uint32_t *dataLen);
182 
183 /**
184  * @brief Sends a control write transfer request. This API works in a synchronous manner.
185  *
186  * @permission ohos.permission.ACCESS_DDK_USB
187  * @param interfaceHandle Interface operation handle.
188  * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol.
189  * @param timeout Timeout duration, in milliseconds.
190  * @param data Data to be transferred.
191  * @param dataLen Data length.
192  * @return <b>0</b> if the operation is successful; a negative value otherwise.
193  * @since 10
194  * @version 1.0
195  */
196 int32_t OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,
197     uint32_t timeout, const uint8_t *data, uint32_t dataLen);
198 
199 /**
200  * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n
201  * and bulk transfer.
202  *
203  * @permission ohos.permission.ACCESS_DDK_USB
204  * @param pipe Pipe used to transfer data.
205  * @param devMmap Device memory map, which can be obtained by calling <b>OH_Usb_CreateDeviceMemMap</b>.
206  * @return <b>0</b> if the operation is successful; a negative value otherwise.
207  * @since 10
208  * @version 1.0
209  */
210 int32_t OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap);
211 
212 /**
213  * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n
214  * and bulk transfer.
215  *
216  * @permission ohos.permission.ACCESS_DDK_USB
217  * @param pipe Pipe used to transfer data.
218  * @param ashmem Shared memory, which can be obtained by calling <b>OH_DDK_CreateAshmem</b>.
219  * @return <b>0</b> if the operation is successful; a negative value otherwise.
220  * @since 12
221  */
222 int32_t OH_Usb_SendPipeRequestWithAshmem(const struct UsbRequestPipe *pipe, DDK_Ashmem *ashmem);
223 
224 /**
225  * @brief Creates a buffer. To avoid resource leakage, destroy a buffer by calling\n
226  * <b>OH_Usb_DestroyDeviceMemMap</b> after use.
227  *
228  * @permission ohos.permission.ACCESS_DDK_USB
229  * @param deviceId ID of the device for which the buffer is to be created.
230  * @param size Buffer size.
231  * @param devMmap Data memory map, through which the created buffer is returned to the caller.
232  * @return <b>0</b> if the operation is successful; a negative value otherwise.
233  * @since 10
234  * @version 1.0
235  */
236 int32_t OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap);
237 
238 /**
239  * @brief Destroys a buffer. To avoid resource leakage, destroy a buffer in time after use.
240  *
241  * @permission ohos.permission.ACCESS_DDK_USB
242  * @param devMmap Device memory map created by calling <b>OH_Usb_CreateDeviceMemMap</b>.
243  * @since 10
244  * @version 1.0
245  */
246 void OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap);
247 
248 /**
249  * @brief Obtain USB devices.
250  *
251  * @permission ohos.permission.ACCESS_DDK_USB
252  * @param devices USB device array.
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} devices is null.
257  * @since 16
258  */
259 int32_t OH_Usb_GetDevices(struct Usb_DeviceArray *devices);
260 /** @} */
261 #ifdef __cplusplus
262 }
263 #endif /* __cplusplus */
264 
265 #endif // USB_DDK_API_H