VirtualBox

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

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

USB: Integrate USB sniffer. Make it possible to specify a file to dump the traffic to when attaching a USB device with VBoxManage

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