Search
lxdream.org :: lxdream/src/drivers/osx_iokit.m
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/osx_iokit.m
changeset 964:f2f3c7612d06
next1023:264e2fd90be8
author nkeynes
date Sat Feb 28 06:26:48 2009 +0000 (15 years ago)
permissions -rw-r--r--
last change Add SDL audio driver, thanks to Wahrhaft!
file annotate diff log raw
nkeynes@964
     1
/**
nkeynes@964
     2
 * $Id$
nkeynes@964
     3
 *
nkeynes@964
     4
 * OSX support functions for handling the IOKit registry. 
nkeynes@964
     5
 * Currently this manages access to CD/DVD drives + media, plus HID devices.
nkeynes@964
     6
 * 
nkeynes@964
     7
 * The HID part is much simpler...
nkeynes@964
     8
 *
nkeynes@964
     9
 * Copyright (c) 2008 Nathan Keynes.
nkeynes@964
    10
 *
nkeynes@964
    11
 * This program is free software; you can redistribute it and/or modify
nkeynes@964
    12
 * it under the terms of the GNU General Public License as published by
nkeynes@964
    13
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@964
    14
 * (at your option) any later version.
nkeynes@964
    15
 *
nkeynes@964
    16
 * This program is distributed in the hope that it will be useful,
nkeynes@964
    17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@964
    18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@964
    19
 * GNU General Public License for more details.
nkeynes@964
    20
 */
nkeynes@964
    21
nkeynes@964
    22
#include <glib/gmem.h>
nkeynes@964
    23
#include <glib/gstrfuncs.h>
nkeynes@964
    24
#include <sys/param.h>
nkeynes@964
    25
#include <paths.h>
nkeynes@964
    26
#include <string.h>
nkeynes@964
    27
#include <unistd.h>
nkeynes@964
    28
#include <stdio.h>
nkeynes@964
    29
#include <fcntl.h>
nkeynes@964
    30
#include <CoreFoundation/CFRunLoop.h>
nkeynes@964
    31
#include <IOKit/IOMessage.h>
nkeynes@964
    32
#include <IOKit/hid/IOHIDLib.h>
nkeynes@964
    33
#include "osx_iokit.h"
nkeynes@964
    34
nkeynes@964
    35
nkeynes@964
    36
nkeynes@964
    37
static IONotificationPortRef notify_port = 0;
nkeynes@964
    38
static io_iterator_t iokit_iterators[4] = {0,0,0,0};
nkeynes@964
    39
nkeynes@964
    40
struct osx_cdrom_drive {
nkeynes@964
    41
    io_string_t ioservice_path;
nkeynes@964
    42
    io_string_t vendor_name;
nkeynes@964
    43
    io_string_t product_name;
nkeynes@964
    44
    char media_path[MAXPATHLEN]; // BSD device path if media present, otherwise the empty string.
nkeynes@964
    45
    io_iterator_t media_load_iterator;
nkeynes@964
    46
    io_iterator_t media_unload_iterator;
nkeynes@964
    47
    int media_fh; // BSD device handle if open, otherwise -1
nkeynes@964
    48
    media_changed_callback_t media_changed;
nkeynes@964
    49
    void *media_changed_user_data;
nkeynes@964
    50
};
nkeynes@964
    51
nkeynes@964
    52
static gboolean get_bsdname_for_iomedia( io_object_t iomedia, char *buf, int buflen );
nkeynes@964
    53
nkeynes@964
    54
/***************** IOKit Callbacks ******************/
nkeynes@964
    55
nkeynes@964
    56
/**
nkeynes@964
    57
 * Called from IOKit for any IOMessages on an IOMedia. Currently the only message
nkeynes@964
    58
 * we're interested in is service termination.
nkeynes@964
    59
 */
nkeynes@964
    60
static void osx_cdrom_media_notify( void *ref, io_service_t service, uint32_t msgType,
nkeynes@964
    61
                                    void *msgArgument )
nkeynes@964
    62
{
nkeynes@964
    63
    if( msgType == kIOMessageServiceIsTerminated ) {
nkeynes@964
    64
        osx_cdrom_drive_t drive = (osx_cdrom_drive_t)ref;
nkeynes@964
    65
        if( drive->media_changed != NULL ) {
nkeynes@964
    66
            drive->media_changed( drive, FALSE, drive->media_changed_user_data );
nkeynes@964
    67
        }
nkeynes@964
    68
        if( drive->media_fh != -1 ) {
nkeynes@964
    69
            close(drive->media_fh);
nkeynes@964
    70
            drive->media_fh = -1;
nkeynes@964
    71
        }
nkeynes@964
    72
        drive->media_path[0] = '\0';
nkeynes@964
    73
        IOObjectRelease( drive->media_unload_iterator );
nkeynes@964
    74
    }
nkeynes@964
    75
}
nkeynes@964
    76
