1 | /* $Id: UsbTestServiceGadgetHost.cpp 61024 2016-05-18 07:45:51Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * UsbTestServ - Remote USB test configuration and execution server, USB gadget host API.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016 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 | * Constants And Macros, Structures and Typedefs *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Internal UTS gadget host instance data.
|
---|
48 | */
|
---|
49 | typedef struct UTSGADGETHOSTINT
|
---|
50 | {
|
---|
51 | /** Reference counter. */
|
---|
52 | volatile uint32_t cRefs;
|
---|
53 | /** Pointer to the gadget host callback table. */
|
---|
54 | PCUTSGADGETHOSTIF pHstIf;
|
---|
55 | /** Interface specific instance data - variable in size. */
|
---|
56 | uint8_t abIfInst[1];
|
---|
57 | } UTSGADGETHOSTINT;
|
---|
58 | /** Pointer to the internal gadget host instance data. */
|
---|
59 | typedef UTSGADGETHOSTINT *PUTSGADGETHOSTINT;
|
---|
60 |
|
---|
61 |
|
---|
62 | /*********************************************************************************************************************************
|
---|
63 | * Global variables *
|
---|
64 | *********************************************************************************************************************************/
|
---|
65 |
|
---|
66 | /** Known gadget host interfaces. */
|
---|
67 | static const PCUTSGADGETHOSTIF g_apUtsGadgetHostIf[] =
|
---|
68 | {
|
---|
69 | &g_UtsGadgetHostIfUsbIp,
|
---|
70 | };
|
---|
71 |
|
---|
72 | /*********************************************************************************************************************************
|
---|
73 | * Internal Functions *
|
---|
74 | *********************************************************************************************************************************/
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Destroys a gadget host instance.
|
---|
79 | *
|
---|
80 | * @returns nothing.
|
---|
81 | * @param pThis The gadget host instance.
|
---|
82 | */
|
---|
83 | static void utsGadgetHostDestroy(PUTSGADGETHOSTINT pThis)
|
---|
84 | {
|
---|
85 | /** @todo: Remove all gadgets. */
|
---|
86 | pThis->pHstIf->pfnTerm((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0]);
|
---|
87 | RTMemFree(pThis);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | DECLHIDDEN(int) utsGadgetHostCreate(UTSGADGETHOSTTYPE enmType, PCUTSGADGETCFGITEM paCfg,
|
---|
92 | PUTSGADGETHOST phGadgetHost)
|
---|
93 | {
|
---|
94 | int rc = VINF_SUCCESS;
|
---|
95 | PCUTSGADGETHOSTIF pIf = NULL;
|
---|
96 |
|
---|
97 | /* Get the interface. */
|
---|
98 | for (unsigned i = 0; i < RT_ELEMENTS(g_apUtsGadgetHostIf); i++)
|
---|
99 | {
|
---|
100 | if (g_apUtsGadgetHostIf[i]->enmType == enmType)
|
---|
101 | {
|
---|
102 | pIf = g_apUtsGadgetHostIf[i];
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (RT_LIKELY(pIf))
|
---|
108 | {
|
---|
109 | PUTSGADGETHOSTINT pThis = (PUTSGADGETHOSTINT)RTMemAllocZ(RT_OFFSETOF(UTSGADGETHOSTINT, abIfInst[pIf->cbIf]));
|
---|
110 | if (RT_LIKELY(pThis))
|
---|
111 | {
|
---|
112 | pThis->cRefs = 1;
|
---|
113 | pThis->pHstIf = pIf;
|
---|
114 | rc = pIf->pfnInit((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0], paCfg);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | *phGadgetHost = pThis;
|
---|
117 | else
|
---|
118 | RTMemFree(pThis);
|
---|
119 | }
|
---|
120 | else
|
---|
121 | rc = VERR_NO_MEMORY;
|
---|
122 | }
|
---|
123 | else
|
---|
124 | rc = VERR_INVALID_PARAMETER;
|
---|
125 |
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | DECLHIDDEN(uint32_t) utsGadgetHostRetain(UTSGADGETHOST hGadgetHost)
|
---|
131 | {
|
---|
132 | PUTSGADGETHOSTINT pThis = hGadgetHost;
|
---|
133 |
|
---|
134 | AssertPtrReturn(pThis, 0);
|
---|
135 |
|
---|
136 | return ASMAtomicIncU32(&pThis->cRefs);
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | DECLHIDDEN(uint32_t) utsGadgetHostRelease(UTSGADGETHOST hGadgetHost)
|
---|
141 | {
|
---|
142 | PUTSGADGETHOSTINT pThis = hGadgetHost;
|
---|
143 |
|
---|
144 | AssertPtrReturn(pThis, 0);
|
---|
145 |
|
---|
146 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
147 | if (!cRefs)
|
---|
148 | utsGadgetHostDestroy(pThis);
|
---|
149 |
|
---|
150 | return cRefs;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | DECLHIDDEN(int) utsGadgetHostGadgetConnect(UTSGADGETHOST hGadgetHost, UTSGADGET hGadget)
|
---|
155 | {
|
---|
156 | PUTSGADGETHOSTINT pThis = hGadgetHost;
|
---|
157 |
|
---|
158 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
159 | return pThis->pHstIf->pfnGadgetConnect((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0], hGadget);
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | DECLHIDDEN(int) utsGadgetHostGadgetDisconnect(UTSGADGETHOST hGadgetHost, UTSGADGET hGadget)
|
---|
164 | {
|
---|
165 | PUTSGADGETHOSTINT pThis = hGadgetHost;
|
---|
166 |
|
---|
167 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
168 | return pThis->pHstIf->pfnGadgetDisconnect((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0], hGadget);
|
---|
169 | }
|
---|
170 |
|
---|