1/*- 2 * Copyright (c) 1999-2002 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h>
| 1/*- 2 * Copyright (c) 1999-2002 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h>
|
28__FBSDID("$FreeBSD: head/sys/kern/kern_conf.c 130585 2004-06-16 09:47:26Z phk $");
| 28__FBSDID("$FreeBSD: head/sys/kern/kern_conf.c 130640 2004-06-17 17:16:53Z phk $");
|
29 30#include <sys/param.h> 31#include <sys/kernel.h> 32#include <sys/systm.h> 33#include <sys/bio.h> 34#include <sys/lock.h> 35#include <sys/mutex.h> 36#include <sys/sysctl.h> 37#include <sys/module.h> 38#include <sys/malloc.h> 39#include <sys/conf.h> 40#include <sys/vnode.h> 41#include <sys/queue.h> 42#include <sys/poll.h> 43#include <sys/ctype.h> 44#include <sys/tty.h> 45#include <machine/stdarg.h> 46 47static MALLOC_DEFINE(M_DEVT, "struct cdev *", "struct cdev *storage"); 48 49/* Built at compile time from sys/conf/majors */ 50extern unsigned char reserved_majors[256]; 51 52/* 53 * This is the number of hash-buckets. Experiements with 'real-life'
| 29 30#include <sys/param.h> 31#include <sys/kernel.h> 32#include <sys/systm.h> 33#include <sys/bio.h> 34#include <sys/lock.h> 35#include <sys/mutex.h> 36#include <sys/sysctl.h> 37#include <sys/module.h> 38#include <sys/malloc.h> 39#include <sys/conf.h> 40#include <sys/vnode.h> 41#include <sys/queue.h> 42#include <sys/poll.h> 43#include <sys/ctype.h> 44#include <sys/tty.h> 45#include <machine/stdarg.h> 46 47static MALLOC_DEFINE(M_DEVT, "struct cdev *", "struct cdev *storage"); 48 49/* Built at compile time from sys/conf/majors */ 50extern unsigned char reserved_majors[256]; 51 52/* 53 * This is the number of hash-buckets. Experiements with 'real-life'
|
54 * udev_t's show that a prime halfway between two powers of two works
| 54 * dev_t's show that a prime halfway between two powers of two works
|
55 * best. 56 */ 57#define DEVT_HASH 83 58 59/* The number of struct cdev *'s we can create before malloc(9) kick in. */ 60#define DEVT_STASH 50 61 62static struct cdev devt_stash[DEVT_STASH]; 63 64static LIST_HEAD(, cdev) dev_hash[DEVT_HASH]; 65 66static LIST_HEAD(, cdev) dev_free; 67 68static int free_devt; 69SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, ""); 70 71static struct mtx devmtx; 72static void freedev(struct cdev *dev);
| 55 * best. 56 */ 57#define DEVT_HASH 83 58 59/* The number of struct cdev *'s we can create before malloc(9) kick in. */ 60#define DEVT_STASH 50 61 62static struct cdev devt_stash[DEVT_STASH]; 63 64static LIST_HEAD(, cdev) dev_hash[DEVT_HASH]; 65 66static LIST_HEAD(, cdev) dev_free; 67 68static int free_devt; 69SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, ""); 70 71static struct mtx devmtx; 72static void freedev(struct cdev *dev);
|
| 73static struct cdev *newdev(int x, int y);
|
73
| 74
|
| 75
|
74static void 75devlock(void) 76{ 77 if (!mtx_initialized(&devmtx)) 78 mtx_init(&devmtx, "struct cdev *", NULL, MTX_DEF); 79 mtx_lock(&devmtx); 80} 81 82static void 83devunlock(void) 84{ 85 mtx_unlock(&devmtx); 86} 87 88void 89dev_ref(struct cdev *dev) 90{ 91 devlock(); 92 dev->si_refcount++; 93 devunlock(); 94} 95 96void 97dev_rel(struct cdev *dev) 98{ 99 devlock(); 100 dev->si_refcount--; 101 KASSERT(dev->si_refcount >= 0, 102 ("dev_rel(%s) gave negative count", devtoname(dev))); 103 if (dev->si_devsw == NULL && dev->si_refcount == 0) { 104 LIST_REMOVE(dev, si_list); 105 freedev(dev); 106 } 107 devunlock(); 108} 109 110void 111cdevsw_ref(struct cdevsw *csw) 112{ 113 devlock(); 114 csw->d_refcount++; 115 devunlock(); 116} 117 118void 119cdevsw_rel(struct cdevsw *csw) 120{ 121 devlock(); 122 csw->d_refcount--; 123 KASSERT(csw->d_refcount >= 0, 124 ("cdevsw_vrel(%s) gave negative count", csw->d_name)); 125 devunlock(); 126} 127
| 76static void 77devlock(void) 78{ 79 if (!mtx_initialized(&devmtx)) 80 mtx_init(&devmtx, "struct cdev *", NULL, MTX_DEF); 81 mtx_lock(&devmtx); 82} 83 84static void 85devunlock(void) 86{ 87 mtx_unlock(&devmtx); 88} 89 90void 91dev_ref(struct cdev *dev) 92{ 93 devlock(); 94 dev->si_refcount++; 95 devunlock(); 96} 97 98void 99dev_rel(struct cdev *dev) 100{ 101 devlock(); 102 dev->si_refcount--; 103 KASSERT(dev->si_refcount >= 0, 104 ("dev_rel(%s) gave negative count", devtoname(dev))); 105 if (dev->si_devsw == NULL && dev->si_refcount == 0) { 106 LIST_REMOVE(dev, si_list); 107 freedev(dev); 108 } 109 devunlock(); 110} 111 112void 113cdevsw_ref(struct cdevsw *csw) 114{ 115 devlock(); 116 csw->d_refcount++; 117 devunlock(); 118} 119 120void 121cdevsw_rel(struct cdevsw *csw) 122{ 123 devlock(); 124 csw->d_refcount--; 125 KASSERT(csw->d_refcount >= 0, 126 ("cdevsw_vrel(%s) gave negative count", csw->d_name)); 127 devunlock(); 128} 129
|
128static struct cdev *makedev(int x, int y); 129
| |
130int 131nullop(void) 132{ 133 134 return (0); 135} 136 137int 138eopnotsupp(void) 139{ 140 141 return (EOPNOTSUPP); 142} 143 144static int 145enxio(void) 146{ 147 return (ENXIO); 148} 149 150static int 151enodev(void) 152{ 153 return (ENODEV); 154} 155 156/* Define a dead_cdevsw for use when devices leave unexpectedly. */ 157 158#define dead_open (d_open_t *)enxio 159#define dead_close (d_close_t *)enxio 160#define dead_read (d_read_t *)enxio 161#define dead_write (d_write_t *)enxio 162#define dead_ioctl (d_ioctl_t *)enxio 163#define dead_poll (d_poll_t *)enodev 164#define dead_mmap (d_mmap_t *)enodev 165 166static void 167dead_strategy(struct bio *bp) 168{ 169 170 biofinish(bp, NULL, ENXIO); 171} 172 173#define dead_dump (dumper_t *)enxio 174#define dead_kqfilter (d_kqfilter_t *)enxio 175 176static struct cdevsw dead_cdevsw = { 177 .d_version = D_VERSION, 178 .d_flags = D_NEEDGIANT, /* XXX: does dead_strategy need this ? */ 179 .d_open = dead_open, 180 .d_close = dead_close, 181 .d_read = dead_read, 182 .d_write = dead_write, 183 .d_ioctl = dead_ioctl, 184 .d_poll = dead_poll, 185 .d_mmap = dead_mmap, 186 .d_strategy = dead_strategy, 187 .d_name = "dead", 188 .d_maj = 255, 189 .d_dump = dead_dump, 190 .d_kqfilter = dead_kqfilter 191}; 192 193/* Default methods if driver does not specify method */ 194 195#define null_open (d_open_t *)nullop 196#define null_close (d_close_t *)nullop 197#define no_read (d_read_t *)enodev 198#define no_write (d_write_t *)enodev 199#define no_ioctl (d_ioctl_t *)enodev 200#define no_mmap (d_mmap_t *)enodev 201 202static int 203no_kqfilter(struct cdev *dev __unused, struct knote *kn __unused) 204{ 205 206 return (1); 207} 208 209static void 210no_strategy(struct bio *bp) 211{ 212 213 biofinish(bp, NULL, ENODEV); 214} 215 216static int 217no_poll(struct cdev *dev __unused, int events, struct thread *td __unused) 218{ 219 /* 220 * Return true for read/write. If the user asked for something 221 * special, return POLLNVAL, so that clients have a way of 222 * determining reliably whether or not the extended 223 * functionality is present without hard-coding knowledge 224 * of specific filesystem implementations. 225 * Stay in sync with vop_nopoll(). 226 */ 227 if (events & ~POLLSTANDARD) 228 return (POLLNVAL); 229 230 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 231} 232 233#define no_dump (dumper_t *)enodev 234 235struct cdevsw * 236devsw(struct cdev *dev) 237{ 238 if (dev->si_devsw != NULL) 239 return (dev->si_devsw); 240 return (&dead_cdevsw); 241} 242 243/* 244 * struct cdev *and u_dev_t primitives 245 */ 246 247int 248major(struct cdev *x) 249{
| 130int 131nullop(void) 132{ 133 134 return (0); 135} 136 137int 138eopnotsupp(void) 139{ 140 141 return (EOPNOTSUPP); 142} 143 144static int 145enxio(void) 146{ 147 return (ENXIO); 148} 149 150static int 151enodev(void) 152{ 153 return (ENODEV); 154} 155 156/* Define a dead_cdevsw for use when devices leave unexpectedly. */ 157 158#define dead_open (d_open_t *)enxio 159#define dead_close (d_close_t *)enxio 160#define dead_read (d_read_t *)enxio 161#define dead_write (d_write_t *)enxio 162#define dead_ioctl (d_ioctl_t *)enxio 163#define dead_poll (d_poll_t *)enodev 164#define dead_mmap (d_mmap_t *)enodev 165 166static void 167dead_strategy(struct bio *bp) 168{ 169 170 biofinish(bp, NULL, ENXIO); 171} 172 173#define dead_dump (dumper_t *)enxio 174#define dead_kqfilter (d_kqfilter_t *)enxio 175 176static struct cdevsw dead_cdevsw = { 177 .d_version = D_VERSION, 178 .d_flags = D_NEEDGIANT, /* XXX: does dead_strategy need this ? */ 179 .d_open = dead_open, 180 .d_close = dead_close, 181 .d_read = dead_read, 182 .d_write = dead_write, 183 .d_ioctl = dead_ioctl, 184 .d_poll = dead_poll, 185 .d_mmap = dead_mmap, 186 .d_strategy = dead_strategy, 187 .d_name = "dead", 188 .d_maj = 255, 189 .d_dump = dead_dump, 190 .d_kqfilter = dead_kqfilter 191}; 192 193/* Default methods if driver does not specify method */ 194 195#define null_open (d_open_t *)nullop 196#define null_close (d_close_t *)nullop 197#define no_read (d_read_t *)enodev 198#define no_write (d_write_t *)enodev 199#define no_ioctl (d_ioctl_t *)enodev 200#define no_mmap (d_mmap_t *)enodev 201 202static int 203no_kqfilter(struct cdev *dev __unused, struct knote *kn __unused) 204{ 205 206 return (1); 207} 208 209static void 210no_strategy(struct bio *bp) 211{ 212 213 biofinish(bp, NULL, ENODEV); 214} 215 216static int 217no_poll(struct cdev *dev __unused, int events, struct thread *td __unused) 218{ 219 /* 220 * Return true for read/write. If the user asked for something 221 * special, return POLLNVAL, so that clients have a way of 222 * determining reliably whether or not the extended 223 * functionality is present without hard-coding knowledge 224 * of specific filesystem implementations. 225 * Stay in sync with vop_nopoll(). 226 */ 227 if (events & ~POLLSTANDARD) 228 return (POLLNVAL); 229 230 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 231} 232 233#define no_dump (dumper_t *)enodev 234 235struct cdevsw * 236devsw(struct cdev *dev) 237{ 238 if (dev->si_devsw != NULL) 239 return (dev->si_devsw); 240 return (&dead_cdevsw); 241} 242 243/* 244 * struct cdev *and u_dev_t primitives 245 */ 246 247int 248major(struct cdev *x) 249{
|
250 if (x == NODEV) 251 return NOUDEV;
| 250 if (x == NULL) 251 return NODEV;
|
252 return((x->si_udev >> 8) & 0xff); 253} 254 255int 256minor(struct cdev *x) 257{
| 252 return((x->si_udev >> 8) & 0xff); 253} 254 255int 256minor(struct cdev *x) 257{
|
258 if (x == NODEV) 259 return NOUDEV;
| 258 if (x == NULL) 259 return NODEV;
|
260 return(x->si_udev & 0xffff00ff); 261} 262 263int 264dev2unit(struct cdev *x) 265{ 266 int i; 267
| 260 return(x->si_udev & 0xffff00ff); 261} 262 263int 264dev2unit(struct cdev *x) 265{ 266 int i; 267
|
268 if (x == NODEV) 269 return NOUDEV;
| 268 if (x == NULL) 269 return NODEV;
|
270 i = minor(x); 271 return ((i & 0xff) | (i >> 8)); 272} 273 274int 275unit2minor(int unit) 276{ 277 278 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit)); 279 return ((unit & 0xff) | ((unit << 8) & ~0xffff)); 280} 281 282static struct cdev * 283allocdev(void) 284{ 285 static int stashed; 286 struct cdev *si; 287 288 if (LIST_FIRST(&dev_free)) { 289 si = LIST_FIRST(&dev_free); 290 LIST_REMOVE(si, si_hash); 291 } else if (stashed >= DEVT_STASH) { 292 MALLOC(si, struct cdev *, sizeof(*si), M_DEVT, 293 M_USE_RESERVE | M_ZERO | M_WAITOK); 294 } else { 295 si = devt_stash + stashed++; 296 bzero(si, sizeof *si); 297 si->si_flags |= SI_STASHED; 298 } 299 si->__si_namebuf[0] = '\0'; 300 si->si_name = si->__si_namebuf; 301 LIST_INIT(&si->si_children); 302 TAILQ_INIT(&si->si_snapshots); 303 return (si); 304} 305 306static struct cdev *
| 270 i = minor(x); 271 return ((i & 0xff) | (i >> 8)); 272} 273 274int 275unit2minor(int unit) 276{ 277 278 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit)); 279 return ((unit & 0xff) | ((unit << 8) & ~0xffff)); 280} 281 282static struct cdev * 283allocdev(void) 284{ 285 static int stashed; 286 struct cdev *si; 287 288 if (LIST_FIRST(&dev_free)) { 289 si = LIST_FIRST(&dev_free); 290 LIST_REMOVE(si, si_hash); 291 } else if (stashed >= DEVT_STASH) { 292 MALLOC(si, struct cdev *, sizeof(*si), M_DEVT, 293 M_USE_RESERVE | M_ZERO | M_WAITOK); 294 } else { 295 si = devt_stash + stashed++; 296 bzero(si, sizeof *si); 297 si->si_flags |= SI_STASHED; 298 } 299 si->__si_namebuf[0] = '\0'; 300 si->si_name = si->__si_namebuf; 301 LIST_INIT(&si->si_children); 302 TAILQ_INIT(&si->si_snapshots); 303 return (si); 304} 305 306static struct cdev *
|
307makedev(int x, int y)
| 307newdev(int x, int y)
|
308{ 309 struct cdev *si;
| 308{ 309 struct cdev *si;
|
310 udev_t udev;
| 310 dev_t udev;
|
311 int hash; 312
| 311 int hash; 312
|
313 if (x == umajor(NOUDEV) && y == uminor(NOUDEV)) 314 panic("makedev of NOUDEV");
| 313 if (x == umajor(NODEV) && y == uminor(NODEV)) 314 panic("newdev of NODEV");
|
315 udev = (x << 8) | y; 316 hash = udev % DEVT_HASH; 317 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 318 if (si->si_udev == udev) 319 return (si); 320 } 321 si = allocdev(); 322 si->si_udev = udev; 323 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 324 return (si); 325} 326 327static void 328freedev(struct cdev *dev) 329{ 330 331 if (dev->si_flags & SI_STASHED) { 332 bzero(dev, sizeof(*dev)); 333 dev->si_flags |= SI_STASHED; 334 LIST_INSERT_HEAD(&dev_free, dev, si_hash); 335 } else { 336 FREE(dev, M_DEVT); 337 } 338} 339
| 315 udev = (x << 8) | y; 316 hash = udev % DEVT_HASH; 317 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 318 if (si->si_udev == udev) 319 return (si); 320 } 321 si = allocdev(); 322 si->si_udev = udev; 323 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 324 return (si); 325} 326 327static void 328freedev(struct cdev *dev) 329{ 330 331 if (dev->si_flags & SI_STASHED) { 332 bzero(dev, sizeof(*dev)); 333 dev->si_flags |= SI_STASHED; 334 LIST_INSERT_HEAD(&dev_free, dev, si_hash); 335 } else { 336 FREE(dev, M_DEVT); 337 } 338} 339
|
340udev_t
| 340dev_t
|
341dev2udev(struct cdev *x) 342{
| 341dev2udev(struct cdev *x) 342{
|
343 if (x == NODEV) 344 return (NOUDEV);
| 343 if (x == NULL) 344 return (NODEV);
|
345 return (x->si_udev); 346} 347 348struct cdev *
| 345 return (x->si_udev); 346} 347 348struct cdev *
|
349udev2dev(udev_t udev)
| 349findcdev(dev_t udev)
|
350{ 351 struct cdev *si; 352 int hash; 353
| 350{ 351 struct cdev *si; 352 int hash; 353
|
354 if (udev == NOUDEV) 355 return (NODEV);
| 354 if (udev == NODEV) 355 return (NULL);
|
356 hash = udev % DEVT_HASH; 357 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 358 if (si->si_udev == udev) 359 return (si); 360 }
| 356 hash = udev % DEVT_HASH; 357 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 358 if (si->si_udev == udev) 359 return (si); 360 }
|
361 return (NODEV);
| 361 return (NULL);
|
362} 363 364int
| 362} 363 364int
|
365uminor(udev_t dev)
| 365uminor(dev_t dev)
|
366{ 367 return (dev & 0xffff00ff); 368} 369 370int
| 366{ 367 return (dev & 0xffff00ff); 368} 369 370int
|
371umajor(udev_t dev)
| 371umajor(dev_t dev)
|
372{ 373 return ((dev & 0xff00) >> 8); 374} 375
| 372{ 373 return ((dev & 0xff00) >> 8); 374} 375
|
376udev_t 377makeudev(int x, int y) 378{ 379 return ((x << 8) | y); 380} 381
| |
382static void 383find_major(struct cdevsw *devsw) 384{ 385 int i; 386 387 for (i = NUMCDEVSW - 1; i > 0; i--) 388 if (reserved_majors[i] != i) 389 break; 390 KASSERT(i > 0, ("Out of major numbers (%s)", devsw->d_name)); 391 devsw->d_maj = i; 392 reserved_majors[i] = i; 393 devsw->d_flags |= D_ALLOCMAJ; 394} 395 396static void 397fini_cdevsw(struct cdevsw *devsw) 398{ 399 if (devsw->d_flags & D_ALLOCMAJ) { 400 reserved_majors[devsw->d_maj] = 0; 401 devsw->d_maj = MAJOR_AUTO; 402 devsw->d_flags &= ~D_ALLOCMAJ; 403 } else if (devsw->d_maj == 0) 404 devsw->d_maj = 256; 405 devsw->d_flags &= ~D_INIT; 406} 407 408static void 409prep_cdevsw(struct cdevsw *devsw) 410{ 411 412 devlock(); 413 414 if (devsw->d_version != D_VERSION_00) { 415 printf( 416 "WARNING: Device driver \"%s\" has wrong version %s\n", 417 devsw->d_name, "and is disabled. Recompile KLD module."); 418 devsw->d_open = dead_open; 419 devsw->d_close = dead_close; 420 devsw->d_read = dead_read; 421 devsw->d_write = dead_write; 422 devsw->d_ioctl = dead_ioctl; 423 devsw->d_poll = dead_poll; 424 devsw->d_mmap = dead_mmap; 425 devsw->d_strategy = dead_strategy; 426 devsw->d_dump = dead_dump; 427 devsw->d_kqfilter = dead_kqfilter; 428 } 429 430 if (devsw->d_flags & D_TTY) { 431 if (devsw->d_ioctl == NULL) devsw->d_ioctl = ttyioctl; 432 if (devsw->d_read == NULL) devsw->d_read = ttyread; 433 if (devsw->d_write == NULL) devsw->d_write = ttywrite; 434 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = ttykqfilter; 435 if (devsw->d_poll == NULL) devsw->d_poll = ttypoll; 436 } 437 438 if (devsw->d_open == NULL) devsw->d_open = null_open; 439 if (devsw->d_close == NULL) devsw->d_close = null_close; 440 if (devsw->d_read == NULL) devsw->d_read = no_read; 441 if (devsw->d_write == NULL) devsw->d_write = no_write; 442 if (devsw->d_ioctl == NULL) devsw->d_ioctl = no_ioctl; 443 if (devsw->d_poll == NULL) devsw->d_poll = no_poll; 444 if (devsw->d_mmap == NULL) devsw->d_mmap = no_mmap; 445 if (devsw->d_strategy == NULL) devsw->d_strategy = no_strategy; 446 if (devsw->d_dump == NULL) devsw->d_dump = no_dump; 447 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = no_kqfilter; 448 449 LIST_INIT(&devsw->d_devs); 450 451 devsw->d_flags |= D_INIT; 452 453 if (devsw->d_maj == MAJOR_AUTO) { 454 find_major(devsw); 455 } else { 456 if (devsw->d_maj == 256) /* XXX: tty_cons.c is magic */ 457 devsw->d_maj = 0; 458 KASSERT(devsw->d_maj >= 0 && devsw->d_maj < 256, 459 ("Invalid major (%d) in make_dev", devsw->d_maj)); 460 if (reserved_majors[devsw->d_maj] != devsw->d_maj) { 461 printf("WARNING: driver \"%s\" used %s %d\n", 462 devsw->d_name, "unreserved major device number", 463 devsw->d_maj); 464 reserved_majors[devsw->d_maj] = devsw->d_maj; 465 } 466 } 467 devunlock(); 468} 469 470struct cdev * 471make_dev(struct cdevsw *devsw, int minornr, uid_t uid, gid_t gid, int perms, const char *fmt, ...) 472{ 473 struct cdev *dev; 474 va_list ap; 475 int i; 476 477 KASSERT((minornr & ~0xffff00ff) == 0, 478 ("Invalid minor (0x%x) in make_dev", minornr)); 479 480 if (!(devsw->d_flags & D_INIT)) 481 prep_cdevsw(devsw);
| 376static void 377find_major(struct cdevsw *devsw) 378{ 379 int i; 380 381 for (i = NUMCDEVSW - 1; i > 0; i--) 382 if (reserved_majors[i] != i) 383 break; 384 KASSERT(i > 0, ("Out of major numbers (%s)", devsw->d_name)); 385 devsw->d_maj = i; 386 reserved_majors[i] = i; 387 devsw->d_flags |= D_ALLOCMAJ; 388} 389 390static void 391fini_cdevsw(struct cdevsw *devsw) 392{ 393 if (devsw->d_flags & D_ALLOCMAJ) { 394 reserved_majors[devsw->d_maj] = 0; 395 devsw->d_maj = MAJOR_AUTO; 396 devsw->d_flags &= ~D_ALLOCMAJ; 397 } else if (devsw->d_maj == 0) 398 devsw->d_maj = 256; 399 devsw->d_flags &= ~D_INIT; 400} 401 402static void 403prep_cdevsw(struct cdevsw *devsw) 404{ 405 406 devlock(); 407 408 if (devsw->d_version != D_VERSION_00) { 409 printf( 410 "WARNING: Device driver \"%s\" has wrong version %s\n", 411 devsw->d_name, "and is disabled. Recompile KLD module."); 412 devsw->d_open = dead_open; 413 devsw->d_close = dead_close; 414 devsw->d_read = dead_read; 415 devsw->d_write = dead_write; 416 devsw->d_ioctl = dead_ioctl; 417 devsw->d_poll = dead_poll; 418 devsw->d_mmap = dead_mmap; 419 devsw->d_strategy = dead_strategy; 420 devsw->d_dump = dead_dump; 421 devsw->d_kqfilter = dead_kqfilter; 422 } 423 424 if (devsw->d_flags & D_TTY) { 425 if (devsw->d_ioctl == NULL) devsw->d_ioctl = ttyioctl; 426 if (devsw->d_read == NULL) devsw->d_read = ttyread; 427 if (devsw->d_write == NULL) devsw->d_write = ttywrite; 428 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = ttykqfilter; 429 if (devsw->d_poll == NULL) devsw->d_poll = ttypoll; 430 } 431 432 if (devsw->d_open == NULL) devsw->d_open = null_open; 433 if (devsw->d_close == NULL) devsw->d_close = null_close; 434 if (devsw->d_read == NULL) devsw->d_read = no_read; 435 if (devsw->d_write == NULL) devsw->d_write = no_write; 436 if (devsw->d_ioctl == NULL) devsw->d_ioctl = no_ioctl; 437 if (devsw->d_poll == NULL) devsw->d_poll = no_poll; 438 if (devsw->d_mmap == NULL) devsw->d_mmap = no_mmap; 439 if (devsw->d_strategy == NULL) devsw->d_strategy = no_strategy; 440 if (devsw->d_dump == NULL) devsw->d_dump = no_dump; 441 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = no_kqfilter; 442 443 LIST_INIT(&devsw->d_devs); 444 445 devsw->d_flags |= D_INIT; 446 447 if (devsw->d_maj == MAJOR_AUTO) { 448 find_major(devsw); 449 } else { 450 if (devsw->d_maj == 256) /* XXX: tty_cons.c is magic */ 451 devsw->d_maj = 0; 452 KASSERT(devsw->d_maj >= 0 && devsw->d_maj < 256, 453 ("Invalid major (%d) in make_dev", devsw->d_maj)); 454 if (reserved_majors[devsw->d_maj] != devsw->d_maj) { 455 printf("WARNING: driver \"%s\" used %s %d\n", 456 devsw->d_name, "unreserved major device number", 457 devsw->d_maj); 458 reserved_majors[devsw->d_maj] = devsw->d_maj; 459 } 460 } 461 devunlock(); 462} 463 464struct cdev * 465make_dev(struct cdevsw *devsw, int minornr, uid_t uid, gid_t gid, int perms, const char *fmt, ...) 466{ 467 struct cdev *dev; 468 va_list ap; 469 int i; 470 471 KASSERT((minornr & ~0xffff00ff) == 0, 472 ("Invalid minor (0x%x) in make_dev", minornr)); 473 474 if (!(devsw->d_flags & D_INIT)) 475 prep_cdevsw(devsw);
|
482 dev = makedev(devsw->d_maj, minornr);
| 476 dev = newdev(devsw->d_maj, minornr);
|
483 if (dev->si_flags & SI_CHEAPCLONE && 484 dev->si_flags & SI_NAMED && 485 dev->si_devsw == devsw) { 486 /* 487 * This is allowed as it removes races and generally 488 * simplifies cloning devices. 489 * XXX: still ?? 490 */ 491 return (dev); 492 } 493 devlock(); 494 KASSERT(!(dev->si_flags & SI_NAMED), 495 ("make_dev() by driver %s on pre-existing device (maj=%d, min=%d, name=%s)", 496 devsw->d_name, major(dev), minor(dev), devtoname(dev))); 497 498 va_start(ap, fmt); 499 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 500 if (i > (sizeof dev->__si_namebuf - 1)) { 501 printf("WARNING: Device name truncated! (%s)", 502 dev->__si_namebuf); 503 } 504 va_end(ap); 505 dev->si_devsw = devsw; 506 dev->si_uid = uid; 507 dev->si_gid = gid; 508 dev->si_mode = perms; 509 dev->si_flags |= SI_NAMED; 510 511 LIST_INSERT_HEAD(&devsw->d_devs, dev, si_list); 512 devfs_create(dev); 513 devunlock(); 514 return (dev); 515} 516 517int 518dev_named(struct cdev *pdev, const char *name) 519{ 520 struct cdev *cdev; 521 522 if (strcmp(devtoname(pdev), name) == 0) 523 return (1); 524 LIST_FOREACH(cdev, &pdev->si_children, si_siblings) 525 if (strcmp(devtoname(cdev), name) == 0) 526 return (1); 527 return (0); 528} 529 530void 531dev_depends(struct cdev *pdev, struct cdev *cdev) 532{ 533 534 devlock(); 535 cdev->si_parent = pdev; 536 cdev->si_flags |= SI_CHILD; 537 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 538 devunlock(); 539} 540 541struct cdev * 542make_dev_alias(struct cdev *pdev, const char *fmt, ...) 543{ 544 struct cdev *dev; 545 va_list ap; 546 int i; 547 548 dev = allocdev(); 549 devlock(); 550 dev->si_flags |= SI_ALIAS; 551 dev->si_flags |= SI_NAMED; 552 va_start(ap, fmt); 553 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 554 if (i > (sizeof dev->__si_namebuf - 1)) { 555 printf("WARNING: Device name truncated! (%s)", 556 dev->__si_namebuf); 557 } 558 va_end(ap); 559 560 devfs_create(dev); 561 devunlock(); 562 dev_depends(pdev, dev); 563 return (dev); 564} 565 566static void 567idestroy_dev(struct cdev *dev) 568{ 569 if (!(dev->si_flags & SI_NAMED)) { 570 printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n", 571 major(dev), minor(dev)); 572 panic("don't do that"); 573 } 574 575 devfs_destroy(dev); 576 577 /* Remove name marking */ 578 dev->si_flags &= ~SI_NAMED; 579 580 /* If we are a child, remove us from the parents list */ 581 if (dev->si_flags & SI_CHILD) { 582 LIST_REMOVE(dev, si_siblings); 583 dev->si_flags &= ~SI_CHILD; 584 } 585 586 /* Kill our children */ 587 while (!LIST_EMPTY(&dev->si_children)) 588 idestroy_dev(LIST_FIRST(&dev->si_children)); 589 590 /* Remove from clone list */ 591 if (dev->si_flags & SI_CLONELIST) { 592 LIST_REMOVE(dev, si_clone); 593 dev->si_flags &= ~SI_CLONELIST; 594 } 595 596 if (!(dev->si_flags & SI_ALIAS)) { 597 /* Remove from cdevsw list */ 598 LIST_REMOVE(dev, si_list); 599 600 /* If cdevsw has no struct cdev *'s, clean it */ 601 if (LIST_EMPTY(&dev->si_devsw->d_devs)) 602 fini_cdevsw(dev->si_devsw); 603 604 LIST_REMOVE(dev, si_hash); 605 } 606 dev->si_drv1 = 0; 607 dev->si_drv2 = 0; 608 dev->si_devsw = NULL; 609 bzero(&dev->__si_u, sizeof(dev->__si_u)); 610 dev->si_flags &= ~SI_ALIAS; 611 if (dev->si_refcount > 0) { 612 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list); 613 } else { 614 freedev(dev); 615 } 616} 617 618void 619destroy_dev(struct cdev *dev) 620{ 621 622 devlock(); 623 idestroy_dev(dev); 624 devunlock(); 625} 626 627const char * 628devtoname(struct cdev *dev) 629{ 630 char *p; 631 int mynor; 632 633 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { 634 p = dev->si_name; 635 if (devsw(dev)) 636 sprintf(p, "#%s/", devsw(dev)->d_name); 637 else 638 sprintf(p, "#%d/", major(dev)); 639 p += strlen(p); 640 mynor = minor(dev); 641 if (mynor < 0 || mynor > 255) 642 sprintf(p, "%#x", (u_int)mynor); 643 else 644 sprintf(p, "%d", mynor); 645 } 646 return (dev->si_name); 647} 648 649int 650dev_stdclone(char *name, char **namep, const char *stem, int *unit) 651{ 652 int u, i; 653 654 i = strlen(stem); 655 if (bcmp(stem, name, i) != 0) 656 return (0); 657 if (!isdigit(name[i])) 658 return (0); 659 u = 0; 660 if (name[i] == '0' && isdigit(name[i+1])) 661 return (0); 662 while (isdigit(name[i])) { 663 u *= 10; 664 u += name[i++] - '0'; 665 } 666 if (u > 0xffffff) 667 return (0); 668 *unit = u; 669 if (namep) 670 *namep = &name[i]; 671 if (name[i]) 672 return (2); 673 return (1); 674} 675 676/* 677 * Helper functions for cloning device drivers. 678 * 679 * The objective here is to make it unnecessary for the device drivers to 680 * use rman or similar to manage their unit number space. Due to the way 681 * we do "on-demand" devices, using rman or other "private" methods 682 * will be very tricky to lock down properly once we lock down this file. 683 * 684 * Instead we give the drivers these routines which puts the struct cdev *'s that 685 * are to be managed on their own list, and gives the driver the ability 686 * to ask for the first free unit number or a given specified unit number. 687 * 688 * In addition these routines support paired devices (pty, nmdm and similar) 689 * by respecting a number of "flag" bits in the minor number. 690 * 691 */ 692 693struct clonedevs { 694 LIST_HEAD(,cdev) head; 695}; 696 697void 698clone_setup(struct clonedevs **cdp) 699{ 700 701 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO); 702 LIST_INIT(&(*cdp)->head); 703} 704 705int 706clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev **dp, u_int extra) 707{ 708 struct clonedevs *cd; 709 struct cdev *dev, *dl, *de; 710 int unit, low, u; 711 712 KASSERT(*cdp != NULL, 713 ("clone_setup() not called in driver \"%s\"", csw->d_name)); 714 KASSERT(!(extra & CLONE_UNITMASK), 715 ("Illegal extra bits (0x%x) in clone_create", extra)); 716 KASSERT(*up <= CLONE_UNITMASK, 717 ("Too high unit (0x%x) in clone_create", *up)); 718 719 if (csw->d_maj == MAJOR_AUTO) 720 find_major(csw); 721 722 /* 723 * Search the list for a lot of things in one go: 724 * A preexisting match is returned immediately. 725 * The lowest free unit number if we are passed -1, and the place 726 * in the list where we should insert that new element. 727 * The place to insert a specified unit number, if applicable 728 * the end of the list. 729 */ 730 unit = *up; 731 low = extra; 732 de = dl = NULL; 733 cd = *cdp; 734 LIST_FOREACH(dev, &cd->head, si_clone) { 735 u = dev2unit(dev); 736 if (u == (unit | extra)) { 737 *dp = dev; 738 return (0); 739 } 740 if (unit == -1 && u == low) { 741 low++; 742 de = dev; 743 continue; 744 } 745 if (u > (unit | extra)) { 746 dl = dev; 747 break; 748 } 749 de = dev; 750 } 751 if (unit == -1) 752 unit = low & CLONE_UNITMASK;
| 477 if (dev->si_flags & SI_CHEAPCLONE && 478 dev->si_flags & SI_NAMED && 479 dev->si_devsw == devsw) { 480 /* 481 * This is allowed as it removes races and generally 482 * simplifies cloning devices. 483 * XXX: still ?? 484 */ 485 return (dev); 486 } 487 devlock(); 488 KASSERT(!(dev->si_flags & SI_NAMED), 489 ("make_dev() by driver %s on pre-existing device (maj=%d, min=%d, name=%s)", 490 devsw->d_name, major(dev), minor(dev), devtoname(dev))); 491 492 va_start(ap, fmt); 493 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 494 if (i > (sizeof dev->__si_namebuf - 1)) { 495 printf("WARNING: Device name truncated! (%s)", 496 dev->__si_namebuf); 497 } 498 va_end(ap); 499 dev->si_devsw = devsw; 500 dev->si_uid = uid; 501 dev->si_gid = gid; 502 dev->si_mode = perms; 503 dev->si_flags |= SI_NAMED; 504 505 LIST_INSERT_HEAD(&devsw->d_devs, dev, si_list); 506 devfs_create(dev); 507 devunlock(); 508 return (dev); 509} 510 511int 512dev_named(struct cdev *pdev, const char *name) 513{ 514 struct cdev *cdev; 515 516 if (strcmp(devtoname(pdev), name) == 0) 517 return (1); 518 LIST_FOREACH(cdev, &pdev->si_children, si_siblings) 519 if (strcmp(devtoname(cdev), name) == 0) 520 return (1); 521 return (0); 522} 523 524void 525dev_depends(struct cdev *pdev, struct cdev *cdev) 526{ 527 528 devlock(); 529 cdev->si_parent = pdev; 530 cdev->si_flags |= SI_CHILD; 531 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 532 devunlock(); 533} 534 535struct cdev * 536make_dev_alias(struct cdev *pdev, const char *fmt, ...) 537{ 538 struct cdev *dev; 539 va_list ap; 540 int i; 541 542 dev = allocdev(); 543 devlock(); 544 dev->si_flags |= SI_ALIAS; 545 dev->si_flags |= SI_NAMED; 546 va_start(ap, fmt); 547 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 548 if (i > (sizeof dev->__si_namebuf - 1)) { 549 printf("WARNING: Device name truncated! (%s)", 550 dev->__si_namebuf); 551 } 552 va_end(ap); 553 554 devfs_create(dev); 555 devunlock(); 556 dev_depends(pdev, dev); 557 return (dev); 558} 559 560static void 561idestroy_dev(struct cdev *dev) 562{ 563 if (!(dev->si_flags & SI_NAMED)) { 564 printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n", 565 major(dev), minor(dev)); 566 panic("don't do that"); 567 } 568 569 devfs_destroy(dev); 570 571 /* Remove name marking */ 572 dev->si_flags &= ~SI_NAMED; 573 574 /* If we are a child, remove us from the parents list */ 575 if (dev->si_flags & SI_CHILD) { 576 LIST_REMOVE(dev, si_siblings); 577 dev->si_flags &= ~SI_CHILD; 578 } 579 580 /* Kill our children */ 581 while (!LIST_EMPTY(&dev->si_children)) 582 idestroy_dev(LIST_FIRST(&dev->si_children)); 583 584 /* Remove from clone list */ 585 if (dev->si_flags & SI_CLONELIST) { 586 LIST_REMOVE(dev, si_clone); 587 dev->si_flags &= ~SI_CLONELIST; 588 } 589 590 if (!(dev->si_flags & SI_ALIAS)) { 591 /* Remove from cdevsw list */ 592 LIST_REMOVE(dev, si_list); 593 594 /* If cdevsw has no struct cdev *'s, clean it */ 595 if (LIST_EMPTY(&dev->si_devsw->d_devs)) 596 fini_cdevsw(dev->si_devsw); 597 598 LIST_REMOVE(dev, si_hash); 599 } 600 dev->si_drv1 = 0; 601 dev->si_drv2 = 0; 602 dev->si_devsw = NULL; 603 bzero(&dev->__si_u, sizeof(dev->__si_u)); 604 dev->si_flags &= ~SI_ALIAS; 605 if (dev->si_refcount > 0) { 606 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list); 607 } else { 608 freedev(dev); 609 } 610} 611 612void 613destroy_dev(struct cdev *dev) 614{ 615 616 devlock(); 617 idestroy_dev(dev); 618 devunlock(); 619} 620 621const char * 622devtoname(struct cdev *dev) 623{ 624 char *p; 625 int mynor; 626 627 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { 628 p = dev->si_name; 629 if (devsw(dev)) 630 sprintf(p, "#%s/", devsw(dev)->d_name); 631 else 632 sprintf(p, "#%d/", major(dev)); 633 p += strlen(p); 634 mynor = minor(dev); 635 if (mynor < 0 || mynor > 255) 636 sprintf(p, "%#x", (u_int)mynor); 637 else 638 sprintf(p, "%d", mynor); 639 } 640 return (dev->si_name); 641} 642 643int 644dev_stdclone(char *name, char **namep, const char *stem, int *unit) 645{ 646 int u, i; 647 648 i = strlen(stem); 649 if (bcmp(stem, name, i) != 0) 650 return (0); 651 if (!isdigit(name[i])) 652 return (0); 653 u = 0; 654 if (name[i] == '0' && isdigit(name[i+1])) 655 return (0); 656 while (isdigit(name[i])) { 657 u *= 10; 658 u += name[i++] - '0'; 659 } 660 if (u > 0xffffff) 661 return (0); 662 *unit = u; 663 if (namep) 664 *namep = &name[i]; 665 if (name[i]) 666 return (2); 667 return (1); 668} 669 670/* 671 * Helper functions for cloning device drivers. 672 * 673 * The objective here is to make it unnecessary for the device drivers to 674 * use rman or similar to manage their unit number space. Due to the way 675 * we do "on-demand" devices, using rman or other "private" methods 676 * will be very tricky to lock down properly once we lock down this file. 677 * 678 * Instead we give the drivers these routines which puts the struct cdev *'s that 679 * are to be managed on their own list, and gives the driver the ability 680 * to ask for the first free unit number or a given specified unit number. 681 * 682 * In addition these routines support paired devices (pty, nmdm and similar) 683 * by respecting a number of "flag" bits in the minor number. 684 * 685 */ 686 687struct clonedevs { 688 LIST_HEAD(,cdev) head; 689}; 690 691void 692clone_setup(struct clonedevs **cdp) 693{ 694 695 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO); 696 LIST_INIT(&(*cdp)->head); 697} 698 699int 700clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev **dp, u_int extra) 701{ 702 struct clonedevs *cd; 703 struct cdev *dev, *dl, *de; 704 int unit, low, u; 705 706 KASSERT(*cdp != NULL, 707 ("clone_setup() not called in driver \"%s\"", csw->d_name)); 708 KASSERT(!(extra & CLONE_UNITMASK), 709 ("Illegal extra bits (0x%x) in clone_create", extra)); 710 KASSERT(*up <= CLONE_UNITMASK, 711 ("Too high unit (0x%x) in clone_create", *up)); 712 713 if (csw->d_maj == MAJOR_AUTO) 714 find_major(csw); 715 716 /* 717 * Search the list for a lot of things in one go: 718 * A preexisting match is returned immediately. 719 * The lowest free unit number if we are passed -1, and the place 720 * in the list where we should insert that new element. 721 * The place to insert a specified unit number, if applicable 722 * the end of the list. 723 */ 724 unit = *up; 725 low = extra; 726 de = dl = NULL; 727 cd = *cdp; 728 LIST_FOREACH(dev, &cd->head, si_clone) { 729 u = dev2unit(dev); 730 if (u == (unit | extra)) { 731 *dp = dev; 732 return (0); 733 } 734 if (unit == -1 && u == low) { 735 low++; 736 de = dev; 737 continue; 738 } 739 if (u > (unit | extra)) { 740 dl = dev; 741 break; 742 } 743 de = dev; 744 } 745 if (unit == -1) 746 unit = low & CLONE_UNITMASK;
|
753 dev = makedev(csw->d_maj, unit2minor(unit | extra));
| 747 dev = newdev(csw->d_maj, unit2minor(unit | extra));
|
754 KASSERT(!(dev->si_flags & SI_CLONELIST), 755 ("Dev %p should not be on clonelist", dev)); 756 if (dl != NULL) 757 LIST_INSERT_BEFORE(dl, dev, si_clone); 758 else if (de != NULL) 759 LIST_INSERT_AFTER(de, dev, si_clone); 760 else 761 LIST_INSERT_HEAD(&cd->head, dev, si_clone); 762 dev->si_flags |= SI_CLONELIST; 763 *up = unit; 764 return (1); 765} 766 767/* 768 * Kill everything still on the list. The driver should already have 769 * disposed of any softc hung of the struct cdev *'s at this time. 770 */ 771void 772clone_cleanup(struct clonedevs **cdp) 773{ 774 struct cdev *dev, *tdev; 775 struct clonedevs *cd; 776 777 cd = *cdp; 778 if (cd == NULL) 779 return; 780 LIST_FOREACH_SAFE(dev, &cd->head, si_clone, tdev) { 781 KASSERT(dev->si_flags & SI_NAMED, 782 ("Driver has goofed in cloning underways udev %x", dev->si_udev)); 783 destroy_dev(dev); 784 } 785 free(cd, M_DEVBUF); 786 *cdp = NULL; 787} 788 789/* 790 * Helper sysctl for devname(3). We're given a {u}struct cdev *and return 791 * the name, if any, registered by the device driver. 792 */ 793static int 794sysctl_devname(SYSCTL_HANDLER_ARGS) 795{ 796 int error;
| 748 KASSERT(!(dev->si_flags & SI_CLONELIST), 749 ("Dev %p should not be on clonelist", dev)); 750 if (dl != NULL) 751 LIST_INSERT_BEFORE(dl, dev, si_clone); 752 else if (de != NULL) 753 LIST_INSERT_AFTER(de, dev, si_clone); 754 else 755 LIST_INSERT_HEAD(&cd->head, dev, si_clone); 756 dev->si_flags |= SI_CLONELIST; 757 *up = unit; 758 return (1); 759} 760 761/* 762 * Kill everything still on the list. The driver should already have 763 * disposed of any softc hung of the struct cdev *'s at this time. 764 */ 765void 766clone_cleanup(struct clonedevs **cdp) 767{ 768 struct cdev *dev, *tdev; 769 struct clonedevs *cd; 770 771 cd = *cdp; 772 if (cd == NULL) 773 return; 774 LIST_FOREACH_SAFE(dev, &cd->head, si_clone, tdev) { 775 KASSERT(dev->si_flags & SI_NAMED, 776 ("Driver has goofed in cloning underways udev %x", dev->si_udev)); 777 destroy_dev(dev); 778 } 779 free(cd, M_DEVBUF); 780 *cdp = NULL; 781} 782 783/* 784 * Helper sysctl for devname(3). We're given a {u}struct cdev *and return 785 * the name, if any, registered by the device driver. 786 */ 787static int 788sysctl_devname(SYSCTL_HANDLER_ARGS) 789{ 790 int error;
|
797 udev_t ud;
| 791 dev_t ud;
|
798 struct cdev *dev; 799 800 error = SYSCTL_IN(req, &ud, sizeof (ud)); 801 if (error) 802 return (error);
| 792 struct cdev *dev; 793 794 error = SYSCTL_IN(req, &ud, sizeof (ud)); 795 if (error) 796 return (error);
|
803 if (ud == NOUDEV)
| 797 if (ud == NODEV)
|
804 return(EINVAL);
| 798 return(EINVAL);
|
805 dev = udev2dev(ud); 806 if (dev == NODEV)
| 799 dev = findcdev(ud); 800 if (dev == NULL)
|
807 error = ENOENT; 808 else 809 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1); 810 return (error); 811} 812 813SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 814 NULL, 0, sysctl_devname, "", "devname(3) handler");
| 801 error = ENOENT; 802 else 803 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1); 804 return (error); 805} 806 807SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 808 NULL, 0, sysctl_devname, "", "devname(3) handler");
|