Loadable Driver Interfaces
Device drivers must be dynamically loadable and should be unloadable to help conserve memory resources. Drivers that can be unloaded are also easier to test, debug, and patch.
Each device driver is required to implement _init(9E), _fini(9E), and_info(9E) entry points to support driver loading and unloading. Example 5-1 shows a typical implementation of loadable driver interfaces.
Example 5-1 Loadable Interface Section
_init() Example
Example 5-2 shows a typical _init(9E) interface.
Example 5-2 _init()
The driver should perform any one-time resource allocation or data initialization during driver loading in _init(). For example, it should initialize any mutexes global to the driver in this routine. The driver should not, however, use _init(9E) to allocate or initialize anything that has to do with a particular instance of the device. Per-instance initialization must be done in attach(9E). For example, if a driver for a printer can handle more than one printer at the same time, it should allocate resources specific to each printer instance in attach(9E).
Note - Once _init(9E) has called mod_install(9F), the driver should not change any of the data structures attached to the modlinkage(9S) structure, as the system may make copies of them or change them.
_fini() Example
The following example demonstrates the _fini(9E) routine.
int _fini(void) { int error; error = mod_remove(&modlinkage); if (error != 0) { return (error); } /* * Cleanup resources allocated in _init() */ ddi_soft_state_fini(&xxstatep); return (0); } |
Similarly, in _fini(), the driver should release any resources that were allocated in _init() and must remove itself from the system module list.
Note - _fini() may be called when the driver is attached to hardware instances. In this case, mod_remove(9F) returns failure. Therefore, driver resources should not be released until mod_remove(9F) returns success.