kern_conf.c revision 49826
1/*-
2 * Parts Copyright (c) 1995 Terrence R. Lambert
3 * Copyright (c) 1995 Julian R. Elischer
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *      This product includes software developed by Terrence R. Lambert.
17 * 4. The name Terrence R. Lambert may not be used to endorse or promote
18 *    products derived from this software without specific prior written
19 *    permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Julian R. Elischer ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $Id: kern_conf.c,v 1.56 1999/08/13 10:29:20 phk Exp $
34 */
35
36#include <sys/param.h>
37#include <sys/kernel.h>
38#include <sys/systm.h>
39#include <sys/module.h>
40#include <sys/malloc.h>
41#include <sys/conf.h>
42#include <sys/vnode.h>
43#include <sys/queue.h>
44#include <machine/stdarg.h>
45
46#define cdevsw_ALLOCSTART	(NUMCDEVSW/2)
47
48struct cdevsw 	*cdevsw[NUMCDEVSW];
49
50static int	bmaj2cmaj[NUMCDEVSW];
51
52MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
53
54#define DEVT_HASH 83
55#define DEVT_STASH 50
56
57static struct specinfo devt_stash[DEVT_STASH];
58
59static SLIST_HEAD(devt_hash_head, specinfo) dev_hash[DEVT_HASH];
60
61/*
62 * Routine to convert from character to block device number.
63 *
64 * A minimal stub routine can always return NODEV.
65 */
66dev_t
67chrtoblk(dev_t dev)
68{
69	struct cdevsw *cd;
70
71	if((cd = devsw(dev)) != NULL) {
72          if (cd->d_bmaj != -1)
73	    return(makebdev(cd->d_bmaj,minor(dev)));
74	}
75	return(NODEV);
76}
77
78struct cdevsw *
79devsw(dev_t dev)
80{
81	if (dev->si_devsw)
82		return (dev->si_devsw);
83        return(cdevsw[major(dev)]);
84}
85
86/*
87 *  Add a cdevsw entry
88 */
89
90int
91cdevsw_add(struct cdevsw *newentry)
92{
93	int i;
94	static int setup;
95
96	if (!setup) {
97		for (i = 0; i < NUMCDEVSW; i++)
98			if (!bmaj2cmaj[i])
99				bmaj2cmaj[i] = 254;
100		setup++;
101	}
102
103	if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) {
104		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
105		    newentry->d_name, newentry->d_maj);
106		return EINVAL;
107	}
108
109	if (cdevsw[newentry->d_maj]) {
110		printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n",
111		    newentry->d_name, cdevsw[newentry->d_maj]->d_name);
112	}
113	cdevsw[newentry->d_maj] = newentry;
114
115	if (newentry->d_bmaj >= 0 && newentry->d_bmaj < NUMCDEVSW) {
116		if (bmaj2cmaj[newentry->d_bmaj] != 254) {
117			printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n",
118			    newentry->d_name,
119			    cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name);
120		}
121		bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj;
122	}
123
124	return 0;
125}
126
127/*
128 *  Remove a cdevsw entry
129 */
130
131int
132cdevsw_remove(struct cdevsw *oldentry)
133{
134	if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
135		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
136		    oldentry->d_name, oldentry->d_maj);
137		return EINVAL;
138	}
139
140	cdevsw[oldentry->d_maj] = NULL;
141
142	if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW)
143		bmaj2cmaj[oldentry->d_bmaj] = 254;
144
145	return 0;
146}
147
148int
149devsw_module_handler(module_t mod, int what, void* arg)
150{
151	struct devsw_module_data* data = (struct devsw_module_data*) arg;
152	int error = 0;
153
154	switch (what) {
155	case MOD_LOAD:
156		error = cdevsw_add(data->cdevsw);
157		if (!error && data->chainevh)
158			error = data->chainevh(mod, what, data->chainarg);
159		return error;
160
161	case MOD_UNLOAD:
162		if (data->chainevh) {
163			error = data->chainevh(mod, what, data->chainarg);
164			if (error)
165				return error;
166		}
167		cdevsw_remove(data->cdevsw);
168		return error;
169	}
170
171	if (data->chainevh)
172		return data->chainevh(mod, what, data->chainarg);
173	else
174		return 0;
175}
176
177/*
178 * dev_t and u_dev_t primitives
179 */
180
181int
182major(dev_t x)
183{
184	if (x == NODEV)
185		return NOUDEV;
186	return((x->si_udev >> 8) & 0xff);
187}
188
189int
190minor(dev_t x)
191{
192	if (x == NODEV)
193		return NOUDEV;
194	return(x->si_udev & 0xffff00ff);
195}
196
197int
198lminor(dev_t x)
199{
200	int i;
201
202	if (x == NODEV)
203		return NOUDEV;
204	i = minor(x);
205	return ((i & 0xff) | (i >> 8));
206}
207
208dev_t
209makebdev(int x, int y)
210{
211	return (makedev(bmaj2cmaj[x], y));
212}
213
214dev_t
215makedev(int x, int y)
216{
217	struct specinfo *si;
218	udev_t	udev;
219	int hash;
220	static int stashed;
221
222	udev = (x << 8) | y;
223	hash = udev % DEVT_HASH;
224	SLIST_FOREACH(si, &dev_hash[hash], si_hash) {
225		if (si->si_udev == udev)
226			return (si);
227	}
228	if (stashed >= DEVT_STASH) {
229		MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
230		    M_USE_RESERVE);
231	} else {
232		si = devt_stash + stashed++;
233	}
234	bzero(si, sizeof(*si));
235	si->si_udev = udev;
236	si->si_bsize_phys = DEV_BSIZE;
237	si->si_bsize_best = BLKDEV_IOSIZE;
238	si->si_bsize_max = MAXBSIZE;
239	if (y > 256)
240		sprintf(si->si_name, "#%d/0x%x", x, y);
241	else
242		sprintf(si->si_name, "#%d/%d", x, y);
243	SLIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
244        return (si);
245}
246
247udev_t
248dev2udev(dev_t x)
249{
250	if (x == NODEV)
251		return NOUDEV;
252	return (x->si_udev);
253}
254
255udev_t
256dev2budev(dev_t x)
257{
258	if (x == NODEV)
259		return NOUDEV;
260	else
261		return makeudev(devsw(x)->d_bmaj, minor(x));
262}
263
264dev_t
265udev2dev(udev_t x, int b)
266{
267	switch (b) {
268		case 0:
269			return makedev(umajor(x), uminor(x));
270		case 1:
271			return makebdev(umajor(x), uminor(x));
272		default:
273			Debugger("udev2dev(...,X)");
274			return NODEV;
275	}
276}
277
278int
279uminor(udev_t dev)
280{
281	return(dev & 0xffff00ff);
282}
283
284int
285umajor(udev_t dev)
286{
287	return((dev & 0xff00) >> 8);
288}
289
290udev_t
291makeudev(int x, int y)
292{
293        return ((x << 8) | y);
294}
295
296dev_t
297make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...)
298{
299	dev_t	dev;
300	va_list ap;
301	int i;
302
303	dev = makedev(devsw->d_maj, minor);
304	va_start(ap, fmt);
305	i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
306	dev->si_name[i] = '\0';
307	va_end(ap);
308	dev->si_devsw = devsw;
309	return (dev);
310}
311
312