xref: /qemu/hw/usb/dev-hid.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1 /*
2  * QEMU USB HID devices
3  *
4  * Copyright (c) 2005 Fabrice Bellard
5  * Copyright (c) 2007 OpenMoko, Inc.  (andrew@openedhand.com)
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  */
25 
26 #include "qemu/osdep.h"
27 #include "ui/console.h"
28 #include "hw/usb.h"
29 #include "migration/vmstate.h"
30 #include "desc.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"
38 
39 struct USBHIDState {
40     USBDevice dev;
41     USBEndpoint *intr;
42     HIDState hid;
43     uint32_t usb_version;
44     char *display;
45     uint32_t head;
46 };
47 typedef struct USBHIDState USBHIDState;
48 
49 #define TYPE_USB_HID "usb-hid"
50 #define USB_HID(obj) OBJECT_CHECK(USBHIDState, (obj), TYPE_USB_HID)
51 
52 enum {
53     STR_MANUFACTURER = 1,
54     STR_PRODUCT_MOUSE,
55     STR_PRODUCT_TABLET,
56     STR_PRODUCT_KEYBOARD,
57     STR_SERIAL_COMPAT,
58     STR_CONFIG_MOUSE,
59     STR_CONFIG_TABLET,
60     STR_CONFIG_KEYBOARD,
61     STR_SERIAL_MOUSE,
62     STR_SERIAL_TABLET,
63     STR_SERIAL_KEYBOARD,
64 };
65 
66 static const USBDescStrings desc_strings = {
67     [STR_MANUFACTURER]     = "QEMU",
68     [STR_PRODUCT_MOUSE]    = "QEMU USB Mouse",
69     [STR_PRODUCT_TABLET]   = "QEMU USB Tablet",
70     [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
71     [STR_SERIAL_COMPAT]    = "42",
72     [STR_CONFIG_MOUSE]     = "HID Mouse",
73     [STR_CONFIG_TABLET]    = "HID Tablet",
74     [STR_CONFIG_KEYBOARD]  = "HID Keyboard",
75     [STR_SERIAL_MOUSE]     = "89126",
76     [STR_SERIAL_TABLET]    = "28754",
77     [STR_SERIAL_KEYBOARD]  = "68284",
78 };
79 
80 static const USBDescIface desc_iface_mouse = {
81     .bInterfaceNumber              = 0,
82     .bNumEndpoints                 = 1,
83     .bInterfaceClass               = USB_CLASS_HID,
84     .bInterfaceSubClass            = 0x01, /* boot */
85     .bInterfaceProtocol            = 0x02,
86     .ndesc                         = 1,
87     .descs = (USBDescOther[]) {
88         {
89             /* HID descriptor */
90             .data = (uint8_t[]) {
91                 0x09,          /*  u8  bLength */
92                 USB_DT_HID,    /*  u8  bDescriptorType */
93                 0x01, 0x00,    /*  u16 HID_class */
94                 0x00,          /*  u8  country_code */
95                 0x01,          /*  u8  num_descriptors */
96                 USB_DT_REPORT, /*  u8  type: Report */
97                 52, 0,         /*  u16 len */
98             },
99         },
100     },
101     .eps = (USBDescEndpoint[]) {
102         {
103             .bEndpointAddress      = USB_DIR_IN | 0x01,
104             .bmAttributes          = USB_ENDPOINT_XFER_INT,
105             .wMaxPacketSize        = 4,
106             .bInterval             = 0x0a,
107         },
108     },
109 };
110 
111 static const USBDescIface desc_iface_mouse2 = {
112     .bInterfaceNumber              = 0,
113     .bNumEndpoints                 = 1,
114     .bInterfaceClass               = USB_CLASS_HID,
115     .bInterfaceSubClass            = 0x01, /* boot */
116     .bInterfaceProtocol            = 0x02,
117     .ndesc                         = 1,
118     .descs = (USBDescOther[]) {
119         {
120             /* HID descriptor */
121             .data = (uint8_t[]) {
122                 0x09,          /*  u8  bLength */
123                 USB_DT_HID,    /*  u8  bDescriptorType */
124                 0x01, 0x00,    /*  u16 HID_class */
125                 0x00,          /*  u8  country_code */
126                 0x01,          /*  u8  num_descriptors */
127                 USB_DT_REPORT, /*  u8  type: Report */
128                 52, 0,         /*  u16 len */
129             },
130         },
131     },
132     .eps = (USBDescEndpoint[]) {
133         {
134             .bEndpointAddress      = USB_DIR_IN | 0x01,
135             .bmAttributes          = USB_ENDPOINT_XFER_INT,
136             .wMaxPacketSize        = 4,
137             .bInterval             = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
138         },
139     },
140 };
141 
142 static const USBDescIface desc_iface_tablet = {
143     .bInterfaceNumber              = 0,
144     .bNumEndpoints                 = 1,
145     .bInterfaceClass               = USB_CLASS_HID,
146     .bInterfaceProtocol            = 0x00,
147     .ndesc                         = 1,
148     .descs = (USBDescOther[]) {
149         {
150             /* HID descriptor */
151             .data = (uint8_t[]) {
152                 0x09,          /*  u8  bLength */
153                 USB_DT_HID,    /*  u8  bDescriptorType */
154                 0x01, 0x00,    /*  u16 HID_class */
155                 0x00,          /*  u8  country_code */
156                 0x01,          /*  u8  num_descriptors */
157                 USB_DT_REPORT, /*  u8  type: Report */
158                 74, 0,         /*  u16 len */
159             },
160         },
161     },
162     .eps = (USBDescEndpoint[]) {
163         {
164             .bEndpointAddress      = USB_DIR_IN | 0x01,
165             .bmAttributes          = USB_ENDPOINT_XFER_INT,
166             .wMaxPacketSize        = 8,
167             .bInterval             = 0x0a,
168         },
169     },
170 };
171 
172 static const USBDescIface desc_iface_tablet2 = {
173     .bInterfaceNumber              = 0,
174     .bNumEndpoints                 = 1,
175     .bInterfaceClass               = USB_CLASS_HID,
176     .bInterfaceProtocol            = 0x00,
177     .ndesc                         = 1,
178     .descs = (USBDescOther[]) {
179         {
180             /* HID descriptor */
181             .data = (uint8_t[]) {
182                 0x09,          /*  u8  bLength */
183                 USB_DT_HID,    /*  u8  bDescriptorType */
184                 0x01, 0x00,    /*  u16 HID_class */
185                 0x00,          /*  u8  country_code */
186                 0x01,          /*  u8  num_descriptors */
187                 USB_DT_REPORT, /*  u8  type: Report */
188                 74, 0,         /*  u16 len */
189             },
190         },
191     },
192     .eps = (USBDescEndpoint[]) {
193         {
194             .bEndpointAddress      = USB_DIR_IN | 0x01,
195             .bmAttributes          = USB_ENDPOINT_XFER_INT,
196             .wMaxPacketSize        = 8,
197             .bInterval             = 4, /* 2 ^ (4-1) * 125 usecs = 1 ms */
198         },
199     },
200 };
201 
202 static const USBDescIface desc_iface_keyboard = {
203     .bInterfaceNumber              = 0,
204     .bNumEndpoints                 = 1,
205     .bInterfaceClass               = USB_CLASS_HID,
206     .bInterfaceSubClass            = 0x01, /* boot */
207     .bInterfaceProtocol            = 0x01, /* keyboard */
208     .ndesc                         = 1,
209     .descs = (USBDescOther[]) {
210         {
211             /* HID descriptor */
212             .data = (uint8_t[]) {
213                 0x09,          /*  u8  bLength */
214                 USB_DT_HID,    /*  u8  bDescriptorType */
215                 0x11, 0x01,    /*  u16 HID_class */
216                 0x00,          /*  u8  country_code */
217                 0x01,          /*  u8  num_descriptors */
218                 USB_DT_REPORT, /*  u8  type: Report */
219                 0x3f, 0,       /*  u16 len */
220             },
221         },
222     },
223     .eps = (USBDescEndpoint[]) {
224         {
225             .bEndpointAddress      = USB_DIR_IN | 0x01,
226             .bmAttributes          = USB_ENDPOINT_XFER_INT,
227             .wMaxPacketSize        = 8,
228             .bInterval             = 0x0a,
229         },
230     },
231 };
232 
233 static const USBDescIface desc_iface_keyboard2 = {
234     .bInterfaceNumber              = 0,
235     .bNumEndpoints                 = 1,
236     .bInterfaceClass               = USB_CLASS_HID,
237     .bInterfaceSubClass            = 0x01, /* boot */
238     .bInterfaceProtocol            = 0x01, /* keyboard */
239     .ndesc                         = 1,
240     .descs = (USBDescOther[]) {
241         {
242             /* HID descriptor */
243             .data = (uint8_t[]) {
244                 0x09,          /*  u8  bLength */
245                 USB_DT_HID,    /*  u8  bDescriptorType */
246                 0x11, 0x01,    /*  u16 HID_class */
247                 0x00,          /*  u8  country_code */
248                 0x01,          /*  u8  num_descriptors */
249                 USB_DT_REPORT, /*  u8  type: Report */
250                 0x3f, 0,       /*  u16 len */
251             },
252         },
253     },
254     .eps = (USBDescEndpoint[]) {
255         {
256             .bEndpointAddress      = USB_DIR_IN | 0x01,
257             .bmAttributes          = USB_ENDPOINT_XFER_INT,
258             .wMaxPacketSize        = 8,
259             .bInterval             = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
260         },
261     },
262 };
263 
264 static const USBDescDevice desc_device_mouse = {
265     .bcdUSB                        = 0x0100,
266     .bMaxPacketSize0               = 8,
267     .bNumConfigurations            = 1,
268     .confs = (USBDescConfig[]) {
269         {
270             .bNumInterfaces        = 1,
271             .bConfigurationValue   = 1,
272             .iConfiguration        = STR_CONFIG_MOUSE,
273             .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
274             .bMaxPower             = 50,
275             .nif = 1,
276             .ifs = &desc_iface_mouse,
277         },
278     },
279 };
280 
281 static const USBDescDevice desc_device_mouse2 = {
282     .bcdUSB                        = 0x0200,
283     .bMaxPacketSize0               = 64,
284     .bNumConfigurations            = 1,
285     .confs = (USBDescConfig[]) {
286         {
287             .bNumInterfaces        = 1,
288             .bConfigurationValue   = 1,
289             .iConfiguration        = STR_CONFIG_MOUSE,
290             .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
291             .bMaxPower             = 50,
292             .nif = 1,
293             .ifs = &desc_iface_mouse2,
294         },
295     },
296 };
297 
298 static const USBDescDevice desc_device_tablet = {
299     .bcdUSB                        = 0x0100,
300     .bMaxPacketSize0               = 8,
301     .bNumConfigurations            = 1,
302     .confs = (USBDescConfig[]) {
303         {
304             .bNumInterfaces        = 1,
305             .bConfigurationValue   = 1,
306             .iConfiguration        = STR_CONFIG_TABLET,
307             .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
308             .bMaxPower             = 50,
309             .nif = 1,
310             .ifs = &desc_iface_tablet,
311         },
312     },
313 };
314 
315 static const USBDescDevice desc_device_tablet2 = {
316     .bcdUSB                        = 0x0200,
317     .bMaxPacketSize0               = 64,
318     .bNumConfigurations            = 1,
319     .confs = (USBDescConfig[]) {
320         {
321             .bNumInterfaces        = 1,
322             .bConfigurationValue   = 1,
323             .iConfiguration        = STR_CONFIG_TABLET,
324             .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
325             .bMaxPower             = 50,
326             .nif = 1,
327             .ifs = &desc_iface_tablet2,
328         },
329     },
330 };
331 
332 static const USBDescDevice desc_device_keyboard = {
333     .bcdUSB                        = 0x0100,
334     .bMaxPacketSize0               = 8,
335     .bNumConfigurations            = 1,
336     .confs = (USBDescConfig[]) {
337         {
338             .bNumInterfaces        = 1,
339             .bConfigurationValue   = 1,
340             .iConfiguration        = STR_CONFIG_KEYBOARD,
341             .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
342             .bMaxPower             = 50,
343             .nif = 1,
344             .ifs = &desc_iface_keyboard,
345         },
346     },
347 };
348 
349 static const USBDescDevice desc_device_keyboard2 = {
350     .bcdUSB                        = 0x0200,
351     .bMaxPacketSize0               = 64,
352     .bNumConfigurations            = 1,
353     .confs = (USBDescConfig[]) {
354         {
355             .bNumInterfaces        = 1,
356             .bConfigurationValue   = 1,
357             .iConfiguration        = STR_CONFIG_KEYBOARD,
358             .bmAttributes          = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
359             .bMaxPower             = 50,
360             .nif = 1,
361             .ifs = &desc_iface_keyboard2,
362         },
363     },
364 };
365 
366 static const USBDescMSOS desc_msos_suspend = {
367     .SelectiveSuspendEnabled = true,
368 };
369 
370 static const USBDesc desc_mouse = {
371     .id = {
372         .idVendor          = 0x0627,
373         .idProduct         = 0x0001,
374         .bcdDevice         = 0,
375         .iManufacturer     = STR_MANUFACTURER,
376         .iProduct          = STR_PRODUCT_MOUSE,
377         .iSerialNumber     = STR_SERIAL_MOUSE,
378     },
379     .full = &desc_device_mouse,
380     .str  = desc_strings,
381     .msos = &desc_msos_suspend,
382 };
383 
384 static const USBDesc desc_mouse2 = {
385     .id = {
386         .idVendor          = 0x0627,
387         .idProduct         = 0x0001,
388         .bcdDevice         = 0,
389         .iManufacturer     = STR_MANUFACTURER,
390         .iProduct          = STR_PRODUCT_MOUSE,
391         .iSerialNumber     = STR_SERIAL_MOUSE,
392     },
393     .full = &desc_device_mouse,
394     .high = &desc_device_mouse2,
395     .str  = desc_strings,
396     .msos = &desc_msos_suspend,
397 };
398 
399 static const USBDesc desc_tablet = {
400     .id = {
401         .idVendor          = 0x0627,
402         .idProduct         = 0x0001,
403         .bcdDevice         = 0,
404         .iManufacturer     = STR_MANUFACTURER,
405         .iProduct          = STR_PRODUCT_TABLET,
406         .iSerialNumber     = STR_SERIAL_TABLET,
407     },
408     .full = &desc_device_tablet,
409     .str  = desc_strings,
410     .msos = &desc_msos_suspend,
411 };
412 
413 static const USBDesc desc_tablet2 = {
414     .id = {
415         .idVendor          = 0x0627,
416         .idProduct         = 0x0001,
417         .bcdDevice         = 0,
418         .iManufacturer     = STR_MANUFACTURER,
419         .iProduct          = STR_PRODUCT_TABLET,
420         .iSerialNumber     = STR_SERIAL_TABLET,
421     },
422     .full = &desc_device_tablet,
423     .high = &desc_device_tablet2,
424     .str  = desc_strings,
425     .msos = &desc_msos_suspend,
426 };
427 
428 static const USBDesc desc_keyboard = {
429     .id = {
430         .idVendor          = 0x0627,
431         .idProduct         = 0x0001,
432         .bcdDevice         = 0,
433         .iManufacturer     = STR_MANUFACTURER,
434         .iProduct          = STR_PRODUCT_KEYBOARD,
435         .iSerialNumber     = STR_SERIAL_KEYBOARD,
436     },
437     .full = &desc_device_keyboard,
438     .str  = desc_strings,
439     .msos = &desc_msos_suspend,
440 };
441 
442 static const USBDesc desc_keyboard2 = {
443     .id = {
444         .idVendor          = 0x0627,
445         .idProduct         = 0x0001,
446         .bcdDevice         = 0,
447         .iManufacturer     = STR_MANUFACTURER,
448         .iProduct          = STR_PRODUCT_KEYBOARD,
449         .iSerialNumber     = STR_SERIAL_KEYBOARD,
450     },
451     .full = &desc_device_keyboard,
452     .high = &desc_device_keyboard2,
453     .str  = desc_strings,
454     .msos = &desc_msos_suspend,
455 };
456 
457 static const uint8_t qemu_mouse_hid_report_descriptor[] = {
458     0x05, 0x01,		/* Usage Page (Generic Desktop) */
459     0x09, 0x02,		/* Usage (Mouse) */
460     0xa1, 0x01,		/* Collection (Application) */
461     0x09, 0x01,		/*   Usage (Pointer) */
462     0xa1, 0x00,		/*   Collection (Physical) */
463     0x05, 0x09,		/*     Usage Page (Button) */
464     0x19, 0x01,		/*     Usage Minimum (1) */
465     0x29, 0x03,		/*     Usage Maximum (3) */
466     0x15, 0x00,		/*     Logical Minimum (0) */
467     0x25, 0x01,		/*     Logical Maximum (1) */
468     0x95, 0x03,		/*     Report Count (3) */
469     0x75, 0x01,		/*     Report Size (1) */
470     0x81, 0x02,		/*     Input (Data, Variable, Absolute) */
471     0x95, 0x01,		/*     Report Count (1) */
472     0x75, 0x05,		/*     Report Size (5) */
473     0x81, 0x01,		/*     Input (Constant) */
474     0x05, 0x01,		/*     Usage Page (Generic Desktop) */
475     0x09, 0x30,		/*     Usage (X) */
476     0x09, 0x31,		/*     Usage (Y) */
477     0x09, 0x38,		/*     Usage (Wheel) */
478     0x15, 0x81,		/*     Logical Minimum (-0x7f) */
479     0x25, 0x7f,		/*     Logical Maximum (0x7f) */
480     0x75, 0x08,		/*     Report Size (8) */
481     0x95, 0x03,		/*     Report Count (3) */
482     0x81, 0x06,		/*     Input (Data, Variable, Relative) */
483     0xc0,		/*   End Collection */
484     0xc0,		/* End Collection */
485 };
486 
487 static const uint8_t qemu_tablet_hid_report_descriptor[] = {
488     0x05, 0x01,		/* Usage Page (Generic Desktop) */
489     0x09, 0x02,		/* Usage (Mouse) */
490     0xa1, 0x01,		/* Collection (Application) */
491     0x09, 0x01,		/*   Usage (Pointer) */
492     0xa1, 0x00,		/*   Collection (Physical) */
493     0x05, 0x09,		/*     Usage Page (Button) */
494     0x19, 0x01,		/*     Usage Minimum (1) */
495     0x29, 0x03,		/*     Usage Maximum (3) */
496     0x15, 0x00,		/*     Logical Minimum (0) */
497     0x25, 0x01,		/*     Logical Maximum (1) */
498     0x95, 0x03,		/*     Report Count (3) */
499     0x75, 0x01,		/*     Report Size (1) */
500     0x81, 0x02,		/*     Input (Data, Variable, Absolute) */
501     0x95, 0x01,		/*     Report Count (1) */
502     0x75, 0x05,		/*     Report Size (5) */
503     0x81, 0x01,		/*     Input (Constant) */
504     0x05, 0x01,		/*     Usage Page (Generic Desktop) */
505     0x09, 0x30,		/*     Usage (X) */
506     0x09, 0x31,		/*     Usage (Y) */
507     0x15, 0x00,		/*     Logical Minimum (0) */
508     0x26, 0xff, 0x7f,	/*     Logical Maximum (0x7fff) */
509     0x35, 0x00,		/*     Physical Minimum (0) */
510     0x46, 0xff, 0x7f,	/*     Physical Maximum (0x7fff) */
511     0x75, 0x10,		/*     Report Size (16) */
512     0x95, 0x02,		/*     Report Count (2) */
513     0x81, 0x02,		/*     Input (Data, Variable, Absolute) */
514     0x05, 0x01,		/*     Usage Page (Generic Desktop) */
515     0x09, 0x38,		/*     Usage (Wheel) */
516     0x15, 0x81,		/*     Logical Minimum (-0x7f) */
517     0x25, 0x7f,		/*     Logical Maximum (0x7f) */
518     0x35, 0x00,		/*     Physical Minimum (same as logical) */
519     0x45, 0x00,		/*     Physical Maximum (same as logical) */
520     0x75, 0x08,		/*     Report Size (8) */
521     0x95, 0x01,		/*     Report Count (1) */
522     0x81, 0x06,		/*     Input (Data, Variable, Relative) */
523     0xc0,		/*   End Collection */
524     0xc0,		/* End Collection */
525 };
526 
527 static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
528     0x05, 0x01,		/* Usage Page (Generic Desktop) */
529     0x09, 0x06,		/* Usage (Keyboard) */
530     0xa1, 0x01,		/* Collection (Application) */
531     0x75, 0x01,		/*   Report Size (1) */
532     0x95, 0x08,		/*   Report Count (8) */
533     0x05, 0x07,		/*   Usage Page (Key Codes) */
534     0x19, 0xe0,		/*   Usage Minimum (224) */
535     0x29, 0xe7,		/*   Usage Maximum (231) */
536     0x15, 0x00,		/*   Logical Minimum (0) */
537     0x25, 0x01,		/*   Logical Maximum (1) */
538     0x81, 0x02,		/*   Input (Data, Variable, Absolute) */
539     0x95, 0x01,		/*   Report Count (1) */
540     0x75, 0x08,		/*   Report Size (8) */
541     0x81, 0x01,		/*   Input (Constant) */
542     0x95, 0x05,		/*   Report Count (5) */
543     0x75, 0x01,		/*   Report Size (1) */
544     0x05, 0x08,		/*   Usage Page (LEDs) */
545     0x19, 0x01,		/*   Usage Minimum (1) */
546     0x29, 0x05,		/*   Usage Maximum (5) */
547     0x91, 0x02,		/*   Output (Data, Variable, Absolute) */
548     0x95, 0x01,		/*   Report Count (1) */
549     0x75, 0x03,		/*   Report Size (3) */
550     0x91, 0x01,		/*   Output (Constant) */
551     0x95, 0x06,		/*   Report Count (6) */
552     0x75, 0x08,		/*   Report Size (8) */
553     0x15, 0x00,		/*   Logical Minimum (0) */
554     0x25, 0xff,		/*   Logical Maximum (255) */
555     0x05, 0x07,		/*   Usage Page (Key Codes) */
556     0x19, 0x00,		/*   Usage Minimum (0) */
557     0x29, 0xff,		/*   Usage Maximum (255) */
558     0x81, 0x00,		/*   Input (Data, Array) */
559     0xc0,		/* End Collection */
560 };
561 
562 static void usb_hid_changed(HIDState *hs)
563 {
564     USBHIDState *us = container_of(hs, USBHIDState, hid);
565 
566     usb_wakeup(us->intr, 0);
567 }
568 
569 static void usb_hid_handle_reset(USBDevice *dev)
570 {
571     USBHIDState *us = USB_HID(dev);
572 
573     hid_reset(&us->hid);
574 }
575 
576 static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
577                int request, int value, int index, int length, uint8_t *data)
578 {
579     USBHIDState *us = USB_HID(dev);
580     HIDState *hs = &us->hid;
581     int ret;
582 
583     ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
584     if (ret >= 0) {
585         return;
586     }
587 
588     switch (request) {
589         /* hid specific requests */
590     case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
591         switch (value >> 8) {
592         case 0x22:
593             if (hs->kind == HID_MOUSE) {
594                 memcpy(data, qemu_mouse_hid_report_descriptor,
595                        sizeof(qemu_mouse_hid_report_descriptor));
596                 p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
597             } else if (hs->kind == HID_TABLET) {
598                 memcpy(data, qemu_tablet_hid_report_descriptor,
599                        sizeof(qemu_tablet_hid_report_descriptor));
600                 p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
601             } else if (hs->kind == HID_KEYBOARD) {
602                 memcpy(data, qemu_keyboard_hid_report_descriptor,
603                        sizeof(qemu_keyboard_hid_report_descriptor));
604                 p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
605             }
606             break;
607         default:
608             goto fail;
609         }
610         break;
611     case HID_GET_REPORT:
612         if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
613             p->actual_length = hid_pointer_poll(hs, data, length);
614         } else if (hs->kind == HID_KEYBOARD) {
615             p->actual_length = hid_keyboard_poll(hs, data, length);
616         }
617         break;
618     case HID_SET_REPORT:
619         if (hs->kind == HID_KEYBOARD) {
620             p->actual_length = hid_keyboard_write(hs, data, length);
621         } else {
622             goto fail;
623         }
624         break;
625     case HID_GET_PROTOCOL:
626         if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
627             goto fail;
628         }
629         data[0] = hs->protocol;
630         p->actual_length = 1;
631         break;
632     case HID_SET_PROTOCOL:
633         if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
634             goto fail;
635         }
636         hs->protocol = value;
637         break;
638     case HID_GET_IDLE:
639         data[0] = hs->idle;
640         p->actual_length = 1;
641         break;
642     case HID_SET_IDLE:
643         hs->idle = (uint8_t) (value >> 8);
644         hid_set_next_idle(hs);
645         if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
646             hid_pointer_activate(hs);
647         }
648         break;
649     default:
650     fail:
651         p->status = USB_RET_STALL;
652         break;
653     }
654 }
655 
656 static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
657 {
658     USBHIDState *us = USB_HID(dev);
659     HIDState *hs = &us->hid;
660     uint8_t buf[p->iov.size];
661     int len = 0;
662 
663     switch (p->pid) {
664     case USB_TOKEN_IN:
665         if (p->ep->nr == 1) {
666             if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
667                 hid_pointer_activate(hs);
668             }
669             if (!hid_has_events(hs)) {
670                 p->status = USB_RET_NAK;
671                 return;
672             }
673             hid_set_next_idle(hs);
674             if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
675                 len = hid_pointer_poll(hs, buf, p->iov.size);
676             } else if (hs->kind == HID_KEYBOARD) {
677                 len = hid_keyboard_poll(hs, buf, p->iov.size);
678             }
679             usb_packet_copy(p, buf, len);
680         } else {
681             goto fail;
682         }
683         break;
684     case USB_TOKEN_OUT:
685     default:
686     fail:
687         p->status = USB_RET_STALL;
688         break;
689     }
690 }
691 
692 static void usb_hid_unrealize(USBDevice *dev)
693 {
694     USBHIDState *us = USB_HID(dev);
695 
696     hid_free(&us->hid);
697 }
698 
699 static void usb_hid_initfn(USBDevice *dev, int kind,
700                            const USBDesc *usb1, const USBDesc *usb2,
701                            Error **errp)
702 {
703     USBHIDState *us = USB_HID(dev);
704     switch (us->usb_version) {
705     case 1:
706         dev->usb_desc = usb1;
707         break;
708     case 2:
709         dev->usb_desc = usb2;
710         break;
711     default:
712         dev->usb_desc = NULL;
713     }
714     if (!dev->usb_desc) {
715         error_setg(errp, "Invalid usb version %d for usb hid device",
716                    us->usb_version);
717         return;
718     }
719 
720     usb_desc_create_serial(dev);
721     usb_desc_init(dev);
722     us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
723     hid_init(&us->hid, kind, usb_hid_changed);
724     if (us->display && us->hid.s) {
725         qemu_input_handler_bind(us->hid.s, us->display, us->head, NULL);
726     }
727 }
728 
729 static void usb_tablet_realize(USBDevice *dev, Error **errp)
730 {
731 
732     usb_hid_initfn(dev, HID_TABLET, &desc_tablet, &desc_tablet2, errp);
733 }
734 
735 static void usb_mouse_realize(USBDevice *dev, Error **errp)
736 {
737     usb_hid_initfn(dev, HID_MOUSE, &desc_mouse, &desc_mouse2, errp);
738 }
739 
740 static void usb_keyboard_realize(USBDevice *dev, Error **errp)
741 {
742     usb_hid_initfn(dev, HID_KEYBOARD, &desc_keyboard, &desc_keyboard2, errp);
743 }
744 
745 static int usb_ptr_post_load(void *opaque, int version_id)
746 {
747     USBHIDState *s = opaque;
748 
749     if (s->dev.remote_wakeup) {
750         hid_pointer_activate(&s->hid);
751     }
752     return 0;
753 }
754 
755 static const VMStateDescription vmstate_usb_ptr = {
756     .name = "usb-ptr",
757     .version_id = 1,
758     .minimum_version_id = 1,
759     .post_load = usb_ptr_post_load,
760     .fields = (VMStateField[]) {
761         VMSTATE_USB_DEVICE(dev, USBHIDState),
762         VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
763         VMSTATE_END_OF_LIST()
764     }
765 };
766 
767 static const VMStateDescription vmstate_usb_kbd = {
768     .name = "usb-kbd",
769     .version_id = 1,
770     .minimum_version_id = 1,
771     .fields = (VMStateField[]) {
772         VMSTATE_USB_DEVICE(dev, USBHIDState),
773         VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
774         VMSTATE_END_OF_LIST()
775     }
776 };
777 
778 static void usb_hid_class_initfn(ObjectClass *klass, void *data)
779 {
780     USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
781 
782     uc->handle_reset   = usb_hid_handle_reset;
783     uc->handle_control = usb_hid_handle_control;
784     uc->handle_data    = usb_hid_handle_data;
785     uc->unrealize      = usb_hid_unrealize;
786     uc->handle_attach  = usb_desc_attach;
787 }
788 
789 static const TypeInfo usb_hid_type_info = {
790     .name = TYPE_USB_HID,
791     .parent = TYPE_USB_DEVICE,
792     .instance_size = sizeof(USBHIDState),
793     .abstract = true,
794     .class_init = usb_hid_class_initfn,
795 };
796 
797 static Property usb_tablet_properties[] = {
798         DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
799         DEFINE_PROP_STRING("display", USBHIDState, display),
800         DEFINE_PROP_UINT32("head", USBHIDState, head, 0),
801         DEFINE_PROP_END_OF_LIST(),
802 };
803 
804 static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
805 {
806     DeviceClass *dc = DEVICE_CLASS(klass);
807     USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
808 
809     uc->realize        = usb_tablet_realize;
810     uc->product_desc   = "QEMU USB Tablet";
811     dc->vmsd = &vmstate_usb_ptr;
812     device_class_set_props(dc, usb_tablet_properties);
813     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
814 }
815 
816 static const TypeInfo usb_tablet_info = {
817     .name          = "usb-tablet",
818     .parent        = TYPE_USB_HID,
819     .class_init    = usb_tablet_class_initfn,
820 };
821 
822 static Property usb_mouse_properties[] = {
823         DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
824         DEFINE_PROP_END_OF_LIST(),
825 };
826 
827 static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
828 {
829     DeviceClass *dc = DEVICE_CLASS(klass);
830     USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
831 
832     uc->realize        = usb_mouse_realize;
833     uc->product_desc   = "QEMU USB Mouse";
834     dc->vmsd = &vmstate_usb_ptr;
835     device_class_set_props(dc, usb_mouse_properties);
836     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
837 }
838 
839 static const TypeInfo usb_mouse_info = {
840     .name          = "usb-mouse",
841     .parent        = TYPE_USB_HID,
842     .class_init    = usb_mouse_class_initfn,
843 };
844 
845 static Property usb_keyboard_properties[] = {
846         DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
847         DEFINE_PROP_STRING("display", USBHIDState, display),
848         DEFINE_PROP_END_OF_LIST(),
849 };
850 
851 static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
852 {
853     DeviceClass *dc = DEVICE_CLASS(klass);
854     USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
855 
856     uc->realize        = usb_keyboard_realize;
857     uc->product_desc   = "QEMU USB Keyboard";
858     dc->vmsd = &vmstate_usb_kbd;
859     device_class_set_props(dc, usb_keyboard_properties);
860     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
861 }
862 
863 static const TypeInfo usb_keyboard_info = {
864     .name          = "usb-kbd",
865     .parent        = TYPE_USB_HID,
866     .class_init    = usb_keyboard_class_initfn,
867 };
868 
869 static void usb_hid_register_types(void)
870 {
871     type_register_static(&usb_hid_type_info);
872     type_register_static(&usb_tablet_info);
873     usb_legacy_register("usb-tablet", "tablet", NULL);
874     type_register_static(&usb_mouse_info);
875     usb_legacy_register("usb-mouse", "mouse", NULL);
876     type_register_static(&usb_keyboard_info);
877     usb_legacy_register("usb-kbd", "keyboard", NULL);
878 }
879 
880 type_init(usb_hid_register_types)
881