VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/linux/USBProxyDevice-linux.cpp@ 96719

Last change on this file since 96719 was 96719, checked in by vboxsync, 2 years ago

USBProxyDevice-linux.cpp: Put struct usbdevfs_urb KUrb in a union with padding for the necessary length to make it work better with Linux 6.0 flexible array changes. bugref:10279

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.4 KB
Line 
1/* $Id: USBProxyDevice-linux.cpp 96719 2022-09-13 12:35:45Z vboxsync $ */
2/** @file
3 * USB device proxy - the Linux backend.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Defined Constants And Macros *
31*********************************************************************************************************************************/
32
33
34/*********************************************************************************************************************************
35* Header Files *
36*********************************************************************************************************************************/
37#define LOG_GROUP LOG_GROUP_DRV_USBPROXY
38
39#include <iprt/stdint.h>
40#include <iprt/err.h>
41#include <iprt/pipe.h>
42
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <sys/vfs.h>
46#include <sys/ioctl.h>
47#include <sys/poll.h>
48#include <stdint.h>
49#include <stdio.h>
50#include <string.h>
51#include <stdlib.h>
52#include <limits.h>
53#include <unistd.h>
54#include <fcntl.h>
55#include <errno.h>
56#ifdef VBOX_WITH_LINUX_COMPILER_H
57# include <linux/compiler.h>
58#endif
59#include <linux/usbdevice_fs.h>
60
61#ifndef RDESKTOP
62# include <VBox/vmm/pdm.h>
63#else
64# define RTCRITSECT void *
65static inline int rtcsNoop() { return VINF_SUCCESS; }
66static inline bool rtcsTrue() { return true; }
67# define RTCritSectInit(a) rtcsNoop()
68# define RTCritSectDelete(a) rtcsNoop()
69# define RTCritSectEnter(a) rtcsNoop()
70# define RTCritSectLeave(a) rtcsNoop()
71# define RTCritSectIsOwner(a) rtcsTrue()
72#endif
73#include <VBox/err.h>
74#include <VBox/log.h>
75#include <iprt/alloc.h>
76#include <iprt/assert.h>
77#include <iprt/asm.h>
78#include <iprt/ctype.h>
79#include <iprt/file.h>
80#include <iprt/linux/sysfs.h>
81#include <iprt/stream.h>
82#include <iprt/string.h>
83#include <iprt/list.h>
84#include <iprt/time.h>
85#include "../USBProxyDevice.h"
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 */
95typedef struct USBPROXYURBLNX
96{
97 /** The kernel URB data. */
98#if RT_GNUC_PREREQ(6, 0)
99 /* gcc 6.2 complains about the [] member of KUrb */
100# pragma GCC diagnostic push
101# pragma GCC diagnostic ignored "-Wpedantic"
102#endif
103 union
104 {
105 struct usbdevfs_urb KUrb;
106 /** Make sure we've got sufficient space for isochronous packets. */
107 uint8_t abKUrbPadding[ RT_UOFFSETOF(struct usbdevfs_urb, iso_frame_desc)
108 + sizeof(struct usbdevfs_iso_packet_desc) * 8];
109 };
110#if RT_GNUC_PREREQ(6, 0)
111# pragma GCC diagnostic pop
112#endif
113 /** Node to link the URB in of the existing lists. */
114 RTLISTNODE NodeList;
115 /** If we've split the VUSBURB up into multiple linux URBs, this is points to the head. */
116 struct USBPROXYURBLNX *pSplitHead;
117 /** The next linux URB if split up. */
118 struct USBPROXYURBLNX *pSplitNext;
119 /** Don't report these back. */
120 bool fCanceledBySubmit;
121 /** This split element is reaped. */
122 bool fSplitElementReaped;
123 /** This URB was discarded. */
124 bool fDiscarded;
125 /** Size to transfer in remaining fragments of a split URB */
126 uint32_t cbSplitRemaining;
127} USBPROXYURBLNX, *PUSBPROXYURBLNX;
128
129/**
130 * Data for the linux usb proxy backend.
131 */
132typedef struct USBPROXYDEVLNX
133{
134 /** The open file. */
135 RTFILE hFile;
136 /** Critical section protecting the lists. */
137 RTCRITSECT CritSect;
138 /** The list of free linux URBs (USBPROXYURBLNX). */
139 RTLISTANCHOR ListFree;
140 /** The list of active linux URBs.
141 * We must maintain this so we can properly reap URBs of a detached device.
142 * Only the split head will appear in this list. (USBPROXYURBLNX) */
143 RTLISTANCHOR ListInFlight;
144 /** Are we using sysfs to find the active configuration? */
145 bool fUsingSysfs;
146 /** Pipe handle for waking up - writing end. */
147 RTPIPE hPipeWakeupW;
148 /** Pipe handle for waking up - reading end. */
149 RTPIPE hPipeWakeupR;
150 /** The device node/sysfs path of the device.
151 * Used to figure out the configuration after a reset. */
152 char *pszPath;
153 /** Mask of claimed interfaces. */
154 uint32_t fClaimedIfsMask;
155} USBPROXYDEVLNX, *PUSBPROXYDEVLNX;
156
157
158/*********************************************************************************************************************************
159* Internal Functions *
160*********************************************************************************************************************************/
161static void usbProxLinuxUrbUnplugged(PUSBPROXYDEV pProxyDev);
162static DECLCALLBACK(int) usbProxyLinuxClaimInterface(PUSBPROXYDEV pProxyDev, int iIf);
163static DECLCALLBACK(int) usbProxyLinuxReleaseInterface(PUSBPROXYDEV pProxyDev, int iIf);
164
165
166/**
167 * Wrapper for the ioctl call.
168 *
169 * This wrapper will repeat the call if we get an EINTR or EAGAIN. It can also
170 * handle ENODEV (detached device) errors.
171 *
172 * @returns whatever ioctl returns.
173 * @param pProxyDev The proxy device.
174 * @param iCmd The ioctl command / function.
175 * @param pvArg The ioctl argument / data.
176 * @param fHandleNoDev Whether to handle ENODEV.
177 * @param cTries The number of retries. Use UINT32_MAX for (kind of) indefinite retries.
178 * @internal
179 */
180static int usbProxyLinuxDoIoCtl(PUSBPROXYDEV pProxyDev, unsigned long iCmd, void *pvArg, bool fHandleNoDev, uint32_t cTries)
181{
182 int rc;
183 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
184 do
185 {
186 do
187 {
188 rc = ioctl(RTFileToNative(pDevLnx->hFile), iCmd, pvArg);
189 if (rc >= 0)
190 return rc;
191 } while (errno == EINTR);
192
193 if (errno == ENODEV && fHandleNoDev)
194 {
195 usbProxLinuxUrbUnplugged(pProxyDev);
196 Log(("usb-linux: ENODEV -> unplugged. pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
197 errno = ENODEV;
198 break;
199 }
200 if (errno != EAGAIN)
201 break;
202 } while (cTries-- > 0);
203
204 return rc;
205}
206
207
208/**
209 * The device has been unplugged.
210 * Cancel all in-flight URBs and put them up for reaping.
211 */
212static void usbProxLinuxUrbUnplugged(PUSBPROXYDEV pProxyDev)
213{
214 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
215
216 /*
217 * Shoot down all flying URBs.
218 */
219 RTCritSectEnter(&pDevLnx->CritSect);
220 pProxyDev->fDetached = true;
221
222 PUSBPROXYURBLNX pUrbLnx;
223 PUSBPROXYURBLNX pUrbLnxNext;
224 RTListForEachSafe(&pDevLnx->ListInFlight, pUrbLnx, pUrbLnxNext, USBPROXYURBLNX, NodeList)
225 {
226 if (!pUrbLnx->fDiscarded)
227 {
228 pUrbLnx->fDiscarded = true;
229 /* Cancel the URB. It will be reaped normally. */
230 ioctl(RTFileToNative(pDevLnx->hFile), USBDEVFS_DISCARDURB, &pUrbLnx->KUrb);
231 if (!pUrbLnx->KUrb.status)
232 pUrbLnx->KUrb.status = -ENODEV;
233 }
234 }
235
236 RTCritSectLeave(&pDevLnx->CritSect);
237}
238
239
240/**
241 * Set the connect state seen by kernel drivers
242 * @internal
243 */
244static void usbProxyLinuxSetConnected(PUSBPROXYDEV pProxyDev, int iIf, bool fConnect, bool fQuiet)
245{
246 if ( iIf >= 32
247 || !(pProxyDev->fMaskedIfs & RT_BIT(iIf)))
248 {
249 struct usbdevfs_ioctl IoCtl;
250 if (!fQuiet)
251 LogFlow(("usbProxyLinuxSetConnected: pProxyDev=%s iIf=%#x fConnect=%s\n",
252 usbProxyGetName(pProxyDev), iIf, fConnect ? "true" : "false"));
253
254 IoCtl.ifno = iIf;
255 IoCtl.ioctl_code = fConnect ? USBDEVFS_CONNECT : USBDEVFS_DISCONNECT;
256 IoCtl.data = NULL;
257 if ( usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_IOCTL, &IoCtl, true, UINT32_MAX)
258 && !fQuiet)
259 Log(("usbProxyLinuxSetConnected: failure, errno=%d. pProxyDev=%s\n",
260 errno, usbProxyGetName(pProxyDev)));
261 }
262}
263
264
265/**
266 * Links the given URB into the in flight list.
267 *
268 * @returns nothing.
269 * @param pDevLnx The proxy device instance - Linux specific data.
270 * @param pUrbLnx The URB to link into the in flight list.
271 */
272static void usbProxyLinuxUrbLinkInFlight(PUSBPROXYDEVLNX pDevLnx, PUSBPROXYURBLNX pUrbLnx)
273{
274 LogFlowFunc(("pDevLnx=%p pUrbLnx=%p\n", pDevLnx, pUrbLnx));
275 Assert(RTCritSectIsOwner(&pDevLnx->CritSect));
276 Assert(!pUrbLnx->pSplitHead || pUrbLnx->pSplitHead == pUrbLnx);
277 RTListAppend(&pDevLnx->ListInFlight, &pUrbLnx->NodeList);
278}
279
280/**
281 * Unlinks the given URB from the in flight list.
282 * @returns nothing.
283 * @param pDevLnx The proxy device instance - Linux specific data.
284 * @param pUrbLnx The URB to link into the in flight list.
285 */
286static void usbProxyLinuxUrbUnlinkInFlight(PUSBPROXYDEVLNX pDevLnx, PUSBPROXYURBLNX pUrbLnx)
287{
288 LogFlowFunc(("pDevLnx=%p pUrbLnx=%p\n", pDevLnx, pUrbLnx));
289 RTCritSectEnter(&pDevLnx->CritSect);
290
291 /*
292 * Remove from the active list.
293 */
294 Assert(!pUrbLnx->pSplitHead || pUrbLnx->pSplitHead == pUrbLnx);
295
296 RTListNodeRemove(&pUrbLnx->NodeList);
297
298 RTCritSectLeave(&pDevLnx->CritSect);
299}
300
301/**
302 * Allocates a linux URB request structure.
303 * @returns Pointer to an active URB request.
304 * @returns NULL on failure.
305 * @param pProxyDev The proxy device instance.
306 * @param pSplitHead The split list head if allocating for a split list.
307 */
308static PUSBPROXYURBLNX usbProxyLinuxUrbAlloc(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pSplitHead)
309{
310 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
311 PUSBPROXYURBLNX pUrbLnx;
312
313 LogFlowFunc(("pProxyDev=%p pSplitHead=%p\n", pProxyDev, pSplitHead));
314
315 RTCritSectEnter(&pDevLnx->CritSect);
316
317 /*
318 * Try remove a linux URB from the free list, if none there allocate a new one.
319 */
320 pUrbLnx = RTListGetFirst(&pDevLnx->ListFree, USBPROXYURBLNX, NodeList);
321 if (pUrbLnx)
322 {
323 RTListNodeRemove(&pUrbLnx->NodeList);
324 RTCritSectLeave(&pDevLnx->CritSect);
325 }
326 else
327 {
328 RTCritSectLeave(&pDevLnx->CritSect);
329 pUrbLnx = (PUSBPROXYURBLNX)RTMemAlloc(sizeof(*pUrbLnx));
330 if (!pUrbLnx)
331 return NULL;
332 }
333
334 pUrbLnx->pSplitHead = pSplitHead;
335 pUrbLnx->pSplitNext = NULL;
336 pUrbLnx->fCanceledBySubmit = false;
337 pUrbLnx->fSplitElementReaped = false;
338 pUrbLnx->fDiscarded = false;
339 LogFlowFunc(("returns pUrbLnx=%p\n", pUrbLnx));
340 return pUrbLnx;
341}
342
343
344/**
345 * Frees a linux URB request structure.
346 *
347 * @param pProxyDev The proxy device instance.
348 * @param pUrbLnx The linux URB to free.
349 */
350static void usbProxyLinuxUrbFree(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx)
351{
352 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
353
354 LogFlowFunc(("pProxyDev=%p pUrbLnx=%p\n", pProxyDev, pUrbLnx));
355
356 /*
357 * Link it into the free list.
358 */
359 RTCritSectEnter(&pDevLnx->CritSect);
360 RTListAppend(&pDevLnx->ListFree, &pUrbLnx->NodeList);
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 */
371static void usbProxyLinuxUrbFreeSplitList(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx)
372{
373 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
374
375 LogFlowFunc(("pProxyDev=%p pUrbLnx=%p\n", pProxyDev, pUrbLnx));
376
377 RTCritSectEnter(&pDevLnx->CritSect);
378
379 pUrbLnx = pUrbLnx->pSplitHead;
380 Assert(pUrbLnx);
381 while (pUrbLnx)
382 {
383 PUSBPROXYURBLNX pFree = pUrbLnx;
384 pUrbLnx = pUrbLnx->pSplitNext;
385 Assert(pFree->pSplitHead);
386 pFree->pSplitHead = pFree->pSplitNext = NULL;
387 usbProxyLinuxUrbFree(pProxyDev, pFree);
388 }
389
390 RTCritSectLeave(&pDevLnx->CritSect);
391}
392
393
394/**
395 * This finds the device in the /proc/bus/usb/bus/addr file and finds
396 * the config with an asterix.
397 *
398 * @returns The Cfg#.
399 * @returns -1 if no active config.
400 * @param pProxyDev The proxy device instance.
401 * @param pszDevNode The path to the device. We infere the location of
402 * the devices file, which bus and device number we're
403 * looking for.
404 * @param piFirstCfg The first configuration. (optional)
405 * @internal
406 */
407static int usbProxyLinuxFindActiveConfigUsbfs(PUSBPROXYDEV pProxyDev, const char *pszDevNode, int *piFirstCfg)
408{
409 RT_NOREF(pProxyDev);
410
411 /*
412 * Set return defaults.
413 */
414 int iActiveCfg = -1;
415 if (piFirstCfg)
416 *piFirstCfg = 1;
417
418 /*
419 * Parse the usbfs device node path and turn it into a path to the "devices" file,
420 * picking up the device number and bus along the way.
421 */
422 size_t cchDevNode = strlen(pszDevNode);
423 char *pszDevices = (char *)RTMemDupEx(pszDevNode, cchDevNode, sizeof("devices"));
424 AssertReturn(pszDevices, iActiveCfg);
425
426 /* the device number */
427 char *psz = pszDevices + cchDevNode;
428 while (*psz != '/')
429 psz--;
430 Assert(pszDevices < psz);
431 uint32_t uDev;
432 int rc = RTStrToUInt32Ex(psz + 1, NULL, 10, &uDev);
433 if (RT_SUCCESS(rc))
434 {
435 /* the bus number */
436 *psz-- = '\0';
437 while (*psz != '/')
438 psz--;
439 Assert(pszDevices < psz);
440 uint32_t uBus;
441 rc = RTStrToUInt32Ex(psz + 1, NULL, 10, &uBus);
442 if (RT_SUCCESS(rc))
443 {
444 strcpy(psz + 1, "devices");
445
446 /*
447 * Open and scan the devices file.
448 * We're ASSUMING that each device starts off with a 'T:' line.
449 */
450 PRTSTREAM pFile;
451 rc = RTStrmOpen(pszDevices, "r", &pFile);
452 if (RT_SUCCESS(rc))
453 {
454 char szLine[1024];
455 while (RT_SUCCESS(RTStrmGetLine(pFile, szLine, sizeof(szLine))))
456 {
457 /* we're only interested in 'T:' lines. */
458 psz = RTStrStripL(szLine);
459 if (psz[0] != 'T' || psz[1] != ':')
460 continue;
461
462 /* Skip ahead to 'Bus' and compare */
463 psz = RTStrStripL(psz + 2); Assert(!strncmp(psz, RT_STR_TUPLE("Bus=")));
464 psz = RTStrStripL(psz + 4);
465 char *pszNext;
466 uint32_t u;
467 rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u); AssertRC(rc);
468 if (RT_FAILURE(rc))
469 continue;
470 if (u != uBus)
471 continue;
472
473 /* Skip ahead to 'Dev#' and compare */
474 psz = strstr(psz, "Dev#="); Assert(psz);
475 if (!psz)
476 continue;
477 psz = RTStrStripL(psz + 5);
478 rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u); AssertRC(rc);
479 if (RT_FAILURE(rc))
480 continue;
481 if (u != uDev)
482 continue;
483
484 /*
485 * Ok, we've found the device.
486 * Scan until we find a selected configuration, the next device, or EOF.
487 */
488 while (RT_SUCCESS(RTStrmGetLine(pFile, szLine, sizeof(szLine))))
489 {
490 psz = RTStrStripL(szLine);
491 if (psz[0] == 'T')
492 break;
493 if (psz[0] != 'C' || psz[1] != ':')
494 continue;
495 const bool fActive = psz[2] == '*';
496 if (!fActive && !piFirstCfg)
497 continue;
498
499 /* Get the 'Cfg#' value. */
500 psz = strstr(psz, "Cfg#="); Assert(psz);
501 if (psz)
502 {
503 psz = RTStrStripL(psz + 5);
504 rc = RTStrToUInt32Ex(psz, &pszNext, 10, &u); AssertRC(rc);
505 if (RT_SUCCESS(rc))
506 {
507 if (piFirstCfg)
508 {
509 *piFirstCfg = u;
510 piFirstCfg = NULL;
511 }
512 if (fActive)
513 iActiveCfg = u;
514 }
515 }
516 if (fActive)
517 break;
518 }
519 break;
520 }
521 RTStrmClose(pFile);
522 }
523 }
524 }
525 RTMemFree(pszDevices);
526
527 return iActiveCfg;
528}
529
530
531/**
532 * This finds the active configuration from sysfs.
533 *
534 * @returns The Cfg#.
535 * @returns -1 if no active config.
536 * @param pProxyDev The proxy device instance.
537 * @param pszPath The sysfs path for the device.
538 * @param piFirstCfg The first configuration. (optional)
539 * @internal
540 */
541static int usbProxyLinuxFindActiveConfigSysfs(PUSBPROXYDEV pProxyDev, const char *pszPath, int *piFirstCfg)
542{
543#ifdef VBOX_USB_WITH_SYSFS
544 if (piFirstCfg != NULL)
545 *piFirstCfg = pProxyDev->paCfgDescs != NULL
546 ? pProxyDev->paCfgDescs[0].Core.bConfigurationValue
547 : 1;
548 int64_t bCfg = 0;
549 int rc = RTLinuxSysFsReadIntFile(10, &bCfg, "%s/bConfigurationValue", pszPath);
550 if (RT_FAILURE(rc))
551 bCfg = -1;
552 return (int)bCfg;
553#else /* !VBOX_USB_WITH_SYSFS */
554 return -1;
555#endif /* !VBOX_USB_WITH_SYSFS */
556}
557
558
559/**
560 * This finds the active configuration.
561 *
562 * @returns The Cfg#.
563 * @returns -1 if no active config.
564 * @param pProxyDev The proxy device instance.
565 * @param pszPath The sysfs path for the device, or the usbfs device
566 * node path.
567 * @param piFirstCfg The first configuration. (optional)
568 * @internal
569 */
570static int usbProxyLinuxFindActiveConfig(PUSBPROXYDEV pProxyDev, const char *pszPath, int *piFirstCfg)
571{
572 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
573 if (pDevLnx->fUsingSysfs)
574 return usbProxyLinuxFindActiveConfigSysfs(pProxyDev, pszPath, piFirstCfg);
575 return usbProxyLinuxFindActiveConfigUsbfs(pProxyDev, pszPath, piFirstCfg);
576}
577
578
579/**
580 * Extracts the Linux file descriptor associated with the kernel USB device.
581 * This is used by rdesktop-vrdp for polling for events.
582 * @returns the FD, or asserts and returns -1 on error
583 * @param pProxyDev The device instance
584 */
585RTDECL(int) USBProxyDeviceLinuxGetFD(PUSBPROXYDEV pProxyDev)
586{
587 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
588 AssertReturn(pDevLnx->hFile != NIL_RTFILE, -1);
589 return RTFileToNative(pDevLnx->hFile);
590}
591
592
593/**
594 * Opens the device file.
595 *
596 * @returns VBox status code.
597 * @param pProxyDev The device instance.
598 * @param pszAddress If we are using usbfs, this is the path to the
599 * device. If we are using sysfs, this is a string of
600 * the form "sysfs:<sysfs path>//device:<device node>".
601 * In the second case, the two paths are guaranteed
602 * not to contain the substring "//".
603 */
604static DECLCALLBACK(int) usbProxyLinuxOpen(PUSBPROXYDEV pProxyDev, const char *pszAddress)
605{
606 LogFlow(("usbProxyLinuxOpen: pProxyDev=%p pszAddress=%s\n", pProxyDev, pszAddress));
607 const char *pszDevNode;
608 const char *pszPath;
609 size_t cchPath;
610 bool fUsingSysfs;
611
612 /*
613 * Are we using sysfs or usbfs?
614 */
615#ifdef VBOX_USB_WITH_SYSFS
616 fUsingSysfs = strncmp(pszAddress, RT_STR_TUPLE("sysfs:")) == 0;
617 if (fUsingSysfs)
618 {
619 pszDevNode = strstr(pszAddress, "//device:");
620 if (!pszDevNode)
621 {
622 LogRel(("usbProxyLinuxOpen: Invalid device address: '%s'\n", pszAddress));
623 return VERR_INVALID_PARAMETER;
624 }
625
626 pszPath = pszAddress + sizeof("sysfs:") - 1;
627 cchPath = pszDevNode - pszPath;
628 pszDevNode += sizeof("//device:") - 1;
629 }
630 else
631#endif /* VBOX_USB_WITH_SYSFS */
632 {
633#ifndef VBOX_USB_WITH_SYSFS
634 fUsingSysfs = false;
635#endif
636 pszPath = pszDevNode = pszAddress;
637 cchPath = strlen(pszPath);
638 }
639
640 /*
641 * Try open the device node.
642 */
643 RTFILE hFile;
644 int rc = RTFileOpen(&hFile, pszDevNode, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
645 if (RT_SUCCESS(rc))
646 {
647 /*
648 * Initialize the linux backend data.
649 */
650 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
651
652 RTListInit(&pDevLnx->ListFree);
653 RTListInit(&pDevLnx->ListInFlight);
654 pDevLnx->pszPath = RTStrDupN(pszPath, cchPath);
655 if (pDevLnx->pszPath)
656 {
657 rc = RTPipeCreate(&pDevLnx->hPipeWakeupR, &pDevLnx->hPipeWakeupW, 0);
658 if (RT_SUCCESS(rc))
659 {
660 pDevLnx->fUsingSysfs = fUsingSysfs;
661 pDevLnx->hFile = hFile;
662 pDevLnx->fClaimedIfsMask = 0;
663 rc = RTCritSectInit(&pDevLnx->CritSect);
664 if (RT_SUCCESS(rc))
665 {
666 LogFlow(("usbProxyLinuxOpen(%p, %s): returns successfully File=%RTfile iActiveCfg=%d\n",
667 pProxyDev, pszAddress, pDevLnx->hFile, pProxyDev->iActiveCfg));
668
669 return VINF_SUCCESS;
670 }
671 RTPipeClose(pDevLnx->hPipeWakeupR);
672 RTPipeClose(pDevLnx->hPipeWakeupW);
673 }
674 }
675 else
676 rc = VERR_NO_MEMORY;
677
678 RTFileClose(hFile);
679 }
680 else if (rc == VERR_ACCESS_DENIED)
681 rc = VERR_VUSB_USBFS_PERMISSION;
682
683 Log(("usbProxyLinuxOpen(%p, %s) failed, rc=%Rrc!\n", pProxyDev, pszAddress, rc));
684 return rc;
685}
686
687
688/**
689 * Claims all the interfaces and figures out the
690 * current configuration.
691 *
692 * @returns VINF_SUCCESS.
693 * @param pProxyDev The proxy device.
694 */
695static DECLCALLBACK(int) usbProxyLinuxInit(PUSBPROXYDEV pProxyDev)
696{
697 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
698
699 /*
700 * Brute force rulez.
701 * usbProxyLinuxSetConnected check for masked interfaces.
702 */
703 unsigned iIf;
704 for (iIf = 0; iIf < 256; iIf++)
705 usbProxyLinuxSetConnected(pProxyDev, iIf, false, true);
706
707 /*
708 * Determine the active configuration.
709 *
710 * If there isn't any active configuration, we will get EHOSTUNREACH (113) errors
711 * when trying to read the device descriptors in usbProxyDevCreate. So, we'll make
712 * the first one active (usually 1) then.
713 */
714 pProxyDev->cIgnoreSetConfigs = 1;
715 int iFirstCfg;
716 pProxyDev->iActiveCfg = usbProxyLinuxFindActiveConfig(pProxyDev, pDevLnx->pszPath, &iFirstCfg);
717 if (pProxyDev->iActiveCfg == -1)
718 {
719 usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_SETCONFIGURATION, &iFirstCfg, false, UINT32_MAX);
720 pProxyDev->iActiveCfg = usbProxyLinuxFindActiveConfig(pProxyDev, pDevLnx->pszPath, NULL);
721 Log(("usbProxyLinuxInit: No active config! Tried to set %d: iActiveCfg=%d\n", iFirstCfg, pProxyDev->iActiveCfg));
722 }
723 else
724 Log(("usbProxyLinuxInit(%p): iActiveCfg=%d\n", pProxyDev, pProxyDev->iActiveCfg));
725 return VINF_SUCCESS;
726}
727
728
729/**
730 * Closes the proxy device.
731 */
732static DECLCALLBACK(void) usbProxyLinuxClose(PUSBPROXYDEV pProxyDev)
733{
734 LogFlow(("usbProxyLinuxClose: pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
735 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
736 AssertPtrReturnVoid(pDevLnx);
737
738 /*
739 * Try put the device in a state which linux can cope with before we release it.
740 * Resetting it would be a nice start, although we must remember
741 * that it might have been disconnected...
742 *
743 * Don't reset if we're masking interfaces or if construction failed.
744 */
745 if (pProxyDev->fInited)
746 {
747 /* ASSUMES: thread == EMT */
748 if ( pProxyDev->fMaskedIfs
749 || !usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RESET, NULL, false, 10))
750 {
751 /* Connect drivers. */
752 unsigned iIf;
753 for (iIf = 0; iIf < 256; iIf++)
754 usbProxyLinuxSetConnected(pProxyDev, iIf, true, true);
755 Log(("USB: Successfully reset device pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
756 }
757 else if (errno != ENODEV)
758 LogRel(("USB: Reset failed, errno=%d, pProxyDev=%s.\n", errno, usbProxyGetName(pProxyDev)));
759 else /* This will happen if device was detached. */
760 Log(("USB: Reset failed, errno=%d (ENODEV), pProxyDev=%s.\n", errno, usbProxyGetName(pProxyDev)));
761 }
762
763 /*
764 * Now we can free all the resources and close the device.
765 */
766 RTCritSectDelete(&pDevLnx->CritSect);
767
768 PUSBPROXYURBLNX pUrbLnx;
769 PUSBPROXYURBLNX pUrbLnxNext;
770 RTListForEachSafe(&pDevLnx->ListInFlight, pUrbLnx, pUrbLnxNext, USBPROXYURBLNX, NodeList)
771 {
772 RTListNodeRemove(&pUrbLnx->NodeList);
773
774 if ( usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pUrbLnx->KUrb, false, UINT32_MAX)
775 && errno != ENODEV
776 && errno != ENOENT)
777 AssertMsgFailed(("errno=%d\n", errno));
778
779 if (pUrbLnx->pSplitHead)
780 {
781 PUSBPROXYURBLNX pCur = pUrbLnx->pSplitNext;
782 while (pCur)
783 {
784 PUSBPROXYURBLNX pFree = pCur;
785 pCur = pFree->pSplitNext;
786 if ( !pFree->fSplitElementReaped
787 && usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pFree->KUrb, false, UINT32_MAX)
788 && errno != ENODEV
789 && errno != ENOENT)
790 AssertMsgFailed(("errno=%d\n", errno));
791 RTMemFree(pFree);
792 }
793 }
794 else
795 Assert(!pUrbLnx->pSplitNext);
796 RTMemFree(pUrbLnx);
797 }
798
799 RTListForEachSafe(&pDevLnx->ListFree, pUrbLnx, pUrbLnxNext, USBPROXYURBLNX, NodeList)
800 {
801 RTListNodeRemove(&pUrbLnx->NodeList);
802 RTMemFree(pUrbLnx);
803 }
804
805 RTFileClose(pDevLnx->hFile);
806 pDevLnx->hFile = NIL_RTFILE;
807
808 RTPipeClose(pDevLnx->hPipeWakeupR);
809 RTPipeClose(pDevLnx->hPipeWakeupW);
810
811 RTStrFree(pDevLnx->pszPath);
812
813 LogFlow(("usbProxyLinuxClose: returns\n"));
814}
815
816
817/** @interface_method_impl{USBPROXYBACK,pfnReset} */
818static DECLCALLBACK(int) usbProxyLinuxReset(PUSBPROXYDEV pProxyDev, bool fResetOnLinux)
819{
820 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
821 RT_NOREF(fResetOnLinux);
822 Assert(!pProxyDev->fMaskedIfs);
823 LogFlow(("usbProxyLinuxReset: pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
824
825 uint32_t fActiveIfsMask = pDevLnx->fClaimedIfsMask;
826 unsigned i;
827
828 /*
829 * Before reset, release claimed interfaces. This less than obvious move
830 * prevents Linux from rebinding in-kernel drivers to the device after reset.
831 */
832 for (i = 0; i < (sizeof(fActiveIfsMask) * 8); ++i)
833 {
834 if (fActiveIfsMask & RT_BIT(i))
835 {
836 usbProxyLinuxReleaseInterface(pProxyDev, i);
837 }
838 }
839
840 if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RESET, NULL, false, 10))
841 {
842 int rc = errno;
843 LogRel(("usb-linux: Reset failed, rc=%Rrc errno=%d.\n", RTErrConvertFromErrno(rc), rc));
844 pProxyDev->iActiveCfg = -1;
845 return RTErrConvertFromErrno(rc);
846 }
847
848 /*
849 * Now reclaim previously claimed interfaces. If that doesn't work, let's hope
850 * the guest/VUSB can recover from that. Can happen if reset changes configuration.
851 */
852 for (i = 0; i < (sizeof(fActiveIfsMask) * 8); ++i)
853 {
854 if (fActiveIfsMask & RT_BIT(i))
855 {
856 usbProxyLinuxClaimInterface(pProxyDev, i);
857 }
858 }
859
860 /* find the active config - damn annoying. */
861 pProxyDev->iActiveCfg = usbProxyLinuxFindActiveConfig(pProxyDev, pDevLnx->pszPath, NULL);
862 LogFlow(("usbProxyLinuxReset: returns successfully iActiveCfg=%d\n", pProxyDev->iActiveCfg));
863
864 pProxyDev->cIgnoreSetConfigs = 2;
865 return VINF_SUCCESS;
866}
867
868
869/**
870 * SET_CONFIGURATION.
871 *
872 * The caller makes sure that it's not called first time after open or reset
873 * with the active interface.
874 *
875 * @returns success indicator.
876 * @param pProxyDev The device instance data.
877 * @param iCfg The configuration to set.
878 */
879static DECLCALLBACK(int) usbProxyLinuxSetConfig(PUSBPROXYDEV pProxyDev, int iCfg)
880{
881 LogFlow(("usbProxyLinuxSetConfig: pProxyDev=%s cfg=%#x\n",
882 usbProxyGetName(pProxyDev), iCfg));
883
884 if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_SETCONFIGURATION, &iCfg, true, UINT32_MAX))
885 {
886 Log(("usb-linux: Set configuration. errno=%d\n", errno));
887 return RTErrConvertFromErrno(errno);
888 }
889 return VINF_SUCCESS;
890}
891
892
893/**
894 * Claims an interface.
895 * @returns success indicator.
896 */
897static DECLCALLBACK(int) usbProxyLinuxClaimInterface(PUSBPROXYDEV pProxyDev, int iIf)
898{
899 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
900
901 LogFlow(("usbProxyLinuxClaimInterface: pProxyDev=%s ifnum=%#x\n", usbProxyGetName(pProxyDev), iIf));
902 usbProxyLinuxSetConnected(pProxyDev, iIf, false, false);
903
904 if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_CLAIMINTERFACE, &iIf, true, UINT32_MAX))
905 {
906 pDevLnx->fClaimedIfsMask &= ~RT_BIT(iIf);
907 LogRel(("usb-linux: Claim interface. errno=%d pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
908 return RTErrConvertFromErrno(errno);
909 }
910 pDevLnx->fClaimedIfsMask |= RT_BIT(iIf);
911 return VINF_SUCCESS;
912}
913
914
915/**
916 * Releases an interface.
917 * @returns success indicator.
918 */
919static DECLCALLBACK(int) usbProxyLinuxReleaseInterface(PUSBPROXYDEV pProxyDev, int iIf)
920{
921 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
922
923 LogFlow(("usbProxyLinuxReleaseInterface: pProxyDev=%s ifnum=%#x\n", usbProxyGetName(pProxyDev), iIf));
924
925 if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_RELEASEINTERFACE, &iIf, true, UINT32_MAX))
926 {
927 LogRel(("usb-linux: Release interface, errno=%d. pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
928 return RTErrConvertFromErrno(errno);
929 }
930 pDevLnx->fClaimedIfsMask &= ~RT_BIT(iIf);
931 return VINF_SUCCESS;
932}
933
934
935/**
936 * SET_INTERFACE.
937 *
938 * @returns success indicator.
939 */
940static DECLCALLBACK(int) usbProxyLinuxSetInterface(PUSBPROXYDEV pProxyDev, int iIf, int iAlt)
941{
942 struct usbdevfs_setinterface SetIf;
943 LogFlow(("usbProxyLinuxSetInterface: pProxyDev=%p iIf=%#x iAlt=%#x\n", pProxyDev, iIf, iAlt));
944
945 SetIf.interface = iIf;
946 SetIf.altsetting = iAlt;
947 if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_SETINTERFACE, &SetIf, true, UINT32_MAX))
948 {
949 Log(("usb-linux: Set interface, errno=%d. pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
950 return RTErrConvertFromErrno(errno);
951 }
952 return VINF_SUCCESS;
953}
954
955
956/**
957 * Clears the halted endpoint 'EndPt'.
958 */
959static DECLCALLBACK(int) usbProxyLinuxClearHaltedEp(PUSBPROXYDEV pProxyDev, unsigned int EndPt)
960{
961 LogFlow(("usbProxyLinuxClearHaltedEp: pProxyDev=%s EndPt=%u\n", usbProxyGetName(pProxyDev), EndPt));
962
963 if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_CLEAR_HALT, &EndPt, true, UINT32_MAX))
964 {
965 /*
966 * Unfortunately this doesn't work on control pipes.
967 * Windows doing this on the default endpoint and possibly other pipes too,
968 * so we'll feign success for ENOENT errors.
969 */
970 if (errno == ENOENT)
971 {
972 Log(("usb-linux: clear_halted_ep failed errno=%d. pProxyDev=%s ep=%d - IGNORED\n",
973 errno, usbProxyGetName(pProxyDev), EndPt));
974 return VINF_SUCCESS;
975 }
976 Log(("usb-linux: clear_halted_ep failed errno=%d. pProxyDev=%s ep=%d\n",
977 errno, usbProxyGetName(pProxyDev), EndPt));
978 return RTErrConvertFromErrno(errno);
979 }
980 return VINF_SUCCESS;
981}
982
983
984/**
985 * Setup packet byte-swapping routines.
986 */
987static void usbProxyLinuxUrbSwapSetup(PVUSBSETUP pSetup)
988{
989 pSetup->wValue = RT_H2LE_U16(pSetup->wValue);
990 pSetup->wIndex = RT_H2LE_U16(pSetup->wIndex);
991 pSetup->wLength = RT_H2LE_U16(pSetup->wLength);
992}
993
994
995/**
996 * Clean up after a failed URB submit.
997 */
998static void usbProxyLinuxCleanupFailedSubmit(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx, PUSBPROXYURBLNX pCur, PVUSBURB pUrb, bool *pfUnplugged)
999{
1000 if (pUrb->enmType == VUSBXFERTYPE_MSG)
1001 usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
1002
1003 /* discard and reap later (walking with pUrbLnx). */
1004 if (pUrbLnx != pCur)
1005 {
1006 for (;;)
1007 {
1008 pUrbLnx->fCanceledBySubmit = true;
1009 pUrbLnx->KUrb.usercontext = NULL;
1010 if (usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pUrbLnx->KUrb, false, UINT32_MAX))
1011 {
1012 if (errno == ENODEV)
1013 *pfUnplugged = true;
1014 else if (errno == ENOENT)
1015 pUrbLnx->fSplitElementReaped = true;
1016 else
1017 LogRel(("USB: Failed to discard %p! errno=%d (pUrb=%p)\n", pUrbLnx->KUrb.usercontext, errno, pUrb)); /* serious! */
1018 }
1019 if (pUrbLnx->pSplitNext == pCur)
1020 {
1021 pUrbLnx->pSplitNext = NULL;
1022 break;
1023 }
1024 pUrbLnx = pUrbLnx->pSplitNext; Assert(pUrbLnx);
1025 }
1026 }
1027
1028 /* free the unsubmitted ones. */
1029 while (pCur)
1030 {
1031 PUSBPROXYURBLNX pFree = pCur;
1032 pCur = pCur->pSplitNext;
1033 usbProxyLinuxUrbFree(pProxyDev, pFree);
1034 }
1035
1036 /* send unplug event if we failed with ENODEV originally. */
1037 if (*pfUnplugged)
1038 usbProxLinuxUrbUnplugged(pProxyDev);
1039}
1040
1041/**
1042 * Submit one URB through the usbfs IOCTL interface, with
1043 * retries
1044 *
1045 * @returns VBox status code.
1046 */
1047static int usbProxyLinuxSubmitURB(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pCur, PVUSBURB pUrb, bool *pfUnplugged)
1048{
1049 RT_NOREF(pUrb);
1050 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
1051 unsigned cTries = 0;
1052
1053 while (ioctl(RTFileToNative(pDevLnx->hFile), USBDEVFS_SUBMITURB, &pCur->KUrb))
1054 {
1055 if (errno == EINTR)
1056 continue;
1057 if (errno == ENODEV)
1058 {
1059 Log(("usbProxyLinuxSubmitURB: ENODEV -> unplugged. pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
1060 *pfUnplugged = true;
1061 return RTErrConvertFromErrno(errno);
1062 }
1063
1064 Log(("usb-linux: Submit URB %p -> %d!!! type=%d ep=%#x buffer_length=%#x cTries=%d\n",
1065 pUrb, errno, pCur->KUrb.type, pCur->KUrb.endpoint, pCur->KUrb.buffer_length, cTries));
1066 if (errno != EBUSY && ++cTries < 3) /* this doesn't work for the floppy :/ */
1067 continue;
1068
1069 return RTErrConvertFromErrno(errno);
1070 }
1071 return VINF_SUCCESS;
1072}
1073
1074/** The split size. 16K in known Linux kernel versions. */
1075#define SPLIT_SIZE 0x4000
1076
1077/**
1078 * Create a URB fragment of up to SPLIT_SIZE size and hook it
1079 * into the list of fragments.
1080 *
1081 * @returns pointer to newly allocated URB fragment or NULL.
1082 */
1083static PUSBPROXYURBLNX usbProxyLinuxSplitURBFragment(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pHead, PUSBPROXYURBLNX pCur)
1084{
1085 PUSBPROXYURBLNX pNew;
1086 uint32_t cbLeft = pCur->cbSplitRemaining;
1087 uint8_t *pb = (uint8_t *)pCur->KUrb.buffer;
1088
1089 LogFlowFunc(("pProxyDev=%p pHead=%p pCur=%p\n", pProxyDev, pHead, pCur));
1090
1091 Assert(cbLeft != 0);
1092 pNew = pCur->pSplitNext = usbProxyLinuxUrbAlloc(pProxyDev, pHead);
1093 if (!pNew)
1094 {
1095 usbProxyLinuxUrbFreeSplitList(pProxyDev, pHead);
1096 return NULL;
1097 }
1098 Assert(pNew->pSplitHead == pHead);
1099 Assert(pNew->pSplitNext == NULL);
1100
1101 pNew->KUrb = pHead->KUrb;
1102 pNew->KUrb.buffer = pb + pCur->KUrb.buffer_length;
1103 pNew->KUrb.buffer_length = RT_MIN(cbLeft, SPLIT_SIZE);
1104 pNew->KUrb.actual_length = 0;
1105
1106 cbLeft -= pNew->KUrb.buffer_length;
1107 Assert(cbLeft < INT32_MAX);
1108 pNew->cbSplitRemaining = cbLeft;
1109 LogFlowFunc(("returns pNew=%p\n", pNew));
1110 return pNew;
1111}
1112
1113/**
1114 * Try splitting up a VUSB URB into smaller URBs which the
1115 * linux kernel (usbfs) can deal with.
1116 *
1117 * NB: For ShortOK reads things get a little tricky - we don't
1118 * know how much data is going to arrive and not all the
1119 * fragment URBs might be filled. We can only safely set up one
1120 * URB at a time -> worse performance but correct behaviour.
1121 *
1122 * @returns VBox status code.
1123 * @param pProxyDev The proxy device.
1124 * @param pUrbLnx The linux URB which was rejected because of being too big.
1125 * @param pUrb The VUSB URB.
1126 */
1127static int usbProxyLinuxUrbQueueSplit(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx, PVUSBURB pUrb)
1128{
1129 /*
1130 * Split it up into SPLIT_SIZE sized blocks.
1131 */
1132 const unsigned cKUrbs = (pUrb->cbData + SPLIT_SIZE - 1) / SPLIT_SIZE;
1133 LogFlow(("usbProxyLinuxUrbQueueSplit: pUrb=%p cKUrbs=%d cbData=%d\n", pUrb, cKUrbs, pUrb->cbData));
1134
1135 uint32_t cbLeft = pUrb->cbData;
1136 uint8_t *pb = &pUrb->abData[0];
1137
1138 /* the first one (already allocated) */
1139 switch (pUrb->enmType)
1140 {
1141 default: /* shut up gcc */
1142 case VUSBXFERTYPE_BULK: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_BULK; break;
1143 case VUSBXFERTYPE_INTR: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_INTERRUPT; break;
1144 case VUSBXFERTYPE_MSG: pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_CONTROL; break;
1145 case VUSBXFERTYPE_ISOC:
1146 AssertMsgFailed(("We can't split isochronous URBs!\n"));
1147 usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
1148 return VERR_INVALID_PARAMETER; /** @todo Better status code. */
1149 }
1150 pUrbLnx->KUrb.endpoint = pUrb->EndPt;
1151 if (pUrb->enmDir == VUSBDIRECTION_IN)
1152 pUrbLnx->KUrb.endpoint |= 0x80;
1153 pUrbLnx->KUrb.flags = 0;
1154 if (pUrb->enmDir == VUSBDIRECTION_IN && pUrb->fShortNotOk)
1155 pUrbLnx->KUrb.flags |= USBDEVFS_URB_SHORT_NOT_OK;
1156 pUrbLnx->KUrb.status = 0;
1157 pUrbLnx->KUrb.buffer = pb;
1158 pUrbLnx->KUrb.buffer_length = RT_MIN(cbLeft, SPLIT_SIZE);
1159 pUrbLnx->KUrb.actual_length = 0;
1160 pUrbLnx->KUrb.start_frame = 0;
1161 pUrbLnx->KUrb.number_of_packets = 0;
1162 pUrbLnx->KUrb.error_count = 0;
1163 pUrbLnx->KUrb.signr = 0;
1164 pUrbLnx->KUrb.usercontext = pUrb;
1165 pUrbLnx->pSplitHead = pUrbLnx;
1166 pUrbLnx->pSplitNext = NULL;
1167
1168 PUSBPROXYURBLNX pCur = pUrbLnx;
1169
1170 cbLeft -= pUrbLnx->KUrb.buffer_length;
1171 pUrbLnx->cbSplitRemaining = cbLeft;
1172
1173 int rc = VINF_SUCCESS;
1174 bool fUnplugged = false;
1175 if (pUrb->enmDir == VUSBDIRECTION_IN && !pUrb->fShortNotOk)
1176 {
1177 /* Subsequent fragments will be queued only after the previous fragment is reaped
1178 * and only if necessary.
1179 */
1180 Log(("usb-linux: Large ShortOK read, only queuing first fragment.\n"));
1181 Assert(pUrbLnx->cbSplitRemaining > 0 && pUrbLnx->cbSplitRemaining < 256 * _1K);
1182 rc = usbProxyLinuxSubmitURB(pProxyDev, pUrbLnx, pUrb, &fUnplugged);
1183 }
1184 else
1185 {
1186 /* the rest. */
1187 unsigned i;
1188 for (i = 1; i < cKUrbs; i++)
1189 {
1190 pCur = usbProxyLinuxSplitURBFragment(pProxyDev, pUrbLnx, pCur);
1191 if (!pCur)
1192 return VERR_NO_MEMORY;
1193 }
1194 Assert(pCur->cbSplitRemaining == 0);
1195
1196 /* Submit the blocks. */
1197 pCur = pUrbLnx;
1198 for (i = 0; i < cKUrbs; i++, pCur = pCur->pSplitNext)
1199 {
1200 rc = usbProxyLinuxSubmitURB(pProxyDev, pCur, pUrb, &fUnplugged);
1201 if (RT_FAILURE(rc))
1202 break;
1203 }
1204 }
1205
1206 if (RT_SUCCESS(rc))
1207 {
1208 pUrb->Dev.pvPrivate = pUrbLnx;
1209 usbProxyLinuxUrbLinkInFlight(USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX), pUrbLnx);
1210 LogFlow(("usbProxyLinuxUrbQueueSplit: ok\n"));
1211 return VINF_SUCCESS;
1212 }
1213
1214 usbProxyLinuxCleanupFailedSubmit(pProxyDev, pUrbLnx, pCur, pUrb, &fUnplugged);
1215 return rc;
1216}
1217
1218
1219/**
1220 * @interface_method_impl{USBPROXYBACK,pfnUrbQueue}
1221 */
1222static DECLCALLBACK(int) usbProxyLinuxUrbQueue(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
1223{
1224 int rc = VINF_SUCCESS;
1225 unsigned cTries;
1226 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
1227 LogFlow(("usbProxyLinuxUrbQueue: pProxyDev=%s pUrb=%p EndPt=%d cbData=%d\n",
1228 usbProxyGetName(pProxyDev), pUrb, pUrb->EndPt, pUrb->cbData));
1229
1230 /*
1231 * Allocate a linux urb.
1232 */
1233 PUSBPROXYURBLNX pUrbLnx = usbProxyLinuxUrbAlloc(pProxyDev, NULL);
1234 if (!pUrbLnx)
1235 return VERR_NO_MEMORY;
1236
1237 pUrbLnx->KUrb.endpoint = pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? 0x80 : 0);
1238 pUrbLnx->KUrb.status = 0;
1239 pUrbLnx->KUrb.flags = 0;
1240 if (pUrb->enmDir == VUSBDIRECTION_IN && pUrb->fShortNotOk)
1241 pUrbLnx->KUrb.flags |= USBDEVFS_URB_SHORT_NOT_OK;
1242 pUrbLnx->KUrb.buffer = pUrb->abData;
1243 pUrbLnx->KUrb.buffer_length = pUrb->cbData;
1244 pUrbLnx->KUrb.actual_length = 0;
1245 pUrbLnx->KUrb.start_frame = 0;
1246 pUrbLnx->KUrb.number_of_packets = 0;
1247 pUrbLnx->KUrb.error_count = 0;
1248 pUrbLnx->KUrb.signr = 0;
1249 pUrbLnx->KUrb.usercontext = pUrb;
1250
1251 switch (pUrb->enmType)
1252 {
1253 case VUSBXFERTYPE_MSG:
1254 pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_CONTROL;
1255 if (pUrb->cbData < sizeof(VUSBSETUP))
1256 {
1257 usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
1258 return VERR_BUFFER_UNDERFLOW;
1259 }
1260 usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
1261 LogFlow(("usbProxyLinuxUrbQueue: message\n"));
1262 break;
1263 case VUSBXFERTYPE_BULK:
1264 pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_BULK;
1265 break;
1266 case VUSBXFERTYPE_ISOC:
1267 pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_ISO;
1268 pUrbLnx->KUrb.flags |= USBDEVFS_URB_ISO_ASAP;
1269 pUrbLnx->KUrb.number_of_packets = pUrb->cIsocPkts;
1270 unsigned i;
1271 for (i = 0; i < pUrb->cIsocPkts; i++)
1272 {
1273#if RT_GNUC_PREREQ(4, 6)
1274# pragma GCC diagnostic push
1275# pragma GCC diagnostic ignored "-Warray-bounds"
1276#endif
1277 pUrbLnx->KUrb.iso_frame_desc[i].length = pUrb->aIsocPkts[i].cb;
1278 pUrbLnx->KUrb.iso_frame_desc[i].actual_length = 0;
1279 pUrbLnx->KUrb.iso_frame_desc[i].status = 0x7fff;
1280#if RT_GNUC_PREREQ(4, 6)
1281# pragma GCC diagnostic pop
1282#endif
1283 }
1284 break;
1285 case VUSBXFERTYPE_INTR:
1286 pUrbLnx->KUrb.type = USBDEVFS_URB_TYPE_INTERRUPT;
1287 break;
1288 default:
1289 rc = VERR_INVALID_PARAMETER; /** @todo better status code. */
1290 }
1291
1292 /*
1293 * We have to serialize access by using the critial section here because this
1294 * thread might be suspended after submitting the URB but before linking it into
1295 * the in flight list. This would get us in trouble when reaping the URB on another
1296 * thread while it isn't in the in flight list.
1297 *
1298 * Linking the URB into the list before submitting it like it was done in the past is not
1299 * possible either because submitting the URB might fail here because the device gets
1300 * detached. The reaper thread gets this event too and might race this thread before we
1301 * can unlink the URB from the active list and the common code might end up freeing
1302 * the common URB structure twice.
1303 */
1304 RTCritSectEnter(&pDevLnx->CritSect);
1305 /*
1306 * Submit it.
1307 */
1308 cTries = 0;
1309 while (ioctl(RTFileToNative(pDevLnx->hFile), USBDEVFS_SUBMITURB, &pUrbLnx->KUrb))
1310 {
1311 if (errno == EINTR)
1312 continue;
1313 if (errno == ENODEV)
1314 {
1315 rc = RTErrConvertFromErrno(errno);
1316 Log(("usbProxyLinuxUrbQueue: ENODEV -> unplugged. pProxyDev=%s\n", usbProxyGetName(pProxyDev)));
1317 if (pUrb->enmType == VUSBXFERTYPE_MSG)
1318 usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
1319
1320 RTCritSectLeave(&pDevLnx->CritSect);
1321 usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
1322 usbProxLinuxUrbUnplugged(pProxyDev);
1323 return rc;
1324 }
1325
1326 /*
1327 * usbfs has or used to have a low buffer limit (16KB) in order to prevent
1328 * processes wasting kmalloc'ed memory. It will return EINVAL if break that
1329 * limit, and we'll have to split the VUSB URB up into multiple linux URBs.
1330 *
1331 * Since this is a limit which is subject to change, we cannot check for it
1332 * before submitting the URB. We just have to try and fail.
1333 */
1334 if ( errno == EINVAL
1335 && pUrb->cbData >= 8*_1K)
1336 {
1337 rc = usbProxyLinuxUrbQueueSplit(pProxyDev, pUrbLnx, pUrb);
1338 RTCritSectLeave(&pDevLnx->CritSect);
1339 return rc;
1340 }
1341
1342 Log(("usb-linux: Queue URB %p -> %d!!! type=%d ep=%#x buffer_length=%#x cTries=%d\n",
1343 pUrb, errno, pUrbLnx->KUrb.type, pUrbLnx->KUrb.endpoint, pUrbLnx->KUrb.buffer_length, cTries));
1344 if (errno != EBUSY && ++cTries < 3) /* this doesn't work for the floppy :/ */
1345 continue;
1346
1347 RTCritSectLeave(&pDevLnx->CritSect);
1348 rc = RTErrConvertFromErrno(errno);
1349 if (pUrb->enmType == VUSBXFERTYPE_MSG)
1350 usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
1351 usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
1352 return rc;
1353 }
1354
1355 usbProxyLinuxUrbLinkInFlight(pDevLnx, pUrbLnx);
1356 RTCritSectLeave(&pDevLnx->CritSect);
1357
1358 LogFlow(("usbProxyLinuxUrbQueue: ok\n"));
1359 pUrb->Dev.pvPrivate = pUrbLnx;
1360 return rc;
1361}
1362
1363
1364/**
1365 * Translate the linux status to a VUSB status.
1366 *
1367 * @remarks see cc_to_error in ohci.h, uhci_map_status in uhci-q.c,
1368 * sitd_complete+itd_complete in ehci-sched.c, and qtd_copy_status in
1369 * ehci-q.c.
1370 */
1371static VUSBSTATUS vusbProxyLinuxStatusToVUsbStatus(int iStatus)
1372{
1373 switch (iStatus)
1374 {
1375 /** @todo VUSBSTATUS_NOT_ACCESSED */
1376 case -EXDEV: /* iso transfer, partial result. */
1377 case 0:
1378 return VUSBSTATUS_OK;
1379
1380 case -EILSEQ:
1381 return VUSBSTATUS_CRC;
1382
1383 case -EREMOTEIO: /* ehci and ohci uses this for underflow error. */
1384 return VUSBSTATUS_DATA_UNDERRUN;
1385 case -EOVERFLOW:
1386 return VUSBSTATUS_DATA_OVERRUN;
1387
1388 case -ETIME:
1389 case -ENODEV:
1390 return VUSBSTATUS_DNR;
1391
1392 //case -ECOMM:
1393 // return VUSBSTATUS_BUFFER_OVERRUN;
1394 //case -ENOSR:
1395 // return VUSBSTATUS_BUFFER_UNDERRUN;
1396
1397 case -EPROTO:
1398 Log(("vusbProxyLinuxStatusToVUsbStatus: DNR/EPPROTO!!\n"));
1399 return VUSBSTATUS_DNR;
1400
1401 case -EPIPE:
1402 Log(("vusbProxyLinuxStatusToVUsbStatus: STALL/EPIPE!!\n"));
1403 return VUSBSTATUS_STALL;
1404
1405 case -ESHUTDOWN:
1406 Log(("vusbProxyLinuxStatusToVUsbStatus: SHUTDOWN!!\n"));
1407 return VUSBSTATUS_STALL;
1408
1409 case -ENOENT:
1410 Log(("vusbProxyLinuxStatusToVUsbStatus: ENOENT!!\n"));
1411 return VUSBSTATUS_STALL;
1412
1413 default:
1414 Log(("vusbProxyLinuxStatusToVUsbStatus: status %d!!\n", iStatus));
1415 return VUSBSTATUS_STALL;
1416 }
1417}
1418
1419
1420/**
1421 * Get and translates the linux status to a VUSB status.
1422 */
1423static VUSBSTATUS vusbProxyLinuxUrbGetStatus(PUSBPROXYURBLNX pUrbLnx)
1424{
1425 return vusbProxyLinuxStatusToVUsbStatus(pUrbLnx->KUrb.status);
1426}
1427
1428
1429/**
1430 * Reap URBs in-flight on a device.
1431 *
1432 * @returns Pointer to a completed URB.
1433 * @returns NULL if no URB was completed.
1434 * @param pProxyDev The device.
1435 * @param cMillies Number of milliseconds to wait. Use 0 to not wait at all.
1436 */
1437static DECLCALLBACK(PVUSBURB) usbProxyLinuxUrbReap(PUSBPROXYDEV pProxyDev, RTMSINTERVAL cMillies)
1438{
1439 PUSBPROXYURBLNX pUrbLnx = NULL;
1440 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
1441
1442 /*
1443 * Block for requested period.
1444 *
1445 * It seems to me that the path of poll() is shorter and
1446 * involves less semaphores than ioctl() on usbfs. So, we'll
1447 * do a poll regardless of whether cMillies == 0 or not.
1448 */
1449 if (cMillies)
1450 {
1451 int cMilliesWait = cMillies == RT_INDEFINITE_WAIT ? -1 : cMillies;
1452
1453 for (;;)
1454 {
1455 struct pollfd pfd[2];
1456 pfd[0].fd = RTFileToNative(pDevLnx->hFile);
1457 pfd[0].events = POLLOUT | POLLWRNORM /* completed async */
1458 | POLLERR | POLLHUP /* disconnected */;
1459 pfd[0].revents = 0;
1460
1461 pfd[1].fd = RTPipeToNative(pDevLnx->hPipeWakeupR);
1462 pfd[1].events = POLLIN | POLLHUP;
1463 pfd[1].revents = 0;
1464
1465 int rc = poll(&pfd[0], 2, cMilliesWait);
1466 Log(("usbProxyLinuxUrbReap: poll rc = %d\n", rc));
1467 if (rc >= 1)
1468 {
1469 /* If the pipe caused the return drain it. */
1470 if (pfd[1].revents & POLLIN)
1471 {
1472 uint8_t bRead;
1473 size_t cbIgnored = 0;
1474 RTPipeRead(pDevLnx->hPipeWakeupR, &bRead, 1, &cbIgnored);
1475 }
1476 break;
1477 }
1478 if (rc >= 0)
1479 return NULL;
1480
1481 if (errno != EAGAIN)
1482 {
1483 Log(("usb-linux: Reap URB - poll -> %d errno=%d pProxyDev=%s\n", rc, errno, usbProxyGetName(pProxyDev)));
1484 return NULL;
1485 }
1486 Log(("usbProxyLinuxUrbReap: poll again - weird!!!\n"));
1487 }
1488 }
1489
1490 /*
1491 * Reap URBs, non-blocking.
1492 */
1493 for (;;)
1494 {
1495 struct usbdevfs_urb *pKUrb;
1496 while (ioctl(RTFileToNative(pDevLnx->hFile), USBDEVFS_REAPURBNDELAY, &pKUrb))
1497 if (errno != EINTR)
1498 {
1499 if (errno == ENODEV)
1500 usbProxLinuxUrbUnplugged(pProxyDev);
1501 else
1502 Log(("usb-linux: Reap URB. errno=%d pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
1503 return NULL;
1504 }
1505 pUrbLnx = (PUSBPROXYURBLNX)pKUrb;
1506
1507 /* split list: Is the entire split list done yet? */
1508 if (pUrbLnx->pSplitHead)
1509 {
1510 pUrbLnx->fSplitElementReaped = true;
1511
1512 /* for variable size URBs, we may need to queue more if the just-reaped URB was completely filled */
1513 if (pUrbLnx->cbSplitRemaining && (pKUrb->actual_length == pKUrb->buffer_length) && !pUrbLnx->pSplitNext)
1514 {
1515 bool fUnplugged = false;
1516 bool fSucceeded;
1517
1518 Assert(pUrbLnx->pSplitHead);
1519 Assert((pKUrb->endpoint & 0x80) && !(pKUrb->flags & USBDEVFS_URB_SHORT_NOT_OK));
1520 PUSBPROXYURBLNX pNew = usbProxyLinuxSplitURBFragment(pProxyDev, pUrbLnx->pSplitHead, pUrbLnx);
1521 if (!pNew)
1522 {
1523 Log(("usb-linux: Allocating URB fragment failed. errno=%d pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
1524 return NULL;
1525 }
1526 PVUSBURB pUrb = (PVUSBURB)pUrbLnx->KUrb.usercontext;
1527 fSucceeded = usbProxyLinuxSubmitURB(pProxyDev, pNew, pUrb, &fUnplugged);
1528 if (fUnplugged)
1529 usbProxLinuxUrbUnplugged(pProxyDev);
1530 if (!fSucceeded)
1531 return NULL;
1532 continue; /* try reaping another URB */
1533 }
1534 PUSBPROXYURBLNX pCur;
1535 for (pCur = pUrbLnx->pSplitHead; pCur; pCur = pCur->pSplitNext)
1536 if (!pCur->fSplitElementReaped)
1537 {
1538 pUrbLnx = NULL;
1539 break;
1540 }
1541 if (!pUrbLnx)
1542 continue;
1543 pUrbLnx = pUrbLnx->pSplitHead;
1544 }
1545 break;
1546 }
1547
1548 /*
1549 * Ok, we got one!
1550 */
1551 PVUSBURB pUrb = (PVUSBURB)pUrbLnx->KUrb.usercontext;
1552 if ( pUrb
1553 && !pUrbLnx->fCanceledBySubmit)
1554 {
1555 if (pUrbLnx->pSplitHead)
1556 {
1557 /* split - find the end byte and the first error status. */
1558 Assert(pUrbLnx == pUrbLnx->pSplitHead);
1559 uint8_t *pbEnd = &pUrb->abData[0];
1560 pUrb->enmStatus = VUSBSTATUS_OK;
1561 PUSBPROXYURBLNX pCur;
1562 for (pCur = pUrbLnx; pCur; pCur = pCur->pSplitNext)
1563 {
1564 if (pCur->KUrb.actual_length)
1565 pbEnd = (uint8_t *)pCur->KUrb.buffer + pCur->KUrb.actual_length;
1566 if (pUrb->enmStatus == VUSBSTATUS_OK)
1567 pUrb->enmStatus = vusbProxyLinuxUrbGetStatus(pCur);
1568 }
1569 pUrb->cbData = pbEnd - &pUrb->abData[0];
1570 usbProxyLinuxUrbUnlinkInFlight(pDevLnx, pUrbLnx);
1571 usbProxyLinuxUrbFreeSplitList(pProxyDev, pUrbLnx);
1572 }
1573 else
1574 {
1575 /* unsplit. */
1576 pUrb->enmStatus = vusbProxyLinuxUrbGetStatus(pUrbLnx);
1577 pUrb->cbData = pUrbLnx->KUrb.actual_length;
1578 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
1579 {
1580 AssertCompile( sizeof(pUrbLnx->abKUrbPadding)
1581 >= RT_UOFFSETOF(struct usbdevfs_urb, iso_frame_desc)
1582 + sizeof(struct usbdevfs_iso_packet_desc) * RT_ELEMENTS(pUrb->aIsocPkts));
1583 unsigned i, off;
1584 for (i = 0, off = 0; i < pUrb->cIsocPkts; i++)
1585 {
1586#if RT_GNUC_PREREQ(4, 6)
1587# pragma GCC diagnostic push
1588# pragma GCC diagnostic ignored "-Warray-bounds"
1589#endif
1590 pUrb->aIsocPkts[i].enmStatus = vusbProxyLinuxStatusToVUsbStatus(pUrbLnx->KUrb.iso_frame_desc[i].status);
1591 Assert(pUrb->aIsocPkts[i].off == off);
1592 pUrb->aIsocPkts[i].cb = pUrbLnx->KUrb.iso_frame_desc[i].actual_length;
1593 off += pUrbLnx->KUrb.iso_frame_desc[i].length;
1594#if RT_GNUC_PREREQ(4, 6)
1595# pragma GCC diagnostic pop
1596#endif
1597 }
1598 }
1599 usbProxyLinuxUrbUnlinkInFlight(pDevLnx, pUrbLnx);
1600 usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
1601 }
1602 pUrb->Dev.pvPrivate = NULL;
1603
1604 /* some adjustments for message transfers. */
1605 if (pUrb->enmType == VUSBXFERTYPE_MSG)
1606 {
1607 pUrb->cbData += sizeof(VUSBSETUP);
1608 usbProxyLinuxUrbSwapSetup((PVUSBSETUP)pUrb->abData);
1609 }
1610 }
1611 else
1612 {
1613 usbProxyLinuxUrbUnlinkInFlight(pDevLnx, pUrbLnx);
1614 usbProxyLinuxUrbFree(pProxyDev, pUrbLnx);
1615 pUrb = NULL;
1616 }
1617
1618 LogFlow(("usbProxyLinuxUrbReap: pProxyDev=%s returns %p\n", usbProxyGetName(pProxyDev), pUrb));
1619 return pUrb;
1620}
1621
1622
1623/**
1624 * Cancels the URB.
1625 * The URB requires reaping, so we don't change its state.
1626 */
1627static DECLCALLBACK(int) usbProxyLinuxUrbCancel(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
1628{
1629 int rc = VINF_SUCCESS;
1630 PUSBPROXYURBLNX pUrbLnx = (PUSBPROXYURBLNX)pUrb->Dev.pvPrivate;
1631 if (pUrbLnx->pSplitHead)
1632 {
1633 /* split */
1634 Assert(pUrbLnx == pUrbLnx->pSplitHead);
1635 PUSBPROXYURBLNX pCur;
1636 for (pCur = pUrbLnx; pCur; pCur = pCur->pSplitNext)
1637 {
1638 if (pCur->fSplitElementReaped)
1639 continue;
1640 if ( !usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pCur->KUrb, true, UINT32_MAX)
1641 || errno == ENOENT)
1642 continue;
1643 if (errno == ENODEV)
1644 break;
1645 /** @todo Think about how to handle errors wrt. to the status code. */
1646 Log(("usb-linux: Discard URB %p failed, errno=%d. pProxyDev=%s!!! (split)\n",
1647 pUrb, errno, usbProxyGetName(pProxyDev)));
1648 }
1649 }
1650 else
1651 {
1652 /* unsplit */
1653 if ( usbProxyLinuxDoIoCtl(pProxyDev, USBDEVFS_DISCARDURB, &pUrbLnx->KUrb, true, UINT32_MAX)
1654 && errno != ENODEV /* deal with elsewhere. */
1655 && errno != ENOENT)
1656 {
1657 Log(("usb-linux: Discard URB %p failed, errno=%d. pProxyDev=%s!!!\n",
1658 pUrb, errno, usbProxyGetName(pProxyDev)));
1659 rc = RTErrConvertFromErrno(errno);
1660 }
1661 }
1662
1663 return rc;
1664}
1665
1666
1667static DECLCALLBACK(int) usbProxyLinuxWakeup(PUSBPROXYDEV pProxyDev)
1668{
1669 PUSBPROXYDEVLNX pDevLnx = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVLNX);
1670 size_t cbIgnored;
1671
1672 LogFlowFunc(("pProxyDev=%p\n", pProxyDev));
1673
1674 return RTPipeWrite(pDevLnx->hPipeWakeupW, "", 1, &cbIgnored);
1675}
1676
1677/**
1678 * The Linux USB Proxy Backend.
1679 */
1680const USBPROXYBACK g_USBProxyDeviceHost =
1681{
1682 /* pszName */
1683 "host",
1684 /* cbBackend */
1685 sizeof(USBPROXYDEVLNX),
1686 usbProxyLinuxOpen,
1687 usbProxyLinuxInit,
1688 usbProxyLinuxClose,
1689 usbProxyLinuxReset,
1690 usbProxyLinuxSetConfig,
1691 usbProxyLinuxClaimInterface,
1692 usbProxyLinuxReleaseInterface,
1693 usbProxyLinuxSetInterface,
1694 usbProxyLinuxClearHaltedEp,
1695 usbProxyLinuxUrbQueue,
1696 usbProxyLinuxUrbCancel,
1697 usbProxyLinuxUrbReap,
1698 usbProxyLinuxWakeup,
1699 0
1700};
1701
1702
1703/*
1704 * Local Variables:
1705 * mode: c
1706 * c-file-style: "bsd"
1707 * c-basic-offset: 4
1708 * End:
1709 */
1710
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette