Input: gamecon - simplify pad type handling
[safe/jmp/linux-2.6] / drivers / input / input.c
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 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/input.h>
16 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/major.h>
19 #include <linux/proc_fs.h>
20 #include <linux/sched.h>
21 #include <linux/seq_file.h>
22 #include <linux/poll.h>
23 #include <linux/device.h>
24 #include <linux/mutex.h>
25 #include <linux/rcupdate.h>
26 #include <linux/smp_lock.h>
27 #include "input-compat.h"
28
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
30 MODULE_DESCRIPTION("Input core");
31 MODULE_LICENSE("GPL");
32
33 #define INPUT_DEVICES   256
34
35 /*
36  * EV_ABS events which should not be cached are listed here.
37  */
38 static unsigned int input_abs_bypass_init_data[] __initdata = {
39         ABS_MT_TOUCH_MAJOR,
40         ABS_MT_TOUCH_MINOR,
41         ABS_MT_WIDTH_MAJOR,
42         ABS_MT_WIDTH_MINOR,
43         ABS_MT_ORIENTATION,
44         ABS_MT_POSITION_X,
45         ABS_MT_POSITION_Y,
46         ABS_MT_TOOL_TYPE,
47         ABS_MT_BLOB_ID,
48         ABS_MT_TRACKING_ID,
49         0
50 };
51 static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
52
53 static LIST_HEAD(input_dev_list);
54 static LIST_HEAD(input_handler_list);
55
56 /*
57  * input_mutex protects access to both input_dev_list and input_handler_list.
58  * This also causes input_[un]register_device and input_[un]register_handler
59  * be mutually exclusive which simplifies locking in drivers implementing
60  * input handlers.
61  */
62 static DEFINE_MUTEX(input_mutex);
63
64 static struct input_handler *input_table[8];
65
66 static inline int is_event_supported(unsigned int code,
67                                      unsigned long *bm, unsigned int max)
68 {
69         return code <= max && test_bit(code, bm);
70 }
71
72 static int input_defuzz_abs_event(int value, int old_val, int fuzz)
73 {
74         if (fuzz) {
75                 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
76                         return old_val;
77
78                 if (value > old_val - fuzz && value < old_val + fuzz)
79                         return (old_val * 3 + value) / 4;
80
81                 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
82                         return (old_val + value) / 2;
83         }
84
85         return value;
86 }
87
88 /*
89  * Pass event first through all filters and then, if event has not been
90  * filtered out, through all open handles. This function is called with
91  * dev->event_lock held and interrupts disabled.
92  */
93 static void input_pass_event(struct input_dev *dev,
94                              unsigned int type, unsigned int code, int value)
95 {
96         struct input_handler *handler;
97         struct input_handle *handle;
98
99         rcu_read_lock();
100
101         handle = rcu_dereference(dev->grab);
102         if (handle)
103                 handle->handler->event(handle, type, code, value);
104         else {
105                 bool filtered = false;
106
107                 list_for_each_entry_rcu(handle, &dev->h_list, d_node) {
108                         if (!handle->open)
109                                 continue;
110
111                         handler = handle->handler;
112                         if (!handler->filter) {
113                                 if (filtered)
114                                         break;
115
116                                 handler->event(handle, type, code, value);
117
118                         } else if (handler->filter(handle, type, code, value))
119                                 filtered = true;
120                 }
121         }
122
123         rcu_read_unlock();
124 }
125
126 /*
127  * Generate software autorepeat event. Note that we take
128  * dev->event_lock here to avoid racing with input_event
129  * which may cause keys get "stuck".
130  */
131 static void input_repeat_key(unsigned long data)
132 {
133         struct input_dev *dev = (void *) data;
134         unsigned long flags;
135
136         spin_lock_irqsave(&dev->event_lock, flags);
137
138         if (test_bit(dev->repeat_key, dev->key) &&
139             is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
140
141                 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
142
143                 if (dev->sync) {
144                         /*
145                          * Only send SYN_REPORT if we are not in a middle
146                          * of driver parsing a new hardware packet.
147                          * Otherwise assume that the driver will send
148                          * SYN_REPORT once it's done.
149                          */
150                         input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
151                 }
152
153                 if (dev->rep[REP_PERIOD])
154                         mod_timer(&dev->timer, jiffies +
155                                         msecs_to_jiffies(dev->rep[REP_PERIOD]));
156         }
157
158         spin_unlock_irqrestore(&dev->event_lock, flags);
159 }
160
161 static void input_start_autorepeat(struct input_dev *dev, int code)
162 {
163         if (test_bit(EV_REP, dev->evbit) &&
164             dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
165             dev->timer.data) {
166                 dev->repeat_key = code;
167                 mod_timer(&dev->timer,
168                           jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
169         }
170 }
171
172 static void input_stop_autorepeat(struct input_dev *dev)
173 {
174         del_timer(&dev->timer);
175 }
176
177 #define INPUT_IGNORE_EVENT      0
178 #define INPUT_PASS_TO_HANDLERS  1
179 #define INPUT_PASS_TO_DEVICE    2
180 #define INPUT_PASS_TO_ALL       (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
181
182 static void input_handle_event(struct input_dev *dev,
183                                unsigned int type, unsigned int code, int value)
184 {
185         int disposition = INPUT_IGNORE_EVENT;
186
187         switch (type) {
188
189         case EV_SYN:
190                 switch (code) {
191                 case SYN_CONFIG:
192                         disposition = INPUT_PASS_TO_ALL;
193                         break;
194
195                 case SYN_REPORT:
196                         if (!dev->sync) {
197                                 dev->sync = 1;
198                                 disposition = INPUT_PASS_TO_HANDLERS;
199                         }
200                         break;
201                 case SYN_MT_REPORT:
202                         dev->sync = 0;
203                         disposition = INPUT_PASS_TO_HANDLERS;
204                         break;
205                 }
206                 break;
207
208         case EV_KEY:
209                 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
210                     !!test_bit(code, dev->key) != value) {
211
212                         if (value != 2) {
213                                 __change_bit(code, dev->key);
214                                 if (value)
215                                         input_start_autorepeat(dev, code);
216                                 else
217                                         input_stop_autorepeat(dev);
218                         }
219
220                         disposition = INPUT_PASS_TO_HANDLERS;
221                 }
222                 break;
223
224         case EV_SW:
225                 if (is_event_supported(code, dev->swbit, SW_MAX) &&
226                     !!test_bit(code, dev->sw) != value) {
227
228                         __change_bit(code, dev->sw);
229                         disposition = INPUT_PASS_TO_HANDLERS;
230                 }
231                 break;
232
233         case EV_ABS:
234                 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
235
236                         if (test_bit(code, input_abs_bypass)) {
237                                 disposition = INPUT_PASS_TO_HANDLERS;
238                                 break;
239                         }
240
241                         value = input_defuzz_abs_event(value,
242                                         dev->abs[code], dev->absfuzz[code]);
243
244                         if (dev->abs[code] != value) {
245                                 dev->abs[code] = value;
246                                 disposition = INPUT_PASS_TO_HANDLERS;
247                         }
248                 }
249                 break;
250
251         case EV_REL:
252                 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
253                         disposition = INPUT_PASS_TO_HANDLERS;
254
255                 break;
256
257         case EV_MSC:
258                 if (is_event_supported(code, dev->mscbit, MSC_MAX))
259                         disposition = INPUT_PASS_TO_ALL;
260
261                 break;
262
263         case EV_LED:
264                 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
265                     !!test_bit(code, dev->led) != value) {
266
267                         __change_bit(code, dev->led);
268                         disposition = INPUT_PASS_TO_ALL;
269                 }
270                 break;
271
272         case EV_SND:
273                 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
274
275                         if (!!test_bit(code, dev->snd) != !!value)
276                                 __change_bit(code, dev->snd);
277                         disposition = INPUT_PASS_TO_ALL;
278                 }
279                 break;
280
281         case EV_REP:
282                 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
283                         dev->rep[code] = value;
284                         disposition = INPUT_PASS_TO_ALL;
285                 }
286                 break;
287
288         case EV_FF:
289                 if (value >= 0)
290                         disposition = INPUT_PASS_TO_ALL;
291                 break;
292
293         case EV_PWR:
294                 disposition = INPUT_PASS_TO_ALL;
295                 break;
296         }
297
298         if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
299                 dev->sync = 0;
300
301         if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
302                 dev->event(dev, type, code, value);
303
304         if (disposition & INPUT_PASS_TO_HANDLERS)
305                 input_pass_event(dev, type, code, value);
306 }
307
308 /**
309  * input_event() - report new input event
310  * @dev: device that generated the event
311  * @type: type of the event
312  * @code: event code
313  * @value: value of the event
314  *
315  * This function should be used by drivers implementing various input
316  * devices to report input events. See also input_inject_event().
317  *
318  * NOTE: input_event() may be safely used right after input device was
319  * allocated with input_allocate_device(), even before it is registered
320  * with input_register_device(), but the event will not reach any of the
321  * input handlers. Such early invocation of input_event() may be used
322  * to 'seed' initial state of a switch or initial position of absolute
323  * axis, etc.
324  */
325 void input_event(struct input_dev *dev,
326                  unsigned int type, unsigned int code, int value)
327 {
328         unsigned long flags;
329
330         if (is_event_supported(type, dev->evbit, EV_MAX)) {
331
332                 spin_lock_irqsave(&dev->event_lock, flags);
333                 add_input_randomness(type, code, value);
334                 input_handle_event(dev, type, code, value);
335                 spin_unlock_irqrestore(&dev->event_lock, flags);
336         }
337 }
338 EXPORT_SYMBOL(input_event);
339
340 /**
341  * input_inject_event() - send input event from input handler
342  * @handle: input handle to send event through
343  * @type: type of the event
344  * @code: event code
345  * @value: value of the event
346  *
347  * Similar to input_event() but will ignore event if device is
348  * "grabbed" and handle injecting event is not the one that owns
349  * the device.
350  */
351 void input_inject_event(struct input_handle *handle,
352                         unsigned int type, unsigned int code, int value)
353 {
354         struct input_dev *dev = handle->dev;
355         struct input_handle *grab;
356         unsigned long flags;
357
358         if (is_event_supported(type, dev->evbit, EV_MAX)) {
359                 spin_lock_irqsave(&dev->event_lock, flags);
360
361                 rcu_read_lock();
362                 grab = rcu_dereference(dev->grab);
363                 if (!grab || grab == handle)
364                         input_handle_event(dev, type, code, value);
365                 rcu_read_unlock();
366
367                 spin_unlock_irqrestore(&dev->event_lock, flags);
368         }
369 }
370 EXPORT_SYMBOL(input_inject_event);
371
372 /**
373  * input_grab_device - grabs device for exclusive use
374  * @handle: input handle that wants to own the device
375  *
376  * When a device is grabbed by an input handle all events generated by
377  * the device are delivered only to this handle. Also events injected
378  * by other input handles are ignored while device is grabbed.
379  */
380 int input_grab_device(struct input_handle *handle)
381 {
382         struct input_dev *dev = handle->dev;
383         int retval;
384
385         retval = mutex_lock_interruptible(&dev->mutex);
386         if (retval)
387                 return retval;
388
389         if (dev->grab) {
390                 retval = -EBUSY;
391                 goto out;
392         }
393
394         rcu_assign_pointer(dev->grab, handle);
395         synchronize_rcu();
396
397  out:
398         mutex_unlock(&dev->mutex);
399         return retval;
400 }
401 EXPORT_SYMBOL(input_grab_device);
402
403 static void __input_release_device(struct input_handle *handle)
404 {
405         struct input_dev *dev = handle->dev;
406
407         if (dev->grab == handle) {
408                 rcu_assign_pointer(dev->grab, NULL);
409                 /* Make sure input_pass_event() notices that grab is gone */
410                 synchronize_rcu();
411
412                 list_for_each_entry(handle, &dev->h_list, d_node)
413                         if (handle->open && handle->handler->start)
414                                 handle->handler->start(handle);
415         }
416 }
417
418 /**
419  * input_release_device - release previously grabbed device
420  * @handle: input handle that owns the device
421  *
422  * Releases previously grabbed device so that other input handles can
423  * start receiving input events. Upon release all handlers attached
424  * to the device have their start() method called so they have a change
425  * to synchronize device state with the rest of the system.
426  */
427 void input_release_device(struct input_handle *handle)
428 {
429         struct input_dev *dev = handle->dev;
430
431         mutex_lock(&dev->mutex);
432         __input_release_device(handle);
433         mutex_unlock(&dev->mutex);
434 }
435 EXPORT_SYMBOL(input_release_device);
436
437 /**
438  * input_open_device - open input device
439  * @handle: handle through which device is being accessed
440  *
441  * This function should be called by input handlers when they
442  * want to start receive events from given input device.
443  */
444 int input_open_device(struct input_handle *handle)
445 {
446         struct input_dev *dev = handle->dev;
447         int retval;
448
449         retval = mutex_lock_interruptible(&dev->mutex);
450         if (retval)
451                 return retval;
452
453         if (dev->going_away) {
454                 retval = -ENODEV;
455                 goto out;
456         }
457
458         handle->open++;
459
460         if (!dev->users++ && dev->open)
461                 retval = dev->open(dev);
462
463         if (retval) {
464                 dev->users--;
465                 if (!--handle->open) {
466                         /*
467                          * Make sure we are not delivering any more events
468                          * through this handle
469                          */
470                         synchronize_rcu();
471                 }
472         }
473
474  out:
475         mutex_unlock(&dev->mutex);
476         return retval;
477 }
478 EXPORT_SYMBOL(input_open_device);
479
480 int input_flush_device(struct input_handle *handle, struct file *file)
481 {
482         struct input_dev *dev = handle->dev;
483         int retval;
484
485         retval = mutex_lock_interruptible(&dev->mutex);
486         if (retval)
487                 return retval;
488
489         if (dev->flush)
490                 retval = dev->flush(dev, file);
491
492         mutex_unlock(&dev->mutex);
493         return retval;
494 }
495 EXPORT_SYMBOL(input_flush_device);
496
497 /**
498  * input_close_device - close input device
499  * @handle: handle through which device is being accessed
500  *
501  * This function should be called by input handlers when they
502  * want to stop receive events from given input device.
503  */
504 void input_close_device(struct input_handle *handle)
505 {
506         struct input_dev *dev = handle->dev;
507
508         mutex_lock(&dev->mutex);
509
510         __input_release_device(handle);
511
512         if (!--dev->users && dev->close)
513                 dev->close(dev);
514
515         if (!--handle->open) {
516                 /*
517                  * synchronize_rcu() makes sure that input_pass_event()
518                  * completed and that no more input events are delivered
519                  * through this handle
520                  */
521                 synchronize_rcu();
522         }
523
524         mutex_unlock(&dev->mutex);
525 }
526 EXPORT_SYMBOL(input_close_device);
527
528 /*
529  * Prepare device for unregistering
530  */
531 static void input_disconnect_device(struct input_dev *dev)
532 {
533         struct input_handle *handle;
534         int code;
535
536         /*
537          * Mark device as going away. Note that we take dev->mutex here
538          * not to protect access to dev->going_away but rather to ensure
539          * that there are no threads in the middle of input_open_device()
540          */
541         mutex_lock(&dev->mutex);
542         dev->going_away = true;
543         mutex_unlock(&dev->mutex);
544
545         spin_lock_irq(&dev->event_lock);
546
547         /*
548          * Simulate keyup events for all pressed keys so that handlers
549          * are not left with "stuck" keys. The driver may continue
550          * generate events even after we done here but they will not
551          * reach any handlers.
552          */
553         if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
554                 for (code = 0; code <= KEY_MAX; code++) {
555                         if (is_event_supported(code, dev->keybit, KEY_MAX) &&
556                             __test_and_clear_bit(code, dev->key)) {
557                                 input_pass_event(dev, EV_KEY, code, 0);
558                         }
559                 }
560                 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
561         }
562
563         list_for_each_entry(handle, &dev->h_list, d_node)
564                 handle->open = 0;
565
566         spin_unlock_irq(&dev->event_lock);
567 }
568
569 static int input_fetch_keycode(struct input_dev *dev, int scancode)
570 {
571         switch (dev->keycodesize) {
572                 case 1:
573                         return ((u8 *)dev->keycode)[scancode];
574
575                 case 2:
576                         return ((u16 *)dev->keycode)[scancode];
577
578                 default:
579                         return ((u32 *)dev->keycode)[scancode];
580         }
581 }
582
583 static int input_default_getkeycode(struct input_dev *dev,
584                                     int scancode, int *keycode)
585 {
586         if (!dev->keycodesize)
587                 return -EINVAL;
588
589         if (scancode >= dev->keycodemax)
590                 return -EINVAL;
591
592         *keycode = input_fetch_keycode(dev, scancode);
593
594         return 0;
595 }
596
597 static int input_default_setkeycode(struct input_dev *dev,
598                                     int scancode, int keycode)
599 {
600         int old_keycode;
601         int i;
602
603         if (scancode >= dev->keycodemax)
604                 return -EINVAL;
605
606         if (!dev->keycodesize)
607                 return -EINVAL;
608
609         if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
610                 return -EINVAL;
611
612         switch (dev->keycodesize) {
613                 case 1: {
614                         u8 *k = (u8 *)dev->keycode;
615                         old_keycode = k[scancode];
616                         k[scancode] = keycode;
617                         break;
618                 }
619                 case 2: {
620                         u16 *k = (u16 *)dev->keycode;
621                         old_keycode = k[scancode];
622                         k[scancode] = keycode;
623                         break;
624                 }
625                 default: {
626                         u32 *k = (u32 *)dev->keycode;
627                         old_keycode = k[scancode];
628                         k[scancode] = keycode;
629                         break;
630                 }
631         }
632
633         __clear_bit(old_keycode, dev->keybit);
634         __set_bit(keycode, dev->keybit);
635
636         for (i = 0; i < dev->keycodemax; i++) {
637                 if (input_fetch_keycode(dev, i) == old_keycode) {
638                         __set_bit(old_keycode, dev->keybit);
639                         break; /* Setting the bit twice is useless, so break */
640                 }
641         }
642
643         return 0;
644 }
645
646 /**
647  * input_get_keycode - retrieve keycode currently mapped to a given scancode
648  * @dev: input device which keymap is being queried
649  * @scancode: scancode (or its equivalent for device in question) for which
650  *      keycode is needed
651  * @keycode: result
652  *
653  * This function should be called by anyone interested in retrieving current
654  * keymap. Presently keyboard and evdev handlers use it.
655  */
656 int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
657 {
658         if (scancode < 0)
659                 return -EINVAL;
660
661         return dev->getkeycode(dev, scancode, keycode);
662 }
663 EXPORT_SYMBOL(input_get_keycode);
664
665 /**
666  * input_get_keycode - assign new keycode to a given scancode
667  * @dev: input device which keymap is being updated
668  * @scancode: scancode (or its equivalent for device in question)
669  * @keycode: new keycode to be assigned to the scancode
670  *
671  * This function should be called by anyone needing to update current
672  * keymap. Presently keyboard and evdev handlers use it.
673  */
674 int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
675 {
676         unsigned long flags;
677         int old_keycode;
678         int retval;
679
680         if (scancode < 0)
681                 return -EINVAL;
682
683         if (keycode < 0 || keycode > KEY_MAX)
684                 return -EINVAL;
685
686         spin_lock_irqsave(&dev->event_lock, flags);
687
688         retval = dev->getkeycode(dev, scancode, &old_keycode);
689         if (retval)
690                 goto out;
691
692         retval = dev->setkeycode(dev, scancode, keycode);
693         if (retval)
694                 goto out;
695
696         /* Make sure KEY_RESERVED did not get enabled. */
697         __clear_bit(KEY_RESERVED, dev->keybit);
698
699         /*
700          * Simulate keyup event if keycode is not present
701          * in the keymap anymore
702          */
703         if (test_bit(EV_KEY, dev->evbit) &&
704             !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
705             __test_and_clear_bit(old_keycode, dev->key)) {
706
707                 input_pass_event(dev, EV_KEY, old_keycode, 0);
708                 if (dev->sync)
709                         input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
710         }
711
712  out:
713         spin_unlock_irqrestore(&dev->event_lock, flags);
714
715         return retval;
716 }
717 EXPORT_SYMBOL(input_set_keycode);
718
719 #define MATCH_BIT(bit, max) \
720                 for (i = 0; i < BITS_TO_LONGS(max); i++) \
721                         if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
722                                 break; \
723                 if (i != BITS_TO_LONGS(max)) \
724                         continue;
725
726 static const struct input_device_id *input_match_device(struct input_handler *handler,
727                                                         struct input_dev *dev)
728 {
729         const struct input_device_id *id;
730         int i;
731
732         for (id = handler->id_table; id->flags || id->driver_info; id++) {
733
734                 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
735                         if (id->bustype != dev->id.bustype)
736                                 continue;
737
738                 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
739                         if (id->vendor != dev->id.vendor)
740                                 continue;
741
742                 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
743                         if (id->product != dev->id.product)
744                                 continue;
745
746                 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
747                         if (id->version != dev->id.version)
748                                 continue;
749
750                 MATCH_BIT(evbit,  EV_MAX);
751                 MATCH_BIT(keybit, KEY_MAX);
752                 MATCH_BIT(relbit, REL_MAX);
753                 MATCH_BIT(absbit, ABS_MAX);
754                 MATCH_BIT(mscbit, MSC_MAX);
755                 MATCH_BIT(ledbit, LED_MAX);
756                 MATCH_BIT(sndbit, SND_MAX);
757                 MATCH_BIT(ffbit,  FF_MAX);
758                 MATCH_BIT(swbit,  SW_MAX);
759
760                 if (!handler->match || handler->match(handler, dev))
761                         return id;
762         }
763
764         return NULL;
765 }
766
767 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
768 {
769         const struct input_device_id *id;
770         int error;
771
772         id = input_match_device(handler, dev);
773         if (!id)
774                 return -ENODEV;
775
776         error = handler->connect(handler, dev, id);
777         if (error && error != -ENODEV)
778                 printk(KERN_ERR
779                         "input: failed to attach handler %s to device %s, "
780                         "error: %d\n",
781                         handler->name, kobject_name(&dev->dev.kobj), error);
782
783         return error;
784 }
785
786 #ifdef CONFIG_COMPAT
787
788 static int input_bits_to_string(char *buf, int buf_size,
789                                 unsigned long bits, bool skip_empty)
790 {
791         int len = 0;
792
793         if (INPUT_COMPAT_TEST) {
794                 u32 dword = bits >> 32;
795                 if (dword || !skip_empty)
796                         len += snprintf(buf, buf_size, "%x ", dword);
797
798                 dword = bits & 0xffffffffUL;
799                 if (dword || !skip_empty || len)
800                         len += snprintf(buf + len, max(buf_size - len, 0),
801                                         "%x", dword);
802         } else {
803                 if (bits || !skip_empty)
804                         len += snprintf(buf, buf_size, "%lx", bits);
805         }
806
807         return len;
808 }
809
810 #else /* !CONFIG_COMPAT */
811
812 static int input_bits_to_string(char *buf, int buf_size,
813                                 unsigned long bits, bool skip_empty)
814 {
815         return bits || !skip_empty ?
816                 snprintf(buf, buf_size, "%lx", bits) : 0;
817 }
818
819 #endif
820
821 #ifdef CONFIG_PROC_FS
822
823 static struct proc_dir_entry *proc_bus_input_dir;
824 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
825 static int input_devices_state;
826
827 static inline void input_wakeup_procfs_readers(void)
828 {
829         input_devices_state++;
830         wake_up(&input_devices_poll_wait);
831 }
832
833 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
834 {
835         poll_wait(file, &input_devices_poll_wait, wait);
836         if (file->f_version != input_devices_state) {
837                 file->f_version = input_devices_state;
838                 return POLLIN | POLLRDNORM;
839         }
840
841         return 0;
842 }
843
844 union input_seq_state {
845         struct {
846                 unsigned short pos;
847                 bool mutex_acquired;
848         };
849         void *p;
850 };
851
852 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
853 {
854         union input_seq_state *state = (union input_seq_state *)&seq->private;
855         int error;
856
857         /* We need to fit into seq->private pointer */
858         BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
859
860         error = mutex_lock_interruptible(&input_mutex);
861         if (error) {
862                 state->mutex_acquired = false;
863                 return ERR_PTR(error);
864         }
865
866         state->mutex_acquired = true;
867
868         return seq_list_start(&input_dev_list, *pos);
869 }
870
871 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
872 {
873         return seq_list_next(v, &input_dev_list, pos);
874 }
875
876 static void input_seq_stop(struct seq_file *seq, void *v)
877 {
878         union input_seq_state *state = (union input_seq_state *)&seq->private;
879
880         if (state->mutex_acquired)
881                 mutex_unlock(&input_mutex);
882 }
883
884 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
885                                    unsigned long *bitmap, int max)
886 {
887         int i;
888         bool skip_empty = true;
889         char buf[18];
890
891         seq_printf(seq, "B: %s=", name);
892
893         for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
894                 if (input_bits_to_string(buf, sizeof(buf),
895                                          bitmap[i], skip_empty)) {
896                         skip_empty = false;
897                         seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
898                 }
899         }
900
901         /*
902          * If no output was produced print a single 0.
903          */
904         if (skip_empty)
905                 seq_puts(seq, "0");
906
907         seq_putc(seq, '\n');
908 }
909
910 static int input_devices_seq_show(struct seq_file *seq, void *v)
911 {
912         struct input_dev *dev = container_of(v, struct input_dev, node);
913         const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
914         struct input_handle *handle;
915
916         seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
917                    dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
918
919         seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
920         seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
921         seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
922         seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
923         seq_printf(seq, "H: Handlers=");
924
925         list_for_each_entry(handle, &dev->h_list, d_node)
926                 seq_printf(seq, "%s ", handle->name);
927         seq_putc(seq, '\n');
928
929         input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
930         if (test_bit(EV_KEY, dev->evbit))
931                 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
932         if (test_bit(EV_REL, dev->evbit))
933                 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
934         if (test_bit(EV_ABS, dev->evbit))
935                 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
936         if (test_bit(EV_MSC, dev->evbit))
937                 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
938         if (test_bit(EV_LED, dev->evbit))
939                 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
940         if (test_bit(EV_SND, dev->evbit))
941                 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
942         if (test_bit(EV_FF, dev->evbit))
943                 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
944         if (test_bit(EV_SW, dev->evbit))
945                 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
946
947         seq_putc(seq, '\n');
948
949         kfree(path);
950         return 0;
951 }
952
953 static const struct seq_operations input_devices_seq_ops = {
954         .start  = input_devices_seq_start,
955         .next   = input_devices_seq_next,
956         .stop   = input_seq_stop,
957         .show   = input_devices_seq_show,
958 };
959
960 static int input_proc_devices_open(struct inode *inode, struct file *file)
961 {
962         return seq_open(file, &input_devices_seq_ops);
963 }
964
965 static const struct file_operations input_devices_fileops = {
966         .owner          = THIS_MODULE,
967         .open           = input_proc_devices_open,
968         .poll           = input_proc_devices_poll,
969         .read           = seq_read,
970         .llseek         = seq_lseek,
971         .release        = seq_release,
972 };
973
974 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
975 {
976         union input_seq_state *state = (union input_seq_state *)&seq->private;
977         int error;
978
979         /* We need to fit into seq->private pointer */
980         BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
981
982         error = mutex_lock_interruptible(&input_mutex);
983         if (error) {
984                 state->mutex_acquired = false;
985                 return ERR_PTR(error);
986         }
987
988         state->mutex_acquired = true;
989         state->pos = *pos;
990
991         return seq_list_start(&input_handler_list, *pos);
992 }
993
994 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
995 {
996         union input_seq_state *state = (union input_seq_state *)&seq->private;
997
998         state->pos = *pos + 1;
999         return seq_list_next(v, &input_handler_list, pos);
1000 }
1001
1002 static int input_handlers_seq_show(struct seq_file *seq, void *v)
1003 {
1004         struct input_handler *handler = container_of(v, struct input_handler, node);
1005         union input_seq_state *state = (union input_seq_state *)&seq->private;
1006
1007         seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
1008         if (handler->filter)
1009                 seq_puts(seq, " (filter)");
1010         if (handler->fops)
1011                 seq_printf(seq, " Minor=%d", handler->minor);
1012         seq_putc(seq, '\n');
1013
1014         return 0;
1015 }
1016
1017 static const struct seq_operations input_handlers_seq_ops = {
1018         .start  = input_handlers_seq_start,
1019         .next   = input_handlers_seq_next,
1020         .stop   = input_seq_stop,
1021         .show   = input_handlers_seq_show,
1022 };
1023
1024 static int input_proc_handlers_open(struct inode *inode, struct file *file)
1025 {
1026         return seq_open(file, &input_handlers_seq_ops);
1027 }
1028
1029 static const struct file_operations input_handlers_fileops = {
1030         .owner          = THIS_MODULE,
1031         .open           = input_proc_handlers_open,
1032         .read           = seq_read,
1033         .llseek         = seq_lseek,
1034         .release        = seq_release,
1035 };
1036
1037 static int __init input_proc_init(void)
1038 {
1039         struct proc_dir_entry *entry;
1040
1041         proc_bus_input_dir = proc_mkdir("bus/input", NULL);
1042         if (!proc_bus_input_dir)
1043                 return -ENOMEM;
1044
1045         entry = proc_create("devices", 0, proc_bus_input_dir,
1046                             &input_devices_fileops);
1047         if (!entry)
1048                 goto fail1;
1049
1050         entry = proc_create("handlers", 0, proc_bus_input_dir,
1051                             &input_handlers_fileops);
1052         if (!entry)
1053                 goto fail2;
1054
1055         return 0;
1056
1057  fail2: remove_proc_entry("devices", proc_bus_input_dir);
1058  fail1: remove_proc_entry("bus/input", NULL);
1059         return -ENOMEM;
1060 }
1061
1062 static void input_proc_exit(void)
1063 {
1064         remove_proc_entry("devices", proc_bus_input_dir);
1065         remove_proc_entry("handlers", proc_bus_input_dir);
1066         remove_proc_entry("bus/input", NULL);
1067 }
1068
1069 #else /* !CONFIG_PROC_FS */
1070 static inline void input_wakeup_procfs_readers(void) { }
1071 static inline int input_proc_init(void) { return 0; }
1072 static inline void input_proc_exit(void) { }
1073 #endif
1074
1075 #define INPUT_DEV_STRING_ATTR_SHOW(name)                                \
1076 static ssize_t input_dev_show_##name(struct device *dev,                \
1077                                      struct device_attribute *attr,     \
1078                                      char *buf)                         \
1079 {                                                                       \
1080         struct input_dev *input_dev = to_input_dev(dev);                \
1081                                                                         \
1082         return scnprintf(buf, PAGE_SIZE, "%s\n",                        \
1083                          input_dev->name ? input_dev->name : "");       \
1084 }                                                                       \
1085 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
1086
1087 INPUT_DEV_STRING_ATTR_SHOW(name);
1088 INPUT_DEV_STRING_ATTR_SHOW(phys);
1089 INPUT_DEV_STRING_ATTR_SHOW(uniq);
1090
1091 static int input_print_modalias_bits(char *buf, int size,
1092                                      char name, unsigned long *bm,
1093                                      unsigned int min_bit, unsigned int max_bit)
1094 {
1095         int len = 0, i;
1096
1097         len += snprintf(buf, max(size, 0), "%c", name);
1098         for (i = min_bit; i < max_bit; i++)
1099                 if (bm[BIT_WORD(i)] & BIT_MASK(i))
1100                         len += snprintf(buf + len, max(size - len, 0), "%X,", i);
1101         return len;
1102 }
1103
1104 static int input_print_modalias(char *buf, int size, struct input_dev *id,
1105                                 int add_cr)
1106 {
1107         int len;
1108
1109         len = snprintf(buf, max(size, 0),
1110                        "input:b%04Xv%04Xp%04Xe%04X-",
1111                        id->id.bustype, id->id.vendor,
1112                        id->id.product, id->id.version);
1113
1114         len += input_print_modalias_bits(buf + len, size - len,
1115                                 'e', id->evbit, 0, EV_MAX);
1116         len += input_print_modalias_bits(buf + len, size - len,
1117                                 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1118         len += input_print_modalias_bits(buf + len, size - len,
1119                                 'r', id->relbit, 0, REL_MAX);
1120         len += input_print_modalias_bits(buf + len, size - len,
1121                                 'a', id->absbit, 0, ABS_MAX);
1122         len += input_print_modalias_bits(buf + len, size - len,
1123                                 'm', id->mscbit, 0, MSC_MAX);
1124         len += input_print_modalias_bits(buf + len, size - len,
1125                                 'l', id->ledbit, 0, LED_MAX);
1126         len += input_print_modalias_bits(buf + len, size - len,
1127                                 's', id->sndbit, 0, SND_MAX);
1128         len += input_print_modalias_bits(buf + len, size - len,
1129                                 'f', id->ffbit, 0, FF_MAX);
1130         len += input_print_modalias_bits(buf + len, size - len,
1131                                 'w', id->swbit, 0, SW_MAX);
1132
1133         if (add_cr)
1134                 len += snprintf(buf + len, max(size - len, 0), "\n");
1135
1136         return len;
1137 }
1138
1139 static ssize_t input_dev_show_modalias(struct device *dev,
1140                                        struct device_attribute *attr,
1141                                        char *buf)
1142 {
1143         struct input_dev *id = to_input_dev(dev);
1144         ssize_t len;
1145
1146         len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1147
1148         return min_t(int, len, PAGE_SIZE);
1149 }
1150 static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
1151
1152 static struct attribute *input_dev_attrs[] = {
1153         &dev_attr_name.attr,
1154         &dev_attr_phys.attr,
1155         &dev_attr_uniq.attr,
1156         &dev_attr_modalias.attr,
1157         NULL
1158 };
1159
1160 static struct attribute_group input_dev_attr_group = {
1161         .attrs  = input_dev_attrs,
1162 };
1163
1164 #define INPUT_DEV_ID_ATTR(name)                                         \
1165 static ssize_t input_dev_show_id_##name(struct device *dev,             \
1166                                         struct device_attribute *attr,  \
1167                                         char *buf)                      \
1168 {                                                                       \
1169         struct input_dev *input_dev = to_input_dev(dev);                \
1170         return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1171 }                                                                       \
1172 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
1173
1174 INPUT_DEV_ID_ATTR(bustype);
1175 INPUT_DEV_ID_ATTR(vendor);
1176 INPUT_DEV_ID_ATTR(product);
1177 INPUT_DEV_ID_ATTR(version);
1178
1179 static struct attribute *input_dev_id_attrs[] = {
1180         &dev_attr_bustype.attr,
1181         &dev_attr_vendor.attr,
1182         &dev_attr_product.attr,
1183         &dev_attr_version.attr,
1184         NULL
1185 };
1186
1187 static struct attribute_group input_dev_id_attr_group = {
1188         .name   = "id",
1189         .attrs  = input_dev_id_attrs,
1190 };
1191
1192 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1193                               int max, int add_cr)
1194 {
1195         int i;
1196         int len = 0;
1197         bool skip_empty = true;
1198
1199         for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1200                 len += input_bits_to_string(buf + len, max(buf_size - len, 0),
1201                                             bitmap[i], skip_empty);
1202                 if (len) {
1203                         skip_empty = false;
1204                         if (i > 0)
1205                                 len += snprintf(buf + len, max(buf_size - len, 0), " ");
1206                 }
1207         }
1208
1209         /*
1210          * If no output was produced print a single 0.
1211          */
1212         if (len == 0)
1213                 len = snprintf(buf, buf_size, "%d", 0);
1214
1215         if (add_cr)
1216                 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1217
1218         return len;
1219 }
1220
1221 #define INPUT_DEV_CAP_ATTR(ev, bm)                                      \
1222 static ssize_t input_dev_show_cap_##bm(struct device *dev,              \
1223                                        struct device_attribute *attr,   \
1224                                        char *buf)                       \
1225 {                                                                       \
1226         struct input_dev *input_dev = to_input_dev(dev);                \
1227         int len = input_print_bitmap(buf, PAGE_SIZE,                    \
1228                                      input_dev->bm##bit, ev##_MAX,      \
1229                                      true);                             \
1230         return min_t(int, len, PAGE_SIZE);                              \
1231 }                                                                       \
1232 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
1233
1234 INPUT_DEV_CAP_ATTR(EV, ev);
1235 INPUT_DEV_CAP_ATTR(KEY, key);
1236 INPUT_DEV_CAP_ATTR(REL, rel);
1237 INPUT_DEV_CAP_ATTR(ABS, abs);
1238 INPUT_DEV_CAP_ATTR(MSC, msc);
1239 INPUT_DEV_CAP_ATTR(LED, led);
1240 INPUT_DEV_CAP_ATTR(SND, snd);
1241 INPUT_DEV_CAP_ATTR(FF, ff);
1242 INPUT_DEV_CAP_ATTR(SW, sw);
1243
1244 static struct attribute *input_dev_caps_attrs[] = {
1245         &dev_attr_ev.attr,
1246         &dev_attr_key.attr,
1247         &dev_attr_rel.attr,
1248         &dev_attr_abs.attr,
1249         &dev_attr_msc.attr,
1250         &dev_attr_led.attr,
1251         &dev_attr_snd.attr,
1252         &dev_attr_ff.attr,
1253         &dev_attr_sw.attr,
1254         NULL
1255 };
1256
1257 static struct attribute_group input_dev_caps_attr_group = {
1258         .name   = "capabilities",
1259         .attrs  = input_dev_caps_attrs,
1260 };
1261
1262 static const struct attribute_group *input_dev_attr_groups[] = {
1263         &input_dev_attr_group,
1264         &input_dev_id_attr_group,
1265         &input_dev_caps_attr_group,
1266         NULL
1267 };
1268
1269 static void input_dev_release(struct device *device)
1270 {
1271         struct input_dev *dev = to_input_dev(device);
1272
1273         input_ff_destroy(dev);
1274         kfree(dev);
1275
1276         module_put(THIS_MODULE);
1277 }
1278
1279 /*
1280  * Input uevent interface - loading event handlers based on
1281  * device bitfields.
1282  */
1283 static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
1284                                    const char *name, unsigned long *bitmap, int max)
1285 {
1286         int len;
1287
1288         if (add_uevent_var(env, "%s=", name))
1289                 return -ENOMEM;
1290
1291         len = input_print_bitmap(&env->buf[env->buflen - 1],
1292                                  sizeof(env->buf) - env->buflen,
1293                                  bitmap, max, false);
1294         if (len >= (sizeof(env->buf) - env->buflen))
1295                 return -ENOMEM;
1296
1297         env->buflen += len;
1298         return 0;
1299 }
1300
1301 static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
1302                                          struct input_dev *dev)
1303 {
1304         int len;
1305
1306         if (add_uevent_var(env, "MODALIAS="))
1307                 return -ENOMEM;
1308
1309         len = input_print_modalias(&env->buf[env->buflen - 1],
1310                                    sizeof(env->buf) - env->buflen,
1311                                    dev, 0);
1312         if (len >= (sizeof(env->buf) - env->buflen))
1313                 return -ENOMEM;
1314
1315         env->buflen += len;
1316         return 0;
1317 }
1318
1319 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...)                              \
1320         do {                                                            \
1321                 int err = add_uevent_var(env, fmt, val);                \
1322                 if (err)                                                \
1323                         return err;                                     \
1324         } while (0)
1325
1326 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max)                         \
1327         do {                                                            \
1328                 int err = input_add_uevent_bm_var(env, name, bm, max);  \
1329                 if (err)                                                \
1330                         return err;                                     \
1331         } while (0)
1332
1333 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev)                             \
1334         do {                                                            \
1335                 int err = input_add_uevent_modalias_var(env, dev);      \
1336                 if (err)                                                \
1337                         return err;                                     \
1338         } while (0)
1339
1340 static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1341 {
1342         struct input_dev *dev = to_input_dev(device);
1343
1344         INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1345                                 dev->id.bustype, dev->id.vendor,
1346                                 dev->id.product, dev->id.version);
1347         if (dev->name)
1348                 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1349         if (dev->phys)
1350                 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
1351         if (dev->uniq)
1352                 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1353
1354         INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1355         if (test_bit(EV_KEY, dev->evbit))
1356                 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1357         if (test_bit(EV_REL, dev->evbit))
1358                 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1359         if (test_bit(EV_ABS, dev->evbit))
1360                 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1361         if (test_bit(EV_MSC, dev->evbit))
1362                 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1363         if (test_bit(EV_LED, dev->evbit))
1364                 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1365         if (test_bit(EV_SND, dev->evbit))
1366                 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1367         if (test_bit(EV_FF, dev->evbit))
1368                 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1369         if (test_bit(EV_SW, dev->evbit))
1370                 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1371
1372         INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
1373
1374         return 0;
1375 }
1376
1377 #define INPUT_DO_TOGGLE(dev, type, bits, on)                            \
1378         do {                                                            \
1379                 int i;                                                  \
1380                 bool active;                                            \
1381                                                                         \
1382                 if (!test_bit(EV_##type, dev->evbit))                   \
1383                         break;                                          \
1384                                                                         \
1385                 for (i = 0; i < type##_MAX; i++) {                      \
1386                         if (!test_bit(i, dev->bits##bit))               \
1387                                 continue;                               \
1388                                                                         \
1389                         active = test_bit(i, dev->bits);                \
1390                         if (!active && !on)                             \
1391                                 continue;                               \
1392                                                                         \
1393                         dev->event(dev, EV_##type, i, on ? active : 0); \
1394                 }                                                       \
1395         } while (0)
1396
1397 #ifdef CONFIG_PM
1398 static void input_dev_reset(struct input_dev *dev, bool activate)
1399 {
1400         if (!dev->event)
1401                 return;
1402
1403         INPUT_DO_TOGGLE(dev, LED, led, activate);
1404         INPUT_DO_TOGGLE(dev, SND, snd, activate);
1405
1406         if (activate && test_bit(EV_REP, dev->evbit)) {
1407                 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1408                 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1409         }
1410 }
1411
1412 static int input_dev_suspend(struct device *dev)
1413 {
1414         struct input_dev *input_dev = to_input_dev(dev);
1415
1416         mutex_lock(&input_dev->mutex);
1417         input_dev_reset(input_dev, false);
1418         mutex_unlock(&input_dev->mutex);
1419
1420         return 0;
1421 }
1422
1423 static int input_dev_resume(struct device *dev)
1424 {
1425         struct input_dev *input_dev = to_input_dev(dev);
1426
1427         mutex_lock(&input_dev->mutex);
1428         input_dev_reset(input_dev, true);
1429         mutex_unlock(&input_dev->mutex);
1430
1431         return 0;
1432 }
1433
1434 static const struct dev_pm_ops input_dev_pm_ops = {
1435         .suspend        = input_dev_suspend,
1436         .resume         = input_dev_resume,
1437         .poweroff       = input_dev_suspend,
1438         .restore        = input_dev_resume,
1439 };
1440 #endif /* CONFIG_PM */
1441
1442 static struct device_type input_dev_type = {
1443         .groups         = input_dev_attr_groups,
1444         .release        = input_dev_release,
1445         .uevent         = input_dev_uevent,
1446 #ifdef CONFIG_PM
1447         .pm             = &input_dev_pm_ops,
1448 #endif
1449 };
1450
1451 static char *input_devnode(struct device *dev, mode_t *mode)
1452 {
1453         return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1454 }
1455
1456 struct class input_class = {
1457         .name           = "input",
1458         .devnode        = input_devnode,
1459 };
1460 EXPORT_SYMBOL_GPL(input_class);
1461
1462 /**
1463  * input_allocate_device - allocate memory for new input device
1464  *
1465  * Returns prepared struct input_dev or NULL.
1466  *
1467  * NOTE: Use input_free_device() to free devices that have not been
1468  * registered; input_unregister_device() should be used for already
1469  * registered devices.
1470  */
1471 struct input_dev *input_allocate_device(void)
1472 {
1473         struct input_dev *dev;
1474
1475         dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1476         if (dev) {
1477                 dev->dev.type = &input_dev_type;
1478                 dev->dev.class = &input_class;
1479                 device_initialize(&dev->dev);
1480                 mutex_init(&dev->mutex);
1481                 spin_lock_init(&dev->event_lock);
1482                 INIT_LIST_HEAD(&dev->h_list);
1483                 INIT_LIST_HEAD(&dev->node);
1484
1485                 __module_get(THIS_MODULE);
1486         }
1487
1488         return dev;
1489 }
1490 EXPORT_SYMBOL(input_allocate_device);
1491
1492 /**
1493  * input_free_device - free memory occupied by input_dev structure
1494  * @dev: input device to free
1495  *
1496  * This function should only be used if input_register_device()
1497  * was not called yet or if it failed. Once device was registered
1498  * use input_unregister_device() and memory will be freed once last
1499  * reference to the device is dropped.
1500  *
1501  * Device should be allocated by input_allocate_device().
1502  *
1503  * NOTE: If there are references to the input device then memory
1504  * will not be freed until last reference is dropped.
1505  */
1506 void input_free_device(struct input_dev *dev)
1507 {
1508         if (dev)
1509                 input_put_device(dev);
1510 }
1511 EXPORT_SYMBOL(input_free_device);
1512
1513 /**
1514  * input_set_capability - mark device as capable of a certain event
1515  * @dev: device that is capable of emitting or accepting event
1516  * @type: type of the event (EV_KEY, EV_REL, etc...)
1517  * @code: event code
1518  *
1519  * In addition to setting up corresponding bit in appropriate capability
1520  * bitmap the function also adjusts dev->evbit.
1521  */
1522 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1523 {
1524         switch (type) {
1525         case EV_KEY:
1526                 __set_bit(code, dev->keybit);
1527                 break;
1528
1529         case EV_REL:
1530                 __set_bit(code, dev->relbit);
1531                 break;
1532
1533         case EV_ABS:
1534                 __set_bit(code, dev->absbit);
1535                 break;
1536
1537         case EV_MSC:
1538                 __set_bit(code, dev->mscbit);
1539                 break;
1540
1541         case EV_SW:
1542                 __set_bit(code, dev->swbit);
1543                 break;
1544
1545         case EV_LED:
1546                 __set_bit(code, dev->ledbit);
1547                 break;
1548
1549         case EV_SND:
1550                 __set_bit(code, dev->sndbit);
1551                 break;
1552
1553         case EV_FF:
1554                 __set_bit(code, dev->ffbit);
1555                 break;
1556
1557         case EV_PWR:
1558                 /* do nothing */
1559                 break;
1560
1561         default:
1562                 printk(KERN_ERR
1563                         "input_set_capability: unknown type %u (code %u)\n",
1564                         type, code);
1565                 dump_stack();
1566                 return;
1567         }
1568
1569         __set_bit(type, dev->evbit);
1570 }
1571 EXPORT_SYMBOL(input_set_capability);
1572
1573 #define INPUT_CLEANSE_BITMASK(dev, type, bits)                          \
1574         do {                                                            \
1575                 if (!test_bit(EV_##type, dev->evbit))                   \
1576                         memset(dev->bits##bit, 0,                       \
1577                                 sizeof(dev->bits##bit));                \
1578         } while (0)
1579
1580 static void input_cleanse_bitmasks(struct input_dev *dev)
1581 {
1582         INPUT_CLEANSE_BITMASK(dev, KEY, key);
1583         INPUT_CLEANSE_BITMASK(dev, REL, rel);
1584         INPUT_CLEANSE_BITMASK(dev, ABS, abs);
1585         INPUT_CLEANSE_BITMASK(dev, MSC, msc);
1586         INPUT_CLEANSE_BITMASK(dev, LED, led);
1587         INPUT_CLEANSE_BITMASK(dev, SND, snd);
1588         INPUT_CLEANSE_BITMASK(dev, FF, ff);
1589         INPUT_CLEANSE_BITMASK(dev, SW, sw);
1590 }
1591
1592 /**
1593  * input_register_device - register device with input core
1594  * @dev: device to be registered
1595  *
1596  * This function registers device with input core. The device must be
1597  * allocated with input_allocate_device() and all it's capabilities
1598  * set up before registering.
1599  * If function fails the device must be freed with input_free_device().
1600  * Once device has been successfully registered it can be unregistered
1601  * with input_unregister_device(); input_free_device() should not be
1602  * called in this case.
1603  */
1604 int input_register_device(struct input_dev *dev)
1605 {
1606         static atomic_t input_no = ATOMIC_INIT(0);
1607         struct input_handler *handler;
1608         const char *path;
1609         int error;
1610
1611         /* Every input device generates EV_SYN/SYN_REPORT events. */
1612         __set_bit(EV_SYN, dev->evbit);
1613
1614         /* KEY_RESERVED is not supposed to be transmitted to userspace. */
1615         __clear_bit(KEY_RESERVED, dev->keybit);
1616
1617         /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
1618         input_cleanse_bitmasks(dev);
1619
1620         /*
1621          * If delay and period are pre-set by the driver, then autorepeating
1622          * is handled by the driver itself and we don't do it in input.c.
1623          */
1624         init_timer(&dev->timer);
1625         if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1626                 dev->timer.data = (long) dev;
1627                 dev->timer.function = input_repeat_key;
1628                 dev->rep[REP_DELAY] = 250;
1629                 dev->rep[REP_PERIOD] = 33;
1630         }
1631
1632         if (!dev->getkeycode)
1633                 dev->getkeycode = input_default_getkeycode;
1634
1635         if (!dev->setkeycode)
1636                 dev->setkeycode = input_default_setkeycode;
1637
1638         dev_set_name(&dev->dev, "input%ld",
1639                      (unsigned long) atomic_inc_return(&input_no) - 1);
1640
1641         error = device_add(&dev->dev);
1642         if (error)
1643                 return error;
1644
1645         path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1646         printk(KERN_INFO "input: %s as %s\n",
1647                 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1648         kfree(path);
1649
1650         error = mutex_lock_interruptible(&input_mutex);
1651         if (error) {
1652                 device_del(&dev->dev);
1653                 return error;
1654         }
1655
1656         list_add_tail(&dev->node, &input_dev_list);
1657
1658         list_for_each_entry(handler, &input_handler_list, node)
1659                 input_attach_handler(dev, handler);
1660
1661         input_wakeup_procfs_readers();
1662
1663         mutex_unlock(&input_mutex);
1664
1665         return 0;
1666 }
1667 EXPORT_SYMBOL(input_register_device);
1668
1669 /**
1670  * input_unregister_device - unregister previously registered device
1671  * @dev: device to be unregistered
1672  *
1673  * This function unregisters an input device. Once device is unregistered
1674  * the caller should not try to access it as it may get freed at any moment.
1675  */
1676 void input_unregister_device(struct input_dev *dev)
1677 {
1678         struct input_handle *handle, *next;
1679
1680         input_disconnect_device(dev);
1681
1682         mutex_lock(&input_mutex);
1683
1684         list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1685                 handle->handler->disconnect(handle);
1686         WARN_ON(!list_empty(&dev->h_list));
1687
1688         del_timer_sync(&dev->timer);
1689         list_del_init(&dev->node);
1690
1691         input_wakeup_procfs_readers();
1692
1693         mutex_unlock(&input_mutex);
1694
1695         device_unregister(&dev->dev);
1696 }
1697 EXPORT_SYMBOL(input_unregister_device);
1698
1699 /**
1700  * input_register_handler - register a new input handler
1701  * @handler: handler to be registered
1702  *
1703  * This function registers a new input handler (interface) for input
1704  * devices in the system and attaches it to all input devices that
1705  * are compatible with the handler.
1706  */
1707 int input_register_handler(struct input_handler *handler)
1708 {
1709         struct input_dev *dev;
1710         int retval;
1711
1712         retval = mutex_lock_interruptible(&input_mutex);
1713         if (retval)
1714                 return retval;
1715
1716         INIT_LIST_HEAD(&handler->h_list);
1717
1718         if (handler->fops != NULL) {
1719                 if (input_table[handler->minor >> 5]) {
1720                         retval = -EBUSY;
1721                         goto out;
1722                 }
1723                 input_table[handler->minor >> 5] = handler;
1724         }
1725
1726         list_add_tail(&handler->node, &input_handler_list);
1727
1728         list_for_each_entry(dev, &input_dev_list, node)
1729                 input_attach_handler(dev, handler);
1730
1731         input_wakeup_procfs_readers();
1732
1733  out:
1734         mutex_unlock(&input_mutex);
1735         return retval;
1736 }
1737 EXPORT_SYMBOL(input_register_handler);
1738
1739 /**
1740  * input_unregister_handler - unregisters an input handler
1741  * @handler: handler to be unregistered
1742  *
1743  * This function disconnects a handler from its input devices and
1744  * removes it from lists of known handlers.
1745  */
1746 void input_unregister_handler(struct input_handler *handler)
1747 {
1748         struct input_handle *handle, *next;
1749
1750         mutex_lock(&input_mutex);
1751
1752         list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1753                 handler->disconnect(handle);
1754         WARN_ON(!list_empty(&handler->h_list));
1755
1756         list_del_init(&handler->node);
1757
1758         if (handler->fops != NULL)
1759                 input_table[handler->minor >> 5] = NULL;
1760
1761         input_wakeup_procfs_readers();
1762
1763         mutex_unlock(&input_mutex);
1764 }
1765 EXPORT_SYMBOL(input_unregister_handler);
1766
1767 /**
1768  * input_handler_for_each_handle - handle iterator
1769  * @handler: input handler to iterate
1770  * @data: data for the callback
1771  * @fn: function to be called for each handle
1772  *
1773  * Iterate over @bus's list of devices, and call @fn for each, passing
1774  * it @data and stop when @fn returns a non-zero value. The function is
1775  * using RCU to traverse the list and therefore may be usind in atonic
1776  * contexts. The @fn callback is invoked from RCU critical section and
1777  * thus must not sleep.
1778  */
1779 int input_handler_for_each_handle(struct input_handler *handler, void *data,
1780                                   int (*fn)(struct input_handle *, void *))
1781 {
1782         struct input_handle *handle;
1783         int retval = 0;
1784
1785         rcu_read_lock();
1786
1787         list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
1788                 retval = fn(handle, data);
1789                 if (retval)
1790                         break;
1791         }
1792
1793         rcu_read_unlock();
1794
1795         return retval;
1796 }
1797 EXPORT_SYMBOL(input_handler_for_each_handle);
1798
1799 /**
1800  * input_register_handle - register a new input handle
1801  * @handle: handle to register
1802  *
1803  * This function puts a new input handle onto device's
1804  * and handler's lists so that events can flow through
1805  * it once it is opened using input_open_device().
1806  *
1807  * This function is supposed to be called from handler's
1808  * connect() method.
1809  */
1810 int input_register_handle(struct input_handle *handle)
1811 {
1812         struct input_handler *handler = handle->handler;
1813         struct input_dev *dev = handle->dev;
1814         int error;
1815
1816         /*
1817          * We take dev->mutex here to prevent race with
1818          * input_release_device().
1819          */
1820         error = mutex_lock_interruptible(&dev->mutex);
1821         if (error)
1822                 return error;
1823
1824         /*
1825          * Filters go to the head of the list, normal handlers
1826          * to the tail.
1827          */
1828         if (handler->filter)
1829                 list_add_rcu(&handle->d_node, &dev->h_list);
1830         else
1831                 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1832
1833         mutex_unlock(&dev->mutex);
1834
1835         /*
1836          * Since we are supposed to be called from ->connect()
1837          * which is mutually exclusive with ->disconnect()
1838          * we can't be racing with input_unregister_handle()
1839          * and so separate lock is not needed here.
1840          */
1841         list_add_tail_rcu(&handle->h_node, &handler->h_list);
1842
1843         if (handler->start)
1844                 handler->start(handle);
1845
1846         return 0;
1847 }
1848 EXPORT_SYMBOL(input_register_handle);
1849
1850 /**
1851  * input_unregister_handle - unregister an input handle
1852  * @handle: handle to unregister
1853  *
1854  * This function removes input handle from device's
1855  * and handler's lists.
1856  *
1857  * This function is supposed to be called from handler's
1858  * disconnect() method.
1859  */
1860 void input_unregister_handle(struct input_handle *handle)
1861 {
1862         struct input_dev *dev = handle->dev;
1863
1864         list_del_rcu(&handle->h_node);
1865
1866         /*
1867          * Take dev->mutex to prevent race with input_release_device().
1868          */
1869         mutex_lock(&dev->mutex);
1870         list_del_rcu(&handle->d_node);
1871         mutex_unlock(&dev->mutex);
1872
1873         synchronize_rcu();
1874 }
1875 EXPORT_SYMBOL(input_unregister_handle);
1876
1877 static int input_open_file(struct inode *inode, struct file *file)
1878 {
1879         struct input_handler *handler;
1880         const struct file_operations *old_fops, *new_fops = NULL;
1881         int err;
1882
1883         lock_kernel();
1884         /* No load-on-demand here? */
1885         handler = input_table[iminor(inode) >> 5];
1886         if (!handler || !(new_fops = fops_get(handler->fops))) {
1887                 err = -ENODEV;
1888                 goto out;
1889         }
1890
1891         /*
1892          * That's _really_ odd. Usually NULL ->open means "nothing special",
1893          * not "no device". Oh, well...
1894          */
1895         if (!new_fops->open) {
1896                 fops_put(new_fops);
1897                 err = -ENODEV;
1898                 goto out;
1899         }
1900         old_fops = file->f_op;
1901         file->f_op = new_fops;
1902
1903         err = new_fops->open(inode, file);
1904
1905         if (err) {
1906                 fops_put(file->f_op);
1907                 file->f_op = fops_get(old_fops);
1908         }
1909         fops_put(old_fops);
1910 out:
1911         unlock_kernel();
1912         return err;
1913 }
1914
1915 static const struct file_operations input_fops = {
1916         .owner = THIS_MODULE,
1917         .open = input_open_file,
1918 };
1919
1920 static void __init input_init_abs_bypass(void)
1921 {
1922         const unsigned int *p;
1923
1924         for (p = input_abs_bypass_init_data; *p; p++)
1925                 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1926 }
1927
1928 static int __init input_init(void)
1929 {
1930         int err;
1931
1932         input_init_abs_bypass();
1933
1934         err = class_register(&input_class);
1935         if (err) {
1936                 printk(KERN_ERR "input: unable to register input_dev class\n");
1937                 return err;
1938         }
1939
1940         err = input_proc_init();
1941         if (err)
1942                 goto fail1;
1943
1944         err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1945         if (err) {
1946                 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1947                 goto fail2;
1948         }
1949
1950         return 0;
1951
1952  fail2: input_proc_exit();
1953  fail1: class_unregister(&input_class);
1954         return err;
1955 }
1956
1957 static void __exit input_exit(void)
1958 {
1959         input_proc_exit();
1960         unregister_chrdev(INPUT_MAJOR, "input");
1961         class_unregister(&input_class);
1962 }
1963
1964 subsys_initcall(input_init);
1965 module_exit(input_exit);