blob: 7570a3f52f18fd98f7c86d129628406012404d34 [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:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040067 if (dev->event)
68 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 break;
70
71 case SYN_REPORT:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040072 if (dev->sync)
73 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 dev->sync = 1;
75 break;
76 }
77 break;
78
79 case EV_KEY:
80
81 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
82 return;
83
84 if (value == 2)
85 break;
86
87 change_bit(code, dev->key);
88
89 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
90 dev->repeat_key = code;
91 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
92 }
93
94 break;
95
Richard Purdie31581062005-09-06 15:19:06 -070096 case EV_SW:
97
98 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
99 return;
100
101 change_bit(code, dev->sw);
102
103 break;
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 case EV_ABS:
106
107 if (code > ABS_MAX || !test_bit(code, dev->absbit))
108 return;
109
110 if (dev->absfuzz[code]) {
111 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
112 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
113 return;
114
115 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
116 (value < dev->abs[code] + dev->absfuzz[code]))
117 value = (dev->abs[code] * 3 + value) >> 2;
118
119 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
120 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
121 value = (dev->abs[code] + value) >> 1;
122 }
123
124 if (dev->abs[code] == value)
125 return;
126
127 dev->abs[code] = value;
128 break;
129
130 case EV_REL:
131
132 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
133 return;
134
135 break;
136
137 case EV_MSC:
138
139 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
140 return;
141
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400142 if (dev->event)
143 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 break;
146
147 case EV_LED:
148
149 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
150 return;
151
152 change_bit(code, dev->led);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400153
154 if (dev->event)
155 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 break;
158
159 case EV_SND:
160
161 if (code > SND_MAX || !test_bit(code, dev->sndbit))
162 return;
163
Dmitry Torokhov8fdc1942006-04-29 01:13:48 -0400164 if (!!test_bit(code, dev->snd) != !!value)
165 change_bit(code, dev->snd);
166
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400167 if (dev->event)
168 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 break;
171
172 case EV_REP:
173
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400174 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 dev->rep[code] = value;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400178 if (dev->event)
179 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 break;
182
183 case EV_FF:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400184 if (dev->event)
185 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187 }
188
189 if (type != EV_SYN)
190 dev->sync = 0;
191
192 if (dev->grab)
193 dev->grab->handler->event(dev->grab, type, code, value);
194 else
195 list_for_each_entry(handle, &dev->h_list, d_node)
196 if (handle->open)
197 handle->handler->event(handle, type, code, value);
198}
199
200static void input_repeat_key(unsigned long data)
201{
202 struct input_dev *dev = (void *) data;
203
204 if (!test_bit(dev->repeat_key, dev->key))
205 return;
206
207 input_event(dev, EV_KEY, dev->repeat_key, 2);
208 input_sync(dev);
209
210 if (dev->rep[REP_PERIOD])
211 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
212}
213
214int input_accept_process(struct input_handle *handle, struct file *file)
215{
216 if (handle->dev->accept)
217 return handle->dev->accept(handle->dev, file);
218
219 return 0;
220}
221
222int input_grab_device(struct input_handle *handle)
223{
224 if (handle->dev->grab)
225 return -EBUSY;
226
227 handle->dev->grab = handle;
228 return 0;
229}
230
231void input_release_device(struct input_handle *handle)
232{
233 if (handle->dev->grab == handle)
234 handle->dev->grab = NULL;
235}
236
237int input_open_device(struct input_handle *handle)
238{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500239 struct input_dev *dev = handle->dev;
240 int err;
241
Jes Sorensene676c232006-02-19 00:21:46 -0500242 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500243 if (err)
244 return err;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500247
248 if (!dev->users++ && dev->open)
249 err = dev->open(dev);
250
251 if (err)
252 handle->open--;
253
Jes Sorensene676c232006-02-19 00:21:46 -0500254 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500255
256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259int input_flush_device(struct input_handle* handle, struct file* file)
260{
261 if (handle->dev->flush)
262 return handle->dev->flush(handle->dev, file);
263
264 return 0;
265}
266
267void input_close_device(struct input_handle *handle)
268{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500269 struct input_dev *dev = handle->dev;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500272
Jes Sorensene676c232006-02-19 00:21:46 -0500273 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500274
275 if (!--dev->users && dev->close)
276 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500278
Jes Sorensene676c232006-02-19 00:21:46 -0500279 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static void input_link_handle(struct input_handle *handle)
283{
284 list_add_tail(&handle->d_node, &handle->dev->h_list);
285 list_add_tail(&handle->h_node, &handle->handler->h_list);
286}
287
288#define MATCH_BIT(bit, max) \
289 for (i = 0; i < NBITS(max); i++) \
290 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
291 break; \
292 if (i != NBITS(max)) \
293 continue;
294
295static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
296{
297 int i;
298
299 for (; id->flags || id->driver_info; id++) {
300
301 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400302 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 continue;
304
305 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400306 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 continue;
308
309 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400310 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 continue;
312
313 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400314 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 continue;
316
317 MATCH_BIT(evbit, EV_MAX);
318 MATCH_BIT(keybit, KEY_MAX);
319 MATCH_BIT(relbit, REL_MAX);
320 MATCH_BIT(absbit, ABS_MAX);
321 MATCH_BIT(mscbit, MSC_MAX);
322 MATCH_BIT(ledbit, LED_MAX);
323 MATCH_BIT(sndbit, SND_MAX);
324 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500325 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return id;
328 }
329
330 return NULL;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500334
335static struct proc_dir_entry *proc_bus_input_dir;
336static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
337static int input_devices_state;
338
339static inline void input_wakeup_procfs_readers(void)
340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 input_devices_state++;
342 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500345static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500347 int state = input_devices_state;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400348
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500349 poll_wait(file, &input_devices_poll_wait, wait);
350 if (state != input_devices_state)
351 return POLLIN | POLLRDNORM;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400352
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500356static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500358 struct list_head *node;
359 loff_t i = 0;
360
361 list_for_each(node, list)
362 if (i++ == *pos)
363 return node;
364
365 return NULL;
366}
367
368static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
369{
370 if (element->next == list)
371 return NULL;
372
373 ++(*pos);
374 return element->next;
375}
376
377static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
378{
379 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
380
381 return list_get_nth_element(&input_dev_list, pos);
382}
383
384static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
385{
386 return list_get_next_element(&input_dev_list, v, pos);
387}
388
389static void input_devices_seq_stop(struct seq_file *seq, void *v)
390{
391 /* release lock here */
392}
393
394static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
395 unsigned long *bitmap, int max)
396{
397 int i;
398
399 for (i = NBITS(max) - 1; i > 0; i--)
400 if (bitmap[i])
401 break;
402
403 seq_printf(seq, "B: %s=", name);
404 for (; i >= 0; i--)
405 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
406 seq_putc(seq, '\n');
407}
408
409static int input_devices_seq_show(struct seq_file *seq, void *v)
410{
411 struct input_dev *dev = container_of(v, struct input_dev, node);
412 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct input_handle *handle;
414
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500415 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
416 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500418 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
419 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
420 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
421 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500423 list_for_each_entry(handle, &dev->h_list, d_node)
424 seq_printf(seq, "%s ", handle->name);
425 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500426
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500427 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
428 if (test_bit(EV_KEY, dev->evbit))
429 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
430 if (test_bit(EV_REL, dev->evbit))
431 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
432 if (test_bit(EV_ABS, dev->evbit))
433 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
434 if (test_bit(EV_MSC, dev->evbit))
435 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
436 if (test_bit(EV_LED, dev->evbit))
437 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
438 if (test_bit(EV_SND, dev->evbit))
439 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
440 if (test_bit(EV_FF, dev->evbit))
441 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
442 if (test_bit(EV_SW, dev->evbit))
443 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500445 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500447 kfree(path);
448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500451static struct seq_operations input_devices_seq_ops = {
452 .start = input_devices_seq_start,
453 .next = input_devices_seq_next,
454 .stop = input_devices_seq_stop,
455 .show = input_devices_seq_show,
456};
457
458static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500460 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500463static struct file_operations input_devices_fileops = {
464 .owner = THIS_MODULE,
465 .open = input_proc_devices_open,
466 .poll = input_proc_devices_poll,
467 .read = seq_read,
468 .llseek = seq_lseek,
469 .release = seq_release,
470};
471
472static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
473{
474 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
475 seq->private = (void *)(unsigned long)*pos;
476 return list_get_nth_element(&input_handler_list, pos);
477}
478
479static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
480{
481 seq->private = (void *)(unsigned long)(*pos + 1);
482 return list_get_next_element(&input_handler_list, v, pos);
483}
484
485static void input_handlers_seq_stop(struct seq_file *seq, void *v)
486{
487 /* release lock here */
488}
489
490static int input_handlers_seq_show(struct seq_file *seq, void *v)
491{
492 struct input_handler *handler = container_of(v, struct input_handler, node);
493
494 seq_printf(seq, "N: Number=%ld Name=%s",
495 (unsigned long)seq->private, handler->name);
496 if (handler->fops)
497 seq_printf(seq, " Minor=%d", handler->minor);
498 seq_putc(seq, '\n');
499
500 return 0;
501}
502static struct seq_operations input_handlers_seq_ops = {
503 .start = input_handlers_seq_start,
504 .next = input_handlers_seq_next,
505 .stop = input_handlers_seq_stop,
506 .show = input_handlers_seq_show,
507};
508
509static int input_proc_handlers_open(struct inode *inode, struct file *file)
510{
511 return seq_open(file, &input_handlers_seq_ops);
512}
513
514static struct file_operations input_handlers_fileops = {
515 .owner = THIS_MODULE,
516 .open = input_proc_handlers_open,
517 .read = seq_read,
518 .llseek = seq_lseek,
519 .release = seq_release,
520};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522static int __init input_proc_init(void)
523{
524 struct proc_dir_entry *entry;
525
526 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500527 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500531
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500532 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500533 if (!entry)
534 goto fail1;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500537 entry->proc_fops = &input_devices_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500538
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500539 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500540 if (!entry)
541 goto fail2;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500544 entry->proc_fops = &input_handlers_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500547
548 fail2: remove_proc_entry("devices", proc_bus_input_dir);
549 fail1: remove_proc_entry("input", proc_bus);
550 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500552
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500553static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500554{
555 remove_proc_entry("devices", proc_bus_input_dir);
556 remove_proc_entry("handlers", proc_bus_input_dir);
557 remove_proc_entry("input", proc_bus);
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500561static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500563static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#endif
565
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500566#define INPUT_DEV_STRING_ATTR_SHOW(name) \
567static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
568{ \
569 struct input_dev *input_dev = to_input_dev(dev); \
570 int retval; \
571 \
Jes Sorensene676c232006-02-19 00:21:46 -0500572 retval = mutex_lock_interruptible(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500573 if (retval) \
574 return retval; \
575 \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500576 retval = scnprintf(buf, PAGE_SIZE, \
577 "%s\n", input_dev->name ? input_dev->name : ""); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500578 \
Jes Sorensene676c232006-02-19 00:21:46 -0500579 mutex_unlock(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500580 \
581 return retval; \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700582} \
583static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500584
585INPUT_DEV_STRING_ATTR_SHOW(name);
586INPUT_DEV_STRING_ATTR_SHOW(phys);
587INPUT_DEV_STRING_ATTR_SHOW(uniq);
588
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500589static int input_print_modalias_bits(char *buf, int size,
590 char name, unsigned long *bm,
591 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100592{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500593 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100594
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500595 len += snprintf(buf, max(size, 0), "%c", name);
596 for (i = min_bit; i < max_bit; i++)
597 if (bm[LONG(i)] & BIT(i))
598 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100599 return len;
600}
601
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500602static int input_print_modalias(char *buf, int size, struct input_dev *id,
603 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100604{
605 int len;
606
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500607 len = snprintf(buf, max(size, 0),
608 "input:b%04Xv%04Xp%04Xe%04X-",
609 id->id.bustype, id->id.vendor,
610 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100611
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500612 len += input_print_modalias_bits(buf + len, size - len,
613 'e', id->evbit, 0, EV_MAX);
614 len += input_print_modalias_bits(buf + len, size - len,
615 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
616 len += input_print_modalias_bits(buf + len, size - len,
617 'r', id->relbit, 0, REL_MAX);
618 len += input_print_modalias_bits(buf + len, size - len,
619 'a', id->absbit, 0, ABS_MAX);
620 len += input_print_modalias_bits(buf + len, size - len,
621 'm', id->mscbit, 0, MSC_MAX);
622 len += input_print_modalias_bits(buf + len, size - len,
623 'l', id->ledbit, 0, LED_MAX);
624 len += input_print_modalias_bits(buf + len, size - len,
625 's', id->sndbit, 0, SND_MAX);
626 len += input_print_modalias_bits(buf + len, size - len,
627 'f', id->ffbit, 0, FF_MAX);
628 len += input_print_modalias_bits(buf + len, size - len,
629 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500630
631 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500632 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500633
Rusty Russell1d8f4302005-12-07 21:40:34 +0100634 return len;
635}
636
637static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
638{
639 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100640 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100641
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500642 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
643
Richard Purdie8a3cf452006-06-26 01:48:21 -0400644 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100645}
646static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
647
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700648static struct attribute *input_dev_attrs[] = {
649 &class_device_attr_name.attr,
650 &class_device_attr_phys.attr,
651 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100652 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700653 NULL
654};
655
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500656static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700657 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500658};
659
660#define INPUT_DEV_ID_ATTR(name) \
661static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
662{ \
663 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500664 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500665} \
666static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
667
668INPUT_DEV_ID_ATTR(bustype);
669INPUT_DEV_ID_ATTR(vendor);
670INPUT_DEV_ID_ATTR(product);
671INPUT_DEV_ID_ATTR(version);
672
673static struct attribute *input_dev_id_attrs[] = {
674 &class_device_attr_bustype.attr,
675 &class_device_attr_vendor.attr,
676 &class_device_attr_product.attr,
677 &class_device_attr_version.attr,
678 NULL
679};
680
681static struct attribute_group input_dev_id_attr_group = {
682 .name = "id",
683 .attrs = input_dev_id_attrs,
684};
685
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500686static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
687 int max, int add_cr)
688{
689 int i;
690 int len = 0;
691
692 for (i = NBITS(max) - 1; i > 0; i--)
693 if (bitmap[i])
694 break;
695
696 for (; i >= 0; i--)
697 len += snprintf(buf + len, max(buf_size - len, 0),
698 "%lx%s", bitmap[i], i > 0 ? " " : "");
699
700 if (add_cr)
701 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
702
703 return len;
704}
705
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500706#define INPUT_DEV_CAP_ATTR(ev, bm) \
707static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
708{ \
709 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500710 int len = input_print_bitmap(buf, PAGE_SIZE, \
711 input_dev->bm##bit, ev##_MAX, 1); \
712 return min_t(int, len, PAGE_SIZE); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500713} \
714static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
715
716INPUT_DEV_CAP_ATTR(EV, ev);
717INPUT_DEV_CAP_ATTR(KEY, key);
718INPUT_DEV_CAP_ATTR(REL, rel);
719INPUT_DEV_CAP_ATTR(ABS, abs);
720INPUT_DEV_CAP_ATTR(MSC, msc);
721INPUT_DEV_CAP_ATTR(LED, led);
722INPUT_DEV_CAP_ATTR(SND, snd);
723INPUT_DEV_CAP_ATTR(FF, ff);
724INPUT_DEV_CAP_ATTR(SW, sw);
725
726static struct attribute *input_dev_caps_attrs[] = {
727 &class_device_attr_ev.attr,
728 &class_device_attr_key.attr,
729 &class_device_attr_rel.attr,
730 &class_device_attr_abs.attr,
731 &class_device_attr_msc.attr,
732 &class_device_attr_led.attr,
733 &class_device_attr_snd.attr,
734 &class_device_attr_ff.attr,
735 &class_device_attr_sw.attr,
736 NULL
737};
738
739static struct attribute_group input_dev_caps_attr_group = {
740 .name = "capabilities",
741 .attrs = input_dev_caps_attrs,
742};
743
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500744static void input_dev_release(struct class_device *class_dev)
745{
746 struct input_dev *dev = to_input_dev(class_dev);
747
748 kfree(dev);
749 module_put(THIS_MODULE);
750}
751
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500752/*
Kay Sievers312c0042005-11-16 09:00:00 +0100753 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500754 * device bitfields.
755 */
Kay Sievers312c0042005-11-16 09:00:00 +0100756static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500757 char *buffer, int buffer_size, int *cur_len,
758 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500759{
760 if (*cur_index >= num_envp - 1)
761 return -ENOMEM;
762
763 envp[*cur_index] = buffer + *cur_len;
764
765 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500766 if (*cur_len >= buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500767 return -ENOMEM;
768
769 *cur_len += input_print_bitmap(buffer + *cur_len,
770 max(buffer_size - *cur_len, 0),
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500771 bitmap, max, 0) + 1;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500772 if (*cur_len > buffer_size)
773 return -ENOMEM;
774
775 (*cur_index)++;
776 return 0;
777}
778
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500779static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
780 char *buffer, int buffer_size, int *cur_len,
781 struct input_dev *dev)
782{
783 if (*cur_index >= num_envp - 1)
784 return -ENOMEM;
785
786 envp[*cur_index] = buffer + *cur_len;
787
788 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
789 "MODALIAS=");
790 if (*cur_len >= buffer_size)
791 return -ENOMEM;
792
793 *cur_len += input_print_modalias(buffer + *cur_len,
794 max(buffer_size - *cur_len, 0),
795 dev, 0) + 1;
796 if (*cur_len > buffer_size)
797 return -ENOMEM;
798
799 (*cur_index)++;
800 return 0;
801}
802
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500803#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
804 do { \
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500805 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500806 buffer, buffer_size, &len, \
807 fmt, val); \
808 if (err) \
809 return err; \
810 } while (0)
811
812#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
813 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100814 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500815 buffer, buffer_size, &len, \
816 name, bm, max); \
817 if (err) \
818 return err; \
819 } while (0)
820
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500821#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
822 do { \
823 int err = input_add_uevent_modalias_var(envp, \
824 num_envp, &i, \
825 buffer, buffer_size, &len, \
826 dev); \
827 if (err) \
828 return err; \
829 } while (0)
830
Kay Sievers312c0042005-11-16 09:00:00 +0100831static int input_dev_uevent(struct class_device *cdev, char **envp,
832 int num_envp, char *buffer, int buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500833{
834 struct input_dev *dev = to_input_dev(cdev);
835 int i = 0;
836 int len = 0;
837
838 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
839 dev->id.bustype, dev->id.vendor,
840 dev->id.product, dev->id.version);
841 if (dev->name)
842 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
843 if (dev->phys)
844 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800845 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500846 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
847
848 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
849 if (test_bit(EV_KEY, dev->evbit))
850 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
851 if (test_bit(EV_REL, dev->evbit))
852 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
853 if (test_bit(EV_ABS, dev->evbit))
854 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
855 if (test_bit(EV_MSC, dev->evbit))
856 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
857 if (test_bit(EV_LED, dev->evbit))
858 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
859 if (test_bit(EV_SND, dev->evbit))
860 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
861 if (test_bit(EV_FF, dev->evbit))
862 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
863 if (test_bit(EV_SW, dev->evbit))
864 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
865
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500866 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500867
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100868 envp[i] = NULL;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500869 return 0;
870}
871
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700872struct class input_class = {
873 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500874 .release = input_dev_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100875 .uevent = input_dev_uevent,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500876};
877
878struct input_dev *input_allocate_device(void)
879{
880 struct input_dev *dev;
881
882 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
883 if (dev) {
884 dev->dynalloc = 1;
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700885 dev->cdev.class = &input_class;
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500886 class_device_initialize(&dev->cdev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400887 mutex_init(&dev->mutex);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500888 INIT_LIST_HEAD(&dev->h_list);
889 INIT_LIST_HEAD(&dev->node);
890 }
891
892 return dev;
893}
894
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400895void input_free_device(struct input_dev *dev)
896{
897 if (dev) {
898
899 mutex_lock(&dev->mutex);
900 dev->name = dev->phys = dev->uniq = NULL;
901 mutex_unlock(&dev->mutex);
902
903 input_put_device(dev);
904 }
905}
906
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500907int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500908{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500909 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500910 struct input_handle *handle;
911 struct input_handler *handler;
912 struct input_device_id *id;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500913 const char *path;
914 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500915
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500916 if (!dev->dynalloc) {
917 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
918 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
919 dev->name ? dev->name : "<Unknown>");
920 return -EINVAL;
921 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500922
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500923 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500924
925 /*
926 * If delay and period are pre-set by the driver, then autorepeating
927 * is handled by the driver itself and we don't do it in input.c.
928 */
929
930 init_timer(&dev->timer);
931 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
932 dev->timer.data = (long) dev;
933 dev->timer.function = input_repeat_key;
934 dev->rep[REP_DELAY] = 250;
935 dev->rep[REP_PERIOD] = 33;
936 }
937
938 INIT_LIST_HEAD(&dev->h_list);
939 list_add_tail(&dev->node, &input_dev_list);
940
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500941 dev->cdev.class = &input_class;
942 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
943 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
944
945 error = class_device_add(&dev->cdev);
946 if (error)
947 return error;
948
949 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
950 if (error)
951 goto fail1;
952
953 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
954 if (error)
955 goto fail2;
956
957 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
958 if (error)
959 goto fail3;
960
961 __module_get(THIS_MODULE);
962
963 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
964 printk(KERN_INFO "input: %s as %s\n",
965 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
966 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -0700967
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500968 list_for_each_entry(handler, &input_handler_list, node)
969 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
970 if ((id = input_match_device(handler->id_table, dev)))
971 if ((handle = handler->connect(handler, dev, id)))
972 input_link_handle(handle);
973
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500974 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500975
976 return 0;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500977
978 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
979 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
980 fail1: class_device_del(&dev->cdev);
981 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500982}
983
984void input_unregister_device(struct input_dev *dev)
985{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400986 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500987
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400988 if (!dev)
989 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500990
991 del_timer_sync(&dev->timer);
992
993 list_for_each_safe(node, next, &dev->h_list) {
994 struct input_handle * handle = to_handle(node);
995 list_del_init(&handle->d_node);
996 list_del_init(&handle->h_node);
997 handle->handler->disconnect(handle);
998 }
999
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001000 list_del_init(&dev->node);
1001
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001002 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
1003 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001004 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001005 class_device_unregister(&dev->cdev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001006
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001007 mutex_lock(&dev->mutex);
1008 dev->name = dev->phys = dev->uniq = NULL;
1009 mutex_unlock(&dev->mutex);
1010
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001011 input_wakeup_procfs_readers();
1012}
1013
1014void input_register_handler(struct input_handler *handler)
1015{
1016 struct input_dev *dev;
1017 struct input_handle *handle;
1018 struct input_device_id *id;
1019
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001020 if (!handler)
1021 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001022
1023 INIT_LIST_HEAD(&handler->h_list);
1024
1025 if (handler->fops != NULL)
1026 input_table[handler->minor >> 5] = handler;
1027
1028 list_add_tail(&handler->node, &input_handler_list);
1029
1030 list_for_each_entry(dev, &input_dev_list, node)
1031 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1032 if ((id = input_match_device(handler->id_table, dev)))
1033 if ((handle = handler->connect(handler, dev, id)))
1034 input_link_handle(handle);
1035
1036 input_wakeup_procfs_readers();
1037}
1038
1039void input_unregister_handler(struct input_handler *handler)
1040{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001041 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001042
1043 list_for_each_safe(node, next, &handler->h_list) {
1044 struct input_handle * handle = to_handle_h(node);
1045 list_del_init(&handle->h_node);
1046 list_del_init(&handle->d_node);
1047 handler->disconnect(handle);
1048 }
1049
1050 list_del_init(&handler->node);
1051
1052 if (handler->fops != NULL)
1053 input_table[handler->minor >> 5] = NULL;
1054
1055 input_wakeup_procfs_readers();
1056}
1057
1058static int input_open_file(struct inode *inode, struct file *file)
1059{
1060 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001061 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001062 int err;
1063
1064 /* No load-on-demand here? */
1065 if (!handler || !(new_fops = fops_get(handler->fops)))
1066 return -ENODEV;
1067
1068 /*
1069 * That's _really_ odd. Usually NULL ->open means "nothing special",
1070 * not "no device". Oh, well...
1071 */
1072 if (!new_fops->open) {
1073 fops_put(new_fops);
1074 return -ENODEV;
1075 }
1076 old_fops = file->f_op;
1077 file->f_op = new_fops;
1078
1079 err = new_fops->open(inode, file);
1080
1081 if (err) {
1082 fops_put(file->f_op);
1083 file->f_op = fops_get(old_fops);
1084 }
1085 fops_put(old_fops);
1086 return err;
1087}
1088
1089static struct file_operations input_fops = {
1090 .owner = THIS_MODULE,
1091 .open = input_open_file,
1092};
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094static int __init input_init(void)
1095{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001096 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001098 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001099 if (err) {
1100 printk(KERN_ERR "input: unable to register input_dev class\n");
1101 return err;
1102 }
1103
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001104 err = input_proc_init();
1105 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001106 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001107
1108 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1109 if (err) {
1110 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001111 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001113
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001114 return 0;
1115
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001116 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001117 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001118 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121static void __exit input_exit(void)
1122{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001123 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001125 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128subsys_initcall(input_init);
1129module_exit(input_exit);