blob: bdd6d1ffafe432b75a5321305c5d0193e3e27bb1 [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"
César Belley84b6c232020-08-12 11:41:23 +020035#include "hw/usb/hid.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020036#include "hw/qdev-properties.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040037#include "qom/object.h"
bellard59ae5402005-11-05 16:57:08 +000038
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040039struct USBHIDState {
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +020040 USBDevice dev;
Gerd Hoffmann7567b512012-01-17 13:25:46 +010041 USBEndpoint *intr;
Gerd Hoffmann0d878ee2011-07-15 13:12:44 +020042 HIDState hid;
Hans de Goede427e3aa2012-11-17 12:47:18 +010043 uint32_t usb_version;
Gerd Hoffmannf85d2832014-05-19 15:26:51 +020044 char *display;
45 uint32_t head;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040046};
balrog47b2d332007-06-22 08:16:00 +000047
Gongleif5669122015-05-06 20:55:26 +080048#define TYPE_USB_HID "usb-hid"
Eduardo Habkost80633962020-09-16 14:25:19 -040049OBJECT_DECLARE_SIMPLE_TYPE(USBHIDState, USB_HID)
Gongleif5669122015-05-06 20:55:26 +080050
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010051enum {
52 STR_MANUFACTURER = 1,
53 STR_PRODUCT_MOUSE,
54 STR_PRODUCT_TABLET,
55 STR_PRODUCT_KEYBOARD,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +010056 STR_SERIAL_COMPAT,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010057 STR_CONFIG_MOUSE,
58 STR_CONFIG_TABLET,
59 STR_CONFIG_KEYBOARD,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +010060 STR_SERIAL_MOUSE,
61 STR_SERIAL_TABLET,
62 STR_SERIAL_KEYBOARD,
bellard59ae5402005-11-05 16:57:08 +000063};
64
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010065static const USBDescStrings desc_strings = {
Crístian Viana93bfef42012-05-30 00:35:51 -030066 [STR_MANUFACTURER] = "QEMU",
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010067 [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
68 [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
69 [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
Gerd Hoffmannb63e1052019-01-10 13:51:08 +010070 [STR_SERIAL_COMPAT] = "42",
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +010071 [STR_CONFIG_MOUSE] = "HID Mouse",
72 [STR_CONFIG_TABLET] = "HID Tablet",
73 [STR_CONFIG_KEYBOARD] = "HID Keyboard",
Gerd Hoffmannb63e1052019-01-10 13:51:08 +010074 [STR_SERIAL_MOUSE] = "89126",
75 [STR_SERIAL_TABLET] = "28754",
76 [STR_SERIAL_KEYBOARD] = "68284",
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,
Phil Dennis-Jordan0cd089e2017-01-25 18:24:35 +0100145 .bInterfaceProtocol = 0x00,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100146 .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,
Phil Dennis-Jordan0cd089e2017-01-25 18:24:35 +0100175 .bInterfaceProtocol = 0x00,
Hans de Goede427e3aa2012-11-17 12:47:18 +0100176 .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,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100376 .iSerialNumber = STR_SERIAL_MOUSE,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100377 },
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,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100390 .iSerialNumber = STR_SERIAL_MOUSE,
Jan Vesely58e4fee2014-09-29 22:21:10 -0400391 },
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,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100405 .iSerialNumber = STR_SERIAL_TABLET,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100406 },
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,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100419 .iSerialNumber = STR_SERIAL_TABLET,
Hans de Goede427e3aa2012-11-17 12:47:18 +0100420 },
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,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100434 .iSerialNumber = STR_SERIAL_KEYBOARD,
Gerd Hoffmann0e4e9692010-11-17 11:05:05 +0100435 },
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,
Gerd Hoffmannb63e1052019-01-10 13:51:08 +0100448 .iSerialNumber = STR_SERIAL_KEYBOARD,
Jan Veselyb13ce072014-09-29 22:21:11 -0400449 },
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) */
Noah Bergbauer1e2d5552021-11-26 15:04:37 +0100464 0x29, 0x05, /* Usage Maximum (5) */
balrog976f8ee2008-05-17 19:55:28 +0000465 0x15, 0x00, /* Logical Minimum (0) */
466 0x25, 0x01, /* Logical Maximum (1) */
Noah Bergbauer1e2d5552021-11-26 15:04:37 +0100467 0x95, 0x05, /* Report Count (5) */
balrog976f8ee2008-05-17 19:55:28 +0000468 0x75, 0x01, /* Report Size (1) */
469 0x81, 0x02, /* Input (Data, Variable, Absolute) */
470 0x95, 0x01, /* Report Count (1) */
Noah Bergbauer1e2d5552021-11-26 15:04:37 +0100471 0x75, 0x03, /* Report Size (3) */
balrog976f8ee2008-05-17 19:55:28 +0000472 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) */
Phil Dennis-Jordan0cd089e2017-01-25 18:24:35 +0100488 0x09, 0x02, /* Usage (Mouse) */
balrog976f8ee2008-05-17 19:55:28 +0000489 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) {
Paolo Bonzini7d374352018-12-13 23:37:37 +0100593 memcpy(data, qemu_mouse_hid_report_descriptor,
594 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,
Paolo Bonzini7d374352018-12-13 23:37:37 +0100598 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;
César Belley84b6c232020-08-12 11:41:23 +0200610 case HID_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;
César Belley84b6c232020-08-12 11:41:23 +0200617 case HID_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;
César Belley84b6c232020-08-12 11:41:23 +0200624 case HID_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;
César Belley84b6c232020-08-12 11:41:23 +0200631 case HID_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;
César Belley84b6c232020-08-12 11:41:23 +0200637 case HID_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;
César Belley84b6c232020-08-12 11:41:23 +0200641 case HID_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 Hoffmann3f67e2e2021-05-03 15:29:11 +0200659 g_autofree uint8_t *buf = g_malloc(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
Markus Armbrusterb69c3c22020-05-05 17:29:24 +0200691static void usb_hid_unrealize(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 Hoffmannb63e1052019-01-10 13:51:08 +0100719 usb_desc_create_serial(dev);
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100720 usb_desc_init(dev);
Gerd Hoffmann7567b512012-01-17 13:25:46 +0100721 us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
Gerd Hoffmann8bde6802011-07-15 14:37:15 +0200722 hid_init(&us->hid, kind, usb_hid_changed);
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200723 if (us->display && us->hid.s) {
724 qemu_input_handler_bind(us->hid.s, us->display, us->head, NULL);
725 }
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200726}
bellard09b26c52006-04-12 21:09:08 +0000727
Gonglei276b7ac2014-09-19 14:48:36 +0800728static void usb_tablet_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200729{
Hans de Goede427e3aa2012-11-17 12:47:18 +0100730
Jan Veselyff326ff2014-09-29 22:21:09 -0400731 usb_hid_initfn(dev, HID_TABLET, &desc_tablet, &desc_tablet2, errp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200732}
bellard1f6e24e2006-06-26 21:00:51 +0000733
Gonglei276b7ac2014-09-19 14:48:36 +0800734static void usb_mouse_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200735{
Jan Vesely58e4fee2014-09-29 22:21:10 -0400736 usb_hid_initfn(dev, HID_MOUSE, &desc_mouse, &desc_mouse2, errp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200737}
738
Gonglei276b7ac2014-09-19 14:48:36 +0800739static void usb_keyboard_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200740{
Jan Veselyb13ce072014-09-29 22:21:11 -0400741 usb_hid_initfn(dev, HID_KEYBOARD, &desc_keyboard, &desc_keyboard2, errp);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200742}
743
Gerd Hoffmann3a3286b2011-10-12 12:54:35 +0200744static int usb_ptr_post_load(void *opaque, int version_id)
745{
746 USBHIDState *s = opaque;
747
748 if (s->dev.remote_wakeup) {
749 hid_pointer_activate(&s->hid);
750 }
751 return 0;
752}
753
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100754static const VMStateDescription vmstate_usb_ptr = {
755 .name = "usb-ptr",
756 .version_id = 1,
757 .minimum_version_id = 1,
Gerd Hoffmann3a3286b2011-10-12 12:54:35 +0200758 .post_load = usb_ptr_post_load,
Juan Quintela6e3d6522014-04-16 13:31:26 +0200759 .fields = (VMStateField[]) {
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100760 VMSTATE_USB_DEVICE(dev, USBHIDState),
Michael Walle1f42d222011-08-09 23:54:54 +0200761 VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100762 VMSTATE_END_OF_LIST()
763 }
764};
765
766static const VMStateDescription vmstate_usb_kbd = {
767 .name = "usb-kbd",
768 .version_id = 1,
769 .minimum_version_id = 1,
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_KEYBOARD_DEVICE(hid, USBHIDState),
Gerd Hoffmannee59e6b2010-12-10 14:40:30 +0100773 VMSTATE_END_OF_LIST()
774 }
775};
776
Anthony Liguori7f595602011-12-04 16:13:14 -0600777static void usb_hid_class_initfn(ObjectClass *klass, void *data)
Anthony Liguori62aed762011-12-15 14:53:10 -0600778{
779 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
780
Anthony Liguori62aed762011-12-15 14:53:10 -0600781 uc->handle_reset = usb_hid_handle_reset;
782 uc->handle_control = usb_hid_handle_control;
783 uc->handle_data = usb_hid_handle_data;
Marc-André Lureauc4fe9702017-02-21 18:14:45 +0400784 uc->unrealize = usb_hid_unrealize;
Hans de Goede427e3aa2012-11-17 12:47:18 +0100785 uc->handle_attach = usb_desc_attach;
Anthony Liguori62aed762011-12-15 14:53:10 -0600786}
787
Gongleif5669122015-05-06 20:55:26 +0800788static const TypeInfo usb_hid_type_info = {
789 .name = TYPE_USB_HID,
790 .parent = TYPE_USB_DEVICE,
791 .instance_size = sizeof(USBHIDState),
792 .abstract = true,
793 .class_init = usb_hid_class_initfn,
794};
795
Hans de Goede427e3aa2012-11-17 12:47:18 +0100796static Property usb_tablet_properties[] = {
797 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200798 DEFINE_PROP_STRING("display", USBHIDState, display),
799 DEFINE_PROP_UINT32("head", USBHIDState, head, 0),
Hans de Goede427e3aa2012-11-17 12:47:18 +0100800 DEFINE_PROP_END_OF_LIST(),
801};
802
Anthony Liguori7f595602011-12-04 16:13:14 -0600803static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
804{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600805 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori7f595602011-12-04 16:13:14 -0600806 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
807
Gonglei276b7ac2014-09-19 14:48:36 +0800808 uc->realize = usb_tablet_realize;
Anthony Liguori7f595602011-12-04 16:13:14 -0600809 uc->product_desc = "QEMU USB Tablet";
Anthony Liguori39bffca2011-12-07 21:34:16 -0600810 dc->vmsd = &vmstate_usb_ptr;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400811 device_class_set_props(dc, usb_tablet_properties);
Marcel Apfelbaum31efd2e2013-08-22 20:11:36 +0300812 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori7f595602011-12-04 16:13:14 -0600813}
814
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100815static const TypeInfo usb_tablet_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600816 .name = "usb-tablet",
Gongleif5669122015-05-06 20:55:26 +0800817 .parent = TYPE_USB_HID,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600818 .class_init = usb_tablet_class_initfn,
Anthony Liguori62aed762011-12-15 14:53:10 -0600819};
820
Jan Vesely58e4fee2014-09-29 22:21:10 -0400821static Property usb_mouse_properties[] = {
822 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
823 DEFINE_PROP_END_OF_LIST(),
824};
825
Anthony Liguori62aed762011-12-15 14:53:10 -0600826static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
827{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600828 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -0600829 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
830
Gonglei276b7ac2014-09-19 14:48:36 +0800831 uc->realize = usb_mouse_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -0600832 uc->product_desc = "QEMU USB Mouse";
Anthony Liguori39bffca2011-12-07 21:34:16 -0600833 dc->vmsd = &vmstate_usb_ptr;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400834 device_class_set_props(dc, usb_mouse_properties);
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300835 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori62aed762011-12-15 14:53:10 -0600836}
837
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100838static const TypeInfo usb_mouse_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600839 .name = "usb-mouse",
Gongleif5669122015-05-06 20:55:26 +0800840 .parent = TYPE_USB_HID,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600841 .class_init = usb_mouse_class_initfn,
Anthony Liguori62aed762011-12-15 14:53:10 -0600842};
843
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200844static Property usb_keyboard_properties[] = {
Jan Veselyb13ce072014-09-29 22:21:11 -0400845 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
Gerd Hoffmannf85d2832014-05-19 15:26:51 +0200846 DEFINE_PROP_STRING("display", USBHIDState, display),
847 DEFINE_PROP_END_OF_LIST(),
848};
849
Anthony Liguori62aed762011-12-15 14:53:10 -0600850static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
851{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600852 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -0600853 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
854
Gonglei276b7ac2014-09-19 14:48:36 +0800855 uc->realize = usb_keyboard_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -0600856 uc->product_desc = "QEMU USB Keyboard";
Anthony Liguori39bffca2011-12-07 21:34:16 -0600857 dc->vmsd = &vmstate_usb_kbd;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400858 device_class_set_props(dc, usb_keyboard_properties);
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300859 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori62aed762011-12-15 14:53:10 -0600860}
861
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100862static const TypeInfo usb_keyboard_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600863 .name = "usb-kbd",
Gongleif5669122015-05-06 20:55:26 +0800864 .parent = TYPE_USB_HID,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600865 .class_init = usb_keyboard_class_initfn,
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200866};
867
Andreas Färber83f7d432012-02-09 15:20:55 +0100868static void usb_hid_register_types(void)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200869{
Gongleif5669122015-05-06 20:55:26 +0800870 type_register_static(&usb_hid_type_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600871 type_register_static(&usb_tablet_info);
Anthony Liguoriba024302011-12-08 14:56:53 -0600872 usb_legacy_register("usb-tablet", "tablet", NULL);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600873 type_register_static(&usb_mouse_info);
Anthony Liguoriba024302011-12-08 14:56:53 -0600874 usb_legacy_register("usb-mouse", "mouse", NULL);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600875 type_register_static(&usb_keyboard_info);
Anthony Liguoriba024302011-12-08 14:56:53 -0600876 usb_legacy_register("usb-kbd", "keyboard", NULL);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200877}
Andreas Färber83f7d432012-02-09 15:20:55 +0100878
879type_init(usb_hid_register_types)