Re: Support for X1 tablet keyboard (was Re: [PATCH] platform/x86: thinkpad_acpi: handle HKEY 0x4012, 0x4013 events)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

On 2/21/21 5:30 PM, Hans de Goede wrote:
> Hi,
> 
> On 2/21/21 2:17 PM, Alexander Kobel wrote:
>> Hi,
>>
>> finally I got to investigate that patch. Thanks for your draft and explanations!
>>
>> On 2/12/21 12:42 AM, Hans de Goede wrote:
>>> Hi,
>>>
>>> On 2/9/21 4:16 PM, Alexander Kobel wrote:
>>>> Hi,
>>>>
>>>> On 2/8/21 11:17 AM, Hans de Goede wrote:
>>>>> On 2/7/21 6:55 PM, Alexander Kobel wrote:
>>>>>> <snip>
>>>>>> I'll go off and try to improve.
>>>>>
>>>>> So Nitin has been kind enough to provide us with some docs for this,
>>>>> please see me reply to Nitin's email and lets continue this part of this mail
>>>>> thread there.
>>>>
>>>> Right. I have the other patch ready, thanks to your great help. I'm
>>>> waiting for Nitin's okay whether / how much info I can copy from the
>>>> reference sheet to source code comments. Once I have that confirmation,
>>>> I will post the revised patch.
>>>>
>>>>> <snip>
>>>>>
>>>>>> Finally, I mentioned some open ends already on a post to ibm-acpi-devel
>>>>>> at https://sourceforge.net/p/ibm-acpi/mailman/message/37200082/; this
>>>>>> very question is among them.
>>>>>> I will start tackling the SW_TABLET_MODE event issue first, but if Mark
>>>>>> and Nitin can already hint about the keyboard shortcuts, it'd be highly
>>>>>> appreciated.
>>>>>
>>>>> I think I might be able to help there, a couple of months ago I bought
>>>>> a second-hand thinkpad-10 tablet which also has a USB attached keyboard.
>>>>>
>>>>> In hindsight I guess I could have asked Mark and Nitin for some more info,
>>>>> but I went on autopilot and just ran hexdump -C on the /dev/hidraw node
>>>>> to see which events all the keys send.
>>>>>
>>>>> And I fired up an usb-sniffer under Windows to figure out the audio-leds,
>>>>> since I'm used to just figure these things out without help from the vendor :)
>>>>
>>>> Yeah, why take the boring route if you know how to do all the work on
>>>> your own... ;-)
>>>>
>>>>> See the recent commits here for my work on this:
>>>>>
>>>>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/log/drivers/hid/hid-lenovo.c
>>>>
>>>> Thanks, very helpful.
>>>>
>>>>> So on the ibm-acpi list message you said that the kbd sends the following:
>>>>>
>>>>> type 4 (EV_MSC), code 4 (MSC_SCAN), value c0001
>>>>> type 1 (EV_KEY), code 240 (KEY_UNKNOWN), value 1
>>>>>
>>>>> For the Fn-keys, does it send the same MSC_SCAN code for *all* the
>>>>> non-working Fn-keys ?
>>>>
>>>> Correct. And I can confirm that /dev/hidraw1 lets me distinguish between
>>>> the keys.
>>>>
>>>>> If so then it seems that this is very much like the thinkpad 10 kbd dock
>>>>> which also does this, see the lenovo_input_mapping_tp10_ultrabook_kbd()
>>>>> function in drivers/hid/hid-lenovo.c .
>>>>>
>>>>> If I have that right, then I think we should be able to get the
>>>>> Fn keys to work without too much trouble. You could try hacking up
>>>>> drivers/hid/hid-lenovo.c a bit:
>>>>
>>>> (Not yet there, but will investigate.)
>>>>
>>>>> 1. Add an entry to the lenovo_devices array like this:
>>>>>
>>>>> 	/*
>>>>> 	 * Note bind to the HID_GROUP_GENERIC group, so that we only bind to the keyboard part,
>>>>> 	 * while letting hid-multitouch.c handle the touchpad and trackpoint.
>>>>> 	 */
>>>>>         { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
>>>>>                      USB_VENDOR_ID_LENOVO,
>>>>>                      USB_DEVICE_ID_LENOVO_X1_TAB),
>>>>>
>>>>> 2. Add the following entry to the switch-case in lenovo_input_mapping() :
>>>>>
>>>>>         case USB_DEVICE_ID_LENOVO_X1_TAB:
>>>>>                 return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
>>>>>                                                                usage, bit, max);
>>>>>
>>>>> And then build hid-lenovo.c and modprobe it.
>>>>>
>>>>> After the modprobe to:
>>>>>
>>>>> ls -l /sys/bus/hid/devices/0003:17EF:60A3.*/driver
>>>>>
>>>>> This should show 2 devices (I guess) with one being bound to hid-lenovo
>>>>> and 1 being bound to hid-multitouch.
>>>>
>>>> So far (without patching hid-lenovo), 2 bound to hid-generic and 1 to
>>>> hid-multitouch.
>>>
>>> Ok, so it seems that they kept the thinkpad 10 kbd bits (mostly) with
>>> 1 keyboard interface using the usb boot kbd interface (so that it will
>>> also work inside the BIOS) and a second interface for multimedia-keys +
>>> the mouse emulation of the thinkpad 10 touchpad, those are interfaces
>>> 1 and 2, except that they removed the mouse emulation as they added a
>>> new proper multi-touch capable touchpad as interface 3; and that one
>>> also handles the pointing stick I believe.
>>>
>>> So yes 2 bound to hid-generic, 1 bound to hid-multitouch seems to be
>>> correct.
>>
>> Right, that's what I observe.
>>
>>>>> If this works some of your Fn + F# keys will now hopefully start doing
>>>>> something, you can play around with modifying lenovo_input_mapping_tp10_ultrabook_kbd
>>>>> to make it do the right thing for your kbd.
>>>>> z
>>>>> ###
>>>>>
>>>>> About LED support, just enabling the LED support bits for the
>>>>> USB_DEVICE_ID_LENOVO_TP10UBKBD handling for now might work fine,
>>>>> but there is a tiny chance that sending the wrong command somehow puts
>>>>> the kbd in firmware update mode, I had that happen once with a Logitech
>>>>> kbd which did not seem to have any kind of handshake / passcode to avoid
>>>>> accidental fw updates (*).
>>>>>
>>>>> If you can give me a dump of the hid-descriptors for your keyboard,
>>>>> then I can check if that the LEDs might work the same way too (or not).
>>>>>
>>>>> The easiest way to get a dump is to run the following command as root:
>>>>>
>>>>> cat /sys/kernel/debug/hid/0003:17EF:60A3.*/rdesc > rdesc
>>>>>
>>>>> And then attach rdesc to your next email.
>>>>
>>>> Please find this one attached already now.
>>>> In case it helps, the * expands to 0057 0058 0059 on my system.
>>>
>>> Ok, so there still is an output-report number 9 on the second interface,
>>> which probably still controls the LEDS but its descriptors are subtly
>>> different. Although different in a good way I guess because the thinkpad
>>> 10 dock descriptor describes the 2 bytes in the output report as being
>>> in the range of 0-1 which is not how they are actually used.
>>>
>>> So I think that the code for the Thinkpad 10 ultrabook keyboard as
>>> Lenovo calls it, should also work on the X1 tablet thin keyboard.
>>
>> Mostly, modulo some key mappings, as expected.
>>
>> The good:
>>
>> LEDs are working exactly as expected with your patch, with the appropriate triggers automatically active. Perfect!
> 
> Good :)
> 
>> The bad:
>>
>> I could adjust some of the key mappings for the X1 Tablet 2nd keyboard. What I couldn't do is to get Fn+F10, Fn+F11, Fn+F12 and Fn+PrtSc to work.
>> Following the logic of /dev/hidraw1 capture (attached), those should be on usage_index 16 to 19. But apparently those are on a different usage page or something like that? Unfortunately, my RTFM skills didn't really help with figuring out how that's supposed to work.
>> (Is looking at the bit indices in /dev/hidraw traces how you figure out those mappings? If there's a better way, I'm eager to be told...)
> 
> You are swapping to low and high byte of the 3 data bytes in the report. Here is an annotated part of the descriptors to explain better:
> 
>   INPUT(3)[INPUT]
>     Field(0)
>       Application(Consumer.0001)
>       Usage(24)
>  0        Consumer.0001 F10 00000001 00000000 00000000
>  1        Consumer.0001 F11 00000010 00000000 00000000
>  2        Consumer.0001 F12 00000100 00000000 00000000
>  3        Consumer.0001 Prt 00001000 00000000 00000000
>  4        Consumer.0001
>  5        Consumer.00e2 F1  00100000 00000000 00000000
>  6        Consumer.0001
>  7        Consumer.0001
>  8        Consumer.0001 ESC 00000000 00000001 00000000
>  9        Consumer.0001 F4  00000000 00000010 00000000
> 10        Consumer.0001 F9  00000000 00000100 00000000
> 11        Consumer.00b7
> 12        Consumer.0001
> 13        Consumer.0001 F7  00000000 00100000 00000000
> 14        Consumer.0001 F8  00000000 01000000 00000000
> 15        Consumer.0001 
> 16        Consumer.0001
> 17        Consumer.0001
> 18        Consumer.0001 
> 19        Consumer.006f F6  00000000 00000000 00001000
> 20        Consumer.00ea F2  00000000 00000000 00010000
> 21        Consumer.00e9 F3  00000000 00000000 00100000
> 22        Consumer.0070 F5  00000000 00000000 01000000
> 23        Consumer.0001
>       Logical Minimum(0)
>       Logical Maximum(1)
>       Report Size(1)
>       Report Count(24)
>       Report Offset(0)
>       Flags( Variable Absolute )
> 
> Notice how the keys with standard codes (which work without mapping)
> F1 - F3, F5, F6 now all line up with _none_ Consumer.0001 entries.
> And if you check those codes in drivers/hid/hid-input.c around
> line 960 you will see the standard mappings line up too.
> 
> IOW your case 16 needs to be case 0, case 17, case 1, etc.

