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
26 #include "ui/console.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
{
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,
79 .bInterfaceClass
= USB_CLASS_HID
,
80 .bInterfaceSubClass
= 0x01, /* boot */
81 .bInterfaceProtocol
= 0x02,
83 .descs
= (USBDescOther
[]) {
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 */
97 .eps
= (USBDescEndpoint
[]) {
99 .bEndpointAddress
= USB_DIR_IN
| 0x01,
100 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
107 static const USBDescIface desc_iface_mouse2
= {
108 .bInterfaceNumber
= 0,
110 .bInterfaceClass
= USB_CLASS_HID
,
111 .bInterfaceSubClass
= 0x01, /* boot */
112 .bInterfaceProtocol
= 0x02,
114 .descs
= (USBDescOther
[]) {
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 */
128 .eps
= (USBDescEndpoint
[]) {
130 .bEndpointAddress
= USB_DIR_IN
| 0x01,
131 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
133 .bInterval
= 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
138 static const USBDescIface desc_iface_tablet
= {
139 .bInterfaceNumber
= 0,
141 .bInterfaceClass
= USB_CLASS_HID
,
142 .bInterfaceProtocol
= 0x02,
144 .descs
= (USBDescOther
[]) {
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 */
158 .eps
= (USBDescEndpoint
[]) {
160 .bEndpointAddress
= USB_DIR_IN
| 0x01,
161 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
168 static const USBDescIface desc_iface_tablet2
= {
169 .bInterfaceNumber
= 0,
171 .bInterfaceClass
= USB_CLASS_HID
,
172 .bInterfaceProtocol
= 0x02,
174 .descs
= (USBDescOther
[]) {
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 */
188 .eps
= (USBDescEndpoint
[]) {
190 .bEndpointAddress
= USB_DIR_IN
| 0x01,
191 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
193 .bInterval
= 4, /* 2 ^ (4-1) * 125 usecs = 1 ms */
198 static const USBDescIface desc_iface_keyboard
= {
199 .bInterfaceNumber
= 0,
201 .bInterfaceClass
= USB_CLASS_HID
,
202 .bInterfaceSubClass
= 0x01, /* boot */
203 .bInterfaceProtocol
= 0x01, /* keyboard */
205 .descs
= (USBDescOther
[]) {
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
,
229 static const USBDescIface desc_iface_keyboard2
= {
230 .bInterfaceNumber
= 0,
232 .bInterfaceClass
= USB_CLASS_HID
,
233 .bInterfaceSubClass
= 0x01, /* boot */
234 .bInterfaceProtocol
= 0x01, /* keyboard */
236 .descs
= (USBDescOther
[]) {
239 .data
= (uint8_t[]) {
240 0x09, /* u8 bLength */
241 USB_DT_HID
, /* u8 bDescriptorType */
242 0x11, 0x01, /* u16 HID_class */
243 0x00, /* u8 country_code */
244 0x01, /* u8 num_descriptors */
245 USB_DT_REPORT
, /* u8 type: Report */
246 0x3f, 0, /* u16 len */
250 .eps
= (USBDescEndpoint
[]) {
252 .bEndpointAddress
= USB_DIR_IN
| 0x01,
253 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
255 .bInterval
= 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
260 static const USBDescDevice desc_device_mouse
= {
262 .bMaxPacketSize0
= 8,
263 .bNumConfigurations
= 1,
264 .confs
= (USBDescConfig
[]) {
267 .bConfigurationValue
= 1,
268 .iConfiguration
= STR_CONFIG_MOUSE
,
269 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
272 .ifs
= &desc_iface_mouse
,
277 static const USBDescDevice desc_device_mouse2
= {
279 .bMaxPacketSize0
= 64,
280 .bNumConfigurations
= 1,
281 .confs
= (USBDescConfig
[]) {
284 .bConfigurationValue
= 1,
285 .iConfiguration
= STR_CONFIG_MOUSE
,
286 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
289 .ifs
= &desc_iface_mouse2
,
294 static const USBDescDevice desc_device_tablet
= {
296 .bMaxPacketSize0
= 8,
297 .bNumConfigurations
= 1,
298 .confs
= (USBDescConfig
[]) {
301 .bConfigurationValue
= 1,
302 .iConfiguration
= STR_CONFIG_TABLET
,
303 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
306 .ifs
= &desc_iface_tablet
,
311 static const USBDescDevice desc_device_tablet2
= {
313 .bMaxPacketSize0
= 64,
314 .bNumConfigurations
= 1,
315 .confs
= (USBDescConfig
[]) {
318 .bConfigurationValue
= 1,
319 .iConfiguration
= STR_CONFIG_TABLET
,
320 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
323 .ifs
= &desc_iface_tablet2
,
328 static const USBDescDevice desc_device_keyboard
= {
330 .bMaxPacketSize0
= 8,
331 .bNumConfigurations
= 1,
332 .confs
= (USBDescConfig
[]) {
335 .bConfigurationValue
= 1,
336 .iConfiguration
= STR_CONFIG_KEYBOARD
,
337 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
340 .ifs
= &desc_iface_keyboard
,
345 static const USBDescDevice desc_device_keyboard2
= {
347 .bMaxPacketSize0
= 64,
348 .bNumConfigurations
= 1,
349 .confs
= (USBDescConfig
[]) {
352 .bConfigurationValue
= 1,
353 .iConfiguration
= STR_CONFIG_KEYBOARD
,
354 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_WAKEUP
,
357 .ifs
= &desc_iface_keyboard2
,
362 static const USBDescMSOS desc_msos_suspend
= {
363 .SelectiveSuspendEnabled
= true,
366 static const USBDesc desc_mouse
= {
371 .iManufacturer
= STR_MANUFACTURER
,
372 .iProduct
= STR_PRODUCT_MOUSE
,
373 .iSerialNumber
= STR_SERIALNUMBER
,
375 .full
= &desc_device_mouse
,
377 .msos
= &desc_msos_suspend
,
380 static const USBDesc desc_mouse2
= {
385 .iManufacturer
= STR_MANUFACTURER
,
386 .iProduct
= STR_PRODUCT_MOUSE
,
387 .iSerialNumber
= STR_SERIALNUMBER
,
389 .full
= &desc_device_mouse
,
390 .high
= &desc_device_mouse2
,
392 .msos
= &desc_msos_suspend
,
395 static const USBDesc desc_tablet
= {
400 .iManufacturer
= STR_MANUFACTURER
,
401 .iProduct
= STR_PRODUCT_TABLET
,
402 .iSerialNumber
= STR_SERIALNUMBER
,
404 .full
= &desc_device_tablet
,
406 .msos
= &desc_msos_suspend
,
409 static const USBDesc desc_tablet2
= {
414 .iManufacturer
= STR_MANUFACTURER
,
415 .iProduct
= STR_PRODUCT_TABLET
,
416 .iSerialNumber
= STR_SERIALNUMBER
,
418 .full
= &desc_device_tablet
,
419 .high
= &desc_device_tablet2
,
421 .msos
= &desc_msos_suspend
,
424 static const USBDesc desc_keyboard
= {
429 .iManufacturer
= STR_MANUFACTURER
,
430 .iProduct
= STR_PRODUCT_KEYBOARD
,
431 .iSerialNumber
= STR_SERIALNUMBER
,
433 .full
= &desc_device_keyboard
,
435 .msos
= &desc_msos_suspend
,
438 static const USBDesc desc_keyboard2
= {
443 .iManufacturer
= STR_MANUFACTURER
,
444 .iProduct
= STR_PRODUCT_KEYBOARD
,
445 .iSerialNumber
= STR_SERIALNUMBER
,
447 .full
= &desc_device_keyboard
,
448 .high
= &desc_device_keyboard2
,
450 .msos
= &desc_msos_suspend
,
453 static const uint8_t qemu_mouse_hid_report_descriptor
[] = {
454 0x05, 0x01, /* Usage Page (Generic Desktop) */
455 0x09, 0x02, /* Usage (Mouse) */
456 0xa1, 0x01, /* Collection (Application) */
457 0x09, 0x01, /* Usage (Pointer) */
458 0xa1, 0x00, /* Collection (Physical) */
459 0x05, 0x09, /* Usage Page (Button) */
460 0x19, 0x01, /* Usage Minimum (1) */
461 0x29, 0x03, /* Usage Maximum (3) */
462 0x15, 0x00, /* Logical Minimum (0) */
463 0x25, 0x01, /* Logical Maximum (1) */
464 0x95, 0x03, /* Report Count (3) */
465 0x75, 0x01, /* Report Size (1) */
466 0x81, 0x02, /* Input (Data, Variable, Absolute) */
467 0x95, 0x01, /* Report Count (1) */
468 0x75, 0x05, /* Report Size (5) */
469 0x81, 0x01, /* Input (Constant) */
470 0x05, 0x01, /* Usage Page (Generic Desktop) */
471 0x09, 0x30, /* Usage (X) */
472 0x09, 0x31, /* Usage (Y) */
473 0x09, 0x38, /* Usage (Wheel) */
474 0x15, 0x81, /* Logical Minimum (-0x7f) */
475 0x25, 0x7f, /* Logical Maximum (0x7f) */
476 0x75, 0x08, /* Report Size (8) */
477 0x95, 0x03, /* Report Count (3) */
478 0x81, 0x06, /* Input (Data, Variable, Relative) */
479 0xc0, /* End Collection */
480 0xc0, /* End Collection */
483 static const uint8_t qemu_tablet_hid_report_descriptor
[] = {
484 0x05, 0x01, /* Usage Page (Generic Desktop) */
485 0x09, 0x01, /* Usage (Pointer) */
486 0xa1, 0x01, /* Collection (Application) */
487 0x09, 0x01, /* Usage (Pointer) */
488 0xa1, 0x00, /* Collection (Physical) */
489 0x05, 0x09, /* Usage Page (Button) */
490 0x19, 0x01, /* Usage Minimum (1) */
491 0x29, 0x03, /* Usage Maximum (3) */
492 0x15, 0x00, /* Logical Minimum (0) */
493 0x25, 0x01, /* Logical Maximum (1) */
494 0x95, 0x03, /* Report Count (3) */
495 0x75, 0x01, /* Report Size (1) */
496 0x81, 0x02, /* Input (Data, Variable, Absolute) */
497 0x95, 0x01, /* Report Count (1) */
498 0x75, 0x05, /* Report Size (5) */
499 0x81, 0x01, /* Input (Constant) */
500 0x05, 0x01, /* Usage Page (Generic Desktop) */
501 0x09, 0x30, /* Usage (X) */
502 0x09, 0x31, /* Usage (Y) */
503 0x15, 0x00, /* Logical Minimum (0) */
504 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
505 0x35, 0x00, /* Physical Minimum (0) */
506 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
507 0x75, 0x10, /* Report Size (16) */
508 0x95, 0x02, /* Report Count (2) */
509 0x81, 0x02, /* Input (Data, Variable, Absolute) */
510 0x05, 0x01, /* Usage Page (Generic Desktop) */
511 0x09, 0x38, /* Usage (Wheel) */
512 0x15, 0x81, /* Logical Minimum (-0x7f) */
513 0x25, 0x7f, /* Logical Maximum (0x7f) */
514 0x35, 0x00, /* Physical Minimum (same as logical) */
515 0x45, 0x00, /* Physical Maximum (same as logical) */
516 0x75, 0x08, /* Report Size (8) */
517 0x95, 0x01, /* Report Count (1) */
518 0x81, 0x06, /* Input (Data, Variable, Relative) */
519 0xc0, /* End Collection */
520 0xc0, /* End Collection */
523 static const uint8_t qemu_keyboard_hid_report_descriptor
[] = {
524 0x05, 0x01, /* Usage Page (Generic Desktop) */
525 0x09, 0x06, /* Usage (Keyboard) */
526 0xa1, 0x01, /* Collection (Application) */
527 0x75, 0x01, /* Report Size (1) */
528 0x95, 0x08, /* Report Count (8) */
529 0x05, 0x07, /* Usage Page (Key Codes) */
530 0x19, 0xe0, /* Usage Minimum (224) */
531 0x29, 0xe7, /* Usage Maximum (231) */
532 0x15, 0x00, /* Logical Minimum (0) */
533 0x25, 0x01, /* Logical Maximum (1) */
534 0x81, 0x02, /* Input (Data, Variable, Absolute) */
535 0x95, 0x01, /* Report Count (1) */
536 0x75, 0x08, /* Report Size (8) */
537 0x81, 0x01, /* Input (Constant) */
538 0x95, 0x05, /* Report Count (5) */
539 0x75, 0x01, /* Report Size (1) */
540 0x05, 0x08, /* Usage Page (LEDs) */
541 0x19, 0x01, /* Usage Minimum (1) */
542 0x29, 0x05, /* Usage Maximum (5) */
543 0x91, 0x02, /* Output (Data, Variable, Absolute) */
544 0x95, 0x01, /* Report Count (1) */
545 0x75, 0x03, /* Report Size (3) */
546 0x91, 0x01, /* Output (Constant) */
547 0x95, 0x06, /* Report Count (6) */
548 0x75, 0x08, /* Report Size (8) */
549 0x15, 0x00, /* Logical Minimum (0) */
550 0x25, 0xff, /* Logical Maximum (255) */
551 0x05, 0x07, /* Usage Page (Key Codes) */
552 0x19, 0x00, /* Usage Minimum (0) */
553 0x29, 0xff, /* Usage Maximum (255) */
554 0x81, 0x00, /* Input (Data, Array) */
555 0xc0, /* End Collection */
558 static void usb_hid_changed(HIDState
*hs
)
560 USBHIDState
*us
= container_of(hs
, USBHIDState
, hid
);
562 usb_wakeup(us
->intr
, 0);
565 static void usb_hid_handle_reset(USBDevice
*dev
)
567 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
572 static void usb_hid_handle_control(USBDevice
*dev
, USBPacket
*p
,
573 int request
, int value
, int index
, int length
, uint8_t *data
)
575 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
576 HIDState
*hs
= &us
->hid
;
579 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
585 /* hid specific requests */
586 case InterfaceRequest
| USB_REQ_GET_DESCRIPTOR
:
587 switch (value
>> 8) {
589 if (hs
->kind
== HID_MOUSE
) {
590 memcpy(data
, qemu_mouse_hid_report_descriptor
,
591 sizeof(qemu_mouse_hid_report_descriptor
));
592 p
->actual_length
= sizeof(qemu_mouse_hid_report_descriptor
);
593 } else if (hs
->kind
== HID_TABLET
) {
594 memcpy(data
, qemu_tablet_hid_report_descriptor
,
595 sizeof(qemu_tablet_hid_report_descriptor
));
596 p
->actual_length
= sizeof(qemu_tablet_hid_report_descriptor
);
597 } else if (hs
->kind
== HID_KEYBOARD
) {
598 memcpy(data
, qemu_keyboard_hid_report_descriptor
,
599 sizeof(qemu_keyboard_hid_report_descriptor
));
600 p
->actual_length
= sizeof(qemu_keyboard_hid_report_descriptor
);
608 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
609 p
->actual_length
= hid_pointer_poll(hs
, data
, length
);
610 } else if (hs
->kind
== HID_KEYBOARD
) {
611 p
->actual_length
= hid_keyboard_poll(hs
, data
, length
);
615 if (hs
->kind
== HID_KEYBOARD
) {
616 p
->actual_length
= hid_keyboard_write(hs
, data
, length
);
622 if (hs
->kind
!= HID_KEYBOARD
&& hs
->kind
!= HID_MOUSE
) {
625 data
[0] = hs
->protocol
;
626 p
->actual_length
= 1;
629 if (hs
->kind
!= HID_KEYBOARD
&& hs
->kind
!= HID_MOUSE
) {
632 hs
->protocol
= value
;
636 p
->actual_length
= 1;
639 hs
->idle
= (uint8_t) (value
>> 8);
640 hid_set_next_idle(hs
);
641 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
642 hid_pointer_activate(hs
);
647 p
->status
= USB_RET_STALL
;
652 static void usb_hid_handle_data(USBDevice
*dev
, USBPacket
*p
)
654 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
655 HIDState
*hs
= &us
->hid
;
656 uint8_t buf
[p
->iov
.size
];
661 if (p
->ep
->nr
== 1) {
662 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
663 hid_pointer_activate(hs
);
665 if (!hid_has_events(hs
)) {
666 p
->status
= USB_RET_NAK
;
669 hid_set_next_idle(hs
);
670 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
671 len
= hid_pointer_poll(hs
, buf
, p
->iov
.size
);
672 } else if (hs
->kind
== HID_KEYBOARD
) {
673 len
= hid_keyboard_poll(hs
, buf
, p
->iov
.size
);
675 usb_packet_copy(p
, buf
, len
);
683 p
->status
= USB_RET_STALL
;
688 static void usb_hid_handle_destroy(USBDevice
*dev
)
690 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
695 static void usb_hid_initfn(USBDevice
*dev
, int kind
,
696 const USBDesc
*usb1
, const USBDesc
*usb2
,
699 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
700 switch (us
->usb_version
) {
702 dev
->usb_desc
= usb1
;
705 dev
->usb_desc
= usb2
;
708 dev
->usb_desc
= NULL
;
710 if (!dev
->usb_desc
) {
711 error_setg(errp
, "Invalid usb version %d for usb hid device",
717 usb_desc_set_string(dev
, STR_SERIALNUMBER
, dev
->serial
);
720 us
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
721 hid_init(&us
->hid
, kind
, usb_hid_changed
);
722 if (us
->display
&& us
->hid
.s
) {
723 qemu_input_handler_bind(us
->hid
.s
, us
->display
, us
->head
, NULL
);
727 static void usb_tablet_realize(USBDevice
*dev
, Error
**errp
)
730 usb_hid_initfn(dev
, HID_TABLET
, &desc_tablet
, &desc_tablet2
, errp
);
733 static void usb_mouse_realize(USBDevice
*dev
, Error
**errp
)
735 usb_hid_initfn(dev
, HID_MOUSE
, &desc_mouse
, &desc_mouse2
, errp
);
738 static void usb_keyboard_realize(USBDevice
*dev
, Error
**errp
)
740 usb_hid_initfn(dev
, HID_KEYBOARD
, &desc_keyboard
, &desc_keyboard2
, errp
);
743 static int usb_ptr_post_load(void *opaque
, int version_id
)
745 USBHIDState
*s
= opaque
;
747 if (s
->dev
.remote_wakeup
) {
748 hid_pointer_activate(&s
->hid
);
753 static const VMStateDescription vmstate_usb_ptr
= {
756 .minimum_version_id
= 1,
757 .post_load
= usb_ptr_post_load
,
758 .fields
= (VMStateField
[]) {
759 VMSTATE_USB_DEVICE(dev
, USBHIDState
),
760 VMSTATE_HID_POINTER_DEVICE(hid
, USBHIDState
),
761 VMSTATE_END_OF_LIST()
765 static const VMStateDescription vmstate_usb_kbd
= {
768 .minimum_version_id
= 1,
769 .fields
= (VMStateField
[]) {
770 VMSTATE_USB_DEVICE(dev
, USBHIDState
),
771 VMSTATE_HID_KEYBOARD_DEVICE(hid
, USBHIDState
),
772 VMSTATE_END_OF_LIST()
776 static void usb_hid_class_initfn(ObjectClass
*klass
, void *data
)
778 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
780 uc
->handle_reset
= usb_hid_handle_reset
;
781 uc
->handle_control
= usb_hid_handle_control
;
782 uc
->handle_data
= usb_hid_handle_data
;
783 uc
->handle_destroy
= usb_hid_handle_destroy
;
784 uc
->handle_attach
= usb_desc_attach
;
787 static Property usb_tablet_properties
[] = {
788 DEFINE_PROP_UINT32("usb_version", USBHIDState
, usb_version
, 2),
789 DEFINE_PROP_STRING("display", USBHIDState
, display
),
790 DEFINE_PROP_UINT32("head", USBHIDState
, head
, 0),
791 DEFINE_PROP_END_OF_LIST(),
794 static void usb_tablet_class_initfn(ObjectClass
*klass
, void *data
)
796 DeviceClass
*dc
= DEVICE_CLASS(klass
);
797 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
799 usb_hid_class_initfn(klass
, data
);
800 uc
->realize
= usb_tablet_realize
;
801 uc
->product_desc
= "QEMU USB Tablet";
802 dc
->vmsd
= &vmstate_usb_ptr
;
803 dc
->props
= usb_tablet_properties
;
804 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
807 static const TypeInfo usb_tablet_info
= {
808 .name
= "usb-tablet",
809 .parent
= TYPE_USB_DEVICE
,
810 .instance_size
= sizeof(USBHIDState
),
811 .class_init
= usb_tablet_class_initfn
,
814 static Property usb_mouse_properties
[] = {
815 DEFINE_PROP_UINT32("usb_version", USBHIDState
, usb_version
, 2),
816 DEFINE_PROP_END_OF_LIST(),
819 static void usb_mouse_class_initfn(ObjectClass
*klass
, void *data
)
821 DeviceClass
*dc
= DEVICE_CLASS(klass
);
822 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
824 usb_hid_class_initfn(klass
, data
);
825 uc
->realize
= usb_mouse_realize
;
826 uc
->product_desc
= "QEMU USB Mouse";
827 dc
->vmsd
= &vmstate_usb_ptr
;
828 dc
->props
= usb_mouse_properties
;
829 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
832 static const TypeInfo usb_mouse_info
= {
834 .parent
= TYPE_USB_DEVICE
,
835 .instance_size
= sizeof(USBHIDState
),
836 .class_init
= usb_mouse_class_initfn
,
839 static Property usb_keyboard_properties
[] = {
840 DEFINE_PROP_UINT32("usb_version", USBHIDState
, usb_version
, 2),
841 DEFINE_PROP_STRING("display", USBHIDState
, display
),
842 DEFINE_PROP_END_OF_LIST(),
845 static void usb_keyboard_class_initfn(ObjectClass
*klass
, void *data
)
847 DeviceClass
*dc
= DEVICE_CLASS(klass
);
848 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
850 usb_hid_class_initfn(klass
, data
);
851 uc
->realize
= usb_keyboard_realize
;
852 uc
->product_desc
= "QEMU USB Keyboard";
853 dc
->vmsd
= &vmstate_usb_kbd
;
854 dc
->props
= usb_keyboard_properties
;
855 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
858 static const TypeInfo usb_keyboard_info
= {
860 .parent
= TYPE_USB_DEVICE
,
861 .instance_size
= sizeof(USBHIDState
),
862 .class_init
= usb_keyboard_class_initfn
,
865 static void usb_hid_register_types(void)
867 type_register_static(&usb_tablet_info
);
868 usb_legacy_register("usb-tablet", "tablet", NULL
);
869 type_register_static(&usb_mouse_info
);
870 usb_legacy_register("usb-mouse", "mouse", NULL
);
871 type_register_static(&usb_keyboard_info
);
872 usb_legacy_register("usb-kbd", "keyboard", NULL
);
875 type_init(usb_hid_register_types
)