From patchwork Thu Feb 16 04:12:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 57837 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp104988wrn; Wed, 15 Feb 2023 20:41:40 -0800 (PST) X-Google-Smtp-Source: AK7set9EKBJr1iji7lDZ49HNF4UTij3KoteNqpBA6n9xrGChYwK1nqdhucgqWMl09/qSSTBt9mM4 X-Received: by 2002:a17:90a:19d6:b0:232:cb61:3883 with SMTP id 22-20020a17090a19d600b00232cb613883mr5615384pjj.14.1676522500309; Wed, 15 Feb 2023 20:41:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676522500; cv=none; d=google.com; s=arc-20160816; b=l9W12/ZsyJTofBGlXbt6Qu5i+v1LG1fdmWbDTT2jPbrqJFSg96p6Yo3yxtP+ABbphl 3ek2uupfyF0DJ9IBGqn5IHQz8EOZBq9MXN7VJerdxpUYnj8NDYj8qOCj9u73LcroTk97 4gm/0NrtONR8RPgIP6xxIi+IOch6scV8XBlO/De7VjgpSqsSySEAYGbt29XDRZMoO20j lXm7t0ARfbkgKBXMgmlLTTEuaAU5nu28nYNM3niI591unE7MR0yosnrle61rs1MWRhdQ 7oEAcGk2X4iD2azd8loXaLiHtXgy/gWGf5FTYQmRU7xMxUpFkUA1GL/ABqyZ5g4kW+a7 m9Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=s3kG3KqpAnKRH5jTko+cRrztEEz97e1+OTJ8o0f3khA=; b=hNobx0FNYfxBx26KPYgpm2tVQQexBRnj9VjUOBa6a1z7gksn7fEJ9cUlaFzm5VNXJ5 vGMcY7riV8wJNhfa337+7dSWCkSGvIU74c2Q+opahCFZ5rd3T7/WClRkZP/UohaSWybS Fq3S0r5dMdGKtFENModPRIKSqstK6XQSHh4WtpGsJ4eNn1k6Nu3p24+NiZPR5FZICvrq ecj/tkLi3PjApPI0eUvilJrzW+INTU3pXIsjWBozl3pWiuh6LAjDBG/y+LhCrMUfTtST rL9JORyWcRi8F8dyn8Q4TWQh1oQShTFoXDbChgC8booxA/7qfo0GUOLCq/xnsAzbig9g lxHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mUnYM0Qa; 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=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b5-20020a17090a6ac500b0023431aa18b0si3722686pjm.146.2023.02.15.20.41.19; Wed, 15 Feb 2023 20:41:40 -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=@gmail.com header.s=20210112 header.b=mUnYM0Qa; 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=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229534AbjBPERE (ORCPT + 99 others); Wed, 15 Feb 2023 23:17:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229568AbjBPERB (ORCPT ); Wed, 15 Feb 2023 23:17:01 -0500 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 590C342BC3; Wed, 15 Feb 2023 20:16:56 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id w20-20020a17090a8a1400b00233d7314c1cso4557407pjn.5; Wed, 15 Feb 2023 20:16:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=s3kG3KqpAnKRH5jTko+cRrztEEz97e1+OTJ8o0f3khA=; b=mUnYM0Qa+8NUCow7tVpQ/S4tAWTr6fRV8xufSzVM9ckuZWVQN/RDLTJRnZu5UZ/Ifd trfNb8rM5YNXnhY2LrpjHIqEX6QjrgPNxr3xB+0IeGlcQEqLAdKHMrykkyL5AiIREvuz 2B7tnWWsKPY0RRb2vMIjh72YJdizV2N2a1sat+daN9AfiGXDK3BUiZDlpi+7bZy/JN2U f6eiPMXstHQEfFo/Ob4ybw3/B6eLZx0PMZ96gGX5i6mQdbMSucUuoNRoQ3q4cDztdlzn KhyDLIvTOzaD48qw/UHitsSAGs6aDHGvAhn6WGhoFoh2pFSkEMRcg25Zli+T+v/nQPLy p4Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=s3kG3KqpAnKRH5jTko+cRrztEEz97e1+OTJ8o0f3khA=; b=Y0c3Ac/priMgAKukF02dmEthD+xyHTVQ+gfATGGbT69eonZnDIwAaL/Hc72mQXYbuY gwoek5XSqWZBZUEMrdTe1mA2gOaMr6Zoqm6q4PW2W/wi2kSsSzAYSimgu0S6mtfq5EUI jVzkwjHOoz1uj0nA037JWLUazgt8rwyr4kfy1qmfyvTEvBIUUPgVHWLtqfFDVPNiUsGP Gco3p0YaFXuWUgDKv9olu1VZ3uzcqZJkYnz7x/dbLNDQ0TXO6T4DSkKNKq65y/RhUPmb IFva0lAzGGIsvNYnqSr5KUv5RcCmNX+26Zh9jN5bFyWR8QTUomSm/cT/7Igcm4TM6617 5jhQ== X-Gm-Message-State: AO0yUKUh1Q/uwtcDMFF5wtZ9Jk4ofsAKPrk0S0uTUe6Z3OKstNtWVcLy OA/4hxWJ3Wb8yuOPV9e7NxJ6JZfWgJrWMg== X-Received: by 2002:a17:902:fa90:b0:19a:853e:92ef with SMTP id lc16-20020a170902fa9000b0019a853e92efmr3533882plb.46.1676521015602; Wed, 15 Feb 2023 20:16:55 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id ij21-20020a170902ab5500b00198d1993b4esm157539plb.69.2023.02.15.20.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Feb 2023 20:16:55 -0800 (PST) From: Orlando Chamberlain To: linux-input@vger.kernel.org Cc: Jonathan Corbet , Jiri Kosina , Benjamin Tissoires , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Pavel Machek , Aditya Garg , Aun-Ali Zaidi , Kerem Karabay , Andy Shevchenko , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Orlando Chamberlain Subject: [PATCH 2/2] HID: apple-magic-backlight: Add driver for keyboard backlight on internal Magic Keyboards Date: Thu, 16 Feb 2023 15:12:28 +1100 Message-Id: <20230216041224.4731-3-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230216041224.4731-1-orlandoch.dev@gmail.com> References: <20230216041224.4731-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757961257256661161?= X-GMAIL-MSGID: =?utf-8?q?1757961257256661161?= 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 Co-developed-by: Kerem Karabay Signed-off-by: Kerem Karabay --- MAINTAINERS | 6 ++ drivers/hid/Kconfig | 13 +++ drivers/hid/Makefile | 1 + drivers/hid/apple-magic-backlight.c | 125 ++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 drivers/hid/apple-magic-backlight.c diff --git a/MAINTAINERS b/MAINTAINERS index fb1471cb5ed3..3319f0c3ed1e 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 +L: linux-input@vger.kernel.org +S: Maintained +F: drivers/hid/apple-magic-backlight.c + HID CORE LAYER M: Jiri Kosina M: Benjamin Tissoires diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index e2a5d30c8895..f4702d32ce2f 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -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 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 e8014c1a2f8b..5cbfe85dd31b 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -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) += 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 000000000000..ed5bcf5bb599 --- /dev/null +++ b/drivers/hid/apple-magic-backlight.c @@ -0,0 +1,125 @@ +// 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 + * Copyright (c) 2023 Orlando Chamberlain + */ + +#include +#include +#include +#include +#include +#include + +#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, u16 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 && hdev->collection[0].usage == HID_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 = ":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 = "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 "); +MODULE_LICENSE("GPL");