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