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"
24 /* we must use this kernel-compatible implementation */
25 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
26 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
27 #define OFF(x) ((x)%BITS_PER_LONG)
28 #define BIT(x) (1UL<<OFF(x))
29 #define LONG(x) ((x)/BITS_PER_LONG)
30 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
37 /* key code ranges above BTN_MISC (start is inclusive, stop is exclusive) */
38 static const struct range high_key_blocks
[] = {
39 { KEY_OK
, BTN_DPAD_UP
},
40 { KEY_ALS_TOGGLE
, BTN_TRIGGER_HAPPY
}
43 static int abs_size_mm(const struct input_absinfo
*absinfo
) {
44 /* Resolution is defined to be in units/mm for ABS_X/Y */
45 return (absinfo
->maximum
- absinfo
->minimum
) / absinfo
->resolution
;
48 static void extract_info(sd_device
*dev
, bool test
) {
49 char width
[DECIMAL_STR_MAX(int)], height
[DECIMAL_STR_MAX(int)];
50 struct input_absinfo xabsinfo
= {}, yabsinfo
= {};
51 _cleanup_close_
int fd
= -1;
53 fd
= sd_device_open(dev
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
57 if (ioctl(fd
, EVIOCGABS(ABS_X
), &xabsinfo
) < 0 ||
58 ioctl(fd
, EVIOCGABS(ABS_Y
), &yabsinfo
) < 0)
61 if (xabsinfo
.resolution
<= 0 || yabsinfo
.resolution
<= 0)
64 xsprintf(width
, "%d", abs_size_mm(&xabsinfo
));
65 xsprintf(height
, "%d", abs_size_mm(&yabsinfo
));
67 udev_builtin_add_property(dev
, test
, "ID_INPUT_WIDTH_MM", width
);
68 udev_builtin_add_property(dev
, test
, "ID_INPUT_HEIGHT_MM", height
);
72 * Read a capability attribute and return bitmask.
73 * @param dev sd_device
74 * @param attr sysfs attribute name (e. g. "capabilities/key")
75 * @param bitmask: Output array which has a sizeof of bitmask_size
77 static void get_cap_mask(sd_device
*pdev
, const char* attr
,
78 unsigned long *bitmask
, size_t bitmask_size
,
87 if (sd_device_get_sysattr_value(pdev
, attr
, &v
) < 0)
90 xsprintf(text
, "%s", v
);
91 log_device_debug(pdev
, "%s raw kernel attribute: %s", attr
, text
);
93 memzero(bitmask
, bitmask_size
);
95 while ((word
= strrchr(text
, ' '))) {
96 r
= safe_atolu_full(word
+1, 16, &val
);
98 log_device_debug_errno(pdev
, r
, "Ignoring %s block which failed to parse: %m", attr
);
99 else if (i
< bitmask_size
/ sizeof(unsigned long))
102 log_device_debug(pdev
, "Ignoring %s block %lX which is larger than maximum size", attr
, val
);
106 r
= safe_atolu_full(text
, 16, &val
);
108 log_device_debug_errno(pdev
, r
, "Ignoring %s block which failed to parse: %m", attr
);
109 else if (i
< bitmask_size
/ sizeof(unsigned long))
112 log_device_debug(pdev
, "Ignoring %s block %lX which is larger than maximum size", attr
, val
);
114 if (test
&& DEBUG_LOGGING
) {
115 log_device_debug(pdev
, "%s decoded bit map:", attr
);
117 val
= bitmask_size
/ sizeof (unsigned long);
118 /* skip trailing zeros */
119 while (bitmask
[val
-1] == 0 && val
> 0)
122 /* IN_SET() cannot be used in assert_cc(). */
123 assert_cc(sizeof(unsigned long) == 4 || sizeof(unsigned long) == 8);
124 for (unsigned long j
= 0; j
< val
; j
++)
125 log_device_debug(pdev
,
126 sizeof(unsigned long) == 4 ? " bit %4lu: %08lX\n" : " bit %4lu: %016lX\n",
127 j
* BITS_PER_LONG
, bitmask
[j
]);
131 static struct input_id
get_input_id(sd_device
*dev
) {
133 struct input_id id
= {};
135 if (sd_device_get_sysattr_value(dev
, "id/bustype", &v
) >= 0)
136 (void) safe_atoux16(v
, &id
.bustype
);
137 if (sd_device_get_sysattr_value(dev
, "id/vendor", &v
) >= 0)
138 (void) safe_atoux16(v
, &id
.vendor
);
139 if (sd_device_get_sysattr_value(dev
, "id/product", &v
) >= 0)
140 (void) safe_atoux16(v
, &id
.product
);
141 if (sd_device_get_sysattr_value(dev
, "id/version", &v
) >= 0)
142 (void) safe_atoux16(v
, &id
.version
);
147 /* pointer devices */
148 static bool test_pointers(sd_device
*dev
,
149 const struct input_id
*id
,
150 const unsigned long* bitmask_ev
,
151 const unsigned long* bitmask_abs
,
152 const unsigned long* bitmask_key
,
153 const unsigned long* bitmask_rel
,
154 const unsigned long* bitmask_props
,
156 bool has_abs_coordinates
= false;
157 bool has_rel_coordinates
= false;
158 bool has_mt_coordinates
= false;
159 size_t num_joystick_axes
= 0;
160 size_t num_joystick_buttons
= 0;
161 bool has_pad_buttons
= false;
162 bool is_direct
= false;
163 bool has_touch
= false;
164 bool has_3d_coordinates
= false;
165 bool has_keys
= false;
166 bool has_stylus
= false;
167 bool has_pen
= false;
168 bool finger_but_no_pen
= false;
169 bool has_mouse_button
= false;
170 bool is_mouse
= false;
171 bool is_abs_mouse
= false;
172 bool is_touchpad
= false;
173 bool is_touchscreen
= false;
174 bool is_tablet
= false;
175 bool is_tablet_pad
= false;
176 bool is_joystick
= false;
177 bool is_accelerometer
= false;
178 bool is_pointing_stick
= false;
180 has_keys
= test_bit(EV_KEY
, bitmask_ev
);
181 has_abs_coordinates
= test_bit(ABS_X
, bitmask_abs
) && test_bit(ABS_Y
, bitmask_abs
);
182 has_3d_coordinates
= has_abs_coordinates
&& test_bit(ABS_Z
, bitmask_abs
);
183 is_accelerometer
= test_bit(INPUT_PROP_ACCELEROMETER
, bitmask_props
);
185 if (!has_keys
&& has_3d_coordinates
)
186 is_accelerometer
= true;
188 if (is_accelerometer
) {
189 udev_builtin_add_property(dev
, test
, "ID_INPUT_ACCELEROMETER", "1");
193 is_pointing_stick
= test_bit(INPUT_PROP_POINTING_STICK
, bitmask_props
);
194 has_stylus
= test_bit(BTN_STYLUS
, bitmask_key
);
195 has_pen
= test_bit(BTN_TOOL_PEN
, bitmask_key
);
196 finger_but_no_pen
= test_bit(BTN_TOOL_FINGER
, bitmask_key
) && !test_bit(BTN_TOOL_PEN
, bitmask_key
);
197 for (int button
= BTN_MOUSE
; button
< BTN_JOYSTICK
&& !has_mouse_button
; button
++)
198 has_mouse_button
= test_bit(button
, bitmask_key
);
199 has_rel_coordinates
= test_bit(EV_REL
, bitmask_ev
) && test_bit(REL_X
, bitmask_rel
) && test_bit(REL_Y
, bitmask_rel
);
200 has_mt_coordinates
= test_bit(ABS_MT_POSITION_X
, bitmask_abs
) && test_bit(ABS_MT_POSITION_Y
, bitmask_abs
);
202 /* unset has_mt_coordinates if devices claims to have all abs axis */
203 if (has_mt_coordinates
&& test_bit(ABS_MT_SLOT
, bitmask_abs
) && test_bit(ABS_MT_SLOT
- 1, bitmask_abs
))
204 has_mt_coordinates
= false;
205 is_direct
= test_bit(INPUT_PROP_DIRECT
, bitmask_props
);
206 has_touch
= test_bit(BTN_TOUCH
, bitmask_key
);
207 has_pad_buttons
= test_bit(BTN_0
, bitmask_key
) && has_stylus
&& !has_pen
;
209 /* joysticks don't necessarily have buttons; e. g.
210 * rudders/pedals are joystick-like, but buttonless; they have
211 * other fancy axes. Others have buttons only but no axes.
213 * The BTN_JOYSTICK range starts after the mouse range, so a mouse
214 * with more than 16 buttons runs into the joystick range (e.g. Mad
215 * Catz Mad Catz M.M.O.TE). Skip those.
217 if (!test_bit(BTN_JOYSTICK
- 1, bitmask_key
)) {
218 for (int button
= BTN_JOYSTICK
; button
< BTN_DIGI
; button
++)
219 if (test_bit(button
, bitmask_key
))
220 num_joystick_buttons
++;
221 for (int button
= BTN_TRIGGER_HAPPY1
; button
<= BTN_TRIGGER_HAPPY40
; button
++)
222 if (test_bit(button
, bitmask_key
))
223 num_joystick_buttons
++;
224 for (int button
= BTN_DPAD_UP
; button
<= BTN_DPAD_RIGHT
; button
++)
225 if (test_bit(button
, bitmask_key
))
226 num_joystick_buttons
++;
228 for (int axis
= ABS_RX
; axis
< ABS_PRESSURE
; axis
++)
229 if (test_bit(axis
, bitmask_abs
))
232 if (has_abs_coordinates
) {
233 if (has_stylus
|| has_pen
)
235 else if (finger_but_no_pen
&& !is_direct
)
237 else if (has_mouse_button
)
238 /* This path is taken by VMware's USB mouse, which has
239 * absolute axes, but no touch/pressure button. */
241 else if (has_touch
|| is_direct
)
242 is_touchscreen
= true;
243 else if (num_joystick_buttons
> 0 || num_joystick_axes
> 0)
245 } else if (num_joystick_buttons
> 0 || num_joystick_axes
> 0)
248 if (has_mt_coordinates
) {
249 if (has_stylus
|| has_pen
)
251 else if (finger_but_no_pen
&& !is_direct
)
253 else if (has_touch
|| is_direct
)
254 is_touchscreen
= true;
257 if (is_tablet
&& has_pad_buttons
)
258 is_tablet_pad
= true;
260 if (!is_tablet
&& !is_touchpad
&& !is_joystick
&&
262 (has_rel_coordinates
||
263 !has_abs_coordinates
)) /* mouse buttons and no axis */
266 /* There is no such thing as an i2c mouse */
267 if (is_mouse
&& id
->bustype
== BUS_I2C
)
268 is_pointing_stick
= true;
270 /* Joystick un-detection. Some keyboards have random joystick buttons
271 * set. Avoid those being labeled as ID_INPUT_JOYSTICK with some heuristics.
272 * The well-known keys represent a (randomly picked) set of key groups.
273 * A joystick may have one of those but probably not several. And a joystick with less than 2 buttons
274 * or axes is not a joystick either.
275 * libinput uses similar heuristics, any changes here should be added to libinput too.
278 static const unsigned int well_known_keyboard_keys
[] = {
279 KEY_LEFTCTRL
, KEY_CAPSLOCK
, KEY_NUMLOCK
, KEY_INSERT
,
280 KEY_MUTE
, KEY_CALC
, KEY_FILE
, KEY_MAIL
, KEY_PLAYPAUSE
,
283 size_t num_well_known_keys
= 0;
286 for (size_t i
= 0; i
< ELEMENTSOF(well_known_keyboard_keys
); i
++)
287 if (test_bit(well_known_keyboard_keys
[i
], bitmask_key
))
288 num_well_known_keys
++;
290 if (num_well_known_keys
>= 4 || num_joystick_buttons
+ num_joystick_axes
< 2) {
291 log_device_debug(dev
, "Input device has %zd joystick buttons and %zd axes but also %zd keyboard key sets, "
292 "assuming this is a keyboard, not a joystick.",
293 num_joystick_buttons
, num_joystick_axes
, num_well_known_keys
);
298 if (is_pointing_stick
)
299 udev_builtin_add_property(dev
, test
, "ID_INPUT_POINTINGSTICK", "1");
300 if (is_mouse
|| is_abs_mouse
)
301 udev_builtin_add_property(dev
, test
, "ID_INPUT_MOUSE", "1");
303 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHPAD", "1");
305 udev_builtin_add_property(dev
, test
, "ID_INPUT_TOUCHSCREEN", "1");
307 udev_builtin_add_property(dev
, test
, "ID_INPUT_JOYSTICK", "1");
309 udev_builtin_add_property(dev
, test
, "ID_INPUT_TABLET", "1");
311 udev_builtin_add_property(dev
, test
, "ID_INPUT_TABLET_PAD", "1");
313 return is_tablet
|| is_mouse
|| is_abs_mouse
|| is_touchpad
|| is_touchscreen
|| is_joystick
|| is_pointing_stick
;
316 /* key like devices */
317 static bool test_key(sd_device
*dev
,
318 const unsigned long* bitmask_ev
,
319 const unsigned long* bitmask_key
,
324 /* do we have any KEY_* capability? */
325 if (!test_bit(EV_KEY
, bitmask_ev
)) {
326 log_device_debug(dev
, "test_key: no EV_KEY capability");
330 /* only consider KEY_* here, not BTN_* */
331 for (size_t i
= 0; i
< BTN_MISC
/BITS_PER_LONG
&& !found
; i
++) {
335 log_device_debug(dev
, "test_key: checking bit block %zu for any keys; found=%s",
336 i
* BITS_PER_LONG
, yes_no(found
));
338 /* If there are no keys in the lower block, check the higher blocks */
339 for (size_t block
= 0; block
< sizeof(high_key_blocks
) / sizeof(struct range
) && !found
; block
++)
340 for (unsigned i
= high_key_blocks
[block
].start
; i
< high_key_blocks
[block
].end
&& !found
; i
++)
341 if (test_bit(i
, bitmask_key
)) {
342 log_device_debug(dev
, "test_key: Found key %x in high block", i
);
347 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
349 /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
350 * those, consider it a full keyboard; do not test KEY_RESERVED, though */
351 if (FLAGS_SET(bitmask_key
[0], 0xFFFFFFFE)) {
352 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEYBOARD", "1");
359 static int builtin_input_id(sd_device
*dev
, sd_netlink
**rtnl
, int argc
, char *argv
[], bool test
) {
361 unsigned long bitmask_ev
[NBITS(EV_MAX
)];
362 unsigned long bitmask_abs
[NBITS(ABS_MAX
)];
363 unsigned long bitmask_key
[NBITS(KEY_MAX
)];
364 unsigned long bitmask_rel
[NBITS(REL_MAX
)];
365 unsigned long bitmask_props
[NBITS(INPUT_PROP_MAX
)];
372 /* walk up the parental chain until we find the real input device; the
373 * argument is very likely a subdevice of this, like eventN */
374 for (pdev
= dev
; pdev
; ) {
377 if (sd_device_get_sysattr_value(pdev
, "capabilities/ev", &s
) >= 0)
380 if (sd_device_get_parent_with_subsystem_devtype(pdev
, "input", NULL
, &pdev
) >= 0)
388 struct input_id id
= get_input_id(pdev
);
390 /* Use this as a flag that input devices were detected, so that this
391 * program doesn't need to be called more than once per device */
392 udev_builtin_add_property(dev
, test
, "ID_INPUT", "1");
393 get_cap_mask(pdev
, "capabilities/ev", bitmask_ev
, sizeof(bitmask_ev
), test
);
394 get_cap_mask(pdev
, "capabilities/abs", bitmask_abs
, sizeof(bitmask_abs
), test
);
395 get_cap_mask(pdev
, "capabilities/rel", bitmask_rel
, sizeof(bitmask_rel
), test
);
396 get_cap_mask(pdev
, "capabilities/key", bitmask_key
, sizeof(bitmask_key
), test
);
397 get_cap_mask(pdev
, "properties", bitmask_props
, sizeof(bitmask_props
), test
);
398 is_pointer
= test_pointers(dev
, &id
, bitmask_ev
, bitmask_abs
,
399 bitmask_key
, bitmask_rel
,
400 bitmask_props
, test
);
401 is_key
= test_key(dev
, bitmask_ev
, bitmask_key
, test
);
402 /* Some evdev nodes have only a scrollwheel */
403 if (!is_pointer
&& !is_key
&& test_bit(EV_REL
, bitmask_ev
) &&
404 (test_bit(REL_WHEEL
, bitmask_rel
) || test_bit(REL_HWHEEL
, bitmask_rel
)))
405 udev_builtin_add_property(dev
, test
, "ID_INPUT_KEY", "1");
406 if (test_bit(EV_SW
, bitmask_ev
))
407 udev_builtin_add_property(dev
, test
, "ID_INPUT_SWITCH", "1");
411 if (sd_device_get_sysname(dev
, &sysname
) >= 0 &&
412 startswith(sysname
, "event"))
413 extract_info(dev
, test
);
418 const UdevBuiltin udev_builtin_input_id
= {
420 .cmd
= builtin_input_id
,
421 .help
= "Input device properties",