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 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "xlat/evdev_abs.h"
34 #ifdef HAVE_LINUX_INPUT_H
36 # include <linux/ioctl.h>
37 # include <linux/input.h>
39 # include "xlat/evdev_autorepeat.h"
40 # include "xlat/evdev_ff_status.h"
41 # include "xlat/evdev_ff_types.h"
42 # include "xlat/evdev_keycode.h"
43 # include "xlat/evdev_leds.h"
44 # include "xlat/evdev_misc.h"
45 # include "xlat/evdev_mtslots.h"
46 # include "xlat/evdev_prop.h"
47 # include "xlat/evdev_relative_axes.h"
48 # include "xlat/evdev_snd.h"
49 # include "xlat/evdev_switch.h"
50 # include "xlat/evdev_sync.h"
56 const size_t evdev_abs_size = ARRAY_SIZE(evdev_abs) - 1;
59 abs_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
63 struct input_absinfo absinfo;
65 if (!umove_or_printaddr(tcp, arg, &absinfo)) {
78 # ifdef HAVE_STRUCT_INPUT_ABSINFO_RESOLUTION
79 tprintf(", resolution=%u",
89 return RVAL_IOCTL_DECODED;
93 keycode_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
97 unsigned int keycode[2];
99 if (!umove_or_printaddr(tcp, arg, &keycode)) {
100 tprintf("[%u, ", keycode[0]);
101 printxval_index(evdev_keycode, keycode[1], "KEY_???");
105 return RVAL_IOCTL_DECODED;
108 # ifdef EVIOCGKEYCODE_V2
110 keycode_V2_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
114 struct input_keymap_entry ike;
116 if (umove_or_printaddr(tcp, arg, &ike))
117 return RVAL_IOCTL_DECODED;
119 tprintf("{flags=%" PRIu8
120 ", len=%" PRIu8 ", ",
127 tprintf("index=%" PRIu16 ", keycode=", ike.index);
128 printxval_index(evdev_keycode, ike.keycode, "KEY_???");
129 tprints(", scancode=[");
130 for (i = 0; i < ARRAY_SIZE(ike.scancode); i++) {
133 tprintf("%" PRIx8, ike.scancode[i]);
142 return RVAL_IOCTL_DECODED;
144 # endif /* EVIOCGKEYCODE_V2 */
147 getid_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
153 if (!umove_or_printaddr(tcp, arg, &id))
154 tprintf("{ID_BUS=%" PRIu16
155 ", ID_VENDOR=%" PRIu16
156 ", ID_PRODUCT=%" PRIu16
157 ", ID_VERSION=%" PRIu16 "}",
163 return RVAL_IOCTL_DECODED;
172 printxval_dispatch(const struct xlat *xlat, size_t xlat_size, uint64_t val,
173 const char *dflt, enum xlat_type xt)
177 printxval_searchn(xlat, xlat_size, val, dflt);
181 printxval_indexn(xlat, xlat_size, val, dflt);
187 decode_bitset_(struct tcb *const tcp, const kernel_ulong_t arg,
188 const struct xlat decode_nr[], const unsigned int max_nr,
189 const char *const dflt, size_t decode_nr_size, enum xlat_type xt)
194 if ((kernel_ulong_t) tcp->u_rval > max_nr)
198 char decoded_arg[size];
200 if (umove_or_printaddr(tcp, arg, &decoded_arg))
201 return RVAL_IOCTL_DECODED;
205 int bit_displayed = 0;
206 int i = next_set_bit(decoded_arg, 0, size);
210 printxval_dispatch(decode_nr, decode_nr_size, i, dflt, xt);
212 while ((i = next_set_bit(decoded_arg, i + 1, size)) > 0) {
213 if (abbrev(tcp) && bit_displayed >= 3) {
218 printxval_dispatch(decode_nr, decode_nr_size, i, dflt,
226 return RVAL_IOCTL_DECODED;
229 #define decode_bitset(tcp_, arg_, decode_nr_, max_nr_, dflt_, xt_) \
230 decode_bitset_((tcp_), (arg_), (decode_nr_), (max_nr_), \
231 (dflt_), ARRAY_SIZE(decode_nr_), (xt_))
233 # ifdef EVIOCGMTSLOTS
235 mtslots_ioctl(struct tcb *const tcp, const unsigned int code,
236 const kernel_ulong_t arg)
240 const size_t size = _IOC_SIZE(code) / sizeof(int);
243 return RVAL_IOCTL_DECODED;
248 if (umove_or_printaddr(tcp, arg, &buffer))
249 return RVAL_IOCTL_DECODED;
252 printxval(evdev_mtslots, buffer[0], "ABS_MT_???");
254 tprints(", values=[");
257 for (i = 1; i < ARRAY_SIZE(buffer); i++)
258 tprintf("%s%d", i > 1 ? ", " : "", buffer[i]);
262 return RVAL_IOCTL_DECODED;
264 # endif /* EVIOCGMTSLOTS */
266 # if defined EVIOCGREP || defined EVIOCSREP
268 repeat_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
271 printpair_int(tcp, arg, "%u");
272 return RVAL_IOCTL_DECODED;
274 # endif /* EVIOCGREP || EVIOCSREP */
277 bit_ioctl(struct tcb *const tcp, const unsigned int ev_nr,
278 const kernel_ulong_t arg)
282 return decode_bitset(tcp, arg, evdev_sync,
283 SYN_MAX, "SYN_???", XT_INDEXED);
285 return decode_bitset(tcp, arg, evdev_keycode,
286 KEY_MAX, "KEY_???", XT_INDEXED);
288 return decode_bitset(tcp, arg, evdev_relative_axes,
289 REL_MAX, "REL_???", XT_INDEXED);
291 return decode_bitset(tcp, arg, evdev_abs,
292 ABS_MAX, "ABS_???", XT_INDEXED);
294 return decode_bitset(tcp, arg, evdev_misc,
295 MSC_MAX, "MSC_???", XT_INDEXED);
297 return decode_bitset(tcp, arg, evdev_switch,
298 SW_MAX, "SW_???", XT_INDEXED);
300 return decode_bitset(tcp, arg, evdev_leds,
301 LED_MAX, "LED_???", XT_INDEXED);
303 return decode_bitset(tcp, arg, evdev_snd,
304 SND_MAX, "SND_???", XT_INDEXED);
306 return decode_bitset(tcp, arg, evdev_autorepeat,
307 REP_MAX, "REP_???", XT_INDEXED);
309 return decode_bitset(tcp, arg, evdev_ff_types,
310 FF_MAX, "FF_???", XT_SORTED);
313 printnum_int(tcp, arg, "%d");
314 return RVAL_IOCTL_DECODED;
316 return decode_bitset(tcp, arg, evdev_ff_status,
317 FF_STATUS_MAX, "FF_STATUS_???",
322 return RVAL_IOCTL_DECODED;
327 evdev_read_ioctl(struct tcb *const tcp, const unsigned int code,
328 const kernel_ulong_t arg)
330 /* fixed-number fixed-length commands */
334 printnum_int(tcp, arg, "%#x");
335 return RVAL_IOCTL_DECODED;
338 printnum_int(tcp, arg, "%u");
339 return RVAL_IOCTL_DECODED;
341 return getid_ioctl(tcp, arg);
344 return repeat_ioctl(tcp, arg);
347 return keycode_ioctl(tcp, arg);
348 # ifdef EVIOCGKEYCODE_V2
349 case EVIOCGKEYCODE_V2:
350 return keycode_V2_ioctl(tcp, arg);
354 /* fixed-number variable-length commands */
355 switch (_IOC_NR(code)) {
356 # ifdef EVIOCGMTSLOTS
357 case _IOC_NR(EVIOCGMTSLOTS(0)):
358 return mtslots_ioctl(tcp, code, arg);
360 case _IOC_NR(EVIOCGNAME(0)):
361 case _IOC_NR(EVIOCGPHYS(0)):
362 case _IOC_NR(EVIOCGUNIQ(0)):
367 printstrn(tcp, arg, tcp->u_rval);
368 return RVAL_IOCTL_DECODED;
370 case _IOC_NR(EVIOCGPROP(0)):
371 return decode_bitset(tcp, arg, evdev_prop,
372 INPUT_PROP_MAX, "PROP_???",
375 case _IOC_NR(EVIOCGSND(0)):
376 return decode_bitset(tcp, arg, evdev_snd,
377 SND_MAX, "SND_???", XT_INDEXED);
379 case _IOC_NR(EVIOCGSW(0)):
380 return decode_bitset(tcp, arg, evdev_switch,
381 SW_MAX, "SW_???", XT_INDEXED);
383 case _IOC_NR(EVIOCGKEY(0)):
384 return decode_bitset(tcp, arg, evdev_keycode,
385 KEY_MAX, "KEY_???", XT_INDEXED);
386 case _IOC_NR(EVIOCGLED(0)):
387 return decode_bitset(tcp, arg, evdev_leds,
388 LED_MAX, "LED_???", XT_INDEXED);
391 /* multi-number fixed-length commands */
392 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0)))
393 return abs_ioctl(tcp, arg);
395 /* multi-number variable-length commands */
396 if ((_IOC_NR(code) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
397 return bit_ioctl(tcp, _IOC_NR(code) & EV_MAX, arg);
403 evdev_write_ioctl(struct tcb *const tcp, const unsigned int code,
404 const kernel_ulong_t arg)
406 /* fixed-number fixed-length commands */
410 return repeat_ioctl(tcp, arg);
413 return keycode_ioctl(tcp, arg);
414 # ifdef EVIOCSKEYCODE_V2
415 case EVIOCSKEYCODE_V2:
416 return keycode_V2_ioctl(tcp, arg);
419 tprintf(", %d", (int) arg);
420 return RVAL_IOCTL_DECODED;
425 tprintf(", %" PRI_klu, arg);
426 return RVAL_IOCTL_DECODED;
427 # ifdef EVIOCSCLOCKID
430 printnum_int(tcp, arg, "%u");
431 return RVAL_IOCTL_DECODED;
434 int rc = evdev_write_ioctl_mpers(tcp, code, arg);
436 if (rc != RVAL_DECODED)
441 /* multi-number fixed-length commands */
442 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0)))
443 return abs_ioctl(tcp, arg);
449 print_evdev_ff_type(const kernel_ulong_t val)
451 printxval(evdev_ff_types, val, "FF_???");
455 evdev_ioctl(struct tcb *const tcp,
456 const unsigned int code, const kernel_ulong_t arg)
458 switch (_IOC_DIR(code)) {
462 return evdev_read_ioctl(tcp, code, arg);
464 return evdev_write_ioctl(tcp, code, arg) | RVAL_DECODED;
470 #endif /* HAVE_LINUX_INPUT_H */