]>
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> | |
6 | * Copyright (C) 2011 Kay Sievers <kay.sievers@vrfy.org> | |
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 | ||
41 | /* | |
42 | * Read a capability attribute and return bitmask. | |
43 | * @param dev udev_device | |
44 | * @param attr sysfs attribute name (e. g. "capabilities/key") | |
45 | * @param bitmask: Output array which has a sizeof of bitmask_size | |
46 | */ | |
47 | static void get_cap_mask(struct udev_device *dev, | |
912541b0 KS |
48 | struct udev_device *pdev, const char* attr, |
49 | unsigned long *bitmask, size_t bitmask_size, | |
50 | bool test) | |
d7867b31 | 51 | { |
912541b0 KS |
52 | struct udev *udev = udev_device_get_udev(dev); |
53 | char text[4096]; | |
54 | unsigned i; | |
55 | char* word; | |
56 | unsigned long val; | |
57 | ||
58 | snprintf(text, sizeof(text), "%s", udev_device_get_sysattr_value(pdev, attr)); | |
59 | info(udev, "%s raw kernel attribute: %s\n", attr, text); | |
60 | ||
61 | memset (bitmask, 0, bitmask_size); | |
62 | i = 0; | |
63 | while ((word = strrchr(text, ' ')) != NULL) { | |
64 | val = strtoul (word+1, NULL, 16); | |
65 | if (i < bitmask_size/sizeof(unsigned long)) | |
66 | bitmask[i] = val; | |
67 | else | |
68 | info(udev, "ignoring %s block %lX which is larger than maximum size\n", attr, val); | |
69 | *word = '\0'; | |
70 | ++i; | |
71 | } | |
72 | val = strtoul (text, NULL, 16); | |
73 | if (i < bitmask_size / sizeof(unsigned long)) | |
74 | bitmask[i] = val; | |
75 | else | |
76 | info(udev, "ignoring %s block %lX which is larger than maximum size\n", attr, val); | |
77 | ||
78 | if (test) { | |
79 | /* printf pattern with the right unsigned long number of hex chars */ | |
80 | snprintf(text, sizeof(text), " bit %%4u: %%0%zilX\n", 2 * sizeof(unsigned long)); | |
81 | info(udev, "%s decoded bit map:\n", attr); | |
82 | val = bitmask_size / sizeof (unsigned long); | |
83 | /* skip over leading zeros */ | |
84 | while (bitmask[val-1] == 0 && val > 0) | |
85 | --val; | |
86 | for (i = 0; i < val; ++i) | |
87 | info(udev, text, i * BITS_PER_LONG, bitmask[i]); | |
88 | } | |
d7867b31 KS |
89 | } |
90 | ||
91 | /* pointer devices */ | |
92 | static void test_pointers (struct udev_device *dev, | |
912541b0 KS |
93 | const unsigned long* bitmask_ev, |
94 | const unsigned long* bitmask_abs, | |
95 | const unsigned long* bitmask_key, | |
96 | const unsigned long* bitmask_rel, | |
97 | bool test) | |
d7867b31 | 98 | { |
912541b0 KS |
99 | int is_mouse = 0; |
100 | int is_touchpad = 0; | |
101 | ||
102 | if (!test_bit (EV_KEY, bitmask_ev)) { | |
103 | if (test_bit (EV_ABS, bitmask_ev) && | |
104 | test_bit (ABS_X, bitmask_abs) && | |
105 | test_bit (ABS_Y, bitmask_abs) && | |
106 | test_bit (ABS_Z, bitmask_abs)) | |
107 | udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1"); | |
108 | return; | |
109 | } | |
110 | ||
111 | if (test_bit (EV_ABS, bitmask_ev) && | |
112 | test_bit (ABS_X, bitmask_abs) && test_bit (ABS_Y, bitmask_abs)) { | |
113 | if (test_bit (BTN_STYLUS, bitmask_key) || test_bit (BTN_TOOL_PEN, bitmask_key)) | |
114 | udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1"); | |
115 | else if (test_bit (BTN_TOOL_FINGER, bitmask_key) && !test_bit (BTN_TOOL_PEN, bitmask_key)) | |
116 | is_touchpad = 1; | |
117 | else if (test_bit (BTN_TRIGGER, bitmask_key) || | |
118 | test_bit (BTN_A, bitmask_key) || | |
119 | test_bit (BTN_1, bitmask_key)) | |
120 | udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1"); | |
121 | else if (test_bit (BTN_MOUSE, bitmask_key)) | |
122 | /* This path is taken by VMware's USB mouse, which has | |
123 | * absolute axes, but no touch/pressure button. */ | |
124 | is_mouse = 1; | |
125 | else if (test_bit (BTN_TOUCH, bitmask_key)) | |
126 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1"); | |
127 | } | |
128 | ||
129 | if (test_bit (EV_REL, bitmask_ev) && | |
130 | test_bit (REL_X, bitmask_rel) && test_bit (REL_Y, bitmask_rel) && | |
131 | test_bit (BTN_MOUSE, bitmask_key)) | |
132 | is_mouse = 1; | |
133 | ||
134 | if (is_mouse) | |
135 | udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1"); | |
136 | if (is_touchpad) | |
137 | udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1"); | |
d7867b31 KS |
138 | } |
139 | ||
140 | /* key like devices */ | |
141 | static void test_key (struct udev_device *dev, | |
912541b0 KS |
142 | const unsigned long* bitmask_ev, |
143 | const unsigned long* bitmask_key, | |
144 | bool test) | |
d7867b31 | 145 | { |
912541b0 KS |
146 | struct udev *udev = udev_device_get_udev(dev); |
147 | unsigned i; | |
148 | unsigned long found; | |
149 | unsigned long mask; | |
150 | ||
151 | /* do we have any KEY_* capability? */ | |
152 | if (!test_bit (EV_KEY, bitmask_ev)) { | |
153 | info(udev, "test_key: no EV_KEY capability\n"); | |
154 | return; | |
155 | } | |
156 | ||
157 | /* only consider KEY_* here, not BTN_* */ | |
158 | found = 0; | |
159 | for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) { | |
160 | found |= bitmask_key[i]; | |
161 | info(udev, "test_key: checking bit block %lu for any keys; found=%i\n", i*BITS_PER_LONG, found > 0); | |
162 | } | |
163 | /* If there are no keys in the lower block, check the higher block */ | |
164 | if (!found) { | |
165 | for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) { | |
166 | if (test_bit (i, bitmask_key)) { | |
167 | info(udev, "test_key: Found key %x in high block\n", i); | |
168 | found = 1; | |
169 | break; | |
170 | } | |
171 | } | |
172 | } | |
173 | ||
174 | if (found > 0) | |
175 | udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); | |
176 | ||
177 | /* the first 32 bits are ESC, numbers, and Q to D; if we have all of | |
178 | * those, consider it a full keyboard; do not test KEY_RESERVED, though */ | |
179 | mask = 0xFFFFFFFE; | |
180 | if ((bitmask_key[0] & mask) == mask) | |
181 | udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1"); | |
d7867b31 KS |
182 | } |
183 | ||
e216e514 | 184 | static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) |
d7867b31 | 185 | { |
912541b0 KS |
186 | struct udev_device *pdev; |
187 | unsigned long bitmask_ev[NBITS(EV_MAX)]; | |
188 | unsigned long bitmask_abs[NBITS(ABS_MAX)]; | |
189 | unsigned long bitmask_key[NBITS(KEY_MAX)]; | |
190 | unsigned long bitmask_rel[NBITS(REL_MAX)]; | |
191 | ||
192 | /* walk up the parental chain until we find the real input device; the | |
193 | * argument is very likely a subdevice of this, like eventN */ | |
194 | pdev = dev; | |
195 | while (pdev != NULL && udev_device_get_sysattr_value(pdev, "capabilities/ev") == NULL) | |
196 | pdev = udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL); | |
197 | ||
198 | /* not an "input" class device */ | |
199 | if (pdev == NULL) | |
200 | return EXIT_SUCCESS; | |
201 | ||
202 | /* Use this as a flag that input devices were detected, so that this | |
203 | * program doesn't need to be called more than once per device */ | |
204 | udev_builtin_add_property(dev, test, "ID_INPUT", "1"); | |
205 | get_cap_mask(dev, pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test); | |
206 | get_cap_mask(dev, pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test); | |
207 | get_cap_mask(dev, pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test); | |
208 | get_cap_mask(dev, pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test); | |
209 | test_pointers(dev, bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel, test); | |
210 | test_key(dev, bitmask_ev, bitmask_key, test); | |
211 | return EXIT_SUCCESS; | |
d7867b31 KS |
212 | } |
213 | ||
214 | const struct udev_builtin udev_builtin_input_id = { | |
912541b0 KS |
215 | .name = "input_id", |
216 | .cmd = builtin_input_id, | |
217 | .help = "input device properties", | |
d7867b31 | 218 | }; |