diff mbox series

[v2] HID: Add Wireless Radio Control feature for Chicony devices

Message ID 20201223055540.204685-1-jhp@endlessos.org (mailing list archive)
State Mainlined
Commit 859b510bb7fa5797cfaf7bf5729336bf0e94dd2e
Delegated to: Jiri Kosina
Headers show
Series [v2] HID: Add Wireless Radio Control feature for Chicony devices | expand

Commit Message

Jian-Hong Pan Dec. 23, 2020, 5:55 a.m. UTC
Some Chicony's keyboards support airplane mode hotkey (Fn+F2) with
"Wireless Radio Control" feature. For example, the wireless keyboard
[04f2:1236] shipped with ASUS all-in-one desktop.

After consulting Chicony for this hotkey, learned the device will send
with 0x11 as the report ID and 0x1 as the value when the key is pressed
down.

This patch maps the event as KEY_RFKILL.

Signed-off-by: Jian-Hong Pan <jhp@endlessos.org>
---
v2: Remove the duplicated key pressed check.

 drivers/hid/hid-chicony.c | 55 +++++++++++++++++++++++++++++++++++++++
 drivers/hid/hid-ids.h     |  1 +
 2 files changed, 56 insertions(+)

Comments

Chris Chiu Jan. 4, 2021, 3:54 p.m. UTC | #1
On Wed, Dec 23, 2020 at 1:57 PM Jian-Hong Pan <jhp@endlessos.org> wrote:
>
> Some Chicony's keyboards support airplane mode hotkey (Fn+F2) with
> "Wireless Radio Control" feature. For example, the wireless keyboard
> [04f2:1236] shipped with ASUS all-in-one desktop.
>
> After consulting Chicony for this hotkey, learned the device will send
> with 0x11 as the report ID and 0x1 as the value when the key is pressed
> down.
>
> This patch maps the event as KEY_RFKILL.
>
> Signed-off-by: Jian-Hong Pan <jhp@endlessos.org>
Reviewed-by: Chris Chiu <chiu@endlessos.org>
> ---
> v2: Remove the duplicated key pressed check.
>
>  drivers/hid/hid-chicony.c | 55 +++++++++++++++++++++++++++++++++++++++
>  drivers/hid/hid-ids.h     |  1 +
>  2 files changed, 56 insertions(+)
>
> diff --git a/drivers/hid/hid-chicony.c b/drivers/hid/hid-chicony.c
> index 3f0ed6a95223..ca556d39da2a 100644
> --- a/drivers/hid/hid-chicony.c
> +++ b/drivers/hid/hid-chicony.c
> @@ -21,6 +21,39 @@
>
>  #include "hid-ids.h"
>
> +#define CH_WIRELESS_CTL_REPORT_ID      0x11
> +
> +static int ch_report_wireless(struct hid_report *report, u8 *data, int size)
> +{
> +       struct hid_device *hdev = report->device;
> +       struct input_dev *input;
> +
> +       if (report->id != CH_WIRELESS_CTL_REPORT_ID || report->maxfield != 1)
> +               return 0;
> +
> +       input = report->field[0]->hidinput->input;
> +       if (!input) {
> +               hid_warn(hdev, "can't find wireless radio control's input");
> +               return 0;
> +       }
> +
> +       input_report_key(input, KEY_RFKILL, 1);
> +       input_sync(input);
> +       input_report_key(input, KEY_RFKILL, 0);
> +       input_sync(input);
> +
> +       return 1;
> +}
> +
> +static int ch_raw_event(struct hid_device *hdev,
> +               struct hid_report *report, u8 *data, int size)
> +{
> +       if (report->application == HID_GD_WIRELESS_RADIO_CTLS)
> +               return ch_report_wireless(report, data, size);
> +
> +       return 0;
> +}
> +
>  #define ch_map_key_clear(c)    hid_map_usage_clear(hi, usage, bit, max, \
>                                         EV_KEY, (c))
>  static int ch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
> @@ -77,10 +110,30 @@ static __u8 *ch_switch12_report_fixup(struct hid_device *hdev, __u8 *rdesc,
>         return rdesc;
>  }
>
> +static int ch_probe(struct hid_device *hdev, const struct hid_device_id *id)
> +{
> +       int ret;
> +
> +       hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
> +       ret = hid_parse(hdev);
> +       if (ret) {
> +               hid_err(hdev, "Chicony hid parse failed: %d\n", ret);
> +               return ret;
> +       }
> +
> +       ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
> +       if (ret) {
> +               hid_err(hdev, "Chicony hw start failed: %d\n", ret);
> +               return ret;
> +       }
> +
> +       return 0;
> +}
>
>  static const struct hid_device_id ch_devices[] = {
>         { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
>         { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS2) },
> +       { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS3) },
>         { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_ACER_SWITCH12) },
>         { }
>  };
> @@ -91,6 +144,8 @@ static struct hid_driver ch_driver = {
>         .id_table = ch_devices,
>         .report_fixup = ch_switch12_report_fixup,
>         .input_mapping = ch_input_mapping,
> +       .probe = ch_probe,
> +       .raw_event = ch_raw_event,
>  };
>  module_hid_driver(ch_driver);
>
> diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
> index 4c5f23640f9c..06d90301a3dc 100644
> --- a/drivers/hid/hid-ids.h
> +++ b/drivers/hid/hid-ids.h
> @@ -270,6 +270,7 @@
>  #define USB_DEVICE_ID_CHICONY_PIXART_USB_OPTICAL_MOUSE 0x1053
>  #define USB_DEVICE_ID_CHICONY_PIXART_USB_OPTICAL_MOUSE2        0x0939
>  #define USB_DEVICE_ID_CHICONY_WIRELESS2        0x1123
> +#define USB_DEVICE_ID_CHICONY_WIRELESS3        0x1236
>  #define USB_DEVICE_ID_ASUS_AK1D                0x1125
>  #define USB_DEVICE_ID_CHICONY_TOSHIBA_WT10A    0x1408
>  #define USB_DEVICE_ID_CHICONY_ACER_SWITCH12    0x1421
> --
> 2.29.2
>
Jiri Kosina Jan. 7, 2021, 9:23 a.m. UTC | #2
On Wed, 23 Dec 2020, Jian-Hong Pan wrote:

