2022-01-24 19:28:02

by Aditya Garg

[permalink] [raw]
Subject: [PATCH v2 1/3] HID: apple: Add support for keyboard backlight on certain T2 Macs.

From: Paul Pawlowski <[email protected]>

This patch introduces the requisite plumbing for supporting keyboard
backlight on T2-attached, USB exposed models. The quirk mechanism was
used to reuse the existing hid-apple driver.

Signed-off-by: Paul Pawlowski <[email protected]>
Signed-off-by: Aun-Ali Zaidi <[email protected]>
Signed-off-by: Aditya Garg <[email protected]>
---
drivers/hid/hid-apple.c | 125 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 125 insertions(+)

diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index 24802a4a6..c22d445a9 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -7,6 +7,7 @@
* Copyright (c) 2005 Michael Haboustak <[email protected]> for Concept2, Inc
* Copyright (c) 2006-2007 Jiri Kosina
* Copyright (c) 2008 Jiri Slaby <[email protected]>
+ * Copyright (c) 2019 Paul Pawlowski <[email protected]>
*/

/*
@@ -33,6 +34,7 @@
/* BIT(7) reserved, was: APPLE_IGNORE_HIDINPUT */
#define APPLE_NUMLOCK_EMULATION BIT(8)
#define APPLE_RDESC_BATTERY BIT(9)
+#define APPLE_BACKLIGHT_CTL 0x0200

#define APPLE_FLAG_FKEY 0x01

@@ -61,6 +63,12 @@ MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. "
"(For people who want to keep PC keyboard muscle memory. "
"[0] = as-is, Mac layout, 1 = swapped, PC layout)");

+struct apple_sc_backlight {
+ struct led_classdev cdev;
+ struct hid_device *hdev;
+ unsigned short backlight_off, backlight_on_min, backlight_on_max;
+};
+
struct apple_sc {
struct hid_device *hdev;
unsigned long quirks;
@@ -68,6 +76,7 @@ struct apple_sc {
unsigned int fn_found;
DECLARE_BITMAP(pressed_numlock, KEY_CNT);
struct timer_list battery_timer;
+ struct apple_sc_backlight *backlight;
};

struct apple_key_translation {
@@ -76,6 +85,20 @@ struct apple_key_translation {
u8 flags;
};

+struct apple_backlight_config_report {
+ u8 report_id;
+ u8 version;
+ u16 backlight_off, backlight_on_min, backlight_on_max;
+};
+
+struct apple_backlight_set_report {
+ u8 report_id;
+ u8 version;
+ u16 backlight;
+ u16 rate;
+};
+
+
static const struct apple_key_translation apple2021_fn_keys[] = {
{ KEY_BACKSPACE, KEY_DELETE },
{ KEY_ENTER, KEY_INSERT },
@@ -530,6 +553,105 @@ static int apple_input_configured(struct hid_device *hdev,
return 0;
}

+static bool apple_backlight_check_support(struct hid_device *hdev)
+{
+ int i;
+ unsigned int hid;
+ struct hid_report *report;
+
+ list_for_each_entry(report, &hdev->report_enum[HID_INPUT_REPORT].report_list, list) {
+ for (i = 0; i < report->maxfield; i++) {
+ hid = report->field[i]->usage->hid;
+ if ((hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR && (hid & HID_USAGE) == 0xf)
+ return true;
+ }
+ }
+
+ return false;
+}
+
+static int apple_backlight_set(struct hid_device *hdev, u16 value, u16 rate)
+{
+ int ret = 0;
+ struct apple_backlight_set_report *rep;
+
+ rep = kmalloc(sizeof(*rep), GFP_KERNEL);
+ if (rep == NULL)
+ return -ENOMEM;
+
+ rep->report_id = 0xB0;
+ rep->version = 1;
+ rep->backlight = value;
+ rep->rate = rate;
+
+ ret = hid_hw_raw_request(hdev, 0xB0u, (u8 *) rep, sizeof(*rep),
+ HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
+
+ kfree(rep);
+ return ret;
+}
+
+static int apple_backlight_led_set(struct led_classdev *led_cdev,
+ enum led_brightness brightness)
+{
+ struct apple_sc_backlight *backlight = container_of(led_cdev,
+ struct apple_sc_backlight, cdev);
+
+ return apple_backlight_set(backlight->hdev, brightness, 0);
+}
+
+static int apple_backlight_init(struct hid_device *hdev)
+{
+ int ret;
+ struct apple_sc *asc = hid_get_drvdata(hdev);
+ struct apple_backlight_config_report *rep;
+
+ if (!apple_backlight_check_support(hdev))
+ return -EINVAL;
+
+ rep = kmalloc(0x200, GFP_KERNEL);
+ if (rep == NULL)
+ return -ENOMEM;
+
+ ret = hid_hw_raw_request(hdev, 0xBFu, (u8 *) rep, sizeof(*rep),
+ HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
+ if (ret < 0) {
+ hid_err(hdev, "backlight request failed: %d\n", ret);
+ goto cleanup_and_exit;
+ }
+ if (ret < 8 || rep->version != 1) {
+ hid_err(hdev, "backlight config struct: bad version %i\n", rep->version);
+ ret = -EINVAL;
+ goto cleanup_and_exit;
+ }
+
+ hid_dbg(hdev, "backlight config: off=%u, on_min=%u, on_max=%u\n",
+ rep->backlight_off, rep->backlight_on_min, rep->backlight_on_max);
+
+ asc->backlight = devm_kzalloc(&hdev->dev, sizeof(*asc->backlight), GFP_KERNEL);
+ if (!asc->backlight) {
+ ret = -ENOMEM;
+ goto cleanup_and_exit;
+ }
+
+ asc->backlight->hdev = hdev;
+ asc->backlight->cdev.name = "apple::kbd_backlight";
+ asc->backlight->cdev.max_brightness = rep->backlight_on_max;
+ asc->backlight->cdev.brightness_set_blocking = apple_backlight_led_set;
+
+ ret = apple_backlight_set(hdev, 0, 0);
+ if (ret < 0) {
+ hid_err(hdev, "backlight set request failed: %d\n", ret);
+ goto cleanup_and_exit;
+ }
+
+ ret = devm_led_classdev_register(&hdev->dev, &asc->backlight->cdev);
+
+cleanup_and_exit:
+ kfree(rep);
+ return ret;
+}
+
static int apple_probe(struct hid_device *hdev,
const struct hid_device_id *id)
{
@@ -565,6 +687,9 @@ static int apple_probe(struct hid_device *hdev,
jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
apple_fetch_battery(hdev);

+ if (quirks & APPLE_BACKLIGHT_CTL)
+ apple_backlight_init(hdev);
+
return 0;
}

--
2.25.1



2022-01-24 19:28:42

by Aditya Garg

[permalink] [raw]
Subject: [PATCH v2 2/3] HID: apple: Add necessary IDs and configuration for T2 Macs.

From: Aun-Ali Zaidi <[email protected]>

This patch adds the necessary IDs and configuration for Macs with
the T2 Security chip.

Signed-off-by: Aun-Ali Zaidi <[email protected]>
Signed-off-by: Aditya Garg <[email protected]>
---
drivers/hid/hid-apple.c | 16 ++++++++++++++++
drivers/hid/hid-ids.h | 8 ++++++++
drivers/hid/hid-quirks.c | 16 ++++++++++++++++
3 files changed, 40 insertions(+)

diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index c22d445a9..823021c24 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -861,6 +861,22 @@ static const struct hid_device_id apple_devices[] = {
.driver_data = APPLE_HAS_FN },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS),
.driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K),
+ .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132),
+ .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680),
+ .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213),
+ .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K),
+ .driver_data = APPLE_HAS_FN },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223),
+ .driver_data = APPLE_HAS_FN },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K),
+ .driver_data = APPLE_HAS_FN },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F),
+ .driver_data = APPLE_HAS_FN },
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI),
.driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO),
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 26cee452e..8ad26c20f 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -167,6 +167,14 @@
#define USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI 0x0272
#define USB_DEVICE_ID_APPLE_WELLSPRING9_ISO 0x0273
#define USB_DEVICE_ID_APPLE_WELLSPRING9_JIS 0x0274
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K 0x027a
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132 0x027b
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680 0x027c
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213 0x027d
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K 0x027e
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223 0x027f
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K 0x0280
+#define USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F 0x0340
#define USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY 0x030a
#define USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY 0x030b
#define USB_DEVICE_ID_APPLE_IRCONTROL 0x8240
diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
index 9af1dc8ae..963cf2a2e 100644
--- a/drivers/hid/hid-quirks.c
+++ b/drivers/hid/hid-quirks.c
@@ -294,6 +294,14 @@ static const struct hid_device_id hid_have_special_driver[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ISO) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F) },
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI) },
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO) },
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS) },
@@ -929,6 +937,14 @@ static const struct hid_device_id hid_mouse_ignore_list[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ANSI) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_ISO) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRING9_JIS) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K) },
+ { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_FOUNTAIN_TP_ONLY) },
{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) },
{ }
--
2.25.1


