xref: /openbmc/qemu/hw/usb/dev-hid.c (revision 1f32989d)
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 #include "hw/hw.h"
26 #include "console.h"
27 #include "hw/usb.h"
28 #include "hw/usb/desc.h"
29 #include "qemu-timer.h"
30 #include "hw/hid.h"
31 
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
39 
40 /* HID descriptor types */
41 #define USB_DT_HID    0x21
42 #define USB_DT_REPORT 0x22
43 #define USB_DT_PHY    0x23
44 
45 typedef struct USBHIDState {
46     USBDevice dev;
47     USBEndpoint *intr;
48     HIDState hid;
49 } USBHIDState;
50 
51 enum {
52     STR_MANUFACTURER = 1,
53     STR_PRODUCT_MOUSE,
54     STR_PRODUCT_TABLET,
55     STR_PRODUCT_KEYBOARD,
56     STR_SERIALNUMBER,
57     STR_CONFIG_MOUSE,
58     STR_CONFIG_TABLET,
59     STR_CONFIG_KEYBOARD,
60 };
61 
62 static const USBDescStrings desc_strings = {
63     [STR_MANUFACTURER]     = "QEMU",
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",
71 };
72 
73 static const USBDescIface desc_iface_mouse = {
74     .bInterfaceNumber              = 0,
75     .bNumEndpoints                 = 1,
76     .bInterfaceClass               = USB_CLASS_HID,
77     .bInterfaceSubClass            = 0x01, /* boot */
78     .bInterfaceProtocol            = 0x02,
79     .ndesc                         = 1,
80     .descs = (USBDescOther[]) {
81         {
82             /* HID descriptor */
83             .data = (uint8_t[]) {
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 */
90                 52, 0,         /*  u16 len */
91             },
92         },
93     },
94     .eps = (USBDescEndpoint[]) {
95         {
96             .bEndpointAddress      = USB_DIR_IN | 0x01,
97             .bmAttributes          = USB_ENDPOINT_XFER_INT,
98             .wMaxPacketSize        = 4,
99             .bInterval             = 0x0a,
100         },
101     },
102 };
103 
104 static const USBDescIface desc_iface_tablet = {
105     .bInterfaceNumber              = 0,
106     .bNumEndpoints                 = 1,
107     .bInterfaceClass               = USB_CLASS_HID,
108     .bInterfaceProtocol            = 0x02,
109     .ndesc                         = 1,
110     .descs = (USBDescOther[]) {
111         {
112             /* HID descriptor */
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 */
120                 74, 0,         /*  u16 len */
121             },
122         },
123     },
124     .eps = (USBDescEndpoint[]) {
125         {
126             .bEndpointAddress      = USB_DIR_IN | 0x01,
127             .bmAttributes          = USB_ENDPOINT_XFER_INT,
128             .wMaxPacketSize        = 8,
129             .bInterval             = 0x0a,
130         },
131     },
132 };
133 
134 static const USBDescIface desc_iface_keyboard = {
135     .bInterfaceNumber              = 0,
136     .bNumEndpoints                 = 1,
137     .bInterfaceClass               = USB_CLASS_HID,
138     .bInterfaceSubClass            = 0x01, /* boot */
139     .bInterfaceProtocol            = 0x01, /* keyboard */
140     .ndesc                         = 1,
141     .descs = (USBDescOther[]) {
142         {
143             /* HID descriptor */
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 */
152             },
153         },
154     },
155     .eps = (USBDescEndpoint[]) {
156         {
157             .bEndpointAddress      = USB_DIR_IN | 0x01,
158             .bmAttributes          = USB_ENDPOINT_XFER_INT,
159             .wMaxPacketSize        = 8,
160             .bInterval             = 0x0a,
161         },
162     },
163 };
164 
165 static const USBDescDevice desc_device_mouse = {
166     .bcdUSB                        = 0x0100,
167     .bMaxPacketSize0               = 8,
168     .bNumConfigurations            = 1,
169     .confs = (USBDescConfig[]) {
170         {
171             .bNumInterfaces        = 1,
172             .bConfigurationValue   = 1,
173             .iConfiguration        = STR_CONFIG_MOUSE,
174             .bmAttributes          = 0xa0,
175             .bMaxPower             = 50,
176             .nif = 1,
177             .ifs = &desc_iface_mouse,
178         },
179     },
180 };
181 
182 static const USBDescDevice desc_device_tablet = {
183     .bcdUSB                        = 0x0100,
184     .bMaxPacketSize0               = 8,
185     .bNumConfigurations            = 1,
186     .confs = (USBDescConfig[]) {
187         {
188             .bNumInterfaces        = 1,
189             .bConfigurationValue   = 1,
190             .iConfiguration        = STR_CONFIG_TABLET,
191             .bmAttributes          = 0xa0,
192             .bMaxPower             = 50,
193             .nif = 1,
194             .ifs = &desc_iface_tablet,
195         },
196     },
197 };
198 
199 static const USBDescDevice desc_device_keyboard = {
200     .bcdUSB                        = 0x0100,
201     .bMaxPacketSize0               = 8,
202     .bNumConfigurations            = 1,
203     .confs = (USBDescConfig[]) {
204         {
205             .bNumInterfaces        = 1,
206             .bConfigurationValue   = 1,
207             .iConfiguration        = STR_CONFIG_KEYBOARD,
208             .bmAttributes          = 0xa0,
209             .bMaxPower             = 50,
210             .nif = 1,
211             .ifs = &desc_iface_keyboard,
212         },
213     },
214 };
215 
216 static const USBDesc desc_mouse = {
217     .id = {
218         .idVendor          = 0x0627,
219         .idProduct         = 0x0001,
220         .bcdDevice         = 0,
221         .iManufacturer     = STR_MANUFACTURER,
222         .iProduct          = STR_PRODUCT_MOUSE,
223         .iSerialNumber     = STR_SERIALNUMBER,
224     },
225     .full = &desc_device_mouse,
226     .str  = desc_strings,
227 };
228 
229 static const USBDesc desc_tablet = {
230     .id = {
231         .idVendor          = 0x0627,
232         .idProduct         = 0x0001,
233         .bcdDevice         = 0,
234         .iManufacturer     = STR_MANUFACTURER,
235         .iProduct          = STR_PRODUCT_TABLET,
236         .iSerialNumber     = STR_SERIALNUMBER,
237     },
238     .full = &desc_device_tablet,
239     .str  = desc_strings,
240 };
241 
242 static const USBDesc desc_keyboard = {
243     .id = {
244         .idVendor          = 0x0627,
245         .idProduct         = 0x0001,
246         .bcdDevice         = 0,
247         .iManufacturer     = STR_MANUFACTURER,
248         .iProduct          = STR_PRODUCT_KEYBOARD,
249         .iSerialNumber     = STR_SERIALNUMBER,
250     },
251     .full = &desc_device_keyboard,
252     .str  = desc_strings,
253 };
254 
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 */
283 };
284 
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 */
323 };
324 
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 */
358 };
359 
360 static void usb_hid_changed(HIDState *hs)
361 {
362     USBHIDState *us = container_of(hs, USBHIDState, hid);
363 
364     usb_wakeup(us->intr);
365 }
366 
367 static void usb_hid_handle_reset(USBDevice *dev)
368 {
369     USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
370 
371     hid_reset(&us->hid);
372 }
373 
374 static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
375                int request, int value, int index, int length, uint8_t *data)
376 {
377     USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
378     HIDState *hs = &us->hid;
379     int ret;
380 
381     ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
382     if (ret >= 0) {
383         return;
384     }
385 
386     switch (request) {
387         /* hid specific requests */
388     case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
389         switch (value >> 8) {
390         case 0x22:
391             if (hs->kind == HID_MOUSE) {
392 		memcpy(data, qemu_mouse_hid_report_descriptor,
393 		       sizeof(qemu_mouse_hid_report_descriptor));
394                 p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
395             } else if (hs->kind == HID_TABLET) {
396                 memcpy(data, qemu_tablet_hid_report_descriptor,
397 		       sizeof(qemu_tablet_hid_report_descriptor));
398                 p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
399             } else if (hs->kind == HID_KEYBOARD) {
400                 memcpy(data, qemu_keyboard_hid_report_descriptor,
401                        sizeof(qemu_keyboard_hid_report_descriptor));
402                 p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
403             }
404             break;
405         default:
406             goto fail;
407         }
408         break;
409     case GET_REPORT:
410         if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
411             p->actual_length = hid_pointer_poll(hs, data, length);
412         } else if (hs->kind == HID_KEYBOARD) {
413             p->actual_length = hid_keyboard_poll(hs, data, length);
414         }
415         break;
416     case SET_REPORT:
417         if (hs->kind == HID_KEYBOARD) {
418             p->actual_length = hid_keyboard_write(hs, data, length);
419         } else {
420             goto fail;
421         }
422         break;
423     case GET_PROTOCOL:
424         if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
425             goto fail;
426         }
427         data[0] = hs->protocol;
428         p->actual_length = 1;
429         break;
430     case SET_PROTOCOL:
431         if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
432             goto fail;
433         }
434         hs->protocol = value;
435         break;
436     case GET_IDLE:
437         data[0] = hs->idle;
438         p->actual_length = 1;
439         break;
440     case SET_IDLE:
441         hs->idle = (uint8_t) (value >> 8);
442         hid_set_next_idle(hs, qemu_get_clock_ns(vm_clock));
443         if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
444             hid_pointer_activate(hs);
445         }
446         break;
447     default:
448     fail:
449         p->status = USB_RET_STALL;
450         break;
451     }
452 }
453 
454 static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
455 {
456     USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
457     HIDState *hs = &us->hid;
458     uint8_t buf[p->iov.size];
459     int len = 0;
460 
461     switch (p->pid) {
462     case USB_TOKEN_IN:
463         if (p->ep->nr == 1) {
464             int64_t curtime = qemu_get_clock_ns(vm_clock);
465             if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
466                 hid_pointer_activate(hs);
467             }
468             if (!hid_has_events(hs) &&
469                 (!hs->idle || hs->next_idle_clock - curtime > 0)) {
470                 p->status = USB_RET_NAK;
471                 return;
472             }
473             hid_set_next_idle(hs, curtime);
474             if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
475                 len = hid_pointer_poll(hs, buf, p->iov.size);
476             } else if (hs->kind == HID_KEYBOARD) {
477                 len = hid_keyboard_poll(hs, buf, p->iov.size);
478             }
479             usb_packet_copy(p, buf, len);
480         } else {
481             goto fail;
482         }
483         break;
484     case USB_TOKEN_OUT:
485     default:
486     fail:
487         p->status = USB_RET_STALL;
488         break;
489     }
490 }
491 
492 static void usb_hid_handle_destroy(USBDevice *dev)
493 {
494     USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
495 
496     hid_free(&us->hid);
497 }
498 
499 static int usb_hid_initfn(USBDevice *dev, int kind)
500 {
501     USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
502 
503     usb_desc_init(dev);
504     us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
505     hid_init(&us->hid, kind, usb_hid_changed);
506     return 0;
507 }
508 
509 static int usb_tablet_initfn(USBDevice *dev)
510 {
511     return usb_hid_initfn(dev, HID_TABLET);
512 }
513 
514 static int usb_mouse_initfn(USBDevice *dev)
515 {
516     return usb_hid_initfn(dev, HID_MOUSE);
517 }
518 
519 static int usb_keyboard_initfn(USBDevice *dev)
520 {
521     return usb_hid_initfn(dev, HID_KEYBOARD);
522 }
523 
524 static int usb_ptr_post_load(void *opaque, int version_id)
525 {
526     USBHIDState *s = opaque;
527 
528     if (s->dev.remote_wakeup) {
529         hid_pointer_activate(&s->hid);
530     }
531     return 0;
532 }
533 
534 static const VMStateDescription vmstate_usb_ptr = {
535     .name = "usb-ptr",
536     .version_id = 1,
537     .minimum_version_id = 1,
538     .post_load = usb_ptr_post_load,
539     .fields = (VMStateField []) {
540         VMSTATE_USB_DEVICE(dev, USBHIDState),
541         VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
542         VMSTATE_END_OF_LIST()
543     }
544 };
545 
546 static const VMStateDescription vmstate_usb_kbd = {
547     .name = "usb-kbd",
548     .version_id = 1,
549     .minimum_version_id = 1,
550     .fields = (VMStateField []) {
551         VMSTATE_USB_DEVICE(dev, USBHIDState),
552         VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
553         VMSTATE_END_OF_LIST()
554     }
555 };
556 
557 static void usb_hid_class_initfn(ObjectClass *klass, void *data)
558 {
559     USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
560 
561     uc->handle_reset   = usb_hid_handle_reset;
562     uc->handle_control = usb_hid_handle_control;
563     uc->handle_data    = usb_hid_handle_data;
564     uc->handle_destroy = usb_hid_handle_destroy;
565 }
566 
567 static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
568 {
569     DeviceClass *dc = DEVICE_CLASS(klass);
570     USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
571 
572     usb_hid_class_initfn(klass, data);
573     uc->init           = usb_tablet_initfn;
574     uc->product_desc   = "QEMU USB Tablet";
575     uc->usb_desc       = &desc_tablet;
576     dc->vmsd = &vmstate_usb_ptr;
577 }
578 
579 static TypeInfo usb_tablet_info = {
580     .name          = "usb-tablet",
581     .parent        = TYPE_USB_DEVICE,
582     .instance_size = sizeof(USBHIDState),
583     .class_init    = usb_tablet_class_initfn,
584 };
585 
586 static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
587 {
588     DeviceClass *dc = DEVICE_CLASS(klass);
589     USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
590 
591     usb_hid_class_initfn(klass, data);
592     uc->init           = usb_mouse_initfn;
593     uc->product_desc   = "QEMU USB Mouse";
594     uc->usb_desc       = &desc_mouse;
595     dc->vmsd = &vmstate_usb_ptr;
596 }
597 
598 static TypeInfo usb_mouse_info = {
599     .name          = "usb-mouse",
600     .parent        = TYPE_USB_DEVICE,
601     .instance_size = sizeof(USBHIDState),
602     .class_init    = usb_mouse_class_initfn,
603 };
604 
605 static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
606 {
607     DeviceClass *dc = DEVICE_CLASS(klass);
608     USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
609 
610     usb_hid_class_initfn(klass, data);
611     uc->init           = usb_keyboard_initfn;
612     uc->product_desc   = "QEMU USB Keyboard";
613     uc->usb_desc       = &desc_keyboard;
614     dc->vmsd = &vmstate_usb_kbd;
615 }
616 
617 static TypeInfo usb_keyboard_info = {
618     .name          = "usb-kbd",
619     .parent        = TYPE_USB_DEVICE,
620     .instance_size = sizeof(USBHIDState),
621     .class_init    = usb_keyboard_class_initfn,
622 };
623 
624 static void usb_hid_register_types(void)
625 {
626     type_register_static(&usb_tablet_info);
627     usb_legacy_register("usb-tablet", "tablet", NULL);
628     type_register_static(&usb_mouse_info);
629     usb_legacy_register("usb-mouse", "mouse", NULL);
630     type_register_static(&usb_keyboard_info);
631     usb_legacy_register("usb-kbd", "keyboard", NULL);
632 }
633 
634 type_init(usb_hid_register_types)
635