1 | /** @file
|
---|
2 | * UsbMouse - USB Human Interface Device Emulation (Mouse).
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2007-2010 Sun Microsystems, Inc.
|
---|
7 | *
|
---|
8 | * Sun Microsystems, Inc. confidential
|
---|
9 | * All rights reserved
|
---|
10 | */
|
---|
11 |
|
---|
12 | /*******************************************************************************
|
---|
13 | * Header Files *
|
---|
14 | *******************************************************************************/
|
---|
15 | #define LOG_GROUP LOG_GROUP_USB_MSD
|
---|
16 | #include <VBox/pdmusb.h>
|
---|
17 | #include <VBox/log.h>
|
---|
18 | #include <VBox/err.h>
|
---|
19 | #include <iprt/assert.h>
|
---|
20 | #include <iprt/critsect.h>
|
---|
21 | #include <iprt/mem.h>
|
---|
22 | #include <iprt/semaphore.h>
|
---|
23 | #include <iprt/string.h>
|
---|
24 | #include <iprt/uuid.h>
|
---|
25 | #include "../Builtins.h"
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Defined Constants And Macros *
|
---|
30 | *******************************************************************************/
|
---|
31 | /** @name USB HID string IDs
|
---|
32 | * @{ */
|
---|
33 | #define USBHID_STR_ID_MANUFACTURER 1
|
---|
34 | #define USBHID_STR_ID_PRODUCT_M 2
|
---|
35 | #define USBHID_STR_ID_PRODUCT_T 3
|
---|
36 | /** @} */
|
---|
37 |
|
---|
38 | /** @name USB HID specific descriptor types
|
---|
39 | * @{ */
|
---|
40 | #define DT_IF_HID_REPORT 0x22
|
---|
41 | /** @} */
|
---|
42 |
|
---|
43 | /** @name USB HID vendor and product IDs
|
---|
44 | * @{ */
|
---|
45 | #define VBOX_USB_VENDOR 0x80EE
|
---|
46 | #define USBHID_PID_MOUSE 0x0020
|
---|
47 | #define USBHID_PID_TABLET 0x0021
|
---|
48 | /** @} */
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Structures and Typedefs *
|
---|
52 | *******************************************************************************/
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The USB HID request state.
|
---|
56 | */
|
---|
57 | typedef enum USBHIDREQSTATE
|
---|
58 | {
|
---|
59 | /** Invalid status. */
|
---|
60 | USBHIDREQSTATE_INVALID = 0,
|
---|
61 | /** Ready to receive a new read request. */
|
---|
62 | USBHIDREQSTATE_READY,
|
---|
63 | /** Have (more) data for the host. */
|
---|
64 | USBHIDREQSTATE_DATA_TO_HOST,
|
---|
65 | /** Waiting to supply status information to the host. */
|
---|
66 | USBHIDREQSTATE_STATUS,
|
---|
67 | /** The end of the valid states. */
|
---|
68 | USBHIDREQSTATE_END
|
---|
69 | } USBHIDREQSTATE;
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Endpoint status data.
|
---|
74 | */
|
---|
75 | typedef struct USBHIDEP
|
---|
76 | {
|
---|
77 | bool fHalted;
|
---|
78 | } USBHIDEP;
|
---|
79 | /** Pointer to the endpoint status. */
|
---|
80 | typedef USBHIDEP *PUSBHIDEP;
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * A URB queue.
|
---|
85 | */
|
---|
86 | typedef struct USBHIDURBQUEUE
|
---|
87 | {
|
---|
88 | /** The head pointer. */
|
---|
89 | PVUSBURB pHead;
|
---|
90 | /** Where to insert the next entry. */
|
---|
91 | PVUSBURB *ppTail;
|
---|
92 | } USBHIDURBQUEUE;
|
---|
93 | /** Pointer to a URB queue. */
|
---|
94 | typedef USBHIDURBQUEUE *PUSBHIDURBQUEUE;
|
---|
95 | /** Pointer to a const URB queue. */
|
---|
96 | typedef USBHIDURBQUEUE const *PCUSBHIDURBQUEUE;
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Mouse movement accumulator.
|
---|
101 | */
|
---|
102 | typedef struct USBHIDM_ACCUM
|
---|
103 | {
|
---|
104 | uint32_t btn;
|
---|
105 | int32_t dX;
|
---|
106 | int32_t dY;
|
---|
107 | int32_t dZ;
|
---|
108 | } USBHIDM_ACCUM, *PUSBHIDM_ACCUM;
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * The USB HID instance data.
|
---|
113 | */
|
---|
114 | typedef struct USBHID
|
---|
115 | {
|
---|
116 | /** Pointer back to the PDM USB Device instance structure. */
|
---|
117 | PPDMUSBINS pUsbIns;
|
---|
118 | /** Critical section protecting the device state. */
|
---|
119 | RTCRITSECT CritSect;
|
---|
120 |
|
---|
121 | /** The current configuration.
|
---|
122 | * (0 - default, 1 - the one supported configuration, i.e configured.) */
|
---|
123 | uint8_t bConfigurationValue;
|
---|
124 | /** Endpoint 0 is the default control pipe, 1 is the dev->host interrupt one. */
|
---|
125 | USBHIDEP aEps[2];
|
---|
126 | /** The state of the HID (state machine).*/
|
---|
127 | USBHIDREQSTATE enmState;
|
---|
128 |
|
---|
129 | /** Pointer movement accumulator. */
|
---|
130 | USBHIDM_ACCUM PtrDelta;
|
---|
131 |
|
---|
132 | /** Pending to-host queue.
|
---|
133 | * The URBs waiting here are waiting for data to become available.
|
---|
134 | */
|
---|
135 | USBHIDURBQUEUE ToHostQueue;
|
---|
136 |
|
---|
137 | /** Done queue
|
---|
138 | * The URBs stashed here are waiting to be reaped. */
|
---|
139 | USBHIDURBQUEUE DoneQueue;
|
---|
140 | /** Signalled when adding an URB to the done queue and fHaveDoneQueueWaiter
|
---|
141 | * is set. */
|
---|
142 | RTSEMEVENT hEvtDoneQueue;
|
---|
143 | /** Someone is waiting on the done queue. */
|
---|
144 | bool fHaveDoneQueueWaiter;
|
---|
145 |
|
---|
146 | /** Is this an absolute pointing device (tablet)? Relative (mouse) otherwise. */
|
---|
147 | bool isAbsolute;
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Mouse port - LUN#0.
|
---|
151 | *
|
---|
152 | * @implements PDMIBASE
|
---|
153 | * @implements PDMIMOUSEPORT
|
---|
154 | */
|
---|
155 | struct
|
---|
156 | {
|
---|
157 | /** The base interface for the mouse port. */
|
---|
158 | PDMIBASE IBase;
|
---|
159 | /** The mouse port base interface. */
|
---|
160 | PDMIMOUSEPORT IPort;
|
---|
161 |
|
---|
162 | /** The base interface of the attached mouse driver. */
|
---|
163 | R3PTRTYPE(PPDMIBASE) pDrvBase;
|
---|
164 | /** The mouse interface of the attached mouse driver. */
|
---|
165 | R3PTRTYPE(PPDMIMOUSECONNECTOR) pDrv;
|
---|
166 | } Lun0;
|
---|
167 |
|
---|
168 | } USBHID;
|
---|
169 | /** Pointer to the USB HID instance data. */
|
---|
170 | typedef USBHID *PUSBHID;
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * The USB HID report structure for relative device.
|
---|
174 | */
|
---|
175 | typedef struct USBHIDM_REPORT
|
---|
176 | {
|
---|
177 | uint8_t btn;
|
---|
178 | int8_t dx;
|
---|
179 | int8_t dy;
|
---|
180 | int8_t dz;
|
---|
181 | } USBHIDM_REPORT, *PUSBHIDM_REPORT;
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * The USB HID report structure for relative device.
|
---|
185 | */
|
---|
186 |
|
---|
187 | typedef struct USBHIDT_REPORT
|
---|
188 | {
|
---|
189 | uint16_t cx;
|
---|
190 | uint16_t cy;
|
---|
191 | int8_t dz;
|
---|
192 | uint8_t btn;
|
---|
193 | } USBHIDT_REPORT, *PUSBHIDT_REPORT;
|
---|
194 |
|
---|
195 | /*******************************************************************************
|
---|
196 | * Global Variables *
|
---|
197 | *******************************************************************************/
|
---|
198 | static const PDMUSBDESCCACHESTRING g_aUsbHidStrings_en_US[] =
|
---|
199 | {
|
---|
200 | { USBHID_STR_ID_MANUFACTURER, "VirtualBox" },
|
---|
201 | { USBHID_STR_ID_PRODUCT_M, "USB Mouse" },
|
---|
202 | { USBHID_STR_ID_PRODUCT_T, "USB Tablet" },
|
---|
203 | };
|
---|
204 |
|
---|
205 | static const PDMUSBDESCCACHELANG g_aUsbHidLanguages[] =
|
---|
206 | {
|
---|
207 | { 0x0409, RT_ELEMENTS(g_aUsbHidStrings_en_US), g_aUsbHidStrings_en_US }
|
---|
208 | };
|
---|
209 |
|
---|
210 | static const VUSBDESCENDPOINTEX g_aUsbHidMEndpointDescs[] =
|
---|
211 | {
|
---|
212 | {
|
---|
213 | {
|
---|
214 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
215 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
216 | /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
|
---|
217 | /* .bmAttributes = */ 3 /* interrupt */,
|
---|
218 | /* .wMaxPacketSize = */ 4,
|
---|
219 | /* .bInterval = */ 10,
|
---|
220 | },
|
---|
221 | /* .pvMore = */ NULL,
|
---|
222 | /* .pvClass = */ NULL,
|
---|
223 | /* .cbClass = */ 0
|
---|
224 | },
|
---|
225 | };
|
---|
226 |
|
---|
227 | static const VUSBDESCENDPOINTEX g_aUsbHidTEndpointDescs[] =
|
---|
228 | {
|
---|
229 | {
|
---|
230 | {
|
---|
231 | /* .bLength = */ sizeof(VUSBDESCENDPOINT),
|
---|
232 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
233 | /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
|
---|
234 | /* .bmAttributes = */ 3 /* interrupt */,
|
---|
235 | /* .wMaxPacketSize = */ 6,
|
---|
236 | /* .bInterval = */ 10,
|
---|
237 | },
|
---|
238 | /* .pvMore = */ NULL,
|
---|
239 | /* .pvClass = */ NULL,
|
---|
240 | /* .cbClass = */ 0
|
---|
241 | },
|
---|
242 | };
|
---|
243 |
|
---|
244 | /* HID report descriptor (mouse). */
|
---|
245 | static const uint8_t g_UsbHidMReportDesc[] =
|
---|
246 | {
|
---|
247 | /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
|
---|
248 | /* Usage */ 0x09, 0x02, /* Mouse */
|
---|
249 | /* Collection */ 0xA1, 0x01, /* Application */
|
---|
250 | /* Usage */ 0x09, 0x01, /* Pointer */
|
---|
251 | /* Collection */ 0xA1, 0x00, /* Physical */
|
---|
252 | /* Usage Page */ 0x05, 0x09, /* Button */
|
---|
253 | /* Usage Minimum */ 0x19, 0x01, /* Button 1 */
|
---|
254 | /* Usage Maximum */ 0x29, 0x03, /* Button 3 */
|
---|
255 | /* Logical Minimum */ 0x15, 0x00, /* 0 */
|
---|
256 | /* Logical Maximum */ 0x25, 0x01, /* 1 */
|
---|
257 | /* Report Count */ 0x95, 0x03, /* 3 */
|
---|
258 | /* Report Size */ 0x75, 0x01, /* 1 */
|
---|
259 | /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
|
---|
260 | /* Report Count */ 0x95, 0x01, /* 1 */
|
---|
261 | /* Report Size */ 0x75, 0x05, /* 5 (padding bits) */
|
---|
262 | /* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
|
---|
263 | /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
|
---|
264 | /* Usage */ 0x09, 0x30, /* X */
|
---|
265 | /* Usage */ 0x09, 0x31, /* Y */
|
---|
266 | /* Usage */ 0x09, 0x38, /* Z (wheel) */
|
---|
267 | /* Logical Minimum */ 0x15, 0x81, /* -127 */
|
---|
268 | /* Logical Maximum */ 0x25, 0x7F, /* +127 */
|
---|
269 | /* Report Size */ 0x75, 0x08, /* 8 */
|
---|
270 | /* Report Count */ 0x95, 0x03, /* 3 */
|
---|
271 | /* Input */ 0x81, 0x06, /* Data, Value, Relative, Bit field */
|
---|
272 | /* End Collection */ 0xC0,
|
---|
273 | /* End Collection */ 0xC0,
|
---|
274 | };
|
---|
275 |
|
---|
276 | /* HID report descriptor (tablet). */
|
---|
277 | static const uint8_t g_UsbHidTReportDesc[] =
|
---|
278 | {
|
---|
279 | /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
|
---|
280 | /* Usage */ 0x09, 0x02, /* Mouse */
|
---|
281 | /* Collection */ 0xA1, 0x01, /* Application */
|
---|
282 | /* Usage */ 0x09, 0x01, /* Pointer */
|
---|
283 | /* Collection */ 0xA1, 0x00, /* Physical */
|
---|
284 | /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
|
---|
285 | /* Usage */ 0x09, 0x30, /* X */
|
---|
286 | /* Usage */ 0x09, 0x31, /* Y */
|
---|
287 | /* Logical Minimum */ 0x15, 0x00, /* 0 */
|
---|
288 | /* Logical Maximum */ 0x26, 0xFF,0x7F,/* 0x7fff */
|
---|
289 | /* Physical Minimum */ 0x35, 0x00, /* 0 */
|
---|
290 | /* Physical Maximum */ 0x46, 0xFF,0x7F,/* 0x7fff */
|
---|
291 | /* Report Size */ 0x75, 0x10, /* 16 */
|
---|
292 | /* Report Count */ 0x95, 0x02, /* 2 */
|
---|
293 | /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
|
---|
294 | /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
|
---|
295 | /* Usage */ 0x09, 0x38, /* Z (wheel) */
|
---|
296 | /* Logical Minimum */ 0x15, 0x81, /* -127 */
|
---|
297 | /* Logical Maximum */ 0x25, 0x7F, /* +127 */
|
---|
298 | /* Report Size */ 0x75, 0x08, /* 8 */
|
---|
299 | /* Report Count */ 0x95, 0x01, /* 1 */
|
---|
300 | /* Input */ 0x81, 0x06, /* Data, Value, Relative, Bit field */
|
---|
301 | /* Usage Page */ 0x05, 0x09, /* Button */
|
---|
302 | /* Usage Minimum */ 0x19, 0x01, /* Button 1 */
|
---|
303 | /* Usage Maximum */ 0x29, 0x03, /* Button 3 */
|
---|
304 | /* Logical Minimum */ 0x15, 0x00, /* 0 */
|
---|
305 | /* Logical Maximum */ 0x25, 0x01, /* 1 */
|
---|
306 | /* Report Count */ 0x95, 0x03, /* 3 */
|
---|
307 | /* Report Size */ 0x75, 0x01, /* 1 */
|
---|
308 | /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
|
---|
309 | /* Report Count */ 0x95, 0x01, /* 1 */
|
---|
310 | /* Report Size */ 0x75, 0x05, /* 5 (padding bits) */
|
---|
311 | /* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
|
---|
312 | /* End Collection */ 0xC0,
|
---|
313 | /* End Collection */ 0xC0,
|
---|
314 | };
|
---|
315 |
|
---|
316 | /* Additional HID class interface descriptor. */
|
---|
317 | static const uint8_t g_UsbHidMIfHidDesc[] =
|
---|
318 | {
|
---|
319 | /* .bLength = */ 0x09,
|
---|
320 | /* .bDescriptorType = */ 0x21, /* HID */
|
---|
321 | /* .bcdHID = */ 0x10, 0x01, /* 1.1 */
|
---|
322 | /* .bCountryCode = */ 0,
|
---|
323 | /* .bNumDescriptors = */ 1,
|
---|
324 | /* .bDescriptorType = */ 0x22, /* Report */
|
---|
325 | /* .wDescriptorLength = */ sizeof(g_UsbHidMReportDesc), 0x00
|
---|
326 | };
|
---|
327 |
|
---|
328 | /* Additional HID class interface descriptor. */
|
---|
329 | static const uint8_t g_UsbHidTIfHidDesc[] =
|
---|
330 | {
|
---|
331 | /* .bLength = */ 0x09,
|
---|
332 | /* .bDescriptorType = */ 0x21, /* HID */
|
---|
333 | /* .bcdHID = */ 0x10, 0x01, /* 1.1 */
|
---|
334 | /* .bCountryCode = */ 0,
|
---|
335 | /* .bNumDescriptors = */ 1,
|
---|
336 | /* .bDescriptorType = */ 0x22, /* Report */
|
---|
337 | /* .wDescriptorLength = */ sizeof(g_UsbHidTReportDesc), 0x00
|
---|
338 | };
|
---|
339 |
|
---|
340 | static const VUSBDESCINTERFACEEX g_UsbHidMInterfaceDesc =
|
---|
341 | {
|
---|
342 | {
|
---|
343 | /* .bLength = */ sizeof(VUSBDESCINTERFACE),
|
---|
344 | /* .bDescriptorType = */ VUSB_DT_INTERFACE,
|
---|
345 | /* .bInterfaceNumber = */ 0,
|
---|
346 | /* .bAlternateSetting = */ 0,
|
---|
347 | /* .bNumEndpoints = */ 1,
|
---|
348 | /* .bInterfaceClass = */ 3 /* HID */,
|
---|
349 | /* .bInterfaceSubClass = */ 1 /* Boot Interface */,
|
---|
350 | /* .bInterfaceProtocol = */ 2 /* Mouse */,
|
---|
351 | /* .iInterface = */ 0
|
---|
352 | },
|
---|
353 | /* .pvMore = */ NULL,
|
---|
354 | /* .pvClass = */ &g_UsbHidMIfHidDesc,
|
---|
355 | /* .cbClass = */ sizeof(g_UsbHidMIfHidDesc),
|
---|
356 | &g_aUsbHidMEndpointDescs[0]
|
---|
357 | };
|
---|
358 |
|
---|
359 | static const VUSBDESCINTERFACEEX g_UsbHidTInterfaceDesc =
|
---|
360 | {
|
---|
361 | {
|
---|
362 | /* .bLength = */ sizeof(VUSBDESCINTERFACE),
|
---|
363 | /* .bDescriptorType = */ VUSB_DT_INTERFACE,
|
---|
364 | /* .bInterfaceNumber = */ 0,
|
---|
365 | /* .bAlternateSetting = */ 0,
|
---|
366 | /* .bNumEndpoints = */ 1,
|
---|
367 | /* .bInterfaceClass = */ 3 /* HID */,
|
---|
368 | /* .bInterfaceSubClass = */ 0 /* No subclass - no boot interface. */,
|
---|
369 | /* .bInterfaceProtocol = */ 0 /* No protocol - no boot interface. */,
|
---|
370 | /* .iInterface = */ 0
|
---|
371 | },
|
---|
372 | /* .pvMore = */ NULL,
|
---|
373 | /* .pvClass = */ &g_UsbHidTIfHidDesc,
|
---|
374 | /* .cbClass = */ sizeof(g_UsbHidTIfHidDesc),
|
---|
375 | &g_aUsbHidTEndpointDescs[0]
|
---|
376 | };
|
---|
377 |
|
---|
378 | static const VUSBINTERFACE g_aUsbHidMInterfaces[] =
|
---|
379 | {
|
---|
380 | { &g_UsbHidMInterfaceDesc, /* .cSettings = */ 1 },
|
---|
381 | };
|
---|
382 |
|
---|
383 | static const VUSBINTERFACE g_aUsbHidTInterfaces[] =
|
---|
384 | {
|
---|
385 | { &g_UsbHidTInterfaceDesc, /* .cSettings = */ 1 },
|
---|
386 | };
|
---|
387 |
|
---|
388 | static const VUSBDESCCONFIGEX g_UsbHidMConfigDesc =
|
---|
389 | {
|
---|
390 | {
|
---|
391 | /* .bLength = */ sizeof(VUSBDESCCONFIG),
|
---|
392 | /* .bDescriptorType = */ VUSB_DT_CONFIG,
|
---|
393 | /* .wTotalLength = */ 0 /* recalculated on read */,
|
---|
394 | /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidMInterfaces),
|
---|
395 | /* .bConfigurationValue =*/ 1,
|
---|
396 | /* .iConfiguration = */ 0,
|
---|
397 | /* .bmAttributes = */ RT_BIT(7),
|
---|
398 | /* .MaxPower = */ 50 /* 100mA */
|
---|
399 | },
|
---|
400 | NULL,
|
---|
401 | &g_aUsbHidMInterfaces[0]
|
---|
402 | };
|
---|
403 |
|
---|
404 | static const VUSBDESCCONFIGEX g_UsbHidTConfigDesc =
|
---|
405 | {
|
---|
406 | {
|
---|
407 | /* .bLength = */ sizeof(VUSBDESCCONFIG),
|
---|
408 | /* .bDescriptorType = */ VUSB_DT_CONFIG,
|
---|
409 | /* .wTotalLength = */ 0 /* recalculated on read */,
|
---|
410 | /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidTInterfaces),
|
---|
411 | /* .bConfigurationValue =*/ 1,
|
---|
412 | /* .iConfiguration = */ 0,
|
---|
413 | /* .bmAttributes = */ RT_BIT(7),
|
---|
414 | /* .MaxPower = */ 50 /* 100mA */
|
---|
415 | },
|
---|
416 | NULL,
|
---|
417 | &g_aUsbHidTInterfaces[0]
|
---|
418 | };
|
---|
419 |
|
---|
420 | static const VUSBDESCDEVICE g_UsbHidMDeviceDesc =
|
---|
421 | {
|
---|
422 | /* .bLength = */ sizeof(g_UsbHidMDeviceDesc),
|
---|
423 | /* .bDescriptorType = */ VUSB_DT_DEVICE,
|
---|
424 | /* .bcdUsb = */ 0x110, /* 1.1 */
|
---|
425 | /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
|
---|
426 | /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
|
---|
427 | /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
|
---|
428 | /* .bMaxPacketSize0 = */ 8,
|
---|
429 | /* .idVendor = */ VBOX_USB_VENDOR,
|
---|
430 | /* .idProduct = */ USBHID_PID_MOUSE,
|
---|
431 | /* .bcdDevice = */ 0x0100, /* 1.0 */
|
---|
432 | /* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
|
---|
433 | /* .iProduct = */ USBHID_STR_ID_PRODUCT_M,
|
---|
434 | /* .iSerialNumber = */ 0,
|
---|
435 | /* .bNumConfigurations = */ 1
|
---|
436 | };
|
---|
437 |
|
---|
438 | static const VUSBDESCDEVICE g_UsbHidTDeviceDesc =
|
---|
439 | {
|
---|
440 | /* .bLength = */ sizeof(g_UsbHidTDeviceDesc),
|
---|
441 | /* .bDescriptorType = */ VUSB_DT_DEVICE,
|
---|
442 | /* .bcdUsb = */ 0x110, /* 1.1 */
|
---|
443 | /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
|
---|
444 | /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
|
---|
445 | /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
|
---|
446 | /* .bMaxPacketSize0 = */ 8,
|
---|
447 | /* .idVendor = */ VBOX_USB_VENDOR,
|
---|
448 | /* .idProduct = */ USBHID_PID_TABLET,
|
---|
449 | /* .bcdDevice = */ 0x0100, /* 1.0 */
|
---|
450 | /* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
|
---|
451 | /* .iProduct = */ USBHID_STR_ID_PRODUCT_T,
|
---|
452 | /* .iSerialNumber = */ 0,
|
---|
453 | /* .bNumConfigurations = */ 1
|
---|
454 | };
|
---|
455 |
|
---|
456 | static const PDMUSBDESCCACHE g_UsbHidMDescCache =
|
---|
457 | {
|
---|
458 | /* .pDevice = */ &g_UsbHidMDeviceDesc,
|
---|
459 | /* .paConfigs = */ &g_UsbHidMConfigDesc,
|
---|
460 | /* .paLanguages = */ g_aUsbHidLanguages,
|
---|
461 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
|
---|
462 | /* .fUseCachedDescriptors = */ true,
|
---|
463 | /* .fUseCachedStringsDescriptors = */ true
|
---|
464 | };
|
---|
465 |
|
---|
466 | static const PDMUSBDESCCACHE g_UsbHidTDescCache =
|
---|
467 | {
|
---|
468 | /* .pDevice = */ &g_UsbHidTDeviceDesc,
|
---|
469 | /* .paConfigs = */ &g_UsbHidTConfigDesc,
|
---|
470 | /* .paLanguages = */ g_aUsbHidLanguages,
|
---|
471 | /* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
|
---|
472 | /* .fUseCachedDescriptors = */ true,
|
---|
473 | /* .fUseCachedStringsDescriptors = */ true
|
---|
474 | };
|
---|
475 |
|
---|
476 |
|
---|
477 | /*******************************************************************************
|
---|
478 | * Internal Functions *
|
---|
479 | *******************************************************************************/
|
---|
480 |
|
---|
481 | /**
|
---|
482 | * Initializes an URB queue.
|
---|
483 | *
|
---|
484 | * @param pQueue The URB queue.
|
---|
485 | */
|
---|
486 | static void usbHidQueueInit(PUSBHIDURBQUEUE pQueue)
|
---|
487 | {
|
---|
488 | pQueue->pHead = NULL;
|
---|
489 | pQueue->ppTail = &pQueue->pHead;
|
---|
490 | }
|
---|
491 |
|
---|
492 |
|
---|
493 |
|
---|
494 | /**
|
---|
495 | * Inserts an URB at the end of the queue.
|
---|
496 | *
|
---|
497 | * @param pQueue The URB queue.
|
---|
498 | * @param pUrb The URB to insert.
|
---|
499 | */
|
---|
500 | DECLINLINE(void) usbHidQueueAddTail(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
|
---|
501 | {
|
---|
502 | pUrb->Dev.pNext = NULL;
|
---|
503 | *pQueue->ppTail = pUrb;
|
---|
504 | pQueue->ppTail = &pUrb->Dev.pNext;
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Unlinks the head of the queue and returns it.
|
---|
510 | *
|
---|
511 | * @returns The head entry.
|
---|
512 | * @param pQueue The URB queue.
|
---|
513 | */
|
---|
514 | DECLINLINE(PVUSBURB) usbHidQueueRemoveHead(PUSBHIDURBQUEUE pQueue)
|
---|
515 | {
|
---|
516 | PVUSBURB pUrb = pQueue->pHead;
|
---|
517 | if (pUrb)
|
---|
518 | {
|
---|
519 | PVUSBURB pNext = pUrb->Dev.pNext;
|
---|
520 | pQueue->pHead = pNext;
|
---|
521 | if (!pNext)
|
---|
522 | pQueue->ppTail = &pQueue->pHead;
|
---|
523 | else
|
---|
524 | pUrb->Dev.pNext = NULL;
|
---|
525 | }
|
---|
526 | return pUrb;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Removes an URB from anywhere in the queue.
|
---|
532 | *
|
---|
533 | * @returns true if found, false if not.
|
---|
534 | * @param pQueue The URB queue.
|
---|
535 | * @param pUrb The URB to remove.
|
---|
536 | */
|
---|
537 | DECLINLINE(bool) usbHidQueueRemove(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
|
---|
538 | {
|
---|
539 | PVUSBURB pCur = pQueue->pHead;
|
---|
540 | if (pCur == pUrb)
|
---|
541 | pQueue->pHead = pUrb->Dev.pNext;
|
---|
542 | else
|
---|
543 | {
|
---|
544 | while (pCur)
|
---|
545 | {
|
---|
546 | if (pCur->Dev.pNext == pUrb)
|
---|
547 | {
|
---|
548 | pCur->Dev.pNext = pUrb->Dev.pNext;
|
---|
549 | break;
|
---|
550 | }
|
---|
551 | pCur = pCur->Dev.pNext;
|
---|
552 | }
|
---|
553 | if (!pCur)
|
---|
554 | return false;
|
---|
555 | }
|
---|
556 | if (!pUrb->Dev.pNext)
|
---|
557 | pQueue->ppTail = &pQueue->pHead;
|
---|
558 | return true;
|
---|
559 | }
|
---|
560 |
|
---|
561 |
|
---|
562 | /**
|
---|
563 | * Checks if the queue is empty or not.
|
---|
564 | *
|
---|
565 | * @returns true if it is, false if it isn't.
|
---|
566 | * @param pQueue The URB queue.
|
---|
567 | */
|
---|
568 | DECLINLINE(bool) usbHidQueueIsEmpty(PCUSBHIDURBQUEUE pQueue)
|
---|
569 | {
|
---|
570 | return pQueue->pHead == NULL;
|
---|
571 | }
|
---|
572 |
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Links an URB into the done queue.
|
---|
576 | *
|
---|
577 | * @param pThis The HID instance.
|
---|
578 | * @param pUrb The URB.
|
---|
579 | */
|
---|
580 | static void usbHidLinkDone(PUSBHID pThis, PVUSBURB pUrb)
|
---|
581 | {
|
---|
582 | usbHidQueueAddTail(&pThis->DoneQueue, pUrb);
|
---|
583 |
|
---|
584 | if (pThis->fHaveDoneQueueWaiter)
|
---|
585 | {
|
---|
586 | int rc = RTSemEventSignal(pThis->hEvtDoneQueue);
|
---|
587 | AssertRC(rc);
|
---|
588 | }
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Completes the URB with a stalled state, halting the pipe.
|
---|
595 | */
|
---|
596 | static int usbHidCompleteStall(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb, const char *pszWhy)
|
---|
597 | {
|
---|
598 | Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
|
---|
599 |
|
---|
600 | pUrb->enmStatus = VUSBSTATUS_STALL;
|
---|
601 |
|
---|
602 | /** @todo figure out if the stall is global or pipe-specific or both. */
|
---|
603 | if (pEp)
|
---|
604 | pEp->fHalted = true;
|
---|
605 | else
|
---|
606 | {
|
---|
607 | pThis->aEps[1].fHalted = true;
|
---|
608 | pThis->aEps[2].fHalted = true;
|
---|
609 | }
|
---|
610 |
|
---|
611 | usbHidLinkDone(pThis, pUrb);
|
---|
612 | return VINF_SUCCESS;
|
---|
613 | }
|
---|
614 |
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * Completes the URB with a OK state.
|
---|
618 | */
|
---|
619 | static int usbHidCompleteOk(PUSBHID pThis, PVUSBURB pUrb, size_t cbData)
|
---|
620 | {
|
---|
621 | Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
|
---|
622 |
|
---|
623 | pUrb->enmStatus = VUSBSTATUS_OK;
|
---|
624 | pUrb->cbData = cbData;
|
---|
625 |
|
---|
626 | usbHidLinkDone(pThis, pUrb);
|
---|
627 | return VINF_SUCCESS;
|
---|
628 | }
|
---|
629 |
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * Reset worker for usbHidUsbReset, usbHidUsbSetConfiguration and
|
---|
633 | * usbHidUrbHandleDefaultPipe.
|
---|
634 | *
|
---|
635 | * @returns VBox status code.
|
---|
636 | * @param pThis The HID instance.
|
---|
637 | * @param pUrb Set when usbHidUrbHandleDefaultPipe is the
|
---|
638 | * caller.
|
---|
639 | * @param fSetConfig Set when usbHidUsbSetConfiguration is the
|
---|
640 | * caller.
|
---|
641 | */
|
---|
642 | static int usbHidResetWorker(PUSBHID pThis, PVUSBURB pUrb, bool fSetConfig)
|
---|
643 | {
|
---|
644 | /*
|
---|
645 | * Wait for the any command currently executing to complete before
|
---|
646 | * resetting. (We cannot cancel its execution.) How we do this depends
|
---|
647 | * on the reset method.
|
---|
648 | */
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * Reset the device state.
|
---|
652 | */
|
---|
653 | pThis->enmState = USBHIDREQSTATE_READY;
|
---|
654 |
|
---|
655 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
|
---|
656 | pThis->aEps[i].fHalted = false;
|
---|
657 |
|
---|
658 | if (!pUrb && !fSetConfig) /* (only device reset) */
|
---|
659 | pThis->bConfigurationValue = 0; /* default */
|
---|
660 |
|
---|
661 | /*
|
---|
662 | * Ditch all pending URBs.
|
---|
663 | */
|
---|
664 | PVUSBURB pCurUrb;
|
---|
665 | while ((pCurUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
|
---|
666 | {
|
---|
667 | pCurUrb->enmStatus = VUSBSTATUS_CRC;
|
---|
668 | usbHidLinkDone(pThis, pCurUrb);
|
---|
669 | }
|
---|
670 |
|
---|
671 | if (pUrb)
|
---|
672 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
673 | return VINF_SUCCESS;
|
---|
674 | }
|
---|
675 |
|
---|
676 |
|
---|
677 | /**
|
---|
678 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
679 | */
|
---|
680 | static DECLCALLBACK(void *) usbHidMouseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
681 | {
|
---|
682 | PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
|
---|
683 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
|
---|
684 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Lun0.IPort);
|
---|
685 | return NULL;
|
---|
686 | }
|
---|
687 |
|
---|
688 | static int8_t clamp_i8(int32_t val)
|
---|
689 | {
|
---|
690 | if (val > 127) {
|
---|
691 | val = 127;
|
---|
692 | } else if (val < -127) {
|
---|
693 | val = -127;
|
---|
694 | }
|
---|
695 | return val;
|
---|
696 | }
|
---|
697 |
|
---|
698 | /**
|
---|
699 | * Relative mouse event handler.
|
---|
700 | *
|
---|
701 | * @returns VBox status code.
|
---|
702 | * @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
|
---|
703 | * @param i32DeltaX The X delta.
|
---|
704 | * @param i32DeltaY The Y delta.
|
---|
705 | * @param i32DeltaZ The Z delta.
|
---|
706 | * @param i32DeltaW The W delta.
|
---|
707 | * @param fButtonStates The button states.
|
---|
708 | */
|
---|
709 | static DECLCALLBACK(int) usbHidMousePutEvent(PPDMIMOUSEPORT pInterface, int32_t i32DeltaX, int32_t i32DeltaY, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
|
---|
710 | {
|
---|
711 | PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
|
---|
712 | // int rc = PDMCritSectEnter(&pThis->CritSect, VERR_SEM_BUSY);
|
---|
713 | // AssertReleaseRC(rc);
|
---|
714 |
|
---|
715 | /* If we aren't in the expected mode, switch. This should only really need to be done once. */
|
---|
716 | // if (pThis->isAbsolute)
|
---|
717 | // pThis->Lun0.pDrv->pfnAbsModeChange(pThis->Lun0.pDrv, pThis->isAbsolute);
|
---|
718 |
|
---|
719 | /* Accumulate movement - the events from the front end may arrive
|
---|
720 | * at a much higher rate than USB can handle.
|
---|
721 | */
|
---|
722 | pThis->PtrDelta.btn = fButtonStates;
|
---|
723 | pThis->PtrDelta.dX += i32DeltaX;
|
---|
724 | pThis->PtrDelta.dY += i32DeltaY;
|
---|
725 | pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
|
---|
726 |
|
---|
727 | /* Check if there's a URB waiting. If so, send a report.
|
---|
728 | */
|
---|
729 | PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
|
---|
730 | if (pUrb)
|
---|
731 | {
|
---|
732 | size_t cbCopy;
|
---|
733 | USBHIDM_REPORT report;
|
---|
734 |
|
---|
735 | //@todo: fix/extend
|
---|
736 | report.btn = pThis->PtrDelta.btn;
|
---|
737 | report.dx = clamp_i8(pThis->PtrDelta.dX);
|
---|
738 | report.dy = clamp_i8(pThis->PtrDelta.dY);
|
---|
739 | report.dz = clamp_i8(pThis->PtrDelta.dZ);
|
---|
740 |
|
---|
741 | cbCopy = sizeof(report);
|
---|
742 | memcpy(&pUrb->abData[0], &report, cbCopy);
|
---|
743 |
|
---|
744 | /* Clear the accumulated movement. */
|
---|
745 | pThis->PtrDelta.dX = pThis->PtrDelta.dY = pThis->PtrDelta.dZ = 0;
|
---|
746 |
|
---|
747 | /* Complete the URB. */
|
---|
748 | usbHidCompleteOk(pThis, pUrb, cbCopy);
|
---|
749 | // LogRel(("Rel movement, dX=%d, dY=%d, dZ=%d, btn=%02x, report size %d\n", report.dx, report.dy, report.dz, report.btn, cbCopy));
|
---|
750 | }
|
---|
751 |
|
---|
752 | // PDMCritSectLeave(&pThis->CritSect);
|
---|
753 | return VINF_SUCCESS;
|
---|
754 | }
|
---|
755 |
|
---|
756 | /**
|
---|
757 | * Absolute mouse event handler.
|
---|
758 | *
|
---|
759 | * @returns VBox status code.
|
---|
760 | * @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
|
---|
761 | * @param u32X The X coordinate.
|
---|
762 | * @param u32Y The Y coordinate.
|
---|
763 | * @param i32DeltaZ The Z delta.
|
---|
764 | * @param i32DeltaW The W delta.
|
---|
765 | * @param fButtonStates The button states.
|
---|
766 | */
|
---|
767 | static DECLCALLBACK(int) usbHidMousePutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t u32X, uint32_t u32Y, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
|
---|
768 | {
|
---|
769 | PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
|
---|
770 | // int rc = PDMCritSectEnter(&pThis->CritSect, VERR_SEM_BUSY);
|
---|
771 | // AssertReleaseRC(rc);
|
---|
772 |
|
---|
773 | Assert(pThis->isAbsolute);
|
---|
774 |
|
---|
775 | /* Accumulate movement - the events from the front end may arrive
|
---|
776 | * at a much higher rate than USB can handle. Probably not a real issue
|
---|
777 | * when only the Z axis is relative.
|
---|
778 | */
|
---|
779 | pThis->PtrDelta.btn = fButtonStates;
|
---|
780 | pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
|
---|
781 |
|
---|
782 | /* Check if there's a URB waiting. If so, send a report.
|
---|
783 | */
|
---|
784 | PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
|
---|
785 | if (pUrb)
|
---|
786 | {
|
---|
787 | size_t cbCopy;
|
---|
788 | USBHIDT_REPORT report;
|
---|
789 |
|
---|
790 | report.btn = pThis->PtrDelta.btn;
|
---|
791 | report.cx = u32X / 2;
|
---|
792 | report.cy = u32Y / 2;
|
---|
793 | report.dz = clamp_i8(pThis->PtrDelta.dZ);
|
---|
794 |
|
---|
795 | cbCopy = sizeof(report);
|
---|
796 | memcpy(&pUrb->abData[0], &report, cbCopy);
|
---|
797 |
|
---|
798 | /* Clear the accumulated movement. */
|
---|
799 | pThis->PtrDelta.dZ = 0;
|
---|
800 |
|
---|
801 | /* Complete the URB. */
|
---|
802 | usbHidCompleteOk(pThis, pUrb, cbCopy);
|
---|
803 | // LogRel(("Abs movement, X=%d, Y=%d, dZ=%d, btn=%02x, report size %d\n", report.cx, report.cy, report.dz, report.btn, cbCopy));
|
---|
804 | }
|
---|
805 |
|
---|
806 | // PDMCritSectLeave(&pThis->CritSect);
|
---|
807 | return VINF_SUCCESS;
|
---|
808 | }
|
---|
809 |
|
---|
810 | /**
|
---|
811 | * @copydoc PDMUSBREG::pfnUrbReap
|
---|
812 | */
|
---|
813 | static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
|
---|
814 | {
|
---|
815 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
816 | LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
|
---|
817 |
|
---|
818 | RTCritSectEnter(&pThis->CritSect);
|
---|
819 |
|
---|
820 | PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
|
---|
821 | if (!pUrb && cMillies)
|
---|
822 | {
|
---|
823 | /* Wait */
|
---|
824 | pThis->fHaveDoneQueueWaiter = true;
|
---|
825 | RTCritSectLeave(&pThis->CritSect);
|
---|
826 |
|
---|
827 | RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
|
---|
828 |
|
---|
829 | RTCritSectEnter(&pThis->CritSect);
|
---|
830 | pThis->fHaveDoneQueueWaiter = false;
|
---|
831 |
|
---|
832 | pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
|
---|
833 | }
|
---|
834 |
|
---|
835 | RTCritSectLeave(&pThis->CritSect);
|
---|
836 |
|
---|
837 | if (pUrb)
|
---|
838 | Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
839 | return pUrb;
|
---|
840 | }
|
---|
841 |
|
---|
842 |
|
---|
843 | /**
|
---|
844 | * @copydoc PDMUSBREG::pfnUrbCancel
|
---|
845 | */
|
---|
846 | static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
847 | {
|
---|
848 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
849 | LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
|
---|
850 | RTCritSectEnter(&pThis->CritSect);
|
---|
851 |
|
---|
852 | /*
|
---|
853 | * Remove the URB from the to-host queue and move it onto the done queue.
|
---|
854 | */
|
---|
855 | if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
|
---|
856 | usbHidLinkDone(pThis, pUrb);
|
---|
857 |
|
---|
858 | RTCritSectLeave(&pThis->CritSect);
|
---|
859 | return VINF_SUCCESS;
|
---|
860 | }
|
---|
861 |
|
---|
862 |
|
---|
863 | /**
|
---|
864 | * Handles request sent to the inbound (device to host) interrupt pipe. This is
|
---|
865 | * rather different from bulk requests because an interrupt read URB may complete
|
---|
866 | * after arbitrarily long time.
|
---|
867 | */
|
---|
868 | static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
|
---|
869 | {
|
---|
870 | /*
|
---|
871 | * Stall the request if the pipe is halted.
|
---|
872 | */
|
---|
873 | if (RT_UNLIKELY(pEp->fHalted))
|
---|
874 | return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
|
---|
875 |
|
---|
876 | /*
|
---|
877 | * Deal with the URB according to the state.
|
---|
878 | */
|
---|
879 | switch (pThis->enmState)
|
---|
880 | {
|
---|
881 | /*
|
---|
882 | * We've data left to transfer to the host.
|
---|
883 | */
|
---|
884 | case USBHIDREQSTATE_DATA_TO_HOST:
|
---|
885 | {
|
---|
886 | AssertFailed();
|
---|
887 | Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
|
---|
888 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
889 | }
|
---|
890 |
|
---|
891 | /*
|
---|
892 | * Status transfer.
|
---|
893 | */
|
---|
894 | case USBHIDREQSTATE_STATUS:
|
---|
895 | {
|
---|
896 | AssertFailed();
|
---|
897 | Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
|
---|
898 | pThis->enmState = USBHIDREQSTATE_READY;
|
---|
899 | return usbHidCompleteOk(pThis, pUrb, 0);
|
---|
900 | }
|
---|
901 |
|
---|
902 | case USBHIDREQSTATE_READY:
|
---|
903 | usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
|
---|
904 | LogFlow(("usbHidHandleIntrDevToHost: Added %p:%s to the queue\n", pUrb, pUrb->pszDesc));
|
---|
905 | return VINF_SUCCESS;
|
---|
906 |
|
---|
907 | /*
|
---|
908 | * Bad states, stall.
|
---|
909 | */
|
---|
910 | default:
|
---|
911 | Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
|
---|
912 | return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
|
---|
913 | }
|
---|
914 | }
|
---|
915 |
|
---|
916 |
|
---|
917 | /**
|
---|
918 | * Handles request sent to the default control pipe.
|
---|
919 | */
|
---|
920 | static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
|
---|
921 | {
|
---|
922 | PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
|
---|
923 | AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
|
---|
924 |
|
---|
925 | if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
|
---|
926 | {
|
---|
927 | switch (pSetup->bRequest)
|
---|
928 | {
|
---|
929 | case VUSB_REQ_GET_DESCRIPTOR:
|
---|
930 | {
|
---|
931 | switch (pSetup->bmRequestType)
|
---|
932 | {
|
---|
933 | case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
934 | {
|
---|
935 | switch (pSetup->wValue >> 8)
|
---|
936 | {
|
---|
937 | case VUSB_DT_STRING:
|
---|
938 | Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
939 | break;
|
---|
940 | default:
|
---|
941 | Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
942 | break;
|
---|
943 | }
|
---|
944 | break;
|
---|
945 | }
|
---|
946 |
|
---|
947 | case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
|
---|
948 | {
|
---|
949 | switch (pSetup->wValue >> 8)
|
---|
950 | {
|
---|
951 | case DT_IF_HID_REPORT:
|
---|
952 | uint32_t cbCopy;
|
---|
953 | uint32_t cbDesc;
|
---|
954 | const uint8_t *pDesc;
|
---|
955 |
|
---|
956 | if (pThis->isAbsolute)
|
---|
957 | {
|
---|
958 | cbDesc = sizeof(g_UsbHidTReportDesc);
|
---|
959 | pDesc = (const uint8_t *)&g_UsbHidTReportDesc;
|
---|
960 | }
|
---|
961 | else
|
---|
962 | {
|
---|
963 | cbDesc = sizeof(g_UsbHidMReportDesc);
|
---|
964 | pDesc = (const uint8_t *)&g_UsbHidMReportDesc;
|
---|
965 | }
|
---|
966 | /* Returned data is written after the setup message. */
|
---|
967 | cbCopy = pUrb->cbData - sizeof(*pSetup);
|
---|
968 | cbCopy = RT_MIN(cbCopy, cbDesc);
|
---|
969 | Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
|
---|
970 | memcpy(&pUrb->abData[sizeof(*pSetup)], pDesc, cbCopy);
|
---|
971 | return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
|
---|
972 | default:
|
---|
973 | Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
|
---|
974 | break;
|
---|
975 | }
|
---|
976 | break;
|
---|
977 | }
|
---|
978 |
|
---|
979 | default:
|
---|
980 | Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
|
---|
981 | return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
|
---|
982 | }
|
---|
983 | break;
|
---|
984 | }
|
---|
985 |
|
---|
986 | case VUSB_REQ_CLEAR_FEATURE:
|
---|
987 | break;
|
---|
988 | }
|
---|
989 |
|
---|
990 | /** @todo implement this. */
|
---|
991 | Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
992 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
993 |
|
---|
994 | usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
|
---|
995 | }
|
---|
996 | /* 3.1 Bulk-Only Mass Storage Reset */
|
---|
997 | else if ( pSetup->bmRequestType == (VUSB_REQ_CLASS | VUSB_TO_INTERFACE)
|
---|
998 | && pSetup->bRequest == 0xff
|
---|
999 | && !pSetup->wValue
|
---|
1000 | && !pSetup->wLength
|
---|
1001 | && pSetup->wIndex == 0)
|
---|
1002 | {
|
---|
1003 | Log(("usbHidHandleDefaultPipe: Bulk-Only Mass Storage Reset\n"));
|
---|
1004 | return usbHidResetWorker(pThis, pUrb, false /*fSetConfig*/);
|
---|
1005 | }
|
---|
1006 | else
|
---|
1007 | {
|
---|
1008 | Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
|
---|
1009 | pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
1010 | return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | return VINF_SUCCESS;
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 |
|
---|
1017 | /**
|
---|
1018 | * @copydoc PDMUSBREG::pfnQueue
|
---|
1019 | */
|
---|
1020 | static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
|
---|
1021 | {
|
---|
1022 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1023 | LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
|
---|
1024 | RTCritSectEnter(&pThis->CritSect);
|
---|
1025 |
|
---|
1026 | /*
|
---|
1027 | * Parse on a per end-point basis.
|
---|
1028 | */
|
---|
1029 | int rc;
|
---|
1030 | switch (pUrb->EndPt)
|
---|
1031 | {
|
---|
1032 | case 0:
|
---|
1033 | rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
|
---|
1034 | break;
|
---|
1035 |
|
---|
1036 | case 0x81:
|
---|
1037 | AssertFailed();
|
---|
1038 | case 0x01:
|
---|
1039 | rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
|
---|
1040 | break;
|
---|
1041 |
|
---|
1042 | default:
|
---|
1043 | AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
|
---|
1044 | rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
|
---|
1045 | break;
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | RTCritSectLeave(&pThis->CritSect);
|
---|
1049 | return rc;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 |
|
---|
1053 | /**
|
---|
1054 | * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
|
---|
1055 | */
|
---|
1056 | static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
|
---|
1057 | {
|
---|
1058 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1059 | LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
|
---|
1060 |
|
---|
1061 | if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
|
---|
1062 | {
|
---|
1063 | RTCritSectEnter(&pThis->CritSect);
|
---|
1064 | pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
|
---|
1065 | RTCritSectLeave(&pThis->CritSect);
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | return VINF_SUCCESS;
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 |
|
---|
1072 | /**
|
---|
1073 | * @copydoc PDMUSBREG::pfnUsbSetInterface
|
---|
1074 | */
|
---|
1075 | static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
|
---|
1076 | {
|
---|
1077 | LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
|
---|
1078 | Assert(bAlternateSetting == 0);
|
---|
1079 | return VINF_SUCCESS;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 |
|
---|
1083 | /**
|
---|
1084 | * @copydoc PDMUSBREG::pfnUsbSetConfiguration
|
---|
1085 | */
|
---|
1086 | static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
|
---|
1087 | const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
|
---|
1088 | {
|
---|
1089 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1090 | LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
|
---|
1091 | Assert(bConfigurationValue == 1);
|
---|
1092 | RTCritSectEnter(&pThis->CritSect);
|
---|
1093 |
|
---|
1094 | /*
|
---|
1095 | * If the same config is applied more than once, it's a kind of reset.
|
---|
1096 | */
|
---|
1097 | if (pThis->bConfigurationValue == bConfigurationValue)
|
---|
1098 | usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
|
---|
1099 | pThis->bConfigurationValue = bConfigurationValue;
|
---|
1100 |
|
---|
1101 | /*
|
---|
1102 | * Set received event type to absolute or relative.
|
---|
1103 | */
|
---|
1104 | pThis->Lun0.pDrv->pfnAbsModeChange(pThis->Lun0.pDrv, pThis->isAbsolute);
|
---|
1105 |
|
---|
1106 | RTCritSectLeave(&pThis->CritSect);
|
---|
1107 | return VINF_SUCCESS;
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 |
|
---|
1111 | /**
|
---|
1112 | * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
|
---|
1113 | */
|
---|
1114 | static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
|
---|
1115 | {
|
---|
1116 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1117 | LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
|
---|
1118 | if (pThis->isAbsolute) {
|
---|
1119 | return &g_UsbHidTDescCache;
|
---|
1120 | } else {
|
---|
1121 | return &g_UsbHidMDescCache;
|
---|
1122 | }
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 |
|
---|
1126 | /**
|
---|
1127 | * @copydoc PDMUSBREG::pfnUsbReset
|
---|
1128 | */
|
---|
1129 | static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
|
---|
1130 | {
|
---|
1131 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1132 | LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
|
---|
1133 | RTCritSectEnter(&pThis->CritSect);
|
---|
1134 |
|
---|
1135 | int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
|
---|
1136 |
|
---|
1137 | RTCritSectLeave(&pThis->CritSect);
|
---|
1138 | return rc;
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 |
|
---|
1142 | /**
|
---|
1143 | * @copydoc PDMUSBREG::pfnDestruct
|
---|
1144 | */
|
---|
1145 | static void usbHidDestruct(PPDMUSBINS pUsbIns)
|
---|
1146 | {
|
---|
1147 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1148 | LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
|
---|
1149 |
|
---|
1150 | if (RTCritSectIsInitialized(&pThis->CritSect))
|
---|
1151 | {
|
---|
1152 | RTCritSectEnter(&pThis->CritSect);
|
---|
1153 | RTCritSectLeave(&pThis->CritSect);
|
---|
1154 | RTCritSectDelete(&pThis->CritSect);
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
|
---|
1158 | {
|
---|
1159 | RTSemEventDestroy(pThis->hEvtDoneQueue);
|
---|
1160 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
1161 | }
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 |
|
---|
1165 | /**
|
---|
1166 | * @copydoc PDMUSBREG::pfnConstruct
|
---|
1167 | */
|
---|
1168 | static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
|
---|
1169 | {
|
---|
1170 | PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
|
---|
1171 | Log(("usbHidConstruct/#%u:\n", iInstance));
|
---|
1172 |
|
---|
1173 | /*
|
---|
1174 | * Perform the basic structure initialization first so the destructor
|
---|
1175 | * will not misbehave.
|
---|
1176 | */
|
---|
1177 | pThis->pUsbIns = pUsbIns;
|
---|
1178 | pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
|
---|
1179 | usbHidQueueInit(&pThis->ToHostQueue);
|
---|
1180 | usbHidQueueInit(&pThis->DoneQueue);
|
---|
1181 |
|
---|
1182 | int rc = RTCritSectInit(&pThis->CritSect);
|
---|
1183 | AssertRCReturn(rc, rc);
|
---|
1184 |
|
---|
1185 | rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
|
---|
1186 | AssertRCReturn(rc, rc);
|
---|
1187 |
|
---|
1188 | /*
|
---|
1189 | * Validate and read the configuration.
|
---|
1190 | */
|
---|
1191 | rc = CFGMR3ValidateConfig(pCfg, "/", "Absolute", "Config", "UsbHid", iInstance);
|
---|
1192 | if (RT_FAILURE(rc))
|
---|
1193 | return rc;
|
---|
1194 | rc = CFGMR3QueryBoolDef(pCfg, "Absolute", &pThis->isAbsolute, false);
|
---|
1195 | if (RT_FAILURE(rc))
|
---|
1196 | return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to query settings"));
|
---|
1197 |
|
---|
1198 | pThis->Lun0.IBase.pfnQueryInterface = usbHidMouseQueryInterface;
|
---|
1199 | pThis->Lun0.IPort.pfnPutEvent = usbHidMousePutEvent;
|
---|
1200 | pThis->Lun0.IPort.pfnPutEventAbs = usbHidMousePutEventAbs;
|
---|
1201 |
|
---|
1202 | /*
|
---|
1203 | * Attach the mouse driver.
|
---|
1204 | */
|
---|
1205 | rc = pUsbIns->pHlpR3->pfnDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Mouse Port");
|
---|
1206 | if (RT_FAILURE(rc))
|
---|
1207 | return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach mouse driver"));
|
---|
1208 |
|
---|
1209 | pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIMOUSECONNECTOR);
|
---|
1210 | if (!pThis->Lun0.pDrv)
|
---|
1211 | return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query mouse interface"));
|
---|
1212 |
|
---|
1213 | return VINF_SUCCESS;
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 |
|
---|
1217 | /**
|
---|
1218 | * The USB Human Interface Device (HID) Mouse registration record.
|
---|
1219 | */
|
---|
1220 | const PDMUSBREG g_UsbHidMou =
|
---|
1221 | {
|
---|
1222 | /* u32Version */
|
---|
1223 | PDM_USBREG_VERSION,
|
---|
1224 | /* szName */
|
---|
1225 | "HidMouse",
|
---|
1226 | /* pszDescription */
|
---|
1227 | "USB HID Mouse.",
|
---|
1228 | /* fFlags */
|
---|
1229 | 0,
|
---|
1230 | /* cMaxInstances */
|
---|
1231 | ~0,
|
---|
1232 | /* cbInstance */
|
---|
1233 | sizeof(USBHID),
|
---|
1234 | /* pfnConstruct */
|
---|
1235 | usbHidConstruct,
|
---|
1236 | /* pfnDestruct */
|
---|
1237 | usbHidDestruct,
|
---|
1238 | /* pfnVMInitComplete */
|
---|
1239 | NULL,
|
---|
1240 | /* pfnVMPowerOn */
|
---|
1241 | NULL,
|
---|
1242 | /* pfnVMReset */
|
---|
1243 | NULL,
|
---|
1244 | /* pfnVMSuspend */
|
---|
1245 | NULL,
|
---|
1246 | /* pfnVMResume */
|
---|
1247 | NULL,
|
---|
1248 | /* pfnVMPowerOff */
|
---|
1249 | NULL,
|
---|
1250 | /* pfnHotPlugged */
|
---|
1251 | NULL,
|
---|
1252 | /* pfnHotUnplugged */
|
---|
1253 | NULL,
|
---|
1254 | /* pfnDriverAttach */
|
---|
1255 | NULL,
|
---|
1256 | /* pfnDriverDetach */
|
---|
1257 | NULL,
|
---|
1258 | /* pfnQueryInterface */
|
---|
1259 | NULL,
|
---|
1260 | /* pfnUsbReset */
|
---|
1261 | usbHidUsbReset,
|
---|
1262 | /* pfnUsbGetCachedDescriptors */
|
---|
1263 | usbHidUsbGetDescriptorCache,
|
---|
1264 | /* pfnUsbSetConfiguration */
|
---|
1265 | usbHidUsbSetConfiguration,
|
---|
1266 | /* pfnUsbSetInterface */
|
---|
1267 | usbHidUsbSetInterface,
|
---|
1268 | /* pfnUsbClearHaltedEndpoint */
|
---|
1269 | usbHidUsbClearHaltedEndpoint,
|
---|
1270 | /* pfnUrbNew */
|
---|
1271 | NULL/*usbHidUrbNew*/,
|
---|
1272 | /* pfnQueue */
|
---|
1273 | usbHidQueue,
|
---|
1274 | /* pfnUrbCancel */
|
---|
1275 | usbHidUrbCancel,
|
---|
1276 | /* pfnUrbReap */
|
---|
1277 | usbHidUrbReap,
|
---|
1278 | /* u32TheEnd */
|
---|
1279 | PDM_USBREG_VERSION
|
---|
1280 | };
|
---|