> Some Chicony's keyboards support airplane mode hotkey (Fn+F2) with
> "Wireless Radio Control" feature. For example, the wireless keyboard
> [04f2:1236] shipped with ASUS all-in-one desktop.
> 
> After consulting Chicony for this hotkey, learned the device will send
> with 0x11 as the report ID and 0x1 as the value when the key is pressed
> down.
> 
> This patch maps the event as KEY_RFKILL.

I don't know how exactly does the report descriptor of that device look 
like, but is this not doable from userspace via setkeycode() (udev/systemd 
is shipping a lot of such mappings already -- see evdev/keyboard 
definitions in hwdb).

Thanks,
Jian-Hong Pan Jan. 11, 2021, 5:40 a.m. UTC | #3
Jiri Kosina <jikos@kernel.org> 於 2021年1月7日 週四 下午5:23寫道:
>
> On Wed, 23 Dec 2020, Jian-Hong Pan wrote:
>
> > Some Chicony's keyboards support airplane mode hotkey (Fn+F2) with
> > "Wireless Radio Control" feature. For example, the wireless keyboard
> > [04f2:1236] shipped with ASUS all-in-one desktop.
> >
> > After consulting Chicony for this hotkey, learned the device will send
> > with 0x11 as the report ID and 0x1 as the value when the key is pressed
> > down.
> >
> > This patch maps the event as KEY_RFKILL.
>
> I don't know how exactly does the report descriptor of that device look
> like, but is this not doable from userspace via setkeycode() (udev/systemd
> is shipping a lot of such mappings already -- see evdev/keyboard
> definitions in hwdb).

Thanks for your suggestion!

I have tested the key with evtest.  But it has no response from all
inputs.  Nor response from xev.

So, I tried usb monitor to see what does it send:

$ lsusb -d 04f2:1236
Bus 001 Device 002: ID 04f2:1236 Chicony Electronics Co., Ltd
$ sudo modprobe usbmon
$ sudo cat /sys/kernel/debug/usb/usbmon/1u
ffff9145e0dea6c0 348311963 C Ii:1:002:1 0:8 8 = 00000000 00000000
ffff9145e0dea6c0 348311996 S Ii:1:002:1 -115:8 8 <
ffff9145e0deaf00 352852533 C Ii:1:002:2 0:4 2 = 1101
ffff9145e0deaf00 352852547 S Ii:1:002:2 -115:4 3 <

It sends 0x1101 for the hotkey.  The same response from hid events:

$ sudo cat /sys/kernel/debug/hid/0003\:04F2\:1236.0002/events
report (size 2) (numbered) =  11 01

Then, I notice there is the RFKILL event listed on the "Chicony USB
Receiver Wireless Radio Control" device:

$ sudo evtest /dev/input/event8
Input driver version is 1.0.1
Input device ID: bus 0x3 vendor 0x4f2 product 0x1236 version 0x111
Input device name: "Chicony USB Receiver Wireless Radio Control"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 103 (KEY_UP)
    Event code 105 (KEY_LEFT)
    Event code 106 (KEY_RIGHT)
    Event code 108 (KEY_DOWN)
    Event code 116 (KEY_POWER)
    Event code 138 (KEY_HELP)
    Event code 139 (KEY_MENU)
    Event code 142 (KEY_SLEEP)
    Event code 143 (KEY_WAKEUP)
    Event code 148 (KEY_PROG1)
    Event code 174 (KEY_EXIT)
    Event code 227 (KEY_SWITCHVIDEOMODE)
    Event code 247 (KEY_RFKILL)
    Event code 314 (BTN_SELECT)
    Event code 315 (BTN_START)
    Event code 353 (KEY_SELECT)
    Event code 356 (KEY_POWER2)
    Event code 408 (KEY_RESTART)
    Event code 438 (KEY_CONTEXT_MENU)
  Event type 2 (EV_REL)
    Event code 9 (REL_MISC)
  Event type 3 (EV_ABS)
