VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/VUSBInternal.h@ 44547

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

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
Line 
1/* $Id: VUSBInternal.h 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * Virtual USB - Internal header.
4 *
5 * This subsystem implements USB devices in a host controller independent
6 * way. All the host controller code has to do is use VUSBHUB for its
7 * root hub implementation and any emulated USB device may be plugged into
8 * the virtual bus.
9 */
10
11/*
12 * Copyright (C) 2006-2011 Oracle Corporation
13 *
14 * This file is part of VirtualBox Open Source Edition (OSE), as
15 * available from http://www.virtualbox.org. This file is free software;
16 * you can redistribute it and/or modify it under the terms of the GNU
17 * General Public License (GPL) as published by the Free Software
18 * Foundation, in version 2 as it comes in the "COPYING" file of the
19 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
20 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
21 */
22
23#ifndef ___VUSBInternal_h
24#define ___VUSBInternal_h
25
26#include <VBox/cdefs.h>
27#include <VBox/types.h>
28#include <VBox/vusb.h>
29#include <VBox/vmm/stam.h>
30#include <iprt/assert.h>
31
32RT_C_DECLS_BEGIN
33
34
35/** @name Internal Device Operations, Structures and Constants.
36 * @{
37 */
38
39/** Pointer to a Virtual USB device (core). */
40typedef struct VUSBDEV *PVUSBDEV;
41/** Pointer to a VUSB hub device. */
42typedef struct VUSBHUB *PVUSBHUB;
43/** Pointer to a VUSB root hub. */
44typedef struct VUSBROOTHUB *PVUSBROOTHUB;
45
46
47/** Number of the default control endpoint */
48#define VUSB_PIPE_DEFAULT 0
49
50/** @name Device addresses
51 * @{ */
52#define VUSB_DEFAULT_ADDRESS 0
53#define VUSB_INVALID_ADDRESS UINT8_C(0xff)
54/** @} */
55
56/** @name Feature bits (1<<FEATURE for the u16Status bit)
57 * @{ */
58#define VUSB_DEV_SELF_POWERED 0
59#define VUSB_DEV_REMOTE_WAKEUP 1
60#define VUSB_EP_HALT 0
61/** @} */
62
63/** Maximum number of endpoint addresses */
64#define VUSB_PIPE_MAX 16
65
66/**
67 * Control-pipe stages.
68 */
69typedef enum CTLSTAGE
70{
71 /** the control pipe is in the setup stage. */
72 CTLSTAGE_SETUP = 0,
73 /** the control pipe is in the data stage. */
74 CTLSTAGE_DATA,
75 /** the control pipe is in the status stage. */
76 CTLSTAGE_STATUS
77} CTLSTAGE;
78
79/**
80 * Extra data for a control pipe.
81 *
82 * This is state information needed for the special multi-stage
83 * transfers performed on this kind of pipes.
84 */
85typedef struct vusb_ctrl_extra
86{
87 /** Current pipe stage. */
88 CTLSTAGE enmStage;
89 /** Success indicator. */
90 bool fOk;
91 /** Set if the message URB has been submitted. */
92 bool fSubmitted;
93 /** Pointer to the SETUP.
94 * This is a pointer to Urb->abData[0]. */
95 PVUSBSETUP pMsg;
96 /** Current DATA pointer.
97 * This starts at pMsg + 1 and is incremented at we read/write data. */
98 uint8_t *pbCur;
99 /** The amount of data left to read on IN operations.
100 * On OUT operations this is not used. */
101 uint32_t cbLeft;
102 /** The amount of data we can house.
103 * This starts at the default 8KB, and this structure will be reallocated to
104 * accommodate any larger request (unlikely). */
105 uint32_t cbMax;
106 /** The message URB. */
107 VUSBURB Urb;
108} VUSBCTRLEXTRA, *PVUSBCTRLEXTRA;
109
110void vusbMsgFreeExtraData(PVUSBCTRLEXTRA pExtra);
111void vusbMsgResetExtraData(PVUSBCTRLEXTRA pExtra);
112
113
114/**
115 * A VUSB pipe
116 */
117typedef struct vusb_pipe
118{
119 PCVUSBDESCENDPOINTEX in;
120 PCVUSBDESCENDPOINTEX out;
121 /** Pointer to the extra state data required to run a control pipe. */
122 PVUSBCTRLEXTRA pCtrl;
123 /** Count of active async transfers. */
124 uint8_t async;
125 /** The periodic read-ahead buffer thread. */
126 RTTHREAD ReadAheadThread;
127 /** Pointer to the reset thread arguments. */
128 void *pvReadAheadArgs;
129 /** Pointer to the first buffered URB. */
130 PVUSBURB pBuffUrbHead;
131 /** Pointer to the last buffered URB. */
132 PVUSBURB pBuffUrbTail;
133 /** Count of URBs in read-ahead buffer. */
134 uint32_t cBuffered;
135 /** Count of URBs submitted for read-ahead but not yet reaped. */
136 uint32_t cSubmitted;
137} VUSBPIPE;
138/** Pointer to a VUSB pipe structure. */
139typedef VUSBPIPE *PVUSBPIPE;
140
141
142/**
143 * Interface state and possible settings.
144 */
145typedef struct vusb_interface_state
146{
147 /** Pointer to the interface descriptor of the currently selected (active)
148 * interface. */
149 PCVUSBDESCINTERFACEEX pCurIfDesc;
150 /** Pointer to the interface settings. */
151 PCVUSBINTERFACE pIf;
152} VUSBINTERFACESTATE;
153/** Pointer to interface state. */
154typedef VUSBINTERFACESTATE *PVUSBINTERFACESTATE;
155/** Pointer to const interface state. */
156typedef const VUSBINTERFACESTATE *PCVUSBINTERFACESTATE;
157
158
159/**
160 * A Virtual USB device (core).
161 *
162 * @implements VUSBIDEVICE
163 */
164typedef struct VUSBDEV
165{
166 /** The device interface exposed to the HCI. */
167 VUSBIDEVICE IDevice;
168 /** Pointer to the PDM USB device instance. */
169 PPDMUSBINS pUsbIns;
170 /** Next device in the chain maintained by the roothub. */
171 PVUSBDEV pNext;
172 /** Pointer to the next device with the same address hash. */
173 PVUSBDEV pNextHash;
174 /** Pointer to the hub this device is attached to. */
175 PVUSBHUB pHub;
176 /** The device state.
177 * Only EMT changes this value. */
178 VUSBDEVICESTATE volatile enmState;
179
180 /** The device address. */
181 uint8_t u8Address;
182 /** The new device address. */
183 uint8_t u8NewAddress;
184 /** The port. */
185 int16_t i16Port;
186 /** Device status. (VUSB_DEV_SELF_POWERED or not.) */
187 uint16_t u16Status;
188
189 /** Pointer to the descriptor cache.
190 * (Provided by the device thru the pfnGetDescriptorCache method.) */
191 PCPDMUSBDESCCACHE pDescCache;
192 /** Current configuration. */
193 PCVUSBDESCCONFIGEX pCurCfgDesc;
194
195 /** Current interface state (including alternate interface setting) - maximum
196 * valid index is config->bNumInterfaces
197 */
198 PVUSBINTERFACESTATE paIfStates;
199
200 /** Pipe/direction -> endpoint descriptor mapping */
201 VUSBPIPE aPipes[VUSB_PIPE_MAX];
202
203 /** Dumper state. */
204 union VUSBDEVURBDUMPERSTATE
205 {
206 /** The current scsi command. */
207 uint8_t u8ScsiCmd;
208 } Urb;
209
210 /** The reset thread. */
211 RTTHREAD hResetThread;
212 /** Pointer to the reset thread arguments. */
213 void *pvResetArgs;
214 /** The reset timer handle. */
215 PTMTIMER pResetTimer;
216} VUSBDEV;
217
218
219
220/** Pointer to the virtual method table for a kind of USB devices. */
221typedef struct vusb_dev_ops *PVUSBDEVOPS;
222
223/** Pointer to the const virtual method table for a kind of USB devices. */
224typedef const struct vusb_dev_ops *PCVUSBDEVOPS;
225
226/**
227 * Virtual method table for USB devices - these are the functions you need to
228 * implement when writing a new device (or hub)
229 *
230 * Note that when creating your structure, you are required to zero the
231 * vusb_dev fields (ie. use calloc).
232 */
233typedef struct vusb_dev_ops
234{
235 /* mandatory */
236 const char *name;
237} VUSBDEVOPS;
238
239
240int vusbDevInit(PVUSBDEV pDev, PPDMUSBINS pUsbIns);
241int vusbDevCreateOld(const char *pszDeviceName, void *pvDriverInit, PCRTUUID pUuid, PVUSBDEV *ppDev);
242void vusbDevDestroy(PVUSBDEV pDev);
243
244DECLINLINE(bool) vusbDevIsRh(PVUSBDEV pDev)
245{
246 return (pDev->pHub == (PVUSBHUB)pDev);
247}
248
249bool vusbDevDoSelectConfig(PVUSBDEV dev, PCVUSBDESCCONFIGEX pCfg);
250void vusbDevMapEndpoint(PVUSBDEV dev, PCVUSBDESCENDPOINTEX ep);
251int vusbDevDetach(PVUSBDEV pDev);
252DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev);
253size_t vusbDevMaxInterfaces(PVUSBDEV dev);
254
255DECLCALLBACK(int) vusbDevReset(PVUSBIDEVICE pDevice, bool fResetOnLinux, PFNVUSBRESETDONE pfnDone, void *pvUser, PVM pVM);
256DECLCALLBACK(int) vusbDevPowerOn(PVUSBIDEVICE pInterface);
257DECLCALLBACK(int) vusbDevPowerOff(PVUSBIDEVICE pInterface);
258DECLCALLBACK(VUSBDEVICESTATE) vusbDevGetState(PVUSBIDEVICE pInterface);
259void vusbDevSetAddress(PVUSBDEV pDev, uint8_t u8Address);
260bool vusbDevStandardRequest(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, void *pvBuf, uint32_t *pcbBuf);
261
262
263/** @} */
264
265
266
267
268
269/** @name Internal Hub Operations, Structures and Constants.
270 * @{
271 */
272
273
274/** Virtual method table for USB hub devices.
275 * Hub and roothub drivers need to implement these functions in addition to the
276 * vusb_dev_ops.
277 */
278typedef struct VUSBHUBOPS
279{
280 int (*pfnAttach)(PVUSBHUB pHub, PVUSBDEV pDev);
281 void (*pfnDetach)(PVUSBHUB pHub, PVUSBDEV pDev);
282} VUSBHUBOPS;
283/** Pointer to a const HUB method table. */
284typedef const VUSBHUBOPS *PCVUSBHUBOPS;
285
286/** A VUSB Hub Device - Hub and roothub drivers need to use this struct
287 * @todo eliminate this (PDM / roothubs only).
288 */
289typedef struct VUSBHUB
290{
291 VUSBDEV Dev;
292 PCVUSBHUBOPS pOps;
293 PVUSBROOTHUB pRootHub;
294 uint16_t cPorts;
295 uint16_t cDevices;
296 /** Name of the hub. Used for logging. */
297 char *pszName;
298} VUSBHUB;
299
300/** @} */
301
302
303/** @name Internal Root Hub Operations, Structures and Constants.
304 * @{
305 */
306
307/**
308 * Per transfer type statistics.
309 */
310typedef struct VUSBROOTHUBTYPESTATS
311{
312 STAMCOUNTER StatUrbsSubmitted;
313 STAMCOUNTER StatUrbsFailed;
314 STAMCOUNTER StatUrbsCancelled;
315
316 STAMCOUNTER StatReqBytes;
317 STAMCOUNTER StatReqReadBytes;
318 STAMCOUNTER StatReqWriteBytes;
319
320 STAMCOUNTER StatActBytes;
321 STAMCOUNTER StatActReadBytes;
322 STAMCOUNTER StatActWriteBytes;
323} VUSBROOTHUBTYPESTATS, *PVUSBROOTHUBTYPESTATS;
324
325
326
327/** The address hash table size. */
328#define VUSB_ADDR_HASHSZ 5
329
330/**
331 * The instance data of a root hub driver.
332 *
333 * This extends the generic VUSB hub.
334 *
335 * @implements VUSBIROOTHUBCONNECTOR
336 */
337typedef struct VUSBROOTHUB
338{
339 /** The HUB.
340 * @todo remove this? */
341 VUSBHUB Hub;
342#if HC_ARCH_BITS == 32
343 uint32_t Alignment0;
344#endif
345 /** Address hash table. */
346 PVUSBDEV apAddrHash[VUSB_ADDR_HASHSZ];
347 /** List of async URBs. */
348 PVUSBURB pAsyncUrbHead;
349 /** The default address. */
350 PVUSBDEV pDefaultAddress;
351
352 /** Pointer to the driver instance. */
353 PPDMDRVINS pDrvIns;
354 /** Pointer to the root hub port interface we're attached to. */
355 PVUSBIROOTHUBPORT pIRhPort;
356 /** Connector interface exposed upwards. */
357 VUSBIROOTHUBCONNECTOR IRhConnector;
358
359 /** Chain of devices attached to this hub. */
360 PVUSBDEV pDevices;
361 /** Availability Bitmap. */
362 VUSBPORTBITMAP Bitmap;
363
364 /** Critical section protecting the free list. */
365 RTCRITSECT CritSect;
366 /** Chain of free URBs. (Singly linked) */
367 PVUSBURB pFreeUrbs;
368 /** The number of URBs in the pool. */
369 uint32_t cUrbsInPool;
370 /** Version of the attached Host Controller. */
371 uint32_t fHcVersions;
372#ifdef VBOX_WITH_STATISTICS
373#if HC_ARCH_BITS == 32
374 uint32_t Alignment1; /**< Counters must be 64-bit aligned. */
375#endif
376 VUSBROOTHUBTYPESTATS Total;
377 VUSBROOTHUBTYPESTATS aTypes[VUSBXFERTYPE_MSG];
378 STAMCOUNTER StatIsocReqPkts;
379 STAMCOUNTER StatIsocReqReadPkts;
380 STAMCOUNTER StatIsocReqWritePkts;
381 STAMCOUNTER StatIsocActPkts;
382 STAMCOUNTER StatIsocActReadPkts;
383 STAMCOUNTER StatIsocActWritePkts;
384 struct
385 {
386 STAMCOUNTER Pkts;
387 STAMCOUNTER Ok;
388 STAMCOUNTER Ok0;
389 STAMCOUNTER DataUnderrun;
390 STAMCOUNTER DataUnderrun0;
391 STAMCOUNTER DataOverrun;
392 STAMCOUNTER NotAccessed;
393 STAMCOUNTER Misc;
394 STAMCOUNTER Bytes;
395 } aStatIsocDetails[8];
396
397 STAMPROFILE StatReapAsyncUrbs;
398 STAMPROFILE StatSubmitUrb;
399#endif
400} VUSBROOTHUB;
401AssertCompileMemberAlignment(VUSBROOTHUB, IRhConnector, 8);
402AssertCompileMemberAlignment(VUSBROOTHUB, Bitmap, 8);
403AssertCompileMemberAlignment(VUSBROOTHUB, CritSect, 8);
404#ifdef VBOX_WITH_STATISTICS
405AssertCompileMemberAlignment(VUSBROOTHUB, Total, 8);
406#endif
407
408/** Converts a pointer to VUSBROOTHUB::IRhConnector to a PVUSBROOTHUB. */
409#define VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface) (PVUSBROOTHUB)( (uintptr_t)(pInterface) - RT_OFFSETOF(VUSBROOTHUB, IRhConnector) )
410
411/**
412 * URB cancellation modes
413 */
414typedef enum CANCELMODE
415{
416 /** complete the URB with an error (CRC). */
417 CANCELMODE_FAIL = 0,
418 /** do not change the URB contents. */
419 CANCELMODE_UNDO
420} CANCELMODE;
421
422/* @} */
423
424
425
426/** @name Internal URB Operations, Structures and Constants.
427 * @{ */
428int vusbUrbSubmit(PVUSBURB pUrb);
429void vusbUrbTrace(PVUSBURB pUrb, const char *pszMsg, bool fComplete);
430void vusbUrbDoReapAsync(PVUSBURB pHead, RTMSINTERVAL cMillies);
431void vusbUrbCancel(PVUSBURB pUrb, CANCELMODE mode);
432void vusbUrbRipe(PVUSBURB pUrb);
433void vusbUrbCompletionRh(PVUSBURB pUrb);
434
435void vusbUrbCompletionReadAhead(PVUSBURB pUrb);
436void vusbReadAheadStart(PVUSBDEV pDev, PVUSBPIPE pPipe);
437void vusbReadAheadStop(void *pvReadAheadArgs);
438int vusbUrbQueueAsyncRh(PVUSBURB pUrb);
439int vusbUrbSubmitBufferedRead(PVUSBURB pUrb, PVUSBPIPE pPipe);
440PVUSBURB vusbRhNewUrb(PVUSBROOTHUB pRh, uint8_t DstAddress, uint32_t cbData, uint32_t cTds);
441
442
443DECLINLINE(void) vusbUrbUnlink(PVUSBURB pUrb)
444{
445 *pUrb->VUsb.ppPrev = pUrb->VUsb.pNext;
446 if (pUrb->VUsb.pNext)
447 pUrb->VUsb.pNext->VUsb.ppPrev = pUrb->VUsb.ppPrev;
448 pUrb->VUsb.pNext = NULL;
449 pUrb->VUsb.ppPrev = NULL;
450}
451
452/** @def vusbUrbAssert
453 * Asserts that a URB is valid.
454 */
455#ifdef VBOX_STRICT
456# define vusbUrbAssert(pUrb) do { \
457 AssertMsg(VALID_PTR((pUrb)), ("%p\n", (pUrb))); \
458 AssertMsg((pUrb)->u32Magic == VUSBURB_MAGIC, ("%#x", (pUrb)->u32Magic)); \
459 AssertMsg((pUrb)->enmState > VUSBURBSTATE_INVALID && (pUrb)->enmState < VUSBURBSTATE_END, \
460 ("%d\n", (pUrb)->enmState)); \
461 } while (0)
462#else
463# define vusbUrbAssert(pUrb) do {} while (0)
464#endif
465
466/** @} */
467
468
469
470
471/**
472 * Addresses are between 0 and 127 inclusive
473 */
474DECLINLINE(uint8_t) vusbHashAddress(uint8_t Address)
475{
476 uint8_t u8Hash = Address;
477 u8Hash ^= (Address >> 2);
478 u8Hash ^= (Address >> 3);
479 u8Hash %= VUSB_ADDR_HASHSZ;
480 return u8Hash;
481}
482
483
484/**
485 * Gets the roothub of a device.
486 *
487 * @returns Pointer to the roothub instance the device is attached to.
488 * @returns NULL if not attached to any hub.
489 * @param pDev Pointer to the device in question.
490 */
491DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev)
492{
493 if (!pDev->pHub)
494 return NULL;
495 return pDev->pHub->pRootHub;
496}
497
498
499
500/** Strings for the CTLSTAGE enum values. */
501extern const char * const g_apszCtlStates[4];
502/** Default message pipe. */
503extern const VUSBDESCENDPOINTEX g_Endpoint0;
504/** Default configuration. */
505extern const VUSBDESCCONFIGEX g_Config0;
506
507RT_C_DECLS_END
508#endif
509
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