VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/solaris/USBProxyDevice-solaris.cpp@ 54785

Last change on this file since 54785 was 50237, checked in by vboxsync, 11 years ago

Another Solaris build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.7 KB
Line 
1/* $Id: USBProxyDevice-solaris.cpp 50237 2014-01-24 23:16:52Z vboxsync $ */
2/** @file
3 * USB device proxy - the Solaris backend.
4 */
5
6/*
7 * Copyright (C) 2009-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_USBPROXY
23#ifdef DEBUG_ramshankar
24# define LOG_INSTANCE RTLogRelDefaultInstance()
25#endif
26#include <sys/poll.h>
27#include <errno.h>
28#include <strings.h>
29#include <limits.h>
30
31#include <VBox/log.h>
32#include <VBox/err.h>
33#include <VBox/vmm/pdm.h>
34
35#include <iprt/string.h>
36#include <iprt/critsect.h>
37#include <iprt/time.h>
38#include <iprt/file.h>
39#include <iprt/mem.h>
40#include <iprt/pipe.h>
41#include "../USBProxyDevice.h"
42#include <VBox/usblib.h>
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47/** Log Prefix. */
48#define USBPROXY "USBProxy"
49
50
51/*******************************************************************************
52* Structures and Typedefs *
53*******************************************************************************/
54/**
55 * Wrapper around the solaris urb request structure.
56 * This is required to track in-flight and landed URBs.
57 */
58typedef struct USBPROXYURBSOL
59{
60 /** Pointer to the Solaris device. */
61 struct USBPROXYDEVSOL *pDevSol;
62 /** Pointer to the VUSB URB (set to NULL if canceled). */
63 PVUSBURB pVUsbUrb;
64 /** Pointer to the next solaris URB. */
65 struct USBPROXYURBSOL *pNext;
66 /** Pointer to the previous solaris URB. */
67 struct USBPROXYURBSOL *pPrev;
68} USBPROXYURBSOL, *PUSBPROXYURBSOL;
69
70/**
71 * Data for the solaris usb proxy backend.
72 */
73typedef struct USBPROXYDEVSOL
74{
75 /** Path of the USB device in the devices tree (persistent). */
76 char *pszDevicePath;
77 /** The connection to the client driver. */
78 RTFILE hFile;
79 /** Pointer to the proxy device instance. */
80 PUSBPROXYDEV pProxyDev;
81 /** Critical section protecting the two lists. */
82 RTCRITSECT CritSect;
83 /** The list of free solaris URBs. Singly linked. */
84 PUSBPROXYURBSOL pFreeHead;
85 /** The list of active solaris URBs. Doubly linked.
86 * We must maintain this so we can properly reap URBs of a detached device.
87 * Only the split head will appear in this list. */
88 PUSBPROXYURBSOL pInFlightHead;
89 /** The list of landed solaris URBs. Doubly linked.
90 * Only the split head will appear in this list. */
91 PUSBPROXYURBSOL pTaxingHead;
92 /** The tail of the landed solaris URBs. */
93 PUSBPROXYURBSOL pTaxingTail;
94 /** Pipe handle for waking up - writing end. */
95 RTPIPE hPipeWakeupW;
96 /** Pipe handle for waking up - reading end. */
97 RTPIPE hPipeWakeupR;
98} USBPROXYDEVSOL, *PUSBPROXYDEVSOL;
99
100static PVUSBURB usbProxySolarisUrbComplete(PUSBPROXYDEVSOL pDevSol);
101
102
103/**
104 * Allocates a Solaris URB request structure.
105 *
106 * @returns Pointer to an active URB request.
107 * @returns NULL on failure.
108 *
109 * @param pDevSol The solaris USB device.
110 */
111static PUSBPROXYURBSOL usbProxySolarisUrbAlloc(PUSBPROXYDEVSOL pDevSol)
112{
113 PUSBPROXYURBSOL pUrbSol;
114
115 RTCritSectEnter(&pDevSol->CritSect);
116
117 /*
118 * Try remove a Solaris URB from the free list, if none there allocate a new one.
119 */
120 pUrbSol = pDevSol->pFreeHead;
121 if (pUrbSol)
122 pDevSol->pFreeHead = pUrbSol->pNext;
123 else
124 {
125 RTCritSectLeave(&pDevSol->CritSect);
126 pUrbSol = (PUSBPROXYURBSOL)RTMemAlloc(sizeof(*pUrbSol));
127 if (!pUrbSol)
128 return NULL;
129 RTCritSectEnter(&pDevSol->CritSect);
130 }
131 pUrbSol->pVUsbUrb = NULL;
132 pUrbSol->pDevSol = pDevSol;
133
134 /*
135 * Link it into the active list
136 */
137 pUrbSol->pPrev = NULL;
138 pUrbSol->pNext = pDevSol->pInFlightHead;
139 if (pUrbSol->pNext)
140 pUrbSol->pNext->pPrev = pUrbSol;
141 pDevSol->pInFlightHead = pUrbSol;
142
143 RTCritSectLeave(&pDevSol->CritSect);
144 return pUrbSol;
145}
146
147
148/**
149 * Frees a Solaris URB request structure.
150 *
151 * @param pDevSol The Solaris USB device.
152 * @param pUrbSol The Solaris URB to free.
153 */
154static void usbProxySolarisUrbFree(PUSBPROXYDEVSOL pDevSol, PUSBPROXYURBSOL pUrbSol)
155{
156 RTCritSectEnter(&pDevSol->CritSect);
157
158 /*
159 * Remove from the active or taxing list.
160 */
161 if (pUrbSol->pNext)
162 pUrbSol->pNext->pPrev = pUrbSol->pPrev;
163 else if (pDevSol->pTaxingTail == pUrbSol)
164 pDevSol->pTaxingTail = pUrbSol->pPrev;
165
166 if (pUrbSol->pPrev)
167 pUrbSol->pPrev->pNext = pUrbSol->pNext;
168 else if (pDevSol->pTaxingHead == pUrbSol)
169 pDevSol->pTaxingHead = pUrbSol->pNext;
170 else if (pDevSol->pInFlightHead == pUrbSol)
171 pDevSol->pInFlightHead = pUrbSol->pNext;
172 else
173 AssertFailed();
174
175 /*
176 * Link it into the free list.
177 */
178 pUrbSol->pPrev = NULL;
179 pUrbSol->pNext = pDevSol->pFreeHead;
180 pDevSol->pFreeHead = pUrbSol;
181
182 pUrbSol->pVUsbUrb = NULL;
183 pUrbSol->pDevSol = NULL;
184
185 RTCritSectLeave(&pDevSol->CritSect);
186}
187
188
189/*
190 * Close the connection to the USB client driver.
191 *
192 * This is required because our userland enumeration relies on drivers/device trees
193 * to recognize active devices, and hence if this device is unplugged we should no
194 * longer keep the client driver loaded.
195 */
196static void usbProxySolarisCloseFile(PUSBPROXYDEVSOL pDevSol)
197{
198 RTFileClose(pDevSol->hFile);
199 pDevSol->hFile = NIL_RTFILE;
200}
201
202
203/**
204 * The client driver IOCtl Wrapper function.
205 *
206 * @returns VBox status code.
207 * @param pDevSol The Solaris device instance.
208 * @param Function The Function.
209 * @param pvData Opaque pointer to the data.
210 * @param cbData Size of the data pointed to by pvData.
211 */
212static int usbProxySolarisIOCtl(PUSBPROXYDEVSOL pDevSol, unsigned Function, void *pvData, size_t cbData)
213{
214 if (RT_UNLIKELY(pDevSol->hFile == NIL_RTFILE))
215 {
216 LogFlow((USBPROXY ":usbProxySolarisIOCtl connection to driver gone!\n"));
217 return VERR_VUSB_DEVICE_NOT_ATTACHED;
218 }
219
220 VBOXUSBREQ Req;
221 Req.u32Magic = VBOXUSB_MAGIC;
222 Req.rc = -1;
223 Req.cbData = cbData;
224 Req.pvDataR3 = pvData;
225
226 int Ret = -1;
227 int rc = RTFileIoCtl(pDevSol->hFile, Function, &Req, sizeof(Req), &Ret);
228 if (RT_SUCCESS(rc))
229 {
230 if (RT_FAILURE(Req.rc))
231 {
232 if (Req.rc == VERR_VUSB_DEVICE_NOT_ATTACHED)
233 {
234 pDevSol->pProxyDev->fDetached = true;
235 usbProxySolarisCloseFile(pDevSol);
236 LogRel((USBPROXY ":Command %#x failed, USB Device '%s' disconnected!\n", Function, pDevSol->pProxyDev->pUsbIns->pszName));
237 }
238 else
239 LogRel((USBPROXY ":Command %#x failed. Req.rc=%Rrc\n", Function, Req.rc));
240 }
241
242 return Req.rc;
243 }
244
245 LogRel((USBPROXY ":Function %#x failed. rc=%Rrc\n", Function, rc));
246 return rc;
247}
248
249
250/**
251 * Get the active configuration from the device. The first time this is called
252 * our client driver would returned the cached configuration since the device is first plugged in.
253 * Subsequent get configuration requests are passed on to the device.
254 *
255 * @returns VBox status code.
256 * @param pDevSol The Solaris device instance.
257 *
258 */
259static inline int usbProxySolarisGetActiveConfig(PUSBPROXYDEVSOL pDevSol)
260{
261 VBOXUSBREQ_GET_CONFIG GetConfigReq;
262 bzero(&GetConfigReq, sizeof(GetConfigReq));
263 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_GET_CONFIG, &GetConfigReq, sizeof(GetConfigReq));
264 if (RT_SUCCESS(rc))
265 {
266 pDevSol->pProxyDev->iActiveCfg = GetConfigReq.bConfigValue;
267 pDevSol->pProxyDev->cIgnoreSetConfigs = 0;
268 }
269 else
270 {
271 if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
272 LogRel((USBPROXY ":Failed to get configuration. rc=%Rrc\n", rc));
273
274 pDevSol->pProxyDev->iActiveCfg = -1;
275 pDevSol->pProxyDev->cIgnoreSetConfigs = 0;
276 }
277 return rc;
278}
279
280
281/**
282 * Opens the USB device.
283 *
284 * @returns VBox status code.
285 * @param pProxyDev The device instance.
286 * @param pszAddress The unique device identifier.
287 * The format of this string is "VendorId:ProducIt:Release:StaticPath".
288 * @param pvBackend Backend specific pointer, unused for the solaris backend.
289 */
290static DECLCALLBACK(int) usbProxySolarisOpen(PUSBPROXYDEV pProxyDev, const char *pszAddress, void *pvBackend)
291{
292 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
293
294 LogFlowFunc((USBPROXY ":usbProxySolarisOpen pProxyDev=%p pszAddress=%s pvBackend=%p\n", pProxyDev, pszAddress, pvBackend));
295
296 /*
297 * Initialize our USB R3 lib.
298 */
299 int rc = USBLibInit();
300 if (RT_SUCCESS(rc))
301 {
302 /*
303 * Allocate and initialize the solaris backend data.
304 */
305 AssertCompile(PATH_MAX >= MAXPATHLEN);
306 char szDeviceIdent[PATH_MAX+48];
307 rc = RTStrPrintf(szDeviceIdent, sizeof(szDeviceIdent), "%s", pszAddress);
308 if (RT_SUCCESS(rc))
309 {
310 rc = RTCritSectInit(&pDevSol->CritSect);
311 if (RT_SUCCESS(rc))
312 {
313 /*
314 * Create wakeup pipe.
315 */
316 rc = RTPipeCreate(&pDevSol->hPipeWakeupR, &pDevSol->hPipeWakeupW, 0);
317 if (RT_SUCCESS(rc))
318 {
319 int Instance;
320 char *pszDevicePath = NULL;
321 rc = USBLibGetClientInfo(szDeviceIdent, &pszDevicePath, &Instance);
322 if (RT_SUCCESS(rc))
323 {
324 pDevSol->pszDevicePath = pszDevicePath;
325
326 /*
327 * Open the client driver.
328 */
329 RTFILE hFile;
330 rc = RTFileOpen(&hFile, pDevSol->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
331 if (RT_SUCCESS(rc))
332 {
333 pDevSol->hFile = hFile;
334 pDevSol->pProxyDev = pProxyDev;
335
336 /*
337 * Verify client driver version.
338 */
339 VBOXUSBREQ_GET_VERSION GetVersionReq;
340 bzero(&GetVersionReq, sizeof(GetVersionReq));
341 rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_GET_VERSION, &GetVersionReq, sizeof(GetVersionReq));
342 if (RT_SUCCESS(rc))
343 {
344 if ( GetVersionReq.u32Major == VBOXUSB_VERSION_MAJOR
345 && GetVersionReq.u32Minor >= VBOXUSB_VERSION_MINOR)
346 {
347 /*
348 * Try & get the current cached config from Solaris.
349 */
350 usbProxySolarisGetActiveConfig(pDevSol);
351 return VINF_SUCCESS;
352 }
353 else
354 {
355 LogRel((USBPROXY ":version mismatch! driver v%d.%d expecting ~v%d.%d\n", GetVersionReq.u32Major,
356 GetVersionReq.u32Minor, VBOXUSB_VERSION_MAJOR, VBOXUSB_VERSION_MINOR));
357 rc = VERR_VERSION_MISMATCH;
358 }
359 }
360 else
361 LogRel((USBPROXY ":failed to query driver version. rc=%Rrc\n", rc));
362
363 RTFileClose(pDevSol->hFile);
364 pDevSol->hFile = NIL_RTFILE;
365 pDevSol->pProxyDev = NULL;
366 }
367 else
368 LogRel((USBPROXY ":failed to open device. rc=%Rrc pszDevicePath=%s\n", rc, pDevSol->pszDevicePath));
369
370 RTStrFree(pDevSol->pszDevicePath);
371 pDevSol->pszDevicePath = NULL;
372 }
373 else
374 {
375 LogRel((USBPROXY ":failed to get client info. rc=%Rrc pszDevicePath=%s\n", rc, pDevSol->pszDevicePath));
376 if (rc == VERR_NOT_FOUND)
377 rc = VERR_OPEN_FAILED;
378 }
379 RTPipeClose(pDevSol->hPipeWakeupR);
380 RTPipeClose(pDevSol->hPipeWakeupW);
381 }
382
383 RTCritSectDelete(&pDevSol->CritSect);
384 }
385 else
386 LogRel((USBPROXY ":RTCritSectInit failed. rc=%Rrc pszAddress=%s\n", rc, pszAddress));
387 }
388 else
389 LogRel((USBPROXY ":RTStrAPrintf failed. rc=%Rrc pszAddress=%s\n", rc, pszAddress));
390 }
391 else
392 LogRel((USBPROXY ":USBLibInit failed. rc=%Rrc\n", rc));
393
394 USBLibTerm();
395 return rc;
396}
397
398
399/**
400 * Close the USB device.
401 *
402 * @param pProxyDev The device instance.
403 */
404static DECLCALLBACK(void) usbProxySolarisClose(PUSBPROXYDEV pProxyDev)
405{
406 LogFlow((USBPROXY ":usbProxySolarisClose: pProxyDev=%p\n", pProxyDev));
407
408 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
409
410 /* Close the device (do not re-enumerate). */
411 VBOXUSBREQ_CLOSE_DEVICE CloseReq;
412 CloseReq.ResetLevel = VBOXUSB_RESET_LEVEL_CLOSE;
413 usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_CLOSE_DEVICE, &CloseReq, sizeof(CloseReq));
414
415 pProxyDev->fDetached = true;
416 usbProxySolarisCloseFile(pDevSol);
417
418 /*
419 * Now we can close it and free all the resources.
420 */
421 RTCritSectDelete(&pDevSol->CritSect);
422
423 PUSBPROXYURBSOL pUrbSol = NULL;
424 while ((pUrbSol = pDevSol->pInFlightHead) != NULL)
425 {
426 pDevSol->pInFlightHead = pUrbSol->pNext;
427 RTMemFree(pUrbSol);
428 }
429
430 while ((pUrbSol = pDevSol->pFreeHead) != NULL)
431 {
432 pDevSol->pFreeHead = pUrbSol->pNext;
433 RTMemFree(pUrbSol);
434 }
435
436 RTPipeClose(pDevSol->hPipeWakeupR);
437 RTPipeClose(pDevSol->hPipeWakeupW);
438
439 RTStrFree(pDevSol->pszDevicePath);
440 pDevSol->pszDevicePath = NULL;
441
442 USBLibTerm();
443}
444
445
446/**
447 * Reset the device.
448 *
449 * @returns VBox status code.
450 * @param pProxyDev The device to reset.
451 * @param fRootHubReset Is this a root hub reset or device specific reset request.
452 */
453static DECLCALLBACK(int) usbProxySolarisReset(PUSBPROXYDEV pProxyDev, bool fRootHubReset)
454{
455 LogFlowFunc((USBPROXY ":usbProxySolarisReset pProxyDev=%s fRootHubReset=%d\n", pProxyDev->pUsbIns->pszName, fRootHubReset));
456
457 /** Pass all resets to the device. The Trekstor USB (1.1) stick requires this to work. */
458 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
459
460 /* Soft reset the device. */
461 VBOXUSBREQ_CLOSE_DEVICE CloseReq;
462 CloseReq.ResetLevel = VBOXUSB_RESET_LEVEL_SOFT;
463 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_CLOSE_DEVICE, &CloseReq, sizeof(CloseReq));
464 if (RT_SUCCESS(rc))
465 {
466 /* Get the active config. Solaris USBA sets a default config. */
467 usbProxySolarisGetActiveConfig(pDevSol);
468 }
469 else if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
470 LogRel((USBPROXY ":usbProxySolarisReset failed. rc=%d\n", rc));
471
472 return rc;
473}
474
475
476/**
477 * Set the active configuration.
478 *
479 * The caller makes sure that it's not called first time after open or reset
480 * with the active interface.
481 *
482 * @returns success indicator.
483 * @param pProxyDev The device instance data.
484 * @param iCfg The configuration value to set.
485 */
486static DECLCALLBACK(int) usbProxySolarisSetConfig(PUSBPROXYDEV pProxyDev, int iCfg)
487{
488 LogFlowFunc((USBPROXY ":usbProxySolarisSetConfig: pProxyDev=%p iCfg=%#x\n", pProxyDev, iCfg));
489
490 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
491 AssertPtrReturn(pDevSol, VERR_INVALID_POINTER);
492
493 VBOXUSBREQ_SET_CONFIG SetConfigReq;
494 SetConfigReq.bConfigValue = iCfg;
495 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_SET_CONFIG, &SetConfigReq, sizeof(SetConfigReq));
496 if ( RT_FAILURE(rc)
497 && rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
498 LogRel((USBPROXY ":usbProxySolarisSetConfig failed to switch configuration. rc=%Rrc\n", rc));
499
500 return rc;
501}
502
503
504/**
505 * Claims an interface.
506 *
507 * This is a stub on Solaris since we release/claim all interfaces at
508 * as and when required with endpoint opens.
509 *
510 * @returns success indicator (always true).
511 */
512static DECLCALLBACK(int) usbProxySolarisClaimInterface(PUSBPROXYDEV pProxyDev, int iIf)
513{
514 return VINF_SUCCESS;
515}
516
517
518/**
519 * Releases an interface.
520 *
521 * This is a stub on Solaris since we release/claim all interfaces at
522 * as and when required with endpoint opens.
523 *
524 * @returns success indicator.
525 */
526static DECLCALLBACK(int) usbProxySolarisReleaseInterface(PUSBPROXYDEV pProxyDev, int iIf)
527{
528 return VINF_SUCCESS;
529}
530
531
532/**
533 * Specify an alternate setting for the specified interface of the current configuration.
534 *
535 * @returns success indicator.
536 */
537static DECLCALLBACK(int) usbProxySolarisSetInterface(PUSBPROXYDEV pProxyDev, int iIf, int iAlt)
538{
539 LogFlowFunc((USBPROXY ":usbProxySolarisSetInterface: pProxyDev=%p iIf=%d iAlt=%d\n", pProxyDev, iIf, iAlt));
540
541 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
542 AssertPtrReturn(pDevSol, VERR_INVALID_POINTER);
543
544 VBOXUSBREQ_SET_INTERFACE SetInterfaceReq;
545 SetInterfaceReq.bInterface = iIf;
546 SetInterfaceReq.bAlternate = iAlt;
547 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_SET_INTERFACE, &SetInterfaceReq, sizeof(SetInterfaceReq));
548 if ( RT_FAILURE(rc)
549 && rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
550 LogRel((USBPROXY ":usbProxySolarisSetInterface failed to set interface. rc=%Rrc\n", rc));
551
552 return rc;
553}
554
555
556/**
557 * Clears the halted endpoint 'EndPt'.
558 */
559static DECLCALLBACK(int) usbProxySolarisClearHaltedEp(PUSBPROXYDEV pProxyDev, unsigned int EndPt)
560{
561 LogFlowFunc((USBPROXY ":usbProxySolarisClearHaltedEp pProxyDev=%p EndPt=%#x\n", pProxyDev, EndPt));
562
563 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
564 AssertPtrReturn(pDevSol, VERR_INVALID_POINTER);
565
566 VBOXUSBREQ_CLEAR_EP ClearEpReq;
567 ClearEpReq.bEndpoint = EndPt;
568 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_CLEAR_EP, &ClearEpReq, sizeof(ClearEpReq));
569 if ( RT_FAILURE(rc)
570 && rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
571 LogRel((USBPROXY ":usbProxySolarisClearHaltedEp failed! rc=%Rrc\n", rc));
572
573 return rc;
574}
575
576
577/**
578 * @copydoc USBPROXYBACK::pfnUrbQueue
579 */
580static DECLCALLBACK(int) usbProxySolarisUrbQueue(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
581{
582 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
583
584 LogFlowFunc((USBPROXY ": usbProxySolarisUrbQueue: pProxyDev=%s pUrb=%p EndPt=%#x enmDir=%d cbData=%d pvData=%p\n",
585 pProxyDev->pUsbIns->pszName, pUrb, pUrb->EndPt, pUrb->enmDir, pUrb->cbData, pUrb->abData));
586
587 PUSBPROXYURBSOL pUrbSol = usbProxySolarisUrbAlloc(pDevSol);
588 if (RT_UNLIKELY(!pUrbSol))
589 {
590 LogRel((USBPROXY ":usbProxySolarisUrbQueue: Failed to allocate URB.\n"));
591 return VERR_NO_MEMORY;
592 }
593
594 pUrbSol->pVUsbUrb = pUrb;
595 pUrbSol->pDevSol = pDevSol;
596
597 uint8_t EndPt = pUrb->EndPt;
598 if (EndPt)
599 EndPt |= pUrb->enmDir == VUSBDIRECTION_IN ? VUSB_DIR_TO_HOST : VUSB_DIR_TO_DEVICE;
600
601 VBOXUSBREQ_URB UrbReq;
602 UrbReq.pvUrbR3 = pUrbSol;
603 UrbReq.bEndpoint = EndPt;
604 UrbReq.enmType = pUrb->enmType;
605 UrbReq.enmDir = pUrb->enmDir;
606 UrbReq.enmStatus = pUrb->enmStatus;
607 UrbReq.fShortOk = !pUrb->fShortNotOk;
608 UrbReq.cbData = pUrb->cbData;
609 UrbReq.pvData = pUrb->abData;
610 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
611 {
612 UrbReq.cIsocPkts = pUrb->cIsocPkts;
613 for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
614 {
615 UrbReq.aIsocPkts[i].cbPkt = pUrb->aIsocPkts[i].cb;
616 UrbReq.aIsocPkts[i].cbActPkt = 0;
617 UrbReq.aIsocPkts[i].enmStatus = VUSBSTATUS_INVALID;
618 }
619 }
620
621 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_SEND_URB, &UrbReq, sizeof(UrbReq));
622 if (RT_SUCCESS(rc))
623 {
624 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
625 LogFlow((USBPROXY ":usbProxySolarisUrbQueue success cbData=%d.\n", pUrb->cbData));
626 pUrb->Dev.pvPrivate = pUrbSol;
627 return VINF_SUCCESS;
628 }
629
630 if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
631 LogRel((USBPROXY ":usbProxySolarisUrbQueue Failed!! pProxyDev=%s pUrb=%p EndPt=%#x bEndpoint=%#x enmType=%d enmDir=%d cbData=%u rc=%Rrc\n",
632 pProxyDev->pUsbIns->pszName, pUrb, pUrb->EndPt, UrbReq.bEndpoint, pUrb->enmType, pUrb->enmDir, pUrb->cbData, rc));
633
634 return rc;
635}
636
637
638/**
639 * Cancels a URB.
640 *
641 * The URB requires reaping, so we don't change its state.
642 * @remark There isn't any way to cancel a specific asynchronous request
643 * on Solaris. So we just abort pending URBs on the pipe.
644 */
645static DECLCALLBACK(int) usbProxySolarisUrbCancel(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
646{
647 PUSBPROXYURBSOL pUrbSol = (PUSBPROXYURBSOL)pUrb->Dev.pvPrivate;
648 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
649 AssertPtrReturn(pDevSol, VERR_INVALID_POINTER);
650
651 LogFlowFunc((USBPROXY ":usbProxySolarisUrbCancel pUrb=%p pUrbSol=%p pDevSol=%p\n", pUrb, pUrbSol, pUrbSol->pDevSol));
652
653 /* Aborting the control pipe isn't supported, pretend success. */
654 if (!pUrb->EndPt)
655 return VINF_SUCCESS;
656
657 VBOXUSBREQ_ABORT_PIPE AbortPipeReq;
658 AbortPipeReq.bEndpoint = pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? VUSB_DIR_TO_HOST : VUSB_DIR_TO_DEVICE);
659 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_ABORT_PIPE, &AbortPipeReq, sizeof(AbortPipeReq));
660 if ( RT_FAILURE(rc)
661 && rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
662 LogRel((USBPROXY ":usbProxySolarisUrbCancel failed to abort pipe. rc=%Rrc\n", rc));
663
664 LogFlow((USBPROXY ":usbProxySolarisUrbCancel: rc=%Rrc.\n", rc));
665 return rc;
666}
667
668
669/**
670 * Reap URBs in-flight on a device.
671 *
672 * @returns Pointer to a completed URB.
673 * @returns NULL if no URB was completed.
674 * @param pProxyDev The device.
675 * @param cMillies Number of milliseconds to wait. Use 0 to not wait at all.
676 */
677static DECLCALLBACK(PVUSBURB) usbProxySolarisUrbReap(PUSBPROXYDEV pProxyDev, RTMSINTERVAL cMillies)
678{
679 //LogFlowFunc((USBPROXY ":usbProxySolarisUrbReap pProxyDev=%p cMillies=%u\n", pProxyDev, cMillies));
680
681 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
682
683 /*
684 * Don't block if nothing is in the air.
685 */
686 if (!pDevSol->pInFlightHead)
687 return NULL;
688
689 /*
690 * Deque URBs inflight or those landed.
691 */
692 if (cMillies > 0)
693 {
694 for (;;)
695 {
696 int cMilliesWait = cMillies == RT_INDEFINITE_WAIT ? -1 : cMillies;
697 struct pollfd pfd[2];
698
699 pfd[0].fd = RTFileToNative(pDevSol->hFile);
700 pfd[0].events = POLLIN;
701 pfd[0].revents = 0;
702
703 pfd[1].fd = RTPipeToNative(pDevSol->hPipeWakeupR);
704 pfd[1].events = POLLIN;
705 pfd[1].revents = 0;
706
707 int rc = poll(&pfd[0], 2, cMilliesWait);
708 if (rc > 0)
709 {
710 if (pfd[0].revents & POLLHUP)
711 {
712 LogRel((USBPROXY ":Reaping failed, USB Device '%s' disconnected!\n", pDevSol->pProxyDev->pUsbIns->pszName));
713 pProxyDev->fDetached = true;
714 usbProxySolarisCloseFile(pDevSol);
715 }
716
717 if (pfd[1].revents & POLLIN)
718 {
719 /* Got woken up, drain pipe. */
720 uint8_t bRead;
721 size_t cbIgnored = 0;
722 RTPipeRead(pDevSol->hPipeWakeupR, &bRead, 1, &cbIgnored);
723
724 /*
725 * It is possible that we got woken up and have an URB pending
726 * for completion. Do it on the way out. Otherwise return
727 * immediately to the caller.
728 */
729 if (!(pfd[0].revents & POLLIN))
730 return NULL;
731 }
732
733 break;
734 }
735
736 if (rc == 0)
737 {
738 //LogFlow((USBPROXY ":usbProxySolarisUrbReap: Timed out\n"));
739 return NULL;
740 }
741 else if (rc != EAGAIN)
742 {
743 LogFlow((USBPROXY ":usbProxySolarisUrbReap Poll rc=%d errno=%d\n", rc, errno));
744 return NULL;
745 }
746 }
747 }
748
749 usbProxySolarisUrbComplete(pDevSol);
750
751 /*
752 * Any URBs pending delivery?
753 */
754 PVUSBURB pUrb = NULL;
755 while (pDevSol->pTaxingHead)
756 {
757 RTCritSectEnter(&pDevSol->CritSect);
758
759 PUSBPROXYURBSOL pUrbSol = pDevSol->pTaxingHead;
760 if (pUrbSol)
761 {
762 pUrb = pUrbSol->pVUsbUrb;
763 if (pUrb)
764 {
765 pUrb->Dev.pvPrivate = NULL;
766 usbProxySolarisUrbFree(pDevSol, pUrbSol);
767 }
768 }
769 RTCritSectLeave(&pDevSol->CritSect);
770 }
771
772 return pUrb;
773}
774
775
776/**
777 * Reads a completed/error'd URB from the client driver (no waiting).
778 *
779 * @param pDevSol The Solaris device instance.
780 */
781static PVUSBURB usbProxySolarisUrbComplete(PUSBPROXYDEVSOL pDevSol)
782{
783 LogFlowFunc((USBPROXY ":usbProxySolarisUrbComplete pDevSol=%p\n", pDevSol));
784
785 VBOXUSBREQ_URB UrbReq;
786 bzero(&UrbReq, sizeof(UrbReq));
787
788 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_REAP_URB, &UrbReq, sizeof(UrbReq));
789 if (RT_SUCCESS(rc))
790 {
791 if (UrbReq.pvUrbR3)
792 {
793 PUSBPROXYURBSOL pUrbSol = (PUSBPROXYURBSOL)UrbReq.pvUrbR3;
794 PVUSBURB pUrb = pUrbSol->pVUsbUrb;
795 if (RT_LIKELY(pUrb))
796 {
797 Assert(pUrb->u32Magic == VUSBURB_MAGIC);
798
799 /*
800 * Update the URB.
801 */
802 if ( pUrb->enmType == VUSBXFERTYPE_ISOC
803 && pUrb->enmDir == VUSBDIRECTION_IN)
804 {
805 size_t cbData = 0;
806 for (unsigned i = 0; i < UrbReq.cIsocPkts; i++)
807 {
808 pUrb->aIsocPkts[i].cb = UrbReq.aIsocPkts[i].cbActPkt;
809 cbData += UrbReq.aIsocPkts[i].cbActPkt;
810 pUrb->aIsocPkts[i].enmStatus = UrbReq.aIsocPkts[i].enmStatus;
811 }
812
813 LogFlow((USBPROXY ":usbProxySolarisUrbComplete ISOC cbData=%d cbActPktSum=%d\n", pUrb->cbData, cbData));
814 pUrb->cbData = cbData;
815 pUrb->enmStatus = UrbReq.enmStatus;
816 }
817 else
818 {
819 pUrb->cbData = UrbReq.cbData;
820 pUrb->enmStatus = UrbReq.enmStatus;
821 }
822
823 RTCritSectEnter(&pDevSol->CritSect);
824
825 /*
826 * Remove from the active list.
827 */
828 if (pUrbSol->pNext)
829 pUrbSol->pNext->pPrev = pUrbSol->pPrev;
830 if (pUrbSol->pPrev)
831 pUrbSol->pPrev->pNext = pUrbSol->pNext;
832 else
833 {
834 Assert(pDevSol->pInFlightHead == pUrbSol);
835 pDevSol->pInFlightHead = pUrbSol->pNext;
836 }
837
838 /*
839 * Link it into the taxing list.
840 */
841 pUrbSol->pNext = NULL;
842 pUrbSol->pPrev = pDevSol->pTaxingTail;
843 if (pDevSol->pTaxingTail)
844 pDevSol->pTaxingTail->pNext = pUrbSol;
845 else
846 pDevSol->pTaxingHead = pUrbSol;
847 pDevSol->pTaxingTail = pUrbSol;
848
849 RTCritSectLeave(&pDevSol->CritSect);
850
851 LogFlow((USBPROXY "usbProxySolarisUrbComplete: cb=%d EndPt=%#x enmDir=%d enmStatus=%s (%d) \n",
852 pUrb->cbData, pUrb->EndPt, pUrb->enmDir, pUrb->enmStatus == VUSBSTATUS_OK ? "OK" : "** Failed **", pUrb->enmStatus));
853// if (pUrb->cbData < 2049)
854// LogFlow((USBPROXY "%.*Rhxd\n", pUrb->cbData, pUrb->abData));
855 return pUrb;
856 }
857 }
858 }
859 else
860 {
861 if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
862 LogRel((USBPROXY ":Reaping URB failed. rc=%Rrc\n", rc));
863 }
864
865 return NULL;
866}
867
868
869static DECLCALLBACK(int) usbProxySolarisWakeup(PUSBPROXYDEV pProxyDev)
870{
871 PUSBPROXYDEVSOL pDevSol = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVSOL);
872 size_t cbIgnored;
873
874 LogFlowFunc(("pProxyDev=%p\n", pProxyDev));
875
876 return RTPipeWrite(pDevSol->hPipeWakeupW, "", 1, &cbIgnored);
877}
878
879
880/**
881 * The Solaris USB Proxy Backend.
882 */
883extern const USBPROXYBACK g_USBProxyDeviceHost =
884{
885 /* pszName */
886 "host",
887 /* cbBackend */
888 sizeof(USBPROXYDEVSOL),
889 usbProxySolarisOpen,
890 NULL,
891 usbProxySolarisClose,
892 usbProxySolarisReset,
893 usbProxySolarisSetConfig,
894 usbProxySolarisClaimInterface,
895 usbProxySolarisReleaseInterface,
896 usbProxySolarisSetInterface,
897 usbProxySolarisClearHaltedEp,
898 usbProxySolarisUrbQueue,
899 usbProxySolarisUrbCancel,
900 usbProxySolarisUrbReap,
901 usbProxySolarisWakeup,
902 0
903};
904
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