patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / input.c
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/pm.h>
21 #include <linux/proc_fs.h>
22 #include <linux/kmod.h>
23 #include <linux/interrupt.h>
24 #include <linux/poll.h>
25 #include <linux/device.h>
26 #include <linux/devfs_fs_kernel.h>
27
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
29 MODULE_DESCRIPTION("Input core");
30 MODULE_LICENSE("GPL");
31
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(input_class);
44
45 #define INPUT_DEVICES   256
46
47 static LIST_HEAD(input_dev_list);
48 static LIST_HEAD(input_handler_list);
49
50 static struct input_handler *input_table[8];
51
52 #ifdef CONFIG_PROC_FS
53 static struct proc_dir_entry *proc_bus_input_dir;
54 DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
55 static int input_devices_state;
56 #endif
57
58 static inline unsigned int ms_to_jiffies(unsigned int ms)
59 {
60         unsigned int j;
61         j = (ms * HZ + 500) / 1000;
62         return (j > 0) ? j : 1;
63 }
64
65
66 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
67 {
68         struct input_handle *handle;
69
70         if (dev->pm_dev)
71                 pm_access(dev->pm_dev);
72
73         if (type > EV_MAX || !test_bit(type, dev->evbit))
74                 return;
75
76         add_mouse_randomness((type << 4) ^ code ^ (code >> 4) ^ value);
77
78         switch (type) {
79
80                 case EV_SYN:
81                         switch (code) {
82                                 case SYN_CONFIG:
83                                         if (dev->event) dev->event(dev, type, code, value);
84                                         break;
85
86                                 case SYN_REPORT:
87                                         if (dev->sync) return;
88                                         dev->sync = 1;
89                                         break;
90                         }
91                         break;
92
93                 case EV_KEY:
94
95                         if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
96                                 return;
97
98                         if (value == 2)
99                                 break;
100
101                         change_bit(code, dev->key);
102
103                         if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->timer.data && value) {
104                                 dev->repeat_key = code;
105                                 mod_timer(&dev->timer, jiffies + ms_to_jiffies(dev->rep[REP_DELAY]));
106                         }
107
108                         break;
109
110                 case EV_ABS:
111
112                         if (code > ABS_MAX || !test_bit(code, dev->absbit))
113                                 return;
114
115                         if (dev->absfuzz[code]) {
116                                 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
117                                     (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
118                                         return;
119
120                                 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
121                                     (value < dev->abs[code] + dev->absfuzz[code]))
122                                         value = (dev->abs[code] * 3 + value) >> 2;
123
124                                 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
125                                     (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
126                                         value = (dev->abs[code] + value) >> 1;
127                         }
128
129                         if (dev->abs[code] == value)
130                                 return;
131
132                         dev->abs[code] = value;
133                         break;
134
135                 case EV_REL:
136
137                         if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
138                                 return;
139
140                         break;
141
142                 case EV_MSC:
143
144                         if (code > MSC_MAX || !test_bit(code, dev->mscbit))
145                                 return;
146
147                         if (dev->event) dev->event(dev, type, code, value);
148
149                         break;
150
151                 case EV_LED:
152
153                         if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
154                                 return;
155
156                         change_bit(code, dev->led);
157                         if (dev->event) dev->event(dev, type, code, value);
158
159                         break;
160
161                 case EV_SND:
162
163                         if (code > SND_MAX || !test_bit(code, dev->sndbit))
164                                 return;
165
166                         if (dev->event) dev->event(dev, type, code, value);
167
168                         break;
169
170                 case EV_REP:
171
172                         if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
173
174                         dev->rep[code] = value;
175                         if (dev->event) dev->event(dev, type, code, value);
176
177                         break;
178
179                 case EV_FF:
180                         if (dev->event) dev->event(dev, type, code, value);
181                         break;
182         }
183
184         if (type != EV_SYN)
185                 dev->sync = 0;
186
187         if (dev->grab)
188                 dev->grab->handler->event(dev->grab, type, code, value);
189         else
190                 list_for_each_entry(handle, &dev->h_list, d_node)
191                         if (handle->open)
192                                 handle->handler->event(handle, type, code, value);
193 }
194
195 static void input_repeat_key(unsigned long data)
196 {
197         struct input_dev *dev = (void *) data;
198
199         if (!test_bit(dev->repeat_key, dev->key))
200                 return;
201
202         input_event(dev, EV_KEY, dev->repeat_key, 2);
203         input_sync(dev);
204
205         mod_timer(&dev->timer, jiffies + ms_to_jiffies(dev->rep[REP_PERIOD]));
206 }
207
208 int input_accept_process(struct input_handle *handle, struct file *file)
209 {
210         if (handle->dev->accept)
211                 return handle->dev->accept(handle->dev, file);
212
213         return 0;
214 }
215
216 int input_grab_device(struct input_handle *handle)
217 {
218         if (handle->dev->grab)
219                 return -EBUSY;
220
221         handle->dev->grab = handle;
222         return 0;
223 }
224
225 void input_release_device(struct input_handle *handle)
226 {
227         if (handle->dev->grab == handle)
228                 handle->dev->grab = NULL;
229 }
230
231 int input_open_device(struct input_handle *handle)
232 {
233         if (handle->dev->pm_dev)
234                 pm_access(handle->dev->pm_dev);
235         handle->open++;
236         if (handle->dev->open)
237                 return handle->dev->open(handle->dev);
238         return 0;
239 }
240
241 int input_flush_device(struct input_handle* handle, struct file* file)
242 {
243         if (handle->dev->flush)
244                 return handle->dev->flush(handle->dev, file);
245
246         return 0;
247 }
248
249 void input_close_device(struct input_handle *handle)
250 {
251         input_release_device(handle);
252         if (handle->dev->pm_dev)
253                 pm_dev_idle(handle->dev->pm_dev);
254         if (handle->dev->close)
255                 handle->dev->close(handle->dev);
256         handle->open--;
257 }
258
259 static void input_link_handle(struct input_handle *handle)
260 {
261         list_add_tail(&handle->d_node, &handle->dev->h_list);
262         list_add_tail(&handle->h_node, &handle->handler->h_list);
263 }
264
265 #define MATCH_BIT(bit, max) \
266                 for (i = 0; i < NBITS(max); i++) \
267                         if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
268                                 break; \
269                 if (i != NBITS(max)) \
270                         continue;
271
272 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
273 {
274         int i;
275
276         for (; id->flags || id->driver_info; id++) {
277
278                 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
279                         if (id->id.bustype != dev->id.bustype)
280                                 continue;
281
282                 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
283                         if (id->id.vendor != dev->id.vendor)
284                                 continue;
285
286                 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
287                         if (id->id.product != dev->id.product)
288                                 continue;
289
290                 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
291                         if (id->id.version != dev->id.version)
292                                 continue;
293
294                 MATCH_BIT(evbit,  EV_MAX);
295                 MATCH_BIT(keybit, KEY_MAX);
296                 MATCH_BIT(relbit, REL_MAX);
297                 MATCH_BIT(absbit, ABS_MAX);
298                 MATCH_BIT(mscbit, MSC_MAX);
299                 MATCH_BIT(ledbit, LED_MAX);
300                 MATCH_BIT(sndbit, SND_MAX);
301                 MATCH_BIT(ffbit,  FF_MAX);
302
303                 return id;
304         }
305
306         return NULL;
307 }
308
309 /*
310  * Input hotplugging interface - loading event handlers based on
311  * device bitfields.
312  */
313
314 #ifdef CONFIG_HOTPLUG
315
316 /*
317  * Input hotplugging invokes what /proc/sys/kernel/hotplug says
318  * (normally /sbin/hotplug) when input devices get added or removed.
319  *
320  * This invokes a user mode policy agent, typically helping to load driver
321  * or other modules, configure the device, and more.  Drivers can provide
322  * a MODULE_DEVICE_TABLE to help with module loading subtasks.
323  *
324  */
325
326 #define SPRINTF_BIT_A(bit, name, max) \
327         do { \
328                 envp[i++] = scratch; \
329                 scratch += sprintf(scratch, name); \
330                 for (j = NBITS(max) - 1; j >= 0; j--) \
331                         if (dev->bit[j]) break; \
332                 for (; j >= 0; j--) \
333                         scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
334                 scratch++; \
335         } while (0)
336
337 #define SPRINTF_BIT_A2(bit, name, max, ev) \
338         do { \
339                 if (test_bit(ev, dev->evbit)) \
340                         SPRINTF_BIT_A(bit, name, max); \
341         } while (0)
342
343 static void input_call_hotplug(char *verb, struct input_dev *dev)
344 {
345         char *argv[3], **envp, *buf, *scratch;
346         int i = 0, j, value;
347
348         if (!hotplug_path[0]) {
349                 printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
350                 return;
351         }
352         if (in_interrupt()) {
353                 printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
354                 return;
355         }
356         if (!current->fs->root) {
357                 printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
358                 return;
359         }
360         if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
361                 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
362                 return;
363         }
364         if (!(buf = kmalloc(1024, GFP_KERNEL))) {
365                 kfree (envp);
366                 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
367                 return;
368         }
369
370         argv[0] = hotplug_path;
371         argv[1] = "input";
372         argv[2] = 0;
373
374         envp[i++] = "HOME=/";
375         envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
376
377         scratch = buf;
378
379         envp[i++] = scratch;
380         scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
381
382         envp[i++] = scratch;
383         scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
384                 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
385
386         if (dev->name) {
387                 envp[i++] = scratch;
388                 scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
389         }
390
391         if (dev->phys) {
392                 envp[i++] = scratch;
393                 scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1;
394         }
395
396         SPRINTF_BIT_A(evbit, "EV=", EV_MAX);
397         SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY);
398         SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL);
399         SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS);
400         SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC);
401         SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED);
402         SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND);
403         SPRINTF_BIT_A2(ffbit,  "FF=",  FF_MAX, EV_FF);
404
405         envp[i++] = 0;
406
407 #ifdef INPUT_DEBUG
408         printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n",
409                 argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]);
410 #endif
411
412         value = call_usermodehelper(argv [0], argv, envp, 0);
413
414         kfree(buf);
415         kfree(envp);
416
417 #ifdef INPUT_DEBUG
418         if (value != 0)
419                 printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
420 #endif
421 }
422
423 #endif
424
425 void input_register_device(struct input_dev *dev)
426 {
427         struct input_handle *handle;
428         struct input_handler *handler;
429         struct input_device_id *id;
430
431         set_bit(EV_SYN, dev->evbit);
432
433         /*
434          * If delay and period are pre-set by the driver, then autorepeating
435          * is handled by the driver itself and we don't do it in input.c.
436          */
437
438         init_timer(&dev->timer);
439         if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
440                 dev->timer.data = (long) dev;
441                 dev->timer.function = input_repeat_key;
442                 dev->rep[REP_DELAY] = 250;
443                 dev->rep[REP_PERIOD] = 33;
444         }
445
446         INIT_LIST_HEAD(&dev->h_list);
447         list_add_tail(&dev->node, &input_dev_list);
448
449         list_for_each_entry(handler, &input_handler_list, node)
450                 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
451                         if ((id = input_match_device(handler->id_table, dev)))
452                                 if ((handle = handler->connect(handler, dev, id)))
453                                         input_link_handle(handle);
454
455 #ifdef CONFIG_HOTPLUG
456         input_call_hotplug("add", dev);
457 #endif
458
459 #ifdef CONFIG_PROC_FS
460         input_devices_state++;
461         wake_up(&input_devices_poll_wait);
462 #endif
463 }
464
465 void input_unregister_device(struct input_dev *dev)
466 {
467         struct list_head * node, * next;
468
469         if (!dev) return;
470
471         if (dev->pm_dev)
472                 pm_unregister(dev->pm_dev);
473
474         del_timer_sync(&dev->timer);
475
476         list_for_each_safe(node, next, &dev->h_list) {
477                 struct input_handle * handle = to_handle(node);
478                 list_del_init(&handle->d_node);
479                 list_del_init(&handle->h_node);
480                 handle->handler->disconnect(handle);
481         }
482
483 #ifdef CONFIG_HOTPLUG
484         input_call_hotplug("remove", dev);
485 #endif
486
487         list_del_init(&dev->node);
488
489 #ifdef CONFIG_PROC_FS
490         input_devices_state++;
491         wake_up(&input_devices_poll_wait);
492 #endif
493 }
494
495 void input_register_handler(struct input_handler *handler)
496 {
497         struct input_dev *dev;
498         struct input_handle *handle;
499         struct input_device_id *id;
500
501         if (!handler) return;
502
503         INIT_LIST_HEAD(&handler->h_list);
504
505         if (handler->fops != NULL)
506                 input_table[handler->minor >> 5] = handler;
507
508         list_add_tail(&handler->node, &input_handler_list);
509
510         list_for_each_entry(dev, &input_dev_list, node)
511                 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
512                         if ((id = input_match_device(handler->id_table, dev)))
513                                 if ((handle = handler->connect(handler, dev, id)))
514                                         input_link_handle(handle);
515
516 #ifdef CONFIG_PROC_FS
517         input_devices_state++;
518         wake_up(&input_devices_poll_wait);
519 #endif
520 }
521
522 void input_unregister_handler(struct input_handler *handler)
523 {
524         struct list_head * node, * next;
525
526         list_for_each_safe(node, next, &handler->h_list) {
527                 struct input_handle * handle = to_handle_h(node);
528                 list_del_init(&handle->h_node);
529                 list_del_init(&handle->d_node);
530                 handler->disconnect(handle);
531         }
532
533         list_del_init(&handler->node);
534
535         if (handler->fops != NULL)
536                 input_table[handler->minor >> 5] = NULL;
537
538 #ifdef CONFIG_PROC_FS
539         input_devices_state++;
540         wake_up(&input_devices_poll_wait);
541 #endif
542 }
543
544 static int input_open_file(struct inode *inode, struct file *file)
545 {
546         struct input_handler *handler = input_table[iminor(inode) >> 5];
547         struct file_operations *old_fops, *new_fops = NULL;
548         int err;
549
550         /* No load-on-demand here? */
551         if (!handler || !(new_fops = fops_get(handler->fops)))
552                 return -ENODEV;
553
554         /*
555          * That's _really_ odd. Usually NULL ->open means "nothing special",
556          * not "no device". Oh, well...
557          */
558         if (!new_fops->open) {
559                 fops_put(new_fops);
560                 return -ENODEV;
561         }
562         old_fops = file->f_op;
563         file->f_op = new_fops;
564
565         err = new_fops->open(inode, file);
566
567         if (err) {
568                 fops_put(file->f_op);
569                 file->f_op = fops_get(old_fops);
570         }
571         fops_put(old_fops);
572         return err;
573 }
574
575 static struct file_operations input_fops = {
576         .owner = THIS_MODULE,
577         .open = input_open_file,
578 };
579
580 #ifdef CONFIG_PROC_FS
581
582 #define SPRINTF_BIT_B(bit, name, max) \
583         do { \
584                 len += sprintf(buf + len, "B: %s", name); \
585                 for (i = NBITS(max) - 1; i >= 0; i--) \
586                         if (dev->bit[i]) break; \
587                 for (; i >= 0; i--) \
588                         len += sprintf(buf + len, "%lx ", dev->bit[i]); \
589                 len += sprintf(buf + len, "\n"); \
590         } while (0)
591
592 #define SPRINTF_BIT_B2(bit, name, max, ev) \
593         do { \
594                 if (test_bit(ev, dev->evbit)) \
595                         SPRINTF_BIT_B(bit, name, max); \
596         } while (0)
597
598
599 static unsigned int input_devices_poll(struct file *file, poll_table *wait)
600 {
601         int state = input_devices_state;
602         poll_wait(file, &input_devices_poll_wait, wait);
603         if (state != input_devices_state)
604                 return POLLIN | POLLRDNORM;
605         return 0;
606 }
607
608 static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
609 {
610         struct input_dev *dev;
611         struct input_handle *handle;
612
613         off_t at = 0;
614         int i, len, cnt = 0;
615
616         list_for_each_entry(dev, &input_dev_list, node) {
617
618                 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
619                         dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
620
621                 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
622                 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
623                 len += sprintf(buf + len, "H: Handlers=");
624
625                 list_for_each_entry(handle, &dev->h_list, d_node)
626                         len += sprintf(buf + len, "%s ", handle->name);
627
628                 len += sprintf(buf + len, "\n");
629
630                 SPRINTF_BIT_B(evbit, "EV=", EV_MAX);
631                 SPRINTF_BIT_B2(keybit, "KEY=", KEY_MAX, EV_KEY);
632                 SPRINTF_BIT_B2(relbit, "REL=", REL_MAX, EV_REL);
633                 SPRINTF_BIT_B2(absbit, "ABS=", ABS_MAX, EV_ABS);
634                 SPRINTF_BIT_B2(mscbit, "MSC=", MSC_MAX, EV_MSC);
635                 SPRINTF_BIT_B2(ledbit, "LED=", LED_MAX, EV_LED);
636                 SPRINTF_BIT_B2(sndbit, "SND=", SND_MAX, EV_SND);
637                 SPRINTF_BIT_B2(ffbit,  "FF=",  FF_MAX, EV_FF);
638
639                 len += sprintf(buf + len, "\n");
640
641                 at += len;
642
643                 if (at >= pos) {
644                         if (!*start) {
645                                 *start = buf + (pos - (at - len));
646                                 cnt = at - pos;
647                         } else  cnt += len;
648                         buf += len;
649                         if (cnt >= count)
650                                 break;
651                 }
652         }
653
654         if (&dev->node == &input_dev_list)
655                 *eof = 1;
656
657         return (count > cnt) ? cnt : count;
658 }
659
660 static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
661 {
662         struct input_handler *handler;
663
664         off_t at = 0;
665         int len = 0, cnt = 0;
666         int i = 0;
667
668         list_for_each_entry(handler, &input_handler_list, node) {
669
670                 if (handler->fops)
671                         len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
672                                 i++, handler->name, handler->minor);
673                 else
674                         len = sprintf(buf, "N: Number=%d Name=%s\n",
675                                 i++, handler->name);
676
677                 at += len;
678
679                 if (at >= pos) {
680                         if (!*start) {
681                                 *start = buf + (pos - (at - len));
682                                 cnt = at - pos;
683                         } else  cnt += len;
684                         buf += len;
685                         if (cnt >= count)
686                                 break;
687                 }
688         }
689         if (&handler->node == &input_handler_list)
690                 *eof = 1;
691
692         return (count > cnt) ? cnt : count;
693 }
694
695 static int __init input_proc_init(void)
696 {
697         struct proc_dir_entry *entry;
698
699         proc_bus_input_dir = proc_mkdir("input", proc_bus);
700         if (proc_bus_input_dir == NULL)
701                 return -ENOMEM;
702         proc_bus_input_dir->owner = THIS_MODULE;
703         entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
704         if (entry == NULL) {
705                 remove_proc_entry("input", proc_bus);
706                 return -ENOMEM;
707         }
708         entry->owner = THIS_MODULE;
709         entry->proc_fops->poll = input_devices_poll;
710         entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
711         if (entry == NULL) {
712                 remove_proc_entry("devices", proc_bus_input_dir);
713                 remove_proc_entry("input", proc_bus);
714                 return -ENOMEM;
715         }
716         entry->owner = THIS_MODULE;
717         return 0;
718 }
719 #else /* !CONFIG_PROC_FS */
720 static inline int input_proc_init(void) { return 0; }
721 #endif
722
723 struct class_simple *input_class;
724
725 static int __init input_init(void)
726 {
727         int retval = -ENOMEM;
728
729         input_class = class_simple_create(THIS_MODULE, "input");
730         if (IS_ERR(input_class))
731                 return PTR_ERR(input_class);
732         input_proc_init();
733         retval = register_chrdev(INPUT_MAJOR, "input", &input_fops);
734         if (retval) {
735                 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
736                 remove_proc_entry("devices", proc_bus_input_dir);
737                 remove_proc_entry("handlers", proc_bus_input_dir);
738                 remove_proc_entry("input", proc_bus);
739                 class_simple_destroy(input_class);
740                 return retval;
741         }
742
743         retval = devfs_mk_dir("input");
744         if (retval) {
745                 remove_proc_entry("devices", proc_bus_input_dir);
746                 remove_proc_entry("handlers", proc_bus_input_dir);
747                 remove_proc_entry("input", proc_bus);
748                 unregister_chrdev(INPUT_MAJOR, "input");
749                 class_simple_destroy(input_class);
750         }
751         return retval;
752 }
753
754 static void __exit input_exit(void)
755 {
756         remove_proc_entry("devices", proc_bus_input_dir);
757         remove_proc_entry("handlers", proc_bus_input_dir);
758         remove_proc_entry("input", proc_bus);
759
760         devfs_remove("input");
761         unregister_chrdev(INPUT_MAJOR, "input");
762         class_simple_destroy(input_class);
763 }
764
765 subsys_initcall(input_init);
766 module_exit(input_exit);