You've already forked linux-t2-patches
mirror of
https://github.com/t2linux/linux-t2-patches.git
synced 2026-04-30 13:52:11 -07:00
275 lines
8.2 KiB
Diff
275 lines
8.2 KiB
Diff
From 9f2b36e1c4f418c126b1d823bc00c620e6c4b811 Mon Sep 17 00:00:00 2001
|
|
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
|
|
Date: Mon, 14 Nov 2022 17:29:52 +0300
|
|
Subject: [PATCH] HID: apple-magic-backlight: init
|
|
|
|
Add support for the keyboard backlight on Intel T2 Macs
|
|
with internal Magic Keyboards (MacBookPro16,x and MacBookAir9,1)
|
|
|
|
Signed-off-by: Orlando Chamberlain <orlandoch.dev@gmail.com>
|
|
Co-developed-by: Kerem Karabay <kekrby@gmail.com>
|
|
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
|
|
---
|
|
MAINTAINERS | 6 ++
|
|
drivers/hid/Kconfig | 14 +++
|
|
drivers/hid/Makefile | 1 +
|
|
drivers/hid/apple-magic-backlight.c | 143 ++++++++++++++++++++++++++++
|
|
drivers/hid/apple-touchbar.c | 23 +++--
|
|
5 files changed, 181 insertions(+), 6 deletions(-)
|
|
create mode 100644 drivers/hid/apple-magic-backlight.c
|
|
|
|
diff --git a/MAINTAINERS b/MAINTAINERS
|
|
index 468f39a37b33..5bdb5f456226 100644
|
|
--- a/MAINTAINERS
|
|
+++ b/MAINTAINERS
|
|
@@ -9192,6 +9192,12 @@ F: include/linux/pm.h
|
|
F: include/linux/suspend.h
|
|
F: kernel/power/
|
|
|
|
+HID APPLE MAGIC BACKLIGHT DRIVER
|
|
+M: Orlando Chamberlain <orlandoch.dev@gmail.com>
|
|
+L: linux-input@vger.kernel.org
|
|
+S: Maintained
|
|
+F: drivers/hid/apple-magic-backlight.c
|
|
+
|
|
HID CORE LAYER
|
|
M: Jiri Kosina <jikos@kernel.org>
|
|
M: Benjamin Tissoires <benjamin.tissoires@redhat.com>
|
|
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
|
|
index 9066e0fe458c..d555bf62e496 100644
|
|
--- a/drivers/hid/Kconfig
|
|
+++ b/drivers/hid/Kconfig
|
|
@@ -148,6 +148,7 @@ config HID_APPLE_IBRIDGE
|
|
|
|
config HID_APPLE_TOUCHBAR
|
|
tristate "Apple Touch Bar"
|
|
+ depends on USB_HID
|
|
help
|
|
Say Y here if you want support for the Touch Bar on recent
|
|
MacBook Pros.
|
|
@@ -155,6 +156,19 @@ config HID_APPLE_TOUCHBAR
|
|
To compile this driver as a module, choose M here: the
|
|
module will be called apple-touchbar.
|
|
|
|
+config HID_APPLE_MAGIC_BACKLIGHT
|
|
+ tristate "Apple Magic Keyboard Backlight"
|
|
+ depends on USB_HID
|
|
+ depends on LEDS_CLASS
|
|
+ depends on NEW_LEDS
|
|
+ help
|
|
+ Say Y here if you want support for the keyboard backlight on Macs with
|
|
+ the magic keyboard (MacBookPro16,x and MacBookAir9,1). Note that this
|
|
+ driver is not for external magic keyboards.
|
|
+
|
|
+ To compile this driver as a module, choose M here: the
|
|
+ module will be called apple-magic-backlight.
|
|
+
|
|
config HID_APPLEIR
|
|
tristate "Apple infrared receiver"
|
|
depends on (USB_HID)
|
|
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
|
|
index c792e42fe43f..a961914ec399 100644
|
|
--- a/drivers/hid/Makefile
|
|
+++ b/drivers/hid/Makefile
|
|
@@ -28,6 +28,7 @@ obj-$(CONFIG_HID_ACRUX) += hid-axff.o
|
|
obj-$(CONFIG_HID_APPLE) += hid-apple.o
|
|
obj-$(CONFIG_HID_APPLE_IBRIDGE) += apple-ibridge.o
|
|
obj-$(CONFIG_HID_APPLE_TOUCHBAR) += apple-touchbar.o
|
|
+obj-$(CONFIG_HID_APPLE_MAGIC_BACKLIGHT) += apple-magic-backlight.o
|
|
obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o
|
|
obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o
|
|
obj-$(CONFIG_HID_ASUS) += hid-asus.o
|
|
diff --git a/drivers/hid/apple-magic-backlight.c b/drivers/hid/apple-magic-backlight.c
|
|
new file mode 100644
|
|
index 000000000000..c7f1e5e90ab7
|
|
--- /dev/null
|
|
+++ b/drivers/hid/apple-magic-backlight.c
|
|
@@ -0,0 +1,143 @@
|
|
+// SPDX-License-Identifier: GPL-2.0
|
|
+/*
|
|
+ * Apple Magic Keyboard Backlight Driver
|
|
+ *
|
|
+ * For Intel Macs with internal Magic Keyboard (MacBookPro16,1-4 and MacBookAir9,1)
|
|
+ *
|
|
+ * Copyright (c) 2022 Kerem Karabay <kekrby@gmail.com>
|
|
+ * Copyright (c) 2023 Orlando Chamberlain <orlandoch.dev@gmail.com>
|
|
+ */
|
|
+
|
|
+#include <linux/hid.h>
|
|
+#include <linux/usb.h>
|
|
+
|
|
+#include "hid-ids.h"
|
|
+
|
|
+#define USAGE_MAGIC_BL 0xff00000f
|
|
+
|
|
+#define APPLE_MAGIC_REPORT_ID_POWER 3
|
|
+#define APPLE_MAGIC_REPORT_ID_BRIGHTNESS 1
|
|
+
|
|
+struct apple_magic_backlight {
|
|
+ struct led_classdev cdev;
|
|
+ struct hid_device *hdev;
|
|
+ struct hid_report *brightness;
|
|
+ struct hid_report *power;
|
|
+};
|
|
+
|
|
+static void apple_magic_backlight_power_set(struct apple_magic_backlight *backlight,
|
|
+ char power, char rate)
|
|
+{
|
|
+ struct hid_report *rep = backlight->power;
|
|
+
|
|
+ rep->field[0]->value[0] = power ? 1 : 0;
|
|
+ rep->field[1]->value[0] = 0x5e; /* Mimic Windows */
|
|
+ rep->field[1]->value[0] |= rate << 8;
|
|
+
|
|
+ hid_hw_request(backlight->hdev, backlight->power, HID_REQ_SET_REPORT);
|
|
+}
|
|
+
|
|
+static void apple_magic_backlight_brightness_set(struct apple_magic_backlight *backlight,
|
|
+ int brightness, char rate)
|
|
+{
|
|
+ struct hid_report *rep = backlight->brightness;
|
|
+
|
|
+ rep->field[0]->value[0] = brightness;
|
|
+ rep->field[1]->value[0] = 0x5e; /* Mimic Windows */
|
|
+ rep->field[1]->value[0] |= rate << 8;
|
|
+
|
|
+ hid_hw_request(backlight->hdev, backlight->brightness, HID_REQ_SET_REPORT);
|
|
+}
|
|
+
|
|
+static void apple_magic_backlight_set(struct apple_magic_backlight *backlight,
|
|
+ int brightness, char rate)
|
|
+{
|
|
+ apple_magic_backlight_power_set(backlight, brightness, rate);
|
|
+ if (brightness)
|
|
+ apple_magic_backlight_brightness_set(backlight, brightness, rate);
|
|
+}
|
|
+
|
|
+static int apple_magic_backlight_led_set(struct led_classdev *led_cdev,
|
|
+ enum led_brightness brightness)
|
|
+{
|
|
+ struct apple_magic_backlight *backlight = container_of(led_cdev,
|
|
+ struct apple_magic_backlight, cdev);
|
|
+
|
|
+ apple_magic_backlight_set(backlight, brightness, 1);
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static int apple_magic_backlight_probe(struct hid_device *hdev,
|
|
+ const struct hid_device_id *id)
|
|
+{
|
|
+ struct apple_magic_backlight *backlight;
|
|
+ int rc;
|
|
+
|
|
+ rc = hid_parse(hdev);
|
|
+ if (rc)
|
|
+ return rc;
|
|
+
|
|
+ /* Ensure this usb endpoint is for the keyboard backlight, not touchbar
|
|
+ * backlight.
|
|
+ */
|
|
+ if (!(hdev->collection && hdev->collection->usage == USAGE_MAGIC_BL))
|
|
+ return -ENODEV;
|
|
+
|
|
+ backlight = devm_kzalloc(&hdev->dev, sizeof(*backlight), GFP_KERNEL);
|
|
+
|
|
+ if (!backlight)
|
|
+ return -ENOMEM;
|
|
+
|
|
+ hid_set_drvdata(hdev, backlight);
|
|
+
|
|
+ rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
|
|
+ if (rc)
|
|
+ return rc;
|
|
+
|
|
+ backlight->brightness = hid_register_report(hdev, HID_FEATURE_REPORT,
|
|
+ APPLE_MAGIC_REPORT_ID_BRIGHTNESS, 0);
|
|
+ backlight->power = hid_register_report(hdev, HID_FEATURE_REPORT,
|
|
+ APPLE_MAGIC_REPORT_ID_POWER, 0);
|
|
+
|
|
+ if (!backlight->brightness || !backlight->power) {
|
|
+ rc = -ENODEV;
|
|
+ goto hw_stop;
|
|
+ }
|
|
+
|
|
+ backlight->hdev = hdev;
|
|
+ backlight->cdev.name = "apple::kbd_backlight";
|
|
+ backlight->cdev.max_brightness = backlight->brightness->field[0]->logical_maximum;
|
|
+ backlight->cdev.brightness_set_blocking = apple_magic_backlight_led_set;
|
|
+
|
|
+ apple_magic_backlight_set(backlight, 0, 0);
|
|
+
|
|
+ return devm_led_classdev_register(&hdev->dev, &backlight->cdev);
|
|
+
|
|
+hw_stop:
|
|
+ hid_hw_stop(hdev);
|
|
+ return rc;
|
|
+}
|
|
+
|
|
+static void apple_magic_backlight_remove(struct hid_device *hdev)
|
|
+{
|
|
+ hid_hw_stop(hdev);
|
|
+}
|
|
+
|
|
+static const struct hid_device_id apple_magic_backlight_hid_ids[] = {
|
|
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
|
|
+ { }
|
|
+};
|
|
+
|
|
+static struct hid_driver apple_magic_backlight_hid_driver = {
|
|
+ .name = "apple-magic-backlight",
|
|
+ .id_table = apple_magic_backlight_hid_ids,
|
|
+ .probe = apple_magic_backlight_probe,
|
|
+ .remove = apple_magic_backlight_remove,
|
|
+};
|
|
+
|
|
+module_hid_driver(apple_magic_backlight_hid_driver);
|
|
+
|
|
+MODULE_DESCRIPTION("MacBook Magic Keyboard Backlight");
|
|
+MODULE_AUTHOR("Orlando Chamberlain <orlandoch.dev@gmail.com>");
|
|
+MODULE_LICENSE("GPL");
|
|
+MODULE_DEVICE_TABLE(hid, apple_magic_backlight_hid_ids);
|
|
diff --git a/drivers/hid/apple-touchbar.c b/drivers/hid/apple-touchbar.c
|
|
index 40c12886e651..7de495b1c7f1 100644
|
|
--- a/drivers/hid/apple-touchbar.c
|
|
+++ b/drivers/hid/apple-touchbar.c
|
|
@@ -1222,6 +1222,23 @@ static int appletb_probe(struct hid_device *hdev,
|
|
unsigned long flags;
|
|
int rc;
|
|
|
|
+ /* initialize the report info */
|
|
+ rc = hid_parse(hdev);
|
|
+ if (rc) {
|
|
+ dev_err(tb_dev->log_dev, "tb: hid parse failed (%d)\n", rc);
|
|
+ goto error;
|
|
+ }
|
|
+
|
|
+ /* Ensure this usb endpoint is for the touchbar backlight, not keyboard
|
|
+ * backlight.
|
|
+ */
|
|
+
|
|
+ if ((hdev->product == USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) &&
|
|
+ !(hdev->collection && hdev->collection->usage ==
|
|
+ HID_USAGE_APPLE_APP)) {
|
|
+ return -ENODEV;
|
|
+ }
|
|
+
|
|
spin_lock_irqsave(&tb_dev->tb_lock, flags);
|
|
|
|
if (!tb_dev->log_dev)
|
|
@@ -1231,12 +1248,6 @@ static int appletb_probe(struct hid_device *hdev,
|
|
|
|
hid_set_drvdata(hdev, tb_dev);
|
|
|
|
- /* initialize the report info */
|
|
- rc = hid_parse(hdev);
|
|
- if (rc) {
|
|
- dev_err(tb_dev->log_dev, "als: hid parse failed (%d)\n", rc);
|
|
- goto error;
|
|
- }
|
|
|
|
rc = appletb_extract_report_and_iface_info(tb_dev, hdev, id);
|
|
if (rc < 0)
|
|
--
|
|
2.39.1
|
|
|