1 /* 2 * Copyright (c) 1997,1998 Doug Rabson 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 * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $ 27 */ 28 29 #include "opt_bus.h" 30 31 #include <sys/param.h> 32 #include <sys/queue.h> 33 #include <sys/malloc.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/kobj.h> 37 #include <sys/bus_private.h> 38 #include <sys/sysctl.h> 39 #include <sys/systm.h> 40 #include <sys/bus.h> 41 #include <sys/rman.h> 42 #include <sys/device.h> 43 #include <sys/lock.h> 44 #include <sys/conf.h> 45 #include <sys/uio.h> 46 #include <sys/filio.h> 47 #include <sys/event.h> 48 #include <sys/signalvar.h> 49 50 #include <machine/stdarg.h> /* for device_printf() */ 51 52 #include <sys/thread2.h> 53 #include <sys/mplock2.h> 54 55 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); 56 57 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 58 59 #ifdef BUS_DEBUG 60 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n")) 61 #define DEVICENAME(d) ((d)? device_get_name(d): "no device") 62 #define DRIVERNAME(d) ((d)? d->name : "no driver") 63 #define DEVCLANAME(d) ((d)? d->name : "no devclass") 64 65 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to 66 * prevent syslog from deleting initial spaces 67 */ 68 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0) 69 70 static void print_device_short(device_t dev, int indent); 71 static void print_device(device_t dev, int indent); 72 void print_device_tree_short(device_t dev, int indent); 73 void print_device_tree(device_t dev, int indent); 74 static void print_driver_short(driver_t *driver, int indent); 75 static void print_driver(driver_t *driver, int indent); 76 static void print_driver_list(driver_list_t drivers, int indent); 77 static void print_devclass_short(devclass_t dc, int indent); 78 static void print_devclass(devclass_t dc, int indent); 79 void print_devclass_list_short(void); 80 void print_devclass_list(void); 81 82 #else 83 /* Make the compiler ignore the function calls */ 84 #define PDEBUG(a) /* nop */ 85 #define DEVICENAME(d) /* nop */ 86 #define DRIVERNAME(d) /* nop */ 87 #define DEVCLANAME(d) /* nop */ 88 89 #define print_device_short(d,i) /* nop */ 90 #define print_device(d,i) /* nop */ 91 #define print_device_tree_short(d,i) /* nop */ 92 #define print_device_tree(d,i) /* nop */ 93 #define print_driver_short(d,i) /* nop */ 94 #define print_driver(d,i) /* nop */ 95 #define print_driver_list(d,i) /* nop */ 96 #define print_devclass_short(d,i) /* nop */ 97 #define print_devclass(d,i) /* nop */ 98 #define print_devclass_list_short() /* nop */ 99 #define print_devclass_list() /* nop */ 100 #endif 101 102 static void device_attach_async(device_t dev); 103 static void device_attach_thread(void *arg); 104 static int device_doattach(device_t dev); 105 106 static int do_async_attach = 0; 107 static int numasyncthreads; 108 TUNABLE_INT("kern.do_async_attach", &do_async_attach); 109 110 /* 111 * /dev/devctl implementation 112 */ 113 114 /* 115 * This design allows only one reader for /dev/devctl. This is not desirable 116 * in the long run, but will get a lot of hair out of this implementation. 117 * Maybe we should make this device a clonable device. 118 * 119 * Also note: we specifically do not attach a device to the device_t tree 120 * to avoid potential chicken and egg problems. One could argue that all 121 * of this belongs to the root node. One could also further argue that the 122 * sysctl interface that we have not might more properly be an ioctl 123 * interface, but at this stage of the game, I'm not inclined to rock that 124 * boat. 125 * 126 * I'm also not sure that the SIGIO support is done correctly or not, as 127 * I copied it from a driver that had SIGIO support that likely hasn't been 128 * tested since 3.4 or 2.2.8! 129 */ 130 131 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS); 132 static int devctl_disable = 0; 133 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable); 134 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0, 135 sysctl_devctl_disable, "I", "devctl disable"); 136 137 static d_open_t devopen; 138 static d_close_t devclose; 139 static d_read_t devread; 140 static d_ioctl_t devioctl; 141 static d_kqfilter_t devkqfilter; 142 143 static struct dev_ops devctl_ops = { 144 { "devctl", 0, 0 }, 145 .d_open = devopen, 146 .d_close = devclose, 147 .d_read = devread, 148 .d_ioctl = devioctl, 149 .d_kqfilter = devkqfilter 150 }; 151 152 struct dev_event_info 153 { 154 char *dei_data; 155 TAILQ_ENTRY(dev_event_info) dei_link; 156 }; 157 158 TAILQ_HEAD(devq, dev_event_info); 159 160 static struct dev_softc 161 { 162 int inuse; 163 int nonblock; 164 struct lock lock; 165 struct kqinfo kq; 166 struct devq devq; 167 struct proc *async_proc; 168 } devsoftc; 169 170 static void 171 devinit(void) 172 { 173 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl"); 174 lockinit(&devsoftc.lock, "dev mtx", 0, 0); 175 TAILQ_INIT(&devsoftc.devq); 176 } 177 178 static int 179 devopen(struct dev_open_args *ap) 180 { 181 if (devsoftc.inuse) 182 return (EBUSY); 183 /* move to init */ 184 devsoftc.inuse = 1; 185 devsoftc.nonblock = 0; 186 devsoftc.async_proc = NULL; 187 return (0); 188 } 189 190 static int 191 devclose(struct dev_close_args *ap) 192 { 193 devsoftc.inuse = 0; 194 lockmgr(&devsoftc.lock, LK_EXCLUSIVE); 195 wakeup(&devsoftc); 196 lockmgr(&devsoftc.lock, LK_RELEASE); 197 198 return (0); 199 } 200 201 /* 202 * The read channel for this device is used to report changes to 203 * userland in realtime. We are required to free the data as well as 204 * the n1 object because we allocate them separately. Also note that 205 * we return one record at a time. If you try to read this device a 206 * character at a time, you will lose the rest of the data. Listening 207 * programs are expected to cope. 208 */ 209 static int 210 devread(struct dev_read_args *ap) 211 { 212 struct uio *uio = ap->a_uio; 213 struct dev_event_info *n1; 214 int rv; 215 216 lockmgr(&devsoftc.lock, LK_EXCLUSIVE); 217 while (TAILQ_EMPTY(&devsoftc.devq)) { 218 if (devsoftc.nonblock) { 219 lockmgr(&devsoftc.lock, LK_RELEASE); 220 return (EAGAIN); 221 } 222 tsleep_interlock(&devsoftc, PCATCH); 223 lockmgr(&devsoftc.lock, LK_RELEASE); 224 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0); 225 lockmgr(&devsoftc.lock, LK_EXCLUSIVE); 226 if (rv) { 227 /* 228 * Need to translate ERESTART to EINTR here? -- jake 229 */ 230 lockmgr(&devsoftc.lock, LK_RELEASE); 231 return (rv); 232 } 233 } 234 n1 = TAILQ_FIRST(&devsoftc.devq); 235 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 236 lockmgr(&devsoftc.lock, LK_RELEASE); 237 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio); 238 kfree(n1->dei_data, M_BUS); 239 kfree(n1, M_BUS); 240 return (rv); 241 } 242 243 static int 244 devioctl(struct dev_ioctl_args *ap) 245 { 246 switch (ap->a_cmd) { 247 248 case FIONBIO: 249 if (*(int*)ap->a_data) 250 devsoftc.nonblock = 1; 251 else 252 devsoftc.nonblock = 0; 253 return (0); 254 case FIOASYNC: 255 if (*(int*)ap->a_data) 256 devsoftc.async_proc = curproc; 257 else 258 devsoftc.async_proc = NULL; 259 return (0); 260 261 /* (un)Support for other fcntl() calls. */ 262 case FIOCLEX: 263 case FIONCLEX: 264 case FIONREAD: 265 case FIOSETOWN: 266 case FIOGETOWN: 267 default: 268 break; 269 } 270 return (ENOTTY); 271 } 272 273 static void dev_filter_detach(struct knote *); 274 static int dev_filter_read(struct knote *, long); 275 276 static struct filterops dev_filtops = 277 { FILTEROP_ISFD, NULL, dev_filter_detach, dev_filter_read }; 278 279 static int 280 devkqfilter(struct dev_kqfilter_args *ap) 281 { 282 struct knote *kn = ap->a_kn; 283 struct klist *klist; 284 285 ap->a_result = 0; 286 lockmgr(&devsoftc.lock, LK_EXCLUSIVE); 287 288 switch (kn->kn_filter) { 289 case EVFILT_READ: 290 kn->kn_fop = &dev_filtops; 291 break; 292 default: 293 ap->a_result = EOPNOTSUPP; 294 lockmgr(&devsoftc.lock, LK_RELEASE); 295 return (0); 296 } 297 298 klist = &devsoftc.kq.ki_note; 299 knote_insert(klist, kn); 300 301 lockmgr(&devsoftc.lock, LK_RELEASE); 302 303 return (0); 304 } 305 306 static void 307 dev_filter_detach(struct knote *kn) 308 { 309 struct klist *klist; 310 311 lockmgr(&devsoftc.lock, LK_EXCLUSIVE); 312 klist = &devsoftc.kq.ki_note; 313 knote_remove(klist, kn); 314 lockmgr(&devsoftc.lock, LK_RELEASE); 315 } 316 317 static int 318 dev_filter_read(struct knote *kn, long hint) 319 { 320 int ready = 0; 321 322 lockmgr(&devsoftc.lock, LK_EXCLUSIVE); 323 if (!TAILQ_EMPTY(&devsoftc.devq)) 324 ready = 1; 325 lockmgr(&devsoftc.lock, LK_RELEASE); 326 327 return (ready); 328 } 329 330 331 /** 332 * @brief Return whether the userland process is running 333 */ 334 boolean_t 335 devctl_process_running(void) 336 { 337 return (devsoftc.inuse == 1); 338 } 339 340 /** 341 * @brief Queue data to be read from the devctl device 342 * 343 * Generic interface to queue data to the devctl device. It is 344 * assumed that @p data is properly formatted. It is further assumed 345 * that @p data is allocated using the M_BUS malloc type. 346 */ 347 void 348 devctl_queue_data(char *data) 349 { 350 struct dev_event_info *n1 = NULL; 351 struct proc *p; 352 353 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT); 354 if (n1 == NULL) 355 return; 356 n1->dei_data = data; 357 lockmgr(&devsoftc.lock, LK_EXCLUSIVE); 358 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link); 359 wakeup(&devsoftc); 360 lockmgr(&devsoftc.lock, LK_RELEASE); 361 get_mplock(); /* XXX */ 362 KNOTE(&devsoftc.kq.ki_note, 0); 363 rel_mplock(); /* XXX */ 364 p = devsoftc.async_proc; 365 if (p != NULL) 366 ksignal(p, SIGIO); 367 } 368 369 /** 370 * @brief Send a 'notification' to userland, using standard ways 371 */ 372 void 373 devctl_notify(const char *system, const char *subsystem, const char *type, 374 const char *data) 375 { 376 int len = 0; 377 char *msg; 378 379 if (system == NULL) 380 return; /* BOGUS! Must specify system. */ 381 if (subsystem == NULL) 382 return; /* BOGUS! Must specify subsystem. */ 383 if (type == NULL) 384 return; /* BOGUS! Must specify type. */ 385 len += strlen(" system=") + strlen(system); 386 len += strlen(" subsystem=") + strlen(subsystem); 387 len += strlen(" type=") + strlen(type); 388 /* add in the data message plus newline. */ 389 if (data != NULL) 390 len += strlen(data); 391 len += 3; /* '!', '\n', and NUL */ 392 msg = kmalloc(len, M_BUS, M_NOWAIT); 393 if (msg == NULL) 394 return; /* Drop it on the floor */ 395 if (data != NULL) 396 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n", 397 system, subsystem, type, data); 398 else 399 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n", 400 system, subsystem, type); 401 devctl_queue_data(msg); 402 } 403 404 /* 405 * Common routine that tries to make sending messages as easy as possible. 406 * We allocate memory for the data, copy strings into that, but do not 407 * free it unless there's an error. The dequeue part of the driver should 408 * free the data. We don't send data when the device is disabled. We do 409 * send data, even when we have no listeners, because we wish to avoid 410 * races relating to startup and restart of listening applications. 411 * 412 * devaddq is designed to string together the type of event, with the 413 * object of that event, plus the plug and play info and location info 414 * for that event. This is likely most useful for devices, but less 415 * useful for other consumers of this interface. Those should use 416 * the devctl_queue_data() interface instead. 417 */ 418 static void 419 devaddq(const char *type, const char *what, device_t dev) 420 { 421 char *data = NULL; 422 char *loc = NULL; 423 char *pnp = NULL; 424 const char *parstr; 425 426 if (devctl_disable) 427 return; 428 data = kmalloc(1024, M_BUS, M_NOWAIT); 429 if (data == NULL) 430 goto bad; 431 432 /* get the bus specific location of this device */ 433 loc = kmalloc(1024, M_BUS, M_NOWAIT); 434 if (loc == NULL) 435 goto bad; 436 *loc = '\0'; 437 bus_child_location_str(dev, loc, 1024); 438 439 /* Get the bus specific pnp info of this device */ 440 pnp = kmalloc(1024, M_BUS, M_NOWAIT); 441 if (pnp == NULL) 442 goto bad; 443 *pnp = '\0'; 444 bus_child_pnpinfo_str(dev, pnp, 1024); 445 446 /* Get the parent of this device, or / if high enough in the tree. */ 447 if (device_get_parent(dev) == NULL) 448 parstr = "."; /* Or '/' ? */ 449 else 450 parstr = device_get_nameunit(device_get_parent(dev)); 451 /* String it all together. */ 452 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp, 453 parstr); 454 kfree(loc, M_BUS); 455 kfree(pnp, M_BUS); 456 devctl_queue_data(data); 457 return; 458 bad: 459 kfree(pnp, M_BUS); 460 kfree(loc, M_BUS); 461 kfree(data, M_BUS); 462 return; 463 } 464 465 /* 466 * A device was added to the tree. We are called just after it successfully 467 * attaches (that is, probe and attach success for this device). No call 468 * is made if a device is merely parented into the tree. See devnomatch 469 * if probe fails. If attach fails, no notification is sent (but maybe 470 * we should have a different message for this). 471 */ 472 static void 473 devadded(device_t dev) 474 { 475 char *pnp = NULL; 476 char *tmp = NULL; 477 478 pnp = kmalloc(1024, M_BUS, M_NOWAIT); 479 if (pnp == NULL) 480 goto fail; 481 tmp = kmalloc(1024, M_BUS, M_NOWAIT); 482 if (tmp == NULL) 483 goto fail; 484 *pnp = '\0'; 485 bus_child_pnpinfo_str(dev, pnp, 1024); 486 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp); 487 devaddq("+", tmp, dev); 488 fail: 489 if (pnp != NULL) 490 kfree(pnp, M_BUS); 491 if (tmp != NULL) 492 kfree(tmp, M_BUS); 493 return; 494 } 495 496 /* 497 * A device was removed from the tree. We are called just before this 498 * happens. 499 */ 500 static void 501 devremoved(device_t dev) 502 { 503 char *pnp = NULL; 504 char *tmp = NULL; 505 506 pnp = kmalloc(1024, M_BUS, M_NOWAIT); 507 if (pnp == NULL) 508 goto fail; 509 tmp = kmalloc(1024, M_BUS, M_NOWAIT); 510 if (tmp == NULL) 511 goto fail; 512 *pnp = '\0'; 513 bus_child_pnpinfo_str(dev, pnp, 1024); 514 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp); 515 devaddq("-", tmp, dev); 516 fail: 517 if (pnp != NULL) 518 kfree(pnp, M_BUS); 519 if (tmp != NULL) 520 kfree(tmp, M_BUS); 521 return; 522 } 523 524 /* 525 * Called when there's no match for this device. This is only called 526 * the first time that no match happens, so we don't keep getitng this 527 * message. Should that prove to be undesirable, we can change it. 528 * This is called when all drivers that can attach to a given bus 529 * decline to accept this device. Other errrors may not be detected. 530 */ 531 static void 532 devnomatch(device_t dev) 533 { 534 devaddq("?", "", dev); 535 } 536 537 static int 538 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS) 539 { 540 struct dev_event_info *n1; 541 int dis, error; 542 543 dis = devctl_disable; 544 error = sysctl_handle_int(oidp, &dis, 0, req); 545 if (error || !req->newptr) 546 return (error); 547 lockmgr(&devsoftc.lock, LK_EXCLUSIVE); 548 devctl_disable = dis; 549 if (dis) { 550 while (!TAILQ_EMPTY(&devsoftc.devq)) { 551 n1 = TAILQ_FIRST(&devsoftc.devq); 552 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 553 kfree(n1->dei_data, M_BUS); 554 kfree(n1, M_BUS); 555 } 556 } 557 lockmgr(&devsoftc.lock, LK_RELEASE); 558 return (0); 559 } 560 561 /* End of /dev/devctl code */ 562 563 TAILQ_HEAD(,device) bus_data_devices; 564 static int bus_data_generation = 1; 565 566 kobj_method_t null_methods[] = { 567 { 0, 0 } 568 }; 569 570 DEFINE_CLASS(null, null_methods, 0); 571 572 /* 573 * Devclass implementation 574 */ 575 576 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 577 578 static devclass_t 579 devclass_find_internal(const char *classname, const char *parentname, 580 int create) 581 { 582 devclass_t dc; 583 584 PDEBUG(("looking for %s", classname)); 585 if (classname == NULL) 586 return(NULL); 587 588 TAILQ_FOREACH(dc, &devclasses, link) 589 if (!strcmp(dc->name, classname)) 590 break; 591 592 if (create && !dc) { 593 PDEBUG(("creating %s", classname)); 594 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1, 595 M_BUS, M_INTWAIT | M_ZERO); 596 if (!dc) 597 return(NULL); 598 dc->parent = NULL; 599 dc->name = (char*) (dc + 1); 600 strcpy(dc->name, classname); 601 dc->devices = NULL; 602 dc->maxunit = 0; 603 TAILQ_INIT(&dc->drivers); 604 TAILQ_INSERT_TAIL(&devclasses, dc, link); 605 606 bus_data_generation_update(); 607 608 } 609 if (parentname && dc && !dc->parent) 610 dc->parent = devclass_find_internal(parentname, NULL, FALSE); 611 612 return(dc); 613 } 614 615 devclass_t 616 devclass_create(const char *classname) 617 { 618 return(devclass_find_internal(classname, NULL, TRUE)); 619 } 620 621 devclass_t 622 devclass_find(const char *classname) 623 { 624 return(devclass_find_internal(classname, NULL, FALSE)); 625 } 626 627 device_t 628 devclass_find_unit(const char *classname, int unit) 629 { 630 devclass_t dc; 631 632 if ((dc = devclass_find(classname)) != NULL) 633 return(devclass_get_device(dc, unit)); 634 return (NULL); 635 } 636 637 int 638 devclass_add_driver(devclass_t dc, driver_t *driver) 639 { 640 driverlink_t dl; 641 device_t dev; 642 int i; 643 644 PDEBUG(("%s", DRIVERNAME(driver))); 645 646 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO); 647 if (!dl) 648 return(ENOMEM); 649 650 /* 651 * Compile the driver's methods. Also increase the reference count 652 * so that the class doesn't get freed when the last instance 653 * goes. This means we can safely use static methods and avoids a 654 * double-free in devclass_delete_driver. 655 */ 656 kobj_class_instantiate(driver); 657 658 /* 659 * Make sure the devclass which the driver is implementing exists. 660 */ 661 devclass_find_internal(driver->name, NULL, TRUE); 662 663 dl->driver = driver; 664 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 665 666 /* 667 * Call BUS_DRIVER_ADDED for any existing busses in this class, 668 * but only if the bus has already been attached (otherwise we 669 * might probe too early). 670 * 671 * This is what will cause a newly loaded module to be associated 672 * with hardware. bus_generic_driver_added() is typically what ends 673 * up being called. 674 */ 675 for (i = 0; i < dc->maxunit; i++) { 676 if ((dev = dc->devices[i]) != NULL) { 677 if (dev->state >= DS_ATTACHED) 678 BUS_DRIVER_ADDED(dev, driver); 679 } 680 } 681 682 bus_data_generation_update(); 683 return(0); 684 } 685 686 int 687 devclass_delete_driver(devclass_t busclass, driver_t *driver) 688 { 689 devclass_t dc = devclass_find(driver->name); 690 driverlink_t dl; 691 device_t dev; 692 int i; 693 int error; 694 695 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 696 697 if (!dc) 698 return(0); 699 700 /* 701 * Find the link structure in the bus' list of drivers. 702 */ 703 TAILQ_FOREACH(dl, &busclass->drivers, link) 704 if (dl->driver == driver) 705 break; 706 707 if (!dl) { 708 PDEBUG(("%s not found in %s list", driver->name, busclass->name)); 709 return(ENOENT); 710 } 711 712 /* 713 * Disassociate from any devices. We iterate through all the 714 * devices in the devclass of the driver and detach any which are 715 * using the driver and which have a parent in the devclass which 716 * we are deleting from. 717 * 718 * Note that since a driver can be in multiple devclasses, we 719 * should not detach devices which are not children of devices in 720 * the affected devclass. 721 */ 722 for (i = 0; i < dc->maxunit; i++) 723 if (dc->devices[i]) { 724 dev = dc->devices[i]; 725 if (dev->driver == driver && dev->parent && 726 dev->parent->devclass == busclass) { 727 if ((error = device_detach(dev)) != 0) 728 return(error); 729 device_set_driver(dev, NULL); 730 } 731 } 732 733 TAILQ_REMOVE(&busclass->drivers, dl, link); 734 kfree(dl, M_BUS); 735 736 kobj_class_uninstantiate(driver); 737 738 bus_data_generation_update(); 739 return(0); 740 } 741 742 static driverlink_t 743 devclass_find_driver_internal(devclass_t dc, const char *classname) 744 { 745 driverlink_t dl; 746 747 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 748 749 TAILQ_FOREACH(dl, &dc->drivers, link) 750 if (!strcmp(dl->driver->name, classname)) 751 return(dl); 752 753 PDEBUG(("not found")); 754 return(NULL); 755 } 756 757 kobj_class_t 758 devclass_find_driver(devclass_t dc, const char *classname) 759 { 760 driverlink_t dl; 761 762 dl = devclass_find_driver_internal(dc, classname); 763 if (dl) 764 return(dl->driver); 765 else 766 return(NULL); 767 } 768 769 const char * 770 devclass_get_name(devclass_t dc) 771 { 772 return(dc->name); 773 } 774 775 device_t 776 devclass_get_device(devclass_t dc, int unit) 777 { 778 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 779 return(NULL); 780 return(dc->devices[unit]); 781 } 782 783 void * 784 devclass_get_softc(devclass_t dc, int unit) 785 { 786 device_t dev; 787 788 dev = devclass_get_device(dc, unit); 789 if (!dev) 790 return(NULL); 791 792 return(device_get_softc(dev)); 793 } 794 795 int 796 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 797 { 798 int i; 799 int count; 800 device_t *list; 801 802 count = 0; 803 for (i = 0; i < dc->maxunit; i++) 804 if (dc->devices[i]) 805 count++; 806 807 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO); 808 if (list == NULL) 809 return(ENOMEM); 810 811 count = 0; 812 for (i = 0; i < dc->maxunit; i++) 813 if (dc->devices[i]) { 814 list[count] = dc->devices[i]; 815 count++; 816 } 817 818 *devlistp = list; 819 *devcountp = count; 820 821 return(0); 822 } 823 824 /** 825 * @brief Get a list of drivers in the devclass 826 * 827 * An array containing a list of pointers to all the drivers in the 828 * given devclass is allocated and returned in @p *listp. The number 829 * of drivers in the array is returned in @p *countp. The caller should 830 * free the array using @c free(p, M_TEMP). 831 * 832 * @param dc the devclass to examine 833 * @param listp gives location for array pointer return value 834 * @param countp gives location for number of array elements 835 * return value 836 * 837 * @retval 0 success 838 * @retval ENOMEM the array allocation failed 839 */ 840 int 841 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp) 842 { 843 driverlink_t dl; 844 driver_t **list; 845 int count; 846 847 count = 0; 848 TAILQ_FOREACH(dl, &dc->drivers, link) 849 count++; 850 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT); 851 if (list == NULL) 852 return (ENOMEM); 853 854 count = 0; 855 TAILQ_FOREACH(dl, &dc->drivers, link) { 856 list[count] = dl->driver; 857 count++; 858 } 859 *listp = list; 860 *countp = count; 861 862 return (0); 863 } 864 865 /** 866 * @brief Get the number of devices in a devclass 867 * 868 * @param dc the devclass to examine 869 */ 870 int 871 devclass_get_count(devclass_t dc) 872 { 873 int count, i; 874 875 count = 0; 876 for (i = 0; i < dc->maxunit; i++) 877 if (dc->devices[i]) 878 count++; 879 return (count); 880 } 881 882 int 883 devclass_get_maxunit(devclass_t dc) 884 { 885 return(dc->maxunit); 886 } 887 888 void 889 devclass_set_parent(devclass_t dc, devclass_t pdc) 890 { 891 dc->parent = pdc; 892 } 893 894 devclass_t 895 devclass_get_parent(devclass_t dc) 896 { 897 return(dc->parent); 898 } 899 900 static int 901 devclass_alloc_unit(devclass_t dc, int *unitp) 902 { 903 int unit = *unitp; 904 905 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 906 907 /* If we have been given a wired unit number, check for existing device */ 908 if (unit != -1) { 909 if (unit >= 0 && unit < dc->maxunit && 910 dc->devices[unit] != NULL) { 911 if (bootverbose) 912 kprintf("%s-: %s%d exists, using next available unit number\n", 913 dc->name, dc->name, unit); 914 /* find the next available slot */ 915 while (++unit < dc->maxunit && dc->devices[unit] != NULL) 916 ; 917 } 918 } else { 919 /* Unwired device, find the next available slot for it */ 920 unit = 0; 921 while (unit < dc->maxunit && dc->devices[unit] != NULL) 922 unit++; 923 } 924 925 /* 926 * We've selected a unit beyond the length of the table, so let's 927 * extend the table to make room for all units up to and including 928 * this one. 929 */ 930 if (unit >= dc->maxunit) { 931 device_t *newlist; 932 int newsize; 933 934 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 935 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS, 936 M_INTWAIT | M_ZERO); 937 if (newlist == NULL) 938 return(ENOMEM); 939 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 940 if (dc->devices) 941 kfree(dc->devices, M_BUS); 942 dc->devices = newlist; 943 dc->maxunit = newsize; 944 } 945 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 946 947 *unitp = unit; 948 return(0); 949 } 950 951 static int 952 devclass_add_device(devclass_t dc, device_t dev) 953 { 954 int buflen, error; 955 956 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 957 958 buflen = strlen(dc->name) + 5; 959 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO); 960 if (!dev->nameunit) 961 return(ENOMEM); 962 963 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 964 kfree(dev->nameunit, M_BUS); 965 dev->nameunit = NULL; 966 return(error); 967 } 968 dc->devices[dev->unit] = dev; 969 dev->devclass = dc; 970 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 971 972 return(0); 973 } 974 975 static int 976 devclass_delete_device(devclass_t dc, device_t dev) 977 { 978 if (!dc || !dev) 979 return(0); 980 981 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 982 983 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 984 panic("devclass_delete_device: inconsistent device class"); 985 dc->devices[dev->unit] = NULL; 986 if (dev->flags & DF_WILDCARD) 987 dev->unit = -1; 988 dev->devclass = NULL; 989 kfree(dev->nameunit, M_BUS); 990 dev->nameunit = NULL; 991 992 return(0); 993 } 994 995 static device_t 996 make_device(device_t parent, const char *name, int unit) 997 { 998 device_t dev; 999 devclass_t dc; 1000 1001 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1002 1003 if (name != NULL) { 1004 dc = devclass_find_internal(name, NULL, TRUE); 1005 if (!dc) { 1006 kprintf("make_device: can't find device class %s\n", name); 1007 return(NULL); 1008 } 1009 } else 1010 dc = NULL; 1011 1012 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO); 1013 if (!dev) 1014 return(0); 1015 1016 dev->parent = parent; 1017 TAILQ_INIT(&dev->children); 1018 kobj_init((kobj_t) dev, &null_class); 1019 dev->driver = NULL; 1020 dev->devclass = NULL; 1021 dev->unit = unit; 1022 dev->nameunit = NULL; 1023 dev->desc = NULL; 1024 dev->busy = 0; 1025 dev->devflags = 0; 1026 dev->flags = DF_ENABLED; 1027 dev->order = 0; 1028 if (unit == -1) 1029 dev->flags |= DF_WILDCARD; 1030 if (name) { 1031 dev->flags |= DF_FIXEDCLASS; 1032 if (devclass_add_device(dc, dev) != 0) { 1033 kobj_delete((kobj_t)dev, M_BUS); 1034 return(NULL); 1035 } 1036 } 1037 dev->ivars = NULL; 1038 dev->softc = NULL; 1039 1040 dev->state = DS_NOTPRESENT; 1041 1042 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1043 bus_data_generation_update(); 1044 1045 return(dev); 1046 } 1047 1048 static int 1049 device_print_child(device_t dev, device_t child) 1050 { 1051 int retval = 0; 1052 1053 if (device_is_alive(child)) 1054 retval += BUS_PRINT_CHILD(dev, child); 1055 else 1056 retval += device_printf(child, " not found\n"); 1057 1058 return(retval); 1059 } 1060 1061 device_t 1062 device_add_child(device_t dev, const char *name, int unit) 1063 { 1064 return device_add_child_ordered(dev, 0, name, unit); 1065 } 1066 1067 device_t 1068 device_add_child_ordered(device_t dev, int order, const char *name, int unit) 1069 { 1070 device_t child; 1071 device_t place; 1072 1073 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev), 1074 order, unit)); 1075 1076 child = make_device(dev, name, unit); 1077 if (child == NULL) 1078 return child; 1079 child->order = order; 1080 1081 TAILQ_FOREACH(place, &dev->children, link) 1082 if (place->order > order) 1083 break; 1084 1085 if (place) { 1086 /* 1087 * The device 'place' is the first device whose order is 1088 * greater than the new child. 1089 */ 1090 TAILQ_INSERT_BEFORE(place, child, link); 1091 } else { 1092 /* 1093 * The new child's order is greater or equal to the order of 1094 * any existing device. Add the child to the tail of the list. 1095 */ 1096 TAILQ_INSERT_TAIL(&dev->children, child, link); 1097 } 1098 1099 bus_data_generation_update(); 1100 return(child); 1101 } 1102 1103 int 1104 device_delete_child(device_t dev, device_t child) 1105 { 1106 int error; 1107 device_t grandchild; 1108 1109 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1110 1111 /* remove children first */ 1112 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 1113 error = device_delete_child(child, grandchild); 1114 if (error) 1115 return(error); 1116 } 1117 1118 if ((error = device_detach(child)) != 0) 1119 return(error); 1120 if (child->devclass) 1121 devclass_delete_device(child->devclass, child); 1122 TAILQ_REMOVE(&dev->children, child, link); 1123 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1124 device_set_desc(child, NULL); 1125 kobj_delete((kobj_t)child, M_BUS); 1126 1127 bus_data_generation_update(); 1128 return(0); 1129 } 1130 1131 /** 1132 * @brief Find a device given a unit number 1133 * 1134 * This is similar to devclass_get_devices() but only searches for 1135 * devices which have @p dev as a parent. 1136 * 1137 * @param dev the parent device to search 1138 * @param unit the unit number to search for. If the unit is -1, 1139 * return the first child of @p dev which has name 1140 * @p classname (that is, the one with the lowest unit.) 1141 * 1142 * @returns the device with the given unit number or @c 1143 * NULL if there is no such device 1144 */ 1145 device_t 1146 device_find_child(device_t dev, const char *classname, int unit) 1147 { 1148 devclass_t dc; 1149 device_t child; 1150 1151 dc = devclass_find(classname); 1152 if (!dc) 1153 return(NULL); 1154 1155 if (unit != -1) { 1156 child = devclass_get_device(dc, unit); 1157 if (child && child->parent == dev) 1158 return (child); 1159 } else { 1160 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { 1161 child = devclass_get_device(dc, unit); 1162 if (child && child->parent == dev) 1163 return (child); 1164 } 1165 } 1166 return(NULL); 1167 } 1168 1169 static driverlink_t 1170 first_matching_driver(devclass_t dc, device_t dev) 1171 { 1172 if (dev->devclass) 1173 return(devclass_find_driver_internal(dc, dev->devclass->name)); 1174 else 1175 return(TAILQ_FIRST(&dc->drivers)); 1176 } 1177 1178 static driverlink_t 1179 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1180 { 1181 if (dev->devclass) { 1182 driverlink_t dl; 1183 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1184 if (!strcmp(dev->devclass->name, dl->driver->name)) 1185 return(dl); 1186 return(NULL); 1187 } else 1188 return(TAILQ_NEXT(last, link)); 1189 } 1190 1191 static int 1192 device_probe_child(device_t dev, device_t child) 1193 { 1194 devclass_t dc; 1195 driverlink_t best = 0; 1196 driverlink_t dl; 1197 int result, pri = 0; 1198 int hasclass = (child->devclass != 0); 1199 1200 dc = dev->devclass; 1201 if (!dc) 1202 panic("device_probe_child: parent device has no devclass"); 1203 1204 if (child->state == DS_ALIVE) 1205 return(0); 1206 1207 for (; dc; dc = dc->parent) { 1208 for (dl = first_matching_driver(dc, child); dl; 1209 dl = next_matching_driver(dc, child, dl)) { 1210 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 1211 device_set_driver(child, dl->driver); 1212 if (!hasclass) 1213 device_set_devclass(child, dl->driver->name); 1214 result = DEVICE_PROBE(child); 1215 if (!hasclass) 1216 device_set_devclass(child, 0); 1217 1218 /* 1219 * If the driver returns SUCCESS, there can be 1220 * no higher match for this device. 1221 */ 1222 if (result == 0) { 1223 best = dl; 1224 pri = 0; 1225 break; 1226 } 1227 1228 /* 1229 * The driver returned an error so it 1230 * certainly doesn't match. 1231 */ 1232 if (result > 0) { 1233 device_set_driver(child, 0); 1234 continue; 1235 } 1236 1237 /* 1238 * A priority lower than SUCCESS, remember the 1239 * best matching driver. Initialise the value 1240 * of pri for the first match. 1241 */ 1242 if (best == 0 || result > pri) { 1243 best = dl; 1244 pri = result; 1245 continue; 1246 } 1247 } 1248 /* 1249 * If we have unambiguous match in this devclass, 1250 * don't look in the parent. 1251 */ 1252 if (best && pri == 0) 1253 break; 1254 } 1255 1256 /* 1257 * If we found a driver, change state and initialise the devclass. 1258 */ 1259 if (best) { 1260 if (!child->devclass) 1261 device_set_devclass(child, best->driver->name); 1262 device_set_driver(child, best->driver); 1263 if (pri < 0) { 1264 /* 1265 * A bit bogus. Call the probe method again to make 1266 * sure that we have the right description. 1267 */ 1268 DEVICE_PROBE(child); 1269 } 1270 1271 bus_data_generation_update(); 1272 child->state = DS_ALIVE; 1273 return(0); 1274 } 1275 1276 return(ENXIO); 1277 } 1278 1279 device_t 1280 device_get_parent(device_t dev) 1281 { 1282 return dev->parent; 1283 } 1284 1285 int 1286 device_get_children(device_t dev, device_t **devlistp, int *devcountp) 1287 { 1288 int count; 1289 device_t child; 1290 device_t *list; 1291 1292 count = 0; 1293 TAILQ_FOREACH(child, &dev->children, link) 1294 count++; 1295 1296 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO); 1297 if (!list) 1298 return(ENOMEM); 1299 1300 count = 0; 1301 TAILQ_FOREACH(child, &dev->children, link) { 1302 list[count] = child; 1303 count++; 1304 } 1305 1306 *devlistp = list; 1307 *devcountp = count; 1308 1309 return(0); 1310 } 1311 1312 driver_t * 1313 device_get_driver(device_t dev) 1314 { 1315 return(dev->driver); 1316 } 1317 1318 devclass_t 1319 device_get_devclass(device_t dev) 1320 { 1321 return(dev->devclass); 1322 } 1323 1324 const char * 1325 device_get_name(device_t dev) 1326 { 1327 if (dev->devclass) 1328 return devclass_get_name(dev->devclass); 1329 return(NULL); 1330 } 1331 1332 const char * 1333 device_get_nameunit(device_t dev) 1334 { 1335 return(dev->nameunit); 1336 } 1337 1338 int 1339 device_get_unit(device_t dev) 1340 { 1341 return(dev->unit); 1342 } 1343 1344 const char * 1345 device_get_desc(device_t dev) 1346 { 1347 return(dev->desc); 1348 } 1349 1350 uint32_t 1351 device_get_flags(device_t dev) 1352 { 1353 return(dev->devflags); 1354 } 1355 1356 int 1357 device_print_prettyname(device_t dev) 1358 { 1359 const char *name = device_get_name(dev); 1360 1361 if (name == 0) 1362 return kprintf("unknown: "); 1363 else 1364 return kprintf("%s%d: ", name, device_get_unit(dev)); 1365 } 1366 1367 int 1368 device_printf(device_t dev, const char * fmt, ...) 1369 { 1370 __va_list ap; 1371 int retval; 1372 1373 retval = device_print_prettyname(dev); 1374 __va_start(ap, fmt); 1375 retval += kvprintf(fmt, ap); 1376 __va_end(ap); 1377 return retval; 1378 } 1379 1380 static void 1381 device_set_desc_internal(device_t dev, const char* desc, int copy) 1382 { 1383 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 1384 kfree(dev->desc, M_BUS); 1385 dev->flags &= ~DF_DESCMALLOCED; 1386 dev->desc = NULL; 1387 } 1388 1389 if (copy && desc) { 1390 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT); 1391 if (dev->desc) { 1392 strcpy(dev->desc, desc); 1393 dev->flags |= DF_DESCMALLOCED; 1394 } 1395 } else { 1396 /* Avoid a -Wcast-qual warning */ 1397 dev->desc = (char *)(uintptr_t) desc; 1398 } 1399 1400 bus_data_generation_update(); 1401 } 1402 1403 void 1404 device_set_desc(device_t dev, const char* desc) 1405 { 1406 device_set_desc_internal(dev, desc, FALSE); 1407 } 1408 1409 void 1410 device_set_desc_copy(device_t dev, const char* desc) 1411 { 1412 device_set_desc_internal(dev, desc, TRUE); 1413 } 1414 1415 void 1416 device_set_flags(device_t dev, uint32_t flags) 1417 { 1418 dev->devflags = flags; 1419 } 1420 1421 void * 1422 device_get_softc(device_t dev) 1423 { 1424 return dev->softc; 1425 } 1426 1427 void 1428 device_set_softc(device_t dev, void *softc) 1429 { 1430 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 1431 kfree(dev->softc, M_BUS); 1432 dev->softc = softc; 1433 if (dev->softc) 1434 dev->flags |= DF_EXTERNALSOFTC; 1435 else 1436 dev->flags &= ~DF_EXTERNALSOFTC; 1437 } 1438 1439 void 1440 device_set_async_attach(device_t dev, int enable) 1441 { 1442 if (enable) 1443 dev->flags |= DF_ASYNCPROBE; 1444 else 1445 dev->flags &= ~DF_ASYNCPROBE; 1446 } 1447 1448 void * 1449 device_get_ivars(device_t dev) 1450 { 1451 return dev->ivars; 1452 } 1453 1454 void 1455 device_set_ivars(device_t dev, void * ivars) 1456 { 1457 if (!dev) 1458 return; 1459 1460 dev->ivars = ivars; 1461 } 1462 1463 device_state_t 1464 device_get_state(device_t dev) 1465 { 1466 return(dev->state); 1467 } 1468 1469 void 1470 device_enable(device_t dev) 1471 { 1472 dev->flags |= DF_ENABLED; 1473 } 1474 1475 void 1476 device_disable(device_t dev) 1477 { 1478 dev->flags &= ~DF_ENABLED; 1479 } 1480 1481 /* 1482 * YYY cannot block 1483 */ 1484 void 1485 device_busy(device_t dev) 1486 { 1487 if (dev->state < DS_ATTACHED) 1488 panic("device_busy: called for unattached device"); 1489 if (dev->busy == 0 && dev->parent) 1490 device_busy(dev->parent); 1491 dev->busy++; 1492 dev->state = DS_BUSY; 1493 } 1494 1495 /* 1496 * YYY cannot block 1497 */ 1498 void 1499 device_unbusy(device_t dev) 1500 { 1501 if (dev->state != DS_BUSY) 1502 panic("device_unbusy: called for non-busy device"); 1503 dev->busy--; 1504 if (dev->busy == 0) { 1505 if (dev->parent) 1506 device_unbusy(dev->parent); 1507 dev->state = DS_ATTACHED; 1508 } 1509 } 1510 1511 void 1512 device_quiet(device_t dev) 1513 { 1514 dev->flags |= DF_QUIET; 1515 } 1516 1517 void 1518 device_verbose(device_t dev) 1519 { 1520 dev->flags &= ~DF_QUIET; 1521 } 1522 1523 int 1524 device_is_quiet(device_t dev) 1525 { 1526 return((dev->flags & DF_QUIET) != 0); 1527 } 1528 1529 int 1530 device_is_enabled(device_t dev) 1531 { 1532 return((dev->flags & DF_ENABLED) != 0); 1533 } 1534 1535 int 1536 device_is_alive(device_t dev) 1537 { 1538 return(dev->state >= DS_ALIVE); 1539 } 1540 1541 int 1542 device_is_attached(device_t dev) 1543 { 1544 return(dev->state >= DS_ATTACHED); 1545 } 1546 1547 int 1548 device_set_devclass(device_t dev, const char *classname) 1549 { 1550 devclass_t dc; 1551 int error; 1552 1553 if (!classname) { 1554 if (dev->devclass) 1555 devclass_delete_device(dev->devclass, dev); 1556 return(0); 1557 } 1558 1559 if (dev->devclass) { 1560 kprintf("device_set_devclass: device class already set\n"); 1561 return(EINVAL); 1562 } 1563 1564 dc = devclass_find_internal(classname, NULL, TRUE); 1565 if (!dc) 1566 return(ENOMEM); 1567 1568 error = devclass_add_device(dc, dev); 1569 1570 bus_data_generation_update(); 1571 return(error); 1572 } 1573 1574 int 1575 device_set_driver(device_t dev, driver_t *driver) 1576 { 1577 if (dev->state >= DS_ATTACHED) 1578 return(EBUSY); 1579 1580 if (dev->driver == driver) 1581 return(0); 1582 1583 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 1584 kfree(dev->softc, M_BUS); 1585 dev->softc = NULL; 1586 } 1587 kobj_delete((kobj_t) dev, 0); 1588 dev->driver = driver; 1589 if (driver) { 1590 kobj_init((kobj_t) dev, (kobj_class_t) driver); 1591 if (!(dev->flags & DF_EXTERNALSOFTC)) { 1592 dev->softc = kmalloc(driver->size, M_BUS, 1593 M_INTWAIT | M_ZERO); 1594 if (!dev->softc) { 1595 kobj_delete((kobj_t)dev, 0); 1596 kobj_init((kobj_t) dev, &null_class); 1597 dev->driver = NULL; 1598 return(ENOMEM); 1599 } 1600 } 1601 } else { 1602 kobj_init((kobj_t) dev, &null_class); 1603 } 1604 1605 bus_data_generation_update(); 1606 return(0); 1607 } 1608 1609 int 1610 device_probe_and_attach(device_t dev) 1611 { 1612 device_t bus = dev->parent; 1613 int error = 0; 1614 1615 if (dev->state >= DS_ALIVE) 1616 return(0); 1617 1618 if ((dev->flags & DF_ENABLED) == 0) { 1619 if (bootverbose) { 1620 device_print_prettyname(dev); 1621 kprintf("not probed (disabled)\n"); 1622 } 1623 return(0); 1624 } 1625 1626 error = device_probe_child(bus, dev); 1627 if (error) { 1628 if (!(dev->flags & DF_DONENOMATCH)) { 1629 BUS_PROBE_NOMATCH(bus, dev); 1630 devnomatch(dev); 1631 dev->flags |= DF_DONENOMATCH; 1632 } 1633 return(error); 1634 } 1635 1636 /* 1637 * Output the exact device chain prior to the attach in case the 1638 * system locks up during attach, and generate the full info after 1639 * the attach so correct irq and other information is displayed. 1640 */ 1641 if (bootverbose && !device_is_quiet(dev)) { 1642 device_t tmp; 1643 1644 kprintf("%s", device_get_nameunit(dev)); 1645 for (tmp = dev->parent; tmp; tmp = tmp->parent) 1646 kprintf(".%s", device_get_nameunit(tmp)); 1647 kprintf("\n"); 1648 } 1649 if (!device_is_quiet(dev)) 1650 device_print_child(bus, dev); 1651 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) { 1652 kprintf("%s: probing asynchronously\n", 1653 device_get_nameunit(dev)); 1654 dev->state = DS_INPROGRESS; 1655 device_attach_async(dev); 1656 error = 0; 1657 } else { 1658 error = device_doattach(dev); 1659 } 1660 return(error); 1661 } 1662 1663 /* 1664 * Device is known to be alive, do the attach asynchronously. 1665 * However, serialize the attaches with the mp lock. 1666 */ 1667 static void 1668 device_attach_async(device_t dev) 1669 { 1670 thread_t td; 1671 1672 atomic_add_int(&numasyncthreads, 1); 1673 lwkt_create(device_attach_thread, dev, &td, NULL, 1674 0, 0, (dev->desc ? dev->desc : "devattach")); 1675 } 1676 1677 static void 1678 device_attach_thread(void *arg) 1679 { 1680 device_t dev = arg; 1681 1682 get_mplock(); /* XXX replace with devattach_token later */ 1683 (void)device_doattach(dev); 1684 atomic_subtract_int(&numasyncthreads, 1); 1685 wakeup(&numasyncthreads); 1686 rel_mplock(); /* XXX replace with devattach_token later */ 1687 } 1688 1689 /* 1690 * Device is known to be alive, do the attach (synchronous or asynchronous) 1691 */ 1692 static int 1693 device_doattach(device_t dev) 1694 { 1695 device_t bus = dev->parent; 1696 int hasclass = (dev->devclass != 0); 1697 int error; 1698 1699 error = DEVICE_ATTACH(dev); 1700 if (error == 0) { 1701 dev->state = DS_ATTACHED; 1702 if (bootverbose && !device_is_quiet(dev)) 1703 device_print_child(bus, dev); 1704 devadded(dev); 1705 } else { 1706 kprintf("device_probe_and_attach: %s%d attach returned %d\n", 1707 dev->driver->name, dev->unit, error); 1708 /* Unset the class that was set in device_probe_child */ 1709 if (!hasclass) 1710 device_set_devclass(dev, 0); 1711 device_set_driver(dev, NULL); 1712 dev->state = DS_NOTPRESENT; 1713 } 1714 return(error); 1715 } 1716 1717 int 1718 device_detach(device_t dev) 1719 { 1720 int error; 1721 1722 PDEBUG(("%s", DEVICENAME(dev))); 1723 if (dev->state == DS_BUSY) 1724 return(EBUSY); 1725 if (dev->state != DS_ATTACHED) 1726 return(0); 1727 1728 if ((error = DEVICE_DETACH(dev)) != 0) 1729 return(error); 1730 devremoved(dev); 1731 device_printf(dev, "detached\n"); 1732 if (dev->parent) 1733 BUS_CHILD_DETACHED(dev->parent, dev); 1734 1735 if (!(dev->flags & DF_FIXEDCLASS)) 1736 devclass_delete_device(dev->devclass, dev); 1737 1738 dev->state = DS_NOTPRESENT; 1739 device_set_driver(dev, NULL); 1740 1741 return(0); 1742 } 1743 1744 int 1745 device_shutdown(device_t dev) 1746 { 1747 if (dev->state < DS_ATTACHED) 1748 return 0; 1749 PDEBUG(("%s", DEVICENAME(dev))); 1750 return DEVICE_SHUTDOWN(dev); 1751 } 1752 1753 int 1754 device_set_unit(device_t dev, int unit) 1755 { 1756 devclass_t dc; 1757 int err; 1758 1759 dc = device_get_devclass(dev); 1760 if (unit < dc->maxunit && dc->devices[unit]) 1761 return(EBUSY); 1762 err = devclass_delete_device(dc, dev); 1763 if (err) 1764 return(err); 1765 dev->unit = unit; 1766 err = devclass_add_device(dc, dev); 1767 if (err) 1768 return(err); 1769 1770 bus_data_generation_update(); 1771 return(0); 1772 } 1773 1774 /*======================================*/ 1775 /* 1776 * Access functions for device resources. 1777 */ 1778 1779 /* Supplied by config(8) in ioconf.c */ 1780 extern struct config_device config_devtab[]; 1781 extern int devtab_count; 1782 1783 /* Runtime version */ 1784 struct config_device *devtab = config_devtab; 1785 1786 static int 1787 resource_new_name(const char *name, int unit) 1788 { 1789 struct config_device *new; 1790 1791 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP, 1792 M_INTWAIT | M_ZERO); 1793 if (new == NULL) 1794 return(-1); 1795 if (devtab && devtab_count > 0) 1796 bcopy(devtab, new, devtab_count * sizeof(*new)); 1797 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT); 1798 if (new[devtab_count].name == NULL) { 1799 kfree(new, M_TEMP); 1800 return(-1); 1801 } 1802 strcpy(new[devtab_count].name, name); 1803 new[devtab_count].unit = unit; 1804 new[devtab_count].resource_count = 0; 1805 new[devtab_count].resources = NULL; 1806 if (devtab && devtab != config_devtab) 1807 kfree(devtab, M_TEMP); 1808 devtab = new; 1809 return devtab_count++; 1810 } 1811 1812 static int 1813 resource_new_resname(int j, const char *resname, resource_type type) 1814 { 1815 struct config_resource *new; 1816 int i; 1817 1818 i = devtab[j].resource_count; 1819 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO); 1820 if (new == NULL) 1821 return(-1); 1822 if (devtab[j].resources && i > 0) 1823 bcopy(devtab[j].resources, new, i * sizeof(*new)); 1824 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT); 1825 if (new[i].name == NULL) { 1826 kfree(new, M_TEMP); 1827 return(-1); 1828 } 1829 strcpy(new[i].name, resname); 1830 new[i].type = type; 1831 if (devtab[j].resources) 1832 kfree(devtab[j].resources, M_TEMP); 1833 devtab[j].resources = new; 1834 devtab[j].resource_count = i + 1; 1835 return(i); 1836 } 1837 1838 static int 1839 resource_match_string(int i, const char *resname, const char *value) 1840 { 1841 int j; 1842 struct config_resource *res; 1843 1844 for (j = 0, res = devtab[i].resources; 1845 j < devtab[i].resource_count; j++, res++) 1846 if (!strcmp(res->name, resname) 1847 && res->type == RES_STRING 1848 && !strcmp(res->u.stringval, value)) 1849 return(j); 1850 return(-1); 1851 } 1852 1853 static int 1854 resource_find(const char *name, int unit, const char *resname, 1855 struct config_resource **result) 1856 { 1857 int i, j; 1858 struct config_resource *res; 1859 1860 /* 1861 * First check specific instances, then generic. 1862 */ 1863 for (i = 0; i < devtab_count; i++) { 1864 if (devtab[i].unit < 0) 1865 continue; 1866 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1867 res = devtab[i].resources; 1868 for (j = 0; j < devtab[i].resource_count; j++, res++) 1869 if (!strcmp(res->name, resname)) { 1870 *result = res; 1871 return(0); 1872 } 1873 } 1874 } 1875 for (i = 0; i < devtab_count; i++) { 1876 if (devtab[i].unit >= 0) 1877 continue; 1878 /* XXX should this `&& devtab[i].unit == unit' be here? */ 1879 /* XXX if so, then the generic match does nothing */ 1880 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1881 res = devtab[i].resources; 1882 for (j = 0; j < devtab[i].resource_count; j++, res++) 1883 if (!strcmp(res->name, resname)) { 1884 *result = res; 1885 return(0); 1886 } 1887 } 1888 } 1889 return(ENOENT); 1890 } 1891 1892 int 1893 resource_int_value(const char *name, int unit, const char *resname, int *result) 1894 { 1895 int error; 1896 struct config_resource *res; 1897 1898 if ((error = resource_find(name, unit, resname, &res)) != 0) 1899 return(error); 1900 if (res->type != RES_INT) 1901 return(EFTYPE); 1902 *result = res->u.intval; 1903 return(0); 1904 } 1905 1906 int 1907 resource_long_value(const char *name, int unit, const char *resname, 1908 long *result) 1909 { 1910 int error; 1911 struct config_resource *res; 1912 1913 if ((error = resource_find(name, unit, resname, &res)) != 0) 1914 return(error); 1915 if (res->type != RES_LONG) 1916 return(EFTYPE); 1917 *result = res->u.longval; 1918 return(0); 1919 } 1920 1921 int 1922 resource_string_value(const char *name, int unit, const char *resname, 1923 char **result) 1924 { 1925 int error; 1926 struct config_resource *res; 1927 1928 if ((error = resource_find(name, unit, resname, &res)) != 0) 1929 return(error); 1930 if (res->type != RES_STRING) 1931 return(EFTYPE); 1932 *result = res->u.stringval; 1933 return(0); 1934 } 1935 1936 int 1937 resource_query_string(int i, const char *resname, const char *value) 1938 { 1939 if (i < 0) 1940 i = 0; 1941 else 1942 i = i + 1; 1943 for (; i < devtab_count; i++) 1944 if (resource_match_string(i, resname, value) >= 0) 1945 return(i); 1946 return(-1); 1947 } 1948 1949 int 1950 resource_locate(int i, const char *resname) 1951 { 1952 if (i < 0) 1953 i = 0; 1954 else 1955 i = i + 1; 1956 for (; i < devtab_count; i++) 1957 if (!strcmp(devtab[i].name, resname)) 1958 return(i); 1959 return(-1); 1960 } 1961 1962 int 1963 resource_count(void) 1964 { 1965 return(devtab_count); 1966 } 1967 1968 char * 1969 resource_query_name(int i) 1970 { 1971 return(devtab[i].name); 1972 } 1973 1974 int 1975 resource_query_unit(int i) 1976 { 1977 return(devtab[i].unit); 1978 } 1979 1980 static int 1981 resource_create(const char *name, int unit, const char *resname, 1982 resource_type type, struct config_resource **result) 1983 { 1984 int i, j; 1985 struct config_resource *res = NULL; 1986 1987 for (i = 0; i < devtab_count; i++) 1988 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1989 res = devtab[i].resources; 1990 break; 1991 } 1992 if (res == NULL) { 1993 i = resource_new_name(name, unit); 1994 if (i < 0) 1995 return(ENOMEM); 1996 res = devtab[i].resources; 1997 } 1998 for (j = 0; j < devtab[i].resource_count; j++, res++) 1999 if (!strcmp(res->name, resname)) { 2000 *result = res; 2001 return(0); 2002 } 2003 j = resource_new_resname(i, resname, type); 2004 if (j < 0) 2005 return(ENOMEM); 2006 res = &devtab[i].resources[j]; 2007 *result = res; 2008 return(0); 2009 } 2010 2011 int 2012 resource_set_int(const char *name, int unit, const char *resname, int value) 2013 { 2014 int error; 2015 struct config_resource *res; 2016 2017 error = resource_create(name, unit, resname, RES_INT, &res); 2018 if (error) 2019 return(error); 2020 if (res->type != RES_INT) 2021 return(EFTYPE); 2022 res->u.intval = value; 2023 return(0); 2024 } 2025 2026 int 2027 resource_set_long(const char *name, int unit, const char *resname, long value) 2028 { 2029 int error; 2030 struct config_resource *res; 2031 2032 error = resource_create(name, unit, resname, RES_LONG, &res); 2033 if (error) 2034 return(error); 2035 if (res->type != RES_LONG) 2036 return(EFTYPE); 2037 res->u.longval = value; 2038 return(0); 2039 } 2040 2041 int 2042 resource_set_string(const char *name, int unit, const char *resname, 2043 const char *value) 2044 { 2045 int error; 2046 struct config_resource *res; 2047 2048 error = resource_create(name, unit, resname, RES_STRING, &res); 2049 if (error) 2050 return(error); 2051 if (res->type != RES_STRING) 2052 return(EFTYPE); 2053 if (res->u.stringval) 2054 kfree(res->u.stringval, M_TEMP); 2055 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT); 2056 if (res->u.stringval == NULL) 2057 return(ENOMEM); 2058 strcpy(res->u.stringval, value); 2059 return(0); 2060 } 2061 2062 static void 2063 resource_cfgload(void *dummy __unused) 2064 { 2065 struct config_resource *res, *cfgres; 2066 int i, j; 2067 int error; 2068 char *name, *resname; 2069 int unit; 2070 resource_type type; 2071 char *stringval; 2072 int config_devtab_count; 2073 2074 config_devtab_count = devtab_count; 2075 devtab = NULL; 2076 devtab_count = 0; 2077 2078 for (i = 0; i < config_devtab_count; i++) { 2079 name = config_devtab[i].name; 2080 unit = config_devtab[i].unit; 2081 2082 for (j = 0; j < config_devtab[i].resource_count; j++) { 2083 cfgres = config_devtab[i].resources; 2084 resname = cfgres[j].name; 2085 type = cfgres[j].type; 2086 error = resource_create(name, unit, resname, type, 2087 &res); 2088 if (error) { 2089 kprintf("create resource %s%d: error %d\n", 2090 name, unit, error); 2091 continue; 2092 } 2093 if (res->type != type) { 2094 kprintf("type mismatch %s%d: %d != %d\n", 2095 name, unit, res->type, type); 2096 continue; 2097 } 2098 switch (type) { 2099 case RES_INT: 2100 res->u.intval = cfgres[j].u.intval; 2101 break; 2102 case RES_LONG: 2103 res->u.longval = cfgres[j].u.longval; 2104 break; 2105 case RES_STRING: 2106 if (res->u.stringval) 2107 kfree(res->u.stringval, M_TEMP); 2108 stringval = cfgres[j].u.stringval; 2109 res->u.stringval = kmalloc(strlen(stringval) + 1, 2110 M_TEMP, M_INTWAIT); 2111 if (res->u.stringval == NULL) 2112 break; 2113 strcpy(res->u.stringval, stringval); 2114 break; 2115 default: 2116 panic("unknown resource type %d", type); 2117 } 2118 } 2119 } 2120 } 2121 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0) 2122 2123 2124 /*======================================*/ 2125 /* 2126 * Some useful method implementations to make life easier for bus drivers. 2127 */ 2128 2129 void 2130 resource_list_init(struct resource_list *rl) 2131 { 2132 SLIST_INIT(rl); 2133 } 2134 2135 void 2136 resource_list_free(struct resource_list *rl) 2137 { 2138 struct resource_list_entry *rle; 2139 2140 while ((rle = SLIST_FIRST(rl)) != NULL) { 2141 if (rle->res) 2142 panic("resource_list_free: resource entry is busy"); 2143 SLIST_REMOVE_HEAD(rl, link); 2144 kfree(rle, M_BUS); 2145 } 2146 } 2147 2148 void 2149 resource_list_add(struct resource_list *rl, 2150 int type, int rid, 2151 u_long start, u_long end, u_long count) 2152 { 2153 struct resource_list_entry *rle; 2154 2155 rle = resource_list_find(rl, type, rid); 2156 if (rle == NULL) { 2157 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS, 2158 M_INTWAIT); 2159 if (!rle) 2160 panic("resource_list_add: can't record entry"); 2161 SLIST_INSERT_HEAD(rl, rle, link); 2162 rle->type = type; 2163 rle->rid = rid; 2164 rle->res = NULL; 2165 } 2166 2167 if (rle->res) 2168 panic("resource_list_add: resource entry is busy"); 2169 2170 rle->start = start; 2171 rle->end = end; 2172 rle->count = count; 2173 } 2174 2175 struct resource_list_entry* 2176 resource_list_find(struct resource_list *rl, 2177 int type, int rid) 2178 { 2179 struct resource_list_entry *rle; 2180 2181 SLIST_FOREACH(rle, rl, link) 2182 if (rle->type == type && rle->rid == rid) 2183 return(rle); 2184 return(NULL); 2185 } 2186 2187 void 2188 resource_list_delete(struct resource_list *rl, 2189 int type, int rid) 2190 { 2191 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 2192 2193 if (rle) { 2194 if (rle->res != NULL) 2195 panic("resource_list_delete: resource has not been released"); 2196 SLIST_REMOVE(rl, rle, resource_list_entry, link); 2197 kfree(rle, M_BUS); 2198 } 2199 } 2200 2201 struct resource * 2202 resource_list_alloc(struct resource_list *rl, 2203 device_t bus, device_t child, 2204 int type, int *rid, 2205 u_long start, u_long end, 2206 u_long count, u_int flags) 2207 { 2208 struct resource_list_entry *rle = 0; 2209 int passthrough = (device_get_parent(child) != bus); 2210 int isdefault = (start == 0UL && end == ~0UL); 2211 2212 if (passthrough) { 2213 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2214 type, rid, 2215 start, end, count, flags)); 2216 } 2217 2218 rle = resource_list_find(rl, type, *rid); 2219 2220 if (!rle) 2221 return(0); /* no resource of that type/rid */ 2222 2223 if (rle->res) 2224 panic("resource_list_alloc: resource entry is busy"); 2225 2226 if (isdefault) { 2227 start = rle->start; 2228 count = max(count, rle->count); 2229 end = max(rle->end, start + count - 1); 2230 } 2231 2232 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2233 type, rid, start, end, count, flags); 2234 2235 /* 2236 * Record the new range. 2237 */ 2238 if (rle->res) { 2239 rle->start = rman_get_start(rle->res); 2240 rle->end = rman_get_end(rle->res); 2241 rle->count = count; 2242 } 2243 2244 return(rle->res); 2245 } 2246 2247 int 2248 resource_list_release(struct resource_list *rl, 2249 device_t bus, device_t child, 2250 int type, int rid, struct resource *res) 2251 { 2252 struct resource_list_entry *rle = 0; 2253 int passthrough = (device_get_parent(child) != bus); 2254 int error; 2255 2256 if (passthrough) { 2257 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2258 type, rid, res)); 2259 } 2260 2261 rle = resource_list_find(rl, type, rid); 2262 2263 if (!rle) 2264 panic("resource_list_release: can't find resource"); 2265 if (!rle->res) 2266 panic("resource_list_release: resource entry is not busy"); 2267 2268 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2269 type, rid, res); 2270 if (error) 2271 return(error); 2272 2273 rle->res = NULL; 2274 return(0); 2275 } 2276 2277 int 2278 resource_list_print_type(struct resource_list *rl, const char *name, int type, 2279 const char *format) 2280 { 2281 struct resource_list_entry *rle; 2282 int printed, retval; 2283 2284 printed = 0; 2285 retval = 0; 2286 /* Yes, this is kinda cheating */ 2287 SLIST_FOREACH(rle, rl, link) { 2288 if (rle->type == type) { 2289 if (printed == 0) 2290 retval += kprintf(" %s ", name); 2291 else 2292 retval += kprintf(","); 2293 printed++; 2294 retval += kprintf(format, rle->start); 2295 if (rle->count > 1) { 2296 retval += kprintf("-"); 2297 retval += kprintf(format, rle->start + 2298 rle->count - 1); 2299 } 2300 } 2301 } 2302 return(retval); 2303 } 2304 2305 /* 2306 * Generic driver/device identify functions. These will install a device 2307 * rendezvous point under the parent using the same name as the driver 2308 * name, which will at a later time be probed and attached. 2309 * 2310 * These functions are used when the parent does not 'scan' its bus for 2311 * matching devices, or for the particular devices using these functions, 2312 * or when the device is a pseudo or synthesized device (such as can be 2313 * found under firewire and ppbus). 2314 */ 2315 int 2316 bus_generic_identify(driver_t *driver, device_t parent) 2317 { 2318 if (parent->state == DS_ATTACHED) 2319 return (0); 2320 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1); 2321 return (0); 2322 } 2323 2324 int 2325 bus_generic_identify_sameunit(driver_t *driver, device_t parent) 2326 { 2327 if (parent->state == DS_ATTACHED) 2328 return (0); 2329 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent)); 2330 return (0); 2331 } 2332 2333 /* 2334 * Call DEVICE_IDENTIFY for each driver. 2335 */ 2336 int 2337 bus_generic_probe(device_t dev) 2338 { 2339 devclass_t dc = dev->devclass; 2340 driverlink_t dl; 2341 2342 TAILQ_FOREACH(dl, &dc->drivers, link) { 2343 DEVICE_IDENTIFY(dl->driver, dev); 2344 } 2345 2346 return(0); 2347 } 2348 2349 /* 2350 * This is an aweful hack due to the isa bus and autoconf code not 2351 * probing the ISA devices until after everything else has configured. 2352 * The ISA bus did a dummy attach long ago so we have to set it back 2353 * to an earlier state so the probe thinks its the initial probe and 2354 * not a bus rescan. 2355 * 2356 * XXX remove by properly defering the ISA bus scan. 2357 */ 2358 int 2359 bus_generic_probe_hack(device_t dev) 2360 { 2361 if (dev->state == DS_ATTACHED) { 2362 dev->state = DS_ALIVE; 2363 bus_generic_probe(dev); 2364 dev->state = DS_ATTACHED; 2365 } 2366 return (0); 2367 } 2368 2369 int 2370 bus_generic_attach(device_t dev) 2371 { 2372 device_t child; 2373 2374 TAILQ_FOREACH(child, &dev->children, link) { 2375 device_probe_and_attach(child); 2376 } 2377 2378 return(0); 2379 } 2380 2381 int 2382 bus_generic_detach(device_t dev) 2383 { 2384 device_t child; 2385 int error; 2386 2387 if (dev->state != DS_ATTACHED) 2388 return(EBUSY); 2389 2390 TAILQ_FOREACH(child, &dev->children, link) 2391 if ((error = device_detach(child)) != 0) 2392 return(error); 2393 2394 return 0; 2395 } 2396 2397 int 2398 bus_generic_shutdown(device_t dev) 2399 { 2400 device_t child; 2401 2402 TAILQ_FOREACH(child, &dev->children, link) 2403 device_shutdown(child); 2404 2405 return(0); 2406 } 2407 2408 int 2409 bus_generic_suspend(device_t dev) 2410 { 2411 int error; 2412 device_t child, child2; 2413 2414 TAILQ_FOREACH(child, &dev->children, link) { 2415 error = DEVICE_SUSPEND(child); 2416 if (error) { 2417 for (child2 = TAILQ_FIRST(&dev->children); 2418 child2 && child2 != child; 2419 child2 = TAILQ_NEXT(child2, link)) 2420 DEVICE_RESUME(child2); 2421 return(error); 2422 } 2423 } 2424 return(0); 2425 } 2426 2427 int 2428 bus_generic_resume(device_t dev) 2429 { 2430 device_t child; 2431 2432 TAILQ_FOREACH(child, &dev->children, link) 2433 DEVICE_RESUME(child); 2434 /* if resume fails, there's nothing we can usefully do... */ 2435 2436 return(0); 2437 } 2438 2439 int 2440 bus_print_child_header(device_t dev, device_t child) 2441 { 2442 int retval = 0; 2443 2444 if (device_get_desc(child)) 2445 retval += device_printf(child, "<%s>", device_get_desc(child)); 2446 else 2447 retval += kprintf("%s", device_get_nameunit(child)); 2448 if (bootverbose) { 2449 if (child->state != DS_ATTACHED) 2450 kprintf(" [tentative]"); 2451 else 2452 kprintf(" [attached!]"); 2453 } 2454 return(retval); 2455 } 2456 2457 int 2458 bus_print_child_footer(device_t dev, device_t child) 2459 { 2460 return(kprintf(" on %s\n", device_get_nameunit(dev))); 2461 } 2462 2463 device_t 2464 bus_generic_add_child(device_t dev, device_t child, int order, 2465 const char *name, int unit) 2466 { 2467 if (dev->parent) 2468 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit); 2469 else 2470 dev = device_add_child_ordered(child, order, name, unit); 2471 return(dev); 2472 2473 } 2474 2475 int 2476 bus_generic_print_child(device_t dev, device_t child) 2477 { 2478 int retval = 0; 2479 2480 retval += bus_print_child_header(dev, child); 2481 retval += bus_print_child_footer(dev, child); 2482 2483 return(retval); 2484 } 2485 2486 int 2487 bus_generic_read_ivar(device_t dev, device_t child, int index, 2488 uintptr_t * result) 2489 { 2490 int error; 2491 2492 if (dev->parent) 2493 error = BUS_READ_IVAR(dev->parent, child, index, result); 2494 else 2495 error = ENOENT; 2496 return (error); 2497 } 2498 2499 int 2500 bus_generic_write_ivar(device_t dev, device_t child, int index, 2501 uintptr_t value) 2502 { 2503 int error; 2504 2505 if (dev->parent) 2506 error = BUS_WRITE_IVAR(dev->parent, child, index, value); 2507 else 2508 error = ENOENT; 2509 return (error); 2510 } 2511 2512 /* 2513 * Resource list are used for iterations, do not recurse. 2514 */ 2515 struct resource_list * 2516 bus_generic_get_resource_list(device_t dev, device_t child) 2517 { 2518 return (NULL); 2519 } 2520 2521 void 2522 bus_generic_driver_added(device_t dev, driver_t *driver) 2523 { 2524 device_t child; 2525 2526 DEVICE_IDENTIFY(driver, dev); 2527 TAILQ_FOREACH(child, &dev->children, link) { 2528 if (child->state == DS_NOTPRESENT) 2529 device_probe_and_attach(child); 2530 } 2531 } 2532 2533 int 2534 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 2535 int flags, driver_intr_t *intr, void *arg, 2536 void **cookiep, lwkt_serialize_t serializer) 2537 { 2538 /* Propagate up the bus hierarchy until someone handles it. */ 2539 if (dev->parent) 2540 return(BUS_SETUP_INTR(dev->parent, child, irq, flags, 2541 intr, arg, cookiep, serializer)); 2542 else 2543 return(EINVAL); 2544 } 2545 2546 int 2547 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 2548 void *cookie) 2549 { 2550 /* Propagate up the bus hierarchy until someone handles it. */ 2551 if (dev->parent) 2552 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 2553 else 2554 return(EINVAL); 2555 } 2556 2557 int 2558 bus_generic_disable_intr(device_t dev, device_t child, void *cookie) 2559 { 2560 if (dev->parent) 2561 return(BUS_DISABLE_INTR(dev->parent, child, cookie)); 2562 else 2563 return(0); 2564 } 2565 2566 void 2567 bus_generic_enable_intr(device_t dev, device_t child, void *cookie) 2568 { 2569 if (dev->parent) 2570 BUS_ENABLE_INTR(dev->parent, child, cookie); 2571 } 2572 2573 int 2574 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig, 2575 enum intr_polarity pol) 2576 { 2577 /* Propagate up the bus hierarchy until someone handles it. */ 2578 if (dev->parent) 2579 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol)); 2580 else 2581 return(EINVAL); 2582 } 2583 2584 struct resource * 2585 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 2586 u_long start, u_long end, u_long count, u_int flags) 2587 { 2588 /* Propagate up the bus hierarchy until someone handles it. */ 2589 if (dev->parent) 2590 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 2591 start, end, count, flags)); 2592 else 2593 return(NULL); 2594 } 2595 2596 int 2597 bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 2598 struct resource *r) 2599 { 2600 /* Propagate up the bus hierarchy until someone handles it. */ 2601 if (dev->parent) 2602 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r)); 2603 else 2604 return(EINVAL); 2605 } 2606 2607 int 2608 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 2609 struct resource *r) 2610 { 2611 /* Propagate up the bus hierarchy until someone handles it. */ 2612 if (dev->parent) 2613 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r)); 2614 else 2615 return(EINVAL); 2616 } 2617 2618 int 2619 bus_generic_deactivate_resource(device_t dev, device_t child, int type, 2620 int rid, struct resource *r) 2621 { 2622 /* Propagate up the bus hierarchy until someone handles it. */ 2623 if (dev->parent) 2624 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 2625 r)); 2626 else 2627 return(EINVAL); 2628 } 2629 2630 int 2631 bus_generic_get_resource(device_t dev, device_t child, int type, int rid, 2632 u_long *startp, u_long *countp) 2633 { 2634 int error; 2635 2636 error = ENOENT; 2637 if (dev->parent) { 2638 error = BUS_GET_RESOURCE(dev->parent, child, type, rid, 2639 startp, countp); 2640 } 2641 return (error); 2642 } 2643 2644 int 2645 bus_generic_set_resource(device_t dev, device_t child, int type, int rid, 2646 u_long start, u_long count) 2647 { 2648 int error; 2649 2650 error = EINVAL; 2651 if (dev->parent) { 2652 error = BUS_SET_RESOURCE(dev->parent, child, type, rid, 2653 start, count); 2654 } 2655 return (error); 2656 } 2657 2658 void 2659 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid) 2660 { 2661 if (dev->parent) 2662 BUS_DELETE_RESOURCE(dev, child, type, rid); 2663 } 2664 2665 int 2666 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 2667 u_long *startp, u_long *countp) 2668 { 2669 struct resource_list *rl = NULL; 2670 struct resource_list_entry *rle = NULL; 2671 2672 rl = BUS_GET_RESOURCE_LIST(dev, child); 2673 if (!rl) 2674 return(EINVAL); 2675 2676 rle = resource_list_find(rl, type, rid); 2677 if (!rle) 2678 return(ENOENT); 2679 2680 if (startp) 2681 *startp = rle->start; 2682 if (countp) 2683 *countp = rle->count; 2684 2685 return(0); 2686 } 2687 2688 int 2689 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 2690 u_long start, u_long count) 2691 { 2692 struct resource_list *rl = NULL; 2693 2694 rl = BUS_GET_RESOURCE_LIST(dev, child); 2695 if (!rl) 2696 return(EINVAL); 2697 2698 resource_list_add(rl, type, rid, start, (start + count - 1), count); 2699 2700 return(0); 2701 } 2702 2703 void 2704 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 2705 { 2706 struct resource_list *rl = NULL; 2707 2708 rl = BUS_GET_RESOURCE_LIST(dev, child); 2709 if (!rl) 2710 return; 2711 2712 resource_list_delete(rl, type, rid); 2713 } 2714 2715 int 2716 bus_generic_rl_release_resource(device_t dev, device_t child, int type, 2717 int rid, struct resource *r) 2718 { 2719 struct resource_list *rl = NULL; 2720 2721 rl = BUS_GET_RESOURCE_LIST(dev, child); 2722 if (!rl) 2723 return(EINVAL); 2724 2725 return(resource_list_release(rl, dev, child, type, rid, r)); 2726 } 2727 2728 struct resource * 2729 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 2730 int *rid, u_long start, u_long end, u_long count, u_int flags) 2731 { 2732 struct resource_list *rl = NULL; 2733 2734 rl = BUS_GET_RESOURCE_LIST(dev, child); 2735 if (!rl) 2736 return(NULL); 2737 2738 return(resource_list_alloc(rl, dev, child, type, rid, 2739 start, end, count, flags)); 2740 } 2741 2742 int 2743 bus_generic_child_present(device_t bus, device_t child) 2744 { 2745 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus)); 2746 } 2747 2748 2749 /* 2750 * Some convenience functions to make it easier for drivers to use the 2751 * resource-management functions. All these really do is hide the 2752 * indirection through the parent's method table, making for slightly 2753 * less-wordy code. In the future, it might make sense for this code 2754 * to maintain some sort of a list of resources allocated by each device. 2755 */ 2756 int 2757 bus_alloc_resources(device_t dev, struct resource_spec *rs, 2758 struct resource **res) 2759 { 2760 int i; 2761 2762 for (i = 0; rs[i].type != -1; i++) 2763 res[i] = NULL; 2764 for (i = 0; rs[i].type != -1; i++) { 2765 res[i] = bus_alloc_resource_any(dev, 2766 rs[i].type, &rs[i].rid, rs[i].flags); 2767 if (res[i] == NULL) { 2768 bus_release_resources(dev, rs, res); 2769 return (ENXIO); 2770 } 2771 } 2772 return (0); 2773 } 2774 2775 void 2776 bus_release_resources(device_t dev, const struct resource_spec *rs, 2777 struct resource **res) 2778 { 2779 int i; 2780 2781 for (i = 0; rs[i].type != -1; i++) 2782 if (res[i] != NULL) { 2783 bus_release_resource( 2784 dev, rs[i].type, rs[i].rid, res[i]); 2785 res[i] = NULL; 2786 } 2787 } 2788 2789 struct resource * 2790 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 2791 u_long count, u_int flags) 2792 { 2793 if (dev->parent == 0) 2794 return(0); 2795 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 2796 count, flags)); 2797 } 2798 2799 int 2800 bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 2801 { 2802 if (dev->parent == 0) 2803 return(EINVAL); 2804 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2805 } 2806 2807 int 2808 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 2809 { 2810 if (dev->parent == 0) 2811 return(EINVAL); 2812 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2813 } 2814 2815 int 2816 bus_release_resource(device_t dev, int type, int rid, struct resource *r) 2817 { 2818 if (dev->parent == 0) 2819 return(EINVAL); 2820 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 2821 } 2822 2823 int 2824 bus_setup_intr(device_t dev, struct resource *r, int flags, 2825 driver_intr_t handler, void *arg, 2826 void **cookiep, lwkt_serialize_t serializer) 2827 { 2828 if (dev->parent == 0) 2829 return(EINVAL); 2830 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg, 2831 cookiep, serializer)); 2832 } 2833 2834 int 2835 bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 2836 { 2837 if (dev->parent == 0) 2838 return(EINVAL); 2839 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 2840 } 2841 2842 void 2843 bus_enable_intr(device_t dev, void *cookie) 2844 { 2845 if (dev->parent) 2846 BUS_ENABLE_INTR(dev->parent, dev, cookie); 2847 } 2848 2849 int 2850 bus_disable_intr(device_t dev, void *cookie) 2851 { 2852 if (dev->parent) 2853 return(BUS_DISABLE_INTR(dev->parent, dev, cookie)); 2854 else 2855 return(0); 2856 } 2857 2858 int 2859 bus_set_resource(device_t dev, int type, int rid, 2860 u_long start, u_long count) 2861 { 2862 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 2863 start, count)); 2864 } 2865 2866 int 2867 bus_get_resource(device_t dev, int type, int rid, 2868 u_long *startp, u_long *countp) 2869 { 2870 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2871 startp, countp)); 2872 } 2873 2874 u_long 2875 bus_get_resource_start(device_t dev, int type, int rid) 2876 { 2877 u_long start, count; 2878 int error; 2879 2880 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2881 &start, &count); 2882 if (error) 2883 return(0); 2884 return(start); 2885 } 2886 2887 u_long 2888 bus_get_resource_count(device_t dev, int type, int rid) 2889 { 2890 u_long start, count; 2891 int error; 2892 2893 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2894 &start, &count); 2895 if (error) 2896 return(0); 2897 return(count); 2898 } 2899 2900 void 2901 bus_delete_resource(device_t dev, int type, int rid) 2902 { 2903 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 2904 } 2905 2906 int 2907 bus_child_present(device_t child) 2908 { 2909 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 2910 } 2911 2912 int 2913 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 2914 { 2915 device_t parent; 2916 2917 parent = device_get_parent(child); 2918 if (parent == NULL) { 2919 *buf = '\0'; 2920 return (0); 2921 } 2922 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 2923 } 2924 2925 int 2926 bus_child_location_str(device_t child, char *buf, size_t buflen) 2927 { 2928 device_t parent; 2929 2930 parent = device_get_parent(child); 2931 if (parent == NULL) { 2932 *buf = '\0'; 2933 return (0); 2934 } 2935 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 2936 } 2937 2938 static int 2939 root_print_child(device_t dev, device_t child) 2940 { 2941 return(0); 2942 } 2943 2944 static int 2945 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 2946 void **cookiep, lwkt_serialize_t serializer) 2947 { 2948 /* 2949 * If an interrupt mapping gets to here something bad has happened. 2950 */ 2951 panic("root_setup_intr"); 2952 } 2953 2954 /* 2955 * If we get here, assume that the device is permanant and really is 2956 * present in the system. Removable bus drivers are expected to intercept 2957 * this call long before it gets here. We return -1 so that drivers that 2958 * really care can check vs -1 or some ERRNO returned higher in the food 2959 * chain. 2960 */ 2961 static int 2962 root_child_present(device_t dev, device_t child) 2963 { 2964 return(-1); 2965 } 2966 2967 /* 2968 * XXX NOTE! other defaults may be set in bus_if.m 2969 */ 2970 static kobj_method_t root_methods[] = { 2971 /* Device interface */ 2972 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 2973 KOBJMETHOD(device_suspend, bus_generic_suspend), 2974 KOBJMETHOD(device_resume, bus_generic_resume), 2975 2976 /* Bus interface */ 2977 KOBJMETHOD(bus_add_child, bus_generic_add_child), 2978 KOBJMETHOD(bus_print_child, root_print_child), 2979 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 2980 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 2981 KOBJMETHOD(bus_setup_intr, root_setup_intr), 2982 KOBJMETHOD(bus_child_present, root_child_present), 2983 2984 { 0, 0 } 2985 }; 2986 2987 static driver_t root_driver = { 2988 "root", 2989 root_methods, 2990 1, /* no softc */ 2991 }; 2992 2993 device_t root_bus; 2994 devclass_t root_devclass; 2995 2996 static int 2997 root_bus_module_handler(module_t mod, int what, void* arg) 2998 { 2999 switch (what) { 3000 case MOD_LOAD: 3001 TAILQ_INIT(&bus_data_devices); 3002 root_bus = make_device(NULL, "root", 0); 3003 root_bus->desc = "System root bus"; 3004 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 3005 root_bus->driver = &root_driver; 3006 root_bus->state = DS_ALIVE; 3007 root_devclass = devclass_find_internal("root", NULL, FALSE); 3008 devinit(); 3009 return(0); 3010 3011 case MOD_SHUTDOWN: 3012 device_shutdown(root_bus); 3013 return(0); 3014 default: 3015 return(0); 3016 } 3017 } 3018 3019 static moduledata_t root_bus_mod = { 3020 "rootbus", 3021 root_bus_module_handler, 3022 0 3023 }; 3024 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 3025 3026 void 3027 root_bus_configure(void) 3028 { 3029 int warncount; 3030 device_t dev; 3031 3032 PDEBUG((".")); 3033 3034 /* 3035 * handle device_identify based device attachments to the root_bus 3036 * (typically nexus). 3037 */ 3038 bus_generic_probe(root_bus); 3039 3040 /* 3041 * Probe and attach the devices under root_bus. 3042 */ 3043 TAILQ_FOREACH(dev, &root_bus->children, link) { 3044 device_probe_and_attach(dev); 3045 } 3046 3047 /* 3048 * Wait for all asynchronous attaches to complete. If we don't 3049 * our legacy ISA bus scan could steal device unit numbers or 3050 * even I/O ports. 3051 */ 3052 warncount = 10; 3053 if (numasyncthreads) 3054 kprintf("Waiting for async drivers to attach\n"); 3055 while (numasyncthreads > 0) { 3056 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK) 3057 --warncount; 3058 if (warncount == 0) { 3059 kprintf("Warning: Still waiting for %d " 3060 "drivers to attach\n", numasyncthreads); 3061 } else if (warncount == -30) { 3062 kprintf("Giving up on %d drivers\n", numasyncthreads); 3063 break; 3064 } 3065 } 3066 root_bus->state = DS_ATTACHED; 3067 } 3068 3069 int 3070 driver_module_handler(module_t mod, int what, void *arg) 3071 { 3072 int error; 3073 struct driver_module_data *dmd; 3074 devclass_t bus_devclass; 3075 kobj_class_t driver; 3076 const char *parentname; 3077 3078 dmd = (struct driver_module_data *)arg; 3079 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 3080 error = 0; 3081 3082 switch (what) { 3083 case MOD_LOAD: 3084 if (dmd->dmd_chainevh) 3085 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3086 3087 driver = dmd->dmd_driver; 3088 PDEBUG(("Loading module: driver %s on bus %s", 3089 DRIVERNAME(driver), dmd->dmd_busname)); 3090 3091 /* 3092 * If the driver has any base classes, make the 3093 * devclass inherit from the devclass of the driver's 3094 * first base class. This will allow the system to 3095 * search for drivers in both devclasses for children 3096 * of a device using this driver. 3097 */ 3098 if (driver->baseclasses) 3099 parentname = driver->baseclasses[0]->name; 3100 else 3101 parentname = NULL; 3102 *dmd->dmd_devclass = devclass_find_internal(driver->name, 3103 parentname, TRUE); 3104 3105 error = devclass_add_driver(bus_devclass, driver); 3106 if (error) 3107 break; 3108 break; 3109 3110 case MOD_UNLOAD: 3111 PDEBUG(("Unloading module: driver %s from bus %s", 3112 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname)); 3113 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver); 3114 3115 if (!error && dmd->dmd_chainevh) 3116 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3117 break; 3118 } 3119 3120 return (error); 3121 } 3122 3123 #ifdef BUS_DEBUG 3124 3125 /* 3126 * The _short versions avoid iteration by not calling anything that prints 3127 * more than oneliners. I love oneliners. 3128 */ 3129 3130 static void 3131 print_device_short(device_t dev, int indent) 3132 { 3133 if (!dev) 3134 return; 3135 3136 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 3137 dev->unit, dev->desc, 3138 (dev->parent? "":"no "), 3139 (TAILQ_EMPTY(&dev->children)? "no ":""), 3140 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 3141 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 3142 (dev->flags&DF_WILDCARD? "wildcard,":""), 3143 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 3144 (dev->ivars? "":"no "), 3145 (dev->softc? "":"no "), 3146 dev->busy)); 3147 } 3148 3149 static void 3150 print_device(device_t dev, int indent) 3151 { 3152 if (!dev) 3153 return; 3154 3155 print_device_short(dev, indent); 3156 3157 indentprintf(("Parent:\n")); 3158 print_device_short(dev->parent, indent+1); 3159 indentprintf(("Driver:\n")); 3160 print_driver_short(dev->driver, indent+1); 3161 indentprintf(("Devclass:\n")); 3162 print_devclass_short(dev->devclass, indent+1); 3163 } 3164 3165 /* 3166 * Print the device and all its children (indented). 3167 */ 3168 void 3169 print_device_tree_short(device_t dev, int indent) 3170 { 3171 device_t child; 3172 3173 if (!dev) 3174 return; 3175 3176 print_device_short(dev, indent); 3177 3178 TAILQ_FOREACH(child, &dev->children, link) 3179 print_device_tree_short(child, indent+1); 3180 } 3181 3182 /* 3183 * Print the device and all its children (indented). 3184 */ 3185 void 3186 print_device_tree(device_t dev, int indent) 3187 { 3188 device_t child; 3189 3190 if (!dev) 3191 return; 3192 3193 print_device(dev, indent); 3194 3195 TAILQ_FOREACH(child, &dev->children, link) 3196 print_device_tree(child, indent+1); 3197 } 3198 3199 static void 3200 print_driver_short(driver_t *driver, int indent) 3201 { 3202 if (!driver) 3203 return; 3204 3205 indentprintf(("driver %s: softc size = %zu\n", 3206 driver->name, driver->size)); 3207 } 3208 3209 static void 3210 print_driver(driver_t *driver, int indent) 3211 { 3212 if (!driver) 3213 return; 3214 3215 print_driver_short(driver, indent); 3216 } 3217 3218 3219 static void 3220 print_driver_list(driver_list_t drivers, int indent) 3221 { 3222 driverlink_t driver; 3223 3224 TAILQ_FOREACH(driver, &drivers, link) 3225 print_driver(driver->driver, indent); 3226 } 3227 3228 static void 3229 print_devclass_short(devclass_t dc, int indent) 3230 { 3231 if (!dc) 3232 return; 3233 3234 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 3235 } 3236 3237 static void 3238 print_devclass(devclass_t dc, int indent) 3239 { 3240 int i; 3241 3242 if (!dc) 3243 return; 3244 3245 print_devclass_short(dc, indent); 3246 indentprintf(("Drivers:\n")); 3247 print_driver_list(dc->drivers, indent+1); 3248 3249 indentprintf(("Devices:\n")); 3250 for (i = 0; i < dc->maxunit; i++) 3251 if (dc->devices[i]) 3252 print_device(dc->devices[i], indent+1); 3253 } 3254 3255 void 3256 print_devclass_list_short(void) 3257 { 3258 devclass_t dc; 3259 3260 kprintf("Short listing of devclasses, drivers & devices:\n"); 3261 TAILQ_FOREACH(dc, &devclasses, link) { 3262 print_devclass_short(dc, 0); 3263 } 3264 } 3265 3266 void 3267 print_devclass_list(void) 3268 { 3269 devclass_t dc; 3270 3271 kprintf("Full listing of devclasses, drivers & devices:\n"); 3272 TAILQ_FOREACH(dc, &devclasses, link) { 3273 print_devclass(dc, 0); 3274 } 3275 } 3276 3277 #endif 3278 3279 /* 3280 * Check to see if a device is disabled via a disabled hint. 3281 */ 3282 int 3283 resource_disabled(const char *name, int unit) 3284 { 3285 int error, value; 3286 3287 error = resource_int_value(name, unit, "disabled", &value); 3288 if (error) 3289 return(0); 3290 return(value); 3291 } 3292 3293 /* 3294 * User-space access to the device tree. 3295 * 3296 * We implement a small set of nodes: 3297 * 3298 * hw.bus Single integer read method to obtain the 3299 * current generation count. 3300 * hw.bus.devices Reads the entire device tree in flat space. 3301 * hw.bus.rman Resource manager interface 3302 * 3303 * We might like to add the ability to scan devclasses and/or drivers to 3304 * determine what else is currently loaded/available. 3305 */ 3306 3307 static int 3308 sysctl_bus(SYSCTL_HANDLER_ARGS) 3309 { 3310 struct u_businfo ubus; 3311 3312 ubus.ub_version = BUS_USER_VERSION; 3313 ubus.ub_generation = bus_data_generation; 3314 3315 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 3316 } 3317 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 3318 "bus-related data"); 3319 3320 static int 3321 sysctl_devices(SYSCTL_HANDLER_ARGS) 3322 { 3323 int *name = (int *)arg1; 3324 u_int namelen = arg2; 3325 int index; 3326 struct device *dev; 3327 struct u_device udev; /* XXX this is a bit big */ 3328 int error; 3329 3330 if (namelen != 2) 3331 return (EINVAL); 3332 3333 if (bus_data_generation_check(name[0])) 3334 return (EINVAL); 3335 3336 index = name[1]; 3337 3338 /* 3339 * Scan the list of devices, looking for the requested index. 3340 */ 3341 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 3342 if (index-- == 0) 3343 break; 3344 } 3345 if (dev == NULL) 3346 return (ENOENT); 3347 3348 /* 3349 * Populate the return array. 3350 */ 3351 bzero(&udev, sizeof(udev)); 3352 udev.dv_handle = (uintptr_t)dev; 3353 udev.dv_parent = (uintptr_t)dev->parent; 3354 if (dev->nameunit != NULL) 3355 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name)); 3356 if (dev->desc != NULL) 3357 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc)); 3358 if (dev->driver != NULL && dev->driver->name != NULL) 3359 strlcpy(udev.dv_drivername, dev->driver->name, 3360 sizeof(udev.dv_drivername)); 3361 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo)); 3362 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location)); 3363 udev.dv_devflags = dev->devflags; 3364 udev.dv_flags = dev->flags; 3365 udev.dv_state = dev->state; 3366 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 3367 return (error); 3368 } 3369 3370 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 3371 "system device tree"); 3372 3373 int 3374 bus_data_generation_check(int generation) 3375 { 3376 if (generation != bus_data_generation) 3377 return (1); 3378 3379 /* XXX generate optimised lists here? */ 3380 return (0); 3381 } 3382 3383 void 3384 bus_data_generation_update(void) 3385 { 3386 bus_data_generation++; 3387 } 3388