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