VirtualBox

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

Last change on this file was 108094, checked in by vboxsync, 12 days ago

USB/RootHub: bugref:10779 Re-attach VUSB devices on resume, prevent double detaching on poweroff after save.

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