2022-06-01 18:16:58

by Hilton Chain

[permalink] [raw]
Subject: Re: [PATCH v2] HID: apple: Workaround for non-Apple keyboards

On Tue, May 31, 2022 at 11:20 AM José Expósito wrote:

> +struct apple_non_apple_keyboard {
> + char *name;
> +};
> +
> struct apple_sc_backlight {
> struct led_classdev cdev;
> struct hid_device *hdev;
> @@ -313,6 +317,29 @@ static const struct apple_key_translation swapped_fn_leftctrl_keys[] = {
> { }
> };
>
> +static const struct apple_non_apple_keyboard non_apple_keyboards[] = {
> + { "SONiX USB DEVICE" },
> + { "Keychron" },
> + { }
>
> Could the "non_apple && strlen(non_apple)" check be avoided by removing
> this empty item?

Hi Jose,
If there's a chance that the device name is an empty string? In such case the
strlen should be preserved.

> +};
> +
> +static bool apple_is_non_apple_keyboard(struct hid_device *hdev)
> +{
> + unsigned long i;
> + unsigned long non_apple_total = sizeof(non_apple_keyboards) /
> + sizeof(struct apple_non_apple_keyboard);
>
> Here you coud take advantage of the "ARRAY_SIZE" macro:
>
> https://kernelnewbies.org/MagicMacros
>
> It'll also allow you to use an int. Something similar to:
>
> int i;
>
> for (i = 0; i < ARRAY_SIZE(non_apple_keyboards); i++) {
> [...]

Thanks for the information!

> @@ -667,11 +694,12 @@ static int apple_input_configured(struct hid_device *hdev,
> if ((asc->quirks & APPLE_HAS_FN) && !asc->fn_found) {
> hid_info(hdev, "Fn key not found (Apple Wireless Keyboard clone?), disabling Fn key handling\n");
> asc->quirks &= ~APPLE_HAS_FN;
> - }
>
> - if (strncmp(hdev->name, "Keychron", 8) == 0) {
> - hid_info(hdev, "Keychron keyboard detected; function keys will default to fnmode=2 behavior\n");
> - asc->quirks |= APPLE_IS_KEYCHRON;
> + if (apple_is_non_apple_keyboard(hdev)) {
> + hid_info(hdev,
> + "Non-apple keyboard detected; function keys will default to fnmode=2 behavior\n");
>
> Checkpatch nitpick:
>
> CHECK: Alignment should match open parenthesis
> FILE: drivers/hid/hid-apple.c:700:
> hid_info(hdev,
> "Non-apple keyboard detected; function keys will default to fnmode=2 behavior\n");
>
> It suggest to add an extra space before "Non-apple ...".
>
> In case you don't know the tool, it helps to find style errors, I
> usually run it like:
>
> $ ./scripts/checkpatch.pl --strict --codespell --git HEAD-1
>
> + asc->quirks |= APPLE_IS_NON_APPLE;
> + }
>
> This slightly changes the behaviour from the previous patch.
> Previously, the APPLE_IS_NON_APPLE quirk was set even if APPLE_HAS_FN
> was not present. Now the condition is nested.
>
> I'm not saying it is wrong (I don't have the required hardware to test
> it), I'm just pointing it out in case it was an accidental change.
> Bryan, should be able to confirm if it works with his keyboard.

Thanks again!

On Tue, 31 May 2022 13:50:30 -0600 Bryan Cain wrote:

> I haven't tested it, but I can tell from reading the patch that it will break
> compatibility with Keychron keyboards like mine, precisely because of the
> nesting.
>
> The biggest reason that my Keychron patch was needed at all was that Keychron
> devices advertise the Fn key, and thus don't hit the first clone check since
> asc->fn_found is actually true for them. So nesting the check for the Keychron
> manufacturer/product name inside of that check won't work.
>
> To tell the truth, I'm still a bit confused about the precise behavior of the
> Sonix firmware that this patch is made to work around. If it's not advertising
> an Apple-style Fn key, why isn't the existing behavior of disabling Fn-key
> handling enough to make it work? The fnmode parameter is ignored entirely
> when APPLE_HAS_FN isn't set, so it's hard to imagine that the change to fnmode
> behavior would even do anything in that case.

Hi Bryan,
Sorry for my inconsiderateness...

It advertises such function:
- FN Function Keys Make Control easily.
FN+F1:My Computer
FN+F2:Browser
FN+F3:Email
...

I made a vital mistake by not properly checking the patch before sending,
that's totally my fault.

Sorry again for the mess I made.

Best wishes,
Chain


2022-06-01 21:14:10

by Hilton Chain

[permalink] [raw]
Subject: [PATCH v3] HID: apple: Properly handle function keys on non-Apple keyboard

This commit extends fa33382c7f74 ("HID: apple: Properly handle function
keys on Keychron keyboards") by adding an array of known non-Apple
keyboards' device names, and the function apple_is_non_apple_keyboard()
to identify and create exception for them.

Signed-off-by: Hilton Chain <[email protected]>
---
drivers/hid/hid-apple.c | 33 ++++++++++++++++++++++++++++-----
1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index 42a568902f49..7d56530d9e3a 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -36,7 +36,7 @@
#define APPLE_NUMLOCK_EMULATION BIT(8)
#define APPLE_RDESC_BATTERY BIT(9)
#define APPLE_BACKLIGHT_CTL BIT(10)
-#define APPLE_IS_KEYCHRON BIT(11)
+#define APPLE_IS_NON_APPLE BIT(11)

#define APPLE_FLAG_FKEY 0x01

@@ -65,6 +65,10 @@ MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. "
"(For people who want to keep PC keyboard muscle memory. "
"[0] = as-is, Mac layout, 1 = swapped, PC layout)");

+struct apple_non_apple_keyboard {
+ char *name;
+};
+
struct apple_sc_backlight {
struct led_classdev cdev;
struct hid_device *hdev;
@@ -313,6 +317,25 @@ static const struct apple_key_translation swapped_fn_leftctrl_keys[] = {
{ }
};

+static const struct apple_non_apple_keyboard non_apple_keyboards[] = {
+ { "SONiX USB DEVICE" },
+ { "Keychron" },
+};
+
+static bool apple_is_non_apple_keyboard(struct hid_device *hdev)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(non_apple_keyboards); i++) {
+ char *non_apple = non_apple_keyboards[i].name;
+
+ if (strlen(non_apple) && strncmp(hdev->name, non_apple, strlen(non_apple)) == 0)
+ return true;
+ }
+
+ return false;
+}
+
static inline void apple_setup_key_translation(struct input_dev *input,
const struct apple_key_translation *table)
{
@@ -363,7 +386,7 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
}

if (fnmode == 3) {
- real_fnmode = (asc->quirks & APPLE_IS_KEYCHRON) ? 2 : 1;
+ real_fnmode = (asc->quirks & APPLE_IS_NON_APPLE) ? 2 : 1;
} else {
real_fnmode = fnmode;
}
@@ -669,9 +692,9 @@ static int apple_input_configured(struct hid_device *hdev,
asc->quirks &= ~APPLE_HAS_FN;
}

- if (strncmp(hdev->name, "Keychron", 8) == 0) {
- hid_info(hdev, "Keychron keyboard detected; function keys will default to fnmode=2 behavior\n");
- asc->quirks |= APPLE_IS_KEYCHRON;
+ if (apple_is_non_apple_keyboard(hdev)) {
+ hid_info(hdev, "Non-apple keyboard detected; function keys will default to fnmode=2 behavior\n");
+ asc->quirks |= APPLE_IS_NON_APPLE;
}

return 0;

base-commit: e1cbc3b96a9974746b2a80c3a6c8a0f7eff7b1b5
--
2.36.1


2022-06-02 00:35:58

by Bryan Cain

[permalink] [raw]
Subject: Re: [PATCH v3] HID: apple: Properly handle function keys on non-Apple keyboard

I can't reply to v4 since I wasn't cc'd on that one and I'm not subscribed to
the mailing list, but while I haven't tested it yet, it looks good.

The only feedback I have is that according to my searching[1][2][3], Varmilo
keyboards pretending to be Apple devices report themselves as "AONE Varmilo
Keyboard", so it might be a good idea to add "AONE" to the list of clone
vendors.

Regards,
Bryan

[1] https://geekhack.org/index.php?topic=110250.msg3013629#msg3013629
[2] https://www.mail-archive.com/[email protected]/msg1750654.html
[3] https://forums.servethehome.com/index.php?threads/varmilo-keyboard-fn-keys-under-linux.29041/

On Tue, May 31, 2022 at 5:27 PM Hilton Chain <[email protected]> wrote:
>
> This commit extends fa33382c7f74 ("HID: apple: Properly handle function
> keys on Keychron keyboards") by adding an array of known non-Apple
> keyboards' device names, and the function apple_is_non_apple_keyboard()
> to identify and create exception for them.
>
> Signed-off-by: Hilton Chain <[email protected]>
> ---
> drivers/hid/hid-apple.c | 33 ++++++++++++++++++++++++++++-----
> 1 file changed, 28 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
> index 42a568902f49..7d56530d9e3a 100644
> --- a/drivers/hid/hid-apple.c
> +++ b/drivers/hid/hid-apple.c
> @@ -36,7 +36,7 @@
> #define APPLE_NUMLOCK_EMULATION BIT(8)
> #define APPLE_RDESC_BATTERY BIT(9)
> #define APPLE_BACKLIGHT_CTL BIT(10)
> -#define APPLE_IS_KEYCHRON BIT(11)
> +#define APPLE_IS_NON_APPLE BIT(11)
>
> #define APPLE_FLAG_FKEY 0x01
>
> @@ -65,6 +65,10 @@ MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. "
> "(For people who want to keep PC keyboard muscle memory. "
> "[0] = as-is, Mac layout, 1 = swapped, PC layout)");
>
> +struct apple_non_apple_keyboard {
> + char *name;
> +};
> +
> struct apple_sc_backlight {
> struct led_classdev cdev;
> struct hid_device *hdev;
> @@ -313,6 +317,25 @@ static const struct apple_key_translation swapped_fn_leftctrl_keys[] = {
> { }
> };
>
> +static const struct apple_non_apple_keyboard non_apple_keyboards[] = {
> + { "SONiX USB DEVICE" },
> + { "Keychron" },
> +};
> +
> +static bool apple_is_non_apple_keyboard(struct hid_device *hdev)
> +{
> + int i;
> +
> + for (i = 0; i < ARRAY_SIZE(non_apple_keyboards); i++) {
> + char *non_apple = non_apple_keyboards[i].name;
> +
> + if (strlen(non_apple) && strncmp(hdev->name, non_apple, strlen(non_apple)) == 0)
> + return true;
> + }
> +
> + return false;
> +}
> +
> static inline void apple_setup_key_translation(struct input_dev *input,
> const struct apple_key_translation *table)
> {
> @@ -363,7 +386,7 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
> }
>
> if (fnmode == 3) {
> - real_fnmode = (asc->quirks & APPLE_IS_KEYCHRON) ? 2 : 1;
> + real_fnmode = (asc->quirks & APPLE_IS_NON_APPLE) ? 2 : 1;
> } else {
> real_fnmode = fnmode;
> }
> @@ -669,9 +692,9 @@ static int apple_input_configured(struct hid_device *hdev,
> asc->quirks &= ~APPLE_HAS_FN;
> }
>
> - if (strncmp(hdev->name, "Keychron", 8) == 0) {
> - hid_info(hdev, "Keychron keyboard detected; function keys will default to fnmode=2 behavior\n");
> - asc->quirks |= APPLE_IS_KEYCHRON;
> + if (apple_is_non_apple_keyboard(hdev)) {
> + hid_info(hdev, "Non-apple keyboard detected; function keys will default to fnmode=2 behavior\n");
> + asc->quirks |= APPLE_IS_NON_APPLE;
> }
>
> return 0;
>
> base-commit: e1cbc3b96a9974746b2a80c3a6c8a0f7eff7b1b5
> --
> 2.36.1
>