...

Also, after debugging, I found its HID application ID is
HID_GD_WIRELESS_RADIO_CTLS 0x0001000c [1].
Then, I searched HID_GD_WIRELESS_RADIO_CTLS in the kernel.  I found
HID_GD_RFKILL_BTN [2] is mapped in hid-input.
However, this key press on the Chicony keyboard maps to nothing, nor
HID_GD_RFKILL_BTN.  Only have the HID report with raw data 0x11 0x00
as mentioned above.
It is more like ignored by the kernel and it even has no scancode.
That's why I try to map it as KEY_RFKILL in the driver.

[1] https://elixir.bootlin.com/linux/v5.10/source/include/linux/hid.h#L181
[2] https://elixir.bootlin.com/linux/v5.10/source/drivers/hid/hid-input.c#L743

Regards,
Jian-Hong Pan
Jiri Kosina Jan. 14, 2021, 12:13 p.m. UTC | #4
On Wed, 23 Dec 2020, Jian-Hong Pan wrote:

> Some Chicony's keyboards support airplane mode hotkey (Fn+F2) with
> "Wireless Radio Control" feature. For example, the wireless keyboard
> [04f2:1236] shipped with ASUS all-in-one desktop.
> 
> After consulting Chicony for this hotkey, learned the device will send
> with 0x11 as the report ID and 0x1 as the value when the key is pressed
> down.
> 
> This patch maps the event as KEY_RFKILL.
> 
> Signed-off-by: Jian-Hong Pan <jhp@endlessos.org>
> ---
> v2: Remove the duplicated key pressed check.

Applied, thanks.
diff mbox series

Patch

diff --git a/drivers/hid/hid-chicony.c b/drivers/hid/hid-chicony.c
index 3f0ed6a95223..ca556d39da2a 100644
--- a/drivers/hid/hid-chicony.c
+++ b/drivers/hid/hid-chicony.c
@@ -21,6 +21,39 @@ 
 
 #include "hid-ids.h"
 
+#define CH_WIRELESS_CTL_REPORT_ID	0x11
+
+static int ch_report_wireless(struct hid_report *report, u8 *data, int size)
+{
+	struct hid_device *hdev = report->device;
+	struct input_dev *input;
+
+	if (report->id != CH_WIRELESS_CTL_REPORT_ID || report->maxfield != 1)
+		return 0;
+
+	input = report->field[0]->hidinput->input;
+	if (!input) {
+		hid_warn(hdev, "can't find wireless radio control's input");
+		return 0;
+	}
+
+	input_report_key(input, KEY_RFKILL, 1);
+	input_sync(input);
+	input_report_key(input, KEY_RFKILL, 0);
+	input_sync(input);
+
+	return 1;
+}
+
+static int ch_raw_event(struct hid_device *hdev,
+		struct hid_report *report, u8 *data, int size)
+{
+	if (report->application == HID_GD_WIRELESS_RADIO_CTLS)
+		return ch_report_wireless(report, data, size);
+
+	return 0;
+}
+
 #define ch_map_key_clear(c)	hid_map_usage_clear(hi, usage, bit, max, \
 					EV_KEY, (c))
 static int ch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
@@ -77,10 +110,30 @@  static __u8 *ch_switch12_report_fixup(struct hid_device *hdev, __u8 *rdesc,
 	return rdesc;
 }
 
+static int ch_probe(struct hid_device *hdev, const struct hid_device_id *id)
+{
+	int ret;
+
+	hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
+	ret = hid_parse(hdev);
+	if (ret) {
+		hid_err(hdev, "Chicony hid parse failed: %d\n", ret);
+		return ret;
+	}
+
+	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+	if (ret) {
+		hid_err(hdev, "Chicony hw start failed: %d\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
 
 static const struct hid_device_id ch_devices[] = {
 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS2) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS3) },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_ACER_SWITCH12) },
 	{ }
 };
@@ -91,6 +144,8 @@  static struct hid_driver ch_driver = {
 	.id_table = ch_devices,
 	.report_fixup = ch_switch12_report_fixup,
 	.input_mapping = ch_input_mapping,
+	.probe = ch_probe,
+	.raw_event = ch_raw_event,
 };
 module_hid_driver(ch_driver);
 
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 4c5f23640f9c..06d90301a3dc 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -270,6 +270,7 @@ 
 #define USB_DEVICE_ID_CHICONY_PIXART_USB_OPTICAL_MOUSE	0x1053
 #define USB_DEVICE_ID_CHICONY_PIXART_USB_OPTICAL_MOUSE2	0x0939
 #define USB_DEVICE_ID_CHICONY_WIRELESS2	0x1123
+#define USB_DEVICE_ID_CHICONY_WIRELESS3	0x1236
 #define USB_DEVICE_ID_ASUS_AK1D		0x1125
 #define USB_DEVICE_ID_CHICONY_TOSHIBA_WT10A	0x1408
 #define USB_DEVICE_ID_CHICONY_ACER_SWITCH12	0x1421