4 * Copyright (c) 1999-2002 Vojtech Pavlik
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/sched.h>
15 #include <linux/smp_lock.h>
16 #include <linux/input.h>
17 #include <linux/module.h>
18 #include <linux/random.h>
19 #include <linux/major.h>
20 #include <linux/proc_fs.h>
21 #include <linux/interrupt.h>
22 #include <linux/poll.h>
23 #include <linux/device.h>
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
26 MODULE_DESCRIPTION("Input core");
27 MODULE_LICENSE("GPL");
29 EXPORT_SYMBOL(input_allocate_device
);
30 EXPORT_SYMBOL(input_register_device
);
31 EXPORT_SYMBOL(input_unregister_device
);
32 EXPORT_SYMBOL(input_register_handler
);
33 EXPORT_SYMBOL(input_unregister_handler
);
34 EXPORT_SYMBOL(input_grab_device
);
35 EXPORT_SYMBOL(input_release_device
);
36 EXPORT_SYMBOL(input_open_device
);
37 EXPORT_SYMBOL(input_close_device
);
38 EXPORT_SYMBOL(input_accept_process
);
39 EXPORT_SYMBOL(input_flush_device
);
40 EXPORT_SYMBOL(input_event
);
41 EXPORT_SYMBOL_GPL(input_class
);
43 #define INPUT_DEVICES 256
45 static LIST_HEAD(input_dev_list
);
46 static LIST_HEAD(input_handler_list
);
48 static struct input_handler
*input_table
[8];
50 void input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
52 struct input_handle
*handle
;
54 if (type
> EV_MAX
|| !test_bit(type
, dev
->evbit
))
57 add_input_randomness(type
, code
, value
);
64 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
68 if (dev
->sync
) return;
76 if (code
> KEY_MAX
|| !test_bit(code
, dev
->keybit
) || !!test_bit(code
, dev
->key
) == value
)
82 change_bit(code
, dev
->key
);
84 if (test_bit(EV_REP
, dev
->evbit
) && dev
->rep
[REP_PERIOD
] && dev
->rep
[REP_DELAY
] && dev
->timer
.data
&& value
) {
85 dev
->repeat_key
= code
;
86 mod_timer(&dev
->timer
, jiffies
+ msecs_to_jiffies(dev
->rep
[REP_DELAY
]));
93 if (code
> SW_MAX
|| !test_bit(code
, dev
->swbit
) || !!test_bit(code
, dev
->sw
) == value
)
96 change_bit(code
, dev
->sw
);
102 if (code
> ABS_MAX
|| !test_bit(code
, dev
->absbit
))
105 if (dev
->absfuzz
[code
]) {
106 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] >> 1)) &&
107 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] >> 1)))
110 if ((value
> dev
->abs
[code
] - dev
->absfuzz
[code
]) &&
111 (value
< dev
->abs
[code
] + dev
->absfuzz
[code
]))
112 value
= (dev
->abs
[code
] * 3 + value
) >> 2;
114 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] << 1)) &&
115 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] << 1)))
116 value
= (dev
->abs
[code
] + value
) >> 1;
119 if (dev
->abs
[code
] == value
)
122 dev
->abs
[code
] = value
;
127 if (code
> REL_MAX
|| !test_bit(code
, dev
->relbit
) || (value
== 0))
134 if (code
> MSC_MAX
|| !test_bit(code
, dev
->mscbit
))
137 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
143 if (code
> LED_MAX
|| !test_bit(code
, dev
->ledbit
) || !!test_bit(code
, dev
->led
) == value
)
146 change_bit(code
, dev
->led
);
147 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
153 if (code
> SND_MAX
|| !test_bit(code
, dev
->sndbit
))
156 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
162 if (code
> REP_MAX
|| value
< 0 || dev
->rep
[code
] == value
) return;
164 dev
->rep
[code
] = value
;
165 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
170 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
178 dev
->grab
->handler
->event(dev
->grab
, type
, code
, value
);
180 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
182 handle
->handler
->event(handle
, type
, code
, value
);
185 static void input_repeat_key(unsigned long data
)
187 struct input_dev
*dev
= (void *) data
;
189 if (!test_bit(dev
->repeat_key
, dev
->key
))
192 input_event(dev
, EV_KEY
, dev
->repeat_key
, 2);
195 if (dev
->rep
[REP_PERIOD
])
196 mod_timer(&dev
->timer
, jiffies
+ msecs_to_jiffies(dev
->rep
[REP_PERIOD
]));
199 int input_accept_process(struct input_handle
*handle
, struct file
*file
)
201 if (handle
->dev
->accept
)
202 return handle
->dev
->accept(handle
->dev
, file
);
207 int input_grab_device(struct input_handle
*handle
)
209 if (handle
->dev
->grab
)
212 handle
->dev
->grab
= handle
;
216 void input_release_device(struct input_handle
*handle
)
218 if (handle
->dev
->grab
== handle
)
219 handle
->dev
->grab
= NULL
;
222 int input_open_device(struct input_handle
*handle
)
224 struct input_dev
*dev
= handle
->dev
;
227 err
= down_interruptible(&dev
->sem
);
233 if (!dev
->users
++ && dev
->open
)
234 err
= dev
->open(dev
);
244 int input_flush_device(struct input_handle
* handle
, struct file
* file
)
246 if (handle
->dev
->flush
)
247 return handle
->dev
->flush(handle
->dev
, file
);
252 void input_close_device(struct input_handle
*handle
)
254 struct input_dev
*dev
= handle
->dev
;
256 input_release_device(handle
);
260 if (!--dev
->users
&& dev
->close
)
267 static void input_link_handle(struct input_handle
*handle
)
269 list_add_tail(&handle
->d_node
, &handle
->dev
->h_list
);
270 list_add_tail(&handle
->h_node
, &handle
->handler
->h_list
);
273 #define MATCH_BIT(bit, max) \
274 for (i = 0; i < NBITS(max); i++) \
275 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
277 if (i != NBITS(max)) \
280 static struct input_device_id
*input_match_device(struct input_device_id
*id
, struct input_dev
*dev
)
284 for (; id
->flags
|| id
->driver_info
; id
++) {
286 if (id
->flags
& INPUT_DEVICE_ID_MATCH_BUS
)
287 if (id
->id
.bustype
!= dev
->id
.bustype
)
290 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VENDOR
)
291 if (id
->id
.vendor
!= dev
->id
.vendor
)
294 if (id
->flags
& INPUT_DEVICE_ID_MATCH_PRODUCT
)
295 if (id
->id
.product
!= dev
->id
.product
)
298 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VERSION
)
299 if (id
->id
.version
!= dev
->id
.version
)
302 MATCH_BIT(evbit
, EV_MAX
);
303 MATCH_BIT(keybit
, KEY_MAX
);
304 MATCH_BIT(relbit
, REL_MAX
);
305 MATCH_BIT(absbit
, ABS_MAX
);
306 MATCH_BIT(mscbit
, MSC_MAX
);
307 MATCH_BIT(ledbit
, LED_MAX
);
308 MATCH_BIT(sndbit
, SND_MAX
);
309 MATCH_BIT(ffbit
, FF_MAX
);
310 MATCH_BIT(swbit
, SW_MAX
);
318 static int input_print_bitmap(char *buf
, int buf_size
, unsigned long *bitmap
, int max
)
323 for (i
= NBITS(max
) - 1; i
> 0; i
--)
328 len
+= snprintf(buf
+ len
, max(buf_size
- len
, 0),
329 "%lx%s", bitmap
[i
], i
> 0 ? " " : "");
333 #ifdef CONFIG_PROC_FS
335 static struct proc_dir_entry
*proc_bus_input_dir
;
336 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait
);
337 static int input_devices_state
;
339 static inline void input_wakeup_procfs_readers(void)
341 input_devices_state
++;
342 wake_up(&input_devices_poll_wait
);
345 static unsigned int input_devices_poll(struct file
*file
, poll_table
*wait
)
347 int state
= input_devices_state
;
348 poll_wait(file
, &input_devices_poll_wait
, wait
);
349 if (state
!= input_devices_state
)
350 return POLLIN
| POLLRDNORM
;
354 #define SPRINTF_BIT(ev, bm) \
356 len += sprintf(buf + len, "B: %s=", #ev); \
357 len += input_print_bitmap(buf + len, INT_MAX, \
358 dev->bm##bit, ev##_MAX); \
359 len += sprintf(buf + len, "\n"); \
362 #define TEST_AND_SPRINTF_BIT(ev, bm) \
364 if (test_bit(EV_##ev, dev->evbit)) \
365 SPRINTF_BIT(ev, bm); \
368 static int input_devices_read(char *buf
, char **start
, off_t pos
, int count
, int *eof
, void *data
)
370 struct input_dev
*dev
;
371 struct input_handle
*handle
;
377 list_for_each_entry(dev
, &input_dev_list
, node
) {
379 path
= kobject_get_path(&dev
->cdev
.kobj
, GFP_KERNEL
);
381 len
= sprintf(buf
, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
382 dev
->id
.bustype
, dev
->id
.vendor
, dev
->id
.product
, dev
->id
.version
);
384 len
+= sprintf(buf
+ len
, "N: Name=\"%s\"\n", dev
->name
? dev
->name
: "");
385 len
+= sprintf(buf
+ len
, "P: Phys=%s\n", dev
->phys
? dev
->phys
: "");
386 len
+= sprintf(buf
+ len
, "S: Sysfs=%s\n", path
? path
: "");
387 len
+= sprintf(buf
+ len
, "H: Handlers=");
389 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
390 len
+= sprintf(buf
+ len
, "%s ", handle
->name
);
392 len
+= sprintf(buf
+ len
, "\n");
395 TEST_AND_SPRINTF_BIT(KEY
, key
);
396 TEST_AND_SPRINTF_BIT(REL
, rel
);
397 TEST_AND_SPRINTF_BIT(ABS
, abs
);
398 TEST_AND_SPRINTF_BIT(MSC
, msc
);
399 TEST_AND_SPRINTF_BIT(LED
, led
);
400 TEST_AND_SPRINTF_BIT(SND
, snd
);
401 TEST_AND_SPRINTF_BIT(FF
, ff
);
402 TEST_AND_SPRINTF_BIT(SW
, sw
);
404 len
+= sprintf(buf
+ len
, "\n");
410 *start
= buf
+ (pos
- (at
- len
));
421 if (&dev
->node
== &input_dev_list
)
424 return (count
> cnt
) ? cnt
: count
;
427 static int input_handlers_read(char *buf
, char **start
, off_t pos
, int count
, int *eof
, void *data
)
429 struct input_handler
*handler
;
432 int len
= 0, cnt
= 0;
435 list_for_each_entry(handler
, &input_handler_list
, node
) {
438 len
= sprintf(buf
, "N: Number=%d Name=%s Minor=%d\n",
439 i
++, handler
->name
, handler
->minor
);
441 len
= sprintf(buf
, "N: Number=%d Name=%s\n",
448 *start
= buf
+ (pos
- (at
- len
));
456 if (&handler
->node
== &input_handler_list
)
459 return (count
> cnt
) ? cnt
: count
;
462 static struct file_operations input_fileops
;
464 static int __init
input_proc_init(void)
466 struct proc_dir_entry
*entry
;
468 proc_bus_input_dir
= proc_mkdir("input", proc_bus
);
469 if (!proc_bus_input_dir
)
472 proc_bus_input_dir
->owner
= THIS_MODULE
;
474 entry
= create_proc_read_entry("devices", 0, proc_bus_input_dir
, input_devices_read
, NULL
);
478 entry
->owner
= THIS_MODULE
;
479 input_fileops
= *entry
->proc_fops
;
480 input_fileops
.poll
= input_devices_poll
;
481 entry
->proc_fops
= &input_fileops
;
483 entry
= create_proc_read_entry("handlers", 0, proc_bus_input_dir
, input_handlers_read
, NULL
);
487 entry
->owner
= THIS_MODULE
;
491 fail2
: remove_proc_entry("devices", proc_bus_input_dir
);
492 fail1
: remove_proc_entry("input", proc_bus
);
496 static void input_proc_exit(void)
498 remove_proc_entry("devices", proc_bus_input_dir
);
499 remove_proc_entry("handlers", proc_bus_input_dir
);
500 remove_proc_entry("input", proc_bus
);
503 #else /* !CONFIG_PROC_FS */
504 static inline void input_wakeup_procfs_readers(void) { }
505 static inline int input_proc_init(void) { return 0; }
506 static inline void input_proc_exit(void) { }
509 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
510 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
512 struct input_dev *input_dev = to_input_dev(dev); \
515 retval = down_interruptible(&input_dev->sem); \
519 retval = sprintf(buf, "%s\n", input_dev->name ? input_dev->name : ""); \
521 up(&input_dev->sem); \
525 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
527 INPUT_DEV_STRING_ATTR_SHOW(name
);
528 INPUT_DEV_STRING_ATTR_SHOW(phys
);
529 INPUT_DEV_STRING_ATTR_SHOW(uniq
);
531 static int print_modalias_bits(char *buf
, char prefix
, unsigned long *arr
,
532 unsigned int min
, unsigned int max
)
536 len
= sprintf(buf
, "%c", prefix
);
537 for (i
= min
; i
< max
; i
++)
538 if (arr
[LONG(i
)] & BIT(i
))
539 len
+= sprintf(buf
+len
, "%X,", i
);
543 static ssize_t
input_dev_show_modalias(struct class_device
*dev
, char *buf
)
545 struct input_dev
*id
= to_input_dev(dev
);
548 len
+= sprintf(buf
+len
, "input:b%04Xv%04Xp%04Xe%04X-",
554 len
+= print_modalias_bits(buf
+len
, 'e', id
->evbit
, 0, EV_MAX
);
555 len
+= print_modalias_bits(buf
+len
, 'k', id
->keybit
,
556 KEY_MIN_INTERESTING
, KEY_MAX
);
557 len
+= print_modalias_bits(buf
+len
, 'r', id
->relbit
, 0, REL_MAX
);
558 len
+= print_modalias_bits(buf
+len
, 'a', id
->absbit
, 0, ABS_MAX
);
559 len
+= print_modalias_bits(buf
+len
, 'm', id
->mscbit
, 0, MSC_MAX
);
560 len
+= print_modalias_bits(buf
+len
, 'l', id
->ledbit
, 0, LED_MAX
);
561 len
+= print_modalias_bits(buf
+len
, 's', id
->sndbit
, 0, SND_MAX
);
562 len
+= print_modalias_bits(buf
+len
, 'f', id
->ffbit
, 0, FF_MAX
);
563 len
+= print_modalias_bits(buf
+len
, 'w', id
->swbit
, 0, SW_MAX
);
564 len
+= sprintf(buf
+len
, "\n");
567 static CLASS_DEVICE_ATTR(modalias
, S_IRUGO
, input_dev_show_modalias
, NULL
);
569 static struct attribute
*input_dev_attrs
[] = {
570 &class_device_attr_name
.attr
,
571 &class_device_attr_phys
.attr
,
572 &class_device_attr_uniq
.attr
,
573 &class_device_attr_modalias
.attr
,
577 static struct attribute_group input_dev_attr_group
= {
578 .attrs
= input_dev_attrs
,
581 #define INPUT_DEV_ID_ATTR(name) \
582 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
584 struct input_dev *input_dev = to_input_dev(dev); \
585 return sprintf(buf, "%04x\n", input_dev->id.name); \
587 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
589 INPUT_DEV_ID_ATTR(bustype
);
590 INPUT_DEV_ID_ATTR(vendor
);
591 INPUT_DEV_ID_ATTR(product
);
592 INPUT_DEV_ID_ATTR(version
);
594 static struct attribute
*input_dev_id_attrs
[] = {
595 &class_device_attr_bustype
.attr
,
596 &class_device_attr_vendor
.attr
,
597 &class_device_attr_product
.attr
,
598 &class_device_attr_version
.attr
,
602 static struct attribute_group input_dev_id_attr_group
= {
604 .attrs
= input_dev_id_attrs
,
607 #define INPUT_DEV_CAP_ATTR(ev, bm) \
608 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
610 struct input_dev *input_dev = to_input_dev(dev); \
611 return input_print_bitmap(buf, PAGE_SIZE, input_dev->bm##bit, ev##_MAX);\
613 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
615 INPUT_DEV_CAP_ATTR(EV
, ev
);
616 INPUT_DEV_CAP_ATTR(KEY
, key
);
617 INPUT_DEV_CAP_ATTR(REL
, rel
);
618 INPUT_DEV_CAP_ATTR(ABS
, abs
);
619 INPUT_DEV_CAP_ATTR(MSC
, msc
);
620 INPUT_DEV_CAP_ATTR(LED
, led
);
621 INPUT_DEV_CAP_ATTR(SND
, snd
);
622 INPUT_DEV_CAP_ATTR(FF
, ff
);
623 INPUT_DEV_CAP_ATTR(SW
, sw
);
625 static struct attribute
*input_dev_caps_attrs
[] = {
626 &class_device_attr_ev
.attr
,
627 &class_device_attr_key
.attr
,
628 &class_device_attr_rel
.attr
,
629 &class_device_attr_abs
.attr
,
630 &class_device_attr_msc
.attr
,
631 &class_device_attr_led
.attr
,
632 &class_device_attr_snd
.attr
,
633 &class_device_attr_ff
.attr
,
634 &class_device_attr_sw
.attr
,
638 static struct attribute_group input_dev_caps_attr_group
= {
639 .name
= "capabilities",
640 .attrs
= input_dev_caps_attrs
,
643 static void input_dev_release(struct class_device
*class_dev
)
645 struct input_dev
*dev
= to_input_dev(class_dev
);
648 module_put(THIS_MODULE
);
652 * Input uevent interface - loading event handlers based on
655 static int input_add_uevent_bm_var(char **envp
, int num_envp
, int *cur_index
,
656 char *buffer
, int buffer_size
, int *cur_len
,
657 const char *name
, unsigned long *bitmap
, int max
)
659 if (*cur_index
>= num_envp
- 1)
662 envp
[*cur_index
] = buffer
+ *cur_len
;
664 *cur_len
+= snprintf(buffer
+ *cur_len
, max(buffer_size
- *cur_len
, 0), name
);
665 if (*cur_len
> buffer_size
)
668 *cur_len
+= input_print_bitmap(buffer
+ *cur_len
,
669 max(buffer_size
- *cur_len
, 0),
671 if (*cur_len
> buffer_size
)
678 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
680 int err = add_uevent_var(envp, num_envp, &i, \
681 buffer, buffer_size, &len, \
687 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
689 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
690 buffer, buffer_size, &len, \
696 static int input_dev_uevent(struct class_device
*cdev
, char **envp
,
697 int num_envp
, char *buffer
, int buffer_size
)
699 struct input_dev
*dev
= to_input_dev(cdev
);
703 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
704 dev
->id
.bustype
, dev
->id
.vendor
,
705 dev
->id
.product
, dev
->id
.version
);
707 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev
->name
);
709 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev
->phys
);
711 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev
->uniq
);
713 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev
->evbit
, EV_MAX
);
714 if (test_bit(EV_KEY
, dev
->evbit
))
715 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev
->keybit
, KEY_MAX
);
716 if (test_bit(EV_REL
, dev
->evbit
))
717 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev
->relbit
, REL_MAX
);
718 if (test_bit(EV_ABS
, dev
->evbit
))
719 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev
->absbit
, ABS_MAX
);
720 if (test_bit(EV_MSC
, dev
->evbit
))
721 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev
->mscbit
, MSC_MAX
);
722 if (test_bit(EV_LED
, dev
->evbit
))
723 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev
->ledbit
, LED_MAX
);
724 if (test_bit(EV_SND
, dev
->evbit
))
725 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev
->sndbit
, SND_MAX
);
726 if (test_bit(EV_FF
, dev
->evbit
))
727 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev
->ffbit
, FF_MAX
);
728 if (test_bit(EV_SW
, dev
->evbit
))
729 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev
->swbit
, SW_MAX
);
736 struct class input_class
= {
738 .release
= input_dev_release
,
739 .uevent
= input_dev_uevent
,
742 struct input_dev
*input_allocate_device(void)
744 struct input_dev
*dev
;
746 dev
= kzalloc(sizeof(struct input_dev
), GFP_KERNEL
);
749 dev
->cdev
.class = &input_class
;
750 class_device_initialize(&dev
->cdev
);
751 INIT_LIST_HEAD(&dev
->h_list
);
752 INIT_LIST_HEAD(&dev
->node
);
758 int input_register_device(struct input_dev
*dev
)
760 static atomic_t input_no
= ATOMIC_INIT(0);
761 struct input_handle
*handle
;
762 struct input_handler
*handler
;
763 struct input_device_id
*id
;
767 if (!dev
->dynalloc
) {
768 printk(KERN_WARNING
"input: device %s is statically allocated, will not register\n"
769 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
770 dev
->name
? dev
->name
: "<Unknown>");
774 init_MUTEX(&dev
->sem
);
775 set_bit(EV_SYN
, dev
->evbit
);
778 * If delay and period are pre-set by the driver, then autorepeating
779 * is handled by the driver itself and we don't do it in input.c.
782 init_timer(&dev
->timer
);
783 if (!dev
->rep
[REP_DELAY
] && !dev
->rep
[REP_PERIOD
]) {
784 dev
->timer
.data
= (long) dev
;
785 dev
->timer
.function
= input_repeat_key
;
786 dev
->rep
[REP_DELAY
] = 250;
787 dev
->rep
[REP_PERIOD
] = 33;
790 INIT_LIST_HEAD(&dev
->h_list
);
791 list_add_tail(&dev
->node
, &input_dev_list
);
793 dev
->cdev
.class = &input_class
;
794 snprintf(dev
->cdev
.class_id
, sizeof(dev
->cdev
.class_id
),
795 "input%ld", (unsigned long) atomic_inc_return(&input_no
) - 1);
797 error
= class_device_add(&dev
->cdev
);
801 error
= sysfs_create_group(&dev
->cdev
.kobj
, &input_dev_attr_group
);
805 error
= sysfs_create_group(&dev
->cdev
.kobj
, &input_dev_id_attr_group
);
809 error
= sysfs_create_group(&dev
->cdev
.kobj
, &input_dev_caps_attr_group
);
813 __module_get(THIS_MODULE
);
815 path
= kobject_get_path(&dev
->cdev
.kobj
, GFP_KERNEL
);
816 printk(KERN_INFO
"input: %s as %s\n",
817 dev
->name
? dev
->name
: "Unspecified device", path
? path
: "N/A");
820 list_for_each_entry(handler
, &input_handler_list
, node
)
821 if (!handler
->blacklist
|| !input_match_device(handler
->blacklist
, dev
))
822 if ((id
= input_match_device(handler
->id_table
, dev
)))
823 if ((handle
= handler
->connect(handler
, dev
, id
)))
824 input_link_handle(handle
);
826 input_wakeup_procfs_readers();
830 fail3
: sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_id_attr_group
);
831 fail2
: sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_attr_group
);
832 fail1
: class_device_del(&dev
->cdev
);
836 void input_unregister_device(struct input_dev
*dev
)
838 struct list_head
* node
, * next
;
842 del_timer_sync(&dev
->timer
);
844 list_for_each_safe(node
, next
, &dev
->h_list
) {
845 struct input_handle
* handle
= to_handle(node
);
846 list_del_init(&handle
->d_node
);
847 list_del_init(&handle
->h_node
);
848 handle
->handler
->disconnect(handle
);
851 list_del_init(&dev
->node
);
853 sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_caps_attr_group
);
854 sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_id_attr_group
);
855 sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_attr_group
);
856 class_device_unregister(&dev
->cdev
);
858 input_wakeup_procfs_readers();
861 void input_register_handler(struct input_handler
*handler
)
863 struct input_dev
*dev
;
864 struct input_handle
*handle
;
865 struct input_device_id
*id
;
867 if (!handler
) return;
869 INIT_LIST_HEAD(&handler
->h_list
);
871 if (handler
->fops
!= NULL
)
872 input_table
[handler
->minor
>> 5] = handler
;
874 list_add_tail(&handler
->node
, &input_handler_list
);
876 list_for_each_entry(dev
, &input_dev_list
, node
)
877 if (!handler
->blacklist
|| !input_match_device(handler
->blacklist
, dev
))
878 if ((id
= input_match_device(handler
->id_table
, dev
)))
879 if ((handle
= handler
->connect(handler
, dev
, id
)))
880 input_link_handle(handle
);
882 input_wakeup_procfs_readers();
885 void input_unregister_handler(struct input_handler
*handler
)
887 struct list_head
* node
, * next
;
889 list_for_each_safe(node
, next
, &handler
->h_list
) {
890 struct input_handle
* handle
= to_handle_h(node
);
891 list_del_init(&handle
->h_node
);
892 list_del_init(&handle
->d_node
);
893 handler
->disconnect(handle
);
896 list_del_init(&handler
->node
);
898 if (handler
->fops
!= NULL
)
899 input_table
[handler
->minor
>> 5] = NULL
;
901 input_wakeup_procfs_readers();
904 static int input_open_file(struct inode
*inode
, struct file
*file
)
906 struct input_handler
*handler
= input_table
[iminor(inode
) >> 5];
907 struct file_operations
*old_fops
, *new_fops
= NULL
;
910 /* No load-on-demand here? */
911 if (!handler
|| !(new_fops
= fops_get(handler
->fops
)))
915 * That's _really_ odd. Usually NULL ->open means "nothing special",
916 * not "no device". Oh, well...
918 if (!new_fops
->open
) {
922 old_fops
= file
->f_op
;
923 file
->f_op
= new_fops
;
925 err
= new_fops
->open(inode
, file
);
928 fops_put(file
->f_op
);
929 file
->f_op
= fops_get(old_fops
);
935 static struct file_operations input_fops
= {
936 .owner
= THIS_MODULE
,
937 .open
= input_open_file
,
940 static int __init
input_init(void)
944 err
= class_register(&input_class
);
946 printk(KERN_ERR
"input: unable to register input_dev class\n");
950 err
= input_proc_init();
954 err
= register_chrdev(INPUT_MAJOR
, "input", &input_fops
);
956 printk(KERN_ERR
"input: unable to register char major %d", INPUT_MAJOR
);
962 fail2
: input_proc_exit();
963 fail1
: class_unregister(&input_class
);
967 static void __exit
input_exit(void)
970 unregister_chrdev(INPUT_MAJOR
, "input");
971 class_unregister(&input_class
);
974 subsys_initcall(input_init
);
975 module_exit(input_exit
);