Gotcha, I suppose. At least on a shallow level... Thanks!
And, yes: that works perfectly.

>> Similarly - I assume - Fn+S should emit SysRq according to https://download.lenovo.com/pccbbs/mobiles_pdf/x1_tablet_gen_2_ug_en.pdf, page 51. This is not on the "consumer control" device, but the usual keyboard, so /dev/hidraw0. Again, couldn't get much further than producing a capture. But I cannot make sense of this one, because way more bits are set, so I cannot extrapolate from your code.
> 
> The input report used by the Fn + key "media keys" use a 24 bit report
> with 1 bit per key. The standard keyboard interface uses 1 byte per
> pressed key (with a maximum of 6 pressed keys) where the full byte
> encodes the scancode of the key. Normally SysRq is 0x46 but for some
> reason your keyboard is sending 0x9a you can map this by adding the following
> to the mapping function:
> 
> 	if (usage->hid == (HID_UP_KEYBOARD | 0x009a))
> 		map_key_clear(KEY_SYSRQ);

And the same here, I think. Works with return 1; after the map_key_clear, see the attached function.

> Likewise for the sleep combo:
> 
> 	if (usage->hid == (HID_UP_KEYBOARD | 0x0072))
> 		map_key_clear(KEY_SLEEP);

This seems unnecessary, as the sleep combo already emits KEY_SLEEP. Which I don't quite get, cause - if I learned correctly how to read the rdesc - sleep should be on 0x0072 should emit F23 (and 0x0078 sleep), but the key produces the 0x0072 pattern according to hidraw.
By the way, the Fn+4 for sleep also works in hid-generic, and also just once, see below.

