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