From patchwork Thu Aug 8 13:51:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 818328 Received: from IND01-MAX-obe.outbound.protection.outlook.com (mail-maxind01olkn2056.outbound.protection.outlook.com [40.92.102.56]) (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 9B9C663D; Thu, 8 Aug 2024 13:51:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.102.56 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723125097; cv=fail; b=H0fPjK+s0Aq3Jk99sYfBBEzPmo9Oi9qksaUbDy6QF1KFB4lLN8dXlHt4OTor9vpH0tkQ+2yjK/iKRDP5xU7zowNC1EvFxb39dXP/ZLJzb3Oii/d4Vpc/DEqx8TLKoki/CQWoJV7rLnxrqfUwEKLgKgwRgddimww6CRrynwqBArw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723125097; c=relaxed/simple; bh=CsA1hnNTXp2yszqw6dH+qhYAVj0Ncj6wszR9ARaW9Bg=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=Y9tPQp5rOMiGvX2CRl5wuFYcDiT+ctnJz1H9fgH3m2eZEmVOpd0plmA7bbNHRlhiEViKNo06fMhJLR0l06+TaiQvhA0ldaDFj5eJH2ElZ8RPEfiUcOtrBhbYEU40WGYZRuaHFAJzPznz2DSkPc6dc4JDoOGsO6JGcD85PtBDXuo= 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=TZoXw0bf; arc=fail smtp.client-ip=40.92.102.56 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="TZoXw0bf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=kIt9plZV2LQSVQSfc907Dbq/i1iMmL+vbY1zpaAYsylslZ6s+7FhmBuj6xNUMa1Dn8kIYVk0LQQP99lfufJnockGBVpIzTf+eTH0CQEIz+Dx3809sOzA00Cv+M+yohLdYkerO8bh7b/UZYa2wf35W1JFjGWiKmlj2h2yb4ex+MyfvtdPx8cpWc2YEb9quaAXYHgYAiMyDf0QDeqKPn9FeGg25FDvVWhY87qLQ+OYwrrLUst7hBXUjAXfB3kbav8wrB/dKKQHEyQEsDBJlt2TxAGquzZQK2CooYlkYIWRHPORwB7gy+BNSLWpTbXEsSGbYpbeRDZPL72QXCPA5dOHSQ== 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=CsA1hnNTXp2yszqw6dH+qhYAVj0Ncj6wszR9ARaW9Bg=; b=EJxf2mBp5oBnK2FtVk3Vo+pqChskK8V+qN6OOLFxUcAGseDgTU01EBTpjgJ+Byje3UuHXB3O+r51OaITHfL/koEZPn6hT+0p1Q+5LDIhVY4L84iHo4NpzHNRyf/H+WVzCnw/Tzo1JAKfuokEziPnMIBaje8CWjHsAl5mQEpZk21d3+NI7T2JzYl+N77paFKatQrNFxaCRQfV2zGzg8GEiXBh+5MQc94KaM4btdjnKG2SxhggzWO+HadiXz9LLwRqPLOKlSqHIlYikEjBIfhzly5GVmIAt9GoAT4oKWCAqFvpkoj2xlq8BDwQ7LvgK3boRDH6RHuXwd91N8eH+OXI0w== 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=CsA1hnNTXp2yszqw6dH+qhYAVj0Ncj6wszR9ARaW9Bg=; b=TZoXw0bf5fPzMj0HsM2JbbJLT++FmTBGMo5F3Uob/Zu0hNyQFhFaqzEOL+OzoKVqtL1tJx8Qi9lDx9s7MYNy+YHS4bej13uuPgtgCVZIWnvn4trsOqzdK0YCi/FnmPM7KlTyIwEPYUw0PmZg0oZpEDInLjH8GrMYJSqviS47msmqnuLAW3uspb0hebaJplCp599s3ibON8ydfIh8Jr/2r0Tkk+B6t4zK9sxcpTdgpM5ffrtJyVmR8bT6NlF6Tmx+UeabJIiCzbkQK3MfsCx5IaoW8MPLI63UzgGOu9+pYJ73asijBoKKUSribA4sucBUZ8CJkhZ1E3eVBHtk2xOLpw== Received: from MA0P287MB0217.INDP287.PROD.OUTLOOK.COM (2603:1096:a01:b3::9) by MA0P287MB1976.INDP287.PROD.OUTLOOK.COM (2603:1096:a01:11f::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.13; Thu, 8 Aug 2024 13:51:28 +0000 Received: from MA0P287MB0217.INDP287.PROD.OUTLOOK.COM ([fe80::98d2:3610:b33c:435a]) by MA0P287MB0217.INDP287.PROD.OUTLOOK.COM ([fe80::98d2:3610:b33c:435a%5]) with mapi id 15.20.7849.013; Thu, 8 Aug 2024 13:51:28 +0000 From: Aditya Garg To: "tzimmermann@suse.de" , "maarten.lankhorst@linux.intel.com" , "mripard@kernel.org" , "airlied@gmail.com" , "daniel@ffwll.ch" , Jiri Kosina , "bentiss@kernel.org" CC: Orlando Chamberlain , Kerem Karabay , Linux Kernel Mailing List , "linux-input@vger.kernel.org" , "dri-devel@lists.freedesktop.org" Subject: [PATCH v3 2/9] HID: hid-appletb-kbd: add driver for the keyboard mode Thread-Topic: [PATCH v3 2/9] HID: hid-appletb-kbd: add driver for the keyboard mode Thread-Index: AQHa6ZoRlngOtLuKXUS/2vc/wywlgQ== Date: Thu, 8 Aug 2024 13:51:28 +0000 Message-ID: <53B2748D-FD87-496D-A926-441356EBAD18@live.com> References: <1368FEE8-58BB-41C9-B9AD-7F2F68FF1D53@live.com> In-Reply-To: <1368FEE8-58BB-41C9-B9AD-7F2F68FF1D53@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-tmn: [UnYFSFvv+q0Qon/HVpUcKPGEevtR/IAIjPTkfqL7kaNBrHAsWpqLZN7J43QIgxQc0MwLm0w3kMY=] x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MA0P287MB0217:EE_|MA0P287MB1976:EE_ x-ms-office365-filtering-correlation-id: 7885be1e-aaf9-4965-2ea3-08dcb7b13398 x-microsoft-antispam: BCL:0; ARA:14566002|461199028|15080799003|8060799006|19110799003|102099032|3412199025|440099028; x-microsoft-antispam-message-info: uBxmaw57Lay5xVt3l2o7/EDUmU8bSVvPex4dYoi1DRq7EJQDBSq5CZDLD6vqwyD0arp9Ey0fyB+/FIyAtQvRNArnX3ZnCSMQYYEYtbze5aR5RzIY5yEoBPAesMEw9dGaPTrbAwY9aWiRX7PztsVnXAEKvLbHKrUNeieaCu6cGrF2u19iPadVjtLT2G51tTExsHFKL1bks6f36zNG4yVk5nAQtSZ7nVRZrKLUZCG20+hZU+gUOdVWq4hGgbue0hcIpFpfRNI//kbM2TV9Ld9cvmHCHfGgo8ak5uvG/IW57XZZCIIE0NruxSDFdB7wFNkZuHHO2YCnE3aUdft3efau5DIn7ITd2OqpKtIUfXnpZh4S8r56zSEFzbQHv6yUsoqIiTWEJb43cR2jguDOkOwx5Pq6v7meFEQxeIwM92P/VRpyNGUZFCmvAn0lsratGbr4+i73S1n56qLLXZq9opn3AKin6DyE9miJilYKytIdPBdRPu7XyEivLLcpcDPV7ZC93N77p+rzAuhzObnLAgjOJSjrfq8M9zZbZDx+Rf4TR+J2qkpPVDn73qg0B7lFkgPmIdrU6jaV2Dxd1BX9yzes2SuWMdHKrPCDK2dscy7G+mf1AAOzuk1HBUjmsYVn7IcXXavOnv7dt+juAK0C2Nf1r0YYlZqbeZbbZ8eWPZBTWsCvXBuGFsm9r6KgV6FvGQa9 x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?mS7nDx3rGT/yAcMXOL4Z2xmqXOy0?= =?utf-8?q?hvb+unAUiAhcYBxApz5QZYHDrsS8B+HQzO1OTL2RKSM49mSBI+BiHiS5lcu554G5i?= =?utf-8?q?C2fvdZtMAK+G9zF6WcTn4XqJWQBVpPQGSCXE1dNDAavt6IE2KQ4L+UMIRnnShrmce?= =?utf-8?q?kdSgObrasQPSJtmclU1D7ZFx1kt0ZxeNHrEG0WGEz0tenM2vf7GJ9xtH4Y7ODABFC?= =?utf-8?q?wwWIrtBzOyOh9hn1YB2H9F1Tl0SY3WGbujbW0q81GdtdFX6ND7o+aVw9qT9ezatNz?= =?utf-8?q?EckSlpJi6WbRXsffH3sODUb08J2eOFSYJMKo1KGrzLa02BH7Ur0Q5xAIjOBCaIuSM?= =?utf-8?q?FH9gOwjoOwp3+c45sj0NZfTK3rEA6lscybvqTOW2/vRin/F5gtHQPECn1Bpk/OKYW?= =?utf-8?q?WFrsl73s66ch+iJTltssEKZL3pbOCnbjmx1Dho6uD3vhZAMpfdfP6NTOxDWNFgVaT?= =?utf-8?q?EL5nrGjTfY5wRbHbML3ApPBvgRrs9awiGCkZpOk4o8BTcqziDXV+6QJWRV6yWqgdC?= =?utf-8?q?Zjk+RgyLC+V1zI4uosrtggM3NlaMEiUoKNigMfCoXkjM0R8l3gwW1XE0hCZFWy/cL?= =?utf-8?q?XTKUP4PpIC8id5Fle0EK5uPn3MD4f6FLwZiewIZ9gaVHPAnUxL/ZIU6pgR0oAxf3l?= =?utf-8?q?D8kFd3Ux4TwGHFAbUyc7x4KUT9LS9ocgyfzuKY/ZlkUudaMtaXJ6ykavZRvwYYL2F?= =?utf-8?q?FTxYNFdpy8L13Z+cHmJ9BoY+tVFs6PbFFraoX4n2ZtTmYBDTI14qxTlRYWU0bPki3?= =?utf-8?q?2ZsWn547nZvdYph/NmRD3DA10TKa0kmkLWBtAqOAeCTW/Whgtga+1vgnG4WniqCEQ?= =?utf-8?q?nntpbnL0ucbc3ZgGp+75R/lHMZAA8iLO9LTOLfQn1+8o8HzTRPL/wdro5yxPQ3zaw?= =?utf-8?q?IhmRZ/3ZjLJmsi3j548V0aZgLD/g1sT5xMGlrehTbAz/sFZN1x5cjtq0cjsmH0wJe?= =?utf-8?q?hGjaD9XXFJaU6QI4NUajEtQEg2GPE/C4kFiyG/LgbW796Adc5J8jD5U3cya/TyECk?= =?utf-8?q?gEZfs5Xymk89EqpiREwO8P8kUw5b4mbQRYEJV9Rv8HmfI8IQ2S9WNFnhpeXijSQvF?= =?utf-8?q?WylAWOjT1o+zPBuwhmQ9NtkchcGZOL0uYLt3xA+Oqk2oIY+9bwzU1UTx/XFVrkJsk?= =?utf-8?q?Nau1Fb0t7ctUcvScIO8CkCFWyAMFeflK6GcZNVbDC0akXYSKOep5qyIbcEAWOGa85?= =?utf-8?q?dLYLmjE1Ri7vIhzibvv9g1I0r1Arx/vsAPMT7cTRyqQdyRjjFtNlqW72W+87wEj74?= =?utf-8?q?mWOTcah7aO4AjcRP?= 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-24072.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: 7885be1e-aaf9-4965-2ea3-08dcb7b13398 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 08 Aug 2024 13:51:28.2165 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MA0P287MB1976 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 | 304 ++++++++++++++++++ drivers/hid/hid-quirks.c | 4 +- 5 files changed, 332 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..e07d57573 --- /dev/null +++ b/drivers/hid/hid-appletb-kbd.c @@ -0,0 +1,304 @@ +// 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[] = { + { 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) },