1*dbb60c8aSBenjamin Tissoires // SPDX-License-Identifier: GPL-2.0 2*dbb60c8aSBenjamin Tissoires /* Copyright (c) 2022 Red Hat */ 3*dbb60c8aSBenjamin Tissoires #include "hid.skel.h" 4*dbb60c8aSBenjamin Tissoires 5*dbb60c8aSBenjamin Tissoires #include "../kselftest_harness.h" 6*dbb60c8aSBenjamin Tissoires 7*dbb60c8aSBenjamin Tissoires #include <bpf/bpf.h> 8*dbb60c8aSBenjamin Tissoires #include <fcntl.h> 9*dbb60c8aSBenjamin Tissoires #include <fnmatch.h> 10*dbb60c8aSBenjamin Tissoires #include <dirent.h> 11*dbb60c8aSBenjamin Tissoires #include <poll.h> 12*dbb60c8aSBenjamin Tissoires #include <pthread.h> 13*dbb60c8aSBenjamin Tissoires #include <stdbool.h> 14*dbb60c8aSBenjamin Tissoires #include <linux/hidraw.h> 15*dbb60c8aSBenjamin Tissoires #include <linux/uhid.h> 16*dbb60c8aSBenjamin Tissoires 17*dbb60c8aSBenjamin Tissoires #define SHOW_UHID_DEBUG 0 18*dbb60c8aSBenjamin Tissoires 19*dbb60c8aSBenjamin Tissoires static unsigned char rdesc[] = { 20*dbb60c8aSBenjamin Tissoires 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 21*dbb60c8aSBenjamin Tissoires 0x09, 0x21, /* Usage (Vendor Usage 0x21) */ 22*dbb60c8aSBenjamin Tissoires 0xa1, 0x01, /* COLLECTION (Application) */ 23*dbb60c8aSBenjamin Tissoires 0x09, 0x01, /* Usage (Vendor Usage 0x01) */ 24*dbb60c8aSBenjamin Tissoires 0xa1, 0x00, /* COLLECTION (Physical) */ 25*dbb60c8aSBenjamin Tissoires 0x85, 0x01, /* REPORT_ID (1) */ 26*dbb60c8aSBenjamin Tissoires 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 27*dbb60c8aSBenjamin Tissoires 0x19, 0x01, /* USAGE_MINIMUM (1) */ 28*dbb60c8aSBenjamin Tissoires 0x29, 0x03, /* USAGE_MAXIMUM (3) */ 29*dbb60c8aSBenjamin Tissoires 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 30*dbb60c8aSBenjamin Tissoires 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 31*dbb60c8aSBenjamin Tissoires 0x95, 0x03, /* REPORT_COUNT (3) */ 32*dbb60c8aSBenjamin Tissoires 0x75, 0x01, /* REPORT_SIZE (1) */ 33*dbb60c8aSBenjamin Tissoires 0x81, 0x02, /* INPUT (Data,Var,Abs) */ 34*dbb60c8aSBenjamin Tissoires 0x95, 0x01, /* REPORT_COUNT (1) */ 35*dbb60c8aSBenjamin Tissoires 0x75, 0x05, /* REPORT_SIZE (5) */ 36*dbb60c8aSBenjamin Tissoires 0x81, 0x01, /* INPUT (Cnst,Var,Abs) */ 37*dbb60c8aSBenjamin Tissoires 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ 38*dbb60c8aSBenjamin Tissoires 0x09, 0x30, /* USAGE (X) */ 39*dbb60c8aSBenjamin Tissoires 0x09, 0x31, /* USAGE (Y) */ 40*dbb60c8aSBenjamin Tissoires 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */ 41*dbb60c8aSBenjamin Tissoires 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */ 42*dbb60c8aSBenjamin Tissoires 0x75, 0x10, /* REPORT_SIZE (16) */ 43*dbb60c8aSBenjamin Tissoires 0x95, 0x02, /* REPORT_COUNT (2) */ 44*dbb60c8aSBenjamin Tissoires 0x81, 0x06, /* INPUT (Data,Var,Rel) */ 45*dbb60c8aSBenjamin Tissoires 46*dbb60c8aSBenjamin Tissoires 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 47*dbb60c8aSBenjamin Tissoires 0x19, 0x01, /* USAGE_MINIMUM (1) */ 48*dbb60c8aSBenjamin Tissoires 0x29, 0x03, /* USAGE_MAXIMUM (3) */ 49*dbb60c8aSBenjamin Tissoires 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 50*dbb60c8aSBenjamin Tissoires 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 51*dbb60c8aSBenjamin Tissoires 0x95, 0x03, /* REPORT_COUNT (3) */ 52*dbb60c8aSBenjamin Tissoires 0x75, 0x01, /* REPORT_SIZE (1) */ 53*dbb60c8aSBenjamin Tissoires 0x91, 0x02, /* Output (Data,Var,Abs) */ 54*dbb60c8aSBenjamin Tissoires 0x95, 0x01, /* REPORT_COUNT (1) */ 55*dbb60c8aSBenjamin Tissoires 0x75, 0x05, /* REPORT_SIZE (5) */ 56*dbb60c8aSBenjamin Tissoires 0x91, 0x01, /* Output (Cnst,Var,Abs) */ 57*dbb60c8aSBenjamin Tissoires 58*dbb60c8aSBenjamin Tissoires 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ 59*dbb60c8aSBenjamin Tissoires 0x19, 0x06, /* USAGE_MINIMUM (6) */ 60*dbb60c8aSBenjamin Tissoires 0x29, 0x08, /* USAGE_MAXIMUM (8) */ 61*dbb60c8aSBenjamin Tissoires 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 62*dbb60c8aSBenjamin Tissoires 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 63*dbb60c8aSBenjamin Tissoires 0x95, 0x03, /* REPORT_COUNT (3) */ 64*dbb60c8aSBenjamin Tissoires 0x75, 0x01, /* REPORT_SIZE (1) */ 65*dbb60c8aSBenjamin Tissoires 0xb1, 0x02, /* Feature (Data,Var,Abs) */ 66*dbb60c8aSBenjamin Tissoires 0x95, 0x01, /* REPORT_COUNT (1) */ 67*dbb60c8aSBenjamin Tissoires 0x75, 0x05, /* REPORT_SIZE (5) */ 68*dbb60c8aSBenjamin Tissoires 0x91, 0x01, /* Output (Cnst,Var,Abs) */ 69*dbb60c8aSBenjamin Tissoires 70*dbb60c8aSBenjamin Tissoires 0xc0, /* END_COLLECTION */ 71*dbb60c8aSBenjamin Tissoires 0xc0, /* END_COLLECTION */ 72*dbb60c8aSBenjamin Tissoires }; 73*dbb60c8aSBenjamin Tissoires 74*dbb60c8aSBenjamin Tissoires struct attach_prog_args { 75*dbb60c8aSBenjamin Tissoires int prog_fd; 76*dbb60c8aSBenjamin Tissoires unsigned int hid; 77*dbb60c8aSBenjamin Tissoires int retval; 78*dbb60c8aSBenjamin Tissoires }; 79*dbb60c8aSBenjamin Tissoires 80*dbb60c8aSBenjamin Tissoires #define ASSERT_OK(data) ASSERT_FALSE(data) 81*dbb60c8aSBenjamin Tissoires #define ASSERT_OK_PTR(ptr) ASSERT_NE(NULL, ptr) 82*dbb60c8aSBenjamin Tissoires 83*dbb60c8aSBenjamin Tissoires #define UHID_LOG(fmt, ...) do { \ 84*dbb60c8aSBenjamin Tissoires if (SHOW_UHID_DEBUG) \ 85*dbb60c8aSBenjamin Tissoires TH_LOG(fmt, ##__VA_ARGS__); \ 86*dbb60c8aSBenjamin Tissoires } while (0) 87*dbb60c8aSBenjamin Tissoires 88*dbb60c8aSBenjamin Tissoires static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER; 89*dbb60c8aSBenjamin Tissoires static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER; 90*dbb60c8aSBenjamin Tissoires 91*dbb60c8aSBenjamin Tissoires /* no need to protect uhid_stopped, only one thread accesses it */ 92*dbb60c8aSBenjamin Tissoires static bool uhid_stopped; 93*dbb60c8aSBenjamin Tissoires 94*dbb60c8aSBenjamin Tissoires static int uhid_write(struct __test_metadata *_metadata, int fd, const struct uhid_event *ev) 95*dbb60c8aSBenjamin Tissoires { 96*dbb60c8aSBenjamin Tissoires ssize_t ret; 97*dbb60c8aSBenjamin Tissoires 98*dbb60c8aSBenjamin Tissoires ret = write(fd, ev, sizeof(*ev)); 99*dbb60c8aSBenjamin Tissoires if (ret < 0) { 100*dbb60c8aSBenjamin Tissoires TH_LOG("Cannot write to uhid: %m"); 101*dbb60c8aSBenjamin Tissoires return -errno; 102*dbb60c8aSBenjamin Tissoires } else if (ret != sizeof(*ev)) { 103*dbb60c8aSBenjamin Tissoires TH_LOG("Wrong size written to uhid: %zd != %zu", 104*dbb60c8aSBenjamin Tissoires ret, sizeof(ev)); 105*dbb60c8aSBenjamin Tissoires return -EFAULT; 106*dbb60c8aSBenjamin Tissoires } else { 107*dbb60c8aSBenjamin Tissoires return 0; 108*dbb60c8aSBenjamin Tissoires } 109*dbb60c8aSBenjamin Tissoires } 110*dbb60c8aSBenjamin Tissoires 111*dbb60c8aSBenjamin Tissoires static int uhid_create(struct __test_metadata *_metadata, int fd, int rand_nb) 112*dbb60c8aSBenjamin Tissoires { 113*dbb60c8aSBenjamin Tissoires struct uhid_event ev; 114*dbb60c8aSBenjamin Tissoires char buf[25]; 115*dbb60c8aSBenjamin Tissoires 116*dbb60c8aSBenjamin Tissoires sprintf(buf, "test-uhid-device-%d", rand_nb); 117*dbb60c8aSBenjamin Tissoires 118*dbb60c8aSBenjamin Tissoires memset(&ev, 0, sizeof(ev)); 119*dbb60c8aSBenjamin Tissoires ev.type = UHID_CREATE; 120*dbb60c8aSBenjamin Tissoires strcpy((char *)ev.u.create.name, buf); 121*dbb60c8aSBenjamin Tissoires ev.u.create.rd_data = rdesc; 122*dbb60c8aSBenjamin Tissoires ev.u.create.rd_size = sizeof(rdesc); 123*dbb60c8aSBenjamin Tissoires ev.u.create.bus = BUS_USB; 124*dbb60c8aSBenjamin Tissoires ev.u.create.vendor = 0x0001; 125*dbb60c8aSBenjamin Tissoires ev.u.create.product = 0x0a37; 126*dbb60c8aSBenjamin Tissoires ev.u.create.version = 0; 127*dbb60c8aSBenjamin Tissoires ev.u.create.country = 0; 128*dbb60c8aSBenjamin Tissoires 129*dbb60c8aSBenjamin Tissoires sprintf(buf, "%d", rand_nb); 130*dbb60c8aSBenjamin Tissoires strcpy((char *)ev.u.create.phys, buf); 131*dbb60c8aSBenjamin Tissoires 132*dbb60c8aSBenjamin Tissoires return uhid_write(_metadata, fd, &ev); 133*dbb60c8aSBenjamin Tissoires } 134*dbb60c8aSBenjamin Tissoires 135*dbb60c8aSBenjamin Tissoires static void uhid_destroy(struct __test_metadata *_metadata, int fd) 136*dbb60c8aSBenjamin Tissoires { 137*dbb60c8aSBenjamin Tissoires struct uhid_event ev; 138*dbb60c8aSBenjamin Tissoires 139*dbb60c8aSBenjamin Tissoires memset(&ev, 0, sizeof(ev)); 140*dbb60c8aSBenjamin Tissoires ev.type = UHID_DESTROY; 141*dbb60c8aSBenjamin Tissoires 142*dbb60c8aSBenjamin Tissoires uhid_write(_metadata, fd, &ev); 143*dbb60c8aSBenjamin Tissoires } 144*dbb60c8aSBenjamin Tissoires 145*dbb60c8aSBenjamin Tissoires static int uhid_event(struct __test_metadata *_metadata, int fd) 146*dbb60c8aSBenjamin Tissoires { 147*dbb60c8aSBenjamin Tissoires struct uhid_event ev; 148*dbb60c8aSBenjamin Tissoires ssize_t ret; 149*dbb60c8aSBenjamin Tissoires 150*dbb60c8aSBenjamin Tissoires memset(&ev, 0, sizeof(ev)); 151*dbb60c8aSBenjamin Tissoires ret = read(fd, &ev, sizeof(ev)); 152*dbb60c8aSBenjamin Tissoires if (ret == 0) { 153*dbb60c8aSBenjamin Tissoires UHID_LOG("Read HUP on uhid-cdev"); 154*dbb60c8aSBenjamin Tissoires return -EFAULT; 155*dbb60c8aSBenjamin Tissoires } else if (ret < 0) { 156*dbb60c8aSBenjamin Tissoires UHID_LOG("Cannot read uhid-cdev: %m"); 157*dbb60c8aSBenjamin Tissoires return -errno; 158*dbb60c8aSBenjamin Tissoires } else if (ret != sizeof(ev)) { 159*dbb60c8aSBenjamin Tissoires UHID_LOG("Invalid size read from uhid-dev: %zd != %zu", 160*dbb60c8aSBenjamin Tissoires ret, sizeof(ev)); 161*dbb60c8aSBenjamin Tissoires return -EFAULT; 162*dbb60c8aSBenjamin Tissoires } 163*dbb60c8aSBenjamin Tissoires 164*dbb60c8aSBenjamin Tissoires switch (ev.type) { 165*dbb60c8aSBenjamin Tissoires case UHID_START: 166*dbb60c8aSBenjamin Tissoires pthread_mutex_lock(&uhid_started_mtx); 167*dbb60c8aSBenjamin Tissoires pthread_cond_signal(&uhid_started); 168*dbb60c8aSBenjamin Tissoires pthread_mutex_unlock(&uhid_started_mtx); 169*dbb60c8aSBenjamin Tissoires 170*dbb60c8aSBenjamin Tissoires UHID_LOG("UHID_START from uhid-dev"); 171*dbb60c8aSBenjamin Tissoires break; 172*dbb60c8aSBenjamin Tissoires case UHID_STOP: 173*dbb60c8aSBenjamin Tissoires uhid_stopped = true; 174*dbb60c8aSBenjamin Tissoires 175*dbb60c8aSBenjamin Tissoires UHID_LOG("UHID_STOP from uhid-dev"); 176*dbb60c8aSBenjamin Tissoires break; 177*dbb60c8aSBenjamin Tissoires case UHID_OPEN: 178*dbb60c8aSBenjamin Tissoires UHID_LOG("UHID_OPEN from uhid-dev"); 179*dbb60c8aSBenjamin Tissoires break; 180*dbb60c8aSBenjamin Tissoires case UHID_CLOSE: 181*dbb60c8aSBenjamin Tissoires UHID_LOG("UHID_CLOSE from uhid-dev"); 182*dbb60c8aSBenjamin Tissoires break; 183*dbb60c8aSBenjamin Tissoires case UHID_OUTPUT: 184*dbb60c8aSBenjamin Tissoires UHID_LOG("UHID_OUTPUT from uhid-dev"); 185*dbb60c8aSBenjamin Tissoires break; 186*dbb60c8aSBenjamin Tissoires case UHID_GET_REPORT: 187*dbb60c8aSBenjamin Tissoires UHID_LOG("UHID_GET_REPORT from uhid-dev"); 188*dbb60c8aSBenjamin Tissoires break; 189*dbb60c8aSBenjamin Tissoires case UHID_SET_REPORT: 190*dbb60c8aSBenjamin Tissoires UHID_LOG("UHID_SET_REPORT from uhid-dev"); 191*dbb60c8aSBenjamin Tissoires break; 192*dbb60c8aSBenjamin Tissoires default: 193*dbb60c8aSBenjamin Tissoires TH_LOG("Invalid event from uhid-dev: %u", ev.type); 194*dbb60c8aSBenjamin Tissoires } 195*dbb60c8aSBenjamin Tissoires 196*dbb60c8aSBenjamin Tissoires return 0; 197*dbb60c8aSBenjamin Tissoires } 198*dbb60c8aSBenjamin Tissoires 199*dbb60c8aSBenjamin Tissoires struct uhid_thread_args { 200*dbb60c8aSBenjamin Tissoires int fd; 201*dbb60c8aSBenjamin Tissoires struct __test_metadata *_metadata; 202*dbb60c8aSBenjamin Tissoires }; 203*dbb60c8aSBenjamin Tissoires static void *uhid_read_events_thread(void *arg) 204*dbb60c8aSBenjamin Tissoires { 205*dbb60c8aSBenjamin Tissoires struct uhid_thread_args *args = (struct uhid_thread_args *)arg; 206*dbb60c8aSBenjamin Tissoires struct __test_metadata *_metadata = args->_metadata; 207*dbb60c8aSBenjamin Tissoires struct pollfd pfds[1]; 208*dbb60c8aSBenjamin Tissoires int fd = args->fd; 209*dbb60c8aSBenjamin Tissoires int ret = 0; 210*dbb60c8aSBenjamin Tissoires 211*dbb60c8aSBenjamin Tissoires pfds[0].fd = fd; 212*dbb60c8aSBenjamin Tissoires pfds[0].events = POLLIN; 213*dbb60c8aSBenjamin Tissoires 214*dbb60c8aSBenjamin Tissoires uhid_stopped = false; 215*dbb60c8aSBenjamin Tissoires 216*dbb60c8aSBenjamin Tissoires while (!uhid_stopped) { 217*dbb60c8aSBenjamin Tissoires ret = poll(pfds, 1, 100); 218*dbb60c8aSBenjamin Tissoires if (ret < 0) { 219*dbb60c8aSBenjamin Tissoires TH_LOG("Cannot poll for fds: %m"); 220*dbb60c8aSBenjamin Tissoires break; 221*dbb60c8aSBenjamin Tissoires } 222*dbb60c8aSBenjamin Tissoires if (pfds[0].revents & POLLIN) { 223*dbb60c8aSBenjamin Tissoires ret = uhid_event(_metadata, fd); 224*dbb60c8aSBenjamin Tissoires if (ret) 225*dbb60c8aSBenjamin Tissoires break; 226*dbb60c8aSBenjamin Tissoires } 227*dbb60c8aSBenjamin Tissoires } 228*dbb60c8aSBenjamin Tissoires 229*dbb60c8aSBenjamin Tissoires return (void *)(long)ret; 230*dbb60c8aSBenjamin Tissoires } 231*dbb60c8aSBenjamin Tissoires 232*dbb60c8aSBenjamin Tissoires static int uhid_start_listener(struct __test_metadata *_metadata, pthread_t *tid, int uhid_fd) 233*dbb60c8aSBenjamin Tissoires { 234*dbb60c8aSBenjamin Tissoires struct uhid_thread_args args = { 235*dbb60c8aSBenjamin Tissoires .fd = uhid_fd, 236*dbb60c8aSBenjamin Tissoires ._metadata = _metadata, 237*dbb60c8aSBenjamin Tissoires }; 238*dbb60c8aSBenjamin Tissoires int err; 239*dbb60c8aSBenjamin Tissoires 240*dbb60c8aSBenjamin Tissoires pthread_mutex_lock(&uhid_started_mtx); 241*dbb60c8aSBenjamin Tissoires err = pthread_create(tid, NULL, uhid_read_events_thread, (void *)&args); 242*dbb60c8aSBenjamin Tissoires ASSERT_EQ(0, err) { 243*dbb60c8aSBenjamin Tissoires TH_LOG("Could not start the uhid thread: %d", err); 244*dbb60c8aSBenjamin Tissoires pthread_mutex_unlock(&uhid_started_mtx); 245*dbb60c8aSBenjamin Tissoires close(uhid_fd); 246*dbb60c8aSBenjamin Tissoires return -EIO; 247*dbb60c8aSBenjamin Tissoires } 248*dbb60c8aSBenjamin Tissoires pthread_cond_wait(&uhid_started, &uhid_started_mtx); 249*dbb60c8aSBenjamin Tissoires pthread_mutex_unlock(&uhid_started_mtx); 250*dbb60c8aSBenjamin Tissoires 251*dbb60c8aSBenjamin Tissoires return 0; 252*dbb60c8aSBenjamin Tissoires } 253*dbb60c8aSBenjamin Tissoires 254*dbb60c8aSBenjamin Tissoires static int uhid_send_event(struct __test_metadata *_metadata, int fd, __u8 *buf, size_t size) 255*dbb60c8aSBenjamin Tissoires { 256*dbb60c8aSBenjamin Tissoires struct uhid_event ev; 257*dbb60c8aSBenjamin Tissoires 258*dbb60c8aSBenjamin Tissoires if (size > sizeof(ev.u.input.data)) 259*dbb60c8aSBenjamin Tissoires return -E2BIG; 260*dbb60c8aSBenjamin Tissoires 261*dbb60c8aSBenjamin Tissoires memset(&ev, 0, sizeof(ev)); 262*dbb60c8aSBenjamin Tissoires ev.type = UHID_INPUT2; 263*dbb60c8aSBenjamin Tissoires ev.u.input2.size = size; 264*dbb60c8aSBenjamin Tissoires 265*dbb60c8aSBenjamin Tissoires memcpy(ev.u.input2.data, buf, size); 266*dbb60c8aSBenjamin Tissoires 267*dbb60c8aSBenjamin Tissoires return uhid_write(_metadata, fd, &ev); 268*dbb60c8aSBenjamin Tissoires } 269*dbb60c8aSBenjamin Tissoires 270*dbb60c8aSBenjamin Tissoires static int setup_uhid(struct __test_metadata *_metadata, int rand_nb) 271*dbb60c8aSBenjamin Tissoires { 272*dbb60c8aSBenjamin Tissoires int fd; 273*dbb60c8aSBenjamin Tissoires const char *path = "/dev/uhid"; 274*dbb60c8aSBenjamin Tissoires int ret; 275*dbb60c8aSBenjamin Tissoires 276*dbb60c8aSBenjamin Tissoires fd = open(path, O_RDWR | O_CLOEXEC); 277*dbb60c8aSBenjamin Tissoires ASSERT_GE(fd, 0) TH_LOG("open uhid-cdev failed; %d", fd); 278*dbb60c8aSBenjamin Tissoires 279*dbb60c8aSBenjamin Tissoires ret = uhid_create(_metadata, fd, rand_nb); 280*dbb60c8aSBenjamin Tissoires ASSERT_EQ(0, ret) { 281*dbb60c8aSBenjamin Tissoires TH_LOG("create uhid device failed: %d", ret); 282*dbb60c8aSBenjamin Tissoires close(fd); 283*dbb60c8aSBenjamin Tissoires } 284*dbb60c8aSBenjamin Tissoires 285*dbb60c8aSBenjamin Tissoires return fd; 286*dbb60c8aSBenjamin Tissoires } 287*dbb60c8aSBenjamin Tissoires 288*dbb60c8aSBenjamin Tissoires static bool match_sysfs_device(int dev_id, const char *workdir, struct dirent *dir) 289*dbb60c8aSBenjamin Tissoires { 290*dbb60c8aSBenjamin Tissoires const char *target = "0003:0001:0A37.*"; 291*dbb60c8aSBenjamin Tissoires char phys[512]; 292*dbb60c8aSBenjamin Tissoires char uevent[1024]; 293*dbb60c8aSBenjamin Tissoires char temp[512]; 294*dbb60c8aSBenjamin Tissoires int fd, nread; 295*dbb60c8aSBenjamin Tissoires bool found = false; 296*dbb60c8aSBenjamin Tissoires 297*dbb60c8aSBenjamin Tissoires if (fnmatch(target, dir->d_name, 0)) 298*dbb60c8aSBenjamin Tissoires return false; 299*dbb60c8aSBenjamin Tissoires 300*dbb60c8aSBenjamin Tissoires /* we found the correct VID/PID, now check for phys */ 301*dbb60c8aSBenjamin Tissoires sprintf(uevent, "%s/%s/uevent", workdir, dir->d_name); 302*dbb60c8aSBenjamin Tissoires 303*dbb60c8aSBenjamin Tissoires fd = open(uevent, O_RDONLY | O_NONBLOCK); 304*dbb60c8aSBenjamin Tissoires if (fd < 0) 305*dbb60c8aSBenjamin Tissoires return false; 306*dbb60c8aSBenjamin Tissoires 307*dbb60c8aSBenjamin Tissoires sprintf(phys, "PHYS=%d", dev_id); 308*dbb60c8aSBenjamin Tissoires 309*dbb60c8aSBenjamin Tissoires nread = read(fd, temp, ARRAY_SIZE(temp)); 310*dbb60c8aSBenjamin Tissoires if (nread > 0 && (strstr(temp, phys)) != NULL) 311*dbb60c8aSBenjamin Tissoires found = true; 312*dbb60c8aSBenjamin Tissoires 313*dbb60c8aSBenjamin Tissoires close(fd); 314*dbb60c8aSBenjamin Tissoires 315*dbb60c8aSBenjamin Tissoires return found; 316*dbb60c8aSBenjamin Tissoires } 317*dbb60c8aSBenjamin Tissoires 318*dbb60c8aSBenjamin Tissoires static int get_hid_id(int dev_id) 319*dbb60c8aSBenjamin Tissoires { 320*dbb60c8aSBenjamin Tissoires const char *workdir = "/sys/devices/virtual/misc/uhid"; 321*dbb60c8aSBenjamin Tissoires const char *str_id; 322*dbb60c8aSBenjamin Tissoires DIR *d; 323*dbb60c8aSBenjamin Tissoires struct dirent *dir; 324*dbb60c8aSBenjamin Tissoires int found = -1, attempts = 3; 325*dbb60c8aSBenjamin Tissoires 326*dbb60c8aSBenjamin Tissoires /* it would be nice to be able to use nftw, but the no_alu32 target doesn't support it */ 327*dbb60c8aSBenjamin Tissoires 328*dbb60c8aSBenjamin Tissoires while (found < 0 && attempts > 0) { 329*dbb60c8aSBenjamin Tissoires attempts--; 330*dbb60c8aSBenjamin Tissoires d = opendir(workdir); 331*dbb60c8aSBenjamin Tissoires if (d) { 332*dbb60c8aSBenjamin Tissoires while ((dir = readdir(d)) != NULL) { 333*dbb60c8aSBenjamin Tissoires if (!match_sysfs_device(dev_id, workdir, dir)) 334*dbb60c8aSBenjamin Tissoires continue; 335*dbb60c8aSBenjamin Tissoires 336*dbb60c8aSBenjamin Tissoires str_id = dir->d_name + sizeof("0003:0001:0A37."); 337*dbb60c8aSBenjamin Tissoires found = (int)strtol(str_id, NULL, 16); 338*dbb60c8aSBenjamin Tissoires 339*dbb60c8aSBenjamin Tissoires break; 340*dbb60c8aSBenjamin Tissoires } 341*dbb60c8aSBenjamin Tissoires closedir(d); 342*dbb60c8aSBenjamin Tissoires } 343*dbb60c8aSBenjamin Tissoires if (found < 0) 344*dbb60c8aSBenjamin Tissoires usleep(100000); 345*dbb60c8aSBenjamin Tissoires } 346*dbb60c8aSBenjamin Tissoires 347*dbb60c8aSBenjamin Tissoires return found; 348*dbb60c8aSBenjamin Tissoires } 349*dbb60c8aSBenjamin Tissoires 350*dbb60c8aSBenjamin Tissoires static int get_hidraw(int dev_id) 351*dbb60c8aSBenjamin Tissoires { 352*dbb60c8aSBenjamin Tissoires const char *workdir = "/sys/devices/virtual/misc/uhid"; 353*dbb60c8aSBenjamin Tissoires char sysfs[1024]; 354*dbb60c8aSBenjamin Tissoires DIR *d, *subd; 355*dbb60c8aSBenjamin Tissoires struct dirent *dir, *subdir; 356*dbb60c8aSBenjamin Tissoires int i, found = -1; 357*dbb60c8aSBenjamin Tissoires 358*dbb60c8aSBenjamin Tissoires /* retry 5 times in case the system is loaded */ 359*dbb60c8aSBenjamin Tissoires for (i = 5; i > 0; i--) { 360*dbb60c8aSBenjamin Tissoires usleep(10); 361*dbb60c8aSBenjamin Tissoires d = opendir(workdir); 362*dbb60c8aSBenjamin Tissoires 363*dbb60c8aSBenjamin Tissoires if (!d) 364*dbb60c8aSBenjamin Tissoires continue; 365*dbb60c8aSBenjamin Tissoires 366*dbb60c8aSBenjamin Tissoires while ((dir = readdir(d)) != NULL) { 367*dbb60c8aSBenjamin Tissoires if (!match_sysfs_device(dev_id, workdir, dir)) 368*dbb60c8aSBenjamin Tissoires continue; 369*dbb60c8aSBenjamin Tissoires 370*dbb60c8aSBenjamin Tissoires sprintf(sysfs, "%s/%s/hidraw", workdir, dir->d_name); 371*dbb60c8aSBenjamin Tissoires 372*dbb60c8aSBenjamin Tissoires subd = opendir(sysfs); 373*dbb60c8aSBenjamin Tissoires if (!subd) 374*dbb60c8aSBenjamin Tissoires continue; 375*dbb60c8aSBenjamin Tissoires 376*dbb60c8aSBenjamin Tissoires while ((subdir = readdir(subd)) != NULL) { 377*dbb60c8aSBenjamin Tissoires if (fnmatch("hidraw*", subdir->d_name, 0)) 378*dbb60c8aSBenjamin Tissoires continue; 379*dbb60c8aSBenjamin Tissoires 380*dbb60c8aSBenjamin Tissoires found = atoi(subdir->d_name + strlen("hidraw")); 381*dbb60c8aSBenjamin Tissoires } 382*dbb60c8aSBenjamin Tissoires 383*dbb60c8aSBenjamin Tissoires closedir(subd); 384*dbb60c8aSBenjamin Tissoires 385*dbb60c8aSBenjamin Tissoires if (found > 0) 386*dbb60c8aSBenjamin Tissoires break; 387*dbb60c8aSBenjamin Tissoires } 388*dbb60c8aSBenjamin Tissoires closedir(d); 389*dbb60c8aSBenjamin Tissoires } 390*dbb60c8aSBenjamin Tissoires 391*dbb60c8aSBenjamin Tissoires return found; 392*dbb60c8aSBenjamin Tissoires } 393*dbb60c8aSBenjamin Tissoires 394*dbb60c8aSBenjamin Tissoires static int open_hidraw(int dev_id) 395*dbb60c8aSBenjamin Tissoires { 396*dbb60c8aSBenjamin Tissoires int hidraw_number; 397*dbb60c8aSBenjamin Tissoires char hidraw_path[64] = { 0 }; 398*dbb60c8aSBenjamin Tissoires 399*dbb60c8aSBenjamin Tissoires hidraw_number = get_hidraw(dev_id); 400*dbb60c8aSBenjamin Tissoires if (hidraw_number < 0) 401*dbb60c8aSBenjamin Tissoires return hidraw_number; 402*dbb60c8aSBenjamin Tissoires 403*dbb60c8aSBenjamin Tissoires /* open hidraw node to check the other side of the pipe */ 404*dbb60c8aSBenjamin Tissoires sprintf(hidraw_path, "/dev/hidraw%d", hidraw_number); 405*dbb60c8aSBenjamin Tissoires return open(hidraw_path, O_RDWR | O_NONBLOCK); 406*dbb60c8aSBenjamin Tissoires } 407*dbb60c8aSBenjamin Tissoires 408*dbb60c8aSBenjamin Tissoires FIXTURE(hid_bpf) { 409*dbb60c8aSBenjamin Tissoires int dev_id; 410*dbb60c8aSBenjamin Tissoires int uhid_fd; 411*dbb60c8aSBenjamin Tissoires int hidraw_fd; 412*dbb60c8aSBenjamin Tissoires int hid_id; 413*dbb60c8aSBenjamin Tissoires pthread_t tid; 414*dbb60c8aSBenjamin Tissoires struct hid *skel; 415*dbb60c8aSBenjamin Tissoires }; 416*dbb60c8aSBenjamin Tissoires static void detach_bpf(FIXTURE_DATA(hid_bpf) * self) 417*dbb60c8aSBenjamin Tissoires { 418*dbb60c8aSBenjamin Tissoires if (self->hidraw_fd) 419*dbb60c8aSBenjamin Tissoires close(self->hidraw_fd); 420*dbb60c8aSBenjamin Tissoires self->hidraw_fd = 0; 421*dbb60c8aSBenjamin Tissoires 422*dbb60c8aSBenjamin Tissoires hid__destroy(self->skel); 423*dbb60c8aSBenjamin Tissoires self->skel = NULL; 424*dbb60c8aSBenjamin Tissoires } 425*dbb60c8aSBenjamin Tissoires 426*dbb60c8aSBenjamin Tissoires FIXTURE_TEARDOWN(hid_bpf) { 427*dbb60c8aSBenjamin Tissoires void *uhid_err; 428*dbb60c8aSBenjamin Tissoires 429*dbb60c8aSBenjamin Tissoires uhid_destroy(_metadata, self->uhid_fd); 430*dbb60c8aSBenjamin Tissoires 431*dbb60c8aSBenjamin Tissoires detach_bpf(self); 432*dbb60c8aSBenjamin Tissoires pthread_join(self->tid, &uhid_err); 433*dbb60c8aSBenjamin Tissoires } 434*dbb60c8aSBenjamin Tissoires #define TEARDOWN_LOG(fmt, ...) do { \ 435*dbb60c8aSBenjamin Tissoires TH_LOG(fmt, ##__VA_ARGS__); \ 436*dbb60c8aSBenjamin Tissoires hid_bpf_teardown(_metadata, self, variant); \ 437*dbb60c8aSBenjamin Tissoires } while (0) 438*dbb60c8aSBenjamin Tissoires 439*dbb60c8aSBenjamin Tissoires FIXTURE_SETUP(hid_bpf) 440*dbb60c8aSBenjamin Tissoires { 441*dbb60c8aSBenjamin Tissoires time_t t; 442*dbb60c8aSBenjamin Tissoires int err; 443*dbb60c8aSBenjamin Tissoires 444*dbb60c8aSBenjamin Tissoires /* initialize random number generator */ 445*dbb60c8aSBenjamin Tissoires srand((unsigned int)time(&t)); 446*dbb60c8aSBenjamin Tissoires 447*dbb60c8aSBenjamin Tissoires self->dev_id = rand() % 1024; 448*dbb60c8aSBenjamin Tissoires 449*dbb60c8aSBenjamin Tissoires self->uhid_fd = setup_uhid(_metadata, self->dev_id); 450*dbb60c8aSBenjamin Tissoires 451*dbb60c8aSBenjamin Tissoires /* locate the uev, self, variant);ent file of the created device */ 452*dbb60c8aSBenjamin Tissoires self->hid_id = get_hid_id(self->dev_id); 453*dbb60c8aSBenjamin Tissoires ASSERT_GT(self->hid_id, 0) 454*dbb60c8aSBenjamin Tissoires TEARDOWN_LOG("Could not locate uhid device id: %d", self->hid_id); 455*dbb60c8aSBenjamin Tissoires 456*dbb60c8aSBenjamin Tissoires err = uhid_start_listener(_metadata, &self->tid, self->uhid_fd); 457*dbb60c8aSBenjamin Tissoires ASSERT_EQ(0, err) TEARDOWN_LOG("could not start udev listener: %d", err); 458*dbb60c8aSBenjamin Tissoires } 459*dbb60c8aSBenjamin Tissoires 460*dbb60c8aSBenjamin Tissoires struct test_program { 461*dbb60c8aSBenjamin Tissoires const char *name; 462*dbb60c8aSBenjamin Tissoires }; 463*dbb60c8aSBenjamin Tissoires #define LOAD_PROGRAMS(progs) \ 464*dbb60c8aSBenjamin Tissoires load_programs(progs, ARRAY_SIZE(progs), _metadata, self, variant) 465*dbb60c8aSBenjamin Tissoires #define LOAD_BPF \ 466*dbb60c8aSBenjamin Tissoires load_programs(NULL, 0, _metadata, self, variant) 467*dbb60c8aSBenjamin Tissoires static void load_programs(const struct test_program programs[], 468*dbb60c8aSBenjamin Tissoires const size_t progs_count, 469*dbb60c8aSBenjamin Tissoires struct __test_metadata *_metadata, 470*dbb60c8aSBenjamin Tissoires FIXTURE_DATA(hid_bpf) * self, 471*dbb60c8aSBenjamin Tissoires const FIXTURE_VARIANT(hid_bpf) * variant) 472*dbb60c8aSBenjamin Tissoires { 473*dbb60c8aSBenjamin Tissoires int attach_fd, err = -EINVAL; 474*dbb60c8aSBenjamin Tissoires struct attach_prog_args args = { 475*dbb60c8aSBenjamin Tissoires .retval = -1, 476*dbb60c8aSBenjamin Tissoires }; 477*dbb60c8aSBenjamin Tissoires DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr, 478*dbb60c8aSBenjamin Tissoires .ctx_in = &args, 479*dbb60c8aSBenjamin Tissoires .ctx_size_in = sizeof(args), 480*dbb60c8aSBenjamin Tissoires ); 481*dbb60c8aSBenjamin Tissoires 482*dbb60c8aSBenjamin Tissoires /* open the bpf file */ 483*dbb60c8aSBenjamin Tissoires self->skel = hid__open(); 484*dbb60c8aSBenjamin Tissoires ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open"); 485*dbb60c8aSBenjamin Tissoires 486*dbb60c8aSBenjamin Tissoires for (int i = 0; i < progs_count; i++) { 487*dbb60c8aSBenjamin Tissoires struct bpf_program *prog; 488*dbb60c8aSBenjamin Tissoires 489*dbb60c8aSBenjamin Tissoires prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 490*dbb60c8aSBenjamin Tissoires programs[i].name); 491*dbb60c8aSBenjamin Tissoires ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 492*dbb60c8aSBenjamin Tissoires 493*dbb60c8aSBenjamin Tissoires bpf_program__set_autoload(prog, true); 494*dbb60c8aSBenjamin Tissoires } 495*dbb60c8aSBenjamin Tissoires 496*dbb60c8aSBenjamin Tissoires err = hid__load(self->skel); 497*dbb60c8aSBenjamin Tissoires ASSERT_OK(err) TH_LOG("hid_skel_load failed: %d", err); 498*dbb60c8aSBenjamin Tissoires 499*dbb60c8aSBenjamin Tissoires attach_fd = bpf_program__fd(self->skel->progs.attach_prog); 500*dbb60c8aSBenjamin Tissoires ASSERT_GE(attach_fd, 0) TH_LOG("locate attach_prog: %d", attach_fd); 501*dbb60c8aSBenjamin Tissoires 502*dbb60c8aSBenjamin Tissoires for (int i = 0; i < progs_count; i++) { 503*dbb60c8aSBenjamin Tissoires struct bpf_program *prog; 504*dbb60c8aSBenjamin Tissoires 505*dbb60c8aSBenjamin Tissoires prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj, 506*dbb60c8aSBenjamin Tissoires programs[i].name); 507*dbb60c8aSBenjamin Tissoires ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name); 508*dbb60c8aSBenjamin Tissoires 509*dbb60c8aSBenjamin Tissoires args.prog_fd = bpf_program__fd(prog); 510*dbb60c8aSBenjamin Tissoires args.hid = self->hid_id; 511*dbb60c8aSBenjamin Tissoires err = bpf_prog_test_run_opts(attach_fd, &tattr); 512*dbb60c8aSBenjamin Tissoires ASSERT_OK(args.retval) TH_LOG("attach_hid(%s): %d", programs[i].name, args.retval); 513*dbb60c8aSBenjamin Tissoires } 514*dbb60c8aSBenjamin Tissoires 515*dbb60c8aSBenjamin Tissoires self->hidraw_fd = open_hidraw(self->dev_id); 516*dbb60c8aSBenjamin Tissoires ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 517*dbb60c8aSBenjamin Tissoires } 518*dbb60c8aSBenjamin Tissoires 519*dbb60c8aSBenjamin Tissoires /* 520*dbb60c8aSBenjamin Tissoires * A simple test to see if the fixture is working fine. 521*dbb60c8aSBenjamin Tissoires * If this fails, none of the other tests will pass. 522*dbb60c8aSBenjamin Tissoires */ 523*dbb60c8aSBenjamin Tissoires TEST_F(hid_bpf, test_create_uhid) 524*dbb60c8aSBenjamin Tissoires { 525*dbb60c8aSBenjamin Tissoires } 526*dbb60c8aSBenjamin Tissoires 527*dbb60c8aSBenjamin Tissoires /* 528*dbb60c8aSBenjamin Tissoires * Attach hid_first_event to the given uhid device, 529*dbb60c8aSBenjamin Tissoires * retrieve and open the matching hidraw node, 530*dbb60c8aSBenjamin Tissoires * inject one event in the uhid device, 531*dbb60c8aSBenjamin Tissoires * check that the program sees it and can change the data 532*dbb60c8aSBenjamin Tissoires */ 533*dbb60c8aSBenjamin Tissoires TEST_F(hid_bpf, raw_event) 534*dbb60c8aSBenjamin Tissoires { 535*dbb60c8aSBenjamin Tissoires const struct test_program progs[] = { 536*dbb60c8aSBenjamin Tissoires { .name = "hid_first_event" }, 537*dbb60c8aSBenjamin Tissoires }; 538*dbb60c8aSBenjamin Tissoires __u8 buf[10] = {0}; 539*dbb60c8aSBenjamin Tissoires int err; 540*dbb60c8aSBenjamin Tissoires 541*dbb60c8aSBenjamin Tissoires LOAD_PROGRAMS(progs); 542*dbb60c8aSBenjamin Tissoires 543*dbb60c8aSBenjamin Tissoires /* check that the program is correctly loaded */ 544*dbb60c8aSBenjamin Tissoires ASSERT_EQ(self->skel->data->callback_check, 52) TH_LOG("callback_check1"); 545*dbb60c8aSBenjamin Tissoires ASSERT_EQ(self->skel->data->callback2_check, 52) TH_LOG("callback2_check1"); 546*dbb60c8aSBenjamin Tissoires 547*dbb60c8aSBenjamin Tissoires /* inject one event */ 548*dbb60c8aSBenjamin Tissoires buf[0] = 1; 549*dbb60c8aSBenjamin Tissoires buf[1] = 42; 550*dbb60c8aSBenjamin Tissoires uhid_send_event(_metadata, self->uhid_fd, buf, 6); 551*dbb60c8aSBenjamin Tissoires 552*dbb60c8aSBenjamin Tissoires /* check that hid_first_event() was executed */ 553*dbb60c8aSBenjamin Tissoires ASSERT_EQ(self->skel->data->callback_check, 42) TH_LOG("callback_check1"); 554*dbb60c8aSBenjamin Tissoires 555*dbb60c8aSBenjamin Tissoires /* read the data from hidraw */ 556*dbb60c8aSBenjamin Tissoires memset(buf, 0, sizeof(buf)); 557*dbb60c8aSBenjamin Tissoires err = read(self->hidraw_fd, buf, sizeof(buf)); 558*dbb60c8aSBenjamin Tissoires ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 559*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[0], 1); 560*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[2], 47); 561*dbb60c8aSBenjamin Tissoires 562*dbb60c8aSBenjamin Tissoires /* inject another event */ 563*dbb60c8aSBenjamin Tissoires memset(buf, 0, sizeof(buf)); 564*dbb60c8aSBenjamin Tissoires buf[0] = 1; 565*dbb60c8aSBenjamin Tissoires buf[1] = 47; 566*dbb60c8aSBenjamin Tissoires uhid_send_event(_metadata, self->uhid_fd, buf, 6); 567*dbb60c8aSBenjamin Tissoires 568*dbb60c8aSBenjamin Tissoires /* check that hid_first_event() was executed */ 569*dbb60c8aSBenjamin Tissoires ASSERT_EQ(self->skel->data->callback_check, 47) TH_LOG("callback_check1"); 570*dbb60c8aSBenjamin Tissoires 571*dbb60c8aSBenjamin Tissoires /* read the data from hidraw */ 572*dbb60c8aSBenjamin Tissoires memset(buf, 0, sizeof(buf)); 573*dbb60c8aSBenjamin Tissoires err = read(self->hidraw_fd, buf, sizeof(buf)); 574*dbb60c8aSBenjamin Tissoires ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 575*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[2], 52); 576*dbb60c8aSBenjamin Tissoires } 577*dbb60c8aSBenjamin Tissoires 578*dbb60c8aSBenjamin Tissoires /* 579*dbb60c8aSBenjamin Tissoires * Ensures that we can attach/detach programs 580*dbb60c8aSBenjamin Tissoires */ 581*dbb60c8aSBenjamin Tissoires TEST_F(hid_bpf, test_attach_detach) 582*dbb60c8aSBenjamin Tissoires { 583*dbb60c8aSBenjamin Tissoires const struct test_program progs[] = { 584*dbb60c8aSBenjamin Tissoires { .name = "hid_first_event" }, 585*dbb60c8aSBenjamin Tissoires }; 586*dbb60c8aSBenjamin Tissoires __u8 buf[10] = {0}; 587*dbb60c8aSBenjamin Tissoires int err; 588*dbb60c8aSBenjamin Tissoires 589*dbb60c8aSBenjamin Tissoires LOAD_PROGRAMS(progs); 590*dbb60c8aSBenjamin Tissoires 591*dbb60c8aSBenjamin Tissoires /* inject one event */ 592*dbb60c8aSBenjamin Tissoires buf[0] = 1; 593*dbb60c8aSBenjamin Tissoires buf[1] = 42; 594*dbb60c8aSBenjamin Tissoires uhid_send_event(_metadata, self->uhid_fd, buf, 6); 595*dbb60c8aSBenjamin Tissoires 596*dbb60c8aSBenjamin Tissoires /* read the data from hidraw */ 597*dbb60c8aSBenjamin Tissoires memset(buf, 0, sizeof(buf)); 598*dbb60c8aSBenjamin Tissoires err = read(self->hidraw_fd, buf, sizeof(buf)); 599*dbb60c8aSBenjamin Tissoires ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 600*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[0], 1); 601*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[2], 47); 602*dbb60c8aSBenjamin Tissoires 603*dbb60c8aSBenjamin Tissoires /* pin the program and immediately unpin it */ 604*dbb60c8aSBenjamin Tissoires #define PIN_PATH "/sys/fs/bpf/hid_first_event" 605*dbb60c8aSBenjamin Tissoires bpf_program__pin(self->skel->progs.hid_first_event, PIN_PATH); 606*dbb60c8aSBenjamin Tissoires remove(PIN_PATH); 607*dbb60c8aSBenjamin Tissoires #undef PIN_PATH 608*dbb60c8aSBenjamin Tissoires usleep(100000); 609*dbb60c8aSBenjamin Tissoires 610*dbb60c8aSBenjamin Tissoires /* detach the program */ 611*dbb60c8aSBenjamin Tissoires detach_bpf(self); 612*dbb60c8aSBenjamin Tissoires 613*dbb60c8aSBenjamin Tissoires self->hidraw_fd = open_hidraw(self->dev_id); 614*dbb60c8aSBenjamin Tissoires ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw"); 615*dbb60c8aSBenjamin Tissoires 616*dbb60c8aSBenjamin Tissoires /* inject another event */ 617*dbb60c8aSBenjamin Tissoires memset(buf, 0, sizeof(buf)); 618*dbb60c8aSBenjamin Tissoires buf[0] = 1; 619*dbb60c8aSBenjamin Tissoires buf[1] = 47; 620*dbb60c8aSBenjamin Tissoires uhid_send_event(_metadata, self->uhid_fd, buf, 6); 621*dbb60c8aSBenjamin Tissoires 622*dbb60c8aSBenjamin Tissoires /* read the data from hidraw */ 623*dbb60c8aSBenjamin Tissoires memset(buf, 0, sizeof(buf)); 624*dbb60c8aSBenjamin Tissoires err = read(self->hidraw_fd, buf, sizeof(buf)); 625*dbb60c8aSBenjamin Tissoires ASSERT_EQ(err, 6) TH_LOG("read_hidraw_no_bpf"); 626*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[0], 1); 627*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[1], 47); 628*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[2], 0); 629*dbb60c8aSBenjamin Tissoires 630*dbb60c8aSBenjamin Tissoires /* re-attach our program */ 631*dbb60c8aSBenjamin Tissoires 632*dbb60c8aSBenjamin Tissoires LOAD_PROGRAMS(progs); 633*dbb60c8aSBenjamin Tissoires 634*dbb60c8aSBenjamin Tissoires /* inject one event */ 635*dbb60c8aSBenjamin Tissoires memset(buf, 0, sizeof(buf)); 636*dbb60c8aSBenjamin Tissoires buf[0] = 1; 637*dbb60c8aSBenjamin Tissoires buf[1] = 42; 638*dbb60c8aSBenjamin Tissoires uhid_send_event(_metadata, self->uhid_fd, buf, 6); 639*dbb60c8aSBenjamin Tissoires 640*dbb60c8aSBenjamin Tissoires /* read the data from hidraw */ 641*dbb60c8aSBenjamin Tissoires memset(buf, 0, sizeof(buf)); 642*dbb60c8aSBenjamin Tissoires err = read(self->hidraw_fd, buf, sizeof(buf)); 643*dbb60c8aSBenjamin Tissoires ASSERT_EQ(err, 6) TH_LOG("read_hidraw"); 644*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[0], 1); 645*dbb60c8aSBenjamin Tissoires ASSERT_EQ(buf[2], 47); 646*dbb60c8aSBenjamin Tissoires } 647*dbb60c8aSBenjamin Tissoires 648*dbb60c8aSBenjamin Tissoires static int libbpf_print_fn(enum libbpf_print_level level, 649*dbb60c8aSBenjamin Tissoires const char *format, va_list args) 650*dbb60c8aSBenjamin Tissoires { 651*dbb60c8aSBenjamin Tissoires char buf[1024]; 652*dbb60c8aSBenjamin Tissoires 653*dbb60c8aSBenjamin Tissoires if (level == LIBBPF_DEBUG) 654*dbb60c8aSBenjamin Tissoires return 0; 655*dbb60c8aSBenjamin Tissoires 656*dbb60c8aSBenjamin Tissoires snprintf(buf, sizeof(buf), "# %s", format); 657*dbb60c8aSBenjamin Tissoires 658*dbb60c8aSBenjamin Tissoires vfprintf(stdout, buf, args); 659*dbb60c8aSBenjamin Tissoires return 0; 660*dbb60c8aSBenjamin Tissoires } 661*dbb60c8aSBenjamin Tissoires 662*dbb60c8aSBenjamin Tissoires static void __attribute__((constructor)) __constructor_order_last(void) 663*dbb60c8aSBenjamin Tissoires { 664*dbb60c8aSBenjamin Tissoires if (!__constructor_order) 665*dbb60c8aSBenjamin Tissoires __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; 666*dbb60c8aSBenjamin Tissoires } 667*dbb60c8aSBenjamin Tissoires 668*dbb60c8aSBenjamin Tissoires int main(int argc, char **argv) 669*dbb60c8aSBenjamin Tissoires { 670*dbb60c8aSBenjamin Tissoires /* Use libbpf 1.0 API mode */ 671*dbb60c8aSBenjamin Tissoires libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 672*dbb60c8aSBenjamin Tissoires libbpf_set_print(libbpf_print_fn); 673*dbb60c8aSBenjamin Tissoires 674*dbb60c8aSBenjamin Tissoires return test_harness_run(argc, argv); 675*dbb60c8aSBenjamin Tissoires } 676