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