]>
Commit | Line | Data |
---|---|---|
d7867b31 KS |
1 | /* |
2 | * compose persistent device path | |
3 | * | |
4 | * Copyright (C) 2009 Martin Pitt <martin.pitt@ubuntu.com> | |
5 | * Portions Copyright (C) 2004 David Zeuthen, <david@fubar.dk> | |
1298001e | 6 | * Copyright (C) 2011 Kay Sievers <kay@vrfy.org> |
d7867b31 KS |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <stdarg.h> | |
25 | #include <unistd.h> | |
26 | #include <string.h> | |
27 | #include <errno.h> | |
28 | #include <linux/limits.h> | |
29 | #include <linux/input.h> | |
30 | ||
31 | #include "udev.h" | |
32 | ||
33 | /* we must use this kernel-compatible implementation */ | |
34 | #define BITS_PER_LONG (sizeof(unsigned long) * 8) | |
35 | #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) | |
36 | #define OFF(x) ((x)%BITS_PER_LONG) | |
37 | #define BIT(x) (1UL<<OFF(x)) | |
38 | #define LONG(x) ((x)/BITS_PER_LONG) | |
39 | #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1) | |
40 | ||
04a9d3a0 KS |
41 | #pragma GCC diagnostic push |
42 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" | |
d7867b31 KS |
43 | /* |
44 | * Read a capability attribute and return bitmask. | |
45 | * @param dev udev_device | |
46 | * @param attr sysfs attribute name (e. g. "capabilities/key") | |
47 | * @param bitmask: Output array which has a sizeof of bitmask_size | |
48 | */ | |
49 | static void get_cap_mask(struct udev_device *dev, | |
912541b0 KS |
50 | struct udev_device *pdev, const char* attr, |
51 | unsigned long *bitmask, size_t bitmask_size, | |
52 | bool test) | |
d7867b31 | 53 | { |
912541b0 KS |
54 | char text[4096]; |
55 | unsigned i; | |
56 | char* word; | |
57 | unsigned long val; | |
58 | ||
59 | snprintf(text, sizeof(text), "%s", udev_device_get_sysattr_value(pdev, attr)); | |
9f6445e3 | 60 | log_debug("%s raw kernel attribute: %s", attr, text); |
912541b0 | 61 | |
29804cc1 | 62 | memzero(bitmask, bitmask_size); |
912541b0 KS |
63 | i = 0; |
64 | while ((word = strrchr(text, ' ')) != NULL) { | |
65 | val = strtoul (word+1, NULL, 16); | |
66 | if (i < bitmask_size/sizeof(unsigned long)) | |
67 | bitmask[i] = val; | |
68 | else | |
9f6445e3 | 69 | log_debug("ignoring %s block %lX which is larger than maximum size", attr, val); |
912541b0 KS |
70 | *word = '\0'; |
71 | ++i; | |
72 | } | |
73 | val = strtoul (text, NULL, 16); | |
74 | if (i < bitmask_size / sizeof(unsigned long)) | |
75 | bitmask[i] = val; | |
76 | else | |
9f6445e3 | 77 | log_debug("ignoring %s block %lX which is larger than maximum size", attr, val); |
912541b0 KS |
78 | |
79 | if (test) { | |
80 | /* printf pattern with the right unsigned long number of hex chars */ | |
81 | snprintf(text, sizeof(text), " bit %%4u: %%0%zilX\n", 2 * sizeof(unsigned long)); | |
9f6445e3 | 82 | log_debug("%s decoded bit map:", attr); |
912541b0 KS |
83 | val = bitmask_size / sizeof (unsigned long); |
84 | /* skip over leading zeros */ | |
85 | while (bitmask[val-1] == 0 && val > 0) | |
86 | --val; | |
87 | for (i = 0; i < val; ++i) | |
baa30fbc | 88 | log_debug(text, i * BITS_PER_LONG, bitmask[i]); |
912541b0 | 89 | } |
d7867b31 | 90 | } |
04a9d3a0 | 91 | #pragma GCC diagnostic pop |
d7867b31 KS |
92 | |
93 | /* pointer devices */ | |
94 | static void test_pointers (struct udev_device *dev, | |
912541b0 KS |
95 | const unsigned long* bitmask_ev, |
96 | const unsigned long* bitmask_abs, | |
97 | const unsigned long* bitmask_key, | |
98 | const unsigned long* bitmask_rel, | |
99 | bool test) | |
d7867b31 | 100 | { |
912541b0 KS |
101 | int is_mouse = 0; |
102 | int is_touchpad = 0; | |
103 | ||
104 | if (!test_bit (EV_KEY, bitmask_ev)) { | |
105 | if (test_bit (EV_ABS, bitmask_ev) && | |
106 | test_bit (ABS_X, bitmask_abs) && | |
107 | test_bit (ABS_Y, bitmask_abs) && | |
108 | test_bit (ABS_Z, bitmask_abs)) | |
109 | udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1"); | |
110 | return; | |
111 | } | |
112 | ||
113 | if (test_bit (EV_ABS, bitmask_ev) && | |
114 | test_bit (ABS_X, bitmask_abs) && test_bit (ABS_Y, bitmask_abs)) { | |
115 | if (test_bit (BTN_STYLUS, bitmask_key) || test_bit (BTN_TOOL_PEN, bitmask_key)) | |
116 | udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1"); | |
117 | else if (test_bit (BTN_TOOL_FINGER, bitmask_key) && !test_bit (BTN_TOOL_PEN, bitmask_key)) | |
118 | is_touchpad = 1; | |
119 | else if (test_bit (BTN_TRIGGER, bitmask_key) || | |
120 | test_bit (BTN_A, bitmask_key) || | |
121 | test_bit (BTN_1, bitmask_key)) | |
122 | udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1"); | |
123 | else if (test_bit (BTN_MOUSE, bitmask_key)) | |
124 | /* This path is taken by VMware's USB mouse, which has | |
125 | * absolute axes, but no touch/pressure button. */ | |
126 | is_mouse = 1; | |
127 | else if (test_bit (BTN_TOUCH, bitmask_key)) | |
128 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1"); | |
129 | } | |
130 | ||
131 | if (test_bit (EV_REL, bitmask_ev) && | |
132 | test_bit (REL_X, bitmask_rel) && test_bit (REL_Y, bitmask_rel) && | |
133 | test_bit (BTN_MOUSE, bitmask_key)) | |
134 | is_mouse = 1; | |
135 | ||
136 | if (is_mouse) | |
137 | udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1"); | |
138 | if (is_touchpad) | |
139 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1"); | |
d7867b31 KS |
140 | } |
141 | ||
142 | /* key like devices */ | |
143 | static void test_key (struct udev_device *dev, | |
912541b0 KS |
144 | const unsigned long* bitmask_ev, |
145 | const unsigned long* bitmask_key, | |
146 | bool test) | |
d7867b31 | 147 | { |
912541b0 KS |
148 | unsigned i; |
149 | unsigned long found; | |
150 | unsigned long mask; | |
151 | ||
152 | /* do we have any KEY_* capability? */ | |
153 | if (!test_bit (EV_KEY, bitmask_ev)) { | |
9f6445e3 | 154 | log_debug("test_key: no EV_KEY capability"); |
912541b0 KS |
155 | return; |
156 | } | |
157 | ||
158 | /* only consider KEY_* here, not BTN_* */ | |
159 | found = 0; | |
160 | for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) { | |
161 | found |= bitmask_key[i]; | |
9f6445e3 | 162 | log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0); |
912541b0 KS |
163 | } |
164 | /* If there are no keys in the lower block, check the higher block */ | |
165 | if (!found) { | |
166 | for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) { | |
167 | if (test_bit (i, bitmask_key)) { | |
9f6445e3 | 168 | log_debug("test_key: Found key %x in high block", i); |
912541b0 KS |
169 | found = 1; |
170 | break; | |
171 | } | |
172 | } | |
173 | } | |
174 | ||
175 | if (found > 0) | |
176 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); | |
177 | ||
178 | /* the first 32 bits are ESC, numbers, and Q to D; if we have all of | |
179 | * those, consider it a full keyboard; do not test KEY_RESERVED, though */ | |
180 | mask = 0xFFFFFFFE; | |
181 | if ((bitmask_key[0] & mask) == mask) | |
182 | udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1"); | |
d7867b31 KS |
183 | } |
184 | ||
e216e514 | 185 | static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) |
d7867b31 | 186 | { |
912541b0 KS |
187 | struct udev_device *pdev; |
188 | unsigned long bitmask_ev[NBITS(EV_MAX)]; | |
189 | unsigned long bitmask_abs[NBITS(ABS_MAX)]; | |
190 | unsigned long bitmask_key[NBITS(KEY_MAX)]; | |
191 | unsigned long bitmask_rel[NBITS(REL_MAX)]; | |
192 | ||
193 | /* walk up the parental chain until we find the real input device; the | |
194 | * argument is very likely a subdevice of this, like eventN */ | |
195 | pdev = dev; | |
196 | while (pdev != NULL && udev_device_get_sysattr_value(pdev, "capabilities/ev") == NULL) | |
197 | pdev = udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL); | |
198 | ||
199 | /* not an "input" class device */ | |
200 | if (pdev == NULL) | |
201 | return EXIT_SUCCESS; | |
202 | ||
203 | /* Use this as a flag that input devices were detected, so that this | |
204 | * program doesn't need to be called more than once per device */ | |
205 | udev_builtin_add_property(dev, test, "ID_INPUT", "1"); | |
206 | get_cap_mask(dev, pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test); | |
207 | get_cap_mask(dev, pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test); | |
208 | get_cap_mask(dev, pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test); | |
209 | get_cap_mask(dev, pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test); | |
210 | test_pointers(dev, bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel, test); | |
211 | test_key(dev, bitmask_ev, bitmask_key, test); | |
212 | return EXIT_SUCCESS; | |
d7867b31 KS |
213 | } |
214 | ||
215 | const struct udev_builtin udev_builtin_input_id = { | |
912541b0 KS |
216 | .name = "input_id", |
217 | .cmd = builtin_input_id, | |
218 | .help = "input device properties", | |
d7867b31 | 219 | }; |