Since LED support via input-leds is grandfathered, the new LED is added directly in hid-input. Signed-off-by: Bernhard Seibold <mail@xxxxxxxxxxxxxxxxxxx> --- drivers/hid/Kconfig | 11 +++++ drivers/hid/hid-input.c | 92 +++++++++++++++++++++++++++++++++++++++++ include/linux/hid.h | 1 + 3 files changed, 104 insertions(+) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 4ce74af79657..71de0af8f460 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -42,6 +42,17 @@ config HID_BATTERY_STRENGTH that support this feature) through power_supply class so that userspace tools, such as upower, can display it. +config HID_LEDS + bool "LED support for HID devices" + select LEDS_CLASS + default y + help + This option adds support for LEDs on HID devices. Currently, the + only supported LED is microphone mute. For all other LEDs, + enable CONFIG_INPUT_LEDS. + + If unsure, say Y. + config HIDRAW bool "/dev/hidraw raw HID device support" help diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c index c8b20d44b147..32d3e6a2ac44 100644 --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -16,6 +16,7 @@ #include <linux/module.h> #include <linux/slab.h> #include <linux/kernel.h> +#include <linux/leds.h> #include <linux/hid.h> #include <linux/hid-debug.h> @@ -104,6 +105,9 @@ static const struct usage_priority hidinput_usages_priorities[] = { #define map_key_clear(c) hid_map_usage_clear(hidinput, usage, &bit, \ &max, EV_KEY, (c)) +#define setup_led(name, trigger) \ + hidinput_setup_led(device, field, usage_index, name, trigger) + static bool match_scancode(struct hid_usage *usage, unsigned int cur_idx, unsigned int scancode) { @@ -674,6 +678,88 @@ static bool hidinput_set_battery_charge_status(struct hid_device *dev, } #endif /* CONFIG_HID_BATTERY_STRENGTH */ +#ifdef CONFIG_HID_LEDS + +struct hid_led { + struct list_head list; + struct led_classdev cdev; + struct hid_field *field; + unsigned int offset; + char *name; +}; + +static int hidinput_led_brightness_set(struct led_classdev *cdev, + enum led_brightness value) +{ + struct device *dev = cdev->dev->parent; + struct hid_device *device = to_hid_device(dev); + struct hid_led *led = container_of(cdev, struct hid_led, cdev); + + hid_set_field(led->field, led->offset, !!value); + schedule_work(&device->led_work); + + return 0; +} + +static void hidinput_setup_led(struct hid_device *device, + struct hid_field *field, unsigned int offset, + const char *name, const char *trigger) +{ + struct hid_led *led; + struct device *dev = &device->dev; + struct device *idev = &field->hidinput->input->dev; + + led = kzalloc(sizeof(*led), GFP_KERNEL); + if (!led) + return; + + led->name = kasprintf(GFP_KERNEL, "%s::%s", dev_name(idev), name); + if (!led->name) { + kfree(led); + return; + } + + led->cdev.name = led->name; + led->cdev.default_trigger = trigger; + led->cdev.max_brightness = 1; + led->cdev.brightness_set_blocking = hidinput_led_brightness_set; + led->field = field; + led->offset = offset; + + if (led_classdev_register(dev, &led->cdev)) { + kfree(name); + kfree(led); + return; + } + + list_add_tail(&led->list, &device->leds); +} + +static void hidinput_cleanup_leds(struct hid_device *device) +{ + struct hid_led *led, *tmp; + + list_for_each_entry_safe(led, tmp, &device->leds, list) { + led_classdev_unregister(&led->cdev); + kfree(led->name); + kfree(led); + } +} + +#else /* !CONFIG_HID_LEDS */ + +static void hidinput_setup_led(struct hid_device *device, + struct hid_field *field, unsigned int offset, + const char *name, const char *trigger) +{ +} + +static void hidinput_cleanup_leds(struct hid_device *device) +{ +} + +#endif /* CONFIG_HID_LEDS */ + static bool hidinput_field_in_collection(struct hid_device *device, struct hid_field *field, unsigned int type, unsigned int usage) { @@ -935,6 +1021,10 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel case 0x19: map_led (LED_MAIL); break; /* "Message Waiting" */ case 0x4d: map_led (LED_CHARGING); break; /* "External Power Connected" */ + case 0x21: /* "Microphone" */ + setup_led("micmute", "audio-micmute"); + break; + default: goto ignore; } break; @@ -2282,6 +2372,7 @@ int hidinput_connect(struct hid_device *hid, unsigned int force) int i, k; INIT_LIST_HEAD(&hid->inputs); + INIT_LIST_HEAD(&hid->leds); INIT_WORK(&hid->led_work, hidinput_led_worker); hid->status &= ~HID_STAT_DUP_DETECTED; @@ -2380,6 +2471,7 @@ void hidinput_disconnect(struct hid_device *hid) { struct hid_input *hidinput, *next; + hidinput_cleanup_leds(hid); hidinput_cleanup_battery(hid); list_for_each_entry_safe(hidinput, next, &hid->inputs, list) { diff --git a/include/linux/hid.h b/include/linux/hid.h index bf43f3ff6664..d7cea5476979 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -617,6 +617,7 @@ struct hid_device { /* device report descriptor */ unsigned country; /* HID country */ struct hid_report_enum report_enum[HID_REPORT_TYPES]; struct work_struct led_work; /* delayed LED worker */ + struct list_head leds; /* List of associated LEDs */ struct semaphore driver_input_lock; /* protects the current driver */ struct device dev; /* device */ -- 2.43.0