1 | /* $Id: USBProxyBackendDarwin.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox USB Proxy Service (in VBoxSVC), Darwin Specialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2024 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 | #include "iokit.h"
|
---|
36 |
|
---|
37 | #include <VBox/usb.h>
|
---|
38 | #include <VBox/usblib.h>
|
---|
39 | #include <iprt/errcore.h>
|
---|
40 |
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <iprt/alloc.h>
|
---|
43 | #include <iprt/assert.h>
|
---|
44 | #include <iprt/file.h>
|
---|
45 | #include <iprt/errcore.h>
|
---|
46 | #include <iprt/asm.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Initialize data members.
|
---|
51 | */
|
---|
52 | USBProxyBackendDarwin::USBProxyBackendDarwin()
|
---|
53 | : USBProxyBackend(), mServiceRunLoopRef(NULL), mNotifyOpaque(NULL), mWaitABitNextTime(false)
|
---|
54 | {
|
---|
55 | }
|
---|
56 |
|
---|
57 | USBProxyBackendDarwin::~USBProxyBackendDarwin()
|
---|
58 | {
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Initializes the object (called right after construction).
|
---|
63 | *
|
---|
64 | * @returns VBox status code.
|
---|
65 | */
|
---|
66 | int USBProxyBackendDarwin::init(USBProxyService *pUsbProxyService, const com::Utf8Str &strId,
|
---|
67 | const com::Utf8Str &strAddress, bool fLoadingSettings)
|
---|
68 | {
|
---|
69 | USBProxyBackend::init(pUsbProxyService, strId, strAddress, fLoadingSettings);
|
---|
70 |
|
---|
71 | unconst(m_strBackend) = Utf8Str("host");
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Start the poller thread.
|
---|
75 | */
|
---|
76 | start();
|
---|
77 | return VINF_SUCCESS;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Stop all service threads and free the device chain.
|
---|
83 | */
|
---|
84 | void USBProxyBackendDarwin::uninit()
|
---|
85 | {
|
---|
86 | LogFlowThisFunc(("\n"));
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Stop the service.
|
---|
90 | */
|
---|
91 | if (isActive())
|
---|
92 | stop();
|
---|
93 |
|
---|
94 | USBProxyBackend::uninit();
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | int USBProxyBackendDarwin::captureDevice(HostUSBDevice *aDevice)
|
---|
99 | {
|
---|
100 | /*
|
---|
101 | * Check preconditions.
|
---|
102 | */
|
---|
103 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
104 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
105 |
|
---|
106 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
107 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
108 |
|
---|
109 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_Capturing);
|
---|
110 |
|
---|
111 | devLock.release();
|
---|
112 | interruptWait();
|
---|
113 | return VINF_SUCCESS;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | int USBProxyBackendDarwin::releaseDevice(HostUSBDevice *aDevice)
|
---|
118 | {
|
---|
119 | /*
|
---|
120 | * Check preconditions.
|
---|
121 | */
|
---|
122 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
123 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
124 |
|
---|
125 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
126 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
127 |
|
---|
128 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_ReleasingToHost);
|
---|
129 |
|
---|
130 | devLock.release();
|
---|
131 | interruptWait();
|
---|
132 | return VINF_SUCCESS;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | bool USBProxyBackendDarwin::isFakeUpdateRequired()
|
---|
137 | {
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | int USBProxyBackendDarwin::wait(RTMSINTERVAL aMillies)
|
---|
143 | {
|
---|
144 | SInt32 rc = CFRunLoopRunInMode(CFSTR(VBOX_IOKIT_MODE_STRING),
|
---|
145 | mWaitABitNextTime && aMillies >= 1000
|
---|
146 | ? 1.0 /* seconds */
|
---|
147 | : aMillies >= 5000 /* Temporary measure to poll for status changes (MSD). */
|
---|
148 | ? 5.0 /* seconds */
|
---|
149 | : aMillies / 1000.0,
|
---|
150 | true);
|
---|
151 | mWaitABitNextTime = rc != kCFRunLoopRunTimedOut;
|
---|
152 |
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | int USBProxyBackendDarwin::interruptWait(void)
|
---|
158 | {
|
---|
159 | if (mServiceRunLoopRef)
|
---|
160 | CFRunLoopStop(mServiceRunLoopRef);
|
---|
161 | return 0;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | PUSBDEVICE USBProxyBackendDarwin::getDevices(void)
|
---|
166 | {
|
---|
167 | /* call iokit.cpp */
|
---|
168 | return DarwinGetUSBDevices();
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | void USBProxyBackendDarwin::serviceThreadInit(void)
|
---|
173 | {
|
---|
174 | mServiceRunLoopRef = CFRunLoopGetCurrent();
|
---|
175 | mNotifyOpaque = DarwinSubscribeUSBNotifications();
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | void USBProxyBackendDarwin::serviceThreadTerm(void)
|
---|
180 | {
|
---|
181 | DarwinUnsubscribeUSBNotifications(mNotifyOpaque);
|
---|
182 | mServiceRunLoopRef = NULL;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Wrapper called from iokit.cpp.
|
---|
188 | *
|
---|
189 | * @param pCur The USB device to free.
|
---|
190 | */
|
---|
191 | void DarwinFreeUSBDeviceFromIOKit(PUSBDEVICE pCur)
|
---|
192 | {
|
---|
193 | USBProxyBackend::freeDevice(pCur);
|
---|
194 | }
|
---|
195 |
|
---|