1 /*
2 * Copyright (c) 2025 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 "usb_device_impl.h"
17
18 #include <cerrno>
19 #include <climits>
20 #include <hdf_base.h>
21 #include <hdf_log.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include "ddk_device_manager.h"
27 #include "ddk_pnp_listener_mgr.h"
28 #include "hitrace_meter.h"
29 #include "ipc_skeleton.h"
30 #include "libusb_adapter.h"
31 #include "parameter.h"
32 #include "parameters.h"
33 #include "usb_sa_subscriber.h"
34 #include "usbd_accessory.h"
35 #include "usbd_function.h"
36 #include "usbd_wrapper.h"
37
38 #define HDF_LOG_TAG UsbDeviceImpl
39 namespace {
40 constexpr const char* EDM_SYSTEM_CAPABILITY = "const.SystemCapability.Driver.ExternalDevice";
41 }
42 using namespace OHOS::HiviewDFX;
43 namespace OHOS {
44 namespace HDI {
45 namespace Usb {
46 namespace V2_0 {
47 HdfDevEventlistener UsbDeviceImpl::listenerForLoadService_ = {nullptr};
48 V1_2::UsbdLoadService UsbDeviceImpl::loadUsbService_ = {USB_SYSTEM_ABILITY_ID};
49 V1_2::UsbdLoadService UsbDeviceImpl::loadHdfEdm_ = {HDF_EXTERNAL_DEVICE_MANAGER_SA_ID};
50 UsbdSubscriber UsbDeviceImpl::subscribers_[MAX_SUBSCRIBER] = {{0}};
51 bool UsbDeviceImpl::isGadgetConnected_ = false;
52 bool UsbDeviceImpl::isEdmExist_ = false;
53 constexpr uint32_t FUNCTION_VALUE_MAX_LEN = 32;
54 static const std::map<std::string, uint32_t> configMap = {
55 {HDC_CONFIG_OFF, USB_FUNCTION_NONE},
56 {HDC_CONFIG_HDC, USB_FUNCTION_HDC},
57 {HDC_CONFIG_ON, USB_FUNCTION_HDC},
58 {HDC_CONFIG_RNDIS, USB_FUNCTION_RNDIS},
59 {HDC_CONFIG_STORAGE, USB_FUNCTION_STORAGE},
60 {HDC_CONFIG_RNDIS_HDC, USB_FUNCTION_HDC + USB_FUNCTION_RNDIS},
61 {HDC_CONFIG_STORAGE_HDC, USB_FUNCTION_HDC + USB_FUNCTION_STORAGE},
62 {HDC_CONFIG_MANUFACTURE_HDC, USB_FUNCTION_MANUFACTURE}
63 };
UsbDeviceInterfaceImplGetInstance(void)64 extern "C" IUsbDeviceInterface *UsbDeviceInterfaceImplGetInstance(void)
65 {
66 using OHOS::HDI::Usb::V2_0::UsbDeviceImpl;
67 UsbDeviceImpl *service = new (std::nothrow) UsbDeviceImpl();
68 if (service == nullptr) {
69 return nullptr;
70 }
71 return service;
72 }
73
UsbDeviceImpl()74 UsbDeviceImpl::UsbDeviceImpl()
75 {
76 V1_2::UsbdFunction::UsbdInitLock();
77 if (OHOS::system::GetBoolParameter("const.security.developermode.state", true)) {
78 loadUsbService_.LoadService();
79 }
80 }
81
~UsbDeviceImpl()82 UsbDeviceImpl::~UsbDeviceImpl()
83 {
84 V1_2::UsbdFunction::UsbdDestroyLock();
85 }
86
GetCurrentFunctions(int32_t & funcs)87 int32_t UsbDeviceImpl::GetCurrentFunctions(int32_t &funcs)
88 {
89 HDF_LOGI("%{public}s: enter", __func__);
90 funcs = V1_2::UsbdFunction::UsbdGetFunction();
91 return HDF_SUCCESS;
92 }
93
SetCurrentFunctions(int32_t funcs)94 int32_t UsbDeviceImpl::SetCurrentFunctions(int32_t funcs)
95 {
96 HDF_LOGI("%{public}s: enter", __func__);
97 int32_t ret = V1_2::UsbdFunction::UsbdSetFunction(funcs);
98 if (ret != HDF_SUCCESS) {
99 HDF_LOGE("%{public}s:UsbdSetFunction failed, ret:%{public}d", __func__, ret);
100 return ret;
101 }
102 return HDF_SUCCESS;
103 }
104
GetAccessoryInfo(std::vector<std::string> & accessoryInfo)105 int32_t UsbDeviceImpl::GetAccessoryInfo(std::vector<std::string> &accessoryInfo)
106 {
107 HDF_LOGI("%{public}s: enter", __func__);
108 return V1_2::UsbdAccessory::GetInstance().GetAccessoryInfo(accessoryInfo);
109 }
110
OpenAccessory(int32_t & fd)111 int32_t UsbDeviceImpl::OpenAccessory(int32_t &fd)
112 {
113 HDF_LOGI("%{public}s: enter", __func__);
114 return V1_2::UsbdAccessory::GetInstance().OpenAccessory(fd);
115 }
116
CloseAccessory(int32_t fd)117 int32_t UsbDeviceImpl::CloseAccessory(int32_t fd)
118 {
119 HDF_LOGI("%{public}s: enter", __func__);
120 return V1_2::UsbdAccessory::GetInstance().CloseAccessory(fd);
121 }
122
UsbdPnpLoaderEventReceived(void * priv,uint32_t id,HdfSBuf * data)123 int32_t UsbDeviceImpl::UsbdPnpLoaderEventReceived(void *priv, uint32_t id, HdfSBuf *data)
124 {
125 HDF_LOGI("%{public}s: enter %{public}u", __func__, id);
126 UsbdSubscriber *usbdSubscriber = static_cast<UsbdSubscriber *>(priv);
127 const sptr<IUsbdSubscriber> subscriber = usbdSubscriber->subscriber;
128
129 int32_t ret = HDF_SUCCESS;
130 if (id == USB_PNP_DRIVER_GADGET_ADD) {
131 HITRACE_METER_NAME(HITRACE_TAG_HDF, "USB_PNP_DRIVER_GADGET_ADD");
132 isGadgetConnected_ = true;
133 USBDeviceInfo info = {ACT_UPDEVICE, 0, 0};
134 if (subscriber == nullptr) {
135 HDF_LOGE("%{public}s: subscriber is nullptr, %{public}d", __func__, __LINE__);
136 return HDF_FAILURE;
137 }
138 ret = subscriber->DeviceEvent(info);
139 } else if (id == USB_PNP_DRIVER_GADGET_REMOVE) {
140 HITRACE_METER_NAME(HITRACE_TAG_HDF, "USB_PNP_DRIVER_GADGET_REMOVE");
141 isGadgetConnected_ = false;
142 USBDeviceInfo info = {ACT_DOWNDEVICE, 0, 0};
143 if (subscriber == nullptr) {
144 HDF_LOGE("%{public}s: subscriber is nullptr, %{public}d", __func__, __LINE__);
145 return HDF_FAILURE;
146 }
147 V1_2::UsbdAccessory::GetInstance().HandleEvent(ACT_DOWNDEVICE);
148 ret = subscriber->DeviceEvent(info);
149 } else if (id == USB_ACCESSORY_START) {
150 if (subscriber == nullptr) {
151 HDF_LOGE("%{public}s: subscriber is nullptr, %{public}d", __func__, __LINE__);
152 return HDF_FAILURE;
153 }
154 HITRACE_METER_NAME(HITRACE_TAG_HDF, "USB_ACCESSORY_START");
155 USBDeviceInfo info = {ACT_ACCESSORYUP, 0, 0};
156 ret = subscriber->DeviceEvent(info);
157 } else if (id == USB_ACCESSORY_SEND) {
158 if (subscriber == nullptr) {
159 HDF_LOGE("%{public}s: subsciber is nullptr, %{public}d", __func__, __LINE__);
160 return HDF_FAILURE;
161 }
162 HITRACE_METER_NAME(HITRACE_TAG_HDF, "USB_ACCESSORY_SEND");
163 USBDeviceInfo info = {ACT_ACCESSORYSEND, 0, 0};
164 ret = subscriber->DeviceEvent(info);
165 } else {
166 HDF_LOGW("%{public}s: device not support this id:%{public}u , %{public}d", __func__, id, __LINE__);
167 return HDF_ERR_NOT_SUPPORT;
168 }
169 HDF_LOGI("%{public}s: out", __func__);
170 return ret;
171 }
172
OnRemoteDied(const wptr<IRemoteObject> & object)173 void UsbDeviceImpl::UsbDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
174 {
175 int32_t i;
176 for (i = 0; i < MAX_SUBSCRIBER; i++) {
177 if (UsbDeviceImpl::subscribers_[i].subscriber == deathSubscriber_) {
178 break;
179 }
180 }
181 if (i == MAX_SUBSCRIBER) {
182 HDF_LOGE("%{public}s: current subscriber not bind", __func__);
183 return;
184 }
185 UsbDeviceImpl::subscribers_[i].subscriber = nullptr;
186 subscribers_[i].remote = nullptr;
187 subscribers_[i].deathRecipient = nullptr;
188 if (DdkListenerMgrRemove(&UsbDeviceImpl::subscribers_[i].usbPnpListener) != HDF_SUCCESS) {
189 HDF_LOGE("%{public}s: remove listerer failed", __func__);
190 }
191 }
192
BindUsbdDeviceSubscriber(const sptr<IUsbdSubscriber> & subscriber)193 int32_t UsbDeviceImpl::BindUsbdDeviceSubscriber(const sptr<IUsbdSubscriber> &subscriber)
194 {
195 int32_t i;
196 if (subscriber == nullptr) {
197 HDF_LOGE("%{public}s:subscriber is null", __func__);
198 return HDF_ERR_INVALID_PARAM;
199 }
200 const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<IUsbdSubscriber>(subscriber);
201 for (i = 0; i < MAX_SUBSCRIBER; i++) {
202 if (subscribers_[i].remote == remote) {
203 break;
204 }
205 }
206 if (i < MAX_SUBSCRIBER) {
207 HDF_LOGI("%{public}s: current subscriber was bind", __func__);
208 return HDF_SUCCESS;
209 }
210 for (i = 0; i < MAX_SUBSCRIBER; i++) {
211 if (subscribers_[i].subscriber == nullptr) {
212 subscribers_[i].subscriber = subscriber;
213 subscribers_[i].impl = this;
214 subscribers_[i].usbPnpListener.callBack = UsbdPnpLoaderEventReceived;
215 subscribers_[i].usbPnpListener.priv = &subscribers_[i];
216 subscribers_[i].remote = remote;
217 subscribers_[i].deathRecipient = new UsbDeviceImpl::UsbDeathRecipient(subscriber);
218 if (subscribers_[i].deathRecipient == nullptr) {
219 HDF_LOGE("%{public}s: new deathRecipient failed", __func__);
220 return HDF_FAILURE;
221 }
222 bool result = subscribers_[i].remote->AddDeathRecipient(
223 static_cast<UsbDeathRecipient *>(subscribers_[i].deathRecipient));
224 if (!result) {
225 HDF_LOGE("%{public}s:AddUsbDeathRecipient failed", __func__);
226 return HDF_FAILURE;
227 }
228
229 HDF_LOGI("%{public}s: index = %{public}d", __func__, i);
230 break;
231 }
232 }
233 if (i == MAX_SUBSCRIBER) {
234 HDF_LOGE("%{public}s: too many listeners", __func__);
235 return HDF_ERR_OUT_OF_RANGE;
236 }
237
238 if (DdkListenerMgrAdd(&subscribers_[i].usbPnpListener) != HDF_SUCCESS) {
239 HDF_LOGE("%{public}s: register listerer failed", __func__);
240 return HDF_FAILURE;
241 }
242 return HDF_SUCCESS;
243 }
244
UnbindUsbdDeviceSubscriber(const sptr<IUsbdSubscriber> & subscriber)245 int32_t UsbDeviceImpl::UnbindUsbdDeviceSubscriber(const sptr<IUsbdSubscriber> &subscriber)
246 {
247 if (subscriber == nullptr) {
248 HDF_LOGE("%{public}s:subscriber is null", __func__);
249 return HDF_ERR_INVALID_PARAM;
250 }
251 int32_t i;
252 const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<IUsbdSubscriber>(subscriber);
253 for (i = 0; i < MAX_SUBSCRIBER; i++) {
254 if (subscribers_[i].remote == remote) {
255 break;
256 }
257 }
258 if (i == MAX_SUBSCRIBER) {
259 HDF_LOGE("%{public}s: current subscriber not bind", __func__);
260 return HDF_DEV_ERR_NO_DEVICE;
261 }
262 bool result = remote->RemoveDeathRecipient(static_cast<UsbDeathRecipient *>(subscribers_[i].deathRecipient));
263 if (!result) {
264 HDF_LOGE("%{public}s:RemoveUsbDeathRecipient failed", __func__);
265 return HDF_FAILURE;
266 }
267
268 subscribers_[i].subscriber = nullptr;
269 subscribers_[i].remote = nullptr;
270 subscribers_[i].deathRecipient = nullptr;
271 if (DdkListenerMgrRemove(&subscribers_[i].usbPnpListener) != HDF_SUCCESS) {
272 HDF_LOGE("%{public}s: remove listerer failed", __func__);
273 return HDF_FAILURE;
274 }
275 return HDF_SUCCESS;
276 }
277
UsbdLoadServiceCallback(void * priv,uint32_t id,HdfSBuf * data)278 int32_t UsbDeviceImpl::UsbdLoadServiceCallback(void *priv, uint32_t id, HdfSBuf *data)
279 {
280 HDF_LOGI("%{public}s: enter", __func__);
281 (void)priv;
282 (void)data;
283 if (id == USB_PNP_DRIVER_GADGET_ADD || id == USB_PNP_NOTIFY_ADD_DEVICE) {
284 if (loadUsbService_.LoadService() != 0) {
285 HDF_LOGE("loadUsbService_ LoadService error");
286 return HDF_FAILURE;
287 }
288 if (isEdmExist_ && id == USB_PNP_NOTIFY_ADD_DEVICE) {
289 if (loadHdfEdm_.LoadService() != 0) {
290 HDF_LOGE("loadHdfEdm_ LoadService error");
291 return HDF_FAILURE;
292 }
293 }
294 }
295 return HDF_SUCCESS;
296 }
297
UpdateFunctionStatus()298 void UsbDeviceImpl::UpdateFunctionStatus()
299 {
300 HDF_LOGI("%{public}s: enter", __func__);
301 char cFunctionValue[FUNCTION_VALUE_MAX_LEN] = {0};
302 int32_t ret = GetParameter(PERSIST_SYS_USB_CONFIG, "invalid", cFunctionValue, FUNCTION_VALUE_MAX_LEN);
303 if (ret <= 0) {
304 HDF_LOGE("%{public}s: GetParameter failed", __func__);
305 }
306
307 std::string functionValue(cFunctionValue);
308 auto it = configMap.find(functionValue);
309 if (it != configMap.end()) {
310 HDF_LOGI("Function is %{public}s", functionValue.c_str());
311 ret = V1_2::UsbdFunction::UsbdUpdateFunction(it->second);
312 if (ret != HDF_SUCCESS) {
313 HDF_LOGE("%{public}s: UsbdUpdateFunction failed", __func__);
314 }
315 }
316 }
317
UsbdEventHandle(void)318 int32_t UsbDeviceImpl::UsbdEventHandle(void)
319 {
320 HDF_LOGI("%{public}s: enter", __func__);
321 UpdateFunctionStatus();
322 isEdmExist_ = OHOS::system::GetBoolParameter(EDM_SYSTEM_CAPABILITY, false);
323 HDF_LOGI("%{public}s: isEmdExit: %{public}d", __func__, isEdmExist_);
324 listenerForLoadService_.callBack = UsbdLoadServiceCallback;
325 int32_t ret = DdkListenerMgrAdd(&listenerForLoadService_);
326 if (ret != HDF_SUCCESS) {
327 HDF_LOGE("%{public}s: register listerer failed", __func__);
328 return HDF_FAILURE;
329 }
330 return ret;
331 }
332
UsbdEventHandleRelease(void)333 int32_t UsbDeviceImpl::UsbdEventHandleRelease(void)
334 {
335 HDF_LOGI("%{public}s: enter", __func__);
336 int32_t ret = DdkListenerMgrRemove(&listenerForLoadService_);
337 if (ret != HDF_SUCCESS) {
338 HDF_LOGE("%{public}s: DdkListenerMgrRemove failed", __func__);
339 }
340 listenerForLoadService_.callBack = nullptr;
341 listenerForLoadService_.priv = nullptr;
342 return ret;
343 }
344 } // namespace v2_0
345 } // namespace Usb
346 } // namespace HDI
347 } // namespace OHOS