1 /*
2 * Copyright (c) 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 #include "v1_0/usb_ddk_stub.h"
17 #include <hdf_base.h>
18 #include <hdf_device_desc.h>
19 #include <hdf_log.h>
20 #include <hdf_sbuf_ipc.h>
21
22 #define HDF_LOG_TAG usb_ddk_driver
23
24 using namespace OHOS::HDI::Usb::Ddk::V1_0;
25
26 struct HdfUsbDdkHost {
27 struct IDeviceIoService ioService;
28 OHOS::sptr<OHOS::IRemoteObject> stub;
29 };
30
UsbDdkDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)31 static int32_t UsbDdkDriverDispatch(
32 struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
33 {
34 auto *hdfUsbDdkHost = CONTAINER_OF(client->device->service, struct HdfUsbDdkHost, ioService);
35
36 OHOS::MessageParcel *dataParcel = nullptr;
37 OHOS::MessageParcel *replyParcel = nullptr;
38 OHOS::MessageOption option;
39
40 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
41 HDF_LOGE("%{public}s: invalid data sbuf object to dispatch", __func__);
42 return HDF_ERR_INVALID_PARAM;
43 }
44 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
45 HDF_LOGE("%{public}s: invalid reply sbuf object to dispatch", __func__);
46 return HDF_ERR_INVALID_PARAM;
47 }
48
49 return hdfUsbDdkHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
50 }
51
HdfUsbDdkDriverInit(struct HdfDeviceObject * deviceObject)52 static int HdfUsbDdkDriverInit(struct HdfDeviceObject *deviceObject)
53 {
54 HDF_LOGI("%{public}s: driver init start", __func__);
55 return HDF_SUCCESS;
56 }
57
HdfUsbDdkDriverBind(struct HdfDeviceObject * deviceObject)58 static int HdfUsbDdkDriverBind(struct HdfDeviceObject *deviceObject)
59 {
60 HDF_LOGI("%{public}s: driver bind start", __func__);
61 auto *hdfUsbDdkHost = new (std::nothrow) HdfUsbDdkHost;
62 if (hdfUsbDdkHost == nullptr) {
63 HDF_LOGE("%{public}s: failed to create create HdfUsbDdkHost object", __func__);
64 return HDF_FAILURE;
65 }
66
67 hdfUsbDdkHost->ioService.Dispatch = UsbDdkDriverDispatch;
68 hdfUsbDdkHost->ioService.Open = NULL;
69 hdfUsbDdkHost->ioService.Release = NULL;
70
71 auto serviceImpl = OHOS::HDI::Usb::Ddk::V1_0::IUsbDdk::Get(true);
72 if (serviceImpl == nullptr) {
73 HDF_LOGE("%{public}s: failed to get of implement service", __func__);
74 delete hdfUsbDdkHost;
75 return HDF_FAILURE;
76 }
77
78 hdfUsbDdkHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(
79 serviceImpl, OHOS::HDI::Usb::Ddk::V1_0::IUsbDdk::GetDescriptor());
80 if (hdfUsbDdkHost->stub == nullptr) {
81 HDF_LOGE("%{public}s: failed to get stub object", __func__);
82 delete hdfUsbDdkHost;
83 return HDF_FAILURE;
84 }
85
86 deviceObject->service = &hdfUsbDdkHost->ioService;
87 return HDF_SUCCESS;
88 }
89
HdfUsbDdkDriverRelease(struct HdfDeviceObject * deviceObject)90 static void HdfUsbDdkDriverRelease(struct HdfDeviceObject *deviceObject)
91 {
92 HDF_LOGI("%{public}s: driver release start", __func__);
93 if (deviceObject->service == nullptr) {
94 return;
95 }
96
97 auto *hdfUsbDdkHost = CONTAINER_OF(deviceObject->service, struct HdfUsbDdkHost, ioService);
98 if (hdfUsbDdkHost != nullptr) {
99 delete hdfUsbDdkHost;
100 }
101 }
102
103 static struct HdfDriverEntry g_usbddkDriverEntry = {
104 .moduleVersion = 1,
105 .moduleName = "",
106 .Bind = HdfUsbDdkDriverBind,
107 .Init = HdfUsbDdkDriverInit,
108 .Release = HdfUsbDdkDriverRelease,
109 };
110 #ifdef __cplusplus
111 extern "C" {
112 #endif /* __cplusplus */
113 HDF_INIT(g_usbddkDriverEntry);
114 #ifdef __cplusplus
115 }
116 #endif /* __cplusplus */
117