kern_conf.c revision 12968
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.8 1995/12/21 20:09:39 julian Exp $
34 */
35
36#include <sys/param.h>
37#include <sys/types.h>
38#include <sys/systm.h>
39#include <sys/conf.h>
40#include <sys/vnode.h>
41
42#define NUMCDEV 96
43#define NUMBDEV 32
44
45struct bdevsw 	*bdevsw[NUMBDEV];
46int	nblkdev = NUMBDEV;
47struct cdevsw 	*cdevsw[NUMCDEV];
48int	nchrdev = NUMCDEV;
49
50
51
52/*
53 * Routine to determine if a device is a disk.
54 *
55 * KLUDGE XXX add flags to cdevsw entries for disks XXX
56 * A minimal stub routine can always return 0.
57 */
58int
59isdisk(dev, type)
60	dev_t dev;
61	int type;
62{
63
64	switch (major(dev)) {
65	case 15:		/* VBLK: vn, VCHR: cd */
66		return (1);
67	case 0:			/* wd */
68	case 2:			/* fd */
69	case 4:			/* sd */
70	case 6:			/* cd */
71	case 7:			/* mcd */
72	case 16:		/* scd */
73	case 17:		/* matcd */
74	case 18:		/* ata */
75	case 19:		/* wcd */
76	case 20:		/* od */
77		if (type == VBLK)
78			return (1);
79		return (0);
80	case 3:			/* wd */
81	case 9:			/* fd */
82	case 13:		/* sd */
83	case 29:		/* mcd */
84	case 43:		/* vn */
85	case 45:		/* scd */
86	case 46:		/* matcd */
87	case 69:		/* wcd */
88	case 70:		/* od */
89		if (type == VCHR)
90			return (1);
91		/* fall through */
92	default:
93		return (0);
94	}
95	/* NOTREACHED */
96}
97
98
99/*
100 * Routine to convert from character to block device number.
101 *
102 * A minimal stub routine can always return NODEV.
103 */
104dev_t
105chrtoblk(dev_t dev)
106{
107	struct bdevsw *bd;
108	struct cdevsw *cd;
109
110	if(cd = cdevsw[major(dev)]) {
111          if ( (bd = cd->d_bdev) )
112	    return(makedev(bd->d_maj,minor(dev)));
113	}
114	return(NODEV);
115}
116
117/*
118 * (re)place an entry in the bdevsw or cdevsw table
119 * return the slot used in major(*descrip)
120 */
121#define ADDENTRY(TTYPE,NXXXDEV) \
122int TTYPE##_add(dev_t *descrip,						\
123		struct TTYPE *newentry,					\
124		struct TTYPE **oldentry)				\
125{									\
126	int i ;								\
127	if ( (int)*descrip == -1) {	/* auto (0 is valid) */		\
128		/*							\
129		 * Search the table looking for a slot...		\
130		 */							\
131		for (i = 0; i < NXXXDEV; i++)				\
132			if (TTYPE[i] == NULL)				\
133				break;		/* found one! */	\
134		/* out of allocable slots? */				\
135		if (i == NXXXDEV) {					\
136			return ENFILE;					\
137		}							\
138	} else {				/* assign */		\
139		i = major(*descrip);					\
140		if (i < 0 || i >= NXXXDEV) {				\
141			return EINVAL;					\
142		}							\
143	}								\
144									\
145	/* maybe save old */						\
146        if (oldentry) {							\
147		*oldentry = TTYPE[i];					\
148	}								\
149	newentry->d_maj = i;						\
150	/* replace with new */						\
151	TTYPE[i] = newentry;						\
152									\
153	/* done!  let them know where we put it */			\
154	*descrip = makedev(i,0);					\
155	return 0;							\
156} \
157
158ADDENTRY(bdevsw, nblkdev)
159ADDENTRY(cdevsw, nchrdev)
160