blob: 9b3bfceff72c0f25a69cebfaef6f31206e6c5f67 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/smp_lock.h>
15#include <linux/input.h>
16#include <linux/module.h>
17#include <linux/random.h>
18#include <linux/major.h>
19#include <linux/proc_fs.h>
Dmitry Torokhov969b21c2006-04-02 00:09:34 -050020#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/interrupt.h>
22#include <linux/poll.h>
23#include <linux/device.h>
Jes Sorensene676c232006-02-19 00:21:46 -050024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
27MODULE_DESCRIPTION("Input core");
28MODULE_LICENSE("GPL");
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define INPUT_DEVICES 256
31
32static LIST_HEAD(input_dev_list);
33static LIST_HEAD(input_handler_list);
34
35static struct input_handler *input_table[8];
36
Dmitry Torokhov0e739d22006-07-06 00:22:43 -040037/**
38 * input_event() - report new input event
Dmitry Torokhov14471902006-11-02 23:26:55 -050039 * @dev: device that generated the event
Dmitry Torokhov0e739d22006-07-06 00:22:43 -040040 * @type: type of the event
41 * @code: event code
42 * @value: value of the event
43 *
44 * This function should be used by drivers implementing various input devices
45 * See also input_inject_event()
46 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
48{
49 struct input_handle *handle;
50
51 if (type > EV_MAX || !test_bit(type, dev->evbit))
52 return;
53
54 add_input_randomness(type, code, value);
55
56 switch (type) {
57
58 case EV_SYN:
59 switch (code) {
60 case SYN_CONFIG:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040061 if (dev->event)
62 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 break;
64
65 case SYN_REPORT:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040066 if (dev->sync)
67 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 dev->sync = 1;
69 break;
70 }
71 break;
72
73 case EV_KEY:
74
75 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
76 return;
77
78 if (value == 2)
79 break;
80
81 change_bit(code, dev->key);
82
83 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
84 dev->repeat_key = code;
85 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
86 }
87
88 break;
89
Richard Purdie31581062005-09-06 15:19:06 -070090 case EV_SW:
91
92 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
93 return;
94
95 change_bit(code, dev->sw);
96
97 break;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 case EV_ABS:
100
101 if (code > ABS_MAX || !test_bit(code, dev->absbit))
102 return;
103
104 if (dev->absfuzz[code]) {
105 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
106 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
107 return;
108
109 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
110 (value < dev->abs[code] + dev->absfuzz[code]))
111 value = (dev->abs[code] * 3 + value) >> 2;
112
113 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
114 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
115 value = (dev->abs[code] + value) >> 1;
116 }
117
118 if (dev->abs[code] == value)
119 return;
120
121 dev->abs[code] = value;
122 break;
123
124 case EV_REL:
125
126 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
127 return;
128
129 break;
130
131 case EV_MSC:
132
133 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
134 return;
135
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400136 if (dev->event)
137 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 break;
140
141 case EV_LED:
142
143 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
144 return;
145
146 change_bit(code, dev->led);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400147
148 if (dev->event)
149 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 break;
152
153 case EV_SND:
154
155 if (code > SND_MAX || !test_bit(code, dev->sndbit))
156 return;
157
Dmitry Torokhov8fdc1942006-04-29 01:13:48 -0400158 if (!!test_bit(code, dev->snd) != !!value)
159 change_bit(code, dev->snd);
160
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400161 if (dev->event)
162 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 break;
165
166 case EV_REP:
167
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400168 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
169 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 dev->rep[code] = value;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400172 if (dev->event)
173 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 break;
176
177 case EV_FF:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400178
179 if (value < 0)
180 return;
181
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400182 if (dev->event)
183 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 }
186
187 if (type != EV_SYN)
188 dev->sync = 0;
189
190 if (dev->grab)
191 dev->grab->handler->event(dev->grab, type, code, value);
192 else
193 list_for_each_entry(handle, &dev->h_list, d_node)
194 if (handle->open)
195 handle->handler->event(handle, type, code, value);
196}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400197EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400199/**
200 * input_inject_event() - send input event from input handler
201 * @handle: input handle to send event through
202 * @type: type of the event
203 * @code: event code
204 * @value: value of the event
205 *
206 * Similar to input_event() but will ignore event if device is "grabbed" and handle
207 * injecting event is not the one that owns the device.
208 */
209void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
210{
211 if (!handle->dev->grab || handle->dev->grab == handle)
212 input_event(handle->dev, type, code, value);
213}
214EXPORT_SYMBOL(input_inject_event);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void input_repeat_key(unsigned long data)
217{
218 struct input_dev *dev = (void *) data;
219
220 if (!test_bit(dev->repeat_key, dev->key))
221 return;
222
223 input_event(dev, EV_KEY, dev->repeat_key, 2);
224 input_sync(dev);
225
226 if (dev->rep[REP_PERIOD])
227 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230int input_grab_device(struct input_handle *handle)
231{
232 if (handle->dev->grab)
233 return -EBUSY;
234
235 handle->dev->grab = handle;
236 return 0;
237}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400238EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240void input_release_device(struct input_handle *handle)
241{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400242 struct input_dev *dev = handle->dev;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400243
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400244 if (dev->grab == handle) {
245 dev->grab = NULL;
246
247 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400248 if (handle->handler->start)
249 handle->handler->start(handle);
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400252EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254int input_open_device(struct input_handle *handle)
255{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500256 struct input_dev *dev = handle->dev;
257 int err;
258
Jes Sorensene676c232006-02-19 00:21:46 -0500259 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500260 if (err)
261 return err;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500264
265 if (!dev->users++ && dev->open)
266 err = dev->open(dev);
267
268 if (err)
269 handle->open--;
270
Jes Sorensene676c232006-02-19 00:21:46 -0500271 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500272
273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400275EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277int input_flush_device(struct input_handle* handle, struct file* file)
278{
279 if (handle->dev->flush)
280 return handle->dev->flush(handle->dev, file);
281
282 return 0;
283}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400284EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286void input_close_device(struct input_handle *handle)
287{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500288 struct input_dev *dev = handle->dev;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500291
Jes Sorensene676c232006-02-19 00:21:46 -0500292 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500293
294 if (!--dev->users && dev->close)
295 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500297
Jes Sorensene676c232006-02-19 00:21:46 -0500298 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400300EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302static void input_link_handle(struct input_handle *handle)
303{
304 list_add_tail(&handle->d_node, &handle->dev->h_list);
305 list_add_tail(&handle->h_node, &handle->handler->h_list);
306}
307
308#define MATCH_BIT(bit, max) \
309 for (i = 0; i < NBITS(max); i++) \
310 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
311 break; \
312 if (i != NBITS(max)) \
313 continue;
314
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400315static const struct input_device_id *input_match_device(const struct input_device_id *id,
316 struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
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 : "");
Dmitry Torokhov15e03ae2007-03-07 23:20:17 -0500442 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500443 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500445 list_for_each_entry(handle, &dev->h_list, d_node)
446 seq_printf(seq, "%s ", handle->name);
447 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500448
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500449 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
450 if (test_bit(EV_KEY, dev->evbit))
451 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
452 if (test_bit(EV_REL, dev->evbit))
453 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
454 if (test_bit(EV_ABS, dev->evbit))
455 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
456 if (test_bit(EV_MSC, dev->evbit))
457 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
458 if (test_bit(EV_LED, dev->evbit))
459 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
460 if (test_bit(EV_SND, dev->evbit))
461 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
462 if (test_bit(EV_FF, dev->evbit))
463 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
464 if (test_bit(EV_SW, dev->evbit))
465 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500467 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500469 kfree(path);
470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500473static struct seq_operations input_devices_seq_ops = {
474 .start = input_devices_seq_start,
475 .next = input_devices_seq_next,
476 .stop = input_devices_seq_stop,
477 .show = input_devices_seq_show,
478};
479
480static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500482 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800485static const struct file_operations input_devices_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500486 .owner = THIS_MODULE,
487 .open = input_proc_devices_open,
488 .poll = input_proc_devices_poll,
489 .read = seq_read,
490 .llseek = seq_lseek,
491 .release = seq_release,
492};
493
494static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
495{
496 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
497 seq->private = (void *)(unsigned long)*pos;
498 return list_get_nth_element(&input_handler_list, pos);
499}
500
501static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
502{
503 seq->private = (void *)(unsigned long)(*pos + 1);
504 return list_get_next_element(&input_handler_list, v, pos);
505}
506
507static void input_handlers_seq_stop(struct seq_file *seq, void *v)
508{
509 /* release lock here */
510}
511
512static int input_handlers_seq_show(struct seq_file *seq, void *v)
513{
514 struct input_handler *handler = container_of(v, struct input_handler, node);
515
516 seq_printf(seq, "N: Number=%ld Name=%s",
517 (unsigned long)seq->private, handler->name);
518 if (handler->fops)
519 seq_printf(seq, " Minor=%d", handler->minor);
520 seq_putc(seq, '\n');
521
522 return 0;
523}
524static struct seq_operations input_handlers_seq_ops = {
525 .start = input_handlers_seq_start,
526 .next = input_handlers_seq_next,
527 .stop = input_handlers_seq_stop,
528 .show = input_handlers_seq_show,
529};
530
531static int input_proc_handlers_open(struct inode *inode, struct file *file)
532{
533 return seq_open(file, &input_handlers_seq_ops);
534}
535
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800536static const struct file_operations input_handlers_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500537 .owner = THIS_MODULE,
538 .open = input_proc_handlers_open,
539 .read = seq_read,
540 .llseek = seq_lseek,
541 .release = seq_release,
542};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544static int __init input_proc_init(void)
545{
546 struct proc_dir_entry *entry;
547
548 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500549 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500553
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500554 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500555 if (!entry)
556 goto fail1;
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_devices_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500560
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500561 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500562 if (!entry)
563 goto fail2;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500566 entry->proc_fops = &input_handlers_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500569
570 fail2: remove_proc_entry("devices", proc_bus_input_dir);
571 fail1: remove_proc_entry("input", proc_bus);
572 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500574
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500575static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500576{
577 remove_proc_entry("devices", proc_bus_input_dir);
578 remove_proc_entry("handlers", proc_bus_input_dir);
579 remove_proc_entry("input", proc_bus);
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500583static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500585static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586#endif
587
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500588#define INPUT_DEV_STRING_ATTR_SHOW(name) \
589static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
590{ \
591 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500592 \
Dmitry Torokhov1efa7702007-02-18 01:40:37 -0500593 return scnprintf(buf, PAGE_SIZE, "%s\n", \
594 input_dev->name ? input_dev->name : ""); \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700595} \
596static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500597
598INPUT_DEV_STRING_ATTR_SHOW(name);
599INPUT_DEV_STRING_ATTR_SHOW(phys);
600INPUT_DEV_STRING_ATTR_SHOW(uniq);
601
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500602static int input_print_modalias_bits(char *buf, int size,
603 char name, unsigned long *bm,
604 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100605{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500606 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100607
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500608 len += snprintf(buf, max(size, 0), "%c", name);
609 for (i = min_bit; i < max_bit; i++)
610 if (bm[LONG(i)] & BIT(i))
611 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100612 return len;
613}
614
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500615static int input_print_modalias(char *buf, int size, struct input_dev *id,
616 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100617{
618 int len;
619
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500620 len = snprintf(buf, max(size, 0),
621 "input:b%04Xv%04Xp%04Xe%04X-",
622 id->id.bustype, id->id.vendor,
623 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100624
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500625 len += input_print_modalias_bits(buf + len, size - len,
626 'e', id->evbit, 0, EV_MAX);
627 len += input_print_modalias_bits(buf + len, size - len,
628 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
629 len += input_print_modalias_bits(buf + len, size - len,
630 'r', id->relbit, 0, REL_MAX);
631 len += input_print_modalias_bits(buf + len, size - len,
632 'a', id->absbit, 0, ABS_MAX);
633 len += input_print_modalias_bits(buf + len, size - len,
634 'm', id->mscbit, 0, MSC_MAX);
635 len += input_print_modalias_bits(buf + len, size - len,
636 'l', id->ledbit, 0, LED_MAX);
637 len += input_print_modalias_bits(buf + len, size - len,
638 's', id->sndbit, 0, SND_MAX);
639 len += input_print_modalias_bits(buf + len, size - len,
640 'f', id->ffbit, 0, FF_MAX);
641 len += input_print_modalias_bits(buf + len, size - len,
642 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500643
644 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500645 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500646
Rusty Russell1d8f4302005-12-07 21:40:34 +0100647 return len;
648}
649
650static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
651{
652 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100653 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100654
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500655 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
656
Richard Purdie8a3cf452006-06-26 01:48:21 -0400657 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100658}
659static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
660
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700661static struct attribute *input_dev_attrs[] = {
662 &class_device_attr_name.attr,
663 &class_device_attr_phys.attr,
664 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100665 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700666 NULL
667};
668
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500669static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700670 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500671};
672
673#define INPUT_DEV_ID_ATTR(name) \
674static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
675{ \
676 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500677 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500678} \
679static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
680
681INPUT_DEV_ID_ATTR(bustype);
682INPUT_DEV_ID_ATTR(vendor);
683INPUT_DEV_ID_ATTR(product);
684INPUT_DEV_ID_ATTR(version);
685
686static struct attribute *input_dev_id_attrs[] = {
687 &class_device_attr_bustype.attr,
688 &class_device_attr_vendor.attr,
689 &class_device_attr_product.attr,
690 &class_device_attr_version.attr,
691 NULL
692};
693
694static struct attribute_group input_dev_id_attr_group = {
695 .name = "id",
696 .attrs = input_dev_id_attrs,
697};
698
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500699static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
700 int max, int add_cr)
701{
702 int i;
703 int len = 0;
704
705 for (i = NBITS(max) - 1; i > 0; i--)
706 if (bitmap[i])
707 break;
708
709 for (; i >= 0; i--)
710 len += snprintf(buf + len, max(buf_size - len, 0),
711 "%lx%s", bitmap[i], i > 0 ? " " : "");
712
713 if (add_cr)
714 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
715
716 return len;
717}
718
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500719#define INPUT_DEV_CAP_ATTR(ev, bm) \
720static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
721{ \
722 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500723 int len = input_print_bitmap(buf, PAGE_SIZE, \
724 input_dev->bm##bit, ev##_MAX, 1); \
725 return min_t(int, len, PAGE_SIZE); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500726} \
727static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
728
729INPUT_DEV_CAP_ATTR(EV, ev);
730INPUT_DEV_CAP_ATTR(KEY, key);
731INPUT_DEV_CAP_ATTR(REL, rel);
732INPUT_DEV_CAP_ATTR(ABS, abs);
733INPUT_DEV_CAP_ATTR(MSC, msc);
734INPUT_DEV_CAP_ATTR(LED, led);
735INPUT_DEV_CAP_ATTR(SND, snd);
736INPUT_DEV_CAP_ATTR(FF, ff);
737INPUT_DEV_CAP_ATTR(SW, sw);
738
739static struct attribute *input_dev_caps_attrs[] = {
740 &class_device_attr_ev.attr,
741 &class_device_attr_key.attr,
742 &class_device_attr_rel.attr,
743 &class_device_attr_abs.attr,
744 &class_device_attr_msc.attr,
745 &class_device_attr_led.attr,
746 &class_device_attr_snd.attr,
747 &class_device_attr_ff.attr,
748 &class_device_attr_sw.attr,
749 NULL
750};
751
752static struct attribute_group input_dev_caps_attr_group = {
753 .name = "capabilities",
754 .attrs = input_dev_caps_attrs,
755};
756
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500757static void input_dev_release(struct class_device *class_dev)
758{
759 struct input_dev *dev = to_input_dev(class_dev);
760
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400761 input_ff_destroy(dev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500762 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400763
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500764 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
Dmitry Torokhov14471902006-11-02 23:26:55 -0500894/**
895 * input_allocate_device - allocate memory for new input device
896 *
897 * Returns prepared struct input_dev or NULL.
898 *
899 * NOTE: Use input_free_device() to free devices that have not been
900 * registered; input_unregister_device() should be used for already
901 * registered devices.
902 */
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500903struct input_dev *input_allocate_device(void)
904{
905 struct input_dev *dev;
906
907 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
908 if (dev) {
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);
Dmitry Torokhov655816e2006-09-14 01:32:14 -0400914
915 __module_get(THIS_MODULE);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500916 }
917
918 return dev;
919}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400920EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500921
Dmitry Torokhov14471902006-11-02 23:26:55 -0500922/**
923 * input_free_device - free memory occupied by input_dev structure
924 * @dev: input device to free
925 *
926 * This function should only be used if input_register_device()
927 * was not called yet or if it failed. Once device was registered
928 * use input_unregister_device() and memory will be freed once last
929 * refrence to the device is dropped.
930 *
931 * Device should be allocated by input_allocate_device().
932 *
933 * NOTE: If there are references to the input device then memory
934 * will not be freed until last reference is dropped.
935 */
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400936void input_free_device(struct input_dev *dev)
937{
938 if (dev) {
939
940 mutex_lock(&dev->mutex);
941 dev->name = dev->phys = dev->uniq = NULL;
942 mutex_unlock(&dev->mutex);
943
944 input_put_device(dev);
945 }
946}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400947EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400948
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500949int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500950{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500951 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500952 struct input_handle *handle;
953 struct input_handler *handler;
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400954 const struct input_device_id *id;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500955 const char *path;
956 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500957
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500958 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500959
960 /*
961 * If delay and period are pre-set by the driver, then autorepeating
962 * is handled by the driver itself and we don't do it in input.c.
963 */
964
965 init_timer(&dev->timer);
966 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
967 dev->timer.data = (long) dev;
968 dev->timer.function = input_repeat_key;
969 dev->rep[REP_DELAY] = 250;
970 dev->rep[REP_PERIOD] = 33;
971 }
972
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500973 list_add_tail(&dev->node, &input_dev_list);
974
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500975 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
976 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
977
978 error = class_device_add(&dev->cdev);
979 if (error)
980 return error;
981
982 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
983 if (error)
984 goto fail1;
985
986 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
987 if (error)
988 goto fail2;
989
990 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
991 if (error)
992 goto fail3;
993
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500994 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
995 printk(KERN_INFO "input: %s as %s\n",
996 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
997 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -0700998
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500999 list_for_each_entry(handler, &input_handler_list, node)
1000 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1001 if ((id = input_match_device(handler->id_table, dev)))
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -04001002 if ((handle = handler->connect(handler, dev, id))) {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001003 input_link_handle(handle);
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -04001004 if (handler->start)
1005 handler->start(handle);
1006 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001007
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001008 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001009
1010 return 0;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001011
1012 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
1013 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
1014 fail1: class_device_del(&dev->cdev);
1015 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001016}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001017EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001018
1019void input_unregister_device(struct input_dev *dev)
1020{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001021 struct list_head *node, *next;
Dmitry Torokhov6d2750c2006-09-10 21:56:06 -04001022 int code;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001023
Dmitry Torokhov6d2750c2006-09-10 21:56:06 -04001024 for (code = 0; code <= KEY_MAX; code++)
1025 if (test_bit(code, dev->key))
1026 input_report_key(dev, code, 0);
1027 input_sync(dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001028
1029 del_timer_sync(&dev->timer);
1030
1031 list_for_each_safe(node, next, &dev->h_list) {
1032 struct input_handle * handle = to_handle(node);
1033 list_del_init(&handle->d_node);
1034 list_del_init(&handle->h_node);
1035 handle->handler->disconnect(handle);
1036 }
1037
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001038 list_del_init(&dev->node);
1039
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001040 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
1041 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001042 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001043
Dmitry Torokhove7374e42006-06-27 08:30:31 -04001044 class_device_unregister(&dev->cdev);
1045
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001046 input_wakeup_procfs_readers();
1047}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001048EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001049
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001050int input_register_handler(struct input_handler *handler)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001051{
1052 struct input_dev *dev;
1053 struct input_handle *handle;
Dmitry Torokhov66e66112006-09-14 01:31:59 -04001054 const struct input_device_id *id;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001055
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001056 INIT_LIST_HEAD(&handler->h_list);
1057
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001058 if (handler->fops != NULL) {
1059 if (input_table[handler->minor >> 5])
1060 return -EBUSY;
1061
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001062 input_table[handler->minor >> 5] = handler;
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001063 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001064
1065 list_add_tail(&handler->node, &input_handler_list);
1066
1067 list_for_each_entry(dev, &input_dev_list, node)
1068 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1069 if ((id = input_match_device(handler->id_table, dev)))
Dmitry Torokhovb6d786d2006-07-19 01:08:51 -04001070 if ((handle = handler->connect(handler, dev, id))) {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001071 input_link_handle(handle);
Dmitry Torokhovb6d786d2006-07-19 01:08:51 -04001072 if (handler->start)
1073 handler->start(handle);
1074 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001075
1076 input_wakeup_procfs_readers();
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001077 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001078}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001079EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001080
1081void input_unregister_handler(struct input_handler *handler)
1082{
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -04001083 struct list_head *node, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001084
1085 list_for_each_safe(node, next, &handler->h_list) {
1086 struct input_handle * handle = to_handle_h(node);
1087 list_del_init(&handle->h_node);
1088 list_del_init(&handle->d_node);
1089 handler->disconnect(handle);
1090 }
1091
1092 list_del_init(&handler->node);
1093
1094 if (handler->fops != NULL)
1095 input_table[handler->minor >> 5] = NULL;
1096
1097 input_wakeup_procfs_readers();
1098}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001099EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001100
1101static int input_open_file(struct inode *inode, struct file *file)
1102{
1103 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001104 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001105 int err;
1106
1107 /* No load-on-demand here? */
1108 if (!handler || !(new_fops = fops_get(handler->fops)))
1109 return -ENODEV;
1110
1111 /*
1112 * That's _really_ odd. Usually NULL ->open means "nothing special",
1113 * not "no device". Oh, well...
1114 */
1115 if (!new_fops->open) {
1116 fops_put(new_fops);
1117 return -ENODEV;
1118 }
1119 old_fops = file->f_op;
1120 file->f_op = new_fops;
1121
1122 err = new_fops->open(inode, file);
1123
1124 if (err) {
1125 fops_put(file->f_op);
1126 file->f_op = fops_get(old_fops);
1127 }
1128 fops_put(old_fops);
1129 return err;
1130}
1131
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001132static const struct file_operations input_fops = {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001133 .owner = THIS_MODULE,
1134 .open = input_open_file,
1135};
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137static int __init input_init(void)
1138{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001139 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001141 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001142 if (err) {
1143 printk(KERN_ERR "input: unable to register input_dev class\n");
1144 return err;
1145 }
1146
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001147 err = input_proc_init();
1148 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001149 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001150
1151 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1152 if (err) {
1153 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001154 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001156
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001157 return 0;
1158
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001159 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001160 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164static void __exit input_exit(void)
1165{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001166 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001168 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
1171subsys_initcall(input_init);
1172module_exit(input_exit);