VirtualBox

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

Last change on this file since 33000 was 30562, checked in by vboxsync, 14 years ago

Devices/Input/UsbMouse: support five buttons

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

© 2023 Oracle
ContactPrivacy policyTerms of Use