VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/win/USBProxyBackendWindows.cpp@ 98351

Last change on this file since 98351 was 98292, checked in by vboxsync, 23 months ago

Main/src-server: rc -> hrc/vrc. Enabled scm rc checks. bugref:10223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/* $Id: USBProxyBackendWindows.cpp 98292 2023-01-25 01:14:53Z vboxsync $ */
2/** @file
3 * VirtualBox USB Proxy Service, Windows Specialization.
4 */
5
6/*
7 * Copyright (C) 2005-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 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_MAIN_USBPROXYBACKEND
33#include "USBProxyBackend.h"
34#include "LoggingNew.h"
35
36#include <VBox/usb.h>
37#include <iprt/errcore.h>
38
39#include <iprt/string.h>
40#include <iprt/alloc.h>
41#include <iprt/assert.h>
42#include <iprt/file.h>
43#include <iprt/errcore.h>
44
45#include <VBox/usblib.h>
46
47
48/**
49 * Initialize data members.
50 */
51USBProxyBackendWindows::USBProxyBackendWindows()
52 : USBProxyBackend(), mhEventInterrupt(INVALID_HANDLE_VALUE)
53{
54 LogFlowThisFunc(("\n"));
55}
56
57USBProxyBackendWindows::~USBProxyBackendWindows()
58{
59}
60
61/**
62 * Initializes the object (called right after construction).
63 *
64 * @returns S_OK on success and non-fatal failures, some COM error otherwise.
65 */
66int USBProxyBackendWindows::init(USBProxyService *aUsbProxyService, const com::Utf8Str &strId,
67 const com::Utf8Str &strAddress, bool fLoadingSettings)
68{
69 USBProxyBackend::init(aUsbProxyService, strId, strAddress, fLoadingSettings);
70
71 unconst(m_strBackend) = Utf8Str("host");
72
73 /*
74 * Create the semaphore (considered fatal).
75 */
76 mhEventInterrupt = CreateEvent(NULL, FALSE, FALSE, NULL);
77 AssertReturn(mhEventInterrupt != INVALID_HANDLE_VALUE, VERR_OUT_OF_RESOURCES);
78
79 /*
80 * Initialize the USB lib and stuff.
81 */
82 int vrc = USBLibInit();
83 if (RT_SUCCESS(vrc))
84 {
85 /*
86 * Start the poller thread.
87 */
88 vrc = start();
89 if (RT_SUCCESS(vrc))
90 {
91 LogFlowThisFunc(("returns successfully\n"));
92 return VINF_SUCCESS;
93 }
94
95 USBLibTerm();
96 }
97
98 CloseHandle(mhEventInterrupt);
99 mhEventInterrupt = INVALID_HANDLE_VALUE;
100
101 LogFlowThisFunc(("returns failure!!! (vrc=%Rrc)\n", vrc));
102 return vrc;
103}
104
105
106/**
107 * Stop all service threads and free the device chain.
108 */
109void USBProxyBackendWindows::uninit()
110{
111 LogFlowThisFunc(("\n"));
112
113 /*
114 * Stop the service.
115 */
116 if (isActive())
117 stop();
118
119 if (mhEventInterrupt != INVALID_HANDLE_VALUE)
120 CloseHandle(mhEventInterrupt);
121 mhEventInterrupt = INVALID_HANDLE_VALUE;
122
123 /*
124 * Terminate the library...
125 */
126 int vrc = USBLibTerm();
127 AssertRC(vrc);
128 USBProxyBackend::uninit();
129}
130
131
132void *USBProxyBackendWindows::insertFilter(PCUSBFILTER aFilter)
133{
134 AssertReturn(aFilter, NULL);
135
136 LogFlow(("USBProxyBackendWindows::insertFilter()\n"));
137
138 void *pvId = USBLibAddFilter(aFilter);
139
140 LogFlow(("USBProxyBackendWindows::insertFilter(): returning pvId=%p\n", pvId));
141
142 return pvId;
143}
144
145
146void USBProxyBackendWindows::removeFilter(void *aID)
147{
148 LogFlow(("USBProxyBackendWindows::removeFilter(): id=%p\n", aID));
149
150 AssertReturnVoid(aID);
151
152 USBLibRemoveFilter(aID);
153}
154
155
156int USBProxyBackendWindows::captureDevice(HostUSBDevice *aDevice)
157{
158 /*
159 * Check preconditions.
160 */
161 AssertReturn(aDevice, VERR_GENERAL_FAILURE);
162 AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
163
164 AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
165 LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
166
167 Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_Capturing);
168
169 /*
170 * Create a one-shot ignore filter for the device
171 * and trigger a re-enumeration of it.
172 */
173 USBFILTER Filter;
174 USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_CAPTURE);
175 initFilterFromDevice(&Filter, aDevice);
176 Log(("USBFILTERIDX_PORT=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_PORT)));
177 Log(("USBFILTERIDX_BUS=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_BUS)));
178
179 void *pvId = USBLibAddFilter(&Filter);
180 if (!pvId)
181 {
182 AssertMsgFailed(("Add one-shot Filter failed\n"));
183 return VERR_GENERAL_FAILURE;
184 }
185
186 int vrc = USBLibRunFilters();
187 if (!RT_SUCCESS(vrc))
188 {
189 AssertMsgFailed(("Run Filters failed\n"));
190 USBLibRemoveFilter(pvId);
191 return vrc;
192 }
193
194 return VINF_SUCCESS;
195}
196
197
198int USBProxyBackendWindows::releaseDevice(HostUSBDevice *aDevice)
199{
200 /*
201 * Check preconditions.
202 */
203 AssertReturn(aDevice, VERR_GENERAL_FAILURE);
204 AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
205
206 AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
207 LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
208
209 Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_ReleasingToHost);
210
211 /*
212 * Create a one-shot ignore filter for the device
213 * and trigger a re-enumeration of it.
214 */
215 USBFILTER Filter;
216 USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_IGNORE);
217 initFilterFromDevice(&Filter, aDevice);
218 Log(("USBFILTERIDX_PORT=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_PORT)));
219 Log(("USBFILTERIDX_BUS=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_BUS)));
220
221 void *pvId = USBLibAddFilter(&Filter);
222 if (!pvId)
223 {
224 AssertMsgFailed(("Add one-shot Filter failed\n"));
225 return VERR_GENERAL_FAILURE;
226 }
227
228 int vrc = USBLibRunFilters();
229 if (!RT_SUCCESS(vrc))
230 {
231 AssertMsgFailed(("Run Filters failed\n"));
232 USBLibRemoveFilter(pvId);
233 return vrc;
234 }
235
236
237 return VINF_SUCCESS;
238}
239
240
241/**
242 * Returns whether devices reported by this backend go through a de/re-attach
243 * and device re-enumeration cycle when they are captured or released.
244 */
245bool USBProxyBackendWindows::i_isDevReEnumerationRequired()
246{
247 return true;
248}
249
250
251int USBProxyBackendWindows::wait(unsigned aMillies)
252{
253 return USBLibWaitChange(aMillies);
254}
255
256
257int USBProxyBackendWindows::interruptWait(void)
258{
259 return USBLibInterruptWaitChange();
260}
261
262/**
263 * Gets a list of all devices the VM can grab
264 */
265PUSBDEVICE USBProxyBackendWindows::getDevices(void)
266{
267 PUSBDEVICE pDevices = NULL;
268 uint32_t cDevices = 0;
269
270 Log(("USBProxyBackendWindows::getDevices\n"));
271 USBLibGetDevices(&pDevices, &cDevices);
272 return pDevices;
273}
274
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette