VirtualBox

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

Last change on this file since 51991 was 49815, checked in by vboxsync, 11 years ago

Try to fix burn

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