26
#include "qemu/osdep.h"
27
#include "ui/console.h"
29
#include "migration/vmstate.h"
31
#include "qapi/error.h"
32
#include "qemu/module.h"
33
#include "qemu/timer.h"
34
#include "hw/input/hid.h"
35
#include "hw/usb/hid.h"
36
#include "hw/qdev-properties.h"
37
#include "qom/object.h"
48
#define TYPE_USB_HID "usb-hid"
49
OBJECT_DECLARE_SIMPLE_TYPE(USBHIDState, USB_HID)
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_SERIAL_COMPAT] = "42",
71
[STR_CONFIG_MOUSE] = "HID Mouse",
72
[STR_CONFIG_TABLET] = "HID Tablet",
73
[STR_CONFIG_KEYBOARD] = "HID Keyboard",
74
[STR_SERIAL_MOUSE] = "89126",
75
[STR_SERIAL_TABLET] = "28754",
76
[STR_SERIAL_KEYBOARD] = "68284",
79
static const USBDescIface desc_iface_mouse = {
80
.bInterfaceNumber = 0,
82
.bInterfaceClass = USB_CLASS_HID,
83
.bInterfaceSubClass = 0x01,
84
.bInterfaceProtocol = 0x02,
86
.descs = (USBDescOther[]) {
100
.eps = (USBDescEndpoint[]) {
102
.bEndpointAddress = USB_DIR_IN | 0x01,
103
.bmAttributes = USB_ENDPOINT_XFER_INT,
110
static const USBDescIface desc_iface_mouse2 = {
111
.bInterfaceNumber = 0,
113
.bInterfaceClass = USB_CLASS_HID,
114
.bInterfaceSubClass = 0x01,
115
.bInterfaceProtocol = 0x02,
117
.descs = (USBDescOther[]) {
120
.data = (uint8_t[]) {
131
.eps = (USBDescEndpoint[]) {
133
.bEndpointAddress = USB_DIR_IN | 0x01,
134
.bmAttributes = USB_ENDPOINT_XFER_INT,
141
static const USBDescIface desc_iface_tablet = {
142
.bInterfaceNumber = 0,
144
.bInterfaceClass = USB_CLASS_HID,
145
.bInterfaceProtocol = 0x00,
147
.descs = (USBDescOther[]) {
150
.data = (uint8_t[]) {
161
.eps = (USBDescEndpoint[]) {
163
.bEndpointAddress = USB_DIR_IN | 0x01,
164
.bmAttributes = USB_ENDPOINT_XFER_INT,
171
static const USBDescIface desc_iface_tablet2 = {
172
.bInterfaceNumber = 0,
174
.bInterfaceClass = USB_CLASS_HID,
175
.bInterfaceProtocol = 0x00,
177
.descs = (USBDescOther[]) {
180
.data = (uint8_t[]) {
191
.eps = (USBDescEndpoint[]) {
193
.bEndpointAddress = USB_DIR_IN | 0x01,
194
.bmAttributes = USB_ENDPOINT_XFER_INT,
201
static const USBDescIface desc_iface_keyboard = {
202
.bInterfaceNumber = 0,
204
.bInterfaceClass = USB_CLASS_HID,
205
.bInterfaceSubClass = 0x01,
206
.bInterfaceProtocol = 0x01,
208
.descs = (USBDescOther[]) {
211
.data = (uint8_t[]) {
222
.eps = (USBDescEndpoint[]) {
224
.bEndpointAddress = USB_DIR_IN | 0x01,
225
.bmAttributes = USB_ENDPOINT_XFER_INT,
232
static const USBDescIface desc_iface_keyboard2 = {
233
.bInterfaceNumber = 0,
235
.bInterfaceClass = USB_CLASS_HID,
236
.bInterfaceSubClass = 0x01,
237
.bInterfaceProtocol = 0x01,
239
.descs = (USBDescOther[]) {
242
.data = (uint8_t[]) {
253
.eps = (USBDescEndpoint[]) {
255
.bEndpointAddress = USB_DIR_IN | 0x01,
256
.bmAttributes = USB_ENDPOINT_XFER_INT,
263
static const USBDescDevice desc_device_mouse = {
265
.bMaxPacketSize0 = 8,
266
.bNumConfigurations = 1,
267
.confs = (USBDescConfig[]) {
270
.bConfigurationValue = 1,
271
.iConfiguration = STR_CONFIG_MOUSE,
272
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
275
.ifs = &desc_iface_mouse,
280
static const USBDescDevice desc_device_mouse2 = {
282
.bMaxPacketSize0 = 64,
283
.bNumConfigurations = 1,
284
.confs = (USBDescConfig[]) {
287
.bConfigurationValue = 1,
288
.iConfiguration = STR_CONFIG_MOUSE,
289
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
292
.ifs = &desc_iface_mouse2,
297
static const USBDescDevice desc_device_tablet = {
299
.bMaxPacketSize0 = 8,
300
.bNumConfigurations = 1,
301
.confs = (USBDescConfig[]) {
304
.bConfigurationValue = 1,
305
.iConfiguration = STR_CONFIG_TABLET,
306
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
309
.ifs = &desc_iface_tablet,
314
static const USBDescDevice desc_device_tablet2 = {
316
.bMaxPacketSize0 = 64,
317
.bNumConfigurations = 1,
318
.confs = (USBDescConfig[]) {
321
.bConfigurationValue = 1,
322
.iConfiguration = STR_CONFIG_TABLET,
323
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
326
.ifs = &desc_iface_tablet2,
331
static const USBDescDevice desc_device_keyboard = {
333
.bMaxPacketSize0 = 8,
334
.bNumConfigurations = 1,
335
.confs = (USBDescConfig[]) {
338
.bConfigurationValue = 1,
339
.iConfiguration = STR_CONFIG_KEYBOARD,
340
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
343
.ifs = &desc_iface_keyboard,
348
static const USBDescDevice desc_device_keyboard2 = {
350
.bMaxPacketSize0 = 64,
351
.bNumConfigurations = 1,
352
.confs = (USBDescConfig[]) {
355
.bConfigurationValue = 1,
356
.iConfiguration = STR_CONFIG_KEYBOARD,
357
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
360
.ifs = &desc_iface_keyboard2,
365
static const USBDescMSOS desc_msos_suspend = {
366
.SelectiveSuspendEnabled = true,
369
static const USBDesc desc_mouse = {
374
.iManufacturer = STR_MANUFACTURER,
375
.iProduct = STR_PRODUCT_MOUSE,
376
.iSerialNumber = STR_SERIAL_MOUSE,
378
.full = &desc_device_mouse,
380
.msos = &desc_msos_suspend,
383
static const USBDesc desc_mouse2 = {
388
.iManufacturer = STR_MANUFACTURER,
389
.iProduct = STR_PRODUCT_MOUSE,
390
.iSerialNumber = STR_SERIAL_MOUSE,
392
.full = &desc_device_mouse,
393
.high = &desc_device_mouse2,
395
.msos = &desc_msos_suspend,
398
static const USBDesc desc_tablet = {
403
.iManufacturer = STR_MANUFACTURER,
404
.iProduct = STR_PRODUCT_TABLET,
405
.iSerialNumber = STR_SERIAL_TABLET,
407
.full = &desc_device_tablet,
409
.msos = &desc_msos_suspend,
412
static const USBDesc desc_tablet2 = {
417
.iManufacturer = STR_MANUFACTURER,
418
.iProduct = STR_PRODUCT_TABLET,
419
.iSerialNumber = STR_SERIAL_TABLET,
421
.full = &desc_device_tablet,
422
.high = &desc_device_tablet2,
424
.msos = &desc_msos_suspend,
427
static const USBDesc desc_keyboard = {
432
.iManufacturer = STR_MANUFACTURER,
433
.iProduct = STR_PRODUCT_KEYBOARD,
434
.iSerialNumber = STR_SERIAL_KEYBOARD,
436
.full = &desc_device_keyboard,
438
.msos = &desc_msos_suspend,
441
static const USBDesc desc_keyboard2 = {
446
.iManufacturer = STR_MANUFACTURER,
447
.iProduct = STR_PRODUCT_KEYBOARD,
448
.iSerialNumber = STR_SERIAL_KEYBOARD,
450
.full = &desc_device_keyboard,
451
.high = &desc_device_keyboard2,
453
.msos = &desc_msos_suspend,
456
static const uint8_t qemu_mouse_hid_report_descriptor[] = {
486
static const uint8_t qemu_tablet_hid_report_descriptor[] = {
526
static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
561
static void usb_hid_changed(HIDState *hs)
563
USBHIDState *us = container_of(hs, USBHIDState, hid);
565
usb_wakeup(us->intr, 0);
568
static void usb_hid_handle_reset(USBDevice *dev)
570
USBHIDState *us = USB_HID(dev);
575
static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
576
int request, int value, int index, int length, uint8_t *data)
578
USBHIDState *us = USB_HID(dev);
579
HIDState *hs = &us->hid;
582
ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
589
case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
590
switch (value >> 8) {
592
if (hs->kind == HID_MOUSE) {
593
memcpy(data, qemu_mouse_hid_report_descriptor,
594
sizeof(qemu_mouse_hid_report_descriptor));
595
p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
596
} else if (hs->kind == HID_TABLET) {
597
memcpy(data, qemu_tablet_hid_report_descriptor,
598
sizeof(qemu_tablet_hid_report_descriptor));
599
p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
600
} else if (hs->kind == HID_KEYBOARD) {
601
memcpy(data, qemu_keyboard_hid_report_descriptor,
602
sizeof(qemu_keyboard_hid_report_descriptor));
603
p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
611
if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
612
p->actual_length = hid_pointer_poll(hs, data, length);
613
} else if (hs->kind == HID_KEYBOARD) {
614
p->actual_length = hid_keyboard_poll(hs, data, length);
618
if (hs->kind == HID_KEYBOARD) {
619
p->actual_length = hid_keyboard_write(hs, data, length);
624
case HID_GET_PROTOCOL:
625
if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
628
data[0] = hs->protocol;
629
p->actual_length = 1;
631
case HID_SET_PROTOCOL:
632
if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
635
hs->protocol = value;
639
p->actual_length = 1;
642
hs->idle = (uint8_t) (value >> 8);
643
hid_set_next_idle(hs);
644
if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
645
hid_pointer_activate(hs);
650
p->status = USB_RET_STALL;
655
static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
657
USBHIDState *us = USB_HID(dev);
658
HIDState *hs = &us->hid;
659
g_autofree uint8_t *buf = g_malloc(p->iov.size);
664
if (p->ep->nr == 1) {
665
if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
666
hid_pointer_activate(hs);
668
if (!hid_has_events(hs)) {
669
p->status = USB_RET_NAK;
672
hid_set_next_idle(hs);
673
if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
674
len = hid_pointer_poll(hs, buf, p->iov.size);
675
} else if (hs->kind == HID_KEYBOARD) {
676
len = hid_keyboard_poll(hs, buf, p->iov.size);
678
usb_packet_copy(p, buf, len);
686
p->status = USB_RET_STALL;
691
static void usb_hid_unrealize(USBDevice *dev)
693
USBHIDState *us = USB_HID(dev);
698
static void usb_hid_initfn(USBDevice *dev, int kind,
699
const USBDesc *usb1, const USBDesc *usb2,
702
USBHIDState *us = USB_HID(dev);
703
switch (us->usb_version) {
705
dev->usb_desc = usb1;
708
dev->usb_desc = usb2;
711
dev->usb_desc = NULL;
713
if (!dev->usb_desc) {
714
error_setg(errp, "Invalid usb version %d for usb hid device",
719
usb_desc_create_serial(dev);
721
us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
722
hid_init(&us->hid, kind, usb_hid_changed);
723
if (us->display && us->hid.s) {
724
qemu_input_handler_bind(us->hid.s, us->display, us->head, NULL);
728
static void usb_tablet_realize(USBDevice *dev, Error **errp)
731
usb_hid_initfn(dev, HID_TABLET, &desc_tablet, &desc_tablet2, errp);
734
static void usb_mouse_realize(USBDevice *dev, Error **errp)
736
usb_hid_initfn(dev, HID_MOUSE, &desc_mouse, &desc_mouse2, errp);
739
static void usb_keyboard_realize(USBDevice *dev, Error **errp)
741
usb_hid_initfn(dev, HID_KEYBOARD, &desc_keyboard, &desc_keyboard2, errp);
744
static int usb_ptr_post_load(void *opaque, int version_id)
746
USBHIDState *s = opaque;
748
if (s->dev.remote_wakeup) {
749
hid_pointer_activate(&s->hid);
754
static const VMStateDescription vmstate_usb_ptr = {
757
.minimum_version_id = 1,
758
.post_load = usb_ptr_post_load,
759
.fields = (const VMStateField[]) {
760
VMSTATE_USB_DEVICE(dev, USBHIDState),
761
VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
762
VMSTATE_END_OF_LIST()
766
static const VMStateDescription vmstate_usb_kbd = {
769
.minimum_version_id = 1,
770
.fields = (const VMStateField[]) {
771
VMSTATE_USB_DEVICE(dev, USBHIDState),
772
VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
773
VMSTATE_END_OF_LIST()
777
static void usb_hid_class_initfn(ObjectClass *klass, void *data)
779
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
781
uc->handle_reset = usb_hid_handle_reset;
782
uc->handle_control = usb_hid_handle_control;
783
uc->handle_data = usb_hid_handle_data;
784
uc->unrealize = usb_hid_unrealize;
785
uc->handle_attach = usb_desc_attach;
788
static const TypeInfo usb_hid_type_info = {
789
.name = TYPE_USB_HID,
790
.parent = TYPE_USB_DEVICE,
791
.instance_size = sizeof(USBHIDState),
793
.class_init = usb_hid_class_initfn,
796
static Property usb_tablet_properties[] = {
797
DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
798
DEFINE_PROP_STRING("display", USBHIDState, display),
799
DEFINE_PROP_UINT32("head", USBHIDState, head, 0),
800
DEFINE_PROP_END_OF_LIST(),
803
static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
805
DeviceClass *dc = DEVICE_CLASS(klass);
806
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
808
uc->realize = usb_tablet_realize;
809
uc->product_desc = "QEMU USB Tablet";
810
dc->vmsd = &vmstate_usb_ptr;
811
device_class_set_props(dc, usb_tablet_properties);
812
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
815
static const TypeInfo usb_tablet_info = {
816
.name = "usb-tablet",
817
.parent = TYPE_USB_HID,
818
.class_init = usb_tablet_class_initfn,
821
static Property usb_mouse_properties[] = {
822
DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
823
DEFINE_PROP_END_OF_LIST(),
826
static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
828
DeviceClass *dc = DEVICE_CLASS(klass);
829
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
831
uc->realize = usb_mouse_realize;
832
uc->product_desc = "QEMU USB Mouse";
833
dc->vmsd = &vmstate_usb_ptr;
834
device_class_set_props(dc, usb_mouse_properties);
835
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
838
static const TypeInfo usb_mouse_info = {
840
.parent = TYPE_USB_HID,
841
.class_init = usb_mouse_class_initfn,
844
static Property usb_keyboard_properties[] = {
845
DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
846
DEFINE_PROP_STRING("display", USBHIDState, display),
847
DEFINE_PROP_END_OF_LIST(),
850
static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
852
DeviceClass *dc = DEVICE_CLASS(klass);
853
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
855
uc->realize = usb_keyboard_realize;
856
uc->product_desc = "QEMU USB Keyboard";
857
dc->vmsd = &vmstate_usb_kbd;
858
device_class_set_props(dc, usb_keyboard_properties);
859
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
862
static const TypeInfo usb_keyboard_info = {
864
.parent = TYPE_USB_HID,
865
.class_init = usb_keyboard_class_initfn,
868
static void usb_hid_register_types(void)
870
type_register_static(&usb_hid_type_info);
871
type_register_static(&usb_tablet_info);
872
usb_legacy_register("usb-tablet", "tablet", NULL);
873
type_register_static(&usb_mouse_info);
874
usb_legacy_register("usb-mouse", "mouse", NULL);
875
type_register_static(&usb_keyboard_info);
876
usb_legacy_register("usb-kbd", "keyboard", NULL);
879
type_init(usb_hid_register_types)