1 | /* $Id: USBProxyDevice-darwin.cpp 57990 2015-10-01 17:53:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * USB device proxy - the Darwin backend.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.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_DRV_USBPROXY
|
---|
23 | #define __STDC_LIMIT_MACROS
|
---|
24 | #define __STDC_CONSTANT_MACROS
|
---|
25 |
|
---|
26 | #include <mach/mach.h>
|
---|
27 | #include <Carbon/Carbon.h>
|
---|
28 | #include <IOKit/IOKitLib.h>
|
---|
29 | #include <mach/mach_error.h>
|
---|
30 | #include <IOKit/usb/IOUSBLib.h>
|
---|
31 | #include <IOKit/IOCFPlugIn.h>
|
---|
32 |
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <VBox/err.h>
|
---|
35 | #include <VBox/vmm/pdm.h>
|
---|
36 |
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/critsect.h>
|
---|
39 | #include <iprt/list.h>
|
---|
40 | #include <iprt/mem.h>
|
---|
41 | #include <iprt/once.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <iprt/time.h>
|
---|
44 |
|
---|
45 | #include "../USBProxyDevice.h"
|
---|
46 | #include <VBox/usblib.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Defined Constants And Macros *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | /** An experiment... */
|
---|
53 | //#define USE_LOW_LATENCY_API 1
|
---|
54 |
|
---|
55 |
|
---|
56 | /*********************************************************************************************************************************
|
---|
57 | * Structures and Typedefs *
|
---|
58 | *********************************************************************************************************************************/
|
---|
59 | /** Forward declaration of the Darwin interface structure. */
|
---|
60 | typedef struct USBPROXYIFOSX *PUSBPROXYIFOSX;
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * A low latency isochronous buffer.
|
---|
65 | *
|
---|
66 | * These are allocated in chunks on an interface level, see USBPROXYISOCBUFCOL.
|
---|
67 | */
|
---|
68 | typedef struct USBPROXYISOCBUF
|
---|
69 | {
|
---|
70 | /** Whether this buffer is in use or not. */
|
---|
71 | bool volatile fUsed;
|
---|
72 | /** Pointer to the buffer. */
|
---|
73 | void *pvBuf;
|
---|
74 | /** Pointer to an array of 8 frames. */
|
---|
75 | IOUSBLowLatencyIsocFrame *paFrames;
|
---|
76 | } USBPROXYISOCBUF, *PUSBPROXYISOCBUF;
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Isochronous buffer collection (associated with an interface).
|
---|
81 | *
|
---|
82 | * These are allocated in decent sized chunks and there isn't supposed
|
---|
83 | * to be too many of these per interface.
|
---|
84 | */
|
---|
85 | typedef struct USBPROXYISOCBUFCOL
|
---|
86 | {
|
---|
87 | /** Write or Read buffers? */
|
---|
88 | USBLowLatencyBufferType enmType;
|
---|
89 | /** The next buffer collection on this interface. */
|
---|
90 | struct USBPROXYISOCBUFCOL *pNext;
|
---|
91 | /** The buffer. */
|
---|
92 | void *pvBuffer;
|
---|
93 | /** The frame. */
|
---|
94 | void *pvFrames;
|
---|
95 | /** The buffers.
|
---|
96 | * The number of buffers here is decided by pvFrame begin allocated in
|
---|
97 | * PAGE_SIZE chunks. The size of IOUSBLowLatencyIsocFrame is 16 bytes
|
---|
98 | * and we require 8 of those per buffer. PAGE_SIZE / (16 * 8) = 32.
|
---|
99 | * @remarks Don't allocate too many as it may temporarily halt the system if
|
---|
100 | * some pool is low / exhausted. (Contiguous memory woes on mach.)
|
---|
101 | */
|
---|
102 | USBPROXYISOCBUF aBuffers[/*32*/ 4];
|
---|
103 | } USBPROXYISOCBUFCOL, *PUSBPROXYISOCBUFCOL;
|
---|
104 |
|
---|
105 | AssertCompileSize(IOUSBLowLatencyIsocFrame, 16);
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Per-urb data for the Darwin usb proxy backend.
|
---|
109 | *
|
---|
110 | * This is required to track in-flight and landed URBs
|
---|
111 | * since we take down the URBs in a different thread (perhaps).
|
---|
112 | */
|
---|
113 | typedef struct USBPROXYURBOSX
|
---|
114 | {
|
---|
115 | /** Pointer to the next Darwin URB. */
|
---|
116 | struct USBPROXYURBOSX *pNext;
|
---|
117 | /** Pointer to the previous Darwin URB. */
|
---|
118 | struct USBPROXYURBOSX *pPrev;
|
---|
119 | /** The millisecond timestamp when this URB was submitted. */
|
---|
120 | uint64_t u64SubmitTS;
|
---|
121 | /** Pointer to the VUSB URB.
|
---|
122 | * This is set to NULL if canceled. */
|
---|
123 | PVUSBURB pVUsbUrb;
|
---|
124 | /** Pointer to the Darwin device. */
|
---|
125 | struct USBPROXYDEVOSX *pDevOsX;
|
---|
126 | /** The transfer type. */
|
---|
127 | VUSBXFERTYPE enmType;
|
---|
128 | /** Union with data depending on transfer type. */
|
---|
129 | union
|
---|
130 | {
|
---|
131 | /** The control message. */
|
---|
132 | IOUSBDevRequest ControlMsg;
|
---|
133 | /** The Isochronous Data. */
|
---|
134 | struct
|
---|
135 | {
|
---|
136 | #ifdef USE_LOW_LATENCY_API
|
---|
137 | /** The low latency isochronous buffer. */
|
---|
138 | PUSBPROXYISOCBUF pBuf;
|
---|
139 | /** Array of frames parallel to the one in VUSBURB. (Same as pBuf->paFrames.) */
|
---|
140 | IOUSBLowLatencyIsocFrame *aFrames;
|
---|
141 | #else
|
---|
142 | /** Array of frames parallel to the one in VUSBURB. */
|
---|
143 | IOUSBIsocFrame aFrames[8];
|
---|
144 | #endif
|
---|
145 | } Isoc;
|
---|
146 | } u;
|
---|
147 | } USBPROXYURBOSX, *PUSBPROXYURBOSX;
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Per-pipe data for the Darwin usb proxy backend.
|
---|
151 | */
|
---|
152 | typedef struct USBPROXYPIPEOSX
|
---|
153 | {
|
---|
154 | /** The endpoint number. */
|
---|
155 | uint8_t u8Endpoint;
|
---|
156 | /** The IOKit pipe reference. */
|
---|
157 | uint8_t u8PipeRef;
|
---|
158 | /** The pipe Transfer type type. */
|
---|
159 | uint8_t u8TransferType;
|
---|
160 | /** The pipe direction. */
|
---|
161 | uint8_t u8Direction;
|
---|
162 | /** The endpoint interval. (interrupt) */
|
---|
163 | uint8_t u8Interval;
|
---|
164 | /** The max packet size. */
|
---|
165 | uint16_t u16MaxPacketSize;
|
---|
166 | /** The next frame number (isochronous pipes only). */
|
---|
167 | uint64_t u64NextFrameNo;
|
---|
168 | } USBPROXYPIPEOSX, *PUSBPROXYPIPEOSX, **PPUSBPROXYPIPEOSX;
|
---|
169 |
|
---|
170 | typedef struct RUNLOOPREFLIST
|
---|
171 | {
|
---|
172 | RTLISTNODE List;
|
---|
173 | CFRunLoopRef RunLoopRef;
|
---|
174 | } RUNLOOPREFLIST, *PRUNLOOPREFLIST;
|
---|
175 | typedef RUNLOOPREFLIST **PPRUNLOOPREFLIST;
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Per-interface data for the Darwin usb proxy backend.
|
---|
179 | */
|
---|
180 | typedef struct USBPROXYIFOSX
|
---|
181 | {
|
---|
182 | /** Pointer to the next interface. */
|
---|
183 | struct USBPROXYIFOSX *pNext;
|
---|
184 | /** The interface number. */
|
---|
185 | uint8_t u8Interface;
|
---|
186 | /** The current alternative interface setting.
|
---|
187 | * This is used to skip unnecessary SetAltInterface calls. */
|
---|
188 | uint8_t u8AltSetting;
|
---|
189 | /** The interface class. (not really used) */
|
---|
190 | uint8_t u8Class;
|
---|
191 | /** The interface protocol. (not really used) */
|
---|
192 | uint8_t u8Protocol;
|
---|
193 | /** The number of pipes. */
|
---|
194 | uint8_t cPipes;
|
---|
195 | /** Array containing all the pipes. (Currently unsorted.) */
|
---|
196 | USBPROXYPIPEOSX aPipes[kUSBMaxPipes];
|
---|
197 | /** The IOUSBDeviceInterface. */
|
---|
198 | IOUSBInterfaceInterface245 **ppIfI;
|
---|
199 | /** The run loop source for the async operations on the interface level. */
|
---|
200 | CFRunLoopSourceRef RunLoopSrcRef;
|
---|
201 | /** List of isochronous buffer collections.
|
---|
202 | * These are allocated on demand by the URB queuing routine and then recycled until the interface is destroyed. */
|
---|
203 | RTLISTANCHOR HeadOfRunLoopLst;
|
---|
204 | PUSBPROXYISOCBUFCOL pIsocBufCols;
|
---|
205 | } USBPROXYIFOSX, *PUSBPROXYIFOSX, **PPUSBPROXYIFOSX;
|
---|
206 | /** Pointer to a pointer to an darwin interface. */
|
---|
207 | typedef USBPROXYIFOSX **PPUSBPROXYIFOSX;
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Per-device Data for the Darwin usb proxy backend.
|
---|
211 | */
|
---|
212 | typedef struct USBPROXYDEVOSX
|
---|
213 | {
|
---|
214 | /** The USB Device IOService object. */
|
---|
215 | io_object_t USBDevice;
|
---|
216 | /** The IOUSBDeviceInterface. */
|
---|
217 | IOUSBDeviceInterface245 **ppDevI;
|
---|
218 | /** The run loop source for the async operations on the device level
|
---|
219 | * (i.e. the default control pipe stuff). */
|
---|
220 | CFRunLoopSourceRef RunLoopSrcRef;
|
---|
221 | /** we want to add and remove RunLoopSourceRefs to run loop's of
|
---|
222 | * every EMT thread participated in USB processing. */
|
---|
223 | RTLISTANCHOR HeadOfRunLoopLst;
|
---|
224 | /** Pointer to the proxy device instance. */
|
---|
225 | PUSBPROXYDEV pProxyDev;
|
---|
226 |
|
---|
227 | /** Pointer to the first interface. */
|
---|
228 | PUSBPROXYIFOSX pIfHead;
|
---|
229 | /** Pointer to the last interface. */
|
---|
230 | PUSBPROXYIFOSX pIfTail;
|
---|
231 |
|
---|
232 | /** Critical section protecting the lists. */
|
---|
233 | RTCRITSECT CritSect;
|
---|
234 | /** The list of free Darwin URBs. Singly linked. */
|
---|
235 | PUSBPROXYURBOSX pFreeHead;
|
---|
236 | /** The list of landed Darwin URBs. Doubly linked.
|
---|
237 | * Only the split head will appear in this list. */
|
---|
238 | PUSBPROXYURBOSX pTaxingHead;
|
---|
239 | /** The tail of the landed Darwin URBs. */
|
---|
240 | PUSBPROXYURBOSX pTaxingTail;
|
---|
241 | /** Last reaper runloop reference, there can be only one runloop at a time. */
|
---|
242 | CFRunLoopRef hRunLoopReapingLast;
|
---|
243 | /** Runloop source for waking up the reaper thread. */
|
---|
244 | CFRunLoopSourceRef hRunLoopSrcWakeRef;
|
---|
245 | /** List of threads used for reaping which can be woken up. */
|
---|
246 | RTLISTANCHOR HeadOfRunLoopWakeLst;
|
---|
247 | /** Runloop reference of the thread reaping. */
|
---|
248 | volatile CFRunLoopRef hRunLoopReaping;
|
---|
249 | /** Flag whether the reaping thread is about the be waked. */
|
---|
250 | volatile bool fReapingThreadWake;
|
---|
251 | } USBPROXYDEVOSX, *PUSBPROXYDEVOSX;
|
---|
252 |
|
---|
253 |
|
---|
254 | /*********************************************************************************************************************************
|
---|
255 | * Global Variables *
|
---|
256 | *********************************************************************************************************************************/
|
---|
257 | static RTONCE g_usbProxyDarwinOnce = RTONCE_INITIALIZER;
|
---|
258 | /** The runloop mode we use.
|
---|
259 | * Since it's difficult to remove this, we leak it to prevent crashes.
|
---|
260 | * @bugref{4407} */
|
---|
261 | static CFStringRef g_pRunLoopMode = NULL;
|
---|
262 | /** The IO Master Port.
|
---|
263 | * Not worth cleaning up. */
|
---|
264 | static mach_port_t g_MasterPort = NULL;
|
---|
265 |
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Init once callback that sets up g_MasterPort and g_pRunLoopMode.
|
---|
269 | *
|
---|
270 | * @returns IPRT status code.
|
---|
271 | *
|
---|
272 | * @param pvUser1 NULL, ignored.
|
---|
273 | * @param pvUser2 NULL, ignored.
|
---|
274 | */
|
---|
275 | static DECLCALLBACK(int32_t) usbProxyDarwinInitOnce(void *pvUser1)
|
---|
276 | {
|
---|
277 | int rc;
|
---|
278 | kern_return_t krc = IOMasterPort(MACH_PORT_NULL, &g_MasterPort);
|
---|
279 | if (krc == KERN_SUCCESS)
|
---|
280 | {
|
---|
281 | g_pRunLoopMode = CFStringCreateWithCString(kCFAllocatorDefault, "VBoxUsbProxyMode", kCFStringEncodingUTF8);
|
---|
282 | if (g_pRunLoopMode)
|
---|
283 | return VINF_SUCCESS;
|
---|
284 | rc = VERR_INTERNAL_ERROR_5;
|
---|
285 | }
|
---|
286 | else
|
---|
287 | rc = RTErrConvertFromDarwin(krc);
|
---|
288 | return rc;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Kicks the reaper thread if it sleeps currently to respond to state changes
|
---|
293 | * or to pick up completed URBs.
|
---|
294 | *
|
---|
295 | * @returns nothing.
|
---|
296 | * @param pDevOsX The darwin device instance data.
|
---|
297 | */
|
---|
298 | static void usbProxyDarwinReaperKick(PUSBPROXYDEVOSX pDevOsX)
|
---|
299 | {
|
---|
300 | CFRunLoopRef hRunLoopWake = (CFRunLoopRef)ASMAtomicReadPtr((void * volatile *)&pDevOsX->hRunLoopReaping);
|
---|
301 | if (hRunLoopWake)
|
---|
302 | {
|
---|
303 | LogFlowFunc(("Waking runloop %p\n", hRunLoopWake));
|
---|
304 | CFRunLoopSourceSignal(pDevOsX->hRunLoopSrcWakeRef);
|
---|
305 | CFRunLoopWakeUp(hRunLoopWake);
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Adds Source ref to current run loop and adds it the list of runloops.
|
---|
311 | */
|
---|
312 | static int usbProxyDarwinAddRunLoopRef(PRTLISTANCHOR pListHead,
|
---|
313 | CFRunLoopSourceRef SourceRef)
|
---|
314 | {
|
---|
315 | AssertPtrReturn(pListHead, VERR_INVALID_PARAMETER);
|
---|
316 | AssertReturn(CFRunLoopSourceIsValid(SourceRef), VERR_INVALID_PARAMETER);
|
---|
317 |
|
---|
318 | if (CFRunLoopContainsSource(CFRunLoopGetCurrent(), SourceRef, g_pRunLoopMode))
|
---|
319 | return VINF_SUCCESS;
|
---|
320 |
|
---|
321 | /* Add to the list */
|
---|
322 | PRUNLOOPREFLIST pListNode = (PRUNLOOPREFLIST)RTMemAllocZ(sizeof(RUNLOOPREFLIST));
|
---|
323 | if (!pListNode)
|
---|
324 | return VERR_NO_MEMORY;
|
---|
325 |
|
---|
326 | pListNode->RunLoopRef = CFRunLoopGetCurrent();
|
---|
327 |
|
---|
328 | CFRetain(pListNode->RunLoopRef);
|
---|
329 | CFRetain(SourceRef); /* We want to be aware of releasing */
|
---|
330 |
|
---|
331 | CFRunLoopAddSource(pListNode->RunLoopRef, SourceRef, g_pRunLoopMode);
|
---|
332 |
|
---|
333 | RTListInit(&pListNode->List);
|
---|
334 |
|
---|
335 | RTListAppend((PRTLISTNODE)pListHead, &pListNode->List);
|
---|
336 |
|
---|
337 | return VINF_SUCCESS;
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * Removes all source reference from mode of run loop's we've registered them.
|
---|
343 | *
|
---|
344 | */
|
---|
345 | static int usbProxyDarwinRemoveSourceRefFromAllRunLoops(PRTLISTANCHOR pHead,
|
---|
346 | CFRunLoopSourceRef SourceRef)
|
---|
347 | {
|
---|
348 | AssertPtrReturn(pHead, VERR_INVALID_PARAMETER);
|
---|
349 |
|
---|
350 | while (!RTListIsEmpty(pHead))
|
---|
351 | {
|
---|
352 | PRUNLOOPREFLIST pNode = RTListGetFirst(pHead, RUNLOOPREFLIST, List);
|
---|
353 | /* XXX: Should Release Reference? */
|
---|
354 | Assert(CFGetRetainCount(pNode->RunLoopRef));
|
---|
355 |
|
---|
356 | CFRunLoopRemoveSource(pNode->RunLoopRef, SourceRef, g_pRunLoopMode);
|
---|
357 | CFRelease(SourceRef);
|
---|
358 | CFRelease(pNode->RunLoopRef);
|
---|
359 |
|
---|
360 | RTListNodeRemove(&pNode->List);
|
---|
361 |
|
---|
362 | RTMemFree(pNode);
|
---|
363 | }
|
---|
364 |
|
---|
365 | return VINF_SUCCESS;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Allocates a Darwin URB request structure.
|
---|
371 | *
|
---|
372 | * @returns Pointer to an active URB request.
|
---|
373 | * @returns NULL on failure.
|
---|
374 | *
|
---|
375 | * @param pDevOsX The darwin proxy device.
|
---|
376 | */
|
---|
377 | static PUSBPROXYURBOSX usbProxyDarwinUrbAlloc(PUSBPROXYDEVOSX pDevOsX)
|
---|
378 | {
|
---|
379 | PUSBPROXYURBOSX pUrbOsX;
|
---|
380 |
|
---|
381 | RTCritSectEnter(&pDevOsX->CritSect);
|
---|
382 |
|
---|
383 | /*
|
---|
384 | * Try remove a Darwin URB from the free list, if none there allocate a new one.
|
---|
385 | */
|
---|
386 | pUrbOsX = pDevOsX->pFreeHead;
|
---|
387 | if (pUrbOsX)
|
---|
388 | {
|
---|
389 | pDevOsX->pFreeHead = pUrbOsX->pNext;
|
---|
390 | RTCritSectLeave(&pDevOsX->CritSect);
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | RTCritSectLeave(&pDevOsX->CritSect);
|
---|
395 | pUrbOsX = (PUSBPROXYURBOSX)RTMemAlloc(sizeof(*pUrbOsX));
|
---|
396 | if (!pUrbOsX)
|
---|
397 | return NULL;
|
---|
398 | }
|
---|
399 | pUrbOsX->pVUsbUrb = NULL;
|
---|
400 | pUrbOsX->pDevOsX = pDevOsX;
|
---|
401 | pUrbOsX->enmType = VUSBXFERTYPE_INVALID;
|
---|
402 |
|
---|
403 | return pUrbOsX;
|
---|
404 | }
|
---|
405 |
|
---|
406 |
|
---|
407 | #ifdef USE_LOW_LATENCY_API
|
---|
408 | /**
|
---|
409 | * Allocates an low latency isochronous buffer.
|
---|
410 | *
|
---|
411 | * @returns VBox status code.
|
---|
412 | * @param pUrbOsX The OsX URB to allocate it for.
|
---|
413 | * @param pIf The interface to allocated it from.
|
---|
414 | */
|
---|
415 | static int usbProxyDarwinUrbAllocIsocBuf(PUSBPROXYURBOSX pUrbOsX, PUSBPROXYIFOSX pIf)
|
---|
416 | {
|
---|
417 | USBLowLatencyBufferType enmLLType = pUrbOsX->pVUsbUrb->enmDir == VUSBDIRECTION_IN
|
---|
418 | ? kUSBLowLatencyWriteBuffer : kUSBLowLatencyReadBuffer;
|
---|
419 |
|
---|
420 | /*
|
---|
421 | * Walk the buffer collection list and look for an unused one.
|
---|
422 | */
|
---|
423 | pUrbOsX->u.Isoc.pBuf = NULL;
|
---|
424 | for (PUSBPROXYISOCBUFCOL pCur = pIf->pIsocBufCols; pCur; pCur = pCur->pNext)
|
---|
425 | if (pCur->enmType == enmLLType)
|
---|
426 | for (unsigned i = 0; i < RT_ELEMENTS(pCur->aBuffers); i++)
|
---|
427 | if (!pCur->aBuffers[i].fUsed)
|
---|
428 | {
|
---|
429 | pCur->aBuffers[i].fUsed = true;
|
---|
430 | pUrbOsX->u.Isoc.pBuf = &pCur->aBuffers[i];
|
---|
431 | AssertPtr(pUrbOsX->u.Isoc.pBuf);
|
---|
432 | AssertPtr(pUrbOsX->u.Isoc.pBuf->pvBuf);
|
---|
433 | pUrbOsX->u.Isoc.aFrames = pCur->aBuffers[i].paFrames;
|
---|
434 | AssertPtr(pUrbOsX->u.Isoc.aFrames);
|
---|
435 | return VINF_SUCCESS;
|
---|
436 | }
|
---|
437 |
|
---|
438 | /*
|
---|
439 | * Didn't find an empty one, create a new buffer collection and take the first buffer.
|
---|
440 | */
|
---|
441 | PUSBPROXYISOCBUFCOL pNew = (PUSBPROXYISOCBUFCOL)RTMemAllocZ(sizeof(*pNew));
|
---|
442 | AssertReturn(pNew, VERR_NO_MEMORY);
|
---|
443 |
|
---|
444 | IOReturn irc = (*pIf->ppIfI)->LowLatencyCreateBuffer(pIf->ppIfI, &pNew->pvBuffer, 8192 * RT_ELEMENTS(pNew->aBuffers), enmLLType);
|
---|
445 | if (irc == kIOReturnSuccess != VALID_PTR(pNew->pvBuffer))
|
---|
446 | {
|
---|
447 | AssertPtr(pNew->pvBuffer);
|
---|
448 | irc = kIOReturnNoMemory;
|
---|
449 | }
|
---|
450 | if (irc == kIOReturnSuccess)
|
---|
451 | {
|
---|
452 | irc = (*pIf->ppIfI)->LowLatencyCreateBuffer(pIf->ppIfI, &pNew->pvFrames, PAGE_SIZE, kUSBLowLatencyFrameListBuffer);
|
---|
453 | if (irc == kIOReturnSuccess != VALID_PTR(pNew->pvFrames))
|
---|
454 | {
|
---|
455 | AssertPtr(pNew->pvFrames);
|
---|
456 | irc = kIOReturnNoMemory;
|
---|
457 | }
|
---|
458 | if (irc == kIOReturnSuccess)
|
---|
459 | {
|
---|
460 | for (unsigned i = 0; i < RT_ELEMENTS(pNew->aBuffers); i++)
|
---|
461 | {
|
---|
462 | //pNew->aBuffers[i].fUsed = false;
|
---|
463 | pNew->aBuffers[i].paFrames = &((IOUSBLowLatencyIsocFrame *)pNew->pvFrames)[i * 8];
|
---|
464 | pNew->aBuffers[i].pvBuf = (uint8_t *)pNew->pvBuffer + i * 8192;
|
---|
465 | }
|
---|
466 |
|
---|
467 | pNew->aBuffers[0].fUsed = true;
|
---|
468 | pUrbOsX->u.Isoc.aFrames = pNew->aBuffers[0].paFrames;
|
---|
469 | pUrbOsX->u.Isoc.pBuf = &pNew->aBuffers[0];
|
---|
470 |
|
---|
471 | pNew->enmType = enmLLType;
|
---|
472 | pNew->pNext = pIf->pIsocBufCols;
|
---|
473 | pIf->pIsocBufCols = pNew;
|
---|
474 |
|
---|
475 | #if 0 /* doesn't help :-/ */
|
---|
476 | /*
|
---|
477 | * If this is the first time we're here, try mess with the policy?
|
---|
478 | */
|
---|
479 | if (!pNew->pNext)
|
---|
480 | for (unsigned iPipe = 0; iPipe < pIf->cPipes; iPipe++)
|
---|
481 | if (pIf->aPipes[iPipe].u8TransferType == kUSBIsoc)
|
---|
482 | {
|
---|
483 | irc = (*pIf->ppIfI)->SetPipePolicy(pIf->ppIfI, pIf->aPipes[iPipe].u8PipeRef,
|
---|
484 | pIf->aPipes[iPipe].u16MaxPacketSize, pIf->aPipes[iPipe].u8Interval);
|
---|
485 | AssertMsg(irc == kIOReturnSuccess, ("%#x\n", irc));
|
---|
486 | }
|
---|
487 | #endif
|
---|
488 |
|
---|
489 | return VINF_SUCCESS;
|
---|
490 | }
|
---|
491 |
|
---|
492 | /* bail out */
|
---|
493 | (*pIf->ppIfI)->LowLatencyDestroyBuffer(pIf->ppIfI, pNew->pvBuffer);
|
---|
494 | }
|
---|
495 | AssertMsgFailed(("%#x\n", irc));
|
---|
496 | RTMemFree(pNew);
|
---|
497 |
|
---|
498 | return RTErrConvertFromDarwin(irc);
|
---|
499 | }
|
---|
500 | #endif /* USE_LOW_LATENCY_API */
|
---|
501 |
|
---|
502 |
|
---|
503 | /**
|
---|
504 | * Frees a Darwin URB request structure.
|
---|
505 | *
|
---|
506 | * @param pDevOsX The darwin proxy device.
|
---|
507 | * @param pUrbOsX The Darwin URB to free.
|
---|
508 | */
|
---|
509 | static void usbProxyDarwinUrbFree(PUSBPROXYDEVOSX pDevOsX, PUSBPROXYURBOSX pUrbOsX)
|
---|
510 | {
|
---|
511 | RTCritSectEnter(&pDevOsX->CritSect);
|
---|
512 |
|
---|
513 | #ifdef USE_LOW_LATENCY_API
|
---|
514 | /*
|
---|
515 | * Free low latency stuff.
|
---|
516 | */
|
---|
517 | if ( pUrbOsX->enmType == VUSBXFERTYPE_ISOC
|
---|
518 | && pUrbOsX->u.Isoc.pBuf)
|
---|
519 | {
|
---|
520 | pUrbOsX->u.Isoc.pBuf->fUsed = false;
|
---|
521 | pUrbOsX->u.Isoc.pBuf = NULL;
|
---|
522 | }
|
---|
523 | #endif
|
---|
524 |
|
---|
525 | /*
|
---|
526 | * Link it into the free list.
|
---|
527 | */
|
---|
528 | pUrbOsX->pPrev = NULL;
|
---|
529 | pUrbOsX->pNext = pDevOsX->pFreeHead;
|
---|
530 | pDevOsX->pFreeHead = pUrbOsX;
|
---|
531 |
|
---|
532 | pUrbOsX->pVUsbUrb = NULL;
|
---|
533 | pUrbOsX->pDevOsX = NULL;
|
---|
534 | pUrbOsX->enmType = VUSBXFERTYPE_INVALID;
|
---|
535 |
|
---|
536 | RTCritSectLeave(&pDevOsX->CritSect);
|
---|
537 | }
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * Translate the IOKit status code to a VUSB status.
|
---|
541 | *
|
---|
542 | * @returns VUSB URB status code.
|
---|
543 | * @param irc IOKit status code.
|
---|
544 | */
|
---|
545 | static VUSBSTATUS vusbProxyDarwinStatusToVUsbStatus(IOReturn irc)
|
---|
546 | {
|
---|
547 | switch (irc)
|
---|
548 | {
|
---|
549 | /* IOKit OHCI VUSB */
|
---|
550 | case kIOReturnSuccess: /* 0 */ return VUSBSTATUS_OK;
|
---|
551 | case kIOUSBCRCErr: /* 1 */ return VUSBSTATUS_CRC;
|
---|
552 | //case kIOUSBBitstufErr: /* 2 */ return VUSBSTATUS_;
|
---|
553 | //case kIOUSBDataToggleErr: /* 3 */ return VUSBSTATUS_;
|
---|
554 | case kIOUSBPipeStalled: /* 4 */ return VUSBSTATUS_STALL;
|
---|
555 | case kIOReturnNotResponding: /* 5 */ return VUSBSTATUS_DNR;
|
---|
556 | //case kIOUSBPIDCheckErr: /* 6 */ return VUSBSTATUS_;
|
---|
557 | //case kIOUSBWrongPIDErr: /* 7 */ return VUSBSTATUS_;
|
---|
558 | case kIOReturnOverrun: /* 8 */ return VUSBSTATUS_DATA_OVERRUN;
|
---|
559 | case kIOReturnUnderrun: /* 9 */ return VUSBSTATUS_DATA_UNDERRUN;
|
---|
560 | //case kIOUSBReserved1Err: /* 10 */ return VUSBSTATUS_;
|
---|
561 | //case kIOUSBReserved2Err: /* 11 */ return VUSBSTATUS_;
|
---|
562 | //case kIOUSBBufferOverrunErr: /* 12 */ return VUSBSTATUS_;
|
---|
563 | //case kIOUSBBufferUnderrunErr: /* 13 */ return VUSBSTATUS_;
|
---|
564 | case kIOUSBNotSent1Err: /* 14 */ return VUSBSTATUS_NOT_ACCESSED/*VUSBSTATUS_OK*/;
|
---|
565 | case kIOUSBNotSent2Err: /* 15 */ return VUSBSTATUS_NOT_ACCESSED/*VUSBSTATUS_OK*/;
|
---|
566 |
|
---|
567 | /* Other errors */
|
---|
568 | case kIOUSBTransactionTimeout: return VUSBSTATUS_DNR;
|
---|
569 | //case kIOReturnAborted: return VUSBSTATUS_CRC; - see on SET_INTERFACE...
|
---|
570 |
|
---|
571 | default:
|
---|
572 | Log(("vusbProxyDarwinStatusToVUsbStatus: irc=%#x!!\n", irc));
|
---|
573 | return VUSBSTATUS_STALL;
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 | /**
|
---|
579 | * Completion callback for an async URB transfer.
|
---|
580 | *
|
---|
581 | * @param pvUrbOsX The Darwin URB.
|
---|
582 | * @param irc The status of the operation.
|
---|
583 | * @param Size Possible the transfer size.
|
---|
584 | */
|
---|
585 | static void usbProxyDarwinUrbAsyncComplete(void *pvUrbOsX, IOReturn irc, void *Size)
|
---|
586 | {
|
---|
587 | PUSBPROXYURBOSX pUrbOsX = (PUSBPROXYURBOSX)pvUrbOsX;
|
---|
588 | PUSBPROXYDEVOSX pDevOsX = pUrbOsX->pDevOsX;
|
---|
589 | const uint32_t cb = (uintptr_t)Size;
|
---|
590 |
|
---|
591 | /*
|
---|
592 | * Do status updates.
|
---|
593 | */
|
---|
594 | PVUSBURB pUrb = pUrbOsX->pVUsbUrb;
|
---|
595 | if (pUrb)
|
---|
596 | {
|
---|
597 | Assert(pUrb->u32Magic == VUSBURB_MAGIC);
|
---|
598 | if (pUrb->enmType == VUSBXFERTYPE_ISOC)
|
---|
599 | {
|
---|
600 | #ifdef USE_LOW_LATENCY_API
|
---|
601 | /* copy the data. */
|
---|
602 | //if (pUrb->enmDir == VUSBDIRECTION_IN)
|
---|
603 | memcpy(pUrb->abData, pUrbOsX->u.Isoc.pBuf->pvBuf, pUrb->cbData);
|
---|
604 | #endif
|
---|
605 | Log3(("AsyncComplete isoc - raw data (%d bytes):\n"
|
---|
606 | "%16.*Rhxd\n", pUrb->cbData, pUrb->cbData, pUrb->abData));
|
---|
607 | uint32_t off = 0;
|
---|
608 | for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
|
---|
609 | {
|
---|
610 | #ifdef USE_LOW_LATENCY_API
|
---|
611 | Log2((" %d{%d/%d-%x-%RX64}", i, pUrbOsX->u.Isoc.aFrames[i].frActCount, pUrb->aIsocPkts[i].cb, pUrbOsX->u.Isoc.aFrames[i].frStatus,
|
---|
612 | RT_MAKE_U64(pUrbOsX->u.Isoc.aFrames[i].frTimeStamp.lo, pUrbOsX->u.Isoc.aFrames[i].frTimeStamp.hi) ));
|
---|
613 | #else
|
---|
614 | Log2((" %d{%d/%d-%x}", i, pUrbOsX->u.Isoc.aFrames[i].frActCount, pUrb->aIsocPkts[i].cb, pUrbOsX->u.Isoc.aFrames[i].frStatus));
|
---|
615 | #endif
|
---|
616 | pUrb->aIsocPkts[i].enmStatus = vusbProxyDarwinStatusToVUsbStatus(pUrbOsX->u.Isoc.aFrames[i].frStatus);
|
---|
617 | pUrb->aIsocPkts[i].cb = pUrbOsX->u.Isoc.aFrames[i].frActCount;
|
---|
618 | off += pUrbOsX->u.Isoc.aFrames[i].frActCount;
|
---|
619 | }
|
---|
620 | Log2(("\n"));
|
---|
621 | #if 0 /** @todo revisit this, wasn't working previously. */
|
---|
622 | for (int i = (int)pUrb->cIsocPkts - 1; i >= 0; i--)
|
---|
623 | Assert( !pUrbOsX->u.Isoc.aFrames[i].frActCount
|
---|
624 | && !pUrbOsX->u.Isoc.aFrames[i].frReqCount
|
---|
625 | && !pUrbOsX->u.Isoc.aFrames[i].frStatus);
|
---|
626 | #endif
|
---|
627 | pUrb->cbData = off; /* 'Size' seems to be pointing at an error code or something... */
|
---|
628 | pUrb->enmStatus = VUSBSTATUS_OK; /* Don't use 'irc'. OHCI expects OK unless it's a really bad error. */
|
---|
629 | }
|
---|
630 | else
|
---|
631 | {
|
---|
632 | pUrb->cbData = cb;
|
---|
633 | pUrb->enmStatus = vusbProxyDarwinStatusToVUsbStatus(irc);
|
---|
634 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
635 | pUrb->cbData += sizeof(VUSBSETUP);
|
---|
636 | }
|
---|
637 | }
|
---|
638 |
|
---|
639 | RTCritSectEnter(&pDevOsX->CritSect);
|
---|
640 |
|
---|
641 | /*
|
---|
642 | * Link it into the taxing list.
|
---|
643 | */
|
---|
644 | pUrbOsX->pNext = NULL;
|
---|
645 | pUrbOsX->pPrev = pDevOsX->pTaxingTail;
|
---|
646 | if (pDevOsX->pTaxingTail)
|
---|
647 | pDevOsX->pTaxingTail->pNext = pUrbOsX;
|
---|
648 | else
|
---|
649 | pDevOsX->pTaxingHead = pUrbOsX;
|
---|
650 | pDevOsX->pTaxingTail = pUrbOsX;
|
---|
651 |
|
---|
652 | RTCritSectLeave(&pDevOsX->CritSect);
|
---|
653 |
|
---|
654 | LogFlow(("%s: usbProxyDarwinUrbAsyncComplete: cb=%d EndPt=%#x irc=%#x (%d)\n",
|
---|
655 | pUrb->pszDesc, cb, pUrb ? pUrb->EndPt : 0xff, irc, pUrb ? pUrb->enmStatus : 0xff));
|
---|
656 | }
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Release all interfaces (current config).
|
---|
660 | *
|
---|
661 | * @param pDevOsX The darwin proxy device.
|
---|
662 | */
|
---|
663 | static void usbProxyDarwinReleaseAllInterfaces(PUSBPROXYDEVOSX pDevOsX)
|
---|
664 | {
|
---|
665 | RTCritSectEnter(&pDevOsX->CritSect);
|
---|
666 |
|
---|
667 | /* Kick the reaper thread out of sleep. */
|
---|
668 | usbProxyDarwinReaperKick(pDevOsX);
|
---|
669 |
|
---|
670 | PUSBPROXYIFOSX pIf = pDevOsX->pIfHead;
|
---|
671 | pDevOsX->pIfHead = pDevOsX->pIfTail = NULL;
|
---|
672 |
|
---|
673 | while (pIf)
|
---|
674 | {
|
---|
675 | PUSBPROXYIFOSX pNext = pIf->pNext;
|
---|
676 | IOReturn irc;
|
---|
677 |
|
---|
678 | if (pIf->RunLoopSrcRef)
|
---|
679 | {
|
---|
680 | int rc = usbProxyDarwinRemoveSourceRefFromAllRunLoops((PRTLISTANCHOR)&pIf->HeadOfRunLoopLst, pIf->RunLoopSrcRef);
|
---|
681 | AssertRC(rc);
|
---|
682 |
|
---|
683 | CFRelease(pIf->RunLoopSrcRef);
|
---|
684 | pIf->RunLoopSrcRef = NULL;
|
---|
685 | RTListInit((PRTLISTNODE)&pIf->HeadOfRunLoopLst);
|
---|
686 | }
|
---|
687 |
|
---|
688 | while (pIf->pIsocBufCols)
|
---|
689 | {
|
---|
690 | PUSBPROXYISOCBUFCOL pCur = pIf->pIsocBufCols;
|
---|
691 | pIf->pIsocBufCols = pCur->pNext;
|
---|
692 | pCur->pNext = NULL;
|
---|
693 |
|
---|
694 | irc = (*pIf->ppIfI)->LowLatencyDestroyBuffer(pIf->ppIfI, pCur->pvBuffer);
|
---|
695 | AssertMsg(irc == kIOReturnSuccess || irc == MACH_SEND_INVALID_DEST, ("%#x\n", irc));
|
---|
696 | pCur->pvBuffer = NULL;
|
---|
697 |
|
---|
698 | irc = (*pIf->ppIfI)->LowLatencyDestroyBuffer(pIf->ppIfI, pCur->pvFrames);
|
---|
699 | AssertMsg(irc == kIOReturnSuccess || irc == MACH_SEND_INVALID_DEST, ("%#x\n", irc));
|
---|
700 | pCur->pvFrames = NULL;
|
---|
701 |
|
---|
702 | RTMemFree(pCur);
|
---|
703 | }
|
---|
704 |
|
---|
705 | irc = (*pIf->ppIfI)->USBInterfaceClose(pIf->ppIfI);
|
---|
706 | AssertMsg(irc == kIOReturnSuccess || irc == kIOReturnNoDevice, ("%#x\n", irc));
|
---|
707 |
|
---|
708 | (*pIf->ppIfI)->Release(pIf->ppIfI);
|
---|
709 | pIf->ppIfI = NULL;
|
---|
710 |
|
---|
711 | RTMemFree(pIf);
|
---|
712 |
|
---|
713 | pIf = pNext;
|
---|
714 | }
|
---|
715 | RTCritSectLeave(&pDevOsX->CritSect);
|
---|
716 | }
|
---|
717 |
|
---|
718 |
|
---|
719 | /**
|
---|
720 | * Get the properties all the pipes associated with an interface.
|
---|
721 | *
|
---|
722 | * This is used when we seize all the interface and after SET_INTERFACE.
|
---|
723 | *
|
---|
724 | * @returns VBox status code.
|
---|
725 | * @param pDevOsX The darwin proxy device.
|
---|
726 | * @param pIf The interface to get pipe props for.
|
---|
727 | */
|
---|
728 | static int usbProxyDarwinGetPipeProperties(PUSBPROXYDEVOSX pDevOsX, PUSBPROXYIFOSX pIf)
|
---|
729 | {
|
---|
730 | /*
|
---|
731 | * Get the pipe (endpoint) count (it might have changed - even on open).
|
---|
732 | */
|
---|
733 | int rc = VINF_SUCCESS;
|
---|
734 | UInt8 cPipes;
|
---|
735 | IOReturn irc = (*pIf->ppIfI)->GetNumEndpoints(pIf->ppIfI, &cPipes);
|
---|
736 | if (irc != kIOReturnSuccess)
|
---|
737 | {
|
---|
738 | pIf->cPipes = 0;
|
---|
739 | if (irc == kIOReturnNoDevice)
|
---|
740 | rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
741 | else
|
---|
742 | rc = RTErrConvertFromDarwin(irc);
|
---|
743 | return rc;
|
---|
744 | }
|
---|
745 | AssertRelease(cPipes < RT_ELEMENTS(pIf->aPipes));
|
---|
746 | pIf->cPipes = cPipes + 1;
|
---|
747 |
|
---|
748 | /*
|
---|
749 | * Get the properties of each pipe.
|
---|
750 | */
|
---|
751 | for (unsigned i = 0; i < pIf->cPipes; i++)
|
---|
752 | {
|
---|
753 | pIf->aPipes[i].u8PipeRef = i;
|
---|
754 | pIf->aPipes[i].u64NextFrameNo = 0;
|
---|
755 | irc = (*pIf->ppIfI)->GetPipeProperties(pIf->ppIfI, i,
|
---|
756 | &pIf->aPipes[i].u8Direction,
|
---|
757 | &pIf->aPipes[i].u8Endpoint,
|
---|
758 | &pIf->aPipes[i].u8TransferType,
|
---|
759 | &pIf->aPipes[i].u16MaxPacketSize,
|
---|
760 | &pIf->aPipes[i].u8Interval);
|
---|
761 | if (irc != kIOReturnSuccess)
|
---|
762 | {
|
---|
763 | LogRel(("USB: Failed to query properties for pipe %#d / interface %#x on device '%s'. (prot=%#x class=%#x)\n",
|
---|
764 | i, pIf->u8Interface, pDevOsX->pProxyDev->pUsbIns->pszName, pIf->u8Protocol, pIf->u8Class));
|
---|
765 | if (irc == kIOReturnNoDevice)
|
---|
766 | rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
767 | else
|
---|
768 | rc = RTErrConvertFromDarwin(irc);
|
---|
769 | pIf->cPipes = i;
|
---|
770 | break;
|
---|
771 | }
|
---|
772 | /* reconstruct bEndpoint */
|
---|
773 | if (pIf->aPipes[i].u8Direction == kUSBIn)
|
---|
774 | pIf->aPipes[i].u8Endpoint |= 0x80;
|
---|
775 | Log2(("usbProxyDarwinGetPipeProperties: #If=%d EndPt=%#x Dir=%d Type=%d PipeRef=%#x MaxPktSize=%#x Interval=%#x\n",
|
---|
776 | pIf->u8Interface, pIf->aPipes[i].u8Endpoint, pIf->aPipes[i].u8Direction, pIf->aPipes[i].u8TransferType,
|
---|
777 | pIf->aPipes[i].u8PipeRef, pIf->aPipes[i].u16MaxPacketSize, pIf->aPipes[i].u8Interval));
|
---|
778 | }
|
---|
779 |
|
---|
780 | /** @todo sort or hash these for speedy lookup... */
|
---|
781 | return VINF_SUCCESS;
|
---|
782 | }
|
---|
783 |
|
---|
784 |
|
---|
785 | /**
|
---|
786 | * Seize all interfaces (current config).
|
---|
787 | *
|
---|
788 | * @returns VBox status code.
|
---|
789 | * @param pDevOsX The darwin proxy device.
|
---|
790 | * @param fMakeTheBestOfIt If set we will not give up on error. This is for
|
---|
791 | * use during SET_CONFIGURATION and similar.
|
---|
792 | */
|
---|
793 | static int usbProxyDarwinSeizeAllInterfaces(PUSBPROXYDEVOSX pDevOsX, bool fMakeTheBestOfIt)
|
---|
794 | {
|
---|
795 | PUSBPROXYDEV pProxyDev = pDevOsX->pProxyDev;
|
---|
796 |
|
---|
797 | RTCritSectEnter(&pDevOsX->CritSect);
|
---|
798 |
|
---|
799 | /*
|
---|
800 | * Create a interface enumerator for all the interface (current config).
|
---|
801 | */
|
---|
802 | io_iterator_t Interfaces = NULL;
|
---|
803 | IOUSBFindInterfaceRequest Req;
|
---|
804 | Req.bInterfaceClass = kIOUSBFindInterfaceDontCare;
|
---|
805 | Req.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
|
---|
806 | Req.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
|
---|
807 | Req.bAlternateSetting = kIOUSBFindInterfaceDontCare;
|
---|
808 | IOReturn irc = (*pDevOsX->ppDevI)->CreateInterfaceIterator(pDevOsX->ppDevI, &Req, &Interfaces);
|
---|
809 | int rc;
|
---|
810 | if (irc == kIOReturnSuccess)
|
---|
811 | {
|
---|
812 | /*
|
---|
813 | * Iterate the interfaces.
|
---|
814 | */
|
---|
815 | io_object_t Interface;
|
---|
816 | rc = VINF_SUCCESS;
|
---|
817 | while ((Interface = IOIteratorNext(Interfaces)))
|
---|
818 | {
|
---|
819 | /*
|
---|
820 | * Create a plug-in and query the IOUSBInterfaceInterface (cute name).
|
---|
821 | */
|
---|
822 | IOCFPlugInInterface **ppPlugInInterface = NULL;
|
---|
823 | kern_return_t krc;
|
---|
824 | SInt32 Score = 0;
|
---|
825 | krc = IOCreatePlugInInterfaceForService(Interface, kIOUSBInterfaceUserClientTypeID,
|
---|
826 | kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
|
---|
827 | IOObjectRelease(Interface);
|
---|
828 | Interface = NULL;
|
---|
829 | if (krc == KERN_SUCCESS)
|
---|
830 | {
|
---|
831 | IOUSBInterfaceInterface245 **ppIfI;
|
---|
832 | HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
|
---|
833 | CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID245),
|
---|
834 | (LPVOID *)&ppIfI);
|
---|
835 | krc = IODestroyPlugInInterface(ppPlugInInterface); Assert(krc == KERN_SUCCESS);
|
---|
836 | ppPlugInInterface = NULL;
|
---|
837 | if (hrc == S_OK)
|
---|
838 | {
|
---|
839 | /*
|
---|
840 | * Query some basic properties first.
|
---|
841 | * (This means we can print more informative messages on failure
|
---|
842 | * to seize the interface.)
|
---|
843 | */
|
---|
844 | UInt8 u8Interface = 0xff;
|
---|
845 | irc = (*ppIfI)->GetInterfaceNumber(ppIfI, &u8Interface);
|
---|
846 | UInt8 u8AltSetting = 0xff;
|
---|
847 | if (irc == kIOReturnSuccess)
|
---|
848 | irc = (*ppIfI)->GetAlternateSetting(ppIfI, &u8AltSetting);
|
---|
849 | UInt8 u8Class = 0xff;
|
---|
850 | if (irc == kIOReturnSuccess)
|
---|
851 | irc = (*ppIfI)->GetInterfaceClass(ppIfI, &u8Class);
|
---|
852 | UInt8 u8Protocol = 0xff;
|
---|
853 | if (irc == kIOReturnSuccess)
|
---|
854 | irc = (*ppIfI)->GetInterfaceProtocol(ppIfI, &u8Protocol);
|
---|
855 | UInt8 cEndpoints = 0;
|
---|
856 | if (irc == kIOReturnSuccess)
|
---|
857 | irc = (*ppIfI)->GetNumEndpoints(ppIfI, &cEndpoints);
|
---|
858 | if (irc == kIOReturnSuccess)
|
---|
859 | {
|
---|
860 | /*
|
---|
861 | * Try seize the interface.
|
---|
862 | */
|
---|
863 | irc = (*ppIfI)->USBInterfaceOpenSeize(ppIfI);
|
---|
864 | if (irc == kIOReturnSuccess)
|
---|
865 | {
|
---|
866 | PUSBPROXYIFOSX pIf = (PUSBPROXYIFOSX)RTMemAllocZ(sizeof(*pIf));
|
---|
867 | if (pIf)
|
---|
868 | {
|
---|
869 | /*
|
---|
870 | * Create the per-interface entry and query the
|
---|
871 | * endpoint data.
|
---|
872 | */
|
---|
873 | /* initialize the entry */
|
---|
874 | pIf->u8Interface = u8Interface;
|
---|
875 | pIf->u8AltSetting = u8AltSetting;
|
---|
876 | pIf->u8Class = u8Class;
|
---|
877 | pIf->u8Protocol = u8Protocol;
|
---|
878 | pIf->cPipes = cEndpoints;
|
---|
879 | pIf->ppIfI = ppIfI;
|
---|
880 |
|
---|
881 | /* query pipe/endpoint properties. */
|
---|
882 | rc = usbProxyDarwinGetPipeProperties(pDevOsX, pIf);
|
---|
883 | if (RT_SUCCESS(rc))
|
---|
884 | {
|
---|
885 | /*
|
---|
886 | * Create the async event source and add it to the
|
---|
887 | * default current run loop.
|
---|
888 | * (Later: Add to the worker thread run loop instead.)
|
---|
889 | */
|
---|
890 | irc = (*ppIfI)->CreateInterfaceAsyncEventSource(ppIfI, &pIf->RunLoopSrcRef);
|
---|
891 | if (irc == kIOReturnSuccess)
|
---|
892 | {
|
---|
893 | RTListInit((PRTLISTNODE)&pIf->HeadOfRunLoopLst);
|
---|
894 | usbProxyDarwinAddRunLoopRef(&pIf->HeadOfRunLoopLst,
|
---|
895 | pIf->RunLoopSrcRef);
|
---|
896 |
|
---|
897 | /*
|
---|
898 | * Just link the interface into the list and we're good.
|
---|
899 | */
|
---|
900 | pIf->pNext = NULL;
|
---|
901 | Log(("USB: Seized interface %#x (alt=%d prot=%#x class=%#x)\n",
|
---|
902 | u8Interface, u8AltSetting, u8Protocol, u8Class));
|
---|
903 | if (pDevOsX->pIfTail)
|
---|
904 | pDevOsX->pIfTail = pDevOsX->pIfTail->pNext = pIf;
|
---|
905 | else
|
---|
906 | pDevOsX->pIfTail = pDevOsX->pIfHead = pIf;
|
---|
907 | continue;
|
---|
908 | }
|
---|
909 | rc = RTErrConvertFromDarwin(irc);
|
---|
910 | }
|
---|
911 |
|
---|
912 | /* failure cleanup. */
|
---|
913 | RTMemFree(pIf);
|
---|
914 | }
|
---|
915 | }
|
---|
916 | else if (irc == kIOReturnExclusiveAccess)
|
---|
917 | {
|
---|
918 | LogRel(("USB: Interface %#x on device '%s' is being used by another process. (prot=%#x class=%#x)\n",
|
---|
919 | u8Interface, pProxyDev->pUsbIns->pszName, u8Protocol, u8Class));
|
---|
920 | rc = VERR_SHARING_VIOLATION;
|
---|
921 | }
|
---|
922 | else
|
---|
923 | {
|
---|
924 | LogRel(("USB: Failed to open interface %#x on device '%s'. (prot=%#x class=%#x) krc=%#x\n",
|
---|
925 | u8Interface, pProxyDev->pUsbIns->pszName, u8Protocol, u8Class, irc));
|
---|
926 | rc = VERR_OPEN_FAILED;
|
---|
927 | }
|
---|
928 | }
|
---|
929 | else
|
---|
930 | {
|
---|
931 | rc = RTErrConvertFromDarwin(irc);
|
---|
932 | LogRel(("USB: Failed to query interface properties on device '%s', irc=%#x.\n",
|
---|
933 | pProxyDev->pUsbIns->pszName, irc));
|
---|
934 | }
|
---|
935 | (*ppIfI)->Release(ppIfI);
|
---|
936 | ppIfI = NULL;
|
---|
937 | }
|
---|
938 | else if (RT_SUCCESS(rc))
|
---|
939 | rc = RTErrConvertFromDarwinCOM(hrc);
|
---|
940 | }
|
---|
941 | else if (RT_SUCCESS(rc))
|
---|
942 | rc = RTErrConvertFromDarwin(krc);
|
---|
943 | if (!fMakeTheBestOfIt)
|
---|
944 | {
|
---|
945 | usbProxyDarwinReleaseAllInterfaces(pDevOsX);
|
---|
946 | break;
|
---|
947 | }
|
---|
948 | } /* iterate */
|
---|
949 | IOObjectRelease(Interfaces);
|
---|
950 | }
|
---|
951 | else if (irc == kIOReturnNoDevice)
|
---|
952 | rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
953 | else
|
---|
954 | {
|
---|
955 | AssertMsgFailed(("%#x\n", irc));
|
---|
956 | rc = VERR_GENERAL_FAILURE;
|
---|
957 | }
|
---|
958 |
|
---|
959 | RTCritSectLeave(&pDevOsX->CritSect);
|
---|
960 | return rc;
|
---|
961 | }
|
---|
962 |
|
---|
963 |
|
---|
964 | /**
|
---|
965 | * Find a particular interface.
|
---|
966 | *
|
---|
967 | * @returns The requested interface or NULL if not found.
|
---|
968 | * @param pDevOsX The darwin proxy device.
|
---|
969 | * @param u8Interface The interface number.
|
---|
970 | */
|
---|
971 | static PUSBPROXYIFOSX usbProxyDarwinGetInterface(PUSBPROXYDEVOSX pDevOsX, uint8_t u8Interface)
|
---|
972 | {
|
---|
973 | if (!pDevOsX->pIfHead)
|
---|
974 | usbProxyDarwinSeizeAllInterfaces(pDevOsX, true /* make the best out of it */);
|
---|
975 |
|
---|
976 | PUSBPROXYIFOSX pIf;
|
---|
977 | for (pIf = pDevOsX->pIfHead; pIf; pIf = pIf->pNext)
|
---|
978 | if (pIf->u8Interface == u8Interface)
|
---|
979 | return pIf;
|
---|
980 |
|
---|
981 | /* AssertMsgFailed(("Cannot find If#=%d\n", u8Interface)); - the 3rd quickcam interface is capture by the ****ing audio crap. */
|
---|
982 | return NULL;
|
---|
983 | }
|
---|
984 |
|
---|
985 |
|
---|
986 | /**
|
---|
987 | * Find a particular endpoint.
|
---|
988 | *
|
---|
989 | * @returns The requested interface or NULL if not found.
|
---|
990 | * @param pDevOsX The darwin proxy device.
|
---|
991 | * @param u8Endpoint The endpoint.
|
---|
992 | * @param pu8PipeRef Where to store the darwin pipe ref.
|
---|
993 | * @param ppPipe Where to store the darwin pipe pointer. (optional)
|
---|
994 | */
|
---|
995 | static PUSBPROXYIFOSX usbProxyDarwinGetInterfaceForEndpoint(PUSBPROXYDEVOSX pDevOsX, uint8_t u8Endpoint,
|
---|
996 | uint8_t *pu8PipeRef, PPUSBPROXYPIPEOSX ppPipe)
|
---|
997 | {
|
---|
998 | if (!pDevOsX->pIfHead)
|
---|
999 | usbProxyDarwinSeizeAllInterfaces(pDevOsX, true /* make the best out of it */);
|
---|
1000 |
|
---|
1001 | PUSBPROXYIFOSX pIf;
|
---|
1002 | for (pIf = pDevOsX->pIfHead; pIf; pIf = pIf->pNext)
|
---|
1003 | {
|
---|
1004 | unsigned i = pIf->cPipes;
|
---|
1005 | while (i-- > 0)
|
---|
1006 | if (pIf->aPipes[i].u8Endpoint == u8Endpoint)
|
---|
1007 | {
|
---|
1008 | *pu8PipeRef = pIf->aPipes[i].u8PipeRef;
|
---|
1009 | if (ppPipe)
|
---|
1010 | *ppPipe = &pIf->aPipes[i];
|
---|
1011 | return pIf;
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | AssertMsgFailed(("Cannot find EndPt=%#x\n", u8Endpoint));
|
---|
1016 | return NULL;
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 |
|
---|
1020 | /**
|
---|
1021 | * Gets an unsigned 32-bit integer value.
|
---|
1022 | *
|
---|
1023 | * @returns Success indicator (true/false).
|
---|
1024 | * @param DictRef The dictionary.
|
---|
1025 | * @param KeyStrRef The key name.
|
---|
1026 | * @param pu32 Where to store the key value.
|
---|
1027 | */
|
---|
1028 | static bool usbProxyDarwinDictGetU32(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, uint32_t *pu32)
|
---|
1029 | {
|
---|
1030 | CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
1031 | if (ValRef)
|
---|
1032 | {
|
---|
1033 | if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt32Type, pu32))
|
---|
1034 | return true;
|
---|
1035 | }
|
---|
1036 | *pu32 = 0;
|
---|
1037 | return false;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 |
|
---|
1041 | /**
|
---|
1042 | * Gets an unsigned 64-bit integer value.
|
---|
1043 | *
|
---|
1044 | * @returns Success indicator (true/false).
|
---|
1045 | * @param DictRef The dictionary.
|
---|
1046 | * @param KeyStrRef The key name.
|
---|
1047 | * @param pu64 Where to store the key value.
|
---|
1048 | */
|
---|
1049 | static bool usbProxyDarwinDictGetU64(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, uint64_t *pu64)
|
---|
1050 | {
|
---|
1051 | CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
|
---|
1052 | if (ValRef)
|
---|
1053 | {
|
---|
1054 | if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt64Type, pu64))
|
---|
1055 | return true;
|
---|
1056 | }
|
---|
1057 | *pu64 = 0;
|
---|
1058 | return false;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 |
|
---|
1062 | static DECLCALLBACK(void) usbProxyDarwinPerformWakeup(void *pInfo)
|
---|
1063 | {
|
---|
1064 | return;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 |
|
---|
1068 | /* -=-=-=-=-=- The exported methods -=-=-=-=-=- */
|
---|
1069 |
|
---|
1070 |
|
---|
1071 | /**
|
---|
1072 | * Opens the USB Device.
|
---|
1073 | *
|
---|
1074 | * @returns VBox status code.
|
---|
1075 | * @param pProxyDev The device instance.
|
---|
1076 | * @param pszAddress The session id and/or location id of the device to open.
|
---|
1077 | * The format of this string is something iokit.c in Main defines, currently
|
---|
1078 | * it's sequences of "[l|s]=<value>" separated by ";".
|
---|
1079 | * @param pvBackend Backend specific pointer, unused for the Darwin backend.
|
---|
1080 | */
|
---|
1081 | static DECLCALLBACK(int) usbProxyDarwinOpen(PUSBPROXYDEV pProxyDev, const char *pszAddress, void *pvBackend)
|
---|
1082 | {
|
---|
1083 | int vrc;
|
---|
1084 | LogFlow(("usbProxyDarwinOpen: pProxyDev=%p pszAddress=%s\n", pProxyDev, pszAddress));
|
---|
1085 |
|
---|
1086 | /*
|
---|
1087 | * Init globals once.
|
---|
1088 | */
|
---|
1089 | vrc = RTOnce(&g_usbProxyDarwinOnce, usbProxyDarwinInitOnce, NULL);
|
---|
1090 | AssertRCReturn(vrc, vrc);
|
---|
1091 |
|
---|
1092 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1093 |
|
---|
1094 | /*
|
---|
1095 | * The idea here was to create a matching directory with the sessionID
|
---|
1096 | * and locationID included, however this doesn't seem to work. So, we'll
|
---|
1097 | * use the product id and vendor id to limit the set of matching device
|
---|
1098 | * and manually match these two properties. sigh.
|
---|
1099 | * (Btw. vendor and product id must be used *together* apparently.)
|
---|
1100 | *
|
---|
1101 | * Wonder if we could use the entry path? Docs indicates says we must
|
---|
1102 | * use IOServiceGetMatchingServices and I'm not in a mood to explore
|
---|
1103 | * this subject further right now. Maybe check this later.
|
---|
1104 | */
|
---|
1105 | CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOUSBDeviceClassName);
|
---|
1106 | AssertReturn(RefMatchingDict, NULL);
|
---|
1107 |
|
---|
1108 | uint64_t u64SessionId = 0;
|
---|
1109 | uint32_t u32LocationId = 0;
|
---|
1110 | const char *psz = pszAddress;
|
---|
1111 | do
|
---|
1112 | {
|
---|
1113 | const char chValue = *psz;
|
---|
1114 | AssertReleaseReturn(psz[1] == '=', VERR_INTERNAL_ERROR);
|
---|
1115 | uint64_t u64Value;
|
---|
1116 | int rc = RTStrToUInt64Ex(psz + 2, (char **)&psz, 0, &u64Value);
|
---|
1117 | AssertReleaseRCReturn(rc, rc);
|
---|
1118 | AssertReleaseReturn(!*psz || *psz == ';', rc);
|
---|
1119 | switch (chValue)
|
---|
1120 | {
|
---|
1121 | case 'l':
|
---|
1122 | u32LocationId = (uint32_t)u64Value;
|
---|
1123 | break;
|
---|
1124 | case 's':
|
---|
1125 | u64SessionId = u64Value;
|
---|
1126 | break;
|
---|
1127 | case 'p':
|
---|
1128 | case 'v':
|
---|
1129 | {
|
---|
1130 | #if 0 /* Guess what, this doesn't 'ing work either! */
|
---|
1131 | SInt32 i32 = (int16_t)u64Value;
|
---|
1132 | CFNumberRef Num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i32);
|
---|
1133 | AssertBreak(Num);
|
---|
1134 | CFDictionarySetValue(RefMatchingDict, chValue == 'p' ? CFSTR(kUSBProductID) : CFSTR(kUSBVendorID), Num);
|
---|
1135 | CFRelease(Num);
|
---|
1136 | #endif
|
---|
1137 | break;
|
---|
1138 | }
|
---|
1139 | default:
|
---|
1140 | AssertReleaseMsgFailedReturn(("chValue=%#x\n", chValue), VERR_INTERNAL_ERROR);
|
---|
1141 | }
|
---|
1142 | if (*psz == ';')
|
---|
1143 | psz++;
|
---|
1144 | } while (*psz);
|
---|
1145 |
|
---|
1146 | io_iterator_t USBDevices = NULL;
|
---|
1147 | IOReturn irc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &USBDevices);
|
---|
1148 | AssertMsgReturn(irc == kIOReturnSuccess, ("irc=%#x\n", irc), NULL);
|
---|
1149 | RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
|
---|
1150 |
|
---|
1151 | unsigned cMatches = 0;
|
---|
1152 | io_object_t USBDevice;
|
---|
1153 | while ((USBDevice = IOIteratorNext(USBDevices)))
|
---|
1154 | {
|
---|
1155 | cMatches++;
|
---|
1156 | CFMutableDictionaryRef PropsRef = 0;
|
---|
1157 | kern_return_t krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
|
---|
1158 | if (krc == KERN_SUCCESS)
|
---|
1159 | {
|
---|
1160 | uint64_t u64CurSessionId;
|
---|
1161 | uint32_t u32CurLocationId;
|
---|
1162 | if ( ( !u64SessionId
|
---|
1163 | || ( usbProxyDarwinDictGetU64(PropsRef, CFSTR("sessionID"), &u64CurSessionId)
|
---|
1164 | && u64CurSessionId == u64SessionId))
|
---|
1165 | && ( !u32LocationId
|
---|
1166 | || ( usbProxyDarwinDictGetU32(PropsRef, CFSTR(kUSBDevicePropertyLocationID), &u32CurLocationId)
|
---|
1167 | && u32CurLocationId == u32LocationId))
|
---|
1168 | )
|
---|
1169 | {
|
---|
1170 | CFRelease(PropsRef);
|
---|
1171 | break;
|
---|
1172 | }
|
---|
1173 | CFRelease(PropsRef);
|
---|
1174 | }
|
---|
1175 | IOObjectRelease(USBDevice);
|
---|
1176 | }
|
---|
1177 | IOObjectRelease(USBDevices);
|
---|
1178 | USBDevices = NULL;
|
---|
1179 | if (!USBDevice)
|
---|
1180 | {
|
---|
1181 | LogRel(("USB: Device '%s' not found (%d pid+vid matches)\n", pszAddress, cMatches));
|
---|
1182 | IOObjectRelease(USBDevices);
|
---|
1183 | return VERR_VUSB_DEVICE_NAME_NOT_FOUND;
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | /*
|
---|
1187 | * Call the USBLib init to make sure we're a valid VBoxUSB client.
|
---|
1188 | * For now we'll ignore failures here and just plunge on, it might still work...
|
---|
1189 | */
|
---|
1190 | vrc = USBLibInit();
|
---|
1191 | if (RT_FAILURE(vrc))
|
---|
1192 | LogRel(("USB: USBLibInit failed - %Rrc\n", vrc));
|
---|
1193 |
|
---|
1194 | /*
|
---|
1195 | * Create a plugin interface for the device and query its IOUSBDeviceInterface.
|
---|
1196 | */
|
---|
1197 | SInt32 Score = 0;
|
---|
1198 | IOCFPlugInInterface **ppPlugInInterface = NULL;
|
---|
1199 | irc = IOCreatePlugInInterfaceForService(USBDevice, kIOUSBDeviceUserClientTypeID,
|
---|
1200 | kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
|
---|
1201 | if (irc == kIOReturnSuccess)
|
---|
1202 | {
|
---|
1203 | IOUSBDeviceInterface245 **ppDevI = NULL;
|
---|
1204 | HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
|
---|
1205 | CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245),
|
---|
1206 | (LPVOID *)&ppDevI);
|
---|
1207 | irc = IODestroyPlugInInterface(ppPlugInInterface); Assert(irc == kIOReturnSuccess);
|
---|
1208 | ppPlugInInterface = NULL;
|
---|
1209 | if (hrc == S_OK)
|
---|
1210 | {
|
---|
1211 | /*
|
---|
1212 | * Try open the device for exclusive access.
|
---|
1213 | * If we fail, we'll try figure out who is using the device and
|
---|
1214 | * convince them to let go of it...
|
---|
1215 | */
|
---|
1216 | irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
|
---|
1217 | if (irc == kIOReturnExclusiveAccess)
|
---|
1218 | {
|
---|
1219 | RTThreadSleep(20);
|
---|
1220 | irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
|
---|
1221 | }
|
---|
1222 | if (irc == kIOReturnSuccess)
|
---|
1223 | {
|
---|
1224 | /*
|
---|
1225 | * Init a proxy device instance.
|
---|
1226 | */
|
---|
1227 | RTListInit((PRTLISTNODE)&pDevOsX->HeadOfRunLoopLst);
|
---|
1228 | vrc = RTCritSectInit(&pDevOsX->CritSect);
|
---|
1229 | if (RT_SUCCESS(vrc))
|
---|
1230 | {
|
---|
1231 | pDevOsX->USBDevice = USBDevice;
|
---|
1232 | pDevOsX->ppDevI = ppDevI;
|
---|
1233 | pDevOsX->pProxyDev = pProxyDev;
|
---|
1234 | pDevOsX->pTaxingHead = NULL;
|
---|
1235 | pDevOsX->pTaxingTail = NULL;
|
---|
1236 | pDevOsX->hRunLoopReapingLast = NULL;
|
---|
1237 |
|
---|
1238 | /*
|
---|
1239 | * Try seize all the interface.
|
---|
1240 | */
|
---|
1241 | char *pszDummyName = pProxyDev->pUsbIns->pszName;
|
---|
1242 | pProxyDev->pUsbIns->pszName = (char *)pszAddress;
|
---|
1243 | vrc = usbProxyDarwinSeizeAllInterfaces(pDevOsX, false /* give up on failure */);
|
---|
1244 | pProxyDev->pUsbIns->pszName = pszDummyName;
|
---|
1245 | if (RT_SUCCESS(vrc))
|
---|
1246 | {
|
---|
1247 | /*
|
---|
1248 | * Create the async event source and add it to the run loop.
|
---|
1249 | */
|
---|
1250 | irc = (*ppDevI)->CreateDeviceAsyncEventSource(ppDevI, &pDevOsX->RunLoopSrcRef);
|
---|
1251 | if (irc == kIOReturnSuccess)
|
---|
1252 | {
|
---|
1253 | /*
|
---|
1254 | * Determine the active configuration.
|
---|
1255 | * Can cause hangs, so drop it for now.
|
---|
1256 | */
|
---|
1257 | /** @todo test Palm. */
|
---|
1258 | //uint8_t u8Cfg;
|
---|
1259 | //irc = (*ppDevI)->GetConfiguration(ppDevI, &u8Cfg);
|
---|
1260 | if (irc != kIOReturnNoDevice)
|
---|
1261 | {
|
---|
1262 | CFRunLoopSourceContext CtxRunLoopSource;
|
---|
1263 | CtxRunLoopSource.version = 0;
|
---|
1264 | CtxRunLoopSource.info = NULL;
|
---|
1265 | CtxRunLoopSource.retain = NULL;
|
---|
1266 | CtxRunLoopSource.release = NULL;
|
---|
1267 | CtxRunLoopSource.copyDescription = NULL;
|
---|
1268 | CtxRunLoopSource.equal = NULL;
|
---|
1269 | CtxRunLoopSource.hash = NULL;
|
---|
1270 | CtxRunLoopSource.schedule = NULL;
|
---|
1271 | CtxRunLoopSource.cancel = NULL;
|
---|
1272 | CtxRunLoopSource.perform = usbProxyDarwinPerformWakeup;
|
---|
1273 | pDevOsX->hRunLoopSrcWakeRef = CFRunLoopSourceCreate(NULL, 0, &CtxRunLoopSource);
|
---|
1274 | if (CFRunLoopSourceIsValid(pDevOsX->hRunLoopSrcWakeRef))
|
---|
1275 | {
|
---|
1276 | //pProxyDev->iActiveCfg = irc == kIOReturnSuccess ? u8Cfg : -1;
|
---|
1277 | RTListInit(&pDevOsX->HeadOfRunLoopWakeLst);
|
---|
1278 | pProxyDev->iActiveCfg = -1;
|
---|
1279 | pProxyDev->cIgnoreSetConfigs = 1;
|
---|
1280 |
|
---|
1281 | usbProxyDarwinAddRunLoopRef(&pDevOsX->HeadOfRunLoopLst, pDevOsX->RunLoopSrcRef);
|
---|
1282 | return VINF_SUCCESS; /* return */
|
---|
1283 | }
|
---|
1284 | else
|
---|
1285 | {
|
---|
1286 | LogRel(("USB: Device '%s' out of memory allocating runloop source\n", pszAddress));
|
---|
1287 | vrc = VERR_NO_MEMORY;
|
---|
1288 | }
|
---|
1289 | }
|
---|
1290 | vrc = VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
1291 | }
|
---|
1292 | else
|
---|
1293 | vrc = RTErrConvertFromDarwin(irc);
|
---|
1294 |
|
---|
1295 | usbProxyDarwinReleaseAllInterfaces(pDevOsX);
|
---|
1296 | }
|
---|
1297 | /* else: already bitched */
|
---|
1298 |
|
---|
1299 | RTCritSectDelete(&pDevOsX->CritSect);
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | irc = (*ppDevI)->USBDeviceClose(ppDevI);
|
---|
1303 | AssertMsg(irc == kIOReturnSuccess, ("%#x\n", irc));
|
---|
1304 | }
|
---|
1305 | else if (irc == kIOReturnExclusiveAccess)
|
---|
1306 | {
|
---|
1307 | LogRel(("USB: Device '%s' is being used by another process\n", pszAddress));
|
---|
1308 | vrc = VERR_SHARING_VIOLATION;
|
---|
1309 | }
|
---|
1310 | else
|
---|
1311 | {
|
---|
1312 | LogRel(("USB: Failed to open device '%s', irc=%#x.\n", pszAddress, irc));
|
---|
1313 | vrc = VERR_OPEN_FAILED;
|
---|
1314 | }
|
---|
1315 | }
|
---|
1316 | else
|
---|
1317 | {
|
---|
1318 | LogRel(("USB: Failed to create plugin interface for device '%s', hrc=%#x.\n", pszAddress, hrc));
|
---|
1319 | vrc = VERR_OPEN_FAILED;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | (*ppDevI)->Release(ppDevI);
|
---|
1323 | }
|
---|
1324 | else
|
---|
1325 | {
|
---|
1326 | LogRel(("USB: Failed to open device '%s', plug-in creation failed with irc=%#x.\n", pszAddress, irc));
|
---|
1327 | vrc = RTErrConvertFromDarwin(irc);
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | USBLibTerm();
|
---|
1331 | return vrc;
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 |
|
---|
1335 | /**
|
---|
1336 | * Closes the proxy device.
|
---|
1337 | */
|
---|
1338 | static DECLCALLBACK(void) usbProxyDarwinClose(PUSBPROXYDEV pProxyDev)
|
---|
1339 | {
|
---|
1340 | LogFlow(("usbProxyDarwinClose: pProxyDev=%s\n", pProxyDev->pUsbIns->pszName));
|
---|
1341 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1342 | AssertPtrReturnVoid(pDevOsX);
|
---|
1343 |
|
---|
1344 | /*
|
---|
1345 | * Release interfaces we've laid claim to, then reset the device
|
---|
1346 | * and finally close it.
|
---|
1347 | */
|
---|
1348 | RTCritSectEnter(&pDevOsX->CritSect);
|
---|
1349 | /* ?? */
|
---|
1350 | RTCritSectLeave(&pDevOsX->CritSect);
|
---|
1351 |
|
---|
1352 | usbProxyDarwinReleaseAllInterfaces(pDevOsX);
|
---|
1353 |
|
---|
1354 | if (pDevOsX->RunLoopSrcRef)
|
---|
1355 | {
|
---|
1356 | int rc = usbProxyDarwinRemoveSourceRefFromAllRunLoops(&pDevOsX->HeadOfRunLoopLst, pDevOsX->RunLoopSrcRef);
|
---|
1357 | AssertRC(rc);
|
---|
1358 |
|
---|
1359 | RTListInit((PRTLISTNODE)&pDevOsX->HeadOfRunLoopLst);
|
---|
1360 |
|
---|
1361 | CFRelease(pDevOsX->RunLoopSrcRef);
|
---|
1362 | pDevOsX->RunLoopSrcRef = NULL;
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | if (pDevOsX->hRunLoopSrcWakeRef)
|
---|
1366 | {
|
---|
1367 | int rc = usbProxyDarwinRemoveSourceRefFromAllRunLoops(&pDevOsX->HeadOfRunLoopWakeLst, pDevOsX->hRunLoopSrcWakeRef);
|
---|
1368 | AssertRC(rc);
|
---|
1369 |
|
---|
1370 | RTListInit((PRTLISTNODE)&pDevOsX->HeadOfRunLoopWakeLst);
|
---|
1371 |
|
---|
1372 | CFRelease(pDevOsX->hRunLoopSrcWakeRef);
|
---|
1373 | pDevOsX->hRunLoopSrcWakeRef = NULL;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | IOReturn irc = (*pDevOsX->ppDevI)->ResetDevice(pDevOsX->ppDevI);
|
---|
1377 |
|
---|
1378 | irc = (*pDevOsX->ppDevI)->USBDeviceClose(pDevOsX->ppDevI);
|
---|
1379 | if (irc != kIOReturnSuccess && irc != kIOReturnNoDevice)
|
---|
1380 | {
|
---|
1381 | LogRel(("USB: USBDeviceClose -> %#x\n", irc));
|
---|
1382 | AssertMsgFailed(("irc=%#x\n", irc));
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | (*pDevOsX->ppDevI)->Release(pDevOsX->ppDevI);
|
---|
1386 | pDevOsX->ppDevI = NULL;
|
---|
1387 | kern_return_t krc = IOObjectRelease(pDevOsX->USBDevice); Assert(krc == KERN_SUCCESS); NOREF(krc);
|
---|
1388 | pDevOsX->USBDevice = NULL;
|
---|
1389 | pDevOsX->pProxyDev = NULL;
|
---|
1390 |
|
---|
1391 | /*
|
---|
1392 | * Free all the resources.
|
---|
1393 | */
|
---|
1394 | RTCritSectDelete(&pDevOsX->CritSect);
|
---|
1395 |
|
---|
1396 | PUSBPROXYURBOSX pUrbOsX;
|
---|
1397 | while ((pUrbOsX = pDevOsX->pFreeHead) != NULL)
|
---|
1398 | {
|
---|
1399 | pDevOsX->pFreeHead = pUrbOsX->pNext;
|
---|
1400 | RTMemFree(pUrbOsX);
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | USBLibTerm();
|
---|
1404 | LogFlow(("usbProxyDarwinClose: returns\n"));
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 |
|
---|
1408 | /**
|
---|
1409 | * Reset a device.
|
---|
1410 | *
|
---|
1411 | * @returns VBox status code.
|
---|
1412 | * @param pDev The device to reset.
|
---|
1413 | */
|
---|
1414 | static DECLCALLBACK(int) usbProxyDarwinReset(PUSBPROXYDEV pProxyDev, bool fResetOnLinux)
|
---|
1415 | {
|
---|
1416 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1417 | LogFlow(("usbProxyDarwinReset: pProxyDev=%s\n", pProxyDev->pUsbIns->pszName));
|
---|
1418 |
|
---|
1419 | IOReturn irc = (*pDevOsX->ppDevI)->ResetDevice(pDevOsX->ppDevI);
|
---|
1420 | int rc;
|
---|
1421 | if (irc == kIOReturnSuccess)
|
---|
1422 | {
|
---|
1423 | /** @todo Some docs say that some drivers will do a default config, check this out ... */
|
---|
1424 | pProxyDev->cIgnoreSetConfigs = 0;
|
---|
1425 | pProxyDev->iActiveCfg = -1;
|
---|
1426 |
|
---|
1427 | rc = VINF_SUCCESS;
|
---|
1428 | }
|
---|
1429 | else if (irc == kIOReturnNoDevice)
|
---|
1430 | rc = VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
1431 | else
|
---|
1432 | {
|
---|
1433 | AssertMsgFailed(("irc=%#x\n", irc));
|
---|
1434 | rc = VERR_GENERAL_FAILURE;
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | LogFlow(("usbProxyDarwinReset: returns success %Rrc\n", rc));
|
---|
1438 | return rc;
|
---|
1439 | }
|
---|
1440 |
|
---|
1441 |
|
---|
1442 | /**
|
---|
1443 | * SET_CONFIGURATION.
|
---|
1444 | *
|
---|
1445 | * The caller makes sure that it's not called first time after open or reset
|
---|
1446 | * with the active interface.
|
---|
1447 | *
|
---|
1448 | * @returns success indicator.
|
---|
1449 | * @param pProxyDev The device instance data.
|
---|
1450 | * @param iCfg The configuration to set.
|
---|
1451 | */
|
---|
1452 | static DECLCALLBACK(int) usbProxyDarwinSetConfig(PUSBPROXYDEV pProxyDev, int iCfg)
|
---|
1453 | {
|
---|
1454 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1455 | LogFlow(("usbProxyDarwinSetConfig: pProxyDev=%s cfg=%#x\n",
|
---|
1456 | pProxyDev->pUsbIns->pszName, iCfg));
|
---|
1457 |
|
---|
1458 | IOReturn irc = (*pDevOsX->ppDevI)->SetConfiguration(pDevOsX->ppDevI, (uint8_t)iCfg);
|
---|
1459 | if (irc != kIOReturnSuccess)
|
---|
1460 | {
|
---|
1461 | Log(("usbProxyDarwinSetConfig: Set configuration -> %#x\n", irc));
|
---|
1462 | return RTErrConvertFromDarwin(irc);
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 | usbProxyDarwinReleaseAllInterfaces(pDevOsX);
|
---|
1466 | usbProxyDarwinSeizeAllInterfaces(pDevOsX, true /* make the best out of it */);
|
---|
1467 | return VINF_SUCCESS;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 |
|
---|
1471 | /**
|
---|
1472 | * Claims an interface.
|
---|
1473 | *
|
---|
1474 | * This is a stub on Darwin since we release/claim all interfaces at
|
---|
1475 | * open/reset/setconfig time.
|
---|
1476 | *
|
---|
1477 | * @returns success indicator (always VINF_SUCCESS).
|
---|
1478 | */
|
---|
1479 | static DECLCALLBACK(int) usbProxyDarwinClaimInterface(PUSBPROXYDEV pProxyDev, int iIf)
|
---|
1480 | {
|
---|
1481 | return VINF_SUCCESS;
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 |
|
---|
1485 | /**
|
---|
1486 | * Releases an interface.
|
---|
1487 | *
|
---|
1488 | * This is a stub on Darwin since we release/claim all interfaces at
|
---|
1489 | * open/reset/setconfig time.
|
---|
1490 | *
|
---|
1491 | * @returns success indicator.
|
---|
1492 | */
|
---|
1493 | static DECLCALLBACK(int) usbProxyDarwinReleaseInterface(PUSBPROXYDEV pProxyDev, int iIf)
|
---|
1494 | {
|
---|
1495 | return VINF_SUCCESS;
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 |
|
---|
1499 | /**
|
---|
1500 | * SET_INTERFACE.
|
---|
1501 | *
|
---|
1502 | * @returns success indicator.
|
---|
1503 | */
|
---|
1504 | static DECLCALLBACK(int) usbProxyDarwinSetInterface(PUSBPROXYDEV pProxyDev, int iIf, int iAlt)
|
---|
1505 | {
|
---|
1506 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1507 | IOReturn irc = kIOReturnSuccess;
|
---|
1508 | PUSBPROXYIFOSX pIf = usbProxyDarwinGetInterface(pDevOsX, iIf);
|
---|
1509 | LogFlow(("usbProxyDarwinSetInterface: pProxyDev=%s iIf=%#x iAlt=%#x iCurAlt=%#x\n",
|
---|
1510 | pProxyDev->pUsbIns->pszName, iIf, iAlt, pIf ? pIf->u8AltSetting : 0xbeef));
|
---|
1511 | if (pIf)
|
---|
1512 | {
|
---|
1513 | /* Avoid SetAlternateInterface when possible as it will recreate the pipes. */
|
---|
1514 | if (iAlt != pIf->u8AltSetting)
|
---|
1515 | {
|
---|
1516 | irc = (*pIf->ppIfI)->SetAlternateInterface(pIf->ppIfI, iAlt);
|
---|
1517 | if (irc == kIOReturnSuccess)
|
---|
1518 | {
|
---|
1519 | usbProxyDarwinGetPipeProperties(pDevOsX, pIf);
|
---|
1520 | return VINF_SUCCESS;
|
---|
1521 | }
|
---|
1522 | }
|
---|
1523 | else
|
---|
1524 | {
|
---|
1525 | /*
|
---|
1526 | * Just send the request anyway?
|
---|
1527 | */
|
---|
1528 | IOUSBDevRequest Req;
|
---|
1529 | Req.bmRequestType = 0x01;
|
---|
1530 | Req.bRequest = 0x0b; /* SET_INTERFACE */
|
---|
1531 | Req.wIndex = iIf;
|
---|
1532 | Req.wValue = iAlt;
|
---|
1533 | Req.wLength = 0;
|
---|
1534 | Req.wLenDone = 0;
|
---|
1535 | Req.pData = NULL;
|
---|
1536 | irc = (*pDevOsX->ppDevI)->DeviceRequest(pDevOsX->ppDevI, &Req);
|
---|
1537 | Log(("usbProxyDarwinSetInterface: SET_INTERFACE(%d,%d) -> irc=%#x\n", iIf, iAlt, irc));
|
---|
1538 | return VINF_SUCCESS;
|
---|
1539 | }
|
---|
1540 | }
|
---|
1541 |
|
---|
1542 | LogFlow(("usbProxyDarwinSetInterface: pProxyDev=%s eiIf=%#x iAlt=%#x - failure - pIf=%p irc=%#x\n",
|
---|
1543 | pProxyDev->pUsbIns->pszName, iIf, iAlt, pIf, irc));
|
---|
1544 | return RTErrConvertFromDarwin(irc);
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 |
|
---|
1548 | /**
|
---|
1549 | * Clears the halted endpoint 'EndPt'.
|
---|
1550 | */
|
---|
1551 | static DECLCALLBACK(int) usbProxyDarwinClearHaltedEp(PUSBPROXYDEV pProxyDev, unsigned int EndPt)
|
---|
1552 | {
|
---|
1553 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1554 | LogFlow(("usbProxyDarwinClearHaltedEp: pProxyDev=%s EndPt=%#x\n", pProxyDev->pUsbIns->pszName, EndPt));
|
---|
1555 |
|
---|
1556 | /*
|
---|
1557 | * Clearing the zero control pipe doesn't make sense and isn't
|
---|
1558 | * supported by the API. Just ignore it.
|
---|
1559 | */
|
---|
1560 | if (EndPt == 0)
|
---|
1561 | return VINF_SUCCESS;
|
---|
1562 |
|
---|
1563 | /*
|
---|
1564 | * Find the interface/pipe combination and invoke the ClearPipeStallBothEnds
|
---|
1565 | * method. (The ResetPipe and ClearPipeStall methods will not send the
|
---|
1566 | * CLEAR_FEATURE(ENDPOINT_HALT) request that this method implements.)
|
---|
1567 | */
|
---|
1568 | IOReturn irc = kIOReturnSuccess;
|
---|
1569 | uint8_t u8PipeRef;
|
---|
1570 | PUSBPROXYIFOSX pIf = usbProxyDarwinGetInterfaceForEndpoint(pDevOsX, EndPt, &u8PipeRef, NULL);
|
---|
1571 | if (pIf)
|
---|
1572 | {
|
---|
1573 | irc = (*pIf->ppIfI)->ClearPipeStallBothEnds(pIf->ppIfI, u8PipeRef);
|
---|
1574 | if (irc == kIOReturnSuccess)
|
---|
1575 | return VINF_SUCCESS;
|
---|
1576 | AssertMsg(irc == kIOReturnNoDevice || irc == kIOReturnNotResponding, ("irc=#x (control pipe?)\n", irc));
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | LogFlow(("usbProxyDarwinClearHaltedEp: pProxyDev=%s EndPt=%#x - failure - pIf=%p irc=%#x\n",
|
---|
1580 | pProxyDev->pUsbIns->pszName, EndPt, pIf, irc));
|
---|
1581 | return RTErrConvertFromDarwin(irc);
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 |
|
---|
1585 | /**
|
---|
1586 | * @copydoc USBPROXYBACK::pfnUrbQueue
|
---|
1587 | */
|
---|
1588 | static DECLCALLBACK(int) usbProxyDarwinUrbQueue(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
|
---|
1589 | {
|
---|
1590 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1591 | LogFlow(("%s: usbProxyDarwinUrbQueue: pProxyDev=%s pUrb=%p EndPt=%d cbData=%d\n",
|
---|
1592 | pUrb->pszDesc, pProxyDev->pUsbIns->pszName, pUrb, pUrb->EndPt, pUrb->cbData));
|
---|
1593 |
|
---|
1594 | /*
|
---|
1595 | * Find the target interface / pipe.
|
---|
1596 | */
|
---|
1597 | uint8_t u8PipeRef = 0xff;
|
---|
1598 | PUSBPROXYIFOSX pIf = NULL;
|
---|
1599 | PUSBPROXYPIPEOSX pPipe = NULL;
|
---|
1600 | if (pUrb->EndPt)
|
---|
1601 | {
|
---|
1602 | /* Make sure the interface is there. */
|
---|
1603 | const uint8_t EndPt = pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? 0x80 : 0);
|
---|
1604 | pIf = usbProxyDarwinGetInterfaceForEndpoint(pDevOsX, EndPt, &u8PipeRef, &pPipe);
|
---|
1605 | if (!pIf)
|
---|
1606 | {
|
---|
1607 | LogFlow(("%s: usbProxyDarwinUrbQueue: pProxyDev=%s EndPt=%d cbData=%d - can't find interface / pipe!!!\n",
|
---|
1608 | pUrb->pszDesc, pProxyDev->pUsbIns->pszName, pUrb->EndPt, pUrb->cbData));
|
---|
1609 | return VERR_NOT_FOUND;
|
---|
1610 | }
|
---|
1611 | }
|
---|
1612 | /* else: pIf == NULL -> default control pipe.*/
|
---|
1613 |
|
---|
1614 | /*
|
---|
1615 | * Allocate a Darwin urb.
|
---|
1616 | */
|
---|
1617 | PUSBPROXYURBOSX pUrbOsX = usbProxyDarwinUrbAlloc(pDevOsX);
|
---|
1618 | if (!pUrbOsX)
|
---|
1619 | return VERR_NO_MEMORY;
|
---|
1620 |
|
---|
1621 | pUrbOsX->u64SubmitTS = RTTimeMilliTS();
|
---|
1622 | pUrbOsX->pVUsbUrb = pUrb;
|
---|
1623 | pUrbOsX->pDevOsX = pDevOsX;
|
---|
1624 | pUrbOsX->enmType = pUrb->enmType;
|
---|
1625 |
|
---|
1626 | /*
|
---|
1627 | * Submit the request.
|
---|
1628 | */
|
---|
1629 | IOReturn irc = kIOReturnError;
|
---|
1630 | switch (pUrb->enmType)
|
---|
1631 | {
|
---|
1632 | case VUSBXFERTYPE_MSG:
|
---|
1633 | {
|
---|
1634 | AssertMsgBreak(pUrb->cbData >= sizeof(VUSBSETUP), ("cbData=%d\n", pUrb->cbData));
|
---|
1635 | PVUSBSETUP pSetup = (PVUSBSETUP)&pUrb->abData[0];
|
---|
1636 | pUrbOsX->u.ControlMsg.bmRequestType = pSetup->bmRequestType;
|
---|
1637 | pUrbOsX->u.ControlMsg.bRequest = pSetup->bRequest;
|
---|
1638 | pUrbOsX->u.ControlMsg.wValue = pSetup->wValue;
|
---|
1639 | pUrbOsX->u.ControlMsg.wIndex = pSetup->wIndex;
|
---|
1640 | pUrbOsX->u.ControlMsg.wLength = pSetup->wLength;
|
---|
1641 | pUrbOsX->u.ControlMsg.pData = pSetup + 1;
|
---|
1642 | pUrbOsX->u.ControlMsg.wLenDone = pSetup->wLength;
|
---|
1643 |
|
---|
1644 | if (pIf)
|
---|
1645 | irc = (*pIf->ppIfI)->ControlRequestAsync(pIf->ppIfI, u8PipeRef, &pUrbOsX->u.ControlMsg,
|
---|
1646 | usbProxyDarwinUrbAsyncComplete, pUrbOsX);
|
---|
1647 | else
|
---|
1648 | irc = (*pDevOsX->ppDevI)->DeviceRequestAsync(pDevOsX->ppDevI, &pUrbOsX->u.ControlMsg,
|
---|
1649 | usbProxyDarwinUrbAsyncComplete, pUrbOsX);
|
---|
1650 | break;
|
---|
1651 | }
|
---|
1652 |
|
---|
1653 | case VUSBXFERTYPE_BULK:
|
---|
1654 | case VUSBXFERTYPE_INTR:
|
---|
1655 | {
|
---|
1656 | AssertBreak(pIf);
|
---|
1657 | Assert(pUrb->enmDir == VUSBDIRECTION_IN || pUrb->enmDir == VUSBDIRECTION_OUT);
|
---|
1658 | if (pUrb->enmDir == VUSBDIRECTION_OUT)
|
---|
1659 | irc = (*pIf->ppIfI)->WritePipeAsync(pIf->ppIfI, u8PipeRef, pUrb->abData, pUrb->cbData,
|
---|
1660 | usbProxyDarwinUrbAsyncComplete, pUrbOsX);
|
---|
1661 | else
|
---|
1662 | irc = (*pIf->ppIfI)->ReadPipeAsync(pIf->ppIfI, u8PipeRef, pUrb->abData, pUrb->cbData,
|
---|
1663 | usbProxyDarwinUrbAsyncComplete, pUrbOsX);
|
---|
1664 |
|
---|
1665 | break;
|
---|
1666 | }
|
---|
1667 |
|
---|
1668 | case VUSBXFERTYPE_ISOC:
|
---|
1669 | {
|
---|
1670 | AssertBreak(pIf);
|
---|
1671 | Assert(pUrb->enmDir == VUSBDIRECTION_IN || pUrb->enmDir == VUSBDIRECTION_OUT);
|
---|
1672 |
|
---|
1673 | #ifdef USE_LOW_LATENCY_API
|
---|
1674 | /* Allocate an isochronous buffer and copy over the data. */
|
---|
1675 | AssertBreak(pUrb->cbData <= 8192);
|
---|
1676 | int rc = usbProxyDarwinUrbAllocIsocBuf(pUrbOsX, pIf);
|
---|
1677 | AssertRCBreak(rc);
|
---|
1678 | if (pUrb->enmDir == VUSBDIRECTION_OUT)
|
---|
1679 | memcpy(pUrbOsX->u.Isoc.pBuf->pvBuf, pUrb->abData, pUrb->cbData);
|
---|
1680 | else
|
---|
1681 | memset(pUrbOsX->u.Isoc.pBuf->pvBuf, 0xfe, pUrb->cbData);
|
---|
1682 | #endif
|
---|
1683 |
|
---|
1684 | /* Get the current frame number (+2) and make sure it doesn't
|
---|
1685 | overlap with the previous request. See WARNING in
|
---|
1686 | ApplUSBUHCI::CreateIsochTransfer for details on the +2. */
|
---|
1687 | UInt64 FrameNo;
|
---|
1688 | AbsoluteTime FrameTime;
|
---|
1689 | irc = (*pIf->ppIfI)->GetBusFrameNumber(pIf->ppIfI, &FrameNo, &FrameTime);
|
---|
1690 | AssertMsg(irc == kIOReturnSuccess, ("GetBusFrameNumber -> %#x\n", irc));
|
---|
1691 | FrameNo += 2;
|
---|
1692 | if (FrameNo <= pPipe->u64NextFrameNo)
|
---|
1693 | FrameNo = pPipe->u64NextFrameNo;
|
---|
1694 |
|
---|
1695 | for (unsigned j = 0; ; j++)
|
---|
1696 | {
|
---|
1697 | unsigned i;
|
---|
1698 | for (i = 0; i < pUrb->cIsocPkts; i++)
|
---|
1699 | {
|
---|
1700 | pUrbOsX->u.Isoc.aFrames[i].frReqCount = pUrb->aIsocPkts[i].cb;
|
---|
1701 | pUrbOsX->u.Isoc.aFrames[i].frActCount = 0;
|
---|
1702 | pUrbOsX->u.Isoc.aFrames[i].frStatus = kIOUSBNotSent1Err;
|
---|
1703 | #ifdef USE_LOW_LATENCY_API
|
---|
1704 | pUrbOsX->u.Isoc.aFrames[i].frTimeStamp.hi = 0;
|
---|
1705 | pUrbOsX->u.Isoc.aFrames[i].frTimeStamp.lo = 0;
|
---|
1706 | #endif
|
---|
1707 | }
|
---|
1708 | for (; i < RT_ELEMENTS(pUrbOsX->u.Isoc.aFrames); i++)
|
---|
1709 | {
|
---|
1710 | pUrbOsX->u.Isoc.aFrames[i].frReqCount = 0;
|
---|
1711 | pUrbOsX->u.Isoc.aFrames[i].frActCount = 0;
|
---|
1712 | pUrbOsX->u.Isoc.aFrames[i].frStatus = kIOReturnError;
|
---|
1713 | #ifdef USE_LOW_LATENCY_API
|
---|
1714 | pUrbOsX->u.Isoc.aFrames[i].frTimeStamp.hi = 0;
|
---|
1715 | pUrbOsX->u.Isoc.aFrames[i].frTimeStamp.lo = 0;
|
---|
1716 | #endif
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | #ifdef USE_LOW_LATENCY_API
|
---|
1720 | if (pUrb->enmDir == VUSBDIRECTION_OUT)
|
---|
1721 | irc = (*pIf->ppIfI)->LowLatencyWriteIsochPipeAsync(pIf->ppIfI, u8PipeRef,
|
---|
1722 | pUrbOsX->u.Isoc.pBuf->pvBuf, FrameNo, pUrb->cIsocPkts, 0, pUrbOsX->u.Isoc.aFrames,
|
---|
1723 | usbProxyDarwinUrbAsyncComplete, pUrbOsX);
|
---|
1724 | else
|
---|
1725 | irc = (*pIf->ppIfI)->LowLatencyReadIsochPipeAsync(pIf->ppIfI, u8PipeRef,
|
---|
1726 | pUrbOsX->u.Isoc.pBuf->pvBuf, FrameNo, pUrb->cIsocPkts, 0, pUrbOsX->u.Isoc.aFrames,
|
---|
1727 | usbProxyDarwinUrbAsyncComplete, pUrbOsX);
|
---|
1728 | #else
|
---|
1729 | if (pUrb->enmDir == VUSBDIRECTION_OUT)
|
---|
1730 | irc = (*pIf->ppIfI)->WriteIsochPipeAsync(pIf->ppIfI, u8PipeRef,
|
---|
1731 | pUrb->abData, FrameNo, pUrb->cIsocPkts, &pUrbOsX->u.Isoc.aFrames[0],
|
---|
1732 | usbProxyDarwinUrbAsyncComplete, pUrbOsX);
|
---|
1733 | else
|
---|
1734 | irc = (*pIf->ppIfI)->ReadIsochPipeAsync(pIf->ppIfI, u8PipeRef,
|
---|
1735 | pUrb->abData, FrameNo, pUrb->cIsocPkts, &pUrbOsX->u.Isoc.aFrames[0],
|
---|
1736 | usbProxyDarwinUrbAsyncComplete, pUrbOsX);
|
---|
1737 | #endif
|
---|
1738 | if ( irc != kIOReturnIsoTooOld
|
---|
1739 | || j >= 5)
|
---|
1740 | {
|
---|
1741 | Log(("%s: usbProxyDarwinUrbQueue: isoc: u64NextFrameNo=%RX64 FrameNo=%RX64 #Frames=%d j=%d (pipe=%d)\n",
|
---|
1742 | pUrb->pszDesc, pPipe->u64NextFrameNo, FrameNo, pUrb->cIsocPkts, j, u8PipeRef));
|
---|
1743 | if (irc == kIOReturnSuccess)
|
---|
1744 | pPipe->u64NextFrameNo = FrameNo + pUrb->cIsocPkts;
|
---|
1745 | break;
|
---|
1746 | }
|
---|
1747 |
|
---|
1748 | /* try again... */
|
---|
1749 | irc = (*pIf->ppIfI)->GetBusFrameNumber(pIf->ppIfI, &FrameNo, &FrameTime);
|
---|
1750 | if (FrameNo <= pPipe->u64NextFrameNo)
|
---|
1751 | FrameNo = pPipe->u64NextFrameNo;
|
---|
1752 | FrameNo += j;
|
---|
1753 | }
|
---|
1754 | break;
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 | default:
|
---|
1758 | AssertMsgFailed(("%s: enmType=%#x\n", pUrb->pszDesc, pUrb->enmType));
|
---|
1759 | break;
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 | /*
|
---|
1763 | * Success?
|
---|
1764 | */
|
---|
1765 | if (RT_LIKELY(irc == kIOReturnSuccess))
|
---|
1766 | {
|
---|
1767 | Log(("%s: usbProxyDarwinUrbQueue: success\n", pUrb->pszDesc));
|
---|
1768 | return VINF_SUCCESS;
|
---|
1769 | }
|
---|
1770 | switch (irc)
|
---|
1771 | {
|
---|
1772 | case kIOUSBPipeStalled:
|
---|
1773 | {
|
---|
1774 | /* Increment in flight counter because the completion handler will decrease it always. */
|
---|
1775 | usbProxyDarwinUrbAsyncComplete(pUrbOsX, kIOUSBPipeStalled, 0);
|
---|
1776 | Log(("%s: usbProxyDarwinUrbQueue: pProxyDev=%s EndPt=%d cbData=%d - failed irc=%#x! (stall)\n",
|
---|
1777 | pUrb->pszDesc, pProxyDev->pUsbIns->pszName, pUrb->EndPt, pUrb->cbData, irc));
|
---|
1778 | return VINF_SUCCESS;
|
---|
1779 | }
|
---|
1780 | }
|
---|
1781 |
|
---|
1782 | usbProxyDarwinUrbFree(pDevOsX, pUrbOsX);
|
---|
1783 | Log(("%s: usbProxyDarwinUrbQueue: pProxyDev=%s EndPt=%d cbData=%d - failed irc=%#x!\n",
|
---|
1784 | pUrb->pszDesc, pProxyDev->pUsbIns->pszName, pUrb->EndPt, pUrb->cbData, irc));
|
---|
1785 | return RTErrConvertFromDarwin(irc);
|
---|
1786 | }
|
---|
1787 |
|
---|
1788 |
|
---|
1789 | /**
|
---|
1790 | * Reap URBs in-flight on a device.
|
---|
1791 | *
|
---|
1792 | * @returns Pointer to a completed URB.
|
---|
1793 | * @returns NULL if no URB was completed.
|
---|
1794 | * @param pProxyDev The device.
|
---|
1795 | * @param cMillies Number of milliseconds to wait. Use 0 to not wait at all.
|
---|
1796 | */
|
---|
1797 | static DECLCALLBACK(PVUSBURB) usbProxyDarwinUrbReap(PUSBPROXYDEV pProxyDev, RTMSINTERVAL cMillies)
|
---|
1798 | {
|
---|
1799 | PVUSBURB pUrb = NULL;
|
---|
1800 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1801 | CFRunLoopRef hRunLoopRef = CFRunLoopGetCurrent();
|
---|
1802 |
|
---|
1803 | Assert(!pDevOsX->hRunLoopReaping);
|
---|
1804 |
|
---|
1805 | /*
|
---|
1806 | * If the last seen runloop for reaping differs we have to check whether the
|
---|
1807 | * the runloop sources are in the new runloop.
|
---|
1808 | */
|
---|
1809 | if (pDevOsX->hRunLoopReapingLast != hRunLoopRef)
|
---|
1810 | {
|
---|
1811 | RTCritSectEnter(&pDevOsX->CritSect);
|
---|
1812 |
|
---|
1813 | /* Every pipe. */
|
---|
1814 | if (!pDevOsX->pIfHead)
|
---|
1815 | usbProxyDarwinSeizeAllInterfaces(pDevOsX, true /* make the best out of it */);
|
---|
1816 |
|
---|
1817 | PUSBPROXYIFOSX pIf;
|
---|
1818 | for (pIf = pDevOsX->pIfHead; pIf; pIf = pIf->pNext)
|
---|
1819 | {
|
---|
1820 | if (!CFRunLoopContainsSource(hRunLoopRef, pIf->RunLoopSrcRef, g_pRunLoopMode))
|
---|
1821 | usbProxyDarwinAddRunLoopRef(&pIf->HeadOfRunLoopLst, pIf->RunLoopSrcRef);
|
---|
1822 | }
|
---|
1823 |
|
---|
1824 | /* Default control pipe. */
|
---|
1825 | if (!CFRunLoopContainsSource(hRunLoopRef, pDevOsX->RunLoopSrcRef, g_pRunLoopMode))
|
---|
1826 | usbProxyDarwinAddRunLoopRef(&pDevOsX->HeadOfRunLoopLst, pDevOsX->RunLoopSrcRef);
|
---|
1827 |
|
---|
1828 | /* Runloop wakeup source. */
|
---|
1829 | if (!CFRunLoopContainsSource(hRunLoopRef, pDevOsX->hRunLoopSrcWakeRef, g_pRunLoopMode))
|
---|
1830 | usbProxyDarwinAddRunLoopRef(&pDevOsX->HeadOfRunLoopWakeLst, pDevOsX->hRunLoopSrcWakeRef);
|
---|
1831 | RTCritSectLeave(&pDevOsX->CritSect);
|
---|
1832 |
|
---|
1833 | pDevOsX->hRunLoopReapingLast = hRunLoopRef;
|
---|
1834 | }
|
---|
1835 |
|
---|
1836 | ASMAtomicXchgPtr((void * volatile *)&pDevOsX->hRunLoopReaping, hRunLoopRef);
|
---|
1837 |
|
---|
1838 | if (ASMAtomicXchgBool(&pDevOsX->fReapingThreadWake, false))
|
---|
1839 | {
|
---|
1840 | /* Return immediately. */
|
---|
1841 | ASMAtomicXchgPtr((void * volatile *)&pDevOsX->hRunLoopReaping, NULL);
|
---|
1842 | return NULL;
|
---|
1843 | }
|
---|
1844 |
|
---|
1845 | /*
|
---|
1846 | * Excercise the runloop until we get an URB or we time out.
|
---|
1847 | */
|
---|
1848 | if ( !pDevOsX->pTaxingHead
|
---|
1849 | && cMillies)
|
---|
1850 | CFRunLoopRunInMode(g_pRunLoopMode, cMillies / 1000.0, true);
|
---|
1851 |
|
---|
1852 | ASMAtomicXchgPtr((void * volatile *)&pDevOsX->hRunLoopReaping, NULL);
|
---|
1853 | ASMAtomicXchgBool(&pDevOsX->fReapingThreadWake, false);
|
---|
1854 |
|
---|
1855 | /*
|
---|
1856 | * Any URBs pending delivery?
|
---|
1857 | */
|
---|
1858 | while ( pDevOsX->pTaxingHead
|
---|
1859 | && !pUrb)
|
---|
1860 | {
|
---|
1861 | RTCritSectEnter(&pDevOsX->CritSect);
|
---|
1862 |
|
---|
1863 | PUSBPROXYURBOSX pUrbOsX = pDevOsX->pTaxingHead;
|
---|
1864 | if (pUrbOsX)
|
---|
1865 | {
|
---|
1866 | /*
|
---|
1867 | * Remove from the taxing list.
|
---|
1868 | */
|
---|
1869 | if (pUrbOsX->pNext)
|
---|
1870 | pUrbOsX->pNext->pPrev = pUrbOsX->pPrev;
|
---|
1871 | else if (pDevOsX->pTaxingTail == pUrbOsX)
|
---|
1872 | pDevOsX->pTaxingTail = pUrbOsX->pPrev;
|
---|
1873 |
|
---|
1874 | if (pUrbOsX->pPrev)
|
---|
1875 | pUrbOsX->pPrev->pNext = pUrbOsX->pNext;
|
---|
1876 | else if (pDevOsX->pTaxingHead == pUrbOsX)
|
---|
1877 | pDevOsX->pTaxingHead = pUrbOsX->pNext;
|
---|
1878 | else
|
---|
1879 | AssertFailed();
|
---|
1880 |
|
---|
1881 | pUrb = pUrbOsX->pVUsbUrb;
|
---|
1882 | if (pUrb)
|
---|
1883 | {
|
---|
1884 | pUrb->Dev.pvPrivate = NULL;
|
---|
1885 | usbProxyDarwinUrbFree(pDevOsX, pUrbOsX);
|
---|
1886 | }
|
---|
1887 | }
|
---|
1888 | RTCritSectLeave(&pDevOsX->CritSect);
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | if (pUrb)
|
---|
1892 | LogFlowFunc(("LEAVE: %s: pProxyDev=%s returns %p\n", pUrb->pszDesc, pProxyDev->pUsbIns->pszName, pUrb));
|
---|
1893 | else
|
---|
1894 | LogFlowFunc(("LEAVE: NULL pProxyDev=%s returns NULL\n", pProxyDev->pUsbIns->pszName));
|
---|
1895 |
|
---|
1896 | return pUrb;
|
---|
1897 | }
|
---|
1898 |
|
---|
1899 |
|
---|
1900 | /**
|
---|
1901 | * Cancels a URB.
|
---|
1902 | *
|
---|
1903 | * The URB requires reaping, so we don't change its state.
|
---|
1904 | *
|
---|
1905 | * @remark There isn't any way to cancel a specific async request
|
---|
1906 | * on darwin. The interface only supports the aborting of
|
---|
1907 | * all URBs pending on an interface / pipe pair. Provided
|
---|
1908 | * the card does the URB cancelling before submitting new
|
---|
1909 | * requests, we should probably be fine...
|
---|
1910 | */
|
---|
1911 | static DECLCALLBACK(int) usbProxyDarwinUrbCancel(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
|
---|
1912 | {
|
---|
1913 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1914 | LogFlow(("%s: usbProxyDarwinUrbCancel: pProxyDev=%s EndPt=%d\n",
|
---|
1915 | pUrb->pszDesc, pProxyDev->pUsbIns->pszName, pUrb->EndPt));
|
---|
1916 |
|
---|
1917 | /*
|
---|
1918 | * Determine the interface / endpoint ref and invoke AbortPipe.
|
---|
1919 | */
|
---|
1920 | IOReturn irc = kIOReturnSuccess;
|
---|
1921 | if (!pUrb->EndPt)
|
---|
1922 | irc = (*pDevOsX->ppDevI)->USBDeviceAbortPipeZero(pDevOsX->ppDevI);
|
---|
1923 | else
|
---|
1924 | {
|
---|
1925 | uint8_t u8PipeRef;
|
---|
1926 | const uint8_t EndPt = pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? 0x80 : 0);
|
---|
1927 | PUSBPROXYIFOSX pIf = usbProxyDarwinGetInterfaceForEndpoint(pDevOsX, EndPt, &u8PipeRef, NULL);
|
---|
1928 | if (pIf)
|
---|
1929 | irc = (*pIf->ppIfI)->AbortPipe(pIf->ppIfI, u8PipeRef);
|
---|
1930 | else /* this may happen if a device reset, set configuration or set interface has been performed. */
|
---|
1931 | Log(("usbProxyDarwinUrbCancel: pProxyDev=%s pUrb=%p EndPt=%d - cannot find the interface / pipe!\n",
|
---|
1932 | pProxyDev->pUsbIns->pszName, pUrb, pUrb->EndPt));
|
---|
1933 | }
|
---|
1934 |
|
---|
1935 | int rc = VINF_SUCCESS;
|
---|
1936 | if (irc != kIOReturnSuccess)
|
---|
1937 | {
|
---|
1938 | Log(("usbProxyDarwinUrbCancel: pProxyDev=%s pUrb=%p EndPt=%d -> %#x!\n",
|
---|
1939 | pProxyDev->pUsbIns->pszName, pUrb, pUrb->EndPt, irc));
|
---|
1940 | rc = RTErrConvertFromDarwin(irc);
|
---|
1941 | }
|
---|
1942 |
|
---|
1943 | return rc;
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 |
|
---|
1947 | static DECLCALLBACK(int) usbProxyDarwinWakeup(PUSBPROXYDEV pProxyDev)
|
---|
1948 | {
|
---|
1949 | PUSBPROXYDEVOSX pDevOsX = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVOSX);
|
---|
1950 |
|
---|
1951 | LogFlow(("usbProxyDarwinWakeup: pProxyDev=%p\n", pProxyDev));
|
---|
1952 |
|
---|
1953 | ASMAtomicXchgBool(&pDevOsX->fReapingThreadWake, true);
|
---|
1954 | usbProxyDarwinReaperKick(pDevOsX);
|
---|
1955 | return VINF_SUCCESS;
|
---|
1956 | }
|
---|
1957 |
|
---|
1958 |
|
---|
1959 | /**
|
---|
1960 | * The Darwin USB Proxy Backend.
|
---|
1961 | */
|
---|
1962 | extern const USBPROXYBACK g_USBProxyDeviceHost =
|
---|
1963 | {
|
---|
1964 | /* pszName */
|
---|
1965 | "host",
|
---|
1966 | /* cbBackend */
|
---|
1967 | sizeof(USBPROXYDEVOSX),
|
---|
1968 | usbProxyDarwinOpen,
|
---|
1969 | NULL,
|
---|
1970 | usbProxyDarwinClose,
|
---|
1971 | usbProxyDarwinReset,
|
---|
1972 | usbProxyDarwinSetConfig,
|
---|
1973 | usbProxyDarwinClaimInterface,
|
---|
1974 | usbProxyDarwinReleaseInterface,
|
---|
1975 | usbProxyDarwinSetInterface,
|
---|
1976 | usbProxyDarwinClearHaltedEp,
|
---|
1977 | usbProxyDarwinUrbQueue,
|
---|
1978 | usbProxyDarwinUrbCancel,
|
---|
1979 | usbProxyDarwinUrbReap,
|
---|
1980 | usbProxyDarwinWakeup,
|
---|
1981 | 0
|
---|
1982 | };
|
---|
1983 |
|
---|