From patchwork Tue Oct 17 11:00:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 735352 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA247C41513 for ; Tue, 17 Oct 2023 11:00:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233570AbjJQLAW (ORCPT ); Tue, 17 Oct 2023 07:00:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234599AbjJQLAV (ORCPT ); Tue, 17 Oct 2023 07:00:21 -0400 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2047.outbound.protection.outlook.com [40.107.249.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEEE7EA; Tue, 17 Oct 2023 04:00:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Sy1olJNOhmDHRBOhus44YvgfCURDJ8BkBmXgF5EyUwn05arVsyDHwZVQf5qTGcfNveYe3dWV9BcjT5SWFufnyVGL58sv/jDcy2vaoDSR36rlB56iGmxXiaMDhSgEc6kwfDrD7CprQ4sfv99Map6oOUFe8W2stgP6ptHMKzHFxr/aUAZ0CuW0Osv0cIVzpN/2ezFuqwNhWcWuzAfIypkPgXCa9Cft+xOLd8FFdoVqFbBWLsslqUY8v5JgTvMuho6LGAe+X+98ZV4FzACbg1yHB3evvED8ksIB5MSk86oJAxYhmQQZbBwsqTw4PZ3rElE9RJ6nC6aXDh+396cUq/Xezg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=OJEi3vJTduqpwezb9xJhU4ifPF+ITuUK8LQovSBDtjI=; b=h6zi0FlKCBmQpU1KBuQ6OiIqS7JIINA3sT4Os/aYdv1Nh+E9ckAcclvUgYhgCHlZj3uAkLc1PwFs91mKSaxA/Cf3iPjFLhvgUSgfYa6qSd0I2myjZhowaOLzfQptF5sHn4ZFVaSvY69rWd3rg8LJh873CclEG+MBylyxV6duh0KW8ak2hUvjATA/QwA8aA56oviG8C5f9Qb+DILac/B2Sj8xZJDZI8iL7AgJoM5qaadKdF8MXcg3XGhTU35bRsPwVvtQr8FBa1nxXP6KCrAccKxvP4r4wjwpPZ/YFzAdYhDRo/clDpxUSqc24zYcxHdeSfSOItg9VcvK16euokf5NA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wolfvision.net; dmarc=pass action=none header.from=wolfvision.net; dkim=pass header.d=wolfvision.net; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wolfvision.net; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OJEi3vJTduqpwezb9xJhU4ifPF+ITuUK8LQovSBDtjI=; b=nBYptv/IjX+ZInccxbjyVS1XcywJEfJSC79fxvlYFQfl+J97PCtKrT7VmqPGmB1+E8SA0vEZHsbXfJpZEMTBm+gTxuLXKwvZLopsLoDMsCDBZWO+urA3i91eaR/jIjZ6LSQgLyVHW2QMEVlGBrDRVlNe9U2A1/XURr1toFSbuGA= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=wolfvision.net; Received: from VE1PR08MB4974.eurprd08.prod.outlook.com (2603:10a6:803:111::15) by AS8PR08MB6295.eurprd08.prod.outlook.com (2603:10a6:20b:295::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6886.36; Tue, 17 Oct 2023 11:00:14 +0000 Received: from VE1PR08MB4974.eurprd08.prod.outlook.com ([fe80::bc92:216b:11ed:db63]) by VE1PR08MB4974.eurprd08.prod.outlook.com ([fe80::bc92:216b:11ed:db63%6]) with mapi id 15.20.6886.034; Tue, 17 Oct 2023 11:00:14 +0000 From: Javier Carrasco Date: Tue, 17 Oct 2023 13:00:07 +0200 Subject: [PATCH v5 1/4] Input: touch-overlay - Add touchscreen overlay object handling Message-Id: <20230510-feature-ts_virtobj_patch-v5-1-ff6b5c4db693@wolfvision.net> References: <20230510-feature-ts_virtobj_patch-v5-0-ff6b5c4db693@wolfvision.net> In-Reply-To: <20230510-feature-ts_virtobj_patch-v5-0-ff6b5c4db693@wolfvision.net> To: Dmitry Torokhov , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Henrik Rydberg , Bastian Hecht , Michael Riesch Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, devicetree@vger.kernel.org, Javier Carrasco X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1697540413; l=13918; i=javier.carrasco@wolfvision.net; s=20230509; h=from:subject:message-id; bh=qqwR/kVL/sYBH0UecdndEJ2NdLb3YcmcKBDR3n9upRE=; b=x37HELZ1xdHJkxwJd6Atm9hDaBs3yJWMpyRpcia9Yz1XStauFQqcc0A1QrqNeg1he36APZDg/ ZyVFcebCV1nAIlKguXz7Z7oxcCPgR1OhsK6ovF8DPCQbhhlh8Qv75Nl X-Developer-Key: i=javier.carrasco@wolfvision.net; a=ed25519; pk=tIGJV7M+tCizagNijF0eGMBGcOsPD+0cWGfKjl4h6K8= X-ClientProxiedBy: VI1PR07CA0250.eurprd07.prod.outlook.com (2603:10a6:803:b4::17) To VE1PR08MB4974.eurprd08.prod.outlook.com (2603:10a6:803:111::15) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VE1PR08MB4974:EE_|AS8PR08MB6295:EE_ X-MS-Office365-Filtering-Correlation-Id: 5ce8f732-03d9-45e6-e69a-08dbcf003d81 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NJdf0Hkaftpq/D3rwJLoFPwnLFyQY6koaFvJPYdLXv8G2k7RpaQdfnOiENzn7A2Cxy38GkEoG601+UUM660pzQa7vBEp+90101jnFMfNmKyL1BUBLCKu46LezitrO5/lk1pCZspk77ooSXMU2uWdy3BC6EFrleSE41Y5/sik4paY/Di46D264VKrbX9eDAqLT8Ak3OxsUvz3FnwUkn6hQtF8hTLIL8zb8ICN+KlrXhexZYl3zzWhdD3mVDdTvb+tulx169zyP0i9VjSpqfzhbNvb5yFf9Ow0XwaiKolGM9ZPlNj030KwJM8lAqd+G/vOVwe4C7UU6ZAgTIuFZ7qoxvPCakkFoy/27JxtnlX4hLxfh3QAxe/BK0Gzpvi7Owy5mkBZHSdwKi1SOWmha+tcFlUtt4jBIJQUbZUv46aO6VAFLRXcl71pjE00QzjLDjb6gsf6pluWx4A5Y4WKsn3WkkJKX8MQjOrwr4+4QFUxbPD3hCgw4yeNsY2HEGuJAkG5Xol7Q9wHjiJSVb84c0K+7Pkm/2ny1rDrqXVl9M0v1wu6BdsiEV9LPNm1sAc34WRwzatJT+J9RrAgKMMUdI8kbQrbIqQ8tpVo14B5gm5ZfbE= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VE1PR08MB4974.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(136003)(346002)(376002)(39840400004)(396003)(366004)(230922051799003)(451199024)(64100799003)(186009)(1800799009)(107886003)(478600001)(38100700002)(52116002)(6506007)(2616005)(6486002)(38350700005)(83380400001)(26005)(966005)(6512007)(6666004)(41300700001)(44832011)(5660300002)(4326008)(8936002)(8676002)(316002)(66946007)(30864003)(86362001)(110136005)(2906002)(66556008)(66476007)(6636002)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?B/GSu/CJ3RdWBGm2+foWmZAa+HKs?= =?utf-8?q?zFxFZuu1ilXtcqYlXMgbw1x7EStQWAknDhpzBoApuGqNzjBlZbKH4BWff3W6vZ5zy?= =?utf-8?q?p+PflYoYHiVkEHYDTUDl1NxmZmgqgirtIddj6LbQArMA0RBfM5qnnqDt2SjrS3w4x?= =?utf-8?q?JbvQflO3BY4V2toY++FbJGxsGwubEs5w+ore/r3IF+0EMW7yOkcghFr5sQ7k4ckLh?= =?utf-8?q?Gr2TewfZusxZP9winHcpOMzaN9X/zI0ShkS12mAPOPUSXYQBq7BEcqXpv/uOt/4rd?= =?utf-8?q?fSdX1uhnb6KGdN482wR5kEnCWlzqlGf97N5tUVgSJdSce8YL1ICMX/l587uR+Fl06?= =?utf-8?q?fm0PtenUDCNC9YqOtCW+98dFojHkXgS3Sx6B19a5LqDWKqkA2Cnz18TtP9IcqXERG?= =?utf-8?q?nr45WIFPusZCB/rtLOJyMWNF3gyDqi0KgdJTS55Um7OJvHLYAHAsQv4ApLbPizsMm?= =?utf-8?q?5IhMWG/BOBCljUHdU/JzIdGX3ul2ikPIYNRvCxveF9WTuLbXvUBrzfLjpgFwT+ScX?= =?utf-8?q?qG/MIDF6b262rmAuyU6LeAbBLp7Tx2eSTeXQLHnccHvrikaOto1vdL/RsWP8GCVrw?= =?utf-8?q?OjgaGb7c+G2OY6K2p+u72aFyoC25ZNTZjxRXkwlVWAouRP7W9iMmlYfkDsrv5uoeW?= =?utf-8?q?Y1eqjScfcDQTqQbKOQqo4vrwS+c5+wRXvcCVdMMd4tffXWLrxnjwTwvVpc8G7CM3u?= =?utf-8?q?T75vbiRs8fNLm67ECtlq/vjFMHb14vZ3iC7jnj8I7zVvOfw4CpQX7rarj0gvaGscG?= =?utf-8?q?aAUtok6zrhqaL2WKT8G8brVVudDrY3GUXmiI3MgG/nTVjtJUTw/yWTKOsGMVe4eLd?= =?utf-8?q?Qq3SfKbWIKeanORan/mZ0kUmS2FvuNaYDMyOjh6oCOAl8t8M7JcUbgs+u2g27xvts?= =?utf-8?q?jMlRgl21CidxqL5R0WDLVS1poddznZ/nmNk3/w7CF6hdXIpiELsGhF0CuloNng3yi?= =?utf-8?q?yypbWtEXkvZeEFHXf6l1JyzvH7v/jNSXUYkqsWQ+6ia1ek3fcKJyYkOwGT1jQrfHX?= =?utf-8?q?ZWqn6aU1WGtOdhB5EN+r82ySAew9p0YyyTtK+7flWaExUHy/9enCw+BqGpWvGeTT6?= =?utf-8?q?HUCuAcAqV9jQh8uPrI/y/Wdej7DELbeDz9VjpJCYAKeuPCkaJ78RKFPnxiUmTFsr6?= =?utf-8?q?sLyrOk/nWxH7OxzmuodXMw6j4glulosYhf5VDtrvIQyRpRUvu0nfshBh+jemj1Yzo?= =?utf-8?q?jKbeVnluOicwFquNwHRY61XN/oXjObhWXJ40PQeX8UaXIp8afa13Ts7UcuUv4z4y8?= =?utf-8?q?4VINtz1i8ZjBZHKgXBRqOwp4PP2+pUPvfpGHf1Ui8oWR/9g5jcalz4LLB6BdoYdfH?= =?utf-8?q?VPQfrMc53BEFJWm4EjEb276lH0TLe2ev4W7uvtikM3KCWdr+mVJGHOAGRXVQAuwbb?= =?utf-8?q?yQ7Pox2B/GODqnZl/F2l8slqhOUL5B3oFrX+K7rd7mf4UH8m/E3LhyBBuW+aTEwIL?= =?utf-8?q?GqnixRrSBnr2vFUQv3vcRhpflTG/HzoUYWVpBz+5ktOiB2MISqivN59Pt+tmSw39I?= =?utf-8?q?oJeWqpXi+csX689kvBOLdHwevsH64wpIsx/HlXGpyc61jDiNb97RNw8=3D?= X-OriginatorOrg: wolfvision.net X-MS-Exchange-CrossTenant-Network-Message-Id: 5ce8f732-03d9-45e6-e69a-08dbcf003d81 X-MS-Exchange-CrossTenant-AuthSource: VE1PR08MB4974.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Oct 2023 11:00:14.3346 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: e94ec9da-9183-471e-83b3-51baa8eb804f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: sp/yNjIl2+KKHBb0kwFSlZDQSvsSuPeMtRMoDHcdppRPPskU5/LSfUoa5Hn+yGcMvcXvmcWmuQaCPxyBX2cad0kN7H20LbI1CCnvKNoXbS0= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB6295 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Some touch devices provide mechanical overlays with different objects like buttons or clipped touchscreen surfaces. In order to support these objects, add a series of helper functions to the input subsystem to transform them into overlay objects via device tree nodes. These overlay objects consume the raw touch events and report the expected input events depending on the object properties. Signed-off-by: Javier Carrasco --- MAINTAINERS | 7 + drivers/input/Makefile | 2 +- drivers/input/touch-overlay.c | 399 ++++++++++++++++++++++++++++++++++++ include/linux/input/touch-overlay.h | 34 +++ 4 files changed, 441 insertions(+), 1 deletion(-) diff --git a/MAINTAINERS b/MAINTAINERS index 7a7bd8bd80e9..00c03824c3ac 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -21884,6 +21884,13 @@ W: https://github.com/srcres258/linux-doc T: git git://github.com/srcres258/linux-doc.git doc-zh-tw F: Documentation/translations/zh_TW/ +TOUCH OVERLAY OBJECTS +M: Javier Carrasco +L: linux-input@vger.kernel.org +S: Maintained +F: drivers/input/touch-overlay.c +F: include/linux/input/touch-overlay.h + TTY LAYER AND SERIAL DRIVERS M: Greg Kroah-Hartman M: Jiri Slaby diff --git a/drivers/input/Makefile b/drivers/input/Makefile index c78753274921..393e9f4d00dc 100644 --- a/drivers/input/Makefile +++ b/drivers/input/Makefile @@ -7,7 +7,7 @@ obj-$(CONFIG_INPUT) += input-core.o input-core-y := input.o input-compat.o input-mt.o input-poller.o ff-core.o -input-core-y += touchscreen.o +input-core-y += touchscreen.o touch-overlay.o obj-$(CONFIG_INPUT_FF_MEMLESS) += ff-memless.o obj-$(CONFIG_INPUT_SPARSEKMAP) += sparse-keymap.o diff --git a/drivers/input/touch-overlay.c b/drivers/input/touch-overlay.c new file mode 100644 index 000000000000..007dbd994474 --- /dev/null +++ b/drivers/input/touch-overlay.c @@ -0,0 +1,399 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Helper functions for overlay objects on touchscreens + * + * Copyright (c) 2023 Javier Carrasco + */ + +#include +#include +#include +#include +#include + +enum touch_overlay_valid_objects { + TOUCH_OVERLAY_TS, + TOUCH_OVERLAY_BTN, +}; + +static const char *const object_names[] = { + [TOUCH_OVERLAY_TS] = "overlay-touchscreen", + [TOUCH_OVERLAY_BTN] = "overlay-buttons", +}; + +struct touch_overlay_segment { + u32 x_origin; + u32 y_origin; + u32 x_size; + u32 y_size; +}; + +struct touch_overlay_button { + struct touch_overlay_segment segment; + u32 key; + bool pressed; + int slot; +}; + +static int touch_overlay_get_segment_props(struct fwnode_handle *segment_node, + struct touch_overlay_segment *segment) +{ + int error; + + error = fwnode_property_read_u32(segment_node, "x-origin", + &segment->x_origin); + if (error < 0) + return error; + + error = fwnode_property_read_u32(segment_node, "y-origin", + &segment->y_origin); + if (error < 0) + return error; + + error = fwnode_property_read_u32(segment_node, "x-size", + &segment->x_size); + if (error < 0) + return error; + + error = fwnode_property_read_u32(segment_node, "y-size", + &segment->y_size); + if (error < 0) + return error; + + return 0; +} + +static int +touch_overlay_get_button_properties(struct device *dev, + struct fwnode_handle *overlay_node, + struct touch_overlay_button *btn) +{ + struct fwnode_handle *btn_node; + int error; + int j = 0; + + fwnode_for_each_child_node(overlay_node, btn_node) { + error = touch_overlay_get_segment_props(btn_node, + &btn[j].segment); + if (error < 0) + goto button_put; + + error = fwnode_property_read_u32(btn_node, "linux,code", + &btn[j].key); + if (error < 0) + goto button_put; + + dev_dbg(dev, "Added button at (%u, %u), size %ux%u, code=%u\n", + btn[j].segment.x_origin, btn[j].segment.y_origin, + btn[j].segment.x_size, btn[j].segment.y_size, btn[j].key); + j++; + } + + return 0; + +button_put: + fwnode_handle_put(btn_node); + return error; +} + +static void touch_overlay_set_button_caps(struct touch_overlay_map *map, + struct input_dev *dev) +{ + int i; + + for (i = 0; i < map->button_count; i++) + input_set_capability(dev, EV_KEY, map->buttons[i].key); +} + +static int touch_overlay_count_buttons(struct device *dev) +{ + struct fwnode_handle *overlay; + struct fwnode_handle *button; + int count = 0; + + overlay = device_get_named_child_node(dev, + object_names[TOUCH_OVERLAY_BTN]); + if (!overlay) + return 0; + + fwnode_for_each_child_node(overlay, button) + count++; + fwnode_handle_put(overlay); + + return count; +} + +static int touch_overlay_map_touchscreen(struct device *dev, + struct touch_overlay_map *map) +{ + struct fwnode_handle *ts_node; + int error = 0; + + ts_node = device_get_named_child_node(dev, + object_names[TOUCH_OVERLAY_TS]); + if (!ts_node) + return 0; + + map->touchscreen = + devm_kzalloc(dev, sizeof(*map->touchscreen), GFP_KERNEL); + if (!map->touchscreen) { + error = -ENOMEM; + goto handle_put; + } + error = touch_overlay_get_segment_props(ts_node, map->touchscreen); + if (error < 0) + goto handle_put; + + map->overlay_touchscreen = true; + dev_dbg(dev, "Added overlay touchscreen at (%u, %u), size %u x %u\n", + map->touchscreen->x_origin, map->touchscreen->y_origin, + map->touchscreen->x_size, map->touchscreen->y_size); + +handle_put: + fwnode_handle_put(ts_node); + + return error; +} + +static int touch_overlay_map_buttons(struct device *dev, + struct touch_overlay_map *map) +{ + struct fwnode_handle *button; + u32 count; + int error = 0; + + count = touch_overlay_count_buttons(dev); + if (!count) + return 0; + + map->buttons = devm_kcalloc(dev, count, + sizeof(*map->buttons), GFP_KERNEL); + if (!map->buttons) { + error = -ENOMEM; + goto map_buttons_ret; + } + button = device_get_named_child_node(dev, + object_names[TOUCH_OVERLAY_BTN]); + if (unlikely(!button)) { + error = -ENODEV; + goto map_buttons_ret; + } + + error = touch_overlay_get_button_properties(dev, button, + map->buttons); + + if (error < 0) + goto map_buttons_put; + + map->button_count = count; + +map_buttons_put: + fwnode_handle_put(button); +map_buttons_ret: + return error; +} + +static bool touch_overlay_defined_objects(struct device *dev) +{ + struct fwnode_handle *obj_node; + int i; + + for (i = 0; i < ARRAY_SIZE(object_names); i++) { + obj_node = device_get_named_child_node(dev, object_names[i]); + if (obj_node) { + fwnode_handle_put(obj_node); + return true; + } + fwnode_handle_put(obj_node); + } + + return false; +} + +/** + * touch_overlay_map_overlay - map overlay objects from the device tree and set + * key capabilities if buttons are defined. + * @keypad: pointer to the already allocated input_dev. + * + * Returns a pointer to the object mapping struct. + * + * If a keypad input device is provided and overlay buttons are defined, + * its button capabilities are set accordingly. + */ +struct touch_overlay_map *touch_overlay_map_overlay(struct input_dev *keypad) +{ + struct device *dev = keypad->dev.parent; + struct touch_overlay_map *map = NULL; + int error; + + if (!touch_overlay_defined_objects(dev)) + return NULL; + + map = devm_kzalloc(dev, sizeof(*map), GFP_KERNEL); + if (!map) { + error = -ENOMEM; + goto map_error; + } + error = touch_overlay_map_touchscreen(dev, map); + if (error < 0) + goto map_error; + + error = touch_overlay_map_buttons(dev, map); + if (error < 0) + goto map_error; + + touch_overlay_set_button_caps(map, keypad); + + return map; + +map_error: + return ERR_PTR(error); +} +EXPORT_SYMBOL(touch_overlay_map_overlay); + +/** + * touch_overlay_get_touchscreen_abs - get abs size from the overlay node + * @map: pointer to the struct that holds the object mapping + * @x: horizontal abs + * @y: vertical abs + * + */ +void touch_overlay_get_touchscreen_abs(struct touch_overlay_map *map, u16 *x, + u16 *y) +{ + *x = map->touchscreen->x_size - 1; + *y = map->touchscreen->y_size - 1; +} +EXPORT_SYMBOL(touch_overlay_get_touchscreen_abs); + +static bool touch_overlay_segment_event(struct touch_overlay_segment *seg, + u32 x, u32 y) +{ + if (!seg) + return false; + + if (x >= seg->x_origin && x < (seg->x_origin + seg->x_size) && + y >= seg->y_origin && y < (seg->y_origin + seg->y_size)) + return true; + + return false; +} + +/** + * touch_overlay_mapped_touchscreen - check if an overlay touchscreen is mapped + * @map: pointer to the struct that holds the object mapping + * + * Returns true if an overlay touchscreen is mapped or false otherwise. + */ +bool touch_overlay_mapped_touchscreen(struct touch_overlay_map *map) +{ + if (!map || !map->overlay_touchscreen) + return false; + + return true; +} +EXPORT_SYMBOL(touch_overlay_mapped_touchscreen); + +/** + * touch_overlay_mapped_buttons - check if overlay buttons are mapped + * @map: pointer to the struct that holds the object mapping + * + * Returns true if overlay buttons mapped or false otherwise. + */ +bool touch_overlay_mapped_buttons(struct touch_overlay_map *map) +{ + if (!map || !map->button_count) + return false; + + return true; +} +EXPORT_SYMBOL(touch_overlay_mapped_buttons); + +static bool touch_overlay_mt_on_touchscreen(struct touch_overlay_map *map, + u32 *x, u32 *y) +{ + bool contact = x && y; + + if (!touch_overlay_mapped_touchscreen(map)) + return true; + + /* Let the caller handle events with no coordinates (release) */ + if (!contact) + return false; + + if (touch_overlay_segment_event(map->touchscreen, *x, *y)) { + *x -= map->touchscreen->x_origin; + *y -= map->touchscreen->y_origin; + return true; + } + + return false; +} + +static bool touch_overlay_button_event(struct input_dev *input, + struct touch_overlay_button *button, + const u32 *x, const u32 *y, u32 slot) +{ + bool contact = x && y; + + if (!contact && button->pressed && button->slot == slot) { + button->pressed = false; + input_report_key(input, button->key, false); + input_sync(input); + return true; + } else if (contact && touch_overlay_segment_event(&button->segment, + *x, *y)) { + button->pressed = true; + button->slot = slot; + input_report_key(input, button->key, true); + input_sync(input); + return true; + } + + return false; +} + +/** + * touch_overlay_process_event - process input events according to the overlay + * mapping. This function acts as a filter to release the calling driver from + * the events that are either related to overlay buttons or out of the overlay + * touchscreen area if defined. + * @map: pointer to the struct that holds the object mapping + * @input: pointer to the input device associated to the event + * @x: pointer to the x coordinate (NULL if not available - no contact) + * @y: pointer to the y coordinate (NULL if not available - no contact) + * @slot: slot associated to the event + * + * Returns true if the event was processed (reported for valid key events + * and dropped for events outside the overlay touchscreen area) or false + * if the event must be processed by the caller. In that case this function + * shifts the (x,y) coordinates to the overlay touchscreen axis if required + */ +bool touch_overlay_process_event(struct touch_overlay_map *map, + struct input_dev *input, + u32 *x, u32 *y, u32 slot) +{ + int i; + + if (!map) + return false; + + /* buttons must be prioritized over overlay touchscreens to account for + * overlappings e.g. a button inside the touchscreen area + */ + if (touch_overlay_mapped_buttons(map)) { + for (i = 0; i < map->button_count; i++) { + if (touch_overlay_button_event(input, &map->buttons[i], + x, y, slot)) + return true; + } + } + /* valid touch events on the overlay touchscreen are left for the client + * to be processed/reported according to its (possibly) unique features + */ + return !touch_overlay_mt_on_touchscreen(map, x, y); +} +EXPORT_SYMBOL(touch_overlay_process_event); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Helper functions for overlay objects on touch devices"); diff --git a/include/linux/input/touch-overlay.h b/include/linux/input/touch-overlay.h new file mode 100644 index 000000000000..3e0db813dc34 --- /dev/null +++ b/include/linux/input/touch-overlay.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2023 Javier Carrasco + */ + +#ifndef _TOUCH_OVERLAY +#define _TOUCH_OVERLAY + +#include + +struct input_dev; +struct device; + +struct touch_overlay_map { + struct touch_overlay_segment *touchscreen; + bool overlay_touchscreen; + struct touch_overlay_button *buttons; + u32 button_count; +}; + +struct touch_overlay_map *touch_overlay_map_overlay(struct input_dev *keypad); + +void touch_overlay_get_touchscreen_abs(struct touch_overlay_map *map, + u16 *x, u16 *y); + +bool touch_overlay_mapped_touchscreen(struct touch_overlay_map *map); + +bool touch_overlay_mapped_buttons(struct touch_overlay_map *map); + +bool touch_overlay_process_event(struct touch_overlay_map *map, + struct input_dev *input, + u32 *x, u32 *y, u32 slot); + +#endif