Input: make modalias code respect allowed buffer size
[linux-2.6/mini2440.git] / drivers / input / input.c
bloba935abeffffc4618bb7f99f429b09e81f1ce4609
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_register_device);
33 EXPORT_SYMBOL(input_unregister_device);
34 EXPORT_SYMBOL(input_register_handler);
35 EXPORT_SYMBOL(input_unregister_handler);
36 EXPORT_SYMBOL(input_grab_device);
37 EXPORT_SYMBOL(input_release_device);
38 EXPORT_SYMBOL(input_open_device);
39 EXPORT_SYMBOL(input_close_device);
40 EXPORT_SYMBOL(input_accept_process);
41 EXPORT_SYMBOL(input_flush_device);
42 EXPORT_SYMBOL(input_event);
43 EXPORT_SYMBOL_GPL(input_class);
45 #define INPUT_DEVICES 256
47 static LIST_HEAD(input_dev_list);
48 static LIST_HEAD(input_handler_list);
50 static struct input_handler *input_table[8];
52 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
54 struct input_handle *handle;
56 if (type > EV_MAX || !test_bit(type, dev->evbit))
57 return;
59 add_input_randomness(type, code, value);
61 switch (type) {
63 case EV_SYN:
64 switch (code) {
65 case SYN_CONFIG:
66 if (dev->event) dev->event(dev, type, code, value);
67 break;
69 case SYN_REPORT:
70 if (dev->sync) return;
71 dev->sync = 1;
72 break;
74 break;
76 case EV_KEY:
78 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
79 return;
81 if (value == 2)
82 break;
84 change_bit(code, dev->key);
86 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
87 dev->repeat_key = code;
88 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
91 break;
93 case EV_SW:
95 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
96 return;
98 change_bit(code, dev->sw);
100 break;
102 case EV_ABS:
104 if (code > ABS_MAX || !test_bit(code, dev->absbit))
105 return;
107 if (dev->absfuzz[code]) {
108 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
109 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
110 return;
112 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
113 (value < dev->abs[code] + dev->absfuzz[code]))
114 value = (dev->abs[code] * 3 + value) >> 2;
116 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
117 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
118 value = (dev->abs[code] + value) >> 1;
121 if (dev->abs[code] == value)
122 return;
124 dev->abs[code] = value;
125 break;
127 case EV_REL:
129 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
130 return;
132 break;
134 case EV_MSC:
136 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
137 return;
139 if (dev->event) dev->event(dev, type, code, value);
141 break;
143 case EV_LED:
145 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
146 return;
148 change_bit(code, dev->led);
149 if (dev->event) dev->event(dev, type, code, value);
151 break;
153 case EV_SND:
155 if (code > SND_MAX || !test_bit(code, dev->sndbit))
156 return;
158 if (dev->event) dev->event(dev, type, code, value);
160 break;
162 case EV_REP:
164 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
166 dev->rep[code] = value;
167 if (dev->event) dev->event(dev, type, code, value);
169 break;
171 case EV_FF:
172 if (dev->event) dev->event(dev, type, code, value);
173 break;
176 if (type != EV_SYN)
177 dev->sync = 0;
179 if (dev->grab)
180 dev->grab->handler->event(dev->grab, type, code, value);
181 else
182 list_for_each_entry(handle, &dev->h_list, d_node)
183 if (handle->open)
184 handle->handler->event(handle, type, code, value);
187 static void input_repeat_key(unsigned long data)
189 struct input_dev *dev = (void *) data;
191 if (!test_bit(dev->repeat_key, dev->key))
192 return;
194 input_event(dev, EV_KEY, dev->repeat_key, 2);
195 input_sync(dev);
197 if (dev->rep[REP_PERIOD])
198 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
201 int input_accept_process(struct input_handle *handle, struct file *file)
203 if (handle->dev->accept)
204 return handle->dev->accept(handle->dev, file);
206 return 0;
209 int input_grab_device(struct input_handle *handle)
211 if (handle->dev->grab)
212 return -EBUSY;
214 handle->dev->grab = handle;
215 return 0;
218 void input_release_device(struct input_handle *handle)
220 if (handle->dev->grab == handle)
221 handle->dev->grab = NULL;
224 int input_open_device(struct input_handle *handle)
226 struct input_dev *dev = handle->dev;
227 int err;
229 err = mutex_lock_interruptible(&dev->mutex);
230 if (err)
231 return err;
233 handle->open++;
235 if (!dev->users++ && dev->open)
236 err = dev->open(dev);
238 if (err)
239 handle->open--;
241 mutex_unlock(&dev->mutex);
243 return err;
246 int input_flush_device(struct input_handle* handle, struct file* file)
248 if (handle->dev->flush)
249 return handle->dev->flush(handle->dev, file);
251 return 0;
254 void input_close_device(struct input_handle *handle)
256 struct input_dev *dev = handle->dev;
258 input_release_device(handle);
260 mutex_lock(&dev->mutex);
262 if (!--dev->users && dev->close)
263 dev->close(dev);
264 handle->open--;
266 mutex_unlock(&dev->mutex);
269 static void input_link_handle(struct input_handle *handle)
271 list_add_tail(&handle->d_node, &handle->dev->h_list);
272 list_add_tail(&handle->h_node, &handle->handler->h_list);
275 #define MATCH_BIT(bit, max) \
276 for (i = 0; i < NBITS(max); i++) \
277 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
278 break; \
279 if (i != NBITS(max)) \
280 continue;
282 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
284 int i;
286 for (; id->flags || id->driver_info; id++) {
288 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
289 if (id->id.bustype != dev->id.bustype)
290 continue;
292 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
293 if (id->id.vendor != dev->id.vendor)
294 continue;
296 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
297 if (id->id.product != dev->id.product)
298 continue;
300 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
301 if (id->id.version != dev->id.version)
302 continue;
304 MATCH_BIT(evbit, EV_MAX);
305 MATCH_BIT(keybit, KEY_MAX);
306 MATCH_BIT(relbit, REL_MAX);
307 MATCH_BIT(absbit, ABS_MAX);
308 MATCH_BIT(mscbit, MSC_MAX);
309 MATCH_BIT(ledbit, LED_MAX);
310 MATCH_BIT(sndbit, SND_MAX);
311 MATCH_BIT(ffbit, FF_MAX);
312 MATCH_BIT(swbit, SW_MAX);
314 return id;
317 return NULL;
320 #ifdef CONFIG_PROC_FS
322 static struct proc_dir_entry *proc_bus_input_dir;
323 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
324 static int input_devices_state;
326 static inline void input_wakeup_procfs_readers(void)
328 input_devices_state++;
329 wake_up(&input_devices_poll_wait);
332 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
334 int state = input_devices_state;
335 poll_wait(file, &input_devices_poll_wait, wait);
336 if (state != input_devices_state)
337 return POLLIN | POLLRDNORM;
338 return 0;
341 static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
343 struct list_head *node;
344 loff_t i = 0;
346 list_for_each(node, list)
347 if (i++ == *pos)
348 return node;
350 return NULL;
353 static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
355 if (element->next == list)
356 return NULL;
358 ++(*pos);
359 return element->next;
362 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
364 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
366 return list_get_nth_element(&input_dev_list, pos);
369 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
371 return list_get_next_element(&input_dev_list, v, pos);
374 static void input_devices_seq_stop(struct seq_file *seq, void *v)
376 /* release lock here */
379 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
380 unsigned long *bitmap, int max)
382 int i;
384 for (i = NBITS(max) - 1; i > 0; i--)
385 if (bitmap[i])
386 break;
388 seq_printf(seq, "B: %s=", name);
389 for (; i >= 0; i--)
390 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
391 seq_putc(seq, '\n');
394 static int input_devices_seq_show(struct seq_file *seq, void *v)
396 struct input_dev *dev = container_of(v, struct input_dev, node);
397 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
398 struct input_handle *handle;
400 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
401 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
403 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
404 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
405 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
406 seq_printf(seq, "H: Handlers=");
408 list_for_each_entry(handle, &dev->h_list, d_node)
409 seq_printf(seq, "%s ", handle->name);
410 seq_putc(seq, '\n');
412 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
413 if (test_bit(EV_KEY, dev->evbit))
414 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
415 if (test_bit(EV_REL, dev->evbit))
416 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
417 if (test_bit(EV_ABS, dev->evbit))
418 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
419 if (test_bit(EV_MSC, dev->evbit))
420 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
421 if (test_bit(EV_LED, dev->evbit))
422 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
423 if (test_bit(EV_SND, dev->evbit))
424 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
425 if (test_bit(EV_FF, dev->evbit))
426 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
427 if (test_bit(EV_SW, dev->evbit))
428 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
430 seq_putc(seq, '\n');
432 kfree(path);
433 return 0;
436 static struct seq_operations input_devices_seq_ops = {
437 .start = input_devices_seq_start,
438 .next = input_devices_seq_next,
439 .stop = input_devices_seq_stop,
440 .show = input_devices_seq_show,
443 static int input_proc_devices_open(struct inode *inode, struct file *file)
445 return seq_open(file, &input_devices_seq_ops);
448 static struct file_operations input_devices_fileops = {
449 .owner = THIS_MODULE,
450 .open = input_proc_devices_open,
451 .poll = input_proc_devices_poll,
452 .read = seq_read,
453 .llseek = seq_lseek,
454 .release = seq_release,
457 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
459 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
460 seq->private = (void *)(unsigned long)*pos;
461 return list_get_nth_element(&input_handler_list, pos);
464 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
466 seq->private = (void *)(unsigned long)(*pos + 1);
467 return list_get_next_element(&input_handler_list, v, pos);
470 static void input_handlers_seq_stop(struct seq_file *seq, void *v)
472 /* release lock here */
475 static int input_handlers_seq_show(struct seq_file *seq, void *v)
477 struct input_handler *handler = container_of(v, struct input_handler, node);
479 seq_printf(seq, "N: Number=%ld Name=%s",
480 (unsigned long)seq->private, handler->name);
481 if (handler->fops)
482 seq_printf(seq, " Minor=%d", handler->minor);
483 seq_putc(seq, '\n');
485 return 0;
487 static struct seq_operations input_handlers_seq_ops = {
488 .start = input_handlers_seq_start,
489 .next = input_handlers_seq_next,
490 .stop = input_handlers_seq_stop,
491 .show = input_handlers_seq_show,
494 static int input_proc_handlers_open(struct inode *inode, struct file *file)
496 return seq_open(file, &input_handlers_seq_ops);
499 static struct file_operations input_handlers_fileops = {
500 .owner = THIS_MODULE,
501 .open = input_proc_handlers_open,
502 .read = seq_read,
503 .llseek = seq_lseek,
504 .release = seq_release,
507 static int __init input_proc_init(void)
509 struct proc_dir_entry *entry;
511 proc_bus_input_dir = proc_mkdir("input", proc_bus);
512 if (!proc_bus_input_dir)
513 return -ENOMEM;
515 proc_bus_input_dir->owner = THIS_MODULE;
517 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
518 if (!entry)
519 goto fail1;
521 entry->owner = THIS_MODULE;
522 entry->proc_fops = &input_devices_fileops;
524 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
525 if (!entry)
526 goto fail2;
528 entry->owner = THIS_MODULE;
529 entry->proc_fops = &input_handlers_fileops;
531 return 0;
533 fail2: remove_proc_entry("devices", proc_bus_input_dir);
534 fail1: remove_proc_entry("input", proc_bus);
535 return -ENOMEM;
538 static void input_proc_exit(void)
540 remove_proc_entry("devices", proc_bus_input_dir);
541 remove_proc_entry("handlers", proc_bus_input_dir);
542 remove_proc_entry("input", proc_bus);
545 #else /* !CONFIG_PROC_FS */
546 static inline void input_wakeup_procfs_readers(void) { }
547 static inline int input_proc_init(void) { return 0; }
548 static inline void input_proc_exit(void) { }
549 #endif
551 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
552 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
554 struct input_dev *input_dev = to_input_dev(dev); \
555 int retval; \
557 retval = mutex_lock_interruptible(&input_dev->mutex); \
558 if (retval) \
559 return retval; \
561 retval = scnprintf(buf, PAGE_SIZE, \
562 "%s\n", input_dev->name ? input_dev->name : ""); \
564 mutex_unlock(&input_dev->mutex); \
566 return retval; \
568 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
570 INPUT_DEV_STRING_ATTR_SHOW(name);
571 INPUT_DEV_STRING_ATTR_SHOW(phys);
572 INPUT_DEV_STRING_ATTR_SHOW(uniq);
574 static int input_print_modalias_bits(char *buf, int size,
575 char name, unsigned long *bm,
576 unsigned int min_bit, unsigned int max_bit)
578 int len = 0, i;
580 len += snprintf(buf, max(size, 0), "%c", name);
581 for (i = min_bit; i < max_bit; i++)
582 if (bm[LONG(i)] & BIT(i))
583 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
584 return len;
587 static int input_print_modalias(char *buf, int size, struct input_dev *id,
588 int add_cr)
590 int len;
592 len = snprintf(buf, max(size, 0),
593 "input:b%04Xv%04Xp%04Xe%04X-",
594 id->id.bustype, id->id.vendor,
595 id->id.product, id->id.version);
597 len += input_print_modalias_bits(buf + len, size - len,
598 'e', id->evbit, 0, EV_MAX);
599 len += input_print_modalias_bits(buf + len, size - len,
600 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
601 len += input_print_modalias_bits(buf + len, size - len,
602 'r', id->relbit, 0, REL_MAX);
603 len += input_print_modalias_bits(buf + len, size - len,
604 'a', id->absbit, 0, ABS_MAX);
605 len += input_print_modalias_bits(buf + len, size - len,
606 'm', id->mscbit, 0, MSC_MAX);
607 len += input_print_modalias_bits(buf + len, size - len,
608 'l', id->ledbit, 0, LED_MAX);
609 len += input_print_modalias_bits(buf + len, size - len,
610 's', id->sndbit, 0, SND_MAX);
611 len += input_print_modalias_bits(buf + len, size - len,
612 'f', id->ffbit, 0, FF_MAX);
613 len += input_print_modalias_bits(buf + len, size - len,
614 'w', id->swbit, 0, SW_MAX);
616 if (add_cr)
617 len += snprintf(buf + len, max(size - len, 0), "\n");
619 return len;
622 static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
624 struct input_dev *id = to_input_dev(dev);
625 ssize_t len;
627 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
629 return max_t(int, len, PAGE_SIZE);
631 static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
633 static struct attribute *input_dev_attrs[] = {
634 &class_device_attr_name.attr,
635 &class_device_attr_phys.attr,
636 &class_device_attr_uniq.attr,
637 &class_device_attr_modalias.attr,
638 NULL
641 static struct attribute_group input_dev_attr_group = {
642 .attrs = input_dev_attrs,
645 #define INPUT_DEV_ID_ATTR(name) \
646 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
648 struct input_dev *input_dev = to_input_dev(dev); \
649 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
651 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
653 INPUT_DEV_ID_ATTR(bustype);
654 INPUT_DEV_ID_ATTR(vendor);
655 INPUT_DEV_ID_ATTR(product);
656 INPUT_DEV_ID_ATTR(version);
658 static struct attribute *input_dev_id_attrs[] = {
659 &class_device_attr_bustype.attr,
660 &class_device_attr_vendor.attr,
661 &class_device_attr_product.attr,
662 &class_device_attr_version.attr,
663 NULL
666 static struct attribute_group input_dev_id_attr_group = {
667 .name = "id",
668 .attrs = input_dev_id_attrs,
671 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
672 int max, int add_cr)
674 int i;
675 int len = 0;
677 for (i = NBITS(max) - 1; i > 0; i--)
678 if (bitmap[i])
679 break;
681 for (; i >= 0; i--)
682 len += snprintf(buf + len, max(buf_size - len, 0),
683 "%lx%s", bitmap[i], i > 0 ? " " : "");
685 if (add_cr)
686 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
688 return len;
691 #define INPUT_DEV_CAP_ATTR(ev, bm) \
692 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
694 struct input_dev *input_dev = to_input_dev(dev); \
695 int len = input_print_bitmap(buf, PAGE_SIZE, \
696 input_dev->bm##bit, ev##_MAX, 1); \
697 return min_t(int, len, PAGE_SIZE); \
699 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
701 INPUT_DEV_CAP_ATTR(EV, ev);
702 INPUT_DEV_CAP_ATTR(KEY, key);
703 INPUT_DEV_CAP_ATTR(REL, rel);
704 INPUT_DEV_CAP_ATTR(ABS, abs);
705 INPUT_DEV_CAP_ATTR(MSC, msc);
706 INPUT_DEV_CAP_ATTR(LED, led);
707 INPUT_DEV_CAP_ATTR(SND, snd);
708 INPUT_DEV_CAP_ATTR(FF, ff);
709 INPUT_DEV_CAP_ATTR(SW, sw);
711 static struct attribute *input_dev_caps_attrs[] = {
712 &class_device_attr_ev.attr,
713 &class_device_attr_key.attr,
714 &class_device_attr_rel.attr,
715 &class_device_attr_abs.attr,
716 &class_device_attr_msc.attr,
717 &class_device_attr_led.attr,
718 &class_device_attr_snd.attr,
719 &class_device_attr_ff.attr,
720 &class_device_attr_sw.attr,
721 NULL
724 static struct attribute_group input_dev_caps_attr_group = {
725 .name = "capabilities",
726 .attrs = input_dev_caps_attrs,
729 static void input_dev_release(struct class_device *class_dev)
731 struct input_dev *dev = to_input_dev(class_dev);
733 kfree(dev);
734 module_put(THIS_MODULE);
738 * Input uevent interface - loading event handlers based on
739 * device bitfields.
741 static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
742 char *buffer, int buffer_size, int *cur_len,
743 const char *name, unsigned long *bitmap, int max)
745 if (*cur_index >= num_envp - 1)
746 return -ENOMEM;
748 envp[*cur_index] = buffer + *cur_len;
750 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
751 if (*cur_len >= buffer_size)
752 return -ENOMEM;
754 *cur_len += input_print_bitmap(buffer + *cur_len,
755 max(buffer_size - *cur_len, 0),
756 bitmap, max, 0) + 1;
757 if (*cur_len > buffer_size)
758 return -ENOMEM;
760 (*cur_index)++;
761 return 0;
764 static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
765 char *buffer, int buffer_size, int *cur_len,
766 struct input_dev *dev)
768 if (*cur_index >= num_envp - 1)
769 return -ENOMEM;
771 envp[*cur_index] = buffer + *cur_len;
773 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
774 "MODALIAS=");
775 if (*cur_len >= buffer_size)
776 return -ENOMEM;
778 *cur_len += input_print_modalias(buffer + *cur_len,
779 max(buffer_size - *cur_len, 0),
780 dev, 0) + 1;
781 if (*cur_len > buffer_size)
782 return -ENOMEM;
784 (*cur_index)++;
785 return 0;
788 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
789 do { \
790 int err = add_uevent_var(envp, num_envp, &i, \
791 buffer, buffer_size, &len, \
792 fmt, val); \
793 if (err) \
794 return err; \
795 } while (0)
797 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
798 do { \
799 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
800 buffer, buffer_size, &len, \
801 name, bm, max); \
802 if (err) \
803 return err; \
804 } while (0)
806 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
807 do { \
808 int err = input_add_uevent_modalias_var(envp, \
809 num_envp, &i, \
810 buffer, buffer_size, &len, \
811 dev); \
812 if (err) \
813 return err; \
814 } while (0)
816 static int input_dev_uevent(struct class_device *cdev, char **envp,
817 int num_envp, char *buffer, int buffer_size)
819 struct input_dev *dev = to_input_dev(cdev);
820 int i = 0;
821 int len = 0;
823 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
824 dev->id.bustype, dev->id.vendor,
825 dev->id.product, dev->id.version);
826 if (dev->name)
827 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
828 if (dev->phys)
829 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
830 if (dev->uniq)
831 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
833 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
834 if (test_bit(EV_KEY, dev->evbit))
835 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
836 if (test_bit(EV_REL, dev->evbit))
837 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
838 if (test_bit(EV_ABS, dev->evbit))
839 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
840 if (test_bit(EV_MSC, dev->evbit))
841 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
842 if (test_bit(EV_LED, dev->evbit))
843 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
844 if (test_bit(EV_SND, dev->evbit))
845 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
846 if (test_bit(EV_FF, dev->evbit))
847 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
848 if (test_bit(EV_SW, dev->evbit))
849 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
851 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
853 envp[i] = NULL;
854 return 0;
857 struct class input_class = {
858 .name = "input",
859 .release = input_dev_release,
860 .uevent = input_dev_uevent,
863 struct input_dev *input_allocate_device(void)
865 struct input_dev *dev;
867 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
868 if (dev) {
869 dev->dynalloc = 1;
870 dev->cdev.class = &input_class;
871 class_device_initialize(&dev->cdev);
872 INIT_LIST_HEAD(&dev->h_list);
873 INIT_LIST_HEAD(&dev->node);
876 return dev;
879 int input_register_device(struct input_dev *dev)
881 static atomic_t input_no = ATOMIC_INIT(0);
882 struct input_handle *handle;
883 struct input_handler *handler;
884 struct input_device_id *id;
885 const char *path;
886 int error;
888 if (!dev->dynalloc) {
889 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
890 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
891 dev->name ? dev->name : "<Unknown>");
892 return -EINVAL;
895 mutex_init(&dev->mutex);
896 set_bit(EV_SYN, dev->evbit);
899 * If delay and period are pre-set by the driver, then autorepeating
900 * is handled by the driver itself and we don't do it in input.c.
903 init_timer(&dev->timer);
904 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
905 dev->timer.data = (long) dev;
906 dev->timer.function = input_repeat_key;
907 dev->rep[REP_DELAY] = 250;
908 dev->rep[REP_PERIOD] = 33;
911 INIT_LIST_HEAD(&dev->h_list);
912 list_add_tail(&dev->node, &input_dev_list);
914 dev->cdev.class = &input_class;
915 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
916 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
918 error = class_device_add(&dev->cdev);
919 if (error)
920 return error;
922 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
923 if (error)
924 goto fail1;
926 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
927 if (error)
928 goto fail2;
930 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
931 if (error)
932 goto fail3;
934 __module_get(THIS_MODULE);
936 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
937 printk(KERN_INFO "input: %s as %s\n",
938 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
939 kfree(path);
941 list_for_each_entry(handler, &input_handler_list, node)
942 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
943 if ((id = input_match_device(handler->id_table, dev)))
944 if ((handle = handler->connect(handler, dev, id)))
945 input_link_handle(handle);
947 input_wakeup_procfs_readers();
949 return 0;
951 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
952 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
953 fail1: class_device_del(&dev->cdev);
954 return error;
957 void input_unregister_device(struct input_dev *dev)
959 struct list_head * node, * next;
961 if (!dev) return;
963 del_timer_sync(&dev->timer);
965 list_for_each_safe(node, next, &dev->h_list) {
966 struct input_handle * handle = to_handle(node);
967 list_del_init(&handle->d_node);
968 list_del_init(&handle->h_node);
969 handle->handler->disconnect(handle);
972 list_del_init(&dev->node);
974 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
975 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
976 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
977 class_device_unregister(&dev->cdev);
979 input_wakeup_procfs_readers();
982 void input_register_handler(struct input_handler *handler)
984 struct input_dev *dev;
985 struct input_handle *handle;
986 struct input_device_id *id;
988 if (!handler) return;
990 INIT_LIST_HEAD(&handler->h_list);
992 if (handler->fops != NULL)
993 input_table[handler->minor >> 5] = handler;
995 list_add_tail(&handler->node, &input_handler_list);
997 list_for_each_entry(dev, &input_dev_list, node)
998 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
999 if ((id = input_match_device(handler->id_table, dev)))
1000 if ((handle = handler->connect(handler, dev, id)))
1001 input_link_handle(handle);
1003 input_wakeup_procfs_readers();
1006 void input_unregister_handler(struct input_handler *handler)
1008 struct list_head * node, * next;
1010 list_for_each_safe(node, next, &handler->h_list) {
1011 struct input_handle * handle = to_handle_h(node);
1012 list_del_init(&handle->h_node);
1013 list_del_init(&handle->d_node);
1014 handler->disconnect(handle);
1017 list_del_init(&handler->node);
1019 if (handler->fops != NULL)
1020 input_table[handler->minor >> 5] = NULL;
1022 input_wakeup_procfs_readers();
1025 static int input_open_file(struct inode *inode, struct file *file)
1027 struct input_handler *handler = input_table[iminor(inode) >> 5];
1028 const struct file_operations *old_fops, *new_fops = NULL;
1029 int err;
1031 /* No load-on-demand here? */
1032 if (!handler || !(new_fops = fops_get(handler->fops)))
1033 return -ENODEV;
1036 * That's _really_ odd. Usually NULL ->open means "nothing special",
1037 * not "no device". Oh, well...
1039 if (!new_fops->open) {
1040 fops_put(new_fops);
1041 return -ENODEV;
1043 old_fops = file->f_op;
1044 file->f_op = new_fops;
1046 err = new_fops->open(inode, file);
1048 if (err) {
1049 fops_put(file->f_op);
1050 file->f_op = fops_get(old_fops);
1052 fops_put(old_fops);
1053 return err;
1056 static struct file_operations input_fops = {
1057 .owner = THIS_MODULE,
1058 .open = input_open_file,
1061 static int __init input_init(void)
1063 int err;
1065 err = class_register(&input_class);
1066 if (err) {
1067 printk(KERN_ERR "input: unable to register input_dev class\n");
1068 return err;
1071 err = input_proc_init();
1072 if (err)
1073 goto fail1;
1075 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1076 if (err) {
1077 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1078 goto fail2;
1081 return 0;
1083 fail2: input_proc_exit();
1084 fail1: class_unregister(&input_class);
1085 return err;
1088 static void __exit input_exit(void)
1090 input_proc_exit();
1091 unregister_chrdev(INPUT_MAJOR, "input");
1092 class_unregister(&input_class);
1095 subsys_initcall(input_init);
1096 module_exit(input_exit);