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