Skip to contentSkip to conversation

platform/x86: thinkpad_acpi: add adaptive_kbd_modes parameter

PATCH In review Eric Wong 2 participants 5 messages Nov 15, 2018 – Nov 25, 2018

Subsystems (2)
lore
Share & tools
lore

Eric Wong

author
#0 lore raw

PATCH 1/1 platform/x86: thinkpad_acpi: add adaptive_kbd_modes parameter

This bitmap parameter allows the user to add/remove modes for DFR_CHANGE_ROW to cycle through.

Users who wish to cycle through WEB_BROWSER_MODE and/or WEB_CONFERENCE_MODE may now do so by enabling corresponding bits.

While some appreciate more modes, I made this feature because I wanted to lock the keyboard into FUNCTION_MODE. This is because my Fn key (DFR_CHANGE_ROW) is mapped to Escape, and my "Esc" key is mapped to grave/asciitilde to match the layout of a regular keyboard.

The default remains unchanged with the DFR_CHANGE_ROW key toggling between HOME_MODE and FUNCTION_MODE. Thus the default "adaptive_kbd_modes" value is 9, but I use 8:

echo 8 >/sys/devices/platform/thinkpad_acpi/adaptive_kbd_modes

The above setting with this change and the following keymap preserves my sanity on the atrocious adaptive keyboard on the 2nd-gen X1 Carbon:

{
echo keymaps 0-255
# Esc key maps to '`' or '~' echo keycode 1 = grave asciitilde

# Fn key maps to Escape
echo keycode 143 = Escape

# Home and End on the keyboard map to Control echo keycode 102 = Control
echo keycode 107 = Control
} | loadkeys -

Or with the following xmodmaprc:

remove Control = Control_L
remove Lock = Control_L
keycode 9 = grave asciitilde grave
keycode 110 = Control_L NoSymbol Control_L keycode 115 = Control_L NoSymbol Control_L keycode 151 = Escape NoSymbol Escape add Control = Control_L

---
 Documentation/laptops/thinkpad-acpi.txt | 11 +++++
 drivers/platform/x86/thinkpad_acpi.c    | 79 ++++++++++++++++++++++-----------
 2 files changed, 64 insertions(+), 26 deletions(-)
Documentation/laptops/thinkpad-acpi.txt +11 -0
index 6cced88de6da..36c8731b6919 100644
@@ -1378,6 +1378,17 @@ For more details about which buttons will appear depending on the mode, please
 review the laptop's user guide:
 http://www.lenovo.com/shop/americas/content/user_guides/x1carbon_2_ug_en.pdf
 
+sysfs device attribute: adaptive_kbd_modes
+
+This bitmap attribute controls the modes the "Fn" key is allowed
+to cycle through.  The value can be read and set.  Enabled bits
+correspond to the modes above (that is, the first bit is "Home mode"
+and the fourth bit "Function mode").
+
+The default value is 9, which allows cycling between Home and Function modes.
+Setting and unsetting corresponding bits allows adding or removing modes
+to cycle through.
+
 Multiple Commands, Module Parameters
 ------------------------------------
 
drivers/platform/x86/thinkpad_acpi.c +53 -26

107 lines not on this page · raw message

Signature · 1 line
EW
author
#1 lore raw
Quoted 3 lines · Eric Wong

The above setting with this change and the following keymap preserves my sanity on the atrocious adaptive keyboard on the 2nd-gen X1 Carbon:

Any comments on this patch? The Esc and F-keys on the keyboard are still numb and I'll be getting rid of the laptop in a few days; but maybe my patch can still be useful to others...

#2 lore raw
Quoted 8 lines · Eric Wong
Quoted 3 lines · Eric Wong

The above setting with this change and the following keymap preserves my sanity on the atrocious adaptive keyboard on the 2nd-gen X1 Carbon:

Any comments on this patch? The Esc and F-keys on the keyboard are still numb and I'll be getting rid of the laptop in a few days; but maybe my patch can still be useful to others...

I've read through and I like it, FWIW. A brilliant idea. I don't have the hardare to test the patch, though....

But I do wonder if you aren't missing an empty mask protection somewhere? If I read this right, then there is nothing preventing you from writing 0 here:

