usb-hid: Add high speed mouse configuration
[qemu.git] / hw / usb / dev-hid.c
blobc238fcd38fe147bc3bacb61373f4b57e0e498d9f
1 /*
2 * QEMU USB HID devices
4 * Copyright (c) 2005 Fabrice Bellard
5 * Copyright (c) 2007 OpenMoko, Inc. (andrew@openedhand.com)
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
23 * THE SOFTWARE.
25 #include "hw/hw.h"
26 #include "ui/console.h"
27 #include "hw/usb.h"
28 #include "hw/usb/desc.h"
29 #include "qemu/timer.h"
30 #include "hw/input/hid.h"
32 /* HID interface requests */
33 #define GET_REPORT 0xa101
34 #define GET_IDLE 0xa102
35 #define GET_PROTOCOL 0xa103
36 #define SET_REPORT 0x2109
37 #define SET_IDLE 0x210a
38 #define SET_PROTOCOL 0x210b
40 /* HID descriptor types */
41 #define USB_DT_HID 0x21
42 #define USB_DT_REPORT 0x22
43 #define USB_DT_PHY 0x23
45 typedef struct USBHIDState {
46 USBDevice dev;
47 USBEndpoint *intr;
48 HIDState hid;
49 uint32_t usb_version;
50 char *display;
51 uint32_t head;
52 } USBHIDState;
54 enum {
55 STR_MANUFACTURER = 1,
56 STR_PRODUCT_MOUSE,
57 STR_PRODUCT_TABLET,
58 STR_PRODUCT_KEYBOARD,
59 STR_SERIALNUMBER,
60 STR_CONFIG_MOUSE,
61 STR_CONFIG_TABLET,
62 STR_CONFIG_KEYBOARD,
65 static const USBDescStrings desc_strings = {
66 [STR_MANUFACTURER] = "QEMU",
67 [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
68 [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
69 [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
70 [STR_SERIALNUMBER] = "42", /* == remote wakeup works */
71 [STR_CONFIG_MOUSE] = "HID Mouse",
72 [STR_CONFIG_TABLET] = "HID Tablet",
73 [STR_CONFIG_KEYBOARD] = "HID Keyboard",
76 static const USBDescIface desc_iface_mouse = {
77 .bInterfaceNumber = 0,
78 .bNumEndpoints = 1,
79 .bInterfaceClass = USB_CLASS_HID,
80 .bInterfaceSubClass = 0x01, /* boot */
81 .bInterfaceProtocol = 0x02,
82 .ndesc = 1,
83 .descs = (USBDescOther[]) {
85 /* HID descriptor */
86 .data = (uint8_t[]) {
87 0x09, /* u8 bLength */
88 USB_DT_HID, /* u8 bDescriptorType */
89 0x01, 0x00, /* u16 HID_class */
90 0x00, /* u8 country_code */
91 0x01, /* u8 num_descriptors */
92 USB_DT_REPORT, /* u8 type: Report */
93 52, 0, /* u16 len */
97 .eps = (USBDescEndpoint[]) {
99 .bEndpointAddress = USB_DIR_IN | 0x01,
100 .bmAttributes = USB_ENDPOINT_XFER_INT,
101 .wMaxPacketSize = 4,
102 .bInterval = 0x0a,
107 static const USBDescIface desc_iface_mouse2 = {
108 .bInterfaceNumber = 0,
109 .bNumEndpoints = 1,
110 .bInterfaceClass = USB_CLASS_HID,
111 .bInterfaceSubClass = 0x01, /* boot */
112 .bInterfaceProtocol = 0x02,
113 .ndesc = 1,
114 .descs = (USBDescOther[]) {
116 /* HID descriptor */
117 .data = (uint8_t[]) {
118 0x09, /* u8 bLength */
119 USB_DT_HID, /* u8 bDescriptorType */
120 0x01, 0x00, /* u16 HID_class */
121 0x00, /* u8 country_code */
122 0x01, /* u8 num_descriptors */
123 USB_DT_REPORT, /* u8 type: Report */
124 52, 0, /* u16 len */
128 .eps = (USBDescEndpoint[]) {
130 .bEndpointAddress = USB_DIR_IN | 0x01,
131 .bmAttributes = USB_ENDPOINT_XFER_INT,
132 .wMaxPacketSize = 4,
133 .bInterval = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
138 static const USBDescIface desc_iface_tablet = {
139 .bInterfaceNumber = 0,
140 .bNumEndpoints = 1,
141 .bInterfaceClass = USB_CLASS_HID,
142 .bInterfaceProtocol = 0x02,
143 .ndesc = 1,
144 .descs = (USBDescOther[]) {
146 /* HID descriptor */
147 .data = (uint8_t[]) {
148 0x09, /* u8 bLength */
149 USB_DT_HID, /* u8 bDescriptorType */
150 0x01, 0x00, /* u16 HID_class */
151 0x00, /* u8 country_code */
152 0x01, /* u8 num_descriptors */
153 USB_DT_REPORT, /* u8 type: Report */
154 74, 0, /* u16 len */
158 .eps = (USBDescEndpoint[]) {
160 .bEndpointAddress = USB_DIR_IN | 0x01,
161 .bmAttributes = USB_ENDPOINT_XFER_INT,
162 .wMaxPacketSize = 8,
163 .bInterval = 0x0a,
168 static const USBDescIface desc_iface_tablet2 = {
169 .bInterfaceNumber = 0,
170 .bNumEndpoints = 1,
171 .bInterfaceClass = USB_CLASS_HID,
172 .bInterfaceProtocol = 0x02,
173 .ndesc = 1,
174 .descs = (USBDescOther[]) {
176 /* HID descriptor */
177 .data = (uint8_t[]) {
178 0x09, /* u8 bLength */
179 USB_DT_HID, /* u8 bDescriptorType */
180 0x01, 0x00, /* u16 HID_class */
181 0x00, /* u8 country_code */
182 0x01, /* u8 num_descriptors */
183 USB_DT_REPORT, /* u8 type: Report */
184 74, 0, /* u16 len */
188 .eps = (USBDescEndpoint[]) {
190 .bEndpointAddress = USB_DIR_IN | 0x01,
191 .bmAttributes = USB_ENDPOINT_XFER_INT,
192 .wMaxPacketSize = 8,
193 .bInterval = 4, /* 2 ^ (4-1) * 125 usecs = 1 ms */
198 static const USBDescIface desc_iface_keyboard = {
199 .bInterfaceNumber = 0,
200 .bNumEndpoints = 1,
201 .bInterfaceClass = USB_CLASS_HID,
202 .bInterfaceSubClass = 0x01, /* boot */
203 .bInterfaceProtocol = 0x01, /* keyboard */
204 .ndesc = 1,
205 .descs = (USBDescOther[]) {
207 /* HID descriptor */
208 .data = (uint8_t[]) {
209 0x09, /* u8 bLength */
210 USB_DT_HID, /* u8 bDescriptorType */
211 0x11, 0x01, /* u16 HID_class */
212 0x00, /* u8 country_code */
213 0x01, /* u8 num_descriptors */
214 USB_DT_REPORT, /* u8 type: Report */
215 0x3f, 0, /* u16 len */
219 .eps = (USBDescEndpoint[]) {
221 .bEndpointAddress = USB_DIR_IN | 0x01,
222 .bmAttributes = USB_ENDPOINT_XFER_INT,
223 .wMaxPacketSize = 8,
224 .bInterval = 0x0a,
229 static const USBDescDevice desc_device_mouse = {
230 .bcdUSB = 0x0100,
231 .bMaxPacketSize0 = 8,
232 .bNumConfigurations = 1,
233 .confs = (USBDescConfig[]) {
235 .bNumInterfaces = 1,
236 .bConfigurationValue = 1,
237 .iConfiguration = STR_CONFIG_MOUSE,
238 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
239 .bMaxPower = 50,
240 .nif = 1,
241 .ifs = &desc_iface_mouse,
246 static const USBDescDevice desc_device_mouse2 = {
247 .bcdUSB = 0x0200,
248 .bMaxPacketSize0 = 64,
249 .bNumConfigurations = 1,
250 .confs = (USBDescConfig[]) {
252 .bNumInterfaces = 1,
253 .bConfigurationValue = 1,
254 .iConfiguration = STR_CONFIG_MOUSE,
255 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
256 .bMaxPower = 50,
257 .nif = 1,
258 .ifs = &desc_iface_mouse2,
263 static const USBDescDevice desc_device_tablet = {
264 .bcdUSB = 0x0100,
265 .bMaxPacketSize0 = 8,
266 .bNumConfigurations = 1,
267 .confs = (USBDescConfig[]) {
269 .bNumInterfaces = 1,
270 .bConfigurationValue = 1,
271 .iConfiguration = STR_CONFIG_TABLET,
272 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
273 .bMaxPower = 50,
274 .nif = 1,
275 .ifs = &desc_iface_tablet,
280 static const USBDescDevice desc_device_tablet2 = {
281 .bcdUSB = 0x0200,
282 .bMaxPacketSize0 = 64,
283 .bNumConfigurations = 1,
284 .confs = (USBDescConfig[]) {
286 .bNumInterfaces = 1,
287 .bConfigurationValue = 1,
288 .iConfiguration = STR_CONFIG_TABLET,
289 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
290 .bMaxPower = 50,
291 .nif = 1,
292 .ifs = &desc_iface_tablet2,
297 static const USBDescDevice desc_device_keyboard = {
298 .bcdUSB = 0x0100,
299 .bMaxPacketSize0 = 8,
300 .bNumConfigurations = 1,
301 .confs = (USBDescConfig[]) {
303 .bNumInterfaces = 1,
304 .bConfigurationValue = 1,
305 .iConfiguration = STR_CONFIG_KEYBOARD,
306 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
307 .bMaxPower = 50,
308 .nif = 1,
309 .ifs = &desc_iface_keyboard,
314 static const USBDescMSOS desc_msos_suspend = {
315 .SelectiveSuspendEnabled = true,
318 static const USBDesc desc_mouse = {
319 .id = {
320 .idVendor = 0x0627,
321 .idProduct = 0x0001,
322 .bcdDevice = 0,
323 .iManufacturer = STR_MANUFACTURER,
324 .iProduct = STR_PRODUCT_MOUSE,
325 .iSerialNumber = STR_SERIALNUMBER,
327 .full = &desc_device_mouse,
328 .str = desc_strings,
329 .msos = &desc_msos_suspend,
332 static const USBDesc desc_mouse2 = {
333 .id = {
334 .idVendor = 0x0627,
335 .idProduct = 0x0001,
336 .bcdDevice = 0,
337 .iManufacturer = STR_MANUFACTURER,
338 .iProduct = STR_PRODUCT_MOUSE,
339 .iSerialNumber = STR_SERIALNUMBER,
341 .full = &desc_device_mouse,
342 .high = &desc_device_mouse2,
343 .str = desc_strings,
344 .msos = &desc_msos_suspend,
347 static const USBDesc desc_tablet = {
348 .id = {
349 .idVendor = 0x0627,
350 .idProduct = 0x0001,
351 .bcdDevice = 0,
352 .iManufacturer = STR_MANUFACTURER,
353 .iProduct = STR_PRODUCT_TABLET,
354 .iSerialNumber = STR_SERIALNUMBER,
356 .full = &desc_device_tablet,
357 .str = desc_strings,
358 .msos = &desc_msos_suspend,
361 static const USBDesc desc_tablet2 = {
362 .id = {
363 .idVendor = 0x0627,
364 .idProduct = 0x0001,
365 .bcdDevice = 0,
366 .iManufacturer = STR_MANUFACTURER,
367 .iProduct = STR_PRODUCT_TABLET,
368 .iSerialNumber = STR_SERIALNUMBER,
370 .full = &desc_device_tablet,
371 .high = &desc_device_tablet2,
372 .str = desc_strings,
373 .msos = &desc_msos_suspend,
376 static const USBDesc desc_keyboard = {
377 .id = {
378 .idVendor = 0x0627,
379 .idProduct = 0x0001,
380 .bcdDevice = 0,
381 .iManufacturer = STR_MANUFACTURER,
382 .iProduct = STR_PRODUCT_KEYBOARD,
383 .iSerialNumber = STR_SERIALNUMBER,
385 .full = &desc_device_keyboard,
386 .str = desc_strings,
387 .msos = &desc_msos_suspend,
390 static const uint8_t qemu_mouse_hid_report_descriptor[] = {
391 0x05, 0x01, /* Usage Page (Generic Desktop) */
392 0x09, 0x02, /* Usage (Mouse) */
393 0xa1, 0x01, /* Collection (Application) */
394 0x09, 0x01, /* Usage (Pointer) */
395 0xa1, 0x00, /* Collection (Physical) */
396 0x05, 0x09, /* Usage Page (Button) */
397 0x19, 0x01, /* Usage Minimum (1) */
398 0x29, 0x03, /* Usage Maximum (3) */
399 0x15, 0x00, /* Logical Minimum (0) */
400 0x25, 0x01, /* Logical Maximum (1) */
401 0x95, 0x03, /* Report Count (3) */
402 0x75, 0x01, /* Report Size (1) */
403 0x81, 0x02, /* Input (Data, Variable, Absolute) */
404 0x95, 0x01, /* Report Count (1) */
405 0x75, 0x05, /* Report Size (5) */
406 0x81, 0x01, /* Input (Constant) */
407 0x05, 0x01, /* Usage Page (Generic Desktop) */
408 0x09, 0x30, /* Usage (X) */
409 0x09, 0x31, /* Usage (Y) */
410 0x09, 0x38, /* Usage (Wheel) */
411 0x15, 0x81, /* Logical Minimum (-0x7f) */
412 0x25, 0x7f, /* Logical Maximum (0x7f) */
413 0x75, 0x08, /* Report Size (8) */
414 0x95, 0x03, /* Report Count (3) */
415 0x81, 0x06, /* Input (Data, Variable, Relative) */
416 0xc0, /* End Collection */
417 0xc0, /* End Collection */
420 static const uint8_t qemu_tablet_hid_report_descriptor[] = {
421 0x05, 0x01, /* Usage Page (Generic Desktop) */
422 0x09, 0x01, /* Usage (Pointer) */
423 0xa1, 0x01, /* Collection (Application) */
424 0x09, 0x01, /* Usage (Pointer) */
425 0xa1, 0x00, /* Collection (Physical) */
426 0x05, 0x09, /* Usage Page (Button) */
427 0x19, 0x01, /* Usage Minimum (1) */
428 0x29, 0x03, /* Usage Maximum (3) */
429 0x15, 0x00, /* Logical Minimum (0) */
430 0x25, 0x01, /* Logical Maximum (1) */
431 0x95, 0x03, /* Report Count (3) */
432 0x75, 0x01, /* Report Size (1) */
433 0x81, 0x02, /* Input (Data, Variable, Absolute) */
434 0x95, 0x01, /* Report Count (1) */
435 0x75, 0x05, /* Report Size (5) */
436 0x81, 0x01, /* Input (Constant) */
437 0x05, 0x01, /* Usage Page (Generic Desktop) */
438 0x09, 0x30, /* Usage (X) */
439 0x09, 0x31, /* Usage (Y) */
440 0x15, 0x00, /* Logical Minimum (0) */
441 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
442 0x35, 0x00, /* Physical Minimum (0) */
443 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
444 0x75, 0x10, /* Report Size (16) */
445 0x95, 0x02, /* Report Count (2) */
446 0x81, 0x02, /* Input (Data, Variable, Absolute) */
447 0x05, 0x01, /* Usage Page (Generic Desktop) */
448 0x09, 0x38, /* Usage (Wheel) */
449 0x15, 0x81, /* Logical Minimum (-0x7f) */
450 0x25, 0x7f, /* Logical Maximum (0x7f) */
451 0x35, 0x00, /* Physical Minimum (same as logical) */
452 0x45, 0x00, /* Physical Maximum (same as logical) */
453 0x75, 0x08, /* Report Size (8) */
454 0x95, 0x01, /* Report Count (1) */
455 0x81, 0x06, /* Input (Data, Variable, Relative) */
456 0xc0, /* End Collection */
457 0xc0, /* End Collection */
460 static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
461 0x05, 0x01, /* Usage Page (Generic Desktop) */
462 0x09, 0x06, /* Usage (Keyboard) */
463 0xa1, 0x01, /* Collection (Application) */
464 0x75, 0x01, /* Report Size (1) */
465 0x95, 0x08, /* Report Count (8) */
466 0x05, 0x07, /* Usage Page (Key Codes) */
467 0x19, 0xe0, /* Usage Minimum (224) */
468 0x29, 0xe7, /* Usage Maximum (231) */
469 0x15, 0x00, /* Logical Minimum (0) */
470 0x25, 0x01, /* Logical Maximum (1) */
471 0x81, 0x02, /* Input (Data, Variable, Absolute) */
472 0x95, 0x01, /* Report Count (1) */
473 0x75, 0x08, /* Report Size (8) */
474 0x81, 0x01, /* Input (Constant) */
475 0x95, 0x05, /* Report Count (5) */
476 0x75, 0x01, /* Report Size (1) */
477 0x05, 0x08, /* Usage Page (LEDs) */
478 0x19, 0x01, /* Usage Minimum (1) */
479 0x29, 0x05, /* Usage Maximum (5) */
480 0x91, 0x02, /* Output (Data, Variable, Absolute) */
481 0x95, 0x01, /* Report Count (1) */
482 0x75, 0x03, /* Report Size (3) */
483 0x91, 0x01, /* Output (Constant) */
484 0x95, 0x06, /* Report Count (6) */
485 0x75, 0x08, /* Report Size (8) */
486 0x15, 0x00, /* Logical Minimum (0) */
487 0x25, 0xff, /* Logical Maximum (255) */
488 0x05, 0x07, /* Usage Page (Key Codes) */
489 0x19, 0x00, /* Usage Minimum (0) */
490 0x29, 0xff, /* Usage Maximum (255) */
491 0x81, 0x00, /* Input (Data, Array) */
492 0xc0, /* End Collection */
495 static void usb_hid_changed(HIDState *hs)
497 USBHIDState *us = container_of(hs, USBHIDState, hid);
499 usb_wakeup(us->intr, 0);
502 static void usb_hid_handle_reset(USBDevice *dev)
504 USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
506 hid_reset(&us->hid);
509 static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
510 int request, int value, int index, int length, uint8_t *data)
512 USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
513 HIDState *hs = &us->hid;
514 int ret;
516 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
517 if (ret >= 0) {
518 return;
521 switch (request) {
522 /* hid specific requests */
523 case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
524 switch (value >> 8) {
525 case 0x22:
526 if (hs->kind == HID_MOUSE) {
527 memcpy(data, qemu_mouse_hid_report_descriptor,
528 sizeof(qemu_mouse_hid_report_descriptor));
529 p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
530 } else if (hs->kind == HID_TABLET) {
531 memcpy(data, qemu_tablet_hid_report_descriptor,
532 sizeof(qemu_tablet_hid_report_descriptor));
533 p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
534 } else if (hs->kind == HID_KEYBOARD) {
535 memcpy(data, qemu_keyboard_hid_report_descriptor,
536 sizeof(qemu_keyboard_hid_report_descriptor));
537 p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
539 break;
540 default:
541 goto fail;
543 break;
544 case GET_REPORT:
545 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
546 p->actual_length = hid_pointer_poll(hs, data, length);
547 } else if (hs->kind == HID_KEYBOARD) {
548 p->actual_length = hid_keyboard_poll(hs, data, length);
550 break;
551 case SET_REPORT:
552 if (hs->kind == HID_KEYBOARD) {
553 p->actual_length = hid_keyboard_write(hs, data, length);
554 } else {
555 goto fail;
557 break;
558 case GET_PROTOCOL:
559 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
560 goto fail;
562 data[0] = hs->protocol;
563 p->actual_length = 1;
564 break;
565 case SET_PROTOCOL:
566 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
567 goto fail;
569 hs->protocol = value;
570 break;
571 case GET_IDLE:
572 data[0] = hs->idle;
573 p->actual_length = 1;
574 break;
575 case SET_IDLE:
576 hs->idle = (uint8_t) (value >> 8);
577 hid_set_next_idle(hs);
578 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
579 hid_pointer_activate(hs);
581 break;
582 default:
583 fail:
584 p->status = USB_RET_STALL;
585 break;
589 static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
591 USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
592 HIDState *hs = &us->hid;
593 uint8_t buf[p->iov.size];
594 int len = 0;
596 switch (p->pid) {
597 case USB_TOKEN_IN:
598 if (p->ep->nr == 1) {
599 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
600 hid_pointer_activate(hs);
602 if (!hid_has_events(hs)) {
603 p->status = USB_RET_NAK;
604 return;
606 hid_set_next_idle(hs);
607 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
608 len = hid_pointer_poll(hs, buf, p->iov.size);
609 } else if (hs->kind == HID_KEYBOARD) {
610 len = hid_keyboard_poll(hs, buf, p->iov.size);
612 usb_packet_copy(p, buf, len);
613 } else {
614 goto fail;
616 break;
617 case USB_TOKEN_OUT:
618 default:
619 fail:
620 p->status = USB_RET_STALL;
621 break;
625 static void usb_hid_handle_destroy(USBDevice *dev)
627 USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
629 hid_free(&us->hid);
632 static void usb_hid_initfn(USBDevice *dev, int kind,
633 const USBDesc *usb1, const USBDesc *usb2,
634 Error **errp)
636 USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
637 switch (us->usb_version) {
638 case 1:
639 dev->usb_desc = usb1;
640 break;
641 case 2:
642 dev->usb_desc = usb2;
643 break;
644 default:
645 dev->usb_desc = NULL;
647 if (!dev->usb_desc) {
648 error_setg(errp, "Invalid usb version %d for usb hid device",
649 us->usb_version);
650 return;
653 if (dev->serial) {
654 usb_desc_set_string(dev, STR_SERIALNUMBER, dev->serial);
656 usb_desc_init(dev);
657 us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
658 hid_init(&us->hid, kind, usb_hid_changed);
659 if (us->display && us->hid.s) {
660 qemu_input_handler_bind(us->hid.s, us->display, us->head, NULL);
664 static void usb_tablet_realize(USBDevice *dev, Error **errp)
667 usb_hid_initfn(dev, HID_TABLET, &desc_tablet, &desc_tablet2, errp);
670 static void usb_mouse_realize(USBDevice *dev, Error **errp)
672 usb_hid_initfn(dev, HID_MOUSE, &desc_mouse, &desc_mouse2, errp);
675 static void usb_keyboard_realize(USBDevice *dev, Error **errp)
677 usb_hid_initfn(dev, HID_KEYBOARD, &desc_keyboard, NULL, errp);
680 static int usb_ptr_post_load(void *opaque, int version_id)
682 USBHIDState *s = opaque;
684 if (s->dev.remote_wakeup) {
685 hid_pointer_activate(&s->hid);
687 return 0;
690 static const VMStateDescription vmstate_usb_ptr = {
691 .name = "usb-ptr",
692 .version_id = 1,
693 .minimum_version_id = 1,
694 .post_load = usb_ptr_post_load,
695 .fields = (VMStateField[]) {
696 VMSTATE_USB_DEVICE(dev, USBHIDState),
697 VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
698 VMSTATE_END_OF_LIST()
702 static const VMStateDescription vmstate_usb_kbd = {
703 .name = "usb-kbd",
704 .version_id = 1,
705 .minimum_version_id = 1,
706 .fields = (VMStateField[]) {
707 VMSTATE_USB_DEVICE(dev, USBHIDState),
708 VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
709 VMSTATE_END_OF_LIST()
713 static void usb_hid_class_initfn(ObjectClass *klass, void *data)
715 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
717 uc->handle_reset = usb_hid_handle_reset;
718 uc->handle_control = usb_hid_handle_control;
719 uc->handle_data = usb_hid_handle_data;
720 uc->handle_destroy = usb_hid_handle_destroy;
721 uc->handle_attach = usb_desc_attach;
724 static Property usb_tablet_properties[] = {
725 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
726 DEFINE_PROP_STRING("display", USBHIDState, display),
727 DEFINE_PROP_UINT32("head", USBHIDState, head, 0),
728 DEFINE_PROP_END_OF_LIST(),
731 static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
733 DeviceClass *dc = DEVICE_CLASS(klass);
734 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
736 usb_hid_class_initfn(klass, data);
737 uc->realize = usb_tablet_realize;
738 uc->product_desc = "QEMU USB Tablet";
739 dc->vmsd = &vmstate_usb_ptr;
740 dc->props = usb_tablet_properties;
741 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
744 static const TypeInfo usb_tablet_info = {
745 .name = "usb-tablet",
746 .parent = TYPE_USB_DEVICE,
747 .instance_size = sizeof(USBHIDState),
748 .class_init = usb_tablet_class_initfn,
751 static Property usb_mouse_properties[] = {
752 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
753 DEFINE_PROP_END_OF_LIST(),
756 static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
758 DeviceClass *dc = DEVICE_CLASS(klass);
759 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
761 usb_hid_class_initfn(klass, data);
762 uc->realize = usb_mouse_realize;
763 uc->product_desc = "QEMU USB Mouse";
764 dc->vmsd = &vmstate_usb_ptr;
765 dc->props = usb_mouse_properties;
766 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
769 static const TypeInfo usb_mouse_info = {
770 .name = "usb-mouse",
771 .parent = TYPE_USB_DEVICE,
772 .instance_size = sizeof(USBHIDState),
773 .class_init = usb_mouse_class_initfn,
776 static Property usb_keyboard_properties[] = {
777 DEFINE_PROP_STRING("display", USBHIDState, display),
778 DEFINE_PROP_END_OF_LIST(),
781 static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
783 DeviceClass *dc = DEVICE_CLASS(klass);
784 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
786 usb_hid_class_initfn(klass, data);
787 uc->realize = usb_keyboard_realize;
788 uc->product_desc = "QEMU USB Keyboard";
789 dc->vmsd = &vmstate_usb_kbd;
790 dc->props = usb_keyboard_properties;
791 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
794 static const TypeInfo usb_keyboard_info = {
795 .name = "usb-kbd",
796 .parent = TYPE_USB_DEVICE,
797 .instance_size = sizeof(USBHIDState),
798 .class_init = usb_keyboard_class_initfn,
801 static void usb_hid_register_types(void)
803 type_register_static(&usb_tablet_info);
804 usb_legacy_register("usb-tablet", "tablet", NULL);
805 type_register_static(&usb_mouse_info);
806 usb_legacy_register("usb-mouse", "mouse", NULL);
807 type_register_static(&usb_keyboard_info);
808 usb_legacy_register("usb-kbd", "keyboard", NULL);
811 type_init(usb_hid_register_types)