[PATCH] INPUT: Fix oops when accessing sysfs files of nested input devices
[linux-2.6-block.git] / drivers / input / input.c
CommitLineData
1da177e4
LT
1/*
2 * The input core
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 */
6
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.
11 */
12
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/kobject_uevent.h>
22#include <linux/interrupt.h>
23#include <linux/poll.h>
24#include <linux/device.h>
1da177e4
LT
25
26MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
27MODULE_DESCRIPTION("Input core");
28MODULE_LICENSE("GPL");
29
d19fbe8a 30EXPORT_SYMBOL(input_allocate_device);
1da177e4
LT
31EXPORT_SYMBOL(input_register_device);
32EXPORT_SYMBOL(input_unregister_device);
33EXPORT_SYMBOL(input_register_handler);
34EXPORT_SYMBOL(input_unregister_handler);
35EXPORT_SYMBOL(input_grab_device);
36EXPORT_SYMBOL(input_release_device);
37EXPORT_SYMBOL(input_open_device);
38EXPORT_SYMBOL(input_close_device);
39EXPORT_SYMBOL(input_accept_process);
40EXPORT_SYMBOL(input_flush_device);
41EXPORT_SYMBOL(input_event);
42EXPORT_SYMBOL(input_class);
23d50901 43EXPORT_SYMBOL_GPL(input_dev_class);
1da177e4
LT
44
45#define INPUT_DEVICES 256
46
47static LIST_HEAD(input_dev_list);
48static LIST_HEAD(input_handler_list);
49
50static struct input_handler *input_table[8];
51
1da177e4
LT
52void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
53{
54 struct input_handle *handle;
55
56 if (type > EV_MAX || !test_bit(type, dev->evbit))
57 return;
58
59 add_input_randomness(type, code, value);
60
61 switch (type) {
62
63 case EV_SYN:
64 switch (code) {
65 case SYN_CONFIG:
66 if (dev->event) dev->event(dev, type, code, value);
67 break;
68
69 case SYN_REPORT:
70 if (dev->sync) return;
71 dev->sync = 1;
72 break;
73 }
74 break;
75
76 case EV_KEY:
77
78 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
79 return;
80
81 if (value == 2)
82 break;
83
84 change_bit(code, dev->key);
85
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]));
89 }
90
91 break;
92
31581066
RP
93 case EV_SW:
94
95 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
96 return;
97
98 change_bit(code, dev->sw);
99
100 break;
101
1da177e4
LT
102 case EV_ABS:
103
104 if (code > ABS_MAX || !test_bit(code, dev->absbit))
105 return;
106
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;
111
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;
115
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;
119 }
120
121 if (dev->abs[code] == value)
122 return;
123
124 dev->abs[code] = value;
125 break;
126
127 case EV_REL:
128
129 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
130 return;
131
132 break;
133
134 case EV_MSC:
135
136 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
137 return;
138
139 if (dev->event) dev->event(dev, type, code, value);
140
141 break;
142
143 case EV_LED:
144
145 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
146 return;
147
148 change_bit(code, dev->led);
149 if (dev->event) dev->event(dev, type, code, value);
150
151 break;
152
153 case EV_SND:
154
155 if (code > SND_MAX || !test_bit(code, dev->sndbit))
156 return;
157
158 if (dev->event) dev->event(dev, type, code, value);
159
160 break;
161
162 case EV_REP:
163
164 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
165
166 dev->rep[code] = value;
167 if (dev->event) dev->event(dev, type, code, value);
168
169 break;
170
171 case EV_FF:
172 if (dev->event) dev->event(dev, type, code, value);
173 break;
174 }
175
176 if (type != EV_SYN)
177 dev->sync = 0;
178
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);
185}
186
187static void input_repeat_key(unsigned long data)
188{
189 struct input_dev *dev = (void *) data;
190
191 if (!test_bit(dev->repeat_key, dev->key))
192 return;
193
194 input_event(dev, EV_KEY, dev->repeat_key, 2);
195 input_sync(dev);
196
197 if (dev->rep[REP_PERIOD])
198 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
199}
200
201int input_accept_process(struct input_handle *handle, struct file *file)
202{
203 if (handle->dev->accept)
204 return handle->dev->accept(handle->dev, file);
205
206 return 0;
207}
208
209int input_grab_device(struct input_handle *handle)
210{
211 if (handle->dev->grab)
212 return -EBUSY;
213
214 handle->dev->grab = handle;
215 return 0;
216}
217
218void input_release_device(struct input_handle *handle)
219{
220 if (handle->dev->grab == handle)
221 handle->dev->grab = NULL;
222}
223
224int input_open_device(struct input_handle *handle)
225{
0fbf87ca
DT
226 struct input_dev *dev = handle->dev;
227 int err;
228
229 err = down_interruptible(&dev->sem);
230 if (err)
231 return err;
232
1da177e4 233 handle->open++;
0fbf87ca
DT
234
235 if (!dev->users++ && dev->open)
236 err = dev->open(dev);
237
238 if (err)
239 handle->open--;
240
241 up(&dev->sem);
242
243 return err;
1da177e4
LT
244}
245
246int input_flush_device(struct input_handle* handle, struct file* file)
247{
248 if (handle->dev->flush)
249 return handle->dev->flush(handle->dev, file);
250
251 return 0;
252}
253
254void input_close_device(struct input_handle *handle)
255{
0fbf87ca
DT
256 struct input_dev *dev = handle->dev;
257
1da177e4 258 input_release_device(handle);
0fbf87ca
DT
259
260 down(&dev->sem);
261
262 if (!--dev->users && dev->close)
263 dev->close(dev);
1da177e4 264 handle->open--;
0fbf87ca
DT
265
266 up(&dev->sem);
1da177e4
LT
267}
268
269static void input_link_handle(struct input_handle *handle)
270{
271 list_add_tail(&handle->d_node, &handle->dev->h_list);
272 list_add_tail(&handle->h_node, &handle->handler->h_list);
273}
274
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;
281
282static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
283{
284 int i;
285
286 for (; id->flags || id->driver_info; id++) {
287
288 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
289 if (id->id.bustype != dev->id.bustype)
290 continue;
291
292 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
293 if (id->id.vendor != dev->id.vendor)
294 continue;
295
296 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
297 if (id->id.product != dev->id.product)
298 continue;
299
300 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
301 if (id->id.version != dev->id.version)
302 continue;
303
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);
ff13f98b 312 MATCH_BIT(swbit, SW_MAX);
1da177e4
LT
313
314 return id;
315 }
316
317 return NULL;
318}
319
f96b434d 320
1da177e4
LT
321/*
322 * Input hotplugging interface - loading event handlers based on
323 * device bitfields.
324 */
325
326#ifdef CONFIG_HOTPLUG
327
328/*
329 * Input hotplugging invokes what /proc/sys/kernel/hotplug says
330 * (normally /sbin/hotplug) when input devices get added or removed.
331 *
332 * This invokes a user mode policy agent, typically helping to load driver
333 * or other modules, configure the device, and more. Drivers can provide
334 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
335 *
336 */
337
338#define SPRINTF_BIT_A(bit, name, max) \
339 do { \
340 envp[i++] = scratch; \
341 scratch += sprintf(scratch, name); \
342 for (j = NBITS(max) - 1; j >= 0; j--) \
343 if (dev->bit[j]) break; \
344 for (; j >= 0; j--) \
345 scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
346 scratch++; \
347 } while (0)
348
349#define SPRINTF_BIT_A2(bit, name, max, ev) \
350 do { \
351 if (test_bit(ev, dev->evbit)) \
352 SPRINTF_BIT_A(bit, name, max); \
353 } while (0)
354
355static void input_call_hotplug(char *verb, struct input_dev *dev)
356{
357 char *argv[3], **envp, *buf, *scratch;
358 int i = 0, j, value;
359
360 if (!hotplug_path[0]) {
361 printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
362 return;
363 }
364 if (in_interrupt()) {
365 printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
366 return;
367 }
368 if (!current->fs->root) {
369 printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
370 return;
371 }
372 if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
373 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
374 return;
375 }
376 if (!(buf = kmalloc(1024, GFP_KERNEL))) {
377 kfree (envp);
378 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
379 return;
380 }
381
382 argv[0] = hotplug_path;
383 argv[1] = "input";
384 argv[2] = NULL;
385
386 envp[i++] = "HOME=/";
387 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
388
389 scratch = buf;
390
391 envp[i++] = scratch;
392 scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
393
394 envp[i++] = scratch;
395 scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
396 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
397
398 if (dev->name) {
399 envp[i++] = scratch;
400 scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
401 }
402
403 if (dev->phys) {
404 envp[i++] = scratch;
405 scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1;
406 }
407
408 SPRINTF_BIT_A(evbit, "EV=", EV_MAX);
409 SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY);
410 SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL);
411 SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS);
412 SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC);
413 SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED);
414 SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND);
415 SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF);
31581066 416 SPRINTF_BIT_A2(swbit, "SW=", SW_MAX, EV_SW);
1da177e4
LT
417
418 envp[i++] = NULL;
419
420#ifdef INPUT_DEBUG
421 printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n",
422 argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]);
423#endif
424
425 value = call_usermodehelper(argv [0], argv, envp, 0);
426
427 kfree(buf);
428 kfree(envp);
429
430#ifdef INPUT_DEBUG
431 if (value != 0)
432 printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
433#endif
434}
435
436#endif
437
5c1e9a6a
DT
438static int input_print_bitmap(char *buf, unsigned long *bitmap, int max)
439{
440 int i;
441 int len = 0;
442
443 for (i = NBITS(max) - 1; i > 0; i--)
444 if (bitmap[i])
445 break;
446
447 for (; i >= 0; i--)
448 len += sprintf(buf + len, "%lx%s", bitmap[i], i > 0 ? " " : "");
449
450 len += sprintf(buf + len, "\n");
451
452 return len;
453}
454
f96b434d
DT
455#ifdef CONFIG_PROC_FS
456
457static struct proc_dir_entry *proc_bus_input_dir;
458static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
459static int input_devices_state;
460
461static inline void input_wakeup_procfs_readers(void)
462{
463 input_devices_state++;
464 wake_up(&input_devices_poll_wait);
465}
466
467static unsigned int input_devices_poll(struct file *file, poll_table *wait)
468{
469 int state = input_devices_state;
470 poll_wait(file, &input_devices_poll_wait, wait);
471 if (state != input_devices_state)
472 return POLLIN | POLLRDNORM;
473 return 0;
474}
475
5c1e9a6a
DT
476#define SPRINTF_BIT_B(ev, bm) \
477 do { \
478 len += sprintf(buf + len, "B: %s=", #ev); \
479 len += input_print_bitmap(buf + len, \
480 dev->bm##bit, ev##_MAX); \
f96b434d
DT
481 } while (0)
482
5c1e9a6a
DT
483#define SPRINTF_BIT_B2(ev, bm) \
484 do { \
485 if (test_bit(EV_##ev, dev->evbit)) \
486 SPRINTF_BIT_B(ev, bm); \
f96b434d
DT
487 } while (0)
488
489static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
490{
491 struct input_dev *dev;
492 struct input_handle *handle;
051b2fea 493 const char *path;
f96b434d
DT
494
495 off_t at = 0;
5c1e9a6a 496 int len, cnt = 0;
f96b434d
DT
497
498 list_for_each_entry(dev, &input_dev_list, node) {
499
051b2fea
DT
500 path = dev->dynalloc ? kobject_get_path(&dev->cdev.kobj, GFP_KERNEL) : NULL;
501
f96b434d
DT
502 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
503 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
504
505 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
506 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
051b2fea 507 len += sprintf(buf + len, "S: Sysfs=%s\n", path ? path : "");
f96b434d
DT
508 len += sprintf(buf + len, "H: Handlers=");
509
510 list_for_each_entry(handle, &dev->h_list, d_node)
511 len += sprintf(buf + len, "%s ", handle->name);
512
513 len += sprintf(buf + len, "\n");
514
5c1e9a6a
DT
515 SPRINTF_BIT_B(EV, ev);
516 SPRINTF_BIT_B2(KEY, key);
517 SPRINTF_BIT_B2(REL, rel);
518 SPRINTF_BIT_B2(ABS, abs);
519 SPRINTF_BIT_B2(MSC, msc);
520 SPRINTF_BIT_B2(LED, led);
521 SPRINTF_BIT_B2(SND, snd);
522 SPRINTF_BIT_B2(FF, ff);
523 SPRINTF_BIT_B2(SW, sw);
f96b434d
DT
524
525 len += sprintf(buf + len, "\n");
526
527 at += len;
528
529 if (at >= pos) {
530 if (!*start) {
531 *start = buf + (pos - (at - len));
532 cnt = at - pos;
533 } else cnt += len;
534 buf += len;
535 if (cnt >= count)
536 break;
537 }
051b2fea
DT
538
539 kfree(path);
f96b434d
DT
540 }
541
542 if (&dev->node == &input_dev_list)
543 *eof = 1;
544
545 return (count > cnt) ? cnt : count;
546}
547
548static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
549{
550 struct input_handler *handler;
551
552 off_t at = 0;
553 int len = 0, cnt = 0;
554 int i = 0;
555
556 list_for_each_entry(handler, &input_handler_list, node) {
557
558 if (handler->fops)
559 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
560 i++, handler->name, handler->minor);
561 else
562 len = sprintf(buf, "N: Number=%d Name=%s\n",
563 i++, handler->name);
564
565 at += len;
566
567 if (at >= pos) {
568 if (!*start) {
569 *start = buf + (pos - (at - len));
570 cnt = at - pos;
571 } else cnt += len;
572 buf += len;
573 if (cnt >= count)
574 break;
575 }
576 }
577 if (&handler->node == &input_handler_list)
578 *eof = 1;
579
580 return (count > cnt) ? cnt : count;
581}
582
583static struct file_operations input_fileops;
584
585static int __init input_proc_init(void)
586{
587 struct proc_dir_entry *entry;
588
589 proc_bus_input_dir = proc_mkdir("input", proc_bus);
590 if (!proc_bus_input_dir)
591 return -ENOMEM;
592
593 proc_bus_input_dir->owner = THIS_MODULE;
594
595 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
596 if (!entry)
597 goto fail1;
598
599 entry->owner = THIS_MODULE;
600 input_fileops = *entry->proc_fops;
601 entry->proc_fops = &input_fileops;
602 entry->proc_fops->poll = input_devices_poll;
603
604 entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
605 if (!entry)
606 goto fail2;
607
608 entry->owner = THIS_MODULE;
609
610 return 0;
611
612 fail2: remove_proc_entry("devices", proc_bus_input_dir);
613 fail1: remove_proc_entry("input", proc_bus);
614 return -ENOMEM;
615}
616
beffbdc2 617static void input_proc_exit(void)
f96b434d
DT
618{
619 remove_proc_entry("devices", proc_bus_input_dir);
620 remove_proc_entry("handlers", proc_bus_input_dir);
621 remove_proc_entry("input", proc_bus);
622}
623
624#else /* !CONFIG_PROC_FS */
625static inline void input_wakeup_procfs_readers(void) { }
626static inline int input_proc_init(void) { return 0; }
627static inline void input_proc_exit(void) { }
628#endif
629
5c1e9a6a
DT
630#define INPUT_DEV_STRING_ATTR_SHOW(name) \
631static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
632{ \
633 struct input_dev *input_dev = to_input_dev(dev); \
634 int retval; \
635 \
636 retval = down_interruptible(&input_dev->sem); \
637 if (retval) \
638 return retval; \
639 \
640 retval = sprintf(buf, "%s\n", input_dev->name ? input_dev->name : ""); \
641 \
642 up(&input_dev->sem); \
643 \
644 return retval; \
629b77a4
GKH
645} \
646static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
5c1e9a6a
DT
647
648INPUT_DEV_STRING_ATTR_SHOW(name);
649INPUT_DEV_STRING_ATTR_SHOW(phys);
650INPUT_DEV_STRING_ATTR_SHOW(uniq);
651
629b77a4
GKH
652static struct attribute *input_dev_attrs[] = {
653 &class_device_attr_name.attr,
654 &class_device_attr_phys.attr,
655 &class_device_attr_uniq.attr,
656 NULL
657};
658
659static struct attribute_group input_dev_group = {
660 .attrs = input_dev_attrs,
5c1e9a6a
DT
661};
662
663#define INPUT_DEV_ID_ATTR(name) \
664static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
665{ \
666 struct input_dev *input_dev = to_input_dev(dev); \
667 return sprintf(buf, "%04x\n", input_dev->id.name); \
668} \
669static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
670
671INPUT_DEV_ID_ATTR(bustype);
672INPUT_DEV_ID_ATTR(vendor);
673INPUT_DEV_ID_ATTR(product);
674INPUT_DEV_ID_ATTR(version);
675
676static struct attribute *input_dev_id_attrs[] = {
677 &class_device_attr_bustype.attr,
678 &class_device_attr_vendor.attr,
679 &class_device_attr_product.attr,
680 &class_device_attr_version.attr,
681 NULL
682};
683
684static struct attribute_group input_dev_id_attr_group = {
685 .name = "id",
686 .attrs = input_dev_id_attrs,
687};
688
689#define INPUT_DEV_CAP_ATTR(ev, bm) \
690static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
691{ \
692 struct input_dev *input_dev = to_input_dev(dev); \
693 return input_print_bitmap(buf, input_dev->bm##bit, ev##_MAX); \
694} \
695static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
696
697INPUT_DEV_CAP_ATTR(EV, ev);
698INPUT_DEV_CAP_ATTR(KEY, key);
699INPUT_DEV_CAP_ATTR(REL, rel);
700INPUT_DEV_CAP_ATTR(ABS, abs);
701INPUT_DEV_CAP_ATTR(MSC, msc);
702INPUT_DEV_CAP_ATTR(LED, led);
703INPUT_DEV_CAP_ATTR(SND, snd);
704INPUT_DEV_CAP_ATTR(FF, ff);
705INPUT_DEV_CAP_ATTR(SW, sw);
706
707static struct attribute *input_dev_caps_attrs[] = {
708 &class_device_attr_ev.attr,
709 &class_device_attr_key.attr,
710 &class_device_attr_rel.attr,
711 &class_device_attr_abs.attr,
712 &class_device_attr_msc.attr,
713 &class_device_attr_led.attr,
714 &class_device_attr_snd.attr,
715 &class_device_attr_ff.attr,
716 &class_device_attr_sw.attr,
717 NULL
718};
719
720static struct attribute_group input_dev_caps_attr_group = {
721 .name = "capabilities",
722 .attrs = input_dev_caps_attrs,
723};
724
d19fbe8a
DT
725static void input_dev_release(struct class_device *class_dev)
726{
727 struct input_dev *dev = to_input_dev(class_dev);
728
729 kfree(dev);
730 module_put(THIS_MODULE);
731}
732
23d50901 733struct class input_dev_class = {
d19fbe8a
DT
734 .name = "input_dev",
735 .release = input_dev_release,
736};
737
738struct input_dev *input_allocate_device(void)
739{
740 struct input_dev *dev;
741
742 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
743 if (dev) {
744 dev->dynalloc = 1;
745 dev->cdev.class = &input_dev_class;
746 class_device_initialize(&dev->cdev);
747 INIT_LIST_HEAD(&dev->h_list);
748 INIT_LIST_HEAD(&dev->node);
749 }
750
751 return dev;
752}
753
754static void input_register_classdevice(struct input_dev *dev)
755{
756 static atomic_t input_no = ATOMIC_INIT(0);
757 const char *path;
758
759 __module_get(THIS_MODULE);
760
761 dev->dev = dev->cdev.dev;
762
763 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
764 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
765
766 path = kobject_get_path(&dev->cdev.class->subsys.kset.kobj, GFP_KERNEL);
767 printk(KERN_INFO "input: %s/%s as %s\n",
768 dev->name ? dev->name : "Unspecified device",
769 path ? path : "", dev->cdev.class_id);
770 kfree(path);
771
772 class_device_add(&dev->cdev);
629b77a4 773 sysfs_create_group(&dev->cdev.kobj, &input_dev_group);
5c1e9a6a
DT
774 sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
775 sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
d19fbe8a
DT
776}
777
1da177e4
LT
778void input_register_device(struct input_dev *dev)
779{
780 struct input_handle *handle;
781 struct input_handler *handler;
782 struct input_device_id *id;
783
784 set_bit(EV_SYN, dev->evbit);
785
0fbf87ca
DT
786 init_MUTEX(&dev->sem);
787
1da177e4
LT
788 /*
789 * If delay and period are pre-set by the driver, then autorepeating
790 * is handled by the driver itself and we don't do it in input.c.
791 */
792
793 init_timer(&dev->timer);
794 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
795 dev->timer.data = (long) dev;
796 dev->timer.function = input_repeat_key;
797 dev->rep[REP_DELAY] = 250;
798 dev->rep[REP_PERIOD] = 33;
799 }
800
801 INIT_LIST_HEAD(&dev->h_list);
802 list_add_tail(&dev->node, &input_dev_list);
803
10204020
GKH
804 if (dev->dynalloc)
805 input_register_classdevice(dev);
806
1da177e4
LT
807 list_for_each_entry(handler, &input_handler_list, node)
808 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
809 if ((id = input_match_device(handler->id_table, dev)))
810 if ((handle = handler->connect(handler, dev, id)))
811 input_link_handle(handle);
812
d19fbe8a 813
1da177e4
LT
814#ifdef CONFIG_HOTPLUG
815 input_call_hotplug("add", dev);
816#endif
817
f96b434d 818 input_wakeup_procfs_readers();
1da177e4
LT
819}
820
821void input_unregister_device(struct input_dev *dev)
822{
823 struct list_head * node, * next;
824
825 if (!dev) return;
826
827 del_timer_sync(&dev->timer);
828
829 list_for_each_safe(node, next, &dev->h_list) {
830 struct input_handle * handle = to_handle(node);
831 list_del_init(&handle->d_node);
832 list_del_init(&handle->h_node);
833 handle->handler->disconnect(handle);
834 }
835
836#ifdef CONFIG_HOTPLUG
837 input_call_hotplug("remove", dev);
838#endif
839
840 list_del_init(&dev->node);
841
5c1e9a6a
DT
842 if (dev->dynalloc) {
843 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
844 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
d19fbe8a 845 class_device_unregister(&dev->cdev);
5c1e9a6a 846 }
d19fbe8a 847
f96b434d 848 input_wakeup_procfs_readers();
1da177e4
LT
849}
850
851void input_register_handler(struct input_handler *handler)
852{
853 struct input_dev *dev;
854 struct input_handle *handle;
855 struct input_device_id *id;
856
857 if (!handler) return;
858
859 INIT_LIST_HEAD(&handler->h_list);
860
861 if (handler->fops != NULL)
862 input_table[handler->minor >> 5] = handler;
863
864 list_add_tail(&handler->node, &input_handler_list);
865
866 list_for_each_entry(dev, &input_dev_list, node)
867 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
868 if ((id = input_match_device(handler->id_table, dev)))
869 if ((handle = handler->connect(handler, dev, id)))
870 input_link_handle(handle);
871
f96b434d 872 input_wakeup_procfs_readers();
1da177e4
LT
873}
874
875void input_unregister_handler(struct input_handler *handler)
876{
877 struct list_head * node, * next;
878
879 list_for_each_safe(node, next, &handler->h_list) {
880 struct input_handle * handle = to_handle_h(node);
881 list_del_init(&handle->h_node);
882 list_del_init(&handle->d_node);
883 handler->disconnect(handle);
884 }
885
886 list_del_init(&handler->node);
887
888 if (handler->fops != NULL)
889 input_table[handler->minor >> 5] = NULL;
890
f96b434d 891 input_wakeup_procfs_readers();
1da177e4
LT
892}
893
894static int input_open_file(struct inode *inode, struct file *file)
895{
896 struct input_handler *handler = input_table[iminor(inode) >> 5];
897 struct file_operations *old_fops, *new_fops = NULL;
898 int err;
899
900 /* No load-on-demand here? */
901 if (!handler || !(new_fops = fops_get(handler->fops)))
902 return -ENODEV;
903
904 /*
905 * That's _really_ odd. Usually NULL ->open means "nothing special",
906 * not "no device". Oh, well...
907 */
908 if (!new_fops->open) {
909 fops_put(new_fops);
910 return -ENODEV;
911 }
912 old_fops = file->f_op;
913 file->f_op = new_fops;
914
915 err = new_fops->open(inode, file);
916
917 if (err) {
918 fops_put(file->f_op);
919 file->f_op = fops_get(old_fops);
920 }
921 fops_put(old_fops);
922 return err;
923}
924
925static struct file_operations input_fops = {
926 .owner = THIS_MODULE,
927 .open = input_open_file,
928};
929
f96b434d 930struct class *input_class;
1da177e4 931
f96b434d 932static int __init input_init(void)
1da177e4 933{
f96b434d 934 int err;
1da177e4 935
d19fbe8a
DT
936 err = class_register(&input_dev_class);
937 if (err) {
938 printk(KERN_ERR "input: unable to register input_dev class\n");
939 return err;
940 }
941
f96b434d
DT
942 input_class = class_create(THIS_MODULE, "input");
943 if (IS_ERR(input_class)) {
944 printk(KERN_ERR "input: unable to register input class\n");
d19fbe8a
DT
945 err = PTR_ERR(input_class);
946 goto fail1;
1da177e4
LT
947 }
948
f96b434d
DT
949 err = input_proc_init();
950 if (err)
d19fbe8a 951 goto fail2;
1da177e4 952
f96b434d
DT
953 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
954 if (err) {
955 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
d19fbe8a 956 goto fail3;
1da177e4 957 }
e334016f 958
1da177e4 959 return 0;
1da177e4 960
d19fbe8a
DT
961 fail3: input_proc_exit();
962 fail2: class_destroy(input_class);
963 fail1: class_unregister(&input_dev_class);
f96b434d 964 return err;
1da177e4
LT
965}
966
967static void __exit input_exit(void)
968{
f96b434d 969 input_proc_exit();
1da177e4 970 unregister_chrdev(INPUT_MAJOR, "input");
1235686f 971 class_destroy(input_class);
d19fbe8a 972 class_unregister(&input_dev_class);
1da177e4
LT
973}
974
975subsys_initcall(input_init);
976module_exit(input_exit);