blob: d3cdb139e96213d7ea38de3b2c793e4ac8aa8e49 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torokhov969b21c2006-04-02 00:09:34 -050021#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/interrupt.h>
23#include <linux/poll.h>
24#include <linux/device.h>
Jes Sorensene676c232006-02-19 00:21:46 -050025#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28MODULE_DESCRIPTION("Input core");
29MODULE_LICENSE("GPL");
30
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -050031EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -040032EXPORT_SYMBOL(input_free_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033EXPORT_SYMBOL(input_register_device);
34EXPORT_SYMBOL(input_unregister_device);
35EXPORT_SYMBOL(input_register_handler);
36EXPORT_SYMBOL(input_unregister_handler);
37EXPORT_SYMBOL(input_grab_device);
38EXPORT_SYMBOL(input_release_device);
39EXPORT_SYMBOL(input_open_device);
40EXPORT_SYMBOL(input_close_device);
41EXPORT_SYMBOL(input_accept_process);
42EXPORT_SYMBOL(input_flush_device);
43EXPORT_SYMBOL(input_event);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -070044EXPORT_SYMBOL_GPL(input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define INPUT_DEVICES 256
47
48static LIST_HEAD(input_dev_list);
49static LIST_HEAD(input_handler_list);
50
51static struct input_handler *input_table[8];
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
54{
55 struct input_handle *handle;
56
57 if (type > EV_MAX || !test_bit(type, dev->evbit))
58 return;
59
60 add_input_randomness(type, code, value);
61
62 switch (type) {
63
64 case EV_SYN:
65 switch (code) {
66 case SYN_CONFIG:
67 if (dev->event) dev->event(dev, type, code, value);
68 break;
69
70 case SYN_REPORT:
71 if (dev->sync) return;
72 dev->sync = 1;
73 break;
74 }
75 break;
76
77 case EV_KEY:
78
79 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
80 return;
81
82 if (value == 2)
83 break;
84
85 change_bit(code, dev->key);
86
87 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
88 dev->repeat_key = code;
89 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
90 }
91
92 break;
93
Richard Purdie31581062005-09-06 15:19:06 -070094 case EV_SW:
95
96 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
97 return;
98
99 change_bit(code, dev->sw);
100
101 break;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 case EV_ABS:
104
105 if (code > ABS_MAX || !test_bit(code, dev->absbit))
106 return;
107
108 if (dev->absfuzz[code]) {
109 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
110 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
111 return;
112
113 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
114 (value < dev->abs[code] + dev->absfuzz[code]))
115 value = (dev->abs[code] * 3 + value) >> 2;
116
117 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
118 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
119 value = (dev->abs[code] + value) >> 1;
120 }
121
122 if (dev->abs[code] == value)
123 return;
124
125 dev->abs[code] = value;
126 break;
127
128 case EV_REL:
129
130 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
131 return;
132
133 break;
134
135 case EV_MSC:
136
137 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
138 return;
139
140 if (dev->event) dev->event(dev, type, code, value);
141
142 break;
143
144 case EV_LED:
145
146 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
147 return;
148
149 change_bit(code, dev->led);
150 if (dev->event) dev->event(dev, type, code, value);
151
152 break;
153
154 case EV_SND:
155
156 if (code > SND_MAX || !test_bit(code, dev->sndbit))
157 return;
158
Dmitry Torokhov8fdc1942006-04-29 01:13:48 -0400159 if (!!test_bit(code, dev->snd) != !!value)
160 change_bit(code, dev->snd);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (dev->event) dev->event(dev, type, code, value);
163
164 break;
165
166 case EV_REP:
167
168 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
169
170 dev->rep[code] = value;
171 if (dev->event) dev->event(dev, type, code, value);
172
173 break;
174
175 case EV_FF:
176 if (dev->event) dev->event(dev, type, code, value);
177 break;
178 }
179
180 if (type != EV_SYN)
181 dev->sync = 0;
182
183 if (dev->grab)
184 dev->grab->handler->event(dev->grab, type, code, value);
185 else
186 list_for_each_entry(handle, &dev->h_list, d_node)
187 if (handle->open)
188 handle->handler->event(handle, type, code, value);
189}
190
191static void input_repeat_key(unsigned long data)
192{
193 struct input_dev *dev = (void *) data;
194
195 if (!test_bit(dev->repeat_key, dev->key))
196 return;
197
198 input_event(dev, EV_KEY, dev->repeat_key, 2);
199 input_sync(dev);
200
201 if (dev->rep[REP_PERIOD])
202 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
203}
204
205int input_accept_process(struct input_handle *handle, struct file *file)
206{
207 if (handle->dev->accept)
208 return handle->dev->accept(handle->dev, file);
209
210 return 0;
211}
212
213int input_grab_device(struct input_handle *handle)
214{
215 if (handle->dev->grab)
216 return -EBUSY;
217
218 handle->dev->grab = handle;
219 return 0;
220}
221
222void input_release_device(struct input_handle *handle)
223{
224 if (handle->dev->grab == handle)
225 handle->dev->grab = NULL;
226}
227
228int input_open_device(struct input_handle *handle)
229{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500230 struct input_dev *dev = handle->dev;
231 int err;
232
Jes Sorensene676c232006-02-19 00:21:46 -0500233 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500234 if (err)
235 return err;
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500238
239 if (!dev->users++ && dev->open)
240 err = dev->open(dev);
241
242 if (err)
243 handle->open--;
244
Jes Sorensene676c232006-02-19 00:21:46 -0500245 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500246
247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250int input_flush_device(struct input_handle* handle, struct file* file)
251{
252 if (handle->dev->flush)
253 return handle->dev->flush(handle->dev, file);
254
255 return 0;
256}
257
258void input_close_device(struct input_handle *handle)
259{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500260 struct input_dev *dev = handle->dev;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500263
Jes Sorensene676c232006-02-19 00:21:46 -0500264 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500265
266 if (!--dev->users && dev->close)
267 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500269
Jes Sorensene676c232006-02-19 00:21:46 -0500270 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273static void input_link_handle(struct input_handle *handle)
274{
275 list_add_tail(&handle->d_node, &handle->dev->h_list);
276 list_add_tail(&handle->h_node, &handle->handler->h_list);
277}
278
279#define MATCH_BIT(bit, max) \
280 for (i = 0; i < NBITS(max); i++) \
281 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
282 break; \
283 if (i != NBITS(max)) \
284 continue;
285
286static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
287{
288 int i;
289
290 for (; id->flags || id->driver_info; id++) {
291
292 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400293 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 continue;
295
296 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400297 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 continue;
299
300 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400301 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 continue;
303
304 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400305 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 continue;
307
308 MATCH_BIT(evbit, EV_MAX);
309 MATCH_BIT(keybit, KEY_MAX);
310 MATCH_BIT(relbit, REL_MAX);
311 MATCH_BIT(absbit, ABS_MAX);
312 MATCH_BIT(mscbit, MSC_MAX);
313 MATCH_BIT(ledbit, LED_MAX);
314 MATCH_BIT(sndbit, SND_MAX);
315 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500316 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 return id;
319 }
320
321 return NULL;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500325
326static struct proc_dir_entry *proc_bus_input_dir;
327static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
328static int input_devices_state;
329
330static inline void input_wakeup_procfs_readers(void)
331{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 input_devices_state++;
333 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500336static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500338 int state = input_devices_state;
339 poll_wait(file, &input_devices_poll_wait, wait);
340 if (state != input_devices_state)
341 return POLLIN | POLLRDNORM;
342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500345static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500347 struct list_head *node;
348 loff_t i = 0;
349
350 list_for_each(node, list)
351 if (i++ == *pos)
352 return node;
353
354 return NULL;
355}
356
357static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
358{
359 if (element->next == list)
360 return NULL;
361
362 ++(*pos);
363 return element->next;
364}
365
366static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
367{
368 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
369
370 return list_get_nth_element(&input_dev_list, pos);
371}
372
373static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
374{
375 return list_get_next_element(&input_dev_list, v, pos);
376}
377
378static void input_devices_seq_stop(struct seq_file *seq, void *v)
379{
380 /* release lock here */
381}
382
383static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
384 unsigned long *bitmap, int max)
385{
386 int i;
387
388 for (i = NBITS(max) - 1; i > 0; i--)
389 if (bitmap[i])
390 break;
391
392 seq_printf(seq, "B: %s=", name);
393 for (; i >= 0; i--)
394 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
395 seq_putc(seq, '\n');
396}
397
398static int input_devices_seq_show(struct seq_file *seq, void *v)
399{
400 struct input_dev *dev = container_of(v, struct input_dev, node);
401 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct input_handle *handle;
403
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500404 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
405 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500407 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
408 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
409 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
410 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500412 list_for_each_entry(handle, &dev->h_list, d_node)
413 seq_printf(seq, "%s ", handle->name);
414 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500415
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500416 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
417 if (test_bit(EV_KEY, dev->evbit))
418 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
419 if (test_bit(EV_REL, dev->evbit))
420 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
421 if (test_bit(EV_ABS, dev->evbit))
422 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
423 if (test_bit(EV_MSC, dev->evbit))
424 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
425 if (test_bit(EV_LED, dev->evbit))
426 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
427 if (test_bit(EV_SND, dev->evbit))
428 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
429 if (test_bit(EV_FF, dev->evbit))
430 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
431 if (test_bit(EV_SW, dev->evbit))
432 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500434 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500436 kfree(path);
437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500440static struct seq_operations input_devices_seq_ops = {
441 .start = input_devices_seq_start,
442 .next = input_devices_seq_next,
443 .stop = input_devices_seq_stop,
444 .show = input_devices_seq_show,
445};
446
447static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500449 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500452static struct file_operations input_devices_fileops = {
453 .owner = THIS_MODULE,
454 .open = input_proc_devices_open,
455 .poll = input_proc_devices_poll,
456 .read = seq_read,
457 .llseek = seq_lseek,
458 .release = seq_release,
459};
460
461static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
462{
463 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
464 seq->private = (void *)(unsigned long)*pos;
465 return list_get_nth_element(&input_handler_list, pos);
466}
467
468static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
469{
470 seq->private = (void *)(unsigned long)(*pos + 1);
471 return list_get_next_element(&input_handler_list, v, pos);
472}
473
474static void input_handlers_seq_stop(struct seq_file *seq, void *v)
475{
476 /* release lock here */
477}
478
479static int input_handlers_seq_show(struct seq_file *seq, void *v)
480{
481 struct input_handler *handler = container_of(v, struct input_handler, node);
482
483 seq_printf(seq, "N: Number=%ld Name=%s",
484 (unsigned long)seq->private, handler->name);
485 if (handler->fops)
486 seq_printf(seq, " Minor=%d", handler->minor);
487 seq_putc(seq, '\n');
488
489 return 0;
490}
491static struct seq_operations input_handlers_seq_ops = {
492 .start = input_handlers_seq_start,
493 .next = input_handlers_seq_next,
494 .stop = input_handlers_seq_stop,
495 .show = input_handlers_seq_show,
496};
497
498static int input_proc_handlers_open(struct inode *inode, struct file *file)
499{
500 return seq_open(file, &input_handlers_seq_ops);
501}
502
503static struct file_operations input_handlers_fileops = {
504 .owner = THIS_MODULE,
505 .open = input_proc_handlers_open,
506 .read = seq_read,
507 .llseek = seq_lseek,
508 .release = seq_release,
509};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static int __init input_proc_init(void)
512{
513 struct proc_dir_entry *entry;
514
515 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500516 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500520
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500521 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500522 if (!entry)
523 goto fail1;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500526 entry->proc_fops = &input_devices_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500527
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500528 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500529 if (!entry)
530 goto fail2;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500533 entry->proc_fops = &input_handlers_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500536
537 fail2: remove_proc_entry("devices", proc_bus_input_dir);
538 fail1: remove_proc_entry("input", proc_bus);
539 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500541
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500542static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500543{
544 remove_proc_entry("devices", proc_bus_input_dir);
545 remove_proc_entry("handlers", proc_bus_input_dir);
546 remove_proc_entry("input", proc_bus);
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500550static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500552static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#endif
554
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500555#define INPUT_DEV_STRING_ATTR_SHOW(name) \
556static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
557{ \
558 struct input_dev *input_dev = to_input_dev(dev); \
559 int retval; \
560 \
Jes Sorensene676c232006-02-19 00:21:46 -0500561 retval = mutex_lock_interruptible(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500562 if (retval) \
563 return retval; \
564 \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500565 retval = scnprintf(buf, PAGE_SIZE, \
566 "%s\n", input_dev->name ? input_dev->name : ""); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500567 \
Jes Sorensene676c232006-02-19 00:21:46 -0500568 mutex_unlock(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500569 \
570 return retval; \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700571} \
572static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500573
574INPUT_DEV_STRING_ATTR_SHOW(name);
575INPUT_DEV_STRING_ATTR_SHOW(phys);
576INPUT_DEV_STRING_ATTR_SHOW(uniq);
577
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500578static int input_print_modalias_bits(char *buf, int size,
579 char name, unsigned long *bm,
580 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100581{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500582 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100583
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500584 len += snprintf(buf, max(size, 0), "%c", name);
585 for (i = min_bit; i < max_bit; i++)
586 if (bm[LONG(i)] & BIT(i))
587 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100588 return len;
589}
590
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500591static int input_print_modalias(char *buf, int size, struct input_dev *id,
592 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100593{
594 int len;
595
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500596 len = snprintf(buf, max(size, 0),
597 "input:b%04Xv%04Xp%04Xe%04X-",
598 id->id.bustype, id->id.vendor,
599 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100600
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500601 len += input_print_modalias_bits(buf + len, size - len,
602 'e', id->evbit, 0, EV_MAX);
603 len += input_print_modalias_bits(buf + len, size - len,
604 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
605 len += input_print_modalias_bits(buf + len, size - len,
606 'r', id->relbit, 0, REL_MAX);
607 len += input_print_modalias_bits(buf + len, size - len,
608 'a', id->absbit, 0, ABS_MAX);
609 len += input_print_modalias_bits(buf + len, size - len,
610 'm', id->mscbit, 0, MSC_MAX);
611 len += input_print_modalias_bits(buf + len, size - len,
612 'l', id->ledbit, 0, LED_MAX);
613 len += input_print_modalias_bits(buf + len, size - len,
614 's', id->sndbit, 0, SND_MAX);
615 len += input_print_modalias_bits(buf + len, size - len,
616 'f', id->ffbit, 0, FF_MAX);
617 len += input_print_modalias_bits(buf + len, size - len,
618 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500619
620 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500621 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500622
Rusty Russell1d8f4302005-12-07 21:40:34 +0100623 return len;
624}
625
626static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
627{
628 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100629 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100630
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500631 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
632
Richard Purdie8a3cf452006-06-26 01:48:21 -0400633 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100634}
635static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
636
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700637static struct attribute *input_dev_attrs[] = {
638 &class_device_attr_name.attr,
639 &class_device_attr_phys.attr,
640 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100641 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700642 NULL
643};
644
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500645static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700646 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500647};
648
649#define INPUT_DEV_ID_ATTR(name) \
650static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
651{ \
652 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500653 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500654} \
655static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
656
657INPUT_DEV_ID_ATTR(bustype);
658INPUT_DEV_ID_ATTR(vendor);
659INPUT_DEV_ID_ATTR(product);
660INPUT_DEV_ID_ATTR(version);
661
662static struct attribute *input_dev_id_attrs[] = {
663 &class_device_attr_bustype.attr,
664 &class_device_attr_vendor.attr,
665 &class_device_attr_product.attr,
666 &class_device_attr_version.attr,
667 NULL
668};
669
670static struct attribute_group input_dev_id_attr_group = {
671 .name = "id",
672 .attrs = input_dev_id_attrs,
673};
674
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500675static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
676 int max, int add_cr)
677{
678 int i;
679 int len = 0;
680
681 for (i = NBITS(max) - 1; i > 0; i--)
682 if (bitmap[i])
683 break;
684
685 for (; i >= 0; i--)
686 len += snprintf(buf + len, max(buf_size - len, 0),
687 "%lx%s", bitmap[i], i > 0 ? " " : "");
688
689 if (add_cr)
690 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
691
692 return len;
693}
694
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500695#define INPUT_DEV_CAP_ATTR(ev, bm) \
696static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
697{ \
698 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500699 int len = input_print_bitmap(buf, PAGE_SIZE, \
700 input_dev->bm##bit, ev##_MAX, 1); \
701 return min_t(int, len, PAGE_SIZE); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500702} \
703static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
704
705INPUT_DEV_CAP_ATTR(EV, ev);
706INPUT_DEV_CAP_ATTR(KEY, key);
707INPUT_DEV_CAP_ATTR(REL, rel);
708INPUT_DEV_CAP_ATTR(ABS, abs);
709INPUT_DEV_CAP_ATTR(MSC, msc);
710INPUT_DEV_CAP_ATTR(LED, led);
711INPUT_DEV_CAP_ATTR(SND, snd);
712INPUT_DEV_CAP_ATTR(FF, ff);
713INPUT_DEV_CAP_ATTR(SW, sw);
714
715static struct attribute *input_dev_caps_attrs[] = {
716 &class_device_attr_ev.attr,
717 &class_device_attr_key.attr,
718 &class_device_attr_rel.attr,
719 &class_device_attr_abs.attr,
720 &class_device_attr_msc.attr,
721 &class_device_attr_led.attr,
722 &class_device_attr_snd.attr,
723 &class_device_attr_ff.attr,
724 &class_device_attr_sw.attr,
725 NULL
726};
727
728static struct attribute_group input_dev_caps_attr_group = {
729 .name = "capabilities",
730 .attrs = input_dev_caps_attrs,
731};
732
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500733static void input_dev_release(struct class_device *class_dev)
734{
735 struct input_dev *dev = to_input_dev(class_dev);
736
737 kfree(dev);
738 module_put(THIS_MODULE);
739}
740
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500741/*
Kay Sievers312c0042005-11-16 09:00:00 +0100742 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500743 * device bitfields.
744 */
Kay Sievers312c0042005-11-16 09:00:00 +0100745static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500746 char *buffer, int buffer_size, int *cur_len,
747 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500748{
749 if (*cur_index >= num_envp - 1)
750 return -ENOMEM;
751
752 envp[*cur_index] = buffer + *cur_len;
753
754 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500755 if (*cur_len >= buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500756 return -ENOMEM;
757
758 *cur_len += input_print_bitmap(buffer + *cur_len,
759 max(buffer_size - *cur_len, 0),
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500760 bitmap, max, 0) + 1;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500761 if (*cur_len > buffer_size)
762 return -ENOMEM;
763
764 (*cur_index)++;
765 return 0;
766}
767
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500768static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
769 char *buffer, int buffer_size, int *cur_len,
770 struct input_dev *dev)
771{
772 if (*cur_index >= num_envp - 1)
773 return -ENOMEM;
774
775 envp[*cur_index] = buffer + *cur_len;
776
777 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
778 "MODALIAS=");
779 if (*cur_len >= buffer_size)
780 return -ENOMEM;
781
782 *cur_len += input_print_modalias(buffer + *cur_len,
783 max(buffer_size - *cur_len, 0),
784 dev, 0) + 1;
785 if (*cur_len > buffer_size)
786 return -ENOMEM;
787
788 (*cur_index)++;
789 return 0;
790}
791
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500792#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
793 do { \
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500794 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500795 buffer, buffer_size, &len, \
796 fmt, val); \
797 if (err) \
798 return err; \
799 } while (0)
800
801#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
802 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100803 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500804 buffer, buffer_size, &len, \
805 name, bm, max); \
806 if (err) \
807 return err; \
808 } while (0)
809
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500810#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
811 do { \
812 int err = input_add_uevent_modalias_var(envp, \
813 num_envp, &i, \
814 buffer, buffer_size, &len, \
815 dev); \
816 if (err) \
817 return err; \
818 } while (0)
819
Kay Sievers312c0042005-11-16 09:00:00 +0100820static int input_dev_uevent(struct class_device *cdev, char **envp,
821 int num_envp, char *buffer, int buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500822{
823 struct input_dev *dev = to_input_dev(cdev);
824 int i = 0;
825 int len = 0;
826
827 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
828 dev->id.bustype, dev->id.vendor,
829 dev->id.product, dev->id.version);
830 if (dev->name)
831 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
832 if (dev->phys)
833 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800834 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500835 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
836
837 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
838 if (test_bit(EV_KEY, dev->evbit))
839 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
840 if (test_bit(EV_REL, dev->evbit))
841 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
842 if (test_bit(EV_ABS, dev->evbit))
843 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
844 if (test_bit(EV_MSC, dev->evbit))
845 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
846 if (test_bit(EV_LED, dev->evbit))
847 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
848 if (test_bit(EV_SND, dev->evbit))
849 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
850 if (test_bit(EV_FF, dev->evbit))
851 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
852 if (test_bit(EV_SW, dev->evbit))
853 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
854
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500855 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500856
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100857 envp[i] = NULL;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500858 return 0;
859}
860
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700861struct class input_class = {
862 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500863 .release = input_dev_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100864 .uevent = input_dev_uevent,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500865};
866
867struct input_dev *input_allocate_device(void)
868{
869 struct input_dev *dev;
870
871 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
872 if (dev) {
873 dev->dynalloc = 1;
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700874 dev->cdev.class = &input_class;
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500875 class_device_initialize(&dev->cdev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400876 mutex_init(&dev->mutex);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500877 INIT_LIST_HEAD(&dev->h_list);
878 INIT_LIST_HEAD(&dev->node);
879 }
880
881 return dev;
882}
883
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400884void input_free_device(struct input_dev *dev)
885{
886 if (dev) {
887
888 mutex_lock(&dev->mutex);
889 dev->name = dev->phys = dev->uniq = NULL;
890 mutex_unlock(&dev->mutex);
891
892 input_put_device(dev);
893 }
894}
895
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500896int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500897{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500898 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500899 struct input_handle *handle;
900 struct input_handler *handler;
901 struct input_device_id *id;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500902 const char *path;
903 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500904
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500905 if (!dev->dynalloc) {
906 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
907 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
908 dev->name ? dev->name : "<Unknown>");
909 return -EINVAL;
910 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500911
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500912 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500913
914 /*
915 * If delay and period are pre-set by the driver, then autorepeating
916 * is handled by the driver itself and we don't do it in input.c.
917 */
918
919 init_timer(&dev->timer);
920 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
921 dev->timer.data = (long) dev;
922 dev->timer.function = input_repeat_key;
923 dev->rep[REP_DELAY] = 250;
924 dev->rep[REP_PERIOD] = 33;
925 }
926
927 INIT_LIST_HEAD(&dev->h_list);
928 list_add_tail(&dev->node, &input_dev_list);
929
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500930 dev->cdev.class = &input_class;
931 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
932 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
933
934 error = class_device_add(&dev->cdev);
935 if (error)
936 return error;
937
938 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
939 if (error)
940 goto fail1;
941
942 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
943 if (error)
944 goto fail2;
945
946 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
947 if (error)
948 goto fail3;
949
950 __module_get(THIS_MODULE);
951
952 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
953 printk(KERN_INFO "input: %s as %s\n",
954 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
955 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -0700956
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500957 list_for_each_entry(handler, &input_handler_list, node)
958 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
959 if ((id = input_match_device(handler->id_table, dev)))
960 if ((handle = handler->connect(handler, dev, id)))
961 input_link_handle(handle);
962
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500963 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500964
965 return 0;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500966
967 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
968 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
969 fail1: class_device_del(&dev->cdev);
970 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500971}
972
973void input_unregister_device(struct input_dev *dev)
974{
975 struct list_head * node, * next;
976
977 if (!dev) return;
978
979 del_timer_sync(&dev->timer);
980
981 list_for_each_safe(node, next, &dev->h_list) {
982 struct input_handle * handle = to_handle(node);
983 list_del_init(&handle->d_node);
984 list_del_init(&handle->h_node);
985 handle->handler->disconnect(handle);
986 }
987
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500988 list_del_init(&dev->node);
989
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500990 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
991 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500992 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500993 class_device_unregister(&dev->cdev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500994
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400995 mutex_lock(&dev->mutex);
996 dev->name = dev->phys = dev->uniq = NULL;
997 mutex_unlock(&dev->mutex);
998
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500999 input_wakeup_procfs_readers();
1000}
1001
1002void input_register_handler(struct input_handler *handler)
1003{
1004 struct input_dev *dev;
1005 struct input_handle *handle;
1006 struct input_device_id *id;
1007
1008 if (!handler) return;
1009
1010 INIT_LIST_HEAD(&handler->h_list);
1011
1012 if (handler->fops != NULL)
1013 input_table[handler->minor >> 5] = handler;
1014
1015 list_add_tail(&handler->node, &input_handler_list);
1016
1017 list_for_each_entry(dev, &input_dev_list, node)
1018 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1019 if ((id = input_match_device(handler->id_table, dev)))
1020 if ((handle = handler->connect(handler, dev, id)))
1021 input_link_handle(handle);
1022
1023 input_wakeup_procfs_readers();
1024}
1025
1026void input_unregister_handler(struct input_handler *handler)
1027{
1028 struct list_head * node, * next;
1029
1030 list_for_each_safe(node, next, &handler->h_list) {
1031 struct input_handle * handle = to_handle_h(node);
1032 list_del_init(&handle->h_node);
1033 list_del_init(&handle->d_node);
1034 handler->disconnect(handle);
1035 }
1036
1037 list_del_init(&handler->node);
1038
1039 if (handler->fops != NULL)
1040 input_table[handler->minor >> 5] = NULL;
1041
1042 input_wakeup_procfs_readers();
1043}
1044
1045static int input_open_file(struct inode *inode, struct file *file)
1046{
1047 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001048 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001049 int err;
1050
1051 /* No load-on-demand here? */
1052 if (!handler || !(new_fops = fops_get(handler->fops)))
1053 return -ENODEV;
1054
1055 /*
1056 * That's _really_ odd. Usually NULL ->open means "nothing special",
1057 * not "no device". Oh, well...
1058 */
1059 if (!new_fops->open) {
1060 fops_put(new_fops);
1061 return -ENODEV;
1062 }
1063 old_fops = file->f_op;
1064 file->f_op = new_fops;
1065
1066 err = new_fops->open(inode, file);
1067
1068 if (err) {
1069 fops_put(file->f_op);
1070 file->f_op = fops_get(old_fops);
1071 }
1072 fops_put(old_fops);
1073 return err;
1074}
1075
1076static struct file_operations input_fops = {
1077 .owner = THIS_MODULE,
1078 .open = input_open_file,
1079};
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081static int __init input_init(void)
1082{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001083 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001085 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001086 if (err) {
1087 printk(KERN_ERR "input: unable to register input_dev class\n");
1088 return err;
1089 }
1090
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001091 err = input_proc_init();
1092 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001093 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001094
1095 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1096 if (err) {
1097 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001098 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001100
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001101 return 0;
1102
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001103 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001104 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001105 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108static void __exit input_exit(void)
1109{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001110 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001112 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115subsys_initcall(input_init);
1116module_exit(input_exit);