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