1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include <hdf_base.h> 17094332d3Sopenharmony_ci#include <hdf_device_desc.h> 18094332d3Sopenharmony_ci#include <hdf_log.h> 19094332d3Sopenharmony_ci#include <hdf_sbuf_ipc.h> 20094332d3Sopenharmony_ci 21094332d3Sopenharmony_ci#include "hdf_usb_pnp_manage.h" 22094332d3Sopenharmony_ci#include "usb_impl.h" 23094332d3Sopenharmony_ci#include "usbd_dispatcher.h" 24094332d3Sopenharmony_ci#include "v1_1/usb_interface_stub.h" 25094332d3Sopenharmony_ci#include "usbd_wrapper.h" 26094332d3Sopenharmony_ci 27094332d3Sopenharmony_ci#define HDF_LOG_TAG Usbd 28094332d3Sopenharmony_ci 29094332d3Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_1; 30094332d3Sopenharmony_ci 31094332d3Sopenharmony_cistruct HdfUsbInterfaceHost { 32094332d3Sopenharmony_ci struct IDeviceIoService ioService; 33094332d3Sopenharmony_ci OHOS::sptr<OHOS::IRemoteObject> stub; 34094332d3Sopenharmony_ci}; 35094332d3Sopenharmony_ci 36094332d3Sopenharmony_cistatic int32_t UsbInterfaceDriverDispatch( 37094332d3Sopenharmony_ci struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply) 38094332d3Sopenharmony_ci{ 39094332d3Sopenharmony_ci if (client == nullptr || client->device == nullptr || client->device->service == nullptr) { 40094332d3Sopenharmony_ci HDF_LOGE("%{public}s:invalid param", __func__); 41094332d3Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 42094332d3Sopenharmony_ci } 43094332d3Sopenharmony_ci OHOS::MessageParcel *dataParcel = nullptr; 44094332d3Sopenharmony_ci OHOS::MessageParcel *replyParcel = nullptr; 45094332d3Sopenharmony_ci OHOS::MessageOption option; 46094332d3Sopenharmony_ci 47094332d3Sopenharmony_ci if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) { 48094332d3Sopenharmony_ci HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__); 49094332d3Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 50094332d3Sopenharmony_ci } 51094332d3Sopenharmony_ci if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) { 52094332d3Sopenharmony_ci HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__); 53094332d3Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 54094332d3Sopenharmony_ci } 55094332d3Sopenharmony_ci 56094332d3Sopenharmony_ci auto *hdfUsbInterfaceHost = CONTAINER_OF(client->device->service, struct HdfUsbInterfaceHost, ioService); 57094332d3Sopenharmony_ci if (hdfUsbInterfaceHost == nullptr || hdfUsbInterfaceHost->stub == nullptr) { 58094332d3Sopenharmony_ci HDF_LOGE("%{public}s:host or stub are nullptr", __func__); 59094332d3Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 60094332d3Sopenharmony_ci } 61094332d3Sopenharmony_ci return hdfUsbInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option); 62094332d3Sopenharmony_ci} 63094332d3Sopenharmony_ci 64094332d3Sopenharmony_cistatic int HdfUsbInterfaceDriverInit(struct HdfDeviceObject * const deviceObject) 65094332d3Sopenharmony_ci{ 66094332d3Sopenharmony_ci if (deviceObject == nullptr) { 67094332d3Sopenharmony_ci HDF_LOGE("%{public}s:deviceObject is nullptr", __func__); 68094332d3Sopenharmony_ci return HDF_ERR_INVALID_OBJECT; 69094332d3Sopenharmony_ci } 70094332d3Sopenharmony_ci return HDF_SUCCESS; 71094332d3Sopenharmony_ci} 72094332d3Sopenharmony_ci 73094332d3Sopenharmony_cistatic int HdfUsbInterfaceDriverBind(struct HdfDeviceObject * const deviceObject) 74094332d3Sopenharmony_ci{ 75094332d3Sopenharmony_ci if (deviceObject == nullptr) { 76094332d3Sopenharmony_ci HDF_LOGE("%{public}s:deviceObject is nullptr", __func__); 77094332d3Sopenharmony_ci return HDF_ERR_INVALID_OBJECT; 78094332d3Sopenharmony_ci } 79094332d3Sopenharmony_ci auto *hdfUsbInterfaceHost = new (std::nothrow) HdfUsbInterfaceHost; 80094332d3Sopenharmony_ci if (hdfUsbInterfaceHost == nullptr) { 81094332d3Sopenharmony_ci HDF_LOGE("%{public}s: failed to create HdfUsbInterfaceHost object", __func__); 82094332d3Sopenharmony_ci return HDF_FAILURE; 83094332d3Sopenharmony_ci } 84094332d3Sopenharmony_ci 85094332d3Sopenharmony_ci hdfUsbInterfaceHost->ioService.Dispatch = UsbInterfaceDriverDispatch; 86094332d3Sopenharmony_ci hdfUsbInterfaceHost->ioService.Open = nullptr; 87094332d3Sopenharmony_ci hdfUsbInterfaceHost->ioService.Release = nullptr; 88094332d3Sopenharmony_ci 89094332d3Sopenharmony_ci auto serviceImpl = OHOS::HDI::Usb::V1_1::IUsbInterface::Get(true); 90094332d3Sopenharmony_ci if (serviceImpl == nullptr) { 91094332d3Sopenharmony_ci HDF_LOGE("%{public}s: failed to get of implement service", __func__); 92094332d3Sopenharmony_ci delete hdfUsbInterfaceHost; 93094332d3Sopenharmony_ci hdfUsbInterfaceHost = nullptr; 94094332d3Sopenharmony_ci return HDF_FAILURE; 95094332d3Sopenharmony_ci } 96094332d3Sopenharmony_ci 97094332d3Sopenharmony_ci hdfUsbInterfaceHost->stub = 98094332d3Sopenharmony_ci OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, 99094332d3Sopenharmony_ci OHOS::HDI::Usb::V1_1::IUsbInterface::GetDescriptor()); 100094332d3Sopenharmony_ci if (hdfUsbInterfaceHost->stub == nullptr) { 101094332d3Sopenharmony_ci HDF_LOGE("%{public}s: failed to get stub object", __func__); 102094332d3Sopenharmony_ci delete hdfUsbInterfaceHost; 103094332d3Sopenharmony_ci hdfUsbInterfaceHost = nullptr; 104094332d3Sopenharmony_ci return HDF_FAILURE; 105094332d3Sopenharmony_ci } 106094332d3Sopenharmony_ci 107094332d3Sopenharmony_ci sptr<UsbImpl> impl = static_cast<UsbImpl *>(serviceImpl.GetRefPtr()); 108094332d3Sopenharmony_ci impl->device_ = deviceObject; 109094332d3Sopenharmony_ci int32_t ret = UsbImpl::UsbdEventHandle(impl); 110094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 111094332d3Sopenharmony_ci HDF_LOGE("%{public}s: UsbdEventHandle failed", __func__); 112094332d3Sopenharmony_ci hdfUsbInterfaceHost->stub = nullptr; 113094332d3Sopenharmony_ci delete hdfUsbInterfaceHost; 114094332d3Sopenharmony_ci hdfUsbInterfaceHost = nullptr; 115094332d3Sopenharmony_ci return HDF_FAILURE; 116094332d3Sopenharmony_ci } 117094332d3Sopenharmony_ci 118094332d3Sopenharmony_ci deviceObject->service = &hdfUsbInterfaceHost->ioService; 119094332d3Sopenharmony_ci return HDF_SUCCESS; 120094332d3Sopenharmony_ci} 121094332d3Sopenharmony_ci 122094332d3Sopenharmony_cistatic void HdfUsbInterfaceDriverRelease(struct HdfDeviceObject *const deviceObject) 123094332d3Sopenharmony_ci{ 124094332d3Sopenharmony_ci int32_t ret = UsbImpl::UsbdEventHandleRelease(); 125094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 126094332d3Sopenharmony_ci HDF_LOGW("%{public}s:UsbdEventHandleRelease ret=%{public}d", __func__, ret); 127094332d3Sopenharmony_ci } 128094332d3Sopenharmony_ci 129094332d3Sopenharmony_ci if (deviceObject == nullptr || deviceObject->service == nullptr) { 130094332d3Sopenharmony_ci HDF_LOGE("HdfUsbInterfaceDriverRelease not initted"); 131094332d3Sopenharmony_ci return; 132094332d3Sopenharmony_ci } 133094332d3Sopenharmony_ci 134094332d3Sopenharmony_ci auto *hdfUsbInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfUsbInterfaceHost, ioService); 135094332d3Sopenharmony_ci if (hdfUsbInterfaceHost == nullptr) { 136094332d3Sopenharmony_ci HDF_LOGE("%{public}s:invalid param", __func__); 137094332d3Sopenharmony_ci return; 138094332d3Sopenharmony_ci } 139094332d3Sopenharmony_ci delete hdfUsbInterfaceHost; 140094332d3Sopenharmony_ci hdfUsbInterfaceHost = nullptr; 141094332d3Sopenharmony_ci deviceObject->service = nullptr; 142094332d3Sopenharmony_ci return; 143094332d3Sopenharmony_ci} 144094332d3Sopenharmony_ci 145094332d3Sopenharmony_cistatic struct HdfDriverEntry g_usbInterfaceDriverEntry = { 146094332d3Sopenharmony_ci .moduleVersion = 1, 147094332d3Sopenharmony_ci .moduleName = "usbd", 148094332d3Sopenharmony_ci .Bind = HdfUsbInterfaceDriverBind, 149094332d3Sopenharmony_ci .Init = HdfUsbInterfaceDriverInit, 150094332d3Sopenharmony_ci .Release = HdfUsbInterfaceDriverRelease, 151094332d3Sopenharmony_ci}; 152094332d3Sopenharmony_ci 153094332d3Sopenharmony_ci#ifdef __cplusplus 154094332d3Sopenharmony_ciextern "C" { 155094332d3Sopenharmony_ci#endif /* __cplusplus */ 156094332d3Sopenharmony_ciHDF_INIT(g_usbInterfaceDriverEntry); 157094332d3Sopenharmony_ci#ifdef __cplusplus 158094332d3Sopenharmony_ci} 159094332d3Sopenharmony_ci#endif /* __cplusplus */ 160