]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/input/input.c
Input: reset name, phys and uniq when unregistering
[mirror_ubuntu-artful-kernel.git] / drivers / input / input.c
CommitLineData
1da177e4
LT
1/*
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>
969b21cd 21#include <linux/seq_file.h>
1da177e4
LT
22#include <linux/interrupt.h>
23#include <linux/poll.h>
24#include <linux/device.h>
e676c232 25#include <linux/mutex.h>
1da177e4
LT
26
27MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28MODULE_DESCRIPTION("Input core");
29MODULE_LICENSE("GPL");
30
d19fbe8a 31EXPORT_SYMBOL(input_allocate_device);
f60d2b11 32EXPORT_SYMBOL(input_free_device);
1da177e4
LT
33EXPORT_SYMBOL(input_register_device);
34EXPORT_SYMBOL(input_unregister_device);
35EXPORT_SYMBOL(input_register_handler);
36EXPORT_SYMBOL(input_unregister_handler);
37EXPORT_SYMBOL(input_grab_device);
38EXPORT_SYMBOL(input_release_device);
39EXPORT_SYMBOL(input_open_device);
40EXPORT_SYMBOL(input_close_device);
41EXPORT_SYMBOL(input_accept_process);
42EXPORT_SYMBOL(input_flush_device);
43EXPORT_SYMBOL(input_event);
ea9f240b 44EXPORT_SYMBOL_GPL(input_class);
1da177e4
LT
45
46#define INPUT_DEVICES 256
47
48static LIST_HEAD(input_dev_list);
49static LIST_HEAD(input_handler_list);
50
51static struct input_handler *input_table[8];
52
1da177e4
LT
53void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
54{
55 struct input_handle *handle;
56
57 if (type > EV_MAX || !test_bit(type, dev->evbit))
58 return;
59
60 add_input_randomness(type, code, value);
61
62 switch (type) {
63
64 case EV_SYN:
65 switch (code) {
66 case SYN_CONFIG:
67 if (dev->event) dev->event(dev, type, code, value);
68 break;
69
70 case SYN_REPORT:
71 if (dev->sync) return;
72 dev->sync = 1;
73 break;
74 }
75 break;
76
77 case EV_KEY:
78
79 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
80 return;
81
82 if (value == 2)
83 break;
84
85 change_bit(code, dev->key);
86
87 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
88 dev->repeat_key = code;
89 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
90 }
91
92 break;
93
31581066
RP
94 case EV_SW:
95
96 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
97 return;
98
99 change_bit(code, dev->sw);
100
101 break;
102
1da177e4
LT
103 case EV_ABS:
104
105 if (code > ABS_MAX || !test_bit(code, dev->absbit))
106 return;
107
108 if (dev->absfuzz[code]) {
109 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
110 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
111 return;
112
113 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
114 (value < dev->abs[code] + dev->absfuzz[code]))
115 value = (dev->abs[code] * 3 + value) >> 2;
116
117 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
118 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
119 value = (dev->abs[code] + value) >> 1;
120 }
121
122 if (dev->abs[code] == value)
123 return;
124
125 dev->abs[code] = value;
126 break;
127
128 case EV_REL:
129
130 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
131 return;
132
133 break;
134
135 case EV_MSC:
136
137 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
138 return;
139
140 if (dev->event) dev->event(dev, type, code, value);
141
142 break;
143
144 case EV_LED:
145
146 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
147 return;
148
149 change_bit(code, dev->led);
150 if (dev->event) dev->event(dev, type, code, value);
151
152 break;
153
154 case EV_SND:
155
156 if (code > SND_MAX || !test_bit(code, dev->sndbit))
157 return;
158
8fdc1948
DT
159 if (!!test_bit(code, dev->snd) != !!value)
160 change_bit(code, dev->snd);
161
1da177e4
LT
162 if (dev->event) dev->event(dev, type, code, value);
163
164 break;
165
166 case EV_REP:
167
168 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
169
170 dev->rep[code] = value;
171 if (dev->event) dev->event(dev, type, code, value);
172
173 break;
174
175 case EV_FF:
176 if (dev->event) dev->event(dev, type, code, value);
177 break;
178 }
179
180 if (type != EV_SYN)
181 dev->sync = 0;
182
183 if (dev->grab)
184 dev->grab->handler->event(dev->grab, type, code, value);
185 else
186 list_for_each_entry(handle, &dev->h_list, d_node)
187 if (handle->open)
188 handle->handler->event(handle, type, code, value);
189}
190
191static void input_repeat_key(unsigned long data)
192{
193 struct input_dev *dev = (void *) data;
194
195 if (!test_bit(dev->repeat_key, dev->key))
196 return;
197
198 input_event(dev, EV_KEY, dev->repeat_key, 2);
199 input_sync(dev);
200
201 if (dev->rep[REP_PERIOD])
202 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
203}
204
205int input_accept_process(struct input_handle *handle, struct file *file)
206{
207 if (handle->dev->accept)
208 return handle->dev->accept(handle->dev, file);
209
210 return 0;
211}
212
213int input_grab_device(struct input_handle *handle)
214{
215 if (handle->dev->grab)
216 return -EBUSY;
217
218 handle->dev->grab = handle;
219 return 0;
220}
221
222void input_release_device(struct input_handle *handle)
223{
224 if (handle->dev->grab == handle)
225 handle->dev->grab = NULL;
226}
227
228int input_open_device(struct input_handle *handle)
229{
0fbf87ca
DT
230 struct input_dev *dev = handle->dev;
231 int err;
232
e676c232 233 err = mutex_lock_interruptible(&dev->mutex);
0fbf87ca
DT
234 if (err)
235 return err;
236
1da177e4 237 handle->open++;
0fbf87ca
DT
238
239 if (!dev->users++ && dev->open)
240 err = dev->open(dev);
241
242 if (err)
243 handle->open--;
244
e676c232 245 mutex_unlock(&dev->mutex);
0fbf87ca
DT
246
247 return err;
1da177e4
LT
248}
249
250int input_flush_device(struct input_handle* handle, struct file* file)
251{
252 if (handle->dev->flush)
253 return handle->dev->flush(handle->dev, file);
254
255 return 0;
256}
257
258void input_close_device(struct input_handle *handle)
259{
0fbf87ca
DT
260 struct input_dev *dev = handle->dev;
261
1da177e4 262 input_release_device(handle);
0fbf87ca 263
e676c232 264 mutex_lock(&dev->mutex);
0fbf87ca
DT
265
266 if (!--dev->users && dev->close)
267 dev->close(dev);
1da177e4 268 handle->open--;
0fbf87ca 269
e676c232 270 mutex_unlock(&dev->mutex);
1da177e4
LT
271}
272
273static void input_link_handle(struct input_handle *handle)
274{
275 list_add_tail(&handle->d_node, &handle->dev->h_list);
276 list_add_tail(&handle->h_node, &handle->handler->h_list);
277}
278
279#define MATCH_BIT(bit, max) \
280 for (i = 0; i < NBITS(max); i++) \
281 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
282 break; \
283 if (i != NBITS(max)) \
284 continue;
285
286static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
287{
288 int i;
289
290 for (; id->flags || id->driver_info; id++) {
291
292 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
ddc5d341 293 if (id->bustype != dev->id.bustype)
1da177e4
LT
294 continue;
295
296 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
ddc5d341 297 if (id->vendor != dev->id.vendor)
1da177e4
LT
298 continue;
299
300 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
ddc5d341 301 if (id->product != dev->id.product)
1da177e4
LT
302 continue;
303
304 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
ddc5d341 305 if (id->version != dev->id.version)
1da177e4
LT
306 continue;
307
308 MATCH_BIT(evbit, EV_MAX);
309 MATCH_BIT(keybit, KEY_MAX);
310 MATCH_BIT(relbit, REL_MAX);
311 MATCH_BIT(absbit, ABS_MAX);
312 MATCH_BIT(mscbit, MSC_MAX);
313 MATCH_BIT(ledbit, LED_MAX);
314 MATCH_BIT(sndbit, SND_MAX);
315 MATCH_BIT(ffbit, FF_MAX);
ff13f98b 316 MATCH_BIT(swbit, SW_MAX);
1da177e4
LT
317
318 return id;
319 }
320
321 return NULL;
322}
323
f96b434d
DT
324#ifdef CONFIG_PROC_FS
325
326static struct proc_dir_entry *proc_bus_input_dir;
327static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
328static int input_devices_state;
329
330static inline void input_wakeup_procfs_readers(void)
331{
332 input_devices_state++;
333 wake_up(&input_devices_poll_wait);
334}
335
969b21cd 336static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
f96b434d
DT
337{
338 int state = input_devices_state;
339 poll_wait(file, &input_devices_poll_wait, wait);
340 if (state != input_devices_state)
341 return POLLIN | POLLRDNORM;
342 return 0;
343}
344
969b21cd
DT
345static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
346{
347 struct list_head *node;
348 loff_t i = 0;
f96b434d 349
969b21cd
DT
350 list_for_each(node, list)
351 if (i++ == *pos)
352 return node;
353
354 return NULL;
355}
f96b434d 356
969b21cd 357static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
f96b434d 358{
969b21cd
DT
359 if (element->next == list)
360 return NULL;
f96b434d 361
969b21cd
DT
362 ++(*pos);
363 return element->next;
364}
f96b434d 365
969b21cd
DT
366static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
367{
368 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
f96b434d 369
969b21cd
DT
370 return list_get_nth_element(&input_dev_list, pos);
371}
051b2fea 372
969b21cd
DT
373static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
374{
375 return list_get_next_element(&input_dev_list, v, pos);
376}
f96b434d 377
969b21cd
DT
378static void input_devices_seq_stop(struct seq_file *seq, void *v)
379{
380 /* release lock here */
381}
f96b434d 382
969b21cd
DT
383static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
384 unsigned long *bitmap, int max)
385{
386 int i;
051b2fea 387
969b21cd
DT
388 for (i = NBITS(max) - 1; i > 0; i--)
389 if (bitmap[i])
390 break;
f96b434d 391
969b21cd
DT
392 seq_printf(seq, "B: %s=", name);
393 for (; i >= 0; i--)
394 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
395 seq_putc(seq, '\n');
396}
f96b434d 397
969b21cd
DT
398static int input_devices_seq_show(struct seq_file *seq, void *v)
399{
400 struct input_dev *dev = container_of(v, struct input_dev, node);
401 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
402 struct input_handle *handle;
403
404 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
405 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
406
407 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
408 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
409 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
410 seq_printf(seq, "H: Handlers=");
411
412 list_for_each_entry(handle, &dev->h_list, d_node)
413 seq_printf(seq, "%s ", handle->name);
414 seq_putc(seq, '\n');
415
416 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
417 if (test_bit(EV_KEY, dev->evbit))
418 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
419 if (test_bit(EV_REL, dev->evbit))
420 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
421 if (test_bit(EV_ABS, dev->evbit))
422 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
423 if (test_bit(EV_MSC, dev->evbit))
424 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
425 if (test_bit(EV_LED, dev->evbit))
426 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
427 if (test_bit(EV_SND, dev->evbit))
428 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
429 if (test_bit(EV_FF, dev->evbit))
430 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
431 if (test_bit(EV_SW, dev->evbit))
432 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
433
434 seq_putc(seq, '\n');
435
436 kfree(path);
437 return 0;
f96b434d
DT
438}
439
969b21cd
DT
440static struct seq_operations input_devices_seq_ops = {
441 .start = input_devices_seq_start,
442 .next = input_devices_seq_next,
443 .stop = input_devices_seq_stop,
444 .show = input_devices_seq_show,
445};
446
447static int input_proc_devices_open(struct inode *inode, struct file *file)
f96b434d 448{
969b21cd
DT
449 return seq_open(file, &input_devices_seq_ops);
450}
451
452static struct file_operations input_devices_fileops = {
453 .owner = THIS_MODULE,
454 .open = input_proc_devices_open,
455 .poll = input_proc_devices_poll,
456 .read = seq_read,
457 .llseek = seq_lseek,
458 .release = seq_release,
459};
460
461static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
462{
463 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
464 seq->private = (void *)(unsigned long)*pos;
465 return list_get_nth_element(&input_handler_list, pos);
466}
f96b434d 467
969b21cd
DT
468static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
469{
470 seq->private = (void *)(unsigned long)(*pos + 1);
471 return list_get_next_element(&input_handler_list, v, pos);
f96b434d
DT
472}
473
969b21cd
DT
474static void input_handlers_seq_stop(struct seq_file *seq, void *v)
475{
476 /* release lock here */
477}
478
479static int input_handlers_seq_show(struct seq_file *seq, void *v)
480{
481 struct input_handler *handler = container_of(v, struct input_handler, node);
482
483 seq_printf(seq, "N: Number=%ld Name=%s",
484 (unsigned long)seq->private, handler->name);
485 if (handler->fops)
486 seq_printf(seq, " Minor=%d", handler->minor);
487 seq_putc(seq, '\n');
488
489 return 0;
490}
491static struct seq_operations input_handlers_seq_ops = {
492 .start = input_handlers_seq_start,
493 .next = input_handlers_seq_next,
494 .stop = input_handlers_seq_stop,
495 .show = input_handlers_seq_show,
496};
497
498static int input_proc_handlers_open(struct inode *inode, struct file *file)
499{
500 return seq_open(file, &input_handlers_seq_ops);
501}
502
503static struct file_operations input_handlers_fileops = {
504 .owner = THIS_MODULE,
505 .open = input_proc_handlers_open,
506 .read = seq_read,
507 .llseek = seq_lseek,
508 .release = seq_release,
509};
f96b434d
DT
510
511static int __init input_proc_init(void)
512{
513 struct proc_dir_entry *entry;
514
515 proc_bus_input_dir = proc_mkdir("input", proc_bus);
516 if (!proc_bus_input_dir)
517 return -ENOMEM;
518
519 proc_bus_input_dir->owner = THIS_MODULE;
520
969b21cd 521 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
f96b434d
DT
522 if (!entry)
523 goto fail1;
524
525 entry->owner = THIS_MODULE;
969b21cd 526 entry->proc_fops = &input_devices_fileops;
f96b434d 527
969b21cd 528 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
f96b434d
DT
529 if (!entry)
530 goto fail2;
531
532 entry->owner = THIS_MODULE;
969b21cd 533 entry->proc_fops = &input_handlers_fileops;
f96b434d
DT
534
535 return 0;
536
537 fail2: remove_proc_entry("devices", proc_bus_input_dir);
538 fail1: remove_proc_entry("input", proc_bus);
539 return -ENOMEM;
540}
541
beffbdc2 542static void input_proc_exit(void)
f96b434d
DT
543{
544 remove_proc_entry("devices", proc_bus_input_dir);
545 remove_proc_entry("handlers", proc_bus_input_dir);
546 remove_proc_entry("input", proc_bus);
547}
548
549#else /* !CONFIG_PROC_FS */
550static inline void input_wakeup_procfs_readers(void) { }
551static inline int input_proc_init(void) { return 0; }
552static inline void input_proc_exit(void) { }
553#endif
554
5c1e9a6a
DT
555#define INPUT_DEV_STRING_ATTR_SHOW(name) \
556static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
557{ \
558 struct input_dev *input_dev = to_input_dev(dev); \
559 int retval; \
560 \
e676c232 561 retval = mutex_lock_interruptible(&input_dev->mutex); \
5c1e9a6a
DT
562 if (retval) \
563 return retval; \
564 \
2db66876
DT
565 retval = scnprintf(buf, PAGE_SIZE, \
566 "%s\n", input_dev->name ? input_dev->name : ""); \
5c1e9a6a 567 \
e676c232 568 mutex_unlock(&input_dev->mutex); \
5c1e9a6a
DT
569 \
570 return retval; \
629b77a4
GKH
571} \
572static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
5c1e9a6a
DT
573
574INPUT_DEV_STRING_ATTR_SHOW(name);
575INPUT_DEV_STRING_ATTR_SHOW(phys);
576INPUT_DEV_STRING_ATTR_SHOW(uniq);
577
ac648a6a
DT
578static int input_print_modalias_bits(char *buf, int size,
579 char name, unsigned long *bm,
580 unsigned int min_bit, unsigned int max_bit)
1d8f430c 581{
ac648a6a 582 int len = 0, i;
1d8f430c 583
ac648a6a
DT
584 len += snprintf(buf, max(size, 0), "%c", name);
585 for (i = min_bit; i < max_bit; i++)
586 if (bm[LONG(i)] & BIT(i))
587 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
1d8f430c
RR
588 return len;
589}
590
2db66876
DT
591static int input_print_modalias(char *buf, int size, struct input_dev *id,
592 int add_cr)
1d8f430c 593{
bd37e5a9 594 int len;
1d8f430c 595
ac648a6a
DT
596 len = snprintf(buf, max(size, 0),
597 "input:b%04Xv%04Xp%04Xe%04X-",
598 id->id.bustype, id->id.vendor,
599 id->id.product, id->id.version);
600
601 len += input_print_modalias_bits(buf + len, size - len,
602 'e', id->evbit, 0, EV_MAX);
603 len += input_print_modalias_bits(buf + len, size - len,
604 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
605 len += input_print_modalias_bits(buf + len, size - len,
606 'r', id->relbit, 0, REL_MAX);
607 len += input_print_modalias_bits(buf + len, size - len,
608 'a', id->absbit, 0, ABS_MAX);
609 len += input_print_modalias_bits(buf + len, size - len,
610 'm', id->mscbit, 0, MSC_MAX);
611 len += input_print_modalias_bits(buf + len, size - len,
612 'l', id->ledbit, 0, LED_MAX);
613 len += input_print_modalias_bits(buf + len, size - len,
614 's', id->sndbit, 0, SND_MAX);
615 len += input_print_modalias_bits(buf + len, size - len,
616 'f', id->ffbit, 0, FF_MAX);
617 len += input_print_modalias_bits(buf + len, size - len,
618 'w', id->swbit, 0, SW_MAX);
2db66876
DT
619
620 if (add_cr)
ac648a6a 621 len += snprintf(buf + len, max(size - len, 0), "\n");
2db66876 622
bd37e5a9
KS
623 return len;
624}
625
626static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
627{
628 struct input_dev *id = to_input_dev(dev);
629 ssize_t len;
630
2db66876
DT
631 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
632
8a3cf456 633 return min_t(int, len, PAGE_SIZE);
1d8f430c
RR
634}
635static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
636
629b77a4
GKH
637static struct attribute *input_dev_attrs[] = {
638 &class_device_attr_name.attr,
639 &class_device_attr_phys.attr,
640 &class_device_attr_uniq.attr,
1d8f430c 641 &class_device_attr_modalias.attr,
629b77a4
GKH
642 NULL
643};
644
bd0ef235 645static struct attribute_group input_dev_attr_group = {
629b77a4 646 .attrs = input_dev_attrs,
5c1e9a6a
DT
647};
648
649#define INPUT_DEV_ID_ATTR(name) \
650static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
651{ \
652 struct input_dev *input_dev = to_input_dev(dev); \
2db66876 653 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
5c1e9a6a
DT
654} \
655static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
656
657INPUT_DEV_ID_ATTR(bustype);
658INPUT_DEV_ID_ATTR(vendor);
659INPUT_DEV_ID_ATTR(product);
660INPUT_DEV_ID_ATTR(version);
661
662static struct attribute *input_dev_id_attrs[] = {
663 &class_device_attr_bustype.attr,
664 &class_device_attr_vendor.attr,
665 &class_device_attr_product.attr,
666 &class_device_attr_version.attr,
667 NULL
668};
669
670static struct attribute_group input_dev_id_attr_group = {
671 .name = "id",
672 .attrs = input_dev_id_attrs,
673};
674
969b21cd
DT
675static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
676 int max, int add_cr)
677{
678 int i;
679 int len = 0;
680
681 for (i = NBITS(max) - 1; i > 0; i--)
682 if (bitmap[i])
683 break;
684
685 for (; i >= 0; i--)
686 len += snprintf(buf + len, max(buf_size - len, 0),
687 "%lx%s", bitmap[i], i > 0 ? " " : "");
688
689 if (add_cr)
690 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
691
692 return len;
693}
694
5c1e9a6a
DT
695#define INPUT_DEV_CAP_ATTR(ev, bm) \
696static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
697{ \
698 struct input_dev *input_dev = to_input_dev(dev); \
2db66876
DT
699 int len = input_print_bitmap(buf, PAGE_SIZE, \
700 input_dev->bm##bit, ev##_MAX, 1); \
701 return min_t(int, len, PAGE_SIZE); \
5c1e9a6a
DT
702} \
703static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
704
705INPUT_DEV_CAP_ATTR(EV, ev);
706INPUT_DEV_CAP_ATTR(KEY, key);
707INPUT_DEV_CAP_ATTR(REL, rel);
708INPUT_DEV_CAP_ATTR(ABS, abs);
709INPUT_DEV_CAP_ATTR(MSC, msc);
710INPUT_DEV_CAP_ATTR(LED, led);
711INPUT_DEV_CAP_ATTR(SND, snd);
712INPUT_DEV_CAP_ATTR(FF, ff);
713INPUT_DEV_CAP_ATTR(SW, sw);
714
715static struct attribute *input_dev_caps_attrs[] = {
716 &class_device_attr_ev.attr,
717 &class_device_attr_key.attr,
718 &class_device_attr_rel.attr,
719 &class_device_attr_abs.attr,
720 &class_device_attr_msc.attr,
721 &class_device_attr_led.attr,
722 &class_device_attr_snd.attr,
723 &class_device_attr_ff.attr,
724 &class_device_attr_sw.attr,
725 NULL
726};
727
728static struct attribute_group input_dev_caps_attr_group = {
729 .name = "capabilities",
730 .attrs = input_dev_caps_attrs,
731};
732
d19fbe8a
DT
733static void input_dev_release(struct class_device *class_dev)
734{
735 struct input_dev *dev = to_input_dev(class_dev);
736
737 kfree(dev);
738 module_put(THIS_MODULE);
739}
740
a7fadbe1 741/*
312c004d 742 * Input uevent interface - loading event handlers based on
a7fadbe1
DT
743 * device bitfields.
744 */
312c004d 745static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
ac648a6a
DT
746 char *buffer, int buffer_size, int *cur_len,
747 const char *name, unsigned long *bitmap, int max)
a7fadbe1
DT
748{
749 if (*cur_index >= num_envp - 1)
750 return -ENOMEM;
751
752 envp[*cur_index] = buffer + *cur_len;
753
754 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
ac648a6a 755 if (*cur_len >= buffer_size)
a7fadbe1
DT
756 return -ENOMEM;
757
758 *cur_len += input_print_bitmap(buffer + *cur_len,
759 max(buffer_size - *cur_len, 0),
2db66876 760 bitmap, max, 0) + 1;
a7fadbe1
DT
761 if (*cur_len > buffer_size)
762 return -ENOMEM;
763
764 (*cur_index)++;
765 return 0;
766}
767
ac648a6a
DT
768static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
769 char *buffer, int buffer_size, int *cur_len,
770 struct input_dev *dev)
771{
772 if (*cur_index >= num_envp - 1)
773 return -ENOMEM;
774
775 envp[*cur_index] = buffer + *cur_len;
776
777 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
778 "MODALIAS=");
779 if (*cur_len >= buffer_size)
780 return -ENOMEM;
781
782 *cur_len += input_print_modalias(buffer + *cur_len,
783 max(buffer_size - *cur_len, 0),
784 dev, 0) + 1;
785 if (*cur_len > buffer_size)
786 return -ENOMEM;
787
788 (*cur_index)++;
789 return 0;
790}
791
a7fadbe1
DT
792#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
793 do { \
ac648a6a 794 int err = add_uevent_var(envp, num_envp, &i, \
a7fadbe1
DT
795 buffer, buffer_size, &len, \
796 fmt, val); \
797 if (err) \
798 return err; \
799 } while (0)
800
801#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
802 do { \
312c004d 803 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
a7fadbe1
DT
804 buffer, buffer_size, &len, \
805 name, bm, max); \
806 if (err) \
807 return err; \
808 } while (0)
809
ac648a6a
DT
810#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
811 do { \
812 int err = input_add_uevent_modalias_var(envp, \
813 num_envp, &i, \
814 buffer, buffer_size, &len, \
815 dev); \
816 if (err) \
817 return err; \
818 } while (0)
819
312c004d
KS
820static int input_dev_uevent(struct class_device *cdev, char **envp,
821 int num_envp, char *buffer, int buffer_size)
a7fadbe1
DT
822{
823 struct input_dev *dev = to_input_dev(cdev);
824 int i = 0;
825 int len = 0;
826
827 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
828 dev->id.bustype, dev->id.vendor,
829 dev->id.product, dev->id.version);
830 if (dev->name)
831 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
832 if (dev->phys)
833 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
08de1f04 834 if (dev->uniq)
a7fadbe1
DT
835 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
836
837 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
838 if (test_bit(EV_KEY, dev->evbit))
839 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
840 if (test_bit(EV_REL, dev->evbit))
841 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
842 if (test_bit(EV_ABS, dev->evbit))
843 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
844 if (test_bit(EV_MSC, dev->evbit))
845 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
846 if (test_bit(EV_LED, dev->evbit))
847 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
848 if (test_bit(EV_SND, dev->evbit))
849 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
850 if (test_bit(EV_FF, dev->evbit))
851 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
852 if (test_bit(EV_SW, dev->evbit))
853 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
854
ac648a6a 855 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
a7fadbe1 856
bd37e5a9 857 envp[i] = NULL;
a7fadbe1
DT
858 return 0;
859}
860
ea9f240b
GKH
861struct class input_class = {
862 .name = "input",
d19fbe8a 863 .release = input_dev_release,
312c004d 864 .uevent = input_dev_uevent,
d19fbe8a
DT
865};
866
867struct input_dev *input_allocate_device(void)
868{
869 struct input_dev *dev;
870
871 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
872 if (dev) {
873 dev->dynalloc = 1;
ea9f240b 874 dev->cdev.class = &input_class;
d19fbe8a 875 class_device_initialize(&dev->cdev);
f60d2b11 876 mutex_init(&dev->mutex);
d19fbe8a
DT
877 INIT_LIST_HEAD(&dev->h_list);
878 INIT_LIST_HEAD(&dev->node);
879 }
880
881 return dev;
882}
883
f60d2b11
DT
884void input_free_device(struct input_dev *dev)
885{
886 if (dev) {
887
888 mutex_lock(&dev->mutex);
889 dev->name = dev->phys = dev->uniq = NULL;
890 mutex_unlock(&dev->mutex);
891
892 input_put_device(dev);
893 }
894}
895
5f945489 896int input_register_device(struct input_dev *dev)
1da177e4 897{
bd0ef235 898 static atomic_t input_no = ATOMIC_INIT(0);
1da177e4
LT
899 struct input_handle *handle;
900 struct input_handler *handler;
901 struct input_device_id *id;
bd0ef235
DT
902 const char *path;
903 int error;
1da177e4 904
5f945489
DT
905 if (!dev->dynalloc) {
906 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
907 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
908 dev->name ? dev->name : "<Unknown>");
909 return -EINVAL;
910 }
1da177e4 911
5f945489 912 set_bit(EV_SYN, dev->evbit);
0fbf87ca 913
1da177e4
LT
914 /*
915 * If delay and period are pre-set by the driver, then autorepeating
916 * is handled by the driver itself and we don't do it in input.c.
917 */
918
919 init_timer(&dev->timer);
920 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
921 dev->timer.data = (long) dev;
922 dev->timer.function = input_repeat_key;
923 dev->rep[REP_DELAY] = 250;
924 dev->rep[REP_PERIOD] = 33;
925 }
926
927 INIT_LIST_HEAD(&dev->h_list);
928 list_add_tail(&dev->node, &input_dev_list);
929
bd0ef235
DT
930 dev->cdev.class = &input_class;
931 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
932 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
933
934 error = class_device_add(&dev->cdev);
935 if (error)
936 return error;
937
938 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
939 if (error)
940 goto fail1;
941
942 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
943 if (error)
944 goto fail2;
945
946 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
947 if (error)
948 goto fail3;
949
950 __module_get(THIS_MODULE);
951
952 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
953 printk(KERN_INFO "input: %s as %s\n",
954 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
955 kfree(path);
10204020 956
1da177e4
LT
957 list_for_each_entry(handler, &input_handler_list, node)
958 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
959 if ((id = input_match_device(handler->id_table, dev)))
960 if ((handle = handler->connect(handler, dev, id)))
961 input_link_handle(handle);
962
f96b434d 963 input_wakeup_procfs_readers();
5f945489
DT
964
965 return 0;
bd0ef235
DT
966
967 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
968 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
969 fail1: class_device_del(&dev->cdev);
970 return error;
1da177e4
LT
971}
972
973void input_unregister_device(struct input_dev *dev)
974{
975 struct list_head * node, * next;
976
977 if (!dev) return;
978
979 del_timer_sync(&dev->timer);
980
981 list_for_each_safe(node, next, &dev->h_list) {
982 struct input_handle * handle = to_handle(node);
983 list_del_init(&handle->d_node);
984 list_del_init(&handle->h_node);
985 handle->handler->disconnect(handle);
986 }
987
1da177e4
LT
988 list_del_init(&dev->node);
989
5f945489
DT
990 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
991 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
bd0ef235 992 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
5f945489 993 class_device_unregister(&dev->cdev);
d19fbe8a 994
f60d2b11
DT
995 mutex_lock(&dev->mutex);
996 dev->name = dev->phys = dev->uniq = NULL;
997 mutex_unlock(&dev->mutex);
998
f96b434d 999 input_wakeup_procfs_readers();
1da177e4
LT
1000}
1001
1002void input_register_handler(struct input_handler *handler)
1003{
1004 struct input_dev *dev;
1005 struct input_handle *handle;
1006 struct input_device_id *id;
1007
1008 if (!handler) return;
1009
1010 INIT_LIST_HEAD(&handler->h_list);
1011
1012 if (handler->fops != NULL)
1013 input_table[handler->minor >> 5] = handler;
1014
1015 list_add_tail(&handler->node, &input_handler_list);
1016
1017 list_for_each_entry(dev, &input_dev_list, node)
1018 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1019 if ((id = input_match_device(handler->id_table, dev)))
1020 if ((handle = handler->connect(handler, dev, id)))
1021 input_link_handle(handle);
1022
f96b434d 1023 input_wakeup_procfs_readers();
1da177e4
LT
1024}
1025
1026void input_unregister_handler(struct input_handler *handler)
1027{
1028 struct list_head * node, * next;
1029
1030 list_for_each_safe(node, next, &handler->h_list) {
1031 struct input_handle * handle = to_handle_h(node);
1032 list_del_init(&handle->h_node);
1033 list_del_init(&handle->d_node);
1034 handler->disconnect(handle);
1035 }
1036
1037 list_del_init(&handler->node);
1038
1039 if (handler->fops != NULL)
1040 input_table[handler->minor >> 5] = NULL;
1041
f96b434d 1042 input_wakeup_procfs_readers();
1da177e4
LT
1043}
1044
1045static int input_open_file(struct inode *inode, struct file *file)
1046{
1047 struct input_handler *handler = input_table[iminor(inode) >> 5];
99ac48f5 1048 const struct file_operations *old_fops, *new_fops = NULL;
1da177e4
LT
1049 int err;
1050
1051 /* No load-on-demand here? */
1052 if (!handler || !(new_fops = fops_get(handler->fops)))
1053 return -ENODEV;
1054
1055 /*
1056 * That's _really_ odd. Usually NULL ->open means "nothing special",
1057 * not "no device". Oh, well...
1058 */
1059 if (!new_fops->open) {
1060 fops_put(new_fops);
1061 return -ENODEV;
1062 }
1063 old_fops = file->f_op;
1064 file->f_op = new_fops;
1065
1066 err = new_fops->open(inode, file);
1067
1068 if (err) {
1069 fops_put(file->f_op);
1070 file->f_op = fops_get(old_fops);
1071 }
1072 fops_put(old_fops);
1073 return err;
1074}
1075
1076static struct file_operations input_fops = {
1077 .owner = THIS_MODULE,
1078 .open = input_open_file,
1079};
1080
f96b434d 1081static int __init input_init(void)
1da177e4 1082{
f96b434d 1083 int err;
1da177e4 1084
ea9f240b 1085 err = class_register(&input_class);
d19fbe8a
DT
1086 if (err) {
1087 printk(KERN_ERR "input: unable to register input_dev class\n");
1088 return err;
1089 }
1090
f96b434d
DT
1091 err = input_proc_init();
1092 if (err)
b0fdfebb 1093 goto fail1;
1da177e4 1094
f96b434d
DT
1095 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1096 if (err) {
1097 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
b0fdfebb 1098 goto fail2;
1da177e4 1099 }
e334016f 1100
1da177e4 1101 return 0;
1da177e4 1102
b0fdfebb 1103 fail2: input_proc_exit();
ea9f240b 1104 fail1: class_unregister(&input_class);
f96b434d 1105 return err;
1da177e4
LT
1106}
1107
1108static void __exit input_exit(void)
1109{
f96b434d 1110 input_proc_exit();
1da177e4 1111 unregister_chrdev(INPUT_MAJOR, "input");
ea9f240b 1112 class_unregister(&input_class);
1da177e4
LT
1113}
1114
1115subsys_initcall(input_init);
1116module_exit(input_exit);