[PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw events.

Vishnu Sankar posted 1 patch 2 months, 1 week ago
drivers/hid/hid-lenovo.c | 118 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 117 insertions(+), 1 deletion(-)
[PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw events.
Posted by Vishnu Sankar 2 months, 1 week ago
HID raw events being handled as Mic mute, Power Modes/Airplane mode,
Selective screenshot/Pickup Phone, KBD Backlight, Display mode and
star/Favourites has been added.
Thinkpad X12 TAB 2 and TAB 1 Folio keyboard's raw events will get
detected with this patch.

Default fn_lock state for these Keyboard are OFF.

Other than these changes, we follow TP10UKBD's processes.

Signed-off-by: Vishnu Sankar <vishnuocv@gmail.com>
Signed-off-by: Vishnu Sankar <vsankar@lenovo.com>
Reviewed-by: Mark Pearson <mpearson-lenovo@squebb.ca>
---
 drivers/hid/hid-lenovo.c | 118 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 117 insertions(+), 1 deletion(-)

diff --git a/drivers/hid/hid-lenovo.c b/drivers/hid/hid-lenovo.c
index e5e72aa52..db14b416f 100644
--- a/drivers/hid/hid-lenovo.c
+++ b/drivers/hid/hid-lenovo.c
@@ -31,12 +31,21 @@
 #include <linux/input.h>
 #include <linux/leds.h>
 #include <linux/workqueue.h>
+#include <linux/platform_profile.h>
 
 #include "hid-ids.h"
 
 /* Userspace expects F20 for mic-mute KEY_MICMUTE does not work */
 #define LENOVO_KEY_MICMUTE KEY_F20
 
+/* HID raw events for ThinkPas X12 Tabs*/
+#define TP_X12_RAW_HOTKEY_FN_F4		0x000200
+#define TP_X12_RAW_HOTKEY_FN_F8		0x100038
+#define TP_X12_RAW_HOTKEY_FN_F10	0x080000
+#define TP_X12_RAW_HOTKEY_FN_F12	0x040000
+#define TP_X12_RAW_HOTKEY_FN_SPACE	0x100018
+#define TP_X12_RAW_HOTKEY_FN_F7		0x080013
+
 struct lenovo_drvdata {
 	u8 led_report[3]; /* Must be first for proper alignment */
 	int led_state;
@@ -71,6 +80,14 @@ struct lenovo_drvdata {
 #define TP10UBKBD_LED_OFF		1
 #define TP10UBKBD_LED_ON		2
 
+/* Function to report raw_events as key events*/
+static inline void report_key_event(struct input_dev *input, int keycode)
+{
+	input_report_key(input, keycode, 1);
+	input_report_key(input, keycode, 0);
+	input_sync(input);
+}
+
 static int lenovo_led_set_tp10ubkbd(struct hid_device *hdev, u8 led_code,
 				    enum led_brightness value)
 {
@@ -472,6 +489,8 @@ static int lenovo_input_mapping(struct hid_device *hdev,
 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
 		return lenovo_input_mapping_tp10_ultrabook_kbd(hdev, hi, field,
 							       usage, bit, max);
+	case USB_DEVICE_ID_LENOVO_X12_TAB:
+	case USB_DEVICE_ID_LENOVO_X12_TAB2:
 	case USB_DEVICE_ID_LENOVO_X1_TAB:
 		return lenovo_input_mapping_x1_tab_kbd(hdev, hi, field, usage, bit, max);
 	default:
@@ -581,6 +600,8 @@ static ssize_t attr_fn_lock_store(struct device *dev,
 	case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
 		lenovo_features_set_cptkbd(hdev);
 		break;
+	case USB_DEVICE_ID_LENOVO_X12_TAB:
+	case USB_DEVICE_ID_LENOVO_X12_TAB2:
 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
 	case USB_DEVICE_ID_LENOVO_X1_TAB:
 		ret = lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, value);
@@ -678,9 +699,62 @@ static const struct attribute_group lenovo_attr_group_cptkbd = {
 	.attrs = lenovo_attributes_cptkbd,
 };
 
+/* Function to handle Lenovo Thinkpad TAB X12's HID raw inputs for fn keys*/
+static int lenovo_raw_event_TP_X12_tab(struct hid_device *hdev, u32 raw_data)
+{
+	struct hid_input *hidinput;
+	struct input_dev *input = NULL;
+
+	/* Iterate through the associated inputs to find the correct input device */
+	list_for_each_entry(hidinput, &hdev->inputs, list) {
+		input = hidinput->input;
+		if (input)
+			break;  /* Use the first valid input device */
+	}
+
+	switch (raw_data) {
+		/* fn-F20 being used here for MIC mute*/
+	case TP_X12_RAW_HOTKEY_FN_F4:
+		report_key_event(input, LENOVO_KEY_MICMUTE);
+		return 1;
+		/* Power-mode or Airplane mode will be called based on the device*/
+	case TP_X12_RAW_HOTKEY_FN_F8:
+		/* TP X12 TAB uses Fn-F8 calls Airplanemode
+		 * Whereas TP X12 TAB2 uses Fn-F8 for toggling
+		 * Power modes
+		 */
+		(hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB) ?
+			report_key_event(input, KEY_RFKILL) :
+			platform_profile_cycle();
+		return 1;
+	case TP_X12_RAW_HOTKEY_FN_F10:
+		/* TAB1 has PICKUP Phone and TAB2 use Snipping tool*/
+		(hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB) ?
+		report_key_event(input, KEY_PICKUP_PHONE) :
+		report_key_event(input, KEY_SELECTIVE_SCREENSHOT);
+		return 1;
+	case TP_X12_RAW_HOTKEY_FN_F12:
+		/* BookMarks/STAR key*/
+		report_key_event(input, KEY_BOOKMARKS);
+		return 1;
+	case TP_X12_RAW_HOTKEY_FN_SPACE:
+		/* Keyboard LED backlight toggle*/
+		report_key_event(input, KEY_KBDILLUMTOGGLE);
+		return 1;
+	case TP_X12_RAW_HOTKEY_FN_F7:
+		/* DISPLAY switching when connecting to external monitors*/
+		report_key_event(input, KEY_SWITCHVIDEOMODE);
+		return 1;
+	default:
+		break;
+	}
+	return 0;
+}
+
 static int lenovo_raw_event(struct hid_device *hdev,
 			struct hid_report *report, u8 *data, int size)
 {
+	u32 raw_data;
 	/*
 	 * Compact USB keyboard's Fn-F12 report holds down many other keys, and
 	 * its own key is outside the usage page range. Remove extra
@@ -695,6 +769,29 @@ static int lenovo_raw_event(struct hid_device *hdev,
 		data[2] = 0x01;
 	}
 
+	/* Lenovo TP X12 Tab KBD's Fn+XX is HID raw data defined. Report ID is 0x03
+	 * For eg: Raw data received for MIC mute is 0x03000200.
+	 */
+	if (unlikely((hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB
+			|| hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB2)
+			&& size >= 3)) {
+		/* data[0] is report ID and is same for all 4byte raw_events from this KBD
+		 * for eg: Fn+F8 0x03,0x10,0x00,0x38
+		 * report ID here for most of the keys are 0x03.
+		 */
+		if (report->id == 0x03)
+			raw_data = (data[1] << 16) | (data[2] << 8) | data[3];
+		/* For some Keys the raw data is 6 bytes long but the last 3 bytes
+		 * will be always Zeros. There is no report-id documented.
+		 * For eg: for Fn+F7: 0x08,0x00,0x13,0x00,0x00,0x00.
+		 * In other words the last 3 bytes are dummy for now.
+		 */
+		else
+			raw_data = (data[0] << 16) | (data[1] << 8) | data[2];
+
+		/* Calling function to generate Key events */
+		lenovo_raw_event_TP_X12_tab(hdev, raw_data);
+	}
 	return 0;
 }
 
@@ -774,6 +871,8 @@ static int lenovo_event(struct hid_device *hdev, struct hid_field *field,
 	case USB_DEVICE_ID_LENOVO_TPIIUSBKBD:
 	case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
 		return lenovo_event_cptkbd(hdev, field, usage, value);
+	case USB_DEVICE_ID_LENOVO_X12_TAB:
+	case USB_DEVICE_ID_LENOVO_X12_TAB2:
 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
 	case USB_DEVICE_ID_LENOVO_X1_TAB:
 		return lenovo_event_tp10ubkbd(hdev, field, usage, value);
@@ -1054,6 +1153,8 @@ static int lenovo_led_brightness_set(struct led_classdev *led_cdev,
 	case USB_DEVICE_ID_LENOVO_TPKBD:
 		lenovo_led_set_tpkbd(hdev);
 		break;
+	case USB_DEVICE_ID_LENOVO_X12_TAB:
+	case USB_DEVICE_ID_LENOVO_X12_TAB2:
 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
 	case USB_DEVICE_ID_LENOVO_X1_TAB:
 		ret = lenovo_led_set_tp10ubkbd(hdev, tp10ubkbd_led[led_nr], value);
@@ -1239,8 +1340,15 @@ static int lenovo_probe_tp10ubkbd(struct hid_device *hdev)
 	 * We cannot read the state, only set it, so we force it to on here
 	 * (which should be a no-op) to make sure that our state matches the
 	 * keyboard's FN-lock state. This is the same as what Windows does.
+	 *
+	 * For X12 TAB and TAB2, the default windows behavious Fn-lock Off.
+	 * Adding additional check to ensure the behaviour in case of
+	 * Thinkpad X12 Tabs.
 	 */
-	data->fn_lock = true;
+
+	data->fn_lock = !(hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB ||
+			hdev->product == USB_DEVICE_ID_LENOVO_X12_TAB2);
+
 	lenovo_led_set_tp10ubkbd(hdev, TP10UBKBD_FN_LOCK_LED, data->fn_lock);
 
 	ret = sysfs_create_group(&hdev->dev.kobj, &lenovo_attr_group_tp10ubkbd);
@@ -1284,6 +1392,8 @@ static int lenovo_probe(struct hid_device *hdev,
 	case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
 		ret = lenovo_probe_cptkbd(hdev);
 		break;
+	case USB_DEVICE_ID_LENOVO_X12_TAB:
+	case USB_DEVICE_ID_LENOVO_X12_TAB2:
 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
 	case USB_DEVICE_ID_LENOVO_X1_TAB:
 		ret = lenovo_probe_tp10ubkbd(hdev);
@@ -1370,6 +1480,8 @@ static void lenovo_remove(struct hid_device *hdev)
 	case USB_DEVICE_ID_LENOVO_TPIIBTKBD:
 		lenovo_remove_cptkbd(hdev);
 		break;
+	case USB_DEVICE_ID_LENOVO_X12_TAB:
+	case USB_DEVICE_ID_LENOVO_X12_TAB2:
 	case USB_DEVICE_ID_LENOVO_TP10UBKBD:
 	case USB_DEVICE_ID_LENOVO_X1_TAB:
 		lenovo_remove_tp10ubkbd(hdev);
@@ -1421,6 +1533,10 @@ static const struct hid_device_id lenovo_devices[] = {
 	 */
 	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
 		     USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X1_TAB) },
+	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
+		     USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X12_TAB) },
+	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
+		     USB_VENDOR_ID_LENOVO, USB_DEVICE_ID_LENOVO_X12_TAB2) },
 	{ }
 };
 
-- 
2.43.0
Re: [PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw events.
Posted by kernel test robot 2 months, 1 week ago
Hi Vishnu,

kernel test robot noticed the following build errors:

[auto build test ERROR on hid/for-next]
[also build test ERROR on linus/master next-20240920]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Vishnu-Sankar/hid-hid-lenovo-Supporting-TP-X12-TAB-1-2-Kbd-Hotkeys-using-raw-events/20240917-180639
base:   https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git for-next
patch link:    https://lore.kernel.org/r/20240917100432.10887-1-vishnuocv%40gmail.com
patch subject: [PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw  events.
config: i386-randconfig-062-20240921 (https://download.01.org/0day-ci/archive/20240921/202409211318.ZsE7JGOi-lkp@intel.com/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240921/202409211318.ZsE7JGOi-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409211318.ZsE7JGOi-lkp@intel.com/

All errors (new ones prefixed by >>, old ones prefixed by <<):

WARNING: modpost: missing MODULE_DESCRIPTION() in kernel/locking/test-ww_mutex.o
WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/devfreq/governor_powersave.o
>> ERROR: modpost: "platform_profile_cycle" [drivers/hid/hid-lenovo.ko] undefined!

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw events.
Posted by Vishnu Sankar 2 months ago
Sorry for the inconvenience.
The base I used was the Master branch.

Should I resubmit this patch again with the base as linus/master next-2024xxxx?

On Sat, Sep 21, 2024 at 3:19 PM kernel test robot <lkp@intel.com> wrote:
>
> Hi Vishnu,
>
> kernel test robot noticed the following build errors:
>
> [auto build test ERROR on hid/for-next]
> [also build test ERROR on linus/master next-20240920]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
>
> url:    https://github.com/intel-lab-lkp/linux/commits/Vishnu-Sankar/hid-hid-lenovo-Supporting-TP-X12-TAB-1-2-Kbd-Hotkeys-using-raw-events/20240917-180639
> base:   https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git for-next
> patch link:    https://lore.kernel.org/r/20240917100432.10887-1-vishnuocv%40gmail.com
> patch subject: [PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw  events.
> config: i386-randconfig-062-20240921 (https://download.01.org/0day-ci/archive/20240921/202409211318.ZsE7JGOi-lkp@intel.com/config)
> compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240921/202409211318.ZsE7JGOi-lkp@intel.com/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202409211318.ZsE7JGOi-lkp@intel.com/
>
> All errors (new ones prefixed by >>, old ones prefixed by <<):
>
> WARNING: modpost: missing MODULE_DESCRIPTION() in kernel/locking/test-ww_mutex.o
> WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/devfreq/governor_powersave.o
> >> ERROR: modpost: "platform_profile_cycle" [drivers/hid/hid-lenovo.ko] undefined!
>
> --
> 0-DAY CI Kernel Test Service
> https://github.com/intel/lkp-tests/wiki



-- 

Regards,

      Vishnu Sankar
     +817015150407 (Japan)
Re: [PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw events.
Posted by Jiri Kosina 2 months ago
On Tue, 24 Sep 2024, Vishnu Sankar wrote:

> Sorry for the inconvenience.
> The base I used was the Master branch.
> 
> Should I resubmit this patch again with the base as linus/master next-2024xxxx?

Please ideally base your patches on:

- topic branch in hid.git for the particular driver you are touching (in 
  this case it'd be called hid.git#for-6.13/lenovo)

- hid.git#master if a topic branch for your particular driver doesn't 
  exist in hid.git

Thanks,

-- 
Jiri Kosina
SUSE Labs
Re: [PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw events.
Posted by Vishnu Sankar 2 months ago
Thank you.

I will resubmit the patch rebasing on hid.git#for-6.13/lenovo.

On Sat, Sep 28, 2024 at 3:19 AM Jiri Kosina <jikos@kernel.org> wrote:
>
> On Tue, 24 Sep 2024, Vishnu Sankar wrote:
>
> > Sorry for the inconvenience.
> > The base I used was the Master branch.
> >
> > Should I resubmit this patch again with the base as linus/master next-2024xxxx?
>
> Please ideally base your patches on:
>
> - topic branch in hid.git for the particular driver you are touching (in
>   this case it'd be called hid.git#for-6.13/lenovo)
>
> - hid.git#master if a topic branch for your particular driver doesn't
>   exist in hid.git
>
> Thanks,
>
> --
> Jiri Kosina
> SUSE Labs
>


-- 

Regards,

      Vishnu Sankar
     +817015150407 (Japan)
Re: [PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw events.
Posted by kernel test robot 2 months, 1 week ago
Hi Vishnu,

kernel test robot noticed the following build errors:

[auto build test ERROR on hid/for-next]
[also build test ERROR on linus/master next-20240920]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Vishnu-Sankar/hid-hid-lenovo-Supporting-TP-X12-TAB-1-2-Kbd-Hotkeys-using-raw-events/20240917-180639
base:   https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git for-next
patch link:    https://lore.kernel.org/r/20240917100432.10887-1-vishnuocv%40gmail.com
patch subject: [PATCH] hid: hid-lenovo: Supporting TP-X12-TAB-1/2 Kbd Hotkeys using raw  events.
config: parisc-randconfig-r062-20240921 (https://download.01.org/0day-ci/archive/20240921/202409210619.eaTT5ACU-lkp@intel.com/config)
compiler: hppa-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240921/202409210619.eaTT5ACU-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202409210619.eaTT5ACU-lkp@intel.com/

All errors (new ones prefixed by >>):

   hppa-linux-ld: drivers/hid/hid-lenovo.o: in function `lenovo_raw_event':
>> hid-lenovo.c:(.text+0x1958): undefined reference to `platform_profile_cycle'

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki