linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Orlando Chamberlain <orlandoch.dev@gmail.com>
To: "Thomas Weißschuh" <thomas@t-8ch.de>
Cc: Aditya Garg <gargaditya08@live.com>,
	Jiri Kosina <jikos@kernel.org>,
	"jkosina@suse.cz" <jkosina@suse.cz>,
	"benjamin.tissoires@redhat.com" <benjamin.tissoires@redhat.com>,
	Andy Shevchenko <andy@infradead.org>,
	"andy.shevchenko@gmail.com" <andy.shevchenko@gmail.com>,
	LKML <linux-kernel@vger.kernel.org>,
	"linux-input@vger.kernel.org" <linux-input@vger.kernel.org>,
	"ronald@innovation.ch" <ronald@innovation.ch>,
	"kekrby@gmail.com" <kekrby@gmail.com>
Subject: Re: [PATCH 3/3] HID: apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards
Date: Sat, 11 Feb 2023 10:24:25 +1100	[thread overview]
Message-ID: <20230211102425.178e1c78@redecorated-mbp> (raw)
In-Reply-To: <20230210162518.pe7ipe44falu3j3k@t-8ch.de>

On Fri, 10 Feb 2023 16:25:18 +0000
Thomas Weißschuh <thomas@t-8ch.de> wrote:

> On Fri, Feb 10, 2023 at 03:45:15AM +0000, Aditya Garg wrote:
> > From: Orlando Chamberlain <orlandoch.dev@gmail.com>
> > 
> > This driver adds 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>
> > Signed-off-by: Aditya Garg <gargaditya08@live.com>
> > ---
> >  MAINTAINERS                         |   6 ++
> >  drivers/hid/Kconfig                 |  13 +++
> >  drivers/hid/Makefile                |   1 +
> >  drivers/hid/apple-magic-backlight.c | 143
> > ++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+)
> >  create mode 100644 drivers/hid/apple-magic-backlight.c
> > 
> > diff --git a/MAINTAINERS b/MAINTAINERS
> > index fb1471cb5..3319f0c3e 100644
> > --- a/MAINTAINERS
> > +++ b/MAINTAINERS
> > @@ -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 <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 4ec669267..ad4612ec5 100644
> > --- a/drivers/hid/Kconfig
> > +++ b/drivers/hid/Kconfig
> > @@ -156,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 c792e42fe..a961914ec 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 000000000..9b128f6df
> > --- /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>
> > + */  
> 
> This patch doesn't seem to depend on the others at all and is much
> simpler.
> Maybe split it out from the series so it can get merged on its own and
> you don't have to carry it around anymore.

Good point, I might send v2 separately from the touchbar and ibridge
patches.

> 
> > +
> > +#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);
> > +  
> 
> The two functions above are nearly identical.

They are indeed quite similar, and I can turn the backlight off with the
brightness one, but when I logged the usb packets Windows used, it used
both so I've done the same in the Linux driver to (hopefully) ensure it
works with any other models or firmware updates that the Windows driver
works on.

> 
> > +
> > +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[0].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) },
> > +	{ }
> > +};
> > +MODULE_DEVICE_TABLE(hid, apple_magic_backlight_hid_ids);
> > +
> > +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,  
> 
> Drop the .remove, it does the same as the default.
> 

I didn't realise that! I will make that change in a v2.

> > +};
> > +
> > +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");
> > -- 
> > 2.37.2
> >   


  reply	other threads:[~2023-02-10 23:24 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-10  3:41 [PATCH 0/3] Touch Bar and Keyboard backlight driver for Intel Macs Aditya Garg
2023-02-10  3:43 ` [PATCH 1/3] HID: apple-ibridge: Add Apple iBridge HID driver for T1 chip Aditya Garg
2023-02-10  3:44   ` [PATCH 2/3] HID: apple-touchbar: Add driver for the Touch Bar on MacBook Pros Aditya Garg
2023-02-10  3:45     ` [PATCH 3/3] HID: apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards Aditya Garg
2023-02-10 16:25       ` Thomas Weißschuh
2023-02-10 23:24         ` Orlando Chamberlain [this message]
2023-02-11  2:23           ` Thomas Weißschuh
2023-02-11  2:42             ` Thomas Weißschuh
2023-02-11 16:56       ` Pavel Machek
2023-02-12  2:28         ` Orlando Chamberlain
2023-02-12  5:16         ` Aditya Garg
2023-02-12 11:18       ` Andy Shevchenko
2023-02-16  1:17         ` Orlando Chamberlain
2023-02-10 16:13     ` [PATCH 2/3] HID: apple-touchbar: Add driver for the Touch Bar on MacBook Pros Thomas Weißschuh
2023-02-12 11:56     ` Andy Shevchenko
2023-02-10  4:56   ` [PATCH 1/3] HID: apple-ibridge: Add Apple iBridge HID driver for T1 chip Thomas Weißschuh
2023-02-10  8:30     ` Aditya Garg
2023-02-10  8:39       ` Benjamin Tissoires
2023-02-10  8:54         ` Aditya Garg
2023-02-10  9:21           ` Benjamin Tissoires
2023-02-10 12:05     ` Aditya Garg
2023-02-10 12:20       ` Orlando Chamberlain
2023-02-10 13:07         ` Aditya Garg
2023-02-10 14:01           ` Benjamin Tissoires
2023-02-10 15:33       ` Thomas Weißschuh
2023-02-10 15:49         ` Aditya Garg
2023-02-10 18:36           ` Thomas Weißschuh 
2023-02-12 11:35   ` Andy Shevchenko
2023-02-10 10:18 ` [PATCH 0/3] Touch Bar and Keyboard backlight driver for Intel Macs Andy Shevchenko
2023-02-10 10:41   ` Aditya Garg
2023-02-10 10:47     ` Orlando Chamberlain
2023-02-10 11:12       ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230211102425.178e1c78@redecorated-mbp \
    --to=orlandoch.dev@gmail.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=andy@infradead.org \
    --cc=benjamin.tissoires@redhat.com \
    --cc=gargaditya08@live.com \
    --cc=jikos@kernel.org \
    --cc=jkosina@suse.cz \
    --cc=kekrby@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ronald@innovation.ch \
    --cc=thomas@t-8ch.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).