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