xref: /qemu/hw/input/hid.c (revision 1ff5eedd1d0facf94b2f272058b83856b361b079)
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         timer_del(hs->idle_timer);
89         timer_free(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_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
98                                get_ticks_per_sec() * hs->idle * 4 / 1000;
99         if (!hs->idle_timer) {
100             hs->idle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, hid_idle_timer, hs);
101         }
102         timer_mod_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(DeviceState *dev, QemuConsole *src,
162                                InputEvent *evt)
163 {
164     HIDState *hs = (HIDState *)dev;
165     int scancodes[3], i, count;
166     int slot;
167 
168     count = qemu_input_key_value_to_scancode(evt->key->key,
169                                              evt->key->down,
170                                              scancodes);
171     if (hs->n + count > QUEUE_LENGTH) {
172         fprintf(stderr, "usb-kbd: warning: key event queue full\n");
173         return;
174     }
175     for (i = 0; i < count; i++) {
176         slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
177         hs->kbd.keycodes[slot] = scancodes[i];
178     }
179     hs->event(hs);
180 }
181 
182 static void hid_keyboard_process_keycode(HIDState *hs)
183 {
184     uint8_t hid_code, key;
185     int i, keycode, slot;
186 
187     if (hs->n == 0) {
188         return;
189     }
190     slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
191     keycode = hs->kbd.keycodes[slot];
192 
193     key = keycode & 0x7f;
194     hid_code = hid_usage_keys[key | ((hs->kbd.modifiers >> 1) & (1 << 7))];
195     hs->kbd.modifiers &= ~(1 << 8);
196 
197     switch (hid_code) {
198     case 0x00:
199         return;
200 
201     case 0xe0:
202         if (hs->kbd.modifiers & (1 << 9)) {
203             hs->kbd.modifiers ^= 3 << 8;
204             return;
205         }
206     case 0xe1 ... 0xe7:
207         if (keycode & (1 << 7)) {
208             hs->kbd.modifiers &= ~(1 << (hid_code & 0x0f));
209             return;
210         }
211     case 0xe8 ... 0xef:
212         hs->kbd.modifiers |= 1 << (hid_code & 0x0f);
213         return;
214     }
215 
216     if (keycode & (1 << 7)) {
217         for (i = hs->kbd.keys - 1; i >= 0; i--) {
218             if (hs->kbd.key[i] == hid_code) {
219                 hs->kbd.key[i] = hs->kbd.key[-- hs->kbd.keys];
220                 hs->kbd.key[hs->kbd.keys] = 0x00;
221                 break;
222             }
223         }
224         if (i < 0) {
225             return;
226         }
227     } else {
228         for (i = hs->kbd.keys - 1; i >= 0; i--) {
229             if (hs->kbd.key[i] == hid_code) {
230                 break;
231             }
232         }
233         if (i < 0) {
234             if (hs->kbd.keys < sizeof(hs->kbd.key)) {
235                 hs->kbd.key[hs->kbd.keys++] = hid_code;
236             }
237         } else {
238             return;
239         }
240     }
241 }
242 
243 static inline int int_clamp(int val, int vmin, int vmax)
244 {
245     if (val < vmin) {
246         return vmin;
247     } else if (val > vmax) {
248         return vmax;
249     } else {
250         return val;
251     }
252 }
253 
254 void hid_pointer_activate(HIDState *hs)
255 {
256     if (!hs->ptr.mouse_grabbed) {
257         qemu_activate_mouse_event_handler(hs->ptr.eh_entry);
258         hs->ptr.mouse_grabbed = 1;
259     }
260 }
261 
262 int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
263 {
264     int dx, dy, dz, b, l;
265     int index;
266     HIDPointerEvent *e;
267 
268     hs->idle_pending = false;
269 
270     hid_pointer_activate(hs);
271 
272     /* When the buffer is empty, return the last event.  Relative
273        movements will all be zero.  */
274     index = (hs->n ? hs->head : hs->head - 1);
275     e = &hs->ptr.queue[index & QUEUE_MASK];
276 
277     if (hs->kind == HID_MOUSE) {
278         dx = int_clamp(e->xdx, -127, 127);
279         dy = int_clamp(e->ydy, -127, 127);
280         e->xdx -= dx;
281         e->ydy -= dy;
282     } else {
283         dx = e->xdx;
284         dy = e->ydy;
285     }
286     dz = int_clamp(e->dz, -127, 127);
287     e->dz -= dz;
288 
289     b = 0;
290     if (e->buttons_state & MOUSE_EVENT_LBUTTON) {
291         b |= 0x01;
292     }
293     if (e->buttons_state & MOUSE_EVENT_RBUTTON) {
294         b |= 0x02;
295     }
296     if (e->buttons_state & MOUSE_EVENT_MBUTTON) {
297         b |= 0x04;
298     }
299 
300     if (hs->n &&
301         !e->dz &&
302         (hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) {
303         /* that deals with this event */
304         QUEUE_INCR(hs->head);
305         hs->n--;
306     }
307 
308     /* Appears we have to invert the wheel direction */
309     dz = 0 - dz;
310     l = 0;
311     switch (hs->kind) {
312     case HID_MOUSE:
313         if (len > l) {
314             buf[l++] = b;
315         }
316         if (len > l) {
317             buf[l++] = dx;
318         }
319         if (len > l) {
320             buf[l++] = dy;
321         }
322         if (len > l) {
323             buf[l++] = dz;
324         }
325         break;
326 
327     case HID_TABLET:
328         if (len > l) {
329             buf[l++] = b;
330         }
331         if (len > l) {
332             buf[l++] = dx & 0xff;
333         }
334         if (len > l) {
335             buf[l++] = dx >> 8;
336         }
337         if (len > l) {
338             buf[l++] = dy & 0xff;
339         }
340         if (len > l) {
341             buf[l++] = dy >> 8;
342         }
343         if (len > l) {
344             buf[l++] = dz;
345         }
346         break;
347 
348     default:
349         abort();
350     }
351 
352     return l;
353 }
354 
355 int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
356 {
357     hs->idle_pending = false;
358 
359     if (len < 2) {
360         return 0;
361     }
362 
363     hid_keyboard_process_keycode(hs);
364 
365     buf[0] = hs->kbd.modifiers & 0xff;
366     buf[1] = 0;
367     if (hs->kbd.keys > 6) {
368         memset(buf + 2, HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
369     } else {
370         memcpy(buf + 2, hs->kbd.key, MIN(8, len) - 2);
371     }
372 
373     return MIN(8, len);
374 }
375 
376 int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len)
377 {
378     if (len > 0) {
379         int ledstate = 0;
380         /* 0x01: Num Lock LED
381          * 0x02: Caps Lock LED
382          * 0x04: Scroll Lock LED
383          * 0x08: Compose LED
384          * 0x10: Kana LED */
385         hs->kbd.leds = buf[0];
386         if (hs->kbd.leds & 0x04) {
387             ledstate |= QEMU_SCROLL_LOCK_LED;
388         }
389         if (hs->kbd.leds & 0x01) {
390             ledstate |= QEMU_NUM_LOCK_LED;
391         }
392         if (hs->kbd.leds & 0x02) {
393             ledstate |= QEMU_CAPS_LOCK_LED;
394         }
395         kbd_put_ledstate(ledstate);
396     }
397     return 0;
398 }
399 
400 void hid_reset(HIDState *hs)
401 {
402     switch (hs->kind) {
403     case HID_KEYBOARD:
404         memset(hs->kbd.keycodes, 0, sizeof(hs->kbd.keycodes));
405         memset(hs->kbd.key, 0, sizeof(hs->kbd.key));
406         hs->kbd.keys = 0;
407         break;
408     case HID_MOUSE:
409     case HID_TABLET:
410         memset(hs->ptr.queue, 0, sizeof(hs->ptr.queue));
411         break;
412     }
413     hs->head = 0;
414     hs->n = 0;
415     hs->protocol = 1;
416     hs->idle = 0;
417     hs->idle_pending = false;
418     hid_del_idle_timer(hs);
419 }
420 
421 void hid_free(HIDState *hs)
422 {
423     switch (hs->kind) {
424     case HID_KEYBOARD:
425         qemu_input_handler_unregister(hs->s);
426         break;
427     case HID_MOUSE:
428     case HID_TABLET:
429         qemu_remove_mouse_event_handler(hs->ptr.eh_entry);
430         break;
431     }
432     hid_del_idle_timer(hs);
433 }
434 
435 static QemuInputHandler hid_keyboard_handler = {
436     .name  = "QEMU HID Keyboard",
437     .mask  = INPUT_EVENT_MASK_KEY,
438     .event = hid_keyboard_event,
439 };
440 
441 void hid_init(HIDState *hs, int kind, HIDEventFunc event)
442 {
443     hs->kind = kind;
444     hs->event = event;
445 
446     if (hs->kind == HID_KEYBOARD) {
447         hs->s = qemu_input_handler_register((DeviceState *)hs,
448                                             &hid_keyboard_handler);
449         qemu_input_handler_activate(hs->s);
450     } else if (hs->kind == HID_MOUSE) {
451         hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
452                                                         0, "QEMU HID Mouse");
453     } else if (hs->kind == HID_TABLET) {
454         hs->ptr.eh_entry = qemu_add_mouse_event_handler(hid_pointer_event, hs,
455                                                         1, "QEMU HID Tablet");
456     }
457 }
458 
459 static int hid_post_load(void *opaque, int version_id)
460 {
461     HIDState *s = opaque;
462 
463     hid_set_next_idle(s);
464     return 0;
465 }
466 
467 static const VMStateDescription vmstate_hid_ptr_queue = {
468     .name = "HIDPointerEventQueue",
469     .version_id = 1,
470     .minimum_version_id = 1,
471     .fields = (VMStateField[]) {
472         VMSTATE_INT32(xdx, HIDPointerEvent),
473         VMSTATE_INT32(ydy, HIDPointerEvent),
474         VMSTATE_INT32(dz, HIDPointerEvent),
475         VMSTATE_INT32(buttons_state, HIDPointerEvent),
476         VMSTATE_END_OF_LIST()
477     }
478 };
479 
480 const VMStateDescription vmstate_hid_ptr_device = {
481     .name = "HIDPointerDevice",
482     .version_id = 1,
483     .minimum_version_id = 1,
484     .post_load = hid_post_load,
485     .fields = (VMStateField[]) {
486         VMSTATE_STRUCT_ARRAY(ptr.queue, HIDState, QUEUE_LENGTH, 0,
487                              vmstate_hid_ptr_queue, HIDPointerEvent),
488         VMSTATE_UINT32(head, HIDState),
489         VMSTATE_UINT32(n, HIDState),
490         VMSTATE_INT32(protocol, HIDState),
491         VMSTATE_UINT8(idle, HIDState),
492         VMSTATE_END_OF_LIST(),
493     }
494 };
495 
496 const VMStateDescription vmstate_hid_keyboard_device = {
497     .name = "HIDKeyboardDevice",
498     .version_id = 1,
499     .minimum_version_id = 1,
500     .post_load = hid_post_load,
501     .fields = (VMStateField[]) {
502         VMSTATE_UINT32_ARRAY(kbd.keycodes, HIDState, QUEUE_LENGTH),
503         VMSTATE_UINT32(head, HIDState),
504         VMSTATE_UINT32(n, HIDState),
505         VMSTATE_UINT16(kbd.modifiers, HIDState),
506         VMSTATE_UINT8(kbd.leds, HIDState),
507         VMSTATE_UINT8_ARRAY(kbd.key, HIDState, 16),
508         VMSTATE_INT32(kbd.keys, HIDState),
509         VMSTATE_INT32(protocol, HIDState),
510         VMSTATE_UINT8(idle, HIDState),
511         VMSTATE_END_OF_LIST(),
512     }
513 };
514