module.h revision 43381
125537Sdfr/*-
225537Sdfr * Copyright (c) 1997 Doug Rabson
325537Sdfr * All rights reserved.
425537Sdfr *
525537Sdfr * Redistribution and use in source and binary forms, with or without
625537Sdfr * modification, are permitted provided that the following conditions
725537Sdfr * are met:
825537Sdfr * 1. Redistributions of source code must retain the above copyright
925537Sdfr *    notice, this list of conditions and the following disclaimer.
1025537Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1125537Sdfr *    notice, this list of conditions and the following disclaimer in the
1225537Sdfr *    documentation and/or other materials provided with the distribution.
1325537Sdfr *
1425537Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1525537Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1625537Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1725537Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1825537Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1925537Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2025537Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2125537Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2225537Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2325537Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2425537Sdfr * SUCH DAMAGE.
2525537Sdfr *
2643381Sdillon *	$Id: module.h,v 1.8 1999/01/27 21:50:00 dillon Exp $
2725537Sdfr */
2825537Sdfr
2925537Sdfr#ifndef _SYS_MODULE_H_
3025537Sdfr#define _SYS_MODULE_H_
3125537Sdfr
3241153Swollmantypedef enum modeventtype {
3325537Sdfr    MOD_LOAD,
3425537Sdfr    MOD_UNLOAD,
3525537Sdfr    MOD_SHUTDOWN
3625537Sdfr} modeventtype_t;
3725537Sdfr
3841153Swollmantypedef	struct module *module_t;
3925537Sdfr
4043301Sdillontypedef	int (*modeventhand_t)(module_t mod, int /*modeventtype_t*/ what,
4141153Swollman			      void *arg);
4225537Sdfr
4325537Sdfr/*
4425537Sdfr * Struct for registering modules statically via SYSINIT.
4525537Sdfr */
4625537Sdfrtypedef struct moduledata {
4741153Swollman	char		*name;	/* module name */
4841153Swollman	modeventhand_t	evhand;	/* event handler */
4941153Swollman	void		*priv;	/* extra data */
5041153Swollman	void		*_file;	/* private; used by linker */
5125537Sdfr} moduledata_t;
5225537Sdfr
5342435Sdfr/*
5442435Sdfr * A module can use this to report module specific data to
5542435Sdfr * the user via kldstat(2).
5642435Sdfr */
5742435Sdfrtypedef union modspecific {
5842435Sdfr    int		intval;
5942435Sdfr    u_int	uintval;
6042435Sdfr    long	longval;
6142435Sdfr    u_long	ulongval;
6242435Sdfr} modspecific_t;
6342435Sdfr
6440137Speter#ifdef KERNEL
6540137Speter
6625537Sdfr#define DECLARE_MODULE(name, data, sub, order) \
6743381Sdillon    SYSINIT(name##module, sub, order, module_register_init, &data) \
6843381Sdillon    struct __hack
6925537Sdfr
7043381Sdillon#define C_DECLARE_MODULE(name, data, sub, order) \
7143381Sdillon    C_SYSINIT(name##module, sub, order, module_register_init, &data) \
7243381Sdillon    struct __hack
7343381Sdillon
7430683Sjmgvoid module_register_init(void *data);
7540137Speterint module_register(const char *name, modeventhand_t callback, void *arg,
7640137Speter		    void *file);
7725537Sdfrmodule_t module_lookupbyname(const char *name);
7825537Sdfrmodule_t module_lookupbyid(int modid);
7925537Sdfrvoid module_reference(module_t mod);
8025537Sdfrvoid module_release(module_t mod);
8125537Sdfrint module_unload(module_t mod);
8225537Sdfrint module_getid(module_t mod);
8325537Sdfrmodule_t module_getfnext(module_t mod);
8442435Sdfrvoid module_setspecific(module_t mod, modspecific_t *datap);
8525537Sdfr
8625537Sdfr#ifdef MOD_DEBUG
8725537Sdfr
8825537Sdfrextern int mod_debug;
8925537Sdfr#define MOD_DEBUG_REFS	1
9025537Sdfr
9125537Sdfr#define MOD_DPF(cat, args)					\
9225537Sdfr	do {							\
9325537Sdfr		if (mod_debug & MOD_DEBUG_##cat) printf args;	\
9425537Sdfr	} while (0)
9525537Sdfr
9625537Sdfr#else
9725537Sdfr
9825537Sdfr#define MOD_DPF(cat, args)
9925537Sdfr
10025537Sdfr#endif
10125537Sdfr
10225537Sdfr#endif /* KERNEL */
10325537Sdfr
10425537Sdfr#define MAXMODNAME	32
10525537Sdfr
10625537Sdfrstruct module_stat {
10725537Sdfr    int		version;	/* set to sizeof(struct module_stat) */
10825537Sdfr    char	name[MAXMODNAME];
10925537Sdfr    int		refs;
11025537Sdfr    int		id;
11142435Sdfr    modspecific_t data;
11225537Sdfr};
11325537Sdfr
11425537Sdfr#ifndef KERNEL
11525537Sdfr
11625537Sdfr#include <sys/cdefs.h>
11725537Sdfr
11825537Sdfr__BEGIN_DECLS
11925537Sdfrint	modnext(int modid);
12025537Sdfrint	modfnext(int modid);
12125537Sdfrint	modstat(int modid, struct module_stat* stat);
12225537Sdfrint	modfind(char *name);
12325537Sdfr__END_DECLS
12425537Sdfr
12525537Sdfr#endif
12625537Sdfr
12725537Sdfr#endif	/* !_SYS_MODULE_H_ */
128