xref: /openbmc/qemu/hw/input/hid.c (revision 761d524d)
1 /*
2  * QEMU HID devices
3  *
4  * Copyright (c) 2005 Fabrice Bellard
5  * Copyright (c) 2007 OpenMoko, Inc.  (andrew@openedhand.com)
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 #include "hw/hw.h"
26 #include "ui/console.h"
27 #include "qemu/timer.h"
28 #include "hw/input/hid.h"
29 
30 #define HID_USAGE_ERROR_ROLLOVER        0x01
31 #define HID_USAGE_POSTFAIL              0x02
32 #define HID_USAGE_ERROR_UNDEFINED       0x03
33 
34 /* Indices are QEMU keycodes, values are from HID Usage Table.  Indices
35  * above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d.  */
36 static const uint8_t hid_usage_keys[0x100] = {
37     0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
38     0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b,
39     0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c,
40     0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16,
41     0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33,
42     0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19,
43     0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55,
44     0xe2, 0x2c, 0x32, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
45     0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f,
46     0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59,
47     0x5a, 0x5b, 0x62, 0x63, 0x00, 0x00, 0x00, 0x44,
48     0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
49     0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00,
50     0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
51     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52     0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65,
53 
54     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57     0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
58     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60     0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
61     0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62     0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x4a,
63     0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
64     0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
65     0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, 0x00, 0x00,
66     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 };
71 
72 bool hid_has_events(HIDState *hs)
73 {
74     return hs->n > 0 || hs->idle_pending;
75 }
76 
77 static void hid_idle_timer(void *opaque)
78 {
79     HIDState *hs = opaque;
80 
81     hs->idle_pending = true;
82     hs->event(hs);
83 }
84 
85 static void hid_del_idle_timer(HIDState *hs)
86 {
87     if (hs->idle_timer) {
88         qemu_del_timer(hs->idle_timer);
89         qemu_free_timer(hs->idle_timer);
90         hs->idle_timer = NULL;
91     }
92 }
93 
94 void hid_set_next_idle(HIDState *hs)
95 {
96     if (hs->idle) {
97         uint64_t expire_time = qemu_get_clock_ns(vm_clock) +
98                                get_ticks_per_sec() * hs->idle * 4 / 1000;
99         if (!hs->idle_timer) {
100             hs->idle_timer = qemu_new_timer_ns(vm_clock, hid_idle_timer, hs);
101         }
102         qemu_mod_timer_ns(hs->idle_timer, expire_time);
103     } else {
104         hid_del_idle_timer(hs);
105     }
106 }
107 
108 static void hid_pointer_event_clear(HIDPointerEvent *e, int buttons)
109 {
110     e->xdx = e->ydy = e->dz = 0;
111     e->buttons_state = buttons;
112 }
113 
114 static void hid_pointer_event_combine(HIDPointerEvent *e, int xyrel,
115                                       int x1, int y1, int z1) {
116     if (xyrel) {
117         e->xdx += x1;
118         e->ydy += y1;
119     } else {
120         e->xdx = x1;
121         e->ydy = y1;
122         /* Windows drivers do not like the 0/0 position and ignore such
123          * events. */
124         if (!(x1 | y1)) {
125             e->xdx = 1;
126         }
127     }
128     e->dz += z1;
129 }
130 
131 static void hid_pointer_event(void *opaque,
132                               int x1, int y1, int z1, int buttons_state)
133 {
134     HIDState *hs = opaque;
135     unsigned use_slot = (hs->head + hs->n - 1) & QUEUE_MASK;
136     unsigned previous_slot = (use_slot - 1) & QUEUE_MASK;
137 
138     /* We combine events where feasible to keep the queue small.  We shouldn't
139      * combine anything with the first event of a particular button state, as
140      * that would change the location of the button state change.  When the
141      * queue is empty, a second event is needed because we don't know if
142      * the first event changed the button state.  */
143     if (hs->n == QUEUE_LENGTH) {
144         /* Queue full.  Discard old button state, combine motion normally.  */
145         hs->ptr.queue[use_slot].buttons_state = buttons_state;
146     } else if (hs->n < 2 ||
147                hs->ptr.queue[use_slot].buttons_state != buttons_state ||
148                hs->ptr.queue[previous_slot].buttons_state !=
149                hs->ptr.queue[use_slot].buttons_state) {
150         /* Cannot or should not combine, so add an empty item to the queue.  */
151         QUEUE_INCR(use_slot);
152         hs->n++;
153         hid_pointer_event_clear(&hs->ptr.queue[use_slot], buttons_state);
154     }
155     hid_pointer_event_combine(&hs->ptr.queue[use_slot],
156                               hs->kind == HID_MOUSE,
157                               x1, y1, z1);
158     hs->event(hs);
159 }
160 
161 static void hid_keyboard_event(void *opaque, int keycode)
162 {
163     HIDState *hs = opaque;
164     int slot;
165 
166     if (hs->n == QUEUE_LENGTH) {
167         fprintf(stderr, "usb-kbd: warning: key event queue full\n");
168         return;
169     }
170     slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
171     hs->kbd.keycodes[slot] = keycode;
172     hs->event(hs);
173 }
174 
175 static void hid_keyboard_process_keycode(HIDState *hs)
176 {
177     uint8_t hid_code, key;
178     int i, keycode, slot;
179 
180     if (hs->n == 0) {
181         return;
182     }
183     slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
184     keycode = hs->kbd.keycodes[slot];
185 
186     key = keycode & 0x7f;
187     hid_code = hid_usage_keys[key | ((hs->kbd.modifiers >> 1) & (1 << 7))];
188     hs->kbd.modifiers &= ~(1 << 8);
189 
190     switch (hid_code) {
191     case 0x00:
192         return;
193 
194     case 0xe0:
195         if (hs->kbd.modifiers & (1 << 9)) {
196             hs->kbd.modifiers ^= 3 << 8;
197             return;
198         }
199     case 0xe1 ... 0xe7:
200         if (keycode & (1 << 7)) {
201             hs->kbd.modifiers &= ~(1 << (hid_code & 0x0f));
202             return;
203         }
204     case 0xe8 ... 0xef:
205         hs->kbd.modifiers |= 1 << (hid_code & 0x0f);
206         return;
207     }
208 
209     if (keycode & (1 << 7)) {
210         for (i = hs->kbd.keys - 1; i >= 0; i--) {
211             if (hs->kbd.key[i] == hid_code) {
212                 hs->kbd.key[i] = hs->kbd.key[-- hs->kbd.keys];
213                 hs->kbd.key[hs->kbd.keys] = 0x00;
214                 break;
215             }
216         }
217         if (i < 0) {
218             return;
219         }
220     } else {
221         for (i = hs->kbd.keys - 1; i >= 0; i--) {
222             if (hs->kbd.key[i] == hid_code) {
223                 break;
224             }
225         }
226         if (i < 0) {
227             if (hs->kbd.keys < sizeof(hs->kbd.key)) {
228                 hs->kbd.key[hs->kbd.keys++] = hid_code;
229             }
230         } else {
231             return;
232         }
233     }
234 }
235 
236 static inline int int_clamp(int val, int vmin, int vmax)
237 {
238     if (val < vmin) {
239         return vmin;
240     } else if (val > vmax) {
241         return vmax;
242     } else {
243         return val;
244     }
245 }
246 
247 void hid_pointer_activate(HIDState *hs)
248 {
249     if (!hs->ptr.mouse_grabbed) {
250         qemu_activate_mouse_event_handler(hs->ptr.eh_entry);
251         hs->ptr.mouse_grabbed = 1;
252     }
253 }
254 
255 int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
256 {
257     int dx, dy, dz, b, l;
258     int index;
259     HIDPointerEvent *e;
260 
261     hs->idle_pending = false;
262 
263     hid_pointer_activate(hs);
264 
265     /* When the buffer is empty, return the last event.  Relative
266        movements will all be zero.  */
267     index = (hs->n ? hs->head : hs->head - 1);
268     e = &hs->ptr.queue[index & QUEUE_MASK];
269 
270     if (hs->kind == HID_MOUSE) {
271         dx = int_clamp(e->xdx, -127, 127);
272         dy = int_clamp(e->ydy, -127, 127);
273         e->xdx -= dx;
274         e->ydy -= dy;
275     } else {
276         dx = e->xdx;
277         dy = e->ydy;
278     }
279     dz = int_clamp(e->dz, -127, 127);
280     e->dz -= dz;
281 
282     b = 0;
283     if (e->buttons_state & MOUSE_EVENT_LBUTTON) {
284         b |= 0x01;
285     }
286     if (e->buttons_state & MOUSE_EVENT_RBUTTON) {
287         b |= 0x02;
288     }
289     if (e->buttons_state & MOUSE_EVENT_MBUTTON) {
290         b |= 0x04;
291     }
292 
293     if (hs->n &&
294         !e->dz &&
295         (hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) {
296         /* that deals with this event */
297         QUEUE_INCR(hs->head);
298         hs->n--;
299     }
300 
301     /* Appears we have to invert the wheel direction */
302     dz = 0 - dz;
303     l = 0;
304     switch (hs->kind) {
305     case HID_MOUSE:
306         if (len > l) {
307             buf[l++] = b;
308         }
309         if (len > l) {
310             buf[l++] = dx;
311         }
312         if (len > l) {
313             buf[l++] = dy;
314         }
315         if (len > l) {
316             buf[l++] = dz;
317         }
318         break;
319 
320     case HID_TABLET:
321         if (len > l) {
322             buf[l++] = b;
323         }
324         if (len > l) {
325             buf[l++] = dx & 0xff;
326         }
327         if (len > l) {
328             buf[l++] = dx >> 8;
329         }
330         if (len > l) {
331             buf[l++] = dy & 0xff;
332         }
333         if (len > l) {
334             buf[l++] = dy >> 8;
335         }
336         if (len > l) {
337             buf[l++] = dz;
338         }
339         break;
340 
341     default:
342         abort();
343     }
344 
345     return l;
346 }
347 
348 int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
349 {
350     hs->idle_pending = false;
351 
352     if (len < 2) {
353         return 0;
354     }
355 
356     hid_keyboard_process_keycode(hs);
357 
358     buf[0] = hs->kbd.modifiers & 0xff;
359     buf[1] = 0;
360     if (hs->kbd.keys > 6) {
361         memset(buf + 2, HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
362     } else {
363         memcpy(buf + 2, hs->kbd.key, MIN(8, len) - 2);
364     }
365 
366     return MIN(8, len);
367 }
368 
369 int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len)
370 {
371     if (len > 0) {
372         int ledstate = 0;
373         /* 0x01: Num Lock LED
374          * 0x02: Caps Lock LED
375          * 0x04: Scroll Lock LED
376          * 0x08: Compose LED
377          * 0x10: Kana LED */
378         hs->kbd.leds = buf[0];
379         if (hs->kbd.leds & 0x04) {
380             ledstate |= QEMU_SCROLL_LOCK_LED;
381         }
382         if (hs->kbd.leds & 0x01) {
383             ledstate |= QEMU_NUM_LOCK_LED;
384         }
385         if (hs->kbd.leds & 0x02) {
386             ledstate |= QEMU_CAPS_LOCK_LED;
387         }
388         kbd_put_ledstate(ledstate);
389     }
390     return 0;
391 }
392 
393 void hid_reset(HIDState *hs)
394 {
395     switch (hs->kind) {
396     case HID_KEYBOARD:
397         memset(hs->kbd.keycodes, 0, sizeof(hs->kbd.keycodes));
398         memset(hs->kbd.key, 0, sizeof(hs->kbd.key));
399         hs->kbd.keys = 0;
400         break;
401     case HID_MOUSE:
402     case HID_TABLET:
403         memset(hs->ptr.queue, 0, sizeof(hs->ptr.queue));
404         break;
405     }
406     hs->head = 0;
407     hs->n = 0;
408     hs->protocol = 1;
409     hs->idle = 0;
410     hs->idle_pending = false;
411     hid_del_idle_timer(hs);
412 }
413 
414 void hid_free(HIDState *hs)
415 {
416     switch (hs->kind) {
417     case HID_KEYBOARD:
418         qemu_remove_kbd_event_handler(hs->kbd.eh_entry);
419         break;
420     case HID_MOUSE:
421     case HID_TABLET:
422         qemu_remove_mouse_event_handler(hs->ptr.eh_entry);
423         break;
424     }
425     hid_del_idle_timer(hs);
426 }
427 
428 void hid_init(HIDState *hs, int kind, HIDEventFunc event)
429 {
430     hs->kind = kind;
431     hs->event = event;
432 
433     if (hs->kind == HID_KEYBOARD) {
434         hs->kbd.eh_entry = qemu_add_kbd_event_handler(hid_keyboard_event, hs);
435     } else if (hs->kind == HID_MOUSE) {
436         hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
437                                                         0, "QEMU HID Mouse");
438     } else if (hs->kind == HID_TABLET) {
439         hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
440                                                         1, "QEMU HID Tablet");
441     }
442 }
443 
444 static int hid_post_load(void *opaque, int version_id)
445 {
446     HIDState *s = opaque;
447 
448     hid_set_next_idle(s);
449     return 0;
450 }
451 
452 static const VMStateDescription vmstate_hid_ptr_queue = {
453     .name = "HIDPointerEventQueue",
454     .version_id = 1,
455     .minimum_version_id = 1,
456     .fields = (VMStateField[]) {
457         VMSTATE_INT32(xdx, HIDPointerEvent),
458         VMSTATE_INT32(ydy, HIDPointerEvent),
459         VMSTATE_INT32(dz, HIDPointerEvent),
460         VMSTATE_INT32(buttons_state, HIDPointerEvent),
461         VMSTATE_END_OF_LIST()
462     }
463 };
464 
465 const VMStateDescription vmstate_hid_ptr_device = {
466     .name = "HIDPointerDevice",
467     .version_id = 1,
468     .minimum_version_id = 1,
469     .post_load = hid_post_load,
470     .fields = (VMStateField[]) {
471         VMSTATE_STRUCT_ARRAY(ptr.queue, HIDState, QUEUE_LENGTH, 0,
472                              vmstate_hid_ptr_queue, HIDPointerEvent),
473         VMSTATE_UINT32(head, HIDState),
474         VMSTATE_UINT32(n, HIDState),
475         VMSTATE_INT32(protocol, HIDState),
476         VMSTATE_UINT8(idle, HIDState),
477         VMSTATE_END_OF_LIST(),
478     }
479 };
480 
481 const VMStateDescription vmstate_hid_keyboard_device = {
482     .name = "HIDKeyboardDevice",
483     .version_id = 1,
484     .minimum_version_id = 1,
485     .post_load = hid_post_load,
486     .fields = (VMStateField[]) {
487         VMSTATE_UINT32_ARRAY(kbd.keycodes, HIDState, QUEUE_LENGTH),
488         VMSTATE_UINT32(head, HIDState),
489         VMSTATE_UINT32(n, HIDState),
490         VMSTATE_UINT16(kbd.modifiers, HIDState),
491         VMSTATE_UINT8(kbd.leds, HIDState),
492         VMSTATE_UINT8_ARRAY(kbd.key, HIDState, 16),
493         VMSTATE_INT32(kbd.keys, HIDState),
494         VMSTATE_INT32(protocol, HIDState),
495         VMSTATE_UINT8(idle, HIDState),
496         VMSTATE_END_OF_LIST(),
497     }
498 };
499