Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
1da177e4 LT |
14 | #include <linux/input.h> |
15 | #include <linux/module.h> | |
16 | #include <linux/random.h> | |
17 | #include <linux/major.h> | |
18 | #include <linux/proc_fs.h> | |
969b21cd | 19 | #include <linux/seq_file.h> |
1da177e4 LT |
20 | #include <linux/poll.h> |
21 | #include <linux/device.h> | |
e676c232 | 22 | #include <linux/mutex.h> |
8006479c | 23 | #include <linux/rcupdate.h> |
2edbf853 | 24 | #include <linux/smp_lock.h> |
1da177e4 LT |
25 | |
26 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); | |
27 | MODULE_DESCRIPTION("Input core"); | |
28 | MODULE_LICENSE("GPL"); | |
29 | ||
1da177e4 LT |
30 | #define INPUT_DEVICES 256 |
31 | ||
61994a61 HR |
32 | /* |
33 | * EV_ABS events which should not be cached are listed here. | |
34 | */ | |
35 | static unsigned int input_abs_bypass_init_data[] __initdata = { | |
36 | 0 | |
37 | }; | |
38 | static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)]; | |
39 | ||
1da177e4 LT |
40 | static LIST_HEAD(input_dev_list); |
41 | static LIST_HEAD(input_handler_list); | |
42 | ||
8006479c DT |
43 | /* |
44 | * input_mutex protects access to both input_dev_list and input_handler_list. | |
45 | * This also causes input_[un]register_device and input_[un]register_handler | |
46 | * be mutually exclusive which simplifies locking in drivers implementing | |
47 | * input handlers. | |
48 | */ | |
49 | static DEFINE_MUTEX(input_mutex); | |
50 | ||
1da177e4 LT |
51 | static struct input_handler *input_table[8]; |
52 | ||
8006479c DT |
53 | static inline int is_event_supported(unsigned int code, |
54 | unsigned long *bm, unsigned int max) | |
1da177e4 | 55 | { |
8006479c DT |
56 | return code <= max && test_bit(code, bm); |
57 | } | |
1da177e4 | 58 | |
8006479c DT |
59 | static int input_defuzz_abs_event(int value, int old_val, int fuzz) |
60 | { | |
61 | if (fuzz) { | |
62 | if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2) | |
63 | return old_val; | |
1da177e4 | 64 | |
8006479c DT |
65 | if (value > old_val - fuzz && value < old_val + fuzz) |
66 | return (old_val * 3 + value) / 4; | |
1da177e4 | 67 | |
8006479c DT |
68 | if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2) |
69 | return (old_val + value) / 2; | |
70 | } | |
1da177e4 | 71 | |
8006479c DT |
72 | return value; |
73 | } | |
1da177e4 | 74 | |
8006479c DT |
75 | /* |
76 | * Pass event through all open handles. This function is called with | |
82ba56c2 | 77 | * dev->event_lock held and interrupts disabled. |
8006479c DT |
78 | */ |
79 | static void input_pass_event(struct input_dev *dev, | |
80 | unsigned int type, unsigned int code, int value) | |
81 | { | |
82ba56c2 DT |
82 | struct input_handle *handle; |
83 | ||
84 | rcu_read_lock(); | |
1da177e4 | 85 | |
82ba56c2 | 86 | handle = rcu_dereference(dev->grab); |
8006479c DT |
87 | if (handle) |
88 | handle->handler->event(handle, type, code, value); | |
89 | else | |
90 | list_for_each_entry_rcu(handle, &dev->h_list, d_node) | |
91 | if (handle->open) | |
92 | handle->handler->event(handle, | |
93 | type, code, value); | |
82ba56c2 | 94 | rcu_read_unlock(); |
8006479c | 95 | } |
1da177e4 | 96 | |
8006479c DT |
97 | /* |
98 | * Generate software autorepeat event. Note that we take | |
99 | * dev->event_lock here to avoid racing with input_event | |
100 | * which may cause keys get "stuck". | |
101 | */ | |
102 | static void input_repeat_key(unsigned long data) | |
103 | { | |
104 | struct input_dev *dev = (void *) data; | |
105 | unsigned long flags; | |
1da177e4 | 106 | |
8006479c | 107 | spin_lock_irqsave(&dev->event_lock, flags); |
1da177e4 | 108 | |
8006479c DT |
109 | if (test_bit(dev->repeat_key, dev->key) && |
110 | is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) { | |
1da177e4 | 111 | |
8006479c | 112 | input_pass_event(dev, EV_KEY, dev->repeat_key, 2); |
1da177e4 | 113 | |
8006479c DT |
114 | if (dev->sync) { |
115 | /* | |
116 | * Only send SYN_REPORT if we are not in a middle | |
117 | * of driver parsing a new hardware packet. | |
118 | * Otherwise assume that the driver will send | |
119 | * SYN_REPORT once it's done. | |
120 | */ | |
121 | input_pass_event(dev, EV_SYN, SYN_REPORT, 1); | |
122 | } | |
31581066 | 123 | |
8006479c DT |
124 | if (dev->rep[REP_PERIOD]) |
125 | mod_timer(&dev->timer, jiffies + | |
126 | msecs_to_jiffies(dev->rep[REP_PERIOD])); | |
127 | } | |
31581066 | 128 | |
8006479c DT |
129 | spin_unlock_irqrestore(&dev->event_lock, flags); |
130 | } | |
31581066 | 131 | |
8006479c DT |
132 | static void input_start_autorepeat(struct input_dev *dev, int code) |
133 | { | |
134 | if (test_bit(EV_REP, dev->evbit) && | |
135 | dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && | |
136 | dev->timer.data) { | |
137 | dev->repeat_key = code; | |
138 | mod_timer(&dev->timer, | |
139 | jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); | |
140 | } | |
141 | } | |
31581066 | 142 | |
e7b5c1ef JB |
143 | static void input_stop_autorepeat(struct input_dev *dev) |
144 | { | |
145 | del_timer(&dev->timer); | |
146 | } | |
147 | ||
8006479c DT |
148 | #define INPUT_IGNORE_EVENT 0 |
149 | #define INPUT_PASS_TO_HANDLERS 1 | |
150 | #define INPUT_PASS_TO_DEVICE 2 | |
151 | #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE) | |
1da177e4 | 152 | |
8006479c DT |
153 | static void input_handle_event(struct input_dev *dev, |
154 | unsigned int type, unsigned int code, int value) | |
155 | { | |
156 | int disposition = INPUT_IGNORE_EVENT; | |
1da177e4 | 157 | |
8006479c | 158 | switch (type) { |
1da177e4 | 159 | |
8006479c DT |
160 | case EV_SYN: |
161 | switch (code) { | |
162 | case SYN_CONFIG: | |
163 | disposition = INPUT_PASS_TO_ALL; | |
164 | break; | |
1da177e4 | 165 | |
8006479c DT |
166 | case SYN_REPORT: |
167 | if (!dev->sync) { | |
168 | dev->sync = 1; | |
169 | disposition = INPUT_PASS_TO_HANDLERS; | |
1da177e4 | 170 | } |
1da177e4 | 171 | break; |
8006479c DT |
172 | } |
173 | break; | |
1da177e4 | 174 | |
8006479c DT |
175 | case EV_KEY: |
176 | if (is_event_supported(code, dev->keybit, KEY_MAX) && | |
177 | !!test_bit(code, dev->key) != value) { | |
1da177e4 | 178 | |
8006479c DT |
179 | if (value != 2) { |
180 | __change_bit(code, dev->key); | |
181 | if (value) | |
182 | input_start_autorepeat(dev, code); | |
e7b5c1ef JB |
183 | else |
184 | input_stop_autorepeat(dev); | |
8006479c | 185 | } |
1da177e4 | 186 | |
8006479c DT |
187 | disposition = INPUT_PASS_TO_HANDLERS; |
188 | } | |
189 | break; | |
1da177e4 | 190 | |
8006479c DT |
191 | case EV_SW: |
192 | if (is_event_supported(code, dev->swbit, SW_MAX) && | |
193 | !!test_bit(code, dev->sw) != value) { | |
1da177e4 | 194 | |
8006479c DT |
195 | __change_bit(code, dev->sw); |
196 | disposition = INPUT_PASS_TO_HANDLERS; | |
197 | } | |
198 | break; | |
1da177e4 | 199 | |
8006479c DT |
200 | case EV_ABS: |
201 | if (is_event_supported(code, dev->absbit, ABS_MAX)) { | |
1da177e4 | 202 | |
61994a61 HR |
203 | if (test_bit(code, input_abs_bypass)) { |
204 | disposition = INPUT_PASS_TO_HANDLERS; | |
205 | break; | |
206 | } | |
207 | ||
8006479c DT |
208 | value = input_defuzz_abs_event(value, |
209 | dev->abs[code], dev->absfuzz[code]); | |
1da177e4 | 210 | |
8006479c DT |
211 | if (dev->abs[code] != value) { |
212 | dev->abs[code] = value; | |
213 | disposition = INPUT_PASS_TO_HANDLERS; | |
214 | } | |
215 | } | |
216 | break; | |
1da177e4 | 217 | |
8006479c DT |
218 | case EV_REL: |
219 | if (is_event_supported(code, dev->relbit, REL_MAX) && value) | |
220 | disposition = INPUT_PASS_TO_HANDLERS; | |
1da177e4 | 221 | |
8006479c | 222 | break; |
1e0afb28 | 223 | |
8006479c DT |
224 | case EV_MSC: |
225 | if (is_event_supported(code, dev->mscbit, MSC_MAX)) | |
226 | disposition = INPUT_PASS_TO_ALL; | |
1da177e4 | 227 | |
8006479c | 228 | break; |
1da177e4 | 229 | |
8006479c DT |
230 | case EV_LED: |
231 | if (is_event_supported(code, dev->ledbit, LED_MAX) && | |
232 | !!test_bit(code, dev->led) != value) { | |
1da177e4 | 233 | |
8006479c DT |
234 | __change_bit(code, dev->led); |
235 | disposition = INPUT_PASS_TO_ALL; | |
236 | } | |
237 | break; | |
238 | ||
239 | case EV_SND: | |
240 | if (is_event_supported(code, dev->sndbit, SND_MAX)) { | |
1da177e4 | 241 | |
8fdc1948 | 242 | if (!!test_bit(code, dev->snd) != !!value) |
8006479c DT |
243 | __change_bit(code, dev->snd); |
244 | disposition = INPUT_PASS_TO_ALL; | |
245 | } | |
246 | break; | |
8fdc1948 | 247 | |
8006479c DT |
248 | case EV_REP: |
249 | if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) { | |
250 | dev->rep[code] = value; | |
251 | disposition = INPUT_PASS_TO_ALL; | |
252 | } | |
253 | break; | |
1da177e4 | 254 | |
8006479c DT |
255 | case EV_FF: |
256 | if (value >= 0) | |
257 | disposition = INPUT_PASS_TO_ALL; | |
258 | break; | |
ed2fa4dd RP |
259 | |
260 | case EV_PWR: | |
261 | disposition = INPUT_PASS_TO_ALL; | |
262 | break; | |
8006479c | 263 | } |
1da177e4 | 264 | |
c9812282 | 265 | if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN) |
8006479c | 266 | dev->sync = 0; |
1da177e4 | 267 | |
8006479c DT |
268 | if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event) |
269 | dev->event(dev, type, code, value); | |
1da177e4 | 270 | |
8006479c DT |
271 | if (disposition & INPUT_PASS_TO_HANDLERS) |
272 | input_pass_event(dev, type, code, value); | |
273 | } | |
1da177e4 | 274 | |
8006479c DT |
275 | /** |
276 | * input_event() - report new input event | |
277 | * @dev: device that generated the event | |
278 | * @type: type of the event | |
279 | * @code: event code | |
280 | * @value: value of the event | |
281 | * | |
282 | * This function should be used by drivers implementing various input | |
283 | * devices. See also input_inject_event(). | |
284 | */ | |
1da177e4 | 285 | |
8006479c DT |
286 | void input_event(struct input_dev *dev, |
287 | unsigned int type, unsigned int code, int value) | |
288 | { | |
289 | unsigned long flags; | |
509ca1a9 | 290 | |
8006479c | 291 | if (is_event_supported(type, dev->evbit, EV_MAX)) { |
509ca1a9 | 292 | |
8006479c DT |
293 | spin_lock_irqsave(&dev->event_lock, flags); |
294 | add_input_randomness(type, code, value); | |
295 | input_handle_event(dev, type, code, value); | |
296 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
1da177e4 | 297 | } |
1da177e4 | 298 | } |
ca56fe07 | 299 | EXPORT_SYMBOL(input_event); |
1da177e4 | 300 | |
0e739d28 DT |
301 | /** |
302 | * input_inject_event() - send input event from input handler | |
303 | * @handle: input handle to send event through | |
304 | * @type: type of the event | |
305 | * @code: event code | |
306 | * @value: value of the event | |
307 | * | |
8006479c DT |
308 | * Similar to input_event() but will ignore event if device is |
309 | * "grabbed" and handle injecting event is not the one that owns | |
310 | * the device. | |
0e739d28 | 311 | */ |
8006479c DT |
312 | void input_inject_event(struct input_handle *handle, |
313 | unsigned int type, unsigned int code, int value) | |
1da177e4 | 314 | { |
8006479c DT |
315 | struct input_dev *dev = handle->dev; |
316 | struct input_handle *grab; | |
317 | unsigned long flags; | |
1da177e4 | 318 | |
8006479c DT |
319 | if (is_event_supported(type, dev->evbit, EV_MAX)) { |
320 | spin_lock_irqsave(&dev->event_lock, flags); | |
1da177e4 | 321 | |
82ba56c2 | 322 | rcu_read_lock(); |
8006479c DT |
323 | grab = rcu_dereference(dev->grab); |
324 | if (!grab || grab == handle) | |
325 | input_handle_event(dev, type, code, value); | |
82ba56c2 | 326 | rcu_read_unlock(); |
1da177e4 | 327 | |
8006479c DT |
328 | spin_unlock_irqrestore(&dev->event_lock, flags); |
329 | } | |
1da177e4 | 330 | } |
8006479c | 331 | EXPORT_SYMBOL(input_inject_event); |
1da177e4 | 332 | |
8006479c DT |
333 | /** |
334 | * input_grab_device - grabs device for exclusive use | |
335 | * @handle: input handle that wants to own the device | |
336 | * | |
337 | * When a device is grabbed by an input handle all events generated by | |
338 | * the device are delivered only to this handle. Also events injected | |
339 | * by other input handles are ignored while device is grabbed. | |
340 | */ | |
1da177e4 LT |
341 | int input_grab_device(struct input_handle *handle) |
342 | { | |
8006479c DT |
343 | struct input_dev *dev = handle->dev; |
344 | int retval; | |
1da177e4 | 345 | |
8006479c DT |
346 | retval = mutex_lock_interruptible(&dev->mutex); |
347 | if (retval) | |
348 | return retval; | |
349 | ||
350 | if (dev->grab) { | |
351 | retval = -EBUSY; | |
352 | goto out; | |
353 | } | |
354 | ||
355 | rcu_assign_pointer(dev->grab, handle); | |
82ba56c2 | 356 | synchronize_rcu(); |
8006479c DT |
357 | |
358 | out: | |
359 | mutex_unlock(&dev->mutex); | |
360 | return retval; | |
1da177e4 | 361 | } |
ca56fe07 | 362 | EXPORT_SYMBOL(input_grab_device); |
1da177e4 | 363 | |
8006479c | 364 | static void __input_release_device(struct input_handle *handle) |
1da177e4 | 365 | { |
a2b2ed2c | 366 | struct input_dev *dev = handle->dev; |
c7e8dc6e | 367 | |
a2b2ed2c | 368 | if (dev->grab == handle) { |
8006479c DT |
369 | rcu_assign_pointer(dev->grab, NULL); |
370 | /* Make sure input_pass_event() notices that grab is gone */ | |
82ba56c2 | 371 | synchronize_rcu(); |
a2b2ed2c AM |
372 | |
373 | list_for_each_entry(handle, &dev->h_list, d_node) | |
8006479c | 374 | if (handle->open && handle->handler->start) |
c7e8dc6e DT |
375 | handle->handler->start(handle); |
376 | } | |
1da177e4 | 377 | } |
8006479c DT |
378 | |
379 | /** | |
380 | * input_release_device - release previously grabbed device | |
381 | * @handle: input handle that owns the device | |
382 | * | |
383 | * Releases previously grabbed device so that other input handles can | |
384 | * start receiving input events. Upon release all handlers attached | |
385 | * to the device have their start() method called so they have a change | |
386 | * to synchronize device state with the rest of the system. | |
387 | */ | |
388 | void input_release_device(struct input_handle *handle) | |
389 | { | |
390 | struct input_dev *dev = handle->dev; | |
391 | ||
392 | mutex_lock(&dev->mutex); | |
393 | __input_release_device(handle); | |
394 | mutex_unlock(&dev->mutex); | |
395 | } | |
ca56fe07 | 396 | EXPORT_SYMBOL(input_release_device); |
1da177e4 | 397 | |
8006479c DT |
398 | /** |
399 | * input_open_device - open input device | |
400 | * @handle: handle through which device is being accessed | |
401 | * | |
402 | * This function should be called by input handlers when they | |
403 | * want to start receive events from given input device. | |
404 | */ | |
1da177e4 LT |
405 | int input_open_device(struct input_handle *handle) |
406 | { | |
0fbf87ca | 407 | struct input_dev *dev = handle->dev; |
8006479c | 408 | int retval; |
0fbf87ca | 409 | |
8006479c DT |
410 | retval = mutex_lock_interruptible(&dev->mutex); |
411 | if (retval) | |
412 | return retval; | |
413 | ||
414 | if (dev->going_away) { | |
415 | retval = -ENODEV; | |
416 | goto out; | |
417 | } | |
0fbf87ca | 418 | |
1da177e4 | 419 | handle->open++; |
0fbf87ca DT |
420 | |
421 | if (!dev->users++ && dev->open) | |
8006479c DT |
422 | retval = dev->open(dev); |
423 | ||
424 | if (retval) { | |
425 | dev->users--; | |
426 | if (!--handle->open) { | |
427 | /* | |
428 | * Make sure we are not delivering any more events | |
429 | * through this handle | |
430 | */ | |
82ba56c2 | 431 | synchronize_rcu(); |
8006479c DT |
432 | } |
433 | } | |
0fbf87ca | 434 | |
8006479c | 435 | out: |
e676c232 | 436 | mutex_unlock(&dev->mutex); |
8006479c | 437 | return retval; |
1da177e4 | 438 | } |
ca56fe07 | 439 | EXPORT_SYMBOL(input_open_device); |
1da177e4 | 440 | |
8006479c | 441 | int input_flush_device(struct input_handle *handle, struct file *file) |
1da177e4 | 442 | { |
8006479c DT |
443 | struct input_dev *dev = handle->dev; |
444 | int retval; | |
1da177e4 | 445 | |
8006479c DT |
446 | retval = mutex_lock_interruptible(&dev->mutex); |
447 | if (retval) | |
448 | return retval; | |
449 | ||
450 | if (dev->flush) | |
451 | retval = dev->flush(dev, file); | |
452 | ||
453 | mutex_unlock(&dev->mutex); | |
454 | return retval; | |
1da177e4 | 455 | } |
ca56fe07 | 456 | EXPORT_SYMBOL(input_flush_device); |
1da177e4 | 457 | |
8006479c DT |
458 | /** |
459 | * input_close_device - close input device | |
460 | * @handle: handle through which device is being accessed | |
461 | * | |
462 | * This function should be called by input handlers when they | |
463 | * want to stop receive events from given input device. | |
464 | */ | |
1da177e4 LT |
465 | void input_close_device(struct input_handle *handle) |
466 | { | |
0fbf87ca DT |
467 | struct input_dev *dev = handle->dev; |
468 | ||
e676c232 | 469 | mutex_lock(&dev->mutex); |
0fbf87ca | 470 | |
8006479c DT |
471 | __input_release_device(handle); |
472 | ||
0fbf87ca DT |
473 | if (!--dev->users && dev->close) |
474 | dev->close(dev); | |
8006479c DT |
475 | |
476 | if (!--handle->open) { | |
477 | /* | |
82ba56c2 | 478 | * synchronize_rcu() makes sure that input_pass_event() |
8006479c DT |
479 | * completed and that no more input events are delivered |
480 | * through this handle | |
481 | */ | |
82ba56c2 | 482 | synchronize_rcu(); |
8006479c | 483 | } |
0fbf87ca | 484 | |
e676c232 | 485 | mutex_unlock(&dev->mutex); |
1da177e4 | 486 | } |
ca56fe07 | 487 | EXPORT_SYMBOL(input_close_device); |
1da177e4 | 488 | |
8006479c DT |
489 | /* |
490 | * Prepare device for unregistering | |
491 | */ | |
492 | static void input_disconnect_device(struct input_dev *dev) | |
493 | { | |
494 | struct input_handle *handle; | |
495 | int code; | |
496 | ||
497 | /* | |
498 | * Mark device as going away. Note that we take dev->mutex here | |
499 | * not to protect access to dev->going_away but rather to ensure | |
500 | * that there are no threads in the middle of input_open_device() | |
501 | */ | |
502 | mutex_lock(&dev->mutex); | |
503 | dev->going_away = 1; | |
504 | mutex_unlock(&dev->mutex); | |
505 | ||
506 | spin_lock_irq(&dev->event_lock); | |
507 | ||
508 | /* | |
509 | * Simulate keyup events for all pressed keys so that handlers | |
510 | * are not left with "stuck" keys. The driver may continue | |
511 | * generate events even after we done here but they will not | |
512 | * reach any handlers. | |
513 | */ | |
514 | if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) { | |
515 | for (code = 0; code <= KEY_MAX; code++) { | |
516 | if (is_event_supported(code, dev->keybit, KEY_MAX) && | |
f4f37c8e | 517 | __test_and_clear_bit(code, dev->key)) { |
8006479c DT |
518 | input_pass_event(dev, EV_KEY, code, 0); |
519 | } | |
520 | } | |
521 | input_pass_event(dev, EV_SYN, SYN_REPORT, 1); | |
522 | } | |
523 | ||
524 | list_for_each_entry(handle, &dev->h_list, d_node) | |
525 | handle->open = 0; | |
526 | ||
527 | spin_unlock_irq(&dev->event_lock); | |
528 | } | |
529 | ||
c8e4c772 MR |
530 | static int input_fetch_keycode(struct input_dev *dev, int scancode) |
531 | { | |
532 | switch (dev->keycodesize) { | |
533 | case 1: | |
534 | return ((u8 *)dev->keycode)[scancode]; | |
535 | ||
536 | case 2: | |
537 | return ((u16 *)dev->keycode)[scancode]; | |
538 | ||
539 | default: | |
540 | return ((u32 *)dev->keycode)[scancode]; | |
541 | } | |
542 | } | |
543 | ||
544 | static int input_default_getkeycode(struct input_dev *dev, | |
545 | int scancode, int *keycode) | |
546 | { | |
547 | if (!dev->keycodesize) | |
548 | return -EINVAL; | |
549 | ||
f4f37c8e | 550 | if (scancode >= dev->keycodemax) |
c8e4c772 MR |
551 | return -EINVAL; |
552 | ||
553 | *keycode = input_fetch_keycode(dev, scancode); | |
554 | ||
555 | return 0; | |
556 | } | |
557 | ||
558 | static int input_default_setkeycode(struct input_dev *dev, | |
559 | int scancode, int keycode) | |
560 | { | |
561 | int old_keycode; | |
562 | int i; | |
563 | ||
f4f37c8e | 564 | if (scancode >= dev->keycodemax) |
c8e4c772 MR |
565 | return -EINVAL; |
566 | ||
567 | if (!dev->keycodesize) | |
568 | return -EINVAL; | |
569 | ||
570 | if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8))) | |
571 | return -EINVAL; | |
572 | ||
573 | switch (dev->keycodesize) { | |
574 | case 1: { | |
575 | u8 *k = (u8 *)dev->keycode; | |
576 | old_keycode = k[scancode]; | |
577 | k[scancode] = keycode; | |
578 | break; | |
579 | } | |
580 | case 2: { | |
581 | u16 *k = (u16 *)dev->keycode; | |
582 | old_keycode = k[scancode]; | |
583 | k[scancode] = keycode; | |
584 | break; | |
585 | } | |
586 | default: { | |
587 | u32 *k = (u32 *)dev->keycode; | |
588 | old_keycode = k[scancode]; | |
589 | k[scancode] = keycode; | |
590 | break; | |
591 | } | |
592 | } | |
593 | ||
594 | clear_bit(old_keycode, dev->keybit); | |
595 | set_bit(keycode, dev->keybit); | |
596 | ||
597 | for (i = 0; i < dev->keycodemax; i++) { | |
598 | if (input_fetch_keycode(dev, i) == old_keycode) { | |
599 | set_bit(old_keycode, dev->keybit); | |
600 | break; /* Setting the bit twice is useless, so break */ | |
601 | } | |
602 | } | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
f4f37c8e DT |
607 | /** |
608 | * input_get_keycode - retrieve keycode currently mapped to a given scancode | |
609 | * @dev: input device which keymap is being queried | |
610 | * @scancode: scancode (or its equivalent for device in question) for which | |
611 | * keycode is needed | |
612 | * @keycode: result | |
613 | * | |
614 | * This function should be called by anyone interested in retrieving current | |
615 | * keymap. Presently keyboard and evdev handlers use it. | |
616 | */ | |
617 | int input_get_keycode(struct input_dev *dev, int scancode, int *keycode) | |
618 | { | |
619 | if (scancode < 0) | |
620 | return -EINVAL; | |
621 | ||
622 | return dev->getkeycode(dev, scancode, keycode); | |
623 | } | |
624 | EXPORT_SYMBOL(input_get_keycode); | |
625 | ||
626 | /** | |
627 | * input_get_keycode - assign new keycode to a given scancode | |
628 | * @dev: input device which keymap is being updated | |
629 | * @scancode: scancode (or its equivalent for device in question) | |
630 | * @keycode: new keycode to be assigned to the scancode | |
631 | * | |
632 | * This function should be called by anyone needing to update current | |
633 | * keymap. Presently keyboard and evdev handlers use it. | |
634 | */ | |
635 | int input_set_keycode(struct input_dev *dev, int scancode, int keycode) | |
636 | { | |
637 | unsigned long flags; | |
638 | int old_keycode; | |
639 | int retval; | |
640 | ||
641 | if (scancode < 0) | |
642 | return -EINVAL; | |
643 | ||
644 | if (keycode < 0 || keycode > KEY_MAX) | |
645 | return -EINVAL; | |
646 | ||
647 | spin_lock_irqsave(&dev->event_lock, flags); | |
648 | ||
649 | retval = dev->getkeycode(dev, scancode, &old_keycode); | |
650 | if (retval) | |
651 | goto out; | |
652 | ||
653 | retval = dev->setkeycode(dev, scancode, keycode); | |
654 | if (retval) | |
655 | goto out; | |
656 | ||
657 | /* | |
658 | * Simulate keyup event if keycode is not present | |
659 | * in the keymap anymore | |
660 | */ | |
661 | if (test_bit(EV_KEY, dev->evbit) && | |
662 | !is_event_supported(old_keycode, dev->keybit, KEY_MAX) && | |
663 | __test_and_clear_bit(old_keycode, dev->key)) { | |
664 | ||
665 | input_pass_event(dev, EV_KEY, old_keycode, 0); | |
666 | if (dev->sync) | |
667 | input_pass_event(dev, EV_SYN, SYN_REPORT, 1); | |
668 | } | |
669 | ||
670 | out: | |
671 | spin_unlock_irqrestore(&dev->event_lock, flags); | |
672 | ||
673 | return retval; | |
674 | } | |
675 | EXPORT_SYMBOL(input_set_keycode); | |
c8e4c772 | 676 | |
1da177e4 | 677 | #define MATCH_BIT(bit, max) \ |
7b19ada2 | 678 | for (i = 0; i < BITS_TO_LONGS(max); i++) \ |
1da177e4 LT |
679 | if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ |
680 | break; \ | |
7b19ada2 | 681 | if (i != BITS_TO_LONGS(max)) \ |
1da177e4 LT |
682 | continue; |
683 | ||
66e66118 DT |
684 | static const struct input_device_id *input_match_device(const struct input_device_id *id, |
685 | struct input_dev *dev) | |
1da177e4 LT |
686 | { |
687 | int i; | |
688 | ||
689 | for (; id->flags || id->driver_info; id++) { | |
690 | ||
691 | if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) | |
ddc5d341 | 692 | if (id->bustype != dev->id.bustype) |
1da177e4 LT |
693 | continue; |
694 | ||
695 | if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) | |
ddc5d341 | 696 | if (id->vendor != dev->id.vendor) |
1da177e4 LT |
697 | continue; |
698 | ||
699 | if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) | |
ddc5d341 | 700 | if (id->product != dev->id.product) |
1da177e4 LT |
701 | continue; |
702 | ||
703 | if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) | |
ddc5d341 | 704 | if (id->version != dev->id.version) |
1da177e4 LT |
705 | continue; |
706 | ||
707 | MATCH_BIT(evbit, EV_MAX); | |
708 | MATCH_BIT(keybit, KEY_MAX); | |
709 | MATCH_BIT(relbit, REL_MAX); | |
710 | MATCH_BIT(absbit, ABS_MAX); | |
711 | MATCH_BIT(mscbit, MSC_MAX); | |
712 | MATCH_BIT(ledbit, LED_MAX); | |
713 | MATCH_BIT(sndbit, SND_MAX); | |
714 | MATCH_BIT(ffbit, FF_MAX); | |
ff13f98b | 715 | MATCH_BIT(swbit, SW_MAX); |
1da177e4 LT |
716 | |
717 | return id; | |
718 | } | |
719 | ||
720 | return NULL; | |
721 | } | |
722 | ||
5b2a0826 DT |
723 | static int input_attach_handler(struct input_dev *dev, struct input_handler *handler) |
724 | { | |
725 | const struct input_device_id *id; | |
726 | int error; | |
727 | ||
728 | if (handler->blacklist && input_match_device(handler->blacklist, dev)) | |
729 | return -ENODEV; | |
730 | ||
731 | id = input_match_device(handler->id_table, dev); | |
732 | if (!id) | |
733 | return -ENODEV; | |
734 | ||
735 | error = handler->connect(handler, dev, id); | |
736 | if (error && error != -ENODEV) | |
737 | printk(KERN_ERR | |
738 | "input: failed to attach handler %s to device %s, " | |
739 | "error: %d\n", | |
9657d75c | 740 | handler->name, kobject_name(&dev->dev.kobj), error); |
5b2a0826 DT |
741 | |
742 | return error; | |
743 | } | |
744 | ||
745 | ||
f96b434d DT |
746 | #ifdef CONFIG_PROC_FS |
747 | ||
748 | static struct proc_dir_entry *proc_bus_input_dir; | |
749 | static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait); | |
750 | static int input_devices_state; | |
751 | ||
752 | static inline void input_wakeup_procfs_readers(void) | |
753 | { | |
754 | input_devices_state++; | |
755 | wake_up(&input_devices_poll_wait); | |
756 | } | |
757 | ||
969b21cd | 758 | static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait) |
f96b434d | 759 | { |
f96b434d | 760 | poll_wait(file, &input_devices_poll_wait, wait); |
fa886612 DT |
761 | if (file->f_version != input_devices_state) { |
762 | file->f_version = input_devices_state; | |
f96b434d | 763 | return POLLIN | POLLRDNORM; |
fa886612 | 764 | } |
1e0afb28 | 765 | |
f96b434d DT |
766 | return 0; |
767 | } | |
768 | ||
969b21cd DT |
769 | static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos) |
770 | { | |
8006479c DT |
771 | if (mutex_lock_interruptible(&input_mutex)) |
772 | return NULL; | |
f96b434d | 773 | |
ad5d972c | 774 | return seq_list_start(&input_dev_list, *pos); |
969b21cd | 775 | } |
051b2fea | 776 | |
969b21cd DT |
777 | static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
778 | { | |
ad5d972c | 779 | return seq_list_next(v, &input_dev_list, pos); |
969b21cd | 780 | } |
f96b434d | 781 | |
969b21cd DT |
782 | static void input_devices_seq_stop(struct seq_file *seq, void *v) |
783 | { | |
8006479c | 784 | mutex_unlock(&input_mutex); |
969b21cd | 785 | } |
f96b434d | 786 | |
969b21cd DT |
787 | static void input_seq_print_bitmap(struct seq_file *seq, const char *name, |
788 | unsigned long *bitmap, int max) | |
789 | { | |
790 | int i; | |
051b2fea | 791 | |
7b19ada2 | 792 | for (i = BITS_TO_LONGS(max) - 1; i > 0; i--) |
969b21cd DT |
793 | if (bitmap[i]) |
794 | break; | |
f96b434d | 795 | |
969b21cd DT |
796 | seq_printf(seq, "B: %s=", name); |
797 | for (; i >= 0; i--) | |
798 | seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : ""); | |
799 | seq_putc(seq, '\n'); | |
800 | } | |
f96b434d | 801 | |
969b21cd DT |
802 | static int input_devices_seq_show(struct seq_file *seq, void *v) |
803 | { | |
804 | struct input_dev *dev = container_of(v, struct input_dev, node); | |
9657d75c | 805 | const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); |
969b21cd DT |
806 | struct input_handle *handle; |
807 | ||
808 | seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n", | |
809 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version); | |
810 | ||
811 | seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); | |
812 | seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : ""); | |
813 | seq_printf(seq, "S: Sysfs=%s\n", path ? path : ""); | |
15e03ae8 | 814 | seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : ""); |
969b21cd DT |
815 | seq_printf(seq, "H: Handlers="); |
816 | ||
817 | list_for_each_entry(handle, &dev->h_list, d_node) | |
818 | seq_printf(seq, "%s ", handle->name); | |
819 | seq_putc(seq, '\n'); | |
820 | ||
821 | input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX); | |
822 | if (test_bit(EV_KEY, dev->evbit)) | |
823 | input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX); | |
824 | if (test_bit(EV_REL, dev->evbit)) | |
825 | input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX); | |
826 | if (test_bit(EV_ABS, dev->evbit)) | |
827 | input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX); | |
828 | if (test_bit(EV_MSC, dev->evbit)) | |
829 | input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX); | |
830 | if (test_bit(EV_LED, dev->evbit)) | |
831 | input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX); | |
832 | if (test_bit(EV_SND, dev->evbit)) | |
833 | input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX); | |
834 | if (test_bit(EV_FF, dev->evbit)) | |
835 | input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX); | |
836 | if (test_bit(EV_SW, dev->evbit)) | |
837 | input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX); | |
838 | ||
839 | seq_putc(seq, '\n'); | |
840 | ||
841 | kfree(path); | |
842 | return 0; | |
f96b434d DT |
843 | } |
844 | ||
cec69c37 | 845 | static const struct seq_operations input_devices_seq_ops = { |
969b21cd DT |
846 | .start = input_devices_seq_start, |
847 | .next = input_devices_seq_next, | |
848 | .stop = input_devices_seq_stop, | |
849 | .show = input_devices_seq_show, | |
850 | }; | |
851 | ||
852 | static int input_proc_devices_open(struct inode *inode, struct file *file) | |
f96b434d | 853 | { |
969b21cd DT |
854 | return seq_open(file, &input_devices_seq_ops); |
855 | } | |
856 | ||
2b8693c0 | 857 | static const struct file_operations input_devices_fileops = { |
969b21cd DT |
858 | .owner = THIS_MODULE, |
859 | .open = input_proc_devices_open, | |
860 | .poll = input_proc_devices_poll, | |
861 | .read = seq_read, | |
862 | .llseek = seq_lseek, | |
863 | .release = seq_release, | |
864 | }; | |
865 | ||
866 | static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos) | |
867 | { | |
8006479c DT |
868 | if (mutex_lock_interruptible(&input_mutex)) |
869 | return NULL; | |
870 | ||
969b21cd | 871 | seq->private = (void *)(unsigned long)*pos; |
ad5d972c | 872 | return seq_list_start(&input_handler_list, *pos); |
969b21cd | 873 | } |
f96b434d | 874 | |
969b21cd DT |
875 | static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
876 | { | |
877 | seq->private = (void *)(unsigned long)(*pos + 1); | |
ad5d972c | 878 | return seq_list_next(v, &input_handler_list, pos); |
f96b434d DT |
879 | } |
880 | ||
969b21cd DT |
881 | static void input_handlers_seq_stop(struct seq_file *seq, void *v) |
882 | { | |
8006479c | 883 | mutex_unlock(&input_mutex); |
969b21cd DT |
884 | } |
885 | ||
886 | static int input_handlers_seq_show(struct seq_file *seq, void *v) | |
887 | { | |
888 | struct input_handler *handler = container_of(v, struct input_handler, node); | |
889 | ||
890 | seq_printf(seq, "N: Number=%ld Name=%s", | |
891 | (unsigned long)seq->private, handler->name); | |
892 | if (handler->fops) | |
893 | seq_printf(seq, " Minor=%d", handler->minor); | |
894 | seq_putc(seq, '\n'); | |
895 | ||
896 | return 0; | |
897 | } | |
cec69c37 | 898 | static const struct seq_operations input_handlers_seq_ops = { |
969b21cd DT |
899 | .start = input_handlers_seq_start, |
900 | .next = input_handlers_seq_next, | |
901 | .stop = input_handlers_seq_stop, | |
902 | .show = input_handlers_seq_show, | |
903 | }; | |
904 | ||
905 | static int input_proc_handlers_open(struct inode *inode, struct file *file) | |
906 | { | |
907 | return seq_open(file, &input_handlers_seq_ops); | |
908 | } | |
909 | ||
2b8693c0 | 910 | static const struct file_operations input_handlers_fileops = { |
969b21cd DT |
911 | .owner = THIS_MODULE, |
912 | .open = input_proc_handlers_open, | |
913 | .read = seq_read, | |
914 | .llseek = seq_lseek, | |
915 | .release = seq_release, | |
916 | }; | |
f96b434d DT |
917 | |
918 | static int __init input_proc_init(void) | |
919 | { | |
920 | struct proc_dir_entry *entry; | |
921 | ||
9c37066d | 922 | proc_bus_input_dir = proc_mkdir("bus/input", NULL); |
f96b434d DT |
923 | if (!proc_bus_input_dir) |
924 | return -ENOMEM; | |
925 | ||
c7705f34 DL |
926 | entry = proc_create("devices", 0, proc_bus_input_dir, |
927 | &input_devices_fileops); | |
f96b434d DT |
928 | if (!entry) |
929 | goto fail1; | |
930 | ||
c7705f34 DL |
931 | entry = proc_create("handlers", 0, proc_bus_input_dir, |
932 | &input_handlers_fileops); | |
f96b434d DT |
933 | if (!entry) |
934 | goto fail2; | |
935 | ||
f96b434d DT |
936 | return 0; |
937 | ||
938 | fail2: remove_proc_entry("devices", proc_bus_input_dir); | |
9c37066d | 939 | fail1: remove_proc_entry("bus/input", NULL); |
f96b434d DT |
940 | return -ENOMEM; |
941 | } | |
942 | ||
beffbdc2 | 943 | static void input_proc_exit(void) |
f96b434d DT |
944 | { |
945 | remove_proc_entry("devices", proc_bus_input_dir); | |
946 | remove_proc_entry("handlers", proc_bus_input_dir); | |
9c37066d | 947 | remove_proc_entry("bus/input", NULL); |
f96b434d DT |
948 | } |
949 | ||
950 | #else /* !CONFIG_PROC_FS */ | |
951 | static inline void input_wakeup_procfs_readers(void) { } | |
952 | static inline int input_proc_init(void) { return 0; } | |
953 | static inline void input_proc_exit(void) { } | |
954 | #endif | |
955 | ||
9657d75c DT |
956 | #define INPUT_DEV_STRING_ATTR_SHOW(name) \ |
957 | static ssize_t input_dev_show_##name(struct device *dev, \ | |
958 | struct device_attribute *attr, \ | |
959 | char *buf) \ | |
960 | { \ | |
961 | struct input_dev *input_dev = to_input_dev(dev); \ | |
962 | \ | |
963 | return scnprintf(buf, PAGE_SIZE, "%s\n", \ | |
964 | input_dev->name ? input_dev->name : ""); \ | |
965 | } \ | |
966 | static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL) | |
5c1e9a6a DT |
967 | |
968 | INPUT_DEV_STRING_ATTR_SHOW(name); | |
969 | INPUT_DEV_STRING_ATTR_SHOW(phys); | |
970 | INPUT_DEV_STRING_ATTR_SHOW(uniq); | |
971 | ||
ac648a6a DT |
972 | static int input_print_modalias_bits(char *buf, int size, |
973 | char name, unsigned long *bm, | |
974 | unsigned int min_bit, unsigned int max_bit) | |
1d8f430c | 975 | { |
ac648a6a | 976 | int len = 0, i; |
1d8f430c | 977 | |
ac648a6a DT |
978 | len += snprintf(buf, max(size, 0), "%c", name); |
979 | for (i = min_bit; i < max_bit; i++) | |
7b19ada2 | 980 | if (bm[BIT_WORD(i)] & BIT_MASK(i)) |
ac648a6a | 981 | len += snprintf(buf + len, max(size - len, 0), "%X,", i); |
1d8f430c RR |
982 | return len; |
983 | } | |
984 | ||
2db66876 DT |
985 | static int input_print_modalias(char *buf, int size, struct input_dev *id, |
986 | int add_cr) | |
1d8f430c | 987 | { |
bd37e5a9 | 988 | int len; |
1d8f430c | 989 | |
ac648a6a DT |
990 | len = snprintf(buf, max(size, 0), |
991 | "input:b%04Xv%04Xp%04Xe%04X-", | |
992 | id->id.bustype, id->id.vendor, | |
993 | id->id.product, id->id.version); | |
994 | ||
995 | len += input_print_modalias_bits(buf + len, size - len, | |
996 | 'e', id->evbit, 0, EV_MAX); | |
997 | len += input_print_modalias_bits(buf + len, size - len, | |
998 | 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX); | |
999 | len += input_print_modalias_bits(buf + len, size - len, | |
1000 | 'r', id->relbit, 0, REL_MAX); | |
1001 | len += input_print_modalias_bits(buf + len, size - len, | |
1002 | 'a', id->absbit, 0, ABS_MAX); | |
1003 | len += input_print_modalias_bits(buf + len, size - len, | |
1004 | 'm', id->mscbit, 0, MSC_MAX); | |
1005 | len += input_print_modalias_bits(buf + len, size - len, | |
1006 | 'l', id->ledbit, 0, LED_MAX); | |
1007 | len += input_print_modalias_bits(buf + len, size - len, | |
1008 | 's', id->sndbit, 0, SND_MAX); | |
1009 | len += input_print_modalias_bits(buf + len, size - len, | |
1010 | 'f', id->ffbit, 0, FF_MAX); | |
1011 | len += input_print_modalias_bits(buf + len, size - len, | |
1012 | 'w', id->swbit, 0, SW_MAX); | |
2db66876 DT |
1013 | |
1014 | if (add_cr) | |
ac648a6a | 1015 | len += snprintf(buf + len, max(size - len, 0), "\n"); |
2db66876 | 1016 | |
bd37e5a9 KS |
1017 | return len; |
1018 | } | |
1019 | ||
9657d75c DT |
1020 | static ssize_t input_dev_show_modalias(struct device *dev, |
1021 | struct device_attribute *attr, | |
1022 | char *buf) | |
bd37e5a9 KS |
1023 | { |
1024 | struct input_dev *id = to_input_dev(dev); | |
1025 | ssize_t len; | |
1026 | ||
2db66876 DT |
1027 | len = input_print_modalias(buf, PAGE_SIZE, id, 1); |
1028 | ||
8a3cf456 | 1029 | return min_t(int, len, PAGE_SIZE); |
1d8f430c | 1030 | } |
9657d75c | 1031 | static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL); |
1d8f430c | 1032 | |
629b77a4 | 1033 | static struct attribute *input_dev_attrs[] = { |
9657d75c DT |
1034 | &dev_attr_name.attr, |
1035 | &dev_attr_phys.attr, | |
1036 | &dev_attr_uniq.attr, | |
1037 | &dev_attr_modalias.attr, | |
629b77a4 GKH |
1038 | NULL |
1039 | }; | |
1040 | ||
bd0ef235 | 1041 | static struct attribute_group input_dev_attr_group = { |
629b77a4 | 1042 | .attrs = input_dev_attrs, |
5c1e9a6a DT |
1043 | }; |
1044 | ||
9657d75c DT |
1045 | #define INPUT_DEV_ID_ATTR(name) \ |
1046 | static ssize_t input_dev_show_id_##name(struct device *dev, \ | |
1047 | struct device_attribute *attr, \ | |
1048 | char *buf) \ | |
1049 | { \ | |
1050 | struct input_dev *input_dev = to_input_dev(dev); \ | |
1051 | return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \ | |
1052 | } \ | |
1053 | static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL) | |
5c1e9a6a DT |
1054 | |
1055 | INPUT_DEV_ID_ATTR(bustype); | |
1056 | INPUT_DEV_ID_ATTR(vendor); | |
1057 | INPUT_DEV_ID_ATTR(product); | |
1058 | INPUT_DEV_ID_ATTR(version); | |
1059 | ||
1060 | static struct attribute *input_dev_id_attrs[] = { | |
9657d75c DT |
1061 | &dev_attr_bustype.attr, |
1062 | &dev_attr_vendor.attr, | |
1063 | &dev_attr_product.attr, | |
1064 | &dev_attr_version.attr, | |
5c1e9a6a DT |
1065 | NULL |
1066 | }; | |
1067 | ||
1068 | static struct attribute_group input_dev_id_attr_group = { | |
1069 | .name = "id", | |
1070 | .attrs = input_dev_id_attrs, | |
1071 | }; | |
1072 | ||
969b21cd DT |
1073 | static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap, |
1074 | int max, int add_cr) | |
1075 | { | |
1076 | int i; | |
1077 | int len = 0; | |
1078 | ||
7b19ada2 | 1079 | for (i = BITS_TO_LONGS(max) - 1; i > 0; i--) |
969b21cd DT |
1080 | if (bitmap[i]) |
1081 | break; | |
1082 | ||
1083 | for (; i >= 0; i--) | |
1084 | len += snprintf(buf + len, max(buf_size - len, 0), | |
1085 | "%lx%s", bitmap[i], i > 0 ? " " : ""); | |
1086 | ||
1087 | if (add_cr) | |
1088 | len += snprintf(buf + len, max(buf_size - len, 0), "\n"); | |
1089 | ||
1090 | return len; | |
1091 | } | |
1092 | ||
9657d75c DT |
1093 | #define INPUT_DEV_CAP_ATTR(ev, bm) \ |
1094 | static ssize_t input_dev_show_cap_##bm(struct device *dev, \ | |
1095 | struct device_attribute *attr, \ | |
1096 | char *buf) \ | |
1097 | { \ | |
1098 | struct input_dev *input_dev = to_input_dev(dev); \ | |
1099 | int len = input_print_bitmap(buf, PAGE_SIZE, \ | |
1100 | input_dev->bm##bit, ev##_MAX, 1); \ | |
1101 | return min_t(int, len, PAGE_SIZE); \ | |
1102 | } \ | |
1103 | static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL) | |
5c1e9a6a DT |
1104 | |
1105 | INPUT_DEV_CAP_ATTR(EV, ev); | |
1106 | INPUT_DEV_CAP_ATTR(KEY, key); | |
1107 | INPUT_DEV_CAP_ATTR(REL, rel); | |
1108 | INPUT_DEV_CAP_ATTR(ABS, abs); | |
1109 | INPUT_DEV_CAP_ATTR(MSC, msc); | |
1110 | INPUT_DEV_CAP_ATTR(LED, led); | |
1111 | INPUT_DEV_CAP_ATTR(SND, snd); | |
1112 | INPUT_DEV_CAP_ATTR(FF, ff); | |
1113 | INPUT_DEV_CAP_ATTR(SW, sw); | |
1114 | ||
1115 | static struct attribute *input_dev_caps_attrs[] = { | |
9657d75c DT |
1116 | &dev_attr_ev.attr, |
1117 | &dev_attr_key.attr, | |
1118 | &dev_attr_rel.attr, | |
1119 | &dev_attr_abs.attr, | |
1120 | &dev_attr_msc.attr, | |
1121 | &dev_attr_led.attr, | |
1122 | &dev_attr_snd.attr, | |
1123 | &dev_attr_ff.attr, | |
1124 | &dev_attr_sw.attr, | |
5c1e9a6a DT |
1125 | NULL |
1126 | }; | |
1127 | ||
1128 | static struct attribute_group input_dev_caps_attr_group = { | |
1129 | .name = "capabilities", | |
1130 | .attrs = input_dev_caps_attrs, | |
1131 | }; | |
1132 | ||
cb9def4d DT |
1133 | static struct attribute_group *input_dev_attr_groups[] = { |
1134 | &input_dev_attr_group, | |
1135 | &input_dev_id_attr_group, | |
1136 | &input_dev_caps_attr_group, | |
1137 | NULL | |
1138 | }; | |
1139 | ||
9657d75c | 1140 | static void input_dev_release(struct device *device) |
d19fbe8a | 1141 | { |
9657d75c | 1142 | struct input_dev *dev = to_input_dev(device); |
d19fbe8a | 1143 | |
509ca1a9 | 1144 | input_ff_destroy(dev); |
d19fbe8a | 1145 | kfree(dev); |
509ca1a9 | 1146 | |
d19fbe8a DT |
1147 | module_put(THIS_MODULE); |
1148 | } | |
1149 | ||
a7fadbe1 | 1150 | /* |
312c004d | 1151 | * Input uevent interface - loading event handlers based on |
a7fadbe1 DT |
1152 | * device bitfields. |
1153 | */ | |
7eff2e7a | 1154 | static int input_add_uevent_bm_var(struct kobj_uevent_env *env, |
ac648a6a | 1155 | const char *name, unsigned long *bitmap, int max) |
a7fadbe1 | 1156 | { |
7eff2e7a | 1157 | int len; |
a7fadbe1 | 1158 | |
7eff2e7a | 1159 | if (add_uevent_var(env, "%s=", name)) |
a7fadbe1 DT |
1160 | return -ENOMEM; |
1161 | ||
7eff2e7a KS |
1162 | len = input_print_bitmap(&env->buf[env->buflen - 1], |
1163 | sizeof(env->buf) - env->buflen, | |
1164 | bitmap, max, 0); | |
1165 | if (len >= (sizeof(env->buf) - env->buflen)) | |
a7fadbe1 DT |
1166 | return -ENOMEM; |
1167 | ||
7eff2e7a | 1168 | env->buflen += len; |
a7fadbe1 DT |
1169 | return 0; |
1170 | } | |
1171 | ||
7eff2e7a | 1172 | static int input_add_uevent_modalias_var(struct kobj_uevent_env *env, |
ac648a6a DT |
1173 | struct input_dev *dev) |
1174 | { | |
7eff2e7a | 1175 | int len; |
ac648a6a | 1176 | |
7eff2e7a | 1177 | if (add_uevent_var(env, "MODALIAS=")) |
ac648a6a DT |
1178 | return -ENOMEM; |
1179 | ||
7eff2e7a KS |
1180 | len = input_print_modalias(&env->buf[env->buflen - 1], |
1181 | sizeof(env->buf) - env->buflen, | |
1182 | dev, 0); | |
1183 | if (len >= (sizeof(env->buf) - env->buflen)) | |
ac648a6a DT |
1184 | return -ENOMEM; |
1185 | ||
7eff2e7a | 1186 | env->buflen += len; |
ac648a6a DT |
1187 | return 0; |
1188 | } | |
1189 | ||
a7fadbe1 DT |
1190 | #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \ |
1191 | do { \ | |
7eff2e7a | 1192 | int err = add_uevent_var(env, fmt, val); \ |
a7fadbe1 DT |
1193 | if (err) \ |
1194 | return err; \ | |
1195 | } while (0) | |
1196 | ||
1197 | #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \ | |
1198 | do { \ | |
7eff2e7a | 1199 | int err = input_add_uevent_bm_var(env, name, bm, max); \ |
a7fadbe1 DT |
1200 | if (err) \ |
1201 | return err; \ | |
1202 | } while (0) | |
1203 | ||
ac648a6a DT |
1204 | #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \ |
1205 | do { \ | |
7eff2e7a | 1206 | int err = input_add_uevent_modalias_var(env, dev); \ |
ac648a6a DT |
1207 | if (err) \ |
1208 | return err; \ | |
1209 | } while (0) | |
1210 | ||
7eff2e7a | 1211 | static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env) |
a7fadbe1 | 1212 | { |
9657d75c | 1213 | struct input_dev *dev = to_input_dev(device); |
a7fadbe1 DT |
1214 | |
1215 | INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x", | |
1216 | dev->id.bustype, dev->id.vendor, | |
1217 | dev->id.product, dev->id.version); | |
1218 | if (dev->name) | |
1219 | INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name); | |
1220 | if (dev->phys) | |
1221 | INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys); | |
08de1f04 | 1222 | if (dev->uniq) |
a7fadbe1 DT |
1223 | INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq); |
1224 | ||
1225 | INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX); | |
1226 | if (test_bit(EV_KEY, dev->evbit)) | |
1227 | INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX); | |
1228 | if (test_bit(EV_REL, dev->evbit)) | |
1229 | INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX); | |
1230 | if (test_bit(EV_ABS, dev->evbit)) | |
1231 | INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX); | |
1232 | if (test_bit(EV_MSC, dev->evbit)) | |
1233 | INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX); | |
1234 | if (test_bit(EV_LED, dev->evbit)) | |
1235 | INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX); | |
1236 | if (test_bit(EV_SND, dev->evbit)) | |
1237 | INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX); | |
1238 | if (test_bit(EV_FF, dev->evbit)) | |
1239 | INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX); | |
1240 | if (test_bit(EV_SW, dev->evbit)) | |
1241 | INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX); | |
1242 | ||
ac648a6a | 1243 | INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev); |
a7fadbe1 DT |
1244 | |
1245 | return 0; | |
1246 | } | |
1247 | ||
9657d75c DT |
1248 | static struct device_type input_dev_type = { |
1249 | .groups = input_dev_attr_groups, | |
1250 | .release = input_dev_release, | |
1251 | .uevent = input_dev_uevent, | |
1252 | }; | |
1253 | ||
ea9f240b | 1254 | struct class input_class = { |
9657d75c | 1255 | .name = "input", |
d19fbe8a | 1256 | }; |
ca56fe07 | 1257 | EXPORT_SYMBOL_GPL(input_class); |
d19fbe8a | 1258 | |
1447190e DT |
1259 | /** |
1260 | * input_allocate_device - allocate memory for new input device | |
1261 | * | |
1262 | * Returns prepared struct input_dev or NULL. | |
1263 | * | |
1264 | * NOTE: Use input_free_device() to free devices that have not been | |
1265 | * registered; input_unregister_device() should be used for already | |
1266 | * registered devices. | |
1267 | */ | |
d19fbe8a DT |
1268 | struct input_dev *input_allocate_device(void) |
1269 | { | |
1270 | struct input_dev *dev; | |
1271 | ||
1272 | dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL); | |
1273 | if (dev) { | |
9657d75c DT |
1274 | dev->dev.type = &input_dev_type; |
1275 | dev->dev.class = &input_class; | |
1276 | device_initialize(&dev->dev); | |
f60d2b11 | 1277 | mutex_init(&dev->mutex); |
8006479c | 1278 | spin_lock_init(&dev->event_lock); |
d19fbe8a DT |
1279 | INIT_LIST_HEAD(&dev->h_list); |
1280 | INIT_LIST_HEAD(&dev->node); | |
655816e4 DT |
1281 | |
1282 | __module_get(THIS_MODULE); | |
d19fbe8a DT |
1283 | } |
1284 | ||
1285 | return dev; | |
1286 | } | |
ca56fe07 | 1287 | EXPORT_SYMBOL(input_allocate_device); |
d19fbe8a | 1288 | |
1447190e DT |
1289 | /** |
1290 | * input_free_device - free memory occupied by input_dev structure | |
1291 | * @dev: input device to free | |
1292 | * | |
1293 | * This function should only be used if input_register_device() | |
1294 | * was not called yet or if it failed. Once device was registered | |
1295 | * use input_unregister_device() and memory will be freed once last | |
8006479c | 1296 | * reference to the device is dropped. |
1447190e DT |
1297 | * |
1298 | * Device should be allocated by input_allocate_device(). | |
1299 | * | |
1300 | * NOTE: If there are references to the input device then memory | |
1301 | * will not be freed until last reference is dropped. | |
1302 | */ | |
f60d2b11 DT |
1303 | void input_free_device(struct input_dev *dev) |
1304 | { | |
54f9e36c | 1305 | if (dev) |
f60d2b11 | 1306 | input_put_device(dev); |
f60d2b11 | 1307 | } |
ca56fe07 | 1308 | EXPORT_SYMBOL(input_free_device); |
f60d2b11 | 1309 | |
534565f2 DT |
1310 | /** |
1311 | * input_set_capability - mark device as capable of a certain event | |
1312 | * @dev: device that is capable of emitting or accepting event | |
1313 | * @type: type of the event (EV_KEY, EV_REL, etc...) | |
1314 | * @code: event code | |
1315 | * | |
1316 | * In addition to setting up corresponding bit in appropriate capability | |
1317 | * bitmap the function also adjusts dev->evbit. | |
1318 | */ | |
1319 | void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code) | |
1320 | { | |
1321 | switch (type) { | |
1322 | case EV_KEY: | |
1323 | __set_bit(code, dev->keybit); | |
1324 | break; | |
1325 | ||
1326 | case EV_REL: | |
1327 | __set_bit(code, dev->relbit); | |
1328 | break; | |
1329 | ||
1330 | case EV_ABS: | |
1331 | __set_bit(code, dev->absbit); | |
1332 | break; | |
1333 | ||
1334 | case EV_MSC: | |
1335 | __set_bit(code, dev->mscbit); | |
1336 | break; | |
1337 | ||
1338 | case EV_SW: | |
1339 | __set_bit(code, dev->swbit); | |
1340 | break; | |
1341 | ||
1342 | case EV_LED: | |
1343 | __set_bit(code, dev->ledbit); | |
1344 | break; | |
1345 | ||
1346 | case EV_SND: | |
1347 | __set_bit(code, dev->sndbit); | |
1348 | break; | |
1349 | ||
1350 | case EV_FF: | |
1351 | __set_bit(code, dev->ffbit); | |
1352 | break; | |
1353 | ||
22d1c398 DB |
1354 | case EV_PWR: |
1355 | /* do nothing */ | |
1356 | break; | |
1357 | ||
534565f2 DT |
1358 | default: |
1359 | printk(KERN_ERR | |
1360 | "input_set_capability: unknown type %u (code %u)\n", | |
1361 | type, code); | |
1362 | dump_stack(); | |
1363 | return; | |
1364 | } | |
1365 | ||
1366 | __set_bit(type, dev->evbit); | |
1367 | } | |
1368 | EXPORT_SYMBOL(input_set_capability); | |
1369 | ||
8006479c DT |
1370 | /** |
1371 | * input_register_device - register device with input core | |
1372 | * @dev: device to be registered | |
1373 | * | |
1374 | * This function registers device with input core. The device must be | |
1375 | * allocated with input_allocate_device() and all it's capabilities | |
1376 | * set up before registering. | |
1377 | * If function fails the device must be freed with input_free_device(). | |
1378 | * Once device has been successfully registered it can be unregistered | |
1379 | * with input_unregister_device(); input_free_device() should not be | |
1380 | * called in this case. | |
1381 | */ | |
5f945489 | 1382 | int input_register_device(struct input_dev *dev) |
1da177e4 | 1383 | { |
bd0ef235 | 1384 | static atomic_t input_no = ATOMIC_INIT(0); |
1da177e4 | 1385 | struct input_handler *handler; |
bd0ef235 DT |
1386 | const char *path; |
1387 | int error; | |
1da177e4 | 1388 | |
8006479c | 1389 | __set_bit(EV_SYN, dev->evbit); |
0fbf87ca | 1390 | |
1da177e4 LT |
1391 | /* |
1392 | * If delay and period are pre-set by the driver, then autorepeating | |
1393 | * is handled by the driver itself and we don't do it in input.c. | |
1394 | */ | |
1395 | ||
1396 | init_timer(&dev->timer); | |
1397 | if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) { | |
1398 | dev->timer.data = (long) dev; | |
1399 | dev->timer.function = input_repeat_key; | |
1400 | dev->rep[REP_DELAY] = 250; | |
1401 | dev->rep[REP_PERIOD] = 33; | |
1402 | } | |
1403 | ||
c8e4c772 MR |
1404 | if (!dev->getkeycode) |
1405 | dev->getkeycode = input_default_getkeycode; | |
1406 | ||
1407 | if (!dev->setkeycode) | |
1408 | dev->setkeycode = input_default_setkeycode; | |
1409 | ||
a6c2490f KS |
1410 | dev_set_name(&dev->dev, "input%ld", |
1411 | (unsigned long) atomic_inc_return(&input_no) - 1); | |
bd0ef235 | 1412 | |
9657d75c | 1413 | error = device_add(&dev->dev); |
bd0ef235 DT |
1414 | if (error) |
1415 | return error; | |
1416 | ||
9657d75c | 1417 | path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); |
bd0ef235 DT |
1418 | printk(KERN_INFO "input: %s as %s\n", |
1419 | dev->name ? dev->name : "Unspecified device", path ? path : "N/A"); | |
1420 | kfree(path); | |
10204020 | 1421 | |
8006479c DT |
1422 | error = mutex_lock_interruptible(&input_mutex); |
1423 | if (error) { | |
1424 | device_del(&dev->dev); | |
1425 | return error; | |
1426 | } | |
1427 | ||
1428 | list_add_tail(&dev->node, &input_dev_list); | |
1429 | ||
1da177e4 | 1430 | list_for_each_entry(handler, &input_handler_list, node) |
5b2a0826 | 1431 | input_attach_handler(dev, handler); |
1da177e4 | 1432 | |
f96b434d | 1433 | input_wakeup_procfs_readers(); |
5f945489 | 1434 | |
8006479c DT |
1435 | mutex_unlock(&input_mutex); |
1436 | ||
5f945489 | 1437 | return 0; |
1da177e4 | 1438 | } |
ca56fe07 | 1439 | EXPORT_SYMBOL(input_register_device); |
1da177e4 | 1440 | |
8006479c DT |
1441 | /** |
1442 | * input_unregister_device - unregister previously registered device | |
1443 | * @dev: device to be unregistered | |
1444 | * | |
1445 | * This function unregisters an input device. Once device is unregistered | |
1446 | * the caller should not try to access it as it may get freed at any moment. | |
1447 | */ | |
1da177e4 LT |
1448 | void input_unregister_device(struct input_dev *dev) |
1449 | { | |
5b2a0826 | 1450 | struct input_handle *handle, *next; |
1da177e4 | 1451 | |
8006479c | 1452 | input_disconnect_device(dev); |
1da177e4 | 1453 | |
8006479c | 1454 | mutex_lock(&input_mutex); |
1da177e4 | 1455 | |
5b2a0826 | 1456 | list_for_each_entry_safe(handle, next, &dev->h_list, d_node) |
1da177e4 | 1457 | handle->handler->disconnect(handle); |
5b2a0826 | 1458 | WARN_ON(!list_empty(&dev->h_list)); |
1da177e4 | 1459 | |
8006479c | 1460 | del_timer_sync(&dev->timer); |
1da177e4 LT |
1461 | list_del_init(&dev->node); |
1462 | ||
f96b434d | 1463 | input_wakeup_procfs_readers(); |
8006479c DT |
1464 | |
1465 | mutex_unlock(&input_mutex); | |
1466 | ||
1467 | device_unregister(&dev->dev); | |
1da177e4 | 1468 | } |
ca56fe07 | 1469 | EXPORT_SYMBOL(input_unregister_device); |
1da177e4 | 1470 | |
8006479c DT |
1471 | /** |
1472 | * input_register_handler - register a new input handler | |
1473 | * @handler: handler to be registered | |
1474 | * | |
1475 | * This function registers a new input handler (interface) for input | |
1476 | * devices in the system and attaches it to all input devices that | |
1477 | * are compatible with the handler. | |
1478 | */ | |
4263cf0f | 1479 | int input_register_handler(struct input_handler *handler) |
1da177e4 LT |
1480 | { |
1481 | struct input_dev *dev; | |
8006479c DT |
1482 | int retval; |
1483 | ||
1484 | retval = mutex_lock_interruptible(&input_mutex); | |
1485 | if (retval) | |
1486 | return retval; | |
1da177e4 | 1487 | |
1da177e4 LT |
1488 | INIT_LIST_HEAD(&handler->h_list); |
1489 | ||
4263cf0f | 1490 | if (handler->fops != NULL) { |
8006479c DT |
1491 | if (input_table[handler->minor >> 5]) { |
1492 | retval = -EBUSY; | |
1493 | goto out; | |
1494 | } | |
1da177e4 | 1495 | input_table[handler->minor >> 5] = handler; |
4263cf0f | 1496 | } |
1da177e4 LT |
1497 | |
1498 | list_add_tail(&handler->node, &input_handler_list); | |
1499 | ||
1500 | list_for_each_entry(dev, &input_dev_list, node) | |
5b2a0826 | 1501 | input_attach_handler(dev, handler); |
1da177e4 | 1502 | |
f96b434d | 1503 | input_wakeup_procfs_readers(); |
8006479c DT |
1504 | |
1505 | out: | |
1506 | mutex_unlock(&input_mutex); | |
1507 | return retval; | |
1da177e4 | 1508 | } |
ca56fe07 | 1509 | EXPORT_SYMBOL(input_register_handler); |
1da177e4 | 1510 | |
8006479c DT |
1511 | /** |
1512 | * input_unregister_handler - unregisters an input handler | |
1513 | * @handler: handler to be unregistered | |
1514 | * | |
1515 | * This function disconnects a handler from its input devices and | |
1516 | * removes it from lists of known handlers. | |
1517 | */ | |
1da177e4 LT |
1518 | void input_unregister_handler(struct input_handler *handler) |
1519 | { | |
5b2a0826 | 1520 | struct input_handle *handle, *next; |
1da177e4 | 1521 | |
8006479c DT |
1522 | mutex_lock(&input_mutex); |
1523 | ||
5b2a0826 | 1524 | list_for_each_entry_safe(handle, next, &handler->h_list, h_node) |
1da177e4 | 1525 | handler->disconnect(handle); |
5b2a0826 | 1526 | WARN_ON(!list_empty(&handler->h_list)); |
1da177e4 LT |
1527 | |
1528 | list_del_init(&handler->node); | |
1529 | ||
1530 | if (handler->fops != NULL) | |
1531 | input_table[handler->minor >> 5] = NULL; | |
1532 | ||
f96b434d | 1533 | input_wakeup_procfs_readers(); |
8006479c DT |
1534 | |
1535 | mutex_unlock(&input_mutex); | |
1da177e4 | 1536 | } |
ca56fe07 | 1537 | EXPORT_SYMBOL(input_unregister_handler); |
1da177e4 | 1538 | |
8006479c DT |
1539 | /** |
1540 | * input_register_handle - register a new input handle | |
1541 | * @handle: handle to register | |
1542 | * | |
1543 | * This function puts a new input handle onto device's | |
1544 | * and handler's lists so that events can flow through | |
1545 | * it once it is opened using input_open_device(). | |
1546 | * | |
1547 | * This function is supposed to be called from handler's | |
1548 | * connect() method. | |
1549 | */ | |
5b2a0826 DT |
1550 | int input_register_handle(struct input_handle *handle) |
1551 | { | |
1552 | struct input_handler *handler = handle->handler; | |
8006479c DT |
1553 | struct input_dev *dev = handle->dev; |
1554 | int error; | |
1555 | ||
1556 | /* | |
1557 | * We take dev->mutex here to prevent race with | |
1558 | * input_release_device(). | |
1559 | */ | |
1560 | error = mutex_lock_interruptible(&dev->mutex); | |
1561 | if (error) | |
1562 | return error; | |
1563 | list_add_tail_rcu(&handle->d_node, &dev->h_list); | |
1564 | mutex_unlock(&dev->mutex); | |
5b2a0826 | 1565 | |
8006479c DT |
1566 | /* |
1567 | * Since we are supposed to be called from ->connect() | |
1568 | * which is mutually exclusive with ->disconnect() | |
1569 | * we can't be racing with input_unregister_handle() | |
1570 | * and so separate lock is not needed here. | |
1571 | */ | |
5b2a0826 DT |
1572 | list_add_tail(&handle->h_node, &handler->h_list); |
1573 | ||
1574 | if (handler->start) | |
1575 | handler->start(handle); | |
1576 | ||
1577 | return 0; | |
1578 | } | |
1579 | EXPORT_SYMBOL(input_register_handle); | |
1580 | ||
8006479c DT |
1581 | /** |
1582 | * input_unregister_handle - unregister an input handle | |
1583 | * @handle: handle to unregister | |
1584 | * | |
1585 | * This function removes input handle from device's | |
1586 | * and handler's lists. | |
1587 | * | |
1588 | * This function is supposed to be called from handler's | |
1589 | * disconnect() method. | |
1590 | */ | |
5b2a0826 DT |
1591 | void input_unregister_handle(struct input_handle *handle) |
1592 | { | |
8006479c DT |
1593 | struct input_dev *dev = handle->dev; |
1594 | ||
5b2a0826 | 1595 | list_del_init(&handle->h_node); |
8006479c DT |
1596 | |
1597 | /* | |
1598 | * Take dev->mutex to prevent race with input_release_device(). | |
1599 | */ | |
1600 | mutex_lock(&dev->mutex); | |
1601 | list_del_rcu(&handle->d_node); | |
1602 | mutex_unlock(&dev->mutex); | |
82ba56c2 | 1603 | synchronize_rcu(); |
5b2a0826 DT |
1604 | } |
1605 | EXPORT_SYMBOL(input_unregister_handle); | |
1606 | ||
1da177e4 LT |
1607 | static int input_open_file(struct inode *inode, struct file *file) |
1608 | { | |
2edbf853 | 1609 | struct input_handler *handler; |
99ac48f5 | 1610 | const struct file_operations *old_fops, *new_fops = NULL; |
1da177e4 LT |
1611 | int err; |
1612 | ||
2edbf853 | 1613 | lock_kernel(); |
1da177e4 | 1614 | /* No load-on-demand here? */ |
2edbf853 JC |
1615 | handler = input_table[iminor(inode) >> 5]; |
1616 | if (!handler || !(new_fops = fops_get(handler->fops))) { | |
1617 | err = -ENODEV; | |
1618 | goto out; | |
1619 | } | |
1da177e4 LT |
1620 | |
1621 | /* | |
1622 | * That's _really_ odd. Usually NULL ->open means "nothing special", | |
1623 | * not "no device". Oh, well... | |
1624 | */ | |
1625 | if (!new_fops->open) { | |
1626 | fops_put(new_fops); | |
2edbf853 JC |
1627 | err = -ENODEV; |
1628 | goto out; | |
1da177e4 LT |
1629 | } |
1630 | old_fops = file->f_op; | |
1631 | file->f_op = new_fops; | |
1632 | ||
1633 | err = new_fops->open(inode, file); | |
1634 | ||
1635 | if (err) { | |
1636 | fops_put(file->f_op); | |
1637 | file->f_op = fops_get(old_fops); | |
1638 | } | |
1639 | fops_put(old_fops); | |
2edbf853 JC |
1640 | out: |
1641 | unlock_kernel(); | |
1da177e4 LT |
1642 | return err; |
1643 | } | |
1644 | ||
2b8693c0 | 1645 | static const struct file_operations input_fops = { |
1da177e4 LT |
1646 | .owner = THIS_MODULE, |
1647 | .open = input_open_file, | |
1648 | }; | |
1649 | ||
61994a61 HR |
1650 | static void __init input_init_abs_bypass(void) |
1651 | { | |
1652 | const unsigned int *p; | |
1653 | ||
1654 | for (p = input_abs_bypass_init_data; *p; p++) | |
1655 | input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p); | |
1656 | } | |
1657 | ||
f96b434d | 1658 | static int __init input_init(void) |
1da177e4 | 1659 | { |
f96b434d | 1660 | int err; |
1da177e4 | 1661 | |
61994a61 HR |
1662 | input_init_abs_bypass(); |
1663 | ||
ea9f240b | 1664 | err = class_register(&input_class); |
d19fbe8a DT |
1665 | if (err) { |
1666 | printk(KERN_ERR "input: unable to register input_dev class\n"); | |
1667 | return err; | |
1668 | } | |
1669 | ||
f96b434d DT |
1670 | err = input_proc_init(); |
1671 | if (err) | |
b0fdfebb | 1672 | goto fail1; |
1da177e4 | 1673 | |
f96b434d DT |
1674 | err = register_chrdev(INPUT_MAJOR, "input", &input_fops); |
1675 | if (err) { | |
1676 | printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR); | |
b0fdfebb | 1677 | goto fail2; |
1da177e4 | 1678 | } |
e334016f | 1679 | |
1da177e4 | 1680 | return 0; |
1da177e4 | 1681 | |
b0fdfebb | 1682 | fail2: input_proc_exit(); |
ea9f240b | 1683 | fail1: class_unregister(&input_class); |
f96b434d | 1684 | return err; |
1da177e4 LT |
1685 | } |
1686 | ||
1687 | static void __exit input_exit(void) | |
1688 | { | |
f96b434d | 1689 | input_proc_exit(); |
1da177e4 | 1690 | unregister_chrdev(INPUT_MAJOR, "input"); |
ea9f240b | 1691 | class_unregister(&input_class); |
1da177e4 LT |
1692 | } |
1693 | ||
1694 | subsys_initcall(input_init); | |
1695 | module_exit(input_exit); |