* [PATCH v10 0/4] Input: support overlay objects on touchscreens
@ 2024-06-26 9:56 Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 1/4] dt-bindings: touchscreen: add touch-overlay property Javier Carrasco
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Javier Carrasco @ 2024-06-26 9:56 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Bastian Hecht, Michael Riesch
Cc: linux-input, devicetree, linux-kernel, Javier Carrasco,
Jeff LaBundy
Some touchscreens are shipped with a physical layer on top of them where
a number of buttons and a resized touchscreen surface might be available.
In order to generate proper key events by overlay buttons and adjust the
touch events to a clipped surface, this series offers a documented,
device-tree-based solution by means of helper functions.
An implementation for a specific touchscreen driver is also included.
The functions in touch-overlay provide a simple workflow to acquire
physical objects from the device tree, map them into a list and generate
events according to the object descriptions.
This feature has been tested with a JT240MHQS-E3 display, which consists
of an st1624 as the base touchscreen and an overlay with two buttons and
a frame that clips its effective surface mounted on it.
To: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Rob Herring <robh@kernel.org>
To: Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>
To: Conor Dooley <conor+dt@kernel.org>
To: Bastian Hecht <hechtb@gmail.com>
To: Michael Riesch <michael.riesch@wolfvision.net>
Cc: linux-input@vger.kernel.org
Cc: devicetree@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Javier Carrasco <javier.carrasco@wolfvision.net>
Changes in v10:
- touch-overlay.c: use fwnode_for_each_available_child_node() macro as
there is no point in adding unavailable objects.
- touch-overlay.c: initialize error in touch_overlay_map(), which would
be returned uninitialized if there were no touch overlay segments.
- touch-overlay.c: fix fwnode_handle refcount (overlay must always be
decremented before returning, fw_segment in error paths of the loop).
- Patch 4/4: update description (the feature does not add a second input
device anymore).
- Link to v9: https://lore.kernel.org/r/20240422-feature-ts_virtobj_patch-v9-0-acf118d12a8a@wolfvision.net
Changes in v9:
- touch-overlay.c: trigger a button release if the finger slides out of
the button segment to be consistent with the button press when sliding
into a button segment (see touch_overlay_button_event()).
- touch-overlay.c: (nit) remove braces in if with a single statement in
touch_overaly_process_event().
- Link to v8: https://lore.kernel.org/r/20240320-feature-ts_virtobj_patch-v8-0-cab6e7dcb1f6@wolfvision.net
Changes in v8:
- touchscreen bindings: fix description formatting.
- Link to v7: https://lore.kernel.org/r/20240119-feature-ts_virtobj_patch-v7-0-eda70985808f@wolfvision.net
Changes in v7:
- General: return to a single input device implementation.
- touchscreen bindings: segment instead of button in the label
description.
- touch-overlay.c: define button-specific data inside segment struct.
- touch-overlay.c: remove fwnode_property_present() and check return
value of fwnode_property_read_u32() in touch_overlay_get_segment().
- touch-overlay.c: simplify return path in touch_overlay_map().
- Link to v6: https://lore.kernel.org/r/20230510-feature-ts_virtobj_patch-v6-0-d8a605975153@wolfvision.net
Changes in v6:
- General: use a single list to manage a single type of object.
- General: swap patches to have bindings preceding the code.
- touch-overlay.c: minor code-sytle fixes.
- Link to v5: https://lore.kernel.org/r/20230510-feature-ts_virtobj_patch-v5-0-ff6b5c4db693@wolfvision.net
Changes in v5:
- touchscreen bindings: move overlay common properties to a $def entry (Rob Herring)
- st1232 bindings: move overlays to the existing example instead of
making a new one (Rob Herring)
- Link to v4: https://lore.kernel.org/r/20230510-feature-ts_virtobj_patch-v4-0-5c6c0fc1eed6@wolfvision.net
Changes in v4:
- General: rename "touchscreen" to "touch" to include other consumers.
- PATCH 1/4: move touch-overlay feature to input core.
- PATCH 1/4, 3/4: set key caps and report key events without consumer's
intervention.
- PATCH 2/4: add missing 'required' field with the required properties.
- Link to v3: https://lore.kernel.org/r/20230510-feature-ts_virtobj_patch-v3-0-b4fb7fc4bab7@wolfvision.net
Changes in v3:
- General: rename "virtobj" and "virtual" to "overlay"
- PATCH 1/4: Make feature bool instead of tristate (selected by
supported touchscreens)
- Link to v2: https://lore.kernel.org/r/20230510-feature-ts_virtobj_patch-v2-0-f68a6bfe7a0f@wolfvision.net
Changes in v2:
- PATCH 1/4: remove preprocessor directives (the module is selected by
the drivers that support the feature). Typo in the commit message.
- PATCH 2/4: more detailed documentation. Images and examples were added.
- PATCH 3/4: select ts-virtobj automatically.
- Link to v1: https://lore.kernel.org/r/20230510-feature-ts_virtobj_patch-v1-0-5ae5e81bc264@wolfvision.net
---
Javier Carrasco (4):
dt-bindings: touchscreen: add touch-overlay property
Input: touch-overlay - Add touchscreen overlay handling
dt-bindings: input: touchscreen: st1232: add touch-overlay example
Input: st1232 - add touch overlays handling
.../input/touchscreen/sitronix,st1232.yaml | 29 +++
.../bindings/input/touchscreen/touchscreen.yaml | 119 ++++++++++
MAINTAINERS | 7 +
drivers/input/Makefile | 2 +-
drivers/input/touch-overlay.c | 264 +++++++++++++++++++++
drivers/input/touchscreen/st1232.c | 48 ++--
include/linux/input/touch-overlay.h | 22 ++
7 files changed, 476 insertions(+), 15 deletions(-)
---
base-commit: f2661062f16b2de5d7b6a5c42a9a5c96326b8454
change-id: 20230510-feature-ts_virtobj_patch-e267540aae74
Best regards,
--
Javier Carrasco <javier.carrasco@wolfvision.net>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v10 1/4] dt-bindings: touchscreen: add touch-overlay property
2024-06-26 9:56 [PATCH v10 0/4] Input: support overlay objects on touchscreens Javier Carrasco
@ 2024-06-26 9:56 ` Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 2/4] Input: touch-overlay - Add touchscreen overlay handling Javier Carrasco
` (2 subsequent siblings)
3 siblings, 0 replies; 9+ messages in thread
From: Javier Carrasco @ 2024-06-26 9:56 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Bastian Hecht, Michael Riesch
Cc: linux-input, devicetree, linux-kernel, Javier Carrasco,
Jeff LaBundy
The touch-overlay encompasses a number of touch areas that define a
clipped touchscreen area and/or buttons with a specific functionality.
A clipped touchscreen area avoids getting events from regions that are
physically hidden by overlay frames.
For touchscreens with printed overlay buttons, sub-nodes with a suitable
key code can be defined to report key events instead of the original
touch events.
Reviewed-by: Jeff LaBundy <jeff@labundy.com>
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Javier Carrasco <javier.carrasco@wolfvision.net>
---
.../bindings/input/touchscreen/touchscreen.yaml | 119 +++++++++++++++++++++
1 file changed, 119 insertions(+)
diff --git a/Documentation/devicetree/bindings/input/touchscreen/touchscreen.yaml b/Documentation/devicetree/bindings/input/touchscreen/touchscreen.yaml
index 431c13335c40..3e3572aa483a 100644
--- a/Documentation/devicetree/bindings/input/touchscreen/touchscreen.yaml
+++ b/Documentation/devicetree/bindings/input/touchscreen/touchscreen.yaml
@@ -87,6 +87,125 @@ properties:
touchscreen-y-plate-ohms:
description: Resistance of the Y-plate in Ohms
+ touch-overlay:
+ description: |
+ List of nodes defining segments (touch areas) on the touchscreen.
+
+ This object can be used to describe a series of segments to restrict
+ the region within touch events are reported or buttons with a specific
+ functionality.
+
+ This is of special interest if the touchscreen is shipped with a physical
+ overlay on top of it with a frame that hides some part of the original
+ touchscreen area. Printed buttons on that overlay are also a typical
+ use case.
+
+ A new touchscreen area is defined as a sub-node without a key code. If a
+ key code is defined in the sub-node, it will be interpreted as a button.
+
+ The x-origin and y-origin properties of a touchscreen area define the
+ offset of a new origin from where the touchscreen events are referenced.
+ This offset is applied to the events accordingly. The x-size and y-size
+ properties define the size of the touchscreen effective area.
+
+ The following example shows a new touchscreen area with the new origin
+ (0',0') for the touch events generated by the device.
+
+ Touchscreen (full area)
+ ┌────────────────────────────────────────┐
+ │ ┌───────────────────────────────┐ │
+ │ │ │ │
+ │ ├ y-size │ │
+ │ │ │ │
+ │ │ touchscreen area │ │
+ │ │ (no key code) │ │
+ │ │ │ │
+ │ │ x-size │ │
+ │ ┌└──────────────┴────────────────┘ │
+ │(0',0') │
+ ┌└────────────────────────────────────────┘
+ (0,0)
+
+ where (0',0') = (0+x-origin,0+y-origin)
+
+ Sub-nodes with key codes report the touch events on their surface as key
+ events instead.
+
+ The following example shows a touchscreen with a single button on it.
+
+ Touchscreen (full area)
+ ┌───────────────────────────────────┐
+ │ │
+ │ │
+ │ ┌─────────┐ │
+ │ │button 0 │ │
+ │ │KEY_POWER│ │
+ │ └─────────┘ │
+ │ │
+ │ │
+ ┌└───────────────────────────────────┘
+ (0,0)
+
+ Segments defining buttons and clipped toushcreen areas can be combined
+ as shown in the following example.
+ In that case only the events within the touchscreen area are reported
+ as touch events. Events within the button areas report their associated
+ key code. Any events outside the defined areas are ignored.
+
+ Touchscreen (full area)
+ ┌─────────┬──────────────────────────────┐
+ │ │ │
+ │ │ ┌───────────────────────┐ │
+ │ button 0│ │ │ │
+ │KEY_POWER│ │ │ │
+ │ │ │ │ │
+ ├─────────┤ │ touchscreen area │ │
+ │ │ │ (no key code) │ │
+ │ │ │ │ │
+ │ button 1│ │ │ │
+ │ KEY_INFO│ ┌└───────────────────────┘ │
+ │ │(0',0') │
+ ┌└─────────┴──────────────────────────────┘
+ (0,0)
+
+ type: object
+
+ patternProperties:
+ '^segment-':
+ type: object
+ description:
+ Each segment is represented as a sub-node.
+ properties:
+ x-origin:
+ description: horizontal origin of the node area
+ $ref: /schemas/types.yaml#/definitions/uint32
+
+ y-origin:
+ description: vertical origin of the node area
+ $ref: /schemas/types.yaml#/definitions/uint32
+
+ x-size:
+ description: horizontal resolution of the node area
+ $ref: /schemas/types.yaml#/definitions/uint32
+
+ y-size:
+ description: vertical resolution of the node area
+ $ref: /schemas/types.yaml#/definitions/uint32
+
+ label:
+ description: descriptive name of the segment
+ $ref: /schemas/types.yaml#/definitions/string
+
+ linux,code: true
+
+ required:
+ - x-origin
+ - y-origin
+ - x-size
+ - y-size
+
+ unevaluatedProperties: false
+
dependencies:
touchscreen-size-x: [ touchscreen-size-y ]
touchscreen-size-y: [ touchscreen-size-x ]
--
2.40.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v10 2/4] Input: touch-overlay - Add touchscreen overlay handling
2024-06-26 9:56 [PATCH v10 0/4] Input: support overlay objects on touchscreens Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 1/4] dt-bindings: touchscreen: add touch-overlay property Javier Carrasco
@ 2024-06-26 9:56 ` Javier Carrasco
2024-07-09 0:08 ` Dmitry Torokhov
2024-06-26 9:56 ` [PATCH v10 3/4] dt-bindings: input: touchscreen: st1232: add touch-overlay example Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 4/4] Input: st1232 - add touch overlays handling Javier Carrasco
3 siblings, 1 reply; 9+ messages in thread
From: Javier Carrasco @ 2024-06-26 9:56 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Bastian Hecht, Michael Riesch
Cc: linux-input, devicetree, linux-kernel, Javier Carrasco,
Jeff LaBundy
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 <jeff@labundy.com>
Signed-off-by: Javier Carrasco <javier.carrasco@wolfvision.net>
---
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 <javier.carrasco@wolfvision.net>
+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 <peterhuewe@gmx.de>
M: Jarkko Sakkinen <jarkko@kernel.org>
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 <javier.carrasco@wolfvision.net>
+ */
+
+#include <linux/input.h>
+#include <linux/input/mt.h>
+#include <linux/input/touch-overlay.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/property.h>
+
+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 <javier.carrasco@wolfvision.net>
+ */
+
+#ifndef _TOUCH_OVERLAY
+#define _TOUCH_OVERLAY
+
+#include <linux/types.h>
+
+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
--
2.40.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v10 3/4] dt-bindings: input: touchscreen: st1232: add touch-overlay example
2024-06-26 9:56 [PATCH v10 0/4] Input: support overlay objects on touchscreens Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 1/4] dt-bindings: touchscreen: add touch-overlay property Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 2/4] Input: touch-overlay - Add touchscreen overlay handling Javier Carrasco
@ 2024-06-26 9:56 ` Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 4/4] Input: st1232 - add touch overlays handling Javier Carrasco
3 siblings, 0 replies; 9+ messages in thread
From: Javier Carrasco @ 2024-06-26 9:56 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Bastian Hecht, Michael Riesch
Cc: linux-input, devicetree, linux-kernel, Javier Carrasco
The touch-overlay feature adds support for segments (touch areas) on the
touchscreen surface that represent overlays with clipped touchscreen
areas and printed buttons.
Add nodes for a clipped touchscreen and overlay buttons to the existing
example.
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Javier Carrasco <javier.carrasco@wolfvision.net>
---
.../input/touchscreen/sitronix,st1232.yaml | 29 ++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/Documentation/devicetree/bindings/input/touchscreen/sitronix,st1232.yaml b/Documentation/devicetree/bindings/input/touchscreen/sitronix,st1232.yaml
index 1d8ca19fd37a..e7ee7a0d74c4 100644
--- a/Documentation/devicetree/bindings/input/touchscreen/sitronix,st1232.yaml
+++ b/Documentation/devicetree/bindings/input/touchscreen/sitronix,st1232.yaml
@@ -37,6 +37,7 @@ unevaluatedProperties: false
examples:
- |
+ #include <dt-bindings/input/linux-event-codes.h>
i2c {
#address-cells = <1>;
#size-cells = <0>;
@@ -46,5 +47,33 @@ examples:
reg = <0x55>;
interrupts = <2 0>;
gpios = <&gpio1 166 0>;
+
+ touch-overlay {
+ segment-0 {
+ label = "Touchscreen";
+ x-origin = <0>;
+ x-size = <240>;
+ y-origin = <40>;
+ y-size = <280>;
+ };
+
+ segment-1a {
+ label = "Camera light";
+ linux,code = <KEY_LIGHTS_TOGGLE>;
+ x-origin = <40>;
+ x-size = <40>;
+ y-origin = <0>;
+ y-size = <40>;
+ };
+
+ segment-2a {
+ label = "Power";
+ linux,code = <KEY_POWER>;
+ x-origin = <160>;
+ x-size = <40>;
+ y-origin = <0>;
+ y-size = <40>;
+ };
+ };
};
};
--
2.40.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v10 4/4] Input: st1232 - add touch overlays handling
2024-06-26 9:56 [PATCH v10 0/4] Input: support overlay objects on touchscreens Javier Carrasco
` (2 preceding siblings ...)
2024-06-26 9:56 ` [PATCH v10 3/4] dt-bindings: input: touchscreen: st1232: add touch-overlay example Javier Carrasco
@ 2024-06-26 9:56 ` Javier Carrasco
2024-07-09 0:44 ` Dmitry Torokhov
3 siblings, 1 reply; 9+ messages in thread
From: Javier Carrasco @ 2024-06-26 9:56 UTC (permalink / raw)
To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
Bastian Hecht, Michael Riesch
Cc: linux-input, devicetree, linux-kernel, Javier Carrasco,
Jeff LaBundy
Use touch-overlay to support overlay objects such as buttons and a
resized frame defined in the device tree.
A key event will be generated if the coordinates of a touch event are
within the area defined by the button properties.
Reviewed-by: Jeff LaBundy <jeff@labundy.com>
Signed-off-by: Javier Carrasco <javier.carrasco@wolfvision.net>
---
drivers/input/touchscreen/st1232.c | 48 +++++++++++++++++++++++++++-----------
1 file changed, 34 insertions(+), 14 deletions(-)
diff --git a/drivers/input/touchscreen/st1232.c b/drivers/input/touchscreen/st1232.c
index 6475084aee1b..4fa31447dbc1 100644
--- a/drivers/input/touchscreen/st1232.c
+++ b/drivers/input/touchscreen/st1232.c
@@ -22,6 +22,7 @@
#include <linux/pm_qos.h>
#include <linux/slab.h>
#include <linux/types.h>
+#include <linux/input/touch-overlay.h>
#define ST1232_TS_NAME "st1232-ts"
#define ST1633_TS_NAME "st1633-ts"
@@ -57,6 +58,7 @@ struct st1232_ts_data {
struct dev_pm_qos_request low_latency_req;
struct gpio_desc *reset_gpio;
const struct st_chip_info *chip_info;
+ struct list_head touch_overlay_list;
int read_buf_len;
u8 *read_buf;
};
@@ -138,14 +140,20 @@ static int st1232_ts_parse_and_report(struct st1232_ts_data *ts)
for (i = 0; i < ts->chip_info->max_fingers; i++) {
u8 *buf = &ts->read_buf[i * 4];
+ bool contact = buf[0] & BIT(7);
+ unsigned int x, y;
- if (buf[0] & BIT(7)) {
- unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
- unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
-
- touchscreen_set_mt_pos(&pos[n_contacts],
- &ts->prop, x, y);
+ if (contact) {
+ x = ((buf[0] & 0x70) << 4) | buf[1];
+ y = ((buf[0] & 0x07) << 8) | buf[2];
+ }
+ if (touch_overlay_process_event(&ts->touch_overlay_list, input,
+ contact ? &x : NULL,
+ contact ? &y : NULL, i))
+ continue;
+ if (contact) {
+ touchscreen_set_mt_pos(&pos[n_contacts], &ts->prop, x, y);
/* st1232 includes a z-axis / touch strength */
if (ts->chip_info->have_z)
z[n_contacts] = ts->read_buf[i + 6];
@@ -292,18 +300,30 @@ static int st1232_ts_probe(struct i2c_client *client)
if (error)
return error;
- /* Read resolution from the chip */
- error = st1232_ts_read_resolution(ts, &max_x, &max_y);
- if (error) {
- dev_err(&client->dev,
- "Failed to read resolution: %d\n", error);
- return error;
- }
-
if (ts->chip_info->have_z)
input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
ts->chip_info->max_area, 0, 0);
+ /* map overlay objects if defined in the device tree */
+ INIT_LIST_HEAD(&ts->touch_overlay_list);
+ error = touch_overlay_map(&ts->touch_overlay_list, input_dev);
+ if (error)
+ return error;
+
+ if (touch_overlay_mapped_touchscreen(&ts->touch_overlay_list)) {
+ /* Read resolution from the overlay touchscreen if defined */
+ touch_overlay_get_touchscreen_abs(&ts->touch_overlay_list,
+ &max_x, &max_y);
+ } else {
+ /* Read resolution from the chip */
+ error = st1232_ts_read_resolution(ts, &max_x, &max_y);
+ if (error) {
+ dev_err(&client->dev,
+ "Failed to read resolution: %d\n", error);
+ return error;
+ }
+ }
+
input_set_abs_params(input_dev, ABS_MT_POSITION_X,
0, max_x, 0, 0);
input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
--
2.40.1
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v10 2/4] Input: touch-overlay - Add touchscreen overlay handling
2024-06-26 9:56 ` [PATCH v10 2/4] Input: touch-overlay - Add touchscreen overlay handling Javier Carrasco
@ 2024-07-09 0:08 ` Dmitry Torokhov
2024-07-10 12:16 ` Javier Carrasco
0 siblings, 1 reply; 9+ messages in thread
From: Dmitry Torokhov @ 2024-07-09 0:08 UTC (permalink / raw)
To: Javier Carrasco
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bastian Hecht,
Michael Riesch, linux-input, devicetree, linux-kernel,
Jeff LaBundy
Hi Javier,
On Wed, Jun 26, 2024 at 11:56:14AM +0200, Javier Carrasco wrote:
> Some touch devices provide mechanical overlays with different objects
> like buttons or clipped touchscreen surfaces.
Thank you for your work. I think it is pretty much ready to be merged,
just a few small comments:
>
> 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.
So if we have overlays and also want to invert/swap axis then the
overlays should be processed first and only then
touchscreen_set_mt_pos() or touchscreen_report_pos() should be called?
But then it will not work if we need help frm the input core to assign
slots in cases when touch controller does not implement [reliable]
contact tracing/identification.
I think this all needs to be clarified.
>
> 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 <jeff@labundy.com>
> Signed-off-by: Javier Carrasco <javier.carrasco@wolfvision.net>
> +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");
We can annotate this as
struct fwnode_handle *overlay __free(fwnode_handle) =
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;
return -ENOMEM;
> + break;
> + }
> + error = touch_overlay_get_segment(fw_segment, segment, input);
> + if (error) {
> + fwnode_handle_put(fw_segment);
return error;
> + break;
> + }
> + list_add_tail(&segment->list, list);
> + }
> + fwnode_handle_put(overlay);
Drop.
> +
> + return error;
return 0;
> +}
> +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;
This is a static function in the module, we are not passing NULL
segments to it ever. Such tests should be done on API boundary.
> +
> + 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);
Do we really need to emit sync here? Can we require/rely on the driver
calling us to emit input_sync() once it's done processing current
frame/packet?
> + 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)
Would it be better to have a separate argument communicating slot state
(contact/no contact)?
> + * @slot: slot associated to the event
What if we are not dealing with an MT device? Can we say that they
should use slot 0 or maybe -1?
> + *
> + * 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 <javier.carrasco@wolfvision.net>
> + */
> +
> +#ifndef _TOUCH_OVERLAY
> +#define _TOUCH_OVERLAY
> +
> +#include <linux/types.h>
> +
> +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
>
> --
> 2.40.1
>
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v10 4/4] Input: st1232 - add touch overlays handling
2024-06-26 9:56 ` [PATCH v10 4/4] Input: st1232 - add touch overlays handling Javier Carrasco
@ 2024-07-09 0:44 ` Dmitry Torokhov
0 siblings, 0 replies; 9+ messages in thread
From: Dmitry Torokhov @ 2024-07-09 0:44 UTC (permalink / raw)
To: Javier Carrasco
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bastian Hecht,
Michael Riesch, linux-input, devicetree, linux-kernel,
Jeff LaBundy
Hi Javier,
On Wed, Jun 26, 2024 at 11:56:16AM +0200, Javier Carrasco wrote:
> Use touch-overlay to support overlay objects such as buttons and a
> resized frame defined in the device tree.
>
> A key event will be generated if the coordinates of a touch event are
> within the area defined by the button properties.
>
> Reviewed-by: Jeff LaBundy <jeff@labundy.com>
> Signed-off-by: Javier Carrasco <javier.carrasco@wolfvision.net>
> ---
> drivers/input/touchscreen/st1232.c | 48 +++++++++++++++++++++++++++-----------
> 1 file changed, 34 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/input/touchscreen/st1232.c b/drivers/input/touchscreen/st1232.c
> index 6475084aee1b..4fa31447dbc1 100644
> --- a/drivers/input/touchscreen/st1232.c
> +++ b/drivers/input/touchscreen/st1232.c
> @@ -22,6 +22,7 @@
> #include <linux/pm_qos.h>
> #include <linux/slab.h>
> #include <linux/types.h>
> +#include <linux/input/touch-overlay.h>
>
> #define ST1232_TS_NAME "st1232-ts"
> #define ST1633_TS_NAME "st1633-ts"
> @@ -57,6 +58,7 @@ struct st1232_ts_data {
> struct dev_pm_qos_request low_latency_req;
> struct gpio_desc *reset_gpio;
> const struct st_chip_info *chip_info;
> + struct list_head touch_overlay_list;
> int read_buf_len;
> u8 *read_buf;
> };
> @@ -138,14 +140,20 @@ static int st1232_ts_parse_and_report(struct st1232_ts_data *ts)
>
> for (i = 0; i < ts->chip_info->max_fingers; i++) {
> u8 *buf = &ts->read_buf[i * 4];
> + bool contact = buf[0] & BIT(7);
> + unsigned int x, y;
>
> - if (buf[0] & BIT(7)) {
> - unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
> - unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
> -
> - touchscreen_set_mt_pos(&pos[n_contacts],
> - &ts->prop, x, y);
> + if (contact) {
> + x = ((buf[0] & 0x70) << 4) | buf[1];
> + y = ((buf[0] & 0x07) << 8) | buf[2];
> + }
> + if (touch_overlay_process_event(&ts->touch_overlay_list, input,
> + contact ? &x : NULL,
> + contact ? &y : NULL, i))
> + continue;
So here is the exact issue I was talking about: you are using index of
the contact as a slot, which assumes that the device maintains position
of contacts in the overall event stream. However the driver uses input
core to track and assign slots (see calls to touchscreen_set_mt_pos()
and input_mt_assign_slots() below), which suggest that the touch
controller may reorder positions as needed.
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v10 2/4] Input: touch-overlay - Add touchscreen overlay handling
2024-07-09 0:08 ` Dmitry Torokhov
@ 2024-07-10 12:16 ` Javier Carrasco
2024-07-11 17:18 ` Dmitry Torokhov
0 siblings, 1 reply; 9+ messages in thread
From: Javier Carrasco @ 2024-07-10 12:16 UTC (permalink / raw)
To: Dmitry Torokhov
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bastian Hecht,
Michael Riesch, linux-input, devicetree, linux-kernel,
Jeff LaBundy
On 09/07/2024 02:08, Dmitry Torokhov wrote:
> Hi Javier,
>
> On Wed, Jun 26, 2024 at 11:56:14AM +0200, Javier Carrasco wrote:
>> Some touch devices provide mechanical overlays with different objects
>> like buttons or clipped touchscreen surfaces.
>
> Thank you for your work. I think it is pretty much ready to be merged,
> just a few small comments:
>
>>
>> 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.
>
> So if we have overlays and also want to invert/swap axis then the
> overlays should be processed first and only then
> touchscreen_set_mt_pos() or touchscreen_report_pos() should be called?
>
> But then it will not work if we need help frm the input core to assign
> slots in cases when touch controller does not implement [reliable]
> contact tracing/identification.
>
> I think this all needs to be clarified.
>
I think this is the most critical point from your feedback.
So far, touch-overlay processes the coordinates of input_mt_pos elements
before passing them to touchscreen_set_mt_pos(). As you pointed out,
that means that it does not act on the slots i.e. it assumes that the
controller does the contact tracing and pos[i] is assigned to slot[i],
which is wrong.
Current status:
[Sensor]->[touch-overlay(filter + button
events)]->[touchscreen_set_mt_pos()]->[input_mt_assign_slots()]->[report
MT events]
If I am not mistaken, I would need a solution that processes the
coordinates associated to assigned slots via input_mt_assign_slots().
Then I would have to avoid generating MT events from slots whose
coordinates are outside of the overlay frame (ignored) or on overlay
buttons (generating button press/release events instead).
But once input_mt_assign_slots() is called, it is already too late for
that processing, isn't it? Unless assigned slots filtered by
touch-overlay are kept from generating MT events somehow, but still used
to keep contact tracing.
Any suggestion on this respect would be more than welcome.
>>
>> 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 <jeff@labundy.com>
>> Signed-off-by: Javier Carrasco <javier.carrasco@wolfvision.net>
>
>> +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");
>
> We can annotate this as
>
> struct fwnode_handle *overlay __free(fwnode_handle) =
> device_get_named_child_node(dev, "touch-overlay");
>
That's right. A scoped version of the loop would have been nice too, but
there is still no such variant for this particular one. I am pushing
that somewhere else, though.
>> + 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;
>
> return -ENOMEM;
>
>> + break;
>> + }
>> + error = touch_overlay_get_segment(fw_segment, segment, input);
>> + if (error) {
>> + fwnode_handle_put(fw_segment);
>
> return error;
>
>> + break;
>> + }
>> + list_add_tail(&segment->list, list);
>> + }
>> + fwnode_handle_put(overlay);
>
> Drop.
>
>> +
>> + return error;
>
> return 0;
>
Ack.
>> +}
>> +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;
>
> This is a static function in the module, we are not passing NULL
> segments to it ever. Such tests should be done on API boundary.
>
Ack.
>> +
>> + 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);
>
> Do we really need to emit sync here? Can we require/rely on the driver
> calling us to emit input_sync() once it's done processing current
> frame/packet?
>
I will test it without it, but that might change anyway depending on the
outcome of your first comment.
>> + 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)
>
> Would it be better to have a separate argument communicating slot state
> (contact/no contact)?
>
Passing NULL would be ok to save one extra argument, but I have no
strong feelings about it.
>> + * @slot: slot associated to the event
>
> What if we are not dealing with an MT device? Can we say that they
> should use slot 0 or maybe -1?
>
slot 0 would be ok. I will document it.
>> + *
>> + * 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 <javier.carrasco@wolfvision.net>
>> + */
>> +
>> +#ifndef _TOUCH_OVERLAY
>> +#define _TOUCH_OVERLAY
>> +
>> +#include <linux/types.h>
>> +
>> +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
>>
>> --
>> 2.40.1
>>
>
> Thanks.
>
Thanks a lot for your feedback.
Best regards,
Javier Carrasco
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v10 2/4] Input: touch-overlay - Add touchscreen overlay handling
2024-07-10 12:16 ` Javier Carrasco
@ 2024-07-11 17:18 ` Dmitry Torokhov
0 siblings, 0 replies; 9+ messages in thread
From: Dmitry Torokhov @ 2024-07-11 17:18 UTC (permalink / raw)
To: Javier Carrasco
Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Bastian Hecht,
Michael Riesch, linux-input, devicetree, linux-kernel,
Jeff LaBundy
On Wed, Jul 10, 2024 at 02:16:13PM +0200, Javier Carrasco wrote:
> On 09/07/2024 02:08, Dmitry Torokhov wrote:
> > Hi Javier,
> >
> > On Wed, Jun 26, 2024 at 11:56:14AM +0200, Javier Carrasco wrote:
> >> Some touch devices provide mechanical overlays with different objects
> >> like buttons or clipped touchscreen surfaces.
> >
> > Thank you for your work. I think it is pretty much ready to be merged,
> > just a few small comments:
> >
> >>
> >> 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.
> >
> > So if we have overlays and also want to invert/swap axis then the
> > overlays should be processed first and only then
> > touchscreen_set_mt_pos() or touchscreen_report_pos() should be called?
> >
> > But then it will not work if we need help frm the input core to assign
> > slots in cases when touch controller does not implement [reliable]
> > contact tracing/identification.
> >
> > I think this all needs to be clarified.
> >
>
> I think this is the most critical point from your feedback.
>
> So far, touch-overlay processes the coordinates of input_mt_pos elements
> before passing them to touchscreen_set_mt_pos(). As you pointed out,
> that means that it does not act on the slots i.e. it assumes that the
> controller does the contact tracing and pos[i] is assigned to slot[i],
> which is wrong.
>
> Current status:
> [Sensor]->[touch-overlay(filter + button
> events)]->[touchscreen_set_mt_pos()]->[input_mt_assign_slots()]->[report
> MT events]
>
> If I am not mistaken, I would need a solution that processes the
> coordinates associated to assigned slots via input_mt_assign_slots().
> Then I would have to avoid generating MT events from slots whose
> coordinates are outside of the overlay frame (ignored) or on overlay
> buttons (generating button press/release events instead).
>
> But once input_mt_assign_slots() is called, it is already too late for
> that processing, isn't it? Unless assigned slots filtered by
> touch-overlay are kept from generating MT events somehow, but still used
> to keep contact tracing.
>
> Any suggestion on this respect would be more than welcome.
The driver is in control which slots to emit the events for, so it can
skip reporting if it wants. Consider the st1232 for which you are making
these adjustments:
for (i = 0; i < ts->chip_info->max_fingers; i++) {
u8 *buf = &ts->read_buf[i * 4];
if (buf[0] & BIT(7)) {
unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
touchscreen_set_mt_pos(&pos[n_contacts],
&ts->prop, x, y);
/* st1232 includes a z-axis / touch strength */
if (ts->chip_info->have_z)
z[n_contacts] = ts->read_buf[i + 6];
n_contacts++;
}
}
input_mt_assign_slots(input, slots, pos, n_contacts, 0);
for (i = 0; i < n_contacts; i++) {
input_mt_slot(input, slots[i]);
input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
input_report_abs(input, ABS_MT_POSITION_X, pos[i].x);
input_report_abs(input, ABS_MT_POSITION_Y, pos[i].y);
if (ts->chip_info->have_z)
input_report_abs(input, ABS_MT_TOUCH_MAJOR, z[i]);
}
Can you call touch_overlay_process_event() from the 2nd for() loop,
something like this:
for (i = 0; i < n_contacts; i++) {
if (touch_overlay_process_event(&ts->touch_overlay_list,
input,
&pos[i].x, &pos[i].y))
continue;
input_mt_slot(input, slots[i]);
...
}
Note that you will no longer able to rely on the input core to drop
"unused" slots because you may need to generate "key up" events for
them, but you do have access to input_mt_is_active() and
input_mt_is_used() so you can implement what you need.
Thanks.
--
Dmitry
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2024-07-11 17:18 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-26 9:56 [PATCH v10 0/4] Input: support overlay objects on touchscreens Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 1/4] dt-bindings: touchscreen: add touch-overlay property Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 2/4] Input: touch-overlay - Add touchscreen overlay handling Javier Carrasco
2024-07-09 0:08 ` Dmitry Torokhov
2024-07-10 12:16 ` Javier Carrasco
2024-07-11 17:18 ` Dmitry Torokhov
2024-06-26 9:56 ` [PATCH v10 3/4] dt-bindings: input: touchscreen: st1232: add touch-overlay example Javier Carrasco
2024-06-26 9:56 ` [PATCH v10 4/4] Input: st1232 - add touch overlays handling Javier Carrasco
2024-07-09 0:44 ` Dmitry Torokhov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).