1 | /** @file
|
---|
2 | *
|
---|
3 | * VUSB Device - USB Device Proxy, the Linux backend.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * innotek GmbH confidential
|
---|
10 | * All rights reserved
|
---|
11 | */
|
---|
12 |
|
---|
13 |
|
---|
14 | /*******************************************************************************
|
---|
15 | * Defined Constants And Macros *
|
---|
16 | *******************************************************************************/
|
---|
17 | /** Define NO_PORT_RESET to skip the slow and broken linux port reset.
|
---|
18 | * Resetting will break PalmOne. */
|
---|
19 | #define NO_PORT_RESET
|
---|
20 | /** Define NO_LOGICAL_RECONNECT to skip the broken logical reconnect handling. */
|
---|
21 | #define NO_LOGICAL_RECONNECT
|
---|
22 |
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #define LOG_GROUP LOG_GROUP_DRV_USBPROXY
|
---|
28 | #if defined(VBOX) && !defined(RDESKTOP)
|
---|
29 | # include <iprt/stdint.h>
|
---|
30 | #endif
|
---|
31 | #include <sys/types.h>
|
---|
32 | #include <sys/stat.h>
|
---|
33 | #include <sys/vfs.h>
|
---|
34 | #include <sys/ioctl.h>
|
---|
35 | #include <sys/poll.h>
|
---|
36 | #include <stdint.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <string.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <limits.h>
|
---|
41 | #include <unistd.h>
|
---|
42 | #include <fcntl.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <ctype.h>
|
---|
45 | #ifndef VBOX_WITHOUT_LINUX_COMPILER_H
|
---|
46 | # include <linux/compiler.h>
|
---|
47 | #endif
|
---|
48 | #include <linux/usbdevice_fs.h>
|
---|
49 | /*
|
---|
50 | * Backlevel 2.4 headers doesn't have these two defines.
|
---|
51 | * They were added some time between 2.4.21 and 2.4.26, probably in 2.4.23.
|
---|
52 | */
|
---|
53 | #ifndef USBDEVFS_DISCONNECT
|
---|
54 | # define USBDEVFS_DISCONNECT _IO('U', 22)
|
---|
55 | # define USBDEVFS_CONNECT _IO('U', 23)
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #ifndef USBDEVFS_URB_SHORT_NOT_OK
|
---|
59 | # define USBDEVFS_URB_SHORT_NOT_OK 0 /* rhel3 doesn't have this. darn! */
|
---|
60 | #endif
|
---|
61 |
|
---|
62 |
|
---|
63 | /* FedoraCore 4 does not have the bit defined by default. */
|
---|
64 | #ifndef POLLWRNORM
|
---|
65 | # define POLLWRNORM 0x0100
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #if defined(VBOX) && !defined(RDESKTOP)
|
---|
69 | # include <VBox/pdm.h>
|
---|
70 | # include <VBox/err.h>
|
---|
71 | # include <VBox/log.h>
|
---|
72 | # include <iprt/assert.h>
|
---|
73 | # include <iprt/stream.h>
|
---|
74 | # include <iprt/alloc.h>
|
---|
75 | # include <iprt/thread.h>
|
---|
76 | # include <iprt/time.h>
|
---|
77 | # include <iprt/asm.h>
|
---|
78 | # include <iprt/string.h>
|
---|
79 | # include <iprt/file.h>
|
---|
80 | # include "../USBProxyDevice.h"
|
---|
81 | #else
|
---|
82 |
|
---|
83 | # include "../rdesktop.h"
|
---|
84 | # include "vrdpusb.h"
|
---|
85 | #endif
|
---|
86 |
|
---|
87 |
|
---|
88 | /*******************************************************************************
|
---|
89 | * Structures and Typedefs *
|
---|
90 | *******************************************************************************/
|
---|
91 | /**
|
---|
92 | * Wrapper around the linux urb request structure.
|
---|
93 | * This is required to track in-flight and landed URBs.
|
---|
94 | */
|
---|
95 | typedef struct USBPROXYURBLNX
|
---|
96 | {
|
---|
97 | /** The kernel URB data */
|
---|
98 | struct usbdevfs_urb KUrb;
|
---|
99 | /** The millisecond timestamp when this URB was submitted. */
|
---|
100 | uint64_t u64SubmitTS;
|
---|
101 | /** Pointer to the next linux URB. */
|
---|
102 | struct USBPROXYURBLNX *pNext;
|
---|
103 | /** Pointer to the previous linux URB. */
|
---|
104 | struct USBPROXYURBLNX *pPrev;
|
---|
105 | /** If we've split the VUSBURB up into multiple linux URBs, this is points to the head. */
|
---|
106 | struct USBPROXYURBLNX *pSplitHead;
|
---|
107 | /** The next linux URB if split up. */
|
---|
108 | struct USBPROXYURBLNX *pSplitNext;
|
---|
109 | /** Whether it has timed out and should be shot down on the next failing reap call. */
|
---|
110 | bool fTimedOut;
|
---|
111 | /** Indicates that this URB has been canceled by timeout and should return an CRC error. */
|
---|
112 | bool fCanceledByTimedOut;
|
---|
113 | /** Don't report these back. */
|
---|
114 | bool fCanceledBySubmit;
|
---|
115 | /** This split element is reaped. */
|
---|
116 | bool fSplitElementReaped;
|
---|
117 | } USBPROXYURBLNX, *PUSBPROXYURBLNX;
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Data for the linux usb proxy backend.
|
---|
121 | */
|
---|
122 | typedef struct USBPROXYDEVLNX
|
---|
123 | {
|
---|
124 | /** The open file. */
|
---|
125 | RTFILE File;
|
---|
126 | /** Critical section protecting the two lists. */
|
---|
127 | RTCRITSECT CritSect;
|
---|
128 | /** The list of free linux URBs. Singly linked. */
|
---|
129 | PUSBPROXYURBLNX pFreeHead;
|
---|
130 | /** The list of active linux URBs. Doubly linked.
|
---|
131 | * We must maintain this so we can properly reap URBs of a detached device.
|
---|
132 | * Only the split head will appear in this list. */
|
---|
133 | PUSBPROXYURBLNX pInFlightHead;
|
---|
134 | /** The list of landed linux URBs. Doubly linked.
|
---|
135 | * Only the split head will appear in this list. */
|
---|
136 | PUSBPROXYURBLNX pTaxingHead;
|
---|
137 | /** The tail of the landed linux URBs. */
|
---|
138 | PUSBPROXYURBLNX pTaxingTail;
|
---|
139 | } USBPROXYDEVLNX, *PUSBPROXYDEVLNX;
|
---|
140 |
|
---|
141 |
|
---|
142 | /*******************************************************************************
|
---|
143 | * Internal Functions *
|
---|
144 | *******************************************************************************/
|
---|
145 | static int usbProxyLinuxDoIoCtl(PUSBPROXYDEV pProxyDev, int iCmd, void *pvArg, bool fHandleNoDev, uint32_t cTries);
|
---|
146 | static void usbProxLinuxUrbUnplugged(PUSBPROXYDEV pProxyDev);
|
---|
147 | static void usbProxyLinuxSetConnected(PUSBPROXYDEV pProyxDev, int iIf, bool fConnect, bool fQuiet);
|
---|
148 | static PUSBPROXYURBLNX usbProxyLinuxUrbAlloc(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pSplitHead);
|
---|
149 | static void usbProxyLinuxUrbFree(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx);
|
---|
150 | static void usbProxyLinuxUrbFreeSplitList(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx);
|
---|
151 | static int usbProxyLinuxFindActiveConfig(PUSBPROXYDEV pProxyDev, const char *pszAddress, int *iFirstCfg);
|
---|
152 |
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Wrapper for the ioctl call.
|
---|
157 | *
|
---|
158 | * This wrapper will repeate the call if we get an EINTR or EAGAIN. It can also
|
---|
159 | * handle ENODEV (detached device) errors.
|
---|
160 | *
|
---|
161 | * @returns whatever ioctl returns.
|
---|
162 | * @param pProxyDev The proxy device.
|
---|
163 | * @param iCmd The ioctl command / function.
|
---|
164 | * @param pvArg The ioctl argument / data.
|
---|
165 | * @param fHandleNoDev Whether to handle ENODEV.
|
---|
166 | * @param cTries The number of retries. Use UINT32_MAX for (kind of) indefinite retries.
|
---|
167 | * @internal
|
---|
168 | */
|
---|
169 | static int usbProxyLinuxDoIoCtl(PUSBPROXYDEV pProxyDev, int iCmd, void *pvArg, bool fHandleNoDev, uint32_t cTries)
|
---|
170 | {
|
---|
171 | int rc;
|
---|
172 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
173 | do
|
---|
174 | {
|
---|
175 | do
|
---|
176 | {
|
---|
177 | rc = ioctl(pDevLnx->File, iCmd, pvArg);
|
---|
178 | if (rc >= 0)
|
---|
179 | return rc;
|
---|
180 | } while (errno == EINTR);
|
---|
181 |
|
---|
182 | if (errno == ENODEV && fHandleNoDev)
|
---|
183 | {
|
---|
184 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
185 | Log(("usb-linux: ENODEV -> unplugged. pProxyDev=%p[%s]\n", pProxyDev, pProxyDev->Dev.pszName));
|
---|
186 | errno = ENODEV;
|
---|
187 | break;
|
---|
188 | }
|
---|
189 | if (errno != EAGAIN)
|
---|
190 | break;
|
---|
191 | } while (cTries-- > 0);
|
---|
192 |
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * The device has been unplugged.
|
---|
199 | * Cancel all in-flight URBs and put them up for reaping.
|
---|
200 | */
|
---|
201 | static void usbProxLinuxUrbUnplugged(PUSBPROXYDEV pProxyDev)
|
---|
202 | {
|
---|
203 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Shoot down all flying URBs.
|
---|
207 | */
|
---|
208 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
209 |
|
---|
210 | PUSBPROXYURBLNX pUrbTaxing = NULL;
|
---|
211 | PUSBPROXYURBLNX pUrbLnx = pDevLnx->pInFlightHead;
|
---|
212 | pDevLnx->pInFlightHead = NULL;
|
---|
213 | while (pUrbLnx)
|
---|
214 | {
|
---|
215 | PUSBPROXYURBLNX pCur = pUrbLnx;
|
---|
216 | pUrbLnx = pUrbLnx->pNext;
|
---|
217 |
|
---|
218 | ioctl(pDevLnx->File, USBDEVFS_DISCARDURB, &pCur->KUrb); /* not sure if this is required.. */
|
---|
219 | if (!pCur->KUrb.status)
|
---|
220 | pCur->KUrb.status = -ENODEV;
|
---|
221 |
|
---|
222 | /* insert into the taxing list. */
|
---|
223 | pCur->pPrev = NULL;
|
---|
224 | if ( !pCur->pSplitHead
|
---|
225 | || pCur == pCur->pSplitHead)
|
---|
226 | {
|
---|
227 | pCur->pNext = pUrbTaxing;
|
---|
228 | if (pUrbTaxing)
|
---|
229 | pUrbTaxing->pPrev = pCur;
|
---|
230 | pUrbTaxing = pCur;
|
---|
231 | }
|
---|
232 | else
|
---|
233 | pCur->pNext = NULL;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /* Append the URBs we shot down to the taxing queue. */
|
---|
237 | if (pUrbTaxing)
|
---|
238 | {
|
---|
239 | pUrbTaxing->pPrev = pDevLnx->pTaxingTail;
|
---|
240 | if (pUrbTaxing->pPrev)
|
---|
241 | pUrbTaxing->pPrev->pNext = pUrbTaxing;
|
---|
242 | else
|
---|
243 | pDevLnx->pTaxingTail = pDevLnx->pTaxingHead = pUrbTaxing;
|
---|
244 | }
|
---|
245 |
|
---|
246 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
247 |
|
---|
248 | vusbDevUnplugged(&pProxyDev->Dev);
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Set the connect state seen by kernel drivers
|
---|
254 | * @internal
|
---|
255 | */
|
---|
256 | static void usbProxyLinuxSetConnected(PUSBPROXYDEV pProxyDev, int iIf, bool fConnect, bool fQuiet)
|
---|
257 | {
|
---|
258 | struct usbdevfs_ioctl IoCtl;
|
---|
259 | if (!fQuiet)
|
---|
260 | LogFlow(("usbProxyLinuxSetConnected: pProxyDev=%p[%s] iIf=%#x fConnect=%d\n",
|
---|
261 | pProxyDev, pProxyDev->Dev.pszName, iIf, fConnect));
|
---|
262 |
|
---|
263 | IoCtl.ifno = iIf;
|
---|
264 | IoCtl.ioctl_code = fConnect ? USBDEVFS_CONNECT : USBDEVFS_DISCONNECT;
|
---|
265 | IoCtl.data = NULL;
|
---|
266 | if ( usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_IOCTL, &IoCtl, true, UINT32_MAX)
|
---|
267 | && !fQuiet)
|
---|
268 | Log(("usbProxyLinuxSetConnected: failure, errno=%d. pProxyDev=%p[%s]\n",
|
---|
269 | errno, pProxyDev, pProxyDev->Dev.pszName));
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Allocates a linux URB request structure.
|
---|
275 | * @returns Pointer to an active URB request.
|
---|
276 | * @returns NULL on failure.
|
---|
277 | * @param pProxyDev The proxy device instance.
|
---|
278 | * @param pSplitHead The split list head if allocating for a split list.
|
---|
279 | */
|
---|
280 | static PUSBPROXYURBLNX usbProxyLinuxUrbAlloc(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pSplitHead)
|
---|
281 | {
|
---|
282 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
283 | PUSBPROXYURBLNX pUrbLnx;
|
---|
284 |
|
---|
285 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
286 |
|
---|
287 | /*
|
---|
288 | * Try remove a linux URB from the free list, if none there allocate a new one.
|
---|
289 | */
|
---|
290 | pUrbLnx = pDevLnx->pFreeHead;
|
---|
291 | if (pUrbLnx)
|
---|
292 | pDevLnx->pFreeHead = pUrbLnx->pNext;
|
---|
293 | else
|
---|
294 | {
|
---|
295 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
296 | pUrbLnx = (PUSBPROXYURBLNX)RTMemAlloc(sizeof(*pUrbLnx));
|
---|
297 | if (!pUrbLnx)
|
---|
298 | return NULL;
|
---|
299 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
300 | }
|
---|
301 | pUrbLnx->pSplitHead = pSplitHead;
|
---|
302 | pUrbLnx->pSplitNext = NULL;
|
---|
303 | pUrbLnx->fTimedOut = false;
|
---|
304 | pUrbLnx->fCanceledByTimedOut = false;
|
---|
305 | pUrbLnx->fCanceledBySubmit = false;
|
---|
306 | pUrbLnx->fSplitElementReaped = false;
|
---|
307 |
|
---|
308 | /*
|
---|
309 | * Link it into the active list
|
---|
310 | */
|
---|
311 | if (!pSplitHead)
|
---|
312 | {
|
---|
313 | pUrbLnx->pPrev = NULL;
|
---|
314 | pUrbLnx->pNext = pDevLnx->pInFlightHead;
|
---|
315 | if (pUrbLnx->pNext)
|
---|
316 | pUrbLnx->pNext->pPrev = pUrbLnx;
|
---|
317 | pDevLnx->pInFlightHead = pUrbLnx;
|
---|
318 | }
|
---|
319 | else
|
---|
320 | pUrbLnx->pPrev = pUrbLnx->pNext = (PUSBPROXYURBLNX)0xdead;
|
---|
321 |
|
---|
322 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
323 | return pUrbLnx;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Frees a linux URB request structure.
|
---|
329 | *
|
---|
330 | * @param pProxyDev The proxy device instance.
|
---|
331 | * @param pUrbLnx The linux URB to free.
|
---|
332 | */
|
---|
333 | static void usbProxyLinuxUrbFree(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx)
|
---|
334 | {
|
---|
335 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
336 |
|
---|
337 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
338 |
|
---|
339 | /*
|
---|
340 | * Remove from the active list.
|
---|
341 | */
|
---|
342 | if ( !pUrbLnx->pSplitHead
|
---|
343 | || pUrbLnx->pSplitHead == pUrbLnx)
|
---|
344 | {
|
---|
345 | if (pUrbLnx->pNext)
|
---|
346 | pUrbLnx->pNext->pPrev = pUrbLnx->pPrev;
|
---|
347 | if (pUrbLnx->pPrev)
|
---|
348 | pUrbLnx->pPrev->pNext = pUrbLnx->pNext;
|
---|
349 | else
|
---|
350 | pDevLnx->pInFlightHead = pUrbLnx->pNext;
|
---|
351 | }
|
---|
352 | pUrbLnx->pSplitHead = pUrbLnx->pSplitNext = NULL;
|
---|
353 |
|
---|
354 | /*
|
---|
355 | * Link it into the free list.
|
---|
356 | */
|
---|
357 | pUrbLnx->pPrev = NULL;
|
---|
358 | pUrbLnx->pNext = pDevLnx->pFreeHead;
|
---|
359 | pDevLnx->pFreeHead = pUrbLnx;
|
---|
360 |
|
---|
361 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Frees split list of a linux URB request structure.
|
---|
367 | *
|
---|
368 | * @param pProxyDev The proxy device instance.
|
---|
369 | * @param pUrbLnx A linux URB to in the split list to be freed.
|
---|
370 | */
|
---|
371 | static void usbProxyLinuxUrbFreeSplitList(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx)
|
---|
372 | {
|
---|
373 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
374 |
|
---|
375 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
376 |
|
---|
377 | pUrbLnx = pUrbLnx->pSplitHead;
|
---|
378 | Assert(pUrbLnx);
|
---|
379 | while (pUrbLnx)
|
---|
380 | {
|
---|
381 | PUSBPROXYURBLNX pFree = pUrbLnx;
|
---|
382 | pUrbLnx = pUrbLnx->pSplitNext;
|
---|
383 | Assert(pFree->pSplitHead);
|
---|
384 | usbProxyLinuxUrbFree(pProxyDev, pFree);
|
---|
385 | }
|
---|
386 |
|
---|
387 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * This finds the device in the /proc/bus/usb/bus/addr file and finds
|
---|
393 | * the config with an asterix.
|
---|
394 | *
|
---|
395 | * @returns The Cfg#.
|
---|
396 | * @returns -1 if no active config.
|
---|
397 | * @param pszAddress The path to the device. We infere the location of the
|
---|
398 | * devices file, which bus and device number we're looking for.
|
---|
399 | * @param iFirstCfg The first configuration. (optional)
|
---|
400 | * @internal
|
---|
401 | */
|
---|
402 | static int usbProxyLinuxFindActiveConfig(PUSBPROXYDEV pProxyDev, const char *pszAddress, int *piFirstCfg)
|
---|
403 | {
|
---|
404 | /*
|
---|
405 | * Set return defaults.
|
---|
406 | */
|
---|
407 | int iActiveCfg = -1;
|
---|
408 | if (piFirstCfg)
|
---|
409 | *piFirstCfg = 1;
|
---|
410 |
|
---|
411 | /*
|
---|
412 | * Interpret the address and turn it into a path to the devices file.
|
---|
413 | */
|
---|
414 | size_t cchAddress = strlen(pszAddress);
|
---|
415 | char *pszDevices = (char *)RTMemDupEx(pszAddress, cchAddress, sizeof("devices"));
|
---|
416 | AssertReturn(pszDevices, iActiveCfg);
|
---|
417 |
|
---|
418 | /* the device number */
|
---|
419 | char *psz = pszDevices + cchAddress;
|
---|
420 | while (*psz != '/')
|
---|
421 | psz--;
|
---|
422 | Assert(pszDevices < psz);
|
---|
423 | uint32_t uDev;
|
---|
424 | int rc = RTStrToUInt32Ex(psz + 1, NULL, 10, &uDev);
|
---|
425 | if (VBOX_SUCCESS(rc))
|
---|
426 | {
|
---|
427 | /* the bus number */
|
---|
428 | *psz-- = '\0';
|
---|
429 | while (*psz != '/')
|
---|
430 | psz--;
|
---|
431 | Assert(pszDevices < psz);
|
---|
432 | uint32_t uBus;
|
---|
433 | rc = RTStrToUInt32Ex(psz + 1, NULL, 10, &uBus);
|
---|
434 | if (VBOX_SUCCESS(rc))
|
---|
435 | {
|
---|
436 | strcpy(psz + 1, "devices");
|
---|
437 |
|
---|
438 | /*
|
---|
439 | * Open and scan the devices file.
|
---|
440 | * We're ASSUMING that each device starts off with a 'T:' line.
|
---|
441 | */
|
---|
442 | PRTSTREAM pFile;
|
---|
443 | rc = RTStrmOpen(pszDevices, "r", &pFile);
|
---|
444 | if (VBOX_SUCCESS(rc))
|
---|
445 | {
|
---|
446 | char szLine[1024];
|
---|
447 | while (VBOX_SUCCESS(RTStrmGetLine(pFile, szLine, sizeof(szLine))))
|
---|
448 | {
|
---|
449 | /* we're only interested in 'T:' lines. */
|
---|
450 | psz = RTStrStripL(szLine);
|
---|
451 | if (psz[0] != 'T' || psz[1] != ':')
|
---|
452 | continue;
|
---|
453 |
|
---|
454 | /* Skip ahead to 'Bus' and compare */
|
---|
455 | psz = RTStrStripL(psz + 2); Assert(!strncmp(psz, "Bus=", 4));
|
---|
456 | psz = RTStrStripL(psz + 4);
|
---|
457 | char *pszNext;
|
---|
458 | uint32_t u;
|
---|
459 | rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u); AssertRC(rc);
|
---|
460 | if (RT_FAILURE(rc))
|
---|
461 | continue;
|
---|
462 | if (u != uBus)
|
---|
463 | continue;
|
---|
464 |
|
---|
465 | /* Skip ahead to 'Dev#' and compare */
|
---|
466 | psz = strstr(psz, "Dev#="); Assert(psz);
|
---|
467 | if (!psz)
|
---|
468 | continue;
|
---|
469 | psz = RTStrStripL(psz + 5);
|
---|
470 | rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u); AssertRC(rc);
|
---|
471 | if (RT_FAILURE(rc))
|
---|
472 | continue;
|
---|
473 | if (u != uDev)
|
---|
474 | continue;
|
---|
475 |
|
---|
476 | /*
|
---|
477 | * Ok, we've found the device.
|
---|
478 | * Scan until we find a selected configuration, the next device, or EOF.
|
---|
479 | */
|
---|
480 | while (VBOX_SUCCESS(RTStrmGetLine(pFile, szLine, sizeof(szLine))))
|
---|
481 | {
|
---|
482 | psz = RTStrStripL(szLine);
|
---|
483 | if (psz[0] == 'T')
|
---|
484 | break;
|
---|
485 | if (psz[0] != 'C' || psz[1] != ':')
|
---|
486 | continue;
|
---|
487 | const bool fActive = psz[2] == '*';
|
---|
488 | if (!fActive && !piFirstCfg)
|
---|
489 | continue;
|
---|
490 |
|
---|
491 | /* Get the 'Cfg#' value. */
|
---|
492 | psz = strstr(psz, "Cfg#="); Assert(psz);
|
---|
493 | if (psz)
|
---|
494 | {
|
---|
495 | psz = RTStrStripL(psz + 5);
|
---|
496 | rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u); AssertRC(rc);
|
---|
497 | if (VBOX_SUCCESS(rc))
|
---|
498 | {
|
---|
499 | if (piFirstCfg)
|
---|
500 | {
|
---|
501 | *piFirstCfg = u;
|
---|
502 | piFirstCfg = NULL;
|
---|
503 | }
|
---|
504 | if (fActive)
|
---|
505 | iActiveCfg = u;
|
---|
506 | }
|
---|
507 | }
|
---|
508 | if (fActive)
|
---|
509 | break;
|
---|
510 | }
|
---|
511 | break;
|
---|
512 | }
|
---|
513 | RTStrmClose(pFile);
|
---|
514 | }
|
---|
515 | }
|
---|
516 | }
|
---|
517 | RTMemFree(pszDevices);
|
---|
518 |
|
---|
519 | return iActiveCfg;
|
---|
520 | }
|
---|
521 |
|
---|
522 | int dev2fd (PUSBPROXYDEV pProxyDev)
|
---|
523 | {
|
---|
524 | if (pProxyDev)
|
---|
525 | {
|
---|
526 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
527 |
|
---|
528 | if (pDevLnx)
|
---|
529 | {
|
---|
530 | return pDevLnx->File;
|
---|
531 | }
|
---|
532 | }
|
---|
533 | return -1;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /**
|
---|
537 | * Opens the /proc/bus/usb/bus/addr file.
|
---|
538 | *
|
---|
539 | * @returns VBox status code.
|
---|
540 | * @param pProxyDev The device instance.
|
---|
541 | * @param pszAddress The path to the device.
|
---|
542 | * @param pvBackend Backend specific pointer, unused for the linux backend.
|
---|
543 | */
|
---|
544 | static int usbProxyLinuxOpen(PUSBPROXYDEV pProxyDev, const char *pszAddress, void *pvBackend)
|
---|
545 | {
|
---|
546 | LogFlow(("usbProxyLinuxOpen: pProxyDev=%p pszAddress=%s\n", pProxyDev, pszAddress));
|
---|
547 | RTFILE File;
|
---|
548 | int rc = RTFileOpen(&File, pszAddress, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
549 | if (VBOX_SUCCESS(rc))
|
---|
550 | {
|
---|
551 | /*
|
---|
552 | * Allocate and initialize the linux backend data.
|
---|
553 | */
|
---|
554 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)RTMemAllocZ(sizeof(*pDevLnx));
|
---|
555 | if (pDevLnx)
|
---|
556 | {
|
---|
557 | pDevLnx->File = File;
|
---|
558 | rc = RTCritSectInit(&pDevLnx->CritSect);
|
---|
559 | if (VBOX_SUCCESS(rc))
|
---|
560 | {
|
---|
561 | pProxyDev->Backend.pv = pDevLnx;
|
---|
562 |
|
---|
563 | /* brute force rulez */
|
---|
564 | unsigned iIf;
|
---|
565 | for (iIf = 0; iIf < 256; iIf++)
|
---|
566 | usbProxyLinuxSetConnected(pProxyDev, iIf, false, true);
|
---|
567 |
|
---|
568 | /*
|
---|
569 | * Determin the active configuration.
|
---|
570 | *
|
---|
571 | * If there isn't any active configuration, we will get EHOSTUNREACH (113) errors
|
---|
572 | * when trying to read the device descriptors in usbProxyDevCreate. So, we'll make
|
---|
573 | * the first one active (usually 1) then.
|
---|
574 | */
|
---|
575 | pProxyDev->cIgnoreSetConfigs = 1;
|
---|
576 | int iFirstCfg;
|
---|
577 | pProxyDev->iActiveCfg = usbProxyLinuxFindActiveConfig(pProxyDev, pszAddress, &iFirstCfg);
|
---|
578 | if (pProxyDev->iActiveCfg == -1)
|
---|
579 | {
|
---|
580 | usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_SETCONFIGURATION, &iFirstCfg, false, UINT32_MAX);
|
---|
581 | pProxyDev->iActiveCfg = usbProxyLinuxFindActiveConfig(pProxyDev, pszAddress, NULL);
|
---|
582 | Log(("usbProxyLinuxOpen: No active config! Tried to set %d: iActiveCfg=%d\n", iFirstCfg, pProxyDev->iActiveCfg));
|
---|
583 | }
|
---|
584 |
|
---|
585 | LogFlow(("usbProxyLinuxOpen(%p, %s): returns successfully File=%d iActiveCfg=%d\n",
|
---|
586 | pProxyDev, pszAddress, pDevLnx->File, pProxyDev->iActiveCfg));
|
---|
587 |
|
---|
588 | return VINF_SUCCESS;
|
---|
589 | }
|
---|
590 |
|
---|
591 | RTMemFree(pDevLnx);
|
---|
592 | }
|
---|
593 | else
|
---|
594 | rc = VERR_NO_MEMORY;
|
---|
595 | RTFileClose(File);
|
---|
596 | }
|
---|
597 | else if (rc == VERR_ACCESS_DENIED)
|
---|
598 | rc = VERR_VUSB_USBFS_PERMISSION;
|
---|
599 |
|
---|
600 | Log(("usbProxyLinuxOpen(%p, %s) failed, rc=%d!\n", pProxyDev, pszAddress, rc));
|
---|
601 | pProxyDev->Backend.pv = NULL;
|
---|
602 |
|
---|
603 | NOREF(pvBackend);
|
---|
604 | return rc;
|
---|
605 | }
|
---|
606 |
|
---|
607 |
|
---|
608 | /**
|
---|
609 | * Closes the proxy device.
|
---|
610 | */
|
---|
611 | static void usbProxyLinuxClose(PUSBPROXYDEV pProxyDev)
|
---|
612 | {
|
---|
613 | LogFlow(("usbProxyLinuxClose: pProxyDev=%p[%s]\n", pProxyDev, pProxyDev->Dev.pszName));
|
---|
614 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
615 | Assert(pDevLnx);
|
---|
616 | if (!pDevLnx)
|
---|
617 | return;
|
---|
618 |
|
---|
619 | /*
|
---|
620 | * Try put the device in a state which linux can cope with before we release it.
|
---|
621 | * Resetting it would be a nice start, although we must remember
|
---|
622 | * that it might have been disconnected...
|
---|
623 | */
|
---|
624 | /* ASSUMES: thread == EMT */
|
---|
625 | if (!usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RESET, NULL, false, 10))
|
---|
626 | {
|
---|
627 | /* Connect drivers. */
|
---|
628 | unsigned iIf;
|
---|
629 | for (iIf = 0; iIf < 256; iIf++)
|
---|
630 | usbProxyLinuxSetConnected(pProxyDev, iIf, true, true);
|
---|
631 | }
|
---|
632 | else
|
---|
633 | Log(("usbProxyLinuxClose: Reset failed, errno=%d.\n", errno));
|
---|
634 |
|
---|
635 | /*
|
---|
636 | * Now we can close it and free all the resources.
|
---|
637 | */
|
---|
638 | RTFileClose(pDevLnx->File);
|
---|
639 | pDevLnx->File = NIL_RTFILE;
|
---|
640 |
|
---|
641 | RTCritSectDelete(&pDevLnx->CritSect);
|
---|
642 |
|
---|
643 | PUSBPROXYURBLNX pUrbLnx;
|
---|
644 | while ((pUrbLnx = pDevLnx->pInFlightHead) != NULL)
|
---|
645 | {
|
---|
646 | pDevLnx->pInFlightHead = pUrbLnx->pNext;
|
---|
647 | if ( usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pUrbLnx->KUrb, false, UINT32_MAX)
|
---|
648 | && errno != ENODEV
|
---|
649 | && errno != ENOENT)
|
---|
650 | AssertMsgFailed(("errno=%d\n", errno));
|
---|
651 | if (pUrbLnx->pSplitHead)
|
---|
652 | {
|
---|
653 | PUSBPROXYURBLNX pCur = pUrbLnx->pSplitNext;
|
---|
654 | while (pCur)
|
---|
655 | {
|
---|
656 | PUSBPROXYURBLNX pFree = pCur;
|
---|
657 | pCur = pFree->pSplitNext;
|
---|
658 | if ( !pFree->fSplitElementReaped
|
---|
659 | && usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pFree->KUrb, false, UINT32_MAX)
|
---|
660 | && errno != ENODEV
|
---|
661 | && errno != ENOENT)
|
---|
662 | AssertMsgFailed(("errno=%d\n", errno));
|
---|
663 | RTMemFree(pFree);
|
---|
664 | }
|
---|
665 | }
|
---|
666 | else
|
---|
667 | Assert(!pUrbLnx->pSplitNext);
|
---|
668 | RTMemFree(pUrbLnx);
|
---|
669 | }
|
---|
670 |
|
---|
671 | while ((pUrbLnx = pDevLnx->pFreeHead) != NULL)
|
---|
672 | {
|
---|
673 | pDevLnx->pFreeHead = pUrbLnx->pNext;
|
---|
674 | RTMemFree(pUrbLnx);
|
---|
675 | }
|
---|
676 |
|
---|
677 | RTMemFree(pDevLnx);
|
---|
678 | pProxyDev->Backend.pv = NULL;
|
---|
679 | LogFlow(("usbProxyLinuxClose: returns\n"));
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | #if defined(NO_PORT_RESET) && !defined(NO_LOGICAL_RECONNECT)
|
---|
684 | /**
|
---|
685 | * Look for the logically reconnected device.
|
---|
686 | * After 5 seconds we'll give up.
|
---|
687 | *
|
---|
688 | * @returns VBox status code.
|
---|
689 | * @thread Reset thread or EMT.
|
---|
690 | */
|
---|
691 | static int usb_reset_logical_reconnect(PUSBPROXYDEV pDev)
|
---|
692 | {
|
---|
693 | FILE * pFile;
|
---|
694 | uint64_t u64StartTS = RTTimeMilliTS();
|
---|
695 |
|
---|
696 | Log2(("usb_reset_logical_reconnect: pDev=%p:{.bBus=%#x, .bDevNum=%#x, .idVendor=%#x, .idProduct=%#x, .bcdDevice=%#x, .u64SerialHash=%#llx .bDevNumParent=%#x .bPort=%#x .bLevel=%#x}\n",
|
---|
697 | pDev, pDev->Info.bBus, pDev->Info.bDevNum, pDev->Info.idVendor, pDev->Info.idProduct, pDev->Info.bcdDevice,
|
---|
698 | pDev->Info.u64SerialHash, pDev->Info.bDevNumParent, pDev->Info.bPort, pDev->Info.bLevel));
|
---|
699 |
|
---|
700 | /* First, let hubd get a chance to logically reconnect the device. */
|
---|
701 | if (!RTThreadYield())
|
---|
702 | RTThreadSleep(1);
|
---|
703 |
|
---|
704 | /*
|
---|
705 | * Search for the new device address.
|
---|
706 | */
|
---|
707 | pFile = get_devices_file();
|
---|
708 | if (!pFile)
|
---|
709 | return VERR_FILE_NOT_FOUND;
|
---|
710 |
|
---|
711 | /*
|
---|
712 | * Loop until found or 5seconds have elapsed.
|
---|
713 | */
|
---|
714 | for (;;) {
|
---|
715 | struct pollfd pfd;
|
---|
716 | uint8_t tmp;
|
---|
717 | int rc;
|
---|
718 | char buf[512];
|
---|
719 | uint64_t u64Elapsed;
|
---|
720 | int got = 0;
|
---|
721 | struct usb_dev_entry id = {0};
|
---|
722 |
|
---|
723 | /*
|
---|
724 | * Since this is kernel ABI we don't need to be too fussy about
|
---|
725 | * the parsing.
|
---|
726 | */
|
---|
727 | while (fgets(buf, sizeof(buf), pFile)) {
|
---|
728 | char *psz = strchr(buf, '\n');
|
---|
729 | if ( psz == NULL ) {
|
---|
730 | AssertMsgFailed(("usb_reset_logical_reconnect: Line to long!!\n"));
|
---|
731 | break;
|
---|
732 | }
|
---|
733 | *psz = '\0';
|
---|
734 |
|
---|
735 | switch ( buf[0] ) {
|
---|
736 | case 'T': /* topology */
|
---|
737 | /* Check if we've got enough for a device. */
|
---|
738 | if (got >= 2) {
|
---|
739 | Log2(("usb_reset_logical_reconnect: {.bBus=%#x, .bDevNum=%#x, .idVendor=%#x, .idProduct=%#x, .bcdDevice=%#x, .u64SerialHash=%#llx, .bDevNumParent=%#x, .bPort=%#x, .bLevel=%#x}\n",
|
---|
740 | id.bBus, id.bDevNum, id.idVendor, id.idProduct, id.bcdDevice, id.u64SerialHash, id.bDevNumParent, id.bPort, id.bLevel));
|
---|
741 | if ( id.bDevNumParent == pDev->Info.bDevNumParent
|
---|
742 | && id.idVendor == pDev->Info.idVendor
|
---|
743 | && id.idProduct == pDev->Info.idProduct
|
---|
744 | && id.bcdDevice == pDev->Info.bcdDevice
|
---|
745 | && id.u64SerialHash == pDev->Info.u64SerialHash
|
---|
746 | && id.bBus == pDev->Info.bBus
|
---|
747 | && id.bPort == pDev->Info.bPort
|
---|
748 | && id.bLevel == pDev->Info.bLevel) {
|
---|
749 | goto l_found;
|
---|
750 | }
|
---|
751 | }
|
---|
752 |
|
---|
753 | /* restart */
|
---|
754 | got = 0;
|
---|
755 | memset(&id, 0, sizeof(id));
|
---|
756 |
|
---|
757 | /*T: Bus=04 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#= 3 Spd=1.5 MxCh= 0*/
|
---|
758 | Log2(("usb_reset_logical_reconnect: %s\n", buf));
|
---|
759 | buf[10] = '\0';
|
---|
760 | if ( !get_u8(buf + 8, &id.bBus) )
|
---|
761 | break;
|
---|
762 | buf[49] = '\0';
|
---|
763 | psz = buf + 46;
|
---|
764 | while ( *psz == ' ' )
|
---|
765 | psz++;
|
---|
766 | if ( !get_u8(psz, &id.bDevNum) )
|
---|
767 | break;
|
---|
768 |
|
---|
769 | buf[17] = '\0';
|
---|
770 | if ( !get_u8(buf + 15, &id.bLevel) )
|
---|
771 | break;
|
---|
772 | buf[25] = '\0';
|
---|
773 | if ( !get_u8(buf + 23, &id.bDevNumParent) )
|
---|
774 | break;
|
---|
775 | buf[33] = '\0';
|
---|
776 | if ( !get_u8(buf + 31, &id.bPort) )
|
---|
777 | break;
|
---|
778 | got++;
|
---|
779 | break;
|
---|
780 |
|
---|
781 | case 'P': /* product */
|
---|
782 | Log2(("usb_reset_logical_reconnect: %s\n", buf));
|
---|
783 | buf[15] = '\0';
|
---|
784 | if ( !get_x16(buf + 11, &id.idVendor) )
|
---|
785 | break;
|
---|
786 | buf[27] = '\0';
|
---|
787 | if ( !get_x16(buf + 23, &id.idProduct) )
|
---|
788 | break;
|
---|
789 | buf[34] = '\0';
|
---|
790 | if ( buf[32] == ' ' )
|
---|
791 | buf[32] = '0';
|
---|
792 | id.bcdDevice = 0;
|
---|
793 | if ( !get_x8(buf + 32, &tmp) )
|
---|
794 | break;
|
---|
795 | id.bcdDevice = tmp << 8;
|
---|
796 | if ( !get_x8(buf + 35, &tmp) )
|
---|
797 | break;
|
---|
798 | id.bcdDevice |= tmp;
|
---|
799 | got++;
|
---|
800 | break;
|
---|
801 |
|
---|
802 | case 'S': /* String descriptor */
|
---|
803 | /* Skip past "S:" and then the whitespace */
|
---|
804 | for(psz = buf + 2; *psz != '\0'; psz++)
|
---|
805 | if ( !isspace(*psz) )
|
---|
806 | break;
|
---|
807 |
|
---|
808 | /* If it is a serial number string, skip past
|
---|
809 | * "SerialNumber="
|
---|
810 | */
|
---|
811 | if ( strncmp(psz, "SerialNumber=", sizeof("SerialNumber=") - 1) )
|
---|
812 | break;
|
---|
813 |
|
---|
814 | Log2(("usb_reset_logical_reconnect: %s\n", buf));
|
---|
815 | psz += sizeof("SerialNumber=") - 1;
|
---|
816 |
|
---|
817 | usb_serial_hash(psz, &id.u64SerialHash);
|
---|
818 | break;
|
---|
819 | }
|
---|
820 | }
|
---|
821 |
|
---|
822 | /*
|
---|
823 | * Check last.
|
---|
824 | */
|
---|
825 | if ( got >= 2
|
---|
826 | && id.bDevNumParent == pDev->Info.bDevNumParent
|
---|
827 | && id.idVendor == pDev->Info.idVendor
|
---|
828 | && id.idProduct == pDev->Info.idProduct
|
---|
829 | && id.bcdDevice == pDev->Info.bcdDevice
|
---|
830 | && id.u64SerialHash == pDev->Info.u64SerialHash
|
---|
831 | && id.bBus == pDev->Info.bBus
|
---|
832 | && id.bPort == pDev->Info.bPort
|
---|
833 | && id.bLevel == pDev->Info.bLevel) {
|
---|
834 | l_found:
|
---|
835 | /* close the existing file descriptor. */
|
---|
836 | RTFileClose(pDevLnx->File);
|
---|
837 | pDevLnx->File = NIL_RTFILE;
|
---|
838 |
|
---|
839 | /* open stuff at the new address. */
|
---|
840 | pDev->Info = id;
|
---|
841 | if (usbProxyLinuxOpen(pDev, &id))
|
---|
842 | return VINF_SUCCESS;
|
---|
843 | break;
|
---|
844 | }
|
---|
845 |
|
---|
846 | /*
|
---|
847 | * Wait for a while and then check the file again.
|
---|
848 | */
|
---|
849 | u64Elapsed = RTTimeMilliTS() - u64StartTS;
|
---|
850 | if (u64Elapsed >= 5000/*ms*/)
|
---|
851 | break; /* done */
|
---|
852 |
|
---|
853 | pfd.fd = fileno(pFile);
|
---|
854 | pfd.events = POLLIN;
|
---|
855 | rc = poll(&pfd, 1, 5000 - u64Elapsed);
|
---|
856 | if (rc < 0) {
|
---|
857 | AssertMsg(errno == EINTR, ("errno=%d\n", errno));
|
---|
858 | RTThreadSleep(32); /* paranoia: don't eat cpu on failure */
|
---|
859 | }
|
---|
860 |
|
---|
861 | rewind(pFile);
|
---|
862 | } /* for loop */
|
---|
863 |
|
---|
864 | return VERR_GENERAL_FAILURE;
|
---|
865 | }
|
---|
866 | #endif /* !NO_PORT_RESET && !NO_LOGICAL_RECONNECT */
|
---|
867 |
|
---|
868 |
|
---|
869 | /**
|
---|
870 | * Reset a device.
|
---|
871 | *
|
---|
872 | * @returns VBox status code.
|
---|
873 | * @param pDev The device to reset.
|
---|
874 | */
|
---|
875 | static int usbProxyLinuxReset(PUSBPROXYDEV pProxyDev)
|
---|
876 | {
|
---|
877 | #ifdef NO_PORT_RESET
|
---|
878 | LogFlow(("usbProxyLinuxReset: pProxyDev=%p[%s] - NO_PORT_RESET\n", pProxyDev, pProxyDev->Dev.pszName));
|
---|
879 | pProxyDev->cIgnoreSetConfigs = 2;
|
---|
880 |
|
---|
881 | #else /* !NO_PORT_RESET */
|
---|
882 | LogFlow(("usbProxyLinuxReset: pProxyDev=%p[%s]\n", pProxyDev, pProxyDev->Dev.pszName));
|
---|
883 | # ifndef NO_LOGICAL_RECONNECT
|
---|
884 | ASMAtomicIncU32(&g_cResetActive);
|
---|
885 | # endif
|
---|
886 |
|
---|
887 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RESET, NULL, false, 10))
|
---|
888 | {
|
---|
889 | int rc = errno;
|
---|
890 | # ifndef NO_LOGICAL_RECONNECT
|
---|
891 | if (rc == ENODEV)
|
---|
892 | {
|
---|
893 | /*
|
---|
894 | * This usually happens because of a 'logical disconnection'.
|
---|
895 | * So, we're in for a real treat from our excellent OS now...
|
---|
896 | */
|
---|
897 | rc2 = usb_reset_logical_reconnect(pProxyDev);
|
---|
898 | if (VBOX_FAILURE(rc2))
|
---|
899 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
900 | if (VBOX_SUCCESS(rc2))
|
---|
901 | {
|
---|
902 | ASMAtomicDecU32(&g_cResetActive);
|
---|
903 | LogFlow(("usbProxyLinuxReset: returns success (after recovering disconnected device!)\n"));
|
---|
904 | return VINF_SUCCESS;
|
---|
905 | }
|
---|
906 | }
|
---|
907 | ASMAtomicDecU32(&g_cResetActive);
|
---|
908 | # endif /* NO_LOGICAL_RECONNECT */
|
---|
909 |
|
---|
910 | Log(("usb-linux: Reset failed, rc=%Vrc errno=%d.\n", RTErrConvertFromErrno(rc), rc));
|
---|
911 | pProxyDev->iActiveCfg = -1;
|
---|
912 | return RTErrConvertFromErrno(rc);
|
---|
913 | }
|
---|
914 |
|
---|
915 | # ifndef NO_LOGICAL_RECONNECT
|
---|
916 | ASMAtomicDecU32(&g_cResetActive);
|
---|
917 | # endif
|
---|
918 |
|
---|
919 | pProxyDev->cIgnoreSetConfigs = 2;
|
---|
920 | LogFlow(("usbProxyLinuxReset: returns success\n"));
|
---|
921 | #endif /* !NO_PORT_RESET */
|
---|
922 | return VINF_SUCCESS;
|
---|
923 | }
|
---|
924 |
|
---|
925 |
|
---|
926 | /**
|
---|
927 | * SET_CONFIGURATION.
|
---|
928 | *
|
---|
929 | * The caller makes sure that it's not called first time after open or reset
|
---|
930 | * with the active interface.
|
---|
931 | *
|
---|
932 | * @returns success indicator.
|
---|
933 | * @param pProxyDev The device instance data.
|
---|
934 | * @param iCfg The configuration to set.
|
---|
935 | */
|
---|
936 | static int usbProxyLinuxSetConfig(PUSBPROXYDEV pProxyDev, int iCfg)
|
---|
937 | {
|
---|
938 | LogFlow(("usbProxyLinuxSetConfig: pProxyDev=%p[%s] cfg=%#x\n",
|
---|
939 | pProxyDev, pProxyDev->Dev.pszName, iCfg));
|
---|
940 |
|
---|
941 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_SETCONFIGURATION, &iCfg, true, UINT32_MAX))
|
---|
942 | {
|
---|
943 | Log(("usb-linux: Set configuration. errno=%d\n", errno));
|
---|
944 | return false;
|
---|
945 | }
|
---|
946 | return true;
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | /**
|
---|
951 | * Claims an interface.
|
---|
952 | * @returns success indicator.
|
---|
953 | */
|
---|
954 | static int usbProxyLinuxClaimInterface(PUSBPROXYDEV pProxyDev, int iIf)
|
---|
955 | {
|
---|
956 | LogFlow(("usbProxyLinuxClaimInterface: pProxyDev=%p[%s] ifnum=%#x\n", pProxyDev, pProxyDev->Dev.pszName, iIf));
|
---|
957 | usbProxyLinuxSetConnected(pProxyDev, iIf, false, false);
|
---|
958 |
|
---|
959 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_CLAIMINTERFACE, &iIf, true, UINT32_MAX))
|
---|
960 | {
|
---|
961 | Log(("usb-linux: Claim interface. errno=%d pProxyDev=%p[%s]\n", errno, pProxyDev, pProxyDev->Dev.pszName));
|
---|
962 | return false;
|
---|
963 | }
|
---|
964 | return true;
|
---|
965 | }
|
---|
966 |
|
---|
967 |
|
---|
968 | /**
|
---|
969 | * Releases an interface.
|
---|
970 | * @returns success indicator.
|
---|
971 | */
|
---|
972 | static int usbProxyLinuxReleaseInterface(PUSBPROXYDEV pProxyDev, int iIf)
|
---|
973 | {
|
---|
974 | LogFlow(("usbProxyLinuxReleaseInterface: pProxyDev=%p[%s] ifnum=%#x\n", pProxyDev, pProxyDev->Dev.pszName, iIf));
|
---|
975 |
|
---|
976 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RELEASEINTERFACE, &iIf, true, UINT32_MAX))
|
---|
977 | {
|
---|
978 | Log(("usb-linux: Release interface, errno=%d. pProxyDev=%p[%s]\n", errno, pProxyDev, pProxyDev->Dev.pszName));
|
---|
979 | return false;
|
---|
980 | }
|
---|
981 | return true;
|
---|
982 | }
|
---|
983 |
|
---|
984 |
|
---|
985 | /**
|
---|
986 | * SET_INTERFACE.
|
---|
987 | *
|
---|
988 | * @returns success indicator.
|
---|
989 | */
|
---|
990 | static int usbProxyLinuxSetInterface(PUSBPROXYDEV pProxyDev, int iIf, int iAlt)
|
---|
991 | {
|
---|
992 | struct usbdevfs_setinterface SetIf;
|
---|
993 | LogFlow(("usbProxyLinuxSetInterface: pProxyDev=%p iIf=%#x iAlt=%#x\n", pProxyDev, iIf, iAlt));
|
---|
994 |
|
---|
995 | SetIf.interface = iIf;
|
---|
996 | SetIf.altsetting = iAlt;
|
---|
997 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_SETINTERFACE, &SetIf, true, UINT32_MAX))
|
---|
998 | {
|
---|
999 | Log(("usb-linux: Set interface, errno=%d. pProxyDev=%p[%s]\n", errno, pProxyDev, pProxyDev->Dev.pszName));
|
---|
1000 | return false;
|
---|
1001 | }
|
---|
1002 | return true;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 |
|
---|
1006 | /**
|
---|
1007 | * Clears the halted endpoint 'EndPt'.
|
---|
1008 | */
|
---|
1009 | static bool usbProxyLinuxClearHaltedEp(PUSBPROXYDEV pProxyDev, unsigned int EndPt)
|
---|
1010 | {
|
---|
1011 | LogFlow(("usbProxyLinuxClearHaltedEp: pProxyDev=%p[%s] EndPt=%u\n", pProxyDev, pProxyDev->Dev.pszName, EndPt));
|
---|
1012 |
|
---|
1013 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_CLEAR_HALT, &EndPt, true, UINT32_MAX))
|
---|
1014 | {
|
---|
1015 | /*
|
---|
1016 | * Unfortunately this doesn't work on control pipes.
|
---|
1017 | * Windows doing this on the default endpoint and possibly other pipes too,
|
---|
1018 | * so we'll feign success for ENOENT errors.
|
---|
1019 | */
|
---|
1020 | if (errno == ENOENT)
|
---|
1021 | {
|
---|
1022 | Log(("usb-linux: clear_halted_ep failed errno=%d. pProxyDev=%p[%s] ep=%d - IGNORED\n",
|
---|
1023 | errno, pProxyDev, pProxyDev->Dev.pszName, EndPt));
|
---|
1024 | return true;
|
---|
1025 | }
|
---|
1026 | Log(("usb-linux: clear_halted_ep failed errno=%d. pProxyDev=%p[%s] ep=%d\n",
|
---|
1027 | errno, pProxyDev, pProxyDev->Dev.pszName, EndPt));
|
---|
1028 | return false;
|
---|
1029 | }
|
---|
1030 | return true;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 |
|
---|
1034 | /**
|
---|
1035 | * Setup packet byte-swapping routines.
|
---|
1036 | */
|
---|
1037 | static void usbProxyLinuxUrbSwapSetup(PVUSBSETUP pSetup)
|
---|
1038 | {
|
---|
1039 | pSetup->wValue = RT_LE2H_U16(pSetup->wValue);
|
---|
1040 | pSetup->wIndex = RT_LE2H_U16(pSetup->wIndex);
|
---|
1041 | pSetup->wLength = RT_LE2H_U16(pSetup->wLength);
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 |
|
---|
1045 | /** The split size. */
|
---|
1046 | #define SPLIT_SIZE 0x2000
|
---|
1047 |
|
---|
1048 | /**
|
---|
1049 | * Try split up a VUSB URB into smaller URBs which the
|
---|
1050 | * linux kernel can deal with.
|
---|
1051 | *
|
---|
1052 | * @returns true / false.
|
---|
1053 | * @param pProxyDev The proxy device.
|
---|
1054 | * @param pUrbLnx The linux URB which was rejected because of being too big.
|
---|
1055 | * @param pUrb The VUSB URB.
|
---|
1056 | */
|
---|
1057 | static int usbProxyLinuxUrbQueueSplit(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx, PVUSBURB pUrb)
|
---|
1058 | {
|
---|
1059 | unsigned cTries;
|
---|
1060 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
1061 |
|
---|
1062 | /*
|
---|
1063 | * Split it up into SPLIT_SIZE sized blocks.
|
---|
1064 | */
|
---|
1065 | const unsigned cKUrbs = (pUrb->cbData + SPLIT_SIZE - 1) / SPLIT_SIZE;
|
---|
1066 | LogFlow(("usbProxyLinuxUrbQueueSplit: pUrb=%p cKUrbs=%d cbData=%d\n", pUrb, cKUrbs, pUrb->cbData));
|
---|
1067 |
|
---|
1068 | uint32_t cbLeft = pUrb->cbData;
|
---|
1069 | uint8_t *pb = &pUrb->abData[0];
|
---|
1070 |
|
---|
1071 | /* the first one (already allocated) */
|
---|
1072 | switch (pUrb->enmType)
|
---|
1073 | {
|
---|
1074 | default: /* shut up gcc */
|
---|
1075 | case VUSBXFERTYPE_BULK: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_BULK; break;
|
---|
1076 | case VUSBXFERTYPE_INTR: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_INTERRUPT; break;
|
---|
1077 | case VUSBXFERTYPE_ISOC: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_ISO; break;
|
---|
1078 | case VUSBXFERTYPE_MSG: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_CONTROL; break;
|
---|
1079 | }
|
---|
1080 | pUrbLnx->KUrb.endpoint = pUrb->EndPt;
|
---|
1081 | if (pUrb->enmDir == VUSBDIRECTION_IN)
|
---|
1082 | pUrbLnx->KUrb.endpoint |= 0x80;
|
---|
1083 | pUrbLnx->KUrb.status = 0;
|
---|
1084 | pUrbLnx->KUrb.flags = pUrb->fShortNotOk ? USBDEVFS_URB_SHORT_NOT_OK : 0; /* ISO_ASAP too? */
|
---|
1085 | pUrbLnx->KUrb.buffer = pb;
|
---|
1086 | pUrbLnx->KUrb.buffer_length = MIN(cbLeft, SPLIT_SIZE);
|
---|
1087 | pUrbLnx->KUrb.actual_length = 0;
|
---|
1088 | pUrbLnx->KUrb.start_frame = 0;
|
---|
1089 | pUrbLnx->KUrb.number_of_packets = 0;
|
---|
1090 | pUrbLnx->KUrb.error_count = 0;
|
---|
1091 | pUrbLnx->KUrb.signr = 0;
|
---|
1092 | pUrbLnx->KUrb.usercontext = pUrb;
|
---|
1093 | pUrbLnx->pSplitHead = pUrbLnx;
|
---|
1094 | pUrbLnx->pSplitNext = NULL;
|
---|
1095 |
|
---|
1096 | pb += pUrbLnx->KUrb.buffer_length;
|
---|
1097 | cbLeft -= pUrbLnx->KUrb.buffer_length;
|
---|
1098 |
|
---|
1099 | /* the rest. */
|
---|
1100 | unsigned i;
|
---|
1101 | PUSBPROXYURBLNX pCur = pUrbLnx;
|
---|
1102 | for (i = 1; i < cKUrbs; i++)
|
---|
1103 | {
|
---|
1104 | Assert(cbLeft != 0);
|
---|
1105 | pCur = pCur->pSplitNext = usbProxyLinuxUrbAlloc(pProxyDev, pUrbLnx);
|
---|
1106 | if (!pCur)
|
---|
1107 | {
|
---|
1108 | usbProxyLinuxUrbFreeSplitList(pProxyDev, pUrbLnx);
|
---|
1109 | return false;
|
---|
1110 | }
|
---|
1111 | Assert(pUrbLnx->pNext != pCur); Assert(pUrbLnx->pPrev != pCur); Assert(pCur->pNext == pCur->pPrev);
|
---|
1112 | Assert(pCur->pSplitHead == pUrbLnx);
|
---|
1113 | Assert(pCur->pSplitNext == NULL);
|
---|
1114 |
|
---|
1115 | pCur->KUrb = pUrbLnx->KUrb;
|
---|
1116 | pCur->KUrb.buffer = pb;
|
---|
1117 | pCur->KUrb.buffer_length = MIN(cbLeft, SPLIT_SIZE);
|
---|
1118 | pCur->KUrb.actual_length = 0;
|
---|
1119 |
|
---|
1120 | pb += pCur->KUrb.buffer_length;
|
---|
1121 | cbLeft -= pCur->KUrb.buffer_length;
|
---|
1122 | }
|
---|
1123 | Assert(cbLeft == 0);
|
---|
1124 |
|
---|
1125 | /*
|
---|
1126 | * Submit them.
|
---|
1127 | */
|
---|
1128 | bool fUnplugged = false;
|
---|
1129 | bool fFailed = false;
|
---|
1130 | pCur = pUrbLnx;
|
---|
1131 | for (i = 0; i < cKUrbs; i++, pCur = pCur->pSplitNext)
|
---|
1132 | {
|
---|
1133 | cTries = 0;
|
---|
1134 | while (ioctl(pDevLnx->File, USBDEVFS_SUBMITURB, &pCur->KUrb))
|
---|
1135 | {
|
---|
1136 | if (errno == EINTR)
|
---|
1137 | continue;
|
---|
1138 | if (errno == ENODEV)
|
---|
1139 | {
|
---|
1140 | Log(("usbProxyLinuxUrbQueueSplit: ENODEV -> unplugged. pProxyDev=%p[%s]\n", pProxyDev, pProxyDev->Dev.pszName));
|
---|
1141 | fFailed = fUnplugged = true;
|
---|
1142 | break;
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | Log(("usb-linux: Queue URB %p -> %d!!! type=%d ep=%#x buffer_length=%#x cTries=%d\n",
|
---|
1146 | pUrb, errno, pCur->KUrb.type, pCur->KUrb.endpoint, pCur->KUrb.buffer_length, cTries));
|
---|
1147 | if (errno != EBUSY && ++cTries < 3) /* this doesn't work for the floppy :/ */
|
---|
1148 | continue;
|
---|
1149 | fFailed = true;
|
---|
1150 | break;
|
---|
1151 | }
|
---|
1152 | if (fFailed)
|
---|
1153 | break;
|
---|
1154 | pCur->u64SubmitTS = RTTimeMilliTS();
|
---|
1155 | }
|
---|
1156 | if (!fFailed)
|
---|
1157 | {
|
---|
1158 | pUrb->Dev.pvProxyUrb = pUrbLnx;
|
---|
1159 | LogFlow(("usbProxyLinuxUrbQueueSplit: ok\n"));
|
---|
1160 | return true;
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | /*
|
---|
1164 | * Clean up.
|
---|
1165 | */
|
---|
1166 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
1167 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1168 |
|
---|
1169 | /* discard and reap later (walking with pUrbLnx). */
|
---|
1170 | if (pUrbLnx != pCur)
|
---|
1171 | {
|
---|
1172 | for (;;)
|
---|
1173 | {
|
---|
1174 | pUrbLnx->fCanceledBySubmit = true;
|
---|
1175 | pUrbLnx->KUrb.usercontext = NULL;
|
---|
1176 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pUrbLnx->KUrb, false, UINT32_MAX))
|
---|
1177 | {
|
---|
1178 | if (errno == ENODEV)
|
---|
1179 | fUnplugged = true;
|
---|
1180 | else if (errno == ENOENT)
|
---|
1181 | pUrbLnx->fSplitElementReaped = true;
|
---|
1182 | else
|
---|
1183 | LogRel(("SUB: Failed to discard %p! errno=%d (pUrb=%p)\n", pUrbLnx->KUrb.usercontext, errno, pUrb)); /* serious! */
|
---|
1184 | }
|
---|
1185 | if (pUrbLnx->pSplitNext == pCur)
|
---|
1186 | {
|
---|
1187 | pUrbLnx->pSplitNext = NULL;
|
---|
1188 | break;
|
---|
1189 | }
|
---|
1190 | pUrbLnx = pUrbLnx->pSplitNext; Assert(pUrbLnx);
|
---|
1191 | }
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | /* free the unsubmitted ones. */
|
---|
1195 | while (pCur)
|
---|
1196 | {
|
---|
1197 | PUSBPROXYURBLNX pFree = pCur;
|
---|
1198 | pCur = pCur->pSplitNext;
|
---|
1199 | usbProxyLinuxUrbFree(pProxyDev, pFree);
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | /* send unplug event if we failed with ENODEV originally. */
|
---|
1203 | if (fUnplugged)
|
---|
1204 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
1205 | return false;
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 |
|
---|
1209 | /**
|
---|
1210 | * @copydoc USBPROXYBACK::pfbUrbQueue
|
---|
1211 | */
|
---|
1212 | static int usbProxyLinuxUrbQueue(PVUSBURB pUrb)
|
---|
1213 | {
|
---|
1214 | unsigned cTries;
|
---|
1215 | PUSBPROXYDEV pProxyDev = (PUSBPROXYDEV)pUrb->pDev;
|
---|
1216 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
1217 | LogFlow(("usbProxyLinuxUrbQueue: pProxyDev=%p[%s] pUrb=%p EndPt=%d cbData=%d\n",
|
---|
1218 | pProxyDev, pProxyDev->Dev.pszName, pUrb, pUrb->EndPt, pUrb->cbData));
|
---|
1219 |
|
---|
1220 | /*
|
---|
1221 | * Allocate a linux urb.
|
---|
1222 | */
|
---|
1223 | PUSBPROXYURBLNX pUrbLnx = usbProxyLinuxUrbAlloc(pProxyDev, NULL);
|
---|
1224 | if (!pUrbLnx)
|
---|
1225 | return false;
|
---|
1226 |
|
---|
1227 | switch (pUrb->enmType)
|
---|
1228 | {
|
---|
1229 | case VUSBXFERTYPE_MSG:
|
---|
1230 | pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_CONTROL;
|
---|
1231 | if (pUrb->cbData < sizeof(VUSBSETUP))
|
---|
1232 | {
|
---|
1233 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1234 | return false;
|
---|
1235 | }
|
---|
1236 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1237 | LogFlow(("usbProxyLinuxUrbQueue: message\n"));
|
---|
1238 | break;
|
---|
1239 | case VUSBXFERTYPE_BULK:
|
---|
1240 | pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_BULK;
|
---|
1241 | break;
|
---|
1242 | case VUSBXFERTYPE_ISOC:
|
---|
1243 | pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_ISO;
|
---|
1244 | break;
|
---|
1245 | case VUSBXFERTYPE_INTR:
|
---|
1246 | pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_INTERRUPT;
|
---|
1247 | break;
|
---|
1248 | default:
|
---|
1249 | goto l_err;
|
---|
1250 | }
|
---|
1251 | pUrbLnx->KUrb.endpoint = pUrb->EndPt;
|
---|
1252 | pUrbLnx->KUrb.status = 0;
|
---|
1253 | pUrbLnx->KUrb.flags = pUrb->fShortNotOk ? USBDEVFS_URB_SHORT_NOT_OK : 0; /* ISO_ASAP too? */
|
---|
1254 | pUrbLnx->KUrb.buffer = pUrb->abData;
|
---|
1255 | pUrbLnx->KUrb.buffer_length = pUrb->cbData;
|
---|
1256 | pUrbLnx->KUrb.actual_length = 0;
|
---|
1257 | pUrbLnx->KUrb.start_frame = 0;
|
---|
1258 | pUrbLnx->KUrb.number_of_packets = 0;
|
---|
1259 | pUrbLnx->KUrb.error_count = 0;
|
---|
1260 | pUrbLnx->KUrb.signr = 0;
|
---|
1261 | pUrbLnx->KUrb.usercontext = pUrb;
|
---|
1262 | switch (pUrb->enmDir)
|
---|
1263 | {
|
---|
1264 | case VUSBDIRECTION_IN:
|
---|
1265 | pUrbLnx->KUrb.endpoint |= 0x80;
|
---|
1266 | break;
|
---|
1267 | case VUSBDIRECTION_OUT:
|
---|
1268 | break;
|
---|
1269 | default:
|
---|
1270 | AssertMsgFailed(("usbProxyLinuxUrbQueue: Invalid directorion %d\n", pUrb->enmDir));
|
---|
1271 | goto l_err;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /*
|
---|
1275 | * Submit it.
|
---|
1276 | */
|
---|
1277 | cTries = 0;
|
---|
1278 | while (ioctl(pDevLnx->File, USBDEVFS_SUBMITURB, &pUrbLnx->KUrb))
|
---|
1279 | {
|
---|
1280 | if (errno == EINTR)
|
---|
1281 | continue;
|
---|
1282 | if (errno == ENODEV)
|
---|
1283 | {
|
---|
1284 | Log(("usbProxyLinuxUrbQueue: ENODEV -> unplugged. pProxyDev=%p[%s]\n", pProxyDev, pProxyDev->Dev.pszName));
|
---|
1285 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
1286 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1287 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1288 |
|
---|
1289 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
1290 | return false;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | /*
|
---|
1294 | * usbfs has or used to have a low buffer limit (16KB) in order to prevent
|
---|
1295 | * processes wasting kmalloc'ed memory. It will return EINVAL if break that
|
---|
1296 | * limit, and we'll have to split the VUSB URB up into multiple linux URBs.
|
---|
1297 | *
|
---|
1298 | * Since this is a limit which is subject to change, we cannot check for it
|
---|
1299 | * before submitting the URB. We just have to try and fail.
|
---|
1300 | */
|
---|
1301 | if ( errno == EINVAL
|
---|
1302 | && pUrb->cbData >= 8*_1K)
|
---|
1303 | return usbProxyLinuxUrbQueueSplit(pProxyDev, pUrbLnx, pUrb);
|
---|
1304 |
|
---|
1305 | Log(("usb-linux: Queue URB %p -> %d!!! type=%d ep=%#x buffer_length=%#x cTries=%d\n",
|
---|
1306 | pUrb, errno, pUrbLnx->KUrb.type, pUrbLnx->KUrb.endpoint, pUrbLnx->KUrb.buffer_length, cTries));
|
---|
1307 | if (errno != EBUSY && ++cTries < 3) /* this doesn't work for the floppy :/ */
|
---|
1308 | continue;
|
---|
1309 | l_err:
|
---|
1310 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
1311 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1312 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1313 | return false;
|
---|
1314 | }
|
---|
1315 | pUrbLnx->u64SubmitTS = RTTimeMilliTS();
|
---|
1316 |
|
---|
1317 | LogFlow(("usbProxyLinuxUrbQueue: ok\n"));
|
---|
1318 | pUrb->Dev.pvProxyUrb = pUrbLnx;
|
---|
1319 | return true;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 |
|
---|
1323 | /**
|
---|
1324 | * Check if any or the in-flight URBs are taking too long and should be cancelled.
|
---|
1325 | *
|
---|
1326 | * Cancelling is done in three turns, first a URB is marked for timeout if it's
|
---|
1327 | * exceeding a certain time limit. Then the next time it's encountered it is actually
|
---|
1328 | * cancelled. The idea now is that it's supposed to be reaped and returned in the next
|
---|
1329 | * round of calls.
|
---|
1330 | *
|
---|
1331 | * @param pProxyDev The proxy device.
|
---|
1332 | * @param pDevLnx The linux backend data.
|
---|
1333 | *
|
---|
1334 | * @todo Make the HCI do proper timeout handling! Current timeout is 3 min and 20 seconds
|
---|
1335 | * as not to break bloomberg which queues IN packages with 3 min timeouts.
|
---|
1336 | */
|
---|
1337 | static void vusbProxyLinuxUrbDoTimeouts(PUSBPROXYDEV pProxyDev, PUSBPROXYDEVLNX pDevLnx)
|
---|
1338 | {
|
---|
1339 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
1340 | uint64_t u64MilliTS = RTTimeMilliTS();
|
---|
1341 | PUSBPROXYURBLNX pCur;
|
---|
1342 | for (pCur = pDevLnx->pInFlightHead;
|
---|
1343 | pCur;
|
---|
1344 | pCur = pCur->pNext)
|
---|
1345 | {
|
---|
1346 | if (pCur->fTimedOut)
|
---|
1347 | {
|
---|
1348 | if (pCur->pSplitHead)
|
---|
1349 | {
|
---|
1350 | /* split */
|
---|
1351 | Assert(pCur == pCur->pSplitHead);
|
---|
1352 | unsigned cFailures = 0;
|
---|
1353 | PUSBPROXYURBLNX pCur2;
|
---|
1354 | for ( pCur2 = pCur; pCur2; pCur2 = pCur2->pSplitNext)
|
---|
1355 | {
|
---|
1356 | if (pCur2->fSplitElementReaped)
|
---|
1357 | continue;
|
---|
1358 |
|
---|
1359 | if ( !usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pCur2->KUrb, true, UINT32_MAX)
|
---|
1360 | || errno == ENOENT)
|
---|
1361 | pCur2->fCanceledByTimedOut = true;
|
---|
1362 | else if (errno != ENODEV)
|
---|
1363 | Log(("vusbProxyLinuxUrbDoTimeouts: pUrb=%p failed errno=%d (!!split!!)\n", pCur2->KUrb.usercontext, errno));
|
---|
1364 | else
|
---|
1365 | goto l_leave; /* ENODEV means break and everything cancelled elsewhere. */
|
---|
1366 | }
|
---|
1367 | LogRel(("USB: Cancelled URB (%p) after %lldms!! (cFailures=%d)\n",
|
---|
1368 | pCur->KUrb.usercontext, u64MilliTS - pCur->u64SubmitTS, cFailures));
|
---|
1369 | }
|
---|
1370 | else
|
---|
1371 | {
|
---|
1372 | /* unsplit */
|
---|
1373 | if ( !usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pCur->KUrb, true, UINT32_MAX)
|
---|
1374 | || errno == -ENOENT)
|
---|
1375 | {
|
---|
1376 | pCur->fCanceledByTimedOut = true;
|
---|
1377 | LogRel(("USB: Cancelled URB (%p) after %lldms!!\n", pCur->KUrb.usercontext, u64MilliTS - pCur->u64SubmitTS));
|
---|
1378 | }
|
---|
1379 | else if (errno != ENODEV)
|
---|
1380 | LogFlow(("vusbProxyLinuxUrbDoTimeouts: pUrb=%p failed errno=%d\n", pCur->KUrb.usercontext, errno));
|
---|
1381 | else
|
---|
1382 | goto l_leave; /* ENODEV means break and everything cancelled elsewhere. */
|
---|
1383 | }
|
---|
1384 | }
|
---|
1385 | else if (u64MilliTS - pCur->u64SubmitTS >= 200*1000 /* 200 sec (180 sec has been observed with XP) */)
|
---|
1386 | pCur->fTimedOut = true;
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | l_leave:
|
---|
1390 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 |
|
---|
1394 | /**
|
---|
1395 | * Get and translates the linux status to a VUSB status.
|
---|
1396 | */
|
---|
1397 | static VUSBSTATUS vusbProxyLinuxUrbGetStatus(PUSBPROXYURBLNX pUrbLnx)
|
---|
1398 | {
|
---|
1399 | switch (pUrbLnx->KUrb.status)
|
---|
1400 | {
|
---|
1401 | case 0:
|
---|
1402 | if (!pUrbLnx->fCanceledByTimedOut)
|
---|
1403 | return VUSBSTATUS_OK;
|
---|
1404 | /* fall thru */
|
---|
1405 |
|
---|
1406 | case -EILSEQ:
|
---|
1407 | return VUSBSTATUS_CRC;
|
---|
1408 |
|
---|
1409 | case -EREMOTEIO: /* ehci and ohci uses this for underflow error. */
|
---|
1410 | return VUSBSTATUS_UNDERFLOW;
|
---|
1411 |
|
---|
1412 | case -ENODEV:
|
---|
1413 | return VUSBSTATUS_DNR;
|
---|
1414 |
|
---|
1415 | //case -ECOMM:
|
---|
1416 | // return VUSBSTATUS_BUFFER_OVERRUN;
|
---|
1417 | //case -ENOSR:
|
---|
1418 | // return VUSBSTATUS_BUFFER_UNDERRUN;
|
---|
1419 |
|
---|
1420 | //case -EPROTO:
|
---|
1421 | // return VUSBSTATUS_BIT_STUFFING;
|
---|
1422 |
|
---|
1423 | default:
|
---|
1424 | Log(("usbProxyLinuxUrbReap: pKUrb status %d!!\n", pUrbLnx->KUrb.status));
|
---|
1425 | return VUSBSTATUS_STALL;
|
---|
1426 | }
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 |
|
---|
1430 | /**
|
---|
1431 | * Reap URBs in-flight on a device.
|
---|
1432 | *
|
---|
1433 | * @returns Pointer to a completed URB.
|
---|
1434 | * @returns NULL if no URB was completed.
|
---|
1435 | * @param pProxyDev The device.
|
---|
1436 | * @param cMillies Number of milliseconds to wait. Use 0 to not wait at all.
|
---|
1437 | */
|
---|
1438 | static PVUSBURB usbProxyLinuxUrbReap(PUSBPROXYDEV pProxyDev, unsigned cMillies)
|
---|
1439 | {
|
---|
1440 | PUSBPROXYURBLNX pUrbLnx = NULL;
|
---|
1441 | PUSBPROXYDEVLNX pDevLnx = (PUSBPROXYDEVLNX)pProxyDev->Backend.pv;
|
---|
1442 |
|
---|
1443 | /*
|
---|
1444 | * Any URBs pending delivery?
|
---|
1445 | */
|
---|
1446 | if (pDevLnx->pTaxingHead)
|
---|
1447 | {
|
---|
1448 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
1449 | pUrbLnx = pDevLnx->pTaxingHead;
|
---|
1450 | if (pUrbLnx)
|
---|
1451 | {
|
---|
1452 | /* unlink from the pending delivery list */
|
---|
1453 | if (pUrbLnx->pNext)
|
---|
1454 | {
|
---|
1455 | pUrbLnx->pNext->pPrev = NULL;
|
---|
1456 | pDevLnx->pTaxingHead = pUrbLnx->pNext;
|
---|
1457 | }
|
---|
1458 | else
|
---|
1459 | pDevLnx->pTaxingHead = pDevLnx->pTaxingTail = NULL;
|
---|
1460 |
|
---|
1461 | /* temporarily into the active list, so free works right. */
|
---|
1462 | pUrbLnx->pPrev = NULL;
|
---|
1463 | pUrbLnx->pNext = pDevLnx->pInFlightHead;
|
---|
1464 | if (pUrbLnx->pNext)
|
---|
1465 | pUrbLnx->pNext->pPrev = pUrbLnx;
|
---|
1466 | pDevLnx->pInFlightHead = pUrbLnx;
|
---|
1467 | }
|
---|
1468 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
1469 | }
|
---|
1470 | if (!pUrbLnx)
|
---|
1471 | {
|
---|
1472 | /*
|
---|
1473 | * Don't block if nothing is in the air.
|
---|
1474 | */
|
---|
1475 | if (!pDevLnx->pInFlightHead)
|
---|
1476 | return NULL;
|
---|
1477 |
|
---|
1478 | /*
|
---|
1479 | * Block for requested period.
|
---|
1480 | *
|
---|
1481 | * It seems to me that the path of poll() is shorter and
|
---|
1482 | * involves less semaphores than ioctl() on usbfs. So, we'll
|
---|
1483 | * do a poll regardless of whether cMillies == 0 or not.
|
---|
1484 | */
|
---|
1485 | if (cMillies)
|
---|
1486 | {
|
---|
1487 |
|
---|
1488 | for (;;)
|
---|
1489 | {
|
---|
1490 | struct pollfd pfd;
|
---|
1491 | int rc;
|
---|
1492 |
|
---|
1493 | pfd.fd = pDevLnx->File;
|
---|
1494 | pfd.events = POLLOUT | POLLWRNORM /* completed async */
|
---|
1495 | | POLLERR | POLLHUP /* disconnected */;
|
---|
1496 | pfd.revents = 0;
|
---|
1497 | rc = poll(&pfd, 1, cMillies);
|
---|
1498 | Log(("usbProxyLinuxUrbReap: poll rc = %d\n", rc));
|
---|
1499 | if (rc >= 1)
|
---|
1500 | break;
|
---|
1501 | if (rc >= 0 /*|| errno == ETIMEOUT*/)
|
---|
1502 | {
|
---|
1503 | vusbProxyLinuxUrbDoTimeouts(pProxyDev, pDevLnx);
|
---|
1504 | return NULL;
|
---|
1505 | }
|
---|
1506 | if (errno != EAGAIN)
|
---|
1507 | {
|
---|
1508 | Log(("usb-linux: Reap URB - poll -> %d errno=%d pProxyDev=%p[%s]\n", rc, errno, pProxyDev, pProxyDev->Dev.pszName));
|
---|
1509 | return NULL;
|
---|
1510 | }
|
---|
1511 | Log(("usbProxyLinuxUrbReap: poll again - weird!!!\n"));
|
---|
1512 | }
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 | /*
|
---|
1516 | * Reap URBs, non-blocking.
|
---|
1517 | */
|
---|
1518 | for (;;)
|
---|
1519 | {
|
---|
1520 | struct usbdevfs_urb *pKUrb;
|
---|
1521 | while (ioctl(pDevLnx->File, USBDEVFS_REAPURBNDELAY, &pKUrb))
|
---|
1522 | if (errno != EINTR)
|
---|
1523 | {
|
---|
1524 | if (errno == ENODEV)
|
---|
1525 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
1526 | else if (errno == EAGAIN)
|
---|
1527 | vusbProxyLinuxUrbDoTimeouts(pProxyDev, pDevLnx);
|
---|
1528 | else
|
---|
1529 | Log(("usb-linux: Reap URB. errno=%d pProxyDev=%p[%s]\n", errno, pProxyDev, pProxyDev->Dev.pszName));
|
---|
1530 | return NULL;
|
---|
1531 | }
|
---|
1532 | pUrbLnx = (PUSBPROXYURBLNX)pKUrb;
|
---|
1533 |
|
---|
1534 | /* split list: Is the entire split list done yet? */
|
---|
1535 | if (pUrbLnx->pSplitHead)
|
---|
1536 | {
|
---|
1537 | pUrbLnx->fSplitElementReaped = true;
|
---|
1538 | PUSBPROXYURBLNX pCur;
|
---|
1539 | for (pCur = pUrbLnx->pSplitHead; pCur; pCur = pCur->pSplitNext)
|
---|
1540 | if (!pCur->fSplitElementReaped)
|
---|
1541 | {
|
---|
1542 | pUrbLnx = NULL;
|
---|
1543 | break;
|
---|
1544 | }
|
---|
1545 | if (!pUrbLnx)
|
---|
1546 | continue;
|
---|
1547 | pUrbLnx = pUrbLnx->pSplitHead;
|
---|
1548 | }
|
---|
1549 | break;
|
---|
1550 | }
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | /*
|
---|
1554 | * Ok, we got one!
|
---|
1555 | */
|
---|
1556 | PVUSBURB pUrb = (PVUSBURB)pUrbLnx->KUrb.usercontext;
|
---|
1557 | if ( pUrb
|
---|
1558 | && !pUrbLnx->fCanceledBySubmit)
|
---|
1559 | {
|
---|
1560 | if (pUrbLnx->pSplitHead)
|
---|
1561 | {
|
---|
1562 | /* split - find the end byte and the first error status. */
|
---|
1563 | Assert(pUrbLnx == pUrbLnx->pSplitHead);
|
---|
1564 | uint8_t *pbEnd = &pUrb->abData[0];
|
---|
1565 | pUrb->enmStatus = VUSBSTATUS_OK;
|
---|
1566 | PUSBPROXYURBLNX pCur;
|
---|
1567 | for (pCur = pUrbLnx; pCur; pCur = pCur->pSplitNext)
|
---|
1568 | {
|
---|
1569 | if (pCur->KUrb.actual_length)
|
---|
1570 | pbEnd = (uint8_t *)pCur->KUrb.buffer + pCur->KUrb.actual_length;
|
---|
1571 | if (pUrb->enmStatus == VUSBSTATUS_OK)
|
---|
1572 | pUrb->enmStatus = vusbProxyLinuxUrbGetStatus(pCur);
|
---|
1573 | }
|
---|
1574 | pUrb->cbData = pbEnd - &pUrb->abData[0];
|
---|
1575 | usbProxyLinuxUrbFreeSplitList(pProxyDev, pUrbLnx);
|
---|
1576 | }
|
---|
1577 | else
|
---|
1578 | {
|
---|
1579 | /* unsplit. */
|
---|
1580 | pUrb->enmStatus = vusbProxyLinuxUrbGetStatus(pUrbLnx);
|
---|
1581 | pUrb->cbData = pUrbLnx->KUrb.actual_length;
|
---|
1582 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1583 | }
|
---|
1584 | pUrb->Dev.pvProxyUrb = NULL;
|
---|
1585 |
|
---|
1586 | /* some adjustments for message transfers. */
|
---|
1587 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
1588 | {
|
---|
1589 | pUrb->cbData += sizeof(VUSBSETUP);
|
---|
1590 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1591 | }
|
---|
1592 | }
|
---|
1593 | else
|
---|
1594 | {
|
---|
1595 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1596 | pUrb = NULL;
|
---|
1597 | }
|
---|
1598 |
|
---|
1599 | LogFlow(("usbProxyLinuxUrbReap: dev=%p[%s] returns %p\n", pProxyDev, pProxyDev->Dev.pszName, pUrb));
|
---|
1600 | return pUrb;
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 |
|
---|
1604 | /**
|
---|
1605 | * Cancels the URB.
|
---|
1606 | * The URB requires reaping, so we don't change its state.
|
---|
1607 | */
|
---|
1608 | static void usbProxyLinuxUrbCancel(PVUSBURB pUrb)
|
---|
1609 | {
|
---|
1610 | PUSBPROXYDEV pProxyDev = (PUSBPROXYDEV)pUrb->pDev;
|
---|
1611 | PUSBPROXYURBLNX pUrbLnx = (PUSBPROXYURBLNX)pUrb->Dev.pvProxyUrb;
|
---|
1612 | if (pUrbLnx->pSplitHead)
|
---|
1613 | {
|
---|
1614 | /* split */
|
---|
1615 | Assert(pUrbLnx == pUrbLnx->pSplitHead);
|
---|
1616 | PUSBPROXYURBLNX pCur;
|
---|
1617 | for (pCur = pUrbLnx; pCur; pCur = pCur->pSplitNext)
|
---|
1618 | {
|
---|
1619 | if (pCur->fSplitElementReaped)
|
---|
1620 | continue;
|
---|
1621 | if ( !usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pCur->KUrb, true, UINT32_MAX)
|
---|
1622 | || errno == ENOENT)
|
---|
1623 | continue;
|
---|
1624 | if (errno == ENODEV)
|
---|
1625 | break;
|
---|
1626 | Log(("usb-linux: Discard URB %p failed, errno=%d. pProxyDev=%p[%s]!!! (split)\n",
|
---|
1627 | pUrb, errno, pProxyDev, pProxyDev->Dev.pszName));
|
---|
1628 | }
|
---|
1629 | }
|
---|
1630 | else
|
---|
1631 | {
|
---|
1632 | /* unsplit */
|
---|
1633 | if ( usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pUrbLnx->KUrb, true, UINT32_MAX)
|
---|
1634 | && errno != ENODEV /* deal with elsewhere. */
|
---|
1635 | && errno != ENOENT)
|
---|
1636 | Log(("usb-linux: Discard URB %p failed, errno=%d. pProxyDev=%p[%s]!!!\n",
|
---|
1637 | pUrb, errno, pProxyDev, pProxyDev->Dev.pszName));
|
---|
1638 | }
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 |
|
---|
1642 | /**
|
---|
1643 | * The Linux USB Proxy Backend.
|
---|
1644 | */
|
---|
1645 | #ifdef __cplusplus
|
---|
1646 | extern
|
---|
1647 | #endif
|
---|
1648 | const USBPROXYBACK g_USBProxyDeviceHost =
|
---|
1649 | {
|
---|
1650 | "host",
|
---|
1651 | usbProxyLinuxOpen,
|
---|
1652 | usbProxyLinuxClose,
|
---|
1653 | usbProxyLinuxReset,
|
---|
1654 | usbProxyLinuxSetConfig,
|
---|
1655 | usbProxyLinuxClaimInterface,
|
---|
1656 | usbProxyLinuxReleaseInterface,
|
---|
1657 | usbProxyLinuxSetInterface,
|
---|
1658 | usbProxyLinuxClearHaltedEp,
|
---|
1659 | usbProxyLinuxUrbQueue,
|
---|
1660 | usbProxyLinuxUrbCancel,
|
---|
1661 | usbProxyLinuxUrbReap,
|
---|
1662 | 0
|
---|
1663 | };
|
---|
1664 |
|
---|
1665 |
|
---|
1666 | /*
|
---|
1667 | * Local Variables:
|
---|
1668 | * mode: c
|
---|
1669 | * c-file-style: "bsd"
|
---|
1670 | * c-basic-offset: 4
|
---|
1671 | * tab-width: 4
|
---|
1672 | * indent-tabs-mode: s
|
---|
1673 | * End:
|
---|
1674 | */
|
---|
1675 |
|
---|