Skip to contentSkip to conversation

HID: apple: Add 2021 magic keyboard FN key mapping

PATCH In review Benjamin Berg 4 participants 5 messages Nov 8, 2021 – Jan 4, 2022 HID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 HID: apple: Add 2021 magic keyboard FN key mapping

From: Benjamin Berg <[email protected]>

The new 2021 apple models have a different FN key assignment. Add a new translation table and use that for the 2021 magic keyboard.

---
 drivers/hid/hid-apple.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/hid/hid-apple.c +28 -1
index 2c9c5faa74a9..61dfe983828f 100644
@@ -70,6 +70,28 @@ struct apple_key_translation {
 	u8 flags;
 };
 
+static const struct apple_key_translation apple2021_fn_keys[] = {
+	{ KEY_BACKSPACE, KEY_DELETE },
+	{ KEY_ENTER,	KEY_INSERT },
+	{ KEY_F1,	KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
+	{ KEY_F2,	KEY_BRIGHTNESSUP,   APPLE_FLAG_FKEY },
+	{ KEY_F3,	KEY_SCALE,          APPLE_FLAG_FKEY },
+	{ KEY_F4,	KEY_SEARCH,         APPLE_FLAG_FKEY },
+	{ KEY_F5,	KEY_MICMUTE,        APPLE_FLAG_FKEY },
+	{ KEY_F6,	KEY_SLEEP,          APPLE_FLAG_FKEY },
+	{ KEY_F7,	KEY_PREVIOUSSONG,   APPLE_FLAG_FKEY },
+	{ KEY_F8,	KEY_PLAYPAUSE,      APPLE_FLAG_FKEY },
+	{ KEY_F9,	KEY_NEXTSONG,       APPLE_FLAG_FKEY },
+	{ KEY_F10,	KEY_MUTE,           APPLE_FLAG_FKEY },
+	{ KEY_F11,	KEY_VOLUMEDOWN,     APPLE_FLAG_FKEY },
+	{ KEY_F12,	KEY_VOLUMEUP,       APPLE_FLAG_FKEY },
+	{ KEY_UP,	KEY_PAGEUP },
+	{ KEY_DOWN,	KEY_PAGEDOWN },
+	{ KEY_LEFT,	KEY_HOME },
+	{ KEY_RIGHT,	KEY_END },
+	{ }
+};
+
 static const struct apple_key_translation macbookair_fn_keys[] = {
 	{ KEY_BACKSPACE, KEY_DELETE },
 	{ KEY_ENTER,	KEY_INSERT },
@@ -214,7 +236,9 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
 	}
 
 	if (fnmode) {
-		if (hid->product >= USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI &&
+		if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021)
+			table = apple2021_fn_keys;
+		else if (hid->product >= USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI &&
 				hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS)
 			table = macbookair_fn_keys;
 		else if (hid->product < 0x21d || hid->product >= 0x300)
@@ -376,6 +400,9 @@ static void apple_setup_input(struct input_dev *input)
 	for (trans = apple_iso_keyboard; trans->from; trans++)
 		set_bit(trans->to, input->keybit);
 
+	for (trans = apple2021_fn_keys; trans->from; trans++)
+		set_bit(trans->to, input->keybit);
+
 	if (swap_fn_leftctrl) {
 		for (trans = swapped_fn_leftctrl_keys; trans->from; trans++)
 			set_bit(trans->to, input->keybit);
Signature · 1 line
2.31.1
Tested-by on v1 1/1
#1 lore raw

drivers/hid/hid-apple.c static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input, · line 240

Quoted 3 lines · Benjamin Berg
drivers/hid/hid-apple.c +2 -0
+               if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021)
+                       table = apple2021_fn_keys;

This will have to be updated to cover USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 and USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021 too.

Other than that, the patch looks good, thanks!

#3 lore raw
Quoted 1 line · José Expósito

Works as expected a code looks good to me.

Actually, just a little comment on the code, sorry for the extra email:

drivers/hid/hid-apple.c struct apple_key_translation { · line 73

Quoted 9 lines
drivers/hid/hid-apple.c +1 -0
index 2c9c5faa74a9..61dfe983828f 100644
@@ -70,6 +70,28 @@ struct apple_key_translation {
 	u8 flags;
 };
 
+static const struct apple_key_translation apple2021_fn_keys[] = {

Since this driver handles many devices, I think that renaming this struct to "magic_keyboard_2021_fn_keys" would help to quickly understand which is the target device.

#4 lore raw

Hej,

Quoted 13 lines · José Expósito
Quoted 1 line · José Expósito

Works as expected a code looks good to me.

Actually, just a little comment on the code, sorry for the extra email:

Quoted 8 lines
drivers/hid/hid-apple.c +0 -0
index 2c9c5faa74a9..61dfe983828f 100644
@@ -70,6 +70,28 @@ struct apple_key_translation {
 	u8 flags;
 };
 
Quoted 5 lines

drivers/hid/hid-apple.c struct apple_key_translation { · line 73

Quoted 1 line
drivers/hid/hid-apple.c +1 -0
+static const struct apple_key_translation apple2021_fn_keys[] = {

Since this driver handles many devices, I think that renaming this struct to "magic_keyboard_2021_fn_keys" would help to quickly understand which is the target device.

A more generic name will make sense in the future since the Apple silicon based MacBooks (except for the 2020 13-inch MacBook Pro) use the same mapping. Keyboard and trackpad on those devices use HID over SPI and identify themself as "Apple Internal Keyboard / Trackpad".

The HID over SPI low level transport is still work in progress but the keyboard works as expected with minimal changes to hid-apple.c.

I can of course just rename the struct in the commit adding support for the MacBooks.

best regards

Janne