input-leds.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_get_count(struct input_dev *dev)
  64. {
  65. unsigned int led_code;
  66. int count = 0;
  67. for_each_set_bit(led_code, dev->ledbit, LED_CNT)
  68. if (input_led_info[led_code].name)
  69. count++;
  70. return count;
  71. }
  72. static int input_leds_connect(struct input_handler *handler,
  73. struct input_dev *dev,
  74. const struct input_device_id *id)
  75. {
  76. struct input_leds *leds;
  77. struct input_led *led;
  78. unsigned int num_leds;
  79. unsigned int led_code;
  80. int led_no;
  81. int error;
  82. num_leds = input_leds_get_count(dev);
  83. if (!num_leds)
  84. return -ENXIO;
  85. leds = kzalloc(struct_size(leds, leds, num_leds), GFP_KERNEL);
  86. if (!leds)
  87. return -ENOMEM;
  88. leds->num_leds = num_leds;
  89. leds->handle.dev = dev;
  90. leds->handle.handler = handler;
  91. leds->handle.name = "leds";
  92. leds->handle.private = leds;
  93. error = input_register_handle(&leds->handle);
  94. if (error)
  95. goto err_free_mem;
  96. error = input_open_device(&leds->handle);
  97. if (error)
  98. goto err_unregister_handle;
  99. led_no = 0;
  100. for_each_set_bit(led_code, dev->ledbit, LED_CNT) {
  101. if (!input_led_info[led_code].name)
  102. continue;
  103. led = &leds->leds[led_no];
  104. led->handle = &leds->handle;
  105. led->code = led_code;
  106. led->cdev.name = kasprintf(GFP_KERNEL, "%s::%s",
  107. dev_name(&dev->dev),
  108. input_led_info[led_code].name);
  109. if (!led->cdev.name) {
  110. error = -ENOMEM;
  111. goto err_unregister_leds;
  112. }
  113. led->cdev.max_brightness = 1;
  114. led->cdev.brightness_get = input_leds_brightness_get;
  115. led->cdev.brightness_set = input_leds_brightness_set;
  116. led->cdev.default_trigger = input_led_info[led_code].trigger;
  117. error = led_classdev_register(&dev->dev, &led->cdev);
  118. if (error) {
  119. dev_err(&dev->dev, "failed to register LED %s: %d\n",
  120. led->cdev.name, error);
  121. kfree(led->cdev.name);
  122. goto err_unregister_leds;
  123. }
  124. led_no++;
  125. }
  126. return 0;
  127. err_unregister_leds:
  128. while (--led_no >= 0) {
  129. struct input_led *led = &leds->leds[led_no];
  130. led_classdev_unregister(&led->cdev);
  131. kfree(led->cdev.name);
  132. }
  133. input_close_device(&leds->handle);
  134. err_unregister_handle:
  135. input_unregister_handle(&leds->handle);
  136. err_free_mem:
  137. kfree(leds);
  138. return error;
  139. }
  140. static void input_leds_disconnect(struct input_handle *handle)
  141. {
  142. struct input_leds *leds = handle->private;
  143. int i;
  144. for (i = 0; i < leds->num_leds; i++) {
  145. struct input_led *led = &leds->leds[i];
  146. led_classdev_unregister(&led->cdev);
  147. kfree(led->cdev.name);
  148. }
  149. input_close_device(handle);
  150. input_unregister_handle(handle);
  151. kfree(leds);
  152. }
  153. static const struct input_device_id input_leds_ids[] = {
  154. {
  155. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  156. .evbit = { BIT_MASK(EV_LED) },
  157. },
  158. { },
  159. };
  160. MODULE_DEVICE_TABLE(input, input_leds_ids);
  161. static struct input_handler input_leds_handler = {
  162. .event = input_leds_event,
  163. .connect = input_leds_connect,
  164. .disconnect = input_leds_disconnect,
  165. .name = "leds",
  166. .id_table = input_leds_ids,
  167. };
  168. static int __init input_leds_init(void)
  169. {
  170. return input_register_handler(&input_leds_handler);
  171. }
  172. module_init(input_leds_init);
  173. static void __exit input_leds_exit(void)
  174. {
  175. input_unregister_handler(&input_leds_handler);
  176. }
  177. module_exit(input_leds_exit);
  178. MODULE_AUTHOR("Samuel Thibault <samuel.thibault@ens-lyon.org>");
  179. MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
  180. MODULE_DESCRIPTION("Input -> LEDs Bridge");
  181. MODULE_LICENSE("GPL v2");