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