xref: /linux/drivers/input/input.c (revision b8bb76713ec50df2f11efee386e16f93d51e1076)
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/input.h>
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/major.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/poll.h>
21 #include <linux/device.h>
22 #include <linux/mutex.h>
23 #include <linux/rcupdate.h>
24 #include <linux/smp_lock.h>
25 
26 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
27 MODULE_DESCRIPTION("Input core");
28 MODULE_LICENSE("GPL");
29 
30 #define INPUT_DEVICES	256
31 
32 static LIST_HEAD(input_dev_list);
33 static LIST_HEAD(input_handler_list);
34 
35 /*
36  * input_mutex protects access to both input_dev_list and input_handler_list.
37  * This also causes input_[un]register_device and input_[un]register_handler
38  * be mutually exclusive which simplifies locking in drivers implementing
39  * input handlers.
40  */
41 static DEFINE_MUTEX(input_mutex);
42 
43 static struct input_handler *input_table[8];
44 
45 static inline int is_event_supported(unsigned int code,
46 				     unsigned long *bm, unsigned int max)
47 {
48 	return code <= max && test_bit(code, bm);
49 }
50 
51 static int input_defuzz_abs_event(int value, int old_val, int fuzz)
52 {
53 	if (fuzz) {
54 		if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
55 			return old_val;
56 
57 		if (value > old_val - fuzz && value < old_val + fuzz)
58 			return (old_val * 3 + value) / 4;
59 
60 		if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
61 			return (old_val + value) / 2;
62 	}
63 
64 	return value;
65 }
66 
67 /*
68  * Pass event through all open handles. This function is called with
69  * dev->event_lock held and interrupts disabled.
70  */
71 static void input_pass_event(struct input_dev *dev,
72 			     unsigned int type, unsigned int code, int value)
73 {
74 	struct input_handle *handle;
75 
76 	rcu_read_lock();
77 
78 	handle = rcu_dereference(dev->grab);
79 	if (handle)
80 		handle->handler->event(handle, type, code, value);
81 	else
82 		list_for_each_entry_rcu(handle, &dev->h_list, d_node)
83 			if (handle->open)
84 				handle->handler->event(handle,
85 							type, code, value);
86 	rcu_read_unlock();
87 }
88 
89 /*
90  * Generate software autorepeat event. Note that we take
91  * dev->event_lock here to avoid racing with input_event
92  * which may cause keys get "stuck".
93  */
94 static void input_repeat_key(unsigned long data)
95 {
96 	struct input_dev *dev = (void *) data;
97 	unsigned long flags;
98 
99 	spin_lock_irqsave(&dev->event_lock, flags);
100 
101 	if (test_bit(dev->repeat_key, dev->key) &&
102 	    is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
103 
104 		input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
105 
106 		if (dev->sync) {
107 			/*
108 			 * Only send SYN_REPORT if we are not in a middle
109 			 * of driver parsing a new hardware packet.
110 			 * Otherwise assume that the driver will send
111 			 * SYN_REPORT once it's done.
112 			 */
113 			input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
114 		}
115 
116 		if (dev->rep[REP_PERIOD])
117 			mod_timer(&dev->timer, jiffies +
118 					msecs_to_jiffies(dev->rep[REP_PERIOD]));
119 	}
120 
121 	spin_unlock_irqrestore(&dev->event_lock, flags);
122 }
123 
124 static void input_start_autorepeat(struct input_dev *dev, int code)
125 {
126 	if (test_bit(EV_REP, dev->evbit) &&
127 	    dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
128 	    dev->timer.data) {
129 		dev->repeat_key = code;
130 		mod_timer(&dev->timer,
131 			  jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
132 	}
133 }
134 
135 #define INPUT_IGNORE_EVENT	0
136 #define INPUT_PASS_TO_HANDLERS	1
137 #define INPUT_PASS_TO_DEVICE	2
138 #define INPUT_PASS_TO_ALL	(INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
139 
140 static void input_handle_event(struct input_dev *dev,
141 			       unsigned int type, unsigned int code, int value)
142 {
143 	int disposition = INPUT_IGNORE_EVENT;
144 
145 	switch (type) {
146 
147 	case EV_SYN:
148 		switch (code) {
149 		case SYN_CONFIG:
150 			disposition = INPUT_PASS_TO_ALL;
151 			break;
152 
153 		case SYN_REPORT:
154 			if (!dev->sync) {
155 				dev->sync = 1;
156 				disposition = INPUT_PASS_TO_HANDLERS;
157 			}
158 			break;
159 		}
160 		break;
161 
162 	case EV_KEY:
163 		if (is_event_supported(code, dev->keybit, KEY_MAX) &&
164 		    !!test_bit(code, dev->key) != value) {
165 
166 			if (value != 2) {
167 				__change_bit(code, dev->key);
168 				if (value)
169 					input_start_autorepeat(dev, code);
170 			}
171 
172 			disposition = INPUT_PASS_TO_HANDLERS;
173 		}
174 		break;
175 
176 	case EV_SW:
177 		if (is_event_supported(code, dev->swbit, SW_MAX) &&
178 		    !!test_bit(code, dev->sw) != value) {
179 
180 			__change_bit(code, dev->sw);
181 			disposition = INPUT_PASS_TO_HANDLERS;
182 		}
183 		break;
184 
185 	case EV_ABS:
186 		if (is_event_supported(code, dev->absbit, ABS_MAX)) {
187 
188 			value = input_defuzz_abs_event(value,
189 					dev->abs[code], dev->absfuzz[code]);
190 
191 			if (dev->abs[code] != value) {
192 				dev->abs[code] = value;
193 				disposition = INPUT_PASS_TO_HANDLERS;
194 			}
195 		}
196 		break;
197 
198 	case EV_REL:
199 		if (is_event_supported(code, dev->relbit, REL_MAX) && value)
200 			disposition = INPUT_PASS_TO_HANDLERS;
201 
202 		break;
203 
204 	case EV_MSC:
205 		if (is_event_supported(code, dev->mscbit, MSC_MAX))
206 			disposition = INPUT_PASS_TO_ALL;
207 
208 		break;
209 
210 	case EV_LED:
211 		if (is_event_supported(code, dev->ledbit, LED_MAX) &&
212 		    !!test_bit(code, dev->led) != value) {
213 
214 			__change_bit(code, dev->led);
215 			disposition = INPUT_PASS_TO_ALL;
216 		}
217 		break;
218 
219 	case EV_SND:
220 		if (is_event_supported(code, dev->sndbit, SND_MAX)) {
221 
222 			if (!!test_bit(code, dev->snd) != !!value)
223 				__change_bit(code, dev->snd);
224 			disposition = INPUT_PASS_TO_ALL;
225 		}
226 		break;
227 
228 	case EV_REP:
229 		if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
230 			dev->rep[code] = value;
231 			disposition = INPUT_PASS_TO_ALL;
232 		}
233 		break;
234 
235 	case EV_FF:
236 		if (value >= 0)
237 			disposition = INPUT_PASS_TO_ALL;
238 		break;
239 
240 	case EV_PWR:
241 		disposition = INPUT_PASS_TO_ALL;
242 		break;
243 	}
244 
245 	if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
246 		dev->sync = 0;
247 
248 	if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
249 		dev->event(dev, type, code, value);
250 
251 	if (disposition & INPUT_PASS_TO_HANDLERS)
252 		input_pass_event(dev, type, code, value);
253 }
254 
255 /**
256  * input_event() - report new input event
257  * @dev: device that generated the event
258  * @type: type of the event
259  * @code: event code
260  * @value: value of the event
261  *
262  * This function should be used by drivers implementing various input
263  * devices. See also input_inject_event().
264  */
265 
266 void input_event(struct input_dev *dev,
267 		 unsigned int type, unsigned int code, int value)
268 {
269 	unsigned long flags;
270 
271 	if (is_event_supported(type, dev->evbit, EV_MAX)) {
272 
273 		spin_lock_irqsave(&dev->event_lock, flags);
274 		add_input_randomness(type, code, value);
275 		input_handle_event(dev, type, code, value);
276 		spin_unlock_irqrestore(&dev->event_lock, flags);
277 	}
278 }
279 EXPORT_SYMBOL(input_event);
280 
281 /**
282  * input_inject_event() - send input event from input handler
283  * @handle: input handle to send event through
284  * @type: type of the event
285  * @code: event code
286  * @value: value of the event
287  *
288  * Similar to input_event() but will ignore event if device is
289  * "grabbed" and handle injecting event is not the one that owns
290  * the device.
291  */
292 void input_inject_event(struct input_handle *handle,
293 			unsigned int type, unsigned int code, int value)
294 {
295 	struct input_dev *dev = handle->dev;
296 	struct input_handle *grab;
297 	unsigned long flags;
298 
299 	if (is_event_supported(type, dev->evbit, EV_MAX)) {
300 		spin_lock_irqsave(&dev->event_lock, flags);
301 
302 		rcu_read_lock();
303 		grab = rcu_dereference(dev->grab);
304 		if (!grab || grab == handle)
305 			input_handle_event(dev, type, code, value);
306 		rcu_read_unlock();
307 
308 		spin_unlock_irqrestore(&dev->event_lock, flags);
309 	}
310 }
311 EXPORT_SYMBOL(input_inject_event);
312 
313 /**
314  * input_grab_device - grabs device for exclusive use
315  * @handle: input handle that wants to own the device
316  *
317  * When a device is grabbed by an input handle all events generated by
318  * the device are delivered only to this handle. Also events injected
319  * by other input handles are ignored while device is grabbed.
320  */
321 int input_grab_device(struct input_handle *handle)
322 {
323 	struct input_dev *dev = handle->dev;
324 	int retval;
325 
326 	retval = mutex_lock_interruptible(&dev->mutex);
327 	if (retval)
328 		return retval;
329 
330 	if (dev->grab) {
331 		retval = -EBUSY;
332 		goto out;
333 	}
334 
335 	rcu_assign_pointer(dev->grab, handle);
336 	synchronize_rcu();
337 
338  out:
339 	mutex_unlock(&dev->mutex);
340 	return retval;
341 }
342 EXPORT_SYMBOL(input_grab_device);
343 
344 static void __input_release_device(struct input_handle *handle)
345 {
346 	struct input_dev *dev = handle->dev;
347 
348 	if (dev->grab == handle) {
349 		rcu_assign_pointer(dev->grab, NULL);
350 		/* Make sure input_pass_event() notices that grab is gone */
351 		synchronize_rcu();
352 
353 		list_for_each_entry(handle, &dev->h_list, d_node)
354 			if (handle->open && handle->handler->start)
355 				handle->handler->start(handle);
356 	}
357 }
358 
359 /**
360  * input_release_device - release previously grabbed device
361  * @handle: input handle that owns the device
362  *
363  * Releases previously grabbed device so that other input handles can
364  * start receiving input events. Upon release all handlers attached
365  * to the device have their start() method called so they have a change
366  * to synchronize device state with the rest of the system.
367  */
368 void input_release_device(struct input_handle *handle)
369 {
370 	struct input_dev *dev = handle->dev;
371 
372 	mutex_lock(&dev->mutex);
373 	__input_release_device(handle);
374 	mutex_unlock(&dev->mutex);
375 }
376 EXPORT_SYMBOL(input_release_device);
377 
378 /**
379  * input_open_device - open input device
380  * @handle: handle through which device is being accessed
381  *
382  * This function should be called by input handlers when they
383  * want to start receive events from given input device.
384  */
385 int input_open_device(struct input_handle *handle)
386 {
387 	struct input_dev *dev = handle->dev;
388 	int retval;
389 
390 	retval = mutex_lock_interruptible(&dev->mutex);
391 	if (retval)
392 		return retval;
393 
394 	if (dev->going_away) {
395 		retval = -ENODEV;
396 		goto out;
397 	}
398 
399 	handle->open++;
400 
401 	if (!dev->users++ && dev->open)
402 		retval = dev->open(dev);
403 
404 	if (retval) {
405 		dev->users--;
406 		if (!--handle->open) {
407 			/*
408 			 * Make sure we are not delivering any more events
409 			 * through this handle
410 			 */
411 			synchronize_rcu();
412 		}
413 	}
414 
415  out:
416 	mutex_unlock(&dev->mutex);
417 	return retval;
418 }
419 EXPORT_SYMBOL(input_open_device);
420 
421 int input_flush_device(struct input_handle *handle, struct file *file)
422 {
423 	struct input_dev *dev = handle->dev;
424 	int retval;
425 
426 	retval = mutex_lock_interruptible(&dev->mutex);
427 	if (retval)
428 		return retval;
429 
430 	if (dev->flush)
431 		retval = dev->flush(dev, file);
432 
433 	mutex_unlock(&dev->mutex);
434 	return retval;
435 }
436 EXPORT_SYMBOL(input_flush_device);
437 
438 /**
439  * input_close_device - close input device
440  * @handle: handle through which device is being accessed
441  *
442  * This function should be called by input handlers when they
443  * want to stop receive events from given input device.
444  */
445 void input_close_device(struct input_handle *handle)
446 {
447 	struct input_dev *dev = handle->dev;
448 
449 	mutex_lock(&dev->mutex);
450 
451 	__input_release_device(handle);
452 
453 	if (!--dev->users && dev->close)
454 		dev->close(dev);
455 
456 	if (!--handle->open) {
457 		/*
458 		 * synchronize_rcu() makes sure that input_pass_event()
459 		 * completed and that no more input events are delivered
460 		 * through this handle
461 		 */
462 		synchronize_rcu();
463 	}
464 
465 	mutex_unlock(&dev->mutex);
466 }
467 EXPORT_SYMBOL(input_close_device);
468 
469 /*
470  * Prepare device for unregistering
471  */
472 static void input_disconnect_device(struct input_dev *dev)
473 {
474 	struct input_handle *handle;
475 	int code;
476 
477 	/*
478 	 * Mark device as going away. Note that we take dev->mutex here
479 	 * not to protect access to dev->going_away but rather to ensure
480 	 * that there are no threads in the middle of input_open_device()
481 	 */
482 	mutex_lock(&dev->mutex);
483 	dev->going_away = 1;
484 	mutex_unlock(&dev->mutex);
485 
486 	spin_lock_irq(&dev->event_lock);
487 
488 	/*
489 	 * Simulate keyup events for all pressed keys so that handlers
490 	 * are not left with "stuck" keys. The driver may continue
491 	 * generate events even after we done here but they will not
492 	 * reach any handlers.
493 	 */
494 	if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
495 		for (code = 0; code <= KEY_MAX; code++) {
496 			if (is_event_supported(code, dev->keybit, KEY_MAX) &&
497 			    __test_and_clear_bit(code, dev->key)) {
498 				input_pass_event(dev, EV_KEY, code, 0);
499 			}
500 		}
501 		input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
502 	}
503 
504 	list_for_each_entry(handle, &dev->h_list, d_node)
505 		handle->open = 0;
506 
507 	spin_unlock_irq(&dev->event_lock);
508 }
509 
510 static int input_fetch_keycode(struct input_dev *dev, int scancode)
511 {
512 	switch (dev->keycodesize) {
513 		case 1:
514 			return ((u8 *)dev->keycode)[scancode];
515 
516 		case 2:
517 			return ((u16 *)dev->keycode)[scancode];
518 
519 		default:
520 			return ((u32 *)dev->keycode)[scancode];
521 	}
522 }
523 
524 static int input_default_getkeycode(struct input_dev *dev,
525 				    int scancode, int *keycode)
526 {
527 	if (!dev->keycodesize)
528 		return -EINVAL;
529 
530 	if (scancode >= dev->keycodemax)
531 		return -EINVAL;
532 
533 	*keycode = input_fetch_keycode(dev, scancode);
534 
535 	return 0;
536 }
537 
538 static int input_default_setkeycode(struct input_dev *dev,
539 				    int scancode, int keycode)
540 {
541 	int old_keycode;
542 	int i;
543 
544 	if (scancode >= dev->keycodemax)
545 		return -EINVAL;
546 
547 	if (!dev->keycodesize)
548 		return -EINVAL;
549 
550 	if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
551 		return -EINVAL;
552 
553 	switch (dev->keycodesize) {
554 		case 1: {
555 			u8 *k = (u8 *)dev->keycode;
556 			old_keycode = k[scancode];
557 			k[scancode] = keycode;
558 			break;
559 		}
560 		case 2: {
561 			u16 *k = (u16 *)dev->keycode;
562 			old_keycode = k[scancode];
563 			k[scancode] = keycode;
564 			break;
565 		}
566 		default: {
567 			u32 *k = (u32 *)dev->keycode;
568 			old_keycode = k[scancode];
569 			k[scancode] = keycode;
570 			break;
571 		}
572 	}
573 
574 	clear_bit(old_keycode, dev->keybit);
575 	set_bit(keycode, dev->keybit);
576 
577 	for (i = 0; i < dev->keycodemax; i++) {
578 		if (input_fetch_keycode(dev, i) == old_keycode) {
579 			set_bit(old_keycode, dev->keybit);
580 			break; /* Setting the bit twice is useless, so break */
581 		}
582 	}
583 
584 	return 0;
585 }
586 
587 /**
588  * input_get_keycode - retrieve keycode currently mapped to a given scancode
589  * @dev: input device which keymap is being queried
590  * @scancode: scancode (or its equivalent for device in question) for which
591  *	keycode is needed
592  * @keycode: result
593  *
594  * This function should be called by anyone interested in retrieving current
595  * keymap. Presently keyboard and evdev handlers use it.
596  */
597 int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
598 {
599 	if (scancode < 0)
600 		return -EINVAL;
601 
602 	return dev->getkeycode(dev, scancode, keycode);
603 }
604 EXPORT_SYMBOL(input_get_keycode);
605 
606 /**
607  * input_get_keycode - assign new keycode to a given scancode
608  * @dev: input device which keymap is being updated
609  * @scancode: scancode (or its equivalent for device in question)
610  * @keycode: new keycode to be assigned to the scancode
611  *
612  * This function should be called by anyone needing to update current
613  * keymap. Presently keyboard and evdev handlers use it.
614  */
615 int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
616 {
617 	unsigned long flags;
618 	int old_keycode;
619 	int retval;
620 
621 	if (scancode < 0)
622 		return -EINVAL;
623 
624 	if (keycode < 0 || keycode > KEY_MAX)
625 		return -EINVAL;
626 
627 	spin_lock_irqsave(&dev->event_lock, flags);
628 
629 	retval = dev->getkeycode(dev, scancode, &old_keycode);
630 	if (retval)
631 		goto out;
632 
633 	retval = dev->setkeycode(dev, scancode, keycode);
634 	if (retval)
635 		goto out;
636 
637 	/*
638 	 * Simulate keyup event if keycode is not present
639 	 * in the keymap anymore
640 	 */
641 	if (test_bit(EV_KEY, dev->evbit) &&
642 	    !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
643 	    __test_and_clear_bit(old_keycode, dev->key)) {
644 
645 		input_pass_event(dev, EV_KEY, old_keycode, 0);
646 		if (dev->sync)
647 			input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
648 	}
649 
650  out:
651 	spin_unlock_irqrestore(&dev->event_lock, flags);
652 
653 	return retval;
654 }
655 EXPORT_SYMBOL(input_set_keycode);
656 
657 #define MATCH_BIT(bit, max) \
658 		for (i = 0; i < BITS_TO_LONGS(max); i++) \
659 			if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
660 				break; \
661 		if (i != BITS_TO_LONGS(max)) \
662 			continue;
663 
664 static const struct input_device_id *input_match_device(const struct input_device_id *id,
665 							struct input_dev *dev)
666 {
667 	int i;
668 
669 	for (; id->flags || id->driver_info; id++) {
670 
671 		if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
672 			if (id->bustype != dev->id.bustype)
673 				continue;
674 
675 		if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
676 			if (id->vendor != dev->id.vendor)
677 				continue;
678 
679 		if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
680 			if (id->product != dev->id.product)
681 				continue;
682 
683 		if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
684 			if (id->version != dev->id.version)
685 				continue;
686 
687 		MATCH_BIT(evbit,  EV_MAX);
688 		MATCH_BIT(keybit, KEY_MAX);
689 		MATCH_BIT(relbit, REL_MAX);
690 		MATCH_BIT(absbit, ABS_MAX);
691 		MATCH_BIT(mscbit, MSC_MAX);
692 		MATCH_BIT(ledbit, LED_MAX);
693 		MATCH_BIT(sndbit, SND_MAX);
694 		MATCH_BIT(ffbit,  FF_MAX);
695 		MATCH_BIT(swbit,  SW_MAX);
696 
697 		return id;
698 	}
699 
700 	return NULL;
701 }
702 
703 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
704 {
705 	const struct input_device_id *id;
706 	int error;
707 
708 	if (handler->blacklist && input_match_device(handler->blacklist, dev))
709 		return -ENODEV;
710 
711 	id = input_match_device(handler->id_table, dev);
712 	if (!id)
713 		return -ENODEV;
714 
715 	error = handler->connect(handler, dev, id);
716 	if (error && error != -ENODEV)
717 		printk(KERN_ERR
718 			"input: failed to attach handler %s to device %s, "
719 			"error: %d\n",
720 			handler->name, kobject_name(&dev->dev.kobj), error);
721 
722 	return error;
723 }
724 
725 
726 #ifdef CONFIG_PROC_FS
727 
728 static struct proc_dir_entry *proc_bus_input_dir;
729 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
730 static int input_devices_state;
731 
732 static inline void input_wakeup_procfs_readers(void)
733 {
734 	input_devices_state++;
735 	wake_up(&input_devices_poll_wait);
736 }
737 
738 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
739 {
740 	int state = input_devices_state;
741 
742 	poll_wait(file, &input_devices_poll_wait, wait);
743 	if (state != input_devices_state)
744 		return POLLIN | POLLRDNORM;
745 
746 	return 0;
747 }
748 
749 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
750 {
751 	if (mutex_lock_interruptible(&input_mutex))
752 		return NULL;
753 
754 	return seq_list_start(&input_dev_list, *pos);
755 }
756 
757 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
758 {
759 	return seq_list_next(v, &input_dev_list, pos);
760 }
761 
762 static void input_devices_seq_stop(struct seq_file *seq, void *v)
763 {
764 	mutex_unlock(&input_mutex);
765 }
766 
767 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
768 				   unsigned long *bitmap, int max)
769 {
770 	int i;
771 
772 	for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
773 		if (bitmap[i])
774 			break;
775 
776 	seq_printf(seq, "B: %s=", name);
777 	for (; i >= 0; i--)
778 		seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
779 	seq_putc(seq, '\n');
780 }
781 
782 static int input_devices_seq_show(struct seq_file *seq, void *v)
783 {
784 	struct input_dev *dev = container_of(v, struct input_dev, node);
785 	const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
786 	struct input_handle *handle;
787 
788 	seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
789 		   dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
790 
791 	seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
792 	seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
793 	seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
794 	seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
795 	seq_printf(seq, "H: Handlers=");
796 
797 	list_for_each_entry(handle, &dev->h_list, d_node)
798 		seq_printf(seq, "%s ", handle->name);
799 	seq_putc(seq, '\n');
800 
801 	input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
802 	if (test_bit(EV_KEY, dev->evbit))
803 		input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
804 	if (test_bit(EV_REL, dev->evbit))
805 		input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
806 	if (test_bit(EV_ABS, dev->evbit))
807 		input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
808 	if (test_bit(EV_MSC, dev->evbit))
809 		input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
810 	if (test_bit(EV_LED, dev->evbit))
811 		input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
812 	if (test_bit(EV_SND, dev->evbit))
813 		input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
814 	if (test_bit(EV_FF, dev->evbit))
815 		input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
816 	if (test_bit(EV_SW, dev->evbit))
817 		input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
818 
819 	seq_putc(seq, '\n');
820 
821 	kfree(path);
822 	return 0;
823 }
824 
825 static const struct seq_operations input_devices_seq_ops = {
826 	.start	= input_devices_seq_start,
827 	.next	= input_devices_seq_next,
828 	.stop	= input_devices_seq_stop,
829 	.show	= input_devices_seq_show,
830 };
831 
832 static int input_proc_devices_open(struct inode *inode, struct file *file)
833 {
834 	return seq_open(file, &input_devices_seq_ops);
835 }
836 
837 static const struct file_operations input_devices_fileops = {
838 	.owner		= THIS_MODULE,
839 	.open		= input_proc_devices_open,
840 	.poll		= input_proc_devices_poll,
841 	.read		= seq_read,
842 	.llseek		= seq_lseek,
843 	.release	= seq_release,
844 };
845 
846 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
847 {
848 	if (mutex_lock_interruptible(&input_mutex))
849 		return NULL;
850 
851 	seq->private = (void *)(unsigned long)*pos;
852 	return seq_list_start(&input_handler_list, *pos);
853 }
854 
855 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
856 {
857 	seq->private = (void *)(unsigned long)(*pos + 1);
858 	return seq_list_next(v, &input_handler_list, pos);
859 }
860 
861 static void input_handlers_seq_stop(struct seq_file *seq, void *v)
862 {
863 	mutex_unlock(&input_mutex);
864 }
865 
866 static int input_handlers_seq_show(struct seq_file *seq, void *v)
867 {
868 	struct input_handler *handler = container_of(v, struct input_handler, node);
869 
870 	seq_printf(seq, "N: Number=%ld Name=%s",
871 		   (unsigned long)seq->private, handler->name);
872 	if (handler->fops)
873 		seq_printf(seq, " Minor=%d", handler->minor);
874 	seq_putc(seq, '\n');
875 
876 	return 0;
877 }
878 static const struct seq_operations input_handlers_seq_ops = {
879 	.start	= input_handlers_seq_start,
880 	.next	= input_handlers_seq_next,
881 	.stop	= input_handlers_seq_stop,
882 	.show	= input_handlers_seq_show,
883 };
884 
885 static int input_proc_handlers_open(struct inode *inode, struct file *file)
886 {
887 	return seq_open(file, &input_handlers_seq_ops);
888 }
889 
890 static const struct file_operations input_handlers_fileops = {
891 	.owner		= THIS_MODULE,
892 	.open		= input_proc_handlers_open,
893 	.read		= seq_read,
894 	.llseek		= seq_lseek,
895 	.release	= seq_release,
896 };
897 
898 static int __init input_proc_init(void)
899 {
900 	struct proc_dir_entry *entry;
901 
902 	proc_bus_input_dir = proc_mkdir("bus/input", NULL);
903 	if (!proc_bus_input_dir)
904 		return -ENOMEM;
905 
906 	entry = proc_create("devices", 0, proc_bus_input_dir,
907 			    &input_devices_fileops);
908 	if (!entry)
909 		goto fail1;
910 
911 	entry = proc_create("handlers", 0, proc_bus_input_dir,
912 			    &input_handlers_fileops);
913 	if (!entry)
914 		goto fail2;
915 
916 	return 0;
917 
918  fail2:	remove_proc_entry("devices", proc_bus_input_dir);
919  fail1: remove_proc_entry("bus/input", NULL);
920 	return -ENOMEM;
921 }
922 
923 static void input_proc_exit(void)
924 {
925 	remove_proc_entry("devices", proc_bus_input_dir);
926 	remove_proc_entry("handlers", proc_bus_input_dir);
927 	remove_proc_entry("bus/input", NULL);
928 }
929 
930 #else /* !CONFIG_PROC_FS */
931 static inline void input_wakeup_procfs_readers(void) { }
932 static inline int input_proc_init(void) { return 0; }
933 static inline void input_proc_exit(void) { }
934 #endif
935 
936 #define INPUT_DEV_STRING_ATTR_SHOW(name)				\
937 static ssize_t input_dev_show_##name(struct device *dev,		\
938 				     struct device_attribute *attr,	\
939 				     char *buf)				\
940 {									\
941 	struct input_dev *input_dev = to_input_dev(dev);		\
942 									\
943 	return scnprintf(buf, PAGE_SIZE, "%s\n",			\
944 			 input_dev->name ? input_dev->name : "");	\
945 }									\
946 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
947 
948 INPUT_DEV_STRING_ATTR_SHOW(name);
949 INPUT_DEV_STRING_ATTR_SHOW(phys);
950 INPUT_DEV_STRING_ATTR_SHOW(uniq);
951 
952 static int input_print_modalias_bits(char *buf, int size,
953 				     char name, unsigned long *bm,
954 				     unsigned int min_bit, unsigned int max_bit)
955 {
956 	int len = 0, i;
957 
958 	len += snprintf(buf, max(size, 0), "%c", name);
959 	for (i = min_bit; i < max_bit; i++)
960 		if (bm[BIT_WORD(i)] & BIT_MASK(i))
961 			len += snprintf(buf + len, max(size - len, 0), "%X,", i);
962 	return len;
963 }
964 
965 static int input_print_modalias(char *buf, int size, struct input_dev *id,
966 				int add_cr)
967 {
968 	int len;
969 
970 	len = snprintf(buf, max(size, 0),
971 		       "input:b%04Xv%04Xp%04Xe%04X-",
972 		       id->id.bustype, id->id.vendor,
973 		       id->id.product, id->id.version);
974 
975 	len += input_print_modalias_bits(buf + len, size - len,
976 				'e', id->evbit, 0, EV_MAX);
977 	len += input_print_modalias_bits(buf + len, size - len,
978 				'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
979 	len += input_print_modalias_bits(buf + len, size - len,
980 				'r', id->relbit, 0, REL_MAX);
981 	len += input_print_modalias_bits(buf + len, size - len,
982 				'a', id->absbit, 0, ABS_MAX);
983 	len += input_print_modalias_bits(buf + len, size - len,
984 				'm', id->mscbit, 0, MSC_MAX);
985 	len += input_print_modalias_bits(buf + len, size - len,
986 				'l', id->ledbit, 0, LED_MAX);
987 	len += input_print_modalias_bits(buf + len, size - len,
988 				's', id->sndbit, 0, SND_MAX);
989 	len += input_print_modalias_bits(buf + len, size - len,
990 				'f', id->ffbit, 0, FF_MAX);
991 	len += input_print_modalias_bits(buf + len, size - len,
992 				'w', id->swbit, 0, SW_MAX);
993 
994 	if (add_cr)
995 		len += snprintf(buf + len, max(size - len, 0), "\n");
996 
997 	return len;
998 }
999 
1000 static ssize_t input_dev_show_modalias(struct device *dev,
1001 				       struct device_attribute *attr,
1002 				       char *buf)
1003 {
1004 	struct input_dev *id = to_input_dev(dev);
1005 	ssize_t len;
1006 
1007 	len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1008 
1009 	return min_t(int, len, PAGE_SIZE);
1010 }
1011 static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
1012 
1013 static struct attribute *input_dev_attrs[] = {
1014 	&dev_attr_name.attr,
1015 	&dev_attr_phys.attr,
1016 	&dev_attr_uniq.attr,
1017 	&dev_attr_modalias.attr,
1018 	NULL
1019 };
1020 
1021 static struct attribute_group input_dev_attr_group = {
1022 	.attrs	= input_dev_attrs,
1023 };
1024 
1025 #define INPUT_DEV_ID_ATTR(name)						\
1026 static ssize_t input_dev_show_id_##name(struct device *dev,		\
1027 					struct device_attribute *attr,	\
1028 					char *buf)			\
1029 {									\
1030 	struct input_dev *input_dev = to_input_dev(dev);		\
1031 	return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name);	\
1032 }									\
1033 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
1034 
1035 INPUT_DEV_ID_ATTR(bustype);
1036 INPUT_DEV_ID_ATTR(vendor);
1037 INPUT_DEV_ID_ATTR(product);
1038 INPUT_DEV_ID_ATTR(version);
1039 
1040 static struct attribute *input_dev_id_attrs[] = {
1041 	&dev_attr_bustype.attr,
1042 	&dev_attr_vendor.attr,
1043 	&dev_attr_product.attr,
1044 	&dev_attr_version.attr,
1045 	NULL
1046 };
1047 
1048 static struct attribute_group input_dev_id_attr_group = {
1049 	.name	= "id",
1050 	.attrs	= input_dev_id_attrs,
1051 };
1052 
1053 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1054 			      int max, int add_cr)
1055 {
1056 	int i;
1057 	int len = 0;
1058 
1059 	for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
1060 		if (bitmap[i])
1061 			break;
1062 
1063 	for (; i >= 0; i--)
1064 		len += snprintf(buf + len, max(buf_size - len, 0),
1065 				"%lx%s", bitmap[i], i > 0 ? " " : "");
1066 
1067 	if (add_cr)
1068 		len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1069 
1070 	return len;
1071 }
1072 
1073 #define INPUT_DEV_CAP_ATTR(ev, bm)					\
1074 static ssize_t input_dev_show_cap_##bm(struct device *dev,		\
1075 				       struct device_attribute *attr,	\
1076 				       char *buf)			\
1077 {									\
1078 	struct input_dev *input_dev = to_input_dev(dev);		\
1079 	int len = input_print_bitmap(buf, PAGE_SIZE,			\
1080 				     input_dev->bm##bit, ev##_MAX, 1);	\
1081 	return min_t(int, len, PAGE_SIZE);				\
1082 }									\
1083 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
1084 
1085 INPUT_DEV_CAP_ATTR(EV, ev);
1086 INPUT_DEV_CAP_ATTR(KEY, key);
1087 INPUT_DEV_CAP_ATTR(REL, rel);
1088 INPUT_DEV_CAP_ATTR(ABS, abs);
1089 INPUT_DEV_CAP_ATTR(MSC, msc);
1090 INPUT_DEV_CAP_ATTR(LED, led);
1091 INPUT_DEV_CAP_ATTR(SND, snd);
1092 INPUT_DEV_CAP_ATTR(FF, ff);
1093 INPUT_DEV_CAP_ATTR(SW, sw);
1094 
1095 static struct attribute *input_dev_caps_attrs[] = {
1096 	&dev_attr_ev.attr,
1097 	&dev_attr_key.attr,
1098 	&dev_attr_rel.attr,
1099 	&dev_attr_abs.attr,
1100 	&dev_attr_msc.attr,
1101 	&dev_attr_led.attr,
1102 	&dev_attr_snd.attr,
1103 	&dev_attr_ff.attr,
1104 	&dev_attr_sw.attr,
1105 	NULL
1106 };
1107 
1108 static struct attribute_group input_dev_caps_attr_group = {
1109 	.name	= "capabilities",
1110 	.attrs	= input_dev_caps_attrs,
1111 };
1112 
1113 static struct attribute_group *input_dev_attr_groups[] = {
1114 	&input_dev_attr_group,
1115 	&input_dev_id_attr_group,
1116 	&input_dev_caps_attr_group,
1117 	NULL
1118 };
1119 
1120 static void input_dev_release(struct device *device)
1121 {
1122 	struct input_dev *dev = to_input_dev(device);
1123 
1124 	input_ff_destroy(dev);
1125 	kfree(dev);
1126 
1127 	module_put(THIS_MODULE);
1128 }
1129 
1130 /*
1131  * Input uevent interface - loading event handlers based on
1132  * device bitfields.
1133  */
1134 static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
1135 				   const char *name, unsigned long *bitmap, int max)
1136 {
1137 	int len;
1138 
1139 	if (add_uevent_var(env, "%s=", name))
1140 		return -ENOMEM;
1141 
1142 	len = input_print_bitmap(&env->buf[env->buflen - 1],
1143 				 sizeof(env->buf) - env->buflen,
1144 				 bitmap, max, 0);
1145 	if (len >= (sizeof(env->buf) - env->buflen))
1146 		return -ENOMEM;
1147 
1148 	env->buflen += len;
1149 	return 0;
1150 }
1151 
1152 static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
1153 					 struct input_dev *dev)
1154 {
1155 	int len;
1156 
1157 	if (add_uevent_var(env, "MODALIAS="))
1158 		return -ENOMEM;
1159 
1160 	len = input_print_modalias(&env->buf[env->buflen - 1],
1161 				   sizeof(env->buf) - env->buflen,
1162 				   dev, 0);
1163 	if (len >= (sizeof(env->buf) - env->buflen))
1164 		return -ENOMEM;
1165 
1166 	env->buflen += len;
1167 	return 0;
1168 }
1169 
1170 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...)				\
1171 	do {								\
1172 		int err = add_uevent_var(env, fmt, val);		\
1173 		if (err)						\
1174 			return err;					\
1175 	} while (0)
1176 
1177 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max)				\
1178 	do {								\
1179 		int err = input_add_uevent_bm_var(env, name, bm, max);	\
1180 		if (err)						\
1181 			return err;					\
1182 	} while (0)
1183 
1184 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev)				\
1185 	do {								\
1186 		int err = input_add_uevent_modalias_var(env, dev);	\
1187 		if (err)						\
1188 			return err;					\
1189 	} while (0)
1190 
1191 static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1192 {
1193 	struct input_dev *dev = to_input_dev(device);
1194 
1195 	INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1196 				dev->id.bustype, dev->id.vendor,
1197 				dev->id.product, dev->id.version);
1198 	if (dev->name)
1199 		INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1200 	if (dev->phys)
1201 		INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
1202 	if (dev->uniq)
1203 		INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1204 
1205 	INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1206 	if (test_bit(EV_KEY, dev->evbit))
1207 		INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1208 	if (test_bit(EV_REL, dev->evbit))
1209 		INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1210 	if (test_bit(EV_ABS, dev->evbit))
1211 		INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1212 	if (test_bit(EV_MSC, dev->evbit))
1213 		INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1214 	if (test_bit(EV_LED, dev->evbit))
1215 		INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1216 	if (test_bit(EV_SND, dev->evbit))
1217 		INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1218 	if (test_bit(EV_FF, dev->evbit))
1219 		INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1220 	if (test_bit(EV_SW, dev->evbit))
1221 		INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1222 
1223 	INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
1224 
1225 	return 0;
1226 }
1227 
1228 static struct device_type input_dev_type = {
1229 	.groups		= input_dev_attr_groups,
1230 	.release	= input_dev_release,
1231 	.uevent		= input_dev_uevent,
1232 };
1233 
1234 struct class input_class = {
1235 	.name		= "input",
1236 };
1237 EXPORT_SYMBOL_GPL(input_class);
1238 
1239 /**
1240  * input_allocate_device - allocate memory for new input device
1241  *
1242  * Returns prepared struct input_dev or NULL.
1243  *
1244  * NOTE: Use input_free_device() to free devices that have not been
1245  * registered; input_unregister_device() should be used for already
1246  * registered devices.
1247  */
1248 struct input_dev *input_allocate_device(void)
1249 {
1250 	struct input_dev *dev;
1251 
1252 	dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1253 	if (dev) {
1254 		dev->dev.type = &input_dev_type;
1255 		dev->dev.class = &input_class;
1256 		device_initialize(&dev->dev);
1257 		mutex_init(&dev->mutex);
1258 		spin_lock_init(&dev->event_lock);
1259 		INIT_LIST_HEAD(&dev->h_list);
1260 		INIT_LIST_HEAD(&dev->node);
1261 
1262 		__module_get(THIS_MODULE);
1263 	}
1264 
1265 	return dev;
1266 }
1267 EXPORT_SYMBOL(input_allocate_device);
1268 
1269 /**
1270  * input_free_device - free memory occupied by input_dev structure
1271  * @dev: input device to free
1272  *
1273  * This function should only be used if input_register_device()
1274  * was not called yet or if it failed. Once device was registered
1275  * use input_unregister_device() and memory will be freed once last
1276  * reference to the device is dropped.
1277  *
1278  * Device should be allocated by input_allocate_device().
1279  *
1280  * NOTE: If there are references to the input device then memory
1281  * will not be freed until last reference is dropped.
1282  */
1283 void input_free_device(struct input_dev *dev)
1284 {
1285 	if (dev)
1286 		input_put_device(dev);
1287 }
1288 EXPORT_SYMBOL(input_free_device);
1289 
1290 /**
1291  * input_set_capability - mark device as capable of a certain event
1292  * @dev: device that is capable of emitting or accepting event
1293  * @type: type of the event (EV_KEY, EV_REL, etc...)
1294  * @code: event code
1295  *
1296  * In addition to setting up corresponding bit in appropriate capability
1297  * bitmap the function also adjusts dev->evbit.
1298  */
1299 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1300 {
1301 	switch (type) {
1302 	case EV_KEY:
1303 		__set_bit(code, dev->keybit);
1304 		break;
1305 
1306 	case EV_REL:
1307 		__set_bit(code, dev->relbit);
1308 		break;
1309 
1310 	case EV_ABS:
1311 		__set_bit(code, dev->absbit);
1312 		break;
1313 
1314 	case EV_MSC:
1315 		__set_bit(code, dev->mscbit);
1316 		break;
1317 
1318 	case EV_SW:
1319 		__set_bit(code, dev->swbit);
1320 		break;
1321 
1322 	case EV_LED:
1323 		__set_bit(code, dev->ledbit);
1324 		break;
1325 
1326 	case EV_SND:
1327 		__set_bit(code, dev->sndbit);
1328 		break;
1329 
1330 	case EV_FF:
1331 		__set_bit(code, dev->ffbit);
1332 		break;
1333 
1334 	case EV_PWR:
1335 		/* do nothing */
1336 		break;
1337 
1338 	default:
1339 		printk(KERN_ERR
1340 			"input_set_capability: unknown type %u (code %u)\n",
1341 			type, code);
1342 		dump_stack();
1343 		return;
1344 	}
1345 
1346 	__set_bit(type, dev->evbit);
1347 }
1348 EXPORT_SYMBOL(input_set_capability);
1349 
1350 /**
1351  * input_register_device - register device with input core
1352  * @dev: device to be registered
1353  *
1354  * This function registers device with input core. The device must be
1355  * allocated with input_allocate_device() and all it's capabilities
1356  * set up before registering.
1357  * If function fails the device must be freed with input_free_device().
1358  * Once device has been successfully registered it can be unregistered
1359  * with input_unregister_device(); input_free_device() should not be
1360  * called in this case.
1361  */
1362 int input_register_device(struct input_dev *dev)
1363 {
1364 	static atomic_t input_no = ATOMIC_INIT(0);
1365 	struct input_handler *handler;
1366 	const char *path;
1367 	int error;
1368 
1369 	__set_bit(EV_SYN, dev->evbit);
1370 
1371 	/*
1372 	 * If delay and period are pre-set by the driver, then autorepeating
1373 	 * is handled by the driver itself and we don't do it in input.c.
1374 	 */
1375 
1376 	init_timer(&dev->timer);
1377 	if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1378 		dev->timer.data = (long) dev;
1379 		dev->timer.function = input_repeat_key;
1380 		dev->rep[REP_DELAY] = 250;
1381 		dev->rep[REP_PERIOD] = 33;
1382 	}
1383 
1384 	if (!dev->getkeycode)
1385 		dev->getkeycode = input_default_getkeycode;
1386 
1387 	if (!dev->setkeycode)
1388 		dev->setkeycode = input_default_setkeycode;
1389 
1390 	dev_set_name(&dev->dev, "input%ld",
1391 		     (unsigned long) atomic_inc_return(&input_no) - 1);
1392 
1393 	error = device_add(&dev->dev);
1394 	if (error)
1395 		return error;
1396 
1397 	path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1398 	printk(KERN_INFO "input: %s as %s\n",
1399 		dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1400 	kfree(path);
1401 
1402 	error = mutex_lock_interruptible(&input_mutex);
1403 	if (error) {
1404 		device_del(&dev->dev);
1405 		return error;
1406 	}
1407 
1408 	list_add_tail(&dev->node, &input_dev_list);
1409 
1410 	list_for_each_entry(handler, &input_handler_list, node)
1411 		input_attach_handler(dev, handler);
1412 
1413 	input_wakeup_procfs_readers();
1414 
1415 	mutex_unlock(&input_mutex);
1416 
1417 	return 0;
1418 }
1419 EXPORT_SYMBOL(input_register_device);
1420 
1421 /**
1422  * input_unregister_device - unregister previously registered device
1423  * @dev: device to be unregistered
1424  *
1425  * This function unregisters an input device. Once device is unregistered
1426  * the caller should not try to access it as it may get freed at any moment.
1427  */
1428 void input_unregister_device(struct input_dev *dev)
1429 {
1430 	struct input_handle *handle, *next;
1431 
1432 	input_disconnect_device(dev);
1433 
1434 	mutex_lock(&input_mutex);
1435 
1436 	list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1437 		handle->handler->disconnect(handle);
1438 	WARN_ON(!list_empty(&dev->h_list));
1439 
1440 	del_timer_sync(&dev->timer);
1441 	list_del_init(&dev->node);
1442 
1443 	input_wakeup_procfs_readers();
1444 
1445 	mutex_unlock(&input_mutex);
1446 
1447 	device_unregister(&dev->dev);
1448 }
1449 EXPORT_SYMBOL(input_unregister_device);
1450 
1451 /**
1452  * input_register_handler - register a new input handler
1453  * @handler: handler to be registered
1454  *
1455  * This function registers a new input handler (interface) for input
1456  * devices in the system and attaches it to all input devices that
1457  * are compatible with the handler.
1458  */
1459 int input_register_handler(struct input_handler *handler)
1460 {
1461 	struct input_dev *dev;
1462 	int retval;
1463 
1464 	retval = mutex_lock_interruptible(&input_mutex);
1465 	if (retval)
1466 		return retval;
1467 
1468 	INIT_LIST_HEAD(&handler->h_list);
1469 
1470 	if (handler->fops != NULL) {
1471 		if (input_table[handler->minor >> 5]) {
1472 			retval = -EBUSY;
1473 			goto out;
1474 		}
1475 		input_table[handler->minor >> 5] = handler;
1476 	}
1477 
1478 	list_add_tail(&handler->node, &input_handler_list);
1479 
1480 	list_for_each_entry(dev, &input_dev_list, node)
1481 		input_attach_handler(dev, handler);
1482 
1483 	input_wakeup_procfs_readers();
1484 
1485  out:
1486 	mutex_unlock(&input_mutex);
1487 	return retval;
1488 }
1489 EXPORT_SYMBOL(input_register_handler);
1490 
1491 /**
1492  * input_unregister_handler - unregisters an input handler
1493  * @handler: handler to be unregistered
1494  *
1495  * This function disconnects a handler from its input devices and
1496  * removes it from lists of known handlers.
1497  */
1498 void input_unregister_handler(struct input_handler *handler)
1499 {
1500 	struct input_handle *handle, *next;
1501 
1502 	mutex_lock(&input_mutex);
1503 
1504 	list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1505 		handler->disconnect(handle);
1506 	WARN_ON(!list_empty(&handler->h_list));
1507 
1508 	list_del_init(&handler->node);
1509 
1510 	if (handler->fops != NULL)
1511 		input_table[handler->minor >> 5] = NULL;
1512 
1513 	input_wakeup_procfs_readers();
1514 
1515 	mutex_unlock(&input_mutex);
1516 }
1517 EXPORT_SYMBOL(input_unregister_handler);
1518 
1519 /**
1520  * input_register_handle - register a new input handle
1521  * @handle: handle to register
1522  *
1523  * This function puts a new input handle onto device's
1524  * and handler's lists so that events can flow through
1525  * it once it is opened using input_open_device().
1526  *
1527  * This function is supposed to be called from handler's
1528  * connect() method.
1529  */
1530 int input_register_handle(struct input_handle *handle)
1531 {
1532 	struct input_handler *handler = handle->handler;
1533 	struct input_dev *dev = handle->dev;
1534 	int error;
1535 
1536 	/*
1537 	 * We take dev->mutex here to prevent race with
1538 	 * input_release_device().
1539 	 */
1540 	error = mutex_lock_interruptible(&dev->mutex);
1541 	if (error)
1542 		return error;
1543 	list_add_tail_rcu(&handle->d_node, &dev->h_list);
1544 	mutex_unlock(&dev->mutex);
1545 	synchronize_rcu();
1546 
1547 	/*
1548 	 * Since we are supposed to be called from ->connect()
1549 	 * which is mutually exclusive with ->disconnect()
1550 	 * we can't be racing with input_unregister_handle()
1551 	 * and so separate lock is not needed here.
1552 	 */
1553 	list_add_tail(&handle->h_node, &handler->h_list);
1554 
1555 	if (handler->start)
1556 		handler->start(handle);
1557 
1558 	return 0;
1559 }
1560 EXPORT_SYMBOL(input_register_handle);
1561 
1562 /**
1563  * input_unregister_handle - unregister an input handle
1564  * @handle: handle to unregister
1565  *
1566  * This function removes input handle from device's
1567  * and handler's lists.
1568  *
1569  * This function is supposed to be called from handler's
1570  * disconnect() method.
1571  */
1572 void input_unregister_handle(struct input_handle *handle)
1573 {
1574 	struct input_dev *dev = handle->dev;
1575 
1576 	list_del_init(&handle->h_node);
1577 
1578 	/*
1579 	 * Take dev->mutex to prevent race with input_release_device().
1580 	 */
1581 	mutex_lock(&dev->mutex);
1582 	list_del_rcu(&handle->d_node);
1583 	mutex_unlock(&dev->mutex);
1584 	synchronize_rcu();
1585 }
1586 EXPORT_SYMBOL(input_unregister_handle);
1587 
1588 static int input_open_file(struct inode *inode, struct file *file)
1589 {
1590 	struct input_handler *handler;
1591 	const struct file_operations *old_fops, *new_fops = NULL;
1592 	int err;
1593 
1594 	lock_kernel();
1595 	/* No load-on-demand here? */
1596 	handler = input_table[iminor(inode) >> 5];
1597 	if (!handler || !(new_fops = fops_get(handler->fops))) {
1598 		err = -ENODEV;
1599 		goto out;
1600 	}
1601 
1602 	/*
1603 	 * That's _really_ odd. Usually NULL ->open means "nothing special",
1604 	 * not "no device". Oh, well...
1605 	 */
1606 	if (!new_fops->open) {
1607 		fops_put(new_fops);
1608 		err = -ENODEV;
1609 		goto out;
1610 	}
1611 	old_fops = file->f_op;
1612 	file->f_op = new_fops;
1613 
1614 	err = new_fops->open(inode, file);
1615 
1616 	if (err) {
1617 		fops_put(file->f_op);
1618 		file->f_op = fops_get(old_fops);
1619 	}
1620 	fops_put(old_fops);
1621 out:
1622 	unlock_kernel();
1623 	return err;
1624 }
1625 
1626 static const struct file_operations input_fops = {
1627 	.owner = THIS_MODULE,
1628 	.open = input_open_file,
1629 };
1630 
1631 static int __init input_init(void)
1632 {
1633 	int err;
1634 
1635 	err = class_register(&input_class);
1636 	if (err) {
1637 		printk(KERN_ERR "input: unable to register input_dev class\n");
1638 		return err;
1639 	}
1640 
1641 	err = input_proc_init();
1642 	if (err)
1643 		goto fail1;
1644 
1645 	err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1646 	if (err) {
1647 		printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1648 		goto fail2;
1649 	}
1650 
1651 	return 0;
1652 
1653  fail2:	input_proc_exit();
1654  fail1:	class_unregister(&input_class);
1655 	return err;
1656 }
1657 
1658 static void __exit input_exit(void)
1659 {
1660 	input_proc_exit();
1661 	unregister_chrdev(INPUT_MAJOR, "input");
1662 	class_unregister(&input_class);
1663 }
1664 
1665 subsys_initcall(input_init);
1666 module_exit(input_exit);
1667