Path: blob/master/tools/testing/selftests/hid/hid_common.h
26302 views
/* SPDX-License-Identifier: GPL-2.0 */1/* Copyright (c) 2022-2024 Red Hat */23#include "../kselftest_harness.h"45#include <fcntl.h>6#include <fnmatch.h>7#include <dirent.h>8#include <poll.h>9#include <pthread.h>10#include <stdbool.h>11#include <linux/hidraw.h>12#include <linux/uhid.h>1314#define SHOW_UHID_DEBUG 01516#define min(a, b) \17({ __typeof__(a) _a = (a); \18__typeof__(b) _b = (b); \19_a < _b ? _a : _b; })2021struct uhid_device {22int dev_id; /* uniq (random) number to identify the device */23int uhid_fd;24int hid_id; /* HID device id in the system */25__u16 bus;26__u32 vid;27__u32 pid;28pthread_t tid; /* thread for reading uhid events */29};3031static unsigned char rdesc[] = {320x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */330x09, 0x21, /* Usage (Vendor Usage 0x21) */340xa1, 0x01, /* COLLECTION (Application) */350x09, 0x01, /* Usage (Vendor Usage 0x01) */360xa1, 0x00, /* COLLECTION (Physical) */370x85, 0x02, /* REPORT_ID (2) */380x19, 0x01, /* USAGE_MINIMUM (1) */390x29, 0x08, /* USAGE_MAXIMUM (3) */400x15, 0x00, /* LOGICAL_MINIMUM (0) */410x25, 0xff, /* LOGICAL_MAXIMUM (255) */420x95, 0x08, /* REPORT_COUNT (8) */430x75, 0x08, /* REPORT_SIZE (8) */440x81, 0x02, /* INPUT (Data,Var,Abs) */450xc0, /* END_COLLECTION */460x09, 0x01, /* Usage (Vendor Usage 0x01) */470xa1, 0x00, /* COLLECTION (Physical) */480x85, 0x01, /* REPORT_ID (1) */490x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */500x19, 0x01, /* USAGE_MINIMUM (1) */510x29, 0x03, /* USAGE_MAXIMUM (3) */520x15, 0x00, /* LOGICAL_MINIMUM (0) */530x25, 0x01, /* LOGICAL_MAXIMUM (1) */540x95, 0x03, /* REPORT_COUNT (3) */550x75, 0x01, /* REPORT_SIZE (1) */560x81, 0x02, /* INPUT (Data,Var,Abs) */570x95, 0x01, /* REPORT_COUNT (1) */580x75, 0x05, /* REPORT_SIZE (5) */590x81, 0x01, /* INPUT (Cnst,Var,Abs) */600x05, 0x01, /* USAGE_PAGE (Generic Desktop) */610x09, 0x30, /* USAGE (X) */620x09, 0x31, /* USAGE (Y) */630x15, 0x81, /* LOGICAL_MINIMUM (-127) */640x25, 0x7f, /* LOGICAL_MAXIMUM (127) */650x75, 0x10, /* REPORT_SIZE (16) */660x95, 0x02, /* REPORT_COUNT (2) */670x81, 0x06, /* INPUT (Data,Var,Rel) */68690x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */700x19, 0x01, /* USAGE_MINIMUM (1) */710x29, 0x03, /* USAGE_MAXIMUM (3) */720x15, 0x00, /* LOGICAL_MINIMUM (0) */730x25, 0x01, /* LOGICAL_MAXIMUM (1) */740x95, 0x03, /* REPORT_COUNT (3) */750x75, 0x01, /* REPORT_SIZE (1) */760x91, 0x02, /* Output (Data,Var,Abs) */770x95, 0x01, /* REPORT_COUNT (1) */780x75, 0x05, /* REPORT_SIZE (5) */790x91, 0x01, /* Output (Cnst,Var,Abs) */80810x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */820x19, 0x06, /* USAGE_MINIMUM (6) */830x29, 0x08, /* USAGE_MAXIMUM (8) */840x15, 0x00, /* LOGICAL_MINIMUM (0) */850x25, 0x01, /* LOGICAL_MAXIMUM (1) */860x95, 0x03, /* REPORT_COUNT (3) */870x75, 0x01, /* REPORT_SIZE (1) */880xb1, 0x02, /* Feature (Data,Var,Abs) */890x95, 0x01, /* REPORT_COUNT (1) */900x75, 0x05, /* REPORT_SIZE (5) */910x91, 0x01, /* Output (Cnst,Var,Abs) */92930xc0, /* END_COLLECTION */940xc0, /* END_COLLECTION */95};9697static __u8 feature_data[] = { 1, 2 };9899#define ASSERT_OK(data) ASSERT_FALSE(data)100#define ASSERT_OK_PTR(ptr) ASSERT_NE(NULL, ptr)101102#define UHID_LOG(fmt, ...) do { \103if (SHOW_UHID_DEBUG) \104TH_LOG(fmt, ##__VA_ARGS__); \105} while (0)106107static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER;108static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER;109110static pthread_mutex_t uhid_output_mtx = PTHREAD_MUTEX_INITIALIZER;111static pthread_cond_t uhid_output_cond = PTHREAD_COND_INITIALIZER;112static unsigned char output_report[10];113114/* no need to protect uhid_stopped, only one thread accesses it */115static bool uhid_stopped;116117static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uhid_event *ev)118{119ssize_t ret;120121ret = write(fd, ev, sizeof(*ev));122if (ret < 0) {123TH_LOG("Cannot write to uhid: %m");124return -errno;125} else if (ret != sizeof(*ev)) {126TH_LOG("Wrong size written to uhid: %zd != %zu",127ret, sizeof(ev));128return -EFAULT;129} else {130return 0;131}132}133134static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb,135__u16 bus, __u32 vid, __u32 pid, __u8 *rdesc,136size_t rdesc_size)137{138struct uhid_event ev;139char buf[25];140141sprintf(buf, "test-uhid-device-%d", rand_nb);142143memset(&ev, 0, sizeof(ev));144ev.type = UHID_CREATE;145strcpy((char *)ev.u.create.name, buf);146ev.u.create.rd_data = rdesc;147ev.u.create.rd_size = rdesc_size;148ev.u.create.bus = bus;149ev.u.create.vendor = vid;150ev.u.create.product = pid;151ev.u.create.version = 0;152ev.u.create.country = 0;153154sprintf(buf, "%d", rand_nb);155strcpy((char *)ev.u.create.phys, buf);156157return uhid_write(_metadata, fd, &ev);158}159160static void uhid_destroy(struct __test_metadata *_metadata, struct uhid_device *hid)161{162struct uhid_event ev;163164memset(&ev, 0, sizeof(ev));165ev.type = UHID_DESTROY;166167uhid_write(_metadata, hid->uhid_fd, &ev);168}169170static int uhid_event(struct __test_metadata *_metadata, int fd)171{172struct uhid_event ev, answer;173ssize_t ret;174175memset(&ev, 0, sizeof(ev));176ret = read(fd, &ev, sizeof(ev));177if (ret == 0) {178UHID_LOG("Read HUP on uhid-cdev");179return -EFAULT;180} else if (ret < 0) {181UHID_LOG("Cannot read uhid-cdev: %m");182return -errno;183} else if (ret != sizeof(ev)) {184UHID_LOG("Invalid size read from uhid-dev: %zd != %zu",185ret, sizeof(ev));186return -EFAULT;187}188189switch (ev.type) {190case UHID_START:191pthread_mutex_lock(&uhid_started_mtx);192pthread_cond_signal(&uhid_started);193pthread_mutex_unlock(&uhid_started_mtx);194195UHID_LOG("UHID_START from uhid-dev");196break;197case UHID_STOP:198uhid_stopped = true;199200UHID_LOG("UHID_STOP from uhid-dev");201break;202case UHID_OPEN:203UHID_LOG("UHID_OPEN from uhid-dev");204break;205case UHID_CLOSE:206UHID_LOG("UHID_CLOSE from uhid-dev");207break;208case UHID_OUTPUT:209UHID_LOG("UHID_OUTPUT from uhid-dev");210211pthread_mutex_lock(&uhid_output_mtx);212memcpy(output_report,213ev.u.output.data,214min(ev.u.output.size, sizeof(output_report)));215pthread_cond_signal(&uhid_output_cond);216pthread_mutex_unlock(&uhid_output_mtx);217break;218case UHID_GET_REPORT:219UHID_LOG("UHID_GET_REPORT from uhid-dev");220221answer.type = UHID_GET_REPORT_REPLY;222answer.u.get_report_reply.id = ev.u.get_report.id;223answer.u.get_report_reply.err = ev.u.get_report.rnum == 1 ? 0 : -EIO;224answer.u.get_report_reply.size = sizeof(feature_data);225memcpy(answer.u.get_report_reply.data, feature_data, sizeof(feature_data));226227uhid_write(_metadata, fd, &answer);228229break;230case UHID_SET_REPORT:231UHID_LOG("UHID_SET_REPORT from uhid-dev");232break;233default:234TH_LOG("Invalid event from uhid-dev: %u", ev.type);235}236237return 0;238}239240struct uhid_thread_args {241int fd;242struct __test_metadata *_metadata;243};244static void *uhid_read_events_thread(void *arg)245{246struct uhid_thread_args *args = (struct uhid_thread_args *)arg;247struct __test_metadata *_metadata = args->_metadata;248struct pollfd pfds[1];249int fd = args->fd;250int ret = 0;251252pfds[0].fd = fd;253pfds[0].events = POLLIN;254255uhid_stopped = false;256257while (!uhid_stopped) {258ret = poll(pfds, 1, 100);259if (ret < 0) {260TH_LOG("Cannot poll for fds: %m");261break;262}263if (pfds[0].revents & POLLIN) {264ret = uhid_event(_metadata, fd);265if (ret)266break;267}268}269270return (void *)(long)ret;271}272273static int uhid_start_listener(struct __test_metadata *_metadata, pthread_t *tid, int uhid_fd)274{275struct uhid_thread_args args = {276.fd = uhid_fd,277._metadata = _metadata,278};279int err;280281pthread_mutex_lock(&uhid_started_mtx);282err = pthread_create(tid, NULL, uhid_read_events_thread, (void *)&args);283ASSERT_EQ(0, err) {284TH_LOG("Could not start the uhid thread: %d", err);285pthread_mutex_unlock(&uhid_started_mtx);286close(uhid_fd);287return -EIO;288}289pthread_cond_wait(&uhid_started, &uhid_started_mtx);290pthread_mutex_unlock(&uhid_started_mtx);291292return 0;293}294295static int uhid_send_event(struct __test_metadata *_metadata, struct uhid_device *hid,296__u8 *buf, size_t size)297{298struct uhid_event ev;299300if (size > sizeof(ev.u.input.data))301return -E2BIG;302303memset(&ev, 0, sizeof(ev));304ev.type = UHID_INPUT2;305ev.u.input2.size = size;306307memcpy(ev.u.input2.data, buf, size);308309return uhid_write(_metadata, hid->uhid_fd, &ev);310}311312static bool match_sysfs_device(struct uhid_device *hid, const char *workdir, struct dirent *dir)313{314char target[20] = "";315char phys[512];316char uevent[1024];317char temp[512];318int fd, nread;319bool found = false;320321snprintf(target, sizeof(target), "%04X:%04X:%04X.*", hid->bus, hid->vid, hid->pid);322323if (fnmatch(target, dir->d_name, 0))324return false;325326/* we found the correct VID/PID, now check for phys */327sprintf(uevent, "%s/%s/uevent", workdir, dir->d_name);328329fd = open(uevent, O_RDONLY | O_NONBLOCK);330if (fd < 0)331return false;332333sprintf(phys, "PHYS=%d", hid->dev_id);334335nread = read(fd, temp, ARRAY_SIZE(temp));336if (nread > 0 && (strstr(temp, phys)) != NULL)337found = true;338339close(fd);340341return found;342}343344static int get_hid_id(struct uhid_device *hid)345{346const char *workdir = "/sys/devices/virtual/misc/uhid";347const char *str_id;348DIR *d;349struct dirent *dir;350int found = -1, attempts = 3;351352/* it would be nice to be able to use nftw, but the no_alu32 target doesn't support it */353354while (found < 0 && attempts > 0) {355attempts--;356d = opendir(workdir);357if (d) {358while ((dir = readdir(d)) != NULL) {359if (!match_sysfs_device(hid, workdir, dir))360continue;361362str_id = dir->d_name + sizeof("0000:0000:0000.");363found = (int)strtol(str_id, NULL, 16);364365break;366}367closedir(d);368}369if (found < 0)370usleep(100000);371}372373return found;374}375376static int get_hidraw(struct uhid_device *hid)377{378const char *workdir = "/sys/devices/virtual/misc/uhid";379char sysfs[1024];380DIR *d, *subd;381struct dirent *dir, *subdir;382int i, found = -1;383384/* retry 5 times in case the system is loaded */385for (i = 5; i > 0; i--) {386usleep(10);387d = opendir(workdir);388389if (!d)390continue;391392while ((dir = readdir(d)) != NULL) {393if (!match_sysfs_device(hid, workdir, dir))394continue;395396sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name);397398subd = opendir(sysfs);399if (!subd)400continue;401402while ((subdir = readdir(subd)) != NULL) {403if (fnmatch("hidraw*", subdir->d_name, 0))404continue;405406found = atoi(subdir->d_name + strlen("hidraw"));407}408409closedir(subd);410411if (found > 0)412break;413}414closedir(d);415}416417return found;418}419420static int open_hidraw(struct uhid_device *hid)421{422int hidraw_number;423char hidraw_path[64] = { 0 };424425hidraw_number = get_hidraw(hid);426if (hidraw_number < 0)427return hidraw_number;428429/* open hidraw node to check the other side of the pipe */430sprintf(hidraw_path, "/dev/hidraw%d", hidraw_number);431return open(hidraw_path, O_RDWR | O_NONBLOCK);432}433434static int setup_uhid(struct __test_metadata *_metadata, struct uhid_device *hid,435__u16 bus, __u32 vid, __u32 pid, const __u8 *rdesc, size_t rdesc_size)436{437const char *path = "/dev/uhid";438time_t t;439int ret;440441/* initialize random number generator */442srand((unsigned int)time(&t));443444hid->dev_id = rand() % 1024;445hid->bus = bus;446hid->vid = vid;447hid->pid = pid;448449hid->uhid_fd = open(path, O_RDWR | O_CLOEXEC);450ASSERT_GE(hid->uhid_fd, 0) TH_LOG("open uhid-cdev failed; %d", hid->uhid_fd);451452ret = uhid_create(_metadata, hid->uhid_fd, hid->dev_id, bus, vid, pid,453(__u8 *)rdesc, rdesc_size);454ASSERT_EQ(0, ret) {455TH_LOG("create uhid device failed: %d", ret);456close(hid->uhid_fd);457return ret;458}459460/* locate the uevent file of the created device */461hid->hid_id = get_hid_id(hid);462ASSERT_GT(hid->hid_id, 0)463TH_LOG("Could not locate uhid device id: %d", hid->hid_id);464465ret = uhid_start_listener(_metadata, &hid->tid, hid->uhid_fd);466ASSERT_EQ(0, ret) {467TH_LOG("could not start udev listener: %d", ret);468close(hid->uhid_fd);469return ret;470}471472return 0;473}474475476