VirtualBox

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

Last change on this file since 39078 was 39078, checked in by vboxsync, 13 years ago

VMM: -Wunused-parameter

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.6 KB
Line 
1/* $Id: PDMUsb.cpp 39078 2011-10-21 14:18:22Z vboxsync $ */
2/** @file
3 * PDM - Pluggable Device and Driver Manager, USB part.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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/version.h>
32#include <VBox/err.h>
33
34#include <VBox/log.h>
35#include <iprt/assert.h>
36#include <iprt/thread.h>
37#include <iprt/string.h>
38#include <iprt/asm.h>
39#include <iprt/alloc.h>
40#include <iprt/alloca.h>
41#include <iprt/path.h>
42#include <iprt/uuid.h>
43
44
45/*******************************************************************************
46* Structures and Typedefs *
47*******************************************************************************/
48/**
49 * Internal callback structure pointer.
50 *
51 * The main purpose is to define the extra data we associate
52 * with PDMUSBREGCB so we can find the VM instance and so on.
53 */
54typedef struct PDMUSBREGCBINT
55{
56 /** The callback structure. */
57 PDMUSBREGCB Core;
58 /** A bit of padding. */
59 uint32_t u32[4];
60 /** VM Handle. */
61 PVM pVM;
62} PDMUSBREGCBINT, *PPDMUSBREGCBINT;
63typedef const PDMUSBREGCBINT *PCPDMUSBREGCBINT;
64
65
66/*******************************************************************************
67* Defined Constants And Macros *
68*******************************************************************************/
69/** @def PDMUSB_ASSERT_USBINS
70 * Asserts the validity of the USB device instance.
71 */
72#ifdef VBOX_STRICT
73# define PDMUSB_ASSERT_USBINS(pUsbIns) \
74 do { \
75 AssertPtr(pUsbIns); \
76 Assert(pUsbIns->u32Version == PDM_USBINS_VERSION); \
77 Assert(pUsbIns->pvInstanceDataR3 == (void *)&pUsbIns->achInstanceData[0]); \
78 } while (0)
79#else
80# define PDMUSB_ASSERT_USBINS(pUsbIns) do { } while (0)
81#endif
82
83
84/*******************************************************************************
85* Internal Functions *
86*******************************************************************************/
87static void pdmR3UsbDestroyDevice(PVM pVM, PPDMUSBINS pUsbIns);
88
89
90/*******************************************************************************
91* Global Variables *
92*******************************************************************************/
93extern const PDMUSBHLP g_pdmR3UsbHlp;
94
95
96AssertCompile(sizeof(PDMUSBINSINT) <= RT_SIZEOFMEMB(PDMUSBINS, Internal.padding));
97
98
99/**
100 * Registers a USB hub driver.
101 *
102 * @returns VBox status code.
103 * @param pVM The VM handle.
104 * @param pDrvIns The driver instance of the hub.
105 * @param fVersions Indicates the kinds of USB devices that can be attached to this HUB.
106 * @param cPorts The number of ports.
107 * @param pUsbHubReg The hub callback structure that PDMUsb uses to interact with it.
108 * @param ppUsbHubHlp The helper callback structure that the hub uses to talk to PDMUsb.
109 * @thread EMT
110 */
111int pdmR3UsbRegisterHub(PVM pVM, PPDMDRVINS pDrvIns, uint32_t fVersions, uint32_t cPorts, PCPDMUSBHUBREG pUsbHubReg, PPCPDMUSBHUBHLP ppUsbHubHlp)
112{
113 /*
114 * Validate input.
115 */
116 /* The driver must be in the USB class. */
117 if (!(pDrvIns->pReg->fClass & PDM_DRVREG_CLASS_USB))
118 {
119 LogRel(("pdmR3UsbRegisterHub: fClass=%#x expected %#x to be set\n", pDrvIns->pReg->fClass, PDM_DRVREG_CLASS_USB));
120 return VERR_INVALID_PARAMETER;
121 }
122 AssertMsgReturn(!(fVersions & ~(VUSB_STDVER_11 | VUSB_STDVER_20)), ("%#x\n", fVersions), VERR_INVALID_PARAMETER);
123 AssertPtrReturn(ppUsbHubHlp, VERR_INVALID_POINTER);
124 AssertPtrReturn(pUsbHubReg, VERR_INVALID_POINTER);
125 AssertReturn(pUsbHubReg->u32Version == PDM_USBHUBREG_VERSION, VERR_INVALID_MAGIC);
126 AssertReturn(pUsbHubReg->u32TheEnd == PDM_USBHUBREG_VERSION, VERR_INVALID_MAGIC);
127 AssertPtrReturn(pUsbHubReg->pfnAttachDevice, VERR_INVALID_PARAMETER);
128 AssertPtrReturn(pUsbHubReg->pfnDetachDevice, VERR_INVALID_PARAMETER);
129
130 /*
131 * Check for duplicate registration and find the last hub for FIFO registration.
132 */
133 PPDMUSBHUB pPrev = NULL;
134 for (PPDMUSBHUB pCur = pVM->pdm.s.pUsbHubs; pCur; pCur = pCur->pNext)
135 {
136 if (pCur->pDrvIns == pDrvIns)
137 return VERR_PDM_USB_HUB_EXISTS;
138 pPrev = pCur;
139 }
140
141 /*
142 * Create an internal USB hub structure.
143 */
144 PPDMUSBHUB pHub = (PPDMUSBHUB)MMR3HeapAlloc(pVM, MM_TAG_PDM_DRIVER, sizeof(*pHub));
145 if (!pHub)
146 return VERR_NO_MEMORY;
147
148 pHub->fVersions = fVersions;
149 pHub->cPorts = cPorts;
150 pHub->cAvailablePorts = cPorts;
151 pHub->pDrvIns = pDrvIns;
152 pHub->Reg = *pUsbHubReg;
153 pHub->pNext = NULL;
154
155 /* link it */
156 if (pPrev)
157 pPrev->pNext = pHub;
158 else
159 pVM->pdm.s.pUsbHubs = pHub;
160
161 Log(("PDM: Registered USB hub %p/%s\n", pDrvIns, pDrvIns->pReg->szName));
162 return VINF_SUCCESS;
163}
164
165
166/**
167 * Loads one device module and call the registration entry point.
168 *
169 * @returns VBox status code.
170 * @param pVM VM handle.
171 * @param pRegCB The registration callback stuff.
172 * @param pszFilename Module filename.
173 * @param pszName Module name.
174 */
175static int pdmR3UsbLoad(PVM pVM, PCPDMUSBREGCBINT pRegCB, const char *pszFilename, const char *pszName)
176{
177 /*
178 * Load it.
179 */
180 int rc = pdmR3LoadR3U(pVM->pUVM, pszFilename, pszName);
181 if (RT_SUCCESS(rc))
182 {
183 /*
184 * Get the registration export and call it.
185 */
186 FNPDMVBOXUSBREGISTER *pfnVBoxUsbRegister;
187 rc = PDMR3LdrGetSymbolR3(pVM, pszName, "VBoxUsbRegister", (void **)&pfnVBoxUsbRegister);
188 if (RT_SUCCESS(rc))
189 {
190 Log(("PDM: Calling VBoxUsbRegister (%p) of %s (%s)\n", pfnVBoxUsbRegister, pszName, pszFilename));
191 rc = pfnVBoxUsbRegister(&pRegCB->Core, VBOX_VERSION);
192 if (RT_SUCCESS(rc))
193 Log(("PDM: Successfully loaded device module %s (%s).\n", pszName, pszFilename));
194 else
195 AssertMsgFailed(("VBoxDevicesRegister failed with rc=%Rrc for module %s (%s)\n", rc, pszName, pszFilename));
196 }
197 else
198 {
199 AssertMsgFailed(("Failed to locate 'VBoxUsbRegister' in %s (%s) rc=%Rrc\n", pszName, pszFilename, rc));
200 if (rc == VERR_SYMBOL_NOT_FOUND)
201 rc = VERR_PDM_NO_REGISTRATION_EXPORT;
202 }
203 }
204 else
205 AssertMsgFailed(("Failed to load VBoxDD!\n"));
206 return rc;
207}
208
209
210
211/**
212 * @interface_method_impl{PDMUSBREGCB,pfnRegister}
213 */
214static DECLCALLBACK(int) pdmR3UsbReg_Register(PCPDMUSBREGCB pCallbacks, PCPDMUSBREG pReg)
215{
216 /*
217 * Validate the registration structure.
218 */
219 Assert(pReg);
220 AssertMsgReturn(pReg->u32Version == PDM_USBREG_VERSION,
221 ("Unknown struct version %#x!\n", pReg->u32Version),
222 VERR_PDM_UNKNOWN_USBREG_VERSION);
223 AssertMsgReturn( pReg->szName[0]
224 && strlen(pReg->szName) < sizeof(pReg->szName),
225 ("Invalid name '%s'\n", pReg->szName),
226 VERR_PDM_INVALID_USB_REGISTRATION);
227 AssertMsgReturn(pReg->fFlags == 0, ("fFlags=%#x\n", pReg->fFlags), VERR_PDM_INVALID_USB_REGISTRATION);
228 AssertMsgReturn(pReg->cMaxInstances > 0,
229 ("Max instances %u! (USB Device %s)\n", pReg->cMaxInstances, pReg->szName),
230 VERR_PDM_INVALID_USB_REGISTRATION);
231 AssertMsgReturn(pReg->cbInstance <= _1M,
232 ("Instance size %d bytes! (USB Device %s)\n", pReg->cbInstance, pReg->szName),
233 VERR_PDM_INVALID_USB_REGISTRATION);
234 AssertMsgReturn(pReg->pfnConstruct, ("No constructor! (USB Device %s)\n", pReg->szName),
235 VERR_PDM_INVALID_USB_REGISTRATION);
236
237 /*
238 * Check for duplicate and find FIFO entry at the same time.
239 */
240 PCPDMUSBREGCBINT pRegCB = (PCPDMUSBREGCBINT)pCallbacks;
241 PPDMUSB pUsbPrev = NULL;
242 PPDMUSB pUsb = pRegCB->pVM->pdm.s.pUsbDevs;
243 for (; pUsb; pUsbPrev = pUsb, pUsb = pUsb->pNext)
244 AssertMsgReturn(strcmp(pUsb->pReg->szName, pReg->szName),
245 ("USB Device '%s' already exists\n", pReg->szName),
246 VERR_PDM_USB_NAME_CLASH);
247
248 /*
249 * Allocate new device structure and insert it into the list.
250 */
251 pUsb = (PPDMUSB)MMR3HeapAlloc(pRegCB->pVM, MM_TAG_PDM_DEVICE, sizeof(*pUsb));
252 if (pUsb)
253 {
254 pUsb->pNext = NULL;
255 pUsb->iNextInstance = 0;
256 pUsb->pInstances = NULL;
257 pUsb->pReg = pReg;
258 pUsb->cchName = (RTUINT)strlen(pReg->szName);
259
260 if (pUsbPrev)
261 pUsbPrev->pNext = pUsb;
262 else
263 pRegCB->pVM->pdm.s.pUsbDevs = pUsb;
264 Log(("PDM: Registered USB device '%s'\n", pReg->szName));
265 return VINF_SUCCESS;
266 }
267 return VERR_NO_MEMORY;
268}
269
270
271/**
272 * Load USB Device modules.
273 *
274 * This is called by pdmR3DevInit() after it has loaded it's device modules.
275 *
276 * @returns VBox status code.
277 * @param pVM The VM handle.
278 */
279int pdmR3UsbLoadModules(PVM pVM)
280{
281 LogFlow(("pdmR3UsbLoadModules:\n"));
282
283 AssertRelease(!(RT_OFFSETOF(PDMUSBINS, achInstanceData) & 15));
284 AssertRelease(sizeof(pVM->pdm.s.pUsbInstances->Internal.s) <= sizeof(pVM->pdm.s.pUsbInstances->Internal.padding));
285
286 /*
287 * Initialize the callback structure.
288 */
289 PDMUSBREGCBINT RegCB;
290 RegCB.Core.u32Version = PDM_USBREG_CB_VERSION;
291 RegCB.Core.pfnRegister = pdmR3UsbReg_Register;
292 RegCB.pVM = pVM;
293
294 /*
295 * Load the builtin module
296 */
297 PCFGMNODE pUsbNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "PDM/USB/");
298 bool fLoadBuiltin;
299 int rc = CFGMR3QueryBool(pUsbNode, "LoadBuiltin", &fLoadBuiltin);
300 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
301 fLoadBuiltin = true;
302 else if (RT_FAILURE(rc))
303 {
304 AssertMsgFailed(("Configuration error: Querying boolean \"LoadBuiltin\" failed with %Rrc\n", rc));
305 return rc;
306 }
307 if (fLoadBuiltin)
308 {
309 /* make filename */
310 char *pszFilename = pdmR3FileR3("VBoxDD", true /*fShared*/);
311 if (!pszFilename)
312 return VERR_NO_TMP_MEMORY;
313 rc = pdmR3UsbLoad(pVM, &RegCB, pszFilename, "VBoxDD");
314 RTMemTmpFree(pszFilename);
315 if (RT_FAILURE(rc))
316 return rc;
317 }
318
319 /*
320 * Load additional device modules.
321 */
322 PCFGMNODE pCur;
323 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
324 {
325 /*
326 * Get the name and path.
327 */
328 char szName[PDMMOD_NAME_LEN];
329 rc = CFGMR3GetName(pCur, &szName[0], sizeof(szName));
330 if (rc == VERR_CFGM_NOT_ENOUGH_SPACE)
331 {
332 AssertMsgFailed(("configuration error: The module name is too long, cchName=%zu.\n", CFGMR3GetNameLen(pCur)));
333 return VERR_PDM_MODULE_NAME_TOO_LONG;
334 }
335 else if (RT_FAILURE(rc))
336 {
337 AssertMsgFailed(("CFGMR3GetName -> %Rrc.\n", rc));
338 return rc;
339 }
340
341 /* the path is optional, if no path the module name + path is used. */
342 char szFilename[RTPATH_MAX];
343 rc = CFGMR3QueryString(pCur, "Path", &szFilename[0], sizeof(szFilename));
344 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
345 strcpy(szFilename, szName);
346 else if (RT_FAILURE(rc))
347 {
348 AssertMsgFailed(("configuration error: Failure to query the module path, rc=%Rrc.\n", rc));
349 return rc;
350 }
351
352 /* prepend path? */
353 if (!RTPathHavePath(szFilename))
354 {
355 char *psz = pdmR3FileR3(szFilename, false /*fShared*/);
356 if (!psz)
357 return VERR_NO_TMP_MEMORY;
358 size_t cch = strlen(psz) + 1;
359 if (cch > sizeof(szFilename))
360 {
361 RTMemTmpFree(psz);
362 AssertMsgFailed(("Filename too long! cch=%d '%s'\n", cch, psz));
363 return VERR_FILENAME_TOO_LONG;
364 }
365 memcpy(szFilename, psz, cch);
366 RTMemTmpFree(psz);
367 }
368
369 /*
370 * Load the module and register it's devices.
371 */
372 rc = pdmR3UsbLoad(pVM, &RegCB, szFilename, szName);
373 if (RT_FAILURE(rc))
374 return rc;
375 }
376
377 return VINF_SUCCESS;
378}
379
380
381/**
382 * Send the init-complete notification to all the USB devices.
383 *
384 * This is called from pdmR3DevInit() after it has do its notification round.
385 *
386 * @returns VBox status code.
387 * @param pVM The VM handle.
388 */
389int pdmR3UsbVMInitComplete(PVM pVM)
390{
391 for (PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
392 {
393 if (pUsbIns->pReg->pfnVMInitComplete)
394 {
395 int rc = pUsbIns->pReg->pfnVMInitComplete(pUsbIns);
396 if (RT_FAILURE(rc))
397 {
398 AssertMsgFailed(("InitComplete on USB device '%s'/%d failed with rc=%Rrc\n",
399 pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
400 return rc;
401 }
402 }
403 }
404 return VINF_SUCCESS;
405}
406
407
408/**
409 * Lookups a device structure by name.
410 * @internal
411 */
412PPDMUSB pdmR3UsbLookup(PVM pVM, const char *pszName)
413{
414 size_t cchName = strlen(pszName);
415 for (PPDMUSB pUsb = pVM->pdm.s.pUsbDevs; pUsb; pUsb = pUsb->pNext)
416 if ( pUsb->cchName == cchName
417 && !strcmp(pUsb->pReg->szName, pszName))
418 return pUsb;
419 return NULL;
420}
421
422
423/**
424 * Locates a suitable hub for the specified kind of device.
425 *
426 * @returns VINF_SUCCESS and *ppHub on success.
427 * VERR_PDM_NO_USB_HUBS or VERR_PDM_NO_USB_PORTS on failure.
428 * @param pVM The VM handle.
429 * @param iUsbVersion The USB device version.
430 * @param ppHub Where to store the pointer to the USB hub.
431 */
432static int pdmR3UsbFindHub(PVM pVM, uint32_t iUsbVersion, PPDMUSBHUB *ppHub)
433{
434 *ppHub = NULL;
435 if (!pVM->pdm.s.pUsbHubs)
436 return VERR_PDM_NO_USB_HUBS;
437
438 for (PPDMUSBHUB pCur = pVM->pdm.s.pUsbHubs; pCur; pCur = pCur->pNext)
439 if ( pCur->cAvailablePorts > 0
440 && ( (pCur->fVersions & iUsbVersion)
441 || pCur->fVersions == VUSB_STDVER_11))
442 {
443 *ppHub = pCur;
444 if (pCur->fVersions & iUsbVersion)
445 break;
446 }
447 if (*ppHub)
448 return VINF_SUCCESS;
449 return VERR_PDM_NO_USB_PORTS;
450}
451
452
453/**
454 * Creates the device.
455 *
456 * @returns VBox status code.
457 * @param pVM The VM handle.
458 * @param pUsbDev The USB device emulation.
459 * @param iInstance -1 if not called by pdmR3UsbInstantiateDevices().
460 * @param pUuid The UUID for this device.
461 * @param pInstanceNode The instance CFGM node. NULL if not called by pdmR3UsbInstantiateDevices().
462 * @param ppConfig Pointer to the device configuration pointer. This is set to NULL if inserted
463 * into the tree or cleaned up.
464 *
465 * In the pdmR3UsbInstantiateDevices() case (pInstanceNode != NULL) this is
466 * the actual config node and will not be cleaned up.
467 *
468 * @parma iUsbVersion The USB version preferred by the device.
469 */
470static int pdmR3UsbCreateDevice(PVM pVM, PPDMUSBHUB pHub, PPDMUSB pUsbDev, int iInstance, PCRTUUID pUuid,
471 PCFGMNODE pInstanceNode, PCFGMNODE *ppConfig, uint32_t iUsbVersion)
472{
473 const bool fAtRuntime = pInstanceNode == NULL;
474 int rc;
475 NOREF(iUsbVersion);
476
477 /*
478 * If not called by pdmR3UsbInstantiateDevices(), we'll have to fix
479 * the configuration now.
480 */
481 /* USB device node. */
482 PCFGMNODE pDevNode = CFGMR3GetChildF(CFGMR3GetRoot(pVM), "USB/%s/", pUsbDev->pReg->szName);
483 if (!pDevNode)
484 {
485 rc = CFGMR3InsertNodeF(CFGMR3GetRoot(pVM), &pDevNode, "USB/%s/", pUsbDev->pReg->szName);
486 AssertRCReturn(rc, rc);
487 }
488
489 /* The instance node and number. */
490 if (!pInstanceNode)
491 {
492 for (unsigned c = 0; c < _2M; c++)
493 {
494 iInstance = pUsbDev->iNextInstance++;
495 rc = CFGMR3InsertNodeF(pDevNode, &pInstanceNode, "%d/", iInstance);
496 if (rc != VERR_CFGM_NODE_EXISTS)
497 break;
498 }
499 AssertRCReturn(rc, rc);
500 }
501 else
502 {
503 Assert(iInstance >= 0);
504 if (iInstance >= (int)pUsbDev->iNextInstance)
505 pUsbDev->iNextInstance = iInstance + 1;
506 }
507
508 /* The instance config node. */
509 PCFGMNODE pConfigToDelete = NULL;
510 PCFGMNODE pConfig = NULL;
511 if (!ppConfig || !*ppConfig)
512 {
513 rc = CFGMR3InsertNode(pInstanceNode, "Config", &pConfig);
514 AssertRCReturn(rc, rc);
515 }
516 else if (fAtRuntime)
517 {
518 rc = CFGMR3InsertSubTree(pInstanceNode, "Config", *ppConfig, &pConfig);
519 AssertRCReturn(rc, rc);
520 *ppConfig = NULL;
521 pConfigToDelete = pConfig;
522 }
523 else
524 pConfig = *ppConfig;
525 Assert(CFGMR3GetChild(pInstanceNode, "Config") == pConfig);
526
527 /* The global device config node. */
528 PCFGMNODE pGlobalConfig = CFGMR3GetChild(pDevNode, "GlobalConfig");
529 if (!pGlobalConfig)
530 {
531 rc = CFGMR3InsertNode(pDevNode, "GlobalConfig", &pGlobalConfig);
532 if (RT_FAILURE(rc))
533 {
534 CFGMR3RemoveNode(pConfigToDelete);
535 AssertRCReturn(rc, rc);
536 }
537 }
538
539 /*
540 * Allocate the device instance.
541 */
542 size_t cb = RT_OFFSETOF(PDMUSBINS, achInstanceData[pUsbDev->pReg->cbInstance]);
543 cb = RT_ALIGN_Z(cb, 16);
544 PPDMUSBINS pUsbIns;
545 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_USB, cb, (void **)&pUsbIns);
546 if (RT_FAILURE(rc))
547 {
548 AssertMsgFailed(("Failed to allocate %d bytes of instance data for USB device '%s'. rc=%Rrc\n",
549 cb, pUsbDev->pReg->szName, rc));
550 CFGMR3RemoveNode(pConfigToDelete);
551 return rc;
552 }
553
554 /*
555 * Initialize it.
556 */
557 pUsbIns->u32Version = PDM_USBINS_VERSION;
558 //pUsbIns->Internal.s.pNext = NULL;
559 //pUsbIns->Internal.s.pPerDeviceNext = NULL;
560 pUsbIns->Internal.s.pUsbDev = pUsbDev;
561 pUsbIns->Internal.s.pVM = pVM;
562 //pUsbIns->Internal.s.pLuns = NULL;
563 pUsbIns->Internal.s.pCfg = pInstanceNode;
564 pUsbIns->Internal.s.pCfgDelete = pConfigToDelete;
565 pUsbIns->Internal.s.pCfgGlobal = pGlobalConfig;
566 pUsbIns->Internal.s.Uuid = *pUuid;
567 //pUsbIns->Internal.s.pHub = NULL;
568 pUsbIns->Internal.s.iPort = UINT32_MAX; /* to be determined. */
569 pUsbIns->Internal.s.fVMSuspended = true;
570 //pUsbIns->Internal.s.pfnAsyncNotify = NULL;
571 pUsbIns->pHlpR3 = &g_pdmR3UsbHlp;
572 pUsbIns->pReg = pUsbDev->pReg;
573 pUsbIns->pCfg = pConfig;
574 pUsbIns->pCfgGlobal = pGlobalConfig;
575 pUsbIns->iInstance = iInstance;
576 pUsbIns->pvInstanceDataR3 = &pUsbIns->achInstanceData[0];
577 pUsbIns->pszName = RTStrDup(pUsbDev->pReg->szName);
578
579 /*
580 * Link it into all the lists.
581 */
582 /* The global instance FIFO. */
583 PPDMUSBINS pPrev1 = pVM->pdm.s.pUsbInstances;
584 if (!pPrev1)
585 pVM->pdm.s.pUsbInstances = pUsbIns;
586 else
587 {
588 while (pPrev1->Internal.s.pNext)
589 {
590 Assert(pPrev1->u32Version == PDM_USBINS_VERSION);
591 pPrev1 = pPrev1->Internal.s.pNext;
592 }
593 pPrev1->Internal.s.pNext = pUsbIns;
594 }
595
596 /* The per device instance FIFO. */
597 PPDMUSBINS pPrev2 = pUsbDev->pInstances;
598 if (!pPrev2)
599 pUsbDev->pInstances = pUsbIns;
600 else
601 {
602 while (pPrev2->Internal.s.pPerDeviceNext)
603 {
604 Assert(pPrev2->u32Version == PDM_USBINS_VERSION);
605 pPrev2 = pPrev2->Internal.s.pPerDeviceNext;
606 }
607 pPrev2->Internal.s.pPerDeviceNext = pUsbIns;
608 }
609
610 /*
611 * Call the constructor.
612 */
613 Log(("PDM: Constructing USB device '%s' instance %d...\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
614 rc = pUsbIns->pReg->pfnConstruct(pUsbIns, pUsbIns->iInstance, pUsbIns->pCfg, pUsbIns->pCfgGlobal);
615 if (RT_SUCCESS(rc))
616 {
617 /*
618 * Attach it to the hub.
619 */
620 Log(("PDM: Attaching it...\n"));
621 rc = pHub->Reg.pfnAttachDevice(pHub->pDrvIns, pUsbIns, &pUsbIns->Internal.s.iPort);
622 if (RT_SUCCESS(rc))
623 {
624 pHub->cAvailablePorts--;
625 Assert((int32_t)pHub->cAvailablePorts >= 0 && pHub->cAvailablePorts < pHub->cPorts);
626 pUsbIns->Internal.s.pHub = pHub;
627
628 /* Send the hot-plugged notification if applicable. */
629 if (fAtRuntime && pUsbIns->pReg->pfnHotPlugged)
630 pUsbIns->pReg->pfnHotPlugged(pUsbIns);
631
632 Log(("PDM: Successfully attached USB device '%s' instance %d to hub %p\n",
633 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub));
634 return VINF_SUCCESS;
635 }
636
637 LogRel(("PDM: Failed to attach USB device '%s' instance %d to hub %p: %Rrc\n",
638 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
639 }
640 else
641 {
642 AssertMsgFailed(("Failed to construct '%s'/%d! %Rra\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
643 if (rc == VERR_VERSION_MISMATCH)
644 rc = VERR_PDM_DRIVER_VERSION_MISMATCH;
645 }
646 if (fAtRuntime)
647 pdmR3UsbDestroyDevice(pVM, pUsbIns);
648 /* else: destructors are invoked later. */
649 return rc;
650}
651
652
653/**
654 * Instantiate USB devices.
655 *
656 * This is called by pdmR3DevInit() after it has instantiated the
657 * other devices and their drivers. If there aren't any hubs
658 * around, we'll silently skip the USB devices.
659 *
660 * @returns VBox status code.
661 * @param pVM
662 */
663int pdmR3UsbInstantiateDevices(PVM pVM)
664{
665 /*
666 * Any hubs?
667 */
668 if (!pVM->pdm.s.pUsbHubs)
669 {
670 Log(("PDM: No USB hubs, skipping USB device instantiation.\n"));
671 return VINF_SUCCESS;
672 }
673
674 /*
675 * Count the device instances.
676 */
677 PCFGMNODE pCur;
678 PCFGMNODE pUsbNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "USB/");
679 PCFGMNODE pInstanceNode;
680 unsigned cUsbDevs = 0;
681 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
682 {
683 PCFGMNODE pGlobal = CFGMR3GetChild(pCur, "GlobalConfig/");
684 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
685 if (pInstanceNode != pGlobal)
686 cUsbDevs++;
687 }
688 if (!cUsbDevs)
689 {
690 Log(("PDM: No USB devices were configured!\n"));
691 return VINF_SUCCESS;
692 }
693 Log2(("PDM: cUsbDevs=%d!\n", cUsbDevs));
694
695 /*
696 * Collect info on each USB device instance.
697 */
698 struct USBDEVORDER
699 {
700 /** Configuration node. */
701 PCFGMNODE pNode;
702 /** Pointer to the USB device. */
703 PPDMUSB pUsbDev;
704 /** Init order. */
705 uint32_t u32Order;
706 /** VBox instance number. */
707 uint32_t iInstance;
708 } *paUsbDevs = (struct USBDEVORDER *)alloca(sizeof(paUsbDevs[0]) * (cUsbDevs + 1)); /* (One extra for swapping) */
709 Assert(paUsbDevs);
710 int rc;
711 unsigned i = 0;
712 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
713 {
714 /* Get the device name. */
715 char szName[sizeof(paUsbDevs[0].pUsbDev->pReg->szName)];
716 rc = CFGMR3GetName(pCur, szName, sizeof(szName));
717 AssertMsgRCReturn(rc, ("Configuration error: device name is too long (or something)! rc=%Rrc\n", rc), rc);
718
719 /* Find the device. */
720 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, szName);
721 AssertMsgReturn(pUsbDev, ("Configuration error: device '%s' not found!\n", szName), VERR_PDM_DEVICE_NOT_FOUND);
722
723 /* Configured priority or use default? */
724 uint32_t u32Order;
725 rc = CFGMR3QueryU32(pCur, "Priority", &u32Order);
726 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
727 u32Order = i << 4;
728 else
729 AssertMsgRCReturn(rc, ("Configuration error: reading \"Priority\" for the '%s' USB device failed rc=%Rrc!\n", szName, rc), rc);
730
731 /* Global config. */
732 PCFGMNODE pGlobal = CFGMR3GetChild(pCur, "GlobalConfig/");
733 if (!pGlobal)
734 {
735 rc = CFGMR3InsertNode(pCur, "GlobalConfig/", &pGlobal);
736 AssertMsgRCReturn(rc, ("Failed to create GlobalConfig node! rc=%Rrc\n", rc), rc);
737 CFGMR3SetRestrictedRoot(pGlobal);
738 }
739
740 /* Enumerate the device instances. */
741 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
742 {
743 if (pInstanceNode == pGlobal)
744 continue;
745
746 paUsbDevs[i].pNode = pInstanceNode;
747 paUsbDevs[i].pUsbDev = pUsbDev;
748 paUsbDevs[i].u32Order = u32Order;
749
750 /* Get the instance number. */
751 char szInstance[32];
752 rc = CFGMR3GetName(pInstanceNode, szInstance, sizeof(szInstance));
753 AssertMsgRCReturn(rc, ("Configuration error: instance name is too long (or something)! rc=%Rrc\n", rc), rc);
754 char *pszNext = NULL;
755 rc = RTStrToUInt32Ex(szInstance, &pszNext, 0, &paUsbDevs[i].iInstance);
756 AssertMsgRCReturn(rc, ("Configuration error: RTStrToInt32Ex failed on the instance name '%s'! rc=%Rrc\n", szInstance, rc), rc);
757 AssertMsgReturn(!*pszNext, ("Configuration error: the instance name '%s' isn't all digits. (%s)\n", szInstance, pszNext), VERR_INVALID_PARAMETER);
758
759 /* next instance */
760 i++;
761 }
762 } /* devices */
763 Assert(i == cUsbDevs);
764
765 /*
766 * Sort the device array ascending on u32Order. (bubble)
767 */
768 unsigned c = cUsbDevs - 1;
769 while (c)
770 {
771 unsigned j = 0;
772 for (i = 0; i < c; i++)
773 if (paUsbDevs[i].u32Order > paUsbDevs[i + 1].u32Order)
774 {
775 paUsbDevs[cUsbDevs] = paUsbDevs[i + 1];
776 paUsbDevs[i + 1] = paUsbDevs[i];
777 paUsbDevs[i] = paUsbDevs[cUsbDevs];
778 j = i;
779 }
780 c = j;
781 }
782
783 /*
784 * Instantiate the devices.
785 */
786 for (i = 0; i < cUsbDevs; i++)
787 {
788 /*
789 * Make sure there is a config node and mark it as restricted.
790 */
791 PCFGMNODE pConfigNode = CFGMR3GetChild(paUsbDevs[i].pNode, "Config/");
792 if (!pConfigNode)
793 {
794 rc = CFGMR3InsertNode(paUsbDevs[i].pNode, "Config", &pConfigNode);
795 AssertMsgRCReturn(rc, ("Failed to create Config node! rc=%Rrc\n", rc), rc);
796 }
797 CFGMR3SetRestrictedRoot(pConfigNode);
798
799 /** @todo
800 * Figure out the USB version from the USB device registration and the configuration.
801 */
802 uint32_t iUsbVersion = VUSB_STDVER_11;
803
804 /*
805 * Find a suitable hub with free ports.
806 */
807 PPDMUSBHUB pHub;
808 rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
809 if (RT_FAILURE(rc))
810 {
811 Log(("pdmR3UsbFindHub failed %Rrc\n", rc));
812 return rc;
813 }
814
815 /*
816 * Create and attach the device.
817 */
818 RTUUID Uuid;
819 rc = RTUuidCreate(&Uuid);
820 AssertRCReturn(rc, rc);
821 rc = pdmR3UsbCreateDevice(pVM, pHub, paUsbDevs[i].pUsbDev, paUsbDevs[i].iInstance, &Uuid, paUsbDevs[i].pNode, &pConfigNode, iUsbVersion);
822 if (RT_FAILURE(rc))
823 return rc;
824 } /* for device instances */
825
826 return VINF_SUCCESS;
827}
828
829
830/**
831 * Creates a USB proxy device instance.
832 *
833 * This will find an appropriate HUB for the USB device, create the necessary CFGM stuff
834 * and try instantiate the proxy device.
835 *
836 * @returns VBox status code.
837 * @param pVM The VM handle.
838 * @param pUuid The UUID to be associated with the device.
839 * @param fRemote Whether it's a remove or local device.
840 * @param pszAddress The address string.
841 * @param pvBackend Pointer to the backend.
842 * @param iUsbVersion The preferred USB version.
843 * @param fMaskedIfs The interfaces to hide from the guest.
844 */
845VMMR3DECL(int) PDMR3USBCreateProxyDevice(PVM pVM, PCRTUUID pUuid, bool fRemote, const char *pszAddress, void *pvBackend,
846 uint32_t iUsbVersion, uint32_t fMaskedIfs)
847{
848 /*
849 * Validate input.
850 */
851 VM_ASSERT_EMT(pVM);
852 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
853 AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
854 AssertReturn( iUsbVersion == VUSB_STDVER_20
855 || iUsbVersion == VUSB_STDVER_11, VERR_INVALID_PARAMETER);
856
857 /*
858 * Find the USBProxy driver.
859 */
860 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, "USBProxy");
861 if (!pUsbDev)
862 {
863 LogRel(("PDMR3USBCreateProxyDevice: The USBProxy device class wasn't found\n"));
864 return VERR_PDM_NO_USBPROXY;
865 }
866
867 /*
868 * Find a suitable hub with free ports.
869 */
870 PPDMUSBHUB pHub;
871 int rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
872 if (RT_FAILURE(rc))
873 {
874 Log(("pdmR3UsbFindHub: failed %Rrc\n", rc));
875 return rc;
876 }
877
878 /*
879 * Create the CFGM configuration node.
880 */
881 PCFGMNODE pConfig = CFGMR3CreateTree(pVM);
882 AssertReturn(pConfig, VERR_NO_MEMORY);
883 do /* break loop */
884 {
885 rc = CFGMR3InsertString(pConfig, "Address", pszAddress); AssertRCBreak(rc);
886 char szUuid[RTUUID_STR_LENGTH];
887 rc = RTUuidToStr(pUuid, &szUuid[0], sizeof(szUuid)); AssertRCBreak(rc);
888 rc = CFGMR3InsertString(pConfig, "UUID", szUuid); AssertRCBreak(rc);
889 rc = CFGMR3InsertInteger(pConfig, "Remote", fRemote); AssertRCBreak(rc);
890 rc = CFGMR3InsertInteger(pConfig, "USBVersion", iUsbVersion); AssertRCBreak(rc);
891 rc = CFGMR3InsertInteger(pConfig, "pvBackend", (uintptr_t)pvBackend); AssertRCBreak(rc);
892 rc = CFGMR3InsertInteger(pConfig, "MaskedIfs", fMaskedIfs); AssertRCBreak(rc);
893 rc = CFGMR3InsertInteger(pConfig, "Force11Device", !(pHub->fVersions & iUsbVersion)); AssertRCBreak(rc);
894 } while (0); /* break loop */
895 if (RT_FAILURE(rc))
896 {
897 CFGMR3RemoveNode(pConfig);
898 LogRel(("PDMR3USBCreateProxyDevice: failed to setup CFGM config, rc=%Rrc\n", rc));
899 return rc;
900 }
901
902 /*
903 * Finally, try create it.
904 */
905 rc = pdmR3UsbCreateDevice(pVM, pHub, pUsbDev, -1, pUuid, NULL, &pConfig, iUsbVersion);
906 if (RT_FAILURE(rc) && pConfig)
907 CFGMR3RemoveNode(pConfig);
908 return rc;
909}
910
911
912/**
913 * Destroys a hot-plugged USB device.
914 *
915 * The device must be detached from the HUB at this point.
916 *
917 * @param pVM Pointer to the shared VM structure.
918 * @param pUsbIns The USB device instance to destroy.
919 * @thread EMT
920 */
921static void pdmR3UsbDestroyDevice(PVM pVM, PPDMUSBINS pUsbIns)
922{
923 Assert(!pUsbIns->Internal.s.pHub);
924
925 /*
926 * Do the unplug notification.
927 */
928 /** @todo what about the drivers? */
929 if (pUsbIns->pReg->pfnHotUnplugged)
930 pUsbIns->pReg->pfnHotUnplugged(pUsbIns);
931
932 /*
933 * Destroy the luns with their driver chains and call the device destructor.
934 */
935 while (pUsbIns->Internal.s.pLuns)
936 {
937 PPDMLUN pLun = pUsbIns->Internal.s.pLuns;
938 pUsbIns->Internal.s.pLuns = pLun->pNext;
939 if (pLun->pTop)
940 pdmR3DrvDestroyChain(pLun->pTop, PDM_TACH_FLAGS_NOT_HOT_PLUG); /* Hotplugging is handled differently here atm. */
941 MMR3HeapFree(pLun);
942 }
943
944 /* finally, the device. */
945 if (pUsbIns->pReg->pfnDestruct)
946 {
947 Log(("PDM: Destructing USB device '%s' instance %d...\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
948 pUsbIns->pReg->pfnDestruct(pUsbIns);
949 }
950 TMR3TimerDestroyUsb(pVM, pUsbIns);
951 //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
952 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
953
954 /*
955 * Unlink it.
956 */
957 /* The global instance FIFO. */
958 if (pVM->pdm.s.pUsbInstances == pUsbIns)
959 pVM->pdm.s.pUsbInstances = pUsbIns->Internal.s.pNext;
960 else
961 {
962 PPDMUSBINS pPrev = pVM->pdm.s.pUsbInstances;
963 while (pPrev && pPrev->Internal.s.pNext != pUsbIns)
964 {
965 Assert(pPrev->u32Version == PDM_USBINS_VERSION);
966 pPrev = pPrev->Internal.s.pNext;
967 }
968 Assert(pPrev); Assert(pPrev != pUsbIns);
969 if (pPrev)
970 pPrev->Internal.s.pNext = pUsbIns->Internal.s.pNext;
971 }
972
973 /* The per device instance FIFO. */
974 PPDMUSB pUsbDev = pUsbIns->Internal.s.pUsbDev;
975 if (pUsbDev->pInstances == pUsbIns)
976 pUsbDev->pInstances = pUsbIns->Internal.s.pPerDeviceNext;
977 else
978 {
979 PPDMUSBINS pPrev = pUsbDev->pInstances;
980 while (pPrev && pPrev->Internal.s.pPerDeviceNext != pUsbIns)
981 {
982 Assert(pPrev->u32Version == PDM_USBINS_VERSION);
983 pPrev = pPrev->Internal.s.pPerDeviceNext;
984 }
985 Assert(pPrev); Assert(pPrev != pUsbIns);
986 if (pPrev)
987 pPrev->Internal.s.pPerDeviceNext = pUsbIns->Internal.s.pPerDeviceNext;
988 }
989
990 /*
991 * Trash it.
992 */
993 pUsbIns->u32Version = 0;
994 pUsbIns->pReg = NULL;
995 if (pUsbIns->pszName)
996 {
997 RTStrFree(pUsbIns->pszName);
998 pUsbIns->pszName = NULL;
999 }
1000 CFGMR3RemoveNode(pUsbIns->Internal.s.pCfgDelete);
1001 MMR3HeapFree(pUsbIns);
1002}
1003
1004
1005/**
1006 * Detaches and destroys a USB device.
1007 *
1008 * @returns VBox status code.
1009 * @param pVM The VM handle.
1010 * @param pUuid The UUID associated with the device to detach.
1011 * @thread EMT
1012 */
1013VMMR3DECL(int) PDMR3USBDetachDevice(PVM pVM, PCRTUUID pUuid)
1014{
1015 /*
1016 * Validate input.
1017 */
1018 VM_ASSERT_EMT(pVM);
1019 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
1020 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
1021
1022 /*
1023 * Search the global list for it.
1024 */
1025 PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances;
1026 for ( ; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1027 if (!RTUuidCompare(&pUsbIns->Internal.s.Uuid, pUuid))
1028 break;
1029 if (!pUsbIns)
1030 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND; /** @todo VERR_PDM_USB_INSTANCE_NOT_FOUND */
1031
1032 /*
1033 * Detach it from the HUB (if it's actually attached to one).
1034 */
1035 PPDMUSBHUB pHub = pUsbIns->Internal.s.pHub;
1036 if (pHub)
1037 {
1038 int rc = pHub->Reg.pfnDetachDevice(pHub->pDrvIns, pUsbIns, pUsbIns->Internal.s.iPort);
1039 if (RT_FAILURE(rc))
1040 {
1041 LogRel(("PDM: Failed to detach USB device '%s' instance %d from %p: %Rrc\n",
1042 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
1043 return rc;
1044 }
1045
1046 pHub->cAvailablePorts++;
1047 Assert(pHub->cAvailablePorts > 0 && pHub->cAvailablePorts <= pHub->cPorts);
1048 pUsbIns->Internal.s.pHub = NULL;
1049 }
1050
1051 /*
1052 * Notify about unplugging and destroy the device with it's drivers.
1053 */
1054 pdmR3UsbDestroyDevice(pVM, pUsbIns);
1055
1056 return VINF_SUCCESS;
1057}
1058
1059
1060/**
1061 * Checks if there are any USB hubs attached.
1062 *
1063 * @returns true / false accordingly.
1064 * @param pVM Pointer to the shared VM structure.
1065 */
1066VMMR3DECL(bool) PDMR3USBHasHub(PVM pVM)
1067{
1068 return pVM->pdm.s.pUsbHubs != NULL;
1069}
1070
1071
1072/** @name USB Device Helpers
1073 * @{
1074 */
1075
1076/** @interface_method_impl{PDMUSBHLPR3,pfnDriverAttach} */
1077static DECLCALLBACK(int) pdmR3UsbHlp_DriverAttach(PPDMUSBINS pUsbIns, RTUINT iLun, PPDMIBASE pBaseInterface, PPDMIBASE *ppBaseInterface, const char *pszDesc)
1078{
1079 PDMUSB_ASSERT_USBINS(pUsbIns);
1080 PVM pVM = pUsbIns->Internal.s.pVM;
1081 VM_ASSERT_EMT(pVM);
1082 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: iLun=%d pBaseInterface=%p ppBaseInterface=%p pszDesc=%p:{%s}\n",
1083 pUsbIns->pReg->szName, pUsbIns->iInstance, iLun, pBaseInterface, ppBaseInterface, pszDesc, pszDesc));
1084
1085 /*
1086 * Lookup the LUN, it might already be registered.
1087 */
1088 PPDMLUN pLunPrev = NULL;
1089 PPDMLUN pLun = pUsbIns->Internal.s.pLuns;
1090 for (; pLun; pLunPrev = pLun, pLun = pLun->pNext)
1091 if (pLun->iLun == iLun)
1092 break;
1093
1094 /*
1095 * Create the LUN if if wasn't found, else check if driver is already attached to it.
1096 */
1097 if (!pLun)
1098 {
1099 if ( !pBaseInterface
1100 || !pszDesc
1101 || !*pszDesc)
1102 {
1103 Assert(pBaseInterface);
1104 Assert(pszDesc || *pszDesc);
1105 return VERR_INVALID_PARAMETER;
1106 }
1107
1108 pLun = (PPDMLUN)MMR3HeapAlloc(pVM, MM_TAG_PDM_LUN, sizeof(*pLun));
1109 if (!pLun)
1110 return VERR_NO_MEMORY;
1111
1112 pLun->iLun = iLun;
1113 pLun->pNext = pLunPrev ? pLunPrev->pNext : NULL;
1114 pLun->pTop = NULL;
1115 pLun->pBottom = NULL;
1116 pLun->pDevIns = NULL;
1117 pLun->pUsbIns = pUsbIns;
1118 pLun->pszDesc = pszDesc;
1119 pLun->pBase = pBaseInterface;
1120 if (!pLunPrev)
1121 pUsbIns->Internal.s.pLuns = pLun;
1122 else
1123 pLunPrev->pNext = pLun;
1124 Log(("pdmR3UsbHlp_DriverAttach: Registered LUN#%d '%s' with device '%s'/%d.\n",
1125 iLun, pszDesc, pUsbIns->pReg->szName, pUsbIns->iInstance));
1126 }
1127 else if (pLun->pTop)
1128 {
1129 AssertMsgFailed(("Already attached! The device should keep track of such things!\n"));
1130 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, VERR_PDM_DRIVER_ALREADY_ATTACHED));
1131 return VERR_PDM_DRIVER_ALREADY_ATTACHED;
1132 }
1133 Assert(pLun->pBase == pBaseInterface);
1134
1135
1136 /*
1137 * Get the attached driver configuration.
1138 */
1139 int rc;
1140 PCFGMNODE pNode = CFGMR3GetChildF(pUsbIns->Internal.s.pCfg, "LUN#%u", iLun);
1141 if (pNode)
1142 rc = pdmR3DrvInstantiate(pVM, pNode, pBaseInterface, NULL /*pDrvAbove*/, pLun, ppBaseInterface);
1143 else
1144 rc = VERR_PDM_NO_ATTACHED_DRIVER;
1145
1146
1147 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1148 return rc;
1149}
1150
1151
1152/** @interface_method_impl{PDMUSBHLP,pfnAssertEMT} */
1153static DECLCALLBACK(bool) pdmR3UsbHlp_AssertEMT(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1154{
1155 PDMUSB_ASSERT_USBINS(pUsbIns);
1156 if (VM_IS_EMT(pUsbIns->Internal.s.pVM))
1157 return true;
1158
1159 char szMsg[100];
1160 RTStrPrintf(szMsg, sizeof(szMsg), "AssertEMT '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance);
1161 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1162 AssertBreakpoint();
1163 return false;
1164}
1165
1166
1167/** @interface_method_impl{PDMUSBHLP,pfnAssertOther} */
1168static DECLCALLBACK(bool) pdmR3UsbHlp_AssertOther(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1169{
1170 PDMUSB_ASSERT_USBINS(pUsbIns);
1171 if (!VM_IS_EMT(pUsbIns->Internal.s.pVM))
1172 return true;
1173
1174 char szMsg[100];
1175 RTStrPrintf(szMsg, sizeof(szMsg), "AssertOther '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance);
1176 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1177 AssertBreakpoint();
1178 return false;
1179}
1180
1181
1182/** @interface_method_impl{PDMUSBHLP,pfnDBGFStopV} */
1183static DECLCALLBACK(int) pdmR3UsbHlp_DBGFStopV(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction, const char *pszFormat, va_list args)
1184{
1185 PDMUSB_ASSERT_USBINS(pUsbIns);
1186#ifdef LOG_ENABLED
1187 va_list va2;
1188 va_copy(va2, args);
1189 LogFlow(("pdmR3UsbHlp_DBGFStopV: caller='%s'/%d: pszFile=%p:{%s} iLine=%d pszFunction=%p:{%s} pszFormat=%p:{%s} (%N)\n",
1190 pUsbIns->pReg->szName, pUsbIns->iInstance, pszFile, pszFile, iLine, pszFunction, pszFunction, pszFormat, pszFormat, pszFormat, &va2));
1191 va_end(va2);
1192#endif
1193
1194 PVM pVM = pUsbIns->Internal.s.pVM;
1195 VM_ASSERT_EMT(pVM);
1196 int rc = DBGFR3EventSrcV(pVM, DBGFEVENT_DEV_STOP, pszFile, iLine, pszFunction, pszFormat, args);
1197 if (rc == VERR_DBGF_NOT_ATTACHED)
1198 rc = VINF_SUCCESS;
1199
1200 LogFlow(("pdmR3UsbHlp_DBGFStopV: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1201 return rc;
1202}
1203
1204
1205/** @interface_method_impl{PDMUSBHLP,pfnDBGFInfoRegister} */
1206static DECLCALLBACK(int) pdmR3UsbHlp_DBGFInfoRegister(PPDMUSBINS pUsbIns, const char *pszName, const char *pszDesc, PFNDBGFHANDLERUSB pfnHandler)
1207{
1208 PDMUSB_ASSERT_USBINS(pUsbIns);
1209 LogFlow(("pdmR3UsbHlp_DBGFInfoRegister: caller='%s'/%d: pszName=%p:{%s} pszDesc=%p:{%s} pfnHandler=%p\n",
1210 pUsbIns->pReg->szName, pUsbIns->iInstance, pszName, pszName, pszDesc, pszDesc, pfnHandler));
1211
1212 PVM pVM = pUsbIns->Internal.s.pVM;
1213 VM_ASSERT_EMT(pVM);
1214 NOREF(pVM); /** @todo int rc = DBGFR3InfoRegisterUsb(pVM, pszName, pszDesc, pfnHandler, pUsbIns); */
1215 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1216
1217 LogFlow(("pdmR3UsbHlp_DBGFInfoRegister: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1218 return rc;
1219}
1220
1221
1222/** @interface_method_impl{PDMUSBHLP,pfnMMHeapAlloc} */
1223static DECLCALLBACK(void *) pdmR3UsbHlp_MMHeapAlloc(PPDMUSBINS pUsbIns, size_t cb)
1224{
1225 PDMUSB_ASSERT_USBINS(pUsbIns);
1226 LogFlow(("pdmR3UsbHlp_MMHeapAlloc: caller='%s'/%d: cb=%#x\n", pUsbIns->pReg->szName, pUsbIns->iInstance, cb));
1227
1228 void *pv = MMR3HeapAlloc(pUsbIns->Internal.s.pVM, MM_TAG_PDM_USB_USER, cb);
1229
1230 LogFlow(("pdmR3UsbHlp_MMHeapAlloc: caller='%s'/%d: returns %p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pv));
1231 return pv;
1232}
1233
1234
1235/** @interface_method_impl{PDMUSBHLP,pfnMMHeapAllocZ} */
1236static DECLCALLBACK(void *) pdmR3UsbHlp_MMHeapAllocZ(PPDMUSBINS pUsbIns, size_t cb)
1237{
1238 PDMUSB_ASSERT_USBINS(pUsbIns);
1239 LogFlow(("pdmR3UsbHlp_MMHeapAllocZ: caller='%s'/%d: cb=%#x\n", pUsbIns->pReg->szName, pUsbIns->iInstance, cb));
1240
1241 void *pv = MMR3HeapAllocZ(pUsbIns->Internal.s.pVM, MM_TAG_PDM_USB_USER, cb);
1242
1243 LogFlow(("pdmR3UsbHlp_MMHeapAllocZ: caller='%s'/%d: returns %p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pv));
1244 return pv;
1245}
1246
1247
1248/** @interface_method_impl{PDMUSBHLP,pfnPDMQueueCreate} */
1249static DECLCALLBACK(int) pdmR3UsbHlp_PDMQueueCreate(PPDMUSBINS pUsbIns, RTUINT cbItem, RTUINT cItems, uint32_t cMilliesInterval,
1250 PFNPDMQUEUEUSB pfnCallback, const char *pszName, PPDMQUEUE *ppQueue)
1251{
1252 PDMUSB_ASSERT_USBINS(pUsbIns);
1253 LogFlow(("pdmR3UsbHlp_PDMQueueCreate: caller='%s'/%d: cbItem=%#x cItems=%#x cMilliesInterval=%u pfnCallback=%p pszName=%p:{%s} ppQueue=%p\n",
1254 pUsbIns->pReg->szName, pUsbIns->iInstance, cbItem, cItems, cMilliesInterval, pfnCallback, pszName, pszName, ppQueue));
1255
1256 PVM pVM = pUsbIns->Internal.s.pVM;
1257 VM_ASSERT_EMT(pVM);
1258
1259 if (pUsbIns->iInstance > 0)
1260 {
1261 pszName = MMR3HeapAPrintf(pVM, MM_TAG_PDM_DEVICE_DESC, "%s_%u", pszName, pUsbIns->iInstance);
1262 AssertLogRelReturn(pszName, VERR_NO_MEMORY);
1263 }
1264
1265 /** @todo int rc = PDMR3QueueCreateUsb(pVM, pUsbIns, cbItem, cItems, cMilliesInterval, pfnCallback, fGCEnabled, pszName, ppQueue); */
1266 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1267
1268 LogFlow(("pdmR3UsbHlp_PDMQueueCreate: caller='%s'/%d: returns %Rrc *ppQueue=%p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc, *ppQueue));
1269 return rc;
1270}
1271
1272
1273/** @interface_method_impl{PDMUSBHLP,pfnSSMRegister} */
1274static DECLCALLBACK(int) pdmR3UsbHlp_SSMRegister(PPDMUSBINS pUsbIns, uint32_t uVersion, size_t cbGuess,
1275 PFNSSMUSBLIVEPREP pfnLivePrep, PFNSSMUSBLIVEEXEC pfnLiveExec, PFNSSMUSBLIVEVOTE pfnLiveVote,
1276 PFNSSMUSBSAVEPREP pfnSavePrep, PFNSSMUSBSAVEEXEC pfnSaveExec, PFNSSMUSBSAVEDONE pfnSaveDone,
1277 PFNSSMUSBLOADPREP pfnLoadPrep, PFNSSMUSBLOADEXEC pfnLoadExec, PFNSSMUSBLOADDONE pfnLoadDone)
1278{
1279 PDMUSB_ASSERT_USBINS(pUsbIns);
1280 VM_ASSERT_EMT(pUsbIns->Internal.s.pVM);
1281 LogFlow(("pdmR3UsbHlp_SSMRegister: caller='%s'/%d: uVersion=#x cbGuess=%#x\n"
1282 " pfnLivePrep=%p pfnLiveExec=%p pfnLiveVote=%p pfnSavePrep=%p pfnSaveExec=%p pfnSaveDone=%p pszLoadPrep=%p pfnLoadExec=%p pfnLoadDone=%p\n",
1283 pUsbIns->pReg->szName, pUsbIns->iInstance, uVersion, cbGuess,
1284 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1285 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1286 pfnLoadPrep, pfnLoadExec, pfnLoadDone));
1287
1288 /** @todo
1289 int rc = SSMR3RegisterUsb(pUsbIns->Internal.s.pVM, pUsbIns, pUsbIns->pReg->szName, pUsbIns->iInstance,
1290 uVersion, cbGuess,
1291 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1292 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1293 pfnLoadPrep, pfnLoadExec, pfnLoadDone); */
1294 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1295
1296 LogFlow(("pdmR3UsbHlp_SSMRegister: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1297 return rc;
1298}
1299
1300
1301/** @interface_method_impl{PDMUSBHLP,pfnSTAMRegisterV} */
1302static DECLCALLBACK(void) pdmR3UsbHlp_STAMRegisterV(PPDMUSBINS pUsbIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
1303 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list args)
1304{
1305 PDMUSB_ASSERT_USBINS(pUsbIns);
1306 PVM pVM = pUsbIns->Internal.s.pVM;
1307 VM_ASSERT_EMT(pVM);
1308
1309 int rc = STAMR3RegisterV(pVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
1310 AssertRC(rc);
1311
1312 NOREF(pVM);
1313}
1314
1315
1316/** @interface_method_impl{PDMUSBHLP,pfnTMTimerCreate} */
1317static DECLCALLBACK(int) pdmR3UsbHlp_TMTimerCreate(PPDMUSBINS pUsbIns, TMCLOCK enmClock, PFNTMTIMERUSB pfnCallback, void *pvUser,
1318 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1319{
1320 PDMUSB_ASSERT_USBINS(pUsbIns);
1321 PVM pVM = pUsbIns->Internal.s.pVM;
1322 VM_ASSERT_EMT(pVM);
1323 LogFlow(("pdmR3UsbHlp_TMTimerCreate: caller='%s'/%d: enmClock=%d pfnCallback=%p pvUser=%p fFlags=%#x pszDesc=%p:{%s} ppTimer=%p\n",
1324 pUsbIns->pReg->szName, pUsbIns->iInstance, enmClock, pfnCallback, pvUser, fFlags, pszDesc, pszDesc, ppTimer));
1325
1326 if (pUsbIns->iInstance > 0) /** @todo use a string cache here later. */
1327 {
1328 char *pszDesc2 = MMR3HeapAPrintf(pVM, MM_TAG_PDM_USB_DESC, "%s [%u]", pszDesc, pUsbIns->iInstance);
1329 if (pszDesc2)
1330 pszDesc = pszDesc2;
1331 }
1332
1333 int rc = TMR3TimerCreateUsb(pVM, pUsbIns, enmClock, pfnCallback, pvUser, fFlags, pszDesc, ppTimer);
1334
1335 LogFlow(("pdmR3UsbHlp_TMTimerCreate: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1336 return rc;
1337}
1338
1339
1340/** @interface_method_impl{PDMUSBHLP,pfnVMSetErrorV} */
1341static DECLCALLBACK(int) pdmR3UsbHlp_VMSetErrorV(PPDMUSBINS pUsbIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
1342{
1343 PDMUSB_ASSERT_USBINS(pUsbIns);
1344 int rc2 = VMSetErrorV(pUsbIns->Internal.s.pVM, rc, RT_SRC_POS_ARGS, pszFormat, va); Assert(rc2 == rc); NOREF(rc2);
1345 return rc;
1346}
1347
1348
1349/** @interface_method_impl{PDMUSBHLP,pfnVMSetRuntimeErrorV} */
1350static DECLCALLBACK(int) pdmR3UsbHlp_VMSetRuntimeErrorV(PPDMUSBINS pUsbIns, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
1351{
1352 PDMUSB_ASSERT_USBINS(pUsbIns);
1353 int rc = VMSetRuntimeErrorV(pUsbIns->Internal.s.pVM, fFlags, pszErrorId, pszFormat, va);
1354 return rc;
1355}
1356
1357
1358/** @interface_method_impl{PDMUSBHLP,pfnVMState} */
1359static DECLCALLBACK(VMSTATE) pdmR3UsbHlp_VMState(PPDMUSBINS pUsbIns)
1360{
1361 PDMUSB_ASSERT_USBINS(pUsbIns);
1362
1363 VMSTATE enmVMState = VMR3GetState(pUsbIns->Internal.s.pVM);
1364
1365 LogFlow(("pdmR3UsbHlp_VMState: caller='%s'/%d: returns %d (%s)\n", pUsbIns->pReg->szName, pUsbIns->iInstance,
1366 enmVMState, VMR3GetStateName(enmVMState)));
1367 return enmVMState;
1368}
1369
1370/** @interface_method_impl{PDMUSBHLP,pfnThreadCreate} */
1371static DECLCALLBACK(int) pdmR3UsbHlp_ThreadCreate(PPDMUSBINS pUsbIns, PPPDMTHREAD ppThread, void *pvUser, PFNPDMTHREADUSB pfnThread,
1372 PFNPDMTHREADWAKEUPUSB pfnWakeup, size_t cbStack, RTTHREADTYPE enmType, const char *pszName)
1373{
1374 PDMUSB_ASSERT_USBINS(pUsbIns);
1375 VM_ASSERT_EMT(pUsbIns->Internal.s.pVM);
1376 LogFlow(("pdmR3UsbHlp_ThreadCreate: caller='%s'/%d: ppThread=%p pvUser=%p pfnThread=%p pfnWakeup=%p cbStack=%#zx enmType=%d pszName=%p:{%s}\n",
1377 pUsbIns->pReg->szName, pUsbIns->iInstance, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName, pszName));
1378
1379 int rc = pdmR3ThreadCreateUsb(pUsbIns->Internal.s.pVM, pUsbIns, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName);
1380
1381 LogFlow(("pdmR3UsbHlp_ThreadCreate: caller='%s'/%d: returns %Rrc *ppThread=%RTthrd\n", pUsbIns->pReg->szName, pUsbIns->iInstance,
1382 rc, *ppThread));
1383 return rc;
1384}
1385
1386
1387/** @interface_method_impl{PDMUSBHLP,pfnSetAsyncNotification} */
1388static DECLCALLBACK(int) pdmR3UsbHlp_SetAsyncNotification(PPDMUSBINS pUsbIns, PFNPDMUSBASYNCNOTIFY pfnAsyncNotify)
1389{
1390 PDMUSB_ASSERT_USBINS(pUsbIns);
1391 VM_ASSERT_EMT0(pUsbIns->Internal.s.pVM);
1392 LogFlow(("pdmR3UsbHlp_SetAsyncNotification: caller='%s'/%d: pfnAsyncNotify=%p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pfnAsyncNotify));
1393
1394 int rc = VINF_SUCCESS;
1395 AssertStmt(pfnAsyncNotify, rc = VERR_INVALID_PARAMETER);
1396 AssertStmt(!pUsbIns->Internal.s.pfnAsyncNotify, rc = VERR_WRONG_ORDER);
1397 AssertStmt(pUsbIns->Internal.s.fVMSuspended || pUsbIns->Internal.s.fVMReset, rc = VERR_WRONG_ORDER);
1398 VMSTATE enmVMState = VMR3GetState(pUsbIns->Internal.s.pVM);
1399 AssertStmt( enmVMState == VMSTATE_SUSPENDING
1400 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1401 || enmVMState == VMSTATE_SUSPENDING_LS
1402 || enmVMState == VMSTATE_RESETTING
1403 || enmVMState == VMSTATE_RESETTING_LS
1404 || enmVMState == VMSTATE_POWERING_OFF
1405 || enmVMState == VMSTATE_POWERING_OFF_LS,
1406 rc = VERR_INVALID_STATE);
1407
1408 if (RT_SUCCESS(rc))
1409 pUsbIns->Internal.s.pfnAsyncNotify = pfnAsyncNotify;
1410
1411 LogFlow(("pdmR3UsbHlp_SetAsyncNotification: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1412 return rc;
1413}
1414
1415
1416/** @interface_method_impl{PDMUSBHLP,pfnAsyncNotificationCompleted} */
1417static DECLCALLBACK(void) pdmR3UsbHlp_AsyncNotificationCompleted(PPDMUSBINS pUsbIns)
1418{
1419 PDMUSB_ASSERT_USBINS(pUsbIns);
1420 PVM pVM = pUsbIns->Internal.s.pVM;
1421
1422 VMSTATE enmVMState = VMR3GetState(pVM);
1423 if ( enmVMState == VMSTATE_SUSPENDING
1424 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1425 || enmVMState == VMSTATE_SUSPENDING_LS
1426 || enmVMState == VMSTATE_RESETTING
1427 || enmVMState == VMSTATE_RESETTING_LS
1428 || enmVMState == VMSTATE_POWERING_OFF
1429 || enmVMState == VMSTATE_POWERING_OFF_LS)
1430 {
1431 LogFlow(("pdmR3UsbHlp_AsyncNotificationCompleted: caller='%s'/%d:\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1432 VMR3AsyncPdmNotificationWakeupU(pVM->pUVM);
1433 }
1434 else
1435 LogFlow(("pdmR3UsbHlp_AsyncNotificationCompleted: caller='%s'/%d: enmVMState=%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance, enmVMState));
1436}
1437
1438
1439/**
1440 * The USB device helper structure.
1441 */
1442const PDMUSBHLP g_pdmR3UsbHlp =
1443{
1444 PDM_USBHLP_VERSION,
1445 pdmR3UsbHlp_DriverAttach,
1446 pdmR3UsbHlp_AssertEMT,
1447 pdmR3UsbHlp_AssertOther,
1448 pdmR3UsbHlp_DBGFStopV,
1449 pdmR3UsbHlp_DBGFInfoRegister,
1450 pdmR3UsbHlp_MMHeapAlloc,
1451 pdmR3UsbHlp_MMHeapAllocZ,
1452 pdmR3UsbHlp_PDMQueueCreate,
1453 pdmR3UsbHlp_SSMRegister,
1454 pdmR3UsbHlp_STAMRegisterV,
1455 pdmR3UsbHlp_TMTimerCreate,
1456 pdmR3UsbHlp_VMSetErrorV,
1457 pdmR3UsbHlp_VMSetRuntimeErrorV,
1458 pdmR3UsbHlp_VMState,
1459 pdmR3UsbHlp_ThreadCreate,
1460 pdmR3UsbHlp_SetAsyncNotification,
1461 pdmR3UsbHlp_AsyncNotificationCompleted,
1462 PDM_USBHLP_VERSION
1463};
1464
1465/** @} */
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