blob: c2e4d9bdcabfeb4e9f7a8abc95f119213774c357 [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{
212 if (handle->dev->grab == handle)
213 handle->dev->grab = NULL;
214}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400215EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217int input_open_device(struct input_handle *handle)
218{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500219 struct input_dev *dev = handle->dev;
220 int err;
221
Jes Sorensene676c232006-02-19 00:21:46 -0500222 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500223 if (err)
224 return err;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500227
228 if (!dev->users++ && dev->open)
229 err = dev->open(dev);
230
231 if (err)
232 handle->open--;
233
Jes Sorensene676c232006-02-19 00:21:46 -0500234 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500235
236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400238EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240int input_flush_device(struct input_handle* handle, struct file* file)
241{
242 if (handle->dev->flush)
243 return handle->dev->flush(handle->dev, file);
244
245 return 0;
246}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400247EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249void input_close_device(struct input_handle *handle)
250{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500251 struct input_dev *dev = handle->dev;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500254
Jes Sorensene676c232006-02-19 00:21:46 -0500255 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500256
257 if (!--dev->users && dev->close)
258 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500260
Jes Sorensene676c232006-02-19 00:21:46 -0500261 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400263EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265static void input_link_handle(struct input_handle *handle)
266{
267 list_add_tail(&handle->d_node, &handle->dev->h_list);
268 list_add_tail(&handle->h_node, &handle->handler->h_list);
269}
270
271#define MATCH_BIT(bit, max) \
272 for (i = 0; i < NBITS(max); i++) \
273 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
274 break; \
275 if (i != NBITS(max)) \
276 continue;
277
278static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
279{
280 int i;
281
282 for (; id->flags || id->driver_info; id++) {
283
284 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400285 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 continue;
287
288 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400289 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 continue;
291
292 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400293 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 continue;
295
296 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400297 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 continue;
299
300 MATCH_BIT(evbit, EV_MAX);
301 MATCH_BIT(keybit, KEY_MAX);
302 MATCH_BIT(relbit, REL_MAX);
303 MATCH_BIT(absbit, ABS_MAX);
304 MATCH_BIT(mscbit, MSC_MAX);
305 MATCH_BIT(ledbit, LED_MAX);
306 MATCH_BIT(sndbit, SND_MAX);
307 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500308 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 return id;
311 }
312
313 return NULL;
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500317
318static struct proc_dir_entry *proc_bus_input_dir;
319static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
320static int input_devices_state;
321
322static inline void input_wakeup_procfs_readers(void)
323{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 input_devices_state++;
325 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500328static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500330 int state = input_devices_state;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400331
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500332 poll_wait(file, &input_devices_poll_wait, wait);
333 if (state != input_devices_state)
334 return POLLIN | POLLRDNORM;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400335
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500339static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500341 struct list_head *node;
342 loff_t i = 0;
343
344 list_for_each(node, list)
345 if (i++ == *pos)
346 return node;
347
348 return NULL;
349}
350
351static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
352{
353 if (element->next == list)
354 return NULL;
355
356 ++(*pos);
357 return element->next;
358}
359
360static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
361{
362 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
363
364 return list_get_nth_element(&input_dev_list, pos);
365}
366
367static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
368{
369 return list_get_next_element(&input_dev_list, v, pos);
370}
371
372static void input_devices_seq_stop(struct seq_file *seq, void *v)
373{
374 /* release lock here */
375}
376
377static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
378 unsigned long *bitmap, int max)
379{
380 int i;
381
382 for (i = NBITS(max) - 1; i > 0; i--)
383 if (bitmap[i])
384 break;
385
386 seq_printf(seq, "B: %s=", name);
387 for (; i >= 0; i--)
388 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
389 seq_putc(seq, '\n');
390}
391
392static int input_devices_seq_show(struct seq_file *seq, void *v)
393{
394 struct input_dev *dev = container_of(v, struct input_dev, node);
395 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct input_handle *handle;
397
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500398 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
399 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500401 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
402 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
403 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
404 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500406 list_for_each_entry(handle, &dev->h_list, d_node)
407 seq_printf(seq, "%s ", handle->name);
408 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500409
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500410 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
411 if (test_bit(EV_KEY, dev->evbit))
412 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
413 if (test_bit(EV_REL, dev->evbit))
414 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
415 if (test_bit(EV_ABS, dev->evbit))
416 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
417 if (test_bit(EV_MSC, dev->evbit))
418 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
419 if (test_bit(EV_LED, dev->evbit))
420 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
421 if (test_bit(EV_SND, dev->evbit))
422 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
423 if (test_bit(EV_FF, dev->evbit))
424 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
425 if (test_bit(EV_SW, dev->evbit))
426 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500428 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500430 kfree(path);
431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500434static struct seq_operations input_devices_seq_ops = {
435 .start = input_devices_seq_start,
436 .next = input_devices_seq_next,
437 .stop = input_devices_seq_stop,
438 .show = input_devices_seq_show,
439};
440
441static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500443 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500446static struct file_operations input_devices_fileops = {
447 .owner = THIS_MODULE,
448 .open = input_proc_devices_open,
449 .poll = input_proc_devices_poll,
450 .read = seq_read,
451 .llseek = seq_lseek,
452 .release = seq_release,
453};
454
455static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
456{
457 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
458 seq->private = (void *)(unsigned long)*pos;
459 return list_get_nth_element(&input_handler_list, pos);
460}
461
462static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
463{
464 seq->private = (void *)(unsigned long)(*pos + 1);
465 return list_get_next_element(&input_handler_list, v, pos);
466}
467
468static void input_handlers_seq_stop(struct seq_file *seq, void *v)
469{
470 /* release lock here */
471}
472
473static int input_handlers_seq_show(struct seq_file *seq, void *v)
474{
475 struct input_handler *handler = container_of(v, struct input_handler, node);
476
477 seq_printf(seq, "N: Number=%ld Name=%s",
478 (unsigned long)seq->private, handler->name);
479 if (handler->fops)
480 seq_printf(seq, " Minor=%d", handler->minor);
481 seq_putc(seq, '\n');
482
483 return 0;
484}
485static struct seq_operations input_handlers_seq_ops = {
486 .start = input_handlers_seq_start,
487 .next = input_handlers_seq_next,
488 .stop = input_handlers_seq_stop,
489 .show = input_handlers_seq_show,
490};
491
492static int input_proc_handlers_open(struct inode *inode, struct file *file)
493{
494 return seq_open(file, &input_handlers_seq_ops);
495}
496
497static struct file_operations input_handlers_fileops = {
498 .owner = THIS_MODULE,
499 .open = input_proc_handlers_open,
500 .read = seq_read,
501 .llseek = seq_lseek,
502 .release = seq_release,
503};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static int __init input_proc_init(void)
506{
507 struct proc_dir_entry *entry;
508
509 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500510 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500514
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500515 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500516 if (!entry)
517 goto fail1;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500520 entry->proc_fops = &input_devices_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500521
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500522 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500523 if (!entry)
524 goto fail2;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500527 entry->proc_fops = &input_handlers_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500530
531 fail2: remove_proc_entry("devices", proc_bus_input_dir);
532 fail1: remove_proc_entry("input", proc_bus);
533 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500535
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500536static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500537{
538 remove_proc_entry("devices", proc_bus_input_dir);
539 remove_proc_entry("handlers", proc_bus_input_dir);
540 remove_proc_entry("input", proc_bus);
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500544static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500546static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#endif
548
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500549#define INPUT_DEV_STRING_ATTR_SHOW(name) \
550static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
551{ \
552 struct input_dev *input_dev = to_input_dev(dev); \
553 int retval; \
554 \
Jes Sorensene676c232006-02-19 00:21:46 -0500555 retval = mutex_lock_interruptible(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500556 if (retval) \
557 return retval; \
558 \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500559 retval = scnprintf(buf, PAGE_SIZE, \
560 "%s\n", input_dev->name ? input_dev->name : ""); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500561 \
Jes Sorensene676c232006-02-19 00:21:46 -0500562 mutex_unlock(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500563 \
564 return retval; \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700565} \
566static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500567
568INPUT_DEV_STRING_ATTR_SHOW(name);
569INPUT_DEV_STRING_ATTR_SHOW(phys);
570INPUT_DEV_STRING_ATTR_SHOW(uniq);
571
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500572static int input_print_modalias_bits(char *buf, int size,
573 char name, unsigned long *bm,
574 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100575{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500576 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100577
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500578 len += snprintf(buf, max(size, 0), "%c", name);
579 for (i = min_bit; i < max_bit; i++)
580 if (bm[LONG(i)] & BIT(i))
581 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100582 return len;
583}
584
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500585static int input_print_modalias(char *buf, int size, struct input_dev *id,
586 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100587{
588 int len;
589
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500590 len = snprintf(buf, max(size, 0),
591 "input:b%04Xv%04Xp%04Xe%04X-",
592 id->id.bustype, id->id.vendor,
593 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100594
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500595 len += input_print_modalias_bits(buf + len, size - len,
596 'e', id->evbit, 0, EV_MAX);
597 len += input_print_modalias_bits(buf + len, size - len,
598 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
599 len += input_print_modalias_bits(buf + len, size - len,
600 'r', id->relbit, 0, REL_MAX);
601 len += input_print_modalias_bits(buf + len, size - len,
602 'a', id->absbit, 0, ABS_MAX);
603 len += input_print_modalias_bits(buf + len, size - len,
604 'm', id->mscbit, 0, MSC_MAX);
605 len += input_print_modalias_bits(buf + len, size - len,
606 'l', id->ledbit, 0, LED_MAX);
607 len += input_print_modalias_bits(buf + len, size - len,
608 's', id->sndbit, 0, SND_MAX);
609 len += input_print_modalias_bits(buf + len, size - len,
610 'f', id->ffbit, 0, FF_MAX);
611 len += input_print_modalias_bits(buf + len, size - len,
612 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500613
614 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500615 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500616
Rusty Russell1d8f4302005-12-07 21:40:34 +0100617 return len;
618}
619
620static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
621{
622 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100623 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100624
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500625 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
626
Richard Purdie8a3cf452006-06-26 01:48:21 -0400627 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100628}
629static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
630
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700631static struct attribute *input_dev_attrs[] = {
632 &class_device_attr_name.attr,
633 &class_device_attr_phys.attr,
634 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100635 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700636 NULL
637};
638
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500639static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700640 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500641};
642
643#define INPUT_DEV_ID_ATTR(name) \
644static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
645{ \
646 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500647 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500648} \
649static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
650
651INPUT_DEV_ID_ATTR(bustype);
652INPUT_DEV_ID_ATTR(vendor);
653INPUT_DEV_ID_ATTR(product);
654INPUT_DEV_ID_ATTR(version);
655
656static struct attribute *input_dev_id_attrs[] = {
657 &class_device_attr_bustype.attr,
658 &class_device_attr_vendor.attr,
659 &class_device_attr_product.attr,
660 &class_device_attr_version.attr,
661 NULL
662};
663
664static struct attribute_group input_dev_id_attr_group = {
665 .name = "id",
666 .attrs = input_dev_id_attrs,
667};
668
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500669static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
670 int max, int add_cr)
671{
672 int i;
673 int len = 0;
674
675 for (i = NBITS(max) - 1; i > 0; i--)
676 if (bitmap[i])
677 break;
678
679 for (; i >= 0; i--)
680 len += snprintf(buf + len, max(buf_size - len, 0),
681 "%lx%s", bitmap[i], i > 0 ? " " : "");
682
683 if (add_cr)
684 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
685
686 return len;
687}
688
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500689#define INPUT_DEV_CAP_ATTR(ev, bm) \
690static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
691{ \
692 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500693 int len = input_print_bitmap(buf, PAGE_SIZE, \
694 input_dev->bm##bit, ev##_MAX, 1); \
695 return min_t(int, len, PAGE_SIZE); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500696} \
697static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
698
699INPUT_DEV_CAP_ATTR(EV, ev);
700INPUT_DEV_CAP_ATTR(KEY, key);
701INPUT_DEV_CAP_ATTR(REL, rel);
702INPUT_DEV_CAP_ATTR(ABS, abs);
703INPUT_DEV_CAP_ATTR(MSC, msc);
704INPUT_DEV_CAP_ATTR(LED, led);
705INPUT_DEV_CAP_ATTR(SND, snd);
706INPUT_DEV_CAP_ATTR(FF, ff);
707INPUT_DEV_CAP_ATTR(SW, sw);
708
709static struct attribute *input_dev_caps_attrs[] = {
710 &class_device_attr_ev.attr,
711 &class_device_attr_key.attr,
712 &class_device_attr_rel.attr,
713 &class_device_attr_abs.attr,
714 &class_device_attr_msc.attr,
715 &class_device_attr_led.attr,
716 &class_device_attr_snd.attr,
717 &class_device_attr_ff.attr,
718 &class_device_attr_sw.attr,
719 NULL
720};
721
722static struct attribute_group input_dev_caps_attr_group = {
723 .name = "capabilities",
724 .attrs = input_dev_caps_attrs,
725};
726
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500727static void input_dev_release(struct class_device *class_dev)
728{
729 struct input_dev *dev = to_input_dev(class_dev);
730
731 kfree(dev);
732 module_put(THIS_MODULE);
733}
734
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500735/*
Kay Sievers312c0042005-11-16 09:00:00 +0100736 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500737 * device bitfields.
738 */
Kay Sievers312c0042005-11-16 09:00:00 +0100739static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500740 char *buffer, int buffer_size, int *cur_len,
741 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500742{
743 if (*cur_index >= num_envp - 1)
744 return -ENOMEM;
745
746 envp[*cur_index] = buffer + *cur_len;
747
748 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500749 if (*cur_len >= buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500750 return -ENOMEM;
751
752 *cur_len += input_print_bitmap(buffer + *cur_len,
753 max(buffer_size - *cur_len, 0),
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500754 bitmap, max, 0) + 1;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500755 if (*cur_len > buffer_size)
756 return -ENOMEM;
757
758 (*cur_index)++;
759 return 0;
760}
761
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500762static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
763 char *buffer, int buffer_size, int *cur_len,
764 struct input_dev *dev)
765{
766 if (*cur_index >= num_envp - 1)
767 return -ENOMEM;
768
769 envp[*cur_index] = buffer + *cur_len;
770
771 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
772 "MODALIAS=");
773 if (*cur_len >= buffer_size)
774 return -ENOMEM;
775
776 *cur_len += input_print_modalias(buffer + *cur_len,
777 max(buffer_size - *cur_len, 0),
778 dev, 0) + 1;
779 if (*cur_len > buffer_size)
780 return -ENOMEM;
781
782 (*cur_index)++;
783 return 0;
784}
785
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500786#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
787 do { \
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500788 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500789 buffer, buffer_size, &len, \
790 fmt, val); \
791 if (err) \
792 return err; \
793 } while (0)
794
795#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
796 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100797 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500798 buffer, buffer_size, &len, \
799 name, bm, max); \
800 if (err) \
801 return err; \
802 } while (0)
803
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500804#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
805 do { \
806 int err = input_add_uevent_modalias_var(envp, \
807 num_envp, &i, \
808 buffer, buffer_size, &len, \
809 dev); \
810 if (err) \
811 return err; \
812 } while (0)
813
Kay Sievers312c0042005-11-16 09:00:00 +0100814static int input_dev_uevent(struct class_device *cdev, char **envp,
815 int num_envp, char *buffer, int buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500816{
817 struct input_dev *dev = to_input_dev(cdev);
818 int i = 0;
819 int len = 0;
820
821 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
822 dev->id.bustype, dev->id.vendor,
823 dev->id.product, dev->id.version);
824 if (dev->name)
825 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
826 if (dev->phys)
827 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800828 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500829 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
830
831 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
832 if (test_bit(EV_KEY, dev->evbit))
833 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
834 if (test_bit(EV_REL, dev->evbit))
835 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
836 if (test_bit(EV_ABS, dev->evbit))
837 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
838 if (test_bit(EV_MSC, dev->evbit))
839 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
840 if (test_bit(EV_LED, dev->evbit))
841 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
842 if (test_bit(EV_SND, dev->evbit))
843 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
844 if (test_bit(EV_FF, dev->evbit))
845 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
846 if (test_bit(EV_SW, dev->evbit))
847 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
848
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500849 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500850
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100851 envp[i] = NULL;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500852 return 0;
853}
854
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700855struct class input_class = {
856 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500857 .release = input_dev_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100858 .uevent = input_dev_uevent,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500859};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400860EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500861
862struct input_dev *input_allocate_device(void)
863{
864 struct input_dev *dev;
865
866 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
867 if (dev) {
868 dev->dynalloc = 1;
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700869 dev->cdev.class = &input_class;
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500870 class_device_initialize(&dev->cdev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400871 mutex_init(&dev->mutex);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500872 INIT_LIST_HEAD(&dev->h_list);
873 INIT_LIST_HEAD(&dev->node);
874 }
875
876 return dev;
877}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400878EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500879
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400880void input_free_device(struct input_dev *dev)
881{
882 if (dev) {
883
884 mutex_lock(&dev->mutex);
885 dev->name = dev->phys = dev->uniq = NULL;
886 mutex_unlock(&dev->mutex);
887
888 input_put_device(dev);
889 }
890}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400891EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400892
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500893int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500894{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500895 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500896 struct input_handle *handle;
897 struct input_handler *handler;
898 struct input_device_id *id;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500899 const char *path;
900 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500901
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500902 if (!dev->dynalloc) {
903 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
904 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
905 dev->name ? dev->name : "<Unknown>");
906 return -EINVAL;
907 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500908
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500909 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500910
911 /*
912 * If delay and period are pre-set by the driver, then autorepeating
913 * is handled by the driver itself and we don't do it in input.c.
914 */
915
916 init_timer(&dev->timer);
917 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
918 dev->timer.data = (long) dev;
919 dev->timer.function = input_repeat_key;
920 dev->rep[REP_DELAY] = 250;
921 dev->rep[REP_PERIOD] = 33;
922 }
923
924 INIT_LIST_HEAD(&dev->h_list);
925 list_add_tail(&dev->node, &input_dev_list);
926
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500927 dev->cdev.class = &input_class;
928 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
929 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
930
931 error = class_device_add(&dev->cdev);
932 if (error)
933 return error;
934
935 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
936 if (error)
937 goto fail1;
938
939 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
940 if (error)
941 goto fail2;
942
943 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
944 if (error)
945 goto fail3;
946
947 __module_get(THIS_MODULE);
948
949 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
950 printk(KERN_INFO "input: %s as %s\n",
951 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
952 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -0700953
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500954 list_for_each_entry(handler, &input_handler_list, node)
955 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
956 if ((id = input_match_device(handler->id_table, dev)))
957 if ((handle = handler->connect(handler, dev, id)))
958 input_link_handle(handle);
959
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500960 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500961
962 return 0;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500963
964 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
965 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
966 fail1: class_device_del(&dev->cdev);
967 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500968}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400969EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500970
971void input_unregister_device(struct input_dev *dev)
972{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400973 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500974
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400975 if (!dev)
976 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500977
978 del_timer_sync(&dev->timer);
979
980 list_for_each_safe(node, next, &dev->h_list) {
981 struct input_handle * handle = to_handle(node);
982 list_del_init(&handle->d_node);
983 list_del_init(&handle->h_node);
984 handle->handler->disconnect(handle);
985 }
986
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500987 list_del_init(&dev->node);
988
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500989 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
990 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500991 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500992
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400993 mutex_lock(&dev->mutex);
994 dev->name = dev->phys = dev->uniq = NULL;
995 mutex_unlock(&dev->mutex);
996
Dmitry Torokhove7374e42006-06-27 08:30:31 -0400997 class_device_unregister(&dev->cdev);
998
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500999 input_wakeup_procfs_readers();
1000}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001001EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001002
1003void input_register_handler(struct input_handler *handler)
1004{
1005 struct input_dev *dev;
1006 struct input_handle *handle;
1007 struct input_device_id *id;
1008
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001009 if (!handler)
1010 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001011
1012 INIT_LIST_HEAD(&handler->h_list);
1013
1014 if (handler->fops != NULL)
1015 input_table[handler->minor >> 5] = handler;
1016
1017 list_add_tail(&handler->node, &input_handler_list);
1018
1019 list_for_each_entry(dev, &input_dev_list, node)
1020 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1021 if ((id = input_match_device(handler->id_table, dev)))
1022 if ((handle = handler->connect(handler, dev, id)))
1023 input_link_handle(handle);
1024
1025 input_wakeup_procfs_readers();
1026}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001027EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001028
1029void input_unregister_handler(struct input_handler *handler)
1030{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001031 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001032
1033 list_for_each_safe(node, next, &handler->h_list) {
1034 struct input_handle * handle = to_handle_h(node);
1035 list_del_init(&handle->h_node);
1036 list_del_init(&handle->d_node);
1037 handler->disconnect(handle);
1038 }
1039
1040 list_del_init(&handler->node);
1041
1042 if (handler->fops != NULL)
1043 input_table[handler->minor >> 5] = NULL;
1044
1045 input_wakeup_procfs_readers();
1046}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001047EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001048
1049static int input_open_file(struct inode *inode, struct file *file)
1050{
1051 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001052 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001053 int err;
1054
1055 /* No load-on-demand here? */
1056 if (!handler || !(new_fops = fops_get(handler->fops)))
1057 return -ENODEV;
1058
1059 /*
1060 * That's _really_ odd. Usually NULL ->open means "nothing special",
1061 * not "no device". Oh, well...
1062 */
1063 if (!new_fops->open) {
1064 fops_put(new_fops);
1065 return -ENODEV;
1066 }
1067 old_fops = file->f_op;
1068 file->f_op = new_fops;
1069
1070 err = new_fops->open(inode, file);
1071
1072 if (err) {
1073 fops_put(file->f_op);
1074 file->f_op = fops_get(old_fops);
1075 }
1076 fops_put(old_fops);
1077 return err;
1078}
1079
1080static struct file_operations input_fops = {
1081 .owner = THIS_MODULE,
1082 .open = input_open_file,
1083};
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085static int __init input_init(void)
1086{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001087 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001089 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001090 if (err) {
1091 printk(KERN_ERR "input: unable to register input_dev class\n");
1092 return err;
1093 }
1094
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001095 err = input_proc_init();
1096 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001097 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001098
1099 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1100 if (err) {
1101 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001102 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001104
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001105 return 0;
1106
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001107 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001108 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112static void __exit input_exit(void)
1113{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001114 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001116 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
1119subsys_initcall(input_init);
1120module_exit(input_exit);