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