VirtualBox

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

Last change on this file since 49469 was 49097, checked in by vboxsync, 11 years ago

Alignment

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/* $Id: VUSBInternal.h 49097 2013-10-14 22:38:09Z 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#if HC_ARCH_BITS == 32
217 /** Align the size to a 8 byte boundary. */
218 uint32_t Alignment0;
219#endif
220} VUSBDEV;
221
222
223
224/** Pointer to the virtual method table for a kind of USB devices. */
225typedef struct vusb_dev_ops *PVUSBDEVOPS;
226
227/** Pointer to the const virtual method table for a kind of USB devices. */
228typedef const struct vusb_dev_ops *PCVUSBDEVOPS;
229
230/**
231 * Virtual method table for USB devices - these are the functions you need to
232 * implement when writing a new device (or hub)
233 *
234 * Note that when creating your structure, you are required to zero the
235 * vusb_dev fields (ie. use calloc).
236 */
237typedef struct vusb_dev_ops
238{
239 /* mandatory */
240 const char *name;
241} VUSBDEVOPS;
242
243
244int vusbDevInit(PVUSBDEV pDev, PPDMUSBINS pUsbIns);
245int vusbDevCreateOld(const char *pszDeviceName, void *pvDriverInit, PCRTUUID pUuid, PVUSBDEV *ppDev);
246void vusbDevDestroy(PVUSBDEV pDev);
247
248DECLINLINE(bool) vusbDevIsRh(PVUSBDEV pDev)
249{
250 return (pDev->pHub == (PVUSBHUB)pDev);
251}
252
253bool vusbDevDoSelectConfig(PVUSBDEV dev, PCVUSBDESCCONFIGEX pCfg);
254void vusbDevMapEndpoint(PVUSBDEV dev, PCVUSBDESCENDPOINTEX ep);
255int vusbDevDetach(PVUSBDEV pDev);
256DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev);
257size_t vusbDevMaxInterfaces(PVUSBDEV dev);
258
259DECLCALLBACK(int) vusbDevReset(PVUSBIDEVICE pDevice, bool fResetOnLinux, PFNVUSBRESETDONE pfnDone, void *pvUser, PVM pVM);
260DECLCALLBACK(int) vusbDevPowerOn(PVUSBIDEVICE pInterface);
261DECLCALLBACK(int) vusbDevPowerOff(PVUSBIDEVICE pInterface);
262DECLCALLBACK(VUSBDEVICESTATE) vusbDevGetState(PVUSBIDEVICE pInterface);
263void vusbDevSetAddress(PVUSBDEV pDev, uint8_t u8Address);
264bool vusbDevStandardRequest(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, void *pvBuf, uint32_t *pcbBuf);
265
266
267/** @} */
268
269
270
271
272
273/** @name Internal Hub Operations, Structures and Constants.
274 * @{
275 */
276
277
278/** Virtual method table for USB hub devices.
279 * Hub and roothub drivers need to implement these functions in addition to the
280 * vusb_dev_ops.
281 */
282typedef struct VUSBHUBOPS
283{
284 int (*pfnAttach)(PVUSBHUB pHub, PVUSBDEV pDev);
285 void (*pfnDetach)(PVUSBHUB pHub, PVUSBDEV pDev);
286} VUSBHUBOPS;
287/** Pointer to a const HUB method table. */
288typedef const VUSBHUBOPS *PCVUSBHUBOPS;
289
290/** A VUSB Hub Device - Hub and roothub drivers need to use this struct
291 * @todo eliminate this (PDM / roothubs only).
292 */
293typedef struct VUSBHUB
294{
295 VUSBDEV Dev;
296 PCVUSBHUBOPS pOps;
297 PVUSBROOTHUB pRootHub;
298 uint16_t cPorts;
299 uint16_t cDevices;
300 /** Name of the hub. Used for logging. */
301 char *pszName;
302} VUSBHUB;
303AssertCompileMemberAlignment(VUSBHUB, pOps, 8);
304
305/** @} */
306
307
308/** @name Internal Root Hub Operations, Structures and Constants.
309 * @{
310 */
311
312/**
313 * Per transfer type statistics.
314 */
315typedef struct VUSBROOTHUBTYPESTATS
316{
317 STAMCOUNTER StatUrbsSubmitted;
318 STAMCOUNTER StatUrbsFailed;
319 STAMCOUNTER StatUrbsCancelled;
320
321 STAMCOUNTER StatReqBytes;
322 STAMCOUNTER StatReqReadBytes;
323 STAMCOUNTER StatReqWriteBytes;
324
325 STAMCOUNTER StatActBytes;
326 STAMCOUNTER StatActReadBytes;
327 STAMCOUNTER StatActWriteBytes;
328} VUSBROOTHUBTYPESTATS, *PVUSBROOTHUBTYPESTATS;
329
330
331
332/** The address hash table size. */
333#define VUSB_ADDR_HASHSZ 5
334
335/**
336 * The instance data of a root hub driver.
337 *
338 * This extends the generic VUSB hub.
339 *
340 * @implements VUSBIROOTHUBCONNECTOR
341 */
342typedef struct VUSBROOTHUB
343{
344 /** The HUB.
345 * @todo remove this? */
346 VUSBHUB Hub;
347#if HC_ARCH_BITS == 32
348 uint32_t Alignment0;
349#endif
350 /** Address hash table. */
351 PVUSBDEV apAddrHash[VUSB_ADDR_HASHSZ];
352 /** List of async URBs. */
353 PVUSBURB pAsyncUrbHead;
354 /** The default address. */
355 PVUSBDEV pDefaultAddress;
356
357 /** Pointer to the driver instance. */
358 PPDMDRVINS pDrvIns;
359 /** Pointer to the root hub port interface we're attached to. */
360 PVUSBIROOTHUBPORT pIRhPort;
361 /** Connector interface exposed upwards. */
362 VUSBIROOTHUBCONNECTOR IRhConnector;
363
364 /** Chain of devices attached to this hub. */
365 PVUSBDEV pDevices;
366 /** Availability Bitmap. */
367 VUSBPORTBITMAP Bitmap;
368
369 /** Critical section protecting the free list. */
370 RTCRITSECT CritSect;
371 /** Chain of free URBs. (Singly linked) */
372 PVUSBURB pFreeUrbs;
373 /** The number of URBs in the pool. */
374 uint32_t cUrbsInPool;
375 /** Version of the attached Host Controller. */
376 uint32_t fHcVersions;
377#ifdef VBOX_WITH_STATISTICS
378#if HC_ARCH_BITS == 32
379 uint32_t Alignment1; /**< Counters must be 64-bit aligned. */
380#endif
381 VUSBROOTHUBTYPESTATS Total;
382 VUSBROOTHUBTYPESTATS aTypes[VUSBXFERTYPE_MSG];
383 STAMCOUNTER StatIsocReqPkts;
384 STAMCOUNTER StatIsocReqReadPkts;
385 STAMCOUNTER StatIsocReqWritePkts;
386 STAMCOUNTER StatIsocActPkts;
387 STAMCOUNTER StatIsocActReadPkts;
388 STAMCOUNTER StatIsocActWritePkts;
389 struct
390 {
391 STAMCOUNTER Pkts;
392 STAMCOUNTER Ok;
393 STAMCOUNTER Ok0;
394 STAMCOUNTER DataUnderrun;
395 STAMCOUNTER DataUnderrun0;
396 STAMCOUNTER DataOverrun;
397 STAMCOUNTER NotAccessed;
398 STAMCOUNTER Misc;
399 STAMCOUNTER Bytes;
400 } aStatIsocDetails[8];
401
402 STAMPROFILE StatReapAsyncUrbs;
403 STAMPROFILE StatSubmitUrb;
404#endif
405} VUSBROOTHUB;
406AssertCompileMemberAlignment(VUSBROOTHUB, IRhConnector, 8);
407AssertCompileMemberAlignment(VUSBROOTHUB, Bitmap, 8);
408AssertCompileMemberAlignment(VUSBROOTHUB, CritSect, 8);
409#ifdef VBOX_WITH_STATISTICS
410AssertCompileMemberAlignment(VUSBROOTHUB, Total, 8);
411#endif
412
413/** Converts a pointer to VUSBROOTHUB::IRhConnector to a PVUSBROOTHUB. */
414#define VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface) (PVUSBROOTHUB)( (uintptr_t)(pInterface) - RT_OFFSETOF(VUSBROOTHUB, IRhConnector) )
415
416/**
417 * URB cancellation modes
418 */
419typedef enum CANCELMODE
420{
421 /** complete the URB with an error (CRC). */
422 CANCELMODE_FAIL = 0,
423 /** do not change the URB contents. */
424 CANCELMODE_UNDO
425} CANCELMODE;
426
427/* @} */
428
429
430
431/** @name Internal URB Operations, Structures and Constants.
432 * @{ */
433int vusbUrbSubmit(PVUSBURB pUrb);
434void vusbUrbTrace(PVUSBURB pUrb, const char *pszMsg, bool fComplete);
435void vusbUrbDoReapAsync(PVUSBURB pHead, RTMSINTERVAL cMillies);
436void vusbUrbCancel(PVUSBURB pUrb, CANCELMODE mode);
437void vusbUrbRipe(PVUSBURB pUrb);
438void vusbUrbCompletionRh(PVUSBURB pUrb);
439
440void vusbUrbCompletionReadAhead(PVUSBURB pUrb);
441void vusbReadAheadStart(PVUSBDEV pDev, PVUSBPIPE pPipe);
442void vusbReadAheadStop(void *pvReadAheadArgs);
443int vusbUrbQueueAsyncRh(PVUSBURB pUrb);
444int vusbUrbSubmitBufferedRead(PVUSBURB pUrb, PVUSBPIPE pPipe);
445PVUSBURB vusbRhNewUrb(PVUSBROOTHUB pRh, uint8_t DstAddress, uint32_t cbData, uint32_t cTds);
446
447
448DECLINLINE(void) vusbUrbUnlink(PVUSBURB pUrb)
449{
450 *pUrb->VUsb.ppPrev = pUrb->VUsb.pNext;
451 if (pUrb->VUsb.pNext)
452 pUrb->VUsb.pNext->VUsb.ppPrev = pUrb->VUsb.ppPrev;
453 pUrb->VUsb.pNext = NULL;
454 pUrb->VUsb.ppPrev = NULL;
455}
456
457/** @def vusbUrbAssert
458 * Asserts that a URB is valid.
459 */
460#ifdef VBOX_STRICT
461# define vusbUrbAssert(pUrb) do { \
462 AssertMsg(VALID_PTR((pUrb)), ("%p\n", (pUrb))); \
463 AssertMsg((pUrb)->u32Magic == VUSBURB_MAGIC, ("%#x", (pUrb)->u32Magic)); \
464 AssertMsg((pUrb)->enmState > VUSBURBSTATE_INVALID && (pUrb)->enmState < VUSBURBSTATE_END, \
465 ("%d\n", (pUrb)->enmState)); \
466 } while (0)
467#else
468# define vusbUrbAssert(pUrb) do {} while (0)
469#endif
470
471/** @} */
472
473
474
475
476/**
477 * Addresses are between 0 and 127 inclusive
478 */
479DECLINLINE(uint8_t) vusbHashAddress(uint8_t Address)
480{
481 uint8_t u8Hash = Address;
482 u8Hash ^= (Address >> 2);
483 u8Hash ^= (Address >> 3);
484 u8Hash %= VUSB_ADDR_HASHSZ;
485 return u8Hash;
486}
487
488
489/**
490 * Gets the roothub of a device.
491 *
492 * @returns Pointer to the roothub instance the device is attached to.
493 * @returns NULL if not attached to any hub.
494 * @param pDev Pointer to the device in question.
495 */
496DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev)
497{
498 if (!pDev->pHub)
499 return NULL;
500 return pDev->pHub->pRootHub;
501}
502
503
504
505/** Strings for the CTLSTAGE enum values. */
506extern const char * const g_apszCtlStates[4];
507/** Default message pipe. */
508extern const VUSBDESCENDPOINTEX g_Endpoint0;
509/** Default configuration. */
510extern const VUSBDESCCONFIGEX g_Config0;
511
512RT_C_DECLS_END
513#endif
514
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