kern_conf.c revision 48936
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.50 1999/07/19 09:37:59 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 45#include <miscfs/specfs/specdev.h> 46 47#define cdevsw_ALLOCSTART (NUMCDEVSW/2) 48 49struct cdevsw *cdevsw[NUMCDEVSW]; 50 51static int bmaj2cmaj[NUMCDEVSW]; 52 53MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage"); 54 55#define DEVT_HASH 83 56#define DEVT_STASH 50 57 58static struct specinfo devt_stash[DEVT_STASH]; 59 60static SLIST_HEAD(devt_hash_head, specinfo) dev_hash[DEVT_HASH]; 61 62/* 63 * Routine to convert from character to block device number. 64 * 65 * A minimal stub routine can always return NODEV. 66 */ 67dev_t 68chrtoblk(dev_t dev) 69{ 70 struct cdevsw *cd; 71 72 if((cd = devsw(dev)) != NULL) { 73 if (cd->d_bmaj != -1) 74 return(makebdev(cd->d_bmaj,minor(dev))); 75 } 76 return(NODEV); 77} 78 79struct cdevsw * 80devsw(dev_t dev) 81{ 82 return(cdevsw[major(dev)]); 83} 84 85struct cdevsw * 86bdevsw(dev_t dev) 87{ 88 return(cdevsw[major(dev)]); 89} 90 91/* 92 * Add a cdevsw entry 93 */ 94 95int 96cdevsw_add(struct cdevsw *newentry) 97{ 98 int i; 99 static int setup; 100 101 if (!setup) { 102 for (i = 0; i < NUMCDEVSW; i++) 103 if (!bmaj2cmaj[i]) 104 bmaj2cmaj[i] = 254; 105 setup++; 106 } 107 108 if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) { 109 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", 110 newentry->d_name, newentry->d_maj); 111 return EINVAL; 112 } 113 114 if (cdevsw[newentry->d_maj]) { 115 printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n", 116 newentry->d_name, cdevsw[newentry->d_maj]->d_name); 117 } 118 cdevsw[newentry->d_maj] = newentry; 119 120 if (newentry->d_bmaj >= 0 && newentry->d_bmaj < NUMCDEVSW) { 121 if (bmaj2cmaj[newentry->d_bmaj] != 254) { 122 printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n", 123 newentry->d_name, 124 cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name); 125 } 126 bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj; 127 } 128 129 return 0; 130} 131 132/* 133 * Remove a cdevsw entry 134 */ 135 136int 137cdevsw_remove(struct cdevsw *oldentry) 138{ 139 if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) { 140 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", 141 oldentry->d_name, oldentry->d_maj); 142 return EINVAL; 143 } 144 145 cdevsw[oldentry->d_maj] = NULL; 146 147 if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW) 148 bmaj2cmaj[oldentry->d_bmaj] = NULL; 149 150 return 0; 151} 152 153int 154devsw_module_handler(module_t mod, int what, void* arg) 155{ 156 struct devsw_module_data* data = (struct devsw_module_data*) arg; 157 int error = 0; 158 159 switch (what) { 160 case MOD_LOAD: 161 error = cdevsw_add(data->cdevsw); 162 if (!error && data->chainevh) 163 error = data->chainevh(mod, what, data->chainarg); 164 return error; 165 166 case MOD_UNLOAD: 167 if (data->chainevh) { 168 error = data->chainevh(mod, what, data->chainarg); 169 if (error) 170 return error; 171 } 172 cdevsw_remove(data->cdevsw); 173 return error; 174 } 175 176 if (data->chainevh) 177 return data->chainevh(mod, what, data->chainarg); 178 else 179 return 0; 180} 181 182/* 183 * dev_t and u_dev_t primitives 184 */ 185 186int 187major(dev_t x) 188{ 189 if (x == NODEV) 190 return NOUDEV; 191 return((x->si_udev >> 8) & 0xff); 192} 193 194int 195minor(dev_t x) 196{ 197 if (x == NODEV) 198 return NOUDEV; 199 return(x->si_udev & 0xffff00ff); 200} 201 202dev_t 203makebdev(int x, int y) 204{ 205 return (makedev(bmaj2cmaj[x], y)); 206} 207 208dev_t 209makedev(int x, int y) 210{ 211 struct specinfo *si; 212 udev_t udev; 213 int hash; 214 static int stashed; 215 216 udev = (x << 8) | y; 217 hash = udev % DEVT_HASH; 218 SLIST_FOREACH(si, &dev_hash[hash], si_hash) { 219 if (si->si_udev == udev) 220 return (si); 221 } 222 if (stashed >= DEVT_STASH) { 223 MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT, 224 M_USE_RESERVE); 225 } else { 226 si = devt_stash + stashed++; 227 } 228 bzero(si, sizeof(*si)); 229 si->si_udev = udev; 230 si->si_bsize_phys = DEV_BSIZE; 231 si->si_bsize_best = BLKDEV_IOSIZE; 232 si->si_bsize_max = MAXBSIZE; 233 SLIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 234 return (si); 235} 236 237udev_t 238dev2udev(dev_t x) 239{ 240 if (x == NODEV) 241 return NOUDEV; 242 return (x->si_udev); 243} 244 245dev_t 246udev2dev(udev_t x, int b) 247{ 248 switch (b) { 249 case 0: 250 return makedev(umajor(x), uminor(x)); 251 case 1: 252 return makebdev(umajor(x), uminor(x)); 253 default: 254 Debugger("udev2dev(...,X)"); 255 return NODEV; 256 } 257} 258 259int 260uminor(udev_t dev) 261{ 262 return(dev & 0xffff00ff); 263} 264 265int 266umajor(udev_t dev) 267{ 268 return((dev & 0xff00) >> 8); 269} 270 271udev_t 272makeudev(int x, int y) 273{ 274 return ((x << 8) | y); 275} 276 277