VirtualBox

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

Last change on this file since 46326 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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