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
{
61 static const USBDescStrings desc_strings
= {
62 [STR_MANUFACTURER
] = "QEMU " QEMU_VERSION
,
63 [STR_PRODUCT_MOUSE
] = "QEMU USB Mouse",
64 [STR_PRODUCT_TABLET
] = "QEMU USB Tablet",
65 [STR_PRODUCT_KEYBOARD
] = "QEMU USB Keyboard",
66 [STR_SERIALNUMBER
] = "42", /* == remote wakeup works */
67 [STR_CONFIG_MOUSE
] = "HID Mouse",
68 [STR_CONFIG_TABLET
] = "HID Tablet",
69 [STR_CONFIG_KEYBOARD
] = "HID Keyboard",
72 static const USBDescIface desc_iface_mouse
= {
73 .bInterfaceNumber
= 0,
75 .bInterfaceClass
= USB_CLASS_HID
,
76 .bInterfaceSubClass
= 0x01, /* boot */
77 .bInterfaceProtocol
= 0x02,
79 .descs
= (USBDescOther
[]) {
83 0x09, /* u8 bLength */
84 USB_DT_HID
, /* u8 bDescriptorType */
85 0x01, 0x00, /* u16 HID_class */
86 0x00, /* u8 country_code */
87 0x01, /* u8 num_descriptors */
88 USB_DT_REPORT
, /* u8 type: Report */
93 .eps
= (USBDescEndpoint
[]) {
95 .bEndpointAddress
= USB_DIR_IN
| 0x01,
96 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
103 static const USBDescIface desc_iface_tablet
= {
104 .bInterfaceNumber
= 0,
106 .bInterfaceClass
= USB_CLASS_HID
,
107 .bInterfaceProtocol
= 0x02,
109 .descs
= (USBDescOther
[]) {
112 .data
= (uint8_t[]) {
113 0x09, /* u8 bLength */
114 USB_DT_HID
, /* u8 bDescriptorType */
115 0x01, 0x00, /* u16 HID_class */
116 0x00, /* u8 country_code */
117 0x01, /* u8 num_descriptors */
118 USB_DT_REPORT
, /* u8 type: Report */
123 .eps
= (USBDescEndpoint
[]) {
125 .bEndpointAddress
= USB_DIR_IN
| 0x01,
126 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
133 static const USBDescIface desc_iface_keyboard
= {
134 .bInterfaceNumber
= 0,
136 .bInterfaceClass
= USB_CLASS_HID
,
137 .bInterfaceSubClass
= 0x01, /* boot */
138 .bInterfaceProtocol
= 0x01, /* keyboard */
140 .descs
= (USBDescOther
[]) {
143 .data
= (uint8_t[]) {
144 0x09, /* u8 bLength */
145 USB_DT_HID
, /* u8 bDescriptorType */
146 0x11, 0x01, /* u16 HID_class */
147 0x00, /* u8 country_code */
148 0x01, /* u8 num_descriptors */
149 USB_DT_REPORT
, /* u8 type: Report */
150 0x3f, 0, /* u16 len */
154 .eps
= (USBDescEndpoint
[]) {
156 .bEndpointAddress
= USB_DIR_IN
| 0x01,
157 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
164 static const USBDescDevice desc_device_mouse
= {
166 .bMaxPacketSize0
= 8,
167 .bNumConfigurations
= 1,
168 .confs
= (USBDescConfig
[]) {
171 .bConfigurationValue
= 1,
172 .iConfiguration
= STR_CONFIG_MOUSE
,
173 .bmAttributes
= 0xa0,
176 .ifs
= &desc_iface_mouse
,
181 static const USBDescDevice desc_device_tablet
= {
183 .bMaxPacketSize0
= 8,
184 .bNumConfigurations
= 1,
185 .confs
= (USBDescConfig
[]) {
188 .bConfigurationValue
= 1,
189 .iConfiguration
= STR_CONFIG_TABLET
,
190 .bmAttributes
= 0xa0,
193 .ifs
= &desc_iface_tablet
,
198 static const USBDescDevice desc_device_keyboard
= {
200 .bMaxPacketSize0
= 8,
201 .bNumConfigurations
= 1,
202 .confs
= (USBDescConfig
[]) {
205 .bConfigurationValue
= 1,
206 .iConfiguration
= STR_CONFIG_KEYBOARD
,
207 .bmAttributes
= 0xa0,
210 .ifs
= &desc_iface_keyboard
,
215 static const USBDesc desc_mouse
= {
220 .iManufacturer
= STR_MANUFACTURER
,
221 .iProduct
= STR_PRODUCT_MOUSE
,
222 .iSerialNumber
= STR_SERIALNUMBER
,
224 .full
= &desc_device_mouse
,
228 static const USBDesc desc_tablet
= {
233 .iManufacturer
= STR_MANUFACTURER
,
234 .iProduct
= STR_PRODUCT_TABLET
,
235 .iSerialNumber
= STR_SERIALNUMBER
,
237 .full
= &desc_device_tablet
,
241 static const USBDesc desc_keyboard
= {
246 .iManufacturer
= STR_MANUFACTURER
,
247 .iProduct
= STR_PRODUCT_KEYBOARD
,
248 .iSerialNumber
= STR_SERIALNUMBER
,
250 .full
= &desc_device_keyboard
,
254 static const uint8_t qemu_mouse_hid_report_descriptor
[] = {
255 0x05, 0x01, /* Usage Page (Generic Desktop) */
256 0x09, 0x02, /* Usage (Mouse) */
257 0xa1, 0x01, /* Collection (Application) */
258 0x09, 0x01, /* Usage (Pointer) */
259 0xa1, 0x00, /* Collection (Physical) */
260 0x05, 0x09, /* Usage Page (Button) */
261 0x19, 0x01, /* Usage Minimum (1) */
262 0x29, 0x03, /* Usage Maximum (3) */
263 0x15, 0x00, /* Logical Minimum (0) */
264 0x25, 0x01, /* Logical Maximum (1) */
265 0x95, 0x03, /* Report Count (3) */
266 0x75, 0x01, /* Report Size (1) */
267 0x81, 0x02, /* Input (Data, Variable, Absolute) */
268 0x95, 0x01, /* Report Count (1) */
269 0x75, 0x05, /* Report Size (5) */
270 0x81, 0x01, /* Input (Constant) */
271 0x05, 0x01, /* Usage Page (Generic Desktop) */
272 0x09, 0x30, /* Usage (X) */
273 0x09, 0x31, /* Usage (Y) */
274 0x09, 0x38, /* Usage (Wheel) */
275 0x15, 0x81, /* Logical Minimum (-0x7f) */
276 0x25, 0x7f, /* Logical Maximum (0x7f) */
277 0x75, 0x08, /* Report Size (8) */
278 0x95, 0x03, /* Report Count (3) */
279 0x81, 0x06, /* Input (Data, Variable, Relative) */
280 0xc0, /* End Collection */
281 0xc0, /* End Collection */
284 static const uint8_t qemu_tablet_hid_report_descriptor
[] = {
285 0x05, 0x01, /* Usage Page (Generic Desktop) */
286 0x09, 0x01, /* Usage (Pointer) */
287 0xa1, 0x01, /* Collection (Application) */
288 0x09, 0x01, /* Usage (Pointer) */
289 0xa1, 0x00, /* Collection (Physical) */
290 0x05, 0x09, /* Usage Page (Button) */
291 0x19, 0x01, /* Usage Minimum (1) */
292 0x29, 0x03, /* Usage Maximum (3) */
293 0x15, 0x00, /* Logical Minimum (0) */
294 0x25, 0x01, /* Logical Maximum (1) */
295 0x95, 0x03, /* Report Count (3) */
296 0x75, 0x01, /* Report Size (1) */
297 0x81, 0x02, /* Input (Data, Variable, Absolute) */
298 0x95, 0x01, /* Report Count (1) */
299 0x75, 0x05, /* Report Size (5) */
300 0x81, 0x01, /* Input (Constant) */
301 0x05, 0x01, /* Usage Page (Generic Desktop) */
302 0x09, 0x30, /* Usage (X) */
303 0x09, 0x31, /* Usage (Y) */
304 0x15, 0x00, /* Logical Minimum (0) */
305 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
306 0x35, 0x00, /* Physical Minimum (0) */
307 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
308 0x75, 0x10, /* Report Size (16) */
309 0x95, 0x02, /* Report Count (2) */
310 0x81, 0x02, /* Input (Data, Variable, Absolute) */
311 0x05, 0x01, /* Usage Page (Generic Desktop) */
312 0x09, 0x38, /* Usage (Wheel) */
313 0x15, 0x81, /* Logical Minimum (-0x7f) */
314 0x25, 0x7f, /* Logical Maximum (0x7f) */
315 0x35, 0x00, /* Physical Minimum (same as logical) */
316 0x45, 0x00, /* Physical Maximum (same as logical) */
317 0x75, 0x08, /* Report Size (8) */
318 0x95, 0x01, /* Report Count (1) */
319 0x81, 0x06, /* Input (Data, Variable, Relative) */
320 0xc0, /* End Collection */
321 0xc0, /* End Collection */
324 static const uint8_t qemu_keyboard_hid_report_descriptor
[] = {
325 0x05, 0x01, /* Usage Page (Generic Desktop) */
326 0x09, 0x06, /* Usage (Keyboard) */
327 0xa1, 0x01, /* Collection (Application) */
328 0x75, 0x01, /* Report Size (1) */
329 0x95, 0x08, /* Report Count (8) */
330 0x05, 0x07, /* Usage Page (Key Codes) */
331 0x19, 0xe0, /* Usage Minimum (224) */
332 0x29, 0xe7, /* Usage Maximum (231) */
333 0x15, 0x00, /* Logical Minimum (0) */
334 0x25, 0x01, /* Logical Maximum (1) */
335 0x81, 0x02, /* Input (Data, Variable, Absolute) */
336 0x95, 0x01, /* Report Count (1) */
337 0x75, 0x08, /* Report Size (8) */
338 0x81, 0x01, /* Input (Constant) */
339 0x95, 0x05, /* Report Count (5) */
340 0x75, 0x01, /* Report Size (1) */
341 0x05, 0x08, /* Usage Page (LEDs) */
342 0x19, 0x01, /* Usage Minimum (1) */
343 0x29, 0x05, /* Usage Maximum (5) */
344 0x91, 0x02, /* Output (Data, Variable, Absolute) */
345 0x95, 0x01, /* Report Count (1) */
346 0x75, 0x03, /* Report Size (3) */
347 0x91, 0x01, /* Output (Constant) */
348 0x95, 0x06, /* Report Count (6) */
349 0x75, 0x08, /* Report Size (8) */
350 0x15, 0x00, /* Logical Minimum (0) */
351 0x25, 0xff, /* Logical Maximum (255) */
352 0x05, 0x07, /* Usage Page (Key Codes) */
353 0x19, 0x00, /* Usage Minimum (0) */
354 0x29, 0xff, /* Usage Maximum (255) */
355 0x81, 0x00, /* Input (Data, Array) */
356 0xc0, /* End Collection */
359 static void usb_hid_changed(HIDState
*hs
)
361 USBHIDState
*us
= container_of(hs
, USBHIDState
, hid
);
363 usb_wakeup(&us
->dev
);
366 static void usb_hid_handle_reset(USBDevice
*dev
)
368 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
373 static int usb_hid_handle_control(USBDevice
*dev
, USBPacket
*p
,
374 int request
, int value
, int index
, int length
, uint8_t *data
)
376 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
377 HIDState
*hs
= &us
->hid
;
380 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
387 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
391 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
394 /* hid specific requests */
395 case InterfaceRequest
| USB_REQ_GET_DESCRIPTOR
:
396 switch (value
>> 8) {
398 if (hs
->kind
== HID_MOUSE
) {
399 memcpy(data
, qemu_mouse_hid_report_descriptor
,
400 sizeof(qemu_mouse_hid_report_descriptor
));
401 ret
= sizeof(qemu_mouse_hid_report_descriptor
);
402 } else if (hs
->kind
== HID_TABLET
) {
403 memcpy(data
, qemu_tablet_hid_report_descriptor
,
404 sizeof(qemu_tablet_hid_report_descriptor
));
405 ret
= sizeof(qemu_tablet_hid_report_descriptor
);
406 } else if (hs
->kind
== HID_KEYBOARD
) {
407 memcpy(data
, qemu_keyboard_hid_report_descriptor
,
408 sizeof(qemu_keyboard_hid_report_descriptor
));
409 ret
= sizeof(qemu_keyboard_hid_report_descriptor
);
417 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
418 ret
= hid_pointer_poll(hs
, data
, length
);
419 } else if (hs
->kind
== HID_KEYBOARD
) {
420 ret
= hid_keyboard_poll(hs
, data
, length
);
424 if (hs
->kind
== HID_KEYBOARD
) {
425 ret
= hid_keyboard_write(hs
, data
, length
);
431 if (hs
->kind
!= HID_KEYBOARD
&& hs
->kind
!= HID_MOUSE
) {
435 data
[0] = hs
->protocol
;
438 if (hs
->kind
!= HID_KEYBOARD
&& hs
->kind
!= HID_MOUSE
) {
442 hs
->protocol
= value
;
449 hs
->idle
= (uint8_t) (value
>> 8);
450 hid_set_next_idle(hs
, qemu_get_clock_ns(vm_clock
));
451 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
452 hid_pointer_activate(hs
);
464 static int usb_hid_handle_data(USBDevice
*dev
, USBPacket
*p
)
466 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
467 HIDState
*hs
= &us
->hid
;
468 uint8_t buf
[p
->iov
.size
];
474 int64_t curtime
= qemu_get_clock_ns(vm_clock
);
475 if (!hid_has_events(hs
) &&
476 (!hs
->idle
|| hs
->next_idle_clock
- curtime
> 0)) {
479 hid_set_next_idle(hs
, curtime
);
480 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
481 ret
= hid_pointer_poll(hs
, buf
, p
->iov
.size
);
482 } else if (hs
->kind
== HID_KEYBOARD
) {
483 ret
= hid_keyboard_poll(hs
, buf
, p
->iov
.size
);
485 usb_packet_copy(p
, buf
, ret
);
499 static void usb_hid_handle_destroy(USBDevice
*dev
)
501 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
506 static int usb_hid_initfn(USBDevice
*dev
, int kind
)
508 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
511 hid_init(&us
->hid
, kind
, usb_hid_changed
);
515 static int usb_tablet_initfn(USBDevice
*dev
)
517 return usb_hid_initfn(dev
, HID_TABLET
);
520 static int usb_mouse_initfn(USBDevice
*dev
)
522 return usb_hid_initfn(dev
, HID_MOUSE
);
525 static int usb_keyboard_initfn(USBDevice
*dev
)
527 return usb_hid_initfn(dev
, HID_KEYBOARD
);
530 static const VMStateDescription vmstate_usb_ptr
= {
533 .minimum_version_id
= 1,
534 .fields
= (VMStateField
[]) {
535 VMSTATE_USB_DEVICE(dev
, USBHIDState
),
536 VMSTATE_HID_POINTER_DEVICE(hid
, USBHIDState
),
537 VMSTATE_END_OF_LIST()
541 static const VMStateDescription vmstate_usb_kbd
= {
544 .minimum_version_id
= 1,
545 .fields
= (VMStateField
[]) {
546 VMSTATE_USB_DEVICE(dev
, USBHIDState
),
547 VMSTATE_HID_KEYBOARD_DEVICE(hid
, USBHIDState
),
548 VMSTATE_END_OF_LIST()
552 static struct USBDeviceInfo hid_info
[] = {
554 .product_desc
= "QEMU USB Tablet",
555 .qdev
.name
= "usb-tablet",
556 .usbdevice_name
= "tablet",
557 .qdev
.size
= sizeof(USBHIDState
),
558 .qdev
.vmsd
= &vmstate_usb_ptr
,
559 .usb_desc
= &desc_tablet
,
560 .init
= usb_tablet_initfn
,
561 .handle_packet
= usb_generic_handle_packet
,
562 .handle_reset
= usb_hid_handle_reset
,
563 .handle_control
= usb_hid_handle_control
,
564 .handle_data
= usb_hid_handle_data
,
565 .handle_destroy
= usb_hid_handle_destroy
,
567 .product_desc
= "QEMU USB Mouse",
568 .qdev
.name
= "usb-mouse",
569 .usbdevice_name
= "mouse",
570 .qdev
.size
= sizeof(USBHIDState
),
571 .qdev
.vmsd
= &vmstate_usb_ptr
,
572 .usb_desc
= &desc_mouse
,
573 .init
= usb_mouse_initfn
,
574 .handle_packet
= usb_generic_handle_packet
,
575 .handle_reset
= usb_hid_handle_reset
,
576 .handle_control
= usb_hid_handle_control
,
577 .handle_data
= usb_hid_handle_data
,
578 .handle_destroy
= usb_hid_handle_destroy
,
580 .product_desc
= "QEMU USB Keyboard",
581 .qdev
.name
= "usb-kbd",
582 .usbdevice_name
= "keyboard",
583 .qdev
.size
= sizeof(USBHIDState
),
584 .qdev
.vmsd
= &vmstate_usb_kbd
,
585 .usb_desc
= &desc_keyboard
,
586 .init
= usb_keyboard_initfn
,
587 .handle_packet
= usb_generic_handle_packet
,
588 .handle_reset
= usb_hid_handle_reset
,
589 .handle_control
= usb_hid_handle_control
,
590 .handle_data
= usb_hid_handle_data
,
591 .handle_destroy
= usb_hid_handle_destroy
,
597 static void usb_hid_register_devices(void)
599 usb_qdev_register_many(hid_info
);
601 device_init(usb_hid_register_devices
)