VirtualBox

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

Last change on this file since 34079 was 33595, checked in by vboxsync, 14 years ago

src/*: more spelling fixes (logging), thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 50.5 KB
Line 
1/* $Id: PDMUsb.cpp 33595 2010-10-29 10:35:00Z 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/pdm.h>
25#include <VBox/vusb.h>
26#include <VBox/mm.h>
27#include <VBox/cfgm.h>
28#include <VBox/vmm.h>
29#include <VBox/sup.h>
30#include <VBox/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", /* fShared = */ true);
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);
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, PCFGMNODE pInstanceNode, PCFGMNODE *ppConfig, uint32_t iUsbVersion)
471{
472 const bool fAtRuntime = pInstanceNode == NULL;
473 int rc;
474
475 /*
476 * If not called by pdmR3UsbInstantiateDevices(), we'll have to fix
477 * the configuration now.
478 */
479 /* USB device node. */
480 PCFGMNODE pDevNode = CFGMR3GetChildF(CFGMR3GetRoot(pVM), "USB/%s/", pUsbDev->pReg->szName);
481 if (!pDevNode)
482 {
483 rc = CFGMR3InsertNodeF(CFGMR3GetRoot(pVM), &pDevNode, "USB/%s/", pUsbDev->pReg->szName);
484 AssertRCReturn(rc, rc);
485 }
486
487 /* The instance node and number. */
488 if (!pInstanceNode)
489 {
490 for (unsigned c = 0; c < _2M; c++)
491 {
492 iInstance = pUsbDev->iNextInstance++;
493 rc = CFGMR3InsertNodeF(pDevNode, &pInstanceNode, "%d/", iInstance);
494 if (rc != VERR_CFGM_NODE_EXISTS)
495 break;
496 }
497 AssertRCReturn(rc, rc);
498 }
499 else
500 {
501 Assert(iInstance >= 0);
502 if (iInstance >= (int)pUsbDev->iNextInstance)
503 pUsbDev->iNextInstance = iInstance + 1;
504 }
505
506 /* The instance config node. */
507 PCFGMNODE pConfigToDelete = NULL;
508 PCFGMNODE pConfig = NULL;
509 if (!ppConfig || !*ppConfig)
510 {
511 rc = CFGMR3InsertNode(pInstanceNode, "Config", &pConfig);
512 AssertRCReturn(rc, rc);
513 }
514 else if (fAtRuntime)
515 {
516 rc = CFGMR3InsertSubTree(pInstanceNode, "Config", *ppConfig, &pConfig);
517 AssertRCReturn(rc, rc);
518 *ppConfig = NULL;
519 pConfigToDelete = pConfig;
520 }
521 else
522 pConfig = *ppConfig;
523 Assert(CFGMR3GetChild(pInstanceNode, "Config") == pConfig);
524
525 /* The global device config node. */
526 PCFGMNODE pGlobalConfig = CFGMR3GetChild(pDevNode, "GlobalConfig");
527 if (!pGlobalConfig)
528 {
529 rc = CFGMR3InsertNode(pDevNode, "GlobalConfig", &pGlobalConfig);
530 if (RT_FAILURE(rc))
531 {
532 CFGMR3RemoveNode(pConfigToDelete);
533 AssertRCReturn(rc, rc);
534 }
535 }
536
537 /*
538 * Allocate the device instance.
539 */
540 size_t cb = RT_OFFSETOF(PDMUSBINS, achInstanceData[pUsbDev->pReg->cbInstance]);
541 cb = RT_ALIGN_Z(cb, 16);
542 PPDMUSBINS pUsbIns;
543 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_USB, cb, (void **)&pUsbIns);
544 if (RT_FAILURE(rc))
545 {
546 AssertMsgFailed(("Failed to allocate %d bytes of instance data for USB device '%s'. rc=%Rrc\n",
547 cb, pUsbDev->pReg->szName, rc));
548 CFGMR3RemoveNode(pConfigToDelete);
549 return rc;
550 }
551
552 /*
553 * Initialize it.
554 */
555 pUsbIns->u32Version = PDM_USBINS_VERSION;
556 //pUsbIns->Internal.s.pNext = NULL;
557 //pUsbIns->Internal.s.pPerDeviceNext = NULL;
558 pUsbIns->Internal.s.pUsbDev = pUsbDev;
559 pUsbIns->Internal.s.pVM = pVM;
560 //pUsbIns->Internal.s.pLuns = NULL;
561 pUsbIns->Internal.s.pCfg = pInstanceNode;
562 pUsbIns->Internal.s.pCfgDelete = pConfigToDelete;
563 pUsbIns->Internal.s.pCfgGlobal = pGlobalConfig;
564 pUsbIns->Internal.s.Uuid = *pUuid;
565 //pUsbIns->Internal.s.pHub = NULL;
566 pUsbIns->Internal.s.iPort = UINT32_MAX; /* to be determined. */
567 pUsbIns->Internal.s.fVMSuspended = true;
568 //pUsbIns->Internal.s.pfnAsyncNotify = NULL;
569 pUsbIns->pHlpR3 = &g_pdmR3UsbHlp;
570 pUsbIns->pReg = pUsbDev->pReg;
571 pUsbIns->pCfg = pConfig;
572 pUsbIns->pCfgGlobal = pGlobalConfig;
573 pUsbIns->iInstance = iInstance;
574 pUsbIns->pvInstanceDataR3 = &pUsbIns->achInstanceData[0];
575 pUsbIns->pszName = RTStrDup(pUsbDev->pReg->szName);
576
577 /*
578 * Link it into all the lists.
579 */
580 /* The global instance FIFO. */
581 PPDMUSBINS pPrev1 = pVM->pdm.s.pUsbInstances;
582 if (!pPrev1)
583 pVM->pdm.s.pUsbInstances = pUsbIns;
584 else
585 {
586 while (pPrev1->Internal.s.pNext)
587 {
588 Assert(pPrev1->u32Version == PDM_USBINS_VERSION);
589 pPrev1 = pPrev1->Internal.s.pNext;
590 }
591 pPrev1->Internal.s.pNext = pUsbIns;
592 }
593
594 /* The per device instance FIFO. */
595 PPDMUSBINS pPrev2 = pUsbDev->pInstances;
596 if (!pPrev2)
597 pUsbDev->pInstances = pUsbIns;
598 else
599 {
600 while (pPrev2->Internal.s.pPerDeviceNext)
601 {
602 Assert(pPrev2->u32Version == PDM_USBINS_VERSION);
603 pPrev2 = pPrev2->Internal.s.pPerDeviceNext;
604 }
605 pPrev2->Internal.s.pPerDeviceNext = pUsbIns;
606 }
607
608 /*
609 * Call the constructor.
610 */
611 Log(("PDM: Constructing USB device '%s' instance %d...\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
612 rc = pUsbIns->pReg->pfnConstruct(pUsbIns, pUsbIns->iInstance, pUsbIns->pCfg, pUsbIns->pCfgGlobal);
613 if (RT_SUCCESS(rc))
614 {
615 /*
616 * Attach it to the hub.
617 */
618 Log(("PDM: Attaching it...\n"));
619 rc = pHub->Reg.pfnAttachDevice(pHub->pDrvIns, pUsbIns, &pUsbIns->Internal.s.iPort);
620 if (RT_SUCCESS(rc))
621 {
622 pHub->cAvailablePorts--;
623 Assert((int32_t)pHub->cAvailablePorts >= 0 && pHub->cAvailablePorts < pHub->cPorts);
624 pUsbIns->Internal.s.pHub = pHub;
625
626 /* Send the hot-plugged notification if applicable. */
627 if (fAtRuntime && pUsbIns->pReg->pfnHotPlugged)
628 pUsbIns->pReg->pfnHotPlugged(pUsbIns);
629
630 Log(("PDM: Successfully attached USB device '%s' instance %d to hub %p\n",
631 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub));
632 return VINF_SUCCESS;
633 }
634
635 LogRel(("PDM: Failed to attach USB device '%s' instance %d to hub %p: %Rrc\n",
636 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
637 }
638 else
639 AssertMsgFailed(("Failed to construct '%s'/%d! %Rra\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
640 if (fAtRuntime)
641 pdmR3UsbDestroyDevice(pVM, pUsbIns);
642 /* else: destructors are invoked later. */
643 return rc;
644}
645
646
647/**
648 * Instantiate USB devices.
649 *
650 * This is called by pdmR3DevInit() after it has instantiated the
651 * other devices and their drivers. If there aren't any hubs
652 * around, we'll silently skip the USB devices.
653 *
654 * @returns VBox status code.
655 * @param pVM
656 */
657int pdmR3UsbInstantiateDevices(PVM pVM)
658{
659 /*
660 * Any hubs?
661 */
662 if (!pVM->pdm.s.pUsbHubs)
663 {
664 Log(("PDM: No USB hubs, skipping USB device instantiation.\n"));
665 return VINF_SUCCESS;
666 }
667
668 /*
669 * Count the device instances.
670 */
671 PCFGMNODE pCur;
672 PCFGMNODE pUsbNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "USB/");
673 PCFGMNODE pInstanceNode;
674 unsigned cUsbDevs = 0;
675 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
676 {
677 PCFGMNODE pGlobal = CFGMR3GetChild(pCur, "GlobalConfig/");
678 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
679 if (pInstanceNode != pGlobal)
680 cUsbDevs++;
681 }
682 if (!cUsbDevs)
683 {
684 Log(("PDM: No USB devices were configured!\n"));
685 return VINF_SUCCESS;
686 }
687 Log2(("PDM: cUsbDevs=%d!\n", cUsbDevs));
688
689 /*
690 * Collect info on each USB device instance.
691 */
692 struct USBDEVORDER
693 {
694 /** Configuration node. */
695 PCFGMNODE pNode;
696 /** Pointer to the USB device. */
697 PPDMUSB pUsbDev;
698 /** Init order. */
699 uint32_t u32Order;
700 /** VBox instance number. */
701 uint32_t iInstance;
702 } *paUsbDevs = (struct USBDEVORDER *)alloca(sizeof(paUsbDevs[0]) * (cUsbDevs + 1)); /* (One extra for swapping) */
703 Assert(paUsbDevs);
704 int rc;
705 unsigned i = 0;
706 for (pCur = CFGMR3GetFirstChild(pUsbNode); pCur; pCur = CFGMR3GetNextChild(pCur))
707 {
708 /* Get the device name. */
709 char szName[sizeof(paUsbDevs[0].pUsbDev->pReg->szName)];
710 rc = CFGMR3GetName(pCur, szName, sizeof(szName));
711 AssertMsgRCReturn(rc, ("Configuration error: device name is too long (or something)! rc=%Rrc\n", rc), rc);
712
713 /* Find the device. */
714 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, szName);
715 AssertMsgReturn(pUsbDev, ("Configuration error: device '%s' not found!\n", szName), VERR_PDM_DEVICE_NOT_FOUND);
716
717 /* Configured priority or use default? */
718 uint32_t u32Order;
719 rc = CFGMR3QueryU32(pCur, "Priority", &u32Order);
720 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
721 u32Order = i << 4;
722 else
723 AssertMsgRCReturn(rc, ("Configuration error: reading \"Priority\" for the '%s' USB device failed rc=%Rrc!\n", szName, rc), rc);
724
725 /* Global config. */
726 PCFGMNODE pGlobal = CFGMR3GetChild(pCur, "GlobalConfig/");
727 if (!pGlobal)
728 {
729 rc = CFGMR3InsertNode(pCur, "GlobalConfig/", &pGlobal);
730 AssertMsgRCReturn(rc, ("Failed to create GlobalConfig node! rc=%Rrc\n", rc), rc);
731 CFGMR3SetRestrictedRoot(pGlobal);
732 }
733
734 /* Enumerate the device instances. */
735 for (pInstanceNode = CFGMR3GetFirstChild(pCur); pInstanceNode; pInstanceNode = CFGMR3GetNextChild(pInstanceNode))
736 {
737 if (pInstanceNode == pGlobal)
738 continue;
739
740 paUsbDevs[i].pNode = pInstanceNode;
741 paUsbDevs[i].pUsbDev = pUsbDev;
742 paUsbDevs[i].u32Order = u32Order;
743
744 /* Get the instance number. */
745 char szInstance[32];
746 rc = CFGMR3GetName(pInstanceNode, szInstance, sizeof(szInstance));
747 AssertMsgRCReturn(rc, ("Configuration error: instance name is too long (or something)! rc=%Rrc\n", rc), rc);
748 char *pszNext = NULL;
749 rc = RTStrToUInt32Ex(szInstance, &pszNext, 0, &paUsbDevs[i].iInstance);
750 AssertMsgRCReturn(rc, ("Configuration error: RTStrToInt32Ex failed on the instance name '%s'! rc=%Rrc\n", szInstance, rc), rc);
751 AssertMsgReturn(!*pszNext, ("Configuration error: the instance name '%s' isn't all digits. (%s)\n", szInstance, pszNext), VERR_INVALID_PARAMETER);
752
753 /* next instance */
754 i++;
755 }
756 } /* devices */
757 Assert(i == cUsbDevs);
758
759 /*
760 * Sort the device array ascending on u32Order. (bubble)
761 */
762 unsigned c = cUsbDevs - 1;
763 while (c)
764 {
765 unsigned j = 0;
766 for (i = 0; i < c; i++)
767 if (paUsbDevs[i].u32Order > paUsbDevs[i + 1].u32Order)
768 {
769 paUsbDevs[cUsbDevs] = paUsbDevs[i + 1];
770 paUsbDevs[i + 1] = paUsbDevs[i];
771 paUsbDevs[i] = paUsbDevs[cUsbDevs];
772 j = i;
773 }
774 c = j;
775 }
776
777 /*
778 * Instantiate the devices.
779 */
780 for (i = 0; i < cUsbDevs; i++)
781 {
782 /*
783 * Make sure there is a config node and mark it as restricted.
784 */
785 PCFGMNODE pConfigNode = CFGMR3GetChild(paUsbDevs[i].pNode, "Config/");
786 if (!pConfigNode)
787 {
788 rc = CFGMR3InsertNode(paUsbDevs[i].pNode, "Config", &pConfigNode);
789 AssertMsgRCReturn(rc, ("Failed to create Config node! rc=%Rrc\n", rc), rc);
790 }
791 CFGMR3SetRestrictedRoot(pConfigNode);
792
793 /** @todo
794 * Figure out the USB version from the USB device registration and the configuration.
795 */
796 uint32_t iUsbVersion = VUSB_STDVER_11;
797
798 /*
799 * Find a suitable hub with free ports.
800 */
801 PPDMUSBHUB pHub;
802 rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
803 if (RT_FAILURE(rc))
804 {
805 Log(("pdmR3UsbFindHub failed %Rrc\n", rc));
806 return rc;
807 }
808
809 /*
810 * Create and attach the device.
811 */
812 RTUUID Uuid;
813 rc = RTUuidCreate(&Uuid);
814 AssertRCReturn(rc, rc);
815 rc = pdmR3UsbCreateDevice(pVM, pHub, paUsbDevs[i].pUsbDev, paUsbDevs[i].iInstance, &Uuid, paUsbDevs[i].pNode, &pConfigNode, iUsbVersion);
816 if (RT_FAILURE(rc))
817 return rc;
818 } /* for device instances */
819
820 return VINF_SUCCESS;
821}
822
823
824/**
825 * Creates a USB proxy device instance.
826 *
827 * This will find an appropriate HUB for the USB device, create the necessary CFGM stuff
828 * and try instantiate the proxy device.
829 *
830 * @returns VBox status code.
831 * @param pVM The VM handle.
832 * @param pUuid The UUID to be associated with the device.
833 * @param fRemote Whether it's a remove or local device.
834 * @param pszAddress The address string.
835 * @param pvBackend Pointer to the backend.
836 * @param iUsbVersion The preferred USB version.
837 * @param fMaskedIfs The interfaces to hide from the guest.
838 */
839VMMR3DECL(int) PDMR3USBCreateProxyDevice(PVM pVM, PCRTUUID pUuid, bool fRemote, const char *pszAddress, void *pvBackend,
840 uint32_t iUsbVersion, uint32_t fMaskedIfs)
841{
842 /*
843 * Validate input.
844 */
845 VM_ASSERT_EMT(pVM);
846 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
847 AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
848 AssertReturn( iUsbVersion == VUSB_STDVER_20
849 || iUsbVersion == VUSB_STDVER_11, VERR_INVALID_PARAMETER);
850
851 /*
852 * Find the USBProxy driver.
853 */
854 PPDMUSB pUsbDev = pdmR3UsbLookup(pVM, "USBProxy");
855 if (!pUsbDev)
856 {
857 LogRel(("PDMR3USBCreateProxyDevice: The USBProxy device class wasn't found\n"));
858 return VERR_PDM_NO_USBPROXY;
859 }
860
861 /*
862 * Find a suitable hub with free ports.
863 */
864 PPDMUSBHUB pHub;
865 int rc = pdmR3UsbFindHub(pVM, iUsbVersion, &pHub);
866 if (RT_FAILURE(rc))
867 {
868 Log(("pdmR3UsbFindHub: failed %Rrc\n", rc));
869 return rc;
870 }
871
872 /*
873 * Create the CFGM configuration node.
874 */
875 PCFGMNODE pConfig = CFGMR3CreateTree(pVM);
876 AssertReturn(pConfig, VERR_NO_MEMORY);
877 do /* break loop */
878 {
879 rc = CFGMR3InsertString(pConfig, "Address", pszAddress); AssertRCBreak(rc);
880 char szUuid[RTUUID_STR_LENGTH];
881 rc = RTUuidToStr(pUuid, &szUuid[0], sizeof(szUuid)); AssertRCBreak(rc);
882 rc = CFGMR3InsertString(pConfig, "UUID", szUuid); AssertRCBreak(rc);
883 rc = CFGMR3InsertInteger(pConfig, "Remote", fRemote); AssertRCBreak(rc);
884 rc = CFGMR3InsertInteger(pConfig, "USBVersion", iUsbVersion); AssertRCBreak(rc);
885 rc = CFGMR3InsertInteger(pConfig, "pvBackend", (uintptr_t)pvBackend); AssertRCBreak(rc);
886 rc = CFGMR3InsertInteger(pConfig, "MaskedIfs", fMaskedIfs); AssertRCBreak(rc);
887 rc = CFGMR3InsertInteger(pConfig, "Force11Device", !(pHub->fVersions & iUsbVersion)); AssertRCBreak(rc);
888 } while (0); /* break loop */
889 if (RT_FAILURE(rc))
890 {
891 CFGMR3RemoveNode(pConfig);
892 LogRel(("PDMR3USBCreateProxyDevice: failed to setup CFGM config, rc=%Rrc\n", rc));
893 return rc;
894 }
895
896 /*
897 * Finally, try create it.
898 */
899 rc = pdmR3UsbCreateDevice(pVM, pHub, pUsbDev, -1, pUuid, NULL, &pConfig, iUsbVersion);
900 if (RT_FAILURE(rc) && pConfig)
901 CFGMR3RemoveNode(pConfig);
902 return rc;
903}
904
905
906/**
907 * Destroys a hot-plugged USB device.
908 *
909 * The device must be detached from the HUB at this point.
910 *
911 * @param pVM Pointer to the shared VM structure.
912 * @param pUsbIns The USB device instance to destroy.
913 * @thread EMT
914 */
915static void pdmR3UsbDestroyDevice(PVM pVM, PPDMUSBINS pUsbIns)
916{
917 Assert(!pUsbIns->Internal.s.pHub);
918
919 /*
920 * Do the unplug notification.
921 */
922 /** @todo what about the drivers? */
923 if (pUsbIns->pReg->pfnHotUnplugged)
924 pUsbIns->pReg->pfnHotUnplugged(pUsbIns);
925
926 /*
927 * Destroy the luns with their driver chains and call the device destructor.
928 */
929 while (pUsbIns->Internal.s.pLuns)
930 {
931 PPDMLUN pLun = pUsbIns->Internal.s.pLuns;
932 pUsbIns->Internal.s.pLuns = pLun->pNext;
933 if (pLun->pTop)
934 pdmR3DrvDestroyChain(pLun->pTop, PDM_TACH_FLAGS_NOT_HOT_PLUG); /* Hotplugging is handled differently here atm. */
935 MMR3HeapFree(pLun);
936 }
937
938 /* finally, the device. */
939 if (pUsbIns->pReg->pfnDestruct)
940 {
941 Log(("PDM: Destructing USB device '%s' instance %d...\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
942 pUsbIns->pReg->pfnDestruct(pUsbIns);
943 }
944 //TMR3TimerDestroyUsb(pVM, pUsbIns);
945 //SSMR3DeregisterUsb(pVM, pUsbIns, NULL, 0);
946 pdmR3ThreadDestroyUsb(pVM, pUsbIns);
947
948 /*
949 * Unlink it.
950 */
951 /* The global instance FIFO. */
952 if (pVM->pdm.s.pUsbInstances == pUsbIns)
953 pVM->pdm.s.pUsbInstances = pUsbIns->Internal.s.pNext;
954 else
955 {
956 PPDMUSBINS pPrev = pVM->pdm.s.pUsbInstances;
957 while (pPrev && pPrev->Internal.s.pNext != pUsbIns)
958 {
959 Assert(pPrev->u32Version == PDM_USBINS_VERSION);
960 pPrev = pPrev->Internal.s.pNext;
961 }
962 Assert(pPrev); Assert(pPrev != pUsbIns);
963 if (pPrev)
964 pPrev->Internal.s.pNext = pUsbIns->Internal.s.pNext;
965 }
966
967 /* The per device instance FIFO. */
968 PPDMUSB pUsbDev = pUsbIns->Internal.s.pUsbDev;
969 if (pUsbDev->pInstances == pUsbIns)
970 pUsbDev->pInstances = pUsbIns->Internal.s.pPerDeviceNext;
971 else
972 {
973 PPDMUSBINS pPrev = pUsbDev->pInstances;
974 while (pPrev && pPrev->Internal.s.pPerDeviceNext != pUsbIns)
975 {
976 Assert(pPrev->u32Version == PDM_USBINS_VERSION);
977 pPrev = pPrev->Internal.s.pPerDeviceNext;
978 }
979 Assert(pPrev); Assert(pPrev != pUsbIns);
980 if (pPrev)
981 pPrev->Internal.s.pPerDeviceNext = pUsbIns->Internal.s.pPerDeviceNext;
982 }
983
984 /*
985 * Trash it.
986 */
987 pUsbIns->u32Version = 0;
988 pUsbIns->pReg = NULL;
989 if (pUsbIns->pszName)
990 {
991 RTStrFree(pUsbIns->pszName);
992 pUsbIns->pszName = NULL;
993 }
994 CFGMR3RemoveNode(pUsbIns->Internal.s.pCfgDelete);
995 MMR3HeapFree(pUsbIns);
996}
997
998
999/**
1000 * Detaches and destroys a USB device.
1001 *
1002 * @returns VBox status code.
1003 * @param pVM The VM handle.
1004 * @param pUuid The UUID associated with the device to detach.
1005 * @thread EMT
1006 */
1007VMMR3DECL(int) PDMR3USBDetachDevice(PVM pVM, PCRTUUID pUuid)
1008{
1009 /*
1010 * Validate input.
1011 */
1012 VM_ASSERT_EMT(pVM);
1013 AssertPtrReturn(pUuid, VERR_INVALID_POINTER);
1014 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
1015
1016 /*
1017 * Search the global list for it.
1018 */
1019 PPDMUSBINS pUsbIns = pVM->pdm.s.pUsbInstances;
1020 for ( ; pUsbIns; pUsbIns = pUsbIns->Internal.s.pNext)
1021 if (!RTUuidCompare(&pUsbIns->Internal.s.Uuid, pUuid))
1022 break;
1023 if (!pUsbIns)
1024 return VERR_PDM_DEVICE_INSTANCE_NOT_FOUND; /** @todo VERR_PDM_USB_INSTANCE_NOT_FOUND */
1025
1026 /*
1027 * Detach it from the HUB (if it's actually attached to one).
1028 */
1029 PPDMUSBHUB pHub = pUsbIns->Internal.s.pHub;
1030 if (pHub)
1031 {
1032 int rc = pHub->Reg.pfnDetachDevice(pHub->pDrvIns, pUsbIns, pUsbIns->Internal.s.iPort);
1033 if (RT_FAILURE(rc))
1034 {
1035 LogRel(("PDM: Failed to detach USB device '%s' instance %d from %p: %Rrc\n",
1036 pUsbIns->pReg->szName, pUsbIns->iInstance, pHub, rc));
1037 return rc;
1038 }
1039
1040 pHub->cAvailablePorts++;
1041 Assert(pHub->cAvailablePorts > 0 && pHub->cAvailablePorts <= pHub->cPorts);
1042 pUsbIns->Internal.s.pHub = NULL;
1043 }
1044
1045 /*
1046 * Notify about unplugging and destroy the device with it's drivers.
1047 */
1048 pdmR3UsbDestroyDevice(pVM, pUsbIns);
1049
1050 return VINF_SUCCESS;
1051}
1052
1053
1054/**
1055 * Checks if there are any USB hubs attached.
1056 *
1057 * @returns true / false accordingly.
1058 * @param pVM Pointer to the shared VM structure.
1059 */
1060VMMR3DECL(bool) PDMR3USBHasHub(PVM pVM)
1061{
1062 return pVM->pdm.s.pUsbHubs != NULL;
1063}
1064
1065
1066/** @name USB Device Helpers
1067 * @{
1068 */
1069
1070/** @interface_method_impl{PDMUSBHLPR3,pfnDriverAttach} */
1071static DECLCALLBACK(int) pdmR3UsbHlp_DriverAttach(PPDMUSBINS pUsbIns, RTUINT iLun, PPDMIBASE pBaseInterface, PPDMIBASE *ppBaseInterface, const char *pszDesc)
1072{
1073 PDMUSB_ASSERT_USBINS(pUsbIns);
1074 PVM pVM = pUsbIns->Internal.s.pVM;
1075 VM_ASSERT_EMT(pVM);
1076 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: iLun=%d pBaseInterface=%p ppBaseInterface=%p pszDesc=%p:{%s}\n",
1077 pUsbIns->pReg->szName, pUsbIns->iInstance, iLun, pBaseInterface, ppBaseInterface, pszDesc, pszDesc));
1078
1079 /*
1080 * Lookup the LUN, it might already be registered.
1081 */
1082 PPDMLUN pLunPrev = NULL;
1083 PPDMLUN pLun = pUsbIns->Internal.s.pLuns;
1084 for (; pLun; pLunPrev = pLun, pLun = pLun->pNext)
1085 if (pLun->iLun == iLun)
1086 break;
1087
1088 /*
1089 * Create the LUN if if wasn't found, else check if driver is already attached to it.
1090 */
1091 if (!pLun)
1092 {
1093 if ( !pBaseInterface
1094 || !pszDesc
1095 || !*pszDesc)
1096 {
1097 Assert(pBaseInterface);
1098 Assert(pszDesc || *pszDesc);
1099 return VERR_INVALID_PARAMETER;
1100 }
1101
1102 pLun = (PPDMLUN)MMR3HeapAlloc(pVM, MM_TAG_PDM_LUN, sizeof(*pLun));
1103 if (!pLun)
1104 return VERR_NO_MEMORY;
1105
1106 pLun->iLun = iLun;
1107 pLun->pNext = pLunPrev ? pLunPrev->pNext : NULL;
1108 pLun->pTop = NULL;
1109 pLun->pBottom = NULL;
1110 pLun->pDevIns = NULL;
1111 pLun->pUsbIns = pUsbIns;
1112 pLun->pszDesc = pszDesc;
1113 pLun->pBase = pBaseInterface;
1114 if (!pLunPrev)
1115 pUsbIns->Internal.s.pLuns = pLun;
1116 else
1117 pLunPrev->pNext = pLun;
1118 Log(("pdmR3UsbHlp_DriverAttach: Registered LUN#%d '%s' with device '%s'/%d.\n",
1119 iLun, pszDesc, pUsbIns->pReg->szName, pUsbIns->iInstance));
1120 }
1121 else if (pLun->pTop)
1122 {
1123 AssertMsgFailed(("Already attached! The device should keep track of such things!\n"));
1124 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, VERR_PDM_DRIVER_ALREADY_ATTACHED));
1125 return VERR_PDM_DRIVER_ALREADY_ATTACHED;
1126 }
1127 Assert(pLun->pBase == pBaseInterface);
1128
1129
1130 /*
1131 * Get the attached driver configuration.
1132 */
1133 int rc;
1134 PCFGMNODE pNode = CFGMR3GetChildF(pUsbIns->Internal.s.pCfg, "LUN#%u", iLun);
1135 if (pNode)
1136 rc = pdmR3DrvInstantiate(pVM, pNode, pBaseInterface, NULL /*pDrvAbove*/, pLun, ppBaseInterface);
1137 else
1138 rc = VERR_PDM_NO_ATTACHED_DRIVER;
1139
1140
1141 LogFlow(("pdmR3UsbHlp_DriverAttach: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1142 return rc;
1143}
1144
1145
1146/** @interface_method_impl{PDMUSBHLP,pfnAssertEMT} */
1147static DECLCALLBACK(bool) pdmR3UsbHlp_AssertEMT(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1148{
1149 PDMUSB_ASSERT_USBINS(pUsbIns);
1150 if (VM_IS_EMT(pUsbIns->Internal.s.pVM))
1151 return true;
1152
1153 char szMsg[100];
1154 RTStrPrintf(szMsg, sizeof(szMsg), "AssertEMT '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance);
1155 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1156 AssertBreakpoint();
1157 return false;
1158}
1159
1160
1161/** @interface_method_impl{PDMUSBHLP,pfnAssertOther} */
1162static DECLCALLBACK(bool) pdmR3UsbHlp_AssertOther(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction)
1163{
1164 PDMUSB_ASSERT_USBINS(pUsbIns);
1165 if (!VM_IS_EMT(pUsbIns->Internal.s.pVM))
1166 return true;
1167
1168 char szMsg[100];
1169 RTStrPrintf(szMsg, sizeof(szMsg), "AssertOther '%s'/%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance);
1170 RTAssertMsg1Weak(szMsg, iLine, pszFile, pszFunction);
1171 AssertBreakpoint();
1172 return false;
1173}
1174
1175
1176/** @interface_method_impl{PDMUSBHLP,pfnDBGFStopV} */
1177static DECLCALLBACK(int) pdmR3UsbHlp_DBGFStopV(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction, const char *pszFormat, va_list args)
1178{
1179 PDMUSB_ASSERT_USBINS(pUsbIns);
1180#ifdef LOG_ENABLED
1181 va_list va2;
1182 va_copy(va2, args);
1183 LogFlow(("pdmR3UsbHlp_DBGFStopV: caller='%s'/%d: pszFile=%p:{%s} iLine=%d pszFunction=%p:{%s} pszFormat=%p:{%s} (%N)\n",
1184 pUsbIns->pReg->szName, pUsbIns->iInstance, pszFile, pszFile, iLine, pszFunction, pszFunction, pszFormat, pszFormat, pszFormat, &va2));
1185 va_end(va2);
1186#endif
1187
1188 PVM pVM = pUsbIns->Internal.s.pVM;
1189 VM_ASSERT_EMT(pVM);
1190 int rc = DBGFR3EventSrcV(pVM, DBGFEVENT_DEV_STOP, pszFile, iLine, pszFunction, pszFormat, args);
1191 if (rc == VERR_DBGF_NOT_ATTACHED)
1192 rc = VINF_SUCCESS;
1193
1194 LogFlow(("pdmR3UsbHlp_DBGFStopV: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1195 return rc;
1196}
1197
1198
1199/** @interface_method_impl{PDMUSBHLP,pfnDBGFInfoRegister} */
1200static DECLCALLBACK(int) pdmR3UsbHlp_DBGFInfoRegister(PPDMUSBINS pUsbIns, const char *pszName, const char *pszDesc, PFNDBGFHANDLERUSB pfnHandler)
1201{
1202 PDMUSB_ASSERT_USBINS(pUsbIns);
1203 LogFlow(("pdmR3UsbHlp_DBGFInfoRegister: caller='%s'/%d: pszName=%p:{%s} pszDesc=%p:{%s} pfnHandler=%p\n",
1204 pUsbIns->pReg->szName, pUsbIns->iInstance, pszName, pszName, pszDesc, pszDesc, pfnHandler));
1205
1206 PVM pVM = pUsbIns->Internal.s.pVM;
1207 VM_ASSERT_EMT(pVM);
1208 /** @todo int rc = DBGFR3InfoRegisterUsb(pVM, pszName, pszDesc, pfnHandler, pUsbIns); */
1209 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1210
1211 LogFlow(("pdmR3UsbHlp_DBGFInfoRegister: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1212 return rc;
1213}
1214
1215
1216/** @interface_method_impl{PDMUSBHLP,pfnMMHeapAlloc} */
1217static DECLCALLBACK(void *) pdmR3UsbHlp_MMHeapAlloc(PPDMUSBINS pUsbIns, size_t cb)
1218{
1219 PDMUSB_ASSERT_USBINS(pUsbIns);
1220 LogFlow(("pdmR3UsbHlp_MMHeapAlloc: caller='%s'/%d: cb=%#x\n", pUsbIns->pReg->szName, pUsbIns->iInstance, cb));
1221
1222 void *pv = MMR3HeapAlloc(pUsbIns->Internal.s.pVM, MM_TAG_PDM_USB_USER, cb);
1223
1224 LogFlow(("pdmR3UsbHlp_MMHeapAlloc: caller='%s'/%d: returns %p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pv));
1225 return pv;
1226}
1227
1228
1229/** @interface_method_impl{PDMUSBHLP,pfnMMHeapAllocZ} */
1230static DECLCALLBACK(void *) pdmR3UsbHlp_MMHeapAllocZ(PPDMUSBINS pUsbIns, size_t cb)
1231{
1232 PDMUSB_ASSERT_USBINS(pUsbIns);
1233 LogFlow(("pdmR3UsbHlp_MMHeapAllocZ: caller='%s'/%d: cb=%#x\n", pUsbIns->pReg->szName, pUsbIns->iInstance, cb));
1234
1235 void *pv = MMR3HeapAllocZ(pUsbIns->Internal.s.pVM, MM_TAG_PDM_USB_USER, cb);
1236
1237 LogFlow(("pdmR3UsbHlp_MMHeapAllocZ: caller='%s'/%d: returns %p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pv));
1238 return pv;
1239}
1240
1241
1242/** @interface_method_impl{PDMUSBHLP,pfnPDMQueueCreate} */
1243static DECLCALLBACK(int) pdmR3UsbHlp_PDMQueueCreate(PPDMUSBINS pUsbIns, RTUINT cbItem, RTUINT cItems, uint32_t cMilliesInterval,
1244 PFNPDMQUEUEUSB pfnCallback, const char *pszName, PPDMQUEUE *ppQueue)
1245{
1246 PDMUSB_ASSERT_USBINS(pUsbIns);
1247 LogFlow(("pdmR3UsbHlp_PDMQueueCreate: caller='%s'/%d: cbItem=%#x cItems=%#x cMilliesInterval=%u pfnCallback=%p pszName=%p:{%s} ppQueue=%p\n",
1248 pUsbIns->pReg->szName, pUsbIns->iInstance, cbItem, cItems, cMilliesInterval, pfnCallback, pszName, pszName, ppQueue));
1249
1250 PVM pVM = pUsbIns->Internal.s.pVM;
1251 VM_ASSERT_EMT(pVM);
1252
1253 if (pUsbIns->iInstance > 0)
1254 {
1255 pszName = MMR3HeapAPrintf(pVM, MM_TAG_PDM_DEVICE_DESC, "%s_%u", pszName, pUsbIns->iInstance);
1256 AssertLogRelReturn(pszName, VERR_NO_MEMORY);
1257 }
1258
1259 /** @todo int rc = PDMR3QueueCreateUsb(pVM, pUsbIns, cbItem, cItems, cMilliesInterval, pfnCallback, fGCEnabled, pszName, ppQueue); */
1260 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1261
1262 LogFlow(("pdmR3UsbHlp_PDMQueueCreate: caller='%s'/%d: returns %Rrc *ppQueue=%p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc, *ppQueue));
1263 return rc;
1264}
1265
1266
1267/** @interface_method_impl{PDMUSBHLP,pfnSSMRegister} */
1268static DECLCALLBACK(int) pdmR3UsbHlp_SSMRegister(PPDMUSBINS pUsbIns, uint32_t uVersion, size_t cbGuess,
1269 PFNSSMUSBLIVEPREP pfnLivePrep, PFNSSMUSBLIVEEXEC pfnLiveExec, PFNSSMUSBLIVEVOTE pfnLiveVote,
1270 PFNSSMUSBSAVEPREP pfnSavePrep, PFNSSMUSBSAVEEXEC pfnSaveExec, PFNSSMUSBSAVEDONE pfnSaveDone,
1271 PFNSSMUSBLOADPREP pfnLoadPrep, PFNSSMUSBLOADEXEC pfnLoadExec, PFNSSMUSBLOADDONE pfnLoadDone)
1272{
1273 PDMUSB_ASSERT_USBINS(pUsbIns);
1274 VM_ASSERT_EMT(pUsbIns->Internal.s.pVM);
1275 LogFlow(("pdmR3UsbHlp_SSMRegister: caller='%s'/%d: uVersion=#x cbGuess=%#x\n"
1276 " pfnLivePrep=%p pfnLiveExec=%p pfnLiveVote=%p pfnSavePrep=%p pfnSaveExec=%p pfnSaveDone=%p pszLoadPrep=%p pfnLoadExec=%p pfnLoadDone=%p\n",
1277 pUsbIns->pReg->szName, pUsbIns->iInstance, uVersion, cbGuess,
1278 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1279 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1280 pfnLoadPrep, pfnLoadExec, pfnLoadDone));
1281
1282 /** @todo
1283 int rc = SSMR3RegisterUsb(pUsbIns->Internal.s.pVM, pUsbIns, pUsbIns->pReg->szName, pUsbIns->iInstance,
1284 uVersion, cbGuess,
1285 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1286 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1287 pfnLoadPrep, pfnLoadExec, pfnLoadDone); */
1288 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1289
1290 LogFlow(("pdmR3UsbHlp_SSMRegister: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1291 return rc;
1292}
1293
1294
1295/** @interface_method_impl{PDMUSBHLP,pfnSTAMRegisterV} */
1296static DECLCALLBACK(void) pdmR3UsbHlp_STAMRegisterV(PPDMUSBINS pUsbIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
1297 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list args)
1298{
1299 PDMUSB_ASSERT_USBINS(pUsbIns);
1300 PVM pVM = pUsbIns->Internal.s.pVM;
1301 VM_ASSERT_EMT(pVM);
1302
1303 int rc = STAMR3RegisterV(pVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
1304 AssertRC(rc);
1305
1306 NOREF(pVM);
1307}
1308
1309
1310/** @interface_method_impl{PDMUSBHLP,pfnTMTimerCreate} */
1311static DECLCALLBACK(int) pdmR3UsbHlp_TMTimerCreate(PPDMUSBINS pUsbIns, TMCLOCK enmClock, PFNTMTIMERUSB pfnCallback, void *pvUser,
1312 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
1313{
1314 PDMUSB_ASSERT_USBINS(pUsbIns);
1315 PVM pVM = pUsbIns->Internal.s.pVM;
1316 VM_ASSERT_EMT(pVM);
1317 LogFlow(("pdmR3UsbHlp_TMTimerCreate: caller='%s'/%d: enmClock=%d pfnCallback=%p pvUser=%p fFlags=%#x pszDesc=%p:{%s} ppTimer=%p\n",
1318 pUsbIns->pReg->szName, pUsbIns->iInstance, enmClock, pfnCallback, pvUser, fFlags, pszDesc, pszDesc, ppTimer));
1319
1320 if (pUsbIns->iInstance > 0) /** @todo use a string cache here later. */
1321 {
1322 char *pszDesc2 = MMR3HeapAPrintf(pVM, MM_TAG_PDM_USB_DESC, "%s [%u]", pszDesc, pUsbIns->iInstance);
1323 if (pszDesc2)
1324 pszDesc = pszDesc2;
1325 }
1326
1327 /** @todo
1328 int rc = TMR3TimerCreateUsb(pVM, pUsbIns, enmClock, pfnCallback, pvUser, fFlags, pszDesc, ppTimer); */
1329 int rc = VERR_NOT_IMPLEMENTED; AssertFailed();
1330
1331 LogFlow(("pdmR3UsbHlp_TMTimerCreate: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1332 return rc;
1333}
1334
1335
1336/** @interface_method_impl{PDMUSBHLP,pfnVMSetErrorV} */
1337static DECLCALLBACK(int) pdmR3UsbHlp_VMSetErrorV(PPDMUSBINS pUsbIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
1338{
1339 PDMUSB_ASSERT_USBINS(pUsbIns);
1340 int rc2 = VMSetErrorV(pUsbIns->Internal.s.pVM, rc, RT_SRC_POS_ARGS, pszFormat, va); Assert(rc2 == rc); NOREF(rc2);
1341 return rc;
1342}
1343
1344
1345/** @interface_method_impl{PDMUSBHLP,pfnVMSetRuntimeErrorV} */
1346static DECLCALLBACK(int) pdmR3UsbHlp_VMSetRuntimeErrorV(PPDMUSBINS pUsbIns, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
1347{
1348 PDMUSB_ASSERT_USBINS(pUsbIns);
1349 int rc = VMSetRuntimeErrorV(pUsbIns->Internal.s.pVM, fFlags, pszErrorId, pszFormat, va);
1350 return rc;
1351}
1352
1353
1354/** @interface_method_impl{PDMUSBHLP,pfnVMState} */
1355static DECLCALLBACK(VMSTATE) pdmR3UsbHlp_VMState(PPDMUSBINS pUsbIns)
1356{
1357 PDMUSB_ASSERT_USBINS(pUsbIns);
1358
1359 VMSTATE enmVMState = VMR3GetState(pUsbIns->Internal.s.pVM);
1360
1361 LogFlow(("pdmR3UsbHlp_VMState: caller='%s'/%d: returns %d (%s)\n", pUsbIns->pReg->szName, pUsbIns->iInstance,
1362 enmVMState, VMR3GetStateName(enmVMState)));
1363 return enmVMState;
1364}
1365
1366
1367/** @interface_method_impl{PDMUSBHLP,pfnSetAsyncNotification} */
1368static DECLCALLBACK(int) pdmR3UsbHlp_SetAsyncNotification(PPDMUSBINS pUsbIns, PFNPDMUSBASYNCNOTIFY pfnAsyncNotify)
1369{
1370 PDMUSB_ASSERT_USBINS(pUsbIns);
1371 VM_ASSERT_EMT0(pUsbIns->Internal.s.pVM);
1372 LogFlow(("pdmR3UsbHlp_SetAsyncNotification: caller='%s'/%d: pfnAsyncNotify=%p\n", pUsbIns->pReg->szName, pUsbIns->iInstance, pfnAsyncNotify));
1373
1374 int rc = VINF_SUCCESS;
1375 AssertStmt(pfnAsyncNotify, rc = VERR_INVALID_PARAMETER);
1376 AssertStmt(!pUsbIns->Internal.s.pfnAsyncNotify, rc = VERR_WRONG_ORDER);
1377 AssertStmt(pUsbIns->Internal.s.fVMSuspended || pUsbIns->Internal.s.fVMReset, rc = VERR_WRONG_ORDER);
1378 VMSTATE enmVMState = VMR3GetState(pUsbIns->Internal.s.pVM);
1379 AssertStmt( enmVMState == VMSTATE_SUSPENDING
1380 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1381 || enmVMState == VMSTATE_SUSPENDING_LS
1382 || enmVMState == VMSTATE_RESETTING
1383 || enmVMState == VMSTATE_RESETTING_LS
1384 || enmVMState == VMSTATE_POWERING_OFF
1385 || enmVMState == VMSTATE_POWERING_OFF_LS,
1386 rc = VERR_INVALID_STATE);
1387
1388 if (RT_SUCCESS(rc))
1389 pUsbIns->Internal.s.pfnAsyncNotify = pfnAsyncNotify;
1390
1391 LogFlow(("pdmR3UsbHlp_SetAsyncNotification: caller='%s'/%d: returns %Rrc\n", pUsbIns->pReg->szName, pUsbIns->iInstance, rc));
1392 return rc;
1393}
1394
1395
1396/** @interface_method_impl{PDMUSBHLP,pfnAsyncNotificationCompleted} */
1397static DECLCALLBACK(void) pdmR3UsbHlp_AsyncNotificationCompleted(PPDMUSBINS pUsbIns)
1398{
1399 PDMUSB_ASSERT_USBINS(pUsbIns);
1400 PVM pVM = pUsbIns->Internal.s.pVM;
1401
1402 VMSTATE enmVMState = VMR3GetState(pVM);
1403 if ( enmVMState == VMSTATE_SUSPENDING
1404 || enmVMState == VMSTATE_SUSPENDING_EXT_LS
1405 || enmVMState == VMSTATE_SUSPENDING_LS
1406 || enmVMState == VMSTATE_RESETTING
1407 || enmVMState == VMSTATE_RESETTING_LS
1408 || enmVMState == VMSTATE_POWERING_OFF
1409 || enmVMState == VMSTATE_POWERING_OFF_LS)
1410 {
1411 LogFlow(("pdmR3UsbHlp_AsyncNotificationCompleted: caller='%s'/%d:\n", pUsbIns->pReg->szName, pUsbIns->iInstance));
1412 VMR3AsyncPdmNotificationWakeupU(pVM->pUVM);
1413 }
1414 else
1415 LogFlow(("pdmR3UsbHlp_AsyncNotificationCompleted: caller='%s'/%d: enmVMState=%d\n", pUsbIns->pReg->szName, pUsbIns->iInstance, enmVMState));
1416}
1417
1418
1419/**
1420 * The USB device helper structure.
1421 */
1422const PDMUSBHLP g_pdmR3UsbHlp =
1423{
1424 PDM_USBHLP_VERSION,
1425 pdmR3UsbHlp_DriverAttach,
1426 pdmR3UsbHlp_AssertEMT,
1427 pdmR3UsbHlp_AssertOther,
1428 pdmR3UsbHlp_DBGFStopV,
1429 pdmR3UsbHlp_DBGFInfoRegister,
1430 pdmR3UsbHlp_MMHeapAlloc,
1431 pdmR3UsbHlp_MMHeapAllocZ,
1432 pdmR3UsbHlp_PDMQueueCreate,
1433 pdmR3UsbHlp_SSMRegister,
1434 pdmR3UsbHlp_STAMRegisterV,
1435 pdmR3UsbHlp_TMTimerCreate,
1436 pdmR3UsbHlp_VMSetErrorV,
1437 pdmR3UsbHlp_VMSetRuntimeErrorV,
1438 pdmR3UsbHlp_VMState,
1439 pdmR3UsbHlp_SetAsyncNotification,
1440 pdmR3UsbHlp_AsyncNotificationCompleted,
1441 PDM_USBHLP_VERSION
1442};
1443
1444/** @} */
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