From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.112 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796213156; b=Lw+ckh5ZJX9xJUy9inWM6YKhAWY9QcyNJgMRg/37hUo9Csjh37uIzmXLu2cVCfVLght/ CnyyZhpwPgzSiSqpyay3JMGMdsLW7hCIJFfiNBY1FFggW8mZ7PJTWrV3Dzzot9GXqfaJ1 m4RZsyJEgac7T8UDalsSzLK2tRuk02536rpQux3Pabo5YmNPRK112K1vELKgFfxQQGpvJ UB9026zVyfUKmCl8QNO6TQDOUxUBjp8qFDI2/jabLZ4jVLtp089pBtqnuA14poAzhcItY W0Q34PJan0Jp6Z5OsjGqMAH/lD+az5ayASJIahyWsKqMN45gnY7Ct/F2Nk6e0x/4+Zw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796213156; h=DMARC-Filter:DKIM-Signature:Received:Message-ID:Subject:From:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date: MIME-Version:User-Agent:X-ZohoMailClient; bh=guFNrQSk8fLEUHjeiMY2xCEKoFtKy3NO/lKBL2cYIiY=; b=RjUSrMb2PjfqtO6WXAEvVVBbZ3EGkOTnojFA70mvKYIUzIJGdEYsK7TS/nWKWTpBON8C FIcOnIMzgu/zybgpjXKRxcWrOH86kLxGQDvSVKde2SRZ5/uJlh6JBU1JFt5qRyafF1vjs rp3/LeCL1cjNqNkmhrx+ulGcUoEtE4JZA0JSfTuLGxL7eFFOfQ+HLviM7b/mBwieXWgnL bcqe6GmNh+9cxTPOrv/ABcF65cJ/FsTHW5+l2jJ+qryXGOi4WPNsAjNi+vFp9SLIAVzN3 dfWo1JMutHouyQKzKsiF3QQvW3otPwicUrLhDFoCyk+oH3HDESsl6QsEgNQMhT8vomA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=rong.moe; spf=pass smtp.mailfrom=rong.moe; dkim=pass header.d=rong.moe header.i=@rong.moe header.a=rsa-sha256 header.s=zmail2048 header.b=Z+dcPlDw; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.112 Received: from sender6-pp-f112.zoho.com (sender6-pp-f112.zoho.com [165.173.180.112]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E1EA606A323; Wed, 2 Dec 2026 12:05:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2E1EA606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=rong.moe Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rong.moe Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=rong.moe header.i=@rong.moe header.a=rsa-sha256 header.s=zmail2048 header.b=Z+dcPlDw ARC-Seal: i=1; a=rsa-sha256; t=1796213131; cv=none; d=zohomail.com; s=zohoarc; b=LOFKRptV8LPFPBJlUeGnLbWZGvil0td/N1i+QGySIoi7hmndE7qUqYLNH9k2KLMJ/POm9B0fs8dkhQ/v0zLMOGQ7pOTVoX5MKqxfGws3Dp3A5hlXrV2tdCwapOlEPQX39VJmGzCmgJ4yUMHbfnzRHMyin6Iki6npTPigntr4oFQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796213131; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=guFNrQSk8fLEUHjeiMY2xCEKoFtKy3NO/lKBL2cYIiY=; b=DLg0xFAd32eytAHxknUOtIwlL6vlj/9f933jxAS/3HnK+luahXB5fTpTSXddmnPGtwgvbDqIv53KMW9DWFtfmpOjjEcyAvCjxsPb33OHQNmlvYJDmxNWUxYt74tk0jNsLIqfrSgTbNf3aL/jHWnNHRI6v2Xdaafv6R2sfnJZxR0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=rong.moe; spf=pass smtp.mailfrom=i@rong.moe; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=Z+dcPlDwssAP99J4OSfA4unV8oG13qk+0DkU32uqmia/9eF9G9Yce7JT4r+AzeenH3GTSd0vKvxBKr+RyZvMbvZTAKSoTi/h2w5ysOO8vNXtswp7PwQN6+M1i1O2iDKPz12TzNd+OjUIZNHFp8OIHZHNBz86YQ8hDVcpET8Mz1LQNpG5TSnyjC3KwpbnQYfpjP0DiMUZErDHmjMX4QmXF9SQJocrTdv9bLD3TgWqVakoLvrsgiWhd6metE60BN1DfbKtAnzC4/HUHYDOSY1pXvJjqYFiO8RSvfNPPbbZwmiOy8WNsDworR1sAYoWhWdTk2kURk0Uj3xCj2DyW6qzEA==; c=relaxed/relaxed; s=zmail2048; d=rong.moe; v=1; bh=guFNrQSk8fLEUHjeiMY2xCEKoFtKy3NO/lKBL2cYIiY=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 1796213128122292.49905913699683; Wed, 2 Dec 2026 04:05:28 -0800 (PST) Message-ID: Subject: Re: [PATCH v4] HID: lenovo-legion: Add keyboard backlight driver for Legion Gen 10 From: Rong Zhang To: ciro.pedrini@gmail.com, Mark Pearson , "Derek J. Clark" , Ike Panhc , Hans de Goede , Ilpo =?ISO-8859-1?Q?J=E4rvinen?= , Jiri Kosina , Benjamin Tissoires Cc: David Cemin , Marco Giunta , Werner Sembach , Aaron Erhardt , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org In-Reply-To: <20261201-legion-lighting-rfc-v4-1-02acc82bc0b6@gmail.com> References: <20261201-legion-lighting-rfc-v4-1-02acc82bc0b6@gmail.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Dec 2026 20:00:22 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Evolution 3.62.1-4 X-ZohoMailClient: External 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 >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > Signed-off-by: Ciro Pedrini > --- > Hi, >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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. >=20 > 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=3D1. >=20 > 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 ---. >=20 > 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 >=20 > 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 >=20 > 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(+) >=20 > diff --git a/MAINTAINERS b/MAINTAINERS > index ba7650cb86b0..6c622b50ac9b 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -15187,6 +15187,7 @@ F:=09Documentation/ABI/testing/sysfs-driver-hid-l= enovo-go > F:=09Documentation/ABI/testing/sysfs-driver-hid-lenovo-go-s > F:=09drivers/hid/hid-lenovo-go-s.c > F:=09drivers/hid/hid-lenovo-go.c > +F:=09drivers/hid/hid-lenovo-legion.c > F:=09drivers/hid/hid-lenovo.c > =20 > 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 > =09S. Say M here to compile this driver as a module. The module will be = called > =09hid-lenovo-go-s. > =20 > +config HID_LENOVO_LEGION > +=09tristate "Lenovo Legion laptop keyboard backlight" > +=09depends on USB_HID > +=09depends on LEDS_CLASS > +=09help > +=09Support for the LEDs of some Lenovo Legion laptops since 2025, with > +=09the brightness changes initiated by predefined shortcuts (e.g., > +=09Fn+Up/Down) reported via brightness_hw_changed. > + > +=09Say M here to compile this driver as a module. The module will be cal= led > +=09hid-lenovo-legion. > + > config HID_LETSKETCH > =09tristate "Letsketch WP9620N tablets" > =09depends 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)=09+=3D hid-lcpower.o > obj-$(CONFIG_HID_LENOVO)=09+=3D hid-lenovo.o > obj-$(CONFIG_HID_LENOVO_GO)=09+=3D hid-lenovo-go.o > obj-$(CONFIG_HID_LENOVO_GO_S)=09+=3D hid-lenovo-go-s.o > +obj-$(CONFIG_HID_LENOVO_LEGION)=09+=3D hid-lenovo-legion.o > obj-$(CONFIG_HID_LETSKETCH)=09+=3D hid-letsketch.o > obj-$(CONFIG_HID_LOGITECH)=09+=3D hid-logitech.o > obj-$(CONFIG_HID_LOGITECH)=09+=3D 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=090x837a > #define USB_DEVICE_ID_ITE_LENOVO_YOGA900=090x8396 > +#define USB_DEVICE_ID_ITE_LENOVO_LEGION_GEN10_KBD=090xc197 > #define I2C_DEVICE_ID_ITE_LENOVO_YOGA_SLIM_7X_KEYBOARD=090x8987 > #define I2C_DEVICE_ID_ITE_LENOVO_YOGA_SLIM_7X_G11_KEYBOARD=090x83db > #define USB_DEVICE_ID_ITE8595=09=090x8595 > diff --git a/drivers/hid/hid-lenovo-legion.c b/drivers/hid/hid-lenovo-leg= ion.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 re= port > + * 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 rep= orts > + * the new level on vendor input report 5 (struct legion_event). It repo= rts > + * every other set the same way, also the host's and unchanged levels. > + * > + * Op 0xD1 reads 0 where this brightness drives the backlight. The Legio= n > + * 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 ha= nded > + * to the HID LampArray of the "Lenovo Lighting" device (048d:c193), whi= ch > + * 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=09=090x07 > + > +#define LEGION_OP_GET_BRIGHTNESS=090xcd > +#define LEGION_OP_SET_BRIGHTNESS=090xce > +#define LEGION_OP_GET_COMPATIBILITY=090xd1 > +#define LEGION_MAX_BRIGHTNESS=09=099 > + > +#define LEGION_EVENT_REPORT_ID=09=090x05 > +#define LEGION_EVENT_BRIGHTNESS=09=090x02=09/* set by the host, Fn+Up, F= n+Space */ > +#define LEGION_EVENT_BRIGHTNESS_DOWN=090x03=09/* Fn+Down */ > + > +/* Request, and its answer: the value is params[0]. */ > +struct legion_msg { > +=09u8 report_id; > +=09u8 op; > +=09__le16 len;=09=09=09/* of params */ You don't need the comment, but > +=09u8 params[]; =09u8 params[] __counted_by(len); Hint: include compiler.h. This patch already depends on it because of, e.g., __packed. > +} __packed; > + > +#define LEGION_REPORT_PARAM_SIZE=09956 > +#define LEGION_REPORT_SIZE=09=09960 > +static_assert(struct_size_t(struct legion_msg, params, LEGION_REPORT_PAR= AM_SIZE) =3D=3D > +=09 LEGION_REPORT_SIZE); > + > +struct legion_event { > +=09u8 report_id; > +=09u8 event; > +=09u8 level;=09=09=09/* brightness after the change */ > +} __packed; > + > +struct legion_kbd { > +=09struct hid_device *hdev; > +=09struct mutex lock;=09=09/* requests and their answers, msg */ > +=09struct legion_msg *msg;=09=09/* LEGION_REPORT_SIZE */ A second allocation is tedious and wasteful. You can embed a DMA buffer into the struct, for example: struct legion_kbd { =09__dma_from_device_group_begin(); =09u8 buf[LEGION_REPORT_SIZE]; =09__dma_from_device_group_end(); =09struct led_classdev led; =09struct mutex lock;=09=09/* Protects buf and leds. */ =09atomic_t level;=09=09=09/* Last level set, reported or read during probe= . */ =09struct hid_device *hdev; }; Note that usually it'd be better to place the DMA buffer at the beginning of the struct to reduce holes. > +=09struct led_classdev led; > +=09atomic_t level;=09=09=09/* last level set, reported or read in probe = */ > +}; > + > +/* > + * Send one request. When @answer is non-NULL, read the controller's ans= wer > + * and return its value there. > + */ > +static int legion_raw_command(struct legion_kbd *kbd, u8 op, const u8 *p= arams, > +=09=09=09 size_t len, u8 *answer) > +{ > +=09size_t get_size =3D struct_size_t(struct legion_msg, params, 1); const? > +=09size_t set_size =3D struct_size_t(struct legion_msg, params, len); > +=09struct legion_msg *msg =3D kbd->msg; > +=09struct hid_device *hdev =3D kbd->hdev; > +=09int ret; Reverse xmas tree. > + > +=09lockdep_assert_held(&kbd->lock); > + > +=09if (len > LEGION_REPORT_PARAM_SIZE) > +=09=09return -EINVAL; > + > +=09msg->report_id =3D LEGION_REPORT_ID; > +=09msg->op =3D op; > +=09msg->len =3D cpu_to_le16(len); > +=09memcpy(msg->params, params, len); > + > +=09ret =3D hid_hw_raw_request(hdev, LEGION_REPORT_ID, (u8 *)msg, set_siz= e, > +=09=09=09=09 HID_FEATURE_REPORT, HID_REQ_SET_REPORT); > +=09if (ret < 0) > +=09=09return ret; > +=09if (!answer) > +=09=09return 0; > + > +=09ret =3D hid_hw_raw_request(hdev, LEGION_REPORT_ID, (u8 *)msg, get_siz= e, > +=09=09=09=09 HID_FEATURE_REPORT, HID_REQ_GET_REPORT); > +=09if (ret < 0) > +=09=09return ret; > +=09if (ret < get_size || msg->report_id !=3D LEGION_REPORT_ID || > +=09 msg->op !=3D op) { > +=09=09hid_dbg(hdev, "unexpected answer to 0x%02x\n", op); > +=09=09return -EIO; The IO itself is successful. How about -EPROTO? > +=09} > + > +=09*answer =3D msg->params[0]; > +=09return 0; > +} > + > +static int legion_simple_command(struct legion_kbd *kbd, u8 op, u8 param= , > +=09=09=09=09 u8 *answer) > +{ > +=09return 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) > +{ > +=09u8 value; > +=09int ret; > + > +=09lockdep_assert_held(&kbd->lock); > + > +=09ret =3D legion_simple_command(kbd, LEGION_OP_GET_BRIGHTNESS, 0, &valu= e); > +=09if (ret) { > +=09=09hid_dbg(kbd->hdev, "reading the brightness failed: %d\n", ret); > +=09=09return ret; > +=09} > + > +=09if (value > kbd->led.max_brightness) { > +=09=09hid_dbg(kbd->hdev, "brightness out of range: %u\n", value); > +=09=09return -EIO; > +=09} > + > +=09return value; > +} > + > +static int legion_brightness_set(struct led_classdev *led, > +=09=09=09=09 enum led_brightness value) > +{ > +=09struct legion_kbd *kbd =3D container_of(led, struct legion_kbd, led); > +=09int old, ret; > + > +=09if (value > led->max_brightness) > +=09=09return -EINVAL; > + > +=09guard(mutex)(&kbd->lock); > + > +=09/* Before sending: the report of this set may come before we return. = */ > +=09old =3D atomic_xchg(&kbd->level, value); > +=09ret =3D legion_simple_command(kbd, LEGION_OP_SET_BRIGHTNESS, value, N= ULL); > +=09if (ret) > +=09=09atomic_cmpxchg(&kbd->level, value, old); > + > +=09return ret; > +} > + > +static enum led_brightness legion_brightness_get(struct led_classdev *le= d) > +{ > +=09struct legion_kbd *kbd =3D container_of(led, struct legion_kbd, led); > + > +=09guard(mutex)(&kbd->lock); > + > +=09return legion_read_level(kbd); > +} > + > +static int legion_raw_event(struct hid_device *hdev, struct hid_report *= report, > +=09=09=09 u8 *data, int size) > +{ > +=09const struct legion_event *ev =3D (const struct legion_event *)data; > +=09struct legion_kbd *kbd =3D hid_get_drvdata(hdev); > + > +=09if (!kbd)=09=09=09/* no LED on this model */ > +=09=09return 0; > + > +=09if (size < sizeof(*ev) || ev->report_id !=3D LEGION_EVENT_REPORT_ID) > +=09=09return 0; > + > +=09if (ev->event !=3D LEGION_EVENT_BRIGHTNESS && > +=09 ev->event !=3D LEGION_EVENT_BRIGHTNESS_DOWN) > +=09=09return 0; > + > +=09if (ev->level > kbd->led.max_brightness) { > +=09=09hid_dbg(hdev, "brightness event out of range: %u\n", ev->level); > +=09=09return 0; > +=09} > + > +=09/* > +=09 * Only a change of the level is the hardware's own: the host's sets > +=09 * and Fn+Space are reported too, with the level already known. > +=09 */ > +=09if (atomic_xchg(&kbd->level, ev->level) =3D=3D ev->level) > +=09=09return 0; > + > +=09/* > +=09 * hid-core calls this only after probe() has returned and not once > +=09 * remove() has started, so the LED is registered. > +=09 */ > +=09led_classdev_notify_brightness_hw_changed(&kbd->led, ev->level); > + > +=09return 0;=09/* let hid-core handle the report as usual */ > +} > + > +static void legion_stop(void *data) > +{ > +=09struct hid_device *hdev =3D data; > + > +=09hid_hw_close(hdev); > +=09hid_hw_stop(hdev); > +} > + > +static int legion_probe(struct hid_device *hdev, const struct hid_device= _id *id) > +{ > +=09struct legion_kbd *kbd; > +=09u8 compat =3D 0; > +=09int ret; > + > +=09/* > +=09 * Keep the input devices hid-generic creates for this keyboard (whic= h > +=09 * sets the same quirk): without it the keyboard, its consumer keys a= nd > +=09 * its radio button would become one input device. > +=09 */ > +=09hdev->quirks |=3D HID_QUIRK_INPUT_PER_APP; > + > +=09ret =3D hid_parse(hdev); > +=09if (ret) > +=09=09return ret; > + > +=09kbd =3D devm_kzalloc_obj(&hdev->dev, *kbd); > +=09if (!kbd) > +=09=09return -ENOMEM; > + > +=09/* Not on the stack: usbhid passes the buffer to the USB core for DMA= . */ > +=09kbd->msg =3D devm_kzalloc(&hdev->dev, LEGION_REPORT_SIZE, GFP_KERNEL)= ; > +=09if (!kbd->msg) > +=09=09return -ENOMEM; > + > +=09ret =3D devm_mutex_init(&hdev->dev, &kbd->lock); > +=09if (ret) > +=09=09return ret; > + > +=09kbd->hdev =3D hdev; > +=09kbd->led.max_brightness =3D LEGION_MAX_BRIGHTNESS; > + > +=09ret =3D hid_hw_start(hdev, HID_CONNECT_DEFAULT); > +=09if (ret) > +=09=09return ret; > + > +=09/* Keep the interrupt endpoint polled so hotkey reports always arrive= . */ > +=09ret =3D hid_hw_open(hdev); > +=09if (ret) { > +=09=09hid_hw_stop(hdev); > +=09=09return ret; > +=09} > + > +=09ret =3D devm_add_action_or_reset(&hdev->dev, legion_stop, hdev); > +=09if (ret) > +=09=09return ret; > + > +=09scoped_guard(mutex, &kbd->lock) { > +=09=09ret =3D legion_simple_command(kbd, LEGION_OP_GET_COMPATIBILITY, 0, > +=09=09=09=09=09 &compat); > +=09=09if (!ret && !compat) > +=09=09=09ret =3D legion_read_level(kbd); > +=09} > + > +=09if (ret < 0) > +=09=09return ret; > +=09if (compat) { > +=09=09hid_dbg(hdev, "backlight not on this controller (0x%02x)\n", compa= t); controller does not own keyboard backlight (compat level: 0x%02x)\n > +=09=09return 0; > +=09} > +=09atomic_set(&kbd->level, ret); > + > +=09kbd->led.name =3D ":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 > +=09kbd->led.brightness_set_blocking =3D legion_brightness_set; > +=09kbd->led.brightness_get =3D legion_brightness_get; > +=09kbd->led.flags =3D LED_RETAIN_AT_SHUTDOWN | LED_BRIGHT_HW_CHANGED; > + > +=09ret =3D devm_led_classdev_register(&hdev->dev, &kbd->led); > +=09if (ret) > +=09=09return ret; > + > +=09hid_set_drvdata(hdev, kbd); > +=09return 0; > +} > + > +static void legion_remove(struct hid_device *hdev) > +{ > +=09/* > +=09 * Done via devm, the LED first. Without .remove, hid-core would stop > +=09 * the device before releasing devm resources: before the LED is gone= , > +=09 * and twice. > +=09 */ > +} > + > +static const struct hid_device_id legion_devices[] =3D { > +=09{ HID_USB_DEVICE(USB_VENDOR_ID_ITE, USB_DEVICE_ID_ITE_LENOVO_LEGION_G= EN10_KBD) }, > +=09{ } > +}; > +MODULE_DEVICE_TABLE(hid, legion_devices); > + > +static struct hid_driver legion_driver =3D { > +=09.name =3D "hid-lenovo-legion", > +=09.id_table =3D legion_devices, > +=09.probe =3D legion_probe, > +=09.remove =3D legion_remove, > +=09.raw_event =3D legion_raw_event, > +}; > +module_hid_driver(legion_driver); > + > +MODULE_AUTHOR("Ciro Pedrini "); > +MODULE_DESCRIPTION("Keyboard backlight of Lenovo Legion laptops"); > +MODULE_LICENSE("GPL"); >=20 > --- > base-commit: f09fec5a32d43dd3f9e32d531d05422a9f6191d0 > change-id: 20261109-legion-lighting-rfc-dc32165df52d >=20 > Best regards,