• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_0_USBIMPL_H
17 #define OHOS_HDI_USB_V1_0_USBIMPL_H
18 
19 #include "hdf_slist.h"
20 #include "hdf_usb_pnp_manage.h"
21 #include "iproxy_broker.h"
22 #include "iremote_object.h"
23 #include "osal_mutex.h"
24 #include "usb_session.h"
25 #include "usbd.h"
26 #include "usbd_load_usb_service.h"
27 #include "v1_0/iusb_interface.h"
28 
29 #define BASE_CLASS_HUB 0x09
30 
31 constexpr uint8_t MAX_INTERFACEID = 0xFF;
32 
33 namespace OHOS {
34 namespace HDI {
35 namespace Usb {
36 namespace V1_0 {
37 class UsbImpl : public IUsbInterface {
38 public:
39     OsalMutex lock_;
40     HdfSList devList_;
41     UsbSession *session_;
42     HdfDeviceObject *device_;
43 
44     UsbImpl();
45     ~UsbImpl() override;
46     int32_t OpenDevice(const UsbDev &dev) override;
47     int32_t CloseDevice(const UsbDev &dev) override;
48     int32_t GetDeviceDescriptor(const UsbDev &dev, std::vector<uint8_t> &decriptor) override;
49     int32_t GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor) override;
50     int32_t GetConfigDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor) override;
51     int32_t GetRawDescriptor(const UsbDev &dev, std::vector<uint8_t> &decriptor) override;
52     int32_t GetFileDescriptor(const UsbDev &dev, int32_t &fd) override;
53     int32_t SetConfig(const UsbDev &dev, uint8_t configIndex) override;
54     int32_t GetConfig(const UsbDev &dev, uint8_t &configIndex) override;
55     int32_t ClaimInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t force) override;
56     int32_t ReleaseInterface(const UsbDev &dev, uint8_t interfaceId) override;
57     int32_t ManageInterface(const UsbDev &dev, uint8_t interfaceId, bool disable) override;
58     int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex) override;
59     int32_t BulkTransferRead(
60         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data) override;
61     int32_t BulkTransferWrite(
62         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data) override;
63     int32_t ControlTransferRead(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &data) override;
64     int32_t ControlTransferWrite(
65         const UsbDev &dev, const UsbCtrlTransfer &ctrl, const std::vector<uint8_t> &data) override;
66     int32_t InterruptTransferRead(
67         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data) override;
68     int32_t InterruptTransferWrite(
69         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data) override;
70     int32_t IsoTransferRead(
71         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data) override;
72     int32_t IsoTransferWrite(
73         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data) override;
74     int32_t RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
75         const std::vector<uint8_t> &buffer) override;
76     int32_t RequestWait(
77         const UsbDev &dev, std::vector<uint8_t> &clientData, std::vector<uint8_t> &buffer, int32_t timeout) override;
78     int32_t RequestCancel(const UsbDev &dev, const UsbPipe &pipe) override;
79     int32_t GetCurrentFunctions(int32_t &funcs) override;
80     int32_t SetCurrentFunctions(int32_t funcs) override;
81     int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override;
82     int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode) override;
83     int32_t BindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber) override;
84     int32_t UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber) override;
85     int32_t RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IUsbdBulkCallback> &cb) override;
86     int32_t UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe) override;
87     int32_t BulkRead(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem) override;
88     int32_t BulkWrite(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem) override;
89     int32_t BulkCancel(const UsbDev &dev, const UsbPipe &pipe) override;
90 
91     static int32_t UsbdRequestSyncReleaseList(HostDevice *port);
92     static int32_t UsbdRequestASyncReleaseList(HostDevice *port);
93     static int32_t UsbdBulkASyncListReleasePort(HostDevice *port);
94     static int32_t UsbdRequestASyncReleaseData(UsbdRequestASync *request);
95     static UsbInterfaceHandle *InterfaceIdToHandle(const HostDevice *dev, uint8_t id);
96     static int32_t UsbdEventHandle(const sptr<UsbImpl> &inst);
97     static int32_t UsbdEventHandleRelease(void);
98 
99     HostDevice *FindDevFromService(uint8_t busNum, uint8_t devAddr);
100 
101 private:
102     void MakeUsbControlParams(
103         UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index);
104     void MakeGetActiveUsbControlParams(
105         UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index);
106     int32_t UsbControlTransferEx(HostDevice *dev, UsbControlParams *ctrParams, int32_t timeout);
107     void MakeSetActiveUsbControlParams(
108         UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index);
109     static int32_t ReOpenDevice(HostDevice *port);
110     int32_t UsbdFindRequestSyncAndCreat(
111         HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr, UsbdRequestSync **request);
112     int32_t UsbdBulkReadSyncBase(
113         int32_t timeout, uint8_t *buffer, uint32_t size, uint32_t *actlength, UsbdRequestSync *requestSync);
114     int32_t UsbdBulkWriteSyncBase(
115         HostDevice *port, UsbdRequestSync *requestSync, const uint8_t *buffer, uint32_t length, int32_t timeout);
116     UsbdRequestASync *UsbdFindRequestASync(HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr);
117     int32_t FunRequestQueueFillAndSubmit(
118         HostDevice *port, UsbdRequestASync *reqAsync, const uint8_t *buffer, uint32_t length);
119     int32_t GetRequestMsgFromQueue(HostDevice *port, UsbdRequestASync **reqMsg);
120     int32_t GetRequestMsgData(
121         HostDevice *port, UsbdRequestASync *reqMsg, int32_t timeout, uint8_t **buffer, uint32_t *length);
122 
123     int32_t BindUsbSubscriber(const sptr<IUsbdSubscriber> &subscriber);
124 
125     UsbdBulkASyncList *UsbdBulkASyncListInit(HostDevice *port, uint8_t ifId, uint8_t epId);
126     UsbdBulkASyncList *UsbdBulkASyncListFind(HostDevice *port, uint8_t ifId, uint8_t epId);
127     int32_t InitAsmBufferHandle(UsbdBufferHandle *handle, int32_t fd, int32_t size);
128     int32_t UsbdBulkReadASyncSubmitStart(UsbdBulkASyncList *list);
129     int32_t UsbdBulkASyncWriteSubmitStart(UsbdBulkASyncList *list);
130     void ReleaseAsmBufferHandle(UsbdBufferHandle *handle);
131     int32_t BulkRequestCancel(UsbdBulkASyncList *list);
132     int32_t HdfReadDevice(int32_t *count, int32_t *size, HdfSBuf *reply);
133     int32_t UsbdReleaseDevices();
134     static void ReportUsbdSysEvent(int32_t code, UsbPnpNotifyMatchInfoTable *infoTable);
135     static int32_t UsbdPnpNotifyAddAndRemoveDevice(HdfSBuf *data, UsbdSubscriber *usbdSubscriber, uint32_t id);
136     static int32_t UsbdPnpLoaderEventReceived(void *priv, uint32_t id, HdfSBuf *data);
137     static int32_t UsbdLoadServiceCallback(void *priv, uint32_t id, HdfSBuf *data);
138     class UsbDeathRecipient : public IRemoteObject::DeathRecipient {
139     public:
UsbDeathRecipient(const sptr<IUsbdSubscriber> & deathSubscriber)140         explicit UsbDeathRecipient(const sptr<IUsbdSubscriber> &deathSubscriber) : deathSubscriber_(deathSubscriber) {};
~UsbDeathRecipient()141         ~UsbDeathRecipient() override {};
142         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
143 
144     private:
145         sptr<IUsbdSubscriber> deathSubscriber_;
146     };
147 
148     void parsePortPath();
149 
150 private:
151     static HdfDevEventlistener listenerForLoadService_;
152     static UsbdSubscriber subscribers_[MAX_SUBSCRIBER];
153     static bool isGadgetConnected_;
154     static uint32_t attachCount_;
155     static uint32_t attachFailedCount_;
156     static UsbdLoadService loadUsbService_;
157     static UsbdLoadService loadHdfEdm_;
158 };
159 } // namespace V1_0
160 } // namespace Usb
161 } // namespace HDI
162 } // namespace OHOS
163 #endif // OHOS_HDI_USB_V1_0_USBIMPL_H
164