From patchwork Sat Aug 17 11:52:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aditya Garg X-Patchwork-Id: 13767176 Received: from IND01-MAX-obe.outbound.protection.outlook.com (mail-maxind01olkn2016.outbound.protection.outlook.com [40.92.102.16]) (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 DB8E7145A16; Sat, 17 Aug 2024 11:52:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.102.16 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723895576; cv=fail; b=Y2sMAakZ1oTzqqmxJ4V04F2hyvX9Ny3giqUdReETOBEJsHcSfD+Fbz7ZSX/+jYHFMBgYkY8hAzUcnxCX+f4J5CtzG4XnHtAThG5FMwqf8RW3pJDLEMDk2ZIbLp9489ZBpJxSmC6zVkPqy9NjsbzKwqtj0at14kazfBv858FHcKY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723895576; c=relaxed/simple; bh=CfuplmT9ZxGuZfBc0XO3QS5a0iemxmrvitV2erTzQmc=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=S7l1iY9XtmrsywBsmdZ2xYyGGljnNbyAd6lfo33Xbg2GF6IOi+v2SlksY+WqCXGYsWNw4UG6m7bvWc4EWsb0ZWQzslo8taWGGZOk1BDcA5vWg0tu//BaZKzm1YA7YXJp+hCr2caAAI0xQWggSmySBhXSUGvx4sHAFeSL2GL118Y= 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=Wh8ozI+f; arc=fail smtp.client-ip=40.92.102.16 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="Wh8ozI+f" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=VspMEWQFV0ZfRtAitO4oILXS3CxKZXZAbelO/34lOdxZN8cXqUhwRDkCYU7HLsPpbRz/j657ac5nWhAsTNXubTqT8rli6SOuzJ6V+L2reg5EfHT4v5jD5f/FCETRHSE1HNHglh+XTBEUSH6TwPkVxDHmViUjNR/oqXs0w9WwAAf/lvdT8+DZ+QdP6wiv4bGb+PVOtuUTUns0gaMQGBHtUjgPUbG+biUMsMITkMHCFSSPwoEM/8mDAQVCWDTAx+jXaPEO9st45XyQFLB/s4JBkMLkhBCa/1MLhMWsMnCkYh3fx1lB/oRXmqo4PGyb2IKPT3v4+ggjhqAx17VCQqbpvA== 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=4RUYCikraQFlzfLUIl+MeSYsG9C4rmRBPfP+8woqoPk=; b=j5kbBCn7c4dG1QBQ7SVwuTge1Y7y61Oo+/HFGWnozats7bCYIvc0pJqJN136DvcOFizU1bjOxIOpdXcrYlUIgK7FpdRRoj0rae5bQinwlZ/kkVpgKZTa6PDH32YUejyZodIfuhy/bGwq3vCT8sgZCt1+WxtuU4ZIxmWseuoXyiCrJ+Sat5qN+MValDSwCeAWcuqS91Sa1DeiDqjtBYbx597sXKRUrU5P+bB+/9cN/JJzvTsOmTxqmg9VOOwScHuRgYew5c3V3cR11a8YY4iO6sK0yd3xtCx9vJ4jMjKfYHKKjvukaQ5hEq7iM+IaUDjHV0O6qsPOM6+dKVwIwYTw3w== 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=4RUYCikraQFlzfLUIl+MeSYsG9C4rmRBPfP+8woqoPk=; b=Wh8ozI+fhaUHDxQQB9c4fdNAswC62Gcv7HdKtJr3y6d50DGVwlfdDlLmHCYIR7yiVmtRKhg16rJVMnYkqf3o2UC8A9ni0uw6yiVbMkMl7/ur57AI6zU97V27pKhgGd7O0FplaXvJGWNbMBxjvO2gYyLC4M2cCcr+B/xVKc8m8jMFTNd4GDE/iWhg5zd3DPpl2MCDSCB+un92Fku2x3tmlp/uG8BQjyd1JWMCBvcH6dY1LqmlzNRgQhVJHcJdujsAYDYCvHTQ4wIQMI7hWDxBUUz5x6aXgd9BBm//MmiPhZI1MQKpoG+RAJInQG1eW6l8U7kxRCfbs/qeWHD1Ibu01w== Received: from MA0P287MB0217.INDP287.PROD.OUTLOOK.COM (2603:1096:a01:b3::9) by PN0P287MB0120.INDP287.PROD.OUTLOOK.COM (2603:1096:c01:e3::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.18; Sat, 17 Aug 2024 11:52:22 +0000 Received: from MA0P287MB0217.INDP287.PROD.OUTLOOK.COM ([fe80::98d2:3610:b33c:435a]) by MA0P287MB0217.INDP287.PROD.OUTLOOK.COM ([fe80::98d2:3610:b33c:435a%6]) with mapi id 15.20.7875.016; Sat, 17 Aug 2024 11:52:22 +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" , =?iso-8859-1?q?Thomas_Wei=DFschuh?= CC: Orlando Chamberlain , Kerem Karabay , Linux Kernel Mailing List , "linux-input@vger.kernel.org" , "dri-devel@lists.freedesktop.org" Subject: [PATCH v5 10/10] drm/tiny: add driver for Apple Touch Bars in x86 Macs Thread-Topic: [PATCH v5 10/10] drm/tiny: add driver for Apple Touch Bars in x86 Macs Thread-Index: AQHa8JvrJkw/kjXWmEuT3rJn6wIjMw== Date: Sat, 17 Aug 2024 11:52:22 +0000 Message-ID: <3C9E8938-32EC-44AC-A783-3BFDE2F01290@live.com> 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-tmn: [1KFCbA5awKrFU4tR3ZJPHWa9xSG/S0xMcOTrkYSYRz1O7WCmTQdluWSrh94vGuOqcT4OY+Vm238=] x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MA0P287MB0217:EE_|PN0P287MB0120:EE_ x-ms-office365-filtering-correlation-id: a4ad0fd6-d483-43ff-4cf8-08dcbeb30de7 x-microsoft-antispam: BCL:0;ARA:14566002|19110799003|8060799006|15080799003|461199028|102099032|3412199025|4302099013|440099028|1602099012; x-microsoft-antispam-message-info: PU8iHMlRKONc2L9wAUGlq8N06Wx6nH2ZCtqG6tK4Ngme8GqtrDAQqId3xf8ZpOuKu5IXCSciqn3it1RTPC5q/c7OGMSsCOAugsARtiQvF6tiKPI09bWQJY2z10XukfqdJ19FKKXIQHzQ3NZokRZcxm3cic/WEhBEwWkYMCMN1HLaKD5q6bdWpibJe952wArpsi/NPbIJNpiepwGGmqcQUaL1uSul6uyr3DdPNFKtdzhiJLoxemup0BehuyJoGliQ9x4G03dry9M23/KFgPuOoelOm+M8Km/OvYU675S3ISfDuQqanIAsK08xKV2ZbicYVZQsclaaG96fd2zEORt7XuoH40q9rdZE+g/ffum0sK5O7qTAKKtpjk+0zvPHh6AMWPQi6O3lOd8F2MTs1/SlsQHCVgneWXZZWimrHQdonAVM4mZSELf9kFfdVgnGsw3atxgAxpjgbfHQ9c//hM9cfG0VyBONeaWIZFcaRxOqUCvWpPmcRob/thhE9wh5GPd5/xi7RiATKtPNb6iStc8EmmQ8E0B+47zJfrXlwjZmDVRc5W42deTVhnIDHQUq00V6usuuFYdk1nq7XZeYMzrzPwtPdWWGo2psb6EWrVthoLOW4HHOgOMnxc1Y/HD9oazQe2F3kBuJumXtd4ww055SPgLF/nvp9KdxsZeTFu8tvZjFgU4vYkzGXHnT+g4C0wXYvKhVrryCuiIoRmjODpWYcF62ZOK0ZPrsSXk+TF/jFk7zo9i3RbrPx1gTof3qK/zSOO6u8P4BThvdwez3/t4sY/DKuzyz5VptkstF1DoJ7r8= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?64LyikjrL2NRascgfnTzP8v?= =?iso-8859-1?q?EvD8O/Ix6Nh7vcC9nfCXeChJlXJTabYqCkMtRp2FEZ0TJuQjPsxp7gq2Qmvw?= =?iso-8859-1?q?JkKqqEUf0qLl742eukdz2eQPTkauk/VyMDUj/XTrg6bU39+VzBz5BrKoxaNR?= =?iso-8859-1?q?tnSV8fGjLuBPbmKvfhtrkEO/Z8/X1uN2BwIq5kKHfKNilgKkkLh8KgbFJXsZ?= =?iso-8859-1?q?Z01S3jBhyVQb/lFTMlvxCJYbY+1rV9x58ZrSgYRYofX5rDNpPpkOMw/pswQV?= =?iso-8859-1?q?NpMJQTNqHLQKqGBOL/mKP8+jM/uaUJPMEkKHMQYaSbiCZ9D+XUl/Uk27ao20?= =?iso-8859-1?q?FOOHVme9gXvaem6H9e4IlRSpnG9dUeggczftlqI1V+oNrS3gTeEJc7F21eLp?= =?iso-8859-1?q?LZ3Wg++2GYszxtpHha2ECCu6q0ePx6ERYf1ZK3ogWBFWJdAbwbe2XktsaiF4?= =?iso-8859-1?q?drEm2d9cgx/6d6sv6S8hHWzfz3ZDpL8Ayn4a6pIxJejyaiCvruvJMLWiMcGE?= =?iso-8859-1?q?WvSv6ZvIy4YqOU0zHcR/nAniY6o+kOUOxFJbe4ZRdcmsrP/04GNWu3Qut4Ur?= =?iso-8859-1?q?y/Y811qtS6fy3rXvDFRiydUNi7RKPmZfQgNm7EhufEPl2NVw1Mju4CMU28rm?= =?iso-8859-1?q?EFhTWzs9nOhh1NkNAGiMhfojh59pn4nqWwOI5+iFbK+3XUTdF/46jIFNIlaf?= =?iso-8859-1?q?m92E/Eie2cwxp+GFNkLj6y6uf/gmzrs61O//hwLAnaety/PS7yo/qEIubJ/2?= =?iso-8859-1?q?gmfpTdV52ITpoppUDg4sdS4yI2rsDmbJr9DbHwQ9Ovyp9OF6lOQjFSy2jdQD?= =?iso-8859-1?q?X3atr5Kwr/W6xF8hw/rOTp4t6X7Bee9S1a5LLYWM9tnW05c3OD/kPoRRcRyy?= =?iso-8859-1?q?O4Rz8744vH9lJ7nnnKMwcXPnUcVTAEEU//jRBckoJDacyxStl8co0NvN9kvW?= =?iso-8859-1?q?n0xAjjV6BZfobGrVdvl8BCjZuhHoTJMwEYIKR4de38giuPrGlWvXbLdi42dy?= =?iso-8859-1?q?vk6Vr8u5WTKAY5XxWexBSeDhox2o33dvFxYl5hZMurtlM/uKBm4nqoUApGfC?= =?iso-8859-1?q?i4rgGuHikNTl+r0UPnv1o1RqaFJmArABXkbIDWv7bzDRebuMTsy1hlelqPHN?= =?iso-8859-1?q?vBrGQpFQPkDJ/cF7J3icBUBI5VTpYzYjh7HsBXhIoQLBngawAPkaKEi8SdGv?= =?iso-8859-1?q?+AhYjsj33Ss1XAT2JCZAKBq/7kw9RGI0m+PCnE2dSnxdkh7hlGRbuvFDhlFW?= =?iso-8859-1?q?Z8ktvkIzIkAe2ao4d2d5ursIEukbs0R1KaCLGbGH5jR4Eh5W+PuS82gFBPJ6?= =?iso-8859-1?q?A+7/9DZKk213939GU+JY1ko3hOwQbhndFdcepaLowHjkMJsBMm4wjD+xV4N4?= =?iso-8859-1?q?A?= Content-ID: <5458A91227687840B844127069B3ED23@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-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: a4ad0fd6-d483-43ff-4cf8-08dcbeb30de7 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 17 Aug 2024 11:52:22.1344 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: PN0P287MB0120 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 support for the display functionality of the second configuration. Note that this driver has only been tested on T2 Macs, and only includes the USB device ID for these devices. Testing on T1 Macs would be appreciated. Credit goes to @imbushuo on GitHub for reverse engineering most of the protocol. Signed-off-by: Kerem Karabay Signed-off-by: Aditya Garg --- MAINTAINERS | 6 + drivers/gpu/drm/tiny/Kconfig | 12 + drivers/gpu/drm/tiny/Makefile | 1 + drivers/gpu/drm/tiny/appletbdrm.c | 624 ++++++++++++++++++++++++++++++ 4 files changed, 643 insertions(+) create mode 100644 drivers/gpu/drm/tiny/appletbdrm.c diff --git a/MAINTAINERS b/MAINTAINERS index 8766f3e5e..2665e6c57 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -6889,6 +6889,12 @@ S: Supported T: git https://gitlab.freedesktop.org/drm/misc/kernel.git F: drivers/gpu/drm/sun4i/sun8i* +DRM DRIVER FOR APPLE TOUCH BARS +M: Kerem Karabay +L: dri-devel@lists.freedesktop.org +S: Maintained +F: drivers/gpu/drm/tiny/appletbdrm.c + DRM DRIVER FOR ARM PL111 CLCD S: Orphan T: git https://gitlab.freedesktop.org/drm/misc/kernel.git diff --git a/drivers/gpu/drm/tiny/Kconfig b/drivers/gpu/drm/tiny/Kconfig index f6889f649..559a97bce 100644 --- a/drivers/gpu/drm/tiny/Kconfig +++ b/drivers/gpu/drm/tiny/Kconfig @@ -1,5 +1,17 @@ # SPDX-License-Identifier: GPL-2.0-only +config DRM_APPLETBDRM + tristate "DRM support for Apple Touch Bars" + depends on DRM && USB && MMU + select DRM_KMS_HELPER + select DRM_GEM_SHMEM_HELPER + help + Say Y here if you want support for the display of Touch Bars on x86 + MacBook Pros. + + To compile this driver as a module, choose M here: the + module will be called appletbdrm. + config DRM_ARCPGU tristate "ARC PGU" depends on DRM && OF diff --git a/drivers/gpu/drm/tiny/Makefile b/drivers/gpu/drm/tiny/Makefile index 76dde89a0..9a1b412e7 100644 --- a/drivers/gpu/drm/tiny/Makefile +++ b/drivers/gpu/drm/tiny/Makefile @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_DRM_APPLETBDRM) += appletbdrm.o obj-$(CONFIG_DRM_ARCPGU) += arcpgu.o obj-$(CONFIG_DRM_BOCHS) += bochs.o obj-$(CONFIG_DRM_CIRRUS_QEMU) += cirrus.o diff --git a/drivers/gpu/drm/tiny/appletbdrm.c b/drivers/gpu/drm/tiny/appletbdrm.c new file mode 100644 index 000000000..b9440ce00 --- /dev/null +++ b/drivers/gpu/drm/tiny/appletbdrm.c @@ -0,0 +1,624 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Apple Touch Bar DRM Driver + * + * Copyright (c) 2023 Kerem Karabay + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define _APPLETBDRM_FOURCC(s) (((s)[0] << 24) | ((s)[1] << 16) | ((s)[2] << 8) | (s)[3]) +#define APPLETBDRM_FOURCC(s) _APPLETBDRM_FOURCC(#s) + +#define APPLETBDRM_PIXEL_FORMAT APPLETBDRM_FOURCC(RGBA) /* The actual format is BGR888 */ +#define APPLETBDRM_BITS_PER_PIXEL 24 + +#define APPLETBDRM_MSG_CLEAR_DISPLAY APPLETBDRM_FOURCC(CLRD) +#define APPLETBDRM_MSG_GET_INFORMATION APPLETBDRM_FOURCC(GINF) +#define APPLETBDRM_MSG_UPDATE_COMPLETE APPLETBDRM_FOURCC(UDCL) +#define APPLETBDRM_MSG_SIGNAL_READINESS APPLETBDRM_FOURCC(REDY) + +#define APPLETBDRM_BULK_MSG_TIMEOUT 1000 + +#define drm_to_adev(_drm) container_of(_drm, struct appletbdrm_device, drm) +#define adev_to_udev(adev) interface_to_usbdev(to_usb_interface(adev->dev)) + +struct appletbdrm_device { + struct device *dev; + + u8 in_ep; + u8 out_ep; + + u32 width; + u32 height; + + struct drm_device drm; + struct drm_display_mode mode; + struct drm_connector connector; + struct drm_simple_display_pipe pipe; + + bool readiness_signal_received; +}; + +struct appletbdrm_request_header { + __le16 unk_00; + __le16 unk_02; + __le32 unk_04; + __le32 unk_08; + __le32 size; +} __packed; + +struct appletbdrm_response_header { + u8 unk_00[16]; + u32 msg; +} __packed; + +struct appletbdrm_simple_request { + struct appletbdrm_request_header header; + u32 msg; + u8 unk_14[8]; + __le32 size; +} __packed; + +struct appletbdrm_information { + struct appletbdrm_response_header header; + u8 unk_14[12]; + __le32 width; + __le32 height; + u8 bits_per_pixel; + __le32 bytes_per_row; + __le32 orientation; + __le32 bitmap_info; + u32 pixel_format; + __le32 width_inches; /* floating point */ + __le32 height_inches; /* floating point */ +} __packed; + +struct appletbdrm_frame { + __le16 begin_x; + __le16 begin_y; + __le16 width; + __le16 height; + __le32 buf_size; + u8 buf[]; +} __packed; + +struct appletbdrm_fb_request_footer { + u8 unk_00[12]; + __le32 unk_0c; + u8 unk_10[12]; + __le32 unk_1c; + __le64 timestamp; + u8 unk_28[12]; + __le32 unk_34; + u8 unk_38[20]; + __le32 unk_4c; +} __packed; + +struct appletbdrm_fb_request { + struct appletbdrm_request_header header; + __le16 unk_10; + u8 msg_id; + u8 unk_13[29]; + /* + * Contents of `data`: + * - struct appletbdrm_frame frames[]; + * - struct appletbdrm_fb_request_footer footer; + * - padding to make the total size a multiple of 16 + */ + u8 data[]; +} __packed; + +struct appletbdrm_fb_request_response { + struct appletbdrm_response_header header; + u8 unk_14[12]; + __le64 timestamp; +} __packed; + +static int appletbdrm_send_request(struct appletbdrm_device *adev, + struct appletbdrm_request_header *request, size_t size) +{ + struct usb_device *udev = adev_to_udev(adev); + struct drm_device *drm = &adev->drm; + int ret, actual_size; + + ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, adev->out_ep), + request, size, &actual_size, APPLETBDRM_BULK_MSG_TIMEOUT); + if (ret) { + drm_err(drm, "Failed to send message (%pe)\n", ERR_PTR(ret)); + return ret; + } + + if (actual_size != size) { + drm_err(drm, "Actual size (%d) doesn't match expected size (%lu)\n", + actual_size, size); + return -EIO; + } + + return ret; +} + +static int appletbdrm_read_response(struct appletbdrm_device *adev, + struct appletbdrm_response_header *response, + size_t size, u32 expected_response) +{ + struct usb_device *udev = adev_to_udev(adev); + struct drm_device *drm = &adev->drm; + int ret, actual_size; + +retry: + ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, adev->in_ep), + response, size, &actual_size, APPLETBDRM_BULK_MSG_TIMEOUT); + if (ret) { + drm_err(drm, "Failed to read response (%pe)\n", ERR_PTR(ret)); + return ret; + } + + /* + * The device responds to the first request sent in a particular + * timeframe after the USB device configuration is set with a readiness + * signal, in which case the response should be read again + */ + if (response->msg == APPLETBDRM_MSG_SIGNAL_READINESS) { + if (!adev->readiness_signal_received) { + adev->readiness_signal_received = true; + goto retry; + } + + drm_err(drm, "Encountered unexpected readiness signal\n"); + return -EIO; + } + + if (actual_size != size) { + drm_err(drm, "Actual size (%d) doesn't match expected size (%lu)\n", + actual_size, size); + return -EIO; + } + + if (response->msg != expected_response) { + drm_err(drm, "Unexpected response from device (expected %p4ch found %p4ch)\n", + &expected_response, &response->msg); + return -EIO; + } + + return 0; +} + +static int appletbdrm_send_msg(struct appletbdrm_device *adev, u32 msg) +{ + struct appletbdrm_simple_request *request; + int ret; + + request = kzalloc(sizeof(*request), GFP_KERNEL); + if (!request) + return -ENOMEM; + + request->header.unk_00 = cpu_to_le16(2); + request->header.unk_02 = cpu_to_le16(0x1512); + request->header.size = cpu_to_le32(sizeof(*request) - sizeof(request->header)); + request->msg = msg; + request->size = request->header.size; + + ret = appletbdrm_send_request(adev, &request->header, sizeof(*request)); + + kfree(request); + + return ret; +} + +static int appletbdrm_clear_display(struct appletbdrm_device *adev) +{ + return appletbdrm_send_msg(adev, APPLETBDRM_MSG_CLEAR_DISPLAY); +} + +static int appletbdrm_signal_readiness(struct appletbdrm_device *adev) +{ + return appletbdrm_send_msg(adev, APPLETBDRM_MSG_SIGNAL_READINESS); +} + +static int appletbdrm_get_information(struct appletbdrm_device *adev) +{ + struct appletbdrm_information *info; + struct drm_device *drm = &adev->drm; + u8 bits_per_pixel; + u32 pixel_format; + int ret; + + info = kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) + return -ENOMEM; + + ret = appletbdrm_send_msg(adev, APPLETBDRM_MSG_GET_INFORMATION); + if (ret) + return ret; + + ret = appletbdrm_read_response(adev, &info->header, sizeof(*info), + APPLETBDRM_MSG_GET_INFORMATION); + if (ret) + goto free_info; + + bits_per_pixel = info->bits_per_pixel; + pixel_format = get_unaligned(&info->pixel_format); + + adev->width = get_unaligned_le32(&info->width); + adev->height = get_unaligned_le32(&info->height); + + if (bits_per_pixel != APPLETBDRM_BITS_PER_PIXEL) { + drm_err(drm, "Encountered unexpected bits per pixel value (%d)\n", bits_per_pixel); + ret = -EINVAL; + goto free_info; + } + + if (pixel_format != APPLETBDRM_PIXEL_FORMAT) { + drm_err(drm, "Encountered unknown pixel format (%p4ch)\n", &pixel_format); + ret = -EINVAL; + goto free_info; + } + +free_info: + kfree(info); + + return ret; +} + +static u32 rect_size(struct drm_rect *rect) +{ + return drm_rect_width(rect) * drm_rect_height(rect) * (APPLETBDRM_BITS_PER_PIXEL / 8); +} + +static int appletbdrm_flush_damage(struct appletbdrm_device *adev, + struct drm_plane_state *old_state, + struct drm_plane_state *state) +{ + struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state); + struct appletbdrm_fb_request_response *response; + struct appletbdrm_fb_request_footer *footer; + struct drm_atomic_helper_damage_iter iter; + struct drm_framebuffer *fb = state->fb; + struct appletbdrm_fb_request *request; + struct drm_device *drm = &adev->drm; + struct appletbdrm_frame *frame; + u64 timestamp = ktime_get_ns(); + struct drm_rect damage; + size_t frames_size = 0; + size_t request_size; + int ret; + + drm_atomic_helper_damage_iter_init(&iter, old_state, state); + drm_atomic_for_each_plane_damage(&iter, &damage) { + frames_size += struct_size(frame, buf, rect_size(&damage)); + } + + if (!frames_size) + return 0; + + request_size = ALIGN(sizeof(*request) + frames_size + sizeof(*footer), 16); + + request = kzalloc(request_size, GFP_KERNEL); + if (!request) + return -ENOMEM; + + response = kzalloc(sizeof(*response), GFP_KERNEL); + if (!response) { + ret = -ENOMEM; + goto free_request; + } + + ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); + if (ret) { + drm_err(drm, "Failed to start CPU framebuffer access (%pe)\n", ERR_PTR(ret)); + goto free_response; + } + + request->header.unk_00 = cpu_to_le16(2); + request->header.unk_02 = cpu_to_le16(0x12); + request->header.unk_04 = cpu_to_le32(9); + request->header.size = cpu_to_le32(request_size - sizeof(request->header)); + request->unk_10 = cpu_to_le16(1); + request->msg_id = timestamp & 0xff; + + frame = (struct appletbdrm_frame *)request->data; + + drm_atomic_helper_damage_iter_init(&iter, old_state, state); + drm_atomic_for_each_plane_damage(&iter, &damage) { + struct iosys_map dst = IOSYS_MAP_INIT_VADDR(frame->buf); + u32 buf_size = rect_size(&damage); + + /* + * The coordinates need to be translated to the coordinate + * system the device expects, see the comment in + * appletbdrm_setup_mode_config + */ + frame->begin_x = cpu_to_le16(damage.y1); + frame->begin_y = cpu_to_le16(adev->height - damage.x2); + frame->width = cpu_to_le16(drm_rect_height(&damage)); + frame->height = cpu_to_le16(drm_rect_width(&damage)); + frame->buf_size = cpu_to_le32(buf_size); + + ret = drm_fb_blit(&dst, NULL, DRM_FORMAT_BGR888, + &shadow_plane_state->data[0], fb, &damage, &shadow_plane_state->fmtcnv_state); + if (ret) { + drm_err(drm, "Failed to copy damage clip (%pe)\n", ERR_PTR(ret)); + goto end_fb_cpu_access; + } + + frame = (void *)frame + struct_size(frame, buf, buf_size); + } + + footer = (struct appletbdrm_fb_request_footer *)&request->data[frames_size]; + + footer->unk_0c = cpu_to_le32(0xfffe); + footer->unk_1c = cpu_to_le32(0x80001); + footer->unk_34 = cpu_to_le32(0x80002); + footer->unk_4c = cpu_to_le32(0xffff); + footer->timestamp = cpu_to_le64(timestamp); + + ret = appletbdrm_send_request(adev, &request->header, request_size); + if (ret) + goto end_fb_cpu_access; + + ret = appletbdrm_read_response(adev, &response->header, sizeof(*response), + APPLETBDRM_MSG_UPDATE_COMPLETE); + if (ret) + goto end_fb_cpu_access; + + if (response->timestamp != footer->timestamp) { + drm_err(drm, "Response timestamp (%llu) doesn't match request timestamp (%llu)\n", + le64_to_cpu(response->timestamp), timestamp); + goto end_fb_cpu_access; + } + +end_fb_cpu_access: + drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); +free_response: + kfree(response); +free_request: + kfree(request); + + return ret; +} + +static int appletbdrm_connector_helper_get_modes(struct drm_connector *connector) +{ + struct appletbdrm_device *adev = drm_to_adev(connector->dev); + + return drm_connector_helper_get_modes_fixed(connector, &adev->mode); +} + +static enum drm_mode_status appletbdrm_pipe_mode_valid(struct drm_simple_display_pipe *pipe, + const struct drm_display_mode *mode) +{ + struct drm_crtc *crtc = &pipe->crtc; + struct appletbdrm_device *adev = drm_to_adev(crtc->dev); + + return drm_crtc_helper_mode_valid_fixed(crtc, mode, &adev->mode); +} + +static void appletbdrm_pipe_disable(struct drm_simple_display_pipe *pipe) +{ + struct appletbdrm_device *adev = drm_to_adev(pipe->crtc.dev); + int idx; + + if (!drm_dev_enter(&adev->drm, &idx)) + return; + + appletbdrm_clear_display(adev); + + drm_dev_exit(idx); +} + +static void appletbdrm_pipe_update(struct drm_simple_display_pipe *pipe, + struct drm_plane_state *old_state) +{ + struct drm_crtc *crtc = &pipe->crtc; + struct appletbdrm_device *adev = drm_to_adev(crtc->dev); + int idx; + + if (!crtc->state->active || !drm_dev_enter(&adev->drm, &idx)) + return; + + appletbdrm_flush_damage(adev, old_state, pipe->plane.state); + + drm_dev_exit(idx); +} + +static const u32 appletbdrm_formats[] = { + DRM_FORMAT_BGR888, + DRM_FORMAT_XRGB8888, /* emulated */ +}; + +static const struct drm_mode_config_funcs appletbdrm_mode_config_funcs = { + .fb_create = drm_gem_fb_create_with_dirty, + .atomic_check = drm_atomic_helper_check, + .atomic_commit = drm_atomic_helper_commit, +}; + +static const struct drm_connector_funcs appletbdrm_connector_funcs = { + .reset = drm_atomic_helper_connector_reset, + .destroy = drm_connector_cleanup, + .fill_modes = drm_helper_probe_single_connector_modes, + .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, + .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, +}; + +static const struct drm_connector_helper_funcs appletbdrm_connector_helper_funcs = { + .get_modes = appletbdrm_connector_helper_get_modes, +}; + +static const struct drm_simple_display_pipe_funcs appletbdrm_pipe_funcs = { + DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS, + .update = appletbdrm_pipe_update, + .disable = appletbdrm_pipe_disable, + .mode_valid = appletbdrm_pipe_mode_valid, +}; + +DEFINE_DRM_GEM_FOPS(appletbdrm_drm_fops); + +static const struct drm_driver appletbdrm_drm_driver = { + DRM_GEM_SHMEM_DRIVER_OPS, + .name = "appletbdrm", + .desc = "Apple Touch Bar DRM Driver", + .date = "20230910", + .major = 1, + .minor = 0, + .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, + .fops = &appletbdrm_drm_fops, +}; + +static int appletbdrm_setup_mode_config(struct appletbdrm_device *adev) +{ + struct drm_connector *connector = &adev->connector; + struct drm_device *drm = &adev->drm; + struct device *dev = adev->dev; + int ret; + + ret = drmm_mode_config_init(drm); + if (ret) + return dev_err_probe(dev, ret, "Failed to initialize mode configuration\n"); + + /* + * The coordinate system used by the device is different from the + * coordinate system of the framebuffer in that the x and y axes are + * swapped, and that the y axis is inverted; so what the device reports + * as the height is actually the width of the framebuffer and vice + * versa + */ + drm->mode_config.min_width = 0; + drm->mode_config.min_height = 0; + drm->mode_config.max_width = max(adev->height, DRM_SHADOW_PLANE_MAX_WIDTH); + drm->mode_config.max_height = max(adev->width, DRM_SHADOW_PLANE_MAX_HEIGHT); + drm->mode_config.preferred_depth = APPLETBDRM_BITS_PER_PIXEL; + drm->mode_config.funcs = &appletbdrm_mode_config_funcs; + + adev->mode = (struct drm_display_mode) { + DRM_MODE_INIT(60, adev->height, adev->width, + DRM_MODE_RES_MM(adev->height, 218), + DRM_MODE_RES_MM(adev->width, 218)) + }; + + ret = drm_connector_init(drm, connector, + &appletbdrm_connector_funcs, DRM_MODE_CONNECTOR_USB); + if (ret) + return dev_err_probe(dev, ret, "Failed to initialize connector\n"); + + drm_connector_helper_add(connector, &appletbdrm_connector_helper_funcs); + + ret = drm_connector_set_panel_orientation(connector, + DRM_MODE_PANEL_ORIENTATION_RIGHT_UP); + if (ret) + return dev_err_probe(dev, ret, "Failed to set panel orientation\n"); + + connector->display_info.non_desktop = true; + ret = drm_object_property_set_value(&connector->base, + drm->mode_config.non_desktop_property, true); + if (ret) + return dev_err_probe(dev, ret, "Failed to set non-desktop property\n"); + + ret = drm_simple_display_pipe_init(drm, &adev->pipe, &appletbdrm_pipe_funcs, + appletbdrm_formats, ARRAY_SIZE(appletbdrm_formats), + NULL, &adev->connector); + if (ret) + return dev_err_probe(dev, ret, "Failed to initialize simple display pipe\n"); + + drm_plane_enable_fb_damage_clips(&adev->pipe.plane); + + drm_mode_config_reset(drm); + + ret = drm_dev_register(drm, 0); + if (ret) + return dev_err_probe(dev, ret, "Failed to register DRM device\n"); + + return 0; +} + +static int appletbdrm_probe(struct usb_interface *intf, + const struct usb_device_id *id) +{ + struct usb_endpoint_descriptor *bulk_in, *bulk_out; + struct device *dev = &intf->dev; + struct appletbdrm_device *adev; + int ret; + + ret = usb_find_common_endpoints(intf->cur_altsetting, &bulk_in, &bulk_out, NULL, NULL); + if (ret) + return dev_err_probe(dev, ret, "Failed to find bulk endpoints\n"); + + adev = devm_drm_dev_alloc(dev, &appletbdrm_drm_driver, struct appletbdrm_device, drm); + if (IS_ERR(adev)) + return PTR_ERR(adev); + + adev->dev = dev; + adev->in_ep = bulk_in->bEndpointAddress; + adev->out_ep = bulk_out->bEndpointAddress; + + usb_set_intfdata(intf, adev); + + ret = appletbdrm_get_information(adev); + if (ret) + return dev_err_probe(dev, ret, "Failed to get display information\n"); + + ret = appletbdrm_signal_readiness(adev); + if (ret) + return dev_err_probe(dev, ret, "Failed to signal readiness\n"); + + ret = appletbdrm_clear_display(adev); + if (ret) + return dev_err_probe(dev, ret, "Failed to clear display\n"); + + return appletbdrm_setup_mode_config(adev); +} + +static void appletbdrm_disconnect(struct usb_interface *intf) +{ + struct appletbdrm_device *adev = usb_get_intfdata(intf); + struct drm_device *drm = &adev->drm; + + drm_dev_unplug(drm); + drm_atomic_helper_shutdown(drm); +} + +static void appletbdrm_shutdown(struct usb_interface *intf) +{ + struct appletbdrm_device *adev = usb_get_intfdata(intf); + + /* + * The framebuffer needs to be cleared on shutdown since its content + * persists across boots + */ + drm_atomic_helper_shutdown(&adev->drm); +} + +static const struct usb_device_id appletbdrm_usb_id_table[] = { + { USB_DEVICE_INTERFACE_CLASS(0x05ac, 0x8302, USB_CLASS_AUDIO_VIDEO) }, + {} +}; +MODULE_DEVICE_TABLE(usb, appletbdrm_usb_id_table); + +static struct usb_driver appletbdrm_usb_driver = { + .name = "appletbdrm", + .probe = appletbdrm_probe, + .disconnect = appletbdrm_disconnect, + .shutdown = appletbdrm_shutdown, + .id_table = appletbdrm_usb_id_table, +}; +module_usb_driver(appletbdrm_usb_driver); + +MODULE_AUTHOR("Kerem Karabay "); +MODULE_DESCRIPTION("Apple Touch Bar DRM Driver"); +MODULE_LICENSE("GPL");