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