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