mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rong Zhang <i@rong.moe>
To: Marco Giunta <marco_giunta@outlook.it>,
	 devnull+ciro.pedrini.gmail.com@kernel.org
Cc: aer@tuxedocomputers.com, bentiss@kernel.org,
	ciro.pedrini@gmail.com,  dcemin@nvidia.com,
	derekjohn.clark@gmail.com, hansg@kernel.org, ikepanhc@gmail.com,
		ilpo.jarvinen@linux.intel.com, jikos@kernel.org,
	linux-input@vger.kernel.org, 	linux-kernel@vger.kernel.org,
	mpearson-lenovo@squebb.ca, 	platform-driver-x86@vger.kernel.org,
	wse@tuxedocomputers.com
Subject: Re: [PATCH v3 0/2] Keyboard backlight for the Lenovo Legion Pro 7 Gen 10
Date: Sun, 29 Nov 2026 02:43:48 +0800	[thread overview]
Message-ID: <e82cbbab27b497c3151fe179cf71ff8d5a8a3331.camel@rong.moe> (raw)
In-Reply-To: <BYAPR19MB229323713AAFB68EC5D8443EFC402@BYAPR19MB2293.namprd19.prod.outlook.com>

Hi Macro,

Thanks for your detailed information.

On Sat, 2026-11-28 at 18:46 +0100, Marco Giunta wrote:
> Hi all,
> 
> Ciro, thank you for this series. Some time ago I started working on
> the exact same driver, but never really had the time to polish it and
> submit it. Now that you did, I can happily and lazily skip this effort.
> 
> I have some high-level comments on the series. For reference, my laptop is
> the Legion Pro 7 16AFR10H, which is the AMD variant of the 16IAX10H.
> 
> Apologies for not sending this earlier, I missed v1 and v2.
> I see that in v3 you already fixed e.g. ignoring the spurious "brightness
> up" event reported after OS/fn+up/fn+space-induced state changes or
> removing the unnecessary sleep, but there are still some missing things.
> 
> 1. More WMI event codes to be ignored
> --------------------------------------------------------------------------
> (This part is for both Ciro and David, since Rong suggested moving patch 1
> into David's series.)
> 
> On my laptop, the WMI codes emitted when pressing fn+space or fn+up/down
> encode the new state, not just "state changed": there are 6 codes for the
> RGB presets and 4 for the brightness levels. Here is the code I had
> written in my draft series:
> 
> 	/*
> 	 * Legion keyboard backlight level, Fn+Up/Down (handled by the firmware,
> 	 * changes reported by hid-lenovo-legion)
> 	 */
> 	{ KE_IGNORE,	0x18 | IDEAPAD_WMI_KEY },	/* off */
> 	{ KE_IGNORE,	0x19 | IDEAPAD_WMI_KEY },	/* low */
> 	{ KE_IGNORE,	0x1a | IDEAPAD_WMI_KEY },	/* medium */
> 	{ KE_IGNORE,	0x26 | IDEAPAD_WMI_KEY },	/* high */
> 	/* Legion keyboard lighting preset, Fn+Space (handled by the firmware) */
> 	{ KE_IGNORE,	0x20 | IDEAPAD_WMI_KEY },	/* preset 1 */
> 	{ KE_IGNORE,	0x21 | IDEAPAD_WMI_KEY },	/* preset 2 */
> 	{ KE_IGNORE,	0x22 | IDEAPAD_WMI_KEY },	/* preset 3 */
> 	{ KE_IGNORE,	0x23 | IDEAPAD_WMI_KEY },	/* preset 4 */
> 	{ KE_IGNORE,	0x24 | IDEAPAD_WMI_KEY },	/* preset 5 */
> 	{ KE_IGNORE,	0x25 | IDEAPAD_WMI_KEY },	/* preset 6 */
> 
> Ciro:
> The three codes in your patch 1 (0x21 for fn+space, 0x1a for fn+up, 0x19
> for fn+down) are exactly my "preset 2", "medium" and "low". Of course the
> Intel and AMD models' firmwares may simply differ here, but it could also
> be that your laptop behaves like mine and each key press you tested
> happened to land on those states.
> Could you check by cycling through *all* brightness levels and presets
> with evtest? If so, the other seven codes still reach userspace as
> KEY_UNKNOWN on your machine as well with the current patch 1.
> 
> My guess is that the two laptops behave identically, since it would be
> unusual for these WMI codes not to be Lenovo-wide constants, especially on
> two closely related models, but this is just a guess on my end. Maybe
> Rong or Mark have some insight on this.
> 
> Regardless of the exact meaning on any particular model, the firmware
> handles these keys and the HID driver reports the brightness changes, so
> none of these WMI events are needed and all ten should be KE_IGNORE'd.
> This is the only relevant (and actionable) point I'm trying to make in
> this section.
> 
> Whichever version of patch 1 carries all ten codes, in this series or in
> David's, will get my Reviewed-by tag.
> 
> 2. 0xd1 compatibility check on other Legion models
> --------------------------------------------------------------------------
> On my Legion, and I would assume yours as well, the KBLC ACPI method is
> present in firmware but it's a stub: trying to do anything with it fails,
> and ideapad-laptop says:
> 
> ideapad_acpi VPC2004:00: Unknown keyboard type: 1
> 
> without registering any platform::kbd_backlight LED cdev.
> 
> This makes sense, given that the backlight is actually driven by the USB
> keyboard controller. However, the opposite happens on the Legion Y9000P
> IAX10, which is the China-exclusive version of the 16IAX10H and has the
> same keyboard controller with the same USB ID. A tester of mine kindly ran
> some tests on that laptop; here's the summary:
> 
> * The IAX10 only has a white backlight, similarly to most Yogas.
> * Consistently with the above, that device uses the KBLC method: the
>   platform::kbd_backlight LED is already registered and functional
>   (tested with upower on an unpatched kernel). Regarding the hardware,
>   the keyboard's brightness is changed with the Yoga-like fn+space combo,
>   while fn+up/down does nothing.
> * On the HID side, GET_BRIGHTNESS always returns 9 regardless of the
>   actual level, and the controller sends no report 5 on fn+space or on
>   brightness changes through platform::kbd_backlight.
> 
> Also note that your 16IAX10H and the tester's Y9000P report the same BIOS
> version, Q7CN77WW (on top of the same 83F5 DMI), so neither the USB ID nor
> DMI/BIOS matching can tell these machines apart. For reference, Lenovo's
> BIOS changelogs, e.g. the latest one:
> https://download.lenovo.com/consumer/mobiles/q7cn83ww.txt
> officially confirm that the same BIOS image is shared between the Legion
> Pro 7 16IAX10H, the Legion Y9000P IAX10H and the Legion Y9000P IAX10.
> 
> So if the driver binds based on the USB ID alone, it will register a
> second, broken kbd_backlight LED on devices that already have a
> functioning one.
> 
> Fortunately, there seems to be a programmatic way of telling these cases
> apart. This project:
> https://github.com/LenovoLegionToolkit-Team/LenovoLegionToolkit/
> is a GPLv3 Windows app aiming to replace the proprietary Lenovo apps, and
> includes a complete reverse engineering of the keyboard's HID protocol.
> 
> Here is a userspace Linux/Python port of the above targeting exclusively
> the RGB functionality:
> https://github.com/alstergee/legion-spectrum-control/
> 
> One opcode reported in these repos that your driver doesn't use is 0xd1,
> which is called "compatibility". I have checked that on my 16AFR10H it
> returns 0, while on my tester's Y9000P it returns 1, so indeed it seems
> like the right tool for the job. Could you confirm that it returns 0
> on your 16IAX10H too, before relying on it?
> 
> Overall, my suggestion would be to add a small helper, something like
> legion_is_compatible(), that returns whether the response to 0xd1 is zero;
> alternatively, something like legion_check_compatibility() that returns
> -ENODEV on failure (I personally prefer the simpler version, but I'll
> leave such details to Rong and Ilpo). 

ACK.

> Either way, I suggest using this
> helper in probe to conditionally skip the LED registration on incompatible
> models, while still keeping the keyboard itself working.
> 
> 3. Legion Pro 5 Gen 10 models with ID 0xc195
> --------------------------------------------------------------------------
> This part isn't directed at anyone in this thread specifically.
> In some open issues in the second GitHub repo I linked, people report
> trying these HID commands in userspace on Legion Pro 5 Gen 10 models (also
> from 2025), whose keyboard controllers have ID 0xc195 instead of 0xc197.
> Brightness and whole-keyboard colour reportedly work, while per-key
> colours light up the wrong keys (which doesn't matter for this driver,
> since it only handles the brightness for now).
> 
> So, if any owner of a Pro 5 Gen 10 model is reading this, please consider
> testing the driver on your device with 0xc195 added to the legion_devices
> table. (I wouldn't recommend adding the ID to the patch without direct
> confirmation.)
> 
> 4. Some notes on patch 2
> --------------------------------------------------------------------------
> I haven't had time to review the code in detail, but a few things stood
> out while skimming through this thread and the past ones:
> 
> * I gave v3 a quick try on my 16AFR10H (on 7.3.6 + Fedora 45 KDE, with
>   devm_kzalloc_obj() replaced by devm_kzalloc() since the former is only
>   available from 7.4). Everything works as expected based on a 5-minute
>   test, including the short reports, suspend/resume (the level is kept
>   and the hotkeys still update the OSD afterwards) and reboots (the level
>   is kept from GRUB onwards, and KDE's slider picks it up).
>   Also, exposing the raw 0-9 range turns out to be a good choice: in my
>   draft I had limited it to the 0/3/6/9 steps used by fn+up/down, but on
>   my laptop the intermediate levels are all visibly different, so KDE's
>   slider gives finer control than the hotkeys do. I didn't know the
>   keyboard had these "hidden" intermediate levels, so thanks for that.
>   BTW, mixing software and hardware controls works as well: starting
>   from an intermediate level (i.e. not a multiple of 3), fn+up and
>   fn+down snap to the nearest multiple of 3 in that direction (e.g.
>   1 + fn+up -> 3, 4 + fn+down -> 3), and KDE's OSD follows on screen.
>   So if this were the final revision I'd give my Tested-by, but I'll
>   save that for a later one.
> * Separately from Rong's question about the function part of the name,
>   "legion::kbd_backlight" uses a vendor/product name as the devicename,
>   which Documentation/leds/leds-class.rst describes as deprecated;
>   ":kbd_backlight" is the documented form, and my suggestion going
>   forward.
> * About Rong's suggestion to serialize legion_brightness_{set,get}() and
>   legion_raw_event() with kbd->lock: if I'm not mistaken, kbd->lock is
>   a mutex and raw_event runs in interrupt context, so it can't be taken
>   there.

Indeed. I missed that. legion_raw_event() doesn't need to be serialized.

>   In my draft I kept a mutex in set/get around the whole SET/GET exchange
>   (with a lockdep assertion in the command helper), plus a spinlock
>   protecting only the cached level, which set() and raw_event both read
>   and write. Unlike your v1 spinlock, which only guarded the redundant
>   "ready" flag, this one protects the current brightness state, which is
>   shared with raw_event.

IMO, using the additional spinlock is equivalent to:

   legion_brightness_set()
   {
   	guard(mutex)();
   
   	old = xchg(&cached, new);
   	ret = legion_simple_command();
   	if (ret)
   		cmpxchg(&cached, old, new);
   }
   
   legion_brightness_get()
   {
   	guard(mutex)();
   
   	legion_simple_command();
   
   	/* With the mutex, there is no need to update the cache brightness. */
   }
   
   legion_raw_event()
   {
   	old = xchg(&cached, new);
   	if (old == new)
   		return;
   }

Thanks,
Marco

>   That said, most of my experience is in the sound tree, so I'm not 100%
>   sure this is the best (or even a correct) approach, and I'll gladly
>   defer to Rong on the details.
> 
> 5. In conclusion...
> --------------------------------------------------------------------------
> Ciro, I'm sure Rong, Ilpo and Mark will offer much more useful and timely
> commentary on the code than I could, so please don't wait for another mail
> from me before sending v4. And when you do, please CC me so I can provide
> my Tested-by and Reviewed-by tags ASAP. Again, I'm quite busy at the
> moment, so I can't give an in-depth review for now, but I'll gladly do
> one later if possible.
> 
> Thanks again for working on this.
> 
> Best regards,
> Marco

      reply	other threads:[~2026-11-28 18:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  1:11 Ciro Pedrini via B4 Relay
2026-11-27  1:11 ` [PATCH v3 1/2] platform/x86: ideapad-laptop: Ignore keyboard backlight hotkeys Ciro Pedrini via B4 Relay
2026-11-27  1:11 ` [PATCH v3 2/2] HID: lenovo-legion: Add keyboard backlight driver for Legion Gen 10 Ciro Pedrini via B4 Relay
2026-11-28 12:01   ` Rong Zhang
2026-11-28 12:00 ` [PATCH v3 0/2] Keyboard backlight for the Lenovo Legion Pro 7 " Rong Zhang
2026-11-28 17:46 ` Marco Giunta
2026-11-28 18:43   ` Rong Zhang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e82cbbab27b497c3151fe179cf71ff8d5a8a3331.camel@rong.moe \
    --to=i@rong.moe \
    --cc=aer@tuxedocomputers.com \
    --cc=bentiss@kernel.org \
    --cc=ciro.pedrini@gmail.com \
    --cc=dcemin@nvidia.com \
    --cc=derekjohn.clark@gmail.com \
    --cc=devnull+ciro.pedrini.gmail.com@kernel.org \
    --cc=hansg@kernel.org \
    --cc=ikepanhc@gmail.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marco_giunta@outlook.it \
    --cc=mpearson-lenovo@squebb.ca \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=wse@tuxedocomputers.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®