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$
2725537Sdfr */
2825537Sdfr
2925537Sdfr#ifndef _SYS_MODULE_H_
3025537Sdfr#define _SYS_MODULE_H_
3125537Sdfr
3259751Speter/*
3359751Speter * Module metadata types
3459751Speter */
3559751Speter#define	MDT_DEPEND	1		/* argument is a module name */
3659751Speter#define	MDT_MODULE	2		/* module declaration */
3759751Speter#define	MDT_VERSION	3		/* module version(s) */
38277205Simp#define	MDT_PNP_INFO	4		/* Plug and play hints record */
3959751Speter
4059751Speter#define	MDT_STRUCT_VERSION	1	/* version of metadata structure */
4159751Speter#define	MDT_SETNAME	"modmetadata_set"
4259751Speter
4341153Swollmantypedef enum modeventtype {
4491472Sarr	MOD_LOAD,
4591472Sarr	MOD_UNLOAD,
46132117Sphk	MOD_SHUTDOWN,
47132117Sphk	MOD_QUIESCE
4825537Sdfr} modeventtype_t;
4925537Sdfr
5091472Sarrtypedef struct module *module_t;
5191472Sarrtypedef int (*modeventhand_t)(module_t, int /* modeventtype_t */, void *);
5225537Sdfr
5325537Sdfr/*
5425537Sdfr * Struct for registering modules statically via SYSINIT.
5525537Sdfr */
5625537Sdfrtypedef struct moduledata {
5791472Sarr	const char	*name;		/* module name */
5891472Sarr	modeventhand_t  evhand;		/* event handler */
5991472Sarr	void		*priv;		/* extra data */
6025537Sdfr} moduledata_t;
6125537Sdfr
6242435Sdfr/*
6391472Sarr * A module can use this to report module specific data to the user via
6491472Sarr * kldstat(2).
6542435Sdfr */
6642435Sdfrtypedef union modspecific {
6791472Sarr	int	intval;
6891472Sarr	u_int	uintval;
6991472Sarr	long	longval;
7091472Sarr	u_long	ulongval;
7142435Sdfr} modspecific_t;
7242435Sdfr
7359751Speter/*
74281463Smarkm * Module dependency declaration
7559751Speter */
7659751Speterstruct mod_depend {
7791472Sarr	int	md_ver_minimum;
7891472Sarr	int	md_ver_preferred;
7991472Sarr	int	md_ver_maximum;
8059751Speter};
8159751Speter
8259751Speter/*
8359751Speter * Module version declaration
8459751Speter */
8559751Speterstruct mod_version {
8691472Sarr	int	mv_version;
8759751Speter};
8859751Speter
8959751Speterstruct mod_metadata {
9091472Sarr	int		md_version;	/* structure version MDTV_* */
9191472Sarr	int		md_type;	/* type of entry MDT_* */
92292077Simp	const void	*md_data;	/* specific data */
9391472Sarr	const char	*md_cval;	/* common string label */
9459751Speter};
9559751Speter
96292077Simpstruct mod_pnp_match_info
97292077Simp{
98292077Simp	const char *descr;	/* Description of the table */
99292077Simp	const char *bus;	/* Name of the bus for this table */
100292077Simp	const void *table;	/* Pointer to pnp table */
101292077Simp	int entry_len;		/* Length of each entry in the table (may be */
102292146Simp				/*   longer than descr describes). */
103292077Simp	int num_entry;		/* Number of entries in the table */
104292077Simp};
10591472Sarr#ifdef	_KERNEL
10640137Speter
10759751Speter#include <sys/linker_set.h>
10859751Speter
10991472Sarr#define	MODULE_METADATA(uniquifier, type, data, cval)			\
11091472Sarr	static struct mod_metadata _mod_metadata##uniquifier = {	\
11191472Sarr		MDT_STRUCT_VERSION,					\
11291472Sarr		type,							\
11391472Sarr		data,							\
11491472Sarr		cval							\
11591472Sarr	};								\
11691472Sarr	DATA_SET(modmetadata_set, _mod_metadata##uniquifier)
11759751Speter
11891472Sarr#define	MODULE_DEPEND(module, mdepend, vmin, vpref, vmax)		\
119283254Sdim	static struct mod_depend _##module##_depend_on_##mdepend	\
120283254Sdim	    __section(".data") = {					\
12191472Sarr		vmin,							\
12291472Sarr		vpref,							\
12391472Sarr		vmax							\
12491472Sarr	};								\
12591472Sarr	MODULE_METADATA(_md_##module##_on_##mdepend, MDT_DEPEND,	\
12691472Sarr	    &_##module##_depend_on_##mdepend, #mdepend)
12759751Speter
128176252Sjhb/*
129176252Sjhb * Every kernel has a 'kernel' module with the version set to
130176252Sjhb * __FreeBSD_version.  We embed a MODULE_DEPEND() inside every module
131176252Sjhb * that depends on the 'kernel' module.  It uses the current value of
132176252Sjhb * __FreeBSD_version as the minimum and preferred versions.  For the
133176252Sjhb * maximum version it rounds the version up to the end of its branch
134176252Sjhb * (i.e. M99999 for M.x).  This allows a module built on M.x to work
135176252Sjhb * on M.y systems where y >= x, but fail on M.z systems where z < x.
136176252Sjhb */
137176252Sjhb#define	MODULE_KERNEL_MAXVER	(roundup(__FreeBSD_version, 100000) - 1)
138176252Sjhb
139213716Skib#define	DECLARE_MODULE_WITH_MAXVER(name, data, sub, order, maxver)	\
140176252Sjhb	MODULE_DEPEND(name, kernel, __FreeBSD_version,			\
141213716Skib	    __FreeBSD_version, maxver);			\
14291472Sarr	MODULE_METADATA(_md_##name, MDT_MODULE, &data, #name);		\
143177253Srwatson	SYSINIT(name##module, sub, order, module_register_init, &data);	\
14491472Sarr	struct __hack
14525537Sdfr
146213716Skib#define	DECLARE_MODULE(name, data, sub, order)				\
147213716Skib	DECLARE_MODULE_WITH_MAXVER(name, data, sub, order, MODULE_KERNEL_MAXVER)
148213716Skib
149213716Skib/*
150213716Skib * The module declared with DECLARE_MODULE_TIED can only be loaded
151213716Skib * into the kernel with exactly the same __FreeBSD_version.
152213716Skib *
153213716Skib * Use it for modules that use kernel interfaces that are not stable
154213716Skib * even on STABLE/X branches.
155213716Skib */
156213716Skib#define	DECLARE_MODULE_TIED(name, data, sub, order)				\
157213716Skib	DECLARE_MODULE_WITH_MAXVER(name, data, sub, order, __FreeBSD_version)
158213716Skib
15991472Sarr#define	MODULE_VERSION(module, version)					\
160283254Sdim	static struct mod_version _##module##_version			\
161283254Sdim	    __section(".data") = {					\
16291472Sarr		version							\
16391472Sarr	};								\
16491472Sarr	MODULE_METADATA(_##module##_version, MDT_VERSION,		\
16591472Sarr	    &_##module##_version, #module)
16659751Speter
167292077Simp/**
168292077Simp * Generic macros to create pnp info hints that modules may export
169298023Sngie * to allow external tools to parse their internal device tables
170292077Simp * to make an informed guess about what driver(s) to load.
171292077Simp */
172292077Simp#define	MODULE_PNP_INFO(d, b, unique, t, l, n)				\
173292077Simp	static const struct mod_pnp_match_info _module_pnp_##b##_##unique = {	\
174292077Simp		.descr = d,						\
175292077Simp		.bus = #b,						\
176292077Simp		.table = t,						\
177292077Simp		.entry_len = l,						\
178292077Simp		.num_entry = n						\
179292077Simp	};								\
180292077Simp	MODULE_METADATA(_md_##b##_pnpinfo_##unique, MDT_PNP_INFO,	\
181292077Simp	    &_module_pnp_##b##_##unique, #b);
182292077Simp/**
183292077Simp * descr is a string that describes each entry in the table. The general
184292077Simp * form is (TYPE:pnp_name[/pnp_name];)*
185292077Simp * where TYPE is one of the following:
186292077Simp *	U8	uint8_t element
187292077Simp *	V8	like U8 and 0xff means match any
188292077Simp *	G16	uint16_t element, any value >= matches
189292077Simp *	L16	uint16_t element, any value <= matches
190292077Simp *	M16	uint16_t element, mask of which of the following fields to use.
191292077Simp *	U16	uint16_t element
192292077Simp *	V16	like U16 and 0xffff means match any
193292077Simp *	U32	uint32_t element
194292077Simp *	V32	like U32 and 0xffffffff means match any
195292077Simp *	W32	Two 16-bit values with first pnp_name in LSW and second in MSW.
196292077Simp *	Z	pointer to a string to match exactly
197292077Simp *	D	like Z, but is the string passed to device_set_descr()
198292077Simp *	P	A pointer that should be ignored
199292077Simp *	E	EISA PNP Identifier (in binary, but bus publishes string)
200292077Simp *	K	Key for whole table. pnp_name=value. must be last, if present.
201292077Simp *
202292077Simp * The pnp_name "#" is reserved for other fields that should be ignored.
203292077Simp */
204292077Simp
20592547Sarrextern struct sx modules_sx;
20692547Sarr
20792547Sarr#define	MOD_XLOCK	sx_xlock(&modules_sx)
20892547Sarr#define	MOD_SLOCK	sx_slock(&modules_sx)
20992547Sarr#define	MOD_XUNLOCK	sx_xunlock(&modules_sx)
21092547Sarr#define	MOD_SUNLOCK	sx_sunlock(&modules_sx)
21192547Sarr#define	MOD_LOCK_ASSERT	sx_assert(&modules_sx, SX_LOCKED)
21292547Sarr#define	MOD_XLOCK_ASSERT	sx_assert(&modules_sx, SX_XLOCKED)
21392547Sarr
21446693Speterstruct linker_file;
21525537Sdfr
21691472Sarrvoid	module_register_init(const void *);
21791472Sarrint	module_register(const struct moduledata *, struct linker_file *);
21891472Sarrmodule_t	module_lookupbyname(const char *);
21991472Sarrmodule_t	module_lookupbyid(int);
220185635Sjhbint	module_quiesce(module_t);
22191472Sarrvoid	module_reference(module_t);
22291472Sarrvoid	module_release(module_t);
223185635Sjhbint	module_unload(module_t);
22491472Sarrint	module_getid(module_t);
22591472Sarrmodule_t	module_getfnext(module_t);
226185635Sjhbconst char *	module_getname(module_t);
22791472Sarrvoid	module_setspecific(module_t, modspecific_t *);
228157818Sjhbstruct linker_file *module_file(module_t);
22925537Sdfr
23091472Sarr#ifdef	MOD_DEBUG
23125537Sdfrextern int mod_debug;
23291472Sarr#define	MOD_DEBUG_REFS	1
23325537Sdfr
23491520Sobrien#define	MOD_DPF(cat, args) do {						\
23591472Sarr	if (mod_debug & MOD_DEBUG_##cat)				\
23691472Sarr		printf(args);						\
23791472Sarr} while (0)
23825537Sdfr
23991472Sarr#else	/* !MOD_DEBUG */
24025537Sdfr
24191472Sarr#define	MOD_DPF(cat, args)
24225537Sdfr#endif
24391472Sarr#endif	/* _KERNEL */
24425537Sdfr
24591472Sarr#define	MAXMODNAME	32
24625537Sdfr
24725537Sdfrstruct module_stat {
24891472Sarr	int		version;	/* set to sizeof(struct module_stat) */
24991472Sarr	char		name[MAXMODNAME];
25091472Sarr	int		refs;
25191472Sarr	int		id;
25291472Sarr	modspecific_t	data;
25325537Sdfr};
25425537Sdfr
25555205Speter#ifndef _KERNEL
25625537Sdfr
25725537Sdfr#include <sys/cdefs.h>
25825537Sdfr
25925537Sdfr__BEGIN_DECLS
26083248Sddint	modnext(int _modid);
26183248Sddint	modfnext(int _modid);
26291472Sarrint	modstat(int _modid, struct module_stat *_stat);
26383248Sddint	modfind(const char *_name);
26425537Sdfr__END_DECLS
26525537Sdfr
26625537Sdfr#endif
26725537Sdfr
26825537Sdfr#endif	/* !_SYS_MODULE_H_ */
269