hid_mouse.c (9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e) | hid_mouse.c (e342d6f6f7d82b48c4540b947d8032a3b7b3e6f8) |
---|---|
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 --- 15 unchanged lines hidden (view full) --- 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" | 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 --- 15 unchanged lines hidden (view full) --- 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#include "hid_bpf_attach.h" | |
33 34static bool running = true; 35 36static void int_exit(int sig) 37{ 38 running = false; 39 exit(0); 40} --- 30 unchanged lines hidden (view full) --- 71 72 str_id = dir + sizeof("0003:0001:0A37."); 73 return (int)strtol(str_id, NULL, 16); 74} 75 76int main(int argc, char **argv) 77{ 78 struct hid_mouse *skel; | 32 33static bool running = true; 34 35static void int_exit(int sig) 36{ 37 running = false; 38 exit(0); 39} --- 30 unchanged lines hidden (view full) --- 70 71 str_id = dir + sizeof("0003:0001:0A37."); 72 return (int)strtol(str_id, NULL, 16); 73} 74 75int main(int argc, char **argv) 76{ 77 struct hid_mouse *skel; |
79 struct bpf_program *prog; | 78 struct bpf_link *link; |
80 int err; 81 const char *optstr = ""; 82 const char *sysfs_path; | 79 int err; 80 const char *optstr = ""; 81 const char *sysfs_path; |
83 int opt, hid_id, attach_fd; 84 struct attach_prog_args args = { 85 .retval = -1, 86 }; 87 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattr, 88 .ctx_in = &args, 89 .ctx_size_in = sizeof(args), 90 ); | 82 int opt, hid_id; |
91 92 while ((opt = getopt(argc, argv, optstr)) != -1) { 93 switch (opt) { 94 default: 95 usage(basename(argv[0])); 96 return 1; 97 } 98 } --- 4 unchanged lines hidden (view full) --- 103 } 104 105 sysfs_path = argv[optind]; 106 if (!sysfs_path) { 107 perror("sysfs"); 108 return 1; 109 } 110 | 83 84 while ((opt = getopt(argc, argv, optstr)) != -1) { 85 switch (opt) { 86 default: 87 usage(basename(argv[0])); 88 return 1; 89 } 90 } --- 4 unchanged lines hidden (view full) --- 95 } 96 97 sysfs_path = argv[optind]; 98 if (!sysfs_path) { 99 perror("sysfs"); 100 return 1; 101 } 102 |
111 skel = hid_mouse__open_and_load(); | 103 skel = hid_mouse__open(); |
112 if (!skel) { 113 fprintf(stderr, "%s %s:%d", __func__, __FILE__, __LINE__); 114 return -1; 115 } 116 117 hid_id = get_hid_id(sysfs_path); 118 119 if (hid_id < 0) { 120 fprintf(stderr, "can not open HID device: %m\n"); 121 return 1; 122 } | 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 } |
123 args.hid = hid_id; | 115 skel->struct_ops.mouse_invert->hid_id = hid_id; |
124 | 116 |
125 attach_fd = bpf_program__fd(skel->progs.attach_prog); 126 if (attach_fd < 0) { 127 fprintf(stderr, "can't locate attach prog: %m\n"); | 117 err = hid_mouse__load(skel); 118 if (err < 0) { 119 fprintf(stderr, "can not load HID-BPF program: %m\n"); |
128 return 1; 129 } 130 | 120 return 1; 121 } 122 |
131 bpf_object__for_each_program(prog, *skel->skeleton->obj) { 132 /* ignore syscalls */ 133 if (bpf_program__get_type(prog) != BPF_PROG_TYPE_TRACING) 134 continue; 135 136 args.retval = -1; 137 args.prog_fd = bpf_program__fd(prog); 138 err = bpf_prog_test_run_opts(attach_fd, &tattr); 139 if (err) { 140 fprintf(stderr, "can't attach prog to hid device %d: %m (err: %d)\n", 141 hid_id, err); 142 return 1; 143 } | 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; |
144 } 145 146 signal(SIGINT, int_exit); 147 signal(SIGTERM, int_exit); 148 149 while (running) 150 sleep(1); 151 152 hid_mouse__destroy(skel); 153 154 return 0; 155} | 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} |