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