Input: fix formatting to better follow CodingStyle
[linux-2.6/verdex.git] / drivers / input / input.c
blob7570a3f52f18fd98f7c86d129628406012404d34
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/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))
58 return;
60 add_input_randomness(type, code, value);
62 switch (type) {
64 case EV_SYN:
65 switch (code) {
66 case SYN_CONFIG:
67 if (dev->event)
68 dev->event(dev, type, code, value);
69 break;
71 case SYN_REPORT:
72 if (dev->sync)
73 return;
74 dev->sync = 1;
75 break;
77 break;
79 case EV_KEY:
81 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
82 return;
84 if (value == 2)
85 break;
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]));
94 break;
96 case EV_SW:
98 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
99 return;
101 change_bit(code, dev->sw);
103 break;
105 case EV_ABS:
107 if (code > ABS_MAX || !test_bit(code, dev->absbit))
108 return;
110 if (dev->absfuzz[code]) {
111 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
112 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
113 return;
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)
125 return;
127 dev->abs[code] = value;
128 break;
130 case EV_REL:
132 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
133 return;
135 break;
137 case EV_MSC:
139 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
140 return;
142 if (dev->event)
143 dev->event(dev, type, code, value);
145 break;
147 case EV_LED:
149 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
150 return;
152 change_bit(code, dev->led);
154 if (dev->event)
155 dev->event(dev, type, code, value);
157 break;
159 case EV_SND:
161 if (code > SND_MAX || !test_bit(code, dev->sndbit))
162 return;
164 if (!!test_bit(code, dev->snd) != !!value)
165 change_bit(code, dev->snd);
167 if (dev->event)
168 dev->event(dev, type, code, value);
170 break;
172 case EV_REP:
174 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
175 return;
177 dev->rep[code] = value;
178 if (dev->event)
179 dev->event(dev, type, code, value);
181 break;
183 case EV_FF:
184 if (dev->event)
185 dev->event(dev, type, code, value);
186 break;
189 if (type != EV_SYN)
190 dev->sync = 0;
192 if (dev->grab)
193 dev->grab->handler->event(dev->grab, type, code, value);
194 else
195 list_for_each_entry(handle, &dev->h_list, d_node)
196 if (handle->open)
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))
205 return;
207 input_event(dev, EV_KEY, dev->repeat_key, 2);
208 input_sync(dev);
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);
219 return 0;
222 int input_grab_device(struct input_handle *handle)
224 if (handle->dev->grab)
225 return -EBUSY;
227 handle->dev->grab = handle;
228 return 0;
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;
240 int err;
242 err = mutex_lock_interruptible(&dev->mutex);
243 if (err)
244 return err;
246 handle->open++;
248 if (!dev->users++ && dev->open)
249 err = dev->open(dev);
251 if (err)
252 handle->open--;
254 mutex_unlock(&dev->mutex);
256 return err;
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);
264 return 0;
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)
276 dev->close(dev);
277 handle->open--;
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]) \
291 break; \
292 if (i != NBITS(max)) \
293 continue;
295 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
297 int i;
299 for (; id->flags || id->driver_info; id++) {
301 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
302 if (id->bustype != dev->id.bustype)
303 continue;
305 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
306 if (id->vendor != dev->id.vendor)
307 continue;
309 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
310 if (id->product != dev->id.product)
311 continue;
313 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
314 if (id->version != dev->id.version)
315 continue;
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);
327 return id;
330 return NULL;
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;
353 return 0;
356 static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
358 struct list_head *node;
359 loff_t i = 0;
361 list_for_each(node, list)
362 if (i++ == *pos)
363 return node;
365 return NULL;
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)
371 return NULL;
373 ++(*pos);
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)
397 int i;
399 for (i = NBITS(max) - 1; i > 0; i--)
400 if (bitmap[i])
401 break;
403 seq_printf(seq, "B: %s=", name);
404 for (; i >= 0; i--)
405 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
406 seq_putc(seq, '\n');
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);
425 seq_putc(seq, '\n');
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);
445 seq_putc(seq, '\n');
447 kfree(path);
448 return 0;
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,
467 .read = seq_read,
468 .llseek = seq_lseek,
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);
496 if (handler->fops)
497 seq_printf(seq, " Minor=%d", handler->minor);
498 seq_putc(seq, '\n');
500 return 0;
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,
517 .read = seq_read,
518 .llseek = seq_lseek,
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)
528 return -ENOMEM;
530 proc_bus_input_dir->owner = THIS_MODULE;
532 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
533 if (!entry)
534 goto fail1;
536 entry->owner = THIS_MODULE;
537 entry->proc_fops = &input_devices_fileops;
539 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
540 if (!entry)
541 goto fail2;
543 entry->owner = THIS_MODULE;
544 entry->proc_fops = &input_handlers_fileops;
546 return 0;
548 fail2: remove_proc_entry("devices", proc_bus_input_dir);
549 fail1: remove_proc_entry("input", proc_bus);
550 return -ENOMEM;
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) { }
564 #endif
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); \
570 int retval; \
572 retval = mutex_lock_interruptible(&input_dev->mutex); \
573 if (retval) \
574 return retval; \
576 retval = scnprintf(buf, PAGE_SIZE, \
577 "%s\n", input_dev->name ? input_dev->name : ""); \
579 mutex_unlock(&input_dev->mutex); \
581 return retval; \
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)
593 int len = 0, i;
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);
599 return len;
602 static int input_print_modalias(char *buf, int size, struct input_dev *id,
603 int add_cr)
605 int len;
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);
631 if (add_cr)
632 len += snprintf(buf + len, max(size - len, 0), "\n");
634 return len;
637 static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
639 struct input_dev *id = to_input_dev(dev);
640 ssize_t len;
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,
653 NULL
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,
678 NULL
681 static struct attribute_group input_dev_id_attr_group = {
682 .name = "id",
683 .attrs = input_dev_id_attrs,
686 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
687 int max, int add_cr)
689 int i;
690 int len = 0;
692 for (i = NBITS(max) - 1; i > 0; i--)
693 if (bitmap[i])
694 break;
696 for (; i >= 0; i--)
697 len += snprintf(buf + len, max(buf_size - len, 0),
698 "%lx%s", bitmap[i], i > 0 ? " " : "");
700 if (add_cr)
701 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
703 return len;
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,
736 NULL
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);
748 kfree(dev);
749 module_put(THIS_MODULE);
753 * Input uevent interface - loading event handlers based on
754 * device bitfields.
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)
761 return -ENOMEM;
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)
767 return -ENOMEM;
769 *cur_len += input_print_bitmap(buffer + *cur_len,
770 max(buffer_size - *cur_len, 0),
771 bitmap, max, 0) + 1;
772 if (*cur_len > buffer_size)
773 return -ENOMEM;
775 (*cur_index)++;
776 return 0;
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)
784 return -ENOMEM;
786 envp[*cur_index] = buffer + *cur_len;
788 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
789 "MODALIAS=");
790 if (*cur_len >= buffer_size)
791 return -ENOMEM;
793 *cur_len += input_print_modalias(buffer + *cur_len,
794 max(buffer_size - *cur_len, 0),
795 dev, 0) + 1;
796 if (*cur_len > buffer_size)
797 return -ENOMEM;
799 (*cur_index)++;
800 return 0;
803 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
804 do { \
805 int err = add_uevent_var(envp, num_envp, &i, \
806 buffer, buffer_size, &len, \
807 fmt, val); \
808 if (err) \
809 return err; \
810 } while (0)
812 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
813 do { \
814 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
815 buffer, buffer_size, &len, \
816 name, bm, max); \
817 if (err) \
818 return err; \
819 } while (0)
821 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
822 do { \
823 int err = input_add_uevent_modalias_var(envp, \
824 num_envp, &i, \
825 buffer, buffer_size, &len, \
826 dev); \
827 if (err) \
828 return err; \
829 } while (0)
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);
835 int i = 0;
836 int len = 0;
838 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
839 dev->id.bustype, dev->id.vendor,
840 dev->id.product, dev->id.version);
841 if (dev->name)
842 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
843 if (dev->phys)
844 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
845 if (dev->uniq)
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);
868 envp[i] = NULL;
869 return 0;
872 struct class input_class = {
873 .name = "input",
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);
883 if (dev) {
884 dev->dynalloc = 1;
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);
892 return dev;
895 void input_free_device(struct input_dev *dev)
897 if (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;
913 const char *path;
914 int error;
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>");
920 return -EINVAL;
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);
946 if (error)
947 return error;
949 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
950 if (error)
951 goto fail1;
953 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
954 if (error)
955 goto fail2;
957 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
958 if (error)
959 goto fail3;
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");
966 kfree(path);
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();
976 return 0;
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);
981 return error;
984 void input_unregister_device(struct input_dev *dev)
986 struct list_head *node, *next;
988 if (!dev)
989 return;
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;
1020 if (!handler)
1021 return;
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;
1062 int err;
1064 /* No load-on-demand here? */
1065 if (!handler || !(new_fops = fops_get(handler->fops)))
1066 return -ENODEV;
1069 * That's _really_ odd. Usually NULL ->open means "nothing special",
1070 * not "no device". Oh, well...
1072 if (!new_fops->open) {
1073 fops_put(new_fops);
1074 return -ENODEV;
1076 old_fops = file->f_op;
1077 file->f_op = new_fops;
1079 err = new_fops->open(inode, file);
1081 if (err) {
1082 fops_put(file->f_op);
1083 file->f_op = fops_get(old_fops);
1085 fops_put(old_fops);
1086 return err;
1089 static struct file_operations input_fops = {
1090 .owner = THIS_MODULE,
1091 .open = input_open_file,
1094 static int __init input_init(void)
1096 int err;
1098 err = class_register(&input_class);
1099 if (err) {
1100 printk(KERN_ERR "input: unable to register input_dev class\n");
1101 return err;
1104 err = input_proc_init();
1105 if (err)
1106 goto fail1;
1108 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1109 if (err) {
1110 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1111 goto fail2;
1114 return 0;
1116 fail2: input_proc_exit();
1117 fail1: class_unregister(&input_class);
1118 return err;
1121 static void __exit input_exit(void)
1123 input_proc_exit();
1124 unregister_chrdev(INPUT_MAJOR, "input");
1125 class_unregister(&input_class);
1128 subsys_initcall(input_init);
1129 module_exit(input_exit);