1 /* 2 * Copyright (c) 2021 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 #ifndef USB_RAW_API_LIBRARY_H 17 #define USB_RAW_API_LIBRARY_H 18 19 #include <fcntl.h> 20 21 #include "hdf_device_desc.h" 22 #include "hdf_usb_pnp_manage.h" 23 #include "usb_session.h" 24 #include "usb_ddk_device.h" 25 #include "usb_ddk_request.h" 26 #include "usb_raw_api.h" 27 28 #define BYTE_LENGTH 8 29 30 #define USB_HOST_PNP_SERVICE_NAME "hdf_usb_pnp_notify_service" 31 32 #define USB_RAW_REQUEST_TIME_ZERO_MS (0) 33 #define USB_RAW_REQUEST_DEFAULT_TIMEOUT (1000) 34 #define USB_RAW_REQUEST_TIMEOUT_MAX (0xFFFFFFFF) 35 36 #define DESC_HEADER_LENGTH 2 37 38 typedef pid_t UsbRawTidType; 39 40 struct UsbRawControlSetup { 41 uint8_t requestType; 42 uint8_t request; 43 uint16_t value; 44 uint16_t index; 45 uint16_t length; 46 } __attribute__((packed)); 47 48 #define USB_RAW_CONTROL_SETUP_SIZE (sizeof(struct UsbRawControlSetup)) 49 50 union UsbiConfigDescBuf { 51 struct UsbConfigDescriptor desc; 52 uint8_t buf[USB_DDK_DT_CONFIG_SIZE]; 53 uint16_t align; /* Force 2-byte alignment */ 54 }; 55 56 enum UsbRawDescriptorType { 57 USB_RAW_CONFIG_DESCRIPTOR_TYPE, 58 USB_RAW_INTERFACE_DESCRIPTOR_TYPE, 59 USB_RAW_ENDPOINT_DESCRIPTOR_TYPE, 60 USB_RAW_AUDIO_ENDPOINT_DESCRIPTOR_TYPE, 61 }; 62 63 enum RawRequestTimeoutFlags { 64 RAW_REQUEST_OS_HANDLES_TIMEOUT = 1U << 0, 65 RAW_REQUEST_TIMEOUT_HANDLED = 1U << 1, 66 RAW_REQUEST_TIMED_OUT = 1U << 2, 67 }; 68 69 struct UsbMessageQueue { 70 struct DListHead entry; 71 struct OsalMutex mutex; 72 struct OsalSem sem; 73 }; 74 75 struct RawUsbRamTestList { 76 uintptr_t address; 77 uint32_t size; 78 struct DListHead list; 79 struct OsalMutex lock; 80 }; 81 #ifdef __cplusplus 82 extern "C" { 83 #endif 84 85 struct UsbSession *RawGetSession(const struct UsbSession *session); 86 int32_t RawInit(struct UsbSession **session); 87 int32_t RawExit(const struct UsbSession *session); 88 struct UsbDeviceHandle *RawOpenDevice(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr); 89 int32_t RawCloseDevice(const struct UsbDeviceHandle *devHandle); 90 int32_t RawClaimInterface(struct UsbDeviceHandle *devHandle, int32_t interfaceNumber); 91 struct UsbHostRequest *AllocRequest(const struct UsbDeviceHandle *devHandle, int32_t isoPackets, size_t length); 92 int32_t FreeRequest(const struct UsbHostRequest *request); 93 int32_t RawFillBulkRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 94 const struct UsbFillRequestData *fillRequestData); 95 int32_t RawFillControlSetup(const unsigned char *setup, const struct UsbControlRequestData *requestData); 96 int32_t RawFillControlRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 97 const struct UsbFillRequestData *fillRequestData); 98 int32_t RawFillInterruptRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 99 const struct UsbFillRequestData *fillRequestData); 100 int32_t RawFillInterruptRequestByMmap(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 101 const struct UsbFillRequestData *fillRequestData); 102 int32_t RawFillIsoRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 103 const struct UsbFillRequestData *fillRequestData); 104 int32_t RawSendControlRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 105 const struct UsbControlRequestData *requestData); 106 int32_t RawSendBulkRequest(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 107 const struct UsbRequestData *requestData); 108 int32_t RawSendInterruptRequest(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 109 const struct UsbRequestData *requestData); 110 struct UsbHostRequest *RawAllocRequest(const struct UsbDeviceHandle *devHandle, int32_t isoPackets, int32_t length); 111 struct UsbHostRequest *RawAllocRequestByMmap( 112 const struct UsbDeviceHandle *devHandle, int32_t isoPackets, int32_t length); 113 int32_t RawFreeRequest(const struct UsbHostRequest *request); 114 int32_t RawFreeRequestByMmap(const struct UsbHostRequest *request); 115 int32_t RawGetConfigDescriptor(const struct UsbDevice *dev, uint8_t configIndex, 116 struct UsbRawConfigDescriptor ** const config); 117 void RawClearConfiguration(struct UsbRawConfigDescriptor *config); 118 int32_t RawGetConfiguration(const struct UsbDeviceHandle *devHandle, int32_t *config); 119 int32_t RawSetConfiguration(const struct UsbDeviceHandle *devHandle, int32_t configuration); 120 int32_t RawGetDescriptor(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle, 121 const struct UsbRawDescriptorParam *param, const unsigned char *data); 122 struct UsbDevice *RawGetDevice(const struct UsbDeviceHandle *devHandle); 123 int32_t RawGetDeviceDescriptor(const struct UsbDevice *dev, struct UsbDeviceDescriptor *desc); 124 int32_t RawReleaseInterface(struct UsbDeviceHandle *devHandle, int32_t interfaceNumber); 125 int32_t RawResetDevice(const struct UsbDeviceHandle *devHandle); 126 int32_t RawSubmitRequest(const struct UsbHostRequest *request); 127 int32_t RawCancelRequest(const struct UsbHostRequest *request); 128 int32_t RawHandleRequest(const struct UsbDeviceHandle *devHandle); 129 int32_t RawClearHalt(const struct UsbDeviceHandle *devHandle, uint8_t pipeAddress); 130 int32_t RawHandleRequestCompletion(struct UsbHostRequest *request, UsbRequestStatus status); 131 int32_t RawSetInterfaceAltsetting( 132 const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber, uint8_t settingIndex); 133 UsbRawTidType RawGetTid(void); 134 int32_t RawRegisterSignal(void); 135 int32_t RawKillSignal(struct UsbDeviceHandle *devHandle, UsbRawTidType tid); 136 int32_t RawInitPnpService(enum UsbPnpNotifyServiceCmd cmdType, struct UsbPnpAddRemoveInfo infoData); 137 void RawRequestListInit(struct UsbDevice *deviceObj); 138 void *RawUsbMemAlloc(size_t size); 139 void *RawUsbMemCalloc(size_t size); 140 void RawUsbMemFree(void *mem); 141 int32_t RawClaimInterfaceForce(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber); 142 void RawAttachKernelDriver(struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber); 143 int32_t RawAttachInterface(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber); 144 int32_t RawDetachInterface(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber); 145 int32_t RawUsbGetUsbSpeed(const struct UsbDeviceHandle *devHandle); 146 bool RawGetInterfaceActiveStatus(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber); 147 int32_t RawUsbCloseCtlProcess(const UsbInterfaceHandle *interfaceHandle); 148 int32_t RawUsbControlMsg(const struct UsbDeviceHandle *devHandle, struct UsbControlRequestData *ctrlData); 149 int32_t GetDeviceFd(struct UsbDevice *dev, mode_t mode); 150 151 #ifdef __cplusplus 152 } 153 #endif 154 #endif /* USB_RAW_API_LIBRARY_H */ 155