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 610 /* 611 * If a parent class is specified, then set that as our parent so 612 * that this devclass will support drivers for the parent class as 613 * well. If the parent class has the same name don't do this though 614 * as it creates a cycle that can trigger an infinite loop in 615 * device_probe_child() if a device exists for which there is no 616 * suitable driver. 617 */ 618 if (parentname && dc && !dc->parent && 619 strcmp(classname, parentname) != 0) 620 dc->parent = devclass_find_internal(parentname, NULL, FALSE); 621 622 return(dc); 623 } 624 625 devclass_t 626 devclass_create(const char *classname) 627 { 628 return(devclass_find_internal(classname, NULL, TRUE)); 629 } 630 631 devclass_t 632 devclass_find(const char *classname) 633 { 634 return(devclass_find_internal(classname, NULL, FALSE)); 635 } 636 637 device_t 638 devclass_find_unit(const char *classname, int unit) 639 { 640 devclass_t dc; 641 642 if ((dc = devclass_find(classname)) != NULL) 643 return(devclass_get_device(dc, unit)); 644 return (NULL); 645 } 646 647 int 648 devclass_add_driver(devclass_t dc, driver_t *driver) 649 { 650 driverlink_t dl; 651 device_t dev; 652 int i; 653 654 PDEBUG(("%s", DRIVERNAME(driver))); 655 656 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO); 657 if (!dl) 658 return(ENOMEM); 659 660 /* 661 * Compile the driver's methods. Also increase the reference count 662 * so that the class doesn't get freed when the last instance 663 * goes. This means we can safely use static methods and avoids a 664 * double-free in devclass_delete_driver. 665 */ 666 kobj_class_instantiate(driver); 667 668 /* 669 * Make sure the devclass which the driver is implementing exists. 670 */ 671 devclass_find_internal(driver->name, NULL, TRUE); 672 673 dl->driver = driver; 674 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 675 676 /* 677 * Call BUS_DRIVER_ADDED for any existing busses in this class, 678 * but only if the bus has already been attached (otherwise we 679 * might probe too early). 680 * 681 * This is what will cause a newly loaded module to be associated 682 * with hardware. bus_generic_driver_added() is typically what ends 683 * up being called. 684 */ 685 for (i = 0; i < dc->maxunit; i++) { 686 if ((dev = dc->devices[i]) != NULL) { 687 if (dev->state >= DS_ATTACHED) 688 BUS_DRIVER_ADDED(dev, driver); 689 } 690 } 691 692 bus_data_generation_update(); 693 return(0); 694 } 695 696 int 697 devclass_delete_driver(devclass_t busclass, driver_t *driver) 698 { 699 devclass_t dc = devclass_find(driver->name); 700 driverlink_t dl; 701 device_t dev; 702 int i; 703 int error; 704 705 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 706 707 if (!dc) 708 return(0); 709 710 /* 711 * Find the link structure in the bus' list of drivers. 712 */ 713 TAILQ_FOREACH(dl, &busclass->drivers, link) 714 if (dl->driver == driver) 715 break; 716 717 if (!dl) { 718 PDEBUG(("%s not found in %s list", driver->name, busclass->name)); 719 return(ENOENT); 720 } 721 722 /* 723 * Disassociate from any devices. We iterate through all the 724 * devices in the devclass of the driver and detach any which are 725 * using the driver and which have a parent in the devclass which 726 * we are deleting from. 727 * 728 * Note that since a driver can be in multiple devclasses, we 729 * should not detach devices which are not children of devices in 730 * the affected devclass. 731 */ 732 for (i = 0; i < dc->maxunit; i++) 733 if (dc->devices[i]) { 734 dev = dc->devices[i]; 735 if (dev->driver == driver && dev->parent && 736 dev->parent->devclass == busclass) { 737 if ((error = device_detach(dev)) != 0) 738 return(error); 739 device_set_driver(dev, NULL); 740 } 741 } 742 743 TAILQ_REMOVE(&busclass->drivers, dl, link); 744 kfree(dl, M_BUS); 745 746 kobj_class_uninstantiate(driver); 747 748 bus_data_generation_update(); 749 return(0); 750 } 751 752 static driverlink_t 753 devclass_find_driver_internal(devclass_t dc, const char *classname) 754 { 755 driverlink_t dl; 756 757 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 758 759 TAILQ_FOREACH(dl, &dc->drivers, link) 760 if (!strcmp(dl->driver->name, classname)) 761 return(dl); 762 763 PDEBUG(("not found")); 764 return(NULL); 765 } 766 767 kobj_class_t 768 devclass_find_driver(devclass_t dc, const char *classname) 769 { 770 driverlink_t dl; 771 772 dl = devclass_find_driver_internal(dc, classname); 773 if (dl) 774 return(dl->driver); 775 else 776 return(NULL); 777 } 778 779 const char * 780 devclass_get_name(devclass_t dc) 781 { 782 return(dc->name); 783 } 784 785 device_t 786 devclass_get_device(devclass_t dc, int unit) 787 { 788 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 789 return(NULL); 790 return(dc->devices[unit]); 791 } 792 793 void * 794 devclass_get_softc(devclass_t dc, int unit) 795 { 796 device_t dev; 797 798 dev = devclass_get_device(dc, unit); 799 if (!dev) 800 return(NULL); 801 802 return(device_get_softc(dev)); 803 } 804 805 int 806 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 807 { 808 int i; 809 int count; 810 device_t *list; 811 812 count = 0; 813 for (i = 0; i < dc->maxunit; i++) 814 if (dc->devices[i]) 815 count++; 816 817 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO); 818 if (list == NULL) 819 return(ENOMEM); 820 821 count = 0; 822 for (i = 0; i < dc->maxunit; i++) 823 if (dc->devices[i]) { 824 list[count] = dc->devices[i]; 825 count++; 826 } 827 828 *devlistp = list; 829 *devcountp = count; 830 831 return(0); 832 } 833 834 /** 835 * @brief Get a list of drivers in the devclass 836 * 837 * An array containing a list of pointers to all the drivers in the 838 * given devclass is allocated and returned in @p *listp. The number 839 * of drivers in the array is returned in @p *countp. The caller should 840 * free the array using @c free(p, M_TEMP). 841 * 842 * @param dc the devclass to examine 843 * @param listp gives location for array pointer return value 844 * @param countp gives location for number of array elements 845 * return value 846 * 847 * @retval 0 success 848 * @retval ENOMEM the array allocation failed 849 */ 850 int 851 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp) 852 { 853 driverlink_t dl; 854 driver_t **list; 855 int count; 856 857 count = 0; 858 TAILQ_FOREACH(dl, &dc->drivers, link) 859 count++; 860 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT); 861 if (list == NULL) 862 return (ENOMEM); 863 864 count = 0; 865 TAILQ_FOREACH(dl, &dc->drivers, link) { 866 list[count] = dl->driver; 867 count++; 868 } 869 *listp = list; 870 *countp = count; 871 872 return (0); 873 } 874 875 /** 876 * @brief Get the number of devices in a devclass 877 * 878 * @param dc the devclass to examine 879 */ 880 int 881 devclass_get_count(devclass_t dc) 882 { 883 int count, i; 884 885 count = 0; 886 for (i = 0; i < dc->maxunit; i++) 887 if (dc->devices[i]) 888 count++; 889 return (count); 890 } 891 892 int 893 devclass_get_maxunit(devclass_t dc) 894 { 895 return(dc->maxunit); 896 } 897 898 void 899 devclass_set_parent(devclass_t dc, devclass_t pdc) 900 { 901 dc->parent = pdc; 902 } 903 904 devclass_t 905 devclass_get_parent(devclass_t dc) 906 { 907 return(dc->parent); 908 } 909 910 static int 911 devclass_alloc_unit(devclass_t dc, int *unitp) 912 { 913 int unit = *unitp; 914 915 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 916 917 /* If we have been given a wired unit number, check for existing device */ 918 if (unit != -1) { 919 if (unit >= 0 && unit < dc->maxunit && 920 dc->devices[unit] != NULL) { 921 if (bootverbose) 922 kprintf("%s-: %s%d exists, using next available unit number\n", 923 dc->name, dc->name, unit); 924 /* find the next available slot */ 925 while (++unit < dc->maxunit && dc->devices[unit] != NULL) 926 ; 927 } 928 } else { 929 /* Unwired device, find the next available slot for it */ 930 unit = 0; 931 while (unit < dc->maxunit && dc->devices[unit] != NULL) 932 unit++; 933 } 934 935 /* 936 * We've selected a unit beyond the length of the table, so let's 937 * extend the table to make room for all units up to and including 938 * this one. 939 */ 940 if (unit >= dc->maxunit) { 941 device_t *newlist; 942 int newsize; 943 944 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 945 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS, 946 M_INTWAIT | M_ZERO); 947 if (newlist == NULL) 948 return(ENOMEM); 949 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit); 950 if (dc->devices) 951 kfree(dc->devices, M_BUS); 952 dc->devices = newlist; 953 dc->maxunit = newsize; 954 } 955 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 956 957 *unitp = unit; 958 return(0); 959 } 960 961 static int 962 devclass_add_device(devclass_t dc, device_t dev) 963 { 964 int buflen, error; 965 966 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 967 968 buflen = strlen(dc->name) + 5; 969 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO); 970 if (!dev->nameunit) 971 return(ENOMEM); 972 973 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) { 974 kfree(dev->nameunit, M_BUS); 975 dev->nameunit = NULL; 976 return(error); 977 } 978 dc->devices[dev->unit] = dev; 979 dev->devclass = dc; 980 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 981 982 return(0); 983 } 984 985 static int 986 devclass_delete_device(devclass_t dc, device_t dev) 987 { 988 if (!dc || !dev) 989 return(0); 990 991 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 992 993 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 994 panic("devclass_delete_device: inconsistent device class"); 995 dc->devices[dev->unit] = NULL; 996 if (dev->flags & DF_WILDCARD) 997 dev->unit = -1; 998 dev->devclass = NULL; 999 kfree(dev->nameunit, M_BUS); 1000 dev->nameunit = NULL; 1001 1002 return(0); 1003 } 1004 1005 static device_t 1006 make_device(device_t parent, const char *name, int unit) 1007 { 1008 device_t dev; 1009 devclass_t dc; 1010 1011 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1012 1013 if (name != NULL) { 1014 dc = devclass_find_internal(name, NULL, TRUE); 1015 if (!dc) { 1016 kprintf("make_device: can't find device class %s\n", name); 1017 return(NULL); 1018 } 1019 } else 1020 dc = NULL; 1021 1022 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO); 1023 if (!dev) 1024 return(0); 1025 1026 dev->parent = parent; 1027 TAILQ_INIT(&dev->children); 1028 kobj_init((kobj_t) dev, &null_class); 1029 dev->driver = NULL; 1030 dev->devclass = NULL; 1031 dev->unit = unit; 1032 dev->nameunit = NULL; 1033 dev->desc = NULL; 1034 dev->busy = 0; 1035 dev->devflags = 0; 1036 dev->flags = DF_ENABLED; 1037 dev->order = 0; 1038 if (unit == -1) 1039 dev->flags |= DF_WILDCARD; 1040 if (name) { 1041 dev->flags |= DF_FIXEDCLASS; 1042 if (devclass_add_device(dc, dev) != 0) { 1043 kobj_delete((kobj_t)dev, M_BUS); 1044 return(NULL); 1045 } 1046 } 1047 dev->ivars = NULL; 1048 dev->softc = NULL; 1049 1050 dev->state = DS_NOTPRESENT; 1051 1052 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1053 bus_data_generation_update(); 1054 1055 return(dev); 1056 } 1057 1058 static int 1059 device_print_child(device_t dev, device_t child) 1060 { 1061 int retval = 0; 1062 1063 if (device_is_alive(child)) 1064 retval += BUS_PRINT_CHILD(dev, child); 1065 else 1066 retval += device_printf(child, " not found\n"); 1067 1068 return(retval); 1069 } 1070 1071 device_t 1072 device_add_child(device_t dev, const char *name, int unit) 1073 { 1074 return device_add_child_ordered(dev, 0, name, unit); 1075 } 1076 1077 device_t 1078 device_add_child_ordered(device_t dev, int order, const char *name, int unit) 1079 { 1080 device_t child; 1081 device_t place; 1082 1083 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev), 1084 order, unit)); 1085 1086 child = make_device(dev, name, unit); 1087 if (child == NULL) 1088 return child; 1089 child->order = order; 1090 1091 TAILQ_FOREACH(place, &dev->children, link) 1092 if (place->order > order) 1093 break; 1094 1095 if (place) { 1096 /* 1097 * The device 'place' is the first device whose order is 1098 * greater than the new child. 1099 */ 1100 TAILQ_INSERT_BEFORE(place, child, link); 1101 } else { 1102 /* 1103 * The new child's order is greater or equal to the order of 1104 * any existing device. Add the child to the tail of the list. 1105 */ 1106 TAILQ_INSERT_TAIL(&dev->children, child, link); 1107 } 1108 1109 bus_data_generation_update(); 1110 return(child); 1111 } 1112 1113 int 1114 device_delete_child(device_t dev, device_t child) 1115 { 1116 int error; 1117 device_t grandchild; 1118 1119 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1120 1121 /* remove children first */ 1122 while ( (grandchild = TAILQ_FIRST(&child->children)) ) { 1123 error = device_delete_child(child, grandchild); 1124 if (error) 1125 return(error); 1126 } 1127 1128 if ((error = device_detach(child)) != 0) 1129 return(error); 1130 if (child->devclass) 1131 devclass_delete_device(child->devclass, child); 1132 TAILQ_REMOVE(&dev->children, child, link); 1133 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1134 device_set_desc(child, NULL); 1135 kobj_delete((kobj_t)child, M_BUS); 1136 1137 bus_data_generation_update(); 1138 return(0); 1139 } 1140 1141 /** 1142 * @brief Find a device given a unit number 1143 * 1144 * This is similar to devclass_get_devices() but only searches for 1145 * devices which have @p dev as a parent. 1146 * 1147 * @param dev the parent device to search 1148 * @param unit the unit number to search for. If the unit is -1, 1149 * return the first child of @p dev which has name 1150 * @p classname (that is, the one with the lowest unit.) 1151 * 1152 * @returns the device with the given unit number or @c 1153 * NULL if there is no such device 1154 */ 1155 device_t 1156 device_find_child(device_t dev, const char *classname, int unit) 1157 { 1158 devclass_t dc; 1159 device_t child; 1160 1161 dc = devclass_find(classname); 1162 if (!dc) 1163 return(NULL); 1164 1165 if (unit != -1) { 1166 child = devclass_get_device(dc, unit); 1167 if (child && child->parent == dev) 1168 return (child); 1169 } else { 1170 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { 1171 child = devclass_get_device(dc, unit); 1172 if (child && child->parent == dev) 1173 return (child); 1174 } 1175 } 1176 return(NULL); 1177 } 1178 1179 static driverlink_t 1180 first_matching_driver(devclass_t dc, device_t dev) 1181 { 1182 if (dev->devclass) 1183 return(devclass_find_driver_internal(dc, dev->devclass->name)); 1184 else 1185 return(TAILQ_FIRST(&dc->drivers)); 1186 } 1187 1188 static driverlink_t 1189 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1190 { 1191 if (dev->devclass) { 1192 driverlink_t dl; 1193 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1194 if (!strcmp(dev->devclass->name, dl->driver->name)) 1195 return(dl); 1196 return(NULL); 1197 } else 1198 return(TAILQ_NEXT(last, link)); 1199 } 1200 1201 static int 1202 device_probe_child(device_t dev, device_t child) 1203 { 1204 devclass_t dc; 1205 driverlink_t best = 0; 1206 driverlink_t dl; 1207 int result, pri = 0; 1208 int hasclass = (child->devclass != 0); 1209 1210 dc = dev->devclass; 1211 if (!dc) 1212 panic("device_probe_child: parent device has no devclass"); 1213 1214 if (child->state == DS_ALIVE) 1215 return(0); 1216 1217 for (; dc; dc = dc->parent) { 1218 for (dl = first_matching_driver(dc, child); dl; 1219 dl = next_matching_driver(dc, child, dl)) { 1220 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 1221 device_set_driver(child, dl->driver); 1222 if (!hasclass) 1223 device_set_devclass(child, dl->driver->name); 1224 result = DEVICE_PROBE(child); 1225 if (!hasclass) 1226 device_set_devclass(child, 0); 1227 1228 /* 1229 * If the driver returns SUCCESS, there can be 1230 * no higher match for this device. 1231 */ 1232 if (result == 0) { 1233 best = dl; 1234 pri = 0; 1235 break; 1236 } 1237 1238 /* 1239 * The driver returned an error so it 1240 * certainly doesn't match. 1241 */ 1242 if (result > 0) { 1243 device_set_driver(child, 0); 1244 continue; 1245 } 1246 1247 /* 1248 * A priority lower than SUCCESS, remember the 1249 * best matching driver. Initialise the value 1250 * of pri for the first match. 1251 */ 1252 if (best == 0 || result > pri) { 1253 best = dl; 1254 pri = result; 1255 continue; 1256 } 1257 } 1258 /* 1259 * If we have unambiguous match in this devclass, 1260 * don't look in the parent. 1261 */ 1262 if (best && pri == 0) 1263 break; 1264 } 1265 1266 /* 1267 * If we found a driver, change state and initialise the devclass. 1268 */ 1269 if (best) { 1270 if (!child->devclass) 1271 device_set_devclass(child, best->driver->name); 1272 device_set_driver(child, best->driver); 1273 if (pri < 0) { 1274 /* 1275 * A bit bogus. Call the probe method again to make 1276 * sure that we have the right description. 1277 */ 1278 DEVICE_PROBE(child); 1279 } 1280 1281 bus_data_generation_update(); 1282 child->state = DS_ALIVE; 1283 return(0); 1284 } 1285 1286 return(ENXIO); 1287 } 1288 1289 device_t 1290 device_get_parent(device_t dev) 1291 { 1292 return dev->parent; 1293 } 1294 1295 int 1296 device_get_children(device_t dev, device_t **devlistp, int *devcountp) 1297 { 1298 int count; 1299 device_t child; 1300 device_t *list; 1301 1302 count = 0; 1303 TAILQ_FOREACH(child, &dev->children, link) 1304 count++; 1305 1306 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO); 1307 if (!list) 1308 return(ENOMEM); 1309 1310 count = 0; 1311 TAILQ_FOREACH(child, &dev->children, link) { 1312 list[count] = child; 1313 count++; 1314 } 1315 1316 *devlistp = list; 1317 *devcountp = count; 1318 1319 return(0); 1320 } 1321 1322 driver_t * 1323 device_get_driver(device_t dev) 1324 { 1325 return(dev->driver); 1326 } 1327 1328 devclass_t 1329 device_get_devclass(device_t dev) 1330 { 1331 return(dev->devclass); 1332 } 1333 1334 const char * 1335 device_get_name(device_t dev) 1336 { 1337 if (dev->devclass) 1338 return devclass_get_name(dev->devclass); 1339 return(NULL); 1340 } 1341 1342 const char * 1343 device_get_nameunit(device_t dev) 1344 { 1345 return(dev->nameunit); 1346 } 1347 1348 int 1349 device_get_unit(device_t dev) 1350 { 1351 return(dev->unit); 1352 } 1353 1354 const char * 1355 device_get_desc(device_t dev) 1356 { 1357 return(dev->desc); 1358 } 1359 1360 uint32_t 1361 device_get_flags(device_t dev) 1362 { 1363 return(dev->devflags); 1364 } 1365 1366 int 1367 device_print_prettyname(device_t dev) 1368 { 1369 const char *name = device_get_name(dev); 1370 1371 if (name == 0) 1372 return kprintf("unknown: "); 1373 else 1374 return kprintf("%s%d: ", name, device_get_unit(dev)); 1375 } 1376 1377 int 1378 device_printf(device_t dev, const char * fmt, ...) 1379 { 1380 __va_list ap; 1381 int retval; 1382 1383 retval = device_print_prettyname(dev); 1384 __va_start(ap, fmt); 1385 retval += kvprintf(fmt, ap); 1386 __va_end(ap); 1387 return retval; 1388 } 1389 1390 static void 1391 device_set_desc_internal(device_t dev, const char* desc, int copy) 1392 { 1393 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 1394 kfree(dev->desc, M_BUS); 1395 dev->flags &= ~DF_DESCMALLOCED; 1396 dev->desc = NULL; 1397 } 1398 1399 if (copy && desc) { 1400 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT); 1401 if (dev->desc) { 1402 strcpy(dev->desc, desc); 1403 dev->flags |= DF_DESCMALLOCED; 1404 } 1405 } else { 1406 /* Avoid a -Wcast-qual warning */ 1407 dev->desc = (char *)(uintptr_t) desc; 1408 } 1409 1410 bus_data_generation_update(); 1411 } 1412 1413 void 1414 device_set_desc(device_t dev, const char* desc) 1415 { 1416 device_set_desc_internal(dev, desc, FALSE); 1417 } 1418 1419 void 1420 device_set_desc_copy(device_t dev, const char* desc) 1421 { 1422 device_set_desc_internal(dev, desc, TRUE); 1423 } 1424 1425 void 1426 device_set_flags(device_t dev, uint32_t flags) 1427 { 1428 dev->devflags = flags; 1429 } 1430 1431 void * 1432 device_get_softc(device_t dev) 1433 { 1434 return dev->softc; 1435 } 1436 1437 void 1438 device_set_softc(device_t dev, void *softc) 1439 { 1440 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 1441 kfree(dev->softc, M_BUS); 1442 dev->softc = softc; 1443 if (dev->softc) 1444 dev->flags |= DF_EXTERNALSOFTC; 1445 else 1446 dev->flags &= ~DF_EXTERNALSOFTC; 1447 } 1448 1449 void 1450 device_set_async_attach(device_t dev, int enable) 1451 { 1452 if (enable) 1453 dev->flags |= DF_ASYNCPROBE; 1454 else 1455 dev->flags &= ~DF_ASYNCPROBE; 1456 } 1457 1458 void * 1459 device_get_ivars(device_t dev) 1460 { 1461 return dev->ivars; 1462 } 1463 1464 void 1465 device_set_ivars(device_t dev, void * ivars) 1466 { 1467 if (!dev) 1468 return; 1469 1470 dev->ivars = ivars; 1471 } 1472 1473 device_state_t 1474 device_get_state(device_t dev) 1475 { 1476 return(dev->state); 1477 } 1478 1479 void 1480 device_enable(device_t dev) 1481 { 1482 dev->flags |= DF_ENABLED; 1483 } 1484 1485 void 1486 device_disable(device_t dev) 1487 { 1488 dev->flags &= ~DF_ENABLED; 1489 } 1490 1491 /* 1492 * YYY cannot block 1493 */ 1494 void 1495 device_busy(device_t dev) 1496 { 1497 if (dev->state < DS_ATTACHED) 1498 panic("device_busy: called for unattached device"); 1499 if (dev->busy == 0 && dev->parent) 1500 device_busy(dev->parent); 1501 dev->busy++; 1502 dev->state = DS_BUSY; 1503 } 1504 1505 /* 1506 * YYY cannot block 1507 */ 1508 void 1509 device_unbusy(device_t dev) 1510 { 1511 if (dev->state != DS_BUSY) 1512 panic("device_unbusy: called for non-busy device"); 1513 dev->busy--; 1514 if (dev->busy == 0) { 1515 if (dev->parent) 1516 device_unbusy(dev->parent); 1517 dev->state = DS_ATTACHED; 1518 } 1519 } 1520 1521 void 1522 device_quiet(device_t dev) 1523 { 1524 dev->flags |= DF_QUIET; 1525 } 1526 1527 void 1528 device_verbose(device_t dev) 1529 { 1530 dev->flags &= ~DF_QUIET; 1531 } 1532 1533 int 1534 device_is_quiet(device_t dev) 1535 { 1536 return((dev->flags & DF_QUIET) != 0); 1537 } 1538 1539 int 1540 device_is_enabled(device_t dev) 1541 { 1542 return((dev->flags & DF_ENABLED) != 0); 1543 } 1544 1545 int 1546 device_is_alive(device_t dev) 1547 { 1548 return(dev->state >= DS_ALIVE); 1549 } 1550 1551 int 1552 device_is_attached(device_t dev) 1553 { 1554 return(dev->state >= DS_ATTACHED); 1555 } 1556 1557 int 1558 device_set_devclass(device_t dev, const char *classname) 1559 { 1560 devclass_t dc; 1561 int error; 1562 1563 if (!classname) { 1564 if (dev->devclass) 1565 devclass_delete_device(dev->devclass, dev); 1566 return(0); 1567 } 1568 1569 if (dev->devclass) { 1570 kprintf("device_set_devclass: device class already set\n"); 1571 return(EINVAL); 1572 } 1573 1574 dc = devclass_find_internal(classname, NULL, TRUE); 1575 if (!dc) 1576 return(ENOMEM); 1577 1578 error = devclass_add_device(dc, dev); 1579 1580 bus_data_generation_update(); 1581 return(error); 1582 } 1583 1584 int 1585 device_set_driver(device_t dev, driver_t *driver) 1586 { 1587 if (dev->state >= DS_ATTACHED) 1588 return(EBUSY); 1589 1590 if (dev->driver == driver) 1591 return(0); 1592 1593 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 1594 kfree(dev->softc, M_BUS); 1595 dev->softc = NULL; 1596 } 1597 kobj_delete((kobj_t) dev, 0); 1598 dev->driver = driver; 1599 if (driver) { 1600 kobj_init((kobj_t) dev, (kobj_class_t) driver); 1601 if (!(dev->flags & DF_EXTERNALSOFTC)) { 1602 dev->softc = kmalloc(driver->size, M_BUS, 1603 M_INTWAIT | M_ZERO); 1604 if (!dev->softc) { 1605 kobj_delete((kobj_t)dev, 0); 1606 kobj_init((kobj_t) dev, &null_class); 1607 dev->driver = NULL; 1608 return(ENOMEM); 1609 } 1610 } 1611 } else { 1612 kobj_init((kobj_t) dev, &null_class); 1613 } 1614 1615 bus_data_generation_update(); 1616 return(0); 1617 } 1618 1619 int 1620 device_probe_and_attach(device_t dev) 1621 { 1622 device_t bus = dev->parent; 1623 int error = 0; 1624 1625 if (dev->state >= DS_ALIVE) 1626 return(0); 1627 1628 if ((dev->flags & DF_ENABLED) == 0) { 1629 if (bootverbose) { 1630 device_print_prettyname(dev); 1631 kprintf("not probed (disabled)\n"); 1632 } 1633 return(0); 1634 } 1635 1636 error = device_probe_child(bus, dev); 1637 if (error) { 1638 if (!(dev->flags & DF_DONENOMATCH)) { 1639 BUS_PROBE_NOMATCH(bus, dev); 1640 devnomatch(dev); 1641 dev->flags |= DF_DONENOMATCH; 1642 } 1643 return(error); 1644 } 1645 1646 /* 1647 * Output the exact device chain prior to the attach in case the 1648 * system locks up during attach, and generate the full info after 1649 * the attach so correct irq and other information is displayed. 1650 */ 1651 if (bootverbose && !device_is_quiet(dev)) { 1652 device_t tmp; 1653 1654 kprintf("%s", device_get_nameunit(dev)); 1655 for (tmp = dev->parent; tmp; tmp = tmp->parent) 1656 kprintf(".%s", device_get_nameunit(tmp)); 1657 kprintf("\n"); 1658 } 1659 if (!device_is_quiet(dev)) 1660 device_print_child(bus, dev); 1661 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) { 1662 kprintf("%s: probing asynchronously\n", 1663 device_get_nameunit(dev)); 1664 dev->state = DS_INPROGRESS; 1665 device_attach_async(dev); 1666 error = 0; 1667 } else { 1668 error = device_doattach(dev); 1669 } 1670 return(error); 1671 } 1672 1673 /* 1674 * Device is known to be alive, do the attach asynchronously. 1675 * However, serialize the attaches with the mp lock. 1676 */ 1677 static void 1678 device_attach_async(device_t dev) 1679 { 1680 thread_t td; 1681 1682 atomic_add_int(&numasyncthreads, 1); 1683 lwkt_create(device_attach_thread, dev, &td, NULL, 1684 0, 0, (dev->desc ? dev->desc : "devattach")); 1685 } 1686 1687 static void 1688 device_attach_thread(void *arg) 1689 { 1690 device_t dev = arg; 1691 1692 get_mplock(); /* XXX replace with devattach_token later */ 1693 (void)device_doattach(dev); 1694 atomic_subtract_int(&numasyncthreads, 1); 1695 wakeup(&numasyncthreads); 1696 rel_mplock(); /* XXX replace with devattach_token later */ 1697 } 1698 1699 /* 1700 * Device is known to be alive, do the attach (synchronous or asynchronous) 1701 */ 1702 static int 1703 device_doattach(device_t dev) 1704 { 1705 device_t bus = dev->parent; 1706 int hasclass = (dev->devclass != 0); 1707 int error; 1708 1709 error = DEVICE_ATTACH(dev); 1710 if (error == 0) { 1711 dev->state = DS_ATTACHED; 1712 if (bootverbose && !device_is_quiet(dev)) 1713 device_print_child(bus, dev); 1714 devadded(dev); 1715 } else { 1716 kprintf("device_probe_and_attach: %s%d attach returned %d\n", 1717 dev->driver->name, dev->unit, error); 1718 /* Unset the class that was set in device_probe_child */ 1719 if (!hasclass) 1720 device_set_devclass(dev, 0); 1721 device_set_driver(dev, NULL); 1722 dev->state = DS_NOTPRESENT; 1723 } 1724 return(error); 1725 } 1726 1727 int 1728 device_detach(device_t dev) 1729 { 1730 int error; 1731 1732 PDEBUG(("%s", DEVICENAME(dev))); 1733 if (dev->state == DS_BUSY) 1734 return(EBUSY); 1735 if (dev->state != DS_ATTACHED) 1736 return(0); 1737 1738 if ((error = DEVICE_DETACH(dev)) != 0) 1739 return(error); 1740 devremoved(dev); 1741 device_printf(dev, "detached\n"); 1742 if (dev->parent) 1743 BUS_CHILD_DETACHED(dev->parent, dev); 1744 1745 if (!(dev->flags & DF_FIXEDCLASS)) 1746 devclass_delete_device(dev->devclass, dev); 1747 1748 dev->state = DS_NOTPRESENT; 1749 device_set_driver(dev, NULL); 1750 1751 return(0); 1752 } 1753 1754 int 1755 device_shutdown(device_t dev) 1756 { 1757 if (dev->state < DS_ATTACHED) 1758 return 0; 1759 PDEBUG(("%s", DEVICENAME(dev))); 1760 return DEVICE_SHUTDOWN(dev); 1761 } 1762 1763 int 1764 device_set_unit(device_t dev, int unit) 1765 { 1766 devclass_t dc; 1767 int err; 1768 1769 dc = device_get_devclass(dev); 1770 if (unit < dc->maxunit && dc->devices[unit]) 1771 return(EBUSY); 1772 err = devclass_delete_device(dc, dev); 1773 if (err) 1774 return(err); 1775 dev->unit = unit; 1776 err = devclass_add_device(dc, dev); 1777 if (err) 1778 return(err); 1779 1780 bus_data_generation_update(); 1781 return(0); 1782 } 1783 1784 /*======================================*/ 1785 /* 1786 * Access functions for device resources. 1787 */ 1788 1789 /* Supplied by config(8) in ioconf.c */ 1790 extern struct config_device config_devtab[]; 1791 extern int devtab_count; 1792 1793 /* Runtime version */ 1794 struct config_device *devtab = config_devtab; 1795 1796 static int 1797 resource_new_name(const char *name, int unit) 1798 { 1799 struct config_device *new; 1800 1801 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP, 1802 M_INTWAIT | M_ZERO); 1803 if (new == NULL) 1804 return(-1); 1805 if (devtab && devtab_count > 0) 1806 bcopy(devtab, new, devtab_count * sizeof(*new)); 1807 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT); 1808 if (new[devtab_count].name == NULL) { 1809 kfree(new, M_TEMP); 1810 return(-1); 1811 } 1812 strcpy(new[devtab_count].name, name); 1813 new[devtab_count].unit = unit; 1814 new[devtab_count].resource_count = 0; 1815 new[devtab_count].resources = NULL; 1816 if (devtab && devtab != config_devtab) 1817 kfree(devtab, M_TEMP); 1818 devtab = new; 1819 return devtab_count++; 1820 } 1821 1822 static int 1823 resource_new_resname(int j, const char *resname, resource_type type) 1824 { 1825 struct config_resource *new; 1826 int i; 1827 1828 i = devtab[j].resource_count; 1829 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO); 1830 if (new == NULL) 1831 return(-1); 1832 if (devtab[j].resources && i > 0) 1833 bcopy(devtab[j].resources, new, i * sizeof(*new)); 1834 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT); 1835 if (new[i].name == NULL) { 1836 kfree(new, M_TEMP); 1837 return(-1); 1838 } 1839 strcpy(new[i].name, resname); 1840 new[i].type = type; 1841 if (devtab[j].resources) 1842 kfree(devtab[j].resources, M_TEMP); 1843 devtab[j].resources = new; 1844 devtab[j].resource_count = i + 1; 1845 return(i); 1846 } 1847 1848 static int 1849 resource_match_string(int i, const char *resname, const char *value) 1850 { 1851 int j; 1852 struct config_resource *res; 1853 1854 for (j = 0, res = devtab[i].resources; 1855 j < devtab[i].resource_count; j++, res++) 1856 if (!strcmp(res->name, resname) 1857 && res->type == RES_STRING 1858 && !strcmp(res->u.stringval, value)) 1859 return(j); 1860 return(-1); 1861 } 1862 1863 static int 1864 resource_find(const char *name, int unit, const char *resname, 1865 struct config_resource **result) 1866 { 1867 int i, j; 1868 struct config_resource *res; 1869 1870 /* 1871 * First check specific instances, then generic. 1872 */ 1873 for (i = 0; i < devtab_count; i++) { 1874 if (devtab[i].unit < 0) 1875 continue; 1876 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1877 res = devtab[i].resources; 1878 for (j = 0; j < devtab[i].resource_count; j++, res++) 1879 if (!strcmp(res->name, resname)) { 1880 *result = res; 1881 return(0); 1882 } 1883 } 1884 } 1885 for (i = 0; i < devtab_count; i++) { 1886 if (devtab[i].unit >= 0) 1887 continue; 1888 /* XXX should this `&& devtab[i].unit == unit' be here? */ 1889 /* XXX if so, then the generic match does nothing */ 1890 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1891 res = devtab[i].resources; 1892 for (j = 0; j < devtab[i].resource_count; j++, res++) 1893 if (!strcmp(res->name, resname)) { 1894 *result = res; 1895 return(0); 1896 } 1897 } 1898 } 1899 return(ENOENT); 1900 } 1901 1902 int 1903 resource_int_value(const char *name, int unit, const char *resname, int *result) 1904 { 1905 int error; 1906 struct config_resource *res; 1907 1908 if ((error = resource_find(name, unit, resname, &res)) != 0) 1909 return(error); 1910 if (res->type != RES_INT) 1911 return(EFTYPE); 1912 *result = res->u.intval; 1913 return(0); 1914 } 1915 1916 int 1917 resource_long_value(const char *name, int unit, const char *resname, 1918 long *result) 1919 { 1920 int error; 1921 struct config_resource *res; 1922 1923 if ((error = resource_find(name, unit, resname, &res)) != 0) 1924 return(error); 1925 if (res->type != RES_LONG) 1926 return(EFTYPE); 1927 *result = res->u.longval; 1928 return(0); 1929 } 1930 1931 int 1932 resource_string_value(const char *name, int unit, const char *resname, 1933 char **result) 1934 { 1935 int error; 1936 struct config_resource *res; 1937 1938 if ((error = resource_find(name, unit, resname, &res)) != 0) 1939 return(error); 1940 if (res->type != RES_STRING) 1941 return(EFTYPE); 1942 *result = res->u.stringval; 1943 return(0); 1944 } 1945 1946 int 1947 resource_query_string(int i, const char *resname, const char *value) 1948 { 1949 if (i < 0) 1950 i = 0; 1951 else 1952 i = i + 1; 1953 for (; i < devtab_count; i++) 1954 if (resource_match_string(i, resname, value) >= 0) 1955 return(i); 1956 return(-1); 1957 } 1958 1959 int 1960 resource_locate(int i, const char *resname) 1961 { 1962 if (i < 0) 1963 i = 0; 1964 else 1965 i = i + 1; 1966 for (; i < devtab_count; i++) 1967 if (!strcmp(devtab[i].name, resname)) 1968 return(i); 1969 return(-1); 1970 } 1971 1972 int 1973 resource_count(void) 1974 { 1975 return(devtab_count); 1976 } 1977 1978 char * 1979 resource_query_name(int i) 1980 { 1981 return(devtab[i].name); 1982 } 1983 1984 int 1985 resource_query_unit(int i) 1986 { 1987 return(devtab[i].unit); 1988 } 1989 1990 static int 1991 resource_create(const char *name, int unit, const char *resname, 1992 resource_type type, struct config_resource **result) 1993 { 1994 int i, j; 1995 struct config_resource *res = NULL; 1996 1997 for (i = 0; i < devtab_count; i++) 1998 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) { 1999 res = devtab[i].resources; 2000 break; 2001 } 2002 if (res == NULL) { 2003 i = resource_new_name(name, unit); 2004 if (i < 0) 2005 return(ENOMEM); 2006 res = devtab[i].resources; 2007 } 2008 for (j = 0; j < devtab[i].resource_count; j++, res++) 2009 if (!strcmp(res->name, resname)) { 2010 *result = res; 2011 return(0); 2012 } 2013 j = resource_new_resname(i, resname, type); 2014 if (j < 0) 2015 return(ENOMEM); 2016 res = &devtab[i].resources[j]; 2017 *result = res; 2018 return(0); 2019 } 2020 2021 int 2022 resource_set_int(const char *name, int unit, const char *resname, int value) 2023 { 2024 int error; 2025 struct config_resource *res; 2026 2027 error = resource_create(name, unit, resname, RES_INT, &res); 2028 if (error) 2029 return(error); 2030 if (res->type != RES_INT) 2031 return(EFTYPE); 2032 res->u.intval = value; 2033 return(0); 2034 } 2035 2036 int 2037 resource_set_long(const char *name, int unit, const char *resname, long value) 2038 { 2039 int error; 2040 struct config_resource *res; 2041 2042 error = resource_create(name, unit, resname, RES_LONG, &res); 2043 if (error) 2044 return(error); 2045 if (res->type != RES_LONG) 2046 return(EFTYPE); 2047 res->u.longval = value; 2048 return(0); 2049 } 2050 2051 int 2052 resource_set_string(const char *name, int unit, const char *resname, 2053 const char *value) 2054 { 2055 int error; 2056 struct config_resource *res; 2057 2058 error = resource_create(name, unit, resname, RES_STRING, &res); 2059 if (error) 2060 return(error); 2061 if (res->type != RES_STRING) 2062 return(EFTYPE); 2063 if (res->u.stringval) 2064 kfree(res->u.stringval, M_TEMP); 2065 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT); 2066 if (res->u.stringval == NULL) 2067 return(ENOMEM); 2068 strcpy(res->u.stringval, value); 2069 return(0); 2070 } 2071 2072 static void 2073 resource_cfgload(void *dummy __unused) 2074 { 2075 struct config_resource *res, *cfgres; 2076 int i, j; 2077 int error; 2078 char *name, *resname; 2079 int unit; 2080 resource_type type; 2081 char *stringval; 2082 int config_devtab_count; 2083 2084 config_devtab_count = devtab_count; 2085 devtab = NULL; 2086 devtab_count = 0; 2087 2088 for (i = 0; i < config_devtab_count; i++) { 2089 name = config_devtab[i].name; 2090 unit = config_devtab[i].unit; 2091 2092 for (j = 0; j < config_devtab[i].resource_count; j++) { 2093 cfgres = config_devtab[i].resources; 2094 resname = cfgres[j].name; 2095 type = cfgres[j].type; 2096 error = resource_create(name, unit, resname, type, 2097 &res); 2098 if (error) { 2099 kprintf("create resource %s%d: error %d\n", 2100 name, unit, error); 2101 continue; 2102 } 2103 if (res->type != type) { 2104 kprintf("type mismatch %s%d: %d != %d\n", 2105 name, unit, res->type, type); 2106 continue; 2107 } 2108 switch (type) { 2109 case RES_INT: 2110 res->u.intval = cfgres[j].u.intval; 2111 break; 2112 case RES_LONG: 2113 res->u.longval = cfgres[j].u.longval; 2114 break; 2115 case RES_STRING: 2116 if (res->u.stringval) 2117 kfree(res->u.stringval, M_TEMP); 2118 stringval = cfgres[j].u.stringval; 2119 res->u.stringval = kmalloc(strlen(stringval) + 1, 2120 M_TEMP, M_INTWAIT); 2121 if (res->u.stringval == NULL) 2122 break; 2123 strcpy(res->u.stringval, stringval); 2124 break; 2125 default: 2126 panic("unknown resource type %d", type); 2127 } 2128 } 2129 } 2130 } 2131 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0) 2132 2133 2134 /*======================================*/ 2135 /* 2136 * Some useful method implementations to make life easier for bus drivers. 2137 */ 2138 2139 void 2140 resource_list_init(struct resource_list *rl) 2141 { 2142 SLIST_INIT(rl); 2143 } 2144 2145 void 2146 resource_list_free(struct resource_list *rl) 2147 { 2148 struct resource_list_entry *rle; 2149 2150 while ((rle = SLIST_FIRST(rl)) != NULL) { 2151 if (rle->res) 2152 panic("resource_list_free: resource entry is busy"); 2153 SLIST_REMOVE_HEAD(rl, link); 2154 kfree(rle, M_BUS); 2155 } 2156 } 2157 2158 void 2159 resource_list_add(struct resource_list *rl, 2160 int type, int rid, 2161 u_long start, u_long end, u_long count) 2162 { 2163 struct resource_list_entry *rle; 2164 2165 rle = resource_list_find(rl, type, rid); 2166 if (rle == NULL) { 2167 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS, 2168 M_INTWAIT); 2169 if (!rle) 2170 panic("resource_list_add: can't record entry"); 2171 SLIST_INSERT_HEAD(rl, rle, link); 2172 rle->type = type; 2173 rle->rid = rid; 2174 rle->res = NULL; 2175 } 2176 2177 if (rle->res) 2178 panic("resource_list_add: resource entry is busy"); 2179 2180 rle->start = start; 2181 rle->end = end; 2182 rle->count = count; 2183 } 2184 2185 struct resource_list_entry* 2186 resource_list_find(struct resource_list *rl, 2187 int type, int rid) 2188 { 2189 struct resource_list_entry *rle; 2190 2191 SLIST_FOREACH(rle, rl, link) 2192 if (rle->type == type && rle->rid == rid) 2193 return(rle); 2194 return(NULL); 2195 } 2196 2197 void 2198 resource_list_delete(struct resource_list *rl, 2199 int type, int rid) 2200 { 2201 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 2202 2203 if (rle) { 2204 if (rle->res != NULL) 2205 panic("resource_list_delete: resource has not been released"); 2206 SLIST_REMOVE(rl, rle, resource_list_entry, link); 2207 kfree(rle, M_BUS); 2208 } 2209 } 2210 2211 struct resource * 2212 resource_list_alloc(struct resource_list *rl, 2213 device_t bus, device_t child, 2214 int type, int *rid, 2215 u_long start, u_long end, 2216 u_long count, u_int flags) 2217 { 2218 struct resource_list_entry *rle = 0; 2219 int passthrough = (device_get_parent(child) != bus); 2220 int isdefault = (start == 0UL && end == ~0UL); 2221 2222 if (passthrough) { 2223 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2224 type, rid, 2225 start, end, count, flags)); 2226 } 2227 2228 rle = resource_list_find(rl, type, *rid); 2229 2230 if (!rle) 2231 return(0); /* no resource of that type/rid */ 2232 2233 if (rle->res) 2234 panic("resource_list_alloc: resource entry is busy"); 2235 2236 if (isdefault) { 2237 start = rle->start; 2238 count = max(count, rle->count); 2239 end = max(rle->end, start + count - 1); 2240 } 2241 2242 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2243 type, rid, start, end, count, flags); 2244 2245 /* 2246 * Record the new range. 2247 */ 2248 if (rle->res) { 2249 rle->start = rman_get_start(rle->res); 2250 rle->end = rman_get_end(rle->res); 2251 rle->count = count; 2252 } 2253 2254 return(rle->res); 2255 } 2256 2257 int 2258 resource_list_release(struct resource_list *rl, 2259 device_t bus, device_t child, 2260 int type, int rid, struct resource *res) 2261 { 2262 struct resource_list_entry *rle = 0; 2263 int passthrough = (device_get_parent(child) != bus); 2264 int error; 2265 2266 if (passthrough) { 2267 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2268 type, rid, res)); 2269 } 2270 2271 rle = resource_list_find(rl, type, rid); 2272 2273 if (!rle) 2274 panic("resource_list_release: can't find resource"); 2275 if (!rle->res) 2276 panic("resource_list_release: resource entry is not busy"); 2277 2278 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2279 type, rid, res); 2280 if (error) 2281 return(error); 2282 2283 rle->res = NULL; 2284 return(0); 2285 } 2286 2287 int 2288 resource_list_print_type(struct resource_list *rl, const char *name, int type, 2289 const char *format) 2290 { 2291 struct resource_list_entry *rle; 2292 int printed, retval; 2293 2294 printed = 0; 2295 retval = 0; 2296 /* Yes, this is kinda cheating */ 2297 SLIST_FOREACH(rle, rl, link) { 2298 if (rle->type == type) { 2299 if (printed == 0) 2300 retval += kprintf(" %s ", name); 2301 else 2302 retval += kprintf(","); 2303 printed++; 2304 retval += kprintf(format, rle->start); 2305 if (rle->count > 1) { 2306 retval += kprintf("-"); 2307 retval += kprintf(format, rle->start + 2308 rle->count - 1); 2309 } 2310 } 2311 } 2312 return(retval); 2313 } 2314 2315 /* 2316 * Generic driver/device identify functions. These will install a device 2317 * rendezvous point under the parent using the same name as the driver 2318 * name, which will at a later time be probed and attached. 2319 * 2320 * These functions are used when the parent does not 'scan' its bus for 2321 * matching devices, or for the particular devices using these functions, 2322 * or when the device is a pseudo or synthesized device (such as can be 2323 * found under firewire and ppbus). 2324 */ 2325 int 2326 bus_generic_identify(driver_t *driver, device_t parent) 2327 { 2328 if (parent->state == DS_ATTACHED) 2329 return (0); 2330 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1); 2331 return (0); 2332 } 2333 2334 int 2335 bus_generic_identify_sameunit(driver_t *driver, device_t parent) 2336 { 2337 if (parent->state == DS_ATTACHED) 2338 return (0); 2339 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent)); 2340 return (0); 2341 } 2342 2343 /* 2344 * Call DEVICE_IDENTIFY for each driver. 2345 */ 2346 int 2347 bus_generic_probe(device_t dev) 2348 { 2349 devclass_t dc = dev->devclass; 2350 driverlink_t dl; 2351 2352 TAILQ_FOREACH(dl, &dc->drivers, link) { 2353 DEVICE_IDENTIFY(dl->driver, dev); 2354 } 2355 2356 return(0); 2357 } 2358 2359 /* 2360 * This is an aweful hack due to the isa bus and autoconf code not 2361 * probing the ISA devices until after everything else has configured. 2362 * The ISA bus did a dummy attach long ago so we have to set it back 2363 * to an earlier state so the probe thinks its the initial probe and 2364 * not a bus rescan. 2365 * 2366 * XXX remove by properly defering the ISA bus scan. 2367 */ 2368 int 2369 bus_generic_probe_hack(device_t dev) 2370 { 2371 if (dev->state == DS_ATTACHED) { 2372 dev->state = DS_ALIVE; 2373 bus_generic_probe(dev); 2374 dev->state = DS_ATTACHED; 2375 } 2376 return (0); 2377 } 2378 2379 int 2380 bus_generic_attach(device_t dev) 2381 { 2382 device_t child; 2383 2384 TAILQ_FOREACH(child, &dev->children, link) { 2385 device_probe_and_attach(child); 2386 } 2387 2388 return(0); 2389 } 2390 2391 int 2392 bus_generic_detach(device_t dev) 2393 { 2394 device_t child; 2395 int error; 2396 2397 if (dev->state != DS_ATTACHED) 2398 return(EBUSY); 2399 2400 TAILQ_FOREACH(child, &dev->children, link) 2401 if ((error = device_detach(child)) != 0) 2402 return(error); 2403 2404 return 0; 2405 } 2406 2407 int 2408 bus_generic_shutdown(device_t dev) 2409 { 2410 device_t child; 2411 2412 TAILQ_FOREACH(child, &dev->children, link) 2413 device_shutdown(child); 2414 2415 return(0); 2416 } 2417 2418 int 2419 bus_generic_suspend(device_t dev) 2420 { 2421 int error; 2422 device_t child, child2; 2423 2424 TAILQ_FOREACH(child, &dev->children, link) { 2425 error = DEVICE_SUSPEND(child); 2426 if (error) { 2427 for (child2 = TAILQ_FIRST(&dev->children); 2428 child2 && child2 != child; 2429 child2 = TAILQ_NEXT(child2, link)) 2430 DEVICE_RESUME(child2); 2431 return(error); 2432 } 2433 } 2434 return(0); 2435 } 2436 2437 int 2438 bus_generic_resume(device_t dev) 2439 { 2440 device_t child; 2441 2442 TAILQ_FOREACH(child, &dev->children, link) 2443 DEVICE_RESUME(child); 2444 /* if resume fails, there's nothing we can usefully do... */ 2445 2446 return(0); 2447 } 2448 2449 int 2450 bus_print_child_header(device_t dev, device_t child) 2451 { 2452 int retval = 0; 2453 2454 if (device_get_desc(child)) 2455 retval += device_printf(child, "<%s>", device_get_desc(child)); 2456 else 2457 retval += kprintf("%s", device_get_nameunit(child)); 2458 if (bootverbose) { 2459 if (child->state != DS_ATTACHED) 2460 kprintf(" [tentative]"); 2461 else 2462 kprintf(" [attached!]"); 2463 } 2464 return(retval); 2465 } 2466 2467 int 2468 bus_print_child_footer(device_t dev, device_t child) 2469 { 2470 return(kprintf(" on %s\n", device_get_nameunit(dev))); 2471 } 2472 2473 device_t 2474 bus_generic_add_child(device_t dev, device_t child, int order, 2475 const char *name, int unit) 2476 { 2477 if (dev->parent) 2478 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit); 2479 else 2480 dev = device_add_child_ordered(child, order, name, unit); 2481 return(dev); 2482 2483 } 2484 2485 int 2486 bus_generic_print_child(device_t dev, device_t child) 2487 { 2488 int retval = 0; 2489 2490 retval += bus_print_child_header(dev, child); 2491 retval += bus_print_child_footer(dev, child); 2492 2493 return(retval); 2494 } 2495 2496 int 2497 bus_generic_read_ivar(device_t dev, device_t child, int index, 2498 uintptr_t * result) 2499 { 2500 int error; 2501 2502 if (dev->parent) 2503 error = BUS_READ_IVAR(dev->parent, child, index, result); 2504 else 2505 error = ENOENT; 2506 return (error); 2507 } 2508 2509 int 2510 bus_generic_write_ivar(device_t dev, device_t child, int index, 2511 uintptr_t value) 2512 { 2513 int error; 2514 2515 if (dev->parent) 2516 error = BUS_WRITE_IVAR(dev->parent, child, index, value); 2517 else 2518 error = ENOENT; 2519 return (error); 2520 } 2521 2522 /* 2523 * Resource list are used for iterations, do not recurse. 2524 */ 2525 struct resource_list * 2526 bus_generic_get_resource_list(device_t dev, device_t child) 2527 { 2528 return (NULL); 2529 } 2530 2531 void 2532 bus_generic_driver_added(device_t dev, driver_t *driver) 2533 { 2534 device_t child; 2535 2536 DEVICE_IDENTIFY(driver, dev); 2537 TAILQ_FOREACH(child, &dev->children, link) { 2538 if (child->state == DS_NOTPRESENT) 2539 device_probe_and_attach(child); 2540 } 2541 } 2542 2543 int 2544 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 2545 int flags, driver_intr_t *intr, void *arg, 2546 void **cookiep, lwkt_serialize_t serializer) 2547 { 2548 /* Propagate up the bus hierarchy until someone handles it. */ 2549 if (dev->parent) 2550 return(BUS_SETUP_INTR(dev->parent, child, irq, flags, 2551 intr, arg, cookiep, serializer)); 2552 else 2553 return(EINVAL); 2554 } 2555 2556 int 2557 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 2558 void *cookie) 2559 { 2560 /* Propagate up the bus hierarchy until someone handles it. */ 2561 if (dev->parent) 2562 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 2563 else 2564 return(EINVAL); 2565 } 2566 2567 int 2568 bus_generic_disable_intr(device_t dev, device_t child, void *cookie) 2569 { 2570 if (dev->parent) 2571 return(BUS_DISABLE_INTR(dev->parent, child, cookie)); 2572 else 2573 return(0); 2574 } 2575 2576 void 2577 bus_generic_enable_intr(device_t dev, device_t child, void *cookie) 2578 { 2579 if (dev->parent) 2580 BUS_ENABLE_INTR(dev->parent, child, cookie); 2581 } 2582 2583 int 2584 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig, 2585 enum intr_polarity pol) 2586 { 2587 /* Propagate up the bus hierarchy until someone handles it. */ 2588 if (dev->parent) 2589 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol)); 2590 else 2591 return(EINVAL); 2592 } 2593 2594 struct resource * 2595 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 2596 u_long start, u_long end, u_long count, u_int flags) 2597 { 2598 /* Propagate up the bus hierarchy until someone handles it. */ 2599 if (dev->parent) 2600 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 2601 start, end, count, flags)); 2602 else 2603 return(NULL); 2604 } 2605 2606 int 2607 bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 2608 struct resource *r) 2609 { 2610 /* Propagate up the bus hierarchy until someone handles it. */ 2611 if (dev->parent) 2612 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r)); 2613 else 2614 return(EINVAL); 2615 } 2616 2617 int 2618 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 2619 struct resource *r) 2620 { 2621 /* Propagate up the bus hierarchy until someone handles it. */ 2622 if (dev->parent) 2623 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r)); 2624 else 2625 return(EINVAL); 2626 } 2627 2628 int 2629 bus_generic_deactivate_resource(device_t dev, device_t child, int type, 2630 int rid, struct resource *r) 2631 { 2632 /* Propagate up the bus hierarchy until someone handles it. */ 2633 if (dev->parent) 2634 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 2635 r)); 2636 else 2637 return(EINVAL); 2638 } 2639 2640 int 2641 bus_generic_get_resource(device_t dev, device_t child, int type, int rid, 2642 u_long *startp, u_long *countp) 2643 { 2644 int error; 2645 2646 error = ENOENT; 2647 if (dev->parent) { 2648 error = BUS_GET_RESOURCE(dev->parent, child, type, rid, 2649 startp, countp); 2650 } 2651 return (error); 2652 } 2653 2654 int 2655 bus_generic_set_resource(device_t dev, device_t child, int type, int rid, 2656 u_long start, u_long count) 2657 { 2658 int error; 2659 2660 error = EINVAL; 2661 if (dev->parent) { 2662 error = BUS_SET_RESOURCE(dev->parent, child, type, rid, 2663 start, count); 2664 } 2665 return (error); 2666 } 2667 2668 void 2669 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid) 2670 { 2671 if (dev->parent) 2672 BUS_DELETE_RESOURCE(dev, child, type, rid); 2673 } 2674 2675 int 2676 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 2677 u_long *startp, u_long *countp) 2678 { 2679 struct resource_list *rl = NULL; 2680 struct resource_list_entry *rle = NULL; 2681 2682 rl = BUS_GET_RESOURCE_LIST(dev, child); 2683 if (!rl) 2684 return(EINVAL); 2685 2686 rle = resource_list_find(rl, type, rid); 2687 if (!rle) 2688 return(ENOENT); 2689 2690 if (startp) 2691 *startp = rle->start; 2692 if (countp) 2693 *countp = rle->count; 2694 2695 return(0); 2696 } 2697 2698 int 2699 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 2700 u_long start, u_long count) 2701 { 2702 struct resource_list *rl = NULL; 2703 2704 rl = BUS_GET_RESOURCE_LIST(dev, child); 2705 if (!rl) 2706 return(EINVAL); 2707 2708 resource_list_add(rl, type, rid, start, (start + count - 1), count); 2709 2710 return(0); 2711 } 2712 2713 void 2714 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 2715 { 2716 struct resource_list *rl = NULL; 2717 2718 rl = BUS_GET_RESOURCE_LIST(dev, child); 2719 if (!rl) 2720 return; 2721 2722 resource_list_delete(rl, type, rid); 2723 } 2724 2725 int 2726 bus_generic_rl_release_resource(device_t dev, device_t child, int type, 2727 int rid, struct resource *r) 2728 { 2729 struct resource_list *rl = NULL; 2730 2731 rl = BUS_GET_RESOURCE_LIST(dev, child); 2732 if (!rl) 2733 return(EINVAL); 2734 2735 return(resource_list_release(rl, dev, child, type, rid, r)); 2736 } 2737 2738 struct resource * 2739 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 2740 int *rid, u_long start, u_long end, u_long count, u_int flags) 2741 { 2742 struct resource_list *rl = NULL; 2743 2744 rl = BUS_GET_RESOURCE_LIST(dev, child); 2745 if (!rl) 2746 return(NULL); 2747 2748 return(resource_list_alloc(rl, dev, child, type, rid, 2749 start, end, count, flags)); 2750 } 2751 2752 int 2753 bus_generic_child_present(device_t bus, device_t child) 2754 { 2755 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus)); 2756 } 2757 2758 2759 /* 2760 * Some convenience functions to make it easier for drivers to use the 2761 * resource-management functions. All these really do is hide the 2762 * indirection through the parent's method table, making for slightly 2763 * less-wordy code. In the future, it might make sense for this code 2764 * to maintain some sort of a list of resources allocated by each device. 2765 */ 2766 int 2767 bus_alloc_resources(device_t dev, struct resource_spec *rs, 2768 struct resource **res) 2769 { 2770 int i; 2771 2772 for (i = 0; rs[i].type != -1; i++) 2773 res[i] = NULL; 2774 for (i = 0; rs[i].type != -1; i++) { 2775 res[i] = bus_alloc_resource_any(dev, 2776 rs[i].type, &rs[i].rid, rs[i].flags); 2777 if (res[i] == NULL) { 2778 bus_release_resources(dev, rs, res); 2779 return (ENXIO); 2780 } 2781 } 2782 return (0); 2783 } 2784 2785 void 2786 bus_release_resources(device_t dev, const struct resource_spec *rs, 2787 struct resource **res) 2788 { 2789 int i; 2790 2791 for (i = 0; rs[i].type != -1; i++) 2792 if (res[i] != NULL) { 2793 bus_release_resource( 2794 dev, rs[i].type, rs[i].rid, res[i]); 2795 res[i] = NULL; 2796 } 2797 } 2798 2799 struct resource * 2800 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 2801 u_long count, u_int flags) 2802 { 2803 if (dev->parent == 0) 2804 return(0); 2805 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 2806 count, flags)); 2807 } 2808 2809 int 2810 bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 2811 { 2812 if (dev->parent == 0) 2813 return(EINVAL); 2814 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2815 } 2816 2817 int 2818 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 2819 { 2820 if (dev->parent == 0) 2821 return(EINVAL); 2822 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2823 } 2824 2825 int 2826 bus_release_resource(device_t dev, int type, int rid, struct resource *r) 2827 { 2828 if (dev->parent == 0) 2829 return(EINVAL); 2830 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 2831 } 2832 2833 int 2834 bus_setup_intr(device_t dev, struct resource *r, int flags, 2835 driver_intr_t handler, void *arg, 2836 void **cookiep, lwkt_serialize_t serializer) 2837 { 2838 if (dev->parent == 0) 2839 return(EINVAL); 2840 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg, 2841 cookiep, serializer)); 2842 } 2843 2844 int 2845 bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 2846 { 2847 if (dev->parent == 0) 2848 return(EINVAL); 2849 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 2850 } 2851 2852 void 2853 bus_enable_intr(device_t dev, void *cookie) 2854 { 2855 if (dev->parent) 2856 BUS_ENABLE_INTR(dev->parent, dev, cookie); 2857 } 2858 2859 int 2860 bus_disable_intr(device_t dev, void *cookie) 2861 { 2862 if (dev->parent) 2863 return(BUS_DISABLE_INTR(dev->parent, dev, cookie)); 2864 else 2865 return(0); 2866 } 2867 2868 int 2869 bus_set_resource(device_t dev, int type, int rid, 2870 u_long start, u_long count) 2871 { 2872 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 2873 start, count)); 2874 } 2875 2876 int 2877 bus_get_resource(device_t dev, int type, int rid, 2878 u_long *startp, u_long *countp) 2879 { 2880 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2881 startp, countp)); 2882 } 2883 2884 u_long 2885 bus_get_resource_start(device_t dev, int type, int rid) 2886 { 2887 u_long start, count; 2888 int error; 2889 2890 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2891 &start, &count); 2892 if (error) 2893 return(0); 2894 return(start); 2895 } 2896 2897 u_long 2898 bus_get_resource_count(device_t dev, int type, int rid) 2899 { 2900 u_long start, count; 2901 int error; 2902 2903 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2904 &start, &count); 2905 if (error) 2906 return(0); 2907 return(count); 2908 } 2909 2910 void 2911 bus_delete_resource(device_t dev, int type, int rid) 2912 { 2913 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 2914 } 2915 2916 int 2917 bus_child_present(device_t child) 2918 { 2919 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 2920 } 2921 2922 int 2923 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 2924 { 2925 device_t parent; 2926 2927 parent = device_get_parent(child); 2928 if (parent == NULL) { 2929 *buf = '\0'; 2930 return (0); 2931 } 2932 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 2933 } 2934 2935 int 2936 bus_child_location_str(device_t child, char *buf, size_t buflen) 2937 { 2938 device_t parent; 2939 2940 parent = device_get_parent(child); 2941 if (parent == NULL) { 2942 *buf = '\0'; 2943 return (0); 2944 } 2945 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 2946 } 2947 2948 static int 2949 root_print_child(device_t dev, device_t child) 2950 { 2951 return(0); 2952 } 2953 2954 static int 2955 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 2956 void **cookiep, lwkt_serialize_t serializer) 2957 { 2958 /* 2959 * If an interrupt mapping gets to here something bad has happened. 2960 */ 2961 panic("root_setup_intr"); 2962 } 2963 2964 /* 2965 * If we get here, assume that the device is permanant and really is 2966 * present in the system. Removable bus drivers are expected to intercept 2967 * this call long before it gets here. We return -1 so that drivers that 2968 * really care can check vs -1 or some ERRNO returned higher in the food 2969 * chain. 2970 */ 2971 static int 2972 root_child_present(device_t dev, device_t child) 2973 { 2974 return(-1); 2975 } 2976 2977 /* 2978 * XXX NOTE! other defaults may be set in bus_if.m 2979 */ 2980 static kobj_method_t root_methods[] = { 2981 /* Device interface */ 2982 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 2983 KOBJMETHOD(device_suspend, bus_generic_suspend), 2984 KOBJMETHOD(device_resume, bus_generic_resume), 2985 2986 /* Bus interface */ 2987 KOBJMETHOD(bus_add_child, bus_generic_add_child), 2988 KOBJMETHOD(bus_print_child, root_print_child), 2989 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 2990 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 2991 KOBJMETHOD(bus_setup_intr, root_setup_intr), 2992 KOBJMETHOD(bus_child_present, root_child_present), 2993 2994 { 0, 0 } 2995 }; 2996 2997 static driver_t root_driver = { 2998 "root", 2999 root_methods, 3000 1, /* no softc */ 3001 }; 3002 3003 device_t root_bus; 3004 devclass_t root_devclass; 3005 3006 static int 3007 root_bus_module_handler(module_t mod, int what, void* arg) 3008 { 3009 switch (what) { 3010 case MOD_LOAD: 3011 TAILQ_INIT(&bus_data_devices); 3012 root_bus = make_device(NULL, "root", 0); 3013 root_bus->desc = "System root bus"; 3014 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 3015 root_bus->driver = &root_driver; 3016 root_bus->state = DS_ALIVE; 3017 root_devclass = devclass_find_internal("root", NULL, FALSE); 3018 devinit(); 3019 return(0); 3020 3021 case MOD_SHUTDOWN: 3022 device_shutdown(root_bus); 3023 return(0); 3024 default: 3025 return(0); 3026 } 3027 } 3028 3029 static moduledata_t root_bus_mod = { 3030 "rootbus", 3031 root_bus_module_handler, 3032 0 3033 }; 3034 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 3035 3036 void 3037 root_bus_configure(void) 3038 { 3039 int warncount; 3040 device_t dev; 3041 3042 PDEBUG((".")); 3043 3044 /* 3045 * handle device_identify based device attachments to the root_bus 3046 * (typically nexus). 3047 */ 3048 bus_generic_probe(root_bus); 3049 3050 /* 3051 * Probe and attach the devices under root_bus. 3052 */ 3053 TAILQ_FOREACH(dev, &root_bus->children, link) { 3054 device_probe_and_attach(dev); 3055 } 3056 3057 /* 3058 * Wait for all asynchronous attaches to complete. If we don't 3059 * our legacy ISA bus scan could steal device unit numbers or 3060 * even I/O ports. 3061 */ 3062 warncount = 10; 3063 if (numasyncthreads) 3064 kprintf("Waiting for async drivers to attach\n"); 3065 while (numasyncthreads > 0) { 3066 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK) 3067 --warncount; 3068 if (warncount == 0) { 3069 kprintf("Warning: Still waiting for %d " 3070 "drivers to attach\n", numasyncthreads); 3071 } else if (warncount == -30) { 3072 kprintf("Giving up on %d drivers\n", numasyncthreads); 3073 break; 3074 } 3075 } 3076 root_bus->state = DS_ATTACHED; 3077 } 3078 3079 int 3080 driver_module_handler(module_t mod, int what, void *arg) 3081 { 3082 int error; 3083 struct driver_module_data *dmd; 3084 devclass_t bus_devclass; 3085 kobj_class_t driver; 3086 const char *parentname; 3087 3088 dmd = (struct driver_module_data *)arg; 3089 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 3090 error = 0; 3091 3092 switch (what) { 3093 case MOD_LOAD: 3094 if (dmd->dmd_chainevh) 3095 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3096 3097 driver = dmd->dmd_driver; 3098 PDEBUG(("Loading module: driver %s on bus %s", 3099 DRIVERNAME(driver), dmd->dmd_busname)); 3100 3101 /* 3102 * If the driver has any base classes, make the 3103 * devclass inherit from the devclass of the driver's 3104 * first base class. This will allow the system to 3105 * search for drivers in both devclasses for children 3106 * of a device using this driver. 3107 */ 3108 if (driver->baseclasses) 3109 parentname = driver->baseclasses[0]->name; 3110 else 3111 parentname = NULL; 3112 *dmd->dmd_devclass = devclass_find_internal(driver->name, 3113 parentname, TRUE); 3114 3115 error = devclass_add_driver(bus_devclass, driver); 3116 if (error) 3117 break; 3118 break; 3119 3120 case MOD_UNLOAD: 3121 PDEBUG(("Unloading module: driver %s from bus %s", 3122 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname)); 3123 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver); 3124 3125 if (!error && dmd->dmd_chainevh) 3126 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3127 break; 3128 } 3129 3130 return (error); 3131 } 3132 3133 #ifdef BUS_DEBUG 3134 3135 /* 3136 * The _short versions avoid iteration by not calling anything that prints 3137 * more than oneliners. I love oneliners. 3138 */ 3139 3140 static void 3141 print_device_short(device_t dev, int indent) 3142 { 3143 if (!dev) 3144 return; 3145 3146 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 3147 dev->unit, dev->desc, 3148 (dev->parent? "":"no "), 3149 (TAILQ_EMPTY(&dev->children)? "no ":""), 3150 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 3151 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 3152 (dev->flags&DF_WILDCARD? "wildcard,":""), 3153 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 3154 (dev->ivars? "":"no "), 3155 (dev->softc? "":"no "), 3156 dev->busy)); 3157 } 3158 3159 static void 3160 print_device(device_t dev, int indent) 3161 { 3162 if (!dev) 3163 return; 3164 3165 print_device_short(dev, indent); 3166 3167 indentprintf(("Parent:\n")); 3168 print_device_short(dev->parent, indent+1); 3169 indentprintf(("Driver:\n")); 3170 print_driver_short(dev->driver, indent+1); 3171 indentprintf(("Devclass:\n")); 3172 print_devclass_short(dev->devclass, indent+1); 3173 } 3174 3175 /* 3176 * Print the device and all its children (indented). 3177 */ 3178 void 3179 print_device_tree_short(device_t dev, int indent) 3180 { 3181 device_t child; 3182 3183 if (!dev) 3184 return; 3185 3186 print_device_short(dev, indent); 3187 3188 TAILQ_FOREACH(child, &dev->children, link) 3189 print_device_tree_short(child, indent+1); 3190 } 3191 3192 /* 3193 * Print the device and all its children (indented). 3194 */ 3195 void 3196 print_device_tree(device_t dev, int indent) 3197 { 3198 device_t child; 3199 3200 if (!dev) 3201 return; 3202 3203 print_device(dev, indent); 3204 3205 TAILQ_FOREACH(child, &dev->children, link) 3206 print_device_tree(child, indent+1); 3207 } 3208 3209 static void 3210 print_driver_short(driver_t *driver, int indent) 3211 { 3212 if (!driver) 3213 return; 3214 3215 indentprintf(("driver %s: softc size = %zu\n", 3216 driver->name, driver->size)); 3217 } 3218 3219 static void 3220 print_driver(driver_t *driver, int indent) 3221 { 3222 if (!driver) 3223 return; 3224 3225 print_driver_short(driver, indent); 3226 } 3227 3228 3229 static void 3230 print_driver_list(driver_list_t drivers, int indent) 3231 { 3232 driverlink_t driver; 3233 3234 TAILQ_FOREACH(driver, &drivers, link) 3235 print_driver(driver->driver, indent); 3236 } 3237 3238 static void 3239 print_devclass_short(devclass_t dc, int indent) 3240 { 3241 if (!dc) 3242 return; 3243 3244 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 3245 } 3246 3247 static void 3248 print_devclass(devclass_t dc, int indent) 3249 { 3250 int i; 3251 3252 if (!dc) 3253 return; 3254 3255 print_devclass_short(dc, indent); 3256 indentprintf(("Drivers:\n")); 3257 print_driver_list(dc->drivers, indent+1); 3258 3259 indentprintf(("Devices:\n")); 3260 for (i = 0; i < dc->maxunit; i++) 3261 if (dc->devices[i]) 3262 print_device(dc->devices[i], indent+1); 3263 } 3264 3265 void 3266 print_devclass_list_short(void) 3267 { 3268 devclass_t dc; 3269 3270 kprintf("Short listing of devclasses, drivers & devices:\n"); 3271 TAILQ_FOREACH(dc, &devclasses, link) { 3272 print_devclass_short(dc, 0); 3273 } 3274 } 3275 3276 void 3277 print_devclass_list(void) 3278 { 3279 devclass_t dc; 3280 3281 kprintf("Full listing of devclasses, drivers & devices:\n"); 3282 TAILQ_FOREACH(dc, &devclasses, link) { 3283 print_devclass(dc, 0); 3284 } 3285 } 3286 3287 #endif 3288 3289 /* 3290 * Check to see if a device is disabled via a disabled hint. 3291 */ 3292 int 3293 resource_disabled(const char *name, int unit) 3294 { 3295 int error, value; 3296 3297 error = resource_int_value(name, unit, "disabled", &value); 3298 if (error) 3299 return(0); 3300 return(value); 3301 } 3302 3303 /* 3304 * User-space access to the device tree. 3305 * 3306 * We implement a small set of nodes: 3307 * 3308 * hw.bus Single integer read method to obtain the 3309 * current generation count. 3310 * hw.bus.devices Reads the entire device tree in flat space. 3311 * hw.bus.rman Resource manager interface 3312 * 3313 * We might like to add the ability to scan devclasses and/or drivers to 3314 * determine what else is currently loaded/available. 3315 */ 3316 3317 static int 3318 sysctl_bus(SYSCTL_HANDLER_ARGS) 3319 { 3320 struct u_businfo ubus; 3321 3322 ubus.ub_version = BUS_USER_VERSION; 3323 ubus.ub_generation = bus_data_generation; 3324 3325 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 3326 } 3327 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 3328 "bus-related data"); 3329 3330 static int 3331 sysctl_devices(SYSCTL_HANDLER_ARGS) 3332 { 3333 int *name = (int *)arg1; 3334 u_int namelen = arg2; 3335 int index; 3336 struct device *dev; 3337 struct u_device udev; /* XXX this is a bit big */ 3338 int error; 3339 3340 if (namelen != 2) 3341 return (EINVAL); 3342 3343 if (bus_data_generation_check(name[0])) 3344 return (EINVAL); 3345 3346 index = name[1]; 3347 3348 /* 3349 * Scan the list of devices, looking for the requested index. 3350 */ 3351 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 3352 if (index-- == 0) 3353 break; 3354 } 3355 if (dev == NULL) 3356 return (ENOENT); 3357 3358 /* 3359 * Populate the return array. 3360 */ 3361 bzero(&udev, sizeof(udev)); 3362 udev.dv_handle = (uintptr_t)dev; 3363 udev.dv_parent = (uintptr_t)dev->parent; 3364 if (dev->nameunit != NULL) 3365 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name)); 3366 if (dev->desc != NULL) 3367 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc)); 3368 if (dev->driver != NULL && dev->driver->name != NULL) 3369 strlcpy(udev.dv_drivername, dev->driver->name, 3370 sizeof(udev.dv_drivername)); 3371 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo)); 3372 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location)); 3373 udev.dv_devflags = dev->devflags; 3374 udev.dv_flags = dev->flags; 3375 udev.dv_state = dev->state; 3376 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 3377 return (error); 3378 } 3379 3380 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 3381 "system device tree"); 3382 3383 int 3384 bus_data_generation_check(int generation) 3385 { 3386 if (generation != bus_data_generation) 3387 return (1); 3388 3389 /* XXX generate optimised lists here? */ 3390 return (0); 3391 } 3392 3393 void 3394 bus_data_generation_update(void) 3395 { 3396 bus_data_generation++; 3397 } 3398 3399 const char * 3400 intr_str_polarity(enum intr_polarity pola) 3401 { 3402 switch (pola) { 3403 case INTR_POLARITY_LOW: 3404 return "low"; 3405 3406 case INTR_POLARITY_HIGH: 3407 return "high"; 3408 3409 case INTR_POLARITY_CONFORM: 3410 return "conform"; 3411 } 3412 return "unknown"; 3413 } 3414 3415 const char * 3416 intr_str_trigger(enum intr_trigger trig) 3417 { 3418 switch (trig) { 3419 case INTR_TRIGGER_EDGE: 3420 return "edge"; 3421 3422 case INTR_TRIGGER_LEVEL: 3423 return "level"; 3424 3425 case INTR_TRIGGER_CONFORM: 3426 return "conform"; 3427 } 3428 return "unknown"; 3429 } 3430