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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/smp_lock.h> |
| 15 | #include <linux/input.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/random.h> |
| 18 | #include <linux/major.h> |
| 19 | #include <linux/proc_fs.h> |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/poll.h> |
| 23 | #include <linux/device.h> |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); |
| 27 | MODULE_DESCRIPTION("Input core"); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #define INPUT_DEVICES 256 |
| 31 | |
| 32 | static LIST_HEAD(input_dev_list); |
| 33 | static LIST_HEAD(input_handler_list); |
| 34 | |
| 35 | static struct input_handler *input_table[8]; |
| 36 | |
Dmitry Torokhov | 0e739d2 | 2006-07-06 00:22:43 -0400 | [diff] [blame] | 37 | /** |
| 38 | * input_event() - report new input event |
Dmitry Torokhov | 1447190 | 2006-11-02 23:26:55 -0500 | [diff] [blame] | 39 | * @dev: device that generated the event |
Dmitry Torokhov | 0e739d2 | 2006-07-06 00:22:43 -0400 | [diff] [blame] | 40 | * @type: type of the event |
| 41 | * @code: event code |
| 42 | * @value: value of the event |
| 43 | * |
| 44 | * This function should be used by drivers implementing various input devices |
| 45 | * See also input_inject_event() |
| 46 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 48 | { |
| 49 | struct input_handle *handle; |
| 50 | |
| 51 | if (type > EV_MAX || !test_bit(type, dev->evbit)) |
| 52 | return; |
| 53 | |
| 54 | add_input_randomness(type, code, value); |
| 55 | |
| 56 | switch (type) { |
| 57 | |
| 58 | case EV_SYN: |
| 59 | switch (code) { |
| 60 | case SYN_CONFIG: |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 61 | if (dev->event) |
| 62 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | break; |
| 64 | |
| 65 | case SYN_REPORT: |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 66 | if (dev->sync) |
| 67 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | dev->sync = 1; |
| 69 | break; |
| 70 | } |
| 71 | break; |
| 72 | |
| 73 | case EV_KEY: |
| 74 | |
| 75 | if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) |
| 76 | return; |
| 77 | |
| 78 | if (value == 2) |
| 79 | break; |
| 80 | |
| 81 | change_bit(code, dev->key); |
| 82 | |
| 83 | if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) { |
| 84 | dev->repeat_key = code; |
| 85 | mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); |
| 86 | } |
| 87 | |
| 88 | break; |
| 89 | |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 90 | case EV_SW: |
| 91 | |
| 92 | if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value) |
| 93 | return; |
| 94 | |
| 95 | change_bit(code, dev->sw); |
| 96 | |
| 97 | break; |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | case EV_ABS: |
| 100 | |
| 101 | if (code > ABS_MAX || !test_bit(code, dev->absbit)) |
| 102 | return; |
| 103 | |
| 104 | if (dev->absfuzz[code]) { |
| 105 | if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) && |
| 106 | (value < dev->abs[code] + (dev->absfuzz[code] >> 1))) |
| 107 | return; |
| 108 | |
| 109 | if ((value > dev->abs[code] - dev->absfuzz[code]) && |
| 110 | (value < dev->abs[code] + dev->absfuzz[code])) |
| 111 | value = (dev->abs[code] * 3 + value) >> 2; |
| 112 | |
| 113 | if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) && |
| 114 | (value < dev->abs[code] + (dev->absfuzz[code] << 1))) |
| 115 | value = (dev->abs[code] + value) >> 1; |
| 116 | } |
| 117 | |
| 118 | if (dev->abs[code] == value) |
| 119 | return; |
| 120 | |
| 121 | dev->abs[code] = value; |
| 122 | break; |
| 123 | |
| 124 | case EV_REL: |
| 125 | |
| 126 | if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0)) |
| 127 | return; |
| 128 | |
| 129 | break; |
| 130 | |
| 131 | case EV_MSC: |
| 132 | |
| 133 | if (code > MSC_MAX || !test_bit(code, dev->mscbit)) |
| 134 | return; |
| 135 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 136 | if (dev->event) |
| 137 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | break; |
| 140 | |
| 141 | case EV_LED: |
| 142 | |
| 143 | if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value) |
| 144 | return; |
| 145 | |
| 146 | change_bit(code, dev->led); |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 147 | |
| 148 | if (dev->event) |
| 149 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | break; |
| 152 | |
| 153 | case EV_SND: |
| 154 | |
| 155 | if (code > SND_MAX || !test_bit(code, dev->sndbit)) |
| 156 | return; |
| 157 | |
Dmitry Torokhov | 8fdc194 | 2006-04-29 01:13:48 -0400 | [diff] [blame] | 158 | if (!!test_bit(code, dev->snd) != !!value) |
| 159 | change_bit(code, dev->snd); |
| 160 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 161 | if (dev->event) |
| 162 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | break; |
| 165 | |
| 166 | case EV_REP: |
| 167 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 168 | if (code > REP_MAX || value < 0 || dev->rep[code] == value) |
| 169 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | dev->rep[code] = value; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 172 | if (dev->event) |
| 173 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | break; |
| 176 | |
| 177 | case EV_FF: |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 178 | |
| 179 | if (value < 0) |
| 180 | return; |
| 181 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 182 | if (dev->event) |
| 183 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | |
| 187 | if (type != EV_SYN) |
| 188 | dev->sync = 0; |
| 189 | |
| 190 | if (dev->grab) |
| 191 | dev->grab->handler->event(dev->grab, type, code, value); |
| 192 | else |
| 193 | list_for_each_entry(handle, &dev->h_list, d_node) |
| 194 | if (handle->open) |
| 195 | handle->handler->event(handle, type, code, value); |
| 196 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 197 | EXPORT_SYMBOL(input_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Dmitry Torokhov | 0e739d2 | 2006-07-06 00:22:43 -0400 | [diff] [blame] | 199 | /** |
| 200 | * input_inject_event() - send input event from input handler |
| 201 | * @handle: input handle to send event through |
| 202 | * @type: type of the event |
| 203 | * @code: event code |
| 204 | * @value: value of the event |
| 205 | * |
| 206 | * Similar to input_event() but will ignore event if device is "grabbed" and handle |
| 207 | * injecting event is not the one that owns the device. |
| 208 | */ |
| 209 | void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) |
| 210 | { |
| 211 | if (!handle->dev->grab || handle->dev->grab == handle) |
| 212 | input_event(handle->dev, type, code, value); |
| 213 | } |
| 214 | EXPORT_SYMBOL(input_inject_event); |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | static void input_repeat_key(unsigned long data) |
| 217 | { |
| 218 | struct input_dev *dev = (void *) data; |
| 219 | |
| 220 | if (!test_bit(dev->repeat_key, dev->key)) |
| 221 | return; |
| 222 | |
| 223 | input_event(dev, EV_KEY, dev->repeat_key, 2); |
| 224 | input_sync(dev); |
| 225 | |
| 226 | if (dev->rep[REP_PERIOD]) |
| 227 | mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD])); |
| 228 | } |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | int input_grab_device(struct input_handle *handle) |
| 231 | { |
| 232 | if (handle->dev->grab) |
| 233 | return -EBUSY; |
| 234 | |
| 235 | handle->dev->grab = handle; |
| 236 | return 0; |
| 237 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 238 | EXPORT_SYMBOL(input_grab_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | void input_release_device(struct input_handle *handle) |
| 241 | { |
Andrew Morton | a2b2ed2 | 2006-07-15 01:17:38 -0400 | [diff] [blame] | 242 | struct input_dev *dev = handle->dev; |
Dmitry Torokhov | c7e8dc6 | 2006-07-06 00:21:03 -0400 | [diff] [blame] | 243 | |
Andrew Morton | a2b2ed2 | 2006-07-15 01:17:38 -0400 | [diff] [blame] | 244 | if (dev->grab == handle) { |
| 245 | dev->grab = NULL; |
| 246 | |
| 247 | list_for_each_entry(handle, &dev->h_list, d_node) |
Dmitry Torokhov | c7e8dc6 | 2006-07-06 00:21:03 -0400 | [diff] [blame] | 248 | if (handle->handler->start) |
| 249 | handle->handler->start(handle); |
| 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 252 | EXPORT_SYMBOL(input_release_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | int input_open_device(struct input_handle *handle) |
| 255 | { |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 256 | struct input_dev *dev = handle->dev; |
| 257 | int err; |
| 258 | |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 259 | err = mutex_lock_interruptible(&dev->mutex); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 260 | if (err) |
| 261 | return err; |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | handle->open++; |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 264 | |
| 265 | if (!dev->users++ && dev->open) |
| 266 | err = dev->open(dev); |
| 267 | |
| 268 | if (err) |
| 269 | handle->open--; |
| 270 | |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 271 | mutex_unlock(&dev->mutex); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 272 | |
| 273 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 275 | EXPORT_SYMBOL(input_open_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | int input_flush_device(struct input_handle* handle, struct file* file) |
| 278 | { |
| 279 | if (handle->dev->flush) |
| 280 | return handle->dev->flush(handle->dev, file); |
| 281 | |
| 282 | return 0; |
| 283 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 284 | EXPORT_SYMBOL(input_flush_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | void input_close_device(struct input_handle *handle) |
| 287 | { |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 288 | struct input_dev *dev = handle->dev; |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | input_release_device(handle); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 291 | |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 292 | mutex_lock(&dev->mutex); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 293 | |
| 294 | if (!--dev->users && dev->close) |
| 295 | dev->close(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | handle->open--; |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 297 | |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 298 | mutex_unlock(&dev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 300 | EXPORT_SYMBOL(input_close_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame^] | 302 | static int input_fetch_keycode(struct input_dev *dev, int scancode) |
| 303 | { |
| 304 | switch (dev->keycodesize) { |
| 305 | case 1: |
| 306 | return ((u8 *)dev->keycode)[scancode]; |
| 307 | |
| 308 | case 2: |
| 309 | return ((u16 *)dev->keycode)[scancode]; |
| 310 | |
| 311 | default: |
| 312 | return ((u32 *)dev->keycode)[scancode]; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static int input_default_getkeycode(struct input_dev *dev, |
| 317 | int scancode, int *keycode) |
| 318 | { |
| 319 | if (!dev->keycodesize) |
| 320 | return -EINVAL; |
| 321 | |
| 322 | if (scancode < 0 || scancode >= dev->keycodemax) |
| 323 | return -EINVAL; |
| 324 | |
| 325 | *keycode = input_fetch_keycode(dev, scancode); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int input_default_setkeycode(struct input_dev *dev, |
| 331 | int scancode, int keycode) |
| 332 | { |
| 333 | int old_keycode; |
| 334 | int i; |
| 335 | |
| 336 | if (scancode < 0 || scancode >= dev->keycodemax) |
| 337 | return -EINVAL; |
| 338 | |
| 339 | if (keycode < 0 || keycode > KEY_MAX) |
| 340 | return -EINVAL; |
| 341 | |
| 342 | if (!dev->keycodesize) |
| 343 | return -EINVAL; |
| 344 | |
| 345 | if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8))) |
| 346 | return -EINVAL; |
| 347 | |
| 348 | switch (dev->keycodesize) { |
| 349 | case 1: { |
| 350 | u8 *k = (u8 *)dev->keycode; |
| 351 | old_keycode = k[scancode]; |
| 352 | k[scancode] = keycode; |
| 353 | break; |
| 354 | } |
| 355 | case 2: { |
| 356 | u16 *k = (u16 *)dev->keycode; |
| 357 | old_keycode = k[scancode]; |
| 358 | k[scancode] = keycode; |
| 359 | break; |
| 360 | } |
| 361 | default: { |
| 362 | u32 *k = (u32 *)dev->keycode; |
| 363 | old_keycode = k[scancode]; |
| 364 | k[scancode] = keycode; |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | clear_bit(old_keycode, dev->keybit); |
| 370 | set_bit(keycode, dev->keybit); |
| 371 | |
| 372 | for (i = 0; i < dev->keycodemax; i++) { |
| 373 | if (input_fetch_keycode(dev, i) == old_keycode) { |
| 374 | set_bit(old_keycode, dev->keybit); |
| 375 | break; /* Setting the bit twice is useless, so break */ |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | static void input_link_handle(struct input_handle *handle) |
| 384 | { |
| 385 | list_add_tail(&handle->d_node, &handle->dev->h_list); |
| 386 | list_add_tail(&handle->h_node, &handle->handler->h_list); |
| 387 | } |
| 388 | |
| 389 | #define MATCH_BIT(bit, max) \ |
| 390 | for (i = 0; i < NBITS(max); i++) \ |
| 391 | if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ |
| 392 | break; \ |
| 393 | if (i != NBITS(max)) \ |
| 394 | continue; |
| 395 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 396 | static const struct input_device_id *input_match_device(const struct input_device_id *id, |
| 397 | struct input_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
| 399 | int i; |
| 400 | |
| 401 | for (; id->flags || id->driver_info; id++) { |
| 402 | |
| 403 | if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 404 | if (id->bustype != dev->id.bustype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | continue; |
| 406 | |
| 407 | if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 408 | if (id->vendor != dev->id.vendor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | continue; |
| 410 | |
| 411 | if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 412 | if (id->product != dev->id.product) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | continue; |
| 414 | |
| 415 | if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 416 | if (id->version != dev->id.version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | continue; |
| 418 | |
| 419 | MATCH_BIT(evbit, EV_MAX); |
| 420 | MATCH_BIT(keybit, KEY_MAX); |
| 421 | MATCH_BIT(relbit, REL_MAX); |
| 422 | MATCH_BIT(absbit, ABS_MAX); |
| 423 | MATCH_BIT(mscbit, MSC_MAX); |
| 424 | MATCH_BIT(ledbit, LED_MAX); |
| 425 | MATCH_BIT(sndbit, SND_MAX); |
| 426 | MATCH_BIT(ffbit, FF_MAX); |
Dmitry Torokhov | ff13f98 | 2005-09-24 02:02:29 -0500 | [diff] [blame] | 427 | MATCH_BIT(swbit, SW_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | return id; |
| 430 | } |
| 431 | |
| 432 | return NULL; |
| 433 | } |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | #ifdef CONFIG_PROC_FS |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 436 | |
| 437 | static struct proc_dir_entry *proc_bus_input_dir; |
| 438 | static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait); |
| 439 | static int input_devices_state; |
| 440 | |
| 441 | static inline void input_wakeup_procfs_readers(void) |
| 442 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | input_devices_state++; |
| 444 | wake_up(&input_devices_poll_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 447 | 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] | 448 | { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 449 | int state = input_devices_state; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 450 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 451 | poll_wait(file, &input_devices_poll_wait, wait); |
| 452 | if (state != input_devices_state) |
| 453 | return POLLIN | POLLRDNORM; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 454 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 455 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 458 | static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 460 | struct list_head *node; |
| 461 | loff_t i = 0; |
| 462 | |
| 463 | list_for_each(node, list) |
| 464 | if (i++ == *pos) |
| 465 | return node; |
| 466 | |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos) |
| 471 | { |
| 472 | if (element->next == list) |
| 473 | return NULL; |
| 474 | |
| 475 | ++(*pos); |
| 476 | return element->next; |
| 477 | } |
| 478 | |
| 479 | static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos) |
| 480 | { |
| 481 | /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ |
| 482 | |
| 483 | return list_get_nth_element(&input_dev_list, pos); |
| 484 | } |
| 485 | |
| 486 | static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 487 | { |
| 488 | return list_get_next_element(&input_dev_list, v, pos); |
| 489 | } |
| 490 | |
| 491 | static void input_devices_seq_stop(struct seq_file *seq, void *v) |
| 492 | { |
| 493 | /* release lock here */ |
| 494 | } |
| 495 | |
| 496 | static void input_seq_print_bitmap(struct seq_file *seq, const char *name, |
| 497 | unsigned long *bitmap, int max) |
| 498 | { |
| 499 | int i; |
| 500 | |
| 501 | for (i = NBITS(max) - 1; i > 0; i--) |
| 502 | if (bitmap[i]) |
| 503 | break; |
| 504 | |
| 505 | seq_printf(seq, "B: %s=", name); |
| 506 | for (; i >= 0; i--) |
| 507 | seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : ""); |
| 508 | seq_putc(seq, '\n'); |
| 509 | } |
| 510 | |
| 511 | static int input_devices_seq_show(struct seq_file *seq, void *v) |
| 512 | { |
| 513 | struct input_dev *dev = container_of(v, struct input_dev, node); |
| 514 | const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | struct input_handle *handle; |
| 516 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 517 | seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n", |
| 518 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 520 | seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); |
| 521 | seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : ""); |
| 522 | seq_printf(seq, "S: Sysfs=%s\n", path ? path : ""); |
Dmitry Torokhov | 15e03ae | 2007-03-07 23:20:17 -0500 | [diff] [blame] | 523 | seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : ""); |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 524 | seq_printf(seq, "H: Handlers="); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 526 | list_for_each_entry(handle, &dev->h_list, d_node) |
| 527 | seq_printf(seq, "%s ", handle->name); |
| 528 | seq_putc(seq, '\n'); |
Dmitry Torokhov | 051b2fe | 2005-09-15 02:01:54 -0500 | [diff] [blame] | 529 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 530 | input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX); |
| 531 | if (test_bit(EV_KEY, dev->evbit)) |
| 532 | input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX); |
| 533 | if (test_bit(EV_REL, dev->evbit)) |
| 534 | input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX); |
| 535 | if (test_bit(EV_ABS, dev->evbit)) |
| 536 | input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX); |
| 537 | if (test_bit(EV_MSC, dev->evbit)) |
| 538 | input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX); |
| 539 | if (test_bit(EV_LED, dev->evbit)) |
| 540 | input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX); |
| 541 | if (test_bit(EV_SND, dev->evbit)) |
| 542 | input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX); |
| 543 | if (test_bit(EV_FF, dev->evbit)) |
| 544 | input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX); |
| 545 | if (test_bit(EV_SW, dev->evbit)) |
| 546 | input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 548 | seq_putc(seq, '\n'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 550 | kfree(path); |
| 551 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 554 | static struct seq_operations input_devices_seq_ops = { |
| 555 | .start = input_devices_seq_start, |
| 556 | .next = input_devices_seq_next, |
| 557 | .stop = input_devices_seq_stop, |
| 558 | .show = input_devices_seq_show, |
| 559 | }; |
| 560 | |
| 561 | static int input_proc_devices_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 563 | return seq_open(file, &input_devices_seq_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 566 | static const struct file_operations input_devices_fileops = { |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 567 | .owner = THIS_MODULE, |
| 568 | .open = input_proc_devices_open, |
| 569 | .poll = input_proc_devices_poll, |
| 570 | .read = seq_read, |
| 571 | .llseek = seq_lseek, |
| 572 | .release = seq_release, |
| 573 | }; |
| 574 | |
| 575 | static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos) |
| 576 | { |
| 577 | /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ |
| 578 | seq->private = (void *)(unsigned long)*pos; |
| 579 | return list_get_nth_element(&input_handler_list, pos); |
| 580 | } |
| 581 | |
| 582 | static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 583 | { |
| 584 | seq->private = (void *)(unsigned long)(*pos + 1); |
| 585 | return list_get_next_element(&input_handler_list, v, pos); |
| 586 | } |
| 587 | |
| 588 | static void input_handlers_seq_stop(struct seq_file *seq, void *v) |
| 589 | { |
| 590 | /* release lock here */ |
| 591 | } |
| 592 | |
| 593 | static int input_handlers_seq_show(struct seq_file *seq, void *v) |
| 594 | { |
| 595 | struct input_handler *handler = container_of(v, struct input_handler, node); |
| 596 | |
| 597 | seq_printf(seq, "N: Number=%ld Name=%s", |
| 598 | (unsigned long)seq->private, handler->name); |
| 599 | if (handler->fops) |
| 600 | seq_printf(seq, " Minor=%d", handler->minor); |
| 601 | seq_putc(seq, '\n'); |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | static struct seq_operations input_handlers_seq_ops = { |
| 606 | .start = input_handlers_seq_start, |
| 607 | .next = input_handlers_seq_next, |
| 608 | .stop = input_handlers_seq_stop, |
| 609 | .show = input_handlers_seq_show, |
| 610 | }; |
| 611 | |
| 612 | static int input_proc_handlers_open(struct inode *inode, struct file *file) |
| 613 | { |
| 614 | return seq_open(file, &input_handlers_seq_ops); |
| 615 | } |
| 616 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 617 | static const struct file_operations input_handlers_fileops = { |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 618 | .owner = THIS_MODULE, |
| 619 | .open = input_proc_handlers_open, |
| 620 | .read = seq_read, |
| 621 | .llseek = seq_lseek, |
| 622 | .release = seq_release, |
| 623 | }; |
Luke Kosewski | e334016fc | 2005-06-01 02:39:28 -0500 | [diff] [blame] | 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | static int __init input_proc_init(void) |
| 626 | { |
| 627 | struct proc_dir_entry *entry; |
| 628 | |
| 629 | proc_bus_input_dir = proc_mkdir("input", proc_bus); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 630 | if (!proc_bus_input_dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | return -ENOMEM; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | proc_bus_input_dir->owner = THIS_MODULE; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 634 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 635 | entry = create_proc_entry("devices", 0, proc_bus_input_dir); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 636 | if (!entry) |
| 637 | goto fail1; |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | entry->owner = THIS_MODULE; |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 640 | entry->proc_fops = &input_devices_fileops; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 641 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 642 | entry = create_proc_entry("handlers", 0, proc_bus_input_dir); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 643 | if (!entry) |
| 644 | goto fail2; |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | entry->owner = THIS_MODULE; |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 647 | entry->proc_fops = &input_handlers_fileops; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | return 0; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 650 | |
| 651 | fail2: remove_proc_entry("devices", proc_bus_input_dir); |
| 652 | fail1: remove_proc_entry("input", proc_bus); |
| 653 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 655 | |
Andrew Morton | beffbdc | 2005-07-01 23:54:30 -0500 | [diff] [blame] | 656 | static void input_proc_exit(void) |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 657 | { |
| 658 | remove_proc_entry("devices", proc_bus_input_dir); |
| 659 | remove_proc_entry("handlers", proc_bus_input_dir); |
| 660 | remove_proc_entry("input", proc_bus); |
| 661 | } |
| 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | #else /* !CONFIG_PROC_FS */ |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 664 | static inline void input_wakeup_procfs_readers(void) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | static inline int input_proc_init(void) { return 0; } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 666 | static inline void input_proc_exit(void) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | #endif |
| 668 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 669 | #define INPUT_DEV_STRING_ATTR_SHOW(name) \ |
| 670 | static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \ |
| 671 | { \ |
| 672 | struct input_dev *input_dev = to_input_dev(dev); \ |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 673 | \ |
Dmitry Torokhov | 1efa770 | 2007-02-18 01:40:37 -0500 | [diff] [blame] | 674 | return scnprintf(buf, PAGE_SIZE, "%s\n", \ |
| 675 | input_dev->name ? input_dev->name : ""); \ |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 676 | } \ |
| 677 | static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL); |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 678 | |
| 679 | INPUT_DEV_STRING_ATTR_SHOW(name); |
| 680 | INPUT_DEV_STRING_ATTR_SHOW(phys); |
| 681 | INPUT_DEV_STRING_ATTR_SHOW(uniq); |
| 682 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 683 | static int input_print_modalias_bits(char *buf, int size, |
| 684 | char name, unsigned long *bm, |
| 685 | unsigned int min_bit, unsigned int max_bit) |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 686 | { |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 687 | int len = 0, i; |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 688 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 689 | len += snprintf(buf, max(size, 0), "%c", name); |
| 690 | for (i = min_bit; i < max_bit; i++) |
| 691 | if (bm[LONG(i)] & BIT(i)) |
| 692 | len += snprintf(buf + len, max(size - len, 0), "%X,", i); |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 693 | return len; |
| 694 | } |
| 695 | |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 696 | static int input_print_modalias(char *buf, int size, struct input_dev *id, |
| 697 | int add_cr) |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 698 | { |
| 699 | int len; |
| 700 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 701 | len = snprintf(buf, max(size, 0), |
| 702 | "input:b%04Xv%04Xp%04Xe%04X-", |
| 703 | id->id.bustype, id->id.vendor, |
| 704 | id->id.product, id->id.version); |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 705 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 706 | len += input_print_modalias_bits(buf + len, size - len, |
| 707 | 'e', id->evbit, 0, EV_MAX); |
| 708 | len += input_print_modalias_bits(buf + len, size - len, |
| 709 | 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX); |
| 710 | len += input_print_modalias_bits(buf + len, size - len, |
| 711 | 'r', id->relbit, 0, REL_MAX); |
| 712 | len += input_print_modalias_bits(buf + len, size - len, |
| 713 | 'a', id->absbit, 0, ABS_MAX); |
| 714 | len += input_print_modalias_bits(buf + len, size - len, |
| 715 | 'm', id->mscbit, 0, MSC_MAX); |
| 716 | len += input_print_modalias_bits(buf + len, size - len, |
| 717 | 'l', id->ledbit, 0, LED_MAX); |
| 718 | len += input_print_modalias_bits(buf + len, size - len, |
| 719 | 's', id->sndbit, 0, SND_MAX); |
| 720 | len += input_print_modalias_bits(buf + len, size - len, |
| 721 | 'f', id->ffbit, 0, FF_MAX); |
| 722 | len += input_print_modalias_bits(buf + len, size - len, |
| 723 | 'w', id->swbit, 0, SW_MAX); |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 724 | |
| 725 | if (add_cr) |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 726 | len += snprintf(buf + len, max(size - len, 0), "\n"); |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 727 | |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 728 | return len; |
| 729 | } |
| 730 | |
| 731 | static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf) |
| 732 | { |
| 733 | struct input_dev *id = to_input_dev(dev); |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 734 | ssize_t len; |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 735 | |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 736 | len = input_print_modalias(buf, PAGE_SIZE, id, 1); |
| 737 | |
Richard Purdie | 8a3cf45 | 2006-06-26 01:48:21 -0400 | [diff] [blame] | 738 | return min_t(int, len, PAGE_SIZE); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 739 | } |
| 740 | static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL); |
| 741 | |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 742 | static struct attribute *input_dev_attrs[] = { |
| 743 | &class_device_attr_name.attr, |
| 744 | &class_device_attr_phys.attr, |
| 745 | &class_device_attr_uniq.attr, |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 746 | &class_device_attr_modalias.attr, |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 747 | NULL |
| 748 | }; |
| 749 | |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 750 | static struct attribute_group input_dev_attr_group = { |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 751 | .attrs = input_dev_attrs, |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 752 | }; |
| 753 | |
| 754 | #define INPUT_DEV_ID_ATTR(name) \ |
| 755 | static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \ |
| 756 | { \ |
| 757 | struct input_dev *input_dev = to_input_dev(dev); \ |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 758 | return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \ |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 759 | } \ |
| 760 | static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL); |
| 761 | |
| 762 | INPUT_DEV_ID_ATTR(bustype); |
| 763 | INPUT_DEV_ID_ATTR(vendor); |
| 764 | INPUT_DEV_ID_ATTR(product); |
| 765 | INPUT_DEV_ID_ATTR(version); |
| 766 | |
| 767 | static struct attribute *input_dev_id_attrs[] = { |
| 768 | &class_device_attr_bustype.attr, |
| 769 | &class_device_attr_vendor.attr, |
| 770 | &class_device_attr_product.attr, |
| 771 | &class_device_attr_version.attr, |
| 772 | NULL |
| 773 | }; |
| 774 | |
| 775 | static struct attribute_group input_dev_id_attr_group = { |
| 776 | .name = "id", |
| 777 | .attrs = input_dev_id_attrs, |
| 778 | }; |
| 779 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 780 | static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap, |
| 781 | int max, int add_cr) |
| 782 | { |
| 783 | int i; |
| 784 | int len = 0; |
| 785 | |
| 786 | for (i = NBITS(max) - 1; i > 0; i--) |
| 787 | if (bitmap[i]) |
| 788 | break; |
| 789 | |
| 790 | for (; i >= 0; i--) |
| 791 | len += snprintf(buf + len, max(buf_size - len, 0), |
| 792 | "%lx%s", bitmap[i], i > 0 ? " " : ""); |
| 793 | |
| 794 | if (add_cr) |
| 795 | len += snprintf(buf + len, max(buf_size - len, 0), "\n"); |
| 796 | |
| 797 | return len; |
| 798 | } |
| 799 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 800 | #define INPUT_DEV_CAP_ATTR(ev, bm) \ |
| 801 | static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \ |
| 802 | { \ |
| 803 | struct input_dev *input_dev = to_input_dev(dev); \ |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 804 | int len = input_print_bitmap(buf, PAGE_SIZE, \ |
| 805 | input_dev->bm##bit, ev##_MAX, 1); \ |
| 806 | return min_t(int, len, PAGE_SIZE); \ |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 807 | } \ |
| 808 | static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL); |
| 809 | |
| 810 | INPUT_DEV_CAP_ATTR(EV, ev); |
| 811 | INPUT_DEV_CAP_ATTR(KEY, key); |
| 812 | INPUT_DEV_CAP_ATTR(REL, rel); |
| 813 | INPUT_DEV_CAP_ATTR(ABS, abs); |
| 814 | INPUT_DEV_CAP_ATTR(MSC, msc); |
| 815 | INPUT_DEV_CAP_ATTR(LED, led); |
| 816 | INPUT_DEV_CAP_ATTR(SND, snd); |
| 817 | INPUT_DEV_CAP_ATTR(FF, ff); |
| 818 | INPUT_DEV_CAP_ATTR(SW, sw); |
| 819 | |
| 820 | static struct attribute *input_dev_caps_attrs[] = { |
| 821 | &class_device_attr_ev.attr, |
| 822 | &class_device_attr_key.attr, |
| 823 | &class_device_attr_rel.attr, |
| 824 | &class_device_attr_abs.attr, |
| 825 | &class_device_attr_msc.attr, |
| 826 | &class_device_attr_led.attr, |
| 827 | &class_device_attr_snd.attr, |
| 828 | &class_device_attr_ff.attr, |
| 829 | &class_device_attr_sw.attr, |
| 830 | NULL |
| 831 | }; |
| 832 | |
| 833 | static struct attribute_group input_dev_caps_attr_group = { |
| 834 | .name = "capabilities", |
| 835 | .attrs = input_dev_caps_attrs, |
| 836 | }; |
| 837 | |
Dmitry Torokhov | cb9def4 | 2007-03-07 23:20:26 -0500 | [diff] [blame] | 838 | static struct attribute_group *input_dev_attr_groups[] = { |
| 839 | &input_dev_attr_group, |
| 840 | &input_dev_id_attr_group, |
| 841 | &input_dev_caps_attr_group, |
| 842 | NULL |
| 843 | }; |
| 844 | |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 845 | static void input_dev_release(struct class_device *class_dev) |
| 846 | { |
| 847 | struct input_dev *dev = to_input_dev(class_dev); |
| 848 | |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 849 | input_ff_destroy(dev); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 850 | kfree(dev); |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 851 | |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 852 | module_put(THIS_MODULE); |
| 853 | } |
| 854 | |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 855 | /* |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 856 | * Input uevent interface - loading event handlers based on |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 857 | * device bitfields. |
| 858 | */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 859 | static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index, |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 860 | char *buffer, int buffer_size, int *cur_len, |
| 861 | const char *name, unsigned long *bitmap, int max) |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 862 | { |
| 863 | if (*cur_index >= num_envp - 1) |
| 864 | return -ENOMEM; |
| 865 | |
| 866 | envp[*cur_index] = buffer + *cur_len; |
| 867 | |
| 868 | *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name); |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 869 | if (*cur_len >= buffer_size) |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 870 | return -ENOMEM; |
| 871 | |
| 872 | *cur_len += input_print_bitmap(buffer + *cur_len, |
| 873 | max(buffer_size - *cur_len, 0), |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 874 | bitmap, max, 0) + 1; |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 875 | if (*cur_len > buffer_size) |
| 876 | return -ENOMEM; |
| 877 | |
| 878 | (*cur_index)++; |
| 879 | return 0; |
| 880 | } |
| 881 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 882 | static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index, |
| 883 | char *buffer, int buffer_size, int *cur_len, |
| 884 | struct input_dev *dev) |
| 885 | { |
| 886 | if (*cur_index >= num_envp - 1) |
| 887 | return -ENOMEM; |
| 888 | |
| 889 | envp[*cur_index] = buffer + *cur_len; |
| 890 | |
| 891 | *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), |
| 892 | "MODALIAS="); |
| 893 | if (*cur_len >= buffer_size) |
| 894 | return -ENOMEM; |
| 895 | |
| 896 | *cur_len += input_print_modalias(buffer + *cur_len, |
| 897 | max(buffer_size - *cur_len, 0), |
| 898 | dev, 0) + 1; |
| 899 | if (*cur_len > buffer_size) |
| 900 | return -ENOMEM; |
| 901 | |
| 902 | (*cur_index)++; |
| 903 | return 0; |
| 904 | } |
| 905 | |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 906 | #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \ |
| 907 | do { \ |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 908 | int err = add_uevent_var(envp, num_envp, &i, \ |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 909 | buffer, buffer_size, &len, \ |
| 910 | fmt, val); \ |
| 911 | if (err) \ |
| 912 | return err; \ |
| 913 | } while (0) |
| 914 | |
| 915 | #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \ |
| 916 | do { \ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 917 | int err = input_add_uevent_bm_var(envp, num_envp, &i, \ |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 918 | buffer, buffer_size, &len, \ |
| 919 | name, bm, max); \ |
| 920 | if (err) \ |
| 921 | return err; \ |
| 922 | } while (0) |
| 923 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 924 | #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \ |
| 925 | do { \ |
| 926 | int err = input_add_uevent_modalias_var(envp, \ |
| 927 | num_envp, &i, \ |
| 928 | buffer, buffer_size, &len, \ |
| 929 | dev); \ |
| 930 | if (err) \ |
| 931 | return err; \ |
| 932 | } while (0) |
| 933 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 934 | static int input_dev_uevent(struct class_device *cdev, char **envp, |
| 935 | int num_envp, char *buffer, int buffer_size) |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 936 | { |
| 937 | struct input_dev *dev = to_input_dev(cdev); |
| 938 | int i = 0; |
| 939 | int len = 0; |
| 940 | |
| 941 | INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x", |
| 942 | dev->id.bustype, dev->id.vendor, |
| 943 | dev->id.product, dev->id.version); |
| 944 | if (dev->name) |
| 945 | INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name); |
| 946 | if (dev->phys) |
| 947 | INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys); |
Dmitry Torokhov | 08de1f0 | 2005-11-08 21:34:29 -0800 | [diff] [blame] | 948 | if (dev->uniq) |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 949 | INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq); |
| 950 | |
| 951 | INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX); |
| 952 | if (test_bit(EV_KEY, dev->evbit)) |
| 953 | INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX); |
| 954 | if (test_bit(EV_REL, dev->evbit)) |
| 955 | INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX); |
| 956 | if (test_bit(EV_ABS, dev->evbit)) |
| 957 | INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX); |
| 958 | if (test_bit(EV_MSC, dev->evbit)) |
| 959 | INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX); |
| 960 | if (test_bit(EV_LED, dev->evbit)) |
| 961 | INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX); |
| 962 | if (test_bit(EV_SND, dev->evbit)) |
| 963 | INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX); |
| 964 | if (test_bit(EV_FF, dev->evbit)) |
| 965 | INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX); |
| 966 | if (test_bit(EV_SW, dev->evbit)) |
| 967 | INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX); |
| 968 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 969 | INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev); |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 970 | |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 971 | envp[i] = NULL; |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 972 | return 0; |
| 973 | } |
| 974 | |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 975 | struct class input_class = { |
| 976 | .name = "input", |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 977 | .release = input_dev_release, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 978 | .uevent = input_dev_uevent, |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 979 | }; |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 980 | EXPORT_SYMBOL_GPL(input_class); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 981 | |
Dmitry Torokhov | 1447190 | 2006-11-02 23:26:55 -0500 | [diff] [blame] | 982 | /** |
| 983 | * input_allocate_device - allocate memory for new input device |
| 984 | * |
| 985 | * Returns prepared struct input_dev or NULL. |
| 986 | * |
| 987 | * NOTE: Use input_free_device() to free devices that have not been |
| 988 | * registered; input_unregister_device() should be used for already |
| 989 | * registered devices. |
| 990 | */ |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 991 | struct input_dev *input_allocate_device(void) |
| 992 | { |
| 993 | struct input_dev *dev; |
| 994 | |
| 995 | dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL); |
| 996 | if (dev) { |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 997 | dev->cdev.class = &input_class; |
Dmitry Torokhov | cb9def4 | 2007-03-07 23:20:26 -0500 | [diff] [blame] | 998 | dev->cdev.groups = input_dev_attr_groups; |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 999 | class_device_initialize(&dev->cdev); |
Dmitry Torokhov | f60d2b1 | 2006-06-26 01:48:36 -0400 | [diff] [blame] | 1000 | mutex_init(&dev->mutex); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 1001 | INIT_LIST_HEAD(&dev->h_list); |
| 1002 | INIT_LIST_HEAD(&dev->node); |
Dmitry Torokhov | 655816e | 2006-09-14 01:32:14 -0400 | [diff] [blame] | 1003 | |
| 1004 | __module_get(THIS_MODULE); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | return dev; |
| 1008 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1009 | EXPORT_SYMBOL(input_allocate_device); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 1010 | |
Dmitry Torokhov | 1447190 | 2006-11-02 23:26:55 -0500 | [diff] [blame] | 1011 | /** |
| 1012 | * input_free_device - free memory occupied by input_dev structure |
| 1013 | * @dev: input device to free |
| 1014 | * |
| 1015 | * This function should only be used if input_register_device() |
| 1016 | * was not called yet or if it failed. Once device was registered |
| 1017 | * use input_unregister_device() and memory will be freed once last |
| 1018 | * refrence to the device is dropped. |
| 1019 | * |
| 1020 | * Device should be allocated by input_allocate_device(). |
| 1021 | * |
| 1022 | * NOTE: If there are references to the input device then memory |
| 1023 | * will not be freed until last reference is dropped. |
| 1024 | */ |
Dmitry Torokhov | f60d2b1 | 2006-06-26 01:48:36 -0400 | [diff] [blame] | 1025 | void input_free_device(struct input_dev *dev) |
| 1026 | { |
| 1027 | if (dev) { |
| 1028 | |
| 1029 | mutex_lock(&dev->mutex); |
| 1030 | dev->name = dev->phys = dev->uniq = NULL; |
| 1031 | mutex_unlock(&dev->mutex); |
| 1032 | |
| 1033 | input_put_device(dev); |
| 1034 | } |
| 1035 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1036 | EXPORT_SYMBOL(input_free_device); |
Dmitry Torokhov | f60d2b1 | 2006-06-26 01:48:36 -0400 | [diff] [blame] | 1037 | |
Dmitry Torokhov | 5f94548 | 2005-11-02 22:51:46 -0500 | [diff] [blame] | 1038 | int input_register_device(struct input_dev *dev) |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1039 | { |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 1040 | static atomic_t input_no = ATOMIC_INIT(0); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1041 | struct input_handle *handle; |
| 1042 | struct input_handler *handler; |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 1043 | const struct input_device_id *id; |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 1044 | const char *path; |
| 1045 | int error; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1046 | |
Dmitry Torokhov | 5f94548 | 2005-11-02 22:51:46 -0500 | [diff] [blame] | 1047 | set_bit(EV_SYN, dev->evbit); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1048 | |
| 1049 | /* |
| 1050 | * If delay and period are pre-set by the driver, then autorepeating |
| 1051 | * is handled by the driver itself and we don't do it in input.c. |
| 1052 | */ |
| 1053 | |
| 1054 | init_timer(&dev->timer); |
| 1055 | if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) { |
| 1056 | dev->timer.data = (long) dev; |
| 1057 | dev->timer.function = input_repeat_key; |
| 1058 | dev->rep[REP_DELAY] = 250; |
| 1059 | dev->rep[REP_PERIOD] = 33; |
| 1060 | } |
| 1061 | |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame^] | 1062 | if (!dev->getkeycode) |
| 1063 | dev->getkeycode = input_default_getkeycode; |
| 1064 | |
| 1065 | if (!dev->setkeycode) |
| 1066 | dev->setkeycode = input_default_setkeycode; |
| 1067 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1068 | list_add_tail(&dev->node, &input_dev_list); |
| 1069 | |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 1070 | snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id), |
| 1071 | "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1); |
| 1072 | |
| 1073 | error = class_device_add(&dev->cdev); |
| 1074 | if (error) |
| 1075 | return error; |
| 1076 | |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 1077 | path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL); |
| 1078 | printk(KERN_INFO "input: %s as %s\n", |
| 1079 | dev->name ? dev->name : "Unspecified device", path ? path : "N/A"); |
| 1080 | kfree(path); |
Greg Kroah-Hartman | 1020402 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1081 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1082 | list_for_each_entry(handler, &input_handler_list, node) |
| 1083 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) |
| 1084 | if ((id = input_match_device(handler->id_table, dev))) |
Dmitry Torokhov | c7e8dc6 | 2006-07-06 00:21:03 -0400 | [diff] [blame] | 1085 | if ((handle = handler->connect(handler, dev, id))) { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1086 | input_link_handle(handle); |
Dmitry Torokhov | c7e8dc6 | 2006-07-06 00:21:03 -0400 | [diff] [blame] | 1087 | if (handler->start) |
| 1088 | handler->start(handle); |
| 1089 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1090 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1091 | input_wakeup_procfs_readers(); |
Dmitry Torokhov | 5f94548 | 2005-11-02 22:51:46 -0500 | [diff] [blame] | 1092 | |
| 1093 | return 0; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1094 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1095 | EXPORT_SYMBOL(input_register_device); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1096 | |
| 1097 | void input_unregister_device(struct input_dev *dev) |
| 1098 | { |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 1099 | struct list_head *node, *next; |
Dmitry Torokhov | 6d2750c | 2006-09-10 21:56:06 -0400 | [diff] [blame] | 1100 | int code; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1101 | |
Dmitry Torokhov | 6d2750c | 2006-09-10 21:56:06 -0400 | [diff] [blame] | 1102 | for (code = 0; code <= KEY_MAX; code++) |
| 1103 | if (test_bit(code, dev->key)) |
| 1104 | input_report_key(dev, code, 0); |
| 1105 | input_sync(dev); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1106 | |
| 1107 | del_timer_sync(&dev->timer); |
| 1108 | |
| 1109 | list_for_each_safe(node, next, &dev->h_list) { |
| 1110 | struct input_handle * handle = to_handle(node); |
| 1111 | list_del_init(&handle->d_node); |
| 1112 | list_del_init(&handle->h_node); |
| 1113 | handle->handler->disconnect(handle); |
| 1114 | } |
| 1115 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1116 | list_del_init(&dev->node); |
| 1117 | |
Dmitry Torokhov | e7374e4 | 2006-06-27 08:30:31 -0400 | [diff] [blame] | 1118 | class_device_unregister(&dev->cdev); |
| 1119 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1120 | input_wakeup_procfs_readers(); |
| 1121 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1122 | EXPORT_SYMBOL(input_unregister_device); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1123 | |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 1124 | int input_register_handler(struct input_handler *handler) |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1125 | { |
| 1126 | struct input_dev *dev; |
| 1127 | struct input_handle *handle; |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 1128 | const struct input_device_id *id; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1129 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1130 | INIT_LIST_HEAD(&handler->h_list); |
| 1131 | |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 1132 | if (handler->fops != NULL) { |
| 1133 | if (input_table[handler->minor >> 5]) |
| 1134 | return -EBUSY; |
| 1135 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1136 | input_table[handler->minor >> 5] = handler; |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 1137 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1138 | |
| 1139 | list_add_tail(&handler->node, &input_handler_list); |
| 1140 | |
| 1141 | list_for_each_entry(dev, &input_dev_list, node) |
| 1142 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) |
| 1143 | if ((id = input_match_device(handler->id_table, dev))) |
Dmitry Torokhov | b6d786d | 2006-07-19 01:08:51 -0400 | [diff] [blame] | 1144 | if ((handle = handler->connect(handler, dev, id))) { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1145 | input_link_handle(handle); |
Dmitry Torokhov | b6d786d | 2006-07-19 01:08:51 -0400 | [diff] [blame] | 1146 | if (handler->start) |
| 1147 | handler->start(handle); |
| 1148 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1149 | |
| 1150 | input_wakeup_procfs_readers(); |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 1151 | return 0; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1152 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1153 | EXPORT_SYMBOL(input_register_handler); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1154 | |
| 1155 | void input_unregister_handler(struct input_handler *handler) |
| 1156 | { |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 1157 | struct list_head *node, *next; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1158 | |
| 1159 | list_for_each_safe(node, next, &handler->h_list) { |
| 1160 | struct input_handle * handle = to_handle_h(node); |
| 1161 | list_del_init(&handle->h_node); |
| 1162 | list_del_init(&handle->d_node); |
| 1163 | handler->disconnect(handle); |
| 1164 | } |
| 1165 | |
| 1166 | list_del_init(&handler->node); |
| 1167 | |
| 1168 | if (handler->fops != NULL) |
| 1169 | input_table[handler->minor >> 5] = NULL; |
| 1170 | |
| 1171 | input_wakeup_procfs_readers(); |
| 1172 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1173 | EXPORT_SYMBOL(input_unregister_handler); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1174 | |
| 1175 | static int input_open_file(struct inode *inode, struct file *file) |
| 1176 | { |
| 1177 | struct input_handler *handler = input_table[iminor(inode) >> 5]; |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 1178 | const struct file_operations *old_fops, *new_fops = NULL; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1179 | int err; |
| 1180 | |
| 1181 | /* No load-on-demand here? */ |
| 1182 | if (!handler || !(new_fops = fops_get(handler->fops))) |
| 1183 | return -ENODEV; |
| 1184 | |
| 1185 | /* |
| 1186 | * That's _really_ odd. Usually NULL ->open means "nothing special", |
| 1187 | * not "no device". Oh, well... |
| 1188 | */ |
| 1189 | if (!new_fops->open) { |
| 1190 | fops_put(new_fops); |
| 1191 | return -ENODEV; |
| 1192 | } |
| 1193 | old_fops = file->f_op; |
| 1194 | file->f_op = new_fops; |
| 1195 | |
| 1196 | err = new_fops->open(inode, file); |
| 1197 | |
| 1198 | if (err) { |
| 1199 | fops_put(file->f_op); |
| 1200 | file->f_op = fops_get(old_fops); |
| 1201 | } |
| 1202 | fops_put(old_fops); |
| 1203 | return err; |
| 1204 | } |
| 1205 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 1206 | static const struct file_operations input_fops = { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1207 | .owner = THIS_MODULE, |
| 1208 | .open = input_open_file, |
| 1209 | }; |
| 1210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | static int __init input_init(void) |
| 1212 | { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1213 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1215 | err = class_register(&input_class); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 1216 | if (err) { |
| 1217 | printk(KERN_ERR "input: unable to register input_dev class\n"); |
| 1218 | return err; |
| 1219 | } |
| 1220 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1221 | err = input_proc_init(); |
| 1222 | if (err) |
Greg Kroah-Hartman | b0fdfeb | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1223 | goto fail1; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1224 | |
| 1225 | err = register_chrdev(INPUT_MAJOR, "input", &input_fops); |
| 1226 | if (err) { |
| 1227 | 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] | 1228 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1230 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1231 | return 0; |
| 1232 | |
Greg Kroah-Hartman | b0fdfeb | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1233 | fail2: input_proc_exit(); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1234 | fail1: class_unregister(&input_class); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1235 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | static void __exit input_exit(void) |
| 1239 | { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1240 | input_proc_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | unregister_chrdev(INPUT_MAJOR, "input"); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1242 | class_unregister(&input_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | subsys_initcall(input_init); |
| 1246 | module_exit(input_exit); |