Skip to content

Commit

Permalink
Add auto touchbar brightness support
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaGarg8 authored Dec 29, 2024
1 parent 9d550ec commit 696e715
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 20 deletions.
10 changes: 5 additions & 5 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 c1017aa85e76f3a5f549f56973ab895f8e22ad27 Mon Sep 17 00:00:00 2001
From 1e34fd0bbe2ad1fd4d2091379b93ac19b84b7af3 Mon Sep 17 00:00:00 2001
From: Kerem Karabay <[email protected]>
Date: Sun, 5 Mar 2023 18:52:43 +0300
Subject: [PATCH] HID: hid-appletb-bl: add driver for the backlight of Apple
Touch Bars
Subject: [PATCH 1/4] 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 @@ -25,7 +25,7 @@ Signed-off-by: Aditya Garg <[email protected]>
create mode 100644 drivers/hid/hid-appletb-bl.c

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 08446c89e..4988c1fb2 100644
index 4d2a89d65..f6678db27 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -148,6 +148,16 @@ config HID_APPLEIR
Expand All @@ -46,7 +46,7 @@ index 08446c89e..4988c1fb2 100644
tristate "Asus"
depends on USB_HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index e40f1ddeb..1d825a474 100644
index 24de45f36..444d24cec 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -29,6 +29,7 @@ obj-$(CONFIG_HID_ALPS) += hid-alps.o
Expand Down
28 changes: 18 additions & 10 deletions 1006-HID-hid-appletb-kbd-add-driver-for-the-keyboard-mode.patch
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
From 5a1adaa13943fd27405f3bbc2ca5e2f24f9641a0 Mon Sep 17 00:00:00 2001
From: Kerem Karabay <[email protected]>
Date: Sun, 5 Mar 2023 18:17:23 +0300
Subject: [PATCH 2/4] 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

The Touch Bars found on x86 Macs support two USB configurations: one
where the device presents itself as a HID keyboard and can display
Expand All @@ -16,11 +23,11 @@ Co-developed-by: Aditya Garg <[email protected]>
Signed-off-by: Aditya Garg <[email protected]>
---
.../ABI/testing/sysfs-driver-hid-appletb-kbd | 13 +
drivers/hid/Kconfig | 11 +
drivers/hid/Kconfig | 13 +
drivers/hid/Makefile | 1 +
drivers/hid/hid-appletb-kbd.c | 304 ++++++++++++++++++
drivers/hid/hid-appletb-kbd.c | 303 ++++++++++++++++++
drivers/hid/hid-quirks.c | 4 +-
5 files changed, 332 insertions(+), 1 deletion(-)
5 files changed, 333 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 @@ -44,16 +51,18 @@ index 000000000..2a19584d0
+ 3 None
+ == =================
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 4988c1fb2..72b665eda 100644
index f6678db27..5b16bfadc 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -158,6 +158,17 @@ config HID_APPLETB_BL
@@ -158,6 +158,19 @@ config HID_APPLETB_BL
To compile this driver as a module, choose M here: the
module will be called hid-appletb-bl.

+config HID_APPLETB_KBD
+ tristate "Apple Touch Bar Keyboard Mode"
+ depends on USB_HID
+ depends on INPUT
+ select INPUT_SPARSEKMAP
+ help
+ Say Y here if you want support for the keyboard mode (escape,
+ function, media and brightness keys) of Touch Bars on x86 MacBook
Expand All @@ -66,7 +75,7 @@ index 4988c1fb2..72b665eda 100644
tristate "Asus"
depends on USB_HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 1d825a474..d903c9a26 100644
index 444d24cec..1989288e0 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -30,6 +30,7 @@ obj-$(CONFIG_HID_ACRUX) += hid-axff.o
Expand All @@ -79,10 +88,10 @@ index 1d825a474..d903c9a26 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..ecac68fc7
index 000000000..80c87396f
--- /dev/null
+++ b/drivers/hid/hid-appletb-kbd.c
@@ -0,0 +1,304 @@
@@ -0,0 +1,303 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Apple Touch Bar Keyboard Mode Driver
Expand Down Expand Up @@ -274,9 +283,8 @@ index 000000000..ecac68fc7
+
+ sparse_keymap_setup(input, appletb_kbd_keymap, NULL);
+
+ for (idx = 0; appletb_kbd_keymap[idx].type != KE_END; idx++) {
+ for (idx = 0; appletb_kbd_keymap[idx].type != KE_END; idx++)
+ input_set_capability(input, EV_KEY, appletb_kbd_keymap[idx].code);
+ }
+
+ return 0;
+}
Expand Down
14 changes: 9 additions & 5 deletions 1017-HID-hid-appletb-kbd-add-support-for-fn-toggle-betwee.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
From e2e32c6300afa1f8a0a2c9fc721cfc975645999c Mon Sep 17 00:00:00 2001
From: Aditya Garg <[email protected]>
HID: hid-appletb-kbd: add support for fn toggle between media and function mode
Date: Fri, 9 Aug 2024 19:40:53 +0530
Subject: [PATCH 3/4] HID: hid-appletb-kbd: add support for fn toggle between
media and function mode