> ###
> 
> Note chances are you have more Fn + 'letter' combinations at least on
> the thinkpad10 kbd I have:
> 
> Fn + T -> SysRq
> Fn + I -> Insert
> Fn + P -> Pause
> Fn + S -> Sysrq
> Fn + K -> ScrollLock
> Fn + B -> Pause
> 
> Note these do not need any special mappings on the thinkpad10 kbd and I guess
> the doubles may have something to do with some non qwerty keymaps.

Not the same on this keyboard. I have Fn + {B,K,P,S,End,4} for {Break,ScrLk,Pause,SysRq,Insert,Sleep}, but only SysRq was missing; the others are available on the "default" device, both with hid-lenovo and hid-generic.

>> The ugly:
>>
>> Fn+4 ("sleep") triggers the appropriate ACPI event button/sleep and emits something on /dev/hidraw0, too, but *only once*. After resuming, no reaction at all (neither on ACPI nor hidraw) until I unload and reload the hid_lenovo module.
> 
> As for the sleep key working only once, what happens after a suspend/resume ?

Nothing. At least nothing I can measure (via evtest, libinput debug-events, cat /dev/hidraw*, dmesg, journal, acpi_listen). The key simply doesn't react anymore.
Irrespective on how I wake the device. Even irrespective of whether I actually suspend the device or disable the sleep handler altogether, e.g. by systemd-inhibit. In only see the KEY_SLEEP press (no release!) event once until I reload the module or unplug and reattach the keyboard.
Very strange. And, by the way: the same for hid-generic.

