aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input
diff options
context:
space:
mode:
authorDmitry Torokhov <dmitry.torokhov@gmail.com>2007-08-30 00:22:11 -0400
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2007-08-30 00:22:11 -0400
commit8006479c9b75fb6594a7b746af3d7f1fbb68f18f (patch)
tree1c1cd28f3fec192fa3e15f3042b4f087b43806d4 /drivers/input
parent501cc54c4d2b0c2bbae1a6490b0e547be46fc09f (diff)
Input: implement proper locking in input core
Also add some kerneldoc documentation to input.h Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Diffstat (limited to 'drivers/input')
-rw-r--r--drivers/input/input.c666
1 files changed, 491 insertions, 175 deletions
diff --git a/drivers/input/input.c b/drivers/input/input.c
index 5fe755586623..c59544f7e232 100644
--- a/drivers/input/input.c
+++ b/drivers/input/input.c
@@ -17,10 +17,10 @@
17#include <linux/major.h> 17#include <linux/major.h>
18#include <linux/proc_fs.h> 18#include <linux/proc_fs.h>
19#include <linux/seq_file.h> 19#include <linux/seq_file.h>
20#include <linux/interrupt.h>
21#include <linux/poll.h> 20#include <linux/poll.h>
22#include <linux/device.h> 21#include <linux/device.h>
23#include <linux/mutex.h> 22#include <linux/mutex.h>
23#include <linux/rcupdate.h>
24 24
25MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); 25MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
26MODULE_DESCRIPTION("Input core"); 26MODULE_DESCRIPTION("Input core");
@@ -31,167 +31,244 @@ MODULE_LICENSE("GPL");
31static LIST_HEAD(input_dev_list); 31static LIST_HEAD(input_dev_list);
32static LIST_HEAD(input_handler_list); 32static LIST_HEAD(input_handler_list);
33 33
34/*
35 * input_mutex protects access to both input_dev_list and input_handler_list.
36 * This also causes input_[un]register_device and input_[un]register_handler
37 * be mutually exclusive which simplifies locking in drivers implementing
38 * input handlers.
39 */
40static DEFINE_MUTEX(input_mutex);
41
34static struct input_handler *input_table[8]; 42static struct input_handler *input_table[8];
35 43
36/** 44static inline int is_event_supported(unsigned int code,
37 * input_event() - report new input event 45 unsigned long *bm, unsigned int max)
38 * @dev: device that generated the event
39 * @type: type of the event
40 * @code: event code
41 * @value: value of the event
42 *
43 * This function should be used by drivers implementing various input devices
44 * See also input_inject_event()
45 */
46void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
47{ 46{
48 struct input_handle *handle; 47 return code <= max && test_bit(code, bm);
48}
49 49
50 if (type > EV_MAX || !test_bit(type, dev->evbit)) 50static int input_defuzz_abs_event(int value, int old_val, int fuzz)
51 return; 51{
52 if (fuzz) {
53 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
54 return old_val;
52 55
53 add_input_randomness(type, code, value); 56 if (value > old_val - fuzz && value < old_val + fuzz)
57 return (old_val * 3 + value) / 4;
54 58
55 switch (type) { 59 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
60 return (old_val + value) / 2;
61 }
56 62
57 case EV_SYN: 63 return value;
58 switch (code) { 64}
59 case SYN_CONFIG:
60 if (dev->event)
61 dev->event(dev, type, code, value);
62 break;
63
64 case SYN_REPORT:
65 if (dev->sync)
66 return;
67 dev->sync = 1;
68 break;
69 }
70 break;
71 65
72 case EV_KEY: 66/*
67 * Pass event through all open handles. This function is called with
68 * dev->event_lock held and interrupts disabled. Because of that we
69 * do not need to use rcu_read_lock() here although we are using RCU
70 * to access handle list. Note that because of that write-side uses
71 * synchronize_sched() instead of synchronize_ru().
72 */
73static void input_pass_event(struct input_dev *dev,
74 unsigned int type, unsigned int code, int value)
75{
76 struct input_handle *handle = rcu_dereference(dev->grab);
73 77
74 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) 78 if (handle)
75 return; 79 handle->handler->event(handle, type, code, value);
80 else
81 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
82 if (handle->open)
83 handle->handler->event(handle,
84 type, code, value);
85}
76 86
77 if (value == 2) 87/*
78 break; 88 * Generate software autorepeat event. Note that we take
89 * dev->event_lock here to avoid racing with input_event
90 * which may cause keys get "stuck".
91 */
92static void input_repeat_key(unsigned long data)
93{
94 struct input_dev *dev = (void *) data;
95 unsigned long flags;
79 96
80 change_bit(code, dev->key); 97 spin_lock_irqsave(&dev->event_lock, flags);
81 98
82 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) { 99 if (test_bit(dev->repeat_key, dev->key) &&
83 dev->repeat_key = code; 100 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
84 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
85 }
86 101
87 break; 102 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
88 103
89 case EV_SW: 104 if (dev->sync) {
105 /*
106 * Only send SYN_REPORT if we are not in a middle
107 * of driver parsing a new hardware packet.
108 * Otherwise assume that the driver will send
109 * SYN_REPORT once it's done.
110 */
111 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
112 }
90 113
91 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value) 114 if (dev->rep[REP_PERIOD])
92 return; 115 mod_timer(&dev->timer, jiffies +
116 msecs_to_jiffies(dev->rep[REP_PERIOD]));
117 }
93 118
94 change_bit(code, dev->sw); 119 spin_unlock_irqrestore(&dev->event_lock, flags);
120}
95 121
96 break; 122static void input_start_autorepeat(struct input_dev *dev, int code)
123{
124 if (test_bit(EV_REP, dev->evbit) &&
125 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
126 dev->timer.data) {
127 dev->repeat_key = code;
128 mod_timer(&dev->timer,
129 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
130 }
131}
97 132
98 case EV_ABS: 133#define INPUT_IGNORE_EVENT 0
134#define INPUT_PASS_TO_HANDLERS 1
135#define INPUT_PASS_TO_DEVICE 2
136#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
99 137
100 if (code > ABS_MAX || !test_bit(code, dev->absbit)) 138static void input_handle_event(struct input_dev *dev,
101 return; 139 unsigned int type, unsigned int code, int value)
140{
141 int disposition = INPUT_IGNORE_EVENT;
102 142
103 if (dev->absfuzz[code]) { 143 switch (type) {
104 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
105 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
106 return;
107 144
108 if ((value > dev->abs[code] - dev->absfuzz[code]) && 145 case EV_SYN:
109 (value < dev->abs[code] + dev->absfuzz[code])) 146 switch (code) {
110 value = (dev->abs[code] * 3 + value) >> 2; 147 case SYN_CONFIG:
148 disposition = INPUT_PASS_TO_ALL;
149 break;
111 150
112 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) && 151 case SYN_REPORT:
113 (value < dev->abs[code] + (dev->absfuzz[code] << 1))) 152 if (!dev->sync) {
114 value = (dev->abs[code] + value) >> 1; 153 dev->sync = 1;
154 disposition = INPUT_PASS_TO_HANDLERS;
115 } 155 }
116
117 if (dev->abs[code] == value)
118 return;
119
120 dev->abs[code] = value;
121 break; 156 break;
157 }
158 break;
122 159
123 case EV_REL: 160 case EV_KEY:
161 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
162 !!test_bit(code, dev->key) != value) {
124 163
125 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0)) 164 if (value != 2) {
126 return; 165 __change_bit(code, dev->key);
166 if (value)
167 input_start_autorepeat(dev, code);
168 }
127 169
128 break; 170 disposition = INPUT_PASS_TO_HANDLERS;
171 }
172 break;
129 173
130 case EV_MSC: 174 case EV_SW:
175 if (is_event_supported(code, dev->swbit, SW_MAX) &&
176 !!test_bit(code, dev->sw) != value) {
131 177
132 if (code > MSC_MAX || !test_bit(code, dev->mscbit)) 178 __change_bit(code, dev->sw);
133 return; 179 disposition = INPUT_PASS_TO_HANDLERS;
180 }
181 break;
134 182
135 if (dev->event) 183 case EV_ABS:
136 dev->event(dev, type, code, value); 184 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
137 185
138 break; 186 value = input_defuzz_abs_event(value,
187 dev->abs[code], dev->absfuzz[code]);
139 188
140 case EV_LED: 189 if (dev->abs[code] != value) {
190 dev->abs[code] = value;
191 disposition = INPUT_PASS_TO_HANDLERS;
192 }
193 }
194 break;
141 195
142 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value) 196 case EV_REL:
143 return; 197 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
198 disposition = INPUT_PASS_TO_HANDLERS;
144 199
145 change_bit(code, dev->led); 200 break;
146 201
147 if (dev->event) 202 case EV_MSC:
148 dev->event(dev, type, code, value); 203 if (is_event_supported(code, dev->mscbit, MSC_MAX))
204 disposition = INPUT_PASS_TO_ALL;
149 205
150 break; 206 break;
207
208 case EV_LED:
209 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
210 !!test_bit(code, dev->led) != value) {
151 211
152 case EV_SND: 212 __change_bit(code, dev->led);
213 disposition = INPUT_PASS_TO_ALL;
214 }
215 break;
153 216
154 if (code > SND_MAX || !test_bit(code, dev->sndbit)) 217 case EV_SND:
155 return; 218 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
156 219
157 if (!!test_bit(code, dev->snd) != !!value) 220 if (!!test_bit(code, dev->snd) != !!value)
158 change_bit(code, dev->snd); 221 __change_bit(code, dev->snd);
222 disposition = INPUT_PASS_TO_ALL;
223 }
224 break;
159 225
160 if (dev->event) 226 case EV_REP:
161 dev->event(dev, type, code, value); 227 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
228 dev->rep[code] = value;
229 disposition = INPUT_PASS_TO_ALL;
230 }
231 break;
162 232
163 break; 233 case EV_FF:
234 if (value >= 0)
235 disposition = INPUT_PASS_TO_ALL;
236 break;
237 }
164 238
165 case EV_REP: 239 if (type != EV_SYN)
240 dev->sync = 0;
166 241
167 if (code > REP_MAX || value < 0 || dev->rep[code] == value) 242 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
168 return; 243 dev->event(dev, type, code, value);
169 244
170 dev->rep[code] = value; 245 if (disposition & INPUT_PASS_TO_HANDLERS)
171 if (dev->event) 246 input_pass_event(dev, type, code, value);
172 dev->event(dev, type, code, value); 247}
173 248
174 break; 249/**
250 * input_event() - report new input event
251 * @dev: device that generated the event
252 * @type: type of the event
253 * @code: event code
254 * @value: value of the event
255 *
256 * This function should be used by drivers implementing various input
257 * devices. See also input_inject_event().
258 */
175 259
176 case EV_FF: 260void input_event(struct input_dev *dev,
261 unsigned int type, unsigned int code, int value)
262{
263 unsigned long flags;
177 264
178 if (value < 0) 265 if (is_event_supported(type, dev->evbit, EV_MAX)) {
179 return;
180 266
181 if (dev->event) 267 spin_lock_irqsave(&dev->event_lock, flags);
182 dev->event(dev, type, code, value); 268 add_input_randomness(type, code, value);
183 break; 269 input_handle_event(dev, type, code, value);
270 spin_unlock_irqrestore(&dev->event_lock, flags);
184 } 271 }
185
186 if (type != EV_SYN)
187 dev->sync = 0;
188
189 if (dev->grab)
190 dev->grab->handler->event(dev->grab, type, code, value);
191 else
192 list_for_each_entry(handle, &dev->h_list, d_node)
193 if (handle->open)
194 handle->handler->event(handle, type, code, value);
195} 272}
196EXPORT_SYMBOL(input_event); 273EXPORT_SYMBOL(input_event);
197 274
@@ -202,102 +279,230 @@ EXPORT_SYMBOL(input_event);
202 * @code: event code 279 * @code: event code
203 * @value: value of the event 280 * @value: value of the event
204 * 281 *
205 * Similar to input_event() but will ignore event if device is "grabbed" and handle 282 * Similar to input_event() but will ignore event if device is
206 * injecting event is not the one that owns the device. 283 * "grabbed" and handle injecting event is not the one that owns
284 * the device.
207 */ 285 */
208void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) 286void input_inject_event(struct input_handle *handle,
287 unsigned int type, unsigned int code, int value)
209{ 288{
210 if (!handle->dev->grab || handle->dev->grab == handle) 289 struct input_dev *dev = handle->dev;
211 input_event(handle->dev, type, code, value); 290 struct input_handle *grab;
212} 291 unsigned long flags;
213EXPORT_SYMBOL(input_inject_event);
214
215static void input_repeat_key(unsigned long data)
216{
217 struct input_dev *dev = (void *) data;
218 292
219 if (!test_bit(dev->repeat_key, dev->key)) 293 if (is_event_supported(type, dev->evbit, EV_MAX)) {
220 return; 294 spin_lock_irqsave(&dev->event_lock, flags);
221 295
222 input_event(dev, EV_KEY, dev->repeat_key, 2); 296 grab = rcu_dereference(dev->grab);
223 input_sync(dev); 297 if (!grab || grab == handle)
298 input_handle_event(dev, type, code, value);
224 299
225 if (dev->rep[REP_PERIOD]) 300 spin_unlock_irqrestore(&dev->event_lock, flags);
226 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD])); 301 }
227} 302}
303EXPORT_SYMBOL(input_inject_event);
228 304
305/**
306 * input_grab_device - grabs device for exclusive use
307 * @handle: input handle that wants to own the device
308 *
309 * When a device is grabbed by an input handle all events generated by
310 * the device are delivered only to this handle. Also events injected
311 * by other input handles are ignored while device is grabbed.
312 */
229int input_grab_device(struct input_handle *handle) 313int input_grab_device(struct input_handle *handle)
230{ 314{
231 if (handle->dev->grab) 315 struct input_dev *dev = handle->dev;
232 return -EBUSY; 316 int retval;
233 317
234 handle->dev->grab = handle; 318 retval = mutex_lock_interruptible(&dev->mutex);
235 return 0; 319 if (retval)
320 return retval;
321
322 if (dev->grab) {
323 retval = -EBUSY;
324 goto out;
325 }
326
327 rcu_assign_pointer(dev->grab, handle);
328 /*
329 * Not using synchronize_rcu() because read-side is protected
330 * by a spinlock with interrupts off instead of rcu_read_lock().
331 */
332 synchronize_sched();
333
334 out:
335 mutex_unlock(&dev->mutex);
336 return retval;
236} 337}
237EXPORT_SYMBOL(input_grab_device); 338EXPORT_SYMBOL(input_grab_device);
238 339
239void input_release_device(struct input_handle *handle) 340static void __input_release_device(struct input_handle *handle)
240{ 341{
241 struct input_dev *dev = handle->dev; 342 struct input_dev *dev = handle->dev;
242 343
243 if (dev->grab == handle) { 344 if (dev->grab == handle) {
244 dev->grab = NULL; 345 rcu_assign_pointer(dev->grab, NULL);
346 /* Make sure input_pass_event() notices that grab is gone */
347 synchronize_sched();
245 348
246 list_for_each_entry(handle, &dev->h_list, d_node) 349 list_for_each_entry(handle, &dev->h_list, d_node)
247 if (handle->handler->start) 350 if (handle->open && handle->handler->start)
248 handle->handler->start(handle); 351 handle->handler->start(handle);
249 } 352 }
250} 353}
354
355/**
356 * input_release_device - release previously grabbed device
357 * @handle: input handle that owns the device
358 *
359 * Releases previously grabbed device so that other input handles can
360 * start receiving input events. Upon release all handlers attached
361 * to the device have their start() method called so they have a change
362 * to synchronize device state with the rest of the system.
363 */
364void input_release_device(struct input_handle *handle)
365{
366 struct input_dev *dev = handle->dev;
367
368 mutex_lock(&dev->mutex);
369 __input_release_device(handle);
370 mutex_unlock(&dev->mutex);
371}
251EXPORT_SYMBOL(input_release_device); 372EXPORT_SYMBOL(input_release_device);
252 373
374/**
375 * input_open_device - open input device
376 * @handle: handle through which device is being accessed
377 *
378 * This function should be called by input handlers when they
379 * want to start receive events from given input device.
380 */
253int input_open_device(struct input_handle *handle) 381int input_open_device(struct input_handle *handle)
254{ 382{
255 struct input_dev *dev = handle->dev; 383 struct input_dev *dev = handle->dev;
256 int err; 384 int retval;
257 385
258 err = mutex_lock_interruptible(&dev->mutex); 386 retval = mutex_lock_interruptible(&dev->mutex);
259 if (err) 387 if (retval)
260 return err; 388 return retval;
389
390 if (dev->going_away) {
391 retval = -ENODEV;
392 goto out;
393 }
261 394
262 handle->open++; 395 handle->open++;
263 396
264 if (!dev->users++ && dev->open) 397 if (!dev->users++ && dev->open)
265 err = dev->open(dev); 398 retval = dev->open(dev);
266 399
267 if (err) 400 if (retval) {
268 handle->open--; 401 dev->users--;
402 if (!--handle->open) {
403 /*
404 * Make sure we are not delivering any more events
405 * through this handle
406 */
407 synchronize_sched();
408 }
409 }
269 410
411 out:
270 mutex_unlock(&dev->mutex); 412 mutex_unlock(&dev->mutex);
271 413 return retval;
272 return err;
273} 414}
274EXPORT_SYMBOL(input_open_device); 415EXPORT_SYMBOL(input_open_device);
275 416
276int input_flush_device(struct input_handle* handle, struct file* file) 417int input_flush_device(struct input_handle *handle, struct file *file)
277{ 418{
278 if (handle->dev->flush) 419 struct input_dev *dev = handle->dev;
279 return handle->dev->flush(handle->dev, file); 420 int retval;
280 421
281 return 0; 422 retval = mutex_lock_interruptible(&dev->mutex);
423 if (retval)
424 return retval;
425
426 if (dev->flush)
427 retval = dev->flush(dev, file);
428
429 mutex_unlock(&dev->mutex);
430 return retval;
282} 431}
283EXPORT_SYMBOL(input_flush_device); 432EXPORT_SYMBOL(input_flush_device);
284 433
434/**
435 * input_close_device - close input device
436 * @handle: handle through which device is being accessed
437 *
438 * This function should be called by input handlers when they
439 * want to stop receive events from given input device.
440 */
285void input_close_device(struct input_handle *handle) 441void input_close_device(struct input_handle *handle)
286{ 442{
287 struct input_dev *dev = handle->dev; 443 struct input_dev *dev = handle->dev;
288 444
289 input_release_device(handle);
290
291 mutex_lock(&dev->mutex); 445 mutex_lock(&dev->mutex);
292 446
447 __input_release_device(handle);
448
293 if (!--dev->users && dev->close) 449 if (!--dev->users && dev->close)
294 dev->close(dev); 450 dev->close(dev);
295 handle->open--; 451
452 if (!--handle->open) {
453 /*
454 * synchronize_sched() makes sure that input_pass_event()
455 * completed and that no more input events are delivered
456 * through this handle
457 */
458 synchronize_sched();
459 }
296 460
297 mutex_unlock(&dev->mutex); 461 mutex_unlock(&dev->mutex);
298} 462}
299EXPORT_SYMBOL(input_close_device); 463EXPORT_SYMBOL(input_close_device);
300 464
465/*
466 * Prepare device for unregistering
467 */
468static void input_disconnect_device(struct input_dev *dev)
469{
470 struct input_handle *handle;
471 int code;
472
473 /*
474 * Mark device as going away. Note that we take dev->mutex here
475 * not to protect access to dev->going_away but rather to ensure
476 * that there are no threads in the middle of input_open_device()
477 */
478 mutex_lock(&dev->mutex);
479 dev->going_away = 1;
480 mutex_unlock(&dev->mutex);
481
482 spin_lock_irq(&dev->event_lock);
483
484 /*
485 * Simulate keyup events for all pressed keys so that handlers
486 * are not left with "stuck" keys. The driver may continue
487 * generate events even after we done here but they will not
488 * reach any handlers.
489 */
490 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
491 for (code = 0; code <= KEY_MAX; code++) {
492 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
493 test_bit(code, dev->key)) {
494 input_pass_event(dev, EV_KEY, code, 0);
495 }
496 }
497 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
498 }
499
500 list_for_each_entry(handle, &dev->h_list, d_node)
501 handle->open = 0;
502
503 spin_unlock_irq(&dev->event_lock);
504}
505
301static int input_fetch_keycode(struct input_dev *dev, int scancode) 506static int input_fetch_keycode(struct input_dev *dev, int scancode)
302{ 507{
303 switch (dev->keycodesize) { 508 switch (dev->keycodesize) {
@@ -473,7 +678,8 @@ static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
473 678
474static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos) 679static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
475{ 680{
476 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ 681 if (mutex_lock_interruptible(&input_mutex))
682 return NULL;
477 683
478 return seq_list_start(&input_dev_list, *pos); 684 return seq_list_start(&input_dev_list, *pos);
479} 685}
@@ -485,7 +691,7 @@ static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
485 691
486static void input_devices_seq_stop(struct seq_file *seq, void *v) 692static void input_devices_seq_stop(struct seq_file *seq, void *v)
487{ 693{
488 /* release lock here */ 694 mutex_unlock(&input_mutex);
489} 695}
490 696
491static void input_seq_print_bitmap(struct seq_file *seq, const char *name, 697static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
@@ -569,7 +775,9 @@ static const struct file_operations input_devices_fileops = {
569 775
570static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos) 776static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
571{ 777{
572 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ 778 if (mutex_lock_interruptible(&input_mutex))
779 return NULL;
780
573 seq->private = (void *)(unsigned long)*pos; 781 seq->private = (void *)(unsigned long)*pos;
574 return seq_list_start(&input_handler_list, *pos); 782 return seq_list_start(&input_handler_list, *pos);
575} 783}
@@ -582,7 +790,7 @@ static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
582 790
583static void input_handlers_seq_stop(struct seq_file *seq, void *v) 791static void input_handlers_seq_stop(struct seq_file *seq, void *v)
584{ 792{
585 /* release lock here */ 793 mutex_unlock(&input_mutex);
586} 794}
587 795
588static int input_handlers_seq_show(struct seq_file *seq, void *v) 796static int input_handlers_seq_show(struct seq_file *seq, void *v)
@@ -1005,6 +1213,7 @@ struct input_dev *input_allocate_device(void)
1005 dev->dev.class = &input_class; 1213 dev->dev.class = &input_class;
1006 device_initialize(&dev->dev); 1214 device_initialize(&dev->dev);
1007 mutex_init(&dev->mutex); 1215 mutex_init(&dev->mutex);
1216 spin_lock_init(&dev->event_lock);
1008 INIT_LIST_HEAD(&dev->h_list); 1217 INIT_LIST_HEAD(&dev->h_list);
1009 INIT_LIST_HEAD(&dev->node); 1218 INIT_LIST_HEAD(&dev->node);
1010 1219
@@ -1022,7 +1231,7 @@ EXPORT_SYMBOL(input_allocate_device);
1022 * This function should only be used if input_register_device() 1231 * This function should only be used if input_register_device()
1023 * was not called yet or if it failed. Once device was registered 1232 * was not called yet or if it failed. Once device was registered
1024 * use input_unregister_device() and memory will be freed once last 1233 * use input_unregister_device() and memory will be freed once last
1025 * refrence to the device is dropped. 1234 * reference to the device is dropped.
1026 * 1235 *
1027 * Device should be allocated by input_allocate_device(). 1236 * Device should be allocated by input_allocate_device().
1028 * 1237 *
@@ -1092,6 +1301,18 @@ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int
1092} 1301}
1093EXPORT_SYMBOL(input_set_capability); 1302EXPORT_SYMBOL(input_set_capability);
1094 1303
1304/**
1305 * input_register_device - register device with input core
1306 * @dev: device to be registered
1307 *
1308 * This function registers device with input core. The device must be
1309 * allocated with input_allocate_device() and all it's capabilities
1310 * set up before registering.
1311 * If function fails the device must be freed with input_free_device().
1312 * Once device has been successfully registered it can be unregistered
1313 * with input_unregister_device(); input_free_device() should not be
1314 * called in this case.
1315 */
1095int input_register_device(struct input_dev *dev) 1316int input_register_device(struct input_dev *dev)
1096{ 1317{
1097 static atomic_t input_no = ATOMIC_INIT(0); 1318 static atomic_t input_no = ATOMIC_INIT(0);
@@ -1099,7 +1320,7 @@ int input_register_device(struct input_dev *dev)
1099 const char *path; 1320 const char *path;
1100 int error; 1321 int error;
1101 1322
1102 set_bit(EV_SYN, dev->evbit); 1323 __set_bit(EV_SYN, dev->evbit);
1103 1324
1104 /* 1325 /*
1105 * If delay and period are pre-set by the driver, then autorepeating 1326 * If delay and period are pre-set by the driver, then autorepeating
@@ -1120,8 +1341,6 @@ int input_register_device(struct input_dev *dev)
1120 if (!dev->setkeycode) 1341 if (!dev->setkeycode)
1121 dev->setkeycode = input_default_setkeycode; 1342 dev->setkeycode = input_default_setkeycode;
1122 1343
1123 list_add_tail(&dev->node, &input_dev_list);
1124
1125 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id), 1344 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
1126 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1); 1345 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
1127 1346
@@ -1137,49 +1356,79 @@ int input_register_device(struct input_dev *dev)
1137 dev->name ? dev->name : "Unspecified device", path ? path : "N/A"); 1356 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1138 kfree(path); 1357 kfree(path);
1139 1358
1359 error = mutex_lock_interruptible(&input_mutex);
1360 if (error) {
1361 device_del(&dev->dev);
1362 return error;
1363 }
1364
1365 list_add_tail(&dev->node, &input_dev_list);
1366
1140 list_for_each_entry(handler, &input_handler_list, node) 1367 list_for_each_entry(handler, &input_handler_list, node)
1141 input_attach_handler(dev, handler); 1368 input_attach_handler(dev, handler);
1142 1369
1143 input_wakeup_procfs_readers(); 1370 input_wakeup_procfs_readers();
1144 1371
1372 mutex_unlock(&input_mutex);
1373
1145 return 0; 1374 return 0;
1146} 1375}
1147EXPORT_SYMBOL(input_register_device); 1376EXPORT_SYMBOL(input_register_device);
1148 1377
1378/**
1379 * input_unregister_device - unregister previously registered device
1380 * @dev: device to be unregistered
1381 *
1382 * This function unregisters an input device. Once device is unregistered
1383 * the caller should not try to access it as it may get freed at any moment.
1384 */
1149void input_unregister_device(struct input_dev *dev) 1385void input_unregister_device(struct input_dev *dev)
1150{ 1386{
1151 struct input_handle *handle, *next; 1387 struct input_handle *handle, *next;
1152 int code;
1153 1388
1154 for (code = 0; code <= KEY_MAX; code++) 1389 input_disconnect_device(dev);
1155 if (test_bit(code, dev->key))
1156 input_report_key(dev, code, 0);
1157 input_sync(dev);
1158 1390
1159 del_timer_sync(&dev->timer); 1391 mutex_lock(&input_mutex);
1160 1392
1161 list_for_each_entry_safe(handle, next, &dev->h_list, d_node) 1393 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1162 handle->handler->disconnect(handle); 1394 handle->handler->disconnect(handle);
1163 WARN_ON(!list_empty(&dev->h_list)); 1395 WARN_ON(!list_empty(&dev->h_list));
1164 1396
1397 del_timer_sync(&dev->timer);
1165 list_del_init(&dev->node); 1398 list_del_init(&dev->node);
1166 1399
1167 device_unregister(&dev->dev);
1168
1169 input_wakeup_procfs_readers(); 1400 input_wakeup_procfs_readers();
1401
1402 mutex_unlock(&input_mutex);
1403
1404 device_unregister(&dev->dev);
1170} 1405}
1171EXPORT_SYMBOL(input_unregister_device); 1406EXPORT_SYMBOL(input_unregister_device);
1172 1407
1408/**
1409 * input_register_handler - register a new input handler
1410 * @handler: handler to be registered
1411 *
1412 * This function registers a new input handler (interface) for input
1413 * devices in the system and attaches it to all input devices that
1414 * are compatible with the handler.
1415 */
1173int input_register_handler(struct input_handler *handler) 1416int input_register_handler(struct input_handler *handler)
1174{ 1417{
1175 struct input_dev *dev; 1418 struct input_dev *dev;
1419 int retval;
1420
1421 retval = mutex_lock_interruptible(&input_mutex);
1422 if (retval)
1423 return retval;
1176 1424
1177 INIT_LIST_HEAD(&handler->h_list); 1425 INIT_LIST_HEAD(&handler->h_list);
1178 1426
1179 if (handler->fops != NULL) { 1427 if (handler->fops != NULL) {
1180 if (input_table[handler->minor >> 5]) 1428 if (input_table[handler->minor >> 5]) {
1181 return -EBUSY; 1429 retval = -EBUSY;
1182 1430 goto out;
1431 }
1183 input_table[handler->minor >> 5] = handler; 1432 input_table[handler->minor >> 5] = handler;
1184 } 1433 }
1185 1434
@@ -1189,14 +1438,26 @@ int input_register_handler(struct input_handler *handler)
1189 input_attach_handler(dev, handler); 1438 input_attach_handler(dev, handler);
1190 1439
1191 input_wakeup_procfs_readers(); 1440 input_wakeup_procfs_readers();
1192 return 0; 1441
1442 out:
1443 mutex_unlock(&input_mutex);
1444 return retval;
1193} 1445}
1194EXPORT_SYMBOL(input_register_handler); 1446EXPORT_SYMBOL(input_register_handler);
1195 1447
1448/**
1449 * input_unregister_handler - unregisters an input handler
1450 * @handler: handler to be unregistered
1451 *
1452 * This function disconnects a handler from its input devices and
1453 * removes it from lists of known handlers.
1454 */
1196void input_unregister_handler(struct input_handler *handler) 1455void input_unregister_handler(struct input_handler *handler)
1197{ 1456{
1198 struct input_handle *handle, *next; 1457 struct input_handle *handle, *next;
1199 1458
1459 mutex_lock(&input_mutex);
1460
1200 list_for_each_entry_safe(handle, next, &handler->h_list, h_node) 1461 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1201 handler->disconnect(handle); 1462 handler->disconnect(handle);
1202 WARN_ON(!list_empty(&handler->h_list)); 1463 WARN_ON(!list_empty(&handler->h_list));
@@ -1207,14 +1468,50 @@ void input_unregister_handler(struct input_handler *handler)
1207 input_table[handler->minor >> 5] = NULL; 1468 input_table[handler->minor >> 5] = NULL;
1208 1469
1209 input_wakeup_procfs_readers(); 1470 input_wakeup_procfs_readers();
1471
1472 mutex_unlock(&input_mutex);
1210} 1473}
1211EXPORT_SYMBOL(input_unregister_handler); 1474EXPORT_SYMBOL(input_unregister_handler);
1212 1475
1476/**
1477 * input_register_handle - register a new input handle
1478 * @handle: handle to register
1479 *
1480 * This function puts a new input handle onto device's
1481 * and handler's lists so that events can flow through
1482 * it once it is opened using input_open_device().
1483 *
1484 * This function is supposed to be called from handler's
1485 * connect() method.
1486 */
1213int input_register_handle(struct input_handle *handle) 1487int input_register_handle(struct input_handle *handle)
1214{ 1488{
1215 struct input_handler *handler = handle->handler; 1489 struct input_handler *handler = handle->handler;
1490 struct input_dev *dev = handle->dev;
1491 int error;
1492
1493 /*
1494 * We take dev->mutex here to prevent race with
1495 * input_release_device().
1496 */
1497 error = mutex_lock_interruptible(&dev->mutex);
1498 if (error)
1499 return error;
1500 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1501 mutex_unlock(&dev->mutex);
1502 /*
1503 * We don't use synchronize_rcu() here because we rely
1504 * on dev->event_lock to protect read-side critical
1505 * section in input_pass_event().
1506 */
1507 synchronize_sched();
1216 1508
1217 list_add_tail(&handle->d_node, &handle->dev->h_list); 1509 /*
1510 * Since we are supposed to be called from ->connect()
1511 * which is mutually exclusive with ->disconnect()
1512 * we can't be racing with input_unregister_handle()
1513 * and so separate lock is not needed here.
1514 */
1218 list_add_tail(&handle->h_node, &handler->h_list); 1515 list_add_tail(&handle->h_node, &handler->h_list);
1219 1516
1220 if (handler->start) 1517 if (handler->start)
@@ -1224,10 +1521,29 @@ int input_register_handle(struct input_handle *handle)
1224} 1521}
1225EXPORT_SYMBOL(input_register_handle); 1522EXPORT_SYMBOL(input_register_handle);
1226 1523
1524/**
1525 * input_unregister_handle - unregister an input handle
1526 * @handle: handle to unregister
1527 *
1528 * This function removes input handle from device's
1529 * and handler's lists.
1530 *
1531 * This function is supposed to be called from handler's
1532 * disconnect() method.
1533 */
1227void input_unregister_handle(struct input_handle *handle) 1534void input_unregister_handle(struct input_handle *handle)
1228{ 1535{
1536 struct input_dev *dev = handle->dev;
1537
1229 list_del_init(&handle->h_node); 1538 list_del_init(&handle->h_node);
1230 list_del_init(&handle->d_node); 1539
1540 /*
1541 * Take dev->mutex to prevent race with input_release_device().
1542 */
1543 mutex_lock(&dev->mutex);
1544 list_del_rcu(&handle->d_node);
1545 mutex_unlock(&dev->mutex);
1546 synchronize_sched();
1231} 1547}
1232EXPORT_SYMBOL(input_unregister_handle); 1548EXPORT_SYMBOL(input_unregister_handle);
1233 1549