1 | /* $Id: USBProxyServiceDarwin.cpp 57990 2015-10-01 17:53:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox USB Proxy Service (in VBoxSVC), Darwin Specialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2012 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 "USBProxyService.h"
|
---|
23 | #include "Logging.h"
|
---|
24 | #include "iokit.h"
|
---|
25 |
|
---|
26 | #include <VBox/usb.h>
|
---|
27 | #include <VBox/usblib.h>
|
---|
28 | #include <VBox/err.h>
|
---|
29 |
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include <iprt/alloc.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/file.h>
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/asm.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Initialize data members.
|
---|
40 | */
|
---|
41 | USBProxyServiceDarwin::USBProxyServiceDarwin(Host *aHost)
|
---|
42 | : USBProxyService(aHost), mServiceRunLoopRef(NULL), mNotifyOpaque(NULL), mWaitABitNextTime(false), mUSBLibInitialized(false)
|
---|
43 | {
|
---|
44 | LogFlowThisFunc(("aHost=%p\n", aHost));
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Initializes the object (called right after construction).
|
---|
50 | *
|
---|
51 | * @returns S_OK on success and non-fatal failures, some COM error otherwise.
|
---|
52 | */
|
---|
53 | HRESULT USBProxyServiceDarwin::init(void)
|
---|
54 | {
|
---|
55 | /*
|
---|
56 | * Initialize the USB library.
|
---|
57 | */
|
---|
58 | int rc = USBLibInit();
|
---|
59 | if (RT_FAILURE(rc))
|
---|
60 | {
|
---|
61 | mLastError = rc;
|
---|
62 | return S_OK;
|
---|
63 | }
|
---|
64 | mUSBLibInitialized = true;
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Start the poller thread.
|
---|
68 | */
|
---|
69 | start();
|
---|
70 | return S_OK;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Stop all service threads and free the device chain.
|
---|
76 | */
|
---|
77 | USBProxyServiceDarwin::~USBProxyServiceDarwin()
|
---|
78 | {
|
---|
79 | LogFlowThisFunc(("\n"));
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Stop the service.
|
---|
83 | */
|
---|
84 | if (isActive())
|
---|
85 | stop();
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Terminate the USB library - it'll
|
---|
89 | */
|
---|
90 | if (mUSBLibInitialized)
|
---|
91 | {
|
---|
92 | USBLibTerm();
|
---|
93 | mUSBLibInitialized = false;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | void *USBProxyServiceDarwin::insertFilter(PCUSBFILTER aFilter)
|
---|
99 | {
|
---|
100 | return USBLibAddFilter(aFilter);
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | void USBProxyServiceDarwin::removeFilter(void *aId)
|
---|
105 | {
|
---|
106 | USBLibRemoveFilter(aId);
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | int USBProxyServiceDarwin::captureDevice(HostUSBDevice *aDevice)
|
---|
111 | {
|
---|
112 | /*
|
---|
113 | * Check preconditions.
|
---|
114 | */
|
---|
115 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
116 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
117 |
|
---|
118 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
119 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
120 |
|
---|
121 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_Capturing);
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Create a one-shot capture filter for the device (don't
|
---|
125 | * match on port) and trigger a re-enumeration of it.
|
---|
126 | */
|
---|
127 | USBFILTER Filter;
|
---|
128 | USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_CAPTURE);
|
---|
129 | initFilterFromDevice(&Filter, aDevice);
|
---|
130 |
|
---|
131 | void *pvId = USBLibAddFilter(&Filter);
|
---|
132 | if (!pvId)
|
---|
133 | return VERR_GENERAL_FAILURE;
|
---|
134 |
|
---|
135 | int rc = DarwinReEnumerateUSBDevice(aDevice->mUsb);
|
---|
136 | if (RT_SUCCESS(rc))
|
---|
137 | aDevice->mOneShotId = pvId;
|
---|
138 | else
|
---|
139 | {
|
---|
140 | USBLibRemoveFilter(pvId);
|
---|
141 | pvId = NULL;
|
---|
142 | }
|
---|
143 | LogFlowThisFunc(("returns %Rrc pvId=%p\n", rc, pvId));
|
---|
144 | return rc;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | void USBProxyServiceDarwin::captureDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
|
---|
149 | {
|
---|
150 | AssertReturnVoid(aDevice->isWriteLockOnCurrentThread());
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Remove the one-shot filter if necessary.
|
---|
154 | */
|
---|
155 | LogFlowThisFunc(("aDevice=%s aSuccess=%RTbool mOneShotId=%p\n", aDevice->i_getName().c_str(), aSuccess, aDevice->mOneShotId));
|
---|
156 | if (!aSuccess && aDevice->mOneShotId)
|
---|
157 | USBLibRemoveFilter(aDevice->mOneShotId);
|
---|
158 | aDevice->mOneShotId = NULL;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | int USBProxyServiceDarwin::releaseDevice(HostUSBDevice *aDevice)
|
---|
163 | {
|
---|
164 | /*
|
---|
165 | * Check preconditions.
|
---|
166 | */
|
---|
167 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
168 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
169 |
|
---|
170 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
171 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
172 |
|
---|
173 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_ReleasingToHost);
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Create a one-shot ignore filter for the device
|
---|
177 | * and trigger a re-enumeration of it.
|
---|
178 | */
|
---|
179 | USBFILTER Filter;
|
---|
180 | USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_IGNORE);
|
---|
181 | initFilterFromDevice(&Filter, aDevice);
|
---|
182 | Log(("USBFILTERIDX_PORT=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_PORT)));
|
---|
183 | Log(("USBFILTERIDX_BUS=%#x\n", USBFilterGetNum(&Filter, USBFILTERIDX_BUS)));
|
---|
184 |
|
---|
185 | void *pvId = USBLibAddFilter(&Filter);
|
---|
186 | if (!pvId)
|
---|
187 | return VERR_GENERAL_FAILURE;
|
---|
188 |
|
---|
189 | int rc = DarwinReEnumerateUSBDevice(aDevice->mUsb);
|
---|
190 | if (RT_SUCCESS(rc))
|
---|
191 | aDevice->mOneShotId = pvId;
|
---|
192 | else
|
---|
193 | {
|
---|
194 | USBLibRemoveFilter(pvId);
|
---|
195 | pvId = NULL;
|
---|
196 | }
|
---|
197 | LogFlowThisFunc(("returns %Rrc pvId=%p\n", rc, pvId));
|
---|
198 | return rc;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | void USBProxyServiceDarwin::releaseDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
|
---|
203 | {
|
---|
204 | AssertReturnVoid(aDevice->isWriteLockOnCurrentThread());
|
---|
205 |
|
---|
206 | /*
|
---|
207 | * Remove the one-shot filter if necessary.
|
---|
208 | */
|
---|
209 | LogFlowThisFunc(("aDevice=%s aSuccess=%RTbool mOneShotId=%p\n", aDevice->i_getName().c_str(), aSuccess, aDevice->mOneShotId));
|
---|
210 | if (!aSuccess && aDevice->mOneShotId)
|
---|
211 | USBLibRemoveFilter(aDevice->mOneShotId);
|
---|
212 | aDevice->mOneShotId = NULL;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /** @todo unused */
|
---|
217 | void USBProxyServiceDarwin::detachingDevice(HostUSBDevice *aDevice)
|
---|
218 | {
|
---|
219 | NOREF(aDevice);
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | bool USBProxyServiceDarwin::updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine)
|
---|
224 | {
|
---|
225 | AssertReturn(aDevice, false);
|
---|
226 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), false);
|
---|
227 | /* Nothing special here so far, so fall back on parent. */
|
---|
228 | return USBProxyService::updateDeviceState(aDevice, aUSBDevice, aRunFilters, aIgnoreMachine);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | int USBProxyServiceDarwin::wait(RTMSINTERVAL aMillies)
|
---|
233 | {
|
---|
234 | SInt32 rc = CFRunLoopRunInMode(CFSTR(VBOX_IOKIT_MODE_STRING),
|
---|
235 | mWaitABitNextTime && aMillies >= 1000
|
---|
236 | ? 1.0 /* seconds */
|
---|
237 | : aMillies >= 5000 /* Temporary measure to poll for status changes (MSD). */
|
---|
238 | ? 5.0 /* seconds */
|
---|
239 | : aMillies / 1000.0,
|
---|
240 | true);
|
---|
241 | mWaitABitNextTime = rc != kCFRunLoopRunTimedOut;
|
---|
242 |
|
---|
243 | return VINF_SUCCESS;
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | int USBProxyServiceDarwin::interruptWait(void)
|
---|
248 | {
|
---|
249 | if (mServiceRunLoopRef)
|
---|
250 | CFRunLoopStop(mServiceRunLoopRef);
|
---|
251 | return 0;
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | PUSBDEVICE USBProxyServiceDarwin::getDevices(void)
|
---|
256 | {
|
---|
257 | /* call iokit.cpp */
|
---|
258 | return DarwinGetUSBDevices();
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | void USBProxyServiceDarwin::serviceThreadInit(void)
|
---|
263 | {
|
---|
264 | mServiceRunLoopRef = CFRunLoopGetCurrent();
|
---|
265 | mNotifyOpaque = DarwinSubscribeUSBNotifications();
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | void USBProxyServiceDarwin::serviceThreadTerm(void)
|
---|
270 | {
|
---|
271 | DarwinUnsubscribeUSBNotifications(mNotifyOpaque);
|
---|
272 | mServiceRunLoopRef = NULL;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Wrapper called from iokit.cpp.
|
---|
278 | *
|
---|
279 | * @param pCur The USB device to free.
|
---|
280 | */
|
---|
281 | void DarwinFreeUSBDeviceFromIOKit(PUSBDEVICE pCur)
|
---|
282 | {
|
---|
283 | USBProxyService::freeDevice(pCur);
|
---|
284 | }
|
---|
285 |
|
---|