blob: 7aeebb9071c2e95a5c98743c1269423cde367cbe [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define INPUT_DEVICES 256
32
33static LIST_HEAD(input_dev_list);
34static LIST_HEAD(input_handler_list);
35
36static struct input_handler *input_table[8];
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038void 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 Torokhov1e0afb22006-06-26 01:48:47 -040052 if (dev->event)
53 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 break;
55
56 case SYN_REPORT:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040057 if (dev->sync)
58 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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 Purdie31581062005-09-06 15:19:06 -070081 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 Torvalds1da177e2005-04-16 15:20:36 -070090 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 Torokhov1e0afb22006-06-26 01:48:47 -0400127 if (dev->event)
128 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
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 Torokhov1e0afb22006-06-26 01:48:47 -0400138
139 if (dev->event)
140 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 break;
143
144 case EV_SND:
145
146 if (code > SND_MAX || !test_bit(code, dev->sndbit))
147 return;
148
Dmitry Torokhov8fdc1942006-04-29 01:13:48 -0400149 if (!!test_bit(code, dev->snd) != !!value)
150 change_bit(code, dev->snd);
151
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400152 if (dev->event)
153 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 break;
156
157 case EV_REP:
158
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400159 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
160 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 dev->rep[code] = value;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400163 if (dev->event)
164 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 break;
167
168 case EV_FF:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400169 if (dev->event)
170 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 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 Torokhovca56fe02006-06-26 01:49:21 -0400184EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200int input_grab_device(struct input_handle *handle)
201{
202 if (handle->dev->grab)
203 return -EBUSY;
204
205 handle->dev->grab = handle;
206 return 0;
207}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400208EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210void input_release_device(struct input_handle *handle)
211{
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400212 if (handle->dev->grab == handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 handle->dev->grab = NULL;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400214
215 list_for_each_entry(handle, &handle->dev->h_list, d_node)
216 if (handle->handler->start)
217 handle->handler->start(handle);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400220EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222int input_open_device(struct input_handle *handle)
223{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500224 struct input_dev *dev = handle->dev;
225 int err;
226
Jes Sorensene676c232006-02-19 00:21:46 -0500227 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500228 if (err)
229 return err;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500232
233 if (!dev->users++ && dev->open)
234 err = dev->open(dev);
235
236 if (err)
237 handle->open--;
238
Jes Sorensene676c232006-02-19 00:21:46 -0500239 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500240
241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400243EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245int input_flush_device(struct input_handle* handle, struct file* file)
246{
247 if (handle->dev->flush)
248 return handle->dev->flush(handle->dev, file);
249
250 return 0;
251}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400252EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254void input_close_device(struct input_handle *handle)
255{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500256 struct input_dev *dev = handle->dev;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500259
Jes Sorensene676c232006-02-19 00:21:46 -0500260 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500261
262 if (!--dev->users && dev->close)
263 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500265
Jes Sorensene676c232006-02-19 00:21:46 -0500266 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400268EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270static void input_link_handle(struct input_handle *handle)
271{
272 list_add_tail(&handle->d_node, &handle->dev->h_list);
273 list_add_tail(&handle->h_node, &handle->handler->h_list);
274}
275
276#define MATCH_BIT(bit, max) \
277 for (i = 0; i < NBITS(max); i++) \
278 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
279 break; \
280 if (i != NBITS(max)) \
281 continue;
282
283static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
284{
285 int i;
286
287 for (; id->flags || id->driver_info; id++) {
288
289 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400290 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 continue;
292
293 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400294 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 continue;
296
297 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400298 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 continue;
300
301 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400302 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 continue;
304
305 MATCH_BIT(evbit, EV_MAX);
306 MATCH_BIT(keybit, KEY_MAX);
307 MATCH_BIT(relbit, REL_MAX);
308 MATCH_BIT(absbit, ABS_MAX);
309 MATCH_BIT(mscbit, MSC_MAX);
310 MATCH_BIT(ledbit, LED_MAX);
311 MATCH_BIT(sndbit, SND_MAX);
312 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500313 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 return id;
316 }
317
318 return NULL;
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500322
323static struct proc_dir_entry *proc_bus_input_dir;
324static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
325static int input_devices_state;
326
327static inline void input_wakeup_procfs_readers(void)
328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 input_devices_state++;
330 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500333static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500335 int state = input_devices_state;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400336
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500337 poll_wait(file, &input_devices_poll_wait, wait);
338 if (state != input_devices_state)
339 return POLLIN | POLLRDNORM;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400340
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500344static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500346 struct list_head *node;
347 loff_t i = 0;
348
349 list_for_each(node, list)
350 if (i++ == *pos)
351 return node;
352
353 return NULL;
354}
355
356static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
357{
358 if (element->next == list)
359 return NULL;
360
361 ++(*pos);
362 return element->next;
363}
364
365static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
366{
367 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
368
369 return list_get_nth_element(&input_dev_list, pos);
370}
371
372static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
373{
374 return list_get_next_element(&input_dev_list, v, pos);
375}
376
377static void input_devices_seq_stop(struct seq_file *seq, void *v)
378{
379 /* release lock here */
380}
381
382static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
383 unsigned long *bitmap, int max)
384{
385 int i;
386
387 for (i = NBITS(max) - 1; i > 0; i--)
388 if (bitmap[i])
389 break;
390
391 seq_printf(seq, "B: %s=", name);
392 for (; i >= 0; i--)
393 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
394 seq_putc(seq, '\n');
395}
396
397static int input_devices_seq_show(struct seq_file *seq, void *v)
398{
399 struct input_dev *dev = container_of(v, struct input_dev, node);
400 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct input_handle *handle;
402
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500403 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
404 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500406 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
407 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
408 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
409 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500411 list_for_each_entry(handle, &dev->h_list, d_node)
412 seq_printf(seq, "%s ", handle->name);
413 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500414
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500415 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
416 if (test_bit(EV_KEY, dev->evbit))
417 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
418 if (test_bit(EV_REL, dev->evbit))
419 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
420 if (test_bit(EV_ABS, dev->evbit))
421 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
422 if (test_bit(EV_MSC, dev->evbit))
423 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
424 if (test_bit(EV_LED, dev->evbit))
425 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
426 if (test_bit(EV_SND, dev->evbit))
427 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
428 if (test_bit(EV_FF, dev->evbit))
429 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
430 if (test_bit(EV_SW, dev->evbit))
431 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500433 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500435 kfree(path);
436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500439static struct seq_operations input_devices_seq_ops = {
440 .start = input_devices_seq_start,
441 .next = input_devices_seq_next,
442 .stop = input_devices_seq_stop,
443 .show = input_devices_seq_show,
444};
445
446static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500448 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500451static struct file_operations input_devices_fileops = {
452 .owner = THIS_MODULE,
453 .open = input_proc_devices_open,
454 .poll = input_proc_devices_poll,
455 .read = seq_read,
456 .llseek = seq_lseek,
457 .release = seq_release,
458};
459
460static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
461{
462 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
463 seq->private = (void *)(unsigned long)*pos;
464 return list_get_nth_element(&input_handler_list, pos);
465}
466
467static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
468{
469 seq->private = (void *)(unsigned long)(*pos + 1);
470 return list_get_next_element(&input_handler_list, v, pos);
471}
472
473static void input_handlers_seq_stop(struct seq_file *seq, void *v)
474{
475 /* release lock here */
476}
477
478static int input_handlers_seq_show(struct seq_file *seq, void *v)
479{
480 struct input_handler *handler = container_of(v, struct input_handler, node);
481
482 seq_printf(seq, "N: Number=%ld Name=%s",
483 (unsigned long)seq->private, handler->name);
484 if (handler->fops)
485 seq_printf(seq, " Minor=%d", handler->minor);
486 seq_putc(seq, '\n');
487
488 return 0;
489}
490static struct seq_operations input_handlers_seq_ops = {
491 .start = input_handlers_seq_start,
492 .next = input_handlers_seq_next,
493 .stop = input_handlers_seq_stop,
494 .show = input_handlers_seq_show,
495};
496
497static int input_proc_handlers_open(struct inode *inode, struct file *file)
498{
499 return seq_open(file, &input_handlers_seq_ops);
500}
501
502static struct file_operations input_handlers_fileops = {
503 .owner = THIS_MODULE,
504 .open = input_proc_handlers_open,
505 .read = seq_read,
506 .llseek = seq_lseek,
507 .release = seq_release,
508};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static int __init input_proc_init(void)
511{
512 struct proc_dir_entry *entry;
513
514 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500515 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500519
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500520 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500521 if (!entry)
522 goto fail1;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500525 entry->proc_fops = &input_devices_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500526
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500527 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500528 if (!entry)
529 goto fail2;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500532 entry->proc_fops = &input_handlers_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500535
536 fail2: remove_proc_entry("devices", proc_bus_input_dir);
537 fail1: remove_proc_entry("input", proc_bus);
538 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500540
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500541static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500542{
543 remove_proc_entry("devices", proc_bus_input_dir);
544 remove_proc_entry("handlers", proc_bus_input_dir);
545 remove_proc_entry("input", proc_bus);
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500549static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500551static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552#endif
553
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500554#define INPUT_DEV_STRING_ATTR_SHOW(name) \
555static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
556{ \
557 struct input_dev *input_dev = to_input_dev(dev); \
558 int retval; \
559 \
Jes Sorensene676c232006-02-19 00:21:46 -0500560 retval = mutex_lock_interruptible(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500561 if (retval) \
562 return retval; \
563 \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500564 retval = scnprintf(buf, PAGE_SIZE, \
565 "%s\n", input_dev->name ? input_dev->name : ""); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500566 \
Jes Sorensene676c232006-02-19 00:21:46 -0500567 mutex_unlock(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500568 \
569 return retval; \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700570} \
571static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500572
573INPUT_DEV_STRING_ATTR_SHOW(name);
574INPUT_DEV_STRING_ATTR_SHOW(phys);
575INPUT_DEV_STRING_ATTR_SHOW(uniq);
576
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500577static int input_print_modalias_bits(char *buf, int size,
578 char name, unsigned long *bm,
579 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100580{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500581 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100582
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500583 len += snprintf(buf, max(size, 0), "%c", name);
584 for (i = min_bit; i < max_bit; i++)
585 if (bm[LONG(i)] & BIT(i))
586 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100587 return len;
588}
589
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500590static int input_print_modalias(char *buf, int size, struct input_dev *id,
591 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100592{
593 int len;
594
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500595 len = snprintf(buf, max(size, 0),
596 "input:b%04Xv%04Xp%04Xe%04X-",
597 id->id.bustype, id->id.vendor,
598 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100599
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500600 len += input_print_modalias_bits(buf + len, size - len,
601 'e', id->evbit, 0, EV_MAX);
602 len += input_print_modalias_bits(buf + len, size - len,
603 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
604 len += input_print_modalias_bits(buf + len, size - len,
605 'r', id->relbit, 0, REL_MAX);
606 len += input_print_modalias_bits(buf + len, size - len,
607 'a', id->absbit, 0, ABS_MAX);
608 len += input_print_modalias_bits(buf + len, size - len,
609 'm', id->mscbit, 0, MSC_MAX);
610 len += input_print_modalias_bits(buf + len, size - len,
611 'l', id->ledbit, 0, LED_MAX);
612 len += input_print_modalias_bits(buf + len, size - len,
613 's', id->sndbit, 0, SND_MAX);
614 len += input_print_modalias_bits(buf + len, size - len,
615 'f', id->ffbit, 0, FF_MAX);
616 len += input_print_modalias_bits(buf + len, size - len,
617 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500618
619 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500620 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500621
Rusty Russell1d8f4302005-12-07 21:40:34 +0100622 return len;
623}
624
625static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
626{
627 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100628 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100629
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500630 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
631
Richard Purdie8a3cf452006-06-26 01:48:21 -0400632 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100633}
634static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
635
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700636static struct attribute *input_dev_attrs[] = {
637 &class_device_attr_name.attr,
638 &class_device_attr_phys.attr,
639 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100640 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700641 NULL
642};
643
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500644static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700645 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500646};
647
648#define INPUT_DEV_ID_ATTR(name) \
649static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
650{ \
651 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500652 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500653} \
654static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
655
656INPUT_DEV_ID_ATTR(bustype);
657INPUT_DEV_ID_ATTR(vendor);
658INPUT_DEV_ID_ATTR(product);
659INPUT_DEV_ID_ATTR(version);
660
661static struct attribute *input_dev_id_attrs[] = {
662 &class_device_attr_bustype.attr,
663 &class_device_attr_vendor.attr,
664 &class_device_attr_product.attr,
665 &class_device_attr_version.attr,
666 NULL
667};
668
669static struct attribute_group input_dev_id_attr_group = {
670 .name = "id",
671 .attrs = input_dev_id_attrs,
672};
673
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500674static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
675 int max, int add_cr)
676{
677 int i;
678 int len = 0;
679
680 for (i = NBITS(max) - 1; i > 0; i--)
681 if (bitmap[i])
682 break;
683
684 for (; i >= 0; i--)
685 len += snprintf(buf + len, max(buf_size - len, 0),
686 "%lx%s", bitmap[i], i > 0 ? " " : "");
687
688 if (add_cr)
689 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
690
691 return len;
692}
693
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500694#define INPUT_DEV_CAP_ATTR(ev, bm) \
695static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
696{ \
697 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500698 int len = input_print_bitmap(buf, PAGE_SIZE, \
699 input_dev->bm##bit, ev##_MAX, 1); \
700 return min_t(int, len, PAGE_SIZE); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500701} \
702static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
703
704INPUT_DEV_CAP_ATTR(EV, ev);
705INPUT_DEV_CAP_ATTR(KEY, key);
706INPUT_DEV_CAP_ATTR(REL, rel);
707INPUT_DEV_CAP_ATTR(ABS, abs);
708INPUT_DEV_CAP_ATTR(MSC, msc);
709INPUT_DEV_CAP_ATTR(LED, led);
710INPUT_DEV_CAP_ATTR(SND, snd);
711INPUT_DEV_CAP_ATTR(FF, ff);
712INPUT_DEV_CAP_ATTR(SW, sw);
713
714static struct attribute *input_dev_caps_attrs[] = {
715 &class_device_attr_ev.attr,
716 &class_device_attr_key.attr,
717 &class_device_attr_rel.attr,
718 &class_device_attr_abs.attr,
719 &class_device_attr_msc.attr,
720 &class_device_attr_led.attr,
721 &class_device_attr_snd.attr,
722 &class_device_attr_ff.attr,
723 &class_device_attr_sw.attr,
724 NULL
725};
726
727static struct attribute_group input_dev_caps_attr_group = {
728 .name = "capabilities",
729 .attrs = input_dev_caps_attrs,
730};
731
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500732static void input_dev_release(struct class_device *class_dev)
733{
734 struct input_dev *dev = to_input_dev(class_dev);
735
736 kfree(dev);
737 module_put(THIS_MODULE);
738}
739
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500740/*
Kay Sievers312c0042005-11-16 09:00:00 +0100741 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500742 * device bitfields.
743 */
Kay Sievers312c0042005-11-16 09:00:00 +0100744static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500745 char *buffer, int buffer_size, int *cur_len,
746 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500747{
748 if (*cur_index >= num_envp - 1)
749 return -ENOMEM;
750
751 envp[*cur_index] = buffer + *cur_len;
752
753 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500754 if (*cur_len >= buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500755 return -ENOMEM;
756
757 *cur_len += input_print_bitmap(buffer + *cur_len,
758 max(buffer_size - *cur_len, 0),
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500759 bitmap, max, 0) + 1;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500760 if (*cur_len > buffer_size)
761 return -ENOMEM;
762
763 (*cur_index)++;
764 return 0;
765}
766
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500767static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
768 char *buffer, int buffer_size, int *cur_len,
769 struct input_dev *dev)
770{
771 if (*cur_index >= num_envp - 1)
772 return -ENOMEM;
773
774 envp[*cur_index] = buffer + *cur_len;
775
776 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
777 "MODALIAS=");
778 if (*cur_len >= buffer_size)
779 return -ENOMEM;
780
781 *cur_len += input_print_modalias(buffer + *cur_len,
782 max(buffer_size - *cur_len, 0),
783 dev, 0) + 1;
784 if (*cur_len > buffer_size)
785 return -ENOMEM;
786
787 (*cur_index)++;
788 return 0;
789}
790
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500791#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
792 do { \
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500793 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500794 buffer, buffer_size, &len, \
795 fmt, val); \
796 if (err) \
797 return err; \
798 } while (0)
799
800#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
801 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100802 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500803 buffer, buffer_size, &len, \
804 name, bm, max); \
805 if (err) \
806 return err; \
807 } while (0)
808
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500809#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
810 do { \
811 int err = input_add_uevent_modalias_var(envp, \
812 num_envp, &i, \
813 buffer, buffer_size, &len, \
814 dev); \
815 if (err) \
816 return err; \
817 } while (0)
818
Kay Sievers312c0042005-11-16 09:00:00 +0100819static int input_dev_uevent(struct class_device *cdev, char **envp,
820 int num_envp, char *buffer, int buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500821{
822 struct input_dev *dev = to_input_dev(cdev);
823 int i = 0;
824 int len = 0;
825
826 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
827 dev->id.bustype, dev->id.vendor,
828 dev->id.product, dev->id.version);
829 if (dev->name)
830 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
831 if (dev->phys)
832 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800833 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500834 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
835
836 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
837 if (test_bit(EV_KEY, dev->evbit))
838 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
839 if (test_bit(EV_REL, dev->evbit))
840 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
841 if (test_bit(EV_ABS, dev->evbit))
842 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
843 if (test_bit(EV_MSC, dev->evbit))
844 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
845 if (test_bit(EV_LED, dev->evbit))
846 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
847 if (test_bit(EV_SND, dev->evbit))
848 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
849 if (test_bit(EV_FF, dev->evbit))
850 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
851 if (test_bit(EV_SW, dev->evbit))
852 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
853
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500854 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500855
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100856 envp[i] = NULL;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500857 return 0;
858}
859
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700860struct class input_class = {
861 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500862 .release = input_dev_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100863 .uevent = input_dev_uevent,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500864};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400865EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500866
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}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400883EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500884
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400885void input_free_device(struct input_dev *dev)
886{
887 if (dev) {
888
889 mutex_lock(&dev->mutex);
890 dev->name = dev->phys = dev->uniq = NULL;
891 mutex_unlock(&dev->mutex);
892
893 input_put_device(dev);
894 }
895}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400896EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400897
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500898int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500899{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500900 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500901 struct input_handle *handle;
902 struct input_handler *handler;
903 struct input_device_id *id;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500904 const char *path;
905 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500906
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500907 if (!dev->dynalloc) {
908 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
909 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
910 dev->name ? dev->name : "<Unknown>");
911 return -EINVAL;
912 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500913
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500914 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500915
916 /*
917 * If delay and period are pre-set by the driver, then autorepeating
918 * is handled by the driver itself and we don't do it in input.c.
919 */
920
921 init_timer(&dev->timer);
922 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
923 dev->timer.data = (long) dev;
924 dev->timer.function = input_repeat_key;
925 dev->rep[REP_DELAY] = 250;
926 dev->rep[REP_PERIOD] = 33;
927 }
928
929 INIT_LIST_HEAD(&dev->h_list);
930 list_add_tail(&dev->node, &input_dev_list);
931
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500932 dev->cdev.class = &input_class;
933 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
934 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
935
936 error = class_device_add(&dev->cdev);
937 if (error)
938 return error;
939
940 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
941 if (error)
942 goto fail1;
943
944 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
945 if (error)
946 goto fail2;
947
948 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
949 if (error)
950 goto fail3;
951
952 __module_get(THIS_MODULE);
953
954 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
955 printk(KERN_INFO "input: %s as %s\n",
956 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
957 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -0700958
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500959 list_for_each_entry(handler, &input_handler_list, node)
960 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
961 if ((id = input_match_device(handler->id_table, dev)))
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400962 if ((handle = handler->connect(handler, dev, id))) {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500963 input_link_handle(handle);
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400964 if (handler->start)
965 handler->start(handle);
966 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500967
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500968 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500969
970 return 0;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500971
972 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
973 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
974 fail1: class_device_del(&dev->cdev);
975 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500976}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400977EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500978
979void input_unregister_device(struct input_dev *dev)
980{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400981 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500982
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400983 if (!dev)
984 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500985
986 del_timer_sync(&dev->timer);
987
988 list_for_each_safe(node, next, &dev->h_list) {
989 struct input_handle * handle = to_handle(node);
990 list_del_init(&handle->d_node);
991 list_del_init(&handle->h_node);
992 handle->handler->disconnect(handle);
993 }
994
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500995 list_del_init(&dev->node);
996
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500997 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
998 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500999 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001000
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001001 mutex_lock(&dev->mutex);
1002 dev->name = dev->phys = dev->uniq = NULL;
1003 mutex_unlock(&dev->mutex);
1004
Dmitry Torokhove7374e42006-06-27 08:30:31 -04001005 class_device_unregister(&dev->cdev);
1006
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001007 input_wakeup_procfs_readers();
1008}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001009EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001010
1011void input_register_handler(struct input_handler *handler)
1012{
1013 struct input_dev *dev;
1014 struct input_handle *handle;
1015 struct input_device_id *id;
1016
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001017 if (!handler)
1018 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001019
1020 INIT_LIST_HEAD(&handler->h_list);
1021
1022 if (handler->fops != NULL)
1023 input_table[handler->minor >> 5] = handler;
1024
1025 list_add_tail(&handler->node, &input_handler_list);
1026
1027 list_for_each_entry(dev, &input_dev_list, node)
1028 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1029 if ((id = input_match_device(handler->id_table, dev)))
1030 if ((handle = handler->connect(handler, dev, id)))
1031 input_link_handle(handle);
1032
1033 input_wakeup_procfs_readers();
1034}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001035EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001036
1037void input_unregister_handler(struct input_handler *handler)
1038{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001039 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001040
1041 list_for_each_safe(node, next, &handler->h_list) {
1042 struct input_handle * handle = to_handle_h(node);
1043 list_del_init(&handle->h_node);
1044 list_del_init(&handle->d_node);
1045 handler->disconnect(handle);
1046 }
1047
1048 list_del_init(&handler->node);
1049
1050 if (handler->fops != NULL)
1051 input_table[handler->minor >> 5] = NULL;
1052
1053 input_wakeup_procfs_readers();
1054}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001055EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001056
1057static int input_open_file(struct inode *inode, struct file *file)
1058{
1059 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001060 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001061 int err;
1062
1063 /* No load-on-demand here? */
1064 if (!handler || !(new_fops = fops_get(handler->fops)))
1065 return -ENODEV;
1066
1067 /*
1068 * That's _really_ odd. Usually NULL ->open means "nothing special",
1069 * not "no device". Oh, well...
1070 */
1071 if (!new_fops->open) {
1072 fops_put(new_fops);
1073 return -ENODEV;
1074 }
1075 old_fops = file->f_op;
1076 file->f_op = new_fops;
1077
1078 err = new_fops->open(inode, file);
1079
1080 if (err) {
1081 fops_put(file->f_op);
1082 file->f_op = fops_get(old_fops);
1083 }
1084 fops_put(old_fops);
1085 return err;
1086}
1087
1088static struct file_operations input_fops = {
1089 .owner = THIS_MODULE,
1090 .open = input_open_file,
1091};
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093static int __init input_init(void)
1094{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001095 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001097 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001098 if (err) {
1099 printk(KERN_ERR "input: unable to register input_dev class\n");
1100 return err;
1101 }
1102
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001103 err = input_proc_init();
1104 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001105 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001106
1107 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1108 if (err) {
1109 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001110 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001112
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001113 return 0;
1114
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001115 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001116 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120static void __exit input_exit(void)
1121{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001122 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001124 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127subsys_initcall(input_init);
1128module_exit(input_exit);