1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2022 Benjamin Tissoires
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 * without haptic feedback
8 * - any other resolution will report N "ticks" in a full rotation with haptic
11 * A good default for low resolution haptic scrolling is 72 (1 "tick" every 5
12 * degrees), and set to 3600 for smooth scrolling.
24 #include <sys/resource.h>
27 #include <linux/bpf.h>
28 #include <linux/errno.h>
31 #include <bpf/libbpf.h>
33 #include "hid_surface_dial.skel.h"
34 #include "hid_bpf_attach.h"
36 static bool running = true;
38 struct haptic_syscall_args {
43 static void int_exit(int sig)
49 static void usage(const char *prog)
52 "%s: %s [OPTIONS] /sys/bus/hid/devices/0BUS:0VID:0PID:00ID\n\n"
54 " -r N\t set the given resolution to the device (number of ticks per 360°)\n\n",
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 " without haptic feedback\n"
61 "- any other resolution will report N 'ticks' in a full rotation with haptic\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");
68 static int get_hid_id(const char *path)
70 const char *str_id, *dir;
74 memset(uevent, 0, sizeof(uevent));
75 snprintf(uevent, sizeof(uevent) - 1, "%s/uevent", path);
77 fd = open(uevent, O_RDONLY | O_NONBLOCK);
83 dir = basename((char *)path);
85 str_id = dir + sizeof("0003:0001:0A37.");
86 return (int)strtol(str_id, NULL, 16);
89 static int attach_prog(struct hid_surface_dial *skel, struct bpf_program *prog, int hid_id)
91 struct attach_prog_args args = {
96 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr,
98 .ctx_size_in = sizeof(args),
101 attach_fd = bpf_program__fd(skel->progs.attach_prog);
103 fprintf(stderr, "can't locate attach prog: %m\n");
107 args.prog_fd = bpf_program__fd(prog);
108 err = bpf_prog_test_run_opts(attach_fd, &tattr);
110 fprintf(stderr, "can't attach prog to hid device %d: %m (err: %d)\n",
117 static int set_haptic(struct hid_surface_dial *skel, int hid_id)
119 struct haptic_syscall_args args = {
124 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr,
126 .ctx_size_in = sizeof(args),
129 haptic_fd = bpf_program__fd(skel->progs.set_haptic);
131 fprintf(stderr, "can't locate haptic prog: %m\n");
135 err = bpf_prog_test_run_opts(haptic_fd, &tattr);
137 fprintf(stderr, "can't set haptic configuration to hid device %d: %m (err: %d)\n",
144 int main(int argc, char **argv)
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;
152 while ((opt = getopt(argc, argv, optstr)) != -1) {
160 l = strtol(optarg, &endp, 10);
167 "invalid r option %s - expecting a number\n",
168 optarg ? optarg : "");
172 resolution = (int) l;
176 usage(basename(argv[0]));
181 if (optind == argc) {
182 usage(basename(argv[0]));
186 sysfs_path = argv[optind];
192 skel = hid_surface_dial__open_and_load();
194 fprintf(stderr, "%s %s:%d", __func__, __FILE__, __LINE__);
198 hid_id = get_hid_id(sysfs_path);
200 fprintf(stderr, "can not open HID device: %m\n");
204 skel->data->resolution = resolution;
205 skel->data->physical = (int)(resolution / 72);
207 bpf_object__for_each_program(prog, *skel->skeleton->obj) {
208 /* ignore syscalls */
209 if (bpf_program__get_type(prog) != BPF_PROG_TYPE_TRACING)
212 attach_prog(skel, prog, hid_id);
215 signal(SIGINT, int_exit);
216 signal(SIGTERM, int_exit);
218 set_haptic(skel, hid_id);
223 hid_surface_dial__destroy(skel);