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