Programmatically “unplug and replug” a USB device to load new driver in OS X?
-
06-07-2021 - |
Question
I'm working on an installer in OS X that installs an IOKit driver for a USB device, and I'm trying to get it to not require a restart at the end. The installer installs the driver correctly and rebuilds the kext cache, and after it runs, if I unplug and replug the USB device, it correctly loads the new driver and everything works fine.
However, I don't want to require the user to physically unplug the device in order for the new driver to load. There's got to be a way to get OS X to load the new driver programmatically - in effect simulate the device being unplugged and plugged back in again, or something similar. How would I go about doing this? So far, hours of Googling has turned up nothing, so any help will be greatly appreciated!
Solution
IOUSBDeviceInterface187::USBDeviceReEnumerate() will do what you want. The only hitch is that to find all of the devices of interest and call this on them manually with IOServiceGetMatchingServices()
.
/*!
@function USBDeviceReEnumerate
@abstract Tells the IOUSBFamily to reenumerate the device.
@discussion This function will send a terminate message to all clients of the IOUSBDevice (such as
IOUSBInterfaces and their drivers, as well as the current User Client), emulating an unplug
of the device. The IOUSBFamily will then enumerate the device as if it had just
been plugged in. This call should be used by clients wishing to take advantage
of the Device Firmware Update Class specification. The device must be open to use this function.
@availability This function is only available with IOUSBDeviceInterface187 and above.
@param self Pointer to the IOUSBDeviceInterface.
@param options A UInt32 reserved for future use. Ignored in current implementation. Set to zero.
@result Returns kIOReturnSuccess if successful, kIOReturnNoDevice if there is no connection to an IOService,
or kIOReturnNotOpen if the device is not open for exclusive access.
*/
IOReturn (*USBDeviceReEnumerate)(void *self, UInt32 options);
Look in IOKit/usb/IOUSBLib.h
OTHER TIPS
Take a look at diskutil
, and especially the mount
and unmount
options. Those will softwarematically eject and mount devices. You can use diskutil list
to get a list of all the currently mounted devices. If you need more info on diskutil, just look at the man page.