Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * The input core | |
4 | * | |
5 | * Copyright (c) 1999-2002 Vojtech Pavlik | |
6 | */ | |
7 | ||
1da177e4 | 8 | |
da0c4901 JP |
9 | #define pr_fmt(fmt) KBUILD_BASENAME ": " fmt |
10 | ||
1da177e4 | 11 | #include <linux/init.h> |
ffd0db97 | 12 | #include <linux/types.h> |
7f8d4cad | 13 | #include <linux/idr.h> |
47c78e89 | 14 | #include <linux/input/mt.h> |
1da177e4 | 15 | #include <linux/module.h> |
5a0e3ad6 | 16 | #include <linux/slab.h> |
1da177e4 LT |
17 | #include <linux/random.h> |
18 | #include <linux/major.h> | |
19 | #include <linux/proc_fs.h> | |
a99bbaf5 | 20 | #include <linux/sched.h> |
969b21cd | 21 | #include <linux/seq_file.h> |
345c7b74 | 22 | #include <linux/pm.h> |
1da177e4 LT |
23 | #include <linux/poll.h> |
24 | #include <linux/device.h> | |
60f07c29 | 25 | #include <linux/kstrtox.h> |
e676c232 | 26 | #include <linux/mutex.h> |
8006479c | 27 | #include <linux/rcupdate.h> |
15e184af | 28 | #include "input-compat.h" |
ebfa0043 | 29 | #include "input-core-private.h" |
e95656ea | 30 | #include "input-poller.h" |
1da177e4 LT |
31 | |
32 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); | |
33 | MODULE_DESCRIPTION("Input core"); | |
34 | MODULE_LICENSE("GPL"); | |
35 | ||
7f8d4cad DT |
36 | #define INPUT_MAX_CHAR_DEVICES 1024 |
37 | #define INPUT_FIRST_DYNAMIC_DEV 256 | |
38 | static DEFINE_IDA(input_ida); | |
1da177e4 LT |
39 | |
40 | static LIST_HEAD(input_dev_list); | |
41 | static LIST_HEAD(input_handler_list); | |
42 | ||
8006479c DT |
43 | /* |
44 | * input_mutex protects access to both input_dev_list and input_handler_list. | |
45 | * This also causes input_[un]register_device and input_[un]register_handler | |
46 | * be mutually exclusive which simplifies locking in drivers implementing | |
47 | * input handlers. | |
48 | */ | |
49 | static DEFINE_MUTEX(input_mutex); | |
50 | ||
4369c64c HR |
51 | static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 }; |
52 | ||
409353cb JL |
53 | static const unsigned int input_max_code[EV_CNT] = { |
54 | [EV_KEY] = KEY_MAX, | |
55 | [EV_REL] = REL_MAX, | |
56 | [EV_ABS] = ABS_MAX, | |
57 | [EV_MSC] = MSC_MAX, | |
58 | [EV_SW] = SW_MAX, | |
59 | [EV_LED] = LED_MAX, | |
60 | [EV_SND] = SND_MAX, | |
61 | [EV_FF] = FF_MAX, | |
62 | }; | |
63 | ||
8006479c DT |
64 | static inline int is_event_supported(unsigned int code, |
65 | unsigned long *bm, unsigned int max) | |
1da177e4 | 66 | { |
8006479c DT |
67 | return code <= max && test_bit(code, bm); |
68 | } | |
1da177e4 | 69 | |
8006479c DT |
70 | static int input_defuzz_abs_event(int value, int old_val, int fuzz) |
71 | { | |
72 | if (fuzz) { | |
73 | if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2) | |
74 | return old_val; | |
1da177e4 | 75 | |
8006479c DT |
76 | if (value > old_val - fuzz && value < old_val + fuzz) |
77 | return (old_val * 3 + value) / 4; | |
1da177e4 | 78 | |
8006479c DT |
79 | if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2) |
80 | return (old_val + value) / 2; | |
81 | } | |
1da177e4 | 82 | |
8006479c DT |
83 | return value; |
84 | } | |
1da177e4 | 85 | |
352ac4bd HR |
86 | static void input_start_autorepeat(struct input_dev *dev, int code) |
87 | { | |
88 | if (test_bit(EV_REP, dev->evbit) && | |
89 | dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && | |
4e974c12 | 90 | dev->timer.function) { |
352ac4bd HR |
91 | dev->repeat_key = code; |
92 | mod_timer(&dev->timer, | |
93 | jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); | |
94 | } | |
95 | } | |
96 | ||
97 | static void input_stop_autorepeat(struct input_dev *dev) | |
98 | { | |
8fa7292f | 99 | timer_delete(&dev->timer); |
352ac4bd HR |
100 | } |
101 | ||
4369c64c HR |
102 | /* |
103 | * Pass values first through all filters and then, if event has not been | |
d469647b DT |
104 | * filtered out, through all open handles. This order is achieved by placing |
105 | * filters at the head of the list of handles attached to the device, and | |
106 | * placing regular handles at the tail of the list. | |
107 | * | |
108 | * This function is called with dev->event_lock held and interrupts disabled. | |
4369c64c HR |
109 | */ |
110 | static void input_pass_values(struct input_dev *dev, | |
111 | struct input_value *vals, unsigned int count) | |
8006479c | 112 | { |
82ba56c2 | 113 | struct input_handle *handle; |
4369c64c | 114 | struct input_value *v; |
ef7995f4 | 115 | |
ebfa0043 AC |
116 | lockdep_assert_held(&dev->event_lock); |
117 | ||
21d8dd0d DT |
118 | scoped_guard(rcu) { |
119 | handle = rcu_dereference(dev->grab); | |
120 | if (handle) { | |
121 | count = handle->handle_events(handle, vals, count); | |
122 | break; | |
123 | } | |
1da177e4 | 124 | |
21d8dd0d | 125 | list_for_each_entry_rcu(handle, &dev->h_list, d_node) { |
2c50ad34 | 126 | if (handle->open) { |
071b24b5 DT |
127 | count = handle->handle_events(handle, vals, |
128 | count); | |
2c50ad34 AG |
129 | if (!count) |
130 | break; | |
131 | } | |
21d8dd0d | 132 | } |
ef7995f4 | 133 | } |
ef7995f4 | 134 | |
352ac4bd | 135 | /* trigger auto repeat for key events */ |
5ab17145 AG |
136 | if (test_bit(EV_REP, dev->evbit) && test_bit(EV_KEY, dev->evbit)) { |
137 | for (v = vals; v != vals + count; v++) { | |
138 | if (v->type == EV_KEY && v->value != 2) { | |
139 | if (v->value) | |
140 | input_start_autorepeat(dev, v->code); | |
141 | else | |
142 | input_stop_autorepeat(dev); | |
143 | } | |
ef7995f4 DT |
144 | } |
145 | } | |
4369c64c | 146 | } |
ef7995f4 | 147 | |
8006479c DT |
148 | #define INPUT_IGNORE_EVENT 0 |
149 | #define INPUT_PASS_TO_HANDLERS 1 | |
150 | #define INPUT_PASS_TO_DEVICE 2 | |
4369c64c HR |
151 | #define INPUT_SLOT 4 |
152 | #define INPUT_FLUSH 8 | |
8006479c | 153 | #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE) |
1da177e4 | 154 | |
40d007e7 HR |
155 | static int input_handle_abs_event(struct input_dev *dev, |
156 | unsigned int code, int *pval) | |
157 | { | |
8d18fba2 | 158 | struct input_mt *mt = dev->mt; |
3516fa16 | 159 | bool is_new_slot = false; |
40d007e7 HR |
160 | bool is_mt_event; |
161 | int *pold; | |
162 | ||
163 | if (code == ABS_MT_SLOT) { | |
164 | /* | |
165 | * "Stage" the event; we'll flush it later, when we | |
144c0f88 | 166 | * get actual touch data. |
40d007e7 | 167 | */ |
8d18fba2 HR |
168 | if (mt && *pval >= 0 && *pval < mt->num_slots) |
169 | mt->slot = *pval; | |
40d007e7 HR |
170 | |
171 | return INPUT_IGNORE_EVENT; | |
172 | } | |
173 | ||
b89529a1 | 174 | is_mt_event = input_is_mt_value(code); |
40d007e7 HR |
175 | |
176 | if (!is_mt_event) { | |
d31b2865 | 177 | pold = &dev->absinfo[code].value; |
8d18fba2 HR |
178 | } else if (mt) { |
179 | pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST]; | |
3516fa16 | 180 | is_new_slot = mt->slot != dev->absinfo[ABS_MT_SLOT].value; |
40d007e7 HR |
181 | } else { |
182 | /* | |
144c0f88 | 183 | * Bypass filtering for multi-touch events when |
40d007e7 HR |
184 | * not employing slots. |
185 | */ | |
186 | pold = NULL; | |
187 | } | |
188 | ||
189 | if (pold) { | |
190 | *pval = input_defuzz_abs_event(*pval, *pold, | |
d31b2865 | 191 | dev->absinfo[code].fuzz); |
40d007e7 HR |
192 | if (*pold == *pval) |
193 | return INPUT_IGNORE_EVENT; | |
194 | ||
195 | *pold = *pval; | |
196 | } | |
197 | ||
198 | /* Flush pending "slot" event */ | |
3516fa16 DT |
199 | if (is_new_slot) { |
200 | dev->absinfo[ABS_MT_SLOT].value = mt->slot; | |
4369c64c | 201 | return INPUT_PASS_TO_HANDLERS | INPUT_SLOT; |
40d007e7 HR |
202 | } |
203 | ||
204 | return INPUT_PASS_TO_HANDLERS; | |
205 | } | |
206 | ||
4369c64c | 207 | static int input_get_disposition(struct input_dev *dev, |
50c5d36d | 208 | unsigned int type, unsigned int code, int *pval) |
8006479c DT |
209 | { |
210 | int disposition = INPUT_IGNORE_EVENT; | |
50c5d36d | 211 | int value = *pval; |
1da177e4 | 212 | |
59b7a5af DT |
213 | /* filter-out events from inhibited devices */ |
214 | if (dev->inhibited) | |
215 | return INPUT_IGNORE_EVENT; | |
216 | ||
8006479c | 217 | switch (type) { |
1da177e4 | 218 | |
8006479c DT |
219 | case EV_SYN: |
220 | switch (code) { | |
221 | case SYN_CONFIG: | |
222 | disposition = INPUT_PASS_TO_ALL; | |
223 | break; | |
1da177e4 | 224 | |
8006479c | 225 | case SYN_REPORT: |
4369c64c | 226 | disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH; |
1da177e4 | 227 | break; |
5e5ee686 | 228 | case SYN_MT_REPORT: |
5e5ee686 HR |
229 | disposition = INPUT_PASS_TO_HANDLERS; |
230 | break; | |
8006479c DT |
231 | } |
232 | break; | |
1da177e4 | 233 | |
8006479c | 234 | case EV_KEY: |
0672120a | 235 | if (is_event_supported(code, dev->keybit, KEY_MAX)) { |
1da177e4 | 236 | |
0672120a HR |
237 | /* auto-repeat bypasses state updates */ |
238 | if (value == 2) { | |
239 | disposition = INPUT_PASS_TO_HANDLERS; | |
240 | break; | |
8006479c | 241 | } |
1da177e4 | 242 | |
0672120a | 243 | if (!!test_bit(code, dev->key) != !!value) { |
1da177e4 | 244 | |
8006479c | 245 | __change_bit(code, dev->key); |
0672120a | 246 | disposition = INPUT_PASS_TO_HANDLERS; |
8006479c | 247 | } |
8006479c DT |
248 | } |
249 | break; | |
1da177e4 | 250 | |
8006479c DT |
251 | case EV_SW: |
252 | if (is_event_supported(code, dev->swbit, SW_MAX) && | |
0672120a | 253 | !!test_bit(code, dev->sw) != !!value) { |
1da177e4 | 254 | |
8006479c DT |
255 | __change_bit(code, dev->sw); |
256 | disposition = INPUT_PASS_TO_HANDLERS; | |
257 | } | |
258 | break; | |
1da177e4 | 259 | |
8006479c | 260 | case EV_ABS: |
40d007e7 | 261 | if (is_event_supported(code, dev->absbit, ABS_MAX)) |
9ae4345a | 262 | disposition = input_handle_abs_event(dev, code, &value); |
61994a61 | 263 | |
8006479c | 264 | break; |
1da177e4 | 265 | |
8006479c DT |
266 | case EV_REL: |
267 | if (is_event_supported(code, dev->relbit, REL_MAX) && value) | |
268 | disposition = INPUT_PASS_TO_HANDLERS; | |
1da177e4 | 269 | |
8006479c | 270 | break; |
1e0afb28 | 271 | |
8006479c DT |
272 | case EV_MSC: |
273 | if (is_event_supported(code, dev->mscbit, MSC_MAX)) | |
274 | disposition = INPUT_PASS_TO_ALL; | |
1da177e4 | 275 | |
8006479c | 276 | break; |
1da177e4 | 277 | |
8006479c DT |
278 | case EV_LED: |
279 | if (is_event_supported(code, dev->ledbit, LED_MAX) && | |
0672120a | 280 | !!test_bit(code, dev->led) != !!value) { |
1da177e4 | 281 | |
8006479c DT |
282 | __change_bit(code, dev->led); |
283 | disposition = INPUT_PASS_TO_ALL; | |
284 | } | |
285 | break; | |
286 | ||
287 | case EV_SND: | |
288 | if (is_event_supported(code, dev->sndbit, SND_MAX)) { | |
1da177e4 | 289 | |
8fdc1948 | 290 | if (!!test_bit(code, dev->snd) != !!value) |
8006479c DT |
291 | __change_bit(code, dev->snd); |
292 | disposition = INPUT_PASS_TO_ALL; | |
293 | } | |
294 | break; | |
8fdc1948 | 295 | |
8006479c DT |
296 | case EV_REP: |
297 | if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) { | |
298 | dev->rep[code] = value; | |
299 | disposition = INPUT_PASS_TO_ALL; | |
300 | } | |
301 | break; | |
1da177e4 | 302 | |
8006479c DT |
303 | case EV_FF: |
304 | if (value >= 0) | |
305 | disposition = INPUT_PASS_TO_ALL; | |
306 | break; | |
ed2fa4dd RP |
307 | |
308 | case EV_PWR: | |
309 | disposition = INPUT_PASS_TO_ALL; | |
310 | break; | |
8006479c | 311 | } |
1da177e4 | 312 | |
50c5d36d | 313 | *pval = value; |
4369c64c HR |
314 | return disposition; |
315 | } | |
316 | ||
59b7a5af DT |
317 | static void input_event_dispose(struct input_dev *dev, int disposition, |
318 | unsigned int type, unsigned int code, int value) | |
4369c64c | 319 | { |
8006479c DT |
320 | if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event) |
321 | dev->event(dev, type, code, value); | |
1da177e4 | 322 | |
4369c64c HR |
323 | if (disposition & INPUT_PASS_TO_HANDLERS) { |
324 | struct input_value *v; | |
325 | ||
326 | if (disposition & INPUT_SLOT) { | |
327 | v = &dev->vals[dev->num_vals++]; | |
328 | v->type = EV_ABS; | |
329 | v->code = ABS_MT_SLOT; | |
330 | v->value = dev->mt->slot; | |
331 | } | |
332 | ||
333 | v = &dev->vals[dev->num_vals++]; | |
334 | v->type = type; | |
335 | v->code = code; | |
336 | v->value = value; | |
337 | } | |
338 | ||
339 | if (disposition & INPUT_FLUSH) { | |
340 | if (dev->num_vals >= 2) | |
341 | input_pass_values(dev, dev->vals, dev->num_vals); | |
342 | dev->num_vals = 0; | |
4370b231 DT |
343 | /* |
344 | * Reset the timestamp on flush so we won't end up | |
345 | * with a stale one. Note we only need to reset the | |
346 | * monolithic one as we use its presence when deciding | |
347 | * whether to generate a synthetic timestamp. | |
348 | */ | |
349 | dev->timestamp[INPUT_CLK_MONO] = ktime_set(0, 0); | |
4369c64c HR |
350 | } else if (dev->num_vals >= dev->max_vals - 2) { |
351 | dev->vals[dev->num_vals++] = input_value_sync; | |
352 | input_pass_values(dev, dev->vals, dev->num_vals); | |
353 | dev->num_vals = 0; | |
354 | } | |
59b7a5af DT |
355 | } |
356 | ||
ebfa0043 AC |
357 | void input_handle_event(struct input_dev *dev, |
358 | unsigned int type, unsigned int code, int value) | |
59b7a5af DT |
359 | { |
360 | int disposition; | |
361 | ||
362 | lockdep_assert_held(&dev->event_lock); | |
363 | ||
364 | disposition = input_get_disposition(dev, type, code, &value); | |
365 | if (disposition != INPUT_IGNORE_EVENT) { | |
366 | if (type != EV_SYN) | |
367 | add_input_randomness(type, code, value); | |
4369c64c | 368 | |
59b7a5af DT |
369 | input_event_dispose(dev, disposition, type, code, value); |
370 | } | |
8006479c | 371 | } |
1da177e4 | 372 | |
8006479c DT |
373 | /** |
374 | * input_event() - report new input event | |
375 | * @dev: device that generated the event | |
376 | * @type: type of the event | |
377 | * @code: event code | |
378 | * @value: value of the event | |
379 | * | |
380 | * This function should be used by drivers implementing various input | |
df2d4637 DT |
381 | * devices to report input events. See also input_inject_event(). |
382 | * | |
383 | * NOTE: input_event() may be safely used right after input device was | |
384 | * allocated with input_allocate_device(), even before it is registered | |
385 | * with input_register_device(), but the event will not reach any of the | |
386 | * input handlers. Such early invocation of input_event() may be used | |
387 | * to 'seed' initial state of a switch or initial position of absolute | |
388 | * axis, etc. | |
8006479c | 389 | */ |
8006479c DT |
390 | void input_event(struct input_dev *dev, |
391 | unsigned int type, unsigned int code, int value) | |
392 | { | |
8006479c | 393 | if (is_event_supported(type, dev->evbit, EV_MAX)) { |
21d8dd0d | 394 | guard(spinlock_irqsave)(&dev->event_lock); |
9ae4345a | 395 | input_handle_event(dev, type, code, value); |
1da177e4 | 396 | } |
1da177e4 | 397 | } |
ca56fe07 | 398 | EXPORT_SYMBOL(input_event); |
1da177e4 | 399 | |
0e739d28 DT |
400 | /** |
401 | * input_inject_event() - send input event from input handler | |
402 | * @handle: input handle to send event through | |
403 | * @type: type of the event | |
404 | * @code: event code | |
405 | * @value: value of the event | |
406 | * | |
8006479c DT |
407 | * Similar to input_event() but will ignore event if device is |
408 | * "grabbed" and handle injecting event is not the one that owns | |
409 | * the device. | |
0e739d28 | 410 | */ |
8006479c DT |
411 | void input_inject_event(struct input_handle *handle, |
412 | unsigned int type, unsigned int code, int value) | |
1da177e4 | 413 | { |
8006479c DT |
414 | struct input_dev *dev = handle->dev; |
415 | struct input_handle *grab; | |
1da177e4 | 416 | |
8006479c | 417 | if (is_event_supported(type, dev->evbit, EV_MAX)) { |
21d8dd0d DT |
418 | guard(spinlock_irqsave)(&dev->event_lock); |
419 | guard(rcu)(); | |
1da177e4 | 420 | |
8006479c DT |
421 | grab = rcu_dereference(dev->grab); |
422 | if (!grab || grab == handle) | |
9ae4345a | 423 | input_handle_event(dev, type, code, value); |
1da177e4 | 424 | |
8006479c | 425 | } |
1da177e4 | 426 | } |
8006479c | 427 | EXPORT_SYMBOL(input_inject_event); |
1da177e4 | 428 | |
d31b2865 DM |
429 | /** |
430 | * input_alloc_absinfo - allocates array of input_absinfo structs | |
431 | * @dev: the input device emitting absolute events | |
432 | * | |
433 | * If the absinfo struct the caller asked for is already allocated, this | |
434 | * functions will not do anything. | |
435 | */ | |
436 | void input_alloc_absinfo(struct input_dev *dev) | |
437 | { | |
100294ce DT |
438 | if (dev->absinfo) |
439 | return; | |
d31b2865 | 440 | |
100294ce DT |
441 | dev->absinfo = kcalloc(ABS_CNT, sizeof(*dev->absinfo), GFP_KERNEL); |
442 | if (!dev->absinfo) { | |
443 | dev_err(dev->dev.parent ?: &dev->dev, | |
444 | "%s: unable to allocate memory\n", __func__); | |
445 | /* | |
446 | * We will handle this allocation failure in | |
447 | * input_register_device() when we refuse to register input | |
448 | * device with ABS bits but without absinfo. | |
449 | */ | |
450 | } | |
d31b2865 DM |
451 | } |
452 | EXPORT_SYMBOL(input_alloc_absinfo); | |
453 | ||
454 | void input_set_abs_params(struct input_dev *dev, unsigned int axis, | |
455 | int min, int max, int fuzz, int flat) | |
456 | { | |
457 | struct input_absinfo *absinfo; | |
458 | ||
3f9ed5c2 HG |
459 | __set_bit(EV_ABS, dev->evbit); |
460 | __set_bit(axis, dev->absbit); | |
461 | ||
d31b2865 DM |
462 | input_alloc_absinfo(dev); |
463 | if (!dev->absinfo) | |
464 | return; | |
465 | ||
466 | absinfo = &dev->absinfo[axis]; | |
467 | absinfo->minimum = min; | |
468 | absinfo->maximum = max; | |
469 | absinfo->fuzz = fuzz; | |
470 | absinfo->flat = flat; | |
d31b2865 DM |
471 | } |
472 | EXPORT_SYMBOL(input_set_abs_params); | |
473 | ||
cb66b9ba HG |
474 | /** |
475 | * input_copy_abs - Copy absinfo from one input_dev to another | |
476 | * @dst: Destination input device to copy the abs settings to | |
477 | * @dst_axis: ABS_* value selecting the destination axis | |
478 | * @src: Source input device to copy the abs settings from | |
479 | * @src_axis: ABS_* value selecting the source axis | |
480 | * | |
481 | * Set absinfo for the selected destination axis by copying it from | |
482 | * the specified source input device's source axis. | |
483 | * This is useful to e.g. setup a pen/stylus input-device for combined | |
484 | * touchscreen/pen hardware where the pen uses the same coordinates as | |
485 | * the touchscreen. | |
486 | */ | |
487 | void input_copy_abs(struct input_dev *dst, unsigned int dst_axis, | |
488 | const struct input_dev *src, unsigned int src_axis) | |
489 | { | |
490 | /* src must have EV_ABS and src_axis set */ | |
491 | if (WARN_ON(!(test_bit(EV_ABS, src->evbit) && | |
492 | test_bit(src_axis, src->absbit)))) | |
493 | return; | |
494 | ||
495 | /* | |
496 | * input_alloc_absinfo() may have failed for the source. Our caller is | |
497 | * expected to catch this when registering the input devices, which may | |
498 | * happen after the input_copy_abs() call. | |
499 | */ | |
500 | if (!src->absinfo) | |
501 | return; | |
502 | ||
503 | input_set_capability(dst, EV_ABS, dst_axis); | |
504 | if (!dst->absinfo) | |
505 | return; | |
506 | ||
507 | dst->absinfo[dst_axis] = src->absinfo[src_axis]; | |
508 | } | |
509 | EXPORT_SYMBOL(input_copy_abs); | |
d31b2865 | 510 | |
8006479c DT |
511 | /** |
512 | * input_grab_device - grabs device for exclusive use | |
513 | * @handle: input handle that wants to own the device | |
514 | * | |
515 | * When a device is grabbed by an input handle all events generated by | |
516 | * the device are delivered only to this handle. Also events injected | |
517 | * by other input handles are ignored while device is grabbed. | |
518 | */ | |
1da177e4 LT |
519 | int input_grab_device(struct input_handle *handle) |
520 | { | |
8006479c | 521 | struct input_dev *dev = handle->dev; |
1da177e4 | 522 | |
21d8dd0d DT |
523 | scoped_cond_guard(mutex_intr, return -EINTR, &dev->mutex) { |
524 | if (dev->grab) | |
525 | return -EBUSY; | |
8006479c | 526 | |
21d8dd0d | 527 | rcu_assign_pointer(dev->grab, handle); |
8006479c DT |
528 | } |
529 | ||
21d8dd0d | 530 | return 0; |
1da177e4 | 531 | } |
ca56fe07 | 532 | EXPORT_SYMBOL(input_grab_device); |
1da177e4 | 533 | |
8006479c | 534 | static void __input_release_device(struct input_handle *handle) |
1da177e4 | 535 | { |
a2b2ed2c | 536 | struct input_dev *dev = handle->dev; |
adc4633c | 537 | struct input_handle *grabber; |
c7e8dc6e | 538 | |
adc4633c DT |
539 | grabber = rcu_dereference_protected(dev->grab, |
540 | lockdep_is_held(&dev->mutex)); | |
541 | if (grabber == handle) { | |
8006479c | 542 | rcu_assign_pointer(dev->grab, NULL); |
59b7a5af | 543 | /* Make sure input_pass_values() notices that grab is gone */ |
82ba56c2 | 544 | synchronize_rcu(); |
a2b2ed2c AM |
545 | |
546 | list_for_each_entry(handle, &dev->h_list, d_node) | |
8006479c | 547 | if (handle->open && handle->handler->start) |
c7e8dc6e DT |
548 | handle->handler->start(handle); |
549 | } | |
1da177e4 | 550 | } |
8006479c DT |
551 | |
552 | /** | |
553 | * input_release_device - release previously grabbed device | |
554 | * @handle: input handle that owns the device | |
555 | * | |
556 | * Releases previously grabbed device so that other input handles can | |
557 | * start receiving input events. Upon release all handlers attached | |
558 | * to the device have their start() method called so they have a change | |
559 | * to synchronize device state with the rest of the system. | |
560 | */ | |
561 | void input_release_device(struct input_handle *handle) | |
562 | { | |
563 | struct input_dev *dev = handle->dev; | |
564 | ||
21d8dd0d | 565 | guard(mutex)(&dev->mutex); |
8006479c | 566 | __input_release_device(handle); |
8006479c | 567 | } |
ca56fe07 | 568 | EXPORT_SYMBOL(input_release_device); |
1da177e4 | 569 | |
8006479c DT |
570 | /** |
571 | * input_open_device - open input device | |
572 | * @handle: handle through which device is being accessed | |
573 | * | |
574 | * This function should be called by input handlers when they | |
575 | * want to start receive events from given input device. | |
576 | */ | |
1da177e4 LT |
577 | int input_open_device(struct input_handle *handle) |
578 | { | |
0fbf87ca | 579 | struct input_dev *dev = handle->dev; |
21d8dd0d | 580 | int error; |
0fbf87ca | 581 | |
21d8dd0d DT |
582 | scoped_cond_guard(mutex_intr, return -EINTR, &dev->mutex) { |
583 | if (dev->going_away) | |
584 | return -ENODEV; | |
0fbf87ca | 585 | |
21d8dd0d | 586 | handle->open++; |
57a06363 | 587 | |
21d8dd0d DT |
588 | if (handle->handler->passive_observer) |
589 | return 0; | |
8006479c | 590 | |
21d8dd0d | 591 | if (dev->users++ || dev->inhibited) { |
8006479c | 592 | /* |
21d8dd0d DT |
593 | * Device is already opened and/or inhibited, |
594 | * so we can exit immediately and report success. | |
8006479c | 595 | */ |
21d8dd0d | 596 | return 0; |
8006479c | 597 | } |
0fbf87ca | 598 | |
21d8dd0d DT |
599 | if (dev->open) { |
600 | error = dev->open(dev); | |
601 | if (error) { | |
602 | dev->users--; | |
603 | handle->open--; | |
604 | /* | |
605 | * Make sure we are not delivering any more | |
606 | * events through this handle. | |
607 | */ | |
608 | synchronize_rcu(); | |
609 | return error; | |
610 | } | |
611 | } | |
e95656ea | 612 | |
21d8dd0d DT |
613 | if (dev->poller) |
614 | input_dev_poller_start(dev->poller); | |
615 | } | |
616 | ||
617 | return 0; | |
1da177e4 | 618 | } |
ca56fe07 | 619 | EXPORT_SYMBOL(input_open_device); |
1da177e4 | 620 | |
8006479c | 621 | int input_flush_device(struct input_handle *handle, struct file *file) |
1da177e4 | 622 | { |
8006479c | 623 | struct input_dev *dev = handle->dev; |
1da177e4 | 624 | |
21d8dd0d DT |
625 | scoped_cond_guard(mutex_intr, return -EINTR, &dev->mutex) { |
626 | if (dev->flush) | |
627 | return dev->flush(dev, file); | |
628 | } | |
8006479c | 629 | |
21d8dd0d | 630 | return 0; |
1da177e4 | 631 | } |
ca56fe07 | 632 | EXPORT_SYMBOL(input_flush_device); |
1da177e4 | 633 | |
8006479c DT |
634 | /** |
635 | * input_close_device - close input device | |
636 | * @handle: handle through which device is being accessed | |
637 | * | |
638 | * This function should be called by input handlers when they | |
639 | * want to stop receive events from given input device. | |
640 | */ | |
1da177e4 LT |
641 | void input_close_device(struct input_handle *handle) |
642 | { | |
0fbf87ca DT |
643 | struct input_dev *dev = handle->dev; |
644 | ||
21d8dd0d | 645 | guard(mutex)(&dev->mutex); |
0fbf87ca | 646 | |
8006479c DT |
647 | __input_release_device(handle); |
648 | ||
57a06363 DT |
649 | if (!handle->handler->passive_observer) { |
650 | if (!--dev->users && !dev->inhibited) { | |
651 | if (dev->poller) | |
652 | input_dev_poller_stop(dev->poller); | |
653 | if (dev->close) | |
654 | dev->close(dev); | |
655 | } | |
e95656ea | 656 | } |
8006479c DT |
657 | |
658 | if (!--handle->open) { | |
659 | /* | |
59b7a5af | 660 | * synchronize_rcu() makes sure that input_pass_values() |
8006479c DT |
661 | * completed and that no more input events are delivered |
662 | * through this handle | |
663 | */ | |
82ba56c2 | 664 | synchronize_rcu(); |
8006479c | 665 | } |
1da177e4 | 666 | } |
ca56fe07 | 667 | EXPORT_SYMBOL(input_close_device); |
1da177e4 | 668 | |
866d7d7b ON |
669 | /* |
670 | * Simulate keyup events for all keys that are marked as pressed. | |
671 | * The function must be called with dev->event_lock held. | |
672 | */ | |
ebfa0043 | 673 | static bool input_dev_release_keys(struct input_dev *dev) |
866d7d7b | 674 | { |
00159f19 | 675 | bool need_sync = false; |
866d7d7b ON |
676 | int code; |
677 | ||
ebfa0043 AC |
678 | lockdep_assert_held(&dev->event_lock); |
679 | ||
866d7d7b | 680 | if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) { |
00159f19 | 681 | for_each_set_bit(code, dev->key, KEY_CNT) { |
59b7a5af | 682 | input_handle_event(dev, EV_KEY, code, 0); |
00159f19 DT |
683 | need_sync = true; |
684 | } | |
866d7d7b | 685 | } |
ebfa0043 AC |
686 | |
687 | return need_sync; | |
866d7d7b ON |
688 | } |
689 | ||
8006479c DT |
690 | /* |
691 | * Prepare device for unregistering | |
692 | */ | |
693 | static void input_disconnect_device(struct input_dev *dev) | |
694 | { | |
695 | struct input_handle *handle; | |
8006479c DT |
696 | |
697 | /* | |
698 | * Mark device as going away. Note that we take dev->mutex here | |
699 | * not to protect access to dev->going_away but rather to ensure | |
700 | * that there are no threads in the middle of input_open_device() | |
701 | */ | |
21d8dd0d DT |
702 | scoped_guard(mutex, &dev->mutex) |
703 | dev->going_away = true; | |
8006479c | 704 | |
21d8dd0d | 705 | guard(spinlock_irq)(&dev->event_lock); |
8006479c DT |
706 | |
707 | /* | |
708 | * Simulate keyup events for all pressed keys so that handlers | |
709 | * are not left with "stuck" keys. The driver may continue | |
710 | * generate events even after we done here but they will not | |
711 | * reach any handlers. | |
712 | */ | |
ebfa0043 AC |
713 | if (input_dev_release_keys(dev)) |
714 | input_handle_event(dev, EV_SYN, SYN_REPORT, 1); | |
8006479c DT |
715 | |
716 | list_for_each_entry(handle, &dev->h_list, d_node) | |
717 | handle->open = 0; | |
8006479c DT |
718 | } |
719 | ||
8613e4c2 MCC |
720 | /** |
721 | * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry | |
722 | * @ke: keymap entry containing scancode to be converted. | |
723 | * @scancode: pointer to the location where converted scancode should | |
724 | * be stored. | |
725 | * | |
726 | * This function is used to convert scancode stored in &struct keymap_entry | |
727 | * into scalar form understood by legacy keymap handling methods. These | |
728 | * methods expect scancodes to be represented as 'unsigned int'. | |
729 | */ | |
730 | int input_scancode_to_scalar(const struct input_keymap_entry *ke, | |
731 | unsigned int *scancode) | |
732 | { | |
733 | switch (ke->len) { | |
734 | case 1: | |
735 | *scancode = *((u8 *)ke->scancode); | |
736 | break; | |
737 | ||
738 | case 2: | |
739 | *scancode = *((u16 *)ke->scancode); | |
740 | break; | |
741 | ||
742 | case 4: | |
743 | *scancode = *((u32 *)ke->scancode); | |
744 | break; | |
745 | ||
746 | default: | |
747 | return -EINVAL; | |
748 | } | |
749 | ||
750 | return 0; | |
751 | } | |
752 | EXPORT_SYMBOL(input_scancode_to_scalar); | |
753 | ||
754 | /* | |
755 | * Those routines handle the default case where no [gs]etkeycode() is | |
756 | * defined. In this case, an array indexed by the scancode is used. | |
757 | */ | |
758 | ||
759 | static unsigned int input_fetch_keycode(struct input_dev *dev, | |
760 | unsigned int index) | |
c8e4c772 MR |
761 | { |
762 | switch (dev->keycodesize) { | |
8613e4c2 MCC |
763 | case 1: |
764 | return ((u8 *)dev->keycode)[index]; | |
c8e4c772 | 765 | |
8613e4c2 MCC |
766 | case 2: |
767 | return ((u16 *)dev->keycode)[index]; | |
c8e4c772 | 768 | |
8613e4c2 MCC |
769 | default: |
770 | return ((u32 *)dev->keycode)[index]; | |
c8e4c772 MR |
771 | } |
772 | } | |
773 | ||
774 | static int input_default_getkeycode(struct input_dev *dev, | |
8613e4c2 | 775 | struct input_keymap_entry *ke) |
c8e4c772 | 776 | { |
8613e4c2 MCC |
777 | unsigned int index; |
778 | int error; | |
779 | ||
c8e4c772 MR |
780 | if (!dev->keycodesize) |
781 | return -EINVAL; | |
782 | ||
8613e4c2 MCC |
783 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) |
784 | index = ke->index; | |
785 | else { | |
786 | error = input_scancode_to_scalar(ke, &index); | |
787 | if (error) | |
788 | return error; | |
789 | } | |
790 | ||
791 | if (index >= dev->keycodemax) | |
c8e4c772 MR |
792 | return -EINVAL; |
793 | ||
8613e4c2 MCC |
794 | ke->keycode = input_fetch_keycode(dev, index); |
795 | ke->index = index; | |
796 | ke->len = sizeof(index); | |
797 | memcpy(ke->scancode, &index, sizeof(index)); | |
c8e4c772 MR |
798 | |
799 | return 0; | |
800 | } | |
801 | ||
802 | static int input_default_setkeycode(struct input_dev *dev, | |
8613e4c2 MCC |
803 | const struct input_keymap_entry *ke, |
804 | unsigned int *old_keycode) | |
c8e4c772 | 805 | { |
8613e4c2 MCC |
806 | unsigned int index; |
807 | int error; | |
c8e4c772 MR |
808 | int i; |
809 | ||
8613e4c2 | 810 | if (!dev->keycodesize) |
c8e4c772 MR |
811 | return -EINVAL; |
812 | ||
8613e4c2 MCC |
813 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { |
814 | index = ke->index; | |
815 | } else { | |
816 | error = input_scancode_to_scalar(ke, &index); | |
817 | if (error) | |
818 | return error; | |
819 | } | |
820 | ||
821 | if (index >= dev->keycodemax) | |
c8e4c772 MR |
822 | return -EINVAL; |
823 | ||
de391d12 | 824 | if (dev->keycodesize < sizeof(ke->keycode) && |
8613e4c2 | 825 | (ke->keycode >> (dev->keycodesize * 8))) |
c8e4c772 MR |
826 | return -EINVAL; |
827 | ||
828 | switch (dev->keycodesize) { | |
829 | case 1: { | |
830 | u8 *k = (u8 *)dev->keycode; | |
8613e4c2 MCC |
831 | *old_keycode = k[index]; |
832 | k[index] = ke->keycode; | |
c8e4c772 MR |
833 | break; |
834 | } | |
835 | case 2: { | |
836 | u16 *k = (u16 *)dev->keycode; | |
8613e4c2 MCC |
837 | *old_keycode = k[index]; |
838 | k[index] = ke->keycode; | |
c8e4c772 MR |
839 | break; |
840 | } | |
841 | default: { | |
842 | u32 *k = (u32 *)dev->keycode; | |
8613e4c2 MCC |
843 | *old_keycode = k[index]; |
844 | k[index] = ke->keycode; | |
c8e4c772 MR |
845 | break; |
846 | } | |
847 | } | |
848 | ||
cb222aed DT |
849 | if (*old_keycode <= KEY_MAX) { |
850 | __clear_bit(*old_keycode, dev->keybit); | |
851 | for (i = 0; i < dev->keycodemax; i++) { | |
852 | if (input_fetch_keycode(dev, i) == *old_keycode) { | |
853 | __set_bit(*old_keycode, dev->keybit); | |
854 | /* Setting the bit twice is useless, so break */ | |
855 | break; | |
856 | } | |
c8e4c772 MR |
857 | } |
858 | } | |
859 | ||
cb222aed | 860 | __set_bit(ke->keycode, dev->keybit); |
c8e4c772 MR |
861 | return 0; |
862 | } | |
863 | ||
f4f37c8e DT |
864 | /** |
865 | * input_get_keycode - retrieve keycode currently mapped to a given scancode | |
866 | * @dev: input device which keymap is being queried | |
8613e4c2 | 867 | * @ke: keymap entry |
f4f37c8e DT |
868 | * |
869 | * This function should be called by anyone interested in retrieving current | |
8613e4c2 | 870 | * keymap. Presently evdev handlers use it. |
f4f37c8e | 871 | */ |
8613e4c2 | 872 | int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke) |
f4f37c8e | 873 | { |
21d8dd0d | 874 | guard(spinlock_irqsave)(&dev->event_lock); |
2e2e3b96 | 875 | |
21d8dd0d | 876 | return dev->getkeycode(dev, ke); |
f4f37c8e DT |
877 | } |
878 | EXPORT_SYMBOL(input_get_keycode); | |
879 | ||
880 | /** | |
8613e4c2 | 881 | * input_set_keycode - attribute a keycode to a given scancode |
f4f37c8e | 882 | * @dev: input device which keymap is being updated |
8613e4c2 | 883 | * @ke: new keymap entry |
f4f37c8e DT |
884 | * |
885 | * This function should be called by anyone needing to update current | |
886 | * keymap. Presently keyboard and evdev handlers use it. | |
887 | */ | |
58b93995 | 888 | int input_set_keycode(struct input_dev *dev, |
8613e4c2 | 889 | const struct input_keymap_entry *ke) |
f4f37c8e | 890 | { |
fd6cf3dd | 891 | unsigned int old_keycode; |
21d8dd0d | 892 | int error; |
f4f37c8e | 893 | |
8613e4c2 | 894 | if (ke->keycode > KEY_MAX) |
f4f37c8e DT |
895 | return -EINVAL; |
896 | ||
21d8dd0d | 897 | guard(spinlock_irqsave)(&dev->event_lock); |
f4f37c8e | 898 | |
21d8dd0d DT |
899 | error = dev->setkeycode(dev, ke, &old_keycode); |
900 | if (error) | |
901 | return error; | |
f4f37c8e | 902 | |
4f93df40 DT |
903 | /* Make sure KEY_RESERVED did not get enabled. */ |
904 | __clear_bit(KEY_RESERVED, dev->keybit); | |
905 | ||
f4f37c8e DT |
906 | /* |
907 | * Simulate keyup event if keycode is not present | |
908 | * in the keymap anymore | |
909 | */ | |
cb222aed DT |
910 | if (old_keycode > KEY_MAX) { |
911 | dev_warn(dev->dev.parent ?: &dev->dev, | |
912 | "%s: got too big old keycode %#x\n", | |
913 | __func__, old_keycode); | |
914 | } else if (test_bit(EV_KEY, dev->evbit) && | |
915 | !is_event_supported(old_keycode, dev->keybit, KEY_MAX) && | |
916 | __test_and_clear_bit(old_keycode, dev->key)) { | |
59b7a5af DT |
917 | /* |
918 | * We have to use input_event_dispose() here directly instead | |
919 | * of input_handle_event() because the key we want to release | |
920 | * here is considered no longer supported by the device and | |
921 | * input_handle_event() will ignore it. | |
922 | */ | |
923 | input_event_dispose(dev, INPUT_PASS_TO_HANDLERS, | |
924 | EV_KEY, old_keycode, 0); | |
925 | input_event_dispose(dev, INPUT_PASS_TO_HANDLERS | INPUT_FLUSH, | |
926 | EV_SYN, SYN_REPORT, 1); | |
f4f37c8e DT |
927 | } |
928 | ||
21d8dd0d | 929 | return 0; |
f4f37c8e DT |
930 | } |
931 | EXPORT_SYMBOL(input_set_keycode); | |
c8e4c772 | 932 | |
55dfce87 DT |
933 | bool input_match_device_id(const struct input_dev *dev, |
934 | const struct input_device_id *id) | |
935 | { | |
936 | if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) | |
937 | if (id->bustype != dev->id.bustype) | |
938 | return false; | |
939 | ||
940 | if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) | |
941 | if (id->vendor != dev->id.vendor) | |
942 | return false; | |
943 | ||
944 | if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) | |
945 | if (id->product != dev->id.product) | |
946 | return false; | |
947 | ||
948 | if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) | |
949 | if (id->version != dev->id.version) | |
950 | return false; | |
951 | ||
952 | if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX) || | |
953 | !bitmap_subset(id->keybit, dev->keybit, KEY_MAX) || | |
954 | !bitmap_subset(id->relbit, dev->relbit, REL_MAX) || | |
955 | !bitmap_subset(id->absbit, dev->absbit, ABS_MAX) || | |
956 | !bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX) || | |
957 | !bitmap_subset(id->ledbit, dev->ledbit, LED_MAX) || | |
958 | !bitmap_subset(id->sndbit, dev->sndbit, SND_MAX) || | |
959 | !bitmap_subset(id->ffbit, dev->ffbit, FF_MAX) || | |
8724ecb0 DT |
960 | !bitmap_subset(id->swbit, dev->swbit, SW_MAX) || |
961 | !bitmap_subset(id->propbit, dev->propbit, INPUT_PROP_MAX)) { | |
55dfce87 DT |
962 | return false; |
963 | } | |
964 | ||
965 | return true; | |
966 | } | |
967 | EXPORT_SYMBOL(input_match_device_id); | |
968 | ||
0b7024ac | 969 | static const struct input_device_id *input_match_device(struct input_handler *handler, |
66e66118 | 970 | struct input_dev *dev) |
1da177e4 | 971 | { |
0b7024ac | 972 | const struct input_device_id *id; |
1da177e4 | 973 | |
0b7024ac | 974 | for (id = handler->id_table; id->flags || id->driver_info; id++) { |
55dfce87 DT |
975 | if (input_match_device_id(dev, id) && |
976 | (!handler->match || handler->match(handler, dev))) { | |
0b7024ac | 977 | return id; |
55dfce87 | 978 | } |
1da177e4 LT |
979 | } |
980 | ||
981 | return NULL; | |
982 | } | |
983 | ||
5b2a0826 DT |
984 | static int input_attach_handler(struct input_dev *dev, struct input_handler *handler) |
985 | { | |
986 | const struct input_device_id *id; | |
987 | int error; | |
988 | ||
0b7024ac | 989 | id = input_match_device(handler, dev); |
5b2a0826 DT |
990 | if (!id) |
991 | return -ENODEV; | |
992 | ||
993 | error = handler->connect(handler, dev, id); | |
994 | if (error && error != -ENODEV) | |
da0c4901 JP |
995 | pr_err("failed to attach handler %s to device %s, error: %d\n", |
996 | handler->name, kobject_name(&dev->dev.kobj), error); | |
5b2a0826 DT |
997 | |
998 | return error; | |
999 | } | |
1000 | ||
15e184af DT |
1001 | #ifdef CONFIG_COMPAT |
1002 | ||
1003 | static int input_bits_to_string(char *buf, int buf_size, | |
1004 | unsigned long bits, bool skip_empty) | |
1005 | { | |
1006 | int len = 0; | |
1007 | ||
b8b4ead1 | 1008 | if (in_compat_syscall()) { |
15e184af DT |
1009 | u32 dword = bits >> 32; |
1010 | if (dword || !skip_empty) | |
1011 | len += snprintf(buf, buf_size, "%x ", dword); | |
1012 | ||
1013 | dword = bits & 0xffffffffUL; | |
1014 | if (dword || !skip_empty || len) | |
1015 | len += snprintf(buf + len, max(buf_size - len, 0), | |
1016 | "%x", dword); | |
1017 | } else { | |
1018 | if (bits || !skip_empty) | |
1019 | len += snprintf(buf, buf_size, "%lx", bits); | |
1020 | } | |
1021 | ||
1022 | return len; | |
1023 | } | |
1024 | ||
1025 | #else /* !CONFIG_COMPAT */ | |
1026 | ||
1027 | static int input_bits_to_string(char *buf, int buf_size, | |
1028 | unsigned long bits, bool skip_empty) | |
1029 | { | |
1030 | return bits || !skip_empty ? | |
1031 | snprintf(buf, buf_size, "%lx", bits) : 0; | |
1032 | } | |
1033 | ||
1034 | #endif | |
5b2a0826 | 1035 | |
f96b434d DT |
1036 | #ifdef CONFIG_PROC_FS |
1037 | ||
1038 | static struct proc_dir_entry *proc_bus_input_dir; | |
1039 | static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait); | |
1040 | static int input_devices_state; | |
1041 | ||
1042 | static inline void input_wakeup_procfs_readers(void) | |
1043 | { | |
1044 | input_devices_state++; | |
1045 | wake_up(&input_devices_poll_wait); | |
1046 | } | |
1047 | ||
7a7ce8b3 CB |
1048 | struct input_seq_state { |
1049 | unsigned short pos; | |
1050 | bool mutex_acquired; | |
1051 | int input_devices_state; | |
1052 | }; | |
1053 | ||
afc9a42b | 1054 | static __poll_t input_proc_devices_poll(struct file *file, poll_table *wait) |
f96b434d | 1055 | { |
7a7ce8b3 CB |
1056 | struct seq_file *seq = file->private_data; |
1057 | struct input_seq_state *state = seq->private; | |
1058 | ||
f96b434d | 1059 | poll_wait(file, &input_devices_poll_wait, wait); |
7a7ce8b3 CB |
1060 | if (state->input_devices_state != input_devices_state) { |
1061 | state->input_devices_state = input_devices_state; | |
a9a08845 | 1062 | return EPOLLIN | EPOLLRDNORM; |
fa886612 | 1063 | } |
1e0afb28 | 1064 | |
f96b434d DT |
1065 | return 0; |
1066 | } | |
1067 | ||
969b21cd DT |
1068 | static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos) |
1069 | { | |
7a7ce8b3 | 1070 | struct input_seq_state *state = seq->private; |
1572ca2a DT |
1071 | int error; |
1072 | ||
1572ca2a DT |
1073 | error = mutex_lock_interruptible(&input_mutex); |
1074 | if (error) { | |
1075 | state->mutex_acquired = false; | |
1076 | return ERR_PTR(error); | |
1077 | } | |
1078 | ||
1079 | state->mutex_acquired = true; | |
f96b434d | 1080 | |
ad5d972c | 1081 | return seq_list_start(&input_dev_list, *pos); |
969b21cd | 1082 | } |
051b2fea | 1083 | |
969b21cd DT |
1084 | static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
1085 | { | |
ad5d972c | 1086 | return seq_list_next(v, &input_dev_list, pos); |
969b21cd | 1087 | } |
f96b434d | 1088 | |
1572ca2a | 1089 | static void input_seq_stop(struct seq_file *seq, void *v) |
969b21cd | 1090 | { |
7a7ce8b3 | 1091 | struct input_seq_state *state = seq->private; |
1572ca2a DT |
1092 | |
1093 | if (state->mutex_acquired) | |
1094 | mutex_unlock(&input_mutex); | |
969b21cd | 1095 | } |
f96b434d | 1096 | |
969b21cd DT |
1097 | static void input_seq_print_bitmap(struct seq_file *seq, const char *name, |
1098 | unsigned long *bitmap, int max) | |
1099 | { | |
1100 | int i; | |
15e184af DT |
1101 | bool skip_empty = true; |
1102 | char buf[18]; | |
f96b434d | 1103 | |
969b21cd | 1104 | seq_printf(seq, "B: %s=", name); |
15e184af DT |
1105 | |
1106 | for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) { | |
1107 | if (input_bits_to_string(buf, sizeof(buf), | |
1108 | bitmap[i], skip_empty)) { | |
1109 | skip_empty = false; | |
1110 | seq_printf(seq, "%s%s", buf, i > 0 ? " " : ""); | |
1111 | } | |
1112 | } | |
1113 | ||
1114 | /* | |
1115 | * If no output was produced print a single 0. | |
1116 | */ | |
1117 | if (skip_empty) | |
bb546136 | 1118 | seq_putc(seq, '0'); |
15e184af | 1119 | |
969b21cd DT |
1120 | seq_putc(seq, '\n'); |
1121 | } | |
f96b434d | 1122 | |
969b21cd DT |
1123 | static int input_devices_seq_show(struct seq_file *seq, void *v) |
1124 | { | |
1125 | struct input_dev *dev = container_of(v, struct input_dev, node); | |
9657d75c | 1126 | const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); |
969b21cd DT |
1127 | struct input_handle *handle; |
1128 | ||
1129 | seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n", | |
1130 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version); | |
1131 | ||
1132 | seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); | |
1133 | seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : ""); | |
1134 | seq_printf(seq, "S: Sysfs=%s\n", path ? path : ""); | |
15e03ae8 | 1135 | seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : ""); |
63c95765 | 1136 | seq_puts(seq, "H: Handlers="); |
969b21cd DT |
1137 | |
1138 | list_for_each_entry(handle, &dev->h_list, d_node) | |
1139 | seq_printf(seq, "%s ", handle->name); | |
1140 | seq_putc(seq, '\n'); | |
1141 | ||
85b77200 HR |
1142 | input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX); |
1143 | ||
969b21cd DT |
1144 | input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX); |
1145 | if (test_bit(EV_KEY, dev->evbit)) | |
1146 | input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX); | |
1147 | if (test_bit(EV_REL, dev->evbit)) | |
1148 | input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX); | |
1149 | if (test_bit(EV_ABS, dev->evbit)) | |
1150 | input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX); | |
1151 | if (test_bit(EV_MSC, dev->evbit)) | |
1152 | input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX); | |
1153 | if (test_bit(EV_LED, dev->evbit)) | |
1154 | input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX); | |
1155 | if (test_bit(EV_SND, dev->evbit)) | |
1156 | input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX); | |
1157 | if (test_bit(EV_FF, dev->evbit)) | |
1158 | input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX); | |
1159 | if (test_bit(EV_SW, dev->evbit)) | |
1160 | input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX); | |
1161 | ||
1162 | seq_putc(seq, '\n'); | |
1163 | ||
1164 | kfree(path); | |
1165 | return 0; | |
f96b434d DT |
1166 | } |
1167 | ||
cec69c37 | 1168 | static const struct seq_operations input_devices_seq_ops = { |
969b21cd DT |
1169 | .start = input_devices_seq_start, |
1170 | .next = input_devices_seq_next, | |
1572ca2a | 1171 | .stop = input_seq_stop, |
969b21cd DT |
1172 | .show = input_devices_seq_show, |
1173 | }; | |
1174 | ||
1175 | static int input_proc_devices_open(struct inode *inode, struct file *file) | |
f96b434d | 1176 | { |
7a7ce8b3 CB |
1177 | return seq_open_private(file, &input_devices_seq_ops, |
1178 | sizeof(struct input_seq_state)); | |
969b21cd DT |
1179 | } |
1180 | ||
97a32539 AD |
1181 | static const struct proc_ops input_devices_proc_ops = { |
1182 | .proc_open = input_proc_devices_open, | |
1183 | .proc_poll = input_proc_devices_poll, | |
1184 | .proc_read = seq_read, | |
1185 | .proc_lseek = seq_lseek, | |
7a7ce8b3 | 1186 | .proc_release = seq_release_private, |
969b21cd DT |
1187 | }; |
1188 | ||
1189 | static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos) | |
1190 | { | |
7a7ce8b3 | 1191 | struct input_seq_state *state = seq->private; |
1572ca2a DT |
1192 | int error; |
1193 | ||
1572ca2a DT |
1194 | error = mutex_lock_interruptible(&input_mutex); |
1195 | if (error) { | |
1196 | state->mutex_acquired = false; | |
1197 | return ERR_PTR(error); | |
1198 | } | |
1199 | ||
1200 | state->mutex_acquired = true; | |
1201 | state->pos = *pos; | |
8006479c | 1202 | |
ad5d972c | 1203 | return seq_list_start(&input_handler_list, *pos); |
969b21cd | 1204 | } |
f96b434d | 1205 | |
969b21cd DT |
1206 | static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
1207 | { | |
7a7ce8b3 | 1208 | struct input_seq_state *state = seq->private; |
f96b434d | 1209 | |
1572ca2a DT |
1210 | state->pos = *pos + 1; |
1211 | return seq_list_next(v, &input_handler_list, pos); | |
969b21cd DT |
1212 | } |
1213 | ||
1214 | static int input_handlers_seq_show(struct seq_file *seq, void *v) | |
1215 | { | |
1216 | struct input_handler *handler = container_of(v, struct input_handler, node); | |
7a7ce8b3 | 1217 | struct input_seq_state *state = seq->private; |
969b21cd | 1218 | |
1572ca2a | 1219 | seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name); |
ef7995f4 DT |
1220 | if (handler->filter) |
1221 | seq_puts(seq, " (filter)"); | |
7f8d4cad | 1222 | if (handler->legacy_minors) |
969b21cd DT |
1223 | seq_printf(seq, " Minor=%d", handler->minor); |
1224 | seq_putc(seq, '\n'); | |
1225 | ||
1226 | return 0; | |
1227 | } | |
1572ca2a | 1228 | |
cec69c37 | 1229 | static const struct seq_operations input_handlers_seq_ops = { |
969b21cd DT |
1230 | .start = input_handlers_seq_start, |
1231 | .next = input_handlers_seq_next, | |
1572ca2a | 1232 | .stop = input_seq_stop, |
969b21cd DT |
1233 | .show = input_handlers_seq_show, |
1234 | }; | |
1235 | ||
1236 | static int input_proc_handlers_open(struct inode *inode, struct file *file) | |
1237 | { | |
7a7ce8b3 CB |
1238 | return seq_open_private(file, &input_handlers_seq_ops, |
1239 | sizeof(struct input_seq_state)); | |
969b21cd DT |
1240 | } |
1241 | ||
97a32539 AD |
1242 | static const struct proc_ops input_handlers_proc_ops = { |
1243 | .proc_open = input_proc_handlers_open, | |
1244 | .proc_read = seq_read, | |
1245 | .proc_lseek = seq_lseek, | |
7a7ce8b3 | 1246 | .proc_release = seq_release_private, |
969b21cd | 1247 | }; |
f96b434d DT |
1248 | |
1249 | static int __init input_proc_init(void) | |
1250 | { | |
1251 | struct proc_dir_entry *entry; | |
1252 | ||
9c37066d | 1253 | proc_bus_input_dir = proc_mkdir("bus/input", NULL); |
f96b434d DT |
1254 | if (!proc_bus_input_dir) |
1255 | return -ENOMEM; | |
1256 | ||
c7705f34 | 1257 | entry = proc_create("devices", 0, proc_bus_input_dir, |
97a32539 | 1258 | &input_devices_proc_ops); |
f96b434d DT |
1259 | if (!entry) |
1260 | goto fail1; | |
1261 | ||
c7705f34 | 1262 | entry = proc_create("handlers", 0, proc_bus_input_dir, |
97a32539 | 1263 | &input_handlers_proc_ops); |
f96b434d DT |
1264 | if (!entry) |
1265 | goto fail2; | |
1266 | ||
f96b434d DT |
1267 | return 0; |
1268 | ||
1269 | fail2: remove_proc_entry("devices", proc_bus_input_dir); | |
9c37066d | 1270 | fail1: remove_proc_entry("bus/input", NULL); |
f96b434d DT |
1271 | return -ENOMEM; |
1272 | } | |
1273 | ||
beffbdc2 | 1274 | static void input_proc_exit(void) |
f96b434d DT |
1275 | { |
1276 | remove_proc_entry("devices", proc_bus_input_dir); | |
1277 | remove_proc_entry("handlers", proc_bus_input_dir); | |
9c37066d | 1278 | remove_proc_entry("bus/input", NULL); |
f96b434d DT |
1279 | } |
1280 | ||
1281 | #else /* !CONFIG_PROC_FS */ | |
1282 | static inline void input_wakeup_procfs_readers(void) { } | |
1283 | static inline int input_proc_init(void) { return 0; } | |
1284 | static inline void input_proc_exit(void) { } | |
1285 | #endif | |
1286 | ||
9657d75c DT |
1287 | #define INPUT_DEV_STRING_ATTR_SHOW(name) \ |
1288 | static ssize_t input_dev_show_##name(struct device *dev, \ | |
1289 | struct device_attribute *attr, \ | |
1290 | char *buf) \ | |
1291 | { \ | |
1292 | struct input_dev *input_dev = to_input_dev(dev); \ | |
1293 | \ | |
d4db8762 | 1294 | return sysfs_emit(buf, "%s\n", \ |
1295 | input_dev->name ? input_dev->name : ""); \ | |
9657d75c DT |
1296 | } \ |
1297 | static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL) | |
5c1e9a6a DT |
1298 | |
1299 | INPUT_DEV_STRING_ATTR_SHOW(name); | |
1300 | INPUT_DEV_STRING_ATTR_SHOW(phys); | |
1301 | INPUT_DEV_STRING_ATTR_SHOW(uniq); | |
1302 | ||
ac648a6a | 1303 | static int input_print_modalias_bits(char *buf, int size, |
162736b0 | 1304 | char name, const unsigned long *bm, |
ac648a6a | 1305 | unsigned int min_bit, unsigned int max_bit) |
1d8f430c | 1306 | { |
0774d190 DT |
1307 | int bit = min_bit; |
1308 | int len = 0; | |
1d8f430c | 1309 | |
ac648a6a | 1310 | len += snprintf(buf, max(size, 0), "%c", name); |
0774d190 DT |
1311 | for_each_set_bit_from(bit, bm, max_bit) |
1312 | len += snprintf(buf + len, max(size - len, 0), "%X,", bit); | |
1d8f430c RR |
1313 | return len; |
1314 | } | |
1315 | ||
0774d190 DT |
1316 | static int input_print_modalias_parts(char *buf, int size, int full_len, |
1317 | const struct input_dev *id) | |
1d8f430c | 1318 | { |
0774d190 | 1319 | int len, klen, remainder, space; |
1d8f430c | 1320 | |
ac648a6a DT |
1321 | len = snprintf(buf, max(size, 0), |
1322 | "input:b%04Xv%04Xp%04Xe%04X-", | |
1323 | id->id.bustype, id->id.vendor, | |
1324 | id->id.product, id->id.version); | |
1325 | ||
1326 | len += input_print_modalias_bits(buf + len, size - len, | |
1327 | 'e', id->evbit, 0, EV_MAX); | |
0774d190 DT |
1328 | |
1329 | /* | |
1330 | * Calculate the remaining space in the buffer making sure we | |
1331 | * have place for the terminating 0. | |
1332 | */ | |
1333 | space = max(size - (len + 1), 0); | |
1334 | ||
1335 | klen = input_print_modalias_bits(buf + len, size - len, | |
ac648a6a | 1336 | 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX); |
0774d190 DT |
1337 | len += klen; |
1338 | ||
1339 | /* | |
1340 | * If we have more data than we can fit in the buffer, check | |
1341 | * if we can trim key data to fit in the rest. We will indicate | |
1342 | * that key data is incomplete by adding "+" sign at the end, like | |
1343 | * this: * "k1,2,3,45,+,". | |
1344 | * | |
1345 | * Note that we shortest key info (if present) is "k+," so we | |
1346 | * can only try to trim if key data is longer than that. | |
1347 | */ | |
1348 | if (full_len && size < full_len + 1 && klen > 3) { | |
1349 | remainder = full_len - len; | |
1350 | /* | |
1351 | * We can only trim if we have space for the remainder | |
1352 | * and also for at least "k+," which is 3 more characters. | |
1353 | */ | |
1354 | if (remainder <= space - 3) { | |
1355 | /* | |
1356 | * We are guaranteed to have 'k' in the buffer, so | |
1357 | * we need at least 3 additional bytes for storing | |
1358 | * "+," in addition to the remainder. | |
1359 | */ | |
1360 | for (int i = size - 1 - remainder - 3; i >= 0; i--) { | |
1361 | if (buf[i] == 'k' || buf[i] == ',') { | |
1362 | strcpy(buf + i + 1, "+,"); | |
1363 | len = i + 3; /* Not counting '\0' */ | |
1364 | break; | |
1365 | } | |
1366 | } | |
1367 | } | |
1368 | } | |
1369 | ||
ac648a6a DT |
1370 | len += input_print_modalias_bits(buf + len, size - len, |
1371 | 'r', id->relbit, 0, REL_MAX); | |
1372 | len += input_print_modalias_bits(buf + len, size - len, | |
1373 | 'a', id->absbit, 0, ABS_MAX); | |
1374 | len += input_print_modalias_bits(buf + len, size - len, | |
1375 | 'm', id->mscbit, 0, MSC_MAX); | |
1376 | len += input_print_modalias_bits(buf + len, size - len, | |
1377 | 'l', id->ledbit, 0, LED_MAX); | |
1378 | len += input_print_modalias_bits(buf + len, size - len, | |
1379 | 's', id->sndbit, 0, SND_MAX); | |
1380 | len += input_print_modalias_bits(buf + len, size - len, | |
1381 | 'f', id->ffbit, 0, FF_MAX); | |
1382 | len += input_print_modalias_bits(buf + len, size - len, | |
1383 | 'w', id->swbit, 0, SW_MAX); | |
2db66876 | 1384 | |
bd37e5a9 KS |
1385 | return len; |
1386 | } | |
1387 | ||
0774d190 DT |
1388 | static int input_print_modalias(char *buf, int size, const struct input_dev *id) |
1389 | { | |
1390 | int full_len; | |
1391 | ||
1392 | /* | |
1393 | * Printing is done in 2 passes: first one figures out total length | |
1394 | * needed for the modalias string, second one will try to trim key | |
1395 | * data in case when buffer is too small for the entire modalias. | |
1396 | * If the buffer is too small regardless, it will fill as much as it | |
1397 | * can (without trimming key data) into the buffer and leave it to | |
1398 | * the caller to figure out what to do with the result. | |
1399 | */ | |
1400 | full_len = input_print_modalias_parts(NULL, 0, 0, id); | |
1401 | return input_print_modalias_parts(buf, size, full_len, id); | |
1402 | } | |
1403 | ||
9657d75c DT |
1404 | static ssize_t input_dev_show_modalias(struct device *dev, |
1405 | struct device_attribute *attr, | |
1406 | char *buf) | |
bd37e5a9 KS |
1407 | { |
1408 | struct input_dev *id = to_input_dev(dev); | |
1409 | ssize_t len; | |
1410 | ||
0774d190 DT |
1411 | len = input_print_modalias(buf, PAGE_SIZE, id); |
1412 | if (len < PAGE_SIZE - 2) | |
1413 | len += snprintf(buf + len, PAGE_SIZE - len, "\n"); | |
2db66876 | 1414 | |
8a3cf456 | 1415 | return min_t(int, len, PAGE_SIZE); |
1d8f430c | 1416 | } |
9657d75c | 1417 | static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL); |
1d8f430c | 1418 | |
162736b0 | 1419 | static int input_print_bitmap(char *buf, int buf_size, const unsigned long *bitmap, |
85b77200 HR |
1420 | int max, int add_cr); |
1421 | ||
1422 | static ssize_t input_dev_show_properties(struct device *dev, | |
1423 | struct device_attribute *attr, | |
1424 | char *buf) | |
1425 | { | |
1426 | struct input_dev *input_dev = to_input_dev(dev); | |
1427 | int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit, | |
1428 | INPUT_PROP_MAX, true); | |
1429 | return min_t(int, len, PAGE_SIZE); | |
1430 | } | |
1431 | static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL); | |
1432 | ||
a1816164 PF |
1433 | static int input_inhibit_device(struct input_dev *dev); |
1434 | static int input_uninhibit_device(struct input_dev *dev); | |
1435 | ||
1436 | static ssize_t inhibited_show(struct device *dev, | |
1437 | struct device_attribute *attr, | |
1438 | char *buf) | |
1439 | { | |
1440 | struct input_dev *input_dev = to_input_dev(dev); | |
1441 | ||
d4db8762 | 1442 | return sysfs_emit(buf, "%d\n", input_dev->inhibited); |
a1816164 PF |
1443 | } |
1444 | ||
1445 | static ssize_t inhibited_store(struct device *dev, | |
1446 | struct device_attribute *attr, const char *buf, | |
1447 | size_t len) | |
1448 | { | |
1449 | struct input_dev *input_dev = to_input_dev(dev); | |
1450 | ssize_t rv; | |
1451 | bool inhibited; | |
1452 | ||
60f07c29 | 1453 | if (kstrtobool(buf, &inhibited)) |
a1816164 PF |
1454 | return -EINVAL; |
1455 | ||
1456 | if (inhibited) | |
1457 | rv = input_inhibit_device(input_dev); | |
1458 | else | |
1459 | rv = input_uninhibit_device(input_dev); | |
1460 | ||
1461 | if (rv != 0) | |
1462 | return rv; | |
1463 | ||
1464 | return len; | |
1465 | } | |
1466 | ||
1467 | static DEVICE_ATTR_RW(inhibited); | |
1468 | ||
629b77a4 | 1469 | static struct attribute *input_dev_attrs[] = { |
9657d75c DT |
1470 | &dev_attr_name.attr, |
1471 | &dev_attr_phys.attr, | |
1472 | &dev_attr_uniq.attr, | |
1473 | &dev_attr_modalias.attr, | |
85b77200 | 1474 | &dev_attr_properties.attr, |
a1816164 | 1475 | &dev_attr_inhibited.attr, |
629b77a4 GKH |
1476 | NULL |
1477 | }; | |
1478 | ||
5e895b74 | 1479 | static const struct attribute_group input_dev_attr_group = { |
629b77a4 | 1480 | .attrs = input_dev_attrs, |
5c1e9a6a DT |
1481 | }; |
1482 | ||
9657d75c DT |
1483 | #define INPUT_DEV_ID_ATTR(name) \ |
1484 | static ssize_t input_dev_show_id_##name(struct device *dev, \ | |
1485 | struct device_attribute *attr, \ | |
1486 | char *buf) \ | |
1487 | { \ | |
1488 | struct input_dev *input_dev = to_input_dev(dev); \ | |
d4db8762 | 1489 | return sysfs_emit(buf, "%04x\n", input_dev->id.name); \ |
9657d75c DT |
1490 | } \ |
1491 | static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL) | |
5c1e9a6a DT |
1492 | |
1493 | INPUT_DEV_ID_ATTR(bustype); | |
1494 | INPUT_DEV_ID_ATTR(vendor); | |
1495 | INPUT_DEV_ID_ATTR(product); | |
1496 | INPUT_DEV_ID_ATTR(version); | |
1497 | ||
1498 | static struct attribute *input_dev_id_attrs[] = { | |
9657d75c DT |
1499 | &dev_attr_bustype.attr, |
1500 | &dev_attr_vendor.attr, | |
1501 | &dev_attr_product.attr, | |
1502 | &dev_attr_version.attr, | |
5c1e9a6a DT |
1503 | NULL |
1504 | }; | |
1505 | ||
5e895b74 | 1506 | static const struct attribute_group input_dev_id_attr_group = { |
5c1e9a6a DT |
1507 | .name = "id", |
1508 | .attrs = input_dev_id_attrs, | |
1509 | }; | |
1510 | ||
162736b0 | 1511 | static int input_print_bitmap(char *buf, int buf_size, const unsigned long *bitmap, |
969b21cd DT |
1512 | int max, int add_cr) |
1513 | { | |
1514 | int i; | |
1515 | int len = 0; | |
15e184af DT |
1516 | bool skip_empty = true; |
1517 | ||
1518 | for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) { | |
1519 | len += input_bits_to_string(buf + len, max(buf_size - len, 0), | |
1520 | bitmap[i], skip_empty); | |
1521 | if (len) { | |
1522 | skip_empty = false; | |
1523 | if (i > 0) | |
1524 | len += snprintf(buf + len, max(buf_size - len, 0), " "); | |
1525 | } | |
1526 | } | |
969b21cd | 1527 | |
15e184af DT |
1528 | /* |
1529 | * If no output was produced print a single 0. | |
1530 | */ | |
1531 | if (len == 0) | |
1532 | len = snprintf(buf, buf_size, "%d", 0); | |
969b21cd DT |
1533 | |
1534 | if (add_cr) | |
1535 | len += snprintf(buf + len, max(buf_size - len, 0), "\n"); | |
1536 | ||
1537 | return len; | |
1538 | } | |
1539 | ||
9657d75c DT |
1540 | #define INPUT_DEV_CAP_ATTR(ev, bm) \ |
1541 | static ssize_t input_dev_show_cap_##bm(struct device *dev, \ | |
1542 | struct device_attribute *attr, \ | |
1543 | char *buf) \ | |
1544 | { \ | |
1545 | struct input_dev *input_dev = to_input_dev(dev); \ | |
1546 | int len = input_print_bitmap(buf, PAGE_SIZE, \ | |
15e184af DT |
1547 | input_dev->bm##bit, ev##_MAX, \ |
1548 | true); \ | |
9657d75c DT |
1549 | return min_t(int, len, PAGE_SIZE); \ |
1550 | } \ | |
1551 | static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL) | |
5c1e9a6a DT |
1552 | |
1553 | INPUT_DEV_CAP_ATTR(EV, ev); | |
1554 | INPUT_DEV_CAP_ATTR(KEY, key); | |
1555 | INPUT_DEV_CAP_ATTR(REL, rel); | |
1556 | INPUT_DEV_CAP_ATTR(ABS, abs); | |
1557 | INPUT_DEV_CAP_ATTR(MSC, msc); | |
1558 | INPUT_DEV_CAP_ATTR(LED, led); | |
1559 | INPUT_DEV_CAP_ATTR(SND, snd); | |
1560 | INPUT_DEV_CAP_ATTR(FF, ff); | |
1561 | INPUT_DEV_CAP_ATTR(SW, sw); | |
1562 | ||
1563 | static struct attribute *input_dev_caps_attrs[] = { | |
9657d75c DT |
1564 | &dev_attr_ev.attr, |
1565 | &dev_attr_key.attr, | |
1566 | &dev_attr_rel.attr, | |
1567 | &dev_attr_abs.attr, | |
1568 | &dev_attr_msc.attr, | |
1569 | &dev_attr_led.attr, | |
1570 | &dev_attr_snd.attr, | |
1571 | &dev_attr_ff.attr, | |
1572 | &dev_attr_sw.attr, | |
5c1e9a6a DT |
1573 | NULL |
1574 | }; | |
1575 | ||
5e895b74 | 1576 | static const struct attribute_group input_dev_caps_attr_group = { |
5c1e9a6a DT |
1577 | .name = "capabilities", |
1578 | .attrs = input_dev_caps_attrs, | |
1579 | }; | |
1580 | ||
a4dbd674 | 1581 | static const struct attribute_group *input_dev_attr_groups[] = { |
cb9def4d DT |
1582 | &input_dev_attr_group, |
1583 | &input_dev_id_attr_group, | |
1584 | &input_dev_caps_attr_group, | |
e95656ea | 1585 | &input_poller_attribute_group, |
cb9def4d DT |
1586 | NULL |
1587 | }; | |
1588 | ||
9657d75c | 1589 | static void input_dev_release(struct device *device) |
d19fbe8a | 1590 | { |
9657d75c | 1591 | struct input_dev *dev = to_input_dev(device); |
d19fbe8a | 1592 | |
509ca1a9 | 1593 | input_ff_destroy(dev); |
40d007e7 | 1594 | input_mt_destroy_slots(dev); |
e95656ea | 1595 | kfree(dev->poller); |
d31b2865 | 1596 | kfree(dev->absinfo); |
4369c64c | 1597 | kfree(dev->vals); |
d19fbe8a | 1598 | kfree(dev); |
509ca1a9 | 1599 | |
d19fbe8a DT |
1600 | module_put(THIS_MODULE); |
1601 | } | |
1602 | ||
a7fadbe1 | 1603 | /* |
312c004d | 1604 | * Input uevent interface - loading event handlers based on |
a7fadbe1 DT |
1605 | * device bitfields. |
1606 | */ | |
7eff2e7a | 1607 | static int input_add_uevent_bm_var(struct kobj_uevent_env *env, |
162736b0 | 1608 | const char *name, const unsigned long *bitmap, int max) |
a7fadbe1 | 1609 | { |
7eff2e7a | 1610 | int len; |
a7fadbe1 | 1611 | |
fcd3027a | 1612 | if (add_uevent_var(env, "%s", name)) |
a7fadbe1 DT |
1613 | return -ENOMEM; |
1614 | ||
7eff2e7a KS |
1615 | len = input_print_bitmap(&env->buf[env->buflen - 1], |
1616 | sizeof(env->buf) - env->buflen, | |
15e184af | 1617 | bitmap, max, false); |
7eff2e7a | 1618 | if (len >= (sizeof(env->buf) - env->buflen)) |
a7fadbe1 DT |
1619 | return -ENOMEM; |
1620 | ||
7eff2e7a | 1621 | env->buflen += len; |
a7fadbe1 DT |
1622 | return 0; |
1623 | } | |
1624 | ||
0774d190 DT |
1625 | /* |
1626 | * This is a pretty gross hack. When building uevent data the driver core | |
1627 | * may try adding more environment variables to kobj_uevent_env without | |
1628 | * telling us, so we have no idea how much of the buffer we can use to | |
1629 | * avoid overflows/-ENOMEM elsewhere. To work around this let's artificially | |
1630 | * reduce amount of memory we will use for the modalias environment variable. | |
1631 | * | |
1632 | * The potential additions are: | |
1633 | * | |
1634 | * SEQNUM=18446744073709551615 - (%llu - 28 bytes) | |
1635 | * HOME=/ (6 bytes) | |
1636 | * PATH=/sbin:/bin:/usr/sbin:/usr/bin (34 bytes) | |
1637 | * | |
1638 | * 68 bytes total. Allow extra buffer - 96 bytes | |
1639 | */ | |
1640 | #define UEVENT_ENV_EXTRA_LEN 96 | |
1641 | ||
7eff2e7a | 1642 | static int input_add_uevent_modalias_var(struct kobj_uevent_env *env, |
162736b0 | 1643 | const struct input_dev *dev) |
ac648a6a | 1644 | { |
7eff2e7a | 1645 | int len; |
ac648a6a | 1646 | |
7eff2e7a | 1647 | if (add_uevent_var(env, "MODALIAS=")) |
ac648a6a DT |
1648 | return -ENOMEM; |
1649 | ||
7eff2e7a | 1650 | len = input_print_modalias(&env->buf[env->buflen - 1], |
0774d190 DT |
1651 | (int)sizeof(env->buf) - env->buflen - |
1652 | UEVENT_ENV_EXTRA_LEN, | |
1653 | dev); | |
1654 | if (len >= ((int)sizeof(env->buf) - env->buflen - | |
1655 | UEVENT_ENV_EXTRA_LEN)) | |
ac648a6a DT |
1656 | return -ENOMEM; |
1657 | ||
7eff2e7a | 1658 | env->buflen += len; |
ac648a6a DT |
1659 | return 0; |
1660 | } | |
1661 | ||
a7fadbe1 DT |
1662 | #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \ |
1663 | do { \ | |
7eff2e7a | 1664 | int err = add_uevent_var(env, fmt, val); \ |
a7fadbe1 DT |
1665 | if (err) \ |
1666 | return err; \ | |
1667 | } while (0) | |
1668 | ||
1669 | #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \ | |
1670 | do { \ | |
7eff2e7a | 1671 | int err = input_add_uevent_bm_var(env, name, bm, max); \ |
a7fadbe1 DT |
1672 | if (err) \ |
1673 | return err; \ | |
1674 | } while (0) | |
1675 | ||
ac648a6a DT |
1676 | #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \ |
1677 | do { \ | |
7eff2e7a | 1678 | int err = input_add_uevent_modalias_var(env, dev); \ |
ac648a6a DT |
1679 | if (err) \ |
1680 | return err; \ | |
1681 | } while (0) | |
1682 | ||
162736b0 | 1683 | static int input_dev_uevent(const struct device *device, struct kobj_uevent_env *env) |
a7fadbe1 | 1684 | { |
162736b0 | 1685 | const struct input_dev *dev = to_input_dev(device); |
a7fadbe1 DT |
1686 | |
1687 | INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x", | |
1688 | dev->id.bustype, dev->id.vendor, | |
1689 | dev->id.product, dev->id.version); | |
1690 | if (dev->name) | |
1691 | INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name); | |
1692 | if (dev->phys) | |
1693 | INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys); | |
08de1f04 | 1694 | if (dev->uniq) |
a7fadbe1 DT |
1695 | INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq); |
1696 | ||
85b77200 HR |
1697 | INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX); |
1698 | ||
a7fadbe1 DT |
1699 | INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX); |
1700 | if (test_bit(EV_KEY, dev->evbit)) | |
1701 | INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX); | |
1702 | if (test_bit(EV_REL, dev->evbit)) | |
1703 | INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX); | |
1704 | if (test_bit(EV_ABS, dev->evbit)) | |
1705 | INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX); | |
1706 | if (test_bit(EV_MSC, dev->evbit)) | |
1707 | INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX); | |
1708 | if (test_bit(EV_LED, dev->evbit)) | |
1709 | INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX); | |
1710 | if (test_bit(EV_SND, dev->evbit)) | |
1711 | INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX); | |
1712 | if (test_bit(EV_FF, dev->evbit)) | |
1713 | INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX); | |
1714 | if (test_bit(EV_SW, dev->evbit)) | |
1715 | INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX); | |
1716 | ||
ac648a6a | 1717 | INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev); |
a7fadbe1 DT |
1718 | |
1719 | return 0; | |
1720 | } | |
1721 | ||
3cc96351 DT |
1722 | #define INPUT_DO_TOGGLE(dev, type, bits, on) \ |
1723 | do { \ | |
1724 | int i; \ | |
1725 | bool active; \ | |
1726 | \ | |
1727 | if (!test_bit(EV_##type, dev->evbit)) \ | |
1728 | break; \ | |
1729 | \ | |
3e2b03da | 1730 | for_each_set_bit(i, dev->bits##bit, type##_CNT) { \ |
3cc96351 DT |
1731 | active = test_bit(i, dev->bits); \ |
1732 | if (!active && !on) \ | |
1733 | continue; \ | |
1734 | \ | |
1735 | dev->event(dev, EV_##type, i, on ? active : 0); \ | |
1736 | } \ | |
ffd0db97 DT |
1737 | } while (0) |
1738 | ||
b50b5216 | 1739 | static void input_dev_toggle(struct input_dev *dev, bool activate) |
ffd0db97 DT |
1740 | { |
1741 | if (!dev->event) | |
1742 | return; | |
1743 | ||
1744 | INPUT_DO_TOGGLE(dev, LED, led, activate); | |
1745 | INPUT_DO_TOGGLE(dev, SND, snd, activate); | |
1746 | ||
1747 | if (activate && test_bit(EV_REP, dev->evbit)) { | |
1748 | dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]); | |
1749 | dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]); | |
1750 | } | |
1751 | } | |
1752 | ||
b50b5216 DT |
1753 | /** |
1754 | * input_reset_device() - reset/restore the state of input device | |
1755 | * @dev: input device whose state needs to be reset | |
1756 | * | |
1757 | * This function tries to reset the state of an opened input device and | |
1758 | * bring internal state and state if the hardware in sync with each other. | |
1759 | * We mark all keys as released, restore LED state, repeat rate, etc. | |
1760 | */ | |
1761 | void input_reset_device(struct input_dev *dev) | |
1762 | { | |
21d8dd0d DT |
1763 | guard(mutex)(&dev->mutex); |
1764 | guard(spinlock_irqsave)(&dev->event_lock); | |
b50b5216 | 1765 | |
768d9aa5 | 1766 | input_dev_toggle(dev, true); |
ebfa0043 AC |
1767 | if (input_dev_release_keys(dev)) |
1768 | input_handle_event(dev, EV_SYN, SYN_REPORT, 1); | |
b50b5216 DT |
1769 | } |
1770 | EXPORT_SYMBOL(input_reset_device); | |
1771 | ||
a1816164 PF |
1772 | static int input_inhibit_device(struct input_dev *dev) |
1773 | { | |
21d8dd0d | 1774 | guard(mutex)(&dev->mutex); |
a1816164 PF |
1775 | |
1776 | if (dev->inhibited) | |
21d8dd0d | 1777 | return 0; |
a1816164 PF |
1778 | |
1779 | if (dev->users) { | |
1780 | if (dev->close) | |
1781 | dev->close(dev); | |
1782 | if (dev->poller) | |
1783 | input_dev_poller_stop(dev->poller); | |
1784 | } | |
1785 | ||
21d8dd0d DT |
1786 | scoped_guard(spinlock_irq, &dev->event_lock) { |
1787 | input_mt_release_slots(dev); | |
1788 | input_dev_release_keys(dev); | |
1789 | input_handle_event(dev, EV_SYN, SYN_REPORT, 1); | |
1790 | input_dev_toggle(dev, false); | |
1791 | } | |
a1816164 PF |
1792 | |
1793 | dev->inhibited = true; | |
1794 | ||
0a112e7c | 1795 | return 0; |
a1816164 PF |
1796 | } |
1797 | ||
1798 | static int input_uninhibit_device(struct input_dev *dev) | |
1799 | { | |
21d8dd0d | 1800 | int error; |
a1816164 | 1801 | |
21d8dd0d | 1802 | guard(mutex)(&dev->mutex); |
a1816164 PF |
1803 | |
1804 | if (!dev->inhibited) | |
21d8dd0d | 1805 | return 0; |
a1816164 PF |
1806 | |
1807 | if (dev->users) { | |
1808 | if (dev->open) { | |
21d8dd0d DT |
1809 | error = dev->open(dev); |
1810 | if (error) | |
1811 | return error; | |
a1816164 PF |
1812 | } |
1813 | if (dev->poller) | |
1814 | input_dev_poller_start(dev->poller); | |
1815 | } | |
1816 | ||
1817 | dev->inhibited = false; | |
a1816164 | 1818 | |
21d8dd0d DT |
1819 | scoped_guard(spinlock_irq, &dev->event_lock) |
1820 | input_dev_toggle(dev, true); | |
1821 | ||
1822 | return 0; | |
a1816164 PF |
1823 | } |
1824 | ||
ffd0db97 DT |
1825 | static int input_dev_suspend(struct device *dev) |
1826 | { | |
1827 | struct input_dev *input_dev = to_input_dev(dev); | |
1828 | ||
21d8dd0d | 1829 | guard(spinlock_irq)(&input_dev->event_lock); |
b50b5216 | 1830 | |
768d9aa5 AM |
1831 | /* |
1832 | * Keys that are pressed now are unlikely to be | |
1833 | * still pressed when we resume. | |
1834 | */ | |
ebfa0043 AC |
1835 | if (input_dev_release_keys(input_dev)) |
1836 | input_handle_event(input_dev, EV_SYN, SYN_REPORT, 1); | |
b50b5216 | 1837 | |
768d9aa5 AM |
1838 | /* Turn off LEDs and sounds, if any are active. */ |
1839 | input_dev_toggle(input_dev, false); | |
1840 | ||
ffd0db97 DT |
1841 | return 0; |
1842 | } | |
1843 | ||
1844 | static int input_dev_resume(struct device *dev) | |
1845 | { | |
1846 | struct input_dev *input_dev = to_input_dev(dev); | |
1847 | ||
21d8dd0d | 1848 | guard(spinlock_irq)(&input_dev->event_lock); |
768d9aa5 AM |
1849 | |
1850 | /* Restore state of LEDs and sounds, if any were active. */ | |
1851 | input_dev_toggle(input_dev, true); | |
1852 | ||
768d9aa5 AM |
1853 | return 0; |
1854 | } | |
1855 | ||
1856 | static int input_dev_freeze(struct device *dev) | |
1857 | { | |
1858 | struct input_dev *input_dev = to_input_dev(dev); | |
1859 | ||
21d8dd0d | 1860 | guard(spinlock_irq)(&input_dev->event_lock); |
768d9aa5 AM |
1861 | |
1862 | /* | |
1863 | * Keys that are pressed now are unlikely to be | |
1864 | * still pressed when we resume. | |
1865 | */ | |
ebfa0043 AC |
1866 | if (input_dev_release_keys(input_dev)) |
1867 | input_handle_event(input_dev, EV_SYN, SYN_REPORT, 1); | |
768d9aa5 | 1868 | |
768d9aa5 AM |
1869 | return 0; |
1870 | } | |
1871 | ||
1872 | static int input_dev_poweroff(struct device *dev) | |
1873 | { | |
1874 | struct input_dev *input_dev = to_input_dev(dev); | |
1875 | ||
21d8dd0d | 1876 | guard(spinlock_irq)(&input_dev->event_lock); |
768d9aa5 AM |
1877 | |
1878 | /* Turn off LEDs and sounds, if any are active. */ | |
1879 | input_dev_toggle(input_dev, false); | |
1880 | ||
ffd0db97 DT |
1881 | return 0; |
1882 | } | |
1883 | ||
1884 | static const struct dev_pm_ops input_dev_pm_ops = { | |
1885 | .suspend = input_dev_suspend, | |
1886 | .resume = input_dev_resume, | |
768d9aa5 AM |
1887 | .freeze = input_dev_freeze, |
1888 | .poweroff = input_dev_poweroff, | |
ffd0db97 DT |
1889 | .restore = input_dev_resume, |
1890 | }; | |
ffd0db97 | 1891 | |
f719315b | 1892 | static const struct device_type input_dev_type = { |
9657d75c DT |
1893 | .groups = input_dev_attr_groups, |
1894 | .release = input_dev_release, | |
1895 | .uevent = input_dev_uevent, | |
345c7b74 | 1896 | .pm = pm_sleep_ptr(&input_dev_pm_ops), |
9657d75c DT |
1897 | }; |
1898 | ||
ff62b8e6 | 1899 | static char *input_devnode(const struct device *dev, umode_t *mode) |
aa5ed63e KS |
1900 | { |
1901 | return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev)); | |
1902 | } | |
1903 | ||
a4735d40 | 1904 | const struct class input_class = { |
9657d75c | 1905 | .name = "input", |
e454cea2 | 1906 | .devnode = input_devnode, |
d19fbe8a | 1907 | }; |
ca56fe07 | 1908 | EXPORT_SYMBOL_GPL(input_class); |
d19fbe8a | 1909 | |
1447190e DT |
1910 | /** |
1911 | * input_allocate_device - allocate memory for new input device | |
1912 | * | |
2be975c6 | 1913 | * Returns prepared struct input_dev or %NULL. |
1447190e DT |
1914 | * |
1915 | * NOTE: Use input_free_device() to free devices that have not been | |
1916 | * registered; input_unregister_device() should be used for already | |
1917 | * registered devices. | |
1918 | */ | |
d19fbe8a DT |
1919 | struct input_dev *input_allocate_device(void) |
1920 | { | |
9c7d66fa | 1921 | static atomic_t input_no = ATOMIC_INIT(-1); |
d19fbe8a DT |
1922 | struct input_dev *dev; |
1923 | ||
c3f6f861 | 1924 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
3544cf57 DT |
1925 | if (!dev) |
1926 | return NULL; | |
1927 | ||
0cd58773 DT |
1928 | /* |
1929 | * Start with space for SYN_REPORT + 7 EV_KEY/EV_MSC events + 2 spare, | |
1930 | * see input_estimate_events_per_packet(). We will tune the number | |
1931 | * when we register the device. | |
1932 | */ | |
1933 | dev->max_vals = 10; | |
1934 | dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL); | |
1935 | if (!dev->vals) { | |
1936 | kfree(dev); | |
1937 | return NULL; | |
1938 | } | |
1939 | ||
3544cf57 DT |
1940 | mutex_init(&dev->mutex); |
1941 | spin_lock_init(&dev->event_lock); | |
1942 | timer_setup(&dev->timer, NULL, 0); | |
1943 | INIT_LIST_HEAD(&dev->h_list); | |
1944 | INIT_LIST_HEAD(&dev->node); | |
1945 | ||
1946 | dev->dev.type = &input_dev_type; | |
1947 | dev->dev.class = &input_class; | |
1948 | device_initialize(&dev->dev); | |
1949 | /* | |
1950 | * From this point on we can no longer simply "kfree(dev)", we need | |
1951 | * to use input_free_device() so that device core properly frees its | |
1952 | * resources associated with the input device. | |
1953 | */ | |
1954 | ||
1955 | dev_set_name(&dev->dev, "input%lu", | |
1956 | (unsigned long)atomic_inc_return(&input_no)); | |
1957 | ||
1958 | __module_get(THIS_MODULE); | |
d19fbe8a DT |
1959 | |
1960 | return dev; | |
1961 | } | |
ca56fe07 | 1962 | EXPORT_SYMBOL(input_allocate_device); |
d19fbe8a | 1963 | |
2be975c6 DT |
1964 | struct input_devres { |
1965 | struct input_dev *input; | |
1966 | }; | |
1967 | ||
1968 | static int devm_input_device_match(struct device *dev, void *res, void *data) | |
1969 | { | |
1970 | struct input_devres *devres = res; | |
1971 | ||
1972 | return devres->input == data; | |
1973 | } | |
1974 | ||
1975 | static void devm_input_device_release(struct device *dev, void *res) | |
1976 | { | |
1977 | struct input_devres *devres = res; | |
1978 | struct input_dev *input = devres->input; | |
1979 | ||
1980 | dev_dbg(dev, "%s: dropping reference to %s\n", | |
1981 | __func__, dev_name(&input->dev)); | |
1982 | input_put_device(input); | |
1983 | } | |
1984 | ||
1985 | /** | |
1986 | * devm_input_allocate_device - allocate managed input device | |
1987 | * @dev: device owning the input device being created | |
1988 | * | |
1989 | * Returns prepared struct input_dev or %NULL. | |
1990 | * | |
1991 | * Managed input devices do not need to be explicitly unregistered or | |
1992 | * freed as it will be done automatically when owner device unbinds from | |
1993 | * its driver (or binding fails). Once managed input device is allocated, | |
1994 | * it is ready to be set up and registered in the same fashion as regular | |
1995 | * input device. There are no special devm_input_device_[un]register() | |
b666263b DT |
1996 | * variants, regular ones work with both managed and unmanaged devices, |
1997 | * should you need them. In most cases however, managed input device need | |
1998 | * not be explicitly unregistered or freed. | |
2be975c6 DT |
1999 | * |
2000 | * NOTE: the owner device is set up as parent of input device and users | |
2001 | * should not override it. | |
2002 | */ | |
2be975c6 DT |
2003 | struct input_dev *devm_input_allocate_device(struct device *dev) |
2004 | { | |
2005 | struct input_dev *input; | |
2006 | struct input_devres *devres; | |
2007 | ||
2008 | devres = devres_alloc(devm_input_device_release, | |
c3f6f861 | 2009 | sizeof(*devres), GFP_KERNEL); |
2be975c6 DT |
2010 | if (!devres) |
2011 | return NULL; | |
2012 | ||
2013 | input = input_allocate_device(); | |
2014 | if (!input) { | |
2015 | devres_free(devres); | |
2016 | return NULL; | |
2017 | } | |
2018 | ||
2019 | input->dev.parent = dev; | |
2020 | input->devres_managed = true; | |
2021 | ||
2022 | devres->input = input; | |
2023 | devres_add(dev, devres); | |
2024 | ||
2025 | return input; | |
2026 | } | |
2027 | EXPORT_SYMBOL(devm_input_allocate_device); | |
2028 | ||
1447190e DT |
2029 | /** |
2030 | * input_free_device - free memory occupied by input_dev structure | |
2031 | * @dev: input device to free | |
2032 | * | |
2033 | * This function should only be used if input_register_device() | |
2034 | * was not called yet or if it failed. Once device was registered | |
2035 | * use input_unregister_device() and memory will be freed once last | |
8006479c | 2036 | * reference to the device is dropped. |
1447190e DT |
2037 | * |
2038 | * Device should be allocated by input_allocate_device(). | |
2039 | * | |
2040 | * NOTE: If there are references to the input device then memory | |
2041 | * will not be freed until last reference is dropped. | |
2042 | */ | |
f60d2b11 DT |
2043 | void input_free_device(struct input_dev *dev) |
2044 | { | |
2be975c6 DT |
2045 | if (dev) { |
2046 | if (dev->devres_managed) | |
2047 | WARN_ON(devres_destroy(dev->dev.parent, | |
2048 | devm_input_device_release, | |
2049 | devm_input_device_match, | |
2050 | dev)); | |
f60d2b11 | 2051 | input_put_device(dev); |
2be975c6 | 2052 | } |
f60d2b11 | 2053 | } |
ca56fe07 | 2054 | EXPORT_SYMBOL(input_free_device); |
f60d2b11 | 2055 | |
3b51c44b AN |
2056 | /** |
2057 | * input_set_timestamp - set timestamp for input events | |
2058 | * @dev: input device to set timestamp for | |
2059 | * @timestamp: the time at which the event has occurred | |
2060 | * in CLOCK_MONOTONIC | |
2061 | * | |
2062 | * This function is intended to provide to the input system a more | |
2063 | * accurate time of when an event actually occurred. The driver should | |
2064 | * call this function as soon as a timestamp is acquired ensuring | |
2065 | * clock conversions in input_set_timestamp are done correctly. | |
2066 | * | |
2067 | * The system entering suspend state between timestamp acquisition and | |
2068 | * calling input_set_timestamp can result in inaccurate conversions. | |
2069 | */ | |
2070 | void input_set_timestamp(struct input_dev *dev, ktime_t timestamp) | |
2071 | { | |
2072 | dev->timestamp[INPUT_CLK_MONO] = timestamp; | |
2073 | dev->timestamp[INPUT_CLK_REAL] = ktime_mono_to_real(timestamp); | |
2074 | dev->timestamp[INPUT_CLK_BOOT] = ktime_mono_to_any(timestamp, | |
2075 | TK_OFFS_BOOT); | |
2076 | } | |
2077 | EXPORT_SYMBOL(input_set_timestamp); | |
2078 | ||
2079 | /** | |
2080 | * input_get_timestamp - get timestamp for input events | |
2081 | * @dev: input device to get timestamp from | |
2082 | * | |
2083 | * A valid timestamp is a timestamp of non-zero value. | |
2084 | */ | |
2085 | ktime_t *input_get_timestamp(struct input_dev *dev) | |
2086 | { | |
2087 | const ktime_t invalid_timestamp = ktime_set(0, 0); | |
2088 | ||
2089 | if (!ktime_compare(dev->timestamp[INPUT_CLK_MONO], invalid_timestamp)) | |
2090 | input_set_timestamp(dev, ktime_get()); | |
2091 | ||
2092 | return dev->timestamp; | |
2093 | } | |
2094 | EXPORT_SYMBOL(input_get_timestamp); | |
2095 | ||
534565f2 DT |
2096 | /** |
2097 | * input_set_capability - mark device as capable of a certain event | |
2098 | * @dev: device that is capable of emitting or accepting event | |
2099 | * @type: type of the event (EV_KEY, EV_REL, etc...) | |
2100 | * @code: event code | |
2101 | * | |
2102 | * In addition to setting up corresponding bit in appropriate capability | |
2103 | * bitmap the function also adjusts dev->evbit. | |
2104 | */ | |
2105 | void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code) | |
2106 | { | |
409353cb JL |
2107 | if (type < EV_CNT && input_max_code[type] && |
2108 | code > input_max_code[type]) { | |
2109 | pr_err("%s: invalid code %u for type %u\n", __func__, code, | |
2110 | type); | |
2111 | dump_stack(); | |
2112 | return; | |
2113 | } | |
2114 | ||
534565f2 DT |
2115 | switch (type) { |
2116 | case EV_KEY: | |
2117 | __set_bit(code, dev->keybit); | |
2118 | break; | |
2119 | ||
2120 | case EV_REL: | |
2121 | __set_bit(code, dev->relbit); | |
2122 | break; | |
2123 | ||
2124 | case EV_ABS: | |
28a2a2e1 | 2125 | input_alloc_absinfo(dev); |
534565f2 DT |
2126 | __set_bit(code, dev->absbit); |
2127 | break; | |
2128 | ||
2129 | case EV_MSC: | |
2130 | __set_bit(code, dev->mscbit); | |
2131 | break; | |
2132 | ||
2133 | case EV_SW: | |
2134 | __set_bit(code, dev->swbit); | |
2135 | break; | |
2136 | ||
2137 | case EV_LED: | |
2138 | __set_bit(code, dev->ledbit); | |
2139 | break; | |
2140 | ||
2141 | case EV_SND: | |
2142 | __set_bit(code, dev->sndbit); | |
2143 | break; | |
2144 | ||
2145 | case EV_FF: | |
2146 | __set_bit(code, dev->ffbit); | |
2147 | break; | |
2148 | ||
22d1c398 DB |
2149 | case EV_PWR: |
2150 | /* do nothing */ | |
2151 | break; | |
2152 | ||
534565f2 | 2153 | default: |
67043f41 | 2154 | pr_err("%s: unknown type %u (code %u)\n", __func__, type, code); |
534565f2 DT |
2155 | dump_stack(); |
2156 | return; | |
2157 | } | |
2158 | ||
2159 | __set_bit(type, dev->evbit); | |
2160 | } | |
2161 | EXPORT_SYMBOL(input_set_capability); | |
2162 | ||
80b4895a JB |
2163 | static unsigned int input_estimate_events_per_packet(struct input_dev *dev) |
2164 | { | |
2165 | int mt_slots; | |
2166 | int i; | |
2167 | unsigned int events; | |
2168 | ||
8d18fba2 HR |
2169 | if (dev->mt) { |
2170 | mt_slots = dev->mt->num_slots; | |
80b4895a JB |
2171 | } else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) { |
2172 | mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum - | |
55bef835 | 2173 | dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1; |
8c127f07 | 2174 | mt_slots = clamp(mt_slots, 2, 32); |
80b4895a JB |
2175 | } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) { |
2176 | mt_slots = 2; | |
2177 | } else { | |
2178 | mt_slots = 0; | |
2179 | } | |
2180 | ||
2181 | events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */ | |
2182 | ||
3e2b03da AG |
2183 | if (test_bit(EV_ABS, dev->evbit)) |
2184 | for_each_set_bit(i, dev->absbit, ABS_CNT) | |
2185 | events += input_is_mt_axis(i) ? mt_slots : 1; | |
80b4895a | 2186 | |
3e2b03da AG |
2187 | if (test_bit(EV_REL, dev->evbit)) |
2188 | events += bitmap_weight(dev->relbit, REL_CNT); | |
80b4895a | 2189 | |
7c75bf99 HR |
2190 | /* Make room for KEY and MSC events */ |
2191 | events += 7; | |
2192 | ||
80b4895a JB |
2193 | return events; |
2194 | } | |
2195 | ||
92a3a587 DT |
2196 | #define INPUT_CLEANSE_BITMASK(dev, type, bits) \ |
2197 | do { \ | |
2198 | if (!test_bit(EV_##type, dev->evbit)) \ | |
2199 | memset(dev->bits##bit, 0, \ | |
2200 | sizeof(dev->bits##bit)); \ | |
2201 | } while (0) | |
2202 | ||
2203 | static void input_cleanse_bitmasks(struct input_dev *dev) | |
2204 | { | |
2205 | INPUT_CLEANSE_BITMASK(dev, KEY, key); | |
2206 | INPUT_CLEANSE_BITMASK(dev, REL, rel); | |
2207 | INPUT_CLEANSE_BITMASK(dev, ABS, abs); | |
2208 | INPUT_CLEANSE_BITMASK(dev, MSC, msc); | |
2209 | INPUT_CLEANSE_BITMASK(dev, LED, led); | |
2210 | INPUT_CLEANSE_BITMASK(dev, SND, snd); | |
2211 | INPUT_CLEANSE_BITMASK(dev, FF, ff); | |
2212 | INPUT_CLEANSE_BITMASK(dev, SW, sw); | |
2213 | } | |
2214 | ||
2be975c6 DT |
2215 | static void __input_unregister_device(struct input_dev *dev) |
2216 | { | |
2217 | struct input_handle *handle, *next; | |
2218 | ||
2219 | input_disconnect_device(dev); | |
2220 | ||
21d8dd0d DT |
2221 | scoped_guard(mutex, &input_mutex) { |
2222 | list_for_each_entry_safe(handle, next, &dev->h_list, d_node) | |
2223 | handle->handler->disconnect(handle); | |
2224 | WARN_ON(!list_empty(&dev->h_list)); | |
2be975c6 | 2225 | |
8fa7292f | 2226 | timer_delete_sync(&dev->timer); |
21d8dd0d | 2227 | list_del_init(&dev->node); |
2be975c6 | 2228 | |
21d8dd0d DT |
2229 | input_wakeup_procfs_readers(); |
2230 | } | |
2be975c6 DT |
2231 | |
2232 | device_del(&dev->dev); | |
2233 | } | |
2234 | ||
2235 | static void devm_input_device_unregister(struct device *dev, void *res) | |
2236 | { | |
2237 | struct input_devres *devres = res; | |
2238 | struct input_dev *input = devres->input; | |
2239 | ||
2240 | dev_dbg(dev, "%s: unregistering device %s\n", | |
2241 | __func__, dev_name(&input->dev)); | |
2242 | __input_unregister_device(input); | |
2243 | } | |
2244 | ||
59b7a5af DT |
2245 | /* |
2246 | * Generate software autorepeat event. Note that we take | |
2247 | * dev->event_lock here to avoid racing with input_event | |
2248 | * which may cause keys get "stuck". | |
2249 | */ | |
2250 | static void input_repeat_key(struct timer_list *t) | |
2251 | { | |
41cb0855 | 2252 | struct input_dev *dev = timer_container_of(dev, t, timer); |
59b7a5af | 2253 | |
21d8dd0d | 2254 | guard(spinlock_irqsave)(&dev->event_lock); |
59b7a5af DT |
2255 | |
2256 | if (!dev->inhibited && | |
2257 | test_bit(dev->repeat_key, dev->key) && | |
2258 | is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) { | |
2259 | ||
2260 | input_set_timestamp(dev, ktime_get()); | |
2261 | input_handle_event(dev, EV_KEY, dev->repeat_key, 2); | |
2262 | input_handle_event(dev, EV_SYN, SYN_REPORT, 1); | |
2263 | ||
2264 | if (dev->rep[REP_PERIOD]) | |
2265 | mod_timer(&dev->timer, jiffies + | |
2266 | msecs_to_jiffies(dev->rep[REP_PERIOD])); | |
2267 | } | |
59b7a5af DT |
2268 | } |
2269 | ||
027c71bb PG |
2270 | /** |
2271 | * input_enable_softrepeat - enable software autorepeat | |
2272 | * @dev: input device | |
2273 | * @delay: repeat delay | |
2274 | * @period: repeat period | |
2275 | * | |
2276 | * Enable software autorepeat on the input device. | |
2277 | */ | |
2278 | void input_enable_softrepeat(struct input_dev *dev, int delay, int period) | |
2279 | { | |
841b86f3 | 2280 | dev->timer.function = input_repeat_key; |
027c71bb PG |
2281 | dev->rep[REP_DELAY] = delay; |
2282 | dev->rep[REP_PERIOD] = period; | |
2283 | } | |
2284 | EXPORT_SYMBOL(input_enable_softrepeat); | |
2285 | ||
39be39ce AP |
2286 | bool input_device_enabled(struct input_dev *dev) |
2287 | { | |
2288 | lockdep_assert_held(&dev->mutex); | |
2289 | ||
a1816164 | 2290 | return !dev->inhibited && dev->users > 0; |
39be39ce AP |
2291 | } |
2292 | EXPORT_SYMBOL_GPL(input_device_enabled); | |
2293 | ||
0cd58773 DT |
2294 | static int input_device_tune_vals(struct input_dev *dev) |
2295 | { | |
2296 | struct input_value *vals; | |
2297 | unsigned int packet_size; | |
2298 | unsigned int max_vals; | |
2299 | ||
2300 | packet_size = input_estimate_events_per_packet(dev); | |
2301 | if (dev->hint_events_per_packet < packet_size) | |
2302 | dev->hint_events_per_packet = packet_size; | |
2303 | ||
2304 | max_vals = dev->hint_events_per_packet + 2; | |
2305 | if (dev->max_vals >= max_vals) | |
2306 | return 0; | |
2307 | ||
2308 | vals = kcalloc(max_vals, sizeof(*vals), GFP_KERNEL); | |
2309 | if (!vals) | |
2310 | return -ENOMEM; | |
2311 | ||
21d8dd0d DT |
2312 | scoped_guard(spinlock_irq, &dev->event_lock) { |
2313 | dev->max_vals = max_vals; | |
2314 | swap(dev->vals, vals); | |
2315 | } | |
0cd58773 DT |
2316 | |
2317 | /* Because of swap() above, this frees the old vals memory */ | |
2318 | kfree(vals); | |
2319 | ||
2320 | return 0; | |
2321 | } | |
2322 | ||
8006479c DT |
2323 | /** |
2324 | * input_register_device - register device with input core | |
2325 | * @dev: device to be registered | |
2326 | * | |
2327 | * This function registers device with input core. The device must be | |
2328 | * allocated with input_allocate_device() and all it's capabilities | |
2329 | * set up before registering. | |
2330 | * If function fails the device must be freed with input_free_device(). | |
2331 | * Once device has been successfully registered it can be unregistered | |
2332 | * with input_unregister_device(); input_free_device() should not be | |
2333 | * called in this case. | |
b666263b DT |
2334 | * |
2335 | * Note that this function is also used to register managed input devices | |
2336 | * (ones allocated with devm_input_allocate_device()). Such managed input | |
2337 | * devices need not be explicitly unregistered or freed, their tear down | |
2338 | * is controlled by the devres infrastructure. It is also worth noting | |
2339 | * that tear down of managed input devices is internally a 2-step process: | |
2340 | * registered managed input device is first unregistered, but stays in | |
2341 | * memory and can still handle input_event() calls (although events will | |
2342 | * not be delivered anywhere). The freeing of managed input device will | |
2343 | * happen later, when devres stack is unwound to the point where device | |
2344 | * allocation was made. | |
8006479c | 2345 | */ |
5f945489 | 2346 | int input_register_device(struct input_dev *dev) |
1da177e4 | 2347 | { |
2be975c6 | 2348 | struct input_devres *devres = NULL; |
1da177e4 | 2349 | struct input_handler *handler; |
bd0ef235 DT |
2350 | const char *path; |
2351 | int error; | |
1da177e4 | 2352 | |
6ecfe51b DT |
2353 | if (test_bit(EV_ABS, dev->evbit) && !dev->absinfo) { |
2354 | dev_err(&dev->dev, | |
2355 | "Absolute device without dev->absinfo, refusing to register\n"); | |
2356 | return -EINVAL; | |
2357 | } | |
2358 | ||
2be975c6 DT |
2359 | if (dev->devres_managed) { |
2360 | devres = devres_alloc(devm_input_device_unregister, | |
c3f6f861 | 2361 | sizeof(*devres), GFP_KERNEL); |
2be975c6 DT |
2362 | if (!devres) |
2363 | return -ENOMEM; | |
2364 | ||
2365 | devres->input = dev; | |
2366 | } | |
2367 | ||
4f93df40 | 2368 | /* Every input device generates EV_SYN/SYN_REPORT events. */ |
8006479c | 2369 | __set_bit(EV_SYN, dev->evbit); |
0fbf87ca | 2370 | |
4f93df40 DT |
2371 | /* KEY_RESERVED is not supposed to be transmitted to userspace. */ |
2372 | __clear_bit(KEY_RESERVED, dev->keybit); | |
2373 | ||
92a3a587 DT |
2374 | /* Make sure that bitmasks not mentioned in dev->evbit are clean. */ |
2375 | input_cleanse_bitmasks(dev); | |
2376 | ||
0cd58773 DT |
2377 | error = input_device_tune_vals(dev); |
2378 | if (error) | |
2be975c6 | 2379 | goto err_devres_free; |
80b4895a | 2380 | |
1da177e4 LT |
2381 | /* |
2382 | * If delay and period are pre-set by the driver, then autorepeating | |
2383 | * is handled by the driver itself and we don't do it in input.c. | |
2384 | */ | |
027c71bb PG |
2385 | if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) |
2386 | input_enable_softrepeat(dev, 250, 33); | |
1da177e4 | 2387 | |
aebd636b DT |
2388 | if (!dev->getkeycode) |
2389 | dev->getkeycode = input_default_getkeycode; | |
c8e4c772 | 2390 | |
aebd636b DT |
2391 | if (!dev->setkeycode) |
2392 | dev->setkeycode = input_default_setkeycode; | |
c8e4c772 | 2393 | |
e95656ea DT |
2394 | if (dev->poller) |
2395 | input_dev_poller_finalize(dev->poller); | |
2396 | ||
9657d75c | 2397 | error = device_add(&dev->dev); |
bd0ef235 | 2398 | if (error) |
0cd58773 | 2399 | goto err_devres_free; |
bd0ef235 | 2400 | |
9657d75c | 2401 | path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); |
da0c4901 JP |
2402 | pr_info("%s as %s\n", |
2403 | dev->name ? dev->name : "Unspecified device", | |
2404 | path ? path : "N/A"); | |
bd0ef235 | 2405 | kfree(path); |
10204020 | 2406 | |
21d8dd0d DT |
2407 | error = -EINTR; |
2408 | scoped_cond_guard(mutex_intr, goto err_device_del, &input_mutex) { | |
2409 | list_add_tail(&dev->node, &input_dev_list); | |
8006479c | 2410 | |
21d8dd0d DT |
2411 | list_for_each_entry(handler, &input_handler_list, node) |
2412 | input_attach_handler(dev, handler); | |
8006479c | 2413 | |
21d8dd0d DT |
2414 | input_wakeup_procfs_readers(); |
2415 | } | |
8006479c | 2416 | |
2be975c6 DT |
2417 | if (dev->devres_managed) { |
2418 | dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n", | |
2419 | __func__, dev_name(&dev->dev)); | |
2420 | devres_add(dev->dev.parent, devres); | |
2421 | } | |
5f945489 | 2422 | return 0; |
2be975c6 DT |
2423 | |
2424 | err_device_del: | |
2425 | device_del(&dev->dev); | |
2be975c6 DT |
2426 | err_devres_free: |
2427 | devres_free(devres); | |
2428 | return error; | |
1da177e4 | 2429 | } |
ca56fe07 | 2430 | EXPORT_SYMBOL(input_register_device); |
1da177e4 | 2431 | |
8006479c DT |
2432 | /** |
2433 | * input_unregister_device - unregister previously registered device | |
2434 | * @dev: device to be unregistered | |
2435 | * | |
2436 | * This function unregisters an input device. Once device is unregistered | |
2437 | * the caller should not try to access it as it may get freed at any moment. | |
2438 | */ | |
1da177e4 LT |
2439 | void input_unregister_device(struct input_dev *dev) |
2440 | { | |
2be975c6 DT |
2441 | if (dev->devres_managed) { |
2442 | WARN_ON(devres_destroy(dev->dev.parent, | |
2443 | devm_input_device_unregister, | |
2444 | devm_input_device_match, | |
2445 | dev)); | |
2446 | __input_unregister_device(dev); | |
2447 | /* | |
2448 | * We do not do input_put_device() here because it will be done | |
2449 | * when 2nd devres fires up. | |
2450 | */ | |
2451 | } else { | |
2452 | __input_unregister_device(dev); | |
2453 | input_put_device(dev); | |
2454 | } | |
1da177e4 | 2455 | } |
ca56fe07 | 2456 | EXPORT_SYMBOL(input_unregister_device); |
1da177e4 | 2457 | |
a184cf98 DT |
2458 | static int input_handler_check_methods(const struct input_handler *handler) |
2459 | { | |
2460 | int count = 0; | |
2461 | ||
2462 | if (handler->filter) | |
2463 | count++; | |
2464 | if (handler->events) | |
2465 | count++; | |
2466 | if (handler->event) | |
2467 | count++; | |
2468 | ||
2469 | if (count > 1) { | |
2470 | pr_err("%s: only one event processing method can be defined (%s)\n", | |
2471 | __func__, handler->name); | |
2472 | return -EINVAL; | |
2473 | } | |
2474 | ||
2475 | return 0; | |
2476 | } | |
2477 | ||
8006479c DT |
2478 | /** |
2479 | * input_register_handler - register a new input handler | |
2480 | * @handler: handler to be registered | |
2481 | * | |
2482 | * This function registers a new input handler (interface) for input | |
2483 | * devices in the system and attaches it to all input devices that | |
2484 | * are compatible with the handler. | |
2485 | */ | |
4263cf0f | 2486 | int input_register_handler(struct input_handler *handler) |
1da177e4 LT |
2487 | { |
2488 | struct input_dev *dev; | |
7f8d4cad | 2489 | int error; |
8006479c | 2490 | |
a184cf98 DT |
2491 | error = input_handler_check_methods(handler); |
2492 | if (error) | |
2493 | return error; | |
2494 | ||
21d8dd0d DT |
2495 | scoped_cond_guard(mutex_intr, return -EINTR, &input_mutex) { |
2496 | INIT_LIST_HEAD(&handler->h_list); | |
d469647b | 2497 | |
21d8dd0d | 2498 | list_add_tail(&handler->node, &input_handler_list); |
1da177e4 | 2499 | |
21d8dd0d DT |
2500 | list_for_each_entry(dev, &input_dev_list, node) |
2501 | input_attach_handler(dev, handler); | |
1da177e4 | 2502 | |
21d8dd0d DT |
2503 | input_wakeup_procfs_readers(); |
2504 | } | |
8006479c | 2505 | |
7f8d4cad | 2506 | return 0; |
1da177e4 | 2507 | } |
ca56fe07 | 2508 | EXPORT_SYMBOL(input_register_handler); |
1da177e4 | 2509 | |
8006479c DT |
2510 | /** |
2511 | * input_unregister_handler - unregisters an input handler | |
2512 | * @handler: handler to be unregistered | |
2513 | * | |
2514 | * This function disconnects a handler from its input devices and | |
2515 | * removes it from lists of known handlers. | |
2516 | */ | |
1da177e4 LT |
2517 | void input_unregister_handler(struct input_handler *handler) |
2518 | { | |
5b2a0826 | 2519 | struct input_handle *handle, *next; |
1da177e4 | 2520 | |
21d8dd0d | 2521 | guard(mutex)(&input_mutex); |
8006479c | 2522 | |
5b2a0826 | 2523 | list_for_each_entry_safe(handle, next, &handler->h_list, h_node) |
1da177e4 | 2524 | handler->disconnect(handle); |
5b2a0826 | 2525 | WARN_ON(!list_empty(&handler->h_list)); |
1da177e4 LT |
2526 | |
2527 | list_del_init(&handler->node); | |
2528 | ||
f96b434d | 2529 | input_wakeup_procfs_readers(); |
1da177e4 | 2530 | } |
ca56fe07 | 2531 | EXPORT_SYMBOL(input_unregister_handler); |
1da177e4 | 2532 | |
66d2a595 DT |
2533 | /** |
2534 | * input_handler_for_each_handle - handle iterator | |
2535 | * @handler: input handler to iterate | |
2536 | * @data: data for the callback | |
2537 | * @fn: function to be called for each handle | |
2538 | * | |
2539 | * Iterate over @bus's list of devices, and call @fn for each, passing | |
2540 | * it @data and stop when @fn returns a non-zero value. The function is | |
ec8beff9 | 2541 | * using RCU to traverse the list and therefore may be using in atomic |
66d2a595 DT |
2542 | * contexts. The @fn callback is invoked from RCU critical section and |
2543 | * thus must not sleep. | |
2544 | */ | |
2545 | int input_handler_for_each_handle(struct input_handler *handler, void *data, | |
2546 | int (*fn)(struct input_handle *, void *)) | |
2547 | { | |
2548 | struct input_handle *handle; | |
21d8dd0d | 2549 | int retval; |
66d2a595 | 2550 | |
21d8dd0d | 2551 | guard(rcu)(); |
66d2a595 DT |
2552 | |
2553 | list_for_each_entry_rcu(handle, &handler->h_list, h_node) { | |
2554 | retval = fn(handle, data); | |
2555 | if (retval) | |
21d8dd0d | 2556 | return retval; |
66d2a595 DT |
2557 | } |
2558 | ||
21d8dd0d | 2559 | return 0; |
66d2a595 DT |
2560 | } |
2561 | EXPORT_SYMBOL(input_handler_for_each_handle); | |
2562 | ||
071b24b5 DT |
2563 | /* |
2564 | * An implementation of input_handle's handle_events() method that simply | |
2565 | * invokes handler->event() method for each event one by one. | |
2566 | */ | |
2567 | static unsigned int input_handle_events_default(struct input_handle *handle, | |
2568 | struct input_value *vals, | |
2569 | unsigned int count) | |
2570 | { | |
2571 | struct input_handler *handler = handle->handler; | |
2572 | struct input_value *v; | |
2573 | ||
2574 | for (v = vals; v != vals + count; v++) | |
2575 | handler->event(handle, v->type, v->code, v->value); | |
2576 | ||
2577 | return count; | |
2578 | } | |
2579 | ||
2580 | /* | |
2581 | * An implementation of input_handle's handle_events() method that invokes | |
2582 | * handler->filter() method for each event one by one and removes events | |
2583 | * that were filtered out from the "vals" array. | |
2584 | */ | |
2585 | static unsigned int input_handle_events_filter(struct input_handle *handle, | |
2586 | struct input_value *vals, | |
2587 | unsigned int count) | |
2588 | { | |
2589 | struct input_handler *handler = handle->handler; | |
2590 | struct input_value *end = vals; | |
2591 | struct input_value *v; | |
2592 | ||
2593 | for (v = vals; v != vals + count; v++) { | |
2594 | if (handler->filter(handle, v->type, v->code, v->value)) | |
2595 | continue; | |
2596 | if (end != v) | |
2597 | *end = *v; | |
2598 | end++; | |
2599 | } | |
2600 | ||
2601 | return end - vals; | |
2602 | } | |
2603 | ||
2604 | /* | |
2605 | * An implementation of input_handle's handle_events() method that does nothing. | |
2606 | */ | |
2607 | static unsigned int input_handle_events_null(struct input_handle *handle, | |
2608 | struct input_value *vals, | |
2609 | unsigned int count) | |
2610 | { | |
2611 | return count; | |
2612 | } | |
2613 | ||
2614 | /* | |
2615 | * Sets up appropriate handle->event_handler based on the input_handler | |
2616 | * associated with the handle. | |
2617 | */ | |
2618 | static void input_handle_setup_event_handler(struct input_handle *handle) | |
2619 | { | |
2620 | struct input_handler *handler = handle->handler; | |
2621 | ||
2622 | if (handler->filter) | |
2623 | handle->handle_events = input_handle_events_filter; | |
2624 | else if (handler->event) | |
2625 | handle->handle_events = input_handle_events_default; | |
2626 | else if (handler->events) | |
2627 | handle->handle_events = handler->events; | |
2628 | else | |
2629 | handle->handle_events = input_handle_events_null; | |
2630 | } | |
2631 | ||
8006479c DT |
2632 | /** |
2633 | * input_register_handle - register a new input handle | |
2634 | * @handle: handle to register | |
2635 | * | |
2636 | * This function puts a new input handle onto device's | |
2637 | * and handler's lists so that events can flow through | |
2638 | * it once it is opened using input_open_device(). | |
2639 | * | |
2640 | * This function is supposed to be called from handler's | |
2641 | * connect() method. | |
2642 | */ | |
5b2a0826 DT |
2643 | int input_register_handle(struct input_handle *handle) |
2644 | { | |
2645 | struct input_handler *handler = handle->handler; | |
8006479c | 2646 | struct input_dev *dev = handle->dev; |
8006479c | 2647 | |
071b24b5 | 2648 | input_handle_setup_event_handler(handle); |
8006479c DT |
2649 | /* |
2650 | * We take dev->mutex here to prevent race with | |
2651 | * input_release_device(). | |
2652 | */ | |
21d8dd0d DT |
2653 | scoped_cond_guard(mutex_intr, return -EINTR, &dev->mutex) { |
2654 | /* | |
2655 | * Filters go to the head of the list, normal handlers | |
2656 | * to the tail. | |
2657 | */ | |
2658 | if (handler->filter) | |
2659 | list_add_rcu(&handle->d_node, &dev->h_list); | |
2660 | else | |
2661 | list_add_tail_rcu(&handle->d_node, &dev->h_list); | |
2662 | } | |
5b2a0826 | 2663 | |
8006479c DT |
2664 | /* |
2665 | * Since we are supposed to be called from ->connect() | |
2666 | * which is mutually exclusive with ->disconnect() | |
2667 | * we can't be racing with input_unregister_handle() | |
2668 | * and so separate lock is not needed here. | |
2669 | */ | |
66d2a595 | 2670 | list_add_tail_rcu(&handle->h_node, &handler->h_list); |
5b2a0826 DT |
2671 | |
2672 | if (handler->start) | |
2673 | handler->start(handle); | |
2674 | ||
2675 | return 0; | |
2676 | } | |
2677 | EXPORT_SYMBOL(input_register_handle); | |
2678 | ||
8006479c DT |
2679 | /** |
2680 | * input_unregister_handle - unregister an input handle | |
2681 | * @handle: handle to unregister | |
2682 | * | |
2683 | * This function removes input handle from device's | |
2684 | * and handler's lists. | |
2685 | * | |
2686 | * This function is supposed to be called from handler's | |
2687 | * disconnect() method. | |
2688 | */ | |
5b2a0826 DT |
2689 | void input_unregister_handle(struct input_handle *handle) |
2690 | { | |
8006479c DT |
2691 | struct input_dev *dev = handle->dev; |
2692 | ||
66d2a595 | 2693 | list_del_rcu(&handle->h_node); |
8006479c DT |
2694 | |
2695 | /* | |
2696 | * Take dev->mutex to prevent race with input_release_device(). | |
2697 | */ | |
21d8dd0d DT |
2698 | scoped_guard(mutex, &dev->mutex) |
2699 | list_del_rcu(&handle->d_node); | |
66d2a595 | 2700 | |
82ba56c2 | 2701 | synchronize_rcu(); |
5b2a0826 DT |
2702 | } |
2703 | EXPORT_SYMBOL(input_unregister_handle); | |
2704 | ||
7f8d4cad DT |
2705 | /** |
2706 | * input_get_new_minor - allocates a new input minor number | |
2707 | * @legacy_base: beginning or the legacy range to be searched | |
2708 | * @legacy_num: size of legacy range | |
2709 | * @allow_dynamic: whether we can also take ID from the dynamic range | |
2710 | * | |
2711 | * This function allocates a new device minor for from input major namespace. | |
2712 | * Caller can request legacy minor by specifying @legacy_base and @legacy_num | |
2713 | * parameters and whether ID can be allocated from dynamic range if there are | |
2714 | * no free IDs in legacy range. | |
2715 | */ | |
2716 | int input_get_new_minor(int legacy_base, unsigned int legacy_num, | |
2717 | bool allow_dynamic) | |
1da177e4 | 2718 | { |
1da177e4 | 2719 | /* |
7f8d4cad DT |
2720 | * This function should be called from input handler's ->connect() |
2721 | * methods, which are serialized with input_mutex, so no additional | |
2722 | * locking is needed here. | |
1da177e4 | 2723 | */ |
7f8d4cad | 2724 | if (legacy_base >= 0) { |
2a992413 CJ |
2725 | int minor = ida_alloc_range(&input_ida, legacy_base, |
2726 | legacy_base + legacy_num - 1, | |
2727 | GFP_KERNEL); | |
7f8d4cad DT |
2728 | if (minor >= 0 || !allow_dynamic) |
2729 | return minor; | |
1da177e4 | 2730 | } |
2f2177c8 | 2731 | |
2a992413 CJ |
2732 | return ida_alloc_range(&input_ida, INPUT_FIRST_DYNAMIC_DEV, |
2733 | INPUT_MAX_CHAR_DEVICES - 1, GFP_KERNEL); | |
1da177e4 | 2734 | } |
7f8d4cad | 2735 | EXPORT_SYMBOL(input_get_new_minor); |
1da177e4 | 2736 | |
7f8d4cad DT |
2737 | /** |
2738 | * input_free_minor - release previously allocated minor | |
2739 | * @minor: minor to be released | |
2740 | * | |
2741 | * This function releases previously allocated input minor so that it can be | |
2742 | * reused later. | |
2743 | */ | |
2744 | void input_free_minor(unsigned int minor) | |
2745 | { | |
2a992413 | 2746 | ida_free(&input_ida, minor); |
7f8d4cad DT |
2747 | } |
2748 | EXPORT_SYMBOL(input_free_minor); | |
1da177e4 | 2749 | |
f96b434d | 2750 | static int __init input_init(void) |
1da177e4 | 2751 | { |
f96b434d | 2752 | int err; |
1da177e4 | 2753 | |
ea9f240b | 2754 | err = class_register(&input_class); |
d19fbe8a | 2755 | if (err) { |
da0c4901 | 2756 | pr_err("unable to register input_dev class\n"); |
d19fbe8a DT |
2757 | return err; |
2758 | } | |
2759 | ||
f96b434d DT |
2760 | err = input_proc_init(); |
2761 | if (err) | |
b0fdfebb | 2762 | goto fail1; |
1da177e4 | 2763 | |
7f8d4cad DT |
2764 | err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0), |
2765 | INPUT_MAX_CHAR_DEVICES, "input"); | |
f96b434d | 2766 | if (err) { |
da0c4901 | 2767 | pr_err("unable to register char major %d", INPUT_MAJOR); |
b0fdfebb | 2768 | goto fail2; |
1da177e4 | 2769 | } |
e334016f | 2770 | |
1da177e4 | 2771 | return 0; |
1da177e4 | 2772 | |
b0fdfebb | 2773 | fail2: input_proc_exit(); |
ea9f240b | 2774 | fail1: class_unregister(&input_class); |
f96b434d | 2775 | return err; |
1da177e4 LT |
2776 | } |
2777 | ||
2778 | static void __exit input_exit(void) | |
2779 | { | |
f96b434d | 2780 | input_proc_exit(); |
7f8d4cad DT |
2781 | unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0), |
2782 | INPUT_MAX_CHAR_DEVICES); | |
ea9f240b | 2783 | class_unregister(&input_class); |
1da177e4 LT |
2784 | } |
2785 | ||
2786 | subsys_initcall(input_init); | |
2787 | module_exit(input_exit); |