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