kern_conf.c revision 49679
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.55 1999/08/08 18:42:47 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
197dev_t
198makebdev(int x, int y)
199{
200	return (makedev(bmaj2cmaj[x], y));
201}
202
203dev_t
204makedev(int x, int y)
205{
206	struct specinfo *si;
207	udev_t	udev;
208	int hash;
209	static int stashed;
210
211	udev = (x << 8) | y;
212	hash = udev % DEVT_HASH;
213	SLIST_FOREACH(si, &dev_hash[hash], si_hash) {
214		if (si->si_udev == udev)
215			return (si);
216	}
217	if (stashed >= DEVT_STASH) {
218		MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
219		    M_USE_RESERVE);
220	} else {
221		si = devt_stash + stashed++;
222	}
223	bzero(si, sizeof(*si));
224	si->si_udev = udev;
225	si->si_bsize_phys = DEV_BSIZE;
226	si->si_bsize_best = BLKDEV_IOSIZE;
227	si->si_bsize_max = MAXBSIZE;
228	if (y > 256)
229		sprintf(si->si_name, "#%d/0x%x", x, y);
230	else
231		sprintf(si->si_name, "#%d/%d", x, y);
232	SLIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
233        return (si);
234}
235
236udev_t
237dev2udev(dev_t x)
238{
239	if (x == NODEV)
240		return NOUDEV;
241	return (x->si_udev);
242}
243
244udev_t
245dev2budev(dev_t x)
246{
247	if (x == NODEV)
248		return NOUDEV;
249	else
250		return makeudev(devsw(x)->d_bmaj, minor(x));
251}
252
253dev_t
254udev2dev(udev_t x, int b)
255{
256	switch (b) {
257		case 0:
258			return makedev(umajor(x), uminor(x));
259		case 1:
260			return makebdev(umajor(x), uminor(x));
261		default:
262			Debugger("udev2dev(...,X)");
263			return NODEV;
264	}
265}
266
267int
268uminor(udev_t dev)
269{
270	return(dev & 0xffff00ff);
271}
272
273int
274umajor(udev_t dev)
275{
276	return((dev & 0xff00) >> 8);
277}
278
279udev_t
280makeudev(int x, int y)
281{
282        return ((x << 8) | y);
283}
284
285dev_t
286make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...)
287{
288	dev_t	dev;
289	va_list ap;
290	int i;
291
292	dev = makedev(devsw->d_maj, minor);
293	va_start(ap, fmt);
294	i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
295	dev->si_name[i] = '\0';
296	va_end(ap);
297	dev->si_devsw = devsw;
298	return (dev);
299}
300
301