xref: /aosp_15_r20/external/ltp/testcases/kernel/input/input01.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/eventX
22*49cdfc7eSAndroid Build Coastguard Worker   */
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <linux/input.h>
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include "input_helper.h"
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 
31*49cdfc7eSAndroid Build Coastguard Worker #define NB_TEST 20
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
34*49cdfc7eSAndroid Build Coastguard Worker static void send_events(void);
35*49cdfc7eSAndroid Build Coastguard Worker static int verify_data(struct input_event *iev, int nb);
36*49cdfc7eSAndroid Build Coastguard Worker static int check_events(void);
37*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static int fd;
40*49cdfc7eSAndroid Build Coastguard Worker static int fd2;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "input01";
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 read from eventX");
65*49cdfc7eSAndroid Build Coastguard Worker 			else
66*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS, "Data received from eventX");
67*49cdfc7eSAndroid Build Coastguard Worker 		break;
68*49cdfc7eSAndroid Build Coastguard Worker 		}
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WAITPID(NULL, pid, NULL, 0);
71*49cdfc7eSAndroid Build Coastguard Worker 	}
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
74*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)77*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	fd = open_uinput();
82*49cdfc7eSAndroid Build Coastguard Worker 	setup_mouse_events(fd);
83*49cdfc7eSAndroid Build Coastguard Worker 	create_device(fd);
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	fd2 = open_device();
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker 
send_events(void)88*49cdfc7eSAndroid Build Coastguard Worker static void send_events(void)
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker 	int nb;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	for (nb = 0; nb < NB_TEST; ++nb) {
93*49cdfc7eSAndroid Build Coastguard Worker 		send_rel_move(fd, 10, 1);
94*49cdfc7eSAndroid Build Coastguard Worker 		usleep(1000);
95*49cdfc7eSAndroid Build Coastguard Worker 	}
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker 
check_events(void)98*49cdfc7eSAndroid Build Coastguard Worker static int check_events(void)
99*49cdfc7eSAndroid Build Coastguard Worker {
100*49cdfc7eSAndroid Build Coastguard Worker 	int nb, rd;
101*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
102*49cdfc7eSAndroid Build Coastguard Worker 	struct input_event iev[64];
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	nb = 0;
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	while (nb < NB_TEST * 3) {
107*49cdfc7eSAndroid Build Coastguard Worker 		rd = read(fd2, iev, sizeof(iev));
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 		if (rd < 0)
110*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "read()");
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 		if (rd == 0 || rd % sizeof(struct input_event)) {
113*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "read() returned unexpected %i", rd);
114*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
115*49cdfc7eSAndroid Build Coastguard Worker 		}
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < rd / sizeof(struct input_event); i++) {
118*49cdfc7eSAndroid Build Coastguard Worker 			if (verify_data(&iev[i], nb++))
119*49cdfc7eSAndroid Build Coastguard Worker 				return 1;
120*49cdfc7eSAndroid Build Coastguard Worker 		}
121*49cdfc7eSAndroid Build Coastguard Worker 	}
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker 
verify_data(struct input_event * iev,int nb)126*49cdfc7eSAndroid Build Coastguard Worker static int verify_data(struct input_event *iev, int nb)
127*49cdfc7eSAndroid Build Coastguard Worker {
128*49cdfc7eSAndroid Build Coastguard Worker 	if (nb % 3 == 0) {
129*49cdfc7eSAndroid Build Coastguard Worker 		if (iev->type != EV_REL) {
130*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO,
131*49cdfc7eSAndroid Build Coastguard Worker 			         "%i: Unexpected event type %i expected %i",
132*49cdfc7eSAndroid Build Coastguard Worker 			         nb, iev->type, EV_REL);
133*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
134*49cdfc7eSAndroid Build Coastguard Worker 		}
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 		if (iev->code != REL_X)
137*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker 		if (iev->value != 10)
140*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
143*49cdfc7eSAndroid Build Coastguard Worker 	}
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker 	if (nb % 3 == 1) {
146*49cdfc7eSAndroid Build Coastguard Worker 		if (iev->type != EV_REL) {
147*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO,
148*49cdfc7eSAndroid Build Coastguard Worker 			         "%i: Unexpected event type %i expected %i",
149*49cdfc7eSAndroid Build Coastguard Worker 			         nb, iev->type, EV_REL);
150*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
151*49cdfc7eSAndroid Build Coastguard Worker 		}
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker 		if (iev->code != REL_Y)
154*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
155*49cdfc7eSAndroid Build Coastguard Worker 
156*49cdfc7eSAndroid Build Coastguard Worker 		if (iev->value != 1)
157*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
160*49cdfc7eSAndroid Build Coastguard Worker 	}
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	if (nb % 3 == 2) {
163*49cdfc7eSAndroid Build Coastguard Worker 		if (iev->type != EV_SYN) {
164*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO,
165*49cdfc7eSAndroid Build Coastguard Worker 			         "%i: Unexpected event type %i expected %i",
166*49cdfc7eSAndroid Build Coastguard Worker 			         nb, iev->type, EV_SYN);
167*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
168*49cdfc7eSAndroid Build Coastguard Worker 		}
169*49cdfc7eSAndroid Build Coastguard Worker 
170*49cdfc7eSAndroid Build Coastguard Worker 		if (iev->code != 0)
171*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 		if (iev->value != 0)
174*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
177*49cdfc7eSAndroid Build Coastguard Worker 	}
178*49cdfc7eSAndroid Build Coastguard Worker 	return 1;
179*49cdfc7eSAndroid Build Coastguard Worker }
180*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)181*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
182*49cdfc7eSAndroid Build Coastguard Worker {
183*49cdfc7eSAndroid Build Coastguard Worker 	if (fd2 > 0 && close(fd2))
184*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "close(fd2)");
185*49cdfc7eSAndroid Build Coastguard Worker 
186*49cdfc7eSAndroid Build Coastguard Worker 	destroy_device(fd);
187*49cdfc7eSAndroid Build Coastguard Worker }
188