blob: e20913942927446ef857c238c7709837ffcfb02e [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
Dmitry Torokhov0e739d22006-07-06 00:22:43 -040038/**
39 * input_event() - report new input event
40 * @handle: device that generated the event
41 * @type: type of the event
42 * @code: event code
43 * @value: value of the event
44 *
45 * This function should be used by drivers implementing various input devices
46 * See also input_inject_event()
47 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
49{
50 struct input_handle *handle;
51
52 if (type > EV_MAX || !test_bit(type, dev->evbit))
53 return;
54
55 add_input_randomness(type, code, value);
56
57 switch (type) {
58
59 case EV_SYN:
60 switch (code) {
61 case SYN_CONFIG:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040062 if (dev->event)
63 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 break;
65
66 case SYN_REPORT:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040067 if (dev->sync)
68 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 dev->sync = 1;
70 break;
71 }
72 break;
73
74 case EV_KEY:
75
76 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
77 return;
78
79 if (value == 2)
80 break;
81
82 change_bit(code, dev->key);
83
84 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
85 dev->repeat_key = code;
86 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
87 }
88
89 break;
90
Richard Purdie31581062005-09-06 15:19:06 -070091 case EV_SW:
92
93 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
94 return;
95
96 change_bit(code, dev->sw);
97
98 break;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 case EV_ABS:
101
102 if (code > ABS_MAX || !test_bit(code, dev->absbit))
103 return;
104
105 if (dev->absfuzz[code]) {
106 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
107 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
108 return;
109
110 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
111 (value < dev->abs[code] + dev->absfuzz[code]))
112 value = (dev->abs[code] * 3 + value) >> 2;
113
114 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
115 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
116 value = (dev->abs[code] + value) >> 1;
117 }
118
119 if (dev->abs[code] == value)
120 return;
121
122 dev->abs[code] = value;
123 break;
124
125 case EV_REL:
126
127 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
128 return;
129
130 break;
131
132 case EV_MSC:
133
134 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
135 return;
136
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400137 if (dev->event)
138 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 break;
141
142 case EV_LED:
143
144 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
145 return;
146
147 change_bit(code, dev->led);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400148
149 if (dev->event)
150 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 break;
153
154 case EV_SND:
155
156 if (code > SND_MAX || !test_bit(code, dev->sndbit))
157 return;
158
Dmitry Torokhov8fdc1942006-04-29 01:13:48 -0400159 if (!!test_bit(code, dev->snd) != !!value)
160 change_bit(code, dev->snd);
161
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400162 if (dev->event)
163 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 break;
166
167 case EV_REP:
168
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400169 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 dev->rep[code] = value;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400173 if (dev->event)
174 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 break;
177
178 case EV_FF:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400179 if (dev->event)
180 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 }
183
184 if (type != EV_SYN)
185 dev->sync = 0;
186
187 if (dev->grab)
188 dev->grab->handler->event(dev->grab, type, code, value);
189 else
190 list_for_each_entry(handle, &dev->h_list, d_node)
191 if (handle->open)
192 handle->handler->event(handle, type, code, value);
193}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400194EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400196/**
197 * input_inject_event() - send input event from input handler
198 * @handle: input handle to send event through
199 * @type: type of the event
200 * @code: event code
201 * @value: value of the event
202 *
203 * Similar to input_event() but will ignore event if device is "grabbed" and handle
204 * injecting event is not the one that owns the device.
205 */
206void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
207{
208 if (!handle->dev->grab || handle->dev->grab == handle)
209 input_event(handle->dev, type, code, value);
210}
211EXPORT_SYMBOL(input_inject_event);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static void input_repeat_key(unsigned long data)
214{
215 struct input_dev *dev = (void *) data;
216
217 if (!test_bit(dev->repeat_key, dev->key))
218 return;
219
220 input_event(dev, EV_KEY, dev->repeat_key, 2);
221 input_sync(dev);
222
223 if (dev->rep[REP_PERIOD])
224 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227int input_grab_device(struct input_handle *handle)
228{
229 if (handle->dev->grab)
230 return -EBUSY;
231
232 handle->dev->grab = handle;
233 return 0;
234}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400235EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237void input_release_device(struct input_handle *handle)
238{
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400239 if (handle->dev->grab == handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 handle->dev->grab = NULL;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400241
242 list_for_each_entry(handle, &handle->dev->h_list, d_node)
243 if (handle->handler->start)
244 handle->handler->start(handle);
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400247EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249int input_open_device(struct input_handle *handle)
250{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500251 struct input_dev *dev = handle->dev;
252 int err;
253
Jes Sorensene676c232006-02-19 00:21:46 -0500254 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500255 if (err)
256 return err;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500259
260 if (!dev->users++ && dev->open)
261 err = dev->open(dev);
262
263 if (err)
264 handle->open--;
265
Jes Sorensene676c232006-02-19 00:21:46 -0500266 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500267
268 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400270EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272int input_flush_device(struct input_handle* handle, struct file* file)
273{
274 if (handle->dev->flush)
275 return handle->dev->flush(handle->dev, file);
276
277 return 0;
278}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400279EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281void input_close_device(struct input_handle *handle)
282{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500283 struct input_dev *dev = handle->dev;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500286
Jes Sorensene676c232006-02-19 00:21:46 -0500287 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500288
289 if (!--dev->users && dev->close)
290 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500292
Jes Sorensene676c232006-02-19 00:21:46 -0500293 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400295EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297static void input_link_handle(struct input_handle *handle)
298{
299 list_add_tail(&handle->d_node, &handle->dev->h_list);
300 list_add_tail(&handle->h_node, &handle->handler->h_list);
301}
302
303#define MATCH_BIT(bit, max) \
304 for (i = 0; i < NBITS(max); i++) \
305 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
306 break; \
307 if (i != NBITS(max)) \
308 continue;
309
310static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
311{
312 int i;
313
314 for (; id->flags || id->driver_info; id++) {
315
316 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400317 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 continue;
319
320 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400321 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 continue;
323
324 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400325 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 continue;
327
328 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400329 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 continue;
331
332 MATCH_BIT(evbit, EV_MAX);
333 MATCH_BIT(keybit, KEY_MAX);
334 MATCH_BIT(relbit, REL_MAX);
335 MATCH_BIT(absbit, ABS_MAX);
336 MATCH_BIT(mscbit, MSC_MAX);
337 MATCH_BIT(ledbit, LED_MAX);
338 MATCH_BIT(sndbit, SND_MAX);
339 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500340 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 return id;
343 }
344
345 return NULL;
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500349
350static struct proc_dir_entry *proc_bus_input_dir;
351static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
352static int input_devices_state;
353
354static inline void input_wakeup_procfs_readers(void)
355{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 input_devices_state++;
357 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500360static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500362 int state = input_devices_state;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400363
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500364 poll_wait(file, &input_devices_poll_wait, wait);
365 if (state != input_devices_state)
366 return POLLIN | POLLRDNORM;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400367
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500371static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500373 struct list_head *node;
374 loff_t i = 0;
375
376 list_for_each(node, list)
377 if (i++ == *pos)
378 return node;
379
380 return NULL;
381}
382
383static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
384{
385 if (element->next == list)
386 return NULL;
387
388 ++(*pos);
389 return element->next;
390}
391
392static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
393{
394 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
395
396 return list_get_nth_element(&input_dev_list, pos);
397}
398
399static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
400{
401 return list_get_next_element(&input_dev_list, v, pos);
402}
403
404static void input_devices_seq_stop(struct seq_file *seq, void *v)
405{
406 /* release lock here */
407}
408
409static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
410 unsigned long *bitmap, int max)
411{
412 int i;
413
414 for (i = NBITS(max) - 1; i > 0; i--)
415 if (bitmap[i])
416 break;
417
418 seq_printf(seq, "B: %s=", name);
419 for (; i >= 0; i--)
420 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
421 seq_putc(seq, '\n');
422}
423
424static int input_devices_seq_show(struct seq_file *seq, void *v)
425{
426 struct input_dev *dev = container_of(v, struct input_dev, node);
427 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct input_handle *handle;
429
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500430 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
431 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500433 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
434 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
435 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
436 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500438 list_for_each_entry(handle, &dev->h_list, d_node)
439 seq_printf(seq, "%s ", handle->name);
440 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500441
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500442 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
443 if (test_bit(EV_KEY, dev->evbit))
444 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
445 if (test_bit(EV_REL, dev->evbit))
446 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
447 if (test_bit(EV_ABS, dev->evbit))
448 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
449 if (test_bit(EV_MSC, dev->evbit))
450 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
451 if (test_bit(EV_LED, dev->evbit))
452 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
453 if (test_bit(EV_SND, dev->evbit))
454 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
455 if (test_bit(EV_FF, dev->evbit))
456 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
457 if (test_bit(EV_SW, dev->evbit))
458 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500460 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500462 kfree(path);
463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500466static struct seq_operations input_devices_seq_ops = {
467 .start = input_devices_seq_start,
468 .next = input_devices_seq_next,
469 .stop = input_devices_seq_stop,
470 .show = input_devices_seq_show,
471};
472
473static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500475 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500478static struct file_operations input_devices_fileops = {
479 .owner = THIS_MODULE,
480 .open = input_proc_devices_open,
481 .poll = input_proc_devices_poll,
482 .read = seq_read,
483 .llseek = seq_lseek,
484 .release = seq_release,
485};
486
487static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
488{
489 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
490 seq->private = (void *)(unsigned long)*pos;
491 return list_get_nth_element(&input_handler_list, pos);
492}
493
494static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
495{
496 seq->private = (void *)(unsigned long)(*pos + 1);
497 return list_get_next_element(&input_handler_list, v, pos);
498}
499
500static void input_handlers_seq_stop(struct seq_file *seq, void *v)
501{
502 /* release lock here */
503}
504
505static int input_handlers_seq_show(struct seq_file *seq, void *v)
506{
507 struct input_handler *handler = container_of(v, struct input_handler, node);
508
509 seq_printf(seq, "N: Number=%ld Name=%s",
510 (unsigned long)seq->private, handler->name);
511 if (handler->fops)
512 seq_printf(seq, " Minor=%d", handler->minor);
513 seq_putc(seq, '\n');
514
515 return 0;
516}
517static struct seq_operations input_handlers_seq_ops = {
518 .start = input_handlers_seq_start,
519 .next = input_handlers_seq_next,
520 .stop = input_handlers_seq_stop,
521 .show = input_handlers_seq_show,
522};
523
524static int input_proc_handlers_open(struct inode *inode, struct file *file)
525{
526 return seq_open(file, &input_handlers_seq_ops);
527}
528
529static struct file_operations input_handlers_fileops = {
530 .owner = THIS_MODULE,
531 .open = input_proc_handlers_open,
532 .read = seq_read,
533 .llseek = seq_lseek,
534 .release = seq_release,
535};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537static int __init input_proc_init(void)
538{
539 struct proc_dir_entry *entry;
540
541 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500542 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500546
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500547 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500548 if (!entry)
549 goto fail1;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500552 entry->proc_fops = &input_devices_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500553
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500554 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500555 if (!entry)
556 goto fail2;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500559 entry->proc_fops = &input_handlers_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500562
563 fail2: remove_proc_entry("devices", proc_bus_input_dir);
564 fail1: remove_proc_entry("input", proc_bus);
565 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500567
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500568static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500569{
570 remove_proc_entry("devices", proc_bus_input_dir);
571 remove_proc_entry("handlers", proc_bus_input_dir);
572 remove_proc_entry("input", proc_bus);
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500576static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500578static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif
580
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500581#define INPUT_DEV_STRING_ATTR_SHOW(name) \
582static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
583{ \
584 struct input_dev *input_dev = to_input_dev(dev); \
585 int retval; \
586 \
Jes Sorensene676c232006-02-19 00:21:46 -0500587 retval = mutex_lock_interruptible(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500588 if (retval) \
589 return retval; \
590 \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500591 retval = scnprintf(buf, PAGE_SIZE, \
592 "%s\n", input_dev->name ? input_dev->name : ""); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500593 \
Jes Sorensene676c232006-02-19 00:21:46 -0500594 mutex_unlock(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500595 \
596 return retval; \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700597} \
598static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500599
600INPUT_DEV_STRING_ATTR_SHOW(name);
601INPUT_DEV_STRING_ATTR_SHOW(phys);
602INPUT_DEV_STRING_ATTR_SHOW(uniq);
603
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500604static int input_print_modalias_bits(char *buf, int size,
605 char name, unsigned long *bm,
606 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100607{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500608 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100609
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500610 len += snprintf(buf, max(size, 0), "%c", name);
611 for (i = min_bit; i < max_bit; i++)
612 if (bm[LONG(i)] & BIT(i))
613 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100614 return len;
615}
616
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500617static int input_print_modalias(char *buf, int size, struct input_dev *id,
618 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100619{
620 int len;
621
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500622 len = snprintf(buf, max(size, 0),
623 "input:b%04Xv%04Xp%04Xe%04X-",
624 id->id.bustype, id->id.vendor,
625 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100626
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500627 len += input_print_modalias_bits(buf + len, size - len,
628 'e', id->evbit, 0, EV_MAX);
629 len += input_print_modalias_bits(buf + len, size - len,
630 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
631 len += input_print_modalias_bits(buf + len, size - len,
632 'r', id->relbit, 0, REL_MAX);
633 len += input_print_modalias_bits(buf + len, size - len,
634 'a', id->absbit, 0, ABS_MAX);
635 len += input_print_modalias_bits(buf + len, size - len,
636 'm', id->mscbit, 0, MSC_MAX);
637 len += input_print_modalias_bits(buf + len, size - len,
638 'l', id->ledbit, 0, LED_MAX);
639 len += input_print_modalias_bits(buf + len, size - len,
640 's', id->sndbit, 0, SND_MAX);
641 len += input_print_modalias_bits(buf + len, size - len,
642 'f', id->ffbit, 0, FF_MAX);
643 len += input_print_modalias_bits(buf + len, size - len,
644 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500645
646 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500647 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500648
Rusty Russell1d8f4302005-12-07 21:40:34 +0100649 return len;
650}
651
652static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
653{
654 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100655 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100656
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500657 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
658
Richard Purdie8a3cf452006-06-26 01:48:21 -0400659 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100660}
661static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
662
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700663static struct attribute *input_dev_attrs[] = {
664 &class_device_attr_name.attr,
665 &class_device_attr_phys.attr,
666 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100667 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700668 NULL
669};
670
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500671static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700672 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500673};
674
675#define INPUT_DEV_ID_ATTR(name) \
676static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
677{ \
678 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500679 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500680} \
681static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
682
683INPUT_DEV_ID_ATTR(bustype);
684INPUT_DEV_ID_ATTR(vendor);
685INPUT_DEV_ID_ATTR(product);
686INPUT_DEV_ID_ATTR(version);
687
688static struct attribute *input_dev_id_attrs[] = {
689 &class_device_attr_bustype.attr,
690 &class_device_attr_vendor.attr,
691 &class_device_attr_product.attr,
692 &class_device_attr_version.attr,
693 NULL
694};
695
696static struct attribute_group input_dev_id_attr_group = {
697 .name = "id",
698 .attrs = input_dev_id_attrs,
699};
700
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500701static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
702 int max, int add_cr)
703{
704 int i;
705 int len = 0;
706
707 for (i = NBITS(max) - 1; i > 0; i--)
708 if (bitmap[i])
709 break;
710
711 for (; i >= 0; i--)
712 len += snprintf(buf + len, max(buf_size - len, 0),
713 "%lx%s", bitmap[i], i > 0 ? " " : "");
714
715 if (add_cr)
716 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
717
718 return len;
719}
720
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500721#define INPUT_DEV_CAP_ATTR(ev, bm) \
722static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
723{ \
724 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500725 int len = input_print_bitmap(buf, PAGE_SIZE, \
726 input_dev->bm##bit, ev##_MAX, 1); \
727 return min_t(int, len, PAGE_SIZE); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500728} \
729static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
730
731INPUT_DEV_CAP_ATTR(EV, ev);
732INPUT_DEV_CAP_ATTR(KEY, key);
733INPUT_DEV_CAP_ATTR(REL, rel);
734INPUT_DEV_CAP_ATTR(ABS, abs);
735INPUT_DEV_CAP_ATTR(MSC, msc);
736INPUT_DEV_CAP_ATTR(LED, led);
737INPUT_DEV_CAP_ATTR(SND, snd);
738INPUT_DEV_CAP_ATTR(FF, ff);
739INPUT_DEV_CAP_ATTR(SW, sw);
740
741static struct attribute *input_dev_caps_attrs[] = {
742 &class_device_attr_ev.attr,
743 &class_device_attr_key.attr,
744 &class_device_attr_rel.attr,
745 &class_device_attr_abs.attr,
746 &class_device_attr_msc.attr,
747 &class_device_attr_led.attr,
748 &class_device_attr_snd.attr,
749 &class_device_attr_ff.attr,
750 &class_device_attr_sw.attr,
751 NULL
752};
753
754static struct attribute_group input_dev_caps_attr_group = {
755 .name = "capabilities",
756 .attrs = input_dev_caps_attrs,
757};
758
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500759static void input_dev_release(struct class_device *class_dev)
760{
761 struct input_dev *dev = to_input_dev(class_dev);
762
763 kfree(dev);
764 module_put(THIS_MODULE);
765}
766
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500767/*
Kay Sievers312c0042005-11-16 09:00:00 +0100768 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500769 * device bitfields.
770 */
Kay Sievers312c0042005-11-16 09:00:00 +0100771static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500772 char *buffer, int buffer_size, int *cur_len,
773 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500774{
775 if (*cur_index >= num_envp - 1)
776 return -ENOMEM;
777
778 envp[*cur_index] = buffer + *cur_len;
779
780 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500781 if (*cur_len >= buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500782 return -ENOMEM;
783
784 *cur_len += input_print_bitmap(buffer + *cur_len,
785 max(buffer_size - *cur_len, 0),
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500786 bitmap, max, 0) + 1;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500787 if (*cur_len > buffer_size)
788 return -ENOMEM;
789
790 (*cur_index)++;
791 return 0;
792}
793
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500794static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
795 char *buffer, int buffer_size, int *cur_len,
796 struct input_dev *dev)
797{
798 if (*cur_index >= num_envp - 1)
799 return -ENOMEM;
800
801 envp[*cur_index] = buffer + *cur_len;
802
803 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
804 "MODALIAS=");
805 if (*cur_len >= buffer_size)
806 return -ENOMEM;
807
808 *cur_len += input_print_modalias(buffer + *cur_len,
809 max(buffer_size - *cur_len, 0),
810 dev, 0) + 1;
811 if (*cur_len > buffer_size)
812 return -ENOMEM;
813
814 (*cur_index)++;
815 return 0;
816}
817
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500818#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
819 do { \
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500820 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500821 buffer, buffer_size, &len, \
822 fmt, val); \
823 if (err) \
824 return err; \
825 } while (0)
826
827#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
828 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100829 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500830 buffer, buffer_size, &len, \
831 name, bm, max); \
832 if (err) \
833 return err; \
834 } while (0)
835
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500836#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
837 do { \
838 int err = input_add_uevent_modalias_var(envp, \
839 num_envp, &i, \
840 buffer, buffer_size, &len, \
841 dev); \
842 if (err) \
843 return err; \
844 } while (0)
845
Kay Sievers312c0042005-11-16 09:00:00 +0100846static int input_dev_uevent(struct class_device *cdev, char **envp,
847 int num_envp, char *buffer, int buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500848{
849 struct input_dev *dev = to_input_dev(cdev);
850 int i = 0;
851 int len = 0;
852
853 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
854 dev->id.bustype, dev->id.vendor,
855 dev->id.product, dev->id.version);
856 if (dev->name)
857 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
858 if (dev->phys)
859 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800860 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500861 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
862
863 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
864 if (test_bit(EV_KEY, dev->evbit))
865 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
866 if (test_bit(EV_REL, dev->evbit))
867 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
868 if (test_bit(EV_ABS, dev->evbit))
869 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
870 if (test_bit(EV_MSC, dev->evbit))
871 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
872 if (test_bit(EV_LED, dev->evbit))
873 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
874 if (test_bit(EV_SND, dev->evbit))
875 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
876 if (test_bit(EV_FF, dev->evbit))
877 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
878 if (test_bit(EV_SW, dev->evbit))
879 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
880
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500881 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500882
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100883 envp[i] = NULL;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500884 return 0;
885}
886
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700887struct class input_class = {
888 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500889 .release = input_dev_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100890 .uevent = input_dev_uevent,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500891};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400892EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500893
894struct input_dev *input_allocate_device(void)
895{
896 struct input_dev *dev;
897
898 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
899 if (dev) {
900 dev->dynalloc = 1;
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700901 dev->cdev.class = &input_class;
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500902 class_device_initialize(&dev->cdev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400903 mutex_init(&dev->mutex);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500904 INIT_LIST_HEAD(&dev->h_list);
905 INIT_LIST_HEAD(&dev->node);
906 }
907
908 return dev;
909}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400910EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500911
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400912void input_free_device(struct input_dev *dev)
913{
914 if (dev) {
915
916 mutex_lock(&dev->mutex);
917 dev->name = dev->phys = dev->uniq = NULL;
918 mutex_unlock(&dev->mutex);
919
920 input_put_device(dev);
921 }
922}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400923EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400924
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500925int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500926{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500927 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500928 struct input_handle *handle;
929 struct input_handler *handler;
930 struct input_device_id *id;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500931 const char *path;
932 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500933
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500934 if (!dev->dynalloc) {
935 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
936 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
937 dev->name ? dev->name : "<Unknown>");
938 return -EINVAL;
939 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500940
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500941 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500942
943 /*
944 * If delay and period are pre-set by the driver, then autorepeating
945 * is handled by the driver itself and we don't do it in input.c.
946 */
947
948 init_timer(&dev->timer);
949 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
950 dev->timer.data = (long) dev;
951 dev->timer.function = input_repeat_key;
952 dev->rep[REP_DELAY] = 250;
953 dev->rep[REP_PERIOD] = 33;
954 }
955
956 INIT_LIST_HEAD(&dev->h_list);
957 list_add_tail(&dev->node, &input_dev_list);
958
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500959 dev->cdev.class = &input_class;
960 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
961 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
962
963 error = class_device_add(&dev->cdev);
964 if (error)
965 return error;
966
967 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
968 if (error)
969 goto fail1;
970
971 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
972 if (error)
973 goto fail2;
974
975 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
976 if (error)
977 goto fail3;
978
979 __module_get(THIS_MODULE);
980
981 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
982 printk(KERN_INFO "input: %s as %s\n",
983 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
984 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -0700985
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500986 list_for_each_entry(handler, &input_handler_list, node)
987 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
988 if ((id = input_match_device(handler->id_table, dev)))
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400989 if ((handle = handler->connect(handler, dev, id))) {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500990 input_link_handle(handle);
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400991 if (handler->start)
992 handler->start(handle);
993 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500994
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500995 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500996
997 return 0;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500998
999 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
1000 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
1001 fail1: class_device_del(&dev->cdev);
1002 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001003}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001004EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001005
1006void input_unregister_device(struct input_dev *dev)
1007{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001008 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001009
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001010 if (!dev)
1011 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001012
1013 del_timer_sync(&dev->timer);
1014
1015 list_for_each_safe(node, next, &dev->h_list) {
1016 struct input_handle * handle = to_handle(node);
1017 list_del_init(&handle->d_node);
1018 list_del_init(&handle->h_node);
1019 handle->handler->disconnect(handle);
1020 }
1021
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001022 list_del_init(&dev->node);
1023
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001024 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
1025 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001026 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001027
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001028 mutex_lock(&dev->mutex);
1029 dev->name = dev->phys = dev->uniq = NULL;
1030 mutex_unlock(&dev->mutex);
1031
Dmitry Torokhove7374e42006-06-27 08:30:31 -04001032 class_device_unregister(&dev->cdev);
1033
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001034 input_wakeup_procfs_readers();
1035}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001036EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001037
1038void input_register_handler(struct input_handler *handler)
1039{
1040 struct input_dev *dev;
1041 struct input_handle *handle;
1042 struct input_device_id *id;
1043
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001044 if (!handler)
1045 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001046
1047 INIT_LIST_HEAD(&handler->h_list);
1048
1049 if (handler->fops != NULL)
1050 input_table[handler->minor >> 5] = handler;
1051
1052 list_add_tail(&handler->node, &input_handler_list);
1053
1054 list_for_each_entry(dev, &input_dev_list, node)
1055 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1056 if ((id = input_match_device(handler->id_table, dev)))
1057 if ((handle = handler->connect(handler, dev, id)))
1058 input_link_handle(handle);
1059
1060 input_wakeup_procfs_readers();
1061}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001062EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001063
1064void input_unregister_handler(struct input_handler *handler)
1065{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001066 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001067
1068 list_for_each_safe(node, next, &handler->h_list) {
1069 struct input_handle * handle = to_handle_h(node);
1070 list_del_init(&handle->h_node);
1071 list_del_init(&handle->d_node);
1072 handler->disconnect(handle);
1073 }
1074
1075 list_del_init(&handler->node);
1076
1077 if (handler->fops != NULL)
1078 input_table[handler->minor >> 5] = NULL;
1079
1080 input_wakeup_procfs_readers();
1081}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001082EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001083
1084static int input_open_file(struct inode *inode, struct file *file)
1085{
1086 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001087 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001088 int err;
1089
1090 /* No load-on-demand here? */
1091 if (!handler || !(new_fops = fops_get(handler->fops)))
1092 return -ENODEV;
1093
1094 /*
1095 * That's _really_ odd. Usually NULL ->open means "nothing special",
1096 * not "no device". Oh, well...
1097 */
1098 if (!new_fops->open) {
1099 fops_put(new_fops);
1100 return -ENODEV;
1101 }
1102 old_fops = file->f_op;
1103 file->f_op = new_fops;
1104
1105 err = new_fops->open(inode, file);
1106
1107 if (err) {
1108 fops_put(file->f_op);
1109 file->f_op = fops_get(old_fops);
1110 }
1111 fops_put(old_fops);
1112 return err;
1113}
1114
1115static struct file_operations input_fops = {
1116 .owner = THIS_MODULE,
1117 .open = input_open_file,
1118};
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120static int __init input_init(void)
1121{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001122 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001124 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001125 if (err) {
1126 printk(KERN_ERR "input: unable to register input_dev class\n");
1127 return err;
1128 }
1129
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001130 err = input_proc_init();
1131 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001132 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001133
1134 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1135 if (err) {
1136 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001137 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001139
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001140 return 0;
1141
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001142 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001143 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001144 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147static void __exit input_exit(void)
1148{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001149 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001151 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154subsys_initcall(input_init);
1155module_exit(input_exit);