2 * Copyright (c) 2015 Etienne Gemsa <etienne.gemsa@lse.epita.fr>
3 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
4 * Copyright (c) 2015-2018 The strace developers.
7 * SPDX-License-Identifier: LGPL-2.1-or-later
12 #include "xlat/evdev_abs.h"
13 #include "xlat/evdev_ev.h"
15 #ifdef HAVE_LINUX_INPUT_H
17 # include <linux/ioctl.h>
18 # include <linux/input.h>
20 # include "xlat/evdev_autorepeat.h"
21 # include "xlat/evdev_ff_status.h"
22 # include "xlat/evdev_ff_types.h"
23 # include "xlat/evdev_keycode.h"
24 # include "xlat/evdev_leds.h"
25 # include "xlat/evdev_misc.h"
26 # include "xlat/evdev_mtslots.h"
27 # include "xlat/evdev_prop.h"
28 # include "xlat/evdev_relative_axes.h"
29 # include "xlat/evdev_snd.h"
30 # include "xlat/evdev_switch.h"
36 const size_t evdev_abs_size = ARRAY_SIZE(evdev_abs) - 1;
39 abs_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
43 struct input_absinfo absinfo;
45 if (!umove_or_printaddr(tcp, arg, &absinfo)) {
58 # ifdef HAVE_STRUCT_INPUT_ABSINFO_RESOLUTION
59 tprintf(", resolution=%u",
69 return RVAL_IOCTL_DECODED;
73 keycode_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
77 unsigned int keycode[2];
79 if (!umove_or_printaddr(tcp, arg, &keycode)) {
80 tprintf("[%u, ", keycode[0]);
81 printxval_index(evdev_keycode, keycode[1], "KEY_???");
85 return RVAL_IOCTL_DECODED;
88 # ifdef EVIOCGKEYCODE_V2
90 keycode_V2_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
94 struct input_keymap_entry ike;
96 if (umove_or_printaddr(tcp, arg, &ike))
97 return RVAL_IOCTL_DECODED;
99 tprintf("{flags=%" PRIu8
100 ", len=%" PRIu8 ", ",
107 tprintf("index=%" PRIu16 ", keycode=", ike.index);
108 printxval_index(evdev_keycode, ike.keycode, "KEY_???");
109 tprints(", scancode=[");
110 for (i = 0; i < ARRAY_SIZE(ike.scancode); i++) {
113 tprintf("%" PRIx8, ike.scancode[i]);
122 return RVAL_IOCTL_DECODED;
124 # endif /* EVIOCGKEYCODE_V2 */
127 getid_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
133 if (!umove_or_printaddr(tcp, arg, &id))
134 tprintf("{ID_BUS=%" PRIu16
135 ", ID_VENDOR=%" PRIu16
136 ", ID_PRODUCT=%" PRIu16
137 ", ID_VERSION=%" PRIu16 "}",
143 return RVAL_IOCTL_DECODED;
147 decode_bitset_(struct tcb *const tcp, const kernel_ulong_t arg,
148 const struct xlat decode_nr[], const unsigned int max_nr,
149 const char *const dflt, size_t decode_nr_size, enum xlat_type xt)
154 if ((kernel_ulong_t) tcp->u_rval > max_nr / 8)
157 size = tcp->u_rval * 8;
158 char decoded_arg[size];
160 if (umove_or_printaddr(tcp, arg, &decoded_arg))
161 return RVAL_IOCTL_DECODED;
165 int bit_displayed = 0;
166 int i = next_set_bit(decoded_arg, 0, size);
170 printxval_dispatch(decode_nr, decode_nr_size, i, dflt, xt);
172 while ((i = next_set_bit(decoded_arg, i + 1, size)) > 0) {
173 if (abbrev(tcp) && bit_displayed >= 3) {
178 printxval_dispatch(decode_nr, decode_nr_size, i, dflt,
186 return RVAL_IOCTL_DECODED;
189 #define decode_bitset(tcp_, arg_, decode_nr_, max_nr_, dflt_, xt_) \
190 decode_bitset_((tcp_), (arg_), (decode_nr_), (max_nr_), \
191 (dflt_), ARRAY_SIZE(decode_nr_), (xt_))
193 # ifdef EVIOCGMTSLOTS
195 mtslots_ioctl(struct tcb *const tcp, const unsigned int code,
196 const kernel_ulong_t arg)
200 const size_t size = _IOC_SIZE(code) / sizeof(int);
203 return RVAL_IOCTL_DECODED;
208 if (umove_or_printaddr(tcp, arg, &buffer))
209 return RVAL_IOCTL_DECODED;
212 printxval(evdev_mtslots, buffer[0], "ABS_MT_???");
214 tprints(", values=[");
217 for (i = 1; i < ARRAY_SIZE(buffer); i++)
218 tprintf("%s%d", i > 1 ? ", " : "", buffer[i]);
222 return RVAL_IOCTL_DECODED;
224 # endif /* EVIOCGMTSLOTS */
226 # if defined EVIOCGREP || defined EVIOCSREP
228 repeat_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
231 printpair_int(tcp, arg, "%u");
232 return RVAL_IOCTL_DECODED;
234 # endif /* EVIOCGREP || EVIOCSREP */
237 bit_ioctl(struct tcb *const tcp, const unsigned int ev_nr,
238 const kernel_ulong_t arg)
242 return decode_bitset(tcp, arg, evdev_ev,
243 EV_MAX, "EV_???", XT_SORTED);
245 return decode_bitset(tcp, arg, evdev_keycode,
246 KEY_MAX, "KEY_???", XT_INDEXED);
248 return decode_bitset(tcp, arg, evdev_relative_axes,
249 REL_MAX, "REL_???", XT_INDEXED);
251 return decode_bitset(tcp, arg, evdev_abs,
252 ABS_MAX, "ABS_???", XT_INDEXED);
254 return decode_bitset(tcp, arg, evdev_misc,
255 MSC_MAX, "MSC_???", XT_INDEXED);
257 return decode_bitset(tcp, arg, evdev_switch,
258 SW_MAX, "SW_???", XT_INDEXED);
260 return decode_bitset(tcp, arg, evdev_leds,
261 LED_MAX, "LED_???", XT_INDEXED);
263 return decode_bitset(tcp, arg, evdev_snd,
264 SND_MAX, "SND_???", XT_INDEXED);
266 return decode_bitset(tcp, arg, evdev_autorepeat,
267 REP_MAX, "REP_???", XT_INDEXED);
269 return decode_bitset(tcp, arg, evdev_ff_types,
270 FF_MAX, "FF_???", XT_SORTED);
273 printnum_int(tcp, arg, "%d");
274 return RVAL_IOCTL_DECODED;
276 return decode_bitset(tcp, arg, evdev_ff_status,
277 FF_STATUS_MAX, "FF_STATUS_???",
282 return RVAL_IOCTL_DECODED;
287 evdev_read_ioctl(struct tcb *const tcp, const unsigned int code,
288 const kernel_ulong_t arg)
290 /* fixed-number fixed-length commands */
294 printnum_int(tcp, arg, "%#x");
295 return RVAL_IOCTL_DECODED;
298 printnum_int(tcp, arg, "%u");
299 return RVAL_IOCTL_DECODED;
301 return getid_ioctl(tcp, arg);
304 return repeat_ioctl(tcp, arg);
307 return keycode_ioctl(tcp, arg);
308 # ifdef EVIOCGKEYCODE_V2
309 case EVIOCGKEYCODE_V2:
310 return keycode_V2_ioctl(tcp, arg);
314 /* fixed-number variable-length commands */
315 switch (_IOC_NR(code)) {
316 # ifdef EVIOCGMTSLOTS
317 case _IOC_NR(EVIOCGMTSLOTS(0)):
318 return mtslots_ioctl(tcp, code, arg);
320 case _IOC_NR(EVIOCGNAME(0)):
321 case _IOC_NR(EVIOCGPHYS(0)):
322 case _IOC_NR(EVIOCGUNIQ(0)):
327 printstrn(tcp, arg, tcp->u_rval);
328 return RVAL_IOCTL_DECODED;
330 case _IOC_NR(EVIOCGPROP(0)):
331 return decode_bitset(tcp, arg, evdev_prop,
332 INPUT_PROP_MAX, "PROP_???",
335 case _IOC_NR(EVIOCGSND(0)):
336 return decode_bitset(tcp, arg, evdev_snd,
337 SND_MAX, "SND_???", XT_INDEXED);
339 case _IOC_NR(EVIOCGSW(0)):
340 return decode_bitset(tcp, arg, evdev_switch,
341 SW_MAX, "SW_???", XT_INDEXED);
343 case _IOC_NR(EVIOCGKEY(0)):
344 return decode_bitset(tcp, arg, evdev_keycode,
345 KEY_MAX, "KEY_???", XT_INDEXED);
346 case _IOC_NR(EVIOCGLED(0)):
347 return decode_bitset(tcp, arg, evdev_leds,
348 LED_MAX, "LED_???", XT_INDEXED);
351 /* multi-number fixed-length commands */
352 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0)))
353 return abs_ioctl(tcp, arg);
355 /* multi-number variable-length commands */
356 if ((_IOC_NR(code) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
357 return bit_ioctl(tcp, _IOC_NR(code) & EV_MAX, arg);
363 evdev_write_ioctl(struct tcb *const tcp, const unsigned int code,
364 const kernel_ulong_t arg)
366 /* fixed-number fixed-length commands */
370 return repeat_ioctl(tcp, arg);
373 return keycode_ioctl(tcp, arg);
374 # ifdef EVIOCSKEYCODE_V2
375 case EVIOCSKEYCODE_V2:
376 return keycode_V2_ioctl(tcp, arg);
379 tprintf(", %d", (int) arg);
380 return RVAL_IOCTL_DECODED;
385 tprintf(", %" PRI_klu, arg);
386 return RVAL_IOCTL_DECODED;
387 # ifdef EVIOCSCLOCKID
390 printnum_int(tcp, arg, "%u");
391 return RVAL_IOCTL_DECODED;
394 int rc = evdev_write_ioctl_mpers(tcp, code, arg);
396 if (rc != RVAL_DECODED)
401 /* multi-number fixed-length commands */
402 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0)))
403 return abs_ioctl(tcp, arg);
409 print_evdev_ff_type(const kernel_ulong_t val)
411 printxval(evdev_ff_types, val, "FF_???");
415 evdev_ioctl(struct tcb *const tcp,
416 const unsigned int code, const kernel_ulong_t arg)
418 switch (_IOC_DIR(code)) {
422 return evdev_read_ioctl(tcp, code, arg);
424 return evdev_write_ioctl(tcp, code, arg) | RVAL_DECODED;
430 #endif /* HAVE_LINUX_INPUT_H */