Hi all,
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
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 | 92 ++++++++++++++++++++++++++++++++---------
1 file changed, 73 insertions(+), 19 deletions(-)
--
2.25.1
The code used to map the apple_key_translation structs is duplicated.
Extract it to a common function.
Refactor, no functional changes.
Signed-off-by: José Expósito <[email protected]>
---
drivers/hid/hid-apple.c | 32 +++++++++++++++-----------------
1 file changed, 15 insertions(+), 17 deletions(-)
diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index 2c9c5faa74a9..aa3d643e3615 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -174,6 +174,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)
{
@@ -359,27 +368,16 @@ 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);
+ 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);
- for (trans = apple_iso_keyboard; 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);
- }
+ 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,
--
2.25.1
The 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.
Signed-off-by: José Expósito <[email protected]>
---
drivers/hid/hid-apple.c | 34 +++++++++++++++++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)
diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index aa3d643e3615..bea752da4ac3 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -70,6 +70,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 macbookair_fn_keys[] = {
{ KEY_BACKSPACE, KEY_DELETE },
{ KEY_ENTER, KEY_INSERT },
@@ -223,7 +244,17 @@ 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_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_WELLSPRING4_ANSI &&
hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS)
table = macbookair_fn_keys;
else if (hid->product < 0x21d || hid->product >= 0x300)
@@ -375,6 +406,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);
if (swap_fn_leftctrl)
apple_setup_key_translation(input, swapped_fn_leftctrl_keys);
--
2.25.1
The 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.
Signed-off-by: José Expósito <[email protected]>
---
drivers/hid/hid-apple.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index bea752da4ac3..d0f23da11dfd 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -91,6 +91,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 macbookair_fn_keys[] = {
{ KEY_BACKSPACE, KEY_DELETE },
{ KEY_ENTER, KEY_INSERT },
@@ -254,6 +274,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_WELLSPRING4_ANSI &&
hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS)
table = macbookair_fn_keys;
@@ -407,6 +430,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);
if (swap_fn_leftctrl)
apple_setup_key_translation(input, swapped_fn_leftctrl_keys);
--
2.25.1