blob: 1fc0517e9428ca21e3b058539df656e00311e416 [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:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400179
180 if (value < 0)
181 return;
182
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400183 if (dev->event)
184 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 break;
186 }
187
188 if (type != EV_SYN)
189 dev->sync = 0;
190
191 if (dev->grab)
192 dev->grab->handler->event(dev->grab, type, code, value);
193 else
194 list_for_each_entry(handle, &dev->h_list, d_node)
195 if (handle->open)
196 handle->handler->event(handle, type, code, value);
197}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400198EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400200/**
201 * input_inject_event() - send input event from input handler
202 * @handle: input handle to send event through
203 * @type: type of the event
204 * @code: event code
205 * @value: value of the event
206 *
207 * Similar to input_event() but will ignore event if device is "grabbed" and handle
208 * injecting event is not the one that owns the device.
209 */
210void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
211{
212 if (!handle->dev->grab || handle->dev->grab == handle)
213 input_event(handle->dev, type, code, value);
214}
215EXPORT_SYMBOL(input_inject_event);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static void input_repeat_key(unsigned long data)
218{
219 struct input_dev *dev = (void *) data;
220
221 if (!test_bit(dev->repeat_key, dev->key))
222 return;
223
224 input_event(dev, EV_KEY, dev->repeat_key, 2);
225 input_sync(dev);
226
227 if (dev->rep[REP_PERIOD])
228 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231int input_grab_device(struct input_handle *handle)
232{
233 if (handle->dev->grab)
234 return -EBUSY;
235
236 handle->dev->grab = handle;
237 return 0;
238}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400239EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241void input_release_device(struct input_handle *handle)
242{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400243 struct input_dev *dev = handle->dev;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400244
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400245 if (dev->grab == handle) {
246 dev->grab = NULL;
247
248 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400249 if (handle->handler->start)
250 handle->handler->start(handle);
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400253EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255int input_open_device(struct input_handle *handle)
256{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500257 struct input_dev *dev = handle->dev;
258 int err;
259
Jes Sorensene676c232006-02-19 00:21:46 -0500260 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500261 if (err)
262 return err;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500265
266 if (!dev->users++ && dev->open)
267 err = dev->open(dev);
268
269 if (err)
270 handle->open--;
271
Jes Sorensene676c232006-02-19 00:21:46 -0500272 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500273
274 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400276EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278int input_flush_device(struct input_handle* handle, struct file* file)
279{
280 if (handle->dev->flush)
281 return handle->dev->flush(handle->dev, file);
282
283 return 0;
284}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400285EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287void input_close_device(struct input_handle *handle)
288{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500289 struct input_dev *dev = handle->dev;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500292
Jes Sorensene676c232006-02-19 00:21:46 -0500293 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500294
295 if (!--dev->users && dev->close)
296 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500298
Jes Sorensene676c232006-02-19 00:21:46 -0500299 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400301EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303static void input_link_handle(struct input_handle *handle)
304{
305 list_add_tail(&handle->d_node, &handle->dev->h_list);
306 list_add_tail(&handle->h_node, &handle->handler->h_list);
307}
308
309#define MATCH_BIT(bit, max) \
310 for (i = 0; i < NBITS(max); i++) \
311 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
312 break; \
313 if (i != NBITS(max)) \
314 continue;
315
316static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
317{
318 int i;
319
320 for (; id->flags || id->driver_info; id++) {
321
322 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400323 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 continue;
325
326 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400327 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 continue;
329
330 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400331 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 continue;
333
334 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400335 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 continue;
337
338 MATCH_BIT(evbit, EV_MAX);
339 MATCH_BIT(keybit, KEY_MAX);
340 MATCH_BIT(relbit, REL_MAX);
341 MATCH_BIT(absbit, ABS_MAX);
342 MATCH_BIT(mscbit, MSC_MAX);
343 MATCH_BIT(ledbit, LED_MAX);
344 MATCH_BIT(sndbit, SND_MAX);
345 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500346 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 return id;
349 }
350
351 return NULL;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500355
356static struct proc_dir_entry *proc_bus_input_dir;
357static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
358static int input_devices_state;
359
360static inline void input_wakeup_procfs_readers(void)
361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 input_devices_state++;
363 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500366static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500368 int state = input_devices_state;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400369
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500370 poll_wait(file, &input_devices_poll_wait, wait);
371 if (state != input_devices_state)
372 return POLLIN | POLLRDNORM;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400373
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500377static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500379 struct list_head *node;
380 loff_t i = 0;
381
382 list_for_each(node, list)
383 if (i++ == *pos)
384 return node;
385
386 return NULL;
387}
388
389static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
390{
391 if (element->next == list)
392 return NULL;
393
394 ++(*pos);
395 return element->next;
396}
397
398static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
399{
400 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
401
402 return list_get_nth_element(&input_dev_list, pos);
403}
404
405static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
406{
407 return list_get_next_element(&input_dev_list, v, pos);
408}
409
410static void input_devices_seq_stop(struct seq_file *seq, void *v)
411{
412 /* release lock here */
413}
414
415static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
416 unsigned long *bitmap, int max)
417{
418 int i;
419
420 for (i = NBITS(max) - 1; i > 0; i--)
421 if (bitmap[i])
422 break;
423
424 seq_printf(seq, "B: %s=", name);
425 for (; i >= 0; i--)
426 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
427 seq_putc(seq, '\n');
428}
429
430static int input_devices_seq_show(struct seq_file *seq, void *v)
431{
432 struct input_dev *dev = container_of(v, struct input_dev, node);
433 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct input_handle *handle;
435
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500436 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
437 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500439 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
440 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
441 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
442 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500444 list_for_each_entry(handle, &dev->h_list, d_node)
445 seq_printf(seq, "%s ", handle->name);
446 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500447
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500448 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
449 if (test_bit(EV_KEY, dev->evbit))
450 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
451 if (test_bit(EV_REL, dev->evbit))
452 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
453 if (test_bit(EV_ABS, dev->evbit))
454 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
455 if (test_bit(EV_MSC, dev->evbit))
456 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
457 if (test_bit(EV_LED, dev->evbit))
458 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
459 if (test_bit(EV_SND, dev->evbit))
460 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
461 if (test_bit(EV_FF, dev->evbit))
462 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
463 if (test_bit(EV_SW, dev->evbit))
464 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500466 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500468 kfree(path);
469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500472static struct seq_operations input_devices_seq_ops = {
473 .start = input_devices_seq_start,
474 .next = input_devices_seq_next,
475 .stop = input_devices_seq_stop,
476 .show = input_devices_seq_show,
477};
478
479static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500481 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500484static struct file_operations input_devices_fileops = {
485 .owner = THIS_MODULE,
486 .open = input_proc_devices_open,
487 .poll = input_proc_devices_poll,
488 .read = seq_read,
489 .llseek = seq_lseek,
490 .release = seq_release,
491};
492
493static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
494{
495 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
496 seq->private = (void *)(unsigned long)*pos;
497 return list_get_nth_element(&input_handler_list, pos);
498}
499
500static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
501{
502 seq->private = (void *)(unsigned long)(*pos + 1);
503 return list_get_next_element(&input_handler_list, v, pos);
504}
505
506static void input_handlers_seq_stop(struct seq_file *seq, void *v)
507{
508 /* release lock here */
509}
510
511static int input_handlers_seq_show(struct seq_file *seq, void *v)
512{
513 struct input_handler *handler = container_of(v, struct input_handler, node);
514
515 seq_printf(seq, "N: Number=%ld Name=%s",
516 (unsigned long)seq->private, handler->name);
517 if (handler->fops)
518 seq_printf(seq, " Minor=%d", handler->minor);
519 seq_putc(seq, '\n');
520
521 return 0;
522}
523static struct seq_operations input_handlers_seq_ops = {
524 .start = input_handlers_seq_start,
525 .next = input_handlers_seq_next,
526 .stop = input_handlers_seq_stop,
527 .show = input_handlers_seq_show,
528};
529
530static int input_proc_handlers_open(struct inode *inode, struct file *file)
531{
532 return seq_open(file, &input_handlers_seq_ops);
533}
534
535static struct file_operations input_handlers_fileops = {
536 .owner = THIS_MODULE,
537 .open = input_proc_handlers_open,
538 .read = seq_read,
539 .llseek = seq_lseek,
540 .release = seq_release,
541};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static int __init input_proc_init(void)
544{
545 struct proc_dir_entry *entry;
546
547 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500548 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500552
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500553 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500554 if (!entry)
555 goto fail1;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500558 entry->proc_fops = &input_devices_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500559
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500560 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500561 if (!entry)
562 goto fail2;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500565 entry->proc_fops = &input_handlers_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500568
569 fail2: remove_proc_entry("devices", proc_bus_input_dir);
570 fail1: remove_proc_entry("input", proc_bus);
571 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500573
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500574static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500575{
576 remove_proc_entry("devices", proc_bus_input_dir);
577 remove_proc_entry("handlers", proc_bus_input_dir);
578 remove_proc_entry("input", proc_bus);
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500582static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500584static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#endif
586
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500587#define INPUT_DEV_STRING_ATTR_SHOW(name) \
588static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
589{ \
590 struct input_dev *input_dev = to_input_dev(dev); \
591 int retval; \
592 \
Jes Sorensene676c232006-02-19 00:21:46 -0500593 retval = mutex_lock_interruptible(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500594 if (retval) \
595 return retval; \
596 \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500597 retval = scnprintf(buf, PAGE_SIZE, \
598 "%s\n", input_dev->name ? input_dev->name : ""); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500599 \
Jes Sorensene676c232006-02-19 00:21:46 -0500600 mutex_unlock(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500601 \
602 return retval; \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700603} \
604static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500605
606INPUT_DEV_STRING_ATTR_SHOW(name);
607INPUT_DEV_STRING_ATTR_SHOW(phys);
608INPUT_DEV_STRING_ATTR_SHOW(uniq);
609
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500610static int input_print_modalias_bits(char *buf, int size,
611 char name, unsigned long *bm,
612 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100613{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500614 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100615
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500616 len += snprintf(buf, max(size, 0), "%c", name);
617 for (i = min_bit; i < max_bit; i++)
618 if (bm[LONG(i)] & BIT(i))
619 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100620 return len;
621}
622
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500623static int input_print_modalias(char *buf, int size, struct input_dev *id,
624 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100625{
626 int len;
627
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500628 len = snprintf(buf, max(size, 0),
629 "input:b%04Xv%04Xp%04Xe%04X-",
630 id->id.bustype, id->id.vendor,
631 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100632
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500633 len += input_print_modalias_bits(buf + len, size - len,
634 'e', id->evbit, 0, EV_MAX);
635 len += input_print_modalias_bits(buf + len, size - len,
636 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
637 len += input_print_modalias_bits(buf + len, size - len,
638 'r', id->relbit, 0, REL_MAX);
639 len += input_print_modalias_bits(buf + len, size - len,
640 'a', id->absbit, 0, ABS_MAX);
641 len += input_print_modalias_bits(buf + len, size - len,
642 'm', id->mscbit, 0, MSC_MAX);
643 len += input_print_modalias_bits(buf + len, size - len,
644 'l', id->ledbit, 0, LED_MAX);
645 len += input_print_modalias_bits(buf + len, size - len,
646 's', id->sndbit, 0, SND_MAX);
647 len += input_print_modalias_bits(buf + len, size - len,
648 'f', id->ffbit, 0, FF_MAX);
649 len += input_print_modalias_bits(buf + len, size - len,
650 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500651
652 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500653 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500654
Rusty Russell1d8f4302005-12-07 21:40:34 +0100655 return len;
656}
657
658static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
659{
660 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100661 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100662
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500663 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
664
Richard Purdie8a3cf452006-06-26 01:48:21 -0400665 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100666}
667static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
668
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700669static struct attribute *input_dev_attrs[] = {
670 &class_device_attr_name.attr,
671 &class_device_attr_phys.attr,
672 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100673 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700674 NULL
675};
676
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500677static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700678 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500679};
680
681#define INPUT_DEV_ID_ATTR(name) \
682static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
683{ \
684 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500685 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500686} \
687static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
688
689INPUT_DEV_ID_ATTR(bustype);
690INPUT_DEV_ID_ATTR(vendor);
691INPUT_DEV_ID_ATTR(product);
692INPUT_DEV_ID_ATTR(version);
693
694static struct attribute *input_dev_id_attrs[] = {
695 &class_device_attr_bustype.attr,
696 &class_device_attr_vendor.attr,
697 &class_device_attr_product.attr,
698 &class_device_attr_version.attr,
699 NULL
700};
701
702static struct attribute_group input_dev_id_attr_group = {
703 .name = "id",
704 .attrs = input_dev_id_attrs,
705};
706
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500707static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
708 int max, int add_cr)
709{
710 int i;
711 int len = 0;
712
713 for (i = NBITS(max) - 1; i > 0; i--)
714 if (bitmap[i])
715 break;
716
717 for (; i >= 0; i--)
718 len += snprintf(buf + len, max(buf_size - len, 0),
719 "%lx%s", bitmap[i], i > 0 ? " " : "");
720
721 if (add_cr)
722 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
723
724 return len;
725}
726
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500727#define INPUT_DEV_CAP_ATTR(ev, bm) \
728static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
729{ \
730 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500731 int len = input_print_bitmap(buf, PAGE_SIZE, \
732 input_dev->bm##bit, ev##_MAX, 1); \
733 return min_t(int, len, PAGE_SIZE); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500734} \
735static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
736
737INPUT_DEV_CAP_ATTR(EV, ev);
738INPUT_DEV_CAP_ATTR(KEY, key);
739INPUT_DEV_CAP_ATTR(REL, rel);
740INPUT_DEV_CAP_ATTR(ABS, abs);
741INPUT_DEV_CAP_ATTR(MSC, msc);
742INPUT_DEV_CAP_ATTR(LED, led);
743INPUT_DEV_CAP_ATTR(SND, snd);
744INPUT_DEV_CAP_ATTR(FF, ff);
745INPUT_DEV_CAP_ATTR(SW, sw);
746
747static struct attribute *input_dev_caps_attrs[] = {
748 &class_device_attr_ev.attr,
749 &class_device_attr_key.attr,
750 &class_device_attr_rel.attr,
751 &class_device_attr_abs.attr,
752 &class_device_attr_msc.attr,
753 &class_device_attr_led.attr,
754 &class_device_attr_snd.attr,
755 &class_device_attr_ff.attr,
756 &class_device_attr_sw.attr,
757 NULL
758};
759
760static struct attribute_group input_dev_caps_attr_group = {
761 .name = "capabilities",
762 .attrs = input_dev_caps_attrs,
763};
764
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500765static void input_dev_release(struct class_device *class_dev)
766{
767 struct input_dev *dev = to_input_dev(class_dev);
768
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400769 input_ff_destroy(dev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500770 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400771
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500772 module_put(THIS_MODULE);
773}
774
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500775/*
Kay Sievers312c0042005-11-16 09:00:00 +0100776 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500777 * device bitfields.
778 */
Kay Sievers312c0042005-11-16 09:00:00 +0100779static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500780 char *buffer, int buffer_size, int *cur_len,
781 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500782{
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), name);
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500789 if (*cur_len >= buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500790 return -ENOMEM;
791
792 *cur_len += input_print_bitmap(buffer + *cur_len,
793 max(buffer_size - *cur_len, 0),
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500794 bitmap, max, 0) + 1;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500795 if (*cur_len > buffer_size)
796 return -ENOMEM;
797
798 (*cur_index)++;
799 return 0;
800}
801
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500802static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
803 char *buffer, int buffer_size, int *cur_len,
804 struct input_dev *dev)
805{
806 if (*cur_index >= num_envp - 1)
807 return -ENOMEM;
808
809 envp[*cur_index] = buffer + *cur_len;
810
811 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
812 "MODALIAS=");
813 if (*cur_len >= buffer_size)
814 return -ENOMEM;
815
816 *cur_len += input_print_modalias(buffer + *cur_len,
817 max(buffer_size - *cur_len, 0),
818 dev, 0) + 1;
819 if (*cur_len > buffer_size)
820 return -ENOMEM;
821
822 (*cur_index)++;
823 return 0;
824}
825
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500826#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
827 do { \
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500828 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500829 buffer, buffer_size, &len, \
830 fmt, val); \
831 if (err) \
832 return err; \
833 } while (0)
834
835#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
836 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100837 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500838 buffer, buffer_size, &len, \
839 name, bm, max); \
840 if (err) \
841 return err; \
842 } while (0)
843
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500844#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
845 do { \
846 int err = input_add_uevent_modalias_var(envp, \
847 num_envp, &i, \
848 buffer, buffer_size, &len, \
849 dev); \
850 if (err) \
851 return err; \
852 } while (0)
853
Kay Sievers312c0042005-11-16 09:00:00 +0100854static int input_dev_uevent(struct class_device *cdev, char **envp,
855 int num_envp, char *buffer, int buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500856{
857 struct input_dev *dev = to_input_dev(cdev);
858 int i = 0;
859 int len = 0;
860
861 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
862 dev->id.bustype, dev->id.vendor,
863 dev->id.product, dev->id.version);
864 if (dev->name)
865 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
866 if (dev->phys)
867 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800868 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500869 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
870
871 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
872 if (test_bit(EV_KEY, dev->evbit))
873 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
874 if (test_bit(EV_REL, dev->evbit))
875 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
876 if (test_bit(EV_ABS, dev->evbit))
877 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
878 if (test_bit(EV_MSC, dev->evbit))
879 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
880 if (test_bit(EV_LED, dev->evbit))
881 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
882 if (test_bit(EV_SND, dev->evbit))
883 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
884 if (test_bit(EV_FF, dev->evbit))
885 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
886 if (test_bit(EV_SW, dev->evbit))
887 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
888
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500889 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500890
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100891 envp[i] = NULL;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500892 return 0;
893}
894
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700895struct class input_class = {
896 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500897 .release = input_dev_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100898 .uevent = input_dev_uevent,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500899};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400900EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500901
902struct input_dev *input_allocate_device(void)
903{
904 struct input_dev *dev;
905
906 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
907 if (dev) {
908 dev->dynalloc = 1;
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700909 dev->cdev.class = &input_class;
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500910 class_device_initialize(&dev->cdev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400911 mutex_init(&dev->mutex);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500912 INIT_LIST_HEAD(&dev->h_list);
913 INIT_LIST_HEAD(&dev->node);
914 }
915
916 return dev;
917}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400918EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500919
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400920void input_free_device(struct input_dev *dev)
921{
922 if (dev) {
923
924 mutex_lock(&dev->mutex);
925 dev->name = dev->phys = dev->uniq = NULL;
926 mutex_unlock(&dev->mutex);
927
928 input_put_device(dev);
929 }
930}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400931EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400932
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500933int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500934{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500935 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500936 struct input_handle *handle;
937 struct input_handler *handler;
938 struct input_device_id *id;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500939 const char *path;
940 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500941
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500942 if (!dev->dynalloc) {
943 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
944 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
945 dev->name ? dev->name : "<Unknown>");
946 return -EINVAL;
947 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500948
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500949 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500950
951 /*
952 * If delay and period are pre-set by the driver, then autorepeating
953 * is handled by the driver itself and we don't do it in input.c.
954 */
955
956 init_timer(&dev->timer);
957 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
958 dev->timer.data = (long) dev;
959 dev->timer.function = input_repeat_key;
960 dev->rep[REP_DELAY] = 250;
961 dev->rep[REP_PERIOD] = 33;
962 }
963
964 INIT_LIST_HEAD(&dev->h_list);
965 list_add_tail(&dev->node, &input_dev_list);
966
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500967 dev->cdev.class = &input_class;
968 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
969 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
970
971 error = class_device_add(&dev->cdev);
972 if (error)
973 return error;
974
975 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
976 if (error)
977 goto fail1;
978
979 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
980 if (error)
981 goto fail2;
982
983 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
984 if (error)
985 goto fail3;
986
987 __module_get(THIS_MODULE);
988
989 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
990 printk(KERN_INFO "input: %s as %s\n",
991 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
992 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -0700993
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500994 list_for_each_entry(handler, &input_handler_list, node)
995 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
996 if ((id = input_match_device(handler->id_table, dev)))
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400997 if ((handle = handler->connect(handler, dev, id))) {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500998 input_link_handle(handle);
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400999 if (handler->start)
1000 handler->start(handle);
1001 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001002
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001003 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001004
1005 return 0;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001006
1007 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
1008 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
1009 fail1: class_device_del(&dev->cdev);
1010 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001011}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001012EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001013
1014void input_unregister_device(struct input_dev *dev)
1015{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001016 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001017
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001018 if (!dev)
1019 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001020
1021 del_timer_sync(&dev->timer);
1022
1023 list_for_each_safe(node, next, &dev->h_list) {
1024 struct input_handle * handle = to_handle(node);
1025 list_del_init(&handle->d_node);
1026 list_del_init(&handle->h_node);
1027 handle->handler->disconnect(handle);
1028 }
1029
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001030 list_del_init(&dev->node);
1031
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001032 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
1033 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001034 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001035
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001036 mutex_lock(&dev->mutex);
1037 dev->name = dev->phys = dev->uniq = NULL;
1038 mutex_unlock(&dev->mutex);
1039
Dmitry Torokhove7374e42006-06-27 08:30:31 -04001040 class_device_unregister(&dev->cdev);
1041
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001042 input_wakeup_procfs_readers();
1043}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001044EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001045
1046void input_register_handler(struct input_handler *handler)
1047{
1048 struct input_dev *dev;
1049 struct input_handle *handle;
1050 struct input_device_id *id;
1051
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001052 if (!handler)
1053 return;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001054
1055 INIT_LIST_HEAD(&handler->h_list);
1056
1057 if (handler->fops != NULL)
1058 input_table[handler->minor >> 5] = handler;
1059
1060 list_add_tail(&handler->node, &input_handler_list);
1061
1062 list_for_each_entry(dev, &input_dev_list, node)
1063 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1064 if ((id = input_match_device(handler->id_table, dev)))
Dmitry Torokhovb6d786d2006-07-19 01:08:51 -04001065 if ((handle = handler->connect(handler, dev, id))) {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001066 input_link_handle(handle);
Dmitry Torokhovb6d786d2006-07-19 01:08:51 -04001067 if (handler->start)
1068 handler->start(handle);
1069 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001070
1071 input_wakeup_procfs_readers();
1072}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001073EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001074
1075void input_unregister_handler(struct input_handler *handler)
1076{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001077 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001078
1079 list_for_each_safe(node, next, &handler->h_list) {
1080 struct input_handle * handle = to_handle_h(node);
1081 list_del_init(&handle->h_node);
1082 list_del_init(&handle->d_node);
1083 handler->disconnect(handle);
1084 }
1085
1086 list_del_init(&handler->node);
1087
1088 if (handler->fops != NULL)
1089 input_table[handler->minor >> 5] = NULL;
1090
1091 input_wakeup_procfs_readers();
1092}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001093EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001094
1095static int input_open_file(struct inode *inode, struct file *file)
1096{
1097 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001098 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001099 int err;
1100
1101 /* No load-on-demand here? */
1102 if (!handler || !(new_fops = fops_get(handler->fops)))
1103 return -ENODEV;
1104
1105 /*
1106 * That's _really_ odd. Usually NULL ->open means "nothing special",
1107 * not "no device". Oh, well...
1108 */
1109 if (!new_fops->open) {
1110 fops_put(new_fops);
1111 return -ENODEV;
1112 }
1113 old_fops = file->f_op;
1114 file->f_op = new_fops;
1115
1116 err = new_fops->open(inode, file);
1117
1118 if (err) {
1119 fops_put(file->f_op);
1120 file->f_op = fops_get(old_fops);
1121 }
1122 fops_put(old_fops);
1123 return err;
1124}
1125
1126static struct file_operations input_fops = {
1127 .owner = THIS_MODULE,
1128 .open = input_open_file,
1129};
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131static int __init input_init(void)
1132{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001133 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001135 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001136 if (err) {
1137 printk(KERN_ERR "input: unable to register input_dev class\n");
1138 return err;
1139 }
1140
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001141 err = input_proc_init();
1142 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001143 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001144
1145 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1146 if (err) {
1147 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001148 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001150
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001151 return 0;
1152
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001153 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001154 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158static void __exit input_exit(void)
1159{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001160 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001162 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165subsys_initcall(input_init);
1166module_exit(input_exit);