1 | /* $Id: UsbTestServiceGadget.cpp 60417 2016-04-11 10:37:04Z 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.215389.xyz. 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 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 |
|
---|
23 | #include <iprt/asm.h>
|
---|
24 | #include <iprt/cdefs.h>
|
---|
25 | #include <iprt/ctype.h>
|
---|
26 | #include <iprt/mem.h>
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include <iprt/types.h>
|
---|
29 |
|
---|
30 | #include "UsbTestServiceGadgetInternal.h"
|
---|
31 |
|
---|
32 | /*********************************************************************************************************************************
|
---|
33 | * Constants And Macros, Structures and Typedefs *
|
---|
34 | *********************************************************************************************************************************/
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Internal UTS gadget host instance data.
|
---|
38 | */
|
---|
39 | typedef struct UTSGADGETINT
|
---|
40 | {
|
---|
41 | /** Reference counter. */
|
---|
42 | volatile uint32_t cRefs;
|
---|
43 | /** Pointer to the gadget class callback table. */
|
---|
44 | PCUTSGADGETCLASSIF pClassIf;
|
---|
45 | /** The gadget host handle. */
|
---|
46 | UTSGADGETHOST hGadgetHost;
|
---|
47 | /** Class specific instance data - variable in size. */
|
---|
48 | uint8_t abClassInst[1];
|
---|
49 | } UTSGADGETINT;
|
---|
50 | /** Pointer to the internal gadget host instance data. */
|
---|
51 | typedef UTSGADGETINT *PUTSGADGETINT;
|
---|
52 |
|
---|
53 |
|
---|
54 | /*********************************************************************************************************************************
|
---|
55 | * Global variables *
|
---|
56 | *********************************************************************************************************************************/
|
---|
57 |
|
---|
58 | /** Known gadget host interfaces. */
|
---|
59 | static const PCUTSGADGETCLASSIF g_apUtsGadgetClass[] =
|
---|
60 | {
|
---|
61 | &g_UtsGadgetClassTest
|
---|
62 | };
|
---|
63 |
|
---|
64 |
|
---|
65 | /*********************************************************************************************************************************
|
---|
66 | * Internal Functions *
|
---|
67 | *********************************************************************************************************************************/
|
---|
68 |
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Destroys a gadget instance.
|
---|
72 | *
|
---|
73 | * @returns nothing.
|
---|
74 | * @param pThis The gadget instance.
|
---|
75 | */
|
---|
76 | static void utsGadgetDestroy(PUTSGADGETINT pThis)
|
---|
77 | {
|
---|
78 | pThis->pClassIf->pfnTerm((PUTSGADGETCLASSINT)&pThis->abClassInst[0]);
|
---|
79 | RTMemFree(pThis);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | DECLHIDDEN(int) utsGadgetCreate(UTSGADGETHOST hGadgetHost, UTSGADGETCLASS enmClass,
|
---|
84 | PCUTSGADGETCFGITEM paCfg, PUTSGADET phGadget)
|
---|
85 | {
|
---|
86 | int rc = VINF_SUCCESS;
|
---|
87 | PCUTSGADGETCLASSIF pClassIf = NULL;
|
---|
88 |
|
---|
89 | /* Get the interface. */
|
---|
90 | for (unsigned i = 0; i < RT_ELEMENTS(g_apUtsGadgetClass); i++)
|
---|
91 | {
|
---|
92 | if (g_apUtsGadgetClass[i]->enmClass == enmClass)
|
---|
93 | {
|
---|
94 | pClassIf = g_apUtsGadgetClass[i];
|
---|
95 | break;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (RT_LIKELY(pClassIf))
|
---|
100 | {
|
---|
101 | PUTSGADGETINT pThis = (PUTSGADGETINT)RTMemAllocZ(RT_OFFSETOF(UTSGADGETINT, abClassInst[pClassIf->cbClass]));
|
---|
102 | if (RT_LIKELY(pThis))
|
---|
103 | {
|
---|
104 | pThis->cRefs = 1;
|
---|
105 | pThis->hGadgetHost = hGadgetHost;
|
---|
106 | pThis->pClassIf = pClassIf;
|
---|
107 | rc = pClassIf->pfnInit((PUTSGADGETCLASSINT)&pThis->abClassInst[0], paCfg);
|
---|
108 | if (RT_SUCCESS(rc))
|
---|
109 | *phGadget = pThis;
|
---|
110 | else
|
---|
111 | RTMemFree(pThis);
|
---|
112 | }
|
---|
113 | else
|
---|
114 | rc = VERR_NO_MEMORY;
|
---|
115 | }
|
---|
116 | else
|
---|
117 | rc = VERR_INVALID_PARAMETER;
|
---|
118 |
|
---|
119 | return rc;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | DECLHIDDEN(uint32_t) utsGadgetRetain(UTSGADGET hGadget)
|
---|
124 | {
|
---|
125 | PUTSGADGETINT pThis = hGadget;
|
---|
126 |
|
---|
127 | AssertPtrReturn(pThis, 0);
|
---|
128 |
|
---|
129 | return ASMAtomicIncU32(&pThis->cRefs);
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | DECLHIDDEN(uint32_t) utsGadgetRelease(UTSGADGET hGadget)
|
---|
134 | {
|
---|
135 | PUTSGADGETINT pThis = hGadget;
|
---|
136 |
|
---|
137 | AssertPtrReturn(pThis, 0);
|
---|
138 |
|
---|
139 | uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
|
---|
140 | if (!cRefs)
|
---|
141 | utsGadgetDestroy(pThis);
|
---|
142 |
|
---|
143 | return cRefs;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | DECLHIDDEN(int) utsGadgetConnect(UTSGADGET hGadget)
|
---|
148 | {
|
---|
149 | PUTSGADGETINT pThis = hGadget;
|
---|
150 |
|
---|
151 | AssertPtrReturn(pThis, 0);
|
---|
152 | return VERR_NOT_IMPLEMENTED;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | DECLHIDDEN(int) utsGadgetDisconnect(UTSGADGET hGadget)
|
---|
157 | {
|
---|
158 | PUTSGADGETINT pThis = hGadget;
|
---|
159 |
|
---|
160 | AssertPtrReturn(pThis, 0);
|
---|
161 | return VERR_NOT_IMPLEMENTED;
|
---|
162 | }
|
---|
163 |
|
---|