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
28 #include "hw/usb/desc.h"
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
{
62 static const USBDescStrings desc_strings
= {
63 [STR_MANUFACTURER
] = "QEMU " QEMU_VERSION
,
64 [STR_PRODUCT_MOUSE
] = "QEMU USB Mouse",
65 [STR_PRODUCT_TABLET
] = "QEMU USB Tablet",
66 [STR_PRODUCT_KEYBOARD
] = "QEMU USB Keyboard",
67 [STR_SERIALNUMBER
] = "42", /* == remote wakeup works */
68 [STR_CONFIG_MOUSE
] = "HID Mouse",
69 [STR_CONFIG_TABLET
] = "HID Tablet",
70 [STR_CONFIG_KEYBOARD
] = "HID Keyboard",
73 static const USBDescIface desc_iface_mouse
= {
74 .bInterfaceNumber
= 0,
76 .bInterfaceClass
= USB_CLASS_HID
,
77 .bInterfaceSubClass
= 0x01, /* boot */
78 .bInterfaceProtocol
= 0x02,
80 .descs
= (USBDescOther
[]) {
84 0x09, /* u8 bLength */
85 USB_DT_HID
, /* u8 bDescriptorType */
86 0x01, 0x00, /* u16 HID_class */
87 0x00, /* u8 country_code */
88 0x01, /* u8 num_descriptors */
89 USB_DT_REPORT
, /* u8 type: Report */
94 .eps
= (USBDescEndpoint
[]) {
96 .bEndpointAddress
= USB_DIR_IN
| 0x01,
97 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
104 static const USBDescIface desc_iface_tablet
= {
105 .bInterfaceNumber
= 0,
107 .bInterfaceClass
= USB_CLASS_HID
,
108 .bInterfaceProtocol
= 0x02,
110 .descs
= (USBDescOther
[]) {
113 .data
= (uint8_t[]) {
114 0x09, /* u8 bLength */
115 USB_DT_HID
, /* u8 bDescriptorType */
116 0x01, 0x00, /* u16 HID_class */
117 0x00, /* u8 country_code */
118 0x01, /* u8 num_descriptors */
119 USB_DT_REPORT
, /* u8 type: Report */
124 .eps
= (USBDescEndpoint
[]) {
126 .bEndpointAddress
= USB_DIR_IN
| 0x01,
127 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
134 static const USBDescIface desc_iface_keyboard
= {
135 .bInterfaceNumber
= 0,
137 .bInterfaceClass
= USB_CLASS_HID
,
138 .bInterfaceSubClass
= 0x01, /* boot */
139 .bInterfaceProtocol
= 0x01, /* keyboard */
141 .descs
= (USBDescOther
[]) {
144 .data
= (uint8_t[]) {
145 0x09, /* u8 bLength */
146 USB_DT_HID
, /* u8 bDescriptorType */
147 0x11, 0x01, /* u16 HID_class */
148 0x00, /* u8 country_code */
149 0x01, /* u8 num_descriptors */
150 USB_DT_REPORT
, /* u8 type: Report */
151 0x3f, 0, /* u16 len */
155 .eps
= (USBDescEndpoint
[]) {
157 .bEndpointAddress
= USB_DIR_IN
| 0x01,
158 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
165 static const USBDescDevice desc_device_mouse
= {
167 .bMaxPacketSize0
= 8,
168 .bNumConfigurations
= 1,
169 .confs
= (USBDescConfig
[]) {
172 .bConfigurationValue
= 1,
173 .iConfiguration
= STR_CONFIG_MOUSE
,
174 .bmAttributes
= 0xa0,
177 .ifs
= &desc_iface_mouse
,
182 static const USBDescDevice desc_device_tablet
= {
184 .bMaxPacketSize0
= 8,
185 .bNumConfigurations
= 1,
186 .confs
= (USBDescConfig
[]) {
189 .bConfigurationValue
= 1,
190 .iConfiguration
= STR_CONFIG_TABLET
,
191 .bmAttributes
= 0xa0,
194 .ifs
= &desc_iface_tablet
,
199 static const USBDescDevice desc_device_keyboard
= {
201 .bMaxPacketSize0
= 8,
202 .bNumConfigurations
= 1,
203 .confs
= (USBDescConfig
[]) {
206 .bConfigurationValue
= 1,
207 .iConfiguration
= STR_CONFIG_KEYBOARD
,
208 .bmAttributes
= 0xa0,
211 .ifs
= &desc_iface_keyboard
,
216 static const USBDesc desc_mouse
= {
221 .iManufacturer
= STR_MANUFACTURER
,
222 .iProduct
= STR_PRODUCT_MOUSE
,
223 .iSerialNumber
= STR_SERIALNUMBER
,
225 .full
= &desc_device_mouse
,
229 static const USBDesc desc_tablet
= {
234 .iManufacturer
= STR_MANUFACTURER
,
235 .iProduct
= STR_PRODUCT_TABLET
,
236 .iSerialNumber
= STR_SERIALNUMBER
,
238 .full
= &desc_device_tablet
,
242 static const USBDesc desc_keyboard
= {
247 .iManufacturer
= STR_MANUFACTURER
,
248 .iProduct
= STR_PRODUCT_KEYBOARD
,
249 .iSerialNumber
= STR_SERIALNUMBER
,
251 .full
= &desc_device_keyboard
,
255 static const uint8_t qemu_mouse_hid_report_descriptor
[] = {
256 0x05, 0x01, /* Usage Page (Generic Desktop) */
257 0x09, 0x02, /* Usage (Mouse) */
258 0xa1, 0x01, /* Collection (Application) */
259 0x09, 0x01, /* Usage (Pointer) */
260 0xa1, 0x00, /* Collection (Physical) */
261 0x05, 0x09, /* Usage Page (Button) */
262 0x19, 0x01, /* Usage Minimum (1) */
263 0x29, 0x03, /* Usage Maximum (3) */
264 0x15, 0x00, /* Logical Minimum (0) */
265 0x25, 0x01, /* Logical Maximum (1) */
266 0x95, 0x03, /* Report Count (3) */
267 0x75, 0x01, /* Report Size (1) */
268 0x81, 0x02, /* Input (Data, Variable, Absolute) */
269 0x95, 0x01, /* Report Count (1) */
270 0x75, 0x05, /* Report Size (5) */
271 0x81, 0x01, /* Input (Constant) */
272 0x05, 0x01, /* Usage Page (Generic Desktop) */
273 0x09, 0x30, /* Usage (X) */
274 0x09, 0x31, /* Usage (Y) */
275 0x09, 0x38, /* Usage (Wheel) */
276 0x15, 0x81, /* Logical Minimum (-0x7f) */
277 0x25, 0x7f, /* Logical Maximum (0x7f) */
278 0x75, 0x08, /* Report Size (8) */
279 0x95, 0x03, /* Report Count (3) */
280 0x81, 0x06, /* Input (Data, Variable, Relative) */
281 0xc0, /* End Collection */
282 0xc0, /* End Collection */
285 static const uint8_t qemu_tablet_hid_report_descriptor
[] = {
286 0x05, 0x01, /* Usage Page (Generic Desktop) */
287 0x09, 0x01, /* Usage (Pointer) */
288 0xa1, 0x01, /* Collection (Application) */
289 0x09, 0x01, /* Usage (Pointer) */
290 0xa1, 0x00, /* Collection (Physical) */
291 0x05, 0x09, /* Usage Page (Button) */
292 0x19, 0x01, /* Usage Minimum (1) */
293 0x29, 0x03, /* Usage Maximum (3) */
294 0x15, 0x00, /* Logical Minimum (0) */
295 0x25, 0x01, /* Logical Maximum (1) */
296 0x95, 0x03, /* Report Count (3) */
297 0x75, 0x01, /* Report Size (1) */
298 0x81, 0x02, /* Input (Data, Variable, Absolute) */
299 0x95, 0x01, /* Report Count (1) */
300 0x75, 0x05, /* Report Size (5) */
301 0x81, 0x01, /* Input (Constant) */
302 0x05, 0x01, /* Usage Page (Generic Desktop) */
303 0x09, 0x30, /* Usage (X) */
304 0x09, 0x31, /* Usage (Y) */
305 0x15, 0x00, /* Logical Minimum (0) */
306 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
307 0x35, 0x00, /* Physical Minimum (0) */
308 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
309 0x75, 0x10, /* Report Size (16) */
310 0x95, 0x02, /* Report Count (2) */
311 0x81, 0x02, /* Input (Data, Variable, Absolute) */
312 0x05, 0x01, /* Usage Page (Generic Desktop) */
313 0x09, 0x38, /* Usage (Wheel) */
314 0x15, 0x81, /* Logical Minimum (-0x7f) */
315 0x25, 0x7f, /* Logical Maximum (0x7f) */
316 0x35, 0x00, /* Physical Minimum (same as logical) */
317 0x45, 0x00, /* Physical Maximum (same as logical) */
318 0x75, 0x08, /* Report Size (8) */
319 0x95, 0x01, /* Report Count (1) */
320 0x81, 0x06, /* Input (Data, Variable, Relative) */
321 0xc0, /* End Collection */
322 0xc0, /* End Collection */
325 static const uint8_t qemu_keyboard_hid_report_descriptor
[] = {
326 0x05, 0x01, /* Usage Page (Generic Desktop) */
327 0x09, 0x06, /* Usage (Keyboard) */
328 0xa1, 0x01, /* Collection (Application) */
329 0x75, 0x01, /* Report Size (1) */
330 0x95, 0x08, /* Report Count (8) */
331 0x05, 0x07, /* Usage Page (Key Codes) */
332 0x19, 0xe0, /* Usage Minimum (224) */
333 0x29, 0xe7, /* Usage Maximum (231) */
334 0x15, 0x00, /* Logical Minimum (0) */
335 0x25, 0x01, /* Logical Maximum (1) */
336 0x81, 0x02, /* Input (Data, Variable, Absolute) */
337 0x95, 0x01, /* Report Count (1) */
338 0x75, 0x08, /* Report Size (8) */
339 0x81, 0x01, /* Input (Constant) */
340 0x95, 0x05, /* Report Count (5) */
341 0x75, 0x01, /* Report Size (1) */
342 0x05, 0x08, /* Usage Page (LEDs) */
343 0x19, 0x01, /* Usage Minimum (1) */
344 0x29, 0x05, /* Usage Maximum (5) */
345 0x91, 0x02, /* Output (Data, Variable, Absolute) */
346 0x95, 0x01, /* Report Count (1) */
347 0x75, 0x03, /* Report Size (3) */
348 0x91, 0x01, /* Output (Constant) */
349 0x95, 0x06, /* Report Count (6) */
350 0x75, 0x08, /* Report Size (8) */
351 0x15, 0x00, /* Logical Minimum (0) */
352 0x25, 0xff, /* Logical Maximum (255) */
353 0x05, 0x07, /* Usage Page (Key Codes) */
354 0x19, 0x00, /* Usage Minimum (0) */
355 0x29, 0xff, /* Usage Maximum (255) */
356 0x81, 0x00, /* Input (Data, Array) */
357 0xc0, /* End Collection */
360 static void usb_hid_changed(HIDState
*hs
)
362 USBHIDState
*us
= container_of(hs
, USBHIDState
, hid
);
364 usb_wakeup(us
->intr
);
367 static void usb_hid_handle_reset(USBDevice
*dev
)
369 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
374 static int usb_hid_handle_control(USBDevice
*dev
, USBPacket
*p
,
375 int request
, int value
, int index
, int length
, uint8_t *data
)
377 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
378 HIDState
*hs
= &us
->hid
;
381 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
388 /* hid specific requests */
389 case InterfaceRequest
| USB_REQ_GET_DESCRIPTOR
:
390 switch (value
>> 8) {
392 if (hs
->kind
== HID_MOUSE
) {
393 memcpy(data
, qemu_mouse_hid_report_descriptor
,
394 sizeof(qemu_mouse_hid_report_descriptor
));
395 ret
= sizeof(qemu_mouse_hid_report_descriptor
);
396 } else if (hs
->kind
== HID_TABLET
) {
397 memcpy(data
, qemu_tablet_hid_report_descriptor
,
398 sizeof(qemu_tablet_hid_report_descriptor
));
399 ret
= sizeof(qemu_tablet_hid_report_descriptor
);
400 } else if (hs
->kind
== HID_KEYBOARD
) {
401 memcpy(data
, qemu_keyboard_hid_report_descriptor
,
402 sizeof(qemu_keyboard_hid_report_descriptor
));
403 ret
= sizeof(qemu_keyboard_hid_report_descriptor
);
411 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
412 ret
= hid_pointer_poll(hs
, data
, length
);
413 } else if (hs
->kind
== HID_KEYBOARD
) {
414 ret
= hid_keyboard_poll(hs
, data
, length
);
418 if (hs
->kind
== HID_KEYBOARD
) {
419 ret
= hid_keyboard_write(hs
, data
, length
);
425 if (hs
->kind
!= HID_KEYBOARD
&& hs
->kind
!= HID_MOUSE
) {
429 data
[0] = hs
->protocol
;
432 if (hs
->kind
!= HID_KEYBOARD
&& hs
->kind
!= HID_MOUSE
) {
436 hs
->protocol
= value
;
443 hs
->idle
= (uint8_t) (value
>> 8);
444 hid_set_next_idle(hs
, qemu_get_clock_ns(vm_clock
));
445 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
446 hid_pointer_activate(hs
);
458 static int usb_hid_handle_data(USBDevice
*dev
, USBPacket
*p
)
460 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
461 HIDState
*hs
= &us
->hid
;
462 uint8_t buf
[p
->iov
.size
];
467 if (p
->ep
->nr
== 1) {
468 int64_t curtime
= qemu_get_clock_ns(vm_clock
);
469 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
470 hid_pointer_activate(hs
);
472 if (!hid_has_events(hs
) &&
473 (!hs
->idle
|| hs
->next_idle_clock
- curtime
> 0)) {
476 hid_set_next_idle(hs
, curtime
);
477 if (hs
->kind
== HID_MOUSE
|| hs
->kind
== HID_TABLET
) {
478 ret
= hid_pointer_poll(hs
, buf
, p
->iov
.size
);
479 } else if (hs
->kind
== HID_KEYBOARD
) {
480 ret
= hid_keyboard_poll(hs
, buf
, p
->iov
.size
);
482 usb_packet_copy(p
, buf
, ret
);
496 static void usb_hid_handle_destroy(USBDevice
*dev
)
498 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
503 static int usb_hid_initfn(USBDevice
*dev
, int kind
)
505 USBHIDState
*us
= DO_UPCAST(USBHIDState
, dev
, dev
);
508 us
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
509 hid_init(&us
->hid
, kind
, usb_hid_changed
);
513 static int usb_tablet_initfn(USBDevice
*dev
)
515 return usb_hid_initfn(dev
, HID_TABLET
);
518 static int usb_mouse_initfn(USBDevice
*dev
)
520 return usb_hid_initfn(dev
, HID_MOUSE
);
523 static int usb_keyboard_initfn(USBDevice
*dev
)
525 return usb_hid_initfn(dev
, HID_KEYBOARD
);
528 static int usb_ptr_post_load(void *opaque
, int version_id
)
530 USBHIDState
*s
= opaque
;
532 if (s
->dev
.remote_wakeup
) {
533 hid_pointer_activate(&s
->hid
);
538 static const VMStateDescription vmstate_usb_ptr
= {
541 .minimum_version_id
= 1,
542 .post_load
= usb_ptr_post_load
,
543 .fields
= (VMStateField
[]) {
544 VMSTATE_USB_DEVICE(dev
, USBHIDState
),
545 VMSTATE_HID_POINTER_DEVICE(hid
, USBHIDState
),
546 VMSTATE_END_OF_LIST()
550 static const VMStateDescription vmstate_usb_kbd
= {
553 .minimum_version_id
= 1,
554 .fields
= (VMStateField
[]) {
555 VMSTATE_USB_DEVICE(dev
, USBHIDState
),
556 VMSTATE_HID_KEYBOARD_DEVICE(hid
, USBHIDState
),
557 VMSTATE_END_OF_LIST()
561 static void usb_hid_class_initfn(ObjectClass
*klass
, void *data
)
563 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
565 uc
->handle_reset
= usb_hid_handle_reset
;
566 uc
->handle_control
= usb_hid_handle_control
;
567 uc
->handle_data
= usb_hid_handle_data
;
568 uc
->handle_destroy
= usb_hid_handle_destroy
;
571 static void usb_tablet_class_initfn(ObjectClass
*klass
, void *data
)
573 DeviceClass
*dc
= DEVICE_CLASS(klass
);
574 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
576 usb_hid_class_initfn(klass
, data
);
577 uc
->init
= usb_tablet_initfn
;
578 uc
->product_desc
= "QEMU USB Tablet";
579 uc
->usb_desc
= &desc_tablet
;
580 dc
->vmsd
= &vmstate_usb_ptr
;
583 static TypeInfo usb_tablet_info
= {
584 .name
= "usb-tablet",
585 .parent
= TYPE_USB_DEVICE
,
586 .instance_size
= sizeof(USBHIDState
),
587 .class_init
= usb_tablet_class_initfn
,
590 static void usb_mouse_class_initfn(ObjectClass
*klass
, void *data
)
592 DeviceClass
*dc
= DEVICE_CLASS(klass
);
593 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
595 usb_hid_class_initfn(klass
, data
);
596 uc
->init
= usb_mouse_initfn
;
597 uc
->product_desc
= "QEMU USB Mouse";
598 uc
->usb_desc
= &desc_mouse
;
599 dc
->vmsd
= &vmstate_usb_ptr
;
602 static TypeInfo usb_mouse_info
= {
604 .parent
= TYPE_USB_DEVICE
,
605 .instance_size
= sizeof(USBHIDState
),
606 .class_init
= usb_mouse_class_initfn
,
609 static void usb_keyboard_class_initfn(ObjectClass
*klass
, void *data
)
611 DeviceClass
*dc
= DEVICE_CLASS(klass
);
612 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
614 usb_hid_class_initfn(klass
, data
);
615 uc
->init
= usb_keyboard_initfn
;
616 uc
->product_desc
= "QEMU USB Keyboard";
617 uc
->usb_desc
= &desc_keyboard
;
618 dc
->vmsd
= &vmstate_usb_kbd
;
621 static TypeInfo usb_keyboard_info
= {
623 .parent
= TYPE_USB_DEVICE
,
624 .instance_size
= sizeof(USBHIDState
),
625 .class_init
= usb_keyboard_class_initfn
,
628 static void usb_hid_register_types(void)
630 type_register_static(&usb_tablet_info
);
631 usb_legacy_register("usb-tablet", "tablet", NULL
);
632 type_register_static(&usb_mouse_info
);
633 usb_legacy_register("usb-mouse", "mouse", NULL
);
634 type_register_static(&usb_keyboard_info
);
635 usb_legacy_register("usb-kbd", "keyboard", NULL
);
638 type_init(usb_hid_register_types
)