From nobody Thu Nov 28 13:41:38 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DB491CB320; Tue, 1 Oct 2024 14:30:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727793048; cv=none; b=nJuNs1VZsu27Yy/pe6at5zYW6BWp/+s00ebim1h+4q1qLIAs5NwiYjVlOEUkO78ttEjNnKefwA5TdymhQIFi9gu51AfCVULE+Owmr5iouveCVMmJvC00z5wiq909sevxN48r3zj1WALdZzdzu9VwgdlCdgaNKX49rI4Z3QiCCLQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727793048; c=relaxed/simple; bh=Zq08hvXmwry7CvfCYsPH7jmFtsXQ5Ycau0/tGo4Jgfc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QhCYznG033/fAP4OlCYryzKnsDsQGNbKFDTsH9PlJd8ek/+k7mUjv3Ogh4WMIEl3ao1q8b30vT5lJvc0agYDT04PgF/hXm0fp6ntM4TBSBa4zJva2xtUIpIu4NOEFcZ1huq13jUIGvAlaJl/5zQ9zCJgDigwmy0OC/Bx+pvOES0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cbfVbkTf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cbfVbkTf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE286C4CEC7; Tue, 1 Oct 2024 14:30:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727793048; bh=Zq08hvXmwry7CvfCYsPH7jmFtsXQ5Ycau0/tGo4Jgfc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=cbfVbkTfrdspMX/kgUYNr3mhsdMe7YYqzbQOYoFup3XKnCLWJ3cM3gcADwGPwnOls ysSQJVmpQqqcGSyDf3FWqvekSSicdH0zP+gAayhy1780sSx1WHFXA+KUg7G+kvK3YH 17F/sMp5hSgVdUxE/JjDYd3aWgUtVVJ7ELMiA+CEZwDFtBLTe3AS+jmm/Wyw62JuGi vg0jhNIggAyMZV0rvMbQv2ZfeTBxuiFoHl/kZclBaX7ycqtfog2Be+ZfXaX3Nn/Jx1 N1rCaZECfuhypDYFIQhK7yEAm/ZHR6fN86q4sAXHyHEdj0c1yoGqjDcJVCfVvNRnFa 3wYpDL9pKGcfQ== From: Benjamin Tissoires Date: Tue, 01 Oct 2024 16:30:11 +0200 Subject: [PATCH HID v3 7/9] selftests/hid: allow to parametrize bus/vid/pid/rdesc on the test device Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241001-hid-bpf-hid-generic-v3-7-2ef1019468df@kernel.org> References: <20241001-hid-bpf-hid-generic-v3-0-2ef1019468df@kernel.org> In-Reply-To: <20241001-hid-bpf-hid-generic-v3-0-2ef1019468df@kernel.org> To: Jiri Kosina , Peter Hutterer , Vicki Pfau , Shuah Khan Cc: linux-input@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1727793031; l=6827; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=Zq08hvXmwry7CvfCYsPH7jmFtsXQ5Ycau0/tGo4Jgfc=; b=Gvp+Grj928N8IfRraiVwY83EyIJCUiGGFqiQ7R2h5P+xtVUrxY0wrO2FHvQ/HAIYAVFfWBspU YVz0avOP8rVA6dFpTkqmEEgMWE1IZwSdgjeJfKfuIef0SSqLlc28X4+ X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= This will be useful to introduce variants in tests to test the interactions between HID-BPF and some kernel modules. Reviewed-by: Peter Hutterer Signed-off-by: Benjamin Tissoires --- no changes in v3 no changes in v2 --- tools/testing/selftests/hid/hid_bpf.c | 2 +- tools/testing/selftests/hid/hid_common.h | 46 ++++++++++++++++++++--------= ---- tools/testing/selftests/hid/hidraw.c | 2 +- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/tools/testing/selftests/hid/hid_bpf.c b/tools/testing/selftest= s/hid/hid_bpf.c index 31ff92e0debd..1e979fb3542b 100644 --- a/tools/testing/selftests/hid/hid_bpf.c +++ b/tools/testing/selftests/hid/hid_bpf.c @@ -58,7 +58,7 @@ FIXTURE_SETUP(hid_bpf) { int err; =20 - err =3D setup_uhid(_metadata, &self->hid); + err =3D setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a36, rdesc,= sizeof(rdesc)); ASSERT_OK(err); } =20 diff --git a/tools/testing/selftests/hid/hid_common.h b/tools/testing/selft= ests/hid/hid_common.h index a7d836a35bb1..f77f69c6657d 100644 --- a/tools/testing/selftests/hid/hid_common.h +++ b/tools/testing/selftests/hid/hid_common.h @@ -23,6 +23,9 @@ struct uhid_device { int dev_id; /* uniq (random) number to identify the device */ int uhid_fd; int hid_id; /* HID device id in the system */ + __u16 bus; + __u32 vid; + __u32 pid; pthread_t tid; /* thread for reading uhid events */ }; =20 @@ -129,7 +132,9 @@ static int uhid_write(struct __test_metadata *_metadata= , int fd, const struct uh } } =20 -static int uhid_create(struct __test_metadata *_metadata, int fd, int rand= _nb) +static int uhid_create(struct __test_metadata *_metadata, int fd, int rand= _nb, + __u16 bus, __u32 vid, __u32 pid, __u8 *rdesc, + size_t rdesc_size) { struct uhid_event ev; char buf[25]; @@ -140,10 +145,10 @@ static int uhid_create(struct __test_metadata *_metad= ata, int fd, int rand_nb) ev.type =3D UHID_CREATE; strcpy((char *)ev.u.create.name, buf); ev.u.create.rd_data =3D rdesc; - ev.u.create.rd_size =3D sizeof(rdesc); - ev.u.create.bus =3D BUS_USB; - ev.u.create.vendor =3D 0x0001; - ev.u.create.product =3D 0x0a37; + ev.u.create.rd_size =3D rdesc_size; + ev.u.create.bus =3D bus; + ev.u.create.vendor =3D vid; + ev.u.create.product =3D pid; ev.u.create.version =3D 0; ev.u.create.country =3D 0; =20 @@ -305,15 +310,17 @@ static int uhid_send_event(struct __test_metadata *_m= etadata, struct uhid_device return uhid_write(_metadata, hid->uhid_fd, &ev); } =20 -static bool match_sysfs_device(int dev_id, const char *workdir, struct dir= ent *dir) +static bool match_sysfs_device(struct uhid_device *hid, const char *workdi= r, struct dirent *dir) { - const char *target =3D "0003:0001:0A37.*"; + char target[20] =3D ""; char phys[512]; char uevent[1024]; char temp[512]; int fd, nread; bool found =3D false; =20 + snprintf(target, sizeof(target), "%04X:%04X:%04X.*", hid->bus, hid->vid, = hid->pid); + if (fnmatch(target, dir->d_name, 0)) return false; =20 @@ -324,7 +331,7 @@ static bool match_sysfs_device(int dev_id, const char *= workdir, struct dirent *d if (fd < 0) return false; =20 - sprintf(phys, "PHYS=3D%d", dev_id); + sprintf(phys, "PHYS=3D%d", hid->dev_id); =20 nread =3D read(fd, temp, ARRAY_SIZE(temp)); if (nread > 0 && (strstr(temp, phys)) !=3D NULL) @@ -335,7 +342,7 @@ static bool match_sysfs_device(int dev_id, const char *= workdir, struct dirent *d return found; } =20 -static int get_hid_id(int dev_id) +static int get_hid_id(struct uhid_device *hid) { const char *workdir =3D "/sys/devices/virtual/misc/uhid"; const char *str_id; @@ -350,10 +357,10 @@ static int get_hid_id(int dev_id) d =3D opendir(workdir); if (d) { while ((dir =3D readdir(d)) !=3D NULL) { - if (!match_sysfs_device(dev_id, workdir, dir)) + if (!match_sysfs_device(hid, workdir, dir)) continue; =20 - str_id =3D dir->d_name + sizeof("0003:0001:0A37."); + str_id =3D dir->d_name + sizeof("0000:0000:0000."); found =3D (int)strtol(str_id, NULL, 16); =20 break; @@ -367,7 +374,7 @@ static int get_hid_id(int dev_id) return found; } =20 -static int get_hidraw(int dev_id) +static int get_hidraw(struct uhid_device *hid) { const char *workdir =3D "/sys/devices/virtual/misc/uhid"; char sysfs[1024]; @@ -384,7 +391,7 @@ static int get_hidraw(int dev_id) continue; =20 while ((dir =3D readdir(d)) !=3D NULL) { - if (!match_sysfs_device(dev_id, workdir, dir)) + if (!match_sysfs_device(hid, workdir, dir)) continue; =20 sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name); @@ -416,7 +423,7 @@ static int open_hidraw(struct uhid_device *hid) int hidraw_number; char hidraw_path[64] =3D { 0 }; =20 - hidraw_number =3D get_hidraw(hid->dev_id); + hidraw_number =3D get_hidraw(hid); if (hidraw_number < 0) return hidraw_number; =20 @@ -425,7 +432,8 @@ static int open_hidraw(struct uhid_device *hid) return open(hidraw_path, O_RDWR | O_NONBLOCK); } =20 -static int setup_uhid(struct __test_metadata *_metadata, struct uhid_devic= e *hid) +static int setup_uhid(struct __test_metadata *_metadata, struct uhid_devic= e *hid, + __u16 bus, __u32 vid, __u32 pid, const __u8 *rdesc, size_t rdesc_s= ize) { const char *path =3D "/dev/uhid"; time_t t; @@ -435,11 +443,15 @@ static int setup_uhid(struct __test_metadata *_metada= ta, struct uhid_device *hid srand((unsigned int)time(&t)); =20 hid->dev_id =3D rand() % 1024; + hid->bus =3D bus; + hid->vid =3D vid; + hid->pid =3D pid; =20 hid->uhid_fd =3D open(path, O_RDWR | O_CLOEXEC); ASSERT_GE(hid->uhid_fd, 0) TH_LOG("open uhid-cdev failed; %d", hid->uhid_= fd); =20 - ret =3D uhid_create(_metadata, hid->uhid_fd, hid->dev_id); + ret =3D uhid_create(_metadata, hid->uhid_fd, hid->dev_id, bus, vid, pid, + (__u8 *)rdesc, rdesc_size); ASSERT_EQ(0, ret) { TH_LOG("create uhid device failed: %d", ret); close(hid->uhid_fd); @@ -447,7 +459,7 @@ static int setup_uhid(struct __test_metadata *_metadata= , struct uhid_device *hid } =20 /* locate the uevent file of the created device */ - hid->hid_id =3D get_hid_id(hid->dev_id); + hid->hid_id =3D get_hid_id(hid); ASSERT_GT(hid->hid_id, 0) TH_LOG("Could not locate uhid device id: %d", hid->hid_id); =20 diff --git a/tools/testing/selftests/hid/hidraw.c b/tools/testing/selftests= /hid/hidraw.c index 5934818b2036..821db37ba4bb 100644 --- a/tools/testing/selftests/hid/hidraw.c +++ b/tools/testing/selftests/hid/hidraw.c @@ -36,7 +36,7 @@ FIXTURE_SETUP(hidraw) { int err; =20 - err =3D setup_uhid(_metadata, &self->hid); + err =3D setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a37, rdesc,= sizeof(rdesc)); ASSERT_OK(err); =20 self->hidraw_fd =3D open_hidraw(&self->hid); --=20 2.46.0