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