module.h revision 59751
1/*-
2 * Copyright (c) 1997 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/sys/module.h 59751 2000-04-29 13:19:31Z peter $
27 */
28
29#ifndef _SYS_MODULE_H_
30#define _SYS_MODULE_H_
31
32/*
33 * Module metadata types
34 */
35#define	MDT_DEPEND	1		/* argument is a module name */
36#define	MDT_MODULE	2		/* module declaration */
37#define	MDT_VERSION	3		/* module version(s) */
38
39#define	MDT_STRUCT_VERSION	1	/* version of metadata structure */
40
41#define	MDT_SETNAME	"modmetadata_set"
42
43typedef enum modeventtype {
44    MOD_LOAD,
45    MOD_UNLOAD,
46    MOD_SHUTDOWN
47} modeventtype_t;
48
49typedef	struct module *module_t;
50
51typedef	int (*modeventhand_t)(module_t mod, int /*modeventtype_t*/ what,
52			      void *arg);
53
54/*
55 * Struct for registering modules statically via SYSINIT.
56 */
57typedef struct moduledata {
58	char		*name;	/* module name */
59	modeventhand_t	evhand;	/* event handler */
60	void		*priv;	/* extra data */
61} moduledata_t;
62
63/*
64 * A module can use this to report module specific data to
65 * the user via kldstat(2).
66 */
67typedef union modspecific {
68    int		intval;
69    u_int	uintval;
70    long	longval;
71    u_long	ulongval;
72} modspecific_t;
73
74/*
75 * Module dependency declarartion
76 */
77struct mod_depend {
78    int		md_ver_minimum;
79    int		md_ver_preferred;
80    int		md_ver_maximum;
81};
82
83/*
84 * Module version declaration
85 */
86struct mod_version {
87    int		mv_version;
88};
89
90struct mod_metadata {
91    int		md_version;		/* structure version MDTV_* */
92    int		md_type;		/* type of entry MDT_* */
93    void 	*md_data;		/* specific data */
94    char	*md_cval;		/* common string label */
95};
96
97#ifdef _KERNEL
98
99#include <sys/linker_set.h>
100
101#define MODULE_METADATA(uniquifier, type, data, cval)		\
102    static struct mod_metadata _mod_metadata ## uniquifier = {	\
103	MDT_STRUCT_VERSION,				\
104	type,						\
105	data,						\
106	cval						\
107    };							\
108    DATA_SET(modmetadata_set, _mod_metadata ## uniquifier)
109
110#define MODULE_DEPEND(module, mdepend, vmin, vpref, vmax) \
111    static struct mod_depend _ ##module ## _depend_on_ ## mdepend = {	\
112	vmin,					\
113	vpref,					\
114	vmax					\
115    };						\
116    MODULE_METADATA(_md_ ##module ## _on_ ##mdepend, MDT_DEPEND, \
117	&_ ##module ## _depend_on_ ##mdepend, #mdepend)
118
119#define DECLARE_MODULE(name, data, sub, order) \
120    MODULE_METADATA(_md_ ##name, MDT_MODULE, &data, #name); \
121    SYSINIT(name##module, sub, order, module_register_init, &data) \
122    struct __hack
123
124#define MODULE_VERSION(module, version) \
125    static struct mod_version _ ## module ## _version = {	\
126	version							\
127    };								\
128    MODULE_METADATA(_ ## module ## _version, MDT_VERSION, 	\
129	& _ ## module ## _version, #module)
130
131void module_register_init(const void *data);
132struct linker_file;
133int module_register(const struct moduledata *data, struct linker_file *lf);
134module_t module_lookupbyname(const char *name);
135module_t module_lookupbyid(int modid);
136void module_reference(module_t mod);
137void module_release(module_t mod);
138int module_unload(module_t mod);
139int module_getid(module_t mod);
140module_t module_getfnext(module_t mod);
141void module_setspecific(module_t mod, modspecific_t *datap);
142
143#ifdef MOD_DEBUG
144
145extern int mod_debug;
146#define MOD_DEBUG_REFS	1
147
148#define MOD_DPF(cat, args)					\
149	do {							\
150		if (mod_debug & MOD_DEBUG_##cat) printf args;	\
151	} while (0)
152
153#else
154
155#define MOD_DPF(cat, args)
156
157#endif
158
159#endif /* _KERNEL */
160
161#define MAXMODNAME	32
162
163struct module_stat {
164    int		version;	/* set to sizeof(struct module_stat) */
165    char	name[MAXMODNAME];
166    int		refs;
167    int		id;
168    modspecific_t data;
169};
170
171#ifndef _KERNEL
172
173#include <sys/cdefs.h>
174
175__BEGIN_DECLS
176int	modnext(int modid);
177int	modfnext(int modid);
178int	modstat(int modid, struct module_stat* stat);
179int	modfind(const char *name);
180__END_DECLS
181
182#endif
183
184#endif	/* !_SYS_MODULE_H_ */
185