blob: 2e7dcd96cb2699a5fb511addcd078adcfe3f4a6b [file] [log] [blame]
bellard59ae5402005-11-05 16:57:08 +00001/*
2 * QEMU USB HID devices
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard59ae5402005-11-05 16:57:08 +00004 * Copyright (c) 2005 Fabrice Bellard
balrog47b2d332007-06-22 08:16:00 +00005 * Copyright (c) 2007 OpenMoko, Inc. (andrew@openedhand.com)
ths5fafdf22007-09-16 21:08:06 +00006 *
bellard59ae5402005-11-05 16:57:08 +00007 * 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 */
Gerd Hoffmannf1ae32a2012-03-07 14:55:18 +010025#include "hw/hw.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010026#include "ui/console.h"
Gerd Hoffmannf1ae32a2012-03-07 14:55:18 +010027#include "hw/usb.h"
28#include "hw/usb/desc.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/timer.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "hw/input/hid.h"
bellard59ae5402005-11-05 16:57:08 +000031
32/* HID interface requests */
33#define GET_REPORT 0xa101
34#define GET_IDLE 0xa102
35#define GET_PROTOCOL 0xa103
balrog47b2d332007-06-22 08:16:00 +000036#define SET_REPORT 0x2109
bellard59ae5402005-11-05 16:57:08 +000037#define SET_IDLE 0x210a
38#define SET_PROTOCOL 0x210b
39
balrog47b2d332007-06-22 08:16:00 +000040/* HID descriptor types */
41#define USB_DT_HID 0x21
42#define USB_DT_REPORT 0x22
43#define USB_DT_PHY 0x23
44
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +020045typedef struct USBHIDState {
46 USBDevice dev;
Gerd Hoffmann7567b512012-01-17 13:25:46 +010047 USBEndpoint *intr;
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +020048 HIDState hid;
Hans de Goede427e3aa2012-11-17 12:47:18 +010049 uint32_t usb_version;
Gerd Hoffmannf85d2832014-05-19 15:26:51 +020050 char *display;
51 uint32_t head;
balrog47b2d332007-06-22 08:16:00 +000052} USBHIDState;
53
Gongleif5669122015-05-06 20:55:26 +080054#define TYPE_USB_HID "usb-hid"
55#define USB_HID(obj) OBJECT_CHECK(USBHIDState, (obj), TYPE_USB_HID)
56
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010057enum {
58 STR_MANUFACTURER = 1,
59 STR_PRODUCT_MOUSE,
60 STR_PRODUCT_TABLET,
61 STR_PRODUCT_KEYBOARD,
62 STR_SERIALNUMBER,
63 STR_CONFIG_MOUSE,
64 STR_CONFIG_TABLET,
65 STR_CONFIG_KEYBOARD,
bellard59ae5402005-11-05 16:57:08 +000066};
67
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010068static const USBDescStrings desc_strings = {
Crístian Viana93bfef42012-05-30 00:35:51 -030069 [STR_MANUFACTURER] = "QEMU",
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010070 [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
71 [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
72 [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
Gerd Hoffmann7b074a22010-12-14 16:46:40 +010073 [STR_SERIALNUMBER] = "42", /* == remote wakeup works */
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010074 [STR_CONFIG_MOUSE] = "HID Mouse",
75 [STR_CONFIG_TABLET] = "HID Tablet",
76 [STR_CONFIG_KEYBOARD] = "HID Keyboard",
bellard09b26c52006-04-12 21:09:08 +000077};
78
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010079static const USBDescIface desc_iface_mouse = {
80 .bInterfaceNumber = 0,
81 .bNumEndpoints = 1,
82 .bInterfaceClass = USB_CLASS_HID,
83 .bInterfaceSubClass = 0x01, /* boot */
84 .bInterfaceProtocol = 0x02,
85 .ndesc = 1,
86 .descs = (USBDescOther[]) {
87 {
88 /* HID descriptor */
89 .data = (uint8_t[]) {
90 0x09, /* u8 bLength */
91 USB_DT_HID, /* u8 bDescriptorType */
92 0x01, 0x00, /* u16 HID_class */
93 0x00, /* u8 country_code */
94 0x01, /* u8 num_descriptors */
95 USB_DT_REPORT, /* u8 type: Report */
96 52, 0, /* u16 len */
97 },
98 },
99 },
100 .eps = (USBDescEndpoint[]) {
101 {
102 .bEndpointAddress = USB_DIR_IN | 0x01,
103 .bmAttributes = USB_ENDPOINT_XFER_INT,
104 .wMaxPacketSize = 4,
105 .bInterval = 0x0a,
106 },
107 },
bellard59ae5402005-11-05 16:57:08 +0000108};
109
Jan Vesely58e4fee2014-09-29 22:21:10 -0400110static const USBDescIface desc_iface_mouse2 = {
111 .bInterfaceNumber = 0,
112 .bNumEndpoints = 1,
113 .bInterfaceClass = USB_CLASS_HID,
114 .bInterfaceSubClass = 0x01, /* boot */
115 .bInterfaceProtocol = 0x02,
116 .ndesc = 1,
117 .descs = (USBDescOther[]) {
118 {
119 /* HID descriptor */
120 .data = (uint8_t[]) {
121 0x09, /* u8 bLength */
122 USB_DT_HID, /* u8 bDescriptorType */
123 0x01, 0x00, /* u16 HID_class */
124 0x00, /* u8 country_code */
125 0x01, /* u8 num_descriptors */
126 USB_DT_REPORT, /* u8 type: Report */
127 52, 0, /* u16 len */
128 },
129 },
130 },
131 .eps = (USBDescEndpoint[]) {
132 {
133 .bEndpointAddress = USB_DIR_IN | 0x01,
134 .bmAttributes = USB_ENDPOINT_XFER_INT,
135 .wMaxPacketSize = 4,
136 .bInterval = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
137 },
138 },
139};
140
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100141static const USBDescIface desc_iface_tablet = {
142 .bInterfaceNumber = 0,
143 .bNumEndpoints = 1,
144 .bInterfaceClass = USB_CLASS_HID,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100145 .bInterfaceProtocol = 0x02,
146 .ndesc = 1,
147 .descs = (USBDescOther[]) {
148 {
149 /* HID descriptor */
150 .data = (uint8_t[]) {
151 0x09, /* u8 bLength */
152 USB_DT_HID, /* u8 bDescriptorType */
153 0x01, 0x00, /* u16 HID_class */
154 0x00, /* u8 country_code */
155 0x01, /* u8 num_descriptors */
156 USB_DT_REPORT, /* u8 type: Report */
157 74, 0, /* u16 len */
158 },
159 },
160 },
161 .eps = (USBDescEndpoint[]) {
162 {
163 .bEndpointAddress = USB_DIR_IN | 0x01,
164 .bmAttributes = USB_ENDPOINT_XFER_INT,
165 .wMaxPacketSize = 8,
166 .bInterval = 0x0a,
167 },
168 },
169};
balrog47b2d332007-06-22 08:16:00 +0000170
Hans de Goede427e3aa2012-11-17 12:47:18 +0100171static const USBDescIface desc_iface_tablet2 = {
172 .bInterfaceNumber = 0,
173 .bNumEndpoints = 1,
174 .bInterfaceClass = USB_CLASS_HID,
175 .bInterfaceProtocol = 0x02,
176 .ndesc = 1,
177 .descs = (USBDescOther[]) {
178 {
179 /* HID descriptor */
180 .data = (uint8_t[]) {
181 0x09, /* u8 bLength */
182 USB_DT_HID, /* u8 bDescriptorType */
183 0x01, 0x00, /* u16 HID_class */
184 0x00, /* u8 country_code */
185 0x01, /* u8 num_descriptors */
186 USB_DT_REPORT, /* u8 type: Report */
187 74, 0, /* u16 len */
188 },
189 },
190 },
191 .eps = (USBDescEndpoint[]) {
192 {
193 .bEndpointAddress = USB_DIR_IN | 0x01,
194 .bmAttributes = USB_ENDPOINT_XFER_INT,
195 .wMaxPacketSize = 8,
196 .bInterval = 4, /* 2 ^ (4-1) * 125 usecs = 1 ms */
197 },
198 },
199};
200
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100201static const USBDescIface desc_iface_keyboard = {
202 .bInterfaceNumber = 0,
203 .bNumEndpoints = 1,
204 .bInterfaceClass = USB_CLASS_HID,
205 .bInterfaceSubClass = 0x01, /* boot */
206 .bInterfaceProtocol = 0x01, /* keyboard */
207 .ndesc = 1,
208 .descs = (USBDescOther[]) {
209 {
210 /* HID descriptor */
211 .data = (uint8_t[]) {
212 0x09, /* u8 bLength */
213 USB_DT_HID, /* u8 bDescriptorType */
214 0x11, 0x01, /* u16 HID_class */
215 0x00, /* u8 country_code */
216 0x01, /* u8 num_descriptors */
217 USB_DT_REPORT, /* u8 type: Report */
218 0x3f, 0, /* u16 len */
219 },
220 },
221 },
222 .eps = (USBDescEndpoint[]) {
223 {
224 .bEndpointAddress = USB_DIR_IN | 0x01,
225 .bmAttributes = USB_ENDPOINT_XFER_INT,
226 .wMaxPacketSize = 8,
227 .bInterval = 0x0a,
228 },
229 },
230};
balrog47b2d332007-06-22 08:16:00 +0000231
Jan Veselyb13ce072014-09-29 22:21:11 -0400232static const USBDescIface desc_iface_keyboard2 = {
233 .bInterfaceNumber = 0,
234 .bNumEndpoints = 1,
235 .bInterfaceClass = USB_CLASS_HID,
236 .bInterfaceSubClass = 0x01, /* boot */
237 .bInterfaceProtocol = 0x01, /* keyboard */
238 .ndesc = 1,
239 .descs = (USBDescOther[]) {
240 {
241 /* HID descriptor */
242 .data = (uint8_t[]) {
243 0x09, /* u8 bLength */
244 USB_DT_HID, /* u8 bDescriptorType */
245 0x11, 0x01, /* u16 HID_class */
246 0x00, /* u8 country_code */
247 0x01, /* u8 num_descriptors */
248 USB_DT_REPORT, /* u8 type: Report */
249 0x3f, 0, /* u16 len */
250 },
251 },
252 },
253 .eps = (USBDescEndpoint[]) {
254 {
255 .bEndpointAddress = USB_DIR_IN | 0x01,
256 .bmAttributes = USB_ENDPOINT_XFER_INT,
257 .wMaxPacketSize = 8,
258 .bInterval = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
259 },
260 },
261};
262
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100263static const USBDescDevice desc_device_mouse = {
264 .bcdUSB = 0x0100,
265 .bMaxPacketSize0 = 8,
266 .bNumConfigurations = 1,
267 .confs = (USBDescConfig[]) {
268 {
269 .bNumInterfaces = 1,
270 .bConfigurationValue = 1,
271 .iConfiguration = STR_CONFIG_MOUSE,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200272 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100273 .bMaxPower = 50,
Brad Hardsadd75082011-04-03 15:33:19 +1000274 .nif = 1,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100275 .ifs = &desc_iface_mouse,
276 },
277 },
278};
balrog47b2d332007-06-22 08:16:00 +0000279
Jan Vesely58e4fee2014-09-29 22:21:10 -0400280static const USBDescDevice desc_device_mouse2 = {
281 .bcdUSB = 0x0200,
282 .bMaxPacketSize0 = 64,
283 .bNumConfigurations = 1,
284 .confs = (USBDescConfig[]) {
285 {
286 .bNumInterfaces = 1,
287 .bConfigurationValue = 1,
288 .iConfiguration = STR_CONFIG_MOUSE,
289 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
290 .bMaxPower = 50,
291 .nif = 1,
292 .ifs = &desc_iface_mouse2,
293 },
294 },
295};
296
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100297static const USBDescDevice desc_device_tablet = {
298 .bcdUSB = 0x0100,
299 .bMaxPacketSize0 = 8,
300 .bNumConfigurations = 1,
301 .confs = (USBDescConfig[]) {
302 {
303 .bNumInterfaces = 1,
304 .bConfigurationValue = 1,
305 .iConfiguration = STR_CONFIG_TABLET,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200306 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100307 .bMaxPower = 50,
Brad Hardsadd75082011-04-03 15:33:19 +1000308 .nif = 1,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100309 .ifs = &desc_iface_tablet,
310 },
311 },
312};
balrog47b2d332007-06-22 08:16:00 +0000313
Hans de Goede427e3aa2012-11-17 12:47:18 +0100314static const USBDescDevice desc_device_tablet2 = {
315 .bcdUSB = 0x0200,
316 .bMaxPacketSize0 = 64,
317 .bNumConfigurations = 1,
318 .confs = (USBDescConfig[]) {
319 {
320 .bNumInterfaces = 1,
321 .bConfigurationValue = 1,
322 .iConfiguration = STR_CONFIG_TABLET,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200323 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
Hans de Goede427e3aa2012-11-17 12:47:18 +0100324 .bMaxPower = 50,
325 .nif = 1,
326 .ifs = &desc_iface_tablet2,
327 },
328 },
329};
330
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100331static const USBDescDevice desc_device_keyboard = {
332 .bcdUSB = 0x0100,
333 .bMaxPacketSize0 = 8,
334 .bNumConfigurations = 1,
335 .confs = (USBDescConfig[]) {
336 {
337 .bNumInterfaces = 1,
338 .bConfigurationValue = 1,
339 .iConfiguration = STR_CONFIG_KEYBOARD,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200340 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100341 .bMaxPower = 50,
Brad Hardsadd75082011-04-03 15:33:19 +1000342 .nif = 1,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100343 .ifs = &desc_iface_keyboard,
344 },
345 },
346};
347
Jan Veselyb13ce072014-09-29 22:21:11 -0400348static const USBDescDevice desc_device_keyboard2 = {
349 .bcdUSB = 0x0200,
350 .bMaxPacketSize0 = 64,
351 .bNumConfigurations = 1,
352 .confs = (USBDescConfig[]) {
353 {
354 .bNumInterfaces = 1,
355 .bConfigurationValue = 1,
356 .iConfiguration = STR_CONFIG_KEYBOARD,
357 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
358 .bMaxPower = 50,
359 .nif = 1,
360 .ifs = &desc_iface_keyboard2,
361 },
362 },
363};
364
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100365static const USBDescMSOS desc_msos_suspend = {
366 .SelectiveSuspendEnabled = true,
367};
368
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100369static const USBDesc desc_mouse = {
370 .id = {
371 .idVendor = 0x0627,
372 .idProduct = 0x0001,
373 .bcdDevice = 0,
374 .iManufacturer = STR_MANUFACTURER,
375 .iProduct = STR_PRODUCT_MOUSE,
376 .iSerialNumber = STR_SERIALNUMBER,
377 },
378 .full = &desc_device_mouse,
379 .str = desc_strings,
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100380 .msos = &desc_msos_suspend,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100381};
382
Jan Vesely58e4fee2014-09-29 22:21:10 -0400383static const USBDesc desc_mouse2 = {
384 .id = {
385 .idVendor = 0x0627,
386 .idProduct = 0x0001,
387 .bcdDevice = 0,
388 .iManufacturer = STR_MANUFACTURER,
389 .iProduct = STR_PRODUCT_MOUSE,
390 .iSerialNumber = STR_SERIALNUMBER,
391 },
392 .full = &desc_device_mouse,
393 .high = &desc_device_mouse2,
394 .str = desc_strings,
395 .msos = &desc_msos_suspend,
396};
397
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100398static const USBDesc desc_tablet = {
399 .id = {
400 .idVendor = 0x0627,
401 .idProduct = 0x0001,
402 .bcdDevice = 0,
403 .iManufacturer = STR_MANUFACTURER,
404 .iProduct = STR_PRODUCT_TABLET,
405 .iSerialNumber = STR_SERIALNUMBER,
406 },
407 .full = &desc_device_tablet,
408 .str = desc_strings,
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100409 .msos = &desc_msos_suspend,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100410};
411
Hans de Goede427e3aa2012-11-17 12:47:18 +0100412static const USBDesc desc_tablet2 = {
413 .id = {
414 .idVendor = 0x0627,
415 .idProduct = 0x0001,
416 .bcdDevice = 0,
417 .iManufacturer = STR_MANUFACTURER,
418 .iProduct = STR_PRODUCT_TABLET,
419 .iSerialNumber = STR_SERIALNUMBER,
420 },
421 .full = &desc_device_tablet,
422 .high = &desc_device_tablet2,
423 .str = desc_strings,
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100424 .msos = &desc_msos_suspend,
Hans de Goede427e3aa2012-11-17 12:47:18 +0100425};
426
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100427static const USBDesc desc_keyboard = {
428 .id = {
429 .idVendor = 0x0627,
430 .idProduct = 0x0001,
431 .bcdDevice = 0,
432 .iManufacturer = STR_MANUFACTURER,
433 .iProduct = STR_PRODUCT_KEYBOARD,
434 .iSerialNumber = STR_SERIALNUMBER,
435 },
436 .full = &desc_device_keyboard,
437 .str = desc_strings,
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100438 .msos = &desc_msos_suspend,
balrog47b2d332007-06-22 08:16:00 +0000439};
440
Jan Veselyb13ce072014-09-29 22:21:11 -0400441static const USBDesc desc_keyboard2 = {
442 .id = {
443 .idVendor = 0x0627,
444 .idProduct = 0x0001,
445 .bcdDevice = 0,
446 .iManufacturer = STR_MANUFACTURER,
447 .iProduct = STR_PRODUCT_KEYBOARD,
448 .iSerialNumber = STR_SERIALNUMBER,
449 },
450 .full = &desc_device_keyboard,
451 .high = &desc_device_keyboard2,
452 .str = desc_strings,
453 .msos = &desc_msos_suspend,
454};
455
bellard59ae5402005-11-05 16:57:08 +0000456static const uint8_t qemu_mouse_hid_report_descriptor[] = {
balrog976f8ee2008-05-17 19:55:28 +0000457 0x05, 0x01, /* Usage Page (Generic Desktop) */
458 0x09, 0x02, /* Usage (Mouse) */
459 0xa1, 0x01, /* Collection (Application) */
460 0x09, 0x01, /* Usage (Pointer) */
461 0xa1, 0x00, /* Collection (Physical) */
462 0x05, 0x09, /* Usage Page (Button) */
463 0x19, 0x01, /* Usage Minimum (1) */
464 0x29, 0x03, /* Usage Maximum (3) */
465 0x15, 0x00, /* Logical Minimum (0) */
466 0x25, 0x01, /* Logical Maximum (1) */
467 0x95, 0x03, /* Report Count (3) */
468 0x75, 0x01, /* Report Size (1) */
469 0x81, 0x02, /* Input (Data, Variable, Absolute) */
470 0x95, 0x01, /* Report Count (1) */
471 0x75, 0x05, /* Report Size (5) */
472 0x81, 0x01, /* Input (Constant) */
473 0x05, 0x01, /* Usage Page (Generic Desktop) */
474 0x09, 0x30, /* Usage (X) */
475 0x09, 0x31, /* Usage (Y) */
476 0x09, 0x38, /* Usage (Wheel) */
477 0x15, 0x81, /* Logical Minimum (-0x7f) */
478 0x25, 0x7f, /* Logical Maximum (0x7f) */
479 0x75, 0x08, /* Report Size (8) */
480 0x95, 0x03, /* Report Count (3) */
481 0x81, 0x06, /* Input (Data, Variable, Relative) */
482 0xc0, /* End Collection */
483 0xc0, /* End Collection */
bellard59ae5402005-11-05 16:57:08 +0000484};
485
bellard09b26c52006-04-12 21:09:08 +0000486static const uint8_t qemu_tablet_hid_report_descriptor[] = {
balrog976f8ee2008-05-17 19:55:28 +0000487 0x05, 0x01, /* Usage Page (Generic Desktop) */
488 0x09, 0x01, /* Usage (Pointer) */
489 0xa1, 0x01, /* Collection (Application) */
490 0x09, 0x01, /* Usage (Pointer) */
491 0xa1, 0x00, /* Collection (Physical) */
492 0x05, 0x09, /* Usage Page (Button) */
493 0x19, 0x01, /* Usage Minimum (1) */
494 0x29, 0x03, /* Usage Maximum (3) */
495 0x15, 0x00, /* Logical Minimum (0) */
496 0x25, 0x01, /* Logical Maximum (1) */
497 0x95, 0x03, /* Report Count (3) */
498 0x75, 0x01, /* Report Size (1) */
499 0x81, 0x02, /* Input (Data, Variable, Absolute) */
500 0x95, 0x01, /* Report Count (1) */
501 0x75, 0x05, /* Report Size (5) */
502 0x81, 0x01, /* Input (Constant) */
503 0x05, 0x01, /* Usage Page (Generic Desktop) */
504 0x09, 0x30, /* Usage (X) */
505 0x09, 0x31, /* Usage (Y) */
506 0x15, 0x00, /* Logical Minimum (0) */
balrogde5c2d02008-09-15 22:26:35 +0000507 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
balrog976f8ee2008-05-17 19:55:28 +0000508 0x35, 0x00, /* Physical Minimum (0) */
balrogde5c2d02008-09-15 22:26:35 +0000509 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
balrog976f8ee2008-05-17 19:55:28 +0000510 0x75, 0x10, /* Report Size (16) */
511 0x95, 0x02, /* Report Count (2) */
512 0x81, 0x02, /* Input (Data, Variable, Absolute) */
513 0x05, 0x01, /* Usage Page (Generic Desktop) */
514 0x09, 0x38, /* Usage (Wheel) */
515 0x15, 0x81, /* Logical Minimum (-0x7f) */
516 0x25, 0x7f, /* Logical Maximum (0x7f) */
517 0x35, 0x00, /* Physical Minimum (same as logical) */
518 0x45, 0x00, /* Physical Maximum (same as logical) */
519 0x75, 0x08, /* Report Size (8) */
520 0x95, 0x01, /* Report Count (1) */
521 0x81, 0x06, /* Input (Data, Variable, Relative) */
522 0xc0, /* End Collection */
523 0xc0, /* End Collection */
bellard09b26c52006-04-12 21:09:08 +0000524};
525
balrog47b2d332007-06-22 08:16:00 +0000526static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
527 0x05, 0x01, /* Usage Page (Generic Desktop) */
528 0x09, 0x06, /* Usage (Keyboard) */
529 0xa1, 0x01, /* Collection (Application) */
530 0x75, 0x01, /* Report Size (1) */
531 0x95, 0x08, /* Report Count (8) */
532 0x05, 0x07, /* Usage Page (Key Codes) */
533 0x19, 0xe0, /* Usage Minimum (224) */
534 0x29, 0xe7, /* Usage Maximum (231) */
535 0x15, 0x00, /* Logical Minimum (0) */
536 0x25, 0x01, /* Logical Maximum (1) */
537 0x81, 0x02, /* Input (Data, Variable, Absolute) */
538 0x95, 0x01, /* Report Count (1) */
539 0x75, 0x08, /* Report Size (8) */
540 0x81, 0x01, /* Input (Constant) */
541 0x95, 0x05, /* Report Count (5) */
542 0x75, 0x01, /* Report Size (1) */
543 0x05, 0x08, /* Usage Page (LEDs) */
544 0x19, 0x01, /* Usage Minimum (1) */
545 0x29, 0x05, /* Usage Maximum (5) */
546 0x91, 0x02, /* Output (Data, Variable, Absolute) */
547 0x95, 0x01, /* Report Count (1) */
548 0x75, 0x03, /* Report Size (3) */
549 0x91, 0x01, /* Output (Constant) */
550 0x95, 0x06, /* Report Count (6) */
551 0x75, 0x08, /* Report Size (8) */
552 0x15, 0x00, /* Logical Minimum (0) */
553 0x25, 0xff, /* Logical Maximum (255) */
554 0x05, 0x07, /* Usage Page (Key Codes) */
555 0x19, 0x00, /* Usage Minimum (0) */
556 0x29, 0xff, /* Usage Maximum (255) */
557 0x81, 0x00, /* Input (Data, Array) */
558 0xc0, /* End Collection */
559};
560
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200561static void usb_hid_changed(HIDState *hs)
balrog47e699d2008-09-29 00:25:17 +0000562{
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200563 USBHIDState *us = container_of(hs, USBHIDState, hid);
balrog47e699d2008-09-29 00:25:17 +0000564
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100565 usb_wakeup(us->intr, 0);
balrog47e699d2008-09-29 00:25:17 +0000566}
567
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200568static void usb_hid_handle_reset(USBDevice *dev)
balrog47b2d332007-06-22 08:16:00 +0000569{
Gongleif5669122015-05-06 20:55:26 +0800570 USBHIDState *us = USB_HID(dev);
balrog47b2d332007-06-22 08:16:00 +0000571
Gerd Hoffmanndcfda672011-07-15 15:08:01 +0200572 hid_reset(&us->hid);
Kevin O'Connor68735b62010-02-13 18:32:17 -0500573}
574
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100575static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
Hans de Goede007fd622011-02-02 16:33:13 +0100576 int request, int value, int index, int length, uint8_t *data)
bellard59ae5402005-11-05 16:57:08 +0000577{
Gongleif5669122015-05-06 20:55:26 +0800578 USBHIDState *us = USB_HID(dev);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200579 HIDState *hs = &us->hid;
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100580 int ret;
bellard59ae5402005-11-05 16:57:08 +0000581
Hans de Goede007fd622011-02-02 16:33:13 +0100582 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100583 if (ret >= 0) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100584 return;
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100585 }
586
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200587 switch (request) {
bellard59ae5402005-11-05 16:57:08 +0000588 /* hid specific requests */
589 case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200590 switch (value >> 8) {
bellard59ae5402005-11-05 16:57:08 +0000591 case 0x22:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200592 if (hs->kind == HID_MOUSE) {
ths5fafdf22007-09-16 21:08:06 +0000593 memcpy(data, qemu_mouse_hid_report_descriptor,
bellard09b26c52006-04-12 21:09:08 +0000594 sizeof(qemu_mouse_hid_report_descriptor));
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100595 p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200596 } else if (hs->kind == HID_TABLET) {
597 memcpy(data, qemu_tablet_hid_report_descriptor,
bellard09b26c52006-04-12 21:09:08 +0000598 sizeof(qemu_tablet_hid_report_descriptor));
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100599 p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200600 } else if (hs->kind == HID_KEYBOARD) {
ths5fafdf22007-09-16 21:08:06 +0000601 memcpy(data, qemu_keyboard_hid_report_descriptor,
balrog47b2d332007-06-22 08:16:00 +0000602 sizeof(qemu_keyboard_hid_report_descriptor));
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100603 p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
balrog47b2d332007-06-22 08:16:00 +0000604 }
605 break;
bellard59ae5402005-11-05 16:57:08 +0000606 default:
607 goto fail;
608 }
609 break;
610 case GET_REPORT:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200611 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100612 p->actual_length = hid_pointer_poll(hs, data, length);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200613 } else if (hs->kind == HID_KEYBOARD) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100614 p->actual_length = hid_keyboard_poll(hs, data, length);
Gerd Hoffmanne7e73892011-07-08 13:19:01 +0200615 }
balrog47b2d332007-06-22 08:16:00 +0000616 break;
617 case SET_REPORT:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200618 if (hs->kind == HID_KEYBOARD) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100619 p->actual_length = hid_keyboard_write(hs, data, length);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200620 } else {
balrog47b2d332007-06-22 08:16:00 +0000621 goto fail;
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200622 }
balrog47b2d332007-06-22 08:16:00 +0000623 break;
624 case GET_PROTOCOL:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200625 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
balrog47b2d332007-06-22 08:16:00 +0000626 goto fail;
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200627 }
Gerd Hoffmannb069d342011-07-15 15:52:33 +0200628 data[0] = hs->protocol;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100629 p->actual_length = 1;
balrog47b2d332007-06-22 08:16:00 +0000630 break;
631 case SET_PROTOCOL:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200632 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
balrog47b2d332007-06-22 08:16:00 +0000633 goto fail;
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200634 }
Gerd Hoffmannb069d342011-07-15 15:52:33 +0200635 hs->protocol = value;
balrog47b2d332007-06-22 08:16:00 +0000636 break;
637 case GET_IDLE:
Gerd Hoffmannb069d342011-07-15 15:52:33 +0200638 data[0] = hs->idle;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100639 p->actual_length = 1;
bellard59ae5402005-11-05 16:57:08 +0000640 break;
641 case SET_IDLE:
Gerd Hoffmannb069d342011-07-15 15:52:33 +0200642 hs->idle = (uint8_t) (value >> 8);
Hans de Goede027c03f2012-12-14 14:35:38 +0100643 hid_set_next_idle(hs);
Gerd Hoffmann21635e12011-08-09 12:35:57 +0200644 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
645 hid_pointer_activate(hs);
646 }
bellard59ae5402005-11-05 16:57:08 +0000647 break;
648 default:
649 fail:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100650 p->status = USB_RET_STALL;
bellard59ae5402005-11-05 16:57:08 +0000651 break;
652 }
bellard59ae5402005-11-05 16:57:08 +0000653}
654
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100655static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
bellard59ae5402005-11-05 16:57:08 +0000656{
Gongleif5669122015-05-06 20:55:26 +0800657 USBHIDState *us = USB_HID(dev);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200658 HIDState *hs = &us->hid;
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200659 uint8_t buf[p->iov.size];
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100660 int len = 0;
bellard59ae5402005-11-05 16:57:08 +0000661
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200662 switch (p->pid) {
bellard59ae5402005-11-05 16:57:08 +0000663 case USB_TOKEN_IN:
Gerd Hoffmann079d0b72012-01-12 13:23:01 +0100664 if (p->ep->nr == 1) {
Gerd Hoffmann299aa1c2012-02-23 15:24:24 +0100665 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
666 hid_pointer_activate(hs);
667 }
Hans de Goede027c03f2012-12-14 14:35:38 +0100668 if (!hid_has_events(hs)) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100669 p->status = USB_RET_NAK;
670 return;
Paolo Bonzini13f8b972011-01-12 13:19:20 +0100671 }
Hans de Goede027c03f2012-12-14 14:35:38 +0100672 hid_set_next_idle(hs);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200673 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100674 len = hid_pointer_poll(hs, buf, p->iov.size);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200675 } else if (hs->kind == HID_KEYBOARD) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100676 len = hid_keyboard_poll(hs, buf, p->iov.size);
Paolo Bonzini13f8b972011-01-12 13:19:20 +0100677 }
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100678 usb_packet_copy(p, buf, len);
bellard59ae5402005-11-05 16:57:08 +0000679 } else {
680 goto fail;
681 }
682 break;
683 case USB_TOKEN_OUT:
684 default:
685 fail:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100686 p->status = USB_RET_STALL;
bellard59ae5402005-11-05 16:57:08 +0000687 break;
688 }
bellard59ae5402005-11-05 16:57:08 +0000689}
690
balrog47b2d332007-06-22 08:16:00 +0000691static void usb_hid_handle_destroy(USBDevice *dev)
bellard059809e2006-07-19 18:06:15 +0000692{
Gongleif5669122015-05-06 20:55:26 +0800693 USBHIDState *us = USB_HID(dev);
bellard059809e2006-07-19 18:06:15 +0000694
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200695 hid_free(&us->hid);
696}
697
Jan Veselyff326ff2014-09-29 22:21:09 -0400698static void usb_hid_initfn(USBDevice *dev, int kind,
699 const USBDesc *usb1, const USBDesc *usb2,
700 Error **errp)
bellard09b26c52006-04-12 21:09:08 +0000701{
Gongleif5669122015-05-06 20:55:26 +0800702 USBHIDState *us = USB_HID(dev);
Jan Veselyff326ff2014-09-29 22:21:09 -0400703 switch (us->usb_version) {
704 case 1:
705 dev->usb_desc = usb1;
706 break;
707 case 2:
708 dev->usb_desc = usb2;
709 break;
710 default:
711 dev->usb_desc = NULL;
712 }
713 if (!dev->usb_desc) {
714 error_setg(errp, "Invalid usb version %d for usb hid device",
715 us->usb_version);
716 return;
717 }
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100718
Gerd Hoffmann71938a02013-06-12 13:01:49 +0200719 if (dev->serial) {
720 usb_desc_set_string(dev, STR_SERIALNUMBER, dev->serial);
721 }
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100722 usb_desc_init(dev);
Gerd Hoffmann7567b512012-01-17 13:25:46 +0100723 us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200724 hid_init(&us->hid, kind, usb_hid_changed);
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200725 if (us->display && us->hid.s) {
726 qemu_input_handler_bind(us->hid.s, us->display, us->head, NULL);
727 }
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200728}
bellard09b26c52006-04-12 21:09:08 +0000729
Gonglei276b7ac2014-09-19 14:48:36 +0800730static void usb_tablet_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200731{
Hans de Goede427e3aa2012-11-17 12:47:18 +0100732
Jan Veselyff326ff2014-09-29 22:21:09 -0400733 usb_hid_initfn(dev, HID_TABLET, &desc_tablet, &desc_tablet2, errp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200734}
bellard1f6e24e2006-06-26 21:00:51 +0000735
Gonglei276b7ac2014-09-19 14:48:36 +0800736static void usb_mouse_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200737{
Jan Vesely58e4fee2014-09-29 22:21:10 -0400738 usb_hid_initfn(dev, HID_MOUSE, &desc_mouse, &desc_mouse2, errp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200739}
740
Gonglei276b7ac2014-09-19 14:48:36 +0800741static void usb_keyboard_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200742{
Jan Veselyb13ce072014-09-29 22:21:11 -0400743 usb_hid_initfn(dev, HID_KEYBOARD, &desc_keyboard, &desc_keyboard2, errp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200744}
745
Gerd Hoffmann3a3286b2011-10-12 12:54:35 +0200746static int usb_ptr_post_load(void *opaque, int version_id)
747{
748 USBHIDState *s = opaque;
749
750 if (s->dev.remote_wakeup) {
751 hid_pointer_activate(&s->hid);
752 }
753 return 0;
754}
755
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100756static const VMStateDescription vmstate_usb_ptr = {
757 .name = "usb-ptr",
758 .version_id = 1,
759 .minimum_version_id = 1,
Gerd Hoffmann3a3286b2011-10-12 12:54:35 +0200760 .post_load = usb_ptr_post_load,
Juan Quintela6e3d6522014-04-16 13:31:26 +0200761 .fields = (VMStateField[]) {
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100762 VMSTATE_USB_DEVICE(dev, USBHIDState),
Michael Walle1f42d222011-08-09 23:54:54 +0200763 VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100764 VMSTATE_END_OF_LIST()
765 }
766};
767
768static const VMStateDescription vmstate_usb_kbd = {
769 .name = "usb-kbd",
770 .version_id = 1,
771 .minimum_version_id = 1,
Juan Quintela6e3d6522014-04-16 13:31:26 +0200772 .fields = (VMStateField[]) {
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100773 VMSTATE_USB_DEVICE(dev, USBHIDState),
Michael Walle1f42d222011-08-09 23:54:54 +0200774 VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100775 VMSTATE_END_OF_LIST()
776 }
777};
778
Anthony Liguori7f595602011-12-04 16:13:14 -0600779static void usb_hid_class_initfn(ObjectClass *klass, void *data)
Anthony Liguori62aed762011-12-15 14:53:10 -0600780{
781 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
782
Anthony Liguori62aed762011-12-15 14:53:10 -0600783 uc->handle_reset = usb_hid_handle_reset;
784 uc->handle_control = usb_hid_handle_control;
785 uc->handle_data = usb_hid_handle_data;
786 uc->handle_destroy = usb_hid_handle_destroy;
Hans de Goede427e3aa2012-11-17 12:47:18 +0100787 uc->handle_attach = usb_desc_attach;
Anthony Liguori62aed762011-12-15 14:53:10 -0600788}
789
Gongleif5669122015-05-06 20:55:26 +0800790static const TypeInfo usb_hid_type_info = {
791 .name = TYPE_USB_HID,
792 .parent = TYPE_USB_DEVICE,
793 .instance_size = sizeof(USBHIDState),
794 .abstract = true,
795 .class_init = usb_hid_class_initfn,
796};
797
Hans de Goede427e3aa2012-11-17 12:47:18 +0100798static Property usb_tablet_properties[] = {
799 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200800 DEFINE_PROP_STRING("display", USBHIDState, display),
801 DEFINE_PROP_UINT32("head", USBHIDState, head, 0),
Hans de Goede427e3aa2012-11-17 12:47:18 +0100802 DEFINE_PROP_END_OF_LIST(),
803};
804
Anthony Liguori7f595602011-12-04 16:13:14 -0600805static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
806{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600807 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori7f595602011-12-04 16:13:14 -0600808 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
809
Gonglei276b7ac2014-09-19 14:48:36 +0800810 uc->realize = usb_tablet_realize;
Anthony Liguori7f595602011-12-04 16:13:14 -0600811 uc->product_desc = "QEMU USB Tablet";
Anthony Liguori39bffca2011-12-07 21:34:16 -0600812 dc->vmsd = &vmstate_usb_ptr;
Hans de Goede427e3aa2012-11-17 12:47:18 +0100813 dc->props = usb_tablet_properties;
Marcel Apfelbaum31efd2e2013-08-22 20:11:36 +0300814 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori7f595602011-12-04 16:13:14 -0600815}
816
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100817static const TypeInfo usb_tablet_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600818 .name = "usb-tablet",
Gongleif5669122015-05-06 20:55:26 +0800819 .parent = TYPE_USB_HID,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600820 .class_init = usb_tablet_class_initfn,
Anthony Liguori62aed762011-12-15 14:53:10 -0600821};
822
Jan Vesely58e4fee2014-09-29 22:21:10 -0400823static Property usb_mouse_properties[] = {
824 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
825 DEFINE_PROP_END_OF_LIST(),
826};
827
Anthony Liguori62aed762011-12-15 14:53:10 -0600828static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
829{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600830 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -0600831 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
832
Gonglei276b7ac2014-09-19 14:48:36 +0800833 uc->realize = usb_mouse_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -0600834 uc->product_desc = "QEMU USB Mouse";
Anthony Liguori39bffca2011-12-07 21:34:16 -0600835 dc->vmsd = &vmstate_usb_ptr;
Jan Vesely58e4fee2014-09-29 22:21:10 -0400836 dc->props = usb_mouse_properties;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300837 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori62aed762011-12-15 14:53:10 -0600838}
839
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100840static const TypeInfo usb_mouse_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600841 .name = "usb-mouse",
Gongleif5669122015-05-06 20:55:26 +0800842 .parent = TYPE_USB_HID,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600843 .class_init = usb_mouse_class_initfn,
Anthony Liguori62aed762011-12-15 14:53:10 -0600844};
845
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200846static Property usb_keyboard_properties[] = {
Jan Veselyb13ce072014-09-29 22:21:11 -0400847 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200848 DEFINE_PROP_STRING("display", USBHIDState, display),
849 DEFINE_PROP_END_OF_LIST(),
850};
851
Anthony Liguori62aed762011-12-15 14:53:10 -0600852static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
853{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600854 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -0600855 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
856
Gonglei276b7ac2014-09-19 14:48:36 +0800857 uc->realize = usb_keyboard_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -0600858 uc->product_desc = "QEMU USB Keyboard";
Anthony Liguori39bffca2011-12-07 21:34:16 -0600859 dc->vmsd = &vmstate_usb_kbd;
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200860 dc->props = usb_keyboard_properties;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300861 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori62aed762011-12-15 14:53:10 -0600862}
863
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100864static const TypeInfo usb_keyboard_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600865 .name = "usb-kbd",
Gongleif5669122015-05-06 20:55:26 +0800866 .parent = TYPE_USB_HID,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600867 .class_init = usb_keyboard_class_initfn,
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200868};
869
Andreas Färber83f7d432012-02-09 15:20:55 +0100870static void usb_hid_register_types(void)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200871{
Gongleif5669122015-05-06 20:55:26 +0800872 type_register_static(&usb_hid_type_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600873 type_register_static(&usb_tablet_info);
Anthony Liguoriba024302011-12-08 14:56:53 -0600874 usb_legacy_register("usb-tablet", "tablet", NULL);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600875 type_register_static(&usb_mouse_info);
Anthony Liguoriba024302011-12-08 14:56:53 -0600876 usb_legacy_register("usb-mouse", "mouse", NULL);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600877 type_register_static(&usb_keyboard_info);
Anthony Liguoriba024302011-12-08 14:56:53 -0600878 usb_legacy_register("usb-kbd", "keyboard", NULL);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200879}
Andreas Färber83f7d432012-02-09 15:20:55 +0100880
881type_init(usb_hid_register_types)