Skip to content

Commit

Permalink
Add support for basic mode of touchbar
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaGarg8 authored Aug 5, 2024
1 parent 7b0f682 commit 9f7064d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 33 deletions.
47 changes: 30 additions & 17 deletions 1005-HID-hid-appletb-bl-add-driver-for-the-backlight-of-A.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
From 05cd738ce1c0e1a930a1dab02528fd9f1c702c38 Mon Sep 17 00:00:00 2001
From c1017aa85e76f3a5f549f56973ab895f8e22ad27 Mon Sep 17 00:00:00 2001
From: Kerem Karabay <[email protected]>
Date: Sun, 5 Mar 2023 18:52:43 +0300
Subject: [PATCH 02/12] HID: hid-appletb-bl: add driver for the backlight of
Apple Touch Bars
Subject: [PATCH] HID: hid-appletb-bl: add driver for the backlight of Apple
Touch Bars
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Expand All @@ -15,19 +15,19 @@ This driver is based on previous work done by Ronald Tschalär

Signed-off-by: Kerem Karabay <[email protected]>
---
MAINTAINERS | 6 ++
MAINTAINERS | 6 +
drivers/hid/Kconfig | 10 ++
drivers/hid/Makefile | 1 +
drivers/hid/hid-appletb-bl.c | 193 +++++++++++++++++++++++++++++++++++
drivers/hid/hid-appletb-bl.c | 206 +++++++++++++++++++++++++++++++++++
drivers/hid/hid-quirks.c | 4 +-
5 files changed, 213 insertions(+), 1 deletion(-)
5 files changed, 226 insertions(+), 1 deletion(-)
create mode 100644 drivers/hid/hid-appletb-bl.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 4cc6bf79f..519b3b736 100644
index 8766f3e5e..ac27f41d4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -9157,6 +9157,12 @@ F: include/linux/pm.h
@@ -9931,6 +9931,12 @@ F: include/linux/pm.h
F: include/linux/suspend.h
F: kernel/power/

Expand All @@ -41,7 +41,7 @@ index 4cc6bf79f..519b3b736 100644
M: Jiri Kosina <[email protected]>
M: Benjamin Tissoires <[email protected]>
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index e11c1c803..cf19a3b33 100644
index 08446c89e..4988c1fb2 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -148,6 +148,16 @@ config HID_APPLEIR
Expand All @@ -62,7 +62,7 @@ index e11c1c803..cf19a3b33 100644
tristate "Asus"
depends on USB_HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 7a9e16015..bc86e38b2 100644
index e40f1ddeb..1d825a474 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -29,6 +29,7 @@ obj-$(CONFIG_HID_ALPS) += hid-alps.o
Expand All @@ -75,10 +75,10 @@ index 7a9e16015..bc86e38b2 100644
obj-$(CONFIG_HID_AUREAL) += hid-aureal.o
diff --git a/drivers/hid/hid-appletb-bl.c b/drivers/hid/hid-appletb-bl.c
new file mode 100644
index 000000000..0c5e4b776
index 000000000..00bbe45df
--- /dev/null
+++ b/drivers/hid/hid-appletb-bl.c
@@ -0,0 +1,193 @@
@@ -0,0 +1,206 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Apple Touch Bar Backlight Driver
Expand All @@ -104,6 +104,13 @@ index 000000000..0c5e4b776
+#define HID_USAGE_AUX1 0xff120020
+#define HID_USAGE_BRIGHTNESS 0xff120021
+
+static int appletb_bl_def_brightness = 2;
+module_param_named(brightness, appletb_bl_def_brightness, int, 0444);
+MODULE_PARM_DESC(brightness, "Default brightness:\n"
+ " 0 - Touchbar is off\n"
+ " 1 - Dim brightness\n"
+ " [2] - Full brightness");
+
+struct appletb_bl {
+ struct hid_field *aux1_field, *brightness_field;
+ struct backlight_device *bdev;
Expand Down Expand Up @@ -214,7 +221,13 @@ index 000000000..0c5e4b776
+ bl->aux1_field = aux1_field;
+ bl->brightness_field = brightness_field;
+
+ ret = appletb_bl_set_brightness(bl, APPLETB_BL_OFF);
+ if (appletb_bl_def_brightness == 0)
+ ret = appletb_bl_set_brightness(bl, APPLETB_BL_OFF);
+ else if (appletb_bl_def_brightness == 1)
+ ret = appletb_bl_set_brightness(bl, APPLETB_BL_DIM);
+ else
+ ret = appletb_bl_set_brightness(bl, APPLETB_BL_ON);
+
+ if (ret) {
+ dev_err_probe(dev, ret, "Failed to set touch bar brightness to off\n");
+ goto close_hw;
Expand Down Expand Up @@ -273,18 +286,18 @@ index 000000000..0c5e4b776
+MODULE_DESCRIPTION("MacBookPro Touch Bar Backlight Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
index 3983b4f28..82e7a80c9 100644
index e0bbf0c63..818d41a35 100644
--- a/drivers/hid/hid-quirks.c
+++ b/drivers/hid/hid-quirks.c
@@ -325,7 +325,6 @@ static const struct hid_device_id hid_have_special_driver[] = {
@@ -328,7 +328,6 @@ static const struct hid_device_id hid_have_special_driver[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021) },
- { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
#endif
#if IS_ENABLED(CONFIG_HID_APPLEIR)
@@ -335,6 +334,9 @@ static const struct hid_device_id hid_have_special_driver[] = {
@@ -338,6 +337,9 @@ static const struct hid_device_id hid_have_special_driver[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL5) },
#endif
Expand All @@ -295,5 +308,5 @@ index 3983b4f28..82e7a80c9 100644
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD) },
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD) },
--
2.42.0
2.43.0

