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