Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7700917imu; Wed, 14 Nov 2018 23:36:54 -0800 (PST) X-Google-Smtp-Source: AJdET5fmtr/umTUjTFU5ZpKqWo7PV3EBegU6Mq2082oGTeGIueMEJmoI1JUHUoBxRPTYd3eR2Sm9 X-Received: by 2002:a17:902:8689:: with SMTP id g9-v6mr5124324plo.44.1542267414639; Wed, 14 Nov 2018 23:36:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542267414; cv=none; d=google.com; s=arc-20160816; b=NN5ShjBwz86bDXL8P1xmrWxS+GacYEWE1gh6kaE5DwurrfOOhXnUzGVzJOQyCF4yZ3 JGsl6rVoyn8ebmMGfI5oJN/+mA7uT87MCOjyoiK514unuUTrzKl+Jt56J4UkZJPqbEWF /ZrhqwXE8JfIa2ThttgVWID++1e/0/Ej1ZLi0wWijd0dl9xY2zb1ONy4rJrFfxo2I4Pm +I1yGAdyjC39Zd4YnOq895+vcdivo7309sPPJ3dbG9qZVQNRiLfGfGrM8zSFIvRDFzzY 5jOJeO8kBpdZmtT2Pt6K7u0w3PkDaHKuVcHH3a8v1FDLpiJbfcft2Mc9cGv5W0l1KPr8 xDYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition:mime-version :message-id:subject:cc:to:from:date; bh=NWZWWnKaI1zFBJrPOoIWOvIY0US6wL88sB/pb1UZI2w=; b=Zg12Tj6q1OGiY8or6/nraJN2oY0Crsn7DjXVKZYwzPj2kLvBsMOm1KyfAeT3+yz7x0 TzXxwAw9MEa2OhIMXKeLFK0/ILvd7d0edX/Wru5I/oLEMEW5ouJs42DPuDsH0se3KNV7 t5p5ZyvnL/sNIkjHjMoeB7YjcQk6aaKarJ0+PrJOBJL96GY1odOKbvwru5X0M8Nj7Q5z 17GNQAvzDFEDaIHGXZYiRpYQcDy2Foc7TGJRdBxxSymGQV0DvCnkzVGhJS+Fj46iZY/p pZ361/JeDiJ4xADXd/P7GfZgaFC05xdrDhXuVbkkmWSrrUlEeVDaWhyylJspleI2Z6r8 J9zg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b3si25699921pgh.496.2018.11.14.23.36.39; Wed, 14 Nov 2018 23:36:54 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728618AbeKORlL (ORCPT + 99 others); Thu, 15 Nov 2018 12:41:11 -0500 Received: from dcvr.yhbt.net ([64.71.152.64]:48492 "EHLO dcvr.yhbt.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726892AbeKORlL (ORCPT ); Thu, 15 Nov 2018 12:41:11 -0500 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 6A0541F87F; Thu, 15 Nov 2018 07:34:30 +0000 (UTC) Date: Thu, 15 Nov 2018 07:34:29 +0000 From: Eric Wong To: Henrique de Moraes Holschuh , Shuduo Sang Cc: Darren Hart , Andy Shevchenko , linux-kernel@vger.kernel.org, ibm-acpi-devel@lists.sourceforge.net, platform-driver-x86@vger.kernel.org Subject: [PATCH] platform/x86: thinkpad_acpi: add adaptive_kbd_modes parameter Message-ID: <20181115073429.z44czywrf7f65ndb@dcvr> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: Eric Wong --- Documentation/laptops/thinkpad-acpi.txt | 11 +++++ drivers/platform/x86/thinkpad_acpi.c | 79 ++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/Documentation/laptops/thinkpad-acpi.txt b/Documentation/laptops/thinkpad-acpi.txt index 6cced88de6da..36c8731b6919 100644 --- a/Documentation/laptops/thinkpad-acpi.txt +++ b/Documentation/laptops/thinkpad-acpi.txt @@ -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 ------------------------------------ diff --git a/drivers/platform/x86/thinkpad_acpi.c b/drivers/platform/x86/thinkpad_acpi.c index fde08a997557..77b4f00e0443 100644 --- a/drivers/platform/x86/thinkpad_acpi.c +++ b/drivers/platform/x86/thinkpad_acpi.c @@ -3094,8 +3094,44 @@ static ssize_t adaptive_kbd_mode_store(struct device *dev, static DEVICE_ATTR_RW(adaptive_kbd_mode); +/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser + * mode, Web conference mode, Function mode and Lay-flat mode. + * We support cycling between Home mode and Function mode by default. + * + * Users may enable and disable other modes by changing the + * adaptive_kbd_modes bitmap attribute + */ +static unsigned adaptive_kbd_modes = + 1 << HOME_MODE | +/* 1 << WEB_BROWSER_MODE | + 1 << WEB_CONFERENCE_MODE | */ + 1 << FUNCTION_MODE; + +static ssize_t adaptive_kbd_modes_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return snprintf(buf, PAGE_SIZE, "%u\n", adaptive_kbd_modes); +} + +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; +} + +static DEVICE_ATTR_RW(adaptive_kbd_modes); + static struct attribute *adaptive_kbd_attributes[] = { &dev_attr_adaptive_kbd_mode.attr, + &dev_attr_adaptive_kbd_modes.attr, NULL }; @@ -3763,20 +3799,7 @@ static int __init hotkey_init(struct ibm_init_struct *iibm) return (res < 0) ? res : 1; } -/* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser - * mode, Web conference mode, Function mode and Lay-flat mode. - * We support Home mode and Function mode currently. - * - * Will consider support rest of modes in future. - * - */ -static const int adaptive_keyboard_modes[] = { - HOME_MODE, -/* WEB_BROWSER_MODE = 2, - WEB_CONFERENCE_MODE = 3, */ - FUNCTION_MODE -}; - +/* Thinkpad X1 Carbon adaptive keyboard */ #define DFR_CHANGE_ROW 0x101 #define DFR_SHOW_QUICKVIEW_ROW 0x102 #define FIRST_ADAPTIVE_KEY 0x103 @@ -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; - return adaptive_keyboard_modes[i]; + /* maybe the user disabled all other modes: */ + if (new_mode == mode) + return mode; + } + return new_mode; } static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode) @@ -3848,6 +3871,10 @@ static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode) return false; new_mode = adaptive_keyboard_get_next_mode( current_mode); + + /* some users may not want cycling */ + if (new_mode == current_mode) + return true; } if (adaptive_keyboard_set_mode(new_mode) < 0) -- EW