xref: /aosp_15_r20/external/ltp/testcases/kernel/input/input03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2015 Cedric Hnyda <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
5*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation; either version 2 of
7*49cdfc7eSAndroid Build Coastguard Worker  * the License, or (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*49cdfc7eSAndroid Build Coastguard Worker  * GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software Foundation,
16*49cdfc7eSAndroid Build Coastguard Worker  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker  /*
20*49cdfc7eSAndroid Build Coastguard Worker   *  Create a virtual device (mouse), send events to /dev/uinput
21*49cdfc7eSAndroid Build Coastguard Worker   *  and check that the events are well received in /dev/input/mice
22*49cdfc7eSAndroid Build Coastguard Worker   */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <linux/input.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <linux/uinput.h>
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
30*49cdfc7eSAndroid Build Coastguard Worker #include "input_helper.h"
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #define NB_TEST 10
33*49cdfc7eSAndroid Build Coastguard Worker #define PS2_RIGHT_BTN 0x02
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
36*49cdfc7eSAndroid Build Coastguard Worker static void send_events(void);
37*49cdfc7eSAndroid Build Coastguard Worker static int check_events(void);
38*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker static int fd, fd2;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "input03";
43*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)44*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
47*49cdfc7eSAndroid Build Coastguard Worker 	int pid;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	setup();
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
54*49cdfc7eSAndroid Build Coastguard Worker 		pid = tst_fork();
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 		switch (pid) {
57*49cdfc7eSAndroid Build Coastguard Worker 		case 0:
58*49cdfc7eSAndroid Build Coastguard Worker 			send_events();
59*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
60*49cdfc7eSAndroid Build Coastguard Worker 		case -1:
61*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
62*49cdfc7eSAndroid Build Coastguard Worker 		default:
63*49cdfc7eSAndroid Build Coastguard Worker 			if (check_events())
64*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "Wrong data received");
65*49cdfc7eSAndroid Build Coastguard Worker 			else
66*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS,
67*49cdfc7eSAndroid Build Coastguard Worker 					"Data received in /dev/input/mice");
68*49cdfc7eSAndroid Build Coastguard Worker 		break;
69*49cdfc7eSAndroid Build Coastguard Worker 		}
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WAITPID(NULL, pid, NULL, 0);
72*49cdfc7eSAndroid Build Coastguard Worker 	}
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
75*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)78*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	fd = open_uinput();
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	setup_mouse_events(fd);
85*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_SET_EVBIT, EV_KEY);
86*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_SET_KEYBIT, BTN_RIGHT);
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	create_device(fd);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	fd2 = SAFE_OPEN(NULL, "/dev/input/mice", O_RDONLY);
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker 
send_events(void)93*49cdfc7eSAndroid Build Coastguard Worker static void send_events(void)
94*49cdfc7eSAndroid Build Coastguard Worker {
95*49cdfc7eSAndroid Build Coastguard Worker 	int nb;
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	for (nb = 0; nb < NB_TEST; ++nb) {
98*49cdfc7eSAndroid Build Coastguard Worker 		send_event(fd, EV_KEY, BTN_RIGHT, 1);
99*49cdfc7eSAndroid Build Coastguard Worker 		send_event(fd, EV_SYN, 0, 0);
100*49cdfc7eSAndroid Build Coastguard Worker 		usleep(1000);
101*49cdfc7eSAndroid Build Coastguard Worker 		send_event(fd, EV_KEY, BTN_RIGHT, 0);
102*49cdfc7eSAndroid Build Coastguard Worker 		send_event(fd, EV_SYN, 0, 0);
103*49cdfc7eSAndroid Build Coastguard Worker 		usleep(1000);
104*49cdfc7eSAndroid Build Coastguard Worker 	}
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker 
check_events(void)107*49cdfc7eSAndroid Build Coastguard Worker static int check_events(void)
108*49cdfc7eSAndroid Build Coastguard Worker {
109*49cdfc7eSAndroid Build Coastguard Worker 	int nb, rd, i, pressed = 0;
110*49cdfc7eSAndroid Build Coastguard Worker 	char buf[30];
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	nb = 0;
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	while (nb < NB_TEST) {
115*49cdfc7eSAndroid Build Coastguard Worker 		rd = read(fd2, buf, sizeof(buf));
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 		if (rd < 0)
118*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, NULL, "read() failed");
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 		if (rd % 3) {
121*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "read() returned %i", rd);
122*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
123*49cdfc7eSAndroid Build Coastguard Worker 		}
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < rd / 3; i++) {
126*49cdfc7eSAndroid Build Coastguard Worker 			if (buf[3*i] & PS2_RIGHT_BTN)
127*49cdfc7eSAndroid Build Coastguard Worker 				pressed = 1;
128*49cdfc7eSAndroid Build Coastguard Worker 
129*49cdfc7eSAndroid Build Coastguard Worker 			if (pressed == 1 && !(buf[3*i] & PS2_RIGHT_BTN)) {
130*49cdfc7eSAndroid Build Coastguard Worker 				pressed = 0;
131*49cdfc7eSAndroid Build Coastguard Worker 				nb++;
132*49cdfc7eSAndroid Build Coastguard Worker 			}
133*49cdfc7eSAndroid Build Coastguard Worker 		}
134*49cdfc7eSAndroid Build Coastguard Worker 	}
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	return nb != NB_TEST;
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)139*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
140*49cdfc7eSAndroid Build Coastguard Worker {
141*49cdfc7eSAndroid Build Coastguard Worker 	if (fd2 > 0 && close(fd2))
142*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN, "close(fd2) failed");
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	destroy_device(fd);
145*49cdfc7eSAndroid Build Coastguard Worker }
146