VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMUsb.cpp@ 66651

Last change on this file since 66651 was 66467, checked in by vboxsync, 8 years ago

pdmR3UsbCreateDevice: Translate VERR_VERSION_MISMATCH to VERR_PDM_USBDEV_VERSION_MISMATCH instead of VERR_PDM_DRIVER_VERSION_MISMATCH (copy & paste?).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 70.6 KB
Line 
1/* $Id: PDMUsb.cpp 66467 2017-04-07 07:54:56Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device and Driver Manager, USB part.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_PDM_DRIVER
23#include "PDMInternal.h"
24#include <VBox/vmm/pdm.h>
25#include <VBox/vusb.h>
26#include <VBox/vmm/mm.h>
27#include <VBox/vmm/cfgm.h>
28#include <VBox/vmm/vmm.h>
29#include <VBox/sup.h>
30#include <VBox/vmm/vm.h>
31#include <VBox/vmm/uvm.h>
32#include <VBox/version.h>
33#include <VBox/err.h>
34
35#include <VBox/log.h>
36#include <iprt/assert.h>
37#include <iprt/thread.h>
38#include <iprt/string.h>
39#include <iprt/asm.h>
40#include <iprt/alloc.h>
41#include <iprt/alloca.h>
42#include <iprt/path.h>
43#include <iprt/uuid.h>
44
45
46/*********************************************************************************************************************************
47* Structures and Typedefs *
48*********************************************************************************************************************************/
49/**
50 * Internal callback structure pointer.
51 *
52 * The main purpose is to define the extra data we associate
53 * with PDMUSBREGCB so we can find the VM instance and so on.
54 */
55typedef struct PDMUSBREGCBINT
56{
57 /** The callback structure. */
58 PDMUSBREGCB Core;
59 /** A bit of padding. */
60 uint32_t u32[4];
61 /** VM Handle. */
62 PVM pVM;
63} PDMUSBREGCBINT, *PPDMUSBREGCBINT;
64typedef const PDMUSBREGCBINT *PCPDMUSBREGCBINT;
65
66
67/*********************************************************************************************************************************
68* Defined Constants And Macros *
69*********************************************************************************************************************************/
70/** @def PDMUSB_ASSERT_USBINS
71 * Asserts the validity of the USB device instance.
72 */
73#ifdef VBOX_STRICT
74# define PDMUSB_ASSERT_USBINS(pUsbIns) \
75 do { \
76 AssertPtr(pUsbIns); \
77 Assert(pUsbIns->u32Version == PDM_USBINS_VERSION); \
78 Assert(pUsbIns->pvInstanceDataR3 == (void *)&pUsbIns->achInstanceData[0]); \
79 } while (0)
80#else
81# define PDMUSB_ASSERT_USBINS(pUsbIns) do { } while (0)
82#endif
83
84
85/*********************************************************************************************************************************
86* Internal Functions *
87*********************************************************************************************************************************/
88static void pdmR3UsbDestroyDevice(PVM pVM, PPDMUSBINS pUsbIns);
89
90
91/*********************************************************************************************************************************
92* Global Variables *
93*********************************************************************************************************************************/
94extern const PDMUSBHLP g_pdmR3UsbHlp;
95
96
97AssertCompile(sizeof(PDMUSBINSINT) <= RT_SIZEOFMEMB(PDMUSBINS, Internal.padding));
98
99
100/**
101 * Registers a USB hub driver.
102 *
103 * @returns VBox status code.
104 * @param pVM The cross context VM structure.
105 * @param pDrvIns The driver instance of the hub.
106 * @param fVersions Indicates the kinds of USB devices that can be attached to this HUB.
107 * @param cPorts The number of ports.
108 * @param pUsbHubReg The hub callback structure that PDMUsb uses to interact with it.
109 * @param ppUsbHubHlp The helper callback structure that the hub uses to talk to PDMUsb.
110 * @thread EMT
111 */
112int pdmR3UsbRegisterHub(PVM pVM, PPDMDRVINS pDrvIns, uint32_t fVersions, uint32_t cPorts, PCPDMUSBHUBREG pUsbHubReg, PPCPDMUSBHUBHLP ppUsbHubHlp)
113{
114 /*
115 * Validate input.
116 */
117 /* The driver must be in the USB class. */
118 if (!(pDrvIns->pReg->fClass & PDM_DRVREG_CLASS_USB))
119 {
120 LogRel(("PDMUsb: pdmR3UsbRegisterHub: fClass=%#x expected %#x to be set\n", pDrvIns->pReg->fClass, PDM_DRVREG_CLASS_USB));
121 return VERR_INVALID_PARAMETER;
122 }
123 AssertMsgReturn(!(fVersions & ~(VUSB_STDVER_11 | VUSB_STDVER_20 | VUSB_STDVER_30)), ("%#x\n", fVersions), VERR_INVALID_PARAMETER);
124 AssertPtrReturn(ppUsbHubHlp, VERR_INVALID_POINTER);
125 AssertPtrReturn(pUsbHubReg, VERR_INVALID_POINTER);
126 AssertReturn(pUsbHubReg->u32Version == PDM_USBHUBREG_VERSION, VERR_INVALID_MAGIC);
127 AssertReturn(pUsbHubReg->u32TheEnd == PDM_USBHUBREG_VERSION, VERR_INVALID_MAGIC);
128 AssertPtrReturn(pUsbHubReg->pfnAttachDevice, VERR_INVALID_PARAMETER);
129 AssertPtrReturn(pUsbHubReg->pfnDetachDevice, VERR_INVALID_PARAMETER);
130
131 /*
132 * Check for duplicate registration and find the last hub for FIFO registration.
133 */
134 PPDMUSBHUB pPrev = NULL;
135 for (PPDMUSBHUB pCur = pVM->pdm.s.pUsbHubs; pCur; pCur = pCur->pNext)
136 {
137 if (pCur->pDrvIns == pDrvIns)
138 return VERR_PDM_USB_HUB_EXISTS;
139 pPrev = pCur;
140 }
141
142 /*
143 * Create an internal USB hub structure.
144 */
145 PPDMUSBHUB pHub = (PPDMUSBHUB)MMR3HeapAlloc(pVM, MM_TAG_PDM_DRIVER, sizeof(*pHub));
146 if (!pHub)
147 return VERR_NO_MEMORY;
148
149 pHub->fVersions = fVersions;
150 pHub->cPorts = cPorts;
151 pHub->cAvailablePorts = cPorts;
152 pHub->pDrvIns = pDrvIns;
153 pHub->Reg = *pUsbHubReg;
154 pHub->pNext = NULL;
155
156 /* link it */
157 if (pPrev)
158 pPrev->pNext = pHub;
159 else
160 pVM->pdm.s.pUsbHubs = pHub;
161
162 Log(("PDM: Registered USB hub %p/%s\n", pDrvIns, pDrvIns->pReg->szName));
163 return VINF_SUCCESS;
164}
165
166
167/**
168 * Loads one device module and call the registration entry point.
169 *
170 * @returns VBox status code.
171 * @param pVM The cross context VM structure.
172 * @param pRegCB The registration callback stuff.
173 * @param pszFilename Module filename.
174 * @param pszName Module name.
175 */
176static int pdmR3UsbLoad(PVM pVM, PCPDMUSBREGCBINT pRegCB, const char *pszFilename, const char *pszName)
177{
178 /*
179 * Load it.
180 */
181 int rc = pdmR3LoadR3U(pVM->pUVM, pszFilename, pszName);
182 if (RT_SUCCESS(rc))
183 {
184 /*
185 * Get the registration export and call it.
186 */
187 FNPDMVBOXUSBREGISTER *pfnVBoxUsbRegister;
188 rc = PDMR3LdrGetSymbolR3(pVM, pszName, "VBoxUsbRegister", (void **)&pfnVBoxUsbRegister);
189 if (RT_SUCCESS(rc))
190 {
191 Log(("PDM: Calling VBoxUsbRegister (%p) of %s (%s)\n", pfnVBoxUsbRegister, pszName, pszFilename));
192 rc = pfnVBoxUsbRegister(&pRegCB->Core, VBOX_VERSION);
193 if (RT_SUCCESS(rc))
194 Log(("PDM: Successfully loaded device module %s (%s).\n", pszName, pszFilename));
195 else
196 AssertMsgFailed(("VBoxDevicesRegister failed with rc=%Rrc for module %s (%s)\n", rc, pszName, pszFilename));
197 }
198 else
199 {
200 AssertMsgFailed(("Failed to locate 'VBoxUsbRegister' in %s (%s) rc=%Rrc\n", pszName, pszFilename, rc));
201 if (rc == VERR_SYMBOL_NOT_FOUND)
202 rc = VERR_PDM_NO_REGISTRATION_EXPORT;
203 }
204 }
205 else
206 AssertMsgFailed(("Failed to load VBoxDD!\n"));
207 return rc;
208}
209
210
211
212/**
213 * @interface_method_impl{PDMUSBREGCB,pfnRegister}
214 */
215static DECLCALLBACK(int) pdmR3UsbReg_Register(PCPDMUSBREGCB pCallbacks, PCPDMUSBREG pReg)
216{
217 /*
218 * Validate the registration structure.
219 */
220 Assert(pReg);
221 AssertMsgReturn(pReg->u32Version == PDM_USBREG_VERSION,
222 ("Unknown struct version %#x!\n", pReg->u32Version),
223 VERR_PDM_UNKNOWN_USBREG_VERSION);
224 AssertMsgReturn( pReg->szName[0]
225 && strlen(pReg->szName) < sizeof(pReg->szName)
226 && pdmR3IsValidName(pReg->szName),
227 ("Invalid name '%.*s'\n", sizeof(pReg->szName), pReg->szName),
228 VERR_PDM_INVALID_USB_REGISTRATION);
229 AssertMsgReturn((pReg->fFlags & ~(PDM_USBREG_HIGHSPEED_CAPABLE | PDM_USBREG_SUPERSPEED_CAPABLE | PDM_USBREG_SAVED_STATE_SUPPORTED)) == 0,
230 ("fFlags=%#x\n", pReg->fFlags), VERR_PDM_INVALID_USB_REGISTRATION);
231 AssertMsgReturn(pReg->cMaxInstances > 0,
232 ("Max instances %u! (USB Device %s)\n", pReg->cMaxInstances, pReg->szName),
233 VERR_PDM_INVALID_USB_REGISTRATION);
234 AssertMsgReturn(pReg->cbInstance <= _1M,
235 ("Instance size %d bytes! (USB Device %s)\n", pReg->cbInstance, pReg->szName),
236 VERR_PDM_INVALID_USB_REGISTRATION);
237 AssertMsgReturn(pReg->pfnConstruct, ("No constructor! (USB Device %s)\n", pReg->szName),
238 VERR_PDM_INVALID_USB_REGISTRATION);
239
240 /*
241 * Check for duplicate and find FIFO entry at the same time.
242 */
243 PCPDMUSBREGCBINT pRegCB = (PCPDMUSBREGCBINT)pCallbacks;
244 PPDMUSB pUsbPrev = NULL;
245 PPDMUSB pUsb = pRegCB->pVM->pdm.s.pUsbDevs;
246 for (; pUsb; pUsbPrev = pUsb, pUsb = pUsb->pNext)
247 AssertMsgReturn(strcmp(pUsb->pReg->szName, pReg->szName),
248 ("USB Device '%s' already exists\n", pReg->szName),
249 VERR_PDM_USB_NAME_CLASH);
250
251 /*
252 * Allocate new device structure and insert it into the list.
253 */
254 pUsb = (PPDMUSB)MMR3HeapAlloc(pRegCB->pVM, MM_TAG_PDM_DEVICE, sizeof(*pUsb));
255 if (pUsb)
256 {
257 pUsb->pNext = NULL;
258 pUsb->iNextInstance = 0;
259 pUsb->pInstances = NULL;
260 pUsb->pReg = pReg;
261 pUsb->cchName = (RTUINT)strlen(pReg->szName);
262
263 if (pUsbPrev)
264 pUsbPrev->pNext = pUsb;
265 else
266 pRegCB->pVM->pdm.s.pUsbDevs = pUsb;
267 Log(("PDM: Registered USB device '%s'\n", pReg->szName));
268 return VINF_SUCCESS;
269 }
270 return VERR_NO_MEMORY;
271}
272
273
274/**
275 * Load USB Device modules.
276 *
277 * This is called by pdmR3DevInit() after it has loaded it's device modules.
278 *
279 * @returns VBox status code.
280 * @param pVM The cross context VM structure.
281 */
282int pdmR3UsbLoadModules(PVM pVM)
283{
284 LogFlow(("pdmR3UsbLoadModules:\n"));
285
286 AssertRelease(!(RT_OFFSETOF(PDMUSBINS, achInstanceData) & 15));
287 AssertRelease(sizeof(pVM->pdm.s.pUsbInstances->Internal.s) <= sizeof(pVM->pdm.s.pUsbInstances->Internal.padding));
288
289 /*
290 * Initialize the callback structure.
291 */
292 PDMUSBREGCBINT RegCB;
293 RegCB.Core.u32Version = PDM_USBREG_CB_VERSION;
294 RegCB.Core.pfnRegister = pdmR3UsbReg_Register;
295 RegCB.pVM = pVM;
296
297 /*
298 * Load the builtin module
299 */
300 PCFGMNODE pUsbNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "PDM/USB/");
301 bool fLoadBuiltin;
302 int rc = CFGMR3QueryBool(pUsbNode, "LoadBuiltin", &fLoadBuiltin);
303 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
304 fLoadBuiltin = true;
305 else if (RT_FAILURE(rc))
306 {
307 AssertMsgFailed(("Configuration error: Querying boolean \"LoadBuiltin\" failed with %Rrc\n", rc));
308 return rc;
309 }
310 if (fLoadBuiltin)
311 {
312 /* make filename */
313 char *pszFilename = pdmR3FileR3("VBoxDD", true /*fShared*/);
314 if (!pszFilename)
315 return VERR_NO_TMP_MEMORY;
316 rc = pdmR3UsbLoad(pVM, &RegCB, pszFilename, "VBoxDD");
317 RTMemTmpFree(pszFilename);
318 if (RT_FAILURE(rc))
319 return rc;
320 }
321
322 /*
323 * Load additional device modules.
324 */
325 PCFGMNODE pCur;
326 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
327 {
328 /*
329 * Get the name and path.
330 */
331 char szName[PDMMOD_NAME_LEN];
332 rc = CFGMR3GetName(pCur, &szName[0], sizeof(szName));
333 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
334 {
335 AssertMsgFailed(("configuration error: The module name is too long, cchName=%zu.\n", CFGMR3GetNameLen(pCur)));
336 return VERR_PDM_MODULE_NAME_TOO_LONG;
337 }
338 else if (RT_FAILURE(rc))
339 {
340 AssertMsgFailed(("CFGMR3GetName -> %Rrc.\n", rc));
341 return rc;
342 }
343
344 /* the path is optional, if no path the module name + path is used. */
345 char szFilename[RTPATH_MAX];
346 rc = CFGMR3QueryString(pCur, "Path", &szFilename[0], sizeof(szFilename));
347 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
348 strcpy(szFilename, szName);
349 else if (RT_FAILURE(rc))
350 {
351 AssertMsgFailed(("configuration error: Failure to query the module path, rc=%Rrc.\n", rc));
352 return rc;
353 }
354
355 /* prepend path? */
356 if (!RTPathHavePath(szFilename))
357 {
358 char *psz = pdmR3FileR3(szFilename, false /*fShared*/);
359 if (!psz)
360 return VERR_NO_TMP_MEMORY;
361 size_t cch = strlen(psz) + 1;
362 if (cch > sizeof(szFilename))
363 {
364 RTMemTmpFree(psz);
365 AssertMsgFailed(("Filename too long! cch=%d '%s'\n", cch, psz));
366 return VERR_FILENAME_TOO_LONG;
367 }
368 memcpy(szFilename, psz, cch);
369 RTMemTmpFree(psz);
370 }
371
372 /*
373 * Load the module and register it's devices.
374 */
375 rc = pdmR3UsbLoad(pVM, &RegCB, szFilename, szName);
376 if (RT_FAILURE(rc))
377 return rc;
378 }
379
380 return VINF_SUCCESS;
381}
382
383
384/**
385 * Send the init-complete notification to all the USB devices.
386 *
387 * This is called from pdmR3DevInit() after it has do its notification round.
388 *
389 * @returns VBox status code.
390 * @param pVM The cross context VM structure.
391 */
392int pdmR3UsbVMInitComplete(PVM pVM)
393{
394 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
395 {
396 if (pUsbIns->pReg->pfnVMInitComplete)
397 {
398 int rc = pUsbIns->pReg->pfnVMInitComplete(pUsbIns);
399 if (RT_FAILURE(rc))
400 {
401 AssertMsgFailed(("InitComplete on USB device '%s'/%d failed with rc=%Rrc\n",
402 pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
403 return rc;
404 }
405 }
406 }
407 return VINF_SUCCESS;
408}
409
410
411/**
412 * Lookups a device structure by name.
413 * @internal
414 */
415PPDMUSB pdmR3UsbLookup(PVM pVM, const char *pszName)
416{
417 size_t cchName = strlen(pszName);
418 for (PPDMUSB pUsb = pVM->pdm.s.pUsbDevs; pUsb; pUsb = pUsb->pNext)
419 if ( pUsb->cchName == cchName
420 && !strcmp(pUsb->pReg->szName, pszName))
421 return pUsb;
422 return NULL;
423}
424
425
426/**
427 * Locates a suitable hub for the specified kind of device.
428 *
429 * @returns VINF_SUCCESS and *ppHub on success.
430 * VERR_PDM_NO_USB_HUBS or VERR_PDM_NO_USB_PORTS on failure.
431 * @param pVM The cross context VM structure.
432 * @param iUsbVersion The USB device version.
433 * @param ppHub Where to store the pointer to the USB hub.
434 */
435static int pdmR3UsbFindHub(PVM pVM, uint32_t iUsbVersion, PPDMUSBHUB *ppHub)
436{
437 *ppHub = NULL;
438 if (!pVM->pdm.s.pUsbHubs)
439 return VERR_PDM_NO_USB_HUBS;
440
441 for (PPDMUSBHUB pCur = pVM->pdm.s.pUsbHubs; pCur; pCur = pCur->pNext)
442 if (pCur->cAvailablePorts > 0)
443 {
444 /* First check for an exact match. */
445 if (pCur->fVersions & iUsbVersion)
446 {
447 *ppHub = pCur;
448 break;
449 }
450 /* For high-speed USB 2.0 devices only, allow USB 1.1 fallback. */
451 if ((iUsbVersion & VUSB_STDVER_20) && (pCur->fVersions == VUSB_STDVER_11))
452 *ppHub = pCur;
453 }
454 if (*ppHub)
455 return VINF_SUCCESS;
456 return VERR_PDM_NO_USB_PORTS;
457}
458
459
460/**
461 * Translates a USB vesion (a bit-mask) to USB speed (enum). Picks
462 * the highest available version.
463 *
464 * @returns VUSBSPEED enum
465 *
466 * @param iUsbVersion The USB version.
467 *
468 */
469static VUSBSPEED pdmR3UsbVer2Spd(uint32_t iUsbVersion)
470{
471 VUSBSPEED enmSpd = VUSB_SPEED_UNKNOWN;
472 Assert(iUsbVersion);
473
474 if (iUsbVersion & VUSB_STDVER_30)
475 enmSpd = VUSB_SPEED_SUPER;
476 else if (iUsbVersion & VUSB_STDVER_20)
477 enmSpd = VUSB_SPEED_HIGH;
478 else if (iUsbVersion & VUSB_STDVER_11)
479 enmSpd = VUSB_SPEED_FULL; /* Can't distinguish LS vs. FS. */
480
481 return enmSpd;
482}
483
484
485/**
486 * Creates the device.
487 *
488 * @returns VBox status code.
489 * @param pVM The cross context VM structure.
490 * @param pHub The USB hub it'll be attached to.
491 * @param pUsbDev The USB device emulation.
492 * @param iInstance -1 if not called by pdmR3UsbInstantiateDevices().
493 * @param pUuid The UUID for this device.
494 * @param ppInstanceNode Pointer to the device instance pointer. This is set to NULL if inserted
495 * into the tree or cleaned up.
496 *
497 * In the pdmR3UsbInstantiateDevices() case (iInstance != -1) this is
498 * the actual instance node and will not be cleaned up.
499 *
500 * @param enmSpeed The speed the USB device is operating at.
501 * @param pszCaptureFilename Path to the file for USB traffic capturing, optional.
502 */
503static int pdmR3UsbCreateDevice(PVM pVM, PPDMUSBHUB pHub, PPDMUSB pUsbDev, int iInstance, PCRTUUID pUuid,
504 PCFGMNODE *ppInstanceNode, VUSBSPEED enmSpeed, const char *pszCaptureFilename)
505{
506 const bool fAtRuntime = iInstance == -1;
507 int rc;
508
509 AssertPtrReturn(ppInstanceNode, VERR_INVALID_POINTER);
510 AssertPtrReturn(*ppInstanceNode, VERR_INVALID_POINTER);
511
512 /*
513 * If not called by pdmR3UsbInstantiateDevices(), we'll have to fix
514 * the configuration now.
515 */
516 /* USB device node. */
517 PCFGMNODE pDevNode = CFGMR3GetChildF(CFGMR3GetRoot(pVM), "USB/%s/", pUsbDev->pReg->szName);
518 if (!pDevNode)
519 {
520 rc = CFGMR3InsertNodeF(CFGMR3GetRoot(pVM), &pDevNode, "USB/%s/", pUsbDev->pReg->szName);
521 AssertRCReturn(rc, rc);
522 }
523
524 /* The instance node and number. */
525 PCFGMNODE pInstanceToDelete = NULL;
526 PCFGMNODE pInstanceNode = NULL;
527 if (fAtRuntime)
528 {
529 /** @todo r=bird: This code is bogus as it ASSUMES that all USB devices are
530 * capable of infinite number of instances. */
531 rc = VINF_SUCCESS; /* Shut up stupid incorrect uninitialized warning from Visual C++ 2010. */
532 for (unsigned c = 0; c < _2M; c++)
533 {
534 iInstance = pUsbDev->iNextInstance++;
535 rc = CFGMR3InsertNodeF(pDevNode, &pInstanceNode, "%d/", iInstance);
536 if (rc != VERR_CFGM_NODE_EXISTS)
537 break;
538 }
539 AssertRCReturn(rc, rc);
540
541 rc = CFGMR3ReplaceSubTree(pInstanceNode, *ppInstanceNode);
542 AssertRCReturn(rc, rc);
543 *ppInstanceNode = NULL;
544 pInstanceToDelete = pInstanceNode;
545 }
546 else
547 {
548 Assert(iInstance >= 0);
549 if (iInstance >= (int)pUsbDev->iNextInstance)
550 pUsbDev->iNextInstance = iInstance + 1;
551 pInstanceNode = *ppInstanceNode;
552 }
553
554 /* Make sure the instance config node exists. */
555 PCFGMNODE pConfig = CFGMR3GetChild(pInstanceNode, "Config");
556 if (!pConfig)
557 {
558 rc = CFGMR3InsertNode(pInstanceNode, "Config", &pConfig);
559 AssertRCReturn(rc, rc);
560 }
561 Assert(CFGMR3GetChild(pInstanceNode, "Config") == pConfig);
562
563 /* The global device config node. */
564 PCFGMNODE pGlobalConfig = CFGMR3GetChild(pDevNode, "GlobalConfig");
565 if (!pGlobalConfig)
566 {
567 rc = CFGMR3InsertNode(pDevNode, "GlobalConfig", &pGlobalConfig);
568 if (RT_FAILURE(rc))
569 {
570 CFGMR3RemoveNode(pInstanceToDelete);
571 AssertRCReturn(rc, rc);
572 }
573 }
574
575 /*
576 * Allocate the device instance.
577 */
578 size_t cb = RT_OFFSETOF(PDMUSBINS, achInstanceData[pUsbDev->pReg->cbInstance]);
579 cb = RT_ALIGN_Z(cb, 16);
580 PPDMUSBINS pUsbIns;
581 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_USB, cb, (void **)&pUsbIns);
582 if (RT_FAILURE(rc))
583 {
584 AssertMsgFailed(("Failed to allocate %d bytes of instance data for USB device '%s'. rc=%Rrc\n",
585 cb, pUsbDev->pReg->szName, rc));
586 CFGMR3RemoveNode(pInstanceToDelete);
587 return rc;
588 }
589
590 /*
591 * Initialize it.
592 */
593 pUsbIns->u32Version = PDM_USBINS_VERSION;
594 //pUsbIns->Internal.s.pNext = NULL;
595 //pUsbIns->Internal.s.pPerDeviceNext = NULL;
596 pUsbIns->Internal.s.pUsbDev = pUsbDev;
597 pUsbIns->Internal.s.pVM = pVM;
598 //pUsbIns->Internal.s.pLuns = NULL;
599 pUsbIns->Internal.s.pCfg = pInstanceNode;
600 pUsbIns->Internal.s.pCfgDelete = pInstanceToDelete;
601 pUsbIns->Internal.s.pCfgGlobal = pGlobalConfig;
602 pUsbIns->Internal.s.Uuid = *pUuid;
603 //pUsbIns->Internal.s.pHub = NULL;
604 pUsbIns->Internal.s.iPort = UINT32_MAX; /* to be determined. */
605 /* Set the flag accordingly.
606 * Otherwise VMPowerOff, VMSuspend will not be called for devices attached at runtime.
607 */
608 pUsbIns->Internal.s.fVMSuspended = !fAtRuntime;
609 //pUsbIns->Internal.s.pfnAsyncNotify = NULL;
610 pUsbIns->pHlpR3 = &g_pdmR3UsbHlp;
611 pUsbIns->pReg = pUsbDev->pReg;
612 pUsbIns->pCfg = pConfig;
613 pUsbIns->pCfgGlobal = pGlobalConfig;
614 pUsbIns->iInstance = iInstance;
615 pUsbIns->pvInstanceDataR3 = &pUsbIns->achInstanceData[0];
616 pUsbIns->pszName = RTStrDup(pUsbDev->pReg->szName);
617 //pUsbIns->fTracing = 0;
618 pUsbIns->idTracing = ++pVM->pdm.s.idTracingOther;
619 pUsbIns->enmSpeed = enmSpeed;
620
621 /*
622 * Link it into all the lists.
623 */
624 /* The global instance FIFO. */
625 PPDMUSBINS pPrev1 = pVM->pdm.s.pUsbInstances;
626 if (!pPrev1)
627 pVM->pdm.s.pUsbInstances = pUsbIns;
628 else
629 {
630 while (pPrev1->Internal.s.pNext)
631 {
632 Assert(pPrev1->u32Version == PDM_USBINS_VERSION);
633 pPrev1 = pPrev1->Internal.s.pNext;
634 }
635 pPrev1->Internal.s.pNext = pUsbIns;
636 }
637
638 /* The per device instance FIFO. */
639 PPDMUSBINS pPrev2 = pUsbDev->pInstances;
640 if (!pPrev2)
641 pUsbDev->pInstances = pUsbIns;
642 else
643 {
644 while (pPrev2->Internal.s.pPerDeviceNext)
645 {
646 Assert(pPrev2->u32Version == PDM_USBINS_VERSION);
647 pPrev2 = pPrev2->Internal.s.pPerDeviceNext;
648 }
649 pPrev2->Internal.s.pPerDeviceNext = pUsbIns;
650 }
651
652 /*
653 * Call the constructor.
654 */
655 Log(("PDM: Constructing USB device '%s' instance %d...\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
656 rc = pUsbIns->pReg->pfnConstruct(pUsbIns, pUsbIns->iInstance, pUsbIns->pCfg, pUsbIns->pCfgGlobal);
657 if (RT_SUCCESS(rc))
658 {
659 /*
660 * Attach it to the hub.
661 */
662 Log(("PDM: Attaching it...\n"));
663 rc = pHub->Reg.pfnAttachDevice(pHub->pDrvIns, pUsbIns, pszCaptureFilename, &pUsbIns->Internal.s.iPort);
664 if (RT_SUCCESS(rc))
665 {
666 pHub->cAvailablePorts--;
667 Assert((int32_t)pHub->cAvailablePorts >= 0 && pHub->cAvailablePorts < pHub->cPorts);
668 pUsbIns->Internal.s.pHub = pHub;
669
670 /* Send the hot-plugged notification if applicable. */
671 if (fAtRuntime && pUsbIns->pReg->pfnHotPlugged)
672 pUsbIns->pReg->pfnHotPlugged(pUsbIns);
673
674 Log(("PDM: Successfully attached USB device '%s' instance %d to hub %p\n",
675 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub));
676 return VINF_SUCCESS;
677 }
678
679 LogRel(("PDMUsb: Failed to attach USB device '%s' instance %d to hub %p: %Rrc\n",
680 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
681 }
682 else
683 {
684 AssertMsgFailed(("Failed to construct '%s'/%d! %Rra\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
685 if (rc == VERR_VERSION_MISMATCH)
686 rc = VERR_PDM_USBDEV_VERSION_MISMATCH;
687 }
688 if (fAtRuntime)
689 pdmR3UsbDestroyDevice(pVM, pUsbIns);
690 /* else: destructors are invoked later. */
691 return rc;
692}
693
694
695/**
696 * Instantiate USB devices.
697 *
698 * This is called by pdmR3DevInit() after it has instantiated the
699 * other devices and their drivers. If there aren't any hubs
700 * around, we'll silently skip the USB devices.
701 *
702 * @returns VBox status code.
703 * @param pVM The cross context VM structure.
704 */
705int pdmR3UsbInstantiateDevices(PVM pVM)
706{
707 /*
708 * Any hubs?
709 */
710 if (!pVM->pdm.s.pUsbHubs)
711 {
712 Log(("PDM: No USB hubs, skipping USB device instantiation.\n"));
713 return VINF_SUCCESS;
714 }
715
716 /*
717 * Count the device instances.
718 */
719 PCFGMNODE pCur;
720 PCFGMNODE pUsbNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "USB/");
721 PCFGMNODE pInstanceNode;
722 unsigned cUsbDevs = 0;
723 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
724 {
725 PCFGMNODE pGlobal = CFGMR3GetChild(pCur, "GlobalConfig/");
726 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
727 if (pInstanceNode != pGlobal)
728 cUsbDevs++;
729 }
730 if (!cUsbDevs)
731 {
732 Log(("PDM: No USB devices were configured!\n"));
733 return VINF_SUCCESS;
734 }
735 Log2(("PDM: cUsbDevs=%d!\n", cUsbDevs));
736
737 /*
738 * Collect info on each USB device instance.
739 */
740 struct USBDEVORDER
741 {
742 /** Configuration node. */
743 PCFGMNODE pNode;
744 /** Pointer to the USB device. */
745 PPDMUSB pUsbDev;
746 /** Init order. */
747 uint32_t u32Order;
748 /** VBox instance number. */
749 uint32_t iInstance;
750 /** Device UUID. */
751 RTUUID Uuid;
752 } *paUsbDevs = (struct USBDEVORDER *)alloca(sizeof(paUsbDevs[0]) * (cUsbDevs + 1)); /* (One extra for swapping) */
753 Assert(paUsbDevs);
754 int rc;
755 unsigned i = 0;
756 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
757 {
758 /* Get the device name. */
759 char szName[sizeof(paUsbDevs[0].pUsbDev->pReg->szName)];
760 rc = CFGMR3GetName(pCur, szName, sizeof(szName));
761 AssertMsgRCReturn(rc, ("Configuration error: device name is too long (or something)! rc=%Rrc\n", rc), rc);
762
763 /* Find the device. */
764 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, szName);
765 AssertMsgReturn(pUsbDev, ("Configuration error: device '%s' not found!\n", szName), VERR_PDM_DEVICE_NOT_FOUND);
766
767 /* Configured priority or use default? */
768 uint32_t u32Order;
769 rc = CFGMR3QueryU32(pCur, "Priority", &u32Order);
770 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
771 u32Order = i << 4;
772 else
773 AssertMsgRCReturn(rc, ("Configuration error: reading \"Priority\" for the '%s' USB device failed rc=%Rrc!\n", szName, rc), rc);
774
775 /* Global config. */
776 PCFGMNODE pGlobal = CFGMR3GetChild(pCur, "GlobalConfig/");
777 if (!pGlobal)
778 {
779 rc = CFGMR3InsertNode(pCur, "GlobalConfig/", &pGlobal);
780 AssertMsgRCReturn(rc, ("Failed to create GlobalConfig node! rc=%Rrc\n", rc), rc);
781 CFGMR3SetRestrictedRoot(pGlobal);
782 }
783
784 /* Enumerate the device instances. */
785 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
786 {
787 if (pInstanceNode == pGlobal)
788 continue;
789
790 /* Use the configured UUID if present, create our own otherwise. */
791 char *pszUuid = NULL;
792
793 RTUuidClear(&paUsbDevs[i].Uuid);
794 rc = CFGMR3QueryStringAlloc(pInstanceNode, "UUID", &pszUuid);
795 if (RT_SUCCESS(rc))
796 {
797 AssertPtr(pszUuid);
798
799 rc = RTUuidFromStr(&paUsbDevs[i].Uuid, pszUuid);
800 AssertMsgRCReturn(rc, ("Failed to convert UUID from string! rc=%Rrc\n", rc), rc);
801 MMR3HeapFree(pszUuid);
802 }
803 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
804 rc = RTUuidCreate(&paUsbDevs[i].Uuid);
805
806 AssertRCReturn(rc, rc);
807 paUsbDevs[i].pNode = pInstanceNode;
808 paUsbDevs[i].pUsbDev = pUsbDev;
809 paUsbDevs[i].u32Order = u32Order;
810
811 /* Get the instance number. */
812 char szInstance[32];
813 rc = CFGMR3GetName(pInstanceNode, szInstance, sizeof(szInstance));
814 AssertMsgRCReturn(rc, ("Configuration error: instance name is too long (or something)! rc=%Rrc\n", rc), rc);
815 char *pszNext = NULL;
816 rc = RTStrToUInt32Ex(szInstance, &pszNext, 0, &paUsbDevs[i].iInstance);
817 AssertMsgRCReturn(rc, ("Configuration error: RTStrToInt32Ex failed on the instance name '%s'! rc=%Rrc\n", szInstance, rc), rc);
818 AssertMsgReturn(!*pszNext, ("Configuration error: the instance name '%s' isn't all digits. (%s)\n", szInstance, pszNext), VERR_INVALID_PARAMETER);
819
820 /* next instance */
821 i++;
822 }
823 } /* devices */
824 Assert(i == cUsbDevs);
825
826 /*
827 * Sort the device array ascending on u32Order. (bubble)
828 */
829 unsigned c = cUsbDevs - 1;
830 while (c)
831 {
832 unsigned j = 0;
833 for (i = 0; i < c; i++)
834 if (paUsbDevs[i].u32Order > paUsbDevs[i + 1].u32Order)
835 {
836 paUsbDevs[cUsbDevs] = paUsbDevs[i + 1];
837 paUsbDevs[i + 1] = paUsbDevs[i];
838 paUsbDevs[i] = paUsbDevs[cUsbDevs];
839 j = i;
840 }
841 c = j;
842 }
843
844 /*
845 * Instantiate the devices.
846 */
847 for (i = 0; i < cUsbDevs; i++)
848 {
849 /*
850 * Make sure there is a config node and mark it as restricted.
851 */
852 PCFGMNODE pConfigNode = CFGMR3GetChild(paUsbDevs[i].pNode, "Config/");
853 if (!pConfigNode)
854 {
855 rc = CFGMR3InsertNode(paUsbDevs[i].pNode, "Config", &pConfigNode);
856 AssertMsgRCReturn(rc, ("Failed to create Config node! rc=%Rrc\n", rc), rc);
857 }
858 CFGMR3SetRestrictedRoot(pConfigNode);
859
860 /*
861 * Every emulated device must support USB 1.x hubs; optionally, high-speed USB 2.0 hubs
862 * might be also supported. This determines where to attach the device.
863 */
864 uint32_t iUsbVersion = VUSB_STDVER_11;
865
866 if (paUsbDevs[i].pUsbDev->pReg->fFlags & PDM_USBREG_HIGHSPEED_CAPABLE)
867 iUsbVersion |= VUSB_STDVER_20;
868 if (paUsbDevs[i].pUsbDev->pReg->fFlags & PDM_USBREG_SUPERSPEED_CAPABLE)
869 iUsbVersion |= VUSB_STDVER_30;
870
871 /*
872 * Find a suitable hub with free ports.
873 */
874 PPDMUSBHUB pHub;
875 rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
876 if (RT_FAILURE(rc))
877 {
878 Log(("pdmR3UsbFindHub failed %Rrc\n", rc));
879 return rc;
880 }
881
882 /*
883 * This is how we inform the device what speed it's communicating at, and hence
884 * which descriptors it should present to the guest.
885 */
886 iUsbVersion &= pHub->fVersions;
887
888 /*
889 * Create and attach the device.
890 */
891 rc = pdmR3UsbCreateDevice(pVM, pHub, paUsbDevs[i].pUsbDev, paUsbDevs[i].iInstance, &paUsbDevs[i].Uuid,
892 &paUsbDevs[i].pNode, pdmR3UsbVer2Spd(iUsbVersion), NULL);
893 if (RT_FAILURE(rc))
894 return rc;
895 } /* for device instances */
896
897 return VINF_SUCCESS;
898}
899
900
901/**
902 * Creates an emulated USB device instance at runtime.
903 *
904 * This will find an appropriate HUB for the USB device
905 * and try instantiate the emulated device.
906 *
907 * @returns VBox status code.
908 * @param pUVM The user mode VM handle.
909 * @param pszDeviceName The name of the PDM device to instantiate.
910 * @param pInstanceNode The instance CFGM node.
911 * @param pUuid The UUID to be associated with the device.
912 * @param pszCaptureFilename Path to the file for USB traffic capturing, optional.
913 *
914 * @thread EMT
915 */
916VMMR3DECL(int) PDMR3UsbCreateEmulatedDevice(PUVM pUVM, const char *pszDeviceName, PCFGMNODE pInstanceNode, PCRTUUID pUuid,
917 const char *pszCaptureFilename)
918{
919 /*
920 * Validate input.
921 */
922 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
923 PVM pVM = pUVM->pVM;
924 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
925 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
926 AssertPtrReturn(pszDeviceName, VERR_INVALID_POINTER);
927 AssertPtrReturn(pInstanceNode, VERR_INVALID_POINTER);
928
929 /*
930 * Find the device.
931 */
932 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, pszDeviceName);
933 if (!pUsbDev)
934 {
935 LogRel(("PDMUsb: PDMR3UsbCreateEmulatedDevice: The '%s' device wasn't found\n", pszDeviceName));
936 return VERR_PDM_NO_USBPROXY;
937 }
938
939 /*
940 * Every device must support USB 1.x hubs; optionally, high-speed USB 2.0 hubs
941 * might be also supported. This determines where to attach the device.
942 */
943 uint32_t iUsbVersion = VUSB_STDVER_11;
944 if (pUsbDev->pReg->fFlags & PDM_USBREG_HIGHSPEED_CAPABLE)
945 iUsbVersion |= VUSB_STDVER_20;
946 if (pUsbDev->pReg->fFlags & PDM_USBREG_SUPERSPEED_CAPABLE)
947 iUsbVersion |= VUSB_STDVER_30;
948
949 /*
950 * Find a suitable hub with free ports.
951 */
952 PPDMUSBHUB pHub;
953 int rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
954 if (RT_FAILURE(rc))
955 {
956 Log(("pdmR3UsbFindHub: failed %Rrc\n", rc));
957 return rc;
958 }
959
960 /*
961 * This is how we inform the device what speed it's communicating at, and hence
962 * which descriptors it should present to the guest.
963 */
964 iUsbVersion &= pHub->fVersions;
965
966 /*
967 * Create and attach the device.
968 */
969 rc = pdmR3UsbCreateDevice(pVM, pHub, pUsbDev, -1, pUuid, &pInstanceNode,
970 pdmR3UsbVer2Spd(iUsbVersion), pszCaptureFilename);
971 AssertRCReturn(rc, rc);
972
973 return rc;
974}
975
976
977/**
978 * Creates a USB proxy device instance.
979 *
980 * This will find an appropriate HUB for the USB device, create the necessary CFGM stuff
981 * and try instantiate the proxy device.
982 *
983 * @returns VBox status code.
984 * @param pUVM The user mode VM handle.
985 * @param pUuid The UUID to be associated with the device.
986 * @param pszBackend The proxy backend to use.
987 * @param pszAddress The address string.
988 * @param pvBackend Pointer to the backend.
989 * @param iUsbVersion The preferred USB version.
990 * @param fMaskedIfs The interfaces to hide from the guest.
991 * @param pszCaptureFilename Path to the file for USB traffic capturing, optional.
992 */
993VMMR3DECL(int) PDMR3UsbCreateProxyDevice(PUVM pUVM, PCRTUUID pUuid, const char *pszBackend, const char *pszAddress, void *pvBackend,
994 uint32_t iUsbVersion, uint32_t fMaskedIfs, const char *pszCaptureFilename)
995{
996 /*
997 * Validate input.
998 */
999 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1000 PVM pVM = pUVM->pVM;
1001 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1002 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
1003 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
1004 AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
1005 AssertReturn( iUsbVersion == VUSB_STDVER_30
1006 || iUsbVersion == VUSB_STDVER_20
1007 || iUsbVersion == VUSB_STDVER_11, VERR_INVALID_PARAMETER);
1008
1009 /*
1010 * Find the USBProxy driver.
1011 */
1012 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, "USBProxy");
1013 if (!pUsbDev)
1014 {
1015 LogRel(("PDMUsb: PDMR3UsbCreateProxyDevice: The USBProxy device class wasn't found\n"));
1016 return VERR_PDM_NO_USBPROXY;
1017 }
1018
1019 /*
1020 * Find a suitable hub with free ports.
1021 */
1022 PPDMUSBHUB pHub;
1023 int rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
1024 if (RT_FAILURE(rc))
1025 {
1026 Log(("pdmR3UsbFindHub: failed %Rrc\n", rc));
1027 return rc;
1028 }
1029
1030 /*
1031 * Create the CFGM instance node.
1032 */
1033 PCFGMNODE pInstance = CFGMR3CreateTree(pUVM);
1034 AssertReturn(pInstance, VERR_NO_MEMORY);
1035 do /* break loop */
1036 {
1037 PCFGMNODE pConfig;
1038 rc = CFGMR3InsertNode(pInstance, "Config", &pConfig); AssertRCBreak(rc);
1039 rc = CFGMR3InsertString(pConfig, "Address", pszAddress); AssertRCBreak(rc);
1040 char szUuid[RTUUID_STR_LENGTH];
1041 rc = RTUuidToStr(pUuid, &szUuid[0], sizeof(szUuid)); AssertRCBreak(rc);
1042 rc = CFGMR3InsertString(pConfig, "UUID", szUuid); AssertRCBreak(rc);
1043 rc = CFGMR3InsertString(pConfig, "Backend", pszBackend); AssertRCBreak(rc);
1044 rc = CFGMR3InsertInteger(pConfig, "USBVersion", iUsbVersion); AssertRCBreak(rc);
1045 rc = CFGMR3InsertInteger(pConfig, "pvBackend", (uintptr_t)pvBackend); AssertRCBreak(rc);
1046 rc = CFGMR3InsertInteger(pConfig, "MaskedIfs", fMaskedIfs); AssertRCBreak(rc);
1047 rc = CFGMR3InsertInteger(pConfig, "Force11Device", !(pHub->fVersions & iUsbVersion)); AssertRCBreak(rc);
1048 } while (0); /* break loop */
1049 if (RT_FAILURE(rc))
1050 {
1051 CFGMR3RemoveNode(pInstance);
1052 LogRel(("PDMUsb: PDMR3UsbCreateProxyDevice: failed to setup CFGM config, rc=%Rrc\n", rc));
1053 return rc;
1054 }
1055
1056 VUSBSPEED enmSpeed = pdmR3UsbVer2Spd(iUsbVersion);
1057
1058 /*
1059 * Finally, try to create it.
1060 */
1061 rc = pdmR3UsbCreateDevice(pVM, pHub, pUsbDev, -1, pUuid, &pInstance, enmSpeed, pszCaptureFilename);
1062 if (RT_FAILURE(rc) && pInstance)
1063 CFGMR3RemoveNode(pInstance);
1064 return rc;
1065}
1066
1067
1068/**
1069 * Destroys a hot-plugged USB device.
1070 *
1071 * The device must be detached from the HUB at this point.
1072 *
1073 * @param pVM The cross context VM structure.
1074 * @param pUsbIns The USB device instance to destroy.
1075 * @thread EMT
1076 */
1077static void pdmR3UsbDestroyDevice(PVM pVM, PPDMUSBINS pUsbIns)
1078{
1079 Assert(!pUsbIns->Internal.s.pHub);
1080
1081 /*
1082 * Do the unplug notification.
1083 */
1084 /** @todo what about the drivers? */
1085 if (pUsbIns->pReg->pfnHotUnplugged)
1086 pUsbIns->pReg->pfnHotUnplugged(pUsbIns);
1087
1088 /*
1089 * Destroy the luns with their driver chains and call the device destructor.
1090 */
1091 while (pUsbIns->Internal.s.pLuns)
1092 {
1093 PPDMLUN pLun = pUsbIns->Internal.s.pLuns;
1094 pUsbIns->Internal.s.pLuns = pLun->pNext;
1095 if (pLun->pTop)
1096 pdmR3DrvDestroyChain(pLun->pTop, PDM_TACH_FLAGS_NOT_HOT_PLUG); /* Hotplugging is handled differently here atm. */
1097 MMR3HeapFree(pLun);
1098 }
1099
1100 /* finally, the device. */
1101 if (pUsbIns->pReg->pfnDestruct)
1102 {
1103 Log(("PDM: Destructing USB device '%s' instance %d...\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1104 pUsbIns->pReg->pfnDestruct(pUsbIns);
1105 }
1106 TMR3TimerDestroyUsb(pVM, pUsbIns);
1107 SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
1108 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
1109#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
1110 pdmR3AsyncCompletionTemplateDestroyUsb(pVM, pUsbIns);
1111#endif
1112
1113 /*
1114 * Unlink it.
1115 */
1116 /* The global instance FIFO. */
1117 if (pVM->pdm.s.pUsbInstances == pUsbIns)
1118 pVM->pdm.s.pUsbInstances = pUsbIns->Internal.s.pNext;
1119 else
1120 {
1121 PPDMUSBINS pPrev = pVM->pdm.s.pUsbInstances;
1122 while (pPrev && pPrev->Internal.s.pNext != pUsbIns)
1123 {
1124 Assert(pPrev->u32Version == PDM_USBINS_VERSION);
1125 pPrev = pPrev->Internal.s.pNext;
1126 }
1127 Assert(pPrev); Assert(pPrev != pUsbIns);
1128 if (pPrev)
1129 pPrev->Internal.s.pNext = pUsbIns->Internal.s.pNext;
1130 }
1131
1132 /* The per device instance FIFO. */
1133 PPDMUSB pUsbDev = pUsbIns->Internal.s.pUsbDev;
1134 if (pUsbDev->pInstances == pUsbIns)
1135 pUsbDev->pInstances = pUsbIns->Internal.s.pPerDeviceNext;
1136 else
1137 {
1138 PPDMUSBINS pPrev = pUsbDev->pInstances;
1139 while (pPrev && pPrev->Internal.s.pPerDeviceNext != pUsbIns)
1140 {
1141 Assert(pPrev->u32Version == PDM_USBINS_VERSION);
1142 pPrev = pPrev->Internal.s.pPerDeviceNext;
1143 }
1144 Assert(pPrev); Assert(pPrev != pUsbIns);
1145 if (pPrev)
1146 pPrev->Internal.s.pPerDeviceNext = pUsbIns->Internal.s.pPerDeviceNext;
1147 }
1148
1149 /*
1150 * Trash it.
1151 */
1152 pUsbIns->u32Version = 0;
1153 pUsbIns->pReg = NULL;
1154 if (pUsbIns->pszName)
1155 {
1156 RTStrFree(pUsbIns->pszName);
1157 pUsbIns->pszName = NULL;
1158 }
1159 CFGMR3RemoveNode(pUsbIns->Internal.s.pCfgDelete);
1160 MMR3HeapFree(pUsbIns);
1161}
1162
1163
1164/**
1165 * Detaches and destroys a USB device.
1166 *
1167 * @returns VBox status code.
1168 * @param pUVM The user mode VM handle.
1169 * @param pUuid The UUID associated with the device to detach.
1170 * @thread EMT
1171 */
1172VMMR3DECL(int) PDMR3UsbDetachDevice(PUVM pUVM, PCRTUUID pUuid)
1173{
1174 /*
1175 * Validate input.
1176 */
1177 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1178 PVM pVM = pUVM->pVM;
1179 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1180 VM_ASSERT_EMT(pVM);
1181 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
1182
1183 /*
1184 * Search the global list for it.
1185 */
1186 PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances;
1187 for ( ; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1188 if (!RTUuidCompare(&pUsbIns->Internal.s.Uuid, pUuid))
1189 break;
1190 if (!pUsbIns)
1191 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND; /** @todo VERR_PDM_USB_INSTANCE_NOT_FOUND */
1192
1193 /*
1194 * Detach it from the HUB (if it's actually attached to one).
1195 */
1196 PPDMUSBHUB pHub = pUsbIns->Internal.s.pHub;
1197 if (pHub)
1198 {
1199 int rc = pHub->Reg.pfnDetachDevice(pHub->pDrvIns, pUsbIns, pUsbIns->Internal.s.iPort);
1200 if (RT_FAILURE(rc))
1201 {
1202 LogRel(("PDMUsb: Failed to detach USB device '%s' instance %d from %p: %Rrc\n",
1203 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
1204 return rc;
1205 }
1206
1207 pHub->cAvailablePorts++;
1208 Assert(pHub->cAvailablePorts > 0 && pHub->cAvailablePorts <= pHub->cPorts);
1209 pUsbIns->Internal.s.pHub = NULL;
1210 }
1211
1212 /*
1213 * Notify about unplugging and destroy the device with it's drivers.
1214 */
1215 pdmR3UsbDestroyDevice(pVM, pUsbIns);
1216
1217 return VINF_SUCCESS;
1218}
1219
1220
1221/**
1222 * Checks if there are any USB hubs attached.
1223 *
1224 * @returns true / false accordingly.
1225 * @param pUVM The user mode VM handle.
1226 */
1227VMMR3DECL(bool) PDMR3UsbHasHub(PUVM pUVM)
1228{
1229 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1230 PVM pVM = pUVM->pVM;
1231 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1232 return pVM->pdm.s.pUsbHubs != NULL;
1233}
1234
1235
1236/**
1237 * Locates a LUN.
1238 *
1239 * @returns VBox status code.
1240 * @param pVM The cross context VM structure.
1241 * @param pszDevice Device name.
1242 * @param iInstance Device instance.
1243 * @param iLun The Logical Unit to obtain the interface of.
1244 * @param ppLun Where to store the pointer to the LUN if found.
1245 * @thread Try only do this in EMT...
1246 */
1247static int pdmR3UsbFindLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPPDMLUN ppLun)
1248{
1249 /*
1250 * Iterate registered devices looking for the device.
1251 */
1252 size_t cchDevice = strlen(pszDevice);
1253 for (PPDMUSB pUsbDev = pVM->pdm.s.pUsbDevs; pUsbDev; pUsbDev = pUsbDev->pNext)
1254 {
1255 if ( pUsbDev->cchName == cchDevice
1256 && !memcmp(pUsbDev->pReg->szName, pszDevice, cchDevice))
1257 {
1258 /*
1259 * Iterate device instances.
1260 */
1261 for (PPDMUSBINS pUsbIns = pUsbDev->pInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pPerDeviceNext)
1262 {
1263 if (pUsbIns->iInstance == iInstance)
1264 {
1265 /*
1266 * Iterate luns.
1267 */
1268 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1269 {
1270 if (pLun->iLun == iLun)
1271 {
1272 *ppLun = pLun;
1273 return VINF_SUCCESS;
1274 }
1275 }
1276 return VERR_PDM_LUN_NOT_FOUND;
1277 }
1278 }
1279 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
1280 }
1281 }
1282 return VERR_PDM_DEVICE_NOT_FOUND;
1283}
1284
1285
1286/**
1287 * Attaches a preconfigured driver to an existing device or driver instance.
1288 *
1289 * This is used to change drivers and suchlike at runtime. The driver or device
1290 * at the end of the chain will be told to attach to whatever is configured
1291 * below it.
1292 *
1293 * @returns VBox status code.
1294 * @param pUVM The user mode VM handle.
1295 * @param pszDevice Device name.
1296 * @param iDevIns Device instance.
1297 * @param iLun The Logical Unit to obtain the interface of.
1298 * @param fFlags Flags, combination of the PDM_TACH_FLAGS_* \#defines.
1299 * @param ppBase Where to store the base interface pointer. Optional.
1300 *
1301 * @thread EMT
1302 */
1303VMMR3DECL(int) PDMR3UsbDriverAttach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun, uint32_t fFlags,
1304 PPPDMIBASE ppBase)
1305{
1306 LogFlow(("PDMR3UsbDriverAttach: pszDevice=%p:{%s} iDevIns=%d iLun=%d fFlags=%#x ppBase=%p\n",
1307 pszDevice, pszDevice, iDevIns, iLun, fFlags, ppBase));
1308 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1309 PVM pVM = pUVM->pVM;
1310 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1311 VM_ASSERT_EMT(pVM);
1312
1313 if (ppBase)
1314 *ppBase = NULL;
1315
1316 /*
1317 * Find the LUN in question.
1318 */
1319 PPDMLUN pLun;
1320 int rc = pdmR3UsbFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
1321 if (RT_SUCCESS(rc))
1322 {
1323 /*
1324 * Anything attached to the LUN?
1325 */
1326 PPDMDRVINS pDrvIns = pLun->pTop;
1327 if (!pDrvIns)
1328 {
1329 /* No, ask the device to attach to the new stuff. */
1330 PPDMUSBINS pUsbIns = pLun->pUsbIns;
1331 if (pUsbIns->pReg->pfnDriverAttach)
1332 {
1333 rc = pUsbIns->pReg->pfnDriverAttach(pUsbIns, iLun, fFlags);
1334 if (RT_SUCCESS(rc) && ppBase)
1335 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
1336 }
1337 else
1338 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
1339 }
1340 else
1341 {
1342 /* Yes, find the bottom most driver and ask it to attach to the new stuff. */
1343 while (pDrvIns->Internal.s.pDown)
1344 pDrvIns = pDrvIns->Internal.s.pDown;
1345 if (pDrvIns->pReg->pfnAttach)
1346 {
1347 rc = pDrvIns->pReg->pfnAttach(pDrvIns, fFlags);
1348 if (RT_SUCCESS(rc) && ppBase)
1349 *ppBase = pDrvIns->Internal.s.pDown
1350 ? &pDrvIns->Internal.s.pDown->IBase
1351 : NULL;
1352 }
1353 else
1354 rc = VERR_PDM_DRIVER_NO_RT_ATTACH;
1355 }
1356 }
1357
1358 if (ppBase)
1359 LogFlow(("PDMR3UsbDriverAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
1360 else
1361 LogFlow(("PDMR3UsbDriverAttach: returns %Rrc\n", rc));
1362 return rc;
1363}
1364
1365
1366/**
1367 * Detaches the specified driver instance.
1368 *
1369 * This is used to replumb drivers at runtime for simulating hot plugging and
1370 * media changes.
1371 *
1372 * This method allows detaching drivers from
1373 * any driver or device by specifying the driver to start detaching at. The
1374 * only prerequisite is that the driver or device above implements the
1375 * pfnDetach callback (PDMDRVREG / PDMUSBREG).
1376 *
1377 * @returns VBox status code.
1378 * @param pUVM The user mode VM handle.
1379 * @param pszDevice Device name.
1380 * @param iDevIns Device instance.
1381 * @param iLun The Logical Unit in which to look for the driver.
1382 * @param pszDriver The name of the driver which to detach. If NULL
1383 * then the entire driver chain is detatched.
1384 * @param iOccurance The occurrence of that driver in the chain. This is
1385 * usually 0.
1386 * @param fFlags Flags, combination of the PDM_TACH_FLAGS_* \#defines.
1387 * @thread EMT
1388 */
1389VMMR3DECL(int) PDMR3UsbDriverDetach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
1390 const char *pszDriver, unsigned iOccurance, uint32_t fFlags)
1391{
1392 LogFlow(("PDMR3UsbDriverDetach: pszDevice=%p:{%s} iDevIns=%u iLun=%u pszDriver=%p:{%s} iOccurance=%u fFlags=%#x\n",
1393 pszDevice, pszDevice, iDevIns, iLun, pszDriver, pszDriver, iOccurance, fFlags));
1394 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1395 PVM pVM = pUVM->pVM;
1396 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1397 VM_ASSERT_EMT(pVM);
1398 AssertPtr(pszDevice);
1399 AssertPtrNull(pszDriver);
1400 Assert(iOccurance == 0 || pszDriver);
1401 Assert(!(fFlags & ~(PDM_TACH_FLAGS_NOT_HOT_PLUG)));
1402
1403 /*
1404 * Find the LUN in question.
1405 */
1406 PPDMLUN pLun;
1407 int rc = pdmR3UsbFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
1408 if (RT_SUCCESS(rc))
1409 {
1410 /*
1411 * Locate the driver.
1412 */
1413 PPDMDRVINS pDrvIns = pLun->pTop;
1414 if (pDrvIns)
1415 {
1416 if (pszDriver)
1417 {
1418 while (pDrvIns)
1419 {
1420 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
1421 {
1422 if (iOccurance == 0)
1423 break;
1424 iOccurance--;
1425 }
1426 pDrvIns = pDrvIns->Internal.s.pDown;
1427 }
1428 }
1429 if (pDrvIns)
1430 rc = pdmR3DrvDetach(pDrvIns, fFlags);
1431 else
1432 rc = VERR_PDM_DRIVER_INSTANCE_NOT_FOUND;
1433 }
1434 else
1435 rc = VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN;
1436 }
1437
1438 LogFlow(("PDMR3UsbDriverDetach: returns %Rrc\n", rc));
1439 return rc;
1440}
1441
1442
1443/**
1444 * Query the interface of the top level driver on a LUN.
1445 *
1446 * @returns VBox status code.
1447 * @param pUVM The user mode VM handle.
1448 * @param pszDevice Device name.
1449 * @param iInstance Device instance.
1450 * @param iLun The Logical Unit to obtain the interface of.
1451 * @param ppBase Where to store the base interface pointer.
1452 * @remark We're not doing any locking ATM, so don't try call this at times when the
1453 * device chain is known to be updated.
1454 */
1455VMMR3DECL(int) PDMR3UsbQueryLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
1456{
1457 LogFlow(("PDMR3UsbQueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
1458 pszDevice, pszDevice, iInstance, iLun, ppBase));
1459 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1460 PVM pVM = pUVM->pVM;
1461 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1462
1463 /*
1464 * Find the LUN.
1465 */
1466 PPDMLUN pLun;
1467 int rc = pdmR3UsbFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1468 if (RT_SUCCESS(rc))
1469 {
1470 if (pLun->pTop)
1471 {
1472 *ppBase = &pLun->pTop->IBase;
1473 LogFlow(("PDMR3UsbQueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
1474 return VINF_SUCCESS;
1475 }
1476 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
1477 }
1478 LogFlow(("PDMR3UsbQueryLun: returns %Rrc\n", rc));
1479 return rc;
1480}
1481
1482
1483/**
1484 * Query the interface of a named driver on a LUN.
1485 *
1486 * If the driver appears more than once in the driver chain, the first instance
1487 * is returned.
1488 *
1489 * @returns VBox status code.
1490 * @param pUVM The user mode VM handle.
1491 * @param pszDevice Device name.
1492 * @param iInstance Device instance.
1493 * @param iLun The Logical Unit to obtain the interface of.
1494 * @param pszDriver The driver name.
1495 * @param ppBase Where to store the base interface pointer.
1496 *
1497 * @remark We're not doing any locking ATM, so don't try call this at times when the
1498 * device chain is known to be updated.
1499 */
1500VMMR3DECL(int) PDMR3UsbQueryDriverOnLun(PUVM pUVM, const char *pszDevice, unsigned iInstance,
1501 unsigned iLun, const char *pszDriver, PPPDMIBASE ppBase)
1502{
1503 LogFlow(("PDMR3QueryDriverOnLun: pszDevice=%p:{%s} iInstance=%u iLun=%u pszDriver=%p:{%s} ppBase=%p\n",
1504 pszDevice, pszDevice, iInstance, iLun, pszDriver, pszDriver, ppBase));
1505 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1506 PVM pVM = pUVM->pVM;
1507 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1508
1509 /*
1510 * Find the LUN.
1511 */
1512 PPDMLUN pLun;
1513 int rc = pdmR3UsbFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1514 if (RT_SUCCESS(rc))
1515 {
1516 if (pLun->pTop)
1517 {
1518 for (PPDMDRVINS pDrvIns = pLun->pTop; pDrvIns; pDrvIns = pDrvIns->Internal.s.pDown)
1519 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
1520 {
1521 *ppBase = &pDrvIns->IBase;
1522 LogFlow(("PDMR3UsbQueryDriverOnLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
1523 return VINF_SUCCESS;
1524
1525 }
1526 rc = VERR_PDM_DRIVER_NOT_FOUND;
1527 }
1528 else
1529 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
1530 }
1531 LogFlow(("PDMR3UsbQueryDriverOnLun: returns %Rrc\n", rc));
1532 return rc;
1533}
1534
1535
1536/** @name USB Device Helpers
1537 * @{
1538 */
1539
1540/** @interface_method_impl{PDMUSBHLP,pfnDriverAttach} */
1541static DECLCALLBACK(int) pdmR3UsbHlp_DriverAttach(PPDMUSBINS pUsbIns, RTUINT iLun, PPDMIBASE pBaseInterface,
1542 PPDMIBASE *ppBaseInterface, const char *pszDesc)
1543{
1544 PDMUSB_ASSERT_USBINS(pUsbIns);
1545 PVM pVM = pUsbIns->Internal.s.pVM;
1546 VM_ASSERT_EMT(pVM);
1547 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: iLun=%d pBaseInterface=%p ppBaseInterface=%p pszDesc=%p:{%s}\n",
1548 pUsbIns->pReg->szName, pUsbIns->iInstance, iLun, pBaseInterface, ppBaseInterface, pszDesc, pszDesc));
1549
1550 /*
1551 * Lookup the LUN, it might already be registered.
1552 */
1553 PPDMLUN pLunPrev = NULL;
1554 PPDMLUN pLun = pUsbIns->Internal.s.pLuns;
1555 for (; pLun; pLunPrev = pLun, pLun = pLun->pNext)
1556 if (pLun->iLun == iLun)
1557 break;
1558
1559 /*
1560 * Create the LUN if if wasn't found, else check if driver is already attached to it.
1561 */
1562 if (!pLun)
1563 {
1564 if ( !pBaseInterface
1565 || !pszDesc
1566 || !*pszDesc)
1567 {
1568 Assert(pBaseInterface);
1569 Assert(pszDesc || *pszDesc);
1570 return VERR_INVALID_PARAMETER;
1571 }
1572
1573 pLun = (PPDMLUN)MMR3HeapAlloc(pVM, MM_TAG_PDM_LUN, sizeof(*pLun));
1574 if (!pLun)
1575 return VERR_NO_MEMORY;
1576
1577 pLun->iLun = iLun;
1578 pLun->pNext = pLunPrev ? pLunPrev->pNext : NULL;
1579 pLun->pTop = NULL;
1580 pLun->pBottom = NULL;
1581 pLun->pDevIns = NULL;
1582 pLun->pUsbIns = pUsbIns;
1583 pLun->pszDesc = pszDesc;
1584 pLun->pBase = pBaseInterface;
1585 if (!pLunPrev)
1586 pUsbIns->Internal.s.pLuns = pLun;
1587 else
1588 pLunPrev->pNext = pLun;
1589 Log(("pdmR3UsbHlp_DriverAttach: Registered LUN#%d '%s' with device '%s'/%d.\n",
1590 iLun, pszDesc, pUsbIns->pReg->szName, pUsbIns->iInstance));
1591 }
1592 else if (pLun->pTop)
1593 {
1594 AssertMsgFailed(("Already attached! The device should keep track of such things!\n"));
1595 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, VERR_PDM_DRIVER_ALREADY_ATTACHED));
1596 return VERR_PDM_DRIVER_ALREADY_ATTACHED;
1597 }
1598 Assert(pLun->pBase == pBaseInterface);
1599
1600
1601 /*
1602 * Get the attached driver configuration.
1603 */
1604 int rc;
1605 PCFGMNODE pNode = CFGMR3GetChildF(pUsbIns->Internal.s.pCfg, "LUN#%u", iLun);
1606 if (pNode)
1607 rc = pdmR3DrvInstantiate(pVM, pNode, pBaseInterface, NULL /*pDrvAbove*/, pLun, ppBaseInterface);
1608 else
1609 rc = VERR_PDM_NO_ATTACHED_DRIVER;
1610
1611
1612 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1613 return rc;
1614}
1615
1616
1617/** @interface_method_impl{PDMUSBHLP,pfnAssertEMT} */
1618static DECLCALLBACK(bool) pdmR3UsbHlp_AssertEMT(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1619{
1620 PDMUSB_ASSERT_USBINS(pUsbIns);
1621 if (VM_IS_EMT(pUsbIns->Internal.s.pVM))
1622 return true;
1623
1624 char szMsg[100];
1625 RTStrPrintf(szMsg, sizeof(szMsg), "AssertEMT '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance);
1626 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1627 AssertBreakpoint();
1628 return false;
1629}
1630
1631
1632/** @interface_method_impl{PDMUSBHLP,pfnAssertOther} */
1633static DECLCALLBACK(bool) pdmR3UsbHlp_AssertOther(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1634{
1635 PDMUSB_ASSERT_USBINS(pUsbIns);
1636 if (!VM_IS_EMT(pUsbIns->Internal.s.pVM))
1637 return true;
1638
1639 char szMsg[100];
1640 RTStrPrintf(szMsg, sizeof(szMsg), "AssertOther '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance);
1641 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1642 AssertBreakpoint();
1643 return false;
1644}
1645
1646
1647/** @interface_method_impl{PDMUSBHLP,pfnDBGFStopV} */
1648static DECLCALLBACK(int) pdmR3UsbHlp_DBGFStopV(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction,
1649 const char *pszFormat, va_list va)
1650{
1651 PDMUSB_ASSERT_USBINS(pUsbIns);
1652#ifdef LOG_ENABLED
1653 va_list va2;
1654 va_copy(va2, va);
1655 LogFlow(("pdmR3UsbHlp_DBGFStopV: caller='%s'/%d: pszFile=%p:{%s} iLine=%d pszFunction=%p:{%s} pszFormat=%p:{%s} (%N)\n",
1656 pUsbIns->pReg->szName, pUsbIns->iInstance, pszFile, pszFile, iLine, pszFunction, pszFunction, pszFormat, pszFormat, pszFormat, &va2));
1657 va_end(va2);
1658#endif
1659
1660 PVM pVM = pUsbIns->Internal.s.pVM;
1661 VM_ASSERT_EMT(pVM);
1662 int rc = DBGFR3EventSrcV(pVM, DBGFEVENT_DEV_STOP, pszFile, iLine, pszFunction, pszFormat, va);
1663 if (rc == VERR_DBGF_NOT_ATTACHED)
1664 rc = VINF_SUCCESS;
1665
1666 LogFlow(("pdmR3UsbHlp_DBGFStopV: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1667 return rc;
1668}
1669
1670
1671/** @interface_method_impl{PDMUSBHLP,pfnDBGFInfoRegister} */
1672static DECLCALLBACK(int) pdmR3UsbHlp_DBGFInfoRegister(PPDMUSBINS pUsbIns, const char *pszName, const char *pszDesc,
1673 PFNDBGFHANDLERUSB pfnHandler)
1674{
1675 PDMUSB_ASSERT_USBINS(pUsbIns);
1676 LogFlow(("pdmR3UsbHlp_DBGFInfoRegister: caller='%s'/%d: pszName=%p:{%s} pszDesc=%p:{%s} pfnHandler=%p\n",
1677 pUsbIns->pReg->szName, pUsbIns->iInstance, pszName, pszName, pszDesc, pszDesc, pfnHandler));
1678
1679 PVM pVM = pUsbIns->Internal.s.pVM;
1680 VM_ASSERT_EMT(pVM);
1681 RT_NOREF4(pVM, pfnHandler, pszDesc, pszName); /** @todo int rc = DBGFR3InfoRegisterUsb(pVM, pszName, pszDesc, pfnHandler, pUsbIns); */
1682 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1683
1684 LogFlow(("pdmR3UsbHlp_DBGFInfoRegister: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1685 return rc;
1686}
1687
1688
1689/** @interface_method_impl{PDMUSBHLP,pfnMMHeapAlloc} */
1690static DECLCALLBACK(void *) pdmR3UsbHlp_MMHeapAlloc(PPDMUSBINS pUsbIns, size_t cb)
1691{
1692 PDMUSB_ASSERT_USBINS(pUsbIns);
1693 LogFlow(("pdmR3UsbHlp_MMHeapAlloc: caller='%s'/%d: cb=%#x\n", pUsbIns->pReg->szName, pUsbIns->iInstance, cb));
1694
1695 void *pv = MMR3HeapAlloc(pUsbIns->Internal.s.pVM, MM_TAG_PDM_USB_USER, cb);
1696
1697 LogFlow(("pdmR3UsbHlp_MMHeapAlloc: caller='%s'/%d: returns %p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pv));
1698 return pv;
1699}
1700
1701
1702/** @interface_method_impl{PDMUSBHLP,pfnMMHeapAllocZ} */
1703static DECLCALLBACK(void *) pdmR3UsbHlp_MMHeapAllocZ(PPDMUSBINS pUsbIns, size_t cb)
1704{
1705 PDMUSB_ASSERT_USBINS(pUsbIns);
1706 LogFlow(("pdmR3UsbHlp_MMHeapAllocZ: caller='%s'/%d: cb=%#x\n", pUsbIns->pReg->szName, pUsbIns->iInstance, cb));
1707
1708 void *pv = MMR3HeapAllocZ(pUsbIns->Internal.s.pVM, MM_TAG_PDM_USB_USER, cb);
1709
1710 LogFlow(("pdmR3UsbHlp_MMHeapAllocZ: caller='%s'/%d: returns %p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pv));
1711 return pv;
1712}
1713
1714
1715/** @interface_method_impl{PDMUSBHLP,pfnPDMQueueCreate} */
1716static DECLCALLBACK(int) pdmR3UsbHlp_PDMQueueCreate(PPDMUSBINS pUsbIns, RTUINT cbItem, RTUINT cItems, uint32_t cMilliesInterval,
1717 PFNPDMQUEUEUSB pfnCallback, const char *pszName, PPDMQUEUE *ppQueue)
1718{
1719 PDMUSB_ASSERT_USBINS(pUsbIns);
1720 LogFlow(("pdmR3UsbHlp_PDMQueueCreate: caller='%s'/%d: cbItem=%#x cItems=%#x cMilliesInterval=%u pfnCallback=%p pszName=%p:{%s} ppQueue=%p\n",
1721 pUsbIns->pReg->szName, pUsbIns->iInstance, cbItem, cItems, cMilliesInterval, pfnCallback, pszName, pszName, ppQueue));
1722
1723 PVM pVM = pUsbIns->Internal.s.pVM;
1724 VM_ASSERT_EMT(pVM);
1725
1726 if (pUsbIns->iInstance > 0)
1727 {
1728 pszName = MMR3HeapAPrintf(pVM, MM_TAG_PDM_DEVICE_DESC, "%s_%u", pszName, pUsbIns->iInstance);
1729 AssertLogRelReturn(pszName, VERR_NO_MEMORY);
1730 }
1731
1732 RT_NOREF5(cbItem, cItems, cMilliesInterval, pfnCallback, ppQueue);
1733 /** @todo int rc = PDMR3QueueCreateUsb(pVM, pUsbIns, cbItem, cItems, cMilliesInterval, pfnCallback, fGCEnabled, pszName, ppQueue); */
1734 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1735
1736 LogFlow(("pdmR3UsbHlp_PDMQueueCreate: caller='%s'/%d: returns %Rrc *ppQueue=%p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc, *ppQueue));
1737 return rc;
1738}
1739
1740
1741/** @interface_method_impl{PDMUSBHLP,pfnSSMRegister} */
1742static DECLCALLBACK(int) pdmR3UsbHlp_SSMRegister(PPDMUSBINS pUsbIns, uint32_t uVersion, size_t cbGuess,
1743 PFNSSMUSBLIVEPREP pfnLivePrep, PFNSSMUSBLIVEEXEC pfnLiveExec, PFNSSMUSBLIVEVOTE pfnLiveVote,
1744 PFNSSMUSBSAVEPREP pfnSavePrep, PFNSSMUSBSAVEEXEC pfnSaveExec, PFNSSMUSBSAVEDONE pfnSaveDone,
1745 PFNSSMUSBLOADPREP pfnLoadPrep, PFNSSMUSBLOADEXEC pfnLoadExec, PFNSSMUSBLOADDONE pfnLoadDone)
1746{
1747 PDMUSB_ASSERT_USBINS(pUsbIns);
1748 VM_ASSERT_EMT(pUsbIns->Internal.s.pVM);
1749 LogFlow(("pdmR3UsbHlp_SSMRegister: caller='%s'/%d: uVersion=%#x cbGuess=%#x\n"
1750 " pfnLivePrep=%p pfnLiveExec=%p pfnLiveVote=%p pfnSavePrep=%p pfnSaveExec=%p pfnSaveDone=%p pszLoadPrep=%p pfnLoadExec=%p pfnLoadDone=%p\n",
1751 pUsbIns->pReg->szName, pUsbIns->iInstance, uVersion, cbGuess,
1752 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1753 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1754 pfnLoadPrep, pfnLoadExec, pfnLoadDone));
1755
1756 int rc = SSMR3RegisterUsb(pUsbIns->Internal.s.pVM, pUsbIns, pUsbIns->pReg->szName, pUsbIns->iInstance,
1757 uVersion, cbGuess,
1758 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1759 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1760 pfnLoadPrep, pfnLoadExec, pfnLoadDone);
1761
1762 LogFlow(("pdmR3UsbHlp_SSMRegister: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1763 return rc;
1764}
1765
1766
1767/** @interface_method_impl{PDMUSBHLP,pfnSTAMRegisterV} */
1768static DECLCALLBACK(void) pdmR3UsbHlp_STAMRegisterV(PPDMUSBINS pUsbIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
1769 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list va)
1770{
1771 PDMUSB_ASSERT_USBINS(pUsbIns);
1772 PVM pVM = pUsbIns->Internal.s.pVM;
1773 VM_ASSERT_EMT(pVM);
1774
1775 int rc = STAMR3RegisterV(pVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, va);
1776 AssertRC(rc);
1777
1778 NOREF(pVM);
1779}
1780
1781
1782/** @interface_method_impl{PDMUSBHLP,pfnTMTimerCreate} */
1783static DECLCALLBACK(int) pdmR3UsbHlp_TMTimerCreate(PPDMUSBINS pUsbIns, TMCLOCK enmClock, PFNTMTIMERUSB pfnCallback, void *pvUser,
1784 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1785{
1786 PDMUSB_ASSERT_USBINS(pUsbIns);
1787 PVM pVM = pUsbIns->Internal.s.pVM;
1788 VM_ASSERT_EMT(pVM);
1789 LogFlow(("pdmR3UsbHlp_TMTimerCreate: caller='%s'/%d: enmClock=%d pfnCallback=%p pvUser=%p fFlags=%#x pszDesc=%p:{%s} ppTimer=%p\n",
1790 pUsbIns->pReg->szName, pUsbIns->iInstance, enmClock, pfnCallback, pvUser, fFlags, pszDesc, pszDesc, ppTimer));
1791
1792 if (pUsbIns->iInstance > 0) /** @todo use a string cache here later. */
1793 {
1794 char *pszDesc2 = MMR3HeapAPrintf(pVM, MM_TAG_PDM_USB_DESC, "%s [%u]", pszDesc, pUsbIns->iInstance);
1795 if (pszDesc2)
1796 pszDesc = pszDesc2;
1797 }
1798
1799 int rc = TMR3TimerCreateUsb(pVM, pUsbIns, enmClock, pfnCallback, pvUser, fFlags, pszDesc, ppTimer);
1800
1801 LogFlow(("pdmR3UsbHlp_TMTimerCreate: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1802 return rc;
1803}
1804
1805
1806/** @interface_method_impl{PDMUSBHLP,pfnVMSetErrorV} */
1807static DECLCALLBACK(int) pdmR3UsbHlp_VMSetErrorV(PPDMUSBINS pUsbIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
1808{
1809 PDMUSB_ASSERT_USBINS(pUsbIns);
1810 int rc2 = VMSetErrorV(pUsbIns->Internal.s.pVM, rc, RT_SRC_POS_ARGS, pszFormat, va); Assert(rc2 == rc); NOREF(rc2);
1811 return rc;
1812}
1813
1814
1815/** @interface_method_impl{PDMUSBHLP,pfnVMSetRuntimeErrorV} */
1816static DECLCALLBACK(int) pdmR3UsbHlp_VMSetRuntimeErrorV(PPDMUSBINS pUsbIns, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
1817{
1818 PDMUSB_ASSERT_USBINS(pUsbIns);
1819 int rc = VMSetRuntimeErrorV(pUsbIns->Internal.s.pVM, fFlags, pszErrorId, pszFormat, va);
1820 return rc;
1821}
1822
1823
1824/** @interface_method_impl{PDMUSBHLP,pfnVMState} */
1825static DECLCALLBACK(VMSTATE) pdmR3UsbHlp_VMState(PPDMUSBINS pUsbIns)
1826{
1827 PDMUSB_ASSERT_USBINS(pUsbIns);
1828
1829 VMSTATE enmVMState = VMR3GetState(pUsbIns->Internal.s.pVM);
1830
1831 LogFlow(("pdmR3UsbHlp_VMState: caller='%s'/%d: returns %d (%s)\n", pUsbIns->pReg->szName, pUsbIns->iInstance,
1832 enmVMState, VMR3GetStateName(enmVMState)));
1833 return enmVMState;
1834}
1835
1836/** @interface_method_impl{PDMUSBHLP,pfnThreadCreate} */
1837static DECLCALLBACK(int) pdmR3UsbHlp_ThreadCreate(PPDMUSBINS pUsbIns, PPPDMTHREAD ppThread, void *pvUser, PFNPDMTHREADUSB pfnThread,
1838 PFNPDMTHREADWAKEUPUSB pfnWakeup, size_t cbStack, RTTHREADTYPE enmType, const char *pszName)
1839{
1840 PDMUSB_ASSERT_USBINS(pUsbIns);
1841 VM_ASSERT_EMT(pUsbIns->Internal.s.pVM);
1842 LogFlow(("pdmR3UsbHlp_ThreadCreate: caller='%s'/%d: ppThread=%p pvUser=%p pfnThread=%p pfnWakeup=%p cbStack=%#zx enmType=%d pszName=%p:{%s}\n",
1843 pUsbIns->pReg->szName, pUsbIns->iInstance, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName, pszName));
1844
1845 int rc = pdmR3ThreadCreateUsb(pUsbIns->Internal.s.pVM, pUsbIns, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName);
1846
1847 LogFlow(("pdmR3UsbHlp_ThreadCreate: caller='%s'/%d: returns %Rrc *ppThread=%RTthrd\n", pUsbIns->pReg->szName, pUsbIns->iInstance,
1848 rc, *ppThread));
1849 return rc;
1850}
1851
1852
1853/** @interface_method_impl{PDMUSBHLP,pfnSetAsyncNotification} */
1854static DECLCALLBACK(int) pdmR3UsbHlp_SetAsyncNotification(PPDMUSBINS pUsbIns, PFNPDMUSBASYNCNOTIFY pfnAsyncNotify)
1855{
1856 PDMUSB_ASSERT_USBINS(pUsbIns);
1857 VM_ASSERT_EMT0(pUsbIns->Internal.s.pVM);
1858 LogFlow(("pdmR3UsbHlp_SetAsyncNotification: caller='%s'/%d: pfnAsyncNotify=%p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pfnAsyncNotify));
1859
1860 int rc = VINF_SUCCESS;
1861 AssertStmt(pfnAsyncNotify, rc = VERR_INVALID_PARAMETER);
1862 AssertStmt(!pUsbIns->Internal.s.pfnAsyncNotify, rc = VERR_WRONG_ORDER);
1863 AssertStmt(pUsbIns->Internal.s.fVMSuspended || pUsbIns->Internal.s.fVMReset, rc = VERR_WRONG_ORDER);
1864 VMSTATE enmVMState = VMR3GetState(pUsbIns->Internal.s.pVM);
1865 AssertStmt( enmVMState == VMSTATE_SUSPENDING
1866 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1867 || enmVMState == VMSTATE_SUSPENDING_LS
1868 || enmVMState == VMSTATE_RESETTING
1869 || enmVMState == VMSTATE_RESETTING_LS
1870 || enmVMState == VMSTATE_POWERING_OFF
1871 || enmVMState == VMSTATE_POWERING_OFF_LS,
1872 rc = VERR_INVALID_STATE);
1873
1874 if (RT_SUCCESS(rc))
1875 pUsbIns->Internal.s.pfnAsyncNotify = pfnAsyncNotify;
1876
1877 LogFlow(("pdmR3UsbHlp_SetAsyncNotification: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1878 return rc;
1879}
1880
1881
1882/** @interface_method_impl{PDMUSBHLP,pfnAsyncNotificationCompleted} */
1883static DECLCALLBACK(void) pdmR3UsbHlp_AsyncNotificationCompleted(PPDMUSBINS pUsbIns)
1884{
1885 PDMUSB_ASSERT_USBINS(pUsbIns);
1886 PVM pVM = pUsbIns->Internal.s.pVM;
1887
1888 VMSTATE enmVMState = VMR3GetState(pVM);
1889 if ( enmVMState == VMSTATE_SUSPENDING
1890 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1891 || enmVMState == VMSTATE_SUSPENDING_LS
1892 || enmVMState == VMSTATE_RESETTING
1893 || enmVMState == VMSTATE_RESETTING_LS
1894 || enmVMState == VMSTATE_POWERING_OFF
1895 || enmVMState == VMSTATE_POWERING_OFF_LS)
1896 {
1897 LogFlow(("pdmR3UsbHlp_AsyncNotificationCompleted: caller='%s'/%d:\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1898 VMR3AsyncPdmNotificationWakeupU(pVM->pUVM);
1899 }
1900 else
1901 LogFlow(("pdmR3UsbHlp_AsyncNotificationCompleted: caller='%s'/%d: enmVMState=%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance, enmVMState));
1902}
1903
1904
1905/** @interface_method_impl{PDMUSBHLP,pfnVMGetSuspendReason} */
1906static DECLCALLBACK(VMSUSPENDREASON) pdmR3UsbHlp_VMGetSuspendReason(PPDMUSBINS pUsbIns)
1907{
1908 PDMUSB_ASSERT_USBINS(pUsbIns);
1909 PVM pVM = pUsbIns->Internal.s.pVM;
1910 VM_ASSERT_EMT(pVM);
1911 VMSUSPENDREASON enmReason = VMR3GetSuspendReason(pVM->pUVM);
1912 LogFlow(("pdmR3UsbHlp_VMGetSuspendReason: caller='%s'/%d: returns %d\n",
1913 pUsbIns->pReg->szName, pUsbIns->iInstance, enmReason));
1914 return enmReason;
1915}
1916
1917
1918/** @interface_method_impl{PDMUSBHLP,pfnVMGetResumeReason} */
1919static DECLCALLBACK(VMRESUMEREASON) pdmR3UsbHlp_VMGetResumeReason(PPDMUSBINS pUsbIns)
1920{
1921 PDMUSB_ASSERT_USBINS(pUsbIns);
1922 PVM pVM = pUsbIns->Internal.s.pVM;
1923 VM_ASSERT_EMT(pVM);
1924 VMRESUMEREASON enmReason = VMR3GetResumeReason(pVM->pUVM);
1925 LogFlow(("pdmR3UsbHlp_VMGetResumeReason: caller='%s'/%d: returns %d\n",
1926 pUsbIns->pReg->szName, pUsbIns->iInstance, enmReason));
1927 return enmReason;
1928}
1929
1930
1931/**
1932 * The USB device helper structure.
1933 */
1934const PDMUSBHLP g_pdmR3UsbHlp =
1935{
1936 PDM_USBHLP_VERSION,
1937 pdmR3UsbHlp_DriverAttach,
1938 pdmR3UsbHlp_AssertEMT,
1939 pdmR3UsbHlp_AssertOther,
1940 pdmR3UsbHlp_DBGFStopV,
1941 pdmR3UsbHlp_DBGFInfoRegister,
1942 pdmR3UsbHlp_MMHeapAlloc,
1943 pdmR3UsbHlp_MMHeapAllocZ,
1944 pdmR3UsbHlp_PDMQueueCreate,
1945 pdmR3UsbHlp_SSMRegister,
1946 pdmR3UsbHlp_STAMRegisterV,
1947 pdmR3UsbHlp_TMTimerCreate,
1948 pdmR3UsbHlp_VMSetErrorV,
1949 pdmR3UsbHlp_VMSetRuntimeErrorV,
1950 pdmR3UsbHlp_VMState,
1951 pdmR3UsbHlp_ThreadCreate,
1952 pdmR3UsbHlp_SetAsyncNotification,
1953 pdmR3UsbHlp_AsyncNotificationCompleted,
1954 pdmR3UsbHlp_VMGetSuspendReason,
1955 pdmR3UsbHlp_VMGetResumeReason,
1956 NULL,
1957 NULL,
1958 NULL,
1959 NULL,
1960 NULL,
1961 NULL,
1962 NULL,
1963 NULL,
1964 NULL,
1965 NULL,
1966 PDM_USBHLP_VERSION
1967};
1968
1969/** @} */
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