• 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 SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex) override;
58     int32_t BulkTransferRead(
59         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data) override;
60     int32_t BulkTransferWrite(
61         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data) override;
62     int32_t ControlTransferRead(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &data) override;
63     int32_t ControlTransferWrite(
64         const UsbDev &dev, const UsbCtrlTransfer &ctrl, const std::vector<uint8_t> &data) override;
65     int32_t InterruptTransferRead(
66         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data) override;
67     int32_t InterruptTransferWrite(
68         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data) override;
69     int32_t IsoTransferRead(
70         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector<uint8_t> &data) override;
71     int32_t IsoTransferWrite(
72         const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector<uint8_t> &data) override;
73     int32_t RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
74         const std::vector<uint8_t> &buffer) override;
75     int32_t RequestWait(
76         const UsbDev &dev, std::vector<uint8_t> &clientData, std::vector<uint8_t> &buffer, int32_t timeout) override;
77     int32_t RequestCancel(const UsbDev &dev, const UsbPipe &pipe) override;
78     int32_t GetCurrentFunctions(int32_t &funcs) override;
79     int32_t SetCurrentFunctions(int32_t funcs) override;
80     int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override;
81     int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode) override;
82     int32_t BindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber) override;
83     int32_t UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber) override;
84     int32_t RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IUsbdBulkCallback> &cb) override;
85     int32_t UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe) override;
86     int32_t BulkRead(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem) override;
87     int32_t BulkWrite(const UsbDev &dev, const UsbPipe &pipe, const sptr<Ashmem> &ashmem) override;
88     int32_t BulkCancel(const UsbDev &dev, const UsbPipe &pipe) override;
89 
90     static int32_t UsbdRequestSyncReleaseList(HostDevice *port);
91     static int32_t UsbdRequestASyncReleaseList(HostDevice *port);
92     static int32_t UsbdBulkASyncListReleasePort(HostDevice *port);
93     static int32_t UsbdRequestASyncReleaseData(UsbdRequestASync *request);
94     static UsbInterfaceHandle *InterfaceIdToHandle(const HostDevice *dev, uint8_t id);
95     static int32_t UsbdEventHandle(const sptr<UsbImpl> &inst);
96     static int32_t UsbdEventHandleRelease(void);
97 
98     HostDevice *FindDevFromService(uint8_t busNum, uint8_t devAddr);
99 
100 private:
101     void MakeUsbControlParams(
102         UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index);
103     void MakeGetActiveUsbControlParams(
104         UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index);
105     int32_t UsbControlTransferEx(HostDevice *dev, UsbControlParams *ctrParams, int32_t timeout);
106     void MakeSetActiveUsbControlParams(
107         UsbControlParams *controlParams, uint8_t *buffer, uint16_t length, uint16_t value, uint16_t index);
108     static int32_t ReOpenDevice(HostDevice *port);
109     int32_t UsbdFindRequestSyncAndCreat(
110         HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr, UsbdRequestSync **request);
111     int32_t UsbdBulkReadSyncBase(
112         int32_t timeout, uint8_t *buffer, uint32_t size, uint32_t *actlength, UsbdRequestSync *requestSync);
113     int32_t UsbdBulkWriteSyncBase(
114         HostDevice *port, UsbdRequestSync *requestSync, const uint8_t *buffer, uint32_t length, int32_t timeout);
115     UsbdRequestASync *UsbdFindRequestASync(HostDevice *port, uint8_t interfaceId, uint8_t pipeAddr);
116     int32_t FunRequestQueueFillAndSubmit(
117         HostDevice *port, UsbdRequestASync *reqAsync, const uint8_t *buffer, uint32_t length);
118     int32_t GetRequestMsgFromQueue(HostDevice *port, UsbdRequestASync **reqMsg);
119     int32_t GetRequestMsgData(
120         HostDevice *port, UsbdRequestASync *reqMsg, int32_t timeout, uint8_t **buffer, uint32_t *length);
121 
122     int32_t BindUsbSubscriber(const sptr<IUsbdSubscriber> &subscriber);
123 
124     UsbdBulkASyncList *UsbdBulkASyncListInit(HostDevice *port, uint8_t ifId, uint8_t epId);
125     UsbdBulkASyncList *UsbdBulkASyncListFind(HostDevice *port, uint8_t ifId, uint8_t epId);
126     int32_t InitAsmBufferHandle(UsbdBufferHandle *handle, int32_t fd, int32_t size);
127     int32_t UsbdBulkReadASyncSubmitStart(UsbdBulkASyncList *list);
128     int32_t UsbdBulkASyncWriteSubmitStart(UsbdBulkASyncList *list);
129     void ReleaseAsmBufferHandle(UsbdBufferHandle *handle);
130     int32_t BulkRequestCancel(UsbdBulkASyncList *list);
131     int32_t HdfReadDevice(int32_t *count, int32_t *size, HdfSBuf *reply);
132     int32_t UsbdReleaseDevices();
133     static void ReportUsbdSysEvent(int32_t code, UsbPnpNotifyMatchInfoTable *infoTable);
134     static int32_t UsbdPnpNotifyAddAndRemoveDevice(HdfSBuf *data, UsbdSubscriber *usbdSubscriber, uint32_t id);
135     static int32_t UsbdPnpLoaderEventReceived(void *priv, uint32_t id, HdfSBuf *data);
136     static int32_t UsbdLoadServiceCallback(void *priv, uint32_t id, HdfSBuf *data);
137     class UsbDeathRecipient : public IRemoteObject::DeathRecipient {
138     public:
UsbDeathRecipient(const sptr<IUsbdSubscriber> & deathSubscriber)139         explicit UsbDeathRecipient(const sptr<IUsbdSubscriber> &deathSubscriber) : deathSubscriber_(deathSubscriber) {};
~UsbDeathRecipient()140         ~UsbDeathRecipient() override {};
141         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
142 
143     private:
144         sptr<IUsbdSubscriber> deathSubscriber_;
145     };
146 
147     void parsePortPath();
148 
149 private:
150     static HdfDevEventlistener listenerForLoadService_;
151     static UsbdSubscriber subscribers_[MAX_SUBSCRIBER];
152     static bool isGadgetConnected_;
153     static uint32_t attachCount_;
154     static uint32_t attachFailedCount_;
155     static UsbdLoadService loadUsbService_;
156     static UsbdLoadService loadHdfEdm_;
157 };
158 } // namespace V1_0
159 } // namespace Usb
160 } // namespace HDI
161 } // namespace OHOS
162 #endif // OHOS_HDI_USB_V1_0_USBIMPL_H
163