Input: Make sure we follow all EV_KEY events
[deliverable/linux.git] / drivers / input / input.c
CommitLineData
1da177e4
LT
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
da0c4901
JP
13#define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
14
1da177e4 15#include <linux/init.h>
ffd0db97 16#include <linux/types.h>
47c78e89 17#include <linux/input/mt.h>
1da177e4 18#include <linux/module.h>
5a0e3ad6 19#include <linux/slab.h>
1da177e4
LT
20#include <linux/random.h>
21#include <linux/major.h>
22#include <linux/proc_fs.h>
a99bbaf5 23#include <linux/sched.h>
969b21cd 24#include <linux/seq_file.h>
1da177e4
LT
25#include <linux/poll.h>
26#include <linux/device.h>
e676c232 27#include <linux/mutex.h>
8006479c 28#include <linux/rcupdate.h>
15e184af 29#include "input-compat.h"
1da177e4
LT
30
31MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
32MODULE_DESCRIPTION("Input core");
33MODULE_LICENSE("GPL");
34
1da177e4
LT
35#define INPUT_DEVICES 256
36
37static LIST_HEAD(input_dev_list);
38static LIST_HEAD(input_handler_list);
39
8006479c
DT
40/*
41 * input_mutex protects access to both input_dev_list and input_handler_list.
42 * This also causes input_[un]register_device and input_[un]register_handler
43 * be mutually exclusive which simplifies locking in drivers implementing
44 * input handlers.
45 */
46static DEFINE_MUTEX(input_mutex);
47
1da177e4
LT
48static struct input_handler *input_table[8];
49
8006479c
DT
50static inline int is_event_supported(unsigned int code,
51 unsigned long *bm, unsigned int max)
1da177e4 52{
8006479c
DT
53 return code <= max && test_bit(code, bm);
54}
1da177e4 55
8006479c
DT
56static int input_defuzz_abs_event(int value, int old_val, int fuzz)
57{
58 if (fuzz) {
59 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
60 return old_val;
1da177e4 61
8006479c
DT
62 if (value > old_val - fuzz && value < old_val + fuzz)
63 return (old_val * 3 + value) / 4;
1da177e4 64
8006479c
DT
65 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
66 return (old_val + value) / 2;
67 }
1da177e4 68
8006479c
DT
69 return value;
70}
1da177e4 71
8006479c 72/*
ef7995f4
DT
73 * Pass event first through all filters and then, if event has not been
74 * filtered out, through all open handles. This function is called with
82ba56c2 75 * dev->event_lock held and interrupts disabled.
8006479c
DT
76 */
77static void input_pass_event(struct input_dev *dev,
78 unsigned int type, unsigned int code, int value)
79{
ef7995f4 80 struct input_handler *handler;
82ba56c2
DT
81 struct input_handle *handle;
82
83 rcu_read_lock();
1da177e4 84
82ba56c2 85 handle = rcu_dereference(dev->grab);
8006479c
DT
86 if (handle)
87 handle->handler->event(handle, type, code, value);
ef7995f4
DT
88 else {
89 bool filtered = false;
90
91 list_for_each_entry_rcu(handle, &dev->h_list, d_node) {
92 if (!handle->open)
93 continue;
94
95 handler = handle->handler;
96 if (!handler->filter) {
97 if (filtered)
98 break;
99
100 handler->event(handle, type, code, value);
101
102 } else if (handler->filter(handle, type, code, value))
103 filtered = true;
104 }
105 }
106
82ba56c2 107 rcu_read_unlock();
8006479c 108}
1da177e4 109
8006479c
DT
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 */
115static void input_repeat_key(unsigned long data)
116{
117 struct input_dev *dev = (void *) data;
118 unsigned long flags;
1da177e4 119
8006479c 120 spin_lock_irqsave(&dev->event_lock, flags);
1da177e4 121
8006479c
DT
122 if (test_bit(dev->repeat_key, dev->key) &&
123 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
1da177e4 124
9ae4345a 125 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
1da177e4 126
8006479c
DT
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 */
9ae4345a 134 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
8006479c 135 }
31581066 136
8006479c
DT
137 if (dev->rep[REP_PERIOD])
138 mod_timer(&dev->timer, jiffies +
139 msecs_to_jiffies(dev->rep[REP_PERIOD]));
140 }
31581066 141
8006479c
DT
142 spin_unlock_irqrestore(&dev->event_lock, flags);
143}
31581066 144
8006479c
DT
145static 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}
31581066 155
e7b5c1ef
JB
156static void input_stop_autorepeat(struct input_dev *dev)
157{
158 del_timer(&dev->timer);
159}
160
8006479c
DT
161#define INPUT_IGNORE_EVENT 0
162#define INPUT_PASS_TO_HANDLERS 1
163#define INPUT_PASS_TO_DEVICE 2
164#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
1da177e4 165
40d007e7
HR
166static int input_handle_abs_event(struct input_dev *dev,
167 unsigned int code, int *pval)
168{
8d18fba2 169 struct input_mt *mt = dev->mt;
40d007e7
HR
170 bool is_mt_event;
171 int *pold;
172
173 if (code == ABS_MT_SLOT) {
174 /*
175 * "Stage" the event; we'll flush it later, when we
144c0f88 176 * get actual touch data.
40d007e7 177 */
8d18fba2
HR
178 if (mt && *pval >= 0 && *pval < mt->num_slots)
179 mt->slot = *pval;
40d007e7
HR
180
181 return INPUT_IGNORE_EVENT;
182 }
183
b89529a1 184 is_mt_event = input_is_mt_value(code);
40d007e7
HR
185
186 if (!is_mt_event) {
d31b2865 187 pold = &dev->absinfo[code].value;
8d18fba2
HR
188 } else if (mt) {
189 pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST];
40d007e7
HR
190 } else {
191 /*
144c0f88 192 * Bypass filtering for multi-touch events when
40d007e7
HR
193 * not employing slots.
194 */
195 pold = NULL;
196 }
197
198 if (pold) {
199 *pval = input_defuzz_abs_event(*pval, *pold,
d31b2865 200 dev->absinfo[code].fuzz);
40d007e7
HR
201 if (*pold == *pval)
202 return INPUT_IGNORE_EVENT;
203
204 *pold = *pval;
205 }
206
207 /* Flush pending "slot" event */
8d18fba2
HR
208 if (is_mt_event && mt && mt->slot != input_abs_get_val(dev, ABS_MT_SLOT)) {
209 input_abs_set_val(dev, ABS_MT_SLOT, mt->slot);
210 input_pass_event(dev, EV_ABS, ABS_MT_SLOT, mt->slot);
40d007e7
HR
211 }
212
213 return INPUT_PASS_TO_HANDLERS;
214}
215
8006479c
DT
216static void input_handle_event(struct input_dev *dev,
217 unsigned int type, unsigned int code, int value)
218{
219 int disposition = INPUT_IGNORE_EVENT;
1da177e4 220
8006479c 221 switch (type) {
1da177e4 222
8006479c
DT
223 case EV_SYN:
224 switch (code) {
225 case SYN_CONFIG:
226 disposition = INPUT_PASS_TO_ALL;
227 break;
1da177e4 228
8006479c
DT
229 case SYN_REPORT:
230 if (!dev->sync) {
20da92de 231 dev->sync = true;
8006479c 232 disposition = INPUT_PASS_TO_HANDLERS;
1da177e4 233 }
1da177e4 234 break;
5e5ee686 235 case SYN_MT_REPORT:
20da92de 236 dev->sync = false;
5e5ee686
HR
237 disposition = INPUT_PASS_TO_HANDLERS;
238 break;
8006479c
DT
239 }
240 break;
1da177e4 241
8006479c 242 case EV_KEY:
0672120a
HR
243 if (is_event_supported(code, dev->keybit, KEY_MAX)) {
244
245 /* auto-repeat bypasses state updates */
246 if (value == 2) {
247 disposition = INPUT_PASS_TO_HANDLERS;
248 break;
249 }
250
251 if (!!test_bit(code, dev->key) != !!value) {
1da177e4 252
8006479c 253 __change_bit(code, dev->key);
0672120a
HR
254 disposition = INPUT_PASS_TO_HANDLERS;
255
8006479c
DT
256 if (value)
257 input_start_autorepeat(dev, code);
e7b5c1ef
JB
258 else
259 input_stop_autorepeat(dev);
8006479c 260 }
8006479c
DT
261 }
262 break;
1da177e4 263
8006479c
DT
264 case EV_SW:
265 if (is_event_supported(code, dev->swbit, SW_MAX) &&
0672120a 266 !!test_bit(code, dev->sw) != !!value) {
1da177e4 267
8006479c
DT
268 __change_bit(code, dev->sw);
269 disposition = INPUT_PASS_TO_HANDLERS;
270 }
271 break;
1da177e4 272
8006479c 273 case EV_ABS:
40d007e7 274 if (is_event_supported(code, dev->absbit, ABS_MAX))
9ae4345a 275 disposition = input_handle_abs_event(dev, code, &value);
61994a61 276
8006479c 277 break;
1da177e4 278
8006479c
DT
279 case EV_REL:
280 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
281 disposition = INPUT_PASS_TO_HANDLERS;
1da177e4 282
8006479c 283 break;
1e0afb28 284
8006479c
DT
285 case EV_MSC:
286 if (is_event_supported(code, dev->mscbit, MSC_MAX))
287 disposition = INPUT_PASS_TO_ALL;
1da177e4 288
8006479c 289 break;
1da177e4 290
8006479c
DT
291 case EV_LED:
292 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
0672120a 293 !!test_bit(code, dev->led) != !!value) {
1da177e4 294
8006479c
DT
295 __change_bit(code, dev->led);
296 disposition = INPUT_PASS_TO_ALL;
297 }
298 break;
299
300 case EV_SND:
301 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
1da177e4 302
8fdc1948 303 if (!!test_bit(code, dev->snd) != !!value)
8006479c
DT
304 __change_bit(code, dev->snd);
305 disposition = INPUT_PASS_TO_ALL;
306 }
307 break;
8fdc1948 308
8006479c
DT
309 case EV_REP:
310 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
311 dev->rep[code] = value;
312 disposition = INPUT_PASS_TO_ALL;
313 }
314 break;
1da177e4 315
8006479c
DT
316 case EV_FF:
317 if (value >= 0)
318 disposition = INPUT_PASS_TO_ALL;
319 break;
ed2fa4dd
RP
320
321 case EV_PWR:
322 disposition = INPUT_PASS_TO_ALL;
323 break;
8006479c 324 }
1da177e4 325
c9812282 326 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
20da92de 327 dev->sync = false;
1da177e4 328
8006479c
DT
329 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
330 dev->event(dev, type, code, value);
1da177e4 331
8006479c 332 if (disposition & INPUT_PASS_TO_HANDLERS)
9ae4345a 333 input_pass_event(dev, type, code, value);
8006479c 334}
1da177e4 335
8006479c
DT
336/**
337 * input_event() - report new input event
338 * @dev: device that generated the event
339 * @type: type of the event
340 * @code: event code
341 * @value: value of the event
342 *
343 * This function should be used by drivers implementing various input
df2d4637
DT
344 * devices to report input events. See also input_inject_event().
345 *
346 * NOTE: input_event() may be safely used right after input device was
347 * allocated with input_allocate_device(), even before it is registered
348 * with input_register_device(), but the event will not reach any of the
349 * input handlers. Such early invocation of input_event() may be used
350 * to 'seed' initial state of a switch or initial position of absolute
351 * axis, etc.
8006479c 352 */
8006479c
DT
353void input_event(struct input_dev *dev,
354 unsigned int type, unsigned int code, int value)
355{
356 unsigned long flags;
509ca1a9 357
8006479c 358 if (is_event_supported(type, dev->evbit, EV_MAX)) {
509ca1a9 359
8006479c
DT
360 spin_lock_irqsave(&dev->event_lock, flags);
361 add_input_randomness(type, code, value);
9ae4345a 362 input_handle_event(dev, type, code, value);
8006479c 363 spin_unlock_irqrestore(&dev->event_lock, flags);
1da177e4 364 }
1da177e4 365}
ca56fe07 366EXPORT_SYMBOL(input_event);
1da177e4 367
0e739d28
DT
368/**
369 * input_inject_event() - send input event from input handler
370 * @handle: input handle to send event through
371 * @type: type of the event
372 * @code: event code
373 * @value: value of the event
374 *
8006479c
DT
375 * Similar to input_event() but will ignore event if device is
376 * "grabbed" and handle injecting event is not the one that owns
377 * the device.
0e739d28 378 */
8006479c
DT
379void input_inject_event(struct input_handle *handle,
380 unsigned int type, unsigned int code, int value)
1da177e4 381{
8006479c
DT
382 struct input_dev *dev = handle->dev;
383 struct input_handle *grab;
384 unsigned long flags;
1da177e4 385
8006479c
DT
386 if (is_event_supported(type, dev->evbit, EV_MAX)) {
387 spin_lock_irqsave(&dev->event_lock, flags);
1da177e4 388
82ba56c2 389 rcu_read_lock();
8006479c
DT
390 grab = rcu_dereference(dev->grab);
391 if (!grab || grab == handle)
9ae4345a 392 input_handle_event(dev, type, code, value);
82ba56c2 393 rcu_read_unlock();
1da177e4 394
8006479c
DT
395 spin_unlock_irqrestore(&dev->event_lock, flags);
396 }
1da177e4 397}
8006479c 398EXPORT_SYMBOL(input_inject_event);
1da177e4 399
d31b2865
DM
400/**
401 * input_alloc_absinfo - allocates array of input_absinfo structs
402 * @dev: the input device emitting absolute events
403 *
404 * If the absinfo struct the caller asked for is already allocated, this
405 * functions will not do anything.
406 */
407void input_alloc_absinfo(struct input_dev *dev)
408{
409 if (!dev->absinfo)
410 dev->absinfo = kcalloc(ABS_CNT, sizeof(struct input_absinfo),
411 GFP_KERNEL);
412
413 WARN(!dev->absinfo, "%s(): kcalloc() failed?\n", __func__);
414}
415EXPORT_SYMBOL(input_alloc_absinfo);
416
417void input_set_abs_params(struct input_dev *dev, unsigned int axis,
418 int min, int max, int fuzz, int flat)
419{
420 struct input_absinfo *absinfo;
421
422 input_alloc_absinfo(dev);
423 if (!dev->absinfo)
424 return;
425
426 absinfo = &dev->absinfo[axis];
427 absinfo->minimum = min;
428 absinfo->maximum = max;
429 absinfo->fuzz = fuzz;
430 absinfo->flat = flat;
431
432 dev->absbit[BIT_WORD(axis)] |= BIT_MASK(axis);
433}
434EXPORT_SYMBOL(input_set_abs_params);
435
436
8006479c
DT
437/**
438 * input_grab_device - grabs device for exclusive use
439 * @handle: input handle that wants to own the device
440 *
441 * When a device is grabbed by an input handle all events generated by
442 * the device are delivered only to this handle. Also events injected
443 * by other input handles are ignored while device is grabbed.
444 */
1da177e4
LT
445int input_grab_device(struct input_handle *handle)
446{
8006479c
DT
447 struct input_dev *dev = handle->dev;
448 int retval;
1da177e4 449
8006479c
DT
450 retval = mutex_lock_interruptible(&dev->mutex);
451 if (retval)
452 return retval;
453
454 if (dev->grab) {
455 retval = -EBUSY;
456 goto out;
457 }
458
459 rcu_assign_pointer(dev->grab, handle);
8006479c
DT
460
461 out:
462 mutex_unlock(&dev->mutex);
463 return retval;
1da177e4 464}
ca56fe07 465EXPORT_SYMBOL(input_grab_device);
1da177e4 466
8006479c 467static void __input_release_device(struct input_handle *handle)
1da177e4 468{
a2b2ed2c 469 struct input_dev *dev = handle->dev;
c7e8dc6e 470
a2b2ed2c 471 if (dev->grab == handle) {
8006479c
DT
472 rcu_assign_pointer(dev->grab, NULL);
473 /* Make sure input_pass_event() notices that grab is gone */
82ba56c2 474 synchronize_rcu();
a2b2ed2c
AM
475
476 list_for_each_entry(handle, &dev->h_list, d_node)
8006479c 477 if (handle->open && handle->handler->start)
c7e8dc6e
DT
478 handle->handler->start(handle);
479 }
1da177e4 480}
8006479c
DT
481
482/**
483 * input_release_device - release previously grabbed device
484 * @handle: input handle that owns the device
485 *
486 * Releases previously grabbed device so that other input handles can
487 * start receiving input events. Upon release all handlers attached
488 * to the device have their start() method called so they have a change
489 * to synchronize device state with the rest of the system.
490 */
491void input_release_device(struct input_handle *handle)
492{
493 struct input_dev *dev = handle->dev;
494
495 mutex_lock(&dev->mutex);
496 __input_release_device(handle);
497 mutex_unlock(&dev->mutex);
498}
ca56fe07 499EXPORT_SYMBOL(input_release_device);
1da177e4 500
8006479c
DT
501/**
502 * input_open_device - open input device
503 * @handle: handle through which device is being accessed
504 *
505 * This function should be called by input handlers when they
506 * want to start receive events from given input device.
507 */
1da177e4
LT
508int input_open_device(struct input_handle *handle)
509{
0fbf87ca 510 struct input_dev *dev = handle->dev;
8006479c 511 int retval;
0fbf87ca 512
8006479c
DT
513 retval = mutex_lock_interruptible(&dev->mutex);
514 if (retval)
515 return retval;
516
517 if (dev->going_away) {
518 retval = -ENODEV;
519 goto out;
520 }
0fbf87ca 521
1da177e4 522 handle->open++;
0fbf87ca
DT
523
524 if (!dev->users++ && dev->open)
8006479c
DT
525 retval = dev->open(dev);
526
527 if (retval) {
528 dev->users--;
529 if (!--handle->open) {
530 /*
531 * Make sure we are not delivering any more events
532 * through this handle
533 */
82ba56c2 534 synchronize_rcu();
8006479c
DT
535 }
536 }
0fbf87ca 537
8006479c 538 out:
e676c232 539 mutex_unlock(&dev->mutex);
8006479c 540 return retval;
1da177e4 541}
ca56fe07 542EXPORT_SYMBOL(input_open_device);
1da177e4 543
8006479c 544int input_flush_device(struct input_handle *handle, struct file *file)
1da177e4 545{
8006479c
DT
546 struct input_dev *dev = handle->dev;
547 int retval;
1da177e4 548
8006479c
DT
549 retval = mutex_lock_interruptible(&dev->mutex);
550 if (retval)
551 return retval;
552
553 if (dev->flush)
554 retval = dev->flush(dev, file);
555
556 mutex_unlock(&dev->mutex);
557 return retval;
1da177e4 558}
ca56fe07 559EXPORT_SYMBOL(input_flush_device);
1da177e4 560
8006479c
DT
561/**
562 * input_close_device - close input device
563 * @handle: handle through which device is being accessed
564 *
565 * This function should be called by input handlers when they
566 * want to stop receive events from given input device.
567 */
1da177e4
LT
568void input_close_device(struct input_handle *handle)
569{
0fbf87ca
DT
570 struct input_dev *dev = handle->dev;
571
e676c232 572 mutex_lock(&dev->mutex);
0fbf87ca 573
8006479c
DT
574 __input_release_device(handle);
575
0fbf87ca
DT
576 if (!--dev->users && dev->close)
577 dev->close(dev);
8006479c
DT
578
579 if (!--handle->open) {
580 /*
82ba56c2 581 * synchronize_rcu() makes sure that input_pass_event()
8006479c
DT
582 * completed and that no more input events are delivered
583 * through this handle
584 */
82ba56c2 585 synchronize_rcu();
8006479c 586 }
0fbf87ca 587
e676c232 588 mutex_unlock(&dev->mutex);
1da177e4 589}
ca56fe07 590EXPORT_SYMBOL(input_close_device);
1da177e4 591
866d7d7b
ON
592/*
593 * Simulate keyup events for all keys that are marked as pressed.
594 * The function must be called with dev->event_lock held.
595 */
596static void input_dev_release_keys(struct input_dev *dev)
597{
598 int code;
599
600 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
601 for (code = 0; code <= KEY_MAX; code++) {
602 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
603 __test_and_clear_bit(code, dev->key)) {
9ae4345a 604 input_pass_event(dev, EV_KEY, code, 0);
866d7d7b
ON
605 }
606 }
9ae4345a 607 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
866d7d7b
ON
608 }
609}
610
8006479c
DT
611/*
612 * Prepare device for unregistering
613 */
614static void input_disconnect_device(struct input_dev *dev)
615{
616 struct input_handle *handle;
8006479c
DT
617
618 /*
619 * Mark device as going away. Note that we take dev->mutex here
620 * not to protect access to dev->going_away but rather to ensure
621 * that there are no threads in the middle of input_open_device()
622 */
623 mutex_lock(&dev->mutex);
ffd0db97 624 dev->going_away = true;
8006479c
DT
625 mutex_unlock(&dev->mutex);
626
627 spin_lock_irq(&dev->event_lock);
628
629 /*
630 * Simulate keyup events for all pressed keys so that handlers
631 * are not left with "stuck" keys. The driver may continue
632 * generate events even after we done here but they will not
633 * reach any handlers.
634 */
866d7d7b 635 input_dev_release_keys(dev);
8006479c
DT
636
637 list_for_each_entry(handle, &dev->h_list, d_node)
638 handle->open = 0;
639
640 spin_unlock_irq(&dev->event_lock);
641}
642
8613e4c2
MCC
643/**
644 * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry
645 * @ke: keymap entry containing scancode to be converted.
646 * @scancode: pointer to the location where converted scancode should
647 * be stored.
648 *
649 * This function is used to convert scancode stored in &struct keymap_entry
650 * into scalar form understood by legacy keymap handling methods. These
651 * methods expect scancodes to be represented as 'unsigned int'.
652 */
653int input_scancode_to_scalar(const struct input_keymap_entry *ke,
654 unsigned int *scancode)
655{
656 switch (ke->len) {
657 case 1:
658 *scancode = *((u8 *)ke->scancode);
659 break;
660
661 case 2:
662 *scancode = *((u16 *)ke->scancode);
663 break;
664
665 case 4:
666 *scancode = *((u32 *)ke->scancode);
667 break;
668
669 default:
670 return -EINVAL;
671 }
672
673 return 0;
674}
675EXPORT_SYMBOL(input_scancode_to_scalar);
676
677/*
678 * Those routines handle the default case where no [gs]etkeycode() is
679 * defined. In this case, an array indexed by the scancode is used.
680 */
681
682static unsigned int input_fetch_keycode(struct input_dev *dev,
683 unsigned int index)
c8e4c772
MR
684{
685 switch (dev->keycodesize) {
8613e4c2
MCC
686 case 1:
687 return ((u8 *)dev->keycode)[index];
c8e4c772 688
8613e4c2
MCC
689 case 2:
690 return ((u16 *)dev->keycode)[index];
c8e4c772 691
8613e4c2
MCC
692 default:
693 return ((u32 *)dev->keycode)[index];
c8e4c772
MR
694 }
695}
696
697static int input_default_getkeycode(struct input_dev *dev,
8613e4c2 698 struct input_keymap_entry *ke)
c8e4c772 699{
8613e4c2
MCC
700 unsigned int index;
701 int error;
702
c8e4c772
MR
703 if (!dev->keycodesize)
704 return -EINVAL;
705
8613e4c2
MCC
706 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
707 index = ke->index;
708 else {
709 error = input_scancode_to_scalar(ke, &index);
710 if (error)
711 return error;
712 }
713
714 if (index >= dev->keycodemax)
c8e4c772
MR
715 return -EINVAL;
716
8613e4c2
MCC
717 ke->keycode = input_fetch_keycode(dev, index);
718 ke->index = index;
719 ke->len = sizeof(index);
720 memcpy(ke->scancode, &index, sizeof(index));
c8e4c772
MR
721
722 return 0;
723}
724
725static int input_default_setkeycode(struct input_dev *dev,
8613e4c2
MCC
726 const struct input_keymap_entry *ke,
727 unsigned int *old_keycode)
c8e4c772 728{
8613e4c2
MCC
729 unsigned int index;
730 int error;
c8e4c772
MR
731 int i;
732
8613e4c2 733 if (!dev->keycodesize)
c8e4c772
MR
734 return -EINVAL;
735
8613e4c2
MCC
736 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
737 index = ke->index;
738 } else {
739 error = input_scancode_to_scalar(ke, &index);
740 if (error)
741 return error;
742 }
743
744 if (index >= dev->keycodemax)
c8e4c772
MR
745 return -EINVAL;
746
de391d12 747 if (dev->keycodesize < sizeof(ke->keycode) &&
8613e4c2 748 (ke->keycode >> (dev->keycodesize * 8)))
c8e4c772
MR
749 return -EINVAL;
750
751 switch (dev->keycodesize) {
752 case 1: {
753 u8 *k = (u8 *)dev->keycode;
8613e4c2
MCC
754 *old_keycode = k[index];
755 k[index] = ke->keycode;
c8e4c772
MR
756 break;
757 }
758 case 2: {
759 u16 *k = (u16 *)dev->keycode;
8613e4c2
MCC
760 *old_keycode = k[index];
761 k[index] = ke->keycode;
c8e4c772
MR
762 break;
763 }
764 default: {
765 u32 *k = (u32 *)dev->keycode;
8613e4c2
MCC
766 *old_keycode = k[index];
767 k[index] = ke->keycode;
c8e4c772
MR
768 break;
769 }
770 }
771
8613e4c2
MCC
772 __clear_bit(*old_keycode, dev->keybit);
773 __set_bit(ke->keycode, dev->keybit);
c8e4c772
MR
774
775 for (i = 0; i < dev->keycodemax; i++) {
8613e4c2
MCC
776 if (input_fetch_keycode(dev, i) == *old_keycode) {
777 __set_bit(*old_keycode, dev->keybit);
c8e4c772
MR
778 break; /* Setting the bit twice is useless, so break */
779 }
780 }
781
782 return 0;
783}
784
f4f37c8e
DT
785/**
786 * input_get_keycode - retrieve keycode currently mapped to a given scancode
787 * @dev: input device which keymap is being queried
8613e4c2 788 * @ke: keymap entry
f4f37c8e
DT
789 *
790 * This function should be called by anyone interested in retrieving current
8613e4c2 791 * keymap. Presently evdev handlers use it.
f4f37c8e 792 */
8613e4c2 793int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)
f4f37c8e 794{
2e2e3b96
DT
795 unsigned long flags;
796 int retval;
797
798 spin_lock_irqsave(&dev->event_lock, flags);
aebd636b 799 retval = dev->getkeycode(dev, ke);
8613e4c2 800 spin_unlock_irqrestore(&dev->event_lock, flags);
aebd636b 801
2e2e3b96 802 return retval;
f4f37c8e
DT
803}
804EXPORT_SYMBOL(input_get_keycode);
805
806/**
8613e4c2 807 * input_set_keycode - attribute a keycode to a given scancode
f4f37c8e 808 * @dev: input device which keymap is being updated
8613e4c2 809 * @ke: new keymap entry
f4f37c8e
DT
810 *
811 * This function should be called by anyone needing to update current
812 * keymap. Presently keyboard and evdev handlers use it.
813 */
58b93995 814int input_set_keycode(struct input_dev *dev,
8613e4c2 815 const struct input_keymap_entry *ke)
f4f37c8e
DT
816{
817 unsigned long flags;
fd6cf3dd 818 unsigned int old_keycode;
f4f37c8e
DT
819 int retval;
820
8613e4c2 821 if (ke->keycode > KEY_MAX)
f4f37c8e
DT
822 return -EINVAL;
823
824 spin_lock_irqsave(&dev->event_lock, flags);
825
aebd636b 826 retval = dev->setkeycode(dev, ke, &old_keycode);
f4f37c8e
DT
827 if (retval)
828 goto out;
829
4f93df40
DT
830 /* Make sure KEY_RESERVED did not get enabled. */
831 __clear_bit(KEY_RESERVED, dev->keybit);
832
f4f37c8e
DT
833 /*
834 * Simulate keyup event if keycode is not present
835 * in the keymap anymore
836 */
837 if (test_bit(EV_KEY, dev->evbit) &&
838 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
839 __test_and_clear_bit(old_keycode, dev->key)) {
840
9ae4345a 841 input_pass_event(dev, EV_KEY, old_keycode, 0);
f4f37c8e 842 if (dev->sync)
9ae4345a 843 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
f4f37c8e
DT
844 }
845
846 out:
847 spin_unlock_irqrestore(&dev->event_lock, flags);
848
849 return retval;
850}
851EXPORT_SYMBOL(input_set_keycode);
c8e4c772 852
1da177e4 853#define MATCH_BIT(bit, max) \
7b19ada2 854 for (i = 0; i < BITS_TO_LONGS(max); i++) \
1da177e4
LT
855 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
856 break; \
7b19ada2 857 if (i != BITS_TO_LONGS(max)) \
1da177e4
LT
858 continue;
859
0b7024ac 860static const struct input_device_id *input_match_device(struct input_handler *handler,
66e66118 861 struct input_dev *dev)
1da177e4 862{
0b7024ac 863 const struct input_device_id *id;
1da177e4
LT
864 int i;
865
0b7024ac 866 for (id = handler->id_table; id->flags || id->driver_info; id++) {
1da177e4
LT
867
868 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
ddc5d341 869 if (id->bustype != dev->id.bustype)
1da177e4
LT
870 continue;
871
872 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
ddc5d341 873 if (id->vendor != dev->id.vendor)
1da177e4
LT
874 continue;
875
876 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
ddc5d341 877 if (id->product != dev->id.product)
1da177e4
LT
878 continue;
879
880 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
ddc5d341 881 if (id->version != dev->id.version)
1da177e4
LT
882 continue;
883
884 MATCH_BIT(evbit, EV_MAX);
885 MATCH_BIT(keybit, KEY_MAX);
886 MATCH_BIT(relbit, REL_MAX);
887 MATCH_BIT(absbit, ABS_MAX);
888 MATCH_BIT(mscbit, MSC_MAX);
889 MATCH_BIT(ledbit, LED_MAX);
890 MATCH_BIT(sndbit, SND_MAX);
891 MATCH_BIT(ffbit, FF_MAX);
ff13f98b 892 MATCH_BIT(swbit, SW_MAX);
1da177e4 893
0b7024ac
DT
894 if (!handler->match || handler->match(handler, dev))
895 return id;
1da177e4
LT
896 }
897
898 return NULL;
899}
900
5b2a0826
DT
901static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
902{
903 const struct input_device_id *id;
904 int error;
905
0b7024ac 906 id = input_match_device(handler, dev);
5b2a0826
DT
907 if (!id)
908 return -ENODEV;
909
910 error = handler->connect(handler, dev, id);
911 if (error && error != -ENODEV)
da0c4901
JP
912 pr_err("failed to attach handler %s to device %s, error: %d\n",
913 handler->name, kobject_name(&dev->dev.kobj), error);
5b2a0826
DT
914
915 return error;
916}
917
15e184af
DT
918#ifdef CONFIG_COMPAT
919
920static int input_bits_to_string(char *buf, int buf_size,
921 unsigned long bits, bool skip_empty)
922{
923 int len = 0;
924
925 if (INPUT_COMPAT_TEST) {
926 u32 dword = bits >> 32;
927 if (dword || !skip_empty)
928 len += snprintf(buf, buf_size, "%x ", dword);
929
930 dword = bits & 0xffffffffUL;
931 if (dword || !skip_empty || len)
932 len += snprintf(buf + len, max(buf_size - len, 0),
933 "%x", dword);
934 } else {
935 if (bits || !skip_empty)
936 len += snprintf(buf, buf_size, "%lx", bits);
937 }
938
939 return len;
940}
941
942#else /* !CONFIG_COMPAT */
943
944static int input_bits_to_string(char *buf, int buf_size,
945 unsigned long bits, bool skip_empty)
946{
947 return bits || !skip_empty ?
948 snprintf(buf, buf_size, "%lx", bits) : 0;
949}
950
951#endif
5b2a0826 952
f96b434d
DT
953#ifdef CONFIG_PROC_FS
954
955static struct proc_dir_entry *proc_bus_input_dir;
956static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
957static int input_devices_state;
958
959static inline void input_wakeup_procfs_readers(void)
960{
961 input_devices_state++;
962 wake_up(&input_devices_poll_wait);
963}
964
969b21cd 965static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
f96b434d 966{
f96b434d 967 poll_wait(file, &input_devices_poll_wait, wait);
fa886612
DT
968 if (file->f_version != input_devices_state) {
969 file->f_version = input_devices_state;
f96b434d 970 return POLLIN | POLLRDNORM;
fa886612 971 }
1e0afb28 972
f96b434d
DT
973 return 0;
974}
975
1572ca2a
DT
976union input_seq_state {
977 struct {
978 unsigned short pos;
979 bool mutex_acquired;
980 };
981 void *p;
982};
983
969b21cd
DT
984static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
985{
1572ca2a
DT
986 union input_seq_state *state = (union input_seq_state *)&seq->private;
987 int error;
988
989 /* We need to fit into seq->private pointer */
990 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
991
992 error = mutex_lock_interruptible(&input_mutex);
993 if (error) {
994 state->mutex_acquired = false;
995 return ERR_PTR(error);
996 }
997
998 state->mutex_acquired = true;
f96b434d 999
ad5d972c 1000 return seq_list_start(&input_dev_list, *pos);
969b21cd 1001}
051b2fea 1002
969b21cd
DT
1003static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1004{
ad5d972c 1005 return seq_list_next(v, &input_dev_list, pos);
969b21cd 1006}
f96b434d 1007
1572ca2a 1008static void input_seq_stop(struct seq_file *seq, void *v)
969b21cd 1009{
1572ca2a
DT
1010 union input_seq_state *state = (union input_seq_state *)&seq->private;
1011
1012 if (state->mutex_acquired)
1013 mutex_unlock(&input_mutex);
969b21cd 1014}
f96b434d 1015
969b21cd
DT
1016static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
1017 unsigned long *bitmap, int max)
1018{
1019 int i;
15e184af
DT
1020 bool skip_empty = true;
1021 char buf[18];
f96b434d 1022
969b21cd 1023 seq_printf(seq, "B: %s=", name);
15e184af
DT
1024
1025 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1026 if (input_bits_to_string(buf, sizeof(buf),
1027 bitmap[i], skip_empty)) {
1028 skip_empty = false;
1029 seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
1030 }
1031 }
1032
1033 /*
1034 * If no output was produced print a single 0.
1035 */
1036 if (skip_empty)
1037 seq_puts(seq, "0");
1038
969b21cd
DT
1039 seq_putc(seq, '\n');
1040}
f96b434d 1041
969b21cd
DT
1042static int input_devices_seq_show(struct seq_file *seq, void *v)
1043{
1044 struct input_dev *dev = container_of(v, struct input_dev, node);
9657d75c 1045 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
969b21cd
DT
1046 struct input_handle *handle;
1047
1048 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
1049 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
1050
1051 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
1052 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
1053 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
15e03ae8 1054 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
969b21cd
DT
1055 seq_printf(seq, "H: Handlers=");
1056
1057 list_for_each_entry(handle, &dev->h_list, d_node)
1058 seq_printf(seq, "%s ", handle->name);
1059 seq_putc(seq, '\n');
1060
85b77200
HR
1061 input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX);
1062
969b21cd
DT
1063 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
1064 if (test_bit(EV_KEY, dev->evbit))
1065 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
1066 if (test_bit(EV_REL, dev->evbit))
1067 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
1068 if (test_bit(EV_ABS, dev->evbit))
1069 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
1070 if (test_bit(EV_MSC, dev->evbit))
1071 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
1072 if (test_bit(EV_LED, dev->evbit))
1073 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
1074 if (test_bit(EV_SND, dev->evbit))
1075 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
1076 if (test_bit(EV_FF, dev->evbit))
1077 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
1078 if (test_bit(EV_SW, dev->evbit))
1079 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
1080
1081 seq_putc(seq, '\n');
1082
1083 kfree(path);
1084 return 0;
f96b434d
DT
1085}
1086
cec69c37 1087static const struct seq_operations input_devices_seq_ops = {
969b21cd
DT
1088 .start = input_devices_seq_start,
1089 .next = input_devices_seq_next,
1572ca2a 1090 .stop = input_seq_stop,
969b21cd
DT
1091 .show = input_devices_seq_show,
1092};
1093
1094static int input_proc_devices_open(struct inode *inode, struct file *file)
f96b434d 1095{
969b21cd
DT
1096 return seq_open(file, &input_devices_seq_ops);
1097}
1098
2b8693c0 1099static const struct file_operations input_devices_fileops = {
969b21cd
DT
1100 .owner = THIS_MODULE,
1101 .open = input_proc_devices_open,
1102 .poll = input_proc_devices_poll,
1103 .read = seq_read,
1104 .llseek = seq_lseek,
1105 .release = seq_release,
1106};
1107
1108static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
1109{
1572ca2a
DT
1110 union input_seq_state *state = (union input_seq_state *)&seq->private;
1111 int error;
1112
1113 /* We need to fit into seq->private pointer */
1114 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
1115
1116 error = mutex_lock_interruptible(&input_mutex);
1117 if (error) {
1118 state->mutex_acquired = false;
1119 return ERR_PTR(error);
1120 }
1121
1122 state->mutex_acquired = true;
1123 state->pos = *pos;
8006479c 1124
ad5d972c 1125 return seq_list_start(&input_handler_list, *pos);
969b21cd 1126}
f96b434d 1127
969b21cd
DT
1128static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1129{
1572ca2a 1130 union input_seq_state *state = (union input_seq_state *)&seq->private;
f96b434d 1131
1572ca2a
DT
1132 state->pos = *pos + 1;
1133 return seq_list_next(v, &input_handler_list, pos);
969b21cd
DT
1134}
1135
1136static int input_handlers_seq_show(struct seq_file *seq, void *v)
1137{
1138 struct input_handler *handler = container_of(v, struct input_handler, node);
1572ca2a 1139 union input_seq_state *state = (union input_seq_state *)&seq->private;
969b21cd 1140
1572ca2a 1141 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
ef7995f4
DT
1142 if (handler->filter)
1143 seq_puts(seq, " (filter)");
969b21cd
DT
1144 if (handler->fops)
1145 seq_printf(seq, " Minor=%d", handler->minor);
1146 seq_putc(seq, '\n');
1147
1148 return 0;
1149}
1572ca2a 1150
cec69c37 1151static const struct seq_operations input_handlers_seq_ops = {
969b21cd
DT
1152 .start = input_handlers_seq_start,
1153 .next = input_handlers_seq_next,
1572ca2a 1154 .stop = input_seq_stop,
969b21cd
DT
1155 .show = input_handlers_seq_show,
1156};
1157
1158static int input_proc_handlers_open(struct inode *inode, struct file *file)
1159{
1160 return seq_open(file, &input_handlers_seq_ops);
1161}
1162
2b8693c0 1163static const struct file_operations input_handlers_fileops = {
969b21cd
DT
1164 .owner = THIS_MODULE,
1165 .open = input_proc_handlers_open,
1166 .read = seq_read,
1167 .llseek = seq_lseek,
1168 .release = seq_release,
1169};
f96b434d
DT
1170
1171static int __init input_proc_init(void)
1172{
1173 struct proc_dir_entry *entry;
1174
9c37066d 1175 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
f96b434d
DT
1176 if (!proc_bus_input_dir)
1177 return -ENOMEM;
1178
c7705f34
DL
1179 entry = proc_create("devices", 0, proc_bus_input_dir,
1180 &input_devices_fileops);
f96b434d
DT
1181 if (!entry)
1182 goto fail1;
1183
c7705f34
DL
1184 entry = proc_create("handlers", 0, proc_bus_input_dir,
1185 &input_handlers_fileops);
f96b434d
DT
1186 if (!entry)
1187 goto fail2;
1188
f96b434d
DT
1189 return 0;
1190
1191 fail2: remove_proc_entry("devices", proc_bus_input_dir);
9c37066d 1192 fail1: remove_proc_entry("bus/input", NULL);
f96b434d
DT
1193 return -ENOMEM;
1194}
1195
beffbdc2 1196static void input_proc_exit(void)
f96b434d
DT
1197{
1198 remove_proc_entry("devices", proc_bus_input_dir);
1199 remove_proc_entry("handlers", proc_bus_input_dir);
9c37066d 1200 remove_proc_entry("bus/input", NULL);
f96b434d
DT
1201}
1202
1203#else /* !CONFIG_PROC_FS */
1204static inline void input_wakeup_procfs_readers(void) { }
1205static inline int input_proc_init(void) { return 0; }
1206static inline void input_proc_exit(void) { }
1207#endif
1208
9657d75c
DT
1209#define INPUT_DEV_STRING_ATTR_SHOW(name) \
1210static ssize_t input_dev_show_##name(struct device *dev, \
1211 struct device_attribute *attr, \
1212 char *buf) \
1213{ \
1214 struct input_dev *input_dev = to_input_dev(dev); \
1215 \
1216 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1217 input_dev->name ? input_dev->name : ""); \
1218} \
1219static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
5c1e9a6a
DT
1220
1221INPUT_DEV_STRING_ATTR_SHOW(name);
1222INPUT_DEV_STRING_ATTR_SHOW(phys);
1223INPUT_DEV_STRING_ATTR_SHOW(uniq);
1224
ac648a6a
DT
1225static int input_print_modalias_bits(char *buf, int size,
1226 char name, unsigned long *bm,
1227 unsigned int min_bit, unsigned int max_bit)
1d8f430c 1228{
ac648a6a 1229 int len = 0, i;
1d8f430c 1230
ac648a6a
DT
1231 len += snprintf(buf, max(size, 0), "%c", name);
1232 for (i = min_bit; i < max_bit; i++)
7b19ada2 1233 if (bm[BIT_WORD(i)] & BIT_MASK(i))
ac648a6a 1234 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
1d8f430c
RR
1235 return len;
1236}
1237
2db66876
DT
1238static int input_print_modalias(char *buf, int size, struct input_dev *id,
1239 int add_cr)
1d8f430c 1240{
bd37e5a9 1241 int len;
1d8f430c 1242
ac648a6a
DT
1243 len = snprintf(buf, max(size, 0),
1244 "input:b%04Xv%04Xp%04Xe%04X-",
1245 id->id.bustype, id->id.vendor,
1246 id->id.product, id->id.version);
1247
1248 len += input_print_modalias_bits(buf + len, size - len,
1249 'e', id->evbit, 0, EV_MAX);
1250 len += input_print_modalias_bits(buf + len, size - len,
1251 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1252 len += input_print_modalias_bits(buf + len, size - len,
1253 'r', id->relbit, 0, REL_MAX);
1254 len += input_print_modalias_bits(buf + len, size - len,
1255 'a', id->absbit, 0, ABS_MAX);
1256 len += input_print_modalias_bits(buf + len, size - len,
1257 'm', id->mscbit, 0, MSC_MAX);
1258 len += input_print_modalias_bits(buf + len, size - len,
1259 'l', id->ledbit, 0, LED_MAX);
1260 len += input_print_modalias_bits(buf + len, size - len,
1261 's', id->sndbit, 0, SND_MAX);
1262 len += input_print_modalias_bits(buf + len, size - len,
1263 'f', id->ffbit, 0, FF_MAX);
1264 len += input_print_modalias_bits(buf + len, size - len,
1265 'w', id->swbit, 0, SW_MAX);
2db66876
DT
1266
1267 if (add_cr)
ac648a6a 1268 len += snprintf(buf + len, max(size - len, 0), "\n");
2db66876 1269
bd37e5a9
KS
1270 return len;
1271}
1272
9657d75c
DT
1273static ssize_t input_dev_show_modalias(struct device *dev,
1274 struct device_attribute *attr,
1275 char *buf)
bd37e5a9
KS
1276{
1277 struct input_dev *id = to_input_dev(dev);
1278 ssize_t len;
1279
2db66876
DT
1280 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1281
8a3cf456 1282 return min_t(int, len, PAGE_SIZE);
1d8f430c 1283}
9657d75c 1284static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
1d8f430c 1285
85b77200
HR
1286static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1287 int max, int add_cr);
1288
1289static ssize_t input_dev_show_properties(struct device *dev,
1290 struct device_attribute *attr,
1291 char *buf)
1292{
1293 struct input_dev *input_dev = to_input_dev(dev);
1294 int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit,
1295 INPUT_PROP_MAX, true);
1296 return min_t(int, len, PAGE_SIZE);
1297}
1298static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL);
1299
629b77a4 1300static struct attribute *input_dev_attrs[] = {
9657d75c
DT
1301 &dev_attr_name.attr,
1302 &dev_attr_phys.attr,
1303 &dev_attr_uniq.attr,
1304 &dev_attr_modalias.attr,
85b77200 1305 &dev_attr_properties.attr,
629b77a4
GKH
1306 NULL
1307};
1308
bd0ef235 1309static struct attribute_group input_dev_attr_group = {
629b77a4 1310 .attrs = input_dev_attrs,
5c1e9a6a
DT
1311};
1312
9657d75c
DT
1313#define INPUT_DEV_ID_ATTR(name) \
1314static ssize_t input_dev_show_id_##name(struct device *dev, \
1315 struct device_attribute *attr, \
1316 char *buf) \
1317{ \
1318 struct input_dev *input_dev = to_input_dev(dev); \
1319 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1320} \
1321static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
5c1e9a6a
DT
1322
1323INPUT_DEV_ID_ATTR(bustype);
1324INPUT_DEV_ID_ATTR(vendor);
1325INPUT_DEV_ID_ATTR(product);
1326INPUT_DEV_ID_ATTR(version);
1327
1328static struct attribute *input_dev_id_attrs[] = {
9657d75c
DT
1329 &dev_attr_bustype.attr,
1330 &dev_attr_vendor.attr,
1331 &dev_attr_product.attr,
1332 &dev_attr_version.attr,
5c1e9a6a
DT
1333 NULL
1334};
1335
1336static struct attribute_group input_dev_id_attr_group = {
1337 .name = "id",
1338 .attrs = input_dev_id_attrs,
1339};
1340
969b21cd
DT
1341static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1342 int max, int add_cr)
1343{
1344 int i;
1345 int len = 0;
15e184af
DT
1346 bool skip_empty = true;
1347
1348 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1349 len += input_bits_to_string(buf + len, max(buf_size - len, 0),
1350 bitmap[i], skip_empty);
1351 if (len) {
1352 skip_empty = false;
1353 if (i > 0)
1354 len += snprintf(buf + len, max(buf_size - len, 0), " ");
1355 }
1356 }
969b21cd 1357
15e184af
DT
1358 /*
1359 * If no output was produced print a single 0.
1360 */
1361 if (len == 0)
1362 len = snprintf(buf, buf_size, "%d", 0);
969b21cd
DT
1363
1364 if (add_cr)
1365 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1366
1367 return len;
1368}
1369
9657d75c
DT
1370#define INPUT_DEV_CAP_ATTR(ev, bm) \
1371static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1372 struct device_attribute *attr, \
1373 char *buf) \
1374{ \
1375 struct input_dev *input_dev = to_input_dev(dev); \
1376 int len = input_print_bitmap(buf, PAGE_SIZE, \
15e184af
DT
1377 input_dev->bm##bit, ev##_MAX, \
1378 true); \
9657d75c
DT
1379 return min_t(int, len, PAGE_SIZE); \
1380} \
1381static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
5c1e9a6a
DT
1382
1383INPUT_DEV_CAP_ATTR(EV, ev);
1384INPUT_DEV_CAP_ATTR(KEY, key);
1385INPUT_DEV_CAP_ATTR(REL, rel);
1386INPUT_DEV_CAP_ATTR(ABS, abs);
1387INPUT_DEV_CAP_ATTR(MSC, msc);
1388INPUT_DEV_CAP_ATTR(LED, led);
1389INPUT_DEV_CAP_ATTR(SND, snd);
1390INPUT_DEV_CAP_ATTR(FF, ff);
1391INPUT_DEV_CAP_ATTR(SW, sw);
1392
1393static struct attribute *input_dev_caps_attrs[] = {
9657d75c
DT
1394 &dev_attr_ev.attr,
1395 &dev_attr_key.attr,
1396 &dev_attr_rel.attr,
1397 &dev_attr_abs.attr,
1398 &dev_attr_msc.attr,
1399 &dev_attr_led.attr,
1400 &dev_attr_snd.attr,
1401 &dev_attr_ff.attr,
1402 &dev_attr_sw.attr,
5c1e9a6a
DT
1403 NULL
1404};
1405
1406static struct attribute_group input_dev_caps_attr_group = {
1407 .name = "capabilities",
1408 .attrs = input_dev_caps_attrs,
1409};
1410
a4dbd674 1411static const struct attribute_group *input_dev_attr_groups[] = {
cb9def4d
DT
1412 &input_dev_attr_group,
1413 &input_dev_id_attr_group,
1414 &input_dev_caps_attr_group,
1415 NULL
1416};
1417
9657d75c 1418static void input_dev_release(struct device *device)
d19fbe8a 1419{
9657d75c 1420 struct input_dev *dev = to_input_dev(device);
d19fbe8a 1421
509ca1a9 1422 input_ff_destroy(dev);
40d007e7 1423 input_mt_destroy_slots(dev);
d31b2865 1424 kfree(dev->absinfo);
d19fbe8a 1425 kfree(dev);
509ca1a9 1426
d19fbe8a
DT
1427 module_put(THIS_MODULE);
1428}
1429
a7fadbe1 1430/*
312c004d 1431 * Input uevent interface - loading event handlers based on
a7fadbe1
DT
1432 * device bitfields.
1433 */
7eff2e7a 1434static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
ac648a6a 1435 const char *name, unsigned long *bitmap, int max)
a7fadbe1 1436{
7eff2e7a 1437 int len;
a7fadbe1 1438
fcd3027a 1439 if (add_uevent_var(env, "%s", name))
a7fadbe1
DT
1440 return -ENOMEM;
1441
7eff2e7a
KS
1442 len = input_print_bitmap(&env->buf[env->buflen - 1],
1443 sizeof(env->buf) - env->buflen,
15e184af 1444 bitmap, max, false);
7eff2e7a 1445 if (len >= (sizeof(env->buf) - env->buflen))
a7fadbe1
DT
1446 return -ENOMEM;
1447
7eff2e7a 1448 env->buflen += len;
a7fadbe1
DT
1449 return 0;
1450}
1451
7eff2e7a 1452static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
ac648a6a
DT
1453 struct input_dev *dev)
1454{
7eff2e7a 1455 int len;
ac648a6a 1456
7eff2e7a 1457 if (add_uevent_var(env, "MODALIAS="))
ac648a6a
DT
1458 return -ENOMEM;
1459
7eff2e7a
KS
1460 len = input_print_modalias(&env->buf[env->buflen - 1],
1461 sizeof(env->buf) - env->buflen,
1462 dev, 0);
1463 if (len >= (sizeof(env->buf) - env->buflen))
ac648a6a
DT
1464 return -ENOMEM;
1465
7eff2e7a 1466 env->buflen += len;
ac648a6a
DT
1467 return 0;
1468}
1469
a7fadbe1
DT
1470#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1471 do { \
7eff2e7a 1472 int err = add_uevent_var(env, fmt, val); \
a7fadbe1
DT
1473 if (err) \
1474 return err; \
1475 } while (0)
1476
1477#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1478 do { \
7eff2e7a 1479 int err = input_add_uevent_bm_var(env, name, bm, max); \
a7fadbe1
DT
1480 if (err) \
1481 return err; \
1482 } while (0)
1483
ac648a6a
DT
1484#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1485 do { \
7eff2e7a 1486 int err = input_add_uevent_modalias_var(env, dev); \
ac648a6a
DT
1487 if (err) \
1488 return err; \
1489 } while (0)
1490
7eff2e7a 1491static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
a7fadbe1 1492{
9657d75c 1493 struct input_dev *dev = to_input_dev(device);
a7fadbe1
DT
1494
1495 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1496 dev->id.bustype, dev->id.vendor,
1497 dev->id.product, dev->id.version);
1498 if (dev->name)
1499 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1500 if (dev->phys)
1501 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
08de1f04 1502 if (dev->uniq)
a7fadbe1
DT
1503 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1504
85b77200
HR
1505 INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX);
1506
a7fadbe1
DT
1507 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1508 if (test_bit(EV_KEY, dev->evbit))
1509 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1510 if (test_bit(EV_REL, dev->evbit))
1511 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1512 if (test_bit(EV_ABS, dev->evbit))
1513 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1514 if (test_bit(EV_MSC, dev->evbit))
1515 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1516 if (test_bit(EV_LED, dev->evbit))
1517 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1518 if (test_bit(EV_SND, dev->evbit))
1519 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1520 if (test_bit(EV_FF, dev->evbit))
1521 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1522 if (test_bit(EV_SW, dev->evbit))
1523 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1524
ac648a6a 1525 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
a7fadbe1
DT
1526
1527 return 0;
1528}
1529
3cc96351
DT
1530#define INPUT_DO_TOGGLE(dev, type, bits, on) \
1531 do { \
1532 int i; \
1533 bool active; \
1534 \
1535 if (!test_bit(EV_##type, dev->evbit)) \
1536 break; \
1537 \
1538 for (i = 0; i < type##_MAX; i++) { \
1539 if (!test_bit(i, dev->bits##bit)) \
1540 continue; \
1541 \
1542 active = test_bit(i, dev->bits); \
1543 if (!active && !on) \
1544 continue; \
1545 \
1546 dev->event(dev, EV_##type, i, on ? active : 0); \
1547 } \
ffd0db97
DT
1548 } while (0)
1549
b50b5216 1550static void input_dev_toggle(struct input_dev *dev, bool activate)
ffd0db97
DT
1551{
1552 if (!dev->event)
1553 return;
1554
1555 INPUT_DO_TOGGLE(dev, LED, led, activate);
1556 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1557
1558 if (activate && test_bit(EV_REP, dev->evbit)) {
1559 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1560 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1561 }
1562}
1563
b50b5216
DT
1564/**
1565 * input_reset_device() - reset/restore the state of input device
1566 * @dev: input device whose state needs to be reset
1567 *
1568 * This function tries to reset the state of an opened input device and
1569 * bring internal state and state if the hardware in sync with each other.
1570 * We mark all keys as released, restore LED state, repeat rate, etc.
1571 */
1572void input_reset_device(struct input_dev *dev)
1573{
1574 mutex_lock(&dev->mutex);
1575
1576 if (dev->users) {
1577 input_dev_toggle(dev, true);
1578
1579 /*
1580 * Keys that have been pressed at suspend time are unlikely
1581 * to be still pressed when we resume.
1582 */
1583 spin_lock_irq(&dev->event_lock);
1584 input_dev_release_keys(dev);
1585 spin_unlock_irq(&dev->event_lock);
1586 }
1587
1588 mutex_unlock(&dev->mutex);
1589}
1590EXPORT_SYMBOL(input_reset_device);
1591
1592#ifdef CONFIG_PM
ffd0db97
DT
1593static int input_dev_suspend(struct device *dev)
1594{
1595 struct input_dev *input_dev = to_input_dev(dev);
1596
1597 mutex_lock(&input_dev->mutex);
b50b5216
DT
1598
1599 if (input_dev->users)
1600 input_dev_toggle(input_dev, false);
1601
ffd0db97
DT
1602 mutex_unlock(&input_dev->mutex);
1603
1604 return 0;
1605}
1606
1607static int input_dev_resume(struct device *dev)
1608{
1609 struct input_dev *input_dev = to_input_dev(dev);
1610
b50b5216 1611 input_reset_device(input_dev);
ffd0db97
DT
1612
1613 return 0;
1614}
1615
1616static const struct dev_pm_ops input_dev_pm_ops = {
1617 .suspend = input_dev_suspend,
1618 .resume = input_dev_resume,
1619 .poweroff = input_dev_suspend,
1620 .restore = input_dev_resume,
1621};
1622#endif /* CONFIG_PM */
1623
9657d75c
DT
1624static struct device_type input_dev_type = {
1625 .groups = input_dev_attr_groups,
1626 .release = input_dev_release,
1627 .uevent = input_dev_uevent,
ffd0db97
DT
1628#ifdef CONFIG_PM
1629 .pm = &input_dev_pm_ops,
1630#endif
9657d75c
DT
1631};
1632
2c9ede55 1633static char *input_devnode(struct device *dev, umode_t *mode)
aa5ed63e
KS
1634{
1635 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1636}
1637
ea9f240b 1638struct class input_class = {
9657d75c 1639 .name = "input",
e454cea2 1640 .devnode = input_devnode,
d19fbe8a 1641};
ca56fe07 1642EXPORT_SYMBOL_GPL(input_class);
d19fbe8a 1643
1447190e
DT
1644/**
1645 * input_allocate_device - allocate memory for new input device
1646 *
1647 * Returns prepared struct input_dev or NULL.
1648 *
1649 * NOTE: Use input_free_device() to free devices that have not been
1650 * registered; input_unregister_device() should be used for already
1651 * registered devices.
1652 */
d19fbe8a
DT
1653struct input_dev *input_allocate_device(void)
1654{
1655 struct input_dev *dev;
1656
1657 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1658 if (dev) {
9657d75c
DT
1659 dev->dev.type = &input_dev_type;
1660 dev->dev.class = &input_class;
1661 device_initialize(&dev->dev);
f60d2b11 1662 mutex_init(&dev->mutex);
8006479c 1663 spin_lock_init(&dev->event_lock);
d19fbe8a
DT
1664 INIT_LIST_HEAD(&dev->h_list);
1665 INIT_LIST_HEAD(&dev->node);
655816e4
DT
1666
1667 __module_get(THIS_MODULE);
d19fbe8a
DT
1668 }
1669
1670 return dev;
1671}
ca56fe07 1672EXPORT_SYMBOL(input_allocate_device);
d19fbe8a 1673
1447190e
DT
1674/**
1675 * input_free_device - free memory occupied by input_dev structure
1676 * @dev: input device to free
1677 *
1678 * This function should only be used if input_register_device()
1679 * was not called yet or if it failed. Once device was registered
1680 * use input_unregister_device() and memory will be freed once last
8006479c 1681 * reference to the device is dropped.
1447190e
DT
1682 *
1683 * Device should be allocated by input_allocate_device().
1684 *
1685 * NOTE: If there are references to the input device then memory
1686 * will not be freed until last reference is dropped.
1687 */
f60d2b11
DT
1688void input_free_device(struct input_dev *dev)
1689{
54f9e36c 1690 if (dev)
f60d2b11 1691 input_put_device(dev);
f60d2b11 1692}
ca56fe07 1693EXPORT_SYMBOL(input_free_device);
f60d2b11 1694
534565f2
DT
1695/**
1696 * input_set_capability - mark device as capable of a certain event
1697 * @dev: device that is capable of emitting or accepting event
1698 * @type: type of the event (EV_KEY, EV_REL, etc...)
1699 * @code: event code
1700 *
1701 * In addition to setting up corresponding bit in appropriate capability
1702 * bitmap the function also adjusts dev->evbit.
1703 */
1704void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1705{
1706 switch (type) {
1707 case EV_KEY:
1708 __set_bit(code, dev->keybit);
1709 break;
1710
1711 case EV_REL:
1712 __set_bit(code, dev->relbit);
1713 break;
1714
1715 case EV_ABS:
1716 __set_bit(code, dev->absbit);
1717 break;
1718
1719 case EV_MSC:
1720 __set_bit(code, dev->mscbit);
1721 break;
1722
1723 case EV_SW:
1724 __set_bit(code, dev->swbit);
1725 break;
1726
1727 case EV_LED:
1728 __set_bit(code, dev->ledbit);
1729 break;
1730
1731 case EV_SND:
1732 __set_bit(code, dev->sndbit);
1733 break;
1734
1735 case EV_FF:
1736 __set_bit(code, dev->ffbit);
1737 break;
1738
22d1c398
DB
1739 case EV_PWR:
1740 /* do nothing */
1741 break;
1742
534565f2 1743 default:
da0c4901
JP
1744 pr_err("input_set_capability: unknown type %u (code %u)\n",
1745 type, code);
534565f2
DT
1746 dump_stack();
1747 return;
1748 }
1749
1750 __set_bit(type, dev->evbit);
1751}
1752EXPORT_SYMBOL(input_set_capability);
1753
80b4895a
JB
1754static unsigned int input_estimate_events_per_packet(struct input_dev *dev)
1755{
1756 int mt_slots;
1757 int i;
1758 unsigned int events;
1759
8d18fba2
HR
1760 if (dev->mt) {
1761 mt_slots = dev->mt->num_slots;
80b4895a
JB
1762 } else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) {
1763 mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum -
1764 dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1,
8c127f07 1765 mt_slots = clamp(mt_slots, 2, 32);
80b4895a
JB
1766 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
1767 mt_slots = 2;
1768 } else {
1769 mt_slots = 0;
1770 }
1771
1772 events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */
1773
1774 for (i = 0; i < ABS_CNT; i++) {
1775 if (test_bit(i, dev->absbit)) {
1776 if (input_is_mt_axis(i))
1777 events += mt_slots;
1778 else
1779 events++;
1780 }
1781 }
1782
1783 for (i = 0; i < REL_CNT; i++)
1784 if (test_bit(i, dev->relbit))
1785 events++;
1786
7c75bf99
HR
1787 /* Make room for KEY and MSC events */
1788 events += 7;
1789
80b4895a
JB
1790 return events;
1791}
1792
92a3a587
DT
1793#define INPUT_CLEANSE_BITMASK(dev, type, bits) \
1794 do { \
1795 if (!test_bit(EV_##type, dev->evbit)) \
1796 memset(dev->bits##bit, 0, \
1797 sizeof(dev->bits##bit)); \
1798 } while (0)
1799
1800static void input_cleanse_bitmasks(struct input_dev *dev)
1801{
1802 INPUT_CLEANSE_BITMASK(dev, KEY, key);
1803 INPUT_CLEANSE_BITMASK(dev, REL, rel);
1804 INPUT_CLEANSE_BITMASK(dev, ABS, abs);
1805 INPUT_CLEANSE_BITMASK(dev, MSC, msc);
1806 INPUT_CLEANSE_BITMASK(dev, LED, led);
1807 INPUT_CLEANSE_BITMASK(dev, SND, snd);
1808 INPUT_CLEANSE_BITMASK(dev, FF, ff);
1809 INPUT_CLEANSE_BITMASK(dev, SW, sw);
1810}
1811
8006479c
DT
1812/**
1813 * input_register_device - register device with input core
1814 * @dev: device to be registered
1815 *
1816 * This function registers device with input core. The device must be
1817 * allocated with input_allocate_device() and all it's capabilities
1818 * set up before registering.
1819 * If function fails the device must be freed with input_free_device().
1820 * Once device has been successfully registered it can be unregistered
1821 * with input_unregister_device(); input_free_device() should not be
1822 * called in this case.
1823 */
5f945489 1824int input_register_device(struct input_dev *dev)
1da177e4 1825{
bd0ef235 1826 static atomic_t input_no = ATOMIC_INIT(0);
1da177e4 1827 struct input_handler *handler;
7c75bf99 1828 unsigned int packet_size;
bd0ef235
DT
1829 const char *path;
1830 int error;
1da177e4 1831
4f93df40 1832 /* Every input device generates EV_SYN/SYN_REPORT events. */
8006479c 1833 __set_bit(EV_SYN, dev->evbit);
0fbf87ca 1834
4f93df40
DT
1835 /* KEY_RESERVED is not supposed to be transmitted to userspace. */
1836 __clear_bit(KEY_RESERVED, dev->keybit);
1837
92a3a587
DT
1838 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
1839 input_cleanse_bitmasks(dev);
1840
7c75bf99
HR
1841 packet_size = input_estimate_events_per_packet(dev);
1842 if (dev->hint_events_per_packet < packet_size)
1843 dev->hint_events_per_packet = packet_size;
80b4895a 1844
1da177e4
LT
1845 /*
1846 * If delay and period are pre-set by the driver, then autorepeating
1847 * is handled by the driver itself and we don't do it in input.c.
1848 */
1da177e4
LT
1849 init_timer(&dev->timer);
1850 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1851 dev->timer.data = (long) dev;
1852 dev->timer.function = input_repeat_key;
1853 dev->rep[REP_DELAY] = 250;
1854 dev->rep[REP_PERIOD] = 33;
1855 }
1856
aebd636b
DT
1857 if (!dev->getkeycode)
1858 dev->getkeycode = input_default_getkeycode;
c8e4c772 1859
aebd636b
DT
1860 if (!dev->setkeycode)
1861 dev->setkeycode = input_default_setkeycode;
c8e4c772 1862
a6c2490f
KS
1863 dev_set_name(&dev->dev, "input%ld",
1864 (unsigned long) atomic_inc_return(&input_no) - 1);
bd0ef235 1865
9657d75c 1866 error = device_add(&dev->dev);
bd0ef235
DT
1867 if (error)
1868 return error;
1869
9657d75c 1870 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
da0c4901
JP
1871 pr_info("%s as %s\n",
1872 dev->name ? dev->name : "Unspecified device",
1873 path ? path : "N/A");
bd0ef235 1874 kfree(path);
10204020 1875
8006479c
DT
1876 error = mutex_lock_interruptible(&input_mutex);
1877 if (error) {
1878 device_del(&dev->dev);
1879 return error;
1880 }
1881
1882 list_add_tail(&dev->node, &input_dev_list);
1883
1da177e4 1884 list_for_each_entry(handler, &input_handler_list, node)
5b2a0826 1885 input_attach_handler(dev, handler);
1da177e4 1886
f96b434d 1887 input_wakeup_procfs_readers();
5f945489 1888
8006479c
DT
1889 mutex_unlock(&input_mutex);
1890
5f945489 1891 return 0;
1da177e4 1892}
ca56fe07 1893EXPORT_SYMBOL(input_register_device);
1da177e4 1894
8006479c
DT
1895/**
1896 * input_unregister_device - unregister previously registered device
1897 * @dev: device to be unregistered
1898 *
1899 * This function unregisters an input device. Once device is unregistered
1900 * the caller should not try to access it as it may get freed at any moment.
1901 */
1da177e4
LT
1902void input_unregister_device(struct input_dev *dev)
1903{
5b2a0826 1904 struct input_handle *handle, *next;
1da177e4 1905
8006479c 1906 input_disconnect_device(dev);
1da177e4 1907
8006479c 1908 mutex_lock(&input_mutex);
1da177e4 1909
5b2a0826 1910 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1da177e4 1911 handle->handler->disconnect(handle);
5b2a0826 1912 WARN_ON(!list_empty(&dev->h_list));
1da177e4 1913
8006479c 1914 del_timer_sync(&dev->timer);
1da177e4
LT
1915 list_del_init(&dev->node);
1916
f96b434d 1917 input_wakeup_procfs_readers();
8006479c
DT
1918
1919 mutex_unlock(&input_mutex);
1920
1921 device_unregister(&dev->dev);
1da177e4 1922}
ca56fe07 1923EXPORT_SYMBOL(input_unregister_device);
1da177e4 1924
8006479c
DT
1925/**
1926 * input_register_handler - register a new input handler
1927 * @handler: handler to be registered
1928 *
1929 * This function registers a new input handler (interface) for input
1930 * devices in the system and attaches it to all input devices that
1931 * are compatible with the handler.
1932 */
4263cf0f 1933int input_register_handler(struct input_handler *handler)
1da177e4
LT
1934{
1935 struct input_dev *dev;
8006479c
DT
1936 int retval;
1937
1938 retval = mutex_lock_interruptible(&input_mutex);
1939 if (retval)
1940 return retval;
1da177e4 1941
1da177e4
LT
1942 INIT_LIST_HEAD(&handler->h_list);
1943
4263cf0f 1944 if (handler->fops != NULL) {
8006479c
DT
1945 if (input_table[handler->minor >> 5]) {
1946 retval = -EBUSY;
1947 goto out;
1948 }
1da177e4 1949 input_table[handler->minor >> 5] = handler;
4263cf0f 1950 }
1da177e4
LT
1951
1952 list_add_tail(&handler->node, &input_handler_list);
1953
1954 list_for_each_entry(dev, &input_dev_list, node)
5b2a0826 1955 input_attach_handler(dev, handler);
1da177e4 1956
f96b434d 1957 input_wakeup_procfs_readers();
8006479c
DT
1958
1959 out:
1960 mutex_unlock(&input_mutex);
1961 return retval;
1da177e4 1962}
ca56fe07 1963EXPORT_SYMBOL(input_register_handler);
1da177e4 1964
8006479c
DT
1965/**
1966 * input_unregister_handler - unregisters an input handler
1967 * @handler: handler to be unregistered
1968 *
1969 * This function disconnects a handler from its input devices and
1970 * removes it from lists of known handlers.
1971 */
1da177e4
LT
1972void input_unregister_handler(struct input_handler *handler)
1973{
5b2a0826 1974 struct input_handle *handle, *next;
1da177e4 1975
8006479c
DT
1976 mutex_lock(&input_mutex);
1977
5b2a0826 1978 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1da177e4 1979 handler->disconnect(handle);
5b2a0826 1980 WARN_ON(!list_empty(&handler->h_list));
1da177e4
LT
1981
1982 list_del_init(&handler->node);
1983
1984 if (handler->fops != NULL)
1985 input_table[handler->minor >> 5] = NULL;
1986
f96b434d 1987 input_wakeup_procfs_readers();
8006479c
DT
1988
1989 mutex_unlock(&input_mutex);
1da177e4 1990}
ca56fe07 1991EXPORT_SYMBOL(input_unregister_handler);
1da177e4 1992
66d2a595
DT
1993/**
1994 * input_handler_for_each_handle - handle iterator
1995 * @handler: input handler to iterate
1996 * @data: data for the callback
1997 * @fn: function to be called for each handle
1998 *
1999 * Iterate over @bus's list of devices, and call @fn for each, passing
2000 * it @data and stop when @fn returns a non-zero value. The function is
2001 * using RCU to traverse the list and therefore may be usind in atonic
2002 * contexts. The @fn callback is invoked from RCU critical section and
2003 * thus must not sleep.
2004 */
2005int input_handler_for_each_handle(struct input_handler *handler, void *data,
2006 int (*fn)(struct input_handle *, void *))
2007{
2008 struct input_handle *handle;
2009 int retval = 0;
2010
2011 rcu_read_lock();
2012
2013 list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
2014 retval = fn(handle, data);
2015 if (retval)
2016 break;
2017 }
2018
2019 rcu_read_unlock();
2020
2021 return retval;
2022}
2023EXPORT_SYMBOL(input_handler_for_each_handle);
2024
8006479c
DT
2025/**
2026 * input_register_handle - register a new input handle
2027 * @handle: handle to register
2028 *
2029 * This function puts a new input handle onto device's
2030 * and handler's lists so that events can flow through
2031 * it once it is opened using input_open_device().
2032 *
2033 * This function is supposed to be called from handler's
2034 * connect() method.
2035 */
5b2a0826
DT
2036int input_register_handle(struct input_handle *handle)
2037{
2038 struct input_handler *handler = handle->handler;
8006479c
DT
2039 struct input_dev *dev = handle->dev;
2040 int error;
2041
2042 /*
2043 * We take dev->mutex here to prevent race with
2044 * input_release_device().
2045 */
2046 error = mutex_lock_interruptible(&dev->mutex);
2047 if (error)
2048 return error;
ef7995f4
DT
2049
2050 /*
2051 * Filters go to the head of the list, normal handlers
2052 * to the tail.
2053 */
2054 if (handler->filter)
2055 list_add_rcu(&handle->d_node, &dev->h_list);
2056 else
2057 list_add_tail_rcu(&handle->d_node, &dev->h_list);
2058
8006479c 2059 mutex_unlock(&dev->mutex);
5b2a0826 2060
8006479c
DT
2061 /*
2062 * Since we are supposed to be called from ->connect()
2063 * which is mutually exclusive with ->disconnect()
2064 * we can't be racing with input_unregister_handle()
2065 * and so separate lock is not needed here.
2066 */
66d2a595 2067 list_add_tail_rcu(&handle->h_node, &handler->h_list);
5b2a0826
DT
2068
2069 if (handler->start)
2070 handler->start(handle);
2071
2072 return 0;
2073}
2074EXPORT_SYMBOL(input_register_handle);
2075
8006479c
DT
2076/**
2077 * input_unregister_handle - unregister an input handle
2078 * @handle: handle to unregister
2079 *
2080 * This function removes input handle from device's
2081 * and handler's lists.
2082 *
2083 * This function is supposed to be called from handler's
2084 * disconnect() method.
2085 */
5b2a0826
DT
2086void input_unregister_handle(struct input_handle *handle)
2087{
8006479c
DT
2088 struct input_dev *dev = handle->dev;
2089
66d2a595 2090 list_del_rcu(&handle->h_node);
8006479c
DT
2091
2092 /*
2093 * Take dev->mutex to prevent race with input_release_device().
2094 */
2095 mutex_lock(&dev->mutex);
2096 list_del_rcu(&handle->d_node);
2097 mutex_unlock(&dev->mutex);
66d2a595 2098
82ba56c2 2099 synchronize_rcu();
5b2a0826
DT
2100}
2101EXPORT_SYMBOL(input_unregister_handle);
2102
1da177e4
LT
2103static int input_open_file(struct inode *inode, struct file *file)
2104{
2edbf853 2105 struct input_handler *handler;
99ac48f5 2106 const struct file_operations *old_fops, *new_fops = NULL;
1da177e4
LT
2107 int err;
2108
2f2177c8
AB
2109 err = mutex_lock_interruptible(&input_mutex);
2110 if (err)
2111 return err;
2112
1da177e4 2113 /* No load-on-demand here? */
2edbf853 2114 handler = input_table[iminor(inode) >> 5];
2f2177c8
AB
2115 if (handler)
2116 new_fops = fops_get(handler->fops);
2117
2118 mutex_unlock(&input_mutex);
1da177e4
LT
2119
2120 /*
2121 * That's _really_ odd. Usually NULL ->open means "nothing special",
2122 * not "no device". Oh, well...
2123 */
2f2177c8 2124 if (!new_fops || !new_fops->open) {
1da177e4 2125 fops_put(new_fops);
2edbf853
JC
2126 err = -ENODEV;
2127 goto out;
1da177e4 2128 }
2f2177c8 2129
1da177e4
LT
2130 old_fops = file->f_op;
2131 file->f_op = new_fops;
2132
2133 err = new_fops->open(inode, file);
1da177e4
LT
2134 if (err) {
2135 fops_put(file->f_op);
2136 file->f_op = fops_get(old_fops);
2137 }
2138 fops_put(old_fops);
2edbf853 2139out:
1da177e4
LT
2140 return err;
2141}
2142
2b8693c0 2143static const struct file_operations input_fops = {
1da177e4
LT
2144 .owner = THIS_MODULE,
2145 .open = input_open_file,
6038f373 2146 .llseek = noop_llseek,
1da177e4
LT
2147};
2148
f96b434d 2149static int __init input_init(void)
1da177e4 2150{
f96b434d 2151 int err;
1da177e4 2152
ea9f240b 2153 err = class_register(&input_class);
d19fbe8a 2154 if (err) {
da0c4901 2155 pr_err("unable to register input_dev class\n");
d19fbe8a
DT
2156 return err;
2157 }
2158
f96b434d
DT
2159 err = input_proc_init();
2160 if (err)
b0fdfebb 2161 goto fail1;
1da177e4 2162
f96b434d
DT
2163 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
2164 if (err) {
da0c4901 2165 pr_err("unable to register char major %d", INPUT_MAJOR);
b0fdfebb 2166 goto fail2;
1da177e4 2167 }
e334016f 2168
1da177e4 2169 return 0;
1da177e4 2170
b0fdfebb 2171 fail2: input_proc_exit();
ea9f240b 2172 fail1: class_unregister(&input_class);
f96b434d 2173 return err;
1da177e4
LT
2174}
2175
2176static void __exit input_exit(void)
2177{
f96b434d 2178 input_proc_exit();
1da177e4 2179 unregister_chrdev(INPUT_MAJOR, "input");
ea9f240b 2180 class_unregister(&input_class);
1da177e4
LT
2181}
2182
2183subsys_initcall(input_init);
2184module_exit(input_exit);
This page took 0.566945 seconds and 5 git commands to generate.