59 changes: 43 additions & 16 deletions 1006-HID-hid-appletb-kbd-add-driver-for-the-keyboard-mode.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
From 514b4f088b7ed916c634ca6f61de72c5f86268dd Mon Sep 17 00:00:00 2001
From 8e77827903df3331dd38238a827d611b52446915 Mon Sep 17 00:00:00 2001
From: Kerem Karabay <[email protected]>
Date: Sun, 5 Mar 2023 18:17:23 +0300
Subject: [PATCH 03/12] HID: hid-appletb-kbd: add driver for the keyboard mode
of Apple Touch Bars
Subject: [PATCH] HID: hid-appletb-kbd: add driver for the keyboard mode of
Apple Touch Bars
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Expand All @@ -23,9 +23,9 @@ Signed-off-by: Kerem Karabay <[email protected]>
.../ABI/testing/sysfs-driver-hid-appletb-kbd | 13 +
drivers/hid/Kconfig | 11 +
drivers/hid/Makefile | 1 +
drivers/hid/hid-appletb-kbd.c | 289 ++++++++++++++++++
drivers/hid/hid-appletb-kbd.c | 316 ++++++++++++++++++
drivers/hid/hid-quirks.c | 4 +-
5 files changed, 317 insertions(+), 1 deletion(-)
5 files changed, 344 insertions(+), 1 deletion(-)
create mode 100644 Documentation/ABI/testing/sysfs-driver-hid-appletb-kbd
create mode 100644 drivers/hid/hid-appletb-kbd.c

