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