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_DDK_LITEOS_ADAPTER_H 17 #define USB_DDK_LITEOS_ADAPTER_H 18 19 #include <stdlib.h> 20 #include <stdint.h> 21 #include <errno.h> 22 #include <fcntl.h> 23 #include <sys/ioctl.h> 24 #include <sys/mman.h> 25 #include <unistd.h> 26 #include "liteos_ddk_usb.h" 27 28 #define MAX_BULK_DATA_BUFFER_LENGTH 4096 29 #define MAX_BULK_URBS_PER_REQUEST 1 30 #define MAX_ISO_PACKETS_PER_URB 128 31 #define MAX_ISO_DATA_BUFFER_LEN (8 * 1024) 32 #define MAX_ISO_URBS_PER_REQUEST (((MAX_ISO_DATA_BUFFER_LEN - 1) / MAX_ISO_PACKETS_PER_URB) + 1) 33 34 #define URBS_PER_REQUEST MAX(MAX_BULK_URBS_PER_REQUEST, MAX_ISO_URBS_PER_REQUEST) 35 36 struct UsbCtrltransfer { 37 uint8_t requestType; 38 uint8_t request; 39 uint16_t wValue; 40 uint16_t wIndex; 41 uint16_t wLength; 42 uint32_t timeout; 43 void *data; 44 }; 45 46 struct UsbSetInterface { 47 unsigned int interface; 48 unsigned int altSetting; 49 }; 50 51 enum UsbRequestFlags { 52 USB_REQUEST_SHORT_NOT_OK = (1U << 0), 53 USB_REQUEST_FREE_BUFFER = (1U << 1), 54 USB_REQUEST_FREE_TRANSFER = (1U << 2), 55 USB_REQUEST_ADD_ZERO_PACKET = (1U << 3) 56 }; 57 58 struct UsbAdapterStreams { 59 unsigned int numStreams; 60 unsigned int numEps; 61 unsigned char eps[0]; 62 }; 63 64 struct ConfigDescriptor { 65 struct UsbConfigDescriptor *desc; 66 size_t actualLen; 67 }; 68 69 struct UsbDevicePriv { 70 void *descriptors; 71 size_t descriptorsLen; 72 uint8_t activeConfig; 73 struct ConfigDescriptor *configDescriptors; 74 }; 75 76 #define BUS_OFFSET 8 77 #define USB_ADAPTER_URB_TYPE_ISO 0 78 #define USB_ADAPTER_URB_TYPE_INTERRUPT 1 79 #define USB_ADAPTER_URB_TYPE_CONTROL 2 80 #define USB_ADAPTER_URB_TYPE_BULK 3 81 #define USB_ADAPTER_URB_SHORT_NOT_OK 0x01 82 #define USB_ADAPTER_URB_ISO_ASAP 0x02 83 #define USB_ADAPTER_URB_BULK_CONTINUATION 0x04 84 #define USB_ADAPTER_URB_QUEUE_BULK 0x10 85 #define USB_ADAPTER_URB_ZERO_PACKET 0x40 86 #define USB_ADAPTER_CAP_ZERO_PACKET 0x01 87 #define USB_ADAPTER_CAP_BULK_CONTINUATION 0x02 88 #define USB_ADAPTER_CAP_NO_PACKET_SIZE_LIM 0x04 89 #define USB_ADAPTER_CAP_BULK_SCATTER_GATHER 0x08 90 #define USB_ADAPTER_CAP_REAP_AFTER_DISCONNECT 0x10 91 92 struct UsbOsAdapterOps { 93 int32_t (*init)(const struct UsbSession *session); 94 void (*exit)(const struct UsbSession *session); 95 struct UsbDeviceHandle *(*openDevice)(struct UsbSession *session, uint8_t busNum, uint8_t usbAddr); 96 void (*closeDevice)(struct UsbDeviceHandle *devHandle); 97 int32_t (*getConfigDescriptor)(const struct UsbDevice *device, uint8_t configIndex, void *buffer, size_t len); 98 int32_t (*getConfiguration)(const struct UsbDeviceHandle *devHandle, uint8_t *activeConfig); 99 int32_t (*setConfiguration)(struct UsbDeviceHandle *devHandle, int32_t activeConfig); 100 int32_t (*claimInterface)(const struct UsbDeviceHandle *devHandle, unsigned int interfaceNumber); 101 int32_t (*releaseInterface)(const struct UsbDeviceHandle *devHandle, unsigned int interfaceNumber); 102 int32_t (*setInterfaceAltsetting)(const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber, 103 uint8_t altsetting); 104 int32_t (*clearHalt)(const struct UsbDeviceHandle *devHandle, unsigned int endpoint); 105 int32_t (*resetDevice)(const struct UsbDeviceHandle *devHandle); 106 struct UsbHostRequest *(*allocRequest)(const struct UsbDeviceHandle *handle, int32_t isoPackets, size_t len); 107 int32_t (*freeRequest)(struct UsbHostRequest *request); 108 int32_t (*submitRequest)(struct UsbHostRequest *request); 109 int32_t (*cancelRequest)(const struct UsbHostRequest *request); 110 int32_t (*urbCompleteHandle)(const struct UsbDeviceHandle *devHandle); 111 }; 112 113 struct UsbOsAdapterOps *UsbAdapterGetOps(void); 114 UsbRawTidType UsbAdapterGetTid(void); 115 int32_t UsbAdapterRegisterSignal(void); 116 int32_t UsbAdapterKillSignal(struct UsbDeviceHandle *devHandle, UsbRawTidType tid); 117 int32_t AdapterAtomicInc(OsalAtomic *v); 118 int32_t AdapterAtomicDec(OsalAtomic *v); 119 120 #endif /* USB_DDK_LITEOS_ADAPTER_H */ 121