VirtualBox

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

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

Try to fix solaris burn

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.2 KB
Line 
1/* $Id: USBProxyDevice-solaris.cpp 49816 2013-12-06 21:50:59Z vboxsync $ */
2/** @file
3 * USB device proxy - the Solaris backend.
4 */
5
6/*
7 * Copyright (C) 2009-2013 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 waiking up - writing end. */
95 RTPIPE hPipeWakeupW;
96 /** Pipe handle for waiking up - reading end. */
97 RTPIPE hPipeWakeupR;
98} USBPROXYDEVSOL, *PUSBPROXYDEVSOL;
99
100PVUSBURB 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 int usbProxySolarisOpen(PUSBPROXYDEV pProxyDev, const char *pszAddress, void *pvBackend)
291{
292 LogFlowFunc((USBPROXY ":usbProxySolarisOpen pProxyDev=%p pszAddress=%s pvBackend=%p\n", pProxyDev, pszAddress, pvBackend));
293
294 /*
295 * Initialize our USB R3 lib.
296 */
297 int rc = USBLibInit();
298 if (RT_SUCCESS(rc))
299 {
300 /*
301 * Allocate and initialize the solaris backend data.
302 */
303 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)RTMemAllocZ(sizeof(*pDevSol));
304 if (RT_LIKELY(pDevSol))
305 {
306 AssertCompile(PATH_MAX >= MAXPATHLEN);
307 char szDeviceIdent[PATH_MAX+48];
308 rc = RTStrPrintf(szDeviceIdent, sizeof(szDeviceIdent), "%s", pszAddress);
309 if (RT_SUCCESS(rc))
310 {
311 rc = RTCritSectInit(&pDevSol->CritSect);
312 if (RT_SUCCESS(rc))
313 {
314 pProxyDev->Backend.pv = pDevSol;
315
316 /*
317 * Create wakeup pipe.
318 */
319 rc = RTPipeCreate(&pDevSol->hPipeWakeupR, &pDevSol->hPipeWakeupW, 0);
320 if (RT_SUCCESS(rc))
321 {
322 int Instance;
323 char *pszDevicePath = NULL;
324 rc = USBLibGetClientInfo(szDeviceIdent, &pszDevicePath, &Instance);
325 if (RT_SUCCESS(rc))
326 {
327 pDevSol->pszDevicePath = pszDevicePath;
328
329 /*
330 * Open the client driver.
331 */
332 RTFILE hFile;
333 rc = RTFileOpen(&hFile, pDevSol->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
334 if (RT_SUCCESS(rc))
335 {
336 pDevSol->hFile = hFile;
337 pDevSol->pProxyDev = pProxyDev;
338
339 /*
340 * Verify client driver version.
341 */
342 VBOXUSBREQ_GET_VERSION GetVersionReq;
343 bzero(&GetVersionReq, sizeof(GetVersionReq));
344 rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_GET_VERSION, &GetVersionReq, sizeof(GetVersionReq));
345 if (RT_SUCCESS(rc))
346 {
347 if ( GetVersionReq.u32Major == VBOXUSB_VERSION_MAJOR
348 && GetVersionReq.u32Minor >= VBOXUSB_VERSION_MINOR)
349 {
350 /*
351 * Try & get the current cached config from Solaris.
352 */
353 usbProxySolarisGetActiveConfig(pDevSol);
354 return VINF_SUCCESS;
355 }
356 else
357 {
358 LogRel((USBPROXY ":version mismatch! driver v%d.%d expecting ~v%d.%d\n", GetVersionReq.u32Major,
359 GetVersionReq.u32Minor, VBOXUSB_VERSION_MAJOR, VBOXUSB_VERSION_MINOR));
360 rc = VERR_VERSION_MISMATCH;
361 }
362 }
363 else
364 {
365 LogRel((USBPROXY ":failed to query driver version. rc=%Rrc\n", rc));
366 }
367
368 RTFileClose(pDevSol->hFile);
369 pDevSol->hFile = NIL_RTFILE;
370 pDevSol->pProxyDev = NULL;
371 }
372 else
373 LogRel((USBPROXY ":failed to open device. rc=%Rrc pszDevicePath=%s\n", rc, pDevSol->pszDevicePath));
374
375 RTStrFree(pDevSol->pszDevicePath);
376 pDevSol->pszDevicePath = NULL;
377 }
378 else
379 {
380 LogRel((USBPROXY ":failed to get client info. rc=%Rrc pszDevicePath=%s\n", rc, pDevSol->pszDevicePath));
381 if (rc == VERR_NOT_FOUND)
382 rc = VERR_OPEN_FAILED;
383 }
384 RTPipeClose(pDevSol->hPipeWakeupR);
385 RTPipeClose(pDevSol->hPipeWakeupW);
386 }
387
388 RTCritSectDelete(&pDevSol->CritSect);
389 }
390 else
391 LogRel((USBPROXY ":RTCritSectInit failed. rc=%Rrc pszAddress=%s\n", rc, pszAddress));
392 }
393 else
394 LogRel((USBPROXY ":RTStrAPrintf failed. rc=%Rrc pszAddress=%s\n", rc, pszAddress));
395
396 RTMemFree(pDevSol);
397 pDevSol = NULL;
398 }
399 else
400 rc = VERR_NO_MEMORY;
401 }
402 else
403 LogRel((USBPROXY ":USBLibInit failed. rc=%Rrc\n", rc));
404
405 USBLibTerm();
406 pProxyDev->Backend.pv = NULL;
407 return rc;
408}
409
410
411/**
412 * Close the USB device.
413 *
414 * @param pProxyDev The device instance.
415 */
416static void usbProxySolarisClose(PUSBPROXYDEV pProxyDev)
417{
418 LogFlow((USBPROXY ":usbProxySolarisClose: pProxyDev=%p\n", pProxyDev));
419
420 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)pProxyDev->Backend.pv;
421
422 /* Close the device (do not re-enumerate). */
423 VBOXUSBREQ_CLOSE_DEVICE CloseReq;
424 CloseReq.ResetLevel = VBOXUSB_RESET_LEVEL_CLOSE;
425 usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_CLOSE_DEVICE, &CloseReq, sizeof(CloseReq));
426
427 pProxyDev->fDetached = true;
428 usbProxySolarisCloseFile(pDevSol);
429
430 /*
431 * Now we can close it and free all the resources.
432 */
433 RTCritSectDelete(&pDevSol->CritSect);
434
435 PUSBPROXYURBSOL pUrbSol = NULL;
436 while ((pUrbSol = pDevSol->pInFlightHead) != NULL)
437 {
438 pDevSol->pInFlightHead = pUrbSol->pNext;
439 RTMemFree(pUrbSol);
440 }
441
442 while ((pUrbSol = pDevSol->pFreeHead) != NULL)
443 {
444 pDevSol->pFreeHead = pUrbSol->pNext;
445 RTMemFree(pUrbSol);
446 }
447
448 RTPipeClose(pDevSol->hPipeWakeupR);
449 RTPipeClose(pDevSol->hPipeWakeupW);
450
451 RTStrFree(pDevSol->pszDevicePath);
452 pDevSol->pszDevicePath = NULL;
453
454 RTMemFree(pDevSol);
455 pProxyDev->Backend.pv = NULL;
456
457 USBLibTerm();
458}
459
460
461/**
462 * Reset the device.
463 *
464 * @returns VBox status code.
465 * @param pProxyDev The device to reset.
466 * @param fRootHubReset Is this a root hub reset or device specific reset request.
467 */
468static int usbProxySolarisReset(PUSBPROXYDEV pProxyDev, bool fRootHubReset)
469{
470 LogFlowFunc((USBPROXY ":usbProxySolarisReset pProxyDev=%s fRootHubReset=%d\n", pProxyDev->pUsbIns->pszName, fRootHubReset));
471
472 /** Pass all resets to the device. The Trekstor USB (1.1) stick requires this to work. */
473 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)pProxyDev->Backend.pv;
474
475 /* Soft reset the device. */
476 VBOXUSBREQ_CLOSE_DEVICE CloseReq;
477 CloseReq.ResetLevel = VBOXUSB_RESET_LEVEL_SOFT;
478 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_CLOSE_DEVICE, &CloseReq, sizeof(CloseReq));
479 if (RT_SUCCESS(rc))
480 {
481 /* Get the active config. Solaris USBA sets a default config. */
482 usbProxySolarisGetActiveConfig(pDevSol);
483 }
484 else if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
485 LogRel((USBPROXY ":usbProxySolarisReset failed. rc=%d\n", rc));
486
487 return rc;
488}
489
490
491/**
492 * Set the active configuration.
493 *
494 * The caller makes sure that it's not called first time after open or reset
495 * with the active interface.
496 *
497 * @returns success indicator.
498 * @param pProxyDev The device instance data.
499 * @param iCfg The configuration value to set.
500 */
501static int usbProxySolarisSetConfig(PUSBPROXYDEV pProxyDev, int iCfg)
502{
503 LogFlowFunc((USBPROXY ":usbProxySolarisSetConfig: pProxyDev=%p iCfg=%#x\n", pProxyDev, iCfg));
504
505 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)pProxyDev->Backend.pv;
506 AssertPtrReturn(pDevSol, VERR_INVALID_POINTER);
507
508 VBOXUSBREQ_SET_CONFIG SetConfigReq;
509 SetConfigReq.bConfigValue = iCfg;
510 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_SET_CONFIG, &SetConfigReq, sizeof(SetConfigReq));
511 if (RT_SUCCESS(rc))
512 return true;
513
514 if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
515 LogRel((USBPROXY ":usbProxySolarisSetConfig failed to switch configuration. rc=%Rrc\n", rc));
516
517 return false;
518}
519
520
521/**
522 * Claims an interface.
523 *
524 * This is a stub on Solaris since we release/claim all interfaces at
525 * as and when required with endpoint opens.
526 *
527 * @returns success indicator (always true).
528 */
529static int usbProxySolarisClaimInterface(PUSBPROXYDEV pProxyDev, int iIf)
530{
531 return true;
532}
533
534
535/**
536 * Releases an interface.
537 *
538 * This is a stub on Solaris since we release/claim all interfaces at
539 * as and when required with endpoint opens.
540 *
541 * @returns success indicator.
542 */
543static int usbProxySolarisReleaseInterface(PUSBPROXYDEV pProxyDev, int iIf)
544{
545 return true;
546}
547
548
549/**
550 * Specify an alternate setting for the specified interface of the current configuration.
551 *
552 * @returns success indicator.
553 */
554static int usbProxySolarisSetInterface(PUSBPROXYDEV pProxyDev, int iIf, int iAlt)
555{
556 LogFlowFunc((USBPROXY ":usbProxySolarisSetInterface: pProxyDev=%p iIf=%d iAlt=%d\n", pProxyDev, iIf, iAlt));
557
558 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)pProxyDev->Backend.pv;
559 AssertPtrReturn(pDevSol, VERR_INVALID_POINTER);
560
561 VBOXUSBREQ_SET_INTERFACE SetInterfaceReq;
562 SetInterfaceReq.bInterface = iIf;
563 SetInterfaceReq.bAlternate = iAlt;
564 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_SET_INTERFACE, &SetInterfaceReq, sizeof(SetInterfaceReq));
565 if (RT_SUCCESS(rc))
566 return true;
567
568 if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
569 LogRel((USBPROXY ":usbProxySolarisSetInterface failed to set interface. rc=%Rrc\n", rc));
570
571 return false;
572}
573
574
575/**
576 * Clears the halted endpoint 'EndPt'.
577 */
578static bool usbProxySolarisClearHaltedEp(PUSBPROXYDEV pProxyDev, unsigned int EndPt)
579{
580 LogFlowFunc((USBPROXY ":usbProxySolarisClearHaltedEp pProxyDev=%p EndPt=%#x\n", pProxyDev, EndPt));
581
582 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)pProxyDev->Backend.pv;
583 AssertPtrReturn(pDevSol, VERR_INVALID_POINTER);
584
585 VBOXUSBREQ_CLEAR_EP ClearEpReq;
586 ClearEpReq.bEndpoint = EndPt;
587 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_CLEAR_EP, &ClearEpReq, sizeof(ClearEpReq));
588 if (RT_SUCCESS(rc))
589 return true;
590
591 if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
592 LogRel((USBPROXY ":usbProxySolarisClearHaltedEp failed! rc=%Rrc\n", rc));
593
594 return false;
595}
596
597
598/**
599 * @copydoc USBPROXYBACK::pfnUrbQueue
600 */
601static int usbProxySolarisUrbQueue(PVUSBURB pUrb)
602{
603 PUSBPROXYDEV pProxyDev = PDMINS_2_DATA(pUrb->pUsbIns, PUSBPROXYDEV);
604 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)pProxyDev->Backend.pv;
605
606 LogFlowFunc((USBPROXY ": usbProxySolarisUrbQueue: pProxyDev=%s pUrb=%p EndPt=%#x enmDir=%d cbData=%d pvData=%p\n",
607 pProxyDev->pUsbIns->pszName, pUrb, pUrb->EndPt, pUrb->enmDir, pUrb->cbData, pUrb->abData));
608
609 PUSBPROXYURBSOL pUrbSol = usbProxySolarisUrbAlloc(pDevSol);
610 if (RT_UNLIKELY(!pUrbSol))
611 {
612 LogRel((USBPROXY ":usbProxySolarisUrbQueue: Failed to allocate URB.\n"));
613 return false;
614 }
615
616 pUrbSol->pVUsbUrb = pUrb;
617 pUrbSol->pDevSol = pDevSol;
618
619 uint8_t EndPt = pUrb->EndPt;
620 if (EndPt)
621 EndPt |= pUrb->enmDir == VUSBDIRECTION_IN ? VUSB_DIR_TO_HOST : VUSB_DIR_TO_DEVICE;
622
623 VBOXUSBREQ_URB UrbReq;
624 UrbReq.pvUrbR3 = pUrbSol;
625 UrbReq.bEndpoint = EndPt;
626 UrbReq.enmType = pUrb->enmType;
627 UrbReq.enmDir = pUrb->enmDir;
628 UrbReq.enmStatus = pUrb->enmStatus;
629 UrbReq.fShortOk = !pUrb->fShortNotOk;
630 UrbReq.cbData = pUrb->cbData;
631 UrbReq.pvData = pUrb->abData;
632 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
633 {
634 UrbReq.cIsocPkts = pUrb->cIsocPkts;
635 for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
636 {
637 UrbReq.aIsocPkts[i].cbPkt = pUrb->aIsocPkts[i].cb;
638 UrbReq.aIsocPkts[i].cbActPkt = 0;
639 UrbReq.aIsocPkts[i].enmStatus = VUSBSTATUS_INVALID;
640 }
641 }
642
643 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_SEND_URB, &UrbReq, sizeof(UrbReq));
644 if (RT_SUCCESS(rc))
645 {
646 if (pUrb->enmType == VUSBXFERTYPE_ISOC)
647 LogFlow((USBPROXY ":usbProxySolarisUrbQueue success cbData=%d.\n", pUrb->cbData));
648 pUrb->Dev.pvPrivate = pUrbSol;
649 return true;
650 }
651
652 if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
653 LogRel((USBPROXY ":usbProxySolarisUrbQueue Failed!! pProxyDev=%s pUrb=%p EndPt=%#x bEndpoint=%#x enmType=%d enmDir=%d cbData=%u rc=%Rrc\n",
654 pProxyDev->pUsbIns->pszName, pUrb, pUrb->EndPt, UrbReq.bEndpoint, pUrb->enmType, pUrb->enmDir, pUrb->cbData, rc));
655
656 return false;
657}
658
659
660/**
661 * Cancels a URB.
662 *
663 * The URB requires reaping, so we don't change its state.
664 * @remark There isn't any way to cancel a specific asynchronous request
665 * on Solaris. So we just abort pending URBs on the pipe.
666 */
667static void usbProxySolarisUrbCancel(PVUSBURB pUrb)
668{
669 PUSBPROXYURBSOL pUrbSol = (PUSBPROXYURBSOL)pUrb->Dev.pvPrivate;
670
671 PUSBPROXYDEV pProxyDev = PDMINS_2_DATA(pUrb->pUsbIns, PUSBPROXYDEV);
672 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)pProxyDev->Backend.pv;
673 AssertPtrReturnVoid(pDevSol);
674
675 LogFlowFunc((USBPROXY ":usbProxySolarisUrbCancel pUrb=%p pUrbSol=%p pDevSol=%p\n", pUrb, pUrbSol, pUrbSol->pDevSol));
676
677 /* Aborting the control pipe isn't supported, pretend success. */
678 if (!pUrb->EndPt)
679 return;
680
681 VBOXUSBREQ_ABORT_PIPE AbortPipeReq;
682 AbortPipeReq.bEndpoint = pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? VUSB_DIR_TO_HOST : VUSB_DIR_TO_DEVICE);
683 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_ABORT_PIPE, &AbortPipeReq, sizeof(AbortPipeReq));
684 if (RT_FAILURE(rc))
685 {
686 if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
687 LogRel((USBPROXY ":usbProxySolarisUrbCancel failed to abort pipe. rc=%Rrc\n", rc));
688 return;
689 }
690
691 LogFlow((USBPROXY ":usbProxySolarisUrbCancel success.\n", rc));
692}
693
694
695/**
696 * Reap URBs in-flight on a device.
697 *
698 * @returns Pointer to a completed URB.
699 * @returns NULL if no URB was completed.
700 * @param pProxyDev The device.
701 * @param cMillies Number of milliseconds to wait. Use 0 to not wait at all.
702 */
703static PVUSBURB usbProxySolarisUrbReap(PUSBPROXYDEV pProxyDev, RTMSINTERVAL cMillies)
704{
705 //LogFlowFunc((USBPROXY ":usbProxySolarisUrbReap pProxyDev=%p cMillies=%u\n", pProxyDev, cMillies));
706
707 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)pProxyDev->Backend.pv;
708
709 /*
710 * Don't block if nothing is in the air.
711 */
712 if (!pDevSol->pInFlightHead)
713 return NULL;
714
715 /*
716 * Deque URBs inflight or those landed.
717 */
718 if (cMillies > 0)
719 {
720 for (;;)
721 {
722 int cMilliesWait = cMillies == RT_INDEFINITE_WAIT ? -1 : cMillies;
723 struct pollfd pfd[2];
724
725 pfd[0].fd = RTFileToNative(pDevSol->hFile);
726 pfd[0].events = POLLIN;
727 pfd[0].revents = 0;
728
729 pfd[1].fd = RTPipeToNative(pDevSol->hPipeWakeupR);
730 pfd[1].events = POLLIN;
731 pfd[1].revents = 0;
732
733 int rc = poll(&pfd[0], 2, cMilliesWait);
734 if (rc > 0)
735 {
736 if (pfd[0].revents & POLLHUP)
737 {
738 LogRel((USBPROXY ":Reaping failed, USB Device '%s' disconnected!\n", pDevSol->pProxyDev->pUsbIns->pszName));
739 pProxyDev->fDetached = true;
740 usbProxySolarisCloseFile(pDevSol);
741 }
742
743 if (pfd[1].revents & POLLIN)
744 {
745 /* Got woken up, drain pipe. */
746 uint8_t bRead;
747 size_t cbIgnored = 0;
748 RTPipeRead(pDevSol->hPipeWakeupR, &bRead, 1, &cbIgnored);
749
750 /*
751 * It is possible that we got woken up and have an URB pending
752 * for completion. Do it on the way out. Otherwise return
753 * immediately to the caller.
754 */
755 if (!(pfd[0].revents & POLLIN))
756 return NULL;
757 }
758
759 break;
760 }
761
762 if (rc == 0)
763 {
764 //LogFlow((USBPROXY ":usbProxySolarisUrbReap: Timed out\n"));
765 return NULL;
766 }
767 else if (rc != EAGAIN)
768 {
769 LogFlow((USBPROXY ":usbProxySolarisUrbReap Poll rc=%d errno=%d\n", rc, errno));
770 return NULL;
771 }
772 }
773 }
774
775 usbProxySolarisUrbComplete(pDevSol);
776
777 /*
778 * Any URBs pending delivery?
779 */
780 PVUSBURB pUrb = NULL;
781 while (pDevSol->pTaxingHead)
782 {
783 RTCritSectEnter(&pDevSol->CritSect);
784
785 PUSBPROXYURBSOL pUrbSol = pDevSol->pTaxingHead;
786 if (pUrbSol)
787 {
788 pUrb = pUrbSol->pVUsbUrb;
789 if (pUrb)
790 {
791 pUrb->Dev.pvPrivate = NULL;
792 usbProxySolarisUrbFree(pDevSol, pUrbSol);
793 }
794 }
795 RTCritSectLeave(&pDevSol->CritSect);
796 }
797
798 return pUrb;
799}
800
801
802/**
803 * Reads a completed/error'd URB from the client driver (no waiting).
804 *
805 * @param pDevSol The Solaris device instance.
806 */
807PVUSBURB usbProxySolarisUrbComplete(PUSBPROXYDEVSOL pDevSol)
808{
809 LogFlowFunc((USBPROXY ":usbProxySolarisUrbComplete pDevSol=%p\n", pDevSol));
810
811 VBOXUSBREQ_URB UrbReq;
812 bzero(&UrbReq, sizeof(UrbReq));
813
814 int rc = usbProxySolarisIOCtl(pDevSol, VBOXUSB_IOCTL_REAP_URB, &UrbReq, sizeof(UrbReq));
815 if (RT_SUCCESS(rc))
816 {
817 if (UrbReq.pvUrbR3)
818 {
819 PUSBPROXYURBSOL pUrbSol = (PUSBPROXYURBSOL)UrbReq.pvUrbR3;
820 PVUSBURB pUrb = pUrbSol->pVUsbUrb;
821 if (RT_LIKELY(pUrb))
822 {
823 Assert(pUrb->u32Magic == VUSBURB_MAGIC);
824
825 /*
826 * Update the URB.
827 */
828 if ( pUrb->enmType == VUSBXFERTYPE_ISOC
829 && pUrb->enmDir == VUSBDIRECTION_IN)
830 {
831 size_t cbData = 0;
832 for (unsigned i = 0; i < UrbReq.cIsocPkts; i++)
833 {
834 pUrb->aIsocPkts[i].cb = UrbReq.aIsocPkts[i].cbActPkt;
835 cbData += UrbReq.aIsocPkts[i].cbActPkt;
836 pUrb->aIsocPkts[i].enmStatus = UrbReq.aIsocPkts[i].enmStatus;
837 }
838
839 LogFlow((USBPROXY ":usbProxySolarisUrbComplete ISOC cbData=%d cbActPktSum=%d\n", pUrb->cbData, cbData));
840 pUrb->cbData = cbData;
841 pUrb->enmStatus = UrbReq.enmStatus;
842 }
843 else
844 {
845 pUrb->cbData = UrbReq.cbData;
846 pUrb->enmStatus = UrbReq.enmStatus;
847 }
848
849 RTCritSectEnter(&pDevSol->CritSect);
850
851 /*
852 * Remove from the active list.
853 */
854 if (pUrbSol->pNext)
855 pUrbSol->pNext->pPrev = pUrbSol->pPrev;
856 if (pUrbSol->pPrev)
857 pUrbSol->pPrev->pNext = pUrbSol->pNext;
858 else
859 {
860 Assert(pDevSol->pInFlightHead == pUrbSol);
861 pDevSol->pInFlightHead = pUrbSol->pNext;
862 }
863
864 /*
865 * Link it into the taxing list.
866 */
867 pUrbSol->pNext = NULL;
868 pUrbSol->pPrev = pDevSol->pTaxingTail;
869 if (pDevSol->pTaxingTail)
870 pDevSol->pTaxingTail->pNext = pUrbSol;
871 else
872 pDevSol->pTaxingHead = pUrbSol;
873 pDevSol->pTaxingTail = pUrbSol;
874
875 RTCritSectLeave(&pDevSol->CritSect);
876
877 LogFlow((USBPROXY "usbProxySolarisUrbComplete: cb=%d EndPt=%#x enmDir=%d enmStatus=%s (%d) \n",
878 pUrb->cbData, pUrb->EndPt, pUrb->enmDir, pUrb->enmStatus == VUSBSTATUS_OK ? "OK" : "** Failed **", pUrb->enmStatus));
879// if (pUrb->cbData < 2049)
880// LogFlow((USBPROXY "%.*Rhxd\n", pUrb->cbData, pUrb->abData));
881 return pUrb;
882 }
883 }
884 }
885 else
886 {
887 if (rc != VERR_VUSB_DEVICE_NOT_ATTACHED)
888 LogRel((USBPROXY ":Reaping URB failed. rc=%Rrc\n", rc));
889 }
890
891 return NULL;
892}
893
894
895static DECLCALLBACK(int) usbProxySolarisWakeup(PUSBPROXYDEV pProxyDev)
896{
897 PUSBPROXYDEVSOL pDevSol = (PUSBPROXYDEVSOL)pProxyDev->Backend.pv;
898 size_t cbIgnored;
899
900 LogFlowFunc(("pProxyDev=%p\n", pProxyDev));
901
902 return RTPipeWrite(pDevSol->hPipeWakeupW, "", 1, &cbIgnored);
903}
904
905
906/**
907 * The Solaris USB Proxy Backend.
908 */
909extern const USBPROXYBACK g_USBProxyDeviceHost =
910{
911 "host",
912 usbProxySolarisOpen,
913 NULL,
914 usbProxySolarisClose,
915 usbProxySolarisReset,
916 usbProxySolarisSetConfig,
917 usbProxySolarisClaimInterface,
918 usbProxySolarisReleaseInterface,
919 usbProxySolarisSetInterface,
920 usbProxySolarisClearHaltedEp,
921 usbProxySolarisUrbQueue,
922 usbProxySolarisUrbCancel,
923 usbProxySolarisUrbReap,
924 usbProxySolarisWakeup,
925 0
926};
927
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