VirtualBox

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

Last change on this file since 37788 was 37788, checked in by vboxsync, 13 years ago

Tablet: Added a CFGM key to modify coordinate munging.

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