Skip to contentSkip to conversation

Apple Magic Keyboard Backlight

PATCH Superseded Orlando Chamberlain 3 participants 6 messages Feb 17, 2023 – Feb 18, 2023

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Apple Magic Keyboard Backlight

This patchseries adds support for the internal keyboard backlight of Macs with Apple's "Magic" keyboard (MacBookPro16,* and MacBookAir9,1), and also documents what names should be used for keyboard backlight leds in Documentation/leds/well-known-leds.txt.

v2->v3:
- remove unneeded header inclusion
- use s32 for report value type
- remove unneeded null check
- don't set drvdata as its never used
- prepend "hid-" to module name

v1->v2:
- drop unneeded remove function
- combine set functions
- add missing header inclusions
- avoid char as argument in favour of u8
- handful of style/formatting fixes
- use standard led name ":white:kbd_backlight"
- rename USAGE_MAGIC_BL to HID_USAGE_MAGIC_BL
- New patch documenting preferred keyboard backlight names

v1: https://lore.kernel.org/linux-input/[email protected]/
v2: https://lore.kernel.org/linux-input/[email protected]/

Orlando Chamberlain (2):
Documentation: leds: standardise keyboard backlight led names
HID: hid-apple-magic-backlight: Add driver for keyboard backlight on
internal Magic Keyboards

 Documentation/leds/well-known-leds.txt  |   8 ++
 MAINTAINERS                             |   6 ++
 drivers/hid/Kconfig                     |  13 +++
 drivers/hid/Makefile                    |   1 +
 drivers/hid/hid-apple-magic-backlight.c | 122 ++++++++++++++++++++++++
 5 files changed, 150 insertions(+)
 create mode 100644 drivers/hid/hid-apple-magic-backlight.c
Signature · 1 line
2.39.1
#1 lore raw

PATCH v3 1/2 Documentation: leds: standardise keyboard backlight led names

Advice use of either "input*:*:kbd_backlight" or ":*:kbd_backlight". We don't want people using vendor or product name (e.g. "smc", "apple", "asus") as this information is available from sysfs anyway, and it made the folder names inconsistent.

---
 Documentation/leds/well-known-leds.txt | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/leds/well-known-leds.txt +8 -0
index 2160382c86be..4e5429fce4d8 100644
@@ -44,6 +44,14 @@ Legacy: "lp5523:kb{1,2,3,4,5,6}" (Nokia N900)
 
 Frontlight/backlight of main keyboard.
 
+Good: ":*:kbd_backlight"
+Good: "input*:*:kbd_backlight"
+Legacy: "*:*:kbd_backlight"
+
+Many drivers have the vendor or product name as the first field of the led name,
+this makes names inconsistent and is redundant as that information is already in
+sysfs.
+
 Legacy: "button-backlight" (Motorola Droid 4)
 
 Some phones have touch buttons below screen; it is different from main
Signature · 1 line
2.39.1
#2 lore raw

PATCH v3 2/2 HID: hid-apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards

In reply to Orlando Chamberlain

This driver adds support for the keyboard backlight on Intel T2 Macs with internal Magic Keyboards (MacBookPro16,x and MacBookAir9,1)

---
v2->v3:
- remove unneeded inclusion
- use s32 for report value type
- remove unneeded null check
- don't set drvdata as its never used
- prepend "hid-" to module name
v1->v2:
- drop unneeded remove function
- combine set functions
- add missing header inclusions
- avoid char as argument in favour of u8
- handful of style/formatting fixes
- use standard led name ":white:kbd_backlight"
- rename USAGE_MAGIC_BL to HID_USAGE_MAGIC_BL

 MAINTAINERS                             |   6 ++
 drivers/hid/Kconfig                     |  13 +++
 drivers/hid/Makefile                    |   1 +
 drivers/hid/hid-apple-magic-backlight.c | 122 ++++++++++++++++++++++++
 4 files changed, 142 insertions(+)
 create mode 100644 drivers/hid/hid-apple-magic-backlight.c
MAINTAINERS +6 -0
index fb1471cb5ed3..3319f0c3ed1e 100644
@@ -9201,6 +9201,12 @@ F:	include/linux/pm.h
 F:	include/linux/suspend.h
 F:	kernel/power/
 
+HID APPLE MAGIC BACKLIGHT DRIVER
+M:	Orlando Chamberlain <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/hid/apple-magic-backlight.c
+
 HID CORE LAYER
 M:	Jiri Kosina <[email protected]>
 M:	Benjamin Tissoires <[email protected]>
