xref: /aosp_15_r20/external/ltp/testcases/kernel/input/input_helper.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) 2015 Cedric Hnyda <[email protected]>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #include <linux/input.h>
20 #include <linux/uinput.h>
21 #include <fnmatch.h>
22 #include <errno.h>
23 #include <poll.h>
24 
25 #include "test.h"
26 #include "safe_macros.h"
27 #include "input_helper.h"
28 #include "lapi/uinput.h"
29 
30 #define VIRTUAL_DEVICE "virtual-device-ltp"
31 
32 #define VIRTUAL_DEVICE_REGEX "*virtual-device-ltp*"
33 
34 static int uinput_loaded;
35 static int check_device(void);
36 
try_open_device(void)37 static int try_open_device(void)
38 {
39 	char path[256];
40 	char name[256];
41 	int ret, fd = -1;
42 	unsigned int i;
43 
44 	for (i = 0; i < 1024; i++) {
45 		snprintf(path, sizeof(path), "/dev/input/event%i", i);
46 
47 		fd = open(path, O_RDONLY);
48 
49 		if (fd < 0 && errno == ENOENT)
50 			continue;
51 
52 		if (fd < 0) {
53 			tst_resm(TINFO | TERRNO, "failed to open %s", path);
54 			break;
55 		}
56 
57 		ret = ioctl(fd, EVIOCGNAME(sizeof(name)), name);
58 		if (ret < 0) {
59 			tst_resm(TINFO | TERRNO,
60 				"ioctl(%s, EVIOCGNAME(256), ...) failed",
61 				path);
62 			break;
63 		}
64 
65 		if (strcmp(name, VIRTUAL_DEVICE) == 0)
66 			return fd;
67 		close(fd);
68 	}
69 
70 	return -1;
71 }
72 
open_device(void)73 int open_device(void)
74 {
75 	int fd;
76 	int retries = 10;
77 
78 	while (retries--) {
79 		fd = try_open_device();
80 		if (fd > 0)
81 			return fd;
82 		tst_resm(TINFO, "Device not found, retrying...");
83 		usleep(10000);
84 	}
85 
86 	tst_brkm(TBROK, NULL, "Unable to find the input device");
87 }
88 
try_load_uinput(void)89 static int try_load_uinput(void)
90 {
91 	const char *argv[] = {"modprobe", "uinput", NULL};
92 	int ret;
93 
94 	tst_resm(TINFO, "Trying to load uinput kernel module");
95 
96 	ret = tst_cmd(NULL, argv, NULL, NULL, TST_CMD_PASS_RETVAL);
97 	if (ret) {
98 		tst_resm(TINFO, "Failed to load the uinput module");
99 		return 0;
100 	}
101 
102 	return 1;
103 }
104 
unload_uinput(void)105 static void unload_uinput(void)
106 {
107 	const char *argv[] = {"modprobe", "-r", "uinput", NULL};
108 	int ret;
109 
110 	tst_resm(TINFO, "Unloading uinput kernel module");
111 
112 	ret = tst_cmd(NULL, argv, NULL, NULL, TST_CMD_PASS_RETVAL);
113 	if (ret)
114 		tst_resm(TWARN, "Failed to unload uinput module");
115 }
116 
117 static const char *uinput_paths[] = {
118 	"/dev/input/uinput",
119 	"/dev/uinput",
120 };
121 
try_open_uinput(void)122 static int try_open_uinput(void)
123 {
124 	unsigned int i;
125 	int fd;
126 
127 	for (i = 0; i < ARRAY_SIZE(uinput_paths); i++) {
128 		fd = open(uinput_paths[i], O_WRONLY | O_NONBLOCK);
129 
130 		if (fd > 0) {
131 			tst_resm(TINFO, "Found uinput dev at %s",
132 			         uinput_paths[i]);
133 			return fd;
134 		}
135 
136 		if (fd < 0 && errno != ENOENT) {
137 			tst_brkm(TBROK | TERRNO, NULL,
138 			         "open(%s)", uinput_paths[i]);
139 		}
140 	}
141 
142 	return -1;
143 }
144 
open_uinput(void)145 int open_uinput(void)
146 {
147 	int fd;
148 	int retries = 10;
149 
150 	fd = try_open_uinput();
151 	if (fd > 0)
152 		return fd;
153 
154 	if (try_load_uinput()) {
155 		while (retries--) {
156 			fd = try_open_uinput();
157 			if (fd > 0) {
158 				uinput_loaded = 1;
159 				return fd;
160 			}
161 			tst_resm(TINFO, "Uinput dev not found, retrying...");
162 			usleep(10000);
163 		}
164 
165 		unload_uinput();
166 	}
167 
168 	tst_brkm(TCONF, NULL, "Unable to find and open uinput");
169 }
170 
send_event(int fd,int event,int code,int value)171 void send_event(int fd, int event, int code, int value)
172 {
173 	struct input_event ev = {
174 		.type = event,
175 		.code = code,
176 		.value = value,
177 	};
178 
179 	SAFE_WRITE(NULL, SAFE_WRITE_ALL, fd, &ev, sizeof(ev));
180 }
181 
send_rel_move(int fd,int x,int y)182 void send_rel_move(int fd, int x, int y)
183 {
184 	send_event(fd, EV_REL, REL_X, x);
185 	send_event(fd, EV_REL, REL_Y, y);
186 	send_event(fd, EV_SYN, 0, 0);
187 }
188 
check_ui_get_sysname_ioctl(int fd)189 static void check_ui_get_sysname_ioctl(int fd)
190 {
191 	char sys_name[256];
192 	char dev_name[256];
193 	char *path;
194 
195 	SAFE_IOCTL(NULL, fd, UI_GET_SYSNAME(sizeof(sys_name)), sys_name, NULL);
196 	SAFE_ASPRINTF(NULL, &path, "/sys/devices/virtual/input/%s/name", sys_name);
197 
198 	if (FILE_SCANF(path, "%s", dev_name)) {
199 		tst_resm(TFAIL|TERRNO, "Failed to read '%s'", path);
200 		free(path);
201 		return;
202 	}
203 
204 	if (!strcmp(VIRTUAL_DEVICE, dev_name))
205 		tst_resm(TPASS, "ioctl UI_GET_SYSNAME returned correct name");
206 	else
207 		tst_resm(TFAIL, "ioctl UI_GET_SYSNAME returned wrong name");
208 
209 	free(path);
210 }
211 
create_device(int fd)212 void create_device(int fd)
213 {
214 	int nb;
215 	struct uinput_user_dev uidev = {
216 		.name = VIRTUAL_DEVICE,
217 		.id = {
218 			.bustype = BUS_USB,
219 			.vendor = 0x1,
220 			.product = 0x1,
221 			.version = 1,
222 		}
223 	};
224 
225 	SAFE_WRITE(NULL, SAFE_WRITE_ALL, fd, &uidev, sizeof(uidev));
226 	SAFE_IOCTL(NULL, fd, UI_DEV_CREATE, NULL);
227 
228 	for (nb = 100; nb > 0; nb--) {
229 		if (check_device()) {
230 			check_ui_get_sysname_ioctl(fd);
231 			return;
232 		}
233 		usleep(10000);
234 	}
235 
236 	destroy_device(fd);
237 	tst_brkm(TBROK, NULL, "Failed to create device");
238 }
239 
setup_mouse_events(int fd)240 void setup_mouse_events(int fd)
241 {
242 	SAFE_IOCTL(NULL, fd, UI_SET_EVBIT, EV_KEY);
243 	SAFE_IOCTL(NULL, fd, UI_SET_KEYBIT, BTN_LEFT);
244 	SAFE_IOCTL(NULL, fd, UI_SET_EVBIT, EV_REL);
245 	SAFE_IOCTL(NULL, fd, UI_SET_RELBIT, REL_X);
246 	SAFE_IOCTL(NULL, fd, UI_SET_RELBIT, REL_Y);
247 }
248 
destroy_device(int fd)249 void destroy_device(int fd)
250 {
251 	SAFE_IOCTL(NULL, fd, UI_DEV_DESTROY, NULL);
252 	SAFE_CLOSE(NULL, fd);
253 
254 	if (uinput_loaded)
255 		unload_uinput();
256 }
257 
check_event_code(struct input_event * iev,int event,int code)258 int check_event_code(struct input_event *iev, int event, int code)
259 {
260 	return iev->type == event && iev->code == code;
261 }
262 
check_sync_event(struct input_event * iev)263 int check_sync_event(struct input_event *iev)
264 {
265 	return check_event_code(iev, EV_SYN, SYN_REPORT);
266 }
267 
268 /*
269  * the value of stray_sync_event:
270  * 0: EV_SYN/SYN_REPORT events should not be received in /dev/input/eventX
271  * 1: EV_SYN/SYN_REPORT events may be received in /dev/input/eventX
272  * On an old kernel(before v3.7.0), EV_SYN/SYN_REPORT events are always
273  * received even though we send empty moves.
274  */
no_events_queued(int fd,int stray_sync_event)275 int no_events_queued(int fd, int stray_sync_event)
276 {
277 	struct pollfd fds = {.fd = fd, .events = POLLIN};
278 	int ret, res;
279 	struct input_event ev;
280 
281 	ret = poll(&fds, 1, 30);
282 
283 	if (ret > 0) {
284 		res = read(fd, &ev, sizeof(ev));
285 
286 		if (res == sizeof(ev)) {
287 			tst_resm(TINFO,
288 				"Unexpected ev type=%i code=%i value=%i",
289 				ev.type, ev.code, ev.value);
290 		}
291 	}
292 
293 	return ret == 0;
294 }
295 
check_device(void)296 static int check_device(void)
297 {
298 	FILE *file;
299 	char line[256];
300 
301 	file = fopen("/proc/bus/input/devices", "r");
302 	if (!file)
303 		return 0;
304 
305 	while (fgets(line, 256, file)) {
306 		if (fnmatch(VIRTUAL_DEVICE_REGEX, line, 0) == 0)
307 			return 1;
308 	}
309 
310 	fclose(file);
311 
312 	return 0;
313 }
314