1 | /* $Id: USBProxyDevice-linux.cpp 50234 2014-01-24 22:48:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * USB device proxy - the Linux backend.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 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 | * Defined Constants And Macros *
|
---|
20 | *******************************************************************************/
|
---|
21 | /** Define NO_PORT_RESET to skip the slow and broken linux port reset.
|
---|
22 | * Resetting will break PalmOne. */
|
---|
23 | #define NO_PORT_RESET
|
---|
24 | /** Define NO_LOGICAL_RECONNECT to skip the broken logical reconnect handling. */
|
---|
25 | #define NO_LOGICAL_RECONNECT
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #define LOG_GROUP LOG_GROUP_DRV_USBPROXY
|
---|
32 |
|
---|
33 | #include <iprt/stdint.h>
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/pipe.h>
|
---|
36 |
|
---|
37 | #include <sys/types.h>
|
---|
38 | #include <sys/stat.h>
|
---|
39 | #include <sys/vfs.h>
|
---|
40 | #include <sys/ioctl.h>
|
---|
41 | #include <sys/poll.h>
|
---|
42 | #include <stdint.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <string.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <limits.h>
|
---|
47 | #include <unistd.h>
|
---|
48 | #include <fcntl.h>
|
---|
49 | #include <errno.h>
|
---|
50 | #ifdef VBOX_WITH_LINUX_COMPILER_H
|
---|
51 | # include <linux/compiler.h>
|
---|
52 | #endif
|
---|
53 | #include <linux/usbdevice_fs.h>
|
---|
54 | /*
|
---|
55 | * Backlevel 2.4 headers doesn't have these two defines.
|
---|
56 | * They were added some time between 2.4.21 and 2.4.26, probably in 2.4.23.
|
---|
57 | */
|
---|
58 | #ifndef USBDEVFS_DISCONNECT
|
---|
59 | # define USBDEVFS_DISCONNECT _IO('U', 22)
|
---|
60 | # define USBDEVFS_CONNECT _IO('U', 23)
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | #ifndef USBDEVFS_URB_SHORT_NOT_OK
|
---|
64 | # define USBDEVFS_URB_SHORT_NOT_OK 0 /* rhel3 doesn't have this. darn! */
|
---|
65 | #endif
|
---|
66 |
|
---|
67 |
|
---|
68 | /* FedoraCore 4 does not have the bit defined by default. */
|
---|
69 | #ifndef POLLWRNORM
|
---|
70 | # define POLLWRNORM 0x0100
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | #ifndef RDESKTOP
|
---|
74 | # include <VBox/vmm/pdm.h>
|
---|
75 | #else
|
---|
76 | # define RTCRITSECT void *
|
---|
77 | static inline int rtcsNoop() { return VINF_SUCCESS; }
|
---|
78 | # define RTCritSectInit(a) rtcsNoop()
|
---|
79 | # define RTCritSectDelete(a) rtcsNoop()
|
---|
80 | # define RTCritSectEnter(a) rtcsNoop()
|
---|
81 | # define RTCritSectLeave(a) rtcsNoop()
|
---|
82 | #endif
|
---|
83 | #include <VBox/err.h>
|
---|
84 | #include <VBox/log.h>
|
---|
85 | #include <iprt/alloc.h>
|
---|
86 | #include <iprt/assert.h>
|
---|
87 | #include <iprt/asm.h>
|
---|
88 | #include <iprt/ctype.h>
|
---|
89 | #include <iprt/file.h>
|
---|
90 | #include <iprt/linux/sysfs.h>
|
---|
91 | #include <iprt/stream.h>
|
---|
92 | #include <iprt/string.h>
|
---|
93 | #if defined(NO_PORT_RESET) && !defined(NO_LOGICAL_RECONNECT)
|
---|
94 | # include <iprt/thread.h>
|
---|
95 | #endif
|
---|
96 | #include <iprt/time.h>
|
---|
97 | #include "../USBProxyDevice.h"
|
---|
98 |
|
---|
99 | /*******************************************************************************
|
---|
100 | * Structures and Typedefs *
|
---|
101 | *******************************************************************************/
|
---|
102 | /**
|
---|
103 | * Wrapper around the linux urb request structure.
|
---|
104 | * This is required to track in-flight and landed URBs.
|
---|
105 | */
|
---|
106 | typedef struct USBPROXYURBLNX
|
---|
107 | {
|
---|
108 | /** The kernel URB data */
|
---|
109 | struct usbdevfs_urb KUrb;
|
---|
110 | /** Space filler for the isochronous packets. */
|
---|
111 | struct usbdevfs_iso_packet_desc aIsocPktsDonUseTheseUseTheOnesInKUrb[8];
|
---|
112 | /** The millisecond timestamp when this URB was submitted. */
|
---|
113 | uint64_t u64SubmitTS;
|
---|
114 | /** Pointer to the next linux URB. */
|
---|
115 | struct USBPROXYURBLNX *pNext;
|
---|
116 | /** Pointer to the previous linux URB. */
|
---|
117 | struct USBPROXYURBLNX *pPrev;
|
---|
118 | /** If we've split the VUSBURB up into multiple linux URBs, this is points to the head. */
|
---|
119 | struct USBPROXYURBLNX *pSplitHead;
|
---|
120 | /** The next linux URB if split up. */
|
---|
121 | struct USBPROXYURBLNX *pSplitNext;
|
---|
122 | /** Whether it has timed out and should be shot down on the next failing reap call. */
|
---|
123 | bool fTimedOut;
|
---|
124 | /** Indicates that this URB has been canceled by timeout and should return an CRC error. */
|
---|
125 | bool fCanceledByTimedOut;
|
---|
126 | /** Don't report these back. */
|
---|
127 | bool fCanceledBySubmit;
|
---|
128 | /** This split element is reaped. */
|
---|
129 | bool fSplitElementReaped;
|
---|
130 | /** Size to transfer in remaining fragments of a split URB */
|
---|
131 | uint32_t cbSplitRemaining;
|
---|
132 | } USBPROXYURBLNX, *PUSBPROXYURBLNX;
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Data for the linux usb proxy backend.
|
---|
136 | */
|
---|
137 | typedef struct USBPROXYDEVLNX
|
---|
138 | {
|
---|
139 | /** The open file. */
|
---|
140 | RTFILE hFile;
|
---|
141 | /** Critical section protecting the two lists. */
|
---|
142 | RTCRITSECT CritSect;
|
---|
143 | /** The list of free linux URBs. Singly linked. */
|
---|
144 | PUSBPROXYURBLNX pFreeHead;
|
---|
145 | /** The list of active linux URBs. Doubly linked.
|
---|
146 | * We must maintain this so we can properly reap URBs of a detached device.
|
---|
147 | * Only the split head will appear in this list. */
|
---|
148 | PUSBPROXYURBLNX pInFlightHead;
|
---|
149 | /** The list of landed linux URBs. Doubly linked.
|
---|
150 | * Only the split head will appear in this list. */
|
---|
151 | PUSBPROXYURBLNX pTaxingHead;
|
---|
152 | /** The tail of the landed linux URBs. */
|
---|
153 | PUSBPROXYURBLNX pTaxingTail;
|
---|
154 | /** Are we using sysfs to find the active configuration? */
|
---|
155 | bool fUsingSysfs;
|
---|
156 | /** Pipe handle for waiking up - writing end. */
|
---|
157 | RTPIPE hPipeWakeupW;
|
---|
158 | /** Pipe handle for waiking up - reading end. */
|
---|
159 | RTPIPE hPipeWakeupR;
|
---|
160 | /** The device node/sysfs path of the device.
|
---|
161 | * Used to figure out the configuration after a reset. */
|
---|
162 | char *pszPath;
|
---|
163 | } USBPROXYDEVLNX, *PUSBPROXYDEVLNX;
|
---|
164 |
|
---|
165 |
|
---|
166 | /*******************************************************************************
|
---|
167 | * Internal Functions *
|
---|
168 | *******************************************************************************/
|
---|
169 | static int usbProxyLinuxDoIoCtl(PUSBPROXYDEV pProxyDev, unsigned long iCmd, void *pvArg, bool fHandleNoDev, uint32_t cTries);
|
---|
170 | static void usbProxLinuxUrbUnplugged(PUSBPROXYDEV pProxyDev);
|
---|
171 | static void usbProxyLinuxSetConnected(PUSBPROXYDEV pProyxDev, int iIf, bool fConnect, bool fQuiet);
|
---|
172 | static PUSBPROXYURBLNX usbProxyLinuxUrbAlloc(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pSplitHead);
|
---|
173 | static void usbProxyLinuxUrbFree(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx);
|
---|
174 | static void usbProxyLinuxUrbFreeSplitList(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx);
|
---|
175 | static int usbProxyLinuxFindActiveConfig(PUSBPROXYDEV pProxyDev, const char *pszPath, int *piFirstCfg);
|
---|
176 |
|
---|
177 |
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Wrapper for the ioctl call.
|
---|
181 | *
|
---|
182 | * This wrapper will repeat the call if we get an EINTR or EAGAIN. It can also
|
---|
183 | * handle ENODEV (detached device) errors.
|
---|
184 | *
|
---|
185 | * @returns whatever ioctl returns.
|
---|
186 | * @param pProxyDev The proxy device.
|
---|
187 | * @param iCmd The ioctl command / function.
|
---|
188 | * @param pvArg The ioctl argument / data.
|
---|
189 | * @param fHandleNoDev Whether to handle ENODEV.
|
---|
190 | * @param cTries The number of retries. Use UINT32_MAX for (kind of) indefinite retries.
|
---|
191 | * @internal
|
---|
192 | */
|
---|
193 | static int usbProxyLinuxDoIoCtl(PUSBPROXYDEV pProxyDev, unsigned long iCmd, void *pvArg, bool fHandleNoDev, uint32_t cTries)
|
---|
194 | {
|
---|
195 | int rc;
|
---|
196 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
197 | do
|
---|
198 | {
|
---|
199 | do
|
---|
200 | {
|
---|
201 | rc = ioctl(RTFileToNative(pDevLnx->hFile), iCmd, pvArg);
|
---|
202 | if (rc >= 0)
|
---|
203 | return rc;
|
---|
204 | } while (errno == EINTR);
|
---|
205 |
|
---|
206 | if (errno == ENODEV && fHandleNoDev)
|
---|
207 | {
|
---|
208 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
209 | Log(("usb-linux: ENODEV -> unplugged. pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
|
---|
210 | errno = ENODEV;
|
---|
211 | break;
|
---|
212 | }
|
---|
213 | if (errno != EAGAIN)
|
---|
214 | break;
|
---|
215 | } while (cTries-- > 0);
|
---|
216 |
|
---|
217 | return rc;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * The device has been unplugged.
|
---|
223 | * Cancel all in-flight URBs and put them up for reaping.
|
---|
224 | */
|
---|
225 | static void usbProxLinuxUrbUnplugged(PUSBPROXYDEV pProxyDev)
|
---|
226 | {
|
---|
227 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Shoot down all flying URBs.
|
---|
231 | */
|
---|
232 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
233 | pProxyDev->fDetached = true;
|
---|
234 |
|
---|
235 | PUSBPROXYURBLNX pUrbTaxing = NULL;
|
---|
236 | PUSBPROXYURBLNX pUrbLnx = pDevLnx->pInFlightHead;
|
---|
237 | pDevLnx->pInFlightHead = NULL;
|
---|
238 | while (pUrbLnx)
|
---|
239 | {
|
---|
240 | PUSBPROXYURBLNX pCur = pUrbLnx;
|
---|
241 | pUrbLnx = pUrbLnx->pNext;
|
---|
242 |
|
---|
243 | ioctl(RTFileToNative(pDevLnx->hFile), USBDEVFS_DISCARDURB, &pCur->KUrb); /* not sure if this is required.. */
|
---|
244 | if (!pCur->KUrb.status)
|
---|
245 | pCur->KUrb.status = -ENODEV;
|
---|
246 |
|
---|
247 | /* insert into the taxing list. */
|
---|
248 | pCur->pPrev = NULL;
|
---|
249 | if ( !pCur->pSplitHead
|
---|
250 | || pCur == pCur->pSplitHead)
|
---|
251 | {
|
---|
252 | pCur->pNext = pUrbTaxing;
|
---|
253 | if (pUrbTaxing)
|
---|
254 | pUrbTaxing->pPrev = pCur;
|
---|
255 | pUrbTaxing = pCur;
|
---|
256 | }
|
---|
257 | else
|
---|
258 | pCur->pNext = NULL;
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* Append the URBs we shot down to the taxing queue. */
|
---|
262 | if (pUrbTaxing)
|
---|
263 | {
|
---|
264 | pUrbTaxing->pPrev = pDevLnx->pTaxingTail;
|
---|
265 | if (pUrbTaxing->pPrev)
|
---|
266 | pUrbTaxing->pPrev->pNext = pUrbTaxing;
|
---|
267 | else
|
---|
268 | pDevLnx->pTaxingTail = pDevLnx->pTaxingHead = pUrbTaxing;
|
---|
269 | }
|
---|
270 |
|
---|
271 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Set the connect state seen by kernel drivers
|
---|
277 | * @internal
|
---|
278 | */
|
---|
279 | static void usbProxyLinuxSetConnected(PUSBPROXYDEV pProxyDev, int iIf, bool fConnect, bool fQuiet)
|
---|
280 | {
|
---|
281 | if ( iIf >= 32
|
---|
282 | || !(pProxyDev->fMaskedIfs & RT_BIT(iIf)))
|
---|
283 | {
|
---|
284 | struct usbdevfs_ioctl IoCtl;
|
---|
285 | if (!fQuiet)
|
---|
286 | LogFlow(("usbProxyLinuxSetConnected: pProxyDev=%s iIf=%#x fConnect=%s\n",
|
---|
287 | usbProxyGetName(pProxyDev), iIf, fConnect ? "true" : "false"));
|
---|
288 |
|
---|
289 | IoCtl.ifno = iIf;
|
---|
290 | IoCtl.ioctl_code = fConnect ? USBDEVFS_CONNECT : USBDEVFS_DISCONNECT;
|
---|
291 | IoCtl.data = NULL;
|
---|
292 | if ( usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_IOCTL, &IoCtl, true, UINT32_MAX)
|
---|
293 | && !fQuiet)
|
---|
294 | Log(("usbProxyLinuxSetConnected: failure, errno=%d. pProxyDev=%s\n",
|
---|
295 | errno, usbProxyGetName(pProxyDev)));
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Allocates a linux URB request structure.
|
---|
302 | * @returns Pointer to an active URB request.
|
---|
303 | * @returns NULL on failure.
|
---|
304 | * @param pProxyDev The proxy device instance.
|
---|
305 | * @param pSplitHead The split list head if allocating for a split list.
|
---|
306 | */
|
---|
307 | static PUSBPROXYURBLNX usbProxyLinuxUrbAlloc(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pSplitHead)
|
---|
308 | {
|
---|
309 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
310 | PUSBPROXYURBLNX pUrbLnx;
|
---|
311 |
|
---|
312 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * Try remove a linux URB from the free list, if none there allocate a new one.
|
---|
316 | */
|
---|
317 | pUrbLnx = pDevLnx->pFreeHead;
|
---|
318 | if (pUrbLnx)
|
---|
319 | pDevLnx->pFreeHead = pUrbLnx->pNext;
|
---|
320 | else
|
---|
321 | {
|
---|
322 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
323 | pUrbLnx = (PUSBPROXYURBLNX)RTMemAlloc(sizeof(*pUrbLnx));
|
---|
324 | if (!pUrbLnx)
|
---|
325 | return NULL;
|
---|
326 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
327 | }
|
---|
328 | pUrbLnx->pSplitHead = pSplitHead;
|
---|
329 | pUrbLnx->pSplitNext = NULL;
|
---|
330 | pUrbLnx->fTimedOut = false;
|
---|
331 | pUrbLnx->fCanceledByTimedOut = false;
|
---|
332 | pUrbLnx->fCanceledBySubmit = false;
|
---|
333 | pUrbLnx->fSplitElementReaped = false;
|
---|
334 |
|
---|
335 | /*
|
---|
336 | * Link it into the active list
|
---|
337 | */
|
---|
338 | if (!pSplitHead)
|
---|
339 | {
|
---|
340 | pUrbLnx->pPrev = NULL;
|
---|
341 | pUrbLnx->pNext = pDevLnx->pInFlightHead;
|
---|
342 | if (pUrbLnx->pNext)
|
---|
343 | pUrbLnx->pNext->pPrev = pUrbLnx;
|
---|
344 | pDevLnx->pInFlightHead = pUrbLnx;
|
---|
345 | }
|
---|
346 | else
|
---|
347 | pUrbLnx->pPrev = pUrbLnx->pNext = (PUSBPROXYURBLNX)0xdead;
|
---|
348 |
|
---|
349 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
350 | return pUrbLnx;
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * Frees a linux URB request structure.
|
---|
356 | *
|
---|
357 | * @param pProxyDev The proxy device instance.
|
---|
358 | * @param pUrbLnx The linux URB to free.
|
---|
359 | */
|
---|
360 | static void usbProxyLinuxUrbFree(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx)
|
---|
361 | {
|
---|
362 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
363 |
|
---|
364 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * Remove from the active list.
|
---|
368 | */
|
---|
369 | if ( !pUrbLnx->pSplitHead
|
---|
370 | || pUrbLnx->pSplitHead == pUrbLnx)
|
---|
371 | {
|
---|
372 | if (pUrbLnx->pNext)
|
---|
373 | pUrbLnx->pNext->pPrev = pUrbLnx->pPrev;
|
---|
374 | if (pUrbLnx->pPrev)
|
---|
375 | pUrbLnx->pPrev->pNext = pUrbLnx->pNext;
|
---|
376 | else
|
---|
377 | pDevLnx->pInFlightHead = pUrbLnx->pNext;
|
---|
378 | }
|
---|
379 | pUrbLnx->pSplitHead = pUrbLnx->pSplitNext = NULL;
|
---|
380 |
|
---|
381 | /*
|
---|
382 | * Link it into the free list.
|
---|
383 | */
|
---|
384 | pUrbLnx->pPrev = NULL;
|
---|
385 | pUrbLnx->pNext = pDevLnx->pFreeHead;
|
---|
386 | pDevLnx->pFreeHead = pUrbLnx;
|
---|
387 |
|
---|
388 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
389 | }
|
---|
390 |
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Frees split list of a linux URB request structure.
|
---|
394 | *
|
---|
395 | * @param pProxyDev The proxy device instance.
|
---|
396 | * @param pUrbLnx A linux URB to in the split list to be freed.
|
---|
397 | */
|
---|
398 | static void usbProxyLinuxUrbFreeSplitList(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx)
|
---|
399 | {
|
---|
400 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
401 |
|
---|
402 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
403 |
|
---|
404 | pUrbLnx = pUrbLnx->pSplitHead;
|
---|
405 | Assert(pUrbLnx);
|
---|
406 | while (pUrbLnx)
|
---|
407 | {
|
---|
408 | PUSBPROXYURBLNX pFree = pUrbLnx;
|
---|
409 | pUrbLnx = pUrbLnx->pSplitNext;
|
---|
410 | Assert(pFree->pSplitHead);
|
---|
411 | usbProxyLinuxUrbFree(pProxyDev, pFree);
|
---|
412 | }
|
---|
413 |
|
---|
414 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
415 | }
|
---|
416 |
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * This finds the device in the /proc/bus/usb/bus/addr file and finds
|
---|
420 | * the config with an asterix.
|
---|
421 | *
|
---|
422 | * @returns The Cfg#.
|
---|
423 | * @returns -1 if no active config.
|
---|
424 | * @param pszDevNode The path to the device. We infere the location of
|
---|
425 | * the devices file, which bus and device number we're
|
---|
426 | * looking for.
|
---|
427 | * @param iFirstCfg The first configuration. (optional)
|
---|
428 | * @internal
|
---|
429 | */
|
---|
430 | static int usbProxyLinuxFindActiveConfigUsbfs(PUSBPROXYDEV pProxyDev, const char *pszDevNode, int *piFirstCfg)
|
---|
431 | {
|
---|
432 | /*
|
---|
433 | * Set return defaults.
|
---|
434 | */
|
---|
435 | int iActiveCfg = -1;
|
---|
436 | if (piFirstCfg)
|
---|
437 | *piFirstCfg = 1;
|
---|
438 |
|
---|
439 | /*
|
---|
440 | * Parse the usbfs device node path and turn it into a path to the "devices" file,
|
---|
441 | * picking up the device number and bus along the way.
|
---|
442 | */
|
---|
443 | size_t cchDevNode = strlen(pszDevNode);
|
---|
444 | char *pszDevices = (char *)RTMemDupEx(pszDevNode, cchDevNode, sizeof("devices"));
|
---|
445 | AssertReturn(pszDevices, iActiveCfg);
|
---|
446 |
|
---|
447 | /* the device number */
|
---|
448 | char *psz = pszDevices + cchDevNode;
|
---|
449 | while (*psz != '/')
|
---|
450 | psz--;
|
---|
451 | Assert(pszDevices < psz);
|
---|
452 | uint32_t uDev;
|
---|
453 | int rc = RTStrToUInt32Ex(psz + 1, NULL, 10, &uDev);
|
---|
454 | if (RT_SUCCESS(rc))
|
---|
455 | {
|
---|
456 | /* the bus number */
|
---|
457 | *psz-- = '\0';
|
---|
458 | while (*psz != '/')
|
---|
459 | psz--;
|
---|
460 | Assert(pszDevices < psz);
|
---|
461 | uint32_t uBus;
|
---|
462 | rc = RTStrToUInt32Ex(psz + 1, NULL, 10, &uBus);
|
---|
463 | if (RT_SUCCESS(rc))
|
---|
464 | {
|
---|
465 | strcpy(psz + 1, "devices");
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * Open and scan the devices file.
|
---|
469 | * We're ASSUMING that each device starts off with a 'T:' line.
|
---|
470 | */
|
---|
471 | PRTSTREAM pFile;
|
---|
472 | rc = RTStrmOpen(pszDevices, "r", &pFile);
|
---|
473 | if (RT_SUCCESS(rc))
|
---|
474 | {
|
---|
475 | char szLine[1024];
|
---|
476 | while (RT_SUCCESS(RTStrmGetLine(pFile, szLine, sizeof(szLine))))
|
---|
477 | {
|
---|
478 | /* we're only interested in 'T:' lines. */
|
---|
479 | psz = RTStrStripL(szLine);
|
---|
480 | if (psz[0] != 'T' || psz[1] != ':')
|
---|
481 | continue;
|
---|
482 |
|
---|
483 | /* Skip ahead to 'Bus' and compare */
|
---|
484 | psz = RTStrStripL(psz + 2); Assert(!strncmp(psz, RT_STR_TUPLE("Bus=")));
|
---|
485 | psz = RTStrStripL(psz + 4);
|
---|
486 | char *pszNext;
|
---|
487 | uint32_t u;
|
---|
488 | rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u); AssertRC(rc);
|
---|
489 | if (RT_FAILURE(rc))
|
---|
490 | continue;
|
---|
491 | if (u != uBus)
|
---|
492 | continue;
|
---|
493 |
|
---|
494 | /* Skip ahead to 'Dev#' and compare */
|
---|
495 | psz = strstr(psz, "Dev#="); Assert(psz);
|
---|
496 | if (!psz)
|
---|
497 | continue;
|
---|
498 | psz = RTStrStripL(psz + 5);
|
---|
499 | rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u); AssertRC(rc);
|
---|
500 | if (RT_FAILURE(rc))
|
---|
501 | continue;
|
---|
502 | if (u != uDev)
|
---|
503 | continue;
|
---|
504 |
|
---|
505 | /*
|
---|
506 | * Ok, we've found the device.
|
---|
507 | * Scan until we find a selected configuration, the next device, or EOF.
|
---|
508 | */
|
---|
509 | while (RT_SUCCESS(RTStrmGetLine(pFile, szLine, sizeof(szLine))))
|
---|
510 | {
|
---|
511 | psz = RTStrStripL(szLine);
|
---|
512 | if (psz[0] == 'T')
|
---|
513 | break;
|
---|
514 | if (psz[0] != 'C' || psz[1] != ':')
|
---|
515 | continue;
|
---|
516 | const bool fActive = psz[2] == '*';
|
---|
517 | if (!fActive && !piFirstCfg)
|
---|
518 | continue;
|
---|
519 |
|
---|
520 | /* Get the 'Cfg#' value. */
|
---|
521 | psz = strstr(psz, "Cfg#="); Assert(psz);
|
---|
522 | if (psz)
|
---|
523 | {
|
---|
524 | psz = RTStrStripL(psz + 5);
|
---|
525 | rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u); AssertRC(rc);
|
---|
526 | if (RT_SUCCESS(rc))
|
---|
527 | {
|
---|
528 | if (piFirstCfg)
|
---|
529 | {
|
---|
530 | *piFirstCfg = u;
|
---|
531 | piFirstCfg = NULL;
|
---|
532 | }
|
---|
533 | if (fActive)
|
---|
534 | iActiveCfg = u;
|
---|
535 | }
|
---|
536 | }
|
---|
537 | if (fActive)
|
---|
538 | break;
|
---|
539 | }
|
---|
540 | break;
|
---|
541 | }
|
---|
542 | RTStrmClose(pFile);
|
---|
543 | }
|
---|
544 | }
|
---|
545 | }
|
---|
546 | RTMemFree(pszDevices);
|
---|
547 |
|
---|
548 | return iActiveCfg;
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | /**
|
---|
553 | * This finds the active configuration from sysfs.
|
---|
554 | *
|
---|
555 | * @returns The Cfg#.
|
---|
556 | * @returns -1 if no active config.
|
---|
557 | * @param pszPath The sysfs path for the device.
|
---|
558 | * @param piFirstCfg The first configuration. (optional)
|
---|
559 | * @internal
|
---|
560 | */
|
---|
561 | static int usbProxyLinuxFindActiveConfigSysfs(PUSBPROXYDEV pProxyDev, const char *pszPath, int *piFirstCfg)
|
---|
562 | {
|
---|
563 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
564 | if (piFirstCfg != NULL)
|
---|
565 | *piFirstCfg = pProxyDev->paCfgDescs != NULL
|
---|
566 | ? pProxyDev->paCfgDescs[0].Core.bConfigurationValue
|
---|
567 | : 1;
|
---|
568 | return RTLinuxSysFsReadIntFile(10, "%s/bConfigurationValue", pszPath); /* returns -1 on failure */
|
---|
569 | #else /* !VBOX_USB_WITH_SYSFS */
|
---|
570 | return -1;
|
---|
571 | #endif /* !VBOX_USB_WITH_SYSFS */
|
---|
572 | }
|
---|
573 |
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * This finds the active configuration.
|
---|
577 | *
|
---|
578 | * @returns The Cfg#.
|
---|
579 | * @returns -1 if no active config.
|
---|
580 | * @param pszPath The sysfs path for the device, or the usbfs device
|
---|
581 | * node path.
|
---|
582 | * @param iFirstCfg The first configuration. (optional)
|
---|
583 | * @internal
|
---|
584 | */
|
---|
585 | static int usbProxyLinuxFindActiveConfig(PUSBPROXYDEV pProxyDev, const char *pszPath, int *piFirstCfg)
|
---|
586 | {
|
---|
587 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
588 | if (pDevLnx->fUsingSysfs)
|
---|
589 | return usbProxyLinuxFindActiveConfigSysfs(pProxyDev, pszPath, piFirstCfg);
|
---|
590 | return usbProxyLinuxFindActiveConfigUsbfs(pProxyDev, pszPath, piFirstCfg);
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * Extracts the Linux file descriptor associated with the kernel USB device.
|
---|
596 | * This is used by rdesktop-vrdp for polling for events.
|
---|
597 | * @returns the FD, or asserts and returns -1 on error
|
---|
598 | * @param pProxyDev The device instance
|
---|
599 | */
|
---|
600 | RTDECL(int) USBProxyDeviceLinuxGetFD(PUSBPROXYDEV pProxyDev)
|
---|
601 | {
|
---|
602 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
603 | AssertReturn(pDevLnx->hFile != NIL_RTFILE, -1);
|
---|
604 | return RTFileToNative(pDevLnx->hFile);
|
---|
605 | }
|
---|
606 |
|
---|
607 |
|
---|
608 | /**
|
---|
609 | * Opens the device file.
|
---|
610 | *
|
---|
611 | * @returns VBox status code.
|
---|
612 | * @param pProxyDev The device instance.
|
---|
613 | * @param pszAddress If we are using usbfs, this is the path to the
|
---|
614 | * device. If we are using sysfs, this is a string of
|
---|
615 | * the form "sysfs:<sysfs path>//device:<device node>".
|
---|
616 | * In the second case, the two paths are guaranteed
|
---|
617 | * not to contain the substring "//".
|
---|
618 | * @param pvBackend Backend specific pointer, unused for the linux backend.
|
---|
619 | */
|
---|
620 | static DECLCALLBACK(int) usbProxyLinuxOpen(PUSBPROXYDEV pProxyDev, const char *pszAddress, void *pvBackend)
|
---|
621 | {
|
---|
622 | LogFlow(("usbProxyLinuxOpen: pProxyDev=%p pszAddress=%s\n", pProxyDev, pszAddress));
|
---|
623 | const char *pszDevNode;
|
---|
624 | const char *pszPath;
|
---|
625 | size_t cchPath;
|
---|
626 | bool fUsingSysfs;
|
---|
627 |
|
---|
628 | /*
|
---|
629 | * Are we using sysfs or usbfs?
|
---|
630 | */
|
---|
631 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
632 | fUsingSysfs = strncmp(pszAddress, RT_STR_TUPLE("sysfs:")) == 0;
|
---|
633 | if (fUsingSysfs)
|
---|
634 | {
|
---|
635 | pszDevNode = strstr(pszAddress, "//device:");
|
---|
636 | if (!pszDevNode)
|
---|
637 | {
|
---|
638 | LogRel(("usbProxyLinuxOpen: Invalid device address: '%s'\n", pszAddress));
|
---|
639 | return VERR_INVALID_PARAMETER;
|
---|
640 | }
|
---|
641 |
|
---|
642 | pszPath = pszAddress + sizeof("sysfs:") - 1;
|
---|
643 | cchPath = pszDevNode - pszPath;
|
---|
644 | pszDevNode += sizeof("//device:") - 1;
|
---|
645 | }
|
---|
646 | else
|
---|
647 | #endif /* VBOX_USB_WITH_SYSFS */
|
---|
648 | {
|
---|
649 | #ifndef VBOX_USB_WITH_SYSFS
|
---|
650 | fUsingSysfs = false;
|
---|
651 | #endif
|
---|
652 | pszPath = pszDevNode = pszAddress;
|
---|
653 | cchPath = strlen(pszPath);
|
---|
654 | }
|
---|
655 |
|
---|
656 | /*
|
---|
657 | * Try open the device node.
|
---|
658 | */
|
---|
659 | RTFILE hFile;
|
---|
660 | int rc = RTFileOpen(&hFile, pszDevNode, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
661 | if (RT_SUCCESS(rc))
|
---|
662 | {
|
---|
663 | /*
|
---|
664 | * Initialize the linux backend data.
|
---|
665 | */
|
---|
666 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
667 | pDevLnx->pszPath = RTStrDupN(pszPath, cchPath);
|
---|
668 | if (pDevLnx->pszPath)
|
---|
669 | {
|
---|
670 | rc = RTPipeCreate(&pDevLnx->hPipeWakeupR, &pDevLnx->hPipeWakeupW, 0);
|
---|
671 | if (RT_SUCCESS(rc))
|
---|
672 | {
|
---|
673 | pDevLnx->fUsingSysfs = fUsingSysfs;
|
---|
674 | pDevLnx->hFile = hFile;
|
---|
675 | rc = RTCritSectInit(&pDevLnx->CritSect);
|
---|
676 | if (RT_SUCCESS(rc))
|
---|
677 | {
|
---|
678 | LogFlow(("usbProxyLinuxOpen(%p, %s): returns successfully File=%RTfile iActiveCfg=%d\n",
|
---|
679 | pProxyDev, pszAddress, pDevLnx->hFile, pProxyDev->iActiveCfg));
|
---|
680 |
|
---|
681 | return VINF_SUCCESS;
|
---|
682 | }
|
---|
683 | RTPipeClose(pDevLnx->hPipeWakeupR);
|
---|
684 | RTPipeClose(pDevLnx->hPipeWakeupW);
|
---|
685 | }
|
---|
686 | }
|
---|
687 | else
|
---|
688 | rc = VERR_NO_MEMORY;
|
---|
689 |
|
---|
690 | RTFileClose(hFile);
|
---|
691 | }
|
---|
692 | else if (rc == VERR_ACCESS_DENIED)
|
---|
693 | rc = VERR_VUSB_USBFS_PERMISSION;
|
---|
694 |
|
---|
695 | Log(("usbProxyLinuxOpen(%p, %s) failed, rc=%s!\n", pProxyDev, pszAddress,
|
---|
696 | RTErrGetShort(rc)));
|
---|
697 |
|
---|
698 | NOREF(pvBackend);
|
---|
699 | return rc;
|
---|
700 | }
|
---|
701 |
|
---|
702 |
|
---|
703 | /**
|
---|
704 | * Claims all the interfaces and figures out the
|
---|
705 | * current configuration.
|
---|
706 | *
|
---|
707 | * @returns VINF_SUCCESS.
|
---|
708 | * @param pProxyDev The proxy device.
|
---|
709 | */
|
---|
710 | static DECLCALLBACK(int) usbProxyLinuxInit(PUSBPROXYDEV pProxyDev)
|
---|
711 | {
|
---|
712 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
713 |
|
---|
714 | /*
|
---|
715 | * Brute force rulez.
|
---|
716 | * usbProxyLinuxSetConnected check for masked interfaces.
|
---|
717 | */
|
---|
718 | unsigned iIf;
|
---|
719 | for (iIf = 0; iIf < 256; iIf++)
|
---|
720 | usbProxyLinuxSetConnected(pProxyDev, iIf, false, true);
|
---|
721 |
|
---|
722 | /*
|
---|
723 | * Determine the active configuration.
|
---|
724 | *
|
---|
725 | * If there isn't any active configuration, we will get EHOSTUNREACH (113) errors
|
---|
726 | * when trying to read the device descriptors in usbProxyDevCreate. So, we'll make
|
---|
727 | * the first one active (usually 1) then.
|
---|
728 | */
|
---|
729 | pProxyDev->cIgnoreSetConfigs = 1;
|
---|
730 | int iFirstCfg;
|
---|
731 | pProxyDev->iActiveCfg = usbProxyLinuxFindActiveConfig(pProxyDev, pDevLnx->pszPath, &iFirstCfg);
|
---|
732 | if (pProxyDev->iActiveCfg == -1)
|
---|
733 | {
|
---|
734 | usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_SETCONFIGURATION, &iFirstCfg, false, UINT32_MAX);
|
---|
735 | pProxyDev->iActiveCfg = usbProxyLinuxFindActiveConfig(pProxyDev, pDevLnx->pszPath, NULL);
|
---|
736 | Log(("usbProxyLinuxInit: No active config! Tried to set %d: iActiveCfg=%d\n", iFirstCfg, pProxyDev->iActiveCfg));
|
---|
737 | }
|
---|
738 | else
|
---|
739 | Log(("usbProxyLinuxInit(%p): iActiveCfg=%d\n", pProxyDev, pProxyDev->iActiveCfg));
|
---|
740 | return VINF_SUCCESS;
|
---|
741 | }
|
---|
742 |
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Closes the proxy device.
|
---|
746 | */
|
---|
747 | static DECLCALLBACK(void) usbProxyLinuxClose(PUSBPROXYDEV pProxyDev)
|
---|
748 | {
|
---|
749 | LogFlow(("usbProxyLinuxClose: pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
|
---|
750 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
751 | AssertPtrReturnVoid(pDevLnx);
|
---|
752 |
|
---|
753 | /*
|
---|
754 | * Try put the device in a state which linux can cope with before we release it.
|
---|
755 | * Resetting it would be a nice start, although we must remember
|
---|
756 | * that it might have been disconnected...
|
---|
757 | *
|
---|
758 | * Don't reset if we're masking interfaces or if construction failed.
|
---|
759 | */
|
---|
760 | if (pProxyDev->fInited)
|
---|
761 | {
|
---|
762 | /* ASSUMES: thread == EMT */
|
---|
763 | if ( pProxyDev->fMaskedIfs
|
---|
764 | || !usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RESET, NULL, false, 10))
|
---|
765 | {
|
---|
766 | /* Connect drivers. */
|
---|
767 | unsigned iIf;
|
---|
768 | for (iIf = 0; iIf < 256; iIf++)
|
---|
769 | usbProxyLinuxSetConnected(pProxyDev, iIf, true, true);
|
---|
770 | LogRel(("USB: Successfully reset device pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
|
---|
771 | }
|
---|
772 | else if (errno != ENODEV)
|
---|
773 | LogRel(("USB: Reset failed, errno=%d, pProxyDev=%s.\n", errno, usbProxyGetName(pProxyDev)));
|
---|
774 | else
|
---|
775 | Log(("USB: Reset failed, errno=%d (ENODEV), pProxyDev=%s.\n", errno, usbProxyGetName(pProxyDev)));
|
---|
776 | }
|
---|
777 |
|
---|
778 | /*
|
---|
779 | * Now we can free all the resources and close the device.
|
---|
780 | */
|
---|
781 | RTCritSectDelete(&pDevLnx->CritSect);
|
---|
782 |
|
---|
783 | PUSBPROXYURBLNX pUrbLnx;
|
---|
784 | while ((pUrbLnx = pDevLnx->pInFlightHead) != NULL)
|
---|
785 | {
|
---|
786 | pDevLnx->pInFlightHead = pUrbLnx->pNext;
|
---|
787 | if ( usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pUrbLnx->KUrb, false, UINT32_MAX)
|
---|
788 | && errno != ENODEV
|
---|
789 | && errno != ENOENT)
|
---|
790 | AssertMsgFailed(("errno=%d\n", errno));
|
---|
791 | if (pUrbLnx->pSplitHead)
|
---|
792 | {
|
---|
793 | PUSBPROXYURBLNX pCur = pUrbLnx->pSplitNext;
|
---|
794 | while (pCur)
|
---|
795 | {
|
---|
796 | PUSBPROXYURBLNX pFree = pCur;
|
---|
797 | pCur = pFree->pSplitNext;
|
---|
798 | if ( !pFree->fSplitElementReaped
|
---|
799 | && usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pFree->KUrb, false, UINT32_MAX)
|
---|
800 | && errno != ENODEV
|
---|
801 | && errno != ENOENT)
|
---|
802 | AssertMsgFailed(("errno=%d\n", errno));
|
---|
803 | RTMemFree(pFree);
|
---|
804 | }
|
---|
805 | }
|
---|
806 | else
|
---|
807 | Assert(!pUrbLnx->pSplitNext);
|
---|
808 | RTMemFree(pUrbLnx);
|
---|
809 | }
|
---|
810 |
|
---|
811 | while ((pUrbLnx = pDevLnx->pFreeHead) != NULL)
|
---|
812 | {
|
---|
813 | pDevLnx->pFreeHead = pUrbLnx->pNext;
|
---|
814 | RTMemFree(pUrbLnx);
|
---|
815 | }
|
---|
816 |
|
---|
817 | RTFileClose(pDevLnx->hFile);
|
---|
818 | pDevLnx->hFile = NIL_RTFILE;
|
---|
819 |
|
---|
820 | RTPipeClose(pDevLnx->hPipeWakeupR);
|
---|
821 | RTPipeClose(pDevLnx->hPipeWakeupW);
|
---|
822 |
|
---|
823 | RTStrFree(pDevLnx->pszPath);
|
---|
824 |
|
---|
825 | LogFlow(("usbProxyLinuxClose: returns\n"));
|
---|
826 | }
|
---|
827 |
|
---|
828 |
|
---|
829 | #if defined(NO_PORT_RESET) && !defined(NO_LOGICAL_RECONNECT)
|
---|
830 | /**
|
---|
831 | * Look for the logically reconnected device.
|
---|
832 | * After 5 seconds we'll give up.
|
---|
833 | *
|
---|
834 | * @returns VBox status code.
|
---|
835 | * @thread Reset thread or EMT.
|
---|
836 | */
|
---|
837 | static int usb_reset_logical_reconnect(PUSBPROXYDEV pDev)
|
---|
838 | {
|
---|
839 | FILE * pFile;
|
---|
840 | uint64_t u64StartTS = RTTimeMilliTS();
|
---|
841 |
|
---|
842 | 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",
|
---|
843 | pDev, pDev->Info.bBus, pDev->Info.bDevNum, pDev->Info.idVendor, pDev->Info.idProduct, pDev->Info.bcdDevice,
|
---|
844 | pDev->Info.u64SerialHash, pDev->Info.bDevNumParent, pDev->Info.bPort, pDev->Info.bLevel));
|
---|
845 |
|
---|
846 | /* First, let hubd get a chance to logically reconnect the device. */
|
---|
847 | if (!RTThreadYield())
|
---|
848 | RTThreadSleep(1);
|
---|
849 |
|
---|
850 | /*
|
---|
851 | * Search for the new device address.
|
---|
852 | */
|
---|
853 | pFile = get_devices_file();
|
---|
854 | if (!pFile)
|
---|
855 | return VERR_FILE_NOT_FOUND;
|
---|
856 |
|
---|
857 | /*
|
---|
858 | * Loop until found or 5seconds have elapsed.
|
---|
859 | */
|
---|
860 | for (;;) {
|
---|
861 | struct pollfd pfd;
|
---|
862 | uint8_t tmp;
|
---|
863 | int rc;
|
---|
864 | char buf[512];
|
---|
865 | uint64_t u64Elapsed;
|
---|
866 | int got = 0;
|
---|
867 | struct usb_dev_entry id = {0};
|
---|
868 |
|
---|
869 | /*
|
---|
870 | * Since this is kernel ABI we don't need to be too fussy about
|
---|
871 | * the parsing.
|
---|
872 | */
|
---|
873 | while (fgets(buf, sizeof(buf), pFile)) {
|
---|
874 | char *psz = strchr(buf, '\n');
|
---|
875 | if ( psz == NULL ) {
|
---|
876 | AssertMsgFailed(("usb_reset_logical_reconnect: Line to long!!\n"));
|
---|
877 | break;
|
---|
878 | }
|
---|
879 | *psz = '\0';
|
---|
880 |
|
---|
881 | switch ( buf[0] ) {
|
---|
882 | case 'T': /* topology */
|
---|
883 | /* Check if we've got enough for a device. */
|
---|
884 | if (got >= 2) {
|
---|
885 | Log2(("usb_reset_logical_reconnect: {.bBus=%#x, .bDevNum=%#x, .idVendor=%#x, .idProduct=%#x, .bcdDevice=%#x, .u64SerialHash=%#llx, .bDevNumParent=%#x, .bPort=%#x, .bLevel=%#x}\n",
|
---|
886 | id.bBus, id.bDevNum, id.idVendor, id.idProduct, id.bcdDevice, id.u64SerialHash, id.bDevNumParent, id.bPort, id.bLevel));
|
---|
887 | if ( id.bDevNumParent == pDev->Info.bDevNumParent
|
---|
888 | && id.idVendor == pDev->Info.idVendor
|
---|
889 | && id.idProduct == pDev->Info.idProduct
|
---|
890 | && id.bcdDevice == pDev->Info.bcdDevice
|
---|
891 | && id.u64SerialHash == pDev->Info.u64SerialHash
|
---|
892 | && id.bBus == pDev->Info.bBus
|
---|
893 | && id.bPort == pDev->Info.bPort
|
---|
894 | && id.bLevel == pDev->Info.bLevel) {
|
---|
895 | goto l_found;
|
---|
896 | }
|
---|
897 | }
|
---|
898 |
|
---|
899 | /* restart */
|
---|
900 | got = 0;
|
---|
901 | memset(&id, 0, sizeof(id));
|
---|
902 |
|
---|
903 | /*T: Bus=04 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#= 3 Spd=1.5 MxCh= 0*/
|
---|
904 | Log2(("usb_reset_logical_reconnect: %s\n", buf));
|
---|
905 | buf[10] = '\0';
|
---|
906 | if ( !get_u8(buf + 8, &id.bBus) )
|
---|
907 | break;
|
---|
908 | buf[49] = '\0';
|
---|
909 | psz = buf + 46;
|
---|
910 | while ( *psz == ' ' )
|
---|
911 | psz++;
|
---|
912 | if ( !get_u8(psz, &id.bDevNum) )
|
---|
913 | break;
|
---|
914 |
|
---|
915 | buf[17] = '\0';
|
---|
916 | if ( !get_u8(buf + 15, &id.bLevel) )
|
---|
917 | break;
|
---|
918 | buf[25] = '\0';
|
---|
919 | if ( !get_u8(buf + 23, &id.bDevNumParent) )
|
---|
920 | break;
|
---|
921 | buf[33] = '\0';
|
---|
922 | if ( !get_u8(buf + 31, &id.bPort) )
|
---|
923 | break;
|
---|
924 | got++;
|
---|
925 | break;
|
---|
926 |
|
---|
927 | case 'P': /* product */
|
---|
928 | Log2(("usb_reset_logical_reconnect: %s\n", buf));
|
---|
929 | buf[15] = '\0';
|
---|
930 | if ( !get_x16(buf + 11, &id.idVendor) )
|
---|
931 | break;
|
---|
932 | buf[27] = '\0';
|
---|
933 | if ( !get_x16(buf + 23, &id.idProduct) )
|
---|
934 | break;
|
---|
935 | buf[34] = '\0';
|
---|
936 | if ( buf[32] == ' ' )
|
---|
937 | buf[32] = '0';
|
---|
938 | id.bcdDevice = 0;
|
---|
939 | if ( !get_x8(buf + 32, &tmp) )
|
---|
940 | break;
|
---|
941 | id.bcdDevice = tmp << 8;
|
---|
942 | if ( !get_x8(buf + 35, &tmp) )
|
---|
943 | break;
|
---|
944 | id.bcdDevice |= tmp;
|
---|
945 | got++;
|
---|
946 | break;
|
---|
947 |
|
---|
948 | case 'S': /* String descriptor */
|
---|
949 | /* Skip past "S:" and then the whitespace */
|
---|
950 | for(psz = buf + 2; *psz != '\0'; psz++)
|
---|
951 | if ( !RT_C_IS_SPACE(*psz) )
|
---|
952 | break;
|
---|
953 |
|
---|
954 | /* If it is a serial number string, skip past
|
---|
955 | * "SerialNumber="
|
---|
956 | */
|
---|
957 | if (strncmp(psz, RT_STR_TUPLE("SerialNumber=")))
|
---|
958 | break;
|
---|
959 |
|
---|
960 | Log2(("usb_reset_logical_reconnect: %s\n", buf));
|
---|
961 | psz += sizeof("SerialNumber=") - 1;
|
---|
962 |
|
---|
963 | usb_serial_hash(psz, &id.u64SerialHash);
|
---|
964 | break;
|
---|
965 | }
|
---|
966 | }
|
---|
967 |
|
---|
968 | /*
|
---|
969 | * Check last.
|
---|
970 | */
|
---|
971 | if ( got >= 2
|
---|
972 | && id.bDevNumParent == pDev->Info.bDevNumParent
|
---|
973 | && id.idVendor == pDev->Info.idVendor
|
---|
974 | && id.idProduct == pDev->Info.idProduct
|
---|
975 | && id.bcdDevice == pDev->Info.bcdDevice
|
---|
976 | && id.u64SerialHash == pDev->Info.u64SerialHash
|
---|
977 | && id.bBus == pDev->Info.bBus
|
---|
978 | && id.bPort == pDev->Info.bPort
|
---|
979 | && id.bLevel == pDev->Info.bLevel) {
|
---|
980 | l_found:
|
---|
981 | /* close the existing file descriptor. */
|
---|
982 | RTFileClose(pDevLnx->File);
|
---|
983 | pDevLnx->File = NIL_RTFILE;
|
---|
984 |
|
---|
985 | /* open stuff at the new address. */
|
---|
986 | pDev->Info = id;
|
---|
987 | if (usbProxyLinuxOpen(pDev, &id))
|
---|
988 | return VINF_SUCCESS;
|
---|
989 | break;
|
---|
990 | }
|
---|
991 |
|
---|
992 | /*
|
---|
993 | * Wait for a while and then check the file again.
|
---|
994 | */
|
---|
995 | u64Elapsed = RTTimeMilliTS() - u64StartTS;
|
---|
996 | if (u64Elapsed >= 5000/*ms*/)
|
---|
997 | break; /* done */
|
---|
998 |
|
---|
999 | pfd.fd = fileno(pFile);
|
---|
1000 | pfd.events = POLLIN;
|
---|
1001 | rc = poll(&pfd, 1, 5000 - u64Elapsed);
|
---|
1002 | if (rc < 0) {
|
---|
1003 | AssertMsg(errno == EINTR, ("errno=%d\n", errno));
|
---|
1004 | RTThreadSleep(32); /* paranoia: don't eat cpu on failure */
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | rewind(pFile);
|
---|
1008 | } /* for loop */
|
---|
1009 |
|
---|
1010 | return VERR_GENERAL_FAILURE;
|
---|
1011 | }
|
---|
1012 | #endif /* !NO_PORT_RESET && !NO_LOGICAL_RECONNECT */
|
---|
1013 |
|
---|
1014 |
|
---|
1015 | /**
|
---|
1016 | * Reset a device.
|
---|
1017 | *
|
---|
1018 | * @returns VBox status code.
|
---|
1019 | * @param pDev The device to reset.
|
---|
1020 | */
|
---|
1021 | static DECLCALLBACK(int) usbProxyLinuxReset(PUSBPROXYDEV pProxyDev, bool fResetOnLinux)
|
---|
1022 | {
|
---|
1023 | #ifdef NO_PORT_RESET
|
---|
1024 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
1025 |
|
---|
1026 | /*
|
---|
1027 | * Specific device resets are NOPs.
|
---|
1028 | * Root hub resets that affects all devices are executed.
|
---|
1029 | *
|
---|
1030 | * The reasoning is that when a root hub reset is done, the guest shouldn't
|
---|
1031 | * will have to re enumerate the devices after doing this kind of reset.
|
---|
1032 | * So, it doesn't really matter if a device is 'logically disconnected'.
|
---|
1033 | */
|
---|
1034 | if ( !fResetOnLinux
|
---|
1035 | || pProxyDev->fMaskedIfs)
|
---|
1036 | LogFlow(("usbProxyLinuxReset: pProxyDev=%s - NO_PORT_RESET\n", usbProxyGetName(pProxyDev)));
|
---|
1037 | else
|
---|
1038 | {
|
---|
1039 | LogFlow(("usbProxyLinuxReset: pProxyDev=%s - Real Reset!\n", usbProxyGetName(pProxyDev)));
|
---|
1040 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RESET, NULL, false, 10))
|
---|
1041 | {
|
---|
1042 | int rc = errno;
|
---|
1043 | Log(("usb-linux: Reset failed, rc=%s errno=%d.\n",
|
---|
1044 | RTErrGetShort(RTErrConvertFromErrno(rc)), rc));
|
---|
1045 | pProxyDev->iActiveCfg = -1;
|
---|
1046 | return RTErrConvertFromErrno(rc);
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | /* find the active config - damn annoying. */
|
---|
1050 | pProxyDev->iActiveCfg = usbProxyLinuxFindActiveConfig(pProxyDev, pDevLnx->pszPath, NULL);
|
---|
1051 | LogFlow(("usbProxyLinuxReset: returns successfully iActiveCfg=%d\n", pProxyDev->iActiveCfg));
|
---|
1052 | }
|
---|
1053 | pProxyDev->cIgnoreSetConfigs = 2;
|
---|
1054 |
|
---|
1055 | #else /* !NO_PORT_RESET */
|
---|
1056 |
|
---|
1057 | /*
|
---|
1058 | * This is the alternative, we will always reset when asked to do so.
|
---|
1059 | *
|
---|
1060 | * The problem we're facing here is that on reset failure linux will do
|
---|
1061 | * a 'logical reconnect' on the device. This will invalidate the current
|
---|
1062 | * handle and we'll have to reopen the device. This is problematic to say
|
---|
1063 | * the least, especially since it happens pretty often.
|
---|
1064 | */
|
---|
1065 | LogFlow(("usbProxyLinuxReset: pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
|
---|
1066 | # ifndef NO_LOGICAL_RECONNECT
|
---|
1067 | ASMAtomicIncU32(&g_cResetActive);
|
---|
1068 | # endif
|
---|
1069 |
|
---|
1070 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RESET, NULL, false, 10))
|
---|
1071 | {
|
---|
1072 | int rc = errno;
|
---|
1073 | # ifndef NO_LOGICAL_RECONNECT
|
---|
1074 | if (rc == ENODEV)
|
---|
1075 | {
|
---|
1076 | /*
|
---|
1077 | * This usually happens because of a 'logical disconnection'.
|
---|
1078 | * So, we're in for a real treat from our excellent OS now...
|
---|
1079 | */
|
---|
1080 | rc2 = usb_reset_logical_reconnect(pProxyDev);
|
---|
1081 | if (RT_FAILURE(rc2))
|
---|
1082 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
1083 | if (RT_SUCCESS(rc2))
|
---|
1084 | {
|
---|
1085 | ASMAtomicDecU32(&g_cResetActive);
|
---|
1086 | LogFlow(("usbProxyLinuxReset: returns success (after recovering disconnected device!)\n"));
|
---|
1087 | return VINF_SUCCESS;
|
---|
1088 | }
|
---|
1089 | }
|
---|
1090 | ASMAtomicDecU32(&g_cResetActive);
|
---|
1091 | # endif /* NO_LOGICAL_RECONNECT */
|
---|
1092 |
|
---|
1093 | Log(("usb-linux: Reset failed, rc=%s errno=%d.\n",
|
---|
1094 | RTErrGetShort(RTErrConvertFromErrno(rc)), rc));
|
---|
1095 | pProxyDev->iActiveCfg = -1;
|
---|
1096 | return RTErrConvertFromErrno(rc);
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | # ifndef NO_LOGICAL_RECONNECT
|
---|
1100 | ASMAtomicDecU32(&g_cResetActive);
|
---|
1101 | # endif
|
---|
1102 |
|
---|
1103 | pProxyDev->cIgnoreSetConfigs = 2;
|
---|
1104 | LogFlow(("usbProxyLinuxReset: returns success\n"));
|
---|
1105 | #endif /* !NO_PORT_RESET */
|
---|
1106 | return VINF_SUCCESS;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 |
|
---|
1110 | /**
|
---|
1111 | * SET_CONFIGURATION.
|
---|
1112 | *
|
---|
1113 | * The caller makes sure that it's not called first time after open or reset
|
---|
1114 | * with the active interface.
|
---|
1115 | *
|
---|
1116 | * @returns success indicator.
|
---|
1117 | * @param pProxyDev The device instance data.
|
---|
1118 | * @param iCfg The configuration to set.
|
---|
1119 | */
|
---|
1120 | static DECLCALLBACK(int) usbProxyLinuxSetConfig(PUSBPROXYDEV pProxyDev, int iCfg)
|
---|
1121 | {
|
---|
1122 | LogFlow(("usbProxyLinuxSetConfig: pProxyDev=%s cfg=%#x\n",
|
---|
1123 | usbProxyGetName(pProxyDev), iCfg));
|
---|
1124 |
|
---|
1125 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_SETCONFIGURATION, &iCfg, true, UINT32_MAX))
|
---|
1126 | {
|
---|
1127 | Log(("usb-linux: Set configuration. errno=%d\n", errno));
|
---|
1128 | return RTErrConvertFromErrno(errno);
|
---|
1129 | }
|
---|
1130 | return VINF_SUCCESS;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 |
|
---|
1134 | /**
|
---|
1135 | * Claims an interface.
|
---|
1136 | * @returns success indicator.
|
---|
1137 | */
|
---|
1138 | static DECLCALLBACK(int) usbProxyLinuxClaimInterface(PUSBPROXYDEV pProxyDev, int iIf)
|
---|
1139 | {
|
---|
1140 | LogFlow(("usbProxyLinuxClaimInterface: pProxyDev=%s ifnum=%#x\n", usbProxyGetName(pProxyDev), iIf));
|
---|
1141 | usbProxyLinuxSetConnected(pProxyDev, iIf, false, false);
|
---|
1142 |
|
---|
1143 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_CLAIMINTERFACE, &iIf, true, UINT32_MAX))
|
---|
1144 | {
|
---|
1145 | Log(("usb-linux: Claim interface. errno=%d pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
|
---|
1146 | return RTErrConvertFromErrno(errno);
|
---|
1147 | }
|
---|
1148 | return VINF_SUCCESS;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 |
|
---|
1152 | /**
|
---|
1153 | * Releases an interface.
|
---|
1154 | * @returns success indicator.
|
---|
1155 | */
|
---|
1156 | static int usbProxyLinuxReleaseInterface(PUSBPROXYDEV pProxyDev, int iIf)
|
---|
1157 | {
|
---|
1158 | LogFlow(("usbProxyLinuxReleaseInterface: pProxyDev=%s ifnum=%#x\n", usbProxyGetName(pProxyDev), iIf));
|
---|
1159 |
|
---|
1160 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RELEASEINTERFACE, &iIf, true, UINT32_MAX))
|
---|
1161 | {
|
---|
1162 | Log(("usb-linux: Release interface, errno=%d. pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
|
---|
1163 | return RTErrConvertFromErrno(errno);
|
---|
1164 | }
|
---|
1165 | return VINF_SUCCESS;
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 |
|
---|
1169 | /**
|
---|
1170 | * SET_INTERFACE.
|
---|
1171 | *
|
---|
1172 | * @returns success indicator.
|
---|
1173 | */
|
---|
1174 | static int usbProxyLinuxSetInterface(PUSBPROXYDEV pProxyDev, int iIf, int iAlt)
|
---|
1175 | {
|
---|
1176 | struct usbdevfs_setinterface SetIf;
|
---|
1177 | LogFlow(("usbProxyLinuxSetInterface: pProxyDev=%p iIf=%#x iAlt=%#x\n", pProxyDev, iIf, iAlt));
|
---|
1178 |
|
---|
1179 | SetIf.interface = iIf;
|
---|
1180 | SetIf.altsetting = iAlt;
|
---|
1181 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_SETINTERFACE, &SetIf, true, UINT32_MAX))
|
---|
1182 | {
|
---|
1183 | Log(("usb-linux: Set interface, errno=%d. pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
|
---|
1184 | return RTErrConvertFromErrno(errno);
|
---|
1185 | }
|
---|
1186 | return VINF_SUCCESS;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 |
|
---|
1190 | /**
|
---|
1191 | * Clears the halted endpoint 'EndPt'.
|
---|
1192 | */
|
---|
1193 | static int usbProxyLinuxClearHaltedEp(PUSBPROXYDEV pProxyDev, unsigned int EndPt)
|
---|
1194 | {
|
---|
1195 | LogFlow(("usbProxyLinuxClearHaltedEp: pProxyDev=%s EndPt=%u\n", usbProxyGetName(pProxyDev), EndPt));
|
---|
1196 |
|
---|
1197 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_CLEAR_HALT, &EndPt, true, UINT32_MAX))
|
---|
1198 | {
|
---|
1199 | /*
|
---|
1200 | * Unfortunately this doesn't work on control pipes.
|
---|
1201 | * Windows doing this on the default endpoint and possibly other pipes too,
|
---|
1202 | * so we'll feign success for ENOENT errors.
|
---|
1203 | */
|
---|
1204 | if (errno == ENOENT)
|
---|
1205 | {
|
---|
1206 | Log(("usb-linux: clear_halted_ep failed errno=%d. pProxyDev=%s ep=%d - IGNORED\n",
|
---|
1207 | errno, usbProxyGetName(pProxyDev), EndPt));
|
---|
1208 | return VINF_SUCCESS;
|
---|
1209 | }
|
---|
1210 | Log(("usb-linux: clear_halted_ep failed errno=%d. pProxyDev=%s ep=%d\n",
|
---|
1211 | errno, usbProxyGetName(pProxyDev), EndPt));
|
---|
1212 | return RTErrConvertFromErrno(errno);
|
---|
1213 | }
|
---|
1214 | return VINF_SUCCESS;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 |
|
---|
1218 | /**
|
---|
1219 | * Setup packet byte-swapping routines.
|
---|
1220 | */
|
---|
1221 | static void usbProxyLinuxUrbSwapSetup(PVUSBSETUP pSetup)
|
---|
1222 | {
|
---|
1223 | pSetup->wValue = RT_H2LE_U16(pSetup->wValue);
|
---|
1224 | pSetup->wIndex = RT_H2LE_U16(pSetup->wIndex);
|
---|
1225 | pSetup->wLength = RT_H2LE_U16(pSetup->wLength);
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 |
|
---|
1229 | /**
|
---|
1230 | * Clean up after a failed URB submit.
|
---|
1231 | */
|
---|
1232 | static void usbProxyLinuxCleanupFailedSubmit(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx, PUSBPROXYURBLNX pCur, PVUSBURB pUrb, bool *pfUnplugged)
|
---|
1233 | {
|
---|
1234 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
1235 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1236 |
|
---|
1237 | /* discard and reap later (walking with pUrbLnx). */
|
---|
1238 | if (pUrbLnx != pCur)
|
---|
1239 | {
|
---|
1240 | for (;;)
|
---|
1241 | {
|
---|
1242 | pUrbLnx->fCanceledBySubmit = true;
|
---|
1243 | pUrbLnx->KUrb.usercontext = NULL;
|
---|
1244 | if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pUrbLnx->KUrb, false, UINT32_MAX))
|
---|
1245 | {
|
---|
1246 | if (errno == ENODEV)
|
---|
1247 | *pfUnplugged = true;
|
---|
1248 | else if (errno == ENOENT)
|
---|
1249 | pUrbLnx->fSplitElementReaped = true;
|
---|
1250 | else
|
---|
1251 | LogRel(("USB: Failed to discard %p! errno=%d (pUrb=%p)\n", pUrbLnx->KUrb.usercontext, errno, pUrb)); /* serious! */
|
---|
1252 | }
|
---|
1253 | if (pUrbLnx->pSplitNext == pCur)
|
---|
1254 | {
|
---|
1255 | pUrbLnx->pSplitNext = NULL;
|
---|
1256 | break;
|
---|
1257 | }
|
---|
1258 | pUrbLnx = pUrbLnx->pSplitNext; Assert(pUrbLnx);
|
---|
1259 | }
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | /* free the unsubmitted ones. */
|
---|
1263 | while (pCur)
|
---|
1264 | {
|
---|
1265 | PUSBPROXYURBLNX pFree = pCur;
|
---|
1266 | pCur = pCur->pSplitNext;
|
---|
1267 | usbProxyLinuxUrbFree(pProxyDev, pFree);
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | /* send unplug event if we failed with ENODEV originally. */
|
---|
1271 | if (*pfUnplugged)
|
---|
1272 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | /**
|
---|
1276 | * Submit one URB through the usbfs IOCTL interface, with
|
---|
1277 | * retries
|
---|
1278 | *
|
---|
1279 | * @returns VBox status code.
|
---|
1280 | */
|
---|
1281 | static int usbProxyLinuxSubmitURB(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pCur, PVUSBURB pUrb, bool *pfUnplugged)
|
---|
1282 | {
|
---|
1283 | int rc = VINF_SUCCESS;
|
---|
1284 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
1285 | unsigned cTries = 0;
|
---|
1286 |
|
---|
1287 | while (ioctl(RTFileToNative(pDevLnx->hFile), USBDEVFS_SUBMITURB, &pCur->KUrb))
|
---|
1288 | {
|
---|
1289 | if (errno == EINTR)
|
---|
1290 | continue;
|
---|
1291 | if (errno == ENODEV)
|
---|
1292 | {
|
---|
1293 | Log(("usbProxyLinuxSubmitURB: ENODEV -> unplugged. pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
|
---|
1294 | *pfUnplugged = true;
|
---|
1295 | return RTErrConvertFromErrno(errno);
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | Log(("usb-linux: Submit URB %p -> %d!!! type=%d ep=%#x buffer_length=%#x cTries=%d\n",
|
---|
1299 | pUrb, errno, pCur->KUrb.type, pCur->KUrb.endpoint, pCur->KUrb.buffer_length, cTries));
|
---|
1300 | if (errno != EBUSY && ++cTries < 3) /* this doesn't work for the floppy :/ */
|
---|
1301 | {
|
---|
1302 | pCur->u64SubmitTS = RTTimeMilliTS();
|
---|
1303 | continue;
|
---|
1304 | }
|
---|
1305 | return RTErrConvertFromErrno(errno);
|
---|
1306 | }
|
---|
1307 | return VINF_SUCCESS;
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | /** The split size. 16K in known Linux kernel versions. */
|
---|
1311 | #define SPLIT_SIZE 0x4000
|
---|
1312 |
|
---|
1313 | /**
|
---|
1314 | * Create a URB fragment of up to SPLIT_SIZE size and hook it
|
---|
1315 | * into the list of fragments.
|
---|
1316 | *
|
---|
1317 | * @returns pointer to newly allocated URB fragment or NULL.
|
---|
1318 | */
|
---|
1319 | static PUSBPROXYURBLNX usbProxyLinuxSplitURBFragment(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pHead, PUSBPROXYURBLNX pCur)
|
---|
1320 | {
|
---|
1321 | PUSBPROXYURBLNX pNew;
|
---|
1322 | uint32_t cbLeft = pCur->cbSplitRemaining;
|
---|
1323 | uint8_t *pb = (uint8_t *)pCur->KUrb.buffer;
|
---|
1324 |
|
---|
1325 | Assert(cbLeft != 0);
|
---|
1326 | pNew = pCur->pSplitNext = usbProxyLinuxUrbAlloc(pProxyDev, pHead);
|
---|
1327 | if (!pNew)
|
---|
1328 | {
|
---|
1329 | usbProxyLinuxUrbFreeSplitList(pProxyDev, pHead);
|
---|
1330 | return NULL;
|
---|
1331 | }
|
---|
1332 | Assert(pHead->pNext != pNew); Assert(pHead->pPrev != pNew); Assert(pNew->pNext == pNew->pPrev);
|
---|
1333 | Assert(pNew->pSplitHead == pHead);
|
---|
1334 | Assert(pNew->pSplitNext == NULL);
|
---|
1335 |
|
---|
1336 | pNew->KUrb = pHead->KUrb;
|
---|
1337 | pNew->KUrb.buffer = pb + pCur->KUrb.buffer_length;
|
---|
1338 | pNew->KUrb.buffer_length = RT_MIN(cbLeft, SPLIT_SIZE);
|
---|
1339 | pNew->KUrb.actual_length = 0;
|
---|
1340 |
|
---|
1341 | cbLeft -= pNew->KUrb.buffer_length;
|
---|
1342 | Assert(cbLeft < INT32_MAX);
|
---|
1343 | pNew->cbSplitRemaining = cbLeft;
|
---|
1344 | return pNew;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | /**
|
---|
1348 | * Try splitting up a VUSB URB into smaller URBs which the
|
---|
1349 | * linux kernel (usbfs) can deal with.
|
---|
1350 | *
|
---|
1351 | * NB: For ShortOK reads things get a little tricky - we don't
|
---|
1352 | * know how much data is going to arrive and not all the
|
---|
1353 | * fragment URBs might be filled. We can only safely set up one
|
---|
1354 | * URB at a time -> worse performance but correct behaviour.
|
---|
1355 | *
|
---|
1356 | * @returns VBox status code.
|
---|
1357 | * @param pProxyDev The proxy device.
|
---|
1358 | * @param pUrbLnx The linux URB which was rejected because of being too big.
|
---|
1359 | * @param pUrb The VUSB URB.
|
---|
1360 | */
|
---|
1361 | static int usbProxyLinuxUrbQueueSplit(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx, PVUSBURB pUrb)
|
---|
1362 | {
|
---|
1363 | /*
|
---|
1364 | * Split it up into SPLIT_SIZE sized blocks.
|
---|
1365 | */
|
---|
1366 | const unsigned cKUrbs = (pUrb->cbData + SPLIT_SIZE - 1) / SPLIT_SIZE;
|
---|
1367 | LogFlow(("usbProxyLinuxUrbQueueSplit: pUrb=%p cKUrbs=%d cbData=%d\n", pUrb, cKUrbs, pUrb->cbData));
|
---|
1368 |
|
---|
1369 | uint32_t cbLeft = pUrb->cbData;
|
---|
1370 | uint8_t *pb = &pUrb->abData[0];
|
---|
1371 |
|
---|
1372 | /* the first one (already allocated) */
|
---|
1373 | switch (pUrb->enmType)
|
---|
1374 | {
|
---|
1375 | default: /* shut up gcc */
|
---|
1376 | case VUSBXFERTYPE_BULK: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_BULK; break;
|
---|
1377 | case VUSBXFERTYPE_INTR: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_INTERRUPT; break;
|
---|
1378 | case VUSBXFERTYPE_MSG: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_CONTROL; break;
|
---|
1379 | case VUSBXFERTYPE_ISOC:
|
---|
1380 | AssertMsgFailed(("We can't split isochronous URBs!\n"));
|
---|
1381 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1382 | return VERR_INVALID_PARAMETER; /** @todo: Better status code. */
|
---|
1383 | }
|
---|
1384 | pUrbLnx->KUrb.endpoint = pUrb->EndPt;
|
---|
1385 | if (pUrb->enmDir == VUSBDIRECTION_IN)
|
---|
1386 | pUrbLnx->KUrb.endpoint |= 0x80;
|
---|
1387 | pUrbLnx->KUrb.status = 0;
|
---|
1388 | pUrbLnx->KUrb.flags = pUrb->fShortNotOk ? USBDEVFS_URB_SHORT_NOT_OK : 0; /* ISO_ASAP too? */
|
---|
1389 | pUrbLnx->KUrb.buffer = pb;
|
---|
1390 | pUrbLnx->KUrb.buffer_length = RT_MIN(cbLeft, SPLIT_SIZE);
|
---|
1391 | pUrbLnx->KUrb.actual_length = 0;
|
---|
1392 | pUrbLnx->KUrb.start_frame = 0;
|
---|
1393 | pUrbLnx->KUrb.number_of_packets = 0;
|
---|
1394 | pUrbLnx->KUrb.error_count = 0;
|
---|
1395 | pUrbLnx->KUrb.signr = 0;
|
---|
1396 | pUrbLnx->KUrb.usercontext = pUrb;
|
---|
1397 | pUrbLnx->pSplitHead = pUrbLnx;
|
---|
1398 | pUrbLnx->pSplitNext = NULL;
|
---|
1399 |
|
---|
1400 | PUSBPROXYURBLNX pCur = pUrbLnx;
|
---|
1401 |
|
---|
1402 | cbLeft -= pUrbLnx->KUrb.buffer_length;
|
---|
1403 | pUrbLnx->cbSplitRemaining = cbLeft;
|
---|
1404 |
|
---|
1405 | int rc = VINF_SUCCESS;
|
---|
1406 | bool fUnplugged = false;
|
---|
1407 | if (pUrb->enmDir == VUSBDIRECTION_IN && !pUrb->fShortNotOk)
|
---|
1408 | {
|
---|
1409 | /* Subsequent fragments will be queued only after the previous fragment is reaped
|
---|
1410 | * and only if necessary.
|
---|
1411 | */
|
---|
1412 | Log(("usb-linux: Large ShortOK read, only queuing first fragment.\n"));
|
---|
1413 | Assert(pUrbLnx->cbSplitRemaining > 0 && pUrbLnx->cbSplitRemaining < 256 * _1K);
|
---|
1414 | rc = usbProxyLinuxSubmitURB(pProxyDev, pUrbLnx, pUrb, &fUnplugged);
|
---|
1415 | }
|
---|
1416 | else
|
---|
1417 | {
|
---|
1418 | /* the rest. */
|
---|
1419 | unsigned i;
|
---|
1420 | for (i = 1; i < cKUrbs; i++)
|
---|
1421 | {
|
---|
1422 | pCur = usbProxyLinuxSplitURBFragment(pProxyDev, pUrbLnx, pCur);
|
---|
1423 | if (!pCur)
|
---|
1424 | return VERR_NO_MEMORY;
|
---|
1425 | }
|
---|
1426 | Assert(pCur->cbSplitRemaining == 0);
|
---|
1427 |
|
---|
1428 | /* Submit the blocks. */
|
---|
1429 | pCur = pUrbLnx;
|
---|
1430 | for (i = 0; i < cKUrbs; i++, pCur = pCur->pSplitNext)
|
---|
1431 | {
|
---|
1432 | rc = usbProxyLinuxSubmitURB(pProxyDev, pCur, pUrb, &fUnplugged);
|
---|
1433 | if (RT_FAILURE(rc))
|
---|
1434 | break;
|
---|
1435 | }
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 | if (RT_SUCCESS(rc))
|
---|
1439 | {
|
---|
1440 | pUrb->Dev.pvPrivate = pUrbLnx;
|
---|
1441 | LogFlow(("usbProxyLinuxUrbQueueSplit: ok\n"));
|
---|
1442 | return VINF_SUCCESS;
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | usbProxyLinuxCleanupFailedSubmit(pProxyDev, pUrbLnx, pCur, pUrb, &fUnplugged);
|
---|
1446 | return rc;
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 |
|
---|
1450 | /**
|
---|
1451 | * @copydoc USBPROXYBACK::pfnUrbQueue
|
---|
1452 | */
|
---|
1453 | static DECLCALLBACK(int) usbProxyLinuxUrbQueue(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
|
---|
1454 | {
|
---|
1455 | int rc = VINF_SUCCESS;
|
---|
1456 | unsigned cTries;
|
---|
1457 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
1458 | LogFlow(("usbProxyLinuxUrbQueue: pProxyDev=%s pUrb=%p EndPt=%d cbData=%d\n",
|
---|
1459 | usbProxyGetName(pProxyDev), pUrb, pUrb->EndPt, pUrb->cbData));
|
---|
1460 |
|
---|
1461 | /*
|
---|
1462 | * Allocate a linux urb.
|
---|
1463 | */
|
---|
1464 | PUSBPROXYURBLNX pUrbLnx = usbProxyLinuxUrbAlloc(pProxyDev, NULL);
|
---|
1465 | if (!pUrbLnx)
|
---|
1466 | return VERR_NO_MEMORY;
|
---|
1467 |
|
---|
1468 | pUrbLnx->KUrb.endpoint = pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? 0x80 : 0);
|
---|
1469 | pUrbLnx->KUrb.status = 0;
|
---|
1470 | pUrbLnx->KUrb.flags = pUrb->fShortNotOk ? USBDEVFS_URB_SHORT_NOT_OK : 0;
|
---|
1471 | pUrbLnx->KUrb.buffer = pUrb->abData;
|
---|
1472 | pUrbLnx->KUrb.buffer_length = pUrb->cbData;
|
---|
1473 | pUrbLnx->KUrb.actual_length = 0;
|
---|
1474 | pUrbLnx->KUrb.start_frame = 0;
|
---|
1475 | pUrbLnx->KUrb.number_of_packets = 0;
|
---|
1476 | pUrbLnx->KUrb.error_count = 0;
|
---|
1477 | pUrbLnx->KUrb.signr = 0;
|
---|
1478 | pUrbLnx->KUrb.usercontext = pUrb;
|
---|
1479 |
|
---|
1480 | switch (pUrb->enmType)
|
---|
1481 | {
|
---|
1482 | case VUSBXFERTYPE_MSG:
|
---|
1483 | pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_CONTROL;
|
---|
1484 | if (pUrb->cbData < sizeof(VUSBSETUP))
|
---|
1485 | {
|
---|
1486 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1487 | return VERR_BUFFER_UNDERFLOW;
|
---|
1488 | }
|
---|
1489 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1490 | LogFlow(("usbProxyLinuxUrbQueue: message\n"));
|
---|
1491 | break;
|
---|
1492 | case VUSBXFERTYPE_BULK:
|
---|
1493 | pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_BULK;
|
---|
1494 | break;
|
---|
1495 | case VUSBXFERTYPE_ISOC:
|
---|
1496 | pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_ISO;
|
---|
1497 | pUrbLnx->KUrb.flags |= USBDEVFS_URB_ISO_ASAP;
|
---|
1498 | pUrbLnx->KUrb.number_of_packets = pUrb->cIsocPkts;
|
---|
1499 | unsigned i;
|
---|
1500 | for (i = 0; i < pUrb->cIsocPkts; i++)
|
---|
1501 | {
|
---|
1502 | pUrbLnx->KUrb.iso_frame_desc[i].length = pUrb->aIsocPkts[i].cb;
|
---|
1503 | pUrbLnx->KUrb.iso_frame_desc[i].actual_length = 0;
|
---|
1504 | pUrbLnx->KUrb.iso_frame_desc[i].status = 0x7fff;
|
---|
1505 | }
|
---|
1506 | break;
|
---|
1507 | case VUSBXFERTYPE_INTR:
|
---|
1508 | pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_INTERRUPT;
|
---|
1509 | break;
|
---|
1510 | default:
|
---|
1511 | rc = VERR_INVALID_PARAMETER; /** @todo: better status code. */
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | /*
|
---|
1515 | * Submit it.
|
---|
1516 | */
|
---|
1517 | cTries = 0;
|
---|
1518 | while (ioctl(RTFileToNative(pDevLnx->hFile), USBDEVFS_SUBMITURB, &pUrbLnx->KUrb))
|
---|
1519 | {
|
---|
1520 | if (errno == EINTR)
|
---|
1521 | continue;
|
---|
1522 | if (errno == ENODEV)
|
---|
1523 | {
|
---|
1524 | Log(("usbProxyLinuxUrbQueue: ENODEV -> unplugged. pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
|
---|
1525 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
1526 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1527 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1528 |
|
---|
1529 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
1530 | return RTErrConvertFromErrno(errno);
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | /*
|
---|
1534 | * usbfs has or used to have a low buffer limit (16KB) in order to prevent
|
---|
1535 | * processes wasting kmalloc'ed memory. It will return EINVAL if break that
|
---|
1536 | * limit, and we'll have to split the VUSB URB up into multiple linux URBs.
|
---|
1537 | *
|
---|
1538 | * Since this is a limit which is subject to change, we cannot check for it
|
---|
1539 | * before submitting the URB. We just have to try and fail.
|
---|
1540 | */
|
---|
1541 | if ( errno == EINVAL
|
---|
1542 | && pUrb->cbData >= 8*_1K)
|
---|
1543 | return usbProxyLinuxUrbQueueSplit(pProxyDev, pUrbLnx, pUrb);
|
---|
1544 |
|
---|
1545 | Log(("usb-linux: Queue URB %p -> %d!!! type=%d ep=%#x buffer_length=%#x cTries=%d\n",
|
---|
1546 | pUrb, errno, pUrbLnx->KUrb.type, pUrbLnx->KUrb.endpoint, pUrbLnx->KUrb.buffer_length, cTries));
|
---|
1547 | if (errno != EBUSY && ++cTries < 3) /* this doesn't work for the floppy :/ */
|
---|
1548 | continue;
|
---|
1549 |
|
---|
1550 | rc = RTErrConvertFromErrno(errno);
|
---|
1551 |
|
---|
1552 | l_err:
|
---|
1553 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
1554 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1555 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1556 | return rc;
|
---|
1557 | }
|
---|
1558 | pUrbLnx->u64SubmitTS = RTTimeMilliTS();
|
---|
1559 |
|
---|
1560 | LogFlow(("usbProxyLinuxUrbQueue: ok\n"));
|
---|
1561 | pUrb->Dev.pvPrivate = pUrbLnx;
|
---|
1562 | return rc;
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 |
|
---|
1566 | /**
|
---|
1567 | * Check if any or the in-flight URBs are taking too long and should be cancelled.
|
---|
1568 | *
|
---|
1569 | * Cancelling is done in three turns, first a URB is marked for timeout if it's
|
---|
1570 | * exceeding a certain time limit. Then the next time it's encountered it is actually
|
---|
1571 | * cancelled. The idea now is that it's supposed to be reaped and returned in the next
|
---|
1572 | * round of calls.
|
---|
1573 | *
|
---|
1574 | * @param pProxyDev The proxy device.
|
---|
1575 | * @param pDevLnx The linux backend data.
|
---|
1576 | *
|
---|
1577 | * @todo Make the HCI do proper timeout handling! Current timeout is 3 min and 20 seconds
|
---|
1578 | * as not to break bloomberg which queues IN packages with 3 min timeouts.
|
---|
1579 | */
|
---|
1580 | static void vusbProxyLinuxUrbDoTimeouts(PUSBPROXYDEV pProxyDev, PUSBPROXYDEVLNX pDevLnx)
|
---|
1581 | {
|
---|
1582 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
1583 | uint64_t u64MilliTS = RTTimeMilliTS();
|
---|
1584 | PUSBPROXYURBLNX pCur;
|
---|
1585 | for (pCur = pDevLnx->pInFlightHead;
|
---|
1586 | pCur;
|
---|
1587 | pCur = pCur->pNext)
|
---|
1588 | {
|
---|
1589 | if (pCur->fTimedOut)
|
---|
1590 | {
|
---|
1591 | if (pCur->pSplitHead)
|
---|
1592 | {
|
---|
1593 | /* split */
|
---|
1594 | Assert(pCur == pCur->pSplitHead);
|
---|
1595 | unsigned cFailures = 0;
|
---|
1596 | PUSBPROXYURBLNX pCur2;
|
---|
1597 | for (pCur2 = pCur; pCur2; pCur2 = pCur2->pSplitNext)
|
---|
1598 | {
|
---|
1599 | if (pCur2->fSplitElementReaped)
|
---|
1600 | continue;
|
---|
1601 |
|
---|
1602 | if ( !usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pCur2->KUrb, true, UINT32_MAX)
|
---|
1603 | || errno == ENOENT)
|
---|
1604 | pCur2->fCanceledByTimedOut = true;
|
---|
1605 | else if (errno != ENODEV)
|
---|
1606 | Log(("vusbProxyLinuxUrbDoTimeouts: pUrb=%p failed errno=%d (!!split!!)\n", pCur2->KUrb.usercontext, errno));
|
---|
1607 | else
|
---|
1608 | goto l_leave; /* ENODEV means break and everything cancelled elsewhere. */
|
---|
1609 | }
|
---|
1610 | LogRel(("USB: Cancelled URB (%p) after %llums!! (cFailures=%d)\n",
|
---|
1611 | pCur->KUrb.usercontext, (long long unsigned) u64MilliTS - pCur->u64SubmitTS, cFailures));
|
---|
1612 | }
|
---|
1613 | else
|
---|
1614 | {
|
---|
1615 | /* unsplit */
|
---|
1616 | if ( !usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pCur->KUrb, true, UINT32_MAX)
|
---|
1617 | || errno == -ENOENT)
|
---|
1618 | {
|
---|
1619 | pCur->fCanceledByTimedOut = true;
|
---|
1620 | LogRel(("USB: Cancelled URB (%p) after %llums!!\n", pCur->KUrb.usercontext, (long long unsigned) u64MilliTS - pCur->u64SubmitTS));
|
---|
1621 | }
|
---|
1622 | else if (errno != ENODEV)
|
---|
1623 | LogFlow(("vusbProxyLinuxUrbDoTimeouts: pUrb=%p failed errno=%d\n", pCur->KUrb.usercontext, errno));
|
---|
1624 | else
|
---|
1625 | goto l_leave; /* ENODEV means break and everything cancelled elsewhere. */
|
---|
1626 | }
|
---|
1627 | }
|
---|
1628 | #if 0
|
---|
1629 | /* Disabled for the time being as some USB devices have URBs pending for an unknown amount of time.
|
---|
1630 | * One example is the OmniKey CardMan 3821. */
|
---|
1631 | else if (u64MilliTS - pCur->u64SubmitTS >= 200*1000 /* 200 sec (180 sec has been observed with XP) */)
|
---|
1632 | pCur->fTimedOut = true;
|
---|
1633 | #endif
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | l_leave:
|
---|
1637 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 |
|
---|
1641 | /**
|
---|
1642 | * Translate the linux status to a VUSB status.
|
---|
1643 | *
|
---|
1644 | * @remarks see cc_to_error in ohci.h, uhci_map_status in uhci-q.c,
|
---|
1645 | * sitd_complete+itd_complete in ehci-sched.c, and qtd_copy_status in
|
---|
1646 | * ehci-q.c.
|
---|
1647 | */
|
---|
1648 | static VUSBSTATUS vusbProxyLinuxStatusToVUsbStatus(int iStatus)
|
---|
1649 | {
|
---|
1650 | switch (iStatus)
|
---|
1651 | {
|
---|
1652 | /** @todo VUSBSTATUS_NOT_ACCESSED */
|
---|
1653 | case -EXDEV: /* iso transfer, partial result. */
|
---|
1654 | case 0:
|
---|
1655 | return VUSBSTATUS_OK;
|
---|
1656 |
|
---|
1657 | case -EILSEQ:
|
---|
1658 | return VUSBSTATUS_CRC;
|
---|
1659 |
|
---|
1660 | case -EREMOTEIO: /* ehci and ohci uses this for underflow error. */
|
---|
1661 | return VUSBSTATUS_DATA_UNDERRUN;
|
---|
1662 | case -EOVERFLOW:
|
---|
1663 | return VUSBSTATUS_DATA_OVERRUN;
|
---|
1664 |
|
---|
1665 | case -ETIME:
|
---|
1666 | case -ENODEV:
|
---|
1667 | return VUSBSTATUS_DNR;
|
---|
1668 |
|
---|
1669 | //case -ECOMM:
|
---|
1670 | // return VUSBSTATUS_BUFFER_OVERRUN;
|
---|
1671 | //case -ENOSR:
|
---|
1672 | // return VUSBSTATUS_BUFFER_UNDERRUN;
|
---|
1673 |
|
---|
1674 | //case -EPROTO:
|
---|
1675 | // return VUSBSTATUS_BIT_STUFFING;
|
---|
1676 |
|
---|
1677 | case -EPIPE:
|
---|
1678 | Log(("vusbProxyLinuxStatusToVUsbStatus: STALL/EPIPE!!\n"));
|
---|
1679 | return VUSBSTATUS_STALL;
|
---|
1680 |
|
---|
1681 | case -ESHUTDOWN:
|
---|
1682 | Log(("vusbProxyLinuxStatusToVUsbStatus: SHUTDOWN!!\n"));
|
---|
1683 | return VUSBSTATUS_STALL;
|
---|
1684 |
|
---|
1685 | default:
|
---|
1686 | Log(("vusbProxyLinuxStatusToVUsbStatus: status %d!!\n", iStatus));
|
---|
1687 | return VUSBSTATUS_STALL;
|
---|
1688 | }
|
---|
1689 | }
|
---|
1690 |
|
---|
1691 |
|
---|
1692 | /**
|
---|
1693 | * Get and translates the linux status to a VUSB status.
|
---|
1694 | */
|
---|
1695 | static VUSBSTATUS vusbProxyLinuxUrbGetStatus(PUSBPROXYURBLNX pUrbLnx)
|
---|
1696 | {
|
---|
1697 | if ( pUrbLnx->fCanceledByTimedOut
|
---|
1698 | && pUrbLnx->KUrb.status == 0)
|
---|
1699 | return VUSBSTATUS_CRC;
|
---|
1700 | return vusbProxyLinuxStatusToVUsbStatus(pUrbLnx->KUrb.status);
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 |
|
---|
1704 | /**
|
---|
1705 | * Reap URBs in-flight on a device.
|
---|
1706 | *
|
---|
1707 | * @returns Pointer to a completed URB.
|
---|
1708 | * @returns NULL if no URB was completed.
|
---|
1709 | * @param pProxyDev The device.
|
---|
1710 | * @param cMillies Number of milliseconds to wait. Use 0 to not wait at all.
|
---|
1711 | */
|
---|
1712 | static DECLCALLBACK(PVUSBURB) usbProxyLinuxUrbReap(PUSBPROXYDEV pProxyDev, RTMSINTERVAL cMillies)
|
---|
1713 | {
|
---|
1714 | PUSBPROXYURBLNX pUrbLnx = NULL;
|
---|
1715 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
1716 |
|
---|
1717 | /*
|
---|
1718 | * Any URBs pending delivery?
|
---|
1719 | */
|
---|
1720 | if (pDevLnx->pTaxingHead)
|
---|
1721 | {
|
---|
1722 | RTCritSectEnter(&pDevLnx->CritSect);
|
---|
1723 | pUrbLnx = pDevLnx->pTaxingHead;
|
---|
1724 | if (pUrbLnx)
|
---|
1725 | {
|
---|
1726 | /* unlink from the pending delivery list */
|
---|
1727 | if (pUrbLnx->pNext)
|
---|
1728 | {
|
---|
1729 | pUrbLnx->pNext->pPrev = NULL;
|
---|
1730 | pDevLnx->pTaxingHead = pUrbLnx->pNext;
|
---|
1731 | }
|
---|
1732 | else
|
---|
1733 | pDevLnx->pTaxingHead = pDevLnx->pTaxingTail = NULL;
|
---|
1734 |
|
---|
1735 | /* temporarily into the active list, so free works right. */
|
---|
1736 | pUrbLnx->pPrev = NULL;
|
---|
1737 | pUrbLnx->pNext = pDevLnx->pInFlightHead;
|
---|
1738 | if (pUrbLnx->pNext)
|
---|
1739 | pUrbLnx->pNext->pPrev = pUrbLnx;
|
---|
1740 | pDevLnx->pInFlightHead = pUrbLnx;
|
---|
1741 | }
|
---|
1742 | RTCritSectLeave(&pDevLnx->CritSect);
|
---|
1743 | }
|
---|
1744 | if (!pUrbLnx)
|
---|
1745 | {
|
---|
1746 | /*
|
---|
1747 | * Don't block if nothing is in the air.
|
---|
1748 | */
|
---|
1749 | if (!pDevLnx->pInFlightHead)
|
---|
1750 | {
|
---|
1751 | int cMilliesWait = cMillies == RT_INDEFINITE_WAIT ? -1 : cMillies;
|
---|
1752 |
|
---|
1753 | LogFlow(("Nothing in flight, going to sleep\n"));
|
---|
1754 |
|
---|
1755 | struct pollfd pfd;
|
---|
1756 |
|
---|
1757 | pfd.fd = RTPipeToNative(pDevLnx->hPipeWakeupR);
|
---|
1758 | pfd.events = POLLIN | POLLHUP;
|
---|
1759 | pfd.revents = 0;
|
---|
1760 |
|
---|
1761 | int rc = poll(&pfd, 1, cMilliesWait);
|
---|
1762 | Log(("usbProxyLinuxUrbReap: poll rc = %d\n", rc));
|
---|
1763 | if (rc >= 1)
|
---|
1764 | {
|
---|
1765 | /* Drain pipe. */
|
---|
1766 | uint8_t bRead;
|
---|
1767 | size_t cbIgnored = 0;
|
---|
1768 | RTPipeRead(pDevLnx->hPipeWakeupR, &bRead, 1, &cbIgnored);
|
---|
1769 | }
|
---|
1770 | return NULL;
|
---|
1771 | }
|
---|
1772 |
|
---|
1773 | /*
|
---|
1774 | * Block for requested period.
|
---|
1775 | *
|
---|
1776 | * It seems to me that the path of poll() is shorter and
|
---|
1777 | * involves less semaphores than ioctl() on usbfs. So, we'll
|
---|
1778 | * do a poll regardless of whether cMillies == 0 or not.
|
---|
1779 | */
|
---|
1780 | if (cMillies)
|
---|
1781 | {
|
---|
1782 | int cMilliesWait = cMillies == RT_INDEFINITE_WAIT ? -1 : cMillies;
|
---|
1783 |
|
---|
1784 | for (;;)
|
---|
1785 | {
|
---|
1786 | struct pollfd pfd[2];
|
---|
1787 | pfd[0].fd = RTFileToNative(pDevLnx->hFile);
|
---|
1788 | pfd[0].events = POLLOUT | POLLWRNORM /* completed async */
|
---|
1789 | | POLLERR | POLLHUP /* disconnected */;
|
---|
1790 | pfd[0].revents = 0;
|
---|
1791 |
|
---|
1792 | pfd[1].fd = RTPipeToNative(pDevLnx->hPipeWakeupR);
|
---|
1793 | pfd[1].events = POLLIN | POLLHUP;
|
---|
1794 | pfd[1].revents = 0;
|
---|
1795 |
|
---|
1796 | int rc = poll(&pfd[0], 2, cMilliesWait);
|
---|
1797 | Log(("usbProxyLinuxUrbReap: poll rc = %d\n", rc));
|
---|
1798 | if (rc >= 1)
|
---|
1799 | {
|
---|
1800 | /* If the pipe caused the return drain it. */
|
---|
1801 | if (pfd[1].revents & POLLIN)
|
---|
1802 | {
|
---|
1803 | uint8_t bRead;
|
---|
1804 | size_t cbIgnored = 0;
|
---|
1805 | RTPipeRead(pDevLnx->hPipeWakeupR, &bRead, 1, &cbIgnored);
|
---|
1806 | }
|
---|
1807 | break;
|
---|
1808 | }
|
---|
1809 | if (rc >= 0 /*|| errno == ETIMEOUT*/)
|
---|
1810 | {
|
---|
1811 | vusbProxyLinuxUrbDoTimeouts(pProxyDev, pDevLnx);
|
---|
1812 | return NULL;
|
---|
1813 | }
|
---|
1814 | if (errno != EAGAIN)
|
---|
1815 | {
|
---|
1816 | Log(("usb-linux: Reap URB - poll -> %d errno=%d pProxyDev=%s\n", rc, errno, usbProxyGetName(pProxyDev)));
|
---|
1817 | return NULL;
|
---|
1818 | }
|
---|
1819 | Log(("usbProxyLinuxUrbReap: poll again - weird!!!\n"));
|
---|
1820 | }
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 | /*
|
---|
1824 | * Reap URBs, non-blocking.
|
---|
1825 | */
|
---|
1826 | for (;;)
|
---|
1827 | {
|
---|
1828 | struct usbdevfs_urb *pKUrb;
|
---|
1829 | while (ioctl(RTFileToNative(pDevLnx->hFile), USBDEVFS_REAPURBNDELAY, &pKUrb))
|
---|
1830 | if (errno != EINTR)
|
---|
1831 | {
|
---|
1832 | if (errno == ENODEV)
|
---|
1833 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
1834 | else if (errno == EAGAIN)
|
---|
1835 | vusbProxyLinuxUrbDoTimeouts(pProxyDev, pDevLnx);
|
---|
1836 | else
|
---|
1837 | Log(("usb-linux: Reap URB. errno=%d pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
|
---|
1838 | return NULL;
|
---|
1839 | }
|
---|
1840 | pUrbLnx = (PUSBPROXYURBLNX)pKUrb;
|
---|
1841 |
|
---|
1842 | /* split list: Is the entire split list done yet? */
|
---|
1843 | if (pUrbLnx->pSplitHead)
|
---|
1844 | {
|
---|
1845 | pUrbLnx->fSplitElementReaped = true;
|
---|
1846 |
|
---|
1847 | /* for variable size URBs, we may need to queue more if the just-reaped URB was completely filled */
|
---|
1848 | if (pUrbLnx->cbSplitRemaining && (pKUrb->actual_length == pKUrb->buffer_length) && !pUrbLnx->pSplitNext)
|
---|
1849 | {
|
---|
1850 | bool fUnplugged = false;
|
---|
1851 | bool fSucceeded;
|
---|
1852 |
|
---|
1853 | Assert(pUrbLnx->pSplitHead);
|
---|
1854 | Assert((pKUrb->endpoint & 0x80) && (!pKUrb->flags & USBDEVFS_URB_SHORT_NOT_OK));
|
---|
1855 | PUSBPROXYURBLNX pNew = usbProxyLinuxSplitURBFragment(pProxyDev, pUrbLnx->pSplitHead, pUrbLnx);
|
---|
1856 | if (!pNew)
|
---|
1857 | {
|
---|
1858 | Log(("usb-linux: Allocating URB fragment failed. errno=%d pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
|
---|
1859 | return NULL;
|
---|
1860 | }
|
---|
1861 | PVUSBURB pUrb = (PVUSBURB)pUrbLnx->KUrb.usercontext;
|
---|
1862 | fSucceeded = usbProxyLinuxSubmitURB(pProxyDev, pNew, pUrb, &fUnplugged);
|
---|
1863 | if (fUnplugged)
|
---|
1864 | usbProxLinuxUrbUnplugged(pProxyDev);
|
---|
1865 | if (!fSucceeded)
|
---|
1866 | return NULL;
|
---|
1867 | continue; /* try reaping another URB */
|
---|
1868 | }
|
---|
1869 | PUSBPROXYURBLNX pCur;
|
---|
1870 | for (pCur = pUrbLnx->pSplitHead; pCur; pCur = pCur->pSplitNext)
|
---|
1871 | if (!pCur->fSplitElementReaped)
|
---|
1872 | {
|
---|
1873 | pUrbLnx = NULL;
|
---|
1874 | break;
|
---|
1875 | }
|
---|
1876 | if (!pUrbLnx)
|
---|
1877 | continue;
|
---|
1878 | pUrbLnx = pUrbLnx->pSplitHead;
|
---|
1879 | }
|
---|
1880 | break;
|
---|
1881 | }
|
---|
1882 | }
|
---|
1883 |
|
---|
1884 | /*
|
---|
1885 | * Ok, we got one!
|
---|
1886 | */
|
---|
1887 | PVUSBURB pUrb = (PVUSBURB)pUrbLnx->KUrb.usercontext;
|
---|
1888 | if ( pUrb
|
---|
1889 | && !pUrbLnx->fCanceledBySubmit)
|
---|
1890 | {
|
---|
1891 | if (pUrbLnx->pSplitHead)
|
---|
1892 | {
|
---|
1893 | /* split - find the end byte and the first error status. */
|
---|
1894 | Assert(pUrbLnx == pUrbLnx->pSplitHead);
|
---|
1895 | uint8_t *pbEnd = &pUrb->abData[0];
|
---|
1896 | pUrb->enmStatus = VUSBSTATUS_OK;
|
---|
1897 | PUSBPROXYURBLNX pCur;
|
---|
1898 | for (pCur = pUrbLnx; pCur; pCur = pCur->pSplitNext)
|
---|
1899 | {
|
---|
1900 | if (pCur->KUrb.actual_length)
|
---|
1901 | pbEnd = (uint8_t *)pCur->KUrb.buffer + pCur->KUrb.actual_length;
|
---|
1902 | if (pUrb->enmStatus == VUSBSTATUS_OK)
|
---|
1903 | pUrb->enmStatus = vusbProxyLinuxUrbGetStatus(pCur);
|
---|
1904 | }
|
---|
1905 | pUrb->cbData = pbEnd - &pUrb->abData[0];
|
---|
1906 | usbProxyLinuxUrbFreeSplitList(pProxyDev, pUrbLnx);
|
---|
1907 | }
|
---|
1908 | else
|
---|
1909 | {
|
---|
1910 | /* unsplit. */
|
---|
1911 | pUrb->enmStatus = vusbProxyLinuxUrbGetStatus(pUrbLnx);
|
---|
1912 | pUrb->cbData = pUrbLnx->KUrb.actual_length;
|
---|
1913 | if (pUrb->enmType == VUSBXFERTYPE_ISOC)
|
---|
1914 | {
|
---|
1915 | unsigned i, off;
|
---|
1916 | for (i = 0, off = 0; i < pUrb->cIsocPkts; i++)
|
---|
1917 | {
|
---|
1918 | pUrb->aIsocPkts[i].enmStatus = vusbProxyLinuxStatusToVUsbStatus(pUrbLnx->KUrb.iso_frame_desc[i].status);
|
---|
1919 | Assert(pUrb->aIsocPkts[i].off == off);
|
---|
1920 | pUrb->aIsocPkts[i].cb = pUrbLnx->KUrb.iso_frame_desc[i].actual_length;
|
---|
1921 | off += pUrbLnx->KUrb.iso_frame_desc[i].length;
|
---|
1922 | }
|
---|
1923 | }
|
---|
1924 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1925 | }
|
---|
1926 | pUrb->Dev.pvPrivate = NULL;
|
---|
1927 |
|
---|
1928 | /* some adjustments for message transfers. */
|
---|
1929 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
1930 | {
|
---|
1931 | pUrb->cbData += sizeof(VUSBSETUP);
|
---|
1932 | usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
|
---|
1933 | }
|
---|
1934 | }
|
---|
1935 | else
|
---|
1936 | {
|
---|
1937 | usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
|
---|
1938 | pUrb = NULL;
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | LogFlow(("usbProxyLinuxUrbReap: pProxyDev=%s returns %p\n", usbProxyGetName(pProxyDev), pUrb));
|
---|
1942 | return pUrb;
|
---|
1943 | }
|
---|
1944 |
|
---|
1945 |
|
---|
1946 | /**
|
---|
1947 | * Cancels the URB.
|
---|
1948 | * The URB requires reaping, so we don't change its state.
|
---|
1949 | */
|
---|
1950 | static DECLCALLBACK(int) usbProxyLinuxUrbCancel(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
|
---|
1951 | {
|
---|
1952 | int rc = VINF_SUCCESS;
|
---|
1953 | PUSBPROXYURBLNX pUrbLnx = (PUSBPROXYURBLNX)pUrb->Dev.pvPrivate;
|
---|
1954 | if (pUrbLnx->pSplitHead)
|
---|
1955 | {
|
---|
1956 | /* split */
|
---|
1957 | Assert(pUrbLnx == pUrbLnx->pSplitHead);
|
---|
1958 | PUSBPROXYURBLNX pCur;
|
---|
1959 | for (pCur = pUrbLnx; pCur; pCur = pCur->pSplitNext)
|
---|
1960 | {
|
---|
1961 | if (pCur->fSplitElementReaped)
|
---|
1962 | continue;
|
---|
1963 | if ( !usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pCur->KUrb, true, UINT32_MAX)
|
---|
1964 | || errno == ENOENT)
|
---|
1965 | continue;
|
---|
1966 | if (errno == ENODEV)
|
---|
1967 | break;
|
---|
1968 | /** @todo: Think about how to handle errors wrt. to the status code. */
|
---|
1969 | Log(("usb-linux: Discard URB %p failed, errno=%d. pProxyDev=%s!!! (split)\n",
|
---|
1970 | pUrb, errno, usbProxyGetName(pProxyDev)));
|
---|
1971 | }
|
---|
1972 | }
|
---|
1973 | else
|
---|
1974 | {
|
---|
1975 | /* unsplit */
|
---|
1976 | if ( usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pUrbLnx->KUrb, true, UINT32_MAX)
|
---|
1977 | && errno != ENODEV /* deal with elsewhere. */
|
---|
1978 | && errno != ENOENT)
|
---|
1979 | {
|
---|
1980 | Log(("usb-linux: Discard URB %p failed, errno=%d. pProxyDev=%s!!!\n",
|
---|
1981 | pUrb, errno, usbProxyGetName(pProxyDev)));
|
---|
1982 | rc = RTErrConvertFromErrno(errno);
|
---|
1983 | }
|
---|
1984 | }
|
---|
1985 |
|
---|
1986 | return rc;
|
---|
1987 | }
|
---|
1988 |
|
---|
1989 |
|
---|
1990 | static DECLCALLBACK(int) usbProxyLinuxWakeup(PUSBPROXYDEV pProxyDev)
|
---|
1991 | {
|
---|
1992 | PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
|
---|
1993 | size_t cbIgnored;
|
---|
1994 |
|
---|
1995 | LogFlowFunc(("pProxyDev=%p\n", pProxyDev));
|
---|
1996 |
|
---|
1997 | return RTPipeWrite(pDevLnx->hPipeWakeupW, "", 1, &cbIgnored);
|
---|
1998 | }
|
---|
1999 |
|
---|
2000 | /**
|
---|
2001 | * The Linux USB Proxy Backend.
|
---|
2002 | */
|
---|
2003 | const USBPROXYBACK g_USBProxyDeviceHost =
|
---|
2004 | {
|
---|
2005 | /* pszName */
|
---|
2006 | "host",
|
---|
2007 | /* cbBackend */
|
---|
2008 | sizeof(USBPROXYDEVLNX),
|
---|
2009 | usbProxyLinuxOpen,
|
---|
2010 | usbProxyLinuxInit,
|
---|
2011 | usbProxyLinuxClose,
|
---|
2012 | usbProxyLinuxReset,
|
---|
2013 | usbProxyLinuxSetConfig,
|
---|
2014 | usbProxyLinuxClaimInterface,
|
---|
2015 | usbProxyLinuxReleaseInterface,
|
---|
2016 | usbProxyLinuxSetInterface,
|
---|
2017 | usbProxyLinuxClearHaltedEp,
|
---|
2018 | usbProxyLinuxUrbQueue,
|
---|
2019 | usbProxyLinuxUrbCancel,
|
---|
2020 | usbProxyLinuxUrbReap,
|
---|
2021 | usbProxyLinuxWakeup,
|
---|
2022 | 0
|
---|
2023 | };
|
---|
2024 |
|
---|
2025 |
|
---|
2026 | /*
|
---|
2027 | * Local Variables:
|
---|
2028 | * mode: c
|
---|
2029 | * c-file-style: "bsd"
|
---|
2030 | * c-basic-offset: 4
|
---|
2031 | * tab-width: 4
|
---|
2032 | * indent-tabs-mode: s
|
---|
2033 | * End:
|
---|
2034 | */
|
---|
2035 |
|
---|