From patchwork Thu Aug 24 13:17:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 716670 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 13E37C3DA6F for ; Thu, 24 Aug 2023 13:18:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240674AbjHXNSD (ORCPT ); Thu, 24 Aug 2023 09:18:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240994AbjHXNRv (ORCPT ); Thu, 24 Aug 2023 09:17:51 -0400 Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on2051.outbound.protection.outlook.com [40.107.8.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A22181B0; Thu, 24 Aug 2023 06:17:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oP6y9qMCZLbduX0vQ/4UMTcBRQbFtfZiX4q6unRzBeehh5SbAEOk+4yyEjQRh7tWnmSF0cQkaZ7iNAlBEaEURTH4rU34UAq6OTi/emh1sUZZUHujP2k5UKW8DqQAU2+FSymaiCFSaXeXnxP3MKd0q+d2M/chsXfOdNCOweUI5h24bMuCVvjiicU/Tdlr0gaG0zXBXuCRpnnxKtuLNL+tfkVH7Wr+HZ2DHsApFuwV2Gx9hLZO1N9lq632HV7SeBzuVDwALqLeI1n2BmIu+N5YbnWazZdZX1QU0NmYN4zxLkgQEEoSN+2ukeeH2LNxnYqglhFGVWZAWiRABof57h5oEQ== 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=nqazVur3+oyuyZKRcL1W+LO0K7QxW9KJEE88Bn/Fh5o=; b=BRwvoSNTAA4aDkUTbQZZd2XoT9K/D+QfsdCjuhmAVYyCN4PZtsUOytRmWNHR/eokuXKNQtL7uyGW4a+DF+pLjwR6u8kgleeVq0mJ8sWRtBWmajrFcxsYi8w0rTC+koICcc/Ri+jub1i3w7xttfmPGEYDEdV6qGuXEoHz4yATNufoTIw/qn/V8St497Y90uYH9FIbeVg2IwK4ZOlQDvzoKsoY964uQI9gam7xC+XAeW38maM0qc15MdyP3yFhzBla7y1gMfVaAjSQW03a8aJQvUkYbfnVYIb3M9iatQ+o5GlUAnTbDNSR24q3zrR8usOZXCDQCFdABvYkk40QBaGQQQ== 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=nqazVur3+oyuyZKRcL1W+LO0K7QxW9KJEE88Bn/Fh5o=; b=dWQXa8Gi0yFfWtqy4OEejJJ6BifKoc95BCjAvsAUHI/5rEs4a3HVigCjzupn0KNushKBHHzonD1rdNfocM4pGZ+AgKCpGXgXWFlyelgX9ckVvk0lFD0SziCGVH5qDQVT8ZrgDY12+SVqUJKCMEvbgTmpLbaKiUG1oqQssRm8l/8= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=wolfvision.net; Received: from AM6PR08MB4966.eurprd08.prod.outlook.com (2603:10a6:20b:d4::23) by AM7PR08MB5397.eurprd08.prod.outlook.com (2603:10a6:20b:dd::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6699.24; Thu, 24 Aug 2023 13:17:45 +0000 Received: from AM6PR08MB4966.eurprd08.prod.outlook.com ([fe80::f7b3:4adb:c638:d8c7]) by AM6PR08MB4966.eurprd08.prod.outlook.com ([fe80::f7b3:4adb:c638:d8c7%6]) with mapi id 15.20.6699.027; Thu, 24 Aug 2023 13:17:45 +0000 From: Javier Carrasco Date: Thu, 24 Aug 2023 15:17:09 +0200 Subject: [PATCH v4 1/4] Input: touch-overlay - Add touchscreen overlay object handling Message-Id: <20230510-feature-ts_virtobj_patch-v4-1-5c6c0fc1eed6@wolfvision.net> References: <20230510-feature-ts_virtobj_patch-v4-0-5c6c0fc1eed6@wolfvision.net> In-Reply-To: <20230510-feature-ts_virtobj_patch-v4-0-5c6c0fc1eed6@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=1692883063; l=13899; i=javier.carrasco@wolfvision.net; s=20230509; h=from:subject:message-id; bh=JL3dP0O0DqFxMnYjj9dQs6fIBWPOaEgzUZcZU6ZZguM=; b=FPkTzo+0yM0Gt9uoR2bSQpyErmk33BNIRe1l4WAQxStttbUWWHyye76VJZwPPuS8iwJ0Sl2yH fEKTmkeaizTC2aP8toakb4JN4JEdRTxckfh0FoCW/EdTCl3Ve6M+6CD X-Developer-Key: i=javier.carrasco@wolfvision.net; a=ed25519; pk=tIGJV7M+tCizagNijF0eGMBGcOsPD+0cWGfKjl4h6K8= X-ClientProxiedBy: VI1PR07CA0301.eurprd07.prod.outlook.com (2603:10a6:800:130::29) To AM6PR08MB4966.eurprd08.prod.outlook.com (2603:10a6:20b:d4::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR08MB4966:EE_|AM7PR08MB5397:EE_ X-MS-Office365-Filtering-Correlation-Id: 67e8615b-a5d5-4de1-6b6f-08dba4a4810c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m5rW+dEK4vVmZnZP6LnXO+IOs3jcbntBs2HZb3ZkJ3n2zTVsWFzUmIit61e9hwFhkVphPvach5s7wnF0gmOgldFf4OXbvidEWU10fggy7YImahLzmnsvRHbcUpjkb8dzAjxhRL/5Mw5QFSLkX+uKMHurVwbK54xs+Yx5kD2eknMzU61HfQ6+umxPdnPBYKAbWzageEMPxpkHp4GQ9AH/DnMtWcPTLHRtjcEx41iaNTcamuoCU7jDCxJP6iGIVo+J3LaMTXMe5+zo70VuAW+RLqx2L2vVQ6CS2Ixc74pJHzvjdzmvaErx5YrxcfzhM/FDO9NpUWc2V6P61NemeLzxVaQPkFHs6O+Fq7Jz9+EFH1wXmgUeEB/874dR32bn0R2PVBYewCnn5XTrRHJwtLUPy6G7CTGb46pCpYm9vinhIRefstG8uMmiBN2Fg3/pQ6VryriHbmmeTycOpPkKYbOwkMDXN/xJ4XKUWse+1NXp+h9NAGatKWGbbVzqVgctFKKKevTF199cd+9Qk+6WfFW5crXokVcglBdKUrnvbq2aGP2IRSO7vZKsYDFG0lVE7c/D5hko3PoHrxzOQ5auTNfHf2UMyd/J79+K6dP3L22FqdQ= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM6PR08MB4966.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(346002)(39840400004)(366004)(376002)(136003)(396003)(451199024)(1800799009)(186009)(2906002)(30864003)(38350700002)(52116002)(6506007)(6486002)(5660300002)(83380400001)(44832011)(26005)(86362001)(38100700002)(8676002)(107886003)(8936002)(4326008)(2616005)(966005)(66946007)(6512007)(66556008)(316002)(66476007)(110136005)(6636002)(478600001)(6666004)(36756003)(41300700001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?OMRtYYBZkujhuUR6b4P/It4L7o0N?= =?utf-8?q?sbTaUdXoolRKBqSNNljdBW5Ls/NLF0WPjD7ctemV+jCx4uNf3G+2ZConJZCGCD0cF?= =?utf-8?q?zVWwhpv2tl/Z0PVngCB9Tbwc1PADrvs9lmxD52j6+KJgQ4y/WT3YTvCt0OPpyle1N?= =?utf-8?q?EjButBgXiXmu4kUUPJQgZ3frST05KRRl++ycVoR5jwu3dlYaLIrDSEV6ju6Qm6SQv?= =?utf-8?q?UzbuFm+0WbxXqV+kIRDSEp3ciX0ZKKMHh/LMs0WOU17iAebdx4hRoEa+Bmgt5Fb4M?= =?utf-8?q?44iIROfioCvbhRbiU30k4b7Bf9F7qUNHFjFa0aOH9QSrhlYj6HxUQO+44YiOT3ZzG?= =?utf-8?q?JfDurcMog6W+MmEeZfhnrtAGCkw8S8bOJlvBT4CsyB52aLV7TgNIDkvMwUb3fJR7w?= =?utf-8?q?6B30/1O+bC2XPGq4XjeEyowMLPbEsc+N7LDdfxXZJuvqNNjGTZtFXNIiyDXQChWqA?= =?utf-8?q?ZKtzV+/rcrYbW3EeRV7yrFYRyGmB+OD7NKiApwJ3mf9dfOJ1akYWcOC/5dtNZXLZt?= =?utf-8?q?PHbdp56ErlGG3gPAgjwPrkUnk4gbQHlati29zIzHHFFUU3MqpCq1t0lEU0uvsBniL?= =?utf-8?q?GD6MDFxw5WkMW8CwxTt0/YXw2hLUu6kCKLMdZ1I07f5dgnOiOdfqO+ukuKkU9CEgO?= =?utf-8?q?lOP7HRML6zB/5FYYLzzj+Cz1DobeMg8KP9oeZ1lF+37NeiyWAUm+Lyi1/uCr/7jQ+?= =?utf-8?q?PEs6zDG3Kbep6fLLrrHEsqO2hbt7D8NFXgjmipJQNWZhSZ8JA5uOXflywOkl4pk08?= =?utf-8?q?5n57jkwm+jWQjgYh3HCelJPB2QrrqMRjUCHrqh3dSBYcbXYZLRM3ZiANpn1dF2qta?= =?utf-8?q?chzi2pnI+dxWTVv9yNgmWS8oLYcA65YH6JASdOYbtHa83urr0He5f3Mr/K6XNGQf/?= =?utf-8?q?NKQdMsBkpzgEbXnGnAMdZZ8hgoK9m502mHv52BFREHTBpayihlrF9LD+JHXfVCJNR?= =?utf-8?q?oKM0l0zp8CrYcHsCpNYNii2bPVsjqslkGZa25Ajy4jyvQM+y2CDbhoeqB3MjVTB0/?= =?utf-8?q?dkZuTj03YcW6ck4oIz7mVryqtyH4PmgpcMiwZlPixNBGdg3fTKvzO6vcsveoofqXE?= =?utf-8?q?C0xGXUdh+ho0waTaIdhdwmmAIvjmf12MzjFdKNy4PZ+uNbm8+zGt0qyaug9sIQK01?= =?utf-8?q?JlZQdANivyUhsY9SYv9gCxBlKkz250NrGGbq3zXyUmuBKxBBaD5xFZv8SiXnP+oHA?= =?utf-8?q?5CgcmVKc1oXmUo8oQzZ+WFuoNpwI05N2smIe2dBomyUQI9PkvTiW8qmUk3nA2aJkE?= =?utf-8?q?g0jKjOl2CqZolnPw6sdZFWASn95byV3rNxajHc9X7Pi7DEfo0s3Rb3+WghDujZSXv?= =?utf-8?q?PCK8noLPhMm2MSWuP29lDWvB3tc3xZE9M+yGotjYV1pRq0LXN1XzocWfcqaPW4Onm?= =?utf-8?q?oYm1RQX2HfYx5et/atInaG1KugZy4moFGBhDtWpjIREjOCBQOIax5wCs/SyZFkJRP?= =?utf-8?q?EN/x3xinoUR9qfqvka4Ib59gSYlAN1oCRqUaUgU913v6mn6ZDDx5Mh/4Ny2H4Y7/j?= =?utf-8?q?0w8TGCMit4HwZfyrQs7x6LvhEuQ/NkKEQHME71Fy4HiRbMyWVftQHjA=3D?= X-OriginatorOrg: wolfvision.net X-MS-Exchange-CrossTenant-Network-Message-Id: 67e8615b-a5d5-4de1-6b6f-08dba4a4810c X-MS-Exchange-CrossTenant-AuthSource: AM6PR08MB4966.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Aug 2023 13:17:45.0925 (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: QGaR3FFGAeThvNBtq2M9K9ysYKcZtnQOGqk9jGAQ/xh+bNCdFpnnl00FyJ+fSt8ZhgtiFQZHD+IvYL4Ga0zHmryHbWQzYxVbI0OB4frNgII= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM7PR08MB5397 Precedence: bulk List-ID: X-Mailing-List: devicetree@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 3be1bdfe8ecc..dbc0d315d3d3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -21625,6 +21625,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 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