1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/* $Id: VUSBReadAhead.cpp $ */
/** @file
* Virtual USB - Read-ahead buffering for periodic endpoints.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_VUSB
#include <VBox/vmm/pdm.h>
#include <VBox/vmm/vmapi.h>
#include <VBox/err.h>
#include <VBox/log.h>
#include <iprt/alloc.h>
#include <iprt/time.h>
#include <iprt/thread.h>
#include <iprt/semaphore.h>
#include <iprt/string.h>
#include <iprt/assert.h>
#include <iprt/asm.h>
#include "VUSBInternal.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Argument package of vusbDevReadAheadThread().
*/
typedef struct vusb_read_ahead_args
{
/** Pointer to the device which the thread is for. */
PVUSBDEV pDev;
/** Pointer to the pipe which the thread is servicing. */
PVUSBPIPE pPipe;
/** A flag indicating a high-speed (vs. low/full-speed) endpoint. */
bool fHighSpeed;
/** A flag telling the thread to terminate. */
bool fTerminate;
} VUSBREADAHEADARGS, *PVUSBREADAHEADARGS;
/*******************************************************************************
* Implementation *
*******************************************************************************/
static PVUSBURB vusbDevNewIsocUrb(PVUSBDEV pDev, unsigned uEndPt, unsigned uInterval, unsigned uPktSize)
{
PVUSBURB pUrb;
unsigned cPackets = 0;
uint32_t cbTotal = 0;
unsigned uNextIndex = 0;
Assert(pDev);
Assert(uEndPt);
Assert(uInterval);
Assert(uPktSize);
/* Calculate the amount of data needed, taking the endpoint's bInterval into account */
for (unsigned i = 0; i < 8; ++i)
{
if (i == uNextIndex)
{
cbTotal += uPktSize;
cPackets++;
uNextIndex += uInterval;
}
}
Assert(cbTotal <= 24576);
// @todo: What do we do if cPackets is 0?
/*
* Allocate and initialize the URB.
*/
Assert(pDev->u8Address != VUSB_INVALID_ADDRESS);
PVUSBROOTHUB pRh = vusbDevGetRh(pDev);
if (!pRh)
/* can happen during disconnect */
return NULL;
pUrb = vusbRhNewUrb(pRh, pDev->u8Address, cbTotal, 1);
if (!pUrb)
/* not much we can do here... */
return NULL;
pUrb->enmType = VUSBXFERTYPE_ISOC;
pUrb->EndPt = uEndPt;
pUrb->enmDir = VUSBDIRECTION_IN;
pUrb->fShortNotOk = false;
pUrb->enmStatus = VUSBSTATUS_OK;
pUrb->Hci.EdAddr = 0;
pUrb->Hci.fUnlinked = false;
// @todo: fill in the rest? The Hci member is not relevant
#ifdef LOG_ENABLED
static unsigned s_iSerial = 0;
s_iSerial = (s_iSerial + 1) % 10000;
RTStrAPrintf(&pUrb->pszDesc, "URB %p prab<%04d", pUrb, s_iSerial); // prab = Periodic Read-Ahead Buffer
#endif
/* Set up the individual packets, again with bInterval in mind */
pUrb->cIsocPkts = 8;
unsigned off = 0;
uNextIndex = 0;
for (unsigned i = 0; i < 8; i++)
{
pUrb->aIsocPkts[i].enmStatus = VUSBSTATUS_NOT_ACCESSED;
pUrb->aIsocPkts[i].off = off;
if (i == uNextIndex) // skip unused packets
{
pUrb->aIsocPkts[i].cb = uPktSize;
off += uPktSize;
uNextIndex += uInterval;
}
else
pUrb->aIsocPkts[i].cb = 0;
}
Assert(off == cbTotal);
return pUrb;
}
/**
* Thread function for performing read-ahead buffering of periodic input.
*
* This thread keeps a buffer (queue) filled with data read from a periodic
* input endpoint.
*
* The problem: In the EHCI emulation, there is a very short period between the
* time when the guest can schedule a request and the time when it expects the results.
* This leads to many dropped URBs because by the time we get the data from the host,
* the guest already gave up and moved on.
*
* The solution: For periodic transfers, we know the worst-case bandwidth. We can
* read ahead and buffer a few milliseconds worth of data. That way data is available
* by the time the guest asks for it and we can deliver it immediately.
*
* @returns success indicator.
* @param Thread This thread.
* @param pvUser Pointer to a VUSBREADAHEADARGS structure.
*/
static DECLCALLBACK(int) vusbDevReadAheadThread(RTTHREAD Thread, void *pvUser)
{
PVUSBPIPE pPipe;
PVUSBREADAHEADARGS pArgs = (PVUSBREADAHEADARGS)pvUser;
PCVUSBDESCENDPOINT pDesc;
PVUSBURB pUrb;
int rc = VINF_SUCCESS;
unsigned max_pkt_size, mult, interval;
LogFlow(("vusb: periodic read-ahead buffer thread started\n"));
Assert(pArgs);
Assert(pArgs->pPipe && pArgs->pDev);
pPipe = pArgs->pPipe;
pDesc = &pPipe->in->Core;
Assert(pDesc);
/* The previous read-ahead thread could be still running (vusbReadAheadStop sets only
* fTerminate to true and returns immediately). Therefore we have to wait until the
* previous thread is done and all submitted URBs are completed. */
while ( pPipe->cSubmitted > 0
&& pPipe->cBuffered > 0)
{
Log2(("vusbDevReadAheadThread: still %u packets submitted, waiting before starting...\n", pPipe->cSubmitted));
RTThreadSleep(1);
}
pPipe->pvReadAheadArgs = pArgs;
pPipe->cBuffered = 0;
/* Figure out the maximum bandwidth we might need */
if (pArgs->fHighSpeed)
{
/* High-speed endpoint */
Assert((pDesc->wMaxPacketSize & 0x1fff) == pDesc->wMaxPacketSize);
Assert(pDesc->bInterval <= 16);
interval = pDesc->bInterval ? 1 << (pDesc->bInterval - 1) : 1;
max_pkt_size = pDesc->wMaxPacketSize & 0x7ff;
mult = ((pDesc->wMaxPacketSize & 0x1800) >> 11) + 1;
}
else
{
/* Full- or low-speed endpoint */
Assert((pDesc->wMaxPacketSize & 0x7ff) == pDesc->wMaxPacketSize);
interval = pDesc->bInterval;
max_pkt_size = pDesc->wMaxPacketSize;
mult = 1;
}
Log(("vusb: interval=%u, max pkt size=%u, multiplier=%u\n", interval, max_pkt_size, mult));
/*
* Submit new URBs in a loop unless the buffer is too full (paused VM etc.). Note that we only
* queue the URBs here, they are reaped on a different thread.
*/
while (pArgs->fTerminate == false)
{
while (pPipe->cSubmitted < 120 && pPipe->cBuffered < 120)
{
pUrb = vusbDevNewIsocUrb(pArgs->pDev, pDesc->bEndpointAddress & 0xF, interval, max_pkt_size * mult);
if (!pUrb) {
/* Happens if device was unplugged. */
Log(("vusb: read-ahead thread failed to allocate new URB; exiting\n"));
vusbReadAheadStop(pvUser);
break;
}
Assert(pUrb->enmState == VUSBURBSTATE_ALLOCATED);
// @todo: at the moment we abuse the Hci.pNext member (which is otherwise entirely unused!)
pUrb->Hci.pNext = (PVUSBURB)pvUser;
pUrb->enmState = VUSBURBSTATE_IN_FLIGHT;
rc = vusbUrbQueueAsyncRh(pUrb);
if (RT_FAILURE(rc))
{
/* Happens if device was unplugged. */
Log(("vusb: read-ahead thread failed to queue URB with %Rrc; exiting\n", rc));
vusbReadAheadStop(pvUser);
break;
}
++pPipe->cSubmitted;
}
RTThreadSleep(1);
}
LogFlow(("vusb: periodic read-ahead buffer thread exiting\n"));
pPipe->pvReadAheadArgs = NULL;
/* wait until there are no more submitted packets */
while (pPipe->cSubmitted > 0)
{
Log2(("vusbDevReadAheadThread: still %u packets submitted, waiting before terminating...\n", pPipe->cSubmitted));
RTThreadSleep(1);
}
/*
* Free all still buffered URBs because another endpoint with a different packet size
* and complete different data formats might be served later.
*/
int cFree = 0;
while (pPipe->pBuffUrbHead)
{
PVUSBURB pBufferedUrb = pPipe->pBuffUrbHead;
pPipe->pBuffUrbHead = pBufferedUrb->Hci.pNext;
pBufferedUrb->VUsb.pfnFree(pBufferedUrb);
cFree++;
}
pPipe->pBuffUrbTail = NULL;
pPipe->cBuffered = 0;
RTMemTmpFree(pArgs);
return rc;
}
/**
* Completes a read-ahead URB. This function does *not* free the URB but puts
* it on a queue instead. The URB is only freed when the guest asks for the data
* (by reading on the buffered pipe) or when the pipe/device is torn down.
*/
void vusbUrbCompletionReadAhead(PVUSBURB pUrb)
{
Assert(pUrb);
Assert(pUrb->Hci.pNext);
PVUSBREADAHEADARGS pArgs = (PVUSBREADAHEADARGS)pUrb->Hci.pNext;
PVUSBPIPE pPipe = pArgs->pPipe;
Assert(pPipe);
pUrb->Hci.pNext = NULL; // @todo: use a more suitable field
if (pPipe->pBuffUrbHead == NULL)
{
// The queue is empty, this is easy
Assert(!pPipe->pBuffUrbTail);
pPipe->pBuffUrbTail = pPipe->pBuffUrbHead = pUrb;
}
else
{
// Some URBs are queued already
Assert(pPipe->pBuffUrbTail);
Assert(!pPipe->pBuffUrbTail->Hci.pNext);
pPipe->pBuffUrbTail = pPipe->pBuffUrbTail->Hci.pNext = pUrb;
}
--pPipe->cSubmitted;
++pPipe->cBuffered;
}
/**
* Process a submit of an input URB on a pipe with read-ahead buffering. Instead
* of passing the URB to the proxy, we use previously read data stored in the
* read-ahead buffer, immediately complete the input URB and free the buffered URB.
*
* @param pUrb The URB submitted by HC
* @param pPipe The pipe with read-ahead buffering
*
* @return int Status code
*/
int vusbUrbSubmitBufferedRead(PVUSBURB pUrb, PVUSBPIPE pPipe)
{
PVUSBURB pBufferedUrb;
Assert(pUrb && pPipe);
pBufferedUrb = pPipe->pBuffUrbHead;
if (pBufferedUrb)
{
unsigned cbTotal;
// There's a URB available in the read-ahead buffer; use it
pPipe->pBuffUrbHead = pBufferedUrb->Hci.pNext;
if (pPipe->pBuffUrbHead == NULL)
pPipe->pBuffUrbTail = NULL;
--pPipe->cBuffered;
// Make sure the buffered URB is what we expect
Assert(pUrb->enmType == pBufferedUrb->enmType);
Assert(pUrb->EndPt == pBufferedUrb->EndPt);
Assert(pUrb->enmDir == pBufferedUrb->enmDir);
pUrb->enmState = VUSBURBSTATE_REAPED;
pUrb->enmStatus = pBufferedUrb->enmStatus;
cbTotal = 0;
// Copy status and data received from the device
for (unsigned i = 0; i < pUrb->cIsocPkts; ++i)
{
unsigned off, len;
off = pBufferedUrb->aIsocPkts[i].off;
len = pBufferedUrb->aIsocPkts[i].cb;
pUrb->aIsocPkts[i].cb = len;
pUrb->aIsocPkts[i].off = off;
pUrb->aIsocPkts[i].enmStatus = pBufferedUrb->aIsocPkts[i].enmStatus;
cbTotal += len;
Assert(pUrb->VUsb.cbDataAllocated >= cbTotal);
memcpy(&pUrb->abData[off], &pBufferedUrb->abData[off], len);
}
// Give back the data to the HC right away and then free the buffered URB
vusbUrbCompletionRh(pUrb);
// This assertion is wrong as the URB could be re-allocated in the meantime by the EMT (race)
// Assert(pUrb->enmState == VUSBURBSTATE_FREE);
Assert(pBufferedUrb->enmState == VUSBURBSTATE_REAPED);
LogFlow(("%s: vusbUrbSubmitBufferedRead: Freeing buffered URB\n", pBufferedUrb->pszDesc));
pBufferedUrb->VUsb.pfnFree(pBufferedUrb);
// This assertion is wrong as the URB could be re-allocated in the meantime by the EMT (race)
// Assert(pBufferedUrb->enmState == VUSBURBSTATE_FREE);
}
else
{
// No URB on hand. Either we exhausted the buffer (shouldn't happen!) or the guest simply
// asked for data too soon. Pretend that the device didn't deliver any data.
pUrb->enmState = VUSBURBSTATE_REAPED;
pUrb->enmStatus = VUSBSTATUS_DATA_UNDERRUN;
for (unsigned i = 0; i < pUrb->cIsocPkts; ++i)
{
pUrb->aIsocPkts[i].cb = 0;
pUrb->aIsocPkts[i].enmStatus = VUSBSTATUS_NOT_ACCESSED;
}
vusbUrbCompletionRh(pUrb);
// This assertion is wrong as the URB could be re-allocated in the meantime by the EMT (race)
// Assert(pUrb->enmState == VUSBURBSTATE_FREE);
LogFlow(("%s: vusbUrbSubmitBufferedRead: No buffered URB available!\n", pBufferedUrb->pszDesc));
}
return VINF_SUCCESS;
}
/* Read-ahead start/stop functions, used primarily to keep the PVUSBREADAHEADARGS struct private to this module. */
void vusbReadAheadStart(PVUSBDEV pDev, PVUSBPIPE pPipe)
{
int rc;
PVUSBREADAHEADARGS pArgs = (PVUSBREADAHEADARGS)RTMemTmpAlloc(sizeof(*pArgs));
if (pArgs)
{
PVUSBROOTHUB pRh = vusbDevGetRh(pDev);
pArgs->pDev = pDev;
pArgs->pPipe = pPipe;
pArgs->fTerminate = false;
pArgs->fHighSpeed = pRh && ((pRh->fHcVersions & VUSB_STDVER_20) != 0);
if (pArgs->fHighSpeed)
rc = RTThreadCreate(&pPipe->ReadAheadThread, vusbDevReadAheadThread, pArgs, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "USBISOC");
else
rc = VERR_VUSB_DEVICE_NOT_ATTACHED; // No buffering for low/full-speed devices at the moment, needs testing.
if (RT_SUCCESS(rc))
{
Log(("vusb: created isochronous read-ahead thread\n"));
}
else
{
Log(("vusb: isochronous read-ahead thread creation failed, rc=%d\n", rc));
pPipe->ReadAheadThread = NIL_RTTHREAD;
RTMemTmpFree(pArgs);
}
}
/* If thread creation failed for any reason, simply fall back to standard processing. */
}
void vusbReadAheadStop(void *pvReadAheadArgs)
{
PVUSBREADAHEADARGS pArgs = (PVUSBREADAHEADARGS)pvReadAheadArgs;
Log(("vusb: terminating read-ahead thread for endpoint\n"));
pArgs->fTerminate = true;
}
/*
* Local Variables:
* mode: c
* c-file-style: "bsd"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: s
* End:
*/
|