1 | /* $Id: VUSBInternal.h 59724 2016-02-18 13:21:07Z 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-2015 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 <VBox/vmm/pdmusb.h>
|
---|
31 | #include <iprt/asm.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/req.h>
|
---|
34 | #include <iprt/list.h>
|
---|
35 |
|
---|
36 | #include "VUSBSniffer.h"
|
---|
37 |
|
---|
38 | RT_C_DECLS_BEGIN
|
---|
39 |
|
---|
40 |
|
---|
41 | /** @name Internal Device Operations, Structures and Constants.
|
---|
42 | * @{
|
---|
43 | */
|
---|
44 |
|
---|
45 | /** Pointer to a Virtual USB device (core). */
|
---|
46 | typedef struct VUSBDEV *PVUSBDEV;
|
---|
47 | /** Pointer to a VUSB hub device. */
|
---|
48 | typedef struct VUSBHUB *PVUSBHUB;
|
---|
49 | /** Pointer to a VUSB root hub. */
|
---|
50 | typedef struct VUSBROOTHUB *PVUSBROOTHUB;
|
---|
51 |
|
---|
52 |
|
---|
53 | /** Number of the default control endpoint */
|
---|
54 | #define VUSB_PIPE_DEFAULT 0
|
---|
55 |
|
---|
56 | /** @name Device addresses
|
---|
57 | * @{ */
|
---|
58 | #define VUSB_DEFAULT_ADDRESS 0
|
---|
59 | #define VUSB_INVALID_ADDRESS UINT8_C(0xff)
|
---|
60 | /** @} */
|
---|
61 |
|
---|
62 | /** @name Feature bits (1<<FEATURE for the u16Status bit)
|
---|
63 | * @{ */
|
---|
64 | #define VUSB_DEV_SELF_POWERED 0
|
---|
65 | #define VUSB_DEV_REMOTE_WAKEUP 1
|
---|
66 | #define VUSB_EP_HALT 0
|
---|
67 | /** @} */
|
---|
68 |
|
---|
69 | /** Maximum number of endpoint addresses */
|
---|
70 | #define VUSB_PIPE_MAX 16
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * The VUSB URB data.
|
---|
74 | */
|
---|
75 | typedef struct VUSBURBVUSBINT
|
---|
76 | {
|
---|
77 | /** URB chain pointer. */
|
---|
78 | PVUSBURB pNext;
|
---|
79 | /** URB chain pointer. */
|
---|
80 | PVUSBURB *ppPrev;
|
---|
81 | /** Pointer to the original for control messages. */
|
---|
82 | PVUSBURB pCtrlUrb;
|
---|
83 | /** Pointer to the VUSB device.
|
---|
84 | * This may be NULL if the destination address is invalid. */
|
---|
85 | PVUSBDEV pDev;
|
---|
86 | /** Specific to the pfnFree function. */
|
---|
87 | void *pvFreeCtx;
|
---|
88 | /**
|
---|
89 | * Callback which will free the URB once it's reaped and completed.
|
---|
90 | * @param pUrb The URB.
|
---|
91 | */
|
---|
92 | DECLCALLBACKMEMBER(void, pfnFree)(PVUSBURB pUrb);
|
---|
93 | /** Submit timestamp. (logging only) */
|
---|
94 | uint64_t u64SubmitTS;
|
---|
95 | /** Opaque data holder when this is a read-ahead URB. */
|
---|
96 | void *pvReadAhead;
|
---|
97 | } VUSBURBVUSBINT;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Control-pipe stages.
|
---|
101 | */
|
---|
102 | typedef enum CTLSTAGE
|
---|
103 | {
|
---|
104 | /** the control pipe is in the setup stage. */
|
---|
105 | CTLSTAGE_SETUP = 0,
|
---|
106 | /** the control pipe is in the data stage. */
|
---|
107 | CTLSTAGE_DATA,
|
---|
108 | /** the control pipe is in the status stage. */
|
---|
109 | CTLSTAGE_STATUS
|
---|
110 | } CTLSTAGE;
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Extra data for a control pipe.
|
---|
114 | *
|
---|
115 | * This is state information needed for the special multi-stage
|
---|
116 | * transfers performed on this kind of pipes.
|
---|
117 | */
|
---|
118 | typedef struct vusb_ctrl_extra
|
---|
119 | {
|
---|
120 | /** Current pipe stage. */
|
---|
121 | CTLSTAGE enmStage;
|
---|
122 | /** Success indicator. */
|
---|
123 | bool fOk;
|
---|
124 | /** Set if the message URB has been submitted. */
|
---|
125 | bool fSubmitted;
|
---|
126 | /** Pointer to the SETUP.
|
---|
127 | * This is a pointer to Urb->abData[0]. */
|
---|
128 | PVUSBSETUP pMsg;
|
---|
129 | /** Current DATA pointer.
|
---|
130 | * This starts at pMsg + 1 and is incremented at we read/write data. */
|
---|
131 | uint8_t *pbCur;
|
---|
132 | /** The amount of data left to read on IN operations.
|
---|
133 | * On OUT operations this is not used. */
|
---|
134 | uint32_t cbLeft;
|
---|
135 | /** The amount of data we can house.
|
---|
136 | * This starts at the default 8KB, and this structure will be reallocated to
|
---|
137 | * accommodate any larger request (unlikely). */
|
---|
138 | uint32_t cbMax;
|
---|
139 | /** The message URB. */
|
---|
140 | VUSBURB Urb;
|
---|
141 | } VUSBCTRLEXTRA, *PVUSBCTRLEXTRA;
|
---|
142 |
|
---|
143 | void vusbMsgFreeExtraData(PVUSBCTRLEXTRA pExtra);
|
---|
144 | void vusbMsgResetExtraData(PVUSBCTRLEXTRA pExtra);
|
---|
145 |
|
---|
146 | /** Opaque VUSB read ahead buffer management handle. */
|
---|
147 | typedef struct VUSBREADAHEADINT *VUSBREADAHEAD;
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * A VUSB pipe
|
---|
151 | */
|
---|
152 | typedef struct vusb_pipe
|
---|
153 | {
|
---|
154 | PCVUSBDESCENDPOINTEX in;
|
---|
155 | PCVUSBDESCENDPOINTEX out;
|
---|
156 | /** Pointer to the extra state data required to run a control pipe. */
|
---|
157 | PVUSBCTRLEXTRA pCtrl;
|
---|
158 | /** Critical section serializing access to the extra state data for a control pipe. */
|
---|
159 | RTCRITSECT CritSectCtrl;
|
---|
160 | /** Count of active async transfers. */
|
---|
161 | volatile uint32_t async;
|
---|
162 | /** Read ahead handle. */
|
---|
163 | VUSBREADAHEAD hReadAhead;
|
---|
164 | } VUSBPIPE;
|
---|
165 | /** Pointer to a VUSB pipe structure. */
|
---|
166 | typedef VUSBPIPE *PVUSBPIPE;
|
---|
167 |
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Interface state and possible settings.
|
---|
171 | */
|
---|
172 | typedef struct vusb_interface_state
|
---|
173 | {
|
---|
174 | /** Pointer to the interface descriptor of the currently selected (active)
|
---|
175 | * interface. */
|
---|
176 | PCVUSBDESCINTERFACEEX pCurIfDesc;
|
---|
177 | /** Pointer to the interface settings. */
|
---|
178 | PCVUSBINTERFACE pIf;
|
---|
179 | } VUSBINTERFACESTATE;
|
---|
180 | /** Pointer to interface state. */
|
---|
181 | typedef VUSBINTERFACESTATE *PVUSBINTERFACESTATE;
|
---|
182 | /** Pointer to const interface state. */
|
---|
183 | typedef const VUSBINTERFACESTATE *PCVUSBINTERFACESTATE;
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * VUSB URB pool.
|
---|
188 | */
|
---|
189 | typedef struct VUSBURBPOOL
|
---|
190 | {
|
---|
191 | /** Critical section protecting the pool. */
|
---|
192 | RTCRITSECT CritSectPool;
|
---|
193 | /** Chain of free URBs by type. (Singly linked) */
|
---|
194 | RTLISTANCHOR aLstFreeUrbs[VUSBXFERTYPE_ELEMENTS];
|
---|
195 | /** The number of URBs in the pool. */
|
---|
196 | volatile uint32_t cUrbsInPool;
|
---|
197 | /** Align the size to a 8 byte boundary. */
|
---|
198 | uint32_t Alignment0;
|
---|
199 | } VUSBURBPOOL;
|
---|
200 | /** Pointer to a VUSB URB pool. */
|
---|
201 | typedef VUSBURBPOOL *PVUSBURBPOOL;
|
---|
202 |
|
---|
203 | AssertCompileSizeAlignment(VUSBURBPOOL, 8);
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * A Virtual USB device (core).
|
---|
207 | *
|
---|
208 | * @implements VUSBIDEVICE
|
---|
209 | */
|
---|
210 | typedef struct VUSBDEV
|
---|
211 | {
|
---|
212 | /** The device interface exposed to the HCI. */
|
---|
213 | VUSBIDEVICE IDevice;
|
---|
214 | /** Pointer to the PDM USB device instance. */
|
---|
215 | PPDMUSBINS pUsbIns;
|
---|
216 | /** Next device in the chain maintained by the roothub. */
|
---|
217 | PVUSBDEV pNext;
|
---|
218 | /** Pointer to the next device with the same address hash. */
|
---|
219 | PVUSBDEV pNextHash;
|
---|
220 | /** Pointer to the hub this device is attached to. */
|
---|
221 | PVUSBHUB pHub;
|
---|
222 | /** The device state. */
|
---|
223 | VUSBDEVICESTATE volatile enmState;
|
---|
224 |
|
---|
225 | /** The device address. */
|
---|
226 | uint8_t u8Address;
|
---|
227 | /** The new device address. */
|
---|
228 | uint8_t u8NewAddress;
|
---|
229 | /** The port. */
|
---|
230 | int16_t i16Port;
|
---|
231 | /** Device status. (VUSB_DEV_SELF_POWERED or not.) */
|
---|
232 | uint16_t u16Status;
|
---|
233 |
|
---|
234 | /** Pointer to the descriptor cache.
|
---|
235 | * (Provided by the device thru the pfnGetDescriptorCache method.) */
|
---|
236 | PCPDMUSBDESCCACHE pDescCache;
|
---|
237 | /** Current configuration. */
|
---|
238 | PCVUSBDESCCONFIGEX pCurCfgDesc;
|
---|
239 |
|
---|
240 | /** Current interface state (including alternate interface setting) - maximum
|
---|
241 | * valid index is config->bNumInterfaces
|
---|
242 | */
|
---|
243 | PVUSBINTERFACESTATE paIfStates;
|
---|
244 |
|
---|
245 | /** Pipe/direction -> endpoint descriptor mapping */
|
---|
246 | VUSBPIPE aPipes[VUSB_PIPE_MAX];
|
---|
247 | /** Critical section protecting the active URB list. */
|
---|
248 | RTCRITSECT CritSectAsyncUrbs;
|
---|
249 | /** List of active async URBs. */
|
---|
250 | PVUSBURB pAsyncUrbHead;
|
---|
251 |
|
---|
252 | /** Dumper state. */
|
---|
253 | union VUSBDEVURBDUMPERSTATE
|
---|
254 | {
|
---|
255 | /** The current scsi command. */
|
---|
256 | uint8_t u8ScsiCmd;
|
---|
257 | } Urb;
|
---|
258 |
|
---|
259 | /** The reset timer handle. */
|
---|
260 | PTMTIMER pResetTimer;
|
---|
261 | /** Reset handler arguments. */
|
---|
262 | void *pvArgs;
|
---|
263 | /** URB submit and reap thread. */
|
---|
264 | RTTHREAD hUrbIoThread;
|
---|
265 | /** Request queue for executing tasks on the I/O thread which should be done
|
---|
266 | * synchronous and without any other thread accessing the USB device. */
|
---|
267 | RTREQQUEUE hReqQueueSync;
|
---|
268 | /** Sniffer instance for this device if configured. */
|
---|
269 | VUSBSNIFFER hSniffer;
|
---|
270 | /** Flag whether the URB I/O thread should terminate. */
|
---|
271 | bool volatile fTerminate;
|
---|
272 | /** Flag whether the I/O thread was woken up. */
|
---|
273 | bool volatile fWokenUp;
|
---|
274 | #if HC_ARCH_BITS == 32
|
---|
275 | /** Align the size to a 8 byte boundary. */
|
---|
276 | bool afAlignment0[2];
|
---|
277 | #endif
|
---|
278 | /** The pool of free URBs for faster allocation. */
|
---|
279 | VUSBURBPOOL UrbPool;
|
---|
280 | } VUSBDEV;
|
---|
281 | AssertCompileSizeAlignment(VUSBDEV, 8);
|
---|
282 |
|
---|
283 |
|
---|
284 | int vusbDevInit(PVUSBDEV pDev, PPDMUSBINS pUsbIns, const char *pszCaptureFilename);
|
---|
285 | void vusbDevDestroy(PVUSBDEV pDev);
|
---|
286 |
|
---|
287 | DECLINLINE(bool) vusbDevIsRh(PVUSBDEV pDev)
|
---|
288 | {
|
---|
289 | return (pDev->pHub == (PVUSBHUB)pDev);
|
---|
290 | }
|
---|
291 |
|
---|
292 | bool vusbDevDoSelectConfig(PVUSBDEV dev, PCVUSBDESCCONFIGEX pCfg);
|
---|
293 | void vusbDevMapEndpoint(PVUSBDEV dev, PCVUSBDESCENDPOINTEX ep);
|
---|
294 | int vusbDevDetach(PVUSBDEV pDev);
|
---|
295 | DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev);
|
---|
296 | size_t vusbDevMaxInterfaces(PVUSBDEV dev);
|
---|
297 |
|
---|
298 | void vusbDevSetAddress(PVUSBDEV pDev, uint8_t u8Address);
|
---|
299 | bool vusbDevStandardRequest(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, void *pvBuf, uint32_t *pcbBuf);
|
---|
300 |
|
---|
301 |
|
---|
302 | /** @} */
|
---|
303 |
|
---|
304 |
|
---|
305 | /** @name Internal Hub Operations, Structures and Constants.
|
---|
306 | * @{
|
---|
307 | */
|
---|
308 |
|
---|
309 |
|
---|
310 | /** Virtual method table for USB hub devices.
|
---|
311 | * Hub and roothub drivers need to implement these functions in addition to the
|
---|
312 | * vusb_dev_ops.
|
---|
313 | */
|
---|
314 | typedef struct VUSBHUBOPS
|
---|
315 | {
|
---|
316 | int (*pfnAttach)(PVUSBHUB pHub, PVUSBDEV pDev);
|
---|
317 | void (*pfnDetach)(PVUSBHUB pHub, PVUSBDEV pDev);
|
---|
318 | } VUSBHUBOPS;
|
---|
319 | /** Pointer to a const HUB method table. */
|
---|
320 | typedef const VUSBHUBOPS *PCVUSBHUBOPS;
|
---|
321 |
|
---|
322 | /** A VUSB Hub Device - Hub and roothub drivers need to use this struct
|
---|
323 | * @todo eliminate this (PDM / roothubs only).
|
---|
324 | */
|
---|
325 | typedef struct VUSBHUB
|
---|
326 | {
|
---|
327 | VUSBDEV Dev;
|
---|
328 | PCVUSBHUBOPS pOps;
|
---|
329 | PVUSBROOTHUB pRootHub;
|
---|
330 | uint16_t cPorts;
|
---|
331 | uint16_t cDevices;
|
---|
332 | /** Name of the hub. Used for logging. */
|
---|
333 | char *pszName;
|
---|
334 | } VUSBHUB;
|
---|
335 | AssertCompileMemberAlignment(VUSBHUB, pOps, 8);
|
---|
336 | AssertCompileSizeAlignment(VUSBHUB, 8);
|
---|
337 |
|
---|
338 | /** @} */
|
---|
339 |
|
---|
340 |
|
---|
341 | /** @name Internal Root Hub Operations, Structures and Constants.
|
---|
342 | * @{
|
---|
343 | */
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Per transfer type statistics.
|
---|
347 | */
|
---|
348 | typedef struct VUSBROOTHUBTYPESTATS
|
---|
349 | {
|
---|
350 | STAMCOUNTER StatUrbsSubmitted;
|
---|
351 | STAMCOUNTER StatUrbsFailed;
|
---|
352 | STAMCOUNTER StatUrbsCancelled;
|
---|
353 |
|
---|
354 | STAMCOUNTER StatReqBytes;
|
---|
355 | STAMCOUNTER StatReqReadBytes;
|
---|
356 | STAMCOUNTER StatReqWriteBytes;
|
---|
357 |
|
---|
358 | STAMCOUNTER StatActBytes;
|
---|
359 | STAMCOUNTER StatActReadBytes;
|
---|
360 | STAMCOUNTER StatActWriteBytes;
|
---|
361 | } VUSBROOTHUBTYPESTATS, *PVUSBROOTHUBTYPESTATS;
|
---|
362 |
|
---|
363 |
|
---|
364 |
|
---|
365 | /** The address hash table size. */
|
---|
366 | #define VUSB_ADDR_HASHSZ 5
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * The instance data of a root hub driver.
|
---|
370 | *
|
---|
371 | * This extends the generic VUSB hub.
|
---|
372 | *
|
---|
373 | * @implements VUSBIROOTHUBCONNECTOR
|
---|
374 | */
|
---|
375 | typedef struct VUSBROOTHUB
|
---|
376 | {
|
---|
377 | /** The HUB.
|
---|
378 | * @todo remove this? */
|
---|
379 | VUSBHUB Hub;
|
---|
380 | /** Address hash table. */
|
---|
381 | PVUSBDEV apAddrHash[VUSB_ADDR_HASHSZ];
|
---|
382 | /** The default address. */
|
---|
383 | PVUSBDEV pDefaultAddress;
|
---|
384 |
|
---|
385 | /** Pointer to the driver instance. */
|
---|
386 | PPDMDRVINS pDrvIns;
|
---|
387 | /** Pointer to the root hub port interface we're attached to. */
|
---|
388 | PVUSBIROOTHUBPORT pIRhPort;
|
---|
389 | /** Connector interface exposed upwards. */
|
---|
390 | VUSBIROOTHUBCONNECTOR IRhConnector;
|
---|
391 |
|
---|
392 | #if HC_ARCH_BITS == 32
|
---|
393 | uint32_t Alignment0;
|
---|
394 | #endif
|
---|
395 |
|
---|
396 | /** Critical section protecting the device list. */
|
---|
397 | RTCRITSECT CritSectDevices;
|
---|
398 | /** Chain of devices attached to this hub. */
|
---|
399 | PVUSBDEV pDevices;
|
---|
400 |
|
---|
401 | #if HC_ARCH_BITS == 32
|
---|
402 | uint32_t Alignment1;
|
---|
403 | #endif
|
---|
404 |
|
---|
405 | /** Availability Bitmap. */
|
---|
406 | VUSBPORTBITMAP Bitmap;
|
---|
407 |
|
---|
408 | /** Sniffer instance for the root hub. */
|
---|
409 | VUSBSNIFFER hSniffer;
|
---|
410 | /** Version of the attached Host Controller. */
|
---|
411 | uint32_t fHcVersions;
|
---|
412 | /** Size of the HCI specific data for each URB. */
|
---|
413 | size_t cbHci;
|
---|
414 | /** Size of the HCI specific TD. */
|
---|
415 | size_t cbHciTd;
|
---|
416 | #ifdef LOG_ENABLED
|
---|
417 | /** A serial number for URBs submitted on the roothub instance.
|
---|
418 | * Only logging builds. */
|
---|
419 | uint32_t iSerial;
|
---|
420 | /** Alignment */
|
---|
421 | uint32_t Alignment2;
|
---|
422 | #endif
|
---|
423 | #ifdef VBOX_WITH_STATISTICS
|
---|
424 | VUSBROOTHUBTYPESTATS Total;
|
---|
425 | VUSBROOTHUBTYPESTATS aTypes[VUSBXFERTYPE_MSG];
|
---|
426 | STAMCOUNTER StatIsocReqPkts;
|
---|
427 | STAMCOUNTER StatIsocReqReadPkts;
|
---|
428 | STAMCOUNTER StatIsocReqWritePkts;
|
---|
429 | STAMCOUNTER StatIsocActPkts;
|
---|
430 | STAMCOUNTER StatIsocActReadPkts;
|
---|
431 | STAMCOUNTER StatIsocActWritePkts;
|
---|
432 | struct
|
---|
433 | {
|
---|
434 | STAMCOUNTER Pkts;
|
---|
435 | STAMCOUNTER Ok;
|
---|
436 | STAMCOUNTER Ok0;
|
---|
437 | STAMCOUNTER DataUnderrun;
|
---|
438 | STAMCOUNTER DataUnderrun0;
|
---|
439 | STAMCOUNTER DataOverrun;
|
---|
440 | STAMCOUNTER NotAccessed;
|
---|
441 | STAMCOUNTER Misc;
|
---|
442 | STAMCOUNTER Bytes;
|
---|
443 | } aStatIsocDetails[8];
|
---|
444 |
|
---|
445 | STAMPROFILE StatReapAsyncUrbs;
|
---|
446 | STAMPROFILE StatSubmitUrb;
|
---|
447 | #endif
|
---|
448 | } VUSBROOTHUB;
|
---|
449 | AssertCompileMemberAlignment(VUSBROOTHUB, IRhConnector, 8);
|
---|
450 | AssertCompileMemberAlignment(VUSBROOTHUB, Bitmap, 8);
|
---|
451 | AssertCompileMemberAlignment(VUSBROOTHUB, CritSectDevices, 8);
|
---|
452 | #ifdef VBOX_WITH_STATISTICS
|
---|
453 | AssertCompileMemberAlignment(VUSBROOTHUB, Total, 8);
|
---|
454 | #endif
|
---|
455 |
|
---|
456 | /** Converts a pointer to VUSBROOTHUB::IRhConnector to a PVUSBROOTHUB. */
|
---|
457 | #define VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface) (PVUSBROOTHUB)( (uintptr_t)(pInterface) - RT_OFFSETOF(VUSBROOTHUB, IRhConnector) )
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * URB cancellation modes
|
---|
461 | */
|
---|
462 | typedef enum CANCELMODE
|
---|
463 | {
|
---|
464 | /** complete the URB with an error (CRC). */
|
---|
465 | CANCELMODE_FAIL = 0,
|
---|
466 | /** do not change the URB contents. */
|
---|
467 | CANCELMODE_UNDO
|
---|
468 | } CANCELMODE;
|
---|
469 |
|
---|
470 | /* @} */
|
---|
471 |
|
---|
472 |
|
---|
473 |
|
---|
474 | /** @name Internal URB Operations, Structures and Constants.
|
---|
475 | * @{ */
|
---|
476 | int vusbUrbSubmit(PVUSBURB pUrb);
|
---|
477 | void vusbUrbTrace(PVUSBURB pUrb, const char *pszMsg, bool fComplete);
|
---|
478 | void vusbUrbDoReapAsync(PVUSBURB pHead, RTMSINTERVAL cMillies);
|
---|
479 | void vusbUrbDoReapAsyncDev(PVUSBDEV pDev, RTMSINTERVAL cMillies);
|
---|
480 | void vusbUrbCancel(PVUSBURB pUrb, CANCELMODE mode);
|
---|
481 | void vusbUrbCancelAsync(PVUSBURB pUrb, CANCELMODE mode);
|
---|
482 | void vusbUrbRipe(PVUSBURB pUrb);
|
---|
483 | void vusbUrbCompletionRh(PVUSBURB pUrb);
|
---|
484 | int vusbUrbSubmitHardError(PVUSBURB pUrb);
|
---|
485 | int vusbUrbErrorRh(PVUSBURB pUrb);
|
---|
486 | int vusbDevUrbIoThreadWakeup(PVUSBDEV pDev);
|
---|
487 | int vusbDevUrbIoThreadCreate(PVUSBDEV pDev);
|
---|
488 | int vusbDevUrbIoThreadDestroy(PVUSBDEV pDev);
|
---|
489 | DECLHIDDEN(int) vusbDevIoThreadExecV(PVUSBDEV pDev, uint32_t fFlags, PFNRT pfnFunction, unsigned cArgs, va_list Args);
|
---|
490 | DECLHIDDEN(int) vusbDevIoThreadExec(PVUSBDEV pDev, uint32_t fFlags, PFNRT pfnFunction, unsigned cArgs, ...);
|
---|
491 | DECLHIDDEN(int) vusbDevIoThreadExecSync(PVUSBDEV pDev, PFNRT pfnFunction, unsigned cArgs, ...);
|
---|
492 | DECLHIDDEN(int) vusbUrbCancelWorker(PVUSBURB pUrb, CANCELMODE enmMode);
|
---|
493 |
|
---|
494 | void vusbUrbCompletionReadAhead(PVUSBURB pUrb);
|
---|
495 | VUSBREADAHEAD vusbReadAheadStart(PVUSBDEV pDev, PVUSBPIPE pPipe);
|
---|
496 | void vusbReadAheadStop(VUSBREADAHEAD hReadAhead);
|
---|
497 | int vusbUrbQueueAsyncRh(PVUSBURB pUrb);
|
---|
498 | int vusbUrbSubmitBufferedRead(PVUSBURB pUrb, VUSBREADAHEAD hReadAhead);
|
---|
499 | PVUSBURB vusbRhNewUrb(PVUSBROOTHUB pRh, uint8_t DstAddress, PVUSBDEV pDev, VUSBXFERTYPE enmType,
|
---|
500 | VUSBDIRECTION enmDir, uint32_t cbData, uint32_t cTds, const char *pszTag);
|
---|
501 |
|
---|
502 | /**
|
---|
503 | * Initializes the given URB pool.
|
---|
504 | *
|
---|
505 | * @returns VBox status code.
|
---|
506 | * @param pUrbPool The URB pool to initialize.
|
---|
507 |
|
---|
508 | */
|
---|
509 | DECLHIDDEN(int) vusbUrbPoolInit(PVUSBURBPOOL pUrbPool);
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Destroy a given URB pool freeing all ressources.
|
---|
513 | *
|
---|
514 | * @returns nothing.
|
---|
515 | * @param pUrbPool The URB pool to destroy.
|
---|
516 | */
|
---|
517 | DECLHIDDEN(void) vusbUrbPoolDestroy(PVUSBURBPOOL pUrbPool);
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Allocate a new URB from the given URB pool.
|
---|
521 | *
|
---|
522 | * @returns Pointer to the new URB or NULL if out of memory.
|
---|
523 | * @param pUrbPool The URB pool to allocate from.
|
---|
524 | * @param enmType Type of the URB.
|
---|
525 | * @param enmDir The direction of the URB.
|
---|
526 | * @param cbData The number of bytes to allocate for the data buffer.
|
---|
527 | * @param cbHci Size of the data private to the HCI for each URB when allocated.
|
---|
528 | * @param cbHciTd Size of one transfer descriptor.
|
---|
529 | * @param cTds Number of transfer descriptors.
|
---|
530 | */
|
---|
531 | DECLHIDDEN(PVUSBURB) vusbUrbPoolAlloc(PVUSBURBPOOL pUrbPool, VUSBXFERTYPE enmType,
|
---|
532 | VUSBDIRECTION enmDir, size_t cbData,
|
---|
533 | size_t cbHci, size_t cbHciTd, unsigned cTds);
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * Frees a given URB.
|
---|
537 | *
|
---|
538 | * @returns nothing.
|
---|
539 | * @param pUrbPool The URB pool the URB was allocated from.
|
---|
540 | * @param pUrb The URB to free.
|
---|
541 | */
|
---|
542 | DECLHIDDEN(void) vusbUrbPoolFree(PVUSBURBPOOL pUrbPool, PVUSBURB pUrb);
|
---|
543 |
|
---|
544 |
|
---|
545 | DECLINLINE(void) vusbUrbUnlink(PVUSBURB pUrb)
|
---|
546 | {
|
---|
547 | PVUSBDEV pDev = pUrb->pVUsb->pDev;
|
---|
548 |
|
---|
549 | RTCritSectEnter(&pDev->CritSectAsyncUrbs);
|
---|
550 | *pUrb->pVUsb->ppPrev = pUrb->pVUsb->pNext;
|
---|
551 | if (pUrb->pVUsb->pNext)
|
---|
552 | pUrb->pVUsb->pNext->pVUsb->ppPrev = pUrb->pVUsb->ppPrev;
|
---|
553 | pUrb->pVUsb->pNext = NULL;
|
---|
554 | pUrb->pVUsb->ppPrev = NULL;
|
---|
555 | RTCritSectLeave(&pDev->CritSectAsyncUrbs);
|
---|
556 | }
|
---|
557 |
|
---|
558 | /** @def vusbUrbAssert
|
---|
559 | * Asserts that a URB is valid.
|
---|
560 | */
|
---|
561 | #ifdef VBOX_STRICT
|
---|
562 | # define vusbUrbAssert(pUrb) do { \
|
---|
563 | AssertMsg(VALID_PTR((pUrb)), ("%p\n", (pUrb))); \
|
---|
564 | AssertMsg((pUrb)->u32Magic == VUSBURB_MAGIC, ("%#x", (pUrb)->u32Magic)); \
|
---|
565 | AssertMsg((pUrb)->enmState > VUSBURBSTATE_INVALID && (pUrb)->enmState < VUSBURBSTATE_END, \
|
---|
566 | ("%d\n", (pUrb)->enmState)); \
|
---|
567 | } while (0)
|
---|
568 | #else
|
---|
569 | # define vusbUrbAssert(pUrb) do {} while (0)
|
---|
570 | #endif
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * @def VUSBDEV_ASSERT_VALID_STATE
|
---|
574 | * Asserts that the give device state is valid.
|
---|
575 | */
|
---|
576 | #define VUSBDEV_ASSERT_VALID_STATE(enmState) \
|
---|
577 | AssertMsg((enmState) > VUSB_DEVICE_STATE_INVALID && (enmState) < VUSB_DEVICE_STATE_DESTROYED, ("enmState=%#x\n", enmState));
|
---|
578 |
|
---|
579 | /** Executes a function synchronously. */
|
---|
580 | #define VUSB_DEV_IO_THREAD_EXEC_FLAGS_SYNC RT_BIT_32(0)
|
---|
581 |
|
---|
582 | /** @} */
|
---|
583 |
|
---|
584 |
|
---|
585 |
|
---|
586 |
|
---|
587 | /**
|
---|
588 | * Addresses are between 0 and 127 inclusive
|
---|
589 | */
|
---|
590 | DECLINLINE(uint8_t) vusbHashAddress(uint8_t Address)
|
---|
591 | {
|
---|
592 | uint8_t u8Hash = Address;
|
---|
593 | u8Hash ^= (Address >> 2);
|
---|
594 | u8Hash ^= (Address >> 3);
|
---|
595 | u8Hash %= VUSB_ADDR_HASHSZ;
|
---|
596 | return u8Hash;
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|
600 | /**
|
---|
601 | * Gets the roothub of a device.
|
---|
602 | *
|
---|
603 | * @returns Pointer to the roothub instance the device is attached to.
|
---|
604 | * @returns NULL if not attached to any hub.
|
---|
605 | * @param pDev Pointer to the device in question.
|
---|
606 | */
|
---|
607 | DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev)
|
---|
608 | {
|
---|
609 | if (!pDev->pHub)
|
---|
610 | return NULL;
|
---|
611 | return pDev->pHub->pRootHub;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Returns the state of the USB device.
|
---|
617 | *
|
---|
618 | * @returns State of the USB device.
|
---|
619 | * @param pDev Pointer to the device.
|
---|
620 | */
|
---|
621 | DECLINLINE(VUSBDEVICESTATE) vusbDevGetState(PVUSBDEV pDev)
|
---|
622 | {
|
---|
623 | VUSBDEVICESTATE enmState = (VUSBDEVICESTATE)ASMAtomicReadU32((volatile uint32_t *)&pDev->enmState);
|
---|
624 | VUSBDEV_ASSERT_VALID_STATE(enmState);
|
---|
625 | return enmState;
|
---|
626 | }
|
---|
627 |
|
---|
628 |
|
---|
629 | /**
|
---|
630 | * Sets the given state for the USB device.
|
---|
631 | *
|
---|
632 | * @returns The old state of the device.
|
---|
633 | * @param pDev Pointer to the device.
|
---|
634 | * @param enmState The new state to set.
|
---|
635 | */
|
---|
636 | DECLINLINE(VUSBDEVICESTATE) vusbDevSetState(PVUSBDEV pDev, VUSBDEVICESTATE enmState)
|
---|
637 | {
|
---|
638 | VUSBDEV_ASSERT_VALID_STATE(enmState);
|
---|
639 | VUSBDEVICESTATE enmStateOld = (VUSBDEVICESTATE)ASMAtomicXchgU32((volatile uint32_t *)&pDev->enmState, enmState);
|
---|
640 | VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
|
---|
641 | return enmStateOld;
|
---|
642 | }
|
---|
643 |
|
---|
644 |
|
---|
645 | /**
|
---|
646 | * Compare and exchange the states for the given USB device.
|
---|
647 | *
|
---|
648 | * @returns true if the state was changed.
|
---|
649 | * @returns false if the state wasn't changed.
|
---|
650 | * @param pDev Pointer to the device.
|
---|
651 | * @param enmStateNew The new state to set.
|
---|
652 | * @param enmStateOld The old state to compare with.
|
---|
653 | */
|
---|
654 | DECLINLINE(bool) vusbDevSetStateCmp(PVUSBDEV pDev, VUSBDEVICESTATE enmStateNew, VUSBDEVICESTATE enmStateOld)
|
---|
655 | {
|
---|
656 | VUSBDEV_ASSERT_VALID_STATE(enmStateNew);
|
---|
657 | VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
|
---|
658 | return ASMAtomicCmpXchgU32((volatile uint32_t *)&pDev->enmState, enmStateNew, enmStateOld);
|
---|
659 | }
|
---|
660 |
|
---|
661 | /** Strings for the CTLSTAGE enum values. */
|
---|
662 | extern const char * const g_apszCtlStates[4];
|
---|
663 | /** Default message pipe. */
|
---|
664 | extern const VUSBDESCENDPOINTEX g_Endpoint0;
|
---|
665 | /** Default configuration. */
|
---|
666 | extern const VUSBDESCCONFIGEX g_Config0;
|
---|
667 |
|
---|
668 | RT_C_DECLS_END
|
---|
669 | #endif
|
---|
670 |
|
---|