From patchwork Tue Aug 6 13:48:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 817806 Received: from IND01-BMX-obe.outbound.protection.outlook.com (mail-bmxind01olkn2020.outbound.protection.outlook.com [40.92.103.20]) (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 AFACB1D47AD; Tue, 6 Aug 2024 13:48:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.103.20 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722952121; cv=fail; b=EnqdOPu7kS9XcoPskzBJIZxAATjvaLxKTMgnRkAPRCxKoSKiiq9nPHwF383suPW/fOMArfsf4Xi33RMHVhNMQi0BQQZPAp8ZMI4Yw9d+vMumEYWJCUqFvsm7gLDduqhIsZruIk+4UfIIXmiJUHDSHUEUZpC3L0qehpJe6GFbjgk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722952121; c=relaxed/simple; bh=HG++wVtj476OCPCePWPcbrLd5qAedkg3MeucG4l49xY=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=j+nddFSqxUCzmPS1a3Cazi7hLg2ujoXe1ydWBE/RLaLg3p8A/e5bq35Q/c3BahgeiLaQzq0hxyZ5w3mMibr44RlZDesWEfatGZjWi7x0MBsTy1v2xJMKAH15izRciVp5goobF6AbEMesaKPGPGjE3A+La83m9zedMQ/9x3u/xFM= 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=N6U3HYQg; arc=fail smtp.client-ip=40.92.103.20 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="N6U3HYQg" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vykDZ0RHxpANR7M6TLrkp4m9pyfvMzMfAec0WorTyrXNo+fEyjeyKimlmaG+cMU4hOjvfNFgPMvPG0oxa1kiT4fBtISxpwJvhrENHYNpIUfTPdGTP8Y63fmjJy/Ba4qopMh0eDioWpzEwjUQTo6ZX3MOpfpsCSvP6YxgG21qO6b59IQnEYcJcu6RHelk+RLOB3DUsT+RBRqqFOcS1b1IKGLbVNAsSHxI1k4prtD5P1nLl8ePXHYZlkeqaULYYp2RJ5vl1FZ8m+MvTmaNyMqJpfmaOI6mneHAOJ5JG5KE4PsU3uxKCwfAZFpGQDETJkOStaEgn54Kl/DyRCgjPWHymA== 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=HG++wVtj476OCPCePWPcbrLd5qAedkg3MeucG4l49xY=; b=Go4EK8IfLmm1UbyMxITI+tCdqWKzyCIAyT3+Ykk+LNhqRAC+PoCAYNZhQOuGuJnsKj+w5PUuAG0baohq99hZHEzpSmDw3ub5XFZP2wcyBTT/Ag5pLiRqaTPNY7CyKpfog6hko2/BkY4vY8C0ycgmuWnLQs4MiJHXagyQYQfbRr9zU2UMyAN0Oq4HSgHfZC1tVfH93vr/EGYP02HOJFFwwGYl10LNH38yvCaHBOXelDZwPASgfu9YyDV5XL4nd3VbxSZ+TPydivO7TaKaWBj4dO4FVJIVUjG/dTdUTN6DrPnXlFrVWCNx+5O+h/6lvi3Kw3vNrV/b9FBW6NQCWTN6BA== 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=HG++wVtj476OCPCePWPcbrLd5qAedkg3MeucG4l49xY=; b=N6U3HYQgBmItxUVWMZnQG2B/dz4BRmgvbEwvgyz7z3AiOo1J2EXzMN/HwqDeRm+6bSP3xhEft4vMaIIOfk6GVIvJk3ah2pLBY0/eshXjZ9wZHJOkbyhbZ4OFdoDi7elNtD39QkWZYv76ktq46etNgmtNngY+iHF9K/taj9EmiZASvsIMMkqC/gCAqDor0pI7txhdhFi9Cj/MXfKR6Z6TdhGxW+AOZ6b0q/sooWwKnAbnhmBisfMbNVOUqU2LbJwwtagd+KZMP1ki/mtZRtMYHdRA8HAKgnsmiygBb/f+sOQF05aV0wjLUAWcSeKSfbAhml+GX3oxw8ozkqX1OD6aKw== Received: from MA0P287MB0217.INDP287.PROD.OUTLOOK.COM (2603:1096:a01:b3::9) by PN0P287MB2100.INDP287.PROD.OUTLOOK.COM (2603:1096:c01:1bd::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.27; Tue, 6 Aug 2024 13:48:29 +0000 Received: from MA0P287MB0217.INDP287.PROD.OUTLOOK.COM ([fe80::98d2:3610:b33c:435a]) by MA0P287MB0217.INDP287.PROD.OUTLOOK.COM ([fe80::98d2:3610:b33c:435a%4]) with mapi id 15.20.7828.023; Tue, 6 Aug 2024 13:48:29 +0000 From: Aditya Garg To: Aditya Garg CC: "tzimmermann@suse.de" , "maarten.lankhorst@linux.intel.com" , "mripard@kernel.org" , "airlied@gmail.com" , "daniel@ffwll.ch" , Jiri Kosina , "bentiss@kernel.org" , Kerem Karabay , Linux Kernel Mailing List , "dri-devel@lists.freedesktop.org" , "linux-input@vger.kernel.org" , Orlando Chamberlain Subject: [PATCH 2/9] HID: hid-appletb-kbd: add driver for the keyboard mode of Apple Touch Bars Thread-Topic: [PATCH 2/9] HID: hid-appletb-kbd: add driver for the keyboard mode of Apple Touch Bars Thread-Index: AQHa6AdRwVs1e1moeUWOBwJtJvK1KQ== Date: Tue, 6 Aug 2024 13:48:29 +0000 Message-ID: References: <021EE0BF-93CA-4A37-863F-851078A0EFB7@live.com> 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-tmn: [WqQ/kCvcNTSp3KOdrNDhkim9o+QlbQpKE8vU7mK7VBsVL+HKw05zObbuLRfzX0rfWa21lZgxkx8=] x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MA0P287MB0217:EE_|PN0P287MB2100:EE_ x-ms-office365-filtering-correlation-id: c8e9bb38-13bc-4392-48a6-08dcb61e7421 x-microsoft-antispam: BCL:0; ARA:14566002|19110799003|461199028|8060799006|102099032|3412199025|440099028|1710799026; x-microsoft-antispam-message-info: zFDR/PuiAZ2ffT9srWxpTtS3Fn/gWYvPftpHEqsCd6ERxd4u68/fE+yT4ncD/avbb1sAGOZtuMgTVZ6oVt9YCwoIKGtfrj6jvSPk334Pqlve6ku3fRep0yYN8J6vz22/EHrhH9tI/NxSHXn2TehgKNAq+Bh8CcSifCrceDcM1klciGaCQGy8L0eIyP/nYpe+cYEGgw01WgcYVsluuAlSFi36wiwjvBqU1o1egARaVgXKEBlDrGkhrNJX0t/qtTtUkdYwqOMXxmCFxELQQCJdcr92IOFr3VrlyLpcSu/EeF+O6M4OAoyPUDIM4w+NUaOwBM9YCcZ2CqvoXI9eJbXeEq485Al4Lnh28bs5Zb1aepkswS8jzMPao6QJemhOWg3UlU5PRh7U+SGn+0t9kU9WY1fbp21p+tid7NPi/MqCzG565ilS1RwCJlRxu+Obf11GE1yxXN5gYN9o3eTtLKzqAMSGHDUoMBRICjFE2dhvlHyeN7Pnhf9Mih3pCpsyonKm8pMUBNveCoP8AITQJ3BIMx2WWgEzG9AFu2FdYQssWoibX8wg0sm1RIY6cOpbJRlfVShQ7VGlCPelOABF5keETKe/zH8yCzIJx7jVnbmcYb7G/OiT8/8ouu1Bz5MfeN+pCcyigwW16ZYiR5qwbHfO9TG1R7y8OeOkGnZMexa2wbQ= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?dSbxl46keGI9gvXtUjFebI+yvtJF?= =?utf-8?q?00Gc8wO0Bz9kCsg5okTw8CusHDPlbwKh3WeHAluYOGGGIQp+wRMI/Xexe1PLkKsg5?= =?utf-8?q?MM/GAdJImzs2xVSOUNuxW+vOJcggG+4q9SJLsPs6W113kpeqbpPrLQ1vBIcysyHIj?= =?utf-8?q?5dCGxRaae45Fl3+ENYL1pjl1/Y9ldvYI82TnvUYdZ5Y+4qWyGZpP3NFlrOQ6jlab6?= =?utf-8?q?3bbX+GrouWhaUaO3WITsLjA/QBEvkzcrRSVrJ+39qlleKMoY0zUu1k5PdFfK+P2qv?= =?utf-8?q?0x4vIr696ELOp4cvxrYyhnw2YEOay4pxtz0FnyOD+jXeMrISQ0e4clNg9baknqWkK?= =?utf-8?q?4iQBCE4lGyZ2ZPoLui1Ua5yFZBWsHPbgixr37YkDrNeHXJ4V7tTuoDlu5wK9fNckf?= =?utf-8?q?Tf5T7PruZy5cTvVc8jKOcbY/mWQszTfOulBUaEWMs6xegonoSXG2YSHy79hvlYdRy?= =?utf-8?q?a761jIof0JwudtuBxpyjqYa5LHT2RvwpUGnkBTsIPxcHHBQaLnzIpDwMm3SOvnDnP?= =?utf-8?q?GrWfcIpmk/9zjF5VhjzGcEDmxbCH73JK1z3XCk2BQyvCNZP1Md2SYUbPOzljX6sqd?= =?utf-8?q?KRDnmd5ooIZ4CosfOOl4pTZ4p2rUNmv8S+GCF+fbUbt6ZI+vpZKBn0FxgxQGAzRL0?= =?utf-8?q?ShzhQ2LjwsBfxvPdJNo332kBiTZaaFEkSIlRC0uvScvAyzTr3661sGwb/+Q41lcP5?= =?utf-8?q?lPttZbAmBtp+swhiJs1JjgRkYr8SKI2ZcmXf3RAXk5Floh3O6QMZp8Fy+UCiNOc8i?= =?utf-8?q?LMoHGXAZVNBvaATTqN5Ax4+1XAQvoHmhQfiQ8lhugj9kfwg3MMi0ULSQv3kI7/wtI?= =?utf-8?q?/ONix0AizC9ioTkJ0PkYm9j4Z3YrTL2C2g9LZ71THXtCO3zpz9C8upeTUwYpSFTYi?= =?utf-8?q?b8b/8GAXV5MsoRQbNyoWlPCUxEQo+j8Rd8TCPewGfkdcK5NKNdn0s/IERH5i4n3Qe?= =?utf-8?q?pfFQMQ1eEqYekOJMplljNPvOFraAtLtv9sefSpNrWoOrbKc19Q5uFBMesJIqjieT0?= =?utf-8?q?wmUxkGso5Ysp9GJfOl/yh+33tcr5/TnFjpzI+N/Bt/cxaAFDVJ/KLhIJUD6kJyq2o?= =?utf-8?q?hif6WbN8nyLxDGdvPWEYnQGVf8OdFlifHNjjbyheJHOBPlTLqk+vvP8a32xwBiwl8?= =?utf-8?q?GI0u1k3jaYDdeK/rN2lTnFZuTawe/MV8Yig5D1zN2a/sY1VAUAXR0et1OWpjChgcW?= =?utf-8?q?aDczZwHNjrKj1JLKmzGsESBIUu4GMvBRSNfaeuyHrMHnwE2bjdGFj6aUdiLj8BC+r?= =?utf-8?q?DiV8T/G0nT6d5uMI?= Content-ID: <0CFAD6A09D42CD45A7E9C6B16F5C79B4@INDP287.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-4755-11-msonline-outlook-bafef.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MA0P287MB0217.INDP287.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: c8e9bb38-13bc-4392-48a6-08dcb61e7421 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 06 Aug 2024 13:48:29.3281 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: PN0P287MB2100 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 | 11 + drivers/hid/Makefile | 1 + drivers/hid/hid-appletb-kbd.c | 316 ++++++++++++++++++ drivers/hid/hid-quirks.c | 4 +- 5 files changed, 344 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 4988c1fb2..72b665eda 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -158,6 +158,17 @@ 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 + 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 1d825a474..d903c9a26 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..453d5c7e1 --- /dev/null +++ b/drivers/hid/hid-appletb-kbd.c @@ -0,0 +1,316 @@ +// 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_FN; +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 only\n" + " 2 - special keys only"); + +struct appletb_kbd { + struct hid_field *mode_field; + + u8 saved_mode; + u8 current_mode; +}; + +static const struct key_entry appletb_kbd_keymap_spcl[] = { + { 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 const struct key_entry appletb_kbd_keymap_fn[] = { + { KE_KEY, KEY_ESC, { KEY_ESC } }, + { KE_KEY, KEY_F1, { KEY_F1 } }, + { KE_KEY, KEY_F2, { KEY_F2 } }, + { KE_KEY, KEY_F3, { KEY_F3 } }, + { KE_KEY, KEY_F4, { KEY_F4 } }, + { KE_KEY, KEY_F5, { KEY_F5 } }, + { KE_KEY, KEY_F6, { KEY_F6 } }, + { KE_KEY, KEY_F7, { KEY_F7 } }, + { KE_KEY, KEY_F8, { KEY_F8 } }, + { KE_KEY, KEY_F9, { KEY_F9 } }, + { KE_KEY, KEY_F10, { KEY_F10 } }, + { KE_KEY, KEY_F11, { KEY_F11 } }, + { KE_KEY, KEY_F12, { KEY_F12 } }, + { 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) +{ + 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_fn, NULL); + sparse_keymap_setup(input, appletb_kbd_keymap_spcl, NULL); + 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) },