From patchwork Tue Dec 31 17:35:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 854782 Received: from PNYPR01CU001.outbound.protection.outlook.com (mail-centralindiaazolkn19010007.outbound.protection.outlook.com [52.103.68.7]) (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 4873B42A8C; Tue, 31 Dec 2024 17:35:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.68.7 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735666511; cv=fail; b=pcSuSPebJpQ3IOFVs3i3YCrnVwBMI9XWAnieUSRND9IUxOMHHUyMY2yR/gSGLTMjWJS+7MNhE+fPk5xc7+QQkJE898ncZSzqLY7tkiGy+WsY9qqQhjZEnNOpG5l0C6dT9t2gLwmjtnQNqMpUzSNvzwIzWt6qr6at1QuCw2Q3qQo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735666511; 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=Etl7cIpXjkqScNDhH9rqnaWwyn5XAhaRUqTiGnL2wUllM7vc1ZaFvqMTHfmax3CA3QSg0WLHVohCFy1JoanCQKi1jhVKadId0wnJZYyqoXmCR4BHQno/BGKuxsyis3P+/RbEbSDJx3ti/SGz8sgQ1o6ePPmCp2xrKpVdDvOsBok= 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=cxe79DZI; arc=fail smtp.client-ip=52.103.68.7 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="cxe79DZI" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hizNd5vba9r6Kd8X79DJ070syeYO9US/97Ni7ZARBk0O5lBPR7Ka8+BfoTcPIgVp5AM+vU36Paz8ieKCBZ7wR3iDdDCMRfXdQF1kzMsJiI0gQ4QnOfC9aKccv59kAEyFRKtJ10ysS+BVM23mPu6+zKMxlpf7ISLbjk6AkRYg5304id+6rIH3MT1tFjAx2YK7bVrZIRVzWAcaDT8tE+uWwEjp9WIGWAWqhnTK1r/Dl0ZiAfHbGh6I9jpTdC/6+kr2Su3HSlBUPnWhWnJ9/U00eCnsEQC4G9xbbfdvTxcf6fA89cfeTY4E1myVSqrxWKHdQ6CLJnCYO1UAVdW/wXPwbA== 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=wiH6U0lCWvpq+HqW+TF4H5orj6NuGl98lyEs7Fu0wchgNk2H4opYA0C7LGEOJuP2GJlpKNdBSYiaGY3RK0eG95gnYj6mWMcgkNkoSqWD09/uqzMYKsJySZFqbEN13fX/1tgw1yS8GeTnpAqsvd4WeOGREMw4cj6fNwIMDWnfM1ahmB64HCeo4asvYdHF0USt2yYMcKGpcgHMH/CdD0AkHE91XXCDrUI9CLOMuNl6usOm9ANFXwmKK+ITzBogByRHpssPLSBxz63O4P+exeuMDvqDN+MrkbBCuDTfvAn3OJDFtYiRfPnMZGy51VmKnp9Grzc3b7O3GdGqlgeij8c4NQ== 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=cxe79DZI9Vt+a6+PT2L4hzcPtCl5KSCyzmzr4upJD6MpZ/3xsgDQ1BIcwIqXBF38KZxwtM/HUWiXa1TlUZHAQGLQl5m4QksN9oj74OkIqqvo5F1KjuhYFwb27sCfgbTA6JnViRCcx9R9xRfaT9asg6kz4dO0t2UMOuvfDESim0zc9kzW73CgiPDnNfR7dxOijPlstsgrd4W232F5fAL0I+ta783o+kciJE5QHDEGoSZcHA80+DdmBN9nqQVaoY/h/UU0nm7v+JqFg1uejwjmIQj87lOxZd46EpjsI4F3dn5Rwi3PqAr/RTlwInDz9pZpZAKjzZ6jWe988ZyGyR3JOQ== Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c01:cd::12) by MAXPR01MB4261.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a01:8::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8293.20; Tue, 31 Dec 2024 17:35:04 +0000 Received: from PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06]) by PN3PR01MB7728.INDPRD01.PROD.OUTLOOK.COM ([fe80::d3b7:2498:cd95:5d06%4]) with mapi id 15.20.8314.012; Tue, 31 Dec 2024 17:35:04 +0000 From: Aditya Garg To: "jkosina@suse.cz" , Jiri Kosina , Benjamin Tissoires , "bentiss@kernel.org" CC: Kerem Karabay , Orlando Chamberlain , "linux-input@vger.kernel.org" , Linux Kernel Mailing List Subject: [PATCH v2 2/4] HID: hid-appletb-kbd: add driver for the keyboard mode of Apple Touch Bars Thread-Topic: [PATCH v2 2/4] HID: hid-appletb-kbd: add driver for the keyboard mode of Apple Touch Bars Thread-Index: AQHbW6pTv7SHMKd5hEW30wbjIbp+VQ== Date: Tue, 31 Dec 2024 17:35:04 +0000 Message-ID: References: In-Reply-To: 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_|MAXPR01MB4261:EE_ x-ms-office365-filtering-correlation-id: 7b9ff9ad-94c5-4dda-8e19-08dd29c1762c x-microsoft-antispam: BCL:0; ARA:14566002|19110799003|15080799006|8062599003|7092599003|8060799006|461199028|440099028|3412199025|102099032; x-microsoft-antispam-message-info: =?utf-8?q?gdNzvxRNQ9Dh1JiSvd8FLOagK9UT41z?= =?utf-8?q?weULSGDqOlq70gXlNQ7F6HPtcBUnoENeFUYz0QY5YOJHUEh5BQzxk0yfyB8YvyS1P?= =?utf-8?q?EkoDJv1osKlCMF3FjBHy6oaYOHMcIkPy4IL28MhtAVPoK1d64qZxi1ryDdEznA71m?= =?utf-8?q?B6frAOIRI/hU6TGnWT8MQ9cZsNABHUEPvifY4n4+CmKCSQofsGjsLXcT1xHxbZ0Nb?= =?utf-8?q?QCrwJWrpmpJ1PlyKRpKmirIiQNEA/spGyICPQtcg7lnXwwvilugMXR1IuNR5Gd8V7?= =?utf-8?q?LZawZn5dEtmzi4rMpPEY5VduccTYC8chZrJBnEE+eB1LC2RuesM85sn8XA1+YDztl?= =?utf-8?q?SbpZeSkYd/PpylIfewW48p/EDDwODQBfcY35PeMPiwHUAH5wr74LTQ+sJwnzcVuTJ?= =?utf-8?q?nCuPiC4eMYoPc4vcUbXe+B6laVhp/fBFqyZXZc+TC42zyHYjpy0dTLHuH4rRdR/tM?= =?utf-8?q?EbBJEpfWQ4W2ReiGj4suYg7XSSO85zO+RLnkYIMO7JHEqMJlgioV2HRB7X9h/sNV9?= =?utf-8?q?MSEPQqpgnTwqLYjvikfKyPIZITrD/yVhr77AtmhztuXMcRKpFjKFWbD3hDDfGPrsN?= =?utf-8?q?g8tM5/r3TIlx56i8xr7HNFwsYRAMGtxdxe4RbTYNtYY+i2g7Dcmk9f9ycqJKLF+O3?= =?utf-8?q?VenVk72T1aoHHlMpCbWB9EB/Gwop0+HRWASvii5TU3WO/oN+XjJxKA5bigPcqmnnj?= =?utf-8?q?/yu6IrYDkkOrx5Q6IpobuSTIZH/oR91JJoWOX3zeUtF4XnRlB7kG3BTdre6NZVT4S?= =?utf-8?q?cElHRgZZ7+RYt6DU8RCEgeH86kNzwUOKbg6YBRRVAkhSyWe/WE0S5yXxMGcpvs/Dc?= =?utf-8?q?8IU4PkjOIMsKRbKcq5Fmn9Eh6vtg7Lf5DygCbEZTELo7W3+B2X5oKkMN37BAZAma2?= =?utf-8?q?voALHz+cTVi34DbVfVebHqDKLF6+3qa+D9IH56/i5Di/ZjluOZ50g6Tz2jdLWM9YF?= =?utf-8?q?EH/LwpT25W2rKykMr6skieAHeQkbCAFqLw7ISgXF48XbNxadB7fQQdAGBhuWDSkF6?= =?utf-8?q?gv79xmHdF+Su8qrJNo899TSTsGRV9d9NHrzJzpQ=3D=3D?= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?cy9pudy2Gq9QLvNDfJkfOgJtYKNY?= =?utf-8?q?sqLlcti/Qchm6VagW49bj/zF9p5wbI2VDLf5ZS59P2P5mm3VDEcX/urGiwDq0X9hH?= =?utf-8?q?tp2fSw0FabU8R5/tAIBu+vSODC+6A8BxUXKvArxLVO4CrlVU2OW7f0NRNrFqNYDMg?= =?utf-8?q?FIkd80qzL3l5+8wA9qHeRzSbH894oN9gFJwhulRlXAT8DyomVeic1S97cruLjd5Oq?= =?utf-8?q?RJothBmQdOqXWN5GcAJFVFO0WlszoMsK4OmfD2vYCUb68noM94X9JJJ6egt9Vt5jD?= =?utf-8?q?j8SOzO1tt3FehxbUVD93BUDPPFDubO11fpD8V8bBBBQ0z+u3nuqmfNWUmWT25IBrw?= =?utf-8?q?3SZJOS4allu+NCuvsRsXEVY1dcFh5nEzhslBU8UGtYrSPQDZ4TkhPurkDOyUpl2FC?= =?utf-8?q?hKUc8Dlh4L2hoj19PdTTnNXzK+u83L8ZETgzE/wFn/60VWel3RDjLiuTC0Fr4bBNu?= =?utf-8?q?y53yQ3ufEJFDmRLAtuHXabZYfsJZobJDuIbbZBjBbFsFTPtmqGkIr4PCpDHIlbK6L?= =?utf-8?q?ADCcC9jyO8ymHRVHsB6IIyNVP2sehfGGDsVvVzNa14yOKFno1LpICea5HgrFuaWMC?= =?utf-8?q?iBRj3cZRExS+tb2jOCsZMvp55Bw4K4TS0+6a5kljxaLIAYYyEKNX25OxRfnsClyYV?= =?utf-8?q?clU1fWlm430awPvYD+oUoLSXqwi4kZCXZhz/nGdaSfwsnpD1d6fNEeadDlRyBThyV?= =?utf-8?q?18ftdaYYtJXHQZjynlcalImbqQVJE/VPCtEK/XkkMCAhB8rN4ZDUJW3X4lr//FMzG?= =?utf-8?q?a9NtTnLErpowwEApc/Ej6G00Mkt/OE9aYQKO8AEM55zG8D9YE0R85LFEjX41QUr4E?= =?utf-8?q?PJElsf9NHemqgvUCbYhcWdoz77D8a3a3/FCEuxDDzNWeo/m7ZLHA65LR4OoLpksdD?= =?utf-8?q?jsxDzIUvqpTJhlpkVS4F50UZdXwBgE+K8+ZAP0hTdDHoncvXIlufOSZAQBZDU+6vL?= =?utf-8?q?GYnlDTPWJaUPvmq8tqgeAmoJzonBQU7CeblB+dsgpdN8c1Gdc4+RSI/H1zlwMXcyL?= =?utf-8?q?APEuD3hyF3Ec/kAOVqBwQ+E5W8903QXEC60KJNNvmCMx9O3lO7kkLW8I8ZoOYfFQJ?= =?utf-8?q?QDxIwkSfbH9RmpRBYqfvj05x8Xcfw6Wo0Rso7XocBi1P3SMadp7e0ZMZmK60uyWTR?= =?utf-8?q?4EkLk1p/A/+wsp1fXQvJ+JwF1KOLgTgiiUE5JDmbDXpzIVg4NpRklETD7gSvD7/ip?= =?utf-8?q?+JFps8W0uVi2hhqmzB9VHctW2lBL55lGetj7NACL1WkR+h6neuD6TubCg1k6rz89j?= =?utf-8?q?+724q387OcFsXIub?= Content-ID: <6EDB1BBD2DB7E34FA102C2A005001234@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: 7b9ff9ad-94c5-4dda-8e19-08dd29c1762c X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 31 Dec 2024 17:35:04.4053 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MAXPR01MB4261 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) },