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