blob: c6f88ebb40c766e21c17104cdbbe12563ed075a7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
29MODULE_DESCRIPTION("Input core");
30MODULE_LICENSE("GPL");
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define INPUT_DEVICES 256
33
Henrik Rydberg61994a62009-04-28 07:45:31 -070034/*
35 * EV_ABS events which should not be cached are listed here.
36 */
37static unsigned int input_abs_bypass_init_data[] __initdata = {
Henrik Rydberg5e5ee682009-04-28 07:47:33 -070038 ABS_MT_TOUCH_MAJOR,
39 ABS_MT_TOUCH_MINOR,
40 ABS_MT_WIDTH_MAJOR,
41 ABS_MT_WIDTH_MINOR,
42 ABS_MT_ORIENTATION,
43 ABS_MT_POSITION_X,
44 ABS_MT_POSITION_Y,
45 ABS_MT_TOOL_TYPE,
46 ABS_MT_BLOB_ID,
Henrik Rydbergdf391e02009-05-23 09:51:20 -070047 ABS_MT_TRACKING_ID,
Henrik Rydberg61994a62009-04-28 07:45:31 -070048 0
49};
50static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static LIST_HEAD(input_dev_list);
53static LIST_HEAD(input_handler_list);
54
Dmitry Torokhov80064792007-08-30 00:22:11 -040055/*
56 * input_mutex protects access to both input_dev_list and input_handler_list.
57 * This also causes input_[un]register_device and input_[un]register_handler
58 * be mutually exclusive which simplifies locking in drivers implementing
59 * input handlers.
60 */
61static DEFINE_MUTEX(input_mutex);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static struct input_handler *input_table[8];
64
Dmitry Torokhov80064792007-08-30 00:22:11 -040065static inline int is_event_supported(unsigned int code,
66 unsigned long *bm, unsigned int max)
67{
68 return code <= max && test_bit(code, bm);
69}
70
71static int input_defuzz_abs_event(int value, int old_val, int fuzz)
72{
73 if (fuzz) {
74 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
75 return old_val;
76
77 if (value > old_val - fuzz && value < old_val + fuzz)
78 return (old_val * 3 + value) / 4;
79
80 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
81 return (old_val + value) / 2;
82 }
83
84 return value;
85}
86
87/*
88 * Pass event through all open handles. This function is called with
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040089 * dev->event_lock held and interrupts disabled.
Dmitry Torokhov80064792007-08-30 00:22:11 -040090 */
91static void input_pass_event(struct input_dev *dev,
92 unsigned int type, unsigned int code, int value)
93{
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040094 struct input_handle *handle;
Dmitry Torokhov80064792007-08-30 00:22:11 -040095
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040096 rcu_read_lock();
97
98 handle = rcu_dereference(dev->grab);
Dmitry Torokhov80064792007-08-30 00:22:11 -040099 if (handle)
100 handle->handler->event(handle, type, code, value);
101 else
102 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
103 if (handle->open)
104 handle->handler->event(handle,
105 type, code, value);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400106 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400107}
108
109/*
110 * Generate software autorepeat event. Note that we take
111 * dev->event_lock here to avoid racing with input_event
112 * which may cause keys get "stuck".
113 */
114static void input_repeat_key(unsigned long data)
115{
116 struct input_dev *dev = (void *) data;
117 unsigned long flags;
118
119 spin_lock_irqsave(&dev->event_lock, flags);
120
121 if (test_bit(dev->repeat_key, dev->key) &&
122 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
123
124 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
125
126 if (dev->sync) {
127 /*
128 * Only send SYN_REPORT if we are not in a middle
129 * of driver parsing a new hardware packet.
130 * Otherwise assume that the driver will send
131 * SYN_REPORT once it's done.
132 */
133 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
134 }
135
136 if (dev->rep[REP_PERIOD])
137 mod_timer(&dev->timer, jiffies +
138 msecs_to_jiffies(dev->rep[REP_PERIOD]));
139 }
140
141 spin_unlock_irqrestore(&dev->event_lock, flags);
142}
143
144static void input_start_autorepeat(struct input_dev *dev, int code)
145{
146 if (test_bit(EV_REP, dev->evbit) &&
147 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
148 dev->timer.data) {
149 dev->repeat_key = code;
150 mod_timer(&dev->timer,
151 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
152 }
153}
154
Johannes Berge7b5c1e2009-01-29 23:17:52 -0800155static void input_stop_autorepeat(struct input_dev *dev)
156{
157 del_timer(&dev->timer);
158}
159
Dmitry Torokhov80064792007-08-30 00:22:11 -0400160#define INPUT_IGNORE_EVENT 0
161#define INPUT_PASS_TO_HANDLERS 1
162#define INPUT_PASS_TO_DEVICE 2
163#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
164
165static void input_handle_event(struct input_dev *dev,
166 unsigned int type, unsigned int code, int value)
167{
168 int disposition = INPUT_IGNORE_EVENT;
169
170 switch (type) {
171
172 case EV_SYN:
173 switch (code) {
174 case SYN_CONFIG:
175 disposition = INPUT_PASS_TO_ALL;
176 break;
177
178 case SYN_REPORT:
179 if (!dev->sync) {
180 dev->sync = 1;
181 disposition = INPUT_PASS_TO_HANDLERS;
182 }
183 break;
Henrik Rydberg5e5ee682009-04-28 07:47:33 -0700184 case SYN_MT_REPORT:
185 dev->sync = 0;
186 disposition = INPUT_PASS_TO_HANDLERS;
187 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400188 }
189 break;
190
191 case EV_KEY:
192 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
193 !!test_bit(code, dev->key) != value) {
194
195 if (value != 2) {
196 __change_bit(code, dev->key);
197 if (value)
198 input_start_autorepeat(dev, code);
Johannes Berge7b5c1e2009-01-29 23:17:52 -0800199 else
200 input_stop_autorepeat(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400201 }
202
203 disposition = INPUT_PASS_TO_HANDLERS;
204 }
205 break;
206
207 case EV_SW:
208 if (is_event_supported(code, dev->swbit, SW_MAX) &&
209 !!test_bit(code, dev->sw) != value) {
210
211 __change_bit(code, dev->sw);
212 disposition = INPUT_PASS_TO_HANDLERS;
213 }
214 break;
215
216 case EV_ABS:
217 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
218
Henrik Rydberg61994a62009-04-28 07:45:31 -0700219 if (test_bit(code, input_abs_bypass)) {
220 disposition = INPUT_PASS_TO_HANDLERS;
221 break;
222 }
223
Dmitry Torokhov80064792007-08-30 00:22:11 -0400224 value = input_defuzz_abs_event(value,
225 dev->abs[code], dev->absfuzz[code]);
226
227 if (dev->abs[code] != value) {
228 dev->abs[code] = value;
229 disposition = INPUT_PASS_TO_HANDLERS;
230 }
231 }
232 break;
233
234 case EV_REL:
235 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
236 disposition = INPUT_PASS_TO_HANDLERS;
237
238 break;
239
240 case EV_MSC:
241 if (is_event_supported(code, dev->mscbit, MSC_MAX))
242 disposition = INPUT_PASS_TO_ALL;
243
244 break;
245
246 case EV_LED:
247 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
248 !!test_bit(code, dev->led) != value) {
249
250 __change_bit(code, dev->led);
251 disposition = INPUT_PASS_TO_ALL;
252 }
253 break;
254
255 case EV_SND:
256 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
257
258 if (!!test_bit(code, dev->snd) != !!value)
259 __change_bit(code, dev->snd);
260 disposition = INPUT_PASS_TO_ALL;
261 }
262 break;
263
264 case EV_REP:
265 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
266 dev->rep[code] = value;
267 disposition = INPUT_PASS_TO_ALL;
268 }
269 break;
270
271 case EV_FF:
272 if (value >= 0)
273 disposition = INPUT_PASS_TO_ALL;
274 break;
Richard Purdieed2fa4d2008-01-03 10:46:21 -0500275
276 case EV_PWR:
277 disposition = INPUT_PASS_TO_ALL;
278 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400279 }
280
Dmitry Torokhovc9812282008-06-02 01:02:40 -0400281 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400282 dev->sync = 0;
283
284 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
285 dev->event(dev, type, code, value);
286
287 if (disposition & INPUT_PASS_TO_HANDLERS)
288 input_pass_event(dev, type, code, value);
289}
290
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400291/**
292 * input_event() - report new input event
Dmitry Torokhov14471902006-11-02 23:26:55 -0500293 * @dev: device that generated the event
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400294 * @type: type of the event
295 * @code: event code
296 * @value: value of the event
297 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400298 * This function should be used by drivers implementing various input
299 * devices. See also input_inject_event().
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400300 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400301
302void input_event(struct input_dev *dev,
303 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400305 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Dmitry Torokhov80064792007-08-30 00:22:11 -0400307 if (is_event_supported(type, dev->evbit, EV_MAX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Dmitry Torokhov80064792007-08-30 00:22:11 -0400309 spin_lock_irqsave(&dev->event_lock, flags);
310 add_input_randomness(type, code, value);
311 input_handle_event(dev, type, code, value);
312 spin_unlock_irqrestore(&dev->event_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400315EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400317/**
318 * input_inject_event() - send input event from input handler
319 * @handle: input handle to send event through
320 * @type: type of the event
321 * @code: event code
322 * @value: value of the event
323 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400324 * Similar to input_event() but will ignore event if device is
325 * "grabbed" and handle injecting event is not the one that owns
326 * the device.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400327 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400328void input_inject_event(struct input_handle *handle,
329 unsigned int type, unsigned int code, int value)
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400330{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400331 struct input_dev *dev = handle->dev;
332 struct input_handle *grab;
333 unsigned long flags;
334
335 if (is_event_supported(type, dev->evbit, EV_MAX)) {
336 spin_lock_irqsave(&dev->event_lock, flags);
337
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400338 rcu_read_lock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400339 grab = rcu_dereference(dev->grab);
340 if (!grab || grab == handle)
341 input_handle_event(dev, type, code, value);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400342 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400343
344 spin_unlock_irqrestore(&dev->event_lock, flags);
345 }
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400346}
347EXPORT_SYMBOL(input_inject_event);
348
Dmitry Torokhov80064792007-08-30 00:22:11 -0400349/**
350 * input_grab_device - grabs device for exclusive use
351 * @handle: input handle that wants to own the device
352 *
353 * When a device is grabbed by an input handle all events generated by
354 * the device are delivered only to this handle. Also events injected
355 * by other input handles are ignored while device is grabbed.
356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357int input_grab_device(struct input_handle *handle)
358{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400359 struct input_dev *dev = handle->dev;
360 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Dmitry Torokhov80064792007-08-30 00:22:11 -0400362 retval = mutex_lock_interruptible(&dev->mutex);
363 if (retval)
364 return retval;
365
366 if (dev->grab) {
367 retval = -EBUSY;
368 goto out;
369 }
370
371 rcu_assign_pointer(dev->grab, handle);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400372 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400373
374 out:
375 mutex_unlock(&dev->mutex);
376 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400378EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dmitry Torokhov80064792007-08-30 00:22:11 -0400380static void __input_release_device(struct input_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400382 struct input_dev *dev = handle->dev;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400383
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400384 if (dev->grab == handle) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400385 rcu_assign_pointer(dev->grab, NULL);
386 /* Make sure input_pass_event() notices that grab is gone */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400387 synchronize_rcu();
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400388
389 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400390 if (handle->open && handle->handler->start)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400391 handle->handler->start(handle);
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
Dmitry Torokhov80064792007-08-30 00:22:11 -0400394
395/**
396 * input_release_device - release previously grabbed device
397 * @handle: input handle that owns the device
398 *
399 * Releases previously grabbed device so that other input handles can
400 * start receiving input events. Upon release all handlers attached
401 * to the device have their start() method called so they have a change
402 * to synchronize device state with the rest of the system.
403 */
404void input_release_device(struct input_handle *handle)
405{
406 struct input_dev *dev = handle->dev;
407
408 mutex_lock(&dev->mutex);
409 __input_release_device(handle);
410 mutex_unlock(&dev->mutex);
411}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400412EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Dmitry Torokhov80064792007-08-30 00:22:11 -0400414/**
415 * input_open_device - open input device
416 * @handle: handle through which device is being accessed
417 *
418 * This function should be called by input handlers when they
419 * want to start receive events from given input device.
420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421int input_open_device(struct input_handle *handle)
422{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500423 struct input_dev *dev = handle->dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400424 int retval;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500425
Dmitry Torokhov80064792007-08-30 00:22:11 -0400426 retval = mutex_lock_interruptible(&dev->mutex);
427 if (retval)
428 return retval;
429
430 if (dev->going_away) {
431 retval = -ENODEV;
432 goto out;
433 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500436
437 if (!dev->users++ && dev->open)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400438 retval = dev->open(dev);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500439
Dmitry Torokhov80064792007-08-30 00:22:11 -0400440 if (retval) {
441 dev->users--;
442 if (!--handle->open) {
443 /*
444 * Make sure we are not delivering any more events
445 * through this handle
446 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400447 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400448 }
449 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500450
Dmitry Torokhov80064792007-08-30 00:22:11 -0400451 out:
Jes Sorensene676c232006-02-19 00:21:46 -0500452 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400453 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400455EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Dmitry Torokhov80064792007-08-30 00:22:11 -0400457int input_flush_device(struct input_handle *handle, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400459 struct input_dev *dev = handle->dev;
460 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Dmitry Torokhov80064792007-08-30 00:22:11 -0400462 retval = mutex_lock_interruptible(&dev->mutex);
463 if (retval)
464 return retval;
465
466 if (dev->flush)
467 retval = dev->flush(dev, file);
468
469 mutex_unlock(&dev->mutex);
470 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400472EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Dmitry Torokhov80064792007-08-30 00:22:11 -0400474/**
475 * input_close_device - close input device
476 * @handle: handle through which device is being accessed
477 *
478 * This function should be called by input handlers when they
479 * want to stop receive events from given input device.
480 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481void input_close_device(struct input_handle *handle)
482{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500483 struct input_dev *dev = handle->dev;
484
Jes Sorensene676c232006-02-19 00:21:46 -0500485 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500486
Dmitry Torokhov80064792007-08-30 00:22:11 -0400487 __input_release_device(handle);
488
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500489 if (!--dev->users && dev->close)
490 dev->close(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400491
492 if (!--handle->open) {
493 /*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400494 * synchronize_rcu() makes sure that input_pass_event()
Dmitry Torokhov80064792007-08-30 00:22:11 -0400495 * completed and that no more input events are delivered
496 * through this handle
497 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400498 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400499 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500500
Jes Sorensene676c232006-02-19 00:21:46 -0500501 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400503EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Dmitry Torokhov80064792007-08-30 00:22:11 -0400505/*
506 * Prepare device for unregistering
507 */
508static void input_disconnect_device(struct input_dev *dev)
509{
510 struct input_handle *handle;
511 int code;
512
513 /*
514 * Mark device as going away. Note that we take dev->mutex here
515 * not to protect access to dev->going_away but rather to ensure
516 * that there are no threads in the middle of input_open_device()
517 */
518 mutex_lock(&dev->mutex);
Dmitry Torokhovffd0db92009-09-16 01:06:43 -0700519 dev->going_away = true;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400520 mutex_unlock(&dev->mutex);
521
522 spin_lock_irq(&dev->event_lock);
523
524 /*
525 * Simulate keyup events for all pressed keys so that handlers
526 * are not left with "stuck" keys. The driver may continue
527 * generate events even after we done here but they will not
528 * reach any handlers.
529 */
530 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
531 for (code = 0; code <= KEY_MAX; code++) {
532 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400533 __test_and_clear_bit(code, dev->key)) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400534 input_pass_event(dev, EV_KEY, code, 0);
535 }
536 }
537 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
538 }
539
540 list_for_each_entry(handle, &dev->h_list, d_node)
541 handle->open = 0;
542
543 spin_unlock_irq(&dev->event_lock);
544}
545
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400546static int input_fetch_keycode(struct input_dev *dev, int scancode)
547{
548 switch (dev->keycodesize) {
549 case 1:
550 return ((u8 *)dev->keycode)[scancode];
551
552 case 2:
553 return ((u16 *)dev->keycode)[scancode];
554
555 default:
556 return ((u32 *)dev->keycode)[scancode];
557 }
558}
559
560static int input_default_getkeycode(struct input_dev *dev,
561 int scancode, int *keycode)
562{
563 if (!dev->keycodesize)
564 return -EINVAL;
565
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400566 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400567 return -EINVAL;
568
569 *keycode = input_fetch_keycode(dev, scancode);
570
571 return 0;
572}
573
574static int input_default_setkeycode(struct input_dev *dev,
575 int scancode, int keycode)
576{
577 int old_keycode;
578 int i;
579
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400580 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400581 return -EINVAL;
582
583 if (!dev->keycodesize)
584 return -EINVAL;
585
586 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
587 return -EINVAL;
588
589 switch (dev->keycodesize) {
590 case 1: {
591 u8 *k = (u8 *)dev->keycode;
592 old_keycode = k[scancode];
593 k[scancode] = keycode;
594 break;
595 }
596 case 2: {
597 u16 *k = (u16 *)dev->keycode;
598 old_keycode = k[scancode];
599 k[scancode] = keycode;
600 break;
601 }
602 default: {
603 u32 *k = (u32 *)dev->keycode;
604 old_keycode = k[scancode];
605 k[scancode] = keycode;
606 break;
607 }
608 }
609
610 clear_bit(old_keycode, dev->keybit);
611 set_bit(keycode, dev->keybit);
612
613 for (i = 0; i < dev->keycodemax; i++) {
614 if (input_fetch_keycode(dev, i) == old_keycode) {
615 set_bit(old_keycode, dev->keybit);
616 break; /* Setting the bit twice is useless, so break */
617 }
618 }
619
620 return 0;
621}
622
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400623/**
624 * input_get_keycode - retrieve keycode currently mapped to a given scancode
625 * @dev: input device which keymap is being queried
626 * @scancode: scancode (or its equivalent for device in question) for which
627 * keycode is needed
628 * @keycode: result
629 *
630 * This function should be called by anyone interested in retrieving current
631 * keymap. Presently keyboard and evdev handlers use it.
632 */
633int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
634{
635 if (scancode < 0)
636 return -EINVAL;
637
638 return dev->getkeycode(dev, scancode, keycode);
639}
640EXPORT_SYMBOL(input_get_keycode);
641
642/**
643 * input_get_keycode - assign new keycode to a given scancode
644 * @dev: input device which keymap is being updated
645 * @scancode: scancode (or its equivalent for device in question)
646 * @keycode: new keycode to be assigned to the scancode
647 *
648 * This function should be called by anyone needing to update current
649 * keymap. Presently keyboard and evdev handlers use it.
650 */
651int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
652{
653 unsigned long flags;
654 int old_keycode;
655 int retval;
656
657 if (scancode < 0)
658 return -EINVAL;
659
660 if (keycode < 0 || keycode > KEY_MAX)
661 return -EINVAL;
662
663 spin_lock_irqsave(&dev->event_lock, flags);
664
665 retval = dev->getkeycode(dev, scancode, &old_keycode);
666 if (retval)
667 goto out;
668
669 retval = dev->setkeycode(dev, scancode, keycode);
670 if (retval)
671 goto out;
672
673 /*
674 * Simulate keyup event if keycode is not present
675 * in the keymap anymore
676 */
677 if (test_bit(EV_KEY, dev->evbit) &&
678 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
679 __test_and_clear_bit(old_keycode, dev->key)) {
680
681 input_pass_event(dev, EV_KEY, old_keycode, 0);
682 if (dev->sync)
683 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
684 }
685
686 out:
687 spin_unlock_irqrestore(&dev->event_lock, flags);
688
689 return retval;
690}
691EXPORT_SYMBOL(input_set_keycode);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693#define MATCH_BIT(bit, max) \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700694 for (i = 0; i < BITS_TO_LONGS(max); i++) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
696 break; \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700697 if (i != BITS_TO_LONGS(max)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 continue;
699
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400700static const struct input_device_id *input_match_device(const struct input_device_id *id,
701 struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 int i;
704
705 for (; id->flags || id->driver_info; id++) {
706
707 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400708 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 continue;
710
711 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400712 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 continue;
714
715 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400716 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 continue;
718
719 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400720 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 continue;
722
723 MATCH_BIT(evbit, EV_MAX);
724 MATCH_BIT(keybit, KEY_MAX);
725 MATCH_BIT(relbit, REL_MAX);
726 MATCH_BIT(absbit, ABS_MAX);
727 MATCH_BIT(mscbit, MSC_MAX);
728 MATCH_BIT(ledbit, LED_MAX);
729 MATCH_BIT(sndbit, SND_MAX);
730 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500731 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 return id;
734 }
735
736 return NULL;
737}
738
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400739static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
740{
741 const struct input_device_id *id;
742 int error;
743
744 if (handler->blacklist && input_match_device(handler->blacklist, dev))
745 return -ENODEV;
746
747 id = input_match_device(handler->id_table, dev);
748 if (!id)
749 return -ENODEV;
750
751 error = handler->connect(handler, dev, id);
752 if (error && error != -ENODEV)
753 printk(KERN_ERR
754 "input: failed to attach handler %s to device %s, "
755 "error: %d\n",
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400756 handler->name, kobject_name(&dev->dev.kobj), error);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -0400757
758 return error;
759}
760
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500763
764static struct proc_dir_entry *proc_bus_input_dir;
765static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
766static int input_devices_state;
767
768static inline void input_wakeup_procfs_readers(void)
769{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 input_devices_state++;
771 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500774static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500776 poll_wait(file, &input_devices_poll_wait, wait);
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800777 if (file->f_version != input_devices_state) {
778 file->f_version = input_devices_state;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500779 return POLLIN | POLLRDNORM;
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800780 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400781
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500785static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
786{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400787 if (mutex_lock_interruptible(&input_mutex))
788 return NULL;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500789
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400790 return seq_list_start(&input_dev_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500791}
792
793static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
794{
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400795 return seq_list_next(v, &input_dev_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500796}
797
798static void input_devices_seq_stop(struct seq_file *seq, void *v)
799{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400800 mutex_unlock(&input_mutex);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500801}
802
803static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
804 unsigned long *bitmap, int max)
805{
806 int i;
807
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700808 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500809 if (bitmap[i])
810 break;
811
812 seq_printf(seq, "B: %s=", name);
813 for (; i >= 0; i--)
814 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
815 seq_putc(seq, '\n');
816}
817
818static int input_devices_seq_show(struct seq_file *seq, void *v)
819{
820 struct input_dev *dev = container_of(v, struct input_dev, node);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400821 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct input_handle *handle;
823
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500824 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
825 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500827 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
828 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
829 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
Dmitry Torokhov15e03ae2007-03-07 23:20:17 -0500830 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500831 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500833 list_for_each_entry(handle, &dev->h_list, d_node)
834 seq_printf(seq, "%s ", handle->name);
835 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500836
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500837 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
838 if (test_bit(EV_KEY, dev->evbit))
839 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
840 if (test_bit(EV_REL, dev->evbit))
841 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
842 if (test_bit(EV_ABS, dev->evbit))
843 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
844 if (test_bit(EV_MSC, dev->evbit))
845 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
846 if (test_bit(EV_LED, dev->evbit))
847 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
848 if (test_bit(EV_SND, dev->evbit))
849 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
850 if (test_bit(EV_FF, dev->evbit))
851 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
852 if (test_bit(EV_SW, dev->evbit))
853 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500855 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500857 kfree(path);
858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Jan Engelhardtcec69c32008-01-31 00:43:32 -0500861static const struct seq_operations input_devices_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500862 .start = input_devices_seq_start,
863 .next = input_devices_seq_next,
864 .stop = input_devices_seq_stop,
865 .show = input_devices_seq_show,
866};
867
868static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500870 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800873static const struct file_operations input_devices_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500874 .owner = THIS_MODULE,
875 .open = input_proc_devices_open,
876 .poll = input_proc_devices_poll,
877 .read = seq_read,
878 .llseek = seq_lseek,
879 .release = seq_release,
880};
881
882static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
883{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400884 if (mutex_lock_interruptible(&input_mutex))
885 return NULL;
886
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500887 seq->private = (void *)(unsigned long)*pos;
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400888 return seq_list_start(&input_handler_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500889}
890
891static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
892{
893 seq->private = (void *)(unsigned long)(*pos + 1);
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400894 return seq_list_next(v, &input_handler_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500895}
896
897static void input_handlers_seq_stop(struct seq_file *seq, void *v)
898{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400899 mutex_unlock(&input_mutex);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500900}
901
902static int input_handlers_seq_show(struct seq_file *seq, void *v)
903{
904 struct input_handler *handler = container_of(v, struct input_handler, node);
905
906 seq_printf(seq, "N: Number=%ld Name=%s",
907 (unsigned long)seq->private, handler->name);
908 if (handler->fops)
909 seq_printf(seq, " Minor=%d", handler->minor);
910 seq_putc(seq, '\n');
911
912 return 0;
913}
Jan Engelhardtcec69c32008-01-31 00:43:32 -0500914static const struct seq_operations input_handlers_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500915 .start = input_handlers_seq_start,
916 .next = input_handlers_seq_next,
917 .stop = input_handlers_seq_stop,
918 .show = input_handlers_seq_show,
919};
920
921static int input_proc_handlers_open(struct inode *inode, struct file *file)
922{
923 return seq_open(file, &input_handlers_seq_ops);
924}
925
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800926static const struct file_operations input_handlers_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500927 .owner = THIS_MODULE,
928 .open = input_proc_handlers_open,
929 .read = seq_read,
930 .llseek = seq_lseek,
931 .release = seq_release,
932};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static int __init input_proc_init(void)
935{
936 struct proc_dir_entry *entry;
937
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700938 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500939 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500941
Denis V. Lunevc7705f3442008-04-29 01:02:35 -0700942 entry = proc_create("devices", 0, proc_bus_input_dir,
943 &input_devices_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500944 if (!entry)
945 goto fail1;
946
Denis V. Lunevc7705f3442008-04-29 01:02:35 -0700947 entry = proc_create("handlers", 0, proc_bus_input_dir,
948 &input_handlers_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500949 if (!entry)
950 goto fail2;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500953
954 fail2: remove_proc_entry("devices", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700955 fail1: remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500956 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500958
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500959static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500960{
961 remove_proc_entry("devices", proc_bus_input_dir);
962 remove_proc_entry("handlers", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700963 remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500967static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500969static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970#endif
971
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400972#define INPUT_DEV_STRING_ATTR_SHOW(name) \
973static ssize_t input_dev_show_##name(struct device *dev, \
974 struct device_attribute *attr, \
975 char *buf) \
976{ \
977 struct input_dev *input_dev = to_input_dev(dev); \
978 \
979 return scnprintf(buf, PAGE_SIZE, "%s\n", \
980 input_dev->name ? input_dev->name : ""); \
981} \
982static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500983
984INPUT_DEV_STRING_ATTR_SHOW(name);
985INPUT_DEV_STRING_ATTR_SHOW(phys);
986INPUT_DEV_STRING_ATTR_SHOW(uniq);
987
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500988static int input_print_modalias_bits(char *buf, int size,
989 char name, unsigned long *bm,
990 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100991{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500992 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100993
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500994 len += snprintf(buf, max(size, 0), "%c", name);
995 for (i = min_bit; i < max_bit; i++)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700996 if (bm[BIT_WORD(i)] & BIT_MASK(i))
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500997 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100998 return len;
999}
1000
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001001static int input_print_modalias(char *buf, int size, struct input_dev *id,
1002 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001003{
1004 int len;
1005
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001006 len = snprintf(buf, max(size, 0),
1007 "input:b%04Xv%04Xp%04Xe%04X-",
1008 id->id.bustype, id->id.vendor,
1009 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001010
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001011 len += input_print_modalias_bits(buf + len, size - len,
1012 'e', id->evbit, 0, EV_MAX);
1013 len += input_print_modalias_bits(buf + len, size - len,
1014 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1015 len += input_print_modalias_bits(buf + len, size - len,
1016 'r', id->relbit, 0, REL_MAX);
1017 len += input_print_modalias_bits(buf + len, size - len,
1018 'a', id->absbit, 0, ABS_MAX);
1019 len += input_print_modalias_bits(buf + len, size - len,
1020 'm', id->mscbit, 0, MSC_MAX);
1021 len += input_print_modalias_bits(buf + len, size - len,
1022 'l', id->ledbit, 0, LED_MAX);
1023 len += input_print_modalias_bits(buf + len, size - len,
1024 's', id->sndbit, 0, SND_MAX);
1025 len += input_print_modalias_bits(buf + len, size - len,
1026 'f', id->ffbit, 0, FF_MAX);
1027 len += input_print_modalias_bits(buf + len, size - len,
1028 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001029
1030 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001031 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001032
Rusty Russell1d8f4302005-12-07 21:40:34 +01001033 return len;
1034}
1035
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001036static ssize_t input_dev_show_modalias(struct device *dev,
1037 struct device_attribute *attr,
1038 char *buf)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001039{
1040 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001041 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001042
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001043 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1044
Richard Purdie8a3cf452006-06-26 01:48:21 -04001045 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001046}
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001047static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001048
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001049static struct attribute *input_dev_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001050 &dev_attr_name.attr,
1051 &dev_attr_phys.attr,
1052 &dev_attr_uniq.attr,
1053 &dev_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001054 NULL
1055};
1056
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001057static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001058 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001059};
1060
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001061#define INPUT_DEV_ID_ATTR(name) \
1062static ssize_t input_dev_show_id_##name(struct device *dev, \
1063 struct device_attribute *attr, \
1064 char *buf) \
1065{ \
1066 struct input_dev *input_dev = to_input_dev(dev); \
1067 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1068} \
1069static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001070
1071INPUT_DEV_ID_ATTR(bustype);
1072INPUT_DEV_ID_ATTR(vendor);
1073INPUT_DEV_ID_ATTR(product);
1074INPUT_DEV_ID_ATTR(version);
1075
1076static struct attribute *input_dev_id_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001077 &dev_attr_bustype.attr,
1078 &dev_attr_vendor.attr,
1079 &dev_attr_product.attr,
1080 &dev_attr_version.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001081 NULL
1082};
1083
1084static struct attribute_group input_dev_id_attr_group = {
1085 .name = "id",
1086 .attrs = input_dev_id_attrs,
1087};
1088
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001089static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1090 int max, int add_cr)
1091{
1092 int i;
1093 int len = 0;
1094
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001095 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001096 if (bitmap[i])
1097 break;
1098
1099 for (; i >= 0; i--)
1100 len += snprintf(buf + len, max(buf_size - len, 0),
1101 "%lx%s", bitmap[i], i > 0 ? " " : "");
1102
1103 if (add_cr)
1104 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1105
1106 return len;
1107}
1108
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001109#define INPUT_DEV_CAP_ATTR(ev, bm) \
1110static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1111 struct device_attribute *attr, \
1112 char *buf) \
1113{ \
1114 struct input_dev *input_dev = to_input_dev(dev); \
1115 int len = input_print_bitmap(buf, PAGE_SIZE, \
1116 input_dev->bm##bit, ev##_MAX, 1); \
1117 return min_t(int, len, PAGE_SIZE); \
1118} \
1119static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001120
1121INPUT_DEV_CAP_ATTR(EV, ev);
1122INPUT_DEV_CAP_ATTR(KEY, key);
1123INPUT_DEV_CAP_ATTR(REL, rel);
1124INPUT_DEV_CAP_ATTR(ABS, abs);
1125INPUT_DEV_CAP_ATTR(MSC, msc);
1126INPUT_DEV_CAP_ATTR(LED, led);
1127INPUT_DEV_CAP_ATTR(SND, snd);
1128INPUT_DEV_CAP_ATTR(FF, ff);
1129INPUT_DEV_CAP_ATTR(SW, sw);
1130
1131static struct attribute *input_dev_caps_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001132 &dev_attr_ev.attr,
1133 &dev_attr_key.attr,
1134 &dev_attr_rel.attr,
1135 &dev_attr_abs.attr,
1136 &dev_attr_msc.attr,
1137 &dev_attr_led.attr,
1138 &dev_attr_snd.attr,
1139 &dev_attr_ff.attr,
1140 &dev_attr_sw.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001141 NULL
1142};
1143
1144static struct attribute_group input_dev_caps_attr_group = {
1145 .name = "capabilities",
1146 .attrs = input_dev_caps_attrs,
1147};
1148
David Brownella4dbd672009-06-24 10:06:31 -07001149static const struct attribute_group *input_dev_attr_groups[] = {
Dmitry Torokhovcb9def42007-03-07 23:20:26 -05001150 &input_dev_attr_group,
1151 &input_dev_id_attr_group,
1152 &input_dev_caps_attr_group,
1153 NULL
1154};
1155
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001156static void input_dev_release(struct device *device)
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001157{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001158 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001159
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001160 input_ff_destroy(dev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001161 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001162
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001163 module_put(THIS_MODULE);
1164}
1165
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001166/*
Kay Sievers312c0042005-11-16 09:00:00 +01001167 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001168 * device bitfields.
1169 */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001170static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001171 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001172{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001173 int len;
1174
1175 if (add_uevent_var(env, "%s=", name))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001176 return -ENOMEM;
1177
Kay Sievers7eff2e72007-08-14 15:15:12 +02001178 len = input_print_bitmap(&env->buf[env->buflen - 1],
1179 sizeof(env->buf) - env->buflen,
1180 bitmap, max, 0);
1181 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001182 return -ENOMEM;
1183
Kay Sievers7eff2e72007-08-14 15:15:12 +02001184 env->buflen += len;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001185 return 0;
1186}
1187
Kay Sievers7eff2e72007-08-14 15:15:12 +02001188static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001189 struct input_dev *dev)
1190{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001191 int len;
1192
1193 if (add_uevent_var(env, "MODALIAS="))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001194 return -ENOMEM;
1195
Kay Sievers7eff2e72007-08-14 15:15:12 +02001196 len = input_print_modalias(&env->buf[env->buflen - 1],
1197 sizeof(env->buf) - env->buflen,
1198 dev, 0);
1199 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001200 return -ENOMEM;
1201
Kay Sievers7eff2e72007-08-14 15:15:12 +02001202 env->buflen += len;
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001203 return 0;
1204}
1205
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001206#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1207 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001208 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001209 if (err) \
1210 return err; \
1211 } while (0)
1212
1213#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1214 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001215 int err = input_add_uevent_bm_var(env, name, bm, max); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001216 if (err) \
1217 return err; \
1218 } while (0)
1219
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001220#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1221 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001222 int err = input_add_uevent_modalias_var(env, dev); \
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001223 if (err) \
1224 return err; \
1225 } while (0)
1226
Kay Sievers7eff2e72007-08-14 15:15:12 +02001227static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001228{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001229 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001230
1231 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1232 dev->id.bustype, dev->id.vendor,
1233 dev->id.product, dev->id.version);
1234 if (dev->name)
1235 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1236 if (dev->phys)
1237 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -08001238 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001239 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1240
1241 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1242 if (test_bit(EV_KEY, dev->evbit))
1243 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1244 if (test_bit(EV_REL, dev->evbit))
1245 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1246 if (test_bit(EV_ABS, dev->evbit))
1247 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1248 if (test_bit(EV_MSC, dev->evbit))
1249 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1250 if (test_bit(EV_LED, dev->evbit))
1251 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1252 if (test_bit(EV_SND, dev->evbit))
1253 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1254 if (test_bit(EV_FF, dev->evbit))
1255 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1256 if (test_bit(EV_SW, dev->evbit))
1257 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1258
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001259 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001260
1261 return 0;
1262}
1263
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001264#define INPUT_DO_TOGGLE(dev, type, bits, on) \
1265 do { \
1266 int i; \
1267 if (!test_bit(EV_##type, dev->evbit)) \
1268 break; \
1269 for (i = 0; i < type##_MAX; i++) { \
1270 if (!test_bit(i, dev->bits##bit) || \
1271 !test_bit(i, dev->bits)) \
1272 continue; \
1273 dev->event(dev, EV_##type, i, on); \
1274 } \
1275 } while (0)
1276
Andrew Morton1c4115e2009-10-01 15:43:55 -07001277#ifdef CONFIG_PM
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001278static void input_dev_reset(struct input_dev *dev, bool activate)
1279{
1280 if (!dev->event)
1281 return;
1282
1283 INPUT_DO_TOGGLE(dev, LED, led, activate);
1284 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1285
1286 if (activate && test_bit(EV_REP, dev->evbit)) {
1287 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1288 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1289 }
1290}
1291
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001292static int input_dev_suspend(struct device *dev)
1293{
1294 struct input_dev *input_dev = to_input_dev(dev);
1295
1296 mutex_lock(&input_dev->mutex);
1297 input_dev_reset(input_dev, false);
1298 mutex_unlock(&input_dev->mutex);
1299
1300 return 0;
1301}
1302
1303static int input_dev_resume(struct device *dev)
1304{
1305 struct input_dev *input_dev = to_input_dev(dev);
1306
1307 mutex_lock(&input_dev->mutex);
1308 input_dev_reset(input_dev, true);
1309 mutex_unlock(&input_dev->mutex);
1310
1311 return 0;
1312}
1313
1314static const struct dev_pm_ops input_dev_pm_ops = {
1315 .suspend = input_dev_suspend,
1316 .resume = input_dev_resume,
1317 .poweroff = input_dev_suspend,
1318 .restore = input_dev_resume,
1319};
1320#endif /* CONFIG_PM */
1321
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001322static struct device_type input_dev_type = {
1323 .groups = input_dev_attr_groups,
1324 .release = input_dev_release,
1325 .uevent = input_dev_uevent,
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001326#ifdef CONFIG_PM
1327 .pm = &input_dev_pm_ops,
1328#endif
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001329};
1330
Kay Sieverse454cea2009-09-18 23:01:12 +02001331static char *input_devnode(struct device *dev, mode_t *mode)
Kay Sieversaa5ed632009-04-30 15:23:42 +02001332{
1333 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1334}
1335
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001336struct class input_class = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001337 .name = "input",
Kay Sieverse454cea2009-09-18 23:01:12 +02001338 .devnode = input_devnode,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001339};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001340EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001341
Dmitry Torokhov14471902006-11-02 23:26:55 -05001342/**
1343 * input_allocate_device - allocate memory for new input device
1344 *
1345 * Returns prepared struct input_dev or NULL.
1346 *
1347 * NOTE: Use input_free_device() to free devices that have not been
1348 * registered; input_unregister_device() should be used for already
1349 * registered devices.
1350 */
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001351struct input_dev *input_allocate_device(void)
1352{
1353 struct input_dev *dev;
1354
1355 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1356 if (dev) {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001357 dev->dev.type = &input_dev_type;
1358 dev->dev.class = &input_class;
1359 device_initialize(&dev->dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001360 mutex_init(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001361 spin_lock_init(&dev->event_lock);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001362 INIT_LIST_HEAD(&dev->h_list);
1363 INIT_LIST_HEAD(&dev->node);
Dmitry Torokhov655816e2006-09-14 01:32:14 -04001364
1365 __module_get(THIS_MODULE);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001366 }
1367
1368 return dev;
1369}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001370EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001371
Dmitry Torokhov14471902006-11-02 23:26:55 -05001372/**
1373 * input_free_device - free memory occupied by input_dev structure
1374 * @dev: input device to free
1375 *
1376 * This function should only be used if input_register_device()
1377 * was not called yet or if it failed. Once device was registered
1378 * use input_unregister_device() and memory will be freed once last
Dmitry Torokhov80064792007-08-30 00:22:11 -04001379 * reference to the device is dropped.
Dmitry Torokhov14471902006-11-02 23:26:55 -05001380 *
1381 * Device should be allocated by input_allocate_device().
1382 *
1383 * NOTE: If there are references to the input device then memory
1384 * will not be freed until last reference is dropped.
1385 */
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001386void input_free_device(struct input_dev *dev)
1387{
Dmitry Torokhov54f9e362007-03-16 00:57:25 -04001388 if (dev)
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001389 input_put_device(dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001390}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001391EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001392
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001393/**
1394 * input_set_capability - mark device as capable of a certain event
1395 * @dev: device that is capable of emitting or accepting event
1396 * @type: type of the event (EV_KEY, EV_REL, etc...)
1397 * @code: event code
1398 *
1399 * In addition to setting up corresponding bit in appropriate capability
1400 * bitmap the function also adjusts dev->evbit.
1401 */
1402void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1403{
1404 switch (type) {
1405 case EV_KEY:
1406 __set_bit(code, dev->keybit);
1407 break;
1408
1409 case EV_REL:
1410 __set_bit(code, dev->relbit);
1411 break;
1412
1413 case EV_ABS:
1414 __set_bit(code, dev->absbit);
1415 break;
1416
1417 case EV_MSC:
1418 __set_bit(code, dev->mscbit);
1419 break;
1420
1421 case EV_SW:
1422 __set_bit(code, dev->swbit);
1423 break;
1424
1425 case EV_LED:
1426 __set_bit(code, dev->ledbit);
1427 break;
1428
1429 case EV_SND:
1430 __set_bit(code, dev->sndbit);
1431 break;
1432
1433 case EV_FF:
1434 __set_bit(code, dev->ffbit);
1435 break;
1436
Dmitry Baryshkov22d1c392007-12-14 01:21:03 -05001437 case EV_PWR:
1438 /* do nothing */
1439 break;
1440
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001441 default:
1442 printk(KERN_ERR
1443 "input_set_capability: unknown type %u (code %u)\n",
1444 type, code);
1445 dump_stack();
1446 return;
1447 }
1448
1449 __set_bit(type, dev->evbit);
1450}
1451EXPORT_SYMBOL(input_set_capability);
1452
Dmitry Torokhov80064792007-08-30 00:22:11 -04001453/**
1454 * input_register_device - register device with input core
1455 * @dev: device to be registered
1456 *
1457 * This function registers device with input core. The device must be
1458 * allocated with input_allocate_device() and all it's capabilities
1459 * set up before registering.
1460 * If function fails the device must be freed with input_free_device().
1461 * Once device has been successfully registered it can be unregistered
1462 * with input_unregister_device(); input_free_device() should not be
1463 * called in this case.
1464 */
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001465int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001466{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001467 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001468 struct input_handler *handler;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001469 const char *path;
1470 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001471
Dmitry Torokhov80064792007-08-30 00:22:11 -04001472 __set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001473
1474 /*
1475 * If delay and period are pre-set by the driver, then autorepeating
1476 * is handled by the driver itself and we don't do it in input.c.
1477 */
1478
1479 init_timer(&dev->timer);
1480 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1481 dev->timer.data = (long) dev;
1482 dev->timer.function = input_repeat_key;
1483 dev->rep[REP_DELAY] = 250;
1484 dev->rep[REP_PERIOD] = 33;
1485 }
1486
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -04001487 if (!dev->getkeycode)
1488 dev->getkeycode = input_default_getkeycode;
1489
1490 if (!dev->setkeycode)
1491 dev->setkeycode = input_default_setkeycode;
1492
Kay Sieversa6c24902008-10-30 00:07:50 -04001493 dev_set_name(&dev->dev, "input%ld",
1494 (unsigned long) atomic_inc_return(&input_no) - 1);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001495
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001496 error = device_add(&dev->dev);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001497 if (error)
1498 return error;
1499
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001500 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001501 printk(KERN_INFO "input: %s as %s\n",
1502 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1503 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -07001504
Dmitry Torokhov80064792007-08-30 00:22:11 -04001505 error = mutex_lock_interruptible(&input_mutex);
1506 if (error) {
1507 device_del(&dev->dev);
1508 return error;
1509 }
1510
1511 list_add_tail(&dev->node, &input_dev_list);
1512
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001513 list_for_each_entry(handler, &input_handler_list, node)
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001514 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001515
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001516 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001517
Dmitry Torokhov80064792007-08-30 00:22:11 -04001518 mutex_unlock(&input_mutex);
1519
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001520 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001521}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001522EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001523
Dmitry Torokhov80064792007-08-30 00:22:11 -04001524/**
1525 * input_unregister_device - unregister previously registered device
1526 * @dev: device to be unregistered
1527 *
1528 * This function unregisters an input device. Once device is unregistered
1529 * the caller should not try to access it as it may get freed at any moment.
1530 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001531void input_unregister_device(struct input_dev *dev)
1532{
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001533 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001534
Dmitry Torokhov80064792007-08-30 00:22:11 -04001535 input_disconnect_device(dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001536
Dmitry Torokhov80064792007-08-30 00:22:11 -04001537 mutex_lock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001538
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001539 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001540 handle->handler->disconnect(handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001541 WARN_ON(!list_empty(&dev->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001542
Dmitry Torokhov80064792007-08-30 00:22:11 -04001543 del_timer_sync(&dev->timer);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001544 list_del_init(&dev->node);
1545
1546 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001547
1548 mutex_unlock(&input_mutex);
1549
1550 device_unregister(&dev->dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001551}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001552EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001553
Dmitry Torokhov80064792007-08-30 00:22:11 -04001554/**
1555 * input_register_handler - register a new input handler
1556 * @handler: handler to be registered
1557 *
1558 * This function registers a new input handler (interface) for input
1559 * devices in the system and attaches it to all input devices that
1560 * are compatible with the handler.
1561 */
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001562int input_register_handler(struct input_handler *handler)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001563{
1564 struct input_dev *dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001565 int retval;
1566
1567 retval = mutex_lock_interruptible(&input_mutex);
1568 if (retval)
1569 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001570
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001571 INIT_LIST_HEAD(&handler->h_list);
1572
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001573 if (handler->fops != NULL) {
Dmitry Torokhov80064792007-08-30 00:22:11 -04001574 if (input_table[handler->minor >> 5]) {
1575 retval = -EBUSY;
1576 goto out;
1577 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001578 input_table[handler->minor >> 5] = handler;
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001579 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001580
1581 list_add_tail(&handler->node, &input_handler_list);
1582
1583 list_for_each_entry(dev, &input_dev_list, node)
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001584 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001585
1586 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001587
1588 out:
1589 mutex_unlock(&input_mutex);
1590 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001591}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001592EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001593
Dmitry Torokhov80064792007-08-30 00:22:11 -04001594/**
1595 * input_unregister_handler - unregisters an input handler
1596 * @handler: handler to be unregistered
1597 *
1598 * This function disconnects a handler from its input devices and
1599 * removes it from lists of known handlers.
1600 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001601void input_unregister_handler(struct input_handler *handler)
1602{
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001603 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001604
Dmitry Torokhov80064792007-08-30 00:22:11 -04001605 mutex_lock(&input_mutex);
1606
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001607 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001608 handler->disconnect(handle);
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001609 WARN_ON(!list_empty(&handler->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001610
1611 list_del_init(&handler->node);
1612
1613 if (handler->fops != NULL)
1614 input_table[handler->minor >> 5] = NULL;
1615
1616 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001617
1618 mutex_unlock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001619}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001620EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001621
Dmitry Torokhov80064792007-08-30 00:22:11 -04001622/**
1623 * input_register_handle - register a new input handle
1624 * @handle: handle to register
1625 *
1626 * This function puts a new input handle onto device's
1627 * and handler's lists so that events can flow through
1628 * it once it is opened using input_open_device().
1629 *
1630 * This function is supposed to be called from handler's
1631 * connect() method.
1632 */
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001633int input_register_handle(struct input_handle *handle)
1634{
1635 struct input_handler *handler = handle->handler;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001636 struct input_dev *dev = handle->dev;
1637 int error;
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001638
Dmitry Torokhov80064792007-08-30 00:22:11 -04001639 /*
1640 * We take dev->mutex here to prevent race with
1641 * input_release_device().
1642 */
1643 error = mutex_lock_interruptible(&dev->mutex);
1644 if (error)
1645 return error;
1646 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1647 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001648
1649 /*
1650 * Since we are supposed to be called from ->connect()
1651 * which is mutually exclusive with ->disconnect()
1652 * we can't be racing with input_unregister_handle()
1653 * and so separate lock is not needed here.
1654 */
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001655 list_add_tail(&handle->h_node, &handler->h_list);
1656
1657 if (handler->start)
1658 handler->start(handle);
1659
1660 return 0;
1661}
1662EXPORT_SYMBOL(input_register_handle);
1663
Dmitry Torokhov80064792007-08-30 00:22:11 -04001664/**
1665 * input_unregister_handle - unregister an input handle
1666 * @handle: handle to unregister
1667 *
1668 * This function removes input handle from device's
1669 * and handler's lists.
1670 *
1671 * This function is supposed to be called from handler's
1672 * disconnect() method.
1673 */
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001674void input_unregister_handle(struct input_handle *handle)
1675{
Dmitry Torokhov80064792007-08-30 00:22:11 -04001676 struct input_dev *dev = handle->dev;
1677
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001678 list_del_init(&handle->h_node);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001679
1680 /*
1681 * Take dev->mutex to prevent race with input_release_device().
1682 */
1683 mutex_lock(&dev->mutex);
1684 list_del_rcu(&handle->d_node);
1685 mutex_unlock(&dev->mutex);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -04001686 synchronize_rcu();
Dmitry Torokhov5b2a08262007-04-12 01:29:46 -04001687}
1688EXPORT_SYMBOL(input_unregister_handle);
1689
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001690static int input_open_file(struct inode *inode, struct file *file)
1691{
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001692 struct input_handler *handler;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001693 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001694 int err;
1695
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001696 lock_kernel();
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001697 /* No load-on-demand here? */
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001698 handler = input_table[iminor(inode) >> 5];
1699 if (!handler || !(new_fops = fops_get(handler->fops))) {
1700 err = -ENODEV;
1701 goto out;
1702 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001703
1704 /*
1705 * That's _really_ odd. Usually NULL ->open means "nothing special",
1706 * not "no device". Oh, well...
1707 */
1708 if (!new_fops->open) {
1709 fops_put(new_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001710 err = -ENODEV;
1711 goto out;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001712 }
1713 old_fops = file->f_op;
1714 file->f_op = new_fops;
1715
1716 err = new_fops->open(inode, file);
1717
1718 if (err) {
1719 fops_put(file->f_op);
1720 file->f_op = fops_get(old_fops);
1721 }
1722 fops_put(old_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001723out:
1724 unlock_kernel();
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001725 return err;
1726}
1727
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001728static const struct file_operations input_fops = {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001729 .owner = THIS_MODULE,
1730 .open = input_open_file,
1731};
1732
Henrik Rydberg61994a62009-04-28 07:45:31 -07001733static void __init input_init_abs_bypass(void)
1734{
1735 const unsigned int *p;
1736
1737 for (p = input_abs_bypass_init_data; *p; p++)
1738 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1739}
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741static int __init input_init(void)
1742{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001743 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Henrik Rydberg61994a62009-04-28 07:45:31 -07001745 input_init_abs_bypass();
1746
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001747 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001748 if (err) {
1749 printk(KERN_ERR "input: unable to register input_dev class\n");
1750 return err;
1751 }
1752
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001753 err = input_proc_init();
1754 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001755 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001756
1757 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1758 if (err) {
1759 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001760 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001762
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001763 return 0;
1764
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001765 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001766 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001767 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
1770static void __exit input_exit(void)
1771{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001772 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001774 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777subsys_initcall(input_init);
1778module_exit(input_exit);