]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-input_id.c
Merge pull request #22759 from msekletar/issue-18077-long-sysfs-paths-hashing
[thirdparty/systemd.git] / src / udev / udev-builtin-input_id.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * expose input properties via udev
4 *
5 * Portions Copyright © 2004 David Zeuthen, <david@fubar.dk>
6 * Copyright © 2014 Carlos Garnacho <carlosg@gnome.org>
7 */
8
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stdarg.h>
12 #include <unistd.h>
13 #include <linux/limits.h>
14
15 #include "device-util.h"
16 #include "fd-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"
22 #include "util.h"
23
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)
31
32 struct range {
33 unsigned start;
34 unsigned end;
35 };
36
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 }
41 };
42
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;
46 }
47
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;
52
53 fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
54 if (fd < 0)
55 return;
56
57 if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 ||
58 ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0)
59 return;
60
61 if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0)
62 return;
63
64 xsprintf(width, "%d", abs_size_mm(&xabsinfo));
65 xsprintf(height, "%d", abs_size_mm(&yabsinfo));
66
67 udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width);
68 udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height);
69 }
70
71 /*
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
76 */
77 static void get_cap_mask(sd_device *pdev, const char* attr,
78 unsigned long *bitmask, size_t bitmask_size,
79 bool test) {
80 const char *v;
81 char text[4096];
82 unsigned i;
83 char* word;
84 unsigned long val;
85 int r;
86
87 if (sd_device_get_sysattr_value(pdev, attr, &v) < 0)
88 v = "";
89
90 xsprintf(text, "%s", v);
91 log_device_debug(pdev, "%s raw kernel attribute: %s", attr, text);
92
93 memzero(bitmask, bitmask_size);
94 i = 0;
95 while ((word = strrchr(text, ' '))) {
96 r = safe_atolu_full(word+1, 16, &val);
97 if (r < 0)
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))
100 bitmask[i] = val;
101 else
102 log_device_debug(pdev, "Ignoring %s block %lX which is larger than maximum size", attr, val);
103 *word = '\0';
104 i++;
105 }
106 r = safe_atolu_full(text, 16, &val);
107 if (r < 0)
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))
110 bitmask[i] = val;
111 else
112 log_device_debug(pdev, "Ignoring %s block %lX which is larger than maximum size", attr, val);
113
114 if (test && DEBUG_LOGGING) {
115 log_device_debug(pdev, "%s decoded bit map:", attr);
116
117 val = bitmask_size / sizeof (unsigned long);
118 /* skip trailing zeros */
119 while (bitmask[val-1] == 0 && val > 0)
120 --val;
121
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]);
128 }
129 }
130
131 static struct input_id get_input_id(sd_device *dev) {
132 const char *v;
133 struct input_id id = {};
134
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);
143
144 return id;
145 }
146
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,
155 bool test) {
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;
179
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);
184
185 if (!has_keys && has_3d_coordinates)
186 is_accelerometer = true;
187
188 if (is_accelerometer) {
189 udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1");
190 return true;
191 }
192
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);
201
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;
208
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.
212 *
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.
216 */
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++;
227 }
228 for (int axis = ABS_RX; axis < ABS_PRESSURE; axis++)
229 if (test_bit(axis, bitmask_abs))
230 num_joystick_axes++;
231
232 if (has_abs_coordinates) {
233 if (has_stylus || has_pen)
234 is_tablet = true;
235 else if (finger_but_no_pen && !is_direct)
236 is_touchpad = true;
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. */
240 is_abs_mouse = true;
241 else if (has_touch || is_direct)
242 is_touchscreen = true;
243 else if (num_joystick_buttons > 0 || num_joystick_axes > 0)
244 is_joystick = true;
245 } else if (num_joystick_buttons > 0 || num_joystick_axes > 0)
246 is_joystick = true;
247
248 if (has_mt_coordinates) {
249 if (has_stylus || has_pen)
250 is_tablet = true;
251 else if (finger_but_no_pen && !is_direct)
252 is_touchpad = true;
253 else if (has_touch || is_direct)
254 is_touchscreen = true;
255 }
256
257 if (is_tablet && has_pad_buttons)
258 is_tablet_pad = true;
259
260 if (!is_tablet && !is_touchpad && !is_joystick &&
261 has_mouse_button &&
262 (has_rel_coordinates ||
263 !has_abs_coordinates)) /* mouse buttons and no axis */
264 is_mouse = true;
265
266 /* There is no such thing as an i2c mouse */
267 if (is_mouse && id->bustype == BUS_I2C)
268 is_pointing_stick = true;
269
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.
276 */
277 if (is_joystick) {
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,
281 KEY_BRIGHTNESSDOWN,
282 };
283 size_t num_well_known_keys = 0;
284
285 if (has_keys)
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++;
289
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);
294 is_joystick = false;
295 }
296 }
297
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");
302 if (is_touchpad)
303 udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1");
304 if (is_touchscreen)
305 udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1");
306 if (is_joystick)
307 udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1");
308 if (is_tablet)
309 udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1");
310 if (is_tablet_pad)
311 udev_builtin_add_property(dev, test, "ID_INPUT_TABLET_PAD", "1");
312
313 return is_tablet || is_mouse || is_abs_mouse || is_touchpad || is_touchscreen || is_joystick || is_pointing_stick;
314 }
315
316 /* key like devices */
317 static bool test_key(sd_device *dev,
318 const unsigned long* bitmask_ev,
319 const unsigned long* bitmask_key,
320 bool test) {
321
322 bool found = false;
323
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");
327 return false;
328 }
329
330 /* only consider KEY_* here, not BTN_* */
331 for (size_t i = 0; i < BTN_MISC/BITS_PER_LONG && !found; i++) {
332 if (bitmask_key[i])
333 found = true;
334
335 log_device_debug(dev, "test_key: checking bit block %zu for any keys; found=%s",
336 i * BITS_PER_LONG, yes_no(found));
337 }
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);
343 found = true;
344 }
345
346 if (found)
347 udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1");
348
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");
353 return true;
354 }
355
356 return found;
357 }
358
359 static int builtin_input_id(sd_device *dev, sd_netlink **rtnl, int argc, char *argv[], bool test) {
360 sd_device *pdev;
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)];
366 const char *sysname;
367 bool is_pointer;
368 bool is_key;
369
370 assert(dev);
371
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; ) {
375 const char *s;
376
377 if (sd_device_get_sysattr_value(pdev, "capabilities/ev", &s) >= 0)
378 break;
379
380 if (sd_device_get_parent_with_subsystem_devtype(pdev, "input", NULL, &pdev) >= 0)
381 continue;
382
383 pdev = NULL;
384 break;
385 }
386
387 if (pdev) {
388 struct input_id id = get_input_id(pdev);
389
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");
408
409 }
410
411 if (sd_device_get_sysname(dev, &sysname) >= 0 &&
412 startswith(sysname, "event"))
413 extract_info(dev, test);
414
415 return 0;
416 }
417
418 const UdevBuiltin udev_builtin_input_id = {
419 .name = "input_id",
420 .cmd = builtin_input_id,
421 .help = "Input device properties",
422 };