1 /*
2 * Copyright (c) 2021 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_pnp_manager.h"
17 #include "devsvc_manager_clnt.h"
18 #include "hdf_base.h"
19 #include "hdf_device_desc.h"
20 #include "hdf_io_service_if.h"
21 #include "hdf_log.h"
22 #include "osal_mem.h"
23 #include "securec.h"
24 #include "usb_ddk_pnp_loader.h"
25
26 #define HDF_LOG_TAG usb_pnp_manager
27
UsbPnpManagerWriteModuleName(struct HdfSBuf * sbuf,const char * moduleName)28 bool UsbPnpManagerWriteModuleName(struct HdfSBuf *sbuf, const char *moduleName)
29 {
30 char modName[128] = {0};
31
32 if (sprintf_s(modName, sizeof(modName) - 1, "lib%s.z.so", moduleName) < 0) {
33 HDF_LOGE("%s: sprintf_s modName fail", __func__);
34 return false;
35 }
36
37 return HdfSbufWriteString(sbuf, modName);
38 }
39
UsbPnpManagerDispatch(struct HdfDeviceIoClient * client,int32_t cmd,struct HdfSBuf * data,struct HdfSBuf * reply)40 static int32_t UsbPnpManagerDispatch(struct HdfDeviceIoClient *client, int32_t cmd,
41 struct HdfSBuf *data, struct HdfSBuf *reply)
42 {
43 (void)client;
44 (void)cmd;
45 (void)data;
46 (void)reply;
47
48 HDF_LOGI("received cmd = %d", cmd);
49 return HDF_SUCCESS;
50 }
51
UsbPnpManagerBind(struct HdfDeviceObject * device)52 static int32_t UsbPnpManagerBind(struct HdfDeviceObject *device)
53 {
54 static struct IDeviceIoService pnpLoaderService = {
55 .Dispatch = UsbPnpManagerDispatch,
56 };
57
58 if (device == NULL) {
59 return HDF_ERR_INVALID_OBJECT;
60 }
61
62 device->service = &pnpLoaderService;
63 HDF_LOGI("usb pnp manager bind success\n");
64
65 return HDF_SUCCESS;
66 }
67
UsbPnpManagerInit(struct HdfDeviceObject * device)68 static int32_t UsbPnpManagerInit(struct HdfDeviceObject *device)
69 {
70 int32_t status;
71 struct HdfIoService *usbPnpServ = HdfIoServiceBind(USB_PNP_NOTIFY_SERVICE_NAME);
72 static struct HdfDevEventlistener usbPnpListener = {
73 .callBack = UsbDdkPnpLoaderEventReceived,
74 };
75 usbPnpListener.priv = (void *)(device);
76
77 if (usbPnpServ == NULL) {
78 HDF_LOGE("HdfIoServiceBind failed");
79 return HDF_ERR_INVALID_OBJECT;
80 }
81
82 status = HdfDeviceRegisterEventListener(usbPnpServ, &usbPnpListener);
83 if (status != HDF_SUCCESS) {
84 HDF_LOGE("HdfDeviceRegisterEventListener faile status=%d", status);
85 return status;
86 }
87
88 HDF_LOGE("UsbPnpManagerInit done");
89 return UsbDdkPnpLoaderEventHandle();
90 }
91
UsbPnpManagerRelease(struct HdfDeviceObject * device)92 static void UsbPnpManagerRelease(struct HdfDeviceObject *device)
93 {
94 (void)device;
95 return;
96 }
97
98 struct HdfDriverEntry g_usbPnpManagerEntry = {
99 .moduleVersion = 1,
100 .Bind = UsbPnpManagerBind,
101 .Init = UsbPnpManagerInit,
102 .Release = UsbPnpManagerRelease,
103 .moduleName = "HDF_USB_PNP_MANAGER",
104 };
105
106 HDF_INIT(g_usbPnpManagerEntry);
107
108