kern_conf.c revision 49535
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.54 1999/08/08 00:34:00 grog 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 return(cdevsw[major(dev)]); 82} 83 84struct cdevsw * 85bdevsw(dev_t dev) 86{ 87 return(cdevsw[major(dev)]); 88} 89 90/* 91 * Add a cdevsw entry 92 */ 93 94int 95cdevsw_add(struct cdevsw *newentry) 96{ 97 int i; 98 static int setup; 99 100 if (!setup) { 101 for (i = 0; i < NUMCDEVSW; i++) 102 if (!bmaj2cmaj[i]) 103 bmaj2cmaj[i] = 254; 104 setup++; 105 } 106 107 if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) { 108 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", 109 newentry->d_name, newentry->d_maj); 110 return EINVAL; 111 } 112 113 if (cdevsw[newentry->d_maj]) { 114 printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n", 115 newentry->d_name, cdevsw[newentry->d_maj]->d_name); 116 } 117 cdevsw[newentry->d_maj] = newentry; 118 119 if (newentry->d_bmaj >= 0 && newentry->d_bmaj < NUMCDEVSW) { 120 if (bmaj2cmaj[newentry->d_bmaj] != 254) { 121 printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n", 122 newentry->d_name, 123 cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name); 124 } 125 bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj; 126 } 127 128 return 0; 129} 130 131/* 132 * Remove a cdevsw entry 133 */ 134 135int 136cdevsw_remove(struct cdevsw *oldentry) 137{ 138 if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) { 139 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", 140 oldentry->d_name, oldentry->d_maj); 141 return EINVAL; 142 } 143 144 cdevsw[oldentry->d_maj] = NULL; 145 146 if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW) 147 bmaj2cmaj[oldentry->d_bmaj] = 254; 148 149 return 0; 150} 151 152int 153devsw_module_handler(module_t mod, int what, void* arg) 154{ 155 struct devsw_module_data* data = (struct devsw_module_data*) arg; 156 int error = 0; 157 158 switch (what) { 159 case MOD_LOAD: 160 error = cdevsw_add(data->cdevsw); 161 if (!error && data->chainevh) 162 error = data->chainevh(mod, what, data->chainarg); 163 return error; 164 165 case MOD_UNLOAD: 166 if (data->chainevh) { 167 error = data->chainevh(mod, what, data->chainarg); 168 if (error) 169 return error; 170 } 171 cdevsw_remove(data->cdevsw); 172 return error; 173 } 174 175 if (data->chainevh) 176 return data->chainevh(mod, what, data->chainarg); 177 else 178 return 0; 179} 180 181/* 182 * dev_t and u_dev_t primitives 183 */ 184 185int 186major(dev_t x) 187{ 188 if (x == NODEV) 189 return NOUDEV; 190 return((x->si_udev >> 8) & 0xff); 191} 192 193int 194minor(dev_t x) 195{ 196 if (x == NODEV) 197 return NOUDEV; 198 return(x->si_udev & 0xffff00ff); 199} 200 201dev_t 202makebdev(int x, int y) 203{ 204 return (makedev(bmaj2cmaj[x], y)); 205} 206 207dev_t 208makedev(int x, int y) 209{ 210 struct specinfo *si; 211 udev_t udev; 212 int hash; 213 static int stashed; 214 215 udev = (x << 8) | y; 216 hash = udev % DEVT_HASH; 217 SLIST_FOREACH(si, &dev_hash[hash], si_hash) { 218 if (si->si_udev == udev) 219 return (si); 220 } 221 if (stashed >= DEVT_STASH) { 222 MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT, 223 M_USE_RESERVE); 224 } else { 225 si = devt_stash + stashed++; 226 } 227 bzero(si, sizeof(*si)); 228 si->si_udev = udev; 229 si->si_bsize_phys = DEV_BSIZE; 230 si->si_bsize_best = BLKDEV_IOSIZE; 231 si->si_bsize_max = MAXBSIZE; 232 if (y > 256) 233 sprintf(si->si_name, "#%d/0x%x", x, y); 234 else 235 sprintf(si->si_name, "#%d/%d", x, y); 236 SLIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 237 return (si); 238} 239 240udev_t 241dev2udev(dev_t x) 242{ 243 if (x == NODEV) 244 return NOUDEV; 245 return (x->si_udev); 246} 247 248udev_t 249dev2budev(dev_t x) 250{ 251 if (x == NODEV) 252 return NOUDEV; 253 else 254 return makeudev(devsw(x)->d_bmaj, minor(x)); 255} 256 257dev_t 258udev2dev(udev_t x, int b) 259{ 260 switch (b) { 261 case 0: 262 return makedev(umajor(x), uminor(x)); 263 case 1: 264 return makebdev(umajor(x), uminor(x)); 265 default: 266 Debugger("udev2dev(...,X)"); 267 return NODEV; 268 } 269} 270 271int 272uminor(udev_t dev) 273{ 274 return(dev & 0xffff00ff); 275} 276 277int 278umajor(udev_t dev) 279{ 280 return((dev & 0xff00) >> 8); 281} 282 283udev_t 284makeudev(int x, int y) 285{ 286 return ((x << 8) | y); 287} 288 289dev_t 290make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...) 291{ 292 dev_t dev; 293 va_list ap; 294 int i; 295 296 dev = makedev(devsw->d_maj, minor); 297 va_start(ap, fmt); 298 i = kvprintf(fmt, NULL, dev->si_name, 32, ap); 299 dev->si_name[i] = '\0'; 300 va_end(ap); 301 dev->si_devsw = devsw; 302 return (dev); 303} 304 305