1 | /* $Id: UsbTestServiceGadget.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * UsbTestServ - Remote USB test configuration and execution server, USB gadget host API.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016-2023 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 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/ctype.h>
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/mem.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 | #include "UsbTestServiceGadgetInternal.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Constants And Macros, Structures and Typedefs *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Internal UTS gadget host instance data.
|
---|
56 | */
|
---|
57 | typedef struct UTSGADGETINT
|
---|
58 | {
|
---|
59 | /** Reference counter. */
|
---|
60 | volatile uint32_t cRefs;
|
---|
61 | /** Pointer to the gadget class callback table. */
|
---|
62 | PCUTSGADGETCLASSIF pClassIf;
|
---|
63 | /** The gadget host handle. */
|
---|
64 | UTSGADGETHOST hGadgetHost;
|
---|
65 | /** Class specific instance data - variable in size. */
|
---|
66 | uint8_t abClassInst[1];
|
---|
67 | } UTSGADGETINT;
|
---|
68 | /** Pointer to the internal gadget host instance data. */
|
---|
69 | typedef UTSGADGETINT *PUTSGADGETINT;
|
---|
70 |
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Global variables *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 |
|
---|
76 | /** Known gadget host interfaces. */
|
---|
77 | static const PCUTSGADGETCLASSIF g_apUtsGadgetClass[] =
|
---|
78 | {
|
---|
79 | &g_UtsGadgetClassTest
|
---|
80 | };
|
---|
81 |
|
---|
82 |
|
---|
83 | /*********************************************************************************************************************************
|
---|
84 | * Internal Functions *
|
---|
85 | *********************************************************************************************************************************/
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Destroys a gadget instance.
|
---|
90 | *
|
---|
91 | * @param pThis The gadget instance.
|
---|
92 | */
|
---|
93 | static void utsGadgetDestroy(PUTSGADGETINT pThis)
|
---|
94 | {
|
---|
95 | pThis->pClassIf->pfnTerm((PUTSGADGETCLASSINT)&pThis->abClassInst[0]);
|
---|
96 | RTMemFree(pThis);
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | DECLHIDDEN(int) utsGadgetCreate(UTSGADGETHOST hGadgetHost, UTSGADGETCLASS enmClass,
|
---|
101 | PCUTSGADGETCFGITEM paCfg, PUTSGADET phGadget)
|
---|
102 | {
|
---|
103 | int rc = VINF_SUCCESS;
|
---|
104 | PCUTSGADGETCLASSIF pClassIf = NULL;
|
---|
105 |
|
---|
106 | /* Get the interface. */
|
---|
107 | for (unsigned i = 0; i < RT_ELEMENTS(g_apUtsGadgetClass); i++)
|
---|
108 | {
|
---|
109 | if (g_apUtsGadgetClass[i]->enmClass == enmClass)
|
---|
110 | {
|
---|
111 | pClassIf = g_apUtsGadgetClass[i];
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (RT_LIKELY(pClassIf))
|
---|
117 | {
|
---|
118 | PUTSGADGETINT pThis = (PUTSGADGETINT)RTMemAllocZ(RT_UOFFSETOF_DYN(UTSGADGETINT, abClassInst[pClassIf->cbClass]));
|
---|
119 | if (RT_LIKELY(pThis))
|
---|
120 | {
|
---|
121 | pThis->cRefs = 1;
|
---|
122 | pThis->hGadgetHost = hGadgetHost;
|
---|
123 | pThis->pClassIf = pClassIf;
|
---|
124 | rc = pClassIf->pfnInit((PUTSGADGETCLASSINT)&pThis->abClassInst[0], paCfg);
|
---|
125 | if (RT_SUCCESS(rc))
|
---|
126 | {
|
---|
127 | /* Connect the gadget to the host. */
|
---|
128 | rc = utsGadgetHostGadgetConnect(pThis->hGadgetHost, pThis);
|
---|
129 | if (RT_SUCCESS(rc))
|
---|
130 | *phGadget = pThis;
|
---|
131 | }
|
---|
132 | else
|
---|
133 | RTMemFree(pThis);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | rc = VERR_NO_MEMORY;
|
---|
137 | }
|
---|
138 | else
|
---|
139 | rc = VERR_INVALID_PARAMETER;
|
---|
140 |
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | DECLHIDDEN(uint32_t) utsGadgetRetain(UTSGADGET hGadget)
|
---|
146 | {
|
---|
147 | PUTSGADGETINT pThis = hGadget;
|
---|
148 |
|
---|
149 | AssertPtrReturn(pThis, 0);
|
---|
150 |
|
---|
151 | return ASMAtomicIncU32(&pThis->cRefs);
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | DECLHIDDEN(uint32_t) utsGadgetRelease(UTSGADGET hGadget)
|
---|
156 | {
|
---|
157 | PUTSGADGETINT pThis = hGadget;
|
---|
158 |
|
---|
159 | AssertPtrReturn(pThis, 0);
|
---|
160 |
|
---|
161 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
162 | if (!cRefs)
|
---|
163 | utsGadgetDestroy(pThis);
|
---|
164 |
|
---|
165 | return cRefs;
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | DECLHIDDEN(uint32_t) utsGadgetGetBusId(UTSGADGET hGadget)
|
---|
170 | {
|
---|
171 | PUTSGADGETINT pThis = hGadget;
|
---|
172 |
|
---|
173 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
174 | return pThis->pClassIf->pfnGetBusId((PUTSGADGETCLASSINT)&pThis->abClassInst[0]);
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | DECLHIDDEN(uint32_t) utsGadgetGetDevId(UTSGADGET hGadget)
|
---|
179 | {
|
---|
180 | PUTSGADGETINT pThis = hGadget;
|
---|
181 |
|
---|
182 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
183 | return 1; /** @todo Current assumption which is true on Linux with dummy_hcd. */
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | DECLHIDDEN(int) utsGadgetConnect(UTSGADGET hGadget)
|
---|
188 | {
|
---|
189 | PUTSGADGETINT pThis = hGadget;
|
---|
190 |
|
---|
191 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
192 | int rc = pThis->pClassIf->pfnConnect((PUTSGADGETCLASSINT)&pThis->abClassInst[0]);
|
---|
193 | if (RT_SUCCESS(rc))
|
---|
194 | rc = utsGadgetHostGadgetConnect(pThis->hGadgetHost, hGadget);
|
---|
195 |
|
---|
196 | return rc;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | DECLHIDDEN(int) utsGadgetDisconnect(UTSGADGET hGadget)
|
---|
201 | {
|
---|
202 | PUTSGADGETINT pThis = hGadget;
|
---|
203 |
|
---|
204 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
205 | int rc = utsGadgetHostGadgetDisconnect(pThis->hGadgetHost, hGadget);
|
---|
206 | if (RT_SUCCESS(rc))
|
---|
207 | rc = pThis->pClassIf->pfnDisconnect((PUTSGADGETCLASSINT)&pThis->abClassInst[0]);
|
---|
208 |
|
---|
209 | return rc;
|
---|
210 | }
|
---|
211 |
|
---|