This patch adds support for the switching between the Media and Function
keys on the touchbar by pressing the Fn key on Apple Internal Keyboard.

Expand All @@ -9,7 +13,7 @@ Signed-off-by: Aditya Garg <[email protected]>
1 file changed, 128 insertions(+)

diff --git a/drivers/hid/hid-appletb-kbd.c b/drivers/hid/hid-appletb-kbd.c
index ecac68fc7..442c4d884 100644
index 80c87396f..de5770500 100644
--- a/drivers/hid/hid-appletb-kbd.c
+++ b/drivers/hid/hid-appletb-kbd.c
@@ -26,6 +26,8 @@
Expand Down Expand Up @@ -116,7 +120,7 @@ index ecac68fc7..442c4d884 100644
static int appletb_kbd_input_configured(struct hid_device *hdev, struct hid_input *hidinput)
{
int idx;
@@ -196,6 +274,40 @@ static int appletb_kbd_input_configured(struct hid_device *hdev, struct hid_inpu
@@ -195,6 +273,40 @@ static int appletb_kbd_input_configured(struct hid_device *hdev, struct hid_inpu
return 0;
}

Expand Down Expand Up @@ -157,7 +161,7 @@ index ecac68fc7..442c4d884 100644
static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
struct appletb_kbd *kbd;
@@ -228,6 +340,20 @@ static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id
@@ -227,6 +339,20 @@ static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id
goto stop_hw;
}

Expand All @@ -178,7 +182,7 @@ index ecac68fc7..442c4d884 100644
ret = appletb_kbd_set_mode(kbd, appletb_tb_def_mode);
if (ret) {
dev_err_probe(dev, ret, "Failed to set touchbar mode\n");
@@ -251,6 +377,8 @@ static void appletb_kbd_remove(struct hid_device *hdev)
@@ -250,6 +376,8 @@ static void appletb_kbd_remove(struct hid_device *hdev)

appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF);

Expand Down
185 changes: 185 additions & 0 deletions 1018-HID-hid-appletb-kbd-add-support-for-automatic-bright.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
From b6124341976d40b5ab07a4fc1bc3fac17c210c17 Mon Sep 17 00:00:00 2001
From: Aditya Garg <[email protected]>
Date: Sun, 29 Dec 2024 12:36:09 +0530
Subject: [PATCH 4/4] HID: hid-appletb-kbd: add support for automatic
brightness control while using the touchbar

On Windows, if there is no input by the user for 60 sec, the Touch Bar dims
automatically, and after further 15 sec, it turns off. On receiving input,
the Touch Bar resets the timer and goes back to full brightness. This patch
implements the same functionality.

Signed-off-by: Aditya Garg <[email protected]>
---
drivers/hid/Kconfig | 1 +
drivers/hid/hid-appletb-kbd.c | 75 +++++++++++++++++++++++++++++++++--
2 files changed, 72 insertions(+), 4 deletions(-)

diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 5b16bfadc..9b183633f 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -161,6 +161,7 @@ config HID_APPLETB_BL
config HID_APPLETB_KBD
tristate "Apple Touch Bar Keyboard Mode"
depends on USB_HID
+ depends on BACKLIGHT_CLASS_DEVICE
depends on INPUT
select INPUT_SPARSEKMAP
help
diff --git a/drivers/hid/hid-appletb-kbd.c b/drivers/hid/hid-appletb-kbd.c
index de5770500..208173faa 100644
--- a/drivers/hid/hid-appletb-kbd.c
+++ b/drivers/hid/hid-appletb-kbd.c
@@ -16,6 +16,8 @@
#include <linux/bitops.h>
#include <linux/module.h>
#include <linux/string.h>
+#include <linux/backlight.h>
+#include <linux/timer.h>
#include <linux/input/sparse-keymap.h>

#include "hid-ids.h"
@@ -27,6 +29,7 @@
#define APPLETB_KBD_MODE_MAX APPLETB_KBD_MODE_OFF

#define APPLETB_DEVID_KEYBOARD 1
+#define APPLETB_DEVID_TRACKPAD 2

#define HID_USAGE_MODE 0x00ff0004

@@ -41,14 +44,25 @@ static bool appletb_tb_fn_toggle = true;
module_param_named(fntoggle, appletb_tb_fn_toggle, bool, 0644);
MODULE_PARM_DESC(fntoggle, "Switch between Fn and media controls on pressing Fn key");

+static int appletb_tb_dim_timeout = 60;
+module_param_named(dim_timeout, appletb_tb_dim_timeout, int, 0644);
+MODULE_PARM_DESC(dim_timeout, "Dim timeout in sec");
+
+static int appletb_tb_idle_timeout = 15;
+module_param_named(idle_timeout, appletb_tb_idle_timeout, int, 0644);
+MODULE_PARM_DESC(idle_timeout, "Idle timeout in sec");
+
struct appletb_kbd {
struct hid_field *mode_field;
-
- u8 saved_mode;
- u8 current_mode;
struct input_handler inp_handler;
struct input_handle kbd_handle;
-
+ struct input_handle tpd_handle;
+ struct backlight_device *backlight_dev;
+ struct timer_list inactivity_timer;
+ bool has_dimmed;
+ bool has_turned_off;
+ u8 saved_mode;
+ u8 current_mode;
};

static const struct key_entry appletb_kbd_keymap[] = {
@@ -146,6 +160,34 @@ static int appletb_tb_key_to_slot(unsigned int code)
}
}

