blob: 7494a0dede792237354e2c5027f8ab848fc5fa6f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * The input core
4 *
5 * Copyright (c) 1999-2002 Vojtech Pavlik
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Joe Perchesda0c4902010-11-29 23:33:07 -08009#define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
Dmitry Torokhovffd0db92009-09-16 01:06:43 -070012#include <linux/types.h>
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -070013#include <linux/idr.h>
Henrik Rydberg47c78e82010-11-27 09:16:48 +010014#include <linux/input/mt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/random.h>
18#include <linux/major.h>
19#include <linux/proc_fs.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040020#include <linux/sched.h>
Dmitry Torokhov969b21c2006-04-02 00:09:34 -050021#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/poll.h>
23#include <linux/device.h>
Jes Sorensene676c232006-02-19 00:21:46 -050024#include <linux/mutex.h>
Dmitry Torokhov80064792007-08-30 00:22:11 -040025#include <linux/rcupdate.h>
Dmitry Torokhov15e184a2010-01-11 00:05:43 -080026#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
29MODULE_DESCRIPTION("Input core");
30MODULE_LICENSE("GPL");
31
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -070032#define INPUT_MAX_CHAR_DEVICES 1024
33#define INPUT_FIRST_DYNAMIC_DEV 256
34static DEFINE_IDA(input_ida);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36static LIST_HEAD(input_dev_list);
37static LIST_HEAD(input_handler_list);
38
Dmitry Torokhov80064792007-08-30 00:22:11 -040039/*
40 * input_mutex protects access to both input_dev_list and input_handler_list.
41 * This also causes input_[un]register_device and input_[un]register_handler
42 * be mutually exclusive which simplifies locking in drivers implementing
43 * input handlers.
44 */
45static DEFINE_MUTEX(input_mutex);
46
Henrik Rydberg4369c642012-09-15 15:23:35 +020047static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 };
48
Dmitry Torokhov80064792007-08-30 00:22:11 -040049static inline int is_event_supported(unsigned int code,
50 unsigned long *bm, unsigned int max)
51{
52 return code <= max && test_bit(code, bm);
53}
54
55static int input_defuzz_abs_event(int value, int old_val, int fuzz)
56{
57 if (fuzz) {
58 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
59 return old_val;
60
61 if (value > old_val - fuzz && value < old_val + fuzz)
62 return (old_val * 3 + value) / 4;
63
64 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
65 return (old_val + value) / 2;
66 }
67
68 return value;
69}
70
Dmitry Torokhov80064792007-08-30 00:22:11 -040071static void input_start_autorepeat(struct input_dev *dev, int code)
72{
73 if (test_bit(EV_REP, dev->evbit) &&
74 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
Kees Cook4e974c12017-11-03 12:21:48 -070075 dev->timer.function) {
Dmitry Torokhov80064792007-08-30 00:22:11 -040076 dev->repeat_key = code;
77 mod_timer(&dev->timer,
78 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
79 }
80}
81
Johannes Berge7b5c1e2009-01-29 23:17:52 -080082static void input_stop_autorepeat(struct input_dev *dev)
83{
84 del_timer(&dev->timer);
85}
86
Dmitry Torokhov80064792007-08-30 00:22:11 -040087/*
88 * Pass event first through all filters and then, if event has not been
89 * filtered out, through all open handles. This function is called with
90 * dev->event_lock held and interrupts disabled.
91 */
Henrik Rydberg4369c642012-09-15 15:23:35 +020092static unsigned int input_to_handler(struct input_handle *handle,
93 struct input_value *vals, unsigned int count)
Dmitry Torokhov80064792007-08-30 00:22:11 -040094{
Henrik Rydberg4369c642012-09-15 15:23:35 +020095 struct input_handler *handler = handle->handler;
96 struct input_value *end = vals;
97 struct input_value *v;
98
Anshul Garg2c50ad342015-01-08 13:47:37 -080099 if (handler->filter) {
100 for (v = vals; v != vals + count; v++) {
101 if (handler->filter(handle, v->type, v->code, v->value))
102 continue;
103 if (end != v)
104 *end = *v;
105 end++;
106 }
107 count = end - vals;
Henrik Rydberg4369c642012-09-15 15:23:35 +0200108 }
109
Henrik Rydberg4369c642012-09-15 15:23:35 +0200110 if (!count)
111 return 0;
112
113 if (handler->events)
114 handler->events(handle, vals, count);
115 else if (handler->event)
Anshul Garg2c50ad342015-01-08 13:47:37 -0800116 for (v = vals; v != vals + count; v++)
Henrik Rydberg4369c642012-09-15 15:23:35 +0200117 handler->event(handle, v->type, v->code, v->value);
118
119 return count;
120}
121
122/*
123 * Pass values first through all filters and then, if event has not been
124 * filtered out, through all open handles. This function is called with
125 * dev->event_lock held and interrupts disabled.
126 */
127static void input_pass_values(struct input_dev *dev,
128 struct input_value *vals, unsigned int count)
129{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400130 struct input_handle *handle;
Henrik Rydberg4369c642012-09-15 15:23:35 +0200131 struct input_value *v;
132
133 if (!count)
134 return;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400135
136 rcu_read_lock();
137
138 handle = rcu_dereference(dev->grab);
Henrik Rydberg4369c642012-09-15 15:23:35 +0200139 if (handle) {
140 count = input_to_handler(handle, vals, count);
141 } else {
142 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
Anshul Garg2c50ad342015-01-08 13:47:37 -0800143 if (handle->open) {
Henrik Rydberg4369c642012-09-15 15:23:35 +0200144 count = input_to_handler(handle, vals, count);
Anshul Garg2c50ad342015-01-08 13:47:37 -0800145 if (!count)
146 break;
147 }
Dmitry Torokhov80064792007-08-30 00:22:11 -0400148 }
149
150 rcu_read_unlock();
Henrik Rydberg352ac4b2012-08-10 21:36:15 +0200151
Henrik Rydberg4369c642012-09-15 15:23:35 +0200152 /* trigger auto repeat for key events */
Anshul Garg5ab17142015-01-08 13:41:24 -0800153 if (test_bit(EV_REP, dev->evbit) && test_bit(EV_KEY, dev->evbit)) {
154 for (v = vals; v != vals + count; v++) {
155 if (v->type == EV_KEY && v->value != 2) {
156 if (v->value)
157 input_start_autorepeat(dev, v->code);
158 else
159 input_stop_autorepeat(dev);
160 }
Henrik Rydberg4369c642012-09-15 15:23:35 +0200161 }
162 }
163}
164
165static void input_pass_event(struct input_dev *dev,
166 unsigned int type, unsigned int code, int value)
167{
168 struct input_value vals[] = { { type, code, value } };
169
170 input_pass_values(dev, vals, ARRAY_SIZE(vals));
Dmitry Torokhov80064792007-08-30 00:22:11 -0400171}
172
173/*
174 * Generate software autorepeat event. Note that we take
175 * dev->event_lock here to avoid racing with input_event
176 * which may cause keys get "stuck".
177 */
Kees Cook4e974c12017-11-03 12:21:48 -0700178static void input_repeat_key(struct timer_list *t)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400179{
Kees Cook4e974c12017-11-03 12:21:48 -0700180 struct input_dev *dev = from_timer(dev, t, timer);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400181 unsigned long flags;
182
183 spin_lock_irqsave(&dev->event_lock, flags);
184
185 if (test_bit(dev->repeat_key, dev->key) &&
186 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
Henrik Rydberg4369c642012-09-15 15:23:35 +0200187 struct input_value vals[] = {
188 { EV_KEY, dev->repeat_key, 2 },
189 input_value_sync
190 };
Dmitry Torokhov80064792007-08-30 00:22:11 -0400191
Henrik Rydberg4369c642012-09-15 15:23:35 +0200192 input_pass_values(dev, vals, ARRAY_SIZE(vals));
Dmitry Torokhov80064792007-08-30 00:22:11 -0400193
194 if (dev->rep[REP_PERIOD])
195 mod_timer(&dev->timer, jiffies +
196 msecs_to_jiffies(dev->rep[REP_PERIOD]));
197 }
198
199 spin_unlock_irqrestore(&dev->event_lock, flags);
200}
201
Dmitry Torokhov80064792007-08-30 00:22:11 -0400202#define INPUT_IGNORE_EVENT 0
203#define INPUT_PASS_TO_HANDLERS 1
204#define INPUT_PASS_TO_DEVICE 2
Henrik Rydberg4369c642012-09-15 15:23:35 +0200205#define INPUT_SLOT 4
206#define INPUT_FLUSH 8
Dmitry Torokhov80064792007-08-30 00:22:11 -0400207#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
208
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700209static int input_handle_abs_event(struct input_dev *dev,
210 unsigned int code, int *pval)
211{
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200212 struct input_mt *mt = dev->mt;
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700213 bool is_mt_event;
214 int *pold;
215
216 if (code == ABS_MT_SLOT) {
217 /*
218 * "Stage" the event; we'll flush it later, when we
Dmitry Torokhov144c0f82010-09-03 10:31:05 -0700219 * get actual touch data.
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700220 */
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200221 if (mt && *pval >= 0 && *pval < mt->num_slots)
222 mt->slot = *pval;
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700223
224 return INPUT_IGNORE_EVENT;
225 }
226
Henrik Rydbergb89529a2012-01-12 19:40:34 +0100227 is_mt_event = input_is_mt_value(code);
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700228
229 if (!is_mt_event) {
Daniel Mackd31b2862010-08-02 20:18:21 -0700230 pold = &dev->absinfo[code].value;
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200231 } else if (mt) {
232 pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST];
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700233 } else {
234 /*
Dmitry Torokhov144c0f82010-09-03 10:31:05 -0700235 * Bypass filtering for multi-touch events when
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700236 * not employing slots.
237 */
238 pold = NULL;
239 }
240
241 if (pold) {
242 *pval = input_defuzz_abs_event(*pval, *pold,
Daniel Mackd31b2862010-08-02 20:18:21 -0700243 dev->absinfo[code].fuzz);
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700244 if (*pold == *pval)
245 return INPUT_IGNORE_EVENT;
246
247 *pold = *pval;
248 }
249
250 /* Flush pending "slot" event */
Henrik Rydberg8d18fba2012-09-15 15:15:58 +0200251 if (is_mt_event && mt && mt->slot != input_abs_get_val(dev, ABS_MT_SLOT)) {
252 input_abs_set_val(dev, ABS_MT_SLOT, mt->slot);
Henrik Rydberg4369c642012-09-15 15:23:35 +0200253 return INPUT_PASS_TO_HANDLERS | INPUT_SLOT;
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700254 }
255
256 return INPUT_PASS_TO_HANDLERS;
257}
258
Henrik Rydberg4369c642012-09-15 15:23:35 +0200259static int input_get_disposition(struct input_dev *dev,
Dmitry Torokhov50c5d362014-07-19 16:30:31 -0700260 unsigned int type, unsigned int code, int *pval)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400261{
262 int disposition = INPUT_IGNORE_EVENT;
Dmitry Torokhov50c5d362014-07-19 16:30:31 -0700263 int value = *pval;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400264
265 switch (type) {
266
267 case EV_SYN:
268 switch (code) {
269 case SYN_CONFIG:
270 disposition = INPUT_PASS_TO_ALL;
271 break;
272
273 case SYN_REPORT:
Henrik Rydberg4369c642012-09-15 15:23:35 +0200274 disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400275 break;
Henrik Rydberg5e5ee682009-04-28 07:47:33 -0700276 case SYN_MT_REPORT:
Henrik Rydberg5e5ee682009-04-28 07:47:33 -0700277 disposition = INPUT_PASS_TO_HANDLERS;
278 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400279 }
280 break;
281
282 case EV_KEY:
Henrik Rydberg06721202012-08-10 21:39:38 +0200283 if (is_event_supported(code, dev->keybit, KEY_MAX)) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400284
Henrik Rydberg06721202012-08-10 21:39:38 +0200285 /* auto-repeat bypasses state updates */
286 if (value == 2) {
287 disposition = INPUT_PASS_TO_HANDLERS;
288 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400289 }
290
Henrik Rydberg06721202012-08-10 21:39:38 +0200291 if (!!test_bit(code, dev->key) != !!value) {
292
Dmitry Torokhov80064792007-08-30 00:22:11 -0400293 __change_bit(code, dev->key);
Henrik Rydberg06721202012-08-10 21:39:38 +0200294 disposition = INPUT_PASS_TO_HANDLERS;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400295 }
Dmitry Torokhov80064792007-08-30 00:22:11 -0400296 }
297 break;
298
299 case EV_SW:
300 if (is_event_supported(code, dev->swbit, SW_MAX) &&
Henrik Rydberg06721202012-08-10 21:39:38 +0200301 !!test_bit(code, dev->sw) != !!value) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400302
303 __change_bit(code, dev->sw);
304 disposition = INPUT_PASS_TO_HANDLERS;
305 }
306 break;
307
308 case EV_ABS:
Henrik Rydberg40d007e2010-07-15 23:10:10 -0700309 if (is_event_supported(code, dev->absbit, ABS_MAX))
Dmitry Torokhov9ae43452011-02-02 23:04:27 -0800310 disposition = input_handle_abs_event(dev, code, &value);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400311
Dmitry Torokhov80064792007-08-30 00:22:11 -0400312 break;
313
314 case EV_REL:
315 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
316 disposition = INPUT_PASS_TO_HANDLERS;
317
318 break;
319
320 case EV_MSC:
321 if (is_event_supported(code, dev->mscbit, MSC_MAX))
322 disposition = INPUT_PASS_TO_ALL;
323
324 break;
325
326 case EV_LED:
327 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
Henrik Rydberg06721202012-08-10 21:39:38 +0200328 !!test_bit(code, dev->led) != !!value) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400329
330 __change_bit(code, dev->led);
331 disposition = INPUT_PASS_TO_ALL;
332 }
333 break;
334
335 case EV_SND:
336 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
337
338 if (!!test_bit(code, dev->snd) != !!value)
339 __change_bit(code, dev->snd);
340 disposition = INPUT_PASS_TO_ALL;
341 }
342 break;
343
344 case EV_REP:
345 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
346 dev->rep[code] = value;
347 disposition = INPUT_PASS_TO_ALL;
348 }
349 break;
350
351 case EV_FF:
352 if (value >= 0)
353 disposition = INPUT_PASS_TO_ALL;
354 break;
Richard Purdieed2fa4d2008-01-03 10:46:21 -0500355
356 case EV_PWR:
357 disposition = INPUT_PASS_TO_ALL;
358 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400359 }
360
Dmitry Torokhov50c5d362014-07-19 16:30:31 -0700361 *pval = value;
Henrik Rydberg4369c642012-09-15 15:23:35 +0200362 return disposition;
363}
364
365static void input_handle_event(struct input_dev *dev,
366 unsigned int type, unsigned int code, int value)
367{
Dmitry Torokhovb55eb292016-01-23 11:29:13 -0800368 int disposition = input_get_disposition(dev, type, code, &value);
Henrik Rydberg4369c642012-09-15 15:23:35 +0200369
Dmitry Torokhovb55eb292016-01-23 11:29:13 -0800370 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
371 add_input_randomness(type, code, value);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400372
373 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
374 dev->event(dev, type, code, value);
375
Henrik Rydberg4369c642012-09-15 15:23:35 +0200376 if (!dev->vals)
377 return;
378
379 if (disposition & INPUT_PASS_TO_HANDLERS) {
380 struct input_value *v;
381
382 if (disposition & INPUT_SLOT) {
383 v = &dev->vals[dev->num_vals++];
384 v->type = EV_ABS;
385 v->code = ABS_MT_SLOT;
386 v->value = dev->mt->slot;
387 }
388
389 v = &dev->vals[dev->num_vals++];
390 v->type = type;
391 v->code = code;
392 v->value = value;
393 }
394
395 if (disposition & INPUT_FLUSH) {
396 if (dev->num_vals >= 2)
397 input_pass_values(dev, dev->vals, dev->num_vals);
398 dev->num_vals = 0;
399 } else if (dev->num_vals >= dev->max_vals - 2) {
400 dev->vals[dev->num_vals++] = input_value_sync;
401 input_pass_values(dev, dev->vals, dev->num_vals);
402 dev->num_vals = 0;
403 }
404
Dmitry Torokhov80064792007-08-30 00:22:11 -0400405}
406
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400407/**
408 * input_event() - report new input event
Dmitry Torokhov14471902006-11-02 23:26:55 -0500409 * @dev: device that generated the event
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400410 * @type: type of the event
411 * @code: event code
412 * @value: value of the event
413 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400414 * This function should be used by drivers implementing various input
Dmitry Torokhovdf2d4632009-12-11 21:57:31 -0800415 * devices to report input events. See also input_inject_event().
416 *
417 * NOTE: input_event() may be safely used right after input device was
418 * allocated with input_allocate_device(), even before it is registered
419 * with input_register_device(), but the event will not reach any of the
420 * input handlers. Such early invocation of input_event() may be used
421 * to 'seed' initial state of a switch or initial position of absolute
422 * axis, etc.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400423 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400424void input_event(struct input_dev *dev,
425 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400427 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Dmitry Torokhov80064792007-08-30 00:22:11 -0400429 if (is_event_supported(type, dev->evbit, EV_MAX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Dmitry Torokhov80064792007-08-30 00:22:11 -0400431 spin_lock_irqsave(&dev->event_lock, flags);
Dmitry Torokhov9ae43452011-02-02 23:04:27 -0800432 input_handle_event(dev, type, code, value);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400433 spin_unlock_irqrestore(&dev->event_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400436EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400438/**
439 * input_inject_event() - send input event from input handler
440 * @handle: input handle to send event through
441 * @type: type of the event
442 * @code: event code
443 * @value: value of the event
444 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400445 * Similar to input_event() but will ignore event if device is
446 * "grabbed" and handle injecting event is not the one that owns
447 * the device.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400448 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400449void input_inject_event(struct input_handle *handle,
450 unsigned int type, unsigned int code, int value)
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400451{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400452 struct input_dev *dev = handle->dev;
453 struct input_handle *grab;
454 unsigned long flags;
455
456 if (is_event_supported(type, dev->evbit, EV_MAX)) {
457 spin_lock_irqsave(&dev->event_lock, flags);
458
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400459 rcu_read_lock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400460 grab = rcu_dereference(dev->grab);
461 if (!grab || grab == handle)
Dmitry Torokhov9ae43452011-02-02 23:04:27 -0800462 input_handle_event(dev, type, code, value);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400463 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400464
465 spin_unlock_irqrestore(&dev->event_lock, flags);
466 }
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400467}
468EXPORT_SYMBOL(input_inject_event);
469
Dmitry Torokhov80064792007-08-30 00:22:11 -0400470/**
Daniel Mackd31b2862010-08-02 20:18:21 -0700471 * input_alloc_absinfo - allocates array of input_absinfo structs
472 * @dev: the input device emitting absolute events
473 *
474 * If the absinfo struct the caller asked for is already allocated, this
475 * functions will not do anything.
476 */
477void input_alloc_absinfo(struct input_dev *dev)
478{
Dmitry Torokhov100294c2018-08-06 15:10:40 -0700479 if (dev->absinfo)
480 return;
Daniel Mackd31b2862010-08-02 20:18:21 -0700481
Dmitry Torokhov100294c2018-08-06 15:10:40 -0700482 dev->absinfo = kcalloc(ABS_CNT, sizeof(*dev->absinfo), GFP_KERNEL);
483 if (!dev->absinfo) {
484 dev_err(dev->dev.parent ?: &dev->dev,
485 "%s: unable to allocate memory\n", __func__);
486 /*
487 * We will handle this allocation failure in
488 * input_register_device() when we refuse to register input
489 * device with ABS bits but without absinfo.
490 */
491 }
Daniel Mackd31b2862010-08-02 20:18:21 -0700492}
493EXPORT_SYMBOL(input_alloc_absinfo);
494
495void input_set_abs_params(struct input_dev *dev, unsigned int axis,
496 int min, int max, int fuzz, int flat)
497{
498 struct input_absinfo *absinfo;
499
500 input_alloc_absinfo(dev);
501 if (!dev->absinfo)
502 return;
503
504 absinfo = &dev->absinfo[axis];
505 absinfo->minimum = min;
506 absinfo->maximum = max;
507 absinfo->fuzz = fuzz;
508 absinfo->flat = flat;
509
Dmitry Torokhov2c9a9cf2014-10-08 09:28:32 -0700510 __set_bit(EV_ABS, dev->evbit);
511 __set_bit(axis, dev->absbit);
Daniel Mackd31b2862010-08-02 20:18:21 -0700512}
513EXPORT_SYMBOL(input_set_abs_params);
514
515
516/**
Dmitry Torokhov80064792007-08-30 00:22:11 -0400517 * input_grab_device - grabs device for exclusive use
518 * @handle: input handle that wants to own the device
519 *
520 * When a device is grabbed by an input handle all events generated by
521 * the device are delivered only to this handle. Also events injected
522 * by other input handles are ignored while device is grabbed.
523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524int input_grab_device(struct input_handle *handle)
525{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400526 struct input_dev *dev = handle->dev;
527 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Dmitry Torokhov80064792007-08-30 00:22:11 -0400529 retval = mutex_lock_interruptible(&dev->mutex);
530 if (retval)
531 return retval;
532
533 if (dev->grab) {
534 retval = -EBUSY;
535 goto out;
536 }
537
538 rcu_assign_pointer(dev->grab, handle);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400539
540 out:
541 mutex_unlock(&dev->mutex);
542 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400544EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dmitry Torokhov80064792007-08-30 00:22:11 -0400546static void __input_release_device(struct input_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400548 struct input_dev *dev = handle->dev;
Dmitry Torokhovadc46332012-10-24 23:53:01 -0700549 struct input_handle *grabber;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400550
Dmitry Torokhovadc46332012-10-24 23:53:01 -0700551 grabber = rcu_dereference_protected(dev->grab,
552 lockdep_is_held(&dev->mutex));
553 if (grabber == handle) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400554 rcu_assign_pointer(dev->grab, NULL);
555 /* Make sure input_pass_event() notices that grab is gone */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400556 synchronize_rcu();
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400557
558 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400559 if (handle->open && handle->handler->start)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400560 handle->handler->start(handle);
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
Dmitry Torokhov80064792007-08-30 00:22:11 -0400563
564/**
565 * input_release_device - release previously grabbed device
566 * @handle: input handle that owns the device
567 *
568 * Releases previously grabbed device so that other input handles can
569 * start receiving input events. Upon release all handlers attached
570 * to the device have their start() method called so they have a change
571 * to synchronize device state with the rest of the system.
572 */
573void input_release_device(struct input_handle *handle)
574{
575 struct input_dev *dev = handle->dev;
576
577 mutex_lock(&dev->mutex);
578 __input_release_device(handle);
579 mutex_unlock(&dev->mutex);
580}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400581EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Dmitry Torokhov80064792007-08-30 00:22:11 -0400583/**
584 * input_open_device - open input device
585 * @handle: handle through which device is being accessed
586 *
587 * This function should be called by input handlers when they
588 * want to start receive events from given input device.
589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590int input_open_device(struct input_handle *handle)
591{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500592 struct input_dev *dev = handle->dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400593 int retval;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500594
Dmitry Torokhov80064792007-08-30 00:22:11 -0400595 retval = mutex_lock_interruptible(&dev->mutex);
596 if (retval)
597 return retval;
598
599 if (dev->going_away) {
600 retval = -ENODEV;
601 goto out;
602 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500605
606 if (!dev->users++ && dev->open)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400607 retval = dev->open(dev);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500608
Dmitry Torokhov80064792007-08-30 00:22:11 -0400609 if (retval) {
610 dev->users--;
611 if (!--handle->open) {
612 /*
613 * Make sure we are not delivering any more events
614 * through this handle
615 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400616 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400617 }
618 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500619
Dmitry Torokhov80064792007-08-30 00:22:11 -0400620 out:
Jes Sorensene676c232006-02-19 00:21:46 -0500621 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400622 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400624EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Dmitry Torokhov80064792007-08-30 00:22:11 -0400626int input_flush_device(struct input_handle *handle, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400628 struct input_dev *dev = handle->dev;
629 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Dmitry Torokhov80064792007-08-30 00:22:11 -0400631 retval = mutex_lock_interruptible(&dev->mutex);
632 if (retval)
633 return retval;
634
635 if (dev->flush)
636 retval = dev->flush(dev, file);
637
638 mutex_unlock(&dev->mutex);
639 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400641EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Dmitry Torokhov80064792007-08-30 00:22:11 -0400643/**
644 * input_close_device - close input device
645 * @handle: handle through which device is being accessed
646 *
647 * This function should be called by input handlers when they
648 * want to stop receive events from given input device.
649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650void input_close_device(struct input_handle *handle)
651{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500652 struct input_dev *dev = handle->dev;
653
Jes Sorensene676c232006-02-19 00:21:46 -0500654 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500655
Dmitry Torokhov80064792007-08-30 00:22:11 -0400656 __input_release_device(handle);
657
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500658 if (!--dev->users && dev->close)
659 dev->close(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400660
661 if (!--handle->open) {
662 /*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400663 * synchronize_rcu() makes sure that input_pass_event()
Dmitry Torokhov80064792007-08-30 00:22:11 -0400664 * completed and that no more input events are delivered
665 * through this handle
666 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400667 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400668 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500669
Jes Sorensene676c232006-02-19 00:21:46 -0500670 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400672EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Dmitry Torokhov80064792007-08-30 00:22:11 -0400674/*
Oliver Neukum866d7d72010-07-01 09:01:50 -0700675 * Simulate keyup events for all keys that are marked as pressed.
676 * The function must be called with dev->event_lock held.
677 */
678static void input_dev_release_keys(struct input_dev *dev)
679{
Dmitry Torokhov00159f12015-08-06 19:15:30 -0700680 bool need_sync = false;
Oliver Neukum866d7d72010-07-01 09:01:50 -0700681 int code;
682
683 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
Dmitry Torokhov00159f12015-08-06 19:15:30 -0700684 for_each_set_bit(code, dev->key, KEY_CNT) {
Anshul Garg3e2b03d2015-06-25 13:33:12 -0700685 input_pass_event(dev, EV_KEY, code, 0);
Dmitry Torokhov00159f12015-08-06 19:15:30 -0700686 need_sync = true;
687 }
688
689 if (need_sync)
690 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
691
Anshul Garg3e2b03d2015-06-25 13:33:12 -0700692 memset(dev->key, 0, sizeof(dev->key));
Oliver Neukum866d7d72010-07-01 09:01:50 -0700693 }
694}
695
696/*
Dmitry Torokhov80064792007-08-30 00:22:11 -0400697 * Prepare device for unregistering
698 */
699static void input_disconnect_device(struct input_dev *dev)
700{
701 struct input_handle *handle;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400702
703 /*
704 * Mark device as going away. Note that we take dev->mutex here
705 * not to protect access to dev->going_away but rather to ensure
706 * that there are no threads in the middle of input_open_device()
707 */
708 mutex_lock(&dev->mutex);
Dmitry Torokhovffd0db92009-09-16 01:06:43 -0700709 dev->going_away = true;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400710 mutex_unlock(&dev->mutex);
711
712 spin_lock_irq(&dev->event_lock);
713
714 /*
715 * Simulate keyup events for all pressed keys so that handlers
716 * are not left with "stuck" keys. The driver may continue
717 * generate events even after we done here but they will not
718 * reach any handlers.
719 */
Oliver Neukum866d7d72010-07-01 09:01:50 -0700720 input_dev_release_keys(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400721
722 list_for_each_entry(handle, &dev->h_list, d_node)
723 handle->open = 0;
724
725 spin_unlock_irq(&dev->event_lock);
726}
727
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700728/**
729 * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry
730 * @ke: keymap entry containing scancode to be converted.
731 * @scancode: pointer to the location where converted scancode should
732 * be stored.
733 *
734 * This function is used to convert scancode stored in &struct keymap_entry
735 * into scalar form understood by legacy keymap handling methods. These
736 * methods expect scancodes to be represented as 'unsigned int'.
737 */
738int input_scancode_to_scalar(const struct input_keymap_entry *ke,
739 unsigned int *scancode)
740{
741 switch (ke->len) {
742 case 1:
743 *scancode = *((u8 *)ke->scancode);
744 break;
745
746 case 2:
747 *scancode = *((u16 *)ke->scancode);
748 break;
749
750 case 4:
751 *scancode = *((u32 *)ke->scancode);
752 break;
753
754 default:
755 return -EINVAL;
756 }
757
758 return 0;
759}
760EXPORT_SYMBOL(input_scancode_to_scalar);
761
762/*
763 * Those routines handle the default case where no [gs]etkeycode() is
764 * defined. In this case, an array indexed by the scancode is used.
765 */
766
767static unsigned int input_fetch_keycode(struct input_dev *dev,
768 unsigned int index)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400769{
770 switch (dev->keycodesize) {
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700771 case 1:
772 return ((u8 *)dev->keycode)[index];
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400773
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700774 case 2:
775 return ((u16 *)dev->keycode)[index];
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400776
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700777 default:
778 return ((u32 *)dev->keycode)[index];
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400779 }
780}
781
782static int input_default_getkeycode(struct input_dev *dev,
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700783 struct input_keymap_entry *ke)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400784{
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700785 unsigned int index;
786 int error;
787
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400788 if (!dev->keycodesize)
789 return -EINVAL;
790
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700791 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
792 index = ke->index;
793 else {
794 error = input_scancode_to_scalar(ke, &index);
795 if (error)
796 return error;
797 }
798
799 if (index >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400800 return -EINVAL;
801
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700802 ke->keycode = input_fetch_keycode(dev, index);
803 ke->index = index;
804 ke->len = sizeof(index);
805 memcpy(ke->scancode, &index, sizeof(index));
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400806
807 return 0;
808}
809
810static int input_default_setkeycode(struct input_dev *dev,
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700811 const struct input_keymap_entry *ke,
812 unsigned int *old_keycode)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400813{
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700814 unsigned int index;
815 int error;
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400816 int i;
817
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400818 if (!dev->keycodesize)
819 return -EINVAL;
820
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700821 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
822 index = ke->index;
823 } else {
824 error = input_scancode_to_scalar(ke, &index);
825 if (error)
826 return error;
827 }
828
829 if (index >= dev->keycodemax)
830 return -EINVAL;
831
Mattia Dongilide391d12010-11-18 09:06:43 -0800832 if (dev->keycodesize < sizeof(ke->keycode) &&
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700833 (ke->keycode >> (dev->keycodesize * 8)))
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400834 return -EINVAL;
835
836 switch (dev->keycodesize) {
837 case 1: {
838 u8 *k = (u8 *)dev->keycode;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700839 *old_keycode = k[index];
840 k[index] = ke->keycode;
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400841 break;
842 }
843 case 2: {
844 u16 *k = (u16 *)dev->keycode;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700845 *old_keycode = k[index];
846 k[index] = ke->keycode;
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400847 break;
848 }
849 default: {
850 u32 *k = (u32 *)dev->keycode;
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700851 *old_keycode = k[index];
852 k[index] = ke->keycode;
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400853 break;
854 }
855 }
856
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700857 __clear_bit(*old_keycode, dev->keybit);
858 __set_bit(ke->keycode, dev->keybit);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400859
860 for (i = 0; i < dev->keycodemax; i++) {
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700861 if (input_fetch_keycode(dev, i) == *old_keycode) {
862 __set_bit(*old_keycode, dev->keybit);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400863 break; /* Setting the bit twice is useless, so break */
864 }
865 }
866
867 return 0;
868}
869
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400870/**
871 * input_get_keycode - retrieve keycode currently mapped to a given scancode
872 * @dev: input device which keymap is being queried
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700873 * @ke: keymap entry
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400874 *
875 * This function should be called by anyone interested in retrieving current
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700876 * keymap. Presently evdev handlers use it.
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400877 */
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700878int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400879{
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700880 unsigned long flags;
881 int retval;
882
883 spin_lock_irqsave(&dev->event_lock, flags);
Dmitry Torokhovaebd6362011-01-31 21:06:39 -0800884 retval = dev->getkeycode(dev, ke);
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700885 spin_unlock_irqrestore(&dev->event_lock, flags);
Dmitry Torokhovaebd6362011-01-31 21:06:39 -0800886
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700887 return retval;
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400888}
889EXPORT_SYMBOL(input_get_keycode);
890
891/**
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700892 * input_set_keycode - attribute a keycode to a given scancode
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400893 * @dev: input device which keymap is being updated
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700894 * @ke: new keymap entry
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400895 *
896 * This function should be called by anyone needing to update current
897 * keymap. Presently keyboard and evdev handlers use it.
898 */
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800899int input_set_keycode(struct input_dev *dev,
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700900 const struct input_keymap_entry *ke)
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400901{
902 unsigned long flags;
Dmitry Torokhovfd6cf3d2010-07-14 00:25:21 -0700903 unsigned int old_keycode;
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400904 int retval;
905
Mauro Carvalho Chehab8613e4c2010-09-09 21:54:22 -0700906 if (ke->keycode > KEY_MAX)
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400907 return -EINVAL;
908
909 spin_lock_irqsave(&dev->event_lock, flags);
910
Dmitry Torokhovaebd6362011-01-31 21:06:39 -0800911 retval = dev->setkeycode(dev, ke, &old_keycode);
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400912 if (retval)
913 goto out;
914
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800915 /* Make sure KEY_RESERVED did not get enabled. */
916 __clear_bit(KEY_RESERVED, dev->keybit);
917
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400918 /*
919 * Simulate keyup event if keycode is not present
920 * in the keymap anymore
921 */
922 if (test_bit(EV_KEY, dev->evbit) &&
923 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
924 __test_and_clear_bit(old_keycode, dev->key)) {
Henrik Rydberg4369c642012-09-15 15:23:35 +0200925 struct input_value vals[] = {
926 { EV_KEY, old_keycode, 0 },
927 input_value_sync
928 };
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400929
Henrik Rydberg4369c642012-09-15 15:23:35 +0200930 input_pass_values(dev, vals, ARRAY_SIZE(vals));
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400931 }
932
933 out:
934 spin_unlock_irqrestore(&dev->event_lock, flags);
935
936 return retval;
937}
938EXPORT_SYMBOL(input_set_keycode);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400939
Dmitry Torokhov55dfce82017-10-09 11:09:33 -0700940bool input_match_device_id(const struct input_dev *dev,
941 const struct input_device_id *id)
942{
943 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
944 if (id->bustype != dev->id.bustype)
945 return false;
946
947 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
948 if (id->vendor != dev->id.vendor)
949 return false;
950
951 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
952 if (id->product != dev->id.product)
953 return false;
954
955 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
956 if (id->version != dev->id.version)
957 return false;
958
959 if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX) ||
960 !bitmap_subset(id->keybit, dev->keybit, KEY_MAX) ||
961 !bitmap_subset(id->relbit, dev->relbit, REL_MAX) ||
962 !bitmap_subset(id->absbit, dev->absbit, ABS_MAX) ||
963 !bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX) ||
964 !bitmap_subset(id->ledbit, dev->ledbit, LED_MAX) ||
965 !bitmap_subset(id->sndbit, dev->sndbit, SND_MAX) ||
966 !bitmap_subset(id->ffbit, dev->ffbit, FF_MAX) ||
Dmitry Torokhov8724ecb2017-10-09 12:01:14 -0700967 !bitmap_subset(id->swbit, dev->swbit, SW_MAX) ||
968 !bitmap_subset(id->propbit, dev->propbit, INPUT_PROP_MAX)) {
Dmitry Torokhov55dfce82017-10-09 11:09:33 -0700969 return false;
970 }
971
972 return true;
973}
974EXPORT_SYMBOL(input_match_device_id);
975
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800976static const struct input_device_id *input_match_device(struct input_handler *handler,
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400977 struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800979 const struct input_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800981 for (id = handler->id_table; id->flags || id->driver_info; id++) {
Dmitry Torokhov55dfce82017-10-09 11:09:33 -0700982 if (input_match_device_id(dev, id) &&
983 (!handler->match || handler->match(handler, dev))) {
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800984 return id;
Dmitry Torokhov55dfce82017-10-09 11:09:33 -0700985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
988 return NULL;
989}
990
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400991static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
992{
993 const struct input_device_id *id;
994 int error;
995
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800996 id = input_match_device(handler, dev);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400997 if (!id)
998 return -ENODEV;
999
1000 error = handler->connect(handler, dev, id);
1001 if (error && error != -ENODEV)
Joe Perchesda0c4902010-11-29 23:33:07 -08001002 pr_err("failed to attach handler %s to device %s, error: %d\n",
1003 handler->name, kobject_name(&dev->dev.kobj), error);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001004
1005 return error;
1006}
1007
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001008#ifdef CONFIG_COMPAT
1009
1010static int input_bits_to_string(char *buf, int buf_size,
1011 unsigned long bits, bool skip_empty)
1012{
1013 int len = 0;
1014
Andrew Mortonb8b4ead2016-03-25 14:20:47 -07001015 if (in_compat_syscall()) {
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001016 u32 dword = bits >> 32;
1017 if (dword || !skip_empty)
1018 len += snprintf(buf, buf_size, "%x ", dword);
1019
1020 dword = bits & 0xffffffffUL;
1021 if (dword || !skip_empty || len)
1022 len += snprintf(buf + len, max(buf_size - len, 0),
1023 "%x", dword);
1024 } else {
1025 if (bits || !skip_empty)
1026 len += snprintf(buf, buf_size, "%lx", bits);
1027 }
1028
1029 return len;
1030}
1031
1032#else /* !CONFIG_COMPAT */
1033
1034static int input_bits_to_string(char *buf, int buf_size,
1035 unsigned long bits, bool skip_empty)
1036{
1037 return bits || !skip_empty ?
1038 snprintf(buf, buf_size, "%lx", bits) : 0;
1039}
1040
1041#endif
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001044
1045static struct proc_dir_entry *proc_bus_input_dir;
1046static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
1047static int input_devices_state;
1048
1049static inline void input_wakeup_procfs_readers(void)
1050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 input_devices_state++;
1052 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Al Viroafc9a422017-07-03 06:39:46 -04001055static __poll_t input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001057 poll_wait(file, &input_devices_poll_wait, wait);
Dmitry Torokhovfa886612009-03-04 00:52:20 -08001058 if (file->f_version != input_devices_state) {
1059 file->f_version = input_devices_state;
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001060 return EPOLLIN | EPOLLRDNORM;
Dmitry Torokhovfa886612009-03-04 00:52:20 -08001061 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001062
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001066union input_seq_state {
1067 struct {
1068 unsigned short pos;
1069 bool mutex_acquired;
1070 };
1071 void *p;
1072};
1073
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001074static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
1075{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001076 union input_seq_state *state = (union input_seq_state *)&seq->private;
1077 int error;
1078
1079 /* We need to fit into seq->private pointer */
1080 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
1081
1082 error = mutex_lock_interruptible(&input_mutex);
1083 if (error) {
1084 state->mutex_acquired = false;
1085 return ERR_PTR(error);
1086 }
1087
1088 state->mutex_acquired = true;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001089
Pavel Emelianovad5d9722007-07-18 00:38:32 -04001090 return seq_list_start(&input_dev_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001091}
1092
1093static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1094{
Pavel Emelianovad5d9722007-07-18 00:38:32 -04001095 return seq_list_next(v, &input_dev_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001096}
1097
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001098static void input_seq_stop(struct seq_file *seq, void *v)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001099{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001100 union input_seq_state *state = (union input_seq_state *)&seq->private;
1101
1102 if (state->mutex_acquired)
1103 mutex_unlock(&input_mutex);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001104}
1105
1106static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
1107 unsigned long *bitmap, int max)
1108{
1109 int i;
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001110 bool skip_empty = true;
1111 char buf[18];
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001112
1113 seq_printf(seq, "B: %s=", name);
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001114
1115 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1116 if (input_bits_to_string(buf, sizeof(buf),
1117 bitmap[i], skip_empty)) {
1118 skip_empty = false;
1119 seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
1120 }
1121 }
1122
1123 /*
1124 * If no output was produced print a single 0.
1125 */
1126 if (skip_empty)
Markus Elfringbb546132017-05-09 17:48:45 -07001127 seq_putc(seq, '0');
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001128
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001129 seq_putc(seq, '\n');
1130}
1131
1132static int input_devices_seq_show(struct seq_file *seq, void *v)
1133{
1134 struct input_dev *dev = container_of(v, struct input_dev, node);
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001135 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 struct input_handle *handle;
1137
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001138 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
1139 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001141 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
1142 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
1143 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
Dmitry Torokhov15e03ae2007-03-07 23:20:17 -05001144 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
Markus Elfring63c95762017-05-09 17:49:59 -07001145 seq_puts(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001147 list_for_each_entry(handle, &dev->h_list, d_node)
1148 seq_printf(seq, "%s ", handle->name);
1149 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -05001150
Henrik Rydberg85b77202010-12-18 20:51:13 +01001151 input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX);
1152
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001153 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
1154 if (test_bit(EV_KEY, dev->evbit))
1155 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
1156 if (test_bit(EV_REL, dev->evbit))
1157 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
1158 if (test_bit(EV_ABS, dev->evbit))
1159 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
1160 if (test_bit(EV_MSC, dev->evbit))
1161 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
1162 if (test_bit(EV_LED, dev->evbit))
1163 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
1164 if (test_bit(EV_SND, dev->evbit))
1165 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
1166 if (test_bit(EV_FF, dev->evbit))
1167 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
1168 if (test_bit(EV_SW, dev->evbit))
1169 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001171 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001173 kfree(path);
1174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Jan Engelhardtcec69c32008-01-31 00:43:32 -05001177static const struct seq_operations input_devices_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001178 .start = input_devices_seq_start,
1179 .next = input_devices_seq_next,
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001180 .stop = input_seq_stop,
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001181 .show = input_devices_seq_show,
1182};
1183
1184static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001186 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001189static const struct file_operations input_devices_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001190 .owner = THIS_MODULE,
1191 .open = input_proc_devices_open,
1192 .poll = input_proc_devices_poll,
1193 .read = seq_read,
1194 .llseek = seq_lseek,
1195 .release = seq_release,
1196};
1197
1198static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
1199{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001200 union input_seq_state *state = (union input_seq_state *)&seq->private;
1201 int error;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001202
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001203 /* We need to fit into seq->private pointer */
1204 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
1205
1206 error = mutex_lock_interruptible(&input_mutex);
1207 if (error) {
1208 state->mutex_acquired = false;
1209 return ERR_PTR(error);
1210 }
1211
1212 state->mutex_acquired = true;
1213 state->pos = *pos;
1214
Pavel Emelianovad5d9722007-07-18 00:38:32 -04001215 return seq_list_start(&input_handler_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001216}
1217
1218static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1219{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001220 union input_seq_state *state = (union input_seq_state *)&seq->private;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001221
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001222 state->pos = *pos + 1;
1223 return seq_list_next(v, &input_handler_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001224}
1225
1226static int input_handlers_seq_show(struct seq_file *seq, void *v)
1227{
1228 struct input_handler *handler = container_of(v, struct input_handler, node);
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001229 union input_seq_state *state = (union input_seq_state *)&seq->private;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001230
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001231 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
Dmitry Torokhovef7995f2010-01-29 23:59:12 -08001232 if (handler->filter)
1233 seq_puts(seq, " (filter)");
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07001234 if (handler->legacy_minors)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001235 seq_printf(seq, " Minor=%d", handler->minor);
1236 seq_putc(seq, '\n');
1237
1238 return 0;
1239}
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001240
Jan Engelhardtcec69c32008-01-31 00:43:32 -05001241static const struct seq_operations input_handlers_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001242 .start = input_handlers_seq_start,
1243 .next = input_handlers_seq_next,
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001244 .stop = input_seq_stop,
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001245 .show = input_handlers_seq_show,
1246};
1247
1248static int input_proc_handlers_open(struct inode *inode, struct file *file)
1249{
1250 return seq_open(file, &input_handlers_seq_ops);
1251}
1252
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001253static const struct file_operations input_handlers_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001254 .owner = THIS_MODULE,
1255 .open = input_proc_handlers_open,
1256 .read = seq_read,
1257 .llseek = seq_lseek,
1258 .release = seq_release,
1259};
Luke Kosewskie334016fc2005-06-01 02:39:28 -05001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261static int __init input_proc_init(void)
1262{
1263 struct proc_dir_entry *entry;
1264
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001265 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001266 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001268
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07001269 entry = proc_create("devices", 0, proc_bus_input_dir,
1270 &input_devices_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001271 if (!entry)
1272 goto fail1;
1273
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07001274 entry = proc_create("handlers", 0, proc_bus_input_dir,
1275 &input_handlers_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001276 if (!entry)
1277 goto fail2;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001280
1281 fail2: remove_proc_entry("devices", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001282 fail1: remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001283 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001285
Andrew Mortonbeffbdc2005-07-01 23:54:30 -05001286static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001287{
1288 remove_proc_entry("devices", proc_bus_input_dir);
1289 remove_proc_entry("handlers", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001290 remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001291}
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001294static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001296static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297#endif
1298
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001299#define INPUT_DEV_STRING_ATTR_SHOW(name) \
1300static ssize_t input_dev_show_##name(struct device *dev, \
1301 struct device_attribute *attr, \
1302 char *buf) \
1303{ \
1304 struct input_dev *input_dev = to_input_dev(dev); \
1305 \
1306 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1307 input_dev->name ? input_dev->name : ""); \
1308} \
1309static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001310
1311INPUT_DEV_STRING_ATTR_SHOW(name);
1312INPUT_DEV_STRING_ATTR_SHOW(phys);
1313INPUT_DEV_STRING_ATTR_SHOW(uniq);
1314
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001315static int input_print_modalias_bits(char *buf, int size,
1316 char name, unsigned long *bm,
1317 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001318{
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001319 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001320
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001321 len += snprintf(buf, max(size, 0), "%c", name);
1322 for (i = min_bit; i < max_bit; i++)
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001323 if (bm[BIT_WORD(i)] & BIT_MASK(i))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001324 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001325 return len;
1326}
1327
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001328static int input_print_modalias(char *buf, int size, struct input_dev *id,
1329 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001330{
1331 int len;
1332
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001333 len = snprintf(buf, max(size, 0),
1334 "input:b%04Xv%04Xp%04Xe%04X-",
1335 id->id.bustype, id->id.vendor,
1336 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001337
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001338 len += input_print_modalias_bits(buf + len, size - len,
1339 'e', id->evbit, 0, EV_MAX);
1340 len += input_print_modalias_bits(buf + len, size - len,
1341 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1342 len += input_print_modalias_bits(buf + len, size - len,
1343 'r', id->relbit, 0, REL_MAX);
1344 len += input_print_modalias_bits(buf + len, size - len,
1345 'a', id->absbit, 0, ABS_MAX);
1346 len += input_print_modalias_bits(buf + len, size - len,
1347 'm', id->mscbit, 0, MSC_MAX);
1348 len += input_print_modalias_bits(buf + len, size - len,
1349 'l', id->ledbit, 0, LED_MAX);
1350 len += input_print_modalias_bits(buf + len, size - len,
1351 's', id->sndbit, 0, SND_MAX);
1352 len += input_print_modalias_bits(buf + len, size - len,
1353 'f', id->ffbit, 0, FF_MAX);
1354 len += input_print_modalias_bits(buf + len, size - len,
1355 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001356
1357 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001358 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001359
Rusty Russell1d8f4302005-12-07 21:40:34 +01001360 return len;
1361}
1362
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001363static ssize_t input_dev_show_modalias(struct device *dev,
1364 struct device_attribute *attr,
1365 char *buf)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001366{
1367 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001368 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001369
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001370 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1371
Richard Purdie8a3cf452006-06-26 01:48:21 -04001372 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001373}
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001374static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001375
Henrik Rydberg85b77202010-12-18 20:51:13 +01001376static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1377 int max, int add_cr);
1378
1379static ssize_t input_dev_show_properties(struct device *dev,
1380 struct device_attribute *attr,
1381 char *buf)
1382{
1383 struct input_dev *input_dev = to_input_dev(dev);
1384 int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit,
1385 INPUT_PROP_MAX, true);
1386 return min_t(int, len, PAGE_SIZE);
1387}
1388static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL);
1389
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001390static struct attribute *input_dev_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001391 &dev_attr_name.attr,
1392 &dev_attr_phys.attr,
1393 &dev_attr_uniq.attr,
1394 &dev_attr_modalias.attr,
Henrik Rydberg85b77202010-12-18 20:51:13 +01001395 &dev_attr_properties.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001396 NULL
1397};
1398
Arvind Yadav5e895b72017-07-10 20:15:49 -07001399static const struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001400 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001401};
1402
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001403#define INPUT_DEV_ID_ATTR(name) \
1404static ssize_t input_dev_show_id_##name(struct device *dev, \
1405 struct device_attribute *attr, \
1406 char *buf) \
1407{ \
1408 struct input_dev *input_dev = to_input_dev(dev); \
1409 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1410} \
1411static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001412
1413INPUT_DEV_ID_ATTR(bustype);
1414INPUT_DEV_ID_ATTR(vendor);
1415INPUT_DEV_ID_ATTR(product);
1416INPUT_DEV_ID_ATTR(version);
1417
1418static struct attribute *input_dev_id_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001419 &dev_attr_bustype.attr,
1420 &dev_attr_vendor.attr,
1421 &dev_attr_product.attr,
1422 &dev_attr_version.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001423 NULL
1424};
1425
Arvind Yadav5e895b72017-07-10 20:15:49 -07001426static const struct attribute_group input_dev_id_attr_group = {
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001427 .name = "id",
1428 .attrs = input_dev_id_attrs,
1429};
1430
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001431static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1432 int max, int add_cr)
1433{
1434 int i;
1435 int len = 0;
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001436 bool skip_empty = true;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001437
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001438 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1439 len += input_bits_to_string(buf + len, max(buf_size - len, 0),
1440 bitmap[i], skip_empty);
1441 if (len) {
1442 skip_empty = false;
1443 if (i > 0)
1444 len += snprintf(buf + len, max(buf_size - len, 0), " ");
1445 }
1446 }
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001447
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001448 /*
1449 * If no output was produced print a single 0.
1450 */
1451 if (len == 0)
1452 len = snprintf(buf, buf_size, "%d", 0);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001453
1454 if (add_cr)
1455 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1456
1457 return len;
1458}
1459
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001460#define INPUT_DEV_CAP_ATTR(ev, bm) \
1461static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1462 struct device_attribute *attr, \
1463 char *buf) \
1464{ \
1465 struct input_dev *input_dev = to_input_dev(dev); \
1466 int len = input_print_bitmap(buf, PAGE_SIZE, \
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001467 input_dev->bm##bit, ev##_MAX, \
1468 true); \
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001469 return min_t(int, len, PAGE_SIZE); \
1470} \
1471static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001472
1473INPUT_DEV_CAP_ATTR(EV, ev);
1474INPUT_DEV_CAP_ATTR(KEY, key);
1475INPUT_DEV_CAP_ATTR(REL, rel);
1476INPUT_DEV_CAP_ATTR(ABS, abs);
1477INPUT_DEV_CAP_ATTR(MSC, msc);
1478INPUT_DEV_CAP_ATTR(LED, led);
1479INPUT_DEV_CAP_ATTR(SND, snd);
1480INPUT_DEV_CAP_ATTR(FF, ff);
1481INPUT_DEV_CAP_ATTR(SW, sw);
1482
1483static struct attribute *input_dev_caps_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001484 &dev_attr_ev.attr,
1485 &dev_attr_key.attr,
1486 &dev_attr_rel.attr,
1487 &dev_attr_abs.attr,
1488 &dev_attr_msc.attr,
1489 &dev_attr_led.attr,
1490 &dev_attr_snd.attr,
1491 &dev_attr_ff.attr,
1492 &dev_attr_sw.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001493 NULL
1494};
1495
Arvind Yadav5e895b72017-07-10 20:15:49 -07001496static const struct attribute_group input_dev_caps_attr_group = {
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001497 .name = "capabilities",
1498 .attrs = input_dev_caps_attrs,
1499};
1500
David Brownella4dbd672009-06-24 10:06:31 -07001501static const struct attribute_group *input_dev_attr_groups[] = {
Dmitry Torokhovcb9def42007-03-07 23:20:26 -05001502 &input_dev_attr_group,
1503 &input_dev_id_attr_group,
1504 &input_dev_caps_attr_group,
1505 NULL
1506};
1507
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001508static void input_dev_release(struct device *device)
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001509{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001510 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001511
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001512 input_ff_destroy(dev);
Henrik Rydberg40d007e2010-07-15 23:10:10 -07001513 input_mt_destroy_slots(dev);
Daniel Mackd31b2862010-08-02 20:18:21 -07001514 kfree(dev->absinfo);
Henrik Rydberg4369c642012-09-15 15:23:35 +02001515 kfree(dev->vals);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001516 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001517
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001518 module_put(THIS_MODULE);
1519}
1520
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001521/*
Kay Sievers312c0042005-11-16 09:00:00 +01001522 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001523 * device bitfields.
1524 */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001525static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001526 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001527{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001528 int len;
1529
Henrik Rydbergfcd30272010-12-18 20:28:26 +01001530 if (add_uevent_var(env, "%s", name))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001531 return -ENOMEM;
1532
Kay Sievers7eff2e72007-08-14 15:15:12 +02001533 len = input_print_bitmap(&env->buf[env->buflen - 1],
1534 sizeof(env->buf) - env->buflen,
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001535 bitmap, max, false);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001536 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001537 return -ENOMEM;
1538
Kay Sievers7eff2e72007-08-14 15:15:12 +02001539 env->buflen += len;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001540 return 0;
1541}
1542
Kay Sievers7eff2e72007-08-14 15:15:12 +02001543static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001544 struct input_dev *dev)
1545{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001546 int len;
1547
1548 if (add_uevent_var(env, "MODALIAS="))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001549 return -ENOMEM;
1550
Kay Sievers7eff2e72007-08-14 15:15:12 +02001551 len = input_print_modalias(&env->buf[env->buflen - 1],
1552 sizeof(env->buf) - env->buflen,
1553 dev, 0);
1554 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001555 return -ENOMEM;
1556
Kay Sievers7eff2e72007-08-14 15:15:12 +02001557 env->buflen += len;
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001558 return 0;
1559}
1560
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001561#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1562 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001563 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001564 if (err) \
1565 return err; \
1566 } while (0)
1567
1568#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1569 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001570 int err = input_add_uevent_bm_var(env, name, bm, max); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001571 if (err) \
1572 return err; \
1573 } while (0)
1574
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001575#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1576 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001577 int err = input_add_uevent_modalias_var(env, dev); \
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001578 if (err) \
1579 return err; \
1580 } while (0)
1581
Kay Sievers7eff2e72007-08-14 15:15:12 +02001582static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001583{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001584 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001585
1586 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1587 dev->id.bustype, dev->id.vendor,
1588 dev->id.product, dev->id.version);
1589 if (dev->name)
1590 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1591 if (dev->phys)
1592 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -08001593 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001594 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1595
Henrik Rydberg85b77202010-12-18 20:51:13 +01001596 INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX);
1597
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001598 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1599 if (test_bit(EV_KEY, dev->evbit))
1600 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1601 if (test_bit(EV_REL, dev->evbit))
1602 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1603 if (test_bit(EV_ABS, dev->evbit))
1604 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1605 if (test_bit(EV_MSC, dev->evbit))
1606 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1607 if (test_bit(EV_LED, dev->evbit))
1608 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1609 if (test_bit(EV_SND, dev->evbit))
1610 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1611 if (test_bit(EV_FF, dev->evbit))
1612 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1613 if (test_bit(EV_SW, dev->evbit))
1614 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1615
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001616 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001617
1618 return 0;
1619}
1620
Dmitry Torokhov3cc96352009-11-12 23:19:05 -08001621#define INPUT_DO_TOGGLE(dev, type, bits, on) \
1622 do { \
1623 int i; \
1624 bool active; \
1625 \
1626 if (!test_bit(EV_##type, dev->evbit)) \
1627 break; \
1628 \
Anshul Garg3e2b03d2015-06-25 13:33:12 -07001629 for_each_set_bit(i, dev->bits##bit, type##_CNT) { \
Dmitry Torokhov3cc96352009-11-12 23:19:05 -08001630 active = test_bit(i, dev->bits); \
1631 if (!active && !on) \
1632 continue; \
1633 \
1634 dev->event(dev, EV_##type, i, on ? active : 0); \
1635 } \
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001636 } while (0)
1637
Dmitry Torokhovb50b5212010-11-03 11:02:31 -07001638static void input_dev_toggle(struct input_dev *dev, bool activate)
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001639{
1640 if (!dev->event)
1641 return;
1642
1643 INPUT_DO_TOGGLE(dev, LED, led, activate);
1644 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1645
1646 if (activate && test_bit(EV_REP, dev->evbit)) {
1647 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1648 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1649 }
1650}
1651
Dmitry Torokhovb50b5212010-11-03 11:02:31 -07001652/**
1653 * input_reset_device() - reset/restore the state of input device
1654 * @dev: input device whose state needs to be reset
1655 *
1656 * This function tries to reset the state of an opened input device and
1657 * bring internal state and state if the hardware in sync with each other.
1658 * We mark all keys as released, restore LED state, repeat rate, etc.
1659 */
1660void input_reset_device(struct input_dev *dev)
1661{
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001662 unsigned long flags;
1663
Dmitry Torokhovb50b5212010-11-03 11:02:31 -07001664 mutex_lock(&dev->mutex);
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001665 spin_lock_irqsave(&dev->event_lock, flags);
Dmitry Torokhovb50b5212010-11-03 11:02:31 -07001666
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001667 input_dev_toggle(dev, true);
1668 input_dev_release_keys(dev);
Dmitry Torokhovb50b5212010-11-03 11:02:31 -07001669
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001670 spin_unlock_irqrestore(&dev->event_lock, flags);
Dmitry Torokhovb50b5212010-11-03 11:02:31 -07001671 mutex_unlock(&dev->mutex);
1672}
1673EXPORT_SYMBOL(input_reset_device);
1674
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001675#ifdef CONFIG_PM_SLEEP
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001676static int input_dev_suspend(struct device *dev)
1677{
1678 struct input_dev *input_dev = to_input_dev(dev);
1679
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001680 spin_lock_irq(&input_dev->event_lock);
Dmitry Torokhovb50b5212010-11-03 11:02:31 -07001681
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001682 /*
1683 * Keys that are pressed now are unlikely to be
1684 * still pressed when we resume.
1685 */
1686 input_dev_release_keys(input_dev);
Dmitry Torokhovb50b5212010-11-03 11:02:31 -07001687
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001688 /* Turn off LEDs and sounds, if any are active. */
1689 input_dev_toggle(input_dev, false);
1690
1691 spin_unlock_irq(&input_dev->event_lock);
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001692
1693 return 0;
1694}
1695
1696static int input_dev_resume(struct device *dev)
1697{
1698 struct input_dev *input_dev = to_input_dev(dev);
1699
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001700 spin_lock_irq(&input_dev->event_lock);
1701
1702 /* Restore state of LEDs and sounds, if any were active. */
1703 input_dev_toggle(input_dev, true);
1704
1705 spin_unlock_irq(&input_dev->event_lock);
1706
1707 return 0;
1708}
1709
1710static int input_dev_freeze(struct device *dev)
1711{
1712 struct input_dev *input_dev = to_input_dev(dev);
1713
1714 spin_lock_irq(&input_dev->event_lock);
1715
1716 /*
1717 * Keys that are pressed now are unlikely to be
1718 * still pressed when we resume.
1719 */
1720 input_dev_release_keys(input_dev);
1721
1722 spin_unlock_irq(&input_dev->event_lock);
1723
1724 return 0;
1725}
1726
1727static int input_dev_poweroff(struct device *dev)
1728{
1729 struct input_dev *input_dev = to_input_dev(dev);
1730
1731 spin_lock_irq(&input_dev->event_lock);
1732
1733 /* Turn off LEDs and sounds, if any are active. */
1734 input_dev_toggle(input_dev, false);
1735
1736 spin_unlock_irq(&input_dev->event_lock);
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001737
1738 return 0;
1739}
1740
1741static const struct dev_pm_ops input_dev_pm_ops = {
1742 .suspend = input_dev_suspend,
1743 .resume = input_dev_resume,
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001744 .freeze = input_dev_freeze,
1745 .poweroff = input_dev_poweroff,
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001746 .restore = input_dev_resume,
1747};
1748#endif /* CONFIG_PM */
1749
Bhumika Goyalf7193152017-01-24 10:33:55 -08001750static const struct device_type input_dev_type = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001751 .groups = input_dev_attr_groups,
1752 .release = input_dev_release,
1753 .uevent = input_dev_uevent,
Aleksej Makarov768d9aa2013-11-23 10:20:36 -08001754#ifdef CONFIG_PM_SLEEP
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001755 .pm = &input_dev_pm_ops,
1756#endif
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001757};
1758
Al Viro2c9ede52011-07-23 20:24:48 -04001759static char *input_devnode(struct device *dev, umode_t *mode)
Kay Sieversaa5ed632009-04-30 15:23:42 +02001760{
1761 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1762}
1763
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001764struct class input_class = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001765 .name = "input",
Kay Sieverse454cea2009-09-18 23:01:12 +02001766 .devnode = input_devnode,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001767};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001768EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001769
Dmitry Torokhov14471902006-11-02 23:26:55 -05001770/**
1771 * input_allocate_device - allocate memory for new input device
1772 *
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07001773 * Returns prepared struct input_dev or %NULL.
Dmitry Torokhov14471902006-11-02 23:26:55 -05001774 *
1775 * NOTE: Use input_free_device() to free devices that have not been
1776 * registered; input_unregister_device() should be used for already
1777 * registered devices.
1778 */
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001779struct input_dev *input_allocate_device(void)
1780{
Aniroop Mathur9c7d66f2014-12-02 15:22:28 -08001781 static atomic_t input_no = ATOMIC_INIT(-1);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001782 struct input_dev *dev;
1783
Markus Elfringc3f6f862017-05-09 17:51:30 -07001784 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001785 if (dev) {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001786 dev->dev.type = &input_dev_type;
1787 dev->dev.class = &input_class;
1788 device_initialize(&dev->dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001789 mutex_init(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001790 spin_lock_init(&dev->event_lock);
Kees Cook4e974c12017-11-03 12:21:48 -07001791 timer_setup(&dev->timer, NULL, 0);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001792 INIT_LIST_HEAD(&dev->h_list);
1793 INIT_LIST_HEAD(&dev->node);
Dmitry Torokhov655816e2006-09-14 01:32:14 -04001794
Richard Leitnerbf1d50f2014-10-08 14:24:15 -07001795 dev_set_name(&dev->dev, "input%lu",
Aniroop Mathur9c7d66f2014-12-02 15:22:28 -08001796 (unsigned long)atomic_inc_return(&input_no));
David Herrmanna60a71b2013-10-06 01:15:08 -07001797
Dmitry Torokhov655816e2006-09-14 01:32:14 -04001798 __module_get(THIS_MODULE);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001799 }
1800
1801 return dev;
1802}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001803EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001804
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07001805struct input_devres {
1806 struct input_dev *input;
1807};
1808
1809static int devm_input_device_match(struct device *dev, void *res, void *data)
1810{
1811 struct input_devres *devres = res;
1812
1813 return devres->input == data;
1814}
1815
1816static void devm_input_device_release(struct device *dev, void *res)
1817{
1818 struct input_devres *devres = res;
1819 struct input_dev *input = devres->input;
1820
1821 dev_dbg(dev, "%s: dropping reference to %s\n",
1822 __func__, dev_name(&input->dev));
1823 input_put_device(input);
1824}
1825
1826/**
1827 * devm_input_allocate_device - allocate managed input device
1828 * @dev: device owning the input device being created
1829 *
1830 * Returns prepared struct input_dev or %NULL.
1831 *
1832 * Managed input devices do not need to be explicitly unregistered or
1833 * freed as it will be done automatically when owner device unbinds from
1834 * its driver (or binding fails). Once managed input device is allocated,
1835 * it is ready to be set up and registered in the same fashion as regular
1836 * input device. There are no special devm_input_device_[un]register()
Dmitry Torokhovb6662632013-01-08 09:10:31 -08001837 * variants, regular ones work with both managed and unmanaged devices,
1838 * should you need them. In most cases however, managed input device need
1839 * not be explicitly unregistered or freed.
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07001840 *
1841 * NOTE: the owner device is set up as parent of input device and users
1842 * should not override it.
1843 */
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07001844struct input_dev *devm_input_allocate_device(struct device *dev)
1845{
1846 struct input_dev *input;
1847 struct input_devres *devres;
1848
1849 devres = devres_alloc(devm_input_device_release,
Markus Elfringc3f6f862017-05-09 17:51:30 -07001850 sizeof(*devres), GFP_KERNEL);
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07001851 if (!devres)
1852 return NULL;
1853
1854 input = input_allocate_device();
1855 if (!input) {
1856 devres_free(devres);
1857 return NULL;
1858 }
1859
1860 input->dev.parent = dev;
1861 input->devres_managed = true;
1862
1863 devres->input = input;
1864 devres_add(dev, devres);
1865
1866 return input;
1867}
1868EXPORT_SYMBOL(devm_input_allocate_device);
1869
Dmitry Torokhov14471902006-11-02 23:26:55 -05001870/**
1871 * input_free_device - free memory occupied by input_dev structure
1872 * @dev: input device to free
1873 *
1874 * This function should only be used if input_register_device()
1875 * was not called yet or if it failed. Once device was registered
1876 * use input_unregister_device() and memory will be freed once last
Dmitry Torokhov80064792007-08-30 00:22:11 -04001877 * reference to the device is dropped.
Dmitry Torokhov14471902006-11-02 23:26:55 -05001878 *
1879 * Device should be allocated by input_allocate_device().
1880 *
1881 * NOTE: If there are references to the input device then memory
1882 * will not be freed until last reference is dropped.
1883 */
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001884void input_free_device(struct input_dev *dev)
1885{
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07001886 if (dev) {
1887 if (dev->devres_managed)
1888 WARN_ON(devres_destroy(dev->dev.parent,
1889 devm_input_device_release,
1890 devm_input_device_match,
1891 dev));
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001892 input_put_device(dev);
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07001893 }
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001894}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001895EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001896
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001897/**
Atif Niyaz3b51c442019-07-24 22:26:31 +03001898 * input_set_timestamp - set timestamp for input events
1899 * @dev: input device to set timestamp for
1900 * @timestamp: the time at which the event has occurred
1901 * in CLOCK_MONOTONIC
1902 *
1903 * This function is intended to provide to the input system a more
1904 * accurate time of when an event actually occurred. The driver should
1905 * call this function as soon as a timestamp is acquired ensuring
1906 * clock conversions in input_set_timestamp are done correctly.
1907 *
1908 * The system entering suspend state between timestamp acquisition and
1909 * calling input_set_timestamp can result in inaccurate conversions.
1910 */
1911void input_set_timestamp(struct input_dev *dev, ktime_t timestamp)
1912{
1913 dev->timestamp[INPUT_CLK_MONO] = timestamp;
1914 dev->timestamp[INPUT_CLK_REAL] = ktime_mono_to_real(timestamp);
1915 dev->timestamp[INPUT_CLK_BOOT] = ktime_mono_to_any(timestamp,
1916 TK_OFFS_BOOT);
1917}
1918EXPORT_SYMBOL(input_set_timestamp);
1919
1920/**
1921 * input_get_timestamp - get timestamp for input events
1922 * @dev: input device to get timestamp from
1923 *
1924 * A valid timestamp is a timestamp of non-zero value.
1925 */
1926ktime_t *input_get_timestamp(struct input_dev *dev)
1927{
1928 const ktime_t invalid_timestamp = ktime_set(0, 0);
1929
1930 if (!ktime_compare(dev->timestamp[INPUT_CLK_MONO], invalid_timestamp))
1931 input_set_timestamp(dev, ktime_get());
1932
1933 return dev->timestamp;
1934}
1935EXPORT_SYMBOL(input_get_timestamp);
1936
1937/**
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001938 * input_set_capability - mark device as capable of a certain event
1939 * @dev: device that is capable of emitting or accepting event
1940 * @type: type of the event (EV_KEY, EV_REL, etc...)
1941 * @code: event code
1942 *
1943 * In addition to setting up corresponding bit in appropriate capability
1944 * bitmap the function also adjusts dev->evbit.
1945 */
1946void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1947{
1948 switch (type) {
1949 case EV_KEY:
1950 __set_bit(code, dev->keybit);
1951 break;
1952
1953 case EV_REL:
1954 __set_bit(code, dev->relbit);
1955 break;
1956
1957 case EV_ABS:
Dmitry Torokhov28a2a2e2013-12-26 17:44:29 -08001958 input_alloc_absinfo(dev);
1959 if (!dev->absinfo)
1960 return;
1961
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001962 __set_bit(code, dev->absbit);
1963 break;
1964
1965 case EV_MSC:
1966 __set_bit(code, dev->mscbit);
1967 break;
1968
1969 case EV_SW:
1970 __set_bit(code, dev->swbit);
1971 break;
1972
1973 case EV_LED:
1974 __set_bit(code, dev->ledbit);
1975 break;
1976
1977 case EV_SND:
1978 __set_bit(code, dev->sndbit);
1979 break;
1980
1981 case EV_FF:
1982 __set_bit(code, dev->ffbit);
1983 break;
1984
Dmitry Baryshkov22d1c392007-12-14 01:21:03 -05001985 case EV_PWR:
1986 /* do nothing */
1987 break;
1988
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001989 default:
Nick Simonov67043f42018-05-15 10:33:52 -07001990 pr_err("%s: unknown type %u (code %u)\n", __func__, type, code);
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001991 dump_stack();
1992 return;
1993 }
1994
1995 __set_bit(type, dev->evbit);
1996}
1997EXPORT_SYMBOL(input_set_capability);
1998
Jeff Brown80b48952011-04-18 10:08:02 -07001999static unsigned int input_estimate_events_per_packet(struct input_dev *dev)
2000{
2001 int mt_slots;
2002 int i;
2003 unsigned int events;
2004
Henrik Rydberg8d18fba2012-09-15 15:15:58 +02002005 if (dev->mt) {
2006 mt_slots = dev->mt->num_slots;
Jeff Brown80b48952011-04-18 10:08:02 -07002007 } else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) {
2008 mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum -
2009 dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1,
Hans Petter Selasky8c127f02011-05-25 09:24:32 -07002010 mt_slots = clamp(mt_slots, 2, 32);
Jeff Brown80b48952011-04-18 10:08:02 -07002011 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
2012 mt_slots = 2;
2013 } else {
2014 mt_slots = 0;
2015 }
2016
2017 events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */
2018
Anshul Garg3e2b03d2015-06-25 13:33:12 -07002019 if (test_bit(EV_ABS, dev->evbit))
2020 for_each_set_bit(i, dev->absbit, ABS_CNT)
2021 events += input_is_mt_axis(i) ? mt_slots : 1;
Jeff Brown80b48952011-04-18 10:08:02 -07002022
Anshul Garg3e2b03d2015-06-25 13:33:12 -07002023 if (test_bit(EV_REL, dev->evbit))
2024 events += bitmap_weight(dev->relbit, REL_CNT);
Jeff Brown80b48952011-04-18 10:08:02 -07002025
Henrik Rydberg7c75bf92012-09-01 16:15:43 +02002026 /* Make room for KEY and MSC events */
2027 events += 7;
2028
Jeff Brown80b48952011-04-18 10:08:02 -07002029 return events;
2030}
2031
Dmitry Torokhov92a3a582010-01-05 17:56:01 -08002032#define INPUT_CLEANSE_BITMASK(dev, type, bits) \
2033 do { \
2034 if (!test_bit(EV_##type, dev->evbit)) \
2035 memset(dev->bits##bit, 0, \
2036 sizeof(dev->bits##bit)); \
2037 } while (0)
2038
2039static void input_cleanse_bitmasks(struct input_dev *dev)
2040{
2041 INPUT_CLEANSE_BITMASK(dev, KEY, key);
2042 INPUT_CLEANSE_BITMASK(dev, REL, rel);
2043 INPUT_CLEANSE_BITMASK(dev, ABS, abs);
2044 INPUT_CLEANSE_BITMASK(dev, MSC, msc);
2045 INPUT_CLEANSE_BITMASK(dev, LED, led);
2046 INPUT_CLEANSE_BITMASK(dev, SND, snd);
2047 INPUT_CLEANSE_BITMASK(dev, FF, ff);
2048 INPUT_CLEANSE_BITMASK(dev, SW, sw);
2049}
2050
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002051static void __input_unregister_device(struct input_dev *dev)
2052{
2053 struct input_handle *handle, *next;
2054
2055 input_disconnect_device(dev);
2056
2057 mutex_lock(&input_mutex);
2058
2059 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
2060 handle->handler->disconnect(handle);
2061 WARN_ON(!list_empty(&dev->h_list));
2062
2063 del_timer_sync(&dev->timer);
2064 list_del_init(&dev->node);
2065
2066 input_wakeup_procfs_readers();
2067
2068 mutex_unlock(&input_mutex);
2069
2070 device_del(&dev->dev);
2071}
2072
2073static void devm_input_device_unregister(struct device *dev, void *res)
2074{
2075 struct input_devres *devres = res;
2076 struct input_dev *input = devres->input;
2077
2078 dev_dbg(dev, "%s: unregistering device %s\n",
2079 __func__, dev_name(&input->dev));
2080 __input_unregister_device(input);
2081}
2082
Dmitry Torokhov80064792007-08-30 00:22:11 -04002083/**
Petri Gynther027c71b2015-10-13 23:13:55 -07002084 * input_enable_softrepeat - enable software autorepeat
2085 * @dev: input device
2086 * @delay: repeat delay
2087 * @period: repeat period
2088 *
2089 * Enable software autorepeat on the input device.
2090 */
2091void input_enable_softrepeat(struct input_dev *dev, int delay, int period)
2092{
Kees Cook841b86f2017-10-23 09:40:42 +02002093 dev->timer.function = input_repeat_key;
Petri Gynther027c71b2015-10-13 23:13:55 -07002094 dev->rep[REP_DELAY] = delay;
2095 dev->rep[REP_PERIOD] = period;
2096}
2097EXPORT_SYMBOL(input_enable_softrepeat);
2098
2099/**
Dmitry Torokhov80064792007-08-30 00:22:11 -04002100 * input_register_device - register device with input core
2101 * @dev: device to be registered
2102 *
2103 * This function registers device with input core. The device must be
2104 * allocated with input_allocate_device() and all it's capabilities
2105 * set up before registering.
2106 * If function fails the device must be freed with input_free_device().
2107 * Once device has been successfully registered it can be unregistered
2108 * with input_unregister_device(); input_free_device() should not be
2109 * called in this case.
Dmitry Torokhovb6662632013-01-08 09:10:31 -08002110 *
2111 * Note that this function is also used to register managed input devices
2112 * (ones allocated with devm_input_allocate_device()). Such managed input
2113 * devices need not be explicitly unregistered or freed, their tear down
2114 * is controlled by the devres infrastructure. It is also worth noting
2115 * that tear down of managed input devices is internally a 2-step process:
2116 * registered managed input device is first unregistered, but stays in
2117 * memory and can still handle input_event() calls (although events will
2118 * not be delivered anywhere). The freeing of managed input device will
2119 * happen later, when devres stack is unwound to the point where device
2120 * allocation was made.
Dmitry Torokhov80064792007-08-30 00:22:11 -04002121 */
Dmitry Torokhov5f945482005-11-02 22:51:46 -05002122int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002123{
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002124 struct input_devres *devres = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002125 struct input_handler *handler;
Henrik Rydberg7c75bf92012-09-01 16:15:43 +02002126 unsigned int packet_size;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05002127 const char *path;
2128 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002129
Dmitry Torokhov6ecfe512017-01-31 15:09:08 -08002130 if (test_bit(EV_ABS, dev->evbit) && !dev->absinfo) {
2131 dev_err(&dev->dev,
2132 "Absolute device without dev->absinfo, refusing to register\n");
2133 return -EINVAL;
2134 }
2135
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002136 if (dev->devres_managed) {
2137 devres = devres_alloc(devm_input_device_unregister,
Markus Elfringc3f6f862017-05-09 17:51:30 -07002138 sizeof(*devres), GFP_KERNEL);
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002139 if (!devres)
2140 return -ENOMEM;
2141
2142 devres->input = dev;
2143 }
2144
Dmitry Torokhov4f93df42010-01-05 17:56:00 -08002145 /* Every input device generates EV_SYN/SYN_REPORT events. */
Dmitry Torokhov80064792007-08-30 00:22:11 -04002146 __set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002147
Dmitry Torokhov4f93df42010-01-05 17:56:00 -08002148 /* KEY_RESERVED is not supposed to be transmitted to userspace. */
2149 __clear_bit(KEY_RESERVED, dev->keybit);
2150
Dmitry Torokhov92a3a582010-01-05 17:56:01 -08002151 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
2152 input_cleanse_bitmasks(dev);
2153
Henrik Rydberg7c75bf92012-09-01 16:15:43 +02002154 packet_size = input_estimate_events_per_packet(dev);
2155 if (dev->hint_events_per_packet < packet_size)
2156 dev->hint_events_per_packet = packet_size;
Jeff Brown80b48952011-04-18 10:08:02 -07002157
Kang Hu95079b82013-10-31 00:47:53 -07002158 dev->max_vals = dev->hint_events_per_packet + 2;
Henrik Rydberg4369c642012-09-15 15:23:35 +02002159 dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002160 if (!dev->vals) {
2161 error = -ENOMEM;
2162 goto err_devres_free;
2163 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002164
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002165 /*
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002166 * If delay and period are pre-set by the driver, then autorepeating
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002167 * is handled by the driver itself and we don't do it in input.c.
Dmitry Torokhov5f945482005-11-02 22:51:46 -05002168 */
Petri Gynther027c71b2015-10-13 23:13:55 -07002169 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD])
2170 input_enable_softrepeat(dev, 250, 33);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002171
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002172 if (!dev->getkeycode)
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002173 dev->getkeycode = input_default_getkeycode;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002174
Dmitry Torokhov80064792007-08-30 00:22:11 -04002175 if (!dev->setkeycode)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002176 dev->setkeycode = input_default_setkeycode;
2177
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002178 error = device_add(&dev->dev);
2179 if (error)
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002180 goto err_free_vals;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002181
2182 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
2183 pr_info("%s as %s\n",
2184 dev->name ? dev->name : "Unspecified device",
2185 path ? path : "N/A");
2186 kfree(path);
2187
2188 error = mutex_lock_interruptible(&input_mutex);
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002189 if (error)
2190 goto err_device_del;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002191
2192 list_add_tail(&dev->node, &input_dev_list);
2193
2194 list_for_each_entry(handler, &input_handler_list, node)
2195 input_attach_handler(dev, handler);
2196
2197 input_wakeup_procfs_readers();
2198
2199 mutex_unlock(&input_mutex);
2200
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002201 if (dev->devres_managed) {
2202 dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
2203 __func__, dev_name(&dev->dev));
2204 devres_add(dev->dev.parent, devres);
2205 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002206 return 0;
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002207
2208err_device_del:
2209 device_del(&dev->dev);
2210err_free_vals:
2211 kfree(dev->vals);
2212 dev->vals = NULL;
2213err_devres_free:
2214 devres_free(devres);
2215 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002216}
2217EXPORT_SYMBOL(input_register_device);
2218
2219/**
2220 * input_unregister_device - unregister previously registered device
2221 * @dev: device to be unregistered
2222 *
2223 * This function unregisters an input device. Once device is unregistered
2224 * the caller should not try to access it as it may get freed at any moment.
2225 */
2226void input_unregister_device(struct input_dev *dev)
2227{
Dmitry Torokhov2be975c2012-11-03 12:16:12 -07002228 if (dev->devres_managed) {
2229 WARN_ON(devres_destroy(dev->dev.parent,
2230 devm_input_device_unregister,
2231 devm_input_device_match,
2232 dev));
2233 __input_unregister_device(dev);
2234 /*
2235 * We do not do input_put_device() here because it will be done
2236 * when 2nd devres fires up.
2237 */
2238 } else {
2239 __input_unregister_device(dev);
2240 input_put_device(dev);
2241 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002242}
2243EXPORT_SYMBOL(input_unregister_device);
2244
Dmitry Torokhov80064792007-08-30 00:22:11 -04002245/**
2246 * input_register_handler - register a new input handler
2247 * @handler: handler to be registered
2248 *
2249 * This function registers a new input handler (interface) for input
2250 * devices in the system and attaches it to all input devices that
2251 * are compatible with the handler.
2252 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002253int input_register_handler(struct input_handler *handler)
2254{
2255 struct input_dev *dev;
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002256 int error;
Dmitry Torokhov80064792007-08-30 00:22:11 -04002257
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002258 error = mutex_lock_interruptible(&input_mutex);
2259 if (error)
2260 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002261
2262 INIT_LIST_HEAD(&handler->h_list);
2263
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002264 list_add_tail(&handler->node, &input_handler_list);
2265
2266 list_for_each_entry(dev, &input_dev_list, node)
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002267 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002268
2269 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04002270
Dmitry Torokhov80064792007-08-30 00:22:11 -04002271 mutex_unlock(&input_mutex);
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002272 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002273}
2274EXPORT_SYMBOL(input_register_handler);
2275
Dmitry Torokhov80064792007-08-30 00:22:11 -04002276/**
2277 * input_unregister_handler - unregisters an input handler
2278 * @handler: handler to be unregistered
2279 *
2280 * This function disconnects a handler from its input devices and
2281 * removes it from lists of known handlers.
2282 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002283void input_unregister_handler(struct input_handler *handler)
2284{
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002285 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002286
Dmitry Torokhov80064792007-08-30 00:22:11 -04002287 mutex_lock(&input_mutex);
2288
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002289 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002290 handler->disconnect(handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002291 WARN_ON(!list_empty(&handler->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002292
2293 list_del_init(&handler->node);
2294
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002295 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04002296
2297 mutex_unlock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002298}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04002299EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002300
Dmitry Torokhov80064792007-08-30 00:22:11 -04002301/**
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08002302 * input_handler_for_each_handle - handle iterator
2303 * @handler: input handler to iterate
2304 * @data: data for the callback
2305 * @fn: function to be called for each handle
2306 *
2307 * Iterate over @bus's list of devices, and call @fn for each, passing
2308 * it @data and stop when @fn returns a non-zero value. The function is
Shailendra Vermaec8beff2015-05-18 10:44:33 -07002309 * using RCU to traverse the list and therefore may be using in atomic
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08002310 * contexts. The @fn callback is invoked from RCU critical section and
2311 * thus must not sleep.
2312 */
2313int input_handler_for_each_handle(struct input_handler *handler, void *data,
2314 int (*fn)(struct input_handle *, void *))
2315{
2316 struct input_handle *handle;
2317 int retval = 0;
2318
2319 rcu_read_lock();
2320
2321 list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
2322 retval = fn(handle, data);
2323 if (retval)
2324 break;
2325 }
2326
2327 rcu_read_unlock();
2328
2329 return retval;
2330}
2331EXPORT_SYMBOL(input_handler_for_each_handle);
2332
2333/**
Dmitry Torokhov80064792007-08-30 00:22:11 -04002334 * input_register_handle - register a new input handle
2335 * @handle: handle to register
2336 *
2337 * This function puts a new input handle onto device's
2338 * and handler's lists so that events can flow through
2339 * it once it is opened using input_open_device().
2340 *
2341 * This function is supposed to be called from handler's
2342 * connect() method.
2343 */
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002344int input_register_handle(struct input_handle *handle)
2345{
2346 struct input_handler *handler = handle->handler;
Dmitry Torokhov80064792007-08-30 00:22:11 -04002347 struct input_dev *dev = handle->dev;
2348 int error;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002349
Dmitry Torokhov80064792007-08-30 00:22:11 -04002350 /*
2351 * We take dev->mutex here to prevent race with
2352 * input_release_device().
2353 */
2354 error = mutex_lock_interruptible(&dev->mutex);
2355 if (error)
2356 return error;
Dmitry Torokhovef7995f2010-01-29 23:59:12 -08002357
2358 /*
2359 * Filters go to the head of the list, normal handlers
2360 * to the tail.
2361 */
2362 if (handler->filter)
2363 list_add_rcu(&handle->d_node, &dev->h_list);
2364 else
2365 list_add_tail_rcu(&handle->d_node, &dev->h_list);
2366
Dmitry Torokhov80064792007-08-30 00:22:11 -04002367 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04002368
2369 /*
2370 * Since we are supposed to be called from ->connect()
2371 * which is mutually exclusive with ->disconnect()
2372 * we can't be racing with input_unregister_handle()
2373 * and so separate lock is not needed here.
2374 */
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08002375 list_add_tail_rcu(&handle->h_node, &handler->h_list);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002376
2377 if (handler->start)
2378 handler->start(handle);
2379
2380 return 0;
2381}
2382EXPORT_SYMBOL(input_register_handle);
2383
Dmitry Torokhov80064792007-08-30 00:22:11 -04002384/**
2385 * input_unregister_handle - unregister an input handle
2386 * @handle: handle to unregister
2387 *
2388 * This function removes input handle from device's
2389 * and handler's lists.
2390 *
2391 * This function is supposed to be called from handler's
2392 * disconnect() method.
2393 */
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002394void input_unregister_handle(struct input_handle *handle)
2395{
Dmitry Torokhov80064792007-08-30 00:22:11 -04002396 struct input_dev *dev = handle->dev;
2397
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08002398 list_del_rcu(&handle->h_node);
Dmitry Torokhov80064792007-08-30 00:22:11 -04002399
2400 /*
2401 * Take dev->mutex to prevent race with input_release_device().
2402 */
2403 mutex_lock(&dev->mutex);
2404 list_del_rcu(&handle->d_node);
2405 mutex_unlock(&dev->mutex);
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08002406
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -04002407 synchronize_rcu();
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04002408}
2409EXPORT_SYMBOL(input_unregister_handle);
2410
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002411/**
2412 * input_get_new_minor - allocates a new input minor number
2413 * @legacy_base: beginning or the legacy range to be searched
2414 * @legacy_num: size of legacy range
2415 * @allow_dynamic: whether we can also take ID from the dynamic range
2416 *
2417 * This function allocates a new device minor for from input major namespace.
2418 * Caller can request legacy minor by specifying @legacy_base and @legacy_num
2419 * parameters and whether ID can be allocated from dynamic range if there are
2420 * no free IDs in legacy range.
2421 */
2422int input_get_new_minor(int legacy_base, unsigned int legacy_num,
2423 bool allow_dynamic)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002424{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002425 /*
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002426 * This function should be called from input handler's ->connect()
2427 * methods, which are serialized with input_mutex, so no additional
2428 * locking is needed here.
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002429 */
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002430 if (legacy_base >= 0) {
2431 int minor = ida_simple_get(&input_ida,
2432 legacy_base,
2433 legacy_base + legacy_num,
2434 GFP_KERNEL);
2435 if (minor >= 0 || !allow_dynamic)
2436 return minor;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002437 }
Arnd Bergmann2f2177c2010-03-09 20:38:48 -08002438
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002439 return ida_simple_get(&input_ida,
2440 INPUT_FIRST_DYNAMIC_DEV, INPUT_MAX_CHAR_DEVICES,
2441 GFP_KERNEL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002442}
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002443EXPORT_SYMBOL(input_get_new_minor);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002444
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002445/**
2446 * input_free_minor - release previously allocated minor
2447 * @minor: minor to be released
2448 *
2449 * This function releases previously allocated input minor so that it can be
2450 * reused later.
2451 */
2452void input_free_minor(unsigned int minor)
2453{
2454 ida_simple_remove(&input_ida, minor);
2455}
2456EXPORT_SYMBOL(input_free_minor);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458static int __init input_init(void)
2459{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002460 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07002462 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05002463 if (err) {
Joe Perchesda0c4902010-11-29 23:33:07 -08002464 pr_err("unable to register input_dev class\n");
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05002465 return err;
2466 }
2467
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002468 err = input_proc_init();
2469 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07002470 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002471
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002472 err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),
2473 INPUT_MAX_CHAR_DEVICES, "input");
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002474 if (err) {
Joe Perchesda0c4902010-11-29 23:33:07 -08002475 pr_err("unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07002476 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002478
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002479 return 0;
2480
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07002481 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07002482 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
2486static void __exit input_exit(void)
2487{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05002488 input_proc_exit();
Dmitry Torokhov7f8d4ca2012-10-08 09:07:24 -07002489 unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0),
2490 INPUT_MAX_CHAR_DEVICES);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07002491 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
2494subsys_initcall(input_init);
2495module_exit(input_exit);