kern_conf.c revision 48240
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.44 1999/06/25 07:49:00 grog 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 = 0;
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		cdevsw_remove(data->cdevsw);
178		return error;
179	}
180
181	if (data->chainevh)
182		return data->chainevh(mod, what, data->chainarg);
183	else
184		return 0;
185}
186
187/*
188 * dev_t and u_dev_t primitives
189 */
190
191#define DEVT_FASCIST 1
192
193int
194major(dev_t x)
195{
196	uintptr_t i = (uintptr_t)x;
197
198#ifdef DEVT_FASCIST
199	return(255 - ((i >> 8) & 0xff));
200#else
201	return((i >> 8) & 0xff);
202#endif
203}
204
205int
206minor(dev_t x)
207{
208	uintptr_t i = (uintptr_t)x;
209
210	return(i & 0xffff00ff);
211}
212
213dev_t
214makebdev(int x, int y)
215{
216	return (makedev(x, y));
217}
218
219dev_t
220makedev(int x, int y)
221{
222#ifdef DEVT_FASCIST
223        return ((dev_t)(uintptr_t) (((255 - x) << 8) | y));
224#else
225        return ((dev_t)(uintptr_t) ((x << 8) | y));
226#endif
227}
228
229udev_t
230dev2udev(dev_t x)
231{
232	return umakedev(major(x), minor(x));
233}
234
235dev_t
236udev2dev(udev_t x, int b)
237{
238	return makedev(umajor(x), uminor(x));
239}
240
241int
242uminor(udev_t dev)
243{
244	return(dev & 0xffff00ff);
245}
246
247int
248umajor(udev_t dev)
249{
250	return((dev & 0xff00) >> 8);
251}
252
253udev_t
254umakedev(int x, int y)
255{
256        return ((x << 8) | y);
257}
258
259