+static void appletb_inactivity_timer(struct timer_list *t)
+{
+ struct appletb_kbd *kbd = from_timer(kbd, t, inactivity_timer);
+
+ if (kbd->backlight_dev) {
+ if (!kbd->has_dimmed) {
+ backlight_device_set_brightness(kbd->backlight_dev, 1);
+ kbd->has_dimmed = true;
+ mod_timer(&kbd->inactivity_timer, jiffies + msecs_to_jiffies(appletb_tb_idle_timeout * 1000));
+ } else if (!kbd->has_turned_off) {
+ backlight_device_set_brightness(kbd->backlight_dev, 0);
+ kbd->has_turned_off = true;
+ }
+ }
+}
+
+static void reset_inactivity_timer(struct appletb_kbd *kbd)
+{
+ if (kbd->backlight_dev) {
+ if (kbd->has_dimmed || kbd->has_turned_off) {
+ backlight_device_set_brightness(kbd->backlight_dev, 2);
+ kbd->has_dimmed = false;
+ kbd->has_turned_off = false;
+ }
+ mod_timer(&kbd->inactivity_timer, jiffies + msecs_to_jiffies(appletb_tb_dim_timeout * 1000));
+ }
+}
+
static int appletb_kbd_hid_event(struct hid_device *hdev, struct hid_field *field,
struct hid_usage *usage, __s32 value)
{
@@ -170,6 +212,8 @@ static int appletb_kbd_hid_event(struct hid_device *hdev, struct hid_field *fiel
if (slot < 0)
return 0;

+ reset_inactivity_timer(kbd);
+
translation = sparse_keymap_entry_from_scancode(input, usage->code);

if (translation && kbd->current_mode == APPLETB_KBD_MODE_SPCL) {
@@ -186,6 +230,8 @@ static void appletb_kbd_inp_event(struct input_handle *handle, unsigned int type
{
struct appletb_kbd *kbd = handle->private;

+ reset_inactivity_timer(kbd);
+
if (type == EV_KEY && code == KEY_FN && appletb_tb_fn_toggle) {
if (value == 1) {
kbd->saved_mode = kbd->current_mode;
@@ -211,6 +257,9 @@ static int appletb_kbd_inp_connect(struct input_handler *handler,
if (id->driver_info == APPLETB_DEVID_KEYBOARD) {
handle = &kbd->kbd_handle;
handle->name = "tbkbd";
+ } else if (id->driver_info == APPLETB_DEVID_TRACKPAD) {
+ handle = &kbd->tpd_handle;
+ handle->name = "tbtpd";
} else {
return -ENOENT;
}
@@ -283,6 +332,15 @@ static const struct input_device_id appletb_kbd_input_devices[] = {
.keybit = { [BIT_WORD(KEY_FN)] = BIT_MASK(KEY_FN) },
.driver_info = APPLETB_DEVID_KEYBOARD,
},
+ {
+ .flags = INPUT_DEVICE_ID_MATCH_BUS |
+ INPUT_DEVICE_ID_MATCH_VENDOR |
+ INPUT_DEVICE_ID_MATCH_KEYBIT,
+ .bustype = BUS_USB,
+ .vendor = USB_VENDOR_ID_APPLE,
+ .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
+ .driver_info = APPLETB_DEVID_TRACKPAD,
+ },
{ }
};

@@ -339,6 +397,14 @@ static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id
goto stop_hw;
}

+ timer_setup(&kbd->inactivity_timer, appletb_inactivity_timer, 0);
+ mod_timer(&kbd->inactivity_timer, jiffies + msecs_to_jiffies(appletb_tb_dim_timeout * 1000));
+ kbd->backlight_dev = backlight_device_get_by_name("appletb_backlight");
+ if (!kbd->backlight_dev)
+ dev_err_probe(dev, ret, "Failed to get backlight device\n");
+ else
+ backlight_device_set_brightness(kbd->backlight_dev, 2);
+
kbd->inp_handler.event = appletb_kbd_inp_event;
kbd->inp_handler.connect = appletb_kbd_inp_connect;
kbd->inp_handler.disconnect = appletb_kbd_inp_disconnect;
@@ -377,6 +443,7 @@ static void appletb_kbd_remove(struct hid_device *hdev)
appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF);

input_unregister_handler(&kbd->inp_handler);
+ del_timer_sync(&kbd->inactivity_timer);

hid_hw_close(hdev);
hid_hw_stop(hdev);
--
2.43.0

0 comments on commit 696e715

Please sign in to comment.