drivers/hid/Kconfig +13 -0
index e2a5d30c8895..fe489632bfd9 100644
@@ -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.
 
+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 hid-apple-magic-backlight.
+
 config HID_APPLEIR
 	tristate "Apple infrared receiver"
 	depends on (USB_HID)
drivers/hid/Makefile +1 -0
index e8014c1a2f8b..dc8df002bc86 100644
@@ -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
 obj-$(CONFIG_HID_APPLE)		+= hid-apple.o
+obj-$(CONFIG_HID_APPLE_MAGIC_BACKLIGHT)	+= hid-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
drivers/hid/hid-apple-magic-backlight.c new file +122 -0

122 lines not on this page · raw message

Signature · 1 line
2.39.1
Reviewed-by on v3 2/2
#3 lore raw

Re: [PATCH v3 2/2] HID: hid-apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards

Quoted 3 lines · Orlando Chamberlain

This driver adds support for the keyboard backlight on Intel T2 Macs with internal Magic Keyboards (MacBookPro16,x and MacBookAir9,1)

LGTM,

Quoted 209 lines

---
v2->v3:
- remove unneeded inclusion
- use s32 for report value type
- remove unneeded null check
- don't set drvdata as its never used
- prepend "hid-" to module name
v1->v2:
- drop unneeded remove function
- combine set functions
- add missing header inclusions
- avoid char as argument in favour of u8
- handful of style/formatting fixes
- use standard led name ":white:kbd_backlight"
- rename USAGE_MAGIC_BL to HID_USAGE_MAGIC_BL

 MAINTAINERS                             |   6 ++
 drivers/hid/Kconfig                     |  13 +++
 drivers/hid/Makefile                    |   1 +
 drivers/hid/hid-apple-magic-backlight.c | 122 ++++++++++++++++++++++++
 4 files changed, 142 insertions(+)
 create mode 100644 drivers/hid/hid-apple-magic-backlight.c
MAINTAINERS +6 -0
index fb1471cb5ed3..3319f0c3ed1e 100644
@@ -9201,6 +9201,12 @@ F:       include/linux/pm.h
 F:     include/linux/suspend.h
 F:     kernel/power/
 
+HID APPLE MAGIC BACKLIGHT DRIVER
+M:     Orlando Chamberlain <[email protected]>
+L:     [email protected]
+S:     Maintained
+F:     drivers/hid/apple-magic-backlight.c
+
 HID CORE LAYER
 M:     Jiri Kosina <[email protected]>
 M:     Benjamin Tissoires <[email protected]>
drivers/hid/Kconfig +13 -0
index e2a5d30c8895..fe489632bfd9 100644
@@ -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.
 
+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 hid-apple-magic-backlight.
+
 config HID_APPLEIR
        tristate "Apple infrared receiver"
        depends on (USB_HID)
drivers/hid/Makefile +1 -0
index e8014c1a2f8b..dc8df002bc86 100644
@@ -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
 obj-$(CONFIG_HID_APPLE)                += hid-apple.o
