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