blob: f8af0945964ecf85147a54712e08b73f237101e1 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
26MODULE_DESCRIPTION("Input core");
27MODULE_LICENSE("GPL");
28
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -050029EXPORT_SYMBOL(input_allocate_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030EXPORT_SYMBOL(input_register_device);
31EXPORT_SYMBOL(input_unregister_device);
32EXPORT_SYMBOL(input_register_handler);
33EXPORT_SYMBOL(input_unregister_handler);
34EXPORT_SYMBOL(input_grab_device);
35EXPORT_SYMBOL(input_release_device);
36EXPORT_SYMBOL(input_open_device);
37EXPORT_SYMBOL(input_close_device);
38EXPORT_SYMBOL(input_accept_process);
39EXPORT_SYMBOL(input_flush_device);
40EXPORT_SYMBOL(input_event);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -070041EXPORT_SYMBOL_GPL(input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define INPUT_DEVICES 256
44
45static LIST_HEAD(input_dev_list);
46static LIST_HEAD(input_handler_list);
47
48static struct input_handler *input_table[8];
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
51{
52 struct input_handle *handle;
53
54 if (type > EV_MAX || !test_bit(type, dev->evbit))
55 return;
56
57 add_input_randomness(type, code, value);
58
59 switch (type) {
60
61 case EV_SYN:
62 switch (code) {
63 case SYN_CONFIG:
64 if (dev->event) dev->event(dev, type, code, value);
65 break;
66
67 case SYN_REPORT:
68 if (dev->sync) return;
69 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
137 if (dev->event) dev->event(dev, type, code, value);
138
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);
147 if (dev->event) dev->event(dev, type, code, value);
148
149 break;
150
151 case EV_SND:
152
153 if (code > SND_MAX || !test_bit(code, dev->sndbit))
154 return;
155
156 if (dev->event) dev->event(dev, type, code, value);
157
158 break;
159
160 case EV_REP:
161
162 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
163
164 dev->rep[code] = value;
165 if (dev->event) dev->event(dev, type, code, value);
166
167 break;
168
169 case EV_FF:
170 if (dev->event) dev->event(dev, type, code, value);
171 break;
172 }
173
174 if (type != EV_SYN)
175 dev->sync = 0;
176
177 if (dev->grab)
178 dev->grab->handler->event(dev->grab, type, code, value);
179 else
180 list_for_each_entry(handle, &dev->h_list, d_node)
181 if (handle->open)
182 handle->handler->event(handle, type, code, value);
183}
184
185static void input_repeat_key(unsigned long data)
186{
187 struct input_dev *dev = (void *) data;
188
189 if (!test_bit(dev->repeat_key, dev->key))
190 return;
191
192 input_event(dev, EV_KEY, dev->repeat_key, 2);
193 input_sync(dev);
194
195 if (dev->rep[REP_PERIOD])
196 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
197}
198
199int input_accept_process(struct input_handle *handle, struct file *file)
200{
201 if (handle->dev->accept)
202 return handle->dev->accept(handle->dev, file);
203
204 return 0;
205}
206
207int input_grab_device(struct input_handle *handle)
208{
209 if (handle->dev->grab)
210 return -EBUSY;
211
212 handle->dev->grab = handle;
213 return 0;
214}
215
216void input_release_device(struct input_handle *handle)
217{
218 if (handle->dev->grab == handle)
219 handle->dev->grab = NULL;
220}
221
222int input_open_device(struct input_handle *handle)
223{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500224 struct input_dev *dev = handle->dev;
225 int err;
226
227 err = down_interruptible(&dev->sem);
228 if (err)
229 return err;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500232
233 if (!dev->users++ && dev->open)
234 err = dev->open(dev);
235
236 if (err)
237 handle->open--;
238
239 up(&dev->sem);
240
241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244int input_flush_device(struct input_handle* handle, struct file* file)
245{
246 if (handle->dev->flush)
247 return handle->dev->flush(handle->dev, file);
248
249 return 0;
250}
251
252void input_close_device(struct input_handle *handle)
253{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500254 struct input_dev *dev = handle->dev;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500257
258 down(&dev->sem);
259
260 if (!--dev->users && dev->close)
261 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500263
264 up(&dev->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267static void input_link_handle(struct input_handle *handle)
268{
269 list_add_tail(&handle->d_node, &handle->dev->h_list);
270 list_add_tail(&handle->h_node, &handle->handler->h_list);
271}
272
273#define MATCH_BIT(bit, max) \
274 for (i = 0; i < NBITS(max); i++) \
275 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
276 break; \
277 if (i != NBITS(max)) \
278 continue;
279
280static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
281{
282 int i;
283
284 for (; id->flags || id->driver_info; id++) {
285
286 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
287 if (id->id.bustype != dev->id.bustype)
288 continue;
289
290 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
291 if (id->id.vendor != dev->id.vendor)
292 continue;
293
294 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
295 if (id->id.product != dev->id.product)
296 continue;
297
298 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
299 if (id->id.version != dev->id.version)
300 continue;
301
302 MATCH_BIT(evbit, EV_MAX);
303 MATCH_BIT(keybit, KEY_MAX);
304 MATCH_BIT(relbit, REL_MAX);
305 MATCH_BIT(absbit, ABS_MAX);
306 MATCH_BIT(mscbit, MSC_MAX);
307 MATCH_BIT(ledbit, LED_MAX);
308 MATCH_BIT(sndbit, SND_MAX);
309 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500310 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 return id;
313 }
314
315 return NULL;
316}
317
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500318static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap, int max)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500319{
320 int i;
321 int len = 0;
322
323 for (i = NBITS(max) - 1; i > 0; i--)
324 if (bitmap[i])
325 break;
326
327 for (; i >= 0; i--)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500328 len += snprintf(buf + len, max(buf_size - len, 0),
329 "%lx%s", bitmap[i], i > 0 ? " " : "");
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500330 return len;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500334
335static struct proc_dir_entry *proc_bus_input_dir;
336static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
337static int input_devices_state;
338
339static inline void input_wakeup_procfs_readers(void)
340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 input_devices_state++;
342 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500345static unsigned int input_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500347 int state = input_devices_state;
348 poll_wait(file, &input_devices_poll_wait, wait);
349 if (state != input_devices_state)
350 return POLLIN | POLLRDNORM;
351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500354#define SPRINTF_BIT(ev, bm) \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500355 do { \
356 len += sprintf(buf + len, "B: %s=", #ev); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500357 len += input_print_bitmap(buf + len, INT_MAX, \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500358 dev->bm##bit, ev##_MAX); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500359 len += sprintf(buf + len, "\n"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 } while (0)
361
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500362#define TEST_AND_SPRINTF_BIT(ev, bm) \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500363 do { \
364 if (test_bit(EV_##ev, dev->evbit)) \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500365 SPRINTF_BIT(ev, bm); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 } while (0)
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
369{
370 struct input_dev *dev;
371 struct input_handle *handle;
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500372 const char *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 off_t at = 0;
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500375 int len, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 list_for_each_entry(dev, &input_dev_list, node) {
378
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500379 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
382 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
383
384 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
385 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500386 len += sprintf(buf + len, "S: Sysfs=%s\n", path ? path : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 len += sprintf(buf + len, "H: Handlers=");
388
389 list_for_each_entry(handle, &dev->h_list, d_node)
390 len += sprintf(buf + len, "%s ", handle->name);
391
392 len += sprintf(buf + len, "\n");
393
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500394 SPRINTF_BIT(EV, ev);
395 TEST_AND_SPRINTF_BIT(KEY, key);
396 TEST_AND_SPRINTF_BIT(REL, rel);
397 TEST_AND_SPRINTF_BIT(ABS, abs);
398 TEST_AND_SPRINTF_BIT(MSC, msc);
399 TEST_AND_SPRINTF_BIT(LED, led);
400 TEST_AND_SPRINTF_BIT(SND, snd);
401 TEST_AND_SPRINTF_BIT(FF, ff);
402 TEST_AND_SPRINTF_BIT(SW, sw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 len += sprintf(buf + len, "\n");
405
406 at += len;
407
408 if (at >= pos) {
409 if (!*start) {
410 *start = buf + (pos - (at - len));
411 cnt = at - pos;
412 } else cnt += len;
413 buf += len;
414 if (cnt >= count)
415 break;
416 }
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500417
418 kfree(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
421 if (&dev->node == &input_dev_list)
422 *eof = 1;
423
424 return (count > cnt) ? cnt : count;
425}
426
427static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
428{
429 struct input_handler *handler;
430
431 off_t at = 0;
432 int len = 0, cnt = 0;
433 int i = 0;
434
435 list_for_each_entry(handler, &input_handler_list, node) {
436
437 if (handler->fops)
438 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
439 i++, handler->name, handler->minor);
440 else
441 len = sprintf(buf, "N: Number=%d Name=%s\n",
442 i++, handler->name);
443
444 at += len;
445
446 if (at >= pos) {
447 if (!*start) {
448 *start = buf + (pos - (at - len));
449 cnt = at - pos;
450 } else cnt += len;
451 buf += len;
452 if (cnt >= count)
453 break;
454 }
455 }
456 if (&handler->node == &input_handler_list)
457 *eof = 1;
458
459 return (count > cnt) ? cnt : count;
460}
461
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500462static struct file_operations input_fileops;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static int __init input_proc_init(void)
465{
466 struct proc_dir_entry *entry;
467
468 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500469 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500475 if (!entry)
476 goto fail1;
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 entry->owner = THIS_MODULE;
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500479 input_fileops = *entry->proc_fops;
Arjan van de Vene2bd4702006-01-10 01:59:51 -0500480 input_fileops.poll = input_devices_poll;
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500481 entry->proc_fops = &input_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500484 if (!entry)
485 goto fail2;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 entry->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500490
491 fail2: remove_proc_entry("devices", proc_bus_input_dir);
492 fail1: remove_proc_entry("input", proc_bus);
493 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500495
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500496static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500497{
498 remove_proc_entry("devices", proc_bus_input_dir);
499 remove_proc_entry("handlers", proc_bus_input_dir);
500 remove_proc_entry("input", proc_bus);
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500504static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500506static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507#endif
508
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500509#define INPUT_DEV_STRING_ATTR_SHOW(name) \
510static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
511{ \
512 struct input_dev *input_dev = to_input_dev(dev); \
513 int retval; \
514 \
515 retval = down_interruptible(&input_dev->sem); \
516 if (retval) \
517 return retval; \
518 \
519 retval = sprintf(buf, "%s\n", input_dev->name ? input_dev->name : ""); \
520 \
521 up(&input_dev->sem); \
522 \
523 return retval; \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700524} \
525static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500526
527INPUT_DEV_STRING_ATTR_SHOW(name);
528INPUT_DEV_STRING_ATTR_SHOW(phys);
529INPUT_DEV_STRING_ATTR_SHOW(uniq);
530
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100531static int print_modalias_bits(char *buf, int size, char prefix, unsigned long *arr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100532 unsigned int min, unsigned int max)
533{
534 int len, i;
535
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100536 len = snprintf(buf, size, "%c", prefix);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100537 for (i = min; i < max; i++)
538 if (arr[LONG(i)] & BIT(i))
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100539 len += snprintf(buf + len, size - len, "%X,", i);
540 return len;
541}
542
543static int print_modalias(char *buf, int size, struct input_dev *id)
544{
545 int len;
546
547 len = snprintf(buf, size, "input:b%04Xv%04Xp%04Xe%04X-",
548 id->id.bustype,
549 id->id.vendor,
550 id->id.product,
551 id->id.version);
552
553 len += print_modalias_bits(buf + len, size - len, 'e', id->evbit,
554 0, EV_MAX);
555 len += print_modalias_bits(buf + len, size - len, 'k', id->keybit,
556 KEY_MIN_INTERESTING, KEY_MAX);
557 len += print_modalias_bits(buf + len, size - len, 'r', id->relbit,
558 0, REL_MAX);
559 len += print_modalias_bits(buf + len, size - len, 'a', id->absbit,
560 0, ABS_MAX);
561 len += print_modalias_bits(buf + len, size - len, 'm', id->mscbit,
562 0, MSC_MAX);
563 len += print_modalias_bits(buf + len, size - len, 'l', id->ledbit,
564 0, LED_MAX);
565 len += print_modalias_bits(buf + len, size - len, 's', id->sndbit,
566 0, SND_MAX);
567 len += print_modalias_bits(buf + len, size - len, 'f', id->ffbit,
568 0, FF_MAX);
569 len += print_modalias_bits(buf + len, size - len, 'w', id->swbit,
570 0, SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100571 return len;
572}
573
574static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
575{
576 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100577 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100578
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100579 len = print_modalias(buf, PAGE_SIZE, id);
580 len += snprintf(buf + len, PAGE_SIZE-len, "\n");
Rusty Russell1d8f4302005-12-07 21:40:34 +0100581 return len;
582}
583static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
584
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700585static struct attribute *input_dev_attrs[] = {
586 &class_device_attr_name.attr,
587 &class_device_attr_phys.attr,
588 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100589 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700590 NULL
591};
592
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500593static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700594 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500595};
596
597#define INPUT_DEV_ID_ATTR(name) \
598static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
599{ \
600 struct input_dev *input_dev = to_input_dev(dev); \
601 return sprintf(buf, "%04x\n", input_dev->id.name); \
602} \
603static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
604
605INPUT_DEV_ID_ATTR(bustype);
606INPUT_DEV_ID_ATTR(vendor);
607INPUT_DEV_ID_ATTR(product);
608INPUT_DEV_ID_ATTR(version);
609
610static struct attribute *input_dev_id_attrs[] = {
611 &class_device_attr_bustype.attr,
612 &class_device_attr_vendor.attr,
613 &class_device_attr_product.attr,
614 &class_device_attr_version.attr,
615 NULL
616};
617
618static struct attribute_group input_dev_id_attr_group = {
619 .name = "id",
620 .attrs = input_dev_id_attrs,
621};
622
623#define INPUT_DEV_CAP_ATTR(ev, bm) \
624static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
625{ \
626 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500627 return input_print_bitmap(buf, PAGE_SIZE, input_dev->bm##bit, ev##_MAX);\
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500628} \
629static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
630
631INPUT_DEV_CAP_ATTR(EV, ev);
632INPUT_DEV_CAP_ATTR(KEY, key);
633INPUT_DEV_CAP_ATTR(REL, rel);
634INPUT_DEV_CAP_ATTR(ABS, abs);
635INPUT_DEV_CAP_ATTR(MSC, msc);
636INPUT_DEV_CAP_ATTR(LED, led);
637INPUT_DEV_CAP_ATTR(SND, snd);
638INPUT_DEV_CAP_ATTR(FF, ff);
639INPUT_DEV_CAP_ATTR(SW, sw);
640
641static struct attribute *input_dev_caps_attrs[] = {
642 &class_device_attr_ev.attr,
643 &class_device_attr_key.attr,
644 &class_device_attr_rel.attr,
645 &class_device_attr_abs.attr,
646 &class_device_attr_msc.attr,
647 &class_device_attr_led.attr,
648 &class_device_attr_snd.attr,
649 &class_device_attr_ff.attr,
650 &class_device_attr_sw.attr,
651 NULL
652};
653
654static struct attribute_group input_dev_caps_attr_group = {
655 .name = "capabilities",
656 .attrs = input_dev_caps_attrs,
657};
658
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500659static void input_dev_release(struct class_device *class_dev)
660{
661 struct input_dev *dev = to_input_dev(class_dev);
662
663 kfree(dev);
664 module_put(THIS_MODULE);
665}
666
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500667/*
Kay Sievers312c0042005-11-16 09:00:00 +0100668 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500669 * device bitfields.
670 */
Kay Sievers312c0042005-11-16 09:00:00 +0100671static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500672 char *buffer, int buffer_size, int *cur_len,
673 const char *name, unsigned long *bitmap, int max)
674{
675 if (*cur_index >= num_envp - 1)
676 return -ENOMEM;
677
678 envp[*cur_index] = buffer + *cur_len;
679
680 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
681 if (*cur_len > buffer_size)
682 return -ENOMEM;
683
684 *cur_len += input_print_bitmap(buffer + *cur_len,
685 max(buffer_size - *cur_len, 0),
686 bitmap, max) + 1;
687 if (*cur_len > buffer_size)
688 return -ENOMEM;
689
690 (*cur_index)++;
691 return 0;
692}
693
694#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
695 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100696 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500697 buffer, buffer_size, &len, \
698 fmt, val); \
699 if (err) \
700 return err; \
701 } while (0)
702
703#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
704 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100705 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500706 buffer, buffer_size, &len, \
707 name, bm, max); \
708 if (err) \
709 return err; \
710 } while (0)
711
Kay Sievers312c0042005-11-16 09:00:00 +0100712static int input_dev_uevent(struct class_device *cdev, char **envp,
713 int num_envp, char *buffer, int buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500714{
715 struct input_dev *dev = to_input_dev(cdev);
716 int i = 0;
717 int len = 0;
718
719 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
720 dev->id.bustype, dev->id.vendor,
721 dev->id.product, dev->id.version);
722 if (dev->name)
723 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
724 if (dev->phys)
725 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800726 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500727 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
728
729 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
730 if (test_bit(EV_KEY, dev->evbit))
731 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
732 if (test_bit(EV_REL, dev->evbit))
733 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
734 if (test_bit(EV_ABS, dev->evbit))
735 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
736 if (test_bit(EV_MSC, dev->evbit))
737 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
738 if (test_bit(EV_LED, dev->evbit))
739 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
740 if (test_bit(EV_SND, dev->evbit))
741 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
742 if (test_bit(EV_FF, dev->evbit))
743 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
744 if (test_bit(EV_SW, dev->evbit))
745 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
746
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100747 envp[i++] = buffer + len;
748 len += snprintf(buffer + len, buffer_size - len, "MODALIAS=");
749 len += print_modalias(buffer + len, buffer_size - len, dev) + 1;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500750
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100751 envp[i] = NULL;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500752 return 0;
753}
754
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700755struct class input_class = {
756 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500757 .release = input_dev_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100758 .uevent = input_dev_uevent,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500759};
760
761struct input_dev *input_allocate_device(void)
762{
763 struct input_dev *dev;
764
765 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
766 if (dev) {
767 dev->dynalloc = 1;
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700768 dev->cdev.class = &input_class;
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500769 class_device_initialize(&dev->cdev);
770 INIT_LIST_HEAD(&dev->h_list);
771 INIT_LIST_HEAD(&dev->node);
772 }
773
774 return dev;
775}
776
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500777int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500778{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500779 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500780 struct input_handle *handle;
781 struct input_handler *handler;
782 struct input_device_id *id;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500783 const char *path;
784 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500785
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500786 if (!dev->dynalloc) {
787 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
788 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
789 dev->name ? dev->name : "<Unknown>");
790 return -EINVAL;
791 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500792
793 init_MUTEX(&dev->sem);
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500794 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500795
796 /*
797 * If delay and period are pre-set by the driver, then autorepeating
798 * is handled by the driver itself and we don't do it in input.c.
799 */
800
801 init_timer(&dev->timer);
802 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
803 dev->timer.data = (long) dev;
804 dev->timer.function = input_repeat_key;
805 dev->rep[REP_DELAY] = 250;
806 dev->rep[REP_PERIOD] = 33;
807 }
808
809 INIT_LIST_HEAD(&dev->h_list);
810 list_add_tail(&dev->node, &input_dev_list);
811
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500812 dev->cdev.class = &input_class;
813 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
814 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
815
816 error = class_device_add(&dev->cdev);
817 if (error)
818 return error;
819
820 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
821 if (error)
822 goto fail1;
823
824 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
825 if (error)
826 goto fail2;
827
828 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
829 if (error)
830 goto fail3;
831
832 __module_get(THIS_MODULE);
833
834 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
835 printk(KERN_INFO "input: %s as %s\n",
836 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
837 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -0700838
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500839 list_for_each_entry(handler, &input_handler_list, node)
840 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
841 if ((id = input_match_device(handler->id_table, dev)))
842 if ((handle = handler->connect(handler, dev, id)))
843 input_link_handle(handle);
844
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500845 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500846
847 return 0;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500848
849 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
850 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
851 fail1: class_device_del(&dev->cdev);
852 return error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500853}
854
855void input_unregister_device(struct input_dev *dev)
856{
857 struct list_head * node, * next;
858
859 if (!dev) return;
860
861 del_timer_sync(&dev->timer);
862
863 list_for_each_safe(node, next, &dev->h_list) {
864 struct input_handle * handle = to_handle(node);
865 list_del_init(&handle->d_node);
866 list_del_init(&handle->h_node);
867 handle->handler->disconnect(handle);
868 }
869
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500870 list_del_init(&dev->node);
871
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500872 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
873 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500874 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
Dmitry Torokhov5f945482005-11-02 22:51:46 -0500875 class_device_unregister(&dev->cdev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500876
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500877 input_wakeup_procfs_readers();
878}
879
880void input_register_handler(struct input_handler *handler)
881{
882 struct input_dev *dev;
883 struct input_handle *handle;
884 struct input_device_id *id;
885
886 if (!handler) return;
887
888 INIT_LIST_HEAD(&handler->h_list);
889
890 if (handler->fops != NULL)
891 input_table[handler->minor >> 5] = handler;
892
893 list_add_tail(&handler->node, &input_handler_list);
894
895 list_for_each_entry(dev, &input_dev_list, node)
896 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
897 if ((id = input_match_device(handler->id_table, dev)))
898 if ((handle = handler->connect(handler, dev, id)))
899 input_link_handle(handle);
900
901 input_wakeup_procfs_readers();
902}
903
904void input_unregister_handler(struct input_handler *handler)
905{
906 struct list_head * node, * next;
907
908 list_for_each_safe(node, next, &handler->h_list) {
909 struct input_handle * handle = to_handle_h(node);
910 list_del_init(&handle->h_node);
911 list_del_init(&handle->d_node);
912 handler->disconnect(handle);
913 }
914
915 list_del_init(&handler->node);
916
917 if (handler->fops != NULL)
918 input_table[handler->minor >> 5] = NULL;
919
920 input_wakeup_procfs_readers();
921}
922
923static int input_open_file(struct inode *inode, struct file *file)
924{
925 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800926 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500927 int err;
928
929 /* No load-on-demand here? */
930 if (!handler || !(new_fops = fops_get(handler->fops)))
931 return -ENODEV;
932
933 /*
934 * That's _really_ odd. Usually NULL ->open means "nothing special",
935 * not "no device". Oh, well...
936 */
937 if (!new_fops->open) {
938 fops_put(new_fops);
939 return -ENODEV;
940 }
941 old_fops = file->f_op;
942 file->f_op = new_fops;
943
944 err = new_fops->open(inode, file);
945
946 if (err) {
947 fops_put(file->f_op);
948 file->f_op = fops_get(old_fops);
949 }
950 fops_put(old_fops);
951 return err;
952}
953
954static struct file_operations input_fops = {
955 .owner = THIS_MODULE,
956 .open = input_open_file,
957};
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959static int __init input_init(void)
960{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500961 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700963 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500964 if (err) {
965 printk(KERN_ERR "input: unable to register input_dev class\n");
966 return err;
967 }
968
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500969 err = input_proc_init();
970 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -0700971 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500972
973 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
974 if (err) {
975 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -0700976 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500978
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500979 return 0;
980
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -0700981 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700982 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500983 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986static void __exit input_exit(void)
987{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500988 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700990 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
993subsys_initcall(input_init);
994module_exit(input_exit);