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