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