I'm developing an iPadOS USB audio-MIDI driver for a hardware device. The audio side uses AudioDriverKit and USBDriverKit (DriverKit SDK 27.0, AudioDriverKit deployment target 21.0, iPadOS 18.0 minimum).
I need clarification about the ordering between IOUserAudioDriver::Stop, StopDevice/StopIO, and in-flight IOOperationHandler callbacks.
According to Apple's DriverKit documentation, IOService::Stop must quiesce outstanding operations before invoking its superclass implementation. AudioDriverKit documents host-controlled audio I/O and the StopDevice → device StopIO sequence.
However, I cannot find an explicit guarantee that the framework has stopped and drained all BeginRead/WriteEnd callbacks before invoking the driver's service Stop.
Relevant implementation (simplified):
// Audio driver service termination
kern_return_t result = StopUSBTransport();
if (result != kIOReturnSuccess) {
return result;
}
// Release resources and close provider interfaces.
return Stop(in_provider, SUPERDISPATCH);
The transport retirement path currently refuses to release resources while framework I/O is marked active:
if (__atomic_load_n(&ivars->m_adk_running,
__ATOMIC_ACQUIRE)) {
// Retain ownership; resources cannot be released safely.
return kIOReturnBusy;
}
The local running state is cleared following successful device superclass StopIO, but this does not independently establish that already-entered framework audio callbacks have returned.
The USB teardown separately prevents new submissions, aborts outstanding transfers, waits for cancellation acknowledgements and native callback completion, and retains resources if ownership cannot be proven safe.
An offline regression exercising service termination while framework I/O is marked active reproduces the Busy result. We have not established that AudioDriverKit delivers this ordering on physical hardware.
Questions:
-
Before invoking
IOUserAudioDriver::Stop, does AudioDriverKit guarantee thatStopDevice/StopIOhas completed and that all outstandingIOOperationHandlerinvocations have returned? -
If not, what supported mechanism can a driver invoke during service
Stopto prevent newBeginRead/WriteEndcallbacks and wait for already-entered callbacks to finish? -
Does calling superclass
IOUserAudioDriver::Stopestablish this callback-drain boundary? If so, how should hardware and USB resources be shut down when provider access is no longer permitted afterward? -
If termination cannot safely complete, is returning
kIOReturnBusya supported way to defer service termination? Is a subsequent retry guaranteed?
I'm looking for the documented lifecycle and synchronization contract, rather than a workaround based on assumed callback ordering.
Any clarification from Apple's DriverKit or Core Audio engineering team would be appreciated.
Handler lifetime: Does AudioDriverKit guarantee that an invoked handler block and its captured context remain valid from before callback entry through return, even if the IOUserAudioDevice is concurrently released or reset?
Basically, yes. IOUserAudioDevice owns the block you pass into SetIOOperationHandler and frees it in free, so it can't/won't be freed until after I/O has definitively stopped.
Having said that...
Retirement and reclamation: Is there a public completion point establishing that no further handler invocations can begin and all existing invocations have returned?
What's your larger concern here? The basic API contract here is that your "Stop" won't be called until your children have stopped interacting with you and that you won't call "super::Stop" until after you've stopped your own activity and disconnected from your provider.
However, issues like memory management are less of an issue than they would be in the kernel, since any memory you've allocated will be reclaimed when your process is destroyed, regardless of whether or not you manually returned that memory to the system.
Can AudioDriverKit release its last reference to the handler block on a real-time audio thread, potentially destroying captured objects there? If no public retirement mechanism exists, what ownership pattern does Apple recommend for safe, non-real-time reclamation, including incomplete teardown or cancellation?
No. As noted above, this is tied to IOUserAudioDevice's lifetime, so it won't be destroyed until everything else is "done".
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware