blob: be18fa99fa2437ea6af582e2d21e1d686b7b16b9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The input core
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 */
12
13#include <linux/init.h>
Dmitry Torokhovffd0db92009-09-16 01:06:43 -070014#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/input.h>
16#include <linux/module.h>
17#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>
Jonathan Corbet2edbf852008-05-15 10:37:16 -060026#include <linux/smp_lock.h>
Dmitry Torokhov15e184a2010-01-11 00:05:43 -080027#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
30MODULE_DESCRIPTION("Input core");
31MODULE_LICENSE("GPL");
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define INPUT_DEVICES 256
34
Henrik Rydberg61994a62009-04-28 07:45:31 -070035/*
36 * EV_ABS events which should not be cached are listed here.
37 */
38static unsigned int input_abs_bypass_init_data[] __initdata = {
Henrik Rydberg5e5ee682009-04-28 07:47:33 -070039 ABS_MT_TOUCH_MAJOR,
40 ABS_MT_TOUCH_MINOR,
41 ABS_MT_WIDTH_MAJOR,
42 ABS_MT_WIDTH_MINOR,
43 ABS_MT_ORIENTATION,
44 ABS_MT_POSITION_X,
45 ABS_MT_POSITION_Y,
46 ABS_MT_TOOL_TYPE,
47 ABS_MT_BLOB_ID,
Henrik Rydbergdf391e02009-05-23 09:51:20 -070048 ABS_MT_TRACKING_ID,
Henrik Rydbergcb6ecf62010-01-28 22:28:27 -080049 ABS_MT_PRESSURE,
Henrik Rydberg61994a62009-04-28 07:45:31 -070050 0
51};
52static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static LIST_HEAD(input_dev_list);
55static LIST_HEAD(input_handler_list);
56
Dmitry Torokhov80064792007-08-30 00:22:11 -040057/*
58 * input_mutex protects access to both input_dev_list and input_handler_list.
59 * This also causes input_[un]register_device and input_[un]register_handler
60 * be mutually exclusive which simplifies locking in drivers implementing
61 * input handlers.
62 */
63static DEFINE_MUTEX(input_mutex);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static struct input_handler *input_table[8];
66
Dmitry Torokhov80064792007-08-30 00:22:11 -040067static inline int is_event_supported(unsigned int code,
68 unsigned long *bm, unsigned int max)
69{
70 return code <= max && test_bit(code, bm);
71}
72
73static int input_defuzz_abs_event(int value, int old_val, int fuzz)
74{
75 if (fuzz) {
76 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
77 return old_val;
78
79 if (value > old_val - fuzz && value < old_val + fuzz)
80 return (old_val * 3 + value) / 4;
81
82 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
83 return (old_val + value) / 2;
84 }
85
86 return value;
87}
88
89/*
Dmitry Torokhovef7995f2010-01-29 23:59:12 -080090 * Pass event first through all filters and then, if event has not been
91 * filtered out, through all open handles. This function is called with
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040092 * dev->event_lock held and interrupts disabled.
Dmitry Torokhov80064792007-08-30 00:22:11 -040093 */
94static void input_pass_event(struct input_dev *dev,
95 unsigned int type, unsigned int code, int value)
96{
Dmitry Torokhovef7995f2010-01-29 23:59:12 -080097 struct input_handler *handler;
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040098 struct input_handle *handle;
Dmitry Torokhov80064792007-08-30 00:22:11 -040099
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400100 rcu_read_lock();
101
102 handle = rcu_dereference(dev->grab);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400103 if (handle)
104 handle->handler->event(handle, type, code, value);
Dmitry Torokhovef7995f2010-01-29 23:59:12 -0800105 else {
106 bool filtered = false;
107
108 list_for_each_entry_rcu(handle, &dev->h_list, d_node) {
109 if (!handle->open)
110 continue;
111
112 handler = handle->handler;
113 if (!handler->filter) {
114 if (filtered)
115 break;
116
117 handler->event(handle, type, code, value);
118
119 } else if (handler->filter(handle, type, code, value))
120 filtered = true;
121 }
122 }
123
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400124 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400125}
126
127/*
128 * Generate software autorepeat event. Note that we take
129 * dev->event_lock here to avoid racing with input_event
130 * which may cause keys get "stuck".
131 */
132static void input_repeat_key(unsigned long data)
133{
134 struct input_dev *dev = (void *) data;
135 unsigned long flags;
136
137 spin_lock_irqsave(&dev->event_lock, flags);
138
139 if (test_bit(dev->repeat_key, dev->key) &&
140 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
141
142 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
143
144 if (dev->sync) {
145 /*
146 * Only send SYN_REPORT if we are not in a middle
147 * of driver parsing a new hardware packet.
148 * Otherwise assume that the driver will send
149 * SYN_REPORT once it's done.
150 */
151 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
152 }
153
154 if (dev->rep[REP_PERIOD])
155 mod_timer(&dev->timer, jiffies +
156 msecs_to_jiffies(dev->rep[REP_PERIOD]));
157 }
158
159 spin_unlock_irqrestore(&dev->event_lock, flags);
160}
161
162static void input_start_autorepeat(struct input_dev *dev, int code)
163{
164 if (test_bit(EV_REP, dev->evbit) &&
165 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
166 dev->timer.data) {
167 dev->repeat_key = code;
168 mod_timer(&dev->timer,
169 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
170 }
171}
172
Johannes Berge7b5c1e2009-01-29 23:17:52 -0800173static void input_stop_autorepeat(struct input_dev *dev)
174{
175 del_timer(&dev->timer);
176}
177
Dmitry Torokhov80064792007-08-30 00:22:11 -0400178#define INPUT_IGNORE_EVENT 0
179#define INPUT_PASS_TO_HANDLERS 1
180#define INPUT_PASS_TO_DEVICE 2
181#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
182
183static void input_handle_event(struct input_dev *dev,
184 unsigned int type, unsigned int code, int value)
185{
186 int disposition = INPUT_IGNORE_EVENT;
187
188 switch (type) {
189
190 case EV_SYN:
191 switch (code) {
192 case SYN_CONFIG:
193 disposition = INPUT_PASS_TO_ALL;
194 break;
195
196 case SYN_REPORT:
197 if (!dev->sync) {
198 dev->sync = 1;
199 disposition = INPUT_PASS_TO_HANDLERS;
200 }
201 break;
Henrik Rydberg5e5ee682009-04-28 07:47:33 -0700202 case SYN_MT_REPORT:
203 dev->sync = 0;
204 disposition = INPUT_PASS_TO_HANDLERS;
205 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400206 }
207 break;
208
209 case EV_KEY:
210 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
211 !!test_bit(code, dev->key) != value) {
212
213 if (value != 2) {
214 __change_bit(code, dev->key);
215 if (value)
216 input_start_autorepeat(dev, code);
Johannes Berge7b5c1e2009-01-29 23:17:52 -0800217 else
218 input_stop_autorepeat(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400219 }
220
221 disposition = INPUT_PASS_TO_HANDLERS;
222 }
223 break;
224
225 case EV_SW:
226 if (is_event_supported(code, dev->swbit, SW_MAX) &&
227 !!test_bit(code, dev->sw) != value) {
228
229 __change_bit(code, dev->sw);
230 disposition = INPUT_PASS_TO_HANDLERS;
231 }
232 break;
233
234 case EV_ABS:
235 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
236
Henrik Rydberg61994a62009-04-28 07:45:31 -0700237 if (test_bit(code, input_abs_bypass)) {
238 disposition = INPUT_PASS_TO_HANDLERS;
239 break;
240 }
241
Dmitry Torokhov80064792007-08-30 00:22:11 -0400242 value = input_defuzz_abs_event(value,
243 dev->abs[code], dev->absfuzz[code]);
244
245 if (dev->abs[code] != value) {
246 dev->abs[code] = value;
247 disposition = INPUT_PASS_TO_HANDLERS;
248 }
249 }
250 break;
251
252 case EV_REL:
253 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
254 disposition = INPUT_PASS_TO_HANDLERS;
255
256 break;
257
258 case EV_MSC:
259 if (is_event_supported(code, dev->mscbit, MSC_MAX))
260 disposition = INPUT_PASS_TO_ALL;
261
262 break;
263
264 case EV_LED:
265 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
266 !!test_bit(code, dev->led) != value) {
267
268 __change_bit(code, dev->led);
269 disposition = INPUT_PASS_TO_ALL;
270 }
271 break;
272
273 case EV_SND:
274 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
275
276 if (!!test_bit(code, dev->snd) != !!value)
277 __change_bit(code, dev->snd);
278 disposition = INPUT_PASS_TO_ALL;
279 }
280 break;
281
282 case EV_REP:
283 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
284 dev->rep[code] = value;
285 disposition = INPUT_PASS_TO_ALL;
286 }
287 break;
288
289 case EV_FF:
290 if (value >= 0)
291 disposition = INPUT_PASS_TO_ALL;
292 break;
Richard Purdieed2fa4d2008-01-03 10:46:21 -0500293
294 case EV_PWR:
295 disposition = INPUT_PASS_TO_ALL;
296 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400297 }
298
Dmitry Torokhovc9812282008-06-02 01:02:40 -0400299 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400300 dev->sync = 0;
301
302 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
303 dev->event(dev, type, code, value);
304
305 if (disposition & INPUT_PASS_TO_HANDLERS)
306 input_pass_event(dev, type, code, value);
307}
308
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400309/**
310 * input_event() - report new input event
Dmitry Torokhov14471902006-11-02 23:26:55 -0500311 * @dev: device that generated the event
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400312 * @type: type of the event
313 * @code: event code
314 * @value: value of the event
315 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400316 * This function should be used by drivers implementing various input
Dmitry Torokhovdf2d4632009-12-11 21:57:31 -0800317 * devices to report input events. See also input_inject_event().
318 *
319 * NOTE: input_event() may be safely used right after input device was
320 * allocated with input_allocate_device(), even before it is registered
321 * with input_register_device(), but the event will not reach any of the
322 * input handlers. Such early invocation of input_event() may be used
323 * to 'seed' initial state of a switch or initial position of absolute
324 * axis, etc.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400325 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400326void input_event(struct input_dev *dev,
327 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400329 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dmitry Torokhov80064792007-08-30 00:22:11 -0400331 if (is_event_supported(type, dev->evbit, EV_MAX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Dmitry Torokhov80064792007-08-30 00:22:11 -0400333 spin_lock_irqsave(&dev->event_lock, flags);
334 add_input_randomness(type, code, value);
335 input_handle_event(dev, type, code, value);
336 spin_unlock_irqrestore(&dev->event_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400339EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400341/**
342 * input_inject_event() - send input event from input handler
343 * @handle: input handle to send event through
344 * @type: type of the event
345 * @code: event code
346 * @value: value of the event
347 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400348 * Similar to input_event() but will ignore event if device is
349 * "grabbed" and handle injecting event is not the one that owns
350 * the device.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400351 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400352void input_inject_event(struct input_handle *handle,
353 unsigned int type, unsigned int code, int value)
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400354{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400355 struct input_dev *dev = handle->dev;
356 struct input_handle *grab;
357 unsigned long flags;
358
359 if (is_event_supported(type, dev->evbit, EV_MAX)) {
360 spin_lock_irqsave(&dev->event_lock, flags);
361
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400362 rcu_read_lock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400363 grab = rcu_dereference(dev->grab);
364 if (!grab || grab == handle)
365 input_handle_event(dev, type, code, value);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400366 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400367
368 spin_unlock_irqrestore(&dev->event_lock, flags);
369 }
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400370}
371EXPORT_SYMBOL(input_inject_event);
372
Dmitry Torokhov80064792007-08-30 00:22:11 -0400373/**
374 * input_grab_device - grabs device for exclusive use
375 * @handle: input handle that wants to own the device
376 *
377 * When a device is grabbed by an input handle all events generated by
378 * the device are delivered only to this handle. Also events injected
379 * by other input handles are ignored while device is grabbed.
380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381int input_grab_device(struct input_handle *handle)
382{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400383 struct input_dev *dev = handle->dev;
384 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dmitry Torokhov80064792007-08-30 00:22:11 -0400386 retval = mutex_lock_interruptible(&dev->mutex);
387 if (retval)
388 return retval;
389
390 if (dev->grab) {
391 retval = -EBUSY;
392 goto out;
393 }
394
395 rcu_assign_pointer(dev->grab, handle);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400396 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400397
398 out:
399 mutex_unlock(&dev->mutex);
400 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400402EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Dmitry Torokhov80064792007-08-30 00:22:11 -0400404static void __input_release_device(struct input_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400406 struct input_dev *dev = handle->dev;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400407
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400408 if (dev->grab == handle) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400409 rcu_assign_pointer(dev->grab, NULL);
410 /* Make sure input_pass_event() notices that grab is gone */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400411 synchronize_rcu();
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400412
413 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400414 if (handle->open && handle->handler->start)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400415 handle->handler->start(handle);
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Dmitry Torokhov80064792007-08-30 00:22:11 -0400418
419/**
420 * input_release_device - release previously grabbed device
421 * @handle: input handle that owns the device
422 *
423 * Releases previously grabbed device so that other input handles can
424 * start receiving input events. Upon release all handlers attached
425 * to the device have their start() method called so they have a change
426 * to synchronize device state with the rest of the system.
427 */
428void input_release_device(struct input_handle *handle)
429{
430 struct input_dev *dev = handle->dev;
431
432 mutex_lock(&dev->mutex);
433 __input_release_device(handle);
434 mutex_unlock(&dev->mutex);
435}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400436EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dmitry Torokhov80064792007-08-30 00:22:11 -0400438/**
439 * input_open_device - open input device
440 * @handle: handle through which device is being accessed
441 *
442 * This function should be called by input handlers when they
443 * want to start receive events from given input device.
444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445int input_open_device(struct input_handle *handle)
446{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500447 struct input_dev *dev = handle->dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400448 int retval;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500449
Dmitry Torokhov80064792007-08-30 00:22:11 -0400450 retval = mutex_lock_interruptible(&dev->mutex);
451 if (retval)
452 return retval;
453
454 if (dev->going_away) {
455 retval = -ENODEV;
456 goto out;
457 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500460
461 if (!dev->users++ && dev->open)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400462 retval = dev->open(dev);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500463
Dmitry Torokhov80064792007-08-30 00:22:11 -0400464 if (retval) {
465 dev->users--;
466 if (!--handle->open) {
467 /*
468 * Make sure we are not delivering any more events
469 * through this handle
470 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400471 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400472 }
473 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500474
Dmitry Torokhov80064792007-08-30 00:22:11 -0400475 out:
Jes Sorensene676c232006-02-19 00:21:46 -0500476 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400477 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400479EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Dmitry Torokhov80064792007-08-30 00:22:11 -0400481int input_flush_device(struct input_handle *handle, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400483 struct input_dev *dev = handle->dev;
484 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Dmitry Torokhov80064792007-08-30 00:22:11 -0400486 retval = mutex_lock_interruptible(&dev->mutex);
487 if (retval)
488 return retval;
489
490 if (dev->flush)
491 retval = dev->flush(dev, file);
492
493 mutex_unlock(&dev->mutex);
494 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400496EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Dmitry Torokhov80064792007-08-30 00:22:11 -0400498/**
499 * input_close_device - close input device
500 * @handle: handle through which device is being accessed
501 *
502 * This function should be called by input handlers when they
503 * want to stop receive events from given input device.
504 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505void input_close_device(struct input_handle *handle)
506{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500507 struct input_dev *dev = handle->dev;
508
Jes Sorensene676c232006-02-19 00:21:46 -0500509 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500510
Dmitry Torokhov80064792007-08-30 00:22:11 -0400511 __input_release_device(handle);
512
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500513 if (!--dev->users && dev->close)
514 dev->close(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400515
516 if (!--handle->open) {
517 /*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400518 * synchronize_rcu() makes sure that input_pass_event()
Dmitry Torokhov80064792007-08-30 00:22:11 -0400519 * completed and that no more input events are delivered
520 * through this handle
521 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400522 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400523 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500524
Jes Sorensene676c232006-02-19 00:21:46 -0500525 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400527EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Dmitry Torokhov80064792007-08-30 00:22:11 -0400529/*
530 * Prepare device for unregistering
531 */
532static void input_disconnect_device(struct input_dev *dev)
533{
534 struct input_handle *handle;
535 int code;
536
537 /*
538 * Mark device as going away. Note that we take dev->mutex here
539 * not to protect access to dev->going_away but rather to ensure
540 * that there are no threads in the middle of input_open_device()
541 */
542 mutex_lock(&dev->mutex);
Dmitry Torokhovffd0db92009-09-16 01:06:43 -0700543 dev->going_away = true;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400544 mutex_unlock(&dev->mutex);
545
546 spin_lock_irq(&dev->event_lock);
547
548 /*
549 * Simulate keyup events for all pressed keys so that handlers
550 * are not left with "stuck" keys. The driver may continue
551 * generate events even after we done here but they will not
552 * reach any handlers.
553 */
554 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
555 for (code = 0; code <= KEY_MAX; code++) {
556 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400557 __test_and_clear_bit(code, dev->key)) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400558 input_pass_event(dev, EV_KEY, code, 0);
559 }
560 }
561 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
562 }
563
564 list_for_each_entry(handle, &dev->h_list, d_node)
565 handle->open = 0;
566
567 spin_unlock_irq(&dev->event_lock);
568}
569
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400570static int input_fetch_keycode(struct input_dev *dev, int scancode)
571{
572 switch (dev->keycodesize) {
573 case 1:
574 return ((u8 *)dev->keycode)[scancode];
575
576 case 2:
577 return ((u16 *)dev->keycode)[scancode];
578
579 default:
580 return ((u32 *)dev->keycode)[scancode];
581 }
582}
583
584static int input_default_getkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800585 unsigned int scancode,
586 unsigned int *keycode)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400587{
588 if (!dev->keycodesize)
589 return -EINVAL;
590
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400591 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400592 return -EINVAL;
593
594 *keycode = input_fetch_keycode(dev, scancode);
595
596 return 0;
597}
598
599static int input_default_setkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800600 unsigned int scancode,
601 unsigned int keycode)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400602{
603 int old_keycode;
604 int i;
605
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400606 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400607 return -EINVAL;
608
609 if (!dev->keycodesize)
610 return -EINVAL;
611
612 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
613 return -EINVAL;
614
615 switch (dev->keycodesize) {
616 case 1: {
617 u8 *k = (u8 *)dev->keycode;
618 old_keycode = k[scancode];
619 k[scancode] = keycode;
620 break;
621 }
622 case 2: {
623 u16 *k = (u16 *)dev->keycode;
624 old_keycode = k[scancode];
625 k[scancode] = keycode;
626 break;
627 }
628 default: {
629 u32 *k = (u32 *)dev->keycode;
630 old_keycode = k[scancode];
631 k[scancode] = keycode;
632 break;
633 }
634 }
635
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800636 __clear_bit(old_keycode, dev->keybit);
637 __set_bit(keycode, dev->keybit);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400638
639 for (i = 0; i < dev->keycodemax; i++) {
640 if (input_fetch_keycode(dev, i) == old_keycode) {
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800641 __set_bit(old_keycode, dev->keybit);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400642 break; /* Setting the bit twice is useless, so break */
643 }
644 }
645
646 return 0;
647}
648
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400649/**
650 * input_get_keycode - retrieve keycode currently mapped to a given scancode
651 * @dev: input device which keymap is being queried
652 * @scancode: scancode (or its equivalent for device in question) for which
653 * keycode is needed
654 * @keycode: result
655 *
656 * This function should be called by anyone interested in retrieving current
657 * keymap. Presently keyboard and evdev handlers use it.
658 */
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800659int input_get_keycode(struct input_dev *dev,
660 unsigned int scancode, unsigned int *keycode)
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400661{
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700662 unsigned long flags;
663 int retval;
664
665 spin_lock_irqsave(&dev->event_lock, flags);
666 retval = dev->getkeycode(dev, scancode, keycode);
667 spin_unlock_irqrestore(&dev->event_lock, flags);
668
669 return retval;
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400670}
671EXPORT_SYMBOL(input_get_keycode);
672
673/**
674 * input_get_keycode - assign new keycode to a given scancode
675 * @dev: input device which keymap is being updated
676 * @scancode: scancode (or its equivalent for device in question)
677 * @keycode: new keycode to be assigned to the scancode
678 *
679 * This function should be called by anyone needing to update current
680 * keymap. Presently keyboard and evdev handlers use it.
681 */
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800682int input_set_keycode(struct input_dev *dev,
683 unsigned int scancode, unsigned int keycode)
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400684{
685 unsigned long flags;
686 int old_keycode;
687 int retval;
688
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800689 if (keycode > KEY_MAX)
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400690 return -EINVAL;
691
692 spin_lock_irqsave(&dev->event_lock, flags);
693
694 retval = dev->getkeycode(dev, scancode, &old_keycode);
695 if (retval)
696 goto out;
697
698 retval = dev->setkeycode(dev, scancode, keycode);
699 if (retval)
700 goto out;
701
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800702 /* Make sure KEY_RESERVED did not get enabled. */
703 __clear_bit(KEY_RESERVED, dev->keybit);
704
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400705 /*
706 * Simulate keyup event if keycode is not present
707 * in the keymap anymore
708 */
709 if (test_bit(EV_KEY, dev->evbit) &&
710 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
711 __test_and_clear_bit(old_keycode, dev->key)) {
712
713 input_pass_event(dev, EV_KEY, old_keycode, 0);
714 if (dev->sync)
715 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
716 }
717
718 out:
719 spin_unlock_irqrestore(&dev->event_lock, flags);
720
721 return retval;
722}
723EXPORT_SYMBOL(input_set_keycode);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725#define MATCH_BIT(bit, max) \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700726 for (i = 0; i < BITS_TO_LONGS(max); i++) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
728 break; \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700729 if (i != BITS_TO_LONGS(max)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 continue;
731
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800732static const struct input_device_id *input_match_device(struct input_handler *handler,
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400733 struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800735 const struct input_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int i;
737
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800738 for (id = handler->id_table; id->flags || id->driver_info; id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400741 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 continue;
743
744 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400745 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 continue;
747
748 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400749 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 continue;
751
752 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400753 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 continue;
755
756 MATCH_BIT(evbit, EV_MAX);
757 MATCH_BIT(keybit, KEY_MAX);
758 MATCH_BIT(relbit, REL_MAX);
759 MATCH_BIT(absbit, ABS_MAX);
760 MATCH_BIT(mscbit, MSC_MAX);
761 MATCH_BIT(ledbit, LED_MAX);
762 MATCH_BIT(sndbit, SND_MAX);
763 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500764 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800766 if (!handler->match || handler->match(handler, dev))
767 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
770 return NULL;
771}
772
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400773static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
774{
775 const struct input_device_id *id;
776 int error;
777
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800778 id = input_match_device(handler, dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400779 if (!id)
780 return -ENODEV;
781
782 error = handler->connect(handler, dev, id);
783 if (error && error != -ENODEV)
784 printk(KERN_ERR
785 "input: failed to attach handler %s to device %s, "
786 "error: %d\n",
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400787 handler->name, kobject_name(&dev->dev.kobj), error);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400788
789 return error;
790}
791
Dmitry Torokhov15e184a2010-01-11 00:05:43 -0800792#ifdef CONFIG_COMPAT
793
794static int input_bits_to_string(char *buf, int buf_size,
795 unsigned long bits, bool skip_empty)
796{
797 int len = 0;
798
799 if (INPUT_COMPAT_TEST) {
800 u32 dword = bits >> 32;
801 if (dword || !skip_empty)
802 len += snprintf(buf, buf_size, "%x ", dword);
803
804 dword = bits & 0xffffffffUL;
805 if (dword || !skip_empty || len)
806 len += snprintf(buf + len, max(buf_size - len, 0),
807 "%x", dword);
808 } else {
809 if (bits || !skip_empty)
810 len += snprintf(buf, buf_size, "%lx", bits);
811 }
812
813 return len;
814}
815
816#else /* !CONFIG_COMPAT */
817
818static int input_bits_to_string(char *buf, int buf_size,
819 unsigned long bits, bool skip_empty)
820{
821 return bits || !skip_empty ?
822 snprintf(buf, buf_size, "%lx", bits) : 0;
823}
824
825#endif
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500828
829static struct proc_dir_entry *proc_bus_input_dir;
830static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
831static int input_devices_state;
832
833static inline void input_wakeup_procfs_readers(void)
834{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 input_devices_state++;
836 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500839static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500841 poll_wait(file, &input_devices_poll_wait, wait);
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800842 if (file->f_version != input_devices_state) {
843 file->f_version = input_devices_state;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500844 return POLLIN | POLLRDNORM;
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800845 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400846
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700850union input_seq_state {
851 struct {
852 unsigned short pos;
853 bool mutex_acquired;
854 };
855 void *p;
856};
857
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500858static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
859{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700860 union input_seq_state *state = (union input_seq_state *)&seq->private;
861 int error;
862
863 /* We need to fit into seq->private pointer */
864 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
865
866 error = mutex_lock_interruptible(&input_mutex);
867 if (error) {
868 state->mutex_acquired = false;
869 return ERR_PTR(error);
870 }
871
872 state->mutex_acquired = true;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500873
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400874 return seq_list_start(&input_dev_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500875}
876
877static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
878{
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400879 return seq_list_next(v, &input_dev_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500880}
881
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700882static void input_seq_stop(struct seq_file *seq, void *v)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500883{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700884 union input_seq_state *state = (union input_seq_state *)&seq->private;
885
886 if (state->mutex_acquired)
887 mutex_unlock(&input_mutex);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500888}
889
890static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
891 unsigned long *bitmap, int max)
892{
893 int i;
Dmitry Torokhov15e184a2010-01-11 00:05:43 -0800894 bool skip_empty = true;
895 char buf[18];
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500896
897 seq_printf(seq, "B: %s=", name);
Dmitry Torokhov15e184a2010-01-11 00:05:43 -0800898
899 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
900 if (input_bits_to_string(buf, sizeof(buf),
901 bitmap[i], skip_empty)) {
902 skip_empty = false;
903 seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
904 }
905 }
906
907 /*
908 * If no output was produced print a single 0.
909 */
910 if (skip_empty)
911 seq_puts(seq, "0");
912
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500913 seq_putc(seq, '\n');
914}
915
916static int input_devices_seq_show(struct seq_file *seq, void *v)
917{
918 struct input_dev *dev = container_of(v, struct input_dev, node);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400919 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct input_handle *handle;
921
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500922 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
923 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500925 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
926 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
927 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
Dmitry Torokhov15e03ae2007-03-07 23:20:17 -0500928 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500929 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500931 list_for_each_entry(handle, &dev->h_list, d_node)
932 seq_printf(seq, "%s ", handle->name);
933 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500934
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500935 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
936 if (test_bit(EV_KEY, dev->evbit))
937 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
938 if (test_bit(EV_REL, dev->evbit))
939 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
940 if (test_bit(EV_ABS, dev->evbit))
941 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
942 if (test_bit(EV_MSC, dev->evbit))
943 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
944 if (test_bit(EV_LED, dev->evbit))
945 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
946 if (test_bit(EV_SND, dev->evbit))
947 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
948 if (test_bit(EV_FF, dev->evbit))
949 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
950 if (test_bit(EV_SW, dev->evbit))
951 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500953 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500955 kfree(path);
956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Jan Engelhardtcec69c32008-01-31 00:43:32 -0500959static const struct seq_operations input_devices_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500960 .start = input_devices_seq_start,
961 .next = input_devices_seq_next,
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700962 .stop = input_seq_stop,
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500963 .show = input_devices_seq_show,
964};
965
966static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500968 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800971static const struct file_operations input_devices_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500972 .owner = THIS_MODULE,
973 .open = input_proc_devices_open,
974 .poll = input_proc_devices_poll,
975 .read = seq_read,
976 .llseek = seq_lseek,
977 .release = seq_release,
978};
979
980static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
981{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700982 union input_seq_state *state = (union input_seq_state *)&seq->private;
983 int error;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400984
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700985 /* We need to fit into seq->private pointer */
986 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
987
988 error = mutex_lock_interruptible(&input_mutex);
989 if (error) {
990 state->mutex_acquired = false;
991 return ERR_PTR(error);
992 }
993
994 state->mutex_acquired = true;
995 state->pos = *pos;
996
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400997 return seq_list_start(&input_handler_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500998}
999
1000static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1001{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001002 union input_seq_state *state = (union input_seq_state *)&seq->private;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001003
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001004 state->pos = *pos + 1;
1005 return seq_list_next(v, &input_handler_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001006}
1007
1008static int input_handlers_seq_show(struct seq_file *seq, void *v)
1009{
1010 struct input_handler *handler = container_of(v, struct input_handler, node);
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001011 union input_seq_state *state = (union input_seq_state *)&seq->private;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001012
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001013 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
Dmitry Torokhovef7995f2010-01-29 23:59:12 -08001014 if (handler->filter)
1015 seq_puts(seq, " (filter)");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001016 if (handler->fops)
1017 seq_printf(seq, " Minor=%d", handler->minor);
1018 seq_putc(seq, '\n');
1019
1020 return 0;
1021}
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001022
Jan Engelhardtcec69c32008-01-31 00:43:32 -05001023static const struct seq_operations input_handlers_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001024 .start = input_handlers_seq_start,
1025 .next = input_handlers_seq_next,
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001026 .stop = input_seq_stop,
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001027 .show = input_handlers_seq_show,
1028};
1029
1030static int input_proc_handlers_open(struct inode *inode, struct file *file)
1031{
1032 return seq_open(file, &input_handlers_seq_ops);
1033}
1034
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001035static const struct file_operations input_handlers_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001036 .owner = THIS_MODULE,
1037 .open = input_proc_handlers_open,
1038 .read = seq_read,
1039 .llseek = seq_lseek,
1040 .release = seq_release,
1041};
Luke Kosewskie334016fc2005-06-01 02:39:28 -05001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043static int __init input_proc_init(void)
1044{
1045 struct proc_dir_entry *entry;
1046
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001047 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001048 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001050
Denis V. Lunevc7705f32008-04-29 01:02:35 -07001051 entry = proc_create("devices", 0, proc_bus_input_dir,
1052 &input_devices_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001053 if (!entry)
1054 goto fail1;
1055
Denis V. Lunevc7705f32008-04-29 01:02:35 -07001056 entry = proc_create("handlers", 0, proc_bus_input_dir,
1057 &input_handlers_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001058 if (!entry)
1059 goto fail2;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001062
1063 fail2: remove_proc_entry("devices", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001064 fail1: remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001065 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001067
Andrew Mortonbeffbdc2005-07-01 23:54:30 -05001068static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001069{
1070 remove_proc_entry("devices", proc_bus_input_dir);
1071 remove_proc_entry("handlers", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001072 remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001076static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001078static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079#endif
1080
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001081#define INPUT_DEV_STRING_ATTR_SHOW(name) \
1082static ssize_t input_dev_show_##name(struct device *dev, \
1083 struct device_attribute *attr, \
1084 char *buf) \
1085{ \
1086 struct input_dev *input_dev = to_input_dev(dev); \
1087 \
1088 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1089 input_dev->name ? input_dev->name : ""); \
1090} \
1091static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001092
1093INPUT_DEV_STRING_ATTR_SHOW(name);
1094INPUT_DEV_STRING_ATTR_SHOW(phys);
1095INPUT_DEV_STRING_ATTR_SHOW(uniq);
1096
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001097static int input_print_modalias_bits(char *buf, int size,
1098 char name, unsigned long *bm,
1099 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001100{
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001101 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001102
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001103 len += snprintf(buf, max(size, 0), "%c", name);
1104 for (i = min_bit; i < max_bit; i++)
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001105 if (bm[BIT_WORD(i)] & BIT_MASK(i))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001106 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001107 return len;
1108}
1109
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001110static int input_print_modalias(char *buf, int size, struct input_dev *id,
1111 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001112{
1113 int len;
1114
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001115 len = snprintf(buf, max(size, 0),
1116 "input:b%04Xv%04Xp%04Xe%04X-",
1117 id->id.bustype, id->id.vendor,
1118 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001119
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001120 len += input_print_modalias_bits(buf + len, size - len,
1121 'e', id->evbit, 0, EV_MAX);
1122 len += input_print_modalias_bits(buf + len, size - len,
1123 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1124 len += input_print_modalias_bits(buf + len, size - len,
1125 'r', id->relbit, 0, REL_MAX);
1126 len += input_print_modalias_bits(buf + len, size - len,
1127 'a', id->absbit, 0, ABS_MAX);
1128 len += input_print_modalias_bits(buf + len, size - len,
1129 'm', id->mscbit, 0, MSC_MAX);
1130 len += input_print_modalias_bits(buf + len, size - len,
1131 'l', id->ledbit, 0, LED_MAX);
1132 len += input_print_modalias_bits(buf + len, size - len,
1133 's', id->sndbit, 0, SND_MAX);
1134 len += input_print_modalias_bits(buf + len, size - len,
1135 'f', id->ffbit, 0, FF_MAX);
1136 len += input_print_modalias_bits(buf + len, size - len,
1137 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001138
1139 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001140 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001141
Rusty Russell1d8f4302005-12-07 21:40:34 +01001142 return len;
1143}
1144
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001145static ssize_t input_dev_show_modalias(struct device *dev,
1146 struct device_attribute *attr,
1147 char *buf)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001148{
1149 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001150 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001151
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001152 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1153
Richard Purdie8a3cf452006-06-26 01:48:21 -04001154 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001155}
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001156static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001157
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001158static struct attribute *input_dev_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001159 &dev_attr_name.attr,
1160 &dev_attr_phys.attr,
1161 &dev_attr_uniq.attr,
1162 &dev_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001163 NULL
1164};
1165
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001166static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001167 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001168};
1169
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001170#define INPUT_DEV_ID_ATTR(name) \
1171static ssize_t input_dev_show_id_##name(struct device *dev, \
1172 struct device_attribute *attr, \
1173 char *buf) \
1174{ \
1175 struct input_dev *input_dev = to_input_dev(dev); \
1176 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1177} \
1178static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001179
1180INPUT_DEV_ID_ATTR(bustype);
1181INPUT_DEV_ID_ATTR(vendor);
1182INPUT_DEV_ID_ATTR(product);
1183INPUT_DEV_ID_ATTR(version);
1184
1185static struct attribute *input_dev_id_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001186 &dev_attr_bustype.attr,
1187 &dev_attr_vendor.attr,
1188 &dev_attr_product.attr,
1189 &dev_attr_version.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001190 NULL
1191};
1192
1193static struct attribute_group input_dev_id_attr_group = {
1194 .name = "id",
1195 .attrs = input_dev_id_attrs,
1196};
1197
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001198static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1199 int max, int add_cr)
1200{
1201 int i;
1202 int len = 0;
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001203 bool skip_empty = true;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001204
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001205 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1206 len += input_bits_to_string(buf + len, max(buf_size - len, 0),
1207 bitmap[i], skip_empty);
1208 if (len) {
1209 skip_empty = false;
1210 if (i > 0)
1211 len += snprintf(buf + len, max(buf_size - len, 0), " ");
1212 }
1213 }
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001214
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001215 /*
1216 * If no output was produced print a single 0.
1217 */
1218 if (len == 0)
1219 len = snprintf(buf, buf_size, "%d", 0);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001220
1221 if (add_cr)
1222 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1223
1224 return len;
1225}
1226
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001227#define INPUT_DEV_CAP_ATTR(ev, bm) \
1228static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1229 struct device_attribute *attr, \
1230 char *buf) \
1231{ \
1232 struct input_dev *input_dev = to_input_dev(dev); \
1233 int len = input_print_bitmap(buf, PAGE_SIZE, \
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001234 input_dev->bm##bit, ev##_MAX, \
1235 true); \
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001236 return min_t(int, len, PAGE_SIZE); \
1237} \
1238static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001239
1240INPUT_DEV_CAP_ATTR(EV, ev);
1241INPUT_DEV_CAP_ATTR(KEY, key);
1242INPUT_DEV_CAP_ATTR(REL, rel);
1243INPUT_DEV_CAP_ATTR(ABS, abs);
1244INPUT_DEV_CAP_ATTR(MSC, msc);
1245INPUT_DEV_CAP_ATTR(LED, led);
1246INPUT_DEV_CAP_ATTR(SND, snd);
1247INPUT_DEV_CAP_ATTR(FF, ff);
1248INPUT_DEV_CAP_ATTR(SW, sw);
1249
1250static struct attribute *input_dev_caps_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001251 &dev_attr_ev.attr,
1252 &dev_attr_key.attr,
1253 &dev_attr_rel.attr,
1254 &dev_attr_abs.attr,
1255 &dev_attr_msc.attr,
1256 &dev_attr_led.attr,
1257 &dev_attr_snd.attr,
1258 &dev_attr_ff.attr,
1259 &dev_attr_sw.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001260 NULL
1261};
1262
1263static struct attribute_group input_dev_caps_attr_group = {
1264 .name = "capabilities",
1265 .attrs = input_dev_caps_attrs,
1266};
1267
David Brownella4dbd672009-06-24 10:06:31 -07001268static const struct attribute_group *input_dev_attr_groups[] = {
Dmitry Torokhovcb9def42007-03-07 23:20:26 -05001269 &input_dev_attr_group,
1270 &input_dev_id_attr_group,
1271 &input_dev_caps_attr_group,
1272 NULL
1273};
1274
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001275static void input_dev_release(struct device *device)
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001276{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001277 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001278
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001279 input_ff_destroy(dev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001280 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001281
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001282 module_put(THIS_MODULE);
1283}
1284
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001285/*
Kay Sievers312c0042005-11-16 09:00:00 +01001286 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001287 * device bitfields.
1288 */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001289static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001290 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001291{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001292 int len;
1293
1294 if (add_uevent_var(env, "%s=", name))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001295 return -ENOMEM;
1296
Kay Sievers7eff2e72007-08-14 15:15:12 +02001297 len = input_print_bitmap(&env->buf[env->buflen - 1],
1298 sizeof(env->buf) - env->buflen,
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001299 bitmap, max, false);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001300 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001301 return -ENOMEM;
1302
Kay Sievers7eff2e72007-08-14 15:15:12 +02001303 env->buflen += len;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001304 return 0;
1305}
1306
Kay Sievers7eff2e72007-08-14 15:15:12 +02001307static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001308 struct input_dev *dev)
1309{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001310 int len;
1311
1312 if (add_uevent_var(env, "MODALIAS="))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001313 return -ENOMEM;
1314
Kay Sievers7eff2e72007-08-14 15:15:12 +02001315 len = input_print_modalias(&env->buf[env->buflen - 1],
1316 sizeof(env->buf) - env->buflen,
1317 dev, 0);
1318 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001319 return -ENOMEM;
1320
Kay Sievers7eff2e72007-08-14 15:15:12 +02001321 env->buflen += len;
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001322 return 0;
1323}
1324
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001325#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1326 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001327 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001328 if (err) \
1329 return err; \
1330 } while (0)
1331
1332#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1333 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001334 int err = input_add_uevent_bm_var(env, name, bm, max); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001335 if (err) \
1336 return err; \
1337 } while (0)
1338
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001339#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1340 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001341 int err = input_add_uevent_modalias_var(env, dev); \
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001342 if (err) \
1343 return err; \
1344 } while (0)
1345
Kay Sievers7eff2e72007-08-14 15:15:12 +02001346static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001347{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001348 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001349
1350 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1351 dev->id.bustype, dev->id.vendor,
1352 dev->id.product, dev->id.version);
1353 if (dev->name)
1354 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1355 if (dev->phys)
1356 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -08001357 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001358 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1359
1360 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1361 if (test_bit(EV_KEY, dev->evbit))
1362 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1363 if (test_bit(EV_REL, dev->evbit))
1364 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1365 if (test_bit(EV_ABS, dev->evbit))
1366 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1367 if (test_bit(EV_MSC, dev->evbit))
1368 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1369 if (test_bit(EV_LED, dev->evbit))
1370 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1371 if (test_bit(EV_SND, dev->evbit))
1372 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1373 if (test_bit(EV_FF, dev->evbit))
1374 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1375 if (test_bit(EV_SW, dev->evbit))
1376 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1377
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001378 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001379
1380 return 0;
1381}
1382
Dmitry Torokhov3cc96352009-11-12 23:19:05 -08001383#define INPUT_DO_TOGGLE(dev, type, bits, on) \
1384 do { \
1385 int i; \
1386 bool active; \
1387 \
1388 if (!test_bit(EV_##type, dev->evbit)) \
1389 break; \
1390 \
1391 for (i = 0; i < type##_MAX; i++) { \
1392 if (!test_bit(i, dev->bits##bit)) \
1393 continue; \
1394 \
1395 active = test_bit(i, dev->bits); \
1396 if (!active && !on) \
1397 continue; \
1398 \
1399 dev->event(dev, EV_##type, i, on ? active : 0); \
1400 } \
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001401 } while (0)
1402
Andrew Morton1c4115e2009-10-01 15:43:55 -07001403#ifdef CONFIG_PM
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001404static void input_dev_reset(struct input_dev *dev, bool activate)
1405{
1406 if (!dev->event)
1407 return;
1408
1409 INPUT_DO_TOGGLE(dev, LED, led, activate);
1410 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1411
1412 if (activate && test_bit(EV_REP, dev->evbit)) {
1413 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1414 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1415 }
1416}
1417
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001418static int input_dev_suspend(struct device *dev)
1419{
1420 struct input_dev *input_dev = to_input_dev(dev);
1421
1422 mutex_lock(&input_dev->mutex);
1423 input_dev_reset(input_dev, false);
1424 mutex_unlock(&input_dev->mutex);
1425
1426 return 0;
1427}
1428
1429static int input_dev_resume(struct device *dev)
1430{
1431 struct input_dev *input_dev = to_input_dev(dev);
1432
1433 mutex_lock(&input_dev->mutex);
1434 input_dev_reset(input_dev, true);
1435 mutex_unlock(&input_dev->mutex);
1436
1437 return 0;
1438}
1439
1440static const struct dev_pm_ops input_dev_pm_ops = {
1441 .suspend = input_dev_suspend,
1442 .resume = input_dev_resume,
1443 .poweroff = input_dev_suspend,
1444 .restore = input_dev_resume,
1445};
1446#endif /* CONFIG_PM */
1447
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001448static struct device_type input_dev_type = {
1449 .groups = input_dev_attr_groups,
1450 .release = input_dev_release,
1451 .uevent = input_dev_uevent,
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001452#ifdef CONFIG_PM
1453 .pm = &input_dev_pm_ops,
1454#endif
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001455};
1456
Kay Sieverse454cea2009-09-18 23:01:12 +02001457static char *input_devnode(struct device *dev, mode_t *mode)
Kay Sieversaa5ed632009-04-30 15:23:42 +02001458{
1459 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1460}
1461
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001462struct class input_class = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001463 .name = "input",
Kay Sieverse454cea2009-09-18 23:01:12 +02001464 .devnode = input_devnode,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001465};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001466EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001467
Dmitry Torokhov14471902006-11-02 23:26:55 -05001468/**
1469 * input_allocate_device - allocate memory for new input device
1470 *
1471 * Returns prepared struct input_dev or NULL.
1472 *
1473 * NOTE: Use input_free_device() to free devices that have not been
1474 * registered; input_unregister_device() should be used for already
1475 * registered devices.
1476 */
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001477struct input_dev *input_allocate_device(void)
1478{
1479 struct input_dev *dev;
1480
1481 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1482 if (dev) {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001483 dev->dev.type = &input_dev_type;
1484 dev->dev.class = &input_class;
1485 device_initialize(&dev->dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001486 mutex_init(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001487 spin_lock_init(&dev->event_lock);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001488 INIT_LIST_HEAD(&dev->h_list);
1489 INIT_LIST_HEAD(&dev->node);
Dmitry Torokhov655816e2006-09-14 01:32:14 -04001490
1491 __module_get(THIS_MODULE);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001492 }
1493
1494 return dev;
1495}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001496EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001497
Dmitry Torokhov14471902006-11-02 23:26:55 -05001498/**
1499 * input_free_device - free memory occupied by input_dev structure
1500 * @dev: input device to free
1501 *
1502 * This function should only be used if input_register_device()
1503 * was not called yet or if it failed. Once device was registered
1504 * use input_unregister_device() and memory will be freed once last
Dmitry Torokhov80064792007-08-30 00:22:11 -04001505 * reference to the device is dropped.
Dmitry Torokhov14471902006-11-02 23:26:55 -05001506 *
1507 * Device should be allocated by input_allocate_device().
1508 *
1509 * NOTE: If there are references to the input device then memory
1510 * will not be freed until last reference is dropped.
1511 */
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001512void input_free_device(struct input_dev *dev)
1513{
Dmitry Torokhov54f9e362007-03-16 00:57:25 -04001514 if (dev)
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001515 input_put_device(dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001516}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001517EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001518
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001519/**
1520 * input_set_capability - mark device as capable of a certain event
1521 * @dev: device that is capable of emitting or accepting event
1522 * @type: type of the event (EV_KEY, EV_REL, etc...)
1523 * @code: event code
1524 *
1525 * In addition to setting up corresponding bit in appropriate capability
1526 * bitmap the function also adjusts dev->evbit.
1527 */
1528void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1529{
1530 switch (type) {
1531 case EV_KEY:
1532 __set_bit(code, dev->keybit);
1533 break;
1534
1535 case EV_REL:
1536 __set_bit(code, dev->relbit);
1537 break;
1538
1539 case EV_ABS:
1540 __set_bit(code, dev->absbit);
1541 break;
1542
1543 case EV_MSC:
1544 __set_bit(code, dev->mscbit);
1545 break;
1546
1547 case EV_SW:
1548 __set_bit(code, dev->swbit);
1549 break;
1550
1551 case EV_LED:
1552 __set_bit(code, dev->ledbit);
1553 break;
1554
1555 case EV_SND:
1556 __set_bit(code, dev->sndbit);
1557 break;
1558
1559 case EV_FF:
1560 __set_bit(code, dev->ffbit);
1561 break;
1562
Dmitry Baryshkov22d1c392007-12-14 01:21:03 -05001563 case EV_PWR:
1564 /* do nothing */
1565 break;
1566
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001567 default:
1568 printk(KERN_ERR
1569 "input_set_capability: unknown type %u (code %u)\n",
1570 type, code);
1571 dump_stack();
1572 return;
1573 }
1574
1575 __set_bit(type, dev->evbit);
1576}
1577EXPORT_SYMBOL(input_set_capability);
1578
Dmitry Torokhov92a3a582010-01-05 17:56:01 -08001579#define INPUT_CLEANSE_BITMASK(dev, type, bits) \
1580 do { \
1581 if (!test_bit(EV_##type, dev->evbit)) \
1582 memset(dev->bits##bit, 0, \
1583 sizeof(dev->bits##bit)); \
1584 } while (0)
1585
1586static void input_cleanse_bitmasks(struct input_dev *dev)
1587{
1588 INPUT_CLEANSE_BITMASK(dev, KEY, key);
1589 INPUT_CLEANSE_BITMASK(dev, REL, rel);
1590 INPUT_CLEANSE_BITMASK(dev, ABS, abs);
1591 INPUT_CLEANSE_BITMASK(dev, MSC, msc);
1592 INPUT_CLEANSE_BITMASK(dev, LED, led);
1593 INPUT_CLEANSE_BITMASK(dev, SND, snd);
1594 INPUT_CLEANSE_BITMASK(dev, FF, ff);
1595 INPUT_CLEANSE_BITMASK(dev, SW, sw);
1596}
1597
Dmitry Torokhov80064792007-08-30 00:22:11 -04001598/**
1599 * input_register_device - register device with input core
1600 * @dev: device to be registered
1601 *
1602 * This function registers device with input core. The device must be
1603 * allocated with input_allocate_device() and all it's capabilities
1604 * set up before registering.
1605 * If function fails the device must be freed with input_free_device().
1606 * Once device has been successfully registered it can be unregistered
1607 * with input_unregister_device(); input_free_device() should not be
1608 * called in this case.
1609 */
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001610int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001611{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001612 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001613 struct input_handler *handler;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001614 const char *path;
1615 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001616
Dmitry Torokhov4f93df42010-01-05 17:56:00 -08001617 /* Every input device generates EV_SYN/SYN_REPORT events. */
Dmitry Torokhov80064792007-08-30 00:22:11 -04001618 __set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001619
Dmitry Torokhov4f93df42010-01-05 17:56:00 -08001620 /* KEY_RESERVED is not supposed to be transmitted to userspace. */
1621 __clear_bit(KEY_RESERVED, dev->keybit);
1622
Dmitry Torokhov92a3a582010-01-05 17:56:01 -08001623 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
1624 input_cleanse_bitmasks(dev);
1625
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001626 /*
1627 * If delay and period are pre-set by the driver, then autorepeating
1628 * is handled by the driver itself and we don't do it in input.c.
1629 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001630 init_timer(&dev->timer);
1631 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1632 dev->timer.data = (long) dev;
1633 dev->timer.function = input_repeat_key;
1634 dev->rep[REP_DELAY] = 250;
1635 dev->rep[REP_PERIOD] = 33;
1636 }
1637
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -04001638 if (!dev->getkeycode)
1639 dev->getkeycode = input_default_getkeycode;
1640
1641 if (!dev->setkeycode)
1642 dev->setkeycode = input_default_setkeycode;
1643
Kay Sieversa6c24902008-10-30 00:07:50 -04001644 dev_set_name(&dev->dev, "input%ld",
1645 (unsigned long) atomic_inc_return(&input_no) - 1);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001646
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001647 error = device_add(&dev->dev);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001648 if (error)
1649 return error;
1650
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001651 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001652 printk(KERN_INFO "input: %s as %s\n",
1653 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1654 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -07001655
Dmitry Torokhov80064792007-08-30 00:22:11 -04001656 error = mutex_lock_interruptible(&input_mutex);
1657 if (error) {
1658 device_del(&dev->dev);
1659 return error;
1660 }
1661
1662 list_add_tail(&dev->node, &input_dev_list);
1663
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001664 list_for_each_entry(handler, &input_handler_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001665 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001666
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001667 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001668
Dmitry Torokhov80064792007-08-30 00:22:11 -04001669 mutex_unlock(&input_mutex);
1670
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001671 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001672}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001673EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001674
Dmitry Torokhov80064792007-08-30 00:22:11 -04001675/**
1676 * input_unregister_device - unregister previously registered device
1677 * @dev: device to be unregistered
1678 *
1679 * This function unregisters an input device. Once device is unregistered
1680 * the caller should not try to access it as it may get freed at any moment.
1681 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001682void input_unregister_device(struct input_dev *dev)
1683{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001684 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001685
Dmitry Torokhov80064792007-08-30 00:22:11 -04001686 input_disconnect_device(dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001687
Dmitry Torokhov80064792007-08-30 00:22:11 -04001688 mutex_lock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001689
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001690 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001691 handle->handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001692 WARN_ON(!list_empty(&dev->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001693
Dmitry Torokhov80064792007-08-30 00:22:11 -04001694 del_timer_sync(&dev->timer);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001695 list_del_init(&dev->node);
1696
1697 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001698
1699 mutex_unlock(&input_mutex);
1700
1701 device_unregister(&dev->dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001702}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001703EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001704
Dmitry Torokhov80064792007-08-30 00:22:11 -04001705/**
1706 * input_register_handler - register a new input handler
1707 * @handler: handler to be registered
1708 *
1709 * This function registers a new input handler (interface) for input
1710 * devices in the system and attaches it to all input devices that
1711 * are compatible with the handler.
1712 */
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001713int input_register_handler(struct input_handler *handler)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001714{
1715 struct input_dev *dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001716 int retval;
1717
1718 retval = mutex_lock_interruptible(&input_mutex);
1719 if (retval)
1720 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001721
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001722 INIT_LIST_HEAD(&handler->h_list);
1723
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001724 if (handler->fops != NULL) {
Dmitry Torokhov80064792007-08-30 00:22:11 -04001725 if (input_table[handler->minor >> 5]) {
1726 retval = -EBUSY;
1727 goto out;
1728 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001729 input_table[handler->minor >> 5] = handler;
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001730 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001731
1732 list_add_tail(&handler->node, &input_handler_list);
1733
1734 list_for_each_entry(dev, &input_dev_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001735 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001736
1737 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001738
1739 out:
1740 mutex_unlock(&input_mutex);
1741 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001742}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001743EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001744
Dmitry Torokhov80064792007-08-30 00:22:11 -04001745/**
1746 * input_unregister_handler - unregisters an input handler
1747 * @handler: handler to be unregistered
1748 *
1749 * This function disconnects a handler from its input devices and
1750 * removes it from lists of known handlers.
1751 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001752void input_unregister_handler(struct input_handler *handler)
1753{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001754 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001755
Dmitry Torokhov80064792007-08-30 00:22:11 -04001756 mutex_lock(&input_mutex);
1757
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001758 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001759 handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001760 WARN_ON(!list_empty(&handler->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001761
1762 list_del_init(&handler->node);
1763
1764 if (handler->fops != NULL)
1765 input_table[handler->minor >> 5] = NULL;
1766
1767 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001768
1769 mutex_unlock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001770}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001771EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001772
Dmitry Torokhov80064792007-08-30 00:22:11 -04001773/**
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001774 * input_handler_for_each_handle - handle iterator
1775 * @handler: input handler to iterate
1776 * @data: data for the callback
1777 * @fn: function to be called for each handle
1778 *
1779 * Iterate over @bus's list of devices, and call @fn for each, passing
1780 * it @data and stop when @fn returns a non-zero value. The function is
1781 * using RCU to traverse the list and therefore may be usind in atonic
1782 * contexts. The @fn callback is invoked from RCU critical section and
1783 * thus must not sleep.
1784 */
1785int input_handler_for_each_handle(struct input_handler *handler, void *data,
1786 int (*fn)(struct input_handle *, void *))
1787{
1788 struct input_handle *handle;
1789 int retval = 0;
1790
1791 rcu_read_lock();
1792
1793 list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
1794 retval = fn(handle, data);
1795 if (retval)
1796 break;
1797 }
1798
1799 rcu_read_unlock();
1800
1801 return retval;
1802}
1803EXPORT_SYMBOL(input_handler_for_each_handle);
1804
1805/**
Dmitry Torokhov80064792007-08-30 00:22:11 -04001806 * input_register_handle - register a new input handle
1807 * @handle: handle to register
1808 *
1809 * This function puts a new input handle onto device's
1810 * and handler's lists so that events can flow through
1811 * it once it is opened using input_open_device().
1812 *
1813 * This function is supposed to be called from handler's
1814 * connect() method.
1815 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001816int input_register_handle(struct input_handle *handle)
1817{
1818 struct input_handler *handler = handle->handler;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001819 struct input_dev *dev = handle->dev;
1820 int error;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001821
Dmitry Torokhov80064792007-08-30 00:22:11 -04001822 /*
1823 * We take dev->mutex here to prevent race with
1824 * input_release_device().
1825 */
1826 error = mutex_lock_interruptible(&dev->mutex);
1827 if (error)
1828 return error;
Dmitry Torokhovef7995f2010-01-29 23:59:12 -08001829
1830 /*
1831 * Filters go to the head of the list, normal handlers
1832 * to the tail.
1833 */
1834 if (handler->filter)
1835 list_add_rcu(&handle->d_node, &dev->h_list);
1836 else
1837 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1838
Dmitry Torokhov80064792007-08-30 00:22:11 -04001839 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001840
1841 /*
1842 * Since we are supposed to be called from ->connect()
1843 * which is mutually exclusive with ->disconnect()
1844 * we can't be racing with input_unregister_handle()
1845 * and so separate lock is not needed here.
1846 */
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001847 list_add_tail_rcu(&handle->h_node, &handler->h_list);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001848
1849 if (handler->start)
1850 handler->start(handle);
1851
1852 return 0;
1853}
1854EXPORT_SYMBOL(input_register_handle);
1855
Dmitry Torokhov80064792007-08-30 00:22:11 -04001856/**
1857 * input_unregister_handle - unregister an input handle
1858 * @handle: handle to unregister
1859 *
1860 * This function removes input handle from device's
1861 * and handler's lists.
1862 *
1863 * This function is supposed to be called from handler's
1864 * disconnect() method.
1865 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001866void input_unregister_handle(struct input_handle *handle)
1867{
Dmitry Torokhov80064792007-08-30 00:22:11 -04001868 struct input_dev *dev = handle->dev;
1869
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001870 list_del_rcu(&handle->h_node);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001871
1872 /*
1873 * Take dev->mutex to prevent race with input_release_device().
1874 */
1875 mutex_lock(&dev->mutex);
1876 list_del_rcu(&handle->d_node);
1877 mutex_unlock(&dev->mutex);
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001878
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -04001879 synchronize_rcu();
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001880}
1881EXPORT_SYMBOL(input_unregister_handle);
1882
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001883static int input_open_file(struct inode *inode, struct file *file)
1884{
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001885 struct input_handler *handler;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001886 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001887 int err;
1888
Arnd Bergmann2f2177c2010-03-09 20:38:48 -08001889 err = mutex_lock_interruptible(&input_mutex);
1890 if (err)
1891 return err;
1892
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001893 /* No load-on-demand here? */
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001894 handler = input_table[iminor(inode) >> 5];
Arnd Bergmann2f2177c2010-03-09 20:38:48 -08001895 if (handler)
1896 new_fops = fops_get(handler->fops);
1897
1898 mutex_unlock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001899
1900 /*
1901 * That's _really_ odd. Usually NULL ->open means "nothing special",
1902 * not "no device". Oh, well...
1903 */
Arnd Bergmann2f2177c2010-03-09 20:38:48 -08001904 if (!new_fops || !new_fops->open) {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001905 fops_put(new_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001906 err = -ENODEV;
1907 goto out;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001908 }
Arnd Bergmann2f2177c2010-03-09 20:38:48 -08001909
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001910 old_fops = file->f_op;
1911 file->f_op = new_fops;
1912
1913 err = new_fops->open(inode, file);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001914 if (err) {
1915 fops_put(file->f_op);
1916 file->f_op = fops_get(old_fops);
1917 }
1918 fops_put(old_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001919out:
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001920 return err;
1921}
1922
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001923static const struct file_operations input_fops = {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001924 .owner = THIS_MODULE,
1925 .open = input_open_file,
1926};
1927
Henrik Rydberg61994a62009-04-28 07:45:31 -07001928static void __init input_init_abs_bypass(void)
1929{
1930 const unsigned int *p;
1931
1932 for (p = input_abs_bypass_init_data; *p; p++)
1933 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936static int __init input_init(void)
1937{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001938 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Henrik Rydberg61994a62009-04-28 07:45:31 -07001940 input_init_abs_bypass();
1941
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001942 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001943 if (err) {
1944 printk(KERN_ERR "input: unable to register input_dev class\n");
1945 return err;
1946 }
1947
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001948 err = input_proc_init();
1949 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001950 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001951
1952 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1953 if (err) {
1954 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001955 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001957
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001958 return 0;
1959
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001960 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001961 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001962 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
1965static void __exit input_exit(void)
1966{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001967 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001969 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
1972subsys_initcall(input_init);
1973module_exit(input_exit);