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