]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/dev-hid.c
s390x: Migrate guest storage keys (initial memory only)
[mirror_qemu.git] / hw / usb / dev-hid.c
CommitLineData
59ae540c
FB
1/*
2 * QEMU USB HID devices
5fafdf24 3 *
59ae540c 4 * Copyright (c) 2005 Fabrice Bellard
47b2d338 5 * Copyright (c) 2007 OpenMoko, Inc. (andrew@openedhand.com)
5fafdf24 6 *
59ae540c
FB
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
f1ae32a1 25#include "hw/hw.h"
28ecbaee 26#include "ui/console.h"
f1ae32a1
GH
27#include "hw/usb.h"
28#include "hw/usb/desc.h"
1de7afc9 29#include "qemu/timer.h"
0d09e41a 30#include "hw/input/hid.h"
59ae540c
FB
31
32/* HID interface requests */
33#define GET_REPORT 0xa101
34#define GET_IDLE 0xa102
35#define GET_PROTOCOL 0xa103
47b2d338 36#define SET_REPORT 0x2109
59ae540c
FB
37#define SET_IDLE 0x210a
38#define SET_PROTOCOL 0x210b
39
47b2d338
AZ
40/* HID descriptor types */
41#define USB_DT_HID 0x21
42#define USB_DT_REPORT 0x22
43#define USB_DT_PHY 0x23
44
0d878eec
GH
45typedef struct USBHIDState {
46 USBDevice dev;
7567b51f 47 USBEndpoint *intr;
0d878eec 48 HIDState hid;
427e3aa1 49 uint32_t usb_version;
f85d2831
GH
50 char *display;
51 uint32_t head;
47b2d338
AZ
52} USBHIDState;
53
f5669129
GA
54#define TYPE_USB_HID "usb-hid"
55#define USB_HID(obj) OBJECT_CHECK(USBHIDState, (obj), TYPE_USB_HID)
56
0e4e9695
GH
57enum {
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,
59ae540c
FB
66};
67
0e4e9695 68static const USBDescStrings desc_strings = {
93bfef4c 69 [STR_MANUFACTURER] = "QEMU",
0e4e9695
GH
70 [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
71 [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
72 [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
7b074a22 73 [STR_SERIALNUMBER] = "42", /* == remote wakeup works */
0e4e9695
GH
74 [STR_CONFIG_MOUSE] = "HID Mouse",
75 [STR_CONFIG_TABLET] = "HID Tablet",
76 [STR_CONFIG_KEYBOARD] = "HID Keyboard",
09b26c5e
FB
77};
78
0e4e9695
GH
79static 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 },
59ae540c
FB
108};
109
58e4fee2
JV
110static 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
0e4e9695
GH
141static const USBDescIface desc_iface_tablet = {
142 .bInterfaceNumber = 0,
143 .bNumEndpoints = 1,
144 .bInterfaceClass = USB_CLASS_HID,
0e4e9695
GH
145 .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};
170
427e3aa1
HG
171static 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
0e4e9695
GH
201static 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};
231
b13ce076
JV
232static 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
0e4e9695
GH
263static 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,
bd93976a 272 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
0e4e9695 273 .bMaxPower = 50,
add75088 274 .nif = 1,
0e4e9695
GH
275 .ifs = &desc_iface_mouse,
276 },
277 },
278};
279
58e4fee2
JV
280static 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
0e4e9695
GH
297static 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,
bd93976a 306 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
0e4e9695 307 .bMaxPower = 50,
add75088 308 .nif = 1,
0e4e9695
GH
309 .ifs = &desc_iface_tablet,
310 },
311 },
312};
313
427e3aa1
HG
314static 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,
bd93976a 323 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
427e3aa1
HG
324 .bMaxPower = 50,
325 .nif = 1,
326 .ifs = &desc_iface_tablet2,
327 },
328 },
329};
330
0e4e9695
GH
331static 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,
bd93976a 340 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
0e4e9695 341 .bMaxPower = 50,
add75088 342 .nif = 1,
0e4e9695
GH
343 .ifs = &desc_iface_keyboard,
344 },
345 },
346};
347
b13ce076
JV
348static 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
88678fbd
GH
365static const USBDescMSOS desc_msos_suspend = {
366 .SelectiveSuspendEnabled = true,
367};
368
0e4e9695
GH
369static 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,
88678fbd 380 .msos = &desc_msos_suspend,
0e4e9695
GH
381};
382
58e4fee2
JV
383static 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
0e4e9695
GH
398static 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,
88678fbd 409 .msos = &desc_msos_suspend,
0e4e9695
GH
410};
411
427e3aa1
HG
412static 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,
88678fbd 424 .msos = &desc_msos_suspend,
427e3aa1
HG
425};
426
0e4e9695
GH
427static 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,
88678fbd 438 .msos = &desc_msos_suspend,
47b2d338
AZ
439};
440
b13ce076
JV
441static 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
59ae540c 456static const uint8_t qemu_mouse_hid_report_descriptor[] = {
976f8eef
AZ
457 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 */
59ae540c
FB
484};
485
09b26c5e 486static const uint8_t qemu_tablet_hid_report_descriptor[] = {
976f8eef
AZ
487 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) */
de5c2d0a 507 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
976f8eef 508 0x35, 0x00, /* Physical Minimum (0) */
de5c2d0a 509 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
976f8eef
AZ
510 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 */
09b26c5e
FB
524};
525
47b2d338
AZ
526static 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
8bde6805 561static void usb_hid_changed(HIDState *hs)
47e699dc 562{
8bde6805 563 USBHIDState *us = container_of(hs, USBHIDState, hid);
47e699dc 564
8550a02d 565 usb_wakeup(us->intr, 0);
47e699dc
AZ
566}
567
8bde6805 568static void usb_hid_handle_reset(USBDevice *dev)
47b2d338 569{
f5669129 570 USBHIDState *us = USB_HID(dev);
0d878eec 571
dcfda673 572 hid_reset(&us->hid);
68735b6c
KC
573}
574
9a77a0f5 575static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
007fd62f 576 int request, int value, int index, int length, uint8_t *data)
59ae540c 577{
f5669129 578 USBHIDState *us = USB_HID(dev);
0d878eec 579 HIDState *hs = &us->hid;
0e4e9695 580 int ret;
59ae540c 581
007fd62f 582 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
0e4e9695 583 if (ret >= 0) {
9a77a0f5 584 return;
0e4e9695 585 }
59ae540c 586
0d878eec 587 switch (request) {
59ae540c
FB
588 /* hid specific requests */
589 case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
0d878eec 590 switch (value >> 8) {
59ae540c 591 case 0x22:
0d878eec 592 if (hs->kind == HID_MOUSE) {
5fafdf24 593 memcpy(data, qemu_mouse_hid_report_descriptor,
09b26c5e 594 sizeof(qemu_mouse_hid_report_descriptor));
9a77a0f5 595 p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
0d878eec
GH
596 } else if (hs->kind == HID_TABLET) {
597 memcpy(data, qemu_tablet_hid_report_descriptor,
09b26c5e 598 sizeof(qemu_tablet_hid_report_descriptor));
9a77a0f5 599 p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
0d878eec 600 } else if (hs->kind == HID_KEYBOARD) {
5fafdf24 601 memcpy(data, qemu_keyboard_hid_report_descriptor,
47b2d338 602 sizeof(qemu_keyboard_hid_report_descriptor));
9a77a0f5 603 p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
47b2d338
AZ
604 }
605 break;
59ae540c
FB
606 default:
607 goto fail;
608 }
609 break;
610 case GET_REPORT:
0d878eec 611 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
9a77a0f5 612 p->actual_length = hid_pointer_poll(hs, data, length);
0d878eec 613 } else if (hs->kind == HID_KEYBOARD) {
9a77a0f5 614 p->actual_length = hid_keyboard_poll(hs, data, length);
e7e73892 615 }
47b2d338
AZ
616 break;
617 case SET_REPORT:
0d878eec 618 if (hs->kind == HID_KEYBOARD) {
9a77a0f5 619 p->actual_length = hid_keyboard_write(hs, data, length);
0d878eec 620 } else {
47b2d338 621 goto fail;
0d878eec 622 }
47b2d338
AZ
623 break;
624 case GET_PROTOCOL:
0d878eec 625 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
47b2d338 626 goto fail;
0d878eec 627 }
b069d348 628 data[0] = hs->protocol;
9a77a0f5 629 p->actual_length = 1;
47b2d338
AZ
630 break;
631 case SET_PROTOCOL:
0d878eec 632 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
47b2d338 633 goto fail;
0d878eec 634 }
b069d348 635 hs->protocol = value;
47b2d338
AZ
636 break;
637 case GET_IDLE:
b069d348 638 data[0] = hs->idle;
9a77a0f5 639 p->actual_length = 1;
59ae540c
FB
640 break;
641 case SET_IDLE:
b069d348 642 hs->idle = (uint8_t) (value >> 8);
027c03f7 643 hid_set_next_idle(hs);
21635e12
GH
644 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
645 hid_pointer_activate(hs);
646 }
59ae540c
FB
647 break;
648 default:
649 fail:
9a77a0f5 650 p->status = USB_RET_STALL;
59ae540c
FB
651 break;
652 }
59ae540c
FB
653}
654
9a77a0f5 655static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
59ae540c 656{
f5669129 657 USBHIDState *us = USB_HID(dev);
0d878eec 658 HIDState *hs = &us->hid;
4f4321c1 659 uint8_t buf[p->iov.size];
9a77a0f5 660 int len = 0;
59ae540c 661
0d878eec 662 switch (p->pid) {
59ae540c 663 case USB_TOKEN_IN:
079d0b7f 664 if (p->ep->nr == 1) {
299aa1c6
GH
665 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
666 hid_pointer_activate(hs);
667 }
027c03f7 668 if (!hid_has_events(hs)) {
9a77a0f5
HG
669 p->status = USB_RET_NAK;
670 return;
13f8b97a 671 }
027c03f7 672 hid_set_next_idle(hs);
0d878eec 673 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
9a77a0f5 674 len = hid_pointer_poll(hs, buf, p->iov.size);
0d878eec 675 } else if (hs->kind == HID_KEYBOARD) {
9a77a0f5 676 len = hid_keyboard_poll(hs, buf, p->iov.size);
13f8b97a 677 }
9a77a0f5 678 usb_packet_copy(p, buf, len);
59ae540c
FB
679 } else {
680 goto fail;
681 }
682 break;
683 case USB_TOKEN_OUT:
684 default:
685 fail:
9a77a0f5 686 p->status = USB_RET_STALL;
59ae540c
FB
687 break;
688 }
59ae540c
FB
689}
690
8bde6805 691static void usb_hid_handle_destroy(USBDevice *dev)
09b26c5e 692{
f5669129 693 USBHIDState *us = USB_HID(dev);
a980a065 694
8bde6805
GH
695 hid_free(&us->hid);
696}
697
ff326ffa
JV
698static void usb_hid_initfn(USBDevice *dev, int kind,
699 const USBDesc *usb1, const USBDesc *usb2,
700 Error **errp)
8bde6805 701{
f5669129 702 USBHIDState *us = USB_HID(dev);
ff326ffa
JV
703 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 }
8bde6805 718
71938a09
GH
719 if (dev->serial) {
720 usb_desc_set_string(dev, STR_SERIALNUMBER, dev->serial);
721 }
8bde6805 722 usb_desc_init(dev);
7567b51f 723 us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
8bde6805 724 hid_init(&us->hid, kind, usb_hid_changed);
f85d2831
GH
725 if (us->display && us->hid.s) {
726 qemu_input_handler_bind(us->hid.s, us->display, us->head, NULL);
727 }
09b26c5e
FB
728}
729
276b7ac8 730static void usb_tablet_realize(USBDevice *dev, Error **errp)
59ae540c 731{
427e3aa1 732
ff326ffa 733 usb_hid_initfn(dev, HID_TABLET, &desc_tablet, &desc_tablet2, errp);
806b6024 734}
59ae540c 735
276b7ac8 736static void usb_mouse_realize(USBDevice *dev, Error **errp)
806b6024 737{
58e4fee2 738 usb_hid_initfn(dev, HID_MOUSE, &desc_mouse, &desc_mouse2, errp);
806b6024 739}
59ae540c 740
276b7ac8 741static void usb_keyboard_realize(USBDevice *dev, Error **errp)
806b6024 742{
b13ce076 743 usb_hid_initfn(dev, HID_KEYBOARD, &desc_keyboard, &desc_keyboard2, errp);
806b6024 744}
59ae540c 745
3a3286bf
GH
746static 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
ee59e6b3
GH
756static const VMStateDescription vmstate_usb_ptr = {
757 .name = "usb-ptr",
758 .version_id = 1,
759 .minimum_version_id = 1,
3a3286bf 760 .post_load = usb_ptr_post_load,
6e3d652a 761 .fields = (VMStateField[]) {
ee59e6b3 762 VMSTATE_USB_DEVICE(dev, USBHIDState),
1f42d222 763 VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
ee59e6b3
GH
764 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,
6e3d652a 772 .fields = (VMStateField[]) {
ee59e6b3 773 VMSTATE_USB_DEVICE(dev, USBHIDState),
1f42d222 774 VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
ee59e6b3
GH
775 VMSTATE_END_OF_LIST()
776 }
777};
778
7f595609 779static void usb_hid_class_initfn(ObjectClass *klass, void *data)
62aed765
AL
780{
781 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
782
62aed765
AL
783 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;
427e3aa1 787 uc->handle_attach = usb_desc_attach;
62aed765
AL
788}
789
f5669129
GA
790static 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
427e3aa1
HG
798static Property usb_tablet_properties[] = {
799 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
f85d2831
GH
800 DEFINE_PROP_STRING("display", USBHIDState, display),
801 DEFINE_PROP_UINT32("head", USBHIDState, head, 0),
427e3aa1
HG
802 DEFINE_PROP_END_OF_LIST(),
803};
804
7f595609
AL
805static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
806{
39bffca2 807 DeviceClass *dc = DEVICE_CLASS(klass);
7f595609
AL
808 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
809
276b7ac8 810 uc->realize = usb_tablet_realize;
7f595609 811 uc->product_desc = "QEMU USB Tablet";
39bffca2 812 dc->vmsd = &vmstate_usb_ptr;
427e3aa1 813 dc->props = usb_tablet_properties;
31efd2e8 814 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
7f595609
AL
815}
816
8c43a6f0 817static const TypeInfo usb_tablet_info = {
39bffca2 818 .name = "usb-tablet",
f5669129 819 .parent = TYPE_USB_HID,
39bffca2 820 .class_init = usb_tablet_class_initfn,
62aed765
AL
821};
822
58e4fee2
JV
823static Property usb_mouse_properties[] = {
824 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
825 DEFINE_PROP_END_OF_LIST(),
826};
827
62aed765
AL
828static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
829{
39bffca2 830 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
831 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
832
276b7ac8 833 uc->realize = usb_mouse_realize;
62aed765 834 uc->product_desc = "QEMU USB Mouse";
39bffca2 835 dc->vmsd = &vmstate_usb_ptr;
58e4fee2 836 dc->props = usb_mouse_properties;
125ee0ed 837 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
62aed765
AL
838}
839
8c43a6f0 840static const TypeInfo usb_mouse_info = {
39bffca2 841 .name = "usb-mouse",
f5669129 842 .parent = TYPE_USB_HID,
39bffca2 843 .class_init = usb_mouse_class_initfn,
62aed765
AL
844};
845
f85d2831 846static Property usb_keyboard_properties[] = {
b13ce076 847 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
f85d2831
GH
848 DEFINE_PROP_STRING("display", USBHIDState, display),
849 DEFINE_PROP_END_OF_LIST(),
850};
851
62aed765
AL
852static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
853{
39bffca2 854 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
855 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
856
276b7ac8 857 uc->realize = usb_keyboard_realize;
62aed765 858 uc->product_desc = "QEMU USB Keyboard";
39bffca2 859 dc->vmsd = &vmstate_usb_kbd;
f85d2831 860 dc->props = usb_keyboard_properties;
125ee0ed 861 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
62aed765
AL
862}
863
8c43a6f0 864static const TypeInfo usb_keyboard_info = {
39bffca2 865 .name = "usb-kbd",
f5669129 866 .parent = TYPE_USB_HID,
39bffca2 867 .class_init = usb_keyboard_class_initfn,
806b6024
GH
868};
869
83f7d43a 870static void usb_hid_register_types(void)
806b6024 871{
f5669129 872 type_register_static(&usb_hid_type_info);
39bffca2 873 type_register_static(&usb_tablet_info);
ba02430f 874 usb_legacy_register("usb-tablet", "tablet", NULL);
39bffca2 875 type_register_static(&usb_mouse_info);
ba02430f 876 usb_legacy_register("usb-mouse", "mouse", NULL);
39bffca2 877 type_register_static(&usb_keyboard_info);
ba02430f 878 usb_legacy_register("usb-kbd", "keyboard", NULL);
806b6024 879}
83f7d43a
AF
880
881type_init(usb_hid_register_types)