]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - samples/uhid/uhid-example.c
4 * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@gmail.com>
6 * The code may be used by anyone for any purpose,
7 * and can serve as a starting point for developing
8 * applications using uhid.
13 * This example emulates a basic 3 buttons mouse with wheel over UHID. Run this
14 * program as root and then use the following keys to control the mouse:
15 * q: Quit the application
16 * 1: Toggle left button (down, up, ...)
17 * 2: Toggle right button
18 * 3: Toggle middle button
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 keyboard
28 * events, though. You need to manually write the EV_LED/LED_XY/1 activation
29 * input event to the evdev device to see it being sent to this device.
31 * If uhid is not available as /dev/uhid, then you can pass a different path as
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.c
35 * And ignore the warning about kernel headers. However, it is recommended to
36 * use the installed uhid.h if available.
48 #include <linux/uhid.h>
51 * HID Report Desciptor
52 * We emulate a basic 3 button mouse with wheel and 3 keyboard LEDs. This is
53 * the report-descriptor as the kernel will parse it:
57 * Physical(GenericDesktop.Pointer)
58 * Application(GenericDesktop.Mouse)
68 * Flags( Variable Absolute )
70 * Physical(GenericDesktop.Pointer)
71 * Application(GenericDesktop.Mouse)
75 * GenericDesktop.Wheel
76 * Logical Minimum(-128)
77 * Logical Maximum(127)
81 * Flags( Variable Relative )
84 * Application(GenericDesktop.Keyboard)
94 * Flags( Variable Absolute )
96 * This is the mapping that we expect:
97 * Button.0001 ---> Key.LeftBtn
98 * Button.0002 ---> Key.RightBtn
99 * Button.0003 ---> Key.MiddleBtn
100 * GenericDesktop.X ---> Relative.X
101 * GenericDesktop.Y ---> Relative.Y
102 * GenericDesktop.Wheel ---> Relative.Wheel
103 * LED.NumLock ---> LED.NumLock
104 * LED.CapsLock ---> LED.CapsLock
105 * LED.ScrollLock ---> LED.ScrollLock
107 * This information can be verified by reading /sys/kernel/debug/hid/<dev>/rdesc
108 * This file should print the same information as showed above.
111 static unsigned char rdesc
[] = {
112 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
113 0x09, 0x02, /* USAGE (Mouse) */
114 0xa1, 0x01, /* COLLECTION (Application) */
115 0x09, 0x01, /* USAGE (Pointer) */
116 0xa1, 0x00, /* COLLECTION (Physical) */
117 0x85, 0x01, /* REPORT_ID (1) */
118 0x05, 0x09, /* USAGE_PAGE (Button) */
119 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
120 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
121 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
122 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
123 0x95, 0x03, /* REPORT_COUNT (3) */
124 0x75, 0x01, /* REPORT_SIZE (1) */
125 0x81, 0x02, /* INPUT (Data,Var,Abs) */
126 0x95, 0x01, /* REPORT_COUNT (1) */
127 0x75, 0x05, /* REPORT_SIZE (5) */
128 0x81, 0x01, /* INPUT (Cnst,Var,Abs) */
129 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
130 0x09, 0x30, /* USAGE (X) */
131 0x09, 0x31, /* USAGE (Y) */
132 0x09, 0x38, /* USAGE (WHEEL) */
133 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
134 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
135 0x75, 0x08, /* REPORT_SIZE (8) */
136 0x95, 0x03, /* REPORT_COUNT (3) */
137 0x81, 0x06, /* INPUT (Data,Var,Rel) */
138 0xc0, /* END_COLLECTION */
139 0xc0, /* END_COLLECTION */
140 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
141 0x09, 0x06, /* USAGE (Keyboard) */
142 0xa1, 0x01, /* COLLECTION (Application) */
143 0x85, 0x02, /* REPORT_ID (2) */
144 0x05, 0x08, /* USAGE_PAGE (Led) */
145 0x19, 0x01, /* USAGE_MINIMUM (1) */
146 0x29, 0x03, /* USAGE_MAXIMUM (3) */
147 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
148 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
149 0x95, 0x03, /* REPORT_COUNT (3) */
150 0x75, 0x01, /* REPORT_SIZE (1) */
151 0x91, 0x02, /* Output (Data,Var,Abs) */
152 0x95, 0x01, /* REPORT_COUNT (1) */
153 0x75, 0x05, /* REPORT_SIZE (5) */
154 0x91, 0x01, /* Output (Cnst,Var,Abs) */
155 0xc0, /* END_COLLECTION */
158 static int uhid_write(int fd
, const struct uhid_event
*ev
)
162 ret
= write(fd
, ev
, sizeof(*ev
));
164 fprintf(stderr
, "Cannot write to uhid: %m\n");
166 } else if (ret
!= sizeof(*ev
)) {
167 fprintf(stderr
, "Wrong size written to uhid: %ld != %lu\n",
175 static int create(int fd
)
177 struct uhid_event ev
;
179 memset(&ev
, 0, sizeof(ev
));
180 ev
.type
= UHID_CREATE
;
181 strcpy((char*)ev
.u
.create
.name
, "test-uhid-device");
182 ev
.u
.create
.rd_data
= rdesc
;
183 ev
.u
.create
.rd_size
= sizeof(rdesc
);
184 ev
.u
.create
.bus
= BUS_USB
;
185 ev
.u
.create
.vendor
= 0x15d9;
186 ev
.u
.create
.product
= 0x0a37;
187 ev
.u
.create
.version
= 0;
188 ev
.u
.create
.country
= 0;
190 return uhid_write(fd
, &ev
);
193 static void destroy(int fd
)
195 struct uhid_event ev
;
197 memset(&ev
, 0, sizeof(ev
));
198 ev
.type
= UHID_DESTROY
;
203 /* This parses raw output reports sent by the kernel to the device. A normal
204 * 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 and
206 * print debug messages for it. */
207 static void handle_output(struct uhid_event
*ev
)
209 /* LED messages are adverised via OUTPUT reports; ignore the rest */
210 if (ev
->u
.output
.rtype
!= UHID_OUTPUT_REPORT
)
212 /* LED reports have length 2 bytes */
213 if (ev
->u
.output
.size
!= 2)
215 /* first byte is report-id which is 0x02 for LEDs in our rdesc */
216 if (ev
->u
.output
.data
[0] != 0x2)
219 /* print flags payload */
220 fprintf(stderr
, "LED output report received with flags %x\n",
221 ev
->u
.output
.data
[1]);
224 static int event(int fd
)
226 struct uhid_event ev
;
229 memset(&ev
, 0, sizeof(ev
));
230 ret
= read(fd
, &ev
, sizeof(ev
));
232 fprintf(stderr
, "Read HUP on uhid-cdev\n");
234 } else if (ret
< 0) {
235 fprintf(stderr
, "Cannot read uhid-cdev: %m\n");
237 } else if (ret
!= sizeof(ev
)) {
238 fprintf(stderr
, "Invalid size read from uhid-dev: %ld != %lu\n",
245 fprintf(stderr
, "UHID_START from uhid-dev\n");
248 fprintf(stderr
, "UHID_STOP from uhid-dev\n");
251 fprintf(stderr
, "UHID_OPEN from uhid-dev\n");
254 fprintf(stderr
, "UHID_CLOSE from uhid-dev\n");
257 fprintf(stderr
, "UHID_OUTPUT from uhid-dev\n");
261 fprintf(stderr
, "UHID_OUTPUT_EV from uhid-dev\n");
264 fprintf(stderr
, "Invalid event from uhid-dev: %u\n", ev
.type
);
270 static bool btn1_down
;
271 static bool btn2_down
;
272 static bool btn3_down
;
273 static signed char abs_hor
;
274 static signed char abs_ver
;
275 static signed char wheel
;
277 static int send_event(int fd
)
279 struct uhid_event ev
;
281 memset(&ev
, 0, sizeof(ev
));
282 ev
.type
= UHID_INPUT
;
285 ev
.u
.input
.data
[0] = 0x1;
287 ev
.u
.input
.data
[1] |= 0x1;
289 ev
.u
.input
.data
[1] |= 0x2;
291 ev
.u
.input
.data
[1] |= 0x4;
293 ev
.u
.input
.data
[2] = abs_hor
;
294 ev
.u
.input
.data
[3] = abs_ver
;
295 ev
.u
.input
.data
[4] = wheel
;
297 return uhid_write(fd
, &ev
);
300 static int keyboard(int fd
)
305 ret
= read(STDIN_FILENO
, buf
, sizeof(buf
));
307 fprintf(stderr
, "Read HUP on stdin\n");
309 } else if (ret
< 0) {
310 fprintf(stderr
, "Cannot read stdin: %m\n");
314 for (i
= 0; i
< ret
; ++i
) {
317 btn1_down
= !btn1_down
;
318 ret
= send_event(fd
);
323 btn2_down
= !btn2_down
;
324 ret
= send_event(fd
);
329 btn3_down
= !btn3_down
;
330 ret
= send_event(fd
);
336 ret
= send_event(fd
);
343 ret
= send_event(fd
);
350 ret
= send_event(fd
);
357 ret
= send_event(fd
);
364 ret
= send_event(fd
);
371 ret
= send_event(fd
);
379 fprintf(stderr
, "Invalid input: %c\n", buf
[i
]);
386 int main(int argc
, char **argv
)
389 const char *path
= "/dev/uhid";
390 struct pollfd pfds
[2];
392 struct termios state
;
394 ret
= tcgetattr(STDIN_FILENO
, &state
);
396 fprintf(stderr
, "Cannot get tty state\n");
398 state
.c_lflag
&= ~ICANON
;
399 state
.c_cc
[VMIN
] = 1;
400 ret
= tcsetattr(STDIN_FILENO
, TCSANOW
, &state
);
402 fprintf(stderr
, "Cannot set tty state\n");
406 if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
407 fprintf(stderr
, "Usage: %s [%s]\n", argv
[0], path
);
414 fprintf(stderr
, "Open uhid-cdev %s\n", path
);
415 fd
= open(path
, O_RDWR
| O_CLOEXEC
);
417 fprintf(stderr
, "Cannot open uhid-cdev %s: %m\n", path
);
421 fprintf(stderr
, "Create uhid device\n");
428 pfds
[0].fd
= STDIN_FILENO
;
429 pfds
[0].events
= POLLIN
;
431 pfds
[1].events
= POLLIN
;
433 fprintf(stderr
, "Press 'q' to quit...\n");
435 ret
= poll(pfds
, 2, -1);
437 fprintf(stderr
, "Cannot poll for fds: %m\n");
440 if (pfds
[0].revents
& POLLHUP
) {
441 fprintf(stderr
, "Received HUP on stdin\n");
444 if (pfds
[1].revents
& POLLHUP
) {
445 fprintf(stderr
, "Received HUP on uhid-cdev\n");
449 if (pfds
[0].revents
& POLLIN
) {
454 if (pfds
[1].revents
& POLLIN
) {
461 fprintf(stderr
, "Destroy uhid device\n");