1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022-2023 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_USBIMPL_H
17094332d3Sopenharmony_ci#define OHOS_HDI_USB_V1_1_USBIMPL_H
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_ci#include "hdf_slist.h"
20094332d3Sopenharmony_ci#include "hdf_usb_pnp_manage.h"
21094332d3Sopenharmony_ci#include "iproxy_broker.h"
22094332d3Sopenharmony_ci#include "iremote_object.h"
23094332d3Sopenharmony_ci#include "osal_mutex.h"
24094332d3Sopenharmony_ci#include "usb_session.h"
25094332d3Sopenharmony_ci#include "usbd.h"
26094332d3Sopenharmony_ci#include "usbd_load_usb_service.h"
27094332d3Sopenharmony_ci#include "v1_1/iusb_interface.h"
28094332d3Sopenharmony_ci
29094332d3Sopenharmony_ci#define BASE_CLASS_HUB 0x09
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_ciconstexpr uint8_t MAX_INTERFACEID = 0xFF;
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_cinamespace OHOS {
34094332d3Sopenharmony_cinamespace HDI {
35094332d3Sopenharmony_cinamespace Usb {
36094332d3Sopenharmony_cinamespace V1_1 {
37094332d3Sopenharmony_ciclass UsbImpl : public IUsbInterface {
38094332d3Sopenharmony_cipublic:
39094332d3Sopenharmony_ci    OsalMutex lock_;
40094332d3Sopenharmony_ci    HdfSList devList_;
41094332d3Sopenharmony_ci    UsbSession *session_;
42094332d3Sopenharmony_ci    HdfDeviceObject *device_;
43094332d3Sopenharmony_ci
44094332d3Sopenharmony_ci    UsbImpl();
45094332d3Sopenharmony_ci    ~UsbImpl() override;
46094332d3Sopenharmony_ci    int32_t OpenDevice(const UsbDev &dev) override;
47094332d3Sopenharmony_ci    int32_t CloseDevice(const UsbDev &dev) override;
48094332d3Sopenharmony_ci    int32_t ResetDevice(const UsbDev &dev) override;
49094332d3Sopenharmony_ci    int32_t GetDeviceDescriptor(const UsbDev &dev, std::vector<uint8_t> &decriptor) override;
50094332d3Sopenharmony_ci    int32_t GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor) override;
51094332d3Sopenharmony_ci    int32_t GetConfigDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor) override;
52094332d3Sopenharmony_ci    int32_t GetRawDescriptor(const UsbDev &dev, std::vector<uint8_t> &decriptor) override;
53094332d3Sopenharmony_ci    int32_t GetFileDescriptor(const UsbDev &dev, int32_t &fd) override;
54094332d3Sopenharmony_ci    int32_t GetDeviceFileDescriptor(const UsbDev &dev, int32_t &fd) override;
55094332d3Sopenharmony_ci    int32_t SetConfig(const UsbDev &dev, uint8_t configIndex) override;
56094332d3Sopenharmony_ci    int32_t GetConfig(const UsbDev &dev, uint8_t &configIndex) override;
57094332d3Sopenharmony_ci    int32_t ClaimInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t force) override;
58094332d3Sopenharmony_ci    int32_t ReleaseInterface(const UsbDev &dev, uint8_t interfaceId) override;
59094332d3Sopenharmony_ci    int32_t ManageInterface(const UsbDev &dev, uint8_t interfaceId, bool disable) override;
60094332d3Sopenharmony_ci    int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex) override;
61094332d3Sopenharmony_ci    int32_t BulkTransferRead(
62094332d3Sopenharmony_ci        const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data) override;
63094332d3Sopenharmony_ci    int32_t BulkTransferReadwithLength(const UsbDev &dev,
64094332d3Sopenharmony_ci        const UsbPipe &pipe, int32_t timeout, int32_t length, std::vector<uint8_t> &data) override;
65094332d3Sopenharmony_ci    int32_t BulkTransferWrite(
66094332d3Sopenharmony_ci        const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data) override;
67094332d3Sopenharmony_ci    int32_t ControlTransferRead(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &data) override;
68094332d3Sopenharmony_ci    int32_t ControlTransferWrite(
69094332d3Sopenharmony_ci        const UsbDev &dev, const UsbCtrlTransfer &ctrl, const std::vector<uint8_t> &data) override;
70094332d3Sopenharmony_ci    int32_t ControlTransferReadwithLength(
71094332d3Sopenharmony_ci        const UsbDev &dev, const UsbCtrlTransferParams &ctrlParams, std::vector<uint8_t> &data) override;
72094332d3Sopenharmony_ci    int32_t InterruptTransferRead(
73094332d3Sopenharmony_ci        const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data) override;
74094332d3Sopenharmony_ci    int32_t InterruptTransferWrite(
75094332d3Sopenharmony_ci        const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data) override;
76094332d3Sopenharmony_ci    int32_t IsoTransferRead(
77094332d3Sopenharmony_ci        const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data) override;
78094332d3Sopenharmony_ci    int32_t IsoTransferWrite(
79094332d3Sopenharmony_ci        const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data) override;
80094332d3Sopenharmony_ci    int32_t RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
81094332d3Sopenharmony_ci        const std::vector<uint8_t> &buffer) override;
82094332d3Sopenharmony_ci    int32_t RequestWait(
83094332d3Sopenharmony_ci        const UsbDev &dev, std::vector<uint8_t> &clientData, std::vector<uint8_t> &buffer, int32_t timeout) override;
84094332d3Sopenharmony_ci    int32_t RequestCancel(const UsbDev &dev, const UsbPipe &pipe) override;
85094332d3Sopenharmony_ci    int32_t GetCurrentFunctions(int32_t &funcs) override;
86094332d3Sopenharmony_ci    int32_t SetCurrentFunctions(int32_t funcs) override;
87094332d3Sopenharmony_ci    int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override;
88094332d3Sopenharmony_ci    int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode) override;
89094332d3Sopenharmony_ci    int32_t BindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber) override;
90094332d3Sopenharmony_ci    int32_t UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber) override;
91094332d3Sopenharmony_ci    int32_t RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IUsbdBulkCallback> &cb) override;
92094332d3Sopenharmony_ci    int32_t UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe) override;
93094332d3Sopenharmony_ci    int32_t BulkRead(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem) override;
94094332d3Sopenharmony_ci    int32_t BulkWrite(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem) override;
95094332d3Sopenharmony_ci    int32_t BulkCancel(const UsbDev &dev, const UsbPipe &pipe) override;
96094332d3Sopenharmony_ci    int32_t ClearHalt(const UsbDev &dev, const UsbPipe &pipe) override;
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_ci    static int32_t UsbdRequestSyncReleaseList(HostDevice *port);
99094332d3Sopenharmony_ci    static int32_t UsbdRequestASyncReleaseList(HostDevice *port);
100094332d3Sopenharmony_ci
101094332d3Sopenharmony_ci    static int32_t UsbdBulkASyncListReleasePort(HostDevice *port);
102094332d3Sopenharmony_ci    static int32_t UsbdRequestASyncReleaseData(UsbdRequestASync *request);
103094332d3Sopenharmony_ci    static UsbInterfaceHandle *InterfaceIdToHandle(const HostDevice *dev, uint8_t id);
104094332d3Sopenharmony_ci    static void UpdateFunctionStatus();
105094332d3Sopenharmony_ci    static int32_t UsbdEventHandle(const sptr<UsbImpl> &inst);
106094332d3Sopenharmony_ci    static int32_t UsbdEventHandleRelease(void);
107094332d3Sopenharmony_ci
108094332d3Sopenharmony_ci    HostDevice *FindDevFromService(uint8_t busNum, uint8_t devAddr);
109094332d3Sopenharmony_ci    int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool &unactivated) override;
110094332d3Sopenharmony_ci    int32_t GetDeviceSpeed(const UsbDev &dev, uint8_t &speed) override;
111094332d3Sopenharmony_ci
112094332d3Sopenharmony_ciprivate:
113094332d3Sopenharmony_ci    void MakeUsbControlParams(
114094332d3Sopenharmony_ci        UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index);
115094332d3Sopenharmony_ci    void MakeGetActiveUsbControlParams(
116094332d3Sopenharmony_ci        UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index);
117094332d3Sopenharmony_ci    int32_t UsbControlTransferEx(HostDevice *dev, UsbControlParams *ctrParams, int32_t timeout);
118094332d3Sopenharmony_ci    void MakeSetActiveUsbControlParams(
119094332d3Sopenharmony_ci        UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index);
120094332d3Sopenharmony_ci    static int32_t ReOpenDevice(HostDevice *port);
121094332d3Sopenharmony_ci    int32_t UsbdFindRequestSyncAndCreat(
122094332d3Sopenharmony_ci        HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr, UsbdRequestSync **request);
123094332d3Sopenharmony_ci    int32_t UsbdFindRequestSyncAndCreatwithLength(HostDevice *port, uint8_t interfaceId,
124094332d3Sopenharmony_ci        uint8_t pipeAddr, int32_t length, UsbdRequestSync **request);
125094332d3Sopenharmony_ci    int32_t UsbdBulkReadSyncBase(
126094332d3Sopenharmony_ci        int32_t timeout, uint8_t *buffer, uint32_t size, uint32_t *actlength, UsbdRequestSync *requestSync);
127094332d3Sopenharmony_ci    int32_t UsbdBulkWriteSyncBase(
128094332d3Sopenharmony_ci        HostDevice *port, UsbdRequestSync *requestSync, const uint8_t *buffer, uint32_t length, int32_t timeout);
129094332d3Sopenharmony_ci    UsbdRequestASync *UsbdFindRequestASync(HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr);
130094332d3Sopenharmony_ci    int32_t FunRequestQueueFillAndSubmit(
131094332d3Sopenharmony_ci        HostDevice *port, UsbdRequestASync *reqAsync, const uint8_t *buffer, uint32_t length);
132094332d3Sopenharmony_ci    int32_t GetRequestMsgFromQueue(HostDevice *port, UsbdRequestASync **reqMsg);
133094332d3Sopenharmony_ci    int32_t GetRequestMsgData(
134094332d3Sopenharmony_ci        HostDevice *port, UsbdRequestASync *reqMsg, int32_t timeout, uint8_t **buffer, uint32_t *length);
135094332d3Sopenharmony_ci
136094332d3Sopenharmony_ci    int32_t BindUsbSubscriber(const sptr<IUsbdSubscriber> &subscriber);
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_ci    UsbdBulkASyncList *UsbdBulkASyncListInit(HostDevice *port, uint8_t ifId, uint8_t epId);
139094332d3Sopenharmony_ci    UsbdBulkASyncList *UsbdBulkASyncListFind(HostDevice *port, uint8_t ifId, uint8_t epId);
140094332d3Sopenharmony_ci    int32_t InitAsmBufferHandle(UsbdBufferHandle *handle, int32_t fd, int32_t size);
141094332d3Sopenharmony_ci    int32_t UsbdBulkReadASyncSubmitStart(UsbdBulkASyncList *list);
142094332d3Sopenharmony_ci    int32_t UsbdBulkASyncWriteSubmitStart(UsbdBulkASyncList *list);
143094332d3Sopenharmony_ci    void ReleaseAsmBufferHandle(UsbdBufferHandle *handle);
144094332d3Sopenharmony_ci    int32_t BulkRequestCancel(UsbdBulkASyncList *list);
145094332d3Sopenharmony_ci    int32_t HdfReadDevice(int32_t *count, int32_t *size, HdfSBuf *reply);
146094332d3Sopenharmony_ci    int32_t UsbdReleaseDevices();
147094332d3Sopenharmony_ci    static void ReportUsbdSysEvent(int32_t code, UsbPnpNotifyMatchInfoTable *infoTable);
148094332d3Sopenharmony_ci    static int32_t UsbdPnpNotifyAddAndRemoveDevice(HdfSBuf *data, UsbdSubscriber *usbdSubscriber, uint32_t id);
149094332d3Sopenharmony_ci    static int32_t UsbdPnpLoaderEventReceived(void *priv, uint32_t id, HdfSBuf *data);
150094332d3Sopenharmony_ci    static int32_t UsbdLoadServiceCallback(void *priv, uint32_t id, HdfSBuf *data);
151094332d3Sopenharmony_ci    class UsbDeathRecipient : public IRemoteObject::DeathRecipient {
152094332d3Sopenharmony_ci    public:
153094332d3Sopenharmony_ci        explicit UsbDeathRecipient(const sptr<IUsbdSubscriber> &deathSubscriber) : deathSubscriber_(deathSubscriber) {};
154094332d3Sopenharmony_ci        ~UsbDeathRecipient() override {};
155094332d3Sopenharmony_ci        void OnRemoteDied(const wptr<IRemoteObject> &object) override;
156094332d3Sopenharmony_ci
157094332d3Sopenharmony_ci    private:
158094332d3Sopenharmony_ci        sptr<IUsbdSubscriber> deathSubscriber_;
159094332d3Sopenharmony_ci    };
160094332d3Sopenharmony_ci
161094332d3Sopenharmony_ci    void parsePortPath();
162094332d3Sopenharmony_ci
163094332d3Sopenharmony_ciprivate:
164094332d3Sopenharmony_ci    static HdfDevEventlistener listenerForLoadService_;
165094332d3Sopenharmony_ci    static UsbdSubscriber subscribers_[MAX_SUBSCRIBER];
166094332d3Sopenharmony_ci    static bool isGadgetConnected_;
167094332d3Sopenharmony_ci    static uint32_t attachCount_;
168094332d3Sopenharmony_ci    static uint32_t attachFailedCount_;
169094332d3Sopenharmony_ci    static UsbdLoadService loadUsbService_;
170094332d3Sopenharmony_ci    static UsbdLoadService loadHdfEdm_;
171094332d3Sopenharmony_ci};
172094332d3Sopenharmony_ci} // namespace V1_1
173094332d3Sopenharmony_ci} // namespace Usb
174094332d3Sopenharmony_ci} // namespace HDI
175094332d3Sopenharmony_ci} // namespace OHOS
176094332d3Sopenharmony_ci#endif // OHOS_HDI_USB_V1_1_USBIMPL_H
177