input-leds.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * LED support for the input layer
  3. *
  4. * Copyright 2010-2015 Samuel Thibault <samuel.thibault@ens-lyon.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/leds.h>
  15. #include <linux/input.h>
  16. #if IS_ENABLED(CONFIG_VT)
  17. #define VT_TRIGGER(_name) .trigger = _name
  18. #else
  19. #define VT_TRIGGER(_name) .trigger = NULL
  20. #endif
  21. static const struct {
  22. const char *name;
  23. const char *trigger;
  24. } input_led_info[LED_CNT] = {
  25. [LED_NUML] = { "numlock", VT_TRIGGER("kbd-numlock") },
  26. [LED_CAPSL] = { "capslock", VT_TRIGGER("kbd-capslock") },
  27. [LED_SCROLLL] = { "scrolllock", VT_TRIGGER("kbd-scrolllock") },
  28. [LED_COMPOSE] = { "compose" },
  29. [LED_KANA] = { "kana", VT_TRIGGER("kbd-kanalock") },
  30. [LED_SLEEP] = { "sleep" } ,
  31. [LED_SUSPEND] = { "suspend" },
  32. [LED_MUTE] = { "mute" },
  33. [LED_MISC] = { "misc" },
  34. [LED_MAIL] = { "mail" },
  35. [LED_CHARGING] = { "charging" },
  36. };
  37. struct input_led {
  38. struct led_classdev cdev;
  39. struct input_handle *handle;
  40. unsigned int code; /* One of LED_* constants */
  41. };
  42. struct input_leds {
  43. struct input_handle handle;
  44. unsigned int num_leds;
  45. struct input_led leds[];
  46. };
  47. static enum led_brightness input_leds_brightness_get(struct led_classdev *cdev)
  48. {
  49. struct input_led *led = container_of(cdev, struct input_led, cdev);
  50. struct input_dev *input = led->handle->dev;
  51. return test_bit(led->code, input->led) ? cdev->max_brightness : 0;
  52. }
  53. static void input_leds_brightness_set(struct led_classdev *cdev,
  54. enum led_brightness brightness)
  55. {
  56. struct input_led *led = container_of(cdev, struct input_led, cdev);
  57. input_inject_event(led->handle, EV_LED, led->code, !!brightness);
  58. }
  59. static void input_leds_event(struct input_handle *handle, unsigned int type,
  60. unsigned int code, int value)
  61. {
  62. }
  63. static int input_leds_connect(struct input_handler *handler,
  64. struct input_dev *dev,
  65. const struct input_device_id *id)
  66. {
  67. struct input_leds *leds;
  68. unsigned int num_leds;
  69. unsigned int led_code;
  70. int led_no;
  71. int error;
  72. num_leds = bitmap_weight(dev->ledbit, LED_CNT);
  73. if (!num_leds)
  74. return -ENXIO;
  75. leds = kzalloc(sizeof(*leds) + num_leds * sizeof(*leds->leds),
  76. GFP_KERNEL);
  77. if (!leds)
  78. return -ENOMEM;
  79. leds->num_leds = num_leds;
  80. leds->handle.dev = dev;
  81. leds->handle.handler = handler;
  82. leds->handle.name = "leds";
  83. leds->handle.private = leds;
  84. error = input_register_handle(&leds->handle);
  85. if (error)
  86. goto err_free_mem;
  87. error = input_open_device(&leds->handle);
  88. if (error)
  89. goto err_unregister_handle;
  90. led_no = 0;
  91. for_each_set_bit(led_code, dev->ledbit, LED_CNT) {
  92. struct input_led *led = &leds->leds[led_no];
  93. led->handle = &leds->handle;
  94. led->code = led_code;
  95. if (WARN_ON(!input_led_info[led_code].name))
  96. continue;
  97. led->cdev.name = kasprintf(GFP_KERNEL, "%s::%s",
  98. dev_name(&dev->dev),
  99. input_led_info[led_code].name);
  100. if (!led->cdev.name) {
  101. error = -ENOMEM;
  102. goto err_unregister_leds;
  103. }
  104. led->cdev.max_brightness = 1;
  105. led->cdev.brightness_get = input_leds_brightness_get;
  106. led->cdev.brightness_set = input_leds_brightness_set;
  107. led->cdev.default_trigger = input_led_info[led_code].trigger;
  108. error = led_classdev_register(&dev->dev, &led->cdev);
  109. if (error) {
  110. dev_err(&dev->dev, "failed to register LED %s: %d\n",
  111. led->cdev.name, error);
  112. kfree(led->cdev.name);
  113. goto err_unregister_leds;
  114. }
  115. led_no++;
  116. }
  117. return 0;
  118. err_unregister_leds:
  119. while (--led_no >= 0) {
  120. struct input_led *led = &leds->leds[led_no];
  121. led_classdev_unregister(&led->cdev);
  122. kfree(led->cdev.name);
  123. }
  124. input_close_device(&leds->handle);
  125. err_unregister_handle:
  126. input_unregister_handle(&leds->handle);
  127. err_free_mem:
  128. kfree(leds);
  129. return error;
  130. }
  131. static void input_leds_disconnect(struct input_handle *handle)
  132. {
  133. struct input_leds *leds = handle->private;
  134. int i;
  135. for (i = 0; i < leds->num_leds; i++) {
  136. struct input_led *led = &leds->leds[i];
  137. led_classdev_unregister(&led->cdev);
  138. kfree(led->cdev.name);
  139. }
  140. input_close_device(handle);
  141. input_unregister_handle(handle);
  142. kfree(leds);
  143. }
  144. static const struct input_device_id input_leds_ids[] = {
  145. {
  146. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  147. .evbit = { BIT_MASK(EV_LED) },
  148. },
  149. { },
  150. };
  151. MODULE_DEVICE_TABLE(input, input_leds_ids);
  152. static struct input_handler input_leds_handler = {
  153. .event = input_leds_event,
  154. .connect = input_leds_connect,
  155. .disconnect = input_leds_disconnect,
  156. .name = "leds",
  157. .id_table = input_leds_ids,
  158. };
  159. static int __init input_leds_init(void)
  160. {
  161. return input_register_handler(&input_leds_handler);
  162. }
  163. module_init(input_leds_init);
  164. static void __exit input_leds_exit(void)
  165. {
  166. input_unregister_handler(&input_leds_handler);
  167. }
  168. module_exit(input_leds_exit);
  169. MODULE_AUTHOR("Samuel Thibault <samuel.thibault@ens-lyon.org>");
  170. MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
  171. MODULE_DESCRIPTION("Input -> LEDs Bridge");
  172. MODULE_LICENSE("GPL v2");