Search code examples
c++iosmacosdriverdriverkit

DriverKit USB driver (dext) process doesn't terminate after device is unplugged


I'm working on a USB driver using DriverKit for macOS (later iPadOS as well). The driver matches, loads, and is able to communicate with my device. However, I've noticed that when I unplug the device, the driver process is still running (as seen in Activity Monitor). If I plug it back in again, a second process starts up (and a third, and fourth, and so on). I've implemented the Stop() method like so:

kern_return_t
IMPL(MyDriver, Stop)
{
    kern_return_t ret = kIOReturnSuccess;

    os_log(OS_LOG_DEFAULT, "Stopping");
    ret = ivars->device->Close(this, 0);
    if (ret != kIOReturnSuccess) {
        os_log(OS_LOG_DEFAULT, "Error closing device: 0x%08x", ret);
    }
    
    ret = Stop(provider, SUPERDISPATCH);
    if (ret != kIOReturnSuccess) {
        os_log(OS_LOG_DEFAULT, "Error closing device: 0x%08x", ret);
    }
    
    os_log(OS_LOG_DEFAULT, "Stop finished.");

    return ret;
}

I've confirmed that the Stop method is indeed being called, and the return value is kIOReturnSuccess. My driver class's free() method is also being called. Might also be worth noting that no client app is communicating with the driver (haven't gotten that far yet).

Is there something else I need to do to tell the system it's OK to stop running the driver process? Is it normal for it to not terminate?


Solution

  • Turns out the problem was that I had forgotten to call super::free() in my driver object's free method. After fixing that the driver process terminates when the device is unplugged.