Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

samples/hid: add Surface Dial example

Add a more complete HID-BPF example.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

authored by

Benjamin Tissoires and committed by
Jiri Kosina
a56a2569 6008105b

+368 -1
+1
samples/hid/.gitignore
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 2 hid_mouse 3 + hid_surface_dial 3 4 *.out 4 5 *.skel.h 5 6 /vmlinux.h
+5 -1
samples/hid/Makefile
··· 7 7 8 8 # List of programs to build 9 9 tprogs-y += hid_mouse 10 + tprogs-y += hid_surface_dial 10 11 11 12 # Libbpf dependencies 12 13 LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf ··· 20 19 EXTRA_BPF_HEADERS := hid_bpf_helpers.h 21 20 22 21 hid_mouse-objs := hid_mouse.o 22 + hid_surface_dial-objs := hid_surface_dial.o 23 23 24 24 # Tell kbuild to always build the programs 25 25 always-y := $(tprogs-y) ··· 158 156 .PHONY: libbpf_hdrs 159 157 160 158 $(obj)/hid_mouse.o: $(obj)/hid_mouse.skel.h 159 + $(obj)/hid_surface_dial.o: $(obj)/hid_surface_dial.skel.h 161 160 162 161 -include $(HID_SAMPLES_PATH)/Makefile.target 163 162 ··· 204 201 -I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \ 205 202 -c $(filter %.bpf.c,$^) -o $@ 206 203 207 - LINKED_SKELS := hid_mouse.skel.h 204 + LINKED_SKELS := hid_mouse.skel.h hid_surface_dial.skel.h 208 205 clean-files += $(LINKED_SKELS) 209 206 210 207 hid_mouse.skel.h-deps := hid_mouse.bpf.o hid_bpf_attach.bpf.o 208 + hid_surface_dial.skel.h-deps := hid_surface_dial.bpf.o hid_bpf_attach.bpf.o 211 209 212 210 LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps))) 213 211
+2
samples/hid/hid_bpf_helpers.h
··· 10 10 unsigned int offset, 11 11 const size_t __sz) __ksym; 12 12 extern int hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, u32 flags) __ksym; 13 + extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym; 14 + extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym; 13 15 extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx, 14 16 __u8 *data, 15 17 size_t buf__sz,
+134
samples/hid/hid_surface_dial.bpf.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* Copyright (c) 2022 Benjamin Tissoires 3 + */ 4 + 5 + #include "vmlinux.h" 6 + #include <bpf/bpf_helpers.h> 7 + #include <bpf/bpf_tracing.h> 8 + #include "hid_bpf_helpers.h" 9 + 10 + #define HID_UP_BUTTON 0x0009 11 + #define HID_GD_WHEEL 0x0038 12 + 13 + SEC("fmod_ret/hid_bpf_device_event") 14 + int BPF_PROG(hid_event, struct hid_bpf_ctx *hctx) 15 + { 16 + __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 9 /* size */); 17 + 18 + if (!data) 19 + return 0; /* EPERM check */ 20 + 21 + /* Touch */ 22 + data[1] &= 0xfd; 23 + 24 + /* X */ 25 + data[4] = 0; 26 + data[5] = 0; 27 + 28 + /* Y */ 29 + data[6] = 0; 30 + data[7] = 0; 31 + 32 + return 0; 33 + } 34 + 35 + /* 72 == 360 / 5 -> 1 report every 5 degrees */ 36 + int resolution = 72; 37 + int physical = 5; 38 + 39 + struct haptic_syscall_args { 40 + unsigned int hid; 41 + int retval; 42 + }; 43 + 44 + static __u8 haptic_data[8]; 45 + 46 + SEC("syscall") 47 + int set_haptic(struct haptic_syscall_args *args) 48 + { 49 + struct hid_bpf_ctx *ctx; 50 + const size_t size = sizeof(haptic_data); 51 + u16 *res; 52 + int ret; 53 + 54 + if (size > sizeof(haptic_data)) 55 + return -7; /* -E2BIG */ 56 + 57 + ctx = hid_bpf_allocate_context(args->hid); 58 + if (!ctx) 59 + return -1; /* EPERM check */ 60 + 61 + haptic_data[0] = 1; /* report ID */ 62 + 63 + ret = hid_bpf_hw_request(ctx, haptic_data, size, HID_FEATURE_REPORT, HID_REQ_GET_REPORT); 64 + 65 + bpf_printk("probed/remove event ret value: %d", ret); 66 + bpf_printk("buf: %02x %02x %02x", 67 + haptic_data[0], 68 + haptic_data[1], 69 + haptic_data[2]); 70 + bpf_printk(" %02x %02x %02x", 71 + haptic_data[3], 72 + haptic_data[4], 73 + haptic_data[5]); 74 + bpf_printk(" %02x %02x", 75 + haptic_data[6], 76 + haptic_data[7]); 77 + 78 + /* whenever resolution multiplier is not 3600, we have the fixed report descriptor */ 79 + res = (u16 *)&haptic_data[1]; 80 + if (*res != 3600) { 81 + // haptic_data[1] = 72; /* resolution multiplier */ 82 + // haptic_data[2] = 0; /* resolution multiplier */ 83 + // haptic_data[3] = 0; /* Repeat Count */ 84 + haptic_data[4] = 3; /* haptic Auto Trigger */ 85 + // haptic_data[5] = 5; /* Waveform Cutoff Time */ 86 + // haptic_data[6] = 80; /* Retrigger Period */ 87 + // haptic_data[7] = 0; /* Retrigger Period */ 88 + } else { 89 + haptic_data[4] = 0; 90 + } 91 + 92 + ret = hid_bpf_hw_request(ctx, haptic_data, size, HID_FEATURE_REPORT, HID_REQ_SET_REPORT); 93 + 94 + bpf_printk("set haptic ret value: %d -> %d", ret, haptic_data[4]); 95 + 96 + args->retval = ret; 97 + 98 + hid_bpf_release_context(ctx); 99 + 100 + return 0; 101 + } 102 + 103 + /* Convert REL_DIAL into REL_WHEEL */ 104 + SEC("fmod_ret/hid_bpf_rdesc_fixup") 105 + int BPF_PROG(hid_rdesc_fixup, struct hid_bpf_ctx *hctx) 106 + { 107 + __u8 *data = hid_bpf_get_data(hctx, 0 /* offset */, 4096 /* size */); 108 + __u16 *res, *phys; 109 + 110 + if (!data) 111 + return 0; /* EPERM check */ 112 + 113 + /* Convert TOUCH into a button */ 114 + data[31] = HID_UP_BUTTON; 115 + data[33] = 2; 116 + 117 + /* Convert REL_DIAL into REL_WHEEL */ 118 + data[45] = HID_GD_WHEEL; 119 + 120 + /* Change Resolution Multiplier */ 121 + phys = (__u16 *)&data[61]; 122 + *phys = physical; 123 + res = (__u16 *)&data[66]; 124 + *res = resolution; 125 + 126 + /* Convert X,Y from Abs to Rel */ 127 + data[88] = 0x06; 128 + data[98] = 0x06; 129 + 130 + return 0; 131 + } 132 + 133 + char _license[] SEC("license") = "GPL"; 134 + u32 _version SEC("version") = 1;
+226
samples/hid/hid_surface_dial.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* Copyright (c) 2022 Benjamin Tissoires 3 + * 4 + * This program will morph the Microsoft Surface Dial into a mouse, 5 + * and depending on the chosen resolution enable or not the haptic feedback: 6 + * - a resolution (-r) of 3600 will report 3600 "ticks" in one full rotation 7 + * wihout haptic feedback 8 + * - any other resolution will report N "ticks" in a full rotation with haptic 9 + * feedback 10 + * 11 + * A good default for low resolution haptic scrolling is 72 (1 "tick" every 5 12 + * degrees), and set to 3600 for smooth scrolling. 13 + */ 14 + 15 + #include <assert.h> 16 + #include <errno.h> 17 + #include <fcntl.h> 18 + #include <libgen.h> 19 + #include <signal.h> 20 + #include <stdbool.h> 21 + #include <stdio.h> 22 + #include <stdlib.h> 23 + #include <string.h> 24 + #include <sys/resource.h> 25 + #include <unistd.h> 26 + 27 + #include <linux/bpf.h> 28 + #include <linux/errno.h> 29 + 30 + #include <bpf/bpf.h> 31 + #include <bpf/libbpf.h> 32 + 33 + #include "hid_surface_dial.skel.h" 34 + #include "hid_bpf_attach.h" 35 + 36 + static bool running = true; 37 + 38 + struct haptic_syscall_args { 39 + unsigned int hid; 40 + int retval; 41 + }; 42 + 43 + static void int_exit(int sig) 44 + { 45 + running = false; 46 + exit(0); 47 + } 48 + 49 + static void usage(const char *prog) 50 + { 51 + fprintf(stderr, 52 + "%s: %s [OPTIONS] /sys/bus/hid/devices/0BUS:0VID:0PID:00ID\n\n" 53 + " OPTIONS:\n" 54 + " -r N\t set the given resolution to the device (number of ticks per 360°)\n\n", 55 + __func__, prog); 56 + fprintf(stderr, 57 + "This program will morph the Microsoft Surface Dial into a mouse,\n" 58 + "and depending on the chosen resolution enable or not the haptic feedback:\n" 59 + "- a resolution (-r) of 3600 will report 3600 'ticks' in one full rotation\n" 60 + " wihout haptic feedback\n" 61 + "- any other resolution will report N 'ticks' in a full rotation with haptic\n" 62 + " feedback\n" 63 + "\n" 64 + "A good default for low resolution haptic scrolling is 72 (1 'tick' every 5\n" 65 + "degrees), and set to 3600 for smooth scrolling.\n"); 66 + } 67 + 68 + static int get_hid_id(const char *path) 69 + { 70 + const char *str_id, *dir; 71 + char uevent[1024]; 72 + int fd; 73 + 74 + memset(uevent, 0, sizeof(uevent)); 75 + snprintf(uevent, sizeof(uevent) - 1, "%s/uevent", path); 76 + 77 + fd = open(uevent, O_RDONLY | O_NONBLOCK); 78 + if (fd < 0) 79 + return -ENOENT; 80 + 81 + close(fd); 82 + 83 + dir = basename((char *)path); 84 + 85 + str_id = dir + sizeof("0003:0001:0A37."); 86 + return (int)strtol(str_id, NULL, 16); 87 + } 88 + 89 + static int attach_prog(struct hid_surface_dial *skel, struct bpf_program *prog, int hid_id) 90 + { 91 + struct attach_prog_args args = { 92 + .hid = hid_id, 93 + .retval = -1, 94 + }; 95 + int attach_fd, err; 96 + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr, 97 + .ctx_in = &args, 98 + .ctx_size_in = sizeof(args), 99 + ); 100 + 101 + attach_fd = bpf_program__fd(skel->progs.attach_prog); 102 + if (attach_fd < 0) { 103 + fprintf(stderr, "can't locate attach prog: %m\n"); 104 + return 1; 105 + } 106 + 107 + args.prog_fd = bpf_program__fd(prog); 108 + err = bpf_prog_test_run_opts(attach_fd, &tattr); 109 + if (err) { 110 + fprintf(stderr, "can't attach prog to hid device %d: %m (err: %d)\n", 111 + hid_id, err); 112 + return 1; 113 + } 114 + return 0; 115 + } 116 + 117 + static int set_haptic(struct hid_surface_dial *skel, int hid_id) 118 + { 119 + struct haptic_syscall_args args = { 120 + .hid = hid_id, 121 + .retval = -1, 122 + }; 123 + int haptic_fd, err; 124 + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr, 125 + .ctx_in = &args, 126 + .ctx_size_in = sizeof(args), 127 + ); 128 + 129 + haptic_fd = bpf_program__fd(skel->progs.set_haptic); 130 + if (haptic_fd < 0) { 131 + fprintf(stderr, "can't locate haptic prog: %m\n"); 132 + return 1; 133 + } 134 + 135 + err = bpf_prog_test_run_opts(haptic_fd, &tattr); 136 + if (err) { 137 + fprintf(stderr, "can't set haptic configuration to hid device %d: %m (err: %d)\n", 138 + hid_id, err); 139 + return 1; 140 + } 141 + return 0; 142 + } 143 + 144 + int main(int argc, char **argv) 145 + { 146 + struct hid_surface_dial *skel; 147 + struct bpf_program *prog; 148 + const char *optstr = "r:"; 149 + const char *sysfs_path; 150 + int opt, hid_id, resolution = 72; 151 + 152 + while ((opt = getopt(argc, argv, optstr)) != -1) { 153 + switch (opt) { 154 + case 'r': 155 + { 156 + char *endp = NULL; 157 + long l = -1; 158 + 159 + if (optarg) { 160 + l = strtol(optarg, &endp, 10); 161 + if (endp && *endp) 162 + l = -1; 163 + } 164 + 165 + if (l < 0) { 166 + fprintf(stderr, 167 + "invalid r option %s - expecting a number\n", 168 + optarg ? optarg : ""); 169 + exit(EXIT_FAILURE); 170 + }; 171 + 172 + resolution = (int) l; 173 + break; 174 + } 175 + default: 176 + usage(basename(argv[0])); 177 + return 1; 178 + } 179 + } 180 + 181 + if (optind == argc) { 182 + usage(basename(argv[0])); 183 + return 1; 184 + } 185 + 186 + sysfs_path = argv[optind]; 187 + if (!sysfs_path) { 188 + perror("sysfs"); 189 + return 1; 190 + } 191 + 192 + skel = hid_surface_dial__open_and_load(); 193 + if (!skel) { 194 + fprintf(stderr, "%s %s:%d", __func__, __FILE__, __LINE__); 195 + return -1; 196 + } 197 + 198 + hid_id = get_hid_id(sysfs_path); 199 + if (hid_id < 0) { 200 + fprintf(stderr, "can not open HID device: %m\n"); 201 + return 1; 202 + } 203 + 204 + skel->data->resolution = resolution; 205 + skel->data->physical = (int)(resolution / 72); 206 + 207 + bpf_object__for_each_program(prog, *skel->skeleton->obj) { 208 + /* ignore syscalls */ 209 + if (bpf_program__get_type(prog) != BPF_PROG_TYPE_TRACING) 210 + continue; 211 + 212 + attach_prog(skel, prog, hid_id); 213 + } 214 + 215 + signal(SIGINT, int_exit); 216 + signal(SIGTERM, int_exit); 217 + 218 + set_haptic(skel, hid_id); 219 + 220 + while (running) 221 + sleep(1); 222 + 223 + hid_surface_dial__destroy(skel); 224 + 225 + return 0; 226 + }