1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#ifndef USB_RAW_API_LIBRARY_H 17094332d3Sopenharmony_ci#define USB_RAW_API_LIBRARY_H 18094332d3Sopenharmony_ci 19094332d3Sopenharmony_ci#include "hdf_device_desc.h" 20094332d3Sopenharmony_ci#include "hdf_usb_pnp_manage.h" 21094332d3Sopenharmony_ci#include "usb_session.h" 22094332d3Sopenharmony_ci#include "usb_ddk_device.h" 23094332d3Sopenharmony_ci#include "usb_ddk_request.h" 24094332d3Sopenharmony_ci#include "usb_raw_api.h" 25094332d3Sopenharmony_ci 26094332d3Sopenharmony_ci#define BYTE_LENGTH 8 27094332d3Sopenharmony_ci 28094332d3Sopenharmony_ci#define USB_HOST_PNP_SERVICE_NAME "hdf_usb_pnp_notify_service" 29094332d3Sopenharmony_ci 30094332d3Sopenharmony_ci#define USB_RAW_REQUEST_TIME_ZERO_MS (0) 31094332d3Sopenharmony_ci#define USB_RAW_REQUEST_DEFAULT_TIMEOUT (1000) 32094332d3Sopenharmony_ci#define USB_RAW_REQUEST_TIMEOUT_MAX (0xFFFFFFFF) 33094332d3Sopenharmony_ci 34094332d3Sopenharmony_ci#define DESC_HEADER_LENGTH 2 35094332d3Sopenharmony_ci 36094332d3Sopenharmony_citypedef pid_t UsbRawTidType; 37094332d3Sopenharmony_ci 38094332d3Sopenharmony_cistruct UsbRawControlSetup { 39094332d3Sopenharmony_ci uint8_t requestType; 40094332d3Sopenharmony_ci uint8_t request; 41094332d3Sopenharmony_ci uint16_t value; 42094332d3Sopenharmony_ci uint16_t index; 43094332d3Sopenharmony_ci uint16_t length; 44094332d3Sopenharmony_ci} __attribute__((packed)); 45094332d3Sopenharmony_ci 46094332d3Sopenharmony_ci#define USB_RAW_CONTROL_SETUP_SIZE (sizeof(struct UsbRawControlSetup)) 47094332d3Sopenharmony_ci 48094332d3Sopenharmony_ciunion UsbiConfigDescBuf { 49094332d3Sopenharmony_ci struct UsbConfigDescriptor desc; 50094332d3Sopenharmony_ci uint8_t buf[USB_DDK_DT_CONFIG_SIZE]; 51094332d3Sopenharmony_ci uint16_t align; /* Force 2-byte alignment */ 52094332d3Sopenharmony_ci}; 53094332d3Sopenharmony_ci 54094332d3Sopenharmony_cienum UsbRawDescriptorType { 55094332d3Sopenharmony_ci USB_RAW_CONFIG_DESCRIPTOR_TYPE, 56094332d3Sopenharmony_ci USB_RAW_INTERFACE_DESCRIPTOR_TYPE, 57094332d3Sopenharmony_ci USB_RAW_ENDPOINT_DESCRIPTOR_TYPE, 58094332d3Sopenharmony_ci USB_RAW_AUDIO_ENDPOINT_DESCRIPTOR_TYPE, 59094332d3Sopenharmony_ci}; 60094332d3Sopenharmony_ci 61094332d3Sopenharmony_cienum RawRequestTimeoutFlags { 62094332d3Sopenharmony_ci RAW_REQUEST_OS_HANDLES_TIMEOUT = 1U << 0, 63094332d3Sopenharmony_ci RAW_REQUEST_TIMEOUT_HANDLED = 1U << 1, 64094332d3Sopenharmony_ci RAW_REQUEST_TIMED_OUT = 1U << 2, 65094332d3Sopenharmony_ci}; 66094332d3Sopenharmony_ci 67094332d3Sopenharmony_cistruct UsbMessageQueue { 68094332d3Sopenharmony_ci struct DListHead entry; 69094332d3Sopenharmony_ci struct OsalMutex mutex; 70094332d3Sopenharmony_ci struct OsalSem sem; 71094332d3Sopenharmony_ci}; 72094332d3Sopenharmony_ci 73094332d3Sopenharmony_cistruct RawUsbRamTestList { 74094332d3Sopenharmony_ci uintptr_t address; 75094332d3Sopenharmony_ci uint32_t size; 76094332d3Sopenharmony_ci struct DListHead list; 77094332d3Sopenharmony_ci struct OsalMutex lock; 78094332d3Sopenharmony_ci}; 79094332d3Sopenharmony_ci#ifdef __cplusplus 80094332d3Sopenharmony_ciextern "C" { 81094332d3Sopenharmony_ci#endif 82094332d3Sopenharmony_ci 83094332d3Sopenharmony_cistruct UsbSession *RawGetSession(const struct UsbSession *session); 84094332d3Sopenharmony_ciint32_t RawInit(struct UsbSession **session); 85094332d3Sopenharmony_ciint32_t RawExit(const struct UsbSession *session); 86094332d3Sopenharmony_cistruct UsbDeviceHandle *RawOpenDevice(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr); 87094332d3Sopenharmony_ciint32_t RawCloseDevice(const struct UsbDeviceHandle *devHandle); 88094332d3Sopenharmony_ciint32_t RawClaimInterface(struct UsbDeviceHandle *devHandle, int32_t interfaceNumber); 89094332d3Sopenharmony_cistruct UsbHostRequest *AllocRequest(const struct UsbDeviceHandle *devHandle, int32_t isoPackets, size_t length); 90094332d3Sopenharmony_ciint32_t FreeRequest(const struct UsbHostRequest *request); 91094332d3Sopenharmony_ciint32_t RawFillBulkRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 92094332d3Sopenharmony_ci const struct UsbFillRequestData *fillRequestData); 93094332d3Sopenharmony_ciint32_t RawFillControlSetup(const unsigned char *setup, const struct UsbControlRequestData *requestData); 94094332d3Sopenharmony_ciint32_t RawFillControlRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 95094332d3Sopenharmony_ci const struct UsbFillRequestData *fillRequestData); 96094332d3Sopenharmony_ciint32_t RawFillInterruptRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 97094332d3Sopenharmony_ci const struct UsbFillRequestData *fillRequestData); 98094332d3Sopenharmony_ciint32_t RawFillInterruptRequestByMmap(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 99094332d3Sopenharmony_ci const struct UsbFillRequestData *fillRequestData); 100094332d3Sopenharmony_ciint32_t RawFillIsoRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 101094332d3Sopenharmony_ci const struct UsbFillRequestData *fillRequestData); 102094332d3Sopenharmony_ciint32_t RawSendControlRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 103094332d3Sopenharmony_ci const struct UsbControlRequestData *requestData); 104094332d3Sopenharmony_ciint32_t RawSendBulkRequest(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 105094332d3Sopenharmony_ci const struct UsbRequestData *requestData); 106094332d3Sopenharmony_ciint32_t RawSendInterruptRequest(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 107094332d3Sopenharmony_ci const struct UsbRequestData *requestData); 108094332d3Sopenharmony_cistruct UsbHostRequest *RawAllocRequest(const struct UsbDeviceHandle *devHandle, int32_t isoPackets, int32_t length); 109094332d3Sopenharmony_cistruct UsbHostRequest *RawAllocRequestByMmap( 110094332d3Sopenharmony_ci const struct UsbDeviceHandle *devHandle, int32_t isoPackets, int32_t length); 111094332d3Sopenharmony_ciint32_t RawFreeRequest(const struct UsbHostRequest *request); 112094332d3Sopenharmony_ciint32_t RawFreeRequestByMmap(const struct UsbHostRequest *request); 113094332d3Sopenharmony_ciint32_t RawGetConfigDescriptor(const struct UsbDevice *dev, uint8_t configIndex, 114094332d3Sopenharmony_ci struct UsbRawConfigDescriptor ** const config); 115094332d3Sopenharmony_civoid RawClearConfiguration(struct UsbRawConfigDescriptor *config); 116094332d3Sopenharmony_ciint32_t RawGetConfiguration(const struct UsbDeviceHandle *devHandle, int32_t *config); 117094332d3Sopenharmony_ciint32_t RawSetConfiguration(const struct UsbDeviceHandle *devHandle, int32_t configuration); 118094332d3Sopenharmony_ciint32_t RawGetDescriptor(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 119094332d3Sopenharmony_ci const struct UsbRawDescriptorParam *param, const unsigned char *data); 120094332d3Sopenharmony_cistruct UsbDevice *RawGetDevice(const struct UsbDeviceHandle *devHandle); 121094332d3Sopenharmony_ciint32_t RawGetDeviceDescriptor(const struct UsbDevice *dev, struct UsbDeviceDescriptor *desc); 122094332d3Sopenharmony_ciint32_t RawReleaseInterface(struct UsbDeviceHandle *devHandle, int32_t interfaceNumber); 123094332d3Sopenharmony_ciint32_t RawResetDevice(const struct UsbDeviceHandle *devHandle); 124094332d3Sopenharmony_ciint32_t RawSubmitRequest(const struct UsbHostRequest *request); 125094332d3Sopenharmony_ciint32_t RawCancelRequest(const struct UsbHostRequest *request); 126094332d3Sopenharmony_ciint32_t RawHandleRequest(const struct UsbDeviceHandle *devHandle); 127094332d3Sopenharmony_ciint32_t RawClearHalt(const struct UsbDeviceHandle *devHandle, uint8_t pipeAddress); 128094332d3Sopenharmony_ciint32_t RawHandleRequestCompletion(struct UsbHostRequest *request, UsbRequestStatus status); 129094332d3Sopenharmony_ciint32_t RawSetInterfaceAltsetting( 130094332d3Sopenharmony_ci const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber, uint8_t settingIndex); 131094332d3Sopenharmony_ciUsbRawTidType RawGetTid(void); 132094332d3Sopenharmony_ciint32_t RawRegisterSignal(void); 133094332d3Sopenharmony_ciint32_t RawKillSignal(struct UsbDeviceHandle *devHandle, UsbRawTidType tid); 134094332d3Sopenharmony_ciint32_t RawInitPnpService(enum UsbPnpNotifyServiceCmd cmdType, struct UsbPnpAddRemoveInfo infoData); 135094332d3Sopenharmony_civoid RawRequestListInit(struct UsbDevice *deviceObj); 136094332d3Sopenharmony_civoid *RawUsbMemAlloc(size_t size); 137094332d3Sopenharmony_civoid *RawUsbMemCalloc(size_t size); 138094332d3Sopenharmony_civoid RawUsbMemFree(void *mem); 139094332d3Sopenharmony_ciint32_t RawClaimInterfaceForce(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber); 140094332d3Sopenharmony_civoid RawAttachKernelDriver(struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber); 141094332d3Sopenharmony_ciint32_t RawAttachInterface(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber); 142094332d3Sopenharmony_ciint32_t RawDetachInterface(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber); 143094332d3Sopenharmony_ci 144094332d3Sopenharmony_ciint32_t RawUsbControlMsg(const struct UsbDeviceHandle *devHandle, struct UsbControlRequestData *ctrlData); 145094332d3Sopenharmony_ciint32_t RawUsbGetUsbSpeed(const struct UsbDeviceHandle *devHandle); 146094332d3Sopenharmony_cibool RawGetInterfaceActiveStatus(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber); 147094332d3Sopenharmony_ciint32_t RawUsbCloseCtlProcess(const UsbInterfaceHandle *interfaceHandle); 148094332d3Sopenharmony_ci 149094332d3Sopenharmony_ci#ifdef __cplusplus 150094332d3Sopenharmony_ci} 151094332d3Sopenharmony_ci#endif 152094332d3Sopenharmony_ci#endif /* USB_RAW_API_LIBRARY_H */ 153