blob: 8ff92aa13a0af85c927bdf7403e0fa872c4c5963 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/input.h>
15#include <linux/module.h>
16#include <linux/random.h>
17#include <linux/major.h>
18#include <linux/proc_fs.h>
Dmitry Torokhov969b21c2006-04-02 00:09:34 -050019#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/poll.h>
21#include <linux/device.h>
Jes Sorensene676c232006-02-19 00:21:46 -050022#include <linux/mutex.h>
Dmitry Torokhov80064792007-08-30 00:22:11 -040023#include <linux/rcupdate.h>
Jonathan Corbet2edbf852008-05-15 10:37:16 -060024#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
27MODULE_DESCRIPTION("Input core");
28MODULE_LICENSE("GPL");
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define INPUT_DEVICES 256
31
Henrik Rydberg61994a62009-04-28 07:45:31 -070032/*
33 * EV_ABS events which should not be cached are listed here.
34 */
35static unsigned int input_abs_bypass_init_data[] __initdata = {
36 0
37};
38static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static LIST_HEAD(input_dev_list);
41static LIST_HEAD(input_handler_list);
42
Dmitry Torokhov80064792007-08-30 00:22:11 -040043/*
44 * input_mutex protects access to both input_dev_list and input_handler_list.
45 * This also causes input_[un]register_device and input_[un]register_handler
46 * be mutually exclusive which simplifies locking in drivers implementing
47 * input handlers.
48 */
49static DEFINE_MUTEX(input_mutex);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static struct input_handler *input_table[8];
52
Dmitry Torokhov80064792007-08-30 00:22:11 -040053static inline int is_event_supported(unsigned int code,
54 unsigned long *bm, unsigned int max)
55{
56 return code <= max && test_bit(code, bm);
57}
58
59static int input_defuzz_abs_event(int value, int old_val, int fuzz)
60{
61 if (fuzz) {
62 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
63 return old_val;
64
65 if (value > old_val - fuzz && value < old_val + fuzz)
66 return (old_val * 3 + value) / 4;
67
68 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
69 return (old_val + value) / 2;
70 }
71
72 return value;
73}
74
75/*
76 * Pass event through all open handles. This function is called with
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040077 * dev->event_lock held and interrupts disabled.
Dmitry Torokhov80064792007-08-30 00:22:11 -040078 */
79static void input_pass_event(struct input_dev *dev,
80 unsigned int type, unsigned int code, int value)
81{
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040082 struct input_handle *handle;
Dmitry Torokhov80064792007-08-30 00:22:11 -040083
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040084 rcu_read_lock();
85
86 handle = rcu_dereference(dev->grab);
Dmitry Torokhov80064792007-08-30 00:22:11 -040087 if (handle)
88 handle->handler->event(handle, type, code, value);
89 else
90 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
91 if (handle->open)
92 handle->handler->event(handle,
93 type, code, value);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -040094 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -040095}
96
97/*
98 * Generate software autorepeat event. Note that we take
99 * dev->event_lock here to avoid racing with input_event
100 * which may cause keys get "stuck".
101 */
102static void input_repeat_key(unsigned long data)
103{
104 struct input_dev *dev = (void *) data;
105 unsigned long flags;
106
107 spin_lock_irqsave(&dev->event_lock, flags);
108
109 if (test_bit(dev->repeat_key, dev->key) &&
110 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
111
112 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
113
114 if (dev->sync) {
115 /*
116 * Only send SYN_REPORT if we are not in a middle
117 * of driver parsing a new hardware packet.
118 * Otherwise assume that the driver will send
119 * SYN_REPORT once it's done.
120 */
121 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
122 }
123
124 if (dev->rep[REP_PERIOD])
125 mod_timer(&dev->timer, jiffies +
126 msecs_to_jiffies(dev->rep[REP_PERIOD]));
127 }
128
129 spin_unlock_irqrestore(&dev->event_lock, flags);
130}
131
132static void input_start_autorepeat(struct input_dev *dev, int code)
133{
134 if (test_bit(EV_REP, dev->evbit) &&
135 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
136 dev->timer.data) {
137 dev->repeat_key = code;
138 mod_timer(&dev->timer,
139 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
140 }
141}
142
Johannes Berge7b5c1e2009-01-29 23:17:52 -0800143static void input_stop_autorepeat(struct input_dev *dev)
144{
145 del_timer(&dev->timer);
146}
147
Dmitry Torokhov80064792007-08-30 00:22:11 -0400148#define INPUT_IGNORE_EVENT 0
149#define INPUT_PASS_TO_HANDLERS 1
150#define INPUT_PASS_TO_DEVICE 2
151#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
152
153static void input_handle_event(struct input_dev *dev,
154 unsigned int type, unsigned int code, int value)
155{
156 int disposition = INPUT_IGNORE_EVENT;
157
158 switch (type) {
159
160 case EV_SYN:
161 switch (code) {
162 case SYN_CONFIG:
163 disposition = INPUT_PASS_TO_ALL;
164 break;
165
166 case SYN_REPORT:
167 if (!dev->sync) {
168 dev->sync = 1;
169 disposition = INPUT_PASS_TO_HANDLERS;
170 }
171 break;
172 }
173 break;
174
175 case EV_KEY:
176 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
177 !!test_bit(code, dev->key) != value) {
178
179 if (value != 2) {
180 __change_bit(code, dev->key);
181 if (value)
182 input_start_autorepeat(dev, code);
Johannes Berge7b5c1e2009-01-29 23:17:52 -0800183 else
184 input_stop_autorepeat(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400185 }
186
187 disposition = INPUT_PASS_TO_HANDLERS;
188 }
189 break;
190
191 case EV_SW:
192 if (is_event_supported(code, dev->swbit, SW_MAX) &&
193 !!test_bit(code, dev->sw) != value) {
194
195 __change_bit(code, dev->sw);
196 disposition = INPUT_PASS_TO_HANDLERS;
197 }
198 break;
199
200 case EV_ABS:
201 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
202
Henrik Rydberg61994a62009-04-28 07:45:31 -0700203 if (test_bit(code, input_abs_bypass)) {
204 disposition = INPUT_PASS_TO_HANDLERS;
205 break;
206 }
207
Dmitry Torokhov80064792007-08-30 00:22:11 -0400208 value = input_defuzz_abs_event(value,
209 dev->abs[code], dev->absfuzz[code]);
210
211 if (dev->abs[code] != value) {
212 dev->abs[code] = value;
213 disposition = INPUT_PASS_TO_HANDLERS;
214 }
215 }
216 break;
217
218 case EV_REL:
219 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
220 disposition = INPUT_PASS_TO_HANDLERS;
221
222 break;
223
224 case EV_MSC:
225 if (is_event_supported(code, dev->mscbit, MSC_MAX))
226 disposition = INPUT_PASS_TO_ALL;
227
228 break;
229
230 case EV_LED:
231 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
232 !!test_bit(code, dev->led) != value) {
233
234 __change_bit(code, dev->led);
235 disposition = INPUT_PASS_TO_ALL;
236 }
237 break;
238
239 case EV_SND:
240 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
241
242 if (!!test_bit(code, dev->snd) != !!value)
243 __change_bit(code, dev->snd);
244 disposition = INPUT_PASS_TO_ALL;
245 }
246 break;
247
248 case EV_REP:
249 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
250 dev->rep[code] = value;
251 disposition = INPUT_PASS_TO_ALL;
252 }
253 break;
254
255 case EV_FF:
256 if (value >= 0)
257 disposition = INPUT_PASS_TO_ALL;
258 break;
Richard Purdieed2fa4d2008-01-03 10:46:21 -0500259
260 case EV_PWR:
261 disposition = INPUT_PASS_TO_ALL;
262 break;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400263 }
264
Dmitry Torokhovc9812282008-06-02 01:02:40 -0400265 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400266 dev->sync = 0;
267
268 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
269 dev->event(dev, type, code, value);
270
271 if (disposition & INPUT_PASS_TO_HANDLERS)
272 input_pass_event(dev, type, code, value);
273}
274
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400275/**
276 * input_event() - report new input event
Dmitry Torokhov14471902006-11-02 23:26:55 -0500277 * @dev: device that generated the event
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400278 * @type: type of the event
279 * @code: event code
280 * @value: value of the event
281 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400282 * This function should be used by drivers implementing various input
283 * devices. See also input_inject_event().
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400284 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400285
286void input_event(struct input_dev *dev,
287 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400289 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Dmitry Torokhov80064792007-08-30 00:22:11 -0400291 if (is_event_supported(type, dev->evbit, EV_MAX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Dmitry Torokhov80064792007-08-30 00:22:11 -0400293 spin_lock_irqsave(&dev->event_lock, flags);
294 add_input_randomness(type, code, value);
295 input_handle_event(dev, type, code, value);
296 spin_unlock_irqrestore(&dev->event_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400299EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400301/**
302 * input_inject_event() - send input event from input handler
303 * @handle: input handle to send event through
304 * @type: type of the event
305 * @code: event code
306 * @value: value of the event
307 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400308 * Similar to input_event() but will ignore event if device is
309 * "grabbed" and handle injecting event is not the one that owns
310 * the device.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400311 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400312void input_inject_event(struct input_handle *handle,
313 unsigned int type, unsigned int code, int value)
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400314{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400315 struct input_dev *dev = handle->dev;
316 struct input_handle *grab;
317 unsigned long flags;
318
319 if (is_event_supported(type, dev->evbit, EV_MAX)) {
320 spin_lock_irqsave(&dev->event_lock, flags);
321
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400322 rcu_read_lock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400323 grab = rcu_dereference(dev->grab);
324 if (!grab || grab == handle)
325 input_handle_event(dev, type, code, value);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400326 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400327
328 spin_unlock_irqrestore(&dev->event_lock, flags);
329 }
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400330}
331EXPORT_SYMBOL(input_inject_event);
332
Dmitry Torokhov80064792007-08-30 00:22:11 -0400333/**
334 * input_grab_device - grabs device for exclusive use
335 * @handle: input handle that wants to own the device
336 *
337 * When a device is grabbed by an input handle all events generated by
338 * the device are delivered only to this handle. Also events injected
339 * by other input handles are ignored while device is grabbed.
340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341int input_grab_device(struct input_handle *handle)
342{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400343 struct input_dev *dev = handle->dev;
344 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Dmitry Torokhov80064792007-08-30 00:22:11 -0400346 retval = mutex_lock_interruptible(&dev->mutex);
347 if (retval)
348 return retval;
349
350 if (dev->grab) {
351 retval = -EBUSY;
352 goto out;
353 }
354
355 rcu_assign_pointer(dev->grab, handle);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400356 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400357
358 out:
359 mutex_unlock(&dev->mutex);
360 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400362EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dmitry Torokhov80064792007-08-30 00:22:11 -0400364static void __input_release_device(struct input_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400366 struct input_dev *dev = handle->dev;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400367
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400368 if (dev->grab == handle) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400369 rcu_assign_pointer(dev->grab, NULL);
370 /* Make sure input_pass_event() notices that grab is gone */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400371 synchronize_rcu();
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400372
373 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400374 if (handle->open && handle->handler->start)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400375 handle->handler->start(handle);
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
Dmitry Torokhov80064792007-08-30 00:22:11 -0400378
379/**
380 * input_release_device - release previously grabbed device
381 * @handle: input handle that owns the device
382 *
383 * Releases previously grabbed device so that other input handles can
384 * start receiving input events. Upon release all handlers attached
385 * to the device have their start() method called so they have a change
386 * to synchronize device state with the rest of the system.
387 */
388void input_release_device(struct input_handle *handle)
389{
390 struct input_dev *dev = handle->dev;
391
392 mutex_lock(&dev->mutex);
393 __input_release_device(handle);
394 mutex_unlock(&dev->mutex);
395}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400396EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Dmitry Torokhov80064792007-08-30 00:22:11 -0400398/**
399 * input_open_device - open input device
400 * @handle: handle through which device is being accessed
401 *
402 * This function should be called by input handlers when they
403 * want to start receive events from given input device.
404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405int input_open_device(struct input_handle *handle)
406{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500407 struct input_dev *dev = handle->dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400408 int retval;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500409
Dmitry Torokhov80064792007-08-30 00:22:11 -0400410 retval = mutex_lock_interruptible(&dev->mutex);
411 if (retval)
412 return retval;
413
414 if (dev->going_away) {
415 retval = -ENODEV;
416 goto out;
417 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500420
421 if (!dev->users++ && dev->open)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400422 retval = dev->open(dev);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500423
Dmitry Torokhov80064792007-08-30 00:22:11 -0400424 if (retval) {
425 dev->users--;
426 if (!--handle->open) {
427 /*
428 * Make sure we are not delivering any more events
429 * through this handle
430 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400431 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400432 }
433 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500434
Dmitry Torokhov80064792007-08-30 00:22:11 -0400435 out:
Jes Sorensene676c232006-02-19 00:21:46 -0500436 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400437 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400439EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Dmitry Torokhov80064792007-08-30 00:22:11 -0400441int input_flush_device(struct input_handle *handle, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400443 struct input_dev *dev = handle->dev;
444 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Dmitry Torokhov80064792007-08-30 00:22:11 -0400446 retval = mutex_lock_interruptible(&dev->mutex);
447 if (retval)
448 return retval;
449
450 if (dev->flush)
451 retval = dev->flush(dev, file);
452
453 mutex_unlock(&dev->mutex);
454 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400456EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Dmitry Torokhov80064792007-08-30 00:22:11 -0400458/**
459 * input_close_device - close input device
460 * @handle: handle through which device is being accessed
461 *
462 * This function should be called by input handlers when they
463 * want to stop receive events from given input device.
464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465void input_close_device(struct input_handle *handle)
466{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500467 struct input_dev *dev = handle->dev;
468
Jes Sorensene676c232006-02-19 00:21:46 -0500469 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500470
Dmitry Torokhov80064792007-08-30 00:22:11 -0400471 __input_release_device(handle);
472
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500473 if (!--dev->users && dev->close)
474 dev->close(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400475
476 if (!--handle->open) {
477 /*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400478 * synchronize_rcu() makes sure that input_pass_event()
Dmitry Torokhov80064792007-08-30 00:22:11 -0400479 * completed and that no more input events are delivered
480 * through this handle
481 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400482 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400483 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500484
Jes Sorensene676c232006-02-19 00:21:46 -0500485 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400487EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Dmitry Torokhov80064792007-08-30 00:22:11 -0400489/*
490 * Prepare device for unregistering
491 */
492static void input_disconnect_device(struct input_dev *dev)
493{
494 struct input_handle *handle;
495 int code;
496
497 /*
498 * Mark device as going away. Note that we take dev->mutex here
499 * not to protect access to dev->going_away but rather to ensure
500 * that there are no threads in the middle of input_open_device()
501 */
502 mutex_lock(&dev->mutex);
503 dev->going_away = 1;
504 mutex_unlock(&dev->mutex);
505
506 spin_lock_irq(&dev->event_lock);
507
508 /*
509 * Simulate keyup events for all pressed keys so that handlers
510 * are not left with "stuck" keys. The driver may continue
511 * generate events even after we done here but they will not
512 * reach any handlers.
513 */
514 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
515 for (code = 0; code <= KEY_MAX; code++) {
516 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400517 __test_and_clear_bit(code, dev->key)) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400518 input_pass_event(dev, EV_KEY, code, 0);
519 }
520 }
521 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
522 }
523
524 list_for_each_entry(handle, &dev->h_list, d_node)
525 handle->open = 0;
526
527 spin_unlock_irq(&dev->event_lock);
528}
529
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400530static int input_fetch_keycode(struct input_dev *dev, int scancode)
531{
532 switch (dev->keycodesize) {
533 case 1:
534 return ((u8 *)dev->keycode)[scancode];
535
536 case 2:
537 return ((u16 *)dev->keycode)[scancode];
538
539 default:
540 return ((u32 *)dev->keycode)[scancode];
541 }
542}
543
544static int input_default_getkeycode(struct input_dev *dev,
545 int scancode, int *keycode)
546{
547 if (!dev->keycodesize)
548 return -EINVAL;
549
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400550 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400551 return -EINVAL;
552
553 *keycode = input_fetch_keycode(dev, scancode);
554
555 return 0;
556}
557
558static int input_default_setkeycode(struct input_dev *dev,
559 int scancode, int keycode)
560{
561 int old_keycode;
562 int i;
563
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400564 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400565 return -EINVAL;
566
567 if (!dev->keycodesize)
568 return -EINVAL;
569
570 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
571 return -EINVAL;
572
573 switch (dev->keycodesize) {
574 case 1: {
575 u8 *k = (u8 *)dev->keycode;
576 old_keycode = k[scancode];
577 k[scancode] = keycode;
578 break;
579 }
580 case 2: {
581 u16 *k = (u16 *)dev->keycode;
582 old_keycode = k[scancode];
583 k[scancode] = keycode;
584 break;
585 }
586 default: {
587 u32 *k = (u32 *)dev->keycode;
588 old_keycode = k[scancode];
589 k[scancode] = keycode;
590 break;
591 }
592 }
593
594 clear_bit(old_keycode, dev->keybit);
595 set_bit(keycode, dev->keybit);
596
597 for (i = 0; i < dev->keycodemax; i++) {
598 if (input_fetch_keycode(dev, i) == old_keycode) {
599 set_bit(old_keycode, dev->keybit);
600 break; /* Setting the bit twice is useless, so break */
601 }
602 }
603
604 return 0;
605}
606
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400607/**
608 * input_get_keycode - retrieve keycode currently mapped to a given scancode
609 * @dev: input device which keymap is being queried
610 * @scancode: scancode (or its equivalent for device in question) for which
611 * keycode is needed
612 * @keycode: result
613 *
614 * This function should be called by anyone interested in retrieving current
615 * keymap. Presently keyboard and evdev handlers use it.
616 */
617int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
618{
619 if (scancode < 0)
620 return -EINVAL;
621
622 return dev->getkeycode(dev, scancode, keycode);
623}
624EXPORT_SYMBOL(input_get_keycode);
625
626/**
627 * input_get_keycode - assign new keycode to a given scancode
628 * @dev: input device which keymap is being updated
629 * @scancode: scancode (or its equivalent for device in question)
630 * @keycode: new keycode to be assigned to the scancode
631 *
632 * This function should be called by anyone needing to update current
633 * keymap. Presently keyboard and evdev handlers use it.
634 */
635int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
636{
637 unsigned long flags;
638 int old_keycode;
639 int retval;
640
641 if (scancode < 0)
642 return -EINVAL;
643
644 if (keycode < 0 || keycode > KEY_MAX)
645 return -EINVAL;
646
647 spin_lock_irqsave(&dev->event_lock, flags);
648
649 retval = dev->getkeycode(dev, scancode, &old_keycode);
650 if (retval)
651 goto out;
652
653 retval = dev->setkeycode(dev, scancode, keycode);
654 if (retval)
655 goto out;
656
657 /*
658 * Simulate keyup event if keycode is not present
659 * in the keymap anymore
660 */
661 if (test_bit(EV_KEY, dev->evbit) &&
662 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
663 __test_and_clear_bit(old_keycode, dev->key)) {
664
665 input_pass_event(dev, EV_KEY, old_keycode, 0);
666 if (dev->sync)
667 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
668 }
669
670 out:
671 spin_unlock_irqrestore(&dev->event_lock, flags);
672
673 return retval;
674}
675EXPORT_SYMBOL(input_set_keycode);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677#define MATCH_BIT(bit, max) \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700678 for (i = 0; i < BITS_TO_LONGS(max); i++) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
680 break; \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700681 if (i != BITS_TO_LONGS(max)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 continue;
683
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400684static const struct input_device_id *input_match_device(const struct input_device_id *id,
685 struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 int i;
688
689 for (; id->flags || id->driver_info; id++) {
690
691 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400692 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 continue;
694
695 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400696 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 continue;
698
699 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400700 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 continue;
702
703 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400704 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 continue;
706
707 MATCH_BIT(evbit, EV_MAX);
708 MATCH_BIT(keybit, KEY_MAX);
709 MATCH_BIT(relbit, REL_MAX);
710 MATCH_BIT(absbit, ABS_MAX);
711 MATCH_BIT(mscbit, MSC_MAX);
712 MATCH_BIT(ledbit, LED_MAX);
713 MATCH_BIT(sndbit, SND_MAX);
714 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500715 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 return id;
718 }
719
720 return NULL;
721}
722
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400723static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
724{
725 const struct input_device_id *id;
726 int error;
727
728 if (handler->blacklist && input_match_device(handler->blacklist, dev))
729 return -ENODEV;
730
731 id = input_match_device(handler->id_table, dev);
732 if (!id)
733 return -ENODEV;
734
735 error = handler->connect(handler, dev, id);
736 if (error && error != -ENODEV)
737 printk(KERN_ERR
738 "input: failed to attach handler %s to device %s, "
739 "error: %d\n",
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400740 handler->name, kobject_name(&dev->dev.kobj), error);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400741
742 return error;
743}
744
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500747
748static struct proc_dir_entry *proc_bus_input_dir;
749static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
750static int input_devices_state;
751
752static inline void input_wakeup_procfs_readers(void)
753{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 input_devices_state++;
755 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500758static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500760 poll_wait(file, &input_devices_poll_wait, wait);
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800761 if (file->f_version != input_devices_state) {
762 file->f_version = input_devices_state;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500763 return POLLIN | POLLRDNORM;
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800764 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400765
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500769static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
770{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400771 if (mutex_lock_interruptible(&input_mutex))
772 return NULL;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500773
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400774 return seq_list_start(&input_dev_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500775}
776
777static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
778{
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400779 return seq_list_next(v, &input_dev_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500780}
781
782static void input_devices_seq_stop(struct seq_file *seq, void *v)
783{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400784 mutex_unlock(&input_mutex);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500785}
786
787static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
788 unsigned long *bitmap, int max)
789{
790 int i;
791
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700792 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500793 if (bitmap[i])
794 break;
795
796 seq_printf(seq, "B: %s=", name);
797 for (; i >= 0; i--)
798 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
799 seq_putc(seq, '\n');
800}
801
802static int input_devices_seq_show(struct seq_file *seq, void *v)
803{
804 struct input_dev *dev = container_of(v, struct input_dev, node);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400805 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct input_handle *handle;
807
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500808 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
809 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500811 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
812 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
813 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
Dmitry Torokhov15e03ae2007-03-07 23:20:17 -0500814 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500815 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500817 list_for_each_entry(handle, &dev->h_list, d_node)
818 seq_printf(seq, "%s ", handle->name);
819 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500820
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500821 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
822 if (test_bit(EV_KEY, dev->evbit))
823 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
824 if (test_bit(EV_REL, dev->evbit))
825 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
826 if (test_bit(EV_ABS, dev->evbit))
827 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
828 if (test_bit(EV_MSC, dev->evbit))
829 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
830 if (test_bit(EV_LED, dev->evbit))
831 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
832 if (test_bit(EV_SND, dev->evbit))
833 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
834 if (test_bit(EV_FF, dev->evbit))
835 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
836 if (test_bit(EV_SW, dev->evbit))
837 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500839 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500841 kfree(path);
842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Jan Engelhardtcec69c32008-01-31 00:43:32 -0500845static const struct seq_operations input_devices_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500846 .start = input_devices_seq_start,
847 .next = input_devices_seq_next,
848 .stop = input_devices_seq_stop,
849 .show = input_devices_seq_show,
850};
851
852static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500854 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800857static const struct file_operations input_devices_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500858 .owner = THIS_MODULE,
859 .open = input_proc_devices_open,
860 .poll = input_proc_devices_poll,
861 .read = seq_read,
862 .llseek = seq_lseek,
863 .release = seq_release,
864};
865
866static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
867{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400868 if (mutex_lock_interruptible(&input_mutex))
869 return NULL;
870
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500871 seq->private = (void *)(unsigned long)*pos;
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400872 return seq_list_start(&input_handler_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500873}
874
875static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
876{
877 seq->private = (void *)(unsigned long)(*pos + 1);
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400878 return seq_list_next(v, &input_handler_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500879}
880
881static void input_handlers_seq_stop(struct seq_file *seq, void *v)
882{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400883 mutex_unlock(&input_mutex);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500884}
885
886static int input_handlers_seq_show(struct seq_file *seq, void *v)
887{
888 struct input_handler *handler = container_of(v, struct input_handler, node);
889
890 seq_printf(seq, "N: Number=%ld Name=%s",
891 (unsigned long)seq->private, handler->name);
892 if (handler->fops)
893 seq_printf(seq, " Minor=%d", handler->minor);
894 seq_putc(seq, '\n');
895
896 return 0;
897}
Jan Engelhardtcec69c32008-01-31 00:43:32 -0500898static const struct seq_operations input_handlers_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500899 .start = input_handlers_seq_start,
900 .next = input_handlers_seq_next,
901 .stop = input_handlers_seq_stop,
902 .show = input_handlers_seq_show,
903};
904
905static int input_proc_handlers_open(struct inode *inode, struct file *file)
906{
907 return seq_open(file, &input_handlers_seq_ops);
908}
909
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800910static const struct file_operations input_handlers_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500911 .owner = THIS_MODULE,
912 .open = input_proc_handlers_open,
913 .read = seq_read,
914 .llseek = seq_lseek,
915 .release = seq_release,
916};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918static int __init input_proc_init(void)
919{
920 struct proc_dir_entry *entry;
921
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700922 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500923 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500925
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700926 entry = proc_create("devices", 0, proc_bus_input_dir,
927 &input_devices_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500928 if (!entry)
929 goto fail1;
930
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700931 entry = proc_create("handlers", 0, proc_bus_input_dir,
932 &input_handlers_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500933 if (!entry)
934 goto fail2;
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500937
938 fail2: remove_proc_entry("devices", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700939 fail1: remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500940 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500942
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500943static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500944{
945 remove_proc_entry("devices", proc_bus_input_dir);
946 remove_proc_entry("handlers", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700947 remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500951static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500953static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954#endif
955
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400956#define INPUT_DEV_STRING_ATTR_SHOW(name) \
957static ssize_t input_dev_show_##name(struct device *dev, \
958 struct device_attribute *attr, \
959 char *buf) \
960{ \
961 struct input_dev *input_dev = to_input_dev(dev); \
962 \
963 return scnprintf(buf, PAGE_SIZE, "%s\n", \
964 input_dev->name ? input_dev->name : ""); \
965} \
966static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500967
968INPUT_DEV_STRING_ATTR_SHOW(name);
969INPUT_DEV_STRING_ATTR_SHOW(phys);
970INPUT_DEV_STRING_ATTR_SHOW(uniq);
971
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500972static int input_print_modalias_bits(char *buf, int size,
973 char name, unsigned long *bm,
974 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100975{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500976 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100977
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500978 len += snprintf(buf, max(size, 0), "%c", name);
979 for (i = min_bit; i < max_bit; i++)
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700980 if (bm[BIT_WORD(i)] & BIT_MASK(i))
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500981 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100982 return len;
983}
984
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500985static int input_print_modalias(char *buf, int size, struct input_dev *id,
986 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100987{
988 int len;
989
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500990 len = snprintf(buf, max(size, 0),
991 "input:b%04Xv%04Xp%04Xe%04X-",
992 id->id.bustype, id->id.vendor,
993 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100994
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500995 len += input_print_modalias_bits(buf + len, size - len,
996 'e', id->evbit, 0, EV_MAX);
997 len += input_print_modalias_bits(buf + len, size - len,
998 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
999 len += input_print_modalias_bits(buf + len, size - len,
1000 'r', id->relbit, 0, REL_MAX);
1001 len += input_print_modalias_bits(buf + len, size - len,
1002 'a', id->absbit, 0, ABS_MAX);
1003 len += input_print_modalias_bits(buf + len, size - len,
1004 'm', id->mscbit, 0, MSC_MAX);
1005 len += input_print_modalias_bits(buf + len, size - len,
1006 'l', id->ledbit, 0, LED_MAX);
1007 len += input_print_modalias_bits(buf + len, size - len,
1008 's', id->sndbit, 0, SND_MAX);
1009 len += input_print_modalias_bits(buf + len, size - len,
1010 'f', id->ffbit, 0, FF_MAX);
1011 len += input_print_modalias_bits(buf + len, size - len,
1012 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001013
1014 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001015 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001016
Rusty Russell1d8f4302005-12-07 21:40:34 +01001017 return len;
1018}
1019
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001020static ssize_t input_dev_show_modalias(struct device *dev,
1021 struct device_attribute *attr,
1022 char *buf)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001023{
1024 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001025 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001026
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001027 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1028
Richard Purdie8a3cf452006-06-26 01:48:21 -04001029 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001030}
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001031static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001032
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001033static struct attribute *input_dev_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001034 &dev_attr_name.attr,
1035 &dev_attr_phys.attr,
1036 &dev_attr_uniq.attr,
1037 &dev_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001038 NULL
1039};
1040
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001041static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001042 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001043};
1044
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001045#define INPUT_DEV_ID_ATTR(name) \
1046static ssize_t input_dev_show_id_##name(struct device *dev, \
1047 struct device_attribute *attr, \
1048 char *buf) \
1049{ \
1050 struct input_dev *input_dev = to_input_dev(dev); \
1051 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1052} \
1053static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001054
1055INPUT_DEV_ID_ATTR(bustype);
1056INPUT_DEV_ID_ATTR(vendor);
1057INPUT_DEV_ID_ATTR(product);
1058INPUT_DEV_ID_ATTR(version);
1059
1060static struct attribute *input_dev_id_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001061 &dev_attr_bustype.attr,
1062 &dev_attr_vendor.attr,
1063 &dev_attr_product.attr,
1064 &dev_attr_version.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001065 NULL
1066};
1067
1068static struct attribute_group input_dev_id_attr_group = {
1069 .name = "id",
1070 .attrs = input_dev_id_attrs,
1071};
1072
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001073static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1074 int max, int add_cr)
1075{
1076 int i;
1077 int len = 0;
1078
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001079 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001080 if (bitmap[i])
1081 break;
1082
1083 for (; i >= 0; i--)
1084 len += snprintf(buf + len, max(buf_size - len, 0),
1085 "%lx%s", bitmap[i], i > 0 ? " " : "");
1086
1087 if (add_cr)
1088 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1089
1090 return len;
1091}
1092
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001093#define INPUT_DEV_CAP_ATTR(ev, bm) \
1094static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1095 struct device_attribute *attr, \
1096 char *buf) \
1097{ \
1098 struct input_dev *input_dev = to_input_dev(dev); \
1099 int len = input_print_bitmap(buf, PAGE_SIZE, \
1100 input_dev->bm##bit, ev##_MAX, 1); \
1101 return min_t(int, len, PAGE_SIZE); \
1102} \
1103static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001104
1105INPUT_DEV_CAP_ATTR(EV, ev);
1106INPUT_DEV_CAP_ATTR(KEY, key);
1107INPUT_DEV_CAP_ATTR(REL, rel);
1108INPUT_DEV_CAP_ATTR(ABS, abs);
1109INPUT_DEV_CAP_ATTR(MSC, msc);
1110INPUT_DEV_CAP_ATTR(LED, led);
1111INPUT_DEV_CAP_ATTR(SND, snd);
1112INPUT_DEV_CAP_ATTR(FF, ff);
1113INPUT_DEV_CAP_ATTR(SW, sw);
1114
1115static struct attribute *input_dev_caps_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001116 &dev_attr_ev.attr,
1117 &dev_attr_key.attr,
1118 &dev_attr_rel.attr,
1119 &dev_attr_abs.attr,
1120 &dev_attr_msc.attr,
1121 &dev_attr_led.attr,
1122 &dev_attr_snd.attr,
1123 &dev_attr_ff.attr,
1124 &dev_attr_sw.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001125 NULL
1126};
1127
1128static struct attribute_group input_dev_caps_attr_group = {
1129 .name = "capabilities",
1130 .attrs = input_dev_caps_attrs,
1131};
1132
Dmitry Torokhovcb9def42007-03-07 23:20:26 -05001133static struct attribute_group *input_dev_attr_groups[] = {
1134 &input_dev_attr_group,
1135 &input_dev_id_attr_group,
1136 &input_dev_caps_attr_group,
1137 NULL
1138};
1139
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001140static void input_dev_release(struct device *device)
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001141{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001142 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001143
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001144 input_ff_destroy(dev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001145 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001146
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001147 module_put(THIS_MODULE);
1148}
1149
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001150/*
Kay Sievers312c0042005-11-16 09:00:00 +01001151 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001152 * device bitfields.
1153 */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001154static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001155 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001156{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001157 int len;
1158
1159 if (add_uevent_var(env, "%s=", name))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001160 return -ENOMEM;
1161
Kay Sievers7eff2e72007-08-14 15:15:12 +02001162 len = input_print_bitmap(&env->buf[env->buflen - 1],
1163 sizeof(env->buf) - env->buflen,
1164 bitmap, max, 0);
1165 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001166 return -ENOMEM;
1167
Kay Sievers7eff2e72007-08-14 15:15:12 +02001168 env->buflen += len;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001169 return 0;
1170}
1171
Kay Sievers7eff2e72007-08-14 15:15:12 +02001172static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001173 struct input_dev *dev)
1174{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001175 int len;
1176
1177 if (add_uevent_var(env, "MODALIAS="))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001178 return -ENOMEM;
1179
Kay Sievers7eff2e72007-08-14 15:15:12 +02001180 len = input_print_modalias(&env->buf[env->buflen - 1],
1181 sizeof(env->buf) - env->buflen,
1182 dev, 0);
1183 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001184 return -ENOMEM;
1185
Kay Sievers7eff2e72007-08-14 15:15:12 +02001186 env->buflen += len;
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001187 return 0;
1188}
1189
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001190#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1191 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001192 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001193 if (err) \
1194 return err; \
1195 } while (0)
1196
1197#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1198 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001199 int err = input_add_uevent_bm_var(env, name, bm, max); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001200 if (err) \
1201 return err; \
1202 } while (0)
1203
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001204#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1205 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001206 int err = input_add_uevent_modalias_var(env, dev); \
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001207 if (err) \
1208 return err; \
1209 } while (0)
1210
Kay Sievers7eff2e72007-08-14 15:15:12 +02001211static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001212{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001213 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001214
1215 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1216 dev->id.bustype, dev->id.vendor,
1217 dev->id.product, dev->id.version);
1218 if (dev->name)
1219 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1220 if (dev->phys)
1221 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -08001222 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001223 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1224
1225 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1226 if (test_bit(EV_KEY, dev->evbit))
1227 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1228 if (test_bit(EV_REL, dev->evbit))
1229 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1230 if (test_bit(EV_ABS, dev->evbit))
1231 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1232 if (test_bit(EV_MSC, dev->evbit))
1233 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1234 if (test_bit(EV_LED, dev->evbit))
1235 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1236 if (test_bit(EV_SND, dev->evbit))
1237 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1238 if (test_bit(EV_FF, dev->evbit))
1239 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1240 if (test_bit(EV_SW, dev->evbit))
1241 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1242
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001243 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001244
1245 return 0;
1246}
1247
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001248static struct device_type input_dev_type = {
1249 .groups = input_dev_attr_groups,
1250 .release = input_dev_release,
1251 .uevent = input_dev_uevent,
1252};
1253
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001254struct class input_class = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001255 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001256};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001257EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001258
Dmitry Torokhov14471902006-11-02 23:26:55 -05001259/**
1260 * input_allocate_device - allocate memory for new input device
1261 *
1262 * Returns prepared struct input_dev or NULL.
1263 *
1264 * NOTE: Use input_free_device() to free devices that have not been
1265 * registered; input_unregister_device() should be used for already
1266 * registered devices.
1267 */
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001268struct input_dev *input_allocate_device(void)
1269{
1270 struct input_dev *dev;
1271
1272 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1273 if (dev) {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001274 dev->dev.type = &input_dev_type;
1275 dev->dev.class = &input_class;
1276 device_initialize(&dev->dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001277 mutex_init(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001278 spin_lock_init(&dev->event_lock);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001279 INIT_LIST_HEAD(&dev->h_list);
1280 INIT_LIST_HEAD(&dev->node);
Dmitry Torokhov655816e2006-09-14 01:32:14 -04001281
1282 __module_get(THIS_MODULE);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001283 }
1284
1285 return dev;
1286}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001287EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001288
Dmitry Torokhov14471902006-11-02 23:26:55 -05001289/**
1290 * input_free_device - free memory occupied by input_dev structure
1291 * @dev: input device to free
1292 *
1293 * This function should only be used if input_register_device()
1294 * was not called yet or if it failed. Once device was registered
1295 * use input_unregister_device() and memory will be freed once last
Dmitry Torokhov80064792007-08-30 00:22:11 -04001296 * reference to the device is dropped.
Dmitry Torokhov14471902006-11-02 23:26:55 -05001297 *
1298 * Device should be allocated by input_allocate_device().
1299 *
1300 * NOTE: If there are references to the input device then memory
1301 * will not be freed until last reference is dropped.
1302 */
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001303void input_free_device(struct input_dev *dev)
1304{
Dmitry Torokhov54f9e362007-03-16 00:57:25 -04001305 if (dev)
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001306 input_put_device(dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001307}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001308EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001309
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001310/**
1311 * input_set_capability - mark device as capable of a certain event
1312 * @dev: device that is capable of emitting or accepting event
1313 * @type: type of the event (EV_KEY, EV_REL, etc...)
1314 * @code: event code
1315 *
1316 * In addition to setting up corresponding bit in appropriate capability
1317 * bitmap the function also adjusts dev->evbit.
1318 */
1319void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1320{
1321 switch (type) {
1322 case EV_KEY:
1323 __set_bit(code, dev->keybit);
1324 break;
1325
1326 case EV_REL:
1327 __set_bit(code, dev->relbit);
1328 break;
1329
1330 case EV_ABS:
1331 __set_bit(code, dev->absbit);
1332 break;
1333
1334 case EV_MSC:
1335 __set_bit(code, dev->mscbit);
1336 break;
1337
1338 case EV_SW:
1339 __set_bit(code, dev->swbit);
1340 break;
1341
1342 case EV_LED:
1343 __set_bit(code, dev->ledbit);
1344 break;
1345
1346 case EV_SND:
1347 __set_bit(code, dev->sndbit);
1348 break;
1349
1350 case EV_FF:
1351 __set_bit(code, dev->ffbit);
1352 break;
1353
Dmitry Baryshkov22d1c392007-12-14 01:21:03 -05001354 case EV_PWR:
1355 /* do nothing */
1356 break;
1357
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001358 default:
1359 printk(KERN_ERR
1360 "input_set_capability: unknown type %u (code %u)\n",
1361 type, code);
1362 dump_stack();
1363 return;
1364 }
1365
1366 __set_bit(type, dev->evbit);
1367}
1368EXPORT_SYMBOL(input_set_capability);
1369
Dmitry Torokhov80064792007-08-30 00:22:11 -04001370/**
1371 * input_register_device - register device with input core
1372 * @dev: device to be registered
1373 *
1374 * This function registers device with input core. The device must be
1375 * allocated with input_allocate_device() and all it's capabilities
1376 * set up before registering.
1377 * If function fails the device must be freed with input_free_device().
1378 * Once device has been successfully registered it can be unregistered
1379 * with input_unregister_device(); input_free_device() should not be
1380 * called in this case.
1381 */
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001382int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001383{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001384 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001385 struct input_handler *handler;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001386 const char *path;
1387 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001388
Dmitry Torokhov80064792007-08-30 00:22:11 -04001389 __set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001390
1391 /*
1392 * If delay and period are pre-set by the driver, then autorepeating
1393 * is handled by the driver itself and we don't do it in input.c.
1394 */
1395
1396 init_timer(&dev->timer);
1397 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1398 dev->timer.data = (long) dev;
1399 dev->timer.function = input_repeat_key;
1400 dev->rep[REP_DELAY] = 250;
1401 dev->rep[REP_PERIOD] = 33;
1402 }
1403
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -04001404 if (!dev->getkeycode)
1405 dev->getkeycode = input_default_getkeycode;
1406
1407 if (!dev->setkeycode)
1408 dev->setkeycode = input_default_setkeycode;
1409
Kay Sieversa6c24902008-10-30 00:07:50 -04001410 dev_set_name(&dev->dev, "input%ld",
1411 (unsigned long) atomic_inc_return(&input_no) - 1);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001412
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001413 error = device_add(&dev->dev);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001414 if (error)
1415 return error;
1416
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001417 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001418 printk(KERN_INFO "input: %s as %s\n",
1419 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1420 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -07001421
Dmitry Torokhov80064792007-08-30 00:22:11 -04001422 error = mutex_lock_interruptible(&input_mutex);
1423 if (error) {
1424 device_del(&dev->dev);
1425 return error;
1426 }
1427
1428 list_add_tail(&dev->node, &input_dev_list);
1429
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001430 list_for_each_entry(handler, &input_handler_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001431 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001432
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001433 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001434
Dmitry Torokhov80064792007-08-30 00:22:11 -04001435 mutex_unlock(&input_mutex);
1436
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001437 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001438}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001439EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001440
Dmitry Torokhov80064792007-08-30 00:22:11 -04001441/**
1442 * input_unregister_device - unregister previously registered device
1443 * @dev: device to be unregistered
1444 *
1445 * This function unregisters an input device. Once device is unregistered
1446 * the caller should not try to access it as it may get freed at any moment.
1447 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001448void input_unregister_device(struct input_dev *dev)
1449{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001450 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001451
Dmitry Torokhov80064792007-08-30 00:22:11 -04001452 input_disconnect_device(dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001453
Dmitry Torokhov80064792007-08-30 00:22:11 -04001454 mutex_lock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001455
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001456 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001457 handle->handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001458 WARN_ON(!list_empty(&dev->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001459
Dmitry Torokhov80064792007-08-30 00:22:11 -04001460 del_timer_sync(&dev->timer);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001461 list_del_init(&dev->node);
1462
1463 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001464
1465 mutex_unlock(&input_mutex);
1466
1467 device_unregister(&dev->dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001468}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001469EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001470
Dmitry Torokhov80064792007-08-30 00:22:11 -04001471/**
1472 * input_register_handler - register a new input handler
1473 * @handler: handler to be registered
1474 *
1475 * This function registers a new input handler (interface) for input
1476 * devices in the system and attaches it to all input devices that
1477 * are compatible with the handler.
1478 */
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001479int input_register_handler(struct input_handler *handler)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001480{
1481 struct input_dev *dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001482 int retval;
1483
1484 retval = mutex_lock_interruptible(&input_mutex);
1485 if (retval)
1486 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001487
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001488 INIT_LIST_HEAD(&handler->h_list);
1489
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001490 if (handler->fops != NULL) {
Dmitry Torokhov80064792007-08-30 00:22:11 -04001491 if (input_table[handler->minor >> 5]) {
1492 retval = -EBUSY;
1493 goto out;
1494 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001495 input_table[handler->minor >> 5] = handler;
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001496 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001497
1498 list_add_tail(&handler->node, &input_handler_list);
1499
1500 list_for_each_entry(dev, &input_dev_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001501 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001502
1503 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001504
1505 out:
1506 mutex_unlock(&input_mutex);
1507 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001508}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001509EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001510
Dmitry Torokhov80064792007-08-30 00:22:11 -04001511/**
1512 * input_unregister_handler - unregisters an input handler
1513 * @handler: handler to be unregistered
1514 *
1515 * This function disconnects a handler from its input devices and
1516 * removes it from lists of known handlers.
1517 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001518void input_unregister_handler(struct input_handler *handler)
1519{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001520 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001521
Dmitry Torokhov80064792007-08-30 00:22:11 -04001522 mutex_lock(&input_mutex);
1523
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001524 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001525 handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001526 WARN_ON(!list_empty(&handler->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001527
1528 list_del_init(&handler->node);
1529
1530 if (handler->fops != NULL)
1531 input_table[handler->minor >> 5] = NULL;
1532
1533 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001534
1535 mutex_unlock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001536}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001537EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001538
Dmitry Torokhov80064792007-08-30 00:22:11 -04001539/**
1540 * input_register_handle - register a new input handle
1541 * @handle: handle to register
1542 *
1543 * This function puts a new input handle onto device's
1544 * and handler's lists so that events can flow through
1545 * it once it is opened using input_open_device().
1546 *
1547 * This function is supposed to be called from handler's
1548 * connect() method.
1549 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001550int input_register_handle(struct input_handle *handle)
1551{
1552 struct input_handler *handler = handle->handler;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001553 struct input_dev *dev = handle->dev;
1554 int error;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001555
Dmitry Torokhov80064792007-08-30 00:22:11 -04001556 /*
1557 * We take dev->mutex here to prevent race with
1558 * input_release_device().
1559 */
1560 error = mutex_lock_interruptible(&dev->mutex);
1561 if (error)
1562 return error;
1563 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1564 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001565
1566 /*
1567 * Since we are supposed to be called from ->connect()
1568 * which is mutually exclusive with ->disconnect()
1569 * we can't be racing with input_unregister_handle()
1570 * and so separate lock is not needed here.
1571 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001572 list_add_tail(&handle->h_node, &handler->h_list);
1573
1574 if (handler->start)
1575 handler->start(handle);
1576
1577 return 0;
1578}
1579EXPORT_SYMBOL(input_register_handle);
1580
Dmitry Torokhov80064792007-08-30 00:22:11 -04001581/**
1582 * input_unregister_handle - unregister an input handle
1583 * @handle: handle to unregister
1584 *
1585 * This function removes input handle from device's
1586 * and handler's lists.
1587 *
1588 * This function is supposed to be called from handler's
1589 * disconnect() method.
1590 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001591void input_unregister_handle(struct input_handle *handle)
1592{
Dmitry Torokhov80064792007-08-30 00:22:11 -04001593 struct input_dev *dev = handle->dev;
1594
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001595 list_del_init(&handle->h_node);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001596
1597 /*
1598 * Take dev->mutex to prevent race with input_release_device().
1599 */
1600 mutex_lock(&dev->mutex);
1601 list_del_rcu(&handle->d_node);
1602 mutex_unlock(&dev->mutex);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -04001603 synchronize_rcu();
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001604}
1605EXPORT_SYMBOL(input_unregister_handle);
1606
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001607static int input_open_file(struct inode *inode, struct file *file)
1608{
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001609 struct input_handler *handler;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001610 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001611 int err;
1612
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001613 lock_kernel();
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001614 /* No load-on-demand here? */
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001615 handler = input_table[iminor(inode) >> 5];
1616 if (!handler || !(new_fops = fops_get(handler->fops))) {
1617 err = -ENODEV;
1618 goto out;
1619 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001620
1621 /*
1622 * That's _really_ odd. Usually NULL ->open means "nothing special",
1623 * not "no device". Oh, well...
1624 */
1625 if (!new_fops->open) {
1626 fops_put(new_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001627 err = -ENODEV;
1628 goto out;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001629 }
1630 old_fops = file->f_op;
1631 file->f_op = new_fops;
1632
1633 err = new_fops->open(inode, file);
1634
1635 if (err) {
1636 fops_put(file->f_op);
1637 file->f_op = fops_get(old_fops);
1638 }
1639 fops_put(old_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001640out:
1641 unlock_kernel();
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001642 return err;
1643}
1644
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001645static const struct file_operations input_fops = {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001646 .owner = THIS_MODULE,
1647 .open = input_open_file,
1648};
1649
Henrik Rydberg61994a62009-04-28 07:45:31 -07001650static void __init input_init_abs_bypass(void)
1651{
1652 const unsigned int *p;
1653
1654 for (p = input_abs_bypass_init_data; *p; p++)
1655 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1656}
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658static int __init input_init(void)
1659{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001660 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Henrik Rydberg61994a62009-04-28 07:45:31 -07001662 input_init_abs_bypass();
1663
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001664 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001665 if (err) {
1666 printk(KERN_ERR "input: unable to register input_dev class\n");
1667 return err;
1668 }
1669
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001670 err = input_proc_init();
1671 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001672 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001673
1674 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1675 if (err) {
1676 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001677 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001679
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001680 return 0;
1681
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001682 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001683 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687static void __exit input_exit(void)
1688{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001689 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001691 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
1694subsys_initcall(input_init);
1695module_exit(input_exit);