~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/hid/hid_mouse.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 }
139 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php