VirtualBox

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

Last change on this file since 27491 was 27129, checked in by vboxsync, 15 years ago

scm cleaned up some whitespace.

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