ALSA: powermac - Lineout detection on G4 DA
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / input.c
blobc82ae82cc43f88d92cd67ce50fde833af23aa88a
1 /*
2 * The input core
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 */
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.
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"
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
30 MODULE_DESCRIPTION("Input core");
31 MODULE_LICENSE("GPL");
33 #define INPUT_DEVICES 256
36 * EV_ABS events which should not be cached are listed here.
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,
51 static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
53 static LIST_HEAD(input_dev_list);
54 static LIST_HEAD(input_handler_list);
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.
62 static DEFINE_MUTEX(input_mutex);
64 static struct input_handler *input_table[8];
66 static inline int is_event_supported(unsigned int code,
67 unsigned long *bm, unsigned int max)
69 return code <= max && test_bit(code, bm);
72 static int input_defuzz_abs_event(int value, int old_val, int fuzz)
74 if (fuzz) {
75 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
76 return old_val;
78 if (value > old_val - fuzz && value < old_val + fuzz)
79 return (old_val * 3 + value) / 4;
81 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
82 return (old_val + value) / 2;
85 return value;
89 * Pass event through all open handles. This function is called with
90 * dev->event_lock held and interrupts disabled.
92 static void input_pass_event(struct input_dev *dev,
93 unsigned int type, unsigned int code, int value)
95 struct input_handle *handle;
97 rcu_read_lock();
99 handle = rcu_dereference(dev->grab);
100 if (handle)
101 handle->handler->event(handle, type, code, value);
102 else
103 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
104 if (handle->open)
105 handle->handler->event(handle,
106 type, code, value);
107 rcu_read_unlock();
111 * Generate software autorepeat event. Note that we take
112 * dev->event_lock here to avoid racing with input_event
113 * which may cause keys get "stuck".
115 static void input_repeat_key(unsigned long data)
117 struct input_dev *dev = (void *) data;
118 unsigned long flags;
120 spin_lock_irqsave(&dev->event_lock, flags);
122 if (test_bit(dev->repeat_key, dev->key) &&
123 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
125 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
127 if (dev->sync) {
129 * Only send SYN_REPORT if we are not in a middle
130 * of driver parsing a new hardware packet.
131 * Otherwise assume that the driver will send
132 * SYN_REPORT once it's done.
134 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
137 if (dev->rep[REP_PERIOD])
138 mod_timer(&dev->timer, jiffies +
139 msecs_to_jiffies(dev->rep[REP_PERIOD]));
142 spin_unlock_irqrestore(&dev->event_lock, flags);
145 static void input_start_autorepeat(struct input_dev *dev, int code)
147 if (test_bit(EV_REP, dev->evbit) &&
148 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
149 dev->timer.data) {
150 dev->repeat_key = code;
151 mod_timer(&dev->timer,
152 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
156 static void input_stop_autorepeat(struct input_dev *dev)
158 del_timer(&dev->timer);
161 #define INPUT_IGNORE_EVENT 0
162 #define INPUT_PASS_TO_HANDLERS 1
163 #define INPUT_PASS_TO_DEVICE 2
164 #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
166 static void input_handle_event(struct input_dev *dev,
167 unsigned int type, unsigned int code, int value)
169 int disposition = INPUT_IGNORE_EVENT;
171 switch (type) {
173 case EV_SYN:
174 switch (code) {
175 case SYN_CONFIG:
176 disposition = INPUT_PASS_TO_ALL;
177 break;
179 case SYN_REPORT:
180 if (!dev->sync) {
181 dev->sync = 1;
182 disposition = INPUT_PASS_TO_HANDLERS;
184 break;
185 case SYN_MT_REPORT:
186 dev->sync = 0;
187 disposition = INPUT_PASS_TO_HANDLERS;
188 break;
190 break;
192 case EV_KEY:
193 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
194 !!test_bit(code, dev->key) != value) {
196 if (value != 2) {
197 __change_bit(code, dev->key);
198 if (value)
199 input_start_autorepeat(dev, code);
200 else
201 input_stop_autorepeat(dev);
204 disposition = INPUT_PASS_TO_HANDLERS;
206 break;
208 case EV_SW:
209 if (is_event_supported(code, dev->swbit, SW_MAX) &&
210 !!test_bit(code, dev->sw) != value) {
212 __change_bit(code, dev->sw);
213 disposition = INPUT_PASS_TO_HANDLERS;
215 break;
217 case EV_ABS:
218 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
220 if (test_bit(code, input_abs_bypass)) {
221 disposition = INPUT_PASS_TO_HANDLERS;
222 break;
225 value = input_defuzz_abs_event(value,
226 dev->abs[code], dev->absfuzz[code]);
228 if (dev->abs[code] != value) {
229 dev->abs[code] = value;
230 disposition = INPUT_PASS_TO_HANDLERS;
233 break;
235 case EV_REL:
236 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
237 disposition = INPUT_PASS_TO_HANDLERS;
239 break;
241 case EV_MSC:
242 if (is_event_supported(code, dev->mscbit, MSC_MAX))
243 disposition = INPUT_PASS_TO_ALL;
245 break;
247 case EV_LED:
248 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
249 !!test_bit(code, dev->led) != value) {
251 __change_bit(code, dev->led);
252 disposition = INPUT_PASS_TO_ALL;
254 break;
256 case EV_SND:
257 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
259 if (!!test_bit(code, dev->snd) != !!value)
260 __change_bit(code, dev->snd);
261 disposition = INPUT_PASS_TO_ALL;
263 break;
265 case EV_REP:
266 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
267 dev->rep[code] = value;
268 disposition = INPUT_PASS_TO_ALL;
270 break;
272 case EV_FF:
273 if (value >= 0)
274 disposition = INPUT_PASS_TO_ALL;
275 break;
277 case EV_PWR:
278 disposition = INPUT_PASS_TO_ALL;
279 break;
282 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
283 dev->sync = 0;
285 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
286 dev->event(dev, type, code, value);
288 if (disposition & INPUT_PASS_TO_HANDLERS)
289 input_pass_event(dev, type, code, value);
293 * input_event() - report new input event
294 * @dev: device that generated the event
295 * @type: type of the event
296 * @code: event code
297 * @value: value of the event
299 * This function should be used by drivers implementing various input
300 * devices. See also input_inject_event().
303 void input_event(struct input_dev *dev,
304 unsigned int type, unsigned int code, int value)
306 unsigned long flags;
308 if (is_event_supported(type, dev->evbit, EV_MAX)) {
310 spin_lock_irqsave(&dev->event_lock, flags);
311 add_input_randomness(type, code, value);
312 input_handle_event(dev, type, code, value);
313 spin_unlock_irqrestore(&dev->event_lock, flags);
316 EXPORT_SYMBOL(input_event);
319 * input_inject_event() - send input event from input handler
320 * @handle: input handle to send event through
321 * @type: type of the event
322 * @code: event code
323 * @value: value of the event
325 * Similar to input_event() but will ignore event if device is
326 * "grabbed" and handle injecting event is not the one that owns
327 * the device.
329 void input_inject_event(struct input_handle *handle,
330 unsigned int type, unsigned int code, int value)
332 struct input_dev *dev = handle->dev;
333 struct input_handle *grab;
334 unsigned long flags;
336 if (is_event_supported(type, dev->evbit, EV_MAX)) {
337 spin_lock_irqsave(&dev->event_lock, flags);
339 rcu_read_lock();
340 grab = rcu_dereference(dev->grab);
341 if (!grab || grab == handle)
342 input_handle_event(dev, type, code, value);
343 rcu_read_unlock();
345 spin_unlock_irqrestore(&dev->event_lock, flags);
348 EXPORT_SYMBOL(input_inject_event);
351 * input_grab_device - grabs device for exclusive use
352 * @handle: input handle that wants to own the device
354 * When a device is grabbed by an input handle all events generated by
355 * the device are delivered only to this handle. Also events injected
356 * by other input handles are ignored while device is grabbed.
358 int input_grab_device(struct input_handle *handle)
360 struct input_dev *dev = handle->dev;
361 int retval;
363 retval = mutex_lock_interruptible(&dev->mutex);
364 if (retval)
365 return retval;
367 if (dev->grab) {
368 retval = -EBUSY;
369 goto out;
372 rcu_assign_pointer(dev->grab, handle);
373 synchronize_rcu();
375 out:
376 mutex_unlock(&dev->mutex);
377 return retval;
379 EXPORT_SYMBOL(input_grab_device);
381 static void __input_release_device(struct input_handle *handle)
383 struct input_dev *dev = handle->dev;
385 if (dev->grab == handle) {
386 rcu_assign_pointer(dev->grab, NULL);
387 /* Make sure input_pass_event() notices that grab is gone */
388 synchronize_rcu();
390 list_for_each_entry(handle, &dev->h_list, d_node)
391 if (handle->open && handle->handler->start)
392 handle->handler->start(handle);
397 * input_release_device - release previously grabbed device
398 * @handle: input handle that owns the device
400 * Releases previously grabbed device so that other input handles can
401 * start receiving input events. Upon release all handlers attached
402 * to the device have their start() method called so they have a change
403 * to synchronize device state with the rest of the system.
405 void input_release_device(struct input_handle *handle)
407 struct input_dev *dev = handle->dev;
409 mutex_lock(&dev->mutex);
410 __input_release_device(handle);
411 mutex_unlock(&dev->mutex);
413 EXPORT_SYMBOL(input_release_device);
416 * input_open_device - open input device
417 * @handle: handle through which device is being accessed
419 * This function should be called by input handlers when they
420 * want to start receive events from given input device.
422 int input_open_device(struct input_handle *handle)
424 struct input_dev *dev = handle->dev;
425 int retval;
427 retval = mutex_lock_interruptible(&dev->mutex);
428 if (retval)
429 return retval;
431 if (dev->going_away) {
432 retval = -ENODEV;
433 goto out;
436 handle->open++;
438 if (!dev->users++ && dev->open)
439 retval = dev->open(dev);
441 if (retval) {
442 dev->users--;
443 if (!--handle->open) {
445 * Make sure we are not delivering any more events
446 * through this handle
448 synchronize_rcu();
452 out:
453 mutex_unlock(&dev->mutex);
454 return retval;
456 EXPORT_SYMBOL(input_open_device);
458 int input_flush_device(struct input_handle *handle, struct file *file)
460 struct input_dev *dev = handle->dev;
461 int retval;
463 retval = mutex_lock_interruptible(&dev->mutex);
464 if (retval)
465 return retval;
467 if (dev->flush)
468 retval = dev->flush(dev, file);
470 mutex_unlock(&dev->mutex);
471 return retval;
473 EXPORT_SYMBOL(input_flush_device);
476 * input_close_device - close input device
477 * @handle: handle through which device is being accessed
479 * This function should be called by input handlers when they
480 * want to stop receive events from given input device.
482 void input_close_device(struct input_handle *handle)
484 struct input_dev *dev = handle->dev;
486 mutex_lock(&dev->mutex);
488 __input_release_device(handle);
490 if (!--dev->users && dev->close)
491 dev->close(dev);
493 if (!--handle->open) {
495 * synchronize_rcu() makes sure that input_pass_event()
496 * completed and that no more input events are delivered
497 * through this handle
499 synchronize_rcu();
502 mutex_unlock(&dev->mutex);
504 EXPORT_SYMBOL(input_close_device);
507 * Prepare device for unregistering
509 static void input_disconnect_device(struct input_dev *dev)
511 struct input_handle *handle;
512 int code;
515 * Mark device as going away. Note that we take dev->mutex here
516 * not to protect access to dev->going_away but rather to ensure
517 * that there are no threads in the middle of input_open_device()
519 mutex_lock(&dev->mutex);
520 dev->going_away = true;
521 mutex_unlock(&dev->mutex);
523 spin_lock_irq(&dev->event_lock);
526 * Simulate keyup events for all pressed keys so that handlers
527 * are not left with "stuck" keys. The driver may continue
528 * generate events even after we done here but they will not
529 * reach any handlers.
531 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
532 for (code = 0; code <= KEY_MAX; code++) {
533 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
534 __test_and_clear_bit(code, dev->key)) {
535 input_pass_event(dev, EV_KEY, code, 0);
538 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
541 list_for_each_entry(handle, &dev->h_list, d_node)
542 handle->open = 0;
544 spin_unlock_irq(&dev->event_lock);
547 static int input_fetch_keycode(struct input_dev *dev, int scancode)
549 switch (dev->keycodesize) {
550 case 1:
551 return ((u8 *)dev->keycode)[scancode];
553 case 2:
554 return ((u16 *)dev->keycode)[scancode];
556 default:
557 return ((u32 *)dev->keycode)[scancode];
561 static int input_default_getkeycode(struct input_dev *dev,
562 int scancode, int *keycode)
564 if (!dev->keycodesize)
565 return -EINVAL;
567 if (scancode >= dev->keycodemax)
568 return -EINVAL;
570 *keycode = input_fetch_keycode(dev, scancode);
572 return 0;
575 static int input_default_setkeycode(struct input_dev *dev,
576 int scancode, int keycode)
578 int old_keycode;
579 int i;
581 if (scancode >= dev->keycodemax)
582 return -EINVAL;
584 if (!dev->keycodesize)
585 return -EINVAL;
587 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
588 return -EINVAL;
590 switch (dev->keycodesize) {
591 case 1: {
592 u8 *k = (u8 *)dev->keycode;
593 old_keycode = k[scancode];
594 k[scancode] = keycode;
595 break;
597 case 2: {
598 u16 *k = (u16 *)dev->keycode;
599 old_keycode = k[scancode];
600 k[scancode] = keycode;
601 break;
603 default: {
604 u32 *k = (u32 *)dev->keycode;
605 old_keycode = k[scancode];
606 k[scancode] = keycode;
607 break;
611 clear_bit(old_keycode, dev->keybit);
612 set_bit(keycode, dev->keybit);
614 for (i = 0; i < dev->keycodemax; i++) {
615 if (input_fetch_keycode(dev, i) == old_keycode) {
616 set_bit(old_keycode, dev->keybit);
617 break; /* Setting the bit twice is useless, so break */
621 return 0;
625 * input_get_keycode - retrieve keycode currently mapped to a given scancode
626 * @dev: input device which keymap is being queried
627 * @scancode: scancode (or its equivalent for device in question) for which
628 * keycode is needed
629 * @keycode: result
631 * This function should be called by anyone interested in retrieving current
632 * keymap. Presently keyboard and evdev handlers use it.
634 int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
636 if (scancode < 0)
637 return -EINVAL;
639 return dev->getkeycode(dev, scancode, keycode);
641 EXPORT_SYMBOL(input_get_keycode);
644 * input_get_keycode - assign new keycode to a given scancode
645 * @dev: input device which keymap is being updated
646 * @scancode: scancode (or its equivalent for device in question)
647 * @keycode: new keycode to be assigned to the scancode
649 * This function should be called by anyone needing to update current
650 * keymap. Presently keyboard and evdev handlers use it.
652 int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
654 unsigned long flags;
655 int old_keycode;
656 int retval;
658 if (scancode < 0)
659 return -EINVAL;
661 if (keycode < 0 || keycode > KEY_MAX)
662 return -EINVAL;
664 spin_lock_irqsave(&dev->event_lock, flags);
666 retval = dev->getkeycode(dev, scancode, &old_keycode);
667 if (retval)
668 goto out;
670 retval = dev->setkeycode(dev, scancode, keycode);
671 if (retval)
672 goto out;
675 * Simulate keyup event if keycode is not present
676 * in the keymap anymore
678 if (test_bit(EV_KEY, dev->evbit) &&
679 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
680 __test_and_clear_bit(old_keycode, dev->key)) {
682 input_pass_event(dev, EV_KEY, old_keycode, 0);
683 if (dev->sync)
684 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
687 out:
688 spin_unlock_irqrestore(&dev->event_lock, flags);
690 return retval;
692 EXPORT_SYMBOL(input_set_keycode);
694 #define MATCH_BIT(bit, max) \
695 for (i = 0; i < BITS_TO_LONGS(max); i++) \
696 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
697 break; \
698 if (i != BITS_TO_LONGS(max)) \
699 continue;
701 static const struct input_device_id *input_match_device(const struct input_device_id *id,
702 struct input_dev *dev)
704 int i;
706 for (; id->flags || id->driver_info; id++) {
708 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
709 if (id->bustype != dev->id.bustype)
710 continue;
712 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
713 if (id->vendor != dev->id.vendor)
714 continue;
716 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
717 if (id->product != dev->id.product)
718 continue;
720 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
721 if (id->version != dev->id.version)
722 continue;
724 MATCH_BIT(evbit, EV_MAX);
725 MATCH_BIT(keybit, KEY_MAX);
726 MATCH_BIT(relbit, REL_MAX);
727 MATCH_BIT(absbit, ABS_MAX);
728 MATCH_BIT(mscbit, MSC_MAX);
729 MATCH_BIT(ledbit, LED_MAX);
730 MATCH_BIT(sndbit, SND_MAX);
731 MATCH_BIT(ffbit, FF_MAX);
732 MATCH_BIT(swbit, SW_MAX);
734 return id;
737 return NULL;
740 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
742 const struct input_device_id *id;
743 int error;
745 if (handler->blacklist && input_match_device(handler->blacklist, dev))
746 return -ENODEV;
748 id = input_match_device(handler->id_table, dev);
749 if (!id)
750 return -ENODEV;
752 error = handler->connect(handler, dev, id);
753 if (error && error != -ENODEV)
754 printk(KERN_ERR
755 "input: failed to attach handler %s to device %s, "
756 "error: %d\n",
757 handler->name, kobject_name(&dev->dev.kobj), error);
759 return error;
762 #ifdef CONFIG_COMPAT
764 static int input_bits_to_string(char *buf, int buf_size,
765 unsigned long bits, bool skip_empty)
767 int len = 0;
769 if (INPUT_COMPAT_TEST) {
770 u32 dword = bits >> 32;
771 if (dword || !skip_empty)
772 len += snprintf(buf, buf_size, "%x ", dword);
774 dword = bits & 0xffffffffUL;
775 if (dword || !skip_empty || len)
776 len += snprintf(buf + len, max(buf_size - len, 0),
777 "%x", dword);
778 } else {
779 if (bits || !skip_empty)
780 len += snprintf(buf, buf_size, "%lx", bits);
783 return len;
786 #else /* !CONFIG_COMPAT */
788 static int input_bits_to_string(char *buf, int buf_size,
789 unsigned long bits, bool skip_empty)
791 return bits || !skip_empty ?
792 snprintf(buf, buf_size, "%lx", bits) : 0;
795 #endif
797 #ifdef CONFIG_PROC_FS
799 static struct proc_dir_entry *proc_bus_input_dir;
800 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
801 static int input_devices_state;
803 static inline void input_wakeup_procfs_readers(void)
805 input_devices_state++;
806 wake_up(&input_devices_poll_wait);
809 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
811 poll_wait(file, &input_devices_poll_wait, wait);
812 if (file->f_version != input_devices_state) {
813 file->f_version = input_devices_state;
814 return POLLIN | POLLRDNORM;
817 return 0;
820 union input_seq_state {
821 struct {
822 unsigned short pos;
823 bool mutex_acquired;
825 void *p;
828 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
830 union input_seq_state *state = (union input_seq_state *)&seq->private;
831 int error;
833 /* We need to fit into seq->private pointer */
834 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
836 error = mutex_lock_interruptible(&input_mutex);
837 if (error) {
838 state->mutex_acquired = false;
839 return ERR_PTR(error);
842 state->mutex_acquired = true;
844 return seq_list_start(&input_dev_list, *pos);
847 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
849 return seq_list_next(v, &input_dev_list, pos);
852 static void input_seq_stop(struct seq_file *seq, void *v)
854 union input_seq_state *state = (union input_seq_state *)&seq->private;
856 if (state->mutex_acquired)
857 mutex_unlock(&input_mutex);
860 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
861 unsigned long *bitmap, int max)
863 int i;
864 bool skip_empty = true;
865 char buf[18];
867 seq_printf(seq, "B: %s=", name);
869 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
870 if (input_bits_to_string(buf, sizeof(buf),
871 bitmap[i], skip_empty)) {
872 skip_empty = false;
873 seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
878 * If no output was produced print a single 0.
880 if (skip_empty)
881 seq_puts(seq, "0");
883 seq_putc(seq, '\n');
886 static int input_devices_seq_show(struct seq_file *seq, void *v)
888 struct input_dev *dev = container_of(v, struct input_dev, node);
889 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
890 struct input_handle *handle;
892 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
893 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
895 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
896 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
897 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
898 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
899 seq_printf(seq, "H: Handlers=");
901 list_for_each_entry(handle, &dev->h_list, d_node)
902 seq_printf(seq, "%s ", handle->name);
903 seq_putc(seq, '\n');
905 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
906 if (test_bit(EV_KEY, dev->evbit))
907 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
908 if (test_bit(EV_REL, dev->evbit))
909 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
910 if (test_bit(EV_ABS, dev->evbit))
911 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
912 if (test_bit(EV_MSC, dev->evbit))
913 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
914 if (test_bit(EV_LED, dev->evbit))
915 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
916 if (test_bit(EV_SND, dev->evbit))
917 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
918 if (test_bit(EV_FF, dev->evbit))
919 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
920 if (test_bit(EV_SW, dev->evbit))
921 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
923 seq_putc(seq, '\n');
925 kfree(path);
926 return 0;
929 static const struct seq_operations input_devices_seq_ops = {
930 .start = input_devices_seq_start,
931 .next = input_devices_seq_next,
932 .stop = input_seq_stop,
933 .show = input_devices_seq_show,
936 static int input_proc_devices_open(struct inode *inode, struct file *file)
938 return seq_open(file, &input_devices_seq_ops);
941 static const struct file_operations input_devices_fileops = {
942 .owner = THIS_MODULE,
943 .open = input_proc_devices_open,
944 .poll = input_proc_devices_poll,
945 .read = seq_read,
946 .llseek = seq_lseek,
947 .release = seq_release,
950 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
952 union input_seq_state *state = (union input_seq_state *)&seq->private;
953 int error;
955 /* We need to fit into seq->private pointer */
956 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
958 error = mutex_lock_interruptible(&input_mutex);
959 if (error) {
960 state->mutex_acquired = false;
961 return ERR_PTR(error);
964 state->mutex_acquired = true;
965 state->pos = *pos;
967 return seq_list_start(&input_handler_list, *pos);
970 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
972 union input_seq_state *state = (union input_seq_state *)&seq->private;
974 state->pos = *pos + 1;
975 return seq_list_next(v, &input_handler_list, pos);
978 static int input_handlers_seq_show(struct seq_file *seq, void *v)
980 struct input_handler *handler = container_of(v, struct input_handler, node);
981 union input_seq_state *state = (union input_seq_state *)&seq->private;
983 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
984 if (handler->fops)
985 seq_printf(seq, " Minor=%d", handler->minor);
986 seq_putc(seq, '\n');
988 return 0;
991 static const struct seq_operations input_handlers_seq_ops = {
992 .start = input_handlers_seq_start,
993 .next = input_handlers_seq_next,
994 .stop = input_seq_stop,
995 .show = input_handlers_seq_show,
998 static int input_proc_handlers_open(struct inode *inode, struct file *file)
1000 return seq_open(file, &input_handlers_seq_ops);
1003 static const struct file_operations input_handlers_fileops = {
1004 .owner = THIS_MODULE,
1005 .open = input_proc_handlers_open,
1006 .read = seq_read,
1007 .llseek = seq_lseek,
1008 .release = seq_release,
1011 static int __init input_proc_init(void)
1013 struct proc_dir_entry *entry;
1015 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
1016 if (!proc_bus_input_dir)
1017 return -ENOMEM;
1019 entry = proc_create("devices", 0, proc_bus_input_dir,
1020 &input_devices_fileops);
1021 if (!entry)
1022 goto fail1;
1024 entry = proc_create("handlers", 0, proc_bus_input_dir,
1025 &input_handlers_fileops);
1026 if (!entry)
1027 goto fail2;
1029 return 0;
1031 fail2: remove_proc_entry("devices", proc_bus_input_dir);
1032 fail1: remove_proc_entry("bus/input", NULL);
1033 return -ENOMEM;
1036 static void input_proc_exit(void)
1038 remove_proc_entry("devices", proc_bus_input_dir);
1039 remove_proc_entry("handlers", proc_bus_input_dir);
1040 remove_proc_entry("bus/input", NULL);
1043 #else /* !CONFIG_PROC_FS */
1044 static inline void input_wakeup_procfs_readers(void) { }
1045 static inline int input_proc_init(void) { return 0; }
1046 static inline void input_proc_exit(void) { }
1047 #endif
1049 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
1050 static ssize_t input_dev_show_##name(struct device *dev, \
1051 struct device_attribute *attr, \
1052 char *buf) \
1054 struct input_dev *input_dev = to_input_dev(dev); \
1056 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1057 input_dev->name ? input_dev->name : ""); \
1059 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
1061 INPUT_DEV_STRING_ATTR_SHOW(name);
1062 INPUT_DEV_STRING_ATTR_SHOW(phys);
1063 INPUT_DEV_STRING_ATTR_SHOW(uniq);
1065 static int input_print_modalias_bits(char *buf, int size,
1066 char name, unsigned long *bm,
1067 unsigned int min_bit, unsigned int max_bit)
1069 int len = 0, i;
1071 len += snprintf(buf, max(size, 0), "%c", name);
1072 for (i = min_bit; i < max_bit; i++)
1073 if (bm[BIT_WORD(i)] & BIT_MASK(i))
1074 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
1075 return len;
1078 static int input_print_modalias(char *buf, int size, struct input_dev *id,
1079 int add_cr)
1081 int len;
1083 len = snprintf(buf, max(size, 0),
1084 "input:b%04Xv%04Xp%04Xe%04X-",
1085 id->id.bustype, id->id.vendor,
1086 id->id.product, id->id.version);
1088 len += input_print_modalias_bits(buf + len, size - len,
1089 'e', id->evbit, 0, EV_MAX);
1090 len += input_print_modalias_bits(buf + len, size - len,
1091 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1092 len += input_print_modalias_bits(buf + len, size - len,
1093 'r', id->relbit, 0, REL_MAX);
1094 len += input_print_modalias_bits(buf + len, size - len,
1095 'a', id->absbit, 0, ABS_MAX);
1096 len += input_print_modalias_bits(buf + len, size - len,
1097 'm', id->mscbit, 0, MSC_MAX);
1098 len += input_print_modalias_bits(buf + len, size - len,
1099 'l', id->ledbit, 0, LED_MAX);
1100 len += input_print_modalias_bits(buf + len, size - len,
1101 's', id->sndbit, 0, SND_MAX);
1102 len += input_print_modalias_bits(buf + len, size - len,
1103 'f', id->ffbit, 0, FF_MAX);
1104 len += input_print_modalias_bits(buf + len, size - len,
1105 'w', id->swbit, 0, SW_MAX);
1107 if (add_cr)
1108 len += snprintf(buf + len, max(size - len, 0), "\n");
1110 return len;
1113 static ssize_t input_dev_show_modalias(struct device *dev,
1114 struct device_attribute *attr,
1115 char *buf)
1117 struct input_dev *id = to_input_dev(dev);
1118 ssize_t len;
1120 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1122 return min_t(int, len, PAGE_SIZE);
1124 static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
1126 static struct attribute *input_dev_attrs[] = {
1127 &dev_attr_name.attr,
1128 &dev_attr_phys.attr,
1129 &dev_attr_uniq.attr,
1130 &dev_attr_modalias.attr,
1131 NULL
1134 static struct attribute_group input_dev_attr_group = {
1135 .attrs = input_dev_attrs,
1138 #define INPUT_DEV_ID_ATTR(name) \
1139 static ssize_t input_dev_show_id_##name(struct device *dev, \
1140 struct device_attribute *attr, \
1141 char *buf) \
1143 struct input_dev *input_dev = to_input_dev(dev); \
1144 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1146 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
1148 INPUT_DEV_ID_ATTR(bustype);
1149 INPUT_DEV_ID_ATTR(vendor);
1150 INPUT_DEV_ID_ATTR(product);
1151 INPUT_DEV_ID_ATTR(version);
1153 static struct attribute *input_dev_id_attrs[] = {
1154 &dev_attr_bustype.attr,
1155 &dev_attr_vendor.attr,
1156 &dev_attr_product.attr,
1157 &dev_attr_version.attr,
1158 NULL
1161 static struct attribute_group input_dev_id_attr_group = {
1162 .name = "id",
1163 .attrs = input_dev_id_attrs,
1166 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1167 int max, int add_cr)
1169 int i;
1170 int len = 0;
1171 bool skip_empty = true;
1173 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1174 len += input_bits_to_string(buf + len, max(buf_size - len, 0),
1175 bitmap[i], skip_empty);
1176 if (len) {
1177 skip_empty = false;
1178 if (i > 0)
1179 len += snprintf(buf + len, max(buf_size - len, 0), " ");
1184 * If no output was produced print a single 0.
1186 if (len == 0)
1187 len = snprintf(buf, buf_size, "%d", 0);
1189 if (add_cr)
1190 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1192 return len;
1195 #define INPUT_DEV_CAP_ATTR(ev, bm) \
1196 static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1197 struct device_attribute *attr, \
1198 char *buf) \
1200 struct input_dev *input_dev = to_input_dev(dev); \
1201 int len = input_print_bitmap(buf, PAGE_SIZE, \
1202 input_dev->bm##bit, ev##_MAX, \
1203 true); \
1204 return min_t(int, len, PAGE_SIZE); \
1206 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
1208 INPUT_DEV_CAP_ATTR(EV, ev);
1209 INPUT_DEV_CAP_ATTR(KEY, key);
1210 INPUT_DEV_CAP_ATTR(REL, rel);
1211 INPUT_DEV_CAP_ATTR(ABS, abs);
1212 INPUT_DEV_CAP_ATTR(MSC, msc);
1213 INPUT_DEV_CAP_ATTR(LED, led);
1214 INPUT_DEV_CAP_ATTR(SND, snd);
1215 INPUT_DEV_CAP_ATTR(FF, ff);
1216 INPUT_DEV_CAP_ATTR(SW, sw);
1218 static struct attribute *input_dev_caps_attrs[] = {
1219 &dev_attr_ev.attr,
1220 &dev_attr_key.attr,
1221 &dev_attr_rel.attr,
1222 &dev_attr_abs.attr,
1223 &dev_attr_msc.attr,
1224 &dev_attr_led.attr,
1225 &dev_attr_snd.attr,
1226 &dev_attr_ff.attr,
1227 &dev_attr_sw.attr,
1228 NULL
1231 static struct attribute_group input_dev_caps_attr_group = {
1232 .name = "capabilities",
1233 .attrs = input_dev_caps_attrs,
1236 static const struct attribute_group *input_dev_attr_groups[] = {
1237 &input_dev_attr_group,
1238 &input_dev_id_attr_group,
1239 &input_dev_caps_attr_group,
1240 NULL
1243 static void input_dev_release(struct device *device)
1245 struct input_dev *dev = to_input_dev(device);
1247 input_ff_destroy(dev);
1248 kfree(dev);
1250 module_put(THIS_MODULE);
1254 * Input uevent interface - loading event handlers based on
1255 * device bitfields.
1257 static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
1258 const char *name, unsigned long *bitmap, int max)
1260 int len;
1262 if (add_uevent_var(env, "%s=", name))
1263 return -ENOMEM;
1265 len = input_print_bitmap(&env->buf[env->buflen - 1],
1266 sizeof(env->buf) - env->buflen,
1267 bitmap, max, false);
1268 if (len >= (sizeof(env->buf) - env->buflen))
1269 return -ENOMEM;
1271 env->buflen += len;
1272 return 0;
1275 static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
1276 struct input_dev *dev)
1278 int len;
1280 if (add_uevent_var(env, "MODALIAS="))
1281 return -ENOMEM;
1283 len = input_print_modalias(&env->buf[env->buflen - 1],
1284 sizeof(env->buf) - env->buflen,
1285 dev, 0);
1286 if (len >= (sizeof(env->buf) - env->buflen))
1287 return -ENOMEM;
1289 env->buflen += len;
1290 return 0;
1293 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1294 do { \
1295 int err = add_uevent_var(env, fmt, val); \
1296 if (err) \
1297 return err; \
1298 } while (0)
1300 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1301 do { \
1302 int err = input_add_uevent_bm_var(env, name, bm, max); \
1303 if (err) \
1304 return err; \
1305 } while (0)
1307 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1308 do { \
1309 int err = input_add_uevent_modalias_var(env, dev); \
1310 if (err) \
1311 return err; \
1312 } while (0)
1314 static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1316 struct input_dev *dev = to_input_dev(device);
1318 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1319 dev->id.bustype, dev->id.vendor,
1320 dev->id.product, dev->id.version);
1321 if (dev->name)
1322 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1323 if (dev->phys)
1324 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
1325 if (dev->uniq)
1326 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1328 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1329 if (test_bit(EV_KEY, dev->evbit))
1330 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1331 if (test_bit(EV_REL, dev->evbit))
1332 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1333 if (test_bit(EV_ABS, dev->evbit))
1334 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1335 if (test_bit(EV_MSC, dev->evbit))
1336 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1337 if (test_bit(EV_LED, dev->evbit))
1338 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1339 if (test_bit(EV_SND, dev->evbit))
1340 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1341 if (test_bit(EV_FF, dev->evbit))
1342 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1343 if (test_bit(EV_SW, dev->evbit))
1344 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1346 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
1348 return 0;
1351 #define INPUT_DO_TOGGLE(dev, type, bits, on) \
1352 do { \
1353 int i; \
1354 bool active; \
1356 if (!test_bit(EV_##type, dev->evbit)) \
1357 break; \
1359 for (i = 0; i < type##_MAX; i++) { \
1360 if (!test_bit(i, dev->bits##bit)) \
1361 continue; \
1363 active = test_bit(i, dev->bits); \
1364 if (!active && !on) \
1365 continue; \
1367 dev->event(dev, EV_##type, i, on ? active : 0); \
1369 } while (0)
1371 #ifdef CONFIG_PM
1372 static void input_dev_reset(struct input_dev *dev, bool activate)
1374 if (!dev->event)
1375 return;
1377 INPUT_DO_TOGGLE(dev, LED, led, activate);
1378 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1380 if (activate && test_bit(EV_REP, dev->evbit)) {
1381 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1382 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1386 static int input_dev_suspend(struct device *dev)
1388 struct input_dev *input_dev = to_input_dev(dev);
1390 mutex_lock(&input_dev->mutex);
1391 input_dev_reset(input_dev, false);
1392 mutex_unlock(&input_dev->mutex);
1394 return 0;
1397 static int input_dev_resume(struct device *dev)
1399 struct input_dev *input_dev = to_input_dev(dev);
1401 mutex_lock(&input_dev->mutex);
1402 input_dev_reset(input_dev, true);
1403 mutex_unlock(&input_dev->mutex);
1405 return 0;
1408 static const struct dev_pm_ops input_dev_pm_ops = {
1409 .suspend = input_dev_suspend,
1410 .resume = input_dev_resume,
1411 .poweroff = input_dev_suspend,
1412 .restore = input_dev_resume,
1414 #endif /* CONFIG_PM */
1416 static struct device_type input_dev_type = {
1417 .groups = input_dev_attr_groups,
1418 .release = input_dev_release,
1419 .uevent = input_dev_uevent,
1420 #ifdef CONFIG_PM
1421 .pm = &input_dev_pm_ops,
1422 #endif
1425 static char *input_devnode(struct device *dev, mode_t *mode)
1427 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1430 struct class input_class = {
1431 .name = "input",
1432 .devnode = input_devnode,
1434 EXPORT_SYMBOL_GPL(input_class);
1437 * input_allocate_device - allocate memory for new input device
1439 * Returns prepared struct input_dev or NULL.
1441 * NOTE: Use input_free_device() to free devices that have not been
1442 * registered; input_unregister_device() should be used for already
1443 * registered devices.
1445 struct input_dev *input_allocate_device(void)
1447 struct input_dev *dev;
1449 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1450 if (dev) {
1451 dev->dev.type = &input_dev_type;
1452 dev->dev.class = &input_class;
1453 device_initialize(&dev->dev);
1454 mutex_init(&dev->mutex);
1455 spin_lock_init(&dev->event_lock);
1456 INIT_LIST_HEAD(&dev->h_list);
1457 INIT_LIST_HEAD(&dev->node);
1459 __module_get(THIS_MODULE);
1462 return dev;
1464 EXPORT_SYMBOL(input_allocate_device);
1467 * input_free_device - free memory occupied by input_dev structure
1468 * @dev: input device to free
1470 * This function should only be used if input_register_device()
1471 * was not called yet or if it failed. Once device was registered
1472 * use input_unregister_device() and memory will be freed once last
1473 * reference to the device is dropped.
1475 * Device should be allocated by input_allocate_device().
1477 * NOTE: If there are references to the input device then memory
1478 * will not be freed until last reference is dropped.
1480 void input_free_device(struct input_dev *dev)
1482 if (dev)
1483 input_put_device(dev);
1485 EXPORT_SYMBOL(input_free_device);
1488 * input_set_capability - mark device as capable of a certain event
1489 * @dev: device that is capable of emitting or accepting event
1490 * @type: type of the event (EV_KEY, EV_REL, etc...)
1491 * @code: event code
1493 * In addition to setting up corresponding bit in appropriate capability
1494 * bitmap the function also adjusts dev->evbit.
1496 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1498 switch (type) {
1499 case EV_KEY:
1500 __set_bit(code, dev->keybit);
1501 break;
1503 case EV_REL:
1504 __set_bit(code, dev->relbit);
1505 break;
1507 case EV_ABS:
1508 __set_bit(code, dev->absbit);
1509 break;
1511 case EV_MSC:
1512 __set_bit(code, dev->mscbit);
1513 break;
1515 case EV_SW:
1516 __set_bit(code, dev->swbit);
1517 break;
1519 case EV_LED:
1520 __set_bit(code, dev->ledbit);
1521 break;
1523 case EV_SND:
1524 __set_bit(code, dev->sndbit);
1525 break;
1527 case EV_FF:
1528 __set_bit(code, dev->ffbit);
1529 break;
1531 case EV_PWR:
1532 /* do nothing */
1533 break;
1535 default:
1536 printk(KERN_ERR
1537 "input_set_capability: unknown type %u (code %u)\n",
1538 type, code);
1539 dump_stack();
1540 return;
1543 __set_bit(type, dev->evbit);
1545 EXPORT_SYMBOL(input_set_capability);
1548 * input_register_device - register device with input core
1549 * @dev: device to be registered
1551 * This function registers device with input core. The device must be
1552 * allocated with input_allocate_device() and all it's capabilities
1553 * set up before registering.
1554 * If function fails the device must be freed with input_free_device().
1555 * Once device has been successfully registered it can be unregistered
1556 * with input_unregister_device(); input_free_device() should not be
1557 * called in this case.
1559 int input_register_device(struct input_dev *dev)
1561 static atomic_t input_no = ATOMIC_INIT(0);
1562 struct input_handler *handler;
1563 const char *path;
1564 int error;
1566 __set_bit(EV_SYN, dev->evbit);
1569 * If delay and period are pre-set by the driver, then autorepeating
1570 * is handled by the driver itself and we don't do it in input.c.
1573 init_timer(&dev->timer);
1574 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1575 dev->timer.data = (long) dev;
1576 dev->timer.function = input_repeat_key;
1577 dev->rep[REP_DELAY] = 250;
1578 dev->rep[REP_PERIOD] = 33;
1581 if (!dev->getkeycode)
1582 dev->getkeycode = input_default_getkeycode;
1584 if (!dev->setkeycode)
1585 dev->setkeycode = input_default_setkeycode;
1587 dev_set_name(&dev->dev, "input%ld",
1588 (unsigned long) atomic_inc_return(&input_no) - 1);
1590 error = device_add(&dev->dev);
1591 if (error)
1592 return error;
1594 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1595 printk(KERN_INFO "input: %s as %s\n",
1596 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1597 kfree(path);
1599 error = mutex_lock_interruptible(&input_mutex);
1600 if (error) {
1601 device_del(&dev->dev);
1602 return error;
1605 list_add_tail(&dev->node, &input_dev_list);
1607 list_for_each_entry(handler, &input_handler_list, node)
1608 input_attach_handler(dev, handler);
1610 input_wakeup_procfs_readers();
1612 mutex_unlock(&input_mutex);
1614 return 0;
1616 EXPORT_SYMBOL(input_register_device);
1619 * input_unregister_device - unregister previously registered device
1620 * @dev: device to be unregistered
1622 * This function unregisters an input device. Once device is unregistered
1623 * the caller should not try to access it as it may get freed at any moment.
1625 void input_unregister_device(struct input_dev *dev)
1627 struct input_handle *handle, *next;
1629 input_disconnect_device(dev);
1631 mutex_lock(&input_mutex);
1633 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1634 handle->handler->disconnect(handle);
1635 WARN_ON(!list_empty(&dev->h_list));
1637 del_timer_sync(&dev->timer);
1638 list_del_init(&dev->node);
1640 input_wakeup_procfs_readers();
1642 mutex_unlock(&input_mutex);
1644 device_unregister(&dev->dev);
1646 EXPORT_SYMBOL(input_unregister_device);
1649 * input_register_handler - register a new input handler
1650 * @handler: handler to be registered
1652 * This function registers a new input handler (interface) for input
1653 * devices in the system and attaches it to all input devices that
1654 * are compatible with the handler.
1656 int input_register_handler(struct input_handler *handler)
1658 struct input_dev *dev;
1659 int retval;
1661 retval = mutex_lock_interruptible(&input_mutex);
1662 if (retval)
1663 return retval;
1665 INIT_LIST_HEAD(&handler->h_list);
1667 if (handler->fops != NULL) {
1668 if (input_table[handler->minor >> 5]) {
1669 retval = -EBUSY;
1670 goto out;
1672 input_table[handler->minor >> 5] = handler;
1675 list_add_tail(&handler->node, &input_handler_list);
1677 list_for_each_entry(dev, &input_dev_list, node)
1678 input_attach_handler(dev, handler);
1680 input_wakeup_procfs_readers();
1682 out:
1683 mutex_unlock(&input_mutex);
1684 return retval;
1686 EXPORT_SYMBOL(input_register_handler);
1689 * input_unregister_handler - unregisters an input handler
1690 * @handler: handler to be unregistered
1692 * This function disconnects a handler from its input devices and
1693 * removes it from lists of known handlers.
1695 void input_unregister_handler(struct input_handler *handler)
1697 struct input_handle *handle, *next;
1699 mutex_lock(&input_mutex);
1701 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1702 handler->disconnect(handle);
1703 WARN_ON(!list_empty(&handler->h_list));
1705 list_del_init(&handler->node);
1707 if (handler->fops != NULL)
1708 input_table[handler->minor >> 5] = NULL;
1710 input_wakeup_procfs_readers();
1712 mutex_unlock(&input_mutex);
1714 EXPORT_SYMBOL(input_unregister_handler);
1717 * input_register_handle - register a new input handle
1718 * @handle: handle to register
1720 * This function puts a new input handle onto device's
1721 * and handler's lists so that events can flow through
1722 * it once it is opened using input_open_device().
1724 * This function is supposed to be called from handler's
1725 * connect() method.
1727 int input_register_handle(struct input_handle *handle)
1729 struct input_handler *handler = handle->handler;
1730 struct input_dev *dev = handle->dev;
1731 int error;
1734 * We take dev->mutex here to prevent race with
1735 * input_release_device().
1737 error = mutex_lock_interruptible(&dev->mutex);
1738 if (error)
1739 return error;
1740 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1741 mutex_unlock(&dev->mutex);
1744 * Since we are supposed to be called from ->connect()
1745 * which is mutually exclusive with ->disconnect()
1746 * we can't be racing with input_unregister_handle()
1747 * and so separate lock is not needed here.
1749 list_add_tail(&handle->h_node, &handler->h_list);
1751 if (handler->start)
1752 handler->start(handle);
1754 return 0;
1756 EXPORT_SYMBOL(input_register_handle);
1759 * input_unregister_handle - unregister an input handle
1760 * @handle: handle to unregister
1762 * This function removes input handle from device's
1763 * and handler's lists.
1765 * This function is supposed to be called from handler's
1766 * disconnect() method.
1768 void input_unregister_handle(struct input_handle *handle)
1770 struct input_dev *dev = handle->dev;
1772 list_del_init(&handle->h_node);
1775 * Take dev->mutex to prevent race with input_release_device().
1777 mutex_lock(&dev->mutex);
1778 list_del_rcu(&handle->d_node);
1779 mutex_unlock(&dev->mutex);
1780 synchronize_rcu();
1782 EXPORT_SYMBOL(input_unregister_handle);
1784 static int input_open_file(struct inode *inode, struct file *file)
1786 struct input_handler *handler;
1787 const struct file_operations *old_fops, *new_fops = NULL;
1788 int err;
1790 lock_kernel();
1791 /* No load-on-demand here? */
1792 handler = input_table[iminor(inode) >> 5];
1793 if (!handler || !(new_fops = fops_get(handler->fops))) {
1794 err = -ENODEV;
1795 goto out;
1799 * That's _really_ odd. Usually NULL ->open means "nothing special",
1800 * not "no device". Oh, well...
1802 if (!new_fops->open) {
1803 fops_put(new_fops);
1804 err = -ENODEV;
1805 goto out;
1807 old_fops = file->f_op;
1808 file->f_op = new_fops;
1810 err = new_fops->open(inode, file);
1812 if (err) {
1813 fops_put(file->f_op);
1814 file->f_op = fops_get(old_fops);
1816 fops_put(old_fops);
1817 out:
1818 unlock_kernel();
1819 return err;
1822 static const struct file_operations input_fops = {
1823 .owner = THIS_MODULE,
1824 .open = input_open_file,
1827 static void __init input_init_abs_bypass(void)
1829 const unsigned int *p;
1831 for (p = input_abs_bypass_init_data; *p; p++)
1832 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1835 static int __init input_init(void)
1837 int err;
1839 input_init_abs_bypass();
1841 err = class_register(&input_class);
1842 if (err) {
1843 printk(KERN_ERR "input: unable to register input_dev class\n");
1844 return err;
1847 err = input_proc_init();
1848 if (err)
1849 goto fail1;
1851 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1852 if (err) {
1853 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1854 goto fail2;
1857 return 0;
1859 fail2: input_proc_exit();
1860 fail1: class_unregister(&input_class);
1861 return err;
1864 static void __exit input_exit(void)
1866 input_proc_exit();
1867 unregister_chrdev(INPUT_MAJOR, "input");
1868 class_unregister(&input_class);
1871 subsys_initcall(input_init);
1872 module_exit(input_exit);