| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* Copyright (c) 2022 Benjamin Tissoires |
| 3 |
* |
| 4 |
* This is a pure HID-BPF example, and should be considered as such: |
| 5 |
* on the Etekcity Scroll 6E, the X and Y axes will be swapped and |
| 6 |
* inverted. On any other device... Not sure what this will do. |
| 7 |
* |
| 8 |
* This C main file is generic though. To adapt the code and test, users |
| 9 |
* must amend only the .bpf.c file, which this program will load any |
| 10 |
* eBPF program it finds. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
#include <errno.h> |
| 15 |
#include <fcntl.h> |
| 16 |
#include <libgen.h> |
| 17 |
#include <signal.h> |
| 18 |
#include <stdbool.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <sys/resource.h> |
| 23 |
#include <unistd.h> |
| 24 |
|
| 25 |
#include <linux/bpf.h> |
| 26 |
#include <linux/errno.h> |
| 27 |
|
| 28 |
#include <bpf/bpf.h> |
| 29 |
#include <bpf/libbpf.h> |
| 30 |
|
| 31 |
#include "hid_mouse.skel.h" |
| 32 |
|
| 33 |
static bool running = true; |
| 34 |
|
| 35 |
static void int_exit(int sig) |
| 36 |
{ |
| 37 |
running = false; |
| 38 |
exit(0); |
| 39 |
} |
| 40 |
|
| 41 |
static void usage(const char *prog) |
| 42 |
{ |
| 43 |
fprintf(stderr, |
| 44 |
"%s: %s /sys/bus/hid/devices/0BUS:0VID:0PID:00ID\n\n", |
| 45 |
__func__, prog); |
| 46 |
fprintf(stderr, |
| 47 |
"This program will upload and attach a HID-BPF program to the given device.\n" |
| 48 |
"On the Etekcity Scroll 6E, the X and Y axis will be inverted, but on any other\n" |
| 49 |
"device, chances are high that the device will not be working anymore\n\n" |
| 50 |
"consider this as a demo and adapt the eBPF program to your needs\n" |
| 51 |
"Hit Ctrl-C to unbind the program and reset the device\n"); |
| 52 |
} |
| 53 |
|
| 54 |
static int get_hid_id(const char *path) |
| 55 |
{ |
| 56 |
const char *str_id, *dir; |
| 57 |
char uevent[1024]; |
| 58 |
int fd; |
| 59 |
|
| 60 |
memset(uevent, 0, sizeof(uevent)); |
| 61 |
snprintf(uevent, sizeof(uevent) - 1, "%s/uevent", path); |
| 62 |
|
| 63 |
fd = open(uevent, O_RDONLY | O_NONBLOCK); |
| 64 |
if (fd < 0) |
| 65 |
return -ENOENT; |
| 66 |
|
| 67 |
close(fd); |
| 68 |
|
| 69 |
dir = basename((char *)path); |
| 70 |
|
| 71 |
str_id = dir + sizeof("0003:0001:0A37."); |
| 72 |
return (int)strtol(str_id, NULL, 16); |
| 73 |
} |
| 74 |
|
| 75 |
int main(int argc, char **argv) |
| 76 |
{ |
| 77 |
struct hid_mouse *skel; |
| 78 |
struct bpf_link *link; |
| 79 |
int err; |
| 80 |
const char *optstr = ""; |
| 81 |
const char *sysfs_path; |
| 82 |
int opt, hid_id; |
| 83 |
|
| 84 |
while ((opt = getopt(argc, argv, optstr)) != -1) { |
| 85 |
switch (opt) { |
| 86 |
default: |
| 87 |
usage(basename(argv[0])); |
| 88 |
return 1; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
if (optind == argc) { |
| 93 |
usage(basename(argv[0])); |
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
sysfs_path = argv[optind]; |
| 98 |
if (!sysfs_path) { |
| 99 |
perror("sysfs"); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
|
| 103 |
skel = hid_mouse__open(); |
| 104 |
if (!skel) { |
| 105 |
fprintf(stderr, "%s %s:%d", __func__, __FILE__, __LINE__); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
|
| 109 |
hid_id = get_hid_id(sysfs_path); |
| 110 |
|
| 111 |
if (hid_id < 0) { |
| 112 |
fprintf(stderr, "can not open HID device: %m\n"); |
| 113 |
return 1; |
| 114 |
} |
| 115 |
skel->struct_ops.mouse_invert->hid_id = hid_id; |
| 116 |
|
| 117 |
err = hid_mouse__load(skel); |
| 118 |
if (err < 0) { |
| 119 |
fprintf(stderr, "can not load HID-BPF program: %m\n"); |
| 120 |
return 1; |
| 121 |
} |
| 122 |
|
| 123 |
link = bpf_map__attach_struct_ops(skel->maps.mouse_invert); |
| 124 |
if (!link) { |
| 125 |
fprintf(stderr, "can not attach HID-BPF program: %m\n"); |
| 126 |
return 1; |
| 127 |
} |
| 128 |
|
| 129 |
signal(SIGINT, int_exit); |
| 130 |
signal(SIGTERM, int_exit); |
| 131 |
|
| 132 |
while (running) |
| 133 |
sleep(1); |
| 134 |
|
| 135 |
hid_mouse__destroy(skel); |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |