kbuild: improve scripts/gcc-version.sh output a bit when called without args
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / input.c
blob5fe7555866230509e48f3a0526858704de27b5b9
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/input.h>
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/major.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/interrupt.h>
21 #include <linux/poll.h>
22 #include <linux/device.h>
23 #include <linux/mutex.h>
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
26 MODULE_DESCRIPTION("Input core");
27 MODULE_LICENSE("GPL");
29 #define INPUT_DEVICES 256
31 static LIST_HEAD(input_dev_list);
32 static LIST_HEAD(input_handler_list);
34 static struct input_handler *input_table[8];
36 /**
37 * input_event() - report new input event
38 * @dev: device that generated the event
39 * @type: type of the event
40 * @code: event code
41 * @value: value of the event
43 * This function should be used by drivers implementing various input devices
44 * See also input_inject_event()
46 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
48 struct input_handle *handle;
50 if (type > EV_MAX || !test_bit(type, dev->evbit))
51 return;
53 add_input_randomness(type, code, value);
55 switch (type) {
57 case EV_SYN:
58 switch (code) {
59 case SYN_CONFIG:
60 if (dev->event)
61 dev->event(dev, type, code, value);
62 break;
64 case SYN_REPORT:
65 if (dev->sync)
66 return;
67 dev->sync = 1;
68 break;
70 break;
72 case EV_KEY:
74 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
75 return;
77 if (value == 2)
78 break;
80 change_bit(code, dev->key);
82 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
83 dev->repeat_key = code;
84 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
87 break;
89 case EV_SW:
91 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
92 return;
94 change_bit(code, dev->sw);
96 break;
98 case EV_ABS:
100 if (code > ABS_MAX || !test_bit(code, dev->absbit))
101 return;
103 if (dev->absfuzz[code]) {
104 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
105 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
106 return;
108 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
109 (value < dev->abs[code] + dev->absfuzz[code]))
110 value = (dev->abs[code] * 3 + value) >> 2;
112 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
113 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
114 value = (dev->abs[code] + value) >> 1;
117 if (dev->abs[code] == value)
118 return;
120 dev->abs[code] = value;
121 break;
123 case EV_REL:
125 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
126 return;
128 break;
130 case EV_MSC:
132 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
133 return;
135 if (dev->event)
136 dev->event(dev, type, code, value);
138 break;
140 case EV_LED:
142 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
143 return;
145 change_bit(code, dev->led);
147 if (dev->event)
148 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 (!!test_bit(code, dev->snd) != !!value)
158 change_bit(code, dev->snd);
160 if (dev->event)
161 dev->event(dev, type, code, value);
163 break;
165 case EV_REP:
167 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
168 return;
170 dev->rep[code] = value;
171 if (dev->event)
172 dev->event(dev, type, code, value);
174 break;
176 case EV_FF:
178 if (value < 0)
179 return;
181 if (dev->event)
182 dev->event(dev, type, code, value);
183 break;
186 if (type != EV_SYN)
187 dev->sync = 0;
189 if (dev->grab)
190 dev->grab->handler->event(dev->grab, type, code, value);
191 else
192 list_for_each_entry(handle, &dev->h_list, d_node)
193 if (handle->open)
194 handle->handler->event(handle, type, code, value);
196 EXPORT_SYMBOL(input_event);
199 * input_inject_event() - send input event from input handler
200 * @handle: input handle to send event through
201 * @type: type of the event
202 * @code: event code
203 * @value: value of the event
205 * Similar to input_event() but will ignore event if device is "grabbed" and handle
206 * injecting event is not the one that owns the device.
208 void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
210 if (!handle->dev->grab || handle->dev->grab == handle)
211 input_event(handle->dev, type, code, value);
213 EXPORT_SYMBOL(input_inject_event);
215 static void input_repeat_key(unsigned long data)
217 struct input_dev *dev = (void *) data;
219 if (!test_bit(dev->repeat_key, dev->key))
220 return;
222 input_event(dev, EV_KEY, dev->repeat_key, 2);
223 input_sync(dev);
225 if (dev->rep[REP_PERIOD])
226 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
229 int input_grab_device(struct input_handle *handle)
231 if (handle->dev->grab)
232 return -EBUSY;
234 handle->dev->grab = handle;
235 return 0;
237 EXPORT_SYMBOL(input_grab_device);
239 void input_release_device(struct input_handle *handle)
241 struct input_dev *dev = handle->dev;
243 if (dev->grab == handle) {
244 dev->grab = NULL;
246 list_for_each_entry(handle, &dev->h_list, d_node)
247 if (handle->handler->start)
248 handle->handler->start(handle);
251 EXPORT_SYMBOL(input_release_device);
253 int input_open_device(struct input_handle *handle)
255 struct input_dev *dev = handle->dev;
256 int err;
258 err = mutex_lock_interruptible(&dev->mutex);
259 if (err)
260 return err;
262 handle->open++;
264 if (!dev->users++ && dev->open)
265 err = dev->open(dev);
267 if (err)
268 handle->open--;
270 mutex_unlock(&dev->mutex);
272 return err;
274 EXPORT_SYMBOL(input_open_device);
276 int input_flush_device(struct input_handle* handle, struct file* file)
278 if (handle->dev->flush)
279 return handle->dev->flush(handle->dev, file);
281 return 0;
283 EXPORT_SYMBOL(input_flush_device);
285 void input_close_device(struct input_handle *handle)
287 struct input_dev *dev = handle->dev;
289 input_release_device(handle);
291 mutex_lock(&dev->mutex);
293 if (!--dev->users && dev->close)
294 dev->close(dev);
295 handle->open--;
297 mutex_unlock(&dev->mutex);
299 EXPORT_SYMBOL(input_close_device);
301 static int input_fetch_keycode(struct input_dev *dev, int scancode)
303 switch (dev->keycodesize) {
304 case 1:
305 return ((u8 *)dev->keycode)[scancode];
307 case 2:
308 return ((u16 *)dev->keycode)[scancode];
310 default:
311 return ((u32 *)dev->keycode)[scancode];
315 static int input_default_getkeycode(struct input_dev *dev,
316 int scancode, int *keycode)
318 if (!dev->keycodesize)
319 return -EINVAL;
321 if (scancode < 0 || scancode >= dev->keycodemax)
322 return -EINVAL;
324 *keycode = input_fetch_keycode(dev, scancode);
326 return 0;
329 static int input_default_setkeycode(struct input_dev *dev,
330 int scancode, int keycode)
332 int old_keycode;
333 int i;
335 if (scancode < 0 || scancode >= dev->keycodemax)
336 return -EINVAL;
338 if (keycode < 0 || keycode > KEY_MAX)
339 return -EINVAL;
341 if (!dev->keycodesize)
342 return -EINVAL;
344 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
345 return -EINVAL;
347 switch (dev->keycodesize) {
348 case 1: {
349 u8 *k = (u8 *)dev->keycode;
350 old_keycode = k[scancode];
351 k[scancode] = keycode;
352 break;
354 case 2: {
355 u16 *k = (u16 *)dev->keycode;
356 old_keycode = k[scancode];
357 k[scancode] = keycode;
358 break;
360 default: {
361 u32 *k = (u32 *)dev->keycode;
362 old_keycode = k[scancode];
363 k[scancode] = keycode;
364 break;
368 clear_bit(old_keycode, dev->keybit);
369 set_bit(keycode, dev->keybit);
371 for (i = 0; i < dev->keycodemax; i++) {
372 if (input_fetch_keycode(dev, i) == old_keycode) {
373 set_bit(old_keycode, dev->keybit);
374 break; /* Setting the bit twice is useless, so break */
378 return 0;
382 #define MATCH_BIT(bit, max) \
383 for (i = 0; i < NBITS(max); i++) \
384 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
385 break; \
386 if (i != NBITS(max)) \
387 continue;
389 static const struct input_device_id *input_match_device(const struct input_device_id *id,
390 struct input_dev *dev)
392 int i;
394 for (; id->flags || id->driver_info; id++) {
396 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
397 if (id->bustype != dev->id.bustype)
398 continue;
400 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
401 if (id->vendor != dev->id.vendor)
402 continue;
404 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
405 if (id->product != dev->id.product)
406 continue;
408 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
409 if (id->version != dev->id.version)
410 continue;
412 MATCH_BIT(evbit, EV_MAX);
413 MATCH_BIT(keybit, KEY_MAX);
414 MATCH_BIT(relbit, REL_MAX);
415 MATCH_BIT(absbit, ABS_MAX);
416 MATCH_BIT(mscbit, MSC_MAX);
417 MATCH_BIT(ledbit, LED_MAX);
418 MATCH_BIT(sndbit, SND_MAX);
419 MATCH_BIT(ffbit, FF_MAX);
420 MATCH_BIT(swbit, SW_MAX);
422 return id;
425 return NULL;
428 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
430 const struct input_device_id *id;
431 int error;
433 if (handler->blacklist && input_match_device(handler->blacklist, dev))
434 return -ENODEV;
436 id = input_match_device(handler->id_table, dev);
437 if (!id)
438 return -ENODEV;
440 error = handler->connect(handler, dev, id);
441 if (error && error != -ENODEV)
442 printk(KERN_ERR
443 "input: failed to attach handler %s to device %s, "
444 "error: %d\n",
445 handler->name, kobject_name(&dev->dev.kobj), error);
447 return error;
451 #ifdef CONFIG_PROC_FS
453 static struct proc_dir_entry *proc_bus_input_dir;
454 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
455 static int input_devices_state;
457 static inline void input_wakeup_procfs_readers(void)
459 input_devices_state++;
460 wake_up(&input_devices_poll_wait);
463 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
465 int state = input_devices_state;
467 poll_wait(file, &input_devices_poll_wait, wait);
468 if (state != input_devices_state)
469 return POLLIN | POLLRDNORM;
471 return 0;
474 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
476 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
478 return seq_list_start(&input_dev_list, *pos);
481 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
483 return seq_list_next(v, &input_dev_list, pos);
486 static void input_devices_seq_stop(struct seq_file *seq, void *v)
488 /* release lock here */
491 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
492 unsigned long *bitmap, int max)
494 int i;
496 for (i = NBITS(max) - 1; i > 0; i--)
497 if (bitmap[i])
498 break;
500 seq_printf(seq, "B: %s=", name);
501 for (; i >= 0; i--)
502 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
503 seq_putc(seq, '\n');
506 static int input_devices_seq_show(struct seq_file *seq, void *v)
508 struct input_dev *dev = container_of(v, struct input_dev, node);
509 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
510 struct input_handle *handle;
512 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
513 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
515 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
516 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
517 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
518 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
519 seq_printf(seq, "H: Handlers=");
521 list_for_each_entry(handle, &dev->h_list, d_node)
522 seq_printf(seq, "%s ", handle->name);
523 seq_putc(seq, '\n');
525 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
526 if (test_bit(EV_KEY, dev->evbit))
527 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
528 if (test_bit(EV_REL, dev->evbit))
529 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
530 if (test_bit(EV_ABS, dev->evbit))
531 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
532 if (test_bit(EV_MSC, dev->evbit))
533 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
534 if (test_bit(EV_LED, dev->evbit))
535 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
536 if (test_bit(EV_SND, dev->evbit))
537 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
538 if (test_bit(EV_FF, dev->evbit))
539 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
540 if (test_bit(EV_SW, dev->evbit))
541 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
543 seq_putc(seq, '\n');
545 kfree(path);
546 return 0;
549 static struct seq_operations input_devices_seq_ops = {
550 .start = input_devices_seq_start,
551 .next = input_devices_seq_next,
552 .stop = input_devices_seq_stop,
553 .show = input_devices_seq_show,
556 static int input_proc_devices_open(struct inode *inode, struct file *file)
558 return seq_open(file, &input_devices_seq_ops);
561 static const struct file_operations input_devices_fileops = {
562 .owner = THIS_MODULE,
563 .open = input_proc_devices_open,
564 .poll = input_proc_devices_poll,
565 .read = seq_read,
566 .llseek = seq_lseek,
567 .release = seq_release,
570 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
572 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
573 seq->private = (void *)(unsigned long)*pos;
574 return seq_list_start(&input_handler_list, *pos);
577 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
579 seq->private = (void *)(unsigned long)(*pos + 1);
580 return seq_list_next(v, &input_handler_list, pos);
583 static void input_handlers_seq_stop(struct seq_file *seq, void *v)
585 /* release lock here */
588 static int input_handlers_seq_show(struct seq_file *seq, void *v)
590 struct input_handler *handler = container_of(v, struct input_handler, node);
592 seq_printf(seq, "N: Number=%ld Name=%s",
593 (unsigned long)seq->private, handler->name);
594 if (handler->fops)
595 seq_printf(seq, " Minor=%d", handler->minor);
596 seq_putc(seq, '\n');
598 return 0;
600 static struct seq_operations input_handlers_seq_ops = {
601 .start = input_handlers_seq_start,
602 .next = input_handlers_seq_next,
603 .stop = input_handlers_seq_stop,
604 .show = input_handlers_seq_show,
607 static int input_proc_handlers_open(struct inode *inode, struct file *file)
609 return seq_open(file, &input_handlers_seq_ops);
612 static const struct file_operations input_handlers_fileops = {
613 .owner = THIS_MODULE,
614 .open = input_proc_handlers_open,
615 .read = seq_read,
616 .llseek = seq_lseek,
617 .release = seq_release,
620 static int __init input_proc_init(void)
622 struct proc_dir_entry *entry;
624 proc_bus_input_dir = proc_mkdir("input", proc_bus);
625 if (!proc_bus_input_dir)
626 return -ENOMEM;
628 proc_bus_input_dir->owner = THIS_MODULE;
630 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
631 if (!entry)
632 goto fail1;
634 entry->owner = THIS_MODULE;
635 entry->proc_fops = &input_devices_fileops;
637 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
638 if (!entry)
639 goto fail2;
641 entry->owner = THIS_MODULE;
642 entry->proc_fops = &input_handlers_fileops;
644 return 0;
646 fail2: remove_proc_entry("devices", proc_bus_input_dir);
647 fail1: remove_proc_entry("input", proc_bus);
648 return -ENOMEM;
651 static void input_proc_exit(void)
653 remove_proc_entry("devices", proc_bus_input_dir);
654 remove_proc_entry("handlers", proc_bus_input_dir);
655 remove_proc_entry("input", proc_bus);
658 #else /* !CONFIG_PROC_FS */
659 static inline void input_wakeup_procfs_readers(void) { }
660 static inline int input_proc_init(void) { return 0; }
661 static inline void input_proc_exit(void) { }
662 #endif
664 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
665 static ssize_t input_dev_show_##name(struct device *dev, \
666 struct device_attribute *attr, \
667 char *buf) \
669 struct input_dev *input_dev = to_input_dev(dev); \
671 return scnprintf(buf, PAGE_SIZE, "%s\n", \
672 input_dev->name ? input_dev->name : ""); \
674 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
676 INPUT_DEV_STRING_ATTR_SHOW(name);
677 INPUT_DEV_STRING_ATTR_SHOW(phys);
678 INPUT_DEV_STRING_ATTR_SHOW(uniq);
680 static int input_print_modalias_bits(char *buf, int size,
681 char name, unsigned long *bm,
682 unsigned int min_bit, unsigned int max_bit)
684 int len = 0, i;
686 len += snprintf(buf, max(size, 0), "%c", name);
687 for (i = min_bit; i < max_bit; i++)
688 if (bm[LONG(i)] & BIT(i))
689 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
690 return len;
693 static int input_print_modalias(char *buf, int size, struct input_dev *id,
694 int add_cr)
696 int len;
698 len = snprintf(buf, max(size, 0),
699 "input:b%04Xv%04Xp%04Xe%04X-",
700 id->id.bustype, id->id.vendor,
701 id->id.product, id->id.version);
703 len += input_print_modalias_bits(buf + len, size - len,
704 'e', id->evbit, 0, EV_MAX);
705 len += input_print_modalias_bits(buf + len, size - len,
706 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
707 len += input_print_modalias_bits(buf + len, size - len,
708 'r', id->relbit, 0, REL_MAX);
709 len += input_print_modalias_bits(buf + len, size - len,
710 'a', id->absbit, 0, ABS_MAX);
711 len += input_print_modalias_bits(buf + len, size - len,
712 'm', id->mscbit, 0, MSC_MAX);
713 len += input_print_modalias_bits(buf + len, size - len,
714 'l', id->ledbit, 0, LED_MAX);
715 len += input_print_modalias_bits(buf + len, size - len,
716 's', id->sndbit, 0, SND_MAX);
717 len += input_print_modalias_bits(buf + len, size - len,
718 'f', id->ffbit, 0, FF_MAX);
719 len += input_print_modalias_bits(buf + len, size - len,
720 'w', id->swbit, 0, SW_MAX);
722 if (add_cr)
723 len += snprintf(buf + len, max(size - len, 0), "\n");
725 return len;
728 static ssize_t input_dev_show_modalias(struct device *dev,
729 struct device_attribute *attr,
730 char *buf)
732 struct input_dev *id = to_input_dev(dev);
733 ssize_t len;
735 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
737 return min_t(int, len, PAGE_SIZE);
739 static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
741 static struct attribute *input_dev_attrs[] = {
742 &dev_attr_name.attr,
743 &dev_attr_phys.attr,
744 &dev_attr_uniq.attr,
745 &dev_attr_modalias.attr,
746 NULL
749 static struct attribute_group input_dev_attr_group = {
750 .attrs = input_dev_attrs,
753 #define INPUT_DEV_ID_ATTR(name) \
754 static ssize_t input_dev_show_id_##name(struct device *dev, \
755 struct device_attribute *attr, \
756 char *buf) \
758 struct input_dev *input_dev = to_input_dev(dev); \
759 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
761 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
763 INPUT_DEV_ID_ATTR(bustype);
764 INPUT_DEV_ID_ATTR(vendor);
765 INPUT_DEV_ID_ATTR(product);
766 INPUT_DEV_ID_ATTR(version);
768 static struct attribute *input_dev_id_attrs[] = {
769 &dev_attr_bustype.attr,
770 &dev_attr_vendor.attr,
771 &dev_attr_product.attr,
772 &dev_attr_version.attr,
773 NULL
776 static struct attribute_group input_dev_id_attr_group = {
777 .name = "id",
778 .attrs = input_dev_id_attrs,
781 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
782 int max, int add_cr)
784 int i;
785 int len = 0;
787 for (i = NBITS(max) - 1; i > 0; i--)
788 if (bitmap[i])
789 break;
791 for (; i >= 0; i--)
792 len += snprintf(buf + len, max(buf_size - len, 0),
793 "%lx%s", bitmap[i], i > 0 ? " " : "");
795 if (add_cr)
796 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
798 return len;
801 #define INPUT_DEV_CAP_ATTR(ev, bm) \
802 static ssize_t input_dev_show_cap_##bm(struct device *dev, \
803 struct device_attribute *attr, \
804 char *buf) \
806 struct input_dev *input_dev = to_input_dev(dev); \
807 int len = input_print_bitmap(buf, PAGE_SIZE, \
808 input_dev->bm##bit, ev##_MAX, 1); \
809 return min_t(int, len, PAGE_SIZE); \
811 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
813 INPUT_DEV_CAP_ATTR(EV, ev);
814 INPUT_DEV_CAP_ATTR(KEY, key);
815 INPUT_DEV_CAP_ATTR(REL, rel);
816 INPUT_DEV_CAP_ATTR(ABS, abs);
817 INPUT_DEV_CAP_ATTR(MSC, msc);
818 INPUT_DEV_CAP_ATTR(LED, led);
819 INPUT_DEV_CAP_ATTR(SND, snd);
820 INPUT_DEV_CAP_ATTR(FF, ff);
821 INPUT_DEV_CAP_ATTR(SW, sw);
823 static struct attribute *input_dev_caps_attrs[] = {
824 &dev_attr_ev.attr,
825 &dev_attr_key.attr,
826 &dev_attr_rel.attr,
827 &dev_attr_abs.attr,
828 &dev_attr_msc.attr,
829 &dev_attr_led.attr,
830 &dev_attr_snd.attr,
831 &dev_attr_ff.attr,
832 &dev_attr_sw.attr,
833 NULL
836 static struct attribute_group input_dev_caps_attr_group = {
837 .name = "capabilities",
838 .attrs = input_dev_caps_attrs,
841 static struct attribute_group *input_dev_attr_groups[] = {
842 &input_dev_attr_group,
843 &input_dev_id_attr_group,
844 &input_dev_caps_attr_group,
845 NULL
848 static void input_dev_release(struct device *device)
850 struct input_dev *dev = to_input_dev(device);
852 input_ff_destroy(dev);
853 kfree(dev);
855 module_put(THIS_MODULE);
859 * Input uevent interface - loading event handlers based on
860 * device bitfields.
862 static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
863 char *buffer, int buffer_size, int *cur_len,
864 const char *name, unsigned long *bitmap, int max)
866 if (*cur_index >= num_envp - 1)
867 return -ENOMEM;
869 envp[*cur_index] = buffer + *cur_len;
871 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
872 if (*cur_len >= buffer_size)
873 return -ENOMEM;
875 *cur_len += input_print_bitmap(buffer + *cur_len,
876 max(buffer_size - *cur_len, 0),
877 bitmap, max, 0) + 1;
878 if (*cur_len > buffer_size)
879 return -ENOMEM;
881 (*cur_index)++;
882 return 0;
885 static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
886 char *buffer, int buffer_size, int *cur_len,
887 struct input_dev *dev)
889 if (*cur_index >= num_envp - 1)
890 return -ENOMEM;
892 envp[*cur_index] = buffer + *cur_len;
894 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
895 "MODALIAS=");
896 if (*cur_len >= buffer_size)
897 return -ENOMEM;
899 *cur_len += input_print_modalias(buffer + *cur_len,
900 max(buffer_size - *cur_len, 0),
901 dev, 0) + 1;
902 if (*cur_len > buffer_size)
903 return -ENOMEM;
905 (*cur_index)++;
906 return 0;
909 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
910 do { \
911 int err = add_uevent_var(envp, num_envp, &i, \
912 buffer, buffer_size, &len, \
913 fmt, val); \
914 if (err) \
915 return err; \
916 } while (0)
918 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
919 do { \
920 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
921 buffer, buffer_size, &len, \
922 name, bm, max); \
923 if (err) \
924 return err; \
925 } while (0)
927 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
928 do { \
929 int err = input_add_uevent_modalias_var(envp, \
930 num_envp, &i, \
931 buffer, buffer_size, &len, \
932 dev); \
933 if (err) \
934 return err; \
935 } while (0)
937 static int input_dev_uevent(struct device *device, char **envp,
938 int num_envp, char *buffer, int buffer_size)
940 struct input_dev *dev = to_input_dev(device);
941 int i = 0;
942 int len = 0;
944 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
945 dev->id.bustype, dev->id.vendor,
946 dev->id.product, dev->id.version);
947 if (dev->name)
948 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
949 if (dev->phys)
950 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
951 if (dev->uniq)
952 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
954 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
955 if (test_bit(EV_KEY, dev->evbit))
956 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
957 if (test_bit(EV_REL, dev->evbit))
958 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
959 if (test_bit(EV_ABS, dev->evbit))
960 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
961 if (test_bit(EV_MSC, dev->evbit))
962 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
963 if (test_bit(EV_LED, dev->evbit))
964 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
965 if (test_bit(EV_SND, dev->evbit))
966 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
967 if (test_bit(EV_FF, dev->evbit))
968 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
969 if (test_bit(EV_SW, dev->evbit))
970 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
972 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
974 envp[i] = NULL;
975 return 0;
978 static struct device_type input_dev_type = {
979 .groups = input_dev_attr_groups,
980 .release = input_dev_release,
981 .uevent = input_dev_uevent,
984 struct class input_class = {
985 .name = "input",
987 EXPORT_SYMBOL_GPL(input_class);
990 * input_allocate_device - allocate memory for new input device
992 * Returns prepared struct input_dev or NULL.
994 * NOTE: Use input_free_device() to free devices that have not been
995 * registered; input_unregister_device() should be used for already
996 * registered devices.
998 struct input_dev *input_allocate_device(void)
1000 struct input_dev *dev;
1002 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1003 if (dev) {
1004 dev->dev.type = &input_dev_type;
1005 dev->dev.class = &input_class;
1006 device_initialize(&dev->dev);
1007 mutex_init(&dev->mutex);
1008 INIT_LIST_HEAD(&dev->h_list);
1009 INIT_LIST_HEAD(&dev->node);
1011 __module_get(THIS_MODULE);
1014 return dev;
1016 EXPORT_SYMBOL(input_allocate_device);
1019 * input_free_device - free memory occupied by input_dev structure
1020 * @dev: input device to free
1022 * This function should only be used if input_register_device()
1023 * was not called yet or if it failed. Once device was registered
1024 * use input_unregister_device() and memory will be freed once last
1025 * refrence to the device is dropped.
1027 * Device should be allocated by input_allocate_device().
1029 * NOTE: If there are references to the input device then memory
1030 * will not be freed until last reference is dropped.
1032 void input_free_device(struct input_dev *dev)
1034 if (dev)
1035 input_put_device(dev);
1037 EXPORT_SYMBOL(input_free_device);
1040 * input_set_capability - mark device as capable of a certain event
1041 * @dev: device that is capable of emitting or accepting event
1042 * @type: type of the event (EV_KEY, EV_REL, etc...)
1043 * @code: event code
1045 * In addition to setting up corresponding bit in appropriate capability
1046 * bitmap the function also adjusts dev->evbit.
1048 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1050 switch (type) {
1051 case EV_KEY:
1052 __set_bit(code, dev->keybit);
1053 break;
1055 case EV_REL:
1056 __set_bit(code, dev->relbit);
1057 break;
1059 case EV_ABS:
1060 __set_bit(code, dev->absbit);
1061 break;
1063 case EV_MSC:
1064 __set_bit(code, dev->mscbit);
1065 break;
1067 case EV_SW:
1068 __set_bit(code, dev->swbit);
1069 break;
1071 case EV_LED:
1072 __set_bit(code, dev->ledbit);
1073 break;
1075 case EV_SND:
1076 __set_bit(code, dev->sndbit);
1077 break;
1079 case EV_FF:
1080 __set_bit(code, dev->ffbit);
1081 break;
1083 default:
1084 printk(KERN_ERR
1085 "input_set_capability: unknown type %u (code %u)\n",
1086 type, code);
1087 dump_stack();
1088 return;
1091 __set_bit(type, dev->evbit);
1093 EXPORT_SYMBOL(input_set_capability);
1095 int input_register_device(struct input_dev *dev)
1097 static atomic_t input_no = ATOMIC_INIT(0);
1098 struct input_handler *handler;
1099 const char *path;
1100 int error;
1102 set_bit(EV_SYN, dev->evbit);
1105 * If delay and period are pre-set by the driver, then autorepeating
1106 * is handled by the driver itself and we don't do it in input.c.
1109 init_timer(&dev->timer);
1110 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1111 dev->timer.data = (long) dev;
1112 dev->timer.function = input_repeat_key;
1113 dev->rep[REP_DELAY] = 250;
1114 dev->rep[REP_PERIOD] = 33;
1117 if (!dev->getkeycode)
1118 dev->getkeycode = input_default_getkeycode;
1120 if (!dev->setkeycode)
1121 dev->setkeycode = input_default_setkeycode;
1123 list_add_tail(&dev->node, &input_dev_list);
1125 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
1126 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
1128 if (dev->cdev.dev)
1129 dev->dev.parent = dev->cdev.dev;
1131 error = device_add(&dev->dev);
1132 if (error)
1133 return error;
1135 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1136 printk(KERN_INFO "input: %s as %s\n",
1137 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1138 kfree(path);
1140 list_for_each_entry(handler, &input_handler_list, node)
1141 input_attach_handler(dev, handler);
1143 input_wakeup_procfs_readers();
1145 return 0;
1147 EXPORT_SYMBOL(input_register_device);
1149 void input_unregister_device(struct input_dev *dev)
1151 struct input_handle *handle, *next;
1152 int code;
1154 for (code = 0; code <= KEY_MAX; code++)
1155 if (test_bit(code, dev->key))
1156 input_report_key(dev, code, 0);
1157 input_sync(dev);
1159 del_timer_sync(&dev->timer);
1161 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1162 handle->handler->disconnect(handle);
1163 WARN_ON(!list_empty(&dev->h_list));
1165 list_del_init(&dev->node);
1167 device_unregister(&dev->dev);
1169 input_wakeup_procfs_readers();
1171 EXPORT_SYMBOL(input_unregister_device);
1173 int input_register_handler(struct input_handler *handler)
1175 struct input_dev *dev;
1177 INIT_LIST_HEAD(&handler->h_list);
1179 if (handler->fops != NULL) {
1180 if (input_table[handler->minor >> 5])
1181 return -EBUSY;
1183 input_table[handler->minor >> 5] = handler;
1186 list_add_tail(&handler->node, &input_handler_list);
1188 list_for_each_entry(dev, &input_dev_list, node)
1189 input_attach_handler(dev, handler);
1191 input_wakeup_procfs_readers();
1192 return 0;
1194 EXPORT_SYMBOL(input_register_handler);
1196 void input_unregister_handler(struct input_handler *handler)
1198 struct input_handle *handle, *next;
1200 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1201 handler->disconnect(handle);
1202 WARN_ON(!list_empty(&handler->h_list));
1204 list_del_init(&handler->node);
1206 if (handler->fops != NULL)
1207 input_table[handler->minor >> 5] = NULL;
1209 input_wakeup_procfs_readers();
1211 EXPORT_SYMBOL(input_unregister_handler);
1213 int input_register_handle(struct input_handle *handle)
1215 struct input_handler *handler = handle->handler;
1217 list_add_tail(&handle->d_node, &handle->dev->h_list);
1218 list_add_tail(&handle->h_node, &handler->h_list);
1220 if (handler->start)
1221 handler->start(handle);
1223 return 0;
1225 EXPORT_SYMBOL(input_register_handle);
1227 void input_unregister_handle(struct input_handle *handle)
1229 list_del_init(&handle->h_node);
1230 list_del_init(&handle->d_node);
1232 EXPORT_SYMBOL(input_unregister_handle);
1234 static int input_open_file(struct inode *inode, struct file *file)
1236 struct input_handler *handler = input_table[iminor(inode) >> 5];
1237 const struct file_operations *old_fops, *new_fops = NULL;
1238 int err;
1240 /* No load-on-demand here? */
1241 if (!handler || !(new_fops = fops_get(handler->fops)))
1242 return -ENODEV;
1245 * That's _really_ odd. Usually NULL ->open means "nothing special",
1246 * not "no device". Oh, well...
1248 if (!new_fops->open) {
1249 fops_put(new_fops);
1250 return -ENODEV;
1252 old_fops = file->f_op;
1253 file->f_op = new_fops;
1255 err = new_fops->open(inode, file);
1257 if (err) {
1258 fops_put(file->f_op);
1259 file->f_op = fops_get(old_fops);
1261 fops_put(old_fops);
1262 return err;
1265 static const struct file_operations input_fops = {
1266 .owner = THIS_MODULE,
1267 .open = input_open_file,
1270 static int __init input_init(void)
1272 int err;
1274 err = class_register(&input_class);
1275 if (err) {
1276 printk(KERN_ERR "input: unable to register input_dev class\n");
1277 return err;
1280 err = input_proc_init();
1281 if (err)
1282 goto fail1;
1284 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1285 if (err) {
1286 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1287 goto fail2;
1290 return 0;
1292 fail2: input_proc_exit();
1293 fail1: class_unregister(&input_class);
1294 return err;
1297 static void __exit input_exit(void)
1299 input_proc_exit();
1300 unregister_chrdev(INPUT_MAJOR, "input");
1301 class_unregister(&input_class);
1304 subsys_initcall(input_init);
1305 module_exit(input_exit);