[3/3] HID: apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards
Message ID | 7D70F1FE-7F54-4D0A-8922-5466AA2AD364@live.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp736934wrn; Thu, 9 Feb 2023 19:57:06 -0800 (PST) X-Google-Smtp-Source: AK7set/pHkd1imPewyrlXy1hfiXhSHANBMAVR9QM/FGmaSqshEGvUu15tt+xGWQ6usHBfbiNOA5t X-Received: by 2002:a17:906:6b06:b0:87b:da76:51b7 with SMTP id q6-20020a1709066b0600b0087bda7651b7mr13426144ejr.63.1676001425995; Thu, 09 Feb 2023 19:57:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1676001425; cv=pass; d=google.com; s=arc-20160816; b=pG3hBVyC/lDZ9kjEEu/Ux1Przp9C3YD8GYzJapiedlE2M+sVwGM0Lsj+KMpD/9M25R xgwN75FGsi8ZGbllPvDSSl1RDiixytZrkJLpN0wJPHA38ekp/f9vw52wvVinMv3UwR0v wz5ehUVy4zD850ZpsQHjnIv14LpvsoYqXQtU+XUgJOAzuqSj0Srt8wsINqTdbcezzUoV As+7qQB0jZ6NbPq44ev1ahHGED2qKN6v1QVD76l5jsUkf/Hwx8JTgL9Y8L6WsTHfzsTk sEH2OFuyQwC77H9swOH+pPXyVOQ1iuQLGCtpn+FAHYzIRfwciUnYjcvo6D2m+zP3amBN o4Yg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:references :message-id:date:thread-index:thread-topic:subject:cc:to:from :dkim-signature; bh=sGbaXX8RSvYbjyEeO9URTT0RTL7VsDmglSbcAradVP4=; b=wjHiYtlkmfyIrwUxQUZjWx0H7gk+CwjryJZUey8oKX9U4LfUWaBVWs2+OYpA4v3Pta KBbyUVcJyLFdTn0kWWXuazFqsNJe4Bw/B11FJRTBmS481WQ2b/y9yfZ2B/NfmQmshuZ/ OoUFn+vNsGlGU5Rv5IFvAu2pWK3t010KkvqZJCeYuKW0u/4IhYv42j5nSMeOKdCzisxL Dnmos21J5OHJqZGV0y3xL7U1hCt2Ern70vCrwT+ezq9UveVSjiBDARZGUvt7F8WWK5tb ZuJQu5a3sPa/EDnDnm6CCeLNLXgSL6OcdS2c4jXpT4VRq7rcWm9RBhACC+xoVds9C6Cd 5R9A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@live.com header.s=selector1 header.b=gozhCjye; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=live.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z7-20020a17090674c700b0087bca83172asi4977540ejl.320.2023.02.09.19.56.43; Thu, 09 Feb 2023 19:57:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@live.com header.s=selector1 header.b=gozhCjye; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=live.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229869AbjBJDpc (ORCPT <rfc822;ybw1215001957@gmail.com> + 99 others); Thu, 9 Feb 2023 22:45:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230320AbjBJDpa (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 9 Feb 2023 22:45:30 -0500 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12olkn2089.outbound.protection.outlook.com [40.92.23.89]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1168A55E5A; Thu, 9 Feb 2023 19:45:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Oq0vh44UbwtEHAUqvSlyip6a7T1aVA9jpYGJC0+OI4rhrL7WMZTXs5qiI2v7obrC19h1D5OqO1HqhSZmUyHzO7+E5w+ENa8B34sM8HNG34iQjvgkAOilrNm2i35Gmdw9hnQo/dyzqaqctPJ5PO8981oVPxRC4Y+iDSsfj9WN8pJ+N0ShHn01x4doVZ7okpp1cHYBmhv9RxmZRbokLvoU5cpvkInjnhCoEsNfrT2uednOsEieiWoVT1ycnlYcX3FsRwCKw0VB3rEUTwetymQHZNkuv1miAg0SdcySld1c5Ri/ZcqnGINbgmZ4718r6TCzkIzb8STjzXQ2wO5ECVTJHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=sGbaXX8RSvYbjyEeO9URTT0RTL7VsDmglSbcAradVP4=; b=YqLQP+CBFOUp5ldVzJ0GR3mYyxFTPFTPrWyE8P3oQb9mW8hCM7EzoFFeqC/C4C77sch4bJ9tO+YQ0ezBKIloBs7YoUds8YWy1Ia1s09zqWPGdsvFGwEhtfSfUHzVa3NIxziBLW92IPdyi34ow4rBPblCc63eckYP1OzjD0YDqtqeCQ1Ks6rDB6xntBSniBji3GVP2w0iTf0uoramLcUV8BBv8XMdWEfr4vUrwCHUu5ZXTDOMi8JqsvblY9hoWFDycAC4aV8r2TJ1YZgnRg3bJVa6zuWBLge7dvVgOmOAxxWAz6HzqE6tLF98dIlVoMlppAnppAusYwAKAwYnSWyc9w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=sGbaXX8RSvYbjyEeO9URTT0RTL7VsDmglSbcAradVP4=; b=gozhCjyeFpdDc42uNwLtyfZhqtijjJ5d8S4KsGfU48bMyf4uaW0e+pOAA24b+wpwMsHrW0F/r8oqt5MHqwXgRP4va5OBF1HBC17zUHBxe/6HeeMdrahNxujOPAISa8EBtnLKXSsHNLfhzBRI5gaypVDqI65RZbISyCKBMBmw4Y2Lobz+krDU6BK08JMBsOVoKWfUEFaH01nv301o+jRESBznnX+wLH6xnQHsONzIkcKTQdrF/mUilPs3fAr3KNTqGRSjkHDRa3lC/0A2secM8fWYVCEEkZa4ZTZvRoPWnyeNxQNA4PzJGvIOJ1VTxK3ZIJLE6gBm2f9bgxbOMUW1Kg== Received: from BM1PR01MB0931.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:2::9) by MAZPR01MB5554.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a01:61::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6086.19; Fri, 10 Feb 2023 03:45:15 +0000 Received: from BM1PR01MB0931.INDPRD01.PROD.OUTLOOK.COM ([fe80::9dc7:9298:c988:474d]) by BM1PR01MB0931.INDPRD01.PROD.OUTLOOK.COM ([fe80::9dc7:9298:c988:474d%11]) with mapi id 15.20.6111.005; Fri, 10 Feb 2023 03:45:15 +0000 From: Aditya Garg <gargaditya08@live.com> To: 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> CC: 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>, Orlando Chamberlain <orlandoch.dev@gmail.com> Subject: [PATCH 3/3] HID: apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards Thread-Topic: [PATCH 3/3] HID: apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards Thread-Index: AQHZPQIWS64uJ4UU002DT0+XMdJmAQ== Date: Fri, 10 Feb 2023 03:45:15 +0000 Message-ID: <7D70F1FE-7F54-4D0A-8922-5466AA2AD364@live.com> References: <E5D8BEBA-3C5B-460F-BD2C-39470A793CC3@live.com> <40274C3D-4F4F-479C-944C-EEBDC78F959C@live.com> <868AA58D-2399-4E4A-A6C6-73F88DB13992@live.com> In-Reply-To: <868AA58D-2399-4E4A-A6C6-73F88DB13992@live.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [J+R67hDQf1GVocUIdVvd5qZEgysQmup0] x-ms-publictraffictype: Email x-ms-traffictypediagnostic: BM1PR01MB0931:EE_|MAZPR01MB5554:EE_ x-ms-office365-filtering-correlation-id: 1b2b259c-47c6-4272-63d2-08db0b1938ba x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: pCShkIKLV2uTtaII1hch92VBEW9ZPomICahicj6IWQ177ceSQeOZhIbGxRskF8kFvFP+0cwROpW5FEOH8CAcXgSmdPvcp7NHcG2Gejf/TuYWtDKkpWNtKmFPYCAPiNZhfTj6wcceSW05rbnVdjuXLDPlacOpP2V/XrujLjwUjQZAOxQJkPh3KTZk03M9xV2XB1cT3p8TLDeGqn4qECwS+M+oYt+9RaObWW9St0nrqBomqKv7K+VonVu6S3zZWfZrkUPqq69Pe2+QazcLBPn1G9aDwK4VQe+gLwyoWaBSQ2O61AFduPGx+L3i6jOpe4gP+uHJh+UTSCymb7ThG3jZsHS59o0SaynPNLqnM2Kex2rPAla49aagvMev7Rsz3MC2M7SsPJ8+/ybwNTpNiY/xSsg6WVmtVxW5ulec/Szjb/h5SCSm8QOCtsLXYCGXRXfvnLLw38hHj13PEoSUUBbtcxD92X+dCqjATdCMnggH2ZOdKv+n1KGbV8OEJIefoyIF6Gff1ZXax+6v8k+m09ETQ4He66TmeZDCSHz+gh4wEqbPjzGHcfK0/5Xt6Y35ia1VL28vYgXZa6rwgZDL86cdi5cvFC3Z0lokVpSbBdmDBfw= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: 13fy/mTm9oHvtXlp+tZqTrkC/nN6bBt47PPDEGPEjdJchvLt/gUqBxMSUtVBUB1vpD2LQaHOyGFeExj9DQ27/lV2GY5HN4YhLVKNqRZqvJ75mWjuCPwxV1a4JZtY2nwp6xyKlG6EDUvQpfgSezJGWtlGCUSiPBIfmc92lN6uHFsdWpiNzTAilB5qkO+efaWUQo0v/AQTuppCgnturcabtYl9FRVBsMaJWWct0LN30X5zRkVOH08vWh6kntqPE2AgNJCXWXTYxmxzqaAf4grHYHdfj0RhtCyjxdWr/mdR/HUoYK/goEumsNe7YCo0cUk+OWD29Oin8BRcXWDLktEFof+uiG1ZRhLMMUMVTebliQVUcwWeMwamU3xrGV1Eard6gsd+0bM5CNL1LGwbZePER3AfP8SnzlRR9o/clToOt0eKiTG3vOV1DpF6xKBXVo/qDYAWenJb9RO9ybutoCfQkEJiAxhbfNfRj11Oa2DTb6UU5cFTf6BLTaNN1dMutG0QoMGsT04+jQO+UxIITmiI7mDQfLaaYcQ8glcp32axLZhYDWYJYh07h8OPeH4qPTh6SCJ/2cP6bXLb2WEduUKpEn277TffoLxuEx+ceT68UnlqYSKsZKDYfOrkSeuTQcO4hhjNNaW+miI9joaMSVT7THJ/NmfY127AHLi+KwlnPihLcojjCbNPZ7B5F+ZQvOyga2XWH/ck+u+o/GkDq9XzafoFdWNS9kPjs+b85DU7H06eouElJWxw4CE/WA1PNBStkK1i8ShUqbbmSrTbZ3MyfGCECB+D/h3pbw0n8ZVO68uPGgeeBSPPDZUoQsIN3PFmVuQBOBl4maoTsrfJEm7+YXqZAq6dazGQR6TQzj/j+rymKnYUrqC/oGUOStgUK39YXPvcZg4jmu3BVJL5VpCllHDE44qrz9JdT1XP2r8+oLJzoMC6kJO48sMfVH0av5+iVutoe9kbihwkC2Xi/t35tHVHA2dSal7hxnw1LHm+4LVFyC7EA7FYnug12YQf4UajNQ2pCj4se2bVStDG/4iS+3+IwIvbxyTW334i5f53Oh806XSXgJQ+qyrtp/2jHgVMuBfEnr2I1uZHydMVEB4L2zykvE6292mKt6nk3io0f6ef2J2edQ+aVYoYycE7fdPVu8rh3MGb8sw8WRGM5e7lGce9p0/4YUzg25XcEFqrU4D6+woMuXJ5EYWyxuaTc0cdh8R1KMgztpXLqaFNItozOnqjPCtdsQJ0TM0BCsi+r6keoP5eOrZbBWsMbgYlwlba7CuLd4+5z9gLRG8hXm7sVg== Content-Type: text/plain; charset="us-ascii" Content-ID: <2007E093061ACA4D85D22EC2F05BEEFA@INDPRD01.PROD.OUTLOOK.COM> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-42ed3.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BM1PR01MB0931.INDPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 1b2b259c-47c6-4272-63d2-08db0b1938ba X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Feb 2023 03:45:15.6728 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MAZPR01MB5554 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757414871330041430?= X-GMAIL-MSGID: =?utf-8?q?1757414871330041430?= |
Series |
Touch Bar and Keyboard backlight driver for Intel Macs
|
|
Commit Message
Aditya Garg
Feb. 10, 2023, 3:45 a.m. UTC
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
Comments
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. > + > +#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. > + > +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. > +}; > + > +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 >
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 > >
On Sat, Feb 11, 2023 at 10:24:25AM +1100, Orlando Chamberlain wrote: > 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> > > > +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. I didn't mean to suggest changing the logic, just the way the code is organized: static void apple_magic_backlight_report_set(struct apple_magic_backlight *backlight, struct hid_report *rep, char value, char 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(backlight->hdev, 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, backlight->power, !!brightness, rate); if (brightness) apple_magic_backlight_report_set(backlight, backlight->brightness, brightness, rate); } This way you can get rid of the duplicated code. > > > > > + > > > +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); +} > > > +
On Sat, Feb 11, 2023 at 02:23:42AM +0000, Thomas Weißschuh wrote: > On Sat, Feb 11, 2023 at 10:24:25AM +1100, Orlando Chamberlain wrote: > > 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> > > > > +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. > > I didn't mean to suggest changing the logic, just the way the code is > organized: > > static void apple_magic_backlight_report_set(struct apple_magic_backlight *backlight, > struct hid_report *rep, char value, char 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(backlight->hdev, 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, backlight->power, !!brightness, rate); > if (brightness) > apple_magic_backlight_report_set(backlight, backlight->brightness, brightness, rate); > } > > This way you can get rid of the duplicated code. Or even better, get rid of the struct apple_magic_backlight parameter altogether: static void apple_magic_backlight_report_set(struct hid_report *rep, char value, char 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_power_set(backlight, brightness, > > > > rate); > > > > + if (brightness) > > > > + apple_magic_backlight_brightness_set(backlight, > > > > brightness, rate); +} > > > > +
Hi! > 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) > + backlight->hdev = hdev; > + backlight->cdev.name = "apple::kbd_backlight"; ":white:kbd_backlight", plus document this in Documentation/leds/well-known-leds.txt so that we keep it consistent accross machines? Thanks, Pavel
On Sat, 11 Feb 2023 17:56:15 +0100 Pavel Machek <pavel@ucw.cz> wrote: > Hi! > > > 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) > > > + backlight->hdev = hdev; > > + backlight->cdev.name = "apple::kbd_backlight"; > > ":white:kbd_backlight", plus document this in > Documentation/leds/well-known-leds.txt so that we keep it consistent > accross machines? As in "apple:white:kbd_backlight" or just ":white:kbd_backlight"? It seems like most examples I can see by grepping around use "apple::kbd_backlight", "asus::kbd_backlight" etc, without any colour information or anything between the two colons. Would we want to start having the led names in new drivers (including this one) as "$vendor:$colour:$location_or_description"? I do notice you've also suggested doing that here https://lore.kernel.org/all/Y+I7xNqkq%2FX6Lag+@duo.ucw.cz/, and that conversation suggests adding the colour won't stop userspace tools from finding the backlight, which is good. One alternative would be to have the colour of the led stored in a sysfs file in the led's sysfs folder, not sure if that'd be better (it does mean the names stay the same), but also I'm not sure what the empty space between the two colons in the current led names is there for, if not colour. > > Thanks, > Pavel
> On 11-Feb-2023, at 10:26 PM, Pavel Machek <pavel@ucw.cz> wrote: > > Hi! > >> 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) > >> + backlight->hdev = hdev; >> + backlight->cdev.name = "apple::kbd_backlight"; > > ":white:kbd_backlight", plus document this in > Documentation/leds/well-known-leds.txt so that we keep it consistent > accross machines? We had used the same name for butterfly keyboards, the code being in hid-apple Will that also be needed to be changed? > > Thanks, > Pavel > -- > People of Russia, stop Putin before his war on Ukraine escalates.
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) ... > +#include <linux/hid.h> > +#include <linux/usb.h> Seems lack of some header inclusions, e.g. where struct led_classdev is defined or -ERRNO codes. > +#include "hid-ids.h" ... > +static void apple_magic_backlight_power_set(struct apple_magic_backlight *backlight, > + char power, char rate) char is a beast, can we use u8 here and in similar cases? ... > + /* Ensure this usb endpoint is for the keyboard backlight, not touchbar > + * backlight. > + */ /* * Multi-line comment style * goes like this. */ ... > + backlight = devm_kzalloc(&hdev->dev, sizeof(*backlight), GFP_KERNEL); > + Redundant blank line. > + if (!backlight) > + return -ENOMEM; ... > +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, > +}; > + Redundant blank line. > +module_hid_driver(apple_magic_backlight_hid_driver);
On Sun, 12 Feb 2023 13:18:33 +0200 Andy Shevchenko <andy@infradead.org> 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) > > ... > > > +#include <linux/hid.h> > > +#include <linux/usb.h> > > Seems lack of some header inclusions, e.g. where struct led_classdev > is defined or -ERRNO codes. > > > +#include "hid-ids.h" > > ... > > > +static void apple_magic_backlight_power_set(struct > > apple_magic_backlight *backlight, > > + char power, char rate) > > char is a beast, can we use u8 here and in similar cases? > > ... > > > + /* Ensure this usb endpoint is for the keyboard backlight, > > not touchbar > > + * backlight. > > + */ > > /* > * Multi-line comment style > * goes like this. > */ > > ... > > > + backlight = devm_kzalloc(&hdev->dev, sizeof(*backlight), > > GFP_KERNEL); > > > + > > Redundant blank line. > > > + if (!backlight) > > + return -ENOMEM; > > ... > > > +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, > > +}; > > > + > > Redundant blank line. > > > +module_hid_driver(apple_magic_backlight_hid_driver); > Thanks for pointing out all these, I'll make those changes in v2.
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> + */ + +#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[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, +}; + +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");