> I think the key may have some special handling to avoid it sending
> a second KEY_SLEEP when the user uses it to wakeup the system, to
> avoid the system immediately going to sleep again when the user tries
> to wake the system this way.

Yes, that'd make sense. So probably the handler should restore something; apparently, that part is initialized when the module is loaded, so it's not just the keyboard firmware.

>> Finally, keyboard backlight is handled in firmware, apparently; Fn+Space is visible on /dev/hidraw1 (see attached capture), but it toggles the backlight levels without any userspace code involved, as far as I can see.
> 
> Yes I saw this in your dump, this is really weird because it sets 3 bits at once in the INPUT(3) report.
> Does it always set the same 3 bits independent of the brightness level ?

Yes; the four key presses are actually one full cycle over off, auto, dim, and bright. hidraw dumps are identical.

>> Also, the keyboard backlight doesn't create an entry in /sys/class/backlight or the like, so neither read nor write access. Out-of-the-box, at least. But I'm not even sure if this is possible in Windows.
> 
> Actuallu kbd-backlighting used the /sys/class/leds interface, but yeah that is not support by Linux ATM for this kbd.
> 
> Yeah, adding support for that (assuming the hw can do it) would definitely require making some USB dumps under Windows
> (after finding sw which can change it from within the OS under Windows).

I double-checked; somewhat unsurprisingly, there is at least a notification client on Windows that displays the new configuration after Fn+Space. The client only reports the setting; not sure if it would be technically feasible to also set the brightness level in software.


But anyways: I feel that I exploited your generosity in helping me far enough. I hope I wasn't just fed, but learned a bit how to fish for myself in the future. So, unless you're really committed to walk me through this further, I won't beg any further.

And in case you're looking for problems to tackle on the Tablet 2nd Gen, this one about the power button could be way more significant - but not sure if it's in an area that you are familiar in:
https://bbs.archlinux.org/viewtopic.php?id=248857
https://bugzilla.kernel.org/show_bug.cgi?id=204763


