blob: a138edab502f725c31b89032e7c131afda61f032 [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
Dmitry V. Levinc758ed02016-05-27 00:40:53 +000054decode_envelope(const struct ff_envelope *envelope)
Etienne Gemsa4f750b92015-02-20 17:14:10 +010055{
Dmitry V. Levinc758ed02016-05-27 00:40:53 +000056 tprintf(", envelope={attack_length=%" PRIu16
57 ", attack_level=%" PRIu16
58 ", fade_length=%" PRIu16
59 ", fade_level=%#x}",
Etienne Gemsa4f750b92015-02-20 17:14:10 +010060 envelope->attack_length,
61 envelope->attack_level,
62 envelope->fade_length,
63 envelope->fade_level);
64}
65
66static int
67ff_effect_ioctl(struct tcb *tcp, long arg)
68{
Dmitry V. Levin3a45eb32016-05-27 00:41:02 +000069 tprints(", ");
70
Etienne Gemsa4f750b92015-02-20 17:14:10 +010071 struct ff_effect ffe;
72
Dmitry V. Levin3a45eb32016-05-27 00:41:02 +000073 if (umove_or_printaddr(tcp, arg, &ffe))
74 return 1;
Etienne Gemsa4f750b92015-02-20 17:14:10 +010075
Dmitry V. Levin3a45eb32016-05-27 00:41:02 +000076 tprints("{type=");
Etienne Gemsa4f750b92015-02-20 17:14:10 +010077 printxval(evdev_ff_types, ffe.type, "FF_???");
Dmitry V. Levin3a45eb32016-05-27 00:41:02 +000078 tprintf(", id=%" PRIu16
79 ", direction=%" PRIu16 ", ",
80 ffe.id,
81 ffe.direction);
Etienne Gemsa4f750b92015-02-20 17:14:10 +010082
Dmitry V. Levin3a45eb32016-05-27 00:41:02 +000083 if (abbrev(tcp)) {
84 tprints("...}");
85 return 1;
Etienne Gemsa4f750b92015-02-20 17:14:10 +010086 }
87
Dmitry V. Levin3a45eb32016-05-27 00:41:02 +000088 tprintf("trigger={button=%" PRIu16
89 ", interval=%" PRIu16 "}"
90 ", replay={length=%" PRIu16
91 ", delay=%" PRIu16 "}",
92 ffe.trigger.button,
93 ffe.trigger.interval,
94 ffe.replay.length,
95 ffe.replay.delay);
96
97 switch (ffe.type) {
98 case FF_CONSTANT:
99 tprintf(", constant={level=%" PRId16,
100 ffe.u.constant.level);
101 decode_envelope(&ffe.u.constant.envelope);
102 tprints("}");
103 break;
104 case FF_RAMP:
105 tprintf(", ramp={start_level=%" PRId16
106 ", end_level=%" PRId16,
107 ffe.u.ramp.start_level,
108 ffe.u.ramp.end_level);
109 decode_envelope(&ffe.u.ramp.envelope);
110 tprints("}");
111 break;
112 case FF_PERIODIC:
113 tprintf(", periodic={waveform=%" PRIu16
114 ", period=%" PRIu16
115 ", magnitude=%" PRId16
116 ", offset=%" PRId16
117 ", phase=%" PRIu16,
118 ffe.u.periodic.waveform,
119 ffe.u.periodic.period,
120 ffe.u.periodic.magnitude,
121 ffe.u.periodic.offset,
122 ffe.u.periodic.phase);
123 decode_envelope(&ffe.u.periodic.envelope);
124 tprintf(", custom_len=%u"
125 ", custom_data=%#lx}",
126 ffe.u.periodic.custom_len,
127 (unsigned long) ffe.u.periodic.custom_data);
128 break;
129 case FF_RUMBLE:
130 tprintf(", rumble={strong_magnitude=%" PRIu16
131 ", weak_magnitude=%" PRIu16 "}",
132 ffe.u.rumble.strong_magnitude,
133 ffe.u.rumble.weak_magnitude);
134 break;
135 default:
136 break;
137 }
138
139 tprints("}");
140
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100141 return 1;
142}
143
144static int
145abs_ioctl(struct tcb *tcp, long arg)
146{
Dmitry V. Levine1670062016-05-27 00:41:14 +0000147 tprints(", ");
148
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100149 struct input_absinfo absinfo;
150
Dmitry V. Levine1670062016-05-27 00:41:14 +0000151 if (!umove_or_printaddr(tcp, arg, &absinfo)) {
152 tprintf("{value=%u"
153 ", minimum=%u, ",
154 absinfo.value,
155 absinfo.minimum);
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100156
Dmitry V. Levine1670062016-05-27 00:41:14 +0000157 if (!abbrev(tcp)) {
158 tprintf("maximum=%u"
159 ", fuzz=%u"
160 ", flat=%u",
161 absinfo.maximum,
162 absinfo.fuzz,
163 absinfo.flat);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000164# ifdef HAVE_STRUCT_INPUT_ABSINFO_RESOLUTION
Dmitry V. Levine1670062016-05-27 00:41:14 +0000165 tprintf(", resolution=%u",
166 absinfo.resolution);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000167# endif
Dmitry V. Levine1670062016-05-27 00:41:14 +0000168 } else {
169 tprints("...");
170 }
171
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100172 tprints("}");
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100173 }
Dmitry V. Levine1670062016-05-27 00:41:14 +0000174
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100175 return 1;
176}
177
178static int
179keycode_ioctl(struct tcb *tcp, long arg)
180{
181 unsigned int keycode[2];
182
183 if (!arg) {
184 tprints(", NULL");
185 return 1;
186 }
187
188 if (!verbose(tcp) || umove(tcp, arg, &keycode) < 0)
189 return 0;
190
191 tprintf(", [%u, ", keycode[0]);
192 printxval(evdev_keycode, keycode[1], "KEY_???");
193 tprints("]");
194 return 1;
195}
196
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000197# ifdef EVIOCGKEYCODE_V2
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100198static int
199keycode_V2_ioctl(struct tcb *tcp, long arg)
200{
201 struct input_keymap_entry ike;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100202
203 if (!arg) {
204 tprints(", NULL");
205 return 1;
206 }
207
208 if (!verbose(tcp) || umove(tcp, arg, &ike) < 0)
209 return 0;
210
Dmitry V. Levin7d8b41a2015-02-21 23:05:26 +0000211 tprintf(", {flags=%" PRIu8 ", len=%" PRIu8, ike.flags, ike.len);
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100212 if (!abbrev(tcp)) {
Dmitry V. Levin7d8b41a2015-02-21 23:05:26 +0000213 unsigned int i;
214
215 tprintf(", index=%" PRIu16 ", keycode=", ike.index);
216 printxval(evdev_keycode, ike.keycode, "KEY_???");
217 tprints(", scancode=[");
218 for (i = 0; i < ARRAY_SIZE(ike.scancode); i++) {
219 if (i > 0)
220 tprints(", ");
221 tprintf("%" PRIx8, ike.scancode[i]);
222 }
223 tprints("]}");
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100224 } else {
225 tprints(", ...}");
226 }
227 return 1;
228}
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000229# endif /* EVIOCGKEYCODE_V2 */
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100230
231static int
232getid_ioctl(struct tcb *tcp, long arg)
233{
234 struct input_id id;
235
236 if (!verbose(tcp) || umove(tcp, arg, &id) < 0)
237 return 0;
238
239 tprintf(", {ID_BUS=%" PRIu16 ", ID_VENDOR=%" PRIu16,
240 id.bustype, id.vendor);
241 if (!abbrev(tcp)) {
242 tprintf(", ID_PRODUCT=%" PRIu16 ", ID_VERSION=%" PRIu16 "}",
243 id.product, id.version);
244 } else {
245 tprints(", ...}");
246 }
247 return 1;
248}
249
250static int
251decode_bitset(struct tcb *tcp, long arg, const struct xlat decode_nr[],
252 const unsigned int max_nr, const char *dflt)
253{
254 if (!verbose(tcp))
255 return 0;
256
257 unsigned int size;
258 if ((unsigned long) tcp->u_rval > max_nr)
259 size = max_nr;
260 else
261 size = tcp->u_rval;
262 char decoded_arg[size];
263
264 if (umoven(tcp, arg, size, decoded_arg) < 0)
265 return 0;
266
267 tprints(", [");
268
269 int bit_displayed = 0;
270 int i = next_set_bit(decoded_arg, 0, size);
271 if (i < 0) {
272 tprints(" 0 ");
273 } else {
274 printxval(decode_nr, i, dflt);
275
276 while ((i = next_set_bit(decoded_arg, i + 1, size)) > 0) {
277 if (abbrev(tcp) && bit_displayed >= 3) {
278 tprints(", ...");
279 break;
280 }
281 tprints(", ");
282 printxval(decode_nr, i, dflt);
283 bit_displayed++;
284 }
285 }
286
287 tprints("]");
288
289 return 1;
290}
291
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000292# ifdef EVIOCGMTSLOTS
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100293static int
294mtslots_ioctl(struct tcb *tcp, const unsigned int code, long arg)
295{
296 const size_t size = _IOC_SIZE(code) / sizeof(int32_t);
297 if (!size)
298 return 0;
299
300 int32_t buffer[size];
301
302 if (!verbose(tcp) || umove(tcp, arg, &buffer) < 0)
303 return 0;
304
305 tprints(", {code=");
306 printxval(evdev_mtslots, buffer[0], "ABS_MT_???");
307
308 unsigned int i;
309 tprints(", values=[");
310
311 for (i = 1; i < ARRAY_SIZE(buffer); i++)
312 tprintf("%s%d", i > 1 ? ", " : "", buffer[i]);
313
314 tprints("]}");
315 return 1;
316}
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000317# endif /* EVIOCGMTSLOTS */
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100318
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000319# if defined EVIOCGREP || defined EVIOCSREP
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100320static int
321repeat_ioctl(struct tcb *tcp, long arg)
322{
Dmitry V. Levinb6795082015-07-06 22:33:39 +0000323 tprints(", ");
324 printpair_int(tcp, arg, "%u");
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100325 return 1;
326}
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000327# endif /* EVIOCGREP || EVIOCSREP */
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100328
329static int
Dmitry V. Levin53767d82016-05-27 00:40:18 +0000330bit_ioctl(struct tcb *tcp, const unsigned int ev_nr, const long arg)
331{
332 switch (ev_nr) {
333 case EV_SYN:
334 return decode_bitset(tcp, arg, evdev_sync,
335 SYN_MAX, "SYN_???");
336 case EV_KEY:
337 return decode_bitset(tcp, arg, evdev_keycode,
338 KEY_MAX, "KEY_???");
339 case EV_REL:
340 return decode_bitset(tcp, arg, evdev_relative_axes,
341 REL_MAX, "REL_???");
342 case EV_ABS:
343 return decode_bitset(tcp, arg,
344 evdev_abs, ABS_MAX, "ABS_???");
345 case EV_MSC:
346 return decode_bitset(tcp, arg,
347 evdev_misc, MSC_MAX, "MSC_???");
348# ifdef EV_SW
349 case EV_SW:
350 return decode_bitset(tcp, arg,
351 evdev_switch, SW_MAX, "SW_???");
352# endif
353 case EV_LED:
354 return decode_bitset(tcp, arg,
355 evdev_leds, LED_MAX, "LED_???");
356 case EV_SND:
357 return decode_bitset(tcp, arg,
358 evdev_snd, SND_MAX, "SND_???");
359 case EV_REP:
360 return decode_bitset(tcp, arg, evdev_autorepeat,
361 REP_MAX, "REP_???");
362 case EV_FF:
363 return decode_bitset(tcp, arg, evdev_ff_types,
364 FF_MAX, "FF_???");
365 case EV_PWR:
366 printnum_int(tcp, arg, "%d");
367 return 1;
368 case EV_FF_STATUS:
369 return decode_bitset(tcp, arg, evdev_ff_status,
370 FF_STATUS_MAX, "FF_STATUS_???");
371 default:
372 return 0;
373 }
374}
375
376static int
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000377evdev_read_ioctl(struct tcb *tcp, const unsigned int code, const long arg)
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100378{
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100379 if (syserror(tcp))
380 return 0;
381
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000382 /* fixed-number fixed-length commands */
383 switch (code) {
384 case EVIOCGVERSION:
385 tprints(", ");
386 printnum_int(tcp, arg, "%" PRIx32);
387 return 1;
388 case EVIOCGEFFECTS:
389 tprints(", ");
390 printnum_int(tcp, arg, "%" PRIu32);
391 return 1;
392 case EVIOCGID:
393 return getid_ioctl(tcp, arg);
394# ifdef EVIOCGREP
395 case EVIOCGREP:
396 return repeat_ioctl(tcp, arg);;
397# endif
398 case EVIOCGKEYCODE:
399 return keycode_ioctl(tcp, arg);
400# ifdef EVIOCGKEYCODE_V2
401 case EVIOCGKEYCODE_V2:
402 return keycode_V2_ioctl(tcp, arg);
403# endif
404 }
405
406 /* fixed-number variable-length commands */
407 switch (_IOC_NR(code)) {
408# ifdef EVIOCGMTSLOTS
409 case _IOC_NR(EVIOCGMTSLOTS(0)):
410 return mtslots_ioctl(tcp, code, arg);
411# endif
412 case _IOC_NR(EVIOCGNAME(0)):
413 case _IOC_NR(EVIOCGPHYS(0)):
414 case _IOC_NR(EVIOCGUNIQ(0)):
415 tprints(", ");
416 printstr(tcp, arg, tcp->u_rval - 1);
417 return 1;
418# ifdef EVIOCGPROP
419 case _IOC_NR(EVIOCGPROP(0)):
420 return decode_bitset(tcp, arg,
421 evdev_prop, INPUT_PROP_MAX, "PROP_???");
422# endif
423 case _IOC_NR(EVIOCGSND(0)):
424 return decode_bitset(tcp, arg,
425 evdev_snd, SND_MAX, "SND_???");
426# ifdef EVIOCGSW
427 case _IOC_NR(EVIOCGSW(0)):
428 return decode_bitset(tcp, arg,
429 evdev_switch, SW_MAX, "SW_???");
430# endif
431 case _IOC_NR(EVIOCGKEY(0)):
432 return decode_bitset(tcp, arg,
433 evdev_keycode, KEY_MAX, "KEY_???");
434 case _IOC_NR(EVIOCGLED(0)):
435 return decode_bitset(tcp, arg,
436 evdev_leds, LED_MAX, "LED_???");
437 }
438
439 /* multi-number fixed-length commands */
440 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0)))
441 return abs_ioctl(tcp, arg);
442
443 /* multi-number variable-length commands */
Dmitry V. Levin53767d82016-05-27 00:40:18 +0000444 if ((_IOC_NR(code) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
445 return bit_ioctl(tcp, _IOC_NR(code) & EV_MAX, arg);
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100446
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000447 return 0;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100448}
449
450static int
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000451evdev_write_ioctl(struct tcb *tcp, const unsigned int code, const long arg)
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100452{
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000453 /* fixed-number fixed-length commands */
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100454 switch (code) {
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000455# ifdef EVIOCSREP
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100456 case EVIOCSREP:
457 return repeat_ioctl(tcp, arg);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000458# endif
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100459 case EVIOCSKEYCODE:
460 return keycode_ioctl(tcp, arg);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000461# ifdef EVIOCSKEYCODE_V2
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100462 case EVIOCSKEYCODE_V2:
463 return keycode_V2_ioctl(tcp, arg);
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000464# endif
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100465 case EVIOCSFF:
466 return ff_effect_ioctl(tcp, arg);
467 case EVIOCRMFF:
Dmitry V. Levin00394242016-05-27 00:40:27 +0000468 tprintf(", %d", (int) arg);
469 return 1;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100470 case EVIOCGRAB:
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000471# ifdef EVIOCREVOKE
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100472 case EVIOCREVOKE:
Dmitry V. Levin8d85c232016-05-27 00:39:33 +0000473# endif
Dmitry V. Levin9635df42016-05-27 00:40:45 +0000474 tprintf(", %lu", arg);
475 return 1;
476# ifdef EVIOCSCLOCKID
477 case EVIOCSCLOCKID:
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100478 tprints(", ");
479 printnum_int(tcp, arg, "%u");
480 return 1;
Dmitry V. Levin9635df42016-05-27 00:40:45 +0000481# endif
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100482 }
Dmitry V. Levin8a18f802016-05-27 00:40:10 +0000483
484 /* multi-number fixed-length commands */
485 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0)))
486 return abs_ioctl(tcp, arg);
487
488 return 0;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100489}
490
491int
492evdev_ioctl(struct tcb *tcp, const unsigned int code, long arg)
493{
494 switch(_IOC_DIR(code)) {
495 case _IOC_READ:
Dmitry V. Levin2114e082016-05-27 00:40:02 +0000496 if (entering(tcp))
497 return 0;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100498 return evdev_read_ioctl(tcp, code, arg);
499 case _IOC_WRITE:
Dmitry V. Levin2114e082016-05-27 00:40:02 +0000500 return evdev_write_ioctl(tcp, code, arg) | RVAL_DECODED;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100501 default:
Dmitry V. Levin2114e082016-05-27 00:40:02 +0000502 return RVAL_DECODED;
Etienne Gemsa4f750b92015-02-20 17:14:10 +0100503 }
504}
505
506#endif /* HAVE_LINUX_INPUT_H */