2022-02-01 20:14:16

by Aditya Garg

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] HID: apple: Add support for keyboard backlight on certain T2 Macs.

Hi

It has been a week since I have sent this series of patches, but I haven't got a reply yet. Before that, I had sent a v1 of the same, on which I wasn't contacted as well. May I have an update on this series. No reply for a long time is something which doesn't sound good.

Regards
Aditya

> On 24-Jan-2022, at 8:38 PM, Aditya Garg <[email protected]> wrote:
>
> From: Paul Pawlowski <[email protected]>
>
> This patch introduces the requisite plumbing for supporting keyboard
> backlight on T2-attached, USB exposed models. The quirk mechanism was
> used to reuse the existing hid-apple driver.
>
> Signed-off-by: Paul Pawlowski <[email protected]>
> Signed-off-by: Aun-Ali Zaidi <[email protected]>
> Signed-off-by: Aditya Garg <[email protected]>
> ---
> drivers/hid/hid-apple.c | 125 ++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 125 insertions(+)
>
> diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
> index 24802a4a6..c22d445a9 100644
> --- a/drivers/hid/hid-apple.c
> +++ b/drivers/hid/hid-apple.c
> @@ -7,6 +7,7 @@
> * Copyright (c) 2005 Michael Haboustak <[email protected]> for Concept2, Inc
> * Copyright (c) 2006-2007 Jiri Kosina
> * Copyright (c) 2008 Jiri Slaby <[email protected]>
> + * Copyright (c) 2019 Paul Pawlowski <[email protected]>
> */
>
> /*
> @@ -33,6 +34,7 @@
> /* BIT(7) reserved, was: APPLE_IGNORE_HIDINPUT */
> #define APPLE_NUMLOCK_EMULATION BIT(8)
> #define APPLE_RDESC_BATTERY BIT(9)
> +#define APPLE_BACKLIGHT_CTL 0x0200
>
> #define APPLE_FLAG_FKEY 0x01
>
> @@ -61,6 +63,12 @@ MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. "
> "(For people who want to keep PC keyboard muscle memory. "
> "[0] = as-is, Mac layout, 1 = swapped, PC layout)");
>
> +struct apple_sc_backlight {
> + struct led_classdev cdev;
> + struct hid_device *hdev;
> + unsigned short backlight_off, backlight_on_min, backlight_on_max;
> +};
> +
> struct apple_sc {
> struct hid_device *hdev;
> unsigned long quirks;
> @@ -68,6 +76,7 @@ struct apple_sc {
> unsigned int fn_found;
> DECLARE_BITMAP(pressed_numlock, KEY_CNT);
> struct timer_list battery_timer;
> + struct apple_sc_backlight *backlight;
> };
>
> struct apple_key_translation {
> @@ -76,6 +85,20 @@ struct apple_key_translation {
> u8 flags;
> };
>
> +struct apple_backlight_config_report {
> + u8 report_id;
> + u8 version;
> + u16 backlight_off, backlight_on_min, backlight_on_max;
> +};
> +
> +struct apple_backlight_set_report {
> + u8 report_id;
> + u8 version;
> + u16 backlight;
> + u16 rate;
> +};
> +
> +
> static const struct apple_key_translation apple2021_fn_keys[] = {
> { KEY_BACKSPACE, KEY_DELETE },
> { KEY_ENTER, KEY_INSERT },
> @@ -530,6 +553,105 @@ static int apple_input_configured(struct hid_device *hdev,
> return 0;
> }
>
> +static bool apple_backlight_check_support(struct hid_device *hdev)
> +{
> + int i;
> + unsigned int hid;
> + struct hid_report *report;
> +
> + list_for_each_entry(report, &hdev->report_enum[HID_INPUT_REPORT].report_list, list) {
> + for (i = 0; i < report->maxfield; i++) {
> + hid = report->field[i]->usage->hid;
> + if ((hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR && (hid & HID_USAGE) == 0xf)
> + return true;
> + }
> + }
> +
> + return false;
> +}
> +
> +static int apple_backlight_set(struct hid_device *hdev, u16 value, u16 rate)
> +{
> + int ret = 0;
> + struct apple_backlight_set_report *rep;
> +
> + rep = kmalloc(sizeof(*rep), GFP_KERNEL);
> + if (rep == NULL)
> + return -ENOMEM;
> +
> + rep->report_id = 0xB0;
> + rep->version = 1;
> + rep->backlight = value;
> + rep->rate = rate;
> +
> + ret = hid_hw_raw_request(hdev, 0xB0u, (u8 *) rep, sizeof(*rep),
> + HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
> +
> + kfree(rep);
> + return ret;
> +}
> +
> +static int apple_backlight_led_set(struct led_classdev *led_cdev,
> + enum led_brightness brightness)
> +{
> + struct apple_sc_backlight *backlight = container_of(led_cdev,
> + struct apple_sc_backlight, cdev);
> +
> + return apple_backlight_set(backlight->hdev, brightness, 0);
> +}
> +
> +static int apple_backlight_init(struct hid_device *hdev)
> +{
> + int ret;
> + struct apple_sc *asc = hid_get_drvdata(hdev);
> + struct apple_backlight_config_report *rep;
> +
> + if (!apple_backlight_check_support(hdev))
> + return -EINVAL;
> +
> + rep = kmalloc(0x200, GFP_KERNEL);
> + if (rep == NULL)
> + return -ENOMEM;
> +
> + ret = hid_hw_raw_request(hdev, 0xBFu, (u8 *) rep, sizeof(*rep),
> + HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
> + if (ret < 0) {
> + hid_err(hdev, "backlight request failed: %d\n", ret);
> + goto cleanup_and_exit;
> + }
> + if (ret < 8 || rep->version != 1) {
> + hid_err(hdev, "backlight config struct: bad version %i\n", rep->version);
> + ret = -EINVAL;
> + goto cleanup_and_exit;
> + }
> +
> + hid_dbg(hdev, "backlight config: off=%u, on_min=%u, on_max=%u\n",
> + rep->backlight_off, rep->backlight_on_min, rep->backlight_on_max);
> +
> + asc->backlight = devm_kzalloc(&hdev->dev, sizeof(*asc->backlight), GFP_KERNEL);
> + if (!asc->backlight) {
> + ret = -ENOMEM;
> + goto cleanup_and_exit;
> + }
> +
> + asc->backlight->hdev = hdev;
> + asc->backlight->cdev.name = "apple::kbd_backlight";
> + asc->backlight->cdev.max_brightness = rep->backlight_on_max;
> + asc->backlight->cdev.brightness_set_blocking = apple_backlight_led_set;
> +
> + ret = apple_backlight_set(hdev, 0, 0);
> + if (ret < 0) {
> + hid_err(hdev, "backlight set request failed: %d\n", ret);
> + goto cleanup_and_exit;
> + }
> +
> + ret = devm_led_classdev_register(&hdev->dev, &asc->backlight->cdev);
> +
> +cleanup_and_exit:
> + kfree(rep);
> + return ret;
> +}
> +
> static int apple_probe(struct hid_device *hdev,
> const struct hid_device_id *id)
> {
> @@ -565,6 +687,9 @@ static int apple_probe(struct hid_device *hdev,
> jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
> apple_fetch_battery(hdev);
>
> + if (quirks & APPLE_BACKLIGHT_CTL)
> + apple_backlight_init(hdev);
> +
> return 0;
> }
>
> --
> 2.25.1
>
>

2022-02-02 13:51:08

by Jiri Kosina

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] HID: apple: Add support for keyboard backlight on certain T2 Macs.

On Mon, 31 Jan 2022, Aditya Garg wrote:

> It has been a week since I have sent this series of patches, but I
> haven't got a reply yet. Before that, I had sent a v1 of the same, on
> which I wasn't contacted as well. May I have an update on this series.
> No reply for a long time is something which doesn't sound good.

A week during merge window and -rc1 phase is not that horrible, please be
a little bit more patient.

The patchset hasn't been lost, it's on my radar and I'll process it this
week still.

Thanks,

--
Jiri Kosina
SUSE Labs

2022-02-03 12:01:45

by Aditya Garg

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] HID: apple: Add support for keyboard backlight on certain T2 Macs.



> On 02-Feb-2022, at 7:10 PM, Jiri Kosina <[email protected]> wrote:
>
> On Mon, 31 Jan 2022, Aditya Garg wrote:
>
>> It has been a week since I have sent this series of patches, but I
>> haven't got a reply yet. Before that, I had sent a v1 of the same, on
>> which I wasn't contacted as well. May I have an update on this series.
>> No reply for a long time is something which doesn't sound good.
>
> A week during merge window and -rc1 phase is not that horrible, please be
> a little bit more patient.
>
Sorry for being impatient :)
> The patchset hasn't been lost, it's on my radar and I'll process it this
> week still.
Thanks
>
> Thanks,
>
> --
> Jiri Kosina
> SUSE Labs
>

2022-02-03 20:45:14

by Aditya Garg

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] HID: apple: Add support for keyboard backlight on certain T2 Macs.



> On 24-Jan-2022, at 8:38 PM, Aditya Garg <[email protected]> wrote:
>
> From: Paul Pawlowski <[email protected]>
>
> This patch introduces the requisite plumbing for supporting keyboard
> backlight on T2-attached, USB exposed models. The quirk mechanism was
> used to reuse the existing hid-apple driver.
>
> Signed-off-by: Paul Pawlowski <[email protected]>
> Signed-off-by: Aun-Ali Zaidi <[email protected]>
> Signed-off-by: Aditya Garg <[email protected]>
> ---
> drivers/hid/hid-apple.c | 125 ++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 125 insertions(+)
>
> diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
> index 24802a4a6..c22d445a9 100644
> --- a/drivers/hid/hid-apple.c
> +++ b/drivers/hid/hid-apple.c
> @@ -7,6 +7,7 @@
> * Copyright (c) 2005 Michael Haboustak <[email protected]> for Concept2, Inc
> * Copyright (c) 2006-2007 Jiri Kosina
> * Copyright (c) 2008 Jiri Slaby <[email protected]>
> + * Copyright (c) 2019 Paul Pawlowski <[email protected]>
> */
>
> /*
> @@ -33,6 +34,7 @@
> /* BIT(7) reserved, was: APPLE_IGNORE_HIDINPUT */
> #define APPLE_NUMLOCK_EMULATION BIT(8)
> #define APPLE_RDESC_BATTERY BIT(9)
> +#define APPLE_BACKLIGHT_CTL 0x0200
Jiri, should it be BIT(10)? 0x0200 is BIT(9) if I ain’t wrong.
>
> #define APPLE_FLAG_FKEY 0x01
>
> @@ -61,6 +63,12 @@ MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. "
> "(For people who want to keep PC keyboard muscle memory. "
> "[0] = as-is, Mac layout, 1 = swapped, PC layout)");
>
> +struct apple_sc_backlight {
> + struct led_classdev cdev;
> + struct hid_device *hdev;
> + unsigned short backlight_off, backlight_on_min, backlight_on_max;
> +};
> +
> struct apple_sc {
> struct hid_device *hdev;
> unsigned long quirks;
> @@ -68,6 +76,7 @@ struct apple_sc {
> unsigned int fn_found;
> DECLARE_BITMAP(pressed_numlock, KEY_CNT);
> struct timer_list battery_timer;
> + struct apple_sc_backlight *backlight;
> };
>
> struct apple_key_translation {
> @@ -76,6 +85,20 @@ struct apple_key_translation {
> u8 flags;
> };
>
> +struct apple_backlight_config_report {
> + u8 report_id;
> + u8 version;
> + u16 backlight_off, backlight_on_min, backlight_on_max;
> +};
> +
> +struct apple_backlight_set_report {
> + u8 report_id;
> + u8 version;
> + u16 backlight;
> + u16 rate;
> +};
> +
> +
> static const struct apple_key_translation apple2021_fn_keys[] = {
> { KEY_BACKSPACE, KEY_DELETE },
> { KEY_ENTER, KEY_INSERT },
> @@ -530,6 +553,105 @@ static int apple_input_configured(struct hid_device *hdev,
> return 0;
> }
>
> +static bool apple_backlight_check_support(struct hid_device *hdev)
> +{
> + int i;
> + unsigned int hid;
> + struct hid_report *report;
> +
> + list_for_each_entry(report, &hdev->report_enum[HID_INPUT_REPORT].report_list, list) {
> + for (i = 0; i < report->maxfield; i++) {
> + hid = report->field[i]->usage->hid;
> + if ((hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR && (hid & HID_USAGE) == 0xf)
> + return true;
> + }
> + }
> +
> + return false;
> +}
> +
> +static int apple_backlight_set(struct hid_device *hdev, u16 value, u16 rate)
> +{
> + int ret = 0;
> + struct apple_backlight_set_report *rep;
> +
> + rep = kmalloc(sizeof(*rep), GFP_KERNEL);
> + if (rep == NULL)
> + return -ENOMEM;
> +
> + rep->report_id = 0xB0;
> + rep->version = 1;
> + rep->backlight = value;
> + rep->rate = rate;
> +
> + ret = hid_hw_raw_request(hdev, 0xB0u, (u8 *) rep, sizeof(*rep),
> + HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
> +
> + kfree(rep);
> + return ret;
> +}
> +
> +static int apple_backlight_led_set(struct led_classdev *led_cdev,
> + enum led_brightness brightness)
> +{
> + struct apple_sc_backlight *backlight = container_of(led_cdev,
> + struct apple_sc_backlight, cdev);
> +
> + return apple_backlight_set(backlight->hdev, brightness, 0);
> +}
> +
> +static int apple_backlight_init(struct hid_device *hdev)
> +{
> + int ret;
> + struct apple_sc *asc = hid_get_drvdata(hdev);
> + struct apple_backlight_config_report *rep;
> +
> + if (!apple_backlight_check_support(hdev))
> + return -EINVAL;
> +
> + rep = kmalloc(0x200, GFP_KERNEL);
> + if (rep == NULL)
> + return -ENOMEM;
> +
> + ret = hid_hw_raw_request(hdev, 0xBFu, (u8 *) rep, sizeof(*rep),
> + HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
> + if (ret < 0) {
> + hid_err(hdev, "backlight request failed: %d\n", ret);
> + goto cleanup_and_exit;
> + }
> + if (ret < 8 || rep->version != 1) {
> + hid_err(hdev, "backlight config struct: bad version %i\n", rep->version);
> + ret = -EINVAL;
> + goto cleanup_and_exit;
> + }
> +
> + hid_dbg(hdev, "backlight config: off=%u, on_min=%u, on_max=%u\n",
> + rep->backlight_off, rep->backlight_on_min, rep->backlight_on_max);
> +
> + asc->backlight = devm_kzalloc(&hdev->dev, sizeof(*asc->backlight), GFP_KERNEL);
> + if (!asc->backlight) {
> + ret = -ENOMEM;
> + goto cleanup_and_exit;
> + }
> +
> + asc->backlight->hdev = hdev;
> + asc->backlight->cdev.name = "apple::kbd_backlight";
> + asc->backlight->cdev.max_brightness = rep->backlight_on_max;
> + asc->backlight->cdev.brightness_set_blocking = apple_backlight_led_set;
> +
> + ret = apple_backlight_set(hdev, 0, 0);
> + if (ret < 0) {
> + hid_err(hdev, "backlight set request failed: %d\n", ret);
> + goto cleanup_and_exit;
> + }
> +
> + ret = devm_led_classdev_register(&hdev->dev, &asc->backlight->cdev);
> +
> +cleanup_and_exit:
> + kfree(rep);
> + return ret;
> +}
> +
> static int apple_probe(struct hid_device *hdev,
> const struct hid_device_id *id)
> {
> @@ -565,6 +687,9 @@ static int apple_probe(struct hid_device *hdev,
> jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
> apple_fetch_battery(hdev);
>
> + if (quirks & APPLE_BACKLIGHT_CTL)
> + apple_backlight_init(hdev);
> +
> return 0;
> }
>
> --
> 2.25.1
>
>

2022-02-06 13:32:36

by Aditya Garg

[permalink] [raw]
Subject: [PATCH v3 1/3] HID: apple: Add support for keyboard backlight on certain T2 Macs.

From: Paul Pawlowski <[email protected]>

This patch introduces the requisite plumbing for supporting keyboard
backlight on T2-attached, USB exposed models. The quirk mechanism was
used to reuse the existing hid-apple driver.

Signed-off-by: Paul Pawlowski <[email protected]>
Signed-off-by: Aun-Ali Zaidi <[email protected]>
Signed-off-by: Aditya Garg <[email protected]>
---
v2 :- Use better approach to map fn keys
v3 :- Use BIT(10) for APPLE_BACKLIGHT_CTL since 0x0200 (BIT (9)) has been used.
drivers/hid/hid-apple.c | 125 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 125 insertions(+)

diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index 24802a4a6..c22d445a9 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -7,6 +7,7 @@
* Copyright (c) 2005 Michael Haboustak <[email protected]> for Concept2, Inc
* Copyright (c) 2006-2007 Jiri Kosina
* Copyright (c) 2008 Jiri Slaby <[email protected]>
+ * Copyright (c) 2019 Paul Pawlowski <[email protected]>
*/

/*
@@ -33,6 +34,7 @@
/* BIT(7) reserved, was: APPLE_IGNORE_HIDINPUT */
#define APPLE_NUMLOCK_EMULATION BIT(8)
#define APPLE_RDESC_BATTERY BIT(9)
+#define APPLE_BACKLIGHT_CTL BIT(10)

#define APPLE_FLAG_FKEY 0x01

@@ -61,6 +63,12 @@ MODULE_PARM_DESC(swap_fn_leftctrl, "Swap the Fn and left Control keys. "
"(For people who want to keep PC keyboard muscle memory. "
"[0] = as-is, Mac layout, 1 = swapped, PC layout)");

+struct apple_sc_backlight {
+ struct led_classdev cdev;
+ struct hid_device *hdev;
+ unsigned short backlight_off, backlight_on_min, backlight_on_max;
+};
+
struct apple_sc {
struct hid_device *hdev;
unsigned long quirks;
@@ -68,6 +76,7 @@ struct apple_sc {
unsigned int fn_found;
DECLARE_BITMAP(pressed_numlock, KEY_CNT);
struct timer_list battery_timer;
+ struct apple_sc_backlight *backlight;
};

struct apple_key_translation {
@@ -76,6 +85,20 @@ struct apple_key_translation {
u8 flags;
};

+struct apple_backlight_config_report {
+ u8 report_id;
+ u8 version;
+ u16 backlight_off, backlight_on_min, backlight_on_max;
+};
+
+struct apple_backlight_set_report {
+ u8 report_id;
+ u8 version;
+ u16 backlight;
+ u16 rate;
+};
+
+
static const struct apple_key_translation apple2021_fn_keys[] = {
{ KEY_BACKSPACE, KEY_DELETE },
{ KEY_ENTER, KEY_INSERT },
@@ -530,6 +553,105 @@ static int apple_input_configured(struct hid_device *hdev,
return 0;
}

+static bool apple_backlight_check_support(struct hid_device *hdev)
+{
+ int i;
+ unsigned int hid;
+ struct hid_report *report;
+
+ list_for_each_entry(report, &hdev->report_enum[HID_INPUT_REPORT].report_list, list) {
+ for (i = 0; i < report->maxfield; i++) {
+ hid = report->field[i]->usage->hid;
+ if ((hid & HID_USAGE_PAGE) == HID_UP_MSVENDOR && (hid & HID_USAGE) == 0xf)
+ return true;
+ }
+ }
+
+ return false;
+}
+
+static int apple_backlight_set(struct hid_device *hdev, u16 value, u16 rate)
+{
+ int ret = 0;
+ struct apple_backlight_set_report *rep;
+
+ rep = kmalloc(sizeof(*rep), GFP_KERNEL);
+ if (rep == NULL)
+ return -ENOMEM;
+
+ rep->report_id = 0xB0;
+ rep->version = 1;
+ rep->backlight = value;
+ rep->rate = rate;
+
+ ret = hid_hw_raw_request(hdev, 0xB0u, (u8 *) rep, sizeof(*rep),
+ HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
+
+ kfree(rep);
+ return ret;
+}
+
+static int apple_backlight_led_set(struct led_classdev *led_cdev,
+ enum led_brightness brightness)
+{
+ struct apple_sc_backlight *backlight = container_of(led_cdev,
+ struct apple_sc_backlight, cdev);
+
+ return apple_backlight_set(backlight->hdev, brightness, 0);
+}
+
+static int apple_backlight_init(struct hid_device *hdev)
+{
+ int ret;
+ struct apple_sc *asc = hid_get_drvdata(hdev);
+ struct apple_backlight_config_report *rep;
+
+ if (!apple_backlight_check_support(hdev))
+ return -EINVAL;
+
+ rep = kmalloc(0x200, GFP_KERNEL);
+ if (rep == NULL)
+ return -ENOMEM;
+
+ ret = hid_hw_raw_request(hdev, 0xBFu, (u8 *) rep, sizeof(*rep),
+ HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
+ if (ret < 0) {
+ hid_err(hdev, "backlight request failed: %d\n", ret);
+ goto cleanup_and_exit;
+ }
+ if (ret < 8 || rep->version != 1) {
+ hid_err(hdev, "backlight config struct: bad version %i\n", rep->version);
+ ret = -EINVAL;
+ goto cleanup_and_exit;
+ }
+
+ hid_dbg(hdev, "backlight config: off=%u, on_min=%u, on_max=%u\n",
+ rep->backlight_off, rep->backlight_on_min, rep->backlight_on_max);
+
+ asc->backlight = devm_kzalloc(&hdev->dev, sizeof(*asc->backlight), GFP_KERNEL);
+ if (!asc->backlight) {
+ ret = -ENOMEM;
+ goto cleanup_and_exit;
+ }
+
+ asc->backlight->hdev = hdev;
+ asc->backlight->cdev.name = "apple::kbd_backlight";
+ asc->backlight->cdev.max_brightness = rep->backlight_on_max;
+ asc->backlight->cdev.brightness_set_blocking = apple_backlight_led_set;
+
+ ret = apple_backlight_set(hdev, 0, 0);
+ if (ret < 0) {
+ hid_err(hdev, "backlight set request failed: %d\n", ret);
+ goto cleanup_and_exit;
+ }
+
+ ret = devm_led_classdev_register(&hdev->dev, &asc->backlight->cdev);
+
+cleanup_and_exit:
+ kfree(rep);
+ return ret;
+}
+
static int apple_probe(struct hid_device *hdev,
const struct hid_device_id *id)
{
@@ -565,6 +687,9 @@ static int apple_probe(struct hid_device *hdev,
jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
apple_fetch_battery(hdev);

+ if (quirks & APPLE_BACKLIGHT_CTL)
+ apple_backlight_init(hdev);
+
return 0;
}

--
2.25.1



2022-02-14 19:08:14

by Jiri Kosina

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] HID: apple: Add support for keyboard backlight on certain T2 Macs.

On Thu, 3 Feb 2022, Aditya Garg wrote:

>
>
> > On 24-Jan-2022, at 8:38 PM, Aditya Garg <[email protected]> wrote:
> >
> > From: Paul Pawlowski <[email protected]>
> >
> > This patch introduces the requisite plumbing for supporting keyboard
> > backlight on T2-attached, USB exposed models. The quirk mechanism was
> > used to reuse the existing hid-apple driver.
> >
> > Signed-off-by: Paul Pawlowski <[email protected]>
> > Signed-off-by: Aun-Ali Zaidi <[email protected]>
> > Signed-off-by: Aditya Garg <[email protected]>
> > ---
> > drivers/hid/hid-apple.c | 125 ++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 125 insertions(+)
> >
> > diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
> > index 24802a4a6..c22d445a9 100644
> > --- a/drivers/hid/hid-apple.c
> > +++ b/drivers/hid/hid-apple.c
> > @@ -7,6 +7,7 @@
> > * Copyright (c) 2005 Michael Haboustak <[email protected]> for Concept2, Inc
> > * Copyright (c) 2006-2007 Jiri Kosina
> > * Copyright (c) 2008 Jiri Slaby <[email protected]>
> > + * Copyright (c) 2019 Paul Pawlowski <[email protected]>
> > */
> >
> > /*
> > @@ -33,6 +34,7 @@
> > /* BIT(7) reserved, was: APPLE_IGNORE_HIDINPUT */
> > #define APPLE_NUMLOCK_EMULATION BIT(8)
> > #define APPLE_RDESC_BATTERY BIT(9)
> > +#define APPLE_BACKLIGHT_CTL 0x0200
> Jiri, should it be BIT(10)? 0x0200 is BIT(9) if I ain’t wrong.

Yes, please use BIT(10). Thanks,

--
Jiri Kosina
SUSE Labs