VirtualBox

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

Last change on this file since 43876 was 43780, checked in by vboxsync, 12 years ago

Added forgotten USB mouse/tablet log group.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.6 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_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};
383
384static const VUSBDESCINTERFACEEX g_UsbHidTInterfaceDesc =
385{
386 {
387 /* .bLength = */ sizeof(VUSBDESCINTERFACE),
388 /* .bDescriptorType = */ VUSB_DT_INTERFACE,
389 /* .bInterfaceNumber = */ 0,
390 /* .bAlternateSetting = */ 0,
391 /* .bNumEndpoints = */ 1,
392 /* .bInterfaceClass = */ 3 /* HID */,
393 /* .bInterfaceSubClass = */ 0 /* No subclass - no boot interface. */,
394 /* .bInterfaceProtocol = */ 0 /* No protocol - no boot interface. */,
395 /* .iInterface = */ 0
396 },
397 /* .pvMore = */ NULL,
398 /* .pvClass = */ &g_UsbHidTIfHidDesc,
399 /* .cbClass = */ sizeof(g_UsbHidTIfHidDesc),
400 &g_aUsbHidTEndpointDescs[0]
401};
402
403static const VUSBINTERFACE g_aUsbHidMInterfaces[] =
404{
405 { &g_UsbHidMInterfaceDesc, /* .cSettings = */ 1 },
406};
407
408static const VUSBINTERFACE g_aUsbHidTInterfaces[] =
409{
410 { &g_UsbHidTInterfaceDesc, /* .cSettings = */ 1 },
411};
412
413static const VUSBDESCCONFIGEX g_UsbHidMConfigDesc =
414{
415 {
416 /* .bLength = */ sizeof(VUSBDESCCONFIG),
417 /* .bDescriptorType = */ VUSB_DT_CONFIG,
418 /* .wTotalLength = */ 0 /* recalculated on read */,
419 /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidMInterfaces),
420 /* .bConfigurationValue =*/ 1,
421 /* .iConfiguration = */ 0,
422 /* .bmAttributes = */ RT_BIT(7),
423 /* .MaxPower = */ 50 /* 100mA */
424 },
425 NULL, /* pvMore */
426 &g_aUsbHidMInterfaces[0],
427 NULL /* pvOriginal */
428};
429
430static const VUSBDESCCONFIGEX g_UsbHidTConfigDesc =
431{
432 {
433 /* .bLength = */ sizeof(VUSBDESCCONFIG),
434 /* .bDescriptorType = */ VUSB_DT_CONFIG,
435 /* .wTotalLength = */ 0 /* recalculated on read */,
436 /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidTInterfaces),
437 /* .bConfigurationValue =*/ 1,
438 /* .iConfiguration = */ 0,
439 /* .bmAttributes = */ RT_BIT(7),
440 /* .MaxPower = */ 50 /* 100mA */
441 },
442 NULL, /* pvMore */
443 &g_aUsbHidTInterfaces[0],
444 NULL /* pvOriginal */
445};
446
447static const VUSBDESCDEVICE g_UsbHidMDeviceDesc =
448{
449 /* .bLength = */ sizeof(g_UsbHidMDeviceDesc),
450 /* .bDescriptorType = */ VUSB_DT_DEVICE,
451 /* .bcdUsb = */ 0x110, /* 1.1 */
452 /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
453 /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
454 /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
455 /* .bMaxPacketSize0 = */ 8,
456 /* .idVendor = */ VBOX_USB_VENDOR,
457 /* .idProduct = */ USBHID_PID_MOUSE,
458 /* .bcdDevice = */ 0x0100, /* 1.0 */
459 /* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
460 /* .iProduct = */ USBHID_STR_ID_PRODUCT_M,
461 /* .iSerialNumber = */ 0,
462 /* .bNumConfigurations = */ 1
463};
464
465static const VUSBDESCDEVICE g_UsbHidTDeviceDesc =
466{
467 /* .bLength = */ sizeof(g_UsbHidTDeviceDesc),
468 /* .bDescriptorType = */ VUSB_DT_DEVICE,
469 /* .bcdUsb = */ 0x110, /* 1.1 */
470 /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
471 /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
472 /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
473 /* .bMaxPacketSize0 = */ 8,
474 /* .idVendor = */ VBOX_USB_VENDOR,
475 /* .idProduct = */ USBHID_PID_TABLET,
476 /* .bcdDevice = */ 0x0100, /* 1.0 */
477 /* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
478 /* .iProduct = */ USBHID_STR_ID_PRODUCT_T,
479 /* .iSerialNumber = */ 0,
480 /* .bNumConfigurations = */ 1
481};
482
483static const PDMUSBDESCCACHE g_UsbHidMDescCache =
484{
485 /* .pDevice = */ &g_UsbHidMDeviceDesc,
486 /* .paConfigs = */ &g_UsbHidMConfigDesc,
487 /* .paLanguages = */ g_aUsbHidLanguages,
488 /* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
489 /* .fUseCachedDescriptors = */ true,
490 /* .fUseCachedStringsDescriptors = */ true
491};
492
493static const PDMUSBDESCCACHE g_UsbHidTDescCache =
494{
495 /* .pDevice = */ &g_UsbHidTDeviceDesc,
496 /* .paConfigs = */ &g_UsbHidTConfigDesc,
497 /* .paLanguages = */ g_aUsbHidLanguages,
498 /* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
499 /* .fUseCachedDescriptors = */ true,
500 /* .fUseCachedStringsDescriptors = */ true
501};
502
503
504/*******************************************************************************
505* Internal Functions *
506*******************************************************************************/
507
508/**
509 * Initializes an URB queue.
510 *
511 * @param pQueue The URB queue.
512 */
513static void usbHidQueueInit(PUSBHIDURBQUEUE pQueue)
514{
515 pQueue->pHead = NULL;
516 pQueue->ppTail = &pQueue->pHead;
517}
518
519
520
521/**
522 * Inserts an URB at the end of the queue.
523 *
524 * @param pQueue The URB queue.
525 * @param pUrb The URB to insert.
526 */
527DECLINLINE(void) usbHidQueueAddTail(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
528{
529 pUrb->Dev.pNext = NULL;
530 *pQueue->ppTail = pUrb;
531 pQueue->ppTail = &pUrb->Dev.pNext;
532}
533
534
535/**
536 * Unlinks the head of the queue and returns it.
537 *
538 * @returns The head entry.
539 * @param pQueue The URB queue.
540 */
541DECLINLINE(PVUSBURB) usbHidQueueRemoveHead(PUSBHIDURBQUEUE pQueue)
542{
543 PVUSBURB pUrb = pQueue->pHead;
544 if (pUrb)
545 {
546 PVUSBURB pNext = pUrb->Dev.pNext;
547 pQueue->pHead = pNext;
548 if (!pNext)
549 pQueue->ppTail = &pQueue->pHead;
550 else
551 pUrb->Dev.pNext = NULL;
552 }
553 return pUrb;
554}
555
556
557/**
558 * Removes an URB from anywhere in the queue.
559 *
560 * @returns true if found, false if not.
561 * @param pQueue The URB queue.
562 * @param pUrb The URB to remove.
563 */
564DECLINLINE(bool) usbHidQueueRemove(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
565{
566 PVUSBURB pCur = pQueue->pHead;
567 if (pCur == pUrb)
568 pQueue->pHead = pUrb->Dev.pNext;
569 else
570 {
571 while (pCur)
572 {
573 if (pCur->Dev.pNext == pUrb)
574 {
575 pCur->Dev.pNext = pUrb->Dev.pNext;
576 break;
577 }
578 pCur = pCur->Dev.pNext;
579 }
580 if (!pCur)
581 return false;
582 }
583 if (!pUrb->Dev.pNext)
584 pQueue->ppTail = &pQueue->pHead;
585 return true;
586}
587
588
589/**
590 * Checks if the queue is empty or not.
591 *
592 * @returns true if it is, false if it isn't.
593 * @param pQueue The URB queue.
594 */
595DECLINLINE(bool) usbHidQueueIsEmpty(PCUSBHIDURBQUEUE pQueue)
596{
597 return pQueue->pHead == NULL;
598}
599
600
601/**
602 * Links an URB into the done queue.
603 *
604 * @param pThis The HID instance.
605 * @param pUrb The URB.
606 */
607static void usbHidLinkDone(PUSBHID pThis, PVUSBURB pUrb)
608{
609 usbHidQueueAddTail(&pThis->DoneQueue, pUrb);
610
611 if (pThis->fHaveDoneQueueWaiter)
612 {
613 int rc = RTSemEventSignal(pThis->hEvtDoneQueue);
614 AssertRC(rc);
615 }
616}
617
618
619
620/**
621 * Completes the URB with a stalled state, halting the pipe.
622 */
623static int usbHidCompleteStall(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb, const char *pszWhy)
624{
625 Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
626
627 pUrb->enmStatus = VUSBSTATUS_STALL;
628
629 /** @todo figure out if the stall is global or pipe-specific or both. */
630 if (pEp)
631 pEp->fHalted = true;
632 else
633 {
634 pThis->aEps[0].fHalted = true;
635 pThis->aEps[1].fHalted = true;
636 }
637
638 usbHidLinkDone(pThis, pUrb);
639 return VINF_SUCCESS;
640}
641
642
643/**
644 * Completes the URB with a OK state.
645 */
646static int usbHidCompleteOk(PUSBHID pThis, PVUSBURB pUrb, size_t cbData)
647{
648 Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
649
650 pUrb->enmStatus = VUSBSTATUS_OK;
651 pUrb->cbData = (uint32_t)cbData;
652
653 usbHidLinkDone(pThis, pUrb);
654 return VINF_SUCCESS;
655}
656
657
658/**
659 * Reset worker for usbHidUsbReset, usbHidUsbSetConfiguration and
660 * usbHidHandleDefaultPipe.
661 *
662 * @returns VBox status code.
663 * @param pThis The HID instance.
664 * @param pUrb Set when usbHidHandleDefaultPipe is the
665 * caller.
666 * @param fSetConfig Set when usbHidUsbSetConfiguration is the
667 * caller.
668 */
669static int usbHidResetWorker(PUSBHID pThis, PVUSBURB pUrb, bool fSetConfig)
670{
671 /*
672 * Wait for the any command currently executing to complete before
673 * resetting. (We cannot cancel its execution.) How we do this depends
674 * on the reset method.
675 */
676
677 /*
678 * Reset the device state.
679 */
680 pThis->enmState = USBHIDREQSTATE_READY;
681 pThis->fHasPendingChanges = false;
682
683 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
684 pThis->aEps[i].fHalted = false;
685
686 if (!pUrb && !fSetConfig) /* (only device reset) */
687 pThis->bConfigurationValue = 0; /* default */
688
689 /*
690 * Ditch all pending URBs.
691 */
692 PVUSBURB pCurUrb;
693 while ((pCurUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
694 {
695 pCurUrb->enmStatus = VUSBSTATUS_CRC;
696 usbHidLinkDone(pThis, pCurUrb);
697 }
698
699 if (pUrb)
700 return usbHidCompleteOk(pThis, pUrb, 0);
701 return VINF_SUCCESS;
702}
703
704static int8_t clamp_i8(int32_t val)
705{
706 if (val > 127) {
707 val = 127;
708 } else if (val < -127) {
709 val = -127;
710 }
711 return val;
712}
713
714/**
715 * Create a USB HID report report based on the currently accumulated data.
716 */
717static size_t usbHidFillReport(PUSBHIDTM_REPORT pReport, PUSBHIDM_ACCUM pAccumulated, bool isAbsolute)
718{
719 size_t cbCopy;
720
721 if (isAbsolute)
722 {
723 pReport->t.btn = pAccumulated->btn;
724 pReport->t.cx = pAccumulated->dX;
725 pReport->t.cy = pAccumulated->dY;
726 pReport->t.dz = clamp_i8(pAccumulated->dZ);
727
728 cbCopy = sizeof(pReport->t);
729// 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));
730 }
731 else
732 {
733 pReport->m.btn = pAccumulated->btn;
734 pReport->m.dx = clamp_i8(pAccumulated->dX);
735 pReport->m.dy = clamp_i8(pAccumulated->dY);
736 pReport->m.dz = clamp_i8(pAccumulated->dZ);
737
738 cbCopy = sizeof(pReport->m);
739// 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));
740 }
741
742 /* Clear the accumulated movement. */
743 RT_ZERO(*pAccumulated);
744
745 return cbCopy;
746}
747
748/**
749 * Sends a state report to the host if there is a pending URB.
750 */
751static int usbHidSendReport(PUSBHID pThis)
752{
753 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
754
755 if (pUrb)
756 {
757 PUSBHIDTM_REPORT pReport = (PUSBHIDTM_REPORT)&pUrb->abData[0];
758 size_t cbCopy;
759
760 cbCopy = usbHidFillReport(pReport, &pThis->PtrDelta, pThis->isAbsolute);
761 pThis->fHasPendingChanges = false;
762 return usbHidCompleteOk(pThis, pUrb, cbCopy);
763 }
764 else
765 {
766 Log2(("No available URB for USB mouse\n"));
767 pThis->fHasPendingChanges = true;
768 }
769 return VINF_EOF;
770}
771
772/**
773 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
774 */
775static DECLCALLBACK(void *) usbHidMouseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
776{
777 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
778 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
779 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Lun0.IPort);
780 return NULL;
781}
782
783/**
784 * Relative mouse event handler.
785 *
786 * @returns VBox status code.
787 * @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
788 * @param i32DeltaX The X delta.
789 * @param i32DeltaY The Y delta.
790 * @param i32DeltaZ The Z delta.
791 * @param i32DeltaW The W delta.
792 * @param fButtonStates The button states.
793 */
794static DECLCALLBACK(int) usbHidMousePutEvent(PPDMIMOUSEPORT pInterface, int32_t i32DeltaX, int32_t i32DeltaY, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
795{
796 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
797 RTCritSectEnter(&pThis->CritSect);
798
799 /* Accumulate movement - the events from the front end may arrive
800 * at a much higher rate than USB can handle.
801 */
802 pThis->PtrDelta.btn = fButtonStates;
803 pThis->PtrDelta.dX += i32DeltaX;
804 pThis->PtrDelta.dY += i32DeltaY;
805 pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
806
807 /* Send a report if possible. */
808 usbHidSendReport(pThis);
809
810 RTCritSectLeave(&pThis->CritSect);
811 return VINF_SUCCESS;
812}
813
814/**
815 * Absolute mouse event handler.
816 *
817 * @returns VBox status code.
818 * @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
819 * @param u32X The X coordinate.
820 * @param u32Y The Y coordinate.
821 * @param i32DeltaZ The Z delta.
822 * @param i32DeltaW The W delta.
823 * @param fButtonStates The button states.
824 */
825static DECLCALLBACK(int) usbHidMousePutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t u32X, uint32_t u32Y, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
826{
827 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
828 RTCritSectEnter(&pThis->CritSect);
829
830 Assert(pThis->isAbsolute);
831
832 /* Accumulate movement - the events from the front end may arrive
833 * at a much higher rate than USB can handle. Probably not a real issue
834 * when only the Z axis is relative (X/Y movement isn't technically
835 * accumulated and only the last value is used).
836 */
837 pThis->PtrDelta.btn = fButtonStates;
838 pThis->PtrDelta.dX = u32X >> pThis->u8CoordShift;
839 pThis->PtrDelta.dY = u32Y >> pThis->u8CoordShift;
840 pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
841
842 /* Send a report if possible. */
843 usbHidSendReport(pThis);
844
845 RTCritSectLeave(&pThis->CritSect);
846 return VINF_SUCCESS;
847}
848
849/**
850 * @copydoc PDMUSBREG::pfnUrbReap
851 */
852static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
853{
854 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
855 LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
856
857 RTCritSectEnter(&pThis->CritSect);
858
859 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
860 if (!pUrb && cMillies)
861 {
862 /* Wait */
863 pThis->fHaveDoneQueueWaiter = true;
864 RTCritSectLeave(&pThis->CritSect);
865
866 RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
867
868 RTCritSectEnter(&pThis->CritSect);
869 pThis->fHaveDoneQueueWaiter = false;
870
871 pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
872 }
873
874 RTCritSectLeave(&pThis->CritSect);
875
876 if (pUrb)
877 Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
878 return pUrb;
879}
880
881
882/**
883 * @copydoc PDMUSBREG::pfnUrbCancel
884 */
885static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
886{
887 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
888 LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
889 RTCritSectEnter(&pThis->CritSect);
890
891 /*
892 * Remove the URB from the to-host queue and move it onto the done queue.
893 */
894 if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
895 usbHidLinkDone(pThis, pUrb);
896
897 RTCritSectLeave(&pThis->CritSect);
898 return VINF_SUCCESS;
899}
900
901
902/**
903 * Handles request sent to the inbound (device to host) interrupt pipe. This is
904 * rather different from bulk requests because an interrupt read URB may complete
905 * after arbitrarily long time.
906 */
907static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
908{
909 /*
910 * Stall the request if the pipe is halted.
911 */
912 if (RT_UNLIKELY(pEp->fHalted))
913 return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
914
915 /*
916 * Deal with the URB according to the state.
917 */
918 switch (pThis->enmState)
919 {
920 /*
921 * We've data left to transfer to the host.
922 */
923 case USBHIDREQSTATE_DATA_TO_HOST:
924 {
925 AssertFailed();
926 Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
927 return usbHidCompleteOk(pThis, pUrb, 0);
928 }
929
930 /*
931 * Status transfer.
932 */
933 case USBHIDREQSTATE_STATUS:
934 {
935 AssertFailed();
936 Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
937 pThis->enmState = USBHIDREQSTATE_READY;
938 return usbHidCompleteOk(pThis, pUrb, 0);
939 }
940
941 case USBHIDREQSTATE_READY:
942 usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
943 /* If a report is pending, send it right away. */
944 if (pThis->fHasPendingChanges)
945 usbHidSendReport(pThis);
946 LogFlow(("usbHidHandleIntrDevToHost: Added %p:%s to the queue\n", pUrb, pUrb->pszDesc));
947 return VINF_SUCCESS;
948
949 /*
950 * Bad states, stall.
951 */
952 default:
953 Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
954 return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
955 }
956}
957
958
959/**
960 * Handles request sent to the default control pipe.
961 */
962static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
963{
964 PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
965 AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
966
967 if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
968 {
969 switch (pSetup->bRequest)
970 {
971 case VUSB_REQ_GET_DESCRIPTOR:
972 {
973 switch (pSetup->bmRequestType)
974 {
975 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
976 {
977 switch (pSetup->wValue >> 8)
978 {
979 case VUSB_DT_STRING:
980 Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
981 break;
982 default:
983 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
984 break;
985 }
986 break;
987 }
988
989 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
990 {
991 switch (pSetup->wValue >> 8)
992 {
993 uint32_t cbCopy;
994 uint32_t cbDesc;
995 const uint8_t *pDesc;
996
997 case DT_IF_HID_DESCRIPTOR:
998 {
999 if (pThis->isAbsolute)
1000 {
1001 cbDesc = sizeof(g_UsbHidTIfHidDesc);
1002 pDesc = (const uint8_t *)&g_UsbHidTIfHidDesc;
1003 }
1004 else
1005 {
1006 cbDesc = sizeof(g_UsbHidMIfHidDesc);
1007 pDesc = (const uint8_t *)&g_UsbHidMIfHidDesc;
1008 }
1009 /* Returned data is written after the setup message. */
1010 cbCopy = pUrb->cbData - sizeof(*pSetup);
1011 cbCopy = RT_MIN(cbCopy, cbDesc);
1012 Log(("usbHidMouse: GET_DESCRIPTOR DT_IF_HID_DESCRIPTOR wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
1013 memcpy(&pUrb->abData[sizeof(*pSetup)], pDesc, cbCopy);
1014 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
1015 }
1016
1017 case DT_IF_HID_REPORT:
1018 {
1019 if (pThis->isAbsolute)
1020 {
1021 cbDesc = sizeof(g_UsbHidTReportDesc);
1022 pDesc = (const uint8_t *)&g_UsbHidTReportDesc;
1023 }
1024 else
1025 {
1026 cbDesc = sizeof(g_UsbHidMReportDesc);
1027 pDesc = (const uint8_t *)&g_UsbHidMReportDesc;
1028 }
1029 /* Returned data is written after the setup message. */
1030 cbCopy = pUrb->cbData - sizeof(*pSetup);
1031 cbCopy = RT_MIN(cbCopy, cbDesc);
1032 Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
1033 memcpy(&pUrb->abData[sizeof(*pSetup)], pDesc, cbCopy);
1034 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
1035 }
1036
1037 default:
1038 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
1039 break;
1040 }
1041 break;
1042 }
1043
1044 default:
1045 Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
1046 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
1047 }
1048 break;
1049 }
1050
1051 case VUSB_REQ_GET_STATUS:
1052 {
1053 uint16_t wRet = 0;
1054
1055 if (pSetup->wLength != 2)
1056 {
1057 Log(("usbHid: Bad GET_STATUS req: wLength=%#x\n", pSetup->wLength));
1058 break;
1059 }
1060 Assert(pSetup->wValue == 0);
1061 switch (pSetup->bmRequestType)
1062 {
1063 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1064 {
1065 Assert(pSetup->wIndex == 0);
1066 Log(("usbHid: GET_STATUS (device)\n"));
1067 wRet = 0; /* Not self-powered, no remote wakeup. */
1068 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1069 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1070 }
1071
1072 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1073 {
1074 if (pSetup->wIndex == 0)
1075 {
1076 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1077 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1078 }
1079 else
1080 {
1081 Log(("usbHid: GET_STATUS (interface) invalid, wIndex=%#x\n", pSetup->wIndex));
1082 }
1083 break;
1084 }
1085
1086 case VUSB_TO_ENDPOINT | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
1087 {
1088 if (pSetup->wIndex < RT_ELEMENTS(pThis->aEps))
1089 {
1090 wRet = pThis->aEps[pSetup->wIndex].fHalted ? 1 : 0;
1091 memcpy(&pUrb->abData[sizeof(*pSetup)], &wRet, sizeof(wRet));
1092 return usbHidCompleteOk(pThis, pUrb, sizeof(wRet) + sizeof(*pSetup));
1093 }
1094 else
1095 {
1096 Log(("usbHid: GET_STATUS (endpoint) invalid, wIndex=%#x\n", pSetup->wIndex));
1097 }
1098 break;
1099 }
1100
1101 default:
1102 Log(("usbHid: Bad GET_STATUS req: bmRequestType=%#x\n", pSetup->bmRequestType));
1103 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_STATUS");
1104 }
1105 break;
1106 }
1107
1108 case VUSB_REQ_CLEAR_FEATURE:
1109 break;
1110 }
1111
1112 /** @todo implement this. */
1113 Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1114 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1115
1116 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
1117 }
1118 /* 3.1 Bulk-Only Mass Storage Reset */
1119 else if ( pSetup->bmRequestType == (VUSB_REQ_CLASS | VUSB_TO_INTERFACE)
1120 && pSetup->bRequest == 0xff
1121 && !pSetup->wValue
1122 && !pSetup->wLength
1123 && pSetup->wIndex == 0)
1124 {
1125 Log(("usbHidHandleDefaultPipe: Bulk-Only Mass Storage Reset\n"));
1126 return usbHidResetWorker(pThis, pUrb, false /*fSetConfig*/);
1127 }
1128 else
1129 {
1130 Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
1131 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
1132 return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
1133 }
1134
1135 return VINF_SUCCESS;
1136}
1137
1138
1139/**
1140 * @copydoc PDMUSBREG::pfnUrbQueue
1141 */
1142static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
1143{
1144 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1145 LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
1146 RTCritSectEnter(&pThis->CritSect);
1147
1148 /*
1149 * Parse on a per end-point basis.
1150 */
1151 int rc;
1152 switch (pUrb->EndPt)
1153 {
1154 case 0:
1155 rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
1156 break;
1157
1158 case 0x81:
1159 AssertFailed();
1160 case 0x01:
1161 rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
1162 break;
1163
1164 default:
1165 AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
1166 rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
1167 break;
1168 }
1169
1170 RTCritSectLeave(&pThis->CritSect);
1171 return rc;
1172}
1173
1174
1175/**
1176 * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
1177 */
1178static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
1179{
1180 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1181 LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
1182
1183 if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
1184 {
1185 RTCritSectEnter(&pThis->CritSect);
1186 pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
1187 RTCritSectLeave(&pThis->CritSect);
1188 }
1189
1190 return VINF_SUCCESS;
1191}
1192
1193
1194/**
1195 * @copydoc PDMUSBREG::pfnUsbSetInterface
1196 */
1197static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
1198{
1199 LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
1200 Assert(bAlternateSetting == 0);
1201 return VINF_SUCCESS;
1202}
1203
1204
1205/**
1206 * @copydoc PDMUSBREG::pfnUsbSetConfiguration
1207 */
1208static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
1209 const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
1210{
1211 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1212 LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
1213 Assert(bConfigurationValue == 1);
1214 RTCritSectEnter(&pThis->CritSect);
1215
1216 /*
1217 * If the same config is applied more than once, it's a kind of reset.
1218 */
1219 if (pThis->bConfigurationValue == bConfigurationValue)
1220 usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
1221 pThis->bConfigurationValue = bConfigurationValue;
1222
1223 /*
1224 * Set received event type to absolute or relative.
1225 */
1226 pThis->Lun0.pDrv->pfnReportModes(pThis->Lun0.pDrv, !pThis->isAbsolute,
1227 pThis->isAbsolute);
1228
1229 RTCritSectLeave(&pThis->CritSect);
1230 return VINF_SUCCESS;
1231}
1232
1233
1234/**
1235 * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
1236 */
1237static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
1238{
1239 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1240 LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
1241 if (pThis->isAbsolute) {
1242 return &g_UsbHidTDescCache;
1243 } else {
1244 return &g_UsbHidMDescCache;
1245 }
1246}
1247
1248
1249/**
1250 * @copydoc PDMUSBREG::pfnUsbReset
1251 */
1252static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
1253{
1254 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1255 LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
1256 RTCritSectEnter(&pThis->CritSect);
1257
1258 int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
1259
1260 RTCritSectLeave(&pThis->CritSect);
1261 return rc;
1262}
1263
1264
1265/**
1266 * @copydoc PDMUSBREG::pfnDestruct
1267 */
1268static void usbHidDestruct(PPDMUSBINS pUsbIns)
1269{
1270 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1271 LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
1272
1273 if (RTCritSectIsInitialized(&pThis->CritSect))
1274 {
1275 RTCritSectEnter(&pThis->CritSect);
1276 RTCritSectLeave(&pThis->CritSect);
1277 RTCritSectDelete(&pThis->CritSect);
1278 }
1279
1280 if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
1281 {
1282 RTSemEventDestroy(pThis->hEvtDoneQueue);
1283 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1284 }
1285}
1286
1287
1288/**
1289 * @copydoc PDMUSBREG::pfnConstruct
1290 */
1291static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
1292{
1293 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
1294 Log(("usbHidConstruct/#%u:\n", iInstance));
1295
1296 /*
1297 * Perform the basic structure initialization first so the destructor
1298 * will not misbehave.
1299 */
1300 pThis->pUsbIns = pUsbIns;
1301 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
1302 usbHidQueueInit(&pThis->ToHostQueue);
1303 usbHidQueueInit(&pThis->DoneQueue);
1304
1305 int rc = RTCritSectInit(&pThis->CritSect);
1306 AssertRCReturn(rc, rc);
1307
1308 rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
1309 AssertRCReturn(rc, rc);
1310
1311 /*
1312 * Validate and read the configuration.
1313 */
1314 rc = CFGMR3ValidateConfig(pCfg, "/", "Absolute|CoordShift", "Config", "UsbHid", iInstance);
1315 if (RT_FAILURE(rc))
1316 return rc;
1317 rc = CFGMR3QueryBoolDef(pCfg, "Absolute", &pThis->isAbsolute, false);
1318 if (RT_FAILURE(rc))
1319 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to query settings"));
1320
1321 pThis->Lun0.IBase.pfnQueryInterface = usbHidMouseQueryInterface;
1322 pThis->Lun0.IPort.pfnPutEvent = usbHidMousePutEvent;
1323 pThis->Lun0.IPort.pfnPutEventAbs = usbHidMousePutEventAbs;
1324
1325 /*
1326 * Attach the mouse driver.
1327 */
1328 rc = PDMUsbHlpDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Mouse Port");
1329 if (RT_FAILURE(rc))
1330 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach mouse driver"));
1331
1332 pThis->Lun0.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Lun0.pDrvBase, PDMIMOUSECONNECTOR);
1333 if (!pThis->Lun0.pDrv)
1334 return PDMUsbHlpVMSetError(pUsbIns, VERR_PDM_MISSING_INTERFACE, RT_SRC_POS, N_("HID failed to query mouse interface"));
1335
1336 rc = CFGMR3QueryU8Def(pCfg, "CoordShift", &pThis->u8CoordShift, 1);
1337 if (RT_FAILURE(rc))
1338 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to query shift factor"));
1339
1340 return VINF_SUCCESS;
1341}
1342
1343
1344/**
1345 * The USB Human Interface Device (HID) Mouse registration record.
1346 */
1347const PDMUSBREG g_UsbHidMou =
1348{
1349 /* u32Version */
1350 PDM_USBREG_VERSION,
1351 /* szName */
1352 "HidMouse",
1353 /* pszDescription */
1354 "USB HID Mouse.",
1355 /* fFlags */
1356 0,
1357 /* cMaxInstances */
1358 ~0U,
1359 /* cbInstance */
1360 sizeof(USBHID),
1361 /* pfnConstruct */
1362 usbHidConstruct,
1363 /* pfnDestruct */
1364 usbHidDestruct,
1365 /* pfnVMInitComplete */
1366 NULL,
1367 /* pfnVMPowerOn */
1368 NULL,
1369 /* pfnVMReset */
1370 NULL,
1371 /* pfnVMSuspend */
1372 NULL,
1373 /* pfnVMResume */
1374 NULL,
1375 /* pfnVMPowerOff */
1376 NULL,
1377 /* pfnHotPlugged */
1378 NULL,
1379 /* pfnHotUnplugged */
1380 NULL,
1381 /* pfnDriverAttach */
1382 NULL,
1383 /* pfnDriverDetach */
1384 NULL,
1385 /* pfnQueryInterface */
1386 NULL,
1387 /* pfnUsbReset */
1388 usbHidUsbReset,
1389 /* pfnUsbGetDescriptorCache */
1390 usbHidUsbGetDescriptorCache,
1391 /* pfnUsbSetConfiguration */
1392 usbHidUsbSetConfiguration,
1393 /* pfnUsbSetInterface */
1394 usbHidUsbSetInterface,
1395 /* pfnUsbClearHaltedEndpoint */
1396 usbHidUsbClearHaltedEndpoint,
1397 /* pfnUrbNew */
1398 NULL/*usbHidUrbNew*/,
1399 /* pfnUrbQueue */
1400 usbHidQueue,
1401 /* pfnUrbCancel */
1402 usbHidUrbCancel,
1403 /* pfnUrbReap */
1404 usbHidUrbReap,
1405 /* u32TheEnd */
1406 PDM_USBREG_VERSION
1407};
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