4 * Copyright (c) 2005 Fabrice Bellard
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
31 /* HID interface requests */
32 #define GET_REPORT 0xa101
33 #define GET_IDLE 0xa102
34 #define GET_PROTOCOL 0xa103
35 #define SET_REPORT 0x2109
36 #define SET_IDLE 0x210a
37 #define SET_PROTOCOL 0x210b
39 /* HID descriptor types */
40 #define USB_DT_HID 0x21
41 #define USB_DT_REPORT 0x22
42 #define USB_DT_PHY 0x23
46 #define USB_KEYBOARD 3
48 typedef struct USBMouseState {
49 int dx, dy, dz, buttons_state;
52 QEMUPutMouseEntry *eh_entry;
55 typedef struct USBKeyboardState {
62 typedef struct USBHIDState {
71 int64_t next_idle_clock;
74 void (*datain)(void *);
88 static const USBDescStrings desc_strings = {
89 [STR_MANUFACTURER] = "QEMU " QEMU_VERSION,
90 [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
91 [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
92 [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
93 [STR_SERIALNUMBER] = "1",
94 [STR_CONFIG_MOUSE] = "HID Mouse",
95 [STR_CONFIG_TABLET] = "HID Tablet",
96 [STR_CONFIG_KEYBOARD] = "HID Keyboard",
99 static const USBDescIface desc_iface_mouse = {
100 .bInterfaceNumber = 0,
102 .bInterfaceClass = USB_CLASS_HID,
103 .bInterfaceSubClass = 0x01, /* boot */
104 .bInterfaceProtocol = 0x02,
106 .descs = (USBDescOther[]) {
109 .data = (uint8_t[]) {
110 0x09, /* u8 bLength */
111 USB_DT_HID, /* u8 bDescriptorType */
112 0x01, 0x00, /* u16 HID_class */
113 0x00, /* u8 country_code */
114 0x01, /* u8 num_descriptors */
115 USB_DT_REPORT, /* u8 type: Report */
120 .eps = (USBDescEndpoint[]) {
122 .bEndpointAddress = USB_DIR_IN | 0x01,
123 .bmAttributes = USB_ENDPOINT_XFER_INT,
130 static const USBDescIface desc_iface_tablet = {
131 .bInterfaceNumber = 0,
133 .bInterfaceClass = USB_CLASS_HID,
134 .bInterfaceSubClass = 0x01, /* boot */
135 .bInterfaceProtocol = 0x02,
137 .descs = (USBDescOther[]) {
140 .data = (uint8_t[]) {
141 0x09, /* u8 bLength */
142 USB_DT_HID, /* u8 bDescriptorType */
143 0x01, 0x00, /* u16 HID_class */
144 0x00, /* u8 country_code */
145 0x01, /* u8 num_descriptors */
146 USB_DT_REPORT, /* u8 type: Report */
151 .eps = (USBDescEndpoint[]) {
153 .bEndpointAddress = USB_DIR_IN | 0x01,
154 .bmAttributes = USB_ENDPOINT_XFER_INT,
161 static const USBDescIface desc_iface_keyboard = {
162 .bInterfaceNumber = 0,
164 .bInterfaceClass = USB_CLASS_HID,
165 .bInterfaceSubClass = 0x01, /* boot */
166 .bInterfaceProtocol = 0x01, /* keyboard */
168 .descs = (USBDescOther[]) {
171 .data = (uint8_t[]) {
172 0x09, /* u8 bLength */
173 USB_DT_HID, /* u8 bDescriptorType */
174 0x11, 0x01, /* u16 HID_class */
175 0x00, /* u8 country_code */
176 0x01, /* u8 num_descriptors */
177 USB_DT_REPORT, /* u8 type: Report */
178 0x3f, 0, /* u16 len */
182 .eps = (USBDescEndpoint[]) {
184 .bEndpointAddress = USB_DIR_IN | 0x01,
185 .bmAttributes = USB_ENDPOINT_XFER_INT,
192 static const USBDescDevice desc_device_mouse = {
194 .bMaxPacketSize0 = 8,
195 .bNumConfigurations = 1,
196 .confs = (USBDescConfig[]) {
199 .bConfigurationValue = 1,
200 .iConfiguration = STR_CONFIG_MOUSE,
201 .bmAttributes = 0xa0,
203 .ifs = &desc_iface_mouse,
208 static const USBDescDevice desc_device_tablet = {
210 .bMaxPacketSize0 = 8,
211 .bNumConfigurations = 1,
212 .confs = (USBDescConfig[]) {
215 .bConfigurationValue = 1,
216 .iConfiguration = STR_CONFIG_TABLET,
217 .bmAttributes = 0xa0,
219 .ifs = &desc_iface_tablet,
224 static const USBDescDevice desc_device_keyboard = {
226 .bMaxPacketSize0 = 8,
227 .bNumConfigurations = 1,
228 .confs = (USBDescConfig[]) {
231 .bConfigurationValue = 1,
232 .iConfiguration = STR_CONFIG_KEYBOARD,
233 .bmAttributes = 0xa0,
235 .ifs = &desc_iface_keyboard,
240 static const USBDesc desc_mouse = {
245 .iManufacturer = STR_MANUFACTURER,
246 .iProduct = STR_PRODUCT_MOUSE,
247 .iSerialNumber = STR_SERIALNUMBER,
249 .full = &desc_device_mouse,
253 static const USBDesc desc_tablet = {
258 .iManufacturer = STR_MANUFACTURER,
259 .iProduct = STR_PRODUCT_TABLET,
260 .iSerialNumber = STR_SERIALNUMBER,
262 .full = &desc_device_tablet,
266 static const USBDesc desc_keyboard = {
271 .iManufacturer = STR_MANUFACTURER,
272 .iProduct = STR_PRODUCT_KEYBOARD,
273 .iSerialNumber = STR_SERIALNUMBER,
275 .full = &desc_device_keyboard,
279 static const uint8_t qemu_mouse_hid_report_descriptor[] = {
280 0x05, 0x01, /* Usage Page (Generic Desktop) */
281 0x09, 0x02, /* Usage (Mouse) */
282 0xa1, 0x01, /* Collection (Application) */
283 0x09, 0x01, /* Usage (Pointer) */
284 0xa1, 0x00, /* Collection (Physical) */
285 0x05, 0x09, /* Usage Page (Button) */
286 0x19, 0x01, /* Usage Minimum (1) */
287 0x29, 0x03, /* Usage Maximum (3) */
288 0x15, 0x00, /* Logical Minimum (0) */
289 0x25, 0x01, /* Logical Maximum (1) */
290 0x95, 0x03, /* Report Count (3) */
291 0x75, 0x01, /* Report Size (1) */
292 0x81, 0x02, /* Input (Data, Variable, Absolute) */
293 0x95, 0x01, /* Report Count (1) */
294 0x75, 0x05, /* Report Size (5) */
295 0x81, 0x01, /* Input (Constant) */
296 0x05, 0x01, /* Usage Page (Generic Desktop) */
297 0x09, 0x30, /* Usage (X) */
298 0x09, 0x31, /* Usage (Y) */
299 0x09, 0x38, /* Usage (Wheel) */
300 0x15, 0x81, /* Logical Minimum (-0x7f) */
301 0x25, 0x7f, /* Logical Maximum (0x7f) */
302 0x75, 0x08, /* Report Size (8) */
303 0x95, 0x03, /* Report Count (3) */
304 0x81, 0x06, /* Input (Data, Variable, Relative) */
305 0xc0, /* End Collection */
306 0xc0, /* End Collection */
309 static const uint8_t qemu_tablet_hid_report_descriptor[] = {
310 0x05, 0x01, /* Usage Page (Generic Desktop) */
311 0x09, 0x01, /* Usage (Pointer) */
312 0xa1, 0x01, /* Collection (Application) */
313 0x09, 0x01, /* Usage (Pointer) */
314 0xa1, 0x00, /* Collection (Physical) */
315 0x05, 0x09, /* Usage Page (Button) */
316 0x19, 0x01, /* Usage Minimum (1) */
317 0x29, 0x03, /* Usage Maximum (3) */
318 0x15, 0x00, /* Logical Minimum (0) */
319 0x25, 0x01, /* Logical Maximum (1) */
320 0x95, 0x03, /* Report Count (3) */
321 0x75, 0x01, /* Report Size (1) */
322 0x81, 0x02, /* Input (Data, Variable, Absolute) */
323 0x95, 0x01, /* Report Count (1) */
324 0x75, 0x05, /* Report Size (5) */
325 0x81, 0x01, /* Input (Constant) */
326 0x05, 0x01, /* Usage Page (Generic Desktop) */
327 0x09, 0x30, /* Usage (X) */
328 0x09, 0x31, /* Usage (Y) */
329 0x15, 0x00, /* Logical Minimum (0) */
330 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
331 0x35, 0x00, /* Physical Minimum (0) */
332 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
333 0x75, 0x10, /* Report Size (16) */
334 0x95, 0x02, /* Report Count (2) */
335 0x81, 0x02, /* Input (Data, Variable, Absolute) */
336 0x05, 0x01, /* Usage Page (Generic Desktop) */
337 0x09, 0x38, /* Usage (Wheel) */
338 0x15, 0x81, /* Logical Minimum (-0x7f) */
339 0x25, 0x7f, /* Logical Maximum (0x7f) */
340 0x35, 0x00, /* Physical Minimum (same as logical) */
341 0x45, 0x00, /* Physical Maximum (same as logical) */
342 0x75, 0x08, /* Report Size (8) */
343 0x95, 0x01, /* Report Count (1) */
344 0x81, 0x06, /* Input (Data, Variable, Relative) */
345 0xc0, /* End Collection */
346 0xc0, /* End Collection */
349 static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
350 0x05, 0x01, /* Usage Page (Generic Desktop) */
351 0x09, 0x06, /* Usage (Keyboard) */
352 0xa1, 0x01, /* Collection (Application) */
353 0x75, 0x01, /* Report Size (1) */
354 0x95, 0x08, /* Report Count (8) */
355 0x05, 0x07, /* Usage Page (Key Codes) */
356 0x19, 0xe0, /* Usage Minimum (224) */
357 0x29, 0xe7, /* Usage Maximum (231) */
358 0x15, 0x00, /* Logical Minimum (0) */
359 0x25, 0x01, /* Logical Maximum (1) */
360 0x81, 0x02, /* Input (Data, Variable, Absolute) */
361 0x95, 0x01, /* Report Count (1) */
362 0x75, 0x08, /* Report Size (8) */
363 0x81, 0x01, /* Input (Constant) */
364 0x95, 0x05, /* Report Count (5) */
365 0x75, 0x01, /* Report Size (1) */
366 0x05, 0x08, /* Usage Page (LEDs) */
367 0x19, 0x01, /* Usage Minimum (1) */
368 0x29, 0x05, /* Usage Maximum (5) */
369 0x91, 0x02, /* Output (Data, Variable, Absolute) */
370 0x95, 0x01, /* Report Count (1) */
371 0x75, 0x03, /* Report Size (3) */
372 0x91, 0x01, /* Output (Constant) */
373 0x95, 0x06, /* Report Count (6) */
374 0x75, 0x08, /* Report Size (8) */
375 0x15, 0x00, /* Logical Minimum (0) */
376 0x25, 0xff, /* Logical Maximum (255) */
377 0x05, 0x07, /* Usage Page (Key Codes) */
378 0x19, 0x00, /* Usage Minimum (0) */
379 0x29, 0xff, /* Usage Maximum (255) */
380 0x81, 0x00, /* Input (Data, Array) */
381 0xc0, /* End Collection */
384 #define USB_HID_USAGE_ERROR_ROLLOVER 0x01
385 #define USB_HID_USAGE_POSTFAIL 0x02
386 #define USB_HID_USAGE_ERROR_UNDEFINED 0x03
388 /* Indices are QEMU keycodes, values are from HID Usage Table. Indices
389 * above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d. */
390 static const uint8_t usb_hid_usage_keys[0x100] = {
391 0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
392 0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b,
393 0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c,
394 0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16,
395 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33,
396 0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19,
397 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55,
398 0xe2, 0x2c, 0x32, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
399 0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f,
400 0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59,
401 0x5a, 0x5b, 0x62, 0x63, 0x00, 0x00, 0x00, 0x44,
402 0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
403 0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00,
404 0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
405 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
406 0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65,
408 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
409 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
410 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
411 0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
412 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
413 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
414 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
415 0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
416 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x4a,
417 0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
418 0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
419 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
420 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
421 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
422 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
423 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
426 static void usb_hid_changed(USBHIDState *hs)
431 hs->datain(hs->datain_opaque);
434 static void usb_mouse_event(void *opaque,
435 int dx1, int dy1, int dz1, int buttons_state)
437 USBHIDState *hs = opaque;
438 USBMouseState *s = &hs->ptr;
443 s->buttons_state = buttons_state;
448 static void usb_tablet_event(void *opaque,
449 int x, int y, int dz, int buttons_state)
451 USBHIDState *hs = opaque;
452 USBMouseState *s = &hs->ptr;
457 s->buttons_state = buttons_state;
462 static void usb_keyboard_event(void *opaque, int keycode)
464 USBHIDState *hs = opaque;
465 USBKeyboardState *s = &hs->kbd;
466 uint8_t hid_code, key;
469 key = keycode & 0x7f;
470 hid_code = usb_hid_usage_keys[key | ((s->modifiers >> 1) & (1 << 7))];
471 s->modifiers &= ~(1 << 8);
478 if (s->modifiers & (1 << 9)) {
479 s->modifiers ^= 3 << 8;
483 if (keycode & (1 << 7)) {
484 s->modifiers &= ~(1 << (hid_code & 0x0f));
488 s->modifiers |= 1 << (hid_code & 0x0f);
492 if (keycode & (1 << 7)) {
493 for (i = s->keys - 1; i >= 0; i --)
494 if (s->key[i] == hid_code) {
495 s->key[i] = s->key[-- s->keys];
496 s->key[s->keys] = 0x00;
503 for (i = s->keys - 1; i >= 0; i --)
504 if (s->key[i] == hid_code)
507 if (s->keys < sizeof(s->key))
508 s->key[s->keys ++] = hid_code;
516 static inline int int_clamp(int val, int vmin, int vmax)
526 static int usb_mouse_poll(USBHIDState *hs, uint8_t *buf, int len)
528 int dx, dy, dz, b, l;
529 USBMouseState *s = &hs->ptr;
531 if (!s->mouse_grabbed) {
532 qemu_activate_mouse_event_handler(s->eh_entry);
533 s->mouse_grabbed = 1;
536 dx = int_clamp(s->dx, -127, 127);
537 dy = int_clamp(s->dy, -127, 127);
538 dz = int_clamp(s->dz, -127, 127);
544 /* Appears we have to invert the wheel direction */
548 if (s->buttons_state & MOUSE_EVENT_LBUTTON)
550 if (s->buttons_state & MOUSE_EVENT_RBUTTON)
552 if (s->buttons_state & MOUSE_EVENT_MBUTTON)
567 static int usb_tablet_poll(USBHIDState *hs, uint8_t *buf, int len)
570 USBMouseState *s = &hs->ptr;
572 if (!s->mouse_grabbed) {
573 qemu_activate_mouse_event_handler(s->eh_entry);
574 s->mouse_grabbed = 1;
577 dz = int_clamp(s->dz, -127, 127);
580 /* Appears we have to invert the wheel direction */
583 if (s->buttons_state & MOUSE_EVENT_LBUTTON)
585 if (s->buttons_state & MOUSE_EVENT_RBUTTON)
587 if (s->buttons_state & MOUSE_EVENT_MBUTTON)
591 buf[1] = s->x & 0xff;
593 buf[3] = s->y & 0xff;
601 static int usb_keyboard_poll(USBKeyboardState *s, uint8_t *buf, int len)
606 buf[0] = s->modifiers & 0xff;
609 memset(buf + 2, USB_HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
611 memcpy(buf + 2, s->key, MIN(8, len) - 2);
616 static int usb_keyboard_write(USBKeyboardState *s, uint8_t *buf, int len)
620 /* 0x01: Num Lock LED
621 * 0x02: Caps Lock LED
622 * 0x04: Scroll Lock LED
627 ledstate |= QEMU_SCROLL_LOCK_LED;
629 ledstate |= QEMU_NUM_LOCK_LED;
631 ledstate |= QEMU_CAPS_LOCK_LED;
632 kbd_put_ledstate(ledstate);
637 static void usb_mouse_handle_reset(USBDevice *dev)
639 USBHIDState *s = (USBHIDState *)dev;
646 s->ptr.buttons_state = 0;
650 static void usb_keyboard_handle_reset(USBDevice *dev)
652 USBHIDState *s = (USBHIDState *)dev;
654 qemu_add_kbd_event_handler(usb_keyboard_event, s);
658 static void usb_hid_set_next_idle(USBHIDState *s, int64_t curtime)
660 s->next_idle_clock = curtime + (get_ticks_per_sec() * s->idle * 4) / 1000;
663 static int usb_hid_handle_control(USBDevice *dev, int request, int value,
664 int index, int length, uint8_t *data)
666 USBHIDState *s = (USBHIDState *)dev;
669 ret = usb_desc_handle_control(dev, request, value, index, length, data);
676 case DeviceRequest | USB_REQ_GET_STATUS:
677 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
678 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
682 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
683 if (value == USB_DEVICE_REMOTE_WAKEUP) {
684 dev->remote_wakeup = 0;
690 case DeviceOutRequest | USB_REQ_SET_FEATURE:
691 if (value == USB_DEVICE_REMOTE_WAKEUP) {
692 dev->remote_wakeup = 1;
698 case DeviceRequest | USB_REQ_GET_INTERFACE:
702 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
705 /* hid specific requests */
706 case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
709 if (s->kind == USB_MOUSE) {
710 memcpy(data, qemu_mouse_hid_report_descriptor,
711 sizeof(qemu_mouse_hid_report_descriptor));
712 ret = sizeof(qemu_mouse_hid_report_descriptor);
713 } else if (s->kind == USB_TABLET) {
714 memcpy(data, qemu_tablet_hid_report_descriptor,
715 sizeof(qemu_tablet_hid_report_descriptor));
716 ret = sizeof(qemu_tablet_hid_report_descriptor);
717 } else if (s->kind == USB_KEYBOARD) {
718 memcpy(data, qemu_keyboard_hid_report_descriptor,
719 sizeof(qemu_keyboard_hid_report_descriptor));
720 ret = sizeof(qemu_keyboard_hid_report_descriptor);
728 if (s->kind == USB_MOUSE)
729 ret = usb_mouse_poll(s, data, length);
730 else if (s->kind == USB_TABLET)
731 ret = usb_tablet_poll(s, data, length);
732 else if (s->kind == USB_KEYBOARD)
733 ret = usb_keyboard_poll(&s->kbd, data, length);
736 if (s->kind == USB_KEYBOARD)
737 ret = usb_keyboard_write(&s->kbd, data, length);
742 if (s->kind != USB_KEYBOARD)
745 data[0] = s->protocol;
748 if (s->kind != USB_KEYBOARD)
758 s->idle = (uint8_t) (value >> 8);
759 usb_hid_set_next_idle(s, qemu_get_clock(vm_clock));
770 static int usb_hid_handle_data(USBDevice *dev, USBPacket *p)
772 USBHIDState *s = (USBHIDState *)dev;
778 int64_t curtime = qemu_get_clock(vm_clock);
779 if (!s->changed && (!s->idle || s->next_idle_clock - curtime > 0))
781 usb_hid_set_next_idle(s, curtime);
783 if (s->kind == USB_MOUSE)
784 ret = usb_mouse_poll(s, p->data, p->len);
785 else if (s->kind == USB_TABLET)
786 ret = usb_tablet_poll(s, p->data, p->len);
787 else if (s->kind == USB_KEYBOARD)
788 ret = usb_keyboard_poll(&s->kbd, p->data, p->len);
802 static void usb_hid_handle_destroy(USBDevice *dev)
804 USBHIDState *s = (USBHIDState *)dev;
808 qemu_remove_kbd_event_handler();
811 qemu_remove_mouse_event_handler(s->ptr.eh_entry);
815 static int usb_hid_initfn(USBDevice *dev, int kind)
817 USBHIDState *s = DO_UPCAST(USBHIDState, dev, dev);
822 if (s->kind == USB_MOUSE) {
823 s->ptr.eh_entry = qemu_add_mouse_event_handler(usb_mouse_event, s,
824 0, "QEMU USB Mouse");
825 } else if (s->kind == USB_TABLET) {
826 s->ptr.eh_entry = qemu_add_mouse_event_handler(usb_tablet_event, s,
827 1, "QEMU USB Tablet");
830 /* Force poll routine to be run and grab input the first time. */
835 static int usb_tablet_initfn(USBDevice *dev)
837 return usb_hid_initfn(dev, USB_TABLET);
840 static int usb_mouse_initfn(USBDevice *dev)
842 return usb_hid_initfn(dev, USB_MOUSE);
845 static int usb_keyboard_initfn(USBDevice *dev)
847 return usb_hid_initfn(dev, USB_KEYBOARD);
850 void usb_hid_datain_cb(USBDevice *dev, void *opaque, void (*datain)(void *))
852 USBHIDState *s = (USBHIDState *)dev;
854 s->datain_opaque = opaque;
858 static struct USBDeviceInfo hid_info[] = {
860 .product_desc = "QEMU USB Tablet",
861 .qdev.name = "usb-tablet",
862 .usbdevice_name = "tablet",
863 .qdev.size = sizeof(USBHIDState),
864 .usb_desc = &desc_tablet,
865 .init = usb_tablet_initfn,
866 .handle_packet = usb_generic_handle_packet,
867 .handle_reset = usb_mouse_handle_reset,
868 .handle_control = usb_hid_handle_control,
869 .handle_data = usb_hid_handle_data,
870 .handle_destroy = usb_hid_handle_destroy,
872 .product_desc = "QEMU USB Mouse",
873 .qdev.name = "usb-mouse",
874 .usbdevice_name = "mouse",
875 .qdev.size = sizeof(USBHIDState),
876 .usb_desc = &desc_mouse,
877 .init = usb_mouse_initfn,
878 .handle_packet = usb_generic_handle_packet,
879 .handle_reset = usb_mouse_handle_reset,
880 .handle_control = usb_hid_handle_control,
881 .handle_data = usb_hid_handle_data,
882 .handle_destroy = usb_hid_handle_destroy,
884 .product_desc = "QEMU USB Keyboard",
885 .qdev.name = "usb-kbd",
886 .usbdevice_name = "keyboard",
887 .qdev.size = sizeof(USBHIDState),
888 .usb_desc = &desc_keyboard,
889 .init = usb_keyboard_initfn,
890 .handle_packet = usb_generic_handle_packet,
891 .handle_reset = usb_keyboard_handle_reset,
892 .handle_control = usb_hid_handle_control,
893 .handle_data = usb_hid_handle_data,
894 .handle_destroy = usb_hid_handle_destroy,
900 static void usb_hid_register_devices(void)
902 usb_qdev_register_many(hid_info);
904 device_init(usb_hid_register_devices)