From: Benjamin Tissoires <bentiss@kernel.org>
To: Jiri Kosina <jikos@kernel.org>,
Peter Hutterer <peter.hutterer@who-t.net>,
Vicki Pfau <vi@endrift.com>, Shuah Khan <shuah@kernel.org>,
Jonathan Corbet <corbet@lwn.net>
Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org, bpf@vger.kernel.org,
linux-doc@vger.kernel.org,
Benjamin Tissoires <bentiss@kernel.org>
Subject: [PATCH HID 6/7] HID: bpf: Allow to control the connect mask of hid-generic from BPF
Date: Tue, 03 Sep 2024 01:14:36 +0900 [thread overview]
Message-ID: <20240903-hid-bpf-hid-generic-v1-6-9511a565b2da@kernel.org> (raw)
In-Reply-To: <20240903-hid-bpf-hid-generic-v1-0-9511a565b2da@kernel.org>
We make struct hid_device_id writeable and use the .driver_data field
of hid-generic as the connect mask.
This way, we can control from a HID-BPF program if a device needs to
be exported through hidraw and/or hid-input mainly.
This is useful in case we want to have a third party program that directly
talks to the hidraw node and we don't want regular input events to be
emitted. This third party program can load a BPF program that instructs
hid-generic to rebind on the device with hidraw only and then open the
hidraw node itself.
When the application is closed, the BPF program is unloaded and the normal
driver takes back the control of the device.
Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
---
drivers/hid/bpf/hid_bpf_struct_ops.c | 1 +
drivers/hid/hid-core.c | 14 ++++++++------
drivers/hid/hid-generic.c | 5 +++--
3 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/drivers/hid/bpf/hid_bpf_struct_ops.c b/drivers/hid/bpf/hid_bpf_struct_ops.c
index 1e13a22f73a1..bb755edd02f0 100644
--- a/drivers/hid/bpf/hid_bpf_struct_ops.c
+++ b/drivers/hid/bpf/hid_bpf_struct_ops.c
@@ -80,6 +80,7 @@ static int hid_bpf_ops_btf_struct_access(struct bpf_verifier_log *log,
WRITE_RANGE(hid_device, name, true),
WRITE_RANGE(hid_device, uniq, true),
WRITE_RANGE(hid_device, phys, true),
+ WRITE_RANGE(hid_device_id, driver_data, false),
WRITE_RANGE(hid_bpf_driver, force_driver, false),
WRITE_RANGE(hid_bpf_driver, ignore_driver, false),
};
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index 7845f0a789ec..2bd279b23aa4 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -2637,15 +2637,17 @@ EXPORT_SYMBOL_GPL(hid_compare_device_paths);
static bool hid_check_device_match(struct hid_device *hdev,
struct hid_driver *hdrv,
- const struct hid_device_id **id)
+ struct hid_device_id *id)
{
+ const struct hid_device_id *_id = hid_match_device(hdev, hdrv);
int ret;
- *id = hid_match_device(hdev, hdrv);
- if (!*id)
+ if (!_id)
return false;
- ret = call_hid_bpf_driver_probe(hdev, hdrv, *id);
+ memcpy(id, _id, sizeof(*id));
+
+ ret = call_hid_bpf_driver_probe(hdev, hdrv, id);
if (ret)
return ret > 0;
@@ -2662,7 +2664,7 @@ static bool hid_check_device_match(struct hid_device *hdev,
static int __hid_device_probe(struct hid_device *hdev, struct hid_driver *hdrv)
{
- const struct hid_device_id *id;
+ struct hid_device_id id;
int ret;
if (!hid_check_device_match(hdev, hdrv, &id))
@@ -2677,7 +2679,7 @@ static int __hid_device_probe(struct hid_device *hdev, struct hid_driver *hdrv)
hdev->driver = hdrv;
if (hdrv->probe) {
- ret = hdrv->probe(hdev, id);
+ ret = hdrv->probe(hdev, &id);
} else { /* default probe */
ret = hid_open_report(hdev);
if (!ret)
diff --git a/drivers/hid/hid-generic.c b/drivers/hid/hid-generic.c
index f9db991d3c5a..5cd1f3a79a4b 100644
--- a/drivers/hid/hid-generic.c
+++ b/drivers/hid/hid-generic.c
@@ -64,11 +64,12 @@ static int hid_generic_probe(struct hid_device *hdev,
if (ret)
return ret;
- return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
+ return hid_hw_start(hdev, id->driver_data);
}
static const struct hid_device_id hid_table[] = {
- { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, HID_ANY_ID, HID_ANY_ID) },
+ { HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, HID_ANY_ID, HID_ANY_ID),
+ .driver_data = HID_CONNECT_DEFAULT },
{ }
};
MODULE_DEVICE_TABLE(hid, hid_table);
--
2.46.0
next prev parent reply other threads:[~2024-09-02 16:15 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-02 16:14 [PATCH HID 0/7] HID: bpf: add a new hook to control hid-generic Benjamin Tissoires
2024-09-02 16:14 ` [PATCH HID 1/7] selftests/hid: add dependency on hid_common.h Benjamin Tissoires
2024-09-02 16:14 ` [PATCH HID 2/7] selftests/hid: cleanup C tests by adding a common struct uhid_device Benjamin Tissoires
2024-09-02 16:14 ` [PATCH HID 3/7] selftests/hid: allow to parametrize bus/vid/pid/rdesc on the test device Benjamin Tissoires
2024-09-02 16:14 ` [PATCH HID 4/7] HID: bpf: allow BPF programs to force using hid-generic Benjamin Tissoires
2024-09-03 5:36 ` Peter Hutterer
2024-09-03 15:05 ` Benjamin Tissoires
2024-09-04 9:28 ` Benjamin Tissoires
2024-09-02 16:14 ` [PATCH HID 5/7] selftests/hid: add test for assigning a given device to hid-generic Benjamin Tissoires
2024-09-02 16:14 ` Benjamin Tissoires [this message]
2024-09-03 5:57 ` [PATCH HID 6/7] HID: bpf: Allow to control the connect mask of hid-generic from BPF Peter Hutterer
2024-09-03 16:32 ` Benjamin Tissoires
2024-09-02 16:14 ` [PATCH HID 7/7] selftests/hid: add test to disable hid-input Benjamin Tissoires
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240903-hid-bpf-hid-generic-v1-6-9511a565b2da@kernel.org \
--to=bentiss@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=jikos@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=peter.hutterer@who-t.net \
--cc=shuah@kernel.org \
--cc=vi@endrift.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).