nkeynes@964: /** nkeynes@964: * $Id$ nkeynes@964: * nkeynes@964: * OSX support functions for handling the IOKit registry. nkeynes@964: * Currently this manages access to CD/DVD drives + media, plus HID devices. nkeynes@964: * nkeynes@964: * The HID part is much simpler... nkeynes@964: * nkeynes@964: * Copyright (c) 2008 Nathan Keynes. nkeynes@964: * nkeynes@964: * This program is free software; you can redistribute it and/or modify nkeynes@964: * it under the terms of the GNU General Public License as published by nkeynes@964: * the Free Software Foundation; either version 2 of the License, or nkeynes@964: * (at your option) any later version. nkeynes@964: * nkeynes@964: * This program is distributed in the hope that it will be useful, nkeynes@964: * but WITHOUT ANY WARRANTY; without even the implied warranty of nkeynes@964: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nkeynes@964: * GNU General Public License for more details. nkeynes@964: */ nkeynes@964: nkeynes@1297: #include nkeynes@964: #include nkeynes@964: #include nkeynes@964: #include nkeynes@964: #include nkeynes@964: #include nkeynes@964: #include nkeynes@964: #include nkeynes@964: #include nkeynes@964: #include nkeynes@964: #include "osx_iokit.h" nkeynes@964: nkeynes@964: nkeynes@964: nkeynes@964: static IONotificationPortRef notify_port = 0; nkeynes@964: static io_iterator_t iokit_iterators[4] = {0,0,0,0}; nkeynes@964: nkeynes@964: struct osx_cdrom_drive { nkeynes@964: io_string_t ioservice_path; nkeynes@964: io_string_t vendor_name; nkeynes@964: io_string_t product_name; nkeynes@964: char media_path[MAXPATHLEN]; // BSD device path if media present, otherwise the empty string. nkeynes@964: io_iterator_t media_load_iterator; nkeynes@964: io_iterator_t media_unload_iterator; nkeynes@964: int media_fh; // BSD device handle if open, otherwise -1 nkeynes@964: media_changed_callback_t media_changed; nkeynes@964: void *media_changed_user_data; nkeynes@964: }; nkeynes@964: nkeynes@964: static gboolean get_bsdname_for_iomedia( io_object_t iomedia, char *buf, int buflen ); nkeynes@1023: static gboolean get_boolean_property( io_object_t io, CFStringRef key, gboolean def ); nkeynes@964: nkeynes@964: /***************** IOKit Callbacks ******************/ nkeynes@964: nkeynes@964: /** nkeynes@964: * Called from IOKit for any IOMessages on an IOMedia. Currently the only message nkeynes@964: * we're interested in is service termination. nkeynes@964: */ nkeynes@964: static void osx_cdrom_media_notify( void *ref, io_service_t service, uint32_t msgType, nkeynes@964: void *msgArgument ) nkeynes@964: { nkeynes@964: if( msgType == kIOMessageServiceIsTerminated ) { nkeynes@964: osx_cdrom_drive_t drive = (osx_cdrom_drive_t)ref; nkeynes@964: if( drive->media_changed != NULL ) { nkeynes@964: drive->media_changed( drive, FALSE, drive->media_changed_user_data ); nkeynes@964: } nkeynes@964: if( drive->media_fh != -1 ) { nkeynes@964: close(drive->media_fh); nkeynes@964: drive->media_fh = -1; nkeynes@964: } nkeynes@964: drive->media_path[0] = '\0'; nkeynes@964: IOObjectRelease( drive->media_unload_iterator ); nkeynes@964: } nkeynes@964: } nkeynes@964: nkeynes@964: /** nkeynes@964: * Called from IOKit when an IOMedia is inserted that we have be interested in. nkeynes@964: * FIXME: Can the matcher be restricted to descendents of the drive node? currently nkeynes@964: * we watch for all IOMedia events and compare the device path to see if it's one we nkeynes@964: * care about. nkeynes@964: * FIXME: We assume for now that a drive has at most one piece of media at a time. nkeynes@964: * If this isn't the case, the system may get a little confused. nkeynes@964: */ nkeynes@964: static void osx_cdrom_media_inserted( void *ref, io_iterator_t iterator ) nkeynes@964: { nkeynes@964: osx_cdrom_drive_t drive = (osx_cdrom_drive_t)ref; nkeynes@964: nkeynes@964: io_object_t object; nkeynes@964: while( (object = IOIteratorNext(iterator)) != 0 ) { nkeynes@964: io_string_t iopath = ""; nkeynes@964: IORegistryEntryGetPath( object, kIOServicePlane, iopath ); nkeynes@964: if( drive != NULL && g_str_has_prefix(iopath, drive->ioservice_path ) && nkeynes@1023: get_boolean_property(object, CFSTR("Whole"), TRUE) && nkeynes@1023: get_bsdname_for_iomedia(object, drive->media_path, sizeof(drive->media_path)) ) { nkeynes@964: // A disc was inserted within the drive of interest nkeynes@964: if( drive->media_fh != -1 ) { nkeynes@964: close(drive->media_fh); nkeynes@964: drive->media_fh = -1; nkeynes@964: } nkeynes@964: nkeynes@964: if( drive->media_changed != NULL ) { nkeynes@964: drive->media_changed(drive, TRUE, drive->media_changed_user_data); nkeynes@964: } nkeynes@964: // Add a notification listener to get removal events. nkeynes@964: IOServiceAddInterestNotification( notify_port, object, kIOGeneralInterest, nkeynes@964: osx_cdrom_media_notify, drive, &drive->media_unload_iterator ); nkeynes@964: nkeynes@964: } nkeynes@964: IOObjectRelease( object ); nkeynes@964: } nkeynes@964: } nkeynes@964: nkeynes@964: static void osx_drives_changed( void *ref, io_iterator_t iterator ) nkeynes@964: { nkeynes@964: io_object_t object; nkeynes@964: while( (object = IOIteratorNext(iterator)) != 0 ) { nkeynes@964: IOObjectRelease(object); nkeynes@964: } nkeynes@964: nkeynes@964: } nkeynes@964: nkeynes@964: /******************** Support functions *********************/ nkeynes@964: nkeynes@964: /** nkeynes@964: * Determine the BSD device name (ie "/dev/rdisk1") for a given IO object. nkeynes@964: * @return TRUE if the device name was retrieved, FALSE if the request failed. nkeynes@964: */ nkeynes@964: static gboolean get_bsdname_for_iomedia( io_object_t iomedia, char *buf, int buflen ) nkeynes@964: { nkeynes@964: gboolean result = FALSE; nkeynes@964: CFTypeRef pathRef = IORegistryEntryCreateCFProperty(iomedia, CFSTR(kIOBSDNameKey), nkeynes@964: kCFAllocatorDefault, 0 ); nkeynes@964: if( pathRef ) { nkeynes@964: char pathlen; nkeynes@964: strcpy( buf, _PATH_DEV "r" ); nkeynes@964: pathlen = strlen(buf); nkeynes@964: if( CFStringGetCString( pathRef, buf + pathlen, buflen-pathlen, nkeynes@964: kCFStringEncodingASCII ) != noErr ) { nkeynes@964: result = TRUE; nkeynes@964: } nkeynes@964: CFRelease(pathRef); nkeynes@964: } nkeynes@964: return result; nkeynes@964: } nkeynes@964: nkeynes@1023: /** nkeynes@1023: * Retrieve a boolean property from the io object, and return as a gboolean. If nkeynes@1023: * the key is not present, return def instead. nkeynes@1023: */ nkeynes@1023: static gboolean get_boolean_property( io_object_t io, CFStringRef key, gboolean def ) nkeynes@1023: { nkeynes@1023: gboolean result = def; nkeynes@1023: CFTypeRef ref = IORegistryEntryCreateCFProperty(io, key, kCFAllocatorDefault, 0 ); nkeynes@1023: if( ref ) { nkeynes@1023: result = CFBooleanGetValue(ref); nkeynes@1023: CFRelease(ref); nkeynes@1023: } nkeynes@1023: return result; nkeynes@1023: } nkeynes@1023: nkeynes@964: static gboolean osx_cdrom_drive_get_name( io_object_t object, char *vendor, int vendor_len, nkeynes@964: char *product, int product_len ) nkeynes@964: { nkeynes@964: gboolean result = FALSE; nkeynes@964: CFMutableDictionaryRef props = 0; nkeynes@964: if( IORegistryEntryCreateCFProperties(object, &props, kCFAllocatorDefault, kNilOptions) == KERN_SUCCESS ) { nkeynes@964: CFDictionaryRef dict = nkeynes@964: (CFDictionaryRef)CFDictionaryGetValue(props, CFSTR(kIOPropertyDeviceCharacteristicsKey)); nkeynes@964: if( dict != NULL ) { nkeynes@964: CFTypeRef value = CFDictionaryGetValue(dict, CFSTR(kIOPropertyVendorNameKey)); nkeynes@964: if( value && CFGetTypeID(value) == CFStringGetTypeID() ) { nkeynes@964: CFStringGetCString( (CFStringRef)value, vendor, vendor_len, kCFStringEncodingUTF8 ); nkeynes@964: } else { nkeynes@964: vendor[0] = 0; nkeynes@964: } nkeynes@964: nkeynes@964: value = CFDictionaryGetValue(dict, CFSTR(kIOPropertyProductNameKey)); nkeynes@964: if ( value && CFGetTypeID(value) == CFStringGetTypeID() ) { nkeynes@964: CFStringGetCString( (CFStringRef)value, product, product_len, kCFStringEncodingUTF8 ); nkeynes@964: } else { nkeynes@964: product[0] = 0; nkeynes@964: } nkeynes@964: result = TRUE; nkeynes@964: } nkeynes@964: nkeynes@964: CFRelease(props); nkeynes@964: } nkeynes@964: return result; nkeynes@964: } nkeynes@964: nkeynes@964: /** nkeynes@964: * Construct and initialize a new osx_cdrom_drive object, including registering nkeynes@964: * it's media inserted notification. nkeynes@964: */ nkeynes@964: static osx_cdrom_drive_t osx_cdrom_drive_new( io_object_t device ) nkeynes@964: { nkeynes@964: osx_cdrom_drive_t drive = g_malloc0(sizeof(struct osx_cdrom_drive)); nkeynes@964: nkeynes@964: IORegistryEntryGetPath( device, kIOServicePlane, drive->ioservice_path ); nkeynes@964: osx_cdrom_drive_get_name( device, drive->vendor_name, sizeof(drive->vendor_name), nkeynes@964: drive->product_name, sizeof(drive->product_name) ); nkeynes@964: drive->media_path[0] = '\0'; nkeynes@964: drive->media_changed = NULL; nkeynes@964: drive->media_changed_user_data = NULL; nkeynes@964: drive->media_fh = -1; nkeynes@964: nkeynes@964: IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification, nkeynes@964: IOServiceMatching("IOMedia"), nkeynes@964: osx_cdrom_media_inserted, drive, nkeynes@964: &drive->media_load_iterator ); nkeynes@964: osx_cdrom_media_inserted( drive, drive->media_load_iterator ); nkeynes@964: return drive; nkeynes@964: } nkeynes@964: nkeynes@964: /************************ Exported functions *************************/ nkeynes@964: nkeynes@964: osx_cdrom_drive_t osx_cdrom_open_drive( const char *devname ) nkeynes@964: { nkeynes@964: io_object_t object = IORegistryEntryFromPath( kIOMasterPortDefault, devname ); nkeynes@964: if( object == MACH_PORT_NULL ) { nkeynes@964: return NULL; nkeynes@964: } nkeynes@964: nkeynes@964: osx_cdrom_drive_t drive = osx_cdrom_drive_new( object ); nkeynes@964: IOObjectRelease( object ); nkeynes@964: return drive; nkeynes@964: } nkeynes@964: nkeynes@964: void osx_cdrom_set_media_changed_callback( osx_cdrom_drive_t drive, nkeynes@964: media_changed_callback_t callback, nkeynes@964: void *user_data ) nkeynes@964: { nkeynes@964: drive->media_changed = callback; nkeynes@964: drive->media_changed_user_data = user_data; nkeynes@964: } nkeynes@964: nkeynes@964: void osx_cdrom_close_drive( osx_cdrom_drive_t drive ) nkeynes@964: { nkeynes@964: IOObjectRelease( drive->media_load_iterator ); nkeynes@964: IOObjectRelease( drive->media_unload_iterator ); nkeynes@964: if( drive->media_fh != -1 ) { nkeynes@964: close(drive->media_fh); nkeynes@964: drive->media_fh = -1; nkeynes@964: } nkeynes@964: g_free( drive ); nkeynes@964: } nkeynes@964: nkeynes@964: int osx_cdrom_get_media_handle( osx_cdrom_drive_t drive ) nkeynes@964: { nkeynes@964: if( drive->media_fh == -1 ) { nkeynes@964: if( drive->media_path[0] != '\0' ) { nkeynes@964: drive->media_fh = open( drive->media_path, O_RDONLY|O_NONBLOCK ); nkeynes@964: } nkeynes@964: } nkeynes@964: return drive->media_fh; nkeynes@964: } nkeynes@964: nkeynes@964: void osx_cdrom_release_media_handle( osx_cdrom_drive_t drive ) nkeynes@964: { nkeynes@964: if( drive->media_fh != -1 ) { nkeynes@964: close( drive->media_fh ); nkeynes@964: drive->media_fh = -1; nkeynes@964: } nkeynes@964: } nkeynes@964: nkeynes@964: static io_object_t iterator_find_cdrom( io_object_t iterator, find_drive_callback_t callback, void *user_data ) nkeynes@964: { nkeynes@964: io_object_t object; nkeynes@964: while( (object = IOIteratorNext(iterator)) != 0 ) { nkeynes@964: io_string_t iopath = ""; nkeynes@964: char product[256], vendor[256]; nkeynes@964: IORegistryEntryGetPath( object, kIOServicePlane, iopath ); nkeynes@964: osx_cdrom_drive_get_name( object, vendor, sizeof(vendor), product, sizeof(product) ); nkeynes@964: if( callback( object, vendor, product, iopath, user_data ) ) { nkeynes@964: IOObjectRelease(iterator); nkeynes@964: return object; nkeynes@964: } nkeynes@964: IOObjectRelease(object); nkeynes@964: } nkeynes@964: IOObjectRelease(iterator); nkeynes@964: return 0; nkeynes@964: } nkeynes@964: nkeynes@964: nkeynes@964: /** nkeynes@964: * Search for a CD or DVD drive (instance of IODVDServices or IOCompactDiscServices). nkeynes@964: * The callback will be called repeatedly until either it returns TRUE, or all drives nkeynes@964: * have been iterated over. nkeynes@964: * nkeynes@964: * @return an IO registry entry for the matched drive, or 0 if no drives matched. nkeynes@964: * nkeynes@964: * Note: Use of IOCompactDiscServices is somewhat tentative since I don't have a Mac nkeynes@964: * with a CD-Rom drive. nkeynes@964: */ nkeynes@964: io_object_t find_cdrom_drive( find_drive_callback_t callback, void *user_data ) nkeynes@964: { nkeynes@964: mach_port_t master_port; nkeynes@964: CFMutableDictionaryRef match; nkeynes@964: io_iterator_t services; nkeynes@964: io_object_t result; nkeynes@964: nkeynes@964: if( IOMasterPort( MACH_PORT_NULL, &master_port ) != KERN_SUCCESS ) { nkeynes@964: return 0; // Failed to get the master port? nkeynes@964: } nkeynes@964: nkeynes@964: match = IOServiceMatching("IODVDServices"); nkeynes@964: if( IOServiceGetMatchingServices(master_port, match, &services) != kIOReturnSuccess ) { nkeynes@964: return 0; nkeynes@964: } nkeynes@964: nkeynes@964: result = iterator_find_cdrom( services, callback, user_data ); nkeynes@964: if( result != 0 ) { nkeynes@964: return result; nkeynes@964: } nkeynes@964: nkeynes@964: match = IOServiceMatching("IOCompactDiscServices"); nkeynes@964: if( IOServiceGetMatchingServices(master_port, match, &services) != kIOReturnSuccess ) { nkeynes@964: return 0; nkeynes@964: } nkeynes@964: return iterator_find_cdrom( services, callback, user_data ); nkeynes@964: } nkeynes@964: nkeynes@964: nkeynes@964: // *********************** Notification management ************************/ nkeynes@964: nkeynes@964: static void osx_hid_inserted( void *ref, io_iterator_t iterator ) nkeynes@964: { nkeynes@964: io_object_t object; nkeynes@964: while( (object = IOIteratorNext(iterator)) != 0 ) { nkeynes@964: io_string_t iopath = ""; nkeynes@964: IORegistryEntryGetPath( object, kIOServicePlane, iopath ); nkeynes@964: IOObjectRelease( object ); nkeynes@964: } nkeynes@964: } nkeynes@964: nkeynes@964: gboolean osx_register_iokit_notifications() nkeynes@964: { nkeynes@964: notify_port = IONotificationPortCreate( kIOMasterPortDefault ); nkeynes@964: CFRunLoopSourceRef runloop_source = IONotificationPortGetRunLoopSource( notify_port ); nkeynes@964: CFRunLoopAddSource( CFRunLoopGetCurrent(), runloop_source, kCFRunLoopCommonModes ); nkeynes@964: nkeynes@964: // Drive notifications nkeynes@964: if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification, nkeynes@964: IOServiceMatching("IOCompactDiscServies"), nkeynes@964: osx_drives_changed, NULL, &iokit_iterators[0] ) != kIOReturnSuccess ) { nkeynes@964: ERROR( "IOServiceAddMatchingNotification failed" ); nkeynes@964: } nkeynes@964: osx_drives_changed(NULL, iokit_iterators[0]); nkeynes@964: if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification, nkeynes@964: IOServiceMatching("IODVDServies"), nkeynes@964: osx_drives_changed, NULL, &iokit_iterators[1] ) != kIOReturnSuccess ) { nkeynes@964: ERROR( "IOServiceAddMatchingNotification failed" ); nkeynes@964: } nkeynes@964: osx_drives_changed(NULL, iokit_iterators[1]); nkeynes@964: nkeynes@964: if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification, nkeynes@964: IOServiceMatching(kIOHIDDeviceKey), nkeynes@964: osx_hid_inserted, NULL, &iokit_iterators[2] ) != kIOReturnSuccess ) { nkeynes@964: ERROR( "IOServiceAddMatchingNotification failed" ); nkeynes@964: } nkeynes@964: osx_hid_inserted(NULL, iokit_iterators[2]); nkeynes@964: return TRUE; nkeynes@964: } nkeynes@964: nkeynes@964: void osx_unregister_iokit_notifications() nkeynes@964: { nkeynes@964: CFRunLoopSourceRef runloop_source = IONotificationPortGetRunLoopSource( notify_port ); nkeynes@964: CFRunLoopRemoveSource( CFRunLoopGetCurrent(), runloop_source, kCFRunLoopCommonModes ); nkeynes@964: IONotificationPortDestroy( notify_port ); nkeynes@964: notify_port = 0; nkeynes@964: }