2

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?

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
  • Have you got IORegistryExplorer running? That can sometimes retain references on service objects. (Or any other apps, possibly your own, that isn’t actively communicating but might have retained the service?) – pmdj Feb 11 '23 at 07:37
  • Thanks for the suggestion @pmdj. I don't have anything else running that I think would retain the service objects. I'm not running IORegistryExplorer. To be more sure, I just rebooted, and only started Activity Monitor. Plugging the device in shows the driver process start up, but again not terminate when I unplug. (This is a week-old Mac with very little installed on it.) – Andrew Madsen Feb 11 '23 at 18:41

1 Answers1

2

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.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97