@@ -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),
};
@@ -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)
@@ -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);
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(-)