VirtualBox

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

Last change on this file since 3801 was 3764, checked in by vboxsync, 18 years ago

Drop holdDevice(). darwin changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 46.5 KB
Line 
1/* $Id: iokit.cpp 3764 2007-07-22 13:19:02Z 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 innotek GmbH
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 as published by the Free Software Foundation,
17 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
18 * distribution. VirtualBox OSE is distributed in the hope that it will
19 * be useful, but WITHOUT ANY WARRANTY of any kind.
20 *
21 * If you received this file as part of a commercial VirtualBox
22 * distribution, then only the terms of your commercial VirtualBox
23 * license agreement apply instead of the previous paragraph.
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.
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 RTPROCESS Owner = NIL_RTPROCESS;
664 RTPROCESS Client = NIL_RTPROCESS;
665 bool fUserClientOnly = true;
666 bool fConfigured = false;
667 bool fInUse = false;
668 bool fSeizable = true;
669 io_object_t Interface;
670 while ((Interface = IOIteratorNext(Interfaces)))
671 {
672 io_name_t szName;
673 krc = IOObjectGetClass(Interface, szName);
674 if ( krc == KERN_SUCCESS
675 && !strcmp(szName, "IOUSBInterface"))
676 {
677 fConfigured = true;
678
679 /*
680 * Iterate the interface children looking for stuff other than
681 * IOUSBUserClientInit objects.
682 */
683 io_iterator_t Children1;
684 krc = IORegistryEntryGetChildIterator(Interface, kIOServicePlane, &Children1);
685 if (krc == KERN_SUCCESS)
686 {
687 io_object_t Child1;
688 while ((Child1 = IOIteratorNext(Children1)))
689 {
690 krc = IOObjectGetClass(Child1, szName);
691 if ( krc == KERN_SUCCESS
692 && strcmp(szName, "IOUSBUserClientInit"))
693 {
694 fUserClientOnly = false;
695
696 if (!strcmp(szName, "IOUSBMassStorageClass"))
697 {
698 /* Only permit capturing MSDs that aren't mounted, at least
699 until the GUI starts poping up warnings about data loss
700 and such when capturing a busy device. */
701 fSeizable = false;
702 fInUse |= darwinIsMassStorageInterfaceInUse(Child1, szName);
703 }
704 else if (!strcmp(szName, "IOUSBHIDDriver")
705 || !strcmp(szName, "AppleHIDMouse")
706 /** @todo more? */)
707 {
708 /* For now, just assume that all HID devices are inaccessible
709 because of the greedy HID service. */
710 fSeizable = false;
711 fInUse = true;
712 }
713 else
714 fInUse = true;
715 }
716 IOObjectRelease(Child1);
717 }
718 IOObjectRelease(Children1);
719 }
720 }
721 /*
722 * Not an interface, could it be VBoxUSBDevice?
723 * If it is, get the owner and client properties.
724 */
725 else if ( krc == KERN_SUCCESS
726 && !strcmp(szName, VBOXUSBDEVICE_CLASS_NAME))
727 {
728 CFMutableDictionaryRef PropsRef = 0;
729 krc = IORegistryEntryCreateCFProperties(Interface, &PropsRef, kCFAllocatorDefault, kNilOptions);
730 if (krc == KERN_SUCCESS)
731 {
732 darwinDictGetProccess(PropsRef, CFSTR(VBOXUSB_OWNER_KEY), &Owner);
733 darwinDictGetProccess(PropsRef, CFSTR(VBOXUSB_CLIENT_KEY), &Client);
734 CFRelease(PropsRef);
735 }
736 }
737
738 IOObjectRelease(Interface);
739 }
740 IOObjectRelease(Interfaces);
741
742 /*
743 * Calc the status.
744 */
745 if ( Owner != NIL_RTPROCESS
746 && !Owner)
747 {
748 if (Owner == RTProcSelf())
749 pCur->enmState = Client == NIL_RTPROCESS || !Client
750 ? USBDEVICESTATE_HELD_BY_PROXY
751 : USBDEVICESTATE_USED_BY_GUEST;
752 else
753 pCur->enmState = USBDEVICESTATE_USED_BY_HOST;
754 }
755 else if (fUserClientOnly)
756 /** @todo how to detect other user client?!? - Look for IOUSBUserClient! */
757 pCur->enmState = !fConfigured
758 ? USBDEVICESTATE_UNUSED
759 : USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
760 else if (!fInUse)
761 pCur->enmState = USBDEVICESTATE_UNUSED;
762 else
763 pCur->enmState = fSeizable
764 ? USBDEVICESTATE_USED_BY_HOST_CAPTURABLE
765 : USBDEVICESTATE_USED_BY_HOST;
766}
767
768
769/**
770 * Enumerate the USB devices returning a FIFO of them.
771 *
772 * @returns Pointer to the head.
773 * USBProxyService::freeDevice is expected to free each of the list elements.
774 */
775PUSBDEVICE DarwinGetUSBDevices(void)
776{
777 AssertReturn(darwinOpenMasterPort(), NULL);
778 //DARWIN_IOKIT_LOG(("DarwinGetUSBDevices\n"));
779
780 /*
781 * Create a matching dictionary for searching for USB Devices in the IOKit.
782 */
783 CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOUSBDeviceClassName);
784 AssertReturn(RefMatchingDict, NULL);
785
786 /*
787 * Perform the search and get a collection of USB Device back.
788 */
789 io_iterator_t USBDevices = NULL;
790 IOReturn rc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &USBDevices);
791 AssertMsgReturn(rc == kIOReturnSuccess, ("rc=%d\n", rc), NULL);
792 RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
793
794 /*
795 * Enumerate the USB Devices.
796 */
797 PUSBDEVICE pHead = NULL;
798 PUSBDEVICE pTail = NULL;
799 unsigned i = 0;
800 io_object_t USBDevice;
801 while ((USBDevice = IOIteratorNext(USBDevices)) != 0)
802 {
803 //DARWIN_IOKIT_DUMP_OBJ(USBDevice);
804
805 /*
806 * Query the device properties from the registry.
807 *
808 * We could alternatively use the device and such, but that will be
809 * slower and we would have to resort to the registry for the three
810 * string anyway.
811 */
812 CFMutableDictionaryRef PropsRef = 0;
813 kern_return_t krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
814 if (krc == KERN_SUCCESS)
815 {
816 bool fOk = false;
817 PUSBDEVICE pCur = (PUSBDEVICE)RTMemAllocZ(sizeof(*pCur));
818 do /* loop for breaking out of on failure. */
819 {
820 AssertBreak(pCur,);
821
822 /*
823 * Mandatory
824 */
825 pCur->bcdUSB = 0; /* we've no idea. */
826 pCur->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE; /* just a default, we'll try harder in a bit. */
827
828 AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDeviceClass), &pCur->bDeviceClass),);
829 /* skip hubs */
830 if (pCur->bDeviceClass == 0x09 /* hub, find a define! */)
831 break;
832 AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDeviceSubClass), &pCur->bDeviceSubClass),);
833 AssertBreak(darwinDictGetU8(PropsRef, CFSTR(kUSBDeviceProtocol), &pCur->bDeviceProtocol),);
834 AssertBreak(darwinDictGetU16(PropsRef, CFSTR(kUSBVendorID), &pCur->idVendor),);
835 AssertBreak(darwinDictGetU16(PropsRef, CFSTR(kUSBProductID), &pCur->idProduct),);
836 AssertBreak(darwinDictGetU16(PropsRef, CFSTR(kUSBDeviceReleaseNumber), &pCur->bcdDevice),);
837 uint32_t u32LocationId;
838 AssertBreak(darwinDictGetU32(PropsRef, CFSTR(kUSBDevicePropertyLocationID), &u32LocationId),);
839 uint64_t u64SessionId;
840 AssertBreak(darwinDictGetU64(PropsRef, CFSTR("sessionID"), &u64SessionId),);
841 char szAddress[64];
842 RTStrPrintf(szAddress, sizeof(szAddress), "p=0x%04RX16;v=0x%04RX16;s=0x%016RX64;l=0x%08RX32",
843 pCur->idProduct, pCur->idVendor, u64SessionId, u32LocationId);
844 pCur->pszAddress = RTStrDup(szAddress);
845 AssertBreak(pCur->pszAddress,);
846
847 /*
848 * Optional.
849 * There are some nameless device in the iMac, apply names to them.
850 */
851 darwinDictGetString(PropsRef, CFSTR("USB Vendor Name"), (char **)&pCur->pszManufacturer);
852 if ( !pCur->pszManufacturer
853 && pCur->idVendor == kIOUSBVendorIDAppleComputer)
854 pCur->pszManufacturer = RTStrDup("Apple Computer, Inc.");
855 darwinDictGetString(PropsRef, CFSTR("USB Product Name"), (char **)&pCur->pszProduct);
856 if ( !pCur->pszProduct
857 && pCur->bDeviceClass == 224 /* Wireless */
858 && pCur->bDeviceSubClass == 1 /* Radio Frequency */
859 && pCur->bDeviceProtocol == 1 /* Bluetooth */)
860 pCur->pszProduct = RTStrDup("Bluetooth");
861 darwinDictGetString(PropsRef, CFSTR("USB Serial Number"), (char **)&pCur->pszSerialNumber);
862
863#if 0 /* leave the remainder as zero for now. */
864 /*
865 * Create a plugin interface for the service and query its USB Device interface.
866 */
867 SInt32 Score = 0;
868 IOCFPlugInInterface **ppPlugInInterface = NULL;
869 rc = IOCreatePlugInInterfaceForService(USBDevice, kIOUSBDeviceUserClientTypeID,
870 kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
871 if (rc == kIOReturnSuccess)
872 {
873 IOUSBDeviceInterface245 **ppUSBDevI = NULL;
874 HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
875 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245),
876 (LPVOID *)&ppUSBDevI);
877 rc = IODestroyPlugInInterface(ppPlugInInterface); Assert(rc == kIOReturnSuccess);
878 ppPlugInInterface = NULL;
879 if (hrc == S_OK)
880 {
881 /** @todo enumerate configurations and interfaces if we actually need them. */
882 //IOReturn (*GetNumberOfConfigurations)(void *self, UInt8 *numConfig);
883 //IOReturn (*GetConfigurationDescriptorPtr)(void *self, UInt8 configIndex, IOUSBConfigurationDescriptorPtr *desc);
884 //IOReturn (*CreateInterfaceIterator)(void *self, IOUSBFindInterfaceRequest *req, io_iterator_t *iter);
885 }
886 long cReft = (*ppUSBDeviceInterface)->Release(ppUSBDeviceInterface); MY_CHECK_CREFS(cRefs);
887 }
888#endif
889 /*
890 * Try determin the state.
891 */
892 darwinDeterminUSBDeviceState(pCur, USBDevice, PropsRef);
893
894 /*
895 * We're good. Link the device.
896 */
897 pCur->pPrev = pTail;
898 if (pTail)
899 pTail = pTail->pNext = pCur;
900 else
901 pTail = pHead = pCur;
902 fOk = true;
903 } while (0);
904
905 /* cleanup on failure / skipped device. */
906 if (!fOk && pCur)
907 DarwinFreeUSBDeviceFromIOKit(pCur);
908
909 CFRelease(PropsRef);
910 }
911 else
912 AssertMsgFailed(("krc=%#x\n", krc));
913
914 IOObjectRelease(USBDevice);
915 i++;
916 }
917
918 IOObjectRelease(USBDevices);
919 //DARWIN_IOKIT_LOG_FLUSH();
920
921 /*
922 * Some post processing. There are a couple of things we have to
923 * make 100% sure about, and that is that the (Apple) keyboard
924 * and mouse most likely to be in use by the user aren't available
925 * for capturing. If there is no Apple mouse or keyboard we'll
926 * take the first one from another vendor.
927 */
928 /* As it turns out, the HID service will take all keyboards and mice
929 and we're not currently able to seize them. */
930 PUSBDEVICE pMouse = NULL;
931 PUSBDEVICE pKeyboard = NULL;
932 for (PUSBDEVICE pCur = pHead; pCur; pCur = pCur->pNext)
933 if (pCur->idVendor == kIOUSBVendorIDAppleComputer)
934 {
935 /*
936 * This test is a bit rough, should check device class/protocol but
937 * we don't have interface info yet so that might be a bit tricky.
938 */
939 if ( ( !pKeyboard
940 || pKeyboard->idVendor != kIOUSBVendorIDAppleComputer)
941 && pCur->pszProduct
942 && strstr(pCur->pszProduct, " Keyboard"))
943 pKeyboard = pCur;
944 else if ( ( !pMouse
945 || pMouse->idVendor != kIOUSBVendorIDAppleComputer)
946 && pCur->pszProduct
947 && strstr(pCur->pszProduct, " Mouse")
948 )
949 pMouse = pCur;
950 }
951 else if (!pKeyboard || !pMouse)
952 {
953 if ( pCur->bDeviceClass == 3 /* HID */
954 && pCur->bDeviceProtocol == 1 /* Keyboard */)
955 pKeyboard = pCur;
956 else if ( pCur->bDeviceClass == 3 /* HID */
957 && pCur->bDeviceProtocol == 2 /* Mouse */)
958 pMouse = pCur;
959 /** @todo examin interfaces */
960 }
961
962 if (pKeyboard)
963 pKeyboard->enmState = USBDEVICESTATE_USED_BY_HOST;
964 if (pMouse)
965 pMouse->enmState = USBDEVICESTATE_USED_BY_HOST;
966
967 return pHead;
968}
969
970
971/**
972 * Triggers re-enumeration of a device.
973 *
974 * @returns VBox status code.
975 * @param pCur The USBDEVICE structure for the device.
976 */
977int DarwinReEnumerateUSBDevice(PCUSBDEVICE pCur)
978{
979 int vrc;
980 const char *pszAddress = pCur->pszAddress;
981 AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
982 AssertReturn(darwinOpenMasterPort(), VERR_GENERAL_FAILURE);
983
984 /*
985 * This code is a short version of the Open method in USBProxyDevice-darwin.cpp stuff.
986 * Fixes made to this code probably applies there too!
987 */
988
989 CFMutableDictionaryRef RefMatchingDict = IOServiceMatching(kIOUSBDeviceClassName);
990 AssertReturn(RefMatchingDict, NULL);
991
992 uint64_t u64SessionId = 0;
993 uint32_t u32LocationId = 0;
994 const char *psz = pszAddress;
995 do
996 {
997 const char chValue = *psz;
998 AssertReleaseReturn(psz[1] == '=', VERR_INTERNAL_ERROR);
999 uint64_t u64Value;
1000 int rc = RTStrToUInt64Ex(psz + 2, (char **)&psz, 0, &u64Value);
1001 AssertReleaseRCReturn(rc, rc);
1002 AssertReleaseReturn(!*psz || *psz == ';', rc);
1003 switch (chValue)
1004 {
1005 case 'l':
1006 u32LocationId = (uint32_t)u64Value;
1007 break;
1008 case 's':
1009 u64SessionId = u64Value;
1010 break;
1011 case 'p':
1012 case 'v':
1013 {
1014#if 0 /* Guess what, this doesn't 'ing work either! */
1015 SInt32 i32 = (int16_t)u64Value;
1016 CFNumberRef Num = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &i32);
1017 AssertBreak(Num,);
1018 CFDictionarySetValue(RefMatchingDict, chValue == 'p' ? CFSTR(kUSBProductID) : CFSTR(kUSBVendorID), Num);
1019 CFRelease(Num);
1020#endif
1021 break;
1022 }
1023 default:
1024 AssertReleaseMsgFailedReturn(("chValue=%#x\n", chValue), VERR_INTERNAL_ERROR);
1025 }
1026 if (*psz == ';')
1027 psz++;
1028 } while (*psz);
1029
1030 io_iterator_t USBDevices = NULL;
1031 IOReturn irc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &USBDevices);
1032 AssertMsgReturn(irc == kIOReturnSuccess, ("irc=%#x\n", irc), NULL);
1033 RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
1034
1035 unsigned cMatches = 0;
1036 io_object_t USBDevice;
1037 while ((USBDevice = IOIteratorNext(USBDevices)))
1038 {
1039 cMatches++;
1040 CFMutableDictionaryRef PropsRef = 0;
1041 kern_return_t krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
1042 if (krc == KERN_SUCCESS)
1043 {
1044 uint64_t u64CurSessionId;
1045 uint32_t u32CurLocationId;
1046 if ( ( !u64SessionId
1047 || ( darwinDictGetU64(PropsRef, CFSTR("sessionID"), &u64CurSessionId)
1048 && u64CurSessionId == u64SessionId))
1049 && ( !u32LocationId
1050 || ( darwinDictGetU32(PropsRef, CFSTR(kUSBDevicePropertyLocationID), &u32CurLocationId)
1051 && u32CurLocationId == u32LocationId))
1052 )
1053 {
1054 CFRelease(PropsRef);
1055 break;
1056 }
1057 CFRelease(PropsRef);
1058 }
1059 IOObjectRelease(USBDevice);
1060 }
1061 IOObjectRelease(USBDevices);
1062 USBDevices = NULL;
1063 if (!USBDevice)
1064 {
1065 LogRel(("USB: Device '%s' not found (%d pid+vid matches)\n", pszAddress, cMatches));
1066 IOObjectRelease(USBDevices);
1067 return VERR_VUSB_DEVICE_NAME_NOT_FOUND;
1068 }
1069
1070 /*
1071 * Create a plugin interface for the device and query its IOUSBDeviceInterface.
1072 */
1073 SInt32 Score = 0;
1074 IOCFPlugInInterface **ppPlugInInterface = NULL;
1075 irc = IOCreatePlugInInterfaceForService(USBDevice, kIOUSBDeviceUserClientTypeID,
1076 kIOCFPlugInInterfaceID, &ppPlugInInterface, &Score);
1077 if (irc == kIOReturnSuccess)
1078 {
1079 IOUSBDeviceInterface245 **ppDevI = NULL;
1080 HRESULT hrc = (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
1081 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245),
1082 (LPVOID *)&ppDevI);
1083 irc = IODestroyPlugInInterface(ppPlugInInterface); Assert(irc == kIOReturnSuccess);
1084 ppPlugInInterface = NULL;
1085 if (hrc == S_OK)
1086 {
1087 /*
1088 * Try open the device for exclusive access.
1089 */
1090 irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
1091 if (irc == kIOReturnExclusiveAccess)
1092 {
1093 RTThreadSleep(20);
1094 irc = (*ppDevI)->USBDeviceOpenSeize(ppDevI);
1095 }
1096 if (irc == kIOReturnSuccess)
1097 {
1098 /*
1099 * Re-enumerate the device and bail out.
1100 */
1101 irc = (*ppDevI)->USBDeviceReEnumerate(ppDevI, 0);
1102 if (irc != kIOReturnSuccess)
1103 {
1104 LogRel(("USB: Failed to open device '%s', plug-in creation failed with irc=%#x.\n", pszAddress, irc));
1105 vrc = RTErrConvertFromDarwinIO(irc);
1106 }
1107
1108 (*ppDevI)->USBDeviceClose(ppDevI);
1109 }
1110 else if (irc == kIOReturnExclusiveAccess)
1111 {
1112 LogRel(("USB: Device '%s' is being used by another process\n", pszAddress));
1113 vrc = VERR_SHARING_VIOLATION;
1114 }
1115 else
1116 {
1117 LogRel(("USB: Failed to open device '%s', irc=%#x.\n", pszAddress, irc));
1118 vrc = VERR_OPEN_FAILED;
1119 }
1120 }
1121 else
1122 {
1123 LogRel(("USB: Failed to create plugin interface for device '%s', hrc=%#x.\n", pszAddress, hrc));
1124 vrc = VERR_OPEN_FAILED;
1125 }
1126
1127 (*ppDevI)->Release(ppDevI);
1128 }
1129 else
1130 {
1131 LogRel(("USB: Failed to open device '%s', plug-in creation failed with irc=%#x.\n", pszAddress, irc));
1132 vrc = RTErrConvertFromDarwinIO(irc);
1133 }
1134
1135 return vrc;
1136}
1137
1138#endif /* VBOX_WITH_USB */
1139
1140
1141/**
1142 * Enumerate the DVD drives returning a FIFO of device name strings.
1143 *
1144 * @returns Pointer to the head.
1145 * The caller is responsible for calling RTMemFree() on each of the nodes.
1146 */
1147PDARWINDVD DarwinGetDVDDrives(void)
1148{
1149 AssertReturn(darwinOpenMasterPort(), NULL);
1150
1151 /*
1152 * Create a matching dictionary for searching for DVD services in the IOKit.
1153 *
1154 * [If I understand this correctly, plain CDROMs doesn't show up as
1155 * IODVDServices. Too keep things simple, we will only support DVDs
1156 * until somebody complains about it and we get hardware to test it on.
1157 * (Unless I'm much mistaken, there aren't any (orignal) intel macs with
1158 * plain cdroms.)]
1159 */
1160 CFMutableDictionaryRef RefMatchingDict = IOServiceMatching("IODVDServices");
1161 AssertReturn(RefMatchingDict, NULL);
1162
1163 /*
1164 * Perform the search and get a collection of DVD services.
1165 */
1166 io_iterator_t DVDServices = NULL;
1167 IOReturn rc = IOServiceGetMatchingServices(g_MasterPort, RefMatchingDict, &DVDServices);
1168 AssertMsgReturn(rc == kIOReturnSuccess, ("rc=%d\n", rc), NULL);
1169 RefMatchingDict = NULL; /* the reference is consumed by IOServiceGetMatchingServices. */
1170
1171 /*
1172 * Enumerate the DVD services.
1173 * (This enumeration must be identical to the one performed in DrvHostBase.cpp.)
1174 */
1175 PDARWINDVD pHead = NULL;
1176 PDARWINDVD pTail = NULL;
1177 unsigned i = 0;
1178 io_object_t DVDService;
1179 while ((DVDService = IOIteratorNext(DVDServices)) != 0)
1180 {
1181 /*
1182 * Get the properties we use to identify the DVD drive.
1183 *
1184 * While there is a (weird 12 byte) GUID, it isn't persistent
1185 * accross boots. So, we have to use a combination of the
1186 * vendor name and product name properties with an optional
1187 * sequence number for identification.
1188 */
1189 CFMutableDictionaryRef PropsRef = 0;
1190 kern_return_t krc = IORegistryEntryCreateCFProperties(DVDService, &PropsRef, kCFAllocatorDefault, kNilOptions);
1191 if (krc == KERN_SUCCESS)
1192 {
1193 /* Get the Device Characteristics dictionary. */
1194 CFDictionaryRef DevCharRef = (CFDictionaryRef)CFDictionaryGetValue(PropsRef, CFSTR(kIOPropertyDeviceCharacteristicsKey));
1195 if (DevCharRef)
1196 {
1197 /* The vendor name. */
1198 char szVendor[128];
1199 char *pszVendor = &szVendor[0];
1200 CFTypeRef ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyVendorNameKey));
1201 if ( ValueRef
1202 && CFGetTypeID(ValueRef) == CFStringGetTypeID()
1203 && CFStringGetCString((CFStringRef)ValueRef, szVendor, sizeof(szVendor), kCFStringEncodingUTF8))
1204 pszVendor = RTStrStrip(szVendor);
1205 else
1206 *pszVendor = '\0';
1207
1208 /* The product name. */
1209 char szProduct[128];
1210 char *pszProduct = &szProduct[0];
1211 ValueRef = CFDictionaryGetValue(DevCharRef, CFSTR(kIOPropertyProductNameKey));
1212 if ( ValueRef
1213 && CFGetTypeID(ValueRef) == CFStringGetTypeID()
1214 && CFStringGetCString((CFStringRef)ValueRef, szProduct, sizeof(szProduct), kCFStringEncodingUTF8))
1215 pszProduct = RTStrStrip(szProduct);
1216 else
1217 *pszProduct = '\0';
1218
1219 /* Construct the name and check for duplicates. */
1220 char szName[256 + 32];
1221 if (*pszVendor || *pszProduct)
1222 {
1223 if (*pszVendor && *pszProduct)
1224 RTStrPrintf(szName, sizeof(szName), "%s %s", pszVendor, pszProduct);
1225 else
1226 strcpy(szName, *pszVendor ? pszVendor : pszProduct);
1227
1228 for (PDARWINDVD pCur = pHead; pCur; pCur = pCur->pNext)
1229 {
1230 if (!strcmp(szName, pCur->szName))
1231 {
1232 if (*pszVendor && *pszProduct)
1233 RTStrPrintf(szName, sizeof(szName), "%s %s (#%u)", pszVendor, pszProduct, i);
1234 else
1235 RTStrPrintf(szName, sizeof(szName), "%s %s (#%u)", *pszVendor ? pszVendor : pszProduct, i);
1236 break;
1237 }
1238 }
1239 }
1240 else
1241 RTStrPrintf(szName, sizeof(szName), "(#%u)", i);
1242
1243 /* Create the device. */
1244 size_t cbName = strlen(szName) + 1;
1245 PDARWINDVD pNew = (PDARWINDVD)RTMemAlloc(RT_OFFSETOF(DARWINDVD, szName[cbName]));
1246 if (pNew)
1247 {
1248 pNew->pNext = NULL;
1249 memcpy(pNew->szName, szName, cbName);
1250 if (pTail)
1251 pTail = pTail->pNext = pNew;
1252 else
1253 pTail = pHead = pNew;
1254 }
1255 }
1256 CFRelease(PropsRef);
1257 }
1258 else
1259 AssertMsgFailed(("krc=%#x\n", krc));
1260
1261 IOObjectRelease(DVDService);
1262 i++;
1263 }
1264
1265 IOObjectRelease(DVDServices);
1266
1267 return pHead;
1268}
1269
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