~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux-2.6.17/drivers/input/input.c

Version: ~ [ 2.6.16 ] ~ [ 2.6.17 ] ~
Architecture: ~ [ ia64 ] ~ [ i386 ] ~ [ arm ] ~ [ ppc ] ~ [ sparc64 ] ~

  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_register_device);
 33 EXPORT_SYMBOL(input_unregister_device);
 34 EXPORT_SYMBOL(input_register_handler);
 35 EXPORT_SYMBOL(input_unregister_handler);
 36 EXPORT_SYMBOL(input_grab_device);
 37 EXPORT_SYMBOL(input_release_device);
 38 EXPORT_SYMBOL(input_open_device);
 39 EXPORT_SYMBOL(input_close_device);
 40 EXPORT_SYMBOL(input_accept_process);
 41 EXPORT_SYMBOL(input_flush_device);
 42 EXPORT_SYMBOL(input_event);
 43 EXPORT_SYMBOL_GPL(input_class);
 44 
 45 #define INPUT_DEVICES   256
 46 
 47 static LIST_HEAD(input_dev_list);
 48 static LIST_HEAD(input_handler_list);
 49 
 50 static struct input_handler *input_table[8];
 51 
 52 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
 53 {
 54         struct input_handle *handle;
 55 
 56         if (type > EV_MAX || !test_bit(type, dev->evbit))
 57                 return;
 58 
 59         add_input_randomness(type, code, value);
 60 
 61         switch (type) {
 62 
 63                 case EV_SYN:
 64                         switch (code) {
 65                                 case SYN_CONFIG:
 66                                         if (dev->event) dev->event(dev, type, code, value);
 67                                         break;
 68 
 69                                 case SYN_REPORT:
 70                                         if (dev->sync) return;
 71                                         dev->sync = 1;
 72                                         break;
 73                         }
 74                         break;
 75 
 76                 case EV_KEY:
 77 
 78                         if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
 79                                 return;
 80 
 81                         if (value == 2)
 82                                 break;
 83 
 84                         change_bit(code, dev->key);
 85 
 86                         if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
 87                                 dev->repeat_key = code;
 88                                 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
 89                         }
 90 
 91                         break;
 92 
 93                 case EV_SW:
 94 
 95                         if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
 96                                 return;
 97 
 98                         change_bit(code, dev->sw);
 99 
100                         break;
101 
102                 case EV_ABS:
103 
104                         if (code > ABS_MAX || !test_bit(code, dev->absbit))
105                                 return;
106 
107                         if (dev->absfuzz[code]) {
108                                 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
109                                     (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
110                                         return;
111 
112                                 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
113                                     (value < dev->abs[code] + dev->absfuzz[code]))
114                                         value = (dev->abs[code] * 3 + value) >> 2;
115 
116                                 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
117                                     (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
118                                         value = (dev->abs[code] + value) >> 1;
119                         }
120 
121                         if (dev->abs[code] == value)
122                                 return;
123 
124                         dev->abs[code] = value;
125                         break;
126 
127                 case EV_REL:
128 
129                         if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
130                                 return;
131 
132                         break;
133 
134                 case EV_MSC:
135 
136                         if (code > MSC_MAX || !test_bit(code, dev->mscbit))
137                                 return;
138 
139                         if (dev->event) dev->event(dev, type, code, value);
140 
141                         break;
142 
143                 case EV_LED:
144 
145                         if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
146                                 return;
147 
148                         change_bit(code, dev->led);
149                         if (dev->event) dev->event(dev, type, code, value);
150 
151                         break;
152 
153                 case EV_SND:
154 
155                         if (code > SND_MAX || !test_bit(code, dev->sndbit))
156                                 return;
157 
158                         if (!!test_bit(code, dev->snd) != !!value)
159                                 change_bit(code, dev->snd);
160 
161                         if (dev->event) dev->event(dev, type, code, value);
162 
163                         break;
164 
165                 case EV_REP:
166 
167                         if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
168 
169                         dev->rep[code] = value;
170                         if (dev->event) dev->event(dev, type, code, value);
171 
172                         break;
173 
174                 case EV_FF:
175                         if (dev->event) dev->event(dev, type, code, value);
176                         break;
177         }
178 
179         if (type != EV_SYN)
180                 dev->sync = 0;
181 
182         if (dev->grab)
183                 dev->grab->handler->event(dev->grab, type, code, value);
184         else
185                 list_for_each_entry(handle, &dev->h_list, d_node)
186                         if (handle->open)
187                                 handle->handler->event(handle, type, code, value);
188 }
189 
190 static void input_repeat_key(unsigned long data)
191 {
192         struct input_dev *dev = (void *) data;
193 
194         if (!test_bit(dev->repeat_key, dev->key))
195                 return;
196 
197         input_event(dev, EV_KEY, dev->repeat_key, 2);
198         input_sync(dev);
199 
200         if (dev->rep[REP_PERIOD])
201                 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
202 }
203 
204 int input_accept_process(struct input_handle *handle, struct file *file)
205 {
206         if (handle->dev->accept)
207                 return handle->dev->accept(handle->dev, file);
208 
209         return 0;
210 }
211 
212 int input_grab_device(struct input_handle *handle)
213 {
214         if (handle->dev->grab)
215                 return -EBUSY;
216 
217         handle->dev->grab = handle;
218         return 0;
219 }
220 
221 void input_release_device(struct input_handle *handle)
222 {
223         if (handle->dev->grab == handle)
224                 handle->dev->grab = NULL;
225 }
226 
227 int input_open_device(struct input_handle *handle)
228 {
229         struct input_dev *dev = handle->dev;
230         int err;
231 
232         err = mutex_lock_interruptible(&dev->mutex);
233         if (err)
234                 return err;
235 
236         handle->open++;
237 
238         if (!dev->users++ && dev->open)
239                 err = dev->open(dev);
240 
241         if (err)
242                 handle->open--;
243 
244         mutex_unlock(&dev->mutex);
245 
246         return err;
247 }
248 
249 int input_flush_device(struct input_handle* handle, struct file* file)
250 {
251         if (handle->dev->flush)
252                 return handle->dev->flush(handle->dev, file);
253 
254         return 0;
255 }
256 
257 void input_close_device(struct input_handle *handle)
258 {
259         struct input_dev *dev = handle->dev;
260 
261         input_release_device(handle);
262 
263         mutex_lock(&dev->mutex);
264 
265         if (!--dev->users && dev->close)
266                 dev->close(dev);
267         handle->open--;
268 
269         mutex_unlock(&dev->mutex);
270 }
271 
272 static void input_link_handle(struct input_handle *handle)
273 {
274         list_add_tail(&handle->d_node, &handle->dev->h_list);
275         list_add_tail(&handle->h_node, &handle->handler->h_list);
276 }
277 
278 #define MATCH_BIT(bit, max) \
279                 for (i = 0; i < NBITS(max); i++) \
280                         if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
281                                 break; \
282                 if (i != NBITS(max)) \
283                         continue;
284 
285 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
286 {
287         int i;
288 
289         for (; id->flags || id->driver_info; id++) {
290 
291                 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
292                         if (id->bustype != dev->id.bustype)
293                                 continue;
294 
295                 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
296                         if (id->vendor != dev->id.vendor)
297                                 continue;
298 
299                 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
300                         if (id->product != dev->id.product)
301                                 continue;
302 
303                 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
304                         if (id->version != dev->id.version)
305                                 continue;
306 
307                 MATCH_BIT(evbit,  EV_MAX);
308                 MATCH_BIT(keybit, KEY_MAX);
309                 MATCH_BIT(relbit, REL_MAX);
310                 MATCH_BIT(absbit, ABS_MAX);
311                 MATCH_BIT(mscbit, MSC_MAX);
312                 MATCH_BIT(ledbit, LED_MAX);
313                 MATCH_BIT(sndbit, SND_MAX);
314                 MATCH_BIT(ffbit,  FF_MAX);
315                 MATCH_BIT(swbit,  SW_MAX);
316 
317                 return id;
318         }
319 
320         return NULL;
321 }
322 
323 #ifdef CONFIG_PROC_FS
324 
325 static struct proc_dir_entry *proc_bus_input_dir;
326 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
327 static int input_devices_state;
328 
329 static inline void input_wakeup_procfs_readers(void)
330 {
331         input_devices_state++;
332         wake_up(&input_devices_poll_wait);
333 }
334 
335 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
336 {
337         int state = input_devices_state;
338         poll_wait(file, &input_devices_poll_wait, wait);
339         if (state != input_devices_state)
340                 return POLLIN | POLLRDNORM;
341         return 0;
342 }
343 
344 static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
345 {
346         struct list_head *node;
347         loff_t i = 0;
348 
349         list_for_each(node, list)
350                 if (i++ == *pos)
351                         return node;
352 
353         return NULL;
354 }
355 
356 static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
357 {
358         if (element->next == list)
359                 return NULL;
360 
361         ++(*pos);
362         return element->next;
363 }
364 
365 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
366 {
367         /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
368 
369         return list_get_nth_element(&input_dev_list, pos);
370 }
371 
372 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
373 {
374         return list_get_next_element(&input_dev_list, v, pos);
375 }
376 
377 static void input_devices_seq_stop(struct seq_file *seq, void *v)
378 {
379         /* release lock here */
380 }
381 
382 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
383                                    unsigned long *bitmap, int max)
384 {
385         int i;
386 
387         for (i = NBITS(max) - 1; i > 0; i--)
388                 if (bitmap[i])
389                         break;
390 
391         seq_printf(seq, "B: %s=", name);
392         for (; i >= 0; i--)
393                 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
394         seq_putc(seq, '\n');
395 }
396 
397 static int input_devices_seq_show(struct seq_file *seq, void *v)
398 {
399         struct input_dev *dev = container_of(v, struct input_dev, node);
400         const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
401         struct input_handle *handle;
402 
403         seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
404                    dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
405 
406         seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
407         seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
408         seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
409         seq_printf(seq, "H: Handlers=");
410 
411         list_for_each_entry(handle, &dev->h_list, d_node)
412                 seq_printf(seq, "%s ", handle->name);
413         seq_putc(seq, '\n');
414 
415         input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
416         if (test_bit(EV_KEY, dev->evbit))
417                 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
418         if (test_bit(EV_REL, dev->evbit))
419                 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
420         if (test_bit(EV_ABS, dev->evbit))
421                 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
422         if (test_bit(EV_MSC, dev->evbit))
423                 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
424         if (test_bit(EV_LED, dev->evbit))
425                 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
426         if (test_bit(EV_SND, dev->evbit))
427                 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
428         if (test_bit(EV_FF, dev->evbit))
429                 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
430         if (test_bit(EV_SW, dev->evbit))
431                 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
432 
433         seq_putc(seq, '\n');
434 
435         kfree(path);
436         return 0;
437 }
438 
439 static struct seq_operations input_devices_seq_ops = {
440         .start  = input_devices_seq_start,
441         .next   = input_devices_seq_next,
442         .stop   = input_devices_seq_stop,
443         .show   = input_devices_seq_show,
444 };
445 
446 static int input_proc_devices_open(struct inode *inode, struct file *file)
447 {
448         return seq_open(file, &input_devices_seq_ops);
449 }
450 
451 static struct file_operations input_devices_fileops = {
452         .owner          = THIS_MODULE,
453         .open           = input_proc_devices_open,
454         .poll           = input_proc_devices_poll,
455         .read           = seq_read,
456         .llseek         = seq_lseek,
457         .release        = seq_release,
458 };
459 
460 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
461 {
462         /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
463         seq->private = (void *)(unsigned long)*pos;
464         return list_get_nth_element(&input_handler_list, pos);
465 }
466 
467 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
468 {
469         seq->private = (void *)(unsigned long)(*pos + 1);
470         return list_get_next_element(&input_handler_list, v, pos);
471 }
472 
473 static void input_handlers_seq_stop(struct seq_file *seq, void *v)
474 {
475         /* release lock here */
476 }
477 
478 static int input_handlers_seq_show(struct seq_file *seq, void *v)
479 {
480         struct input_handler *handler = container_of(v, struct input_handler, node);
481 
482         seq_printf(seq, "N: Number=%ld Name=%s",
483                    (unsigned long)seq->private, handler->name);
484         if (handler->fops)
485                 seq_printf(seq, " Minor=%d", handler->minor);
486         seq_putc(seq, '\n');
487 
488         return 0;
489 }
490 static struct seq_operations input_handlers_seq_ops = {
491         .start  = input_handlers_seq_start,
492         .next   = input_handlers_seq_next,
493         .stop   = input_handlers_seq_stop,
494         .show   = input_handlers_seq_show,
495 };
496 
497 static int input_proc_handlers_open(struct inode *inode, struct file *file)
498 {
499         return seq_open(file, &input_handlers_seq_ops);
500 }
501 
502 static struct file_operations input_handlers_fileops = {
503         .owner          = THIS_MODULE,
504         .open           = input_proc_handlers_open,
505         .read           = seq_read,
506         .llseek         = seq_lseek,
507         .release        = seq_release,
508 };
509 
510 static int __init input_proc_init(void)
511 {
512         struct proc_dir_entry *entry;
513 
514         proc_bus_input_dir = proc_mkdir("input", proc_bus);
515         if (!proc_bus_input_dir)
516                 return -ENOMEM;
517 
518         proc_bus_input_dir->owner = THIS_MODULE;
519 
520         entry = create_proc_entry("devices", 0, proc_bus_input_dir);
521         if (!entry)
522                 goto fail1;
523 
524         entry->owner = THIS_MODULE;
525         entry->proc_fops = &input_devices_fileops;
526 
527         entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
528         if (!entry)
529                 goto fail2;
530 
531         entry->owner = THIS_MODULE;
532         entry->proc_fops = &input_handlers_fileops;
533 
534         return 0;
535 
536  fail2: remove_proc_entry("devices", proc_bus_input_dir);
537  fail1: remove_proc_entry("input", proc_bus);
538         return -ENOMEM;
539 }
540 
541 static void input_proc_exit(void)
542 {
543         remove_proc_entry("devices", proc_bus_input_dir);
544         remove_proc_entry("handlers", proc_bus_input_dir);
545         remove_proc_entry("input", proc_bus);
546 }
547 
548 #else /* !CONFIG_PROC_FS */
549 static inline void input_wakeup_procfs_readers(void) { }
550 static inline int input_proc_init(void) { return 0; }
551 static inline void input_proc_exit(void) { }
552 #endif
553 
554 #define INPUT_DEV_STRING_ATTR_SHOW(name)                                        \
555 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf)       \
556 {                                                                               \
557         struct input_dev *input_dev = to_input_dev(dev);                        \
558         int retval;                                                             \
559                                                                                 \
560         retval = mutex_lock_interruptible(&input_dev->mutex);                   \
561         if (retval)                                                             \
562                 return retval;                                                  \
563                                                                                 \
564         retval = scnprintf(buf, PAGE_SIZE,                                      \
565                            "%s\n", input_dev->name ? input_dev->name : "");     \
566                                                                                 \
567         mutex_unlock(&input_dev->mutex);                                        \
568                                                                                 \
569         return retval;                                                          \
570 }                                                                               \
571 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
572 
573 INPUT_DEV_STRING_ATTR_SHOW(name);
574 INPUT_DEV_STRING_ATTR_SHOW(phys);
575 INPUT_DEV_STRING_ATTR_SHOW(uniq);
576 
577 static int input_print_modalias_bits(char *buf, int size,
578                                      char name, unsigned long *bm,
579                                      unsigned int min_bit, unsigned int max_bit)
580 {
581         int len = 0, i;
582 
583         len += snprintf(buf, max(size, 0), "%c", name);
584         for (i = min_bit; i < max_bit; i++)
585                 if (bm[LONG(i)] & BIT(i))
586                         len += snprintf(buf + len, max(size - len, 0), "%X,", i);
587         return len;
588 }
589 
590 static int input_print_modalias(char *buf, int size, struct input_dev *id,
591                                 int add_cr)
592 {
593         int len;
594 
595         len = snprintf(buf, max(size, 0),
596                        "input:b%04Xv%04Xp%04Xe%04X-",
597                        id->id.bustype, id->id.vendor,
598                        id->id.product, id->id.version);
599 
600         len += input_print_modalias_bits(buf + len, size - len,
601                                 'e', id->evbit, 0, EV_MAX);
602         len += input_print_modalias_bits(buf + len, size - len,
603                                 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
604         len += input_print_modalias_bits(buf + len, size - len,
605                                 'r', id->relbit, 0, REL_MAX);
606         len += input_print_modalias_bits(buf + len, size - len,
607                                 'a', id->absbit, 0, ABS_MAX);
608         len += input_print_modalias_bits(buf + len, size - len,
609                                 'm', id->mscbit, 0, MSC_MAX);
610         len += input_print_modalias_bits(buf + len, size - len,
611                                 'l', id->ledbit, 0, LED_MAX);
612         len += input_print_modalias_bits(buf + len, size - len,
613                                 's', id->sndbit, 0, SND_MAX);
614         len += input_print_modalias_bits(buf + len, size - len,
615                                 'f', id->ffbit, 0, FF_MAX);
616         len += input_print_modalias_bits(buf + len, size - len,
617                                 'w', id->swbit, 0, SW_MAX);
618 
619         if (add_cr)
620                 len += snprintf(buf + len, max(size - len, 0), "\n");
621 
622         return len;
623 }
624 
625 static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
626 {
627         struct input_dev *id = to_input_dev(dev);
628         ssize_t len;
629 
630         len = input_print_modalias(buf, PAGE_SIZE, id, 1);
631 
632         return max_t(int, len, PAGE_SIZE);
633 }
634 static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
635 
636 static struct attribute *input_dev_attrs[] = {
637         &class_device_attr_name.attr,
638         &class_device_attr_phys.attr,
639         &class_device_attr_uniq.attr,
640         &class_device_attr_modalias.attr,
641         NULL
642 };
643 
644 static struct attribute_group input_dev_attr_group = {
645         .attrs  = input_dev_attrs,
646 };
647 
648 #define INPUT_DEV_ID_ATTR(name)                                                 \
649 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf)    \
650 {                                                                               \
651         struct input_dev *input_dev = to_input_dev(dev);                        \
652         return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name);         \
653 }                                                                               \
654 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
655 
656 INPUT_DEV_ID_ATTR(bustype);
657 INPUT_DEV_ID_ATTR(vendor);
658 INPUT_DEV_ID_ATTR(product);
659 INPUT_DEV_ID_ATTR(version);
660 
661 static struct attribute *input_dev_id_attrs[] = {
662         &class_device_attr_bustype.attr,
663         &class_device_attr_vendor.attr,
664         &class_device_attr_product.attr,
665         &class_device_attr_version.attr,
666         NULL
667 };
668 
669 static struct attribute_group input_dev_id_attr_group = {
670         .name   = "id",
671         .attrs  = input_dev_id_attrs,
672 };
673 
674 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
675                               int max, int add_cr)
676 {
677         int i;
678         int len = 0;
679 
680         for (i = NBITS(max) - 1; i > 0; i--)
681                 if (bitmap[i])
682                         break;
683 
684         for (; i >= 0; i--)
685                 len += snprintf(buf + len, max(buf_size - len, 0),
686                                 "%lx%s", bitmap[i], i > 0 ? " " : "");
687 
688         if (add_cr)
689                 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
690 
691         return len;
692 }
693 
694 #define INPUT_DEV_CAP_ATTR(ev, bm)                                              \
695 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf)     \
696 {                                                                               \
697         struct input_dev *input_dev = to_input_dev(dev);                        \
698         int len = input_print_bitmap(buf, PAGE_SIZE,                            \
699                                      input_dev->bm##bit, ev##_MAX, 1);          \
700         return min_t(int, len, PAGE_SIZE);                                      \
701 }                                                                               \
702 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
703 
704 INPUT_DEV_CAP_ATTR(EV, ev);
705 INPUT_DEV_CAP_ATTR(KEY, key);
706 INPUT_DEV_CAP_ATTR(REL, rel);
707 INPUT_DEV_CAP_ATTR(ABS, abs);
708 INPUT_DEV_CAP_ATTR(MSC, msc);
709 INPUT_DEV_CAP_ATTR(LED, led);
710 INPUT_DEV_CAP_ATTR(SND, snd);
711 INPUT_DEV_CAP_ATTR(FF, ff);
712 INPUT_DEV_CAP_ATTR(SW, sw);
713 
714 static struct attribute *input_dev_caps_attrs[] = {
715         &class_device_attr_ev.attr,
716         &class_device_attr_key.attr,
717         &class_device_attr_rel.attr,
718         &class_device_attr_abs.attr,
719         &class_device_attr_msc.attr,
720         &class_device_attr_led.attr,
721         &class_device_attr_snd.attr,
722         &class_device_attr_ff.attr,
723         &class_device_attr_sw.attr,
724         NULL
725 };
726 
727 static struct attribute_group input_dev_caps_attr_group = {
728         .name   = "capabilities",
729         .attrs  = input_dev_caps_attrs,
730 };
731 
732 static void input_dev_release(struct class_device *class_dev)
733 {
734         struct input_dev *dev = to_input_dev(class_dev);
735 
736         kfree(dev);
737         module_put(THIS_MODULE);
738 }
739 
740 /*
741  * Input uevent interface - loading event handlers based on
742  * device bitfields.
743  */
744 static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
745                                    char *buffer, int buffer_size, int *cur_len,
746                                    const char *name, unsigned long *bitmap, int max)
747 {
748         if (*cur_index >= num_envp - 1)
749                 return -ENOMEM;
750 
751         envp[*cur_index] = buffer + *cur_len;
752 
753         *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
754         if (*cur_len >= buffer_size)
755                 return -ENOMEM;
756 
757         *cur_len += input_print_bitmap(buffer + *cur_len,
758                                         max(buffer_size - *cur_len, 0),
759                                         bitmap, max, 0) + 1;
760         if (*cur_len > buffer_size)
761                 return -ENOMEM;
762 
763         (*cur_index)++;
764         return 0;
765 }
766 
767 static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
768                                          char *buffer, int buffer_size, int *cur_len,
769                                          struct input_dev *dev)
770 {
771         if (*cur_index >= num_envp - 1)
772                 return -ENOMEM;
773 
774         envp[*cur_index] = buffer + *cur_len;
775 
776         *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
777                              "MODALIAS=");
778         if (*cur_len >= buffer_size)
779                 return -ENOMEM;
780 
781         *cur_len += input_print_modalias(buffer + *cur_len,
782                                          max(buffer_size - *cur_len, 0),
783                                          dev, 0) + 1;
784         if (*cur_len > buffer_size)
785                 return -ENOMEM;
786 
787         (*cur_index)++;
788         return 0;
789 }
790 
791 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...)                              \
792         do {                                                            \
793                 int err = add_uevent_var(envp, num_envp, &i,            \
794                                         buffer, buffer_size, &len,      \
795                                         fmt, val);                      \
796                 if (err)                                                \
797                         return err;                                     \
798         } while (0)
799 
800 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max)                         \
801         do {                                                            \
802                 int err = input_add_uevent_bm_var(envp, num_envp, &i,   \
803                                         buffer, buffer_size, &len,      \
804                                         name, bm, max);                 \
805                 if (err)                                                \
806                         return err;                                     \
807         } while (0)
808 
809 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev)                             \
810         do {                                                            \
811                 int err = input_add_uevent_modalias_var(envp,           \
812                                         num_envp, &i,                   \
813                                         buffer, buffer_size, &len,      \
814                                         dev);                           \
815                 if (err)                                                \
816                         return err;                                     \
817         } while (0)
818 
819 static int input_dev_uevent(struct class_device *cdev, char **envp,
820                             int num_envp, char *buffer, int buffer_size)
821 {
822         struct input_dev *dev = to_input_dev(cdev);
823         int i = 0;
824         int len = 0;
825 
826         INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
827                                 dev->id.bustype, dev->id.vendor,
828                                 dev->id.product, dev->id.version);
829         if (dev->name)
830                 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
831         if (dev->phys)
832                 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
833         if (dev->uniq)
834                 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
835 
836         INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
837         if (test_bit(EV_KEY, dev->evbit))
838                 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
839         if (test_bit(EV_REL, dev->evbit))
840                 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
841         if (test_bit(EV_ABS, dev->evbit))
842                 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
843         if (test_bit(EV_MSC, dev->evbit))
844                 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
845         if (test_bit(EV_LED, dev->evbit))
846                 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
847         if (test_bit(EV_SND, dev->evbit))
848                 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
849         if (test_bit(EV_FF, dev->evbit))
850                 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
851         if (test_bit(EV_SW, dev->evbit))
852                 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
853 
854         INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
855 
856         envp[i] = NULL;
857         return 0;
858 }
859 
860 struct class input_class = {
861         .name                   = "input",
862         .release                = input_dev_release,
863         .uevent                 = input_dev_uevent,
864 };
865 
866 struct input_dev *input_allocate_device(void)
867 {
868         struct input_dev *dev;
869 
870         dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
871         if (dev) {
872                 dev->dynalloc = 1;
873                 dev->cdev.class = &input_class;
874                 class_device_initialize(&dev->cdev);
875                 INIT_LIST_HEAD(&dev->h_list);
876                 INIT_LIST_HEAD(&dev->node);
877         }
878 
879         return dev;
880 }
881 
882 int input_register_device(struct input_dev *dev)
883 {
884         static atomic_t input_no = ATOMIC_INIT(0);
885         struct input_handle *handle;
886         struct input_handler *handler;
887         struct input_device_id *id;
888         const char *path;
889         int error;
890 
891         if (!dev->dynalloc) {
892                 printk(KERN_WARNING "input: device %s is static