1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* $Id: AudioDriver.cpp $ */
/** @file
* VirtualBox audio base class for Main audio drivers.
*/
/*
* Copyright (C) 2018-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
#include "LoggingNew.h"
#include <VBox/log.h>
#include <VBox/vmm/cfgm.h>
#include <VBox/vmm/pdmaudioifs.h>
#include <VBox/vmm/pdmapi.h>
#include <VBox/vmm/pdmdrv.h>
#include <VBox/vmm/vmmr3vtable.h>
#include "AudioDriver.h"
#include "ConsoleImpl.h"
AudioDriver::AudioDriver(Console *pConsole)
: mpConsole(pConsole)
, mfAttached(false)
{
}
AudioDriver::~AudioDriver(void)
{
}
AudioDriver &AudioDriver::operator=(AudioDriver const &a_rThat) RT_NOEXCEPT
{
mpConsole = a_rThat.mpConsole;
mCfg = a_rThat.mCfg;
mfAttached = a_rThat.mfAttached;
return *this;
}
/**
* Initializes the audio driver with a certain (device) configuration.
*
* @returns VBox status code.
* @param pCfg Audio driver configuration to use.
*/
int AudioDriver::InitializeConfig(AudioDriverCfg *pCfg)
{
AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
/* Sanity. */
AssertReturn(pCfg->strDev.isNotEmpty(), VERR_INVALID_PARAMETER);
AssertReturn(pCfg->uLUN != UINT8_MAX, VERR_INVALID_PARAMETER);
AssertReturn(pCfg->strName.isNotEmpty(), VERR_INVALID_PARAMETER);
/* Apply configuration. */
mCfg = *pCfg;
return VINF_SUCCESS;
}
/**
* Attaches the driver via EMT, if configured.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle for talking to EMT.
* @param pVMM The VMM ring-3 vtable.
* @param pAutoLock The callers auto lock instance. Can be NULL if not
* locked.
*/
int AudioDriver::doAttachDriverViaEmt(PUVM pUVM, PCVMMR3VTABLE pVMM, util::AutoWriteLock *pAutoLock)
{
if (!isConfigured())
return VINF_SUCCESS;
PVMREQ pReq;
int vrc = pVMM->pfnVMR3ReqCallU(pUVM, VMCPUID_ANY, &pReq, 0 /* no wait! */, VMREQFLAGS_VBOX_STATUS,
(PFNRT)attachDriverOnEmt, 1, this);
if (vrc == VERR_TIMEOUT)
{
/* Release the lock before a blocking VMR3* call (EMT might wait for it, @bugref{7648})! */
if (pAutoLock)
pAutoLock->release();
vrc = pVMM->pfnVMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
if (pAutoLock)
pAutoLock->acquire();
}
AssertRC(vrc);
pVMM->pfnVMR3ReqFree(pReq);
return vrc;
}
/**
* Configures the audio driver (to CFGM) and attaches it to the audio chain.
* Does nothing if the audio driver already is attached.
*
* @returns VBox status code.
* @param pThis Audio driver to detach.
*/
/* static */
DECLCALLBACK(int) AudioDriver::attachDriverOnEmt(AudioDriver *pThis)
{
AssertPtrReturn(pThis, VERR_INVALID_POINTER);
Console::SafeVMPtrQuiet ptrVM(pThis->mpConsole);
Assert(ptrVM.isOk());
if (pThis->mfAttached) /* Already attached? Bail out. */
{
LogFunc(("%s: Already attached\n", pThis->mCfg.strName.c_str()));
return VINF_SUCCESS;
}
AudioDriverCfg *pCfg = &pThis->mCfg;
LogFunc(("strName=%s, strDevice=%s, uInst=%u, uLUN=%u\n",
pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN));
/* Detach the driver chain from the audio device first. */
int vrc = ptrVM.vtable()->pfnPDMR3DeviceDetach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN, 0 /* fFlags */);
if (RT_SUCCESS(vrc))
{
vrc = pThis->configure(pCfg->uLUN, true /* Attach */);
if (RT_SUCCESS(vrc))
vrc = ptrVM.vtable()->pfnPDMR3DriverAttach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN,
0 /* fFlags */, NULL /* ppBase */);
}
if (RT_SUCCESS(vrc))
{
pThis->mfAttached = true;
LogRel2(("%s: Driver attached (LUN #%u)\n", pCfg->strName.c_str(), pCfg->uLUN));
}
else
LogRel(("%s: Failed to attach audio driver, rc=%Rrc\n", pCfg->strName.c_str(), vrc));
LogFunc(("Returning %Rrc\n", vrc));
return vrc;
}
/**
* Detatches the driver via EMT, if configured.
*
* @returns VBox status code.
* @param pUVM The user mode VM handle for talking to EMT.
* @param pVMM The VMM ring-3 vtable.
* @param pAutoLock The callers auto lock instance. Can be NULL if not
* locked.
*/
int AudioDriver::doDetachDriverViaEmt(PUVM pUVM, PCVMMR3VTABLE pVMM, util::AutoWriteLock *pAutoLock)
{
if (!isConfigured())
return VINF_SUCCESS;
PVMREQ pReq;
int vrc = pVMM->pfnVMR3ReqCallU(pUVM, VMCPUID_ANY, &pReq, 0 /* no wait! */, VMREQFLAGS_VBOX_STATUS,
(PFNRT)detachDriverOnEmt, 1, this);
if (vrc == VERR_TIMEOUT)
{
/* Release the lock before a blocking VMR3* call (EMT might wait for it, @bugref{7648})! */
if (pAutoLock)
pAutoLock->release();
vrc = pVMM->pfnVMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
if (pAutoLock)
pAutoLock->acquire();
}
AssertRC(vrc);
pVMM->pfnVMR3ReqFree(pReq);
return vrc;
}
/**
* Detaches an already attached audio driver from the audio chain.
* Does nothing if the audio driver already is detached or not attached.
*
* @returns VBox status code.
* @param pThis Audio driver to detach.
*/
/* static */
DECLCALLBACK(int) AudioDriver::detachDriverOnEmt(AudioDriver *pThis)
{
AssertPtrReturn(pThis, VERR_INVALID_POINTER);
if (!pThis->mfAttached) /* Not attached? Bail out. */
{
LogFunc(("%s: Not attached\n", pThis->mCfg.strName.c_str()));
return VINF_SUCCESS;
}
Console::SafeVMPtrQuiet ptrVM(pThis->mpConsole);
Assert(ptrVM.isOk());
AudioDriverCfg *pCfg = &pThis->mCfg;
Assert(pCfg->uLUN != UINT8_MAX);
LogFunc(("strName=%s, strDevice=%s, uInst=%u, uLUN=%u\n",
pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN));
/* Destroy the entire driver chain for the specified LUN.
*
* Start with the "AUDIO" driver, as this driver serves as the audio connector between
* the device emulation and the select backend(s). */
int vrc = ptrVM.vtable()->pfnPDMR3DriverDetach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN,
"AUDIO", 0 /* iOccurrence */, 0 /* fFlags */);
if (RT_SUCCESS(vrc))
vrc = pThis->configure(pCfg->uLUN, false /* Detach */);/** @todo r=bird: Illogical and from what I can tell pointless! */
if (RT_SUCCESS(vrc))
{
pThis->mfAttached = false;
LogRel2(("%s: Driver detached\n", pCfg->strName.c_str()));
}
else
LogRel(("%s: Failed to detach audio driver, vrc=%Rrc\n", pCfg->strName.c_str(), vrc));
LogFunc(("Returning %Rrc\n", vrc));
return vrc;
}
/**
* Configures the audio driver via CFGM.
*
* @returns VBox status code.
* @param uLUN LUN to attach driver to.
* @param fAttach Whether to attach or detach the driver configuration to CFGM.
*
* @thread EMT
*/
int AudioDriver::configure(unsigned uLUN, bool fAttach)
{
Console::SafeVMPtrQuiet ptrVM(mpConsole);
AssertReturn(ptrVM.isOk(), VERR_INVALID_STATE);
PCFGMNODE pRoot = ptrVM.vtable()->pfnCFGMR3GetRootU(ptrVM.rawUVM());
AssertPtr(pRoot);
PCFGMNODE pDev0 = ptrVM.vtable()->pfnCFGMR3GetChildF(pRoot, "Devices/%s/%u/", mCfg.strDev.c_str(), mCfg.uInst);
if (!pDev0) /* No audio device configured? Bail out. */
{
LogRel2(("%s: No audio device configured, skipping to attach driver\n", mCfg.strName.c_str()));
return VINF_SUCCESS;
}
int vrc = VINF_SUCCESS;
PCFGMNODE pDevLun = ptrVM.vtable()->pfnCFGMR3GetChildF(pDev0, "LUN#%u/", uLUN);
if (fAttach)
{
do /* break "loop" */
{
AssertMsgBreakStmt(pDevLun, ("%s: Device LUN #%u not found\n", mCfg.strName.c_str(), uLUN), vrc = VERR_NOT_FOUND);
LogRel2(("%s: Configuring audio driver (to LUN #%u)\n", mCfg.strName.c_str(), uLUN));
ptrVM.vtable()->pfnCFGMR3RemoveNode(pDevLun); /* Remove LUN completely first. */
/* Insert new LUN configuration and build up the new driver chain. */
vrc = ptrVM.vtable()->pfnCFGMR3InsertNodeF(pDev0, &pDevLun, "LUN#%u/", uLUN); AssertRCBreak(vrc);
vrc = ptrVM.vtable()->pfnCFGMR3InsertString(pDevLun, "Driver", "AUDIO"); AssertRCBreak(vrc);
PCFGMNODE pLunCfg;
vrc = ptrVM.vtable()->pfnCFGMR3InsertNode(pDevLun, "Config", &pLunCfg); AssertRCBreak(vrc);
vrc = ptrVM.vtable()->pfnCFGMR3InsertStringF(pLunCfg, "DriverName", "%s", mCfg.strName.c_str()); AssertRCBreak(vrc);
vrc = ptrVM.vtable()->pfnCFGMR3InsertInteger(pLunCfg, "InputEnabled", mCfg.fEnabledIn); AssertRCBreak(vrc);
vrc = ptrVM.vtable()->pfnCFGMR3InsertInteger(pLunCfg, "OutputEnabled", mCfg.fEnabledOut); AssertRCBreak(vrc);
PCFGMNODE pAttachedDriver;
vrc = ptrVM.vtable()->pfnCFGMR3InsertNode(pDevLun, "AttachedDriver", &pAttachedDriver); AssertRCBreak(vrc);
vrc = ptrVM.vtable()->pfnCFGMR3InsertStringF(pAttachedDriver, "Driver", "%s", mCfg.strName.c_str()); AssertRCBreak(vrc);
PCFGMNODE pAttachedDriverCfg;
vrc = ptrVM.vtable()->pfnCFGMR3InsertNode(pAttachedDriver, "Config", &pAttachedDriverCfg); AssertRCBreak(vrc);
/* Call the (virtual) method for driver-specific configuration. */
vrc = configureDriver(pAttachedDriverCfg, ptrVM.vtable()); AssertRCBreak(vrc);
} while (0);
}
else /* Detach */
{
LogRel2(("%s: Unconfiguring audio driver\n", mCfg.strName.c_str()));
}
if (RT_SUCCESS(vrc))
{
#ifdef LOG_ENABLED
LogFunc(("%s: fAttach=%RTbool\n", mCfg.strName.c_str(), fAttach));
ptrVM.vtable()->pfnCFGMR3Dump(pDevLun);
#endif
}
else
LogRel(("%s: %s audio driver failed with vrc=%Rrc\n", mCfg.strName.c_str(), fAttach ? "Configuring" : "Unconfiguring", vrc));
LogFunc(("Returning %Rrc\n", vrc));
return vrc;
}
|