Input: limit attributes' output to PAGE_SIZE
[linux-2.6/linux-2.6-openrd.git] / drivers / input / input.c
blob8dcd3931fa629696746b87a6a3886da56642f557
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/interrupt.h>
22 #include <linux/poll.h>
23 #include <linux/device.h>
24 #include <linux/mutex.h>
26 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
27 MODULE_DESCRIPTION("Input core");
28 MODULE_LICENSE("GPL");
30 EXPORT_SYMBOL(input_allocate_device);
31 EXPORT_SYMBOL(input_register_device);
32 EXPORT_SYMBOL(input_unregister_device);
33 EXPORT_SYMBOL(input_register_handler);
34 EXPORT_SYMBOL(input_unregister_handler);
35 EXPORT_SYMBOL(input_grab_device);
36 EXPORT_SYMBOL(input_release_device);
37 EXPORT_SYMBOL(input_open_device);
38 EXPORT_SYMBOL(input_close_device);
39 EXPORT_SYMBOL(input_accept_process);
40 EXPORT_SYMBOL(input_flush_device);
41 EXPORT_SYMBOL(input_event);
42 EXPORT_SYMBOL_GPL(input_class);
44 #define INPUT_DEVICES 256
46 static LIST_HEAD(input_dev_list);
47 static LIST_HEAD(input_handler_list);
49 static struct input_handler *input_table[8];
51 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
53 struct input_handle *handle;
55 if (type > EV_MAX || !test_bit(type, dev->evbit))
56 return;
58 add_input_randomness(type, code, value);
60 switch (type) {
62 case EV_SYN:
63 switch (code) {
64 case SYN_CONFIG:
65 if (dev->event) dev->event(dev, type, code, value);
66 break;
68 case SYN_REPORT:
69 if (dev->sync) return;
70 dev->sync = 1;
71 break;
73 break;
75 case EV_KEY:
77 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
78 return;
80 if (value == 2)
81 break;
83 change_bit(code, dev->key);
85 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
86 dev->repeat_key = code;
87 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
90 break;
92 case EV_SW:
94 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
95 return;
97 change_bit(code, dev->sw);
99 break;
101 case EV_ABS:
103 if (code > ABS_MAX || !test_bit(code, dev->absbit))
104 return;
106 if (dev->absfuzz[code]) {
107 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
108 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
109 return;
111 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
112 (value < dev->abs[code] + dev->absfuzz[code]))
113 value = (dev->abs[code] * 3 + value) >> 2;
115 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
116 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
117 value = (dev->abs[code] + value) >> 1;
120 if (dev->abs[code] == value)
121 return;
123 dev->abs[code] = value;
124 break;
126 case EV_REL:
128 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
129 return;
131 break;
133 case EV_MSC:
135 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
136 return;
138 if (dev->event) dev->event(dev, type, code, value);
140 break;
142 case EV_LED:
144 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
145 return;
147 change_bit(code, dev->led);
148 if (dev->event) dev->event(dev, type, code, value);
150 break;
152 case EV_SND:
154 if (code > SND_MAX || !test_bit(code, dev->sndbit))
155 return;
157 if (dev->event) dev->event(dev, type, code, value);
159 break;
161 case EV_REP:
163 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
165 dev->rep[code] = value;
166 if (dev->event) dev->event(dev, type, code, value);
168 break;
170 case EV_FF:
171 if (dev->event) dev->event(dev, type, code, value);
172 break;
175 if (type != EV_SYN)
176 dev->sync = 0;
178 if (dev->grab)
179 dev->grab->handler->event(dev->grab, type, code, value);
180 else
181 list_for_each_entry(handle, &dev->h_list, d_node)
182 if (handle->open)
183 handle->handler->event(handle, type, code, value);
186 static void input_repeat_key(unsigned long data)
188 struct input_dev *dev = (void *) data;
190 if (!test_bit(dev->repeat_key, dev->key))
191 return;
193 input_event(dev, EV_KEY, dev->repeat_key, 2);
194 input_sync(dev);
196 if (dev->rep[REP_PERIOD])
197 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
200 int input_accept_process(struct input_handle *handle, struct file *file)
202 if (handle->dev->accept)
203 return handle->dev->accept(handle->dev, file);
205 return 0;
208 int input_grab_device(struct input_handle *handle)
210 if (handle->dev->grab)
211 return -EBUSY;
213 handle->dev->grab = handle;
214 return 0;
217 void input_release_device(struct input_handle *handle)
219 if (handle->dev->grab == handle)
220 handle->dev->grab = NULL;
223 int input_open_device(struct input_handle *handle)
225 struct input_dev *dev = handle->dev;
226 int err;
228 err = mutex_lock_interruptible(&dev->mutex);
229 if (err)
230 return err;
232 handle->open++;
234 if (!dev->users++ && dev->open)
235 err = dev->open(dev);
237 if (err)
238 handle->open--;
240 mutex_unlock(&dev->mutex);
242 return err;
245 int input_flush_device(struct input_handle* handle, struct file* file)
247 if (handle->dev->flush)
248 return handle->dev->flush(handle->dev, file);
250 return 0;
253 void input_close_device(struct input_handle *handle)
255 struct input_dev *dev = handle->dev;
257 input_release_device(handle);
259 mutex_lock(&dev->mutex);
261 if (!--dev->users && dev->close)
262 dev->close(dev);
263 handle->open--;
265 mutex_unlock(&dev->mutex);
268 static void input_link_handle(struct input_handle *handle)
270 list_add_tail(&handle->d_node, &handle->dev->h_list);
271 list_add_tail(&handle->h_node, &handle->handler->h_list);
274 #define MATCH_BIT(bit, max) \
275 for (i = 0; i < NBITS(max); i++) \
276 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
277 break; \
278 if (i != NBITS(max)) \
279 continue;
281 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
283 int i;
285 for (; id->flags || id->driver_info; id++) {
287 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
288 if (id->id.bustype != dev->id.bustype)
289 continue;
291 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
292 if (id->id.vendor != dev->id.vendor)
293 continue;
295 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
296 if (id->id.product != dev->id.product)
297 continue;
299 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
300 if (id->id.version != dev->id.version)
301 continue;
303 MATCH_BIT(evbit, EV_MAX);
304 MATCH_BIT(keybit, KEY_MAX);
305 MATCH_BIT(relbit, REL_MAX);
306 MATCH_BIT(absbit, ABS_MAX);
307 MATCH_BIT(mscbit, MSC_MAX);
308 MATCH_BIT(ledbit, LED_MAX);
309 MATCH_BIT(sndbit, SND_MAX);
310 MATCH_BIT(ffbit, FF_MAX);
311 MATCH_BIT(swbit, SW_MAX);
313 return id;
316 return NULL;
319 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
320 int max, int add_cr)
322 int i;
323 int len = 0;
325 for (i = NBITS(max) - 1; i > 0; i--)
326 if (bitmap[i])
327 break;
329 for (; i >= 0; i--)
330 len += snprintf(buf + len, max(buf_size - len, 0),
331 "%lx%s", bitmap[i], i > 0 ? " " : "");
333 if (add_cr)
334 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
336 return len;
339 #ifdef CONFIG_PROC_FS
341 static struct proc_dir_entry *proc_bus_input_dir;
342 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
343 static int input_devices_state;
345 static inline void input_wakeup_procfs_readers(void)
347 input_devices_state++;
348 wake_up(&input_devices_poll_wait);
351 static unsigned int input_devices_poll(struct file *file, poll_table *wait)
353 int state = input_devices_state;
354 poll_wait(file, &input_devices_poll_wait, wait);
355 if (state != input_devices_state)
356 return POLLIN | POLLRDNORM;
357 return 0;
360 #define SPRINTF_BIT(ev, bm) \
361 do { \
362 len += sprintf(buf + len, "B: %s=", #ev); \
363 len += input_print_bitmap(buf + len, INT_MAX, \
364 dev->bm##bit, ev##_MAX, 1); \
365 } while (0)
367 #define TEST_AND_SPRINTF_BIT(ev, bm) \
368 do { \
369 if (test_bit(EV_##ev, dev->evbit)) \
370 SPRINTF_BIT(ev, bm); \
371 } while (0)
373 static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
375 struct input_dev *dev;
376 struct input_handle *handle;
377 const char *path;
379 off_t at = 0;
380 int len, cnt = 0;
382 list_for_each_entry(dev, &input_dev_list, node) {
384 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
386 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
387 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
389 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
390 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
391 len += sprintf(buf + len, "S: Sysfs=%s\n", path ? path : "");
392 len += sprintf(buf + len, "H: Handlers=");
394 list_for_each_entry(handle, &dev->h_list, d_node)
395 len += sprintf(buf + len, "%s ", handle->name);
397 len += sprintf(buf + len, "\n");
399 SPRINTF_BIT(EV, ev);
400 TEST_AND_SPRINTF_BIT(KEY, key);
401 TEST_AND_SPRINTF_BIT(REL, rel);
402 TEST_AND_SPRINTF_BIT(ABS, abs);
403 TEST_AND_SPRINTF_BIT(MSC, msc);
404 TEST_AND_SPRINTF_BIT(LED, led);
405 TEST_AND_SPRINTF_BIT(SND, snd);
406 TEST_AND_SPRINTF_BIT(FF, ff);
407 TEST_AND_SPRINTF_BIT(SW, sw);
409 len += sprintf(buf + len, "\n");
411 at += len;
413 if (at >= pos) {
414 if (!*start) {
415 *start = buf + (pos - (at - len));
416 cnt = at - pos;
417 } else cnt += len;
418 buf += len;
419 if (cnt >= count)
420 break;
423 kfree(path);
426 if (&dev->node == &input_dev_list)
427 *eof = 1;
429 return (count > cnt) ? cnt : count;
432 static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
434 struct input_handler *handler;
436 off_t at = 0;
437 int len = 0, cnt = 0;
438 int i = 0;
440 list_for_each_entry(handler, &input_handler_list, node) {
442 if (handler->fops)
443 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
444 i++, handler->name, handler->minor);
445 else
446 len = sprintf(buf, "N: Number=%d Name=%s\n",
447 i++, handler->name);
449 at += len;
451 if (at >= pos) {
452 if (!*start) {
453 *start = buf + (pos - (at - len));
454 cnt = at - pos;
455 } else cnt += len;
456 buf += len;
457 if (cnt >= count)
458 break;
461 if (&handler->node == &input_handler_list)
462 *eof = 1;
464 return (count > cnt) ? cnt : count;
467 static struct file_operations input_fileops;
469 static int __init input_proc_init(void)
471 struct proc_dir_entry *entry;
473 proc_bus_input_dir = proc_mkdir("input", proc_bus);
474 if (!proc_bus_input_dir)
475 return -ENOMEM;
477 proc_bus_input_dir->owner = THIS_MODULE;
479 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
480 if (!entry)
481 goto fail1;
483 entry->owner = THIS_MODULE;
484 input_fileops = *entry->proc_fops;
485 input_fileops.poll = input_devices_poll;
486 entry->proc_fops = &input_fileops;
488 entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
489 if (!entry)
490 goto fail2;
492 entry->owner = THIS_MODULE;
494 return 0;
496 fail2: remove_proc_entry("devices", proc_bus_input_dir);
497 fail1: remove_proc_entry("input", proc_bus);
498 return -ENOMEM;
501 static void input_proc_exit(void)
503 remove_proc_entry("devices", proc_bus_input_dir);
504 remove_proc_entry("handlers", proc_bus_input_dir);
505 remove_proc_entry("input", proc_bus);
508 #else /* !CONFIG_PROC_FS */
509 static inline void input_wakeup_procfs_readers(void) { }
510 static inline int input_proc_init(void) { return 0; }
511 static inline void input_proc_exit(void) { }
512 #endif
514 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
515 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
517 struct input_dev *input_dev = to_input_dev(dev); \
518 int retval; \
520 retval = mutex_lock_interruptible(&input_dev->mutex); \
521 if (retval) \
522 return retval; \
524 retval = scnprintf(buf, PAGE_SIZE, \
525 "%s\n", input_dev->name ? input_dev->name : ""); \
527 mutex_unlock(&input_dev->mutex); \
529 return retval; \
531 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
533 INPUT_DEV_STRING_ATTR_SHOW(name);
534 INPUT_DEV_STRING_ATTR_SHOW(phys);
535 INPUT_DEV_STRING_ATTR_SHOW(uniq);
537 static int print_modalias_bits(char *buf, int size, char prefix, unsigned long *arr,
538 unsigned int min, unsigned int max)
540 int len, i;
542 len = snprintf(buf, size, "%c", prefix);
543 for (i = min; i < max; i++)
544 if (arr[LONG(i)] & BIT(i))
545 len += snprintf(buf + len, size - len, "%X,", i);
546 return len;
549 static int input_print_modalias(char *buf, int size, struct input_dev *id,
550 int add_cr)
552 int len;
554 len = snprintf(buf, size, "input:b%04Xv%04Xp%04Xe%04X-",
555 id->id.bustype,
556 id->id.vendor,
557 id->id.product,
558 id->id.version);
560 len += print_modalias_bits(buf + len, size - len, 'e', id->evbit,
561 0, EV_MAX);
562 len += print_modalias_bits(buf + len, size - len, 'k', id->keybit,
563 KEY_MIN_INTERESTING, KEY_MAX);
564 len += print_modalias_bits(buf + len, size - len, 'r', id->relbit,
565 0, REL_MAX);
566 len += print_modalias_bits(buf + len, size - len, 'a', id->absbit,
567 0, ABS_MAX);
568 len += print_modalias_bits(buf + len, size - len, 'm', id->mscbit,
569 0, MSC_MAX);
570 len += print_modalias_bits(buf + len, size - len, 'l', id->ledbit,
571 0, LED_MAX);
572 len += print_modalias_bits(buf + len, size - len, 's', id->sndbit,
573 0, SND_MAX);
574 len += print_modalias_bits(buf + len, size - len, 'f', id->ffbit,
575 0, FF_MAX);
576 len += print_modalias_bits(buf + len, size - len, 'w', id->swbit,
577 0, SW_MAX);
579 if (add_cr)
580 len += snprintf(buf + len, size - len, "\n");
582 return len;
585 static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
587 struct input_dev *id = to_input_dev(dev);
588 ssize_t len;
590 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
592 return max_t(int, len, PAGE_SIZE);
594 static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
596 static struct attribute *input_dev_attrs[] = {
597 &class_device_attr_name.attr,
598 &class_device_attr_phys.attr,
599 &class_device_attr_uniq.attr,
600 &class_device_attr_modalias.attr,
601 NULL
604 static struct attribute_group input_dev_attr_group = {
605 .attrs = input_dev_attrs,
608 #define INPUT_DEV_ID_ATTR(name) \
609 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
611 struct input_dev *input_dev = to_input_dev(dev); \
612 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
614 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
616 INPUT_DEV_ID_ATTR(bustype);
617 INPUT_DEV_ID_ATTR(vendor);
618 INPUT_DEV_ID_ATTR(product);
619 INPUT_DEV_ID_ATTR(version);
621 static struct attribute *input_dev_id_attrs[] = {
622 &class_device_attr_bustype.attr,
623 &class_device_attr_vendor.attr,
624 &class_device_attr_product.attr,
625 &class_device_attr_version.attr,
626 NULL
629 static struct attribute_group input_dev_id_attr_group = {
630 .name = "id",
631 .attrs = input_dev_id_attrs,
634 #define INPUT_DEV_CAP_ATTR(ev, bm) \
635 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
637 struct input_dev *input_dev = to_input_dev(dev); \
638 int len = input_print_bitmap(buf, PAGE_SIZE, \
639 input_dev->bm##bit, ev##_MAX, 1); \
640 return min_t(int, len, PAGE_SIZE); \
642 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
644 INPUT_DEV_CAP_ATTR(EV, ev);
645 INPUT_DEV_CAP_ATTR(KEY, key);
646 INPUT_DEV_CAP_ATTR(REL, rel);
647 INPUT_DEV_CAP_ATTR(ABS, abs);
648 INPUT_DEV_CAP_ATTR(MSC, msc);
649 INPUT_DEV_CAP_ATTR(LED, led);
650 INPUT_DEV_CAP_ATTR(SND, snd);
651 INPUT_DEV_CAP_ATTR(FF, ff);
652 INPUT_DEV_CAP_ATTR(SW, sw);
654 static struct attribute *input_dev_caps_attrs[] = {
655 &class_device_attr_ev.attr,
656 &class_device_attr_key.attr,
657 &class_device_attr_rel.attr,
658 &class_device_attr_abs.attr,
659 &class_device_attr_msc.attr,
660 &class_device_attr_led.attr,
661 &class_device_attr_snd.attr,
662 &class_device_attr_ff.attr,
663 &class_device_attr_sw.attr,
664 NULL
667 static struct attribute_group input_dev_caps_attr_group = {
668 .name = "capabilities",
669 .attrs = input_dev_caps_attrs,
672 static void input_dev_release(struct class_device *class_dev)
674 struct input_dev *dev = to_input_dev(class_dev);
676 kfree(dev);
677 module_put(THIS_MODULE);
681 * Input uevent interface - loading event handlers based on
682 * device bitfields.
684 static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
685 char *buffer, int buffer_size, int *cur_len,
686 const char *name, unsigned long *bitmap, int max)
688 if (*cur_index >= num_envp - 1)
689 return -ENOMEM;
691 envp[*cur_index] = buffer + *cur_len;
693 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
694 if (*cur_len > buffer_size)
695 return -ENOMEM;
697 *cur_len += input_print_bitmap(buffer + *cur_len,
698 max(buffer_size - *cur_len, 0),
699 bitmap, max, 0) + 1;
700 if (*cur_len > buffer_size)
701 return -ENOMEM;
703 (*cur_index)++;
704 return 0;
707 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
708 do { \
709 int err = add_uevent_var(envp, num_envp, &i, \
710 buffer, buffer_size, &len, \
711 fmt, val); \
712 if (err) \
713 return err; \
714 } while (0)
716 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
717 do { \
718 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
719 buffer, buffer_size, &len, \
720 name, bm, max); \
721 if (err) \
722 return err; \
723 } while (0)
725 static int input_dev_uevent(struct class_device *cdev, char **envp,
726 int num_envp, char *buffer, int buffer_size)
728 struct input_dev *dev = to_input_dev(cdev);
729 int i = 0;
730 int len = 0;
732 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
733 dev->id.bustype, dev->id.vendor,
734 dev->id.product, dev->id.version);
735 if (dev->name)
736 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
737 if (dev->phys)
738 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
739 if (dev->uniq)
740 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
742 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
743 if (test_bit(EV_KEY, dev->evbit))
744 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
745 if (test_bit(EV_REL, dev->evbit))
746 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
747 if (test_bit(EV_ABS, dev->evbit))
748 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
749 if (test_bit(EV_MSC, dev->evbit))
750 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
751 if (test_bit(EV_LED, dev->evbit))
752 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
753 if (test_bit(EV_SND, dev->evbit))
754 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
755 if (test_bit(EV_FF, dev->evbit))
756 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
757 if (test_bit(EV_SW, dev->evbit))
758 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
760 envp[i++] = buffer + len;
761 len += snprintf(buffer + len, buffer_size - len, "MODALIAS=");
762 len += input_print_modalias(buffer + len, buffer_size - len, dev, 0) + 1;
764 envp[i] = NULL;
765 return 0;
768 struct class input_class = {
769 .name = "input",
770 .release = input_dev_release,
771 .uevent = input_dev_uevent,
774 struct input_dev *input_allocate_device(void)
776 struct input_dev *dev;
778 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
779 if (dev) {
780 dev->dynalloc = 1;
781 dev->cdev.class = &input_class;
782 class_device_initialize(&dev->cdev);
783 INIT_LIST_HEAD(&dev->h_list);
784 INIT_LIST_HEAD(&dev->node);
787 return dev;
790 int input_register_device(struct input_dev *dev)
792 static atomic_t input_no = ATOMIC_INIT(0);
793 struct input_handle *handle;
794 struct input_handler *handler;
795 struct input_device_id *id;
796 const char *path;
797 int error;
799 if (!dev->dynalloc) {
800 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
801 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
802 dev->name ? dev->name : "<Unknown>");
803 return -EINVAL;
806 mutex_init(&dev->mutex);
807 set_bit(EV_SYN, dev->evbit);
810 * If delay and period are pre-set by the driver, then autorepeating
811 * is handled by the driver itself and we don't do it in input.c.
814 init_timer(&dev->timer);
815 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
816 dev->timer.data = (long) dev;
817 dev->timer.function = input_repeat_key;
818 dev->rep[REP_DELAY] = 250;
819 dev->rep[REP_PERIOD] = 33;
822 INIT_LIST_HEAD(&dev->h_list);
823 list_add_tail(&dev->node, &input_dev_list);
825 dev->cdev.class = &input_class;
826 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
827 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
829 error = class_device_add(&dev->cdev);
830 if (error)
831 return error;
833 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
834 if (error)
835 goto fail1;
837 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
838 if (error)
839 goto fail2;
841 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
842 if (error)
843 goto fail3;
845 __module_get(THIS_MODULE);
847 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
848 printk(KERN_INFO "input: %s as %s\n",
849 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
850 kfree(path);
852 list_for_each_entry(handler, &input_handler_list, node)
853 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
854 if ((id = input_match_device(handler->id_table, dev)))
855 if ((handle = handler->connect(handler, dev, id)))
856 input_link_handle(handle);
858 input_wakeup_procfs_readers();
860 return 0;
862 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
863 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
864 fail1: class_device_del(&dev->cdev);
865 return error;
868 void input_unregister_device(struct input_dev *dev)
870 struct list_head * node, * next;
872 if (!dev) return;
874 del_timer_sync(&dev->timer);
876 list_for_each_safe(node, next, &dev->h_list) {
877 struct input_handle * handle = to_handle(node);
878 list_del_init(&handle->d_node);
879 list_del_init(&handle->h_node);
880 handle->handler->disconnect(handle);
883 list_del_init(&dev->node);
885 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
886 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
887 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
888 class_device_unregister(&dev->cdev);
890 input_wakeup_procfs_readers();
893 void input_register_handler(struct input_handler *handler)
895 struct input_dev *dev;
896 struct input_handle *handle;
897 struct input_device_id *id;
899 if (!handler) return;
901 INIT_LIST_HEAD(&handler->h_list);
903 if (handler->fops != NULL)
904 input_table[handler->minor >> 5] = handler;
906 list_add_tail(&handler->node, &input_handler_list);
908 list_for_each_entry(dev, &input_dev_list, node)
909 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
910 if ((id = input_match_device(handler->id_table, dev)))
911 if ((handle = handler->connect(handler, dev, id)))
912 input_link_handle(handle);
914 input_wakeup_procfs_readers();
917 void input_unregister_handler(struct input_handler *handler)
919 struct list_head * node, * next;
921 list_for_each_safe(node, next, &handler->h_list) {
922 struct input_handle * handle = to_handle_h(node);
923 list_del_init(&handle->h_node);
924 list_del_init(&handle->d_node);
925 handler->disconnect(handle);
928 list_del_init(&handler->node);
930 if (handler->fops != NULL)
931 input_table[handler->minor >> 5] = NULL;
933 input_wakeup_procfs_readers();
936 static int input_open_file(struct inode *inode, struct file *file)
938 struct input_handler *handler = input_table[iminor(inode) >> 5];
939 const struct file_operations *old_fops, *new_fops = NULL;
940 int err;
942 /* No load-on-demand here? */
943 if (!handler || !(new_fops = fops_get(handler->fops)))
944 return -ENODEV;
947 * That's _really_ odd. Usually NULL ->open means "nothing special",
948 * not "no device". Oh, well...
950 if (!new_fops->open) {
951 fops_put(new_fops);
952 return -ENODEV;
954 old_fops = file->f_op;
955 file->f_op = new_fops;
957 err = new_fops->open(inode, file);
959 if (err) {
960 fops_put(file->f_op);
961 file->f_op = fops_get(old_fops);
963 fops_put(old_fops);
964 return err;
967 static struct file_operations input_fops = {
968 .owner = THIS_MODULE,
969 .open = input_open_file,
972 static int __init input_init(void)
974 int err;
976 err = class_register(&input_class);
977 if (err) {
978 printk(KERN_ERR "input: unable to register input_dev class\n");
979 return err;
982 err = input_proc_init();
983 if (err)
984 goto fail1;
986 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
987 if (err) {
988 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
989 goto fail2;
992 return 0;
994 fail2: input_proc_exit();
995 fail1: class_unregister(&input_class);
996 return err;
999 static void __exit input_exit(void)
1001 input_proc_exit();
1002 unregister_chrdev(INPUT_MAJOR, "input");
1003 class_unregister(&input_class);
1006 subsys_initcall(input_init);
1007 module_exit(input_exit);