VirtualBox

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

Last change on this file since 56085 was 53624, checked in by vboxsync, 10 years ago

scm automatic cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 67.4 KB
Line 
1/* $Id: PDMUsb.cpp 53624 2014-12-31 14:59:44Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device and Driver Manager, USB part.
4 */
5
6/*
7 * Copyright (C) 2006-2014 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 Pointer to the VM.
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(("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 Pointer to the VM.
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_EMULATED_DEVICE)) == 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 Pointer to the VM.
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 Pointer to the VM.
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 Pointer to the VM.
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 Pointer to the VM.
490 * @param pUsbDev The USB device emulation.
491 * @param iInstance -1 if not called by pdmR3UsbInstantiateDevices().
492 * @param pUuid The UUID for this device.
493 * @param ppInstanceNode Pointer to the device instance pointer. This is set to NULL if inserted
494 * into the tree or cleaned up.
495 *
496 * In the pdmR3UsbInstantiateDevices() case (iInstance != -1) this is
497 * the actual instance node and will not be cleaned up.
498 *
499 * @param enmSpeed The speed the USB device is operating at.
500 * @param pszCaptureFilename Path to the file for USB traffic capturing, optional.
501 */
502static int pdmR3UsbCreateDevice(PVM pVM, PPDMUSBHUB pHub, PPDMUSB pUsbDev, int iInstance, PCRTUUID pUuid,
503 PCFGMNODE *ppInstanceNode, VUSBSPEED enmSpeed, const char *pszCaptureFilename)
504{
505 const bool fAtRuntime = iInstance == -1;
506 int rc;
507
508 AssertPtrReturn(ppInstanceNode, VERR_INVALID_POINTER);
509 AssertPtrReturn(*ppInstanceNode, VERR_INVALID_POINTER);
510
511 /*
512 * If not called by pdmR3UsbInstantiateDevices(), we'll have to fix
513 * the configuration now.
514 */
515 /* USB device node. */
516 PCFGMNODE pDevNode = CFGMR3GetChildF(CFGMR3GetRoot(pVM), "USB/%s/", pUsbDev->pReg->szName);
517 if (!pDevNode)
518 {
519 rc = CFGMR3InsertNodeF(CFGMR3GetRoot(pVM), &pDevNode, "USB/%s/", pUsbDev->pReg->szName);
520 AssertRCReturn(rc, rc);
521 }
522
523 /* The instance node and number. */
524 PCFGMNODE pInstanceToDelete = NULL;
525 PCFGMNODE pInstanceNode = NULL;
526 if (fAtRuntime)
527 {
528 /** @todo r=bird: This code is bogus as it ASSUMES that all USB devices are
529 * capable of infinite number of instances. */
530 for (unsigned c = 0; c < _2M; c++)
531 {
532 iInstance = pUsbDev->iNextInstance++;
533 rc = CFGMR3InsertNodeF(pDevNode, &pInstanceNode, "%d/", iInstance);
534 if (rc != VERR_CFGM_NODE_EXISTS)
535 break;
536 }
537 AssertRCReturn(rc, rc);
538
539 rc = CFGMR3ReplaceSubTree(pInstanceNode, *ppInstanceNode);
540 AssertRCReturn(rc, rc);
541 *ppInstanceNode = NULL;
542 pInstanceToDelete = pInstanceNode;
543 }
544 else
545 {
546 Assert(iInstance >= 0);
547 if (iInstance >= (int)pUsbDev->iNextInstance)
548 pUsbDev->iNextInstance = iInstance + 1;
549 pInstanceNode = *ppInstanceNode;
550 }
551
552 /* Make sure the instance config node exists. */
553 PCFGMNODE pConfig = CFGMR3GetChild(pInstanceNode, "Config");
554 if (!pConfig)
555 {
556 rc = CFGMR3InsertNode(pInstanceNode, "Config", &pConfig);
557 AssertRCReturn(rc, rc);
558 }
559 Assert(CFGMR3GetChild(pInstanceNode, "Config") == pConfig);
560
561 /* The global device config node. */
562 PCFGMNODE pGlobalConfig = CFGMR3GetChild(pDevNode, "GlobalConfig");
563 if (!pGlobalConfig)
564 {
565 rc = CFGMR3InsertNode(pDevNode, "GlobalConfig", &pGlobalConfig);
566 if (RT_FAILURE(rc))
567 {
568 CFGMR3RemoveNode(pInstanceToDelete);
569 AssertRCReturn(rc, rc);
570 }
571 }
572
573 /*
574 * Allocate the device instance.
575 */
576 size_t cb = RT_OFFSETOF(PDMUSBINS, achInstanceData[pUsbDev->pReg->cbInstance]);
577 cb = RT_ALIGN_Z(cb, 16);
578 PPDMUSBINS pUsbIns;
579 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_USB, cb, (void **)&pUsbIns);
580 if (RT_FAILURE(rc))
581 {
582 AssertMsgFailed(("Failed to allocate %d bytes of instance data for USB device '%s'. rc=%Rrc\n",
583 cb, pUsbDev->pReg->szName, rc));
584 CFGMR3RemoveNode(pInstanceToDelete);
585 return rc;
586 }
587
588 /*
589 * Initialize it.
590 */
591 pUsbIns->u32Version = PDM_USBINS_VERSION;
592 //pUsbIns->Internal.s.pNext = NULL;
593 //pUsbIns->Internal.s.pPerDeviceNext = NULL;
594 pUsbIns->Internal.s.pUsbDev = pUsbDev;
595 pUsbIns->Internal.s.pVM = pVM;
596 //pUsbIns->Internal.s.pLuns = NULL;
597 pUsbIns->Internal.s.pCfg = pInstanceNode;
598 pUsbIns->Internal.s.pCfgDelete = pInstanceToDelete;
599 pUsbIns->Internal.s.pCfgGlobal = pGlobalConfig;
600 pUsbIns->Internal.s.Uuid = *pUuid;
601 //pUsbIns->Internal.s.pHub = NULL;
602 pUsbIns->Internal.s.iPort = UINT32_MAX; /* to be determined. */
603 /* Set the flag accordingly.
604 * Oherwise VMPowerOff, VMSuspend will not be called for devices attached at runtime.
605 */
606 pUsbIns->Internal.s.fVMSuspended = !fAtRuntime;
607 //pUsbIns->Internal.s.pfnAsyncNotify = NULL;
608 pUsbIns->pHlpR3 = &g_pdmR3UsbHlp;
609 pUsbIns->pReg = pUsbDev->pReg;
610 pUsbIns->pCfg = pConfig;
611 pUsbIns->pCfgGlobal = pGlobalConfig;
612 pUsbIns->iInstance = iInstance;
613 pUsbIns->pvInstanceDataR3 = &pUsbIns->achInstanceData[0];
614 pUsbIns->pszName = RTStrDup(pUsbDev->pReg->szName);
615 //pUsbIns->fTracing = 0;
616 pUsbIns->idTracing = ++pVM->pdm.s.idTracingOther;
617 pUsbIns->enmSpeed = enmSpeed;
618
619 /*
620 * Link it into all the lists.
621 */
622 /* The global instance FIFO. */
623 PPDMUSBINS pPrev1 = pVM->pdm.s.pUsbInstances;
624 if (!pPrev1)
625 pVM->pdm.s.pUsbInstances = pUsbIns;
626 else
627 {
628 while (pPrev1->Internal.s.pNext)
629 {
630 Assert(pPrev1->u32Version == PDM_USBINS_VERSION);
631 pPrev1 = pPrev1->Internal.s.pNext;
632 }
633 pPrev1->Internal.s.pNext = pUsbIns;
634 }
635
636 /* The per device instance FIFO. */
637 PPDMUSBINS pPrev2 = pUsbDev->pInstances;
638 if (!pPrev2)
639 pUsbDev->pInstances = pUsbIns;
640 else
641 {
642 while (pPrev2->Internal.s.pPerDeviceNext)
643 {
644 Assert(pPrev2->u32Version == PDM_USBINS_VERSION);
645 pPrev2 = pPrev2->Internal.s.pPerDeviceNext;
646 }
647 pPrev2->Internal.s.pPerDeviceNext = pUsbIns;
648 }
649
650 /*
651 * Call the constructor.
652 */
653 Log(("PDM: Constructing USB device '%s' instance %d...\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
654 rc = pUsbIns->pReg->pfnConstruct(pUsbIns, pUsbIns->iInstance, pUsbIns->pCfg, pUsbIns->pCfgGlobal);
655 if (RT_SUCCESS(rc))
656 {
657 /*
658 * Attach it to the hub.
659 */
660 Log(("PDM: Attaching it...\n"));
661 rc = pHub->Reg.pfnAttachDevice(pHub->pDrvIns, pUsbIns, pszCaptureFilename, &pUsbIns->Internal.s.iPort);
662 if (RT_SUCCESS(rc))
663 {
664 pHub->cAvailablePorts--;
665 Assert((int32_t)pHub->cAvailablePorts >= 0 && pHub->cAvailablePorts < pHub->cPorts);
666 pUsbIns->Internal.s.pHub = pHub;
667
668 /* Send the hot-plugged notification if applicable. */
669 if (fAtRuntime && pUsbIns->pReg->pfnHotPlugged)
670 pUsbIns->pReg->pfnHotPlugged(pUsbIns);
671
672 Log(("PDM: Successfully attached USB device '%s' instance %d to hub %p\n",
673 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub));
674 return VINF_SUCCESS;
675 }
676
677 LogRel(("PDM: Failed to attach USB device '%s' instance %d to hub %p: %Rrc\n",
678 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
679 }
680 else
681 {
682 AssertMsgFailed(("Failed to construct '%s'/%d! %Rra\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
683 if (rc == VERR_VERSION_MISMATCH)
684 rc = VERR_PDM_DRIVER_VERSION_MISMATCH;
685 }
686 if (fAtRuntime)
687 pdmR3UsbDestroyDevice(pVM, pUsbIns);
688 /* else: destructors are invoked later. */
689 return rc;
690}
691
692
693/**
694 * Instantiate USB devices.
695 *
696 * This is called by pdmR3DevInit() after it has instantiated the
697 * other devices and their drivers. If there aren't any hubs
698 * around, we'll silently skip the USB devices.
699 *
700 * @returns VBox status code.
701 * @param pVM
702 */
703int pdmR3UsbInstantiateDevices(PVM pVM)
704{
705 /*
706 * Any hubs?
707 */
708 if (!pVM->pdm.s.pUsbHubs)
709 {
710 Log(("PDM: No USB hubs, skipping USB device instantiation.\n"));
711 return VINF_SUCCESS;
712 }
713
714 /*
715 * Count the device instances.
716 */
717 PCFGMNODE pCur;
718 PCFGMNODE pUsbNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "USB/");
719 PCFGMNODE pInstanceNode;
720 unsigned cUsbDevs = 0;
721 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
722 {
723 PCFGMNODE pGlobal = CFGMR3GetChild(pCur, "GlobalConfig/");
724 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
725 if (pInstanceNode != pGlobal)
726 cUsbDevs++;
727 }
728 if (!cUsbDevs)
729 {
730 Log(("PDM: No USB devices were configured!\n"));
731 return VINF_SUCCESS;
732 }
733 Log2(("PDM: cUsbDevs=%d!\n", cUsbDevs));
734
735 /*
736 * Collect info on each USB device instance.
737 */
738 struct USBDEVORDER
739 {
740 /** Configuration node. */
741 PCFGMNODE pNode;
742 /** Pointer to the USB device. */
743 PPDMUSB pUsbDev;
744 /** Init order. */
745 uint32_t u32Order;
746 /** VBox instance number. */
747 uint32_t iInstance;
748 /** Device UUID. */
749 RTUUID Uuid;
750 } *paUsbDevs = (struct USBDEVORDER *)alloca(sizeof(paUsbDevs[0]) * (cUsbDevs + 1)); /* (One extra for swapping) */
751 Assert(paUsbDevs);
752 int rc;
753 unsigned i = 0;
754 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
755 {
756 /* Get the device name. */
757 char szName[sizeof(paUsbDevs[0].pUsbDev->pReg->szName)];
758 rc = CFGMR3GetName(pCur, szName, sizeof(szName));
759 AssertMsgRCReturn(rc, ("Configuration error: device name is too long (or something)! rc=%Rrc\n", rc), rc);
760
761 /* Find the device. */
762 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, szName);
763 AssertMsgReturn(pUsbDev, ("Configuration error: device '%s' not found!\n", szName), VERR_PDM_DEVICE_NOT_FOUND);
764
765 /* Configured priority or use default? */
766 uint32_t u32Order;
767 rc = CFGMR3QueryU32(pCur, "Priority", &u32Order);
768 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
769 u32Order = i << 4;
770 else
771 AssertMsgRCReturn(rc, ("Configuration error: reading \"Priority\" for the '%s' USB device failed rc=%Rrc!\n", szName, rc), rc);
772
773 /* Global config. */
774 PCFGMNODE pGlobal = CFGMR3GetChild(pCur, "GlobalConfig/");
775 if (!pGlobal)
776 {
777 rc = CFGMR3InsertNode(pCur, "GlobalConfig/", &pGlobal);
778 AssertMsgRCReturn(rc, ("Failed to create GlobalConfig node! rc=%Rrc\n", rc), rc);
779 CFGMR3SetRestrictedRoot(pGlobal);
780 }
781
782 /* Enumerate the device instances. */
783 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
784 {
785 if (pInstanceNode == pGlobal)
786 continue;
787
788 /* Use the configured UUID if present, create our own otherwise. */
789 char *pszUuid = NULL;
790
791 RTUuidClear(&paUsbDevs[i].Uuid);
792 rc = CFGMR3QueryStringAlloc(pInstanceNode, "UUID", &pszUuid);
793 if (RT_SUCCESS(rc))
794 {
795 AssertPtr(pszUuid);
796
797 rc = RTUuidFromStr(&paUsbDevs[i].Uuid, pszUuid);
798 AssertMsgRCReturn(rc, ("Failed to convert UUID from string! rc=%Rrc\n", rc), rc);
799 MMR3HeapFree(pszUuid);
800 }
801 else if (rc == VERR_CFGM_VALUE_NOT_FOUND)
802 rc = RTUuidCreate(&paUsbDevs[i].Uuid);
803
804 AssertRCReturn(rc, rc);
805 paUsbDevs[i].pNode = pInstanceNode;
806 paUsbDevs[i].pUsbDev = pUsbDev;
807 paUsbDevs[i].u32Order = u32Order;
808
809 /* Get the instance number. */
810 char szInstance[32];
811 rc = CFGMR3GetName(pInstanceNode, szInstance, sizeof(szInstance));
812 AssertMsgRCReturn(rc, ("Configuration error: instance name is too long (or something)! rc=%Rrc\n", rc), rc);
813 char *pszNext = NULL;
814 rc = RTStrToUInt32Ex(szInstance, &pszNext, 0, &paUsbDevs[i].iInstance);
815 AssertMsgRCReturn(rc, ("Configuration error: RTStrToInt32Ex failed on the instance name '%s'! rc=%Rrc\n", szInstance, rc), rc);
816 AssertMsgReturn(!*pszNext, ("Configuration error: the instance name '%s' isn't all digits. (%s)\n", szInstance, pszNext), VERR_INVALID_PARAMETER);
817
818 /* next instance */
819 i++;
820 }
821 } /* devices */
822 Assert(i == cUsbDevs);
823
824 /*
825 * Sort the device array ascending on u32Order. (bubble)
826 */
827 unsigned c = cUsbDevs - 1;
828 while (c)
829 {
830 unsigned j = 0;
831 for (i = 0; i < c; i++)
832 if (paUsbDevs[i].u32Order > paUsbDevs[i + 1].u32Order)
833 {
834 paUsbDevs[cUsbDevs] = paUsbDevs[i + 1];
835 paUsbDevs[i + 1] = paUsbDevs[i];
836 paUsbDevs[i] = paUsbDevs[cUsbDevs];
837 j = i;
838 }
839 c = j;
840 }
841
842 /*
843 * Instantiate the devices.
844 */
845 for (i = 0; i < cUsbDevs; i++)
846 {
847 /*
848 * Make sure there is a config node and mark it as restricted.
849 */
850 PCFGMNODE pConfigNode = CFGMR3GetChild(paUsbDevs[i].pNode, "Config/");
851 if (!pConfigNode)
852 {
853 rc = CFGMR3InsertNode(paUsbDevs[i].pNode, "Config", &pConfigNode);
854 AssertMsgRCReturn(rc, ("Failed to create Config node! rc=%Rrc\n", rc), rc);
855 }
856 CFGMR3SetRestrictedRoot(pConfigNode);
857
858 /*
859 * Every emulated device must support USB 1.x hubs; optionally, high-speed USB 2.0 hubs
860 * might be also supported. This determines where to attach the device.
861 */
862 uint32_t iUsbVersion = VUSB_STDVER_11;
863
864 if (paUsbDevs[i].pUsbDev->pReg->fFlags & PDM_USBREG_HIGHSPEED_CAPABLE)
865 iUsbVersion |= VUSB_STDVER_20;
866 if (paUsbDevs[i].pUsbDev->pReg->fFlags & PDM_USBREG_SUPERSPEED_CAPABLE)
867 iUsbVersion |= VUSB_STDVER_30;
868
869 /*
870 * Find a suitable hub with free ports.
871 */
872 PPDMUSBHUB pHub;
873 rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
874 if (RT_FAILURE(rc))
875 {
876 Log(("pdmR3UsbFindHub failed %Rrc\n", rc));
877 return rc;
878 }
879
880 /*
881 * This is how we inform the device what speed it's communicating at, and hence
882 * which descriptors it should present to the guest.
883 */
884 iUsbVersion &= pHub->fVersions;
885
886 /*
887 * Create and attach the device.
888 */
889 rc = pdmR3UsbCreateDevice(pVM, pHub, paUsbDevs[i].pUsbDev, paUsbDevs[i].iInstance, &paUsbDevs[i].Uuid,
890 &paUsbDevs[i].pNode, pdmR3UsbVer2Spd(iUsbVersion), NULL);
891 if (RT_FAILURE(rc))
892 return rc;
893 } /* for device instances */
894
895 return VINF_SUCCESS;
896}
897
898
899/**
900 * Creates an emulated USB device instance at runtime.
901 *
902 * This will find an appropriate HUB for the USB device
903 * and try instantiate the emulated device.
904 *
905 * @returns VBox status code.
906 * @param pUVM The user mode VM handle.
907 * @param pszDeviceName The name of the PDM device to instantiate.
908 * @param pInstanceNode The instance CFGM node.
909 * @param pUuid The UUID to be associated with the device.
910 * @param pszCaptureFilename Path to the file for USB traffic capturing, optional.
911 *
912 * @thread EMT
913 */
914VMMR3DECL(int) PDMR3UsbCreateEmulatedDevice(PUVM pUVM, const char *pszDeviceName, PCFGMNODE pInstanceNode, PCRTUUID pUuid,
915 const char *pszCaptureFilename)
916{
917 /*
918 * Validate input.
919 */
920 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
921 PVM pVM = pUVM->pVM;
922 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
923 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
924 AssertPtrReturn(pszDeviceName, VERR_INVALID_POINTER);
925 AssertPtrReturn(pInstanceNode, VERR_INVALID_POINTER);
926
927 /*
928 * Find the device.
929 */
930 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, pszDeviceName);
931 if (!pUsbDev)
932 {
933 LogRel(("PDMR3UsbCreateEmulatedDevice: The '%s' device wasn't found\n", pszDeviceName));
934 return VERR_PDM_NO_USBPROXY;
935 }
936
937 /*
938 * Every device must support USB 1.x hubs; optionally, high-speed USB 2.0 hubs
939 * might be also supported. This determines where to attach the device.
940 */
941 uint32_t iUsbVersion = VUSB_STDVER_11;
942 if (pUsbDev->pReg->fFlags & PDM_USBREG_HIGHSPEED_CAPABLE)
943 iUsbVersion |= VUSB_STDVER_20;
944 if (pUsbDev->pReg->fFlags & PDM_USBREG_SUPERSPEED_CAPABLE)
945 iUsbVersion |= VUSB_STDVER_30;
946
947 /*
948 * Find a suitable hub with free ports.
949 */
950 PPDMUSBHUB pHub;
951 int rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
952 if (RT_FAILURE(rc))
953 {
954 Log(("pdmR3UsbFindHub: failed %Rrc\n", rc));
955 return rc;
956 }
957
958 /*
959 * This is how we inform the device what speed it's communicating at, and hence
960 * which descriptors it should present to the guest.
961 */
962 iUsbVersion &= pHub->fVersions;
963
964 /*
965 * Create and attach the device.
966 */
967 rc = pdmR3UsbCreateDevice(pVM, pHub, pUsbDev, -1, pUuid, &pInstanceNode,
968 pdmR3UsbVer2Spd(iUsbVersion), pszCaptureFilename);
969 AssertRCReturn(rc, rc);
970
971 return rc;
972}
973
974
975/**
976 * Creates a USB proxy device instance.
977 *
978 * This will find an appropriate HUB for the USB device, create the necessary CFGM stuff
979 * and try instantiate the proxy device.
980 *
981 * @returns VBox status code.
982 * @param pUVM The user mode VM handle.
983 * @param pUuid The UUID to be associated with the device.
984 * @param fRemote Whether it's a remove or local device.
985 * @param pszAddress The address string.
986 * @param pvBackend Pointer to the backend.
987 * @param iUsbVersion The preferred USB version.
988 * @param fMaskedIfs The interfaces to hide from the guest.
989 * @param pszCaptureFilename Path to the file for USB traffic capturing, optional.
990 */
991VMMR3DECL(int) PDMR3UsbCreateProxyDevice(PUVM pUVM, PCRTUUID pUuid, bool fRemote, const char *pszAddress, void *pvBackend,
992 uint32_t iUsbVersion, uint32_t fMaskedIfs, const char *pszCaptureFilename)
993{
994 /*
995 * Validate input.
996 */
997 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
998 PVM pVM = pUVM->pVM;
999 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1000 VM_ASSERT_EMT_RETURN(pVM, VERR_VM_THREAD_NOT_EMT);
1001 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
1002 AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
1003 AssertReturn( iUsbVersion == VUSB_STDVER_30
1004 || iUsbVersion == VUSB_STDVER_20
1005 || iUsbVersion == VUSB_STDVER_11, VERR_INVALID_PARAMETER);
1006
1007 /*
1008 * Find the USBProxy driver.
1009 */
1010 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, "USBProxy");
1011 if (!pUsbDev)
1012 {
1013 LogRel(("PDMR3UsbCreateProxyDevice: The USBProxy device class wasn't found\n"));
1014 return VERR_PDM_NO_USBPROXY;
1015 }
1016
1017 /*
1018 * Find a suitable hub with free ports.
1019 */
1020 PPDMUSBHUB pHub;
1021 int rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
1022 if (RT_FAILURE(rc))
1023 {
1024 Log(("pdmR3UsbFindHub: failed %Rrc\n", rc));
1025 return rc;
1026 }
1027
1028 /*
1029 * Create the CFGM instance node.
1030 */
1031 PCFGMNODE pInstance = CFGMR3CreateTree(pUVM);
1032 AssertReturn(pInstance, VERR_NO_MEMORY);
1033 do /* break loop */
1034 {
1035 PCFGMNODE pConfig;
1036 rc = CFGMR3InsertNode(pInstance, "Config", &pConfig); AssertRCBreak(rc);
1037 rc = CFGMR3InsertString(pConfig, "Address", pszAddress); AssertRCBreak(rc);
1038 char szUuid[RTUUID_STR_LENGTH];
1039 rc = RTUuidToStr(pUuid, &szUuid[0], sizeof(szUuid)); AssertRCBreak(rc);
1040 rc = CFGMR3InsertString(pConfig, "UUID", szUuid); AssertRCBreak(rc);
1041 rc = CFGMR3InsertInteger(pConfig, "Remote", fRemote); AssertRCBreak(rc);
1042 rc = CFGMR3InsertInteger(pConfig, "USBVersion", iUsbVersion); AssertRCBreak(rc);
1043 rc = CFGMR3InsertInteger(pConfig, "pvBackend", (uintptr_t)pvBackend); AssertRCBreak(rc);
1044 rc = CFGMR3InsertInteger(pConfig, "MaskedIfs", fMaskedIfs); AssertRCBreak(rc);
1045 rc = CFGMR3InsertInteger(pConfig, "Force11Device", !(pHub->fVersions & iUsbVersion)); AssertRCBreak(rc);
1046 } while (0); /* break loop */
1047 if (RT_FAILURE(rc))
1048 {
1049 CFGMR3RemoveNode(pInstance);
1050 LogRel(("PDMR3UsbCreateProxyDevice: failed to setup CFGM config, rc=%Rrc\n", rc));
1051 return rc;
1052 }
1053
1054 VUSBSPEED enmSpeed = pdmR3UsbVer2Spd(iUsbVersion);
1055
1056 /*
1057 * Finally, try to create it.
1058 */
1059 rc = pdmR3UsbCreateDevice(pVM, pHub, pUsbDev, -1, pUuid, &pInstance, enmSpeed, pszCaptureFilename);
1060 if (RT_FAILURE(rc) && pInstance)
1061 CFGMR3RemoveNode(pInstance);
1062 return rc;
1063}
1064
1065
1066/**
1067 * Destroys a hot-plugged USB device.
1068 *
1069 * The device must be detached from the HUB at this point.
1070 *
1071 * @param pVM Pointer to the VM.
1072 * @param pUsbIns The USB device instance to destroy.
1073 * @thread EMT
1074 */
1075static void pdmR3UsbDestroyDevice(PVM pVM, PPDMUSBINS pUsbIns)
1076{
1077 Assert(!pUsbIns->Internal.s.pHub);
1078
1079 /*
1080 * Do the unplug notification.
1081 */
1082 /** @todo what about the drivers? */
1083 if (pUsbIns->pReg->pfnHotUnplugged)
1084 pUsbIns->pReg->pfnHotUnplugged(pUsbIns);
1085
1086 /*
1087 * Destroy the luns with their driver chains and call the device destructor.
1088 */
1089 while (pUsbIns->Internal.s.pLuns)
1090 {
1091 PPDMLUN pLun = pUsbIns->Internal.s.pLuns;
1092 pUsbIns->Internal.s.pLuns = pLun->pNext;
1093 if (pLun->pTop)
1094 pdmR3DrvDestroyChain(pLun->pTop, PDM_TACH_FLAGS_NOT_HOT_PLUG); /* Hotplugging is handled differently here atm. */
1095 MMR3HeapFree(pLun);
1096 }
1097
1098 /* finally, the device. */
1099 if (pUsbIns->pReg->pfnDestruct)
1100 {
1101 Log(("PDM: Destructing USB device '%s' instance %d...\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1102 pUsbIns->pReg->pfnDestruct(pUsbIns);
1103 }
1104 TMR3TimerDestroyUsb(pVM, pUsbIns);
1105 SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
1106 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
1107#ifdef VBOX_WITH_PDM_ASYNC_COMPLETION
1108 pdmR3AsyncCompletionTemplateDestroyUsb(pVM, pUsbIns);
1109#endif
1110
1111 /*
1112 * Unlink it.
1113 */
1114 /* The global instance FIFO. */
1115 if (pVM->pdm.s.pUsbInstances == pUsbIns)
1116 pVM->pdm.s.pUsbInstances = pUsbIns->Internal.s.pNext;
1117 else
1118 {
1119 PPDMUSBINS pPrev = pVM->pdm.s.pUsbInstances;
1120 while (pPrev && pPrev->Internal.s.pNext != pUsbIns)
1121 {
1122 Assert(pPrev->u32Version == PDM_USBINS_VERSION);
1123 pPrev = pPrev->Internal.s.pNext;
1124 }
1125 Assert(pPrev); Assert(pPrev != pUsbIns);
1126 if (pPrev)
1127 pPrev->Internal.s.pNext = pUsbIns->Internal.s.pNext;
1128 }
1129
1130 /* The per device instance FIFO. */
1131 PPDMUSB pUsbDev = pUsbIns->Internal.s.pUsbDev;
1132 if (pUsbDev->pInstances == pUsbIns)
1133 pUsbDev->pInstances = pUsbIns->Internal.s.pPerDeviceNext;
1134 else
1135 {
1136 PPDMUSBINS pPrev = pUsbDev->pInstances;
1137 while (pPrev && pPrev->Internal.s.pPerDeviceNext != pUsbIns)
1138 {
1139 Assert(pPrev->u32Version == PDM_USBINS_VERSION);
1140 pPrev = pPrev->Internal.s.pPerDeviceNext;
1141 }
1142 Assert(pPrev); Assert(pPrev != pUsbIns);
1143 if (pPrev)
1144 pPrev->Internal.s.pPerDeviceNext = pUsbIns->Internal.s.pPerDeviceNext;
1145 }
1146
1147 /*
1148 * Trash it.
1149 */
1150 pUsbIns->u32Version = 0;
1151 pUsbIns->pReg = NULL;
1152 if (pUsbIns->pszName)
1153 {
1154 RTStrFree(pUsbIns->pszName);
1155 pUsbIns->pszName = NULL;
1156 }
1157 CFGMR3RemoveNode(pUsbIns->Internal.s.pCfgDelete);
1158 MMR3HeapFree(pUsbIns);
1159}
1160
1161
1162/**
1163 * Detaches and destroys a USB device.
1164 *
1165 * @returns VBox status code.
1166 * @param pUVM The user mode VM handle.
1167 * @param pUuid The UUID associated with the device to detach.
1168 * @thread EMT
1169 */
1170VMMR3DECL(int) PDMR3UsbDetachDevice(PUVM pUVM, PCRTUUID pUuid)
1171{
1172 /*
1173 * Validate input.
1174 */
1175 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1176 PVM pVM = pUVM->pVM;
1177 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1178 VM_ASSERT_EMT(pVM);
1179 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
1180
1181 /*
1182 * Search the global list for it.
1183 */
1184 PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances;
1185 for ( ; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1186 if (!RTUuidCompare(&pUsbIns->Internal.s.Uuid, pUuid))
1187 break;
1188 if (!pUsbIns)
1189 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND; /** @todo VERR_PDM_USB_INSTANCE_NOT_FOUND */
1190
1191 /*
1192 * Detach it from the HUB (if it's actually attached to one).
1193 */
1194 PPDMUSBHUB pHub = pUsbIns->Internal.s.pHub;
1195 if (pHub)
1196 {
1197 int rc = pHub->Reg.pfnDetachDevice(pHub->pDrvIns, pUsbIns, pUsbIns->Internal.s.iPort);
1198 if (RT_FAILURE(rc))
1199 {
1200 LogRel(("PDM: Failed to detach USB device '%s' instance %d from %p: %Rrc\n",
1201 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
1202 return rc;
1203 }
1204
1205 pHub->cAvailablePorts++;
1206 Assert(pHub->cAvailablePorts > 0 && pHub->cAvailablePorts <= pHub->cPorts);
1207 pUsbIns->Internal.s.pHub = NULL;
1208 }
1209
1210 /*
1211 * Notify about unplugging and destroy the device with it's drivers.
1212 */
1213 pdmR3UsbDestroyDevice(pVM, pUsbIns);
1214
1215 return VINF_SUCCESS;
1216}
1217
1218
1219/**
1220 * Checks if there are any USB hubs attached.
1221 *
1222 * @returns true / false accordingly.
1223 * @param pUVM The user mode VM handle.
1224 */
1225VMMR3DECL(bool) PDMR3UsbHasHub(PUVM pUVM)
1226{
1227 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
1228 PVM pVM = pUVM->pVM;
1229 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
1230 return pVM->pdm.s.pUsbHubs != NULL;
1231}
1232
1233
1234/**
1235 * Locates a LUN.
1236 *
1237 * @returns VBox status code.
1238 * @param pVM Pointer to the VM.
1239 * @param pszDevice Device name.
1240 * @param iInstance Device instance.
1241 * @param iLun The Logical Unit to obtain the interface of.
1242 * @param ppLun Where to store the pointer to the LUN if found.
1243 * @thread Try only do this in EMT...
1244 */
1245static int pdmR3UsbFindLun(PVM pVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPPDMLUN ppLun)
1246{
1247 /*
1248 * Iterate registered devices looking for the device.
1249 */
1250 size_t cchDevice = strlen(pszDevice);
1251 for (PPDMUSB pUsbDev = pVM->pdm.s.pUsbDevs; pUsbDev; pUsbDev = pUsbDev->pNext)
1252 {
1253 if ( pUsbDev->cchName == cchDevice
1254 && !memcmp(pUsbDev->pReg->szName, pszDevice, cchDevice))
1255 {
1256 /*
1257 * Iterate device instances.
1258 */
1259 for (PPDMUSBINS pUsbIns = pUsbDev->pInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pPerDeviceNext)
1260 {
1261 if (pUsbIns->iInstance == iInstance)
1262 {
1263 /*
1264 * Iterate luns.
1265 */
1266 for (PPDMLUN pLun = pUsbIns->Internal.s.pLuns; pLun; pLun = pLun->pNext)
1267 {
1268 if (pLun->iLun == iLun)
1269 {
1270 *ppLun = pLun;
1271 return VINF_SUCCESS;
1272 }
1273 }
1274 return VERR_PDM_LUN_NOT_FOUND;
1275 }
1276 }
1277 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND;
1278 }
1279 }
1280 return VERR_PDM_DEVICE_NOT_FOUND;
1281}
1282
1283
1284/**
1285 * Attaches a preconfigured driver to an existing device or driver instance.
1286 *
1287 * This is used to change drivers and suchlike at runtime. The driver or device
1288 * at the end of the chain will be told to attach to whatever is configured
1289 * below it.
1290 *
1291 * @returns VBox status code.
1292 * @param pUVM The user mode VM handle.
1293 * @param pszDevice Device name.
1294 * @param iInstance Device instance.
1295 * @param iLun The Logical Unit to obtain the interface of.
1296 * @param fFlags Flags, combination of the PDM_TACH_FLAGS_* \#defines.
1297 * @param ppBase Where to store the base interface pointer. Optional.
1298 *
1299 * @thread EMT
1300 */
1301VMMR3DECL(int) PDMR3UsbDriverAttach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun, uint32_t fFlags,
1302 PPPDMIBASE ppBase)
1303{
1304 LogFlow(("PDMR3UsbDriverAttach: pszDevice=%p:{%s} iDevIns=%d iLun=%d fFlags=%#x ppBase=%p\n",
1305 pszDevice, pszDevice, iDevIns, iLun, fFlags, ppBase));
1306 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1307 PVM pVM = pUVM->pVM;
1308 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1309 VM_ASSERT_EMT(pVM);
1310
1311 if (ppBase)
1312 *ppBase = NULL;
1313
1314 /*
1315 * Find the LUN in question.
1316 */
1317 PPDMLUN pLun;
1318 int rc = pdmR3UsbFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
1319 if (RT_SUCCESS(rc))
1320 {
1321 /*
1322 * Anything attached to the LUN?
1323 */
1324 PPDMDRVINS pDrvIns = pLun->pTop;
1325 if (!pDrvIns)
1326 {
1327 /* No, ask the device to attach to the new stuff. */
1328 PPDMUSBINS pUsbIns = pLun->pUsbIns;
1329 if (pUsbIns->pReg->pfnDriverAttach)
1330 {
1331 rc = pUsbIns->pReg->pfnDriverAttach(pUsbIns, iLun, fFlags);
1332 if (RT_SUCCESS(rc) && ppBase)
1333 *ppBase = pLun->pTop ? &pLun->pTop->IBase : NULL;
1334 }
1335 else
1336 rc = VERR_PDM_DEVICE_NO_RT_ATTACH;
1337 }
1338 else
1339 {
1340 /* Yes, find the bottom most driver and ask it to attach to the new stuff. */
1341 while (pDrvIns->Internal.s.pDown)
1342 pDrvIns = pDrvIns->Internal.s.pDown;
1343 if (pDrvIns->pReg->pfnAttach)
1344 {
1345 rc = pDrvIns->pReg->pfnAttach(pDrvIns, fFlags);
1346 if (RT_SUCCESS(rc) && ppBase)
1347 *ppBase = pDrvIns->Internal.s.pDown
1348 ? &pDrvIns->Internal.s.pDown->IBase
1349 : NULL;
1350 }
1351 else
1352 rc = VERR_PDM_DRIVER_NO_RT_ATTACH;
1353 }
1354 }
1355
1356 if (ppBase)
1357 LogFlow(("PDMR3UsbDriverAttach: returns %Rrc *ppBase=%p\n", rc, *ppBase));
1358 else
1359 LogFlow(("PDMR3UsbDriverAttach: returns %Rrc\n", rc));
1360 return rc;
1361}
1362
1363
1364/**
1365 * Detaches the specified driver instance.
1366 *
1367 * This is used to replumb drivers at runtime for simulating hot plugging and
1368 * media changes.
1369 *
1370 * This method allows detaching drivers from
1371 * any driver or device by specifying the driver to start detaching at. The
1372 * only prerequisite is that the driver or device above implements the
1373 * pfnDetach callback (PDMDRVREG / PDMUSBREG).
1374 *
1375 * @returns VBox status code.
1376 * @param pUVM The user mode VM handle.
1377 * @param pszDevice Device name.
1378 * @param iDevIns Device instance.
1379 * @param iLun The Logical Unit in which to look for the driver.
1380 * @param pszDriver The name of the driver which to detach. If NULL
1381 * then the entire driver chain is detatched.
1382 * @param iOccurance The occurrence of that driver in the chain. This is
1383 * usually 0.
1384 * @param fFlags Flags, combination of the PDM_TACH_FLAGS_* \#defines.
1385 * @thread EMT
1386 */
1387VMMR3DECL(int) PDMR3UsbDriverDetach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
1388 const char *pszDriver, unsigned iOccurance, uint32_t fFlags)
1389{
1390 LogFlow(("PDMR3UsbDriverDetach: pszDevice=%p:{%s} iDevIns=%u iLun=%u pszDriver=%p:{%s} iOccurance=%u fFlags=%#x\n",
1391 pszDevice, pszDevice, iDevIns, iLun, pszDriver, iOccurance, fFlags));
1392 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1393 PVM pVM = pUVM->pVM;
1394 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1395 VM_ASSERT_EMT(pVM);
1396 AssertPtr(pszDevice);
1397 AssertPtrNull(pszDriver);
1398 Assert(iOccurance == 0 || pszDriver);
1399 Assert(!(fFlags & ~(PDM_TACH_FLAGS_NOT_HOT_PLUG)));
1400
1401 /*
1402 * Find the LUN in question.
1403 */
1404 PPDMLUN pLun;
1405 int rc = pdmR3UsbFindLun(pVM, pszDevice, iDevIns, iLun, &pLun);
1406 if (RT_SUCCESS(rc))
1407 {
1408 /*
1409 * Locate the driver.
1410 */
1411 PPDMDRVINS pDrvIns = pLun->pTop;
1412 if (pDrvIns)
1413 {
1414 if (pszDriver)
1415 {
1416 while (pDrvIns)
1417 {
1418 if (!strcmp(pDrvIns->pReg->szName, pszDriver))
1419 {
1420 if (iOccurance == 0)
1421 break;
1422 iOccurance--;
1423 }
1424 pDrvIns = pDrvIns->Internal.s.pDown;
1425 }
1426 }
1427 if (pDrvIns)
1428 rc = pdmR3DrvDetach(pDrvIns, fFlags);
1429 else
1430 rc = VERR_PDM_DRIVER_INSTANCE_NOT_FOUND;
1431 }
1432 else
1433 rc = VINF_PDM_NO_DRIVER_ATTACHED_TO_LUN;
1434 }
1435
1436 LogFlow(("PDMR3UsbDriverDetach: returns %Rrc\n", rc));
1437 return rc;
1438}
1439
1440
1441/**
1442 * Query the interface of the top level driver on a LUN.
1443 *
1444 * @returns VBox status code.
1445 * @param pUVM The user mode VM handle.
1446 * @param pszDevice Device name.
1447 * @param iInstance Device instance.
1448 * @param iLun The Logical Unit to obtain the interface of.
1449 * @param ppBase Where to store the base interface pointer.
1450 * @remark We're not doing any locking ATM, so don't try call this at times when the
1451 * device chain is known to be updated.
1452 */
1453VMMR3DECL(int) PDMR3UsbQueryLun(PUVM pUVM, const char *pszDevice, unsigned iInstance, unsigned iLun, PPDMIBASE *ppBase)
1454{
1455 LogFlow(("PDMR3UsbQueryLun: pszDevice=%p:{%s} iInstance=%u iLun=%u ppBase=%p\n",
1456 pszDevice, pszDevice, iInstance, iLun, ppBase));
1457 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1458 PVM pVM = pUVM->pVM;
1459 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1460
1461 /*
1462 * Find the LUN.
1463 */
1464 PPDMLUN pLun;
1465 int rc = pdmR3UsbFindLun(pVM, pszDevice, iInstance, iLun, &pLun);
1466 if (RT_SUCCESS(rc))
1467 {
1468 if (pLun->pTop)
1469 {
1470 *ppBase = &pLun->pTop->IBase;
1471 LogFlow(("PDMR3UsbQueryLun: return %Rrc and *ppBase=%p\n", VINF_SUCCESS, *ppBase));
1472 return VINF_SUCCESS;
1473 }
1474 rc = VERR_PDM_NO_DRIVER_ATTACHED_TO_LUN;
1475 }
1476 LogFlow(("PDMR3UsbQueryLun: returns %Rrc\n", rc));
1477 return rc;
1478}
1479
1480
1481/** @name USB Device Helpers
1482 * @{
1483 */
1484
1485/** @interface_method_impl{PDMUSBHLPR3,pfnDriverAttach} */
1486static DECLCALLBACK(int) pdmR3UsbHlp_DriverAttach(PPDMUSBINS pUsbIns, RTUINT iLun, PPDMIBASE pBaseInterface,
1487 PPDMIBASE *ppBaseInterface, const char *pszDesc)
1488{
1489 PDMUSB_ASSERT_USBINS(pUsbIns);
1490 PVM pVM = pUsbIns->Internal.s.pVM;
1491 VM_ASSERT_EMT(pVM);
1492 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: iLun=%d pBaseInterface=%p ppBaseInterface=%p pszDesc=%p:{%s}\n",
1493 pUsbIns->pReg->szName, pUsbIns->iInstance, iLun, pBaseInterface, ppBaseInterface, pszDesc, pszDesc));
1494
1495 /*
1496 * Lookup the LUN, it might already be registered.
1497 */
1498 PPDMLUN pLunPrev = NULL;
1499 PPDMLUN pLun = pUsbIns->Internal.s.pLuns;
1500 for (; pLun; pLunPrev = pLun, pLun = pLun->pNext)
1501 if (pLun->iLun == iLun)
1502 break;
1503
1504 /*
1505 * Create the LUN if if wasn't found, else check if driver is already attached to it.
1506 */
1507 if (!pLun)
1508 {
1509 if ( !pBaseInterface
1510 || !pszDesc
1511 || !*pszDesc)
1512 {
1513 Assert(pBaseInterface);
1514 Assert(pszDesc || *pszDesc);
1515 return VERR_INVALID_PARAMETER;
1516 }
1517
1518 pLun = (PPDMLUN)MMR3HeapAlloc(pVM, MM_TAG_PDM_LUN, sizeof(*pLun));
1519 if (!pLun)
1520 return VERR_NO_MEMORY;
1521
1522 pLun->iLun = iLun;
1523 pLun->pNext = pLunPrev ? pLunPrev->pNext : NULL;
1524 pLun->pTop = NULL;
1525 pLun->pBottom = NULL;
1526 pLun->pDevIns = NULL;
1527 pLun->pUsbIns = pUsbIns;
1528 pLun->pszDesc = pszDesc;
1529 pLun->pBase = pBaseInterface;
1530 if (!pLunPrev)
1531 pUsbIns->Internal.s.pLuns = pLun;
1532 else
1533 pLunPrev->pNext = pLun;
1534 Log(("pdmR3UsbHlp_DriverAttach: Registered LUN#%d '%s' with device '%s'/%d.\n",
1535 iLun, pszDesc, pUsbIns->pReg->szName, pUsbIns->iInstance));
1536 }
1537 else if (pLun->pTop)
1538 {
1539 AssertMsgFailed(("Already attached! The device should keep track of such things!\n"));
1540 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, VERR_PDM_DRIVER_ALREADY_ATTACHED));
1541 return VERR_PDM_DRIVER_ALREADY_ATTACHED;
1542 }
1543 Assert(pLun->pBase == pBaseInterface);
1544
1545
1546 /*
1547 * Get the attached driver configuration.
1548 */
1549 int rc;
1550 PCFGMNODE pNode = CFGMR3GetChildF(pUsbIns->Internal.s.pCfg, "LUN#%u", iLun);
1551 if (pNode)
1552 rc = pdmR3DrvInstantiate(pVM, pNode, pBaseInterface, NULL /*pDrvAbove*/, pLun, ppBaseInterface);
1553 else
1554 rc = VERR_PDM_NO_ATTACHED_DRIVER;
1555
1556
1557 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1558 return rc;
1559}
1560
1561
1562/** @interface_method_impl{PDMUSBHLP,pfnAssertEMT} */
1563static DECLCALLBACK(bool) pdmR3UsbHlp_AssertEMT(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1564{
1565 PDMUSB_ASSERT_USBINS(pUsbIns);
1566 if (VM_IS_EMT(pUsbIns->Internal.s.pVM))
1567 return true;
1568
1569 char szMsg[100];
1570 RTStrPrintf(szMsg, sizeof(szMsg), "AssertEMT '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance);
1571 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1572 AssertBreakpoint();
1573 return false;
1574}
1575
1576
1577/** @interface_method_impl{PDMUSBHLP,pfnAssertOther} */
1578static DECLCALLBACK(bool) pdmR3UsbHlp_AssertOther(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1579{
1580 PDMUSB_ASSERT_USBINS(pUsbIns);
1581 if (!VM_IS_EMT(pUsbIns->Internal.s.pVM))
1582 return true;
1583
1584 char szMsg[100];
1585 RTStrPrintf(szMsg, sizeof(szMsg), "AssertOther '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance);
1586 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1587 AssertBreakpoint();
1588 return false;
1589}
1590
1591
1592/** @interface_method_impl{PDMUSBHLP,pfnDBGFStopV} */
1593static DECLCALLBACK(int) pdmR3UsbHlp_DBGFStopV(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction, const char *pszFormat, va_list args)
1594{
1595 PDMUSB_ASSERT_USBINS(pUsbIns);
1596#ifdef LOG_ENABLED
1597 va_list va2;
1598 va_copy(va2, args);
1599 LogFlow(("pdmR3UsbHlp_DBGFStopV: caller='%s'/%d: pszFile=%p:{%s} iLine=%d pszFunction=%p:{%s} pszFormat=%p:{%s} (%N)\n",
1600 pUsbIns->pReg->szName, pUsbIns->iInstance, pszFile, pszFile, iLine, pszFunction, pszFunction, pszFormat, pszFormat, pszFormat, &va2));
1601 va_end(va2);
1602#endif
1603
1604 PVM pVM = pUsbIns->Internal.s.pVM;
1605 VM_ASSERT_EMT(pVM);
1606 int rc = DBGFR3EventSrcV(pVM, DBGFEVENT_DEV_STOP, pszFile, iLine, pszFunction, pszFormat, args);
1607 if (rc == VERR_DBGF_NOT_ATTACHED)
1608 rc = VINF_SUCCESS;
1609
1610 LogFlow(("pdmR3UsbHlp_DBGFStopV: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1611 return rc;
1612}
1613
1614
1615/** @interface_method_impl{PDMUSBHLP,pfnDBGFInfoRegister} */
1616static DECLCALLBACK(int) pdmR3UsbHlp_DBGFInfoRegister(PPDMUSBINS pUsbIns, const char *pszName, const char *pszDesc, PFNDBGFHANDLERUSB pfnHandler)
1617{
1618 PDMUSB_ASSERT_USBINS(pUsbIns);
1619 LogFlow(("pdmR3UsbHlp_DBGFInfoRegister: caller='%s'/%d: pszName=%p:{%s} pszDesc=%p:{%s} pfnHandler=%p\n",
1620 pUsbIns->pReg->szName, pUsbIns->iInstance, pszName, pszName, pszDesc, pszDesc, pfnHandler));
1621
1622 PVM pVM = pUsbIns->Internal.s.pVM;
1623 VM_ASSERT_EMT(pVM);
1624 NOREF(pVM); /** @todo int rc = DBGFR3InfoRegisterUsb(pVM, pszName, pszDesc, pfnHandler, pUsbIns); */
1625 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1626
1627 LogFlow(("pdmR3UsbHlp_DBGFInfoRegister: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1628 return rc;
1629}
1630
1631
1632/** @interface_method_impl{PDMUSBHLP,pfnMMHeapAlloc} */
1633static DECLCALLBACK(void *) pdmR3UsbHlp_MMHeapAlloc(PPDMUSBINS pUsbIns, size_t cb)
1634{
1635 PDMUSB_ASSERT_USBINS(pUsbIns);
1636 LogFlow(("pdmR3UsbHlp_MMHeapAlloc: caller='%s'/%d: cb=%#x\n", pUsbIns->pReg->szName, pUsbIns->iInstance, cb));
1637
1638 void *pv = MMR3HeapAlloc(pUsbIns->Internal.s.pVM, MM_TAG_PDM_USB_USER, cb);
1639
1640 LogFlow(("pdmR3UsbHlp_MMHeapAlloc: caller='%s'/%d: returns %p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pv));
1641 return pv;
1642}
1643
1644
1645/** @interface_method_impl{PDMUSBHLP,pfnMMHeapAllocZ} */
1646static DECLCALLBACK(void *) pdmR3UsbHlp_MMHeapAllocZ(PPDMUSBINS pUsbIns, size_t cb)
1647{
1648 PDMUSB_ASSERT_USBINS(pUsbIns);
1649 LogFlow(("pdmR3UsbHlp_MMHeapAllocZ: caller='%s'/%d: cb=%#x\n", pUsbIns->pReg->szName, pUsbIns->iInstance, cb));
1650
1651 void *pv = MMR3HeapAllocZ(pUsbIns->Internal.s.pVM, MM_TAG_PDM_USB_USER, cb);
1652
1653 LogFlow(("pdmR3UsbHlp_MMHeapAllocZ: caller='%s'/%d: returns %p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pv));
1654 return pv;
1655}
1656
1657
1658/** @interface_method_impl{PDMUSBHLP,pfnPDMQueueCreate} */
1659static DECLCALLBACK(int) pdmR3UsbHlp_PDMQueueCreate(PPDMUSBINS pUsbIns, RTUINT cbItem, RTUINT cItems, uint32_t cMilliesInterval,
1660 PFNPDMQUEUEUSB pfnCallback, const char *pszName, PPDMQUEUE *ppQueue)
1661{
1662 PDMUSB_ASSERT_USBINS(pUsbIns);
1663 LogFlow(("pdmR3UsbHlp_PDMQueueCreate: caller='%s'/%d: cbItem=%#x cItems=%#x cMilliesInterval=%u pfnCallback=%p pszName=%p:{%s} ppQueue=%p\n",
1664 pUsbIns->pReg->szName, pUsbIns->iInstance, cbItem, cItems, cMilliesInterval, pfnCallback, pszName, pszName, ppQueue));
1665
1666 PVM pVM = pUsbIns->Internal.s.pVM;
1667 VM_ASSERT_EMT(pVM);
1668
1669 if (pUsbIns->iInstance > 0)
1670 {
1671 pszName = MMR3HeapAPrintf(pVM, MM_TAG_PDM_DEVICE_DESC, "%s_%u", pszName, pUsbIns->iInstance);
1672 AssertLogRelReturn(pszName, VERR_NO_MEMORY);
1673 }
1674
1675 /** @todo int rc = PDMR3QueueCreateUsb(pVM, pUsbIns, cbItem, cItems, cMilliesInterval, pfnCallback, fGCEnabled, pszName, ppQueue); */
1676 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1677
1678 LogFlow(("pdmR3UsbHlp_PDMQueueCreate: caller='%s'/%d: returns %Rrc *ppQueue=%p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc, *ppQueue));
1679 return rc;
1680}
1681
1682
1683/** @interface_method_impl{PDMUSBHLP,pfnSSMRegister} */
1684static DECLCALLBACK(int) pdmR3UsbHlp_SSMRegister(PPDMUSBINS pUsbIns, uint32_t uVersion, size_t cbGuess,
1685 PFNSSMUSBLIVEPREP pfnLivePrep, PFNSSMUSBLIVEEXEC pfnLiveExec, PFNSSMUSBLIVEVOTE pfnLiveVote,
1686 PFNSSMUSBSAVEPREP pfnSavePrep, PFNSSMUSBSAVEEXEC pfnSaveExec, PFNSSMUSBSAVEDONE pfnSaveDone,
1687 PFNSSMUSBLOADPREP pfnLoadPrep, PFNSSMUSBLOADEXEC pfnLoadExec, PFNSSMUSBLOADDONE pfnLoadDone)
1688{
1689 PDMUSB_ASSERT_USBINS(pUsbIns);
1690 VM_ASSERT_EMT(pUsbIns->Internal.s.pVM);
1691 LogFlow(("pdmR3UsbHlp_SSMRegister: caller='%s'/%d: uVersion=#x cbGuess=%#x\n"
1692 " pfnLivePrep=%p pfnLiveExec=%p pfnLiveVote=%p pfnSavePrep=%p pfnSaveExec=%p pfnSaveDone=%p pszLoadPrep=%p pfnLoadExec=%p pfnLoadDone=%p\n",
1693 pUsbIns->pReg->szName, pUsbIns->iInstance, uVersion, cbGuess,
1694 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1695 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1696 pfnLoadPrep, pfnLoadExec, pfnLoadDone));
1697
1698 int rc = SSMR3RegisterUsb(pUsbIns->Internal.s.pVM, pUsbIns, pUsbIns->pReg->szName, pUsbIns->iInstance,
1699 uVersion, cbGuess,
1700 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1701 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1702 pfnLoadPrep, pfnLoadExec, pfnLoadDone);
1703
1704 LogFlow(("pdmR3UsbHlp_SSMRegister: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1705 return rc;
1706}
1707
1708
1709/** @interface_method_impl{PDMUSBHLP,pfnSTAMRegisterV} */
1710static DECLCALLBACK(void) pdmR3UsbHlp_STAMRegisterV(PPDMUSBINS pUsbIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
1711 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list args)
1712{
1713 PDMUSB_ASSERT_USBINS(pUsbIns);
1714 PVM pVM = pUsbIns->Internal.s.pVM;
1715 VM_ASSERT_EMT(pVM);
1716
1717 int rc = STAMR3RegisterV(pVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
1718 AssertRC(rc);
1719
1720 NOREF(pVM);
1721}
1722
1723
1724/** @interface_method_impl{PDMUSBHLP,pfnTMTimerCreate} */
1725static DECLCALLBACK(int) pdmR3UsbHlp_TMTimerCreate(PPDMUSBINS pUsbIns, TMCLOCK enmClock, PFNTMTIMERUSB pfnCallback, void *pvUser,
1726 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1727{
1728 PDMUSB_ASSERT_USBINS(pUsbIns);
1729 PVM pVM = pUsbIns->Internal.s.pVM;
1730 VM_ASSERT_EMT(pVM);
1731 LogFlow(("pdmR3UsbHlp_TMTimerCreate: caller='%s'/%d: enmClock=%d pfnCallback=%p pvUser=%p fFlags=%#x pszDesc=%p:{%s} ppTimer=%p\n",
1732 pUsbIns->pReg->szName, pUsbIns->iInstance, enmClock, pfnCallback, pvUser, fFlags, pszDesc, pszDesc, ppTimer));
1733
1734 if (pUsbIns->iInstance > 0) /** @todo use a string cache here later. */
1735 {
1736 char *pszDesc2 = MMR3HeapAPrintf(pVM, MM_TAG_PDM_USB_DESC, "%s [%u]", pszDesc, pUsbIns->iInstance);
1737 if (pszDesc2)
1738 pszDesc = pszDesc2;
1739 }
1740
1741 int rc = TMR3TimerCreateUsb(pVM, pUsbIns, enmClock, pfnCallback, pvUser, fFlags, pszDesc, ppTimer);
1742
1743 LogFlow(("pdmR3UsbHlp_TMTimerCreate: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1744 return rc;
1745}
1746
1747
1748/** @interface_method_impl{PDMUSBHLP,pfnVMSetErrorV} */
1749static DECLCALLBACK(int) pdmR3UsbHlp_VMSetErrorV(PPDMUSBINS pUsbIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
1750{
1751 PDMUSB_ASSERT_USBINS(pUsbIns);
1752 int rc2 = VMSetErrorV(pUsbIns->Internal.s.pVM, rc, RT_SRC_POS_ARGS, pszFormat, va); Assert(rc2 == rc); NOREF(rc2);
1753 return rc;
1754}
1755
1756
1757/** @interface_method_impl{PDMUSBHLP,pfnVMSetRuntimeErrorV} */
1758static DECLCALLBACK(int) pdmR3UsbHlp_VMSetRuntimeErrorV(PPDMUSBINS pUsbIns, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
1759{
1760 PDMUSB_ASSERT_USBINS(pUsbIns);
1761 int rc = VMSetRuntimeErrorV(pUsbIns->Internal.s.pVM, fFlags, pszErrorId, pszFormat, va);
1762 return rc;
1763}
1764
1765
1766/** @interface_method_impl{PDMUSBHLP,pfnVMState} */
1767static DECLCALLBACK(VMSTATE) pdmR3UsbHlp_VMState(PPDMUSBINS pUsbIns)
1768{
1769 PDMUSB_ASSERT_USBINS(pUsbIns);
1770
1771 VMSTATE enmVMState = VMR3GetState(pUsbIns->Internal.s.pVM);
1772
1773 LogFlow(("pdmR3UsbHlp_VMState: caller='%s'/%d: returns %d (%s)\n", pUsbIns->pReg->szName, pUsbIns->iInstance,
1774 enmVMState, VMR3GetStateName(enmVMState)));
1775 return enmVMState;
1776}
1777
1778/** @interface_method_impl{PDMUSBHLP,pfnThreadCreate} */
1779static DECLCALLBACK(int) pdmR3UsbHlp_ThreadCreate(PPDMUSBINS pUsbIns, PPPDMTHREAD ppThread, void *pvUser, PFNPDMTHREADUSB pfnThread,
1780 PFNPDMTHREADWAKEUPUSB pfnWakeup, size_t cbStack, RTTHREADTYPE enmType, const char *pszName)
1781{
1782 PDMUSB_ASSERT_USBINS(pUsbIns);
1783 VM_ASSERT_EMT(pUsbIns->Internal.s.pVM);
1784 LogFlow(("pdmR3UsbHlp_ThreadCreate: caller='%s'/%d: ppThread=%p pvUser=%p pfnThread=%p pfnWakeup=%p cbStack=%#zx enmType=%d pszName=%p:{%s}\n",
1785 pUsbIns->pReg->szName, pUsbIns->iInstance, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName, pszName));
1786
1787 int rc = pdmR3ThreadCreateUsb(pUsbIns->Internal.s.pVM, pUsbIns, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName);
1788
1789 LogFlow(("pdmR3UsbHlp_ThreadCreate: caller='%s'/%d: returns %Rrc *ppThread=%RTthrd\n", pUsbIns->pReg->szName, pUsbIns->iInstance,
1790 rc, *ppThread));
1791 return rc;
1792}
1793
1794
1795/** @interface_method_impl{PDMUSBHLP,pfnSetAsyncNotification} */
1796static DECLCALLBACK(int) pdmR3UsbHlp_SetAsyncNotification(PPDMUSBINS pUsbIns, PFNPDMUSBASYNCNOTIFY pfnAsyncNotify)
1797{
1798 PDMUSB_ASSERT_USBINS(pUsbIns);
1799 VM_ASSERT_EMT0(pUsbIns->Internal.s.pVM);
1800 LogFlow(("pdmR3UsbHlp_SetAsyncNotification: caller='%s'/%d: pfnAsyncNotify=%p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pfnAsyncNotify));
1801
1802 int rc = VINF_SUCCESS;
1803 AssertStmt(pfnAsyncNotify, rc = VERR_INVALID_PARAMETER);
1804 AssertStmt(!pUsbIns->Internal.s.pfnAsyncNotify, rc = VERR_WRONG_ORDER);
1805 AssertStmt(pUsbIns->Internal.s.fVMSuspended || pUsbIns->Internal.s.fVMReset, rc = VERR_WRONG_ORDER);
1806 VMSTATE enmVMState = VMR3GetState(pUsbIns->Internal.s.pVM);
1807 AssertStmt( enmVMState == VMSTATE_SUSPENDING
1808 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1809 || enmVMState == VMSTATE_SUSPENDING_LS
1810 || enmVMState == VMSTATE_RESETTING
1811 || enmVMState == VMSTATE_RESETTING_LS
1812 || enmVMState == VMSTATE_POWERING_OFF
1813 || enmVMState == VMSTATE_POWERING_OFF_LS,
1814 rc = VERR_INVALID_STATE);
1815
1816 if (RT_SUCCESS(rc))
1817 pUsbIns->Internal.s.pfnAsyncNotify = pfnAsyncNotify;
1818
1819 LogFlow(("pdmR3UsbHlp_SetAsyncNotification: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1820 return rc;
1821}
1822
1823
1824/** @interface_method_impl{PDMUSBHLP,pfnAsyncNotificationCompleted} */
1825static DECLCALLBACK(void) pdmR3UsbHlp_AsyncNotificationCompleted(PPDMUSBINS pUsbIns)
1826{
1827 PDMUSB_ASSERT_USBINS(pUsbIns);
1828 PVM pVM = pUsbIns->Internal.s.pVM;
1829
1830 VMSTATE enmVMState = VMR3GetState(pVM);
1831 if ( enmVMState == VMSTATE_SUSPENDING
1832 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1833 || enmVMState == VMSTATE_SUSPENDING_LS
1834 || enmVMState == VMSTATE_RESETTING
1835 || enmVMState == VMSTATE_RESETTING_LS
1836 || enmVMState == VMSTATE_POWERING_OFF
1837 || enmVMState == VMSTATE_POWERING_OFF_LS)
1838 {
1839 LogFlow(("pdmR3UsbHlp_AsyncNotificationCompleted: caller='%s'/%d:\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1840 VMR3AsyncPdmNotificationWakeupU(pVM->pUVM);
1841 }
1842 else
1843 LogFlow(("pdmR3UsbHlp_AsyncNotificationCompleted: caller='%s'/%d: enmVMState=%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance, enmVMState));
1844}
1845
1846
1847/** @interface_method_impl{PDMUSBHLP,pfnVMGetSuspendReason} */
1848static DECLCALLBACK(VMSUSPENDREASON) pdmR3UsbHlp_VMGetSuspendReason(PPDMUSBINS pUsbIns)
1849{
1850 PDMUSB_ASSERT_USBINS(pUsbIns);
1851 PVM pVM = pUsbIns->Internal.s.pVM;
1852 VM_ASSERT_EMT(pVM);
1853 VMSUSPENDREASON enmReason = VMR3GetSuspendReason(pVM->pUVM);
1854 LogFlow(("pdmR3UsbHlp_VMGetSuspendReason: caller='%s'/%d: returns %d\n",
1855 pUsbIns->pReg->szName, pUsbIns->iInstance, enmReason));
1856 return enmReason;
1857}
1858
1859
1860/** @interface_method_impl{PDMUSBHLP,pfnVMGetResumeReason} */
1861static DECLCALLBACK(VMRESUMEREASON) pdmR3UsbHlp_VMGetResumeReason(PPDMUSBINS pUsbIns)
1862{
1863 PDMUSB_ASSERT_USBINS(pUsbIns);
1864 PVM pVM = pUsbIns->Internal.s.pVM;
1865 VM_ASSERT_EMT(pVM);
1866 VMRESUMEREASON enmReason = VMR3GetResumeReason(pVM->pUVM);
1867 LogFlow(("pdmR3UsbHlp_VMGetResumeReason: caller='%s'/%d: returns %d\n",
1868 pUsbIns->pReg->szName, pUsbIns->iInstance, enmReason));
1869 return enmReason;
1870}
1871
1872
1873/**
1874 * The USB device helper structure.
1875 */
1876const PDMUSBHLP g_pdmR3UsbHlp =
1877{
1878 PDM_USBHLP_VERSION,
1879 pdmR3UsbHlp_DriverAttach,
1880 pdmR3UsbHlp_AssertEMT,
1881 pdmR3UsbHlp_AssertOther,
1882 pdmR3UsbHlp_DBGFStopV,
1883 pdmR3UsbHlp_DBGFInfoRegister,
1884 pdmR3UsbHlp_MMHeapAlloc,
1885 pdmR3UsbHlp_MMHeapAllocZ,
1886 pdmR3UsbHlp_PDMQueueCreate,
1887 pdmR3UsbHlp_SSMRegister,
1888 pdmR3UsbHlp_STAMRegisterV,
1889 pdmR3UsbHlp_TMTimerCreate,
1890 pdmR3UsbHlp_VMSetErrorV,
1891 pdmR3UsbHlp_VMSetRuntimeErrorV,
1892 pdmR3UsbHlp_VMState,
1893 pdmR3UsbHlp_ThreadCreate,
1894 pdmR3UsbHlp_SetAsyncNotification,
1895 pdmR3UsbHlp_AsyncNotificationCompleted,
1896 pdmR3UsbHlp_VMGetSuspendReason,
1897 pdmR3UsbHlp_VMGetResumeReason,
1898 NULL,
1899 NULL,
1900 NULL,
1901 NULL,
1902 NULL,
1903 NULL,
1904 NULL,
1905 NULL,
1906 NULL,
1907 NULL,
1908 PDM_USBHLP_VERSION
1909};
1910
1911/** @} */
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