1/*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1997,1998,2003 Doug Rabson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30 31#include <sys/param.h> 32#include <sys/kobj.h> 33#include <sys/malloc.h> 34#include <sys/queue.h> 35#include <sys/systm.h> 36#include <sys/bus.h> 37#include <sys/mutex.h> 38#include "los_hwi.h" 39 40#ifndef BOOTVERBOSE 41#define BOOTVERBOSE 0 42#endif 43int bootverbose = BOOTVERBOSE; 44 45/* 46 * * Used to attach drivers to devclasses. 47 * */ 48typedef struct driverlink *driverlink_t; 49struct driverlink { 50 kobj_class_t driver; 51 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */ 52 int pass; 53 TAILQ_ENTRY(driverlink) passlink; 54}; 55 56/* 57 * Forward declarations 58 */ 59typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t; 60typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t; 61typedef TAILQ_HEAD(device_list, device) device_list_t; 62 63struct devclass { 64 TAILQ_ENTRY(devclass) link; 65 devclass_t parent; /* parent in devclass hierarchy */ 66 driver_list_t drivers; /* bus devclasses store drivers for bus */ 67 char *name; 68 device_t *devices; /* array of devices indexed by unit */ 69 int maxunit; /* size of devices array */ 70 int flags; 71#define DC_HAS_CHILDREN 1 72}; 73 74/** 75 * @brief Implementation of device. 76 */ 77struct device { 78 /* 79 * A device is a kernel object. The first field must be the 80 * current ops table for the object. 81 */ 82 KOBJ_FIELDS; 83 84 /* 85 * Device hierarchy. 86 */ 87 TAILQ_ENTRY(device) link; /**< list of devices in parent */ 88 TAILQ_ENTRY(device) devlink; /**< global device list membership */ 89 device_t parent; /**< parent of this device */ 90 device_list_t children; /**< list of child devices */ 91 92 /* 93 * Details of this device. 94 */ 95 driver_t *driver; /**< current driver */ 96 devclass_t devclass; /**< current device class */ 97 int unit; /**< current unit number */ 98 char* nameunit; /**< name+unit e.g. foodev0 */ 99 char* desc; /**< driver specific description */ 100 int busy; /**< count of calls to device_busy() */ 101 device_state_t state; /**< current device state */ 102 uint32_t devflags; /**< api level flags for device_get_flags() */ 103 u_int flags; /**< internal device flags */ 104 u_int order; /**< order from device_add_child_ordered() */ 105 void *ivars; /**< instance variables */ 106 void *softc; /**< current driver's variables */ 107}; 108 109static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 110 111#ifdef BUS_DEBUG 112static int bus_debug = 1; 113 114#define PDEBUG(a) if (bus_debug) {PRINTK("%s:%d: ", __func__, __LINE__), PRINTK a; PRINTK("\n");} 115#define DEVICENAME(d) ((d)? device_get_name(d): "no device") 116#define DRIVERNAME(d) ((d)? d->name : "no driver") 117#define DEVCLANAME(d) ((d)? d->name : "no devclass") 118 119/** 120 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to 121 * prevent syslog from deleting initial spaces 122 */ 123#define indentprintf(p) do { int iJ; PRINTK("."); for (iJ=0; iJ<indent; iJ++) PRINTK(" "); PRINTK p ; } while (0) 124 125static void print_device_short(device_t dev, int indent); 126static void print_device(device_t dev, int indent); 127void print_device_tree_short(device_t dev, int indent); 128void print_device_tree(device_t dev, int indent); 129static void print_driver_short(driver_t *driver, int indent); 130static void print_driver(driver_t *driver, int indent); 131static void print_driver_list(driver_list_t drivers, int indent); 132static void print_devclass_short(devclass_t dc, int indent); 133static void print_devclass(devclass_t dc, int indent); 134void print_devclass_list_short(void); 135void print_devclass_list(void); 136 137#else 138/* Make the compiler ignore the function calls */ 139#define PDEBUG(a) /* nop */ 140#define DEVICENAME(d) /* nop */ 141#define DRIVERNAME(d) /* nop */ 142#define DEVCLANAME(d) /* nop */ 143 144#define print_device_short(d,i) /* nop */ 145#define print_device(d,i) /* nop */ 146#define print_device_tree_short(d,i) /* nop */ 147#define print_device_tree(d,i) /* nop */ 148#define print_driver_short(d,i) /* nop */ 149#define print_driver(d,i) /* nop */ 150#define print_driver_list(d,i) /* nop */ 151#define print_devclass_short(d,i) /* nop */ 152#define print_devclass(d,i) /* nop */ 153#define print_devclass_list_short() /* nop */ 154#define print_devclass_list() /* nop */ 155#endif 156 157static TAILQ_HEAD(,device) bus_data_devices; 158static int bus_data_generation = 1; 159 160static kobj_method_t null_methods[] = { 161 KOBJMETHOD_END 162}; 163 164DEFINE_CLASS(null, null_methods, 0); 165 166/* 167 * Bus pass implementation 168 */ 169 170static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes); 171int bus_current_pass = BUS_PASS_ROOT; 172 173/** 174 * @brief Return the name of the device's devclass or @c NULL if there 175 * is none. 176 */ 177const char * 178device_get_name(device_t dev) 179{ 180 if (dev != NULL && dev->devclass) 181 return (devclass_get_name(dev->devclass)); 182 return (NULL); 183} 184 185/** 186 * @internal 187 * @brief Register the pass level of a new driver attachment 188 * 189 * Register a new driver attachment's pass level. If no driver 190 * attachment with the same pass level has been added, then @p new 191 * will be added to the global passes list. 192 * 193 * @param new the new driver attachment 194 */ 195static void 196driver_register_pass(struct driverlink *new) 197{ 198 struct driverlink *dl = NULL; 199 200 /* We only consider pass numbers during boot. */ 201 if (bus_current_pass == BUS_PASS_DEFAULT) 202 return; 203 204 /* 205 * Walk the passes list. If we already know about this pass 206 * then there is nothing to do. If we don't, then insert this 207 * driver link into the list. 208 */ 209 TAILQ_FOREACH(dl, &passes, passlink) { 210 if (dl->pass < new->pass) 211 continue; 212 if (dl->pass == new->pass) 213 return; 214 TAILQ_INSERT_BEFORE(dl, new, passlink); 215 return; 216 } 217 TAILQ_INSERT_TAIL(&passes, new, passlink); 218} 219 220/** 221 * @brief Raise the current bus pass 222 * 223 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS() 224 * method on the root bus to kick off a new device tree scan for each 225 * new pass level that has at least one driver. 226 */ 227void 228bus_set_pass(int pass) 229{ 230 struct driverlink *dl = NULL; 231 232 if (bus_current_pass > pass) 233 panic("Attempt to lower bus pass level"); 234 235 TAILQ_FOREACH(dl, &passes, passlink) { 236 /* Skip pass values below the current pass level. */ 237 if (dl->pass <= bus_current_pass) 238 continue; 239 240 /* 241 * Bail once we hit a driver with a pass level that is 242 * too high. 243 */ 244 if (dl->pass > pass) 245 break; 246 247 /* 248 * Raise the pass level to the next level and rescan 249 * the tree. 250 */ 251 bus_current_pass = dl->pass; 252 BUS_NEW_PASS(root_bus); 253 } 254 255 /* 256 * If there isn't a driver registered for the requested pass, 257 * then bus_current_pass might still be less than 'pass'. Set 258 * it to 'pass' in that case. 259 */ 260 if (bus_current_pass < pass) 261 bus_current_pass = pass; 262 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level")); 263} 264 265/* 266 * Devclass implementation 267 */ 268 269static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 270 271/** 272 * @internal 273 * @brief Find or create a device class 274 * 275 * If a device class with the name @p classname exists, return it, 276 * otherwise if @p create is non-zero create and return a new device 277 * class. 278 * 279 * If @p parentname is non-NULL, the parent of the devclass is set to 280 * the devclass of that name. 281 * 282 * @param classname the devclass name to find or create 283 * @param parentname the parent devclass name or @c NULL 284 * @param create non-zero to create a devclass 285 */ 286static devclass_t 287devclass_find_internal(const char *classname, const char *parentname, 288 int create) 289{ 290 devclass_t dc; 291 292 PDEBUG(("looking for %s", classname)); 293 if (!classname) 294 return (NULL); 295 296 TAILQ_FOREACH(dc, &devclasses, link) { 297 if (!strcmp(dc->name, classname)) 298 break; 299 } 300 301 if (create && !dc) { 302 dc = bsd_malloc(sizeof(struct devclass) + strlen(classname) + 1, 303 M_BUS, M_NOWAIT | M_ZERO); 304 if (!dc) 305 return (NULL); 306 dc->parent = NULL; 307 dc->name = (char*) (dc + 1); 308 if (strcpy_s(dc->name, strlen(classname) + 1, classname) != EOK) { 309 bsd_free(dc, M_BUS); 310 return (NULL); 311 } 312 TAILQ_INIT(&dc->drivers); 313 TAILQ_INSERT_TAIL(&devclasses, dc, link); 314 315 PDEBUG(("create dc %p, %s", dc, classname)); 316 bus_data_generation_update(); 317 } 318 319 /* 320 * If a parent class is specified, then set that as our parent so 321 * that this devclass will support drivers for the parent class as 322 * well. If the parent class has the same name don't do this though 323 * as it creates a cycle that can trigger an infinite loop in 324 * device_probe_child() if a device exists for which there is no 325 * suitable driver. 326 */ 327 if (parentname && dc && !dc->parent && 328 strcmp(classname, parentname) != 0) { 329 dc->parent = devclass_find_internal(parentname, NULL, TRUE); 330 if (dc->parent == NULL) { 331 return (NULL); 332 } 333 dc->parent->flags = (unsigned int)dc->parent->flags | DC_HAS_CHILDREN; 334 } 335 336 PDEBUG(("found dc %p, %s", dc, classname)); 337 return (dc); 338} 339 340/** 341 * @brief Create a device class 342 * 343 * If a device class with the name @p classname exists, return it, 344 * otherwise create and return a new device class. 345 * 346 * @param classname the devclass name to find or create 347 */ 348devclass_t 349devclass_create(const char *classname) 350{ 351 return (devclass_find_internal(classname, NULL, TRUE)); 352} 353 354/** 355 * @brief Find a device class 356 * 357 * If a device class with the name @p classname exists, return it, 358 * otherwise return @c NULL. 359 * 360 * @param classname the devclass name to find 361 */ 362devclass_t 363devclass_find(const char *classname) 364{ 365 return (devclass_find_internal(classname, NULL, FALSE)); 366} 367 368/** 369 * @brief Register that a device driver has been added to a devclass 370 * 371 * Register that a device driver has been added to a devclass. This 372 * is called by devclass_add_driver to accomplish the recursive 373 * notification of all the children classes of dc, as well as dc. 374 * Each layer will have BUS_DRIVER_ADDED() called for all instances of 375 * the devclass. 376 * 377 * We do a full search here of the devclass list at each iteration 378 * level to save storing children-lists in the devclass structure. If 379 * we ever move beyond a few dozen devices doing this, we may need to 380 * reevaluate... 381 * 382 * @param dc the devclass to edit 383 * @param driver the driver that was just added 384 */ 385static void 386devclass_driver_added(devclass_t dc, driver_t *driver) 387{ 388 devclass_t parent; 389 int i; 390 391 /* 392 * Call BUS_DRIVER_ADDED for any existing busses in this class. 393 */ 394 for (i = 0; i < dc->maxunit; i++) 395 if (dc->devices[i] && device_is_attached(dc->devices[i])) 396 BUS_DRIVER_ADDED(dc->devices[i], driver); 397 398 /* 399 * Walk through the children classes. Since we only keep a 400 * single parent pointer around, we walk the entire list of 401 * devclasses looking for children. We set the 402 * DC_HAS_CHILDREN flag when a child devclass is created on 403 * the parent, so we only walk the list for those devclasses 404 * that have children. 405 */ 406 if (!((unsigned int)dc->flags & DC_HAS_CHILDREN)) 407 return; 408 parent = dc; 409 TAILQ_FOREACH(dc, &devclasses, link) { 410 if (dc->parent == parent) 411 devclass_driver_added(dc, driver); 412 } 413} 414 415/** 416 * @brief Add a device driver to a device class 417 * 418 * Add a device driver to a devclass. This is normally called 419 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of 420 * all devices in the devclass will be called to allow them to attempt 421 * to re-probe any unmatched children. 422 * 423 * @param dc the devclass to edit 424 * @param driver the driver to register 425 */ 426int 427devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp) 428{ 429 driverlink_t dl = NULL; 430 const char *parentname = NULL; 431 432 PDEBUG(("%s +", DRIVERNAME(driver))); 433 434 /* Don't allow invalid pass values. */ 435 if (pass <= BUS_PASS_ROOT) 436 return (EINVAL); 437 438 dl = bsd_malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 439 if (!dl) 440 return (ENOMEM); 441 442 /* 443 * Compile the driver's methods. Also increase the reference count 444 * so that the class doesn't get freed when the last instance 445 * goes. This means we can safely use static methods and avoids a 446 * double-free in devclass_delete_driver. 447 */ 448 kobj_class_compile((kobj_class_t) driver); 449 450 /* 451 * If the driver has any base classes, make the 452 * devclass inherit from the devclass of the driver's 453 * first base class. This will allow the system to 454 * search for drivers in both devclasses for children 455 * of a device using this driver. 456 */ 457 if (driver->baseclasses) 458 parentname = driver->baseclasses[0]->name; 459 else 460 parentname = NULL; 461 *dcp = devclass_find_internal(driver->name, parentname, TRUE); 462 463 dl->driver = driver; 464 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 465 driver->refs++; /* XXX: kobj_mtx */ 466 dl->pass = pass; 467 driver_register_pass(dl); 468 469 devclass_driver_added(dc, driver); 470 bus_data_generation_update(); 471 PDEBUG(("%s -", DRIVERNAME(driver))); 472 return (0); 473} 474 475/** 476 * @brief Register that a device driver has been deleted from a devclass 477 * 478 * Register that a device driver has been removed from a devclass. 479 * This is called by devclass_delete_driver to accomplish the 480 * recursive notification of all the children classes of busclass, as 481 * well as busclass. Each layer will attempt to detach the driver 482 * from any devices that are children of the bus's devclass. The function 483 * will return an error if a device fails to detach. 484 * 485 * We do a full search here of the devclass list at each iteration 486 * level to save storing children-lists in the devclass structure. If 487 * we ever move beyond a few dozen devices doing this, we may need to 488 * reevaluate... 489 * 490 * @param busclass the devclass of the parent bus 491 * @param dc the devclass of the driver being deleted 492 * @param driver the driver being deleted 493 */ 494static int 495devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver) 496{ 497 devclass_t parent; 498 device_t dev; 499 int error, i; 500 501 /* 502 * Disassociate from any devices. We iterate through all the 503 * devices in the devclass of the driver and detach any which are 504 * using the driver and which have a parent in the devclass which 505 * we are deleting from. 506 * 507 * Note that since a driver can be in multiple devclasses, we 508 * should not detach devices which are not children of devices in 509 * the affected devclass. 510 */ 511 for (i = 0; i < dc->maxunit; i++) { 512 if (dc->devices[i]) { 513 dev = dc->devices[i]; 514 if (dev->driver == driver && dev->parent && 515 dev->parent->devclass == busclass) { 516 if ((error = device_detach(dev)) != 0) 517 return (error); 518 BUS_PROBE_NOMATCH(dev->parent, dev); 519 // devnomatch(dev); 520 dev->flags |= DF_DONENOMATCH; 521 } 522 } 523 } 524 525 /* 526 * Walk through the children classes. Since we only keep a 527 * single parent pointer around, we walk the entire list of 528 * devclasses looking for children. We set the 529 * DC_HAS_CHILDREN flag when a child devclass is created on 530 * the parent, so we only walk the list for those devclasses 531 * that have children. 532 */ 533 if (!((unsigned int)busclass->flags & DC_HAS_CHILDREN)) 534 return (0); 535 parent = busclass; 536 TAILQ_FOREACH(busclass, &devclasses, link) { 537 if (busclass->parent == parent) { 538 error = devclass_driver_deleted(busclass, dc, driver); 539 if (error) 540 return (error); 541 } 542 } 543 return (0); 544} 545 546/** 547 * @brief Delete a device driver from a device class 548 * 549 * Delete a device driver from a devclass. This is normally called 550 * automatically by DRIVER_MODULE(). 551 * 552 * If the driver is currently attached to any devices, 553 * devclass_delete_driver() will first attempt to detach from each 554 * device. If one of the detach calls fails, the driver will not be 555 * deleted. 556 * 557 * @param dc the devclass to edit 558 * @param driver the driver to unregister 559 */ 560int 561devclass_delete_driver(devclass_t busclass, driver_t *driver) 562{ 563 devclass_t dc = devclass_find(driver->name); 564 driverlink_t dl = NULL; 565 int error; 566 567 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 568 569 if (!dc) 570 return (0); 571 572 /* 573 * Find the link structure in the bus' list of drivers. 574 */ 575 TAILQ_FOREACH(dl, &busclass->drivers, link) { 576 if (dl->driver == driver) 577 break; 578 } 579 580 if (!dl) { 581 PDEBUG(("%s not found in %s list", driver->name, 582 busclass->name)); 583 return (ENOENT); 584 } 585 586 error = devclass_driver_deleted(busclass, dc, driver); 587 if (error != 0) 588 return (error); 589 590 TAILQ_REMOVE(&busclass->drivers, dl, link); 591 bsd_free(dl, M_BUS); 592 593 /* XXX: kobj_mtx */ 594 driver->refs--; 595 if (driver->refs == 0) 596 kobj_class_free((kobj_class_t) driver); 597 598 bus_data_generation_update(); 599 return (0); 600} 601 602/** 603 * @brief Quiesces a set of device drivers from a device class 604 * 605 * Quiesce a device driver from a devclass. This is normally called 606 * automatically by DRIVER_MODULE(). 607 * 608 * If the driver is currently attached to any devices, 609 * devclass_quiesece_driver() will first attempt to quiesce each 610 * device. 611 * 612 * @param dc the devclass to edit 613 * @param driver the driver to unregister 614 */ 615static int 616devclass_quiesce_driver(devclass_t busclass, driver_t *driver) 617{ 618 devclass_t dc = devclass_find(driver->name); 619 driverlink_t dl = NULL; 620 device_t dev; 621 int i; 622 int error; 623 624 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 625 626 if (!dc) 627 return (0); 628 629 /* 630 * Find the link structure in the bus' list of drivers. 631 */ 632 TAILQ_FOREACH(dl, &busclass->drivers, link) { 633 if (dl->driver == driver) 634 break; 635 } 636 637 if (!dl) { 638 PDEBUG(("%s not found in %s list", driver->name, 639 busclass->name)); 640 return (ENOENT); 641 } 642 643 /* 644 * Quiesce all devices. We iterate through all the devices in 645 * the devclass of the driver and quiesce any which are using 646 * the driver and which have a parent in the devclass which we 647 * are quiescing. 648 * 649 * Note that since a driver can be in multiple devclasses, we 650 * should not quiesce devices which are not children of 651 * devices in the affected devclass. 652 */ 653 for (i = 0; i < dc->maxunit; i++) { 654 if (dc->devices[i]) { 655 dev = dc->devices[i]; 656 if (dev->driver == driver && dev->parent && 657 dev->parent->devclass == busclass) { 658 if ((error = device_quiesce(dev)) != 0) 659 return (error); 660 } 661 } 662 } 663 664 return (0); 665} 666 667/** 668 * @internal 669 */ 670static driverlink_t 671devclass_find_driver_internal(devclass_t dc, const char *classname) 672{ 673 driverlink_t dl = NULL; 674 675 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 676 677 TAILQ_FOREACH(dl, &dc->drivers, link) { 678 if (!strcmp(dl->driver->name, classname)) 679 return (dl); 680 } 681 682 PDEBUG(("not found")); 683 return (NULL); 684} 685 686/** 687 * @brief Return the name of the devclass 688 */ 689const char * 690devclass_get_name(devclass_t dc) 691{ 692 return (dc->name); 693} 694 695/** 696 * @brief Find a device given a unit number 697 * 698 * @param dc the devclass to search 699 * @param unit the unit number to search for 700 * 701 * @returns the device with the given unit number or @c 702 * NULL if there is no such device 703 */ 704device_t 705devclass_get_device(devclass_t dc, int unit) 706{ 707 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 708 return (NULL); 709 return (dc->devices[unit]); 710} 711 712/** 713 * @brief Find the softc field of a device given a unit number 714 * 715 * @param dc the devclass to search 716 * @param unit the unit number to search for 717 * 718 * @returns the softc field of the device with the given 719 * unit number or @c NULL if there is no such 720 * device 721 */ 722void * 723devclass_get_softc(devclass_t dc, int unit) 724{ 725 device_t dev; 726 727 dev = devclass_get_device(dc, unit); 728 if (!dev) 729 return (NULL); 730 731 return (device_get_softc(dev)); 732} 733 734/** 735 * @brief Get a list of devices in the devclass 736 * 737 * An array containing a list of all the devices in the given devclass 738 * is allocated and returned in @p *devlistp. The number of devices 739 * in the array is returned in @p *devcountp. The caller should free 740 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0. 741 * 742 * @param dc the devclass to examine 743 * @param devlistp points at location for array pointer return 744 * value 745 * @param devcountp points at location for array size return value 746 * 747 * @retval 0 success 748 * @retval ENOMEM the array allocation failed 749 */ 750int 751devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 752{ 753 int count, i; 754 device_t *list; 755 756 count = devclass_get_count(dc); 757 list = bsd_malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 758 if (!list) 759 return (ENOMEM); 760 761 count = 0; 762 for (i = 0; i < dc->maxunit; i++) { 763 if (dc->devices[i]) { 764 list[count] = dc->devices[i]; 765 count++; 766 } 767 } 768 769 *devlistp = list; 770 *devcountp = count; 771 772 return (0); 773} 774 775/** 776 * @brief Get a list of drivers in the devclass 777 * 778 * An array containing a list of pointers to all the drivers in the 779 * given devclass is allocated and returned in @p *listp. The number 780 * of drivers in the array is returned in @p *countp. The caller should 781 * free the array using @c free(p, M_TEMP). 782 * 783 * @param dc the devclass to examine 784 * @param listp gives location for array pointer return value 785 * @param countp gives location for number of array elements 786 * return value 787 * 788 * @retval 0 success 789 * @retval ENOMEM the array allocation failed 790 */ 791int 792devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp) 793{ 794 driverlink_t dl = NULL; 795 driver_t **list = NULL; 796 int count; 797 798 count = 0; 799 TAILQ_FOREACH(dl, &dc->drivers, link) 800 count++; 801 list = bsd_malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT); 802 if (list == NULL) 803 return (ENOMEM); 804 805 count = 0; 806 TAILQ_FOREACH(dl, &dc->drivers, link) { 807 list[count] = dl->driver; 808 count++; 809 } 810 *listp = list; 811 *countp = count; 812 813 return (0); 814} 815 816/** 817 * @brief Get the number of devices in a devclass 818 * 819 * @param dc the devclass to examine 820 */ 821int 822devclass_get_count(devclass_t dc) 823{ 824 int count, i; 825 826 count = 0; 827 for (i = 0; i < dc->maxunit; i++) 828 if (dc->devices[i]) 829 count++; 830 return (count); 831} 832 833/** 834 * @brief Get the maximum unit number used in a devclass 835 * 836 * Note that this is one greater than the highest currently-allocated 837 * unit. If a null devclass_t is passed in, -1 is returned to indicate 838 * that not even the devclass has been allocated yet. 839 * 840 * @param dc the devclass to examine 841 */ 842int 843devclass_get_maxunit(devclass_t dc) 844{ 845 if (dc == NULL) 846 return (-1); 847 return (dc->maxunit); 848} 849 850/** 851 * @brief Find a free unit number in a devclass 852 * 853 * This function searches for the first unused unit number greater 854 * that or equal to @p unit. 855 * 856 * @param dc the devclass to examine 857 * @param unit the first unit number to check 858 */ 859int 860devclass_find_free_unit(devclass_t dc, int unit) 861{ 862 if (dc == NULL) 863 return (unit); 864 while (unit < dc->maxunit && dc->devices[unit] != NULL) 865 unit++; 866 return (unit); 867} 868 869/** 870 * @brief Set the parent of a devclass 871 * 872 * The parent class is normally initialised automatically by 873 * DRIVER_MODULE(). 874 * 875 * @param dc the devclass to edit 876 * @param pdc the new parent devclass 877 */ 878void 879devclass_set_parent(devclass_t dc, devclass_t pdc) 880{ 881 dc->parent = pdc; 882} 883 884/** 885 * @brief Get the parent of a devclass 886 * 887 * @param dc the devclass to examine 888 */ 889devclass_t 890devclass_get_parent(devclass_t dc) 891{ 892 return (dc->parent); 893} 894 895/** 896 * @internal 897 * @brief Allocate a unit number 898 * 899 * On entry, @p *unitp is the desired unit number (or @c -1 if any 900 * will do). The allocated unit number is returned in @p *unitp. 901 902 * @param dc the devclass to allocate from 903 * @param unitp points at the location for the allocated unit 904 * number 905 * 906 * @retval 0 success 907 * @retval EEXIST the requested unit number is already allocated 908 * @retval ENOMEM memory allocation failure 909 */ 910static int 911devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp) 912{ 913 int unit = *unitp; 914 915 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 916 917 /* Ask the parent bus if it wants to wire this device. */ 918 if (unit == -1) 919 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name, &unit); 920 921 /* If we were given a wired unit number, check for existing device */ 922 /* XXX imp XXX */ 923 if (unit != -1) { 924 if (unit >= 0 && unit < dc->maxunit && dc->devices[unit] != NULL) { 925 if (bootverbose) 926 printf("%s: %s%d already exists; skipping it\n", dc->name, dc->name, *unitp); 927 return (EEXIST); 928 } 929 } else { 930 /* Unwired device, find the next available slot for it */ 931 unit = 0; 932 for (unit = 0;; unit++) { 933 /* If this device slot is already in use, skip it. */ 934 if (unit < dc->maxunit && dc->devices[unit] != NULL) 935 continue; 936 937 break; 938 } 939 } 940 PDEBUG(("mid: unit %d in devclass %s", unit, DEVCLANAME(dc))); 941 /* 942 * We've selected a unit beyond the length of the table, so let's 943 * extend the table to make room for all units up to and including 944 * this one. 945 */ 946 if (unit >= dc->maxunit) { 947 device_t *newlist, *oldlist; 948 int newsize; 949 950 oldlist = dc->devices; 951 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 952 newlist = bsd_malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 953 PDEBUG(("oldlist %p, newlist %p, newsize %d", oldlist, newlist, newsize)); 954 if (!newlist) 955 return (ENOMEM); 956 PDEBUG(("start memcpy, size %d", sizeof(device_t) * dc->maxunit)); 957 if (oldlist != NULL) 958 (void)memcpy_s(newlist, sizeof(device_t) * dc->maxunit, oldlist, sizeof(device_t) * dc->maxunit); 959 PDEBUG(("start memset, maxunit %d, size %d", dc->maxunit, sizeof(device_t) * (newsize - dc->maxunit))); 960 (void)memset_s(newlist + dc->maxunit, sizeof(device_t) * (newsize - dc->maxunit), 0, 961 sizeof(device_t) * (newsize - dc->maxunit)); 962 dc->devices = newlist; 963 dc->maxunit = newsize; 964 if (oldlist != NULL) 965 bsd_free(oldlist, M_BUS); 966 } 967 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 968 969 *unitp = unit; 970 return (0); 971} 972 973/** 974 * @internal 975 */ 976static void 977device_set_desc_internal(device_t dev, const char* desc, int copy) 978{ 979 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 980 bsd_free(dev->desc, M_BUS); 981 dev->flags &= ~DF_DESCMALLOCED; 982 dev->desc = NULL; 983 } 984 985 if (copy && desc) { 986 dev->desc = bsd_malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 987 if (dev->desc) { 988 if (strcpy_s(dev->desc, strlen(desc) + 1, desc) != EOK) { 989 PDEBUG(("desc internal copy desc failed!\n")); 990 } 991 dev->flags |= DF_DESCMALLOCED; 992 } 993 } else { 994 /* Avoid a -Wcast-qual warning */ 995 dev->desc = (char *)(uintptr_t) desc; 996 } 997 998 bus_data_generation_update(); 999} 1000 1001/** 1002 * @brief Set the device's description 1003 * 1004 * The value of @c desc should be a string constant that will not 1005 * change (at least until the description is changed in a subsequent 1006 * call to device_set_desc() or device_set_desc_copy()). 1007 */ 1008void 1009device_set_desc(device_t dev, const char* desc) 1010{ 1011 device_set_desc_internal(dev, desc, FALSE); 1012} 1013 1014/** 1015 * @brief Set the device's description 1016 * 1017 * The string pointed to by @c desc is copied. Use this function if 1018 * the device description is generated, (e.g. with sprintf()). 1019 */ 1020void 1021device_set_desc_copy(device_t dev, const char* desc) 1022{ 1023 device_set_desc_internal(dev, desc, TRUE); 1024} 1025 1026/** 1027 * @brief Return the device's description string 1028 */ 1029const char * 1030device_get_desc(device_t dev) 1031{ 1032 return (dev->desc); 1033} 1034 1035/** 1036 * @brief Return the device's softc field 1037 * 1038 * The softc is allocated and zeroed when a driver is attached, based 1039 * on the size field of the driver. 1040 */ 1041void * 1042device_get_softc(device_t dev) 1043{ 1044 return (dev->softc); 1045} 1046 1047/** 1048 * @brief Set the device's softc field 1049 * 1050 * Most drivers do not need to use this since the softc is allocated 1051 * automatically when the driver is attached. 1052 */ 1053void 1054device_set_softc(device_t dev, void *softc) 1055{ 1056 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 1057 bsd_free(dev->softc, M_BUS_SC); 1058 dev->softc = softc; 1059 if (dev->softc) 1060 dev->flags |= DF_EXTERNALSOFTC; 1061 else 1062 dev->flags &= ~DF_EXTERNALSOFTC; 1063} 1064 1065/** 1066 * @brief Free claimed softc 1067 * 1068 * Most drivers do not need to use this since the softc is freed 1069 * automatically when the driver is detached. 1070 */ 1071void 1072device_free_softc(void *softc) 1073{ 1074 bsd_free(softc, M_BUS_SC); 1075} 1076 1077/** 1078 * @brief Claim softc 1079 * 1080 * This function can be used to let the driver free the automatically 1081 * allocated softc using "device_free_softc()". This function is 1082 * useful when the driver is refcounting the softc and the softc 1083 * cannot be freed when the "device_detach" method is called. 1084 */ 1085void 1086device_claim_softc(device_t dev) 1087{ 1088 if (dev->softc) 1089 dev->flags |= DF_EXTERNALSOFTC; 1090 else 1091 dev->flags &= ~DF_EXTERNALSOFTC; 1092} 1093 1094/** 1095 * @brief Get the device's ivars field 1096 * 1097 * The ivars field is used by the parent device to store per-device 1098 * state (e.g. the physical location of the device or a list of 1099 * resources). 1100 */ 1101void * 1102device_get_ivars(device_t dev) 1103{ 1104 1105 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 1106 return (dev->ivars); 1107} 1108 1109/** 1110 * @brief Set the device's ivars field 1111 */ 1112void 1113device_set_ivars(device_t dev, void * ivars) 1114{ 1115 1116 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 1117 dev->ivars = ivars; 1118} 1119 1120/** 1121 * @brief Return the device's state 1122 */ 1123device_state_t 1124device_get_state(device_t dev) 1125{ 1126 return (dev->state); 1127} 1128 1129/** 1130 * @brief Set the DF_ENABLED flag for the device 1131 */ 1132void 1133device_enable(device_t dev) 1134{ 1135 dev->flags |= DF_ENABLED; 1136} 1137 1138/** 1139 * @brief Clear the DF_ENABLED flag for the device 1140 */ 1141void 1142device_disable(device_t dev) 1143{ 1144 dev->flags &= ~DF_ENABLED; 1145} 1146 1147/** 1148 * @brief Increment the busy counter for the device 1149 */ 1150void 1151device_busy(device_t dev) 1152{ 1153 if (dev->state < DS_ATTACHING) 1154 panic("device_busy: called for unattached device"); 1155 if (dev->busy == 0 && dev->parent) 1156 device_busy(dev->parent); 1157 dev->busy++; 1158 if (dev->state == DS_ATTACHED) 1159 dev->state = DS_BUSY; 1160} 1161 1162/** 1163 * @brief Decrement the busy counter for the device 1164 */ 1165void 1166device_unbusy(device_t dev) 1167{ 1168 if (dev->busy != 0 && dev->state != DS_BUSY && 1169 dev->state != DS_ATTACHING) 1170 panic("device_unbusy: called for non-busy device %s", 1171 device_get_nameunit(dev)); 1172 dev->busy--; 1173 if (dev->busy == 0) { 1174 if (dev->parent) 1175 device_unbusy(dev->parent); 1176 if (dev->state == DS_BUSY) 1177 dev->state = DS_ATTACHED; 1178 } 1179} 1180 1181/** 1182 * @brief Set the DF_QUIET flag for the device 1183 */ 1184void 1185device_quiet(device_t dev) 1186{ 1187 dev->flags |= DF_QUIET; 1188} 1189 1190/** 1191 * @brief Clear the DF_QUIET flag for the device 1192 */ 1193void 1194device_verbose(device_t dev) 1195{ 1196 dev->flags &= ~DF_QUIET; 1197} 1198 1199/** 1200 * @brief Return non-zero if the DF_QUIET flag is set on the device 1201 */ 1202int 1203device_is_quiet(device_t dev) 1204{ 1205 return ((dev->flags & DF_QUIET) != 0); 1206} 1207 1208/** 1209 * @brief Return non-zero if the DF_ENABLED flag is set on the device 1210 */ 1211int 1212device_is_enabled(device_t dev) 1213{ 1214 return ((dev->flags & DF_ENABLED) != 0); 1215} 1216 1217/** 1218 * @brief Return non-zero if the device was successfully probed 1219 */ 1220int 1221device_is_alive(device_t dev) 1222{ 1223 return (dev->state >= DS_ALIVE); 1224} 1225 1226/** 1227 * @brief Return non-zero if the device currently has a driver 1228 * attached to it 1229 */ 1230int 1231device_is_attached(device_t dev) 1232{ 1233 return (dev->state >= DS_ATTACHED); 1234} 1235 1236/** 1237 * @brief Return non-zero if the device is currently suspended. 1238 */ 1239int 1240device_is_suspended(device_t dev) 1241{ 1242 return ((dev->flags & DF_SUSPENDED) != 0); 1243} 1244 1245 1246 1247/** 1248 * @brief Return a string containing the device's devclass name 1249 * followed by an ascii representation of the device's unit number 1250 * (e.g. @c "foo2"). 1251 */ 1252const char * 1253device_get_nameunit(device_t dev) 1254{ 1255 return (dev->nameunit); 1256} 1257 1258/** 1259 * @internal 1260 * @brief Add a device to a devclass 1261 * 1262 * A unit number is allocated for the device (using the device's 1263 * preferred unit number if any) and the device is registered in the 1264 * devclass. This allows the device to be looked up by its unit 1265 * number, e.g. by decoding a dev_t minor number. 1266 * 1267 * @param dc the devclass to add to 1268 * @param dev the device to add 1269 * 1270 * @retval 0 success 1271 * @retval EEXIST the requested unit number is already allocated 1272 * @retval ENOMEM memory allocation failure 1273 */ 1274static int 1275devclass_add_device(devclass_t dc, device_t dev) 1276{ 1277 int buflen, error; 1278 1279 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1280 1281 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX); 1282 if (buflen < 0) 1283 return (ENOMEM); 1284 dev->nameunit = bsd_malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 1285 if (!dev->nameunit) 1286 return (ENOMEM); 1287 1288 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) { 1289 bsd_free(dev->nameunit, M_BUS); 1290 dev->nameunit = NULL; 1291 return (error); 1292 } 1293 dc->devices[dev->unit] = dev; 1294 dev->devclass = dc; 1295 if (snprintf_s(dev->nameunit, buflen, buflen - 1, "%s%d", dc->name, dev->unit) < 0) { 1296 bsd_free(dev->nameunit, M_BUS); 1297 dev->nameunit = NULL; 1298 return (ENOMEM); 1299 } 1300 PDEBUG(("dev->nameunit : %s", dev->nameunit)); 1301 1302 return (0); 1303} 1304 1305/** 1306 * @internal 1307 * @brief Delete a device from a devclass 1308 * 1309 * The device is removed from the devclass's device list and its unit 1310 * number is freed. 1311 1312 * @param dc the devclass to delete from 1313 * @param dev the device to delete 1314 * 1315 * @retval 0 success 1316 */ 1317static int 1318devclass_delete_device(devclass_t dc, device_t dev) 1319{ 1320 if (!dc || !dev) 1321 return (0); 1322 1323 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1324 1325 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 1326 panic("devclass_delete_device: inconsistent device class"); 1327 dc->devices[dev->unit] = NULL; 1328 if (dev->flags & DF_WILDCARD) 1329 dev->unit = -1; 1330 dev->devclass = NULL; 1331 bsd_free(dev->nameunit, M_BUS); 1332 dev->nameunit = NULL; 1333 1334 return (0); 1335} 1336 1337/** 1338 * @internal 1339 * @brief Make a new device and add it as a child of @p parent 1340 * 1341 * @param parent the parent of the new device 1342 * @param name the devclass name of the new device or @c NULL 1343 * to leave the devclass unspecified 1344 * @parem unit the unit number of the new device of @c -1 to 1345 * leave the unit number unspecified 1346 * 1347 * @returns the new device 1348 */ 1349static device_t 1350make_device(device_t parent, const char *name, int unit) 1351{ 1352 device_t dev; 1353 devclass_t dc; 1354 1355 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1356 1357 if (name) { 1358 dc = devclass_find_internal(name, NULL, TRUE); 1359 if (!dc) { 1360 printf("make_device: can't find device class %s\n", 1361 name); 1362 return (NULL); 1363 } 1364 } else { 1365 dc = NULL; 1366 } 1367 1368 dev = bsd_malloc(sizeof(struct device), 0, M_NOWAIT|M_ZERO); 1369 if (!dev) 1370 return (NULL); 1371 1372 dev->parent = parent; 1373 TAILQ_INIT(&dev->children); 1374 kobj_init((kobj_t) dev, &null_class); 1375 dev->driver = NULL; 1376 dev->devclass = NULL; 1377 dev->unit = unit; 1378 dev->nameunit = NULL; 1379 dev->desc = NULL; 1380 dev->busy = 0; 1381 dev->devflags = 0; 1382 dev->flags = DF_ENABLED; 1383 dev->order = 0; 1384 if (unit == -1) 1385 dev->flags |= DF_WILDCARD; 1386 if (name) { 1387 dev->flags |= DF_FIXEDCLASS; 1388 if (devclass_add_device(dc, dev)) { 1389 kobj_delete((kobj_t) dev, NULL); 1390 return (NULL); 1391 } 1392 } 1393 dev->ivars = NULL; 1394 dev->softc = NULL; 1395 1396 dev->state = DS_NOTPRESENT; 1397 1398 PDEBUG(("bus_data_devices (%p) ", &bus_data_devices)); 1399 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1400 bus_data_generation_update(); 1401 1402 PDEBUG(("%s at %s as unit %d success", name, DEVICENAME(parent), unit)); 1403 return (dev); 1404} 1405 1406/** 1407 * @internal 1408 * @brief Print a description of a device. 1409 */ 1410static int 1411device_print_child(device_t dev, device_t child) 1412{ 1413 int retval = 0; 1414 1415 if (device_is_alive(child)) 1416 retval += BUS_PRINT_CHILD(dev, child); 1417 else 1418 retval += device_printf(child, " not found\n"); 1419 1420 return (retval); 1421} 1422 1423/** 1424 * @brief Create a new device 1425 * 1426 * This creates a new device and adds it as a child of an existing 1427 * parent device. The new device will be added after the last existing 1428 * child with order zero. 1429 * 1430 * @param dev the device which will be the parent of the 1431 * new child device 1432 * @param name devclass name for new device or @c NULL if not 1433 * specified 1434 * @param unit unit number for new device or @c -1 if not 1435 * specified 1436 * 1437 * @returns the new device 1438 */ 1439device_t 1440device_add_child(device_t dev, const char *name, int unit) 1441{ 1442 return (device_add_child_ordered(dev, 0, name, unit)); 1443} 1444 1445device_t 1446bus_get_device(device_t dev, const char *name) 1447{ 1448 device_t child; 1449 1450 PDEBUG(("+")); 1451 TAILQ_FOREACH(child, &dev->children, link) { 1452 if (!strcmp(device_get_name(child), name)) 1453 return (child); 1454 } 1455 PDEBUG(("-")); 1456 return (NULL); 1457} 1458 1459/** 1460 * @brief Create a new device 1461 * 1462 * This creates a new device and adds it as a child of an existing 1463 * parent device. The new device will be added after the last existing 1464 * child with the same order. 1465 * 1466 * @param dev the device which will be the parent of the 1467 * new child device 1468 * @param order a value which is used to partially sort the 1469 * children of @p dev - devices created using 1470 * lower values of @p order appear first in @p 1471 * dev's list of children 1472 * @param name devclass name for new device or @c NULL if not 1473 * specified 1474 * @param unit unit number for new device or @c -1 if not 1475 * specified 1476 * 1477 * @returns the new device 1478 */ 1479device_t 1480device_add_child_ordered(device_t dev, u_int order, const char *name, int unit) 1481{ 1482 device_t child; 1483 device_t place; 1484 1485 PDEBUG(("%s at %s with order %u as unit %d", 1486 name, DEVICENAME(dev), order, unit)); 1487 KASSERT(name != NULL || unit == -1, 1488 ("child device with wildcard name and specific unit number")); 1489 1490 child = make_device(dev, name, unit); 1491 if (child == NULL) 1492 return (child); 1493 child->order = order; 1494 1495 TAILQ_FOREACH(place, &dev->children, link) { 1496 if (place->order > order) 1497 break; 1498 } 1499 1500 if (place) { 1501 /* 1502 * The device 'place' is the first device whose order is 1503 * greater than the new child. 1504 */ 1505 TAILQ_INSERT_BEFORE(place, child, link); 1506 } else { 1507 /* 1508 * The new child's order is greater or equal to the order of 1509 * any existing device. Add the child to the tail of the list. 1510 */ 1511 TAILQ_INSERT_TAIL(&dev->children, child, link); 1512 } 1513 1514 bus_data_generation_update(); 1515 PDEBUG(("%s at %s with order %u as unit %d success", 1516 name, DEVICENAME(dev), order, unit)); 1517 return (child); 1518} 1519 1520/** 1521 * @brief Delete a device 1522 * 1523 * This function deletes a device along with all of its children. If 1524 * the device currently has a driver attached to it, the device is 1525 * detached first using device_detach(). 1526 * 1527 * @param dev the parent device 1528 * @param child the device to delete 1529 * 1530 * @retval 0 success 1531 * @retval non-zero a unit error code describing the error 1532 */ 1533int 1534device_delete_child(device_t dev, device_t child) 1535{ 1536 int error; 1537 device_t grandchild; 1538 1539 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1540 1541 /* detach parent before deleting children, if any */ 1542 if ((error = device_detach(child)) != 0) 1543 return (error); 1544 1545 /* remove children second */ 1546 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) { 1547 error = device_delete_child(child, grandchild); 1548 if (error) 1549 return (error); 1550 } 1551 1552 if (child->devclass) 1553 devclass_delete_device(child->devclass, child); 1554 if (child->parent) 1555 BUS_CHILD_DELETED(dev, child); 1556 TAILQ_REMOVE(&dev->children, child, link); 1557 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1558 kobj_delete((kobj_t) child, M_BUS); 1559 1560 bus_data_generation_update(); 1561 return (0); 1562} 1563 1564/** 1565 * @brief Delete all children devices of the given device, if any. 1566 * 1567 * This function deletes all children devices of the given device, if 1568 * any, using the device_delete_child() function for each device it 1569 * finds. If a child device cannot be deleted, this function will 1570 * return an error code. 1571 * 1572 * @param dev the parent device 1573 * 1574 * @retval 0 success 1575 * @retval non-zero a device would not detach 1576 */ 1577int 1578device_delete_children(device_t dev) 1579{ 1580 device_t child; 1581 int error; 1582 1583 PDEBUG(("Deleting all children of %s", DEVICENAME(dev))); 1584 1585 error = 0; 1586 1587 while ((child = TAILQ_FIRST(&dev->children)) != NULL) { 1588 error = device_delete_child(dev, child); 1589 if (error) { 1590 PDEBUG(("Failed deleting %s", DEVICENAME(child))); 1591 break; 1592 } 1593 } 1594 return (error); 1595} 1596 1597/** 1598 * @brief Probe a device and attach a driver if possible 1599 * 1600 * calls device_probe() and attaches if that was successful. 1601 */ 1602int 1603device_probe_and_attach(device_t dev) 1604{ 1605 int error; 1606 1607 PDEBUG(("+")); 1608 GIANT_REQUIRED; 1609 1610 error = device_probe(dev); 1611 if (error == -1) 1612 return (0); 1613 else if (error != 0) 1614 return (error); 1615 1616 //CURVNET_SET_QUIET(vnet0); 1617 error = device_attach(dev); 1618 //CURVNET_RESTORE(); 1619 PDEBUG(("-")); 1620 return error; 1621} 1622 1623/** 1624 * @brief Attach a device driver to a device 1625 * 1626 * This function is a wrapper around the DEVICE_ATTACH() driver 1627 * method. In addition to calling DEVICE_ATTACH(), it initialises the 1628 * device's sysctl tree, optionally prints a description of the device 1629 * and queues a notification event for user-based device management 1630 * services. 1631 * 1632 * Normally this function is only called internally from 1633 * device_probe_and_attach(). 1634 * 1635 * @param dev the device to initialise 1636 * 1637 * @retval 0 success 1638 * @retval ENXIO no driver was found 1639 * @retval ENOMEM memory allocation failure 1640 * @retval non-zero some other unix error code 1641 */ 1642int 1643device_attach(device_t dev) 1644{ 1645 uint64_t attachtime; 1646 int error; 1647 PDEBUG(("+")); 1648 1649 //if (resource_disabled(dev->driver->name, dev->unit)) { 1650 // device_disable(dev); 1651 // if (bootverbose) 1652 // device_printf(dev, "disabled via hints entry\n"); 1653 // return (ENXIO); 1654 //} 1655 1656 //device_sysctl_init(dev); 1657 if (!device_is_quiet(dev)) 1658 device_print_child(dev->parent, dev); 1659 // attachtime = get_cyclecount(); 1660 dev->state = DS_ATTACHING; 1661 if ((error = DEVICE_ATTACH(dev)) != 0) { 1662 printf("device_attach: %s%d attach returned %d\n", 1663 dev->driver->name, dev->unit, error); 1664 if (!(dev->flags & DF_FIXEDCLASS)) 1665 devclass_delete_device(dev->devclass, dev); 1666 (void)device_set_driver(dev, NULL); 1667 //device_sysctl_fini(dev); 1668 KASSERT(dev->busy == 0, ("attach failed but busy")); 1669 dev->state = DS_NOTPRESENT; 1670 return (error); 1671 } 1672 // attachtime = get_cyclecount() - attachtime; 1673 /* 1674 * 4 bits per device is a reasonable value for desktop and server 1675 * hardware with good get_cyclecount() implementations, but WILL 1676 * need to be adjusted on other platforms. 1677 */ 1678#define RANDOM_PROBE_BIT_GUESS 4 1679 if (bootverbose) 1680 printf("random: harvesting attach, %zu bytes (%d bits) from %s%d\n", 1681 sizeof(attachtime), RANDOM_PROBE_BIT_GUESS, 1682 dev->driver->name, dev->unit); 1683 //random_harvest_direct(&attachtime, sizeof(attachtime), 1684 // RANDOM_PROBE_BIT_GUESS, RANDOM_ATTACH); 1685 //device_sysctl_update(dev); 1686 if (dev->busy) 1687 dev->state = DS_BUSY; 1688 else 1689 dev->state = DS_ATTACHED; 1690 dev->flags &= ~DF_DONENOMATCH; 1691 //EVENTHANDLER_INVOKE(device_attach, dev); 1692 //devadded(dev); 1693 PDEBUG(("-")); 1694 return (0); 1695} 1696 1697/** 1698 * @brief Probe a device, and return this status. 1699 * 1700 * This function is the core of the device autoconfiguration 1701 * system. Its purpose is to select a suitable driver for a device and 1702 * then call that driver to initialise the hardware appropriately. The 1703 * driver is selected by calling the DEVICE_PROBE() method of a set of 1704 * candidate drivers and then choosing the driver which returned the 1705 * best value. This driver is then attached to the device using 1706 * device_attach(). 1707 * 1708 * The set of suitable drivers is taken from the list of drivers in 1709 * the parent device's devclass. If the device was originally created 1710 * with a specific class name (see device_add_child()), only drivers 1711 * with that name are probed, otherwise all drivers in the devclass 1712 * are probed. If no drivers return successful probe values in the 1713 * parent devclass, the search continues in the parent of that 1714 * devclass (see devclass_get_parent()) if any. 1715 * 1716 * @param dev the device to initialise 1717 * 1718 * @retval 0 success 1719 * @retval ENXIO no driver was found 1720 * @retval ENOMEM memory allocation failure 1721 * @retval non-zero some other unix error code 1722 * @retval -1 Device already attached 1723 */ 1724int 1725device_probe(device_t dev) 1726{ 1727 int error; 1728 1729 GIANT_REQUIRED; 1730 PDEBUG(("+")); 1731 1732 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0) 1733 return (-1); 1734 1735 if (!(dev->flags & DF_ENABLED)) { 1736 if (bootverbose && device_get_name(dev) != NULL) { 1737 device_print_prettyname(dev); 1738 printf("not probed (disabled)\n"); 1739 } 1740 return (-1); 1741 } 1742 if ((error = device_probe_child(dev->parent, dev)) != 0) { 1743 if (bus_current_pass == BUS_PASS_DEFAULT && 1744 !(dev->flags & DF_DONENOMATCH)) { 1745 BUS_PROBE_NOMATCH(dev->parent, dev); 1746 //devnomatch(dev); 1747 dev->flags |= DF_DONENOMATCH; 1748 } 1749 return (error); 1750 } 1751 PDEBUG(("-")); 1752 return (0); 1753} 1754 1755/** 1756 * @brief Tells a driver to quiesce itself. 1757 * 1758 * This function is a wrapper around the DEVICE_QUIESCE() driver 1759 * method. If the call to DEVICE_QUIESCE() succeeds. 1760 * 1761 * @param dev the device to quiesce 1762 * 1763 * @retval 0 success 1764 * @retval ENXIO no driver was found 1765 * @retval ENOMEM memory allocation failure 1766 * @retval non-zero some other unix error code 1767 */ 1768int 1769device_quiesce(device_t dev) 1770{ 1771 1772 PDEBUG(("%s", DEVICENAME(dev))); 1773 if (dev->state == DS_BUSY) 1774 return (EBUSY); 1775 if (dev->state != DS_ATTACHED) 1776 return (0); 1777 1778 return (DEVICE_QUIESCE(dev)); 1779} 1780 1781/** 1782 * @brief Notify a device of system shutdown 1783 * 1784 * This function calls the DEVICE_SHUTDOWN() driver method if the 1785 * device currently has an attached driver. 1786 * 1787 * @returns the value returned by DEVICE_SHUTDOWN() 1788 */ 1789int 1790device_shutdown(device_t dev) 1791{ 1792 if (dev->state < DS_ATTACHED) 1793 return (0); 1794 return (DEVICE_SHUTDOWN(dev)); 1795} 1796 1797// bus 1798// 1799// 1800device_t 1801bus_generic_add_child(device_t dev, u_int order, const char *name, int unit) 1802{ 1803 1804 return (device_add_child_ordered(dev, order, name, unit)); 1805} 1806 1807/** 1808 * @brief Helper function for implementing DEVICE_PROBE() 1809 * 1810 * This function can be used to help implement the DEVICE_PROBE() for 1811 * a bus (i.e. a device which has other devices attached to it). It 1812 * calls the DEVICE_IDENTIFY() method of each driver in the device's 1813 * devclass. 1814 */ 1815int 1816bus_generic_probe(device_t dev) 1817{ 1818 devclass_t dc = dev->devclass; 1819 driverlink_t dl = NULL; 1820 PDEBUG(("+")); 1821 1822 TAILQ_FOREACH(dl, &dc->drivers, link) { 1823 /* 1824 * If this driver's pass is too high, then ignore it. 1825 * For most drivers in the default pass, this will 1826 * never be true. For early-pass drivers they will 1827 * only call the identify routines of eligible drivers 1828 * when this routine is called. Drivers for later 1829 * passes should have their identify routines called 1830 * on early-pass busses during BUS_NEW_PASS(). 1831 */ 1832 if (dl->pass > bus_current_pass) 1833 continue; 1834 DEVICE_IDENTIFY(dl->driver, dev); 1835 } 1836 1837 PDEBUG(("-")); 1838 return (0); 1839} 1840 1841/** 1842 * @brief Helper function for implementing DEVICE_ATTACH() 1843 * 1844 * This function can be used to help implement the DEVICE_ATTACH() for 1845 * a bus. It calls device_probe_and_attach() for each of the device's 1846 * children. 1847 */ 1848int 1849bus_generic_attach(device_t dev) 1850{ 1851 device_t child; 1852 1853 PDEBUG(("+")); 1854 TAILQ_FOREACH(child, &dev->children, link) { 1855 device_probe_and_attach(child); 1856 } 1857 PDEBUG(("-")); 1858 return (0); 1859} 1860 1861/** 1862 * @brief Helper function for implementing DEVICE_DETACH() 1863 * 1864 * This function can be used to help implement the DEVICE_DETACH() for 1865 * a bus. It calls device_detach() for each of the device's 1866 * children. 1867 */ 1868int 1869bus_generic_detach(device_t dev) 1870{ 1871 device_t child; 1872 int error; 1873 1874 if (dev->state != DS_ATTACHED) 1875 return (EBUSY); 1876 1877 /* 1878 * Detach children in the reverse order. 1879 * See bus_generic_suspend for details. 1880 */ 1881 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 1882 if ((error = device_detach(child)) != 0) 1883 return (error); 1884 } 1885 1886 return (0); 1887} 1888 1889/** 1890 * @brief Helper function for implementing DEVICE_SHUTDOWN() 1891 * 1892 * This function can be used to help implement the DEVICE_SHUTDOWN() 1893 * for a bus. It calls device_shutdown() for each of the device's 1894 * children. 1895 */ 1896int 1897bus_generic_shutdown(device_t dev) 1898{ 1899 device_t child; 1900 1901 /* 1902 * Shut down children in the reverse order. 1903 * See bus_generic_suspend for details. 1904 */ 1905 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 1906 device_shutdown(child); 1907 } 1908 1909 return (0); 1910} 1911 1912/** 1913 * @brief Default function for suspending a child device. 1914 * 1915 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD(). 1916 */ 1917int 1918bus_generic_suspend_child(device_t dev, device_t child) 1919{ 1920 int error; 1921 1922 error = DEVICE_SUSPEND(child); 1923 1924 if (error == 0) 1925 child->flags |= DF_SUSPENDED; 1926 1927 return (error); 1928} 1929 1930/** 1931 * @brief Default function for resuming a child device. 1932 * 1933 * This function is to be used by a bus's DEVICE_RESUME_CHILD(). 1934 */ 1935int 1936bus_generic_resume_child(device_t dev, device_t child) 1937{ 1938 1939 DEVICE_RESUME(child); 1940 child->flags &= ~DF_SUSPENDED; 1941 1942 return (0); 1943} 1944 1945/** 1946 * @brief Helper function for implementing DEVICE_SUSPEND() 1947 * 1948 * This function can be used to help implement the DEVICE_SUSPEND() 1949 * for a bus. It calls DEVICE_SUSPEND() for each of the device's 1950 * children. If any call to DEVICE_SUSPEND() fails, the suspend 1951 * operation is aborted and any devices which were suspended are 1952 * resumed immediately by calling their DEVICE_RESUME() methods. 1953 */ 1954int 1955bus_generic_suspend(device_t dev) 1956{ 1957 int error; 1958 device_t child; 1959 1960 /* 1961 * Suspend children in the reverse order. 1962 * For most buses all children are equal, so the order does not matter. 1963 * Other buses, such as acpi, carefully order their child devices to 1964 * express implicit dependencies between them. For such buses it is 1965 * safer to bring down devices in the reverse order. 1966 */ 1967 TAILQ_FOREACH_REVERSE(child, &dev->children, device_list, link) { 1968 error = BUS_SUSPEND_CHILD(dev, child); 1969 if (error != 0) { 1970 child = TAILQ_NEXT(child, link); 1971 if (child != NULL) { 1972 TAILQ_FOREACH_FROM(child, &dev->children, link) 1973 BUS_RESUME_CHILD(dev, child); 1974 } 1975 return (error); 1976 } 1977 } 1978 return (0); 1979} 1980 1981/** 1982 * @brief Helper function for implementing DEVICE_RESUME() 1983 * 1984 * This function can be used to help implement the DEVICE_RESUME() for 1985 * a bus. It calls DEVICE_RESUME() on each of the device's children. 1986 */ 1987int 1988bus_generic_resume(device_t dev) 1989{ 1990 device_t child; 1991 1992 TAILQ_FOREACH(child, &dev->children, link) { 1993 BUS_RESUME_CHILD(dev, child); 1994 /* if resume fails, there's nothing we can usefully do... */ 1995 } 1996 return (0); 1997} 1998 1999 2000 2001/** 2002 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2003 * 2004 * This function prints the first part of the ascii representation of 2005 * @p child, including its name, unit and description (if any - see 2006 * device_set_desc()). 2007 * 2008 * @returns the number of characters printed 2009 */ 2010int 2011bus_print_child_header(device_t dev, device_t child) 2012{ 2013 int retval = 0; 2014 2015 if (device_get_desc(child)) { 2016 retval += device_printf(child, "<%s>", device_get_desc(child)); 2017 } else { 2018 retval += printf("%s", device_get_nameunit(child)); 2019 } 2020 2021 return (retval); 2022} 2023 2024/** 2025 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2026 * 2027 * This function prints the last part of the ascii representation of 2028 * @p child, which consists of the string @c " on " followed by the 2029 * name and unit of the @p dev. 2030 * 2031 * @returns the number of characters printed 2032 */ 2033int 2034bus_print_child_footer(device_t dev, device_t child) 2035{ 2036 return (printf(" on %s\n", device_get_nameunit(dev))); 2037} 2038 2039/** 2040 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2041 * 2042 * This function prints out the VM domain for the given device. 2043 * 2044 * @returns the number of characters printed 2045 */ 2046int 2047bus_print_child_domain(device_t dev, device_t child) 2048{ 2049 int domain; 2050 2051 /* No domain? Don't print anything */ 2052 if (BUS_GET_DOMAIN(dev, child, &domain) != 0) 2053 return (0); 2054 2055 return (printf(" numa-domain %d", domain)); 2056} 2057 2058/** 2059 * @brief Helper function for implementing BUS_PRINT_CHILD(). 2060 * 2061 * This function simply calls bus_print_child_header() followed by 2062 * bus_print_child_footer(). 2063 * 2064 * @returns the number of characters printed 2065 */ 2066int 2067bus_generic_print_child(device_t dev, device_t child) 2068{ 2069 int retval = 0; 2070 2071 retval += bus_print_child_header(dev, child); 2072 retval += bus_print_child_domain(dev, child); 2073 retval += bus_print_child_footer(dev, child); 2074 2075 return (retval); 2076} 2077 2078/** 2079 * @brief Helper function for implementing BUS_DRIVER_ADDED(). 2080 * 2081 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's 2082 * DEVICE_IDENTIFY() method to allow it to add new children to the bus 2083 * and then calls device_probe_and_attach() for each unattached child. 2084 */ 2085void 2086bus_generic_driver_added(device_t dev, driver_t *driver) 2087{ 2088 device_t child; 2089 PDEBUG(("+")); 2090 2091 DEVICE_IDENTIFY(driver, dev); 2092 PDEBUG(("dev->children %p", dev->children)); 2093 TAILQ_FOREACH(child, &dev->children, link) { 2094 if (child->state == DS_NOTPRESENT || 2095 (child->flags & DF_REBID)) 2096 device_probe_and_attach(child); 2097 } 2098 PDEBUG(("-")); 2099} 2100 2101/** 2102 * @brief Helper function for implementing BUS_NEW_PASS(). 2103 * 2104 * This implementing of BUS_NEW_PASS() first calls the identify 2105 * routines for any drivers that probe at the current pass. Then it 2106 * walks the list of devices for this bus. If a device is already 2107 * attached, then it calls BUS_NEW_PASS() on that device. If the 2108 * device is not already attached, it attempts to attach a driver to 2109 * it. 2110 */ 2111void 2112bus_generic_new_pass(device_t dev) 2113{ 2114 driverlink_t dl = NULL; 2115 devclass_t dc; 2116 device_t child; 2117 2118 dc = dev->devclass; 2119 TAILQ_FOREACH(dl, &dc->drivers, link) { 2120 if (dl->pass == bus_current_pass) 2121 DEVICE_IDENTIFY(dl->driver, dev); 2122 } 2123 TAILQ_FOREACH(child, &dev->children, link) { 2124 if (child->state >= DS_ATTACHED) 2125 BUS_NEW_PASS(child); 2126 else if (child->state == DS_NOTPRESENT) 2127 device_probe_and_attach(child); 2128 } 2129} 2130 2131// delete resouce and intr 2132 2133/** 2134 * @brief Helper function for implementing BUS_CHILD_PRESENT(). 2135 * 2136 * This simple implementation of BUS_CHILD_PRESENT() simply calls the 2137 * BUS_CHILD_PRESENT() method of the parent of @p dev. 2138 */ 2139int 2140bus_generic_child_present(device_t dev, device_t child) 2141{ 2142 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev)); 2143} 2144 2145int 2146bus_generic_get_domain(device_t dev, device_t child, int *domain) 2147{ 2148 2149 if (dev->parent) 2150 return (BUS_GET_DOMAIN(dev->parent, dev, domain)); 2151 2152 return (ENOENT); 2153} 2154 2155/** 2156 * @brief Helper function for implementing BUS_RESCAN(). 2157 * 2158 * This null implementation of BUS_RESCAN() always fails to indicate 2159 * the bus does not support rescanning. 2160 */ 2161int 2162bus_null_rescan(device_t dev) 2163{ 2164 2165 return (ENXIO); 2166} 2167 2168/** 2169 * @brief Wrapper function for BUS_CHILD_PRESENT(). 2170 * 2171 * This function simply calls the BUS_CHILD_PRESENT() method of the 2172 * parent of @p dev. 2173 */ 2174int 2175bus_child_present(device_t child) 2176{ 2177 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 2178} 2179 2180/** 2181 * @brief Wrapper function for BUS_GET_DOMAIN(). 2182 * 2183 * This function simply calls the BUS_GET_DOMAIN() method of the 2184 * parent of @p dev. 2185 */ 2186int 2187bus_get_domain(device_t dev, int *domain) 2188{ 2189 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain)); 2190} 2191 2192int 2193bus_data_generation_check(int generation) 2194{ 2195 if (generation != bus_data_generation) 2196 return (1); 2197 2198 /* XXX generate optimised lists here? */ 2199 return (0); 2200} 2201 2202void 2203bus_data_generation_update(void) 2204{ 2205 bus_data_generation++; 2206} 2207 2208/** 2209 * @brief Print the name of the device followed by a colon and a space 2210 * 2211 * @returns the number of characters printed 2212 */ 2213int 2214device_print_prettyname(device_t dev) 2215{ 2216 const char *name = device_get_name(dev); 2217 2218 if (name == NULL) 2219 return (printf("unknown: ")); 2220 return (printf("%s%d: ", name, device_get_unit(dev))); 2221} 2222 2223/** 2224 * @brief Return the device's unit number. 2225 */ 2226int 2227device_get_unit(device_t dev) 2228{ 2229 return (dev->unit); 2230} 2231 2232/** 2233 * @internal 2234 */ 2235static driverlink_t 2236first_matching_driver(devclass_t dc, device_t dev) 2237{ 2238 if (dev->devclass) 2239 return (devclass_find_driver_internal(dc, dev->devclass->name)); 2240 return (TAILQ_FIRST(&dc->drivers)); 2241} 2242 2243/** 2244 * @internal 2245 */ 2246static driverlink_t 2247next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 2248{ 2249 if (dev->devclass) { 2250 driverlink_t dl; 2251 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 2252 if (!strcmp(dev->devclass->name, dl->driver->name)) 2253 return (dl); 2254 return (NULL); 2255 } 2256 return (TAILQ_NEXT(last, link)); 2257} 2258 2259 2260/** 2261 * @brief Set the devclass of a device 2262 * @see devclass_add_device(). 2263 */ 2264int 2265device_set_devclass(device_t dev, const char *classname) 2266{ 2267 devclass_t dc; 2268 int error; 2269 2270 if (!classname) { 2271 if (dev->devclass) 2272 devclass_delete_device(dev->devclass, dev); 2273 return (0); 2274 } 2275 2276 if (dev->devclass) { 2277 printf("device_set_devclass: device class already set\n"); 2278 return (EINVAL); 2279 } 2280 2281 dc = devclass_find_internal(classname, NULL, TRUE); 2282 if (!dc) 2283 return (ENOMEM); 2284 2285 error = devclass_add_device(dc, dev); 2286 2287 bus_data_generation_update(); 2288 return (error); 2289} 2290 2291/** 2292 * @brief Detach a driver from a device 2293 * 2294 * This function is a wrapper around the DEVICE_DETACH() driver 2295 * method. If the call to DEVICE_DETACH() succeeds, it calls 2296 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a 2297 * notification event for user-based device management services and 2298 * cleans up the device's sysctl tree. 2299 * 2300 * @param dev the device to un-initialise 2301 * 2302 * @retval 0 success 2303 * @retval ENXIO no driver was found 2304 * @retval ENOMEM memory allocation failure 2305 * @retval non-zero some other unix error code 2306 */ 2307int 2308device_detach(device_t dev) 2309{ 2310 int error; 2311 2312 GIANT_REQUIRED; 2313 2314 PDEBUG(("%s", DEVICENAME(dev))); 2315 if (dev->state == DS_BUSY) 2316 return (EBUSY); 2317 if (dev->state == DS_ATTACHING) { 2318 device_printf(dev, "device in attaching state! Deferring detach.\n"); 2319 return (EBUSY); 2320 } 2321 if (dev->state != DS_ATTACHED) 2322 return (0); 2323 2324 if ((error = DEVICE_DETACH(dev)) != 0) { 2325 return (error); 2326 } 2327 2328 if (!device_is_quiet(dev)) 2329 device_printf(dev, "detached\n"); 2330 if (dev->parent) { 2331 PDEBUG(("BUS_CHILD_DETACHED %s", DEVICENAME(dev->parent))); 2332 BUS_CHILD_DETACHED(dev->parent, dev); 2333 } 2334 2335 if (!(dev->flags & DF_FIXEDCLASS)) { 2336 PDEBUG(("devclass_delete_device")); 2337 devclass_delete_device(dev->devclass, dev); 2338 } 2339 2340 device_verbose(dev); 2341 dev->state = DS_NOTPRESENT; 2342 (void)device_set_driver(dev, NULL); 2343 2344 return (0); 2345} 2346 2347/** 2348 * @brief Return the parent of a device 2349 */ 2350device_t 2351device_get_parent(device_t dev) 2352{ 2353 return (dev->parent); 2354} 2355 2356/** 2357 * @brief Print the name of the device followed by a colon, a space 2358 * and the result of calling vprintf() with the value of @p fmt and 2359 * the following arguments. 2360 * 2361 * @returns the number of characters printed 2362 */ 2363int 2364device_printf(device_t dev, const char * fmt, ...) 2365{ 2366 va_list ap; 2367 int retval; 2368 2369 retval = device_print_prettyname(dev); 2370 va_start(ap, fmt); 2371 retval += vprintf(fmt, ap); 2372 va_end(ap); 2373 return (retval); 2374} 2375 2376/** 2377 * @brief Set the driver of a device 2378 * 2379 * @retval 0 success 2380 * @retval EBUSY the device already has a driver attached 2381 * @retval ENOMEM a memory allocation failure occurred 2382 */ 2383int 2384device_set_driver(device_t dev, driver_t *driver) 2385{ 2386 if (dev->state >= DS_ATTACHED) 2387 return (EBUSY); 2388 2389 if (dev->driver == driver) 2390 return (0); 2391 2392 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 2393 bsd_free(dev->softc, M_BUS_SC); 2394 dev->softc = NULL; 2395 } 2396 device_set_desc(dev, NULL); 2397 kobj_delete((kobj_t) dev, NULL); 2398 dev->driver = driver; 2399 if (driver) { 2400 kobj_init((kobj_t) dev, (kobj_class_t) driver); 2401 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 2402 dev->softc = bsd_malloc(driver->size, M_BUS_SC, 2403 M_NOWAIT | M_ZERO); 2404 if (!dev->softc) { 2405 kobj_delete((kobj_t) dev, NULL); 2406 kobj_init((kobj_t) dev, &null_class); 2407 dev->driver = NULL; 2408 return (ENOMEM); 2409 } 2410 } 2411 } else { 2412 kobj_init((kobj_t) dev, &null_class); 2413 } 2414 2415 bus_data_generation_update(); 2416 return (0); 2417} 2418 2419/** 2420 * @internal 2421 */ 2422int 2423device_probe_child(device_t dev, device_t child) 2424{ 2425 devclass_t dc; 2426 driverlink_t best = NULL; 2427 driverlink_t dl = NULL; 2428 int result, pri = 0; 2429 int hasclass = (child->devclass != NULL); 2430 2431 GIANT_REQUIRED; 2432 2433 dc = dev->devclass; 2434 if (!dc) 2435 panic("device_probe_child: parent device has no devclass"); 2436 2437 /* 2438 * If the state is already probed, then return. However, don't 2439 * return if we can rebid this object. 2440 */ 2441 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0) 2442 return (0); 2443 2444 for (; dc; dc = dc->parent) { 2445 for (dl = first_matching_driver(dc, child); 2446 dl; 2447 dl = next_matching_driver(dc, child, dl)) { 2448 /* If this driver's pass is too high, then ignore it. */ 2449 if (dl->pass > bus_current_pass) 2450 continue; 2451 2452 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 2453 result = device_set_driver(child, dl->driver); 2454 if (result == ENOMEM) 2455 return (result); 2456 else if (result != 0) 2457 continue; 2458 if (!hasclass) { 2459 if (device_set_devclass(child, 2460 dl->driver->name) != 0) { 2461 char const * devname = 2462 device_get_name(child); 2463 if (devname == NULL) 2464 devname = "(unknown)"; 2465 printf("driver bug: Unable to set " 2466 "devclass (class: %s " 2467 "devname: %s)\n", 2468 dl->driver->name, 2469 devname); 2470 (void)device_set_driver(child, NULL); 2471 continue; 2472 } 2473 } 2474 2475 /* Fetch any flags for the device before probing. */ 2476 // resource_int_value(dl->driver->name, child->unit, 2477 // "flags", &child->devflags); 2478 PDEBUG(("start DEVICE_PROBE")); 2479 result = DEVICE_PROBE(child); 2480 2481 /* Reset flags and devclass before the next probe. */ 2482 child->devflags = 0; 2483 if (!hasclass) 2484 (void)device_set_devclass(child, NULL); 2485 2486 /* 2487 * If the driver returns SUCCESS, there can be 2488 * no higher match for this device. 2489 */ 2490 if (result == 0) { 2491 best = dl; 2492 pri = 0; 2493 break; 2494 } 2495 2496 /* 2497 * Reset DF_QUIET in case this driver doesn't 2498 * end up as the best driver. 2499 */ 2500 device_verbose(child); 2501 2502 PDEBUG(("probe result: %d", result)); 2503 /* 2504 * Probes that return BUS_PROBE_NOWILDCARD or lower 2505 * only match on devices whose driver was explicitly 2506 * specified. 2507 */ 2508 if (result <= BUS_PROBE_NOWILDCARD && 2509 !(child->flags & DF_FIXEDCLASS)) { 2510 result = ENXIO; 2511 } 2512 2513 /* 2514 * The driver returned an error so it 2515 * certainly doesn't match. 2516 */ 2517 if (result > 0) { 2518 (void)device_set_driver(child, NULL); 2519 continue; 2520 } 2521 2522 /* 2523 * A priority lower than SUCCESS, remember the 2524 * best matching driver. Initialise the value 2525 * of pri for the first match. 2526 */ 2527 if (best == NULL || result > pri) { 2528 best = dl; 2529 pri = result; 2530 continue; 2531 } 2532 } 2533 /* 2534 * If we have an unambiguous match in this devclass, 2535 * don't look in the parent. 2536 */ 2537 if (best && pri == 0) 2538 break; 2539 } 2540 2541 /* 2542 * If we found a driver, change state and initialise the devclass. 2543 */ 2544 /* XXX What happens if we rebid and got no best? */ 2545 if (best) { 2546 PDEBUG(("in best")); 2547 /* 2548 * If this device was attached, and we were asked to 2549 * rescan, and it is a different driver, then we have 2550 * to detach the old driver and reattach this new one. 2551 * Note, we don't have to check for DF_REBID here 2552 * because if the state is > DS_ALIVE, we know it must 2553 * be. 2554 * 2555 * This assumes that all DF_REBID drivers can have 2556 * their probe routine called at any time and that 2557 * they are idempotent as well as completely benign in 2558 * normal operations. 2559 * 2560 * We also have to make sure that the detach 2561 * succeeded, otherwise we fail the operation (or 2562 * maybe it should just fail silently? I'm torn). 2563 */ 2564 if (child->state > DS_ALIVE && best->driver != child->driver) 2565 if ((result = device_detach(dev)) != 0) 2566 return (result); 2567 2568 /* Set the winning driver, devclass, and flags. */ 2569 if (!child->devclass) { 2570 result = device_set_devclass(child, best->driver->name); 2571 if (result != 0) 2572 return (result); 2573 } 2574 result = device_set_driver(child, best->driver); 2575 if (result != 0) 2576 return (result); 2577 // resource_int_value(best->driver->name, child->unit, 2578 // "flags", &child->devflags); 2579 2580 if (pri < 0) { 2581 /* 2582 * A bit bogus. Call the probe method again to make 2583 * sure that we have the right description. 2584 */ 2585 DEVICE_PROBE(child); 2586#if 0 2587 child->flags |= DF_REBID; 2588#endif 2589 } else 2590 child->flags &= ~DF_REBID; 2591 child->state = DS_ALIVE; 2592 2593 bus_data_generation_update(); 2594 PDEBUG(("-")); 2595 return (0); 2596 } 2597 2598 PDEBUG(("ENXIO -")); 2599 return (ENXIO); 2600} 2601 2602 2603/** 2604 * @brief Return the current devclass for the device or @c NULL if 2605 * there is none. 2606 */ 2607devclass_t 2608device_get_devclass(device_t dev) 2609{ 2610 return (dev->devclass); 2611} 2612 2613/** 2614 * @brief Initialise a resource list. 2615 * 2616 * @param rl the resource list to initialise 2617 */ 2618void 2619resource_list_init(struct resource_list *rl) 2620{ 2621 STAILQ_INIT(rl); 2622} 2623 2624/** 2625 * @brief Reclaim memory used by a resource list. 2626 * 2627 * This function frees the memory for all resource entries on the list 2628 * (if any). 2629 * 2630 * @param rl the resource list to free 2631 */ 2632void 2633resource_list_free(struct resource_list *rl) 2634{ 2635 struct resource_list_entry *rle; 2636 2637 while ((rle = STAILQ_FIRST(rl)) != NULL) { 2638 if (rle->res) 2639 bsd_free(rle->res, M_BUS); 2640 STAILQ_REMOVE_HEAD(rl, link); 2641 bsd_free(rle, M_BUS); 2642 } 2643} 2644 2645/** 2646 * @brief Add or modify a resource entry. 2647 * 2648 * If an existing entry exists with the same type and rid, it will be 2649 * modified using the given values of @p start, @p end and @p 2650 * count. If no entry exists, a new one will be created using the 2651 * given values. The resource list entry that matches is then returned. 2652 * 2653 * @param rl the resource list to edit 2654 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2655 * @param rid the resource identifier 2656 * @param start the start address of the resource 2657 * @param end the end address of the resource 2658 * @param count XXX end-start+1 2659 */ 2660struct resource_list_entry * 2661resource_list_add(struct resource_list *rl, int type, int rid, 2662 rman_res_t start, rman_res_t end, rman_res_t count) 2663{ 2664 struct resource_list_entry *rle; 2665 2666 rle = resource_list_find(rl, type, rid); 2667 if (!rle) { 2668 rle = bsd_malloc(sizeof(struct resource_list_entry), M_BUS, 2669 M_NOWAIT); 2670 if (!rle) 2671 panic("resource_list_add: can't record entry"); 2672 STAILQ_INSERT_TAIL(rl, rle, link); 2673 rle->type = type; 2674 rle->rid = rid; 2675 rle->res = NULL; 2676 rle->flags = 0; 2677 } 2678 2679 if (rle->res) 2680 panic("resource_list_add: resource entry is busy"); 2681 2682 rle->res = bsd_malloc(sizeof(struct resource), M_BUS, M_NOWAIT); 2683 if (rle->res == NULL) { 2684 panic("resource_list_add: resource is busy"); 2685 } 2686 rle->res->start = start; 2687 rle->res->end = end; 2688 rle->res->count = count; 2689 return (rle); 2690} 2691 2692/** 2693 * @brief Find a resource entry by type and rid. 2694 * 2695 * @param rl the resource list to search 2696 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2697 * @param rid the resource identifier 2698 * 2699 * @returns the resource entry pointer or NULL if there is no such 2700 * entry. 2701 */ 2702struct resource_list_entry * 2703resource_list_find(struct resource_list *rl, int type, int rid) 2704{ 2705 struct resource_list_entry *rle = NULL; 2706 2707 STAILQ_FOREACH(rle, rl, link) { 2708 if (rle->type == type && rle->rid == rid) 2709 return (rle); 2710 } 2711 return (NULL); 2712} 2713 2714/** 2715 * @brief Wrapper function for BUS_ALLOC_RESOURCE(). 2716 * 2717 * This function simply calls the BUS_ALLOC_RESOURCE() method of the 2718 * parent of @p dev. 2719 */ 2720struct resource * 2721bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start, 2722 rman_res_t end, rman_res_t count, u_int flags) 2723{ 2724 struct resource *res = NULL; 2725 2726 if (dev->parent == NULL) 2727 return (NULL); 2728 res = BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 2729 count, flags); 2730 return (res); 2731} 2732 2733// root driver 2734static int 2735root_print_child(device_t dev, device_t child) 2736{ 2737 int retval = 0; 2738 2739 retval += bus_print_child_header(dev, child); 2740 retval += printf("\n"); 2741 2742 return (retval); 2743} 2744 2745static kobj_method_t root_methods[] = { 2746 /* Device interface */ 2747 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 2748 KOBJMETHOD(device_suspend, bus_generic_suspend), 2749 KOBJMETHOD(device_resume, bus_generic_resume), 2750 2751 /* Bus interface */ 2752 KOBJMETHOD(bus_print_child, root_print_child), 2753 2754 KOBJMETHOD_END 2755}; 2756 2757static driver_t root_driver = { 2758 "root", 2759 root_methods, 2760 1, /* no softc */ 2761}; 2762 2763device_t root_bus; 2764devclass_t root_devclass; 2765 2766static int 2767root_bus_module_handler(module_t mod, int what, void* arg) 2768{ 2769 PDEBUG(("+")); 2770 switch (what) { 2771 case MOD_LOAD: 2772 TAILQ_INIT(&bus_data_devices); 2773 kobj_class_compile((kobj_class_t) &root_driver); 2774 root_bus = make_device(NULL, "root", 0); 2775 root_bus->desc = "System root bus"; 2776 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 2777 root_bus->driver = &root_driver; 2778 root_bus->state = DS_ATTACHED; 2779 root_devclass = devclass_find_internal("root", NULL, FALSE); 2780 //devinit(); 2781 return (0); 2782 2783 case MOD_SHUTDOWN: 2784 device_shutdown(root_bus); 2785 return (0); 2786 default: 2787 return (EOPNOTSUPP); 2788 } 2789 PDEBUG(("-")); 2790 2791 return (0); 2792} 2793 2794/** 2795 * @brief Automatically configure devices 2796 * 2797 * This function begins the autoconfiguration process by calling 2798 * device_probe_and_attach() for each child of the @c root0 device. 2799 */ 2800void 2801root_bus_configure(void) 2802{ 2803 PDEBUG(("+")); 2804 root_bus_module_handler(NULL, MOD_LOAD, NULL); 2805 2806 /* Eventually this will be split up, but this is sufficient for now. */ 2807 bus_set_pass(BUS_PASS_DEFAULT); 2808 PDEBUG(("-")); 2809} 2810 2811/** 2812 * @brief Module handler for registering device drivers 2813 * 2814 * This module handler is used to automatically register device 2815 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls 2816 * devclass_add_driver() for the driver described by the 2817 * driver_module_data structure pointed to by @p arg 2818 */ 2819int 2820driver_module_handler(module_t mod, int what, void *arg) 2821{ 2822 struct driver_module_data *dmd = NULL; 2823 devclass_t bus_devclass; 2824 kobj_class_t driver; 2825 int error, pass; 2826 2827 PDEBUG(("+")); 2828 2829 dmd = (struct driver_module_data *)arg; 2830 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 2831 if (bus_devclass == NULL) { 2832 PDEBUG(("-")); 2833 return EINVAL; 2834 } 2835 2836 error = 0; 2837 2838 switch (what) { 2839 case MOD_LOAD: 2840 if (dmd->dmd_chainevh) 2841 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 2842 2843 pass = dmd->dmd_pass; 2844 driver = dmd->dmd_driver; 2845 PDEBUG(("Loading module: driver %s on bus %s (pass %d)", 2846 DRIVERNAME(driver), dmd->dmd_busname, pass)); 2847 error = devclass_add_driver(bus_devclass, driver, pass, 2848 dmd->dmd_devclass); 2849 break; 2850 2851 case MOD_UNLOAD: 2852 PDEBUG(("Unloading module: driver %s from bus %s", 2853 DRIVERNAME(dmd->dmd_driver), 2854 dmd->dmd_busname)); 2855 error = devclass_delete_driver(bus_devclass, 2856 dmd->dmd_driver); 2857 2858 if (!error && dmd->dmd_chainevh) 2859 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 2860 break; 2861 case MOD_QUIESCE: 2862 PDEBUG(("Quiesce module: driver %s from bus %s", 2863 DRIVERNAME(dmd->dmd_driver), 2864 dmd->dmd_busname)); 2865 error = devclass_quiesce_driver(bus_devclass, 2866 dmd->dmd_driver); 2867 2868 if (!error && dmd->dmd_chainevh) 2869 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 2870 break; 2871 default: 2872 error = EOPNOTSUPP; 2873 break; 2874 } 2875 PDEBUG(("-")); 2876 2877 return (error); 2878} 2879 2880#ifdef BUS_DEBUG 2881 2882/* the _short versions avoid iteration by not calling anything that prints 2883 * more than oneliners. I love oneliners. 2884 */ 2885 2886static void 2887print_device_short(device_t dev, int indent) 2888{ 2889 if (!dev) 2890 return; 2891 2892 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 2893 dev->unit, dev->desc, 2894 (dev->parent? "":"no "), 2895 (TAILQ_EMPTY(&dev->children)? "no ":""), 2896 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 2897 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 2898 (dev->flags&DF_WILDCARD? "wildcard,":""), 2899 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 2900 (dev->flags&DF_REBID? "rebiddable,":""), 2901 (dev->ivars? "":"no "), 2902 (dev->softc? "":"no "), 2903 dev->busy)); 2904} 2905 2906static void 2907print_device(device_t dev, int indent) 2908{ 2909 if (!dev) 2910 return; 2911 2912 print_device_short(dev, indent); 2913 2914 indentprintf(("Parent:\n")); 2915 print_device_short(dev->parent, indent+1); 2916 indentprintf(("Driver:\n")); 2917 print_driver_short(dev->driver, indent+1); 2918 indentprintf(("Devclass:\n")); 2919 print_devclass_short(dev->devclass, indent+1); 2920} 2921 2922void 2923print_device_tree_short(device_t dev, int indent) 2924/* print the device and all its children (indented) */ 2925{ 2926 device_t child; 2927 2928 if (!dev) 2929 return; 2930 2931 print_device_short(dev, indent); 2932 2933 TAILQ_FOREACH(child, &dev->children, link) { 2934 print_device_tree_short(child, indent+1); 2935 } 2936} 2937 2938void 2939print_device_tree(device_t dev, int indent) 2940/* print the device and all its children (indented) */ 2941{ 2942 device_t child; 2943 2944 if (!dev) 2945 return; 2946 2947 print_device(dev, indent); 2948 2949 TAILQ_FOREACH(child, &dev->children, link) { 2950 print_device_tree(child, indent+1); 2951 } 2952} 2953 2954static void 2955print_driver_short(driver_t *driver, int indent) 2956{ 2957 if (!driver) 2958 return; 2959 2960 indentprintf(("driver %s: softc size = %zd\n", 2961 driver->name, driver->size)); 2962} 2963 2964static void 2965print_driver(driver_t *driver, int indent) 2966{ 2967 if (!driver) 2968 return; 2969 2970 print_driver_short(driver, indent); 2971} 2972 2973static void 2974print_driver_list(driver_list_t drivers, int indent) 2975{ 2976 driverlink_t driver = NULL; 2977 2978 TAILQ_FOREACH(driver, &drivers, link) { 2979 print_driver(driver->driver, indent); 2980 } 2981} 2982 2983static void 2984print_devclass_short(devclass_t dc, int indent) 2985{ 2986 if ( !dc ) 2987 return; 2988 2989 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 2990} 2991 2992static void 2993print_devclass(devclass_t dc, int indent) 2994{ 2995 int i; 2996 2997 if ( !dc ) 2998 return; 2999 3000 print_devclass_short(dc, indent); 3001 indentprintf(("Drivers:\n")); 3002 print_driver_list(dc->drivers, indent+1); 3003 3004 indentprintf(("Devices:\n")); 3005 for (i = 0; i < dc->maxunit; i++) 3006 if (dc->devices[i]) 3007 print_device(dc->devices[i], indent+1); 3008} 3009 3010void 3011print_devclass_list_short(void) 3012{ 3013 devclass_t dc; 3014 3015 printf("Short listing of devclasses, drivers & devices:\n"); 3016 TAILQ_FOREACH(dc, &devclasses, link) { 3017 print_devclass_short(dc, 0); 3018 } 3019} 3020 3021void 3022print_devclass_list(void) 3023{ 3024 devclass_t dc; 3025 3026 printf("Full listing of devclasses, drivers & devices:\n"); 3027 TAILQ_FOREACH(dc, &devclasses, link) { 3028 print_devclass(dc, 0); 3029 } 3030} 3031 3032#endif 3033 3034/* port for interrupt setup and teardown */ 3035int 3036bus_setup_intr(int irq, int flags, driver_intr_t *intr, void *arg) 3037{ 3038 int ret; 3039 HwiIrqParam irqParam = {0}; 3040 3041 if (OS_INT_ACTIVE) { 3042 return OS_ERRNO_HWI_INTERR; 3043 } 3044 3045 irqParam.swIrq = irq; 3046 irqParam.pDevId = arg; 3047 3048 ret = LOS_HwiCreate(irq, 0, (HWI_MODE_T)flags, (HWI_PROC_FUNC)intr, &irqParam); 3049 if (ret == LOS_OK) { 3050 HalIrqUnmask(irq); 3051 } 3052 return ret; 3053} 3054int 3055bus_teardown_intr(int irq, void *arg) 3056{ 3057 HwiIrqParam irqParam = {0}; 3058 3059 if (OS_INT_ACTIVE) { 3060 return -1; 3061 } 3062 3063 irqParam.swIrq = irq; 3064 irqParam.pDevId = arg; 3065 3066 return LOS_HwiDelete(irq, &irqParam); 3067} 3068