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