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