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;
167 decode_bitset_(struct tcb *const tcp, const kernel_ulong_t arg,
168 const struct xlat decode_nr[], const unsigned int max_nr,
169 const char *const dflt, size_t decode_nr_size, enum xlat_type xt)
174 if ((kernel_ulong_t) tcp->u_rval > max_nr)
178 char decoded_arg[size];
180 if (umove_or_printaddr(tcp, arg, &decoded_arg))
181 return RVAL_IOCTL_DECODED;
185 int bit_displayed = 0;
186 int i = next_set_bit(decoded_arg, 0, size);
190 printxval_dispatch(decode_nr, decode_nr_size, i, dflt, xt);
192 while ((i = next_set_bit(decoded_arg, i + 1, size)) > 0) {
193 if (abbrev(tcp) && bit_displayed >= 3) {
198 printxval_dispatch(decode_nr, decode_nr_size, i, dflt,
206 return RVAL_IOCTL_DECODED;
209 #define decode_bitset(tcp_, arg_, decode_nr_, max_nr_, dflt_, xt_) \
210 decode_bitset_((tcp_), (arg_), (decode_nr_), (max_nr_), \
211 (dflt_), ARRAY_SIZE(decode_nr_), (xt_))
213 # ifdef EVIOCGMTSLOTS
215 mtslots_ioctl(struct tcb *const tcp, const unsigned int code,
216 const kernel_ulong_t arg)
220 const size_t size = _IOC_SIZE(code) / sizeof(int);
223 return RVAL_IOCTL_DECODED;
228 if (umove_or_printaddr(tcp, arg, &buffer))
229 return RVAL_IOCTL_DECODED;
232 printxval(evdev_mtslots, buffer[0], "ABS_MT_???");
234 tprints(", values=[");
237 for (i = 1; i < ARRAY_SIZE(buffer); i++)
238 tprintf("%s%d", i > 1 ? ", " : "", buffer[i]);
242 return RVAL_IOCTL_DECODED;
244 # endif /* EVIOCGMTSLOTS */
246 # if defined EVIOCGREP || defined EVIOCSREP
248 repeat_ioctl(struct tcb *const tcp, const kernel_ulong_t arg)
251 printpair_int(tcp, arg, "%u");
252 return RVAL_IOCTL_DECODED;
254 # endif /* EVIOCGREP || EVIOCSREP */
257 bit_ioctl(struct tcb *const tcp, const unsigned int ev_nr,
258 const kernel_ulong_t arg)
262 return decode_bitset(tcp, arg, evdev_sync,
263 SYN_MAX, "SYN_???", XT_INDEXED);
265 return decode_bitset(tcp, arg, evdev_keycode,
266 KEY_MAX, "KEY_???", XT_INDEXED);
268 return decode_bitset(tcp, arg, evdev_relative_axes,
269 REL_MAX, "REL_???", XT_INDEXED);
271 return decode_bitset(tcp, arg, evdev_abs,
272 ABS_MAX, "ABS_???", XT_INDEXED);
274 return decode_bitset(tcp, arg, evdev_misc,
275 MSC_MAX, "MSC_???", XT_INDEXED);
277 return decode_bitset(tcp, arg, evdev_switch,
278 SW_MAX, "SW_???", XT_INDEXED);
280 return decode_bitset(tcp, arg, evdev_leds,
281 LED_MAX, "LED_???", XT_INDEXED);
283 return decode_bitset(tcp, arg, evdev_snd,
284 SND_MAX, "SND_???", XT_INDEXED);
286 return decode_bitset(tcp, arg, evdev_autorepeat,
287 REP_MAX, "REP_???", XT_INDEXED);
289 return decode_bitset(tcp, arg, evdev_ff_types,
290 FF_MAX, "FF_???", XT_SORTED);
293 printnum_int(tcp, arg, "%d");
294 return RVAL_IOCTL_DECODED;
296 return decode_bitset(tcp, arg, evdev_ff_status,
297 FF_STATUS_MAX, "FF_STATUS_???",
302 return RVAL_IOCTL_DECODED;
307 evdev_read_ioctl(struct tcb *const tcp, const unsigned int code,
308 const kernel_ulong_t arg)
310 /* fixed-number fixed-length commands */
314 printnum_int(tcp, arg, "%#x");
315 return RVAL_IOCTL_DECODED;
318 printnum_int(tcp, arg, "%u");
319 return RVAL_IOCTL_DECODED;
321 return getid_ioctl(tcp, arg);
324 return repeat_ioctl(tcp, arg);
327 return keycode_ioctl(tcp, arg);
328 # ifdef EVIOCGKEYCODE_V2
329 case EVIOCGKEYCODE_V2:
330 return keycode_V2_ioctl(tcp, arg);
334 /* fixed-number variable-length commands */
335 switch (_IOC_NR(code)) {
336 # ifdef EVIOCGMTSLOTS
337 case _IOC_NR(EVIOCGMTSLOTS(0)):
338 return mtslots_ioctl(tcp, code, arg);
340 case _IOC_NR(EVIOCGNAME(0)):
341 case _IOC_NR(EVIOCGPHYS(0)):
342 case _IOC_NR(EVIOCGUNIQ(0)):
347 printstrn(tcp, arg, tcp->u_rval);
348 return RVAL_IOCTL_DECODED;
350 case _IOC_NR(EVIOCGPROP(0)):
351 return decode_bitset(tcp, arg, evdev_prop,
352 INPUT_PROP_MAX, "PROP_???",
355 case _IOC_NR(EVIOCGSND(0)):
356 return decode_bitset(tcp, arg, evdev_snd,
357 SND_MAX, "SND_???", XT_INDEXED);
359 case _IOC_NR(EVIOCGSW(0)):
360 return decode_bitset(tcp, arg, evdev_switch,
361 SW_MAX, "SW_???", XT_INDEXED);
363 case _IOC_NR(EVIOCGKEY(0)):
364 return decode_bitset(tcp, arg, evdev_keycode,
365 KEY_MAX, "KEY_???", XT_INDEXED);
366 case _IOC_NR(EVIOCGLED(0)):
367 return decode_bitset(tcp, arg, evdev_leds,
368 LED_MAX, "LED_???", XT_INDEXED);
371 /* multi-number fixed-length commands */
372 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0)))
373 return abs_ioctl(tcp, arg);
375 /* multi-number variable-length commands */
376 if ((_IOC_NR(code) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
377 return bit_ioctl(tcp, _IOC_NR(code) & EV_MAX, arg);
383 evdev_write_ioctl(struct tcb *const tcp, const unsigned int code,
384 const kernel_ulong_t arg)
386 /* fixed-number fixed-length commands */
390 return repeat_ioctl(tcp, arg);
393 return keycode_ioctl(tcp, arg);
394 # ifdef EVIOCSKEYCODE_V2
395 case EVIOCSKEYCODE_V2:
396 return keycode_V2_ioctl(tcp, arg);
399 tprintf(", %d", (int) arg);
400 return RVAL_IOCTL_DECODED;
405 tprintf(", %" PRI_klu, arg);
406 return RVAL_IOCTL_DECODED;
407 # ifdef EVIOCSCLOCKID
410 printnum_int(tcp, arg, "%u");
411 return RVAL_IOCTL_DECODED;
414 int rc = evdev_write_ioctl_mpers(tcp, code, arg);
416 if (rc != RVAL_DECODED)
421 /* multi-number fixed-length commands */
422 if ((_IOC_NR(code) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0)))
423 return abs_ioctl(tcp, arg);
429 print_evdev_ff_type(const kernel_ulong_t val)
431 printxval(evdev_ff_types, val, "FF_???");
435 evdev_ioctl(struct tcb *const tcp,
436 const unsigned int code, const kernel_ulong_t arg)
438 switch (_IOC_DIR(code)) {
442 return evdev_read_ioctl(tcp, code, arg);
444 return evdev_write_ioctl(tcp, code, arg) | RVAL_DECODED;
450 #endif /* HAVE_LINUX_INPUT_H */