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