VirtualBox

source: vbox/trunk/include/VBox/vmm/pdmusb.h@ 49092

Last change on this file since 49092 was 49092, checked in by vboxsync, 12 years ago

VUSB,EHCI,OHCI: Add a method to query whether a device is emulated or not. Required for working saved states when using the MSD device

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 41.2 KB
Line 
1/** @file
2 * PDM - Pluggable Device Manager, USB Devices.
3 */
4
5/*
6 * Copyright (C) 2006-2013 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___VBox_vmm_pdmusb_h
27#define ___VBox_vmm_pdmusb_h
28
29#include <VBox/vmm/pdmqueue.h>
30#include <VBox/vmm/pdmcritsect.h>
31#include <VBox/vmm/pdmthread.h>
32#include <VBox/vmm/pdmifs.h>
33#include <VBox/vmm/pdmcommon.h>
34#include <VBox/vmm/tm.h>
35#include <VBox/vmm/ssm.h>
36#include <VBox/vmm/cfgm.h>
37#include <VBox/vmm/dbgf.h>
38#include <VBox/vmm/mm.h>
39#include <VBox/err.h>
40#include <VBox/vusb.h>
41#include <iprt/stdarg.h>
42
43RT_C_DECLS_BEGIN
44
45/** @defgroup grp_pdm_usbdev The USB Devices API
46 * @ingroup grp_pdm
47 * @{
48 */
49
50
51/**
52 * A string entry for the USB descriptor cache.
53 */
54typedef struct PDMUSBDESCCACHESTRING
55{
56 /** The string index. */
57 uint8_t idx;
58 /** The UTF-8 representation of the string. */
59 const char *psz;
60} PDMUSBDESCCACHESTRING;
61/** Pointer to a const string entry. */
62typedef PDMUSBDESCCACHESTRING const *PCPDMUSBDESCCACHESTRING;
63
64
65/**
66 * A language entry for the USB descriptor cache.
67 */
68typedef struct PDMUSBDESCCACHELANG
69{
70 /** The language ID for the strings in this block. */
71 uint16_t idLang;
72 /** The number of strings in the array. */
73 uint16_t cStrings;
74 /** Pointer to an array of associated strings.
75 * This must be sorted in ascending order by string index as a binary lookup
76 * will be performed. */
77 PCPDMUSBDESCCACHESTRING paStrings;
78} PDMUSBDESCCACHELANG;
79/** Pointer to a const language entry. */
80typedef PDMUSBDESCCACHELANG const *PCPDMUSBDESCCACHELANG;
81
82
83/**
84 * USB descriptor cache.
85 *
86 * This structure is owned by the USB device but provided to the PDM/VUSB layer
87 * thru the PDMUSBREG::pfnGetDescriptorCache method. PDM/VUSB will use the
88 * information here to map addresses to endpoints, perform SET_CONFIGURATION
89 * requests, and optionally perform GET_DESCRIPTOR requests (see flag).
90 *
91 * Currently, only device and configuration descriptors are cached.
92 */
93typedef struct PDMUSBDESCCACHE
94{
95 /** USB device descriptor */
96 PCVUSBDESCDEVICE pDevice;
97 /** USB Descriptor arrays (pDev->bNumConfigurations) */
98 PCVUSBDESCCONFIGEX paConfigs;
99 /** Language IDs and their associated strings.
100 * This must be sorted in ascending order by language ID as a binary lookup
101 * will be used. */
102 PCPDMUSBDESCCACHELANG paLanguages;
103 /** The number of entries in the array pointed to by paLanguages. */
104 uint16_t cLanguages;
105 /** Use the cached descriptors for GET_DESCRIPTOR requests. */
106 bool fUseCachedDescriptors;
107 /** Use the cached string descriptors. */
108 bool fUseCachedStringsDescriptors;
109} PDMUSBDESCCACHE;
110/** Pointer to an USB descriptor cache. */
111typedef PDMUSBDESCCACHE *PPDMUSBDESCCACHE;
112/** Pointer to a const USB descriptor cache. */
113typedef const PDMUSBDESCCACHE *PCPDMUSBDESCCACHE;
114
115
116/** PDM Device Flags.
117 * @{ */
118/** A high-speed capable USB 2.0 device (also required to support full-speed). */
119#define PDM_USBREG_HIGHSPEED_CAPABLE RT_BIT(0)
120/** Indicates that the device is fully emulated and is not used to pass through
121 * a host device. */
122#define PDM_USBREG_EMULATED_DEVICE RT_BIT(1)
123/** @} */
124
125/** PDM USB Device Registration Structure,
126 *
127 * This structure is used when registering a device from VBoxUsbRegister() in HC Ring-3.
128 * The PDM will make use of this structure until the VM is destroyed.
129 */
130typedef struct PDMUSBREG
131{
132 /** Structure version. PDM_DEVREG_VERSION defines the current version. */
133 uint32_t u32Version;
134 /** Device name. */
135 char szName[32];
136 /** The description of the device. The UTF-8 string pointed to shall, like this structure,
137 * remain unchanged from registration till VM destruction. */
138 const char *pszDescription;
139
140 /** Flags, combination of the PDM_USBREG_FLAGS_* \#defines. */
141 RTUINT fFlags;
142 /** Maximum number of instances (per VM). */
143 RTUINT cMaxInstances;
144 /** Size of the instance data. */
145 RTUINT cbInstance;
146
147
148 /**
149 * Construct an USB device instance for a VM.
150 *
151 * @returns VBox status.
152 * @param pUsbIns The USB device instance data.
153 * If the registration structure is needed, it will be
154 * accessible thru pUsbDev->pReg.
155 * @param iInstance Instance number. Use this to figure out which registers
156 * and such to use. The instance number is also found in
157 * pUsbDev->iInstance, but since it's likely to be
158 * frequently used PDM passes it as parameter.
159 * @param pCfg Configuration node handle for the device. Use this to
160 * obtain the configuration of the device instance. It is
161 * also found in pUsbDev->pCfg, but since it is primary
162 * usage will in this function it is passed as a parameter.
163 * @param pCfgGlobal Handle to the global device configuration. Also found
164 * in pUsbDev->pCfgGlobal.
165 * @remarks This callback is required.
166 */
167 DECLR3CALLBACKMEMBER(int, pfnConstruct,(PPDMUSBINS pUsbIns, int iInstance, PCFGMNODE pCfg, PCFGMNODE pCfgGlobal));
168
169 /**
170 * Destruct an USB device instance.
171 *
172 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
173 * resources can be freed correctly.
174 *
175 * This method will be called regardless of the pfnConstruct result to avoid
176 * complicated failure paths.
177 *
178 * @param pUsbIns The USB device instance data.
179 * @remarks Optional.
180 */
181 DECLR3CALLBACKMEMBER(void, pfnDestruct,(PPDMUSBINS pUsbIns));
182
183
184 /**
185 * Init complete notification.
186 *
187 * This can be done to do communication with other devices and other
188 * initialization which requires everything to be in place.
189 *
190 * @returns VBOX status code.
191 * @param pUsbIns The USB device instance data.
192 * @remarks Optional.
193 * @remarks Not called when hotplugged.
194 */
195 DECLR3CALLBACKMEMBER(int, pfnVMInitComplete,(PPDMUSBINS pUsbIns));
196
197 /**
198 * VM Power On notification.
199 *
200 * @returns VBox status.
201 * @param pUsbIns The USB device instance data.
202 * @remarks Optional.
203 */
204 DECLR3CALLBACKMEMBER(void, pfnVMPowerOn,(PPDMUSBINS pUsbIns));
205
206 /**
207 * VM Reset notification.
208 *
209 * @returns VBox status.
210 * @param pUsbIns The USB device instance data.
211 * @remarks Optional.
212 */
213 DECLR3CALLBACKMEMBER(void, pfnVMReset,(PPDMUSBINS pUsbIns));
214
215 /**
216 * VM Suspend notification.
217 *
218 * @returns VBox status.
219 * @param pUsbIns The USB device instance data.
220 * @remarks Optional.
221 */
222 DECLR3CALLBACKMEMBER(void, pfnVMSuspend,(PPDMUSBINS pUsbIns));
223
224 /**
225 * VM Resume notification.
226 *
227 * @returns VBox status.
228 * @param pUsbIns The USB device instance data.
229 * @remarks Optional.
230 */
231 DECLR3CALLBACKMEMBER(void, pfnVMResume,(PPDMUSBINS pUsbIns));
232
233 /**
234 * VM Power Off notification.
235 *
236 * This is only called when the VMR3PowerOff call is made on a running VM. This
237 * means that there is no notification if the VM was suspended before being
238 * powered of. There will also be no callback when hot plugging devices.
239 *
240 * @param pUsbIns The USB device instance data.
241 */
242 DECLR3CALLBACKMEMBER(void, pfnVMPowerOff,(PPDMUSBINS pUsbIns));
243
244 /**
245 * Called after the constructor when attaching a device at run time.
246 *
247 * This can be used to do tasks normally assigned to pfnInitComplete and/or pfnVMPowerOn.
248 *
249 * @returns VBox status.
250 * @param pUsbIns The USB device instance data.
251 * @remarks Optional.
252 */
253 DECLR3CALLBACKMEMBER(void, pfnHotPlugged,(PPDMUSBINS pUsbIns));
254
255 /**
256 * Called before the destructor when a device is unplugged at run time.
257 *
258 * This can be used to do tasks normally assigned to pfnVMSuspend and/or pfnVMPowerOff.
259 *
260 * @returns VBox status.
261 * @param pUsbIns The USB device instance data.
262 * @remarks Optional.
263 */
264 DECLR3CALLBACKMEMBER(void, pfnHotUnplugged,(PPDMUSBINS pUsbIns));
265 /**
266 * Driver Attach command.
267 *
268 * This is called to let the USB device attach to a driver for a specified LUN
269 * at runtime. This is not called during VM construction, the device constructor
270 * have to attach to all the available drivers.
271 *
272 * @returns VBox status code.
273 * @param pUsbIns The USB device instance data.
274 * @param iLUN The logical unit which is being detached.
275 * @param fFlags Flags, combination of the PDM_TACH_FLAGS_* \#defines.
276 * @remarks Optional.
277 */
278 DECLR3CALLBACKMEMBER(int, pfnDriverAttach,(PPDMUSBINS pUsbIns, unsigned iLUN, uint32_t fFlags));
279
280 /**
281 * Driver Detach notification.
282 *
283 * This is called when a driver is detaching itself from a LUN of the device.
284 * The device should adjust it's state to reflect this.
285 *
286 * @param pUsbIns The USB device instance data.
287 * @param iLUN The logical unit which is being detached.
288 * @param fFlags Flags, combination of the PDM_TACH_FLAGS_* \#defines.
289 * @remarks Optional.
290 */
291 DECLR3CALLBACKMEMBER(void, pfnDriverDetach,(PPDMUSBINS pUsbIns, unsigned iLUN, uint32_t fFlags));
292
293 /**
294 * Query the base interface of a logical unit.
295 *
296 * @returns VBOX status code.
297 * @param pUsbIns The USB device instance data.
298 * @param iLUN The logicial unit to query.
299 * @param ppBase Where to store the pointer to the base interface of the LUN.
300 * @remarks Optional.
301 */
302 DECLR3CALLBACKMEMBER(int, pfnQueryInterface,(PPDMUSBINS pUsbIns, unsigned iLUN, PPDMIBASE *ppBase));
303
304 /**
305 * Requests the USB device to reset.
306 *
307 * @returns VBox status code.
308 * @param pUsbIns The USB device instance.
309 * @param fResetOnLinux A hint to the usb proxy.
310 * Don't use this unless you're the linux proxy device.
311 * @thread Any thread.
312 * @remarks Optional.
313 */
314 DECLR3CALLBACKMEMBER(int, pfnUsbReset,(PPDMUSBINS pUsbIns, bool fResetOnLinux));
315
316 /**
317 * Query device and configuration descriptors for the caching and servicing
318 * relevant GET_DESCRIPTOR requests.
319 *
320 * @returns Pointer to the descriptor cache (read-only).
321 * @param pUsbIns The USB device instance.
322 * @remarks Mandatory.
323 */
324 DECLR3CALLBACKMEMBER(PCPDMUSBDESCCACHE, pfnUsbGetDescriptorCache,(PPDMUSBINS pUsbIns));
325
326 /**
327 * SET_CONFIGURATION request.
328 *
329 * @returns VBox status code.
330 * @param pUsbIns The USB device instance.
331 * @param bConfigurationValue The bConfigurationValue of the new configuration.
332 * @param pvOldCfgDesc Internal - for the device proxy.
333 * @param pvOldIfState Internal - for the device proxy.
334 * @param pvNewCfgDesc Internal - for the device proxy.
335 * @remarks Optional.
336 */
337 DECLR3CALLBACKMEMBER(int, pfnUsbSetConfiguration,(PPDMUSBINS pUsbIns, uint8_t bConfigurationValue,
338 const void *pvOldCfgDesc, const void *pvOldIfState, const void *pvNewCfgDesc));
339
340 /**
341 * SET_INTERFACE request.
342 *
343 * @returns VBox status code.
344 * @param pUsbIns The USB device instance.
345 * @param bInterfaceNumber The interface number.
346 * @param bAlternateSetting The alternate setting.
347 * @remarks Optional.
348 */
349 DECLR3CALLBACKMEMBER(int, pfnUsbSetInterface,(PPDMUSBINS pUsbIns, uint8_t bInterfaceNumber, uint8_t bAlternateSetting));
350
351 /**
352 * Clears the halted state of an endpoint. (Optional)
353 *
354 * This called when VUSB sees a CLEAR_FEATURE(ENDPOINT_HALT) on request
355 * on the zero pipe.
356 *
357 * @returns VBox status code.
358 * @param pUsbIns The USB device instance.
359 * @param uEndpoint The endpoint to clear.
360 * @remarks Optional.
361 */
362 DECLR3CALLBACKMEMBER(int, pfnUsbClearHaltedEndpoint,(PPDMUSBINS pUsbIns, unsigned uEndpoint));
363
364 /**
365 * Allocates an URB.
366 *
367 * This can be used to make use of shared user/kernel mode buffers.
368 *
369 * @returns VBox status code.
370 * @param pUsbIns The USB device instance.
371 * @param cbData The size of the data buffer.
372 * @param cTds The number of TDs.
373 * @param enmType The type of URB.
374 * @param ppUrb Where to store the allocated URB.
375 * @remarks Optional.
376 * @remarks Not implemented yet.
377 */
378 DECLR3CALLBACKMEMBER(int, pfnUrbNew,(PPDMUSBINS pUsbIns, size_t cbData, size_t cTds, VUSBXFERTYPE enmType, PVUSBURB *ppUrb));
379
380 /**
381 * Queues an URB for processing.
382 *
383 * @returns VBox status code.
384 * @retval VINF_SUCCESS on success.
385 * @retval VERR_VUSB_DEVICE_NOT_ATTACHED if the device has been disconnected.
386 * @retval VERR_VUSB_FAILED_TO_QUEUE_URB as a general failure kind of thing.
387 * @retval TBD - document new stuff!
388 *
389 * @param pUsbIns The USB device instance.
390 * @param pUrb The URB to process.
391 * @remarks Mandatory.
392 */
393 DECLR3CALLBACKMEMBER(int, pfnUrbQueue,(PPDMUSBINS pUsbIns, PVUSBURB pUrb));
394
395 /**
396 * Cancels an URB.
397 *
398 * @returns VBox status code.
399 * @param pUsbIns The USB device instance.
400 * @param pUrb The URB to cancel.
401 * @remarks Mandatory.
402 */
403 DECLR3CALLBACKMEMBER(int, pfnUrbCancel,(PPDMUSBINS pUsbIns, PVUSBURB pUrb));
404
405 /**
406 * Reaps an URB.
407 *
408 * @returns A ripe URB, NULL if none.
409 * @param pUsbIns The USB device instance.
410 * @param cMillies How log to wait for an URB to become ripe.
411 * @remarks Mandatory.
412 */
413 DECLR3CALLBACKMEMBER(PVUSBURB, pfnUrbReap,(PPDMUSBINS pUsbIns, RTMSINTERVAL cMillies));
414
415
416 /** Just some init precaution. Must be set to PDM_USBREG_VERSION. */
417 uint32_t u32TheEnd;
418} PDMUSBREG;
419/** Pointer to a PDM USB Device Structure. */
420typedef PDMUSBREG *PPDMUSBREG;
421/** Const pointer to a PDM USB Device Structure. */
422typedef PDMUSBREG const *PCPDMUSBREG;
423
424/** Current USBREG version number. */
425#define PDM_USBREG_VERSION PDM_VERSION_MAKE(0xeeff, 1, 0)
426
427/** PDM USB Device Flags.
428 * @{ */
429/* none yet */
430/** @} */
431
432
433#ifdef IN_RING3
434
435/**
436 * PDM USB Device API.
437 */
438typedef struct PDMUSBHLP
439{
440 /** Structure version. PDM_USBHLP_VERSION defines the current version. */
441 uint32_t u32Version;
442
443 /**
444 * Attaches a driver (chain) to the USB device.
445 *
446 * The first call for a LUN this will serve as a registartion of the LUN. The pBaseInterface and
447 * the pszDesc string will be registered with that LUN and kept around for PDMR3QueryUSBDeviceLun().
448 *
449 * @returns VBox status code.
450 * @param pUsbIns The USB device instance.
451 * @param iLun The logical unit to attach.
452 * @param pBaseInterface Pointer to the base interface for that LUN. (device side / down)
453 * @param ppBaseInterface Where to store the pointer to the base interface. (driver side / up)
454 * @param pszDesc Pointer to a string describing the LUN. This string must remain valid
455 * for the live of the device instance.
456 */
457 DECLR3CALLBACKMEMBER(int, pfnDriverAttach,(PPDMUSBINS pUsbIns, RTUINT iLun, PPDMIBASE pBaseInterface, PPDMIBASE *ppBaseInterface, const char *pszDesc));
458
459 /**
460 * Assert that the current thread is the emulation thread.
461 *
462 * @returns True if correct.
463 * @returns False if wrong.
464 * @param pUsbIns The USB device instance.
465 * @param pszFile Filename of the assertion location.
466 * @param iLine Linenumber of the assertion location.
467 * @param pszFunction Function of the assertion location.
468 */
469 DECLR3CALLBACKMEMBER(bool, pfnAssertEMT,(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction));
470
471 /**
472 * Assert that the current thread is NOT the emulation thread.
473 *
474 * @returns True if correct.
475 * @returns False if wrong.
476 * @param pUsbIns The USB device instance.
477 * @param pszFile Filename of the assertion location.
478 * @param iLine Linenumber of the assertion location.
479 * @param pszFunction Function of the assertion location.
480 */
481 DECLR3CALLBACKMEMBER(bool, pfnAssertOther,(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction));
482
483 /**
484 * Stops the VM and enters the debugger to look at the guest state.
485 *
486 * Use the PDMUsbDBGFStop() inline function with the RT_SRC_POS macro instead of
487 * invoking this function directly.
488 *
489 * @returns VBox status code which must be passed up to the VMM.
490 * @param pUsbIns The USB device instance.
491 * @param pszFile Filename of the assertion location.
492 * @param iLine The linenumber of the assertion location.
493 * @param pszFunction Function of the assertion location.
494 * @param pszFormat Message. (optional)
495 * @param va Message parameters.
496 */
497 DECLR3CALLBACKMEMBER(int, pfnDBGFStopV,(PPDMUSBINS pUsbIns, const char *pszFile, unsigned iLine, const char *pszFunction, const char *pszFormat, va_list va));
498
499 /**
500 * Register a info handler with DBGF,
501 *
502 * @returns VBox status code.
503 * @param pUsbIns The USB device instance.
504 * @param pszName The identifier of the info.
505 * @param pszDesc The description of the info and any arguments the handler may take.
506 * @param pfnHandler The handler function to be called to display the info.
507 */
508 DECLR3CALLBACKMEMBER(int, pfnDBGFInfoRegister,(PPDMUSBINS pUsbIns, const char *pszName, const char *pszDesc, PFNDBGFHANDLERUSB pfnHandler));
509
510 /**
511 * Allocate memory which is associated with current VM instance
512 * and automatically freed on it's destruction.
513 *
514 * @returns Pointer to allocated memory. The memory is *NOT* zero-ed.
515 * @param pUsbIns The USB device instance.
516 * @param cb Number of bytes to allocate.
517 */
518 DECLR3CALLBACKMEMBER(void *, pfnMMHeapAlloc,(PPDMUSBINS pUsbIns, size_t cb));
519
520 /**
521 * Allocate memory which is associated with current VM instance
522 * and automatically freed on it's destruction. The memory is ZEROed.
523 *
524 * @returns Pointer to allocated memory. The memory is *NOT* zero-ed.
525 * @param pUsbIns The USB device instance.
526 * @param cb Number of bytes to allocate.
527 */
528 DECLR3CALLBACKMEMBER(void *, pfnMMHeapAllocZ,(PPDMUSBINS pUsbIns, size_t cb));
529
530 /**
531 * Create a queue.
532 *
533 * @returns VBox status code.
534 * @param pUsbIns The USB device instance.
535 * @param cbItem Size a queue item.
536 * @param cItems Number of items in the queue.
537 * @param cMilliesInterval Number of milliseconds between polling the queue.
538 * If 0 then the emulation thread will be notified whenever an item arrives.
539 * @param pfnCallback The consumer function.
540 * @param pszName The queue base name. The instance number will be
541 * appended automatically.
542 * @param ppQueue Where to store the queue handle on success.
543 * @thread The emulation thread.
544 */
545 DECLR3CALLBACKMEMBER(int, pfnPDMQueueCreate,(PPDMUSBINS pUsbIns, RTUINT cbItem, RTUINT cItems, uint32_t cMilliesInterval,
546 PFNPDMQUEUEUSB pfnCallback, const char *pszName, PPDMQUEUE *ppQueue));
547
548 /**
549 * Register a save state data unit.
550 *
551 * @returns VBox status.
552 * @param pUsbIns The USB device instance.
553 * @param uVersion Data layout version number.
554 * @param cbGuess The approximate amount of data in the unit.
555 * Only for progress indicators.
556 *
557 * @param pfnLivePrep Prepare live save callback, optional.
558 * @param pfnLiveExec Execute live save callback, optional.
559 * @param pfnLiveVote Vote live save callback, optional.
560 *
561 * @param pfnSavePrep Prepare save callback, optional.
562 * @param pfnSaveExec Execute save callback, optional.
563 * @param pfnSaveDone Done save callback, optional.
564 *
565 * @param pfnLoadPrep Prepare load callback, optional.
566 * @param pfnLoadExec Execute load callback, optional.
567 * @param pfnLoadDone Done load callback, optional.
568 */
569 DECLR3CALLBACKMEMBER(int, pfnSSMRegister,(PPDMUSBINS pUsbIns, uint32_t uVersion, size_t cbGuess,
570 PFNSSMUSBLIVEPREP pfnLivePrep, PFNSSMUSBLIVEEXEC pfnLiveExec, PFNSSMUSBLIVEVOTE pfnLiveVote,
571 PFNSSMUSBSAVEPREP pfnSavePrep, PFNSSMUSBSAVEEXEC pfnSaveExec, PFNSSMUSBSAVEDONE pfnSaveDone,
572 PFNSSMUSBLOADPREP pfnLoadPrep, PFNSSMUSBLOADEXEC pfnLoadExec, PFNSSMUSBLOADDONE pfnLoadDone));
573
574 /**
575 * Register a STAM sample.
576 *
577 * Use the PDMUsbHlpSTAMRegister wrapper.
578 *
579 * @returns VBox status.
580 * @param pUsbIns The USB device instance.
581 * @param pvSample Pointer to the sample.
582 * @param enmType Sample type. This indicates what pvSample is pointing at.
583 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
584 * @param enmUnit Sample unit.
585 * @param pszDesc Sample description.
586 * @param pszName The sample name format string.
587 * @param va Arguments to the format string.
588 */
589 DECLR3CALLBACKMEMBER(void, pfnSTAMRegisterV,(PPDMUSBINS pUsbIns, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility,
590 STAMUNIT enmUnit, const char *pszDesc, const char *pszName, va_list va));
591
592 /**
593 * Creates a timer.
594 *
595 * @returns VBox status.
596 * @param pUsbIns The USB device instance.
597 * @param enmClock The clock to use on this timer.
598 * @param pfnCallback Callback function.
599 * @param pvUser User argument for the callback.
600 * @param fFlags Flags, see TMTIMER_FLAGS_*.
601 * @param pszDesc Pointer to description string which must stay around
602 * until the timer is fully destroyed (i.e. a bit after TMTimerDestroy()).
603 * @param ppTimer Where to store the timer on success.
604 */
605 DECLR3CALLBACKMEMBER(int, pfnTMTimerCreate,(PPDMUSBINS pUsbIns, TMCLOCK enmClock, PFNTMTIMERUSB pfnCallback, void *pvUser,
606 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer));
607
608 /**
609 * Set the VM error message
610 *
611 * @returns rc.
612 * @param pUsbIns The USB device instance.
613 * @param rc VBox status code.
614 * @param RT_SRC_POS_DECL Use RT_SRC_POS.
615 * @param pszFormat Error message format string.
616 * @param va Error message arguments.
617 */
618 DECLR3CALLBACKMEMBER(int, pfnVMSetErrorV,(PPDMUSBINS pUsbIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va));
619
620 /**
621 * Set the VM runtime error message
622 *
623 * @returns VBox status code.
624 * @param pUsbIns The USB device instance.
625 * @param fFlags The action flags. See VMSETRTERR_FLAGS_*.
626 * @param pszErrorId Error ID string.
627 * @param pszFormat Error message format string.
628 * @param va Error message arguments.
629 */
630 DECLR3CALLBACKMEMBER(int, pfnVMSetRuntimeErrorV,(PPDMUSBINS pUsbIns, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va));
631
632 /**
633 * Gets the VM state.
634 *
635 * @returns VM state.
636 * @param pUsbIns The USB device instance.
637 * @thread Any thread (just keep in mind that it's volatile info).
638 */
639 DECLR3CALLBACKMEMBER(VMSTATE, pfnVMState, (PPDMUSBINS pUsbIns));
640
641 /**
642 * Creates a PDM thread.
643 *
644 * This differs from the RTThreadCreate() API in that PDM takes care of suspending,
645 * resuming, and destroying the thread as the VM state changes.
646 *
647 * @returns VBox status code.
648 * @param pDevIns The device instance.
649 * @param ppThread Where to store the thread 'handle'.
650 * @param pvUser The user argument to the thread function.
651 * @param pfnThread The thread function.
652 * @param pfnWakeup The wakup callback. This is called on the EMT
653 * thread when a state change is pending.
654 * @param cbStack See RTThreadCreate.
655 * @param enmType See RTThreadCreate.
656 * @param pszName See RTThreadCreate.
657 */
658 DECLR3CALLBACKMEMBER(int, pfnThreadCreate,(PPDMUSBINS pUsbIns, PPPDMTHREAD ppThread, void *pvUser, PFNPDMTHREADUSB pfnThread,
659 PFNPDMTHREADWAKEUPUSB pfnWakeup, size_t cbStack, RTTHREADTYPE enmType, const char *pszName));
660
661 /**
662 * Set up asynchronous handling of a suspend, reset or power off notification.
663 *
664 * This shall only be called when getting the notification. It must be called
665 * for each one.
666 *
667 * @returns VBox status code.
668 * @param pUSBIns The USB device instance.
669 * @param pfnAsyncNotify The callback.
670 * @thread EMT(0)
671 */
672 DECLR3CALLBACKMEMBER(int, pfnSetAsyncNotification, (PPDMUSBINS pUSbIns, PFNPDMUSBASYNCNOTIFY pfnAsyncNotify));
673
674 /**
675 * Notify EMT(0) that the device has completed the asynchronous notification
676 * handling.
677 *
678 * This can be called at any time, spurious calls will simply be ignored.
679 *
680 * @param pUSBIns The USB device instance.
681 * @thread Any
682 */
683 DECLR3CALLBACKMEMBER(void, pfnAsyncNotificationCompleted, (PPDMUSBINS pUsbIns));
684
685 /**
686 * Gets the reason for the most recent VM suspend.
687 *
688 * @returns The suspend reason. VMSUSPENDREASON_INVALID is returned if no
689 * suspend has been made or if the pUsbIns is invalid.
690 * @param pUsbIns The driver instance.
691 */
692 DECLR3CALLBACKMEMBER(VMSUSPENDREASON, pfnVMGetSuspendReason,(PPDMUSBINS pUsbIns));
693
694 /**
695 * Gets the reason for the most recent VM resume.
696 *
697 * @returns The resume reason. VMRESUMEREASON_INVALID is returned if no
698 * resume has been made or if the pUsbIns is invalid.
699 * @param pUsbIns The driver instance.
700 */
701 DECLR3CALLBACKMEMBER(VMRESUMEREASON, pfnVMGetResumeReason,(PPDMUSBINS pUsbIns));
702
703 /** @name Space reserved for minor interface changes.
704 * @{ */
705 DECLR3CALLBACKMEMBER(void, pfnReserved0,(PPDMUSBINS pUsbIns));
706 DECLR3CALLBACKMEMBER(void, pfnReserved1,(PPDMUSBINS pUsbIns));
707 DECLR3CALLBACKMEMBER(void, pfnReserved2,(PPDMUSBINS pUsbIns));
708 DECLR3CALLBACKMEMBER(void, pfnReserved3,(PPDMUSBINS pUsbIns));
709 DECLR3CALLBACKMEMBER(void, pfnReserved4,(PPDMUSBINS pUsbIns));
710 DECLR3CALLBACKMEMBER(void, pfnReserved5,(PPDMUSBINS pUsbIns));
711 DECLR3CALLBACKMEMBER(void, pfnReserved6,(PPDMUSBINS pUsbIns));
712 DECLR3CALLBACKMEMBER(void, pfnReserved7,(PPDMUSBINS pUsbIns));
713 DECLR3CALLBACKMEMBER(void, pfnReserved8,(PPDMUSBINS pUsbIns));
714 DECLR3CALLBACKMEMBER(void, pfnReserved9,(PPDMUSBINS pUsbIns));
715 /** @} */
716
717 /** Just a safety precaution. */
718 uint32_t u32TheEnd;
719} PDMUSBHLP;
720/** Pointer PDM USB Device API. */
721typedef PDMUSBHLP *PPDMUSBHLP;
722/** Pointer const PDM USB Device API. */
723typedef const PDMUSBHLP *PCPDMUSBHLP;
724
725/** Current USBHLP version number. */
726#define PDM_USBHLP_VERSION PDM_VERSION_MAKE(0xeefe, 3, 0)
727
728#endif /* IN_RING3 */
729
730/**
731 * PDM USB Device Instance.
732 */
733typedef struct PDMUSBINS
734{
735 /** Structure version. PDM_USBINS_VERSION defines the current version. */
736 uint32_t u32Version;
737 /** USB device instance number. */
738 uint32_t iInstance;
739 /** The base interface of the device.
740 * The device constructor initializes this if it has any device level
741 * interfaces to export. To obtain this interface call PDMR3QueryUSBDevice(). */
742 PDMIBASE IBase;
743#if HC_ARCH_BITS == 32
744 uint32_t u32Alignment; /**< Alignment padding. */
745#endif
746
747 /** Internal data. */
748 union
749 {
750#ifdef PDMUSBINSINT_DECLARED
751 PDMUSBINSINT s;
752#endif
753 uint8_t padding[HC_ARCH_BITS == 32 ? 96 : 128];
754 } Internal;
755
756 /** Pointer the PDM USB Device API. */
757 R3PTRTYPE(PCPDMUSBHLP) pHlpR3;
758 /** Pointer to the USB device registration structure. */
759 R3PTRTYPE(PCPDMUSBREG) pReg;
760 /** Configuration handle. */
761 R3PTRTYPE(PCFGMNODE) pCfg;
762 /** The (device) global configuration handle. */
763 R3PTRTYPE(PCFGMNODE) pCfgGlobal;
764 /** Pointer to device instance data. */
765 R3PTRTYPE(void *) pvInstanceDataR3;
766 /** Pointer to the VUSB Device structure.
767 * Internal to VUSB, don't touch.
768 * @todo Moved this to PDMUSBINSINT. */
769 R3PTRTYPE(void *) pvVUsbDev2;
770 /** Device name for using when logging.
771 * The constructor sets this and the destructor frees it. */
772 R3PTRTYPE(char *) pszName;
773 /** Tracing indicator. */
774 uint32_t fTracing;
775 /** The tracing ID of this device. */
776 uint32_t idTracing;
777 /** The USB version of the hub this device is attached to. Used to
778 * determine whether the device communicates at high-speed or full-/low-speed. */
779 uint32_t iUsbHubVersion;
780
781 /** Padding to make achInstanceData aligned at 32 byte boundary. */
782 uint32_t au32Padding[HC_ARCH_BITS == 32 ? 2 : 3];
783
784 /** Device instance data. The size of this area is defined
785 * in the PDMUSBREG::cbInstanceData field. */
786 char achInstanceData[8];
787} PDMUSBINS;
788
789/** Current USBINS version number. */
790#define PDM_USBINS_VERSION PDM_VERSION_MAKE(0xeefd, 2, 0)
791
792/**
793 * Checks the structure versions of the USB device instance and USB device
794 * helpers, returning if they are incompatible.
795 *
796 * This is for use in the constructor.
797 *
798 * @param pUsbIns The USB device instance pointer.
799 */
800#define PDMUSB_CHECK_VERSIONS_RETURN(pUsbIns) \
801 do \
802 { \
803 PPDMUSBINS pUsbInsTypeCheck = (pUsbIns); NOREF(pUsbInsTypeCheck); \
804 AssertLogRelMsgReturn(PDM_VERSION_ARE_COMPATIBLE((pUsbIns)->u32Version, PDM_USBINS_VERSION), \
805 ("DevIns=%#x mine=%#x\n", (pUsbIns)->u32Version, PDM_USBINS_VERSION), \
806 VERR_PDM_USBINS_VERSION_MISMATCH); \
807 AssertLogRelMsgReturn(PDM_VERSION_ARE_COMPATIBLE((pUsbIns)->pHlpR3->u32Version, PDM_USBHLP_VERSION), \
808 ("DevHlp=%#x mine=%#x\n", (pUsbIns)->pHlpR3->u32Version, PDM_USBHLP_VERSION), \
809 VERR_PDM_USBHLPR3_VERSION_MISMATCH); \
810 } while (0)
811
812/**
813 * Quietly checks the structure versions of the USB device instance and
814 * USB device helpers, returning if they are incompatible.
815 *
816 * This is for use in the destructor.
817 *
818 * @param pUsbIns The USB device instance pointer.
819 */
820#define PDMUSB_CHECK_VERSIONS_RETURN_QUIET(pUsbIns) \
821 do \
822 { \
823 PPDMUSBINS pUsbInsTypeCheck = (pUsbIns); NOREF(pUsbInsTypeCheck); \
824 if (RT_UNLIKELY(!PDM_VERSION_ARE_COMPATIBLE((pUsbIns)->u32Version, PDM_USBINS_VERSION) )) \
825 return VERR_PDM_USBINS_VERSION_MISMATCH; \
826 if (RT_UNLIKELY(!PDM_VERSION_ARE_COMPATIBLE((pUsbIns)->pHlpR3->u32Version, PDM_USBHLPR3_VERSION) )) \
827 return VERR_PDM_USBHLPR3_VERSION_MISMATCH; \
828 } while (0)
829
830
831/** Converts a pointer to the PDMUSBINS::IBase to a pointer to PDMUSBINS. */
832#define PDMIBASE_2_PDMUSB(pInterface) ( (PPDMUSBINS)((char *)(pInterface) - RT_OFFSETOF(PDMUSBINS, IBase)) )
833
834
835/** @def PDMUSB_ASSERT_EMT
836 * Assert that the current thread is the emulation thread.
837 */
838#ifdef VBOX_STRICT
839# define PDMUSB_ASSERT_EMT(pUsbIns) pUsbIns->pHlpR3->pfnAssertEMT(pUsbIns, __FILE__, __LINE__, __FUNCTION__)
840#else
841# define PDMUSB_ASSERT_EMT(pUsbIns) do { } while (0)
842#endif
843
844/** @def PDMUSB_ASSERT_OTHER
845 * Assert that the current thread is NOT the emulation thread.
846 */
847#ifdef VBOX_STRICT
848# define PDMUSB_ASSERT_OTHER(pUsbIns) pUsbIns->pHlpR3->pfnAssertOther(pUsbIns, __FILE__, __LINE__, __FUNCTION__)
849#else
850# define PDMUSB_ASSERT_OTHER(pUsbIns) do { } while (0)
851#endif
852
853/** @def PDMUSB_SET_ERROR
854 * Set the VM error. See PDMUsbHlpVMSetError() for printf like message
855 * formatting.
856 */
857#define PDMUSB_SET_ERROR(pUsbIns, rc, pszError) \
858 PDMUsbHlpVMSetError(pUsbIns, rc, RT_SRC_POS, "%s", pszError)
859
860/** @def PDMUSB_SET_RUNTIME_ERROR
861 * Set the VM runtime error. See PDMUsbHlpVMSetRuntimeError() for printf like
862 * message formatting.
863 */
864#define PDMUSB_SET_RUNTIME_ERROR(pUsbIns, fFlags, pszErrorId, pszError) \
865 PDMUsbHlpVMSetRuntimeError(pUsbIns, fFlags, pszErrorId, "%s", pszError)
866
867
868#ifdef IN_RING3
869
870/**
871 * @copydoc PDMUSBHLP::pfnDriverAttach
872 */
873DECLINLINE(int) PDMUsbHlpDriverAttach(PPDMUSBINS pUsbIns, RTUINT iLun, PPDMIBASE pBaseInterface, PPDMIBASE *ppBaseInterface, const char *pszDesc)
874{
875 return pUsbIns->pHlpR3->pfnDriverAttach(pUsbIns, iLun, pBaseInterface, ppBaseInterface, pszDesc);
876}
877
878/**
879 * VBOX_STRICT wrapper for pHlpR3->pfnDBGFStopV.
880 *
881 * @returns VBox status code which must be passed up to the VMM.
882 * @param pUsbIns Device instance.
883 * @param RT_SRC_POS_DECL Use RT_SRC_POS.
884 * @param pszFormat Message. (optional)
885 * @param ... Message parameters.
886 */
887DECLINLINE(int) PDMUsbDBGFStop(PPDMUSBINS pUsbIns, RT_SRC_POS_DECL, const char *pszFormat, ...)
888{
889#ifdef VBOX_STRICT
890 int rc;
891 va_list va;
892 va_start(va, pszFormat);
893 rc = pUsbIns->pHlpR3->pfnDBGFStopV(pUsbIns, RT_SRC_POS_ARGS, pszFormat, va);
894 va_end(va);
895 return rc;
896#else
897 NOREF(pUsbIns);
898 NOREF(pszFile);
899 NOREF(iLine);
900 NOREF(pszFunction);
901 NOREF(pszFormat);
902 return VINF_SUCCESS;
903#endif
904}
905
906/**
907 * @copydoc PDMUSBHLP::pfnVMState
908 */
909DECLINLINE(VMSTATE) PDMUsbHlpVMState(PPDMUSBINS pUsbIns)
910{
911 return pUsbIns->pHlpR3->pfnVMState(pUsbIns);
912}
913
914/**
915 * @copydoc PDMUSBHLP::pfnThreadCreate
916 */
917DECLINLINE(int) PDMUsbHlpThreadCreate(PPDMUSBINS pUsbIns, PPPDMTHREAD ppThread, void *pvUser, PFNPDMTHREADUSB pfnThread,
918 PFNPDMTHREADWAKEUPUSB pfnWakeup, size_t cbStack, RTTHREADTYPE enmType, const char *pszName)
919{
920 return pUsbIns->pHlpR3->pfnThreadCreate(pUsbIns, ppThread, pvUser, pfnThread, pfnWakeup, cbStack, enmType, pszName);
921}
922
923
924/**
925 * @copydoc PDMUSBHLP::pfnSetAsyncNotification
926 */
927DECLINLINE(int) PDMUsbHlpSetAsyncNotification(PPDMUSBINS pUsbIns, PFNPDMUSBASYNCNOTIFY pfnAsyncNotify)
928{
929 return pUsbIns->pHlpR3->pfnSetAsyncNotification(pUsbIns, pfnAsyncNotify);
930}
931
932/**
933 * @copydoc PDMUSBHLP::pfnAsyncNotificationCompleted
934 */
935DECLINLINE(void) PDMUsbHlpAsyncNotificationCompleted(PPDMUSBINS pUsbIns)
936{
937 pUsbIns->pHlpR3->pfnAsyncNotificationCompleted(pUsbIns);
938}
939
940/**
941 * Set the VM error message
942 *
943 * @returns rc.
944 * @param pUsbIns The USB device instance.
945 * @param rc VBox status code.
946 * @param RT_SRC_POS_DECL Use RT_SRC_POS.
947 * @param pszFormat Error message format string.
948 * @param ... Error message arguments.
949 */
950DECLINLINE(int) PDMUsbHlpVMSetError(PPDMUSBINS pUsbIns, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
951{
952 va_list va;
953 va_start(va, pszFormat);
954 rc = pUsbIns->pHlpR3->pfnVMSetErrorV(pUsbIns, rc, RT_SRC_POS_ARGS, pszFormat, va);
955 va_end(va);
956 return rc;
957}
958
959/**
960 * @copydoc PDMUSBHLP::pfnMMHeapAlloc
961 */
962DECLINLINE(void *) PDMUsbHlpMMHeapAlloc(PPDMUSBINS pUsbIns, size_t cb)
963{
964 return pUsbIns->pHlpR3->pfnMMHeapAlloc(pUsbIns, cb);
965}
966
967/**
968 * @copydoc PDMUSBHLP::pfnMMHeapAllocZ
969 */
970DECLINLINE(void *) PDMUsbHlpMMHeapAllocZ(PPDMUSBINS pUsbIns, size_t cb)
971{
972 return pUsbIns->pHlpR3->pfnMMHeapAllocZ(pUsbIns, cb);
973}
974
975/**
976 * Frees memory allocated by PDMUsbHlpMMHeapAlloc or PDMUsbHlpMMHeapAllocZ.
977 *
978 * @param pUsbIns The USB device instance.
979 * @param pv The memory to free. NULL is fine.
980 */
981DECLINLINE(void) PDMUsbHlpMMHeapFree(PPDMUSBINS pUsbIns, void *pv)
982{
983 NOREF(pUsbIns);
984 MMR3HeapFree(pv);
985}
986
987/**
988 * @copydoc PDMUSBHLP::pfnTMTimerCreate
989 */
990DECLINLINE(int) PDMUsbHlpTMTimerCreate(PPDMUSBINS pUsbIns, TMCLOCK enmClock, PFNTMTIMERUSB pfnCallback, void *pvUser,
991 uint32_t fFlags, const char *pszDesc, PPTMTIMERR3 ppTimer)
992{
993 return pUsbIns->pHlpR3->pfnTMTimerCreate(pUsbIns, enmClock, pfnCallback, pvUser, fFlags, pszDesc, ppTimer);
994}
995
996/**
997 * @copydoc PDMUSBHLP::pfnSSMRegister
998 */
999DECLINLINE(int) PDMUsbHlpSSMRegister(PPDMUSBINS pUsbIns, uint32_t uVersion, size_t cbGuess,
1000 PFNSSMUSBLIVEPREP pfnLivePrep, PFNSSMUSBLIVEEXEC pfnLiveExec, PFNSSMUSBLIVEVOTE pfnLiveVote,
1001 PFNSSMUSBSAVEPREP pfnSavePrep, PFNSSMUSBSAVEEXEC pfnSaveExec, PFNSSMUSBSAVEDONE pfnSaveDone,
1002 PFNSSMUSBLOADPREP pfnLoadPrep, PFNSSMUSBLOADEXEC pfnLoadExec, PFNSSMUSBLOADDONE pfnLoadDone)
1003{
1004 return pUsbIns->pHlpR3->pfnSSMRegister(pUsbIns, uVersion, cbGuess,
1005 pfnLivePrep, pfnLiveExec, pfnLiveVote,
1006 pfnSavePrep, pfnSaveExec, pfnSaveDone,
1007 pfnLoadPrep, pfnLoadExec, pfnLoadDone);
1008}
1009
1010#endif /* IN_RING3 */
1011
1012
1013
1014/** Pointer to callbacks provided to the VBoxUsbRegister() call. */
1015typedef const struct PDMUSBREGCB *PCPDMUSBREGCB;
1016
1017/**
1018 * Callbacks for VBoxUSBDeviceRegister().
1019 */
1020typedef struct PDMUSBREGCB
1021{
1022 /** Interface version.
1023 * This is set to PDM_USBREG_CB_VERSION. */
1024 uint32_t u32Version;
1025
1026 /**
1027 * Registers a device with the current VM instance.
1028 *
1029 * @returns VBox status code.
1030 * @param pCallbacks Pointer to the callback table.
1031 * @param pReg Pointer to the USB device registration record.
1032 * This data must be permanent and readonly.
1033 */
1034 DECLR3CALLBACKMEMBER(int, pfnRegister,(PCPDMUSBREGCB pCallbacks, PCPDMUSBREG pReg));
1035} PDMUSBREGCB;
1036
1037/** Current version of the PDMUSBREGCB structure. */
1038#define PDM_USBREG_CB_VERSION PDM_VERSION_MAKE(0xeefc, 1, 0)
1039
1040
1041/**
1042 * The VBoxUsbRegister callback function.
1043 *
1044 * PDM will invoke this function after loading a USB device module and letting
1045 * the module decide which devices to register and how to handle conflicts.
1046 *
1047 * @returns VBox status code.
1048 * @param pCallbacks Pointer to the callback table.
1049 * @param u32Version VBox version number.
1050 */
1051typedef DECLCALLBACK(int) FNPDMVBOXUSBREGISTER(PCPDMUSBREGCB pCallbacks, uint32_t u32Version);
1052
1053VMMR3DECL(int) PDMR3UsbCreateEmulatedDevice(PUVM pUVM, const char *pszDeviceName, PCFGMNODE pDeviceNode, PCRTUUID pUuid);
1054VMMR3DECL(int) PDMR3UsbCreateProxyDevice(PUVM pUVM, PCRTUUID pUuid, bool fRemote, const char *pszAddress, void *pvBackend,
1055 uint32_t iUsbVersion, uint32_t fMaskedIfs);
1056VMMR3DECL(int) PDMR3UsbDetachDevice(PUVM pUVM, PCRTUUID pUuid);
1057VMMR3DECL(bool) PDMR3UsbHasHub(PUVM pUVM);
1058VMMR3DECL(int) PDMR3UsbDriverAttach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun, uint32_t fFlags,
1059 PPPDMIBASE ppBase);
1060VMMR3DECL(int) PDMR3UsbDriverDetach(PUVM pUVM, const char *pszDevice, unsigned iDevIns, unsigned iLun,
1061 const char *pszDriver, unsigned iOccurance, uint32_t fFlags);
1062
1063/** @} */
1064
1065RT_C_DECLS_END
1066
1067#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette