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