1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
7 * The code may be used by anyone for any purpose,
8 * and can serve as a starting point for developing
9 * applications using uhid.
14 * This example emulates a basic 3 buttons mouse with wheel over UHID. Run this
15 * program as root and then use the following keys to control the mouse:
16 * q: Quit the application
17 * 1: Toggle left button (down, up, ...)
18 * 2: Toggle right button
19 * 3: Toggle middle button
27 * Additionally to 3 button mouse, 3 keyboard LEDs are also supported (LED_NUML,
28 * LED_CAPSL and LED_SCROLLL). The device doesn't generate any related keyboard
29 * events, though. You need to manually write the EV_LED/LED_XY/1 activation
30 * input event to the evdev device to see it being sent to this device.
32 * If uhid is not available as /dev/uhid, then you can pass a different path as
34 * If <linux/uhid.h> is not installed in /usr, then compile this with:
35 * gcc -o ./uhid_test -Wall -I./include ./samples/uhid/uhid-example.c
36 * And ignore the warning about kernel headers. However, it is recommended to
37 * use the installed uhid.h if available.
49 #include <linux/uhid.h>
52 * HID Report Desciptor
53 * We emulate a basic 3 button mouse with wheel and 3 keyboard LEDs. This is
54 * the report-descriptor as the kernel will parse it:
58 * Physical(GenericDesktop.Pointer)
59 * Application(GenericDesktop.Mouse)
69 * Flags( Variable Absolute )
71 * Physical(GenericDesktop.Pointer)
72 * Application(GenericDesktop.Mouse)
76 * GenericDesktop.Wheel
77 * Logical Minimum(-128)
78 * Logical Maximum(127)
82 * Flags( Variable Relative )
85 * Application(GenericDesktop.Keyboard)
95 * Flags( Variable Absolute )
97 * This is the mapping that we expect:
98 * Button.0001 ---> Key.LeftBtn
99 * Button.0002 ---> Key.RightBtn
100 * Button.0003 ---> Key.MiddleBtn
101 * GenericDesktop.X ---> Relative.X
102 * GenericDesktop.Y ---> Relative.Y
103 * GenericDesktop.Wheel ---> Relative.Wheel
104 * LED.NumLock ---> LED.NumLock
105 * LED.CapsLock ---> LED.CapsLock
106 * LED.ScrollLock ---> LED.ScrollLock
108 * This information can be verified by reading /sys/kernel/debug/hid/<dev>/rdesc
109 * This file should print the same information as showed above.
112 static unsigned char rdesc
[] = {
113 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
114 0x09, 0x02, /* USAGE (Mouse) */
115 0xa1, 0x01, /* COLLECTION (Application) */
116 0x09, 0x01, /* USAGE (Pointer) */
117 0xa1, 0x00, /* COLLECTION (Physical) */
118 0x85, 0x01, /* REPORT_ID (1) */
119 0x05, 0x09, /* USAGE_PAGE (Button) */
120 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
121 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
122 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
123 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
124 0x95, 0x03, /* REPORT_COUNT (3) */
125 0x75, 0x01, /* REPORT_SIZE (1) */
126 0x81, 0x02, /* INPUT (Data,Var,Abs) */
127 0x95, 0x01, /* REPORT_COUNT (1) */
128 0x75, 0x05, /* REPORT_SIZE (5) */
129 0x81, 0x01, /* INPUT (Cnst,Var,Abs) */
130 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
131 0x09, 0x30, /* USAGE (X) */
132 0x09, 0x31, /* USAGE (Y) */
133 0x09, 0x38, /* USAGE (WHEEL) */
134 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
135 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
136 0x75, 0x08, /* REPORT_SIZE (8) */
137 0x95, 0x03, /* REPORT_COUNT (3) */
138 0x81, 0x06, /* INPUT (Data,Var,Rel) */
139 0xc0, /* END_COLLECTION */
140 0xc0, /* END_COLLECTION */
141 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
142 0x09, 0x06, /* USAGE (Keyboard) */
143 0xa1, 0x01, /* COLLECTION (Application) */
144 0x85, 0x02, /* REPORT_ID (2) */
145 0x05, 0x08, /* USAGE_PAGE (Led) */
146 0x19, 0x01, /* USAGE_MINIMUM (1) */
147 0x29, 0x03, /* USAGE_MAXIMUM (3) */
148 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
149 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
150 0x95, 0x03, /* REPORT_COUNT (3) */
151 0x75, 0x01, /* REPORT_SIZE (1) */
152 0x91, 0x02, /* Output (Data,Var,Abs) */
153 0x95, 0x01, /* REPORT_COUNT (1) */
154 0x75, 0x05, /* REPORT_SIZE (5) */
155 0x91, 0x01, /* Output (Cnst,Var,Abs) */
156 0xc0, /* END_COLLECTION */
159 static int uhid_write(int fd
, const struct uhid_event
*ev
)
163 ret
= write(fd
, ev
, sizeof(*ev
));
165 fprintf(stderr
, "Cannot write to uhid: %m\n");
167 } else if (ret
!= sizeof(*ev
)) {
168 fprintf(stderr
, "Wrong size written to uhid: %zd != %zu\n",
176 static int create(int fd
)
178 struct uhid_event ev
;
180 memset(&ev
, 0, sizeof(ev
));
181 ev
.type
= UHID_CREATE
;
182 strcpy((char*)ev
.u
.create
.name
, "test-uhid-device");
183 ev
.u
.create
.rd_data
= rdesc
;
184 ev
.u
.create
.rd_size
= sizeof(rdesc
);
185 ev
.u
.create
.bus
= BUS_USB
;
186 ev
.u
.create
.vendor
= 0x15d9;
187 ev
.u
.create
.product
= 0x0a37;
188 ev
.u
.create
.version
= 0;
189 ev
.u
.create
.country
= 0;
191 return uhid_write(fd
, &ev
);
194 static void destroy(int fd
)
196 struct uhid_event ev
;
198 memset(&ev
, 0, sizeof(ev
));
199 ev
.type
= UHID_DESTROY
;
204 /* This parses raw output reports sent by the kernel to the device. A normal
205 * uhid program shouldn't do this but instead just forward the raw report.
206 * However, for ducomentational purposes, we try to detect LED events here and
207 * print debug messages for it. */
208 static void handle_output(struct uhid_event
*ev
)
210 /* LED messages are adverised via OUTPUT reports; ignore the rest */
211 if (ev
->u
.output
.rtype
!= UHID_OUTPUT_REPORT
)
213 /* LED reports have length 2 bytes */
214 if (ev
->u
.output
.size
!= 2)
216 /* first byte is report-id which is 0x02 for LEDs in our rdesc */
217 if (ev
->u
.output
.data
[0] != 0x2)
220 /* print flags payload */
221 fprintf(stderr
, "LED output report received with flags %x\n",
222 ev
->u
.output
.data
[1]);
225 static int event(int fd
)
227 struct uhid_event ev
;
230 memset(&ev
, 0, sizeof(ev
));
231 ret
= read(fd
, &ev
, sizeof(ev
));
233 fprintf(stderr
, "Read HUP on uhid-cdev\n");
235 } else if (ret
< 0) {
236 fprintf(stderr
, "Cannot read uhid-cdev: %m\n");
238 } else if (ret
!= sizeof(ev
)) {
239 fprintf(stderr
, "Invalid size read from uhid-dev: %zd != %zu\n",
246 fprintf(stderr
, "UHID_START from uhid-dev\n");
249 fprintf(stderr
, "UHID_STOP from uhid-dev\n");
252 fprintf(stderr
, "UHID_OPEN from uhid-dev\n");
255 fprintf(stderr
, "UHID_CLOSE from uhid-dev\n");
258 fprintf(stderr
, "UHID_OUTPUT from uhid-dev\n");
262 fprintf(stderr
, "UHID_OUTPUT_EV from uhid-dev\n");
265 fprintf(stderr
, "Invalid event from uhid-dev: %u\n", ev
.type
);
271 static bool btn1_down
;
272 static bool btn2_down
;
273 static bool btn3_down
;
274 static signed char abs_hor
;
275 static signed char abs_ver
;
276 static signed char wheel
;
278 static int send_event(int fd
)
280 struct uhid_event ev
;
282 memset(&ev
, 0, sizeof(ev
));
283 ev
.type
= UHID_INPUT
;
286 ev
.u
.input
.data
[0] = 0x1;
288 ev
.u
.input
.data
[1] |= 0x1;
290 ev
.u
.input
.data
[1] |= 0x2;
292 ev
.u
.input
.data
[1] |= 0x4;
294 ev
.u
.input
.data
[2] = abs_hor
;
295 ev
.u
.input
.data
[3] = abs_ver
;
296 ev
.u
.input
.data
[4] = wheel
;
298 return uhid_write(fd
, &ev
);
301 static int keyboard(int fd
)
306 ret
= read(STDIN_FILENO
, buf
, sizeof(buf
));
308 fprintf(stderr
, "Read HUP on stdin\n");
310 } else if (ret
< 0) {
311 fprintf(stderr
, "Cannot read stdin: %m\n");
315 for (i
= 0; i
< ret
; ++i
) {
318 btn1_down
= !btn1_down
;
319 ret
= send_event(fd
);
324 btn2_down
= !btn2_down
;
325 ret
= send_event(fd
);
330 btn3_down
= !btn3_down
;
331 ret
= send_event(fd
);
337 ret
= send_event(fd
);
344 ret
= send_event(fd
);
351 ret
= send_event(fd
);
358 ret
= send_event(fd
);
365 ret
= send_event(fd
);
372 ret
= send_event(fd
);
380 fprintf(stderr
, "Invalid input: %c\n", buf
[i
]);
387 int main(int argc
, char **argv
)
390 const char *path
= "/dev/uhid";
391 struct pollfd pfds
[2];
393 struct termios state
;
395 ret
= tcgetattr(STDIN_FILENO
, &state
);
397 fprintf(stderr
, "Cannot get tty state\n");
399 state
.c_lflag
&= ~ICANON
;
400 state
.c_cc
[VMIN
] = 1;
401 ret
= tcsetattr(STDIN_FILENO
, TCSANOW
, &state
);
403 fprintf(stderr
, "Cannot set tty state\n");
407 if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
408 fprintf(stderr
, "Usage: %s [%s]\n", argv
[0], path
);
415 fprintf(stderr
, "Open uhid-cdev %s\n", path
);
416 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
418 fprintf(stderr
, "Cannot open uhid-cdev %s: %m\n", path
);
422 fprintf(stderr
, "Create uhid device\n");
429 pfds
[0].fd
= STDIN_FILENO
;
430 pfds
[0].events
= POLLIN
;
432 pfds
[1].events
= POLLIN
;
434 fprintf(stderr
, "Press 'q' to quit...\n");
436 ret
= poll(pfds
, 2, -1);
438 fprintf(stderr
, "Cannot poll for fds: %m\n");
441 if (pfds
[0].revents
& POLLHUP
) {
442 fprintf(stderr
, "Received HUP on stdin\n");
445 if (pfds
[1].revents
& POLLHUP
) {
446 fprintf(stderr
, "Received HUP on uhid-cdev\n");
450 if (pfds
[0].revents
& POLLIN
) {
455 if (pfds
[1].revents
& POLLIN
) {
462 fprintf(stderr
, "Destroy uhid device\n");