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