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