>> Bottom line: this is mostly usable already, modulo the adjustments for the different keys. I'd like to make F10 to F12 work before it hits testing; everything else is icing on the cake, I suppose. Do you have an hint for me how I can approach that?
> 
> See above, I think we are pretty close to solving this.

Attached is the modified version of the input mapping for this keyboard, working subject to the above mentioned restrictions. I think this is fine.
As you did all the real work, I feel this should be your contribution. But of course I can also prepare a patch on top of yours.

> Note in the mean time I've posted a hid-lenovo patch-series with various improvements related to
> the LED handling. I'll send you an offlist mail with the latest patches so that you can base any work you do on top of those.

Didn't have a look yet, but will do.

>> Also, I'd make sure that this is about the "ThinkPad X1 Tablet Thin Keyboard **Gen 2**". The consumer functions are different for the **Gen 1** keyboard, so I would also adjust the function names. I do have an old Gen 1 keyboard lying around, but unfortunately it's either broken (it lights up shortly after attaching in Windows, but doesn't report keypresses at all, and pretends to be completely dead in Linux), or it's incompatible with my X1 Tablet 2nd Gen. So I cannot test how your patch might impact the Gen1, too... :-/
> 
> I would expect the Gen1 to have a different product-id, so my patch shouldn't do anything.

Right. In this light, perhaps the function should still be called lenovo_input_mapping_x1_tab2_kbd (note the "2")?

>>> I've prepared a set of patches which enable the tp10ubkbd code on
>>> the X1 tablet thin keyboard. But beware as mentioned before there is a
>>> tiny chance that sending the wrong command somehow puts the kbd in
>>> firmware update mode. I believe that trying the tp10ubkbd code is safe,
>>> esp. since this is using a 2 byte large output report and using that
>>> for fw-updating would be a bit weird. Still there is a small risk
>>> (there always is when poking hw) so I will leave it up to you if
>>> you are willing to try this.
>>
>> No issue at all, and everything below works just as expected.
> 
> Good.
> 
> Regards,
> 
> Hans


Thanks a lot,
Alex
static int lenovo_input_mapping_x1_tab_kbd(struct hid_device *hdev,
		struct hid_input *hi, struct hid_field *field,
		struct hid_usage *usage, unsigned long **bit, int *max)
{
	/*
	 * The ThinkPad X1 Tablet Thin Keyboard uses 0x000c0001 usage for
	 * a bunch of keys which have no standard consumer page code.
	 */
	if (usage->hid == 0x000c0001) {
		switch (usage->usage_index) {
		case 0: /* Fn-F10: Enable/disable bluetooth */
			map_key_clear(KEY_BLUETOOTH);
			return 1;
		case 1: /* Fn-F11: Keyboard settings */
			map_key_clear(KEY_KEYBOARD);
			return 1;
		case 2: /* Fn-F12: User function / Cortana */
			map_key_clear(KEY_MACRO1);
			return 1;
		case 3: /* Fn-PrtSc: Snipping tool */
			map_key_clear(KEY_SELECTIVE_SCREENSHOT);
			return 1;
		case 8: /* Fn-Esc: Fn-lock toggle */
			map_key_clear(KEY_FN_ESC);
			return 1;
		case 9: /* Fn-F4: Mute/unmute microphone */
			map_key_clear(KEY_MICMUTE);
			return 1;
		case 10: /* Fn-F9: Settings */
			map_key_clear(KEY_CONFIG);
			return 1;
		case 13: /* Fn-F7: Manage external displays */
			map_key_clear(KEY_SWITCHVIDEOMODE);
			return 1;
		case 14: /* Fn-F8: Enable/disable wifi */
			map_key_clear(KEY_WLAN);
			return 1;
		}
	}

	if (usage->hid == (HID_UP_KEYBOARD | 0x009a)) {
		map_key_clear(KEY_SYSRQ);
		return 1;
	}

	return 0;
}

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


[Index of Archives]     [Linux Media Devel]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Linux Wireless Networking]     [Linux Omap]

  Powered by Linux