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