nkeynes@964
    77
/**
nkeynes@964
    78
 * Called from IOKit when an IOMedia is inserted that we have be interested in.
nkeynes@964
    79
 * FIXME: Can the matcher be restricted to descendents of the drive node? currently
nkeynes@964
    80
 * we watch for all IOMedia events and compare the device path to see if it's one we
nkeynes@964
    81
 * care about.
nkeynes@964
    82
 * FIXME: We assume for now that a drive has at most one piece of media at a time. 
nkeynes@964
    83
 * If this isn't the case, the system may get a little confused.
nkeynes@964
    84
 */
nkeynes@964
    85
static void osx_cdrom_media_inserted( void *ref, io_iterator_t iterator )
nkeynes@964
    86
{
nkeynes@964
    87
    osx_cdrom_drive_t drive = (osx_cdrom_drive_t)ref;
nkeynes@964
    88
nkeynes@964
    89
    io_object_t object;
nkeynes@964
    90
    while( (object = IOIteratorNext(iterator)) != 0 ) {
nkeynes@964
    91
        io_string_t iopath = "";
nkeynes@964
    92
        IORegistryEntryGetPath( object, kIOServicePlane, iopath );
nkeynes@964
    93
        if( drive != NULL && g_str_has_prefix(iopath, drive->ioservice_path ) &&
nkeynes@964
    94
                get_bsdname_for_iomedia(object, drive->media_path, sizeof(drive->media_path)) ) {
nkeynes@964
    95
            // A disc was inserted within the drive of interest
nkeynes@964
    96
            if( drive->media_fh != -1 ) {
nkeynes@964
    97
                close(drive->media_fh);
nkeynes@964
    98
                drive->media_fh = -1;
nkeynes@964
    99
            }
nkeynes@964
   100
nkeynes@964
   101
            if( drive->media_changed != NULL ) {
nkeynes@964
   102
                drive->media_changed(drive, TRUE, drive->media_changed_user_data);
nkeynes@964
   103
            }
nkeynes@964
   104
            // Add a notification listener to get removal events.
nkeynes@964
   105
            IOServiceAddInterestNotification( notify_port, object, kIOGeneralInterest, 
nkeynes@964
   106
                                              osx_cdrom_media_notify, drive, &drive->media_unload_iterator ); 
nkeynes@964
   107
nkeynes@964
   108
        }
nkeynes@964
   109
        IOObjectRelease( object );
nkeynes@964
   110
    }
nkeynes@964
   111
}
nkeynes@964
   112
nkeynes@964
   113
static void osx_drives_changed( void *ref, io_iterator_t iterator )
nkeynes@964
   114
{
nkeynes@964
   115
    io_object_t object;
nkeynes@964
   116
    while( (object = IOIteratorNext(iterator)) != 0 ) {
nkeynes@964
   117
        IOObjectRelease(object);
nkeynes@964
   118
    }
nkeynes@964
   119
nkeynes@964
   120
}
nkeynes@964
   121
nkeynes@964
   122
/******************** Support functions *********************/
nkeynes@964
   123
nkeynes@964
   124
/**
nkeynes@964
   125
 * Determine the BSD device name (ie "/dev/rdisk1") for a given IO object.
nkeynes@964
   126
 * @return TRUE if the device name was retrieved, FALSE if the request failed.
nkeynes@964
   127
 */
nkeynes@964
   128
static gboolean get_bsdname_for_iomedia( io_object_t iomedia, char *buf, int buflen )
nkeynes@964
   129
{
nkeynes@964
   130
    gboolean result = FALSE;
nkeynes@964
   131
    CFTypeRef pathRef = IORegistryEntryCreateCFProperty(iomedia, CFSTR(kIOBSDNameKey),
nkeynes@964
   132
            kCFAllocatorDefault, 0 );
nkeynes@964
   133
    if( pathRef ) {
nkeynes@964
   134
        char pathlen;
nkeynes@964
   135
        strcpy( buf, _PATH_DEV "r" );
nkeynes@964
   136
        pathlen = strlen(buf);
nkeynes@964
   137
        if( CFStringGetCString( pathRef, buf + pathlen, buflen-pathlen,
nkeynes@964
   138
                kCFStringEncodingASCII ) != noErr ) {
nkeynes@964
   139
            result = TRUE;
nkeynes@964
   140
        }
nkeynes@964
   141
        CFRelease(pathRef);
nkeynes@964
   142
    }
nkeynes@964
   143
    return result;
nkeynes@964
   144
}
nkeynes@964
   145
