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
29 #include "qemu-timer.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
{
49 void (*datain
)(void *);
63 static const USBDescStrings desc_strings
= {
64 [STR_MANUFACTURER
] = "QEMU " QEMU_VERSION
,
65 [STR_PRODUCT_MOUSE
] = "QEMU USB Mouse",
66 [STR_PRODUCT_TABLET
] = "QEMU USB Tablet",
67 [STR_PRODUCT_KEYBOARD
] = "QEMU USB Keyboard",
68 [STR_SERIALNUMBER
] = "42", /* == remote wakeup works */
69 [STR_CONFIG_MOUSE
] = "HID Mouse",
70 [STR_CONFIG_TABLET
] = "HID Tablet",
71 [STR_CONFIG_KEYBOARD
] = "HID Keyboard",
74 static const USBDescIface desc_iface_mouse
= {
75 .bInterfaceNumber
= 0,
77 .bInterfaceClass
= USB_CLASS_HID
,
78 .bInterfaceSubClass
= 0x01, /* boot */
79 .bInterfaceProtocol
= 0x02,
81 .descs
= (USBDescOther
[]) {
85 0x09, /* u8 bLength */
86 USB_DT_HID
, /* u8 bDescriptorType */
87 0x01, 0x00, /* u16 HID_class */
88 0x00, /* u8 country_code */
89 0x01, /* u8 num_descriptors */
90 USB_DT_REPORT
, /* u8 type: Report */
95 .eps
= (USBDescEndpoint
[]) {
97 .bEndpointAddress
= USB_DIR_IN
| 0x01,
98 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
105 static const USBDescIface desc_iface_tablet
= {
106 .bInterfaceNumber
= 0,
108 .bInterfaceClass
= USB_CLASS_HID
,
109 .bInterfaceProtocol
= 0x02,
111 .descs
= (USBDescOther
[]) {
114 .data
= (uint8_t[]) {
115 0x09, /* u8 bLength */
116 USB_DT_HID
, /* u8 bDescriptorType */
117 0x01, 0x00, /* u16 HID_class */
118 0x00, /* u8 country_code */
119 0x01, /* u8 num_descriptors */
120 USB_DT_REPORT
, /* u8 type: Report */
125 .eps
= (USBDescEndpoint
[]) {
127 .bEndpointAddress
= USB_DIR_IN
| 0x01,
128 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
135 static const USBDescIface desc_iface_keyboard
= {
136 .bInterfaceNumber
= 0,
138 .bInterfaceClass
= USB_CLASS_HID
,
139 .bInterfaceSubClass
= 0x01, /* boot */
140 .bInterfaceProtocol
= 0x01, /* keyboard */
142 .descs
= (USBDescOther
[]) {
145 .data
= (uint8_t[]) {
146 0x09, /* u8 bLength */
147 USB_DT_HID
, /* u8 bDescriptorType */
148 0x11, 0x01, /* u16 HID_class */
149 0x00, /* u8 country_code */
150 0x01, /* u8 num_descriptors */
151 USB_DT_REPORT
, /* u8 type: Report */
152 0x3f, 0, /* u16 len */
156 .eps
= (USBDescEndpoint
[]) {
158 .bEndpointAddress
= USB_DIR_IN
| 0x01,
159 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
166 static const USBDescDevice desc_device_mouse
= {
168 .bMaxPacketSize0
= 8,
169 .bNumConfigurations
= 1,
170 .confs
= (USBDescConfig
[]) {
173 .bConfigurationValue
= 1,
174 .iConfiguration
= STR_CONFIG_MOUSE
,
175 .bmAttributes
= 0xa0,
178 .ifs
= &desc_iface_mouse
,
183 static const USBDescDevice desc_device_tablet
= {
185 .bMaxPacketSize0
= 8,
186 .bNumConfigurations
= 1,
187 .confs
= (USBDescConfig
[]) {
190 .bConfigurationValue
= 1,
191 .iConfiguration
= STR_CONFIG_TABLET
,
192 .bmAttributes
= 0xa0,
195 .ifs
= &desc_iface_tablet
,
200 static const USBDescDevice desc_device_keyboard
= {
202 .bMaxPacketSize0
= 8,
203 .bNumConfigurations
= 1,
204 .confs
= (USBDescConfig
[]) {
207 .bConfigurationValue
= 1,
208 .iConfiguration
= STR_CONFIG_KEYBOARD
,
209 .bmAttributes
= 0xa0,
212 .ifs
= &desc_iface_keyboard
,
217 static const USBDesc desc_mouse
= {
222 .iManufacturer
= STR_MANUFACTURER
,
223 .iProduct
= STR_PRODUCT_MOUSE
,
224 .iSerialNumber
= STR_SERIALNUMBER
,
226 .full
= &desc_device_mouse
,
230 static const USBDesc desc_tablet
= {
235 .iManufacturer
= STR_MANUFACTURER
,
236 .iProduct
= STR_PRODUCT_TABLET
,
237 .iSerialNumber
= STR_SERIALNUMBER
,
239 .full
= &desc_device_tablet
,
243 static const USBDesc desc_keyboard
= {
248 .iManufacturer
= STR_MANUFACTURER
,
249 .iProduct
= STR_PRODUCT_KEYBOARD
,
250 .iSerialNumber
= STR_SERIALNUMBER
,
252 .full
= &desc_device_keyboard
,
256 static const uint8_t qemu_mouse_hid_report_descriptor
[] = {
257 0x05, 0x01, /* Usage Page (Generic Desktop) */
258 0x09, 0x02, /* Usage (Mouse) */
259 0xa1, 0x01, /* Collection (Application) */
260 0x09, 0x01, /* Usage (Pointer) */
261 0xa1, 0x00, /* Collection (Physical) */
262 0x05, 0x09, /* Usage Page (Button) */
263 0x19, 0x01, /* Usage Minimum (1) */
264 0x29, 0x03, /* Usage Maximum (3) */
265 0x15, 0x00, /* Logical Minimum (0) */
266 0x25, 0x01, /* Logical Maximum (1) */
267 0x95, 0x03, /* Report Count (3) */
268 0x75, 0x01, /* Report Size (1) */
269 0x81, 0x02, /* Input (Data, Variable, Absolute) */
270 0x95, 0x01, /* Report Count (1) */
271 0x75, 0x05, /* Report Size (5) */
272 0x81, 0x01, /* Input (Constant) */
273 0x05, 0x01, /* Usage Page (Generic Desktop) */
274 0x09, 0x30, /* Usage (X) */
275 0x09, 0x31, /* Usage (Y) */
276 0x09, 0x38, /* Usage (Wheel) */
277 0x15, 0x81, /* Logical Minimum (-0x7f) */
278 0x25, 0x7f, /* Logical Maximum (0x7f) */
279 0x75, 0x08, /* Report Size (8) */
280 0x95, 0x03, /* Report Count (3) */
281 0x81, 0x06, /* Input (Data, Variable, Relative) */
282 0xc0, /* End Collection */
283 0xc0, /* End Collection */
286 static const uint8_t qemu_tablet_hid_report_descriptor
[] = {
287 0x05, 0x01, /* Usage Page (Generic Desktop) */
288 0x09, 0x01, /* Usage (Pointer) */
289 0xa1, 0x01, /* Collection (Application) */
290 0x09, 0x01, /* Usage (Pointer) */
291 0xa1, 0x00, /* Collection (Physical) */
292 0x05, 0x09, /* Usage Page (Button) */
293 0x19, 0x01, /* Usage Minimum (1) */
294 0x29, 0x03, /* Usage Maximum (3) */
295 0x15, 0x00, /* Logical Minimum (0) */
296 0x25, 0x01, /* Logical Maximum (1) */
297 0x95, 0x03, /* Report Count (3) */
298 0x75, 0x01, /* Report Size (1) */
299 0x81, 0x02, /* Input (Data, Variable, Absolute) */
300 0x95, 0x01, /* Report Count (1) */
301 0x75, 0x05, /* Report Size (5) */
302 0x81, 0x01, /* Input (Constant) */
303 0x05, 0x01, /* Usage Page (Generic Desktop) */
304 0x09, 0x30, /* Usage (X) */
305 0x09, 0x31, /* Usage (Y) */
306 0x15, 0x00, /* Logical Minimum (0) */
307 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
308 0x35, 0x00, /* Physical Minimum (0) */
309 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
310 0x75, 0x10, /* Report Size (16) */
311 0x95, 0x02, /* Report Count (2) */
312 0x81, 0x02, /* Input (Data, Variable, Absolute) */
313 0x05, 0x01, /* Usage Page (Generic Desktop) */
314 0x09, 0x38, /* Usage (Wheel) */
315 0x15, 0x81, /* Logical Minimum (-0x7f) */
316 0x25, 0x7f, /* Logical Maximum (0x7f) */
317 0x35, 0x00, /* Physical Minimum (same as logical) */
318 0x45, 0x00, /* Physical Maximum (same as logical) */
319 0x75, 0x08, /* Report Size (8) */
320 0x95, 0x01, /* Report Count (1) */
321 0x81, 0x06, /* Input (Data, Variable, Relative) */
322 0xc0, /* End Collection */
323 0xc0, /* End Collection */
326 static const uint8_t qemu_keyboard_hid_report_descriptor
[] = {
327 0x05, 0x01, /* Usage Page (Generic Desktop) */
328 0x09, 0x06, /* Usage (Keyboard) */
329 0xa1, 0x01, /* Collection (Application) */
330 0x75, 0x01, /* Report Size (1) */
331 0x95, 0x08, /* Report Count (8) */
332 0x05, 0x07, /* Usage Page (Key Codes) */
333 0x19, 0xe0, /* Usage Minimum (224) */
334 0x29, 0xe7, /* Usage Maximum (231) */
335 0x15, 0x00, /* Logical Minimum (0) */
336 0x25, 0x01, /* Logical Maximum (1) */
337 0x81, 0x02, /* Input (Data, Variable, Absolute) */
338 0x95, 0x01, /* Report Count (1) */
339 0x75, 0x08, /* Report Size (8) */
340 0x81, 0x01, /* Input (Constant) */
341 0x95, 0x05, /* Report Count (5) */
342 0x75, 0x01, /* Report Size (1) */
343 0x05, 0x08, /* Usage Page (LEDs) */
344 0x19, 0x01, /* Usage Minimum (1) */
345 0x29, 0x05, /* Usage Maximum (5) */
346 0x91, 0x02, /* Output (Data, Variable, Absolute) */
347 0x95, 0x01, /* Report Count (1) */
348 0x75, 0x03, /* Report Size (3) */
349 0x91, 0x01, /* Output (Constant) */
350 0x95, 0x06, /* Report Count (6) */
351 0x75, 0x08, /* Report Size (8) */
352 0x15, 0x00, /* Logical Minimum (0) */
353 0x25, 0xff, /* Logical Maximum (255) */
354 0x05, 0x07, /* Usage Page (Key Codes) */
355 0x19, 0x00, /* Usage Minimum (0) */
356 0x29, 0xff, /* Usage Maximum (255) */
357 0x81, 0x00, /* Input (Data, Array) */
358 0xc0, /* End Collection */
361 static void usb_hid_changed(HIDState
*hs
)
363 USBHIDState
*us
= container_of(hs
, USBHIDState
, hid
);
366 us
->datain(us
->datain_opaque
);
369 usb_wakeup(&us
->dev
);
372 static void usb_hid_handle_reset(USBDevice
*dev
)
374 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
379 static int usb_hid_handle_control(USBDevice
*dev
, USBPacket
*p
,
380 int request
, int value
, int index
, int length
, uint8_t *data
)
382 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
383 HIDState
*hs
= &us
->hid
;
386 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
393 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
397 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
400 /* hid specific requests */
401 case InterfaceRequest
| USB_REQ_GET_DESCRIPTOR
:
402 switch (value
>> 8) {
404 if (hs
->kind
== HID_MOUSE
) {
405 memcpy(data
, qemu_mouse_hid_report_descriptor
,
406 sizeof(qemu_mouse_hid_report_descriptor
));
407 ret
= sizeof(qemu_mouse_hid_report_descriptor
);
408 } else if (hs
->kind
== HID_TABLET
) {
409 memcpy(data
, qemu_tablet_hid_report_descriptor
,
410 sizeof(qemu_tablet_hid_report_descriptor
));
411 ret
= sizeof(qemu_tablet_hid_report_descriptor
);
412 } else if (hs
->kind
== HID_KEYBOARD
) {
413 memcpy(data
, qemu_keyboard_hid_report_descriptor
,
414 sizeof(qemu_keyboard_hid_report_descriptor
));
415 ret
= sizeof(qemu_keyboard_hid_report_descriptor
);
423 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
424 ret
= hid_pointer_poll(hs
, data
, length
);
425 } else if (hs
->kind
== HID_KEYBOARD
) {
426 ret
= hid_keyboard_poll(hs
, data
, length
);
430 if (hs
->kind
== HID_KEYBOARD
) {
431 ret
= hid_keyboard_write(hs
, data
, length
);
437 if (hs
->kind
!= HID_KEYBOARD
&& hs
->kind
!= HID_MOUSE
) {
441 data
[0] = hs
->protocol
;
444 if (hs
->kind
!= HID_KEYBOARD
&& hs
->kind
!= HID_MOUSE
) {
448 hs
->protocol
= value
;
455 hs
->idle
= (uint8_t) (value
>> 8);
456 hid_set_next_idle(hs
, qemu_get_clock_ns(vm_clock
));
467 static int usb_hid_handle_data(USBDevice
*dev
, USBPacket
*p
)
469 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
470 HIDState
*hs
= &us
->hid
;
471 uint8_t buf
[p
->iov
.size
];
477 int64_t curtime
= qemu_get_clock_ns(vm_clock
);
478 if (!hid_has_events(hs
) &&
479 (!hs
->idle
|| hs
->next_idle_clock
- curtime
> 0)) {
482 hid_set_next_idle(hs
, curtime
);
483 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
484 ret
= hid_pointer_poll(hs
, buf
, p
->iov
.size
);
485 } else if (hs
->kind
== HID_KEYBOARD
) {
486 ret
= hid_keyboard_poll(hs
, buf
, p
->iov
.size
);
488 usb_packet_copy(p
, buf
, ret
);
502 static void usb_hid_handle_destroy(USBDevice
*dev
)
504 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
509 static int usb_hid_initfn(USBDevice
*dev
, int kind
)
511 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
514 hid_init(&us
->hid
, kind
, usb_hid_changed
);
518 static int usb_tablet_initfn(USBDevice
*dev
)
520 return usb_hid_initfn(dev
, HID_TABLET
);
523 static int usb_mouse_initfn(USBDevice
*dev
)
525 return usb_hid_initfn(dev
, HID_MOUSE
);
528 static int usb_keyboard_initfn(USBDevice
*dev
)
530 return usb_hid_initfn(dev
, HID_KEYBOARD
);
533 void usb_hid_datain_cb(USBDevice
*dev
, void *opaque
, void (*datain
)(void *))
535 USBHIDState
*s
= (USBHIDState
*)dev
;
537 s
->datain_opaque
= opaque
;
541 static int usb_hid_post_load(void *opaque
, int version_id
)
543 USBHIDState
*s
= opaque
;
546 hid_set_next_idle(&s
->hid
, qemu_get_clock_ns(vm_clock
));
551 static const VMStateDescription vmstate_usb_ptr_queue
= {
552 .name
= "usb-ptr-queue",
554 .minimum_version_id
= 1,
555 .fields
= (VMStateField
[]) {
556 VMSTATE_INT32(xdx
, HIDPointerEvent
),
557 VMSTATE_INT32(ydy
, HIDPointerEvent
),
558 VMSTATE_INT32(dz
, HIDPointerEvent
),
559 VMSTATE_INT32(buttons_state
, HIDPointerEvent
),
560 VMSTATE_END_OF_LIST()
563 static const VMStateDescription vmstate_usb_ptr
= {
566 .minimum_version_id
= 1,
567 .post_load
= usb_hid_post_load
,
568 .fields
= (VMStateField
[]) {
569 VMSTATE_USB_DEVICE(dev
, USBHIDState
),
570 VMSTATE_STRUCT_ARRAY(hid
.ptr
.queue
, USBHIDState
, QUEUE_LENGTH
, 0,
571 vmstate_usb_ptr_queue
, HIDPointerEvent
),
572 VMSTATE_UINT32(hid
.head
, USBHIDState
),
573 VMSTATE_UINT32(hid
.n
, USBHIDState
),
574 VMSTATE_INT32(hid
.protocol
, USBHIDState
),
575 VMSTATE_UINT8(hid
.idle
, USBHIDState
),
576 VMSTATE_END_OF_LIST()
580 static const VMStateDescription vmstate_usb_kbd
= {
583 .minimum_version_id
= 1,
584 .post_load
= usb_hid_post_load
,
585 .fields
= (VMStateField
[]) {
586 VMSTATE_USB_DEVICE(dev
, USBHIDState
),
587 VMSTATE_UINT32_ARRAY(hid
.kbd
.keycodes
, USBHIDState
, QUEUE_LENGTH
),
588 VMSTATE_UINT32(hid
.head
, USBHIDState
),
589 VMSTATE_UINT32(hid
.n
, USBHIDState
),
590 VMSTATE_UINT16(hid
.kbd
.modifiers
, USBHIDState
),
591 VMSTATE_UINT8(hid
.kbd
.leds
, USBHIDState
),
592 VMSTATE_UINT8_ARRAY(hid
.kbd
.key
, USBHIDState
, 16),
593 VMSTATE_INT32(hid
.kbd
.keys
, USBHIDState
),
594 VMSTATE_INT32(hid
.protocol
, USBHIDState
),
595 VMSTATE_UINT8(hid
.idle
, USBHIDState
),
596 VMSTATE_END_OF_LIST()
600 static struct USBDeviceInfo hid_info
[] = {
602 .product_desc
= "QEMU USB Tablet",
603 .qdev
.name
= "usb-tablet",
604 .usbdevice_name
= "tablet",
605 .qdev
.size
= sizeof(USBHIDState
),
606 .qdev
.vmsd
= &vmstate_usb_ptr
,
607 .usb_desc
= &desc_tablet
,
608 .init
= usb_tablet_initfn
,
609 .handle_packet
= usb_generic_handle_packet
,
610 .handle_reset
= usb_hid_handle_reset
,
611 .handle_control
= usb_hid_handle_control
,
612 .handle_data
= usb_hid_handle_data
,
613 .handle_destroy
= usb_hid_handle_destroy
,
615 .product_desc
= "QEMU USB Mouse",
616 .qdev
.name
= "usb-mouse",
617 .usbdevice_name
= "mouse",
618 .qdev
.size
= sizeof(USBHIDState
),
619 .qdev
.vmsd
= &vmstate_usb_ptr
,
620 .usb_desc
= &desc_mouse
,
621 .init
= usb_mouse_initfn
,
622 .handle_packet
= usb_generic_handle_packet
,
623 .handle_reset
= usb_hid_handle_reset
,
624 .handle_control
= usb_hid_handle_control
,
625 .handle_data
= usb_hid_handle_data
,
626 .handle_destroy
= usb_hid_handle_destroy
,
628 .product_desc
= "QEMU USB Keyboard",
629 .qdev
.name
= "usb-kbd",
630 .usbdevice_name
= "keyboard",
631 .qdev
.size
= sizeof(USBHIDState
),
632 .qdev
.vmsd
= &vmstate_usb_kbd
,
633 .usb_desc
= &desc_keyboard
,
634 .init
= usb_keyboard_initfn
,
635 .handle_packet
= usb_generic_handle_packet
,
636 .handle_reset
= usb_hid_handle_reset
,
637 .handle_control
= usb_hid_handle_control
,
638 .handle_data
= usb_hid_handle_data
,
639 .handle_destroy
= usb_hid_handle_destroy
,
645 static void usb_hid_register_devices(void)
647 usb_qdev_register_many(hid_info
);
649 device_init(usb_hid_register_devices
)