VirtualBox

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

Last change on this file since 35753 was 35346, checked in by vboxsync, 14 years ago

VMM reorg: Moving the public include files from include/VBox to include/VBox/vmm.

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