VirtualBox

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

Last change on this file since 46765 was 46758, checked in by vboxsync, 12 years ago

Devices/Input/UsbMouse: add basic unit test harness.

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

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