// SPDX-License-Identifier: GPL-2.01/*2* UHID Example3*4* Copyright (c) 2012-2013 David Herrmann <[email protected]>5*6* The code may be used by anyone for any purpose,7* and can serve as a starting point for developing8* applications using uhid.9*/1011/*12* UHID Example13* This example emulates a basic 3 buttons mouse with wheel over UHID. Run this14* program as root and then use the following keys to control the mouse:15* q: Quit the application16* 1: Toggle left button (down, up, ...)17* 2: Toggle right button18* 3: Toggle middle button19* a: Move mouse left20* d: Move mouse right21* w: Move mouse up22* s: Move mouse down23* r: Move wheel up24* f: Move wheel down25*26* Additionally to 3 button mouse, 3 keyboard LEDs are also supported (LED_NUML,27* LED_CAPSL and LED_SCROLLL). The device doesn't generate any related keyboard28* events, though. You need to manually write the EV_LED/LED_XY/1 activation29* input event to the evdev device to see it being sent to this device.30*31* If uhid is not available as /dev/uhid, then you can pass a different path as32* first argument.33* If <linux/uhid.h> is not installed in /usr, then compile this with:34* gcc -o ./uhid_test -Wall -I./include ./samples/uhid/uhid-example.c35* And ignore the warning about kernel headers. However, it is recommended to36* use the installed uhid.h if available.37*/3839#include <errno.h>40#include <fcntl.h>41#include <poll.h>42#include <stdbool.h>43#include <stdio.h>44#include <stdlib.h>45#include <string.h>46#include <termios.h>47#include <unistd.h>48#include <linux/uhid.h>4950/*51* HID Report Desciptor52* We emulate a basic 3 button mouse with wheel and 3 keyboard LEDs. This is53* the report-descriptor as the kernel will parse it:54*55* INPUT(1)[INPUT]56* Field(0)57* Physical(GenericDesktop.Pointer)58* Application(GenericDesktop.Mouse)59* Usage(3)60* Button.000161* Button.000262* Button.000363* Logical Minimum(0)64* Logical Maximum(1)65* Report Size(1)66* Report Count(3)67* Report Offset(0)68* Flags( Variable Absolute )69* Field(1)70* Physical(GenericDesktop.Pointer)71* Application(GenericDesktop.Mouse)72* Usage(3)73* GenericDesktop.X74* GenericDesktop.Y75* GenericDesktop.Wheel76* Logical Minimum(-128)77* Logical Maximum(127)78* Report Size(8)79* Report Count(3)80* Report Offset(8)81* Flags( Variable Relative )82* OUTPUT(2)[OUTPUT]83* Field(0)84* Application(GenericDesktop.Keyboard)85* Usage(3)86* LED.NumLock87* LED.CapsLock88* LED.ScrollLock89* Logical Minimum(0)90* Logical Maximum(1)91* Report Size(1)92* Report Count(3)93* Report Offset(0)94* Flags( Variable Absolute )95*96* This is the mapping that we expect:97* Button.0001 ---> Key.LeftBtn98* Button.0002 ---> Key.RightBtn99* Button.0003 ---> Key.MiddleBtn100* GenericDesktop.X ---> Relative.X101* GenericDesktop.Y ---> Relative.Y102* GenericDesktop.Wheel ---> Relative.Wheel103* LED.NumLock ---> LED.NumLock104* LED.CapsLock ---> LED.CapsLock105* LED.ScrollLock ---> LED.ScrollLock106*107* This information can be verified by reading /sys/kernel/debug/hid/<dev>/rdesc108* This file should print the same information as showed above.109*/110111static unsigned char rdesc[] = {1120x05, 0x01, /* USAGE_PAGE (Generic Desktop) */1130x09, 0x02, /* USAGE (Mouse) */1140xa1, 0x01, /* COLLECTION (Application) */1150x09, 0x01, /* USAGE (Pointer) */1160xa1, 0x00, /* COLLECTION (Physical) */1170x85, 0x01, /* REPORT_ID (1) */1180x05, 0x09, /* USAGE_PAGE (Button) */1190x19, 0x01, /* USAGE_MINIMUM (Button 1) */1200x29, 0x03, /* USAGE_MAXIMUM (Button 3) */1210x15, 0x00, /* LOGICAL_MINIMUM (0) */1220x25, 0x01, /* LOGICAL_MAXIMUM (1) */1230x95, 0x03, /* REPORT_COUNT (3) */1240x75, 0x01, /* REPORT_SIZE (1) */1250x81, 0x02, /* INPUT (Data,Var,Abs) */1260x95, 0x01, /* REPORT_COUNT (1) */1270x75, 0x05, /* REPORT_SIZE (5) */1280x81, 0x01, /* INPUT (Cnst,Var,Abs) */1290x05, 0x01, /* USAGE_PAGE (Generic Desktop) */1300x09, 0x30, /* USAGE (X) */1310x09, 0x31, /* USAGE (Y) */1320x09, 0x38, /* USAGE (WHEEL) */1330x15, 0x81, /* LOGICAL_MINIMUM (-127) */1340x25, 0x7f, /* LOGICAL_MAXIMUM (127) */1350x75, 0x08, /* REPORT_SIZE (8) */1360x95, 0x03, /* REPORT_COUNT (3) */1370x81, 0x06, /* INPUT (Data,Var,Rel) */1380xc0, /* END_COLLECTION */1390xc0, /* END_COLLECTION */1400x05, 0x01, /* USAGE_PAGE (Generic Desktop) */1410x09, 0x06, /* USAGE (Keyboard) */1420xa1, 0x01, /* COLLECTION (Application) */1430x85, 0x02, /* REPORT_ID (2) */1440x05, 0x08, /* USAGE_PAGE (Led) */1450x19, 0x01, /* USAGE_MINIMUM (1) */1460x29, 0x03, /* USAGE_MAXIMUM (3) */1470x15, 0x00, /* LOGICAL_MINIMUM (0) */1480x25, 0x01, /* LOGICAL_MAXIMUM (1) */1490x95, 0x03, /* REPORT_COUNT (3) */1500x75, 0x01, /* REPORT_SIZE (1) */1510x91, 0x02, /* Output (Data,Var,Abs) */1520x95, 0x01, /* REPORT_COUNT (1) */1530x75, 0x05, /* REPORT_SIZE (5) */1540x91, 0x01, /* Output (Cnst,Var,Abs) */1550xc0, /* END_COLLECTION */156};157158static int uhid_write(int fd, const struct uhid_event *ev)159{160ssize_t ret;161162ret = write(fd, ev, sizeof(*ev));163if (ret < 0) {164fprintf(stderr, "Cannot write to uhid: %m\n");165return -errno;166} else if (ret != sizeof(*ev)) {167fprintf(stderr, "Wrong size written to uhid: %zd != %zu\n",168ret, sizeof(ev));169return -EFAULT;170} else {171return 0;172}173}174175static int create(int fd)176{177struct uhid_event ev;178179memset(&ev, 0, sizeof(ev));180ev.type = UHID_CREATE;181strcpy((char*)ev.u.create.name, "test-uhid-device");182ev.u.create.rd_data = rdesc;183ev.u.create.rd_size = sizeof(rdesc);184ev.u.create.bus = BUS_USB;185ev.u.create.vendor = 0x15d9;186ev.u.create.product = 0x0a37;187ev.u.create.version = 0;188ev.u.create.country = 0;189190return uhid_write(fd, &ev);191}192193static void destroy(int fd)194{195struct uhid_event ev;196197memset(&ev, 0, sizeof(ev));198ev.type = UHID_DESTROY;199200uhid_write(fd, &ev);201}202203/* This parses raw output reports sent by the kernel to the device. A normal204* uhid program shouldn't do this but instead just forward the raw report.205* However, for ducomentational purposes, we try to detect LED events here and206* print debug messages for it. */207static void handle_output(struct uhid_event *ev)208{209/* LED messages are adverised via OUTPUT reports; ignore the rest */210if (ev->u.output.rtype != UHID_OUTPUT_REPORT)211return;212/* LED reports have length 2 bytes */213if (ev->u.output.size != 2)214return;215/* first byte is report-id which is 0x02 for LEDs in our rdesc */216if (ev->u.output.data[0] != 0x2)217return;218219/* print flags payload */220fprintf(stderr, "LED output report received with flags %x\n",221ev->u.output.data[1]);222}223224static int event(int fd)225{226struct uhid_event ev;227ssize_t ret;228229memset(&ev, 0, sizeof(ev));230ret = read(fd, &ev, sizeof(ev));231if (ret == 0) {232fprintf(stderr, "Read HUP on uhid-cdev\n");233return -EFAULT;234} else if (ret < 0) {235fprintf(stderr, "Cannot read uhid-cdev: %m\n");236return -errno;237} else if (ret != sizeof(ev)) {238fprintf(stderr, "Invalid size read from uhid-dev: %zd != %zu\n",239ret, sizeof(ev));240return -EFAULT;241}242243switch (ev.type) {244case UHID_START:245fprintf(stderr, "UHID_START from uhid-dev\n");246break;247case UHID_STOP:248fprintf(stderr, "UHID_STOP from uhid-dev\n");249break;250case UHID_OPEN:251fprintf(stderr, "UHID_OPEN from uhid-dev\n");252break;253case UHID_CLOSE:254fprintf(stderr, "UHID_CLOSE from uhid-dev\n");255break;256case UHID_OUTPUT:257fprintf(stderr, "UHID_OUTPUT from uhid-dev\n");258handle_output(&ev);259break;260case UHID_OUTPUT_EV:261fprintf(stderr, "UHID_OUTPUT_EV from uhid-dev\n");262break;263default:264fprintf(stderr, "Invalid event from uhid-dev: %u\n", ev.type);265}266267return 0;268}269270static bool btn1_down;271static bool btn2_down;272static bool btn3_down;273static signed char abs_hor;274static signed char abs_ver;275static signed char wheel;276277static int send_event(int fd)278{279struct uhid_event ev;280281memset(&ev, 0, sizeof(ev));282ev.type = UHID_INPUT;283ev.u.input.size = 5;284285ev.u.input.data[0] = 0x1;286if (btn1_down)287ev.u.input.data[1] |= 0x1;288if (btn2_down)289ev.u.input.data[1] |= 0x2;290if (btn3_down)291ev.u.input.data[1] |= 0x4;292293ev.u.input.data[2] = abs_hor;294ev.u.input.data[3] = abs_ver;295ev.u.input.data[4] = wheel;296297return uhid_write(fd, &ev);298}299300static int keyboard(int fd)301{302char buf[128];303ssize_t ret, i;304305ret = read(STDIN_FILENO, buf, sizeof(buf));306if (ret == 0) {307fprintf(stderr, "Read HUP on stdin\n");308return -EFAULT;309} else if (ret < 0) {310fprintf(stderr, "Cannot read stdin: %m\n");311return -errno;312}313314for (i = 0; i < ret; ++i) {315switch (buf[i]) {316case '1':317btn1_down = !btn1_down;318ret = send_event(fd);319if (ret)320return ret;321break;322case '2':323btn2_down = !btn2_down;324ret = send_event(fd);325if (ret)326return ret;327break;328case '3':329btn3_down = !btn3_down;330ret = send_event(fd);331if (ret)332return ret;333break;334case 'a':335abs_hor = -20;336ret = send_event(fd);337abs_hor = 0;338if (ret)339return ret;340break;341case 'd':342abs_hor = 20;343ret = send_event(fd);344abs_hor = 0;345if (ret)346return ret;347break;348case 'w':349abs_ver = -20;350ret = send_event(fd);351abs_ver = 0;352if (ret)353return ret;354break;355case 's':356abs_ver = 20;357ret = send_event(fd);358abs_ver = 0;359if (ret)360return ret;361break;362case 'r':363wheel = 1;364ret = send_event(fd);365wheel = 0;366if (ret)367return ret;368break;369case 'f':370wheel = -1;371ret = send_event(fd);372wheel = 0;373if (ret)374return ret;375break;376case 'q':377return -ECANCELED;378default:379fprintf(stderr, "Invalid input: %c\n", buf[i]);380}381}382383return 0;384}385386int main(int argc, char **argv)387{388int fd;389const char *path = "/dev/uhid";390struct pollfd pfds[2];391int ret;392struct termios state;393394ret = tcgetattr(STDIN_FILENO, &state);395if (ret) {396fprintf(stderr, "Cannot get tty state\n");397} else {398state.c_lflag &= ~ICANON;399state.c_cc[VMIN] = 1;400ret = tcsetattr(STDIN_FILENO, TCSANOW, &state);401if (ret)402fprintf(stderr, "Cannot set tty state\n");403}404405if (argc >= 2) {406if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {407fprintf(stderr, "Usage: %s [%s]\n", argv[0], path);408return EXIT_SUCCESS;409} else {410path = argv[1];411}412}413414fprintf(stderr, "Open uhid-cdev %s\n", path);415fd = open(path, O_RDWR | O_CLOEXEC);416if (fd < 0) {417fprintf(stderr, "Cannot open uhid-cdev %s: %m\n", path);418return EXIT_FAILURE;419}420421fprintf(stderr, "Create uhid device\n");422ret = create(fd);423if (ret) {424close(fd);425return EXIT_FAILURE;426}427428pfds[0].fd = STDIN_FILENO;429pfds[0].events = POLLIN;430pfds[1].fd = fd;431pfds[1].events = POLLIN;432433fprintf(stderr, "Press 'q' to quit...\n");434while (1) {435ret = poll(pfds, 2, -1);436if (ret < 0) {437fprintf(stderr, "Cannot poll for fds: %m\n");438break;439}440if (pfds[0].revents & POLLHUP) {441fprintf(stderr, "Received HUP on stdin\n");442break;443}444if (pfds[1].revents & POLLHUP) {445fprintf(stderr, "Received HUP on uhid-cdev\n");446break;447}448449if (pfds[0].revents & POLLIN) {450ret = keyboard(fd);451if (ret)452break;453}454if (pfds[1].revents & POLLIN) {455ret = event(fd);456if (ret)457break;458}459}460461fprintf(stderr, "Destroy uhid device\n");462destroy(fd);463return EXIT_SUCCESS;464}465466467