Expand All @@ -49,7 +49,7 @@ index 000000000..2a19584d0
+ 3 None
+ == =================
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index cf19a3b33..852de13aa 100644
index 4988c1fb2..72b665eda 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -158,6 +158,17 @@ config HID_APPLETB_BL
Expand All @@ -71,7 +71,7 @@ index cf19a3b33..852de13aa 100644
tristate "Asus"
depends on USB_HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index bc86e38b2..5b60015fd 100644
index 1d825a474..d903c9a26 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -30,6 +30,7 @@ obj-$(CONFIG_HID_ACRUX) += hid-axff.o
Expand All @@ -84,16 +84,17 @@ index bc86e38b2..5b60015fd 100644
obj-$(CONFIG_HID_AUREAL) += hid-aureal.o
diff --git a/drivers/hid/hid-appletb-kbd.c b/drivers/hid/hid-appletb-kbd.c
new file mode 100644
index 000000000..bc004c408
index 000000000..453d5c7e1
--- /dev/null
+++ b/drivers/hid/hid-appletb-kbd.c
@@ -0,0 +1,289 @@
@@ -0,0 +1,316 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Apple Touch Bar Keyboard Mode Driver
+ *
+ * Copyright (c) 2017-2018 Ronald Tschalär
+ * Copyright (c) 2022-2023 Kerem Karabay <[email protected]>
+ * Copyright (c) 2024 Aditya Garg <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Expand All @@ -117,14 +118,21 @@ index 000000000..bc004c408
+
+#define HID_USAGE_MODE 0x00ff0004
+
+static int appletb_tb_def_mode = APPLETB_KBD_MODE_FN;
+module_param_named(mode, appletb_tb_def_mode, int, 0444);
+MODULE_PARM_DESC(mode, "Default touchbar mode:\n"
+ " 0 - escape key only\n"
+ " [1] - function-keys only\n"
+ " 2 - special keys only");
+
+struct appletb_kbd {
+ struct hid_field *mode_field;
+
+ u8 saved_mode;
+ u8 current_mode;
+};
+
+static const struct key_entry appletb_kbd_keymap[] = {
+static const struct key_entry appletb_kbd_keymap_spcl[] = {
+ { KE_KEY, KEY_ESC, { KEY_ESC } },
+ { KE_KEY, KEY_F1, { KEY_BRIGHTNESSDOWN } },
+ { KE_KEY, KEY_F2, { KEY_BRIGHTNESSUP } },
Expand All @@ -141,6 +149,23 @@ index 000000000..bc004c408
+ { KE_END, 0 }
+};
+
+static const struct key_entry appletb_kbd_keymap_fn[] = {
+ { KE_KEY, KEY_ESC, { KEY_ESC } },
+ { KE_KEY, KEY_F1, { KEY_F1 } },
+ { KE_KEY, KEY_F2, { KEY_F2 } },
+ { KE_KEY, KEY_F3, { KEY_F3 } },
+ { KE_KEY, KEY_F4, { KEY_F4 } },
+ { KE_KEY, KEY_F5, { KEY_F5 } },
+ { KE_KEY, KEY_F6, { KEY_F6 } },
+ { KE_KEY, KEY_F7, { KEY_F7 } },
+ { KE_KEY, KEY_F8, { KEY_F8 } },
+ { KE_KEY, KEY_F9, { KEY_F9 } },
+ { KE_KEY, KEY_F10, { KEY_F10 } },
+ { KE_KEY, KEY_F11, { KEY_F11 } },
+ { KE_KEY, KEY_F12, { KEY_F12 } },
+ { KE_END, 0 }
+};
+
+static int appletb_kbd_set_mode(struct appletb_kbd *kbd, u8 mode)
+{
+ struct hid_report *report = kbd->mode_field->report;
Expand Down Expand Up @@ -268,7 +293,9 @@ index 000000000..bc004c408
+
+ __set_bit(EV_REP, input->evbit);
+
+ return sparse_keymap_setup(input, appletb_kbd_keymap, NULL);
+ sparse_keymap_setup(input, appletb_kbd_keymap_fn, NULL);
+ sparse_keymap_setup(input, appletb_kbd_keymap_spcl, NULL);
+ return 0;
+}
+
+static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id *id)
Expand Down Expand Up @@ -303,7 +330,7 @@ index 000000000..bc004c408
+ goto stop_hw;
+ }
+
+ ret = appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF);
+ ret = appletb_kbd_set_mode(kbd, appletb_tb_def_mode);
+ if (ret) {
+ dev_err_probe(dev, ret, "Failed to set touchbar mode\n");
+ goto close_hw;
Expand Down Expand Up @@ -378,18 +405,18 @@ index 000000000..bc004c408
+MODULE_DESCRIPTION("MacBookPro Touch Bar Keyboard Mode Driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
index 82e7a80c9..82be9dfaf 100644
index 818d41a35..7c576d654 100644
--- a/drivers/hid/hid-quirks.c
+++ b/drivers/hid/hid-quirks.c
@@ -325,7 +325,6 @@ static const struct hid_device_id hid_have_special_driver[] = {
@@ -328,7 +328,6 @@ static const struct hid_device_id hid_have_special_driver[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021) },
- { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) },
#endif
#if IS_ENABLED(CONFIG_HID_APPLEIR)
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL) },
@@ -337,6 +336,9 @@ static const struct hid_device_id hid_have_special_driver[] = {
@@ -340,6 +339,9 @@ static const struct hid_device_id hid_have_special_driver[] = {
#if IS_ENABLED(CONFIG_HID_APPLETB_BL)
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
#endif
Expand All @@ -400,5 +427,5 @@ index 82e7a80c9..82be9dfaf 100644
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD) },
{ HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD) },
--
2.42.0
2.43.0

0 comments on commit 9f7064d

Please sign in to comment.