1 /* 2 * Copyright (c) 2022 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 USBD_H 17 #define USBD_H 18 19 #include "data_fifo.h" 20 #include "hdf_base.h" 21 #include "hdf_device_desc.h" 22 #include "osal_sem.h" 23 #include "refbase.h" 24 #include "usb_ddk.h" 25 #include "usb_ddk_interface.h" 26 #include "usb_session.h" 27 #include "usbd_type.h" 28 #include "v1_0/iusb_interface.h" 29 #include "v1_0/iusbd_bulk_callback.h" 30 31 #define USB_MAX_INTERFACES 32 32 #define USB_MAX_DEVICE_NUMBERS 127 33 #define DIRECTION_MASK 0x1 34 #define USB_CTRL_SET_TIMEOUT 5000 35 #define USB_PIPE_DIR_OFFSET 7 36 #define MAX_SUBSCRIBER 10 37 #define USBD_BULKASYNCREQ_NUM_MAX 64 38 39 namespace OHOS { 40 namespace HDI { 41 namespace Usb { 42 namespace V1_0 { 43 struct UsbdBulkASyncReqList; 44 struct UsbdBulkASyncList; 45 class UsbImpl; 46 struct UsbdBufferHandle { 47 int32_t fd; /**< buffer fd, -1 if not supported */ 48 uint32_t size; /* < size of memory */ 49 uint8_t *starAddr; 50 uint32_t cur; 51 uint32_t rcur; 52 uint8_t cbflg; 53 struct OsalMutex lock; 54 }; 55 56 struct UsbdBulkASyncReqNode { 57 struct DListHead node; 58 struct UsbRequest *request; 59 struct UsbdBulkASyncReqList *list; 60 int32_t use; 61 int32_t id; 62 }; 63 64 struct UsbdBulkASyncReqList { 65 struct UsbdBulkASyncReqNode node[USBD_BULKASYNCREQ_NUM_MAX]; 66 struct UsbdBulkASyncList *pList; 67 struct DListHead eList; 68 struct DListHead uList; 69 struct OsalMutex elock; 70 struct OsalMutex ulock; 71 }; 72 73 struct UsbdBulkASyncList { 74 struct HostDevice *instance; 75 struct UsbdBulkASyncList *next; 76 UsbInterfaceHandle *ifHandle; 77 sptr<HDI::Usb::V1_0::IUsbdBulkCallback> cb; 78 struct UsbdBulkASyncReqList rList; 79 struct UsbPipeInfo pipe; 80 struct UsbRequestParams params; 81 struct UsbdBufferHandle asmHandle; 82 uint8_t ifId; 83 uint8_t epId; 84 }; 85 86 struct HostDevice { 87 struct HdfSListNode node; 88 struct DataFifo readFifo; 89 struct HdfSList requestQueue; 90 struct OsalMutex requestLock; 91 struct HdfSList reqSyncList; 92 struct OsalMutex reqSyncLock; 93 struct HdfSList reqASyncList; 94 struct OsalMutex reqASyncLock; 95 struct OsalMutex writeLock; 96 struct OsalMutex readLock; 97 struct OsalMutex lock; 98 struct UsbRequest *ctrlReq; 99 struct UsbInterface *ctrIface; 100 struct UsbPipeInfo *ctrPipe; 101 struct UsbdBulkASyncList *bulkASyncList; 102 UsbInterfaceHandle *ctrDevHandle; 103 UsbImpl *service; 104 struct UsbInterface *iface[USB_MAX_INTERFACES]; 105 UsbInterfaceHandle *devHandle[USB_MAX_INTERFACES]; 106 uint8_t interfaceIndex[USB_MAX_INTERFACES]; 107 uint8_t busNum; 108 uint8_t devAddr; 109 uint8_t interfaceCnt; 110 bool initFlag; 111 }; 112 113 struct RequestMsg { 114 struct UsbRequest *request; 115 void *clientData; 116 uint32_t clientLength; 117 void *buffer; 118 uint32_t length; 119 }; 120 121 struct UsbControlParams { 122 uint8_t request; 123 UsbRequestTargetType target; 124 UsbControlRequestType reqType; 125 UsbRequestDirection directon; 126 uint16_t value; 127 uint16_t index; 128 void *data; 129 uint16_t size; 130 }; 131 132 struct UsbDescriptorParams { 133 UsbInterfaceHandle *devHandle; 134 struct UsbRequest *request; 135 uint8_t type; 136 uint8_t index; 137 void *buf; 138 uint16_t size; 139 }; 140 141 struct UsbdService { 142 struct IDeviceIoService service; 143 struct HdfDeviceObject *device; 144 struct UsbdSubscriber *subscriber; 145 struct UsbSession *session; 146 struct HdfSList devList; 147 struct OsalMutex lock; 148 }; 149 150 struct UsbdRequestSync { 151 struct HdfSListNode node; 152 struct UsbRequest *request; 153 UsbInterfaceHandle *ifHandle; 154 struct OsalMutex lock; 155 struct UsbPipeInfo pipe; 156 struct UsbRequestParams params; 157 uint8_t endPointAddr; 158 }; 159 160 struct UsbdRequestASync { 161 struct HdfSListNode node; 162 struct HdfSListNode qNode; 163 UsbInterfaceHandle *ifHandle; 164 struct RequestMsg reqMsg; 165 struct OsalMutex lock; 166 struct UsbPipeInfo pipe; 167 struct UsbRequestParams params; 168 uint8_t endPointAddr; 169 uint8_t status; 170 }; 171 172 struct UsbdSubscriber { 173 sptr<IUsbdSubscriber> subscriber; 174 void *impl; 175 struct HdfDevEventlistener usbPnpListener; 176 sptr<IRemoteObject> remote; 177 void *deathRecipient; 178 }; 179 } // namespace V1_0 180 } // namespace Usb 181 } // namespace HDI 182 } // namespace OHOS 183 #endif // USBD_H 184