VirtualBox

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

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

USB: Added a virtual USB mouse and keyboard. Not yet enabled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.0 KB
Line 
1/** @file
2 * UsbMouse - USB Human Interface Device Emulation (Mouse).
3 */
4
5/*
6 * Copyright (C) 2007-2010 Sun Microsystems, Inc.
7 *
8 * Sun Microsystems, Inc. confidential
9 * All rights reserved
10 */
11
12/*******************************************************************************
13* Header Files *
14*******************************************************************************/
15#define LOG_GROUP LOG_GROUP_USB_MSD
16#include <VBox/pdmusb.h>
17#include <VBox/log.h>
18#include <VBox/err.h>
19#include <iprt/assert.h>
20#include <iprt/critsect.h>
21#include <iprt/mem.h>
22#include <iprt/semaphore.h>
23#include <iprt/string.h>
24#include <iprt/uuid.h>
25#include "../Builtins.h"
26
27
28/*******************************************************************************
29* Defined Constants And Macros *
30*******************************************************************************/
31/** @name USB HID string IDs
32 * @{ */
33#define USBHID_STR_ID_MANUFACTURER 1
34#define USBHID_STR_ID_PRODUCT 2
35/** @} */
36
37/** @name USB HID specific descriptor types
38 * @{ */
39#define DT_IF_HID_REPORT 0x22
40/** @} */
41
42/** @name USB HID vendor and product IDs
43 * @{ */
44#define VBOX_USB_VENDOR 0x80EE
45#define USBHID_PID_MOUSE 0x0020
46/** @} */
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51
52/**
53 * The USB HID request state.
54 */
55typedef enum USBHIDREQSTATE
56{
57 /** Invalid status. */
58 USBHIDREQSTATE_INVALID = 0,
59 /** Ready to receive a new read request. */
60 USBHIDREQSTATE_READY,
61 /** Have (more) data for the host. */
62 USBHIDREQSTATE_DATA_TO_HOST,
63 /** Waiting to supply status information to the host. */
64 USBHIDREQSTATE_STATUS,
65 /** The end of the valid states. */
66 USBHIDREQSTATE_END
67} USBHIDREQSTATE;
68
69
70/**
71 * Endpoint status data.
72 */
73typedef struct USBHIDEP
74{
75 bool fHalted;
76} USBHIDEP;
77/** Pointer to the endpoint status. */
78typedef USBHIDEP *PUSBHIDEP;
79
80
81/**
82 * A URB queue.
83 */
84typedef struct USBHIDURBQUEUE
85{
86 /** The head pointer. */
87 PVUSBURB pHead;
88 /** Where to insert the next entry. */
89 PVUSBURB *ppTail;
90} USBHIDURBQUEUE;
91/** Pointer to a URB queue. */
92typedef USBHIDURBQUEUE *PUSBHIDURBQUEUE;
93/** Pointer to a const URB queue. */
94typedef USBHIDURBQUEUE const *PCUSBHIDURBQUEUE;
95
96
97/**
98 * Mouse movement accumulator.
99 */
100typedef struct USBHIDM_ACCUM
101{
102 uint32_t btn;
103 int32_t dX;
104 int32_t dY;
105 int32_t dZ;
106} USBHIDM_ACCUM, *PUSBHIDM_ACCUM;
107
108
109/**
110 * The USB HID instance data.
111 */
112typedef struct USBHID
113{
114 /** Pointer back to the PDM USB Device instance structure. */
115 PPDMUSBINS pUsbIns;
116 /** Critical section protecting the device state. */
117 RTCRITSECT CritSect;
118
119 /** The current configuration.
120 * (0 - default, 1 - the one supported configuration, i.e configured.) */
121 uint8_t bConfigurationValue;
122 /** Endpoint 0 is the default control pipe, 1 is the dev->host interrupt one. */
123 USBHIDEP aEps[2];
124 /** The state of the HID (state machine).*/
125 USBHIDREQSTATE enmState;
126
127 /** Pointer movement accumulator. */
128 USBHIDM_ACCUM PtrDelta;
129
130 /** Pending to-host queue.
131 * The URBs waiting here are waiting for data to become available.
132 */
133 USBHIDURBQUEUE ToHostQueue;
134
135 /** Done queue
136 * The URBs stashed here are waiting to be reaped. */
137 USBHIDURBQUEUE DoneQueue;
138 /** Signalled when adding an URB to the done queue and fHaveDoneQueueWaiter
139 * is set. */
140 RTSEMEVENT hEvtDoneQueue;
141 /** Someone is waiting on the done queue. */
142 bool fHaveDoneQueueWaiter;
143
144 /**
145 * Mouse port - LUN#0.
146 *
147 * @implements PDMIBASE
148 * @implements PDMIMOUSEPORT
149 */
150 struct
151 {
152 /** The base interface for the mouse port. */
153 PDMIBASE IBase;
154 /** The mouse port base interface. */
155 PDMIMOUSEPORT IPort;
156
157 /** The base interface of the attached mouse driver. */
158 R3PTRTYPE(PPDMIBASE) pDrvBase;
159 /** The mouse interface of the attached mouse driver. */
160 R3PTRTYPE(PPDMIMOUSECONNECTOR) pDrv;
161 } Lun0;
162
163} USBHID;
164/** Pointer to the USB HID instance data. */
165typedef USBHID *PUSBHID;
166
167/**
168 * The USB HID report structure.
169 */
170typedef struct USBHIDM_REPORT
171{
172 uint8_t btn;
173 int8_t dx;
174 int8_t dy;
175 int8_t dz;
176} USBHIDM_REPORT, *PUSBHIDM_REPORT;
177
178/*******************************************************************************
179* Global Variables *
180*******************************************************************************/
181static const PDMUSBDESCCACHESTRING g_aUsbHidStrings_en_US[] =
182{
183 { USBHID_STR_ID_MANUFACTURER, "VirtualBox" },
184 { USBHID_STR_ID_PRODUCT, "USB Mouse" },
185};
186
187static const PDMUSBDESCCACHELANG g_aUsbHidLanguages[] =
188{
189 { 0x0409, RT_ELEMENTS(g_aUsbHidStrings_en_US), g_aUsbHidStrings_en_US }
190};
191
192static const VUSBDESCENDPOINTEX g_aUsbHidEndpointDescs[] =
193{
194 {
195 {
196 /* .bLength = */ sizeof(VUSBDESCENDPOINT),
197 /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
198 /* .bEndpointAddress = */ 0x81 /* ep=1, in */,
199 /* .bmAttributes = */ 3 /* interrupt */,
200 /* .wMaxPacketSize = */ 4,
201 /* .bInterval = */ 10,
202 },
203 /* .pvMore = */ NULL,
204 /* .pvClass = */ NULL,
205 /* .cbClass = */ 0
206 },
207};
208
209/* HID report descriptor. */
210static const uint8_t g_UsbHidReportDesc[] =
211{
212 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
213 /* Usage */ 0x09, 0x02, /* Mouse */
214 /* Collection */ 0xA1, 0x01, /* Application */
215 /* Usage */ 0x09, 0x01, /* Pointer */
216 /* Collection */ 0xA1, 0x00, /* Physical */
217 /* Usage Page */ 0x05, 0x09, /* Button */
218 /* Usage Minimum */ 0x19, 0x01, /* Button 1 */
219 /* Usage Maximum */ 0x29, 0x03, /* Button 3 */
220 /* Logical Minimum */ 0x15, 0x00, /* 0 */
221 /* Logical Maximum */ 0x25, 0x01, /* 1 */
222 /* Report Count */ 0x95, 0x03, /* 3 */
223 /* Report Size */ 0x75, 0x01, /* 1 */
224 /* Input */ 0x81, 0x02, /* Data, Value, Absolute, Bit field */
225 /* Report Count */ 0x95, 0x01, /* 1 */
226 /* Report Size */ 0x75, 0x05, /* 5 (padding bits) */
227 /* Input */ 0x81, 0x03, /* Constant, Value, Absolute, Bit field */
228 /* Usage Page */ 0x05, 0x01, /* Generic Desktop */
229 /* Usage */ 0x09, 0x30, /* X */
230 /* Usage */ 0x09, 0x31, /* Y */
231 /* Usage */ 0x09, 0x38, /* Z (wheel) */
232 /* Logical Minimum */ 0x15, 0x81, /* -127 */
233 /* Logical Maximum */ 0x25, 0x7F, /* +127 */
234 /* Report Size */ 0x75, 0x08, /* 8 */
235 /* Report Count */ 0x95, 0x03, /* 3 */
236 /* Input */ 0x81, 0x06, /* Data, Value, Relative, Bit field */
237 /* End Collection */ 0xC0,
238 /* End Collection */ 0xC0,
239};
240
241/* Additional HID class interface descriptor. */
242static const uint8_t g_UsbHidIfHidDesc[] =
243{
244 /* .bLength = */ 0x09,
245 /* .bDescriptorType = */ 0x21, /* HID */
246 /* .bcdHID = */ 0x10, 0x01, /* 1.1 */
247 /* .bCountryCode = */ 0,
248 /* .bNumDescriptors = */ 1,
249 /* .bDescriptorType = */ 0x22, /* Report */
250 /* .wDescriptorLength = */ sizeof(g_UsbHidReportDesc), 0x00
251};
252
253static const VUSBDESCINTERFACEEX g_UsbHidInterfaceDesc =
254{
255 {
256 /* .bLength = */ sizeof(VUSBDESCINTERFACE),
257 /* .bDescriptorType = */ VUSB_DT_INTERFACE,
258 /* .bInterfaceNumber = */ 0,
259 /* .bAlternateSetting = */ 0,
260 /* .bNumEndpoints = */ 1,
261 /* .bInterfaceClass = */ 3 /* HID */,
262 /* .bInterfaceSubClass = */ 1 /* Boot Interface */,
263 /* .bInterfaceProtocol = */ 2 /* Mouse */,
264 /* .iInterface = */ 0
265 },
266 /* .pvMore = */ NULL,
267 /* .pvClass = */ &g_UsbHidIfHidDesc,
268 /* .cbClass = */ sizeof(g_UsbHidIfHidDesc),
269 &g_aUsbHidEndpointDescs[0]
270};
271
272static const VUSBINTERFACE g_aUsbHidInterfaces[] =
273{
274 { &g_UsbHidInterfaceDesc, /* .cSettings = */ 1 },
275};
276
277static const VUSBDESCCONFIGEX g_UsbHidConfigDesc =
278{
279 {
280 /* .bLength = */ sizeof(VUSBDESCCONFIG),
281 /* .bDescriptorType = */ VUSB_DT_CONFIG,
282 /* .wTotalLength = */ 0 /* recalculated on read */,
283 /* .bNumInterfaces = */ RT_ELEMENTS(g_aUsbHidInterfaces),
284 /* .bConfigurationValue =*/ 1,
285 /* .iConfiguration = */ 0,
286 /* .bmAttributes = */ RT_BIT(7),
287 /* .MaxPower = */ 50 /* 100mA */
288 },
289 NULL,
290 &g_aUsbHidInterfaces[0]
291};
292
293static const VUSBDESCDEVICE g_UsbHidDeviceDesc =
294{
295 /* .bLength = */ sizeof(g_UsbHidDeviceDesc),
296 /* .bDescriptorType = */ VUSB_DT_DEVICE,
297 /* .bcdUsb = */ 0x110, /* 1.1 */
298 /* .bDeviceClass = */ 0 /* Class specified in the interface desc. */,
299 /* .bDeviceSubClass = */ 0 /* Subclass specified in the interface desc. */,
300 /* .bDeviceProtocol = */ 0 /* Protocol specified in the interface desc. */,
301 /* .bMaxPacketSize0 = */ 8,
302 /* .idVendor = */ VBOX_USB_VENDOR,
303 /* .idProduct = */ USBHID_PID_MOUSE,
304 /* .bcdDevice = */ 0x0100, /* 1.0 */
305 /* .iManufacturer = */ USBHID_STR_ID_MANUFACTURER,
306 /* .iProduct = */ USBHID_STR_ID_PRODUCT,
307 /* .iSerialNumber = */ 0,
308 /* .bNumConfigurations = */ 1
309};
310
311static const PDMUSBDESCCACHE g_UsbHidDescCache =
312{
313 /* .pDevice = */ &g_UsbHidDeviceDesc,
314 /* .paConfigs = */ &g_UsbHidConfigDesc,
315 /* .paLanguages = */ g_aUsbHidLanguages,
316 /* .cLanguages = */ RT_ELEMENTS(g_aUsbHidLanguages),
317 /* .fUseCachedDescriptors = */ true,
318 /* .fUseCachedStringsDescriptors = */ true
319};
320
321
322/*******************************************************************************
323* Internal Functions *
324*******************************************************************************/
325
326/**
327 * Initializes an URB queue.
328 *
329 * @param pQueue The URB queue.
330 */
331static void usbHidQueueInit(PUSBHIDURBQUEUE pQueue)
332{
333 pQueue->pHead = NULL;
334 pQueue->ppTail = &pQueue->pHead;
335}
336
337
338
339/**
340 * Inserts an URB at the end of the queue.
341 *
342 * @param pQueue The URB queue.
343 * @param pUrb The URB to insert.
344 */
345DECLINLINE(void) usbHidQueueAddTail(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
346{
347 pUrb->Dev.pNext = NULL;
348 *pQueue->ppTail = pUrb;
349 pQueue->ppTail = &pUrb->Dev.pNext;
350}
351
352
353/**
354 * Unlinks the head of the queue and returns it.
355 *
356 * @returns The head entry.
357 * @param pQueue The URB queue.
358 */
359DECLINLINE(PVUSBURB) usbHidQueueRemoveHead(PUSBHIDURBQUEUE pQueue)
360{
361 PVUSBURB pUrb = pQueue->pHead;
362 if (pUrb)
363 {
364 PVUSBURB pNext = pUrb->Dev.pNext;
365 pQueue->pHead = pNext;
366 if (!pNext)
367 pQueue->ppTail = &pQueue->pHead;
368 else
369 pUrb->Dev.pNext = NULL;
370 }
371 return pUrb;
372}
373
374
375/**
376 * Removes an URB from anywhere in the queue.
377 *
378 * @returns true if found, false if not.
379 * @param pQueue The URB queue.
380 * @param pUrb The URB to remove.
381 */
382DECLINLINE(bool) usbHidQueueRemove(PUSBHIDURBQUEUE pQueue, PVUSBURB pUrb)
383{
384 PVUSBURB pCur = pQueue->pHead;
385 if (pCur == pUrb)
386 pQueue->pHead = pUrb->Dev.pNext;
387 else
388 {
389 while (pCur)
390 {
391 if (pCur->Dev.pNext == pUrb)
392 {
393 pCur->Dev.pNext = pUrb->Dev.pNext;
394 break;
395 }
396 pCur = pCur->Dev.pNext;
397 }
398 if (!pCur)
399 return false;
400 }
401 if (!pUrb->Dev.pNext)
402 pQueue->ppTail = &pQueue->pHead;
403 return true;
404}
405
406
407/**
408 * Checks if the queue is empty or not.
409 *
410 * @returns true if it is, false if it isn't.
411 * @param pQueue The URB queue.
412 */
413DECLINLINE(bool) usbHidQueueIsEmpty(PCUSBHIDURBQUEUE pQueue)
414{
415 return pQueue->pHead == NULL;
416}
417
418
419/**
420 * Links an URB into the done queue.
421 *
422 * @param pThis The HID instance.
423 * @param pUrb The URB.
424 */
425static void usbHidLinkDone(PUSBHID pThis, PVUSBURB pUrb)
426{
427 usbHidQueueAddTail(&pThis->DoneQueue, pUrb);
428
429 if (pThis->fHaveDoneQueueWaiter)
430 {
431 int rc = RTSemEventSignal(pThis->hEvtDoneQueue);
432 AssertRC(rc);
433 }
434}
435
436
437
438/**
439 * Completes the URB with a stalled state, halting the pipe.
440 */
441static int usbHidCompleteStall(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb, const char *pszWhy)
442{
443 Log(("usbHidCompleteStall/#%u: pUrb=%p:%s: %s\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, pszWhy));
444
445 pUrb->enmStatus = VUSBSTATUS_STALL;
446
447 /** @todo figure out if the stall is global or pipe-specific or both. */
448 if (pEp)
449 pEp->fHalted = true;
450 else
451 {
452 pThis->aEps[1].fHalted = true;
453 pThis->aEps[2].fHalted = true;
454 }
455
456 usbHidLinkDone(pThis, pUrb);
457 return VINF_SUCCESS;
458}
459
460
461/**
462 * Completes the URB with a OK state.
463 */
464static int usbHidCompleteOk(PUSBHID pThis, PVUSBURB pUrb, size_t cbData)
465{
466 Log(("usbHidCompleteOk/#%u: pUrb=%p:%s cbData=%#zx\n", pThis->pUsbIns->iInstance, pUrb, pUrb->pszDesc, cbData));
467
468 pUrb->enmStatus = VUSBSTATUS_OK;
469 pUrb->cbData = cbData;
470
471 usbHidLinkDone(pThis, pUrb);
472 return VINF_SUCCESS;
473}
474
475
476/**
477 * Reset worker for usbHidUsbReset, usbHidUsbSetConfiguration and
478 * usbHidUrbHandleDefaultPipe.
479 *
480 * @returns VBox status code.
481 * @param pThis The HID instance.
482 * @param pUrb Set when usbHidUrbHandleDefaultPipe is the
483 * caller.
484 * @param fSetConfig Set when usbHidUsbSetConfiguration is the
485 * caller.
486 */
487static int usbHidResetWorker(PUSBHID pThis, PVUSBURB pUrb, bool fSetConfig)
488{
489 /*
490 * Wait for the any command currently executing to complete before
491 * resetting. (We cannot cancel its execution.) How we do this depends
492 * on the reset method.
493 */
494
495 /*
496 * Reset the device state.
497 */
498 pThis->enmState = USBHIDREQSTATE_READY;
499
500 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aEps); i++)
501 pThis->aEps[i].fHalted = false;
502
503 if (!pUrb && !fSetConfig) /* (only device reset) */
504 pThis->bConfigurationValue = 0; /* default */
505
506 /*
507 * Ditch all pending URBs.
508 */
509 PVUSBURB pCurUrb;
510 while ((pCurUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue)) != NULL)
511 {
512 pCurUrb->enmStatus = VUSBSTATUS_CRC;
513 usbHidLinkDone(pThis, pCurUrb);
514 }
515
516 if (pUrb)
517 return usbHidCompleteOk(pThis, pUrb, 0);
518 return VINF_SUCCESS;
519}
520
521
522/**
523 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
524 */
525static DECLCALLBACK(void *) usbHidMouseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
526{
527 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IBase);
528 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Lun0.IBase);
529 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Lun0.IPort);
530 return NULL;
531}
532
533static int8_t clamp_i8(int32_t val)
534{
535 if (val > 127) {
536 val = 127;
537 } else if (val < -127) {
538 val = -127;
539 }
540 return val;
541}
542
543/**
544 * Mouse event handler.
545 *
546 * @returns VBox status code.
547 * @param pInterface Pointer to the mouse port interface (KBDState::Mouse.iPort).
548 * @param i32DeltaX The X delta.
549 * @param i32DeltaY The Y delta.
550 * @param i32DeltaZ The Z delta.
551 * @param i32DeltaW The W delta.
552 * @param fButtonStates The button states.
553 */
554static DECLCALLBACK(int) usbHidMousePutEvent(PPDMIMOUSEPORT pInterface, int32_t i32DeltaX, int32_t i32DeltaY, int32_t i32DeltaZ, int32_t i32DeltaW, uint32_t fButtonStates)
555{
556 PUSBHID pThis = RT_FROM_MEMBER(pInterface, USBHID, Lun0.IPort);
557// int rc = PDMCritSectEnter(&pThis->CritSect, VERR_SEM_BUSY);
558// AssertReleaseRC(rc);
559
560 /* Accumulate movement - the events from the front end may arrive
561 * at a much higher rate than USB can handle.
562 */
563 pThis->PtrDelta.btn = fButtonStates;
564 pThis->PtrDelta.dX += i32DeltaX;
565 pThis->PtrDelta.dY += i32DeltaY;
566 pThis->PtrDelta.dZ -= i32DeltaZ; /* Inverted! */
567
568 /* Check if there's a URB waiting. If so, send a report.
569 */
570 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->ToHostQueue);
571 if (pUrb)
572 {
573 USBHIDM_REPORT report;
574 size_t cbCopy;
575
576 //@todo: fix/extend
577 report.btn = pThis->PtrDelta.btn;
578 report.dx = clamp_i8(pThis->PtrDelta.dX);
579 report.dy = clamp_i8(pThis->PtrDelta.dY);
580 report.dz = clamp_i8(pThis->PtrDelta.dZ);
581
582 cbCopy = sizeof(report);
583 memcpy(&pUrb->abData[0], &report, cbCopy);
584 usbHidCompleteOk(pThis, pUrb, cbCopy);
585
586 /* Clear the accumulated movement. */
587 pThis->PtrDelta.dX = pThis->PtrDelta.dY = pThis->PtrDelta.dZ = 0;
588 }
589
590// PDMCritSectLeave(&pThis->CritSect);
591 return VINF_SUCCESS;
592}
593
594/**
595 * @copydoc PDMUSBREG::pfnUrbReap
596 */
597static DECLCALLBACK(PVUSBURB) usbHidUrbReap(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies)
598{
599 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
600 LogFlow(("usbHidUrbReap/#%u: cMillies=%u\n", pUsbIns->iInstance, cMillies));
601
602 RTCritSectEnter(&pThis->CritSect);
603
604 PVUSBURB pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
605 if (!pUrb && cMillies)
606 {
607 /* Wait */
608 pThis->fHaveDoneQueueWaiter = true;
609 RTCritSectLeave(&pThis->CritSect);
610
611 RTSemEventWait(pThis->hEvtDoneQueue, cMillies);
612
613 RTCritSectEnter(&pThis->CritSect);
614 pThis->fHaveDoneQueueWaiter = false;
615
616 pUrb = usbHidQueueRemoveHead(&pThis->DoneQueue);
617 }
618
619 RTCritSectLeave(&pThis->CritSect);
620
621 if (pUrb)
622 Log(("usbHidUrbReap/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
623 return pUrb;
624}
625
626
627/**
628 * @copydoc PDMUSBREG::pfnUrbCancel
629 */
630static DECLCALLBACK(int) usbHidUrbCancel(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
631{
632 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
633 LogFlow(("usbHidUrbCancel/#%u: pUrb=%p:%s\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc));
634 RTCritSectEnter(&pThis->CritSect);
635
636 /*
637 * Remove the URB from the to-host queue and move it onto the done queue.
638 */
639 if (usbHidQueueRemove(&pThis->ToHostQueue, pUrb))
640 usbHidLinkDone(pThis, pUrb);
641
642 RTCritSectLeave(&pThis->CritSect);
643 return VINF_SUCCESS;
644}
645
646
647/**
648 * Handles request sent to the inbound (device to host) interrupt pipe. This is
649 * rather different from bulk requests because an interrupt read URB may complete
650 * after arbitrarily long time.
651 */
652static int usbHidHandleIntrDevToHost(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
653{
654 /*
655 * Stall the request if the pipe is halted.
656 */
657 if (RT_UNLIKELY(pEp->fHalted))
658 return usbHidCompleteStall(pThis, NULL, pUrb, "Halted pipe");
659
660 /*
661 * Deal with the URB according to the state.
662 */
663 switch (pThis->enmState)
664 {
665 /*
666 * We've data left to transfer to the host.
667 */
668 case USBHIDREQSTATE_DATA_TO_HOST:
669 {
670 AssertFailed();
671 Log(("usbHidHandleIntrDevToHost: Entering STATUS\n"));
672 return usbHidCompleteOk(pThis, pUrb, 0);
673 }
674
675 /*
676 * Status transfer.
677 */
678 case USBHIDREQSTATE_STATUS:
679 {
680 AssertFailed();
681 Log(("usbHidHandleIntrDevToHost: Entering READY\n"));
682 pThis->enmState = USBHIDREQSTATE_READY;
683 return usbHidCompleteOk(pThis, pUrb, 0);
684 }
685
686 case USBHIDREQSTATE_READY:
687 usbHidQueueAddTail(&pThis->ToHostQueue, pUrb);
688 LogFlow(("usbHidHandleIntrDevToHost: Added %p:%s to the queue\n", pUrb, pUrb->pszDesc));
689 return VINF_SUCCESS;
690
691 /*
692 * Bad states, stall.
693 */
694 default:
695 Log(("usbHidHandleIntrDevToHost: enmState=%d cbData=%#x\n", pThis->enmState, pUrb->cbData));
696 return usbHidCompleteStall(pThis, NULL, pUrb, "Really bad state (D2H)!");
697 }
698}
699
700
701/**
702 * Handles request sent to the default control pipe.
703 */
704static int usbHidHandleDefaultPipe(PUSBHID pThis, PUSBHIDEP pEp, PVUSBURB pUrb)
705{
706 PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
707 AssertReturn(pUrb->cbData >= sizeof(*pSetup), VERR_VUSB_FAILED_TO_QUEUE_URB);
708
709 if ((pSetup->bmRequestType & VUSB_REQ_MASK) == VUSB_REQ_STANDARD)
710 {
711 switch (pSetup->bRequest)
712 {
713 case VUSB_REQ_GET_DESCRIPTOR:
714 {
715 switch (pSetup->bmRequestType)
716 {
717 case VUSB_TO_DEVICE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
718 {
719 switch (pSetup->wValue >> 8)
720 {
721 case VUSB_DT_STRING:
722 Log(("usbHid: GET_DESCRIPTOR DT_STRING wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
723 break;
724 default:
725 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
726 break;
727 }
728 break;
729 }
730
731 case VUSB_TO_INTERFACE | VUSB_REQ_STANDARD | VUSB_DIR_TO_HOST:
732 {
733 switch (pSetup->wValue >> 8)
734 {
735 case DT_IF_HID_REPORT:
736 uint32_t cbCopy;
737
738 /* Returned data is written after the setup message. */
739 cbCopy = pUrb->cbData - sizeof(*pSetup);
740 cbCopy = RT_MIN(cbCopy, sizeof(g_UsbHidReportDesc));
741 Log(("usbHid: GET_DESCRIPTOR DT_IF_HID_REPORT wValue=%#x wIndex=%#x cbCopy=%#x\n", pSetup->wValue, pSetup->wIndex, cbCopy));
742 memcpy(&pUrb->abData[sizeof(*pSetup)], &g_UsbHidReportDesc, cbCopy);
743 return usbHidCompleteOk(pThis, pUrb, cbCopy + sizeof(*pSetup));
744 default:
745 Log(("usbHid: GET_DESCRIPTOR, huh? wValue=%#x wIndex=%#x\n", pSetup->wValue, pSetup->wIndex));
746 break;
747 }
748 break;
749 }
750
751 default:
752 Log(("usbHid: Bad GET_DESCRIPTOR req: bmRequestType=%#x\n", pSetup->bmRequestType));
753 return usbHidCompleteStall(pThis, pEp, pUrb, "Bad GET_DESCRIPTOR");
754 }
755 break;
756 }
757
758 case VUSB_REQ_CLEAR_FEATURE:
759 break;
760 }
761
762 /** @todo implement this. */
763 Log(("usbHid: Implement standard request: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
764 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
765
766 usbHidCompleteStall(pThis, pEp, pUrb, "TODO: standard request stuff");
767 }
768 /* 3.1 Bulk-Only Mass Storage Reset */
769 else if ( pSetup->bmRequestType == (VUSB_REQ_CLASS | VUSB_TO_INTERFACE)
770 && pSetup->bRequest == 0xff
771 && !pSetup->wValue
772 && !pSetup->wLength
773 && pSetup->wIndex == 0)
774 {
775 Log(("usbHidHandleDefaultPipe: Bulk-Only Mass Storage Reset\n"));
776 return usbHidResetWorker(pThis, pUrb, false /*fSetConfig*/);
777 }
778 else
779 {
780 Log(("usbHid: Unknown control msg: bmRequestType=%#x bRequest=%#x wValue=%#x wIndex=%#x wLength=%#x\n",
781 pSetup->bmRequestType, pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
782 return usbHidCompleteStall(pThis, pEp, pUrb, "Unknown control msg");
783 }
784
785 return VINF_SUCCESS;
786}
787
788
789/**
790 * @copydoc PDMUSBREG::pfnQueue
791 */
792static DECLCALLBACK(int) usbHidQueue(PPDMUSBINS pUsbIns, PVUSBURB pUrb)
793{
794 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
795 LogFlow(("usbHidQueue/#%u: pUrb=%p:%s EndPt=%#x\n", pUsbIns->iInstance, pUrb, pUrb->pszDesc, pUrb->EndPt));
796 RTCritSectEnter(&pThis->CritSect);
797
798 /*
799 * Parse on a per end-point basis.
800 */
801 int rc;
802 switch (pUrb->EndPt)
803 {
804 case 0:
805 rc = usbHidHandleDefaultPipe(pThis, &pThis->aEps[0], pUrb);
806 break;
807
808 case 0x81:
809 AssertFailed();
810 case 0x01:
811 rc = usbHidHandleIntrDevToHost(pThis, &pThis->aEps[1], pUrb);
812 break;
813
814 default:
815 AssertMsgFailed(("EndPt=%d\n", pUrb->EndPt));
816 rc = VERR_VUSB_FAILED_TO_QUEUE_URB;
817 break;
818 }
819
820 RTCritSectLeave(&pThis->CritSect);
821 return rc;
822}
823
824
825/**
826 * @copydoc PDMUSBREG::pfnUsbClearHaltedEndpoint
827 */
828static DECLCALLBACK(int) usbHidUsbClearHaltedEndpoint(PPDMUSBINS pUsbIns, unsigned uEndpoint)
829{
830 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
831 LogFlow(("usbHidUsbClearHaltedEndpoint/#%u: uEndpoint=%#x\n", pUsbIns->iInstance, uEndpoint));
832
833 if ((uEndpoint & ~0x80) < RT_ELEMENTS(pThis->aEps))
834 {
835 RTCritSectEnter(&pThis->CritSect);
836 pThis->aEps[(uEndpoint & ~0x80)].fHalted = false;
837 RTCritSectLeave(&pThis->CritSect);
838 }
839
840 return VINF_SUCCESS;
841}
842
843
844/**
845 * @copydoc PDMUSBREG::pfnUsbSetInterface
846 */
847static DECLCALLBACK(int) usbHidUsbSetInterface(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting)
848{
849 LogFlow(("usbHidUsbSetInterface/#%u: bInterfaceNumber=%u bAlternateSetting=%u\n", pUsbIns->iInstance, bInterfaceNumber, bAlternateSetting));
850 Assert(bAlternateSetting == 0);
851 return VINF_SUCCESS;
852}
853
854
855/**
856 * @copydoc PDMUSBREG::pfnUsbSetConfiguration
857 */
858static DECLCALLBACK(int) usbHidUsbSetConfiguration(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
859 const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc)
860{
861 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
862 LogFlow(("usbHidUsbSetConfiguration/#%u: bConfigurationValue=%u\n", pUsbIns->iInstance, bConfigurationValue));
863 Assert(bConfigurationValue == 1);
864 RTCritSectEnter(&pThis->CritSect);
865
866 /*
867 * If the same config is applied more than once, it's a kind of reset.
868 */
869 if (pThis->bConfigurationValue == bConfigurationValue)
870 usbHidResetWorker(pThis, NULL, true /*fSetConfig*/); /** @todo figure out the exact difference */
871 pThis->bConfigurationValue = bConfigurationValue;
872
873 RTCritSectLeave(&pThis->CritSect);
874 return VINF_SUCCESS;
875}
876
877
878/**
879 * @copydoc PDMUSBREG::pfnUsbGetDescriptorCache
880 */
881static DECLCALLBACK(PCPDMUSBDESCCACHE) usbHidUsbGetDescriptorCache(PPDMUSBINS pUsbIns)
882{
883 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
884 LogFlow(("usbHidUsbGetDescriptorCache/#%u:\n", pUsbIns->iInstance));
885 return &g_UsbHidDescCache;
886}
887
888
889/**
890 * @copydoc PDMUSBREG::pfnUsbReset
891 */
892static DECLCALLBACK(int) usbHidUsbReset(PPDMUSBINS pUsbIns, bool fResetOnLinux)
893{
894 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
895 LogFlow(("usbHidUsbReset/#%u:\n", pUsbIns->iInstance));
896 RTCritSectEnter(&pThis->CritSect);
897
898 int rc = usbHidResetWorker(pThis, NULL, false /*fSetConfig*/);
899
900 RTCritSectLeave(&pThis->CritSect);
901 return rc;
902}
903
904
905/**
906 * @copydoc PDMUSBREG::pfnDestruct
907 */
908static void usbHidDestruct(PPDMUSBINS pUsbIns)
909{
910 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
911 LogFlow(("usbHidDestruct/#%u:\n", pUsbIns->iInstance));
912
913 if (RTCritSectIsInitialized(&pThis->CritSect))
914 {
915 RTCritSectEnter(&pThis->CritSect);
916 RTCritSectLeave(&pThis->CritSect);
917 RTCritSectDelete(&pThis->CritSect);
918 }
919
920 if (pThis->hEvtDoneQueue != NIL_RTSEMEVENT)
921 {
922 RTSemEventDestroy(pThis->hEvtDoneQueue);
923 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
924 }
925}
926
927
928/**
929 * @copydoc PDMUSBREG::pfnConstruct
930 */
931static DECLCALLBACK(int) usbHidConstruct(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal)
932{
933 PUSBHID pThis = PDMINS_2_DATA(pUsbIns, PUSBHID);
934 Log(("usbHidConstruct/#%u:\n", iInstance));
935
936 /*
937 * Perform the basic structure initialization first so the destructor
938 * will not misbehave.
939 */
940 pThis->pUsbIns = pUsbIns;
941 pThis->hEvtDoneQueue = NIL_RTSEMEVENT;
942 usbHidQueueInit(&pThis->ToHostQueue);
943 usbHidQueueInit(&pThis->DoneQueue);
944
945 int rc = RTCritSectInit(&pThis->CritSect);
946 AssertRCReturn(rc, rc);
947
948 rc = RTSemEventCreate(&pThis->hEvtDoneQueue);
949 AssertRCReturn(rc, rc);
950
951 /*
952 * Validate and read the configuration.
953 */
954 rc = CFGMR3ValidateConfig(pCfg, "/", "", "", "UsbHid", iInstance);
955 if (RT_FAILURE(rc))
956 return rc;
957
958 pThis->Lun0.IBase.pfnQueryInterface = usbHidMouseQueryInterface;
959 pThis->Lun0.IPort.pfnPutEvent = usbHidMousePutEvent;
960
961 /*
962 * Attach the mouse driver.
963 */
964 rc = pUsbIns->pHlpR3->pfnDriverAttach(pUsbIns, 0 /*iLun*/, &pThis->Lun0.IBase, &pThis->Lun0.pDrvBase, "Mouse Port");
965 if (RT_FAILURE(rc))
966 return PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, N_("HID failed to attach mouse driver"));
967
968 return VINF_SUCCESS;
969}
970
971
972/**
973 * The USB Human Interface Device (HID) Mouse registration record.
974 */
975const PDMUSBREG g_UsbHidMou =
976{
977 /* u32Version */
978 PDM_USBREG_VERSION,
979 /* szName */
980 "HidMouse",
981 /* pszDescription */
982 "USB HID Mouse.",
983 /* fFlags */
984 0,
985 /* cMaxInstances */
986 ~0,
987 /* cbInstance */
988 sizeof(USBHID),
989 /* pfnConstruct */
990 usbHidConstruct,
991 /* pfnDestruct */
992 usbHidDestruct,
993 /* pfnVMInitComplete */
994 NULL,
995 /* pfnVMPowerOn */
996 NULL,
997 /* pfnVMReset */
998 NULL,
999 /* pfnVMSuspend */
1000 NULL,
1001 /* pfnVMResume */
1002 NULL,
1003 /* pfnVMPowerOff */
1004 NULL,
1005 /* pfnHotPlugged */
1006 NULL,
1007 /* pfnHotUnplugged */
1008 NULL,
1009 /* pfnDriverAttach */
1010 NULL,
1011 /* pfnDriverDetach */
1012 NULL,
1013 /* pfnQueryInterface */
1014 NULL,
1015 /* pfnUsbReset */
1016 usbHidUsbReset,
1017 /* pfnUsbGetCachedDescriptors */
1018 usbHidUsbGetDescriptorCache,
1019 /* pfnUsbSetConfiguration */
1020 usbHidUsbSetConfiguration,
1021 /* pfnUsbSetInterface */
1022 usbHidUsbSetInterface,
1023 /* pfnUsbClearHaltedEndpoint */
1024 usbHidUsbClearHaltedEndpoint,
1025 /* pfnUrbNew */
1026 NULL/*usbHidUrbNew*/,
1027 /* pfnQueue */
1028 usbHidQueue,
1029 /* pfnUrbCancel */
1030 usbHidUrbCancel,
1031 /* pfnUrbReap */
1032 usbHidUrbReap,
1033 /* u32TheEnd */
1034 PDM_USBREG_VERSION
1035};
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette