]> Git Repo - qemu.git/blame - hw/usb/dev-hid.c
Merge remote-tracking branch 'remotes/armbru/tags/pull-misc-2019-06-11-v3' into staging
[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. ([email protected])
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 */
0b8fa32f 25
e532b2e0 26#include "qemu/osdep.h"
f1ae32a1 27#include "hw/hw.h"
28ecbaee 28#include "ui/console.h"
f1ae32a1 29#include "hw/usb.h"
463581a8 30#include "desc.h"
da34e65c 31#include "qapi/error.h"
0b8fa32f 32#include "qemu/module.h"
1de7afc9 33#include "qemu/timer.h"
0d09e41a 34#include "hw/input/hid.h"
59ae540c
FB
35
36/* HID interface requests */
37#define GET_REPORT 0xa101
38#define GET_IDLE 0xa102
39#define GET_PROTOCOL 0xa103
47b2d338 40#define SET_REPORT 0x2109
59ae540c
FB
41#define SET_IDLE 0x210a
42#define SET_PROTOCOL 0x210b
43
47b2d338
AZ
44/* HID descriptor types */
45#define USB_DT_HID 0x21
46#define USB_DT_REPORT 0x22
47#define USB_DT_PHY 0x23
48
0d878eec
GH
49typedef struct USBHIDState {
50 USBDevice dev;
7567b51f 51 USBEndpoint *intr;
0d878eec 52 HIDState hid;
427e3aa1 53 uint32_t usb_version;
f85d2831
GH
54 char *display;
55 uint32_t head;
47b2d338
AZ
56} USBHIDState;
57
f5669129
GA
58#define TYPE_USB_HID "usb-hid"
59#define USB_HID(obj) OBJECT_CHECK(USBHIDState, (obj), TYPE_USB_HID)
60
0e4e9695
GH
61enum {
62 STR_MANUFACTURER = 1,
63 STR_PRODUCT_MOUSE,
64 STR_PRODUCT_TABLET,
65 STR_PRODUCT_KEYBOARD,
b63e1050 66 STR_SERIAL_COMPAT,
0e4e9695
GH
67 STR_CONFIG_MOUSE,
68 STR_CONFIG_TABLET,
69 STR_CONFIG_KEYBOARD,
b63e1050
GH
70 STR_SERIAL_MOUSE,
71 STR_SERIAL_TABLET,
72 STR_SERIAL_KEYBOARD,
59ae540c
FB
73};
74
0e4e9695 75static const USBDescStrings desc_strings = {
93bfef4c 76 [STR_MANUFACTURER] = "QEMU",
0e4e9695
GH
77 [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
78 [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
79 [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
b63e1050 80 [STR_SERIAL_COMPAT] = "42",
0e4e9695
GH
81 [STR_CONFIG_MOUSE] = "HID Mouse",
82 [STR_CONFIG_TABLET] = "HID Tablet",
83 [STR_CONFIG_KEYBOARD] = "HID Keyboard",
b63e1050
GH
84 [STR_SERIAL_MOUSE] = "89126",
85 [STR_SERIAL_TABLET] = "28754",
86 [STR_SERIAL_KEYBOARD] = "68284",
09b26c5e
FB
87};
88
0e4e9695
GH
89static const USBDescIface desc_iface_mouse = {
90 .bInterfaceNumber = 0,
91 .bNumEndpoints = 1,
92 .bInterfaceClass = USB_CLASS_HID,
93 .bInterfaceSubClass = 0x01, /* boot */
94 .bInterfaceProtocol = 0x02,
95 .ndesc = 1,
96 .descs = (USBDescOther[]) {
97 {
98 /* HID descriptor */
99 .data = (uint8_t[]) {
100 0x09, /* u8 bLength */
101 USB_DT_HID, /* u8 bDescriptorType */
102 0x01, 0x00, /* u16 HID_class */
103 0x00, /* u8 country_code */
104 0x01, /* u8 num_descriptors */
105 USB_DT_REPORT, /* u8 type: Report */
106 52, 0, /* u16 len */
107 },
108 },
109 },
110 .eps = (USBDescEndpoint[]) {
111 {
112 .bEndpointAddress = USB_DIR_IN | 0x01,
113 .bmAttributes = USB_ENDPOINT_XFER_INT,
114 .wMaxPacketSize = 4,
115 .bInterval = 0x0a,
116 },
117 },
59ae540c
FB
118};
119
58e4fee2
JV
120static const USBDescIface desc_iface_mouse2 = {
121 .bInterfaceNumber = 0,
122 .bNumEndpoints = 1,
123 .bInterfaceClass = USB_CLASS_HID,
124 .bInterfaceSubClass = 0x01, /* boot */
125 .bInterfaceProtocol = 0x02,
126 .ndesc = 1,
127 .descs = (USBDescOther[]) {
128 {
129 /* HID descriptor */
130 .data = (uint8_t[]) {
131 0x09, /* u8 bLength */
132 USB_DT_HID, /* u8 bDescriptorType */
133 0x01, 0x00, /* u16 HID_class */
134 0x00, /* u8 country_code */
135 0x01, /* u8 num_descriptors */
136 USB_DT_REPORT, /* u8 type: Report */
137 52, 0, /* u16 len */
138 },
139 },
140 },
141 .eps = (USBDescEndpoint[]) {
142 {
143 .bEndpointAddress = USB_DIR_IN | 0x01,
144 .bmAttributes = USB_ENDPOINT_XFER_INT,
145 .wMaxPacketSize = 4,
146 .bInterval = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
147 },
148 },
149};
150
0e4e9695
GH
151static const USBDescIface desc_iface_tablet = {
152 .bInterfaceNumber = 0,
153 .bNumEndpoints = 1,
154 .bInterfaceClass = USB_CLASS_HID,
0cd089e9 155 .bInterfaceProtocol = 0x00,
0e4e9695
GH
156 .ndesc = 1,
157 .descs = (USBDescOther[]) {
158 {
159 /* HID descriptor */
160 .data = (uint8_t[]) {
161 0x09, /* u8 bLength */
162 USB_DT_HID, /* u8 bDescriptorType */
163 0x01, 0x00, /* u16 HID_class */
164 0x00, /* u8 country_code */
165 0x01, /* u8 num_descriptors */
166 USB_DT_REPORT, /* u8 type: Report */
167 74, 0, /* u16 len */
168 },
169 },
170 },
171 .eps = (USBDescEndpoint[]) {
172 {
173 .bEndpointAddress = USB_DIR_IN | 0x01,
174 .bmAttributes = USB_ENDPOINT_XFER_INT,
175 .wMaxPacketSize = 8,
176 .bInterval = 0x0a,
177 },
178 },
179};
180
427e3aa1
HG
181static const USBDescIface desc_iface_tablet2 = {
182 .bInterfaceNumber = 0,
183 .bNumEndpoints = 1,
184 .bInterfaceClass = USB_CLASS_HID,
0cd089e9 185 .bInterfaceProtocol = 0x00,
427e3aa1
HG
186 .ndesc = 1,
187 .descs = (USBDescOther[]) {
188 {
189 /* HID descriptor */
190 .data = (uint8_t[]) {
191 0x09, /* u8 bLength */
192 USB_DT_HID, /* u8 bDescriptorType */
193 0x01, 0x00, /* u16 HID_class */
194 0x00, /* u8 country_code */
195 0x01, /* u8 num_descriptors */
196 USB_DT_REPORT, /* u8 type: Report */
197 74, 0, /* u16 len */
198 },
199 },
200 },
201 .eps = (USBDescEndpoint[]) {
202 {
203 .bEndpointAddress = USB_DIR_IN | 0x01,
204 .bmAttributes = USB_ENDPOINT_XFER_INT,
205 .wMaxPacketSize = 8,
206 .bInterval = 4, /* 2 ^ (4-1) * 125 usecs = 1 ms */
207 },
208 },
209};
210
0e4e9695
GH
211static const USBDescIface desc_iface_keyboard = {
212 .bInterfaceNumber = 0,
213 .bNumEndpoints = 1,
214 .bInterfaceClass = USB_CLASS_HID,
215 .bInterfaceSubClass = 0x01, /* boot */
216 .bInterfaceProtocol = 0x01, /* keyboard */
217 .ndesc = 1,
218 .descs = (USBDescOther[]) {
219 {
220 /* HID descriptor */
221 .data = (uint8_t[]) {
222 0x09, /* u8 bLength */
223 USB_DT_HID, /* u8 bDescriptorType */
224 0x11, 0x01, /* u16 HID_class */
225 0x00, /* u8 country_code */
226 0x01, /* u8 num_descriptors */
227 USB_DT_REPORT, /* u8 type: Report */
228 0x3f, 0, /* u16 len */
229 },
230 },
231 },
232 .eps = (USBDescEndpoint[]) {
233 {
234 .bEndpointAddress = USB_DIR_IN | 0x01,
235 .bmAttributes = USB_ENDPOINT_XFER_INT,
236 .wMaxPacketSize = 8,
237 .bInterval = 0x0a,
238 },
239 },
240};
241
b13ce076
JV
242static const USBDescIface desc_iface_keyboard2 = {
243 .bInterfaceNumber = 0,
244 .bNumEndpoints = 1,
245 .bInterfaceClass = USB_CLASS_HID,
246 .bInterfaceSubClass = 0x01, /* boot */
247 .bInterfaceProtocol = 0x01, /* keyboard */
248 .ndesc = 1,
249 .descs = (USBDescOther[]) {
250 {
251 /* HID descriptor */
252 .data = (uint8_t[]) {
253 0x09, /* u8 bLength */
254 USB_DT_HID, /* u8 bDescriptorType */
255 0x11, 0x01, /* u16 HID_class */
256 0x00, /* u8 country_code */
257 0x01, /* u8 num_descriptors */
258 USB_DT_REPORT, /* u8 type: Report */
259 0x3f, 0, /* u16 len */
260 },
261 },
262 },
263 .eps = (USBDescEndpoint[]) {
264 {
265 .bEndpointAddress = USB_DIR_IN | 0x01,
266 .bmAttributes = USB_ENDPOINT_XFER_INT,
267 .wMaxPacketSize = 8,
268 .bInterval = 7, /* 2 ^ (8-1) * 125 usecs = 8 ms */
269 },
270 },
271};
272
0e4e9695
GH
273static const USBDescDevice desc_device_mouse = {
274 .bcdUSB = 0x0100,
275 .bMaxPacketSize0 = 8,
276 .bNumConfigurations = 1,
277 .confs = (USBDescConfig[]) {
278 {
279 .bNumInterfaces = 1,
280 .bConfigurationValue = 1,
281 .iConfiguration = STR_CONFIG_MOUSE,
bd93976a 282 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
0e4e9695 283 .bMaxPower = 50,
add75088 284 .nif = 1,
0e4e9695
GH
285 .ifs = &desc_iface_mouse,
286 },
287 },
288};
289
58e4fee2
JV
290static const USBDescDevice desc_device_mouse2 = {
291 .bcdUSB = 0x0200,
292 .bMaxPacketSize0 = 64,
293 .bNumConfigurations = 1,
294 .confs = (USBDescConfig[]) {
295 {
296 .bNumInterfaces = 1,
297 .bConfigurationValue = 1,
298 .iConfiguration = STR_CONFIG_MOUSE,
299 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
300 .bMaxPower = 50,
301 .nif = 1,
302 .ifs = &desc_iface_mouse2,
303 },
304 },
305};
306
0e4e9695
GH
307static const USBDescDevice desc_device_tablet = {
308 .bcdUSB = 0x0100,
309 .bMaxPacketSize0 = 8,
310 .bNumConfigurations = 1,
311 .confs = (USBDescConfig[]) {
312 {
313 .bNumInterfaces = 1,
314 .bConfigurationValue = 1,
315 .iConfiguration = STR_CONFIG_TABLET,
bd93976a 316 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
0e4e9695 317 .bMaxPower = 50,
add75088 318 .nif = 1,
0e4e9695
GH
319 .ifs = &desc_iface_tablet,
320 },
321 },
322};
323
427e3aa1
HG
324static const USBDescDevice desc_device_tablet2 = {
325 .bcdUSB = 0x0200,
326 .bMaxPacketSize0 = 64,
327 .bNumConfigurations = 1,
328 .confs = (USBDescConfig[]) {
329 {
330 .bNumInterfaces = 1,
331 .bConfigurationValue = 1,
332 .iConfiguration = STR_CONFIG_TABLET,
bd93976a 333 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
427e3aa1
HG
334 .bMaxPower = 50,
335 .nif = 1,
336 .ifs = &desc_iface_tablet2,
337 },
338 },
339};
340
0e4e9695
GH
341static const USBDescDevice desc_device_keyboard = {
342 .bcdUSB = 0x0100,
343 .bMaxPacketSize0 = 8,
344 .bNumConfigurations = 1,
345 .confs = (USBDescConfig[]) {
346 {
347 .bNumInterfaces = 1,
348 .bConfigurationValue = 1,
349 .iConfiguration = STR_CONFIG_KEYBOARD,
bd93976a 350 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
0e4e9695 351 .bMaxPower = 50,
add75088 352 .nif = 1,
0e4e9695
GH
353 .ifs = &desc_iface_keyboard,
354 },
355 },
356};
357
b13ce076
JV
358static const USBDescDevice desc_device_keyboard2 = {
359 .bcdUSB = 0x0200,
360 .bMaxPacketSize0 = 64,
361 .bNumConfigurations = 1,
362 .confs = (USBDescConfig[]) {
363 {
364 .bNumInterfaces = 1,
365 .bConfigurationValue = 1,
366 .iConfiguration = STR_CONFIG_KEYBOARD,
367 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
368 .bMaxPower = 50,
369 .nif = 1,
370 .ifs = &desc_iface_keyboard2,
371 },
372 },
373};
374
88678fbd
GH
375static const USBDescMSOS desc_msos_suspend = {
376 .SelectiveSuspendEnabled = true,
377};
378
0e4e9695
GH
379static const USBDesc desc_mouse = {
380 .id = {
381 .idVendor = 0x0627,
382 .idProduct = 0x0001,
383 .bcdDevice = 0,
384 .iManufacturer = STR_MANUFACTURER,
385 .iProduct = STR_PRODUCT_MOUSE,
b63e1050 386 .iSerialNumber = STR_SERIAL_MOUSE,
0e4e9695
GH
387 },
388 .full = &desc_device_mouse,
389 .str = desc_strings,
88678fbd 390 .msos = &desc_msos_suspend,
0e4e9695
GH
391};
392
58e4fee2
JV
393static const USBDesc desc_mouse2 = {
394 .id = {
395 .idVendor = 0x0627,
396 .idProduct = 0x0001,
397 .bcdDevice = 0,
398 .iManufacturer = STR_MANUFACTURER,
399 .iProduct = STR_PRODUCT_MOUSE,
b63e1050 400 .iSerialNumber = STR_SERIAL_MOUSE,
58e4fee2
JV
401 },
402 .full = &desc_device_mouse,
403 .high = &desc_device_mouse2,
404 .str = desc_strings,
405 .msos = &desc_msos_suspend,
406};
407
0e4e9695
GH
408static const USBDesc desc_tablet = {
409 .id = {
410 .idVendor = 0x0627,
411 .idProduct = 0x0001,
412 .bcdDevice = 0,
413 .iManufacturer = STR_MANUFACTURER,
414 .iProduct = STR_PRODUCT_TABLET,
b63e1050 415 .iSerialNumber = STR_SERIAL_TABLET,
0e4e9695
GH
416 },
417 .full = &desc_device_tablet,
418 .str = desc_strings,
88678fbd 419 .msos = &desc_msos_suspend,
0e4e9695
GH
420};
421
427e3aa1
HG
422static const USBDesc desc_tablet2 = {
423 .id = {
424 .idVendor = 0x0627,
425 .idProduct = 0x0001,
426 .bcdDevice = 0,
427 .iManufacturer = STR_MANUFACTURER,
428 .iProduct = STR_PRODUCT_TABLET,
b63e1050 429 .iSerialNumber = STR_SERIAL_TABLET,
427e3aa1
HG
430 },
431 .full = &desc_device_tablet,
432 .high = &desc_device_tablet2,
433 .str = desc_strings,
88678fbd 434 .msos = &desc_msos_suspend,
427e3aa1
HG
435};
436
0e4e9695
GH
437static const USBDesc desc_keyboard = {
438 .id = {
439 .idVendor = 0x0627,
440 .idProduct = 0x0001,
441 .bcdDevice = 0,
442 .iManufacturer = STR_MANUFACTURER,
443 .iProduct = STR_PRODUCT_KEYBOARD,
b63e1050 444 .iSerialNumber = STR_SERIAL_KEYBOARD,
0e4e9695
GH
445 },
446 .full = &desc_device_keyboard,
447 .str = desc_strings,
88678fbd 448 .msos = &desc_msos_suspend,
47b2d338
AZ
449};
450
b13ce076
JV
451static const USBDesc desc_keyboard2 = {
452 .id = {
453 .idVendor = 0x0627,
454 .idProduct = 0x0001,
455 .bcdDevice = 0,
456 .iManufacturer = STR_MANUFACTURER,
457 .iProduct = STR_PRODUCT_KEYBOARD,
b63e1050 458 .iSerialNumber = STR_SERIAL_KEYBOARD,
b13ce076
JV
459 },
460 .full = &desc_device_keyboard,
461 .high = &desc_device_keyboard2,
462 .str = desc_strings,
463 .msos = &desc_msos_suspend,
464};
465
59ae540c 466static const uint8_t qemu_mouse_hid_report_descriptor[] = {
976f8eef
AZ
467 0x05, 0x01, /* Usage Page (Generic Desktop) */
468 0x09, 0x02, /* Usage (Mouse) */
469 0xa1, 0x01, /* Collection (Application) */
470 0x09, 0x01, /* Usage (Pointer) */
471 0xa1, 0x00, /* Collection (Physical) */
472 0x05, 0x09, /* Usage Page (Button) */
473 0x19, 0x01, /* Usage Minimum (1) */
474 0x29, 0x03, /* Usage Maximum (3) */
475 0x15, 0x00, /* Logical Minimum (0) */
476 0x25, 0x01, /* Logical Maximum (1) */
477 0x95, 0x03, /* Report Count (3) */
478 0x75, 0x01, /* Report Size (1) */
479 0x81, 0x02, /* Input (Data, Variable, Absolute) */
480 0x95, 0x01, /* Report Count (1) */
481 0x75, 0x05, /* Report Size (5) */
482 0x81, 0x01, /* Input (Constant) */
483 0x05, 0x01, /* Usage Page (Generic Desktop) */
484 0x09, 0x30, /* Usage (X) */
485 0x09, 0x31, /* Usage (Y) */
486 0x09, 0x38, /* Usage (Wheel) */
487 0x15, 0x81, /* Logical Minimum (-0x7f) */
488 0x25, 0x7f, /* Logical Maximum (0x7f) */
489 0x75, 0x08, /* Report Size (8) */
490 0x95, 0x03, /* Report Count (3) */
491 0x81, 0x06, /* Input (Data, Variable, Relative) */
492 0xc0, /* End Collection */
493 0xc0, /* End Collection */
59ae540c
FB
494};
495
09b26c5e 496static const uint8_t qemu_tablet_hid_report_descriptor[] = {
976f8eef 497 0x05, 0x01, /* Usage Page (Generic Desktop) */
0cd089e9 498 0x09, 0x02, /* Usage (Mouse) */
976f8eef
AZ
499 0xa1, 0x01, /* Collection (Application) */
500 0x09, 0x01, /* Usage (Pointer) */
501 0xa1, 0x00, /* Collection (Physical) */
502 0x05, 0x09, /* Usage Page (Button) */
503 0x19, 0x01, /* Usage Minimum (1) */
504 0x29, 0x03, /* Usage Maximum (3) */
505 0x15, 0x00, /* Logical Minimum (0) */
506 0x25, 0x01, /* Logical Maximum (1) */
507 0x95, 0x03, /* Report Count (3) */
508 0x75, 0x01, /* Report Size (1) */
509 0x81, 0x02, /* Input (Data, Variable, Absolute) */
510 0x95, 0x01, /* Report Count (1) */
511 0x75, 0x05, /* Report Size (5) */
512 0x81, 0x01, /* Input (Constant) */
513 0x05, 0x01, /* Usage Page (Generic Desktop) */
514 0x09, 0x30, /* Usage (X) */
515 0x09, 0x31, /* Usage (Y) */
516 0x15, 0x00, /* Logical Minimum (0) */
de5c2d0a 517 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
976f8eef 518 0x35, 0x00, /* Physical Minimum (0) */
de5c2d0a 519 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
976f8eef
AZ
520 0x75, 0x10, /* Report Size (16) */
521 0x95, 0x02, /* Report Count (2) */
522 0x81, 0x02, /* Input (Data, Variable, Absolute) */
523 0x05, 0x01, /* Usage Page (Generic Desktop) */
524 0x09, 0x38, /* Usage (Wheel) */
525 0x15, 0x81, /* Logical Minimum (-0x7f) */
526 0x25, 0x7f, /* Logical Maximum (0x7f) */
527 0x35, 0x00, /* Physical Minimum (same as logical) */
528 0x45, 0x00, /* Physical Maximum (same as logical) */
529 0x75, 0x08, /* Report Size (8) */
530 0x95, 0x01, /* Report Count (1) */
531 0x81, 0x06, /* Input (Data, Variable, Relative) */
532 0xc0, /* End Collection */
533 0xc0, /* End Collection */
09b26c5e
FB
534};
535
47b2d338
AZ
536static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
537 0x05, 0x01, /* Usage Page (Generic Desktop) */
538 0x09, 0x06, /* Usage (Keyboard) */
539 0xa1, 0x01, /* Collection (Application) */
540 0x75, 0x01, /* Report Size (1) */
541 0x95, 0x08, /* Report Count (8) */
542 0x05, 0x07, /* Usage Page (Key Codes) */
543 0x19, 0xe0, /* Usage Minimum (224) */
544 0x29, 0xe7, /* Usage Maximum (231) */
545 0x15, 0x00, /* Logical Minimum (0) */
546 0x25, 0x01, /* Logical Maximum (1) */
547 0x81, 0x02, /* Input (Data, Variable, Absolute) */
548 0x95, 0x01, /* Report Count (1) */
549 0x75, 0x08, /* Report Size (8) */
550 0x81, 0x01, /* Input (Constant) */
551 0x95, 0x05, /* Report Count (5) */
552 0x75, 0x01, /* Report Size (1) */
553 0x05, 0x08, /* Usage Page (LEDs) */
554 0x19, 0x01, /* Usage Minimum (1) */
555 0x29, 0x05, /* Usage Maximum (5) */
556 0x91, 0x02, /* Output (Data, Variable, Absolute) */
557 0x95, 0x01, /* Report Count (1) */
558 0x75, 0x03, /* Report Size (3) */
559 0x91, 0x01, /* Output (Constant) */
560 0x95, 0x06, /* Report Count (6) */
561 0x75, 0x08, /* Report Size (8) */
562 0x15, 0x00, /* Logical Minimum (0) */
563 0x25, 0xff, /* Logical Maximum (255) */
564 0x05, 0x07, /* Usage Page (Key Codes) */
565 0x19, 0x00, /* Usage Minimum (0) */
566 0x29, 0xff, /* Usage Maximum (255) */
567 0x81, 0x00, /* Input (Data, Array) */
568 0xc0, /* End Collection */
569};
570
8bde6805 571static void usb_hid_changed(HIDState *hs)
47e699dc 572{
8bde6805 573 USBHIDState *us = container_of(hs, USBHIDState, hid);
47e699dc 574
8550a02d 575 usb_wakeup(us->intr, 0);
47e699dc
AZ
576}
577
8bde6805 578static void usb_hid_handle_reset(USBDevice *dev)
47b2d338 579{
f5669129 580 USBHIDState *us = USB_HID(dev);
0d878eec 581
dcfda673 582 hid_reset(&us->hid);
68735b6c
KC
583}
584
9a77a0f5 585static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
007fd62f 586 int request, int value, int index, int length, uint8_t *data)
59ae540c 587{
f5669129 588 USBHIDState *us = USB_HID(dev);
0d878eec 589 HIDState *hs = &us->hid;
0e4e9695 590 int ret;
59ae540c 591
007fd62f 592 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
0e4e9695 593 if (ret >= 0) {
9a77a0f5 594 return;
0e4e9695 595 }
59ae540c 596
0d878eec 597 switch (request) {
59ae540c
FB
598 /* hid specific requests */
599 case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
0d878eec 600 switch (value >> 8) {
59ae540c 601 case 0x22:
0d878eec 602 if (hs->kind == HID_MOUSE) {
7d37435b
PB
603 memcpy(data, qemu_mouse_hid_report_descriptor,
604 sizeof(qemu_mouse_hid_report_descriptor));
9a77a0f5 605 p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
0d878eec
GH
606 } else if (hs->kind == HID_TABLET) {
607 memcpy(data, qemu_tablet_hid_report_descriptor,
7d37435b 608 sizeof(qemu_tablet_hid_report_descriptor));
9a77a0f5 609 p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
0d878eec 610 } else if (hs->kind == HID_KEYBOARD) {
5fafdf24 611 memcpy(data, qemu_keyboard_hid_report_descriptor,
47b2d338 612 sizeof(qemu_keyboard_hid_report_descriptor));
9a77a0f5 613 p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
47b2d338
AZ
614 }
615 break;
59ae540c
FB
616 default:
617 goto fail;
618 }
619 break;
620 case GET_REPORT:
0d878eec 621 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
9a77a0f5 622 p->actual_length = hid_pointer_poll(hs, data, length);
0d878eec 623 } else if (hs->kind == HID_KEYBOARD) {
9a77a0f5 624 p->actual_length = hid_keyboard_poll(hs, data, length);
e7e73892 625 }
47b2d338
AZ
626 break;
627 case SET_REPORT:
0d878eec 628 if (hs->kind == HID_KEYBOARD) {
9a77a0f5 629 p->actual_length = hid_keyboard_write(hs, data, length);
0d878eec 630 } else {
47b2d338 631 goto fail;
0d878eec 632 }
47b2d338
AZ
633 break;
634 case GET_PROTOCOL:
0d878eec 635 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
47b2d338 636 goto fail;
0d878eec 637 }
b069d348 638 data[0] = hs->protocol;
9a77a0f5 639 p->actual_length = 1;
47b2d338
AZ
640 break;
641 case SET_PROTOCOL:
0d878eec 642 if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
47b2d338 643 goto fail;
0d878eec 644 }
b069d348 645 hs->protocol = value;
47b2d338
AZ
646 break;
647 case GET_IDLE:
b069d348 648 data[0] = hs->idle;
9a77a0f5 649 p->actual_length = 1;
59ae540c
FB
650 break;
651 case SET_IDLE:
b069d348 652 hs->idle = (uint8_t) (value >> 8);
027c03f7 653 hid_set_next_idle(hs);
21635e12
GH
654 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
655 hid_pointer_activate(hs);
656 }
59ae540c
FB
657 break;
658 default:
659 fail:
9a77a0f5 660 p->status = USB_RET_STALL;
59ae540c
FB
661 break;
662 }
59ae540c
FB
663}
664
9a77a0f5 665static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
59ae540c 666{
f5669129 667 USBHIDState *us = USB_HID(dev);
0d878eec 668 HIDState *hs = &us->hid;
4f4321c1 669 uint8_t buf[p->iov.size];
9a77a0f5 670 int len = 0;
59ae540c 671
0d878eec 672 switch (p->pid) {
59ae540c 673 case USB_TOKEN_IN:
079d0b7f 674 if (p->ep->nr == 1) {
299aa1c6
GH
675 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
676 hid_pointer_activate(hs);
677 }
027c03f7 678 if (!hid_has_events(hs)) {
9a77a0f5
HG
679 p->status = USB_RET_NAK;
680 return;
13f8b97a 681 }
027c03f7 682 hid_set_next_idle(hs);
0d878eec 683 if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
9a77a0f5 684 len = hid_pointer_poll(hs, buf, p->iov.size);
0d878eec 685 } else if (hs->kind == HID_KEYBOARD) {
9a77a0f5 686 len = hid_keyboard_poll(hs, buf, p->iov.size);
13f8b97a 687 }
9a77a0f5 688 usb_packet_copy(p, buf, len);
59ae540c
FB
689 } else {
690 goto fail;
691 }
692 break;
693 case USB_TOKEN_OUT:
694 default:
695 fail:
9a77a0f5 696 p->status = USB_RET_STALL;
59ae540c
FB
697 break;
698 }
59ae540c
FB
699}
700
c4fe9700 701static void usb_hid_unrealize(USBDevice *dev, Error **errp)
09b26c5e 702{
f5669129 703 USBHIDState *us = USB_HID(dev);
a980a065 704
8bde6805
GH
705 hid_free(&us->hid);
706}
707
ff326ffa
JV
708static void usb_hid_initfn(USBDevice *dev, int kind,
709 const USBDesc *usb1, const USBDesc *usb2,
710 Error **errp)
8bde6805 711{
f5669129 712 USBHIDState *us = USB_HID(dev);
ff326ffa
JV
713 switch (us->usb_version) {
714 case 1:
715 dev->usb_desc = usb1;
716 break;
717 case 2:
718 dev->usb_desc = usb2;
719 break;
720 default:
721 dev->usb_desc = NULL;
722 }
723 if (!dev->usb_desc) {
724 error_setg(errp, "Invalid usb version %d for usb hid device",
725 us->usb_version);
726 return;
727 }
8bde6805 728
b63e1050 729 usb_desc_create_serial(dev);
8bde6805 730 usb_desc_init(dev);
7567b51f 731 us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
8bde6805 732 hid_init(&us->hid, kind, usb_hid_changed);
f85d2831
GH
733 if (us->display && us->hid.s) {
734 qemu_input_handler_bind(us->hid.s, us->display, us->head, NULL);
735 }
09b26c5e
FB
736}
737
276b7ac8 738static void usb_tablet_realize(USBDevice *dev, Error **errp)
59ae540c 739{
427e3aa1 740
ff326ffa 741 usb_hid_initfn(dev, HID_TABLET, &desc_tablet, &desc_tablet2, errp);
806b6024 742}
59ae540c 743
276b7ac8 744static void usb_mouse_realize(USBDevice *dev, Error **errp)
806b6024 745{
58e4fee2 746 usb_hid_initfn(dev, HID_MOUSE, &desc_mouse, &desc_mouse2, errp);
806b6024 747}
59ae540c 748
276b7ac8 749static void usb_keyboard_realize(USBDevice *dev, Error **errp)
806b6024 750{
b13ce076 751 usb_hid_initfn(dev, HID_KEYBOARD, &desc_keyboard, &desc_keyboard2, errp);
806b6024 752}
59ae540c 753
3a3286bf
GH
754static int usb_ptr_post_load(void *opaque, int version_id)
755{
756 USBHIDState *s = opaque;
757
758 if (s->dev.remote_wakeup) {
759 hid_pointer_activate(&s->hid);
760 }
761 return 0;
762}
763
ee59e6b3
GH
764static const VMStateDescription vmstate_usb_ptr = {
765 .name = "usb-ptr",
766 .version_id = 1,
767 .minimum_version_id = 1,
3a3286bf 768 .post_load = usb_ptr_post_load,
6e3d652a 769 .fields = (VMStateField[]) {
ee59e6b3 770 VMSTATE_USB_DEVICE(dev, USBHIDState),
1f42d222 771 VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
ee59e6b3
GH
772 VMSTATE_END_OF_LIST()
773 }
774};
775
776static const VMStateDescription vmstate_usb_kbd = {
777 .name = "usb-kbd",
778 .version_id = 1,
779 .minimum_version_id = 1,
6e3d652a 780 .fields = (VMStateField[]) {
ee59e6b3 781 VMSTATE_USB_DEVICE(dev, USBHIDState),
1f42d222 782 VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
ee59e6b3
GH
783 VMSTATE_END_OF_LIST()
784 }
785};
786
7f595609 787static void usb_hid_class_initfn(ObjectClass *klass, void *data)
62aed765
AL
788{
789 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
790
62aed765
AL
791 uc->handle_reset = usb_hid_handle_reset;
792 uc->handle_control = usb_hid_handle_control;
793 uc->handle_data = usb_hid_handle_data;
c4fe9700 794 uc->unrealize = usb_hid_unrealize;
427e3aa1 795 uc->handle_attach = usb_desc_attach;
62aed765
AL
796}
797
f5669129
GA
798static const TypeInfo usb_hid_type_info = {
799 .name = TYPE_USB_HID,
800 .parent = TYPE_USB_DEVICE,
801 .instance_size = sizeof(USBHIDState),
802 .abstract = true,
803 .class_init = usb_hid_class_initfn,
804};
805
427e3aa1
HG
806static Property usb_tablet_properties[] = {
807 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
f85d2831
GH
808 DEFINE_PROP_STRING("display", USBHIDState, display),
809 DEFINE_PROP_UINT32("head", USBHIDState, head, 0),
427e3aa1
HG
810 DEFINE_PROP_END_OF_LIST(),
811};
812
7f595609
AL
813static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
814{
39bffca2 815 DeviceClass *dc = DEVICE_CLASS(klass);
7f595609
AL
816 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
817
276b7ac8 818 uc->realize = usb_tablet_realize;
7f595609 819 uc->product_desc = "QEMU USB Tablet";
39bffca2 820 dc->vmsd = &vmstate_usb_ptr;
427e3aa1 821 dc->props = usb_tablet_properties;
31efd2e8 822 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
7f595609
AL
823}
824
8c43a6f0 825static const TypeInfo usb_tablet_info = {
39bffca2 826 .name = "usb-tablet",
f5669129 827 .parent = TYPE_USB_HID,
39bffca2 828 .class_init = usb_tablet_class_initfn,
62aed765
AL
829};
830
58e4fee2
JV
831static Property usb_mouse_properties[] = {
832 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
833 DEFINE_PROP_END_OF_LIST(),
834};
835
62aed765
AL
836static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
837{
39bffca2 838 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
839 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
840
276b7ac8 841 uc->realize = usb_mouse_realize;
62aed765 842 uc->product_desc = "QEMU USB Mouse";
39bffca2 843 dc->vmsd = &vmstate_usb_ptr;
58e4fee2 844 dc->props = usb_mouse_properties;
125ee0ed 845 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
62aed765
AL
846}
847
8c43a6f0 848static const TypeInfo usb_mouse_info = {
39bffca2 849 .name = "usb-mouse",
f5669129 850 .parent = TYPE_USB_HID,
39bffca2 851 .class_init = usb_mouse_class_initfn,
62aed765
AL
852};
853
f85d2831 854static Property usb_keyboard_properties[] = {
b13ce076 855 DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
f85d2831
GH
856 DEFINE_PROP_STRING("display", USBHIDState, display),
857 DEFINE_PROP_END_OF_LIST(),
858};
859
62aed765
AL
860static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
861{
39bffca2 862 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
863 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
864
276b7ac8 865 uc->realize = usb_keyboard_realize;
62aed765 866 uc->product_desc = "QEMU USB Keyboard";
39bffca2 867 dc->vmsd = &vmstate_usb_kbd;
f85d2831 868 dc->props = usb_keyboard_properties;
125ee0ed 869 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
62aed765
AL
870}
871
8c43a6f0 872static const TypeInfo usb_keyboard_info = {
39bffca2 873 .name = "usb-kbd",
f5669129 874 .parent = TYPE_USB_HID,
39bffca2 875 .class_init = usb_keyboard_class_initfn,
806b6024
GH
876};
877
83f7d43a 878static void usb_hid_register_types(void)
806b6024 879{
f5669129 880 type_register_static(&usb_hid_type_info);
39bffca2 881 type_register_static(&usb_tablet_info);
ba02430f 882 usb_legacy_register("usb-tablet", "tablet", NULL);
39bffca2 883 type_register_static(&usb_mouse_info);
ba02430f 884 usb_legacy_register("usb-mouse", "mouse", NULL);
39bffca2 885 type_register_static(&usb_keyboard_info);
ba02430f 886 usb_legacy_register("usb-kbd", "keyboard", NULL);
806b6024 887}
83f7d43a
AF
888
889type_init(usb_hid_register_types)
This page took 1.268994 seconds and 4 git commands to generate.