module.h revision 50477
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 *
2650477Speter * $FreeBSD: head/sys/sys/module.h 50477 1999-08-28 01:08:13Z peter $
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 */
5025537Sdfr} moduledata_t;
5125537Sdfr
5242435Sdfr/*
5342435Sdfr * A module can use this to report module specific data to
5442435Sdfr * the user via kldstat(2).
5542435Sdfr */
5642435Sdfrtypedef union modspecific {
5742435Sdfr    int		intval;
5842435Sdfr    u_int	uintval;
5942435Sdfr    long	longval;
6042435Sdfr    u_long	ulongval;
6142435Sdfr} modspecific_t;
6242435Sdfr
6340137Speter#ifdef KERNEL
6440137Speter
6525537Sdfr#define DECLARE_MODULE(name, data, sub, order) \
6643381Sdillon    SYSINIT(name##module, sub, order, module_register_init, &data) \
6743381Sdillon    struct __hack
6825537Sdfr
6943387Sdillonvoid module_register_init(const void *data);
7046693Speterstruct linker_file;
7146693Speterint module_register(const struct moduledata *data, struct linker_file *lf);
7225537Sdfrmodule_t module_lookupbyname(const char *name);
7325537Sdfrmodule_t module_lookupbyid(int modid);
7425537Sdfrvoid module_reference(module_t mod);
7525537Sdfrvoid module_release(module_t mod);
7625537Sdfrint module_unload(module_t mod);
7725537Sdfrint module_getid(module_t mod);
7825537Sdfrmodule_t module_getfnext(module_t mod);
7942435Sdfrvoid module_setspecific(module_t mod, modspecific_t *datap);
8025537Sdfr
8125537Sdfr#ifdef MOD_DEBUG
8225537Sdfr
8325537Sdfrextern int mod_debug;
8425537Sdfr#define MOD_DEBUG_REFS	1
8525537Sdfr
8625537Sdfr#define MOD_DPF(cat, args)					\
8725537Sdfr	do {							\
8825537Sdfr		if (mod_debug & MOD_DEBUG_##cat) printf args;	\
8925537Sdfr	} while (0)
9025537Sdfr
9125537Sdfr#else
9225537Sdfr
9325537Sdfr#define MOD_DPF(cat, args)
9425537Sdfr
9525537Sdfr#endif
9625537Sdfr
9725537Sdfr#endif /* KERNEL */
9825537Sdfr
9925537Sdfr#define MAXMODNAME	32
10025537Sdfr
10125537Sdfrstruct module_stat {
10225537Sdfr    int		version;	/* set to sizeof(struct module_stat) */
10325537Sdfr    char	name[MAXMODNAME];
10425537Sdfr    int		refs;
10525537Sdfr    int		id;
10642435Sdfr    modspecific_t data;
10725537Sdfr};
10825537Sdfr
10925537Sdfr#ifndef KERNEL
11025537Sdfr
11125537Sdfr#include <sys/cdefs.h>
11225537Sdfr
11325537Sdfr__BEGIN_DECLS
11425537Sdfrint	modnext(int modid);
11525537Sdfrint	modfnext(int modid);
11625537Sdfrint	modstat(int modid, struct module_stat* stat);
11725537Sdfrint	modfind(char *name);
11825537Sdfr__END_DECLS
11925537Sdfr
12025537Sdfr#endif
12125537Sdfr
12225537Sdfr#endif	/* !_SYS_MODULE_H_ */
123