blob: 97a856a90055d56d8fac96ecd5107ed10f58374b [file] [log] [blame]
Etienne Gemsa4f750b92015-02-20 17:14:10 +01001/*
2 * Copyright (c) 2015 Etienne Gemsa <etienne.gemsa@lse.epita.fr>
3 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "defs.h"
30
31#ifdef HAVE_LINUX_INPUT_H
Etienne Gemsa4f750b92015-02-20 17:14:10 +010032
Dmitry V. Levin8d85c232016-05-27 00:39:33 +000033# include <linux/ioctl.h>
34# include <linux/input.h>
35# include "xlat/evdev_abs.h"
36# include "xlat/evdev_autorepeat.h"
37# include "xlat/evdev_ff_status.h"
38# include "xlat/evdev_ff_types.h"
39# include "xlat/evdev_keycode.h"
40# include "xlat/evdev_leds.h"
41# include "xlat/evdev_misc.h"
42# include "xlat/evdev_mtslots.h"
43# include "xlat/evdev_prop.h"
44# include "xlat/evdev_relative_axes.h"
45# include "xlat/evdev_snd.h"
46# include "xlat/evdev_switch.h"
47# include "xlat/evdev_sync.h"
48
49# ifndef SYN_MAX
50# define SYN_MAX 0xf
51# endif
Dmitry V. Levin19dadf92015-02-22 02:50:33 +030052
Etienne Gemsa4f750b92015-02-20 17:14:10 +010053static void
54decode_envelope(struct ff_envelope *envelope)
55{
56 tprintf(", envelope={attack_length=%" PRIu16 ", attack_level=%" PRIu16
57 ", fade_length=%" PRIu16 ", fade_level=%" PRIx32 "}",
58 envelope->attack_length,
59 envelope->attack_level,
60 envelope->fade_length,
61 envelope->fade_level);
62}
63
64static int
65ff_effect_ioctl(struct tcb *tcp, long arg)
66{
67 struct ff_effect ffe;
68
69 if (!verbose(tcp) || umove(tcp, arg, &ffe) < 0)
70 return 0;
71
72 tprints(", {type=");
73 printxval(evdev_ff_types, ffe.type, "FF_???");
74 tprintf(", id=%" PRIu16 ", direction=%" PRIu16,
75 ffe.id, ffe.direction);
76
77 if (!abbrev(tcp)) {
78 tprintf(", trigger={button=%" PRIu16 ", interval=%" PRIu16 "}",
79 ffe.trigger.button, ffe.trigger.interval);
80 tprintf(", replay={lenght=%" PRIu16 ", delay=%" PRIu16 "}",
81 ffe.replay.length, ffe.replay.delay);
82 switch (ffe.type) {
83 case FF_CONSTANT:
84 tprintf(", constant_ef={%" PRIi16,
85 ffe.u.constant.level);
86 decode_envelope(&ffe.u.constant.envelope);
87 tprints("}");
88 return 1;
89 case FF_RAMP:
90 tprintf(", ramp={start_level=%" PRIi16
91 ", end_level=%" PRIi16,
92 ffe.u.ramp.start_level,
93 ffe.u.ramp.end_level);
94 decode_envelope(&ffe.u.ramp.envelope);
95 tprints("}");
96 return 1;
97 case FF_PERIODIC:
98 tprintf(", periodic_ef={waveform=%" PRIu16
99 ", period=%" PRIu16
100 ", magnitude=%" PRIi16
101 ", offset=%" PRIi16
102 ", phase=%" PRIu16,
103 ffe.u.periodic.waveform,
104 ffe.u.periodic.period,
105 ffe.u.periodic.magnitude,
106 ffe.u.periodic.offset,
107 ffe.u.periodic.phase);
108 decode_envelope(&ffe.u.periodic.envelope);
109 tprintf(", custom_len=%" PRIu32
110 ", *custom_data=%#lx}",
111 ffe.u.periodic.custom_len,
112 (unsigned long)ffe.u.periodic.custom_data);
113 return 1;
114 case FF_RUMBLE:
115 tprintf(", rumble={strong_magnitude=%" PRIu16
116 ", weak_magnitude=%" PRIu16 "}",
117 ffe.u.rumble.strong_magnitude,
118 ffe.u.rumble.weak_magnitude);
119 return 1;
120 case FF_SPRING:
121 case FF_FRICTION:
122 case FF_DAMPER:
123 case FF_INERTIA:
124 case FF_CUSTOM:
125 break;
126 default :
127 break;
128 }
129 }
130
131 tprints(", ...}");
132 return 1;
133}
134
135static int
136abs_ioctl(struct tcb *tcp, long arg)
137{
138 struct input_absinfo absinfo;
139
140 if (!verbose(tcp) || umove(tcp, arg, &absinfo) < 0)
141 return 0;
142
143 tprintf(", {value=%" PRIu32 ", minimum=%" PRIu32,
144 absinfo.value, absinfo.minimum);
145 if (!abbrev(tcp)) {
146 tprintf(", maximum=%" PRIu32 ", fuzz=%" PRIu32,
147 absinfo.maximum, absinfo.fuzz);
Dmitry V. Levin19dadf92015-02-22 02:50:33 +0300148 tprintf(", flat=%" PRIu32, absinfo.flat);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000149# ifdef HAVE_STRUCT_INPUT_ABSINFO_RESOLUTION
Dmitry V. Levin19dadf92015-02-22 02:50:33 +0300150 tprintf(", resolution=%" PRIu32, absinfo.resolution);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000151# endif
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100152 tprints("}");
153 } else {
154 tprints(", ...}");
155 }
156 return 1;
157}
158
159static int
160keycode_ioctl(struct tcb *tcp, long arg)
161{
162 unsigned int keycode[2];
163
164 if (!arg) {
165 tprints(", NULL");
166 return 1;
167 }
168
169 if (!verbose(tcp) || umove(tcp, arg, &keycode) < 0)
170 return 0;
171
172 tprintf(", [%u, ", keycode[0]);
173 printxval(evdev_keycode, keycode[1], "KEY_???");
174 tprints("]");
175 return 1;
176}
177
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000178# ifdef EVIOCGKEYCODE_V2
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100179static int
180keycode_V2_ioctl(struct tcb *tcp, long arg)
181{
182 struct input_keymap_entry ike;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100183
184 if (!arg) {
185 tprints(", NULL");
186 return 1;
187 }
188
189 if (!verbose(tcp) || umove(tcp, arg, &ike) < 0)
190 return 0;
191
Dmitry V. Levin7d8b41a2015-02-21 23:05:26 +0000192 tprintf(", {flags=%" PRIu8 ", len=%" PRIu8, ike.flags, ike.len);
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100193 if (!abbrev(tcp)) {
Dmitry V. Levin7d8b41a2015-02-21 23:05:26 +0000194 unsigned int i;
195
196 tprintf(", index=%" PRIu16 ", keycode=", ike.index);
197 printxval(evdev_keycode, ike.keycode, "KEY_???");
198 tprints(", scancode=[");
199 for (i = 0; i < ARRAY_SIZE(ike.scancode); i++) {
200 if (i > 0)
201 tprints(", ");
202 tprintf("%" PRIx8, ike.scancode[i]);
203 }
204 tprints("]}");
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100205 } else {
206 tprints(", ...}");
207 }
208 return 1;
209}
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000210# endif /* EVIOCGKEYCODE_V2 */
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100211
212static int
213getid_ioctl(struct tcb *tcp, long arg)
214{
215 struct input_id id;
216
217 if (!verbose(tcp) || umove(tcp, arg, &id) < 0)
218 return 0;
219
220 tprintf(", {ID_BUS=%" PRIu16 ", ID_VENDOR=%" PRIu16,
221 id.bustype, id.vendor);
222 if (!abbrev(tcp)) {
223 tprintf(", ID_PRODUCT=%" PRIu16 ", ID_VERSION=%" PRIu16 "}",
224 id.product, id.version);
225 } else {
226 tprints(", ...}");
227 }
228 return 1;
229}
230
231static int
232decode_bitset(struct tcb *tcp, long arg, const struct xlat decode_nr[],
233 const unsigned int max_nr, const char *dflt)
234{
235 if (!verbose(tcp))
236 return 0;
237
238 unsigned int size;
239 if ((unsigned long) tcp->u_rval > max_nr)
240 size = max_nr;
241 else
242 size = tcp->u_rval;
243 char decoded_arg[size];
244
245 if (umoven(tcp, arg, size, decoded_arg) < 0)
246 return 0;
247
248 tprints(", [");
249
250 int bit_displayed = 0;
251 int i = next_set_bit(decoded_arg, 0, size);
252 if (i < 0) {
253 tprints(" 0 ");
254 } else {
255 printxval(decode_nr, i, dflt);
256
257 while ((i = next_set_bit(decoded_arg, i + 1, size)) > 0) {
258 if (abbrev(tcp) && bit_displayed >= 3) {
259 tprints(", ...");
260 break;
261 }
262 tprints(", ");
263 printxval(decode_nr, i, dflt);
264 bit_displayed++;
265 }
266 }
267
268 tprints("]");
269
270 return 1;
271}
272
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000273# ifdef EVIOCGMTSLOTS
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100274static int
275mtslots_ioctl(struct tcb *tcp, const unsigned int code, long arg)
276{
277 const size_t size = _IOC_SIZE(code) / sizeof(int32_t);
278 if (!size)
279 return 0;
280
281 int32_t buffer[size];
282
283 if (!verbose(tcp) || umove(tcp, arg, &buffer) < 0)
284 return 0;
285
286 tprints(", {code=");
287 printxval(evdev_mtslots, buffer[0], "ABS_MT_???");
288
289 unsigned int i;
290 tprints(", values=[");
291
292 for (i = 1; i < ARRAY_SIZE(buffer); i++)
293 tprintf("%s%d", i > 1 ? ", " : "", buffer[i]);
294
295 tprints("]}");
296 return 1;
297}
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000298# endif /* EVIOCGMTSLOTS */
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100299
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000300# if defined EVIOCGREP || defined EVIOCSREP
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100301static int
302repeat_ioctl(struct tcb *tcp, long arg)
303{
Dmitry V. Levinb6795082015-07-06 22:33:39 +0000304 tprints(", ");
305 printpair_int(tcp, arg, "%u");
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100306 return 1;
307}
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000308# endif /* EVIOCGREP || EVIOCSREP */
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100309
310static int
Dmitry V. Levin53767d82016-05-27 00:40:18 +0000311bit_ioctl(struct tcb *tcp, const unsigned int ev_nr, const long arg)
312{
313 switch (ev_nr) {
314 case EV_SYN:
315 return decode_bitset(tcp, arg, evdev_sync,
316 SYN_MAX, "SYN_???");
317 case EV_KEY:
318 return decode_bitset(tcp, arg, evdev_keycode,
319 KEY_MAX, "KEY_???");
320 case EV_REL:
321 return decode_bitset(tcp, arg, evdev_relative_axes,
322 REL_MAX, "REL_???");
323 case EV_ABS:
324 return decode_bitset(tcp, arg,
325 evdev_abs, ABS_MAX, "ABS_???");
326 case EV_MSC:
327 return decode_bitset(tcp, arg,
328 evdev_misc, MSC_MAX, "MSC_???");
329# ifdef EV_SW
330 case EV_SW:
331 return decode_bitset(tcp, arg,
332 evdev_switch, SW_MAX, "SW_???");
333# endif
334 case EV_LED:
335 return decode_bitset(tcp, arg,
336 evdev_leds, LED_MAX, "LED_???");
337 case EV_SND:
338 return decode_bitset(tcp, arg,
339 evdev_snd, SND_MAX, "SND_???");
340 case EV_REP:
341 return decode_bitset(tcp, arg, evdev_autorepeat,
342 REP_MAX, "REP_???");
343 case EV_FF:
344 return decode_bitset(tcp, arg, evdev_ff_types,
345 FF_MAX, "FF_???");
346 case EV_PWR:
347 printnum_int(tcp, arg, "%d");
348 return 1;
349 case EV_FF_STATUS:
350 return decode_bitset(tcp, arg, evdev_ff_status,
351 FF_STATUS_MAX, "FF_STATUS_???");
352 default:
353 return 0;
354 }
355}
356
357static int
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000358evdev_read_ioctl(struct tcb *tcp, const unsigned int code, const long arg)
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100359{
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100360 if (syserror(tcp))
361 return 0;
362
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000363 /* fixed-number fixed-length commands */
364 switch (code) {
365 case EVIOCGVERSION:
366 tprints(", ");
367 printnum_int(tcp, arg, "%" PRIx32);
368 return 1;
369 case EVIOCGEFFECTS:
370 tprints(", ");
371 printnum_int(tcp, arg, "%" PRIu32);
372 return 1;
373 case EVIOCGID:
374 return getid_ioctl(tcp, arg);
375# ifdef EVIOCGREP
376 case EVIOCGREP:
377 return repeat_ioctl(tcp, arg);;
378# endif
379 case EVIOCGKEYCODE:
380 return keycode_ioctl(tcp, arg);
381# ifdef EVIOCGKEYCODE_V2
382 case EVIOCGKEYCODE_V2:
383 return keycode_V2_ioctl(tcp, arg);
384# endif
385 }
386
387 /* fixed-number variable-length commands */
388 switch (_IOC_NR(code)) {
389# ifdef EVIOCGMTSLOTS
390 case _IOC_NR(EVIOCGMTSLOTS(0)):
391 return mtslots_ioctl(tcp, code, arg);
392# endif
393 case _IOC_NR(EVIOCGNAME(0)):
394 case _IOC_NR(EVIOCGPHYS(0)):
395 case _IOC_NR(EVIOCGUNIQ(0)):
396 tprints(", ");
397 printstr(tcp, arg, tcp->u_rval - 1);
398 return 1;
399# ifdef EVIOCGPROP
400 case _IOC_NR(EVIOCGPROP(0)):
401 return decode_bitset(tcp, arg,
402 evdev_prop, INPUT_PROP_MAX, "PROP_???");
403# endif
404 case _IOC_NR(EVIOCGSND(0)):
405 return decode_bitset(tcp, arg,
406 evdev_snd, SND_MAX, "SND_???");
407# ifdef EVIOCGSW
408 case _IOC_NR(EVIOCGSW(0)):
409 return decode_bitset(tcp, arg,
410 evdev_switch, SW_MAX, "SW_???");
411# endif
412 case _IOC_NR(EVIOCGKEY(0)):
413 return decode_bitset(tcp, arg,
414 evdev_keycode, KEY_MAX, "KEY_???");
415 case _IOC_NR(EVIOCGLED(0)):
416 return decode_bitset(tcp, arg,
417 evdev_leds, LED_MAX, "LED_???");
418 }
419
420 /* multi-number fixed-length commands */
421 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0)))
422 return abs_ioctl(tcp, arg);
423
424 /* multi-number variable-length commands */
Dmitry V. Levin53767d82016-05-27 00:40:18 +0000425 if ((_IOC_NR(code) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
426 return bit_ioctl(tcp, _IOC_NR(code) & EV_MAX, arg);
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100427
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000428 return 0;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100429}
430
431static int
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000432evdev_write_ioctl(struct tcb *tcp, const unsigned int code, const long arg)
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100433{
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000434 /* fixed-number fixed-length commands */
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100435 switch (code) {
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000436# ifdef EVIOCSREP
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100437 case EVIOCSREP:
438 return repeat_ioctl(tcp, arg);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000439# endif
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100440 case EVIOCSKEYCODE:
441 return keycode_ioctl(tcp, arg);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000442# ifdef EVIOCSKEYCODE_V2
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100443 case EVIOCSKEYCODE_V2:
444 return keycode_V2_ioctl(tcp, arg);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000445# endif
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100446 case EVIOCSFF:
447 return ff_effect_ioctl(tcp, arg);
448 case EVIOCRMFF:
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000449# ifdef EVIOCSCLOCKID
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100450 case EVIOCSCLOCKID:
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000451# endif
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100452 case EVIOCGRAB:
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000453# ifdef EVIOCREVOKE
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100454 case EVIOCREVOKE:
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000455# endif
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100456 tprints(", ");
457 printnum_int(tcp, arg, "%u");
458 return 1;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100459 }
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000460
461 /* multi-number fixed-length commands */
462 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0)))
463 return abs_ioctl(tcp, arg);
464
465 return 0;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100466}
467
468int
469evdev_ioctl(struct tcb *tcp, const unsigned int code, long arg)
470{
471 switch(_IOC_DIR(code)) {
472 case _IOC_READ:
Dmitry V. Levin2114e082016-05-27 00:40:02 +0000473 if (entering(tcp))
474 return 0;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100475 return evdev_read_ioctl(tcp, code, arg);
476 case _IOC_WRITE:
Dmitry V. Levin2114e082016-05-27 00:40:02 +0000477 return evdev_write_ioctl(tcp, code, arg) | RVAL_DECODED;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100478 default:
Dmitry V. Levin2114e082016-05-27 00:40:02 +0000479 return RVAL_DECODED;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100480 }
481}
482
483#endif /* HAVE_LINUX_INPUT_H */