subr_bus.c revision 253346
169990Sdes/*- 269990Sdes * Copyright (c) 1997,1998,2003 Doug Rabson 369990Sdes * All rights reserved. 469990Sdes * 569990Sdes * Redistribution and use in source and binary forms, with or without 669990Sdes * modification, are permitted provided that the following conditions 769990Sdes * are met: 869990Sdes * 1. Redistributions of source code must retain the above copyright 969990Sdes * notice, this list of conditions and the following disclaimer. 1069990Sdes * 2. Redistributions in binary form must reproduce the above copyright 1169990Sdes * notice, this list of conditions and the following disclaimer in the 1269990Sdes * documentation and/or other materials provided with the distribution. 1369990Sdes * 1469990Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1569990Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1669990Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1769990Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1869990Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1969990Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2069990Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2169990Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2269990Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2369990Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2469990Sdes * SUCH DAMAGE. 2569990Sdes */ 2669990Sdes 2769990Sdes#include <sys/cdefs.h> 2869990Sdes__FBSDID("$FreeBSD: head/sys/kern/subr_bus.c 253346 2013-07-15 01:32:55Z rodrigc $"); 29116182Sobrien 30116182Sobrien#include "opt_bus.h" 31116182Sobrien 3269990Sdes#include <sys/param.h> 3374840Sken#include <sys/conf.h> 3474840Sken#include <sys/filio.h> 3584097Sdes#include <sys/lock.h> 3669990Sdes#include <sys/kernel.h> 3769990Sdes#include <sys/kobj.h> 3869990Sdes#include <sys/limits.h> 3984097Sdes#include <sys/malloc.h> 4069990Sdes#include <sys/module.h> 4174840Sken#include <sys/mutex.h> 4284097Sdes#include <sys/poll.h> 4374840Sken#include <sys/proc.h> 4488950Skbyanc#include <sys/condvar.h> 4578340Sjlemon#include <sys/queue.h> 4688950Skbyanc#include <machine/bus.h> 4774840Sken#include <sys/rman.h> 4869990Sdes#include <sys/selinfo.h> 4984097Sdes#include <sys/signalvar.h> 5084097Sdes#include <sys/sysctl.h> 5174840Sken#include <sys/systm.h> 5269990Sdes#include <sys/uio.h> 53111119Simp#include <sys/bus.h> 5489121Skbyanc#include <sys/interrupt.h> 5574840Sken 5689121Skbyanc#include <net/vnet.h> 5789121Skbyanc 5889121Skbyanc#include <machine/stdarg.h> 5989121Skbyanc 6074840Sken#include <vm/uma.h> 6169990Sdes 6271721SdesSYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); 6371721SdesSYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL); 6471721Sdes 6589121Skbyanc/* 6689121Skbyanc * Used to attach drivers to devclasses. 6789121Skbyanc */ 6889121Skbyanctypedef struct driverlink *driverlink_t; 6989121Skbyancstruct driverlink { 7088950Skbyanc kobj_class_t driver; 7188950Skbyanc TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */ 7271721Sdes int pass; 7371721Sdes TAILQ_ENTRY(driverlink) passlink; 7471721Sdes}; 7571721Sdes 7689121Skbyanc/* 7789121Skbyanc * Forward declarations 7871721Sdes */ 7989121Skbyanctypedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t; 8089121Skbyanctypedef TAILQ_HEAD(driver_list, driverlink) driver_list_t; 8189121Skbyanctypedef TAILQ_HEAD(device_list, device) device_list_t; 8288950Skbyanc 8371721Sdesstruct devclass { 8471721Sdes TAILQ_ENTRY(devclass) link; 8571721Sdes devclass_t parent; /* parent in devclass hierarchy */ 8674840Sken driver_list_t drivers; /* bus devclasses store drivers for bus */ 8769990Sdes char *name; 8892664Speter device_t *devices; /* array of devices indexed by unit */ 8969990Sdes int maxunit; /* size of devices array */ 9069990Sdes int flags; 9173891Sdes#define DC_HAS_CHILDREN 1 9269990Sdes 9388950Skbyanc struct sysctl_ctx_list sysctl_ctx; 9469990Sdes struct sysctl_oid *sysctl_tree; 9569990Sdes}; 9669990Sdes 9769990Sdes/** 9869990Sdes * @brief Implementation of device. 9992664Speter */ 10069990Sdesstruct device { 10169990Sdes /* 10273891Sdes * A device is a kernel object. The first field must be the 10369990Sdes * current ops table for the object. 10469990Sdes */ 10589121Skbyanc KOBJ_FIELDS; 10689121Skbyanc 10774840Sken /* 10889121Skbyanc * Device hierarchy. 10989121Skbyanc */ 11074840Sken TAILQ_ENTRY(device) link; /**< list of devices in parent */ 11169990Sdes TAILQ_ENTRY(device) devlink; /**< global device list membership */ 11288950Skbyanc device_t parent; /**< parent of this device */ 11388950Skbyanc device_list_t children; /**< list of child devices */ 11488950Skbyanc 11588950Skbyanc /* 11688950Skbyanc * Details of this device. 11788950Skbyanc */ 11888950Skbyanc driver_t *driver; /**< current driver */ 119103404Sphk devclass_t devclass; /**< current device class */ 12088950Skbyanc int unit; /**< current unit number */ 12188950Skbyanc char* nameunit; /**< name+unit e.g. foodev0 */ 12288950Skbyanc char* desc; /**< driver specific description */ 12388950Skbyanc int busy; /**< count of calls to device_busy() */ 12488950Skbyanc device_state_t state; /**< current device state */ 12588950Skbyanc uint32_t devflags; /**< api level flags for device_get_flags() */ 12688950Skbyanc u_int flags; /**< internal device flags */ 12788950Skbyanc#define DF_ENABLED 0x01 /* device should be probed/attached */ 12888950Skbyanc#define DF_FIXEDCLASS 0x02 /* devclass specified at create time */ 12969990Sdes#define DF_WILDCARD 0x04 /* unit was originally wildcard */ 13088950Skbyanc#define DF_DESCMALLOCED 0x08 /* description was malloced */ 13188950Skbyanc#define DF_QUIET 0x10 /* don't print verbose attach message */ 13288950Skbyanc#define DF_DONENOMATCH 0x20 /* don't execute DEVICE_NOMATCH again */ 13388950Skbyanc#define DF_EXTERNALSOFTC 0x40 /* softc not allocated by us */ 13488950Skbyanc#define DF_REBID 0x80 /* Can rebid after attach */ 13588950Skbyanc u_int order; /**< order from device_add_child_ordered() */ 13688950Skbyanc void *ivars; /**< instance variables */ 13788950Skbyanc void *softc; /**< current driver's variables */ 13888950Skbyanc 13988950Skbyanc struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */ 14088950Skbyanc struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */ 14188950Skbyanc}; 14288950Skbyanc 14388950Skbyancstatic MALLOC_DEFINE(M_BUS, "bus", "Bus data structures"); 14488950Skbyancstatic MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc"); 14588950Skbyanc 14688950Skbyanc#ifdef BUS_DEBUG 14788950Skbyanc 14888950Skbyancstatic int bus_debug = 1; 14988950SkbyancTUNABLE_INT("bus.debug", &bus_debug); 15088950SkbyancSYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0, 15188950Skbyanc "Debug bus code"); 15288950Skbyanc 15388950Skbyanc#define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");} 15488950Skbyanc#define DEVICENAME(d) ((d)? device_get_name(d): "no device") 15588950Skbyanc#define DRIVERNAME(d) ((d)? d->name : "no driver") 15669990Sdes#define DEVCLANAME(d) ((d)? d->name : "no devclass") 15769990Sdes 15869990Sdes/** 15969990Sdes * Produce the indenting, indent*2 spaces plus a '.' ahead of that to 16077989Sdes * prevent syslog from deleting initial spaces 16171721Sdes */ 16269990Sdes#define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0) 16371721Sdes 16471721Sdesstatic void print_device_short(device_t dev, int indent); 16588950Skbyancstatic void print_device(device_t dev, int indent); 16688950Skbyancvoid print_device_tree_short(device_t dev, int indent); 16769990Sdesvoid print_device_tree(device_t dev, int indent); 16888950Skbyancstatic void print_driver_short(driver_t *driver, int indent); 16977989Sdesstatic void print_driver(driver_t *driver, int indent); 17077989Sdesstatic void print_driver_list(driver_list_t drivers, int indent); 17177989Sdesstatic void print_devclass_short(devclass_t dc, int indent); 17277989Sdesstatic void print_devclass(devclass_t dc, int indent); 17377989Sdesvoid print_devclass_list_short(void); 17488950Skbyancvoid print_devclass_list(void); 17577989Sdes 17677989Sdes#else 17777989Sdes/* Make the compiler ignore the function calls */ 17888950Skbyanc#define PDEBUG(a) /* nop */ 17977989Sdes#define DEVICENAME(d) /* nop */ 18069990Sdes#define DRIVERNAME(d) /* nop */ 18169990Sdes#define DEVCLANAME(d) /* nop */ 18269990Sdes 18377989Sdes#define print_device_short(d,i) /* nop */ 18469990Sdes#define print_device(d,i) /* nop */ 18588950Skbyanc#define print_device_tree_short(d,i) /* nop */ 18688950Skbyanc#define print_device_tree(d,i) /* nop */ 18774840Sken#define print_driver_short(d,i) /* nop */ 18877989Sdes#define print_driver(d,i) /* nop */ 18977989Sdes#define print_driver_list(d,i) /* nop */ 19077989Sdes#define print_devclass_short(d,i) /* nop */ 19177989Sdes#define print_devclass(d,i) /* nop */ 19277989Sdes#define print_devclass_list_short() /* nop */ 19369990Sdes#define print_devclass_list() /* nop */ 19477989Sdes#endif 19569990Sdes 19669990Sdes/* 19784097Sdes * dev sysctl tree 19869990Sdes */ 19984097Sdes 20084097Sdesenum { 20184097Sdes DEVCLASS_SYSCTL_PARENT, 20284097Sdes}; 20384097Sdes 20484097Sdesstatic int 20587594Sobriendevclass_sysctl_handler(SYSCTL_HANDLER_ARGS) 20684097Sdes{ 20787594Sobrien devclass_t dc = (devclass_t)arg1; 20884097Sdes const char *value; 20984097Sdes 21084097Sdes switch (arg2) { 21184097Sdes case DEVCLASS_SYSCTL_PARENT: 21284097Sdes value = dc->parent ? dc->parent->name : ""; 21384097Sdes break; 21484097Sdes default: 21584097Sdes return (EINVAL); 21684097Sdes } 21784097Sdes return (SYSCTL_OUT(req, value, strlen(value))); 21884097Sdes} 21984097Sdes 22084097Sdesstatic void 22184097Sdesdevclass_sysctl_init(devclass_t dc) 22284097Sdes{ 22384097Sdes 22484097Sdes if (dc->sysctl_tree != NULL) 22584097Sdes return; 22688950Skbyanc sysctl_ctx_init(&dc->sysctl_ctx); 22771721Sdes dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx, 22871721Sdes SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name, 22971721Sdes CTLFLAG_RD, NULL, ""); 23071721Sdes SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree), 23171721Sdes OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD, 23271724Sdes dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A", 23371721Sdes "parent class"); 23471721Sdes} 23571721Sdes 23671721Sdesenum { 23771721Sdes DEVICE_SYSCTL_DESC, 23871721Sdes DEVICE_SYSCTL_DRIVER, 23971721Sdes DEVICE_SYSCTL_LOCATION, 24088950Skbyanc DEVICE_SYSCTL_PNPINFO, 24188950Skbyanc DEVICE_SYSCTL_PARENT, 24269990Sdes}; 24369990Sdes 24471721Sdesstatic int 24569990Sdesdevice_sysctl_handler(SYSCTL_HANDLER_ARGS) 24669990Sdes{ 24769990Sdes device_t dev = (device_t)arg1; 248125937Sdes const char *value; 24969990Sdes char *buf; 25069990Sdes int error; 25169990Sdes 25269990Sdes buf = NULL; 253125937Sdes switch (arg2) { 25469990Sdes case DEVICE_SYSCTL_DESC: 25569990Sdes value = dev->desc ? dev->desc : ""; 25669990Sdes break; 25769990Sdes case DEVICE_SYSCTL_DRIVER: 25869990Sdes value = dev->driver ? dev->driver->name : ""; 25969990Sdes break; 26069990Sdes case DEVICE_SYSCTL_LOCATION: 26178077Sdes value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); 26278077Sdes bus_child_location_str(dev, buf, 1024); 26378077Sdes break; 264131869Sdes case DEVICE_SYSCTL_PNPINFO: 26578077Sdes value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO); 266131869Sdes bus_child_pnpinfo_str(dev, buf, 1024); 267131868Sdes break; 26878077Sdes case DEVICE_SYSCTL_PARENT: 26978077Sdes value = dev->parent ? dev->parent->nameunit : ""; 270125937Sdes break; 27178077Sdes default: 27278077Sdes return (EINVAL); 273125937Sdes } 27489765Sphk error = SYSCTL_OUT(req, value, strlen(value)); 27588950Skbyanc if (buf != NULL) 27688950Skbyanc free(buf, M_BUS); 27778077Sdes return (error); 27888950Skbyanc} 27978077Sdes 28078077Sdesstatic void 28178077Sdesdevice_sysctl_init(device_t dev) 28278077Sdes{ 28378077Sdes devclass_t dc = dev->devclass; 28478077Sdes 28578077Sdes if (dev->sysctl_tree != NULL) 28678077Sdes return; 28778077Sdes devclass_sysctl_init(dc); 28878077Sdes sysctl_ctx_init(&dev->sysctl_ctx); 28978077Sdes dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx, 29078077Sdes SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO, 29178077Sdes dev->nameunit + strlen(dc->name), 29278077Sdes CTLFLAG_RD, NULL, ""); 29378077Sdes SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 29478077Sdes OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD, 29578077Sdes dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A", 29678077Sdes "device description"); 29778077Sdes SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 29878077Sdes OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD, 29978077Sdes dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A", 30078077Sdes "device driver name"); 30188950Skbyanc SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 30288950Skbyanc OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD, 30388950Skbyanc dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A", 30488950Skbyanc "device location relative to parent"); 30578092Sdes SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 30678092Sdes OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD, 30778095Sdes dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A", 308125937Sdes "device identification"); 30978077Sdes SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree), 31078077Sdes OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD, 31178077Sdes dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A", 31278077Sdes "parent device"); 31378077Sdes} 31478077Sdes 31578077Sdesstatic void 31678077Sdesdevice_sysctl_update(device_t dev) 317131869Sdes{ 31878077Sdes devclass_t dc = dev->devclass; 31978077Sdes 32078077Sdes if (dev->sysctl_tree == NULL) 321125937Sdes return; 32278077Sdes sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name)); 323131869Sdes} 32478077Sdes 32578077Sdesstatic void 32678077Sdesdevice_sysctl_fini(device_t dev) 32769990Sdes{ 32869990Sdes if (dev->sysctl_tree == NULL) 32969990Sdes return; 33074840Sken sysctl_ctx_free(&dev->sysctl_ctx); 33169990Sdes dev->sysctl_tree = NULL; 33269990Sdes} 33369990Sdes 334125937Sdes/* 33569990Sdes * /dev/devctl implementation 33669990Sdes */ 337125937Sdes 33888950Skbyanc/* 33988950Skbyanc * This design allows only one reader for /dev/devctl. This is not desirable 34088950Skbyanc * in the long run, but will get a lot of hair out of this implementation. 34169990Sdes * Maybe we should make this device a clonable device. 34288950Skbyanc * 34369990Sdes * Also note: we specifically do not attach a device to the device_t tree 34469990Sdes * to avoid potential chicken and egg problems. One could argue that all 34569990Sdes * of this belongs to the root node. One could also further argue that the 34669990Sdes * sysctl interface that we have not might more properly be an ioctl 34769990Sdes * interface, but at this stage of the game, I'm not inclined to rock that 34869990Sdes * boat. 34969990Sdes * 35078077Sdes * I'm also not sure that the SIGIO support is done correctly or not, as 35169990Sdes * I copied it from a driver that had SIGIO support that likely hasn't been 35288950Skbyanc * tested since 3.4 or 2.2.8! 35378077Sdes */ 35478077Sdes 35578077Sdes/* Deprecated way to adjust queue length */ 35678077Sdesstatic int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS); 35778077Sdes/* XXX Need to support old-style tunable hw.bus.devctl_disable" */ 358125937SdesSYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, NULL, 35978077Sdes 0, sysctl_devctl_disable, "I", "devctl disable -- deprecated"); 36078077Sdes 36178077Sdes#define DEVCTL_DEFAULT_QUEUE_LEN 1000 36278077Sdesstatic int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS); 36378077Sdesstatic int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN; 36478077SdesTUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length); 36588950SkbyancSYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW, NULL, 36688950Skbyanc 0, sysctl_devctl_queue, "I", "devctl queue length"); 36788950Skbyanc 36888950Skbyancstatic d_open_t devopen; 36988950Skbyancstatic d_close_t devclose; 37088950Skbyancstatic d_read_t devread; 37178077Sdesstatic d_ioctl_t devioctl; 37278077Sdesstatic d_poll_t devpoll; 37378077Sdes 37478077Sdesstatic struct cdevsw dev_cdevsw = { 37578077Sdes .d_version = D_VERSION, 37678077Sdes .d_flags = D_NEEDGIANT, 37778077Sdes .d_open = devopen, 37878077Sdes .d_close = devclose, 37978077Sdes .d_read = devread, 38078077Sdes .d_ioctl = devioctl, 381125937Sdes .d_poll = devpoll, 38278077Sdes .d_name = "devctl", 38378077Sdes}; 38478077Sdes 38578077Sdesstruct dev_event_info 38678077Sdes{ 38769990Sdes char *dei_data; 38869990Sdes TAILQ_ENTRY(dev_event_info) dei_link; 38969990Sdes}; 39074840Sken 39169990SdesTAILQ_HEAD(devq, dev_event_info); 39269990Sdes 39369990Sdesstatic struct dev_softc 394125937Sdes{ 39571721Sdes int inuse; 39669990Sdes int nonblock; 39769990Sdes int queued; 39869990Sdes struct mtx mtx; 39969990Sdes struct cv cv; 40088950Skbyanc struct selinfo sel; 40169990Sdes struct devq devq; 40269990Sdes struct proc *async_proc; 40388950Skbyanc} devsoftc; 40469990Sdes 405115311Speterstatic struct cdev *devctl_dev; 40671721Sdes 40769990Sdesstatic void 40869990Sdesdevinit(void) 40969990Sdes{ 41088950Skbyanc devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL, 41169990Sdes UID_ROOT, GID_WHEEL, 0600, "devctl"); 41287594Sobrien mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF); 41388950Skbyanc cv_init(&devsoftc.cv, "dev cv"); 41469990Sdes TAILQ_INIT(&devsoftc.devq); 41569990Sdes} 41669990Sdes 41788950Skbyancstatic int 418115311Speterdevopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 41988950Skbyanc{ 420115311Speter if (devsoftc.inuse) 421115311Speter return (EBUSY); 42288950Skbyanc /* move to init */ 42388950Skbyanc devsoftc.inuse = 1; 42469990Sdes devsoftc.nonblock = 0; 42574840Sken devsoftc.async_proc = NULL; 42674840Sken return (0); 42774840Sken} 42874840Sken 42974840Skenstatic int 43074840Skendevclose(struct cdev *dev, int fflag, int devtype, struct thread *td) 43174840Sken{ 43274840Sken devsoftc.inuse = 0; 43374840Sken mtx_lock(&devsoftc.mtx); 43488950Skbyanc cv_broadcast(&devsoftc.cv); 43589646Sphk mtx_unlock(&devsoftc.mtx); 43674840Sken devsoftc.async_proc = NULL; 43774840Sken return (0); 43869990Sdes} 43969990Sdes 44069990Sdes/* 44169990Sdes * The read channel for this device is used to report changes to 44269990Sdes * userland in realtime. We are required to free the data as well as 44369990Sdes * the n1 object because we allocate them separately. Also note that 44469990Sdes * we return one record at a time. If you try to read this device a 44569990Sdes * character at a time, you will lose the rest of the data. Listening 44669990Sdes * programs are expected to cope. 44788950Skbyanc */ 44888950Skbyancstatic int 44988950Skbyancdevread(struct cdev *dev, struct uio *uio, int ioflag) 45088950Skbyanc{ 45188950Skbyanc struct dev_event_info *n1; 45288950Skbyanc int rv; 45388950Skbyanc 45488950Skbyanc mtx_lock(&devsoftc.mtx); 45588950Skbyanc while (TAILQ_EMPTY(&devsoftc.devq)) { 45688950Skbyanc if (devsoftc.nonblock) { 45788950Skbyanc mtx_unlock(&devsoftc.mtx); 45888950Skbyanc return (EAGAIN); 45988950Skbyanc } 46088950Skbyanc rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx); 46188950Skbyanc if (rv) { 46269990Sdes /* 46369990Sdes * Need to translate ERESTART to EINTR here? -- jake 46469990Sdes */ 46569990Sdes mtx_unlock(&devsoftc.mtx); 46669990Sdes return (rv); 46769990Sdes } 46869990Sdes } 469125937Sdes n1 = TAILQ_FIRST(&devsoftc.devq); 47069990Sdes TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 47169990Sdes devsoftc.queued--; 472125937Sdes mtx_unlock(&devsoftc.mtx); 47388950Skbyanc rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio); 47469990Sdes free(n1->dei_data, M_BUS); 47569990Sdes free(n1, M_BUS); 47669990Sdes return (rv); 47773891Sdes} 47873891Sdes 47969990Sdesstatic int 48069990Sdesdevioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 48169990Sdes{ 48269990Sdes switch (cmd) { 48388950Skbyanc 48484097Sdes case FIONBIO: 48584097Sdes if (*(int*)data) 48684097Sdes devsoftc.nonblock = 1; 48784097Sdes else 48884097Sdes devsoftc.nonblock = 0; 48984097Sdes return (0); 490125937Sdes case FIOASYNC: 49184097Sdes if (*(int*)data) 49284097Sdes devsoftc.async_proc = td->td_proc; 493125937Sdes else 49484097Sdes devsoftc.async_proc = NULL; 49584097Sdes return (0); 49684097Sdes 49784097Sdes /* (un)Support for other fcntl() calls. */ 49884097Sdes case FIOCLEX: 49984097Sdes case FIONCLEX: 50084097Sdes case FIONREAD: 50171721Sdes case FIOSETOWN: 50271721Sdes case FIOGETOWN: 50371721Sdes default: 50471721Sdes break; 50571721Sdes } 50671721Sdes return (ENOTTY); 50771721Sdes} 50871721Sdes 50971721Sdesstatic int 51069990Sdesdevpoll(struct cdev *dev, int events, struct thread *td) 51169990Sdes{ 51271721Sdes int revents = 0; 51369990Sdes 51469990Sdes mtx_lock(&devsoftc.mtx); 51569990Sdes if (events & (POLLIN | POLLRDNORM)) { 51669990Sdes if (!TAILQ_EMPTY(&devsoftc.devq)) 517125937Sdes revents = events & (POLLIN | POLLRDNORM); 51873891Sdes else 51971721Sdes selrecord(td, &devsoftc.sel); 52069990Sdes } 52169990Sdes mtx_unlock(&devsoftc.mtx); 52269990Sdes 52369990Sdes return (revents); 52469990Sdes} 52569990Sdes 52669990Sdes/** 52769990Sdes * @brief Return whether the userland process is running 52869990Sdes */ 52969990Sdesboolean_t 53069990Sdesdevctl_process_running(void) 531125937Sdes{ 53269990Sdes return (devsoftc.inuse == 1); 53369990Sdes} 53469990Sdes 53569990Sdes/** 53669990Sdes * @brief Queue data to be read from the devctl device 53769990Sdes * 53871721Sdes * Generic interface to queue data to the devctl device. It is 53969990Sdes * assumed that @p data is properly formatted. It is further assumed 54069990Sdes * that @p data is allocated using the M_BUS malloc type. 54169990Sdes */ 54271724Sdesvoid 543125937Sdesdevctl_queue_data_f(char *data, int flags) 54469990Sdes{ 54571721Sdes struct dev_event_info *n1 = NULL, *n2 = NULL; 54669990Sdes struct proc *p; 54769990Sdes 54869990Sdes if (strlen(data) == 0) 54969990Sdes goto out; 55069990Sdes if (devctl_queue_length == 0) 55169990Sdes goto out; 55269990Sdes n1 = malloc(sizeof(*n1), M_BUS, flags); 55369990Sdes if (n1 == NULL) 55469990Sdes goto out; 55588219Sdillon n1->dei_data = data; 55688219Sdillon mtx_lock(&devsoftc.mtx); 55769990Sdes if (devctl_queue_length == 0) { 55869990Sdes mtx_unlock(&devsoftc.mtx); 559125937Sdes free(n1->dei_data, M_BUS); 56069990Sdes free(n1, M_BUS); 56174840Sken return; 56288219Sdillon } 56369990Sdes /* Leave at least one spot in the queue... */ 56488219Sdillon while (devsoftc.queued > devctl_queue_length - 1) { 56577989Sdes n2 = TAILQ_FIRST(&devsoftc.devq); 56669990Sdes TAILQ_REMOVE(&devsoftc.devq, n2, dei_link); 567104449Sphk free(n2->dei_data, M_BUS); 568104449Sphk free(n2, M_BUS); 569104449Sphk devsoftc.queued--; 570104449Sphk } 571104449Sphk TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link); 572104449Sphk devsoftc.queued++; 573104449Sphk cv_broadcast(&devsoftc.cv); 574104449Sphk mtx_unlock(&devsoftc.mtx); 575104449Sphk selwakeup(&devsoftc.sel); 576104449Sphk p = devsoftc.async_proc; 577 if (p != NULL) { 578 PROC_LOCK(p); 579 kern_psignal(p, SIGIO); 580 PROC_UNLOCK(p); 581 } 582 return; 583out: 584 /* 585 * We have to free data on all error paths since the caller 586 * assumes it will be free'd when this item is dequeued. 587 */ 588 free(data, M_BUS); 589 return; 590} 591 592void 593devctl_queue_data(char *data) 594{ 595 596 devctl_queue_data_f(data, M_NOWAIT); 597} 598 599/** 600 * @brief Send a 'notification' to userland, using standard ways 601 */ 602void 603devctl_notify_f(const char *system, const char *subsystem, const char *type, 604 const char *data, int flags) 605{ 606 int len = 0; 607 char *msg; 608 609 if (system == NULL) 610 return; /* BOGUS! Must specify system. */ 611 if (subsystem == NULL) 612 return; /* BOGUS! Must specify subsystem. */ 613 if (type == NULL) 614 return; /* BOGUS! Must specify type. */ 615 len += strlen(" system=") + strlen(system); 616 len += strlen(" subsystem=") + strlen(subsystem); 617 len += strlen(" type=") + strlen(type); 618 /* add in the data message plus newline. */ 619 if (data != NULL) 620 len += strlen(data); 621 len += 3; /* '!', '\n', and NUL */ 622 msg = malloc(len, M_BUS, flags); 623 if (msg == NULL) 624 return; /* Drop it on the floor */ 625 if (data != NULL) 626 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n", 627 system, subsystem, type, data); 628 else 629 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n", 630 system, subsystem, type); 631 devctl_queue_data_f(msg, flags); 632} 633 634void 635devctl_notify(const char *system, const char *subsystem, const char *type, 636 const char *data) 637{ 638 639 devctl_notify_f(system, subsystem, type, data, M_NOWAIT); 640} 641 642/* 643 * Common routine that tries to make sending messages as easy as possible. 644 * We allocate memory for the data, copy strings into that, but do not 645 * free it unless there's an error. The dequeue part of the driver should 646 * free the data. We don't send data when the device is disabled. We do 647 * send data, even when we have no listeners, because we wish to avoid 648 * races relating to startup and restart of listening applications. 649 * 650 * devaddq is designed to string together the type of event, with the 651 * object of that event, plus the plug and play info and location info 652 * for that event. This is likely most useful for devices, but less 653 * useful for other consumers of this interface. Those should use 654 * the devctl_queue_data() interface instead. 655 */ 656static void 657devaddq(const char *type, const char *what, device_t dev) 658{ 659 char *data = NULL; 660 char *loc = NULL; 661 char *pnp = NULL; 662 const char *parstr; 663 664 if (!devctl_queue_length)/* Rare race, but lost races safely discard */ 665 return; 666 data = malloc(1024, M_BUS, M_NOWAIT); 667 if (data == NULL) 668 goto bad; 669 670 /* get the bus specific location of this device */ 671 loc = malloc(1024, M_BUS, M_NOWAIT); 672 if (loc == NULL) 673 goto bad; 674 *loc = '\0'; 675 bus_child_location_str(dev, loc, 1024); 676 677 /* Get the bus specific pnp info of this device */ 678 pnp = malloc(1024, M_BUS, M_NOWAIT); 679 if (pnp == NULL) 680 goto bad; 681 *pnp = '\0'; 682 bus_child_pnpinfo_str(dev, pnp, 1024); 683 684 /* Get the parent of this device, or / if high enough in the tree. */ 685 if (device_get_parent(dev) == NULL) 686 parstr = "."; /* Or '/' ? */ 687 else 688 parstr = device_get_nameunit(device_get_parent(dev)); 689 /* String it all together. */ 690 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp, 691 parstr); 692 free(loc, M_BUS); 693 free(pnp, M_BUS); 694 devctl_queue_data(data); 695 return; 696bad: 697 free(pnp, M_BUS); 698 free(loc, M_BUS); 699 free(data, M_BUS); 700 return; 701} 702 703/* 704 * A device was added to the tree. We are called just after it successfully 705 * attaches (that is, probe and attach success for this device). No call 706 * is made if a device is merely parented into the tree. See devnomatch 707 * if probe fails. If attach fails, no notification is sent (but maybe 708 * we should have a different message for this). 709 */ 710static void 711devadded(device_t dev) 712{ 713 devaddq("+", device_get_nameunit(dev), dev); 714} 715 716/* 717 * A device was removed from the tree. We are called just before this 718 * happens. 719 */ 720static void 721devremoved(device_t dev) 722{ 723 devaddq("-", device_get_nameunit(dev), dev); 724} 725 726/* 727 * Called when there's no match for this device. This is only called 728 * the first time that no match happens, so we don't keep getting this 729 * message. Should that prove to be undesirable, we can change it. 730 * This is called when all drivers that can attach to a given bus 731 * decline to accept this device. Other errors may not be detected. 732 */ 733static void 734devnomatch(device_t dev) 735{ 736 devaddq("?", "", dev); 737} 738 739static int 740sysctl_devctl_disable(SYSCTL_HANDLER_ARGS) 741{ 742 struct dev_event_info *n1; 743 int dis, error; 744 745 dis = devctl_queue_length == 0; 746 error = sysctl_handle_int(oidp, &dis, 0, req); 747 if (error || !req->newptr) 748 return (error); 749 mtx_lock(&devsoftc.mtx); 750 if (dis) { 751 while (!TAILQ_EMPTY(&devsoftc.devq)) { 752 n1 = TAILQ_FIRST(&devsoftc.devq); 753 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 754 free(n1->dei_data, M_BUS); 755 free(n1, M_BUS); 756 } 757 devsoftc.queued = 0; 758 devctl_queue_length = 0; 759 } else { 760 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN; 761 } 762 mtx_unlock(&devsoftc.mtx); 763 return (0); 764} 765 766static int 767sysctl_devctl_queue(SYSCTL_HANDLER_ARGS) 768{ 769 struct dev_event_info *n1; 770 int q, error; 771 772 q = devctl_queue_length; 773 error = sysctl_handle_int(oidp, &q, 0, req); 774 if (error || !req->newptr) 775 return (error); 776 if (q < 0) 777 return (EINVAL); 778 mtx_lock(&devsoftc.mtx); 779 devctl_queue_length = q; 780 while (devsoftc.queued > devctl_queue_length) { 781 n1 = TAILQ_FIRST(&devsoftc.devq); 782 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link); 783 free(n1->dei_data, M_BUS); 784 free(n1, M_BUS); 785 devsoftc.queued--; 786 } 787 mtx_unlock(&devsoftc.mtx); 788 return (0); 789} 790 791/* End of /dev/devctl code */ 792 793static TAILQ_HEAD(,device) bus_data_devices; 794static int bus_data_generation = 1; 795 796static kobj_method_t null_methods[] = { 797 KOBJMETHOD_END 798}; 799 800DEFINE_CLASS(null, null_methods, 0); 801 802/* 803 * Bus pass implementation 804 */ 805 806static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes); 807int bus_current_pass = BUS_PASS_ROOT; 808 809/** 810 * @internal 811 * @brief Register the pass level of a new driver attachment 812 * 813 * Register a new driver attachment's pass level. If no driver 814 * attachment with the same pass level has been added, then @p new 815 * will be added to the global passes list. 816 * 817 * @param new the new driver attachment 818 */ 819static void 820driver_register_pass(struct driverlink *new) 821{ 822 struct driverlink *dl; 823 824 /* We only consider pass numbers during boot. */ 825 if (bus_current_pass == BUS_PASS_DEFAULT) 826 return; 827 828 /* 829 * Walk the passes list. If we already know about this pass 830 * then there is nothing to do. If we don't, then insert this 831 * driver link into the list. 832 */ 833 TAILQ_FOREACH(dl, &passes, passlink) { 834 if (dl->pass < new->pass) 835 continue; 836 if (dl->pass == new->pass) 837 return; 838 TAILQ_INSERT_BEFORE(dl, new, passlink); 839 return; 840 } 841 TAILQ_INSERT_TAIL(&passes, new, passlink); 842} 843 844/** 845 * @brief Raise the current bus pass 846 * 847 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS() 848 * method on the root bus to kick off a new device tree scan for each 849 * new pass level that has at least one driver. 850 */ 851void 852bus_set_pass(int pass) 853{ 854 struct driverlink *dl; 855 856 if (bus_current_pass > pass) 857 panic("Attempt to lower bus pass level"); 858 859 TAILQ_FOREACH(dl, &passes, passlink) { 860 /* Skip pass values below the current pass level. */ 861 if (dl->pass <= bus_current_pass) 862 continue; 863 864 /* 865 * Bail once we hit a driver with a pass level that is 866 * too high. 867 */ 868 if (dl->pass > pass) 869 break; 870 871 /* 872 * Raise the pass level to the next level and rescan 873 * the tree. 874 */ 875 bus_current_pass = dl->pass; 876 BUS_NEW_PASS(root_bus); 877 } 878 879 /* 880 * If there isn't a driver registered for the requested pass, 881 * then bus_current_pass might still be less than 'pass'. Set 882 * it to 'pass' in that case. 883 */ 884 if (bus_current_pass < pass) 885 bus_current_pass = pass; 886 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level")); 887} 888 889/* 890 * Devclass implementation 891 */ 892 893static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses); 894 895/** 896 * @internal 897 * @brief Find or create a device class 898 * 899 * If a device class with the name @p classname exists, return it, 900 * otherwise if @p create is non-zero create and return a new device 901 * class. 902 * 903 * If @p parentname is non-NULL, the parent of the devclass is set to 904 * the devclass of that name. 905 * 906 * @param classname the devclass name to find or create 907 * @param parentname the parent devclass name or @c NULL 908 * @param create non-zero to create a devclass 909 */ 910static devclass_t 911devclass_find_internal(const char *classname, const char *parentname, 912 int create) 913{ 914 devclass_t dc; 915 916 PDEBUG(("looking for %s", classname)); 917 if (!classname) 918 return (NULL); 919 920 TAILQ_FOREACH(dc, &devclasses, link) { 921 if (!strcmp(dc->name, classname)) 922 break; 923 } 924 925 if (create && !dc) { 926 PDEBUG(("creating %s", classname)); 927 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1, 928 M_BUS, M_NOWAIT | M_ZERO); 929 if (!dc) 930 return (NULL); 931 dc->parent = NULL; 932 dc->name = (char*) (dc + 1); 933 strcpy(dc->name, classname); 934 TAILQ_INIT(&dc->drivers); 935 TAILQ_INSERT_TAIL(&devclasses, dc, link); 936 937 bus_data_generation_update(); 938 } 939 940 /* 941 * If a parent class is specified, then set that as our parent so 942 * that this devclass will support drivers for the parent class as 943 * well. If the parent class has the same name don't do this though 944 * as it creates a cycle that can trigger an infinite loop in 945 * device_probe_child() if a device exists for which there is no 946 * suitable driver. 947 */ 948 if (parentname && dc && !dc->parent && 949 strcmp(classname, parentname) != 0) { 950 dc->parent = devclass_find_internal(parentname, NULL, TRUE); 951 dc->parent->flags |= DC_HAS_CHILDREN; 952 } 953 954 return (dc); 955} 956 957/** 958 * @brief Create a device class 959 * 960 * If a device class with the name @p classname exists, return it, 961 * otherwise create and return a new device class. 962 * 963 * @param classname the devclass name to find or create 964 */ 965devclass_t 966devclass_create(const char *classname) 967{ 968 return (devclass_find_internal(classname, NULL, TRUE)); 969} 970 971/** 972 * @brief Find a device class 973 * 974 * If a device class with the name @p classname exists, return it, 975 * otherwise return @c NULL. 976 * 977 * @param classname the devclass name to find 978 */ 979devclass_t 980devclass_find(const char *classname) 981{ 982 return (devclass_find_internal(classname, NULL, FALSE)); 983} 984 985/** 986 * @brief Register that a device driver has been added to a devclass 987 * 988 * Register that a device driver has been added to a devclass. This 989 * is called by devclass_add_driver to accomplish the recursive 990 * notification of all the children classes of dc, as well as dc. 991 * Each layer will have BUS_DRIVER_ADDED() called for all instances of 992 * the devclass. 993 * 994 * We do a full search here of the devclass list at each iteration 995 * level to save storing children-lists in the devclass structure. If 996 * we ever move beyond a few dozen devices doing this, we may need to 997 * reevaluate... 998 * 999 * @param dc the devclass to edit 1000 * @param driver the driver that was just added 1001 */ 1002static void 1003devclass_driver_added(devclass_t dc, driver_t *driver) 1004{ 1005 devclass_t parent; 1006 int i; 1007 1008 /* 1009 * Call BUS_DRIVER_ADDED for any existing busses in this class. 1010 */ 1011 for (i = 0; i < dc->maxunit; i++) 1012 if (dc->devices[i] && device_is_attached(dc->devices[i])) 1013 BUS_DRIVER_ADDED(dc->devices[i], driver); 1014 1015 /* 1016 * Walk through the children classes. Since we only keep a 1017 * single parent pointer around, we walk the entire list of 1018 * devclasses looking for children. We set the 1019 * DC_HAS_CHILDREN flag when a child devclass is created on 1020 * the parent, so we only walk the list for those devclasses 1021 * that have children. 1022 */ 1023 if (!(dc->flags & DC_HAS_CHILDREN)) 1024 return; 1025 parent = dc; 1026 TAILQ_FOREACH(dc, &devclasses, link) { 1027 if (dc->parent == parent) 1028 devclass_driver_added(dc, driver); 1029 } 1030} 1031 1032/** 1033 * @brief Add a device driver to a device class 1034 * 1035 * Add a device driver to a devclass. This is normally called 1036 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of 1037 * all devices in the devclass will be called to allow them to attempt 1038 * to re-probe any unmatched children. 1039 * 1040 * @param dc the devclass to edit 1041 * @param driver the driver to register 1042 */ 1043int 1044devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp) 1045{ 1046 driverlink_t dl; 1047 const char *parentname; 1048 1049 PDEBUG(("%s", DRIVERNAME(driver))); 1050 1051 /* Don't allow invalid pass values. */ 1052 if (pass <= BUS_PASS_ROOT) 1053 return (EINVAL); 1054 1055 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO); 1056 if (!dl) 1057 return (ENOMEM); 1058 1059 /* 1060 * Compile the driver's methods. Also increase the reference count 1061 * so that the class doesn't get freed when the last instance 1062 * goes. This means we can safely use static methods and avoids a 1063 * double-free in devclass_delete_driver. 1064 */ 1065 kobj_class_compile((kobj_class_t) driver); 1066 1067 /* 1068 * If the driver has any base classes, make the 1069 * devclass inherit from the devclass of the driver's 1070 * first base class. This will allow the system to 1071 * search for drivers in both devclasses for children 1072 * of a device using this driver. 1073 */ 1074 if (driver->baseclasses) 1075 parentname = driver->baseclasses[0]->name; 1076 else 1077 parentname = NULL; 1078 *dcp = devclass_find_internal(driver->name, parentname, TRUE); 1079 1080 dl->driver = driver; 1081 TAILQ_INSERT_TAIL(&dc->drivers, dl, link); 1082 driver->refs++; /* XXX: kobj_mtx */ 1083 dl->pass = pass; 1084 driver_register_pass(dl); 1085 1086 devclass_driver_added(dc, driver); 1087 bus_data_generation_update(); 1088 return (0); 1089} 1090 1091/** 1092 * @brief Register that a device driver has been deleted from a devclass 1093 * 1094 * Register that a device driver has been removed from a devclass. 1095 * This is called by devclass_delete_driver to accomplish the 1096 * recursive notification of all the children classes of busclass, as 1097 * well as busclass. Each layer will attempt to detach the driver 1098 * from any devices that are children of the bus's devclass. The function 1099 * will return an error if a device fails to detach. 1100 * 1101 * We do a full search here of the devclass list at each iteration 1102 * level to save storing children-lists in the devclass structure. If 1103 * we ever move beyond a few dozen devices doing this, we may need to 1104 * reevaluate... 1105 * 1106 * @param busclass the devclass of the parent bus 1107 * @param dc the devclass of the driver being deleted 1108 * @param driver the driver being deleted 1109 */ 1110static int 1111devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver) 1112{ 1113 devclass_t parent; 1114 device_t dev; 1115 int error, i; 1116 1117 /* 1118 * Disassociate from any devices. We iterate through all the 1119 * devices in the devclass of the driver and detach any which are 1120 * using the driver and which have a parent in the devclass which 1121 * we are deleting from. 1122 * 1123 * Note that since a driver can be in multiple devclasses, we 1124 * should not detach devices which are not children of devices in 1125 * the affected devclass. 1126 */ 1127 for (i = 0; i < dc->maxunit; i++) { 1128 if (dc->devices[i]) { 1129 dev = dc->devices[i]; 1130 if (dev->driver == driver && dev->parent && 1131 dev->parent->devclass == busclass) { 1132 if ((error = device_detach(dev)) != 0) 1133 return (error); 1134 BUS_PROBE_NOMATCH(dev->parent, dev); 1135 devnomatch(dev); 1136 dev->flags |= DF_DONENOMATCH; 1137 } 1138 } 1139 } 1140 1141 /* 1142 * Walk through the children classes. Since we only keep a 1143 * single parent pointer around, we walk the entire list of 1144 * devclasses looking for children. We set the 1145 * DC_HAS_CHILDREN flag when a child devclass is created on 1146 * the parent, so we only walk the list for those devclasses 1147 * that have children. 1148 */ 1149 if (!(busclass->flags & DC_HAS_CHILDREN)) 1150 return (0); 1151 parent = busclass; 1152 TAILQ_FOREACH(busclass, &devclasses, link) { 1153 if (busclass->parent == parent) { 1154 error = devclass_driver_deleted(busclass, dc, driver); 1155 if (error) 1156 return (error); 1157 } 1158 } 1159 return (0); 1160} 1161 1162/** 1163 * @brief Delete a device driver from a device class 1164 * 1165 * Delete a device driver from a devclass. This is normally called 1166 * automatically by DRIVER_MODULE(). 1167 * 1168 * If the driver is currently attached to any devices, 1169 * devclass_delete_driver() will first attempt to detach from each 1170 * device. If one of the detach calls fails, the driver will not be 1171 * deleted. 1172 * 1173 * @param dc the devclass to edit 1174 * @param driver the driver to unregister 1175 */ 1176int 1177devclass_delete_driver(devclass_t busclass, driver_t *driver) 1178{ 1179 devclass_t dc = devclass_find(driver->name); 1180 driverlink_t dl; 1181 int error; 1182 1183 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 1184 1185 if (!dc) 1186 return (0); 1187 1188 /* 1189 * Find the link structure in the bus' list of drivers. 1190 */ 1191 TAILQ_FOREACH(dl, &busclass->drivers, link) { 1192 if (dl->driver == driver) 1193 break; 1194 } 1195 1196 if (!dl) { 1197 PDEBUG(("%s not found in %s list", driver->name, 1198 busclass->name)); 1199 return (ENOENT); 1200 } 1201 1202 error = devclass_driver_deleted(busclass, dc, driver); 1203 if (error != 0) 1204 return (error); 1205 1206 TAILQ_REMOVE(&busclass->drivers, dl, link); 1207 free(dl, M_BUS); 1208 1209 /* XXX: kobj_mtx */ 1210 driver->refs--; 1211 if (driver->refs == 0) 1212 kobj_class_free((kobj_class_t) driver); 1213 1214 bus_data_generation_update(); 1215 return (0); 1216} 1217 1218/** 1219 * @brief Quiesces a set of device drivers from a device class 1220 * 1221 * Quiesce a device driver from a devclass. This is normally called 1222 * automatically by DRIVER_MODULE(). 1223 * 1224 * If the driver is currently attached to any devices, 1225 * devclass_quiesece_driver() will first attempt to quiesce each 1226 * device. 1227 * 1228 * @param dc the devclass to edit 1229 * @param driver the driver to unregister 1230 */ 1231static int 1232devclass_quiesce_driver(devclass_t busclass, driver_t *driver) 1233{ 1234 devclass_t dc = devclass_find(driver->name); 1235 driverlink_t dl; 1236 device_t dev; 1237 int i; 1238 int error; 1239 1240 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass))); 1241 1242 if (!dc) 1243 return (0); 1244 1245 /* 1246 * Find the link structure in the bus' list of drivers. 1247 */ 1248 TAILQ_FOREACH(dl, &busclass->drivers, link) { 1249 if (dl->driver == driver) 1250 break; 1251 } 1252 1253 if (!dl) { 1254 PDEBUG(("%s not found in %s list", driver->name, 1255 busclass->name)); 1256 return (ENOENT); 1257 } 1258 1259 /* 1260 * Quiesce all devices. We iterate through all the devices in 1261 * the devclass of the driver and quiesce any which are using 1262 * the driver and which have a parent in the devclass which we 1263 * are quiescing. 1264 * 1265 * Note that since a driver can be in multiple devclasses, we 1266 * should not quiesce devices which are not children of 1267 * devices in the affected devclass. 1268 */ 1269 for (i = 0; i < dc->maxunit; i++) { 1270 if (dc->devices[i]) { 1271 dev = dc->devices[i]; 1272 if (dev->driver == driver && dev->parent && 1273 dev->parent->devclass == busclass) { 1274 if ((error = device_quiesce(dev)) != 0) 1275 return (error); 1276 } 1277 } 1278 } 1279 1280 return (0); 1281} 1282 1283/** 1284 * @internal 1285 */ 1286static driverlink_t 1287devclass_find_driver_internal(devclass_t dc, const char *classname) 1288{ 1289 driverlink_t dl; 1290 1291 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc))); 1292 1293 TAILQ_FOREACH(dl, &dc->drivers, link) { 1294 if (!strcmp(dl->driver->name, classname)) 1295 return (dl); 1296 } 1297 1298 PDEBUG(("not found")); 1299 return (NULL); 1300} 1301 1302/** 1303 * @brief Return the name of the devclass 1304 */ 1305const char * 1306devclass_get_name(devclass_t dc) 1307{ 1308 return (dc->name); 1309} 1310 1311/** 1312 * @brief Find a device given a unit number 1313 * 1314 * @param dc the devclass to search 1315 * @param unit the unit number to search for 1316 * 1317 * @returns the device with the given unit number or @c 1318 * NULL if there is no such device 1319 */ 1320device_t 1321devclass_get_device(devclass_t dc, int unit) 1322{ 1323 if (dc == NULL || unit < 0 || unit >= dc->maxunit) 1324 return (NULL); 1325 return (dc->devices[unit]); 1326} 1327 1328/** 1329 * @brief Find the softc field of a device given a unit number 1330 * 1331 * @param dc the devclass to search 1332 * @param unit the unit number to search for 1333 * 1334 * @returns the softc field of the device with the given 1335 * unit number or @c NULL if there is no such 1336 * device 1337 */ 1338void * 1339devclass_get_softc(devclass_t dc, int unit) 1340{ 1341 device_t dev; 1342 1343 dev = devclass_get_device(dc, unit); 1344 if (!dev) 1345 return (NULL); 1346 1347 return (device_get_softc(dev)); 1348} 1349 1350/** 1351 * @brief Get a list of devices in the devclass 1352 * 1353 * An array containing a list of all the devices in the given devclass 1354 * is allocated and returned in @p *devlistp. The number of devices 1355 * in the array is returned in @p *devcountp. The caller should free 1356 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0. 1357 * 1358 * @param dc the devclass to examine 1359 * @param devlistp points at location for array pointer return 1360 * value 1361 * @param devcountp points at location for array size return value 1362 * 1363 * @retval 0 success 1364 * @retval ENOMEM the array allocation failed 1365 */ 1366int 1367devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp) 1368{ 1369 int count, i; 1370 device_t *list; 1371 1372 count = devclass_get_count(dc); 1373 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 1374 if (!list) 1375 return (ENOMEM); 1376 1377 count = 0; 1378 for (i = 0; i < dc->maxunit; i++) { 1379 if (dc->devices[i]) { 1380 list[count] = dc->devices[i]; 1381 count++; 1382 } 1383 } 1384 1385 *devlistp = list; 1386 *devcountp = count; 1387 1388 return (0); 1389} 1390 1391/** 1392 * @brief Get a list of drivers in the devclass 1393 * 1394 * An array containing a list of pointers to all the drivers in the 1395 * given devclass is allocated and returned in @p *listp. The number 1396 * of drivers in the array is returned in @p *countp. The caller should 1397 * free the array using @c free(p, M_TEMP). 1398 * 1399 * @param dc the devclass to examine 1400 * @param listp gives location for array pointer return value 1401 * @param countp gives location for number of array elements 1402 * return value 1403 * 1404 * @retval 0 success 1405 * @retval ENOMEM the array allocation failed 1406 */ 1407int 1408devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp) 1409{ 1410 driverlink_t dl; 1411 driver_t **list; 1412 int count; 1413 1414 count = 0; 1415 TAILQ_FOREACH(dl, &dc->drivers, link) 1416 count++; 1417 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT); 1418 if (list == NULL) 1419 return (ENOMEM); 1420 1421 count = 0; 1422 TAILQ_FOREACH(dl, &dc->drivers, link) { 1423 list[count] = dl->driver; 1424 count++; 1425 } 1426 *listp = list; 1427 *countp = count; 1428 1429 return (0); 1430} 1431 1432/** 1433 * @brief Get the number of devices in a devclass 1434 * 1435 * @param dc the devclass to examine 1436 */ 1437int 1438devclass_get_count(devclass_t dc) 1439{ 1440 int count, i; 1441 1442 count = 0; 1443 for (i = 0; i < dc->maxunit; i++) 1444 if (dc->devices[i]) 1445 count++; 1446 return (count); 1447} 1448 1449/** 1450 * @brief Get the maximum unit number used in a devclass 1451 * 1452 * Note that this is one greater than the highest currently-allocated 1453 * unit. If a null devclass_t is passed in, -1 is returned to indicate 1454 * that not even the devclass has been allocated yet. 1455 * 1456 * @param dc the devclass to examine 1457 */ 1458int 1459devclass_get_maxunit(devclass_t dc) 1460{ 1461 if (dc == NULL) 1462 return (-1); 1463 return (dc->maxunit); 1464} 1465 1466/** 1467 * @brief Find a free unit number in a devclass 1468 * 1469 * This function searches for the first unused unit number greater 1470 * that or equal to @p unit. 1471 * 1472 * @param dc the devclass to examine 1473 * @param unit the first unit number to check 1474 */ 1475int 1476devclass_find_free_unit(devclass_t dc, int unit) 1477{ 1478 if (dc == NULL) 1479 return (unit); 1480 while (unit < dc->maxunit && dc->devices[unit] != NULL) 1481 unit++; 1482 return (unit); 1483} 1484 1485/** 1486 * @brief Set the parent of a devclass 1487 * 1488 * The parent class is normally initialised automatically by 1489 * DRIVER_MODULE(). 1490 * 1491 * @param dc the devclass to edit 1492 * @param pdc the new parent devclass 1493 */ 1494void 1495devclass_set_parent(devclass_t dc, devclass_t pdc) 1496{ 1497 dc->parent = pdc; 1498} 1499 1500/** 1501 * @brief Get the parent of a devclass 1502 * 1503 * @param dc the devclass to examine 1504 */ 1505devclass_t 1506devclass_get_parent(devclass_t dc) 1507{ 1508 return (dc->parent); 1509} 1510 1511struct sysctl_ctx_list * 1512devclass_get_sysctl_ctx(devclass_t dc) 1513{ 1514 return (&dc->sysctl_ctx); 1515} 1516 1517struct sysctl_oid * 1518devclass_get_sysctl_tree(devclass_t dc) 1519{ 1520 return (dc->sysctl_tree); 1521} 1522 1523/** 1524 * @internal 1525 * @brief Allocate a unit number 1526 * 1527 * On entry, @p *unitp is the desired unit number (or @c -1 if any 1528 * will do). The allocated unit number is returned in @p *unitp. 1529 1530 * @param dc the devclass to allocate from 1531 * @param unitp points at the location for the allocated unit 1532 * number 1533 * 1534 * @retval 0 success 1535 * @retval EEXIST the requested unit number is already allocated 1536 * @retval ENOMEM memory allocation failure 1537 */ 1538static int 1539devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp) 1540{ 1541 const char *s; 1542 int unit = *unitp; 1543 1544 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc))); 1545 1546 /* Ask the parent bus if it wants to wire this device. */ 1547 if (unit == -1) 1548 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name, 1549 &unit); 1550 1551 /* If we were given a wired unit number, check for existing device */ 1552 /* XXX imp XXX */ 1553 if (unit != -1) { 1554 if (unit >= 0 && unit < dc->maxunit && 1555 dc->devices[unit] != NULL) { 1556 if (bootverbose) 1557 printf("%s: %s%d already exists; skipping it\n", 1558 dc->name, dc->name, *unitp); 1559 return (EEXIST); 1560 } 1561 } else { 1562 /* Unwired device, find the next available slot for it */ 1563 unit = 0; 1564 for (unit = 0;; unit++) { 1565 /* If there is an "at" hint for a unit then skip it. */ 1566 if (resource_string_value(dc->name, unit, "at", &s) == 1567 0) 1568 continue; 1569 1570 /* If this device slot is already in use, skip it. */ 1571 if (unit < dc->maxunit && dc->devices[unit] != NULL) 1572 continue; 1573 1574 break; 1575 } 1576 } 1577 1578 /* 1579 * We've selected a unit beyond the length of the table, so let's 1580 * extend the table to make room for all units up to and including 1581 * this one. 1582 */ 1583 if (unit >= dc->maxunit) { 1584 device_t *newlist, *oldlist; 1585 int newsize; 1586 1587 oldlist = dc->devices; 1588 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t)); 1589 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT); 1590 if (!newlist) 1591 return (ENOMEM); 1592 if (oldlist != NULL) 1593 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit); 1594 bzero(newlist + dc->maxunit, 1595 sizeof(device_t) * (newsize - dc->maxunit)); 1596 dc->devices = newlist; 1597 dc->maxunit = newsize; 1598 if (oldlist != NULL) 1599 free(oldlist, M_BUS); 1600 } 1601 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc))); 1602 1603 *unitp = unit; 1604 return (0); 1605} 1606 1607/** 1608 * @internal 1609 * @brief Add a device to a devclass 1610 * 1611 * A unit number is allocated for the device (using the device's 1612 * preferred unit number if any) and the device is registered in the 1613 * devclass. This allows the device to be looked up by its unit 1614 * number, e.g. by decoding a dev_t minor number. 1615 * 1616 * @param dc the devclass to add to 1617 * @param dev the device to add 1618 * 1619 * @retval 0 success 1620 * @retval EEXIST the requested unit number is already allocated 1621 * @retval ENOMEM memory allocation failure 1622 */ 1623static int 1624devclass_add_device(devclass_t dc, device_t dev) 1625{ 1626 int buflen, error; 1627 1628 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1629 1630 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX); 1631 if (buflen < 0) 1632 return (ENOMEM); 1633 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO); 1634 if (!dev->nameunit) 1635 return (ENOMEM); 1636 1637 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) { 1638 free(dev->nameunit, M_BUS); 1639 dev->nameunit = NULL; 1640 return (error); 1641 } 1642 dc->devices[dev->unit] = dev; 1643 dev->devclass = dc; 1644 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit); 1645 1646 return (0); 1647} 1648 1649/** 1650 * @internal 1651 * @brief Delete a device from a devclass 1652 * 1653 * The device is removed from the devclass's device list and its unit 1654 * number is freed. 1655 1656 * @param dc the devclass to delete from 1657 * @param dev the device to delete 1658 * 1659 * @retval 0 success 1660 */ 1661static int 1662devclass_delete_device(devclass_t dc, device_t dev) 1663{ 1664 if (!dc || !dev) 1665 return (0); 1666 1667 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc))); 1668 1669 if (dev->devclass != dc || dc->devices[dev->unit] != dev) 1670 panic("devclass_delete_device: inconsistent device class"); 1671 dc->devices[dev->unit] = NULL; 1672 if (dev->flags & DF_WILDCARD) 1673 dev->unit = -1; 1674 dev->devclass = NULL; 1675 free(dev->nameunit, M_BUS); 1676 dev->nameunit = NULL; 1677 1678 return (0); 1679} 1680 1681/** 1682 * @internal 1683 * @brief Make a new device and add it as a child of @p parent 1684 * 1685 * @param parent the parent of the new device 1686 * @param name the devclass name of the new device or @c NULL 1687 * to leave the devclass unspecified 1688 * @parem unit the unit number of the new device of @c -1 to 1689 * leave the unit number unspecified 1690 * 1691 * @returns the new device 1692 */ 1693static device_t 1694make_device(device_t parent, const char *name, int unit) 1695{ 1696 device_t dev; 1697 devclass_t dc; 1698 1699 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit)); 1700 1701 if (name) { 1702 dc = devclass_find_internal(name, NULL, TRUE); 1703 if (!dc) { 1704 printf("make_device: can't find device class %s\n", 1705 name); 1706 return (NULL); 1707 } 1708 } else { 1709 dc = NULL; 1710 } 1711 1712 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO); 1713 if (!dev) 1714 return (NULL); 1715 1716 dev->parent = parent; 1717 TAILQ_INIT(&dev->children); 1718 kobj_init((kobj_t) dev, &null_class); 1719 dev->driver = NULL; 1720 dev->devclass = NULL; 1721 dev->unit = unit; 1722 dev->nameunit = NULL; 1723 dev->desc = NULL; 1724 dev->busy = 0; 1725 dev->devflags = 0; 1726 dev->flags = DF_ENABLED; 1727 dev->order = 0; 1728 if (unit == -1) 1729 dev->flags |= DF_WILDCARD; 1730 if (name) { 1731 dev->flags |= DF_FIXEDCLASS; 1732 if (devclass_add_device(dc, dev)) { 1733 kobj_delete((kobj_t) dev, M_BUS); 1734 return (NULL); 1735 } 1736 } 1737 dev->ivars = NULL; 1738 dev->softc = NULL; 1739 1740 dev->state = DS_NOTPRESENT; 1741 1742 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink); 1743 bus_data_generation_update(); 1744 1745 return (dev); 1746} 1747 1748/** 1749 * @internal 1750 * @brief Print a description of a device. 1751 */ 1752static int 1753device_print_child(device_t dev, device_t child) 1754{ 1755 int retval = 0; 1756 1757 if (device_is_alive(child)) 1758 retval += BUS_PRINT_CHILD(dev, child); 1759 else 1760 retval += device_printf(child, " not found\n"); 1761 1762 return (retval); 1763} 1764 1765/** 1766 * @brief Create a new device 1767 * 1768 * This creates a new device and adds it as a child of an existing 1769 * parent device. The new device will be added after the last existing 1770 * child with order zero. 1771 * 1772 * @param dev the device which will be the parent of the 1773 * new child device 1774 * @param name devclass name for new device or @c NULL if not 1775 * specified 1776 * @param unit unit number for new device or @c -1 if not 1777 * specified 1778 * 1779 * @returns the new device 1780 */ 1781device_t 1782device_add_child(device_t dev, const char *name, int unit) 1783{ 1784 return (device_add_child_ordered(dev, 0, name, unit)); 1785} 1786 1787/** 1788 * @brief Create a new device 1789 * 1790 * This creates a new device and adds it as a child of an existing 1791 * parent device. The new device will be added after the last existing 1792 * child with the same order. 1793 * 1794 * @param dev the device which will be the parent of the 1795 * new child device 1796 * @param order a value which is used to partially sort the 1797 * children of @p dev - devices created using 1798 * lower values of @p order appear first in @p 1799 * dev's list of children 1800 * @param name devclass name for new device or @c NULL if not 1801 * specified 1802 * @param unit unit number for new device or @c -1 if not 1803 * specified 1804 * 1805 * @returns the new device 1806 */ 1807device_t 1808device_add_child_ordered(device_t dev, u_int order, const char *name, int unit) 1809{ 1810 device_t child; 1811 device_t place; 1812 1813 PDEBUG(("%s at %s with order %u as unit %d", 1814 name, DEVICENAME(dev), order, unit)); 1815 KASSERT(name != NULL || unit == -1, 1816 ("child device with wildcard name and specific unit number")); 1817 1818 child = make_device(dev, name, unit); 1819 if (child == NULL) 1820 return (child); 1821 child->order = order; 1822 1823 TAILQ_FOREACH(place, &dev->children, link) { 1824 if (place->order > order) 1825 break; 1826 } 1827 1828 if (place) { 1829 /* 1830 * The device 'place' is the first device whose order is 1831 * greater than the new child. 1832 */ 1833 TAILQ_INSERT_BEFORE(place, child, link); 1834 } else { 1835 /* 1836 * The new child's order is greater or equal to the order of 1837 * any existing device. Add the child to the tail of the list. 1838 */ 1839 TAILQ_INSERT_TAIL(&dev->children, child, link); 1840 } 1841 1842 bus_data_generation_update(); 1843 return (child); 1844} 1845 1846/** 1847 * @brief Delete a device 1848 * 1849 * This function deletes a device along with all of its children. If 1850 * the device currently has a driver attached to it, the device is 1851 * detached first using device_detach(). 1852 * 1853 * @param dev the parent device 1854 * @param child the device to delete 1855 * 1856 * @retval 0 success 1857 * @retval non-zero a unit error code describing the error 1858 */ 1859int 1860device_delete_child(device_t dev, device_t child) 1861{ 1862 int error; 1863 device_t grandchild; 1864 1865 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); 1866 1867 /* remove children first */ 1868 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) { 1869 error = device_delete_child(child, grandchild); 1870 if (error) 1871 return (error); 1872 } 1873 1874 if ((error = device_detach(child)) != 0) 1875 return (error); 1876 if (child->devclass) 1877 devclass_delete_device(child->devclass, child); 1878 if (child->parent) 1879 BUS_CHILD_DELETED(dev, child); 1880 TAILQ_REMOVE(&dev->children, child, link); 1881 TAILQ_REMOVE(&bus_data_devices, child, devlink); 1882 kobj_delete((kobj_t) child, M_BUS); 1883 1884 bus_data_generation_update(); 1885 return (0); 1886} 1887 1888/** 1889 * @brief Delete all children devices of the given device, if any. 1890 * 1891 * This function deletes all children devices of the given device, if 1892 * any, using the device_delete_child() function for each device it 1893 * finds. If a child device cannot be deleted, this function will 1894 * return an error code. 1895 * 1896 * @param dev the parent device 1897 * 1898 * @retval 0 success 1899 * @retval non-zero a device would not detach 1900 */ 1901int 1902device_delete_children(device_t dev) 1903{ 1904 device_t child; 1905 int error; 1906 1907 PDEBUG(("Deleting all children of %s", DEVICENAME(dev))); 1908 1909 error = 0; 1910 1911 while ((child = TAILQ_FIRST(&dev->children)) != NULL) { 1912 error = device_delete_child(dev, child); 1913 if (error) { 1914 PDEBUG(("Failed deleting %s", DEVICENAME(child))); 1915 break; 1916 } 1917 } 1918 return (error); 1919} 1920 1921/** 1922 * @brief Find a device given a unit number 1923 * 1924 * This is similar to devclass_get_devices() but only searches for 1925 * devices which have @p dev as a parent. 1926 * 1927 * @param dev the parent device to search 1928 * @param unit the unit number to search for. If the unit is -1, 1929 * return the first child of @p dev which has name 1930 * @p classname (that is, the one with the lowest unit.) 1931 * 1932 * @returns the device with the given unit number or @c 1933 * NULL if there is no such device 1934 */ 1935device_t 1936device_find_child(device_t dev, const char *classname, int unit) 1937{ 1938 devclass_t dc; 1939 device_t child; 1940 1941 dc = devclass_find(classname); 1942 if (!dc) 1943 return (NULL); 1944 1945 if (unit != -1) { 1946 child = devclass_get_device(dc, unit); 1947 if (child && child->parent == dev) 1948 return (child); 1949 } else { 1950 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { 1951 child = devclass_get_device(dc, unit); 1952 if (child && child->parent == dev) 1953 return (child); 1954 } 1955 } 1956 return (NULL); 1957} 1958 1959/** 1960 * @internal 1961 */ 1962static driverlink_t 1963first_matching_driver(devclass_t dc, device_t dev) 1964{ 1965 if (dev->devclass) 1966 return (devclass_find_driver_internal(dc, dev->devclass->name)); 1967 return (TAILQ_FIRST(&dc->drivers)); 1968} 1969 1970/** 1971 * @internal 1972 */ 1973static driverlink_t 1974next_matching_driver(devclass_t dc, device_t dev, driverlink_t last) 1975{ 1976 if (dev->devclass) { 1977 driverlink_t dl; 1978 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) 1979 if (!strcmp(dev->devclass->name, dl->driver->name)) 1980 return (dl); 1981 return (NULL); 1982 } 1983 return (TAILQ_NEXT(last, link)); 1984} 1985 1986/** 1987 * @internal 1988 */ 1989int 1990device_probe_child(device_t dev, device_t child) 1991{ 1992 devclass_t dc; 1993 driverlink_t best = NULL; 1994 driverlink_t dl; 1995 int result, pri = 0; 1996 int hasclass = (child->devclass != NULL); 1997 1998 GIANT_REQUIRED; 1999 2000 dc = dev->devclass; 2001 if (!dc) 2002 panic("device_probe_child: parent device has no devclass"); 2003 2004 /* 2005 * If the state is already probed, then return. However, don't 2006 * return if we can rebid this object. 2007 */ 2008 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0) 2009 return (0); 2010 2011 for (; dc; dc = dc->parent) { 2012 for (dl = first_matching_driver(dc, child); 2013 dl; 2014 dl = next_matching_driver(dc, child, dl)) { 2015 /* If this driver's pass is too high, then ignore it. */ 2016 if (dl->pass > bus_current_pass) 2017 continue; 2018 2019 PDEBUG(("Trying %s", DRIVERNAME(dl->driver))); 2020 result = device_set_driver(child, dl->driver); 2021 if (result == ENOMEM) 2022 return (result); 2023 else if (result != 0) 2024 continue; 2025 if (!hasclass) { 2026 if (device_set_devclass(child, 2027 dl->driver->name) != 0) { 2028 char const * devname = 2029 device_get_name(child); 2030 if (devname == NULL) 2031 devname = "(unknown)"; 2032 printf("driver bug: Unable to set " 2033 "devclass (class: %s " 2034 "devname: %s)\n", 2035 dl->driver->name, 2036 devname); 2037 (void)device_set_driver(child, NULL); 2038 continue; 2039 } 2040 } 2041 2042 /* Fetch any flags for the device before probing. */ 2043 resource_int_value(dl->driver->name, child->unit, 2044 "flags", &child->devflags); 2045 2046 result = DEVICE_PROBE(child); 2047 2048 /* Reset flags and devclass before the next probe. */ 2049 child->devflags = 0; 2050 if (!hasclass) 2051 (void)device_set_devclass(child, NULL); 2052 2053 /* 2054 * If the driver returns SUCCESS, there can be 2055 * no higher match for this device. 2056 */ 2057 if (result == 0) { 2058 best = dl; 2059 pri = 0; 2060 break; 2061 } 2062 2063 /* 2064 * The driver returned an error so it 2065 * certainly doesn't match. 2066 */ 2067 if (result > 0) { 2068 (void)device_set_driver(child, NULL); 2069 continue; 2070 } 2071 2072 /* 2073 * A priority lower than SUCCESS, remember the 2074 * best matching driver. Initialise the value 2075 * of pri for the first match. 2076 */ 2077 if (best == NULL || result > pri) { 2078 /* 2079 * Probes that return BUS_PROBE_NOWILDCARD 2080 * or lower only match when they are set 2081 * in stone by the parent bus. 2082 */ 2083 if (result <= BUS_PROBE_NOWILDCARD && 2084 child->flags & DF_WILDCARD) 2085 continue; 2086 best = dl; 2087 pri = result; 2088 continue; 2089 } 2090 } 2091 /* 2092 * If we have an unambiguous match in this devclass, 2093 * don't look in the parent. 2094 */ 2095 if (best && pri == 0) 2096 break; 2097 } 2098 2099 /* 2100 * If we found a driver, change state and initialise the devclass. 2101 */ 2102 /* XXX What happens if we rebid and got no best? */ 2103 if (best) { 2104 /* 2105 * If this device was attached, and we were asked to 2106 * rescan, and it is a different driver, then we have 2107 * to detach the old driver and reattach this new one. 2108 * Note, we don't have to check for DF_REBID here 2109 * because if the state is > DS_ALIVE, we know it must 2110 * be. 2111 * 2112 * This assumes that all DF_REBID drivers can have 2113 * their probe routine called at any time and that 2114 * they are idempotent as well as completely benign in 2115 * normal operations. 2116 * 2117 * We also have to make sure that the detach 2118 * succeeded, otherwise we fail the operation (or 2119 * maybe it should just fail silently? I'm torn). 2120 */ 2121 if (child->state > DS_ALIVE && best->driver != child->driver) 2122 if ((result = device_detach(dev)) != 0) 2123 return (result); 2124 2125 /* Set the winning driver, devclass, and flags. */ 2126 if (!child->devclass) { 2127 result = device_set_devclass(child, best->driver->name); 2128 if (result != 0) 2129 return (result); 2130 } 2131 result = device_set_driver(child, best->driver); 2132 if (result != 0) 2133 return (result); 2134 resource_int_value(best->driver->name, child->unit, 2135 "flags", &child->devflags); 2136 2137 if (pri < 0) { 2138 /* 2139 * A bit bogus. Call the probe method again to make 2140 * sure that we have the right description. 2141 */ 2142 DEVICE_PROBE(child); 2143#if 0 2144 child->flags |= DF_REBID; 2145#endif 2146 } else 2147 child->flags &= ~DF_REBID; 2148 child->state = DS_ALIVE; 2149 2150 bus_data_generation_update(); 2151 return (0); 2152 } 2153 2154 return (ENXIO); 2155} 2156 2157/** 2158 * @brief Return the parent of a device 2159 */ 2160device_t 2161device_get_parent(device_t dev) 2162{ 2163 return (dev->parent); 2164} 2165 2166/** 2167 * @brief Get a list of children of a device 2168 * 2169 * An array containing a list of all the children of the given device 2170 * is allocated and returned in @p *devlistp. The number of devices 2171 * in the array is returned in @p *devcountp. The caller should free 2172 * the array using @c free(p, M_TEMP). 2173 * 2174 * @param dev the device to examine 2175 * @param devlistp points at location for array pointer return 2176 * value 2177 * @param devcountp points at location for array size return value 2178 * 2179 * @retval 0 success 2180 * @retval ENOMEM the array allocation failed 2181 */ 2182int 2183device_get_children(device_t dev, device_t **devlistp, int *devcountp) 2184{ 2185 int count; 2186 device_t child; 2187 device_t *list; 2188 2189 count = 0; 2190 TAILQ_FOREACH(child, &dev->children, link) { 2191 count++; 2192 } 2193 if (count == 0) { 2194 *devlistp = NULL; 2195 *devcountp = 0; 2196 return (0); 2197 } 2198 2199 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO); 2200 if (!list) 2201 return (ENOMEM); 2202 2203 count = 0; 2204 TAILQ_FOREACH(child, &dev->children, link) { 2205 list[count] = child; 2206 count++; 2207 } 2208 2209 *devlistp = list; 2210 *devcountp = count; 2211 2212 return (0); 2213} 2214 2215/** 2216 * @brief Return the current driver for the device or @c NULL if there 2217 * is no driver currently attached 2218 */ 2219driver_t * 2220device_get_driver(device_t dev) 2221{ 2222 return (dev->driver); 2223} 2224 2225/** 2226 * @brief Return the current devclass for the device or @c NULL if 2227 * there is none. 2228 */ 2229devclass_t 2230device_get_devclass(device_t dev) 2231{ 2232 return (dev->devclass); 2233} 2234 2235/** 2236 * @brief Return the name of the device's devclass or @c NULL if there 2237 * is none. 2238 */ 2239const char * 2240device_get_name(device_t dev) 2241{ 2242 if (dev != NULL && dev->devclass) 2243 return (devclass_get_name(dev->devclass)); 2244 return (NULL); 2245} 2246 2247/** 2248 * @brief Return a string containing the device's devclass name 2249 * followed by an ascii representation of the device's unit number 2250 * (e.g. @c "foo2"). 2251 */ 2252const char * 2253device_get_nameunit(device_t dev) 2254{ 2255 return (dev->nameunit); 2256} 2257 2258/** 2259 * @brief Return the device's unit number. 2260 */ 2261int 2262device_get_unit(device_t dev) 2263{ 2264 return (dev->unit); 2265} 2266 2267/** 2268 * @brief Return the device's description string 2269 */ 2270const char * 2271device_get_desc(device_t dev) 2272{ 2273 return (dev->desc); 2274} 2275 2276/** 2277 * @brief Return the device's flags 2278 */ 2279uint32_t 2280device_get_flags(device_t dev) 2281{ 2282 return (dev->devflags); 2283} 2284 2285struct sysctl_ctx_list * 2286device_get_sysctl_ctx(device_t dev) 2287{ 2288 return (&dev->sysctl_ctx); 2289} 2290 2291struct sysctl_oid * 2292device_get_sysctl_tree(device_t dev) 2293{ 2294 return (dev->sysctl_tree); 2295} 2296 2297/** 2298 * @brief Print the name of the device followed by a colon and a space 2299 * 2300 * @returns the number of characters printed 2301 */ 2302int 2303device_print_prettyname(device_t dev) 2304{ 2305 const char *name = device_get_name(dev); 2306 2307 if (name == NULL) 2308 return (printf("unknown: ")); 2309 return (printf("%s%d: ", name, device_get_unit(dev))); 2310} 2311 2312/** 2313 * @brief Print the name of the device followed by a colon, a space 2314 * and the result of calling vprintf() with the value of @p fmt and 2315 * the following arguments. 2316 * 2317 * @returns the number of characters printed 2318 */ 2319int 2320device_printf(device_t dev, const char * fmt, ...) 2321{ 2322 va_list ap; 2323 int retval; 2324 2325 retval = device_print_prettyname(dev); 2326 va_start(ap, fmt); 2327 retval += vprintf(fmt, ap); 2328 va_end(ap); 2329 return (retval); 2330} 2331 2332/** 2333 * @internal 2334 */ 2335static void 2336device_set_desc_internal(device_t dev, const char* desc, int copy) 2337{ 2338 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) { 2339 free(dev->desc, M_BUS); 2340 dev->flags &= ~DF_DESCMALLOCED; 2341 dev->desc = NULL; 2342 } 2343 2344 if (copy && desc) { 2345 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT); 2346 if (dev->desc) { 2347 strcpy(dev->desc, desc); 2348 dev->flags |= DF_DESCMALLOCED; 2349 } 2350 } else { 2351 /* Avoid a -Wcast-qual warning */ 2352 dev->desc = (char *)(uintptr_t) desc; 2353 } 2354 2355 bus_data_generation_update(); 2356} 2357 2358/** 2359 * @brief Set the device's description 2360 * 2361 * The value of @c desc should be a string constant that will not 2362 * change (at least until the description is changed in a subsequent 2363 * call to device_set_desc() or device_set_desc_copy()). 2364 */ 2365void 2366device_set_desc(device_t dev, const char* desc) 2367{ 2368 device_set_desc_internal(dev, desc, FALSE); 2369} 2370 2371/** 2372 * @brief Set the device's description 2373 * 2374 * The string pointed to by @c desc is copied. Use this function if 2375 * the device description is generated, (e.g. with sprintf()). 2376 */ 2377void 2378device_set_desc_copy(device_t dev, const char* desc) 2379{ 2380 device_set_desc_internal(dev, desc, TRUE); 2381} 2382 2383/** 2384 * @brief Set the device's flags 2385 */ 2386void 2387device_set_flags(device_t dev, uint32_t flags) 2388{ 2389 dev->devflags = flags; 2390} 2391 2392/** 2393 * @brief Return the device's softc field 2394 * 2395 * The softc is allocated and zeroed when a driver is attached, based 2396 * on the size field of the driver. 2397 */ 2398void * 2399device_get_softc(device_t dev) 2400{ 2401 return (dev->softc); 2402} 2403 2404/** 2405 * @brief Set the device's softc field 2406 * 2407 * Most drivers do not need to use this since the softc is allocated 2408 * automatically when the driver is attached. 2409 */ 2410void 2411device_set_softc(device_t dev, void *softc) 2412{ 2413 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) 2414 free(dev->softc, M_BUS_SC); 2415 dev->softc = softc; 2416 if (dev->softc) 2417 dev->flags |= DF_EXTERNALSOFTC; 2418 else 2419 dev->flags &= ~DF_EXTERNALSOFTC; 2420} 2421 2422/** 2423 * @brief Free claimed softc 2424 * 2425 * Most drivers do not need to use this since the softc is freed 2426 * automatically when the driver is detached. 2427 */ 2428void 2429device_free_softc(void *softc) 2430{ 2431 free(softc, M_BUS_SC); 2432} 2433 2434/** 2435 * @brief Claim softc 2436 * 2437 * This function can be used to let the driver free the automatically 2438 * allocated softc using "device_free_softc()". This function is 2439 * useful when the driver is refcounting the softc and the softc 2440 * cannot be freed when the "device_detach" method is called. 2441 */ 2442void 2443device_claim_softc(device_t dev) 2444{ 2445 if (dev->softc) 2446 dev->flags |= DF_EXTERNALSOFTC; 2447 else 2448 dev->flags &= ~DF_EXTERNALSOFTC; 2449} 2450 2451/** 2452 * @brief Get the device's ivars field 2453 * 2454 * The ivars field is used by the parent device to store per-device 2455 * state (e.g. the physical location of the device or a list of 2456 * resources). 2457 */ 2458void * 2459device_get_ivars(device_t dev) 2460{ 2461 2462 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)")); 2463 return (dev->ivars); 2464} 2465 2466/** 2467 * @brief Set the device's ivars field 2468 */ 2469void 2470device_set_ivars(device_t dev, void * ivars) 2471{ 2472 2473 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)")); 2474 dev->ivars = ivars; 2475} 2476 2477/** 2478 * @brief Return the device's state 2479 */ 2480device_state_t 2481device_get_state(device_t dev) 2482{ 2483 return (dev->state); 2484} 2485 2486/** 2487 * @brief Set the DF_ENABLED flag for the device 2488 */ 2489void 2490device_enable(device_t dev) 2491{ 2492 dev->flags |= DF_ENABLED; 2493} 2494 2495/** 2496 * @brief Clear the DF_ENABLED flag for the device 2497 */ 2498void 2499device_disable(device_t dev) 2500{ 2501 dev->flags &= ~DF_ENABLED; 2502} 2503 2504/** 2505 * @brief Increment the busy counter for the device 2506 */ 2507void 2508device_busy(device_t dev) 2509{ 2510 if (dev->state < DS_ATTACHING) 2511 panic("device_busy: called for unattached device"); 2512 if (dev->busy == 0 && dev->parent) 2513 device_busy(dev->parent); 2514 dev->busy++; 2515 if (dev->state == DS_ATTACHED) 2516 dev->state = DS_BUSY; 2517} 2518 2519/** 2520 * @brief Decrement the busy counter for the device 2521 */ 2522void 2523device_unbusy(device_t dev) 2524{ 2525 if (dev->busy != 0 && dev->state != DS_BUSY && 2526 dev->state != DS_ATTACHING) 2527 panic("device_unbusy: called for non-busy device %s", 2528 device_get_nameunit(dev)); 2529 dev->busy--; 2530 if (dev->busy == 0) { 2531 if (dev->parent) 2532 device_unbusy(dev->parent); 2533 if (dev->state == DS_BUSY) 2534 dev->state = DS_ATTACHED; 2535 } 2536} 2537 2538/** 2539 * @brief Set the DF_QUIET flag for the device 2540 */ 2541void 2542device_quiet(device_t dev) 2543{ 2544 dev->flags |= DF_QUIET; 2545} 2546 2547/** 2548 * @brief Clear the DF_QUIET flag for the device 2549 */ 2550void 2551device_verbose(device_t dev) 2552{ 2553 dev->flags &= ~DF_QUIET; 2554} 2555 2556/** 2557 * @brief Return non-zero if the DF_QUIET flag is set on the device 2558 */ 2559int 2560device_is_quiet(device_t dev) 2561{ 2562 return ((dev->flags & DF_QUIET) != 0); 2563} 2564 2565/** 2566 * @brief Return non-zero if the DF_ENABLED flag is set on the device 2567 */ 2568int 2569device_is_enabled(device_t dev) 2570{ 2571 return ((dev->flags & DF_ENABLED) != 0); 2572} 2573 2574/** 2575 * @brief Return non-zero if the device was successfully probed 2576 */ 2577int 2578device_is_alive(device_t dev) 2579{ 2580 return (dev->state >= DS_ALIVE); 2581} 2582 2583/** 2584 * @brief Return non-zero if the device currently has a driver 2585 * attached to it 2586 */ 2587int 2588device_is_attached(device_t dev) 2589{ 2590 return (dev->state >= DS_ATTACHED); 2591} 2592 2593/** 2594 * @brief Set the devclass of a device 2595 * @see devclass_add_device(). 2596 */ 2597int 2598device_set_devclass(device_t dev, const char *classname) 2599{ 2600 devclass_t dc; 2601 int error; 2602 2603 if (!classname) { 2604 if (dev->devclass) 2605 devclass_delete_device(dev->devclass, dev); 2606 return (0); 2607 } 2608 2609 if (dev->devclass) { 2610 printf("device_set_devclass: device class already set\n"); 2611 return (EINVAL); 2612 } 2613 2614 dc = devclass_find_internal(classname, NULL, TRUE); 2615 if (!dc) 2616 return (ENOMEM); 2617 2618 error = devclass_add_device(dc, dev); 2619 2620 bus_data_generation_update(); 2621 return (error); 2622} 2623 2624/** 2625 * @brief Set the driver of a device 2626 * 2627 * @retval 0 success 2628 * @retval EBUSY the device already has a driver attached 2629 * @retval ENOMEM a memory allocation failure occurred 2630 */ 2631int 2632device_set_driver(device_t dev, driver_t *driver) 2633{ 2634 if (dev->state >= DS_ATTACHED) 2635 return (EBUSY); 2636 2637 if (dev->driver == driver) 2638 return (0); 2639 2640 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) { 2641 free(dev->softc, M_BUS_SC); 2642 dev->softc = NULL; 2643 } 2644 device_set_desc(dev, NULL); 2645 kobj_delete((kobj_t) dev, NULL); 2646 dev->driver = driver; 2647 if (driver) { 2648 kobj_init((kobj_t) dev, (kobj_class_t) driver); 2649 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) { 2650 dev->softc = malloc(driver->size, M_BUS_SC, 2651 M_NOWAIT | M_ZERO); 2652 if (!dev->softc) { 2653 kobj_delete((kobj_t) dev, NULL); 2654 kobj_init((kobj_t) dev, &null_class); 2655 dev->driver = NULL; 2656 return (ENOMEM); 2657 } 2658 } 2659 } else { 2660 kobj_init((kobj_t) dev, &null_class); 2661 } 2662 2663 bus_data_generation_update(); 2664 return (0); 2665} 2666 2667/** 2668 * @brief Probe a device, and return this status. 2669 * 2670 * This function is the core of the device autoconfiguration 2671 * system. Its purpose is to select a suitable driver for a device and 2672 * then call that driver to initialise the hardware appropriately. The 2673 * driver is selected by calling the DEVICE_PROBE() method of a set of 2674 * candidate drivers and then choosing the driver which returned the 2675 * best value. This driver is then attached to the device using 2676 * device_attach(). 2677 * 2678 * The set of suitable drivers is taken from the list of drivers in 2679 * the parent device's devclass. If the device was originally created 2680 * with a specific class name (see device_add_child()), only drivers 2681 * with that name are probed, otherwise all drivers in the devclass 2682 * are probed. If no drivers return successful probe values in the 2683 * parent devclass, the search continues in the parent of that 2684 * devclass (see devclass_get_parent()) if any. 2685 * 2686 * @param dev the device to initialise 2687 * 2688 * @retval 0 success 2689 * @retval ENXIO no driver was found 2690 * @retval ENOMEM memory allocation failure 2691 * @retval non-zero some other unix error code 2692 * @retval -1 Device already attached 2693 */ 2694int 2695device_probe(device_t dev) 2696{ 2697 int error; 2698 2699 GIANT_REQUIRED; 2700 2701 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0) 2702 return (-1); 2703 2704 if (!(dev->flags & DF_ENABLED)) { 2705 if (bootverbose && device_get_name(dev) != NULL) { 2706 device_print_prettyname(dev); 2707 printf("not probed (disabled)\n"); 2708 } 2709 return (-1); 2710 } 2711 if ((error = device_probe_child(dev->parent, dev)) != 0) { 2712 if (bus_current_pass == BUS_PASS_DEFAULT && 2713 !(dev->flags & DF_DONENOMATCH)) { 2714 BUS_PROBE_NOMATCH(dev->parent, dev); 2715 devnomatch(dev); 2716 dev->flags |= DF_DONENOMATCH; 2717 } 2718 return (error); 2719 } 2720 return (0); 2721} 2722 2723/** 2724 * @brief Probe a device and attach a driver if possible 2725 * 2726 * calls device_probe() and attaches if that was successful. 2727 */ 2728int 2729device_probe_and_attach(device_t dev) 2730{ 2731 int error; 2732 2733 GIANT_REQUIRED; 2734 2735 error = device_probe(dev); 2736 if (error == -1) 2737 return (0); 2738 else if (error != 0) 2739 return (error); 2740 2741 CURVNET_SET_QUIET(vnet0); 2742 error = device_attach(dev); 2743 CURVNET_RESTORE(); 2744 return error; 2745} 2746 2747/** 2748 * @brief Attach a device driver to a device 2749 * 2750 * This function is a wrapper around the DEVICE_ATTACH() driver 2751 * method. In addition to calling DEVICE_ATTACH(), it initialises the 2752 * device's sysctl tree, optionally prints a description of the device 2753 * and queues a notification event for user-based device management 2754 * services. 2755 * 2756 * Normally this function is only called internally from 2757 * device_probe_and_attach(). 2758 * 2759 * @param dev the device to initialise 2760 * 2761 * @retval 0 success 2762 * @retval ENXIO no driver was found 2763 * @retval ENOMEM memory allocation failure 2764 * @retval non-zero some other unix error code 2765 */ 2766int 2767device_attach(device_t dev) 2768{ 2769 int error; 2770 2771 if (resource_disabled(dev->driver->name, dev->unit)) { 2772 device_disable(dev); 2773 if (bootverbose) 2774 device_printf(dev, "disabled via hints entry\n"); 2775 return (ENXIO); 2776 } 2777 2778 device_sysctl_init(dev); 2779 if (!device_is_quiet(dev)) 2780 device_print_child(dev->parent, dev); 2781 dev->state = DS_ATTACHING; 2782 if ((error = DEVICE_ATTACH(dev)) != 0) { 2783 printf("device_attach: %s%d attach returned %d\n", 2784 dev->driver->name, dev->unit, error); 2785 if (!(dev->flags & DF_FIXEDCLASS)) 2786 devclass_delete_device(dev->devclass, dev); 2787 (void)device_set_driver(dev, NULL); 2788 device_sysctl_fini(dev); 2789 KASSERT(dev->busy == 0, ("attach failed but busy")); 2790 dev->state = DS_NOTPRESENT; 2791 return (error); 2792 } 2793 device_sysctl_update(dev); 2794 if (dev->busy) 2795 dev->state = DS_BUSY; 2796 else 2797 dev->state = DS_ATTACHED; 2798 dev->flags &= ~DF_DONENOMATCH; 2799 devadded(dev); 2800 return (0); 2801} 2802 2803/** 2804 * @brief Detach a driver from a device 2805 * 2806 * This function is a wrapper around the DEVICE_DETACH() driver 2807 * method. If the call to DEVICE_DETACH() succeeds, it calls 2808 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a 2809 * notification event for user-based device management services and 2810 * cleans up the device's sysctl tree. 2811 * 2812 * @param dev the device to un-initialise 2813 * 2814 * @retval 0 success 2815 * @retval ENXIO no driver was found 2816 * @retval ENOMEM memory allocation failure 2817 * @retval non-zero some other unix error code 2818 */ 2819int 2820device_detach(device_t dev) 2821{ 2822 int error; 2823 2824 GIANT_REQUIRED; 2825 2826 PDEBUG(("%s", DEVICENAME(dev))); 2827 if (dev->state == DS_BUSY) 2828 return (EBUSY); 2829 if (dev->state != DS_ATTACHED) 2830 return (0); 2831 2832 if ((error = DEVICE_DETACH(dev)) != 0) 2833 return (error); 2834 devremoved(dev); 2835 if (!device_is_quiet(dev)) 2836 device_printf(dev, "detached\n"); 2837 if (dev->parent) 2838 BUS_CHILD_DETACHED(dev->parent, dev); 2839 2840 if (!(dev->flags & DF_FIXEDCLASS)) 2841 devclass_delete_device(dev->devclass, dev); 2842 2843 dev->state = DS_NOTPRESENT; 2844 (void)device_set_driver(dev, NULL); 2845 device_sysctl_fini(dev); 2846 2847 return (0); 2848} 2849 2850/** 2851 * @brief Tells a driver to quiesce itself. 2852 * 2853 * This function is a wrapper around the DEVICE_QUIESCE() driver 2854 * method. If the call to DEVICE_QUIESCE() succeeds. 2855 * 2856 * @param dev the device to quiesce 2857 * 2858 * @retval 0 success 2859 * @retval ENXIO no driver was found 2860 * @retval ENOMEM memory allocation failure 2861 * @retval non-zero some other unix error code 2862 */ 2863int 2864device_quiesce(device_t dev) 2865{ 2866 2867 PDEBUG(("%s", DEVICENAME(dev))); 2868 if (dev->state == DS_BUSY) 2869 return (EBUSY); 2870 if (dev->state != DS_ATTACHED) 2871 return (0); 2872 2873 return (DEVICE_QUIESCE(dev)); 2874} 2875 2876/** 2877 * @brief Notify a device of system shutdown 2878 * 2879 * This function calls the DEVICE_SHUTDOWN() driver method if the 2880 * device currently has an attached driver. 2881 * 2882 * @returns the value returned by DEVICE_SHUTDOWN() 2883 */ 2884int 2885device_shutdown(device_t dev) 2886{ 2887 if (dev->state < DS_ATTACHED) 2888 return (0); 2889 return (DEVICE_SHUTDOWN(dev)); 2890} 2891 2892/** 2893 * @brief Set the unit number of a device 2894 * 2895 * This function can be used to override the unit number used for a 2896 * device (e.g. to wire a device to a pre-configured unit number). 2897 */ 2898int 2899device_set_unit(device_t dev, int unit) 2900{ 2901 devclass_t dc; 2902 int err; 2903 2904 dc = device_get_devclass(dev); 2905 if (unit < dc->maxunit && dc->devices[unit]) 2906 return (EBUSY); 2907 err = devclass_delete_device(dc, dev); 2908 if (err) 2909 return (err); 2910 dev->unit = unit; 2911 err = devclass_add_device(dc, dev); 2912 if (err) 2913 return (err); 2914 2915 bus_data_generation_update(); 2916 return (0); 2917} 2918 2919/*======================================*/ 2920/* 2921 * Some useful method implementations to make life easier for bus drivers. 2922 */ 2923 2924/** 2925 * @brief Initialise a resource list. 2926 * 2927 * @param rl the resource list to initialise 2928 */ 2929void 2930resource_list_init(struct resource_list *rl) 2931{ 2932 STAILQ_INIT(rl); 2933} 2934 2935/** 2936 * @brief Reclaim memory used by a resource list. 2937 * 2938 * This function frees the memory for all resource entries on the list 2939 * (if any). 2940 * 2941 * @param rl the resource list to free 2942 */ 2943void 2944resource_list_free(struct resource_list *rl) 2945{ 2946 struct resource_list_entry *rle; 2947 2948 while ((rle = STAILQ_FIRST(rl)) != NULL) { 2949 if (rle->res) 2950 panic("resource_list_free: resource entry is busy"); 2951 STAILQ_REMOVE_HEAD(rl, link); 2952 free(rle, M_BUS); 2953 } 2954} 2955 2956/** 2957 * @brief Add a resource entry. 2958 * 2959 * This function adds a resource entry using the given @p type, @p 2960 * start, @p end and @p count values. A rid value is chosen by 2961 * searching sequentially for the first unused rid starting at zero. 2962 * 2963 * @param rl the resource list to edit 2964 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2965 * @param start the start address of the resource 2966 * @param end the end address of the resource 2967 * @param count XXX end-start+1 2968 */ 2969int 2970resource_list_add_next(struct resource_list *rl, int type, u_long start, 2971 u_long end, u_long count) 2972{ 2973 int rid; 2974 2975 rid = 0; 2976 while (resource_list_find(rl, type, rid) != NULL) 2977 rid++; 2978 resource_list_add(rl, type, rid, start, end, count); 2979 return (rid); 2980} 2981 2982/** 2983 * @brief Add or modify a resource entry. 2984 * 2985 * If an existing entry exists with the same type and rid, it will be 2986 * modified using the given values of @p start, @p end and @p 2987 * count. If no entry exists, a new one will be created using the 2988 * given values. The resource list entry that matches is then returned. 2989 * 2990 * @param rl the resource list to edit 2991 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 2992 * @param rid the resource identifier 2993 * @param start the start address of the resource 2994 * @param end the end address of the resource 2995 * @param count XXX end-start+1 2996 */ 2997struct resource_list_entry * 2998resource_list_add(struct resource_list *rl, int type, int rid, 2999 u_long start, u_long end, u_long count) 3000{ 3001 struct resource_list_entry *rle; 3002 3003 rle = resource_list_find(rl, type, rid); 3004 if (!rle) { 3005 rle = malloc(sizeof(struct resource_list_entry), M_BUS, 3006 M_NOWAIT); 3007 if (!rle) 3008 panic("resource_list_add: can't record entry"); 3009 STAILQ_INSERT_TAIL(rl, rle, link); 3010 rle->type = type; 3011 rle->rid = rid; 3012 rle->res = NULL; 3013 rle->flags = 0; 3014 } 3015 3016 if (rle->res) 3017 panic("resource_list_add: resource entry is busy"); 3018 3019 rle->start = start; 3020 rle->end = end; 3021 rle->count = count; 3022 return (rle); 3023} 3024 3025/** 3026 * @brief Determine if a resource entry is busy. 3027 * 3028 * Returns true if a resource entry is busy meaning that it has an 3029 * associated resource that is not an unallocated "reserved" resource. 3030 * 3031 * @param rl the resource list to search 3032 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3033 * @param rid the resource identifier 3034 * 3035 * @returns Non-zero if the entry is busy, zero otherwise. 3036 */ 3037int 3038resource_list_busy(struct resource_list *rl, int type, int rid) 3039{ 3040 struct resource_list_entry *rle; 3041 3042 rle = resource_list_find(rl, type, rid); 3043 if (rle == NULL || rle->res == NULL) 3044 return (0); 3045 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) { 3046 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE), 3047 ("reserved resource is active")); 3048 return (0); 3049 } 3050 return (1); 3051} 3052 3053/** 3054 * @brief Determine if a resource entry is reserved. 3055 * 3056 * Returns true if a resource entry is reserved meaning that it has an 3057 * associated "reserved" resource. The resource can either be 3058 * allocated or unallocated. 3059 * 3060 * @param rl the resource list to search 3061 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3062 * @param rid the resource identifier 3063 * 3064 * @returns Non-zero if the entry is reserved, zero otherwise. 3065 */ 3066int 3067resource_list_reserved(struct resource_list *rl, int type, int rid) 3068{ 3069 struct resource_list_entry *rle; 3070 3071 rle = resource_list_find(rl, type, rid); 3072 if (rle != NULL && rle->flags & RLE_RESERVED) 3073 return (1); 3074 return (0); 3075} 3076 3077/** 3078 * @brief Find a resource entry by type and rid. 3079 * 3080 * @param rl the resource list to search 3081 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3082 * @param rid the resource identifier 3083 * 3084 * @returns the resource entry pointer or NULL if there is no such 3085 * entry. 3086 */ 3087struct resource_list_entry * 3088resource_list_find(struct resource_list *rl, int type, int rid) 3089{ 3090 struct resource_list_entry *rle; 3091 3092 STAILQ_FOREACH(rle, rl, link) { 3093 if (rle->type == type && rle->rid == rid) 3094 return (rle); 3095 } 3096 return (NULL); 3097} 3098 3099/** 3100 * @brief Delete a resource entry. 3101 * 3102 * @param rl the resource list to edit 3103 * @param type the resource entry type (e.g. SYS_RES_MEMORY) 3104 * @param rid the resource identifier 3105 */ 3106void 3107resource_list_delete(struct resource_list *rl, int type, int rid) 3108{ 3109 struct resource_list_entry *rle = resource_list_find(rl, type, rid); 3110 3111 if (rle) { 3112 if (rle->res != NULL) 3113 panic("resource_list_delete: resource has not been released"); 3114 STAILQ_REMOVE(rl, rle, resource_list_entry, link); 3115 free(rle, M_BUS); 3116 } 3117} 3118 3119/** 3120 * @brief Allocate a reserved resource 3121 * 3122 * This can be used by busses to force the allocation of resources 3123 * that are always active in the system even if they are not allocated 3124 * by a driver (e.g. PCI BARs). This function is usually called when 3125 * adding a new child to the bus. The resource is allocated from the 3126 * parent bus when it is reserved. The resource list entry is marked 3127 * with RLE_RESERVED to note that it is a reserved resource. 3128 * 3129 * Subsequent attempts to allocate the resource with 3130 * resource_list_alloc() will succeed the first time and will set 3131 * RLE_ALLOCATED to note that it has been allocated. When a reserved 3132 * resource that has been allocated is released with 3133 * resource_list_release() the resource RLE_ALLOCATED is cleared, but 3134 * the actual resource remains allocated. The resource can be released to 3135 * the parent bus by calling resource_list_unreserve(). 3136 * 3137 * @param rl the resource list to allocate from 3138 * @param bus the parent device of @p child 3139 * @param child the device for which the resource is being reserved 3140 * @param type the type of resource to allocate 3141 * @param rid a pointer to the resource identifier 3142 * @param start hint at the start of the resource range - pass 3143 * @c 0UL for any start address 3144 * @param end hint at the end of the resource range - pass 3145 * @c ~0UL for any end address 3146 * @param count hint at the size of range required - pass @c 1 3147 * for any size 3148 * @param flags any extra flags to control the resource 3149 * allocation - see @c RF_XXX flags in 3150 * <sys/rman.h> for details 3151 * 3152 * @returns the resource which was allocated or @c NULL if no 3153 * resource could be allocated 3154 */ 3155struct resource * 3156resource_list_reserve(struct resource_list *rl, device_t bus, device_t child, 3157 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 3158{ 3159 struct resource_list_entry *rle = NULL; 3160 int passthrough = (device_get_parent(child) != bus); 3161 struct resource *r; 3162 3163 if (passthrough) 3164 panic( 3165 "resource_list_reserve() should only be called for direct children"); 3166 if (flags & RF_ACTIVE) 3167 panic( 3168 "resource_list_reserve() should only reserve inactive resources"); 3169 3170 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count, 3171 flags); 3172 if (r != NULL) { 3173 rle = resource_list_find(rl, type, *rid); 3174 rle->flags |= RLE_RESERVED; 3175 } 3176 return (r); 3177} 3178 3179/** 3180 * @brief Helper function for implementing BUS_ALLOC_RESOURCE() 3181 * 3182 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list 3183 * and passing the allocation up to the parent of @p bus. This assumes 3184 * that the first entry of @c device_get_ivars(child) is a struct 3185 * resource_list. This also handles 'passthrough' allocations where a 3186 * child is a remote descendant of bus by passing the allocation up to 3187 * the parent of bus. 3188 * 3189 * Typically, a bus driver would store a list of child resources 3190 * somewhere in the child device's ivars (see device_get_ivars()) and 3191 * its implementation of BUS_ALLOC_RESOURCE() would find that list and 3192 * then call resource_list_alloc() to perform the allocation. 3193 * 3194 * @param rl the resource list to allocate from 3195 * @param bus the parent device of @p child 3196 * @param child the device which is requesting an allocation 3197 * @param type the type of resource to allocate 3198 * @param rid a pointer to the resource identifier 3199 * @param start hint at the start of the resource range - pass 3200 * @c 0UL for any start address 3201 * @param end hint at the end of the resource range - pass 3202 * @c ~0UL for any end address 3203 * @param count hint at the size of range required - pass @c 1 3204 * for any size 3205 * @param flags any extra flags to control the resource 3206 * allocation - see @c RF_XXX flags in 3207 * <sys/rman.h> for details 3208 * 3209 * @returns the resource which was allocated or @c NULL if no 3210 * resource could be allocated 3211 */ 3212struct resource * 3213resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, 3214 int type, int *rid, u_long start, u_long end, u_long count, u_int flags) 3215{ 3216 struct resource_list_entry *rle = NULL; 3217 int passthrough = (device_get_parent(child) != bus); 3218 int isdefault = (start == 0UL && end == ~0UL); 3219 3220 if (passthrough) { 3221 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3222 type, rid, start, end, count, flags)); 3223 } 3224 3225 rle = resource_list_find(rl, type, *rid); 3226 3227 if (!rle) 3228 return (NULL); /* no resource of that type/rid */ 3229 3230 if (rle->res) { 3231 if (rle->flags & RLE_RESERVED) { 3232 if (rle->flags & RLE_ALLOCATED) 3233 return (NULL); 3234 if ((flags & RF_ACTIVE) && 3235 bus_activate_resource(child, type, *rid, 3236 rle->res) != 0) 3237 return (NULL); 3238 rle->flags |= RLE_ALLOCATED; 3239 return (rle->res); 3240 } 3241 panic("resource_list_alloc: resource entry is busy"); 3242 } 3243 3244 if (isdefault) { 3245 start = rle->start; 3246 count = ulmax(count, rle->count); 3247 end = ulmax(rle->end, start + count - 1); 3248 } 3249 3250 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 3251 type, rid, start, end, count, flags); 3252 3253 /* 3254 * Record the new range. 3255 */ 3256 if (rle->res) { 3257 rle->start = rman_get_start(rle->res); 3258 rle->end = rman_get_end(rle->res); 3259 rle->count = count; 3260 } 3261 3262 return (rle->res); 3263} 3264 3265/** 3266 * @brief Helper function for implementing BUS_RELEASE_RESOURCE() 3267 * 3268 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally 3269 * used with resource_list_alloc(). 3270 * 3271 * @param rl the resource list which was allocated from 3272 * @param bus the parent device of @p child 3273 * @param child the device which is requesting a release 3274 * @param type the type of resource to release 3275 * @param rid the resource identifier 3276 * @param res the resource to release 3277 * 3278 * @retval 0 success 3279 * @retval non-zero a standard unix error code indicating what 3280 * error condition prevented the operation 3281 */ 3282int 3283resource_list_release(struct resource_list *rl, device_t bus, device_t child, 3284 int type, int rid, struct resource *res) 3285{ 3286 struct resource_list_entry *rle = NULL; 3287 int passthrough = (device_get_parent(child) != bus); 3288 int error; 3289 3290 if (passthrough) { 3291 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 3292 type, rid, res)); 3293 } 3294 3295 rle = resource_list_find(rl, type, rid); 3296 3297 if (!rle) 3298 panic("resource_list_release: can't find resource"); 3299 if (!rle->res) 3300 panic("resource_list_release: resource entry is not busy"); 3301 if (rle->flags & RLE_RESERVED) { 3302 if (rle->flags & RLE_ALLOCATED) { 3303 if (rman_get_flags(res) & RF_ACTIVE) { 3304 error = bus_deactivate_resource(child, type, 3305 rid, res); 3306 if (error) 3307 return (error); 3308 } 3309 rle->flags &= ~RLE_ALLOCATED; 3310 return (0); 3311 } 3312 return (EINVAL); 3313 } 3314 3315 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, 3316 type, rid, res); 3317 if (error) 3318 return (error); 3319 3320 rle->res = NULL; 3321 return (0); 3322} 3323 3324/** 3325 * @brief Release all active resources of a given type 3326 * 3327 * Release all active resources of a specified type. This is intended 3328 * to be used to cleanup resources leaked by a driver after detach or 3329 * a failed attach. 3330 * 3331 * @param rl the resource list which was allocated from 3332 * @param bus the parent device of @p child 3333 * @param child the device whose active resources are being released 3334 * @param type the type of resources to release 3335 * 3336 * @retval 0 success 3337 * @retval EBUSY at least one resource was active 3338 */ 3339int 3340resource_list_release_active(struct resource_list *rl, device_t bus, 3341 device_t child, int type) 3342{ 3343 struct resource_list_entry *rle; 3344 int error, retval; 3345 3346 retval = 0; 3347 STAILQ_FOREACH(rle, rl, link) { 3348 if (rle->type != type) 3349 continue; 3350 if (rle->res == NULL) 3351 continue; 3352 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == 3353 RLE_RESERVED) 3354 continue; 3355 retval = EBUSY; 3356 error = resource_list_release(rl, bus, child, type, 3357 rman_get_rid(rle->res), rle->res); 3358 if (error != 0) 3359 device_printf(bus, 3360 "Failed to release active resource: %d\n", error); 3361 } 3362 return (retval); 3363} 3364 3365 3366/** 3367 * @brief Fully release a reserved resource 3368 * 3369 * Fully releases a resource reserved via resource_list_reserve(). 3370 * 3371 * @param rl the resource list which was allocated from 3372 * @param bus the parent device of @p child 3373 * @param child the device whose reserved resource is being released 3374 * @param type the type of resource to release 3375 * @param rid the resource identifier 3376 * @param res the resource to release 3377 * 3378 * @retval 0 success 3379 * @retval non-zero a standard unix error code indicating what 3380 * error condition prevented the operation 3381 */ 3382int 3383resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child, 3384 int type, int rid) 3385{ 3386 struct resource_list_entry *rle = NULL; 3387 int passthrough = (device_get_parent(child) != bus); 3388 3389 if (passthrough) 3390 panic( 3391 "resource_list_unreserve() should only be called for direct children"); 3392 3393 rle = resource_list_find(rl, type, rid); 3394 3395 if (!rle) 3396 panic("resource_list_unreserve: can't find resource"); 3397 if (!(rle->flags & RLE_RESERVED)) 3398 return (EINVAL); 3399 if (rle->flags & RLE_ALLOCATED) 3400 return (EBUSY); 3401 rle->flags &= ~RLE_RESERVED; 3402 return (resource_list_release(rl, bus, child, type, rid, rle->res)); 3403} 3404 3405/** 3406 * @brief Print a description of resources in a resource list 3407 * 3408 * Print all resources of a specified type, for use in BUS_PRINT_CHILD(). 3409 * The name is printed if at least one resource of the given type is available. 3410 * The format is used to print resource start and end. 3411 * 3412 * @param rl the resource list to print 3413 * @param name the name of @p type, e.g. @c "memory" 3414 * @param type type type of resource entry to print 3415 * @param format printf(9) format string to print resource 3416 * start and end values 3417 * 3418 * @returns the number of characters printed 3419 */ 3420int 3421resource_list_print_type(struct resource_list *rl, const char *name, int type, 3422 const char *format) 3423{ 3424 struct resource_list_entry *rle; 3425 int printed, retval; 3426 3427 printed = 0; 3428 retval = 0; 3429 /* Yes, this is kinda cheating */ 3430 STAILQ_FOREACH(rle, rl, link) { 3431 if (rle->type == type) { 3432 if (printed == 0) 3433 retval += printf(" %s ", name); 3434 else 3435 retval += printf(","); 3436 printed++; 3437 retval += printf(format, rle->start); 3438 if (rle->count > 1) { 3439 retval += printf("-"); 3440 retval += printf(format, rle->start + 3441 rle->count - 1); 3442 } 3443 } 3444 } 3445 return (retval); 3446} 3447 3448/** 3449 * @brief Releases all the resources in a list. 3450 * 3451 * @param rl The resource list to purge. 3452 * 3453 * @returns nothing 3454 */ 3455void 3456resource_list_purge(struct resource_list *rl) 3457{ 3458 struct resource_list_entry *rle; 3459 3460 while ((rle = STAILQ_FIRST(rl)) != NULL) { 3461 if (rle->res) 3462 bus_release_resource(rman_get_device(rle->res), 3463 rle->type, rle->rid, rle->res); 3464 STAILQ_REMOVE_HEAD(rl, link); 3465 free(rle, M_BUS); 3466 } 3467} 3468 3469device_t 3470bus_generic_add_child(device_t dev, u_int order, const char *name, int unit) 3471{ 3472 3473 return (device_add_child_ordered(dev, order, name, unit)); 3474} 3475 3476/** 3477 * @brief Helper function for implementing DEVICE_PROBE() 3478 * 3479 * This function can be used to help implement the DEVICE_PROBE() for 3480 * a bus (i.e. a device which has other devices attached to it). It 3481 * calls the DEVICE_IDENTIFY() method of each driver in the device's 3482 * devclass. 3483 */ 3484int 3485bus_generic_probe(device_t dev) 3486{ 3487 devclass_t dc = dev->devclass; 3488 driverlink_t dl; 3489 3490 TAILQ_FOREACH(dl, &dc->drivers, link) { 3491 /* 3492 * If this driver's pass is too high, then ignore it. 3493 * For most drivers in the default pass, this will 3494 * never be true. For early-pass drivers they will 3495 * only call the identify routines of eligible drivers 3496 * when this routine is called. Drivers for later 3497 * passes should have their identify routines called 3498 * on early-pass busses during BUS_NEW_PASS(). 3499 */ 3500 if (dl->pass > bus_current_pass) 3501 continue; 3502 DEVICE_IDENTIFY(dl->driver, dev); 3503 } 3504 3505 return (0); 3506} 3507 3508/** 3509 * @brief Helper function for implementing DEVICE_ATTACH() 3510 * 3511 * This function can be used to help implement the DEVICE_ATTACH() for 3512 * a bus. It calls device_probe_and_attach() for each of the device's 3513 * children. 3514 */ 3515int 3516bus_generic_attach(device_t dev) 3517{ 3518 device_t child; 3519 3520 TAILQ_FOREACH(child, &dev->children, link) { 3521 device_probe_and_attach(child); 3522 } 3523 3524 return (0); 3525} 3526 3527/** 3528 * @brief Helper function for implementing DEVICE_DETACH() 3529 * 3530 * This function can be used to help implement the DEVICE_DETACH() for 3531 * a bus. It calls device_detach() for each of the device's 3532 * children. 3533 */ 3534int 3535bus_generic_detach(device_t dev) 3536{ 3537 device_t child; 3538 int error; 3539 3540 if (dev->state != DS_ATTACHED) 3541 return (EBUSY); 3542 3543 TAILQ_FOREACH(child, &dev->children, link) { 3544 if ((error = device_detach(child)) != 0) 3545 return (error); 3546 } 3547 3548 return (0); 3549} 3550 3551/** 3552 * @brief Helper function for implementing DEVICE_SHUTDOWN() 3553 * 3554 * This function can be used to help implement the DEVICE_SHUTDOWN() 3555 * for a bus. It calls device_shutdown() for each of the device's 3556 * children. 3557 */ 3558int 3559bus_generic_shutdown(device_t dev) 3560{ 3561 device_t child; 3562 3563 TAILQ_FOREACH(child, &dev->children, link) { 3564 device_shutdown(child); 3565 } 3566 3567 return (0); 3568} 3569 3570/** 3571 * @brief Helper function for implementing DEVICE_SUSPEND() 3572 * 3573 * This function can be used to help implement the DEVICE_SUSPEND() 3574 * for a bus. It calls DEVICE_SUSPEND() for each of the device's 3575 * children. If any call to DEVICE_SUSPEND() fails, the suspend 3576 * operation is aborted and any devices which were suspended are 3577 * resumed immediately by calling their DEVICE_RESUME() methods. 3578 */ 3579int 3580bus_generic_suspend(device_t dev) 3581{ 3582 int error; 3583 device_t child, child2; 3584 3585 TAILQ_FOREACH(child, &dev->children, link) { 3586 error = DEVICE_SUSPEND(child); 3587 if (error) { 3588 for (child2 = TAILQ_FIRST(&dev->children); 3589 child2 && child2 != child; 3590 child2 = TAILQ_NEXT(child2, link)) 3591 DEVICE_RESUME(child2); 3592 return (error); 3593 } 3594 } 3595 return (0); 3596} 3597 3598/** 3599 * @brief Helper function for implementing DEVICE_RESUME() 3600 * 3601 * This function can be used to help implement the DEVICE_RESUME() for 3602 * a bus. It calls DEVICE_RESUME() on each of the device's children. 3603 */ 3604int 3605bus_generic_resume(device_t dev) 3606{ 3607 device_t child; 3608 3609 TAILQ_FOREACH(child, &dev->children, link) { 3610 DEVICE_RESUME(child); 3611 /* if resume fails, there's nothing we can usefully do... */ 3612 } 3613 return (0); 3614} 3615 3616/** 3617 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3618 * 3619 * This function prints the first part of the ascii representation of 3620 * @p child, including its name, unit and description (if any - see 3621 * device_set_desc()). 3622 * 3623 * @returns the number of characters printed 3624 */ 3625int 3626bus_print_child_header(device_t dev, device_t child) 3627{ 3628 int retval = 0; 3629 3630 if (device_get_desc(child)) { 3631 retval += device_printf(child, "<%s>", device_get_desc(child)); 3632 } else { 3633 retval += printf("%s", device_get_nameunit(child)); 3634 } 3635 3636 return (retval); 3637} 3638 3639/** 3640 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3641 * 3642 * This function prints the last part of the ascii representation of 3643 * @p child, which consists of the string @c " on " followed by the 3644 * name and unit of the @p dev. 3645 * 3646 * @returns the number of characters printed 3647 */ 3648int 3649bus_print_child_footer(device_t dev, device_t child) 3650{ 3651 return (printf(" on %s\n", device_get_nameunit(dev))); 3652} 3653 3654/** 3655 * @brief Helper function for implementing BUS_PRINT_CHILD(). 3656 * 3657 * This function simply calls bus_print_child_header() followed by 3658 * bus_print_child_footer(). 3659 * 3660 * @returns the number of characters printed 3661 */ 3662int 3663bus_generic_print_child(device_t dev, device_t child) 3664{ 3665 int retval = 0; 3666 3667 retval += bus_print_child_header(dev, child); 3668 retval += bus_print_child_footer(dev, child); 3669 3670 return (retval); 3671} 3672 3673/** 3674 * @brief Stub function for implementing BUS_READ_IVAR(). 3675 * 3676 * @returns ENOENT 3677 */ 3678int 3679bus_generic_read_ivar(device_t dev, device_t child, int index, 3680 uintptr_t * result) 3681{ 3682 return (ENOENT); 3683} 3684 3685/** 3686 * @brief Stub function for implementing BUS_WRITE_IVAR(). 3687 * 3688 * @returns ENOENT 3689 */ 3690int 3691bus_generic_write_ivar(device_t dev, device_t child, int index, 3692 uintptr_t value) 3693{ 3694 return (ENOENT); 3695} 3696 3697/** 3698 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST(). 3699 * 3700 * @returns NULL 3701 */ 3702struct resource_list * 3703bus_generic_get_resource_list(device_t dev, device_t child) 3704{ 3705 return (NULL); 3706} 3707 3708/** 3709 * @brief Helper function for implementing BUS_DRIVER_ADDED(). 3710 * 3711 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's 3712 * DEVICE_IDENTIFY() method to allow it to add new children to the bus 3713 * and then calls device_probe_and_attach() for each unattached child. 3714 */ 3715void 3716bus_generic_driver_added(device_t dev, driver_t *driver) 3717{ 3718 device_t child; 3719 3720 DEVICE_IDENTIFY(driver, dev); 3721 TAILQ_FOREACH(child, &dev->children, link) { 3722 if (child->state == DS_NOTPRESENT || 3723 (child->flags & DF_REBID)) 3724 device_probe_and_attach(child); 3725 } 3726} 3727 3728/** 3729 * @brief Helper function for implementing BUS_NEW_PASS(). 3730 * 3731 * This implementing of BUS_NEW_PASS() first calls the identify 3732 * routines for any drivers that probe at the current pass. Then it 3733 * walks the list of devices for this bus. If a device is already 3734 * attached, then it calls BUS_NEW_PASS() on that device. If the 3735 * device is not already attached, it attempts to attach a driver to 3736 * it. 3737 */ 3738void 3739bus_generic_new_pass(device_t dev) 3740{ 3741 driverlink_t dl; 3742 devclass_t dc; 3743 device_t child; 3744 3745 dc = dev->devclass; 3746 TAILQ_FOREACH(dl, &dc->drivers, link) { 3747 if (dl->pass == bus_current_pass) 3748 DEVICE_IDENTIFY(dl->driver, dev); 3749 } 3750 TAILQ_FOREACH(child, &dev->children, link) { 3751 if (child->state >= DS_ATTACHED) 3752 BUS_NEW_PASS(child); 3753 else if (child->state == DS_NOTPRESENT) 3754 device_probe_and_attach(child); 3755 } 3756} 3757 3758/** 3759 * @brief Helper function for implementing BUS_SETUP_INTR(). 3760 * 3761 * This simple implementation of BUS_SETUP_INTR() simply calls the 3762 * BUS_SETUP_INTR() method of the parent of @p dev. 3763 */ 3764int 3765bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 3766 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 3767 void **cookiep) 3768{ 3769 /* Propagate up the bus hierarchy until someone handles it. */ 3770 if (dev->parent) 3771 return (BUS_SETUP_INTR(dev->parent, child, irq, flags, 3772 filter, intr, arg, cookiep)); 3773 return (EINVAL); 3774} 3775 3776/** 3777 * @brief Helper function for implementing BUS_TEARDOWN_INTR(). 3778 * 3779 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the 3780 * BUS_TEARDOWN_INTR() method of the parent of @p dev. 3781 */ 3782int 3783bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, 3784 void *cookie) 3785{ 3786 /* Propagate up the bus hierarchy until someone handles it. */ 3787 if (dev->parent) 3788 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie)); 3789 return (EINVAL); 3790} 3791 3792/** 3793 * @brief Helper function for implementing BUS_ADJUST_RESOURCE(). 3794 * 3795 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the 3796 * BUS_ADJUST_RESOURCE() method of the parent of @p dev. 3797 */ 3798int 3799bus_generic_adjust_resource(device_t dev, device_t child, int type, 3800 struct resource *r, u_long start, u_long end) 3801{ 3802 /* Propagate up the bus hierarchy until someone handles it. */ 3803 if (dev->parent) 3804 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start, 3805 end)); 3806 return (EINVAL); 3807} 3808 3809/** 3810 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 3811 * 3812 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the 3813 * BUS_ALLOC_RESOURCE() method of the parent of @p dev. 3814 */ 3815struct resource * 3816bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, 3817 u_long start, u_long end, u_long count, u_int flags) 3818{ 3819 /* Propagate up the bus hierarchy until someone handles it. */ 3820 if (dev->parent) 3821 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 3822 start, end, count, flags)); 3823 return (NULL); 3824} 3825 3826/** 3827 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 3828 * 3829 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the 3830 * BUS_RELEASE_RESOURCE() method of the parent of @p dev. 3831 */ 3832int 3833bus_generic_release_resource(device_t dev, device_t child, int type, int rid, 3834 struct resource *r) 3835{ 3836 /* Propagate up the bus hierarchy until someone handles it. */ 3837 if (dev->parent) 3838 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 3839 r)); 3840 return (EINVAL); 3841} 3842 3843/** 3844 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE(). 3845 * 3846 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the 3847 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev. 3848 */ 3849int 3850bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, 3851 struct resource *r) 3852{ 3853 /* Propagate up the bus hierarchy until someone handles it. */ 3854 if (dev->parent) 3855 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 3856 r)); 3857 return (EINVAL); 3858} 3859 3860/** 3861 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE(). 3862 * 3863 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the 3864 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev. 3865 */ 3866int 3867bus_generic_deactivate_resource(device_t dev, device_t child, int type, 3868 int rid, struct resource *r) 3869{ 3870 /* Propagate up the bus hierarchy until someone handles it. */ 3871 if (dev->parent) 3872 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid, 3873 r)); 3874 return (EINVAL); 3875} 3876 3877/** 3878 * @brief Helper function for implementing BUS_BIND_INTR(). 3879 * 3880 * This simple implementation of BUS_BIND_INTR() simply calls the 3881 * BUS_BIND_INTR() method of the parent of @p dev. 3882 */ 3883int 3884bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq, 3885 int cpu) 3886{ 3887 3888 /* Propagate up the bus hierarchy until someone handles it. */ 3889 if (dev->parent) 3890 return (BUS_BIND_INTR(dev->parent, child, irq, cpu)); 3891 return (EINVAL); 3892} 3893 3894/** 3895 * @brief Helper function for implementing BUS_CONFIG_INTR(). 3896 * 3897 * This simple implementation of BUS_CONFIG_INTR() simply calls the 3898 * BUS_CONFIG_INTR() method of the parent of @p dev. 3899 */ 3900int 3901bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, 3902 enum intr_polarity pol) 3903{ 3904 3905 /* Propagate up the bus hierarchy until someone handles it. */ 3906 if (dev->parent) 3907 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol)); 3908 return (EINVAL); 3909} 3910 3911/** 3912 * @brief Helper function for implementing BUS_DESCRIBE_INTR(). 3913 * 3914 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the 3915 * BUS_DESCRIBE_INTR() method of the parent of @p dev. 3916 */ 3917int 3918bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq, 3919 void *cookie, const char *descr) 3920{ 3921 3922 /* Propagate up the bus hierarchy until someone handles it. */ 3923 if (dev->parent) 3924 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie, 3925 descr)); 3926 return (EINVAL); 3927} 3928 3929/** 3930 * @brief Helper function for implementing BUS_GET_DMA_TAG(). 3931 * 3932 * This simple implementation of BUS_GET_DMA_TAG() simply calls the 3933 * BUS_GET_DMA_TAG() method of the parent of @p dev. 3934 */ 3935bus_dma_tag_t 3936bus_generic_get_dma_tag(device_t dev, device_t child) 3937{ 3938 3939 /* Propagate up the bus hierarchy until someone handles it. */ 3940 if (dev->parent != NULL) 3941 return (BUS_GET_DMA_TAG(dev->parent, child)); 3942 return (NULL); 3943} 3944 3945/** 3946 * @brief Helper function for implementing BUS_GET_RESOURCE(). 3947 * 3948 * This implementation of BUS_GET_RESOURCE() uses the 3949 * resource_list_find() function to do most of the work. It calls 3950 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3951 * search. 3952 */ 3953int 3954bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, 3955 u_long *startp, u_long *countp) 3956{ 3957 struct resource_list * rl = NULL; 3958 struct resource_list_entry * rle = NULL; 3959 3960 rl = BUS_GET_RESOURCE_LIST(dev, child); 3961 if (!rl) 3962 return (EINVAL); 3963 3964 rle = resource_list_find(rl, type, rid); 3965 if (!rle) 3966 return (ENOENT); 3967 3968 if (startp) 3969 *startp = rle->start; 3970 if (countp) 3971 *countp = rle->count; 3972 3973 return (0); 3974} 3975 3976/** 3977 * @brief Helper function for implementing BUS_SET_RESOURCE(). 3978 * 3979 * This implementation of BUS_SET_RESOURCE() uses the 3980 * resource_list_add() function to do most of the work. It calls 3981 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 3982 * edit. 3983 */ 3984int 3985bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, 3986 u_long start, u_long count) 3987{ 3988 struct resource_list * rl = NULL; 3989 3990 rl = BUS_GET_RESOURCE_LIST(dev, child); 3991 if (!rl) 3992 return (EINVAL); 3993 3994 resource_list_add(rl, type, rid, start, (start + count - 1), count); 3995 3996 return (0); 3997} 3998 3999/** 4000 * @brief Helper function for implementing BUS_DELETE_RESOURCE(). 4001 * 4002 * This implementation of BUS_DELETE_RESOURCE() uses the 4003 * resource_list_delete() function to do most of the work. It calls 4004 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to 4005 * edit. 4006 */ 4007void 4008bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid) 4009{ 4010 struct resource_list * rl = NULL; 4011 4012 rl = BUS_GET_RESOURCE_LIST(dev, child); 4013 if (!rl) 4014 return; 4015 4016 resource_list_delete(rl, type, rid); 4017 4018 return; 4019} 4020 4021/** 4022 * @brief Helper function for implementing BUS_RELEASE_RESOURCE(). 4023 * 4024 * This implementation of BUS_RELEASE_RESOURCE() uses the 4025 * resource_list_release() function to do most of the work. It calls 4026 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4027 */ 4028int 4029bus_generic_rl_release_resource(device_t dev, device_t child, int type, 4030 int rid, struct resource *r) 4031{ 4032 struct resource_list * rl = NULL; 4033 4034 if (device_get_parent(child) != dev) 4035 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child, 4036 type, rid, r)); 4037 4038 rl = BUS_GET_RESOURCE_LIST(dev, child); 4039 if (!rl) 4040 return (EINVAL); 4041 4042 return (resource_list_release(rl, dev, child, type, rid, r)); 4043} 4044 4045/** 4046 * @brief Helper function for implementing BUS_ALLOC_RESOURCE(). 4047 * 4048 * This implementation of BUS_ALLOC_RESOURCE() uses the 4049 * resource_list_alloc() function to do most of the work. It calls 4050 * BUS_GET_RESOURCE_LIST() to find a suitable resource list. 4051 */ 4052struct resource * 4053bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, 4054 int *rid, u_long start, u_long end, u_long count, u_int flags) 4055{ 4056 struct resource_list * rl = NULL; 4057 4058 if (device_get_parent(child) != dev) 4059 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child, 4060 type, rid, start, end, count, flags)); 4061 4062 rl = BUS_GET_RESOURCE_LIST(dev, child); 4063 if (!rl) 4064 return (NULL); 4065 4066 return (resource_list_alloc(rl, dev, child, type, rid, 4067 start, end, count, flags)); 4068} 4069 4070/** 4071 * @brief Helper function for implementing BUS_CHILD_PRESENT(). 4072 * 4073 * This simple implementation of BUS_CHILD_PRESENT() simply calls the 4074 * BUS_CHILD_PRESENT() method of the parent of @p dev. 4075 */ 4076int 4077bus_generic_child_present(device_t dev, device_t child) 4078{ 4079 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev)); 4080} 4081 4082/* 4083 * Some convenience functions to make it easier for drivers to use the 4084 * resource-management functions. All these really do is hide the 4085 * indirection through the parent's method table, making for slightly 4086 * less-wordy code. In the future, it might make sense for this code 4087 * to maintain some sort of a list of resources allocated by each device. 4088 */ 4089 4090int 4091bus_alloc_resources(device_t dev, struct resource_spec *rs, 4092 struct resource **res) 4093{ 4094 int i; 4095 4096 for (i = 0; rs[i].type != -1; i++) 4097 res[i] = NULL; 4098 for (i = 0; rs[i].type != -1; i++) { 4099 res[i] = bus_alloc_resource_any(dev, 4100 rs[i].type, &rs[i].rid, rs[i].flags); 4101 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) { 4102 bus_release_resources(dev, rs, res); 4103 return (ENXIO); 4104 } 4105 } 4106 return (0); 4107} 4108 4109void 4110bus_release_resources(device_t dev, const struct resource_spec *rs, 4111 struct resource **res) 4112{ 4113 int i; 4114 4115 for (i = 0; rs[i].type != -1; i++) 4116 if (res[i] != NULL) { 4117 bus_release_resource( 4118 dev, rs[i].type, rs[i].rid, res[i]); 4119 res[i] = NULL; 4120 } 4121} 4122 4123/** 4124 * @brief Wrapper function for BUS_ALLOC_RESOURCE(). 4125 * 4126 * This function simply calls the BUS_ALLOC_RESOURCE() method of the 4127 * parent of @p dev. 4128 */ 4129struct resource * 4130bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, 4131 u_long count, u_int flags) 4132{ 4133 if (dev->parent == NULL) 4134 return (NULL); 4135 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end, 4136 count, flags)); 4137} 4138 4139/** 4140 * @brief Wrapper function for BUS_ADJUST_RESOURCE(). 4141 * 4142 * This function simply calls the BUS_ADJUST_RESOURCE() method of the 4143 * parent of @p dev. 4144 */ 4145int 4146bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start, 4147 u_long end) 4148{ 4149 if (dev->parent == NULL) 4150 return (EINVAL); 4151 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end)); 4152} 4153 4154/** 4155 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE(). 4156 * 4157 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the 4158 * parent of @p dev. 4159 */ 4160int 4161bus_activate_resource(device_t dev, int type, int rid, struct resource *r) 4162{ 4163 if (dev->parent == NULL) 4164 return (EINVAL); 4165 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 4166} 4167 4168/** 4169 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE(). 4170 * 4171 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the 4172 * parent of @p dev. 4173 */ 4174int 4175bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) 4176{ 4177 if (dev->parent == NULL) 4178 return (EINVAL); 4179 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r)); 4180} 4181 4182/** 4183 * @brief Wrapper function for BUS_RELEASE_RESOURCE(). 4184 * 4185 * This function simply calls the BUS_RELEASE_RESOURCE() method of the 4186 * parent of @p dev. 4187 */ 4188int 4189bus_release_resource(device_t dev, int type, int rid, struct resource *r) 4190{ 4191 if (dev->parent == NULL) 4192 return (EINVAL); 4193 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r)); 4194} 4195 4196/** 4197 * @brief Wrapper function for BUS_SETUP_INTR(). 4198 * 4199 * This function simply calls the BUS_SETUP_INTR() method of the 4200 * parent of @p dev. 4201 */ 4202int 4203bus_setup_intr(device_t dev, struct resource *r, int flags, 4204 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep) 4205{ 4206 int error; 4207 4208 if (dev->parent == NULL) 4209 return (EINVAL); 4210 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler, 4211 arg, cookiep); 4212 if (error != 0) 4213 return (error); 4214 if (handler != NULL && !(flags & INTR_MPSAFE)) 4215 device_printf(dev, "[GIANT-LOCKED]\n"); 4216 return (0); 4217} 4218 4219/** 4220 * @brief Wrapper function for BUS_TEARDOWN_INTR(). 4221 * 4222 * This function simply calls the BUS_TEARDOWN_INTR() method of the 4223 * parent of @p dev. 4224 */ 4225int 4226bus_teardown_intr(device_t dev, struct resource *r, void *cookie) 4227{ 4228 if (dev->parent == NULL) 4229 return (EINVAL); 4230 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie)); 4231} 4232 4233/** 4234 * @brief Wrapper function for BUS_BIND_INTR(). 4235 * 4236 * This function simply calls the BUS_BIND_INTR() method of the 4237 * parent of @p dev. 4238 */ 4239int 4240bus_bind_intr(device_t dev, struct resource *r, int cpu) 4241{ 4242 if (dev->parent == NULL) 4243 return (EINVAL); 4244 return (BUS_BIND_INTR(dev->parent, dev, r, cpu)); 4245} 4246 4247/** 4248 * @brief Wrapper function for BUS_DESCRIBE_INTR(). 4249 * 4250 * This function first formats the requested description into a 4251 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of 4252 * the parent of @p dev. 4253 */ 4254int 4255bus_describe_intr(device_t dev, struct resource *irq, void *cookie, 4256 const char *fmt, ...) 4257{ 4258 va_list ap; 4259 char descr[MAXCOMLEN + 1]; 4260 4261 if (dev->parent == NULL) 4262 return (EINVAL); 4263 va_start(ap, fmt); 4264 vsnprintf(descr, sizeof(descr), fmt, ap); 4265 va_end(ap); 4266 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr)); 4267} 4268 4269/** 4270 * @brief Wrapper function for BUS_SET_RESOURCE(). 4271 * 4272 * This function simply calls the BUS_SET_RESOURCE() method of the 4273 * parent of @p dev. 4274 */ 4275int 4276bus_set_resource(device_t dev, int type, int rid, 4277 u_long start, u_long count) 4278{ 4279 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid, 4280 start, count)); 4281} 4282 4283/** 4284 * @brief Wrapper function for BUS_GET_RESOURCE(). 4285 * 4286 * This function simply calls the BUS_GET_RESOURCE() method of the 4287 * parent of @p dev. 4288 */ 4289int 4290bus_get_resource(device_t dev, int type, int rid, 4291 u_long *startp, u_long *countp) 4292{ 4293 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4294 startp, countp)); 4295} 4296 4297/** 4298 * @brief Wrapper function for BUS_GET_RESOURCE(). 4299 * 4300 * This function simply calls the BUS_GET_RESOURCE() method of the 4301 * parent of @p dev and returns the start value. 4302 */ 4303u_long 4304bus_get_resource_start(device_t dev, int type, int rid) 4305{ 4306 u_long start, count; 4307 int error; 4308 4309 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4310 &start, &count); 4311 if (error) 4312 return (0); 4313 return (start); 4314} 4315 4316/** 4317 * @brief Wrapper function for BUS_GET_RESOURCE(). 4318 * 4319 * This function simply calls the BUS_GET_RESOURCE() method of the 4320 * parent of @p dev and returns the count value. 4321 */ 4322u_long 4323bus_get_resource_count(device_t dev, int type, int rid) 4324{ 4325 u_long start, count; 4326 int error; 4327 4328 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid, 4329 &start, &count); 4330 if (error) 4331 return (0); 4332 return (count); 4333} 4334 4335/** 4336 * @brief Wrapper function for BUS_DELETE_RESOURCE(). 4337 * 4338 * This function simply calls the BUS_DELETE_RESOURCE() method of the 4339 * parent of @p dev. 4340 */ 4341void 4342bus_delete_resource(device_t dev, int type, int rid) 4343{ 4344 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid); 4345} 4346 4347/** 4348 * @brief Wrapper function for BUS_CHILD_PRESENT(). 4349 * 4350 * This function simply calls the BUS_CHILD_PRESENT() method of the 4351 * parent of @p dev. 4352 */ 4353int 4354bus_child_present(device_t child) 4355{ 4356 return (BUS_CHILD_PRESENT(device_get_parent(child), child)); 4357} 4358 4359/** 4360 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR(). 4361 * 4362 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the 4363 * parent of @p dev. 4364 */ 4365int 4366bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen) 4367{ 4368 device_t parent; 4369 4370 parent = device_get_parent(child); 4371 if (parent == NULL) { 4372 *buf = '\0'; 4373 return (0); 4374 } 4375 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen)); 4376} 4377 4378/** 4379 * @brief Wrapper function for BUS_CHILD_LOCATION_STR(). 4380 * 4381 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the 4382 * parent of @p dev. 4383 */ 4384int 4385bus_child_location_str(device_t child, char *buf, size_t buflen) 4386{ 4387 device_t parent; 4388 4389 parent = device_get_parent(child); 4390 if (parent == NULL) { 4391 *buf = '\0'; 4392 return (0); 4393 } 4394 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen)); 4395} 4396 4397/** 4398 * @brief Wrapper function for BUS_GET_DMA_TAG(). 4399 * 4400 * This function simply calls the BUS_GET_DMA_TAG() method of the 4401 * parent of @p dev. 4402 */ 4403bus_dma_tag_t 4404bus_get_dma_tag(device_t dev) 4405{ 4406 device_t parent; 4407 4408 parent = device_get_parent(dev); 4409 if (parent == NULL) 4410 return (NULL); 4411 return (BUS_GET_DMA_TAG(parent, dev)); 4412} 4413 4414/* Resume all devices and then notify userland that we're up again. */ 4415static int 4416root_resume(device_t dev) 4417{ 4418 int error; 4419 4420 error = bus_generic_resume(dev); 4421 if (error == 0) 4422 devctl_notify("kern", "power", "resume", NULL); 4423 return (error); 4424} 4425 4426static int 4427root_print_child(device_t dev, device_t child) 4428{ 4429 int retval = 0; 4430 4431 retval += bus_print_child_header(dev, child); 4432 retval += printf("\n"); 4433 4434 return (retval); 4435} 4436 4437static int 4438root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, 4439 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep) 4440{ 4441 /* 4442 * If an interrupt mapping gets to here something bad has happened. 4443 */ 4444 panic("root_setup_intr"); 4445} 4446 4447/* 4448 * If we get here, assume that the device is permanant and really is 4449 * present in the system. Removable bus drivers are expected to intercept 4450 * this call long before it gets here. We return -1 so that drivers that 4451 * really care can check vs -1 or some ERRNO returned higher in the food 4452 * chain. 4453 */ 4454static int 4455root_child_present(device_t dev, device_t child) 4456{ 4457 return (-1); 4458} 4459 4460static kobj_method_t root_methods[] = { 4461 /* Device interface */ 4462 KOBJMETHOD(device_shutdown, bus_generic_shutdown), 4463 KOBJMETHOD(device_suspend, bus_generic_suspend), 4464 KOBJMETHOD(device_resume, root_resume), 4465 4466 /* Bus interface */ 4467 KOBJMETHOD(bus_print_child, root_print_child), 4468 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar), 4469 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar), 4470 KOBJMETHOD(bus_setup_intr, root_setup_intr), 4471 KOBJMETHOD(bus_child_present, root_child_present), 4472 4473 KOBJMETHOD_END 4474}; 4475 4476static driver_t root_driver = { 4477 "root", 4478 root_methods, 4479 1, /* no softc */ 4480}; 4481 4482device_t root_bus; 4483devclass_t root_devclass; 4484 4485static int 4486root_bus_module_handler(module_t mod, int what, void* arg) 4487{ 4488 switch (what) { 4489 case MOD_LOAD: 4490 TAILQ_INIT(&bus_data_devices); 4491 kobj_class_compile((kobj_class_t) &root_driver); 4492 root_bus = make_device(NULL, "root", 0); 4493 root_bus->desc = "System root bus"; 4494 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver); 4495 root_bus->driver = &root_driver; 4496 root_bus->state = DS_ATTACHED; 4497 root_devclass = devclass_find_internal("root", NULL, FALSE); 4498 devinit(); 4499 return (0); 4500 4501 case MOD_SHUTDOWN: 4502 device_shutdown(root_bus); 4503 return (0); 4504 default: 4505 return (EOPNOTSUPP); 4506 } 4507 4508 return (0); 4509} 4510 4511static moduledata_t root_bus_mod = { 4512 "rootbus", 4513 root_bus_module_handler, 4514 NULL 4515}; 4516DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 4517 4518/** 4519 * @brief Automatically configure devices 4520 * 4521 * This function begins the autoconfiguration process by calling 4522 * device_probe_and_attach() for each child of the @c root0 device. 4523 */ 4524void 4525root_bus_configure(void) 4526{ 4527 4528 PDEBUG((".")); 4529 4530 /* Eventually this will be split up, but this is sufficient for now. */ 4531 bus_set_pass(BUS_PASS_DEFAULT); 4532} 4533 4534/** 4535 * @brief Module handler for registering device drivers 4536 * 4537 * This module handler is used to automatically register device 4538 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls 4539 * devclass_add_driver() for the driver described by the 4540 * driver_module_data structure pointed to by @p arg 4541 */ 4542int 4543driver_module_handler(module_t mod, int what, void *arg) 4544{ 4545 struct driver_module_data *dmd; 4546 devclass_t bus_devclass; 4547 kobj_class_t driver; 4548 int error, pass; 4549 4550 dmd = (struct driver_module_data *)arg; 4551 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE); 4552 error = 0; 4553 4554 switch (what) { 4555 case MOD_LOAD: 4556 if (dmd->dmd_chainevh) 4557 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 4558 4559 pass = dmd->dmd_pass; 4560 driver = dmd->dmd_driver; 4561 PDEBUG(("Loading module: driver %s on bus %s (pass %d)", 4562 DRIVERNAME(driver), dmd->dmd_busname, pass)); 4563 error = devclass_add_driver(bus_devclass, driver, pass, 4564 dmd->dmd_devclass); 4565 break; 4566 4567 case MOD_UNLOAD: 4568 PDEBUG(("Unloading module: driver %s from bus %s", 4569 DRIVERNAME(dmd->dmd_driver), 4570 dmd->dmd_busname)); 4571 error = devclass_delete_driver(bus_devclass, 4572 dmd->dmd_driver); 4573 4574 if (!error && dmd->dmd_chainevh) 4575 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 4576 break; 4577 case MOD_QUIESCE: 4578 PDEBUG(("Quiesce module: driver %s from bus %s", 4579 DRIVERNAME(dmd->dmd_driver), 4580 dmd->dmd_busname)); 4581 error = devclass_quiesce_driver(bus_devclass, 4582 dmd->dmd_driver); 4583 4584 if (!error && dmd->dmd_chainevh) 4585 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg); 4586 break; 4587 default: 4588 error = EOPNOTSUPP; 4589 break; 4590 } 4591 4592 return (error); 4593} 4594 4595/** 4596 * @brief Enumerate all hinted devices for this bus. 4597 * 4598 * Walks through the hints for this bus and calls the bus_hinted_child 4599 * routine for each one it fines. It searches first for the specific 4600 * bus that's being probed for hinted children (eg isa0), and then for 4601 * generic children (eg isa). 4602 * 4603 * @param dev bus device to enumerate 4604 */ 4605void 4606bus_enumerate_hinted_children(device_t bus) 4607{ 4608 int i; 4609 const char *dname, *busname; 4610 int dunit; 4611 4612 /* 4613 * enumerate all devices on the specific bus 4614 */ 4615 busname = device_get_nameunit(bus); 4616 i = 0; 4617 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 4618 BUS_HINTED_CHILD(bus, dname, dunit); 4619 4620 /* 4621 * and all the generic ones. 4622 */ 4623 busname = device_get_name(bus); 4624 i = 0; 4625 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0) 4626 BUS_HINTED_CHILD(bus, dname, dunit); 4627} 4628 4629#ifdef BUS_DEBUG 4630 4631/* the _short versions avoid iteration by not calling anything that prints 4632 * more than oneliners. I love oneliners. 4633 */ 4634 4635static void 4636print_device_short(device_t dev, int indent) 4637{ 4638 if (!dev) 4639 return; 4640 4641 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n", 4642 dev->unit, dev->desc, 4643 (dev->parent? "":"no "), 4644 (TAILQ_EMPTY(&dev->children)? "no ":""), 4645 (dev->flags&DF_ENABLED? "enabled,":"disabled,"), 4646 (dev->flags&DF_FIXEDCLASS? "fixed,":""), 4647 (dev->flags&DF_WILDCARD? "wildcard,":""), 4648 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""), 4649 (dev->flags&DF_REBID? "rebiddable,":""), 4650 (dev->ivars? "":"no "), 4651 (dev->softc? "":"no "), 4652 dev->busy)); 4653} 4654 4655static void 4656print_device(device_t dev, int indent) 4657{ 4658 if (!dev) 4659 return; 4660 4661 print_device_short(dev, indent); 4662 4663 indentprintf(("Parent:\n")); 4664 print_device_short(dev->parent, indent+1); 4665 indentprintf(("Driver:\n")); 4666 print_driver_short(dev->driver, indent+1); 4667 indentprintf(("Devclass:\n")); 4668 print_devclass_short(dev->devclass, indent+1); 4669} 4670 4671void 4672print_device_tree_short(device_t dev, int indent) 4673/* print the device and all its children (indented) */ 4674{ 4675 device_t child; 4676 4677 if (!dev) 4678 return; 4679 4680 print_device_short(dev, indent); 4681 4682 TAILQ_FOREACH(child, &dev->children, link) { 4683 print_device_tree_short(child, indent+1); 4684 } 4685} 4686 4687void 4688print_device_tree(device_t dev, int indent) 4689/* print the device and all its children (indented) */ 4690{ 4691 device_t child; 4692 4693 if (!dev) 4694 return; 4695 4696 print_device(dev, indent); 4697 4698 TAILQ_FOREACH(child, &dev->children, link) { 4699 print_device_tree(child, indent+1); 4700 } 4701} 4702 4703static void 4704print_driver_short(driver_t *driver, int indent) 4705{ 4706 if (!driver) 4707 return; 4708 4709 indentprintf(("driver %s: softc size = %zd\n", 4710 driver->name, driver->size)); 4711} 4712 4713static void 4714print_driver(driver_t *driver, int indent) 4715{ 4716 if (!driver) 4717 return; 4718 4719 print_driver_short(driver, indent); 4720} 4721 4722static void 4723print_driver_list(driver_list_t drivers, int indent) 4724{ 4725 driverlink_t driver; 4726 4727 TAILQ_FOREACH(driver, &drivers, link) { 4728 print_driver(driver->driver, indent); 4729 } 4730} 4731 4732static void 4733print_devclass_short(devclass_t dc, int indent) 4734{ 4735 if ( !dc ) 4736 return; 4737 4738 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit)); 4739} 4740 4741static void 4742print_devclass(devclass_t dc, int indent) 4743{ 4744 int i; 4745 4746 if ( !dc ) 4747 return; 4748 4749 print_devclass_short(dc, indent); 4750 indentprintf(("Drivers:\n")); 4751 print_driver_list(dc->drivers, indent+1); 4752 4753 indentprintf(("Devices:\n")); 4754 for (i = 0; i < dc->maxunit; i++) 4755 if (dc->devices[i]) 4756 print_device(dc->devices[i], indent+1); 4757} 4758 4759void 4760print_devclass_list_short(void) 4761{ 4762 devclass_t dc; 4763 4764 printf("Short listing of devclasses, drivers & devices:\n"); 4765 TAILQ_FOREACH(dc, &devclasses, link) { 4766 print_devclass_short(dc, 0); 4767 } 4768} 4769 4770void 4771print_devclass_list(void) 4772{ 4773 devclass_t dc; 4774 4775 printf("Full listing of devclasses, drivers & devices:\n"); 4776 TAILQ_FOREACH(dc, &devclasses, link) { 4777 print_devclass(dc, 0); 4778 } 4779} 4780 4781#endif 4782 4783/* 4784 * User-space access to the device tree. 4785 * 4786 * We implement a small set of nodes: 4787 * 4788 * hw.bus Single integer read method to obtain the 4789 * current generation count. 4790 * hw.bus.devices Reads the entire device tree in flat space. 4791 * hw.bus.rman Resource manager interface 4792 * 4793 * We might like to add the ability to scan devclasses and/or drivers to 4794 * determine what else is currently loaded/available. 4795 */ 4796 4797static int 4798sysctl_bus(SYSCTL_HANDLER_ARGS) 4799{ 4800 struct u_businfo ubus; 4801 4802 ubus.ub_version = BUS_USER_VERSION; 4803 ubus.ub_generation = bus_data_generation; 4804 4805 return (SYSCTL_OUT(req, &ubus, sizeof(ubus))); 4806} 4807SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus, 4808 "bus-related data"); 4809 4810static int 4811sysctl_devices(SYSCTL_HANDLER_ARGS) 4812{ 4813 int *name = (int *)arg1; 4814 u_int namelen = arg2; 4815 int index; 4816 struct device *dev; 4817 struct u_device udev; /* XXX this is a bit big */ 4818 int error; 4819 4820 if (namelen != 2) 4821 return (EINVAL); 4822 4823 if (bus_data_generation_check(name[0])) 4824 return (EINVAL); 4825 4826 index = name[1]; 4827 4828 /* 4829 * Scan the list of devices, looking for the requested index. 4830 */ 4831 TAILQ_FOREACH(dev, &bus_data_devices, devlink) { 4832 if (index-- == 0) 4833 break; 4834 } 4835 if (dev == NULL) 4836 return (ENOENT); 4837 4838 /* 4839 * Populate the return array. 4840 */ 4841 bzero(&udev, sizeof(udev)); 4842 udev.dv_handle = (uintptr_t)dev; 4843 udev.dv_parent = (uintptr_t)dev->parent; 4844 if (dev->nameunit != NULL) 4845 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name)); 4846 if (dev->desc != NULL) 4847 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc)); 4848 if (dev->driver != NULL && dev->driver->name != NULL) 4849 strlcpy(udev.dv_drivername, dev->driver->name, 4850 sizeof(udev.dv_drivername)); 4851 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo)); 4852 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location)); 4853 udev.dv_devflags = dev->devflags; 4854 udev.dv_flags = dev->flags; 4855 udev.dv_state = dev->state; 4856 error = SYSCTL_OUT(req, &udev, sizeof(udev)); 4857 return (error); 4858} 4859 4860SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices, 4861 "system device tree"); 4862 4863int 4864bus_data_generation_check(int generation) 4865{ 4866 if (generation != bus_data_generation) 4867 return (1); 4868 4869 /* XXX generate optimised lists here? */ 4870 return (0); 4871} 4872 4873void 4874bus_data_generation_update(void) 4875{ 4876 bus_data_generation++; 4877} 4878 4879int 4880bus_free_resource(device_t dev, int type, struct resource *r) 4881{ 4882 if (r == NULL) 4883 return (0); 4884 return (bus_release_resource(dev, type, rman_get_rid(r), r)); 4885} 4886