1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * expose input properties via udev
5 * Portions Copyright © 2004 David Zeuthen, <david@fubar.dk>
6 * Copyright © 2014 Carlos Garnacho <carlosg@gnome.org>
13 #include <linux/limits.h>
15 #include "device-util.h"
17 #include "missing_input.h"
18 #include "parse-util.h"
19 #include "stdio-util.h"
20 #include "string-util.h"
21 #include "udev-builtin.h"
23 /* we must use this kernel-compatible implementation */
24 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
25 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
26 #define OFF(x) ((x)%BITS_PER_LONG)
27 #define BIT(x) (1UL<<OFF(x))
28 #define LONG(x) ((x)/BITS_PER_LONG)
29 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
36 /* key code ranges above BTN_MISC (start is inclusive, stop is exclusive) */
37 static const struct range high_key_blocks
[] = {
38 { KEY_OK
, BTN_DPAD_UP
},
39 { KEY_ALS_TOGGLE
, BTN_TRIGGER_HAPPY
}
42 static int abs_size_mm(const struct input_absinfo
*absinfo
) {
43 /* Resolution is defined to be in units/mm for ABS_X/Y */
44 return (absinfo
->maximum
- absinfo
->minimum
) / absinfo
->resolution
;
47 static void extract_info(sd_device
*dev
, bool test
) {
48 char width
[DECIMAL_STR_MAX(int)], height
[DECIMAL_STR_MAX(int)];
49 struct input_absinfo xabsinfo
= {}, yabsinfo
= {};
50 _cleanup_close_
int fd
= -EBADF
;
52 fd
= sd_device_open(dev
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
56 if (ioctl(fd
, EVIOCGABS(ABS_X
), &xabsinfo
) < 0 ||
57 ioctl(fd
, EVIOCGABS(ABS_Y
), &yabsinfo
) < 0)
60 if (xabsinfo
.resolution
<= 0 || yabsinfo
.resolution
<= 0)
63 xsprintf(width
, "%d", abs_size_mm(&xabsinfo
));
64 xsprintf(height
, "%d", abs_size_mm(&yabsinfo
));
66 udev_builtin_add_property(dev
, test
, "ID_INPUT_WIDTH_MM", width
);
67 udev_builtin_add_property(dev
, test
, "ID_INPUT_HEIGHT_MM", height
);
71 * Read a capability attribute and return bitmask.
72 * @param dev sd_device
73 * @param attr sysfs attribute name (e. g. "capabilities/key")
74 * @param bitmask: Output array which has a sizeof of bitmask_size
76 static void get_cap_mask(sd_device
*pdev
, const char* attr
,
77 unsigned long *bitmask
, size_t bitmask_size
,
86 if (sd_device_get_sysattr_value(pdev
, attr
, &v
) < 0)
89 xsprintf(text
, "%s", v
);
90 log_device_debug(pdev
, "%s raw kernel attribute: %s", attr
, text
);
92 memzero(bitmask
, bitmask_size
);
94 while ((word
= strrchr(text
, ' '))) {
95 r
= safe_atolu_full(word
+1, 16, &val
);
97 log_device_debug_errno(pdev
, r
, "Ignoring %s block which failed to parse: %m", attr
);
98 else if (i
< bitmask_size
/ sizeof(unsigned long))
101 log_device_debug(pdev
, "Ignoring %s block %lX which is larger than maximum size", attr
, val
);
105 r
= safe_atolu_full(text
, 16, &val
);
107 log_device_debug_errno(pdev
, r
, "Ignoring %s block which failed to parse: %m", attr
);
108 else if (i
< bitmask_size
/ sizeof(unsigned long))
111 log_device_debug(pdev
, "Ignoring %s block %lX which is larger than maximum size", attr
, val
);
113 if (test
&& DEBUG_LOGGING
) {
114 log_device_debug(pdev
, "%s decoded bit map:", attr
);
116 val
= bitmask_size
/ sizeof (unsigned long);
117 /* skip trailing zeros */
118 while (bitmask
[val
-1] == 0 && val
> 0)
121 /* IN_SET() cannot be used in assert_cc(). */
122 assert_cc(sizeof(unsigned long) == 4 || sizeof(unsigned long) == 8);
123 for (unsigned long j
= 0; j
< val
; j
++)
124 log_device_debug(pdev
,
125 sizeof(unsigned long) == 4 ? " bit %4lu: %08lX\n" : " bit %4lu: %016lX\n",
126 j
* BITS_PER_LONG
, bitmask
[j
]);
130 static struct input_id
get_input_id(sd_device
*dev
) {
132 struct input_id id
= {};
134 if (sd_device_get_sysattr_value(dev
, "id/bustype", &v
) >= 0)
135 (void) safe_atoux16(v
, &id
.bustype
);
136 if (sd_device_get_sysattr_value(dev
, "id/vendor", &v
) >= 0)
137 (void) safe_atoux16(v
, &id
.vendor
);
138 if (sd_device_get_sysattr_value(dev
, "id/product", &v
) >= 0)
139 (void) safe_atoux16(v
, &id
.product
);
140 if (sd_device_get_sysattr_value(dev
, "id/version", &v
) >= 0)
141 (void) safe_atoux16(v
, &id
.version
);
146 /* pointer devices */
147 static bool test_pointers(sd_device
*dev
,
148 const struct input_id
*id
,
149 const unsigned long* bitmask_ev
,
150 const unsigned long* bitmask_abs
,
151 const unsigned long* bitmask_key
,
152 const unsigned long* bitmask_rel
,
153 const unsigned long* bitmask_props
,
155 bool has_abs_coordinates
= false;
156 bool has_rel_coordinates
= false;
157 bool has_mt_coordinates
= false;
158 size_t num_joystick_axes
= 0;
159 size_t num_joystick_buttons
= 0;
160 bool has_pad_buttons
= false;
161 bool is_direct
= false;
162 bool has_touch
= false;
163 bool has_3d_coordinates
= false;
164 bool has_keys
= false;
165 bool has_stylus
= false;
166 bool has_pen
= false;
167 bool finger_but_no_pen
= false;
168 bool has_mouse_button
= false;
169 bool is_mouse
= false;
170 bool is_abs_mouse
= false;
171 bool is_touchpad
= false;
172 bool is_touchscreen
= false;
173 bool is_tablet
= false;
174 bool is_tablet_pad
= false;
175 bool is_joystick
= false;
176 bool is_accelerometer
= false;
177 bool is_pointing_stick
= false;
179 has_keys
= test_bit(EV_KEY
, bitmask_ev
);
180 has_abs_coordinates
= test_bit(ABS_X
, bitmask_abs
) && test_bit(ABS_Y
, bitmask_abs
);
181 has_3d_coordinates
= has_abs_coordinates
&& test_bit(ABS_Z
, bitmask_abs
);
182 is_accelerometer
= test_bit(INPUT_PROP_ACCELEROMETER
, bitmask_props
);
184 if (!has_keys
&& has_3d_coordinates
)
185 is_accelerometer
= true;
187 if (is_accelerometer
) {
188 udev_builtin_add_property(dev
, test
, "ID_INPUT_ACCELEROMETER", "1");
192 is_pointing_stick
= test_bit(INPUT_PROP_POINTING_STICK
, bitmask_props
);
193 has_stylus
= test_bit(BTN_STYLUS
, bitmask_key
);
194 has_pen
= test_bit(BTN_TOOL_PEN
, bitmask_key
);
195 finger_but_no_pen
= test_bit(BTN_TOOL_FINGER
, bitmask_key
) && !test_bit(BTN_TOOL_PEN
, bitmask_key
);
196 for (int button
= BTN_MOUSE
; button
< BTN_JOYSTICK
&& !has_mouse_button
; button
++)
197 has_mouse_button
= test_bit(button
, bitmask_key
);
198 has_rel_coordinates
= test_bit(EV_REL
, bitmask_ev
) && test_bit(REL_X
, bitmask_rel
) && test_bit(REL_Y
, bitmask_rel
);
199 has_mt_coordinates
= test_bit(ABS_MT_POSITION_X
, bitmask_abs
) && test_bit(ABS_MT_POSITION_Y
, bitmask_abs
);
201 /* unset has_mt_coordinates if devices claims to have all abs axis */
202 if (has_mt_coordinates
&& test_bit(ABS_MT_SLOT
, bitmask_abs
) && test_bit(ABS_MT_SLOT
- 1, bitmask_abs
))
203 has_mt_coordinates
= false;
204 is_direct
= test_bit(INPUT_PROP_DIRECT
, bitmask_props
);
205 has_touch
= test_bit(BTN_TOUCH
, bitmask_key
);
206 has_pad_buttons
= test_bit(BTN_0
, bitmask_key
) && has_stylus
&& !has_pen
;
208 /* joysticks don't necessarily have buttons; e. g.
209 * rudders/pedals are joystick-like, but buttonless; they have
210 * other fancy axes. Others have buttons only but no axes.
212 * The BTN_JOYSTICK range starts after the mouse range, so a mouse
213 * with more than 16 buttons runs into the joystick range (e.g. Mad
214 * Catz Mad Catz M.M.O.TE). Skip those.
216 if (!test_bit(BTN_JOYSTICK
- 1, bitmask_key
)) {
217 for (int button
= BTN_JOYSTICK
; button
< BTN_DIGI
; button
++)
218 if (test_bit(button
, bitmask_key
))
219 num_joystick_buttons
++;
220 for (int button
= BTN_TRIGGER_HAPPY1
; button
<= BTN_TRIGGER_HAPPY40
; button
++)
221 if (test_bit(button
, bitmask_key
))
222 num_joystick_buttons
++;
223 for (int button
= BTN_DPAD_UP
; button
<= BTN_DPAD_RIGHT
; button
++)
224 if (test_bit(button
, bitmask_key
))
225 num_joystick_buttons
++;
227 for (int axis
= ABS_RX
; axis
< ABS_PRESSURE
; axis
++)
228 if (test_bit(axis
, bitmask_abs
))
231 if (has_abs_coordinates
) {
232 if (has_stylus
|| has_pen
)
234 else if (finger_but_no_pen
&& !is_direct
)
236 else if (has_mouse_button
)
237 /* This path is taken by VMware's USB mouse, which has
238 * absolute axes, but no touch/pressure button. */
240 else if (has_touch
|| is_direct
)
241 is_touchscreen
= true;
242 else if (num_joystick_buttons
> 0 || num_joystick_axes
> 0)
244 } else if (num_joystick_buttons
> 0 || num_joystick_axes
> 0)
247 if (has_mt_coordinates
) {
248 if (has_stylus
|| has_pen
)
250 else if (finger_but_no_pen
&& !is_direct
)
252 else if (has_touch
|| is_direct
)
253 is_touchscreen
= true;
256 if (is_tablet
&& has_pad_buttons
)
257 is_tablet_pad
= true;
259 if (!is_tablet
&& !is_touchpad
&& !is_joystick
&&
261 (has_rel_coordinates
||
262 !has_abs_coordinates
)) /* mouse buttons and no axis */
265 /* There is no such thing as an i2c mouse */
266 if (is_mouse
&& id
->bustype
== BUS_I2C
)
267 is_pointing_stick
= true;
269 /* Joystick un-detection. Some keyboards have random joystick buttons
270 * set. Avoid those being labeled as ID_INPUT_JOYSTICK with some heuristics.
271 * The well-known keys represent a (randomly picked) set of key groups.
272 * A joystick may have one of those but probably not several. And a joystick with less than 2 buttons
273 * or axes is not a joystick either.
274 * libinput uses similar heuristics, any changes here should be added to libinput too.
277 static const unsigned int well_known_keyboard_keys
[] = {
278 KEY_LEFTCTRL
, KEY_CAPSLOCK
, KEY_NUMLOCK
, KEY_INSERT
,
279 KEY_MUTE
, KEY_CALC
, KEY_FILE
, KEY_MAIL
, KEY_PLAYPAUSE
,
282 size_t num_well_known_keys
= 0;
285 for (size_t i
= 0; i
< ELEMENTSOF(well_known_keyboard_keys
); i
++)
286 if (test_bit(well_known_keyboard_keys
[i
], bitmask_key
))
287 num_well_known_keys
++;
289 if (num_well_known_keys
>= 4 || num_joystick_buttons
+ num_joystick_axes
< 2) {
290 log_device_debug(dev
, "Input device has %zu joystick buttons and %zu axes but also %zu keyboard key sets, "
291 "assuming this is a keyboard, not a joystick.",
292 num_joystick_buttons
, num_joystick_axes
, num_well_known_keys
);
297 if (is_pointing_stick
)
298 udev_builtin_add_property(dev
, test
, "ID_INPUT_POINTINGSTICK", "1");
299 if (is_mouse
|| is_abs_mouse
)
300 udev_builtin_add_property(dev
, test
, "ID_INPUT_MOUSE", "1");
302 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHPAD", "1");
304 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHSCREEN", "1");
306 udev_builtin_add_property(dev
, test
, "ID_INPUT_JOYSTICK", "1");
308 udev_builtin_add_property(dev
, test
, "ID_INPUT_TABLET", "1");
310 udev_builtin_add_property(dev
, test
, "ID_INPUT_TABLET_PAD", "1");
312 return is_tablet
|| is_mouse
|| is_abs_mouse
|| is_touchpad
|| is_touchscreen
|| is_joystick
|| is_pointing_stick
;
315 /* key like devices */
316 static bool test_key(sd_device
*dev
,
317 const unsigned long* bitmask_ev
,
318 const unsigned long* bitmask_key
,
323 /* do we have any KEY_* capability? */
324 if (!test_bit(EV_KEY
, bitmask_ev
)) {
325 log_device_debug(dev
, "test_key: no EV_KEY capability");
329 /* only consider KEY_* here, not BTN_* */
330 for (size_t i
= 0; i
< BTN_MISC
/BITS_PER_LONG
&& !found
; i
++) {
334 log_device_debug(dev
, "test_key: checking bit block %zu for any keys; found=%s",
335 i
* BITS_PER_LONG
, yes_no(found
));
337 /* If there are no keys in the lower block, check the higher blocks */
338 for (size_t block
= 0; block
< sizeof(high_key_blocks
) / sizeof(struct range
) && !found
; block
++)
339 for (unsigned i
= high_key_blocks
[block
].start
; i
< high_key_blocks
[block
].end
&& !found
; i
++)
340 if (test_bit(i
, bitmask_key
)) {
341 log_device_debug(dev
, "test_key: Found key %x in high block", i
);
346 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
348 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
349 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
350 if (FLAGS_SET(bitmask_key
[0], 0xFFFFFFFE)) {
351 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEYBOARD", "1");
358 static int builtin_input_id(sd_device
*dev
, sd_netlink
**rtnl
, int argc
, char *argv
[], bool test
) {
360 unsigned long bitmask_ev
[NBITS(EV_MAX
)];
361 unsigned long bitmask_abs
[NBITS(ABS_MAX
)];
362 unsigned long bitmask_key
[NBITS(KEY_MAX
)];
363 unsigned long bitmask_rel
[NBITS(REL_MAX
)];
364 unsigned long bitmask_props
[NBITS(INPUT_PROP_MAX
)];
371 /* walk up the parental chain until we find the real input device; the
372 * argument is very likely a subdevice of this, like eventN */
373 for (pdev
= dev
; pdev
; ) {
376 if (sd_device_get_sysattr_value(pdev
, "capabilities/ev", &s
) >= 0)
379 if (sd_device_get_parent_with_subsystem_devtype(pdev
, "input", NULL
, &pdev
) >= 0)
387 struct input_id id
= get_input_id(pdev
);
389 /* Use this as a flag that input devices were detected, so that this
390 * program doesn't need to be called more than once per device */
391 udev_builtin_add_property(dev
, test
, "ID_INPUT", "1");
392 get_cap_mask(pdev
, "capabilities/ev", bitmask_ev
, sizeof(bitmask_ev
), test
);
393 get_cap_mask(pdev
, "capabilities/abs", bitmask_abs
, sizeof(bitmask_abs
), test
);
394 get_cap_mask(pdev
, "capabilities/rel", bitmask_rel
, sizeof(bitmask_rel
), test
);
395 get_cap_mask(pdev
, "capabilities/key", bitmask_key
, sizeof(bitmask_key
), test
);
396 get_cap_mask(pdev
, "properties", bitmask_props
, sizeof(bitmask_props
), test
);
397 is_pointer
= test_pointers(dev
, &id
, bitmask_ev
, bitmask_abs
,
398 bitmask_key
, bitmask_rel
,
399 bitmask_props
, test
);
400 is_key
= test_key(dev
, bitmask_ev
, bitmask_key
, test
);
401 /* Some evdev nodes have only a scrollwheel */
402 if (!is_pointer
&& !is_key
&& test_bit(EV_REL
, bitmask_ev
) &&
403 (test_bit(REL_WHEEL
, bitmask_rel
) || test_bit(REL_HWHEEL
, bitmask_rel
)))
404 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
405 if (test_bit(EV_SW
, bitmask_ev
))
406 udev_builtin_add_property(dev
, test
, "ID_INPUT_SWITCH", "1");
410 if (sd_device_get_sysname(dev
, &sysname
) >= 0 &&
411 startswith(sysname
, "event"))
412 extract_info(dev
, test
);
417 const UdevBuiltin udev_builtin_input_id
= {
419 .cmd
= builtin_input_id
,
420 .help
= "Input device properties",