1 | /* $Id: UsbTestServiceGadgetHost.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * UsbTestServ - Remote USB test configuration and execution server, USB gadget host API.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016-2017 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 |
|
---|
32 | #include <iprt/asm.h>
|
---|
33 | #include <iprt/cdefs.h>
|
---|
34 | #include <iprt/ctype.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/types.h>
|
---|
38 |
|
---|
39 | #include "UsbTestServiceGadget.h"
|
---|
40 | #include "UsbTestServiceGadgetHostInternal.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Constants And Macros, Structures and Typedefs *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Internal UTS gadget host instance data.
|
---|
49 | */
|
---|
50 | typedef struct UTSGADGETHOSTINT
|
---|
51 | {
|
---|
52 | /** Reference counter. */
|
---|
53 | volatile uint32_t cRefs;
|
---|
54 | /** Pointer to the gadget host callback table. */
|
---|
55 | PCUTSGADGETHOSTIF pHstIf;
|
---|
56 | /** Interface specific instance data - variable in size. */
|
---|
57 | uint8_t abIfInst[1];
|
---|
58 | } UTSGADGETHOSTINT;
|
---|
59 | /** Pointer to the internal gadget host instance data. */
|
---|
60 | typedef UTSGADGETHOSTINT *PUTSGADGETHOSTINT;
|
---|
61 |
|
---|
62 |
|
---|
63 | /*********************************************************************************************************************************
|
---|
64 | * Global variables *
|
---|
65 | *********************************************************************************************************************************/
|
---|
66 |
|
---|
67 | /** Known gadget host interfaces. */
|
---|
68 | static const PCUTSGADGETHOSTIF g_apUtsGadgetHostIf[] =
|
---|
69 | {
|
---|
70 | &g_UtsGadgetHostIfUsbIp,
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | /*********************************************************************************************************************************
|
---|
75 | * Internal Functions *
|
---|
76 | *********************************************************************************************************************************/
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Destroys a gadget host instance.
|
---|
81 | *
|
---|
82 | * @returns nothing.
|
---|
83 | * @param pThis The gadget host instance.
|
---|
84 | */
|
---|
85 | static void utsGadgetHostDestroy(PUTSGADGETHOSTINT pThis)
|
---|
86 | {
|
---|
87 | /** @todo Remove all gadgets. */
|
---|
88 | pThis->pHstIf->pfnTerm((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0]);
|
---|
89 | RTMemFree(pThis);
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | DECLHIDDEN(int) utsGadgetHostCreate(UTSGADGETHOSTTYPE enmType, PCUTSGADGETCFGITEM paCfg,
|
---|
94 | PUTSGADGETHOST phGadgetHost)
|
---|
95 | {
|
---|
96 | int rc = VINF_SUCCESS;
|
---|
97 | PCUTSGADGETHOSTIF pIf = NULL;
|
---|
98 |
|
---|
99 | /* Get the interface. */
|
---|
100 | for (unsigned i = 0; i < RT_ELEMENTS(g_apUtsGadgetHostIf); i++)
|
---|
101 | {
|
---|
102 | if (g_apUtsGadgetHostIf[i]->enmType == enmType)
|
---|
103 | {
|
---|
104 | pIf = g_apUtsGadgetHostIf[i];
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (RT_LIKELY(pIf))
|
---|
110 | {
|
---|
111 | PUTSGADGETHOSTINT pThis = (PUTSGADGETHOSTINT)RTMemAllocZ(RT_OFFSETOF(UTSGADGETHOSTINT, abIfInst[pIf->cbIf]));
|
---|
112 | if (RT_LIKELY(pThis))
|
---|
113 | {
|
---|
114 | pThis->cRefs = 1;
|
---|
115 | pThis->pHstIf = pIf;
|
---|
116 | rc = pIf->pfnInit((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0], paCfg);
|
---|
117 | if (RT_SUCCESS(rc))
|
---|
118 | *phGadgetHost = pThis;
|
---|
119 | else
|
---|
120 | RTMemFree(pThis);
|
---|
121 | }
|
---|
122 | else
|
---|
123 | rc = VERR_NO_MEMORY;
|
---|
124 | }
|
---|
125 | else
|
---|
126 | rc = VERR_INVALID_PARAMETER;
|
---|
127 |
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | DECLHIDDEN(uint32_t) utsGadgetHostRetain(UTSGADGETHOST hGadgetHost)
|
---|
133 | {
|
---|
134 | PUTSGADGETHOSTINT pThis = hGadgetHost;
|
---|
135 |
|
---|
136 | AssertPtrReturn(pThis, 0);
|
---|
137 |
|
---|
138 | return ASMAtomicIncU32(&pThis->cRefs);
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | DECLHIDDEN(uint32_t) utsGadgetHostRelease(UTSGADGETHOST hGadgetHost)
|
---|
143 | {
|
---|
144 | PUTSGADGETHOSTINT pThis = hGadgetHost;
|
---|
145 |
|
---|
146 | AssertPtrReturn(pThis, 0);
|
---|
147 |
|
---|
148 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
149 | if (!cRefs)
|
---|
150 | utsGadgetHostDestroy(pThis);
|
---|
151 |
|
---|
152 | return cRefs;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | DECLHIDDEN(int) utsGadgetHostGadgetConnect(UTSGADGETHOST hGadgetHost, UTSGADGET hGadget)
|
---|
157 | {
|
---|
158 | PUTSGADGETHOSTINT pThis = hGadgetHost;
|
---|
159 |
|
---|
160 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
161 | return pThis->pHstIf->pfnGadgetConnect((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0], hGadget);
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | DECLHIDDEN(int) utsGadgetHostGadgetDisconnect(UTSGADGETHOST hGadgetHost, UTSGADGET hGadget)
|
---|
166 | {
|
---|
167 | PUTSGADGETHOSTINT pThis = hGadgetHost;
|
---|
168 |
|
---|
169 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
170 | return pThis->pHstIf->pfnGadgetDisconnect((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0], hGadget);
|
---|
171 | }
|
---|
172 |
|
---|