1 // SPDX-License-Identifier: GPL-2.0-only
3 * LED support for the input layer
5 * Copyright 2010-2015 Samuel Thibault <samuel.thibault@ens-lyon.org>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/leds.h>
13 #include <linux/input.h>
15 #if IS_ENABLED(CONFIG_VT)
16 #define VT_TRIGGER(_name) .trigger = _name
18 #define VT_TRIGGER(_name) .trigger = NULL
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" },
39 struct led_classdev cdev
;
40 struct input_handle
*handle
;
41 unsigned int code
; /* One of LED_* constants */
45 struct input_handle handle
;
46 unsigned int num_leds
;
47 struct input_led leds
[];
50 static enum led_brightness
input_leds_brightness_get(struct led_classdev
*cdev
)
52 struct input_led
*led
= container_of(cdev
, struct input_led
, cdev
);
53 struct input_dev
*input
= led
->handle
->dev
;
55 return test_bit(led
->code
, input
->led
) ? cdev
->max_brightness
: 0;
58 static void input_leds_brightness_set(struct led_classdev
*cdev
,
59 enum led_brightness brightness
)
61 struct input_led
*led
= container_of(cdev
, struct input_led
, cdev
);
63 input_inject_event(led
->handle
, EV_LED
, led
->code
, !!brightness
);
66 static void input_leds_event(struct input_handle
*handle
, unsigned int type
,
67 unsigned int code
, int value
)
71 static int input_leds_get_count(struct input_dev
*dev
)
73 unsigned int led_code
;
76 for_each_set_bit(led_code
, dev
->ledbit
, LED_CNT
)
77 if (input_led_info
[led_code
].name
)
83 static int input_leds_connect(struct input_handler
*handler
,
84 struct input_dev
*dev
,
85 const struct input_device_id
*id
)
87 struct input_leds
*leds
;
88 struct input_led
*led
;
89 unsigned int num_leds
;
90 unsigned int led_code
;
94 num_leds
= input_leds_get_count(dev
);
98 leds
= kzalloc(struct_size(leds
, leds
, num_leds
), GFP_KERNEL
);
102 leds
->num_leds
= num_leds
;
104 leds
->handle
.dev
= dev
;
105 leds
->handle
.handler
= handler
;
106 leds
->handle
.name
= "leds";
107 leds
->handle
.private = leds
;
109 error
= input_register_handle(&leds
->handle
);
113 error
= input_open_device(&leds
->handle
);
115 goto err_unregister_handle
;
118 for_each_set_bit(led_code
, dev
->ledbit
, LED_CNT
) {
119 if (!input_led_info
[led_code
].name
)
122 led
= &leds
->leds
[led_no
];
123 led
->handle
= &leds
->handle
;
124 led
->code
= led_code
;
126 led
->cdev
.name
= kasprintf(GFP_KERNEL
, "%s::%s",
128 input_led_info
[led_code
].name
);
129 if (!led
->cdev
.name
) {
131 goto err_unregister_leds
;
134 led
->cdev
.max_brightness
= 1;
135 led
->cdev
.brightness_get
= input_leds_brightness_get
;
136 led
->cdev
.brightness_set
= input_leds_brightness_set
;
137 led
->cdev
.default_trigger
= input_led_info
[led_code
].trigger
;
139 error
= led_classdev_register(&dev
->dev
, &led
->cdev
);
141 dev_err(&dev
->dev
, "failed to register LED %s: %d\n",
142 led
->cdev
.name
, error
);
143 kfree(led
->cdev
.name
);
144 goto err_unregister_leds
;
153 while (--led_no
>= 0) {
154 struct input_led
*led
= &leds
->leds
[led_no
];
156 led_classdev_unregister(&led
->cdev
);
157 kfree(led
->cdev
.name
);
160 input_close_device(&leds
->handle
);
162 err_unregister_handle
:
163 input_unregister_handle(&leds
->handle
);
170 static void input_leds_disconnect(struct input_handle
*handle
)
172 struct input_leds
*leds
= handle
->private;
175 for (i
= 0; i
< leds
->num_leds
; i
++) {
176 struct input_led
*led
= &leds
->leds
[i
];
178 led_classdev_unregister(&led
->cdev
);
179 kfree(led
->cdev
.name
);
182 input_close_device(handle
);
183 input_unregister_handle(handle
);
188 static const struct input_device_id input_leds_ids
[] = {
190 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
,
191 .evbit
= { BIT_MASK(EV_LED
) },
195 MODULE_DEVICE_TABLE(input
, input_leds_ids
);
197 static struct input_handler input_leds_handler
= {
198 .event
= input_leds_event
,
199 .connect
= input_leds_connect
,
200 .disconnect
= input_leds_disconnect
,
202 .id_table
= input_leds_ids
,
205 static int __init
input_leds_init(void)
207 return input_register_handler(&input_leds_handler
);
209 module_init(input_leds_init
);
211 static void __exit
input_leds_exit(void)
213 input_unregister_handler(&input_leds_handler
);
215 module_exit(input_leds_exit
);
217 MODULE_AUTHOR("Samuel Thibault <samuel.thibault@ens-lyon.org>");
218 MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
219 MODULE_DESCRIPTION("Input -> LEDs Bridge");
220 MODULE_LICENSE("GPL v2");