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