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);
67 if (dev->event) dev->event(dev, type, code, value);
71 if (dev->sync) return;
79 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
85 change_bit(code, dev->key);
87 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
88 dev->repeat_key = code;
89 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
96 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
99 change_bit(code, dev->sw);
105 if (code > ABS_MAX || !test_bit(code, dev->absbit))
108 if (dev->absfuzz[code]) {
109 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
110 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
113 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
114 (value < dev->abs[code] + dev->absfuzz[code]))
115 value = (dev->abs[code] * 3 + value) >> 2;
117 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
118 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
119 value = (dev->abs[code] + value) >> 1;
122 if (dev->abs[code] == value)
125 dev->abs[code] = value;
130 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
137 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
140 if (dev->event) dev->event(dev, type, code, value);
146 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
149 change_bit(code, dev->led);
150 if (dev->event) dev->event(dev, type, code, value);
156 if (code > SND_MAX || !test_bit(code, dev->sndbit))
159 if (!!test_bit(code, dev->snd) != !!value)
160 change_bit(code, dev->snd);
162 if (dev->event) dev->event(dev, type, code, value);
168 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
170 dev->rep[code] = value;
171 if (dev->event) dev->event(dev, type, code, value);
176 if (dev->event) dev->event(dev, type, code, value);
184 dev->grab->handler->event(dev->grab, type, code, value);
186 list_for_each_entry(handle, &dev->h_list, d_node)
188 handle->handler->event(handle, type, code, value);
191 static void input_repeat_key(unsigned long data)
193 struct input_dev *dev = (void *) data;
195 if (!test_bit(dev->repeat_key, dev->key))
198 input_event(dev, EV_KEY, dev->repeat_key, 2);
201 if (dev->rep[REP_PERIOD])
202 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
205 int input_accept_process(struct input_handle *handle, struct file *file)
207 if (handle->dev->accept)
208 return handle->dev->accept(handle->dev, file);
213 int input_grab_device(struct input_handle *handle)
215 if (handle->dev->grab)
218 handle->dev->grab = handle;
222 void input_release_device(struct input_handle *handle)
224 if (handle->dev->grab == handle)
225 handle->dev->grab = NULL;
228 int input_open_device(struct input_handle *handle)
230 struct input_dev *dev = handle->dev;
233 err = mutex_lock_interruptible(&dev->mutex);
239 if (!dev->users++ && dev->open)
240 err = dev->open(dev);
245 mutex_unlock(&dev->mutex);
250 int input_flush_device(struct input_handle* handle, struct file* file)
252 if (handle->dev->flush)
253 return handle->dev->flush(handle->dev, file);
258 void input_close_device(struct input_handle *handle)
260 struct input_dev *dev = handle->dev;
262 input_release_device(handle);
264 mutex_lock(&dev->mutex);
266 if (!--dev->users && dev->close)
270 mutex_unlock(&dev->mutex);
273 static void input_link_handle(struct input_handle *handle)
275 list_add_tail(&handle->d_node, &handle->dev->h_list);
276 list_add_tail(&handle->h_node, &handle->handler->h_list);
279 #define MATCH_BIT(bit, max) \
280 for (i = 0; i < NBITS(max); i++) \
281 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
283 if (i != NBITS(max)) \
286 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
290 for (; id->flags || id->driver_info; id++) {
292 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
293 if (id->bustype != dev->id.bustype)
296 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
297 if (id->vendor != dev->id.vendor)
300 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
301 if (id->product != dev->id.product)
304 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
305 if (id->version != dev->id.version)
308 MATCH_BIT(evbit, EV_MAX);
309 MATCH_BIT(keybit, KEY_MAX);
310 MATCH_BIT(relbit, REL_MAX);
311 MATCH_BIT(absbit, ABS_MAX);
312 MATCH_BIT(mscbit, MSC_MAX);
313 MATCH_BIT(ledbit, LED_MAX);
314 MATCH_BIT(sndbit, SND_MAX);
315 MATCH_BIT(ffbit, FF_MAX);
316 MATCH_BIT(swbit, SW_MAX);
324 #ifdef CONFIG_PROC_FS
326 static struct proc_dir_entry *proc_bus_input_dir;
327 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
328 static int input_devices_state;
330 static inline void input_wakeup_procfs_readers(void)
332 input_devices_state++;
333 wake_up(&input_devices_poll_wait);
336 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
338 int state = input_devices_state;
339 poll_wait(file, &input_devices_poll_wait, wait);
340 if (state != input_devices_state)
341 return POLLIN | POLLRDNORM;
345 static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
347 struct list_head *node;
350 list_for_each(node, list)
357 static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
359 if (element->next == list)
363 return element->next;
366 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
368 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
370 return list_get_nth_element(&input_dev_list, pos);
373 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
375 return list_get_next_element(&input_dev_list, v, pos);
378 static void input_devices_seq_stop(struct seq_file *seq, void *v)
380 /* release lock here */
383 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
384 unsigned long *bitmap, int max)
388 for (i = NBITS(max) - 1; i > 0; i--)
392 seq_printf(seq, "B: %s=", name);
394 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
398 static int input_devices_seq_show(struct seq_file *seq, void *v)
400 struct input_dev *dev = container_of(v, struct input_dev, node);
401 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
402 struct input_handle *handle;
404 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
405 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
407 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
408 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
409 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
410 seq_printf(seq, "H: Handlers=");
412 list_for_each_entry(handle, &dev->h_list, d_node)
413 seq_printf(seq, "%s ", handle->name);
416 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
417 if (test_bit(EV_KEY, dev->evbit))
418 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
419 if (test_bit(EV_REL, dev->evbit))
420 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
421 if (test_bit(EV_ABS, dev->evbit))
422 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
423 if (test_bit(EV_MSC, dev->evbit))
424 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
425 if (test_bit(EV_LED, dev->evbit))
426 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
427 if (test_bit(EV_SND, dev->evbit))
428 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
429 if (test_bit(EV_FF, dev->evbit))
430 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
431 if (test_bit(EV_SW, dev->evbit))
432 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
440 static struct seq_operations input_devices_seq_ops = {
441 .start = input_devices_seq_start,
442 .next = input_devices_seq_next,
443 .stop = input_devices_seq_stop,
444 .show = input_devices_seq_show,
447 static int input_proc_devices_open(struct inode *inode, struct file *file)
449 return seq_open(file, &input_devices_seq_ops);
452 static struct file_operations input_devices_fileops = {
453 .owner = THIS_MODULE,
454 .open = input_proc_devices_open,
455 .poll = input_proc_devices_poll,
458 .release = seq_release,
461 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
463 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
464 seq->private = (void *)(unsigned long)*pos;
465 return list_get_nth_element(&input_handler_list, pos);
468 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
470 seq->private = (void *)(unsigned long)(*pos + 1);
471 return list_get_next_element(&input_handler_list, v, pos);
474 static void input_handlers_seq_stop(struct seq_file *seq, void *v)
476 /* release lock here */
479 static int input_handlers_seq_show(struct seq_file *seq, void *v)
481 struct input_handler *handler = container_of(v, struct input_handler, node);
483 seq_printf(seq, "N: Number=%ld Name=%s",
484 (unsigned long)seq->private, handler->name);
486 seq_printf(seq, " Minor=%d", handler->minor);
491 static struct seq_operations input_handlers_seq_ops = {
492 .start = input_handlers_seq_start,
493 .next = input_handlers_seq_next,
494 .stop = input_handlers_seq_stop,
495 .show = input_handlers_seq_show,
498 static int input_proc_handlers_open(struct inode *inode, struct file *file)
500 return seq_open(file, &input_handlers_seq_ops);
503 static struct file_operations input_handlers_fileops = {
504 .owner = THIS_MODULE,
505 .open = input_proc_handlers_open,
508 .release = seq_release,
511 static int __init input_proc_init(void)
513 struct proc_dir_entry *entry;
515 proc_bus_input_dir = proc_mkdir("input", proc_bus);
516 if (!proc_bus_input_dir)
519 proc_bus_input_dir->owner = THIS_MODULE;
521 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
525 entry->owner = THIS_MODULE;
526 entry->proc_fops = &input_devices_fileops;
528 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
532 entry->owner = THIS_MODULE;
533 entry->proc_fops = &input_handlers_fileops;
537 fail2: remove_proc_entry("devices", proc_bus_input_dir);
538 fail1: remove_proc_entry("input", proc_bus);
542 static void input_proc_exit(void)
544 remove_proc_entry("devices", proc_bus_input_dir);
545 remove_proc_entry("handlers", proc_bus_input_dir);
546 remove_proc_entry("input", proc_bus);
549 #else /* !CONFIG_PROC_FS */
550 static inline void input_wakeup_procfs_readers(void) { }
551 static inline int input_proc_init(void) { return 0; }
552 static inline void input_proc_exit(void) { }
555 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
556 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
558 struct input_dev *input_dev = to_input_dev(dev); \
561 retval = mutex_lock_interruptible(&input_dev->mutex); \
565 retval = scnprintf(buf, PAGE_SIZE, \
566 "%s\n", input_dev->name ? input_dev->name : ""); \
568 mutex_unlock(&input_dev->mutex); \
572 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
574 INPUT_DEV_STRING_ATTR_SHOW(name);
575 INPUT_DEV_STRING_ATTR_SHOW(phys);
576 INPUT_DEV_STRING_ATTR_SHOW(uniq);
578 static int input_print_modalias_bits(char *buf, int size,
579 char name, unsigned long *bm,
580 unsigned int min_bit, unsigned int max_bit)
584 len += snprintf(buf, max(size, 0), "%c", name);
585 for (i = min_bit; i < max_bit; i++)
586 if (bm[LONG(i)] & BIT(i))
587 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
591 static int input_print_modalias(char *buf, int size, struct input_dev *id,
596 len = snprintf(buf, max(size, 0),
597 "input:b%04Xv%04Xp%04Xe%04X-",
598 id->id.bustype, id->id.vendor,
599 id->id.product, id->id.version);
601 len += input_print_modalias_bits(buf + len, size - len,
602 'e', id->evbit, 0, EV_MAX);
603 len += input_print_modalias_bits(buf + len, size - len,
604 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
605 len += input_print_modalias_bits(buf + len, size - len,
606 'r', id->relbit, 0, REL_MAX);
607 len += input_print_modalias_bits(buf + len, size - len,
608 'a', id->absbit, 0, ABS_MAX);
609 len += input_print_modalias_bits(buf + len, size - len,
610 'm', id->mscbit, 0, MSC_MAX);
611 len += input_print_modalias_bits(buf + len, size - len,
612 'l', id->ledbit, 0, LED_MAX);
613 len += input_print_modalias_bits(buf + len, size - len,
614 's', id->sndbit, 0, SND_MAX);
615 len += input_print_modalias_bits(buf + len, size - len,
616 'f', id->ffbit, 0, FF_MAX);
617 len += input_print_modalias_bits(buf + len, size - len,
618 'w', id->swbit, 0, SW_MAX);
621 len += snprintf(buf + len, max(size - len, 0), "\n");
626 static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
628 struct input_dev *id = to_input_dev(dev);
631 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
633 return min_t(int, len, PAGE_SIZE);
635 static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
637 static struct attribute *input_dev_attrs[] = {
638 &class_device_attr_name.attr,
639 &class_device_attr_phys.attr,
640 &class_device_attr_uniq.attr,
641 &class_device_attr_modalias.attr,
645 static struct attribute_group input_dev_attr_group = {
646 .attrs = input_dev_attrs,
649 #define INPUT_DEV_ID_ATTR(name) \
650 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
652 struct input_dev *input_dev = to_input_dev(dev); \
653 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
655 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
657 INPUT_DEV_ID_ATTR(bustype);
658 INPUT_DEV_ID_ATTR(vendor);
659 INPUT_DEV_ID_ATTR(product);
660 INPUT_DEV_ID_ATTR(version);
662 static struct attribute *input_dev_id_attrs[] = {
663 &class_device_attr_bustype.attr,
664 &class_device_attr_vendor.attr,
665 &class_device_attr_product.attr,
666 &class_device_attr_version.attr,
670 static struct attribute_group input_dev_id_attr_group = {
672 .attrs = input_dev_id_attrs,
675 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
681 for (i = NBITS(max) - 1; i > 0; i--)
686 len += snprintf(buf + len, max(buf_size - len, 0),
687 "%lx%s", bitmap[i], i > 0 ? " " : "");
690 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
695 #define INPUT_DEV_CAP_ATTR(ev, bm) \
696 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
698 struct input_dev *input_dev = to_input_dev(dev); \
699 int len = input_print_bitmap(buf, PAGE_SIZE, \
700 input_dev->bm##bit, ev##_MAX, 1); \
701 return min_t(int, len, PAGE_SIZE); \
703 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
705 INPUT_DEV_CAP_ATTR(EV, ev);
706 INPUT_DEV_CAP_ATTR(KEY, key);
707 INPUT_DEV_CAP_ATTR(REL, rel);
708 INPUT_DEV_CAP_ATTR(ABS, abs);
709 INPUT_DEV_CAP_ATTR(MSC, msc);
710 INPUT_DEV_CAP_ATTR(LED, led);
711 INPUT_DEV_CAP_ATTR(SND, snd);
712 INPUT_DEV_CAP_ATTR(FF, ff);
713 INPUT_DEV_CAP_ATTR(SW, sw);
715 static struct attribute *input_dev_caps_attrs[] = {
716 &class_device_attr_ev.attr,
717 &class_device_attr_key.attr,
718 &class_device_attr_rel.attr,
719 &class_device_attr_abs.attr,
720 &class_device_attr_msc.attr,
721 &class_device_attr_led.attr,
722 &class_device_attr_snd.attr,
723 &class_device_attr_ff.attr,
724 &class_device_attr_sw.attr,
728 static struct attribute_group input_dev_caps_attr_group = {
729 .name = "capabilities",
730 .attrs = input_dev_caps_attrs,
733 static void input_dev_release(struct class_device *class_dev)
735 struct input_dev *dev = to_input_dev(class_dev);
738 module_put(THIS_MODULE);
742 * Input uevent interface - loading event handlers based on
745 static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
746 char *buffer, int buffer_size, int *cur_len,
747 const char *name, unsigned long *bitmap, int max)
749 if (*cur_index >= num_envp - 1)
752 envp[*cur_index] = buffer + *cur_len;
754 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
755 if (*cur_len >= buffer_size)
758 *cur_len += input_print_bitmap(buffer + *cur_len,
759 max(buffer_size - *cur_len, 0),
761 if (*cur_len > buffer_size)
768 static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
769 char *buffer, int buffer_size, int *cur_len,
770 struct input_dev *dev)
772 if (*cur_index >= num_envp - 1)
775 envp[*cur_index] = buffer + *cur_len;
777 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
779 if (*cur_len >= buffer_size)
782 *cur_len += input_print_modalias(buffer + *cur_len,
783 max(buffer_size - *cur_len, 0),
785 if (*cur_len > buffer_size)
792 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
794 int err = add_uevent_var(envp, num_envp, &i, \
795 buffer, buffer_size, &len, \
801 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
803 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
804 buffer, buffer_size, &len, \
810 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
812 int err = input_add_uevent_modalias_var(envp, \
814 buffer, buffer_size, &len, \
820 static int input_dev_uevent(struct class_device *cdev, char **envp,
821 int num_envp, char *buffer, int buffer_size)
823 struct input_dev *dev = to_input_dev(cdev);
827 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
828 dev->id.bustype, dev->id.vendor,
829 dev->id.product, dev->id.version);
831 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
833 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
835 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
837 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
838 if (test_bit(EV_KEY, dev->evbit))
839 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
840 if (test_bit(EV_REL, dev->evbit))
841 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
842 if (test_bit(EV_ABS, dev->evbit))
843 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
844 if (test_bit(EV_MSC, dev->evbit))
845 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
846 if (test_bit(EV_LED, dev->evbit))
847 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
848 if (test_bit(EV_SND, dev->evbit))
849 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
850 if (test_bit(EV_FF, dev->evbit))
851 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
852 if (test_bit(EV_SW, dev->evbit))
853 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
855 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
861 struct class input_class = {
863 .release = input_dev_release,
864 .uevent = input_dev_uevent,
867 struct input_dev *input_allocate_device(void)
869 struct input_dev *dev;
871 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
874 dev->cdev.class = &input_class;
875 class_device_initialize(&dev->cdev);
876 mutex_init(&dev->mutex);
877 INIT_LIST_HEAD(&dev->h_list);
878 INIT_LIST_HEAD(&dev->node);
884 void input_free_device(struct input_dev *dev)
888 mutex_lock(&dev->mutex);
889 dev->name = dev->phys = dev->uniq = NULL;
890 mutex_unlock(&dev->mutex);
892 input_put_device(dev);
896 int input_register_device(struct input_dev *dev)
898 static atomic_t input_no = ATOMIC_INIT(0);
899 struct input_handle *handle;
900 struct input_handler *handler;
901 struct input_device_id *id;
905 if (!dev->dynalloc) {
906 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
907 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
908 dev->name ? dev->name : "<Unknown>");
912 set_bit(EV_SYN, dev->evbit);
915 * If delay and period are pre-set by the driver, then autorepeating
916 * is handled by the driver itself and we don't do it in input.c.
919 init_timer(&dev->timer);
920 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
921 dev->timer.data = (long) dev;
922 dev->timer.function = input_repeat_key;
923 dev->rep[REP_DELAY] = 250;
924 dev->rep[REP_PERIOD] = 33;
927 INIT_LIST_HEAD(&dev->h_list);
928 list_add_tail(&dev->node, &input_dev_list);
930 dev->cdev.class = &input_class;
931 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
932 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
934 error = class_device_add(&dev->cdev);
938 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
942 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
946 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
950 __module_get(THIS_MODULE);
952 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
953 printk(KERN_INFO "input: %s as %s\n",
954 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
957 list_for_each_entry(handler, &input_handler_list, node)
958 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
959 if ((id = input_match_device(handler->id_table, dev)))
960 if ((handle = handler->connect(handler, dev, id)))
961 input_link_handle(handle);
963 input_wakeup_procfs_readers();
967 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
968 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
969 fail1: class_device_del(&dev->cdev);
973 void input_unregister_device(struct input_dev *dev)
975 struct list_head * node, * next;
979 del_timer_sync(&dev->timer);
981 list_for_each_safe(node, next, &dev->h_list) {
982 struct input_handle * handle = to_handle(node);
983 list_del_init(&handle->d_node);
984 list_del_init(&handle->h_node);
985 handle->handler->disconnect(handle);
988 list_del_init(&dev->node);
990 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
991 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
992 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
993 class_device_unregister(&dev->cdev);
995 mutex_lock(&dev->mutex);
996 dev->name = dev->phys = dev->uniq = NULL;
997 mutex_unlock(&dev->mutex);
999 input_wakeup_procfs_readers();
1002 void input_register_handler(struct input_handler *handler)
1004 struct input_dev *dev;
1005 struct input_handle *handle;
1006 struct input_device_id *id;
1008 if (!handler) return;
1010 INIT_LIST_HEAD(&handler->h_list);
1012 if (handler->fops != NULL)
1013 input_table[handler->minor >> 5] = handler;
1015 list_add_tail(&handler->node, &input_handler_list);
1017 list_for_each_entry(dev, &input_dev_list, node)
1018 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1019 if ((id = input_match_device(handler->id_table, dev)))
1020 if ((handle = handler->connect(handler, dev, id)))
1021 input_link_handle(handle);
1023 input_wakeup_procfs_readers();
1026 void input_unregister_handler(struct input_handler *handler)
1028 struct list_head * node, * next;
1030 list_for_each_safe(node, next, &handler->h_list) {
1031 struct input_handle * handle = to_handle_h(node);
1032 list_del_init(&handle->h_node);
1033 list_del_init(&handle->d_node);
1034 handler->disconnect(handle);
1037 list_del_init(&handler->node);
1039 if (handler->fops != NULL)
1040 input_table[handler->minor >> 5] = NULL;
1042 input_wakeup_procfs_readers();
1045 static int input_open_file(struct inode *inode, struct file *file)
1047 struct input_handler *handler = input_table[iminor(inode) >> 5];
1048 const struct file_operations *old_fops, *new_fops = NULL;
1051 /* No load-on-demand here? */
1052 if (!handler || !(new_fops = fops_get(handler->fops)))
1056 * That's _really_ odd. Usually NULL ->open means "nothing special",
1057 * not "no device". Oh, well...
1059 if (!new_fops->open) {
1063 old_fops = file->f_op;
1064 file->f_op = new_fops;
1066 err = new_fops->open(inode, file);
1069 fops_put(file->f_op);
1070 file->f_op = fops_get(old_fops);
1076 static struct file_operations input_fops = {
1077 .owner = THIS_MODULE,
1078 .open = input_open_file,
1081 static int __init input_init(void)
1085 err = class_register(&input_class);
1087 printk(KERN_ERR "input: unable to register input_dev class\n");
1091 err = input_proc_init();
1095 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1097 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1103 fail2: input_proc_exit();
1104 fail1: class_unregister(&input_class);
1108 static void __exit input_exit(void)
1111 unregister_chrdev(INPUT_MAJOR, "input");
1112 class_unregister(&input_class);
1115 subsys_initcall(input_init);
1116 module_exit(input_exit);