From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.16 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796220326; b=Rtu/ML+m1NqHXfBPIsnRT6RxYxa1/Ul4dtT2yd8qWfw9lbMyCPafkpEmennJHsf8oD17 ibNEz4qAwJjryYGPmL4w7MUzZ/0rjtkXLjJKWi0PtD/9H60r1CpGfGoY5C+AWQ3cUrfzz mKzNYgB7lWM0FKMxQomh8RKZKAcgpdNDjraF095K1/ewOwFEGii9d3XgD09XBpyziM42T GG6zy6kWIiTh3MZNXdvQetNj9VFEIRtU1woVAY7TA929XZR/aZFFTrhBt3N2hN8SGGATE J4rP9TElVQVHZw7/7A+F5NHj4Hg5aipgorinKPGddwWmkPOkogUSqyj+ccIoktMr4mg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796220326; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:Date:To:cc: Subject:In-Reply-To:Message-ID:References:MIME-Version:Content-Type; bh=FXQswU/CYcJoInfct/pNek+TeBRZDXIWjhZ6rVG4Mmk=; b=Mq+wcUv1v3yVeKMNThy81iUaMhJUlcpEFbXQuVGyYWx83OAZ+mkP0zM63fB3TpIDSMyI UhOP04nsUEKfDSkudMHXyWNcf+H5HfrPQGqaYYSmr3tmy6CKpdQyAQ+7/g4d6fNnAptnT PkaCqN+poZcZ4bROHzCmkoAcunAnQdniPuIFjIDbZMB+KZQnYfVRr2K+FB7gtluSAQEBh 2nFHhv9hlDpQjFzPWFh5OP8VSHEnWITG4PZNig6aiBVhUoYQy39QOl22SKQy9sJfk+SLD YFXqpmmGr4XBozxEiRikTXjukdAlsa8or5UyFCQC4UXriJaozi2n0Tg3z1gqqlvU28g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=bi8PPtEB; arc=none smtp.remote-ip=192.198.163.16 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75BA4606A333; Wed, 2 Dec 2026 14:05:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 75BA4606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=bi8PPtEB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796220325; x=1827756325; h=from:date:to:cc:subject:in-reply-to:message-id: references:mime-version; bh=FXQswU/CYcJoInfct/pNek+TeBRZDXIWjhZ6rVG4Mmk=; b=bi8PPtEBHvojxydzbGAnDvBy5WCQPDTLcNbZxXvz1RiNdTat5qAdFYXk R6Fg1XlxFFyaHzZEEiFjH6S1PZ256sz4mEhktW28OLGDEqA3ulyqPgnVX 36EFrWmbEMI0FrwM3PxYn8gB26Se3eGjln0ZYqHxkHHX+anBln/FOO+iL W9fL32x1H6borarO0DSzAtjJKZIlsfuKbObUzBl2EONwS58Gq1nhGdi5u NNKC+fZ+bEHKYeJ2bOL8d6oa6kOLSInXX4gga+wKFXnTWWv23eNokNvNv 8O2LAAkZLqdhCtpa2tq/01q8qRwIWr5ISa3cHpvBPFDBZLPPgQ7isAVtF A==; X-CSE-ConnectionGUID: ryKzo8wdQ3OGvP50QFViRQ== X-CSE-MsgGUID: jHViKiVwQ560BlD2nOM2Uw== X-IronPort-AV: E=McAfee;i="6800,10657,11984"; a="5148441" X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="5148441" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 06:05:24 -0800 X-CSE-ConnectionGUID: N7VtkgefRQKMsw80cJtcMw== X-CSE-MsgGUID: STTte7QIS4Snxd8D0jIolw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="13347346" Received: from ijarvine-mobl1.ger.corp.intel.com (HELO localhost) ([10.245.245.219]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 06:05:18 -0800 From: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Date: Wed, 2 Dec 2026 16:05:14 +0200 (EET) To: Rong Zhang cc: ciro.pedrini@gmail.com, Mark Pearson , "Derek J. Clark" , Ike Panhc , Hans de Goede , Jiri Kosina , Benjamin Tissoires , David Cemin , Marco Giunta , Werner Sembach , Aaron Erhardt , platform-driver-x86@vger.kernel.org, LKML , linux-input@vger.kernel.org Subject: Re: [PATCH v4] HID: lenovo-legion: Add keyboard backlight driver for Legion Gen 10 In-Reply-To: Message-ID: <582b3fa9-c290-65d8-c0d0-65215f2299f5@linux.intel.com> References: <20261201-legion-lighting-rfc-v4-1-02acc82bc0b6@gmail.com> X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII On Wed, 2 Dec 2026, Rong Zhang wrote: > Hi Ciro, > > Thanks for working on this. > > On Tue, 2026-12-01 at 21:53 +0000, Ciro Pedrini via B4 Relay wrote: > > From: Ciro Pedrini > > > > The keyboard backlight of the Lenovo Legion Pro 7 16IAX10H (2025) has no > > Linux support: on the test unit nothing ever lit, because the keyboard > > controller's brightness was 0 from the factory and nothing exposes it. > > > > The ITE 8258 keyboard controller (048d:c197) is the built-in keyboard and > > carries a vendor protocol on feature report 0x07 (up to 960 bytes): a > > request, answered by reading the report back. Its hardware brightness, > > 0..9, is stored in the controller and applies to every LED of the machine > > (keys, front strip, rear vents, lid logo). The firmware handles Fn+Up and > > Fn+Down itself and reports the new level on vendor input report 5; it > > reports every other set there too, the host's and unchanged ones > > included. > > > > Expose the brightness as a kbd_backlight LED, :kbd_backlight, with > > brightness_hw_changed for the hotkeys so that desktops show their OSD. > > Only a change of the level is reported there, so the host's own sets and > > Fn+Space, which come back with the level already known, are not. > > Unregistering keeps the level (LED_RETAIN_AT_SHUTDOWN): switching it off > > would be stored by the controller. > > > > The same controller ID is used by models whose white backlight is driven > > by ideapad-laptop instead (Legion Y9000P IAX10: same DMI and BIOS). There > > the controller's op 0xD1 reads 1 instead of 0, and the driver registers > > no LED, leaving the keyboard as it is. > > > > The colours stay the controller's own effects, which Fn+Space cycles. The > > machine's HID LampArray (048d:c193) is left to hid-generic, or to a > > generic LampArray driver. > > > > Tested on the 16IAX10H (83F5) with 7.3 as a module: brightness from sysfs > > and KDE, the hotkeys with KDE's OSD, unbinding and rebinding. > > > > Signed-off-by: Ciro Pedrini > > --- > > Hi, > > > > This is v4 of the Legion keyboard backlight driver. Ilpo applied the > > ideapad-laptop patch (1/2 of v3), so this is only the HID driver now. > > > > Short background: on my laptop the keyboard backlight never turned on > > under Linux. The keyboard controller (048d:c197) stores a brightness from > > 0 to 9 behind a vendor feature report (0x07), it was 0 from the factory, > > and nothing on Linux could change it. That brightness dims every LED of > > the machine. Fn+Up/Down/Space are handled by the firmware, which sends > > the new level on a vendor input report. The driver exposes the > > brightness as a kbd_backlight LED, with brightness_hw_changed for the > > hotkeys so the desktop OSD works. > > > > Besides the locking changes from the v3 review, v4 checks op 0xD1 in > > probe, as Marco suggested: the Legion Y9000P IAX10 has the same > > controller ID, DMI and BIOS, but a white backlight driven by > > ideapad-laptop, and reads 1 there (0 here and on Marco's 16AFR10H). On > > such a machine the driver registers no LED and leaves the keyboard as > > it is. I could only test the 0 case. > > > > The other WMI codes Marco found (each brightness level and effect has > > its own) will follow as a separate ideapad-laptop patch on top of > > Ilpo's branch. > > > > I tested it on my 16IAX10H (83F5, BIOS Q7CN77WW) with 7.3 as a module: > > brightness from sysfs and KDE, the hotkeys and the OSD, Fn+Space, > > unbind and rebind. It's based on pdx86 review-ilpo-next and builds with > > W=1. > > > > About how this was made: I found the protocol with usbmon captures and > > by looking at Lenovo's Windows software. I wrote the code with help from > > Claude Code (Claude Opus 5.5). I went through all of it and tested it > > myself, and the patch has a short note about it below the ---. > > > > Thanks, > > Ciro > > --- > > Changes in v4: > > - Only the HID driver: the ideapad-laptop patch was applied by Ilpo. > > - Locking (Rong, Marco, Sashiko): brightness set/get hold the mutex for > > the whole exchange and legion_raw_command() asserts it; the level is an > > atomic_t shared with raw_event (xchg, and cmpxchg to undo a failed > > set); get no longer updates it, probe reads it once. > > - Back to one buffer allocated in probe; no memset before the GET (Rong). > > - Op 0xD1 is read in probe; if it isn't 0, no LED is registered (Marco, > > Rong). > > - LED name ":kbd_backlight", without the vendor devicename (Marco). > > - Link to v3: https://lore.kernel.org/r/20261127-legion-lighting-rfc-v3-0-15c55d9f15b7@gmail.com > > > > Changes in v3: > > - Short reports: send only the request bytes and read only the answer > > bytes, right after the request; no more 100 ms wait (Rong). > > - The buffer is allocated per exchange. Not on the stack as Rong > > suggested: usbhid hands it to the USB core for DMA. > > - legion_raw_command() and legion_simple_command(), and the request > > length checked against the 960-byte report with a static_assert > > (Rong). > > - Out-of-range values and failed reads return errors instead of being > > clamped or replaced; dbg messages, no hex dump (Ilpo, Rong). > > - devm for hid_hw_start()/hid_hw_open() and the LED; .remove stays, > > empty, so hid-core doesn't stop the device twice (Ilpo, Rong). > > - Dropped the (int) casts and the "(2025)"; comments trimmed, the one > > about HID_QUIRK_INPUT_PER_APP now says why; Kconfig help about what > > the driver does (Ilpo, Rong). > > - New: only report a level change to brightness_hw_changed (see above). > > - Rebased on current pdx86 for-next; Rong's Acked-by on patch 1. > > - Link to v2: https://lore.kernel.org/r/20261110-legion-lighting-rfc-v2-0-845726afb71c@gmail.com > > > > Changes in v2: > > - Only the keyboard controller (c197) now, as Rong suggested. No > > LampArray, no colours, no lighting_mode, just a kbd_backlight LED. > > - Ilpo's comments: struct with __le16 for the request, guard(), > > devm_mutex_init(), no magic numbers, includes. > > - Removed the "ready" flag, its spinlock and the work item. raw_event > > can't run during probe/remove, so it notifies the LED directly. > > - Keep led_cdev.brightness in sync with the hotkeys (from the Sashiko > > review). > > - Moved the note about the AI tool out of the changelogs, below the ---. > > - Link to v1: https://lore.kernel.org/r/20261109-legion-lighting-rfc-v1-0-a87e1db8c5e3@gmail.com > > --- > > Prepared with Claude Code (Claude Opus 5.5) from measurements on the > > laptop (usbmon captures) and analysis of the vendor's Windows software > > for interoperability; I reviewed and tested it. > > --- > > MAINTAINERS | 1 + > > drivers/hid/Kconfig | 12 ++ > > drivers/hid/Makefile | 1 + > > drivers/hid/hid-ids.h | 1 + > > drivers/hid/hid-lenovo-legion.c | 331 ++++++++++++++++++++++++++++++++++++++++ > > 5 files changed, 346 insertions(+) > > > > diff --git a/MAINTAINERS b/MAINTAINERS > > index ba7650cb86b0..6c622b50ac9b 100644 > > --- a/MAINTAINERS > > +++ b/MAINTAINERS > > @@ -15187,6 +15187,7 @@ F: Documentation/ABI/testing/sysfs-driver-hid-lenovo-go > > F: Documentation/ABI/testing/sysfs-driver-hid-lenovo-go-s > > F: drivers/hid/hid-lenovo-go-s.c > > F: drivers/hid/hid-lenovo-go.c > > +F: drivers/hid/hid-lenovo-legion.c > > F: drivers/hid/hid-lenovo.c > > > > LENOVO YOGA BOOK 9 KEYBOARD DOCK DRIVER > > diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig > > index 2481bc01c435..336be96dcd66 100644 > > --- a/drivers/hid/Kconfig > > +++ b/drivers/hid/Kconfig > > @@ -652,6 +652,18 @@ config HID_LENOVO_GO_S > > S. Say M here to compile this driver as a module. The module will be called > > hid-lenovo-go-s. > > > > +config HID_LENOVO_LEGION > > + tristate "Lenovo Legion laptop keyboard backlight" > > + depends on USB_HID > > + depends on LEDS_CLASS > > + help > > + Support for the LEDs of some Lenovo Legion laptops since 2025, with > > + the brightness changes initiated by predefined shortcuts (e.g., > > + Fn+Up/Down) reported via brightness_hw_changed. > > + > > + Say M here to compile this driver as a module. The module will be called > > + hid-lenovo-legion. > > + > > config HID_LETSKETCH > > tristate "Letsketch WP9620N tablets" > > depends on USB_HID > > diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile > > index 43a8eb0f389b..713bb1c89861 100644 > > --- a/drivers/hid/Makefile > > +++ b/drivers/hid/Makefile > > @@ -78,6 +78,7 @@ obj-$(CONFIG_HID_LCPOWER) += hid-lcpower.o > > obj-$(CONFIG_HID_LENOVO) += hid-lenovo.o > > obj-$(CONFIG_HID_LENOVO_GO) += hid-lenovo-go.o > > obj-$(CONFIG_HID_LENOVO_GO_S) += hid-lenovo-go-s.o > > +obj-$(CONFIG_HID_LENOVO_LEGION) += hid-lenovo-legion.o > > obj-$(CONFIG_HID_LETSKETCH) += hid-letsketch.o > > obj-$(CONFIG_HID_LOGITECH) += hid-logitech.o > > obj-$(CONFIG_HID_LOGITECH) += hid-lg-g15.o > > diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h > > index 91319aa6516d..992b9b147e95 100644 > > --- a/drivers/hid/hid-ids.h > > +++ b/drivers/hid/hid-ids.h > > @@ -773,6 +773,7 @@ > > #define USB_DEVICE_ID_ITE_LENOVO_YOGA2 0x8350 > > #define I2C_DEVICE_ID_ITE_LENOVO_LEGION_Y720 0x837a > > #define USB_DEVICE_ID_ITE_LENOVO_YOGA900 0x8396 > > +#define USB_DEVICE_ID_ITE_LENOVO_LEGION_GEN10_KBD 0xc197 > > #define I2C_DEVICE_ID_ITE_LENOVO_YOGA_SLIM_7X_KEYBOARD 0x8987 > > #define I2C_DEVICE_ID_ITE_LENOVO_YOGA_SLIM_7X_G11_KEYBOARD 0x83db > > #define USB_DEVICE_ID_ITE8595 0x8595 > > diff --git a/drivers/hid/hid-lenovo-legion.c b/drivers/hid/hid-lenovo-legion.c > > new file mode 100644 > > index 000000000000..181fe6bc0f84 > > --- /dev/null > > +++ b/drivers/hid/hid-lenovo-legion.c > > @@ -0,0 +1,331 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* > > + * HID driver for the keyboard backlight of Lenovo Legion laptops. > > + * > > + * The ITE 8258 keyboard controller (048d:c197) is the built-in keyboard and > > + * carries Lenovo's vendor lighting protocol on feature report 0x07 (up to > > + * 960 bytes): a request (struct legion_msg), answered by reading the report > > + * back with a GET_FEATURE. Requests and answers may be shorter than the > > + * report; the answer is ready as soon as the request has been sent. > > + * > > + * The backlight brightness is op 0xCE (set) / 0xCD (get), 0..9, stored in the > > + * controller; it applies to every LED of the machine (keys, front strip, rear > > + * vents, lid logo). The firmware handles Fn+Up / Fn+Down itself and reports > > + * the new level on vendor input report 5 (struct legion_event). It reports > > + * every other set the same way, also the host's and unchanged levels. > > + * > > + * Op 0xD1 reads 0 where this brightness drives the backlight. The Legion > > + * Y9000P IAX10 has the same controller ID, but a white backlight driven by > > + * ideapad-laptop, and reads 1: there the driver registers no LED. > > + * > > + * The colours are the controller's own effects. The LEDs can also be handed > > + * to the HID LampArray of the "Lenovo Lighting" device (048d:c193), which > > + * this driver leaves to others. > > + */ > > + > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > +#include > > + > > +#include > > + > > +#include "hid-ids.h" > > + > > +#define LEGION_REPORT_ID 0x07 > > + > > +#define LEGION_OP_GET_BRIGHTNESS 0xcd > > +#define LEGION_OP_SET_BRIGHTNESS 0xce > > +#define LEGION_OP_GET_COMPATIBILITY 0xd1 > > +#define LEGION_MAX_BRIGHTNESS 9 > > + > > +#define LEGION_EVENT_REPORT_ID 0x05 > > +#define LEGION_EVENT_BRIGHTNESS 0x02 /* set by the host, Fn+Up, Fn+Space */ > > +#define LEGION_EVENT_BRIGHTNESS_DOWN 0x03 /* Fn+Down */ > > + > > +/* Request, and its answer: the value is params[0]. */ > > +struct legion_msg { > > + u8 report_id; > > + u8 op; > > + __le16 len; /* of params */ > > You don't need the comment, but > > > + u8 params[]; > > u8 params[] __counted_by(len); > > Hint: include compiler.h. This patch already depends on it because of, > e.g., __packed. Unfortunately that won't work because the member is __le16. Sadly __counted_by() cannot take expressions like __counted_by(le16_to_cpu(len)) (AFAIK) so we cannot tell it to do the byte-swapping when hardening wants to check length. Another thing it cannot do is offsets like __counted_by(len + 2) which is common in cases that include header size into the len. Having an ability to do simple expressions like that would allow applying __counted_by() naturally to much wider set of scenarios, with all hardening benefits it brings with it when hardening is compiled in. I don't know if compiler people are aware of this major limitation or not. -- i. > > +} __packed; > > + > > +#define LEGION_REPORT_PARAM_SIZE 956 > > +#define LEGION_REPORT_SIZE 960 > > +static_assert(struct_size_t(struct legion_msg, params, LEGION_REPORT_PARAM_SIZE) == > > + LEGION_REPORT_SIZE); > > + > > +struct legion_event { > > + u8 report_id; > > + u8 event; > > + u8 level; /* brightness after the change */ > > +} __packed; > > + > > +struct legion_kbd { > > + struct hid_device *hdev; > > + struct mutex lock; /* requests and their answers, msg */ > > + struct legion_msg *msg; /* LEGION_REPORT_SIZE */ > > A second allocation is tedious and wasteful. You can embed a DMA buffer > into the struct, for example: > > struct legion_kbd { > __dma_from_device_group_begin(); > u8 buf[LEGION_REPORT_SIZE]; > __dma_from_device_group_end(); > struct led_classdev led; > struct mutex lock; /* Protects buf and leds. */ > atomic_t level; /* Last level set, reported or read during probe. */ > struct hid_device *hdev; > }; > > Note that usually it'd be better to place the DMA buffer at the beginning > of the struct to reduce holes. > > > + struct led_classdev led; > > + atomic_t level; /* last level set, reported or read in probe */ > > +}; > > + > > +/* > > + * Send one request. When @answer is non-NULL, read the controller's answer > > + * and return its value there. > > + */ > > +static int legion_raw_command(struct legion_kbd *kbd, u8 op, const u8 *params, > > + size_t len, u8 *answer) > > +{ > > + size_t get_size = struct_size_t(struct legion_msg, params, 1); > > const? > > > + size_t set_size = struct_size_t(struct legion_msg, params, len); > > + struct legion_msg *msg = kbd->msg; > > + struct hid_device *hdev = kbd->hdev; > > + int ret; > > Reverse xmas tree. > > > + > > + lockdep_assert_held(&kbd->lock); > > + > > + if (len > LEGION_REPORT_PARAM_SIZE) > > + return -EINVAL; > > + > > + msg->report_id = LEGION_REPORT_ID; > > + msg->op = op; > > + msg->len = cpu_to_le16(len); > > + memcpy(msg->params, params, len); > > + > > + ret = hid_hw_raw_request(hdev, LEGION_REPORT_ID, (u8 *)msg, set_size, > > + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); > > + if (ret < 0) > > + return ret; > > + if (!answer) > > + return 0; > > + > > + ret = hid_hw_raw_request(hdev, LEGION_REPORT_ID, (u8 *)msg, get_size, > > + HID_FEATURE_REPORT, HID_REQ_GET_REPORT); > > + if (ret < 0) > > + return ret; > > + if (ret < get_size || msg->report_id != LEGION_REPORT_ID || > > + msg->op != op) { > > + hid_dbg(hdev, "unexpected answer to 0x%02x\n", op); > > + return -EIO; > > The IO itself is successful. How about -EPROTO? > > > + } > > + > > + *answer = msg->params[0]; > > + return 0; > > +} > > + > > +static int legion_simple_command(struct legion_kbd *kbd, u8 op, u8 param, > > + u8 *answer) > > +{ > > + return legion_raw_command(kbd, op, ¶m, sizeof(param), answer); > > +} > > + > > +/* Returns the level, or a negative error. */ > > +static int legion_read_level(struct legion_kbd *kbd) > > +{ > > + u8 value; > > + int ret; > > + > > + lockdep_assert_held(&kbd->lock); > > + > > + ret = legion_simple_command(kbd, LEGION_OP_GET_BRIGHTNESS, 0, &value); > > + if (ret) { > > + hid_dbg(kbd->hdev, "reading the brightness failed: %d\n", ret); > > + return ret; > > + } > > + > > + if (value > kbd->led.max_brightness) { > > + hid_dbg(kbd->hdev, "brightness out of range: %u\n", value); > > + return -EIO; > > + } > > + > > + return value; > > +} > > + > > +static int legion_brightness_set(struct led_classdev *led, > > + enum led_brightness value) > > +{ > > + struct legion_kbd *kbd = container_of(led, struct legion_kbd, led); > > + int old, ret; > > + > > + if (value > led->max_brightness) > > + return -EINVAL; > > + > > + guard(mutex)(&kbd->lock); > > + > > + /* Before sending: the report of this set may come before we return. */ > > + old = atomic_xchg(&kbd->level, value); > > + ret = legion_simple_command(kbd, LEGION_OP_SET_BRIGHTNESS, value, NULL); > > + if (ret) > > + atomic_cmpxchg(&kbd->level, value, old); > > + > > + return ret; > > +} > > + > > +static enum led_brightness legion_brightness_get(struct led_classdev *led) > > +{ > > + struct legion_kbd *kbd = container_of(led, struct legion_kbd, led); > > + > > + guard(mutex)(&kbd->lock); > > + > > + return legion_read_level(kbd); > > +} > > + > > +static int legion_raw_event(struct hid_device *hdev, struct hid_report *report, > > + u8 *data, int size) > > +{ > > + const struct legion_event *ev = (const struct legion_event *)data; > > + struct legion_kbd *kbd = hid_get_drvdata(hdev); > > + > > + if (!kbd) /* no LED on this model */ > > + return 0; > > + > > + if (size < sizeof(*ev) || ev->report_id != LEGION_EVENT_REPORT_ID) > > + return 0; > > + > > + if (ev->event != LEGION_EVENT_BRIGHTNESS && > > + ev->event != LEGION_EVENT_BRIGHTNESS_DOWN) > > + return 0; > > + > > + if (ev->level > kbd->led.max_brightness) { > > + hid_dbg(hdev, "brightness event out of range: %u\n", ev->level); > > + return 0; > > + } > > + > > + /* > > + * Only a change of the level is the hardware's own: the host's sets > > + * and Fn+Space are reported too, with the level already known. > > + */ > > + if (atomic_xchg(&kbd->level, ev->level) == ev->level) > > + return 0; > > + > > + /* > > + * hid-core calls this only after probe() has returned and not once > > + * remove() has started, so the LED is registered. > > + */ > > + led_classdev_notify_brightness_hw_changed(&kbd->led, ev->level); > > + > > + return 0; /* let hid-core handle the report as usual */ > > +} > > + > > +static void legion_stop(void *data) > > +{ > > + struct hid_device *hdev = data; > > + > > + hid_hw_close(hdev); > > + hid_hw_stop(hdev); > > +} > > + > > +static int legion_probe(struct hid_device *hdev, const struct hid_device_id *id) > > +{ > > + struct legion_kbd *kbd; > > + u8 compat = 0; > > + int ret; > > + > > + /* > > + * Keep the input devices hid-generic creates for this keyboard (which > > + * sets the same quirk): without it the keyboard, its consumer keys and > > + * its radio button would become one input device. > > + */ > > + hdev->quirks |= HID_QUIRK_INPUT_PER_APP; > > + > > + ret = hid_parse(hdev); > > + if (ret) > > + return ret; > > + > > + kbd = devm_kzalloc_obj(&hdev->dev, *kbd); > > + if (!kbd) > > + return -ENOMEM; > > + > > + /* Not on the stack: usbhid passes the buffer to the USB core for DMA. */ > > + kbd->msg = devm_kzalloc(&hdev->dev, LEGION_REPORT_SIZE, GFP_KERNEL); > > + if (!kbd->msg) > > + return -ENOMEM; > > + > > + ret = devm_mutex_init(&hdev->dev, &kbd->lock); > > + if (ret) > > + return ret; > > + > > + kbd->hdev = hdev; > > + kbd->led.max_brightness = LEGION_MAX_BRIGHTNESS; > > + > > + ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); > > + if (ret) > > + return ret; > > + > > + /* Keep the interrupt endpoint polled so hotkey reports always arrive. */ > > + ret = hid_hw_open(hdev); > > + if (ret) { > > + hid_hw_stop(hdev); > > + return ret; > > + } > > + > > + ret = devm_add_action_or_reset(&hdev->dev, legion_stop, hdev); > > + if (ret) > > + return ret; > > + > > + scoped_guard(mutex, &kbd->lock) { > > + ret = legion_simple_command(kbd, LEGION_OP_GET_COMPATIBILITY, 0, > > + &compat); > > + if (!ret && !compat) > > + ret = legion_read_level(kbd); > > + } > > + > > + if (ret < 0) > > + return ret; > > + if (compat) { > > + hid_dbg(hdev, "backlight not on this controller (0x%02x)\n", compat); > > controller does not own keyboard backlight (compat level: 0x%02x)\n > > > + return 0; > > + } > > + atomic_set(&kbd->level, ret); > > + > > + kbd->led.name = ":kbd_backlight"; > > Please use input*::kbd_backlight. > > Generally you can check input devices by either adding an > input_configured() callback or iterating though hdev->inputs, then you > can consult hidinput->application to determine which input device is the > keyboard. The input* name can be collected using > dev_name(&hidinput->input_dev). > > Other than these issues, I believe we've been on the right path. > > Thanks, > Rong > > > + kbd->led.brightness_set_blocking = legion_brightness_set; > > + kbd->led.brightness_get = legion_brightness_get; > > + kbd->led.flags = LED_RETAIN_AT_SHUTDOWN | LED_BRIGHT_HW_CHANGED; > > + > > + ret = devm_led_classdev_register(&hdev->dev, &kbd->led); > > + if (ret) > > + return ret; > > + > > + hid_set_drvdata(hdev, kbd); > > + return 0; > > +} > > + > > +static void legion_remove(struct hid_device *hdev) > > +{ > > + /* > > + * Done via devm, the LED first. Without .remove, hid-core would stop > > + * the device before releasing devm resources: before the LED is gone, > > + * and twice. > > + */ > > +} > > + > > +static const struct hid_device_id legion_devices[] = { > > + { HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_LENOVO_LEGION_GEN10_KBD) }, > > + { } > > +}; > > +MODULE_DEVICE_TABLE(hid, legion_devices); > > + > > +static struct hid_driver legion_driver = { > > + .name = "hid-lenovo-legion", > > + .id_table = legion_devices, > > + .probe = legion_probe, > > + .remove = legion_remove, > > + .raw_event = legion_raw_event, > > +}; > > +module_hid_driver(legion_driver); > > + > > +MODULE_AUTHOR("Ciro Pedrini "); > > +MODULE_DESCRIPTION("Keyboard backlight of Lenovo Legion laptops"); > > +MODULE_LICENSE("GPL"); > > > > --- > > base-commit: f09fec5a32d43dd3f9e32d531d05422a9f6191d0 > > change-id: 20261109-legion-lighting-rfc-dc32165df52d > > > > Best regards, >