blob: 67ec8b69ec011ac463936a00d0a2530982f3b825 [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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020025
Peter Maydelle532b2e2016-01-26 18:17:12 +000026#include "qemu/osdep.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/console.h"
Gerd Hoffmannf1ae32a2012-03-07 14:55:18 +010028#include "hw/usb.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020029#include "migration/vmstate.h"
Michael S. Tsirkin463581a2018-05-03 22:50:48 +030030#include "desc.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010031#include "qapi/error.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020032#include "qemu/module.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/timer.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010034#include "hw/input/hid.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020035#include "hw/qdev-properties.h"
bellard59ae5402005-11-05 16:57:08 +000036
37/* HID interface requests */
38#define GET_REPORT 0xa101
39#define GET_IDLE 0xa102
40#define GET_PROTOCOL 0xa103
balrog47b2d332007-06-22 08:16:00 +000041#define SET_REPORT 0x2109
bellard59ae5402005-11-05 16:57:08 +000042#define SET_IDLE 0x210a
43#define SET_PROTOCOL 0x210b
44
balrog47b2d332007-06-22 08:16:00 +000045/* HID descriptor types */
46#define USB_DT_HID 0x21
47#define USB_DT_REPORT 0x22
48#define USB_DT_PHY 0x23
49
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +020050typedef struct USBHIDState {
51 USBDevice dev;
Gerd Hoffmann7567b512012-01-17 13:25:46 +010052 USBEndpoint *intr;
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +020053 HIDState hid;
Hans de Goede427e3aa2012-11-17 12:47:18 +010054 uint32_t usb_version;
Gerd Hoffmannf85d2832014-05-19 15:26:51 +020055 char *display;
56 uint32_t head;
balrog47b2d332007-06-22 08:16:00 +000057} USBHIDState;
58
Gongleif5669122015-05-06 20:55:26 +080059#define TYPE_USB_HID "usb-hid"
60#define USB_HID(obj) OBJECT_CHECK(USBHIDState, (obj), TYPE_USB_HID)
61
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010062enum {
63 STR_MANUFACTURER = 1,
64 STR_PRODUCT_MOUSE,
65 STR_PRODUCT_TABLET,
66 STR_PRODUCT_KEYBOARD,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +010067 STR_SERIAL_COMPAT,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010068 STR_CONFIG_MOUSE,
69 STR_CONFIG_TABLET,
70 STR_CONFIG_KEYBOARD,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +010071 STR_SERIAL_MOUSE,
72 STR_SERIAL_TABLET,
73 STR_SERIAL_KEYBOARD,
bellard59ae5402005-11-05 16:57:08 +000074};
75
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010076static const USBDescStrings desc_strings = {
Crístian Viana93bfef42012-05-30 00:35:51 -030077 [STR_MANUFACTURER] = "QEMU",
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010078 [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
79 [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
80 [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
Gerd Hoffmannb63e1052019-01-10 13:51:08 +010081 [STR_SERIAL_COMPAT] = "42",
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010082 [STR_CONFIG_MOUSE] = "HID Mouse",
83 [STR_CONFIG_TABLET] = "HID Tablet",
84 [STR_CONFIG_KEYBOARD] = "HID Keyboard",
Gerd Hoffmannb63e1052019-01-10 13:51:08 +010085 [STR_SERIAL_MOUSE] = "89126",
86 [STR_SERIAL_TABLET] = "28754",
87 [STR_SERIAL_KEYBOARD] = "68284",
bellard09b26c52006-04-12 21:09:08 +000088};
89
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010090static const USBDescIface desc_iface_mouse = {
91 .bInterfaceNumber = 0,
92 .bNumEndpoints = 1,
93 .bInterfaceClass = USB_CLASS_HID,
94 .bInterfaceSubClass = 0x01, /* boot */
95 .bInterfaceProtocol = 0x02,
96 .ndesc = 1,
97 .descs = (USBDescOther[]) {
98 {
99 /* HID descriptor */
100 .data = (uint8_t[]) {
101 0x09, /* u8 bLength */
102 USB_DT_HID, /* u8 bDescriptorType */
103 0x01, 0x00, /* u16 HID_class */
104 0x00, /* u8 country_code */
105 0x01, /* u8 num_descriptors */
106 USB_DT_REPORT, /* u8 type: Report */
107 52, 0, /* u16 len */
108 },
109 },
110 },
111 .eps = (USBDescEndpoint[]) {
112 {
113 .bEndpointAddress = USB_DIR_IN | 0x01,
114 .bmAttributes = USB_ENDPOINT_XFER_INT,
115 .wMaxPacketSize = 4,
116 .bInterval = 0x0a,
117 },
118 },
bellard59ae5402005-11-05 16:57:08 +0000119};
120
Jan Vesely58e4fee2014-09-29 22:21:10 -0400121static const USBDescIface desc_iface_mouse2 = {
122 .bInterfaceNumber = 0,
123 .bNumEndpoints = 1,
124 .bInterfaceClass = USB_CLASS_HID,
125 .bInterfaceSubClass = 0x01, /* boot */
126 .bInterfaceProtocol = 0x02,
127 .ndesc = 1,
128 .descs = (USBDescOther[]) {
129 {
130 /* HID descriptor */
131 .data = (uint8_t[]) {
132 0x09, /* u8 bLength */
133 USB_DT_HID, /* u8 bDescriptorType */
134 0x01, 0x00, /* u16 HID_class */
135 0x00, /* u8 country_code */
136 0x01, /* u8 num_descriptors */
137 USB_DT_REPORT, /* u8 type: Report */
138 52, 0, /* u16 len */
139 },
140 },
141 },
142 .eps = (USBDescEndpoint[]) {
143 {
144 .bEndpointAddress = USB_DIR_IN | 0x01,
145 .bmAttributes = USB_ENDPOINT_XFER_INT,
146 .wMaxPacketSize = 4,
147 .bInterval = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
148 },
149 },
150};
151
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100152static const USBDescIface desc_iface_tablet = {
153 .bInterfaceNumber = 0,
154 .bNumEndpoints = 1,
155 .bInterfaceClass = USB_CLASS_HID,
Phil Dennis-Jordan0cd089e2017-01-25 18:24:35 +0100156 .bInterfaceProtocol = 0x00,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100157 .ndesc = 1,
158 .descs = (USBDescOther[]) {
159 {
160 /* HID descriptor */
161 .data = (uint8_t[]) {
162 0x09, /* u8 bLength */
163 USB_DT_HID, /* u8 bDescriptorType */
164 0x01, 0x00, /* u16 HID_class */
165 0x00, /* u8 country_code */
166 0x01, /* u8 num_descriptors */
167 USB_DT_REPORT, /* u8 type: Report */
168 74, 0, /* u16 len */
169 },
170 },
171 },
172 .eps = (USBDescEndpoint[]) {
173 {
174 .bEndpointAddress = USB_DIR_IN | 0x01,
175 .bmAttributes = USB_ENDPOINT_XFER_INT,
176 .wMaxPacketSize = 8,
177 .bInterval = 0x0a,
178 },
179 },
180};
balrog47b2d332007-06-22 08:16:00 +0000181
Hans de Goede427e3aa2012-11-17 12:47:18 +0100182static const USBDescIface desc_iface_tablet2 = {
183 .bInterfaceNumber = 0,
184 .bNumEndpoints = 1,
185 .bInterfaceClass = USB_CLASS_HID,
Phil Dennis-Jordan0cd089e2017-01-25 18:24:35 +0100186 .bInterfaceProtocol = 0x00,
Hans de Goede427e3aa2012-11-17 12:47:18 +0100187 .ndesc = 1,
188 .descs = (USBDescOther[]) {
189 {
190 /* HID descriptor */
191 .data = (uint8_t[]) {
192 0x09, /* u8 bLength */
193 USB_DT_HID, /* u8 bDescriptorType */
194 0x01, 0x00, /* u16 HID_class */
195 0x00, /* u8 country_code */
196 0x01, /* u8 num_descriptors */
197 USB_DT_REPORT, /* u8 type: Report */
198 74, 0, /* u16 len */
199 },
200 },
201 },
202 .eps = (USBDescEndpoint[]) {
203 {
204 .bEndpointAddress = USB_DIR_IN | 0x01,
205 .bmAttributes = USB_ENDPOINT_XFER_INT,
206 .wMaxPacketSize = 8,
207 .bInterval = 4, /* 2 ^ (4-1) * 125 usecs = 1 ms */
208 },
209 },
210};
211
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100212static const USBDescIface desc_iface_keyboard = {
213 .bInterfaceNumber = 0,
214 .bNumEndpoints = 1,
215 .bInterfaceClass = USB_CLASS_HID,
216 .bInterfaceSubClass = 0x01, /* boot */
217 .bInterfaceProtocol = 0x01, /* keyboard */
218 .ndesc = 1,
219 .descs = (USBDescOther[]) {
220 {
221 /* HID descriptor */
222 .data = (uint8_t[]) {
223 0x09, /* u8 bLength */
224 USB_DT_HID, /* u8 bDescriptorType */
225 0x11, 0x01, /* u16 HID_class */
226 0x00, /* u8 country_code */
227 0x01, /* u8 num_descriptors */
228 USB_DT_REPORT, /* u8 type: Report */
229 0x3f, 0, /* u16 len */
230 },
231 },
232 },
233 .eps = (USBDescEndpoint[]) {
234 {
235 .bEndpointAddress = USB_DIR_IN | 0x01,
236 .bmAttributes = USB_ENDPOINT_XFER_INT,
237 .wMaxPacketSize = 8,
238 .bInterval = 0x0a,
239 },
240 },
241};
balrog47b2d332007-06-22 08:16:00 +0000242
Jan Veselyb13ce072014-09-29 22:21:11 -0400243static const USBDescIface desc_iface_keyboard2 = {
244 .bInterfaceNumber = 0,
245 .bNumEndpoints = 1,
246 .bInterfaceClass = USB_CLASS_HID,
247 .bInterfaceSubClass = 0x01, /* boot */
248 .bInterfaceProtocol = 0x01, /* keyboard */
249 .ndesc = 1,
250 .descs = (USBDescOther[]) {
251 {
252 /* HID descriptor */
253 .data = (uint8_t[]) {
254 0x09, /* u8 bLength */
255 USB_DT_HID, /* u8 bDescriptorType */
256 0x11, 0x01, /* u16 HID_class */
257 0x00, /* u8 country_code */
258 0x01, /* u8 num_descriptors */
259 USB_DT_REPORT, /* u8 type: Report */
260 0x3f, 0, /* u16 len */
261 },
262 },
263 },
264 .eps = (USBDescEndpoint[]) {
265 {
266 .bEndpointAddress = USB_DIR_IN | 0x01,
267 .bmAttributes = USB_ENDPOINT_XFER_INT,
268 .wMaxPacketSize = 8,
269 .bInterval = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
270 },
271 },
272};
273
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100274static const USBDescDevice desc_device_mouse = {
275 .bcdUSB = 0x0100,
276 .bMaxPacketSize0 = 8,
277 .bNumConfigurations = 1,
278 .confs = (USBDescConfig[]) {
279 {
280 .bNumInterfaces = 1,
281 .bConfigurationValue = 1,
282 .iConfiguration = STR_CONFIG_MOUSE,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200283 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100284 .bMaxPower = 50,
Brad Hardsadd75082011-04-03 15:33:19 +1000285 .nif = 1,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100286 .ifs = &desc_iface_mouse,
287 },
288 },
289};
balrog47b2d332007-06-22 08:16:00 +0000290
Jan Vesely58e4fee2014-09-29 22:21:10 -0400291static const USBDescDevice desc_device_mouse2 = {
292 .bcdUSB = 0x0200,
293 .bMaxPacketSize0 = 64,
294 .bNumConfigurations = 1,
295 .confs = (USBDescConfig[]) {
296 {
297 .bNumInterfaces = 1,
298 .bConfigurationValue = 1,
299 .iConfiguration = STR_CONFIG_MOUSE,
300 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
301 .bMaxPower = 50,
302 .nif = 1,
303 .ifs = &desc_iface_mouse2,
304 },
305 },
306};
307
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100308static const USBDescDevice desc_device_tablet = {
309 .bcdUSB = 0x0100,
310 .bMaxPacketSize0 = 8,
311 .bNumConfigurations = 1,
312 .confs = (USBDescConfig[]) {
313 {
314 .bNumInterfaces = 1,
315 .bConfigurationValue = 1,
316 .iConfiguration = STR_CONFIG_TABLET,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200317 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100318 .bMaxPower = 50,
Brad Hardsadd75082011-04-03 15:33:19 +1000319 .nif = 1,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100320 .ifs = &desc_iface_tablet,
321 },
322 },
323};
balrog47b2d332007-06-22 08:16:00 +0000324
Hans de Goede427e3aa2012-11-17 12:47:18 +0100325static const USBDescDevice desc_device_tablet2 = {
326 .bcdUSB = 0x0200,
327 .bMaxPacketSize0 = 64,
328 .bNumConfigurations = 1,
329 .confs = (USBDescConfig[]) {
330 {
331 .bNumInterfaces = 1,
332 .bConfigurationValue = 1,
333 .iConfiguration = STR_CONFIG_TABLET,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200334 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
Hans de Goede427e3aa2012-11-17 12:47:18 +0100335 .bMaxPower = 50,
336 .nif = 1,
337 .ifs = &desc_iface_tablet2,
338 },
339 },
340};
341
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100342static const USBDescDevice desc_device_keyboard = {
343 .bcdUSB = 0x0100,
344 .bMaxPacketSize0 = 8,
345 .bNumConfigurations = 1,
346 .confs = (USBDescConfig[]) {
347 {
348 .bNumInterfaces = 1,
349 .bConfigurationValue = 1,
350 .iConfiguration = STR_CONFIG_KEYBOARD,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200351 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100352 .bMaxPower = 50,
Brad Hardsadd75082011-04-03 15:33:19 +1000353 .nif = 1,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100354 .ifs = &desc_iface_keyboard,
355 },
356 },
357};
358
Jan Veselyb13ce072014-09-29 22:21:11 -0400359static const USBDescDevice desc_device_keyboard2 = {
360 .bcdUSB = 0x0200,
361 .bMaxPacketSize0 = 64,
362 .bNumConfigurations = 1,
363 .confs = (USBDescConfig[]) {
364 {
365 .bNumInterfaces = 1,
366 .bConfigurationValue = 1,
367 .iConfiguration = STR_CONFIG_KEYBOARD,
368 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
369 .bMaxPower = 50,
370 .nif = 1,
371 .ifs = &desc_iface_keyboard2,
372 },
373 },
374};
375
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100376static const USBDescMSOS desc_msos_suspend = {
377 .SelectiveSuspendEnabled = true,
378};
379
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100380static const USBDesc desc_mouse = {
381 .id = {
382 .idVendor = 0x0627,
383 .idProduct = 0x0001,
384 .bcdDevice = 0,
385 .iManufacturer = STR_MANUFACTURER,
386 .iProduct = STR_PRODUCT_MOUSE,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100387 .iSerialNumber = STR_SERIAL_MOUSE,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100388 },
389 .full = &desc_device_mouse,
390 .str = desc_strings,
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100391 .msos = &desc_msos_suspend,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100392};
393
Jan Vesely58e4fee2014-09-29 22:21:10 -0400394static const USBDesc desc_mouse2 = {
395 .id = {
396 .idVendor = 0x0627,
397 .idProduct = 0x0001,
398 .bcdDevice = 0,
399 .iManufacturer = STR_MANUFACTURER,
400 .iProduct = STR_PRODUCT_MOUSE,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100401 .iSerialNumber = STR_SERIAL_MOUSE,
Jan Vesely58e4fee2014-09-29 22:21:10 -0400402 },
403 .full = &desc_device_mouse,
404 .high = &desc_device_mouse2,
405 .str = desc_strings,
406 .msos = &desc_msos_suspend,
407};
408
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100409static const USBDesc desc_tablet = {
410 .id = {
411 .idVendor = 0x0627,
412 .idProduct = 0x0001,
413 .bcdDevice = 0,
414 .iManufacturer = STR_MANUFACTURER,
415 .iProduct = STR_PRODUCT_TABLET,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100416 .iSerialNumber = STR_SERIAL_TABLET,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100417 },
418 .full = &desc_device_tablet,
419 .str = desc_strings,
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100420 .msos = &desc_msos_suspend,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100421};
422
Hans de Goede427e3aa2012-11-17 12:47:18 +0100423static const USBDesc desc_tablet2 = {
424 .id = {
425 .idVendor = 0x0627,
426 .idProduct = 0x0001,
427 .bcdDevice = 0,
428 .iManufacturer = STR_MANUFACTURER,
429 .iProduct = STR_PRODUCT_TABLET,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100430 .iSerialNumber = STR_SERIAL_TABLET,
Hans de Goede427e3aa2012-11-17 12:47:18 +0100431 },
432 .full = &desc_device_tablet,
433 .high = &desc_device_tablet2,
434 .str = desc_strings,
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100435 .msos = &desc_msos_suspend,
Hans de Goede427e3aa2012-11-17 12:47:18 +0100436};
437
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100438static const USBDesc desc_keyboard = {
439 .id = {
440 .idVendor = 0x0627,
441 .idProduct = 0x0001,
442 .bcdDevice = 0,
443 .iManufacturer = STR_MANUFACTURER,
444 .iProduct = STR_PRODUCT_KEYBOARD,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100445 .iSerialNumber = STR_SERIAL_KEYBOARD,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100446 },
447 .full = &desc_device_keyboard,
448 .str = desc_strings,
Gerd Hoffmann88678fb2013-11-20 07:33:50 +0100449 .msos = &desc_msos_suspend,
balrog47b2d332007-06-22 08:16:00 +0000450};
451
Jan Veselyb13ce072014-09-29 22:21:11 -0400452static const USBDesc desc_keyboard2 = {
453 .id = {
454 .idVendor = 0x0627,
455 .idProduct = 0x0001,
456 .bcdDevice = 0,
457 .iManufacturer = STR_MANUFACTURER,
458 .iProduct = STR_PRODUCT_KEYBOARD,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100459 .iSerialNumber = STR_SERIAL_KEYBOARD,
Jan Veselyb13ce072014-09-29 22:21:11 -0400460 },
461 .full = &desc_device_keyboard,
462 .high = &desc_device_keyboard2,
463 .str = desc_strings,
464 .msos = &desc_msos_suspend,
465};
466
bellard59ae5402005-11-05 16:57:08 +0000467static const uint8_t qemu_mouse_hid_report_descriptor[] = {
balrog976f8ee2008-05-17 19:55:28 +0000468 0x05, 0x01, /* Usage Page (Generic Desktop) */
469 0x09, 0x02, /* Usage (Mouse) */
470 0xa1, 0x01, /* Collection (Application) */
471 0x09, 0x01, /* Usage (Pointer) */
472 0xa1, 0x00, /* Collection (Physical) */
473 0x05, 0x09, /* Usage Page (Button) */
474 0x19, 0x01, /* Usage Minimum (1) */
475 0x29, 0x03, /* Usage Maximum (3) */
476 0x15, 0x00, /* Logical Minimum (0) */
477 0x25, 0x01, /* Logical Maximum (1) */
478 0x95, 0x03, /* Report Count (3) */
479 0x75, 0x01, /* Report Size (1) */
480 0x81, 0x02, /* Input (Data, Variable, Absolute) */
481 0x95, 0x01, /* Report Count (1) */
482 0x75, 0x05, /* Report Size (5) */
483 0x81, 0x01, /* Input (Constant) */
484 0x05, 0x01, /* Usage Page (Generic Desktop) */
485 0x09, 0x30, /* Usage (X) */
486 0x09, 0x31, /* Usage (Y) */
487 0x09, 0x38, /* Usage (Wheel) */
488 0x15, 0x81, /* Logical Minimum (-0x7f) */
489 0x25, 0x7f, /* Logical Maximum (0x7f) */
490 0x75, 0x08, /* Report Size (8) */
491 0x95, 0x03, /* Report Count (3) */
492 0x81, 0x06, /* Input (Data, Variable, Relative) */
493 0xc0, /* End Collection */
494 0xc0, /* End Collection */
bellard59ae5402005-11-05 16:57:08 +0000495};
496
bellard09b26c52006-04-12 21:09:08 +0000497static const uint8_t qemu_tablet_hid_report_descriptor[] = {
balrog976f8ee2008-05-17 19:55:28 +0000498 0x05, 0x01, /* Usage Page (Generic Desktop) */
Phil Dennis-Jordan0cd089e2017-01-25 18:24:35 +0100499 0x09, 0x02, /* Usage (Mouse) */
balrog976f8ee2008-05-17 19:55:28 +0000500 0xa1, 0x01, /* Collection (Application) */
501 0x09, 0x01, /* Usage (Pointer) */
502 0xa1, 0x00, /* Collection (Physical) */
503 0x05, 0x09, /* Usage Page (Button) */
504 0x19, 0x01, /* Usage Minimum (1) */
505 0x29, 0x03, /* Usage Maximum (3) */
506 0x15, 0x00, /* Logical Minimum (0) */
507 0x25, 0x01, /* Logical Maximum (1) */
508 0x95, 0x03, /* Report Count (3) */
509 0x75, 0x01, /* Report Size (1) */
510 0x81, 0x02, /* Input (Data, Variable, Absolute) */
511 0x95, 0x01, /* Report Count (1) */
512 0x75, 0x05, /* Report Size (5) */
513 0x81, 0x01, /* Input (Constant) */
514 0x05, 0x01, /* Usage Page (Generic Desktop) */
515 0x09, 0x30, /* Usage (X) */
516 0x09, 0x31, /* Usage (Y) */
517 0x15, 0x00, /* Logical Minimum (0) */
balrogde5c2d02008-09-15 22:26:35 +0000518 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
balrog976f8ee2008-05-17 19:55:28 +0000519 0x35, 0x00, /* Physical Minimum (0) */
balrogde5c2d02008-09-15 22:26:35 +0000520 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
balrog976f8ee2008-05-17 19:55:28 +0000521 0x75, 0x10, /* Report Size (16) */
522 0x95, 0x02, /* Report Count (2) */
523 0x81, 0x02, /* Input (Data, Variable, Absolute) */
524 0x05, 0x01, /* Usage Page (Generic Desktop) */
525 0x09, 0x38, /* Usage (Wheel) */
526 0x15, 0x81, /* Logical Minimum (-0x7f) */
527 0x25, 0x7f, /* Logical Maximum (0x7f) */
528 0x35, 0x00, /* Physical Minimum (same as logical) */
529 0x45, 0x00, /* Physical Maximum (same as logical) */
530 0x75, 0x08, /* Report Size (8) */
531 0x95, 0x01, /* Report Count (1) */
532 0x81, 0x06, /* Input (Data, Variable, Relative) */
533 0xc0, /* End Collection */
534 0xc0, /* End Collection */
bellard09b26c52006-04-12 21:09:08 +0000535};
536
balrog47b2d332007-06-22 08:16:00 +0000537static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
538 0x05, 0x01, /* Usage Page (Generic Desktop) */
539 0x09, 0x06, /* Usage (Keyboard) */
540 0xa1, 0x01, /* Collection (Application) */
541 0x75, 0x01, /* Report Size (1) */
542 0x95, 0x08, /* Report Count (8) */
543 0x05, 0x07, /* Usage Page (Key Codes) */
544 0x19, 0xe0, /* Usage Minimum (224) */
545 0x29, 0xe7, /* Usage Maximum (231) */
546 0x15, 0x00, /* Logical Minimum (0) */
547 0x25, 0x01, /* Logical Maximum (1) */
548 0x81, 0x02, /* Input (Data, Variable, Absolute) */
549 0x95, 0x01, /* Report Count (1) */
550 0x75, 0x08, /* Report Size (8) */
551 0x81, 0x01, /* Input (Constant) */
552 0x95, 0x05, /* Report Count (5) */
553 0x75, 0x01, /* Report Size (1) */
554 0x05, 0x08, /* Usage Page (LEDs) */
555 0x19, 0x01, /* Usage Minimum (1) */
556 0x29, 0x05, /* Usage Maximum (5) */
557 0x91, 0x02, /* Output (Data, Variable, Absolute) */
558 0x95, 0x01, /* Report Count (1) */
559 0x75, 0x03, /* Report Size (3) */
560 0x91, 0x01, /* Output (Constant) */
561 0x95, 0x06, /* Report Count (6) */
562 0x75, 0x08, /* Report Size (8) */
563 0x15, 0x00, /* Logical Minimum (0) */
564 0x25, 0xff, /* Logical Maximum (255) */
565 0x05, 0x07, /* Usage Page (Key Codes) */
566 0x19, 0x00, /* Usage Minimum (0) */
567 0x29, 0xff, /* Usage Maximum (255) */
568 0x81, 0x00, /* Input (Data, Array) */
569 0xc0, /* End Collection */
570};
571
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200572static void usb_hid_changed(HIDState *hs)
balrog47e699d2008-09-29 00:25:17 +0000573{
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200574 USBHIDState *us = container_of(hs, USBHIDState, hid);
balrog47e699d2008-09-29 00:25:17 +0000575
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100576 usb_wakeup(us->intr, 0);
balrog47e699d2008-09-29 00:25:17 +0000577}
578
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200579static void usb_hid_handle_reset(USBDevice *dev)
balrog47b2d332007-06-22 08:16:00 +0000580{
Gongleif5669122015-05-06 20:55:26 +0800581 USBHIDState *us = USB_HID(dev);
balrog47b2d332007-06-22 08:16:00 +0000582
Gerd Hoffmanndcfda672011-07-15 15:08:01 +0200583 hid_reset(&us->hid);
Kevin O'Connor68735b62010-02-13 18:32:17 -0500584}
585
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100586static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
Hans de Goede007fd622011-02-02 16:33:13 +0100587 int request, int value, int index, int length, uint8_t *data)
bellard59ae5402005-11-05 16:57:08 +0000588{
Gongleif5669122015-05-06 20:55:26 +0800589 USBHIDState *us = USB_HID(dev);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200590 HIDState *hs = &us->hid;
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100591 int ret;
bellard59ae5402005-11-05 16:57:08 +0000592
Hans de Goede007fd622011-02-02 16:33:13 +0100593 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100594 if (ret >= 0) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100595 return;
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100596 }
597
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200598 switch (request) {
bellard59ae5402005-11-05 16:57:08 +0000599 /* hid specific requests */
600 case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200601 switch (value >> 8) {
bellard59ae5402005-11-05 16:57:08 +0000602 case 0x22:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200603 if (hs->kind == HID_MOUSE) {
Paolo Bonzini7d374352018-12-13 23:37:37 +0100604 memcpy(data, qemu_mouse_hid_report_descriptor,
605 sizeof(qemu_mouse_hid_report_descriptor));
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100606 p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200607 } else if (hs->kind == HID_TABLET) {
608 memcpy(data, qemu_tablet_hid_report_descriptor,
Paolo Bonzini7d374352018-12-13 23:37:37 +0100609 sizeof(qemu_tablet_hid_report_descriptor));
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100610 p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200611 } else if (hs->kind == HID_KEYBOARD) {
ths5fafdf22007-09-16 21:08:06 +0000612 memcpy(data, qemu_keyboard_hid_report_descriptor,
balrog47b2d332007-06-22 08:16:00 +0000613 sizeof(qemu_keyboard_hid_report_descriptor));
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100614 p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
balrog47b2d332007-06-22 08:16:00 +0000615 }
616 break;
bellard59ae5402005-11-05 16:57:08 +0000617 default:
618 goto fail;
619 }
620 break;
621 case GET_REPORT:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200622 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100623 p->actual_length = hid_pointer_poll(hs, data, length);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200624 } else if (hs->kind == HID_KEYBOARD) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100625 p->actual_length = hid_keyboard_poll(hs, data, length);
Gerd Hoffmanne7e73892011-07-08 13:19:01 +0200626 }
balrog47b2d332007-06-22 08:16:00 +0000627 break;
628 case SET_REPORT:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200629 if (hs->kind == HID_KEYBOARD) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100630 p->actual_length = hid_keyboard_write(hs, data, length);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200631 } else {
balrog47b2d332007-06-22 08:16:00 +0000632 goto fail;
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200633 }
balrog47b2d332007-06-22 08:16:00 +0000634 break;
635 case GET_PROTOCOL:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200636 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
balrog47b2d332007-06-22 08:16:00 +0000637 goto fail;
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200638 }
Gerd Hoffmannb069d342011-07-15 15:52:33 +0200639 data[0] = hs->protocol;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100640 p->actual_length = 1;
balrog47b2d332007-06-22 08:16:00 +0000641 break;
642 case SET_PROTOCOL:
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200643 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
balrog47b2d332007-06-22 08:16:00 +0000644 goto fail;
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200645 }
Gerd Hoffmannb069d342011-07-15 15:52:33 +0200646 hs->protocol = value;
balrog47b2d332007-06-22 08:16:00 +0000647 break;
648 case GET_IDLE:
Gerd Hoffmannb069d342011-07-15 15:52:33 +0200649 data[0] = hs->idle;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100650 p->actual_length = 1;
bellard59ae5402005-11-05 16:57:08 +0000651 break;
652 case SET_IDLE:
Gerd Hoffmannb069d342011-07-15 15:52:33 +0200653 hs->idle = (uint8_t) (value >> 8);
Hans de Goede027c03f2012-12-14 14:35:38 +0100654 hid_set_next_idle(hs);
Gerd Hoffmann21635e12011-08-09 12:35:57 +0200655 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
656 hid_pointer_activate(hs);
657 }
bellard59ae5402005-11-05 16:57:08 +0000658 break;
659 default:
660 fail:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100661 p->status = USB_RET_STALL;
bellard59ae5402005-11-05 16:57:08 +0000662 break;
663 }
bellard59ae5402005-11-05 16:57:08 +0000664}
665
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100666static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
bellard59ae5402005-11-05 16:57:08 +0000667{
Gongleif5669122015-05-06 20:55:26 +0800668 USBHIDState *us = USB_HID(dev);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200669 HIDState *hs = &us->hid;
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200670 uint8_t buf[p->iov.size];
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100671 int len = 0;
bellard59ae5402005-11-05 16:57:08 +0000672
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200673 switch (p->pid) {
bellard59ae5402005-11-05 16:57:08 +0000674 case USB_TOKEN_IN:
Gerd Hoffmann079d0b72012-01-12 13:23:01 +0100675 if (p->ep->nr == 1) {
Gerd Hoffmann299aa1c2012-02-23 15:24:24 +0100676 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
677 hid_pointer_activate(hs);
678 }
Hans de Goede027c03f2012-12-14 14:35:38 +0100679 if (!hid_has_events(hs)) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100680 p->status = USB_RET_NAK;
681 return;
Paolo Bonzini13f8b972011-01-12 13:19:20 +0100682 }
Hans de Goede027c03f2012-12-14 14:35:38 +0100683 hid_set_next_idle(hs);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200684 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100685 len = hid_pointer_poll(hs, buf, p->iov.size);
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +0200686 } else if (hs->kind == HID_KEYBOARD) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100687 len = hid_keyboard_poll(hs, buf, p->iov.size);
Paolo Bonzini13f8b972011-01-12 13:19:20 +0100688 }
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100689 usb_packet_copy(p, buf, len);
bellard59ae5402005-11-05 16:57:08 +0000690 } else {
691 goto fail;
692 }
693 break;
694 case USB_TOKEN_OUT:
695 default:
696 fail:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100697 p->status = USB_RET_STALL;
bellard59ae5402005-11-05 16:57:08 +0000698 break;
699 }
bellard59ae5402005-11-05 16:57:08 +0000700}
701
Marc-André Lureauc4fe9702017-02-21 18:14:45 +0400702static void usb_hid_unrealize(USBDevice *dev, Error **errp)
bellard059809e2006-07-19 18:06:15 +0000703{
Gongleif5669122015-05-06 20:55:26 +0800704 USBHIDState *us = USB_HID(dev);
bellard059809e2006-07-19 18:06:15 +0000705
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200706 hid_free(&us->hid);
707}
708
Jan Veselyff326ff2014-09-29 22:21:09 -0400709static void usb_hid_initfn(USBDevice *dev, int kind,
710 const USBDesc *usb1, const USBDesc *usb2,
711 Error **errp)
bellard09b26c52006-04-12 21:09:08 +0000712{
Gongleif5669122015-05-06 20:55:26 +0800713 USBHIDState *us = USB_HID(dev);
Jan Veselyff326ff2014-09-29 22:21:09 -0400714 switch (us->usb_version) {
715 case 1:
716 dev->usb_desc = usb1;
717 break;
718 case 2:
719 dev->usb_desc = usb2;
720 break;
721 default:
722 dev->usb_desc = NULL;
723 }
724 if (!dev->usb_desc) {
725 error_setg(errp, "Invalid usb version %d for usb hid device",
726 us->usb_version);
727 return;
728 }
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100729
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100730 usb_desc_create_serial(dev);
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100731 usb_desc_init(dev);
Gerd Hoffmann7567b512012-01-17 13:25:46 +0100732 us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200733 hid_init(&us->hid, kind, usb_hid_changed);
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200734 if (us->display && us->hid.s) {
735 qemu_input_handler_bind(us->hid.s, us->display, us->head, NULL);
736 }
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200737}
bellard09b26c52006-04-12 21:09:08 +0000738
Gonglei276b7ac2014-09-19 14:48:36 +0800739static void usb_tablet_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200740{
Hans de Goede427e3aa2012-11-17 12:47:18 +0100741
Jan Veselyff326ff2014-09-29 22:21:09 -0400742 usb_hid_initfn(dev, HID_TABLET, &desc_tablet, &desc_tablet2, errp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200743}
bellard1f6e24e2006-06-26 21:00:51 +0000744
Gonglei276b7ac2014-09-19 14:48:36 +0800745static void usb_mouse_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200746{
Jan Vesely58e4fee2014-09-29 22:21:10 -0400747 usb_hid_initfn(dev, HID_MOUSE, &desc_mouse, &desc_mouse2, errp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200748}
749
Gonglei276b7ac2014-09-19 14:48:36 +0800750static void usb_keyboard_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200751{
Jan Veselyb13ce072014-09-29 22:21:11 -0400752 usb_hid_initfn(dev, HID_KEYBOARD, &desc_keyboard, &desc_keyboard2, errp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200753}
754
Gerd Hoffmann3a3286b2011-10-12 12:54:35 +0200755static int usb_ptr_post_load(void *opaque, int version_id)
756{
757 USBHIDState *s = opaque;
758
759 if (s->dev.remote_wakeup) {
760 hid_pointer_activate(&s->hid);
761 }
762 return 0;
763}
764
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100765static const VMStateDescription vmstate_usb_ptr = {
766 .name = "usb-ptr",
767 .version_id = 1,
768 .minimum_version_id = 1,
Gerd Hoffmann3a3286b2011-10-12 12:54:35 +0200769 .post_load = usb_ptr_post_load,
Juan Quintela6e3d6522014-04-16 13:31:26 +0200770 .fields = (VMStateField[]) {
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100771 VMSTATE_USB_DEVICE(dev, USBHIDState),
Michael Walle1f42d222011-08-09 23:54:54 +0200772 VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100773 VMSTATE_END_OF_LIST()
774 }
775};
776
777static const VMStateDescription vmstate_usb_kbd = {
778 .name = "usb-kbd",
779 .version_id = 1,
780 .minimum_version_id = 1,
Juan Quintela6e3d6522014-04-16 13:31:26 +0200781 .fields = (VMStateField[]) {
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100782 VMSTATE_USB_DEVICE(dev, USBHIDState),
Michael Walle1f42d222011-08-09 23:54:54 +0200783 VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100784 VMSTATE_END_OF_LIST()
785 }
786};
787
Anthony Liguori7f595602011-12-04 16:13:14 -0600788static void usb_hid_class_initfn(ObjectClass *klass, void *data)
Anthony Liguori62aed762011-12-15 14:53:10 -0600789{
790 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
791
Anthony Liguori62aed762011-12-15 14:53:10 -0600792 uc->handle_reset = usb_hid_handle_reset;
793 uc->handle_control = usb_hid_handle_control;
794 uc->handle_data = usb_hid_handle_data;
Marc-André Lureauc4fe9702017-02-21 18:14:45 +0400795 uc->unrealize = usb_hid_unrealize;
Hans de Goede427e3aa2012-11-17 12:47:18 +0100796 uc->handle_attach = usb_desc_attach;
Anthony Liguori62aed762011-12-15 14:53:10 -0600797}
798
Gongleif5669122015-05-06 20:55:26 +0800799static const TypeInfo usb_hid_type_info = {
800 .name = TYPE_USB_HID,
801 .parent = TYPE_USB_DEVICE,
802 .instance_size = sizeof(USBHIDState),
803 .abstract = true,
804 .class_init = usb_hid_class_initfn,
805};
806
Hans de Goede427e3aa2012-11-17 12:47:18 +0100807static Property usb_tablet_properties[] = {
808 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200809 DEFINE_PROP_STRING("display", USBHIDState, display),
810 DEFINE_PROP_UINT32("head", USBHIDState, head, 0),
Hans de Goede427e3aa2012-11-17 12:47:18 +0100811 DEFINE_PROP_END_OF_LIST(),
812};
813
Anthony Liguori7f595602011-12-04 16:13:14 -0600814static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
815{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600816 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori7f595602011-12-04 16:13:14 -0600817 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
818
Gonglei276b7ac2014-09-19 14:48:36 +0800819 uc->realize = usb_tablet_realize;
Anthony Liguori7f595602011-12-04 16:13:14 -0600820 uc->product_desc = "QEMU USB Tablet";
Anthony Liguori39bffca2011-12-07 21:34:16 -0600821 dc->vmsd = &vmstate_usb_ptr;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400822 device_class_set_props(dc, usb_tablet_properties);
Marcel Apfelbaum31efd2e2013-08-22 20:11:36 +0300823 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori7f595602011-12-04 16:13:14 -0600824}
825
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100826static const TypeInfo usb_tablet_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600827 .name = "usb-tablet",
Gongleif5669122015-05-06 20:55:26 +0800828 .parent = TYPE_USB_HID,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600829 .class_init = usb_tablet_class_initfn,
Anthony Liguori62aed762011-12-15 14:53:10 -0600830};
831
Jan Vesely58e4fee2014-09-29 22:21:10 -0400832static Property usb_mouse_properties[] = {
833 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
834 DEFINE_PROP_END_OF_LIST(),
835};
836
Anthony Liguori62aed762011-12-15 14:53:10 -0600837static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
838{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600839 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -0600840 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
841
Gonglei276b7ac2014-09-19 14:48:36 +0800842 uc->realize = usb_mouse_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -0600843 uc->product_desc = "QEMU USB Mouse";
Anthony Liguori39bffca2011-12-07 21:34:16 -0600844 dc->vmsd = &vmstate_usb_ptr;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400845 device_class_set_props(dc, usb_mouse_properties);
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300846 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori62aed762011-12-15 14:53:10 -0600847}
848
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100849static const TypeInfo usb_mouse_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600850 .name = "usb-mouse",
Gongleif5669122015-05-06 20:55:26 +0800851 .parent = TYPE_USB_HID,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600852 .class_init = usb_mouse_class_initfn,
Anthony Liguori62aed762011-12-15 14:53:10 -0600853};
854
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200855static Property usb_keyboard_properties[] = {
Jan Veselyb13ce072014-09-29 22:21:11 -0400856 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200857 DEFINE_PROP_STRING("display", USBHIDState, display),
858 DEFINE_PROP_END_OF_LIST(),
859};
860
Anthony Liguori62aed762011-12-15 14:53:10 -0600861static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
862{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600863 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -0600864 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
865
Gonglei276b7ac2014-09-19 14:48:36 +0800866 uc->realize = usb_keyboard_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -0600867 uc->product_desc = "QEMU USB Keyboard";
Anthony Liguori39bffca2011-12-07 21:34:16 -0600868 dc->vmsd = &vmstate_usb_kbd;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400869 device_class_set_props(dc, usb_keyboard_properties);
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300870 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori62aed762011-12-15 14:53:10 -0600871}
872
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100873static const TypeInfo usb_keyboard_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600874 .name = "usb-kbd",
Gongleif5669122015-05-06 20:55:26 +0800875 .parent = TYPE_USB_HID,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600876 .class_init = usb_keyboard_class_initfn,
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200877};
878
Andreas Färber83f7d432012-02-09 15:20:55 +0100879static void usb_hid_register_types(void)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200880{
Gongleif5669122015-05-06 20:55:26 +0800881 type_register_static(&usb_hid_type_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600882 type_register_static(&usb_tablet_info);
Anthony Liguoriba024302011-12-08 14:56:53 -0600883 usb_legacy_register("usb-tablet", "tablet", NULL);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600884 type_register_static(&usb_mouse_info);
Anthony Liguoriba024302011-12-08 14:56:53 -0600885 usb_legacy_register("usb-mouse", "mouse", NULL);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600886 type_register_static(&usb_keyboard_info);
Anthony Liguoriba024302011-12-08 14:56:53 -0600887 usb_legacy_register("usb-kbd", "keyboard", NULL);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200888}
Andreas Färber83f7d432012-02-09 15:20:55 +0100889
890type_init(usb_hid_register_types)