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  * @kit DriverDevelopmentKit
26  * @syscap SystemCapability.Driver.USB.Extension
27  * @since 10
28  * @version 1.0
29  */
30 
31 /**
32  * @file usb_ddk_api.h
33  *
34  * @brief Declares the USB DDK APIs used by the USB host to access USB devices.
35  *
36  * @since 10
37  * @version 1.0
38  */
39 
40 #include <stdint.h>
41 
42 #include "ddk_types.h"
43 #include "usb_ddk_types.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif /* __cplusplus */
48 
49 /**
50  * @brief Initializes the DDK.
51  *
52  * @permission ohos.permission.ACCESS_DDK_USB
53  * @return {@link USB_DDK_SUCCESS} the operation is successful.
54  *         {@link USB_DDK_FAILED} permission check failed or connect usb ddk service failed or internal error failed.
55  * @since 10
56  * @version 1.0
57  */
58 int32_t OH_Usb_Init(void);
59 
60 /**
61  * @brief Releases the DDK.
62  *
63  * @permission ohos.permission.ACCESS_DDK_USB
64  * @since 10
65  * @version 1.0
66  */
67 void OH_Usb_Release(void);
68 
69 /**
70  * @brief Obtains the USB device descriptor.
71  *
72  * @permission ohos.permission.ACCESS_DDK_USB
73  * @param deviceId ID of the device whose descriptor is to be obtained.
74  * @param desc Standard device descriptor defined in the USB protocol.
75  * @return {@link USB_DDK_SUCCESS} the operation is successful.
76  *         {@link USB_DDK_FAILED} permission check failed or internal error failed.
77  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
78  *         {@link USB_DDK_INVALID_PARAMETER} desc is null.
79  * @since 10
80  * @version 1.0
81  */
82 int32_t OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc);
83 
84 /**
85  * @brief Obtains the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n
86  * to release a descriptor after use.
87  *
88  * @permission ohos.permission.ACCESS_DDK_USB
89  * @param deviceId ID of the device whose configuration descriptor is to be obtained.
90  * @param configIndex Configuration index, which corresponds to <b>bConfigurationValue</b> in the USB protocol.
91  * @param config Configuration descriptor, which includes the standard configuration descriptor defined in the\n
92  * USB protocol and the associated interface descriptor and endpoint descriptor.
93  * @return {@link USB_DDK_SUCCESS} the operation is successful.
94  *         {@link USB_DDK_FAILED} permission check failed or internal error failed.
95  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
96  *         {@link USB_DDK_INVALID_PARAMETER} config is null.
97  * @since 10
98  * @version 1.0
99  */
100 int32_t OH_Usb_GetConfigDescriptor(
101     uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor ** const config);
102 
103 /**
104  * @brief Releases the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n
105  * to release a descriptor after use.
106  *
107  * @permission ohos.permission.ACCESS_DDK_USB
108  * @param config Configuration descriptor obtained by calling <b>OH_Usb_GetConfigDescriptor</b>.
109  * @since 10
110  * @version 1.0
111  */
112 void OH_Usb_FreeConfigDescriptor(struct UsbDdkConfigDescriptor * const config);
113 
114 /**
115  * @brief Claims a USB interface.
116  *
117  * @permission ohos.permission.ACCESS_DDK_USB
118  * @param deviceId ID of the device to be operated.
119  * @param interfaceIndex Interface index, which corresponds to <b>bInterfaceNumber</b> in the USB protocol.
120  * @param interfaceHandle Interface operation handle. After the interface is claimed successfully, a value will be\n
121  * assigned to this parameter.
122  * @return {@link USB_DDK_SUCCESS} the operation is successful.
123  *         {@link USB_DDK_FAILED} permission check failed or internal error failed.
124  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
125  *         {@link USB_DDK_INVALID_PARAMETER} interfaceHandle is null.
126  * @since 10
127  * @version 1.0
128  */
129 int32_t OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle);
130 
131 /**
132  * @brief Releases a USB interface.
133  *
134  * @permission ohos.permission.ACCESS_DDK_USB
135  * @param interfaceHandle Interface operation handle.
136  * @return {@link USB_DDK_SUCCESS} the operation is successful.
137  *         {@link USB_DDK_FAILED} permission check failed or internal error failed.
138  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
139  * @since 10
140  * @version 1.0
141  */
142 int32_t OH_Usb_ReleaseInterface(uint64_t interfaceHandle);
143 
144 /**
145  * @brief Activates the alternate setting of the USB interface.
146  *
147  * @permission ohos.permission.ACCESS_DDK_USB
148  * @param interfaceHandle Interface operation handle.
149  * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n
150  * in the USB protocol.
151  * @return {@link USB_DDK_SUCCESS} the operation is successful.
152  *         {@link USB_DDK_FAILED} permission check failed or internal error 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_SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex);
158 
159 /**
160  * @brief Obtains the activated 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_FAILED} permission check failed or internal error failed.
168  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
169  *         {@link USB_DDK_INVALID_PARAMETER} settingIndex is null.
170  * @since 10
171  * @version 1.0
172  */
173 int32_t OH_Usb_GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t *settingIndex);
174 
175 /**
176  * @brief Sends a control read transfer request. This API works in a synchronous manner.
177  *
178  * @permission ohos.permission.ACCESS_DDK_USB
179  * @param interfaceHandle Interface operation handle.
180  * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol.
181  * @param timeout Timeout duration, in milliseconds.
182  * @param data Data to be transferred.
183  * @param dataLen Data length. The return value indicates the length of the actually read data.
184  * @return {@link USB_DDK_SUCCESS} the operation is successful.
185  *         {@link USB_DDK_FAILED} permission check failed or internal error failed.
186  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
187  *         {@link USB_DDK_INVALID_PARAMETER} setup is null or data is null or dataLen is null or dataLen is less than\n
188  *         size of the read data.
189  *         {@link USB_DDK_MEMORY_ERROR} the memory of read data copies failed.
190  * @since 10
191  * @version 1.0
192  */
193 int32_t OH_Usb_SendControlReadRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,
194     uint32_t timeout, uint8_t *data, uint32_t *dataLen);
195 
196 /**
197  * @brief Sends a control write transfer request. This API works in a synchronous manner.
198  *
199  * @permission ohos.permission.ACCESS_DDK_USB
200  * @param interfaceHandle Interface operation handle.
201  * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol.
202  * @param timeout Timeout duration, in milliseconds.
203  * @param data Data to be transferred.
204  * @param dataLen Data length.
205  * @return {@link USB_DDK_SUCCESS} the operation is successful.
206  *         {@link USB_DDK_FAILED} permission check failed or internal error failed.
207  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
208  *         {@link USB_DDK_INVALID_PARAMETER} setup is null or data is null.
209  * @since 10
210  * @version 1.0
211  */
212 int32_t OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup,
213     uint32_t timeout, const uint8_t *data, uint32_t dataLen);
214 
215 /**
216  * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n
217  * and bulk transfer.
218  *
219  * @permission ohos.permission.ACCESS_DDK_USB
220  * @param pipe Pipe used to transfer data.
221  * @param devMmap Device memory map, which can be obtained by calling <b>OH_Usb_CreateDeviceMemMap</b>.
222  * @return {@link USB_DDK_SUCCESS} the operation is successful.
223  *         {@link USB_DDK_FAILED} permission check failed or internal error failed.
224  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
225  *         {@link USB_DDK_INVALID_PARAMETER} pipe is null or devMmap is null or address of devMmap is null.
226  * @since 10
227  * @version 1.0
228  */
229 int32_t OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap);
230 
231 /**
232  * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n
233  * and bulk transfer.
234  *
235  * @permission ohos.permission.ACCESS_DDK_USB
236  * @param pipe Pipe used to transfer data.
237  * @param ashmem Shared memory, which can be obtained by calling <b>OH_DDK_CreateAshmem</b>.
238  * @return {@link USB_DDK_SUCCESS} the operation is successful.
239  *         {@link USB_DDK_FAILED} permission check failed or internal error failed.
240  *         {@link USB_DDK_INVALID_OPERATION} connect usb ddk service failed.
241  *         {@link USB_DDK_INVALID_PARAMETER} pipe is null or ashmem is null or address of ashmem is null.
242  * @since 12
243  */
244 int32_t OH_Usb_SendPipeRequestWithAshmem(const struct UsbRequestPipe *pipe, DDK_Ashmem *ashmem);
245 
246 /**
247  * @brief Creates a buffer. To avoid resource leakage, destroy a buffer by calling\n
248  * <b>OH_Usb_DestroyDeviceMemMap</b> after use.
249  *
250  * @permission ohos.permission.ACCESS_DDK_USB
251  * @param deviceId ID of the device for which the buffer is to be created.
252  * @param size Buffer size.
253  * @param devMmap Data memory map, through which the created buffer is returned to the caller.
254  * @return {@link USB_DDK_SUCCESS} the operation is successful.
255  *         {@link USB_DDK_FAILED} permission check failed or internal error failed.
256  *         {@link USB_DDK_INVALID_PARAMETER} devMmap is null.
257  *         {@link USB_DDK_MEMORY_ERROR} mmap failed or alloc memory of devMmap failed.
258  * @since 10
259  * @version 1.0
260  */
261 int32_t OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap);
262 
263 /**
264  * @brief Destroys a buffer. To avoid resource leakage, destroy a buffer in time after use.
265  *
266  * @permission ohos.permission.ACCESS_DDK_USB
267  * @param devMmap Device memory map created by calling <b>OH_Usb_CreateDeviceMemMap</b>.
268  * @since 10
269  * @version 1.0
270  */
271 void OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap);
272 /** @} */
273 #ifdef __cplusplus
274 }
275 #endif /* __cplusplus */
276 
277 #endif // USB_DDK_API_H