Files
linux-t2-patches/1006-HID-hid-apple-magic-backlight-Add-driver-for-keyboar.patch
T

208 lines
6.6 KiB
Diff
Raw Normal View History

2023-02-18 20:33:10 +11:00
From c124f5401040d02abd6d349979be29acd1e88545 Mon Sep 17 00:00:00 2001
2023-01-28 15:50:49 +05:30
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
2023-02-18 20:33:10 +11:00
Date: Fri, 10 Feb 2023 23:14:31 +1100
Subject: [PATCH 6/8] HID: hid-apple-magic-backlight: Add driver for keyboard
2023-02-09 21:03:55 +05:30
backlight on internal Magic Keyboards
2023-02-18 20:33:10 +11:00
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
2022-11-16 19:19:28 +05:30
2023-02-09 21:03:55 +05:30
This driver adds support for the keyboard backlight on Intel T2 Macs
2023-01-28 15:50:49 +05:30
with internal Magic Keyboards (MacBookPro16,x and MacBookAir9,1)
2022-11-16 19:19:28 +05:30
2023-01-28 15:50:49 +05:30
Co-developed-by: Kerem Karabay <kekrby@gmail.com>
2022-11-16 19:19:28 +05:30
Signed-off-by: Kerem Karabay <kekrby@gmail.com>
2023-02-18 20:33:10 +11:00
Signed-off-by: Orlando Chamberlain <orlandoch.dev@gmail.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Reviewed-by: Thomas Weißschuh <linux@weissschuh.net>
2022-11-16 19:19:28 +05:30
---
2023-02-18 20:33:10 +11:00
MAINTAINERS | 6 ++
drivers/hid/Kconfig | 13 +++
drivers/hid/Makefile | 1 +
drivers/hid/hid-apple-magic-backlight.c | 120 ++++++++++++++++++++++++
4 files changed, 140 insertions(+)
create mode 100644 drivers/hid/hid-apple-magic-backlight.c
2022-11-16 19:19:28 +05:30
2023-01-28 15:50:49 +05:30
diff --git a/MAINTAINERS b/MAINTAINERS
2023-02-18 20:33:10 +11:00
index 6a47510d1592..b93c68158be3 100644
2023-01-28 15:50:49 +05:30
--- a/MAINTAINERS
+++ b/MAINTAINERS
2023-02-18 20:33:10 +11:00
@@ -9201,6 +9201,12 @@ F: include/linux/pm.h
2023-01-28 15:50:49 +05:30
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>
2022-11-16 19:19:28 +05:30
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
2023-02-18 20:33:10 +11:00
index e2a5d30c8895..fe489632bfd9 100644
2022-11-16 19:19:28 +05:30
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
2023-02-18 20:33:10 +11:00
@@ -130,6 +130,19 @@ config HID_APPLE
Say Y here if you want support for keyboards of Apple iBooks, PowerBooks,
MacBooks, MacBook Pros and Apple Aluminum.
2022-11-16 19:19:28 +05:30
+config HID_APPLE_MAGIC_BACKLIGHT
2023-01-28 15:50:49 +05:30
+ tristate "Apple Magic Keyboard Backlight"
+ depends on USB_HID
+ depends on LEDS_CLASS
+ depends on NEW_LEDS
2022-11-16 19:19:28 +05:30
+ 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
2023-02-18 20:33:10 +11:00
+ module will be called hid-apple-magic-backlight.
2022-11-16 19:19:28 +05:30
+
config HID_APPLEIR
tristate "Apple infrared receiver"
depends on (USB_HID)
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
2023-02-18 20:33:10 +11:00
index e8014c1a2f8b..dc8df002bc86 100644
2022-11-16 19:19:28 +05:30
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
2023-02-18 20:33:10 +11:00
@@ -26,6 +26,7 @@ obj-$(CONFIG_HID_ACCUTOUCH) += hid-accutouch.o
obj-$(CONFIG_HID_ALPS) += hid-alps.o
obj-$(CONFIG_HID_ACRUX) += hid-axff.o
2022-11-16 19:19:28 +05:30
obj-$(CONFIG_HID_APPLE) += hid-apple.o
2023-02-18 20:33:10 +11:00
+obj-$(CONFIG_HID_APPLE_MAGIC_BACKLIGHT) += hid-apple-magic-backlight.o
2022-11-16 19:19:28 +05:30
obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o
obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o
obj-$(CONFIG_HID_ASUS) += hid-asus.o
2023-02-18 20:33:10 +11:00
diff --git a/drivers/hid/hid-apple-magic-backlight.c b/drivers/hid/hid-apple-magic-backlight.c
2022-11-16 19:19:28 +05:30
new file mode 100644
2023-02-18 20:33:10 +11:00
index 000000000000..f0fc02ff3b2d
2022-11-16 19:19:28 +05:30
--- /dev/null
2023-02-18 20:33:10 +11:00
+++ b/drivers/hid/hid-apple-magic-backlight.c
@@ -0,0 +1,120 @@
2022-11-16 19:19:28 +05:30
+// SPDX-License-Identifier: GPL-2.0
+/*
2023-01-28 15:50:49 +05:30
+ * Apple Magic Keyboard Backlight Driver
+ *
+ * For Intel Macs with internal Magic Keyboard (MacBookPro16,1-4 and MacBookAir9,1)
2022-11-16 19:19:28 +05:30
+ *
+ * Copyright (c) 2022 Kerem Karabay <kekrby@gmail.com>
2023-01-28 15:50:49 +05:30
+ * Copyright (c) 2023 Orlando Chamberlain <orlandoch.dev@gmail.com>
2022-11-16 19:19:28 +05:30
+ */
+
+#include <linux/hid.h>
2023-02-18 20:33:10 +11:00
+#include <linux/leds.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <dt-bindings/leds/common.h>
2022-11-16 19:19:28 +05:30
+
+#include "hid-ids.h"
+
2023-02-18 20:33:10 +11:00
+#define HID_USAGE_MAGIC_BL 0xff00000f
2023-01-28 15:50:49 +05:30
+
+#define APPLE_MAGIC_REPORT_ID_POWER 3
+#define APPLE_MAGIC_REPORT_ID_BRIGHTNESS 1
2022-11-16 19:19:28 +05:30
+
+struct apple_magic_backlight {
+ struct led_classdev cdev;
2023-01-28 15:50:49 +05:30
+ struct hid_report *brightness;
+ struct hid_report *power;
2022-11-16 19:19:28 +05:30
+};
+
2023-02-18 20:33:10 +11:00
+static void apple_magic_backlight_report_set(struct hid_report *rep, s32 value, u8 rate)
2022-11-16 19:19:28 +05:30
+{
2023-02-18 20:33:10 +11:00
+ rep->field[0]->value[0] = value;
2023-01-28 15:50:49 +05:30
+ rep->field[1]->value[0] = 0x5e; /* Mimic Windows */
+ rep->field[1]->value[0] |= rate << 8;
2022-11-16 19:19:28 +05:30
+
2023-02-18 20:33:10 +11:00
+ hid_hw_request(rep->device, rep, HID_REQ_SET_REPORT);
2022-11-16 19:19:28 +05:30
+}
+
2023-01-28 15:50:49 +05:30
+static void apple_magic_backlight_set(struct apple_magic_backlight *backlight,
+ int brightness, char rate)
2022-11-16 19:19:28 +05:30
+{
2023-02-18 20:33:10 +11:00
+ apple_magic_backlight_report_set(backlight->power, brightness ? 1 : 0, rate);
2023-01-28 15:50:49 +05:30
+ if (brightness)
2023-02-18 20:33:10 +11:00
+ apple_magic_backlight_report_set(backlight->brightness, brightness, rate);
2022-11-16 19:19:28 +05:30
+}
+
+static int apple_magic_backlight_led_set(struct led_classdev *led_cdev,
2022-11-18 10:27:51 +03:00
+ enum led_brightness brightness)
2022-11-16 19:19:28 +05:30
+{
+ struct apple_magic_backlight *backlight = container_of(led_cdev,
+ struct apple_magic_backlight, cdev);
+
2023-01-28 15:50:49 +05:30
+ apple_magic_backlight_set(backlight, brightness, 1);
+ return 0;
2022-11-16 19:19:28 +05:30
+}
+
+static int apple_magic_backlight_probe(struct hid_device *hdev,
+ const struct hid_device_id *id)
+{
+ struct apple_magic_backlight *backlight;
2023-01-28 15:50:49 +05:30
+ int rc;
2022-11-16 19:19:28 +05:30
+
2023-01-28 15:50:49 +05:30
+ rc = hid_parse(hdev);
+ if (rc)
+ return rc;
+
2023-02-18 20:33:10 +11:00
+ /*
+ * Ensure this usb endpoint is for the keyboard backlight, not touchbar
2023-01-28 15:50:49 +05:30
+ * backlight.
+ */
2023-02-18 20:33:10 +11:00
+ if (hdev->collection[0].usage != HID_USAGE_MAGIC_BL)
2022-11-16 19:19:28 +05:30
+ return -ENODEV;
+
+ backlight = devm_kzalloc(&hdev->dev, sizeof(*backlight), GFP_KERNEL);
2023-01-28 15:50:49 +05:30
+ if (!backlight)
2022-11-16 19:19:28 +05:30
+ return -ENOMEM;
2023-01-28 15:50:49 +05:30
+
+ 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;
2022-11-16 19:19:28 +05:30
+ }
+
2023-02-18 20:33:10 +11:00
+ backlight->cdev.name = ":white:" LED_FUNCTION_KBD_BACKLIGHT;
2023-01-28 15:50:49 +05:30
+ backlight->cdev.max_brightness = backlight->brightness->field[0]->logical_maximum;
2022-11-16 19:19:28 +05:30
+ backlight->cdev.brightness_set_blocking = apple_magic_backlight_led_set;
+
2023-01-28 15:50:49 +05:30
+ apple_magic_backlight_set(backlight, 0, 0);
2022-11-16 19:19:28 +05:30
+
+ return devm_led_classdev_register(&hdev->dev, &backlight->cdev);
2023-01-28 15:50:49 +05:30
+
+hw_stop:
+ hid_hw_stop(hdev);
+ return rc;
+}
+
2022-11-16 19:19:28 +05:30
+static const struct hid_device_id apple_magic_backlight_hid_ids[] = {
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
+ { }
+};
2023-02-09 21:03:55 +05:30
+MODULE_DEVICE_TABLE(hid, apple_magic_backlight_hid_ids);
2022-11-16 19:19:28 +05:30
+
+static struct hid_driver apple_magic_backlight_hid_driver = {
2023-02-18 20:33:10 +11:00
+ .name = "hid-apple-magic-backlight",
2022-11-16 19:19:28 +05:30
+ .id_table = apple_magic_backlight_hid_ids,
+ .probe = apple_magic_backlight_probe,
+};
+module_hid_driver(apple_magic_backlight_hid_driver);
+
+MODULE_DESCRIPTION("MacBook Magic Keyboard Backlight");
2023-01-28 15:50:49 +05:30
+MODULE_AUTHOR("Orlando Chamberlain <orlandoch.dev@gmail.com>");
2022-11-16 19:19:28 +05:30
+MODULE_LICENSE("GPL");
--
2023-02-18 20:33:10 +11:00
2.39.1
2022-11-16 19:19:28 +05:30