1 | /* $Id: VUSBDevice.cpp 59775 2016-02-22 13:58:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual USB - Device.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DRV_VUSB
|
---|
23 | #include <VBox/vmm/pdm.h>
|
---|
24 | #include <VBox/vmm/vmapi.h>
|
---|
25 | #include <VBox/err.h>
|
---|
26 | #include <VBox/log.h>
|
---|
27 | #include <iprt/alloc.h>
|
---|
28 | #include <iprt/time.h>
|
---|
29 | #include <iprt/thread.h>
|
---|
30 | #include <iprt/semaphore.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include "VUSBInternal.h"
|
---|
35 |
|
---|
36 | #include "VUSBSniffer.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Structures and Typedefs *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | /**
|
---|
43 | * Argument package of vusbDevResetThread().
|
---|
44 | */
|
---|
45 | typedef struct vusb_reset_args
|
---|
46 | {
|
---|
47 | /** Pointer to the device which is being reset. */
|
---|
48 | PVUSBDEV pDev;
|
---|
49 | /** The reset return code. */
|
---|
50 | int rc;
|
---|
51 | /** Pointer to the completion callback. */
|
---|
52 | PFNVUSBRESETDONE pfnDone;
|
---|
53 | /** User argument to pfnDone. */
|
---|
54 | void *pvUser;
|
---|
55 | } VUSBRESETARGS, *PVUSBRESETARGS;
|
---|
56 |
|
---|
57 |
|
---|
58 | /*********************************************************************************************************************************
|
---|
59 | * Global Variables *
|
---|
60 | *********************************************************************************************************************************/
|
---|
61 | /** Default message pipe. */
|
---|
62 | const VUSBDESCENDPOINTEX g_Endpoint0 =
|
---|
63 | {
|
---|
64 | {
|
---|
65 | /* .bLength = */ VUSB_DT_ENDPOINT_MIN_LEN,
|
---|
66 | /* .bDescriptorType = */ VUSB_DT_ENDPOINT,
|
---|
67 | /* .bEndpointAddress = */ 0,
|
---|
68 | /* .bmAttributes = */ 0,
|
---|
69 | /* .wMaxPacketSize = */ 64,
|
---|
70 | /* .bInterval = */ 0
|
---|
71 | },
|
---|
72 | NULL
|
---|
73 | };
|
---|
74 |
|
---|
75 | /** Default configuration. */
|
---|
76 | const VUSBDESCCONFIGEX g_Config0 =
|
---|
77 | {
|
---|
78 | {
|
---|
79 | /* .bLength = */ VUSB_DT_CONFIG_MIN_LEN,
|
---|
80 | /* .bDescriptorType = */ VUSB_DT_CONFIG,
|
---|
81 | /* .WTotalLength = */ 0, /* (auto-calculated) */
|
---|
82 | /* .bNumInterfaces = */ 0,
|
---|
83 | /* .bConfigurationValue =*/ 0,
|
---|
84 | /* .iConfiguration = */ 0,
|
---|
85 | /* .bmAttributes = */ 0x80,
|
---|
86 | /* .MaxPower = */ 14
|
---|
87 | },
|
---|
88 | NULL,
|
---|
89 | NULL
|
---|
90 | };
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | static PCVUSBDESCCONFIGEX vusbDevFindCfgDesc(PVUSBDEV pDev, int iCfg)
|
---|
95 | {
|
---|
96 | if (iCfg == 0)
|
---|
97 | return &g_Config0;
|
---|
98 |
|
---|
99 | for (unsigned i = 0; i < pDev->pDescCache->pDevice->bNumConfigurations; i++)
|
---|
100 | if (pDev->pDescCache->paConfigs[i].Core.bConfigurationValue == iCfg)
|
---|
101 | return &pDev->pDescCache->paConfigs[i];
|
---|
102 | return NULL;
|
---|
103 | }
|
---|
104 |
|
---|
105 | static PVUSBINTERFACESTATE vusbDevFindIfState(PVUSBDEV pDev, int iIf)
|
---|
106 | {
|
---|
107 | for (unsigned i = 0; i < pDev->pCurCfgDesc->Core.bNumInterfaces; i++)
|
---|
108 | if (pDev->paIfStates[i].pIf->paSettings[0].Core.bInterfaceNumber == iIf)
|
---|
109 | return &pDev->paIfStates[i];
|
---|
110 | return NULL;
|
---|
111 | }
|
---|
112 |
|
---|
113 | static PCVUSBDESCINTERFACEEX vusbDevFindAltIfDesc(PVUSBDEV pDev, PCVUSBINTERFACESTATE pIfState, int iAlt)
|
---|
114 | {
|
---|
115 | for (uint32_t i = 0; i < pIfState->pIf->cSettings; i++)
|
---|
116 | if (pIfState->pIf->paSettings[i].Core.bAlternateSetting == iAlt)
|
---|
117 | return &pIfState->pIf->paSettings[i];
|
---|
118 | return NULL;
|
---|
119 | }
|
---|
120 |
|
---|
121 | void vusbDevMapEndpoint(PVUSBDEV pDev, PCVUSBDESCENDPOINTEX pEndPtDesc)
|
---|
122 | {
|
---|
123 | uint8_t i8Addr = pEndPtDesc->Core.bEndpointAddress & 0xF;
|
---|
124 | PVUSBPIPE pPipe = &pDev->aPipes[i8Addr];
|
---|
125 | LogFlow(("vusbDevMapEndpoint: pDev=%p[%s] pEndPtDesc=%p{.bEndpointAddress=%#x, .bmAttributes=%#x} p=%p stage %s->SETUP\n",
|
---|
126 | pDev, pDev->pUsbIns->pszName, pEndPtDesc, pEndPtDesc->Core.bEndpointAddress, pEndPtDesc->Core.bmAttributes,
|
---|
127 | pPipe, g_apszCtlStates[pPipe->pCtrl ? pPipe->pCtrl->enmStage : 3]));
|
---|
128 |
|
---|
129 | if ((pEndPtDesc->Core.bmAttributes & 0x3) == 0)
|
---|
130 | {
|
---|
131 | Log(("vusb: map message pipe on address %u\n", i8Addr));
|
---|
132 | pPipe->in = pEndPtDesc;
|
---|
133 | pPipe->out = pEndPtDesc;
|
---|
134 | }
|
---|
135 | else if (pEndPtDesc->Core.bEndpointAddress & 0x80)
|
---|
136 | {
|
---|
137 | Log(("vusb: map input pipe on address %u\n", i8Addr));
|
---|
138 | pPipe->in = pEndPtDesc;
|
---|
139 | }
|
---|
140 | else
|
---|
141 | {
|
---|
142 | Log(("vusb: map output pipe on address %u\n", i8Addr));
|
---|
143 | pPipe->out = pEndPtDesc;
|
---|
144 |
|
---|
145 | #if 0
|
---|
146 | if ((pEndPtDesc->Core.bmAttributes & 0x03) == 1)
|
---|
147 | {
|
---|
148 | int rc = vusbBufferedPipeCreate(pDev, pPipe, VUSBDIRECTION_OUT, pDev->pUsbIns->enmSpeed,
|
---|
149 | 32 /* cLatencyMs*/, &pPipe->hBuffer);
|
---|
150 | if (RT_SUCCESS(rc))
|
---|
151 | LogRel(("VUSB: Created a buffered pipe for isochronous output endpoint\n"));
|
---|
152 | else
|
---|
153 | LogRel(("VUSB: Failed to create a buffered pipe for isochronous output endpoint with rc=%Rrc\n", rc));
|
---|
154 | }
|
---|
155 | #endif
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (pPipe->pCtrl)
|
---|
159 | {
|
---|
160 | vusbMsgFreeExtraData(pPipe->pCtrl);
|
---|
161 | pPipe->pCtrl = NULL;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | static void unmap_endpoint(PVUSBDEV pDev, PCVUSBDESCENDPOINTEX pEndPtDesc)
|
---|
166 | {
|
---|
167 | uint8_t EndPt = pEndPtDesc->Core.bEndpointAddress & 0xF;
|
---|
168 | PVUSBPIPE pPipe = &pDev->aPipes[EndPt];
|
---|
169 | LogFlow(("unmap_endpoint: pDev=%p[%s] pEndPtDesc=%p{.bEndpointAddress=%#x, .bmAttributes=%#x} p=%p stage %s->SETUP\n",
|
---|
170 | pDev, pDev->pUsbIns->pszName, pEndPtDesc, pEndPtDesc->Core.bEndpointAddress, pEndPtDesc->Core.bmAttributes,
|
---|
171 | pPipe, g_apszCtlStates[pPipe->pCtrl ? pPipe->pCtrl->enmStage : 3]));
|
---|
172 |
|
---|
173 | if ((pEndPtDesc->Core.bmAttributes & 0x3) == 0)
|
---|
174 | {
|
---|
175 | Log(("vusb: unmap MSG pipe from address %u (%#x)\n", EndPt, pEndPtDesc->Core.bEndpointAddress));
|
---|
176 | pPipe->in = NULL;
|
---|
177 | pPipe->out = NULL;
|
---|
178 | }
|
---|
179 | else if (pEndPtDesc->Core.bEndpointAddress & 0x80)
|
---|
180 | {
|
---|
181 | Log(("vusb: unmap IN pipe from address %u (%#x)\n", EndPt, pEndPtDesc->Core.bEndpointAddress));
|
---|
182 | pPipe->in = NULL;
|
---|
183 |
|
---|
184 | /* Terminate the pipe buffer if created. */
|
---|
185 | if (pPipe->hBuffer)
|
---|
186 | {
|
---|
187 | vusbBufferedPipeDestroy(pPipe->hBuffer);
|
---|
188 | pPipe->hBuffer = NULL;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | else
|
---|
192 | {
|
---|
193 | Log(("vusb: unmap OUT pipe from address %u (%#x)\n", EndPt, pEndPtDesc->Core.bEndpointAddress));
|
---|
194 | pPipe->out = NULL;
|
---|
195 |
|
---|
196 | /* Terminate the pipe buffer if created. */
|
---|
197 | if (pPipe->hBuffer)
|
---|
198 | {
|
---|
199 | vusbBufferedPipeDestroy(pPipe->hBuffer);
|
---|
200 | pPipe->hBuffer = NULL;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (pPipe->pCtrl)
|
---|
205 | {
|
---|
206 | vusbMsgFreeExtraData(pPipe->pCtrl);
|
---|
207 | pPipe->pCtrl = NULL;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | static void map_interface(PVUSBDEV pDev, PCVUSBDESCINTERFACEEX pIfDesc)
|
---|
212 | {
|
---|
213 | LogFlow(("map_interface: pDev=%p[%s] pIfDesc=%p:{.iInterface=%d, .bAlternateSetting=%d}\n",
|
---|
214 | pDev, pDev->pUsbIns->pszName, pIfDesc, pIfDesc->Core.iInterface, pIfDesc->Core.bAlternateSetting));
|
---|
215 |
|
---|
216 | for (unsigned i = 0; i < pIfDesc->Core.bNumEndpoints; i++)
|
---|
217 | {
|
---|
218 | if ((pIfDesc->paEndpoints[i].Core.bEndpointAddress & 0xF) == VUSB_PIPE_DEFAULT)
|
---|
219 | Log(("vusb: Endpoint 0x%x on interface %u.%u tried to override the default message pipe!!!\n",
|
---|
220 | pIfDesc->paEndpoints[i].Core.bEndpointAddress, pIfDesc->Core.bInterfaceNumber, pIfDesc->Core.bAlternateSetting));
|
---|
221 | else
|
---|
222 | vusbDevMapEndpoint(pDev, &pIfDesc->paEndpoints[i]);
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Worker that resets the pipe data on select config and detach.
|
---|
229 | *
|
---|
230 | * This leaves the critical section unmolested
|
---|
231 | *
|
---|
232 | * @param pPipe The pipe which data should be reset.
|
---|
233 | */
|
---|
234 | static void vusbDevResetPipeData(PVUSBPIPE pPipe)
|
---|
235 | {
|
---|
236 | vusbMsgFreeExtraData(pPipe->pCtrl);
|
---|
237 | pPipe->pCtrl = NULL;
|
---|
238 |
|
---|
239 | if (pPipe->hBuffer)
|
---|
240 | {
|
---|
241 | vusbBufferedPipeDestroy(pPipe->hBuffer);
|
---|
242 | pPipe->hBuffer = NULL;
|
---|
243 | }
|
---|
244 |
|
---|
245 | RT_ZERO(pPipe->in);
|
---|
246 | RT_ZERO(pPipe->out);
|
---|
247 | pPipe->async = 0;
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | bool vusbDevDoSelectConfig(PVUSBDEV pDev, PCVUSBDESCCONFIGEX pCfgDesc)
|
---|
252 | {
|
---|
253 | LogFlow(("vusbDevDoSelectConfig: pDev=%p[%s] pCfgDesc=%p:{.iConfiguration=%d}\n",
|
---|
254 | pDev, pDev->pUsbIns->pszName, pCfgDesc, pCfgDesc->Core.iConfiguration));
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * Clean up all pipes and interfaces.
|
---|
258 | */
|
---|
259 | unsigned i;
|
---|
260 | for (i = 0; i < VUSB_PIPE_MAX; i++)
|
---|
261 | if (i != VUSB_PIPE_DEFAULT)
|
---|
262 | vusbDevResetPipeData(&pDev->aPipes[i]);
|
---|
263 | memset(pDev->paIfStates, 0, pCfgDesc->Core.bNumInterfaces * sizeof(pDev->paIfStates[0]));
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * Map in the default setting for every interface.
|
---|
267 | */
|
---|
268 | for (i = 0; i < pCfgDesc->Core.bNumInterfaces; i++)
|
---|
269 | {
|
---|
270 | PCVUSBINTERFACE pIf;
|
---|
271 | struct vusb_interface_state *pIfState;
|
---|
272 |
|
---|
273 | pIf = &pCfgDesc->paIfs[i];
|
---|
274 | pIfState = &pDev->paIfStates[i];
|
---|
275 | pIfState->pIf = pIf;
|
---|
276 |
|
---|
277 | /*
|
---|
278 | * Find the 0 setting, if it is not present we just use
|
---|
279 | * the lowest numbered one.
|
---|
280 | */
|
---|
281 | for (uint32_t j = 0; j < pIf->cSettings; j++)
|
---|
282 | {
|
---|
283 | if ( !pIfState->pCurIfDesc
|
---|
284 | || pIf->paSettings[j].Core.bAlternateSetting < pIfState->pCurIfDesc->Core.bAlternateSetting)
|
---|
285 | pIfState->pCurIfDesc = &pIf->paSettings[j];
|
---|
286 | if (pIfState->pCurIfDesc->Core.bAlternateSetting == 0)
|
---|
287 | break;
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (pIfState->pCurIfDesc)
|
---|
291 | map_interface(pDev, pIfState->pCurIfDesc);
|
---|
292 | }
|
---|
293 |
|
---|
294 | pDev->pCurCfgDesc = pCfgDesc;
|
---|
295 |
|
---|
296 | if (pCfgDesc->Core.bmAttributes & 0x40)
|
---|
297 | pDev->u16Status |= (1 << VUSB_DEV_SELF_POWERED);
|
---|
298 | else
|
---|
299 | pDev->u16Status &= ~(1 << VUSB_DEV_SELF_POWERED);
|
---|
300 |
|
---|
301 | return true;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Standard device request: SET_CONFIGURATION
|
---|
306 | * @returns success indicator.
|
---|
307 | */
|
---|
308 | static bool vusbDevStdReqSetConfig(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
309 | {
|
---|
310 | unsigned iCfg = pSetup->wValue & 0xff;
|
---|
311 |
|
---|
312 | if ((pSetup->bmRequestType & VUSB_RECIP_MASK) != VUSB_TO_DEVICE)
|
---|
313 | {
|
---|
314 | Log(("vusb: error: %s: SET_CONFIGURATION - invalid request (dir) !!!\n", pDev->pUsbIns->pszName));
|
---|
315 | return false;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /*
|
---|
319 | * Check that the device is in a valid state.
|
---|
320 | * (The caller has already checked that it's not being reset.)
|
---|
321 | */
|
---|
322 | const VUSBDEVICESTATE enmState = vusbDevGetState(pDev);
|
---|
323 | if (enmState == VUSB_DEVICE_STATE_DEFAULT)
|
---|
324 | {
|
---|
325 | LogFlow(("vusbDevStdReqSetConfig: %s: default dev state !!?\n", pDev->pUsbIns->pszName));
|
---|
326 | return false;
|
---|
327 | }
|
---|
328 |
|
---|
329 | PCVUSBDESCCONFIGEX pNewCfgDesc = vusbDevFindCfgDesc(pDev, iCfg);
|
---|
330 | if (!pNewCfgDesc)
|
---|
331 | {
|
---|
332 | Log(("vusb: error: %s: config %i not found !!!\n", pDev->pUsbIns->pszName, iCfg));
|
---|
333 | return false;
|
---|
334 | }
|
---|
335 |
|
---|
336 | if (iCfg == 0)
|
---|
337 | vusbDevSetState(pDev, VUSB_DEVICE_STATE_ADDRESS);
|
---|
338 | else
|
---|
339 | vusbDevSetState(pDev, VUSB_DEVICE_STATE_CONFIGURED);
|
---|
340 | if (pDev->pUsbIns->pReg->pfnUsbSetConfiguration)
|
---|
341 | {
|
---|
342 | int rc = vusbDevIoThreadExecSync(pDev, (PFNRT)pDev->pUsbIns->pReg->pfnUsbSetConfiguration, 5,
|
---|
343 | pDev->pUsbIns, pNewCfgDesc->Core.bConfigurationValue,
|
---|
344 | pDev->pCurCfgDesc, pDev->paIfStates, pNewCfgDesc);
|
---|
345 | if (RT_FAILURE(rc))
|
---|
346 | {
|
---|
347 | Log(("vusb: error: %s: failed to set config %i (%Rrc) !!!\n", pDev->pUsbIns->pszName, iCfg, rc));
|
---|
348 | return false;
|
---|
349 | }
|
---|
350 | }
|
---|
351 | Log(("vusb: %p[%s]: SET_CONFIGURATION: Selected config %u\n", pDev, pDev->pUsbIns->pszName, iCfg));
|
---|
352 | return vusbDevDoSelectConfig(pDev, pNewCfgDesc);
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | /**
|
---|
357 | * Standard device request: GET_CONFIGURATION
|
---|
358 | * @returns success indicator.
|
---|
359 | */
|
---|
360 | static bool vusbDevStdReqGetConfig(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
361 | {
|
---|
362 | if ((pSetup->bmRequestType & VUSB_RECIP_MASK) != VUSB_TO_DEVICE)
|
---|
363 | {
|
---|
364 | Log(("vusb: error: %s: GET_CONFIGURATION - invalid request (dir) !!!\n", pDev->pUsbIns->pszName));
|
---|
365 | return false;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /*
|
---|
369 | * Check that the device is in a valid state.
|
---|
370 | * (The caller has already checked that it's not being reset.)
|
---|
371 | */
|
---|
372 | const VUSBDEVICESTATE enmState = vusbDevGetState(pDev);
|
---|
373 | if ( enmState != VUSB_DEVICE_STATE_CONFIGURED
|
---|
374 | && enmState != VUSB_DEVICE_STATE_ADDRESS)
|
---|
375 | {
|
---|
376 | LogFlow(("vusbDevStdReqGetConfig: error: %s: invalid device state %d!!!\n", pDev->pUsbIns->pszName, enmState));
|
---|
377 | return false;
|
---|
378 | }
|
---|
379 |
|
---|
380 | if (*pcbBuf < 1)
|
---|
381 | {
|
---|
382 | LogFlow(("vusbDevStdReqGetConfig: %s: no space for data!\n", pDev->pUsbIns->pszName));
|
---|
383 | return true;
|
---|
384 | }
|
---|
385 |
|
---|
386 | uint8_t iCfg;
|
---|
387 | if (enmState == VUSB_DEVICE_STATE_ADDRESS)
|
---|
388 | iCfg = 0;
|
---|
389 | else
|
---|
390 | iCfg = pDev->pCurCfgDesc->Core.bConfigurationValue;
|
---|
391 |
|
---|
392 | *pbBuf = iCfg;
|
---|
393 | *pcbBuf = 1;
|
---|
394 | LogFlow(("vusbDevStdReqGetConfig: %s: returns iCfg=%d\n", pDev->pUsbIns->pszName, iCfg));
|
---|
395 | return true;
|
---|
396 | }
|
---|
397 |
|
---|
398 | /**
|
---|
399 | * Standard device request: GET_INTERFACE
|
---|
400 | * @returns success indicator.
|
---|
401 | */
|
---|
402 | static bool vusbDevStdReqGetInterface(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
403 | {
|
---|
404 | if ((pSetup->bmRequestType & VUSB_RECIP_MASK) != VUSB_TO_INTERFACE)
|
---|
405 | {
|
---|
406 | Log(("vusb: error: %s: GET_INTERFACE - invalid request (dir) !!!\n", pDev->pUsbIns->pszName));
|
---|
407 | return false;
|
---|
408 | }
|
---|
409 |
|
---|
410 | /*
|
---|
411 | * Check that the device is in a valid state.
|
---|
412 | * (The caller has already checked that it's not being reset.)
|
---|
413 | */
|
---|
414 | const VUSBDEVICESTATE enmState = vusbDevGetState(pDev);
|
---|
415 | if (enmState != VUSB_DEVICE_STATE_CONFIGURED)
|
---|
416 | {
|
---|
417 | LogFlow(("vusbDevStdReqGetInterface: error: %s: invalid device state %d!!!\n", pDev->pUsbIns->pszName, enmState));
|
---|
418 | return false;
|
---|
419 | }
|
---|
420 |
|
---|
421 | if (*pcbBuf < 1)
|
---|
422 | {
|
---|
423 | LogFlow(("vusbDevStdReqGetInterface: %s: no space for data!\n", pDev->pUsbIns->pszName));
|
---|
424 | return true;
|
---|
425 | }
|
---|
426 |
|
---|
427 | for (unsigned i = 0; i < pDev->pCurCfgDesc->Core.bNumInterfaces; i++)
|
---|
428 | {
|
---|
429 | PCVUSBDESCINTERFACEEX pIfDesc = pDev->paIfStates[i].pCurIfDesc;
|
---|
430 | if ( pIfDesc
|
---|
431 | && pSetup->wIndex == pIfDesc->Core.bInterfaceNumber)
|
---|
432 | {
|
---|
433 | *pbBuf = pIfDesc->Core.bAlternateSetting;
|
---|
434 | *pcbBuf = 1;
|
---|
435 | Log(("vusb: %s: GET_INTERFACE: %u.%u\n", pDev->pUsbIns->pszName, pIfDesc->Core.bInterfaceNumber, *pbBuf));
|
---|
436 | return true;
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | Log(("vusb: error: %s: GET_INTERFACE - unknown iface %u !!!\n", pDev->pUsbIns->pszName, pSetup->wIndex));
|
---|
441 | return false;
|
---|
442 | }
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * Standard device request: SET_INTERFACE
|
---|
446 | * @returns success indicator.
|
---|
447 | */
|
---|
448 | static bool vusbDevStdReqSetInterface(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
449 | {
|
---|
450 | if ((pSetup->bmRequestType & VUSB_RECIP_MASK) != VUSB_TO_INTERFACE)
|
---|
451 | {
|
---|
452 | Log(("vusb: error: %s: SET_INTERFACE - invalid request (dir) !!!\n", pDev->pUsbIns->pszName));
|
---|
453 | return false;
|
---|
454 | }
|
---|
455 |
|
---|
456 | /*
|
---|
457 | * Check that the device is in a valid state.
|
---|
458 | * (The caller has already checked that it's not being reset.)
|
---|
459 | */
|
---|
460 | const VUSBDEVICESTATE enmState = vusbDevGetState(pDev);
|
---|
461 | if (enmState != VUSB_DEVICE_STATE_CONFIGURED)
|
---|
462 | {
|
---|
463 | LogFlow(("vusbDevStdReqSetInterface: error: %s: invalid device state %d !!!\n", pDev->pUsbIns->pszName, enmState));
|
---|
464 | return false;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * Find the interface.
|
---|
469 | */
|
---|
470 | uint8_t iIf = pSetup->wIndex;
|
---|
471 | PVUSBINTERFACESTATE pIfState = vusbDevFindIfState(pDev, iIf);
|
---|
472 | if (!pIfState)
|
---|
473 | {
|
---|
474 | LogFlow(("vusbDevStdReqSetInterface: error: %s: couldn't find interface %u !!!\n", pDev->pUsbIns->pszName, iIf));
|
---|
475 | return false;
|
---|
476 | }
|
---|
477 | uint8_t iAlt = pSetup->wValue;
|
---|
478 | PCVUSBDESCINTERFACEEX pIfDesc = vusbDevFindAltIfDesc(pDev, pIfState, iAlt);
|
---|
479 | if (!pIfDesc)
|
---|
480 | {
|
---|
481 | LogFlow(("vusbDevStdReqSetInterface: error: %s: couldn't find alt interface %u.%u !!!\n", pDev->pUsbIns->pszName, iIf, iAlt));
|
---|
482 | return false;
|
---|
483 | }
|
---|
484 |
|
---|
485 | if (pDev->pUsbIns->pReg->pfnUsbSetInterface)
|
---|
486 | {
|
---|
487 | int rc = vusbDevIoThreadExecSync(pDev, (PFNRT)pDev->pUsbIns->pReg->pfnUsbSetInterface, 3, pDev->pUsbIns, iIf, iAlt);
|
---|
488 | if (RT_FAILURE(rc))
|
---|
489 | {
|
---|
490 | LogFlow(("vusbDevStdReqSetInterface: error: %s: couldn't find alt interface %u.%u (%Rrc)\n", pDev->pUsbIns->pszName, iIf, iAlt, rc));
|
---|
491 | return false;
|
---|
492 | }
|
---|
493 | }
|
---|
494 |
|
---|
495 | for (unsigned i = 0; i < pIfState->pCurIfDesc->Core.bNumEndpoints; i++)
|
---|
496 | unmap_endpoint(pDev, &pIfState->pCurIfDesc->paEndpoints[i]);
|
---|
497 |
|
---|
498 | Log(("vusb: SET_INTERFACE: Selected %u.%u\n", iIf, iAlt));
|
---|
499 |
|
---|
500 | map_interface(pDev, pIfDesc);
|
---|
501 | pIfState->pCurIfDesc = pIfDesc;
|
---|
502 |
|
---|
503 | return true;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /**
|
---|
507 | * Standard device request: SET_ADDRESS
|
---|
508 | * @returns success indicator.
|
---|
509 | */
|
---|
510 | static bool vusbDevStdReqSetAddress(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
511 | {
|
---|
512 | if ((pSetup->bmRequestType & VUSB_RECIP_MASK) != VUSB_TO_DEVICE)
|
---|
513 | {
|
---|
514 | Log(("vusb: error: %s: SET_ADDRESS - invalid request (dir) !!!\n", pDev->pUsbIns->pszName));
|
---|
515 | return false;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /*
|
---|
519 | * Check that the device is in a valid state.
|
---|
520 | * (The caller has already checked that it's not being reset.)
|
---|
521 | */
|
---|
522 | const VUSBDEVICESTATE enmState = vusbDevGetState(pDev);
|
---|
523 | if ( enmState != VUSB_DEVICE_STATE_DEFAULT
|
---|
524 | && enmState != VUSB_DEVICE_STATE_ADDRESS)
|
---|
525 | {
|
---|
526 | LogFlow(("vusbDevStdReqSetAddress: error: %s: invalid device state %d !!!\n", pDev->pUsbIns->pszName, enmState));
|
---|
527 | return false;
|
---|
528 | }
|
---|
529 |
|
---|
530 | pDev->u8NewAddress = pSetup->wValue;
|
---|
531 | return true;
|
---|
532 | }
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * Standard device request: CLEAR_FEATURE
|
---|
536 | * @returns success indicator.
|
---|
537 | *
|
---|
538 | * @remark This is only called for VUSB_TO_ENDPOINT && ep == 0 && wValue == ENDPOINT_HALT.
|
---|
539 | * All other cases of CLEAR_FEATURE is handled in the normal async/sync manner.
|
---|
540 | */
|
---|
541 | static bool vusbDevStdReqClearFeature(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
542 | {
|
---|
543 | switch (pSetup->bmRequestType & VUSB_RECIP_MASK)
|
---|
544 | {
|
---|
545 | case VUSB_TO_DEVICE:
|
---|
546 | Log(("vusb: ClearFeature: dev(%u): selector=%u\n", pSetup->wIndex, pSetup->wValue));
|
---|
547 | break;
|
---|
548 | case VUSB_TO_INTERFACE:
|
---|
549 | Log(("vusb: ClearFeature: iface(%u): selector=%u\n", pSetup->wIndex, pSetup->wValue));
|
---|
550 | break;
|
---|
551 | case VUSB_TO_ENDPOINT:
|
---|
552 | Log(("vusb: ClearFeature: ep(%u): selector=%u\n", pSetup->wIndex, pSetup->wValue));
|
---|
553 | if ( !EndPt /* Default control pipe only */
|
---|
554 | && pSetup->wValue == 0 /* ENDPOINT_HALT */
|
---|
555 | && pDev->pUsbIns->pReg->pfnUsbClearHaltedEndpoint)
|
---|
556 | {
|
---|
557 | int rc = vusbDevIoThreadExecSync(pDev, (PFNRT)pDev->pUsbIns->pReg->pfnUsbClearHaltedEndpoint,
|
---|
558 | 2, pDev->pUsbIns, pSetup->wIndex);
|
---|
559 | return RT_SUCCESS(rc);
|
---|
560 | }
|
---|
561 | break;
|
---|
562 | default:
|
---|
563 | AssertMsgFailed(("VUSB_TO_OTHER!\n"));
|
---|
564 | break;
|
---|
565 | }
|
---|
566 |
|
---|
567 | AssertMsgFailed(("Invalid safe check !!!\n"));
|
---|
568 | return false;
|
---|
569 | }
|
---|
570 |
|
---|
571 | /**
|
---|
572 | * Standard device request: SET_FEATURE
|
---|
573 | * @returns success indicator.
|
---|
574 | */
|
---|
575 | static bool vusbDevStdReqSetFeature(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
576 | {
|
---|
577 | switch (pSetup->bmRequestType & VUSB_RECIP_MASK)
|
---|
578 | {
|
---|
579 | case VUSB_TO_DEVICE:
|
---|
580 | Log(("vusb: SetFeature: dev(%u): selector=%u\n",
|
---|
581 | pSetup->wIndex, pSetup->wValue));
|
---|
582 | break;
|
---|
583 | case VUSB_TO_INTERFACE:
|
---|
584 | Log(("vusb: SetFeature: if(%u): selector=%u\n",
|
---|
585 | pSetup->wIndex, pSetup->wValue));
|
---|
586 | break;
|
---|
587 | case VUSB_TO_ENDPOINT:
|
---|
588 | Log(("vusb: SetFeature: ep(%u): selector=%u\n",
|
---|
589 | pSetup->wIndex, pSetup->wValue));
|
---|
590 | break;
|
---|
591 | default:
|
---|
592 | AssertMsgFailed(("VUSB_TO_OTHER!\n"));
|
---|
593 | return false;
|
---|
594 | }
|
---|
595 | AssertMsgFailed(("This stuff is bogus\n"));
|
---|
596 | return false;
|
---|
597 | }
|
---|
598 |
|
---|
599 | static bool vusbDevStdReqGetStatus(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
600 | {
|
---|
601 | if (*pcbBuf != 2)
|
---|
602 | {
|
---|
603 | LogFlow(("vusbDevStdReqGetStatus: %s: buffer is too small! (%d)\n", pDev->pUsbIns->pszName, *pcbBuf));
|
---|
604 | return false;
|
---|
605 | }
|
---|
606 |
|
---|
607 | uint16_t u16Status;
|
---|
608 | switch (pSetup->bmRequestType & VUSB_RECIP_MASK)
|
---|
609 | {
|
---|
610 | case VUSB_TO_DEVICE:
|
---|
611 | u16Status = pDev->u16Status;
|
---|
612 | LogFlow(("vusbDevStdReqGetStatus: %s: device status %#x (%d)\n", pDev->pUsbIns->pszName, u16Status, u16Status));
|
---|
613 | break;
|
---|
614 | case VUSB_TO_INTERFACE:
|
---|
615 | u16Status = 0;
|
---|
616 | LogFlow(("vusbDevStdReqGetStatus: %s: bogus interface status request!!\n", pDev->pUsbIns->pszName));
|
---|
617 | break;
|
---|
618 | case VUSB_TO_ENDPOINT:
|
---|
619 | u16Status = 0;
|
---|
620 | LogFlow(("vusbDevStdReqGetStatus: %s: bogus endpoint status request!!\n", pDev->pUsbIns->pszName));
|
---|
621 | break;
|
---|
622 | default:
|
---|
623 | AssertMsgFailed(("VUSB_TO_OTHER!\n"));
|
---|
624 | return false;
|
---|
625 | }
|
---|
626 |
|
---|
627 | *(uint16_t *)pbBuf = u16Status;
|
---|
628 | return true;
|
---|
629 | }
|
---|
630 |
|
---|
631 |
|
---|
632 | /**
|
---|
633 | * Finds a cached string.
|
---|
634 | *
|
---|
635 | * @returns Pointer to the cached string if found. NULL if not.
|
---|
636 | * @param paLanguages The languages to search.
|
---|
637 | * @param cLanguages The number of languages in the table.
|
---|
638 | * @param idLang The language ID.
|
---|
639 | * @param iString The string index.
|
---|
640 | */
|
---|
641 | static PCPDMUSBDESCCACHESTRING FindCachedString(PCPDMUSBDESCCACHELANG paLanguages, unsigned cLanguages,
|
---|
642 | uint16_t idLang, uint8_t iString)
|
---|
643 | {
|
---|
644 | /** @todo binary lookups! */
|
---|
645 | unsigned iCurLang = cLanguages;
|
---|
646 | while (iCurLang-- > 0)
|
---|
647 | if (paLanguages[iCurLang].idLang == idLang)
|
---|
648 | {
|
---|
649 | PCPDMUSBDESCCACHESTRING paStrings = paLanguages[iCurLang].paStrings;
|
---|
650 | unsigned iCurStr = paLanguages[iCurLang].cStrings;
|
---|
651 | while (iCurStr-- > 0)
|
---|
652 | if (paStrings[iCurStr].idx == iString)
|
---|
653 | return &paStrings[iCurStr];
|
---|
654 | break;
|
---|
655 | }
|
---|
656 | return NULL;
|
---|
657 | }
|
---|
658 |
|
---|
659 |
|
---|
660 | /** Macro for copying descriptor data. */
|
---|
661 | #define COPY_DATA(pbDst, cbLeft, pvSrc, cbSrc) \
|
---|
662 | do { \
|
---|
663 | uint32_t cbSrc_ = cbSrc; \
|
---|
664 | uint32_t cbCopy = RT_MIN(cbLeft, cbSrc_); \
|
---|
665 | memcpy(pbBuf, pvSrc, cbCopy); \
|
---|
666 | cbLeft -= cbCopy; \
|
---|
667 | if (!cbLeft) \
|
---|
668 | return; \
|
---|
669 | pbBuf += cbCopy; \
|
---|
670 | } while (0)
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Internal function for reading the language IDs.
|
---|
674 | */
|
---|
675 | static void ReadCachedStringDesc(PCPDMUSBDESCCACHESTRING pString, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
676 | {
|
---|
677 | uint32_t cbLeft = *pcbBuf;
|
---|
678 |
|
---|
679 | RTUTF16 wsz[128]; /* 128-1 => bLength=0xff */
|
---|
680 | PRTUTF16 pwsz = wsz;
|
---|
681 | size_t cwc;
|
---|
682 | int rc = RTStrToUtf16Ex(pString->psz, RT_ELEMENTS(wsz) - 1, &pwsz, RT_ELEMENTS(wsz), &cwc);
|
---|
683 | if (RT_FAILURE(rc))
|
---|
684 | {
|
---|
685 | AssertRC(rc);
|
---|
686 | wsz[0] = 'e';
|
---|
687 | wsz[1] = 'r';
|
---|
688 | wsz[2] = 'r';
|
---|
689 | cwc = 3;
|
---|
690 | }
|
---|
691 |
|
---|
692 | VUSBDESCSTRING StringDesc;
|
---|
693 | StringDesc.bLength = (uint8_t)(sizeof(StringDesc) + cwc * sizeof(RTUTF16));
|
---|
694 | StringDesc.bDescriptorType = VUSB_DT_STRING;
|
---|
695 | COPY_DATA(pbBuf, cbLeft, &StringDesc, sizeof(StringDesc));
|
---|
696 | COPY_DATA(pbBuf, cbLeft, wsz, (uint32_t)cwc * sizeof(RTUTF16));
|
---|
697 |
|
---|
698 | /* updated the size of the output buffer. */
|
---|
699 | *pcbBuf -= cbLeft;
|
---|
700 | }
|
---|
701 |
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * Internal function for reading the language IDs.
|
---|
705 | */
|
---|
706 | static void ReadCachedLangIdDesc(PCPDMUSBDESCCACHELANG paLanguages, unsigned cLanguages,
|
---|
707 | uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
708 | {
|
---|
709 | uint32_t cbLeft = *pcbBuf;
|
---|
710 |
|
---|
711 | VUSBDESCLANGID LangIdDesc;
|
---|
712 | size_t cbDesc = sizeof(LangIdDesc) + cLanguages * sizeof(paLanguages[0].idLang);
|
---|
713 | LangIdDesc.bLength = (uint8_t)RT_MIN(0xff, cbDesc);
|
---|
714 | LangIdDesc.bDescriptorType = VUSB_DT_STRING;
|
---|
715 | COPY_DATA(pbBuf, cbLeft, &LangIdDesc, sizeof(LangIdDesc));
|
---|
716 |
|
---|
717 | unsigned iLanguage = cLanguages;
|
---|
718 | while (iLanguage-- > 0)
|
---|
719 | COPY_DATA(pbBuf, cbLeft, &paLanguages[iLanguage].idLang, sizeof(paLanguages[iLanguage].idLang));
|
---|
720 |
|
---|
721 | /* updated the size of the output buffer. */
|
---|
722 | *pcbBuf -= cbLeft;
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | /**
|
---|
727 | * Internal function which performs a descriptor read on the cached descriptors.
|
---|
728 | */
|
---|
729 | static void ReadCachedConfigDesc(PCVUSBDESCCONFIGEX pCfgDesc, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
730 | {
|
---|
731 | uint32_t cbLeft = *pcbBuf;
|
---|
732 |
|
---|
733 | /** @todo See @bugref{2693} */
|
---|
734 | /*
|
---|
735 | * Make a copy of the config descriptor and calculate the wTotalLength field.
|
---|
736 | */
|
---|
737 | VUSBDESCCONFIG CfgDesc;
|
---|
738 | memcpy(&CfgDesc, pCfgDesc, VUSB_DT_CONFIG_MIN_LEN);
|
---|
739 | uint32_t cbTotal = pCfgDesc->Core.bLength;
|
---|
740 | for (unsigned i = 0; i < pCfgDesc->Core.bNumInterfaces; i++)
|
---|
741 | {
|
---|
742 | PCVUSBINTERFACE pIf = &pCfgDesc->paIfs[i];
|
---|
743 | for (uint32_t j = 0; j < pIf->cSettings; j++)
|
---|
744 | {
|
---|
745 | cbTotal += pIf->paSettings[j].cbIAD;
|
---|
746 | cbTotal += pIf->paSettings[j].Core.bLength;
|
---|
747 | cbTotal += pIf->paSettings[j].cbClass;
|
---|
748 | for (unsigned k = 0; k < pIf->paSettings[j].Core.bNumEndpoints; k++)
|
---|
749 | {
|
---|
750 | cbTotal += pIf->paSettings[j].paEndpoints[k].Core.bLength;
|
---|
751 | cbTotal += pIf->paSettings[j].paEndpoints[k].cbSsepc;
|
---|
752 | cbTotal += pIf->paSettings[j].paEndpoints[k].cbClass;
|
---|
753 | }
|
---|
754 | }
|
---|
755 | }
|
---|
756 | CfgDesc.wTotalLength = RT_H2LE_U16(cbTotal);
|
---|
757 |
|
---|
758 | /*
|
---|
759 | * Copy the config descriptor
|
---|
760 | */
|
---|
761 | COPY_DATA(pbBuf, cbLeft, &CfgDesc, VUSB_DT_CONFIG_MIN_LEN);
|
---|
762 | COPY_DATA(pbBuf, cbLeft, pCfgDesc->pvMore, pCfgDesc->Core.bLength - VUSB_DT_CONFIG_MIN_LEN);
|
---|
763 |
|
---|
764 | /*
|
---|
765 | * Copy out all the interfaces for this configuration
|
---|
766 | */
|
---|
767 | for (unsigned i = 0; i < pCfgDesc->Core.bNumInterfaces; i++)
|
---|
768 | {
|
---|
769 | PCVUSBINTERFACE pIf = &pCfgDesc->paIfs[i];
|
---|
770 | for (uint32_t j = 0; j < pIf->cSettings; j++)
|
---|
771 | {
|
---|
772 | PCVUSBDESCINTERFACEEX pIfDesc = &pIf->paSettings[j];
|
---|
773 |
|
---|
774 | COPY_DATA(pbBuf, cbLeft, pIfDesc->pIAD, pIfDesc->cbIAD);
|
---|
775 | COPY_DATA(pbBuf, cbLeft, pIfDesc, VUSB_DT_INTERFACE_MIN_LEN);
|
---|
776 | COPY_DATA(pbBuf, cbLeft, pIfDesc->pvMore, pIfDesc->Core.bLength - VUSB_DT_INTERFACE_MIN_LEN);
|
---|
777 | COPY_DATA(pbBuf, cbLeft, pIfDesc->pvClass, pIfDesc->cbClass);
|
---|
778 |
|
---|
779 | /*
|
---|
780 | * Copy out all the endpoints for this interface
|
---|
781 | */
|
---|
782 | for (unsigned k = 0; k < pIfDesc->Core.bNumEndpoints; k++)
|
---|
783 | {
|
---|
784 | VUSBDESCENDPOINT EndPtDesc;
|
---|
785 | memcpy(&EndPtDesc, &pIfDesc->paEndpoints[k], VUSB_DT_ENDPOINT_MIN_LEN);
|
---|
786 | EndPtDesc.wMaxPacketSize = RT_H2LE_U16(EndPtDesc.wMaxPacketSize);
|
---|
787 |
|
---|
788 | COPY_DATA(pbBuf, cbLeft, &EndPtDesc, VUSB_DT_ENDPOINT_MIN_LEN);
|
---|
789 | COPY_DATA(pbBuf, cbLeft, pIfDesc->paEndpoints[k].pvMore, EndPtDesc.bLength - VUSB_DT_ENDPOINT_MIN_LEN);
|
---|
790 | COPY_DATA(pbBuf, cbLeft, pIfDesc->paEndpoints[k].pvSsepc, pIfDesc->paEndpoints[k].cbSsepc);
|
---|
791 | COPY_DATA(pbBuf, cbLeft, pIfDesc->paEndpoints[k].pvClass, pIfDesc->paEndpoints[k].cbClass);
|
---|
792 | }
|
---|
793 | }
|
---|
794 | }
|
---|
795 |
|
---|
796 | /* updated the size of the output buffer. */
|
---|
797 | *pcbBuf -= cbLeft;
|
---|
798 | }
|
---|
799 |
|
---|
800 | /**
|
---|
801 | * Internal function which performs a descriptor read on the cached descriptors.
|
---|
802 | */
|
---|
803 | static void ReadCachedDeviceDesc(PCVUSBDESCDEVICE pDevDesc, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
804 | {
|
---|
805 | uint32_t cbLeft = *pcbBuf;
|
---|
806 |
|
---|
807 | /*
|
---|
808 | * Duplicate the device description and update some fields we keep in cpu type.
|
---|
809 | */
|
---|
810 | Assert(sizeof(VUSBDESCDEVICE) == 18);
|
---|
811 | VUSBDESCDEVICE DevDesc = *pDevDesc;
|
---|
812 | DevDesc.bcdUSB = RT_H2LE_U16(DevDesc.bcdUSB);
|
---|
813 | DevDesc.idVendor = RT_H2LE_U16(DevDesc.idVendor);
|
---|
814 | DevDesc.idProduct = RT_H2LE_U16(DevDesc.idProduct);
|
---|
815 | DevDesc.bcdDevice = RT_H2LE_U16(DevDesc.bcdDevice);
|
---|
816 |
|
---|
817 | COPY_DATA(pbBuf, cbLeft, &DevDesc, sizeof(DevDesc));
|
---|
818 | COPY_DATA(pbBuf, cbLeft, pDevDesc + 1, pDevDesc->bLength - sizeof(DevDesc));
|
---|
819 |
|
---|
820 | /* updated the size of the output buffer. */
|
---|
821 | *pcbBuf -= cbLeft;
|
---|
822 | }
|
---|
823 |
|
---|
824 | #undef COPY_DATA
|
---|
825 |
|
---|
826 | /**
|
---|
827 | * Standard device request: GET_DESCRIPTOR
|
---|
828 | * @returns success indicator.
|
---|
829 | * @remark not really used yet as we consider GET_DESCRIPTOR 'safe'.
|
---|
830 | */
|
---|
831 | static bool vusbDevStdReqGetDescriptor(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, uint8_t *pbBuf, uint32_t *pcbBuf)
|
---|
832 | {
|
---|
833 | if ((pSetup->bmRequestType & VUSB_RECIP_MASK) == VUSB_TO_DEVICE)
|
---|
834 | {
|
---|
835 | switch (pSetup->wValue >> 8)
|
---|
836 | {
|
---|
837 | case VUSB_DT_DEVICE:
|
---|
838 | ReadCachedDeviceDesc(pDev->pDescCache->pDevice, pbBuf, pcbBuf);
|
---|
839 | LogFlow(("vusbDevStdReqGetDescriptor: %s: %u bytes of device descriptors\n", pDev->pUsbIns->pszName, *pcbBuf));
|
---|
840 | return true;
|
---|
841 |
|
---|
842 | case VUSB_DT_CONFIG:
|
---|
843 | {
|
---|
844 | unsigned int iIndex = (pSetup->wValue & 0xff);
|
---|
845 | if (iIndex >= pDev->pDescCache->pDevice->bNumConfigurations)
|
---|
846 | {
|
---|
847 | LogFlow(("vusbDevStdReqGetDescriptor: %s: iIndex=%p >= bNumConfigurations=%d !!!\n",
|
---|
848 | pDev->pUsbIns->pszName, iIndex, pDev->pDescCache->pDevice->bNumConfigurations));
|
---|
849 | return false;
|
---|
850 | }
|
---|
851 | ReadCachedConfigDesc(&pDev->pDescCache->paConfigs[iIndex], pbBuf, pcbBuf);
|
---|
852 | LogFlow(("vusbDevStdReqGetDescriptor: %s: %u bytes of config descriptors\n", pDev->pUsbIns->pszName, *pcbBuf));
|
---|
853 | return true;
|
---|
854 | }
|
---|
855 |
|
---|
856 | case VUSB_DT_STRING:
|
---|
857 | {
|
---|
858 | if (pSetup->wIndex == 0)
|
---|
859 | {
|
---|
860 | ReadCachedLangIdDesc(pDev->pDescCache->paLanguages, pDev->pDescCache->cLanguages, pbBuf, pcbBuf);
|
---|
861 | LogFlow(("vusbDevStdReqGetDescriptor: %s: %u bytes of language ID (string) descriptors\n", pDev->pUsbIns->pszName, *pcbBuf));
|
---|
862 | return true;
|
---|
863 | }
|
---|
864 | PCPDMUSBDESCCACHESTRING pString;
|
---|
865 | pString = FindCachedString(pDev->pDescCache->paLanguages, pDev->pDescCache->cLanguages,
|
---|
866 | pSetup->wIndex, pSetup->wValue & 0xff);
|
---|
867 | if (pString)
|
---|
868 | {
|
---|
869 | ReadCachedStringDesc(pString, pbBuf, pcbBuf);
|
---|
870 | LogFlow(("vusbDevStdReqGetDescriptor: %s: %u bytes of string descriptors \"%s\"\n",
|
---|
871 | pDev->pUsbIns->pszName, *pcbBuf, pString->psz));
|
---|
872 | return true;
|
---|
873 | }
|
---|
874 | break;
|
---|
875 | }
|
---|
876 |
|
---|
877 | default:
|
---|
878 | break;
|
---|
879 | }
|
---|
880 | }
|
---|
881 | Log(("vusb: %s: warning: unknown descriptor: type=%u descidx=%u lang=%u len=%u!!!\n",
|
---|
882 | pDev->pUsbIns->pszName, pSetup->wValue >> 8, pSetup->wValue & 0xff, pSetup->wIndex, pSetup->wLength));
|
---|
883 | return false;
|
---|
884 | }
|
---|
885 |
|
---|
886 |
|
---|
887 | /**
|
---|
888 | * Service the standard USB requests.
|
---|
889 | *
|
---|
890 | * Devices may call this from controlmsg() if you want vusb core to handle your standard
|
---|
891 | * request, it's not necessary - you could handle them manually
|
---|
892 | *
|
---|
893 | * @param pDev The device.
|
---|
894 | * @param EndPoint The endpoint.
|
---|
895 | * @param pSetup Pointer to the setup request structure.
|
---|
896 | * @param pvBuf Buffer?
|
---|
897 | * @param pcbBuf ?
|
---|
898 | */
|
---|
899 | bool vusbDevStandardRequest(PVUSBDEV pDev, int EndPoint, PVUSBSETUP pSetup, void *pvBuf, uint32_t *pcbBuf)
|
---|
900 | {
|
---|
901 | static bool (* const s_apfnStdReq[VUSB_REQ_MAX])(PVUSBDEV, int, PVUSBSETUP, uint8_t *, uint32_t *) =
|
---|
902 | {
|
---|
903 | vusbDevStdReqGetStatus,
|
---|
904 | vusbDevStdReqClearFeature,
|
---|
905 | NULL,
|
---|
906 | vusbDevStdReqSetFeature,
|
---|
907 | NULL,
|
---|
908 | vusbDevStdReqSetAddress,
|
---|
909 | vusbDevStdReqGetDescriptor,
|
---|
910 | NULL,
|
---|
911 | vusbDevStdReqGetConfig,
|
---|
912 | vusbDevStdReqSetConfig,
|
---|
913 | vusbDevStdReqGetInterface,
|
---|
914 | vusbDevStdReqSetInterface,
|
---|
915 | NULL /* for iso */
|
---|
916 | };
|
---|
917 |
|
---|
918 | /*
|
---|
919 | * Check that the device is in a valid state.
|
---|
920 | */
|
---|
921 | const VUSBDEVICESTATE enmState = vusbDevGetState(pDev);
|
---|
922 | if (enmState == VUSB_DEVICE_STATE_RESET)
|
---|
923 | {
|
---|
924 | LogRel(("VUSB: %s: standard control message ignored, the device is resetting\n", pDev->pUsbIns->pszName));
|
---|
925 | return false;
|
---|
926 | }
|
---|
927 |
|
---|
928 | /*
|
---|
929 | * Do the request if it's one we want to deal with.
|
---|
930 | */
|
---|
931 | if ( pSetup->bRequest >= VUSB_REQ_MAX
|
---|
932 | || !s_apfnStdReq[pSetup->bRequest])
|
---|
933 | {
|
---|
934 | Log(("vusb: warning: standard req not implemented: message %u: val=%u idx=%u len=%u !!!\n",
|
---|
935 | pSetup->bRequest, pSetup->wValue, pSetup->wIndex, pSetup->wLength));
|
---|
936 | return false;
|
---|
937 | }
|
---|
938 |
|
---|
939 | return s_apfnStdReq[pSetup->bRequest](pDev, EndPoint, pSetup, (uint8_t *)pvBuf, pcbBuf);
|
---|
940 | }
|
---|
941 |
|
---|
942 |
|
---|
943 | /**
|
---|
944 | * Add a device to the address hash
|
---|
945 | */
|
---|
946 | static void vusbDevAddressHash(PVUSBDEV pDev)
|
---|
947 | {
|
---|
948 | if (pDev->u8Address == VUSB_INVALID_ADDRESS)
|
---|
949 | return;
|
---|
950 | uint8_t u8Hash = vusbHashAddress(pDev->u8Address);
|
---|
951 | pDev->pNextHash = pDev->pHub->pRootHub->apAddrHash[u8Hash];
|
---|
952 | pDev->pHub->pRootHub->apAddrHash[u8Hash] = pDev;
|
---|
953 | }
|
---|
954 |
|
---|
955 | /**
|
---|
956 | * Remove a device from the address hash
|
---|
957 | */
|
---|
958 | static void vusbDevAddressUnHash(PVUSBDEV pDev)
|
---|
959 | {
|
---|
960 | if (pDev->u8Address == VUSB_INVALID_ADDRESS)
|
---|
961 | return;
|
---|
962 |
|
---|
963 | uint8_t u8Hash = vusbHashAddress(pDev->u8Address);
|
---|
964 | pDev->u8Address = VUSB_INVALID_ADDRESS;
|
---|
965 | pDev->u8NewAddress = VUSB_INVALID_ADDRESS;
|
---|
966 |
|
---|
967 | PVUSBDEV pCur = pDev->pHub->pRootHub->apAddrHash[u8Hash];
|
---|
968 | if (pCur == pDev)
|
---|
969 | {
|
---|
970 | /* special case, we're at the head */
|
---|
971 | pDev->pHub->pRootHub->apAddrHash[u8Hash] = pDev->pNextHash;
|
---|
972 | pDev->pNextHash = NULL;
|
---|
973 | }
|
---|
974 | else
|
---|
975 | {
|
---|
976 | /* search the list */
|
---|
977 | PVUSBDEV pPrev;
|
---|
978 | for (pPrev = pCur, pCur = pCur->pNextHash;
|
---|
979 | pCur;
|
---|
980 | pPrev = pCur, pCur = pCur->pNextHash)
|
---|
981 | {
|
---|
982 | if (pCur == pDev)
|
---|
983 | {
|
---|
984 | pPrev->pNextHash = pCur->pNextHash;
|
---|
985 | pDev->pNextHash = NULL;
|
---|
986 | break;
|
---|
987 | }
|
---|
988 | }
|
---|
989 | }
|
---|
990 | }
|
---|
991 |
|
---|
992 | /**
|
---|
993 | * Sets the address of a device.
|
---|
994 | *
|
---|
995 | * Called by status_completion() and vusbDevResetWorker().
|
---|
996 | */
|
---|
997 | void vusbDevSetAddress(PVUSBDEV pDev, uint8_t u8Address)
|
---|
998 | {
|
---|
999 | LogFlow(("vusbDevSetAddress: pDev=%p[%s]/%i u8Address=%#x\n",
|
---|
1000 | pDev, pDev->pUsbIns->pszName, pDev->i16Port, u8Address));
|
---|
1001 |
|
---|
1002 | /*
|
---|
1003 | * Check that the device is in a valid state.
|
---|
1004 | */
|
---|
1005 | const VUSBDEVICESTATE enmState = vusbDevGetState(pDev);
|
---|
1006 | VUSBDEV_ASSERT_VALID_STATE(enmState);
|
---|
1007 | if ( enmState == VUSB_DEVICE_STATE_ATTACHED
|
---|
1008 | || enmState == VUSB_DEVICE_STATE_DETACHED)
|
---|
1009 | {
|
---|
1010 | LogFlow(("vusbDevSetAddress: %s: fails because %d < POWERED\n", pDev->pUsbIns->pszName, pDev->enmState));
|
---|
1011 | return;
|
---|
1012 | }
|
---|
1013 | if (enmState == VUSB_DEVICE_STATE_RESET)
|
---|
1014 | {
|
---|
1015 | LogRel(("VUSB: %s: set address ignored, the device is resetting\n", pDev->pUsbIns->pszName));
|
---|
1016 | return;
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /*
|
---|
1020 | * Ok, get on with it.
|
---|
1021 | */
|
---|
1022 | if (pDev->u8Address == u8Address)
|
---|
1023 | return;
|
---|
1024 |
|
---|
1025 | PVUSBROOTHUB pRh = vusbDevGetRh(pDev);
|
---|
1026 | AssertPtrReturnVoid(pRh);
|
---|
1027 | if (pDev->u8Address == VUSB_DEFAULT_ADDRESS)
|
---|
1028 | pRh->pDefaultAddress = NULL;
|
---|
1029 |
|
---|
1030 | vusbDevAddressUnHash(pDev);
|
---|
1031 |
|
---|
1032 | if (u8Address == VUSB_DEFAULT_ADDRESS)
|
---|
1033 | {
|
---|
1034 | if (pRh->pDefaultAddress != NULL)
|
---|
1035 | {
|
---|
1036 | vusbDevAddressUnHash(pRh->pDefaultAddress);
|
---|
1037 | vusbDevSetStateCmp(pRh->pDefaultAddress, VUSB_DEVICE_STATE_POWERED, VUSB_DEVICE_STATE_DEFAULT);
|
---|
1038 | Log(("2 DEFAULT ADDRS\n"));
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | pRh->pDefaultAddress = pDev;
|
---|
1042 | vusbDevSetState(pDev, VUSB_DEVICE_STATE_DEFAULT);
|
---|
1043 | }
|
---|
1044 | else
|
---|
1045 | vusbDevSetState(pDev, VUSB_DEVICE_STATE_ADDRESS);
|
---|
1046 |
|
---|
1047 | pDev->u8Address = u8Address;
|
---|
1048 | vusbDevAddressHash(pDev);
|
---|
1049 |
|
---|
1050 | Log(("vusb: %p[%s]/%i: Assigned address %u\n",
|
---|
1051 | pDev, pDev->pUsbIns->pszName, pDev->i16Port, u8Address));
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 |
|
---|
1055 | static DECLCALLBACK(int) vusbDevCancelAllUrbsWorker(PVUSBDEV pDev, bool fDetaching)
|
---|
1056 | {
|
---|
1057 | /*
|
---|
1058 | * Iterate the URBs and cancel them.
|
---|
1059 | */
|
---|
1060 | PVUSBURBVUSB pVUsbUrb, pVUsbUrbNext;
|
---|
1061 | RTListForEachSafe(&pDev->LstAsyncUrbs, pVUsbUrb, pVUsbUrbNext, VUSBURBVUSBINT, NdLst)
|
---|
1062 | {
|
---|
1063 | PVUSBURB pUrb = pVUsbUrb->pUrb;
|
---|
1064 |
|
---|
1065 | Assert(pUrb->pVUsb->pDev == pDev);
|
---|
1066 |
|
---|
1067 | LogFlow(("%s: vusbDevCancelAllUrbs: CANCELING URB\n", pUrb->pszDesc));
|
---|
1068 | int rc = vusbUrbCancelWorker(pUrb, CANCELMODE_FAIL);
|
---|
1069 | AssertRC(rc);
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | /*
|
---|
1073 | * Reap any URBs which became ripe during cancel now.
|
---|
1074 | */
|
---|
1075 | RTCritSectEnter(&pDev->CritSectAsyncUrbs);
|
---|
1076 | unsigned cReaped;
|
---|
1077 | do
|
---|
1078 | {
|
---|
1079 | cReaped = 0;
|
---|
1080 | pVUsbUrb = RTListGetFirst(&pDev->LstAsyncUrbs, VUSBURBVUSBINT, NdLst);
|
---|
1081 | while (pVUsbUrb)
|
---|
1082 | {
|
---|
1083 | PVUSBURBVUSB pNext = RTListGetNext(&pDev->LstAsyncUrbs, pVUsbUrb, VUSBURBVUSBINT, NdLst);
|
---|
1084 | PVUSBURB pUrb = pVUsbUrb->pUrb;
|
---|
1085 | Assert(pUrb->pVUsb->pDev == pDev);
|
---|
1086 |
|
---|
1087 | PVUSBURB pRipe = NULL;
|
---|
1088 | if (pUrb->enmState == VUSBURBSTATE_REAPED)
|
---|
1089 | pRipe = pUrb;
|
---|
1090 | else if (pUrb->enmState == VUSBURBSTATE_CANCELLED)
|
---|
1091 | #ifdef RT_OS_WINDOWS /** @todo Windows doesn't do cancelling, thus this kludge to prevent really bad
|
---|
1092 | * things from happening if we leave a pending URB behinds. */
|
---|
1093 | pRipe = pDev->pUsbIns->pReg->pfnUrbReap(pDev->pUsbIns, fDetaching ? 1500 : 0 /*ms*/);
|
---|
1094 | #else
|
---|
1095 | pRipe = pDev->pUsbIns->pReg->pfnUrbReap(pDev->pUsbIns, fDetaching ? 10 : 0 /*ms*/);
|
---|
1096 | #endif
|
---|
1097 | else
|
---|
1098 | AssertMsgFailed(("pUrb=%p enmState=%d\n", pUrb, pUrb->enmState));
|
---|
1099 | if (pRipe)
|
---|
1100 | {
|
---|
1101 | if ( pNext
|
---|
1102 | && pRipe == pNext->pUrb)
|
---|
1103 | pNext = RTListGetNext(&pDev->LstAsyncUrbs, pNext, VUSBURBVUSBINT, NdLst);
|
---|
1104 | vusbUrbRipe(pRipe);
|
---|
1105 | cReaped++;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | pVUsbUrb = pNext;
|
---|
1109 | }
|
---|
1110 | } while (cReaped > 0);
|
---|
1111 |
|
---|
1112 | /*
|
---|
1113 | * If we're detaching, we'll have to orphan any leftover URBs.
|
---|
1114 | */
|
---|
1115 | if (fDetaching)
|
---|
1116 | {
|
---|
1117 | RTListForEachSafe(&pDev->LstAsyncUrbs, pVUsbUrb, pVUsbUrbNext, VUSBURBVUSBINT, NdLst)
|
---|
1118 | {
|
---|
1119 | PVUSBURB pUrb = pVUsbUrb->pUrb;
|
---|
1120 | Assert(pUrb->pVUsb->pDev == pDev);
|
---|
1121 |
|
---|
1122 | AssertMsgFailed(("%s: Leaking left over URB! state=%d pDev=%p[%s]\n",
|
---|
1123 | pUrb->pszDesc, pUrb->enmState, pDev, pDev->pUsbIns->pszName));
|
---|
1124 | vusbUrbUnlink(pUrb);
|
---|
1125 | /* Unlink isn't enough, because boundary timer and detaching will try to reap it.
|
---|
1126 | * It was tested with MSD & iphone attachment to vSMP guest, if
|
---|
1127 | * it breaks anything, please add comment here, why we should unlink only.
|
---|
1128 | */
|
---|
1129 | pUrb->pVUsb->pfnFree(pUrb);
|
---|
1130 | }
|
---|
1131 | }
|
---|
1132 | RTCritSectLeave(&pDev->CritSectAsyncUrbs);
|
---|
1133 | return VINF_SUCCESS;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | /**
|
---|
1137 | * Cancels and completes (with CRC failure) all async URBs pending
|
---|
1138 | * on a device. This is typically done as part of a reset and
|
---|
1139 | * before detaching a device.
|
---|
1140 | *
|
---|
1141 | * @param fDetaching If set, we will unconditionally unlink (and leak)
|
---|
1142 | * any URBs which isn't reaped.
|
---|
1143 | */
|
---|
1144 | DECLHIDDEN(void) vusbDevCancelAllUrbs(PVUSBDEV pDev, bool fDetaching)
|
---|
1145 | {
|
---|
1146 | int rc = vusbDevIoThreadExecSync(pDev, (PFNRT)vusbDevCancelAllUrbsWorker, 2, pDev, fDetaching);
|
---|
1147 | AssertRC(rc);
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 |
|
---|
1151 | static DECLCALLBACK(int) vusbDevUrbIoThread(RTTHREAD hThread, void *pvUser)
|
---|
1152 | {
|
---|
1153 | PVUSBDEV pDev = (PVUSBDEV)pvUser;
|
---|
1154 |
|
---|
1155 | /* Notify the starter that we are up and running. */
|
---|
1156 | RTThreadUserSignal(hThread);
|
---|
1157 |
|
---|
1158 | LogFlowFunc(("Entering work loop\n"));
|
---|
1159 |
|
---|
1160 | while (!ASMAtomicReadBool(&pDev->fTerminate))
|
---|
1161 | {
|
---|
1162 | if (vusbDevGetState(pDev) != VUSB_DEVICE_STATE_RESET)
|
---|
1163 | vusbUrbDoReapAsyncDev(pDev, RT_INDEFINITE_WAIT);
|
---|
1164 |
|
---|
1165 | /* Process any URBs waiting to be cancelled first. */
|
---|
1166 | int rc = RTReqQueueProcess(pDev->hReqQueueSync, 0); /* Don't wait if there is nothing to do. */
|
---|
1167 | Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | return VINF_SUCCESS;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | int vusbDevUrbIoThreadWakeup(PVUSBDEV pDev)
|
---|
1174 | {
|
---|
1175 | ASMAtomicXchgBool(&pDev->fWokenUp, true);
|
---|
1176 | return pDev->pUsbIns->pReg->pfnWakeup(pDev->pUsbIns);
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | /**
|
---|
1180 | * Create the URB I/O thread.
|
---|
1181 | *
|
---|
1182 | * @returns VBox status code.
|
---|
1183 | * @param pDev The VUSB device.
|
---|
1184 | */
|
---|
1185 | int vusbDevUrbIoThreadCreate(PVUSBDEV pDev)
|
---|
1186 | {
|
---|
1187 | int rc = VINF_SUCCESS;
|
---|
1188 |
|
---|
1189 | ASMAtomicXchgBool(&pDev->fTerminate, false);
|
---|
1190 | rc = RTThreadCreateF(&pDev->hUrbIoThread, vusbDevUrbIoThread, pDev, 0, RTTHREADTYPE_IO,
|
---|
1191 | RTTHREADFLAGS_WAITABLE, "USBDevIo-%d", pDev->i16Port);
|
---|
1192 | if (RT_SUCCESS(rc))
|
---|
1193 | {
|
---|
1194 | /* Wait for it to become active. */
|
---|
1195 | rc = RTThreadUserWait(pDev->hUrbIoThread, RT_INDEFINITE_WAIT);
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | return rc;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | /**
|
---|
1202 | * Destro the URB I/O thread.
|
---|
1203 | *
|
---|
1204 | * @returns VBox status code.
|
---|
1205 | * @param pDev The VUSB device.
|
---|
1206 | */
|
---|
1207 | int vusbDevUrbIoThreadDestroy(PVUSBDEV pDev)
|
---|
1208 | {
|
---|
1209 | int rc = VINF_SUCCESS;
|
---|
1210 | int rcThread = VINF_SUCCESS;
|
---|
1211 |
|
---|
1212 | ASMAtomicXchgBool(&pDev->fTerminate, true);
|
---|
1213 | vusbDevUrbIoThreadWakeup(pDev);
|
---|
1214 |
|
---|
1215 | rc = RTThreadWait(pDev->hUrbIoThread, RT_INDEFINITE_WAIT, &rcThread);
|
---|
1216 | if (RT_SUCCESS(rc))
|
---|
1217 | rc = rcThread;
|
---|
1218 |
|
---|
1219 | pDev->hUrbIoThread = NIL_RTTHREAD;
|
---|
1220 |
|
---|
1221 | return rc;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 |
|
---|
1225 | /**
|
---|
1226 | * Detaches a device from the hub it's attached to.
|
---|
1227 | *
|
---|
1228 | * @returns VBox status code.
|
---|
1229 | * @param pDev The device to detach.
|
---|
1230 | *
|
---|
1231 | * @remark This can be called in any state but reset.
|
---|
1232 | */
|
---|
1233 | int vusbDevDetach(PVUSBDEV pDev)
|
---|
1234 | {
|
---|
1235 | LogFlow(("vusbDevDetach: pDev=%p[%s] enmState=%#x\n", pDev, pDev->pUsbIns->pszName, pDev->enmState));
|
---|
1236 | VUSBDEV_ASSERT_VALID_STATE(pDev->enmState);
|
---|
1237 | Assert(pDev->enmState != VUSB_DEVICE_STATE_RESET);
|
---|
1238 |
|
---|
1239 | vusbDevCancelAllUrbs(pDev, true);
|
---|
1240 | vusbDevAddressUnHash(pDev);
|
---|
1241 |
|
---|
1242 | PVUSBROOTHUB pRh = vusbDevGetRh(pDev);
|
---|
1243 | if (!pRh)
|
---|
1244 | AssertMsgFailedReturn(("Not attached!\n"), VERR_VUSB_DEVICE_NOT_ATTACHED);
|
---|
1245 | if (pRh->pDefaultAddress == pDev)
|
---|
1246 | pRh->pDefaultAddress = NULL;
|
---|
1247 |
|
---|
1248 | pDev->pHub->pOps->pfnDetach(pDev->pHub, pDev);
|
---|
1249 | pDev->i16Port = -1;
|
---|
1250 | vusbDevSetState(pDev, VUSB_DEVICE_STATE_DETACHED);
|
---|
1251 | pDev->pHub = NULL;
|
---|
1252 |
|
---|
1253 | /* Remove the configuration */
|
---|
1254 | pDev->pCurCfgDesc = NULL;
|
---|
1255 | for (unsigned i = 0; i < RT_ELEMENTS(pDev->aPipes); i++)
|
---|
1256 | vusbDevResetPipeData(&pDev->aPipes[i]);
|
---|
1257 | return VINF_SUCCESS;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 |
|
---|
1261 | /**
|
---|
1262 | * Destroys a device, detaching it from the hub if necessary.
|
---|
1263 | *
|
---|
1264 | * @param pDev The device.
|
---|
1265 | * @thread EMT
|
---|
1266 | */
|
---|
1267 | void vusbDevDestroy(PVUSBDEV pDev)
|
---|
1268 | {
|
---|
1269 | LogFlow(("vusbDevDestroy: pDev=%p[%s] enmState=%d\n", pDev, pDev->pUsbIns->pszName, pDev->enmState));
|
---|
1270 |
|
---|
1271 | /*
|
---|
1272 | * Deal with pending async reset.
|
---|
1273 | * (anything but reset)
|
---|
1274 | */
|
---|
1275 | vusbDevSetStateCmp(pDev, VUSB_DEVICE_STATE_DEFAULT, VUSB_DEVICE_STATE_RESET);
|
---|
1276 |
|
---|
1277 | /*
|
---|
1278 | * Detach and free resources.
|
---|
1279 | */
|
---|
1280 | if (pDev->pHub)
|
---|
1281 | vusbDevDetach(pDev);
|
---|
1282 | RTMemFree(pDev->paIfStates);
|
---|
1283 | TMR3TimerDestroy(pDev->pResetTimer);
|
---|
1284 | pDev->pResetTimer = NULL;
|
---|
1285 | for (unsigned i = 0; i < RT_ELEMENTS(pDev->aPipes); i++)
|
---|
1286 | {
|
---|
1287 | Assert(pDev->aPipes[i].pCtrl == NULL);
|
---|
1288 | RTCritSectDelete(&pDev->aPipes[i].CritSectCtrl);
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 | /*
|
---|
1292 | * Destroy I/O thread and request queue last because they might still be used
|
---|
1293 | * when cancelling URBs.
|
---|
1294 | */
|
---|
1295 | vusbDevUrbIoThreadDestroy(pDev);
|
---|
1296 |
|
---|
1297 | int rc = RTReqQueueDestroy(pDev->hReqQueueSync);
|
---|
1298 | AssertRC(rc);
|
---|
1299 |
|
---|
1300 | if (pDev->hSniffer != VUSBSNIFFER_NIL)
|
---|
1301 | VUSBSnifferDestroy(pDev->hSniffer);
|
---|
1302 |
|
---|
1303 | vusbUrbPoolDestroy(&pDev->UrbPool);
|
---|
1304 |
|
---|
1305 | RTCritSectDelete(&pDev->CritSectAsyncUrbs);
|
---|
1306 | /* Not using vusbDevSetState() deliberately here because it would assert on the state. */
|
---|
1307 | pDev->enmState = VUSB_DEVICE_STATE_DESTROYED;
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 |
|
---|
1311 | /* -=-=-=-=-=- VUSBIDEVICE methods -=-=-=-=-=- */
|
---|
1312 |
|
---|
1313 |
|
---|
1314 | /**
|
---|
1315 | * The actual reset has been done, do completion on EMT.
|
---|
1316 | *
|
---|
1317 | * There are several things we have to do now, like set default
|
---|
1318 | * config and address, and cleanup the state of control pipes.
|
---|
1319 | *
|
---|
1320 | * It's possible that the device has a delayed destroy request
|
---|
1321 | * pending when we get here. This can happen for async resetting.
|
---|
1322 | * We deal with it here, since we're now executing on the EMT
|
---|
1323 | * thread and the destruction will be properly serialized now.
|
---|
1324 | *
|
---|
1325 | * @param pDev The device that is being reset.
|
---|
1326 | * @param rc The vusbDevResetWorker return code.
|
---|
1327 | * @param pfnDone The done callback specified by the caller of vusbDevReset().
|
---|
1328 | * @param pvUser The user argument for the callback.
|
---|
1329 | */
|
---|
1330 | static void vusbDevResetDone(PVUSBDEV pDev, int rc, PFNVUSBRESETDONE pfnDone, void *pvUser)
|
---|
1331 | {
|
---|
1332 | VUSBDEV_ASSERT_VALID_STATE(pDev->enmState);
|
---|
1333 | Assert(pDev->enmState == VUSB_DEVICE_STATE_RESET);
|
---|
1334 |
|
---|
1335 | /*
|
---|
1336 | * Do control pipe cleanup regardless of state and result.
|
---|
1337 | */
|
---|
1338 | for (unsigned i = 0; i < VUSB_PIPE_MAX; i++)
|
---|
1339 | if (pDev->aPipes[i].pCtrl)
|
---|
1340 | vusbMsgResetExtraData(pDev->aPipes[i].pCtrl);
|
---|
1341 |
|
---|
1342 | /*
|
---|
1343 | * Switch to the default state.
|
---|
1344 | */
|
---|
1345 | vusbDevSetState(pDev, VUSB_DEVICE_STATE_DEFAULT);
|
---|
1346 | pDev->u16Status = 0;
|
---|
1347 | vusbDevDoSelectConfig(pDev, &g_Config0);
|
---|
1348 | if (!vusbDevIsRh(pDev))
|
---|
1349 | vusbDevSetAddress(pDev, VUSB_DEFAULT_ADDRESS);
|
---|
1350 | if (pfnDone)
|
---|
1351 | pfnDone(&pDev->IDevice, rc, pvUser);
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 |
|
---|
1355 | /**
|
---|
1356 | * Timer callback for doing reset completion.
|
---|
1357 | *
|
---|
1358 | * @param pUsbIns The USB device instance.
|
---|
1359 | * @param pTimer The timer instance.
|
---|
1360 | * @param pvUser The VUSB device data.
|
---|
1361 | * @thread EMT
|
---|
1362 | */
|
---|
1363 | static DECLCALLBACK(void) vusbDevResetDoneTimer(PPDMUSBINS pUsbIns, PTMTIMER pTimer, void *pvUser)
|
---|
1364 | {
|
---|
1365 | PVUSBDEV pDev = (PVUSBDEV)pvUser;
|
---|
1366 | PVUSBRESETARGS pArgs = (PVUSBRESETARGS)pDev->pvArgs;
|
---|
1367 | Assert(pDev->pUsbIns == pUsbIns);
|
---|
1368 |
|
---|
1369 | AssertPtr(pArgs);
|
---|
1370 |
|
---|
1371 | /*
|
---|
1372 | * Reset-done processing and cleanup.
|
---|
1373 | */
|
---|
1374 | pDev->pvArgs = NULL;
|
---|
1375 | vusbDevResetDone(pDev, pArgs->rc, pArgs->pfnDone, pArgs->pvUser);
|
---|
1376 | RTMemFree(pArgs);
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 |
|
---|
1380 | /**
|
---|
1381 | * Perform the actual reset.
|
---|
1382 | *
|
---|
1383 | * @thread EMT or a VUSB reset thread.
|
---|
1384 | */
|
---|
1385 | static int vusbDevResetWorker(PVUSBDEV pDev, bool fResetOnLinux, bool fUseTimer, PVUSBRESETARGS pArgs)
|
---|
1386 | {
|
---|
1387 | int rc = VINF_SUCCESS;
|
---|
1388 | uint64_t u64EndTS = TMTimerGet(pDev->pResetTimer) + TMTimerFromMilli(pDev->pResetTimer, 10);
|
---|
1389 |
|
---|
1390 | if (pDev->pUsbIns->pReg->pfnUsbReset)
|
---|
1391 | rc = pDev->pUsbIns->pReg->pfnUsbReset(pDev->pUsbIns, fResetOnLinux);
|
---|
1392 |
|
---|
1393 | if (pArgs)
|
---|
1394 | {
|
---|
1395 | pArgs->rc = rc;
|
---|
1396 | rc = VINF_SUCCESS;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | if (fUseTimer)
|
---|
1400 | {
|
---|
1401 | /*
|
---|
1402 | * We use a timer to communicate the result back to EMT.
|
---|
1403 | * This avoids suspend + poweroff issues, and it should give
|
---|
1404 | * us more accurate scheduling than making this thread sleep.
|
---|
1405 | */
|
---|
1406 | int rc2 = TMTimerSet(pDev->pResetTimer, u64EndTS);
|
---|
1407 | AssertReleaseRC(rc2);
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | LogFlow(("vusbDevResetWorker: %s: returns %Rrc\n", pDev->pUsbIns->pszName, rc));
|
---|
1411 | return rc;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 |
|
---|
1415 | /**
|
---|
1416 | * Resets a device.
|
---|
1417 | *
|
---|
1418 | * Since a device reset shall take at least 10ms from the guest point of view,
|
---|
1419 | * it must be performed asynchronously. We create a thread which performs this
|
---|
1420 | * operation and ensures it will take at least 10ms.
|
---|
1421 | *
|
---|
1422 | * At times - like init - a synchronous reset is required, this can be done
|
---|
1423 | * by passing NULL for pfnDone.
|
---|
1424 | *
|
---|
1425 | * While the device is being reset it is in the VUSB_DEVICE_STATE_RESET state.
|
---|
1426 | * On completion it will be in the VUSB_DEVICE_STATE_DEFAULT state if successful,
|
---|
1427 | * or in the VUSB_DEVICE_STATE_DETACHED state if the rest failed.
|
---|
1428 | *
|
---|
1429 | * @returns VBox status code.
|
---|
1430 | *
|
---|
1431 | * @param pDev Pointer to the VUSB device interface.
|
---|
1432 | * @param fResetOnLinux Whether it's safe to reset the device(s) on a linux
|
---|
1433 | * host system. See discussion of logical reconnects elsewhere.
|
---|
1434 | * @param pfnDone Pointer to the completion routine. If NULL a synchronous
|
---|
1435 | * reset is preformed not respecting the 10ms.
|
---|
1436 | * @param pVM Pointer to the VM handle for performing the done function
|
---|
1437 | * on the EMT thread.
|
---|
1438 | * @thread EMT
|
---|
1439 | */
|
---|
1440 | static DECLCALLBACK(int) vusbIDeviceReset(PVUSBIDEVICE pDevice, bool fResetOnLinux, PFNVUSBRESETDONE pfnDone, void *pvUser, PVM pVM)
|
---|
1441 | {
|
---|
1442 | PVUSBDEV pDev = (PVUSBDEV)pDevice;
|
---|
1443 | Assert(!pfnDone || pVM);
|
---|
1444 | LogFlow(("vusb: reset: [%s]/%i\n", pDev->pUsbIns->pszName, pDev->i16Port));
|
---|
1445 |
|
---|
1446 | /*
|
---|
1447 | * Only one reset operation at a time.
|
---|
1448 | */
|
---|
1449 | const VUSBDEVICESTATE enmStateOld = vusbDevSetState(pDev, VUSB_DEVICE_STATE_RESET);
|
---|
1450 | if (enmStateOld == VUSB_DEVICE_STATE_RESET)
|
---|
1451 | {
|
---|
1452 | LogRel(("VUSB: %s: reset request is ignored, the device is already resetting!\n", pDev->pUsbIns->pszName));
|
---|
1453 | return VERR_VUSB_DEVICE_IS_RESETTING;
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | /*
|
---|
1457 | * First, cancel all async URBs.
|
---|
1458 | */
|
---|
1459 | vusbDevCancelAllUrbs(pDev, false);
|
---|
1460 |
|
---|
1461 | /* Async or sync? */
|
---|
1462 | if (pfnDone)
|
---|
1463 | {
|
---|
1464 | /*
|
---|
1465 | * Async fashion.
|
---|
1466 | */
|
---|
1467 | PVUSBRESETARGS pArgs = (PVUSBRESETARGS)RTMemTmpAlloc(sizeof(*pArgs));
|
---|
1468 | if (pArgs)
|
---|
1469 | {
|
---|
1470 | pArgs->pDev = pDev;
|
---|
1471 | pArgs->pfnDone = pfnDone;
|
---|
1472 | pArgs->pvUser = pvUser;
|
---|
1473 | pArgs->rc = VINF_SUCCESS;
|
---|
1474 | AssertPtrNull(pDev->pvArgs);
|
---|
1475 | pDev->pvArgs = pArgs;
|
---|
1476 | int rc = vusbDevIoThreadExec(pDev, 0 /* fFlags */, (PFNRT)vusbDevResetWorker, 4, pDev, fResetOnLinux, true, pArgs);
|
---|
1477 | if (RT_SUCCESS(rc))
|
---|
1478 | return rc;
|
---|
1479 |
|
---|
1480 | RTMemTmpFree(pArgs);
|
---|
1481 | }
|
---|
1482 | /* fall back to sync on failure */
|
---|
1483 | }
|
---|
1484 |
|
---|
1485 | /*
|
---|
1486 | * Sync fashion.
|
---|
1487 | */
|
---|
1488 | int rc = vusbDevResetWorker(pDev, fResetOnLinux, false, NULL);
|
---|
1489 | vusbDevResetDone(pDev, rc, pfnDone, pvUser);
|
---|
1490 | return rc;
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 |
|
---|
1494 | /**
|
---|
1495 | * Powers on the device.
|
---|
1496 | *
|
---|
1497 | * @returns VBox status code.
|
---|
1498 | * @param pInterface Pointer to the device interface structure.
|
---|
1499 | */
|
---|
1500 | static DECLCALLBACK(int) vusbIDevicePowerOn(PVUSBIDEVICE pInterface)
|
---|
1501 | {
|
---|
1502 | PVUSBDEV pDev = (PVUSBDEV)pInterface;
|
---|
1503 | LogFlow(("vusbDevPowerOn: pDev=%p[%s]\n", pDev, pDev->pUsbIns->pszName));
|
---|
1504 |
|
---|
1505 | /*
|
---|
1506 | * Check that the device is in a valid state.
|
---|
1507 | */
|
---|
1508 | const VUSBDEVICESTATE enmState = vusbDevGetState(pDev);
|
---|
1509 | if (enmState == VUSB_DEVICE_STATE_DETACHED)
|
---|
1510 | {
|
---|
1511 | Log(("vusb: warning: attempt to power on detached device %p[%s]\n", pDev, pDev->pUsbIns->pszName));
|
---|
1512 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
1513 | }
|
---|
1514 | if (enmState == VUSB_DEVICE_STATE_RESET)
|
---|
1515 | {
|
---|
1516 | LogRel(("VUSB: %s: power on ignored, the device is resetting!\n", pDev->pUsbIns->pszName));
|
---|
1517 | return VERR_VUSB_DEVICE_IS_RESETTING;
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | /*
|
---|
1521 | * Do the job.
|
---|
1522 | */
|
---|
1523 | if (enmState == VUSB_DEVICE_STATE_ATTACHED)
|
---|
1524 | vusbDevSetState(pDev, VUSB_DEVICE_STATE_POWERED);
|
---|
1525 |
|
---|
1526 | return VINF_SUCCESS;
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 |
|
---|
1530 | /**
|
---|
1531 | * Powers off the device.
|
---|
1532 | *
|
---|
1533 | * @returns VBox status code.
|
---|
1534 | * @param pInterface Pointer to the device interface structure.
|
---|
1535 | */
|
---|
1536 | static DECLCALLBACK(int) vusbIDevicePowerOff(PVUSBIDEVICE pInterface)
|
---|
1537 | {
|
---|
1538 | PVUSBDEV pDev = (PVUSBDEV)pInterface;
|
---|
1539 | LogFlow(("vusbDevPowerOff: pDev=%p[%s]\n", pDev, pDev->pUsbIns->pszName));
|
---|
1540 |
|
---|
1541 | /*
|
---|
1542 | * Check that the device is in a valid state.
|
---|
1543 | */
|
---|
1544 | const VUSBDEVICESTATE enmState = vusbDevGetState(pDev);
|
---|
1545 | if (enmState == VUSB_DEVICE_STATE_DETACHED)
|
---|
1546 | {
|
---|
1547 | Log(("vusb: warning: attempt to power off detached device %p[%s]\n", pDev, pDev->pUsbIns->pszName));
|
---|
1548 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
1549 | }
|
---|
1550 | if (enmState == VUSB_DEVICE_STATE_RESET)
|
---|
1551 | {
|
---|
1552 | LogRel(("VUSB: %s: power off ignored, the device is resetting!\n", pDev->pUsbIns->pszName));
|
---|
1553 | return VERR_VUSB_DEVICE_IS_RESETTING;
|
---|
1554 | }
|
---|
1555 |
|
---|
1556 | /*
|
---|
1557 | * If it's a root hub, we will have to cancel all URBs and reap them.
|
---|
1558 | */
|
---|
1559 | if (vusbDevIsRh(pDev))
|
---|
1560 | {
|
---|
1561 | PVUSBROOTHUB pRh = (PVUSBROOTHUB)pDev;
|
---|
1562 | VUSBIRhCancelAllUrbs(&pRh->IRhConnector);
|
---|
1563 | VUSBIRhReapAsyncUrbs(&pRh->IRhConnector, pInterface, 0);
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | vusbDevSetState(pDev, VUSB_DEVICE_STATE_ATTACHED);
|
---|
1567 | return VINF_SUCCESS;
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 |
|
---|
1571 | /**
|
---|
1572 | * Get the state of the device.
|
---|
1573 | *
|
---|
1574 | * @returns Device state.
|
---|
1575 | * @param pInterface Pointer to the device interface structure.
|
---|
1576 | */
|
---|
1577 | static DECLCALLBACK(VUSBDEVICESTATE) vusbIDeviceGetState(PVUSBIDEVICE pInterface)
|
---|
1578 | {
|
---|
1579 | return vusbDevGetState((PVUSBDEV)pInterface);
|
---|
1580 | }
|
---|
1581 |
|
---|
1582 |
|
---|
1583 | /**
|
---|
1584 | * @interface_method_impl{VUSBIDEVICE,pfnIsSavedStateSupported}
|
---|
1585 | */
|
---|
1586 | static DECLCALLBACK(bool) vusbIDeviceIsSavedStateSupported(PVUSBIDEVICE pInterface)
|
---|
1587 | {
|
---|
1588 | PVUSBDEV pDev = (PVUSBDEV)pInterface;
|
---|
1589 | bool fSavedStateSupported = RT_BOOL(pDev->pUsbIns->pReg->fFlags & PDM_USBREG_SAVED_STATE_SUPPORTED);
|
---|
1590 |
|
---|
1591 | LogFlowFunc(("pInterface=%p\n", pInterface));
|
---|
1592 |
|
---|
1593 | LogFlowFunc(("returns %RTbool\n", fSavedStateSupported));
|
---|
1594 | return fSavedStateSupported;
|
---|
1595 | }
|
---|
1596 |
|
---|
1597 |
|
---|
1598 | /**
|
---|
1599 | * @interface_method_impl{VUSBIDEVICE,pfnGetState}
|
---|
1600 | */
|
---|
1601 | static DECLCALLBACK(VUSBSPEED) vusbIDeviceGetSpeed(PVUSBIDEVICE pInterface)
|
---|
1602 | {
|
---|
1603 | PVUSBDEV pDev = (PVUSBDEV)pInterface;
|
---|
1604 | VUSBSPEED enmSpeed = pDev->pUsbIns->enmSpeed;
|
---|
1605 |
|
---|
1606 | LogFlowFunc(("pInterface=%p, returns %u\n", pInterface, enmSpeed));
|
---|
1607 | return enmSpeed;
|
---|
1608 | }
|
---|
1609 |
|
---|
1610 |
|
---|
1611 | /**
|
---|
1612 | * The maximum number of interfaces the device can have in all of it's configuration.
|
---|
1613 | *
|
---|
1614 | * @returns Number of interfaces.
|
---|
1615 | * @param pDev The device.
|
---|
1616 | */
|
---|
1617 | size_t vusbDevMaxInterfaces(PVUSBDEV pDev)
|
---|
1618 | {
|
---|
1619 | uint8_t cMax = 0;
|
---|
1620 | unsigned i = pDev->pDescCache->pDevice->bNumConfigurations;
|
---|
1621 | while (i-- > 0)
|
---|
1622 | {
|
---|
1623 | if (pDev->pDescCache->paConfigs[i].Core.bNumInterfaces > cMax)
|
---|
1624 | cMax = pDev->pDescCache->paConfigs[i].Core.bNumInterfaces;
|
---|
1625 | }
|
---|
1626 |
|
---|
1627 | return cMax;
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 |
|
---|
1631 | /**
|
---|
1632 | * Executes a given function on the I/O thread.
|
---|
1633 | *
|
---|
1634 | * @returns IPRT status code.
|
---|
1635 | * @param pDev The USB device instance data.
|
---|
1636 | * @param fFlags Combination of VUSB_DEV_IO_THREAD_EXEC_FLAGS_*
|
---|
1637 | * @param pfnFunction The function to execute.
|
---|
1638 | * @param cArgs Number of arguments to the function.
|
---|
1639 | * @param Args The parameter list.
|
---|
1640 | *
|
---|
1641 | * @remarks See remarks on RTReqQueueCallV
|
---|
1642 | */
|
---|
1643 | DECLHIDDEN(int) vusbDevIoThreadExecV(PVUSBDEV pDev, uint32_t fFlags, PFNRT pfnFunction, unsigned cArgs, va_list Args)
|
---|
1644 | {
|
---|
1645 | int rc = VINF_SUCCESS;
|
---|
1646 | PRTREQ hReq = NULL;
|
---|
1647 |
|
---|
1648 | Assert(pDev->hUrbIoThread != NIL_RTTHREAD);
|
---|
1649 | if (RT_LIKELY(pDev->hUrbIoThread != NIL_RTTHREAD))
|
---|
1650 | {
|
---|
1651 | uint32_t fReqFlags = RTREQFLAGS_IPRT_STATUS;
|
---|
1652 |
|
---|
1653 | if (!(fFlags & VUSB_DEV_IO_THREAD_EXEC_FLAGS_SYNC))
|
---|
1654 | fReqFlags |= RTREQFLAGS_NO_WAIT;
|
---|
1655 |
|
---|
1656 | rc = RTReqQueueCallV(pDev->hReqQueueSync, &hReq, 0 /* cMillies */, fReqFlags, pfnFunction, cArgs, Args);
|
---|
1657 | Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
|
---|
1658 |
|
---|
1659 | /* In case we are called on the I/O thread just process the request. */
|
---|
1660 | if ( pDev->hUrbIoThread == RTThreadSelf()
|
---|
1661 | && (fFlags & VUSB_DEV_IO_THREAD_EXEC_FLAGS_SYNC))
|
---|
1662 | {
|
---|
1663 | int rc2 = RTReqQueueProcess(pDev->hReqQueueSync, 0);
|
---|
1664 | Assert(RT_SUCCESS(rc2) || rc2 == VERR_TIMEOUT);
|
---|
1665 | }
|
---|
1666 | else
|
---|
1667 | vusbDevUrbIoThreadWakeup(pDev);
|
---|
1668 |
|
---|
1669 | if ( rc == VERR_TIMEOUT
|
---|
1670 | && (fFlags & VUSB_DEV_IO_THREAD_EXEC_FLAGS_SYNC))
|
---|
1671 | {
|
---|
1672 | rc = RTReqWait(hReq, RT_INDEFINITE_WAIT);
|
---|
1673 | AssertRC(rc);
|
---|
1674 | }
|
---|
1675 | RTReqRelease(hReq);
|
---|
1676 | }
|
---|
1677 | else
|
---|
1678 | rc = VERR_INVALID_STATE;
|
---|
1679 |
|
---|
1680 | return rc;
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 |
|
---|
1684 | /**
|
---|
1685 | * Executes a given function on the I/O thread.
|
---|
1686 | *
|
---|
1687 | * @returns IPRT status code.
|
---|
1688 | * @param pDev The USB device instance data.
|
---|
1689 | * @param fFlags Combination of VUSB_DEV_IO_THREAD_EXEC_FLAGS_*
|
---|
1690 | * @param pfnFunction The function to execute.
|
---|
1691 | * @param cArgs Number of arguments to the function.
|
---|
1692 | * @param ... The parameter list.
|
---|
1693 | *
|
---|
1694 | * @remarks See remarks on RTReqQueueCallV
|
---|
1695 | */
|
---|
1696 | DECLHIDDEN(int) vusbDevIoThreadExec(PVUSBDEV pDev, uint32_t fFlags, PFNRT pfnFunction, unsigned cArgs, ...)
|
---|
1697 | {
|
---|
1698 | int rc = VINF_SUCCESS;
|
---|
1699 | va_list va;
|
---|
1700 |
|
---|
1701 | va_start(va, cArgs);
|
---|
1702 | rc = vusbDevIoThreadExecV(pDev, fFlags, pfnFunction, cArgs, va);
|
---|
1703 | va_end(va);
|
---|
1704 | return rc;
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 |
|
---|
1708 | /**
|
---|
1709 | * Executes a given function synchronously on the I/O thread waiting for it to complete.
|
---|
1710 | *
|
---|
1711 | * @returns IPRT status code.
|
---|
1712 | * @param pDev The USB device instance data
|
---|
1713 | * @param pfnFunction The function to execute.
|
---|
1714 | * @param cArgs Number of arguments to the function.
|
---|
1715 | * @param ... The parameter list.
|
---|
1716 | *
|
---|
1717 | * @remarks See remarks on RTReqQueueCallV
|
---|
1718 | */
|
---|
1719 | DECLHIDDEN(int) vusbDevIoThreadExecSync(PVUSBDEV pDev, PFNRT pfnFunction, unsigned cArgs, ...)
|
---|
1720 | {
|
---|
1721 | int rc = VINF_SUCCESS;
|
---|
1722 | va_list va;
|
---|
1723 |
|
---|
1724 | va_start(va, cArgs);
|
---|
1725 | rc = vusbDevIoThreadExecV(pDev, VUSB_DEV_IO_THREAD_EXEC_FLAGS_SYNC, pfnFunction, cArgs, va);
|
---|
1726 | va_end(va);
|
---|
1727 | return rc;
|
---|
1728 | }
|
---|
1729 |
|
---|
1730 |
|
---|
1731 | static DECLCALLBACK(int) vusbDevGetDescriptorCacheWorker(PPDMUSBINS pUsbIns, PCPDMUSBDESCCACHE *ppDescCache)
|
---|
1732 | {
|
---|
1733 | *ppDescCache = pUsbIns->pReg->pfnUsbGetDescriptorCache(pUsbIns);
|
---|
1734 | return VINF_SUCCESS;
|
---|
1735 | }
|
---|
1736 |
|
---|
1737 | /**
|
---|
1738 | * Initialize a new VUSB device.
|
---|
1739 | *
|
---|
1740 | * @returns VBox status code.
|
---|
1741 | * @param pDev The VUSB device to initialize.
|
---|
1742 | * @param pUsbIns Pointer to the PDM USB Device instance.
|
---|
1743 | */
|
---|
1744 | int vusbDevInit(PVUSBDEV pDev, PPDMUSBINS pUsbIns, const char *pszCaptureFilename)
|
---|
1745 | {
|
---|
1746 | /*
|
---|
1747 | * Initialize the device data members.
|
---|
1748 | * (All that are Non-Zero at least.)
|
---|
1749 | */
|
---|
1750 | Assert(!pDev->IDevice.pfnReset);
|
---|
1751 | Assert(!pDev->IDevice.pfnPowerOn);
|
---|
1752 | Assert(!pDev->IDevice.pfnPowerOff);
|
---|
1753 | Assert(!pDev->IDevice.pfnGetState);
|
---|
1754 | Assert(!pDev->IDevice.pfnIsSavedStateSupported);
|
---|
1755 |
|
---|
1756 | pDev->IDevice.pfnReset = vusbIDeviceReset;
|
---|
1757 | pDev->IDevice.pfnPowerOn = vusbIDevicePowerOn;
|
---|
1758 | pDev->IDevice.pfnPowerOff = vusbIDevicePowerOff;
|
---|
1759 | pDev->IDevice.pfnGetState = vusbIDeviceGetState;
|
---|
1760 | pDev->IDevice.pfnIsSavedStateSupported = vusbIDeviceIsSavedStateSupported;
|
---|
1761 | pDev->IDevice.pfnGetSpeed = vusbIDeviceGetSpeed;
|
---|
1762 | pDev->pUsbIns = pUsbIns;
|
---|
1763 | pDev->pNext = NULL;
|
---|
1764 | pDev->pNextHash = NULL;
|
---|
1765 | pDev->pHub = NULL;
|
---|
1766 | pDev->enmState = VUSB_DEVICE_STATE_DETACHED;
|
---|
1767 | pDev->u8Address = VUSB_INVALID_ADDRESS;
|
---|
1768 | pDev->u8NewAddress = VUSB_INVALID_ADDRESS;
|
---|
1769 | pDev->i16Port = -1;
|
---|
1770 | pDev->u16Status = 0;
|
---|
1771 | pDev->pDescCache = NULL;
|
---|
1772 | pDev->pCurCfgDesc = NULL;
|
---|
1773 | pDev->paIfStates = NULL;
|
---|
1774 | RTListInit(&pDev->LstAsyncUrbs);
|
---|
1775 | memset(&pDev->aPipes[0], 0, sizeof(pDev->aPipes));
|
---|
1776 | for (unsigned i = 0; i < RT_ELEMENTS(pDev->aPipes); i++)
|
---|
1777 | {
|
---|
1778 | int rc = RTCritSectInit(&pDev->aPipes[i].CritSectCtrl);
|
---|
1779 | AssertRCReturn(rc, rc);
|
---|
1780 | }
|
---|
1781 | pDev->pResetTimer = NULL;
|
---|
1782 | pDev->hSniffer = VUSBSNIFFER_NIL;
|
---|
1783 |
|
---|
1784 | int rc = RTCritSectInit(&pDev->CritSectAsyncUrbs);
|
---|
1785 | AssertRCReturn(rc, rc);
|
---|
1786 |
|
---|
1787 | /* Create the URB pool. */
|
---|
1788 | rc = vusbUrbPoolInit(&pDev->UrbPool);
|
---|
1789 | AssertRCReturn(rc, rc);
|
---|
1790 |
|
---|
1791 | /* Setup request queue executing synchronous tasks on the I/O thread. */
|
---|
1792 | rc = RTReqQueueCreate(&pDev->hReqQueueSync);
|
---|
1793 | AssertRCReturn(rc, rc);
|
---|
1794 |
|
---|
1795 | /* Create I/O thread. */
|
---|
1796 | rc = vusbDevUrbIoThreadCreate(pDev);
|
---|
1797 | AssertRCReturn(rc, rc);
|
---|
1798 |
|
---|
1799 | /*
|
---|
1800 | * Create the reset timer.
|
---|
1801 | */
|
---|
1802 | rc = PDMUsbHlpTMTimerCreate(pDev->pUsbIns, TMCLOCK_VIRTUAL, vusbDevResetDoneTimer, pDev, 0 /*fFlags*/,
|
---|
1803 | "USB Device Reset Timer", &pDev->pResetTimer);
|
---|
1804 | AssertRCReturn(rc, rc);
|
---|
1805 |
|
---|
1806 | if (pszCaptureFilename)
|
---|
1807 | {
|
---|
1808 | rc = VUSBSnifferCreate(&pDev->hSniffer, 0, pszCaptureFilename, NULL, NULL);
|
---|
1809 | AssertRCReturn(rc, rc);
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | /*
|
---|
1813 | * Get the descriptor cache from the device. (shall cannot fail)
|
---|
1814 | */
|
---|
1815 | rc = vusbDevIoThreadExecSync(pDev, (PFNRT)vusbDevGetDescriptorCacheWorker, 2, pUsbIns, &pDev->pDescCache);
|
---|
1816 | AssertRC(rc);
|
---|
1817 | AssertPtr(pDev->pDescCache);
|
---|
1818 | #ifdef VBOX_STRICT
|
---|
1819 | if (pDev->pDescCache->fUseCachedStringsDescriptors)
|
---|
1820 | {
|
---|
1821 | int32_t iPrevId = -1;
|
---|
1822 | for (unsigned iLang = 0; iLang < pDev->pDescCache->cLanguages; iLang++)
|
---|
1823 | {
|
---|
1824 | Assert((int32_t)pDev->pDescCache->paLanguages[iLang].idLang > iPrevId);
|
---|
1825 | iPrevId = pDev->pDescCache->paLanguages[iLang].idLang;
|
---|
1826 |
|
---|
1827 | int32_t idxPrevStr = -1;
|
---|
1828 | PCPDMUSBDESCCACHESTRING paStrings = pDev->pDescCache->paLanguages[iLang].paStrings;
|
---|
1829 | unsigned cStrings = pDev->pDescCache->paLanguages[iLang].cStrings;
|
---|
1830 | for (unsigned iStr = 0; iStr < cStrings; iStr++)
|
---|
1831 | {
|
---|
1832 | Assert((int32_t)paStrings[iStr].idx > idxPrevStr);
|
---|
1833 | idxPrevStr = paStrings[iStr].idx;
|
---|
1834 | size_t cch = strlen(paStrings[iStr].psz);
|
---|
1835 | Assert(cch <= 127);
|
---|
1836 | }
|
---|
1837 | }
|
---|
1838 | }
|
---|
1839 | #endif
|
---|
1840 |
|
---|
1841 | /*
|
---|
1842 | * Allocate memory for the interface states.
|
---|
1843 | */
|
---|
1844 | size_t cbIface = vusbDevMaxInterfaces(pDev) * sizeof(*pDev->paIfStates);
|
---|
1845 | pDev->paIfStates = (PVUSBINTERFACESTATE)RTMemAllocZ(cbIface);
|
---|
1846 | AssertMsgReturn(pDev->paIfStates, ("RTMemAllocZ(%d) failed\n", cbIface), VERR_NO_MEMORY);
|
---|
1847 |
|
---|
1848 | return VINF_SUCCESS;
|
---|
1849 | }
|
---|
1850 |
|
---|
1851 | /*
|
---|
1852 | * Local Variables:
|
---|
1853 | * mode: c
|
---|
1854 | * c-file-style: "bsd"
|
---|
1855 | * c-basic-offset: 4
|
---|
1856 | * tab-width: 4
|
---|
1857 | * indent-tabs-mode: s
|
---|
1858 | * End:
|
---|
1859 | */
|
---|
1860 |
|
---|