nkeynes@964
   146
static gboolean osx_cdrom_drive_get_name( io_object_t object, char *vendor, int vendor_len,
nkeynes@964
   147
                                          char *product, int product_len )
nkeynes@964
   148
{
nkeynes@964
   149
    gboolean result = FALSE;
nkeynes@964
   150
    CFMutableDictionaryRef props = 0;
nkeynes@964
   151
    if( IORegistryEntryCreateCFProperties(object, &props, kCFAllocatorDefault, kNilOptions) == KERN_SUCCESS ) {
nkeynes@964
   152
        CFDictionaryRef dict = 
nkeynes@964
   153
            (CFDictionaryRef)CFDictionaryGetValue(props, CFSTR(kIOPropertyDeviceCharacteristicsKey));
nkeynes@964
   154
        if( dict != NULL ) {
nkeynes@964
   155
            CFTypeRef value = CFDictionaryGetValue(dict, CFSTR(kIOPropertyVendorNameKey));
nkeynes@964
   156
            if( value && CFGetTypeID(value) == CFStringGetTypeID() ) {
nkeynes@964
   157
                CFStringGetCString( (CFStringRef)value, vendor, vendor_len, kCFStringEncodingUTF8 );
nkeynes@964
   158
            } else {
nkeynes@964
   159
                vendor[0] = 0;
nkeynes@964
   160
            }
nkeynes@964
   161
nkeynes@964
   162
            value = CFDictionaryGetValue(dict, CFSTR(kIOPropertyProductNameKey));
nkeynes@964
   163
            if ( value && CFGetTypeID(value) == CFStringGetTypeID() ) {
nkeynes@964
   164
                CFStringGetCString( (CFStringRef)value, product, product_len, kCFStringEncodingUTF8 );
nkeynes@964
   165
            } else {
nkeynes@964
   166
                product[0] = 0;
nkeynes@964
   167
            }
nkeynes@964
   168
            result = TRUE;
nkeynes@964
   169
        }
nkeynes@964
   170
nkeynes@964
   171
        CFRelease(props);
nkeynes@964
   172
    }
nkeynes@964
   173
    return result;
nkeynes@964
   174
}
nkeynes@964
   175
nkeynes@964
   176
/**
nkeynes@964
   177
 * Construct and initialize a new osx_cdrom_drive object, including registering
nkeynes@964
   178
 * it's media inserted notification.
nkeynes@964
   179
 */
nkeynes@964
   180
static osx_cdrom_drive_t osx_cdrom_drive_new( io_object_t device )  
nkeynes@964
   181
{
nkeynes@964
   182
    osx_cdrom_drive_t drive = g_malloc0(sizeof(struct osx_cdrom_drive));
nkeynes@964
   183
nkeynes@964
   184
    IORegistryEntryGetPath( device, kIOServicePlane, drive->ioservice_path );
nkeynes@964
   185
    osx_cdrom_drive_get_name( device, drive->vendor_name, sizeof(drive->vendor_name),
nkeynes@964
   186
                              drive->product_name, sizeof(drive->product_name) );
nkeynes@964
   187
    drive->media_path[0] = '\0';
nkeynes@964
   188
    drive->media_changed = NULL;
nkeynes@964
   189
    drive->media_changed_user_data = NULL;
nkeynes@964
   190
    drive->media_fh = -1;
nkeynes@964
   191
nkeynes@964
   192
    IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification, 
nkeynes@964
   193
                                      IOServiceMatching("IOMedia"),
nkeynes@964
   194
                                      osx_cdrom_media_inserted, drive, 
nkeynes@964
   195
                                      &drive->media_load_iterator );
nkeynes@964
   196
    osx_cdrom_media_inserted( drive, drive->media_load_iterator );
nkeynes@964
   197
    return drive;
nkeynes@964
   198
}
nkeynes@964
   199
nkeynes@964
   200
/************************ Exported functions *************************/ 
nkeynes@964
   201
nkeynes@964
   202
osx_cdrom_drive_t osx_cdrom_open_drive( const char *devname )
nkeynes@964
   203
{
nkeynes@964
   204
    io_object_t object = IORegistryEntryFromPath( kIOMasterPortDefault, devname );
nkeynes@964
   205
    if( object == MACH_PORT_NULL ) {
nkeynes@964
   206
        return NULL;
nkeynes@964
   207
    }
nkeynes@964
   208
nkeynes@964
   209
    osx_cdrom_drive_t drive = osx_cdrom_drive_new( object );
nkeynes@964
   210
    IOObjectRelease( object );
nkeynes@964
   211
    return drive;
nkeynes@964
   212
}
nkeynes@964
   213
