blob: 9c79bd56b51acbf17d90033789801d984bc492b8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/random.h>
19#include <linux/major.h>
20#include <linux/proc_fs.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040021#include <linux/sched.h>
Dmitry Torokhov969b21c2006-04-02 00:09:34 -050022#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/poll.h>
24#include <linux/device.h>
Jes Sorensene676c232006-02-19 00:21:46 -050025#include <linux/mutex.h>
Dmitry Torokhov80064792007-08-30 00:22:11 -040026#include <linux/rcupdate.h>
Jonathan Corbet2edbf852008-05-15 10:37:16 -060027#include <linux/smp_lock.h>
Dmitry Torokhov15e184a2010-01-11 00:05:43 -080028#include "input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
31MODULE_DESCRIPTION("Input core");
32MODULE_LICENSE("GPL");
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define INPUT_DEVICES 256
35
Henrik Rydberg61994a62009-04-28 07:45:31 -070036/*
37 * EV_ABS events which should not be cached are listed here.
38 */
39static unsigned int input_abs_bypass_init_data[] __initdata = {
Henrik Rydberg5e5ee682009-04-28 07:47:33 -070040 ABS_MT_TOUCH_MAJOR,
41 ABS_MT_TOUCH_MINOR,
42 ABS_MT_WIDTH_MAJOR,
43 ABS_MT_WIDTH_MINOR,
44 ABS_MT_ORIENTATION,
45 ABS_MT_POSITION_X,
46 ABS_MT_POSITION_Y,
47 ABS_MT_TOOL_TYPE,
48 ABS_MT_BLOB_ID,
Henrik Rydbergdf391e02009-05-23 09:51:20 -070049 ABS_MT_TRACKING_ID,
Henrik Rydbergcb6ecf62010-01-28 22:28:27 -080050 ABS_MT_PRESSURE,
Henrik Rydberg61994a62009-04-28 07:45:31 -070051 0
52};
53static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static LIST_HEAD(input_dev_list);
56static LIST_HEAD(input_handler_list);
57
Dmitry Torokhov80064792007-08-30 00:22:11 -040058/*
59 * input_mutex protects access to both input_dev_list and input_handler_list.
60 * This also causes input_[un]register_device and input_[un]register_handler
61 * be mutually exclusive which simplifies locking in drivers implementing
62 * input handlers.
63 */
64static DEFINE_MUTEX(input_mutex);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static struct input_handler *input_table[8];
67
Dmitry Torokhov80064792007-08-30 00:22:11 -040068static inline int is_event_supported(unsigned int code,
69 unsigned long *bm, unsigned int max)
70{
71 return code <= max && test_bit(code, bm);
72}
73
74static int input_defuzz_abs_event(int value, int old_val, int fuzz)
75{
76 if (fuzz) {
77 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
78 return old_val;
79
80 if (value > old_val - fuzz && value < old_val + fuzz)
81 return (old_val * 3 + value) / 4;
82
83 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
84 return (old_val + value) / 2;
85 }
86
87 return value;
88}
89
90/*
Dmitry Torokhovef7995f2010-01-29 23:59:12 -080091 * Pass event first through all filters and then, if event has not been
92 * filtered out, through all open handles. This function is called with
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040093 * dev->event_lock held and interrupts disabled.
Dmitry Torokhov80064792007-08-30 00:22:11 -040094 */
95static void input_pass_event(struct input_dev *dev,
96 unsigned int type, unsigned int code, int value)
97{
Dmitry Torokhovef7995f2010-01-29 23:59:12 -080098 struct input_handler *handler;
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040099 struct input_handle *handle;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400100
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400101 rcu_read_lock();
102
103 handle = rcu_dereference(dev->grab);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400104 if (handle)
105 handle->handler->event(handle, type, code, value);
Dmitry Torokhovef7995f2010-01-29 23:59:12 -0800106 else {
107 bool filtered = false;
108
109 list_for_each_entry_rcu(handle, &dev->h_list, d_node) {
110 if (!handle->open)
111 continue;
112
113 handler = handle->handler;
114 if (!handler->filter) {
115 if (filtered)
116 break;
117
118 handler->event(handle, type, code, value);
119
120 } else if (handler->filter(handle, type, code, value))
121 filtered = true;
122 }
123 }
124
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400125 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400126}
127
128/*
129 * Generate software autorepeat event. Note that we take
130 * dev->event_lock here to avoid racing with input_event
131 * which may cause keys get "stuck".
132 */
133static void input_repeat_key(unsigned long data)
134{
135 struct input_dev *dev = (void *) data;
136 unsigned long flags;
137
138 spin_lock_irqsave(&dev->event_lock, flags);
139
140 if (test_bit(dev->repeat_key, dev->key) &&
141 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
142
143 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
144
145 if (dev->sync) {
146 /*
147 * Only send SYN_REPORT if we are not in a middle
148 * of driver parsing a new hardware packet.
149 * Otherwise assume that the driver will send
150 * SYN_REPORT once it's done.
151 */
152 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
153 }
154
155 if (dev->rep[REP_PERIOD])
156 mod_timer(&dev->timer, jiffies +
157 msecs_to_jiffies(dev->rep[REP_PERIOD]));
158 }
159
160 spin_unlock_irqrestore(&dev->event_lock, flags);
161}
162
163static void input_start_autorepeat(struct input_dev *dev, int code)
164{
165 if (test_bit(EV_REP, dev->evbit) &&
166 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
167 dev->timer.data) {
168 dev->repeat_key = code;
169 mod_timer(&dev->timer,
170 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
171 }
172}
173
Johannes Berge7b5c1e2009-01-29 23:17:52 -0800174static void input_stop_autorepeat(struct input_dev *dev)
175{
176 del_timer(&dev->timer);
177}
178
Dmitry Torokhov80064792007-08-30 00:22:11 -0400179#define INPUT_IGNORE_EVENT 0
180#define INPUT_PASS_TO_HANDLERS 1
181#define INPUT_PASS_TO_DEVICE 2
182#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
183
184static void input_handle_event(struct input_dev *dev,
185 unsigned int type, unsigned int code, int value)
186{
187 int disposition = INPUT_IGNORE_EVENT;
188
189 switch (type) {
190
191 case EV_SYN:
192 switch (code) {
193 case SYN_CONFIG:
194 disposition = INPUT_PASS_TO_ALL;
195 break;
196
197 case SYN_REPORT:
198 if (!dev->sync) {
199 dev->sync = 1;
200 disposition = INPUT_PASS_TO_HANDLERS;
201 }
202 break;
Henrik Rydberg5e5ee682009-04-28 07:47:33 -0700203 case SYN_MT_REPORT:
204 dev->sync = 0;
205 disposition = INPUT_PASS_TO_HANDLERS;
206 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400207 }
208 break;
209
210 case EV_KEY:
211 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
212 !!test_bit(code, dev->key) != value) {
213
214 if (value != 2) {
215 __change_bit(code, dev->key);
216 if (value)
217 input_start_autorepeat(dev, code);
Johannes Berge7b5c1e2009-01-29 23:17:52 -0800218 else
219 input_stop_autorepeat(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400220 }
221
222 disposition = INPUT_PASS_TO_HANDLERS;
223 }
224 break;
225
226 case EV_SW:
227 if (is_event_supported(code, dev->swbit, SW_MAX) &&
228 !!test_bit(code, dev->sw) != value) {
229
230 __change_bit(code, dev->sw);
231 disposition = INPUT_PASS_TO_HANDLERS;
232 }
233 break;
234
235 case EV_ABS:
236 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
237
Henrik Rydberg61994a62009-04-28 07:45:31 -0700238 if (test_bit(code, input_abs_bypass)) {
239 disposition = INPUT_PASS_TO_HANDLERS;
240 break;
241 }
242
Dmitry Torokhov80064792007-08-30 00:22:11 -0400243 value = input_defuzz_abs_event(value,
244 dev->abs[code], dev->absfuzz[code]);
245
246 if (dev->abs[code] != value) {
247 dev->abs[code] = value;
248 disposition = INPUT_PASS_TO_HANDLERS;
249 }
250 }
251 break;
252
253 case EV_REL:
254 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
255 disposition = INPUT_PASS_TO_HANDLERS;
256
257 break;
258
259 case EV_MSC:
260 if (is_event_supported(code, dev->mscbit, MSC_MAX))
261 disposition = INPUT_PASS_TO_ALL;
262
263 break;
264
265 case EV_LED:
266 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
267 !!test_bit(code, dev->led) != value) {
268
269 __change_bit(code, dev->led);
270 disposition = INPUT_PASS_TO_ALL;
271 }
272 break;
273
274 case EV_SND:
275 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
276
277 if (!!test_bit(code, dev->snd) != !!value)
278 __change_bit(code, dev->snd);
279 disposition = INPUT_PASS_TO_ALL;
280 }
281 break;
282
283 case EV_REP:
284 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
285 dev->rep[code] = value;
286 disposition = INPUT_PASS_TO_ALL;
287 }
288 break;
289
290 case EV_FF:
291 if (value >= 0)
292 disposition = INPUT_PASS_TO_ALL;
293 break;
Richard Purdieed2fa4d2008-01-03 10:46:21 -0500294
295 case EV_PWR:
296 disposition = INPUT_PASS_TO_ALL;
297 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400298 }
299
Dmitry Torokhovc9812282008-06-02 01:02:40 -0400300 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400301 dev->sync = 0;
302
303 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
304 dev->event(dev, type, code, value);
305
306 if (disposition & INPUT_PASS_TO_HANDLERS)
307 input_pass_event(dev, type, code, value);
308}
309
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400310/**
311 * input_event() - report new input event
Dmitry Torokhov14471902006-11-02 23:26:55 -0500312 * @dev: device that generated the event
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400313 * @type: type of the event
314 * @code: event code
315 * @value: value of the event
316 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400317 * This function should be used by drivers implementing various input
Dmitry Torokhovdf2d4632009-12-11 21:57:31 -0800318 * devices to report input events. See also input_inject_event().
319 *
320 * NOTE: input_event() may be safely used right after input device was
321 * allocated with input_allocate_device(), even before it is registered
322 * with input_register_device(), but the event will not reach any of the
323 * input handlers. Such early invocation of input_event() may be used
324 * to 'seed' initial state of a switch or initial position of absolute
325 * axis, etc.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400326 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400327void input_event(struct input_dev *dev,
328 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400330 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dmitry Torokhov80064792007-08-30 00:22:11 -0400332 if (is_event_supported(type, dev->evbit, EV_MAX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Dmitry Torokhov80064792007-08-30 00:22:11 -0400334 spin_lock_irqsave(&dev->event_lock, flags);
335 add_input_randomness(type, code, value);
336 input_handle_event(dev, type, code, value);
337 spin_unlock_irqrestore(&dev->event_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400340EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400342/**
343 * input_inject_event() - send input event from input handler
344 * @handle: input handle to send event through
345 * @type: type of the event
346 * @code: event code
347 * @value: value of the event
348 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400349 * Similar to input_event() but will ignore event if device is
350 * "grabbed" and handle injecting event is not the one that owns
351 * the device.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400352 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400353void input_inject_event(struct input_handle *handle,
354 unsigned int type, unsigned int code, int value)
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400355{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400356 struct input_dev *dev = handle->dev;
357 struct input_handle *grab;
358 unsigned long flags;
359
360 if (is_event_supported(type, dev->evbit, EV_MAX)) {
361 spin_lock_irqsave(&dev->event_lock, flags);
362
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400363 rcu_read_lock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400364 grab = rcu_dereference(dev->grab);
365 if (!grab || grab == handle)
366 input_handle_event(dev, type, code, value);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400367 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400368
369 spin_unlock_irqrestore(&dev->event_lock, flags);
370 }
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400371}
372EXPORT_SYMBOL(input_inject_event);
373
Dmitry Torokhov80064792007-08-30 00:22:11 -0400374/**
375 * input_grab_device - grabs device for exclusive use
376 * @handle: input handle that wants to own the device
377 *
378 * When a device is grabbed by an input handle all events generated by
379 * the device are delivered only to this handle. Also events injected
380 * by other input handles are ignored while device is grabbed.
381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382int input_grab_device(struct input_handle *handle)
383{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400384 struct input_dev *dev = handle->dev;
385 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dmitry Torokhov80064792007-08-30 00:22:11 -0400387 retval = mutex_lock_interruptible(&dev->mutex);
388 if (retval)
389 return retval;
390
391 if (dev->grab) {
392 retval = -EBUSY;
393 goto out;
394 }
395
396 rcu_assign_pointer(dev->grab, handle);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400397 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400398
399 out:
400 mutex_unlock(&dev->mutex);
401 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400403EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Dmitry Torokhov80064792007-08-30 00:22:11 -0400405static void __input_release_device(struct input_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400407 struct input_dev *dev = handle->dev;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400408
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400409 if (dev->grab == handle) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400410 rcu_assign_pointer(dev->grab, NULL);
411 /* Make sure input_pass_event() notices that grab is gone */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400412 synchronize_rcu();
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400413
414 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400415 if (handle->open && handle->handler->start)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400416 handle->handler->start(handle);
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
Dmitry Torokhov80064792007-08-30 00:22:11 -0400419
420/**
421 * input_release_device - release previously grabbed device
422 * @handle: input handle that owns the device
423 *
424 * Releases previously grabbed device so that other input handles can
425 * start receiving input events. Upon release all handlers attached
426 * to the device have their start() method called so they have a change
427 * to synchronize device state with the rest of the system.
428 */
429void input_release_device(struct input_handle *handle)
430{
431 struct input_dev *dev = handle->dev;
432
433 mutex_lock(&dev->mutex);
434 __input_release_device(handle);
435 mutex_unlock(&dev->mutex);
436}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400437EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Dmitry Torokhov80064792007-08-30 00:22:11 -0400439/**
440 * input_open_device - open input device
441 * @handle: handle through which device is being accessed
442 *
443 * This function should be called by input handlers when they
444 * want to start receive events from given input device.
445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446int input_open_device(struct input_handle *handle)
447{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500448 struct input_dev *dev = handle->dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400449 int retval;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500450
Dmitry Torokhov80064792007-08-30 00:22:11 -0400451 retval = mutex_lock_interruptible(&dev->mutex);
452 if (retval)
453 return retval;
454
455 if (dev->going_away) {
456 retval = -ENODEV;
457 goto out;
458 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500461
462 if (!dev->users++ && dev->open)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400463 retval = dev->open(dev);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500464
Dmitry Torokhov80064792007-08-30 00:22:11 -0400465 if (retval) {
466 dev->users--;
467 if (!--handle->open) {
468 /*
469 * Make sure we are not delivering any more events
470 * through this handle
471 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400472 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400473 }
474 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500475
Dmitry Torokhov80064792007-08-30 00:22:11 -0400476 out:
Jes Sorensene676c232006-02-19 00:21:46 -0500477 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400478 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400480EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Dmitry Torokhov80064792007-08-30 00:22:11 -0400482int input_flush_device(struct input_handle *handle, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400484 struct input_dev *dev = handle->dev;
485 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Dmitry Torokhov80064792007-08-30 00:22:11 -0400487 retval = mutex_lock_interruptible(&dev->mutex);
488 if (retval)
489 return retval;
490
491 if (dev->flush)
492 retval = dev->flush(dev, file);
493
494 mutex_unlock(&dev->mutex);
495 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400497EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Dmitry Torokhov80064792007-08-30 00:22:11 -0400499/**
500 * input_close_device - close input device
501 * @handle: handle through which device is being accessed
502 *
503 * This function should be called by input handlers when they
504 * want to stop receive events from given input device.
505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506void input_close_device(struct input_handle *handle)
507{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500508 struct input_dev *dev = handle->dev;
509
Jes Sorensene676c232006-02-19 00:21:46 -0500510 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500511
Dmitry Torokhov80064792007-08-30 00:22:11 -0400512 __input_release_device(handle);
513
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500514 if (!--dev->users && dev->close)
515 dev->close(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400516
517 if (!--handle->open) {
518 /*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400519 * synchronize_rcu() makes sure that input_pass_event()
Dmitry Torokhov80064792007-08-30 00:22:11 -0400520 * completed and that no more input events are delivered
521 * through this handle
522 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400523 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400524 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500525
Jes Sorensene676c232006-02-19 00:21:46 -0500526 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400528EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Dmitry Torokhov80064792007-08-30 00:22:11 -0400530/*
531 * Prepare device for unregistering
532 */
533static void input_disconnect_device(struct input_dev *dev)
534{
535 struct input_handle *handle;
536 int code;
537
538 /*
539 * Mark device as going away. Note that we take dev->mutex here
540 * not to protect access to dev->going_away but rather to ensure
541 * that there are no threads in the middle of input_open_device()
542 */
543 mutex_lock(&dev->mutex);
Dmitry Torokhovffd0db92009-09-16 01:06:43 -0700544 dev->going_away = true;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400545 mutex_unlock(&dev->mutex);
546
547 spin_lock_irq(&dev->event_lock);
548
549 /*
550 * Simulate keyup events for all pressed keys so that handlers
551 * are not left with "stuck" keys. The driver may continue
552 * generate events even after we done here but they will not
553 * reach any handlers.
554 */
555 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
556 for (code = 0; code <= KEY_MAX; code++) {
557 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400558 __test_and_clear_bit(code, dev->key)) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400559 input_pass_event(dev, EV_KEY, code, 0);
560 }
561 }
562 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
563 }
564
565 list_for_each_entry(handle, &dev->h_list, d_node)
566 handle->open = 0;
567
568 spin_unlock_irq(&dev->event_lock);
569}
570
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400571static int input_fetch_keycode(struct input_dev *dev, int scancode)
572{
573 switch (dev->keycodesize) {
574 case 1:
575 return ((u8 *)dev->keycode)[scancode];
576
577 case 2:
578 return ((u16 *)dev->keycode)[scancode];
579
580 default:
581 return ((u32 *)dev->keycode)[scancode];
582 }
583}
584
585static int input_default_getkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800586 unsigned int scancode,
587 unsigned int *keycode)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400588{
589 if (!dev->keycodesize)
590 return -EINVAL;
591
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400592 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400593 return -EINVAL;
594
595 *keycode = input_fetch_keycode(dev, scancode);
596
597 return 0;
598}
599
600static int input_default_setkeycode(struct input_dev *dev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800601 unsigned int scancode,
602 unsigned int keycode)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400603{
604 int old_keycode;
605 int i;
606
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400607 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400608 return -EINVAL;
609
610 if (!dev->keycodesize)
611 return -EINVAL;
612
613 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
614 return -EINVAL;
615
616 switch (dev->keycodesize) {
617 case 1: {
618 u8 *k = (u8 *)dev->keycode;
619 old_keycode = k[scancode];
620 k[scancode] = keycode;
621 break;
622 }
623 case 2: {
624 u16 *k = (u16 *)dev->keycode;
625 old_keycode = k[scancode];
626 k[scancode] = keycode;
627 break;
628 }
629 default: {
630 u32 *k = (u32 *)dev->keycode;
631 old_keycode = k[scancode];
632 k[scancode] = keycode;
633 break;
634 }
635 }
636
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800637 __clear_bit(old_keycode, dev->keybit);
638 __set_bit(keycode, dev->keybit);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400639
640 for (i = 0; i < dev->keycodemax; i++) {
641 if (input_fetch_keycode(dev, i) == old_keycode) {
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800642 __set_bit(old_keycode, dev->keybit);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400643 break; /* Setting the bit twice is useless, so break */
644 }
645 }
646
647 return 0;
648}
649
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400650/**
651 * input_get_keycode - retrieve keycode currently mapped to a given scancode
652 * @dev: input device which keymap is being queried
653 * @scancode: scancode (or its equivalent for device in question) for which
654 * keycode is needed
655 * @keycode: result
656 *
657 * This function should be called by anyone interested in retrieving current
658 * keymap. Presently keyboard and evdev handlers use it.
659 */
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800660int input_get_keycode(struct input_dev *dev,
661 unsigned int scancode, unsigned int *keycode)
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400662{
Dmitry Torokhov2e2e3b92010-03-21 22:56:15 -0700663 unsigned long flags;
664 int retval;
665
666 spin_lock_irqsave(&dev->event_lock, flags);
667 retval = dev->getkeycode(dev, scancode, keycode);
668 spin_unlock_irqrestore(&dev->event_lock, flags);
669
670 return retval;
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400671}
672EXPORT_SYMBOL(input_get_keycode);
673
674/**
675 * input_get_keycode - assign new keycode to a given scancode
676 * @dev: input device which keymap is being updated
677 * @scancode: scancode (or its equivalent for device in question)
678 * @keycode: new keycode to be assigned to the scancode
679 *
680 * This function should be called by anyone needing to update current
681 * keymap. Presently keyboard and evdev handlers use it.
682 */
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800683int input_set_keycode(struct input_dev *dev,
684 unsigned int scancode, unsigned int keycode)
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400685{
686 unsigned long flags;
687 int old_keycode;
688 int retval;
689
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800690 if (keycode > KEY_MAX)
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400691 return -EINVAL;
692
693 spin_lock_irqsave(&dev->event_lock, flags);
694
695 retval = dev->getkeycode(dev, scancode, &old_keycode);
696 if (retval)
697 goto out;
698
699 retval = dev->setkeycode(dev, scancode, keycode);
700 if (retval)
701 goto out;
702
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800703 /* Make sure KEY_RESERVED did not get enabled. */
704 __clear_bit(KEY_RESERVED, dev->keybit);
705
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400706 /*
707 * Simulate keyup event if keycode is not present
708 * in the keymap anymore
709 */
710 if (test_bit(EV_KEY, dev->evbit) &&
711 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
712 __test_and_clear_bit(old_keycode, dev->key)) {
713
714 input_pass_event(dev, EV_KEY, old_keycode, 0);
715 if (dev->sync)
716 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
717 }
718
719 out:
720 spin_unlock_irqrestore(&dev->event_lock, flags);
721
722 return retval;
723}
724EXPORT_SYMBOL(input_set_keycode);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#define MATCH_BIT(bit, max) \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700727 for (i = 0; i < BITS_TO_LONGS(max); i++) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
729 break; \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700730 if (i != BITS_TO_LONGS(max)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 continue;
732
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800733static const struct input_device_id *input_match_device(struct input_handler *handler,
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400734 struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800736 const struct input_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int i;
738
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800739 for (id = handler->id_table; id->flags || id->driver_info; id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400742 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 continue;
744
745 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400746 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 continue;
748
749 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400750 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 continue;
752
753 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400754 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 continue;
756
757 MATCH_BIT(evbit, EV_MAX);
758 MATCH_BIT(keybit, KEY_MAX);
759 MATCH_BIT(relbit, REL_MAX);
760 MATCH_BIT(absbit, ABS_MAX);
761 MATCH_BIT(mscbit, MSC_MAX);
762 MATCH_BIT(ledbit, LED_MAX);
763 MATCH_BIT(sndbit, SND_MAX);
764 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500765 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800767 if (!handler->match || handler->match(handler, dev))
768 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
771 return NULL;
772}
773
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400774static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
775{
776 const struct input_device_id *id;
777 int error;
778
Dmitry Torokhov0b7024ac2010-02-02 21:08:26 -0800779 id = input_match_device(handler, dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400780 if (!id)
781 return -ENODEV;
782
783 error = handler->connect(handler, dev, id);
784 if (error && error != -ENODEV)
785 printk(KERN_ERR
786 "input: failed to attach handler %s to device %s, "
787 "error: %d\n",
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400788 handler->name, kobject_name(&dev->dev.kobj), error);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400789
790 return error;
791}
792
Dmitry Torokhov15e184a2010-01-11 00:05:43 -0800793#ifdef CONFIG_COMPAT
794
795static int input_bits_to_string(char *buf, int buf_size,
796 unsigned long bits, bool skip_empty)
797{
798 int len = 0;
799
800 if (INPUT_COMPAT_TEST) {
801 u32 dword = bits >> 32;
802 if (dword || !skip_empty)
803 len += snprintf(buf, buf_size, "%x ", dword);
804
805 dword = bits & 0xffffffffUL;
806 if (dword || !skip_empty || len)
807 len += snprintf(buf + len, max(buf_size - len, 0),
808 "%x", dword);
809 } else {
810 if (bits || !skip_empty)
811 len += snprintf(buf, buf_size, "%lx", bits);
812 }
813
814 return len;
815}
816
817#else /* !CONFIG_COMPAT */
818
819static int input_bits_to_string(char *buf, int buf_size,
820 unsigned long bits, bool skip_empty)
821{
822 return bits || !skip_empty ?
823 snprintf(buf, buf_size, "%lx", bits) : 0;
824}
825
826#endif
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500829
830static struct proc_dir_entry *proc_bus_input_dir;
831static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
832static int input_devices_state;
833
834static inline void input_wakeup_procfs_readers(void)
835{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 input_devices_state++;
837 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500840static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500842 poll_wait(file, &input_devices_poll_wait, wait);
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800843 if (file->f_version != input_devices_state) {
844 file->f_version = input_devices_state;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500845 return POLLIN | POLLRDNORM;
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800846 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400847
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700851union input_seq_state {
852 struct {
853 unsigned short pos;
854 bool mutex_acquired;
855 };
856 void *p;
857};
858
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500859static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
860{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700861 union input_seq_state *state = (union input_seq_state *)&seq->private;
862 int error;
863
864 /* We need to fit into seq->private pointer */
865 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
866
867 error = mutex_lock_interruptible(&input_mutex);
868 if (error) {
869 state->mutex_acquired = false;
870 return ERR_PTR(error);
871 }
872
873 state->mutex_acquired = true;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500874
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400875 return seq_list_start(&input_dev_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500876}
877
878static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
879{
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400880 return seq_list_next(v, &input_dev_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500881}
882
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700883static void input_seq_stop(struct seq_file *seq, void *v)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500884{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700885 union input_seq_state *state = (union input_seq_state *)&seq->private;
886
887 if (state->mutex_acquired)
888 mutex_unlock(&input_mutex);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500889}
890
891static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
892 unsigned long *bitmap, int max)
893{
894 int i;
Dmitry Torokhov15e184a2010-01-11 00:05:43 -0800895 bool skip_empty = true;
896 char buf[18];
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500897
898 seq_printf(seq, "B: %s=", name);
Dmitry Torokhov15e184a2010-01-11 00:05:43 -0800899
900 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
901 if (input_bits_to_string(buf, sizeof(buf),
902 bitmap[i], skip_empty)) {
903 skip_empty = false;
904 seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
905 }
906 }
907
908 /*
909 * If no output was produced print a single 0.
910 */
911 if (skip_empty)
912 seq_puts(seq, "0");
913
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500914 seq_putc(seq, '\n');
915}
916
917static int input_devices_seq_show(struct seq_file *seq, void *v)
918{
919 struct input_dev *dev = container_of(v, struct input_dev, node);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400920 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct input_handle *handle;
922
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500923 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
924 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500926 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
927 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
928 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
Dmitry Torokhov15e03ae2007-03-07 23:20:17 -0500929 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500930 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500932 list_for_each_entry(handle, &dev->h_list, d_node)
933 seq_printf(seq, "%s ", handle->name);
934 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500935
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500936 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
937 if (test_bit(EV_KEY, dev->evbit))
938 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
939 if (test_bit(EV_REL, dev->evbit))
940 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
941 if (test_bit(EV_ABS, dev->evbit))
942 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
943 if (test_bit(EV_MSC, dev->evbit))
944 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
945 if (test_bit(EV_LED, dev->evbit))
946 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
947 if (test_bit(EV_SND, dev->evbit))
948 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
949 if (test_bit(EV_FF, dev->evbit))
950 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
951 if (test_bit(EV_SW, dev->evbit))
952 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500954 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500956 kfree(path);
957 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Jan Engelhardtcec69c32008-01-31 00:43:32 -0500960static const struct seq_operations input_devices_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500961 .start = input_devices_seq_start,
962 .next = input_devices_seq_next,
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700963 .stop = input_seq_stop,
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500964 .show = input_devices_seq_show,
965};
966
967static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500969 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800972static const struct file_operations input_devices_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500973 .owner = THIS_MODULE,
974 .open = input_proc_devices_open,
975 .poll = input_proc_devices_poll,
976 .read = seq_read,
977 .llseek = seq_lseek,
978 .release = seq_release,
979};
980
981static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
982{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700983 union input_seq_state *state = (union input_seq_state *)&seq->private;
984 int error;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400985
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700986 /* We need to fit into seq->private pointer */
987 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
988
989 error = mutex_lock_interruptible(&input_mutex);
990 if (error) {
991 state->mutex_acquired = false;
992 return ERR_PTR(error);
993 }
994
995 state->mutex_acquired = true;
996 state->pos = *pos;
997
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400998 return seq_list_start(&input_handler_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500999}
1000
1001static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1002{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001003 union input_seq_state *state = (union input_seq_state *)&seq->private;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001004
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001005 state->pos = *pos + 1;
1006 return seq_list_next(v, &input_handler_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001007}
1008
1009static int input_handlers_seq_show(struct seq_file *seq, void *v)
1010{
1011 struct input_handler *handler = container_of(v, struct input_handler, node);
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001012 union input_seq_state *state = (union input_seq_state *)&seq->private;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001013
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001014 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
Dmitry Torokhovef7995f2010-01-29 23:59:12 -08001015 if (handler->filter)
1016 seq_puts(seq, " (filter)");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001017 if (handler->fops)
1018 seq_printf(seq, " Minor=%d", handler->minor);
1019 seq_putc(seq, '\n');
1020
1021 return 0;
1022}
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001023
Jan Engelhardtcec69c32008-01-31 00:43:32 -05001024static const struct seq_operations input_handlers_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001025 .start = input_handlers_seq_start,
1026 .next = input_handlers_seq_next,
Dmitry Torokhov1572ca22009-10-13 23:37:30 -07001027 .stop = input_seq_stop,
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001028 .show = input_handlers_seq_show,
1029};
1030
1031static int input_proc_handlers_open(struct inode *inode, struct file *file)
1032{
1033 return seq_open(file, &input_handlers_seq_ops);
1034}
1035
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001036static const struct file_operations input_handlers_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001037 .owner = THIS_MODULE,
1038 .open = input_proc_handlers_open,
1039 .read = seq_read,
1040 .llseek = seq_lseek,
1041 .release = seq_release,
1042};
Luke Kosewskie334016fc2005-06-01 02:39:28 -05001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static int __init input_proc_init(void)
1045{
1046 struct proc_dir_entry *entry;
1047
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001048 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001049 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001051
Denis V. Lunevc7705f32008-04-29 01:02:35 -07001052 entry = proc_create("devices", 0, proc_bus_input_dir,
1053 &input_devices_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001054 if (!entry)
1055 goto fail1;
1056
Denis V. Lunevc7705f32008-04-29 01:02:35 -07001057 entry = proc_create("handlers", 0, proc_bus_input_dir,
1058 &input_handlers_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001059 if (!entry)
1060 goto fail2;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001063
1064 fail2: remove_proc_entry("devices", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001065 fail1: remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001066 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001068
Andrew Mortonbeffbdc2005-07-01 23:54:30 -05001069static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001070{
1071 remove_proc_entry("devices", proc_bus_input_dir);
1072 remove_proc_entry("handlers", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001073 remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001077static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001079static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080#endif
1081
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001082#define INPUT_DEV_STRING_ATTR_SHOW(name) \
1083static ssize_t input_dev_show_##name(struct device *dev, \
1084 struct device_attribute *attr, \
1085 char *buf) \
1086{ \
1087 struct input_dev *input_dev = to_input_dev(dev); \
1088 \
1089 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1090 input_dev->name ? input_dev->name : ""); \
1091} \
1092static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001093
1094INPUT_DEV_STRING_ATTR_SHOW(name);
1095INPUT_DEV_STRING_ATTR_SHOW(phys);
1096INPUT_DEV_STRING_ATTR_SHOW(uniq);
1097
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001098static int input_print_modalias_bits(char *buf, int size,
1099 char name, unsigned long *bm,
1100 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001101{
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001102 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001103
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001104 len += snprintf(buf, max(size, 0), "%c", name);
1105 for (i = min_bit; i < max_bit; i++)
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001106 if (bm[BIT_WORD(i)] & BIT_MASK(i))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001107 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001108 return len;
1109}
1110
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001111static int input_print_modalias(char *buf, int size, struct input_dev *id,
1112 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001113{
1114 int len;
1115
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001116 len = snprintf(buf, max(size, 0),
1117 "input:b%04Xv%04Xp%04Xe%04X-",
1118 id->id.bustype, id->id.vendor,
1119 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001120
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001121 len += input_print_modalias_bits(buf + len, size - len,
1122 'e', id->evbit, 0, EV_MAX);
1123 len += input_print_modalias_bits(buf + len, size - len,
1124 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1125 len += input_print_modalias_bits(buf + len, size - len,
1126 'r', id->relbit, 0, REL_MAX);
1127 len += input_print_modalias_bits(buf + len, size - len,
1128 'a', id->absbit, 0, ABS_MAX);
1129 len += input_print_modalias_bits(buf + len, size - len,
1130 'm', id->mscbit, 0, MSC_MAX);
1131 len += input_print_modalias_bits(buf + len, size - len,
1132 'l', id->ledbit, 0, LED_MAX);
1133 len += input_print_modalias_bits(buf + len, size - len,
1134 's', id->sndbit, 0, SND_MAX);
1135 len += input_print_modalias_bits(buf + len, size - len,
1136 'f', id->ffbit, 0, FF_MAX);
1137 len += input_print_modalias_bits(buf + len, size - len,
1138 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001139
1140 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001141 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001142
Rusty Russell1d8f4302005-12-07 21:40:34 +01001143 return len;
1144}
1145
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001146static ssize_t input_dev_show_modalias(struct device *dev,
1147 struct device_attribute *attr,
1148 char *buf)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001149{
1150 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001151 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001152
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001153 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1154
Richard Purdie8a3cf452006-06-26 01:48:21 -04001155 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001156}
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001157static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001158
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001159static struct attribute *input_dev_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001160 &dev_attr_name.attr,
1161 &dev_attr_phys.attr,
1162 &dev_attr_uniq.attr,
1163 &dev_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001164 NULL
1165};
1166
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001167static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001168 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001169};
1170
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001171#define INPUT_DEV_ID_ATTR(name) \
1172static ssize_t input_dev_show_id_##name(struct device *dev, \
1173 struct device_attribute *attr, \
1174 char *buf) \
1175{ \
1176 struct input_dev *input_dev = to_input_dev(dev); \
1177 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1178} \
1179static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001180
1181INPUT_DEV_ID_ATTR(bustype);
1182INPUT_DEV_ID_ATTR(vendor);
1183INPUT_DEV_ID_ATTR(product);
1184INPUT_DEV_ID_ATTR(version);
1185
1186static struct attribute *input_dev_id_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001187 &dev_attr_bustype.attr,
1188 &dev_attr_vendor.attr,
1189 &dev_attr_product.attr,
1190 &dev_attr_version.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001191 NULL
1192};
1193
1194static struct attribute_group input_dev_id_attr_group = {
1195 .name = "id",
1196 .attrs = input_dev_id_attrs,
1197};
1198
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001199static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1200 int max, int add_cr)
1201{
1202 int i;
1203 int len = 0;
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001204 bool skip_empty = true;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001205
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001206 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1207 len += input_bits_to_string(buf + len, max(buf_size - len, 0),
1208 bitmap[i], skip_empty);
1209 if (len) {
1210 skip_empty = false;
1211 if (i > 0)
1212 len += snprintf(buf + len, max(buf_size - len, 0), " ");
1213 }
1214 }
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001215
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001216 /*
1217 * If no output was produced print a single 0.
1218 */
1219 if (len == 0)
1220 len = snprintf(buf, buf_size, "%d", 0);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001221
1222 if (add_cr)
1223 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1224
1225 return len;
1226}
1227
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001228#define INPUT_DEV_CAP_ATTR(ev, bm) \
1229static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1230 struct device_attribute *attr, \
1231 char *buf) \
1232{ \
1233 struct input_dev *input_dev = to_input_dev(dev); \
1234 int len = input_print_bitmap(buf, PAGE_SIZE, \
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001235 input_dev->bm##bit, ev##_MAX, \
1236 true); \
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001237 return min_t(int, len, PAGE_SIZE); \
1238} \
1239static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001240
1241INPUT_DEV_CAP_ATTR(EV, ev);
1242INPUT_DEV_CAP_ATTR(KEY, key);
1243INPUT_DEV_CAP_ATTR(REL, rel);
1244INPUT_DEV_CAP_ATTR(ABS, abs);
1245INPUT_DEV_CAP_ATTR(MSC, msc);
1246INPUT_DEV_CAP_ATTR(LED, led);
1247INPUT_DEV_CAP_ATTR(SND, snd);
1248INPUT_DEV_CAP_ATTR(FF, ff);
1249INPUT_DEV_CAP_ATTR(SW, sw);
1250
1251static struct attribute *input_dev_caps_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001252 &dev_attr_ev.attr,
1253 &dev_attr_key.attr,
1254 &dev_attr_rel.attr,
1255 &dev_attr_abs.attr,
1256 &dev_attr_msc.attr,
1257 &dev_attr_led.attr,
1258 &dev_attr_snd.attr,
1259 &dev_attr_ff.attr,
1260 &dev_attr_sw.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001261 NULL
1262};
1263
1264static struct attribute_group input_dev_caps_attr_group = {
1265 .name = "capabilities",
1266 .attrs = input_dev_caps_attrs,
1267};
1268
David Brownella4dbd672009-06-24 10:06:31 -07001269static const struct attribute_group *input_dev_attr_groups[] = {
Dmitry Torokhovcb9def42007-03-07 23:20:26 -05001270 &input_dev_attr_group,
1271 &input_dev_id_attr_group,
1272 &input_dev_caps_attr_group,
1273 NULL
1274};
1275
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001276static void input_dev_release(struct device *device)
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001277{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001278 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001279
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001280 input_ff_destroy(dev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001281 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001282
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001283 module_put(THIS_MODULE);
1284}
1285
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001286/*
Kay Sievers312c0042005-11-16 09:00:00 +01001287 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001288 * device bitfields.
1289 */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001290static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001291 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001292{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001293 int len;
1294
1295 if (add_uevent_var(env, "%s=", name))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001296 return -ENOMEM;
1297
Kay Sievers7eff2e72007-08-14 15:15:12 +02001298 len = input_print_bitmap(&env->buf[env->buflen - 1],
1299 sizeof(env->buf) - env->buflen,
Dmitry Torokhov15e184a2010-01-11 00:05:43 -08001300 bitmap, max, false);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001301 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001302 return -ENOMEM;
1303
Kay Sievers7eff2e72007-08-14 15:15:12 +02001304 env->buflen += len;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001305 return 0;
1306}
1307
Kay Sievers7eff2e72007-08-14 15:15:12 +02001308static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001309 struct input_dev *dev)
1310{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001311 int len;
1312
1313 if (add_uevent_var(env, "MODALIAS="))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001314 return -ENOMEM;
1315
Kay Sievers7eff2e72007-08-14 15:15:12 +02001316 len = input_print_modalias(&env->buf[env->buflen - 1],
1317 sizeof(env->buf) - env->buflen,
1318 dev, 0);
1319 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001320 return -ENOMEM;
1321
Kay Sievers7eff2e72007-08-14 15:15:12 +02001322 env->buflen += len;
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001323 return 0;
1324}
1325
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001326#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1327 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001328 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001329 if (err) \
1330 return err; \
1331 } while (0)
1332
1333#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1334 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001335 int err = input_add_uevent_bm_var(env, name, bm, max); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001336 if (err) \
1337 return err; \
1338 } while (0)
1339
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001340#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1341 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001342 int err = input_add_uevent_modalias_var(env, dev); \
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001343 if (err) \
1344 return err; \
1345 } while (0)
1346
Kay Sievers7eff2e72007-08-14 15:15:12 +02001347static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001348{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001349 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001350
1351 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1352 dev->id.bustype, dev->id.vendor,
1353 dev->id.product, dev->id.version);
1354 if (dev->name)
1355 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1356 if (dev->phys)
1357 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -08001358 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001359 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1360
1361 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1362 if (test_bit(EV_KEY, dev->evbit))
1363 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1364 if (test_bit(EV_REL, dev->evbit))
1365 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1366 if (test_bit(EV_ABS, dev->evbit))
1367 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1368 if (test_bit(EV_MSC, dev->evbit))
1369 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1370 if (test_bit(EV_LED, dev->evbit))
1371 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1372 if (test_bit(EV_SND, dev->evbit))
1373 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1374 if (test_bit(EV_FF, dev->evbit))
1375 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1376 if (test_bit(EV_SW, dev->evbit))
1377 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1378
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001379 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001380
1381 return 0;
1382}
1383
Dmitry Torokhov3cc96352009-11-12 23:19:05 -08001384#define INPUT_DO_TOGGLE(dev, type, bits, on) \
1385 do { \
1386 int i; \
1387 bool active; \
1388 \
1389 if (!test_bit(EV_##type, dev->evbit)) \
1390 break; \
1391 \
1392 for (i = 0; i < type##_MAX; i++) { \
1393 if (!test_bit(i, dev->bits##bit)) \
1394 continue; \
1395 \
1396 active = test_bit(i, dev->bits); \
1397 if (!active && !on) \
1398 continue; \
1399 \
1400 dev->event(dev, EV_##type, i, on ? active : 0); \
1401 } \
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001402 } while (0)
1403
Andrew Morton1c4115e2009-10-01 15:43:55 -07001404#ifdef CONFIG_PM
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001405static void input_dev_reset(struct input_dev *dev, bool activate)
1406{
1407 if (!dev->event)
1408 return;
1409
1410 INPUT_DO_TOGGLE(dev, LED, led, activate);
1411 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1412
1413 if (activate && test_bit(EV_REP, dev->evbit)) {
1414 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1415 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1416 }
1417}
1418
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001419static int input_dev_suspend(struct device *dev)
1420{
1421 struct input_dev *input_dev = to_input_dev(dev);
1422
1423 mutex_lock(&input_dev->mutex);
1424 input_dev_reset(input_dev, false);
1425 mutex_unlock(&input_dev->mutex);
1426
1427 return 0;
1428}
1429
1430static int input_dev_resume(struct device *dev)
1431{
1432 struct input_dev *input_dev = to_input_dev(dev);
1433
1434 mutex_lock(&input_dev->mutex);
1435 input_dev_reset(input_dev, true);
1436 mutex_unlock(&input_dev->mutex);
1437
1438 return 0;
1439}
1440
1441static const struct dev_pm_ops input_dev_pm_ops = {
1442 .suspend = input_dev_suspend,
1443 .resume = input_dev_resume,
1444 .poweroff = input_dev_suspend,
1445 .restore = input_dev_resume,
1446};
1447#endif /* CONFIG_PM */
1448
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001449static struct device_type input_dev_type = {
1450 .groups = input_dev_attr_groups,
1451 .release = input_dev_release,
1452 .uevent = input_dev_uevent,
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001453#ifdef CONFIG_PM
1454 .pm = &input_dev_pm_ops,
1455#endif
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001456};
1457
Kay Sieverse454cea2009-09-18 23:01:12 +02001458static char *input_devnode(struct device *dev, mode_t *mode)
Kay Sieversaa5ed632009-04-30 15:23:42 +02001459{
1460 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1461}
1462
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001463struct class input_class = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001464 .name = "input",
Kay Sieverse454cea2009-09-18 23:01:12 +02001465 .devnode = input_devnode,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001466};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001467EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001468
Dmitry Torokhov14471902006-11-02 23:26:55 -05001469/**
1470 * input_allocate_device - allocate memory for new input device
1471 *
1472 * Returns prepared struct input_dev or NULL.
1473 *
1474 * NOTE: Use input_free_device() to free devices that have not been
1475 * registered; input_unregister_device() should be used for already
1476 * registered devices.
1477 */
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001478struct input_dev *input_allocate_device(void)
1479{
1480 struct input_dev *dev;
1481
1482 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1483 if (dev) {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001484 dev->dev.type = &input_dev_type;
1485 dev->dev.class = &input_class;
1486 device_initialize(&dev->dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001487 mutex_init(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001488 spin_lock_init(&dev->event_lock);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001489 INIT_LIST_HEAD(&dev->h_list);
1490 INIT_LIST_HEAD(&dev->node);
Dmitry Torokhov655816e2006-09-14 01:32:14 -04001491
1492 __module_get(THIS_MODULE);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001493 }
1494
1495 return dev;
1496}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001497EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001498
Dmitry Torokhov14471902006-11-02 23:26:55 -05001499/**
1500 * input_free_device - free memory occupied by input_dev structure
1501 * @dev: input device to free
1502 *
1503 * This function should only be used if input_register_device()
1504 * was not called yet or if it failed. Once device was registered
1505 * use input_unregister_device() and memory will be freed once last
Dmitry Torokhov80064792007-08-30 00:22:11 -04001506 * reference to the device is dropped.
Dmitry Torokhov14471902006-11-02 23:26:55 -05001507 *
1508 * Device should be allocated by input_allocate_device().
1509 *
1510 * NOTE: If there are references to the input device then memory
1511 * will not be freed until last reference is dropped.
1512 */
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001513void input_free_device(struct input_dev *dev)
1514{
Dmitry Torokhov54f9e362007-03-16 00:57:25 -04001515 if (dev)
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001516 input_put_device(dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001517}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001518EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001519
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001520/**
1521 * input_set_capability - mark device as capable of a certain event
1522 * @dev: device that is capable of emitting or accepting event
1523 * @type: type of the event (EV_KEY, EV_REL, etc...)
1524 * @code: event code
1525 *
1526 * In addition to setting up corresponding bit in appropriate capability
1527 * bitmap the function also adjusts dev->evbit.
1528 */
1529void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1530{
1531 switch (type) {
1532 case EV_KEY:
1533 __set_bit(code, dev->keybit);
1534 break;
1535
1536 case EV_REL:
1537 __set_bit(code, dev->relbit);
1538 break;
1539
1540 case EV_ABS:
1541 __set_bit(code, dev->absbit);
1542 break;
1543
1544 case EV_MSC:
1545 __set_bit(code, dev->mscbit);
1546 break;
1547
1548 case EV_SW:
1549 __set_bit(code, dev->swbit);
1550 break;
1551
1552 case EV_LED:
1553 __set_bit(code, dev->ledbit);
1554 break;
1555
1556 case EV_SND:
1557 __set_bit(code, dev->sndbit);
1558 break;
1559
1560 case EV_FF:
1561 __set_bit(code, dev->ffbit);
1562 break;
1563
Dmitry Baryshkov22d1c392007-12-14 01:21:03 -05001564 case EV_PWR:
1565 /* do nothing */
1566 break;
1567
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001568 default:
1569 printk(KERN_ERR
1570 "input_set_capability: unknown type %u (code %u)\n",
1571 type, code);
1572 dump_stack();
1573 return;
1574 }
1575
1576 __set_bit(type, dev->evbit);
1577}
1578EXPORT_SYMBOL(input_set_capability);
1579
Dmitry Torokhov92a3a582010-01-05 17:56:01 -08001580#define INPUT_CLEANSE_BITMASK(dev, type, bits) \
1581 do { \
1582 if (!test_bit(EV_##type, dev->evbit)) \
1583 memset(dev->bits##bit, 0, \
1584 sizeof(dev->bits##bit)); \
1585 } while (0)
1586
1587static void input_cleanse_bitmasks(struct input_dev *dev)
1588{
1589 INPUT_CLEANSE_BITMASK(dev, KEY, key);
1590 INPUT_CLEANSE_BITMASK(dev, REL, rel);
1591 INPUT_CLEANSE_BITMASK(dev, ABS, abs);
1592 INPUT_CLEANSE_BITMASK(dev, MSC, msc);
1593 INPUT_CLEANSE_BITMASK(dev, LED, led);
1594 INPUT_CLEANSE_BITMASK(dev, SND, snd);
1595 INPUT_CLEANSE_BITMASK(dev, FF, ff);
1596 INPUT_CLEANSE_BITMASK(dev, SW, sw);
1597}
1598
Dmitry Torokhov80064792007-08-30 00:22:11 -04001599/**
1600 * input_register_device - register device with input core
1601 * @dev: device to be registered
1602 *
1603 * This function registers device with input core. The device must be
1604 * allocated with input_allocate_device() and all it's capabilities
1605 * set up before registering.
1606 * If function fails the device must be freed with input_free_device().
1607 * Once device has been successfully registered it can be unregistered
1608 * with input_unregister_device(); input_free_device() should not be
1609 * called in this case.
1610 */
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001611int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001612{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001613 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001614 struct input_handler *handler;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001615 const char *path;
1616 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001617
Dmitry Torokhov4f93df42010-01-05 17:56:00 -08001618 /* Every input device generates EV_SYN/SYN_REPORT events. */
Dmitry Torokhov80064792007-08-30 00:22:11 -04001619 __set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001620
Dmitry Torokhov4f93df42010-01-05 17:56:00 -08001621 /* KEY_RESERVED is not supposed to be transmitted to userspace. */
1622 __clear_bit(KEY_RESERVED, dev->keybit);
1623
Dmitry Torokhov92a3a582010-01-05 17:56:01 -08001624 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
1625 input_cleanse_bitmasks(dev);
1626
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001627 /*
1628 * If delay and period are pre-set by the driver, then autorepeating
1629 * is handled by the driver itself and we don't do it in input.c.
1630 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001631 init_timer(&dev->timer);
1632 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1633 dev->timer.data = (long) dev;
1634 dev->timer.function = input_repeat_key;
1635 dev->rep[REP_DELAY] = 250;
1636 dev->rep[REP_PERIOD] = 33;
1637 }
1638
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -04001639 if (!dev->getkeycode)
1640 dev->getkeycode = input_default_getkeycode;
1641
1642 if (!dev->setkeycode)
1643 dev->setkeycode = input_default_setkeycode;
1644
Kay Sieversa6c24902008-10-30 00:07:50 -04001645 dev_set_name(&dev->dev, "input%ld",
1646 (unsigned long) atomic_inc_return(&input_no) - 1);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001647
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001648 error = device_add(&dev->dev);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001649 if (error)
1650 return error;
1651
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001652 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001653 printk(KERN_INFO "input: %s as %s\n",
1654 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1655 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -07001656
Dmitry Torokhov80064792007-08-30 00:22:11 -04001657 error = mutex_lock_interruptible(&input_mutex);
1658 if (error) {
1659 device_del(&dev->dev);
1660 return error;
1661 }
1662
1663 list_add_tail(&dev->node, &input_dev_list);
1664
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001665 list_for_each_entry(handler, &input_handler_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001666 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001667
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001668 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001669
Dmitry Torokhov80064792007-08-30 00:22:11 -04001670 mutex_unlock(&input_mutex);
1671
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001672 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001673}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001674EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001675
Dmitry Torokhov80064792007-08-30 00:22:11 -04001676/**
1677 * input_unregister_device - unregister previously registered device
1678 * @dev: device to be unregistered
1679 *
1680 * This function unregisters an input device. Once device is unregistered
1681 * the caller should not try to access it as it may get freed at any moment.
1682 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001683void input_unregister_device(struct input_dev *dev)
1684{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001685 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001686
Dmitry Torokhov80064792007-08-30 00:22:11 -04001687 input_disconnect_device(dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001688
Dmitry Torokhov80064792007-08-30 00:22:11 -04001689 mutex_lock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001690
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001691 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001692 handle->handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001693 WARN_ON(!list_empty(&dev->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001694
Dmitry Torokhov80064792007-08-30 00:22:11 -04001695 del_timer_sync(&dev->timer);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001696 list_del_init(&dev->node);
1697
1698 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001699
1700 mutex_unlock(&input_mutex);
1701
1702 device_unregister(&dev->dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001703}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001704EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001705
Dmitry Torokhov80064792007-08-30 00:22:11 -04001706/**
1707 * input_register_handler - register a new input handler
1708 * @handler: handler to be registered
1709 *
1710 * This function registers a new input handler (interface) for input
1711 * devices in the system and attaches it to all input devices that
1712 * are compatible with the handler.
1713 */
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001714int input_register_handler(struct input_handler *handler)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001715{
1716 struct input_dev *dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001717 int retval;
1718
1719 retval = mutex_lock_interruptible(&input_mutex);
1720 if (retval)
1721 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001722
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001723 INIT_LIST_HEAD(&handler->h_list);
1724
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001725 if (handler->fops != NULL) {
Dmitry Torokhov80064792007-08-30 00:22:11 -04001726 if (input_table[handler->minor >> 5]) {
1727 retval = -EBUSY;
1728 goto out;
1729 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001730 input_table[handler->minor >> 5] = handler;
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001731 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001732
1733 list_add_tail(&handler->node, &input_handler_list);
1734
1735 list_for_each_entry(dev, &input_dev_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001736 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001737
1738 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001739
1740 out:
1741 mutex_unlock(&input_mutex);
1742 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001743}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001744EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001745
Dmitry Torokhov80064792007-08-30 00:22:11 -04001746/**
1747 * input_unregister_handler - unregisters an input handler
1748 * @handler: handler to be unregistered
1749 *
1750 * This function disconnects a handler from its input devices and
1751 * removes it from lists of known handlers.
1752 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001753void input_unregister_handler(struct input_handler *handler)
1754{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001755 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001756
Dmitry Torokhov80064792007-08-30 00:22:11 -04001757 mutex_lock(&input_mutex);
1758
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001759 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001760 handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001761 WARN_ON(!list_empty(&handler->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001762
1763 list_del_init(&handler->node);
1764
1765 if (handler->fops != NULL)
1766 input_table[handler->minor >> 5] = NULL;
1767
1768 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001769
1770 mutex_unlock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001771}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001772EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001773
Dmitry Torokhov80064792007-08-30 00:22:11 -04001774/**
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001775 * input_handler_for_each_handle - handle iterator
1776 * @handler: input handler to iterate
1777 * @data: data for the callback
1778 * @fn: function to be called for each handle
1779 *
1780 * Iterate over @bus's list of devices, and call @fn for each, passing
1781 * it @data and stop when @fn returns a non-zero value. The function is
1782 * using RCU to traverse the list and therefore may be usind in atonic
1783 * contexts. The @fn callback is invoked from RCU critical section and
1784 * thus must not sleep.
1785 */
1786int input_handler_for_each_handle(struct input_handler *handler, void *data,
1787 int (*fn)(struct input_handle *, void *))
1788{
1789 struct input_handle *handle;
1790 int retval = 0;
1791
1792 rcu_read_lock();
1793
1794 list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
1795 retval = fn(handle, data);
1796 if (retval)
1797 break;
1798 }
1799
1800 rcu_read_unlock();
1801
1802 return retval;
1803}
1804EXPORT_SYMBOL(input_handler_for_each_handle);
1805
1806/**
Dmitry Torokhov80064792007-08-30 00:22:11 -04001807 * input_register_handle - register a new input handle
1808 * @handle: handle to register
1809 *
1810 * This function puts a new input handle onto device's
1811 * and handler's lists so that events can flow through
1812 * it once it is opened using input_open_device().
1813 *
1814 * This function is supposed to be called from handler's
1815 * connect() method.
1816 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001817int input_register_handle(struct input_handle *handle)
1818{
1819 struct input_handler *handler = handle->handler;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001820 struct input_dev *dev = handle->dev;
1821 int error;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001822
Dmitry Torokhov80064792007-08-30 00:22:11 -04001823 /*
1824 * We take dev->mutex here to prevent race with
1825 * input_release_device().
1826 */
1827 error = mutex_lock_interruptible(&dev->mutex);
1828 if (error)
1829 return error;
Dmitry Torokhovef7995f2010-01-29 23:59:12 -08001830
1831 /*
1832 * Filters go to the head of the list, normal handlers
1833 * to the tail.
1834 */
1835 if (handler->filter)
1836 list_add_rcu(&handle->d_node, &dev->h_list);
1837 else
1838 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1839
Dmitry Torokhov80064792007-08-30 00:22:11 -04001840 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001841
1842 /*
1843 * Since we are supposed to be called from ->connect()
1844 * which is mutually exclusive with ->disconnect()
1845 * we can't be racing with input_unregister_handle()
1846 * and so separate lock is not needed here.
1847 */
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001848 list_add_tail_rcu(&handle->h_node, &handler->h_list);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001849
1850 if (handler->start)
1851 handler->start(handle);
1852
1853 return 0;
1854}
1855EXPORT_SYMBOL(input_register_handle);
1856
Dmitry Torokhov80064792007-08-30 00:22:11 -04001857/**
1858 * input_unregister_handle - unregister an input handle
1859 * @handle: handle to unregister
1860 *
1861 * This function removes input handle from device's
1862 * and handler's lists.
1863 *
1864 * This function is supposed to be called from handler's
1865 * disconnect() method.
1866 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001867void input_unregister_handle(struct input_handle *handle)
1868{
Dmitry Torokhov80064792007-08-30 00:22:11 -04001869 struct input_dev *dev = handle->dev;
1870
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001871 list_del_rcu(&handle->h_node);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001872
1873 /*
1874 * Take dev->mutex to prevent race with input_release_device().
1875 */
1876 mutex_lock(&dev->mutex);
1877 list_del_rcu(&handle->d_node);
1878 mutex_unlock(&dev->mutex);
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001879
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -04001880 synchronize_rcu();
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001881}
1882EXPORT_SYMBOL(input_unregister_handle);
1883
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001884static int input_open_file(struct inode *inode, struct file *file)
1885{
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001886 struct input_handler *handler;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001887 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001888 int err;
1889
Arnd Bergmann2f2177c2010-03-09 20:38:48 -08001890 err = mutex_lock_interruptible(&input_mutex);
1891 if (err)
1892 return err;
1893
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001894 /* No load-on-demand here? */
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001895 handler = input_table[iminor(inode) >> 5];
Arnd Bergmann2f2177c2010-03-09 20:38:48 -08001896 if (handler)
1897 new_fops = fops_get(handler->fops);
1898
1899 mutex_unlock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001900
1901 /*
1902 * That's _really_ odd. Usually NULL ->open means "nothing special",
1903 * not "no device". Oh, well...
1904 */
Arnd Bergmann2f2177c2010-03-09 20:38:48 -08001905 if (!new_fops || !new_fops->open) {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001906 fops_put(new_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001907 err = -ENODEV;
1908 goto out;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001909 }
Arnd Bergmann2f2177c2010-03-09 20:38:48 -08001910
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001911 old_fops = file->f_op;
1912 file->f_op = new_fops;
1913
1914 err = new_fops->open(inode, file);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001915 if (err) {
1916 fops_put(file->f_op);
1917 file->f_op = fops_get(old_fops);
1918 }
1919 fops_put(old_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001920out:
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001921 return err;
1922}
1923
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001924static const struct file_operations input_fops = {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001925 .owner = THIS_MODULE,
1926 .open = input_open_file,
1927};
1928
Henrik Rydberg61994a62009-04-28 07:45:31 -07001929static void __init input_init_abs_bypass(void)
1930{
1931 const unsigned int *p;
1932
1933 for (p = input_abs_bypass_init_data; *p; p++)
1934 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1935}
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937static int __init input_init(void)
1938{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001939 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Henrik Rydberg61994a62009-04-28 07:45:31 -07001941 input_init_abs_bypass();
1942
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001943 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001944 if (err) {
1945 printk(KERN_ERR "input: unable to register input_dev class\n");
1946 return err;
1947 }
1948
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001949 err = input_proc_init();
1950 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001951 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001952
1953 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1954 if (err) {
1955 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001956 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001958
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001959 return 0;
1960
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001961 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001962 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
1966static void __exit input_exit(void)
1967{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001968 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001970 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973subsys_initcall(input_init);
1974module_exit(input_exit);