From patchwork Sun Dec 29 09:42:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 13922633 X-Patchwork-Delegate: jikos@jikos.cz Received: from PNYPR01CU001.outbound.protection.outlook.com (mail-centralindiaazolkn19010012.outbound.protection.outlook.com [52.103.68.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80E0D282F1; Sun, 29 Dec 2024 09:42:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.68.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735465381; cv=fail; b=ayAgCWPWLWcCH/BH+HSfbaMBoWqnk9zCm4uOzXTZQKe4A2gYSOErgOpEezSTK3NMgG60GbmexqS2KIMsV1xvm1h/Zw6D0mCv8Jrq57RA7tfp0OshAtxc2adgucZfp3ImgQEIzJjkYRBg2nN9N5vBoSgobbp7d8h7SUd0l8CeZDY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735465381; c=relaxed/simple; bh=JBg4oY8JLDISqSDF1ROly9ZHI/+V2b7TZtxrF8NJZtA=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=gAn11tjC8DGgqux9dYo1SEYKang9O77b+2TMmBNJMfPKZ6QUpNgpKVP045iJ0+XLCQ1kBudJKYDxwASbH+GyS2b+Iyj1Tkk7OT8ojiWCVrj6rooTGy/oVw5Run4wYNOAIeJyucf7Bb0KXkw4GrzXoGioXepCnL1qc9fClHtESHI= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com; spf=pass smtp.mailfrom=live.com; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b=ZAkUYuxi; arc=fail smtp.client-ip=52.103.68.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=live.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b="ZAkUYuxi" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xBAMR4VPTO/nFFkTHNDrFxpT0HrOiHFU3/oFQ+GUaH6Izzbo1H81HcNu3oarL7ZyIUSC66BxgXKu2OEhGA6UCCu8o6M9WHhTyjzPtsmGOyPlYtznyZZd5nnNtDwBrQ1xwIdHG41o1e62pc417EMXSOuMZokXAx3v4iEI8HqtU/FWrhbpjPN8t/Dpkd2R1/WAYeWOQwD9HZQQXrXQkjfjXkqs/48jIVqWAaajMk44CJBL0PxRyGRRxaqGH0T2BqhATiBakf0g3NWs+bqiCKOPzZwvh1UFWgnOx29N2hD0Xz1miMQrhsjxdmxueZzmYe7b33t6nlDZvsD8vOkfSRyxhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=JBg4oY8JLDISqSDF1ROly9ZHI/+V2b7TZtxrF8NJZtA=; b=pGLfxlFK63KImzwGQ/7pG3FyKMkjqtVRZnIBafEuIQuLwc6R4Jg1SctYfYMfssFtI+EeOdeb1pTpbVi2eln3es7uOh9GhugdRdhKZsNGpBRFkJstEiiyTi28lD8G0D+00VgM3TlukfJe5DldS/YOwh9Ws2nGtmgG7DnhzrwrrFcXUAoAkmiduLoWwPBtJmL2sJM9OcaHsfs+gaIuOCnLfxdJO0h/YWe2/I7LMWSJHdQXObCNzSeCdZOYMW0KuWlKY5fdObSeRjcOMrVeWNlnWO2SbaA3D6/f+9oiamVRcQxQFTelrwHP5UIqzBshfQYeO2W49qyC/ZBWrTcfgliUMA== 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=JBg4oY8JLDISqSDF1ROly9ZHI/+V2b7TZtxrF8NJZtA=; b=ZAkUYuxig4M3Nbw1vsUeElMVGbEl102rZxFh7gnxDiYLdH+FFGYjU32mEQP7jADmYORvvvRfYqS8a/u89P12f/+XGDMBHaRFegCCcR8zq0ozwKKJg2EhHllqyRo0+TZA1qdCgBv2zBvrEVTsKDJJI3T0jjNsAPf1S6yHB8tojpYBhW3w7PeMeGAagLyTNjMlUSAeIGnGfFglZGUsMSAXNMBnx8OLRcO1/wz8xE4V7C9tkPfTDED2WDvn02+2BzfE2+vG1iDP6wxQVDoEjGakIN/6HsIRx8l9c2OxXMRViDhclysCE+363KUZ4SD3OU90WQw5VEsS0VtPp8uG41Q0VA== Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c01:cd::12) by MA0PR01MB7898.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a01:1c::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8293.18; Sun, 29 Dec 2024 09:42:54 +0000 Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06]) by PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06%3]) with mapi id 15.20.8293.000; Sun, 29 Dec 2024 09:42:54 +0000 From: Aditya Garg To: "jikos@kernel.org" , Benjamin Tissoires , "bentiss@kernel.org" CC: Kerem Karabay , Orlando Chamberlain , "linux-input@vger.kernel.org" , Linux Kernel Mailing List Subject: [PATCH 1/4] HID: hid-appletb-bl: add driver for the backlight of Apple Touch Bars Thread-Topic: [PATCH 1/4] HID: hid-appletb-bl: add driver for the backlight of Apple Touch Bars Thread-Index: AQHbWdYIZZSB+6FUGESIDO5+AECisA== Date: Sun, 29 Dec 2024 09:42:54 +0000 Message-ID: References: <793F0F3B-D51C-49C8-9236-124CF130E833@live.com> In-Reply-To: <793F0F3B-D51C-49C8-9236-124CF130E833@live.com> Accept-Language: en-IN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PN3PR01MB7728:EE_|MA0PR01MB7898:EE_ x-ms-office365-filtering-correlation-id: 91a79b6b-15e6-4428-0988-08dd27ed2b5d x-microsoft-antispam: BCL:0;ARA:14566002|8060799006|7092599003|461199028|19110799003|8062599003|15080799006|102099032|440099028|3412199025; x-microsoft-antispam-message-info: =?utf-8?q?f8OOINrAtNMdnw5464OcUgf/80/Hcpm?= =?utf-8?q?pbMc3ub3M+BOuTo7WIWl7drCaeUyFhd/UR31uJVS8xHl4sVVY3Otezo000sBZuQk0?= =?utf-8?q?1MZ+fr4StZuSNTyYoTeAuI8neVyvkIekOMUndaTeJLvYx/BboZOE019Y4DU+bR4Cw?= =?utf-8?q?pOSSVpJs6EJ6q9ZxVa6cx9aqwL2HnumK2gyKn/1wJSHQUEjvR8ucnsiU8+LtQS3QP?= =?utf-8?q?wQ1WmROF6/lY0tqJGZT9wRpFF1lQK8GLjvv+nx5wH3H8AYNv/P5Zvdw8A+C4wnjuw?= =?utf-8?q?UzOJ0pNNU9tU6PtzIZP1gmL8rgsq0stCeffkqn/f1b3V1wv/FQBHZru35ImL5YqlJ?= =?utf-8?q?cmZbdpIv4n4SGS2+asYoiaayROnMWLQsdwFtQR4BnUcHpQGcWKwtmaYDh8uX5j5zu?= =?utf-8?q?bJii3y18WsFOduCeHJZF0ij5qKE2Fwz7cYFSeZL+Xyt87YcsYqvu0o+fFU05ytaVv?= =?utf-8?q?U8cMue6CGGVjTYLnVLcSVxIaPG0nX0y82GLhhe+GBnMNuXbHz4I1Iu+ZYwuvZ/fql?= =?utf-8?q?3DJqV0K+mfmetB/vp29tLEiwC7yYBrSo4V566M7eRNq2KbSiAKjE2Xyazg1qmVatg?= =?utf-8?q?e+C0uTgqfniv2kaRYNzgno3p5/fp36W/zPecpv+EwhaN6ZPuudyxwaJViRdlz5v3h?= =?utf-8?q?F4unmElk4PLQYBI3W6lWrT+Q8ubAFl3Uu944F2WfVEjokH0cesKGbDUGqijoDYlqq?= =?utf-8?q?tpi76cIvaVqIiBjtrRB3oZAfvSZorCEQJLIHMeQTowsqvozeneUMMcUt+2xEll2V9?= =?utf-8?q?00X1RcquQDDgDoQCVYLVV+T2NSc5w91CKj3hbdF9yJ3bKwsXF+6wIx03a2F4QkB1p?= =?utf-8?q?HbzsC/pnwKmuk3TU/80utaAnqXaNh7YwgOTs3g+GTDsD4PQnyoG7JEfClAWu6oW9t?= =?utf-8?q?QmFDiAJQMupY5IAPum49L8sl4+5OsqbitybDlStuEcPjOtQ+D0p2biUzDdEII18tP?= =?utf-8?q?Di1xSQvckUd3lTRMyN0IYA4TXtaKhbdKqsw/b1yS4I1zFo4o0DrVPV6V8BB0jGPxX?= =?utf-8?q?U6ToqaigWGePWg4yhoyee80DDgEkLYuiUBccm0Q=3D=3D?= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?rk05wrJGkCoWsvEhWVrJ18csXglx?= =?utf-8?q?rrrWGJSTgvyOH3U2IFW5YYFLGqqCvxe7vBW/eES7SJEOjT61v5th0MaBh9yNx610M?= =?utf-8?q?9QP09ZmvanRg7JYY5+j4rQyC94hw3Mi83ZyhLbAMV3FiJmwTKZC4OLdYEyUyxZoKJ?= =?utf-8?q?ZGK77OeTK5MpJJHPX2F7r8bOWFXOqSIn1zf9Ui5EsZGjLiMGt15VNcy78J9bQpv9S?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PZ5EzKlh7ZzbKTHvn/SAx7NH8cPJljVkIthOBE1YYDc5yjzGUO5Pw5S1bg3+WeEF7?= =?utf-8?q?hlrgaXAQxqIUZy4pZLBcyv5hrM2cwmWZWgnSit2Ldi4ppWbf5Msa5I3fByRaOgrtc?= =?utf-8?q?Dt6XDIGqDOemB1xuEZVmEWZpIjN0ObNlLEaiTxXnxaw9dDZW+eox1IF6xmRqtAdIx?= =?utf-8?q?agivVzenEQzHksjC?= Content-ID: <6AAE23BAEBD87D4FA893982B925FEAD8@INDPRD01.PROD.OUTLOOK.COM> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-7719-20-msonline-outlook-ae5c4.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 91a79b6b-15e6-4428-0988-08dd27ed2b5d X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Dec 2024 09:42:54.4192 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MA0PR01MB7898 From: Kerem Karabay This patch adds a driver for the backlight of Apple Touch Bars on x86 Macs. Note that currently only T2 Macs are supported. This driver is based on previous work done by Ronald Tschalär . Signed-off-by: Kerem Karabay Co-developed-by: Aditya Garg Signed-off-by: Aditya Garg --- drivers/hid/Kconfig | 10 ++ drivers/hid/Makefile | 1 + drivers/hid/hid-appletb-bl.c | 207 +++++++++++++++++++++++++++++++++++ drivers/hid/hid-quirks.c | 4 +- 4 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 drivers/hid/hid-appletb-bl.c diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 4d2a89d65..f6678db27 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -148,6 +148,16 @@ config HID_APPLEIR Say Y here if you want support for Apple infrared remote control. +config HID_APPLETB_BL + tristate "Apple Touch Bar Backlight" + depends on BACKLIGHT_CLASS_DEVICE + help + Say Y here if you want support for the backlight of Touch Bars on x86 + MacBook Pros. + + To compile this driver as a module, choose M here: the + module will be called hid-appletb-bl. + config HID_ASUS tristate "Asus" depends on USB_HID diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 24de45f36..444d24cec 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -29,6 +29,7 @@ obj-$(CONFIG_HID_ALPS) += hid-alps.o obj-$(CONFIG_HID_ACRUX) += hid-axff.o obj-$(CONFIG_HID_APPLE) += hid-apple.o obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o +obj-$(CONFIG_HID_APPLETB_BL) += hid-appletb-bl.o obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o obj-$(CONFIG_HID_ASUS) += hid-asus.o obj-$(CONFIG_HID_AUREAL) += hid-aureal.o diff --git a/drivers/hid/hid-appletb-bl.c b/drivers/hid/hid-appletb-bl.c new file mode 100644 index 000000000..819157686 --- /dev/null +++ b/drivers/hid/hid-appletb-bl.c @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Apple Touch Bar Backlight Driver + * + * Copyright (c) 2017-2018 Ronald Tschalär + * Copyright (c) 2022-2023 Kerem Karabay + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include + +#include "hid-ids.h" + +#define APPLETB_BL_ON 1 +#define APPLETB_BL_DIM 3 +#define APPLETB_BL_OFF 4 + +#define HID_UP_APPLEVENDOR_TB_BL 0xff120000 + +#define HID_VD_APPLE_TB_BRIGHTNESS 0xff120001 +#define HID_USAGE_AUX1 0xff120020 +#define HID_USAGE_BRIGHTNESS 0xff120021 + +static int appletb_bl_def_brightness = 2; +module_param_named(brightness, appletb_bl_def_brightness, int, 0444); +MODULE_PARM_DESC(brightness, "Default brightness:\n" + " 0 - Touchbar is off\n" + " 1 - Dim brightness\n" + " [2] - Full brightness"); + +struct appletb_bl { + struct hid_field *aux1_field, *brightness_field; + struct backlight_device *bdev; + + bool full_on; +}; + +static const u8 appletb_bl_brightness_map[] = { + APPLETB_BL_OFF, + APPLETB_BL_DIM, + APPLETB_BL_ON, +}; + +static int appletb_bl_set_brightness(struct appletb_bl *bl, u8 brightness) +{ + struct hid_report *report = bl->brightness_field->report; + struct hid_device *hdev = report->device; + int ret; + + ret = hid_set_field(bl->aux1_field, 0, 1); + if (ret) { + hid_err(hdev, "Failed to set auxiliary field (%pe)\n", ERR_PTR(ret)); + return ret; + } + + ret = hid_set_field(bl->brightness_field, 0, brightness); + if (ret) { + hid_err(hdev, "Failed to set brightness field (%pe)\n", ERR_PTR(ret)); + return ret; + } + + if (!bl->full_on) { + ret = hid_hw_power(hdev, PM_HINT_FULLON); + if (ret < 0) { + hid_err(hdev, "Device didn't power on (%pe)\n", ERR_PTR(ret)); + return ret; + } + + bl->full_on = true; + } + + hid_hw_request(hdev, report, HID_REQ_SET_REPORT); + + if (brightness == APPLETB_BL_OFF) { + hid_hw_power(hdev, PM_HINT_NORMAL); + bl->full_on = false; + } + + return 0; +} + +static int appletb_bl_update_status(struct backlight_device *bdev) +{ + struct appletb_bl *bl = bl_get_data(bdev); + u8 brightness; + + if (backlight_is_blank(bdev)) + brightness = APPLETB_BL_OFF; + else + brightness = appletb_bl_brightness_map[backlight_get_brightness(bdev)]; + + return appletb_bl_set_brightness(bl, brightness); +} + +static const struct backlight_ops appletb_bl_backlight_ops = { + .options = BL_CORE_SUSPENDRESUME, + .update_status = appletb_bl_update_status, +}; + +static int appletb_bl_probe(struct hid_device *hdev, const struct hid_device_id *id) +{ + struct hid_field *aux1_field, *brightness_field; + struct backlight_properties bl_props = { 0 }; + struct device *dev = &hdev->dev; + struct appletb_bl *bl; + int ret; + + ret = hid_parse(hdev); + if (ret) + return dev_err_probe(dev, ret, "HID parse failed\n"); + + aux1_field = hid_find_field(hdev, HID_FEATURE_REPORT, + HID_VD_APPLE_TB_BRIGHTNESS, HID_USAGE_AUX1); + + brightness_field = hid_find_field(hdev, HID_FEATURE_REPORT, + HID_VD_APPLE_TB_BRIGHTNESS, HID_USAGE_BRIGHTNESS); + + if (!aux1_field || !brightness_field) + return -ENODEV; + + if (aux1_field->report != brightness_field->report) + return dev_err_probe(dev, -ENODEV, "Encountered unexpected report structure\n"); + + bl = devm_kzalloc(dev, sizeof(*bl), GFP_KERNEL); + if (!bl) + return -ENOMEM; + + ret = hid_hw_start(hdev, HID_CONNECT_DRIVER); + if (ret) + return dev_err_probe(dev, ret, "HID hardware start failed\n"); + + ret = hid_hw_open(hdev); + if (ret) { + dev_err_probe(dev, ret, "HID hardware open failed\n"); + goto stop_hw; + } + + bl->aux1_field = aux1_field; + bl->brightness_field = brightness_field; + + if (appletb_bl_def_brightness == 0) + ret = appletb_bl_set_brightness(bl, APPLETB_BL_OFF); + else if (appletb_bl_def_brightness == 1) + ret = appletb_bl_set_brightness(bl, APPLETB_BL_DIM); + else + ret = appletb_bl_set_brightness(bl, APPLETB_BL_ON); + + if (ret) { + dev_err_probe(dev, ret, "Failed to set touch bar brightness to off\n"); + goto close_hw; + } + + bl_props.type = BACKLIGHT_RAW; + bl_props.max_brightness = ARRAY_SIZE(appletb_bl_brightness_map) - 1; + + bl->bdev = devm_backlight_device_register(dev, "appletb_backlight", dev, bl, + &appletb_bl_backlight_ops, &bl_props); + if (IS_ERR(bl->bdev)) { + ret = PTR_ERR(bl->bdev); + dev_err_probe(dev, ret, "Failed to register backlight device\n"); + goto close_hw; + } + + hid_set_drvdata(hdev, bl); + + return 0; + +close_hw: + hid_hw_close(hdev); +stop_hw: + hid_hw_stop(hdev); + + return ret; +} + +static void appletb_bl_remove(struct hid_device *hdev) +{ + struct appletb_bl *bl = hid_get_drvdata(hdev); + + appletb_bl_set_brightness(bl, APPLETB_BL_OFF); + + hid_hw_close(hdev); + hid_hw_stop(hdev); +} + +static const struct hid_device_id appletb_bl_hid_ids[] = { + /* MacBook Pro's 2018, 2019, with T2 chip: iBridge DFR Brightness */ + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) }, + { } +}; +MODULE_DEVICE_TABLE(hid, appletb_bl_hid_ids); + +static struct hid_driver appletb_bl_hid_driver = { + .name = "hid-appletb-bl", + .id_table = appletb_bl_hid_ids, + .probe = appletb_bl_probe, + .remove = appletb_bl_remove, +}; +module_hid_driver(appletb_bl_hid_driver); + +MODULE_AUTHOR("Ronald Tschalär"); +MODULE_AUTHOR("Kerem Karabay "); +MODULE_DESCRIPTION("MacBookPro Touch Bar Backlight Driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c index e0bbf0c63..818d41a35 100644 --- a/drivers/hid/hid-quirks.c +++ b/drivers/hid/hid-quirks.c @@ -328,7 +328,6 @@ static const struct hid_device_id hid_have_special_driver[] = { { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) }, { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021) }, { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021) }, - { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) }, { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) }, #endif #if IS_ENABLED(CONFIG_HID_APPLEIR) @@ -338,6 +337,9 @@ static const struct hid_device_id hid_have_special_driver[] = { { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL4) }, { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL5) }, #endif +#if IS_ENABLED(CONFIG_HID_APPLETB_BL) + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) }, +#endif #if IS_ENABLED(CONFIG_HID_ASUS) { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD) }, { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD) }, From patchwork Sun Dec 29 09:43:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 13922634 Received: from PNYPR01CU001.outbound.protection.outlook.com (mail-centralindiaazolkn19010012.outbound.protection.outlook.com [52.103.68.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6AD878F36; Sun, 29 Dec 2024 09:44:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.68.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735465444; cv=fail; b=JaGqzGbam30+ndUcably/SnbCLRor2WYXDq9axSnMBxnkj2yc9xjq5s6xV5EO867QSeq+qd+g0eywdpnUOlw3NFrIkzz2LDvlat1pxbOFm6BmfYGsgMZ2H/Z1EN6UV7fn77T/TVHiv0fIq+4qYh8VSTTKhbtUUgJYkffLC30SZo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735465444; c=relaxed/simple; bh=nQ/DwVgFVO3Vllcf1AzVKw4C/c+9cDooz0F7eTtoE7Q=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=i/qR6ZkMeMOUWtJutYJ790AmQK0Ih7hTzedNGWiTbs9SbdjzduK+7cbtWtnYkzX0UDwWaupB6ai2gEueSCa66Sv2bSjw8kg51i33BnJyqKKbdC+heDzWd0eUp3uX3ls4CBBYHFuZfaHsu+N2B03cAUg9tOEO3oV1L/IVWDRkcTM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com; spf=pass smtp.mailfrom=live.com; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b=leBVw/se; arc=fail smtp.client-ip=52.103.68.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=live.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b="leBVw/se" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pVJGEqg0XGScLcaTy+SW6Ql48v5uka1F/kQWzkzAo0rcfrkIIdWRyKjQrOA8h7gqrHXEBAtgtA6YoNFSOxUAr09svHW3KEr2Gq5xX69oT7N5AJp0B4VO6rGakhm0q2KGx6ShTBZ2TCZOgDR6GA3klbmQAzAJJLKCeD1PcScFLXyqZDgX5jfqWYoL30pT6gipL5QrhVJA6vgGMh6F4cMWQnWyK+WDVWepMxonyenpHGKhCi0EEWIgUM9GsulzuC6ii2szcNL1RvuZ/yDr06KNR7yRYyTF4FYu/BSAgSUSey4WH3PLPvHe4w2ZO3OCkpJxyHD4Q6c2PB7ld/G7mHJbHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=nQ/DwVgFVO3Vllcf1AzVKw4C/c+9cDooz0F7eTtoE7Q=; b=wbUJtRZe1JD07ikHohxpmRHqUOz1OdG3p8BegK/uZrP0QCwZeiIsF1vJlPOTx+kIPjtwa/HgGZZREgDyHXlk0sHeG+meEMLXftCHXE2Ff4oB0fQbR8ZFaVrE1FsarTwMTehmvez4iL5W9XPFV3frp3yZ3Zn1jCnMBk0DG+2LYERDmnf4if50KVpVDoJviVqkocFYySUth9PG2z17m/UgLobZ9kWNvaqhudMox+2aXTsgV037jx/4G1Qg77dZAbRAUqGO+yhYLJELHnnMyEnDb91rApAIg3XWb5L0ZmCvrZp0avQW4+ZsX83zSf9+G4/RuNkd0A6Cq76RVe2CSPhp+w== 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=nQ/DwVgFVO3Vllcf1AzVKw4C/c+9cDooz0F7eTtoE7Q=; b=leBVw/se9dK+J/PDwFrOamX6pisNyUQx/0a8fsMf86QpiWc+Rsp9BzqFdPwC2UVyjDYjCz1NIp6xdTcbQxZMDhe6fOBPFiv248PFv5lve3XpqWF0GqHk0KlVU15VuUhoZZxTjVEin6fUB3sYtQC4ekhOr774oGSLN1NvMUMXz2rZ+Z4J4hZmKlnrbJ/eOxdFp4ovNiYmha5m6b3RP9WlIhD+XaqplYita3ezwcS/PYrr6NMSWgqOI7r4e57eaqhqIqAUy7p3O4kzYbXrYE3gQaOFDeHGrci07BpD3FpruIuctoyPidCzsmJPLPq3KAbV0v4dtuzsXeIL+l24g6ax0Q== Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c01:cd::12) by MA0PR01MB7898.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a01:1c::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8293.18; Sun, 29 Dec 2024 09:43:57 +0000 Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06]) by PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06%3]) with mapi id 15.20.8293.000; Sun, 29 Dec 2024 09:43:57 +0000 From: Aditya Garg To: "jikos@kernel.org" , Benjamin Tissoires , "bentiss@kernel.org" CC: Kerem Karabay , Orlando Chamberlain , "linux-input@vger.kernel.org" , Linux Kernel Mailing List Subject: [PATCH 2/4] HID: hid-appletb-kbd: add driver for the keyboard mode of Apple Touch Bars Thread-Topic: [PATCH 2/4] HID: hid-appletb-kbd: add driver for the keyboard mode of Apple Touch Bars Thread-Index: AQHbWdYuylyprycvE0+t9mFJBj4wEg== Date: Sun, 29 Dec 2024 09:43:56 +0000 Message-ID: References: <793F0F3B-D51C-49C8-9236-124CF130E833@live.com> In-Reply-To: <793F0F3B-D51C-49C8-9236-124CF130E833@live.com> Accept-Language: en-IN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PN3PR01MB7728:EE_|MA0PR01MB7898:EE_ x-ms-office365-filtering-correlation-id: 6fe55142-5f5f-4cac-87ff-08dd27ed50a7 x-microsoft-antispam: BCL:0;ARA:14566002|8060799006|7092599003|461199028|19110799003|8062599003|15080799006|102099032|440099028|3412199025; x-microsoft-antispam-message-info: =?utf-8?q?wZLvy54rNiVxgSGIJeSIe6CLetL4cxa?= =?utf-8?q?R++/9BssP0YYsaht27D9dxw9Z7JFsTbyiRuFa4AILL9BGkVNKCZqhtgGiAS1YYVI5?= =?utf-8?q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eowRvcQmXVLnXx/pJUXFkgaArd5vWxDVf9ChWZ7X6VX0JiHyJt+?= =?utf-8?q?4CW9CXEVm6h1EVcm6keVwpTYSsAw2sFMLTKvQhA=3D=3D?= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?QFCJ555h72YzpOCskhNhdlYDpt3U?= =?utf-8?q?8foY3ONeMAIdNHEEZ1MltgHW42sauycny9FhweHS8FTdGx4YDetjJ4mN37FJsE1z2?= =?utf-8?q?HBEwJIBnd5LgCJOr/U4JSs3UPTaqMGAId9FUaVrSk6Y7PdyM8oHaQO7eiQehPL3UT?= =?utf-8?q?crENr4krZtPy+hiqRgjGxyzKBPGNMNnxgE0C8YpXu0DKbnoT4Bwky7Uwfem7UFVSq?= =?utf-8?q?VDsEwNVEfKjEv2KEdnJLzKjoTrhb3Hchl5jIDF2kMMJSeyp2dx4qQbOZOoaECOjI+?= =?utf-8?q?74Kyfj20/aSkN0mXPhqBxJkj8WTn9rg2nUB13mlksSuB5wwwgBdvfzthHfTf2chdZ?= =?utf-8?q?QKAy9s4ajU7pgBCrMNFhYjkwzuEYdKXzkvhuZzE+207x2iADB35jDuuJ0gov4v3C2?= =?utf-8?q?91W/PMikaMqLbWq4oF6KGgoyi9YNcvZI5yf3+mO+KRBDhCPplCFKMqLbaZLYkvDqp?= =?utf-8?q?TX7wAhWw+MQQY6361urLqgaoS7EJ4I8m6mQ6wO+fDqw76DiGqTFknUJE+lggC3lUH?= =?utf-8?q?4iznFZfx1+lTkiUATmMYJtDuKdP0a3GRVfofLmdo6AthcSkuZAMfk88OCBwIcEaEt?= =?utf-8?q?RChGCHacnE09RQqoUuV87Zf4RrGYoF98mdDGFxuhIKDh2d2/j9Wl4zb3eWuufpz9c?= =?utf-8?q?fsnTXIpCAz1/osFDYwpiK91YtcUk3cQM5rKBPIOPfmgwWUlSwG8FdaEhbT9qRo1bp?= =?utf-8?q?wINZy8gNESuQugdp34/bdPJmdnrraHxJNtUJ8pPibvsqkAbZ/two/HEx6xZxzP/ku?= =?utf-8?q?Wx7yGhsOJPLNCJPGmExY0ij7FoqTQPaLIOyA1HhAbNNuTHvbrJtuIzy60EgmeqRxe?= =?utf-8?q?SymWXwKF5labVWtSlAIsWqoj2O1Yn6KisYDUnkjF4ARvN8KN2LfRdo0BFl+SSXpbJ?= =?utf-8?q?KbO45lBHpmgPFfctQgey5B2Ct/fEbYYS5swcw4j9Pz9pvZKcKKbONtHXIkY3XWi8F?= =?utf-8?q?ISuL6OUx7aOw20VpH84TLeIBli5Med/8yFsTX39igc7OnLoRwvhQ1Qu+C0snDTB5n?= =?utf-8?q?gEsDF3RyUkFp/s2qWCeCHD699iI21a4fSx6sYN3mHgwDW0KjGD8FgVrXcK+89pgVi?= =?utf-8?q?9vsgFznwQobdcR2X7BZOEg7k1cr1aNhfuWvzjcwoDdIxeXLZgwV1nLuBqDXxQ9PCz?= =?utf-8?q?p4jc+8Fry8VpLi0CaNngYqpeMkkSVsGqGQMwC7arboIk66Pii8iD2CjZM6uzh/2oi?= =?utf-8?q?hcYpNSI6+SbNxMxP4Naws7ICp42wcNo3EvmQsJkwK/oQGgWiIYKgubuVmbggAmGK0?= =?utf-8?q?wK31UwNLM59rKCBk?= Content-ID: <2F6BC0AE33ADC94789B0ADF5216CB7C4@INDPRD01.PROD.OUTLOOK.COM> Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-7719-20-msonline-outlook-ae5c4.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 6fe55142-5f5f-4cac-87ff-08dd27ed50a7 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Dec 2024 09:43:57.0024 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MA0PR01MB7898 From: Kerem Karabay The Touch Bars found on x86 Macs support two USB configurations: one where the device presents itself as a HID keyboard and can display predefined sets of keys, and one where the operating system has full control over what is displayed. This commit adds a driver for the display functionality of the first configuration. Note that currently only T2 Macs are supported. This driver is based on previous work done by Ronald Tschalär . Signed-off-by: Kerem Karabay Co-developed-by: Aditya Garg Signed-off-by: Aditya Garg --- .../ABI/testing/sysfs-driver-hid-appletb-kbd | 13 + drivers/hid/Kconfig | 13 + drivers/hid/Makefile | 1 + drivers/hid/hid-appletb-kbd.c | 303 ++++++++++++++++++ drivers/hid/hid-quirks.c | 4 +- 5 files changed, 333 insertions(+), 1 deletion(-) create mode 100644 Documentation/ABI/testing/sysfs-driver-hid-appletb-kbd create mode 100644 drivers/hid/hid-appletb-kbd.c diff --git a/Documentation/ABI/testing/sysfs-driver-hid-appletb-kbd b/Documentation/ABI/testing/sysfs-driver-hid-appletb-kbd new file mode 100644 index 000000000..2a19584d0 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-driver-hid-appletb-kbd @@ -0,0 +1,13 @@ +What: /sys/bus/hid/drivers/hid-appletb-kbd//mode +Date: September, 2023 +KernelVersion: 6.5 +Contact: linux-input@vger.kernel.org +Description: + The set of keys displayed on the Touch Bar. + Valid values are: + == ================= + 0 Escape key only + 1 Function keys + 2 Media/brightness keys + 3 None + == ================= diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index f6678db27..5b16bfadc 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -158,6 +158,19 @@ config HID_APPLETB_BL To compile this driver as a module, choose M here: the module will be called hid-appletb-bl. +config HID_APPLETB_KBD + tristate "Apple Touch Bar Keyboard Mode" + depends on USB_HID + depends on INPUT + select INPUT_SPARSEKMAP + help + Say Y here if you want support for the keyboard mode (escape, + function, media and brightness keys) of Touch Bars on x86 MacBook + Pros. + + To compile this driver as a module, choose M here: the + module will be called hid-appletb-kbd. + config HID_ASUS tristate "Asus" depends on USB_HID diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 444d24cec..1989288e0 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_HID_ACRUX) += hid-axff.o obj-$(CONFIG_HID_APPLE) += hid-apple.o obj-$(CONFIG_HID_APPLEIR) += hid-appleir.o obj-$(CONFIG_HID_APPLETB_BL) += hid-appletb-bl.o +obj-$(CONFIG_HID_APPLETB_KBD) += hid-appletb-kbd.o obj-$(CONFIG_HID_CREATIVE_SB0540) += hid-creative-sb0540.o obj-$(CONFIG_HID_ASUS) += hid-asus.o obj-$(CONFIG_HID_AUREAL) += hid-aureal.o diff --git a/drivers/hid/hid-appletb-kbd.c b/drivers/hid/hid-appletb-kbd.c new file mode 100644 index 000000000..80c87396f --- /dev/null +++ b/drivers/hid/hid-appletb-kbd.c @@ -0,0 +1,303 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Apple Touch Bar Keyboard Mode Driver + * + * Copyright (c) 2017-2018 Ronald Tschalär + * Copyright (c) 2022-2023 Kerem Karabay + * Copyright (c) 2024 Aditya Garg + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hid-ids.h" + +#define APPLETB_KBD_MODE_ESC 0 +#define APPLETB_KBD_MODE_FN 1 +#define APPLETB_KBD_MODE_SPCL 2 +#define APPLETB_KBD_MODE_OFF 3 +#define APPLETB_KBD_MODE_MAX APPLETB_KBD_MODE_OFF + +#define HID_USAGE_MODE 0x00ff0004 + +static int appletb_tb_def_mode = APPLETB_KBD_MODE_SPCL; +module_param_named(mode, appletb_tb_def_mode, int, 0444); +MODULE_PARM_DESC(mode, "Default touchbar mode:\n" + " 0 - escape key only\n" + " 1 - function-keys\n" + " [2] - special keys"); + +struct appletb_kbd { + struct hid_field *mode_field; + + u8 saved_mode; + u8 current_mode; +}; + +static const struct key_entry appletb_kbd_keymap[] = { + { KE_KEY, KEY_ESC, { KEY_ESC } }, + { KE_KEY, KEY_F1, { KEY_BRIGHTNESSDOWN } }, + { KE_KEY, KEY_F2, { KEY_BRIGHTNESSUP } }, + { KE_KEY, KEY_F3, { KEY_RESERVED } }, + { KE_KEY, KEY_F4, { KEY_RESERVED } }, + { KE_KEY, KEY_F5, { KEY_KBDILLUMDOWN } }, + { KE_KEY, KEY_F6, { KEY_KBDILLUMUP } }, + { KE_KEY, KEY_F7, { KEY_PREVIOUSSONG } }, + { KE_KEY, KEY_F8, { KEY_PLAYPAUSE } }, + { KE_KEY, KEY_F9, { KEY_NEXTSONG } }, + { KE_KEY, KEY_F10, { KEY_MUTE } }, + { KE_KEY, KEY_F11, { KEY_VOLUMEDOWN } }, + { KE_KEY, KEY_F12, { KEY_VOLUMEUP } }, + { KE_END, 0 } +}; + +static int appletb_kbd_set_mode(struct appletb_kbd *kbd, u8 mode) +{ + struct hid_report *report = kbd->mode_field->report; + struct hid_device *hdev = report->device; + int ret; + + ret = hid_hw_power(hdev, PM_HINT_FULLON); + if (ret) { + hid_err(hdev, "Device didn't resume (%pe)\n", ERR_PTR(ret)); + return ret; + } + + ret = hid_set_field(kbd->mode_field, 0, mode); + if (ret) { + hid_err(hdev, "Failed to set mode field to %u (%pe)\n", mode, ERR_PTR(ret)); + goto power_normal; + } + + hid_hw_request(hdev, report, HID_REQ_SET_REPORT); + + kbd->current_mode = mode; + +power_normal: + hid_hw_power(hdev, PM_HINT_NORMAL); + + return ret; +} + +static ssize_t mode_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct appletb_kbd *kbd = dev_get_drvdata(dev); + + return sysfs_emit(buf, "%d\n", kbd->current_mode); +} + +static ssize_t mode_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + struct appletb_kbd *kbd = dev_get_drvdata(dev); + u8 mode; + int ret; + + ret = kstrtou8(buf, 0, &mode); + if (ret) + return ret; + + if (mode > APPLETB_KBD_MODE_MAX) + return -EINVAL; + + ret = appletb_kbd_set_mode(kbd, mode); + + return ret < 0 ? ret : size; +} +static DEVICE_ATTR_RW(mode); + +struct attribute *appletb_kbd_attrs[] = { + &dev_attr_mode.attr, + NULL +}; +ATTRIBUTE_GROUPS(appletb_kbd); + +static int appletb_tb_key_to_slot(unsigned int code) +{ + switch (code) { + case KEY_ESC: + return 0; + case KEY_F1 ... KEY_F10: + return code - KEY_F1 + 1; + case KEY_F11 ... KEY_F12: + return code - KEY_F11 + 11; + + default: + return -EINVAL; + } +} + +static int appletb_kbd_hid_event(struct hid_device *hdev, struct hid_field *field, + struct hid_usage *usage, __s32 value) +{ + struct appletb_kbd *kbd = hid_get_drvdata(hdev); + struct key_entry *translation; + struct input_dev *input; + int slot; + + if ((usage->hid & HID_USAGE_PAGE) != HID_UP_KEYBOARD || usage->type != EV_KEY) + return 0; + + input = field->hidinput->input; + + /* + * Skip non-touch-bar keys. + * + * Either the touch bar itself or usbhid generate a slew of key-down + * events for all the meta keys. None of which we're at all interested + * in. + */ + slot = appletb_tb_key_to_slot(usage->code); + if (slot < 0) + return 0; + + translation = sparse_keymap_entry_from_scancode(input, usage->code); + + if (translation && kbd->current_mode == APPLETB_KBD_MODE_SPCL) { + input_event(input, usage->type, translation->keycode, value); + + return 1; + } + + return kbd->current_mode == APPLETB_KBD_MODE_OFF; +} + +static int appletb_kbd_input_configured(struct hid_device *hdev, struct hid_input *hidinput) +{ + int idx; + struct input_dev *input = hidinput->input; + + /* + * Clear various input capabilities that are blindly set by the hid + * driver (usbkbd.c) + */ + memset(input->evbit, 0, sizeof(input->evbit)); + memset(input->keybit, 0, sizeof(input->keybit)); + memset(input->ledbit, 0, sizeof(input->ledbit)); + + __set_bit(EV_REP, input->evbit); + + sparse_keymap_setup(input, appletb_kbd_keymap, NULL); + + for (idx = 0; appletb_kbd_keymap[idx].type != KE_END; idx++) + input_set_capability(input, EV_KEY, appletb_kbd_keymap[idx].code); + + return 0; +} + +static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id *id) +{ + struct appletb_kbd *kbd; + struct device *dev = &hdev->dev; + struct hid_field *mode_field; + int ret; + + ret = hid_parse(hdev); + if (ret) + return dev_err_probe(dev, ret, "HID parse failed\n"); + + mode_field = hid_find_field(hdev, HID_OUTPUT_REPORT, + HID_GD_KEYBOARD, HID_USAGE_MODE); + if (!mode_field) + return -ENODEV; + + kbd = devm_kzalloc(dev, sizeof(*kbd), GFP_KERNEL); + if (!kbd) + return -ENOMEM; + + kbd->mode_field = mode_field; + + ret = hid_hw_start(hdev, HID_CONNECT_HIDINPUT); + if (ret) + return dev_err_probe(dev, ret, "HID hw start failed\n"); + + ret = hid_hw_open(hdev); + if (ret) { + dev_err_probe(dev, ret, "HID hw open failed\n"); + goto stop_hw; + } + + ret = appletb_kbd_set_mode(kbd, appletb_tb_def_mode); + if (ret) { + dev_err_probe(dev, ret, "Failed to set touchbar mode\n"); + goto close_hw; + } + + hid_set_drvdata(hdev, kbd); + + return 0; + +close_hw: + hid_hw_close(hdev); +stop_hw: + hid_hw_stop(hdev); + return ret; +} + +static void appletb_kbd_remove(struct hid_device *hdev) +{ + struct appletb_kbd *kbd = hid_get_drvdata(hdev); + + appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF); + + hid_hw_close(hdev); + hid_hw_stop(hdev); +} + +#ifdef CONFIG_PM +static int appletb_kbd_suspend(struct hid_device *hdev, pm_message_t msg) +{ + struct appletb_kbd *kbd = hid_get_drvdata(hdev); + + kbd->saved_mode = kbd->current_mode; + appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF); + + return 0; +} + +static int appletb_kbd_reset_resume(struct hid_device *hdev) +{ + struct appletb_kbd *kbd = hid_get_drvdata(hdev); + + appletb_kbd_set_mode(kbd, kbd->saved_mode); + + return 0; +} +#endif + +static const struct hid_device_id appletb_kbd_hid_ids[] = { + /* MacBook Pro's 2018, 2019, with T2 chip: iBridge Display */ + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) }, + { } +}; +MODULE_DEVICE_TABLE(hid, appletb_kbd_hid_ids); + +static struct hid_driver appletb_kbd_hid_driver = { + .name = "hid-appletb-kbd", + .id_table = appletb_kbd_hid_ids, + .probe = appletb_kbd_probe, + .remove = appletb_kbd_remove, + .event = appletb_kbd_hid_event, + .input_configured = appletb_kbd_input_configured, +#ifdef CONFIG_PM + .suspend = appletb_kbd_suspend, + .reset_resume = appletb_kbd_reset_resume, +#endif + .driver.dev_groups = appletb_kbd_groups, +}; +module_hid_driver(appletb_kbd_hid_driver); + +MODULE_AUTHOR("Ronald Tschalär"); +MODULE_AUTHOR("Kerem Karabay "); +MODULE_DESCRIPTION("MacBookPro Touch Bar Keyboard Mode Driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c index 818d41a35..7c576d654 100644 --- a/drivers/hid/hid-quirks.c +++ b/drivers/hid/hid-quirks.c @@ -328,7 +328,6 @@ static const struct hid_device_id hid_have_special_driver[] = { { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_GEYSER1_TP_ONLY) }, { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021) }, { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021) }, - { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) }, #endif #if IS_ENABLED(CONFIG_HID_APPLEIR) { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_IRCONTROL) }, @@ -340,6 +339,9 @@ static const struct hid_device_id hid_have_special_driver[] = { #if IS_ENABLED(CONFIG_HID_APPLETB_BL) { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT) }, #endif +#if IS_ENABLED(CONFIG_HID_APPLETB_KBD) + { HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_DISPLAY) }, +#endif #if IS_ENABLED(CONFIG_HID_ASUS) { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_KEYBOARD) }, { HID_I2C_DEVICE(USB_VENDOR_ID_ASUSTEK, USB_DEVICE_ID_ASUSTEK_I2C_TOUCHPAD) }, From patchwork Sun Dec 29 09:44:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 13922635 X-Patchwork-Delegate: jikos@jikos.cz Received: from MA0PR01CU009.outbound.protection.outlook.com (mail-southindiaazolkn19010013.outbound.protection.outlook.com [52.103.67.13]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A03A2282F1; Sun, 29 Dec 2024 09:44:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.67.13 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735465485; cv=fail; b=WZhIm8U2e2ZESxkr/6Sq3L/M3qviyZEJZvo/gFaCKsMWp9nTZctWLsdkMWRBksKKIs9ubfJjhjndPNIv3ENozGIvdilbn0A+hbC/JHLsPV5bs/OBFc7+/NYGOkrRZqcACtS6+u+1n5rBMHnDqW1Dnrtp+aE4GAluMOycdR9Z0hE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735465485; c=relaxed/simple; bh=3mz6Axn8y/+plncvXp/+66j7MYDyHlf4AYZpWN7weFg=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=dsGzwoo2WiGJpAwr4sb414zkyhS/c5z9DSewgMVWgOfzUb6dd1IQuMV4LVhluARre+jdHfgKEAS+NVxdusko4DpsJhsyVDMOQrJmw4e1Icyd0QrsbYnXj6arw68rB0+HETKxnPRDdjROgFOr01kNczBlE5b/nlSces+U3kOI/pc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com; spf=pass smtp.mailfrom=live.com; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b=MXurec+X; arc=fail smtp.client-ip=52.103.67.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=live.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b="MXurec+X" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Gcm58Zywb59HSEYMwp2YyzBb1fu9ZXW60xUHd66hGLaWi7003raHzIMX27v9gmleP905377MTcb45Bdd+JIIrRNJdHQIf0Un6JXYHTPPL+m5ypVWKYz63dHeIMtCOqiO4FvmgupBWlR0Aetizir/Xrs7FAw6WcF4/xYiDZiidbqlbhAfLUokcdoALQabZo4nlW0sI9vvzlY6cdz8nfiYmFoLwYIRAVfIZS9G+cBH03DJcORZIEq3fdJOGgUTNZdwreuavQiSyhhoYxqZ/RLnVZBTldzz8ZuvwS3YjbummUWwlnhYlsbbG+nmMGkXlZ7I6Vs1sETnuRxYxmhDGV9Asg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=1VBoik1et6ZCxTD6sE8zAbSU02w5p31wr/Vj6VJ2+EQ=; b=gIjT9ZaGfy+BlYLu/4XNw48RDWXAbhAIbSXXSrss32qlkJGRLyfUhLuwTDUEMmRxrOKiM8spW6JbwgcyN7XgjOaJRa9RUaypI3Y0WjLdHvYFuvirxK3rq1sI/tx7Kvc58dPF3K6ccHnqEXivjSMpMz3xCbtPo/4GKznCrkrnc3z74eGnLMMUYoxzAm1d64J1sC9OzL7Zsw9r42Uq2OnbDx6+8xzxw66trjk9ED3zraNEkXSw1IG525kqhvw9NXAVqEElMhbGTivS4k/JkMm0fVSxVj1K7wfaEHhLrmLy+p5eHSJOuRy6PFtbkZP+ZceB6EQyu+6SkzR7t8m7/ke84Q== 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=1VBoik1et6ZCxTD6sE8zAbSU02w5p31wr/Vj6VJ2+EQ=; b=MXurec+Xp9GSXgnRf+L9cX6s0QNW8ol8urxA/1bBzv52/2Ku7BQpswKYC3xMfX8PeOf8eixHmxIgVAz0N8Y2udP2ueacmlBDD3V1YJC688itwyf0iSM4jmXQ0b3R+cCRgM+/bIh25YqGCRIkRlzsTXSHPoeBIgimHL4hLeK2wFR/wR24+Tmuah77qviL3+Yb0Bqn36yT2sL08pezwqUloz9ElsBBAcJRVSwDAcoYTKwfT99PyzxmSW2WcHILM7J9H+oFXMSAOcYjsjj+DxQyzo7OfrKkSyprQOtheNOo4lVxAchK+Yuyyqlqio89fTk/YbyfYZXvuZcmVy0Wv6joCQ== Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c01:cd::12) by MA0PR01MB7898.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a01:1c::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8293.18; Sun, 29 Dec 2024 09:44:39 +0000 Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06]) by PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06%3]) with mapi id 15.20.8293.000; Sun, 29 Dec 2024 09:44:39 +0000 From: Aditya Garg To: "jikos@kernel.org" , Benjamin Tissoires , "bentiss@kernel.org" CC: Kerem Karabay , Orlando Chamberlain , "linux-input@vger.kernel.org" , Linux Kernel Mailing List Subject: [PATCH 3/4] HID: hid-appletb-kbd: add support for fn toggle between media and function mode Thread-Topic: [PATCH 3/4] HID: hid-appletb-kbd: add support for fn toggle between media and function mode Thread-Index: AQHbWdZH1kWO/QwfQkSPEWEX2c2Tvg== Date: Sun, 29 Dec 2024 09:44:38 +0000 Message-ID: <559DE2B4-74D7-4C6C-93BB-041CFBAA44EC@live.com> References: <793F0F3B-D51C-49C8-9236-124CF130E833@live.com> In-Reply-To: <793F0F3B-D51C-49C8-9236-124CF130E833@live.com> Accept-Language: en-IN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PN3PR01MB7728:EE_|MA0PR01MB7898:EE_ x-ms-office365-filtering-correlation-id: 59143d45-42f2-4163-2e0a-08dd27ed69a9 x-microsoft-antispam: BCL:0;ARA:14566002|8060799006|7092599003|461199028|19110799003|8062599003|15080799006|102099032|440099028|3412199025; x-microsoft-antispam-message-info: IltvNQjp70ePstIichcPmu8ySl1g1p4+TFcZcqSv8VmAKq7S8NhrfyeT00+cWz6MYV66tF/NP0swXgkzRu+RTvbrep3nKcFsF6yTmwwgtp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/IKEB8RI8o+p/gVSz/YsSCBfLNmfNdryGAtQfO3D10LBNoXxSgVKKwM7r4QmBpfyE1fpp11zHDGpWG8EO2FHsFrvmwE9z0Et18cwmG8d5SVvz8jnHDXe5Uc1js3GT8o4DeCF27uRkPGwDTgIOXdEK/LpT1c71dL5eN5o2ChQLJjMHxCnA4QWBZ6pi9YmZMvrKg/uaS2CXuAcXMBwsLVOVx7MaHNZV29rOPdMEZTcsaoNI2hnQzH8 Content-ID: Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-7719-20-msonline-outlook-ae5c4.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 59143d45-42f2-4163-2e0a-08dd27ed69a9 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Dec 2024 09:44:38.9838 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MA0PR01MB7898 From: Aditya Garg This patch adds support for the switching between the Media and Function keys on the touchbar by pressing the Fn key on Apple Internal Keyboard. Signed-off-by: Aditya Garg --- drivers/hid/hid-appletb-kbd.c | 128 ++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/drivers/hid/hid-appletb-kbd.c b/drivers/hid/hid-appletb-kbd.c index 80c87396f..de5770500 100644 --- a/drivers/hid/hid-appletb-kbd.c +++ b/drivers/hid/hid-appletb-kbd.c @@ -26,6 +26,8 @@ #define APPLETB_KBD_MODE_OFF 3 #define APPLETB_KBD_MODE_MAX APPLETB_KBD_MODE_OFF +#define APPLETB_DEVID_KEYBOARD 1 + #define HID_USAGE_MODE 0x00ff0004 static int appletb_tb_def_mode = APPLETB_KBD_MODE_SPCL; @@ -35,11 +37,18 @@ MODULE_PARM_DESC(mode, "Default touchbar mode:\n" " 1 - function-keys\n" " [2] - special keys"); +static bool appletb_tb_fn_toggle = true; +module_param_named(fntoggle, appletb_tb_fn_toggle, bool, 0644); +MODULE_PARM_DESC(fntoggle, "Switch between Fn and media controls on pressing Fn key"); + struct appletb_kbd { struct hid_field *mode_field; u8 saved_mode; u8 current_mode; + struct input_handler inp_handler; + struct input_handle kbd_handle; + }; static const struct key_entry appletb_kbd_keymap[] = { @@ -172,6 +181,75 @@ static int appletb_kbd_hid_event(struct hid_device *hdev, struct hid_field *fiel return kbd->current_mode == APPLETB_KBD_MODE_OFF; } +static void appletb_kbd_inp_event(struct input_handle *handle, unsigned int type, + unsigned int code, int value) +{ + struct appletb_kbd *kbd = handle->private; + + if (type == EV_KEY && code == KEY_FN && appletb_tb_fn_toggle) { + if (value == 1) { + kbd->saved_mode = kbd->current_mode; + if (kbd->current_mode == APPLETB_KBD_MODE_SPCL) + appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_FN); + else if (kbd->current_mode == APPLETB_KBD_MODE_FN) + appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_SPCL); + } else if (value == 0) { + if (kbd->saved_mode != kbd->current_mode) + appletb_kbd_set_mode(kbd, kbd->saved_mode); + } + } +} + +static int appletb_kbd_inp_connect(struct input_handler *handler, + struct input_dev *dev, + const struct input_device_id *id) +{ + struct appletb_kbd *kbd = handler->private; + struct input_handle *handle; + int rc; + + if (id->driver_info == APPLETB_DEVID_KEYBOARD) { + handle = &kbd->kbd_handle; + handle->name = "tbkbd"; + } else { + return -ENOENT; + } + + if (handle->dev) + return -EEXIST; + + handle->open = 0; + handle->dev = input_get_device(dev); + handle->handler = handler; + handle->private = kbd; + + rc = input_register_handle(handle); + if (rc) + goto err_free_dev; + + rc = input_open_device(handle); + if (rc) + goto err_unregister_handle; + + return 0; + + err_unregister_handle: + input_unregister_handle(handle); + err_free_dev: + input_put_device(handle->dev); + handle->dev = NULL; + return rc; +} + +static void appletb_kbd_inp_disconnect(struct input_handle *handle) +{ + input_close_device(handle); + input_unregister_handle(handle); + + input_put_device(handle->dev); + handle->dev = NULL; +} + static int appletb_kbd_input_configured(struct hid_device *hdev, struct hid_input *hidinput) { int idx; @@ -195,6 +273,40 @@ static int appletb_kbd_input_configured(struct hid_device *hdev, struct hid_inpu return 0; } +static const struct input_device_id appletb_kbd_input_devices[] = { + { + .flags = INPUT_DEVICE_ID_MATCH_BUS | + INPUT_DEVICE_ID_MATCH_VENDOR | + INPUT_DEVICE_ID_MATCH_KEYBIT, + .bustype = BUS_USB, + .vendor = USB_VENDOR_ID_APPLE, + .keybit = { [BIT_WORD(KEY_FN)] = BIT_MASK(KEY_FN) }, + .driver_info = APPLETB_DEVID_KEYBOARD, + }, + { } +}; + +static bool appletb_kbd_match_internal_device(struct input_handler *handler, + struct input_dev *inp_dev) +{ + struct device *dev = &inp_dev->dev; + + /* in kernel: dev && !is_usb_device(dev) */ + while (dev && !(dev->type && dev->type->name && + !strcmp(dev->type->name, "usb_device"))) + dev = dev->parent; + + /* + * Apple labels all their internal keyboards and trackpads as such, + * instead of maintaining an ever expanding list of product-id's we + * just look at the device's product name. + */ + if (dev) + return !!strstr(to_usb_device(dev)->product, "Internal Keyboard"); + + return false; +} + static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id *id) { struct appletb_kbd *kbd; @@ -227,6 +339,20 @@ static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id goto stop_hw; } + kbd->inp_handler.event = appletb_kbd_inp_event; + kbd->inp_handler.connect = appletb_kbd_inp_connect; + kbd->inp_handler.disconnect = appletb_kbd_inp_disconnect; + kbd->inp_handler.name = "appletb"; + kbd->inp_handler.id_table = appletb_kbd_input_devices; + kbd->inp_handler.match = appletb_kbd_match_internal_device; + kbd->inp_handler.private = kbd; + + ret = input_register_handler(&kbd->inp_handler); + if (ret) { + dev_err_probe(dev, ret, "Unable to register keyboard handler\n"); + goto close_hw; + } + ret = appletb_kbd_set_mode(kbd, appletb_tb_def_mode); if (ret) { dev_err_probe(dev, ret, "Failed to set touchbar mode\n"); @@ -250,6 +376,8 @@ static void appletb_kbd_remove(struct hid_device *hdev) appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF); + input_unregister_handler(&kbd->inp_handler); + hid_hw_close(hdev); hid_hw_stop(hdev); } From patchwork Sun Dec 29 09:45:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 13922636 X-Patchwork-Delegate: jikos@jikos.cz Received: from PNYPR01CU001.outbound.protection.outlook.com (mail-centralindiaazolkn19010008.outbound.protection.outlook.com [52.103.68.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A043D282F1; Sun, 29 Dec 2024 09:45:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.68.8 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735465524; cv=fail; b=bS3qhI91OgeaLooprtq8vhMjlnFsOPfWFVKewTr1x+YwcpEtNeCRHG1vf/VsChUPnER7/yMLKcMwovZZCxYgTJsXVrWFMucO1QikciZws2gSWws5FXPc+tlIVh/y2qvfTEEv6c6OZP20Kj4kgVSD0+qI6vqikeVgFDpLfjaUZhM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735465524; c=relaxed/simple; bh=XWRCGCOMPi6gxTSnJPJeKzQ9b1tVg7mMei/C+FjYBds=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=EHEFDD7Ik+QGNXrJTIXYf0iijU6g6jmknb4/g3FQTOl8OvYP3Pf8VoQnlHohvEzwEOf0Jaxi1g2+JvHkBhkop8eDwbiZaBXTHmkSPB5Kr0oFXHW1uOtJLitVbcY0c+1pw1tejQJbcXOfEFrRezNgcorEPsoigkPpaLb4LwFOG8c= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com; spf=pass smtp.mailfrom=live.com; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b=KEq0Y1eg; arc=fail smtp.client-ip=52.103.68.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=live.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=live.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=live.com header.i=@live.com header.b="KEq0Y1eg" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jc3tV2W79cg3TtdGtwu7YlEvhSJb4LdjpEVOnHgzD2gMdpT295HjpkiAzGRmHZPl/wN/LUoJczOwIfXoUQPNXakyzBwECFY9DnDu3OvSBtOh3xiV+xCoLaWzzYn4ZYiEMjnmuy/m0/A7DpbNtUB1zn4vu1zCxJ4kXXdHy4oNcXKk32XxUT3map5l/vW1NpAEPHOgqHC6pLR1haCPMSIWWKVQO9a+dmbZV0DXV2+lMM1wqhYdJ5YvVMX/8j+TEV9XbHphjRaHPvYa+NmmT3ZNbaktZS5F98gIgA9dp9ioGTjspVAS0fe5rjusJC6EaNZDkFeqWqyOHTxGKPOA64DWvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=HYKlzpleH6bheKyvd3AKaS8Da9sIJkfumC4r9kG/Y7A=; b=pWor1kJm/K7wRmthknyBcJGsMKMXHexQZZtCKIrV4rlT/fgzWG0Kyg8FnKG24Gxp4vqxOJbZ3nHO0beA5KklRnu04ACo/BMxpzqwlyOlYnuW5DnAHC6EpQ0HnFjyeyxJk8EpBTpIuKHAlboE+K6hQIh3vs31aIXawtK7GGjbt8eBA26oxrSTrjEmTKYTc1CArsXOBYke4U80tcM/h7/7vxgjRGQKWFgpFQiORcV2RcBBl1GPWl7tCiuv7QbiZCOChX7XljC0tWLUWbf0ntKw/jKh49Yp4xwTKfR+bY3Zkuz1BIA/yLLw9iL4RTUix1ARunzvLe/CGoleB22ezk5O1g== 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=HYKlzpleH6bheKyvd3AKaS8Da9sIJkfumC4r9kG/Y7A=; b=KEq0Y1egVNfQuryeUzURI+M8TxzDEGWw0GXe7Nix2XEFWXvDls7BvT2n2yLo50h3xfoKamk3sQeMK17uaaNV1pG2yu3DoUOBuPCP6348CV2bUZlY6ZbMpbojcTKmFkXoU62T4XLHLvPHUKY2W/Phm2oi9uUEUbFpyEmctmJ9RMOi1H1xAyuGdCNeBpc54U/YCOxK3+tFJeO6f6iSyVVNDkzFM3grghl7LbFaT4rmvBUEI84NHQH3MhrujV3R0jE7vrLZlPSEBxG/1GlT5xTQIN5IBVR+aRhNpebOIgpWm9YLZzxAB1rHoE+b+q8Rj9Q6XKXgIIkbXP8NSJImvC2IUQ== Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c01:cd::12) by MA0PR01MB7898.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a01:1c::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8293.18; Sun, 29 Dec 2024 09:45:18 +0000 Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06]) by PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06%3]) with mapi id 15.20.8293.000; Sun, 29 Dec 2024 09:45:18 +0000 From: Aditya Garg To: "jikos@kernel.org" , Benjamin Tissoires , "bentiss@kernel.org" CC: Kerem Karabay , Orlando Chamberlain , "linux-input@vger.kernel.org" , Linux Kernel Mailing List Subject: [PATCH 4/4] HID: hid-appletb-kbd: add support for automatic brightness control while using the touchbar Thread-Topic: [PATCH 4/4] HID: hid-appletb-kbd: add support for automatic brightness control while using the touchbar Thread-Index: AQHbWdZeHGC/fiwqz0KpaW55sKTtjw== Date: Sun, 29 Dec 2024 09:45:17 +0000 Message-ID: References: <793F0F3B-D51C-49C8-9236-124CF130E833@live.com> In-Reply-To: <793F0F3B-D51C-49C8-9236-124CF130E833@live.com> Accept-Language: en-IN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PN3PR01MB7728:EE_|MA0PR01MB7898:EE_ x-ms-office365-filtering-correlation-id: a6bd46b8-1cfc-4cac-a07a-08dd27ed80ea x-microsoft-antispam: BCL:0;ARA:14566002|8060799006|7092599003|461199028|19110799003|8062599003|15080799006|102099032|440099028|3412199025; x-microsoft-antispam-message-info: f8191xTNJp9J6V5CbRExvDtliDPWIU0jQ5WRWfdAQrxygwoUtppD8XblkHORFMv7OPTtKcWYSpRKS2o1O9D/VOX4G8s97zJiQ7/KTI/CbbsCG6MZ6qOFaVqsVSHJ4BplHHn8oP6tikbJuR6PEgeAmQEfFf81wQnQC/J7qb2whC2b+Mif/ACl65gTHXrZhTl1/RMg5YyeA3OiiO1EPZvAswYMS+wei1PjCEwXWjnFyCin3FzdktRFCYealJwu/la9J6BjrriVD41Up/1Hoycjn3uVS+i+hqfWBlI2gVPtMcWVKxU8v1GBDOWhBJJIEDuy6XXZZT+PB6ynETy0hoYXNhJKBsYGTonp30XaX+fknCF0J98qkvW5kY9U+nFW5ljkJe8p+HRs60joQLImVnThnJW++XRsPXL2Ae9AoVkIYaBqWDxCylhleRXTXMGOKOXzoZitYwSIOW60TWYjF+HMdxPkNChx8adZXjM68IKHOazyydHAZzFeau4Jk7rilASd32em/9uBee9sZQYTKl5y92G9shOVWUup/smXFbDIqR4Z777eXifM8QkXxBQeCfp0e0TlT/Q1NK637rGg+Kl6A4MfOmLFiLAM+vL3C5zTXnqIhJB8EhPb14uEhoxg7A5yJNDSA4a+Vvocd0EfKeyXO9kglVOZkWtefSeepAJXXmVjpQEMXWAkr+9/k/jiAcABhRxYMgFtrBD5Nei/vTvhLxRubZJLZMNQVxwkT3hmbnHMAMn8DDl4O+73YiW0jVFh+/wAP/6TKjAibL29GOTtUzPXbQRASp6Ia14ZepDc29qLIOWAIeHwY82EhWgGYDBr6WbTmUyjkZJXgEFK2qo3Y+btdzw13jlOoLe96yH0kYdy7LynlXRRYcNJK7XT2IIiJe0WIczIQLrCnvHElJblEKyuJXkHuhTikUIXkNM/4B31pK4cZKW9h5RXzAjQM8cocFWoazk/vjnxRgBfDSJXZ17SjQQntVrapZEQnHcptrq3X5/lhzhC0L+48Xf2geVEqXhh9rNb5wdp37IKfWVCTzQwSq+rHaB9k3BEMGqkJk463vplVfook5X+S8N/VMhllcqVwvHbJ9dzvjycjrkUdA== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: 48Uksr6/6S7DuyyxZLcydg2D8oBd2bjVgIaUUbOLF8+UCY9gxrVICO83kNDPLEtaUc1FjNpSjnWwivEN33xSu0uz07DwNwlsAOBbmik5dZ7w2eHcnWf+SMMLYxj/pu0SrkShkmeHbePbK5fAk5dW1VMp4Memm4Q1Ah8LSepqO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qn4SzIXuyon2yCLySUc+wIogxM6biyjIy0VO2XDEbETQFw1M5FWrrcxvBqDaKk6Lw3o9eiwKrrnxcoajVmnkCQ7EYHDuV0fjk Content-ID: Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-7719-20-msonline-outlook-ae5c4.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: a6bd46b8-1cfc-4cac-a07a-08dd27ed80ea X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 29 Dec 2024 09:45:17.9738 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MA0PR01MB7898 From: Aditya Garg On Windows, if there is no input by the user for 60 sec, the Touch Bar dims automatically, and after further 15 sec, it turns off. On receiving input, the Touch Bar resets the timer and goes back to full brightness. This patch implements the same functionality. Signed-off-by: Aditya Garg --- drivers/hid/Kconfig | 1 + drivers/hid/hid-appletb-kbd.c | 80 +++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 5b16bfadc..9b183633f 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -161,6 +161,7 @@ config HID_APPLETB_BL config HID_APPLETB_KBD tristate "Apple Touch Bar Keyboard Mode" depends on USB_HID + depends on BACKLIGHT_CLASS_DEVICE depends on INPUT select INPUT_SPARSEKMAP help diff --git a/drivers/hid/hid-appletb-kbd.c b/drivers/hid/hid-appletb-kbd.c index de5770500..ecc8c84f4 100644 --- a/drivers/hid/hid-appletb-kbd.c +++ b/drivers/hid/hid-appletb-kbd.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #include "hid-ids.h" @@ -27,6 +29,7 @@ #define APPLETB_KBD_MODE_MAX APPLETB_KBD_MODE_OFF #define APPLETB_DEVID_KEYBOARD 1 +#define APPLETB_DEVID_TRACKPAD 2 #define HID_USAGE_MODE 0x00ff0004 @@ -41,14 +44,29 @@ static bool appletb_tb_fn_toggle = true; module_param_named(fntoggle, appletb_tb_fn_toggle, bool, 0644); MODULE_PARM_DESC(fntoggle, "Switch between Fn and media controls on pressing Fn key"); +static bool appletb_tb_autodim = true; +module_param_named(autodim, appletb_tb_autodim, bool, 0644); +MODULE_PARM_DESC(autodim, "Automatically dim and turn off the Touch Bar after some time"); + +static int appletb_tb_dim_timeout = 60; +module_param_named(dim_timeout, appletb_tb_dim_timeout, int, 0644); +MODULE_PARM_DESC(dim_timeout, "Dim timeout in sec"); + +static int appletb_tb_idle_timeout = 15; +module_param_named(idle_timeout, appletb_tb_idle_timeout, int, 0644); +MODULE_PARM_DESC(idle_timeout, "Idle timeout in sec"); + struct appletb_kbd { struct hid_field *mode_field; - - u8 saved_mode; - u8 current_mode; struct input_handler inp_handler; struct input_handle kbd_handle; - + struct input_handle tpd_handle; + struct backlight_device *backlight_dev; + struct timer_list inactivity_timer; + bool has_dimmed; + bool has_turned_off; + u8 saved_mode; + u8 current_mode; }; static const struct key_entry appletb_kbd_keymap[] = { @@ -146,6 +164,34 @@ static int appletb_tb_key_to_slot(unsigned int code) } } +static void appletb_inactivity_timer(struct timer_list *t) +{ + struct appletb_kbd *kbd = from_timer(kbd, t, inactivity_timer); + + if (kbd->backlight_dev && appletb_tb_autodim) { + if (!kbd->has_dimmed) { + backlight_device_set_brightness(kbd->backlight_dev, 1); + kbd->has_dimmed = true; + mod_timer(&kbd->inactivity_timer, jiffies + msecs_to_jiffies(appletb_tb_idle_timeout * 1000)); + } else if (!kbd->has_turned_off) { + backlight_device_set_brightness(kbd->backlight_dev, 0); + kbd->has_turned_off = true; + } + } +} + +static void reset_inactivity_timer(struct appletb_kbd *kbd) +{ + if (kbd->backlight_dev && appletb_tb_autodim) { + if (kbd->has_dimmed || kbd->has_turned_off) { + backlight_device_set_brightness(kbd->backlight_dev, 2); + kbd->has_dimmed = false; + kbd->has_turned_off = false; + } + mod_timer(&kbd->inactivity_timer, jiffies + msecs_to_jiffies(appletb_tb_dim_timeout * 1000)); + } +} + static int appletb_kbd_hid_event(struct hid_device *hdev, struct hid_field *field, struct hid_usage *usage, __s32 value) { @@ -170,6 +216,8 @@ static int appletb_kbd_hid_event(struct hid_device *hdev, struct hid_field *fiel if (slot < 0) return 0; + reset_inactivity_timer(kbd); + translation = sparse_keymap_entry_from_scancode(input, usage->code); if (translation && kbd->current_mode == APPLETB_KBD_MODE_SPCL) { @@ -186,6 +234,8 @@ static void appletb_kbd_inp_event(struct input_handle *handle, unsigned int type { struct appletb_kbd *kbd = handle->private; + reset_inactivity_timer(kbd); + if (type == EV_KEY && code == KEY_FN && appletb_tb_fn_toggle) { if (value == 1) { kbd->saved_mode = kbd->current_mode; @@ -211,6 +261,9 @@ static int appletb_kbd_inp_connect(struct input_handler *handler, if (id->driver_info == APPLETB_DEVID_KEYBOARD) { handle = &kbd->kbd_handle; handle->name = "tbkbd"; + } else if (id->driver_info == APPLETB_DEVID_TRACKPAD) { + handle = &kbd->tpd_handle; + handle->name = "tbtpd"; } else { return -ENOENT; } @@ -283,6 +336,15 @@ static const struct input_device_id appletb_kbd_input_devices[] = { .keybit = { [BIT_WORD(KEY_FN)] = BIT_MASK(KEY_FN) }, .driver_info = APPLETB_DEVID_KEYBOARD, }, + { + .flags = INPUT_DEVICE_ID_MATCH_BUS | + INPUT_DEVICE_ID_MATCH_VENDOR | + INPUT_DEVICE_ID_MATCH_KEYBIT, + .bustype = BUS_USB, + .vendor = USB_VENDOR_ID_APPLE, + .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) }, + .driver_info = APPLETB_DEVID_TRACKPAD, + }, { } }; @@ -339,6 +401,15 @@ static int appletb_kbd_probe(struct hid_device *hdev, const struct hid_device_id goto stop_hw; } + kbd->backlight_dev = backlight_device_get_by_name("appletb_backlight"); + if (!kbd->backlight_dev) + dev_err_probe(dev, ret, "Failed to get backlight device\n"); + else { + backlight_device_set_brightness(kbd->backlight_dev, 2); + timer_setup(&kbd->inactivity_timer, appletb_inactivity_timer, 0); + mod_timer(&kbd->inactivity_timer, jiffies + msecs_to_jiffies(appletb_tb_dim_timeout * 1000)); + } + kbd->inp_handler.event = appletb_kbd_inp_event; kbd->inp_handler.connect = appletb_kbd_inp_connect; kbd->inp_handler.disconnect = appletb_kbd_inp_disconnect; @@ -377,6 +448,7 @@ static void appletb_kbd_remove(struct hid_device *hdev) appletb_kbd_set_mode(kbd, APPLETB_KBD_MODE_OFF); input_unregister_handler(&kbd->inp_handler); + del_timer_sync(&kbd->inactivity_timer); hid_hw_close(hdev); hid_hw_stop(hdev);