VirtualBox

source: vbox/trunk/src/VBox/Devices/Input/UsbMouse.cpp@ 46757

Last change on this file since 46757 was 46730, checked in by vboxsync, 12 years ago

Devices/Input/UsbMouse: re-factor code to make it easier to put it inside a unit test.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette