Magic Keyboard first generation and 2015 FN key mapping
v2 cover Magic Keyboard first generation and 2015 FN key mapping
Hi everyone,
This patchset adds the missing key mapping for the Magic Keyboard first generation and the 2015 models.
Since the first generation model had numlock emulation in place, I decided to fix the bug (described in the patch) and keep it in place just because it has been there for so long that people might use it. Having said that, there is no indication in the keyboard about which keys can be used as numbers or how to enable/disable numlock. I think that a patch removing APPLE_NUMLOCK_EMULATION might make sense, but I didn't want to remove existing functionality without asking :)
Thanks a lot,
José Expósito
v2: Resolve conflicts
José Expósito (3):
HID: apple: Refactor key translation setup
HID: apple: Magic Keyboard first generation FN key mapping
HID: apple: Magic Keyboard 2015 FN key mapping
drivers/hid/hid-apple.c | 100 ++++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 24 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/3 HID: apple: Refactor key translation setup
The code used to map the apple_key_translation structs is duplicated. Extract it to a common function.
Refactor, no functional changes.
--- drivers/hid/hid-apple.c | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-)
drivers/hid/hid-apple.c +17 -21
@@ -202,6 +202,15 @@ static const struct apple_key_translation swapped_fn_leftctrl_keys[] = { { } }; +static inline void apple_setup_key_translation (struct input_dev *input, +const struct apple_key_translation *table) +{ +const struct apple_key_translation *trans; + +for (trans = table; trans->from; trans++) +set_bit (trans->to, input->keybit); +} +static const struct apple_key_translation *apple_find_translation (const struct apple_key_translation *table,u16 from) {
@@ -452,30 +461,17 @@ static __u8 *apple_report_fixup(struct hid_device *hdev, __u8 *rdesc,static void apple_setup_input (struct input_dev *input) { -const struct apple_key_translation *trans; -set_bit (KEY_NUMLOCK , input->keybit);/* Enable all needed keys */ -for (trans = apple_fn_keys; trans->from; trans++) -set_bit (trans->to, input->keybit); - -for (trans = powerbook_fn_keys; trans->from; trans++) -set_bit (trans->to, input->keybit); - -for (trans = powerbook_numlock_keys; trans->from; trans++) -set_bit (trans->to, input->keybit); - -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); - } +apple_setup_key_translation (input, apple_fn_keys); +apple_setup_key_translation (input, powerbook_fn_keys); +apple_setup_key_translation (input, powerbook_numlock_keys); +apple_setup_key_translation (input, apple_iso_keyboard); +apple_setup_key_translation (input, apple2021_fn_keys); + +if (swap_fn_leftctrl) +apple_setup_key_translation (input, swapped_fn_leftctrl_keys); } static int apple_input_mapping(struct hid_device *hdev, struct hid_input *hi,
Signature · 1 line
2.25.1
PATCH v2 2/3 HID: apple: Magic Keyboard first generation FN key mapping
In reply to José ExpósitoThe function key mapping for the Magic Keyboard first generation (2007, 2009 and 2011 aluminum wireless models) was not present and the default one was used instead.
This caused two main issues:
- The F5 and F6 keys were sending KEY_KBDILLUMDOWN and KEY_KBDILLUMUP; however, the keyboard is not backlited.
- The keyboard has the APPLE_NUMLOCK_EMULATION quirk with F6 set as the KEY_NUMLOCK key by "powerbook_numlock_keys". However, because F6 was mapped to KEY_KBDILLUMUP by the default mapping it was not possible to switch the numlock status.
This means that, if numlock was enabled on session startup, it was not possible to disable it without connecting another keyboard.
Add a custom translation table for the device leaving F5 unassigned and using F6 as the KEY_NUMLOCK key.
--- drivers/hid/hid-apple.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-)
drivers/hid/hid-apple.c +35 -3
@@ -76,6 +76,27 @@ struct apple_key_translation {u8 flags; }; +static const struct apple_key_translation magic_keyboard_alu_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_DASHBOARD ,APPLE_FLAG_FKEY }, + {KEY_F6 ,KEY_NUMLOCK ,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 apple2021_fn_keys[] = { {KEY_BACKSPACE ,KEY_DELETE }, {KEY_ENTER ,KEY_INSERT },
@@ -251,9 +272,19 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input, }if (fnmode) { -if (hid->product ==USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021 || - hid->product ==USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 || - hid->product ==USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021 ) +if (hid->product ==USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI || + hid->product ==USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO || + hid->product ==USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS || + hid->product ==USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI || + hid->product ==USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO || + hid->product ==USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS || + hid->product ==USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI || + hid->product ==USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO || + hid->product ==USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS ) + table = magic_keyboard_alu_fn_keys; +else if (hid->product ==USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021 || + hid->product ==USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 || + hid->product ==USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021 ) table = apple2021_fn_keys;else if (hid->product >=USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI && hid->product <=USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS )
@@ -468,6 +499,7 @@ static void apple_setup_input(struct input_dev *input)apple_setup_key_translation (input, powerbook_fn_keys);apple_setup_key_translation (input, powerbook_numlock_keys);apple_setup_key_translation (input, apple_iso_keyboard); +apple_setup_key_translation (input, magic_keyboard_alu_fn_keys);apple_setup_key_translation (input, apple2021_fn_keys);if (swap_fn_leftctrl)
Signature · 1 line
2.25.1
PATCH v2 3/3 HID: apple: Magic Keyboard 2015 FN key mapping
In reply to José ExpósitoThe Magic Keyboard 2015 function key mapping was not present and the default mapping was used.
While this worked for most keys, the F5 and F6 keys were sending KEY_KBDILLUMDOWN and KEY_KBDILLUMUP; however, the keyboard is not backlited.
Add a custom translation table for the keyboard leaving F5 and F6 unassigned to mimic the default behavior on macOS.
--- drivers/hid/hid-apple.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
drivers/hid/hid-apple.c +24 -0
@@ -97,6 +97,26 @@ static const struct apple_key_translation magic_keyboard_alu_fn_keys[] = { { } }; +static const struct apple_key_translation magic_keyboard_2015_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_DASHBOARD ,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 apple2021_fn_keys[] = { {KEY_BACKSPACE ,KEY_DELETE }, {KEY_ENTER ,KEY_INSERT },
@@ -282,6 +302,9 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input, hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO || hid->product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS) table = magic_keyboard_alu_fn_keys; + else if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015 || + hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015) + table = magic_keyboard_2015_fn_keys; else if (hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021 || hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021 || hid->product == USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021)
@@ -500,6 +523,7 @@ static void apple_setup_input(struct input_dev *input)apple_setup_key_translation (input, powerbook_numlock_keys);apple_setup_key_translation (input, apple_iso_keyboard);apple_setup_key_translation (input, magic_keyboard_alu_fn_keys); +apple_setup_key_translation (input, magic_keyboard_2015_fn_keys);apple_setup_key_translation (input, apple2021_fn_keys);if (swap_fn_leftctrl)
Signature · 1 line
2.25.1
Quoted 13 lines · José Expósito
Hi everyone,
This patchset adds the missing key mapping for the Magic Keyboard first generation and the 2015 models.
Since the first generation model had numlock emulation in place, I decided to fix the bug (described in the patch) and keep it in place just because it has been there for so long that people might use it. Having said that, there is no indication in the keyboard about which keys can be used as numbers or how to enable/disable numlock. I think that a patch removing
APPLE_NUMLOCK_EMULATIONmight make sense, but I didn't want to remove existing functionality without asking :)
Quoted 4 lines
Thanks a lot,
José Expósitov2: Resolve conflicts
Queued in hid.git#for-5.18/apple. Thanks José,
Signature · 2 lines
Jiri Kosina SUSE Labs