1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2022 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 OHOS_HDI_USB_V1_1_USBD_DISPATCHER_H 17094332d3Sopenharmony_ci#define OHOS_HDI_USB_V1_1_USBD_DISPATCHER_H 18094332d3Sopenharmony_ci 19094332d3Sopenharmony_ci#include "usbd.h" 20094332d3Sopenharmony_ci 21094332d3Sopenharmony_ci#define HDF_LOG_TAG Usbd 22094332d3Sopenharmony_ci 23094332d3Sopenharmony_ci#define MAX_BUFF_SIZE 16384 24094332d3Sopenharmony_ci#define MAX_CONTROL_BUFF_SIZE 1024 25094332d3Sopenharmony_ci#define READ_BUF_SIZE 8192 26094332d3Sopenharmony_ci 27094332d3Sopenharmony_ci#define USB_CTRL_SET_TIMEOUT 5000 28094332d3Sopenharmony_ci#define GET_STRING_SET_TIMEOUT 50 29094332d3Sopenharmony_ci#define USB_PIPE_DIR_OFFSET 7 30094332d3Sopenharmony_ci#define CHARFORMAT 8 31094332d3Sopenharmony_ci#define USB_REUQEST_SLEEP_TIME 100 32094332d3Sopenharmony_ci#define USB_MAX_DESCRIPTOR_SIZE 256 33094332d3Sopenharmony_ci#define USB_BULK_CALLBACK_SLEEP_TIME 500 34094332d3Sopenharmony_ci#define USB_BULK_CANCEL_SLEEP_TIME 1000 35094332d3Sopenharmony_ci 36094332d3Sopenharmony_ci#define OPEN_SLEPP_TIME 1 37094332d3Sopenharmony_ci#define SUBMIT_SLEEP_TIME 1 38094332d3Sopenharmony_ci#define USBD_ASYNC_GETENODE_TIME 1 39094332d3Sopenharmony_ci#define USBD_ASYNC_GETENODE_TRY 3 40094332d3Sopenharmony_ci 41094332d3Sopenharmony_ci#define POS_STEP 3 42094332d3Sopenharmony_ci 43094332d3Sopenharmony_ci#define MULTIPLE 3 44094332d3Sopenharmony_ci 45094332d3Sopenharmony_ci#define USB_RECIP_MASK 0x1F 46094332d3Sopenharmony_ci#define ENDPOINT_DIRECTION_MASK 0x1 47094332d3Sopenharmony_ci#define CMD_TYPE_MASK 0x3 48094332d3Sopenharmony_ci 49094332d3Sopenharmony_ci#define MAX_REQUESTASYNC_NUM 20 50094332d3Sopenharmony_ci 51094332d3Sopenharmony_ciconstexpr int32_t CMD_OFFSET_5 = 5; 52094332d3Sopenharmony_ciconstexpr int32_t DIRECTION_OFFSET_7 = 7; 53094332d3Sopenharmony_ciconstexpr int32_t TYPE_OFFSET_8 = 8; 54094332d3Sopenharmony_ci 55094332d3Sopenharmony_cienum UsbdReqNodeStatus { 56094332d3Sopenharmony_ci USBD_REQNODE_INIT, 57094332d3Sopenharmony_ci USBD_REQNODE_NOUSE, 58094332d3Sopenharmony_ci USBD_REQNODE_USE, 59094332d3Sopenharmony_ci USBD_REQNODE_OTHER, 60094332d3Sopenharmony_ci}; 61094332d3Sopenharmony_ci 62094332d3Sopenharmony_cinamespace OHOS { 63094332d3Sopenharmony_cinamespace HDI { 64094332d3Sopenharmony_cinamespace Usb { 65094332d3Sopenharmony_cinamespace V1_1 { 66094332d3Sopenharmony_ciclass UsbImpl; 67094332d3Sopenharmony_ciclass UsbdDispatcher { 68094332d3Sopenharmony_cipublic: 69094332d3Sopenharmony_ci static int32_t UsbdAllocFifo(DataFifo *fifo, uint32_t size); 70094332d3Sopenharmony_ci static void UsbdFreeFifo(DataFifo *fifo); 71094332d3Sopenharmony_ci static void UsbdReadCallback(UsbRequest *req); 72094332d3Sopenharmony_ci static void UsbdWriteCallback(UsbRequest *req); 73094332d3Sopenharmony_ci static int32_t UsbControlSetUp(UsbControlParams *controlParams, UsbControlRequest *controlReq); 74094332d3Sopenharmony_ci static UsbInterface *GetUsbInterfaceById(const HostDevice *dev, uint8_t interfaceIndex); 75094332d3Sopenharmony_ci static int32_t GetInterfacePipe( 76094332d3Sopenharmony_ci const HostDevice *dev, UsbInterface *interface, uint8_t pipeAddr, UsbPipeInfo *pipe); 77094332d3Sopenharmony_ci static int32_t GetPipe(const HostDevice *dev, uint8_t interfaceId, uint8_t pipeId, UsbPipeInfo *pipe); 78094332d3Sopenharmony_ci static void UsbdFreeCtrlPipe(HostDevice *dev); 79094332d3Sopenharmony_ci static int32_t UsbdGetCtrlPipe(HostDevice *dev); 80094332d3Sopenharmony_ci static UsbdRequestSync *UsbdFindRequestSync(HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr); 81094332d3Sopenharmony_ci static UsbdRequestSync *UsbdRequestSyncAlloc(void); 82094332d3Sopenharmony_ci static void UsbRequestParamsWSyncInit(UsbRequestParams *params, int32_t timeout, const UsbPipeInfo *pipe); 83094332d3Sopenharmony_ci static int32_t UsbdRequestSyncInit( 84094332d3Sopenharmony_ci HostDevice *port, UsbInterfaceHandle *ifHandle, UsbPipeInfo *pipe, UsbdRequestSync *requestSync); 85094332d3Sopenharmony_ci static int32_t UsbdRequestSyncInitwithLength(HostDevice *port, UsbInterfaceHandle *ifHandle, 86094332d3Sopenharmony_ci UsbPipeInfo *pipe, int32_t length, UsbdRequestSync *requestSync); 87094332d3Sopenharmony_ci static int32_t UsbdRequestSyncRelease(UsbdRequestSync *requestSync); 88094332d3Sopenharmony_ci static void UsbRequestParamsInit(UsbRequestParams *params, int32_t timeout); 89094332d3Sopenharmony_ci static int32_t CtrlTranParamGetReqType(HdfSBuf *data, UsbControlParams *pCtrParams, uint32_t requestType); 90094332d3Sopenharmony_ci static int32_t CtrlTransferParamInit(HdfSBuf *data, UsbControlParams *pCtrParams, int32_t *timeout); 91094332d3Sopenharmony_ci static void UsbdReleaseInterfaces(HostDevice *dev); 92094332d3Sopenharmony_ci static void UsbdCloseInterfaces(HostDevice *dev); 93094332d3Sopenharmony_ci static int32_t UsbdOpenInterfaces(HostDevice *dev); 94094332d3Sopenharmony_ci static void RemoveDevFromService(UsbImpl *service, HostDevice *port); 95094332d3Sopenharmony_ci static int32_t UsbdClaimInterfaces(HostDevice *dev); 96094332d3Sopenharmony_ci static int32_t ReturnGetPipes(int32_t ret, HostDevice *dev); 97094332d3Sopenharmony_ci static int32_t ReturnOpenInterfaces(int32_t ret, HostDevice *dev); 98094332d3Sopenharmony_ci static int32_t ReturnClainInterfaces(int32_t ret, HostDevice *dev); 99094332d3Sopenharmony_ci static int32_t UsbdInit(HostDevice *dev); 100094332d3Sopenharmony_ci static int32_t UsbdRequestASyncRelease(UsbdRequestASync *request); 101094332d3Sopenharmony_ci static int32_t UsbdBulkASyncReqRelease(UsbdBulkASyncReqList *list); 102094332d3Sopenharmony_ci static int32_t UsbdBulkASyncListRelease(UsbdBulkASyncList *list); 103094332d3Sopenharmony_ci static void UsbdRelease(HostDevice *dev); 104094332d3Sopenharmony_ci static int32_t UsbdMallocAndFill(uint8_t *&dataAddr, const std::vector<uint8_t> &data); 105094332d3Sopenharmony_ci static int32_t FillReqAyncParams( 106094332d3Sopenharmony_ci UsbdRequestASync *userData, UsbPipeInfo *pipe, 107094332d3Sopenharmony_ci UsbRequestParams *params, const uint8_t *buffer, uint32_t length); 108094332d3Sopenharmony_ci static UsbdRequestASync *UsbdRequestASyncAlloc(void); 109094332d3Sopenharmony_ci static int32_t UsbdRequestASyncInit( 110094332d3Sopenharmony_ci HostDevice *port, UsbInterfaceHandle *ifHandle, UsbPipeInfo *pipe, UsbdRequestASync *request); 111094332d3Sopenharmony_ci static UsbdRequestASync *UsbdRequestASyncCreatAndInsert(HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr); 112094332d3Sopenharmony_ci static int32_t HostDeviceInit(HostDevice *port); 113094332d3Sopenharmony_ci static int32_t HostDeviceCreate(HostDevice **port); 114094332d3Sopenharmony_ci static int32_t FunAttachDevice(HostDevice *port, HdfSBuf *data, HdfSBuf *reply); 115094332d3Sopenharmony_ci static int32_t UsbdDeviceCreateAndAttach(const sptr<UsbImpl> &service, uint8_t busNum, uint8_t devAddr); 116094332d3Sopenharmony_ci static int32_t FunDetachDevice(HostDevice *port, HdfSBuf *data); 117094332d3Sopenharmony_ci static int32_t UsbdDeviceDettach(UsbImpl *service, uint8_t busNum, uint8_t devAddr); 118094332d3Sopenharmony_ci static HostDevice *UsbdFindDevForBusNum(UsbImpl *service, uint8_t busNum); 119094332d3Sopenharmony_ci static int32_t UsbdRemoveBusDev(UsbImpl *service, uint8_t busNum, const sptr<IUsbdSubscriber> &subscriber); 120094332d3Sopenharmony_ci static int32_t UsbdBulkASyncReqInit(UsbdBulkASyncReqList *list, UsbdBulkASyncList *pList); 121094332d3Sopenharmony_ci static UsbdBulkASyncList *UsbdBulkASyncListAlloc(HostDevice *port, uint8_t ifId, uint8_t epId); 122094332d3Sopenharmony_ci static int32_t UsbdBulkASyncReqNodeSetNoUse(UsbdBulkASyncReqNode *db); 123094332d3Sopenharmony_ci static UsbdBulkASyncReqNode *UsbdBulkASyncReqGetENode(UsbdBulkASyncReqList *list); 124094332d3Sopenharmony_ci static int32_t UsbdBulkReadRemoteCallback( 125094332d3Sopenharmony_ci const sptr<IUsbdBulkCallback> &service, int32_t status, UsbdBufferHandle *handle); 126094332d3Sopenharmony_ci static int32_t UsbdBulkWriteRemoteCallback( 127094332d3Sopenharmony_ci const sptr<IUsbdBulkCallback> &service, int32_t status, UsbdBufferHandle *handle); 128094332d3Sopenharmony_ci static int32_t UsbdBulkASyncPutAsmData(UsbdBufferHandle *handle, uint8_t *buffer, uint32_t len); 129094332d3Sopenharmony_ci static int32_t UsbdBulkAsyncGetAsmData(UsbdBufferHandle *handle, UsbRequestParams *params, uint16_t maxPacketSize); 130094332d3Sopenharmony_ci static int32_t UsbdBulkAsyncGetAsmReqLen(UsbdBufferHandle *handle, uint32_t *reqLen, uint16_t maxPacketSize); 131094332d3Sopenharmony_ci static int32_t UsbdBulkASyncReqWriteAutoSubmit(UsbRequest *request); 132094332d3Sopenharmony_ci static int32_t UsbdBulkASyncReqReadAutoSubmit(UsbRequest *request); 133094332d3Sopenharmony_ci static void UsbdBulkASyncWriteCallbackAutoSubmit(UsbRequest *request); 134094332d3Sopenharmony_ci static void UsbdBulkASyncReadCallbackAutoSubmit(UsbRequest *request); 135094332d3Sopenharmony_ci static int32_t UsbdBulkASyncReqFillParams(UsbPipeInfo *pipe, UsbRequestParams *params, uint8_t *buffer); 136094332d3Sopenharmony_ci static int32_t UsbdBulkASyncReqWriteSubmit(UsbdBulkASyncReqNode *req); 137094332d3Sopenharmony_ci static int32_t UsbdBulkASyncReqReadSubmit(UsbdBulkASyncReqNode *db); 138094332d3Sopenharmony_ci}; 139094332d3Sopenharmony_ci} // namespace V1_1 140094332d3Sopenharmony_ci} // namespace Usb 141094332d3Sopenharmony_ci} // namespace HDI 142094332d3Sopenharmony_ci} // namespace OHOS 143094332d3Sopenharmony_ci#endif // OHOS_HDI_USB_V1_1_USBD_DISPATCHER_H 144