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, int type, int rid, 2160 u_long start, u_long end, u_long count, int cpuid) 2161 { 2162 struct resource_list_entry *rle; 2163 2164 rle = resource_list_find(rl, type, rid); 2165 if (rle == NULL) { 2166 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS, 2167 M_INTWAIT); 2168 if (!rle) 2169 panic("resource_list_add: can't record entry"); 2170 SLIST_INSERT_HEAD(rl, rle, link); 2171 rle->type = type; 2172 rle->rid = rid; 2173 rle->res = NULL; 2174 rle->cpuid = -1; 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 if (cpuid != -1) { 2185 if (rle->cpuid != -1 && rle->cpuid != cpuid) { 2186 panic("resource_list_add: moving from cpu%d -> cpu%d\n", 2187 rle->cpuid, cpuid); 2188 } 2189 rle->cpuid = cpuid; 2190 } 2191 } 2192 2193 struct resource_list_entry* 2194 resource_list_find(struct resource_list *rl, 2195 int type, int rid) 2196 { 2197 struct resource_list_entry *rle; 2198 2199 SLIST_FOREACH(rle, rl, link) 2200 if (rle->type == type && rle->rid == rid) 2201 return(rle); 2202 return(NULL); 2203 } 2204 2205 void 2206 resource_list_delete(struct resource_list *rl, 2207 int type, int rid) 2208 { 2209 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 2210 2211 if (rle) { 2212 if (rle->res != NULL) 2213 panic("resource_list_delete: resource has not been released"); 2214 SLIST_REMOVE(rl, rle, resource_list_entry, link); 2215 kfree(rle, M_BUS); 2216 } 2217 } 2218 2219 struct resource * 2220 resource_list_alloc(struct resource_list *rl, 2221 device_t bus, device_t child, 2222 int type, int *rid, 2223 u_long start, u_long end, 2224 u_long count, u_int flags, int cpuid) 2225 { 2226 struct resource_list_entry *rle = 0; 2227 int passthrough = (device_get_parent(child) != bus); 2228 int isdefault = (start == 0UL && end == ~0UL); 2229 2230 if (passthrough) { 2231 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2232 type, rid, 2233 start, end, count, flags, cpuid)); 2234 } 2235 2236 rle = resource_list_find(rl, type, *rid); 2237 2238 if (!rle) 2239 return(0); /* no resource of that type/rid */ 2240 2241 if (rle->res) 2242 panic("resource_list_alloc: resource entry is busy"); 2243 2244 if (isdefault) { 2245 start = rle->start; 2246 count = max(count, rle->count); 2247 end = max(rle->end, start + count - 1); 2248 } 2249 cpuid = rle->cpuid; 2250 2251 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 2252 type, rid, start, end, count, 2253 flags, cpuid); 2254 2255 /* 2256 * Record the new range. 2257 */ 2258 if (rle->res) { 2259 rle->start = rman_get_start(rle->res); 2260 rle->end = rman_get_end(rle->res); 2261 rle->count = count; 2262 } 2263 2264 return(rle->res); 2265 } 2266 2267 int 2268 resource_list_release(struct resource_list *rl, 2269 device_t bus, device_t child, 2270 int type, int rid, struct resource *res) 2271 { 2272 struct resource_list_entry *rle = 0; 2273 int passthrough = (device_get_parent(child) != bus); 2274 int error; 2275 2276 if (passthrough) { 2277 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2278 type, rid, res)); 2279 } 2280 2281 rle = resource_list_find(rl, type, rid); 2282 2283 if (!rle) 2284 panic("resource_list_release: can't find resource"); 2285 if (!rle->res) 2286 panic("resource_list_release: resource entry is not busy"); 2287 2288 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 2289 type, rid, res); 2290 if (error) 2291 return(error); 2292 2293 rle->res = NULL; 2294 return(0); 2295 } 2296 2297 int 2298 resource_list_print_type(struct resource_list *rl, const char *name, int type, 2299 const char *format) 2300 { 2301 struct resource_list_entry *rle; 2302 int printed, retval; 2303 2304 printed = 0; 2305 retval = 0; 2306 /* Yes, this is kinda cheating */ 2307 SLIST_FOREACH(rle, rl, link) { 2308 if (rle->type == type) { 2309 if (printed == 0) 2310 retval += kprintf(" %s ", name); 2311 else 2312 retval += kprintf(","); 2313 printed++; 2314 retval += kprintf(format, rle->start); 2315 if (rle->count > 1) { 2316 retval += kprintf("-"); 2317 retval += kprintf(format, rle->start + 2318 rle->count - 1); 2319 } 2320 } 2321 } 2322 return(retval); 2323 } 2324 2325 /* 2326 * Generic driver/device identify functions. These will install a device 2327 * rendezvous point under the parent using the same name as the driver 2328 * name, which will at a later time be probed and attached. 2329 * 2330 * These functions are used when the parent does not 'scan' its bus for 2331 * matching devices, or for the particular devices using these functions, 2332 * or when the device is a pseudo or synthesized device (such as can be 2333 * found under firewire and ppbus). 2334 */ 2335 int 2336 bus_generic_identify(driver_t *driver, device_t parent) 2337 { 2338 if (parent->state == DS_ATTACHED) 2339 return (0); 2340 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1); 2341 return (0); 2342 } 2343 2344 int 2345 bus_generic_identify_sameunit(driver_t *driver, device_t parent) 2346 { 2347 if (parent->state == DS_ATTACHED) 2348 return (0); 2349 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent)); 2350 return (0); 2351 } 2352 2353 /* 2354 * Call DEVICE_IDENTIFY for each driver. 2355 */ 2356 int 2357 bus_generic_probe(device_t dev) 2358 { 2359 devclass_t dc = dev->devclass; 2360 driverlink_t dl; 2361 2362 TAILQ_FOREACH(dl, &dc->drivers, link) { 2363 DEVICE_IDENTIFY(dl->driver, dev); 2364 } 2365 2366 return(0); 2367 } 2368 2369 /* 2370 * This is an aweful hack due to the isa bus and autoconf code not 2371 * probing the ISA devices until after everything else has configured. 2372 * The ISA bus did a dummy attach long ago so we have to set it back 2373 * to an earlier state so the probe thinks its the initial probe and 2374 * not a bus rescan. 2375 * 2376 * XXX remove by properly defering the ISA bus scan. 2377 */ 2378 int 2379 bus_generic_probe_hack(device_t dev) 2380 { 2381 if (dev->state == DS_ATTACHED) { 2382 dev->state = DS_ALIVE; 2383 bus_generic_probe(dev); 2384 dev->state = DS_ATTACHED; 2385 } 2386 return (0); 2387 } 2388 2389 int 2390 bus_generic_attach(device_t dev) 2391 { 2392 device_t child; 2393 2394 TAILQ_FOREACH(child, &dev->children, link) { 2395 device_probe_and_attach(child); 2396 } 2397 2398 return(0); 2399 } 2400 2401 int 2402 bus_generic_detach(device_t dev) 2403 { 2404 device_t child; 2405 int error; 2406 2407 if (dev->state != DS_ATTACHED) 2408 return(EBUSY); 2409 2410 TAILQ_FOREACH(child, &dev->children, link) 2411 if ((error = device_detach(child)) != 0) 2412 return(error); 2413 2414 return 0; 2415 } 2416 2417 int 2418 bus_generic_shutdown(device_t dev) 2419 { 2420 device_t child; 2421 2422 TAILQ_FOREACH(child, &dev->children, link) 2423 device_shutdown(child); 2424 2425 return(0); 2426 } 2427 2428 int 2429 bus_generic_suspend(device_t dev) 2430 { 2431 int error; 2432 device_t child, child2; 2433 2434 TAILQ_FOREACH(child, &dev->children, link) { 2435 error = DEVICE_SUSPEND(child); 2436 if (error) { 2437 for (child2 = TAILQ_FIRST(&dev->children); 2438 child2 && child2 != child; 2439 child2 = TAILQ_NEXT(child2, link)) 2440 DEVICE_RESUME(child2); 2441 return(error); 2442 } 2443 } 2444 return(0); 2445 } 2446 2447 int 2448 bus_generic_resume(device_t dev) 2449 { 2450 device_t child; 2451 2452 TAILQ_FOREACH(child, &dev->children, link) 2453 DEVICE_RESUME(child); 2454 /* if resume fails, there's nothing we can usefully do... */ 2455 2456 return(0); 2457 } 2458 2459 int 2460 bus_print_child_header(device_t dev, device_t child) 2461 { 2462 int retval = 0; 2463 2464 if (device_get_desc(child)) 2465 retval += device_printf(child, "<%s>", device_get_desc(child)); 2466 else 2467 retval += kprintf("%s", device_get_nameunit(child)); 2468 if (bootverbose) { 2469 if (child->state != DS_ATTACHED) 2470 kprintf(" [tentative]"); 2471 else 2472 kprintf(" [attached!]"); 2473 } 2474 return(retval); 2475 } 2476 2477 int 2478 bus_print_child_footer(device_t dev, device_t child) 2479 { 2480 return(kprintf(" on %s\n", device_get_nameunit(dev))); 2481 } 2482 2483 device_t 2484 bus_generic_add_child(device_t dev, device_t child, int order, 2485 const char *name, int unit) 2486 { 2487 if (dev->parent) 2488 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit); 2489 else 2490 dev = device_add_child_ordered(child, order, name, unit); 2491 return(dev); 2492 2493 } 2494 2495 int 2496 bus_generic_print_child(device_t dev, device_t child) 2497 { 2498 int retval = 0; 2499 2500 retval += bus_print_child_header(dev, child); 2501 retval += bus_print_child_footer(dev, child); 2502 2503 return(retval); 2504 } 2505 2506 int 2507 bus_generic_read_ivar(device_t dev, device_t child, int index, 2508 uintptr_t * result) 2509 { 2510 int error; 2511 2512 if (dev->parent) 2513 error = BUS_READ_IVAR(dev->parent, child, index, result); 2514 else 2515 error = ENOENT; 2516 return (error); 2517 } 2518 2519 int 2520 bus_generic_write_ivar(device_t dev, device_t child, int index, 2521 uintptr_t value) 2522 { 2523 int error; 2524 2525 if (dev->parent) 2526 error = BUS_WRITE_IVAR(dev->parent, child, index, value); 2527 else 2528 error = ENOENT; 2529 return (error); 2530 } 2531 2532 /* 2533 * Resource list are used for iterations, do not recurse. 2534 */ 2535 struct resource_list * 2536 bus_generic_get_resource_list(device_t dev, device_t child) 2537 { 2538 return (NULL); 2539 } 2540 2541 void 2542 bus_generic_driver_added(device_t dev, driver_t *driver) 2543 { 2544 device_t child; 2545 2546 DEVICE_IDENTIFY(driver, dev); 2547 TAILQ_FOREACH(child, &dev->children, link) { 2548 if (child->state == DS_NOTPRESENT) 2549 device_probe_and_attach(child); 2550 } 2551 } 2552 2553 int 2554 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 2555 int flags, driver_intr_t *intr, void *arg, 2556 void **cookiep, lwkt_serialize_t serializer) 2557 { 2558 /* Propagate up the bus hierarchy until someone handles it. */ 2559 if (dev->parent) 2560 return(BUS_SETUP_INTR(dev->parent, child, irq, flags, 2561 intr, arg, cookiep, serializer)); 2562 else 2563 return(EINVAL); 2564 } 2565 2566 int 2567 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 2568 void *cookie) 2569 { 2570 /* Propagate up the bus hierarchy until someone handles it. */ 2571 if (dev->parent) 2572 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 2573 else 2574 return(EINVAL); 2575 } 2576 2577 int 2578 bus_generic_disable_intr(device_t dev, device_t child, void *cookie) 2579 { 2580 if (dev->parent) 2581 return(BUS_DISABLE_INTR(dev->parent, child, cookie)); 2582 else 2583 return(0); 2584 } 2585 2586 void 2587 bus_generic_enable_intr(device_t dev, device_t child, void *cookie) 2588 { 2589 if (dev->parent) 2590 BUS_ENABLE_INTR(dev->parent, child, cookie); 2591 } 2592 2593 int 2594 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig, 2595 enum intr_polarity pol) 2596 { 2597 /* Propagate up the bus hierarchy until someone handles it. */ 2598 if (dev->parent) 2599 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol)); 2600 else 2601 return(EINVAL); 2602 } 2603 2604 struct resource * 2605 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 2606 u_long start, u_long end, u_long count, u_int flags, int cpuid) 2607 { 2608 /* Propagate up the bus hierarchy until someone handles it. */ 2609 if (dev->parent) 2610 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 2611 start, end, count, flags, cpuid)); 2612 else 2613 return(NULL); 2614 } 2615 2616 int 2617 bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 2618 struct resource *r) 2619 { 2620 /* Propagate up the bus hierarchy until someone handles it. */ 2621 if (dev->parent) 2622 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r)); 2623 else 2624 return(EINVAL); 2625 } 2626 2627 int 2628 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 2629 struct resource *r) 2630 { 2631 /* Propagate up the bus hierarchy until someone handles it. */ 2632 if (dev->parent) 2633 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r)); 2634 else 2635 return(EINVAL); 2636 } 2637 2638 int 2639 bus_generic_deactivate_resource(device_t dev, device_t child, int type, 2640 int rid, struct resource *r) 2641 { 2642 /* Propagate up the bus hierarchy until someone handles it. */ 2643 if (dev->parent) 2644 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 2645 r)); 2646 else 2647 return(EINVAL); 2648 } 2649 2650 int 2651 bus_generic_get_resource(device_t dev, device_t child, int type, int rid, 2652 u_long *startp, u_long *countp) 2653 { 2654 int error; 2655 2656 error = ENOENT; 2657 if (dev->parent) { 2658 error = BUS_GET_RESOURCE(dev->parent, child, type, rid, 2659 startp, countp); 2660 } 2661 return (error); 2662 } 2663 2664 int 2665 bus_generic_set_resource(device_t dev, device_t child, int type, int rid, 2666 u_long start, u_long count, int cpuid) 2667 { 2668 int error; 2669 2670 error = EINVAL; 2671 if (dev->parent) { 2672 error = BUS_SET_RESOURCE(dev->parent, child, type, rid, 2673 start, count, cpuid); 2674 } 2675 return (error); 2676 } 2677 2678 void 2679 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid) 2680 { 2681 if (dev->parent) 2682 BUS_DELETE_RESOURCE(dev, child, type, rid); 2683 } 2684 2685 int 2686 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 2687 u_long *startp, u_long *countp) 2688 { 2689 struct resource_list *rl = NULL; 2690 struct resource_list_entry *rle = NULL; 2691 2692 rl = BUS_GET_RESOURCE_LIST(dev, child); 2693 if (!rl) 2694 return(EINVAL); 2695 2696 rle = resource_list_find(rl, type, rid); 2697 if (!rle) 2698 return(ENOENT); 2699 2700 if (startp) 2701 *startp = rle->start; 2702 if (countp) 2703 *countp = rle->count; 2704 2705 return(0); 2706 } 2707 2708 int 2709 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 2710 u_long start, u_long count, int cpuid) 2711 { 2712 struct resource_list *rl = NULL; 2713 2714 rl = BUS_GET_RESOURCE_LIST(dev, child); 2715 if (!rl) 2716 return(EINVAL); 2717 2718 resource_list_add(rl, type, rid, start, (start + count - 1), count, 2719 cpuid); 2720 2721 return(0); 2722 } 2723 2724 void 2725 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 2726 { 2727 struct resource_list *rl = NULL; 2728 2729 rl = BUS_GET_RESOURCE_LIST(dev, child); 2730 if (!rl) 2731 return; 2732 2733 resource_list_delete(rl, type, rid); 2734 } 2735 2736 int 2737 bus_generic_rl_release_resource(device_t dev, device_t child, int type, 2738 int rid, struct resource *r) 2739 { 2740 struct resource_list *rl = NULL; 2741 2742 rl = BUS_GET_RESOURCE_LIST(dev, child); 2743 if (!rl) 2744 return(EINVAL); 2745 2746 return(resource_list_release(rl, dev, child, type, rid, r)); 2747 } 2748 2749 struct resource * 2750 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 2751 int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid) 2752 { 2753 struct resource_list *rl = NULL; 2754 2755 rl = BUS_GET_RESOURCE_LIST(dev, child); 2756 if (!rl) 2757 return(NULL); 2758 2759 return(resource_list_alloc(rl, dev, child, type, rid, 2760 start, end, count, flags, cpuid)); 2761 } 2762 2763 int 2764 bus_generic_child_present(device_t bus, device_t child) 2765 { 2766 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus)); 2767 } 2768 2769 2770 /* 2771 * Some convenience functions to make it easier for drivers to use the 2772 * resource-management functions. All these really do is hide the 2773 * indirection through the parent's method table, making for slightly 2774 * less-wordy code. In the future, it might make sense for this code 2775 * to maintain some sort of a list of resources allocated by each device. 2776 */ 2777 int 2778 bus_alloc_resources(device_t dev, struct resource_spec *rs, 2779 struct resource **res) 2780 { 2781 int i; 2782 2783 for (i = 0; rs[i].type != -1; i++) 2784 res[i] = NULL; 2785 for (i = 0; rs[i].type != -1; i++) { 2786 res[i] = bus_alloc_resource_any(dev, 2787 rs[i].type, &rs[i].rid, rs[i].flags); 2788 if (res[i] == NULL) { 2789 bus_release_resources(dev, rs, res); 2790 return (ENXIO); 2791 } 2792 } 2793 return (0); 2794 } 2795 2796 void 2797 bus_release_resources(device_t dev, const struct resource_spec *rs, 2798 struct resource **res) 2799 { 2800 int i; 2801 2802 for (i = 0; rs[i].type != -1; i++) 2803 if (res[i] != NULL) { 2804 bus_release_resource( 2805 dev, rs[i].type, rs[i].rid, res[i]); 2806 res[i] = NULL; 2807 } 2808 } 2809 2810 struct resource * 2811 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 2812 u_long count, u_int flags) 2813 { 2814 if (dev->parent == 0) 2815 return(0); 2816 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 2817 count, flags, -1)); 2818 } 2819 2820 int 2821 bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 2822 { 2823 if (dev->parent == 0) 2824 return(EINVAL); 2825 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2826 } 2827 2828 int 2829 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 2830 { 2831 if (dev->parent == 0) 2832 return(EINVAL); 2833 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 2834 } 2835 2836 int 2837 bus_release_resource(device_t dev, int type, int rid, struct resource *r) 2838 { 2839 if (dev->parent == 0) 2840 return(EINVAL); 2841 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 2842 } 2843 2844 int 2845 bus_setup_intr(device_t dev, struct resource *r, int flags, 2846 driver_intr_t handler, void *arg, 2847 void **cookiep, lwkt_serialize_t serializer) 2848 { 2849 if (dev->parent == 0) 2850 return(EINVAL); 2851 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg, 2852 cookiep, serializer)); 2853 } 2854 2855 int 2856 bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 2857 { 2858 if (dev->parent == 0) 2859 return(EINVAL); 2860 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 2861 } 2862 2863 void 2864 bus_enable_intr(device_t dev, void *cookie) 2865 { 2866 if (dev->parent) 2867 BUS_ENABLE_INTR(dev->parent, dev, cookie); 2868 } 2869 2870 int 2871 bus_disable_intr(device_t dev, void *cookie) 2872 { 2873 if (dev->parent) 2874 return(BUS_DISABLE_INTR(dev->parent, dev, cookie)); 2875 else 2876 return(0); 2877 } 2878 2879 int 2880 bus_set_resource(device_t dev, int type, int rid, 2881 u_long start, u_long count, int cpuid) 2882 { 2883 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 2884 start, count, cpuid)); 2885 } 2886 2887 int 2888 bus_get_resource(device_t dev, int type, int rid, 2889 u_long *startp, u_long *countp) 2890 { 2891 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2892 startp, countp)); 2893 } 2894 2895 u_long 2896 bus_get_resource_start(device_t dev, int type, int rid) 2897 { 2898 u_long start, count; 2899 int error; 2900 2901 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2902 &start, &count); 2903 if (error) 2904 return(0); 2905 return(start); 2906 } 2907 2908 u_long 2909 bus_get_resource_count(device_t dev, int type, int rid) 2910 { 2911 u_long start, count; 2912 int error; 2913 2914 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 2915 &start, &count); 2916 if (error) 2917 return(0); 2918 return(count); 2919 } 2920 2921 void 2922 bus_delete_resource(device_t dev, int type, int rid) 2923 { 2924 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 2925 } 2926 2927 int 2928 bus_child_present(device_t child) 2929 { 2930 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 2931 } 2932 2933 int 2934 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 2935 { 2936 device_t parent; 2937 2938 parent = device_get_parent(child); 2939 if (parent == NULL) { 2940 *buf = '\0'; 2941 return (0); 2942 } 2943 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 2944 } 2945 2946 int 2947 bus_child_location_str(device_t child, char *buf, size_t buflen) 2948 { 2949 device_t parent; 2950 2951 parent = device_get_parent(child); 2952 if (parent == NULL) { 2953 *buf = '\0'; 2954 return (0); 2955 } 2956 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 2957 } 2958 2959 static int 2960 root_print_child(device_t dev, device_t child) 2961 { 2962 return(0); 2963 } 2964 2965 static int 2966 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg, 2967 void **cookiep, lwkt_serialize_t serializer) 2968 { 2969 /* 2970 * If an interrupt mapping gets to here something bad has happened. 2971 */ 2972 panic("root_setup_intr"); 2973 } 2974 2975 /* 2976 * If we get here, assume that the device is permanant and really is 2977 * present in the system. Removable bus drivers are expected to intercept 2978 * this call long before it gets here. We return -1 so that drivers that 2979 * really care can check vs -1 or some ERRNO returned higher in the food 2980 * chain. 2981 */ 2982 static int 2983 root_child_present(device_t dev, device_t child) 2984 { 2985 return(-1); 2986 } 2987 2988 /* 2989 * XXX NOTE! other defaults may be set in bus_if.m 2990 */ 2991 static kobj_method_t root_methods[] = { 2992 /* Device interface */ 2993 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 2994 KOBJMETHOD(device_suspend, bus_generic_suspend), 2995 KOBJMETHOD(device_resume, bus_generic_resume), 2996 2997 /* Bus interface */ 2998 KOBJMETHOD(bus_add_child, bus_generic_add_child), 2999 KOBJMETHOD(bus_print_child, root_print_child), 3000 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 3001 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 3002 KOBJMETHOD(bus_setup_intr, root_setup_intr), 3003 KOBJMETHOD(bus_child_present, root_child_present), 3004 3005 { 0, 0 } 3006 }; 3007 3008 static driver_t root_driver = { 3009 "root", 3010 root_methods, 3011 1, /* no softc */ 3012 }; 3013 3014 device_t root_bus; 3015 devclass_t root_devclass; 3016 3017 static int 3018 root_bus_module_handler(module_t mod, int what, void* arg) 3019 { 3020 switch (what) { 3021 case MOD_LOAD: 3022 TAILQ_INIT(&bus_data_devices); 3023 root_bus = make_device(NULL, "root", 0); 3024 root_bus->desc = "System root bus"; 3025 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 3026 root_bus->driver = &root_driver; 3027 root_bus->state = DS_ALIVE; 3028 root_devclass = devclass_find_internal("root", NULL, FALSE); 3029 devinit(); 3030 return(0); 3031 3032 case MOD_SHUTDOWN: 3033 device_shutdown(root_bus); 3034 return(0); 3035 default: 3036 return(0); 3037 } 3038 } 3039 3040 static moduledata_t root_bus_mod = { 3041 "rootbus", 3042 root_bus_module_handler, 3043 0 3044 }; 3045 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 3046 3047 void 3048 root_bus_configure(void) 3049 { 3050 int warncount; 3051 device_t dev; 3052 3053 PDEBUG((".")); 3054 3055 /* 3056 * handle device_identify based device attachments to the root_bus 3057 * (typically nexus). 3058 */ 3059 bus_generic_probe(root_bus); 3060 3061 /* 3062 * Probe and attach the devices under root_bus. 3063 */ 3064 TAILQ_FOREACH(dev, &root_bus->children, link) { 3065 device_probe_and_attach(dev); 3066 } 3067 3068 /* 3069 * Wait for all asynchronous attaches to complete. If we don't 3070 * our legacy ISA bus scan could steal device unit numbers or 3071 * even I/O ports. 3072 */ 3073 warncount = 10; 3074 if (numasyncthreads) 3075 kprintf("Waiting for async drivers to attach\n"); 3076 while (numasyncthreads > 0) { 3077 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK) 3078 --warncount; 3079 if (warncount == 0) { 3080 kprintf("Warning: Still waiting for %d " 3081 "drivers to attach\n", numasyncthreads); 3082 } else if (warncount == -30) { 3083 kprintf("Giving up on %d drivers\n", numasyncthreads); 3084 break; 3085 } 3086 } 3087 root_bus->state = DS_ATTACHED; 3088 } 3089 3090 int 3091 driver_module_handler(module_t mod, int what, void *arg) 3092 { 3093 int error; 3094 struct driver_module_data *dmd; 3095 devclass_t bus_devclass; 3096 kobj_class_t driver; 3097 const char *parentname; 3098 3099 dmd = (struct driver_module_data *)arg; 3100 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 3101 error = 0; 3102 3103 switch (what) { 3104 case MOD_LOAD: 3105 if (dmd->dmd_chainevh) 3106 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3107 3108 driver = dmd->dmd_driver; 3109 PDEBUG(("Loading module: driver %s on bus %s", 3110 DRIVERNAME(driver), dmd->dmd_busname)); 3111 3112 /* 3113 * If the driver has any base classes, make the 3114 * devclass inherit from the devclass of the driver's 3115 * first base class. This will allow the system to 3116 * search for drivers in both devclasses for children 3117 * of a device using this driver. 3118 */ 3119 if (driver->baseclasses) 3120 parentname = driver->baseclasses[0]->name; 3121 else 3122 parentname = NULL; 3123 *dmd->dmd_devclass = devclass_find_internal(driver->name, 3124 parentname, TRUE); 3125 3126 error = devclass_add_driver(bus_devclass, driver); 3127 if (error) 3128 break; 3129 break; 3130 3131 case MOD_UNLOAD: 3132 PDEBUG(("Unloading module: driver %s from bus %s", 3133 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname)); 3134 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver); 3135 3136 if (!error && dmd->dmd_chainevh) 3137 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 3138 break; 3139 } 3140 3141 return (error); 3142 } 3143 3144 #ifdef BUS_DEBUG 3145 3146 /* 3147 * The _short versions avoid iteration by not calling anything that prints 3148 * more than oneliners. I love oneliners. 3149 */ 3150 3151 static void 3152 print_device_short(device_t dev, int indent) 3153 { 3154 if (!dev) 3155 return; 3156 3157 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 3158 dev->unit, dev->desc, 3159 (dev->parent? "":"no "), 3160 (TAILQ_EMPTY(&dev->children)? "no ":""), 3161 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 3162 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 3163 (dev->flags&DF_WILDCARD? "wildcard,":""), 3164 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 3165 (dev->ivars? "":"no "), 3166 (dev->softc? "":"no "), 3167 dev->busy)); 3168 } 3169 3170 static void 3171 print_device(device_t dev, int indent) 3172 { 3173 if (!dev) 3174 return; 3175 3176 print_device_short(dev, indent); 3177 3178 indentprintf(("Parent:\n")); 3179 print_device_short(dev->parent, indent+1); 3180 indentprintf(("Driver:\n")); 3181 print_driver_short(dev->driver, indent+1); 3182 indentprintf(("Devclass:\n")); 3183 print_devclass_short(dev->devclass, indent+1); 3184 } 3185 3186 /* 3187 * Print the device and all its children (indented). 3188 */ 3189 void 3190 print_device_tree_short(device_t dev, int indent) 3191 { 3192 device_t child; 3193 3194 if (!dev) 3195 return; 3196 3197 print_device_short(dev, indent); 3198 3199 TAILQ_FOREACH(child, &dev->children, link) 3200 print_device_tree_short(child, indent+1); 3201 } 3202 3203 /* 3204 * Print the device and all its children (indented). 3205 */ 3206 void 3207 print_device_tree(device_t dev, int indent) 3208 { 3209 device_t child; 3210 3211 if (!dev) 3212 return; 3213 3214 print_device(dev, indent); 3215 3216 TAILQ_FOREACH(child, &dev->children, link) 3217 print_device_tree(child, indent+1); 3218 } 3219 3220 static void 3221 print_driver_short(driver_t *driver, int indent) 3222 { 3223 if (!driver) 3224 return; 3225 3226 indentprintf(("driver %s: softc size = %zu\n", 3227 driver->name, driver->size)); 3228 } 3229 3230 static void 3231 print_driver(driver_t *driver, int indent) 3232 { 3233 if (!driver) 3234 return; 3235 3236 print_driver_short(driver, indent); 3237 } 3238 3239 3240 static void 3241 print_driver_list(driver_list_t drivers, int indent) 3242 { 3243 driverlink_t driver; 3244 3245 TAILQ_FOREACH(driver, &drivers, link) 3246 print_driver(driver->driver, indent); 3247 } 3248 3249 static void 3250 print_devclass_short(devclass_t dc, int indent) 3251 { 3252 if (!dc) 3253 return; 3254 3255 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 3256 } 3257 3258 static void 3259 print_devclass(devclass_t dc, int indent) 3260 { 3261 int i; 3262 3263 if (!dc) 3264 return; 3265 3266 print_devclass_short(dc, indent); 3267 indentprintf(("Drivers:\n")); 3268 print_driver_list(dc->drivers, indent+1); 3269 3270 indentprintf(("Devices:\n")); 3271 for (i = 0; i < dc->maxunit; i++) 3272 if (dc->devices[i]) 3273 print_device(dc->devices[i], indent+1); 3274 } 3275 3276 void 3277 print_devclass_list_short(void) 3278 { 3279 devclass_t dc; 3280 3281 kprintf("Short listing of devclasses, drivers & devices:\n"); 3282 TAILQ_FOREACH(dc, &devclasses, link) { 3283 print_devclass_short(dc, 0); 3284 } 3285 } 3286 3287 void 3288 print_devclass_list(void) 3289 { 3290 devclass_t dc; 3291 3292 kprintf("Full listing of devclasses, drivers & devices:\n"); 3293 TAILQ_FOREACH(dc, &devclasses, link) { 3294 print_devclass(dc, 0); 3295 } 3296 } 3297 3298 #endif 3299 3300 /* 3301 * Check to see if a device is disabled via a disabled hint. 3302 */ 3303 int 3304 resource_disabled(const char *name, int unit) 3305 { 3306 int error, value; 3307 3308 error = resource_int_value(name, unit, "disabled", &value); 3309 if (error) 3310 return(0); 3311 return(value); 3312 } 3313 3314 /* 3315 * User-space access to the device tree. 3316 * 3317 * We implement a small set of nodes: 3318 * 3319 * hw.bus Single integer read method to obtain the 3320 * current generation count. 3321 * hw.bus.devices Reads the entire device tree in flat space. 3322 * hw.bus.rman Resource manager interface 3323 * 3324 * We might like to add the ability to scan devclasses and/or drivers to 3325 * determine what else is currently loaded/available. 3326 */ 3327 3328 static int 3329 sysctl_bus(SYSCTL_HANDLER_ARGS) 3330 { 3331 struct u_businfo ubus; 3332 3333 ubus.ub_version = BUS_USER_VERSION; 3334 ubus.ub_generation = bus_data_generation; 3335 3336 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 3337 } 3338 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 3339 "bus-related data"); 3340 3341 static int 3342 sysctl_devices(SYSCTL_HANDLER_ARGS) 3343 { 3344 int *name = (int *)arg1; 3345 u_int namelen = arg2; 3346 int index; 3347 struct device *dev; 3348 struct u_device udev; /* XXX this is a bit big */ 3349 int error; 3350 3351 if (namelen != 2) 3352 return (EINVAL); 3353 3354 if (bus_data_generation_check(name[0])) 3355 return (EINVAL); 3356 3357 index = name[1]; 3358 3359 /* 3360 * Scan the list of devices, looking for the requested index. 3361 */ 3362 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 3363 if (index-- == 0) 3364 break; 3365 } 3366 if (dev == NULL) 3367 return (ENOENT); 3368 3369 /* 3370 * Populate the return array. 3371 */ 3372 bzero(&udev, sizeof(udev)); 3373 udev.dv_handle = (uintptr_t)dev; 3374 udev.dv_parent = (uintptr_t)dev->parent; 3375 if (dev->nameunit != NULL) 3376 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name)); 3377 if (dev->desc != NULL) 3378 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc)); 3379 if (dev->driver != NULL && dev->driver->name != NULL) 3380 strlcpy(udev.dv_drivername, dev->driver->name, 3381 sizeof(udev.dv_drivername)); 3382 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo)); 3383 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location)); 3384 udev.dv_devflags = dev->devflags; 3385 udev.dv_flags = dev->flags; 3386 udev.dv_state = dev->state; 3387 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 3388 return (error); 3389 } 3390 3391 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 3392 "system device tree"); 3393 3394 int 3395 bus_data_generation_check(int generation) 3396 { 3397 if (generation != bus_data_generation) 3398 return (1); 3399 3400 /* XXX generate optimised lists here? */ 3401 return (0); 3402 } 3403 3404 void 3405 bus_data_generation_update(void) 3406 { 3407 bus_data_generation++; 3408 } 3409 3410 const char * 3411 intr_str_polarity(enum intr_polarity pola) 3412 { 3413 switch (pola) { 3414 case INTR_POLARITY_LOW: 3415 return "low"; 3416 3417 case INTR_POLARITY_HIGH: 3418 return "high"; 3419 3420 case INTR_POLARITY_CONFORM: 3421 return "conform"; 3422 } 3423 return "unknown"; 3424 } 3425 3426 const char * 3427 intr_str_trigger(enum intr_trigger trig) 3428 { 3429 switch (trig) { 3430 case INTR_TRIGGER_EDGE: 3431 return "edge"; 3432 3433 case INTR_TRIGGER_LEVEL: 3434 return "level"; 3435 3436 case INTR_TRIGGER_CONFORM: 3437 return "conform"; 3438 } 3439 return "unknown"; 3440 } 3441