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