drivers/platform/x86/thinkpad_acpi.c static ssize_t adaptive_kbd_mode_store(struct device *dev, · line 3128

Quoted 12 lines
drivers/platform/x86/thinkpad_acpi.c +12 -0
+static ssize_t adaptive_kbd_modes_store(struct device *dev,
+			struct device_attribute *attr,
+			const char *buf, size_t count)
+{
+	unsigned long t;
+
+	if (parse_strtoul(buf, (1 << LAYFLAT_MODE) - 1, &t))
+		return -EINVAL;
+
+	adaptive_kbd_modes = (unsigned int)t;
+	return count;
+}

And then I believe you have a busy loop here:

Quoted 21 lines
drivers/platform/x86/thinkpad_acpi.c +5 -11
@@ -3815,20 +3838,20 @@ static int adaptive_keyboard_set_mode(int new_mode)
 
 static int adaptive_keyboard_get_next_mode(int mode)
 {
-	size_t i;
-	size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
-
-	for (i = 0; i <= max_mode; i++) {
-		if (adaptive_keyboard_modes[i] == mode)
-			break;
-	}
+	int max_mode = fls(adaptive_kbd_modes);
+	int new_mode = mode >= max_mode ? HOME_MODE : mode + 1;
 
-	if (i >= max_mode)
-		i = 0;
-	else
-		i++;
+	/* make sure the new mode is allowed by the user */
+	while (!(adaptive_kbd_modes & (1 << new_mode))) {
+		new_mode++;

drivers/platform/x86/thinkpad_acpi.c static int adaptive_keyboard_set_mode(int new_mode) · line 3855

Quoted 10 lines
drivers/platform/x86/thinkpad_acpi.c +7 -1
+		if (new_mode > max_mode)
+			new_mode = HOME_MODE;
 
-	return adaptive_keyboard_modes[i];
+		/* maybe the user disabled all other modes: */
+		if (new_mode == mode)
+			return mode;
+	}
+	return new_mode;
 }

Or am I reading this wrong?

Bjørn

author
#3 lore raw
Quoted 6 lines · Bjørn Mork
Quoted 5 lines · Eric Wong
Quoted 3 lines · Eric Wong

The above setting with this change and the following keymap preserves my sanity on the atrocious adaptive keyboard on the 2nd-gen X1 Carbon:

Quoted 6 lines
Quoted 3 lines

Any comments on this patch? The Esc and F-keys on the keyboard are still numb and I'll be getting rid of the laptop in a few days; but maybe my patch can still be useful to others...

I've read through and I like it, FWIW. A brilliant idea. I don't have the hardare to test the patch, though....

Thanks for checking it out.

Quoted 4 lines

But I do wonder if you aren't missing an empty mask protection somewhere? If I read this right, then there is nothing preventing you from writing 0 here:

Quoted 12 lines

drivers/platform/x86/thinkpad_acpi.c static ssize_t adaptive_kbd_mode_store(struct device *dev, · line 3128

Quoted 12 lines
drivers/platform/x86/thinkpad_acpi.c +12 -0
+static ssize_t adaptive_kbd_modes_store(struct device *dev,
+			struct device_attribute *attr,
+			const char *buf, size_t count)
+{
+	unsigned long t;
+
+	if (parse_strtoul(buf, (1 << LAYFLAT_MODE) - 1, &t))
+		return -EINVAL;
+
+	adaptive_kbd_modes = (unsigned int)t;
+	return count;
+}

Right, 0 is allowed; and it will lock the current mode into place...

Quoted 26 lines

And then I believe you have a busy loop here:

Quoted 24 lines
drivers/platform/x86/thinkpad_acpi.c +7 -11
@@ -3815,20 +3838,20 @@ static int adaptive_keyboard_set_mode(int new_mode)
 
 static int adaptive_keyboard_get_next_mode(int mode)
 {
-	size_t i;
-	size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
-
-	for (i = 0; i <= max_mode; i++) {
-		if (adaptive_keyboard_modes[i] == mode)
-			break;
-	}
+	int max_mode = fls(adaptive_kbd_modes);
+	int new_mode = mode >= max_mode ? HOME_MODE : mode + 1;
 
-	if (i >= max_mode)
-		i = 0;
-	else
-		i++;
+	/* make sure the new mode is allowed by the user */
+	while (!(adaptive_kbd_modes & (1 << new_mode))) {
+		new_mode++;
+		if (new_mode > max_mode)
+			new_mode = HOME_MODE;
 
Quoted 7 lines

drivers/platform/x86/thinkpad_acpi.c static int adaptive_keyboard_set_mode(int new_mode) · line 3855

Quoted 7 lines
drivers/platform/x86/thinkpad_acpi.c +5 -1
-	return adaptive_keyboard_modes[i];
+		/* maybe the user disabled all other modes: */
+		if (new_mode == mode)
+			return mode;
+	}
+	return new_mode;
 }

Not a busy loop, since new_mode will reset at HOME_MODE (0) and then it'll hit "new_mode == mode" and remain locked in to the current mode.

Quoted 1 line

Or am I reading this wrong?

It seems that way. My initial iteration of this patch did have a busy loop, but I fixed it before publishing :)

Thanks again for the review.

#4 lore raw
Quoted 28 lines · Eric Wong
Quoted 26 lines · Bjørn Mork

And then I believe you have a busy loop here:

Quoted 24 lines
drivers/platform/x86/thinkpad_acpi.c +7 -11
@@ -3815,20 +3838,20 @@ static int adaptive_keyboard_set_mode(int new_mode)
 
 static int adaptive_keyboard_get_next_mode(int mode)
 {
-	size_t i;
-	size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
-
-	for (i = 0; i <= max_mode; i++) {
-		if (adaptive_keyboard_modes[i] == mode)
-			break;
-	}
+	int max_mode = fls(adaptive_kbd_modes);
+	int new_mode = mode >= max_mode ? HOME_MODE : mode + 1;
 
-	if (i >= max_mode)
-		i = 0;
-	else
-		i++;
+	/* make sure the new mode is allowed by the user */
+	while (!(adaptive_kbd_modes & (1 << new_mode))) {
+		new_mode++;
+		if (new_mode > max_mode)
+			new_mode = HOME_MODE;
 
Quoted 11 lines
Quoted 7 lines

drivers/platform/x86/thinkpad_acpi.c static int adaptive_keyboard_set_mode(int new_mode) · line 3855

Quoted 7 lines
drivers/platform/x86/thinkpad_acpi.c +5 -1
-	return adaptive_keyboard_modes[i];
+		/* maybe the user disabled all other modes: */
+		if (new_mode == mode)
+			return mode;
+	}
+	return new_mode;
 }

Not a busy loop, since new_mode will reset at HOME_MODE (0) and then it'll hit "new_mode == mode" and remain locked in to the current mode.

Right. I see it now. Thanks for explaining.

I guess I was expecting a complete loop bypass ala

if (!max_mode)
return mode:

but your solution will of course work just as fine.

Bjørn

_______________________________________________
ibm-acpi-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ibm-acpi-devel