kern_conf.c revision 48211
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.43 1999/06/01 20:41:26 dt Exp $
34 */
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/module.h>
39#include <sys/conf.h>
40#include <sys/vnode.h>
41
42#define cdevsw_ALLOCSTART	(NUMCDEVSW/2)
43
44struct cdevsw 	*cdevsw[NUMCDEVSW];
45
46int	bmaj2cmaj[NUMCDEVSW];
47
48/*
49 * Routine to convert from character to block device number.
50 *
51 * A minimal stub routine can always return NODEV.
52 */
53dev_t
54chrtoblk(dev_t dev)
55{
56	struct cdevsw *cd;
57
58	if((cd = devsw(dev)) != NULL) {
59          if (cd->d_bmaj != -1)
60	    return(makedev(cd->d_bmaj,minor(dev)));
61	}
62	return(NODEV);
63}
64
65struct cdevsw *
66devsw(dev_t dev)
67{
68        return(cdevsw[major(dev)]);
69}
70
71struct cdevsw *
72bdevsw(dev_t dev)
73{
74        struct cdevsw *c;
75        int i = major(dev);
76
77        if (bmaj2cmaj[i] == 254)
78                return 0;
79
80        c = cdevsw[bmaj2cmaj[major(dev)]];
81        if (!c) {
82                printf("bogus bdev dev_t %p, no cdev\n", (void *)dev);
83                Debugger("Bummer");
84                return 0;
85        }
86        /* CMAJ zero is the console, which has no strategy so this works */
87        if (c->d_strategy)
88                return (c);
89        return (0);
90}
91
92/*
93 *  Add a cdevsw entry
94 */
95
96int
97cdevsw_add(struct cdevsw *newentry)
98{
99	int i;
100	static int setup;
101
102	if (!setup) {
103		for (i = 0; i < NUMCDEVSW; i++)
104			if (!bmaj2cmaj[i])
105				bmaj2cmaj[i] = 254;
106		setup++;
107	}
108
109	if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) {
110		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
111		    newentry->d_name, newentry->d_maj);
112		return EINVAL;
113	}
114
115	cdevsw[newentry->d_maj] = newentry;
116
117	if (newentry->d_bmaj >= 0 && newentry->d_bmaj < NUMCDEVSW)
118		bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj;
119
120	return 0;
121}
122
123/*
124 *  Remove a cdevsw entry
125 */
126
127int
128cdevsw_remove(struct cdevsw *oldentry)
129{
130	if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
131		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
132		    oldentry->d_name, oldentry->d_maj);
133		return EINVAL;
134	}
135
136	cdevsw[oldentry->d_maj] = NULL;
137
138	if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW)
139		bmaj2cmaj[oldentry->d_bmaj] = NULL;
140
141	return 0;
142}
143
144int
145devsw_module_handler(module_t mod, int what, void* arg)
146{
147	struct devsw_module_data* data = (struct devsw_module_data*) arg;
148	int error;
149
150	if (data->cmaj == NOMAJ)
151		data->cdev = NODEV;
152	else
153		data->cdev = makedev(data->cmaj, 0);
154	switch (what) {
155	case MOD_LOAD:
156		error = cdevsw_add(data->cdevsw);
157		if (!error && data->cdevsw->d_strategy != nostrategy) {
158			if (data->bmaj == NOMAJ) {
159				data->bdev = data->cdev;
160				data->bmaj = data->cmaj;
161			} else {
162				data->bdev = makedev(data->bmaj, 0);
163			}
164			data->cdevsw->d_maj = data->bmaj;
165			bmaj2cmaj[major(data->bdev)] = major(data->cdev);
166		}
167		if (!error && data->chainevh)
168			error = data->chainevh(mod, what, data->chainarg);
169		return error;
170
171	case MOD_UNLOAD:
172		if (data->chainevh) {
173			error = data->chainevh(mod, what, data->chainarg);
174			if (error)
175				return error;
176		}
177		if (data->cdevsw->d_strategy != nostrategy)
178			bmaj2cmaj[major(data->bdev)] = 0;
179		return error;
180	}
181
182	if (data->chainevh)
183		return data->chainevh(mod, what, data->chainarg);
184	else
185		return 0;
186}
187
188/*
189 * dev_t and u_dev_t primitives
190 */
191
192#define DEVT_FASCIST 1
193
194int
195major(dev_t x)
196{
197	uintptr_t i = (uintptr_t)x;
198
199#ifdef DEVT_FASCIST
200	return(255 - ((i >> 8) & 0xff));
201#else
202	return((i >> 8) & 0xff);
203#endif
204}
205
206int
207minor(dev_t x)
208{
209	uintptr_t i = (uintptr_t)x;
210
211	return(i & 0xffff00ff);
212}
213
214dev_t
215makebdev(int x, int y)
216{
217	return (makedev(x, y));
218}
219
220dev_t
221makedev(int x, int y)
222{
223#ifdef DEVT_FASCIST
224        return ((dev_t) (((255 - x) << 8) | y));
225#else
226        return ((dev_t) ((x << 8) | y));
227#endif
228}
229
230udev_t
231dev2udev(dev_t x)
232{
233	return umakedev(major(x), minor(x));
234}
235
236dev_t
237udev2dev(udev_t x, int b)
238{
239	return makedev(umajor(x), uminor(x));
240}
241
242int
243uminor(udev_t dev)
244{
245	return(dev & 0xffff00ff);
246}
247
248int
249umajor(udev_t dev)
250{
251	return((dev & 0xff00) >> 8);
252}
253
254udev_t
255umakedev(int x, int y)
256{
257        return ((x << 8) | y);
258}
259
260