Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * LED support for the input layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2010-2015 Samuel Thibault <samuel.thibault@ens-lyon.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #if IS_ENABLED(CONFIG_VT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define VT_TRIGGER(_name)	.trigger = _name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define VT_TRIGGER(_name)	.trigger = NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	const char *trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) } input_led_info[LED_CNT] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	[LED_NUML]	= { "numlock", VT_TRIGGER("kbd-numlock") },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	[LED_CAPSL]	= { "capslock", VT_TRIGGER("kbd-capslock") },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	[LED_SCROLLL]	= { "scrolllock", VT_TRIGGER("kbd-scrolllock") },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	[LED_COMPOSE]	= { "compose" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	[LED_KANA]	= { "kana", VT_TRIGGER("kbd-kanalock") },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	[LED_SLEEP]	= { "sleep" } ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	[LED_SUSPEND]	= { "suspend" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	[LED_MUTE]	= { "mute" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	[LED_MISC]	= { "misc" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	[LED_MAIL]	= { "mail" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	[LED_CHARGING]	= { "charging" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) struct input_led {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct led_classdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct input_handle *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	unsigned int code; /* One of LED_* constants */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) struct input_leds {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct input_handle handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	unsigned int num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct input_led leds[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static enum led_brightness input_leds_brightness_get(struct led_classdev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct input_led *led = container_of(cdev, struct input_led, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct input_dev *input = led->handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return test_bit(led->code, input->led) ? cdev->max_brightness : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static void input_leds_brightness_set(struct led_classdev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 				      enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct input_led *led = container_of(cdev, struct input_led, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	input_inject_event(led->handle, EV_LED, led->code, !!brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static void input_leds_event(struct input_handle *handle, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			     unsigned int code, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int input_leds_get_count(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	unsigned int led_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	for_each_set_bit(led_code, dev->ledbit, LED_CNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (input_led_info[led_code].name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static int input_leds_connect(struct input_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			      struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			      const struct input_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct input_leds *leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct input_led *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	unsigned int num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	unsigned int led_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int led_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	num_leds = input_leds_get_count(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (!num_leds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	leds = kzalloc(struct_size(leds, leds, num_leds), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (!leds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	leds->num_leds = num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	leds->handle.dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	leds->handle.handler = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	leds->handle.name = "leds";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	leds->handle.private = leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	error = input_register_handle(&leds->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	error = input_open_device(&leds->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		goto err_unregister_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	led_no = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	for_each_set_bit(led_code, dev->ledbit, LED_CNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		if (!input_led_info[led_code].name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		led = &leds->leds[led_no];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		led->handle = &leds->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		led->code = led_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		led->cdev.name = kasprintf(GFP_KERNEL, "%s::%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 					   dev_name(&dev->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 					   input_led_info[led_code].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		if (!led->cdev.name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			goto err_unregister_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		led->cdev.max_brightness = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		led->cdev.brightness_get = input_leds_brightness_get;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		led->cdev.brightness_set = input_leds_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		led->cdev.default_trigger = input_led_info[led_code].trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		error = led_classdev_register(&dev->dev, &led->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			dev_err(&dev->dev, "failed to register LED %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 				led->cdev.name, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			kfree(led->cdev.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			goto err_unregister_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		led_no++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) err_unregister_leds:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	while (--led_no >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		struct input_led *led = &leds->leds[led_no];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		led_classdev_unregister(&led->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		kfree(led->cdev.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	input_close_device(&leds->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) err_unregister_handle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	input_unregister_handle(&leds->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) err_free_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	kfree(leds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static void input_leds_disconnect(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct input_leds *leds = handle->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	for (i = 0; i < leds->num_leds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		struct input_led *led = &leds->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		led_classdev_unregister(&led->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		kfree(led->cdev.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	input_close_device(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	input_unregister_handle(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	kfree(leds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static const struct input_device_id input_leds_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		.evbit = { BIT_MASK(EV_LED) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) MODULE_DEVICE_TABLE(input, input_leds_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static struct input_handler input_leds_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.event =	input_leds_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.connect =	input_leds_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.disconnect =	input_leds_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.name =		"leds",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	.id_table =	input_leds_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int __init input_leds_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return input_register_handler(&input_leds_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) module_init(input_leds_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static void __exit input_leds_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	input_unregister_handler(&input_leds_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) module_exit(input_leds_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) MODULE_AUTHOR("Samuel Thibault <samuel.thibault@ens-lyon.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) MODULE_AUTHOR("Dmitry Torokhov <dmitry.torokhov@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) MODULE_DESCRIPTION("Input -> LEDs Bridge");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) MODULE_LICENSE("GPL v2");