VirtualBox

source: vbox/trunk/src/VBox/Main/darwin/iokit.cpp@ 8484

Last change on this file since 8484 was 8484, checked in by vboxsync, 17 years ago

Darwin USB fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 47.0 KB
Line 
1/* $Id: iokit.cpp 8484 2008-04-30 00:12:33Z vboxsync $ */
2/** @file
3 * Main - Darwin IOKit Routines.
4 *
5 * Because IOKit makes use of COM like interfaces, it does not mix very
6 * well with COM/XPCOM and must therefore be isolated from it using a
7 * simpler C interface.
8 */
9
10/*
11 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
12 *
13 * This file is part of VirtualBox Open Source Edition (OSE), as
14 * available from http://www.virtualbox.org. This file is free software;
15 * you can redistribute it and/or modify it under the terms of the GNU
16 * General Public License (GPL) as published by the Free Software
17 * Foundation, in version 2 as it comes in the "COPYING" file of the
18 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
19 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
22 * Clara, CA 95054 USA or visit http://www.sun.com if you need
23 * additional information or have any questions.
24 */
25
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#define LOG_GROUP LOG_GROUP_MAIN
31
32#include <mach/mach.h>
33#include <Carbon/Carbon.h>
34#include <IOKit/IOKitLib.h>
35#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
36#include <IOKit/scsi-commands/SCSITaskLib.h>
37#include <mach/mach_error.h>
38#ifdef VBOX_WITH_USB
39# include <IOKit/usb/IOUSBLib.h>
40# include <IOKit/IOCFPlugIn.h>
41#endif
42
43#include <VBox/log.h>
44#include <VBox/err.h>
45#include <iprt/mem.h>
46#include <iprt/string.h>
47#include <iprt/process.h>
48#include <iprt/assert.h>
49#include <iprt/thread.h>
50
51#include "iokit.h"
52
53
54/*******************************************************************************
55* Defined Constants And Macros *
56*******************************************************************************/
57/** An attempt at catching reference leaks. */
58#define MY_CHECK_CREFS(cRefs) do { AssertMsg(cRefs < 25, ("%ld\n", cRefs)); NOREF(cRefs); } while (0)
59
60/** Contains the pid of the current client. If 0, the kernel is the current client. */
61#define VBOXUSB_CLIENT_KEY "VBoxUSB-Client"
62/** Contains the pid of the filter owner (i.e. the VBoxSVC pid). */
63#define VBOXUSB_OWNER_KEY "VBoxUSB-Owner"
64/** The VBoxUSBDevice class name. */
65#define VBOXUSBDEVICE_CLASS_NAME "org_virtualbox_VBoxUSBDevice"
66
67
68/*******************************************************************************
69* Global Variables *
70*******************************************************************************/
71/** The IO Master Port. */
72static mach_port_t g_MasterPort = NULL;
73
74
75/**
76 * Lazily opens the master port.
77 *
78 * @returns true if the port is open, false on failure (very unlikely).
79 */
80static bool darwinOpenMasterPort(void)
81{
82 if (!g_MasterPort)
83 {
84 kern_return_t krc = IOMasterPort(MACH_PORT_NULL, &g_MasterPort);
85 AssertReturn(krc == KERN_SUCCESS, false);
86 }
87 return true;
88}
89
90
91#ifdef VBOX_WITH_USB
92
93/**
94 * Gets an unsigned 8-bit integer value.
95 *
96 * @returns Success indicator (true/false).
97 * @param DictRef The dictionary.
98 * @param KeyStrRef The key name.
99 * @param pu8 Where to store the key value.
100 */
101static bool darwinDictGetU8(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, uint8_t *pu8)
102{
103 CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
104 if (ValRef)
105 {
106 if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt8Type, pu8))
107 return true;
108 }
109 *pu8 = 0;
110 return false;
111}
112
113
114/**
115 * Gets an unsigned 16-bit integer value.
116 *
117 * @returns Success indicator (true/false).
118 * @param DictRef The dictionary.
119 * @param KeyStrRef The key name.
120 * @param pu16 Where to store the key value.
121 */
122static bool darwinDictGetU16(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, uint16_t *pu16)
123{
124 CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
125 if (ValRef)
126 {
127 if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt16Type, pu16))
128 return true;
129 }
130 *pu16 = 0;
131 return false;
132}
133
134
135/**
136 * Gets an unsigned 32-bit integer value.
137 *
138 * @returns Success indicator (true/false).
139 * @param DictRef The dictionary.
140 * @param KeyStrRef The key name.
141 * @param pu32 Where to store the key value.
142 */
143static bool darwinDictGetU32(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, uint32_t *pu32)
144{
145 CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
146 if (ValRef)
147 {
148 if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt32Type, pu32))
149 return true;
150 }
151 *pu32 = 0;
152 return false;
153}
154
155
156/**
157 * Gets an unsigned 64-bit integer value.
158 *
159 * @returns Success indicator (true/false).
160 * @param DictRef The dictionary.
161 * @param KeyStrRef The key name.
162 * @param pu64 Where to store the key value.
163 */
164static bool darwinDictGetU64(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, uint64_t *pu64)
165{
166 CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
167 if (ValRef)
168 {
169 if (CFNumberGetValue((CFNumberRef)ValRef, kCFNumberSInt64Type, pu64))
170 return true;
171 }
172 *pu64 = 0;
173 return false;
174}
175
176
177/**
178 * Gets a RTPROCESS value.
179 *
180 * @returns Success indicator (true/false).
181 * @param DictRef The dictionary.
182 * @param KeyStrRef The key name.
183 * @param pProcess Where to store the key value.
184 */
185static bool darwinDictGetProccess(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, PRTPROCESS pProcess)
186{
187 switch (sizeof(*pProcess))
188 {
189 case sizeof(uint16_t): return darwinDictGetU16(DictRef, KeyStrRef, (uint16_t *)pProcess);
190 case sizeof(uint32_t): return darwinDictGetU32(DictRef, KeyStrRef, (uint32_t *)pProcess);
191 case sizeof(uint64_t): return darwinDictGetU64(DictRef, KeyStrRef, (uint64_t *)pProcess);
192 default:
193 AssertMsgFailedReturn(("%d\n", sizeof(*pProcess)), false);
194 }
195}
196
197
198/**
199 * Gets string value, converted to UTF-8 and put in a IPRT string buffer.
200 *
201 * @returns Success indicator (true/false).
202 * @param DictRef The dictionary.
203 * @param KeyStrRef The key name.
204 * @param ppsz Where to store the key value. Free with RTStrFree. Set to NULL on failure.
205 */
206static bool darwinDictGetString(CFMutableDictionaryRef DictRef, CFStringRef KeyStrRef, char **ppsz)
207{
208 CFTypeRef ValRef = CFDictionaryGetValue(DictRef, KeyStrRef);
209 if (ValRef)
210 {
211 char szBuf[512];
212 if (CFStringGetCString((CFStringRef)ValRef, szBuf, sizeof(szBuf), kCFStringEncodingUTF8))
213 {
214 *ppsz = RTStrDup(RTStrStrip(szBuf));
215 if (*ppsz)
216 return true;
217 }
218 }
219 *ppsz = NULL;
220 return false;
221}
222
223
224#if 1 /* dumping disabled */
225# define DARWIN_IOKIT_LOG(a) Log(a)
226# define DARWIN_IOKIT_LOG_FLUSH() do {} while (0)
227# define DARWIN_IOKIT_DUMP_OBJ(o) do {} while (0)
228#else
229# if 0
230# include <iprt/stream.h>
231# define DARWIN_IOKIT_LOG(a) RTPrintf a
232# define DARWIN_IOKIT_LOG_FLUSH() RTStrmFlush(g_pStdOut)
233# else
234# define DARWIN_IOKIT_LOG(a) RTLogPrintf a
235# define DARWIN_IOKIT_LOG(a) RTLogFlush()
236# endif
237# define DARWIN_IOKIT_DUMP_OBJ(o) darwinDumpObj(o)
238
239/**
240 * Callback for dumping a dictionary key.
241 *
242 * @param pvKey The key name.
243 * @param pvValue The key value
244 * @param pvUser The recursion depth.
245 */
246static void darwinDumpDictCallback(const void *pvKey, const void *pvValue, void *pvUser)
247{
248 /* display the key name. */
249 char *pszKey = (char *)RTMemTmpAlloc(1024);
250 if (!CFStringGetCString((CFStringRef)pvKey, pszKey, 1024, kCFStringEncodingUTF8))
251 strcpy(pszKey, "CFStringGetCString failure");
252 DARWIN_IOKIT_LOG(("%+*s%s", (int)(uintptr_t)pvUser, "", pszKey));
253 RTMemTmpFree(pszKey);
254
255 /* display the value type */
256 CFTypeID Type = CFGetTypeID(pvValue);
257 DARWIN_IOKIT_LOG((" [%d-", Type));
258
259 /* display the value */
260 if (Type == CFDictionaryGetTypeID())
261 {
262 DARWIN_IOKIT_LOG(("dictionary] =\n"
263 "%-*s{\n", (int)(uintptr_t)pvUser, ""));
264 CFDictionaryApplyFunction((CFDictionaryRef)pvValue, darwinDumpDictCallback, (void *)((uintptr_t)pvUser + 4));
265 DARWIN_IOKIT_LOG(("%-*s}\n", (int)(uintptr_t)pvUser, ""));
266 }
267 else if (Type == CFNumberGetTypeID())
268 {
269 union
270 {
271 SInt8 s8;
272 SInt16 s16;
273 SInt32 s32;
274 SInt64 s64;
275 Float32 rf32;
276 Float64 rd64;
277 char ch;
278 short s;
279 int i;
280 long l;
281 long long ll;
282 float rf;
283 double rd;
284 CFIndex iCF;
285 } u;
286 memset(&u, 0, sizeof(u));
287 CFNumberType NumType = CFNumberGetType((CFNumberRef)pvValue);
288 if (CFNumberGetValue((CFNumberRef)pvValue, NumType, &u))
289 {
290 switch (CFNumberGetType((CFNumberRef)pvValue))
291 {
292 case kCFNumberSInt8Type: DARWIN_IOKIT_LOG(("SInt8] = %RI8 (%#RX8)\n", NumType, u.s8, u.s8)); break;
293 case kCFNumberSInt16Type: DARWIN_IOKIT_LOG(("SInt16] = %RI16 (%#RX16)\n", NumType, u.s16, u.s16)); break;
294 case kCFNumberSInt32Type: DARWIN_IOKIT_LOG(("SInt32] = %RI32 (%#RX32)\n", NumType, u.s32, u.s32)); break;
295 case kCFNumberSInt64Type: DARWIN_IOKIT_LOG(("SInt64] = %RI64 (%#RX64)\n", NumType, u.s64, u.s64)); break;
296 case kCFNumberFloat32Type: DARWIN_IOKIT_LOG(("float32] = %#lx\n", NumType, u.l)); break;
297 case kCFNumberFloat64Type: DARWIN_IOKIT_LOG(("float64] = %#llx\n", NumType, u.ll)); break;
298 case kCFNumberFloatType: DARWIN_IOKIT_LOG(("float] = %#lx\n", NumType, u.l)); break;
299 case kCFNumberDoubleType: DARWIN_IOKIT_LOG(("double] = %#llx\n", NumType, u.ll)); break;
300 case kCFNumberCharType: DARWIN_IOKIT_LOG(("char] = %hhd (%hhx)\n", NumType, u.ch, u.ch)); break;
301 case kCFNumberShortType: DARWIN_IOKIT_LOG(("short] = %hd (%hx)\n", NumType, u.s, u.s)); break;
302 case kCFNumberIntType: DARWIN_IOKIT_LOG(("int] = %d (%#x)\n", NumType, u.i, u.i)); break;
303 case kCFNumberLongType: DARWIN_IOKIT_LOG(("long] = %ld (%#lx)\n", NumType, u.l, u.l)); break;
304 case kCFNumberLongLongType: DARWIN_IOKIT_LOG(("long long] = %lld (%#llx)\n", NumType, u.ll, u.ll)); break;
305 case kCFNumberCFIndexType: DARWIN_IOKIT_LOG(("CFIndex] = %lld (%#llx)\n", NumType, (long long)u.iCF, (long long)u.iCF)); break;
306 break;
307 default: DARWIN_IOKIT_LOG(("%d?] = %lld (%llx)\n", NumType, u.ll, u.ll)); break;
308 }
309 }
310 else
311 DARWIN_IOKIT_LOG(("number] = CFNumberGetValue failed\n"));
312 }
313 else if (Type == CFBooleanGetTypeID())
314 DARWIN_IOKIT_LOG(("boolean] = %RTbool\n", CFBooleanGetValue((CFBooleanRef)pvValue)));
315 else if (Type == CFStringGetTypeID())
316 {
317 DARWIN_IOKIT_LOG(("string] = "));
318 char *pszValue = (char *)RTMemTmpAlloc(16*_1K);
319 if (!CFStringGetCString((CFStringRef)pvValue, pszValue, 16*_1K, kCFStringEncodingUTF8))
320 strcpy(pszValue, "CFStringGetCString failure");
321 DARWIN_IOKIT_LOG(("\"%s\"\n", pszValue));
322 RTMemTmpFree(pszValue);
323 }
324 else
325 DARWIN_IOKIT_LOG(("??] = %p\n", pvValue));
326}
327
328
329/**
330 * Dumps a dictionary to the log.
331 *
332 * @param DictRef The dictionary to dump.
333 */
334static void darwinDumpDict(CFMutableDictionaryRef DictRef, unsigned cIndents)
335{
336 CFDictionaryApplyFunction(DictRef, darwinDumpDictCallback, (void *)(uintptr_t)cIndents);
337 DARWIN_IOKIT_LOG_FLUSH();
338}
339
340
341/**
342 * Dumps an I/O kit registry object and all it children.
343 * @param Object The object to dump.
344 * @param cIndents The number of indents to use.
345 */
346static void darwinDumpObjInt(io_object_t Object, unsigned cIndents)
347{
348 static io_string_t s_szPath;
349 kern_return_t krc = IORegistryEntryGetPath(Object, kIOServicePlane, s_szPath);
350 if (krc != KERN_SUCCESS)
351 strcpy(s_szPath, "IORegistryEntryGetPath failed");
352 DARWIN_IOKIT_LOG(("Dumping %p - %s:\n", (const void *)Object, s_szPath));
353
354 CFMutableDictionaryRef PropsRef = 0;
355 krc = IORegistryEntryCreateCFProperties(Object, &PropsRef, kCFAllocatorDefault, kNilOptions);
356 if (krc == KERN_SUCCESS)
357 {
358 darwinDumpDict(PropsRef, cIndents + 4);
359 CFRelease(PropsRef);
360 }
361
362 /*
363 * Children.
364 */
365 io_iterator_t Children;
366 krc = IORegistryEntryGetChildIterator(Object, kIOServicePlane, &Children);
367 if (krc == KERN_SUCCESS)
368 {
369 io_object_t Child;
370 while ((Child = IOIteratorNext(Children)))
371 {
372 darwinDumpObjInt(Child, cIndents + 4);
373 IOObjectRelease(Child);
374 }
375 IOObjectRelease(Children);
376 }
377 else
378 DARWIN_IOKIT_LOG(("IORegistryEntryGetChildIterator -> %#x\n", krc));
379}
380
381/**
382 * Dumps an I/O kit registry object and all it children.
383 * @param Object The object to dump.
384 */
385static void darwinDumpObj(io_object_t Object)
386{
387 darwinDumpObjInt(Object, 0);
388}
389
390#endif
391
392
393/**
394 * Notification data created by DarwinSubscribeUSBNotifications, used by
395 * the callbacks and finally freed by DarwinUnsubscribeUSBNotifications.
396 */
397typedef struct DARWINUSBNOTIFY
398{
399 /** The notification port.
400 * It's shared between the notification callbacks. */
401 IONotificationPortRef NotifyPort;
402 /** The run loop source for NotifyPort. */
403 CFRunLoopSourceRef NotifyRLSrc;
404 /** The attach notification iterator. */
405 io_iterator_t AttachIterator;
406 /** The 2nd attach notification iterator. */
407 io_iterator_t AttachIterator2;
408 /** The detach notificaiton iterator. */
409 io_iterator_t DetachIterator;
410} DARWINUSBNOTIFY, *PDARWINUSBNOTIFY;
411
412
413/**
414 * Run thru an interrator.
415 *
416 * The docs says this is necessary to start getting notifications,
417 * so this function is called in the callbacks and right after
418 * registering the notification.
419 *
420 * @param pIterator The iterator reference.
421 */
422static void darwinDrainIterator(io_iterator_t pIterator)
423{
424 io_object_t Object;
425 while ((Object = IOIteratorNext(pIterator)))
426 {
427 DARWIN_IOKIT_DUMP_OBJ(Object);
428 IOObjectRelease(Object);
429 }
430}
431
432
433/**
434 * Callback for the 1st attach notification.
435 *
436 * @param pvNotify Our data.
437 * @param NotifyIterator The notification iterator.
438 */
439static void darwinUSBAttachNotification1(void *pvNotify, io_iterator_t NotifyIterator)
440{
441 DARWIN_IOKIT_LOG(("USB Attach Notification1\n"));
442 NOREF(pvNotify); //PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)pvNotify;
443 darwinDrainIterator(NotifyIterator);
444}
445
446
447/**
448 * Callback for the 2nd attach notification.
449 *
450 * @param pvNotify Our data.
451 * @param NotifyIterator The notification iterator.
452 */
453static void darwinUSBAttachNotification2(void *pvNotify, io_iterator_t NotifyIterator)
454{
455 DARWIN_IOKIT_LOG(("USB Attach Notification2\n"));
456 NOREF(pvNotify); //PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)pvNotify;
457 darwinDrainIterator(NotifyIterator);
458}
459
460
461/**
462 * Callback for the detach notifications.
463 *
464 * @param pvNotify Our data.
465 * @param NotifyIterator The notification iterator.
466 */
467static void darwinUSBDetachNotification(void *pvNotify, io_iterator_t NotifyIterator)
468{
469 DARWIN_IOKIT_LOG(("USB Detach Notification\n"));
470 NOREF(pvNotify); //PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)pvNotify;
471 darwinDrainIterator(NotifyIterator);
472}
473
474
475/**
476 * Subscribes the run loop to USB notification events relevant to
477 * device attach/detach.
478 *
479 * The source mode for these events is defined as VBOX_IOKIT_MODE_STRING
480 * so that the caller can listen to events from this mode only and
481 * re-evalutate the list of attached devices whenever an event arrives.
482 *
483 * @returns opaque for passing to the unsubscribe function. If NULL
484 * something unexpectedly failed during subscription.
485 */
486void *DarwinSubscribeUSBNotifications(void)
487{
488 AssertReturn(darwinOpenMasterPort(), NULL);
489
490 PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)RTMemAllocZ(sizeof(*pNotify));
491 AssertReturn(pNotify, NULL);
492
493 /*
494 * Create the notification port, bake it into a runloop source which we
495 * then add to our run loop.
496 */
497 pNotify->NotifyPort = IONotificationPortCreate(g_MasterPort);
498 Assert(pNotify->NotifyPort);
499 if (pNotify->NotifyPort)
500 {
501 pNotify->NotifyRLSrc = IONotificationPortGetRunLoopSource(pNotify->NotifyPort);
502 Assert(pNotify->NotifyRLSrc);
503 if (pNotify->NotifyRLSrc)
504 {
505 CFRunLoopAddSource(CFRunLoopGetCurrent(), pNotify->NotifyRLSrc, CFSTR(VBOX_IOKIT_MODE_STRING));
506
507 /*
508 * Create the notifcation callbacks.
509 */
510 kern_return_t rc = IOServiceAddMatchingNotification(pNotify->NotifyPort,
511 kIOPublishNotification,
512 IOServiceMatching(kIOUSBDeviceClassName),
513 darwinUSBAttachNotification1,
514 pNotify,
515 &pNotify->AttachIterator);
516 if (rc == KERN_SUCCESS)
517 {
518 darwinDrainIterator(pNotify->AttachIterator);
519 rc = IOServiceAddMatchingNotification(pNotify->NotifyPort,
520 kIOMatchedNotification,
521 IOServiceMatching(kIOUSBDeviceClassName),
522 darwinUSBAttachNotification2,
523 pNotify,
524 &pNotify->AttachIterator2);
525 if (rc == KERN_SUCCESS)
526 {
527 darwinDrainIterator(pNotify->AttachIterator2);
528 rc = IOServiceAddMatchingNotification(pNotify->NotifyPort,
529 kIOTerminatedNotification,
530 IOServiceMatching(kIOUSBDeviceClassName),
531 darwinUSBDetachNotification,
532 pNotify,
533 &pNotify->DetachIterator);
534 {
535 darwinDrainIterator(pNotify->DetachIterator);
536 return pNotify;
537 }
538 IOObjectRelease(pNotify->AttachIterator2);
539 }
540 IOObjectRelease(pNotify->AttachIterator);
541 }
542 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), pNotify->NotifyRLSrc, CFSTR(VBOX_IOKIT_MODE_STRING));
543 }
544 IONotificationPortDestroy(pNotify->NotifyPort);
545 }
546
547 RTMemFree(pNotify);
548 return NULL;
549}
550
551
552/**
553 * Unsubscribe the run loop from USB notification subscribed to
554 * by DarwinSubscribeUSBNotifications.
555 *
556 * @param pvOpaque The return value from DarwinSubscribeUSBNotifications.
557 */
558void DarwinUnsubscribeUSBNotifications(void *pvOpaque)
559{
560 PDARWINUSBNOTIFY pNotify = (PDARWINUSBNOTIFY)pvOpaque;
561 if (!pNotify)
562 return;
563
564 IOObjectRelease(pNotify->AttachIterator);
565 pNotify->AttachIterator = NULL;
566 IOObjectRelease(pNotify->AttachIterator2);
567 pNotify->AttachIterator2 = NULL;
568 IOObjectRelease(pNotify->DetachIterator);
569 pNotify->DetachIterator = NULL;
570
571 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), pNotify->NotifyRLSrc, CFSTR(VBOX_IOKIT_MODE_STRING));
572 IONotificationPortDestroy(pNotify->NotifyPort);
573 pNotify->NotifyRLSrc = NULL;
574 pNotify->NotifyPort = NULL;
575
576 RTMemFree(pNotify);
577}
578
579
580/**
581 * Decends recursivly into a IORegistry tree locating the first object of a given class.
582 *
583 * The search is performed depth first.
584 *
585 * @returns Object reference if found, NULL if not.
586 * @param Object The current tree root.
587 * @param pszClass The name of the class we're looking for.
588 * @param pszNameBuf A scratch buffer for query the class name in to avoid
589 * wasting 128 bytes on an io_name_t object for every recursion.
590 */
591static io_object_t darwinFindObjectByClass(io_object_t Object, const char *pszClass, io_name_t pszNameBuf)
592{
593 io_iterator_t Children;
594 kern_return_t krc = IORegistryEntryGetChildIterator(Object, kIOServicePlane, &Children);
595 if (krc != KERN_SUCCESS)
596 return NULL;
597 io_object_t Child;
598 while ((Child = IOIteratorNext(Children)))
599 {
600 krc = IOObjectGetClass(Child, pszNameBuf);
601 if ( krc == KERN_SUCCESS
602 && !strcmp(pszNameBuf, pszClass))
603 break;
604
605 io_object_t GrandChild = darwinFindObjectByClass(Child, pszClass, pszNameBuf);
606 IOObjectRelease(Child);
607 if (GrandChild)
608 {
609 Child = GrandChild;
610 break;
611 }
612 }
613 IOObjectRelease(Children);
614 return Child;
615}
616
617
618/**
619 * Decends recursivly into IOUSBMassStorageClass tree to check whether
620 * the MSD is mounted or not.
621 *
622 * The current heuristic is to look for the IOMedia class.
623 *
624 * @returns true if mounted, false if not.
625 * @param MSDObj The IOUSBMassStorageClass object.
626 * @param pszNameBuf A scratch buffer for query the class name in to avoid
627 * wasting 128 bytes on an io_name_t object for every recursion.
628 */
629static bool darwinIsMassStorageInterfaceInUse(io_object_t MSDObj, io_name_t pszNameBuf)
630{
631 io_object_t MediaObj = darwinFindObjectByClass(MSDObj, "IOMedia", pszNameBuf);
632 if (MediaObj)
633 {
634 /* more checks? */
635 IOObjectRelease(MediaObj);
636 return true;
637 }
638 return false;
639}
640
641
642/**
643 * Worker function for DarwinGetUSBDevices() that tries to figure out
644 * what state the device is in and set enmState.
645 *
646 * This is mostly a matter of distinguishing between devices that nobody
647 * uses, devices that can be seized and devices that cannot be grabbed.
648 *
649 * @param pCur The USB device data.
650 * @param USBDevice The USB device object.
651 * @param PropsRef The USB device properties.
652 */
653static void darwinDeterminUSBDeviceState(PUSBDEVICE pCur, io_object_t USBDevice, CFMutableDictionaryRef PropsRef)
654{
655 /*
656 * Iterate the interfaces (among the children of the IOUSBDevice object).
657 */
658 io_iterator_t Interfaces;
659 kern_return_t krc = IORegistryEntryGetChildIterator(USBDevice, kIOServicePlane, &Interfaces);
660 if (krc != KERN_SUCCESS)
661 return;
662
663 bool fHaveOwner = false;
664 RTPROCESS Owner = NIL_RTPROCESS;
665 bool fHaveClient = false;
666 RTPROCESS Client = NIL_RTPROCESS;
667 bool fUserClientOnly = true;
668 bool fConfigured = false;
669 bool fInUse = false;
670 bool fSeizable = true;
671 io_object_t Interface;
672 while ((Interface = IOIteratorNext(Interfaces)))
673 {
674 io_name_t szName;
675 krc = IOObjectGetClass(Interface, szName);
676 if ( krc == KERN_SUCCESS
677 && !strcmp(szName, "IOUSBInterface"))
678 {
679 fConfigured = true;
680
681 /*
682 * Iterate the interface children looking for stuff other than
683 * IOUSBUserClientInit objects.
684 */
685 io_iterator_t Children1;
686 krc = IORegistryEntryGetChildIterator(Interface, kIOServicePlane, &Children1);
687 if (krc == KERN_SUCCESS)
688 {
689 io_object_t Child1;
690 while ((Child1 = IOIteratorNext(Children1)))
691 {
692 krc = IOObjectGetClass(Child1, szName);
693 if ( krc == KERN_SUCCESS
694 && strcmp(szName, "IOUSBUserClientInit"))
695 {
696 fUserClientOnly = false;
697
698 if (!strcmp(szName, "IOUSBMassStorageClass"))
699 {
700 /* Only permit capturing MSDs that aren't mounted, at least
701 until the GUI starts poping up warnings about data loss
702 and such when capturing a busy device. */
703 fSeizable = false;
704 fInUse |= darwinIsMassStorageInterfaceInUse(Child1, szName);
705 }
706 else if (!strcmp(szName, "IOUSBHIDDriver")
707 || !strcmp(szName, "AppleHIDMouse")
708 /** @todo more? */)
709 {
710 /* For now, just assume that all HID devices are inaccessible
711 because of the greedy HID service. */
712 fSeizable = false;
713 fInUse = true;
714 }
715 else
716 fInUse = true;
717 }
718 IOObjectRelease(Child1);
719 }
720 IOObjectRelease(Children1);
721 }
722 }
723 /*
724 * Not an interface, could it be VBoxUSBDevice?
725 * If it is, get the owner and client properties.
726 */
727 else if ( krc == KERN_SUCCESS
728 && !strcmp(szName, VBOXUSBDEVICE_CLASS_NAME))
729 {
730 CFMutableDictionaryRef PropsRef = 0;
731 krc = IORegistryEntryCreateCFProperties(Interface, &PropsRef, kCFAllocatorDefault, kNilOptions);
732 if (krc == KERN_SUCCESS)
733 {
734 fHaveOwner = darwinDictGetProccess(PropsRef, CFSTR(VBOXUSB_OWNER_KEY), &Owner);
735 fHaveClient = darwinDictGetProccess(PropsRef, CFSTR(VBOXUSB_CLIENT_KEY), &Client);
736 CFRelease(PropsRef);
737 }
738 }
739
740 IOObjectRelease(Interface);
741 }
742 IOObjectRelease(Interfaces);
743
744 /*
745 * Calc the status.
746 */
747 if (fHaveOwner)
748 {
749 if (Owner == RTProcSelf())
750 pCur->enmState = !fHaveClient || Client == NIL_RTPROCESS || !Client
751 ? USBDEVICESTATE_HELD_BY_PROXY
752 : USBDEVICESTATE_USED_BY_GUEST;
753 else
754 pCur->enmState = USBDEVICESTATE_USED_BY_HOST;
755 }
756 else if (fUserClientOnly)
757 /** @todo how to detect other user client?!? - Look for IOUSBUserClient! */
758 pCur->enmState = !fConfigured
759 ? USBDEVICESTATE_UNUSED
760 : USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
761 else if (!fInUse)
762 pCur->enmState = USBDEVICESTATE_UNUSED;
763 else
764 pCur->enmState = fSeizable
765 ? USBDEVICESTATE_USED_BY_HOST_CAPTURABLE
766 : USBDEVICESTATE_USED_BY_HOST;
767}
768
769
770/**
771 * Enumerate the USB devices returning a FIFO of them.
772 *
773 * @returns Pointer to the head.
774 * USBProxyService::freeDevice is expected to free each of the list elements.
775 */
776PUSBDEVICE DarwinGetUSBDevices(void)
777{
778 AssertReturn(darwinOpenMasterPort(), NULL);
779 //DARWIN_IOKIT_LOG(("DarwinGetUSBDevices\n"));
780
781 /*
782 * Create a matching dictionary for searching for USB Devices in the IOKit.
783 */
784 CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOUSBDeviceClassName);
785 AssertReturn(RefMatchingDict, NULL);
786
787 /*
788 * Perform the search and get a collection of USB Device back.
789 */
790 io_iterator_t USBDevices = NULL;
791 IOReturn rc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &USBDevices);
792 AssertMsgReturn(rc == kIOReturnSuccess, ("rc=%d\n", rc), NULL);
793 RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
794
795 /*
796 * Enumerate the USB Devices.
797 */
798 PUSBDEVICE pHead = NULL;
799 PUSBDEVICE pTail = NULL;
800 unsigned i = 0;
801 io_object_t USBDevice;
802 while ((USBDevice = IOIteratorNext(USBDevices)) != 0)
803 {
804 //DARWIN_IOKIT_DUMP_OBJ(USBDevice);
805
806 /*
807 * Query the device properties from the registry.
808 *
809 * We could alternatively use the device and such, but that will be
810 * slower and we would have to resort to the registry for the three
811 * string anyway.
812 */
813 CFMutableDictionaryRef PropsRef = 0;
814 kern_return_t krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
815 if (krc == KERN_SUCCESS)
816 {
817 bool fOk = false;
818 PUSBDEVICE pCur = (PUSBDEVICE)RTMemAllocZ(sizeof(*pCur));
819 do /* loop for breaking out of on failure. */
820 {
821 AssertBreak(pCur,);
822
823 /*
824 * Mandatory
825 */
826 pCur->bcdUSB = 0; /* we've no idea. */
827 pCur->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE; /* just a default, we'll try harder in a bit. */
828
829 AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDeviceClass), &pCur->bDeviceClass),);
830 /* skip hubs */
831 if (pCur->bDeviceClass == 0x09 /* hub, find a define! */)
832 break;
833 AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDeviceSubClass), &pCur->bDeviceSubClass),);
834 AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDeviceProtocol), &pCur->bDeviceProtocol),);
835 AssertBreak(darwinDictGetU16(PropsRef, CFSTR(kUSBVendorID), &pCur->idVendor),);
836 AssertBreak(darwinDictGetU16(PropsRef, CFSTR(kUSBProductID), &pCur->idProduct),);
837 AssertBreak(darwinDictGetU16(PropsRef, CFSTR(kUSBDeviceReleaseNumber), &pCur->bcdDevice),);
838 uint32_t u32LocationId;
839 AssertBreak(darwinDictGetU32(PropsRef, CFSTR(kUSBDevicePropertyLocationID), &u32LocationId),);
840 uint64_t u64SessionId;
841 AssertBreak(darwinDictGetU64(PropsRef, CFSTR("sessionID"), &u64SessionId),);
842 char szAddress[64];
843 RTStrPrintf(szAddress, sizeof(szAddress), "p=0x%04RX16;v=0x%04RX16;s=0x%016RX64;l=0x%08RX32",
844 pCur->idProduct, pCur->idVendor, u64SessionId, u32LocationId);
845 pCur->pszAddress = RTStrDup(szAddress);
846 AssertBreak(pCur->pszAddress,);
847 uint8_t bSpeed;
848 AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDevicePropertySpeed), &bSpeed),);
849 Assert(bSpeed <= 2);
850 pCur->enmSpeed = bSpeed == 2 ? USBDEVICESPEED_HIGH
851 : bSpeed == 1 ? USBDEVICESPEED_FULL
852 : bSpeed == 0 ? USBDEVICESPEED_LOW
853 : USBDEVICESPEED_UNKNOWN;
854
855 /*
856 * Optional.
857 * There are some nameless device in the iMac, apply names to them.
858 */
859 darwinDictGetString(PropsRef, CFSTR("USB Vendor Name"), (char **)&pCur->pszManufacturer);
860 if ( !pCur->pszManufacturer
861 && pCur->idVendor == kIOUSBVendorIDAppleComputer)
862 pCur->pszManufacturer = RTStrDup("Apple Computer, Inc.");
863 darwinDictGetString(PropsRef, CFSTR("USB Product Name"), (char **)&pCur->pszProduct);
864 if ( !pCur->pszProduct
865 && pCur->bDeviceClass == 224 /* Wireless */
866 && pCur->bDeviceSubClass == 1 /* Radio Frequency */
867 && pCur->bDeviceProtocol == 1 /* Bluetooth */)
868 pCur->pszProduct = RTStrDup("Bluetooth");
869 darwinDictGetString(PropsRef, CFSTR("USB Serial Number"), (char **)&pCur->pszSerialNumber);
870
871#if 0 /* leave the remainder as zero for now. */
872 /*
873 * Create a plugin interface for the service and query its USB Device interface.
874 */
875 SInt32 Score = 0;
876 IOCFPlugInInterface **ppPlugInInterface = NULL;
877 rc = IOCreatePlugInInterfaceForService(USBDevice, kIOUSBDeviceUserClientTypeID,
878 kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
879 if (rc == kIOReturnSuccess)
880 {
881 IOUSBDeviceInterface245 **ppUSBDevI = NULL;
882 HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
883 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245),
884 (LPVOID *)&ppUSBDevI);
885 rc = IODestroyPlugInInterface(ppPlugInInterface); Assert(rc == kIOReturnSuccess);
886 ppPlugInInterface = NULL;
887 if (hrc == S_OK)
888 {
889 /** @todo enumerate configurations and interfaces if we actually need them. */
890 //IOReturn (*GetNumberOfConfigurations)(void *self, UInt8 *numConfig);
891 //IOReturn (*GetConfigurationDescriptorPtr)(void *self, UInt8 configIndex, IOUSBConfigurationDescriptorPtr *desc);
892 //IOReturn (*CreateInterfaceIterator)(void *self, IOUSBFindInterfaceRequest *req, io_iterator_t *iter);
893 }
894 long cReft = (*ppUSBDeviceInterface)->Release(ppUSBDeviceInterface); MY_CHECK_CREFS(cRefs);
895 }
896#endif
897 /*
898 * Try determin the state.
899 */
900 darwinDeterminUSBDeviceState(pCur, USBDevice, PropsRef);
901
902 /*
903 * We're good. Link the device.
904 */
905 pCur->pPrev = pTail;
906 if (pTail)
907 pTail = pTail->pNext = pCur;
908 else
909 pTail = pHead = pCur;
910 fOk = true;
911 } while (0);
912
913 /* cleanup on failure / skipped device. */
914 if (!fOk && pCur)
915 DarwinFreeUSBDeviceFromIOKit(pCur);
916
917 CFRelease(PropsRef);
918 }
919 else
920 AssertMsgFailed(("krc=%#x\n", krc));
921
922 IOObjectRelease(USBDevice);
923 i++;
924 }
925
926 IOObjectRelease(USBDevices);
927 //DARWIN_IOKIT_LOG_FLUSH();
928
929 /*
930 * Some post processing. There are a couple of things we have to
931 * make 100% sure about, and that is that the (Apple) keyboard
932 * and mouse most likely to be in use by the user aren't available
933 * for capturing. If there is no Apple mouse or keyboard we'll
934 * take the first one from another vendor.
935 */
936 /* As it turns out, the HID service will take all keyboards and mice
937 and we're not currently able to seize them. */
938 PUSBDEVICE pMouse = NULL;
939 PUSBDEVICE pKeyboard = NULL;
940 for (PUSBDEVICE pCur = pHead; pCur; pCur = pCur->pNext)
941 if (pCur->idVendor == kIOUSBVendorIDAppleComputer)
942 {
943 /*
944 * This test is a bit rough, should check device class/protocol but
945 * we don't have interface info yet so that might be a bit tricky.
946 */
947 if ( ( !pKeyboard
948 || pKeyboard->idVendor != kIOUSBVendorIDAppleComputer)
949 && pCur->pszProduct
950 && strstr(pCur->pszProduct, " Keyboard"))
951 pKeyboard = pCur;
952 else if ( ( !pMouse
953 || pMouse->idVendor != kIOUSBVendorIDAppleComputer)
954 && pCur->pszProduct
955 && strstr(pCur->pszProduct, " Mouse")
956 )
957 pMouse = pCur;
958 }
959 else if (!pKeyboard || !pMouse)
960 {
961 if ( pCur->bDeviceClass == 3 /* HID */
962 && pCur->bDeviceProtocol == 1 /* Keyboard */)
963 pKeyboard = pCur;
964 else if ( pCur->bDeviceClass == 3 /* HID */
965 && pCur->bDeviceProtocol == 2 /* Mouse */)
966 pMouse = pCur;
967 /** @todo examin interfaces */
968 }
969
970 if (pKeyboard)
971 pKeyboard->enmState = USBDEVICESTATE_USED_BY_HOST;
972 if (pMouse)
973 pMouse->enmState = USBDEVICESTATE_USED_BY_HOST;
974
975 return pHead;
976}
977
978
979/**
980 * Triggers re-enumeration of a device.
981 *
982 * @returns VBox status code.
983 * @param pCur The USBDEVICE structure for the device.
984 */
985int DarwinReEnumerateUSBDevice(PCUSBDEVICE pCur)
986{
987 int vrc;
988 const char *pszAddress = pCur->pszAddress;
989 AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
990 AssertReturn(darwinOpenMasterPort(), VERR_GENERAL_FAILURE);
991
992 /*
993 * This code is a short version of the Open method in USBProxyDevice-darwin.cpp stuff.
994 * Fixes made to this code probably applies there too!
995 */
996
997 CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOUSBDeviceClassName);
998 AssertReturn(RefMatchingDict, NULL);
999
1000 uint64_t u64SessionId = 0;
1001 uint32_t u32LocationId = 0;
1002 const char *psz = pszAddress;
1003 do
1004 {
1005 const char chValue = *psz;
1006 AssertReleaseReturn(psz[1] == '=', VERR_INTERNAL_ERROR);
1007 uint64_t u64Value;
1008 int rc = RTStrToUInt64Ex(psz + 2, (char **)&psz, 0, &u64Value);
1009 AssertReleaseRCReturn(rc, rc);
1010 AssertReleaseReturn(!*psz || *psz == ';', rc);
1011 switch (chValue)
1012 {
1013 case 'l':
1014 u32LocationId = (uint32_t)u64Value;
1015 break;
1016 case 's':
1017 u64SessionId = u64Value;
1018 break;
1019 case 'p':
1020 case 'v':
1021 {
1022#if 0 /* Guess what, this doesn't 'ing work either! */
1023 SInt32 i32 = (int16_t)u64Value;
1024 CFNumberRef Num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i32);
1025 AssertBreak(Num,);
1026 CFDictionarySetValue(RefMatchingDict, chValue == 'p' ? CFSTR(kUSBProductID) : CFSTR(kUSBVendorID), Num);
1027 CFRelease(Num);
1028#endif
1029 break;
1030 }
1031 default:
1032 AssertReleaseMsgFailedReturn(("chValue=%#x\n", chValue), VERR_INTERNAL_ERROR);
1033 }
1034 if (*psz == ';')
1035 psz++;
1036 } while (*psz);
1037
1038 io_iterator_t USBDevices = NULL;
1039 IOReturn irc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &USBDevices);
1040 AssertMsgReturn(irc == kIOReturnSuccess, ("irc=%#x\n", irc), NULL);
1041 RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
1042
1043 unsigned cMatches = 0;
1044 io_object_t USBDevice;
1045 while ((USBDevice = IOIteratorNext(USBDevices)))
1046 {
1047 cMatches++;
1048 CFMutableDictionaryRef PropsRef = 0;
1049 kern_return_t krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
1050 if (krc == KERN_SUCCESS)
1051 {
1052 uint64_t u64CurSessionId;
1053 uint32_t u32CurLocationId;
1054 if ( ( !u64SessionId
1055 || ( darwinDictGetU64(PropsRef, CFSTR("sessionID"), &u64CurSessionId)
1056 && u64CurSessionId == u64SessionId))
1057 && ( !u32LocationId
1058 || ( darwinDictGetU32(PropsRef, CFSTR(kUSBDevicePropertyLocationID), &u32CurLocationId)
1059 && u32CurLocationId == u32LocationId))
1060 )
1061 {
1062 CFRelease(PropsRef);
1063 break;
1064 }
1065 CFRelease(PropsRef);
1066 }
1067 IOObjectRelease(USBDevice);
1068 }
1069 IOObjectRelease(USBDevices);
1070 USBDevices = NULL;
1071 if (!USBDevice)
1072 {
1073 LogRel(("USB: Device '%s' not found (%d pid+vid matches)\n", pszAddress, cMatches));
1074 IOObjectRelease(USBDevices);
1075 return VERR_VUSB_DEVICE_NAME_NOT_FOUND;
1076 }
1077
1078 /*
1079 * Create a plugin interface for the device and query its IOUSBDeviceInterface.
1080 */
1081 SInt32 Score = 0;
1082 IOCFPlugInInterface **ppPlugInInterface = NULL;
1083 irc = IOCreatePlugInInterfaceForService(USBDevice, kIOUSBDeviceUserClientTypeID,
1084 kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
1085 if (irc == kIOReturnSuccess)
1086 {
1087 IOUSBDeviceInterface245 **ppDevI = NULL;
1088 HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
1089 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245),
1090 (LPVOID *)&ppDevI);
1091 irc = IODestroyPlugInInterface(ppPlugInInterface); Assert(irc == kIOReturnSuccess);
1092 ppPlugInInterface = NULL;
1093 if (hrc == S_OK)
1094 {
1095 /*
1096 * Try open the device for exclusive access.
1097 */
1098 irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
1099 if (irc == kIOReturnExclusiveAccess)
1100 {
1101 RTThreadSleep(20);
1102 irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
1103 }
1104 if (irc == kIOReturnSuccess)
1105 {
1106 /*
1107 * Re-enumerate the device and bail out.
1108 */
1109 irc = (*ppDevI)->USBDeviceReEnumerate(ppDevI, 0);
1110 if (irc == kIOReturnSuccess)
1111 vrc = VINF_SUCCESS;
1112 else
1113 {
1114 LogRel(("USB: Failed to open device '%s', plug-in creation failed with irc=%#x.\n", pszAddress, irc));
1115 vrc = RTErrConvertFromDarwinIO(irc);
1116 }
1117
1118 (*ppDevI)->USBDeviceClose(ppDevI);
1119 }
1120 else if (irc == kIOReturnExclusiveAccess)
1121 {
1122 LogRel(("USB: Device '%s' is being used by another process\n", pszAddress));
1123 vrc = VERR_SHARING_VIOLATION;
1124 }
1125 else
1126 {
1127 LogRel(("USB: Failed to open device '%s', irc=%#x.\n", pszAddress, irc));
1128 vrc = VERR_OPEN_FAILED;
1129 }
1130 }
1131 else
1132 {
1133 LogRel(("USB: Failed to create plugin interface for device '%s', hrc=%#x.\n", pszAddress, hrc));
1134 vrc = VERR_OPEN_FAILED;
1135 }
1136
1137 (*ppDevI)->Release(ppDevI);
1138 }
1139 else
1140 {
1141 LogRel(("USB: Failed to open device '%s', plug-in creation failed with irc=%#x.\n", pszAddress, irc));
1142 vrc = RTErrConvertFromDarwinIO(irc);
1143 }
1144
1145 return vrc;
1146}
1147
1148#endif /* VBOX_WITH_USB */
1149
1150
1151/**
1152 * Enumerate the DVD drives returning a FIFO of device name strings.
1153 *
1154 * @returns Pointer to the head.
1155 * The caller is responsible for calling RTMemFree() on each of the nodes.
1156 */
1157PDARWINDVD DarwinGetDVDDrives(void)
1158{
1159 AssertReturn(darwinOpenMasterPort(), NULL);
1160
1161 /*
1162 * Create a matching dictionary for searching for DVD services in the IOKit.
1163 *
1164 * [If I understand this correctly, plain CDROMs doesn't show up as
1165 * IODVDServices. Too keep things simple, we will only support DVDs
1166 * until somebody complains about it and we get hardware to test it on.
1167 * (Unless I'm much mistaken, there aren't any (orignal) intel macs with
1168 * plain cdroms.)]
1169 */
1170 CFMutableDictionaryRef RefMatchingDict = IOServiceMatching("IODVDServices");
1171 AssertReturn(RefMatchingDict, NULL);
1172
1173 /*
1174 * Perform the search and get a collection of DVD services.
1175 */
1176 io_iterator_t DVDServices = NULL;
1177 IOReturn rc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &DVDServices);
1178 AssertMsgReturn(rc == kIOReturnSuccess, ("rc=%d\n", rc), NULL);
1179 RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
1180
1181 /*
1182 * Enumerate the DVD services.
1183 * (This enumeration must be identical to the one performed in DrvHostBase.cpp.)
1184 */
1185 PDARWINDVD pHead = NULL;
1186 PDARWINDVD pTail = NULL;
1187 unsigned i = 0;
1188 io_object_t DVDService;
1189 while ((DVDService = IOIteratorNext(DVDServices)) != 0)
1190 {
1191 /*
1192 * Get the properties we use to identify the DVD drive.
1193 *
1194 * While there is a (weird 12 byte) GUID, it isn't persistent
1195 * accross boots. So, we have to use a combination of the
1196 * vendor name and product name properties with an optional
1197 * sequence number for identification.
1198 */
1199 CFMutableDictionaryRef PropsRef = 0;
1200 kern_return_t krc = IORegistryEntryCreateCFProperties(DVDService, &PropsRef, kCFAllocatorDefault, kNilOptions);
1201 if (krc == KERN_SUCCESS)
1202 {
1203 /* Get the Device Characteristics dictionary. */
1204 CFDictionaryRef DevCharRef = (CFDictionaryRef)CFDictionaryGetValue(PropsRef, CFSTR(kIOPropertyDeviceCharacteristicsKey));
1205 if (DevCharRef)
1206 {
1207 /* The vendor name. */
1208 char szVendor[128];
1209 char *pszVendor = &szVendor[0];
1210 CFTypeRef ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyVendorNameKey));
1211 if ( ValueRef
1212 && CFGetTypeID(ValueRef) == CFStringGetTypeID()
1213 && CFStringGetCString((CFStringRef)ValueRef, szVendor, sizeof(szVendor), kCFStringEncodingUTF8))
1214 pszVendor = RTStrStrip(szVendor);
1215 else
1216 *pszVendor = '\0';
1217
1218 /* The product name. */
1219 char szProduct[128];
1220 char *pszProduct = &szProduct[0];
1221 ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyProductNameKey));
1222 if ( ValueRef
1223 && CFGetTypeID(ValueRef) == CFStringGetTypeID()
1224 && CFStringGetCString((CFStringRef)ValueRef, szProduct, sizeof(szProduct), kCFStringEncodingUTF8))
1225 pszProduct = RTStrStrip(szProduct);
1226 else
1227 *pszProduct = '\0';
1228
1229 /* Construct the name and check for duplicates. */
1230 char szName[256 + 32];
1231 if (*pszVendor || *pszProduct)
1232 {
1233 if (*pszVendor && *pszProduct)
1234 RTStrPrintf(szName, sizeof(szName), "%s %s", pszVendor, pszProduct);
1235 else
1236 strcpy(szName, *pszVendor ? pszVendor : pszProduct);
1237
1238 for (PDARWINDVD pCur = pHead; pCur; pCur = pCur->pNext)
1239 {
1240 if (!strcmp(szName, pCur->szName))
1241 {
1242 if (*pszVendor && *pszProduct)
1243 RTStrPrintf(szName, sizeof(szName), "%s %s (#%u)", pszVendor, pszProduct, i);
1244 else
1245 RTStrPrintf(szName, sizeof(szName), "%s %s (#%u)", *pszVendor ? pszVendor : pszProduct, i);
1246 break;
1247 }
1248 }
1249 }
1250 else
1251 RTStrPrintf(szName, sizeof(szName), "(#%u)", i);
1252
1253 /* Create the device. */
1254 size_t cbName = strlen(szName) + 1;
1255 PDARWINDVD pNew = (PDARWINDVD)RTMemAlloc(RT_OFFSETOF(DARWINDVD, szName[cbName]));
1256 if (pNew)
1257 {
1258 pNew->pNext = NULL;
1259 memcpy(pNew->szName, szName, cbName);
1260 if (pTail)
1261 pTail = pTail->pNext = pNew;
1262 else
1263 pTail = pHead = pNew;
1264 }
1265 }
1266 CFRelease(PropsRef);
1267 }
1268 else
1269 AssertMsgFailed(("krc=%#x\n", krc));
1270
1271 IOObjectRelease(DVDService);
1272 i++;
1273 }
1274
1275 IOObjectRelease(DVDServices);
1276
1277 return pHead;
1278}
1279
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette