112636162Sopenharmony_ci/* 212636162Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 312636162Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 412636162Sopenharmony_ci * you may not use this file except in compliance with the License. 512636162Sopenharmony_ci * You may obtain a copy of the License at 612636162Sopenharmony_ci * 712636162Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 812636162Sopenharmony_ci * 912636162Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1012636162Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1112636162Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1212636162Sopenharmony_ci * See the License for the specific language governing permissions and 1312636162Sopenharmony_ci * limitations under the License. 1412636162Sopenharmony_ci */ 1512636162Sopenharmony_ci#ifndef USB_DDK_API_H 1612636162Sopenharmony_ci#define USB_DDK_API_H 1712636162Sopenharmony_ci 1812636162Sopenharmony_ci/** 1912636162Sopenharmony_ci * @addtogroup UsbDdk 2012636162Sopenharmony_ci * @{ 2112636162Sopenharmony_ci * 2212636162Sopenharmony_ci * @brief Provides USB DDK APIs to open and close USB interfaces, perform non-isochronous and isochronous\n 2312636162Sopenharmony_ci * data transfer over USB pipes, and implement control transfer and interrupt transfer, etc. 2412636162Sopenharmony_ci * 2512636162Sopenharmony_ci * @syscap SystemCapability.Driver.USB.Extension 2612636162Sopenharmony_ci * @since 10 2712636162Sopenharmony_ci * @version 1.0 2812636162Sopenharmony_ci */ 2912636162Sopenharmony_ci 3012636162Sopenharmony_ci/** 3112636162Sopenharmony_ci * @file usb_ddk_api.h 3212636162Sopenharmony_ci * 3312636162Sopenharmony_ci * @brief Declares the USB DDK APIs used by the USB host to access USB devices. 3412636162Sopenharmony_ci * 3512636162Sopenharmony_ci * @since 10 3612636162Sopenharmony_ci * @version 1.0 3712636162Sopenharmony_ci */ 3812636162Sopenharmony_ci 3912636162Sopenharmony_ci#include <stdint.h> 4012636162Sopenharmony_ci 4112636162Sopenharmony_ci#include "ddk_types.h" 4212636162Sopenharmony_ci#include "usb_ddk_types.h" 4312636162Sopenharmony_ci 4412636162Sopenharmony_ci#ifdef __cplusplus 4512636162Sopenharmony_ciextern "C" { 4612636162Sopenharmony_ci#endif /* __cplusplus */ 4712636162Sopenharmony_ci 4812636162Sopenharmony_ci/** 4912636162Sopenharmony_ci * @brief Initializes the DDK. 5012636162Sopenharmony_ci * 5112636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 5212636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 5312636162Sopenharmony_ci * @since 10 5412636162Sopenharmony_ci * @version 1.0 5512636162Sopenharmony_ci */ 5612636162Sopenharmony_ciint32_t OH_Usb_Init(void); 5712636162Sopenharmony_ci 5812636162Sopenharmony_ci/** 5912636162Sopenharmony_ci * @brief Releases the DDK. 6012636162Sopenharmony_ci * 6112636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 6212636162Sopenharmony_ci * @since 10 6312636162Sopenharmony_ci * @version 1.0 6412636162Sopenharmony_ci */ 6512636162Sopenharmony_civoid OH_Usb_Release(void); 6612636162Sopenharmony_ci 6712636162Sopenharmony_ci/** 6812636162Sopenharmony_ci * @brief Obtains the USB device descriptor. 6912636162Sopenharmony_ci * 7012636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 7112636162Sopenharmony_ci * @param deviceId ID of the device whose descriptor is to be obtained. 7212636162Sopenharmony_ci * @param desc Standard device descriptor defined in the USB protocol. 7312636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 7412636162Sopenharmony_ci * @since 10 7512636162Sopenharmony_ci * @version 1.0 7612636162Sopenharmony_ci */ 7712636162Sopenharmony_ciint32_t OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc); 7812636162Sopenharmony_ci 7912636162Sopenharmony_ci/** 8012636162Sopenharmony_ci * @brief Obtains the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n 8112636162Sopenharmony_ci * to release a descriptor after use. 8212636162Sopenharmony_ci * 8312636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 8412636162Sopenharmony_ci * @param deviceId ID of the device whose configuration descriptor is to be obtained. 8512636162Sopenharmony_ci * @param configIndex Configuration index, which corresponds to <b>bConfigurationValue</b> in the USB protocol. 8612636162Sopenharmony_ci * @param config Configuration descriptor, which includes the standard configuration descriptor defined in the\n 8712636162Sopenharmony_ci * USB protocol and the associated interface descriptor and endpoint descriptor. 8812636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 8912636162Sopenharmony_ci * @since 10 9012636162Sopenharmony_ci * @version 1.0 9112636162Sopenharmony_ci */ 9212636162Sopenharmony_ciint32_t OH_Usb_GetConfigDescriptor( 9312636162Sopenharmony_ci uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor ** const config); 9412636162Sopenharmony_ci 9512636162Sopenharmony_ci/** 9612636162Sopenharmony_ci * @brief Releases the configuration descriptor. To avoid memory leakage, use <b>OH_Usb_FreeConfigDescriptor</b>\n 9712636162Sopenharmony_ci * to release a descriptor after use. 9812636162Sopenharmony_ci * 9912636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 10012636162Sopenharmony_ci * @param config Configuration descriptor obtained by calling <b>OH_Usb_GetConfigDescriptor</b>. 10112636162Sopenharmony_ci * @since 10 10212636162Sopenharmony_ci * @version 1.0 10312636162Sopenharmony_ci */ 10412636162Sopenharmony_civoid OH_Usb_FreeConfigDescriptor(struct UsbDdkConfigDescriptor * const config); 10512636162Sopenharmony_ci 10612636162Sopenharmony_ci/** 10712636162Sopenharmony_ci * @brief Claims a USB interface. 10812636162Sopenharmony_ci * 10912636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 11012636162Sopenharmony_ci * @param deviceId ID of the device to be operated. 11112636162Sopenharmony_ci * @param interfaceIndex Interface index, which corresponds to <b>bInterfaceNumber</b> in the USB protocol. 11212636162Sopenharmony_ci * @param interfaceHandle Interface operation handle. After the interface is claimed successfully, a value will be\n 11312636162Sopenharmony_ci * assigned to this parameter. 11412636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 11512636162Sopenharmony_ci * @since 10 11612636162Sopenharmony_ci * @version 1.0 11712636162Sopenharmony_ci */ 11812636162Sopenharmony_ciint32_t OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle); 11912636162Sopenharmony_ci 12012636162Sopenharmony_ci/** 12112636162Sopenharmony_ci * @brief Releases a USB interface. 12212636162Sopenharmony_ci * 12312636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 12412636162Sopenharmony_ci * @param interfaceHandle Interface operation handle. 12512636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 12612636162Sopenharmony_ci * @since 10 12712636162Sopenharmony_ci * @version 1.0 12812636162Sopenharmony_ci */ 12912636162Sopenharmony_ciint32_t OH_Usb_ReleaseInterface(uint64_t interfaceHandle); 13012636162Sopenharmony_ci 13112636162Sopenharmony_ci/** 13212636162Sopenharmony_ci * @brief Activates the alternate setting of the USB interface. 13312636162Sopenharmony_ci * 13412636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 13512636162Sopenharmony_ci * @param interfaceHandle Interface operation handle. 13612636162Sopenharmony_ci * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n 13712636162Sopenharmony_ci * in the USB protocol. 13812636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 13912636162Sopenharmony_ci * @since 10 14012636162Sopenharmony_ci * @version 1.0 14112636162Sopenharmony_ci */ 14212636162Sopenharmony_ciint32_t OH_Usb_SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex); 14312636162Sopenharmony_ci 14412636162Sopenharmony_ci/** 14512636162Sopenharmony_ci * @brief Obtains the activated alternate setting of the USB interface. 14612636162Sopenharmony_ci * 14712636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 14812636162Sopenharmony_ci * @param interfaceHandle Interface operation handle. 14912636162Sopenharmony_ci * @param settingIndex Index of the alternate setting, which corresponds to <b>bAlternateSetting</b>\n 15012636162Sopenharmony_ci * in the USB protocol. 15112636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 15212636162Sopenharmony_ci * @since 10 15312636162Sopenharmony_ci * @version 1.0 15412636162Sopenharmony_ci */ 15512636162Sopenharmony_ciint32_t OH_Usb_GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t *settingIndex); 15612636162Sopenharmony_ci 15712636162Sopenharmony_ci/** 15812636162Sopenharmony_ci * @brief Sends a control read transfer request. This API works in a synchronous manner. 15912636162Sopenharmony_ci * 16012636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 16112636162Sopenharmony_ci * @param interfaceHandle Interface operation handle. 16212636162Sopenharmony_ci * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol. 16312636162Sopenharmony_ci * @param timeout Timeout duration, in milliseconds. 16412636162Sopenharmony_ci * @param data Data to be transferred. 16512636162Sopenharmony_ci * @param dataLen Data length. The return value indicates the length of the actually read data. 16612636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 16712636162Sopenharmony_ci * @since 10 16812636162Sopenharmony_ci * @version 1.0 16912636162Sopenharmony_ci */ 17012636162Sopenharmony_ciint32_t OH_Usb_SendControlReadRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup, 17112636162Sopenharmony_ci uint32_t timeout, uint8_t *data, uint32_t *dataLen); 17212636162Sopenharmony_ci 17312636162Sopenharmony_ci/** 17412636162Sopenharmony_ci * @brief Sends a control write transfer request. This API works in a synchronous manner. 17512636162Sopenharmony_ci * 17612636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 17712636162Sopenharmony_ci * @param interfaceHandle Interface operation handle. 17812636162Sopenharmony_ci * @param setup Request data, which corresponds to <b>Setup Data</b> in the USB protocol. 17912636162Sopenharmony_ci * @param timeout Timeout duration, in milliseconds. 18012636162Sopenharmony_ci * @param data Data to be transferred. 18112636162Sopenharmony_ci * @param dataLen Data length. 18212636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 18312636162Sopenharmony_ci * @since 10 18412636162Sopenharmony_ci * @version 1.0 18512636162Sopenharmony_ci */ 18612636162Sopenharmony_ciint32_t OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup *setup, 18712636162Sopenharmony_ci uint32_t timeout, const uint8_t *data, uint32_t dataLen); 18812636162Sopenharmony_ci 18912636162Sopenharmony_ci/** 19012636162Sopenharmony_ci * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n 19112636162Sopenharmony_ci * and bulk transfer. 19212636162Sopenharmony_ci * 19312636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 19412636162Sopenharmony_ci * @param pipe Pipe used to transfer data. 19512636162Sopenharmony_ci * @param devMmap Device memory map, which can be obtained by calling <b>OH_Usb_CreateDeviceMemMap</b>. 19612636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 19712636162Sopenharmony_ci * @since 10 19812636162Sopenharmony_ci * @version 1.0 19912636162Sopenharmony_ci */ 20012636162Sopenharmony_ciint32_t OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap); 20112636162Sopenharmony_ci 20212636162Sopenharmony_ci/** 20312636162Sopenharmony_ci * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n 20412636162Sopenharmony_ci * and bulk transfer. 20512636162Sopenharmony_ci * 20612636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 20712636162Sopenharmony_ci * @param pipe Pipe used to transfer data. 20812636162Sopenharmony_ci * @param ashmem Shared memory, which can be obtained by calling <b>OH_DDK_CreateAshmem</b>. 20912636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 21012636162Sopenharmony_ci * @since 12 21112636162Sopenharmony_ci */ 21212636162Sopenharmony_ciint32_t OH_Usb_SendPipeRequestWithAshmem(const struct UsbRequestPipe *pipe, DDK_Ashmem *ashmem); 21312636162Sopenharmony_ci 21412636162Sopenharmony_ci/** 21512636162Sopenharmony_ci * @brief Creates a buffer. To avoid resource leakage, destroy a buffer by calling\n 21612636162Sopenharmony_ci * <b>OH_Usb_DestroyDeviceMemMap</b> after use. 21712636162Sopenharmony_ci * 21812636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 21912636162Sopenharmony_ci * @param deviceId ID of the device for which the buffer is to be created. 22012636162Sopenharmony_ci * @param size Buffer size. 22112636162Sopenharmony_ci * @param devMmap Data memory map, through which the created buffer is returned to the caller. 22212636162Sopenharmony_ci * @return <b>0</b> if the operation is successful; a negative value otherwise. 22312636162Sopenharmony_ci * @since 10 22412636162Sopenharmony_ci * @version 1.0 22512636162Sopenharmony_ci */ 22612636162Sopenharmony_ciint32_t OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap); 22712636162Sopenharmony_ci 22812636162Sopenharmony_ci/** 22912636162Sopenharmony_ci * @brief Destroys a buffer. To avoid resource leakage, destroy a buffer in time after use. 23012636162Sopenharmony_ci * 23112636162Sopenharmony_ci * @permission ohos.permission.ACCESS_DDK_USB 23212636162Sopenharmony_ci * @param devMmap Device memory map created by calling <b>OH_Usb_CreateDeviceMemMap</b>. 23312636162Sopenharmony_ci * @since 10 23412636162Sopenharmony_ci * @version 1.0 23512636162Sopenharmony_ci */ 23612636162Sopenharmony_civoid OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap); 23712636162Sopenharmony_ci/** @} */ 23812636162Sopenharmony_ci#ifdef __cplusplus 23912636162Sopenharmony_ci} 24012636162Sopenharmony_ci#endif /* __cplusplus */ 24112636162Sopenharmony_ci 24212636162Sopenharmony_ci#endif // USB_DDK_API_H