1 | /** @file
|
---|
2 | * VirtualBox USB Proxy Service (base) class.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * If you received this file as part of a commercial VirtualBox
|
---|
17 | * distribution, then only the terms of your commercial VirtualBox
|
---|
18 | * license agreement apply instead of the previous paragraph.
|
---|
19 | */
|
---|
20 |
|
---|
21 |
|
---|
22 | #ifndef ____H_USBPROXYSERVICE
|
---|
23 | #define ____H_USBPROXYSERVICE
|
---|
24 |
|
---|
25 | #include "HostImpl.h"
|
---|
26 | #include "HostUSBDeviceImpl.h"
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Base class for the USB Proxy service.
|
---|
30 | */
|
---|
31 | class USBProxyService
|
---|
32 | {
|
---|
33 | public:
|
---|
34 | USBProxyService (Host *aHost);
|
---|
35 | virtual ~USBProxyService();
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * A filter was inserted / loaded.
|
---|
39 | *
|
---|
40 | * @param aFilter Pointer to the inserted filter.
|
---|
41 | * @return ID of the inserted filter
|
---|
42 | */
|
---|
43 | virtual void *insertFilter (IUSBDeviceFilter *aFilter);
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * A filter was removed.
|
---|
47 | *
|
---|
48 | * @param aID ID of the filter to remove
|
---|
49 | */
|
---|
50 | virtual void removeFilter (void *aID);
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * A VM is trying to capture a device, do necessary preperations.
|
---|
54 | *
|
---|
55 | * @returns VBox status code.
|
---|
56 | * @param pDevice The device in question.
|
---|
57 | */
|
---|
58 | virtual int captureDevice (HostUSBDevice *pDevice);
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * The device is to be held so that the host OS will not start using it.
|
---|
62 | *
|
---|
63 | * @returns VBox status code.
|
---|
64 | * @param pDevice The device in question.
|
---|
65 | */
|
---|
66 | virtual int holdDevice (HostUSBDevice *pDevice);
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * A VM is releasing a device back to the host.
|
---|
70 | *
|
---|
71 | * @returns VBox status code.
|
---|
72 | * @param pDevice The device in question.
|
---|
73 | */
|
---|
74 | virtual int releaseDevice (HostUSBDevice *pDevice);
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * A VM is releaseing a device back to be held or assigned to another VM.
|
---|
78 | * A port reset should be performed.
|
---|
79 | *
|
---|
80 | * @returns VBox status code.
|
---|
81 | * @param pDevice The device in question.
|
---|
82 | */
|
---|
83 | virtual int resetDevice (HostUSBDevice *pDevice);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Query if the service is active and working.
|
---|
87 | *
|
---|
88 | * @returns true if the service is up running.
|
---|
89 | * @returns false if the service isn't running.
|
---|
90 | */
|
---|
91 | bool isActive (void);
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Get last error.
|
---|
95 | * Can be used to check why the proxy !isActive() upon construction.
|
---|
96 | *
|
---|
97 | * @returns VBox status code.
|
---|
98 | */
|
---|
99 | int getLastError (void);
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Calculate the hash of the serial string.
|
---|
103 | *
|
---|
104 | * 64bit FNV1a, chosen because it is designed to hash in to a power of two
|
---|
105 | * space, and is much quicker and simpler than, say, a half MD4.
|
---|
106 | *
|
---|
107 | * @returns the hash.
|
---|
108 | * @param aSerial The serial string.
|
---|
109 | */
|
---|
110 | static uint64_t calcSerialHash (const char *aSerial);
|
---|
111 |
|
---|
112 | protected:
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Starts the service.
|
---|
116 | *
|
---|
117 | * @returns VBox status.
|
---|
118 | */
|
---|
119 | int start (void);
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Stops the service.
|
---|
123 | *
|
---|
124 | * @returns VBox status.
|
---|
125 | */
|
---|
126 | int stop (void);
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Wait for a change in the USB devices attached to the host.
|
---|
130 | *
|
---|
131 | * @returns VBox status (ignored).
|
---|
132 | * @param aMillies Number of milliseconds to wait.
|
---|
133 | */
|
---|
134 | virtual int wait (unsigned aMillies);
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Interrupt any wait() call in progress.
|
---|
138 | *
|
---|
139 | * @returns VBox status.
|
---|
140 | */
|
---|
141 | virtual int interruptWait (void);
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Get a list of USB device currently attached to the host.
|
---|
145 | *
|
---|
146 | * @returns Pointer to a list of USB devices.
|
---|
147 | * The list nodes are freed individually by calling freeDevice().
|
---|
148 | */
|
---|
149 | virtual PUSBDEVICE getDevices (void);
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * First call made on the service thread, use it to do
|
---|
153 | * thread initialization.
|
---|
154 | */
|
---|
155 | virtual void serviceThreadInit (void);
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * First call made on the service thread, use it to do
|
---|
159 | * thread termination.
|
---|
160 | */
|
---|
161 | virtual void serviceThreadTerm (void);
|
---|
162 |
|
---|
163 | public:
|
---|
164 | /**
|
---|
165 | * Free one USB device returned by getDevice().
|
---|
166 | *
|
---|
167 | * @param pDevice Pointer to the device.
|
---|
168 | */
|
---|
169 | static void freeDevice (PUSBDEVICE pDevice);
|
---|
170 |
|
---|
171 | private:
|
---|
172 | /**
|
---|
173 | * Process any relevant changes in the attached USB devices.
|
---|
174 | */
|
---|
175 | void processChanges (void);
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * The service thread created by start().
|
---|
179 | *
|
---|
180 | * @param Thread The thread handle.
|
---|
181 | * @param pvUser Pointer to the USBProxyService instance.
|
---|
182 | */
|
---|
183 | static DECLCALLBACK (int) serviceThread (RTTHREAD Thread, void *pvUser);
|
---|
184 |
|
---|
185 | protected:
|
---|
186 | /** Pointer to the Host object. */
|
---|
187 | Host *mHost;
|
---|
188 | /** Thread handle of the service thread. */
|
---|
189 | RTTHREAD mThread;
|
---|
190 | /** Flag which stop() sets to cause serviceThread to return. */
|
---|
191 | bool volatile mTerminate;
|
---|
192 | /** List of smart HostUSBDevice pointers. */
|
---|
193 | typedef std::list <ComObjPtr <HostUSBDevice> > HostUSBDeviceList;
|
---|
194 | /** List of the known USB devices. */
|
---|
195 | HostUSBDeviceList mDevices;
|
---|
196 | /** VBox status code of the last failure.
|
---|
197 | * (Only used by start(), stop() and the child constructors.) */
|
---|
198 | int mLastError;
|
---|
199 | };
|
---|
200 |
|
---|
201 |
|
---|
202 | #ifdef VBOX_WITH_USB
|
---|
203 |
|
---|
204 | # ifdef __DARWIN__
|
---|
205 | # include <VBox/param.h>
|
---|
206 | # undef PAGE_SHIFT
|
---|
207 | # undef PAGE_SIZE
|
---|
208 | # define OSType Carbon_OSType
|
---|
209 | # include <Carbon/Carbon.h>
|
---|
210 | # undef OSType
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * The Darwin hosted USB Proxy Service.
|
---|
214 | */
|
---|
215 | class USBProxyServiceDarwin : public USBProxyService
|
---|
216 | {
|
---|
217 | public:
|
---|
218 | USBProxyServiceDarwin (Host *aHost);
|
---|
219 | ~USBProxyServiceDarwin();
|
---|
220 |
|
---|
221 | virtual int captureDevice (HostUSBDevice *aDevice);
|
---|
222 | virtual int holdDevice (HostUSBDevice *aDevice);
|
---|
223 | virtual int releaseDevice (HostUSBDevice *aDevice);
|
---|
224 | virtual int resetDevice (HostUSBDevice *aDevice);
|
---|
225 |
|
---|
226 | protected:
|
---|
227 | virtual int wait (unsigned aMillies);
|
---|
228 | virtual int interruptWait (void);
|
---|
229 | virtual PUSBDEVICE getDevices (void);
|
---|
230 | virtual void serviceThreadInit (void);
|
---|
231 | virtual void serviceThreadTerm (void);
|
---|
232 |
|
---|
233 | private:
|
---|
234 | /** Reference to the runloop of the service thread.
|
---|
235 | * This is NULL if the service thread isn't running. */
|
---|
236 | CFRunLoopRef mServiceRunLoopRef;
|
---|
237 | /** The opaque value returned by DarwinSubscribeUSBNotifications. */
|
---|
238 | void *mNotifyOpaque;
|
---|
239 | /** A hack to work around the problem with the usb device enumration
|
---|
240 | * not including newly attached devices. */
|
---|
241 | bool mWaitABitNextTime;
|
---|
242 | };
|
---|
243 | # endif /* __DARWIN__ */
|
---|
244 |
|
---|
245 |
|
---|
246 | # ifdef __LINUX__
|
---|
247 | # include <stdio.h>
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * The Linux hosted USB Proxy Service.
|
---|
251 | */
|
---|
252 | class USBProxyServiceLinux : public USBProxyService
|
---|
253 | {
|
---|
254 | public:
|
---|
255 | USBProxyServiceLinux (Host *aHost, const char *aUsbfsRoot = "/proc/bus/usb");
|
---|
256 | ~USBProxyServiceLinux();
|
---|
257 |
|
---|
258 | virtual int captureDevice (HostUSBDevice *aDevice);
|
---|
259 | virtual int holdDevice (HostUSBDevice *aDevice);
|
---|
260 | virtual int releaseDevice (HostUSBDevice *aDevice);
|
---|
261 | virtual int resetDevice (HostUSBDevice *aDevice);
|
---|
262 |
|
---|
263 | protected:
|
---|
264 | virtual int wait (unsigned aMillies);
|
---|
265 | virtual int interruptWait (void);
|
---|
266 | virtual PUSBDEVICE getDevices (void);
|
---|
267 |
|
---|
268 | private:
|
---|
269 | /** File handle to the '/proc/bus/usb/devices' file. */
|
---|
270 | RTFILE mFile;
|
---|
271 | /** Stream for mFile. */
|
---|
272 | FILE *mStream;
|
---|
273 | /** Pipe used to interrupt wait(), the read end. */
|
---|
274 | RTFILE mWakeupPipeR;
|
---|
275 | /** Pipe used to interrupt wait(), the write end. */
|
---|
276 | RTFILE mWakeupPipeW;
|
---|
277 | /** The root of usbfs. */
|
---|
278 | Utf8Str mUsbfsRoot;
|
---|
279 | };
|
---|
280 | # endif /* __LINUX__ */
|
---|
281 |
|
---|
282 |
|
---|
283 | # ifdef __WIN__
|
---|
284 | /**
|
---|
285 | * The Win32 hosted USB Proxy Service.
|
---|
286 | */
|
---|
287 | class USBProxyServiceWin32 : public USBProxyService
|
---|
288 | {
|
---|
289 | public:
|
---|
290 | USBProxyServiceWin32 (Host *aHost);
|
---|
291 | ~USBProxyServiceWin32();
|
---|
292 |
|
---|
293 | virtual void *insertFilter (IUSBDeviceFilter *aFilter);
|
---|
294 | virtual void removeFilter (void *aID);
|
---|
295 |
|
---|
296 | virtual int captureDevice (HostUSBDevice *aDevice);
|
---|
297 | virtual int holdDevice (HostUSBDevice *aDevice);
|
---|
298 | virtual int releaseDevice (HostUSBDevice *aDevice);
|
---|
299 | virtual int resetDevice (HostUSBDevice *aDevice);
|
---|
300 |
|
---|
301 | protected:
|
---|
302 | virtual int wait (unsigned aMillies);
|
---|
303 | virtual int interruptWait (void);
|
---|
304 | virtual PUSBDEVICE getDevices (void);
|
---|
305 |
|
---|
306 | private:
|
---|
307 |
|
---|
308 | HANDLE hEventInterrupt;
|
---|
309 | };
|
---|
310 | # endif /* __WIN__ */
|
---|
311 |
|
---|
312 | #endif /* VBOX_WITH_USB */
|
---|
313 |
|
---|
314 |
|
---|
315 | #endif /* !____H_USBPROXYSERVICE */
|
---|