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