1 /*
2 * Copyright (c) 2022-2023 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 #include "ddk_uevent_handle.h"
16
17 #include <linux/netlink.h>
18 #include <pthread.h>
19 #include <string.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include "ddk_pnp_listener_mgr.h"
25 #include "hdf_base.h"
26 #include "hdf_log.h"
27 #include "securec.h"
28 #include "usbfn_uevent_handle.h"
29
30 #define UEVENT_MSG_LEN 2048
31 #define UEVENT_SOCKET_GROUPS 0xffffffff
32 #define UEVENT_SOCKET_BUFF_SIZE (64 * 1024)
33 #define TIMEVAL_SECOND 0
34 #define TIMEVAL_USECOND (100 * 1000)
35
36 #define HDF_LOG_TAG usbfn_uevent
37
38 char *g_gadgetEventPath = "invalid_path";
39
40 struct UsbFnUeventInfo {
41 const char *devPath;
42 const char *subSystem;
43 const char *usbState;
44 const char *dualRoleMode;
45 };
46
UsbFnDispatchUevent(const struct UsbFnUeventInfo * info)47 static void UsbFnDispatchUevent(const struct UsbFnUeventInfo *info)
48 {
49 bool isGadgetConnect = strcmp(info->devPath, g_gadgetEventPath) == 0;
50 isGadgetConnect = (isGadgetConnect && strcmp(info->usbState, "CONNECTED") == 0);
51 if (isGadgetConnect) {
52 DdkListenerMgrNotifyAll(NULL, USB_PNP_DRIVER_GADGET_ADD);
53 return;
54 }
55
56 bool isGadgetDisconnect = strcmp(info->devPath, g_gadgetEventPath) == 0;
57 isGadgetDisconnect = (isGadgetDisconnect && strcmp(info->usbState, "DISCONNECTED") == 0);
58 if (isGadgetDisconnect) {
59 DdkListenerMgrNotifyAll(NULL, USB_PNP_DRIVER_GADGET_REMOVE);
60 return;
61 }
62
63 bool isPort2Device = strcmp(info->subSystem, "dual_role_usb") == 0;
64 isPort2Device = (isPort2Device && strcmp(info->dualRoleMode, "ufp") == 0);
65 if (isPort2Device) {
66 DdkListenerMgrNotifyAll(NULL, USB_PNP_DRIVER_PORT_DEVICE);
67 return;
68 }
69
70 bool isPort2Host = strcmp(info->subSystem, "dual_role_usb") == 0;
71 isPort2Host = (isPort2Host && strcmp(info->dualRoleMode, "dfp") == 0);
72 if (isPort2Host) {
73 DdkListenerMgrNotifyAll(NULL, USB_PNP_DRIVER_PORT_HOST);
74 return;
75 }
76 }
77
UsbFnHandleUevent(const char msg[],ssize_t rcvLen)78 void UsbFnHandleUevent(const char msg[], ssize_t rcvLen)
79 {
80 struct UsbFnUeventInfo info = {
81 .devPath = "",
82 .subSystem = "",
83 .usbState = "",
84 .dualRoleMode = "",
85 };
86
87 const char *msgTmp = msg;
88 while (*msgTmp && (msgTmp - msg < rcvLen)) {
89 if (strncmp(msgTmp, "DEVPATH=", strlen("DEVPATH=")) == 0) {
90 msgTmp += strlen("DEVPATH=");
91 info.devPath = msgTmp;
92 } else if (strncmp(msgTmp, "SUBSYSTEM=", strlen("SUBSYSTEM=")) == 0 &&
93 strlen(info.subSystem) == 0) { // some uevent has more than one SUBSYSTEM property
94 msgTmp += strlen("SUBSYSTEM=");
95 info.subSystem = msgTmp;
96 } else if (strncmp(msgTmp, "USB_STATE=", strlen("USB_STATE=")) == 0) {
97 msgTmp += strlen("USB_STATE=");
98 info.usbState = msgTmp;
99 } else if (strncmp(msgTmp, "DUAL_ROLE_MODE=", strlen("DUAL_ROLE_MODE=")) == 0) {
100 msgTmp += strlen("DUAL_ROLE_MODE=");
101 info.dualRoleMode = msgTmp;
102 }
103 msgTmp += strlen(msgTmp) + 1; // 1 is a skip character '\0'
104 }
105
106 UsbFnDispatchUevent(&info);
107 return;
108 }
109
UsbFnUeventInit(const char * gadgetEventPath)110 int32_t UsbFnUeventInit(const char *gadgetEventPath)
111 {
112 if (gadgetEventPath == NULL) {
113 HDF_LOGE("%{puiblic}s invalid param", __func__);
114 return HDF_ERR_INVALID_PARAM;
115 }
116
117 g_gadgetEventPath = (char *)gadgetEventPath;
118 return HDF_SUCCESS;
119 }