From patchwork Wed Jun 26 09:56:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Javier Carrasco X-Patchwork-Id: 808010 Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03on2134.outbound.protection.outlook.com [40.107.104.134]) (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 90CAF176247; Wed, 26 Jun 2024 09:56:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.104.134 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719395791; cv=fail; b=KU1ry2ReyDK8offjE59WLPiQsYarogy7TbGbkQ5NyA8h98Sro5pJjZ2ib+489zHZ2WatxCKaGT7hlzg9Zqykmi5BVY/UpXZq+rYEak8s1+JQQBiet2OAH17jQoLqy3xPO/QBazoJ38d2EOQQMl2RVPznLpTgaWd4R+UzF6OVNrI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719395791; c=relaxed/simple; bh=6B/C0oxZu4Olzt/Ju5SccabXqApih9npj2j2VCrFKU4=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=tnPWRtteUR+gl2gLyPqy3e4YPcTC67HT07WH2MXWEzkzysikwIEza52+y2bEbb00Nj8LGLwhG/kY0vNRh66BjXBlUNFvIv42xRPZGcT8TzKNe/aG1r14gkaFNkbmkp0Ox8XfA9lhOIIXnZKrDIgmOe8VdnPjVsMBbS00mrsOes0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wolfvision.net; spf=pass smtp.mailfrom=wolfvision.net; dkim=pass (1024-bit key) header.d=wolfvision.net header.i=@wolfvision.net header.b=q7AEez9D; arc=fail smtp.client-ip=40.107.104.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=wolfvision.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wolfvision.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=wolfvision.net header.i=@wolfvision.net header.b="q7AEez9D" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hDeGyQvAwih8MWSfa5JaOBzK2kiHIHUFOeZqBnIKtoxdLq2dnzN42GCV/a0te507Pb2Nb1IjDj+JHlOdavr0V7O98G3CxtPqwresk/vhtI9S0ky2Juw6zUmWqHBsy4uCnbRMlt2YUXetRONyOUbJdt+ZLVG3+wD4gUEzKw3CD5pj24283rx52jdUz+AKDsWmNQapiegkSnGf7mP+twyxPPNDtkYm9Bnzm07JpHReSwLTnpai0HSucwRFxEFnNxs2YWaWCPYdfo6/xxKBIQ0NzZpYzyVQpMNiiYuD2RF1GH3Ogg/DU059GxsfWO4w4zOxbCcEh+3PSwyIMBrV75z/QQ== 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=roqc8FkYwAZlQedOCVa4cYily+ZuhxMgO69P0lr/2eQ=; b=UPNz72BBSt2ejUJymk7Y72imditZgf4wE8y7amMSpdehCw4zPk12eL1SjXRm1bFmhnOZNhBkfla0kypS/auBDIYqj/zGS7wrSHNVbiknCrUMNyVFu39zVHrQ5fVM7RIoC5SfnwSezngf9APJwiLI0VvfnMAFUSvo6y1NfSTd3H6qR//SPLvn9CS5g9UzKMZyPr6OmLoixXKzn3CBluA2hxwMk9FvG9+d9whO98h9QMBfVvYcm5ERjoOydNPzan+B7DVkF/C4Kg8Eujgp7SDJL2r6LEjM1DxSrllj3727XsZkatFmIFjY/3raN6j/GryR8eq3hw4tunOswYOnGQjHWg== 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=roqc8FkYwAZlQedOCVa4cYily+ZuhxMgO69P0lr/2eQ=; b=q7AEez9D9+Jas7pYpsPS/94r/oblNuL+Rg9SJ6rW4VpcehZxU61KyD6PsMZaWzufJFlj9T7TSO3nniic5oW96iVlv7PxnRJRpipFx61RHxQ8D5f697GvrbjE9MpoJwNRJHuDBriart8ysOrtiMin+eGma3R/s/hZtdE3V85JDqA= 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 GV2PR08MB8679.eurprd08.prod.outlook.com (2603:10a6:150:b3::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.30; Wed, 26 Jun 2024 09:56:20 +0000 Received: from VE1PR08MB4974.eurprd08.prod.outlook.com ([fe80::a08:11be:8709:f5e1]) by VE1PR08MB4974.eurprd08.prod.outlook.com ([fe80::a08:11be:8709:f5e1%4]) with mapi id 15.20.7698.025; Wed, 26 Jun 2024 09:56:20 +0000 From: Javier Carrasco Date: Wed, 26 Jun 2024 11:56:14 +0200 Subject: [PATCH v10 2/4] Input: touch-overlay - Add touchscreen overlay handling Message-Id: <20240626-feature-ts_virtobj_patch-v10-2-873ad79bb2c9@wolfvision.net> References: <20240626-feature-ts_virtobj_patch-v10-0-873ad79bb2c9@wolfvision.net> In-Reply-To: <20240626-feature-ts_virtobj_patch-v10-0-873ad79bb2c9@wolfvision.net> To: Dmitry Torokhov , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Bastian Hecht , Michael Riesch Cc: linux-input@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Javier Carrasco , Jeff LaBundy X-Mailer: b4 0.14-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1719395779; l=10965; i=javier.carrasco@wolfvision.net; s=20240617; h=from:subject:message-id; bh=6B/C0oxZu4Olzt/Ju5SccabXqApih9npj2j2VCrFKU4=; b=1Xt6RN6QiGWGylIWBlZL+TFXHHgw0SKrZf/j0VGo6maqQcpjoAbQkmIg5/2RyqiC1ySeYqgpS Ut3LAYKCiuGDx/OllYJkirABOO/q6X6wtFOE1hnioKMrr9/bpM/3ECJ X-Developer-Key: i=javier.carrasco@wolfvision.net; a=ed25519; pk=hfASRUP6l4lf3Lo2mjLM085/h37dT3m0Qj1HejXDPDc= X-ClientProxiedBy: VI1PR09CA0118.eurprd09.prod.outlook.com (2603:10a6:803:78::41) To VE1PR08MB4974.eurprd08.prod.outlook.com (2603:10a6:803:111::15) Precedence: bulk X-Mailing-List: linux-input@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VE1PR08MB4974:EE_|GV2PR08MB8679:EE_ X-MS-Office365-Filtering-Correlation-Id: b232ca1f-7702-4b1a-9bc8-08dc95c63abe X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230038|366014|52116012|376012|1800799022|38350700012; X-Microsoft-Antispam-Message-Info: =?utf-8?q?NlB5iCSfUS637TwnMNQ+jwj1Ol3pU/N?= =?utf-8?q?64vs176kpk4v5iNmyzzuqyTfx3vfDgFD9MF4DNWFMWNQRcqWdtAqyP8gALqy+c4nJ?= =?utf-8?q?lFPxjCvoY28pJteODIDZVAUg2AEXCmf/5T7w0sJQkBBmP+7Gg6yOyCtdrTX/zxomk?= =?utf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qdI67lWWJXjDpQeGETUEoU1ED+wsuXR5xUtT64SEmnFz1EfB9i5b4ZobLSH+?= =?utf-8?q?ToFwxmNpdvls1t/kNxxdBouK7M+3iZsHaRBxHyQ1eVFr2DN7bMaDPSrEKhhpnF0lu?= =?utf-8?q?dN0JBZQYq8qjNuaRj2xQ6wfKTDRatQIUbpHreqIcaWmw/jL5ukRuOiNxEqlfhgDQg?= =?utf-8?q?arGTnoAG8LQ1FFKQG/cNvWxMFHHTAGIUVp3pTxMLkeRckAfO9qSB8b3UEWJAm5nka?= =?utf-8?q?u7CyYSBw0mGPuMVqsTesowvxH7k5JMnjhuDiOMXuhn/f9BGHSujvyq2Gb/8aifw4l?= =?utf-8?q?epUSLeI4HuqAneQzsyCMkqOj0Gwvo4ZslQ=3D=3D?= 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:(13230038)(366014)(52116012)(376012)(1800799022)(38350700012); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?PJqm4jPznvpBOqyl6L1LXgn6t4sH?= =?utf-8?q?EubyT7xQL43WRp/ZWebknhXcRiZHAU3rI4M3vNpqaDmFC8faVfGFWBvhKf+nwaqH3?= =?utf-8?q?32HNcRiL/SuP1Jb0uQCrr+rq6gJhufVgoWFLnznrLFyE9sdz3OfqJ0u3aoUFOlYuS?= =?utf-8?q?Drzkwg7fns3F+g67tYAz0miriFQB/U4OqN6ldf3Cus9/cxELj24GPfUOnhiUnLmUC?= =?utf-8?q?zvBQSG3d4NrLAOdMo5fUj3619LcHKwAyZpx5wCU8PpSGyOE3L136RGsk5ZJpIJYKL?= =?utf-8?q?FX2/hwALLY3sQmzkFz8SbJhb/W5rNsBfQApvihUJNgFKzLuxW43Shq3OV3dXPJKzM?= =?utf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-8?q?iPhwH3T6FKNZ1G+RhXndZ4bvpQaO8WcfvX2asVAGAlFuwo2XBEhJzOFhScRmkPd8X?= =?utf-8?q?X0O1S2EGRYqhFqIHwXnhHjRyL7Th8jzlb4f8xxvI/p5FrvV1eGCcOaQD64G4dtM7o?= =?utf-8?q?WySd8FFb2KJiSIBzXpWypu3nJyY04YdIgE15n13LV60UKYD2avNbXAss8OG7v8EJp?= =?utf-8?q?55kVddHlGNCHyrNDb+iMYrmHFbjOwlqcnJkhIG6nzQc3LLW7EVR58EFgbRgVj03xr?= =?utf-8?q?41SPYYZlQlqQz/ziiPPevEmRknz3D/m0h3q+K15zZ+zJT77Mgk91QZ25cw4pLnwq7?= =?utf-8?q?B3QPSgS+sjKMIdGveTbdtNFiQjfC9ysKBv/t2QeaPMFn97FgX6thIYQ=3D?= X-OriginatorOrg: wolfvision.net X-MS-Exchange-CrossTenant-Network-Message-Id: b232ca1f-7702-4b1a-9bc8-08dc95c63abe X-MS-Exchange-CrossTenant-AuthSource: VE1PR08MB4974.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2024 09:56:20.2488 (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: xTDt+aFBU11V36Lnsw55YASfxItcHltMBIPHhyo5g97C8ay3PIEYR8Of1a/vTS5cFoz2vcEj1E/auoWq9NyqbX0oj/hCyWKET4wGX2jpCcc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV2PR08MB8679 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. Note that the current implementation allows for multiple definitions of touchscreen areas (regions that report touch events), but only the first one will be used for the touchscreen device that the consumers typically provide. Should the need for multiple touchscreen areas arise, additional touchscreen devices would be required at the consumer side. There is no limitation in the number of touch areas defined as buttons. Reviewed-by: Jeff LaBundy Signed-off-by: Javier Carrasco --- MAINTAINERS | 7 + drivers/input/Makefile | 2 +- drivers/input/touch-overlay.c | 264 ++++++++++++++++++++++++++++++++++++ include/linux/input/touch-overlay.h | 22 +++ 4 files changed, 294 insertions(+), 1 deletion(-) diff --git a/MAINTAINERS b/MAINTAINERS index 2ca8f35dfe03..fbe2824d99c1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22739,6 +22739,13 @@ L: platform-driver-x86@vger.kernel.org S: Maintained F: drivers/platform/x86/toshiba-wmi.c +TOUCH OVERLAY +M: Javier Carrasco +L: linux-input@vger.kernel.org +S: Maintained +F: drivers/input/touch-overlay.c +F: include/linux/input/touch-overlay.h + TPM DEVICE DRIVER M: Peter Huewe M: Jarkko Sakkinen 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..5f839b206358 --- /dev/null +++ b/drivers/input/touch-overlay.c @@ -0,0 +1,264 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Helper functions for overlay objects on touchscreens + * + * Copyright (c) 2023 Javier Carrasco + */ + +#include +#include +#include +#include +#include +#include + +struct touch_overlay_segment { + struct list_head list; + u32 x_origin; + u32 y_origin; + u32 x_size; + u32 y_size; + u32 key; + bool pressed; + int slot; +}; + +static int touch_overlay_get_segment(struct fwnode_handle *segment_node, + struct touch_overlay_segment *segment, + struct input_dev *input) +{ + int error; + + error = fwnode_property_read_u32(segment_node, "x-origin", + &segment->x_origin); + if (error) + return error; + + error = fwnode_property_read_u32(segment_node, "y-origin", + &segment->y_origin); + if (error) + return error; + + error = fwnode_property_read_u32(segment_node, "x-size", + &segment->x_size); + if (error) + return error; + + error = fwnode_property_read_u32(segment_node, "y-size", + &segment->y_size); + if (error) + return error; + + error = fwnode_property_read_u32(segment_node, "linux,code", + &segment->key); + if (!error) + input_set_capability(input, EV_KEY, segment->key); + else if (error != -EINVAL) + return error; + + return 0; +} + +/** + * touch_overlay_map - map overlay objects from the device tree and set + * key capabilities if buttons are defined. + * @list: pointer to the list that will hold the segments + * @input: pointer to the already allocated input_dev + * + * Returns 0 on success and error number otherwise. + * + * If buttons are defined, key capabilities are set accordingly. + */ +int touch_overlay_map(struct list_head *list, struct input_dev *input) +{ + struct fwnode_handle *overlay, *fw_segment; + struct device *dev = input->dev.parent; + struct touch_overlay_segment *segment; + int error = 0; + + overlay = device_get_named_child_node(dev, "touch-overlay"); + if (!overlay) + return 0; + + fwnode_for_each_available_child_node(overlay, fw_segment) { + segment = devm_kzalloc(dev, sizeof(*segment), GFP_KERNEL); + if (!segment) { + fwnode_handle_put(fw_segment); + error = -ENOMEM; + break; + } + error = touch_overlay_get_segment(fw_segment, segment, input); + if (error) { + fwnode_handle_put(fw_segment); + break; + } + list_add_tail(&segment->list, list); + } + fwnode_handle_put(overlay); + + return error; +} +EXPORT_SYMBOL(touch_overlay_map); + +/** + * touch_overlay_get_touchscreen_abs - get abs size from the touchscreen area. + * @list: pointer to the list that holds the segments + * @x: horizontal abs + * @y: vertical abs + */ +void touch_overlay_get_touchscreen_abs(struct list_head *list, u16 *x, u16 *y) +{ + struct touch_overlay_segment *segment; + struct list_head *ptr; + + list_for_each(ptr, list) { + segment = list_entry(ptr, struct touch_overlay_segment, list); + if (!segment->key) { + *x = segment->x_size - 1; + *y = segment->y_size - 1; + break; + } + } +} +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 a touchscreen area is mapped + * @list: pointer to the list that holds the segments + * + * Returns true if a touchscreen area is mapped or false otherwise. + */ +bool touch_overlay_mapped_touchscreen(struct list_head *list) +{ + struct touch_overlay_segment *segment; + struct list_head *ptr; + + list_for_each(ptr, list) { + segment = list_entry(ptr, struct touch_overlay_segment, list); + if (!segment->key) + return true; + } + + return false; +} +EXPORT_SYMBOL(touch_overlay_mapped_touchscreen); + +static bool touch_overlay_event_on_ts(struct list_head *list, u32 *x, u32 *y) +{ + struct touch_overlay_segment *segment; + struct list_head *ptr; + bool valid_touch = true; + + if (!x || !y) + return false; + + list_for_each(ptr, list) { + segment = list_entry(ptr, struct touch_overlay_segment, list); + if (segment->key) + continue; + + if (touch_overlay_segment_event(segment, *x, *y)) { + *x -= segment->x_origin; + *y -= segment->y_origin; + return true; + } + /* ignore touch events outside the defined area */ + valid_touch = false; + } + + return valid_touch; +} + +static bool touch_overlay_button_event(struct input_dev *input, + struct touch_overlay_segment *segment, + const u32 *x, const u32 *y, u32 slot) +{ + bool contact = x && y; + + if (segment->slot == slot && segment->pressed) { + /* button release */ + if (!contact) { + segment->pressed = false; + input_report_key(input, segment->key, false); + input_sync(input); + return true; + } + + /* sliding out of the button releases it */ + if (!touch_overlay_segment_event(segment, *x, *y)) { + segment->pressed = false; + input_report_key(input, segment->key, false); + input_sync(input); + /* keep available for a possible touch event */ + return false; + } + /* ignore sliding on the button while pressed */ + return true; + } else if (contact && touch_overlay_segment_event(segment, *x, *y)) { + segment->pressed = true; + segment->slot = slot; + input_report_key(input, segment->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. + * @list: pointer to the list that holds the segments + * @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 list_head *list, + struct input_dev *input, + u32 *x, u32 *y, u32 slot) +{ + struct touch_overlay_segment *segment; + struct list_head *ptr; + + /* + * buttons must be prioritized over overlay touchscreens to account for + * overlappings e.g. a button inside the touchscreen area. + */ + list_for_each(ptr, list) { + segment = list_entry(ptr, struct touch_overlay_segment, list); + if (segment->key && + touch_overlay_button_event(input, segment, 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_event_on_ts(list, 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..cef05c46000d --- /dev/null +++ b/include/linux/input/touch-overlay.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (c) 2023 Javier Carrasco + */ + +#ifndef _TOUCH_OVERLAY +#define _TOUCH_OVERLAY + +#include + +struct input_dev; + +int touch_overlay_map(struct list_head *list, struct input_dev *input); + +void touch_overlay_get_touchscreen_abs(struct list_head *list, u16 *x, u16 *y); + +bool touch_overlay_mapped_touchscreen(struct list_head *list); + +bool touch_overlay_process_event(struct list_head *list, struct input_dev *input, + u32 *x, u32 *y, u32 slot); + +#endif