blob: 3e22bb11ee05a4b38b5503e2203268bc78776544 [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>
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
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -050030EXPORT_SYMBOL(input_allocate_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031EXPORT_SYMBOL(input_register_device);
32EXPORT_SYMBOL(input_unregister_device);
33EXPORT_SYMBOL(input_register_handler);
34EXPORT_SYMBOL(input_unregister_handler);
35EXPORT_SYMBOL(input_grab_device);
36EXPORT_SYMBOL(input_release_device);
37EXPORT_SYMBOL(input_open_device);
38EXPORT_SYMBOL(input_close_device);
39EXPORT_SYMBOL(input_accept_process);
40EXPORT_SYMBOL(input_flush_device);
41EXPORT_SYMBOL(input_event);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -070042EXPORT_SYMBOL_GPL(input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define INPUT_DEVICES 256
45
46static LIST_HEAD(input_dev_list);
47static LIST_HEAD(input_handler_list);
48
49static struct input_handler *input_table[8];
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
52{
53 struct input_handle *handle;
54
55 if (type > EV_MAX || !test_bit(type, dev->evbit))
56 return;
57
58 add_input_randomness(type, code, value);
59
60 switch (type) {
61
62 case EV_SYN:
63 switch (code) {
64 case SYN_CONFIG:
65 if (dev->event) dev->event(dev, type, code, value);
66 break;
67
68 case SYN_REPORT:
69 if (dev->sync) return;
70 dev->sync = 1;
71 break;
72 }
73 break;
74
75 case EV_KEY:
76
77 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
78 return;
79
80 if (value == 2)
81 break;
82
83 change_bit(code, dev->key);
84
85 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
86 dev->repeat_key = code;
87 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
88 }
89
90 break;
91
Richard Purdie31581062005-09-06 15:19:06 -070092 case EV_SW:
93
94 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
95 return;
96
97 change_bit(code, dev->sw);
98
99 break;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 case EV_ABS:
102
103 if (code > ABS_MAX || !test_bit(code, dev->absbit))
104 return;
105
106 if (dev->absfuzz[code]) {
107 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
108 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
109 return;
110
111 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
112 (value < dev->abs[code] + dev->absfuzz[code]))
113 value = (dev->abs[code] * 3 + value) >> 2;
114
115 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
116 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
117 value = (dev->abs[code] + value) >> 1;
118 }
119
120 if (dev->abs[code] == value)
121 return;
122
123 dev->abs[code] = value;
124 break;
125
126 case EV_REL:
127
128 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
129 return;
130
131 break;
132
133 case EV_MSC:
134
135 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
136 return;
137
138 if (dev->event) dev->event(dev, type, code, value);
139
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);
148 if (dev->event) dev->event(dev, type, code, value);
149
150 break;
151
152 case EV_SND:
153
154 if (code > SND_MAX || !test_bit(code, dev->sndbit))
155 return;
156
157 if (dev->event) dev->event(dev, type, code, value);
158
159 break;
160
161 case EV_REP:
162
163 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
164
165 dev->rep[code] = value;
166 if (dev->event) dev->event(dev, type, code, value);
167
168 break;
169
170 case EV_FF:
171 if (dev->event) dev->event(dev, type, code, value);
172 break;
173 }
174
175 if (type != EV_SYN)
176 dev->sync = 0;
177
178 if (dev->grab)
179 dev->grab->handler->event(dev->grab, type, code, value);
180 else
181 list_for_each_entry(handle, &dev->h_list, d_node)
182 if (handle->open)
183 handle->handler->event(handle, type, code, value);
184}
185
186static void input_repeat_key(unsigned long data)
187{
188 struct input_dev *dev = (void *) data;
189
190 if (!test_bit(dev->repeat_key, dev->key))
191 return;
192
193 input_event(dev, EV_KEY, dev->repeat_key, 2);
194 input_sync(dev);
195
196 if (dev->rep[REP_PERIOD])
197 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
198}
199
200int input_accept_process(struct input_handle *handle, struct file *file)
201{
202 if (handle->dev->accept)
203 return handle->dev->accept(handle->dev, file);
204
205 return 0;
206}
207
208int input_grab_device(struct input_handle *handle)
209{
210 if (handle->dev->grab)
211 return -EBUSY;
212
213 handle->dev->grab = handle;
214 return 0;
215}
216
217void input_release_device(struct input_handle *handle)
218{
219 if (handle->dev->grab == handle)
220 handle->dev->grab = NULL;
221}
222
223int input_open_device(struct input_handle *handle)
224{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500225 struct input_dev *dev = handle->dev;
226 int err;
227
Jes Sorensene676c232006-02-19 00:21:46 -0500228 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500229 if (err)
230 return err;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500233
234 if (!dev->users++ && dev->open)
235 err = dev->open(dev);
236
237 if (err)
238 handle->open--;
239
Jes Sorensene676c232006-02-19 00:21:46 -0500240 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500241
242 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245int input_flush_device(struct input_handle* handle, struct file* file)
246{
247 if (handle->dev->flush)
248 return handle->dev->flush(handle->dev, file);
249
250 return 0;
251}
252
253void input_close_device(struct input_handle *handle)
254{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500255 struct input_dev *dev = handle->dev;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500258
Jes Sorensene676c232006-02-19 00:21:46 -0500259 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500260
261 if (!--dev->users && dev->close)
262 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500264
Jes Sorensene676c232006-02-19 00:21:46 -0500265 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268static void input_link_handle(struct input_handle *handle)
269{
270 list_add_tail(&handle->d_node, &handle->dev->h_list);
271 list_add_tail(&handle->h_node, &handle->handler->h_list);
272}
273
274#define MATCH_BIT(bit, max) \
275 for (i = 0; i < NBITS(max); i++) \
276 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
277 break; \
278 if (i != NBITS(max)) \
279 continue;
280
281static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
282{
283 int i;
284
285 for (; id->flags || id->driver_info; id++) {
286
287 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
288 if (id->id.bustype != dev->id.bustype)
289 continue;
290
291 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
292 if (id->id.vendor != dev->id.vendor)
293 continue;
294
295 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
296 if (id->id.product != dev->id.product)
297 continue;
298
299 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
300 if (id->id.version != dev->id.version)
301 continue;
302
303 MATCH_BIT(evbit, EV_MAX);
304 MATCH_BIT(keybit, KEY_MAX);
305 MATCH_BIT(relbit, REL_MAX);
306 MATCH_BIT(absbit, ABS_MAX);
307 MATCH_BIT(mscbit, MSC_MAX);
308 MATCH_BIT(ledbit, LED_MAX);
309 MATCH_BIT(sndbit, SND_MAX);
310 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500311 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 return id;
314 }
315
316 return NULL;
317}
318
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500319static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap, int max)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500320{
321 int i;
322 int len = 0;
323
324 for (i = NBITS(max) - 1; i > 0; i--)
325 if (bitmap[i])
326 break;
327
328 for (; i >= 0; i--)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500329 len += snprintf(buf + len, max(buf_size - len, 0),
330 "%lx%s", bitmap[i], i > 0 ? " " : "");
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500331 return len;
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500335
336static struct proc_dir_entry *proc_bus_input_dir;
337static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
338static int input_devices_state;
339
340static inline void input_wakeup_procfs_readers(void)
341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 input_devices_state++;
343 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500346static unsigned int input_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500348 int state = input_devices_state;
349 poll_wait(file, &input_devices_poll_wait, wait);
350 if (state != input_devices_state)
351 return POLLIN | POLLRDNORM;
352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500355#define SPRINTF_BIT(ev, bm) \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500356 do { \
357 len += sprintf(buf + len, "B: %s=", #ev); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500358 len += input_print_bitmap(buf + len, INT_MAX, \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500359 dev->bm##bit, ev##_MAX); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500360 len += sprintf(buf + len, "\n"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 } while (0)
362
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500363#define TEST_AND_SPRINTF_BIT(ev, bm) \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500364 do { \
365 if (test_bit(EV_##ev, dev->evbit)) \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500366 SPRINTF_BIT(ev, bm); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 } while (0)
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
370{
371 struct input_dev *dev;
372 struct input_handle *handle;
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500373 const char *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 off_t at = 0;
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500376 int len, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 list_for_each_entry(dev, &input_dev_list, node) {
379
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500380 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
383 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
384
385 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
386 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500387 len += sprintf(buf + len, "S: Sysfs=%s\n", path ? path : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 len += sprintf(buf + len, "H: Handlers=");
389
390 list_for_each_entry(handle, &dev->h_list, d_node)
391 len += sprintf(buf + len, "%s ", handle->name);
392
393 len += sprintf(buf + len, "\n");
394
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500395 SPRINTF_BIT(EV, ev);
396 TEST_AND_SPRINTF_BIT(KEY, key);
397 TEST_AND_SPRINTF_BIT(REL, rel);
398 TEST_AND_SPRINTF_BIT(ABS, abs);
399 TEST_AND_SPRINTF_BIT(MSC, msc);
400 TEST_AND_SPRINTF_BIT(LED, led);
401 TEST_AND_SPRINTF_BIT(SND, snd);
402 TEST_AND_SPRINTF_BIT(FF, ff);
403 TEST_AND_SPRINTF_BIT(SW, sw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 len += sprintf(buf + len, "\n");
406
407 at += len;
408
409 if (at >= pos) {
410 if (!*start) {
411 *start = buf + (pos - (at - len));
412 cnt = at - pos;
413 } else cnt += len;
414 buf += len;
415 if (cnt >= count)
416 break;
417 }
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500418
419 kfree(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 if (&dev->node == &input_dev_list)
423 *eof = 1;
424
425 return (count > cnt) ? cnt : count;
426}
427
428static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
429{
430 struct input_handler *handler;
431
432 off_t at = 0;
433 int len = 0, cnt = 0;
434 int i = 0;
435
436 list_for_each_entry(handler, &input_handler_list, node) {
437
438 if (handler->fops)
439 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
440 i++, handler->name, handler->minor);
441 else
442 len = sprintf(buf, "N: Number=%d Name=%s\n",
443 i++, handler->name);
444
445 at += len;
446
447 if (at >= pos) {
448 if (!*start) {
449 *start = buf + (pos - (at - len));
450 cnt = at - pos;
451 } else cnt += len;
452 buf += len;
453 if (cnt >= count)
454 break;
455 }
456 }
457 if (&handler->node == &input_handler_list)
458 *eof = 1;
459
460 return (count > cnt) ? cnt : count;
461}
462
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500463static struct file_operations input_fileops;
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static int __init input_proc_init(void)
466{
467 struct proc_dir_entry *entry;
468
469 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500470 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500476 if (!entry)
477 goto fail1;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 entry->owner = THIS_MODULE;
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500480 input_fileops = *entry->proc_fops;
Arjan van de Vene2bd4702006-01-10 01:59:51 -0500481 input_fileops.poll = input_devices_poll;
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500482 entry->proc_fops = &input_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500485 if (!entry)
486 goto fail2;
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 entry->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500491
492 fail2: remove_proc_entry("devices", proc_bus_input_dir);
493 fail1: remove_proc_entry("input", proc_bus);
494 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500496
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500497static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500498{
499 remove_proc_entry("devices", proc_bus_input_dir);
500 remove_proc_entry("handlers", proc_bus_input_dir);
501 remove_proc_entry("input", proc_bus);
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500505static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500507static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508#endif
509
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500510#define INPUT_DEV_STRING_ATTR_SHOW(name) \
511static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
512{ \
513 struct input_dev *input_dev = to_input_dev(dev); \
514 int retval; \
515 \
Jes Sorensene676c232006-02-19 00:21:46 -0500516 retval = mutex_lock_interruptible(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500517 if (retval) \
518 return retval; \
519 \
520 retval = sprintf(buf, "%s\n", input_dev->name ? input_dev->name : ""); \
521 \
Jes Sorensene676c232006-02-19 00:21:46 -0500522 mutex_unlock(&input_dev->mutex); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500523 \
524 return retval; \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700525} \
526static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500527
528INPUT_DEV_STRING_ATTR_SHOW(name);
529INPUT_DEV_STRING_ATTR_SHOW(phys);
530INPUT_DEV_STRING_ATTR_SHOW(uniq);
531
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100532static int print_modalias_bits(char *buf, int size, char prefix, unsigned long *arr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100533 unsigned int min, unsigned int max)
534{
535 int len, i;
536
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100537 len = snprintf(buf, size, "%c", prefix);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100538 for (i = min; i < max; i++)
539 if (arr[LONG(i)] & BIT(i))
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100540 len += snprintf(buf + len, size - len, "%X,", i);
541 return len;
542}
543
544static int print_modalias(char *buf, int size, struct input_dev *id)
545{
546 int len;
547
548 len = snprintf(buf, size, "input:b%04Xv%04Xp%04Xe%04X-",
549 id->id.bustype,
550 id->id.vendor,
551 id->id.product,
552 id->id.version);
553
554 len += print_modalias_bits(buf + len, size - len, 'e', id->evbit,
555 0, EV_MAX);
556 len += print_modalias_bits(buf + len, size - len, 'k', id->keybit,
557 KEY_MIN_INTERESTING, KEY_MAX);
558 len += print_modalias_bits(buf + len, size - len, 'r', id->relbit,
559 0, REL_MAX);
560 len += print_modalias_bits(buf + len, size - len, 'a', id->absbit,
561 0, ABS_MAX);
562 len += print_modalias_bits(buf + len, size - len, 'm', id->mscbit,
563 0, MSC_MAX);
564 len += print_modalias_bits(buf + len, size - len, 'l', id->ledbit,
565 0, LED_MAX);
566 len += print_modalias_bits(buf + len, size - len, 's', id->sndbit,
567 0, SND_MAX);
568 len += print_modalias_bits(buf + len, size - len, 'f', id->ffbit,
569 0, FF_MAX);
570 len += print_modalias_bits(buf + len, size - len, 'w', id->swbit,
571 0, SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100572 return len;
573}
574
575static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
576{
577 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100578 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100579
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100580 len = print_modalias(buf, PAGE_SIZE, id);
581 len += snprintf(buf + len, PAGE_SIZE-len, "\n");
Rusty Russell1d8f4302005-12-07 21:40:34 +0100582 return len;
583}
584static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
585
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700586static struct attribute *input_dev_attrs[] = {
587 &class_device_attr_name.attr,
588 &class_device_attr_phys.attr,
589 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100590 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700591 NULL
592};
593
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500594static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700595 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500596};
597
598#define INPUT_DEV_ID_ATTR(name) \
599static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
600{ \
601 struct input_dev *input_dev = to_input_dev(dev); \
602 return sprintf(buf, "%04x\n", input_dev->id.name); \
603} \
604static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
605
606INPUT_DEV_ID_ATTR(bustype);
607INPUT_DEV_ID_ATTR(vendor);
608INPUT_DEV_ID_ATTR(product);
609INPUT_DEV_ID_ATTR(version);
610
611static struct attribute *input_dev_id_attrs[] = {
612 &class_device_attr_bustype.attr,
613 &class_device_attr_vendor.attr,
614 &class_device_attr_product.attr,
615 &class_device_attr_version.attr,
616 NULL
617};
618
619static struct attribute_group input_dev_id_attr_group = {
620 .name = "id",
621 .attrs = input_dev_id_attrs,
622};
623
624#define INPUT_DEV_CAP_ATTR(ev, bm) \
625static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
626{ \
627 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500628 return input_print_bitmap(buf, PAGE_SIZE, input_dev->bm##bit, ev##_MAX);\
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500629} \
630static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
631
632INPUT_DEV_CAP_ATTR(EV, ev);
633INPUT_DEV_CAP_ATTR(KEY, key);
634INPUT_DEV_CAP_ATTR(REL, rel);
635INPUT_DEV_CAP_ATTR(ABS, abs);
636INPUT_DEV_CAP_ATTR(MSC, msc);
637INPUT_DEV_CAP_ATTR(LED, led);
638INPUT_DEV_CAP_ATTR(SND, snd);
639INPUT_DEV_CAP_ATTR(FF, ff);
640INPUT_DEV_CAP_ATTR(SW, sw);
641
642static struct attribute *input_dev_caps_attrs[] = {
643 &class_device_attr_ev.attr,
644 &class_device_attr_key.attr,
645 &class_device_attr_rel.attr,
646 &class_device_attr_abs.attr,
647 &class_device_attr_msc.attr,
648 &class_device_attr_led.attr,
649 &class_device_attr_snd.attr,
650 &class_device_attr_ff.attr,
651 &class_device_attr_sw.attr,
652 NULL
653};
654
655static struct attribute_group input_dev_caps_attr_group = {
656 .name = "capabilities",
657 .attrs = input_dev_caps_attrs,
658};
659
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500660static void input_dev_release(struct class_device *class_dev)
661{
662 struct input_dev *dev = to_input_dev(class_dev);
663
664 kfree(dev);
665 module_put(THIS_MODULE);
666}
667
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500668/*
Kay Sievers312c0042005-11-16 09:00:00 +0100669 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500670 * device bitfields.
671 */
Kay Sievers312c0042005-11-16 09:00:00 +0100672static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500673 char *buffer, int buffer_size, int *cur_len,
674 const char *name, unsigned long *bitmap, int max)
675{
676 if (*cur_index >= num_envp - 1)
677 return -ENOMEM;
678
679 envp[*cur_index] = buffer + *cur_len;
680
681 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
682 if (*cur_len > buffer_size)
683 return -ENOMEM;
684
685 *cur_len += input_print_bitmap(buffer + *cur_len,
686 max(buffer_size - *cur_len, 0),
687 bitmap, max) + 1;
688 if (*cur_len > buffer_size)
689 return -ENOMEM;
690
691 (*cur_index)++;
692 return 0;
693}
694
695#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
696 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100697 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500698 buffer, buffer_size, &len, \
699 fmt, val); \
700 if (err) \
701 return err; \
702 } while (0)
703
704#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
705 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100706 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500707 buffer, buffer_size, &len, \
708 name, bm, max); \
709 if (err) \
710 return err; \
711 } while (0)
712
Kay Sievers312c0042005-11-16 09:00:00 +0100713static int input_dev_uevent(struct class_device *cdev, char **envp,
714 int num_envp, char *buffer, int buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500715{
716 struct input_dev *dev = to_input_dev(cdev);
717 int i = 0;
718 int len = 0;
719
720 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
721 dev->id.bustype, dev->id.vendor,
722 dev->id.product, dev->id.version);
723 if (dev->name)
724 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
725 if (dev->phys)
726 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800727 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500728 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
729
730 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
731 if (test_bit(EV_KEY, dev->evbit))
732 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
733 if (test_bit(EV_REL, dev->evbit))
734 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
735 if (test_bit(EV_ABS, dev->evbit))
736 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
737 if (test_bit(EV_MSC, dev->evbit))
738 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
739 if (test_bit(EV_LED, dev->evbit))
740 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
741 if (test_bit(EV_SND, dev->evbit))
742 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
743 if (test_bit(EV_FF, dev->evbit))
744 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
745 if (test_bit(EV_SW, dev->evbit))
746 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
747
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100748 envp[i++] = buffer + len;
749 len += snprintf(buffer + len, buffer_size - len, "MODALIAS=");
750 len += print_modalias(buffer + len, buffer_size - len, dev) + 1;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500751
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100752 envp[i] = NULL;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500753 return 0;
754}
755
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700756struct class input_class = {
757 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500758 .release = input_dev_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100759 .uevent = input_dev_uevent,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500760};
761
762struct input_dev *input_allocate_device(void)
763{
764 struct input_dev *dev;
765
766 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
767 if (dev) {
768 dev->dynalloc = 1;
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700769 dev->cdev.class = &input_class;
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500770 class_device_initialize(&dev->cdev);
771 INIT_LIST_HEAD(&dev->h_list);
772 INIT_LIST_HEAD(&dev->node);
773 }
774
775 return dev;
776}
777
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500778int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500779{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500780 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500781 struct input_handle *handle;
782 struct input_handler *handler;
783 struct input_device_id *id;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500784 const char *path;
785 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500786
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500787 if (!dev->dynalloc) {
788 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
789 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
790 dev->name ? dev->name : "<Unknown>");
791 return -EINVAL;
792 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500793
Jes Sorensene676c232006-02-19 00:21:46 -0500794 mutex_init(&dev->mutex);
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500795 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500796
797 /*
798 * If delay and period are pre-set by the driver, then autorepeating
799 * is handled by the driver itself and we don't do it in input.c.
800 */
801
802 init_timer(&dev->timer);
803 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
804 dev->timer.data = (long) dev;
805 dev->timer.function = input_repeat_key;
806 dev->rep[REP_DELAY] = 250;
807 dev->rep[REP_PERIOD] = 33;
808 }
809
810 INIT_LIST_HEAD(&dev->h_list);
811 list_add_tail(&dev->node, &input_dev_list);
812
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500813 dev->cdev.class = &input_class;
814 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
815 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
816
817 error = class_device_add(&dev->cdev);
818 if (error)
819 return error;
820
821 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
822 if (error)
823 goto fail1;
824
825 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
826 if (error)
827 goto fail2;
828
829 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
830 if (error)
831 goto fail3;
832
833 __module_get(THIS_MODULE);
834
835 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
836 printk(KERN_INFO "input: %s as %s\n",
837 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
838 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -0700839
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500840 list_for_each_entry(handler, &input_handler_list, node)
841 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
842 if ((id = input_match_device(handler->id_table, dev)))
843 if ((handle = handler->connect(handler, dev, id)))
844 input_link_handle(handle);
845
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500846 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500847
848 return 0;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500849
850 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
851 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
852 fail1: class_device_del(&dev->cdev);
853 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500854}
855
856void input_unregister_device(struct input_dev *dev)
857{
858 struct list_head * node, * next;
859
860 if (!dev) return;
861
862 del_timer_sync(&dev->timer);
863
864 list_for_each_safe(node, next, &dev->h_list) {
865 struct input_handle * handle = to_handle(node);
866 list_del_init(&handle->d_node);
867 list_del_init(&handle->h_node);
868 handle->handler->disconnect(handle);
869 }
870
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500871 list_del_init(&dev->node);
872
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500873 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
874 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500875 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500876 class_device_unregister(&dev->cdev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500877
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500878 input_wakeup_procfs_readers();
879}
880
881void input_register_handler(struct input_handler *handler)
882{
883 struct input_dev *dev;
884 struct input_handle *handle;
885 struct input_device_id *id;
886
887 if (!handler) return;
888
889 INIT_LIST_HEAD(&handler->h_list);
890
891 if (handler->fops != NULL)
892 input_table[handler->minor >> 5] = handler;
893
894 list_add_tail(&handler->node, &input_handler_list);
895
896 list_for_each_entry(dev, &input_dev_list, node)
897 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
898 if ((id = input_match_device(handler->id_table, dev)))
899 if ((handle = handler->connect(handler, dev, id)))
900 input_link_handle(handle);
901
902 input_wakeup_procfs_readers();
903}
904
905void input_unregister_handler(struct input_handler *handler)
906{
907 struct list_head * node, * next;
908
909 list_for_each_safe(node, next, &handler->h_list) {
910 struct input_handle * handle = to_handle_h(node);
911 list_del_init(&handle->h_node);
912 list_del_init(&handle->d_node);
913 handler->disconnect(handle);
914 }
915
916 list_del_init(&handler->node);
917
918 if (handler->fops != NULL)
919 input_table[handler->minor >> 5] = NULL;
920
921 input_wakeup_procfs_readers();
922}
923
924static int input_open_file(struct inode *inode, struct file *file)
925{
926 struct input_handler *handler = input_table[iminor(inode) >> 5];
927 struct file_operations *old_fops, *new_fops = NULL;
928 int err;
929
930 /* No load-on-demand here? */
931 if (!handler || !(new_fops = fops_get(handler->fops)))
932 return -ENODEV;
933
934 /*
935 * That's _really_ odd. Usually NULL ->open means "nothing special",
936 * not "no device". Oh, well...
937 */
938 if (!new_fops->open) {
939 fops_put(new_fops);
940 return -ENODEV;
941 }
942 old_fops = file->f_op;
943 file->f_op = new_fops;
944
945 err = new_fops->open(inode, file);
946
947 if (err) {
948 fops_put(file->f_op);
949 file->f_op = fops_get(old_fops);
950 }
951 fops_put(old_fops);
952 return err;
953}
954
955static struct file_operations input_fops = {
956 .owner = THIS_MODULE,
957 .open = input_open_file,
958};
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960static int __init input_init(void)
961{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500962 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700964 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500965 if (err) {
966 printk(KERN_ERR "input: unable to register input_dev class\n");
967 return err;
968 }
969
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500970 err = input_proc_init();
971 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -0700972 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500973
974 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
975 if (err) {
976 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -0700977 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500979
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500980 return 0;
981
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -0700982 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700983 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987static void __exit input_exit(void)
988{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500989 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700991 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994subsys_initcall(input_init);
995module_exit(input_exit);