nkeynes@964
   214
void osx_cdrom_set_media_changed_callback( osx_cdrom_drive_t drive, 
nkeynes@964
   215
                                           media_changed_callback_t callback,
nkeynes@964
   216
                                           void *user_data )
nkeynes@964
   217
{
nkeynes@964
   218
    drive->media_changed = callback;
nkeynes@964
   219
    drive->media_changed_user_data = user_data;
nkeynes@964
   220
}
nkeynes@964
   221
nkeynes@964
   222
void osx_cdrom_close_drive( osx_cdrom_drive_t drive )
nkeynes@964
   223
{
nkeynes@964
   224
    IOObjectRelease( drive->media_load_iterator );
nkeynes@964
   225
    IOObjectRelease( drive->media_unload_iterator );
nkeynes@964
   226
    if( drive->media_fh != -1 ) {
nkeynes@964
   227
        close(drive->media_fh);
nkeynes@964
   228
        drive->media_fh = -1;
nkeynes@964
   229
    }
nkeynes@964
   230
    g_free( drive );
nkeynes@964
   231
}
nkeynes@964
   232
nkeynes@964
   233
int osx_cdrom_get_media_handle( osx_cdrom_drive_t drive )
nkeynes@964
   234
{
nkeynes@964
   235
    if( drive->media_fh == -1 ) {
nkeynes@964
   236
        if( drive->media_path[0] != '\0' ) {
nkeynes@964
   237
            drive->media_fh = open( drive->media_path, O_RDONLY|O_NONBLOCK );
nkeynes@964
   238
        }
nkeynes@964
   239
    }
nkeynes@964
   240
    return drive->media_fh;
nkeynes@964
   241
}
nkeynes@964
   242
nkeynes@964
   243
void osx_cdrom_release_media_handle( osx_cdrom_drive_t drive )
nkeynes@964
   244
{
nkeynes@964
   245
    if( drive->media_fh != -1 ) {
nkeynes@964
   246
        close( drive->media_fh );
nkeynes@964
   247
        drive->media_fh = -1; 
nkeynes@964
   248
    }
nkeynes@964
   249
}
nkeynes@964
   250
nkeynes@964
   251
static io_object_t iterator_find_cdrom( io_object_t iterator, find_drive_callback_t callback, void *user_data )
nkeynes@964
   252
{
nkeynes@964
   253
    io_object_t object;
nkeynes@964
   254
    while( (object = IOIteratorNext(iterator)) != 0 ) {
nkeynes@964
   255
        io_string_t iopath = "";
nkeynes@964
   256
        char product[256], vendor[256];
nkeynes@964
   257
        IORegistryEntryGetPath( object, kIOServicePlane, iopath );
nkeynes@964
   258
        osx_cdrom_drive_get_name( object, vendor, sizeof(vendor), product, sizeof(product) );
nkeynes@964
   259
        if( callback( object, vendor, product, iopath, user_data ) ) {
nkeynes@964
   260
            IOObjectRelease(iterator);
nkeynes@964
   261
            return object;
nkeynes@964
   262
        }
nkeynes@964
   263
        IOObjectRelease(object);
nkeynes@964
   264
    }
nkeynes@964
   265
    IOObjectRelease(iterator);
nkeynes@964
   266
    return 0;
nkeynes@964
   267
}
nkeynes@964
   268
nkeynes@964
   269
nkeynes@964
   270
/**
nkeynes@964
   271
 * Search for a CD or DVD drive (instance of IODVDServices or IOCompactDiscServices).
nkeynes@964
   272
 * The callback will be called repeatedly until either it returns TRUE, or all drives
nkeynes@964
   273
 * have been iterated over.
nkeynes@964
   274
 * 
nkeynes@964
   275
 * @return an IO registry entry for the matched drive, or 0 if no drives matched.
nkeynes@964
   276
 * 
nkeynes@964
   277
 * Note: Use of IOCompactDiscServices is somewhat tentative since I don't have a Mac
nkeynes@964
   278
 * with a CD-Rom drive.
nkeynes@964
   279
 */ 
nkeynes@964
   280
