blob: a31394c1eca818aa2b665c95203a9df5e0b60a69 [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
Dmitry Torokhovdf2d4632009-12-11 21:57:31 -0800299 * devices to report input events. See also input_inject_event().
300 *
301 * NOTE: input_event() may be safely used right after input device was
302 * allocated with input_allocate_device(), even before it is registered
303 * with input_register_device(), but the event will not reach any of the
304 * input handlers. Such early invocation of input_event() may be used
305 * to 'seed' initial state of a switch or initial position of absolute
306 * axis, etc.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400307 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400308void input_event(struct input_dev *dev,
309 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400311 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dmitry Torokhov80064792007-08-30 00:22:11 -0400313 if (is_event_supported(type, dev->evbit, EV_MAX)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Dmitry Torokhov80064792007-08-30 00:22:11 -0400315 spin_lock_irqsave(&dev->event_lock, flags);
316 add_input_randomness(type, code, value);
317 input_handle_event(dev, type, code, value);
318 spin_unlock_irqrestore(&dev->event_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400321EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400323/**
324 * input_inject_event() - send input event from input handler
325 * @handle: input handle to send event through
326 * @type: type of the event
327 * @code: event code
328 * @value: value of the event
329 *
Dmitry Torokhov80064792007-08-30 00:22:11 -0400330 * Similar to input_event() but will ignore event if device is
331 * "grabbed" and handle injecting event is not the one that owns
332 * the device.
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400333 */
Dmitry Torokhov80064792007-08-30 00:22:11 -0400334void input_inject_event(struct input_handle *handle,
335 unsigned int type, unsigned int code, int value)
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400336{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400337 struct input_dev *dev = handle->dev;
338 struct input_handle *grab;
339 unsigned long flags;
340
341 if (is_event_supported(type, dev->evbit, EV_MAX)) {
342 spin_lock_irqsave(&dev->event_lock, flags);
343
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400344 rcu_read_lock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400345 grab = rcu_dereference(dev->grab);
346 if (!grab || grab == handle)
347 input_handle_event(dev, type, code, value);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400348 rcu_read_unlock();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400349
350 spin_unlock_irqrestore(&dev->event_lock, flags);
351 }
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400352}
353EXPORT_SYMBOL(input_inject_event);
354
Dmitry Torokhov80064792007-08-30 00:22:11 -0400355/**
356 * input_grab_device - grabs device for exclusive use
357 * @handle: input handle that wants to own the device
358 *
359 * When a device is grabbed by an input handle all events generated by
360 * the device are delivered only to this handle. Also events injected
361 * by other input handles are ignored while device is grabbed.
362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363int input_grab_device(struct input_handle *handle)
364{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400365 struct input_dev *dev = handle->dev;
366 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Dmitry Torokhov80064792007-08-30 00:22:11 -0400368 retval = mutex_lock_interruptible(&dev->mutex);
369 if (retval)
370 return retval;
371
372 if (dev->grab) {
373 retval = -EBUSY;
374 goto out;
375 }
376
377 rcu_assign_pointer(dev->grab, handle);
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400378 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400379
380 out:
381 mutex_unlock(&dev->mutex);
382 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400384EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dmitry Torokhov80064792007-08-30 00:22:11 -0400386static void __input_release_device(struct input_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400388 struct input_dev *dev = handle->dev;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400389
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400390 if (dev->grab == handle) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400391 rcu_assign_pointer(dev->grab, NULL);
392 /* Make sure input_pass_event() notices that grab is gone */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400393 synchronize_rcu();
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400394
395 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400396 if (handle->open && handle->handler->start)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400397 handle->handler->start(handle);
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
Dmitry Torokhov80064792007-08-30 00:22:11 -0400400
401/**
402 * input_release_device - release previously grabbed device
403 * @handle: input handle that owns the device
404 *
405 * Releases previously grabbed device so that other input handles can
406 * start receiving input events. Upon release all handlers attached
407 * to the device have their start() method called so they have a change
408 * to synchronize device state with the rest of the system.
409 */
410void input_release_device(struct input_handle *handle)
411{
412 struct input_dev *dev = handle->dev;
413
414 mutex_lock(&dev->mutex);
415 __input_release_device(handle);
416 mutex_unlock(&dev->mutex);
417}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400418EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Dmitry Torokhov80064792007-08-30 00:22:11 -0400420/**
421 * input_open_device - open input device
422 * @handle: handle through which device is being accessed
423 *
424 * This function should be called by input handlers when they
425 * want to start receive events from given input device.
426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427int input_open_device(struct input_handle *handle)
428{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500429 struct input_dev *dev = handle->dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400430 int retval;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500431
Dmitry Torokhov80064792007-08-30 00:22:11 -0400432 retval = mutex_lock_interruptible(&dev->mutex);
433 if (retval)
434 return retval;
435
436 if (dev->going_away) {
437 retval = -ENODEV;
438 goto out;
439 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500442
443 if (!dev->users++ && dev->open)
Dmitry Torokhov80064792007-08-30 00:22:11 -0400444 retval = dev->open(dev);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500445
Dmitry Torokhov80064792007-08-30 00:22:11 -0400446 if (retval) {
447 dev->users--;
448 if (!--handle->open) {
449 /*
450 * Make sure we are not delivering any more events
451 * through this handle
452 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400453 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400454 }
455 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500456
Dmitry Torokhov80064792007-08-30 00:22:11 -0400457 out:
Jes Sorensene676c232006-02-19 00:21:46 -0500458 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400459 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400461EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Dmitry Torokhov80064792007-08-30 00:22:11 -0400463int input_flush_device(struct input_handle *handle, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Dmitry Torokhov80064792007-08-30 00:22:11 -0400465 struct input_dev *dev = handle->dev;
466 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dmitry Torokhov80064792007-08-30 00:22:11 -0400468 retval = mutex_lock_interruptible(&dev->mutex);
469 if (retval)
470 return retval;
471
472 if (dev->flush)
473 retval = dev->flush(dev, file);
474
475 mutex_unlock(&dev->mutex);
476 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400478EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Dmitry Torokhov80064792007-08-30 00:22:11 -0400480/**
481 * input_close_device - close input device
482 * @handle: handle through which device is being accessed
483 *
484 * This function should be called by input handlers when they
485 * want to stop receive events from given input device.
486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487void input_close_device(struct input_handle *handle)
488{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500489 struct input_dev *dev = handle->dev;
490
Jes Sorensene676c232006-02-19 00:21:46 -0500491 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500492
Dmitry Torokhov80064792007-08-30 00:22:11 -0400493 __input_release_device(handle);
494
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500495 if (!--dev->users && dev->close)
496 dev->close(dev);
Dmitry Torokhov80064792007-08-30 00:22:11 -0400497
498 if (!--handle->open) {
499 /*
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400500 * synchronize_rcu() makes sure that input_pass_event()
Dmitry Torokhov80064792007-08-30 00:22:11 -0400501 * completed and that no more input events are delivered
502 * through this handle
503 */
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -0400504 synchronize_rcu();
Dmitry Torokhov80064792007-08-30 00:22:11 -0400505 }
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500506
Jes Sorensene676c232006-02-19 00:21:46 -0500507 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400509EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Dmitry Torokhov80064792007-08-30 00:22:11 -0400511/*
512 * Prepare device for unregistering
513 */
514static void input_disconnect_device(struct input_dev *dev)
515{
516 struct input_handle *handle;
517 int code;
518
519 /*
520 * Mark device as going away. Note that we take dev->mutex here
521 * not to protect access to dev->going_away but rather to ensure
522 * that there are no threads in the middle of input_open_device()
523 */
524 mutex_lock(&dev->mutex);
Dmitry Torokhovffd0db92009-09-16 01:06:43 -0700525 dev->going_away = true;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400526 mutex_unlock(&dev->mutex);
527
528 spin_lock_irq(&dev->event_lock);
529
530 /*
531 * Simulate keyup events for all pressed keys so that handlers
532 * are not left with "stuck" keys. The driver may continue
533 * generate events even after we done here but they will not
534 * reach any handlers.
535 */
536 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
537 for (code = 0; code <= KEY_MAX; code++) {
538 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400539 __test_and_clear_bit(code, dev->key)) {
Dmitry Torokhov80064792007-08-30 00:22:11 -0400540 input_pass_event(dev, EV_KEY, code, 0);
541 }
542 }
543 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
544 }
545
546 list_for_each_entry(handle, &dev->h_list, d_node)
547 handle->open = 0;
548
549 spin_unlock_irq(&dev->event_lock);
550}
551
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400552static int input_fetch_keycode(struct input_dev *dev, int scancode)
553{
554 switch (dev->keycodesize) {
555 case 1:
556 return ((u8 *)dev->keycode)[scancode];
557
558 case 2:
559 return ((u16 *)dev->keycode)[scancode];
560
561 default:
562 return ((u32 *)dev->keycode)[scancode];
563 }
564}
565
566static int input_default_getkeycode(struct input_dev *dev,
567 int scancode, int *keycode)
568{
569 if (!dev->keycodesize)
570 return -EINVAL;
571
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400572 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400573 return -EINVAL;
574
575 *keycode = input_fetch_keycode(dev, scancode);
576
577 return 0;
578}
579
580static int input_default_setkeycode(struct input_dev *dev,
581 int scancode, int keycode)
582{
583 int old_keycode;
584 int i;
585
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400586 if (scancode >= dev->keycodemax)
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400587 return -EINVAL;
588
589 if (!dev->keycodesize)
590 return -EINVAL;
591
592 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
593 return -EINVAL;
594
595 switch (dev->keycodesize) {
596 case 1: {
597 u8 *k = (u8 *)dev->keycode;
598 old_keycode = k[scancode];
599 k[scancode] = keycode;
600 break;
601 }
602 case 2: {
603 u16 *k = (u16 *)dev->keycode;
604 old_keycode = k[scancode];
605 k[scancode] = keycode;
606 break;
607 }
608 default: {
609 u32 *k = (u32 *)dev->keycode;
610 old_keycode = k[scancode];
611 k[scancode] = keycode;
612 break;
613 }
614 }
615
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800616 __clear_bit(old_keycode, dev->keybit);
617 __set_bit(keycode, dev->keybit);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400618
619 for (i = 0; i < dev->keycodemax; i++) {
620 if (input_fetch_keycode(dev, i) == old_keycode) {
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800621 __set_bit(old_keycode, dev->keybit);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400622 break; /* Setting the bit twice is useless, so break */
623 }
624 }
625
626 return 0;
627}
628
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400629/**
630 * input_get_keycode - retrieve keycode currently mapped to a given scancode
631 * @dev: input device which keymap is being queried
632 * @scancode: scancode (or its equivalent for device in question) for which
633 * keycode is needed
634 * @keycode: result
635 *
636 * This function should be called by anyone interested in retrieving current
637 * keymap. Presently keyboard and evdev handlers use it.
638 */
639int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
640{
641 if (scancode < 0)
642 return -EINVAL;
643
644 return dev->getkeycode(dev, scancode, keycode);
645}
646EXPORT_SYMBOL(input_get_keycode);
647
648/**
649 * input_get_keycode - assign new keycode to a given scancode
650 * @dev: input device which keymap is being updated
651 * @scancode: scancode (or its equivalent for device in question)
652 * @keycode: new keycode to be assigned to the scancode
653 *
654 * This function should be called by anyone needing to update current
655 * keymap. Presently keyboard and evdev handlers use it.
656 */
657int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
658{
659 unsigned long flags;
660 int old_keycode;
661 int retval;
662
663 if (scancode < 0)
664 return -EINVAL;
665
666 if (keycode < 0 || keycode > KEY_MAX)
667 return -EINVAL;
668
669 spin_lock_irqsave(&dev->event_lock, flags);
670
671 retval = dev->getkeycode(dev, scancode, &old_keycode);
672 if (retval)
673 goto out;
674
675 retval = dev->setkeycode(dev, scancode, keycode);
676 if (retval)
677 goto out;
678
Dmitry Torokhov4f93df42010-01-05 17:56:00 -0800679 /* Make sure KEY_RESERVED did not get enabled. */
680 __clear_bit(KEY_RESERVED, dev->keybit);
681
Dmitry Torokhovf4f37c82007-11-04 00:41:12 -0400682 /*
683 * Simulate keyup event if keycode is not present
684 * in the keymap anymore
685 */
686 if (test_bit(EV_KEY, dev->evbit) &&
687 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
688 __test_and_clear_bit(old_keycode, dev->key)) {
689
690 input_pass_event(dev, EV_KEY, old_keycode, 0);
691 if (dev->sync)
692 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
693 }
694
695 out:
696 spin_unlock_irqrestore(&dev->event_lock, flags);
697
698 return retval;
699}
700EXPORT_SYMBOL(input_set_keycode);
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702#define MATCH_BIT(bit, max) \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700703 for (i = 0; i < BITS_TO_LONGS(max); i++) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
705 break; \
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700706 if (i != BITS_TO_LONGS(max)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 continue;
708
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400709static const struct input_device_id *input_match_device(const struct input_device_id *id,
710 struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 int i;
713
714 for (; id->flags || id->driver_info; id++) {
715
716 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400717 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 continue;
719
720 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400721 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 continue;
723
724 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400725 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 continue;
727
728 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400729 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 continue;
731
732 MATCH_BIT(evbit, EV_MAX);
733 MATCH_BIT(keybit, KEY_MAX);
734 MATCH_BIT(relbit, REL_MAX);
735 MATCH_BIT(absbit, ABS_MAX);
736 MATCH_BIT(mscbit, MSC_MAX);
737 MATCH_BIT(ledbit, LED_MAX);
738 MATCH_BIT(sndbit, SND_MAX);
739 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500740 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 return id;
743 }
744
745 return NULL;
746}
747
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400748static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
749{
750 const struct input_device_id *id;
751 int error;
752
753 if (handler->blacklist && input_match_device(handler->blacklist, dev))
754 return -ENODEV;
755
756 id = input_match_device(handler->id_table, dev);
757 if (!id)
758 return -ENODEV;
759
760 error = handler->connect(handler, dev, id);
761 if (error && error != -ENODEV)
762 printk(KERN_ERR
763 "input: failed to attach handler %s to device %s, "
764 "error: %d\n",
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400765 handler->name, kobject_name(&dev->dev.kobj), error);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400766
767 return error;
768}
769
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500772
773static struct proc_dir_entry *proc_bus_input_dir;
774static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
775static int input_devices_state;
776
777static inline void input_wakeup_procfs_readers(void)
778{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 input_devices_state++;
780 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500783static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500785 poll_wait(file, &input_devices_poll_wait, wait);
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800786 if (file->f_version != input_devices_state) {
787 file->f_version = input_devices_state;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500788 return POLLIN | POLLRDNORM;
Dmitry Torokhovfa886612009-03-04 00:52:20 -0800789 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400790
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700794union input_seq_state {
795 struct {
796 unsigned short pos;
797 bool mutex_acquired;
798 };
799 void *p;
800};
801
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500802static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
803{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700804 union input_seq_state *state = (union input_seq_state *)&seq->private;
805 int error;
806
807 /* We need to fit into seq->private pointer */
808 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
809
810 error = mutex_lock_interruptible(&input_mutex);
811 if (error) {
812 state->mutex_acquired = false;
813 return ERR_PTR(error);
814 }
815
816 state->mutex_acquired = true;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500817
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400818 return seq_list_start(&input_dev_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500819}
820
821static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
822{
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400823 return seq_list_next(v, &input_dev_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500824}
825
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700826static void input_seq_stop(struct seq_file *seq, void *v)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500827{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700828 union input_seq_state *state = (union input_seq_state *)&seq->private;
829
830 if (state->mutex_acquired)
831 mutex_unlock(&input_mutex);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500832}
833
834static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
835 unsigned long *bitmap, int max)
836{
837 int i;
838
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700839 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500840 if (bitmap[i])
841 break;
842
843 seq_printf(seq, "B: %s=", name);
844 for (; i >= 0; i--)
845 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
846 seq_putc(seq, '\n');
847}
848
849static int input_devices_seq_show(struct seq_file *seq, void *v)
850{
851 struct input_dev *dev = container_of(v, struct input_dev, node);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400852 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct input_handle *handle;
854
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500855 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
856 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500858 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
859 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
860 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
Dmitry Torokhov15e03ae2007-03-07 23:20:17 -0500861 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500862 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500864 list_for_each_entry(handle, &dev->h_list, d_node)
865 seq_printf(seq, "%s ", handle->name);
866 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500867
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500868 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
869 if (test_bit(EV_KEY, dev->evbit))
870 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
871 if (test_bit(EV_REL, dev->evbit))
872 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
873 if (test_bit(EV_ABS, dev->evbit))
874 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
875 if (test_bit(EV_MSC, dev->evbit))
876 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
877 if (test_bit(EV_LED, dev->evbit))
878 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
879 if (test_bit(EV_SND, dev->evbit))
880 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
881 if (test_bit(EV_FF, dev->evbit))
882 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
883 if (test_bit(EV_SW, dev->evbit))
884 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500886 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500888 kfree(path);
889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Jan Engelhardtcec69c32008-01-31 00:43:32 -0500892static const struct seq_operations input_devices_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500893 .start = input_devices_seq_start,
894 .next = input_devices_seq_next,
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700895 .stop = input_seq_stop,
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500896 .show = input_devices_seq_show,
897};
898
899static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500901 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800904static const struct file_operations input_devices_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500905 .owner = THIS_MODULE,
906 .open = input_proc_devices_open,
907 .poll = input_proc_devices_poll,
908 .read = seq_read,
909 .llseek = seq_lseek,
910 .release = seq_release,
911};
912
913static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
914{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700915 union input_seq_state *state = (union input_seq_state *)&seq->private;
916 int error;
Dmitry Torokhov80064792007-08-30 00:22:11 -0400917
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700918 /* We need to fit into seq->private pointer */
919 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
920
921 error = mutex_lock_interruptible(&input_mutex);
922 if (error) {
923 state->mutex_acquired = false;
924 return ERR_PTR(error);
925 }
926
927 state->mutex_acquired = true;
928 state->pos = *pos;
929
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400930 return seq_list_start(&input_handler_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500931}
932
933static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
934{
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700935 union input_seq_state *state = (union input_seq_state *)&seq->private;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500936
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700937 state->pos = *pos + 1;
938 return seq_list_next(v, &input_handler_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500939}
940
941static int input_handlers_seq_show(struct seq_file *seq, void *v)
942{
943 struct input_handler *handler = container_of(v, struct input_handler, node);
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700944 union input_seq_state *state = (union input_seq_state *)&seq->private;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500945
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700946 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500947 if (handler->fops)
948 seq_printf(seq, " Minor=%d", handler->minor);
949 seq_putc(seq, '\n');
950
951 return 0;
952}
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700953
Jan Engelhardtcec69c32008-01-31 00:43:32 -0500954static const struct seq_operations input_handlers_seq_ops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500955 .start = input_handlers_seq_start,
956 .next = input_handlers_seq_next,
Dmitry Torokhov1572ca22009-10-13 23:37:30 -0700957 .stop = input_seq_stop,
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500958 .show = input_handlers_seq_show,
959};
960
961static int input_proc_handlers_open(struct inode *inode, struct file *file)
962{
963 return seq_open(file, &input_handlers_seq_ops);
964}
965
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800966static const struct file_operations input_handlers_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500967 .owner = THIS_MODULE,
968 .open = input_proc_handlers_open,
969 .read = seq_read,
970 .llseek = seq_lseek,
971 .release = seq_release,
972};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static int __init input_proc_init(void)
975{
976 struct proc_dir_entry *entry;
977
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700978 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500979 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500981
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700982 entry = proc_create("devices", 0, proc_bus_input_dir,
983 &input_devices_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500984 if (!entry)
985 goto fail1;
986
Denis V. Lunevc7705f32008-04-29 01:02:35 -0700987 entry = proc_create("handlers", 0, proc_bus_input_dir,
988 &input_handlers_fileops);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500989 if (!entry)
990 goto fail2;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500993
994 fail2: remove_proc_entry("devices", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700995 fail1: remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500996 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500998
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500999static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001000{
1001 remove_proc_entry("devices", proc_bus_input_dir);
1002 remove_proc_entry("handlers", proc_bus_input_dir);
Alexey Dobriyan9c370662008-04-29 01:01:41 -07001003 remove_proc_entry("bus/input", NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001007static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001009static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010#endif
1011
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001012#define INPUT_DEV_STRING_ATTR_SHOW(name) \
1013static ssize_t input_dev_show_##name(struct device *dev, \
1014 struct device_attribute *attr, \
1015 char *buf) \
1016{ \
1017 struct input_dev *input_dev = to_input_dev(dev); \
1018 \
1019 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1020 input_dev->name ? input_dev->name : ""); \
1021} \
1022static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001023
1024INPUT_DEV_STRING_ATTR_SHOW(name);
1025INPUT_DEV_STRING_ATTR_SHOW(phys);
1026INPUT_DEV_STRING_ATTR_SHOW(uniq);
1027
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001028static int input_print_modalias_bits(char *buf, int size,
1029 char name, unsigned long *bm,
1030 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001031{
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001032 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001033
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001034 len += snprintf(buf, max(size, 0), "%c", name);
1035 for (i = min_bit; i < max_bit; i++)
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001036 if (bm[BIT_WORD(i)] & BIT_MASK(i))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001037 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001038 return len;
1039}
1040
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001041static int input_print_modalias(char *buf, int size, struct input_dev *id,
1042 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001043{
1044 int len;
1045
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001046 len = snprintf(buf, max(size, 0),
1047 "input:b%04Xv%04Xp%04Xe%04X-",
1048 id->id.bustype, id->id.vendor,
1049 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001050
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001051 len += input_print_modalias_bits(buf + len, size - len,
1052 'e', id->evbit, 0, EV_MAX);
1053 len += input_print_modalias_bits(buf + len, size - len,
1054 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1055 len += input_print_modalias_bits(buf + len, size - len,
1056 'r', id->relbit, 0, REL_MAX);
1057 len += input_print_modalias_bits(buf + len, size - len,
1058 'a', id->absbit, 0, ABS_MAX);
1059 len += input_print_modalias_bits(buf + len, size - len,
1060 'm', id->mscbit, 0, MSC_MAX);
1061 len += input_print_modalias_bits(buf + len, size - len,
1062 'l', id->ledbit, 0, LED_MAX);
1063 len += input_print_modalias_bits(buf + len, size - len,
1064 's', id->sndbit, 0, SND_MAX);
1065 len += input_print_modalias_bits(buf + len, size - len,
1066 'f', id->ffbit, 0, FF_MAX);
1067 len += input_print_modalias_bits(buf + len, size - len,
1068 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001069
1070 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001071 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001072
Rusty Russell1d8f4302005-12-07 21:40:34 +01001073 return len;
1074}
1075
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001076static ssize_t input_dev_show_modalias(struct device *dev,
1077 struct device_attribute *attr,
1078 char *buf)
Rusty Russell1d8f4302005-12-07 21:40:34 +01001079{
1080 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +01001081 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +01001082
Dmitry Torokhov2db66872006-04-02 00:09:26 -05001083 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1084
Richard Purdie8a3cf452006-06-26 01:48:21 -04001085 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001086}
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001087static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
Rusty Russell1d8f4302005-12-07 21:40:34 +01001088
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001089static struct attribute *input_dev_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001090 &dev_attr_name.attr,
1091 &dev_attr_phys.attr,
1092 &dev_attr_uniq.attr,
1093 &dev_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001094 NULL
1095};
1096
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001097static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -07001098 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001099};
1100
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001101#define INPUT_DEV_ID_ATTR(name) \
1102static ssize_t input_dev_show_id_##name(struct device *dev, \
1103 struct device_attribute *attr, \
1104 char *buf) \
1105{ \
1106 struct input_dev *input_dev = to_input_dev(dev); \
1107 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1108} \
1109static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001110
1111INPUT_DEV_ID_ATTR(bustype);
1112INPUT_DEV_ID_ATTR(vendor);
1113INPUT_DEV_ID_ATTR(product);
1114INPUT_DEV_ID_ATTR(version);
1115
1116static struct attribute *input_dev_id_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001117 &dev_attr_bustype.attr,
1118 &dev_attr_vendor.attr,
1119 &dev_attr_product.attr,
1120 &dev_attr_version.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001121 NULL
1122};
1123
1124static struct attribute_group input_dev_id_attr_group = {
1125 .name = "id",
1126 .attrs = input_dev_id_attrs,
1127};
1128
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001129static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1130 int max, int add_cr)
1131{
1132 int i;
1133 int len = 0;
1134
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001135 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
Dmitry Torokhov969b21c2006-04-02 00:09:34 -05001136 if (bitmap[i])
1137 break;
1138
1139 for (; i >= 0; i--)
1140 len += snprintf(buf + len, max(buf_size - len, 0),
1141 "%lx%s", bitmap[i], i > 0 ? " " : "");
1142
1143 if (add_cr)
1144 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1145
1146 return len;
1147}
1148
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001149#define INPUT_DEV_CAP_ATTR(ev, bm) \
1150static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1151 struct device_attribute *attr, \
1152 char *buf) \
1153{ \
1154 struct input_dev *input_dev = to_input_dev(dev); \
1155 int len = input_print_bitmap(buf, PAGE_SIZE, \
1156 input_dev->bm##bit, ev##_MAX, 1); \
1157 return min_t(int, len, PAGE_SIZE); \
1158} \
1159static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001160
1161INPUT_DEV_CAP_ATTR(EV, ev);
1162INPUT_DEV_CAP_ATTR(KEY, key);
1163INPUT_DEV_CAP_ATTR(REL, rel);
1164INPUT_DEV_CAP_ATTR(ABS, abs);
1165INPUT_DEV_CAP_ATTR(MSC, msc);
1166INPUT_DEV_CAP_ATTR(LED, led);
1167INPUT_DEV_CAP_ATTR(SND, snd);
1168INPUT_DEV_CAP_ATTR(FF, ff);
1169INPUT_DEV_CAP_ATTR(SW, sw);
1170
1171static struct attribute *input_dev_caps_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001172 &dev_attr_ev.attr,
1173 &dev_attr_key.attr,
1174 &dev_attr_rel.attr,
1175 &dev_attr_abs.attr,
1176 &dev_attr_msc.attr,
1177 &dev_attr_led.attr,
1178 &dev_attr_snd.attr,
1179 &dev_attr_ff.attr,
1180 &dev_attr_sw.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -05001181 NULL
1182};
1183
1184static struct attribute_group input_dev_caps_attr_group = {
1185 .name = "capabilities",
1186 .attrs = input_dev_caps_attrs,
1187};
1188
David Brownella4dbd672009-06-24 10:06:31 -07001189static const struct attribute_group *input_dev_attr_groups[] = {
Dmitry Torokhovcb9def42007-03-07 23:20:26 -05001190 &input_dev_attr_group,
1191 &input_dev_id_attr_group,
1192 &input_dev_caps_attr_group,
1193 NULL
1194};
1195
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001196static void input_dev_release(struct device *device)
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001197{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001198 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001199
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001200 input_ff_destroy(dev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001201 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -04001202
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001203 module_put(THIS_MODULE);
1204}
1205
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001206/*
Kay Sievers312c0042005-11-16 09:00:00 +01001207 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001208 * device bitfields.
1209 */
Kay Sievers7eff2e72007-08-14 15:15:12 +02001210static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001211 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001212{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001213 int len;
1214
1215 if (add_uevent_var(env, "%s=", name))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001216 return -ENOMEM;
1217
Kay Sievers7eff2e72007-08-14 15:15:12 +02001218 len = input_print_bitmap(&env->buf[env->buflen - 1],
1219 sizeof(env->buf) - env->buflen,
1220 bitmap, max, 0);
1221 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001222 return -ENOMEM;
1223
Kay Sievers7eff2e72007-08-14 15:15:12 +02001224 env->buflen += len;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001225 return 0;
1226}
1227
Kay Sievers7eff2e72007-08-14 15:15:12 +02001228static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001229 struct input_dev *dev)
1230{
Kay Sievers7eff2e72007-08-14 15:15:12 +02001231 int len;
1232
1233 if (add_uevent_var(env, "MODALIAS="))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001234 return -ENOMEM;
1235
Kay Sievers7eff2e72007-08-14 15:15:12 +02001236 len = input_print_modalias(&env->buf[env->buflen - 1],
1237 sizeof(env->buf) - env->buflen,
1238 dev, 0);
1239 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001240 return -ENOMEM;
1241
Kay Sievers7eff2e72007-08-14 15:15:12 +02001242 env->buflen += len;
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001243 return 0;
1244}
1245
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001246#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1247 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001248 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001249 if (err) \
1250 return err; \
1251 } while (0)
1252
1253#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1254 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001255 int err = input_add_uevent_bm_var(env, name, bm, max); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001256 if (err) \
1257 return err; \
1258 } while (0)
1259
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001260#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1261 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +02001262 int err = input_add_uevent_modalias_var(env, dev); \
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001263 if (err) \
1264 return err; \
1265 } while (0)
1266
Kay Sievers7eff2e72007-08-14 15:15:12 +02001267static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001268{
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001269 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001270
1271 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1272 dev->id.bustype, dev->id.vendor,
1273 dev->id.product, dev->id.version);
1274 if (dev->name)
1275 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1276 if (dev->phys)
1277 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -08001278 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001279 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1280
1281 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1282 if (test_bit(EV_KEY, dev->evbit))
1283 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1284 if (test_bit(EV_REL, dev->evbit))
1285 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1286 if (test_bit(EV_ABS, dev->evbit))
1287 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1288 if (test_bit(EV_MSC, dev->evbit))
1289 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1290 if (test_bit(EV_LED, dev->evbit))
1291 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1292 if (test_bit(EV_SND, dev->evbit))
1293 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1294 if (test_bit(EV_FF, dev->evbit))
1295 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1296 if (test_bit(EV_SW, dev->evbit))
1297 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1298
Dmitry Torokhovac648a62006-04-02 00:09:51 -05001299 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -05001300
1301 return 0;
1302}
1303
Dmitry Torokhov3cc96352009-11-12 23:19:05 -08001304#define INPUT_DO_TOGGLE(dev, type, bits, on) \
1305 do { \
1306 int i; \
1307 bool active; \
1308 \
1309 if (!test_bit(EV_##type, dev->evbit)) \
1310 break; \
1311 \
1312 for (i = 0; i < type##_MAX; i++) { \
1313 if (!test_bit(i, dev->bits##bit)) \
1314 continue; \
1315 \
1316 active = test_bit(i, dev->bits); \
1317 if (!active && !on) \
1318 continue; \
1319 \
1320 dev->event(dev, EV_##type, i, on ? active : 0); \
1321 } \
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001322 } while (0)
1323
Andrew Morton1c4115e2009-10-01 15:43:55 -07001324#ifdef CONFIG_PM
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001325static void input_dev_reset(struct input_dev *dev, bool activate)
1326{
1327 if (!dev->event)
1328 return;
1329
1330 INPUT_DO_TOGGLE(dev, LED, led, activate);
1331 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1332
1333 if (activate && test_bit(EV_REP, dev->evbit)) {
1334 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1335 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1336 }
1337}
1338
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001339static int input_dev_suspend(struct device *dev)
1340{
1341 struct input_dev *input_dev = to_input_dev(dev);
1342
1343 mutex_lock(&input_dev->mutex);
1344 input_dev_reset(input_dev, false);
1345 mutex_unlock(&input_dev->mutex);
1346
1347 return 0;
1348}
1349
1350static int input_dev_resume(struct device *dev)
1351{
1352 struct input_dev *input_dev = to_input_dev(dev);
1353
1354 mutex_lock(&input_dev->mutex);
1355 input_dev_reset(input_dev, true);
1356 mutex_unlock(&input_dev->mutex);
1357
1358 return 0;
1359}
1360
1361static const struct dev_pm_ops input_dev_pm_ops = {
1362 .suspend = input_dev_suspend,
1363 .resume = input_dev_resume,
1364 .poweroff = input_dev_suspend,
1365 .restore = input_dev_resume,
1366};
1367#endif /* CONFIG_PM */
1368
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001369static struct device_type input_dev_type = {
1370 .groups = input_dev_attr_groups,
1371 .release = input_dev_release,
1372 .uevent = input_dev_uevent,
Dmitry Torokhovffd0db92009-09-16 01:06:43 -07001373#ifdef CONFIG_PM
1374 .pm = &input_dev_pm_ops,
1375#endif
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001376};
1377
Kay Sieverse454cea2009-09-18 23:01:12 +02001378static char *input_devnode(struct device *dev, mode_t *mode)
Kay Sieversaa5ed632009-04-30 15:23:42 +02001379{
1380 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1381}
1382
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001383struct class input_class = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001384 .name = "input",
Kay Sieverse454cea2009-09-18 23:01:12 +02001385 .devnode = input_devnode,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001386};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001387EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001388
Dmitry Torokhov14471902006-11-02 23:26:55 -05001389/**
1390 * input_allocate_device - allocate memory for new input device
1391 *
1392 * Returns prepared struct input_dev or NULL.
1393 *
1394 * NOTE: Use input_free_device() to free devices that have not been
1395 * registered; input_unregister_device() should be used for already
1396 * registered devices.
1397 */
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001398struct input_dev *input_allocate_device(void)
1399{
1400 struct input_dev *dev;
1401
1402 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1403 if (dev) {
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001404 dev->dev.type = &input_dev_type;
1405 dev->dev.class = &input_class;
1406 device_initialize(&dev->dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001407 mutex_init(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001408 spin_lock_init(&dev->event_lock);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001409 INIT_LIST_HEAD(&dev->h_list);
1410 INIT_LIST_HEAD(&dev->node);
Dmitry Torokhov655816e2006-09-14 01:32:14 -04001411
1412 __module_get(THIS_MODULE);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001413 }
1414
1415 return dev;
1416}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001417EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001418
Dmitry Torokhov14471902006-11-02 23:26:55 -05001419/**
1420 * input_free_device - free memory occupied by input_dev structure
1421 * @dev: input device to free
1422 *
1423 * This function should only be used if input_register_device()
1424 * was not called yet or if it failed. Once device was registered
1425 * use input_unregister_device() and memory will be freed once last
Dmitry Torokhov80064792007-08-30 00:22:11 -04001426 * reference to the device is dropped.
Dmitry Torokhov14471902006-11-02 23:26:55 -05001427 *
1428 * Device should be allocated by input_allocate_device().
1429 *
1430 * NOTE: If there are references to the input device then memory
1431 * will not be freed until last reference is dropped.
1432 */
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001433void input_free_device(struct input_dev *dev)
1434{
Dmitry Torokhov54f9e362007-03-16 00:57:25 -04001435 if (dev)
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001436 input_put_device(dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001437}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001438EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001439
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001440/**
1441 * input_set_capability - mark device as capable of a certain event
1442 * @dev: device that is capable of emitting or accepting event
1443 * @type: type of the event (EV_KEY, EV_REL, etc...)
1444 * @code: event code
1445 *
1446 * In addition to setting up corresponding bit in appropriate capability
1447 * bitmap the function also adjusts dev->evbit.
1448 */
1449void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1450{
1451 switch (type) {
1452 case EV_KEY:
1453 __set_bit(code, dev->keybit);
1454 break;
1455
1456 case EV_REL:
1457 __set_bit(code, dev->relbit);
1458 break;
1459
1460 case EV_ABS:
1461 __set_bit(code, dev->absbit);
1462 break;
1463
1464 case EV_MSC:
1465 __set_bit(code, dev->mscbit);
1466 break;
1467
1468 case EV_SW:
1469 __set_bit(code, dev->swbit);
1470 break;
1471
1472 case EV_LED:
1473 __set_bit(code, dev->ledbit);
1474 break;
1475
1476 case EV_SND:
1477 __set_bit(code, dev->sndbit);
1478 break;
1479
1480 case EV_FF:
1481 __set_bit(code, dev->ffbit);
1482 break;
1483
Dmitry Baryshkov22d1c392007-12-14 01:21:03 -05001484 case EV_PWR:
1485 /* do nothing */
1486 break;
1487
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001488 default:
1489 printk(KERN_ERR
1490 "input_set_capability: unknown type %u (code %u)\n",
1491 type, code);
1492 dump_stack();
1493 return;
1494 }
1495
1496 __set_bit(type, dev->evbit);
1497}
1498EXPORT_SYMBOL(input_set_capability);
1499
Dmitry Torokhov92a3a582010-01-05 17:56:01 -08001500#define INPUT_CLEANSE_BITMASK(dev, type, bits) \
1501 do { \
1502 if (!test_bit(EV_##type, dev->evbit)) \
1503 memset(dev->bits##bit, 0, \
1504 sizeof(dev->bits##bit)); \
1505 } while (0)
1506
1507static void input_cleanse_bitmasks(struct input_dev *dev)
1508{
1509 INPUT_CLEANSE_BITMASK(dev, KEY, key);
1510 INPUT_CLEANSE_BITMASK(dev, REL, rel);
1511 INPUT_CLEANSE_BITMASK(dev, ABS, abs);
1512 INPUT_CLEANSE_BITMASK(dev, MSC, msc);
1513 INPUT_CLEANSE_BITMASK(dev, LED, led);
1514 INPUT_CLEANSE_BITMASK(dev, SND, snd);
1515 INPUT_CLEANSE_BITMASK(dev, FF, ff);
1516 INPUT_CLEANSE_BITMASK(dev, SW, sw);
1517}
1518
Dmitry Torokhov80064792007-08-30 00:22:11 -04001519/**
1520 * input_register_device - register device with input core
1521 * @dev: device to be registered
1522 *
1523 * This function registers device with input core. The device must be
1524 * allocated with input_allocate_device() and all it's capabilities
1525 * set up before registering.
1526 * If function fails the device must be freed with input_free_device().
1527 * Once device has been successfully registered it can be unregistered
1528 * with input_unregister_device(); input_free_device() should not be
1529 * called in this case.
1530 */
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001531int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001532{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001533 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001534 struct input_handler *handler;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001535 const char *path;
1536 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001537
Dmitry Torokhov4f93df42010-01-05 17:56:00 -08001538 /* Every input device generates EV_SYN/SYN_REPORT events. */
Dmitry Torokhov80064792007-08-30 00:22:11 -04001539 __set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001540
Dmitry Torokhov4f93df42010-01-05 17:56:00 -08001541 /* KEY_RESERVED is not supposed to be transmitted to userspace. */
1542 __clear_bit(KEY_RESERVED, dev->keybit);
1543
Dmitry Torokhov92a3a582010-01-05 17:56:01 -08001544 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
1545 input_cleanse_bitmasks(dev);
1546
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001547 /*
1548 * If delay and period are pre-set by the driver, then autorepeating
1549 * is handled by the driver itself and we don't do it in input.c.
1550 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001551 init_timer(&dev->timer);
1552 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1553 dev->timer.data = (long) dev;
1554 dev->timer.function = input_repeat_key;
1555 dev->rep[REP_DELAY] = 250;
1556 dev->rep[REP_PERIOD] = 33;
1557 }
1558
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -04001559 if (!dev->getkeycode)
1560 dev->getkeycode = input_default_getkeycode;
1561
1562 if (!dev->setkeycode)
1563 dev->setkeycode = input_default_setkeycode;
1564
Kay Sieversa6c24902008-10-30 00:07:50 -04001565 dev_set_name(&dev->dev, "input%ld",
1566 (unsigned long) atomic_inc_return(&input_no) - 1);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001567
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001568 error = device_add(&dev->dev);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001569 if (error)
1570 return error;
1571
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001572 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001573 printk(KERN_INFO "input: %s as %s\n",
1574 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1575 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -07001576
Dmitry Torokhov80064792007-08-30 00:22:11 -04001577 error = mutex_lock_interruptible(&input_mutex);
1578 if (error) {
1579 device_del(&dev->dev);
1580 return error;
1581 }
1582
1583 list_add_tail(&dev->node, &input_dev_list);
1584
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001585 list_for_each_entry(handler, &input_handler_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001586 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001587
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001588 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001589
Dmitry Torokhov80064792007-08-30 00:22:11 -04001590 mutex_unlock(&input_mutex);
1591
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001592 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001593}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001594EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001595
Dmitry Torokhov80064792007-08-30 00:22:11 -04001596/**
1597 * input_unregister_device - unregister previously registered device
1598 * @dev: device to be unregistered
1599 *
1600 * This function unregisters an input device. Once device is unregistered
1601 * the caller should not try to access it as it may get freed at any moment.
1602 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001603void input_unregister_device(struct input_dev *dev)
1604{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001605 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001606
Dmitry Torokhov80064792007-08-30 00:22:11 -04001607 input_disconnect_device(dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001608
Dmitry Torokhov80064792007-08-30 00:22:11 -04001609 mutex_lock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001610
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001611 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001612 handle->handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001613 WARN_ON(!list_empty(&dev->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001614
Dmitry Torokhov80064792007-08-30 00:22:11 -04001615 del_timer_sync(&dev->timer);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001616 list_del_init(&dev->node);
1617
1618 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001619
1620 mutex_unlock(&input_mutex);
1621
1622 device_unregister(&dev->dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001623}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001624EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001625
Dmitry Torokhov80064792007-08-30 00:22:11 -04001626/**
1627 * input_register_handler - register a new input handler
1628 * @handler: handler to be registered
1629 *
1630 * This function registers a new input handler (interface) for input
1631 * devices in the system and attaches it to all input devices that
1632 * are compatible with the handler.
1633 */
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001634int input_register_handler(struct input_handler *handler)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001635{
1636 struct input_dev *dev;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001637 int retval;
1638
1639 retval = mutex_lock_interruptible(&input_mutex);
1640 if (retval)
1641 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001642
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001643 INIT_LIST_HEAD(&handler->h_list);
1644
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001645 if (handler->fops != NULL) {
Dmitry Torokhov80064792007-08-30 00:22:11 -04001646 if (input_table[handler->minor >> 5]) {
1647 retval = -EBUSY;
1648 goto out;
1649 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001650 input_table[handler->minor >> 5] = handler;
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001651 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001652
1653 list_add_tail(&handler->node, &input_handler_list);
1654
1655 list_for_each_entry(dev, &input_dev_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001656 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001657
1658 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001659
1660 out:
1661 mutex_unlock(&input_mutex);
1662 return retval;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001663}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001664EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001665
Dmitry Torokhov80064792007-08-30 00:22:11 -04001666/**
1667 * input_unregister_handler - unregisters an input handler
1668 * @handler: handler to be unregistered
1669 *
1670 * This function disconnects a handler from its input devices and
1671 * removes it from lists of known handlers.
1672 */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001673void input_unregister_handler(struct input_handler *handler)
1674{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001675 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001676
Dmitry Torokhov80064792007-08-30 00:22:11 -04001677 mutex_lock(&input_mutex);
1678
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001679 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001680 handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001681 WARN_ON(!list_empty(&handler->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001682
1683 list_del_init(&handler->node);
1684
1685 if (handler->fops != NULL)
1686 input_table[handler->minor >> 5] = NULL;
1687
1688 input_wakeup_procfs_readers();
Dmitry Torokhov80064792007-08-30 00:22:11 -04001689
1690 mutex_unlock(&input_mutex);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001691}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001692EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001693
Dmitry Torokhov80064792007-08-30 00:22:11 -04001694/**
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001695 * input_handler_for_each_handle - handle iterator
1696 * @handler: input handler to iterate
1697 * @data: data for the callback
1698 * @fn: function to be called for each handle
1699 *
1700 * Iterate over @bus's list of devices, and call @fn for each, passing
1701 * it @data and stop when @fn returns a non-zero value. The function is
1702 * using RCU to traverse the list and therefore may be usind in atonic
1703 * contexts. The @fn callback is invoked from RCU critical section and
1704 * thus must not sleep.
1705 */
1706int input_handler_for_each_handle(struct input_handler *handler, void *data,
1707 int (*fn)(struct input_handle *, void *))
1708{
1709 struct input_handle *handle;
1710 int retval = 0;
1711
1712 rcu_read_lock();
1713
1714 list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
1715 retval = fn(handle, data);
1716 if (retval)
1717 break;
1718 }
1719
1720 rcu_read_unlock();
1721
1722 return retval;
1723}
1724EXPORT_SYMBOL(input_handler_for_each_handle);
1725
1726/**
Dmitry Torokhov80064792007-08-30 00:22:11 -04001727 * input_register_handle - register a new input handle
1728 * @handle: handle to register
1729 *
1730 * This function puts a new input handle onto device's
1731 * and handler's lists so that events can flow through
1732 * it once it is opened using input_open_device().
1733 *
1734 * This function is supposed to be called from handler's
1735 * connect() method.
1736 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001737int input_register_handle(struct input_handle *handle)
1738{
1739 struct input_handler *handler = handle->handler;
Dmitry Torokhov80064792007-08-30 00:22:11 -04001740 struct input_dev *dev = handle->dev;
1741 int error;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001742
Dmitry Torokhov80064792007-08-30 00:22:11 -04001743 /*
1744 * We take dev->mutex here to prevent race with
1745 * input_release_device().
1746 */
1747 error = mutex_lock_interruptible(&dev->mutex);
1748 if (error)
1749 return error;
1750 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1751 mutex_unlock(&dev->mutex);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001752
1753 /*
1754 * Since we are supposed to be called from ->connect()
1755 * which is mutually exclusive with ->disconnect()
1756 * we can't be racing with input_unregister_handle()
1757 * and so separate lock is not needed here.
1758 */
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001759 list_add_tail_rcu(&handle->h_node, &handler->h_list);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001760
1761 if (handler->start)
1762 handler->start(handle);
1763
1764 return 0;
1765}
1766EXPORT_SYMBOL(input_register_handle);
1767
Dmitry Torokhov80064792007-08-30 00:22:11 -04001768/**
1769 * input_unregister_handle - unregister an input handle
1770 * @handle: handle to unregister
1771 *
1772 * This function removes input handle from device's
1773 * and handler's lists.
1774 *
1775 * This function is supposed to be called from handler's
1776 * disconnect() method.
1777 */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001778void input_unregister_handle(struct input_handle *handle)
1779{
Dmitry Torokhov80064792007-08-30 00:22:11 -04001780 struct input_dev *dev = handle->dev;
1781
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001782 list_del_rcu(&handle->h_node);
Dmitry Torokhov80064792007-08-30 00:22:11 -04001783
1784 /*
1785 * Take dev->mutex to prevent race with input_release_device().
1786 */
1787 mutex_lock(&dev->mutex);
1788 list_del_rcu(&handle->d_node);
1789 mutex_unlock(&dev->mutex);
Dmitry Torokhov66d2a592009-12-01 21:54:35 -08001790
Dmitry Torokhov82ba56c2007-10-13 15:46:55 -04001791 synchronize_rcu();
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001792}
1793EXPORT_SYMBOL(input_unregister_handle);
1794
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001795static int input_open_file(struct inode *inode, struct file *file)
1796{
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001797 struct input_handler *handler;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001798 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001799 int err;
1800
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001801 lock_kernel();
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001802 /* No load-on-demand here? */
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001803 handler = input_table[iminor(inode) >> 5];
1804 if (!handler || !(new_fops = fops_get(handler->fops))) {
1805 err = -ENODEV;
1806 goto out;
1807 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001808
1809 /*
1810 * That's _really_ odd. Usually NULL ->open means "nothing special",
1811 * not "no device". Oh, well...
1812 */
1813 if (!new_fops->open) {
1814 fops_put(new_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001815 err = -ENODEV;
1816 goto out;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001817 }
1818 old_fops = file->f_op;
1819 file->f_op = new_fops;
1820
1821 err = new_fops->open(inode, file);
1822
1823 if (err) {
1824 fops_put(file->f_op);
1825 file->f_op = fops_get(old_fops);
1826 }
1827 fops_put(old_fops);
Jonathan Corbet2edbf852008-05-15 10:37:16 -06001828out:
1829 unlock_kernel();
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001830 return err;
1831}
1832
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001833static const struct file_operations input_fops = {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001834 .owner = THIS_MODULE,
1835 .open = input_open_file,
1836};
1837
Henrik Rydberg61994a62009-04-28 07:45:31 -07001838static void __init input_init_abs_bypass(void)
1839{
1840 const unsigned int *p;
1841
1842 for (p = input_abs_bypass_init_data; *p; p++)
1843 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1844}
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846static int __init input_init(void)
1847{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001848 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Henrik Rydberg61994a62009-04-28 07:45:31 -07001850 input_init_abs_bypass();
1851
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001852 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001853 if (err) {
1854 printk(KERN_ERR "input: unable to register input_dev class\n");
1855 return err;
1856 }
1857
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001858 err = input_proc_init();
1859 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001860 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001861
1862 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1863 if (err) {
1864 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001865 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001867
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001868 return 0;
1869
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001870 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001871 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001872 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
1875static void __exit input_exit(void)
1876{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001877 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001879 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882subsys_initcall(input_init);
1883module_exit(input_exit);