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