git: 183088934a6c - main - evdev: Extend EVIOCGRAB ioctl scope to cover kbd interface

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Fri, 18 Nov 2022 23:56:11 UTC
The branch main has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=183088934a6c4384e45ac6d01c04754e0d7a991c

commit 183088934a6c4384e45ac6d01c04754e0d7a991c
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2022-11-18 23:54:46 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2022-11-18 23:54:46 +0000

    evdev: Extend EVIOCGRAB ioctl scope to cover kbd interface
    
    of various keyboard drivers.
    EVIOCGRAB ioctl execution on /dev/input/event# device node gains
    exclusive access to this device to caller. It is used mostly for
    development purposes and remote control software. See e.g.
    https://reviews.freebsd.org/D30020 which is the reason of creation
    of this change.
    Keyboard grabbing is disabled in KDB and during panics.
    
    MFC with:       4a0db5e2920c
    Tested by:      corvink
    Differential revision:  https://reviews.freebsd.org/D30542
---
 sys/dev/atkbdc/atkbd.c        | 3 +++
 sys/dev/evdev/evdev.c         | 2 ++
 sys/dev/gpio/gpiokeys.c       | 4 ++++
 sys/dev/hid/hkbd.c            | 4 ++++
 sys/dev/hyperv/input/hv_kbd.c | 2 ++
 sys/dev/kbdmux/kbdmux.c       | 3 +++
 sys/dev/usb/input/ukbd.c      | 4 ++++
 7 files changed, 22 insertions(+)

diff --git a/sys/dev/atkbdc/atkbd.c b/sys/dev/atkbdc/atkbd.c
index 3e261115527b..eb76882bf9f7 100644
--- a/sys/dev/atkbdc/atkbd.c
+++ b/sys/dev/atkbdc/atkbd.c
@@ -703,6 +703,9 @@ next_code:
 			evdev_sync(state->ks_evdev);
 		}
 	}
+
+	if (state->ks_evdev != NULL && evdev_is_grabbed(state->ks_evdev))
+		return (NOKEY);
 #endif
 
 	/* return the byte as is for the K_RAW mode */
diff --git a/sys/dev/evdev/evdev.c b/sys/dev/evdev/evdev.c
index a0a039f0b691..2bb365125b5a 100644
--- a/sys/dev/evdev/evdev.c
+++ b/sys/dev/evdev/evdev.c
@@ -1097,6 +1097,8 @@ evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
 bool
 evdev_is_grabbed(struct evdev_dev *evdev)
 {
+	if (kdb_active || SCHEDULER_STOPPED())
+		return (false);
 	/*
 	 * The function is intended to be called from evdev-unrelated parts of
 	 * code like syscons-compatible parts of mouse and keyboard drivers.
diff --git a/sys/dev/gpio/gpiokeys.c b/sys/dev/gpio/gpiokeys.c
index bea5844bad5e..36746923f01b 100644
--- a/sys/dev/gpio/gpiokeys.c
+++ b/sys/dev/gpio/gpiokeys.c
@@ -194,6 +194,10 @@ gpiokeys_key_event(struct gpiokeys_softc *sc, struct gpiokey *key, int pressed)
 		evdev_push_key(sc->sc_evdev, key->evcode, pressed);
 		evdev_sync(sc->sc_evdev);
 	}
+	if (evdev_is_grabbed(sc->sc_evdev)) {
+		GPIOKEYS_UNLOCK(sc);
+		return;
+	}
 #endif
 	if (key->keycode != GPIOKEY_NONE) {
 		code = key->keycode & SCAN_KEYCODE_MASK;
diff --git a/sys/dev/hid/hkbd.c b/sys/dev/hid/hkbd.c
index 6fee3b0094a5..09339a762679 100644
--- a/sys/dev/hid/hkbd.c
+++ b/sys/dev/hid/hkbd.c
@@ -391,6 +391,8 @@ hkbd_put_key(struct hkbd_softc *sc, uint32_t key)
 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL)
 		evdev_push_event(sc->sc_evdev, EV_KEY,
 		    evdev_hid2key(KEY_INDEX(key)), !(key & KEY_RELEASE));
+	if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev))
+		return;
 #endif
 
 	tail = (sc->sc_inputtail + 1) % HKBD_IN_BUF_SIZE;
@@ -541,6 +543,8 @@ hkbd_interrupt(struct hkbd_softc *sc)
 #ifdef EVDEV_SUPPORT
 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL)
 		evdev_sync(sc->sc_evdev);
+	if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev))
+		return;
 #endif
 
 	/* wakeup keyboard system */
diff --git a/sys/dev/hyperv/input/hv_kbd.c b/sys/dev/hyperv/input/hv_kbd.c
index 53aacda7fbcb..ea1a8b883fbb 100644
--- a/sys/dev/hyperv/input/hv_kbd.c
+++ b/sys/dev/hyperv/input/hv_kbd.c
@@ -340,6 +340,8 @@ next_code:
 			evdev_sync(sc->ks_evdev);
 		}
 	}
+	if (sc->ks_evdev != NULL && evdev_is_grabbed(sc->ks_evdev))
+		return (NOKEY);
 #endif
 	++kbd->kb_count;
 	DEBUG_HVKBD(kbd, "read scan: 0x%x\n", scancode);
diff --git a/sys/dev/kbdmux/kbdmux.c b/sys/dev/kbdmux/kbdmux.c
index 3bbdc78b7c1a..b70e956830e5 100644
--- a/sys/dev/kbdmux/kbdmux.c
+++ b/sys/dev/kbdmux/kbdmux.c
@@ -715,6 +715,9 @@ next_code:
 			evdev_sync(state->ks_evdev);
 		}
 	}
+
+	if (state->ks_evdev != NULL && evdev_is_grabbed(state->ks_evdev))
+		return (NOKEY);
 #endif
 
 	/* return the byte as is for the K_RAW mode */
diff --git a/sys/dev/usb/input/ukbd.c b/sys/dev/usb/input/ukbd.c
index 4353888505da..c8658f9b62da 100644
--- a/sys/dev/usb/input/ukbd.c
+++ b/sys/dev/usb/input/ukbd.c
@@ -394,6 +394,8 @@ ukbd_put_key(struct ukbd_softc *sc, uint32_t key)
 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL)
 		evdev_push_event(sc->sc_evdev, EV_KEY,
 		    evdev_hid2key(KEY_INDEX(key)), !(key & KEY_RELEASE));
+	if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev))
+		return;
 #endif
 
 	if (sc->sc_inputs < UKBD_IN_BUF_SIZE) {
@@ -562,6 +564,8 @@ ukbd_interrupt(struct ukbd_softc *sc)
 #ifdef EVDEV_SUPPORT
 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_KBD && sc->sc_evdev != NULL)
 		evdev_sync(sc->sc_evdev);
+	if (sc->sc_evdev != NULL && evdev_is_grabbed(sc->sc_evdev))
+		return;
 #endif
 
 	/* wakeup keyboard system */