VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/DrvVUSBRootHub.cpp@ 93939

Last change on this file since 93939 was 93939, checked in by vboxsync, 3 years ago

Devices/USB: Get rid of the address hashing mechanism and replace it with a lookup table approach, bugref:10196

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 83.7 KB
Line 
1/* $Id: DrvVUSBRootHub.cpp 93939 2022-02-24 17:23:37Z vboxsync $ */
2/** @file
3 * Virtual USB - Root Hub Driver.
4 */
5
6/*
7 * Copyright (C) 2005-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/** @page pg_dev_vusb VUSB - Virtual USB
20 *
21 * @todo read thru this and correct typos. Merge with old docs.
22 *
23 *
24 * The Virtual USB component glues USB devices and host controllers together.
25 * The VUSB takes the form of a PDM driver which is attached to the HCI. USB
26 * devices are created by, attached to, and managed by the VUSB roothub. The
27 * VUSB also exposes an interface which is used by Main to attach and detach
28 * proxied USB devices.
29 *
30 *
31 * @section sec_dev_vusb_urb The Life of an URB
32 *
33 * The URB is created when the HCI calls the roothub (VUSB) method pfnNewUrb.
34 * VUSB has a pool of URBs, if no free URBs are available a new one is
35 * allocated. The returned URB starts life in the ALLOCATED state and all
36 * fields are initialized with sensible defaults.
37 *
38 * The HCI then copies any request data into the URB if it's an host2dev
39 * transfer. It then submits the URB by calling the pfnSubmitUrb roothub
40 * method.
41 *
42 * pfnSubmitUrb will start by checking if it knows the device address, and if
43 * it doesn't the URB is completed with a device-not-ready error. When the
44 * device address is known to it, action is taken based on the kind of
45 * transfer it is. There are four kinds of transfers: 1. control, 2. bulk,
46 * 3. interrupt, and 4. isochronous. In either case something eventually ends
47 * up being submitted to the device.
48 *
49 *
50 * If an URB fails submitting, may or may not be completed. This depends on
51 * heuristics in some cases and on the kind of failure in others. If
52 * pfnSubmitUrb returns a failure, the HCI should retry submitting it at a
53 * later time. If pfnSubmitUrb returns success the URB is submitted, and it
54 * can even been completed.
55 *
56 * The URB is in the IN_FLIGHT state from the time it's successfully submitted
57 * and till it's reaped or cancelled.
58 *
59 * When an URB transfer or in some case submit failure occurs, the pfnXferError
60 * callback of the HCI is consulted about what to do. If pfnXferError indicates
61 * that the URB should be retried, pfnSubmitUrb will fail. If it indicates that
62 * it should fail, the URB will be completed.
63 *
64 * Completing an URB means that the URB status is set and the HCI
65 * pfnXferCompletion callback is invoked with the URB. The HCI is the supposed
66 * to report the transfer status to the guest OS. After completion the URB
67 * is freed and returned to the pool, unless it was cancelled. If it was
68 * cancelled it will have to await reaping before it's actually freed.
69 *
70 *
71 * @subsection subsec_dev_vusb_urb_ctrl Control
72 *
73 * The control transfer is the most complex one, from VUSB's point of view,
74 * with its three stages and being bi-directional. A control transfer starts
75 * with a SETUP packet containing the request description and two basic
76 * parameters. It is followed by zero or more DATA packets which either picks
77 * up incoming data (dev2host) or supplies the request data (host2dev). This
78 * can then be followed by a STATUS packet which gets the status of the whole
79 * transfer.
80 *
81 * What makes the control transfer complicated is that for a host2dev request
82 * the URB is assembled from the SETUP and DATA stage, and for a dev2host
83 * request the returned data must be kept around for the DATA stage. For both
84 * transfer directions the status of the transfer has to be kept around for
85 * the STATUS stage.
86 *
87 * To complicate matters further, VUSB must intercept and in some cases emulate
88 * some of the standard requests in order to keep the virtual device state
89 * correct and provide the correct virtualization of a device.
90 *
91 * @subsection subsec_dev_vusb_urb_bulk Bulk and Interrupt
92 *
93 * The bulk and interrupt transfer types are relativly simple compared to the
94 * control transfer. VUSB is not inspecting the request content or anything,
95 * but passes it down the device.
96 *
97 * @subsection subsec_dev_vusb_urb_isoc Isochronous
98 *
99 * This kind of transfers hasn't yet been implemented.
100 *
101 */
102
103
104/** @page pg_dev_vusb_old VUSB - Virtual USB Core
105 *
106 * The virtual USB core is controlled by the roothub and the underlying HCI
107 * emulator, it is responsible for device addressing, managing configurations,
108 * interfaces and endpoints, assembling and splitting multi-part control
109 * messages and in general acts as a middle layer between the USB device
110 * emulation code and USB HCI emulation code.
111 *
112 * All USB devices are represented by a struct vusb_dev. This structure
113 * contains things like the device state, device address, all the configuration
114 * descriptors, the currently selected configuration and a mapping between
115 * endpoint addresses and endpoint descriptors.
116 *
117 * Each vusb_dev also has a pointer to a vusb_dev_ops structure which serves as
118 * the virtual method table and includes a virtual constructor and destructor.
119 * After a vusb_dev is created it may be attached to a hub device such as a
120 * roothub (using vusbHubAttach). Although each hub structure has cPorts
121 * and cDevices fields, it is the responsibility of the hub device to allocate
122 * a free port for the new device.
123 *
124 * Devices can chose one of two interfaces for dealing with requests, the
125 * synchronous interface or the asynchronous interface. The synchronous
126 * interface is much simpler and ought to be used for devices which are
127 * unlikely to sleep for long periods in order to serve requests. The
128 * asynchronous interface on the other hand is more difficult to use but is
129 * useful for the USB proxy or if one were to write a mass storage device
130 * emulator. Currently the synchronous interface only supports control and bulk
131 * endpoints and is no longer used by anything.
132 *
133 * In order to use the asynchronous interface, the queue_urb, cancel_urb and
134 * pfnUrbReap fields must be set in the devices vusb_dev_ops structure. The
135 * queue_urb method is used to submit a request to a device without blocking,
136 * it returns 1 if successful and 0 on any kind of failure. A successfully
137 * queued URB is completed when the pfnUrbReap method returns it. Each function
138 * address is reference counted so that pfnUrbReap will only be called if there
139 * are URBs outstanding. For a roothub to reap an URB from any one of it's
140 * devices, the vusbRhReapAsyncUrbs() function is used.
141 *
142 * There are four types of messages an URB may contain:
143 * -# Control - represents a single packet of a multi-packet control
144 * transfer, these are only really used by the host controller to
145 * submit the parts to the usb core.
146 * -# Message - the usb core assembles multiple control transfers in
147 * to single message transfers. In this case the data buffer
148 * contains the setup packet in little endian followed by the full
149 * buffer. In the case of an host-to-device control message, the
150 * message packet is created when the STATUS transfer is seen. In
151 * the case of device-to-host messages, the message packet is
152 * created after the SETUP transfer is seen. Also, certain control
153 * requests never go the real device and get handled synchronously.
154 * -# Bulk - Currently the only endpoint type that does error checking
155 * and endpoint halting.
156 * -# Interrupt - The only non-periodic type supported.
157 *
158 * Hubs are special cases of devices, they have a number of downstream ports
159 * that other devices can be attached to and removed from.
160 *
161 * After a device has been attached (vusbHubAttach):
162 * -# The hub attach method is called, which sends a hub status
163 * change message to the OS.
164 * -# The OS resets the device, and it appears on the default
165 * address with it's config 0 selected (a pseudo-config that
166 * contains only 1 interface with 1 endpoint - the default
167 * message pipe).
168 * -# The OS assigns the device a new address and selects an
169 * appropriate config.
170 * -# The device is ready.
171 *
172 * After a device has been detached (vusbDevDetach):
173 * -# All pending URBs are cancelled.
174 * -# The devices address is unassigned.
175 * -# The hub detach method is called which signals the OS
176 * of the status change.
177 * -# The OS unlinks the ED's for that device.
178 *
179 * A device can also request detachment from within its own methods by
180 * calling vusbDevUnplugged().
181 *
182 * Roothubs are responsible for driving the whole system, they are special
183 * cases of hubs and as such implement attach and detach methods, each one
184 * is described by a struct vusb_roothub. Once a roothub has submitted an
185 * URB to the USB core, a number of callbacks to the roothub are required
186 * for when the URB completes, since the roothub typically wants to inform
187 * the OS when transfers are completed.
188 *
189 * There are four callbacks to be concerned with:
190 * -# prepare - This is called after the URB is successfully queued.
191 * -# completion - This is called after the URB completed.
192 * -# error - This is called if the URB errored, some systems have
193 * automatic resubmission of failed requests, so this callback
194 * should keep track of the error count and return 1 if the count
195 * is above the number of allowed resubmissions.
196 * -# halt_ep - This is called after errors on bulk pipes in order
197 * to halt the pipe.
198 *
199 */
200
201
202/*********************************************************************************************************************************
203* Header Files *
204*********************************************************************************************************************************/
205#define LOG_GROUP LOG_GROUP_DRV_VUSB
206#include <VBox/vmm/pdm.h>
207#include <VBox/vmm/vmapi.h>
208#include <VBox/err.h>
209#include <iprt/alloc.h>
210#include <VBox/log.h>
211#include <iprt/time.h>
212#include <iprt/thread.h>
213#include <iprt/semaphore.h>
214#include <iprt/string.h>
215#include <iprt/assert.h>
216#include <iprt/asm.h>
217#include <iprt/uuid.h>
218#include "VUSBInternal.h"
219#include "VBoxDD.h"
220
221
222#define VUSB_ROOTHUB_SAVED_STATE_VERSION 1
223
224
225/**
226 * Data used for reattaching devices on a state load.
227 */
228typedef struct VUSBROOTHUBLOAD
229{
230 /** Timer used once after state load to inform the guest about new devices.
231 * We do this to be sure the guest get any disconnect / reconnect on the
232 * same port. */
233 TMTIMERHANDLE hTimer;
234 /** Number of detached devices. */
235 unsigned cDevs;
236 /** Array of devices which were detached. */
237 PVUSBDEV apDevs[VUSB_DEVICES_MAX];
238} VUSBROOTHUBLOAD;
239
240
241/**
242 * Returns the attached VUSB device for the given port or NULL if none is attached.
243 *
244 * @returns Pointer to the VUSB device or NULL if not found.
245 * @param pThis The VUSB roothub device instance.
246 * @param uPort The port to get the device for.
247 *
248 * @note The reference count of the VUSB device structure is retained to prevent it from going away.
249 */
250static PVUSBDEV vusbR3RhGetVUsbDevByPortRetain(PVUSBROOTHUB pThis, uint32_t uPort)
251{
252 PVUSBDEV pDev = NULL;
253
254 AssertReturn(uPort < RT_ELEMENTS(pThis->apDevByPort), NULL);
255
256 RTCritSectEnter(&pThis->CritSectDevices);
257
258 pDev = pThis->apDevByPort[uPort];
259 if (RT_LIKELY(pDev))
260 vusbDevRetain(pDev);
261
262 RTCritSectLeave(&pThis->CritSectDevices);
263
264 return pDev;
265}
266
267
268/**
269 * Returns the attached VUSB device for the given port or NULL if none is attached.
270 *
271 * @returns Pointer to the VUSB device or NULL if not found.
272 * @param pThis The VUSB roothub device instance.
273 * @param u8Address The address to get the device for.
274 *
275 * @note The reference count of the VUSB device structure is retained to prevent it from going away.
276 */
277static PVUSBDEV vusbR3RhGetVUsbDevByAddrRetain(PVUSBROOTHUB pThis, uint8_t u8Address)
278{
279 PVUSBDEV pDev = NULL;
280
281 AssertReturn(u8Address < RT_ELEMENTS(pThis->apDevByAddr), NULL);
282
283 RTCritSectEnter(&pThis->CritSectDevices);
284
285 pDev = pThis->apDevByAddr[u8Address];
286 if (RT_LIKELY(pDev))
287 vusbDevRetain(pDev);
288
289 RTCritSectLeave(&pThis->CritSectDevices);
290
291 return pDev;
292}
293
294
295/**
296 * Attaches a device to a specific hub.
297 *
298 * This function is called by the vusb_add_device() and vusbRhAttachDevice().
299 *
300 * @returns VBox status code.
301 * @param pHub The hub to attach it to.
302 * @param pDev The device to attach.
303 * @thread EMT
304 */
305static int vusbHubAttach(PVUSBHUB pHub, PVUSBDEV pDev)
306{
307 LogFlow(("vusbHubAttach: pHub=%p[%s] pDev=%p[%s]\n", pHub, pHub->pszName, pDev, pDev->pUsbIns->pszName));
308 return vusbDevAttach(pDev, pHub);
309}
310
311
312/* -=-=-=-=-=- PDMUSBHUBREG methods -=-=-=-=-=- */
313
314/** @interface_method_impl{PDMUSBHUBREG,pfnAttachDevice} */
315static DECLCALLBACK(int) vusbPDMHubAttachDevice(PPDMDRVINS pDrvIns, PPDMUSBINS pUsbIns, const char *pszCaptureFilename, uint32_t *piPort)
316{
317 PVUSBROOTHUB pThis = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
318
319 /*
320 * Allocate a new VUSB device and initialize it.
321 */
322 PVUSBDEV pDev = (PVUSBDEV)RTMemAllocZ(sizeof(*pDev));
323 AssertReturn(pDev, VERR_NO_MEMORY);
324 int rc = vusbDevInit(pDev, pUsbIns, pszCaptureFilename);
325 if (RT_SUCCESS(rc))
326 {
327 pUsbIns->pvVUsbDev2 = pDev;
328 rc = vusbHubAttach(&pThis->Hub, pDev);
329 if (RT_SUCCESS(rc))
330 {
331 *piPort = UINT32_MAX; /// @todo implement piPort
332 return rc;
333 }
334
335 RTMemFree(pDev->paIfStates);
336 pUsbIns->pvVUsbDev2 = NULL;
337 }
338 vusbDevRelease(pDev);
339 return rc;
340}
341
342
343/** @interface_method_impl{PDMUSBHUBREG,pfnDetachDevice} */
344static DECLCALLBACK(int) vusbPDMHubDetachDevice(PPDMDRVINS pDrvIns, PPDMUSBINS pUsbIns, uint32_t iPort)
345{
346 RT_NOREF(pDrvIns, iPort);
347 PVUSBDEV pDev = (PVUSBDEV)pUsbIns->pvVUsbDev2;
348 Assert(pDev);
349
350 /*
351 * Deal with pending async reset.
352 * (anything but reset)
353 */
354 vusbDevSetStateCmp(pDev, VUSB_DEVICE_STATE_DEFAULT, VUSB_DEVICE_STATE_RESET);
355
356 /*
357 * Detach and free resources.
358 */
359 if (pDev->pHub)
360 vusbDevDetach(pDev);
361
362 vusbDevRelease(pDev);
363 return VINF_SUCCESS;
364}
365
366/**
367 * The hub registration structure.
368 */
369static const PDMUSBHUBREG g_vusbHubReg =
370{
371 PDM_USBHUBREG_VERSION,
372 vusbPDMHubAttachDevice,
373 vusbPDMHubDetachDevice,
374 PDM_USBHUBREG_VERSION
375};
376
377
378/* -=-=-=-=-=- VUSBIROOTHUBCONNECTOR methods -=-=-=-=-=- */
379
380
381/**
382 * Callback for freeing an URB.
383 * @param pUrb The URB to free.
384 */
385static DECLCALLBACK(void) vusbRhFreeUrb(PVUSBURB pUrb)
386{
387 /*
388 * Assert sanity.
389 */
390 vusbUrbAssert(pUrb);
391 PVUSBROOTHUB pRh = (PVUSBROOTHUB)pUrb->pVUsb->pvFreeCtx;
392 Assert(pRh);
393
394 Assert(pUrb->enmState != VUSBURBSTATE_FREE);
395
396 /*
397 * Free the URB description (logging builds only).
398 */
399 if (pUrb->pszDesc)
400 {
401 RTStrFree(pUrb->pszDesc);
402 pUrb->pszDesc = NULL;
403 }
404
405 /* The URB comes from the roothub if there is no device (invalid address). */
406 if (pUrb->pVUsb->pDev)
407 {
408 PVUSBDEV pDev = pUrb->pVUsb->pDev;
409
410 vusbUrbPoolFree(&pUrb->pVUsb->pDev->UrbPool, pUrb);
411 vusbDevRelease(pDev);
412 }
413 else
414 vusbUrbPoolFree(&pRh->Hub.Dev.UrbPool, pUrb);
415}
416
417
418/**
419 * Worker routine for vusbRhConnNewUrb().
420 */
421static PVUSBURB vusbRhNewUrb(PVUSBROOTHUB pRh, uint8_t DstAddress, uint32_t uPort, VUSBXFERTYPE enmType,
422 VUSBDIRECTION enmDir, uint32_t cbData, uint32_t cTds, const char *pszTag)
423{
424 RT_NOREF(pszTag);
425 PVUSBURBPOOL pUrbPool = &pRh->Hub.Dev.UrbPool;
426
427 if (RT_UNLIKELY(cbData > (32 * _1M)))
428 {
429 LogFunc(("Bad URB size (%u)!\n", cbData));
430 return NULL;
431 }
432
433 PVUSBDEV pDev;
434 if (uPort == VUSB_DEVICE_PORT_INVALID)
435 pDev = vusbR3RhGetVUsbDevByAddrRetain(pRh, DstAddress);
436 else
437 pDev = vusbR3RhGetVUsbDevByPortRetain(pRh, uPort);
438
439 if (pDev)
440 pUrbPool = &pDev->UrbPool;
441
442 PVUSBURB pUrb = vusbUrbPoolAlloc(pUrbPool, enmType, enmDir, cbData,
443 pRh->cbHci, pRh->cbHciTd, cTds);
444 if (RT_LIKELY(pUrb))
445 {
446 pUrb->pVUsb->pvFreeCtx = pRh;
447 pUrb->pVUsb->pfnFree = vusbRhFreeUrb;
448 pUrb->DstAddress = DstAddress;
449 pUrb->pVUsb->pDev = pDev;
450
451#ifdef LOG_ENABLED
452 const char *pszType = NULL;
453
454 switch(pUrb->enmType)
455 {
456 case VUSBXFERTYPE_CTRL:
457 pszType = "ctrl";
458 break;
459 case VUSBXFERTYPE_INTR:
460 pszType = "intr";
461 break;
462 case VUSBXFERTYPE_BULK:
463 pszType = "bulk";
464 break;
465 case VUSBXFERTYPE_ISOC:
466 pszType = "isoc";
467 break;
468 default:
469 pszType = "invld";
470 break;
471 }
472
473 pRh->iSerial = (pRh->iSerial + 1) % 10000;
474 RTStrAPrintf(&pUrb->pszDesc, "URB %p %s%c%04d (%s)", pUrb, pszType,
475 (pUrb->enmDir == VUSBDIRECTION_IN) ? '<' : ((pUrb->enmDir == VUSBDIRECTION_SETUP) ? 's' : '>'),
476 pRh->iSerial, pszTag ? pszTag : "<none>");
477#endif
478 }
479
480 return pUrb;
481}
482
483
484/**
485 * Calculate frame timer variables given a frame rate.
486 */
487static void vusbRhR3CalcTimerIntervals(PVUSBROOTHUB pThis, uint32_t u32FrameRate)
488{
489 pThis->nsWait = RT_NS_1SEC / u32FrameRate;
490 pThis->uFrameRate = u32FrameRate;
491 /* Inform the HCD about the new frame rate. */
492 pThis->pIRhPort->pfnFrameRateChanged(pThis->pIRhPort, u32FrameRate);
493}
494
495
496/**
497 * Calculates the new frame rate based on the idle detection and number of idle
498 * cycles.
499 *
500 * @returns nothing.
501 * @param pThis The roothub instance data.
502 * @param fIdle Flag whether the last frame didn't produce any activity.
503 */
504static void vusbRhR3FrameRateCalcNew(PVUSBROOTHUB pThis, bool fIdle)
505{
506 uint32_t uNewFrameRate = pThis->uFrameRate;
507
508 /*
509 * Adjust the frame timer interval based on idle detection.
510 */
511 if (fIdle)
512 {
513 pThis->cIdleCycles++;
514 /* Set the new frame rate based on how long we've been idle. Tunable. */
515 switch (pThis->cIdleCycles)
516 {
517 case 4: uNewFrameRate = 500; break; /* 2ms interval */
518 case 16:uNewFrameRate = 125; break; /* 8ms interval */
519 case 24:uNewFrameRate = 50; break; /* 20ms interval */
520 default: break;
521 }
522 /* Avoid overflow. */
523 if (pThis->cIdleCycles > 60000)
524 pThis->cIdleCycles = 20000;
525 }
526 else
527 {
528 if (pThis->cIdleCycles)
529 {
530 pThis->cIdleCycles = 0;
531 uNewFrameRate = pThis->uFrameRateDefault;
532 }
533 }
534
535 if ( uNewFrameRate != pThis->uFrameRate
536 && uNewFrameRate)
537 {
538 LogFlow(("Frame rate changed from %u to %u\n", pThis->uFrameRate, uNewFrameRate));
539 vusbRhR3CalcTimerIntervals(pThis, uNewFrameRate);
540 }
541}
542
543
544/**
545 * The core frame processing routine keeping track of the elapsed time and calling into
546 * the device emulation above us to do the work.
547 *
548 * @returns Relative timespan when to process the next frame.
549 * @param pThis The roothub instance data.
550 * @param fCallback Flag whether this method is called from the URB completion callback or
551 * from the worker thread (only used for statistics).
552 */
553DECLHIDDEN(uint64_t) vusbRhR3ProcessFrame(PVUSBROOTHUB pThis, bool fCallback)
554{
555 uint64_t tsNext = 0;
556 uint64_t tsNanoStart = RTTimeNanoTS();
557
558 /* Don't do anything if we are not supposed to process anything (EHCI and XHCI). */
559 if (!pThis->uFrameRateDefault)
560 return 0;
561
562 if (ASMAtomicXchgBool(&pThis->fFrameProcessing, true))
563 return pThis->nsWait;
564
565 if ( tsNanoStart > pThis->tsFrameProcessed
566 && tsNanoStart - pThis->tsFrameProcessed >= 750 * RT_NS_1US)
567 {
568 LogFlowFunc(("Starting new frame at ts %llu\n", tsNanoStart));
569
570 bool fIdle = pThis->pIRhPort->pfnStartFrame(pThis->pIRhPort, 0 /* u32FrameNo */);
571 vusbRhR3FrameRateCalcNew(pThis, fIdle);
572
573 uint64_t tsNow = RTTimeNanoTS();
574 tsNext = (tsNanoStart + pThis->nsWait) > tsNow ? (tsNanoStart + pThis->nsWait) - tsNow : 0;
575 pThis->tsFrameProcessed = tsNanoStart;
576 LogFlowFunc(("Current frame took %llu nano seconds to process, next frame in %llu ns\n", tsNow - tsNanoStart, tsNext));
577 if (fCallback)
578 STAM_COUNTER_INC(&pThis->StatFramesProcessedClbk);
579 else
580 STAM_COUNTER_INC(&pThis->StatFramesProcessedThread);
581 }
582 else
583 {
584 tsNext = (pThis->tsFrameProcessed + pThis->nsWait) > tsNanoStart ? (pThis->tsFrameProcessed + pThis->nsWait) - tsNanoStart : 0;
585 LogFlowFunc(("Next frame is too far away in the future, waiting... (tsNanoStart=%llu tsFrameProcessed=%llu)\n",
586 tsNanoStart, pThis->tsFrameProcessed));
587 }
588
589 ASMAtomicXchgBool(&pThis->fFrameProcessing, false);
590 LogFlowFunc(("returns %llu\n", tsNext));
591 return tsNext;
592}
593
594
595/**
596 * Worker for processing frames periodically.
597 *
598 * @returns VBox status code.
599 * @param pDrvIns The driver instance.
600 * @param pThread The PDM thread structure for the thread this worker runs on.
601 */
602static DECLCALLBACK(int) vusbRhR3PeriodFrameWorker(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
603{
604 RT_NOREF(pDrvIns);
605 int rc = VINF_SUCCESS;
606 PVUSBROOTHUB pThis = (PVUSBROOTHUB)pThread->pvUser;
607
608 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
609 return VINF_SUCCESS;
610
611 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
612 {
613 while ( !ASMAtomicReadU32(&pThis->uFrameRateDefault)
614 && pThread->enmState == PDMTHREADSTATE_RUNNING)
615 {
616 /* Signal the waiter that we are stopped now. */
617 rc = RTSemEventMultiSignal(pThis->hSemEventPeriodFrameStopped);
618 AssertRC(rc);
619
620 rc = RTSemEventMultiWait(pThis->hSemEventPeriodFrame, RT_INDEFINITE_WAIT);
621 RTSemEventMultiReset(pThis->hSemEventPeriodFrame);
622
623 /*
624 * Notify the device above about the frame rate changed if we are supposed to
625 * process frames.
626 */
627 uint32_t uFrameRate = ASMAtomicReadU32(&pThis->uFrameRateDefault);
628 if (uFrameRate)
629 vusbRhR3CalcTimerIntervals(pThis, uFrameRate);
630 }
631
632 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_TIMEOUT, ("%Rrc\n", rc), rc);
633 if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
634 break;
635
636 uint64_t tsNext = vusbRhR3ProcessFrame(pThis, false /* fCallback */);
637
638 if (tsNext >= 250 * RT_NS_1US)
639 {
640 rc = RTSemEventMultiWaitEx(pThis->hSemEventPeriodFrame, RTSEMWAIT_FLAGS_RELATIVE | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_UNINTERRUPTIBLE,
641 tsNext);
642 AssertLogRelMsg(RT_SUCCESS(rc) || rc == VERR_TIMEOUT, ("%Rrc\n", rc));
643 RTSemEventMultiReset(pThis->hSemEventPeriodFrame);
644 }
645 }
646
647 return VINF_SUCCESS;
648}
649
650
651/**
652 * Unblock the periodic frame thread so it can respond to a state change.
653 *
654 * @returns VBox status code.
655 * @param pDrvIns The driver instance.
656 * @param pThread The send thread.
657 */
658static DECLCALLBACK(int) vusbRhR3PeriodFrameWorkerWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
659{
660 RT_NOREF(pThread);
661 PVUSBROOTHUB pThis = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
662 return RTSemEventMultiSignal(pThis->hSemEventPeriodFrame);
663}
664
665
666/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnSetUrbParams} */
667static DECLCALLBACK(int) vusbRhSetUrbParams(PVUSBIROOTHUBCONNECTOR pInterface, size_t cbHci, size_t cbHciTd)
668{
669 PVUSBROOTHUB pRh = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
670
671 pRh->cbHci = cbHci;
672 pRh->cbHciTd = cbHciTd;
673
674 return VINF_SUCCESS;
675}
676
677
678/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnReset} */
679static DECLCALLBACK(int) vusbR3RhReset(PVUSBIROOTHUBCONNECTOR pInterface, bool fResetOnLinux)
680{
681 PVUSBROOTHUB pRh = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
682 return pRh->pIRhPort->pfnReset(pRh->pIRhPort, fResetOnLinux);
683}
684
685
686/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnPowerOn} */
687static DECLCALLBACK(int) vusbR3RhPowerOn(PVUSBIROOTHUBCONNECTOR pInterface)
688{
689 PVUSBROOTHUB pRh = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
690 LogFlow(("vusR3bRhPowerOn: pRh=%p\n", pRh));
691
692 Assert( pRh->Hub.Dev.enmState != VUSB_DEVICE_STATE_DETACHED
693 && pRh->Hub.Dev.enmState != VUSB_DEVICE_STATE_RESET);
694
695 if (pRh->Hub.Dev.enmState == VUSB_DEVICE_STATE_ATTACHED)
696 pRh->Hub.Dev.enmState = VUSB_DEVICE_STATE_POWERED;
697
698 return VINF_SUCCESS;
699}
700
701
702/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnPowerOff} */
703static DECLCALLBACK(int) vusbR3RhPowerOff(PVUSBIROOTHUBCONNECTOR pInterface)
704{
705 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
706 LogFlow(("vusbR3RhDevPowerOff: pThis=%p\n", pThis));
707
708 Assert( pThis->Hub.Dev.enmState != VUSB_DEVICE_STATE_DETACHED
709 && pThis->Hub.Dev.enmState != VUSB_DEVICE_STATE_RESET);
710
711 /*
712 * Cancel all URBs and reap them.
713 */
714 VUSBIRhCancelAllUrbs(&pThis->IRhConnector);
715 for (uint32_t uPort = 0; uPort < RT_ELEMENTS(pThis->apDevByPort); uPort++)
716 VUSBIRhReapAsyncUrbs(&pThis->IRhConnector, uPort, 0);
717
718 pThis->Hub.Dev.enmState = VUSB_DEVICE_STATE_ATTACHED;
719 return VINF_SUCCESS;
720}
721
722
723/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnNewUrb} */
724static DECLCALLBACK(PVUSBURB) vusbRhConnNewUrb(PVUSBIROOTHUBCONNECTOR pInterface, uint8_t DstAddress, uint32_t uPort, VUSBXFERTYPE enmType,
725 VUSBDIRECTION enmDir, uint32_t cbData, uint32_t cTds, const char *pszTag)
726{
727 PVUSBROOTHUB pRh = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
728 return vusbRhNewUrb(pRh, DstAddress, uPort, enmType, enmDir, cbData, cTds, pszTag);
729}
730
731
732/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnFreeUrb} */
733static DECLCALLBACK(int) vusbRhConnFreeUrb(PVUSBIROOTHUBCONNECTOR pInterface, PVUSBURB pUrb)
734{
735 RT_NOREF(pInterface);
736 pUrb->pVUsb->pfnFree(pUrb);
737 return VINF_SUCCESS;
738}
739
740
741/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnSubmitUrb} */
742static DECLCALLBACK(int) vusbRhSubmitUrb(PVUSBIROOTHUBCONNECTOR pInterface, PVUSBURB pUrb, PPDMLED pLed)
743{
744 PVUSBROOTHUB pRh = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
745 STAM_PROFILE_START(&pRh->StatSubmitUrb, a);
746
747#ifdef VBOX_WITH_STATISTICS
748 /*
749 * Total and per-type submit statistics.
750 */
751 Assert(pUrb->enmType >= 0 && pUrb->enmType < (int)RT_ELEMENTS(pRh->aTypes));
752 STAM_COUNTER_INC(&pRh->Total.StatUrbsSubmitted);
753 STAM_COUNTER_INC(&pRh->aTypes[pUrb->enmType].StatUrbsSubmitted);
754
755 STAM_COUNTER_ADD(&pRh->Total.StatReqBytes, pUrb->cbData);
756 STAM_COUNTER_ADD(&pRh->aTypes[pUrb->enmType].StatReqBytes, pUrb->cbData);
757 if (pUrb->enmDir == VUSBDIRECTION_IN)
758 {
759 STAM_COUNTER_ADD(&pRh->Total.StatReqReadBytes, pUrb->cbData);
760 STAM_COUNTER_ADD(&pRh->aTypes[pUrb->enmType].StatReqReadBytes, pUrb->cbData);
761 }
762 else
763 {
764 STAM_COUNTER_ADD(&pRh->Total.StatReqWriteBytes, pUrb->cbData);
765 STAM_COUNTER_ADD(&pRh->aTypes[pUrb->enmType].StatReqWriteBytes, pUrb->cbData);
766 }
767
768 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
769 {
770 STAM_COUNTER_ADD(&pRh->StatIsocReqPkts, pUrb->cIsocPkts);
771 if (pUrb->enmDir == VUSBDIRECTION_IN)
772 STAM_COUNTER_ADD(&pRh->StatIsocReqReadPkts, pUrb->cIsocPkts);
773 else
774 STAM_COUNTER_ADD(&pRh->StatIsocReqWritePkts, pUrb->cIsocPkts);
775 }
776#endif
777
778 /* If there is a sniffer on the roothub record the URB there. */
779 if (pRh->hSniffer != VUSBSNIFFER_NIL)
780 {
781 int rc = VUSBSnifferRecordEvent(pRh->hSniffer, pUrb, VUSBSNIFFEREVENT_SUBMIT);
782 if (RT_FAILURE(rc))
783 LogRel(("VUSB: Capturing URB submit event on the root hub failed with %Rrc\n", rc));
784 }
785
786 /*
787 * The device was resolved when we allocated the URB.
788 * Submit it to the device if we found it, if not fail with device-not-ready.
789 */
790 int rc;
791 if ( pUrb->pVUsb->pDev
792 && pUrb->pVUsb->pDev->pUsbIns)
793 {
794 switch (pUrb->enmDir)
795 {
796 case VUSBDIRECTION_IN:
797 pLed->Asserted.s.fReading = pLed->Actual.s.fReading = 1;
798 rc = vusbUrbSubmit(pUrb);
799 pLed->Actual.s.fReading = 0;
800 break;
801 case VUSBDIRECTION_OUT:
802 pLed->Asserted.s.fWriting = pLed->Actual.s.fWriting = 1;
803 rc = vusbUrbSubmit(pUrb);
804 pLed->Actual.s.fWriting = 0;
805 break;
806 default:
807 rc = vusbUrbSubmit(pUrb);
808 break;
809 }
810
811 if (RT_FAILURE(rc))
812 {
813 LogFlow(("vusbRhSubmitUrb: freeing pUrb=%p\n", pUrb));
814 pUrb->pVUsb->pfnFree(pUrb);
815 }
816 }
817 else
818 {
819 vusbDevRetain(&pRh->Hub.Dev);
820 pUrb->pVUsb->pDev = &pRh->Hub.Dev;
821 Log(("vusb: pRh=%p: SUBMIT: Address %i not found!!!\n", pRh, pUrb->DstAddress));
822
823 pUrb->enmState = VUSBURBSTATE_REAPED;
824 pUrb->enmStatus = VUSBSTATUS_DNR;
825 vusbUrbCompletionRh(pUrb);
826 rc = VINF_SUCCESS;
827 }
828
829 STAM_PROFILE_STOP(&pRh->StatSubmitUrb, a);
830 return rc;
831}
832
833
834static DECLCALLBACK(int) vusbRhReapAsyncUrbsWorker(PVUSBDEV pDev, RTMSINTERVAL cMillies)
835{
836 if (!cMillies)
837 vusbUrbDoReapAsync(&pDev->LstAsyncUrbs, 0);
838 else
839 {
840 uint64_t u64Start = RTTimeMilliTS();
841 do
842 {
843 vusbUrbDoReapAsync(&pDev->LstAsyncUrbs, RT_MIN(cMillies >> 8, 10));
844 } while ( !RTListIsEmpty(&pDev->LstAsyncUrbs)
845 && RTTimeMilliTS() - u64Start < cMillies);
846 }
847
848 return VINF_SUCCESS;
849}
850
851/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnReapAsyncUrbs} */
852static DECLCALLBACK(void) vusbRhReapAsyncUrbs(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uPort, RTMSINTERVAL cMillies)
853{
854 PVUSBROOTHUB pRh = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface); NOREF(pRh);
855 PVUSBDEV pDev = vusbR3RhGetVUsbDevByPortRetain(pRh, uPort);
856
857 if ( !pDev
858 || RTListIsEmpty(&pDev->LstAsyncUrbs))
859 return;
860
861 STAM_PROFILE_START(&pRh->StatReapAsyncUrbs, a);
862 int rc = vusbDevIoThreadExecSync(pDev, (PFNRT)vusbRhReapAsyncUrbsWorker, 2, pDev, cMillies);
863 AssertRC(rc);
864 STAM_PROFILE_STOP(&pRh->StatReapAsyncUrbs, a);
865
866 vusbDevRelease(pDev);
867}
868
869
870/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnCancelUrbsEp} */
871static DECLCALLBACK(int) vusbRhCancelUrbsEp(PVUSBIROOTHUBCONNECTOR pInterface, PVUSBURB pUrb)
872{
873 PVUSBROOTHUB pRh = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
874 AssertReturn(pRh, VERR_INVALID_PARAMETER);
875 AssertReturn(pUrb, VERR_INVALID_PARAMETER);
876
877 /// @todo This method of URB canceling may not work on non-Linux hosts.
878 /*
879 * Cancel and reap the URB(s) on an endpoint.
880 */
881 LogFlow(("vusbRhCancelUrbsEp: pRh=%p pUrb=%p\n", pRh, pUrb));
882
883 vusbUrbCancelAsync(pUrb, CANCELMODE_UNDO);
884
885 /* The reaper thread will take care of completing the URB. */
886
887 return VINF_SUCCESS;
888}
889
890/**
891 * Worker doing the actual cancelling of all outstanding URBs on the device I/O thread.
892 *
893 * @returns VBox status code.
894 * @param pDev USB device instance data.
895 */
896static DECLCALLBACK(int) vusbRhCancelAllUrbsWorker(PVUSBDEV pDev)
897{
898 /*
899 * Cancel the URBS.
900 *
901 * Not using th CritAsyncUrbs critical section here is safe
902 * as the I/O thread is the only thread accessing this struture at the
903 * moment.
904 */
905 PVUSBURBVUSB pVUsbUrb, pVUsbUrbNext;
906 RTListForEachSafe(&pDev->LstAsyncUrbs, pVUsbUrb, pVUsbUrbNext, VUSBURBVUSBINT, NdLst)
907 {
908 PVUSBURB pUrb = pVUsbUrb->pUrb;
909 /* Call the worker directly. */
910 vusbUrbCancelWorker(pUrb, CANCELMODE_FAIL);
911 }
912
913 return VINF_SUCCESS;
914}
915
916/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnCancelAllUrbs} */
917static DECLCALLBACK(void) vusbRhCancelAllUrbs(PVUSBIROOTHUBCONNECTOR pInterface)
918{
919 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
920
921 RTCritSectEnter(&pThis->CritSectDevices);
922 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apDevByPort); i++)
923 {
924 PVUSBDEV pDev = pThis->apDevByPort[i];
925 if (pDev)
926 vusbDevIoThreadExecSync(pDev, (PFNRT)vusbRhCancelAllUrbsWorker, 1, pDev);
927 }
928 RTCritSectLeave(&pThis->CritSectDevices);
929}
930
931/**
932 * Worker doing the actual cancelling of all outstanding per-EP URBs on the
933 * device I/O thread.
934 *
935 * @returns VBox status code.
936 * @param pDev USB device instance data.
937 * @param EndPt Endpoint number.
938 * @param enmDir Endpoint direction.
939 */
940static DECLCALLBACK(int) vusbRhAbortEpWorker(PVUSBDEV pDev, int EndPt, VUSBDIRECTION enmDir)
941{
942 /*
943 * Iterate the URBs, find ones corresponding to given EP, and cancel them.
944 */
945 PVUSBURBVUSB pVUsbUrb, pVUsbUrbNext;
946 RTListForEachSafe(&pDev->LstAsyncUrbs, pVUsbUrb, pVUsbUrbNext, VUSBURBVUSBINT, NdLst)
947 {
948 PVUSBURB pUrb = pVUsbUrb->pUrb;
949
950 Assert(pUrb->pVUsb->pDev == pDev);
951
952 /* For the default control EP, direction does not matter. */
953 if (pUrb->EndPt == EndPt && (pUrb->enmDir == enmDir || !EndPt))
954 {
955 LogFlow(("%s: vusbRhAbortEpWorker: CANCELING URB\n", pUrb->pszDesc));
956 int rc = vusbUrbCancelWorker(pUrb, CANCELMODE_UNDO);
957 AssertRC(rc);
958 }
959 }
960
961 return VINF_SUCCESS;
962}
963
964
965/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnAbortEp} */
966static DECLCALLBACK(int) vusbRhAbortEp(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uPort, int EndPt, VUSBDIRECTION enmDir)
967{
968 PVUSBROOTHUB pRh = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
969 PVUSBDEV pDev = vusbR3RhGetVUsbDevByPortRetain(pRh, uPort);
970
971 if (&pRh->Hub != pDev->pHub)
972 AssertFailedReturn(VERR_INVALID_PARAMETER);
973
974 vusbDevIoThreadExecSync(pDev, (PFNRT)vusbRhAbortEpWorker, 3, pDev, EndPt, enmDir);
975 vusbDevRelease(pDev);
976
977 /* The reaper thread will take care of completing the URB. */
978
979 return VINF_SUCCESS;
980}
981
982
983/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnSetPeriodicFrameProcessing} */
984static DECLCALLBACK(int) vusbRhSetFrameProcessing(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uFrameRate)
985{
986 int rc = VINF_SUCCESS;
987 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
988
989 /* Create the frame thread lazily. */
990 if ( !pThis->hThreadPeriodFrame
991 && uFrameRate)
992 {
993 ASMAtomicXchgU32(&pThis->uFrameRateDefault, uFrameRate);
994 pThis->uFrameRate = uFrameRate;
995 vusbRhR3CalcTimerIntervals(pThis, uFrameRate);
996
997 rc = RTSemEventMultiCreate(&pThis->hSemEventPeriodFrame);
998 AssertRCReturn(rc, rc);
999
1000 rc = RTSemEventMultiCreate(&pThis->hSemEventPeriodFrameStopped);
1001 AssertRCReturn(rc, rc);
1002
1003 rc = PDMDrvHlpThreadCreate(pThis->pDrvIns, &pThis->hThreadPeriodFrame, pThis, vusbRhR3PeriodFrameWorker,
1004 vusbRhR3PeriodFrameWorkerWakeup, 0, RTTHREADTYPE_IO, "VUsbPeriodFrm");
1005 AssertRCReturn(rc, rc);
1006
1007 VMSTATE enmState = PDMDrvHlpVMState(pThis->pDrvIns);
1008 if ( enmState == VMSTATE_RUNNING
1009 || enmState == VMSTATE_RUNNING_LS)
1010 {
1011 rc = PDMDrvHlpThreadResume(pThis->pDrvIns, pThis->hThreadPeriodFrame);
1012 AssertRCReturn(rc, rc);
1013 }
1014 }
1015 else if ( pThis->hThreadPeriodFrame
1016 && !uFrameRate)
1017 {
1018 /* Stop processing. */
1019 uint32_t uFrameRateOld = ASMAtomicXchgU32(&pThis->uFrameRateDefault, uFrameRate);
1020 if (uFrameRateOld)
1021 {
1022 rc = RTSemEventMultiReset(pThis->hSemEventPeriodFrameStopped);
1023 AssertRC(rc);
1024
1025 /* Signal the frame thread to stop. */
1026 RTSemEventMultiSignal(pThis->hSemEventPeriodFrame);
1027
1028 /* Wait for signal from the thread that it stopped. */
1029 rc = RTSemEventMultiWait(pThis->hSemEventPeriodFrameStopped, RT_INDEFINITE_WAIT);
1030 AssertRC(rc);
1031 }
1032 }
1033 else if ( pThis->hThreadPeriodFrame
1034 && uFrameRate)
1035 {
1036 /* Just switch to the new frame rate and let the periodic frame thread pick it up. */
1037 uint32_t uFrameRateOld = ASMAtomicXchgU32(&pThis->uFrameRateDefault, uFrameRate);
1038
1039 /* Signal the frame thread to continue if it was stopped. */
1040 if (!uFrameRateOld)
1041 RTSemEventMultiSignal(pThis->hSemEventPeriodFrame);
1042 }
1043
1044 return rc;
1045}
1046
1047
1048/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnGetPeriodicFrameRate} */
1049static DECLCALLBACK(uint32_t) vusbRhGetPeriodicFrameRate(PVUSBIROOTHUBCONNECTOR pInterface)
1050{
1051 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
1052
1053 return pThis->uFrameRate;
1054}
1055
1056/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnUpdateIsocFrameDelta} */
1057static DECLCALLBACK(uint32_t) vusbRhUpdateIsocFrameDelta(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uPort,
1058 int EndPt, VUSBDIRECTION enmDir, uint16_t uNewFrameID, uint8_t uBits)
1059{
1060 PVUSBROOTHUB pRh = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
1061 AssertReturn(pRh, 0);
1062 PVUSBDEV pDev = vusbR3RhGetVUsbDevByPortRetain(pRh, uPort); AssertPtr(pDev);
1063 PVUSBPIPE pPipe = &pDev->aPipes[EndPt];
1064 uint32_t *puLastFrame;
1065 int32_t uFrameDelta;
1066 uint32_t uMaxVal = 1 << uBits;
1067
1068 puLastFrame = enmDir == VUSBDIRECTION_IN ? &pPipe->uLastFrameIn : &pPipe->uLastFrameOut;
1069 uFrameDelta = uNewFrameID - *puLastFrame;
1070 *puLastFrame = uNewFrameID;
1071 /* Take care of wrap-around. */
1072 if (uFrameDelta < 0)
1073 uFrameDelta += uMaxVal;
1074
1075 vusbDevRelease(pDev);
1076 return (uint16_t)uFrameDelta;
1077}
1078
1079
1080/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnDevReset} */
1081static DECLCALLBACK(int) vusbR3RhDevReset(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uPort, bool fResetOnLinux,
1082 PFNVUSBRESETDONE pfnDone, void *pvUser, PVM pVM)
1083{
1084 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
1085 PVUSBDEV pDev = vusbR3RhGetVUsbDevByPortRetain(pThis, uPort);
1086 AssertPtr(pDev);
1087
1088 int rc = VUSBIDevReset(&pDev->IDevice, fResetOnLinux, pfnDone, pvUser, pVM);
1089 vusbDevRelease(pDev);
1090 return rc;
1091}
1092
1093
1094/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnDevPowerOn} */
1095static DECLCALLBACK(int) vusbR3RhDevPowerOn(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uPort)
1096{
1097 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
1098 PVUSBDEV pDev = vusbR3RhGetVUsbDevByPortRetain(pThis, uPort);
1099 AssertPtr(pDev);
1100
1101 int rc = VUSBIDevPowerOn(&pDev->IDevice);
1102 vusbDevRelease(pDev);
1103 return rc;
1104}
1105
1106
1107/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnDevPowerOff} */
1108static DECLCALLBACK(int) vusbR3RhDevPowerOff(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uPort)
1109{
1110 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
1111 PVUSBDEV pDev = vusbR3RhGetVUsbDevByPortRetain(pThis, uPort);
1112 AssertPtr(pDev);
1113
1114 int rc = VUSBIDevPowerOff(&pDev->IDevice);
1115 vusbDevRelease(pDev);
1116 return rc;
1117}
1118
1119
1120/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnDevGetState} */
1121static DECLCALLBACK(VUSBDEVICESTATE) vusbR3RhDevGetState(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uPort)
1122{
1123 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
1124 PVUSBDEV pDev = vusbR3RhGetVUsbDevByPortRetain(pThis, uPort);
1125 AssertPtr(pDev);
1126
1127 VUSBDEVICESTATE enmState = VUSBIDevGetState(&pDev->IDevice);
1128 vusbDevRelease(pDev);
1129 return enmState;
1130}
1131
1132
1133/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnDevIsSavedStateSupported} */
1134static DECLCALLBACK(bool) vusbR3RhDevIsSavedStateSupported(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uPort)
1135{
1136 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
1137 PVUSBDEV pDev = vusbR3RhGetVUsbDevByPortRetain(pThis, uPort);
1138 AssertPtr(pDev);
1139
1140 bool fSavedStateSupported = VUSBIDevIsSavedStateSupported(&pDev->IDevice);
1141 vusbDevRelease(pDev);
1142 return fSavedStateSupported;
1143}
1144
1145
1146/** @interface_method_impl{VUSBIROOTHUBCONNECTOR,pfnDevGetSpeed} */
1147static DECLCALLBACK(VUSBSPEED) vusbR3RhDevGetSpeed(PVUSBIROOTHUBCONNECTOR pInterface, uint32_t uPort)
1148{
1149 PVUSBROOTHUB pThis = VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface);
1150 PVUSBDEV pDev = vusbR3RhGetVUsbDevByPortRetain(pThis, uPort);
1151 AssertPtr(pDev);
1152
1153 VUSBSPEED enmSpeed = pDev->IDevice.pfnGetSpeed(&pDev->IDevice);
1154 vusbDevRelease(pDev);
1155 return enmSpeed;
1156}
1157
1158
1159static const char *vusbGetSpeedString(VUSBSPEED enmSpeed)
1160{
1161 const char *pszSpeed = NULL;
1162
1163 switch (enmSpeed)
1164 {
1165 case VUSB_SPEED_LOW:
1166 pszSpeed = "Low";
1167 break;
1168 case VUSB_SPEED_FULL:
1169 pszSpeed = "Full";
1170 break;
1171 case VUSB_SPEED_HIGH:
1172 pszSpeed = "High";
1173 break;
1174 case VUSB_SPEED_VARIABLE:
1175 pszSpeed = "Variable";
1176 break;
1177 case VUSB_SPEED_SUPER:
1178 pszSpeed = "Super";
1179 break;
1180 case VUSB_SPEED_SUPERPLUS:
1181 pszSpeed = "SuperPlus";
1182 break;
1183 default:
1184 pszSpeed = "Unknown";
1185 break;
1186 }
1187 return pszSpeed;
1188}
1189
1190
1191/**
1192 * @callback_method_impl{FNSSMDRVSAVEPREP, All URBs needs to be canceled.}
1193 */
1194static DECLCALLBACK(int) vusbR3RhSavePrep(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
1195{
1196 PVUSBROOTHUB pThis = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
1197 LogFlow(("vusbR3RhSavePrep:\n"));
1198 RT_NOREF(pSSM);
1199
1200 /*
1201 * Detach all proxied devices.
1202 */
1203 RTCritSectEnter(&pThis->CritSectDevices);
1204
1205 /** @todo we a) can't tell which are proxied, and b) this won't work well when continuing after saving! */
1206 for (unsigned i = 0; i < RT_ELEMENTS(pThis->apDevByPort); i++)
1207 {
1208 PVUSBDEV pDev = pThis->apDevByPort[i];
1209 if (pDev)
1210 {
1211 if (!VUSBIDevIsSavedStateSupported(&pDev->IDevice))
1212 {
1213 int rc = vusbDevDetach(pDev);
1214 AssertRC(rc);
1215
1216 /*
1217 * Save the device pointers here so we can reattach them afterwards.
1218 * This will work fine even if the save fails since the Done handler is
1219 * called unconditionally if the Prep handler was called.
1220 */
1221 pThis->apDevByPort[i] = pDev;
1222 }
1223 }
1224 }
1225
1226 RTCritSectLeave(&pThis->CritSectDevices);
1227
1228 /*
1229 * Kill old load data which might be hanging around.
1230 */
1231 if (pThis->pLoad)
1232 {
1233 PDMDrvHlpTimerDestroy(pDrvIns, pThis->pLoad->hTimer);
1234 pThis->pLoad->hTimer = NIL_TMTIMERHANDLE;
1235 PDMDrvHlpMMHeapFree(pDrvIns, pThis->pLoad);
1236 pThis->pLoad = NULL;
1237 }
1238
1239 return VINF_SUCCESS;
1240}
1241
1242
1243/**
1244 * @callback_method_impl{FNSSMDRVSAVEDONE}
1245 */
1246static DECLCALLBACK(int) vusbR3RhSaveDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
1247{
1248 PVUSBROOTHUB pThis = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
1249 PVUSBDEV aPortsOld[VUSB_DEVICES_MAX];
1250 unsigned i;
1251 LogFlow(("vusbR3RhSaveDone:\n"));
1252 RT_NOREF(pSSM);
1253
1254 /* Save the current data. */
1255 memcpy(aPortsOld, pThis->apDevByPort, sizeof(aPortsOld));
1256 AssertCompile(sizeof(aPortsOld) == sizeof(pThis->apDevByPort));
1257
1258 /*
1259 * NULL the dev pointers.
1260 */
1261 for (i = 0; i < RT_ELEMENTS(pThis->apDevByPort); i++)
1262 if (pThis->apDevByPort[i] && !VUSBIDevIsSavedStateSupported(&pThis->apDevByPort[i]->IDevice))
1263 pThis->apDevByPort[i] = NULL;
1264
1265 /*
1266 * Attach the devices.
1267 */
1268 for (i = 0; i < RT_ELEMENTS(pThis->apDevByPort); i++)
1269 {
1270 PVUSBDEV pDev = aPortsOld[i];
1271 if (pDev && !VUSBIDevIsSavedStateSupported(&pDev->IDevice))
1272 vusbHubAttach(&pThis->Hub, pDev);
1273 }
1274
1275 return VINF_SUCCESS;
1276}
1277
1278
1279/**
1280 * @callback_method_impl{FNSSMDRVLOADPREP, This must detach the devices
1281 * currently attached and save them for reconnect after the state load has been
1282 * completed.}
1283 */
1284static DECLCALLBACK(int) vusbR3RhLoadPrep(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
1285{
1286 PVUSBROOTHUB pThis = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
1287 int rc = VINF_SUCCESS;
1288 LogFlow(("vusbR3RhLoadPrep:\n"));
1289 RT_NOREF(pSSM);
1290
1291 if (!pThis->pLoad)
1292 {
1293 VUSBROOTHUBLOAD Load;
1294 unsigned i;
1295
1296 /// @todo This is all bogus.
1297 /*
1298 * Detach all devices which are present in this session. Save them in the load
1299 * structure so we can reattach them after restoring the guest.
1300 */
1301 Load.hTimer = NIL_TMTIMERHANDLE;
1302 Load.cDevs = 0;
1303 for (i = 0; i < RT_ELEMENTS(pThis->apDevByPort); i++)
1304 {
1305 PVUSBDEV pDev = pThis->apDevByPort[i];
1306 if (pDev && !VUSBIDevIsSavedStateSupported(&pDev->IDevice))
1307 {
1308 Load.apDevs[Load.cDevs++] = pDev;
1309 vusbDevDetach(pDev);
1310 Assert(!pThis->apDevByPort[i]);
1311 }
1312 }
1313
1314 /*
1315 * Any devices to reattach? If so, duplicate the Load struct.
1316 */
1317 if (Load.cDevs)
1318 {
1319 pThis->pLoad = (PVUSBROOTHUBLOAD)RTMemAllocZ(sizeof(Load));
1320 if (!pThis->pLoad)
1321 return VERR_NO_MEMORY;
1322 *pThis->pLoad = Load;
1323 }
1324 }
1325 /* else: we ASSUME no device can be attached or detached in the time
1326 * between a state load and the pLoad stuff processing. */
1327 return rc;
1328}
1329
1330
1331/**
1332 * Reattaches devices after a saved state load.
1333 */
1334static DECLCALLBACK(void) vusbR3RhLoadReattachDevices(PPDMDRVINS pDrvIns, TMTIMERHANDLE hTimer, void *pvUser)
1335{
1336 PVUSBROOTHUB pThis = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
1337 PVUSBROOTHUBLOAD pLoad = pThis->pLoad;
1338 LogFlow(("vusbR3RhLoadReattachDevices:\n"));
1339 Assert(hTimer == pLoad->hTimer); RT_NOREF(pvUser);
1340
1341 /*
1342 * Reattach devices.
1343 */
1344 for (unsigned i = 0; i < pLoad->cDevs; i++)
1345 vusbHubAttach(&pThis->Hub, pLoad->apDevs[i]);
1346
1347 /*
1348 * Cleanup.
1349 */
1350 PDMDrvHlpTimerDestroy(pDrvIns, hTimer);
1351 pLoad->hTimer = NIL_TMTIMERHANDLE;
1352 RTMemFree(pLoad);
1353 pThis->pLoad = NULL;
1354}
1355
1356
1357/**
1358 * @callback_method_impl{FNSSMDRVLOADDONE}
1359 */
1360static DECLCALLBACK(int) vusbR3RhLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
1361{
1362 PVUSBROOTHUB pThis = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
1363 LogFlow(("vusbR3RhLoadDone:\n"));
1364 RT_NOREF(pSSM);
1365
1366 /*
1367 * Start a timer if we've got devices to reattach
1368 */
1369 if (pThis->pLoad)
1370 {
1371 int rc = PDMDrvHlpTMTimerCreate(pDrvIns, TMCLOCK_VIRTUAL, vusbR3RhLoadReattachDevices, NULL,
1372 TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_NO_RING0,
1373 "VUSB reattach on load", &pThis->pLoad->hTimer);
1374 if (RT_SUCCESS(rc))
1375 rc = PDMDrvHlpTimerSetMillies(pDrvIns, pThis->pLoad->hTimer, 250);
1376 return rc;
1377 }
1378
1379 return VINF_SUCCESS;
1380}
1381
1382
1383/* -=-=-=-=-=- VUSB Hub methods -=-=-=-=-=- */
1384
1385
1386/**
1387 * Attach the device to the hub.
1388 * Port assignments and all such stuff is up to this routine.
1389 *
1390 * @returns VBox status code.
1391 * @param pHub Pointer to the hub.
1392 * @param pDev Pointer to the device.
1393 */
1394static int vusbRhHubOpAttach(PVUSBHUB pHub, PVUSBDEV pDev)
1395{
1396 PVUSBROOTHUB pRh = (PVUSBROOTHUB)pHub;
1397
1398 /*
1399 * Assign a port.
1400 */
1401 int iPort = ASMBitFirstSet(&pRh->Bitmap, sizeof(pRh->Bitmap) * 8);
1402 if (iPort < 0)
1403 {
1404 LogRel(("VUSB: No ports available!\n"));
1405 return VERR_VUSB_NO_PORTS;
1406 }
1407 ASMBitClear(&pRh->Bitmap, iPort);
1408 pHub->cDevices++;
1409 pDev->i16Port = iPort;
1410
1411 /*
1412 * Call the HCI attach routine and let it have its say before the device is
1413 * linked into the device list of this hub.
1414 */
1415 VUSBSPEED enmSpeed = pDev->IDevice.pfnGetSpeed(&pDev->IDevice);
1416 int rc = pRh->pIRhPort->pfnAttach(pRh->pIRhPort, iPort, enmSpeed);
1417 if (RT_SUCCESS(rc))
1418 {
1419 RTCritSectEnter(&pRh->CritSectDevices);
1420 Assert(!pRh->apDevByPort[iPort]);
1421 pRh->apDevByPort[iPort] = pDev;
1422
1423 RTCritSectLeave(&pRh->CritSectDevices);
1424 LogRel(("VUSB: Attached '%s' to port %d on %s (%sSpeed)\n", pDev->pUsbIns->pszName,
1425 iPort, pHub->pszName, vusbGetSpeedString(pDev->pUsbIns->enmSpeed)));
1426 }
1427 else
1428 {
1429 ASMBitSet(&pRh->Bitmap, iPort);
1430 pHub->cDevices--;
1431 pDev->i16Port = -1;
1432 LogRel(("VUSB: Failed to attach '%s' to port %d, rc=%Rrc\n", pDev->pUsbIns->pszName, iPort, rc));
1433 }
1434 return rc;
1435}
1436
1437
1438/**
1439 * Detach the device from the hub.
1440 *
1441 * @returns VBox status code.
1442 * @param pHub Pointer to the hub.
1443 * @param pDev Pointer to the device.
1444 */
1445static void vusbRhHubOpDetach(PVUSBHUB pHub, PVUSBDEV pDev)
1446{
1447 PVUSBROOTHUB pRh = (PVUSBROOTHUB)pHub;
1448 Assert(pDev->i16Port != -1);
1449
1450 /* Check that it's attached and remvoe it. */
1451 RTCritSectEnter(&pRh->CritSectDevices);
1452 Assert(pRh->apDevByPort[pDev->i16Port] == pDev);
1453 pRh->apDevByPort[pDev->i16Port] = NULL;
1454
1455 if (pDev->u8Address == VUSB_DEFAULT_ADDRESS)
1456 {
1457 AssertPtr(pRh->apDevByAddr[VUSB_DEFAULT_ADDRESS]);
1458
1459 if (pDev == pRh->apDevByAddr[VUSB_DEFAULT_ADDRESS])
1460 pRh->apDevByAddr[VUSB_DEFAULT_ADDRESS] = pDev->pNextDefAddr;
1461 else
1462 {
1463 /* Search the list for the device and remove it. */
1464 PVUSBDEV pDevPrev = pRh->apDevByAddr[VUSB_DEFAULT_ADDRESS];
1465
1466 while ( pDevPrev
1467 && pDevPrev->pNextDefAddr != pDev)
1468 pDevPrev = pDevPrev->pNextDefAddr;
1469
1470 AssertPtr(pDevPrev);
1471 pDevPrev->pNextDefAddr = pDev->pNextDefAddr;
1472 }
1473
1474 pDev->pNextDefAddr = NULL;
1475 }
1476 else
1477 {
1478 Assert(pRh->apDevByAddr[pDev->u8Address] == pDev);
1479 pRh->apDevByAddr[pDev->u8Address] = NULL;
1480 }
1481 RTCritSectLeave(&pRh->CritSectDevices);
1482
1483 /*
1484 * Detach the device and mark the port as available.
1485 */
1486 unsigned uPort = pDev->i16Port;
1487 pRh->pIRhPort->pfnDetach(pRh->pIRhPort, uPort);
1488 LogRel(("VUSB: Detached '%s' from port %u on %s\n", pDev->pUsbIns->pszName, uPort, pHub->pszName));
1489 ASMBitSet(&pRh->Bitmap, uPort);
1490 pHub->cDevices--;
1491}
1492
1493
1494/**
1495 * The Hub methods implemented by the root hub.
1496 */
1497static const VUSBHUBOPS s_VUsbRhHubOps =
1498{
1499 vusbRhHubOpAttach,
1500 vusbRhHubOpDetach
1501};
1502
1503
1504
1505/* -=-=-=-=-=- PDM Base interface methods -=-=-=-=-=- */
1506
1507
1508/**
1509 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1510 */
1511static DECLCALLBACK(void *) vusbRhQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1512{
1513 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1514 PVUSBROOTHUB pRh = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
1515
1516 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1517 PDMIBASE_RETURN_INTERFACE(pszIID, VUSBIROOTHUBCONNECTOR, &pRh->IRhConnector);
1518 PDMIBASE_RETURN_INTERFACE(pszIID, VUSBIDEVICE, &pRh->Hub.Dev.IDevice);
1519 return NULL;
1520}
1521
1522
1523/* -=-=-=-=-=- PDM Driver methods -=-=-=-=-=- */
1524
1525
1526/**
1527 * Destruct a driver instance.
1528 *
1529 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
1530 * resources can be freed correctly.
1531 *
1532 * @param pDrvIns The driver instance data.
1533 */
1534static DECLCALLBACK(void) vusbRhDestruct(PPDMDRVINS pDrvIns)
1535{
1536 PVUSBROOTHUB pRh = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
1537 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1538
1539 vusbUrbPoolDestroy(&pRh->Hub.Dev.UrbPool);
1540 if (pRh->Hub.pszName)
1541 {
1542 RTStrFree(pRh->Hub.pszName);
1543 pRh->Hub.pszName = NULL;
1544 }
1545 if (pRh->hSniffer != VUSBSNIFFER_NIL)
1546 VUSBSnifferDestroy(pRh->hSniffer);
1547
1548 if (pRh->hSemEventPeriodFrame)
1549 RTSemEventMultiDestroy(pRh->hSemEventPeriodFrame);
1550
1551 if (pRh->hSemEventPeriodFrameStopped)
1552 RTSemEventMultiDestroy(pRh->hSemEventPeriodFrameStopped);
1553
1554 RTCritSectDelete(&pRh->CritSectDevices);
1555}
1556
1557
1558/**
1559 * Construct a root hub driver instance.
1560 *
1561 * @copydoc FNPDMDRVCONSTRUCT
1562 */
1563static DECLCALLBACK(int) vusbRhConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1564{
1565 RT_NOREF(fFlags);
1566 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1567 PVUSBROOTHUB pThis = PDMINS_2_DATA(pDrvIns, PVUSBROOTHUB);
1568 PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
1569
1570 LogFlow(("vusbRhConstruct: Instance %d\n", pDrvIns->iInstance));
1571
1572 /*
1573 * Validate configuration.
1574 */
1575 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "CaptureFilename", "");
1576
1577 /*
1578 * Check that there are no drivers below us.
1579 */
1580 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
1581 ("Configuration error: Not possible to attach anything to this driver!\n"),
1582 VERR_PDM_DRVINS_NO_ATTACH);
1583
1584 /*
1585 * Initialize the critical sections.
1586 */
1587 int rc = RTCritSectInit(&pThis->CritSectDevices);
1588 if (RT_FAILURE(rc))
1589 return rc;
1590
1591 char *pszCaptureFilename = NULL;
1592 rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "CaptureFilename", &pszCaptureFilename);
1593 if ( RT_FAILURE(rc)
1594 && rc != VERR_CFGM_VALUE_NOT_FOUND)
1595 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1596 N_("Configuration error: Failed to query value of \"CaptureFilename\""));
1597
1598 /*
1599 * Initialize the data members.
1600 */
1601 pDrvIns->IBase.pfnQueryInterface = vusbRhQueryInterface;
1602 /* the usb device */
1603 pThis->Hub.Dev.enmState = VUSB_DEVICE_STATE_ATTACHED;
1604 pThis->Hub.Dev.cRefs = 1;
1605 /* the hub */
1606 pThis->Hub.pOps = &s_VUsbRhHubOps;
1607 pThis->Hub.pRootHub = pThis;
1608 //pThis->hub.cPorts - later
1609 pThis->Hub.cDevices = 0;
1610 pThis->Hub.Dev.pHub = &pThis->Hub;
1611 RTStrAPrintf(&pThis->Hub.pszName, "RootHub#%d", pDrvIns->iInstance);
1612 /* misc */
1613 pThis->pDrvIns = pDrvIns;
1614 /* the connector */
1615 pThis->IRhConnector.pfnSetUrbParams = vusbRhSetUrbParams;
1616 pThis->IRhConnector.pfnReset = vusbR3RhReset;
1617 pThis->IRhConnector.pfnPowerOn = vusbR3RhPowerOn;
1618 pThis->IRhConnector.pfnPowerOff = vusbR3RhPowerOff;
1619 pThis->IRhConnector.pfnNewUrb = vusbRhConnNewUrb;
1620 pThis->IRhConnector.pfnFreeUrb = vusbRhConnFreeUrb;
1621 pThis->IRhConnector.pfnSubmitUrb = vusbRhSubmitUrb;
1622 pThis->IRhConnector.pfnReapAsyncUrbs = vusbRhReapAsyncUrbs;
1623 pThis->IRhConnector.pfnCancelUrbsEp = vusbRhCancelUrbsEp;
1624 pThis->IRhConnector.pfnCancelAllUrbs = vusbRhCancelAllUrbs;
1625 pThis->IRhConnector.pfnAbortEp = vusbRhAbortEp;
1626 pThis->IRhConnector.pfnSetPeriodicFrameProcessing = vusbRhSetFrameProcessing;
1627 pThis->IRhConnector.pfnGetPeriodicFrameRate = vusbRhGetPeriodicFrameRate;
1628 pThis->IRhConnector.pfnUpdateIsocFrameDelta = vusbRhUpdateIsocFrameDelta;
1629 pThis->IRhConnector.pfnDevReset = vusbR3RhDevReset;
1630 pThis->IRhConnector.pfnDevPowerOn = vusbR3RhDevPowerOn;
1631 pThis->IRhConnector.pfnDevPowerOff = vusbR3RhDevPowerOff;
1632 pThis->IRhConnector.pfnDevGetState = vusbR3RhDevGetState;
1633 pThis->IRhConnector.pfnDevIsSavedStateSupported = vusbR3RhDevIsSavedStateSupported;
1634 pThis->IRhConnector.pfnDevGetSpeed = vusbR3RhDevGetSpeed;
1635 pThis->hSniffer = VUSBSNIFFER_NIL;
1636 pThis->cbHci = 0;
1637 pThis->cbHciTd = 0;
1638 pThis->fFrameProcessing = false;
1639#ifdef LOG_ENABLED
1640 pThis->iSerial = 0;
1641#endif
1642 /*
1643 * Resolve interface(s).
1644 */
1645 pThis->pIRhPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, VUSBIROOTHUBPORT);
1646 AssertMsgReturn(pThis->pIRhPort, ("Configuration error: the device/driver above us doesn't expose any VUSBIROOTHUBPORT interface!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
1647
1648 /*
1649 * Get number of ports and the availability bitmap.
1650 * ASSUME that the number of ports reported now at creation time is the max number.
1651 */
1652 pThis->Hub.cPorts = pThis->pIRhPort->pfnGetAvailablePorts(pThis->pIRhPort, &pThis->Bitmap);
1653 Log(("vusbRhConstruct: cPorts=%d\n", pThis->Hub.cPorts));
1654
1655 /*
1656 * Get the USB version of the attached HC.
1657 * ASSUME that version 2.0 implies high-speed.
1658 */
1659 pThis->fHcVersions = pThis->pIRhPort->pfnGetUSBVersions(pThis->pIRhPort);
1660 Log(("vusbRhConstruct: fHcVersions=%u\n", pThis->fHcVersions));
1661
1662 rc = vusbUrbPoolInit(&pThis->Hub.Dev.UrbPool);
1663 if (RT_FAILURE(rc))
1664 return rc;
1665
1666 if (pszCaptureFilename)
1667 {
1668 rc = VUSBSnifferCreate(&pThis->hSniffer, 0, pszCaptureFilename, NULL, NULL);
1669 if (RT_FAILURE(rc))
1670 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1671 N_("VUSBSniffer cannot open '%s' for writing. The directory must exist and it must be writable for the current user"),
1672 pszCaptureFilename);
1673
1674 PDMDrvHlpMMHeapFree(pDrvIns, pszCaptureFilename);
1675 }
1676
1677 /*
1678 * Register ourselves as a USB hub.
1679 * The current implementation uses the VUSBIRHCONFIG interface for communication.
1680 */
1681 PCPDMUSBHUBHLP pHlpUsb; /* not used currently */
1682 rc = PDMDrvHlpUSBRegisterHub(pDrvIns, pThis->fHcVersions, pThis->Hub.cPorts, &g_vusbHubReg, &pHlpUsb);
1683 if (RT_FAILURE(rc))
1684 return rc;
1685
1686 /*
1687 * Register the saved state data unit for attaching devices.
1688 */
1689 rc = PDMDrvHlpSSMRegisterEx(pDrvIns, VUSB_ROOTHUB_SAVED_STATE_VERSION, 0,
1690 NULL, NULL, NULL,
1691 vusbR3RhSavePrep, NULL, vusbR3RhSaveDone,
1692 vusbR3RhLoadPrep, NULL, vusbR3RhLoadDone);
1693 AssertRCReturn(rc, rc);
1694
1695 /*
1696 * Statistics. (It requires a 30" monitor or extremely tiny fonts to edit this "table".)
1697 */
1698#ifdef VBOX_WITH_STATISTICS
1699 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->Total.StatUrbsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "The number of URBs submitted.", "/VUSB/%d/UrbsSubmitted", pDrvIns->iInstance);
1700 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatUrbsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Bulk transfer.", "/VUSB/%d/UrbsSubmitted/Bulk", pDrvIns->iInstance);
1701 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatUrbsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Control transfer.", "/VUSB/%d/UrbsSubmitted/Ctrl", pDrvIns->iInstance);
1702 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatUrbsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Interrupt transfer.", "/VUSB/%d/UrbsSubmitted/Intr", pDrvIns->iInstance);
1703 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatUrbsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Isochronous transfer.", "/VUSB/%d/UrbsSubmitted/Isoc", pDrvIns->iInstance);
1704
1705 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->Total.StatUrbsCancelled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "The number of URBs cancelled. (included in failed)", "/VUSB/%d/UrbsCancelled", pDrvIns->iInstance);
1706 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatUrbsCancelled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Bulk transfer.", "/VUSB/%d/UrbsCancelled/Bulk", pDrvIns->iInstance);
1707 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatUrbsCancelled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Control transfer.", "/VUSB/%d/UrbsCancelled/Ctrl", pDrvIns->iInstance);
1708 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatUrbsCancelled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Interrupt transfer.", "/VUSB/%d/UrbsCancelled/Intr", pDrvIns->iInstance);
1709 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatUrbsCancelled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Isochronous transfer.", "/VUSB/%d/UrbsCancelled/Isoc", pDrvIns->iInstance);
1710
1711 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->Total.StatUrbsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "The number of URBs failing.", "/VUSB/%d/UrbsFailed", pDrvIns->iInstance);
1712 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatUrbsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Bulk transfer.", "/VUSB/%d/UrbsFailed/Bulk", pDrvIns->iInstance);
1713 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatUrbsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Control transfer.", "/VUSB/%d/UrbsFailed/Ctrl", pDrvIns->iInstance);
1714 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatUrbsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Interrupt transfer.", "/VUSB/%d/UrbsFailed/Intr", pDrvIns->iInstance);
1715 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatUrbsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Isochronous transfer.", "/VUSB/%d/UrbsFailed/Isoc", pDrvIns->iInstance);
1716
1717 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->Total.StatReqBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Total requested transfer.", "/VUSB/%d/ReqBytes", pDrvIns->iInstance);
1718 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatReqBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Bulk transfer.", "/VUSB/%d/ReqBytes/Bulk", pDrvIns->iInstance);
1719 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatReqBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Control transfer.", "/VUSB/%d/ReqBytes/Ctrl", pDrvIns->iInstance);
1720 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatReqBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Interrupt transfer.", "/VUSB/%d/ReqBytes/Intr", pDrvIns->iInstance);
1721 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatReqBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Isochronous transfer.", "/VUSB/%d/ReqBytes/Isoc", pDrvIns->iInstance);
1722
1723 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->Total.StatReqReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Total requested read transfer.", "/VUSB/%d/ReqReadBytes", pDrvIns->iInstance);
1724 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatReqReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Bulk transfer.", "/VUSB/%d/ReqReadBytes/Bulk", pDrvIns->iInstance);
1725 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatReqReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Control transfer.", "/VUSB/%d/ReqReadBytes/Ctrl", pDrvIns->iInstance);
1726 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatReqReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Interrupt transfer.", "/VUSB/%d/ReqReadBytes/Intr", pDrvIns->iInstance);
1727 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatReqReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Isochronous transfer.", "/VUSB/%d/ReqReadBytes/Isoc", pDrvIns->iInstance);
1728
1729 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->Total.StatReqWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Total requested write transfer.", "/VUSB/%d/ReqWriteBytes", pDrvIns->iInstance);
1730 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatReqWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Bulk transfer.", "/VUSB/%d/ReqWriteBytes/Bulk", pDrvIns->iInstance);
1731 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatReqWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Control transfer.", "/VUSB/%d/ReqWriteBytes/Ctrl", pDrvIns->iInstance);
1732 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatReqWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Interrupt transfer.", "/VUSB/%d/ReqWriteBytes/Intr", pDrvIns->iInstance);
1733 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatReqWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Isochronous transfer.", "/VUSB/%d/ReqWriteBytes/Isoc", pDrvIns->iInstance);
1734
1735 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->Total.StatActBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Actual total transfer.", "/VUSB/%d/ActBytes", pDrvIns->iInstance);
1736 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatActBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Bulk transfer.", "/VUSB/%d/ActBytes/Bulk", pDrvIns->iInstance);
1737 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatActBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Control transfer.", "/VUSB/%d/ActBytes/Ctrl", pDrvIns->iInstance);
1738 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatActBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Interrupt transfer.", "/VUSB/%d/ActBytes/Intr", pDrvIns->iInstance);
1739 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatActBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Isochronous transfer.", "/VUSB/%d/ActBytes/Isoc", pDrvIns->iInstance);
1740
1741 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->Total.StatActReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Actual total read transfer.", "/VUSB/%d/ActReadBytes", pDrvIns->iInstance);
1742 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatActReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Bulk transfer.", "/VUSB/%d/ActReadBytes/Bulk", pDrvIns->iInstance);
1743 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatActReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Control transfer.", "/VUSB/%d/ActReadBytes/Ctrl", pDrvIns->iInstance);
1744 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatActReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Interrupt transfer.", "/VUSB/%d/ActReadBytes/Intr", pDrvIns->iInstance);
1745 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatActReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Isochronous transfer.", "/VUSB/%d/ActReadBytes/Isoc", pDrvIns->iInstance);
1746
1747 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->Total.StatActWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Actual total write transfer.", "/VUSB/%d/ActWriteBytes", pDrvIns->iInstance);
1748 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatActWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Bulk transfer.", "/VUSB/%d/ActWriteBytes/Bulk", pDrvIns->iInstance);
1749 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatActWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Control transfer.", "/VUSB/%d/ActWriteBytes/Ctrl", pDrvIns->iInstance);
1750 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatActWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Interrupt transfer.", "/VUSB/%d/ActWriteBytes/Intr", pDrvIns->iInstance);
1751 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatActWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Isochronous transfer.", "/VUSB/%d/ActWriteBytes/Isoc", pDrvIns->iInstance);
1752
1753 /* bulk */
1754 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatUrbsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of submitted URBs.", "/VUSB/%d/Bulk/Urbs", pDrvIns->iInstance);
1755 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatUrbsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of failed URBs.", "/VUSB/%d/Bulk/UrbsFailed", pDrvIns->iInstance);
1756 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatUrbsCancelled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of cancelled URBs.", "/VUSB/%d/Bulk/UrbsFailed/Cancelled", pDrvIns->iInstance);
1757 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatActBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of bytes transferred.", "/VUSB/%d/Bulk/ActBytes", pDrvIns->iInstance);
1758 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatActReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Read.", "/VUSB/%d/Bulk/ActBytes/Read", pDrvIns->iInstance);
1759 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatActWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Write.", "/VUSB/%d/Bulk/ActBytes/Write", pDrvIns->iInstance);
1760 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatReqBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Requested number of bytes.", "/VUSB/%d/Bulk/ReqBytes", pDrvIns->iInstance);
1761 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatReqReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Read.", "/VUSB/%d/Bulk/ReqBytes/Read", pDrvIns->iInstance);
1762 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_BULK].StatReqWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Write.", "/VUSB/%d/Bulk/ReqBytes/Write", pDrvIns->iInstance);
1763
1764 /* control */
1765 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatUrbsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of submitted URBs.", "/VUSB/%d/Ctrl/Urbs", pDrvIns->iInstance);
1766 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatUrbsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of failed URBs.", "/VUSB/%d/Ctrl/UrbsFailed", pDrvIns->iInstance);
1767 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatUrbsCancelled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of cancelled URBs.", "/VUSB/%d/Ctrl/UrbsFailed/Cancelled", pDrvIns->iInstance);
1768 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatActBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of bytes transferred.", "/VUSB/%d/Ctrl/ActBytes", pDrvIns->iInstance);
1769 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatActReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Read.", "/VUSB/%d/Ctrl/ActBytes/Read", pDrvIns->iInstance);
1770 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatActWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Write.", "/VUSB/%d/Ctrl/ActBytes/Write", pDrvIns->iInstance);
1771 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatReqBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Requested number of bytes.", "/VUSB/%d/Ctrl/ReqBytes", pDrvIns->iInstance);
1772 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatReqReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Read.", "/VUSB/%d/Ctrl/ReqBytes/Read", pDrvIns->iInstance);
1773 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_CTRL].StatReqWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Write.", "/VUSB/%d/Ctrl/ReqBytes/Write", pDrvIns->iInstance);
1774
1775 /* interrupt */
1776 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatUrbsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of submitted URBs.", "/VUSB/%d/Intr/Urbs", pDrvIns->iInstance);
1777 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatUrbsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of failed URBs.", "/VUSB/%d/Intr/UrbsFailed", pDrvIns->iInstance);
1778 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatUrbsCancelled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of cancelled URBs.", "/VUSB/%d/Intr/UrbsFailed/Cancelled", pDrvIns->iInstance);
1779 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatActBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of bytes transferred.", "/VUSB/%d/Intr/ActBytes", pDrvIns->iInstance);
1780 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatActReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Read.", "/VUSB/%d/Intr/ActBytes/Read", pDrvIns->iInstance);
1781 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatActWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Write.", "/VUSB/%d/Intr/ActBytes/Write", pDrvIns->iInstance);
1782 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatReqBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Requested number of bytes.", "/VUSB/%d/Intr/ReqBytes", pDrvIns->iInstance);
1783 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatReqReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Read.", "/VUSB/%d/Intr/ReqBytes/Read", pDrvIns->iInstance);
1784 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_INTR].StatReqWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Write.", "/VUSB/%d/Intr/ReqBytes/Write", pDrvIns->iInstance);
1785
1786 /* isochronous */
1787 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatUrbsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of submitted URBs.", "/VUSB/%d/Isoc/Urbs", pDrvIns->iInstance);
1788 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatUrbsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of failed URBs.", "/VUSB/%d/Isoc/UrbsFailed", pDrvIns->iInstance);
1789 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatUrbsCancelled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of cancelled URBs.", "/VUSB/%d/Isoc/UrbsFailed/Cancelled", pDrvIns->iInstance);
1790 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatActBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of bytes transferred.", "/VUSB/%d/Isoc/ActBytes", pDrvIns->iInstance);
1791 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatActReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Read.", "/VUSB/%d/Isoc/ActBytes/Read", pDrvIns->iInstance);
1792 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatActWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Write.", "/VUSB/%d/Isoc/ActBytes/Write", pDrvIns->iInstance);
1793 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatReqBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Requested number of bytes.", "/VUSB/%d/Isoc/ReqBytes", pDrvIns->iInstance);
1794 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatReqReadBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Read.", "/VUSB/%d/Isoc/ReqBytes/Read", pDrvIns->iInstance);
1795 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aTypes[VUSBXFERTYPE_ISOC].StatReqWriteBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Write.", "/VUSB/%d/Isoc/ReqBytes/Write", pDrvIns->iInstance);
1796 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatIsocActPkts, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of isochronous packets returning data.", "/VUSB/%d/Isoc/ActPkts", pDrvIns->iInstance);
1797 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatIsocActReadPkts, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Read.", "/VUSB/%d/Isoc/ActPkts/Read", pDrvIns->iInstance);
1798 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatIsocActWritePkts, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Write.", "/VUSB/%d/Isoc/ActPkts/Write", pDrvIns->iInstance);
1799 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatIsocReqPkts, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Requested number of isochronous packets.", "/VUSB/%d/Isoc/ReqPkts", pDrvIns->iInstance);
1800 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatIsocReqReadPkts, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Read.", "/VUSB/%d/Isoc/ReqPkts/Read", pDrvIns->iInstance);
1801 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatIsocReqWritePkts, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Write.", "/VUSB/%d/Isoc/ReqPkts/Write", pDrvIns->iInstance);
1802
1803 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aStatIsocDetails); i++)
1804 {
1805 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aStatIsocDetails[i].Pkts, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT, ".", "/VUSB/%d/Isoc/%d", pDrvIns->iInstance, i);
1806 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aStatIsocDetails[i].Ok, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT, ".", "/VUSB/%d/Isoc/%d/Ok", pDrvIns->iInstance, i);
1807 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aStatIsocDetails[i].Ok0, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT, ".", "/VUSB/%d/Isoc/%d/Ok0", pDrvIns->iInstance, i);
1808 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aStatIsocDetails[i].DataUnderrun, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT, ".", "/VUSB/%d/Isoc/%d/DataUnderrun", pDrvIns->iInstance, i);
1809 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aStatIsocDetails[i].DataUnderrun0, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT, ".", "/VUSB/%d/Isoc/%d/DataUnderrun0", pDrvIns->iInstance, i);
1810 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aStatIsocDetails[i].DataOverrun, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT, ".", "/VUSB/%d/Isoc/%d/DataOverrun", pDrvIns->iInstance, i);
1811 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aStatIsocDetails[i].NotAccessed, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT, ".", "/VUSB/%d/Isoc/%d/NotAccessed", pDrvIns->iInstance, i);
1812 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aStatIsocDetails[i].Misc, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT, ".", "/VUSB/%d/Isoc/%d/Misc", pDrvIns->iInstance, i);
1813 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->aStatIsocDetails[i].Bytes, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, ".", "/VUSB/%d/Isoc/%d/Bytes", pDrvIns->iInstance, i);
1814 }
1815
1816 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReapAsyncUrbs, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling the vusbRhReapAsyncUrbs body (omitting calls when nothing is in-flight).",
1817 "/VUSB/%d/ReapAsyncUrbs", pDrvIns->iInstance);
1818 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatSubmitUrb, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling the vusbRhSubmitUrb body.",
1819 "/VUSB/%d/SubmitUrb", pDrvIns->iInstance);
1820 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatFramesProcessedThread, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Processed frames in the dedicated thread",
1821 "/VUSB/%d/FramesProcessedThread", pDrvIns->iInstance);
1822 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatFramesProcessedClbk, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Processed frames in the URB completion callback",
1823 "/VUSB/%d/FramesProcessedClbk", pDrvIns->iInstance);
1824#endif
1825 PDMDrvHlpSTAMRegisterF(pDrvIns, (void *)&pThis->Hub.Dev.UrbPool.cUrbsInPool, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "The number of URBs in the pool.",
1826 "/VUSB/%d/cUrbsInPool", pDrvIns->iInstance);
1827
1828 return VINF_SUCCESS;
1829}
1830
1831
1832/**
1833 * VUSB Root Hub driver registration record.
1834 */
1835const PDMDRVREG g_DrvVUSBRootHub =
1836{
1837 /* u32Version */
1838 PDM_DRVREG_VERSION,
1839 /* szName */
1840 "VUSBRootHub",
1841 /* szRCMod */
1842 "",
1843 /* szR0Mod */
1844 "",
1845 /* pszDescription */
1846 "VUSB Root Hub Driver.",
1847 /* fFlags */
1848 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1849 /* fClass. */
1850 PDM_DRVREG_CLASS_USB,
1851 /* cMaxInstances */
1852 ~0U,
1853 /* cbInstance */
1854 sizeof(VUSBROOTHUB),
1855 /* pfnConstruct */
1856 vusbRhConstruct,
1857 /* pfnDestruct */
1858 vusbRhDestruct,
1859 /* pfnRelocate */
1860 NULL,
1861 /* pfnIOCtl */
1862 NULL,
1863 /* pfnPowerOn */
1864 NULL,
1865 /* pfnReset */
1866 NULL,
1867 /* pfnSuspend */
1868 NULL,
1869 /* pfnResume */
1870 NULL,
1871 /* pfnAttach */
1872 NULL,
1873 /* pfnDetach */
1874 NULL,
1875 /* pfnPowerOff */
1876 NULL,
1877 /* pfnSoftReset */
1878 NULL,
1879 /* u32EndVersion */
1880 PDM_DRVREG_VERSION
1881};
1882
1883/*
1884 * Local Variables:
1885 * mode: c
1886 * c-file-style: "bsd"
1887 * c-basic-offset: 4
1888 * tab-width: 4
1889 * indent-tabs-mode: s
1890 * End:
1891 */
1892
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