VirtualBox

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

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

Tiny refactoring.

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