Skip to content

Commit e14e0ea

Browse files
author
Benjamin Tissoires
committed
selftests/hid: add test for assigning a given device to hid-generic
We use a well known VID/PID on a driver that doesn't need to talk to the device, ensures we created the device against the target driver, then load our program and ensure we have unbound to this driver and use hid-generic instead. Reviewed-by: Peter Hutterer <[email protected]> Acked-by: Shuah Khan <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent 645c224 commit e14e0ea

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

tools/testing/selftests/hid/hid_bpf.c

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,41 @@ FIXTURE_TEARDOWN(hid_bpf) {
5454
hid_bpf_teardown(_metadata, self, variant); \
5555
} while (0)
5656

57+
struct specific_device {
58+
const char test_name[64];
59+
__u16 bus;
60+
__u32 vid;
61+
__u32 pid;
62+
};
63+
5764
FIXTURE_SETUP(hid_bpf)
5865
{
66+
const struct specific_device *match = NULL;
5967
int err;
6068

61-
err = setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a36, rdesc, sizeof(rdesc));
69+
const struct specific_device devices[] = {
70+
{
71+
.test_name = "test_hid_driver_probe",
72+
.bus = BUS_BLUETOOTH,
73+
.vid = 0x05ac, /* USB_VENDOR_ID_APPLE */
74+
.pid = 0x022c, /* USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI */
75+
}, {
76+
.test_name = "*",
77+
.bus = BUS_USB,
78+
.vid = 0x0001,
79+
.pid = 0x0a36,
80+
}};
81+
82+
for (int i = 0; i < ARRAY_SIZE(devices); i++) {
83+
match = &devices[i];
84+
if (!strncmp(_metadata->name, devices[i].test_name, sizeof(devices[i].test_name)))
85+
break;
86+
}
87+
88+
ASSERT_OK_PTR(match);
89+
90+
err = setup_uhid(_metadata, &self->hid, match->bus, match->vid, match->pid,
91+
rdesc, sizeof(rdesc));
6292
ASSERT_OK(err);
6393
}
6494

@@ -855,6 +885,54 @@ TEST_F(hid_bpf, test_hid_attach_flags)
855885
ASSERT_EQ(buf[3], 3);
856886
}
857887

888+
static bool is_using_driver(struct __test_metadata *_metadata, struct uhid_device *hid,
889+
const char *driver)
890+
{
891+
char driver_line[512];
892+
char uevent[1024];
893+
char temp[512];
894+
int fd, nread;
895+
bool found = false;
896+
897+
sprintf(uevent, "/sys/bus/hid/devices/%04X:%04X:%04X.%04X/uevent",
898+
hid->bus, hid->vid, hid->pid, hid->hid_id);
899+
900+
fd = open(uevent, O_RDONLY | O_NONBLOCK);
901+
if (fd < 0) {
902+
TH_LOG("couldn't open '%s': %d, %d", uevent, fd, errno);
903+
return false;
904+
}
905+
906+
sprintf(driver_line, "DRIVER=%s", driver);
907+
908+
nread = read(fd, temp, ARRAY_SIZE(temp));
909+
if (nread > 0 && (strstr(temp, driver_line)) != NULL)
910+
found = true;
911+
912+
close(fd);
913+
914+
return found;
915+
}
916+
917+
/*
918+
* Attach hid_driver_probe to the given uhid device,
919+
* check that the device is now using hid-generic.
920+
*/
921+
TEST_F(hid_bpf, test_hid_driver_probe)
922+
{
923+
const struct test_program progs[] = {
924+
{
925+
.name = "hid_test_driver_probe",
926+
},
927+
};
928+
929+
ASSERT_TRUE(is_using_driver(_metadata, &self->hid, "apple"));
930+
931+
LOAD_PROGRAMS(progs);
932+
933+
ASSERT_TRUE(is_using_driver(_metadata, &self->hid, "hid-generic"));
934+
}
935+
858936
/*
859937
* Attach hid_rdesc_fixup to the given uhid device,
860938
* retrieve and open the matching hidraw node,

tools/testing/selftests/hid/progs/hid.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,15 @@ SEC(".struct_ops.link")
598598
struct hid_bpf_ops test_infinite_loop_input_report = {
599599
.hid_device_event = (void *)hid_test_infinite_loop_input_report,
600600
};
601+
602+
SEC("?struct_ops.s/hid_rdesc_fixup")
603+
int BPF_PROG(hid_test_driver_probe, struct hid_bpf_ctx *hid_ctx)
604+
{
605+
hid_ctx->hid->quirks |= HID_QUIRK_IGNORE_SPECIAL_DRIVER;
606+
return 0;
607+
}
608+
609+
SEC(".struct_ops.link")
610+
struct hid_bpf_ops test_driver_probe = {
611+
.hid_rdesc_fixup = (void *)hid_test_driver_probe,
612+
};

tools/testing/selftests/hid/progs/hid_bpf_helpers.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ struct hid_bpf_ops {
8484
struct hid_device *hdev;
8585
};
8686

87+
#define BIT(n) (1U << n)
88+
8789
#ifndef BPF_F_BEFORE
88-
#define BPF_F_BEFORE (1U << 3)
90+
#define BPF_F_BEFORE BIT(3)
8991
#endif
9092

93+
#define HID_QUIRK_IGNORE_SPECIAL_DRIVER BIT(22)
94+
9195
/* following are kfuncs exported by HID for HID-BPF */
9296
extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,
9397
unsigned int offset,

0 commit comments

Comments
 (0)