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