Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
#include <sys/modctl.h> int _fini(void)
int _info(struct modinfo *modinfop);
int _init(void)
A pointer to an opaque modinfo structure.
_info() returns information about a loadable module. _info() returns the value returned by mod_info(9F).
_fini() prepares a loadable module for unloading. It is called when the system wants to unload a module. If the module determines that it can be unloaded, then _fini() returns the value returned by mod_remove(9F). Upon successful return from _fini() no other routine in the module will be called before _init() is called. If _init() did not successfully complete, _fini() will not be called.
_info() should return the return value from mod_info(9F)
_fini() should return the return value from mod_remove(9F). _fini() is permitted to return EBUSY prior to calling mod_remove(9F) if the driver should not be unloaded. Driver global resources, such as mutexes and calls to ddi_soft_state_fini(9F), should only be destroyed in _fini() after mod_remove() returns successfully.
The following example demonstrates how to initialize and free a mutex(9F).
#include <sys/modctl.h> #include <sys/ddi.h> #include <sys/sunddi.h> static struct dev_ops drv_ops; /* * Module linkage information for the kernel. */ static struct modldrv modldrv = { &mod_driverops, /* Type of module. This one is a driver */ "Sample Driver", &drv_ops /* driver ops */ }; static struct modlinkage modlinkage = { MODREV_1, &modldrv, NULL }; /* * Global driver mutex */ static kmutex_t xx_global_mutex; int _init(void) { int i; /* * Initialize global mutex before mod_install'ing driver. * If mod_install() fails, must clean up mutex initialization */ mutex_init(&xx_global_mutex, NULL, MUTEX_DRIVER, (void *)NULL); if ((i = mod_install(&modlinkage)) != 0) { mutex_destroy(&xx_global_mutex); } return (i); } int _info(struct modinfo *modinfop) { return (mod_info(&modlinkage, modinfop)); } int _fini(void) { int i; /* * If mod_remove() is successful, we destroy our global mutex */ if ((i = mod_remove(&modlinkage)) == 0) { mutex_destroy(&xx_global_mutex); } return (i); }
Writing Device Drivers