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