/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include "hdf_usb_pnp_manage.h" #include "usb_impl.h" #include "usbd_dispatcher.h" #include "v1_0/usb_interface_stub.h" #define HDF_LOG_TAG Usbd using namespace OHOS::HDI::Usb::V1_0; struct HdfUsbInterfaceHost { struct IDeviceIoService ioService; OHOS::sptr stub; }; static int32_t UsbInterfaceDriverDispatch( struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply) { auto *hdfUsbInterfaceHost = CONTAINER_OF(client->device->service, struct HdfUsbInterfaceHost, ioService); OHOS::MessageParcel *dataParcel = nullptr; OHOS::MessageParcel *replyParcel = nullptr; OHOS::MessageOption option; if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) { HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__); return HDF_ERR_INVALID_PARAM; } if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) { HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__); return HDF_ERR_INVALID_PARAM; } return hdfUsbInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option); } static int HdfUsbInterfaceDriverInit(struct HdfDeviceObject * const deviceObject) { if (deviceObject == nullptr) { HDF_LOGE("%{public}s:deviceObject is nullptr", __func__); return HDF_ERR_INVALID_OBJECT; } return HDF_SUCCESS; } static int HdfUsbInterfaceDriverBind(struct HdfDeviceObject * const deviceObject) { if (deviceObject == nullptr) { HDF_LOGE("%{public}s:deviceObject is nullptr", __func__); return HDF_ERR_INVALID_OBJECT; } auto *hdfUsbInterfaceHost = new (std::nothrow) HdfUsbInterfaceHost; if (hdfUsbInterfaceHost == nullptr) { HDF_LOGE("%{public}s: failed to create HdfUsbInterfaceHost object", __func__); return HDF_FAILURE; } hdfUsbInterfaceHost->ioService.Dispatch = UsbInterfaceDriverDispatch; hdfUsbInterfaceHost->ioService.Open = nullptr; hdfUsbInterfaceHost->ioService.Release = nullptr; auto serviceImpl = IUsbInterface::Get(true); if (serviceImpl == nullptr) { HDF_LOGE("%{public}s: failed to get of implement service", __func__); delete hdfUsbInterfaceHost; return HDF_FAILURE; } hdfUsbInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, IUsbInterface::GetDescriptor()); if (hdfUsbInterfaceHost->stub == nullptr) { HDF_LOGE("%{public}s: failed to get stub object", __func__); delete hdfUsbInterfaceHost; return HDF_FAILURE; } deviceObject->service = &hdfUsbInterfaceHost->ioService; sptr impl = static_cast(serviceImpl.GetRefPtr()); impl->device_ = deviceObject; int32_t ret = UsbImpl::UsbdEventHandle(impl); if (ret != HDF_SUCCESS) { HDF_LOGE("%{public}s: UsbdEventHandle failed", __func__); return HDF_FAILURE; } return HDF_SUCCESS; } static void HdfUsbInterfaceDriverRelease(struct HdfDeviceObject * const deviceObject) { if (deviceObject->service == nullptr) { HDF_LOGE("HdfUsbInterfaceDriverRelease not initted"); return; } int32_t ret = UsbImpl::UsbdEventHandleRelease(); if (ret != HDF_SUCCESS) { HDF_LOGE("%{public}s:UsbdEventHandleRelease ret=%{public}d", __func__, ret); } auto *hdfUsbInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfUsbInterfaceHost, ioService); delete hdfUsbInterfaceHost; } static struct HdfDriverEntry g_usbInterfaceDriverEntry = { .moduleVersion = 1, .moduleName = "usbd", .Bind = HdfUsbInterfaceDriverBind, .Init = HdfUsbInterfaceDriverInit, .Release = HdfUsbInterfaceDriverRelease, }; #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ HDF_INIT(g_usbInterfaceDriverEntry); #ifdef __cplusplus } #endif /* __cplusplus */