From 32ea5f778d1dc67ab08967d62ad31b12e70b7095 Mon Sep 17 00:00:00 2001
From: gonzo <gonzo@FreeBSD.org>
Date: Sat, 22 Oct 2016 16:38:39 +0000
Subject: MFC r306647, r306855, r306857

r306647:
const-ify struct evdev_methods

Submitted by:	Vladimir Kondratiev <wulf@cicgroup.ru>
Suggested by:	hselasky

r306855:
Allow using of driver's mutex instead internal one for evdev locking.

Add new API call: evdev_register_mtx which takes lock argument that
should be used instead of internal one for evdev locking. Useful for
cases if evdev_push_event() is always called with driver's lock taken
and reduces amount of lock aquisitions. This allows to avoid LOR
between ev_open/ev_close invocations and evdev_push_event() Such LOR
can happen when ev_open/ev_close methods acquire driver lock and
evdev_push_event() is called with this lock taken.

Submitted by:	Vladimir Kondratiev <wulf@cicgroup.ru>

r306857:
Implement EVDEV_FLAG_MT_AUTOREL flag (autorelease touchpoints)

Automaticaly release (send ABS_MT_TRACKING_ID = -1) MT-slots
that has not been listed in current MT protocol type B report.

Slot is counted as listed if corresponding ABS_MT_SLOT event
has been sent regardless of other MT events.

Events are sent on SYN_REPORT event.

Submitted by:	Vladimir Kondratiev <wulf@cicgroup.ru>
---
 sys/dev/evdev/evdev.c | 48 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 10 deletions(-)

(limited to 'sys/dev/evdev/evdev.c')

diff --git a/sys/dev/evdev/evdev.c b/sys/dev/evdev/evdev.c
index 095552e..63e0496 100644
--- a/sys/dev/evdev/evdev.c
+++ b/sys/dev/evdev/evdev.c
@@ -187,8 +187,8 @@ evdev_estimate_report_size(struct evdev_dev *evdev)
 	return (size);
 }
 
-int
-evdev_register(struct evdev_dev *evdev)
+static int
+evdev_register_common(struct evdev_dev *evdev)
 {
 	int ret;
 
@@ -196,7 +196,6 @@ evdev_register(struct evdev_dev *evdev)
 	    evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
 
 	/* Initialize internal structures */
-	mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
 	LIST_INIT(&evdev->ev_clients);
 
 	if (evdev_event_supported(evdev, EV_REP) &&
@@ -228,6 +227,19 @@ evdev_register(struct evdev_dev *evdev)
 	/* Create char device node */
 	ret = evdev_cdev_create(evdev);
 bail_out:
+	return (ret);
+}
+
+int
+evdev_register(struct evdev_dev *evdev)
+{
+	int ret;
+
+	evdev->ev_lock_type = EV_LOCK_INTERNAL;
+	evdev->ev_lock = &evdev->ev_mtx;
+	mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF);
+
+	ret = evdev_register_common(evdev);
 	if (ret != 0)
 		mtx_destroy(&evdev->ev_mtx);
 
@@ -235,6 +247,15 @@ bail_out:
 }
 
 int
+evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx)
+{
+
+	evdev->ev_lock_type = EV_LOCK_MTX;
+	evdev->ev_lock = mtx;
+	return (evdev_register_common(evdev));
+}
+
+int
 evdev_unregister(struct evdev_dev *evdev)
 {
 	struct evdev_client *client;
@@ -257,7 +278,7 @@ evdev_unregister(struct evdev_dev *evdev)
 	/* destroy_dev can sleep so release lock */
 	ret = evdev_cdev_destroy(evdev);
 	evdev->ev_cdev = NULL;
-	if (ret == 0)
+	if (ret == 0 && evdev->ev_lock_type == EV_LOCK_INTERNAL)
 		mtx_destroy(&evdev->ev_mtx);
 
 	evdev_free_absinfo(evdev->ev_absinfo);
@@ -302,7 +323,7 @@ evdev_set_serial(struct evdev_dev *evdev, const char *serial)
 
 inline void
 evdev_set_methods(struct evdev_dev *evdev, void *softc,
-    struct evdev_methods *methods)
+    const struct evdev_methods *methods)
 {
 
 	evdev->ev_methods = methods;
@@ -665,6 +686,8 @@ evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
 
 	case EV_SYN:
 		if (code == SYN_REPORT) {
+			/* Count empty reports as well as non empty */
+			evdev->ev_report_count++;
 			/* Skip empty reports */
 			if (!evdev->ev_report_opened)
 				return (EV_SKIP_EVENT);
@@ -701,10 +724,7 @@ evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
 		EVDEV_CLIENT_UNLOCKQ(client);
 	}
 
-	/* Update counters */
 	evdev->ev_event_count++;
-	if (type == EV_SYN && code == SYN_REPORT)
-		evdev->ev_report_count++;
 }
 
 void
@@ -735,16 +755,24 @@ evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
     int32_t value)
 {
 
+	if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
+		EVDEV_LOCK_ASSERT(evdev);
+
 	if (evdev_check_event(evdev, type, code, value) != 0)
 		return (EINVAL);
 
-	EVDEV_LOCK(evdev);
+	if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
+		EVDEV_LOCK(evdev);
 	evdev_modify_event(evdev, type, code, &value);
+	if (type == EV_SYN && code == SYN_REPORT &&
+	     bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
+		evdev_send_mt_autorel(evdev);
 	if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened &&
 	    bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
 		evdev_send_mt_compat(evdev);
 	evdev_send_event(evdev, type, code, value);
-	EVDEV_UNLOCK(evdev);
+	if (evdev->ev_lock_type == EV_LOCK_INTERNAL)
+		EVDEV_UNLOCK(evdev);
 
 	return (0);
 }
-- 
cgit v1.1