• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <pthread.h>
18 #include <unistd.h>
19 
20 #include "ddk_device_manager.h"
21 #include "ddk_pnp_listener_mgr.h"
22 #include "ddk_uevent_handle.h"
23 #include "devsvc_manager_clnt.h"
24 #include "hdf_base.h"
25 #include "hdf_device_desc.h"
26 #include "hdf_device_object.h"
27 #include "hdf_io_service_if.h"
28 #include "hdf_log.h"
29 #include "osal_mem.h"
30 #include "securec.h"
31 #include "usb_ddk_pnp_loader.h"
32 
33 #define HDF_LOG_TAG    usb_pnp_manager
34 #define MODULENAMESIZE 128
35 
UsbPnpManagerWriteModuleName(struct HdfSBuf * sbuf,const char * moduleName)36 bool UsbPnpManagerWriteModuleName(struct HdfSBuf *sbuf, const char *moduleName)
37 {
38     char modName[MODULENAMESIZE] = {0};
39     if (sprintf_s(modName, MODULENAMESIZE, "lib%s.z.so", moduleName) < 0) {
40         HDF_LOGE("%{public}s: sprintf_s modName failed", __func__);
41         return false;
42     }
43 
44     return HdfSbufWriteString(sbuf, modName);
45 }
46 
UsbPnpManagerDispatch(struct HdfDeviceIoClient * client,int32_t cmd,struct HdfSBuf * data,struct HdfSBuf * reply)47 static int32_t UsbPnpManagerDispatch(
48     struct HdfDeviceIoClient *client, int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
49 {
50     (void)client;
51     (void)cmd;
52     (void)data;
53     (void)reply;
54 
55     HDF_LOGI("received cmd = %d", cmd);
56     return HDF_SUCCESS;
57 }
58 
UsbPnpManagerBind(struct HdfDeviceObject * device)59 static int32_t UsbPnpManagerBind(struct HdfDeviceObject *device)
60 {
61     static struct IDeviceIoService pnpLoaderService = {
62         .Dispatch = UsbPnpManagerDispatch,
63     };
64 
65     if (device == NULL) {
66         return HDF_ERR_INVALID_OBJECT;
67     }
68 
69     device->service = &pnpLoaderService;
70     HDF_LOGI("usb pnp manager bind success\n");
71 
72     return HDF_SUCCESS;
73 }
74 
75 #ifdef USB_EVENT_NOTIFY_LINUX_NATIVE_MODE
UsbPnpManagerStartUeventThread(void)76 int32_t UsbPnpManagerStartUeventThread(void)
77 {
78     pthread_t tid;
79     int32_t ret = pthread_create(&tid, NULL, DdkUeventMain, NULL);
80     if (ret != 0) {
81         HDF_LOGE("%{public}s: create thread failed:%{public}d", __func__, ret);
82         return ret;
83     }
84 
85     ret = pthread_setname_np(tid, "usbpnpUeventThd");
86     if (ret != 0) {
87         HDF_LOGE("%{public}s: set thread name failed:%{public}d", __func__, ret);
88     }
89     return ret;
90 }
91 #endif
92 
UsbPnpManagerInit(struct HdfDeviceObject * device)93 static int32_t UsbPnpManagerInit(struct HdfDeviceObject *device)
94 {
95     static struct HdfDevEventlistener usbPnpListener = {
96         .callBack = UsbDdkPnpLoaderEventReceived,
97     };
98     usbPnpListener.priv = (void *)(device);
99 
100     int32_t ret = DdkDevMgrInit();
101     if (ret != HDF_SUCCESS) {
102         HDF_LOGE("%{public}s: DdkDevMgrInit error", __func__);
103         return HDF_FAILURE;
104     }
105 
106     ret = DdkListenerMgrInit();
107     if (ret != HDF_SUCCESS) {
108         HDF_LOGE("%{public}s: DdkListenerMgrInit error", __func__);
109         return HDF_FAILURE;
110     }
111 
112     ret = DdkUeventInit();
113     if (ret != HDF_SUCCESS) {
114         HDF_LOGE("%{public}s: DdkUeventInit error", __func__);
115         return ret;
116     }
117 #ifdef USB_EVENT_NOTIFY_LINUX_NATIVE_MODE
118     if (UsbPnpManagerStartUeventThread() != HDF_SUCCESS) {
119         HDF_LOGE("%{public}s: start uevent thread failed", __func__);
120         return HDF_FAILURE;
121     }
122 #endif
123     ret = UsbDdkPnpLoaderEventHandle();
124     if (ret != HDF_SUCCESS) {
125         HDF_LOGE("%{public}s: UsbDdkPnpLoaderEventHandle failed", __func__);
126         return ret;
127     }
128     if (DdkListenerMgrAdd(&usbPnpListener) != HDF_SUCCESS) {
129         HDF_LOGE("%{public}s: add listener failed", __func__);
130         return HDF_FAILURE;
131     }
132     HDF_LOGI("UsbPnpManagerInit done");
133     return HDF_SUCCESS;
134 }
135 
UsbPnpManagerRelease(struct HdfDeviceObject * device)136 static void UsbPnpManagerRelease(struct HdfDeviceObject *device)
137 {
138     (void)device;
139     return;
140 }
141 
142 struct HdfDriverEntry g_usbPnpManagerEntry = {
143     .moduleVersion = 1,
144     .Bind = UsbPnpManagerBind,
145     .Init = UsbPnpManagerInit,
146     .Release = UsbPnpManagerRelease,
147     .moduleName = "HDF_USB_PNP_MANAGER",
148 };
149 
150 HDF_INIT(g_usbPnpManagerEntry);
151