io_object_t find_cdrom_drive( find_drive_callback_t callback, void *user_data )
nkeynes@964
   281
{
nkeynes@964
   282
    mach_port_t master_port;
nkeynes@964
   283
    CFMutableDictionaryRef match;
nkeynes@964
   284
    io_iterator_t services;
nkeynes@964
   285
    io_object_t result;
nkeynes@964
   286
nkeynes@964
   287
    if( IOMasterPort( MACH_PORT_NULL, &master_port ) != KERN_SUCCESS ) {
nkeynes@964
   288
        return 0; // Failed to get the master port?
nkeynes@964
   289
    }
nkeynes@964
   290
nkeynes@964
   291
    match = IOServiceMatching("IODVDServices");
nkeynes@964
   292
    if( IOServiceGetMatchingServices(master_port, match, &services) != kIOReturnSuccess ) {
nkeynes@964
   293
        return 0;
nkeynes@964
   294
    }
nkeynes@964
   295
nkeynes@964
   296
    result = iterator_find_cdrom( services, callback, user_data );
nkeynes@964
   297
    if( result != 0 ) {
nkeynes@964
   298
        return result;
nkeynes@964
   299
    }
nkeynes@964
   300
nkeynes@964
   301
    match = IOServiceMatching("IOCompactDiscServices");
nkeynes@964
   302
    if( IOServiceGetMatchingServices(master_port, match, &services) != kIOReturnSuccess ) {
nkeynes@964
   303
        return 0;
nkeynes@964
   304
    }
nkeynes@964
   305
    return iterator_find_cdrom( services, callback, user_data );
nkeynes@964
   306
}
nkeynes@964
   307
nkeynes@964
   308
nkeynes@964
   309
// *********************** Notification management ************************/
nkeynes@964
   310
nkeynes@964
   311
static void osx_hid_inserted( void *ref, io_iterator_t iterator )
nkeynes@964
   312
{
nkeynes@964
   313
    io_object_t object;
nkeynes@964
   314
    while( (object = IOIteratorNext(iterator)) != 0 ) {
nkeynes@964
   315
        io_string_t iopath = "";
nkeynes@964
   316
        IORegistryEntryGetPath( object, kIOServicePlane, iopath );
nkeynes@964
   317
        IOObjectRelease( object );
nkeynes@964
   318
    }
nkeynes@964
   319
}
nkeynes@964
   320
nkeynes@964
   321
gboolean osx_register_iokit_notifications()
nkeynes@964
   322
{
nkeynes@964
   323
    notify_port = IONotificationPortCreate( kIOMasterPortDefault );
nkeynes@964
   324
    CFRunLoopSourceRef runloop_source = IONotificationPortGetRunLoopSource( notify_port );
nkeynes@964
   325
    CFRunLoopAddSource( CFRunLoopGetCurrent(), runloop_source, kCFRunLoopCommonModes );
nkeynes@964
   326
nkeynes@964
   327
    // Drive notifications
nkeynes@964
   328
    if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification,
nkeynes@964
   329
            IOServiceMatching("IOCompactDiscServies"),
nkeynes@964
   330
            osx_drives_changed, NULL, &iokit_iterators[0] ) != kIOReturnSuccess ) {
nkeynes@964
   331
        ERROR( "IOServiceAddMatchingNotification failed" );
nkeynes@964
   332
    }
nkeynes@964
   333
    osx_drives_changed(NULL, iokit_iterators[0]);
nkeynes@964
   334
    if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification,
nkeynes@964
   335
            IOServiceMatching("IODVDServies"),
nkeynes@964
   336
            osx_drives_changed, NULL, &iokit_iterators[1] ) != kIOReturnSuccess ) {
nkeynes@964
   337
        ERROR( "IOServiceAddMatchingNotification failed" );
nkeynes@964
   338
    }
nkeynes@964
   339
    osx_drives_changed(NULL, iokit_iterators[1]);
nkeynes@964
   340
nkeynes@964
   341
    if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification, 
nkeynes@964
   342
            IOServiceMatching(kIOHIDDeviceKey),
nkeynes@964
   343
            osx_hid_inserted, NULL, &iokit_iterators[2] ) != kIOReturnSuccess ) {
nkeynes@964
   344
        ERROR( "IOServiceAddMatchingNotification failed" );
nkeynes@964
   345
    }
nkeynes@964
   346
    osx_hid_inserted(NULL, iokit_iterators[2]);
nkeynes@964
   347
    return TRUE;
nkeynes@964
   348
}
nkeynes@964
   349
nkeynes@964
   350
void osx_unregister_iokit_notifications()
nkeynes@964
   351
{
nkeynes@964
   352
    CFRunLoopSourceRef runloop_source = IONotificationPortGetRunLoopSource( notify_port );
nkeynes@964
   353
    CFRunLoopRemoveSource( CFRunLoopGetCurrent(), runloop_source, kCFRunLoopCommonModes );
nkeynes@964
   354
    IONotificationPortDestroy( notify_port );
nkeynes@964
   355
    notify_port = 0;
nkeynes@964
   356
}
.