kern_conf.c revision 48510
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.45 1999/06/26 11:39:27 dfr 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	if (cdevsw[newentry->d_maj]) {
116		printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n",
117		    newentry->d_name, cdevsw[newentry->d_maj]->d_name);
118	}
119	cdevsw[newentry->d_maj] = newentry;
120
121	if (newentry->d_bmaj >= 0 && newentry->d_bmaj < NUMCDEVSW) {
122		if (bmaj2cmaj[newentry->d_bmaj] != 254) {
123			printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n",
124			    newentry->d_name,
125			    cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name);
126		}
127		bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj;
128	}
129
130	return 0;
131}
132
133/*
134 *  Remove a cdevsw entry
135 */
136
137int
138cdevsw_remove(struct cdevsw *oldentry)
139{
140	if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
141		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
142		    oldentry->d_name, oldentry->d_maj);
143		return EINVAL;
144	}
145
146	cdevsw[oldentry->d_maj] = NULL;
147
148	if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW)
149		bmaj2cmaj[oldentry->d_bmaj] = NULL;
150
151	return 0;
152}
153
154int
155devsw_module_handler(module_t mod, int what, void* arg)
156{
157	struct devsw_module_data* data = (struct devsw_module_data*) arg;
158	int error = 0;
159
160	if (data->cmaj == NOMAJ)
161		data->cdev = NODEV;
162	else
163		data->cdev = makedev(data->cmaj, 0);
164	switch (what) {
165	case MOD_LOAD:
166		error = cdevsw_add(data->cdevsw);
167		if (!error && data->cdevsw->d_strategy != nostrategy) {
168			if (data->bmaj == NOMAJ) {
169				data->bdev = data->cdev;
170				data->bmaj = data->cmaj;
171			} else {
172				data->bdev = makedev(data->bmaj, 0);
173			}
174			data->cdevsw->d_maj = data->bmaj;
175			bmaj2cmaj[major(data->bdev)] = major(data->cdev);
176		}
177		if (!error && data->chainevh)
178			error = data->chainevh(mod, what, data->chainarg);
179		return error;
180
181	case MOD_UNLOAD:
182		if (data->chainevh) {
183			error = data->chainevh(mod, what, data->chainarg);
184			if (error)
185				return error;
186		}
187		cdevsw_remove(data->cdevsw);
188		return error;
189	}
190
191	if (data->chainevh)
192		return data->chainevh(mod, what, data->chainarg);
193	else
194		return 0;
195}
196
197/*
198 * dev_t and u_dev_t primitives
199 */
200
201#define DEVT_FASCIST 1
202
203int
204major(dev_t x)
205{
206	uintptr_t i = (uintptr_t)x;
207
208#ifdef DEVT_FASCIST
209	return(255 - ((i >> 8) & 0xff));
210#else
211	return((i >> 8) & 0xff);
212#endif
213}
214
215int
216minor(dev_t x)
217{
218	uintptr_t i = (uintptr_t)x;
219
220	return(i & 0xffff00ff);
221}
222
223dev_t
224makebdev(int x, int y)
225{
226	return (makedev(x, y));
227}
228
229dev_t
230makedev(int x, int y)
231{
232#ifdef DEVT_FASCIST
233        return ((dev_t)(uintptr_t) (((255 - x) << 8) | y));
234#else
235        return ((dev_t)(uintptr_t) ((x << 8) | y));
236#endif
237}
238
239udev_t
240dev2udev(dev_t x)
241{
242	return umakedev(major(x), minor(x));
243}
244
245dev_t
246udev2dev(udev_t x, int b)
247{
248	return makedev(umajor(x), uminor(x));
249}
250
251int
252uminor(udev_t dev)
253{
254	return(dev & 0xffff00ff);
255}
256
257int
258umajor(udev_t dev)
259{
260	return((dev & 0xff00) >> 8);
261}
262
263udev_t
264umakedev(int x, int y)
265{
266        return ((x << 8) | y);
267}
268
269