+obj-$(CONFIG_HID_APPLE_MAGIC_BACKLIGHT)        += hid-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
drivers/hid/hid-apple-magic-backlight.c new file +122 -1
new file mode 100644
index 000000000000..210af2849e78
@@ -0,0 +1,122 @@
+// 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 <[email protected]>
+ * Copyright (c) 2023 Orlando Chamberlain <[email protected]>
+ */
+
+#include <linux/hid.h>
+#include <linux/leds.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <dt-bindings/leds/common.h>
+
+#include "hid-ids.h"
+
+#define HID_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_report_set(struct hid_report *rep, s32 value, u8 rate)
+{
+       rep->field[0]->value[0] = value;
+       rep->field[1]->value[0] = 0x5e; /* Mimic Windows */
+       rep->field[1]->value[0] |= rate << 8;
+
+       hid_hw_request(rep->device, rep, HID_REQ_SET_REPORT);
+}
+
+static void apple_magic_backlight_set(struct apple_magic_backlight *backlight,
+                                    int brightness, char rate)
+{
+       apple_magic_backlight_report_set(backlight->power, brightness ? 1 : 0, rate);
+       if (brightness)
+               apple_magic_backlight_report_set(backlight->brightness, 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[0].usage != HID_USAGE_MAGIC_BL)
+               return -ENODEV;
+
+       backlight = devm_kzalloc(&hdev->dev, sizeof(*backlight), GFP_KERNEL);
+       if (!backlight)
+               return -ENOMEM;
+
+       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 = ":white:" LED_FUNCTION_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 const struct hid_device_id apple_magic_backlight_hid_ids[] = {
+       { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
+       { }
+};
+MODULE_DEVICE_TABLE(hid, apple_magic_backlight_hid_ids);
+
+static struct hid_driver apple_magic_backlight_hid_driver = {
+       .name = "hid-apple-magic-backlight",
+       .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");
+MODULE_AUTHOR("Orlando Chamberlain <[email protected]>");
+MODULE_LICENSE("GPL");
--

2.39.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v3 2/2
#4 lore raw

Re: [PATCH v3 2/2] HID: hid-apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards

In reply to Orlando Chamberlain
Quoted 2 lines · Orlando Chamberlain

This driver adds support for the keyboard backlight on Intel T2 Macs with internal Magic Keyboards (MacBookPro16,x and MacBookAir9,1)

One other tiny nitpick below, if it goes to v4.

Quoted 100 lines

---
v2->v3:
- remove unneeded inclusion
- use s32 for report value type
- remove unneeded null check
- don't set drvdata as its never used
- prepend "hid-" to module name
v1->v2:
- drop unneeded remove function
- combine set functions
- add missing header inclusions
- avoid char as argument in favour of u8
- handful of style/formatting fixes
- use standard led name ":white:kbd_backlight"
- rename USAGE_MAGIC_BL to HID_USAGE_MAGIC_BL

 MAINTAINERS                             |   6 ++
 drivers/hid/Kconfig                     |  13 +++
 drivers/hid/Makefile                    |   1 +
 drivers/hid/hid-apple-magic-backlight.c | 122 ++++++++++++++++++++++++
 4 files changed, 142 insertions(+)
 create mode 100644 drivers/hid/hid-apple-magic-backlight.c
MAINTAINERS +6 -0
index fb1471cb5ed3..3319f0c3ed1e 100644
@@ -9201,6 +9201,12 @@ F:	include/linux/pm.h
 F:	include/linux/suspend.h
 F:	kernel/power/
 
+HID APPLE MAGIC BACKLIGHT DRIVER
+M:	Orlando Chamberlain <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/hid/apple-magic-backlight.c
+
 HID CORE LAYER
 M:	Jiri Kosina <[email protected]>
 M:	Benjamin Tissoires <[email protected]>
drivers/hid/Kconfig +13 -0
index e2a5d30c8895..fe489632bfd9 100644
@@ -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.
 
+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 hid-apple-magic-backlight.
+
 config HID_APPLEIR
 	tristate "Apple infrared receiver"
 	depends on (USB_HID)
drivers/hid/Makefile +1 -0
index e8014c1a2f8b..dc8df002bc86 100644
@@ -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
 obj-$(CONFIG_HID_APPLE)		+= hid-apple.o
+obj-$(CONFIG_HID_APPLE_MAGIC_BACKLIGHT)	+= hid-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
drivers/hid/hid-apple-magic-backlight.c new file +16 -0
new file mode 100644
index 000000000000..210af2849e78
@@ -0,0 +1,122 @@
+// 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 <[email protected]>
+ * Copyright (c) 2023 Orlando Chamberlain <[email protected]>
+ */
+
+#include <linux/hid.h>
+#include <linux/leds.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <dt-bindings/leds/common.h>
+

drivers/hid/hid-apple-magic-backlight.c line 26

Quoted 10 lines
drivers/hid/hid-apple-magic-backlight.c new file +10 -0
+#include "hid-ids.h"
+
+#define HID_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;

hdev is not used anymore.

Quoted 99 lines
drivers/hid/hid-apple-magic-backlight.c new file +96 -0
+	struct hid_report *brightness;
+	struct hid_report *power;
+};
+
+static void apple_magic_backlight_report_set(struct hid_report *rep, s32 value, u8 rate)
+{
+	rep->field[0]->value[0] = value;
+	rep->field[1]->value[0] = 0x5e; /* Mimic Windows */
+	rep->field[1]->value[0] |= rate << 8;
+
+	hid_hw_request(rep->device, rep, HID_REQ_SET_REPORT);
+}
+
+static void apple_magic_backlight_set(struct apple_magic_backlight *backlight,
+				     int brightness, char rate)
+{
+	apple_magic_backlight_report_set(backlight->power, brightness ? 1 : 0, rate);
+	if (brightness)
+		apple_magic_backlight_report_set(backlight->brightness, 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[0].usage != HID_USAGE_MAGIC_BL)
+		return -ENODEV;
+
+	backlight = devm_kzalloc(&hdev->dev, sizeof(*backlight), GFP_KERNEL);
+	if (!backlight)
+		return -ENOMEM;
+
+	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 = ":white:" LED_FUNCTION_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 const struct hid_device_id apple_magic_backlight_hid_ids[] = {
+	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },
+	{ }
+};
+MODULE_DEVICE_TABLE(hid, apple_magic_backlight_hid_ids);
+
+static struct hid_driver apple_magic_backlight_hid_driver = {
+	.name = "hid-apple-magic-backlight",
+	.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");
+MODULE_AUTHOR("Orlando Chamberlain <[email protected]>");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.39.1
#5 lore raw

Re: [PATCH v3 2/2] HID: hid-apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards

Quoted 7 lines · On Fri, 17 Feb 2023 14:13:59 +0000 Thomas Weißschuh <[email protected]> wrote:
Quoted 2 lines · Orlando Chamberlain

This driver adds support for the keyboard backlight on Intel T2 Macs with internal Magic Keyboards (MacBookPro16,x and MacBookAir9,1)

One other tiny nitpick below, if it goes to v4.

Thanks, I'll send a v4 with that fixed.

Quoted 228 lines
Quoted 104 lines

---
v2->v3:
- remove unneeded inclusion
- use s32 for report value type
- remove unneeded null check
- don't set drvdata as its never used
- prepend "hid-" to module name
v1->v2:
- drop unneeded remove function
- combine set functions
- add missing header inclusions
- avoid char as argument in favour of u8
- handful of style/formatting fixes
- use standard led name ":white:kbd_backlight"
- rename USAGE_MAGIC_BL to HID_USAGE_MAGIC_BL

 MAINTAINERS                             |   6 ++
 drivers/hid/Kconfig                     |  13 +++
 drivers/hid/Makefile                    |   1 +
 drivers/hid/hid-apple-magic-backlight.c | 122
++++++++++++++++++++++++ 4 files changed, 142 insertions(+)
 create mode 100644 drivers/hid/hid-apple-magic-backlight.c
MAINTAINERS +6 -0
index fb1471cb5ed3..3319f0c3ed1e 100644
@@ -9201,6 +9201,12 @@ F:	include/linux/pm.h
 F:	include/linux/suspend.h
 F:	kernel/power/
 
+HID APPLE MAGIC BACKLIGHT DRIVER
+M:	Orlando Chamberlain <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/hid/apple-magic-backlight.c
+
 HID CORE LAYER
 M:	Jiri Kosina <[email protected]>
 M:	Benjamin Tissoires <[email protected]>
drivers/hid/Kconfig +0 -0
index e2a5d30c8895..fe489632bfd9 100644
@@ -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.

drivers/hid/Kconfig +7 -0
+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

drivers/hid/Kconfig +1 -0
+	the magic keyboard (MacBookPro16,x and MacBookAir9,1).

Note that this

drivers/hid/Kconfig +5 -0
+	driver is not for external magic keyboards.
+
+	To compile this driver as a module, choose M here: the
+	module will be called hid-apple-magic-backlight.
+
 config HID_APPLEIR
 	tristate "Apple infrared receiver"
 	depends on (USB_HID)
drivers/hid/Makefile +0 -0
index e8014c1a2f8b..dc8df002bc86 100644
@@ -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
obj-$(CONFIG_HID_APPLE) += hid-apple.o
+obj-$(CONFIG_HID_APPLE_MAGIC_BACKLIGHT) +=
hid-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/hid-apple-magic-backlight.c b/drivers/hid/hid-apple-magic-backlight.c new file mode 100644 index 000000000000..210af2849e78

drivers/hid/hid-apple-magic-backlight.c new file +5 -0
@@ -0,0 +1,122 @@
+// 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 <[email protected]>
+ * Copyright (c) 2023 Orlando Chamberlain <[email protected]>
+ */

drivers/hid/hid-apple-magic-backlight.c new file +7 -0
+
+#include <linux/hid.h>
+#include <linux/leds.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <dt-bindings/leds/common.h>
+

drivers/hid/hid-apple-magic-backlight.c line 26

Quoted 10 lines
drivers/hid/hid-apple-magic-backlight.c new file +10 -0
+#include "hid-ids.h"
+
+#define HID_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;  

hdev is not used anymore.

Quoted 111 lines
drivers/hid/hid-apple-magic-backlight.c new file +5 -0
+	struct hid_report *brightness;
+	struct hid_report *power;
+};
+
+static void apple_magic_backlight_report_set(struct hid_report

*rep, s32 value, u8 rate) +{

drivers/hid/hid-apple-magic-backlight.c new file +8 -0
+	rep->field[0]->value[0] = value;
+	rep->field[1]->value[0] = 0x5e; /* Mimic Windows */
+	rep->field[1]->value[0] |= rate << 8;
+
+	hid_hw_request(rep->device, rep, HID_REQ_SET_REPORT);
+}
+
+static void apple_magic_backlight_set(struct apple_magic_backlight

*backlight,

drivers/hid/hid-apple-magic-backlight.c new file +3 -0
+				     int brightness, char rate)
+{
+	apple_magic_backlight_report_set(backlight->power,

brightness ? 1 : 0, rate);

drivers/hid/hid-apple-magic-backlight.c new file +2 -0
+	if (brightness)
+

apple_magic_backlight_report_set(backlight->brightness, brightness, rate); +} +
+static int apple_magic_backlight_led_set(struct led_classdev *led_cdev,

drivers/hid/hid-apple-magic-backlight.c new file +1 -0
+					 enum led_brightness

brightness) +{

drivers/hid/hid-apple-magic-backlight.c new file +1 -0
+	struct apple_magic_backlight *backlight =

container_of(led_cdev,

drivers/hid/hid-apple-magic-backlight.c new file +8 -0
+			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) +{

drivers/hid/hid-apple-magic-backlight.c new file +9 -0
+	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

drivers/hid/hid-apple-magic-backlight.c new file +6 -0
+	 * backlight.
+	 */
+	if (hdev->collection[0].usage != HID_USAGE_MAGIC_BL)
+		return -ENODEV;
+
+	backlight = devm_kzalloc(&hdev->dev, sizeof(*backlight),

GFP_KERNEL);

drivers/hid/hid-apple-magic-backlight.c new file +8 -0
+	if (!backlight)
+		return -ENOMEM;
+
+	rc = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+	if (rc)
+		return rc;
+
+	backlight->brightness = hid_register_report(hdev,

HID_FEATURE_REPORT,

drivers/hid/hid-apple-magic-backlight.c new file +2 -0
+			APPLE_MAGIC_REPORT_ID_BRIGHTNESS, 0);
+	backlight->power = hid_register_report(hdev,

HID_FEATURE_REPORT,

drivers/hid/hid-apple-magic-backlight.c new file +9 -0
+			APPLE_MAGIC_REPORT_ID_POWER, 0);
+
+	if (!backlight->brightness || !backlight->power) {
+		rc = -ENODEV;
+		goto hw_stop;
+	}
+
+	backlight->hdev = hdev;
+	backlight->cdev.name = ":white:"

LED_FUNCTION_KBD_BACKLIGHT;

drivers/hid/hid-apple-magic-backlight.c new file +1 -0
+	backlight->cdev.max_brightness =

backlight->brightness->field[0]->logical_maximum;

drivers/hid/hid-apple-magic-backlight.c new file +1 -0
+	backlight->cdev.brightness_set_blocking =

apple_magic_backlight_led_set; +

drivers/hid/hid-apple-magic-backlight.c new file +3 -0
+	apple_magic_backlight_set(backlight, 0, 0);
+
+	return devm_led_classdev_register(&hdev->dev,

&backlight->cdev); +

drivers/hid/hid-apple-magic-backlight.c new file +6 -0
+hw_stop:
+	hid_hw_stop(hdev);
+	return rc;
+}
+
+static const struct hid_device_id apple_magic_backlight_hid_ids[]

= {

drivers/hid/hid-apple-magic-backlight.c new file +1 -0
+	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE,

USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) },

drivers/hid/hid-apple-magic-backlight.c new file +14 -0
+	{ }
+};
+MODULE_DEVICE_TABLE(hid, apple_magic_backlight_hid_ids);
+
+static struct hid_driver apple_magic_backlight_hid_driver = {
+	.name = "hid-apple-magic-backlight",
+	.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");
+MODULE_AUTHOR("Orlando Chamberlain <[email protected]>");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.39.1