Skip to content

Commit 72c5547

Browse files
author
Benjamin Tissoires
committed
selftests/hid: allow to parametrize bus/vid/pid/rdesc on the test device
This will be useful to introduce variants in tests to test the interactions between HID-BPF and some kernel modules. 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 4fb41df commit 72c5547

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

tools/testing/selftests/hid/hid_bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ FIXTURE_SETUP(hid_bpf)
5858
{
5959
int err;
6060

61-
err = setup_uhid(_metadata, &self->hid);
61+
err = setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a36, rdesc, sizeof(rdesc));
6262
ASSERT_OK(err);
6363
}
6464

tools/testing/selftests/hid/hid_common.h

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ struct uhid_device {
2323
int dev_id; /* uniq (random) number to identify the device */
2424
int uhid_fd;
2525
int hid_id; /* HID device id in the system */
26+
__u16 bus;
27+
__u32 vid;
28+
__u32 pid;
2629
pthread_t tid; /* thread for reading uhid events */
2730
};
2831

@@ -129,7 +132,9 @@ static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uh
129132
}
130133
}
131134

132-
static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb)
135+
static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb,
136+
__u16 bus, __u32 vid, __u32 pid, __u8 *rdesc,
137+
size_t rdesc_size)
133138
{
134139
struct uhid_event ev;
135140
char buf[25];
@@ -140,10 +145,10 @@ static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb)
140145
ev.type = UHID_CREATE;
141146
strcpy((char *)ev.u.create.name, buf);
142147
ev.u.create.rd_data = rdesc;
143-
ev.u.create.rd_size = sizeof(rdesc);
144-
ev.u.create.bus = BUS_USB;
145-
ev.u.create.vendor = 0x0001;
146-
ev.u.create.product = 0x0a37;
148+
ev.u.create.rd_size = rdesc_size;
149+
ev.u.create.bus = bus;
150+
ev.u.create.vendor = vid;
151+
ev.u.create.product = pid;
147152
ev.u.create.version = 0;
148153
ev.u.create.country = 0;
149154

@@ -305,15 +310,17 @@ static int uhid_send_event(struct __test_metadata *_metadata, struct uhid_device
305310
return uhid_write(_metadata, hid->uhid_fd, &ev);
306311
}
307312

308-
static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir)
313+
static bool match_sysfs_device(struct uhid_device *hid, const char *workdir, struct dirent *dir)
309314
{
310-
const char *target = "0003:0001:0A37.*";
315+
char target[20] = "";
311316
char phys[512];
312317
char uevent[1024];
313318
char temp[512];
314319
int fd, nread;
315320
bool found = false;
316321

322+
snprintf(target, sizeof(target), "%04X:%04X:%04X.*", hid->bus, hid->vid, hid->pid);
323+
317324
if (fnmatch(target, dir->d_name, 0))
318325
return false;
319326

@@ -324,7 +331,7 @@ static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *d
324331
if (fd < 0)
325332
return false;
326333

327-
sprintf(phys, "PHYS=%d", dev_id);
334+
sprintf(phys, "PHYS=%d", hid->dev_id);
328335

329336
nread = read(fd, temp, ARRAY_SIZE(temp));
330337
if (nread > 0 && (strstr(temp, phys)) != NULL)
@@ -335,7 +342,7 @@ static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *d
335342
return found;
336343
}
337344

338-
static int get_hid_id(int dev_id)
345+
static int get_hid_id(struct uhid_device *hid)
339346
{
340347
const char *workdir = "/sys/devices/virtual/misc/uhid";
341348
const char *str_id;
@@ -350,10 +357,10 @@ static int get_hid_id(int dev_id)
350357
d = opendir(workdir);
351358
if (d) {
352359
while ((dir = readdir(d)) != NULL) {
353-
if (!match_sysfs_device(dev_id, workdir, dir))
360+
if (!match_sysfs_device(hid, workdir, dir))
354361
continue;
355362

356-
str_id = dir->d_name + sizeof("0003:0001:0A37.");
363+
str_id = dir->d_name + sizeof("0000:0000:0000.");
357364
found = (int)strtol(str_id, NULL, 16);
358365

359366
break;
@@ -367,7 +374,7 @@ static int get_hid_id(int dev_id)
367374
return found;
368375
}
369376

370-
static int get_hidraw(int dev_id)
377+
static int get_hidraw(struct uhid_device *hid)
371378
{
372379
const char *workdir = "/sys/devices/virtual/misc/uhid";
373380
char sysfs[1024];
@@ -384,7 +391,7 @@ static int get_hidraw(int dev_id)
384391
continue;
385392

386393
while ((dir = readdir(d)) != NULL) {
387-
if (!match_sysfs_device(dev_id, workdir, dir))
394+
if (!match_sysfs_device(hid, workdir, dir))
388395
continue;
389396

390397
sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name);
@@ -416,7 +423,7 @@ static int open_hidraw(struct uhid_device *hid)
416423
int hidraw_number;
417424
char hidraw_path[64] = { 0 };
418425

419-
hidraw_number = get_hidraw(hid->dev_id);
426+
hidraw_number = get_hidraw(hid);
420427
if (hidraw_number < 0)
421428
return hidraw_number;
422429

@@ -425,7 +432,8 @@ static int open_hidraw(struct uhid_device *hid)
425432
return open(hidraw_path, O_RDWR | O_NONBLOCK);
426433
}
427434

428-
static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid)
435+
static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid,
436+
__u16 bus, __u32 vid, __u32 pid, const __u8 *rdesc, size_t rdesc_size)
429437
{
430438
const char *path = "/dev/uhid";
431439
time_t t;
@@ -435,19 +443,23 @@ static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid
435443
srand((unsigned int)time(&t));
436444

437445
hid->dev_id = rand() % 1024;
446+
hid->bus = bus;
447+
hid->vid = vid;
448+
hid->pid = pid;
438449

439450
hid->uhid_fd = open(path, O_RDWR | O_CLOEXEC);
440451
ASSERT_GE(hid->uhid_fd, 0) TH_LOG("open uhid-cdev failed; %d", hid->uhid_fd);
441452

442-
ret = uhid_create(_metadata, hid->uhid_fd, hid->dev_id);
453+
ret = uhid_create(_metadata, hid->uhid_fd, hid->dev_id, bus, vid, pid,
454+
(__u8 *)rdesc, rdesc_size);
443455
ASSERT_EQ(0, ret) {
444456
TH_LOG("create uhid device failed: %d", ret);
445457
close(hid->uhid_fd);
446458
return ret;
447459
}
448460

449461
/* locate the uevent file of the created device */
450-
hid->hid_id = get_hid_id(hid->dev_id);
462+
hid->hid_id = get_hid_id(hid);
451463
ASSERT_GT(hid->hid_id, 0)
452464
TH_LOG("Could not locate uhid device id: %d", hid->hid_id);
453465

tools/testing/selftests/hid/hidraw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ FIXTURE_SETUP(hidraw)
3636
{
3737
int err;
3838

39-
err = setup_uhid(_metadata, &self->hid);
39+
err = setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a37, rdesc, sizeof(rdesc));
4040
ASSERT_OK(err);
4141

4242
self->hidraw_fd = open_hidraw(&self->hid);

0 commit comments

Comments
 (0)