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/seq_file.h>
22 #include <linux/interrupt.h>
23 #include <linux/poll.h>
24 #include <linux/device.h>
25 #include <linux/mutex.h>
27 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28 MODULE_DESCRIPTION("Input core");
29 MODULE_LICENSE("GPL");
31 EXPORT_SYMBOL(input_allocate_device
);
32 EXPORT_SYMBOL(input_free_device
);
33 EXPORT_SYMBOL(input_register_device
);
34 EXPORT_SYMBOL(input_unregister_device
);
35 EXPORT_SYMBOL(input_register_handler
);
36 EXPORT_SYMBOL(input_unregister_handler
);
37 EXPORT_SYMBOL(input_grab_device
);
38 EXPORT_SYMBOL(input_release_device
);
39 EXPORT_SYMBOL(input_open_device
);
40 EXPORT_SYMBOL(input_close_device
);
41 EXPORT_SYMBOL(input_accept_process
);
42 EXPORT_SYMBOL(input_flush_device
);
43 EXPORT_SYMBOL(input_event
);
44 EXPORT_SYMBOL_GPL(input_class
);
46 #define INPUT_DEVICES 256
48 static LIST_HEAD(input_dev_list
);
49 static LIST_HEAD(input_handler_list
);
51 static struct input_handler
*input_table
[8];
53 void input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
55 struct input_handle
*handle
;
57 if (type
> EV_MAX
|| !test_bit(type
, dev
->evbit
))
60 add_input_randomness(type
, code
, value
);
68 dev
->event(dev
, type
, code
, value
);
81 if (code
> KEY_MAX
|| !test_bit(code
, dev
->keybit
) || !!test_bit(code
, dev
->key
) == value
)
87 change_bit(code
, dev
->key
);
89 if (test_bit(EV_REP
, dev
->evbit
) && dev
->rep
[REP_PERIOD
] && dev
->rep
[REP_DELAY
] && dev
->timer
.data
&& value
) {
90 dev
->repeat_key
= code
;
91 mod_timer(&dev
->timer
, jiffies
+ msecs_to_jiffies(dev
->rep
[REP_DELAY
]));
98 if (code
> SW_MAX
|| !test_bit(code
, dev
->swbit
) || !!test_bit(code
, dev
->sw
) == value
)
101 change_bit(code
, dev
->sw
);
107 if (code
> ABS_MAX
|| !test_bit(code
, dev
->absbit
))
110 if (dev
->absfuzz
[code
]) {
111 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] >> 1)) &&
112 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] >> 1)))
115 if ((value
> dev
->abs
[code
] - dev
->absfuzz
[code
]) &&
116 (value
< dev
->abs
[code
] + dev
->absfuzz
[code
]))
117 value
= (dev
->abs
[code
] * 3 + value
) >> 2;
119 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] << 1)) &&
120 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] << 1)))
121 value
= (dev
->abs
[code
] + value
) >> 1;
124 if (dev
->abs
[code
] == value
)
127 dev
->abs
[code
] = value
;
132 if (code
> REL_MAX
|| !test_bit(code
, dev
->relbit
) || (value
== 0))
139 if (code
> MSC_MAX
|| !test_bit(code
, dev
->mscbit
))
143 dev
->event(dev
, type
, code
, value
);
149 if (code
> LED_MAX
|| !test_bit(code
, dev
->ledbit
) || !!test_bit(code
, dev
->led
) == value
)
152 change_bit(code
, dev
->led
);
155 dev
->event(dev
, type
, code
, value
);
161 if (code
> SND_MAX
|| !test_bit(code
, dev
->sndbit
))
164 if (!!test_bit(code
, dev
->snd
) != !!value
)
165 change_bit(code
, dev
->snd
);
168 dev
->event(dev
, type
, code
, value
);
174 if (code
> REP_MAX
|| value
< 0 || dev
->rep
[code
] == value
)
177 dev
->rep
[code
] = value
;
179 dev
->event(dev
, type
, code
, value
);
185 dev
->event(dev
, type
, code
, value
);
193 dev
->grab
->handler
->event(dev
->grab
, type
, code
, value
);
195 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
197 handle
->handler
->event(handle
, type
, code
, value
);
200 static void input_repeat_key(unsigned long data
)
202 struct input_dev
*dev
= (void *) data
;
204 if (!test_bit(dev
->repeat_key
, dev
->key
))
207 input_event(dev
, EV_KEY
, dev
->repeat_key
, 2);
210 if (dev
->rep
[REP_PERIOD
])
211 mod_timer(&dev
->timer
, jiffies
+ msecs_to_jiffies(dev
->rep
[REP_PERIOD
]));
214 int input_accept_process(struct input_handle
*handle
, struct file
*file
)
216 if (handle
->dev
->accept
)
217 return handle
->dev
->accept(handle
->dev
, file
);
222 int input_grab_device(struct input_handle
*handle
)
224 if (handle
->dev
->grab
)
227 handle
->dev
->grab
= handle
;
231 void input_release_device(struct input_handle
*handle
)
233 if (handle
->dev
->grab
== handle
)
234 handle
->dev
->grab
= NULL
;
237 int input_open_device(struct input_handle
*handle
)
239 struct input_dev
*dev
= handle
->dev
;
242 err
= mutex_lock_interruptible(&dev
->mutex
);
248 if (!dev
->users
++ && dev
->open
)
249 err
= dev
->open(dev
);
254 mutex_unlock(&dev
->mutex
);
259 int input_flush_device(struct input_handle
* handle
, struct file
* file
)
261 if (handle
->dev
->flush
)
262 return handle
->dev
->flush(handle
->dev
, file
);
267 void input_close_device(struct input_handle
*handle
)
269 struct input_dev
*dev
= handle
->dev
;
271 input_release_device(handle
);
273 mutex_lock(&dev
->mutex
);
275 if (!--dev
->users
&& dev
->close
)
279 mutex_unlock(&dev
->mutex
);
282 static void input_link_handle(struct input_handle
*handle
)
284 list_add_tail(&handle
->d_node
, &handle
->dev
->h_list
);
285 list_add_tail(&handle
->h_node
, &handle
->handler
->h_list
);
288 #define MATCH_BIT(bit, max) \
289 for (i = 0; i < NBITS(max); i++) \
290 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
292 if (i != NBITS(max)) \
295 static struct input_device_id
*input_match_device(struct input_device_id
*id
, struct input_dev
*dev
)
299 for (; id
->flags
|| id
->driver_info
; id
++) {
301 if (id
->flags
& INPUT_DEVICE_ID_MATCH_BUS
)
302 if (id
->bustype
!= dev
->id
.bustype
)
305 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VENDOR
)
306 if (id
->vendor
!= dev
->id
.vendor
)
309 if (id
->flags
& INPUT_DEVICE_ID_MATCH_PRODUCT
)
310 if (id
->product
!= dev
->id
.product
)
313 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VERSION
)
314 if (id
->version
!= dev
->id
.version
)
317 MATCH_BIT(evbit
, EV_MAX
);
318 MATCH_BIT(keybit
, KEY_MAX
);
319 MATCH_BIT(relbit
, REL_MAX
);
320 MATCH_BIT(absbit
, ABS_MAX
);
321 MATCH_BIT(mscbit
, MSC_MAX
);
322 MATCH_BIT(ledbit
, LED_MAX
);
323 MATCH_BIT(sndbit
, SND_MAX
);
324 MATCH_BIT(ffbit
, FF_MAX
);
325 MATCH_BIT(swbit
, SW_MAX
);
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_proc_devices_poll(struct file
*file
, poll_table
*wait
)
347 int state
= input_devices_state
;
349 poll_wait(file
, &input_devices_poll_wait
, wait
);
350 if (state
!= input_devices_state
)
351 return POLLIN
| POLLRDNORM
;
356 static struct list_head
*list_get_nth_element(struct list_head
*list
, loff_t
*pos
)
358 struct list_head
*node
;
361 list_for_each(node
, list
)
368 static struct list_head
*list_get_next_element(struct list_head
*list
, struct list_head
*element
, loff_t
*pos
)
370 if (element
->next
== list
)
374 return element
->next
;
377 static void *input_devices_seq_start(struct seq_file
*seq
, loff_t
*pos
)
379 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
381 return list_get_nth_element(&input_dev_list
, pos
);
384 static void *input_devices_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
386 return list_get_next_element(&input_dev_list
, v
, pos
);
389 static void input_devices_seq_stop(struct seq_file
*seq
, void *v
)
391 /* release lock here */
394 static void input_seq_print_bitmap(struct seq_file
*seq
, const char *name
,
395 unsigned long *bitmap
, int max
)
399 for (i
= NBITS(max
) - 1; i
> 0; i
--)
403 seq_printf(seq
, "B: %s=", name
);
405 seq_printf(seq
, "%lx%s", bitmap
[i
], i
> 0 ? " " : "");
409 static int input_devices_seq_show(struct seq_file
*seq
, void *v
)
411 struct input_dev
*dev
= container_of(v
, struct input_dev
, node
);
412 const char *path
= kobject_get_path(&dev
->cdev
.kobj
, GFP_KERNEL
);
413 struct input_handle
*handle
;
415 seq_printf(seq
, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
416 dev
->id
.bustype
, dev
->id
.vendor
, dev
->id
.product
, dev
->id
.version
);
418 seq_printf(seq
, "N: Name=\"%s\"\n", dev
->name
? dev
->name
: "");
419 seq_printf(seq
, "P: Phys=%s\n", dev
->phys
? dev
->phys
: "");
420 seq_printf(seq
, "S: Sysfs=%s\n", path
? path
: "");
421 seq_printf(seq
, "H: Handlers=");
423 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
424 seq_printf(seq
, "%s ", handle
->name
);
427 input_seq_print_bitmap(seq
, "EV", dev
->evbit
, EV_MAX
);
428 if (test_bit(EV_KEY
, dev
->evbit
))
429 input_seq_print_bitmap(seq
, "KEY", dev
->keybit
, KEY_MAX
);
430 if (test_bit(EV_REL
, dev
->evbit
))
431 input_seq_print_bitmap(seq
, "REL", dev
->relbit
, REL_MAX
);
432 if (test_bit(EV_ABS
, dev
->evbit
))
433 input_seq_print_bitmap(seq
, "ABS", dev
->absbit
, ABS_MAX
);
434 if (test_bit(EV_MSC
, dev
->evbit
))
435 input_seq_print_bitmap(seq
, "MSC", dev
->mscbit
, MSC_MAX
);
436 if (test_bit(EV_LED
, dev
->evbit
))
437 input_seq_print_bitmap(seq
, "LED", dev
->ledbit
, LED_MAX
);
438 if (test_bit(EV_SND
, dev
->evbit
))
439 input_seq_print_bitmap(seq
, "SND", dev
->sndbit
, SND_MAX
);
440 if (test_bit(EV_FF
, dev
->evbit
))
441 input_seq_print_bitmap(seq
, "FF", dev
->ffbit
, FF_MAX
);
442 if (test_bit(EV_SW
, dev
->evbit
))
443 input_seq_print_bitmap(seq
, "SW", dev
->swbit
, SW_MAX
);
451 static struct seq_operations input_devices_seq_ops
= {
452 .start
= input_devices_seq_start
,
453 .next
= input_devices_seq_next
,
454 .stop
= input_devices_seq_stop
,
455 .show
= input_devices_seq_show
,
458 static int input_proc_devices_open(struct inode
*inode
, struct file
*file
)
460 return seq_open(file
, &input_devices_seq_ops
);
463 static struct file_operations input_devices_fileops
= {
464 .owner
= THIS_MODULE
,
465 .open
= input_proc_devices_open
,
466 .poll
= input_proc_devices_poll
,
469 .release
= seq_release
,
472 static void *input_handlers_seq_start(struct seq_file
*seq
, loff_t
*pos
)
474 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
475 seq
->private = (void *)(unsigned long)*pos
;
476 return list_get_nth_element(&input_handler_list
, pos
);
479 static void *input_handlers_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
481 seq
->private = (void *)(unsigned long)(*pos
+ 1);
482 return list_get_next_element(&input_handler_list
, v
, pos
);
485 static void input_handlers_seq_stop(struct seq_file
*seq
, void *v
)
487 /* release lock here */
490 static int input_handlers_seq_show(struct seq_file
*seq
, void *v
)
492 struct input_handler
*handler
= container_of(v
, struct input_handler
, node
);
494 seq_printf(seq
, "N: Number=%ld Name=%s",
495 (unsigned long)seq
->private, handler
->name
);
497 seq_printf(seq
, " Minor=%d", handler
->minor
);
502 static struct seq_operations input_handlers_seq_ops
= {
503 .start
= input_handlers_seq_start
,
504 .next
= input_handlers_seq_next
,
505 .stop
= input_handlers_seq_stop
,
506 .show
= input_handlers_seq_show
,
509 static int input_proc_handlers_open(struct inode
*inode
, struct file
*file
)
511 return seq_open(file
, &input_handlers_seq_ops
);
514 static struct file_operations input_handlers_fileops
= {
515 .owner
= THIS_MODULE
,
516 .open
= input_proc_handlers_open
,
519 .release
= seq_release
,
522 static int __init
input_proc_init(void)
524 struct proc_dir_entry
*entry
;
526 proc_bus_input_dir
= proc_mkdir("input", proc_bus
);
527 if (!proc_bus_input_dir
)
530 proc_bus_input_dir
->owner
= THIS_MODULE
;
532 entry
= create_proc_entry("devices", 0, proc_bus_input_dir
);
536 entry
->owner
= THIS_MODULE
;
537 entry
->proc_fops
= &input_devices_fileops
;
539 entry
= create_proc_entry("handlers", 0, proc_bus_input_dir
);
543 entry
->owner
= THIS_MODULE
;
544 entry
->proc_fops
= &input_handlers_fileops
;
548 fail2
: remove_proc_entry("devices", proc_bus_input_dir
);
549 fail1
: remove_proc_entry("input", proc_bus
);
553 static void input_proc_exit(void)
555 remove_proc_entry("devices", proc_bus_input_dir
);
556 remove_proc_entry("handlers", proc_bus_input_dir
);
557 remove_proc_entry("input", proc_bus
);
560 #else /* !CONFIG_PROC_FS */
561 static inline void input_wakeup_procfs_readers(void) { }
562 static inline int input_proc_init(void) { return 0; }
563 static inline void input_proc_exit(void) { }
566 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
567 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
569 struct input_dev *input_dev = to_input_dev(dev); \
572 retval = mutex_lock_interruptible(&input_dev->mutex); \
576 retval = scnprintf(buf, PAGE_SIZE, \
577 "%s\n", input_dev->name ? input_dev->name : ""); \
579 mutex_unlock(&input_dev->mutex); \
583 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
585 INPUT_DEV_STRING_ATTR_SHOW(name
);
586 INPUT_DEV_STRING_ATTR_SHOW(phys
);
587 INPUT_DEV_STRING_ATTR_SHOW(uniq
);
589 static int input_print_modalias_bits(char *buf
, int size
,
590 char name
, unsigned long *bm
,
591 unsigned int min_bit
, unsigned int max_bit
)
595 len
+= snprintf(buf
, max(size
, 0), "%c", name
);
596 for (i
= min_bit
; i
< max_bit
; i
++)
597 if (bm
[LONG(i
)] & BIT(i
))
598 len
+= snprintf(buf
+ len
, max(size
- len
, 0), "%X,", i
);
602 static int input_print_modalias(char *buf
, int size
, struct input_dev
*id
,
607 len
= snprintf(buf
, max(size
, 0),
608 "input:b%04Xv%04Xp%04Xe%04X-",
609 id
->id
.bustype
, id
->id
.vendor
,
610 id
->id
.product
, id
->id
.version
);
612 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
613 'e', id
->evbit
, 0, EV_MAX
);
614 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
615 'k', id
->keybit
, KEY_MIN_INTERESTING
, KEY_MAX
);
616 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
617 'r', id
->relbit
, 0, REL_MAX
);
618 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
619 'a', id
->absbit
, 0, ABS_MAX
);
620 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
621 'm', id
->mscbit
, 0, MSC_MAX
);
622 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
623 'l', id
->ledbit
, 0, LED_MAX
);
624 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
625 's', id
->sndbit
, 0, SND_MAX
);
626 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
627 'f', id
->ffbit
, 0, FF_MAX
);
628 len
+= input_print_modalias_bits(buf
+ len
, size
- len
,
629 'w', id
->swbit
, 0, SW_MAX
);
632 len
+= snprintf(buf
+ len
, max(size
- len
, 0), "\n");
637 static ssize_t
input_dev_show_modalias(struct class_device
*dev
, char *buf
)
639 struct input_dev
*id
= to_input_dev(dev
);
642 len
= input_print_modalias(buf
, PAGE_SIZE
, id
, 1);
644 return min_t(int, len
, PAGE_SIZE
);
646 static CLASS_DEVICE_ATTR(modalias
, S_IRUGO
, input_dev_show_modalias
, NULL
);
648 static struct attribute
*input_dev_attrs
[] = {
649 &class_device_attr_name
.attr
,
650 &class_device_attr_phys
.attr
,
651 &class_device_attr_uniq
.attr
,
652 &class_device_attr_modalias
.attr
,
656 static struct attribute_group input_dev_attr_group
= {
657 .attrs
= input_dev_attrs
,
660 #define INPUT_DEV_ID_ATTR(name) \
661 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
663 struct input_dev *input_dev = to_input_dev(dev); \
664 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
666 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
668 INPUT_DEV_ID_ATTR(bustype
);
669 INPUT_DEV_ID_ATTR(vendor
);
670 INPUT_DEV_ID_ATTR(product
);
671 INPUT_DEV_ID_ATTR(version
);
673 static struct attribute
*input_dev_id_attrs
[] = {
674 &class_device_attr_bustype
.attr
,
675 &class_device_attr_vendor
.attr
,
676 &class_device_attr_product
.attr
,
677 &class_device_attr_version
.attr
,
681 static struct attribute_group input_dev_id_attr_group
= {
683 .attrs
= input_dev_id_attrs
,
686 static int input_print_bitmap(char *buf
, int buf_size
, unsigned long *bitmap
,
692 for (i
= NBITS(max
) - 1; i
> 0; i
--)
697 len
+= snprintf(buf
+ len
, max(buf_size
- len
, 0),
698 "%lx%s", bitmap
[i
], i
> 0 ? " " : "");
701 len
+= snprintf(buf
+ len
, max(buf_size
- len
, 0), "\n");
706 #define INPUT_DEV_CAP_ATTR(ev, bm) \
707 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
709 struct input_dev *input_dev = to_input_dev(dev); \
710 int len = input_print_bitmap(buf, PAGE_SIZE, \
711 input_dev->bm##bit, ev##_MAX, 1); \
712 return min_t(int, len, PAGE_SIZE); \
714 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
716 INPUT_DEV_CAP_ATTR(EV
, ev
);
717 INPUT_DEV_CAP_ATTR(KEY
, key
);
718 INPUT_DEV_CAP_ATTR(REL
, rel
);
719 INPUT_DEV_CAP_ATTR(ABS
, abs
);
720 INPUT_DEV_CAP_ATTR(MSC
, msc
);
721 INPUT_DEV_CAP_ATTR(LED
, led
);
722 INPUT_DEV_CAP_ATTR(SND
, snd
);
723 INPUT_DEV_CAP_ATTR(FF
, ff
);
724 INPUT_DEV_CAP_ATTR(SW
, sw
);
726 static struct attribute
*input_dev_caps_attrs
[] = {
727 &class_device_attr_ev
.attr
,
728 &class_device_attr_key
.attr
,
729 &class_device_attr_rel
.attr
,
730 &class_device_attr_abs
.attr
,
731 &class_device_attr_msc
.attr
,
732 &class_device_attr_led
.attr
,
733 &class_device_attr_snd
.attr
,
734 &class_device_attr_ff
.attr
,
735 &class_device_attr_sw
.attr
,
739 static struct attribute_group input_dev_caps_attr_group
= {
740 .name
= "capabilities",
741 .attrs
= input_dev_caps_attrs
,
744 static void input_dev_release(struct class_device
*class_dev
)
746 struct input_dev
*dev
= to_input_dev(class_dev
);
749 module_put(THIS_MODULE
);
753 * Input uevent interface - loading event handlers based on
756 static int input_add_uevent_bm_var(char **envp
, int num_envp
, int *cur_index
,
757 char *buffer
, int buffer_size
, int *cur_len
,
758 const char *name
, unsigned long *bitmap
, int max
)
760 if (*cur_index
>= num_envp
- 1)
763 envp
[*cur_index
] = buffer
+ *cur_len
;
765 *cur_len
+= snprintf(buffer
+ *cur_len
, max(buffer_size
- *cur_len
, 0), name
);
766 if (*cur_len
>= buffer_size
)
769 *cur_len
+= input_print_bitmap(buffer
+ *cur_len
,
770 max(buffer_size
- *cur_len
, 0),
772 if (*cur_len
> buffer_size
)
779 static int input_add_uevent_modalias_var(char **envp
, int num_envp
, int *cur_index
,
780 char *buffer
, int buffer_size
, int *cur_len
,
781 struct input_dev
*dev
)
783 if (*cur_index
>= num_envp
- 1)
786 envp
[*cur_index
] = buffer
+ *cur_len
;
788 *cur_len
+= snprintf(buffer
+ *cur_len
, max(buffer_size
- *cur_len
, 0),
790 if (*cur_len
>= buffer_size
)
793 *cur_len
+= input_print_modalias(buffer
+ *cur_len
,
794 max(buffer_size
- *cur_len
, 0),
796 if (*cur_len
> buffer_size
)
803 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
805 int err = add_uevent_var(envp, num_envp, &i, \
806 buffer, buffer_size, &len, \
812 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
814 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
815 buffer, buffer_size, &len, \
821 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
823 int err = input_add_uevent_modalias_var(envp, \
825 buffer, buffer_size, &len, \
831 static int input_dev_uevent(struct class_device
*cdev
, char **envp
,
832 int num_envp
, char *buffer
, int buffer_size
)
834 struct input_dev
*dev
= to_input_dev(cdev
);
838 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
839 dev
->id
.bustype
, dev
->id
.vendor
,
840 dev
->id
.product
, dev
->id
.version
);
842 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev
->name
);
844 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev
->phys
);
846 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev
->uniq
);
848 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev
->evbit
, EV_MAX
);
849 if (test_bit(EV_KEY
, dev
->evbit
))
850 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev
->keybit
, KEY_MAX
);
851 if (test_bit(EV_REL
, dev
->evbit
))
852 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev
->relbit
, REL_MAX
);
853 if (test_bit(EV_ABS
, dev
->evbit
))
854 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev
->absbit
, ABS_MAX
);
855 if (test_bit(EV_MSC
, dev
->evbit
))
856 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev
->mscbit
, MSC_MAX
);
857 if (test_bit(EV_LED
, dev
->evbit
))
858 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev
->ledbit
, LED_MAX
);
859 if (test_bit(EV_SND
, dev
->evbit
))
860 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev
->sndbit
, SND_MAX
);
861 if (test_bit(EV_FF
, dev
->evbit
))
862 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev
->ffbit
, FF_MAX
);
863 if (test_bit(EV_SW
, dev
->evbit
))
864 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev
->swbit
, SW_MAX
);
866 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev
);
872 struct class input_class
= {
874 .release
= input_dev_release
,
875 .uevent
= input_dev_uevent
,
878 struct input_dev
*input_allocate_device(void)
880 struct input_dev
*dev
;
882 dev
= kzalloc(sizeof(struct input_dev
), GFP_KERNEL
);
885 dev
->cdev
.class = &input_class
;
886 class_device_initialize(&dev
->cdev
);
887 mutex_init(&dev
->mutex
);
888 INIT_LIST_HEAD(&dev
->h_list
);
889 INIT_LIST_HEAD(&dev
->node
);
895 void input_free_device(struct input_dev
*dev
)
899 mutex_lock(&dev
->mutex
);
900 dev
->name
= dev
->phys
= dev
->uniq
= NULL
;
901 mutex_unlock(&dev
->mutex
);
903 input_put_device(dev
);
907 int input_register_device(struct input_dev
*dev
)
909 static atomic_t input_no
= ATOMIC_INIT(0);
910 struct input_handle
*handle
;
911 struct input_handler
*handler
;
912 struct input_device_id
*id
;
916 if (!dev
->dynalloc
) {
917 printk(KERN_WARNING
"input: device %s is statically allocated, will not register\n"
918 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
919 dev
->name
? dev
->name
: "<Unknown>");
923 set_bit(EV_SYN
, dev
->evbit
);
926 * If delay and period are pre-set by the driver, then autorepeating
927 * is handled by the driver itself and we don't do it in input.c.
930 init_timer(&dev
->timer
);
931 if (!dev
->rep
[REP_DELAY
] && !dev
->rep
[REP_PERIOD
]) {
932 dev
->timer
.data
= (long) dev
;
933 dev
->timer
.function
= input_repeat_key
;
934 dev
->rep
[REP_DELAY
] = 250;
935 dev
->rep
[REP_PERIOD
] = 33;
938 INIT_LIST_HEAD(&dev
->h_list
);
939 list_add_tail(&dev
->node
, &input_dev_list
);
941 dev
->cdev
.class = &input_class
;
942 snprintf(dev
->cdev
.class_id
, sizeof(dev
->cdev
.class_id
),
943 "input%ld", (unsigned long) atomic_inc_return(&input_no
) - 1);
945 error
= class_device_add(&dev
->cdev
);
949 error
= sysfs_create_group(&dev
->cdev
.kobj
, &input_dev_attr_group
);
953 error
= sysfs_create_group(&dev
->cdev
.kobj
, &input_dev_id_attr_group
);
957 error
= sysfs_create_group(&dev
->cdev
.kobj
, &input_dev_caps_attr_group
);
961 __module_get(THIS_MODULE
);
963 path
= kobject_get_path(&dev
->cdev
.kobj
, GFP_KERNEL
);
964 printk(KERN_INFO
"input: %s as %s\n",
965 dev
->name
? dev
->name
: "Unspecified device", path
? path
: "N/A");
968 list_for_each_entry(handler
, &input_handler_list
, node
)
969 if (!handler
->blacklist
|| !input_match_device(handler
->blacklist
, dev
))
970 if ((id
= input_match_device(handler
->id_table
, dev
)))
971 if ((handle
= handler
->connect(handler
, dev
, id
)))
972 input_link_handle(handle
);
974 input_wakeup_procfs_readers();
978 fail3
: sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_id_attr_group
);
979 fail2
: sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_attr_group
);
980 fail1
: class_device_del(&dev
->cdev
);
984 void input_unregister_device(struct input_dev
*dev
)
986 struct list_head
*node
, *next
;
991 del_timer_sync(&dev
->timer
);
993 list_for_each_safe(node
, next
, &dev
->h_list
) {
994 struct input_handle
* handle
= to_handle(node
);
995 list_del_init(&handle
->d_node
);
996 list_del_init(&handle
->h_node
);
997 handle
->handler
->disconnect(handle
);
1000 list_del_init(&dev
->node
);
1002 sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_caps_attr_group
);
1003 sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_id_attr_group
);
1004 sysfs_remove_group(&dev
->cdev
.kobj
, &input_dev_attr_group
);
1005 class_device_unregister(&dev
->cdev
);
1007 mutex_lock(&dev
->mutex
);
1008 dev
->name
= dev
->phys
= dev
->uniq
= NULL
;
1009 mutex_unlock(&dev
->mutex
);
1011 input_wakeup_procfs_readers();
1014 void input_register_handler(struct input_handler
*handler
)
1016 struct input_dev
*dev
;
1017 struct input_handle
*handle
;
1018 struct input_device_id
*id
;
1023 INIT_LIST_HEAD(&handler
->h_list
);
1025 if (handler
->fops
!= NULL
)
1026 input_table
[handler
->minor
>> 5] = handler
;
1028 list_add_tail(&handler
->node
, &input_handler_list
);
1030 list_for_each_entry(dev
, &input_dev_list
, node
)
1031 if (!handler
->blacklist
|| !input_match_device(handler
->blacklist
, dev
))
1032 if ((id
= input_match_device(handler
->id_table
, dev
)))
1033 if ((handle
= handler
->connect(handler
, dev
, id
)))
1034 input_link_handle(handle
);
1036 input_wakeup_procfs_readers();
1039 void input_unregister_handler(struct input_handler
*handler
)
1041 struct list_head
*node
, *next
;
1043 list_for_each_safe(node
, next
, &handler
->h_list
) {
1044 struct input_handle
* handle
= to_handle_h(node
);
1045 list_del_init(&handle
->h_node
);
1046 list_del_init(&handle
->d_node
);
1047 handler
->disconnect(handle
);
1050 list_del_init(&handler
->node
);
1052 if (handler
->fops
!= NULL
)
1053 input_table
[handler
->minor
>> 5] = NULL
;
1055 input_wakeup_procfs_readers();
1058 static int input_open_file(struct inode
*inode
, struct file
*file
)
1060 struct input_handler
*handler
= input_table
[iminor(inode
) >> 5];
1061 const struct file_operations
*old_fops
, *new_fops
= NULL
;
1064 /* No load-on-demand here? */
1065 if (!handler
|| !(new_fops
= fops_get(handler
->fops
)))
1069 * That's _really_ odd. Usually NULL ->open means "nothing special",
1070 * not "no device". Oh, well...
1072 if (!new_fops
->open
) {
1076 old_fops
= file
->f_op
;
1077 file
->f_op
= new_fops
;
1079 err
= new_fops
->open(inode
, file
);
1082 fops_put(file
->f_op
);
1083 file
->f_op
= fops_get(old_fops
);
1089 static struct file_operations input_fops
= {
1090 .owner
= THIS_MODULE
,
1091 .open
= input_open_file
,
1094 static int __init
input_init(void)
1098 err
= class_register(&input_class
);
1100 printk(KERN_ERR
"input: unable to register input_dev class\n");
1104 err
= input_proc_init();
1108 err
= register_chrdev(INPUT_MAJOR
, "input", &input_fops
);
1110 printk(KERN_ERR
"input: unable to register char major %d", INPUT_MAJOR
);
1116 fail2
: input_proc_exit();
1117 fail1
: class_unregister(&input_class
);
1121 static void __exit
input_exit(void)
1124 unregister_chrdev(INPUT_MAJOR
, "input");
1125 class_unregister(&input_class
);
1128 subsys_initcall(input_init
);
1129 module_exit(input_exit
);