]>
Commit | Line | Data |
---|---|---|
e7145211 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
d7867b31 | 2 | /* |
24447733 | 3 | * expose input properties via udev |
d7867b31 | 4 | * |
810adae9 | 5 | * Portions Copyright © 2004 David Zeuthen, <david@fubar.dk> |
810adae9 | 6 | * Copyright © 2014 Carlos Garnacho <carlosg@gnome.org> |
d7867b31 KS |
7 | */ |
8 | ||
07630cea | 9 | #include <errno.h> |
f5947a5e | 10 | #include <fcntl.h> |
07630cea | 11 | #include <stdarg.h> |
d7867b31 | 12 | #include <stdlib.h> |
07630cea | 13 | #include <unistd.h> |
d7867b31 | 14 | #include <linux/limits.h> |
d7867b31 | 15 | |
d9de38ca | 16 | #include "device-util.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
f5947a5e | 18 | #include "missing_input.h" |
3d7ac1c6 | 19 | #include "parse-util.h" |
d054f0a4 | 20 | #include "stdio-util.h" |
07630cea | 21 | #include "string-util.h" |
07a26e42 | 22 | #include "udev-builtin.h" |
24447733 | 23 | #include "util.h" |
d7867b31 KS |
24 | |
25 | /* we must use this kernel-compatible implementation */ | |
26 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) | |
27 | #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) | |
28 | #define OFF(x) ((x)%BITS_PER_LONG) | |
29 | #define BIT(x) (1UL<<OFF(x)) | |
30 | #define LONG(x) ((x)/BITS_PER_LONG) | |
31 | #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1) | |
32 | ||
f472d466 NL |
33 | struct range { |
34 | unsigned start; | |
35 | unsigned end; | |
36 | }; | |
37 | ||
38 | /* key code ranges above BTN_MISC (start is inclusive, stop is exclusive)*/ | |
39 | static const struct range high_key_blocks[] = { | |
40 | { KEY_OK, BTN_DPAD_UP }, | |
41 | { KEY_ALS_TOGGLE, BTN_TRIGGER_HAPPY } | |
42 | }; | |
43 | ||
a1e92eee | 44 | static int abs_size_mm(const struct input_absinfo *absinfo) { |
24447733 DH |
45 | /* Resolution is defined to be in units/mm for ABS_X/Y */ |
46 | return (absinfo->maximum - absinfo->minimum) / absinfo->resolution; | |
47 | } | |
48 | ||
ff799927 | 49 | static void extract_info(sd_device *dev, const char *devpath, bool test) { |
24447733 DH |
50 | char width[DECIMAL_STR_MAX(int)], height[DECIMAL_STR_MAX(int)]; |
51 | struct input_absinfo xabsinfo = {}, yabsinfo = {}; | |
52 | _cleanup_close_ int fd = -1; | |
53 | ||
54 | fd = open(devpath, O_RDONLY|O_CLOEXEC); | |
55 | if (fd < 0) | |
56 | return; | |
57 | ||
58 | if (ioctl(fd, EVIOCGABS(ABS_X), &xabsinfo) < 0 || | |
59 | ioctl(fd, EVIOCGABS(ABS_Y), &yabsinfo) < 0) | |
60 | return; | |
61 | ||
62 | if (xabsinfo.resolution <= 0 || yabsinfo.resolution <= 0) | |
63 | return; | |
64 | ||
d054f0a4 DM |
65 | xsprintf(width, "%d", abs_size_mm(&xabsinfo)); |
66 | xsprintf(height, "%d", abs_size_mm(&yabsinfo)); | |
24447733 | 67 | |
ff799927 YW |
68 | udev_builtin_add_property(dev, test, "ID_INPUT_WIDTH_MM", width); |
69 | udev_builtin_add_property(dev, test, "ID_INPUT_HEIGHT_MM", height); | |
24447733 DH |
70 | } |
71 | ||
d7867b31 KS |
72 | /* |
73 | * Read a capability attribute and return bitmask. | |
ff799927 | 74 | * @param dev sd_device |
d7867b31 KS |
75 | * @param attr sysfs attribute name (e. g. "capabilities/key") |
76 | * @param bitmask: Output array which has a sizeof of bitmask_size | |
77 | */ | |
d9de38ca | 78 | static void get_cap_mask(sd_device *pdev, const char* attr, |
912541b0 | 79 | unsigned long *bitmask, size_t bitmask_size, |
9ec6e95b | 80 | bool test) { |
975a9007 | 81 | const char *v; |
912541b0 KS |
82 | char text[4096]; |
83 | unsigned i; | |
84 | char* word; | |
85 | unsigned long val; | |
86 | ||
ff799927 YW |
87 | if (sd_device_get_sysattr_value(pdev, attr, &v) < 0) |
88 | v = ""; | |
975a9007 | 89 | |
d054f0a4 | 90 | xsprintf(text, "%s", v); |
d9de38ca | 91 | log_device_debug(pdev, "%s raw kernel attribute: %s", attr, text); |
912541b0 | 92 | |
29804cc1 | 93 | memzero(bitmask, bitmask_size); |
912541b0 KS |
94 | i = 0; |
95 | while ((word = strrchr(text, ' ')) != NULL) { | |
d9de38ca YW |
96 | val = strtoul(word+1, NULL, 16); |
97 | if (i < bitmask_size / sizeof(unsigned long)) | |
912541b0 KS |
98 | bitmask[i] = val; |
99 | else | |
d9de38ca | 100 | log_device_debug(pdev, "Ignoring %s block %lX which is larger than maximum size", attr, val); |
912541b0 KS |
101 | *word = '\0'; |
102 | ++i; | |
103 | } | |
104 | val = strtoul (text, NULL, 16); | |
105 | if (i < bitmask_size / sizeof(unsigned long)) | |
106 | bitmask[i] = val; | |
107 | else | |
d9de38ca | 108 | log_device_debug(pdev, "Ignoring %s block %lX which is larger than maximum size", attr, val); |
912541b0 KS |
109 | |
110 | if (test) { | |
111 | /* printf pattern with the right unsigned long number of hex chars */ | |
d054f0a4 DM |
112 | xsprintf(text, " bit %%4u: %%0%zulX\n", |
113 | 2 * sizeof(unsigned long)); | |
d9de38ca | 114 | log_device_debug(pdev, "%s decoded bit map:", attr); |
912541b0 KS |
115 | val = bitmask_size / sizeof (unsigned long); |
116 | /* skip over leading zeros */ | |
117 | while (bitmask[val-1] == 0 && val > 0) | |
118 | --val; | |
bcfce235 LP |
119 | for (i = 0; i < val; ++i) { |
120 | DISABLE_WARNING_FORMAT_NONLITERAL; | |
d9de38ca | 121 | log_device_debug(pdev, text, i * BITS_PER_LONG, bitmask[i]); |
bcfce235 LP |
122 | REENABLE_WARNING; |
123 | } | |
912541b0 | 124 | } |
d7867b31 KS |
125 | } |
126 | ||
3d7ac1c6 PH |
127 | static struct input_id get_input_id(sd_device *dev) { |
128 | const char *v; | |
129 | struct input_id id = {}; | |
130 | ||
131 | if (sd_device_get_sysattr_value(dev, "id/bustype", &v) >= 0) | |
132 | (void) safe_atoux16(v, &id.bustype); | |
133 | if (sd_device_get_sysattr_value(dev, "id/vendor", &v) >= 0) | |
134 | (void) safe_atoux16(v, &id.vendor); | |
135 | if (sd_device_get_sysattr_value(dev, "id/product", &v) >= 0) | |
136 | (void) safe_atoux16(v, &id.product); | |
137 | if (sd_device_get_sysattr_value(dev, "id/version", &v) >= 0) | |
138 | (void) safe_atoux16(v, &id.version); | |
139 | ||
140 | return id; | |
141 | } | |
142 | ||
d7867b31 | 143 | /* pointer devices */ |
ff799927 | 144 | static bool test_pointers(sd_device *dev, |
3d7ac1c6 | 145 | const struct input_id *id, |
b914418a PH |
146 | const unsigned long* bitmask_ev, |
147 | const unsigned long* bitmask_abs, | |
148 | const unsigned long* bitmask_key, | |
149 | const unsigned long* bitmask_rel, | |
150 | const unsigned long* bitmask_props, | |
151 | bool test) { | |
22811ad0 | 152 | int button, axis; |
15264e5a AP |
153 | bool has_abs_coordinates = false; |
154 | bool has_rel_coordinates = false; | |
fa5a113d | 155 | bool has_mt_coordinates = false; |
15264e5a | 156 | bool has_joystick_axes_or_buttons = false; |
fa5a113d | 157 | bool is_direct = false; |
15264e5a AP |
158 | bool has_touch = false; |
159 | bool has_3d_coordinates = false; | |
160 | bool has_keys = false; | |
161 | bool stylus_or_pen = false; | |
162 | bool finger_but_no_pen = false; | |
163 | bool has_mouse_button = false; | |
164 | bool is_mouse = false; | |
165 | bool is_touchpad = false; | |
166 | bool is_touchscreen = false; | |
167 | bool is_tablet = false; | |
168 | bool is_joystick = false; | |
169 | bool is_accelerometer = false; | |
3d7ac1c6 | 170 | bool is_pointing_stick = false; |
15264e5a AP |
171 | |
172 | has_keys = test_bit(EV_KEY, bitmask_ev); | |
173 | has_abs_coordinates = test_bit(ABS_X, bitmask_abs) && test_bit(ABS_Y, bitmask_abs); | |
174 | has_3d_coordinates = has_abs_coordinates && test_bit(ABS_Z, bitmask_abs); | |
175 | is_accelerometer = test_bit(INPUT_PROP_ACCELEROMETER, bitmask_props); | |
176 | ||
177 | if (!has_keys && has_3d_coordinates) | |
178 | is_accelerometer = true; | |
179 | ||
180 | if (is_accelerometer) { | |
ff799927 | 181 | udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1"); |
941a2aca | 182 | return true; |
bd1acc9f HG |
183 | } |
184 | ||
15264e5a AP |
185 | is_pointing_stick = test_bit(INPUT_PROP_POINTING_STICK, bitmask_props); |
186 | stylus_or_pen = test_bit(BTN_STYLUS, bitmask_key) || test_bit(BTN_TOOL_PEN, bitmask_key); | |
187 | finger_but_no_pen = test_bit(BTN_TOOL_FINGER, bitmask_key) && !test_bit(BTN_TOOL_PEN, bitmask_key); | |
22811ad0 MS |
188 | for (button = BTN_MOUSE; button < BTN_JOYSTICK && !has_mouse_button; button++) |
189 | has_mouse_button = test_bit(button, bitmask_key); | |
15264e5a | 190 | has_rel_coordinates = test_bit(EV_REL, bitmask_ev) && test_bit(REL_X, bitmask_rel) && test_bit(REL_Y, bitmask_rel); |
fa5a113d | 191 | has_mt_coordinates = test_bit(ABS_MT_POSITION_X, bitmask_abs) && test_bit(ABS_MT_POSITION_Y, bitmask_abs); |
495968cf AP |
192 | |
193 | /* unset has_mt_coordinates if devices claims to have all abs axis */ | |
9ed794a3 | 194 | if (has_mt_coordinates && test_bit(ABS_MT_SLOT, bitmask_abs) && test_bit(ABS_MT_SLOT - 1, bitmask_abs)) |
495968cf | 195 | has_mt_coordinates = false; |
fa5a113d | 196 | is_direct = test_bit(INPUT_PROP_DIRECT, bitmask_props); |
15264e5a | 197 | has_touch = test_bit(BTN_TOUCH, bitmask_key); |
0cd6767c | 198 | |
15264e5a AP |
199 | /* joysticks don't necessarily have buttons; e. g. |
200 | * rudders/pedals are joystick-like, but buttonless; they have | |
0cd6767c PH |
201 | * other fancy axes. Others have buttons only but no axes. |
202 | * | |
203 | * The BTN_JOYSTICK range starts after the mouse range, so a mouse | |
204 | * with more than 16 buttons runs into the joystick range (e.g. Mad | |
205 | * Catz Mad Catz M.M.O.TE). Skip those. | |
206 | */ | |
207 | if (!test_bit(BTN_JOYSTICK - 1, bitmask_key)) { | |
208 | for (button = BTN_JOYSTICK; button < BTN_DIGI && !has_joystick_axes_or_buttons; button++) | |
209 | has_joystick_axes_or_buttons = test_bit(button, bitmask_key); | |
210 | for (button = BTN_TRIGGER_HAPPY1; button <= BTN_TRIGGER_HAPPY40 && !has_joystick_axes_or_buttons; button++) | |
211 | has_joystick_axes_or_buttons = test_bit(button, bitmask_key); | |
212 | for (button = BTN_DPAD_UP; button <= BTN_DPAD_RIGHT && !has_joystick_axes_or_buttons; button++) | |
213 | has_joystick_axes_or_buttons = test_bit(button, bitmask_key); | |
214 | } | |
22811ad0 MS |
215 | for (axis = ABS_RX; axis < ABS_PRESSURE && !has_joystick_axes_or_buttons; axis++) |
216 | has_joystick_axes_or_buttons = test_bit(axis, bitmask_abs); | |
15264e5a AP |
217 | |
218 | if (has_abs_coordinates) { | |
219 | if (stylus_or_pen) | |
220 | is_tablet = true; | |
fa5a113d | 221 | else if (finger_but_no_pen && !is_direct) |
15264e5a AP |
222 | is_touchpad = true; |
223 | else if (has_mouse_button) | |
912541b0 KS |
224 | /* This path is taken by VMware's USB mouse, which has |
225 | * absolute axes, but no touch/pressure button. */ | |
15264e5a | 226 | is_mouse = true; |
1a3439ef | 227 | else if (has_touch || is_direct) |
15264e5a AP |
228 | is_touchscreen = true; |
229 | else if (has_joystick_axes_or_buttons) | |
230 | is_joystick = true; | |
38cd55b0 | 231 | } else if (has_joystick_axes_or_buttons) |
2e856c63 | 232 | is_joystick = true; |
2e856c63 | 233 | |
d3a37494 AP |
234 | if (has_mt_coordinates) { |
235 | if (stylus_or_pen) | |
236 | is_tablet = true; | |
237 | else if (finger_but_no_pen && !is_direct) | |
238 | is_touchpad = true; | |
239 | else if (has_touch || is_direct) | |
240 | is_touchscreen = true; | |
241 | } | |
912541b0 | 242 | |
774ff9ba PH |
243 | if (!is_tablet && !is_touchpad && !is_joystick && |
244 | has_mouse_button && | |
0c21944e MS |
245 | (has_rel_coordinates || |
246 | !has_abs_coordinates)) /* mouse buttons and no axis */ | |
15264e5a | 247 | is_mouse = true; |
912541b0 | 248 | |
3d7ac1c6 PH |
249 | /* There is no such thing as an i2c mouse */ |
250 | if (is_mouse && id->bustype == BUS_I2C) | |
251 | is_pointing_stick = true; | |
252 | ||
15264e5a | 253 | if (is_pointing_stick) |
ff799927 | 254 | udev_builtin_add_property(dev, test, "ID_INPUT_POINTINGSTICK", "1"); |
15264e5a | 255 | if (is_mouse) |
ff799927 | 256 | udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1"); |
15264e5a | 257 | if (is_touchpad) |
ff799927 | 258 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1"); |
15264e5a | 259 | if (is_touchscreen) |
ff799927 | 260 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1"); |
15264e5a | 261 | if (is_joystick) |
ff799927 | 262 | udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1"); |
15264e5a | 263 | if (is_tablet) |
ff799927 | 264 | udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1"); |
15264e5a AP |
265 | |
266 | return is_tablet || is_mouse || is_touchpad || is_touchscreen || is_joystick || is_pointing_stick; | |
d7867b31 KS |
267 | } |
268 | ||
269 | /* key like devices */ | |
ff799927 | 270 | static bool test_key(sd_device *dev, |
b914418a PH |
271 | const unsigned long* bitmask_ev, |
272 | const unsigned long* bitmask_key, | |
273 | bool test) { | |
912541b0 KS |
274 | unsigned i; |
275 | unsigned long found; | |
276 | unsigned long mask; | |
941a2aca | 277 | bool ret = false; |
912541b0 KS |
278 | |
279 | /* do we have any KEY_* capability? */ | |
b914418a | 280 | if (!test_bit(EV_KEY, bitmask_ev)) { |
d9de38ca | 281 | log_device_debug(dev, "test_key: no EV_KEY capability"); |
941a2aca | 282 | return false; |
912541b0 KS |
283 | } |
284 | ||
285 | /* only consider KEY_* here, not BTN_* */ | |
286 | found = 0; | |
287 | for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) { | |
288 | found |= bitmask_key[i]; | |
d9de38ca | 289 | log_device_debug(dev, "test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0); |
912541b0 | 290 | } |
f472d466 | 291 | /* If there are no keys in the lower block, check the higher blocks */ |
912541b0 | 292 | if (!found) { |
f472d466 NL |
293 | unsigned block; |
294 | for (block = 0; block < (sizeof(high_key_blocks) / sizeof(struct range)); ++block) { | |
295 | for (i = high_key_blocks[block].start; i < high_key_blocks[block].end; ++i) { | |
296 | if (test_bit(i, bitmask_key)) { | |
d9de38ca | 297 | log_device_debug(dev, "test_key: Found key %x in high block", i); |
f472d466 NL |
298 | found = 1; |
299 | break; | |
300 | } | |
912541b0 KS |
301 | } |
302 | } | |
303 | } | |
304 | ||
941a2aca | 305 | if (found > 0) { |
ff799927 | 306 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); |
941a2aca HG |
307 | ret = true; |
308 | } | |
912541b0 KS |
309 | |
310 | /* the first 32 bits are ESC, numbers, and Q to D; if we have all of | |
311 | * those, consider it a full keyboard; do not test KEY_RESERVED, though */ | |
312 | mask = 0xFFFFFFFE; | |
d94a24ca | 313 | if (FLAGS_SET(bitmask_key[0], mask)) { |
ff799927 | 314 | udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1"); |
941a2aca HG |
315 | ret = true; |
316 | } | |
317 | ||
318 | return ret; | |
d7867b31 KS |
319 | } |
320 | ||
3fc2e9a2 YW |
321 | static int builtin_input_id(sd_device *dev, int argc, char *argv[], bool test) { |
322 | sd_device *pdev; | |
912541b0 KS |
323 | unsigned long bitmask_ev[NBITS(EV_MAX)]; |
324 | unsigned long bitmask_abs[NBITS(ABS_MAX)]; | |
325 | unsigned long bitmask_key[NBITS(KEY_MAX)]; | |
326 | unsigned long bitmask_rel[NBITS(REL_MAX)]; | |
606df97b | 327 | unsigned long bitmask_props[NBITS(INPUT_PROP_MAX)]; |
24447733 | 328 | const char *sysname, *devnode; |
37186823 HG |
329 | bool is_pointer; |
330 | bool is_key; | |
912541b0 | 331 | |
3b64e4d4 TG |
332 | assert(dev); |
333 | ||
912541b0 KS |
334 | /* walk up the parental chain until we find the real input device; the |
335 | * argument is very likely a subdevice of this, like eventN */ | |
ff799927 YW |
336 | for (pdev = dev; pdev; ) { |
337 | const char *s; | |
338 | ||
339 | if (sd_device_get_sysattr_value(pdev, "capabilities/ev", &s) >= 0) | |
340 | break; | |
341 | ||
342 | if (sd_device_get_parent_with_subsystem_devtype(pdev, "input", NULL, &pdev) >= 0) | |
343 | continue; | |
344 | ||
345 | pdev = NULL; | |
346 | break; | |
347 | } | |
912541b0 | 348 | |
24447733 | 349 | if (pdev) { |
3d7ac1c6 PH |
350 | struct input_id id = get_input_id(pdev); |
351 | ||
24447733 DH |
352 | /* Use this as a flag that input devices were detected, so that this |
353 | * program doesn't need to be called more than once per device */ | |
ff799927 | 354 | udev_builtin_add_property(dev, test, "ID_INPUT", "1"); |
d9de38ca YW |
355 | get_cap_mask(pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test); |
356 | get_cap_mask(pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test); | |
357 | get_cap_mask(pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test); | |
358 | get_cap_mask(pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test); | |
359 | get_cap_mask(pdev, "properties", bitmask_props, sizeof(bitmask_props), test); | |
3d7ac1c6 | 360 | is_pointer = test_pointers(dev, &id, bitmask_ev, bitmask_abs, |
37186823 HG |
361 | bitmask_key, bitmask_rel, |
362 | bitmask_props, test); | |
363 | is_key = test_key(dev, bitmask_ev, bitmask_key, test); | |
364 | /* Some evdev nodes have only a scrollwheel */ | |
365 | if (!is_pointer && !is_key && test_bit(EV_REL, bitmask_ev) && | |
366 | (test_bit(REL_WHEEL, bitmask_rel) || test_bit(REL_HWHEEL, bitmask_rel))) | |
ff799927 | 367 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); |
64083a60 | 368 | if (test_bit(EV_SW, bitmask_ev)) |
ff799927 | 369 | udev_builtin_add_property(dev, test, "ID_INPUT_SWITCH", "1"); |
64083a60 | 370 | |
24447733 DH |
371 | } |
372 | ||
ff799927 YW |
373 | if (sd_device_get_devname(dev, &devnode) >= 0 && |
374 | sd_device_get_sysname(dev, &sysname) >= 0 && | |
375 | startswith(sysname, "event")) | |
24447733 DH |
376 | extract_info(dev, devnode, test); |
377 | ||
d354690e | 378 | return 0; |
d7867b31 KS |
379 | } |
380 | ||
25de7aa7 | 381 | const UdevBuiltin udev_builtin_input_id = { |
912541b0 KS |
382 | .name = "input_id", |
383 | .cmd = builtin_input_id, | |
5ac0162c | 384 | .help = "Input device properties", |
d7867b31 | 385 | }; |