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 OHOS_HDI_USB_V1_1_USBD_DISPATCHER_H
17#define OHOS_HDI_USB_V1_1_USBD_DISPATCHER_H
18
19#include "usbd.h"
20
21#define HDF_LOG_TAG Usbd
22
23#define MAX_BUFF_SIZE         16384
24#define MAX_CONTROL_BUFF_SIZE 1024
25#define READ_BUF_SIZE         8192
26
27#define USB_CTRL_SET_TIMEOUT         5000
28#define GET_STRING_SET_TIMEOUT       50
29#define USB_PIPE_DIR_OFFSET          7
30#define CHARFORMAT                   8
31#define USB_REUQEST_SLEEP_TIME       100
32#define USB_MAX_DESCRIPTOR_SIZE      256
33#define USB_BULK_CALLBACK_SLEEP_TIME 500
34#define USB_BULK_CANCEL_SLEEP_TIME   1000
35
36#define OPEN_SLEPP_TIME          1
37#define SUBMIT_SLEEP_TIME        1
38#define USBD_ASYNC_GETENODE_TIME 1
39#define USBD_ASYNC_GETENODE_TRY  3
40
41#define POS_STEP 3
42
43#define MULTIPLE   3
44
45#define USB_RECIP_MASK          0x1F
46#define ENDPOINT_DIRECTION_MASK 0x1
47#define CMD_TYPE_MASK           0x3
48
49#define MAX_REQUESTASYNC_NUM 20
50
51constexpr int32_t CMD_OFFSET_5 = 5;
52constexpr int32_t DIRECTION_OFFSET_7 = 7;
53constexpr int32_t TYPE_OFFSET_8 = 8;
54
55enum UsbdReqNodeStatus {
56    USBD_REQNODE_INIT,
57    USBD_REQNODE_NOUSE,
58    USBD_REQNODE_USE,
59    USBD_REQNODE_OTHER,
60};
61
62namespace OHOS {
63namespace HDI {
64namespace Usb {
65namespace V1_1 {
66class UsbImpl;
67class UsbdDispatcher {
68public:
69    static int32_t UsbdAllocFifo(DataFifo *fifo, uint32_t size);
70    static void UsbdFreeFifo(DataFifo *fifo);
71    static void UsbdReadCallback(UsbRequest *req);
72    static void UsbdWriteCallback(UsbRequest *req);
73    static int32_t UsbControlSetUp(UsbControlParams *controlParams, UsbControlRequest *controlReq);
74    static UsbInterface *GetUsbInterfaceById(const HostDevice *dev, uint8_t interfaceIndex);
75    static int32_t GetInterfacePipe(
76        const HostDevice *dev, UsbInterface *interface, uint8_t pipeAddr, UsbPipeInfo *pipe);
77    static int32_t GetPipe(const HostDevice *dev, uint8_t interfaceId, uint8_t pipeId, UsbPipeInfo *pipe);
78    static void UsbdFreeCtrlPipe(HostDevice *dev);
79    static int32_t UsbdGetCtrlPipe(HostDevice *dev);
80    static UsbdRequestSync *UsbdFindRequestSync(HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr);
81    static UsbdRequestSync *UsbdRequestSyncAlloc(void);
82    static void UsbRequestParamsWSyncInit(UsbRequestParams *params, int32_t timeout, const UsbPipeInfo *pipe);
83    static int32_t UsbdRequestSyncInit(
84        HostDevice *port, UsbInterfaceHandle *ifHandle, UsbPipeInfo *pipe, UsbdRequestSync *requestSync);
85    static int32_t UsbdRequestSyncInitwithLength(HostDevice *port, UsbInterfaceHandle *ifHandle,
86        UsbPipeInfo *pipe, int32_t length, UsbdRequestSync *requestSync);
87    static int32_t UsbdRequestSyncRelease(UsbdRequestSync *requestSync);
88    static void UsbRequestParamsInit(UsbRequestParams *params, int32_t timeout);
89    static int32_t CtrlTranParamGetReqType(HdfSBuf *data, UsbControlParams *pCtrParams, uint32_t requestType);
90    static int32_t CtrlTransferParamInit(HdfSBuf *data, UsbControlParams *pCtrParams, int32_t *timeout);
91    static void UsbdReleaseInterfaces(HostDevice *dev);
92    static void UsbdCloseInterfaces(HostDevice *dev);
93    static int32_t UsbdOpenInterfaces(HostDevice *dev);
94    static void RemoveDevFromService(UsbImpl *service, HostDevice *port);
95    static int32_t UsbdClaimInterfaces(HostDevice *dev);
96    static int32_t ReturnGetPipes(int32_t ret, HostDevice *dev);
97    static int32_t ReturnOpenInterfaces(int32_t ret, HostDevice *dev);
98    static int32_t ReturnClainInterfaces(int32_t ret, HostDevice *dev);
99    static int32_t UsbdInit(HostDevice *dev);
100    static int32_t UsbdRequestASyncRelease(UsbdRequestASync *request);
101    static int32_t UsbdBulkASyncReqRelease(UsbdBulkASyncReqList *list);
102    static int32_t UsbdBulkASyncListRelease(UsbdBulkASyncList *list);
103    static void UsbdRelease(HostDevice *dev);
104    static int32_t UsbdMallocAndFill(uint8_t *&dataAddr, const std::vector<uint8_t> &data);
105    static int32_t FillReqAyncParams(
106        UsbdRequestASync *userData, UsbPipeInfo *pipe,
107        UsbRequestParams *params, const uint8_t *buffer, uint32_t length);
108    static UsbdRequestASync *UsbdRequestASyncAlloc(void);
109    static int32_t UsbdRequestASyncInit(
110        HostDevice *port, UsbInterfaceHandle *ifHandle, UsbPipeInfo *pipe, UsbdRequestASync *request);
111    static UsbdRequestASync *UsbdRequestASyncCreatAndInsert(HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr);
112    static int32_t HostDeviceInit(HostDevice *port);
113    static int32_t HostDeviceCreate(HostDevice **port);
114    static int32_t FunAttachDevice(HostDevice *port, HdfSBuf *data, HdfSBuf *reply);
115    static int32_t UsbdDeviceCreateAndAttach(const sptr<UsbImpl> &service, uint8_t busNum, uint8_t devAddr);
116    static int32_t FunDetachDevice(HostDevice *port, HdfSBuf *data);
117    static int32_t UsbdDeviceDettach(UsbImpl *service, uint8_t busNum, uint8_t devAddr);
118    static HostDevice *UsbdFindDevForBusNum(UsbImpl *service, uint8_t busNum);
119    static int32_t UsbdRemoveBusDev(UsbImpl *service, uint8_t busNum, const sptr<IUsbdSubscriber> &subscriber);
120    static int32_t UsbdBulkASyncReqInit(UsbdBulkASyncReqList *list, UsbdBulkASyncList *pList);
121    static UsbdBulkASyncList *UsbdBulkASyncListAlloc(HostDevice *port, uint8_t ifId, uint8_t epId);
122    static int32_t UsbdBulkASyncReqNodeSetNoUse(UsbdBulkASyncReqNode *db);
123    static UsbdBulkASyncReqNode *UsbdBulkASyncReqGetENode(UsbdBulkASyncReqList *list);
124    static int32_t UsbdBulkReadRemoteCallback(
125        const sptr<IUsbdBulkCallback> &service, int32_t status, UsbdBufferHandle *handle);
126    static int32_t UsbdBulkWriteRemoteCallback(
127        const sptr<IUsbdBulkCallback> &service, int32_t status, UsbdBufferHandle *handle);
128    static int32_t UsbdBulkASyncPutAsmData(UsbdBufferHandle *handle, uint8_t *buffer, uint32_t len);
129    static int32_t UsbdBulkAsyncGetAsmData(UsbdBufferHandle *handle, UsbRequestParams *params, uint16_t maxPacketSize);
130    static int32_t UsbdBulkAsyncGetAsmReqLen(UsbdBufferHandle *handle, uint32_t *reqLen, uint16_t maxPacketSize);
131    static int32_t UsbdBulkASyncReqWriteAutoSubmit(UsbRequest *request);
132    static int32_t UsbdBulkASyncReqReadAutoSubmit(UsbRequest *request);
133    static void UsbdBulkASyncWriteCallbackAutoSubmit(UsbRequest *request);
134    static void UsbdBulkASyncReadCallbackAutoSubmit(UsbRequest *request);
135    static int32_t UsbdBulkASyncReqFillParams(UsbPipeInfo *pipe, UsbRequestParams *params, uint8_t *buffer);
136    static int32_t UsbdBulkASyncReqWriteSubmit(UsbdBulkASyncReqNode *req);
137    static int32_t UsbdBulkASyncReqReadSubmit(UsbdBulkASyncReqNode *db);
138};
139} // namespace V1_1
140} // namespace Usb
141} // namespace HDI
142} // namespace OHOS
143#endif // OHOS_HDI_USB_V1_1_USBD_DISPATCHER_H
144