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 #include <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_log.h>
19 #include <hdf_sbuf_ipc.h>
20
21 #include "hdf_usb_pnp_manage.h"
22 #include "usb_impl.h"
23 #include "usbd_dispatcher.h"
24 #include "v1_0/usb_interface_stub.h"
25
26 #define HDF_LOG_TAG Usbd
27
28 using namespace OHOS::HDI::Usb::V1_0;
29
30 struct HdfUsbInterfaceHost {
31 struct IDeviceIoService ioService;
32 OHOS::sptr<OHOS::IRemoteObject> stub;
33 };
34
UsbInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)35 static int32_t UsbInterfaceDriverDispatch(
36 struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
37 {
38 auto *hdfUsbInterfaceHost = CONTAINER_OF(client->device->service, struct HdfUsbInterfaceHost, ioService);
39
40 OHOS::MessageParcel *dataParcel = nullptr;
41 OHOS::MessageParcel *replyParcel = nullptr;
42 OHOS::MessageOption option;
43
44 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
45 HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
46 return HDF_ERR_INVALID_PARAM;
47 }
48 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
49 HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
50 return HDF_ERR_INVALID_PARAM;
51 }
52
53 return hdfUsbInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
54 }
55
HdfUsbInterfaceDriverInit(struct HdfDeviceObject * const deviceObject)56 static int HdfUsbInterfaceDriverInit(struct HdfDeviceObject * const deviceObject)
57 {
58 if (deviceObject == nullptr) {
59 HDF_LOGE("%{public}s:deviceObject is nullptr", __func__);
60 return HDF_ERR_INVALID_OBJECT;
61 }
62 return HDF_SUCCESS;
63 }
64
HdfUsbInterfaceDriverBind(struct HdfDeviceObject * const deviceObject)65 static int HdfUsbInterfaceDriverBind(struct HdfDeviceObject * const deviceObject)
66 {
67 if (deviceObject == nullptr) {
68 HDF_LOGE("%{public}s:deviceObject is nullptr", __func__);
69 return HDF_ERR_INVALID_OBJECT;
70 }
71 auto *hdfUsbInterfaceHost = new (std::nothrow) HdfUsbInterfaceHost;
72 if (hdfUsbInterfaceHost == nullptr) {
73 HDF_LOGE("%{public}s: failed to create HdfUsbInterfaceHost object", __func__);
74 return HDF_FAILURE;
75 }
76
77 hdfUsbInterfaceHost->ioService.Dispatch = UsbInterfaceDriverDispatch;
78 hdfUsbInterfaceHost->ioService.Open = nullptr;
79 hdfUsbInterfaceHost->ioService.Release = nullptr;
80
81 auto serviceImpl = IUsbInterface::Get(true);
82 if (serviceImpl == nullptr) {
83 HDF_LOGE("%{public}s: failed to get of implement service", __func__);
84 delete hdfUsbInterfaceHost;
85 return HDF_FAILURE;
86 }
87
88 hdfUsbInterfaceHost->stub =
89 OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, IUsbInterface::GetDescriptor());
90 if (hdfUsbInterfaceHost->stub == nullptr) {
91 HDF_LOGE("%{public}s: failed to get stub object", __func__);
92 delete hdfUsbInterfaceHost;
93 return HDF_FAILURE;
94 }
95 deviceObject->service = &hdfUsbInterfaceHost->ioService;
96
97 sptr<UsbImpl> impl = static_cast<UsbImpl *>(serviceImpl.GetRefPtr());
98 impl->device_ = deviceObject;
99 int32_t ret = UsbImpl::UsbdEventHandle(impl);
100 if (ret != HDF_SUCCESS) {
101 HDF_LOGE("%{public}s: UsbdEventHandle failed", __func__);
102 return HDF_FAILURE;
103 }
104 return HDF_SUCCESS;
105 }
106
HdfUsbInterfaceDriverRelease(struct HdfDeviceObject * const deviceObject)107 static void HdfUsbInterfaceDriverRelease(struct HdfDeviceObject * const deviceObject)
108 {
109 if (deviceObject->service == nullptr) {
110 HDF_LOGE("HdfUsbInterfaceDriverRelease not initted");
111 return;
112 }
113
114 int32_t ret = UsbImpl::UsbdEventHandleRelease();
115 if (ret != HDF_SUCCESS) {
116 HDF_LOGE("%{public}s:UsbdEventHandleRelease ret=%{public}d", __func__, ret);
117 }
118 auto *hdfUsbInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfUsbInterfaceHost, ioService);
119 delete hdfUsbInterfaceHost;
120 }
121
122 static struct HdfDriverEntry g_usbInterfaceDriverEntry = {
123 .moduleVersion = 1,
124 .moduleName = "usbd",
125 .Bind = HdfUsbInterfaceDriverBind,
126 .Init = HdfUsbInterfaceDriverInit,
127 .Release = HdfUsbInterfaceDriverRelease,
128 };
129
130 #ifdef __cplusplus
131 extern "C" {
132 #endif /* __cplusplus */
133 HDF_INIT(g_usbInterfaceDriverEntry);
134 #ifdef __cplusplus
135 }
136 #endif /* __cplusplus */
137