FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_bus.c
1 /*-
2 * Copyright (c) 1997,1998,2003 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_bus.h"
31
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/filio.h>
35 #include <sys/lock.h>
36 #include <sys/kernel.h>
37 #include <sys/kobj.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/poll.h>
42 #include <sys/proc.h>
43 #include <sys/condvar.h>
44 #include <sys/queue.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <sys/selinfo.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/uio.h>
52 #include <sys/bus.h>
53 #include <sys/interrupt.h>
54
55 #include <machine/stdarg.h>
56
57 #include <vm/uma.h>
58
59 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
60 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
61
62 /*
63 * Used to attach drivers to devclasses.
64 */
65 typedef struct driverlink *driverlink_t;
66 struct driverlink {
67 kobj_class_t driver;
68 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
69 };
70
71 /*
72 * Forward declarations
73 */
74 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
75 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
76 typedef TAILQ_HEAD(device_list, device) device_list_t;
77
78 struct devclass {
79 TAILQ_ENTRY(devclass) link;
80 devclass_t parent; /* parent in devclass hierarchy */
81 driver_list_t drivers; /* bus devclasses store drivers for bus */
82 char *name;
83 device_t *devices; /* array of devices indexed by unit */
84 int maxunit; /* size of devices array */
85
86 struct sysctl_ctx_list sysctl_ctx;
87 struct sysctl_oid *sysctl_tree;
88 };
89
90 /**
91 * @brief Implementation of device.
92 */
93 struct device {
94 /*
95 * A device is a kernel object. The first field must be the
96 * current ops table for the object.
97 */
98 KOBJ_FIELDS;
99
100 /*
101 * Device hierarchy.
102 */
103 TAILQ_ENTRY(device) link; /**< list of devices in parent */
104 TAILQ_ENTRY(device) devlink; /**< global device list membership */
105 device_t parent; /**< parent of this device */
106 device_list_t children; /**< list of child devices */
107
108 /*
109 * Details of this device.
110 */
111 driver_t *driver; /**< current driver */
112 devclass_t devclass; /**< current device class */
113 int unit; /**< current unit number */
114 char* nameunit; /**< name+unit e.g. foodev0 */
115 char* desc; /**< driver specific description */
116 int busy; /**< count of calls to device_busy() */
117 device_state_t state; /**< current device state */
118 u_int32_t devflags; /**< api level flags for device_get_flags() */
119 u_short flags; /**< internal device flags */
120 #define DF_ENABLED 1 /* device should be probed/attached */
121 #define DF_FIXEDCLASS 2 /* devclass specified at create time */
122 #define DF_WILDCARD 4 /* unit was originally wildcard */
123 #define DF_DESCMALLOCED 8 /* description was malloced */
124 #define DF_QUIET 16 /* don't print verbose attach message */
125 #define DF_DONENOMATCH 32 /* don't execute DEVICE_NOMATCH again */
126 #define DF_EXTERNALSOFTC 64 /* softc not allocated by us */
127 #define DF_REBID 128 /* Can rebid after attach */
128 u_char order; /**< order from device_add_child_ordered() */
129 u_char pad;
130 void *ivars; /**< instance variables */
131 void *softc; /**< current driver's variables */
132
133 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
134 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
135 };
136
137 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
138 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
139
140 #ifdef BUS_DEBUG
141
142 static int bus_debug = 1;
143 TUNABLE_INT("bus.debug", &bus_debug);
144 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
145 "Debug bus code");
146
147 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
148 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
149 #define DRIVERNAME(d) ((d)? d->name : "no driver")
150 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
151
152 /**
153 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
154 * prevent syslog from deleting initial spaces
155 */
156 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
157
158 static void print_device_short(device_t dev, int indent);
159 static void print_device(device_t dev, int indent);
160 void print_device_tree_short(device_t dev, int indent);
161 void print_device_tree(device_t dev, int indent);
162 static void print_driver_short(driver_t *driver, int indent);
163 static void print_driver(driver_t *driver, int indent);
164 static void print_driver_list(driver_list_t drivers, int indent);
165 static void print_devclass_short(devclass_t dc, int indent);
166 static void print_devclass(devclass_t dc, int indent);
167 void print_devclass_list_short(void);
168 void print_devclass_list(void);
169
170 #else
171 /* Make the compiler ignore the function calls */
172 #define PDEBUG(a) /* nop */
173 #define DEVICENAME(d) /* nop */
174 #define DRIVERNAME(d) /* nop */
175 #define DEVCLANAME(d) /* nop */
176
177 #define print_device_short(d,i) /* nop */
178 #define print_device(d,i) /* nop */
179 #define print_device_tree_short(d,i) /* nop */
180 #define print_device_tree(d,i) /* nop */
181 #define print_driver_short(d,i) /* nop */
182 #define print_driver(d,i) /* nop */
183 #define print_driver_list(d,i) /* nop */
184 #define print_devclass_short(d,i) /* nop */
185 #define print_devclass(d,i) /* nop */
186 #define print_devclass_list_short() /* nop */
187 #define print_devclass_list() /* nop */
188 #endif
189
190 /*
191 * dev sysctl tree
192 */
193
194 enum {
195 DEVCLASS_SYSCTL_PARENT,
196 };
197
198 static int
199 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
200 {
201 devclass_t dc = (devclass_t)arg1;
202 const char *value;
203
204 switch (arg2) {
205 case DEVCLASS_SYSCTL_PARENT:
206 value = dc->parent ? dc->parent->name : "";
207 break;
208 default:
209 return (EINVAL);
210 }
211 return (SYSCTL_OUT(req, value, strlen(value)));
212 }
213
214 static void
215 devclass_sysctl_init(devclass_t dc)
216 {
217
218 if (dc->sysctl_tree != NULL)
219 return;
220 sysctl_ctx_init(&dc->sysctl_ctx);
221 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
222 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
223 CTLFLAG_RD, 0, "");
224 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
225 OID_AUTO, "%parent", CTLFLAG_RD,
226 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
227 "parent class");
228 }
229
230 enum {
231 DEVICE_SYSCTL_DESC,
232 DEVICE_SYSCTL_DRIVER,
233 DEVICE_SYSCTL_LOCATION,
234 DEVICE_SYSCTL_PNPINFO,
235 DEVICE_SYSCTL_PARENT,
236 };
237
238 static int
239 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
240 {
241 device_t dev = (device_t)arg1;
242 const char *value;
243 char *buf;
244 int error;
245
246 buf = NULL;
247 switch (arg2) {
248 case DEVICE_SYSCTL_DESC:
249 value = dev->desc ? dev->desc : "";
250 break;
251 case DEVICE_SYSCTL_DRIVER:
252 value = dev->driver ? dev->driver->name : "";
253 break;
254 case DEVICE_SYSCTL_LOCATION:
255 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
256 bus_child_location_str(dev, buf, 1024);
257 break;
258 case DEVICE_SYSCTL_PNPINFO:
259 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
260 bus_child_pnpinfo_str(dev, buf, 1024);
261 break;
262 case DEVICE_SYSCTL_PARENT:
263 value = dev->parent ? dev->parent->nameunit : "";
264 break;
265 default:
266 return (EINVAL);
267 }
268 error = SYSCTL_OUT(req, value, strlen(value));
269 if (buf != NULL)
270 free(buf, M_BUS);
271 return (error);
272 }
273
274 static void
275 device_sysctl_init(device_t dev)
276 {
277 devclass_t dc = dev->devclass;
278
279 if (dev->sysctl_tree != NULL)
280 return;
281 devclass_sysctl_init(dc);
282 sysctl_ctx_init(&dev->sysctl_ctx);
283 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
284 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
285 dev->nameunit + strlen(dc->name),
286 CTLFLAG_RD, 0, "");
287 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
288 OID_AUTO, "%desc", CTLFLAG_RD,
289 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
290 "device description");
291 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
292 OID_AUTO, "%driver", CTLFLAG_RD,
293 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
294 "device driver name");
295 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 OID_AUTO, "%location", CTLFLAG_RD,
297 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
298 "device location relative to parent");
299 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 OID_AUTO, "%pnpinfo", CTLFLAG_RD,
301 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
302 "device identification");
303 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 OID_AUTO, "%parent", CTLFLAG_RD,
305 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
306 "parent device");
307 }
308
309 static void
310 device_sysctl_fini(device_t dev)
311 {
312 if (dev->sysctl_tree == NULL)
313 return;
314 sysctl_ctx_free(&dev->sysctl_ctx);
315 dev->sysctl_tree = NULL;
316 }
317
318 /*
319 * /dev/devctl implementation
320 */
321
322 /*
323 * This design allows only one reader for /dev/devctl. This is not desirable
324 * in the long run, but will get a lot of hair out of this implementation.
325 * Maybe we should make this device a clonable device.
326 *
327 * Also note: we specifically do not attach a device to the device_t tree
328 * to avoid potential chicken and egg problems. One could argue that all
329 * of this belongs to the root node. One could also further argue that the
330 * sysctl interface that we have not might more properly be an ioctl
331 * interface, but at this stage of the game, I'm not inclined to rock that
332 * boat.
333 *
334 * I'm also not sure that the SIGIO support is done correctly or not, as
335 * I copied it from a driver that had SIGIO support that likely hasn't been
336 * tested since 3.4 or 2.2.8!
337 */
338
339 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
340 static int devctl_disable = 0;
341 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
342 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
343 sysctl_devctl_disable, "I", "devctl disable");
344
345 static d_open_t devopen;
346 static d_close_t devclose;
347 static d_read_t devread;
348 static d_ioctl_t devioctl;
349 static d_poll_t devpoll;
350
351 static struct cdevsw dev_cdevsw = {
352 .d_version = D_VERSION,
353 .d_flags = D_NEEDGIANT,
354 .d_open = devopen,
355 .d_close = devclose,
356 .d_read = devread,
357 .d_ioctl = devioctl,
358 .d_poll = devpoll,
359 .d_name = "devctl",
360 };
361
362 struct dev_event_info
363 {
364 char *dei_data;
365 TAILQ_ENTRY(dev_event_info) dei_link;
366 };
367
368 TAILQ_HEAD(devq, dev_event_info);
369
370 static struct dev_softc
371 {
372 int inuse;
373 int nonblock;
374 struct mtx mtx;
375 struct cv cv;
376 struct selinfo sel;
377 struct devq devq;
378 struct proc *async_proc;
379 } devsoftc;
380
381 static struct cdev *devctl_dev;
382
383 static void
384 devinit(void)
385 {
386 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
387 "devctl");
388 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
389 cv_init(&devsoftc.cv, "dev cv");
390 TAILQ_INIT(&devsoftc.devq);
391 }
392
393 static int
394 devopen(struct cdev *dev, int oflags, int devtype, d_thread_t *td)
395 {
396 if (devsoftc.inuse)
397 return (EBUSY);
398 /* move to init */
399 devsoftc.inuse = 1;
400 devsoftc.nonblock = 0;
401 devsoftc.async_proc = NULL;
402 return (0);
403 }
404
405 static int
406 devclose(struct cdev *dev, int fflag, int devtype, d_thread_t *td)
407 {
408 devsoftc.inuse = 0;
409 mtx_lock(&devsoftc.mtx);
410 cv_broadcast(&devsoftc.cv);
411 mtx_unlock(&devsoftc.mtx);
412
413 return (0);
414 }
415
416 /*
417 * The read channel for this device is used to report changes to
418 * userland in realtime. We are required to free the data as well as
419 * the n1 object because we allocate them separately. Also note that
420 * we return one record at a time. If you try to read this device a
421 * character at a time, you will lose the rest of the data. Listening
422 * programs are expected to cope.
423 */
424 static int
425 devread(struct cdev *dev, struct uio *uio, int ioflag)
426 {
427 struct dev_event_info *n1;
428 int rv;
429
430 mtx_lock(&devsoftc.mtx);
431 while (TAILQ_EMPTY(&devsoftc.devq)) {
432 if (devsoftc.nonblock) {
433 mtx_unlock(&devsoftc.mtx);
434 return (EAGAIN);
435 }
436 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
437 if (rv) {
438 /*
439 * Need to translate ERESTART to EINTR here? -- jake
440 */
441 mtx_unlock(&devsoftc.mtx);
442 return (rv);
443 }
444 }
445 n1 = TAILQ_FIRST(&devsoftc.devq);
446 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
447 mtx_unlock(&devsoftc.mtx);
448 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
449 free(n1->dei_data, M_BUS);
450 free(n1, M_BUS);
451 return (rv);
452 }
453
454 static int
455 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
456 {
457 switch (cmd) {
458
459 case FIONBIO:
460 if (*(int*)data)
461 devsoftc.nonblock = 1;
462 else
463 devsoftc.nonblock = 0;
464 return (0);
465 case FIOASYNC:
466 if (*(int*)data)
467 devsoftc.async_proc = td->td_proc;
468 else
469 devsoftc.async_proc = NULL;
470 return (0);
471
472 /* (un)Support for other fcntl() calls. */
473 case FIOCLEX:
474 case FIONCLEX:
475 case FIONREAD:
476 case FIOSETOWN:
477 case FIOGETOWN:
478 default:
479 break;
480 }
481 return (ENOTTY);
482 }
483
484 static int
485 devpoll(struct cdev *dev, int events, d_thread_t *td)
486 {
487 int revents = 0;
488
489 mtx_lock(&devsoftc.mtx);
490 if (events & (POLLIN | POLLRDNORM)) {
491 if (!TAILQ_EMPTY(&devsoftc.devq))
492 revents = events & (POLLIN | POLLRDNORM);
493 else
494 selrecord(td, &devsoftc.sel);
495 }
496 mtx_unlock(&devsoftc.mtx);
497
498 return (revents);
499 }
500
501 /**
502 * @brief Return whether the userland process is running
503 */
504 boolean_t
505 devctl_process_running(void)
506 {
507 return (devsoftc.inuse == 1);
508 }
509
510 /**
511 * @brief Queue data to be read from the devctl device
512 *
513 * Generic interface to queue data to the devctl device. It is
514 * assumed that @p data is properly formatted. It is further assumed
515 * that @p data is allocated using the M_BUS malloc type.
516 */
517 void
518 devctl_queue_data(char *data)
519 {
520 struct dev_event_info *n1 = NULL;
521 struct proc *p;
522
523 n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT);
524 if (n1 == NULL)
525 return;
526 n1->dei_data = data;
527 mtx_lock(&devsoftc.mtx);
528 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
529 cv_broadcast(&devsoftc.cv);
530 mtx_unlock(&devsoftc.mtx);
531 selwakeup(&devsoftc.sel);
532 p = devsoftc.async_proc;
533 if (p != NULL) {
534 PROC_LOCK(p);
535 psignal(p, SIGIO);
536 PROC_UNLOCK(p);
537 }
538 }
539
540 /**
541 * @brief Send a 'notification' to userland, using standard ways
542 */
543 void
544 devctl_notify(const char *system, const char *subsystem, const char *type,
545 const char *data)
546 {
547 int len = 0;
548 char *msg;
549
550 if (system == NULL)
551 return; /* BOGUS! Must specify system. */
552 if (subsystem == NULL)
553 return; /* BOGUS! Must specify subsystem. */
554 if (type == NULL)
555 return; /* BOGUS! Must specify type. */
556 len += strlen(" system=") + strlen(system);
557 len += strlen(" subsystem=") + strlen(subsystem);
558 len += strlen(" type=") + strlen(type);
559 /* add in the data message plus newline. */
560 if (data != NULL)
561 len += strlen(data);
562 len += 3; /* '!', '\n', and NUL */
563 msg = malloc(len, M_BUS, M_NOWAIT);
564 if (msg == NULL)
565 return; /* Drop it on the floor */
566 if (data != NULL)
567 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
568 system, subsystem, type, data);
569 else
570 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
571 system, subsystem, type);
572 devctl_queue_data(msg);
573 }
574
575 /*
576 * Common routine that tries to make sending messages as easy as possible.
577 * We allocate memory for the data, copy strings into that, but do not
578 * free it unless there's an error. The dequeue part of the driver should
579 * free the data. We don't send data when the device is disabled. We do
580 * send data, even when we have no listeners, because we wish to avoid
581 * races relating to startup and restart of listening applications.
582 *
583 * devaddq is designed to string together the type of event, with the
584 * object of that event, plus the plug and play info and location info
585 * for that event. This is likely most useful for devices, but less
586 * useful for other consumers of this interface. Those should use
587 * the devctl_queue_data() interface instead.
588 */
589 static void
590 devaddq(const char *type, const char *what, device_t dev)
591 {
592 char *data = NULL;
593 char *loc = NULL;
594 char *pnp = NULL;
595 const char *parstr;
596
597 if (devctl_disable)
598 return;
599 data = malloc(1024, M_BUS, M_NOWAIT);
600 if (data == NULL)
601 goto bad;
602
603 /* get the bus specific location of this device */
604 loc = malloc(1024, M_BUS, M_NOWAIT);
605 if (loc == NULL)
606 goto bad;
607 *loc = '\0';
608 bus_child_location_str(dev, loc, 1024);
609
610 /* Get the bus specific pnp info of this device */
611 pnp = malloc(1024, M_BUS, M_NOWAIT);
612 if (pnp == NULL)
613 goto bad;
614 *pnp = '\0';
615 bus_child_pnpinfo_str(dev, pnp, 1024);
616
617 /* Get the parent of this device, or / if high enough in the tree. */
618 if (device_get_parent(dev) == NULL)
619 parstr = "."; /* Or '/' ? */
620 else
621 parstr = device_get_nameunit(device_get_parent(dev));
622 /* String it all together. */
623 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
624 parstr);
625 free(loc, M_BUS);
626 free(pnp, M_BUS);
627 devctl_queue_data(data);
628 return;
629 bad:
630 free(pnp, M_BUS);
631 free(loc, M_BUS);
632 free(data, M_BUS);
633 return;
634 }
635
636 /*
637 * A device was added to the tree. We are called just after it successfully
638 * attaches (that is, probe and attach success for this device). No call
639 * is made if a device is merely parented into the tree. See devnomatch
640 * if probe fails. If attach fails, no notification is sent (but maybe
641 * we should have a different message for this).
642 */
643 static void
644 devadded(device_t dev)
645 {
646 char *pnp = NULL;
647 char *tmp = NULL;
648
649 pnp = malloc(1024, M_BUS, M_NOWAIT);
650 if (pnp == NULL)
651 goto fail;
652 tmp = malloc(1024, M_BUS, M_NOWAIT);
653 if (tmp == NULL)
654 goto fail;
655 *pnp = '\0';
656 bus_child_pnpinfo_str(dev, pnp, 1024);
657 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
658 devaddq("+", tmp, dev);
659 fail:
660 if (pnp != NULL)
661 free(pnp, M_BUS);
662 if (tmp != NULL)
663 free(tmp, M_BUS);
664 return;
665 }
666
667 /*
668 * A device was removed from the tree. We are called just before this
669 * happens.
670 */
671 static void
672 devremoved(device_t dev)
673 {
674 char *pnp = NULL;
675 char *tmp = NULL;
676
677 pnp = malloc(1024, M_BUS, M_NOWAIT);
678 if (pnp == NULL)
679 goto fail;
680 tmp = malloc(1024, M_BUS, M_NOWAIT);
681 if (tmp == NULL)
682 goto fail;
683 *pnp = '\0';
684 bus_child_pnpinfo_str(dev, pnp, 1024);
685 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
686 devaddq("-", tmp, dev);
687 fail:
688 if (pnp != NULL)
689 free(pnp, M_BUS);
690 if (tmp != NULL)
691 free(tmp, M_BUS);
692 return;
693 }
694
695 /*
696 * Called when there's no match for this device. This is only called
697 * the first time that no match happens, so we don't keep getitng this
698 * message. Should that prove to be undesirable, we can change it.
699 * This is called when all drivers that can attach to a given bus
700 * decline to accept this device. Other errrors may not be detected.
701 */
702 static void
703 devnomatch(device_t dev)
704 {
705 devaddq("?", "", dev);
706 }
707
708 static int
709 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
710 {
711 struct dev_event_info *n1;
712 int dis, error;
713
714 dis = devctl_disable;
715 error = sysctl_handle_int(oidp, &dis, 0, req);
716 if (error || !req->newptr)
717 return (error);
718 mtx_lock(&devsoftc.mtx);
719 devctl_disable = dis;
720 if (dis) {
721 while (!TAILQ_EMPTY(&devsoftc.devq)) {
722 n1 = TAILQ_FIRST(&devsoftc.devq);
723 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
724 free(n1->dei_data, M_BUS);
725 free(n1, M_BUS);
726 }
727 }
728 mtx_unlock(&devsoftc.mtx);
729 return (0);
730 }
731
732 /* End of /dev/devctl code */
733
734 TAILQ_HEAD(,device) bus_data_devices;
735 static int bus_data_generation = 1;
736
737 kobj_method_t null_methods[] = {
738 { 0, 0 }
739 };
740
741 DEFINE_CLASS(null, null_methods, 0);
742
743 /*
744 * Devclass implementation
745 */
746
747 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
748
749
750 /**
751 * @internal
752 * @brief Find or create a device class
753 *
754 * If a device class with the name @p classname exists, return it,
755 * otherwise if @p create is non-zero create and return a new device
756 * class.
757 *
758 * If @p parentname is non-NULL, the parent of the devclass is set to
759 * the devclass of that name.
760 *
761 * @param classname the devclass name to find or create
762 * @param parentname the parent devclass name or @c NULL
763 * @param create non-zero to create a devclass
764 */
765 static devclass_t
766 devclass_find_internal(const char *classname, const char *parentname,
767 int create)
768 {
769 devclass_t dc;
770
771 PDEBUG(("looking for %s", classname));
772 if (!classname)
773 return (NULL);
774
775 TAILQ_FOREACH(dc, &devclasses, link) {
776 if (!strcmp(dc->name, classname))
777 break;
778 }
779
780 if (create && !dc) {
781 PDEBUG(("creating %s", classname));
782 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
783 M_BUS, M_NOWAIT|M_ZERO);
784 if (!dc)
785 return (NULL);
786 dc->parent = NULL;
787 dc->name = (char*) (dc + 1);
788 strcpy(dc->name, classname);
789 TAILQ_INIT(&dc->drivers);
790 TAILQ_INSERT_TAIL(&devclasses, dc, link);
791
792 bus_data_generation_update();
793 }
794
795 /*
796 * If a parent class is specified, then set that as our parent so
797 * that this devclass will support drivers for the parent class as
798 * well. If the parent class has the same name don't do this though
799 * as it creates a cycle that can trigger an infinite loop in
800 * device_probe_child() if a device exists for which there is no
801 * suitable driver.
802 */
803 if (parentname && dc && !dc->parent &&
804 strcmp(classname, parentname) != 0) {
805 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
806 }
807
808 return (dc);
809 }
810
811 /**
812 * @brief Create a device class
813 *
814 * If a device class with the name @p classname exists, return it,
815 * otherwise create and return a new device class.
816 *
817 * @param classname the devclass name to find or create
818 */
819 devclass_t
820 devclass_create(const char *classname)
821 {
822 return (devclass_find_internal(classname, NULL, TRUE));
823 }
824
825 /**
826 * @brief Find a device class
827 *
828 * If a device class with the name @p classname exists, return it,
829 * otherwise return @c NULL.
830 *
831 * @param classname the devclass name to find
832 */
833 devclass_t
834 devclass_find(const char *classname)
835 {
836 return (devclass_find_internal(classname, NULL, FALSE));
837 }
838
839 /**
840 * @brief Add a device driver to a device class
841 *
842 * Add a device driver to a devclass. This is normally called
843 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
844 * all devices in the devclass will be called to allow them to attempt
845 * to re-probe any unmatched children.
846 *
847 * @param dc the devclass to edit
848 * @param driver the driver to register
849 */
850 int
851 devclass_add_driver(devclass_t dc, driver_t *driver)
852 {
853 driverlink_t dl;
854 int i;
855
856 PDEBUG(("%s", DRIVERNAME(driver)));
857
858 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
859 if (!dl)
860 return (ENOMEM);
861
862 /*
863 * Compile the driver's methods. Also increase the reference count
864 * so that the class doesn't get freed when the last instance
865 * goes. This means we can safely use static methods and avoids a
866 * double-free in devclass_delete_driver.
867 */
868 kobj_class_compile((kobj_class_t) driver);
869
870 /*
871 * Make sure the devclass which the driver is implementing exists.
872 */
873 devclass_find_internal(driver->name, NULL, TRUE);
874
875 dl->driver = driver;
876 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
877 driver->refs++; /* XXX: kobj_mtx */
878
879 /*
880 * Call BUS_DRIVER_ADDED for any existing busses in this class.
881 */
882 for (i = 0; i < dc->maxunit; i++)
883 if (dc->devices[i])
884 BUS_DRIVER_ADDED(dc->devices[i], driver);
885
886 bus_data_generation_update();
887 return (0);
888 }
889
890 /**
891 * @brief Delete a device driver from a device class
892 *
893 * Delete a device driver from a devclass. This is normally called
894 * automatically by DRIVER_MODULE().
895 *
896 * If the driver is currently attached to any devices,
897 * devclass_delete_driver() will first attempt to detach from each
898 * device. If one of the detach calls fails, the driver will not be
899 * deleted.
900 *
901 * @param dc the devclass to edit
902 * @param driver the driver to unregister
903 */
904 int
905 devclass_delete_driver(devclass_t busclass, driver_t *driver)
906 {
907 devclass_t dc = devclass_find(driver->name);
908 driverlink_t dl;
909 device_t dev;
910 int i;
911 int error;
912
913 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
914
915 if (!dc)
916 return (0);
917
918 /*
919 * Find the link structure in the bus' list of drivers.
920 */
921 TAILQ_FOREACH(dl, &busclass->drivers, link) {
922 if (dl->driver == driver)
923 break;
924 }
925
926 if (!dl) {
927 PDEBUG(("%s not found in %s list", driver->name,
928 busclass->name));
929 return (ENOENT);
930 }
931
932 /*
933 * Disassociate from any devices. We iterate through all the
934 * devices in the devclass of the driver and detach any which are
935 * using the driver and which have a parent in the devclass which
936 * we are deleting from.
937 *
938 * Note that since a driver can be in multiple devclasses, we
939 * should not detach devices which are not children of devices in
940 * the affected devclass.
941 */
942 for (i = 0; i < dc->maxunit; i++) {
943 if (dc->devices[i]) {
944 dev = dc->devices[i];
945 if (dev->driver == driver && dev->parent &&
946 dev->parent->devclass == busclass) {
947 if ((error = device_detach(dev)) != 0)
948 return (error);
949 device_set_driver(dev, NULL);
950 }
951 }
952 }
953
954 TAILQ_REMOVE(&busclass->drivers, dl, link);
955 free(dl, M_BUS);
956
957 /* XXX: kobj_mtx */
958 driver->refs--;
959 if (driver->refs == 0)
960 kobj_class_free((kobj_class_t) driver);
961
962 bus_data_generation_update();
963 return (0);
964 }
965
966 /**
967 * @brief Quiesces a set of device drivers from a device class
968 *
969 * Quiesce a device driver from a devclass. This is normally called
970 * automatically by DRIVER_MODULE().
971 *
972 * If the driver is currently attached to any devices,
973 * devclass_quiesece_driver() will first attempt to quiesce each
974 * device.
975 *
976 * @param dc the devclass to edit
977 * @param driver the driver to unregister
978 */
979 int
980 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
981 {
982 devclass_t dc = devclass_find(driver->name);
983 driverlink_t dl;
984 device_t dev;
985 int i;
986 int error;
987
988 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
989
990 if (!dc)
991 return (0);
992
993 /*
994 * Find the link structure in the bus' list of drivers.
995 */
996 TAILQ_FOREACH(dl, &busclass->drivers, link) {
997 if (dl->driver == driver)
998 break;
999 }
1000
1001 if (!dl) {
1002 PDEBUG(("%s not found in %s list", driver->name,
1003 busclass->name));
1004 return (ENOENT);
1005 }
1006
1007 /*
1008 * Quiesce all devices. We iterate through all the devices in
1009 * the devclass of the driver and quiesce any which are using
1010 * the driver and which have a parent in the devclass which we
1011 * are quiescing.
1012 *
1013 * Note that since a driver can be in multiple devclasses, we
1014 * should not quiesce devices which are not children of
1015 * devices in the affected devclass.
1016 */
1017 for (i = 0; i < dc->maxunit; i++) {
1018 if (dc->devices[i]) {
1019 dev = dc->devices[i];
1020 if (dev->driver == driver && dev->parent &&
1021 dev->parent->devclass == busclass) {
1022 if ((error = device_quiesce(dev)) != 0)
1023 return (error);
1024 }
1025 }
1026 }
1027
1028 return (0);
1029 }
1030
1031 /**
1032 * @internal
1033 */
1034 static driverlink_t
1035 devclass_find_driver_internal(devclass_t dc, const char *classname)
1036 {
1037 driverlink_t dl;
1038
1039 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1040
1041 TAILQ_FOREACH(dl, &dc->drivers, link) {
1042 if (!strcmp(dl->driver->name, classname))
1043 return (dl);
1044 }
1045
1046 PDEBUG(("not found"));
1047 return (NULL);
1048 }
1049
1050 /**
1051 * @brief Search a devclass for a driver
1052 *
1053 * This function searches the devclass's list of drivers and returns
1054 * the first driver whose name is @p classname or @c NULL if there is
1055 * no driver of that name.
1056 *
1057 * @param dc the devclass to search
1058 * @param classname the driver name to search for
1059 */
1060 kobj_class_t
1061 devclass_find_driver(devclass_t dc, const char *classname)
1062 {
1063 driverlink_t dl;
1064
1065 dl = devclass_find_driver_internal(dc, classname);
1066 if (dl)
1067 return (dl->driver);
1068 return (NULL);
1069 }
1070
1071 /**
1072 * @brief Return the name of the devclass
1073 */
1074 const char *
1075 devclass_get_name(devclass_t dc)
1076 {
1077 return (dc->name);
1078 }
1079
1080 /**
1081 * @brief Find a device given a unit number
1082 *
1083 * @param dc the devclass to search
1084 * @param unit the unit number to search for
1085 *
1086 * @returns the device with the given unit number or @c
1087 * NULL if there is no such device
1088 */
1089 device_t
1090 devclass_get_device(devclass_t dc, int unit)
1091 {
1092 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1093 return (NULL);
1094 return (dc->devices[unit]);
1095 }
1096
1097 /**
1098 * @brief Find the softc field of a device given a unit number
1099 *
1100 * @param dc the devclass to search
1101 * @param unit the unit number to search for
1102 *
1103 * @returns the softc field of the device with the given
1104 * unit number or @c NULL if there is no such
1105 * device
1106 */
1107 void *
1108 devclass_get_softc(devclass_t dc, int unit)
1109 {
1110 device_t dev;
1111
1112 dev = devclass_get_device(dc, unit);
1113 if (!dev)
1114 return (NULL);
1115
1116 return (device_get_softc(dev));
1117 }
1118
1119 /**
1120 * @brief Get a list of devices in the devclass
1121 *
1122 * An array containing a list of all the devices in the given devclass
1123 * is allocated and returned in @p *devlistp. The number of devices
1124 * in the array is returned in @p *devcountp. The caller should free
1125 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1126 *
1127 * @param dc the devclass to examine
1128 * @param devlistp points at location for array pointer return
1129 * value
1130 * @param devcountp points at location for array size return value
1131 *
1132 * @retval 0 success
1133 * @retval ENOMEM the array allocation failed
1134 */
1135 int
1136 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1137 {
1138 int count, i;
1139 device_t *list;
1140
1141 count = devclass_get_count(dc);
1142 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1143 if (!list)
1144 return (ENOMEM);
1145
1146 count = 0;
1147 for (i = 0; i < dc->maxunit; i++) {
1148 if (dc->devices[i]) {
1149 list[count] = dc->devices[i];
1150 count++;
1151 }
1152 }
1153
1154 *devlistp = list;
1155 *devcountp = count;
1156
1157 return (0);
1158 }
1159
1160 /**
1161 * @brief Get a list of drivers in the devclass
1162 *
1163 * An array containing a list of pointers to all the drivers in the
1164 * given devclass is allocated and returned in @p *listp. The number
1165 * of drivers in the array is returned in @p *countp. The caller should
1166 * free the array using @c free(p, M_TEMP).
1167 *
1168 * @param dc the devclass to examine
1169 * @param listp gives location for array pointer return value
1170 * @param countp gives location for number of array elements
1171 * return value
1172 *
1173 * @retval 0 success
1174 * @retval ENOMEM the array allocation failed
1175 */
1176 int
1177 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1178 {
1179 driverlink_t dl;
1180 driver_t **list;
1181 int count;
1182
1183 count = 0;
1184 TAILQ_FOREACH(dl, &dc->drivers, link)
1185 count++;
1186 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1187 if (list == NULL)
1188 return (ENOMEM);
1189
1190 count = 0;
1191 TAILQ_FOREACH(dl, &dc->drivers, link) {
1192 list[count] = dl->driver;
1193 count++;
1194 }
1195 *listp = list;
1196 *countp = count;
1197
1198 return (0);
1199 }
1200
1201 /**
1202 * @brief Get the number of devices in a devclass
1203 *
1204 * @param dc the devclass to examine
1205 */
1206 int
1207 devclass_get_count(devclass_t dc)
1208 {
1209 int count, i;
1210
1211 count = 0;
1212 for (i = 0; i < dc->maxunit; i++)
1213 if (dc->devices[i])
1214 count++;
1215 return (count);
1216 }
1217
1218 /**
1219 * @brief Get the maximum unit number used in a devclass
1220 *
1221 * Note that this is one greater than the highest currently-allocated
1222 * unit.
1223 *
1224 * @param dc the devclass to examine
1225 */
1226 int
1227 devclass_get_maxunit(devclass_t dc)
1228 {
1229 return (dc->maxunit);
1230 }
1231
1232 /**
1233 * @brief Find a free unit number in a devclass
1234 *
1235 * This function searches for the first unused unit number greater
1236 * that or equal to @p unit.
1237 *
1238 * @param dc the devclass to examine
1239 * @param unit the first unit number to check
1240 */
1241 int
1242 devclass_find_free_unit(devclass_t dc, int unit)
1243 {
1244 if (dc == NULL)
1245 return (unit);
1246 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1247 unit++;
1248 return (unit);
1249 }
1250
1251 /**
1252 * @brief Set the parent of a devclass
1253 *
1254 * The parent class is normally initialised automatically by
1255 * DRIVER_MODULE().
1256 *
1257 * @param dc the devclass to edit
1258 * @param pdc the new parent devclass
1259 */
1260 void
1261 devclass_set_parent(devclass_t dc, devclass_t pdc)
1262 {
1263 dc->parent = pdc;
1264 }
1265
1266 /**
1267 * @brief Get the parent of a devclass
1268 *
1269 * @param dc the devclass to examine
1270 */
1271 devclass_t
1272 devclass_get_parent(devclass_t dc)
1273 {
1274 return (dc->parent);
1275 }
1276
1277 struct sysctl_ctx_list *
1278 devclass_get_sysctl_ctx(devclass_t dc)
1279 {
1280 return (&dc->sysctl_ctx);
1281 }
1282
1283 struct sysctl_oid *
1284 devclass_get_sysctl_tree(devclass_t dc)
1285 {
1286 return (dc->sysctl_tree);
1287 }
1288
1289 /**
1290 * @internal
1291 * @brief Allocate a unit number
1292 *
1293 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1294 * will do). The allocated unit number is returned in @p *unitp.
1295
1296 * @param dc the devclass to allocate from
1297 * @param unitp points at the location for the allocated unit
1298 * number
1299 *
1300 * @retval 0 success
1301 * @retval EEXIST the requested unit number is already allocated
1302 * @retval ENOMEM memory allocation failure
1303 */
1304 static int
1305 devclass_alloc_unit(devclass_t dc, int *unitp)
1306 {
1307 int unit = *unitp;
1308
1309 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1310
1311 /* If we were given a wired unit number, check for existing device */
1312 /* XXX imp XXX */
1313 if (unit != -1) {
1314 if (unit >= 0 && unit < dc->maxunit &&
1315 dc->devices[unit] != NULL) {
1316 if (bootverbose)
1317 printf("%s: %s%d already exists; skipping it\n",
1318 dc->name, dc->name, *unitp);
1319 return (EEXIST);
1320 }
1321 } else {
1322 /* Unwired device, find the next available slot for it */
1323 unit = 0;
1324 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1325 unit++;
1326 }
1327
1328 /*
1329 * We've selected a unit beyond the length of the table, so let's
1330 * extend the table to make room for all units up to and including
1331 * this one.
1332 */
1333 if (unit >= dc->maxunit) {
1334 device_t *newlist;
1335 int newsize;
1336
1337 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1338 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1339 if (!newlist)
1340 return (ENOMEM);
1341 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1342 bzero(newlist + dc->maxunit,
1343 sizeof(device_t) * (newsize - dc->maxunit));
1344 if (dc->devices)
1345 free(dc->devices, M_BUS);
1346 dc->devices = newlist;
1347 dc->maxunit = newsize;
1348 }
1349 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1350
1351 *unitp = unit;
1352 return (0);
1353 }
1354
1355 /**
1356 * @internal
1357 * @brief Add a device to a devclass
1358 *
1359 * A unit number is allocated for the device (using the device's
1360 * preferred unit number if any) and the device is registered in the
1361 * devclass. This allows the device to be looked up by its unit
1362 * number, e.g. by decoding a dev_t minor number.
1363 *
1364 * @param dc the devclass to add to
1365 * @param dev the device to add
1366 *
1367 * @retval 0 success
1368 * @retval EEXIST the requested unit number is already allocated
1369 * @retval ENOMEM memory allocation failure
1370 */
1371 static int
1372 devclass_add_device(devclass_t dc, device_t dev)
1373 {
1374 int buflen, error;
1375
1376 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1377
1378 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit);
1379 if (buflen < 0)
1380 return (ENOMEM);
1381 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1382 if (!dev->nameunit)
1383 return (ENOMEM);
1384
1385 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1386 free(dev->nameunit, M_BUS);
1387 dev->nameunit = NULL;
1388 return (error);
1389 }
1390 dc->devices[dev->unit] = dev;
1391 dev->devclass = dc;
1392 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1393
1394 return (0);
1395 }
1396
1397 /**
1398 * @internal
1399 * @brief Delete a device from a devclass
1400 *
1401 * The device is removed from the devclass's device list and its unit
1402 * number is freed.
1403
1404 * @param dc the devclass to delete from
1405 * @param dev the device to delete
1406 *
1407 * @retval 0 success
1408 */
1409 static int
1410 devclass_delete_device(devclass_t dc, device_t dev)
1411 {
1412 if (!dc || !dev)
1413 return (0);
1414
1415 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1416
1417 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1418 panic("devclass_delete_device: inconsistent device class");
1419 dc->devices[dev->unit] = NULL;
1420 if (dev->flags & DF_WILDCARD)
1421 dev->unit = -1;
1422 dev->devclass = NULL;
1423 free(dev->nameunit, M_BUS);
1424 dev->nameunit = NULL;
1425
1426 return (0);
1427 }
1428
1429 /**
1430 * @internal
1431 * @brief Make a new device and add it as a child of @p parent
1432 *
1433 * @param parent the parent of the new device
1434 * @param name the devclass name of the new device or @c NULL
1435 * to leave the devclass unspecified
1436 * @parem unit the unit number of the new device of @c -1 to
1437 * leave the unit number unspecified
1438 *
1439 * @returns the new device
1440 */
1441 static device_t
1442 make_device(device_t parent, const char *name, int unit)
1443 {
1444 device_t dev;
1445 devclass_t dc;
1446
1447 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1448
1449 if (name) {
1450 dc = devclass_find_internal(name, NULL, TRUE);
1451 if (!dc) {
1452 printf("make_device: can't find device class %s\n",
1453 name);
1454 return (NULL);
1455 }
1456 } else {
1457 dc = NULL;
1458 }
1459
1460 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1461 if (!dev)
1462 return (NULL);
1463
1464 dev->parent = parent;
1465 TAILQ_INIT(&dev->children);
1466 kobj_init((kobj_t) dev, &null_class);
1467 dev->driver = NULL;
1468 dev->devclass = NULL;
1469 dev->unit = unit;
1470 dev->nameunit = NULL;
1471 dev->desc = NULL;
1472 dev->busy = 0;
1473 dev->devflags = 0;
1474 dev->flags = DF_ENABLED;
1475 dev->order = 0;
1476 if (unit == -1)
1477 dev->flags |= DF_WILDCARD;
1478 if (name) {
1479 dev->flags |= DF_FIXEDCLASS;
1480 if (devclass_add_device(dc, dev)) {
1481 kobj_delete((kobj_t) dev, M_BUS);
1482 return (NULL);
1483 }
1484 }
1485 dev->ivars = NULL;
1486 dev->softc = NULL;
1487
1488 dev->state = DS_NOTPRESENT;
1489
1490 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1491 bus_data_generation_update();
1492
1493 return (dev);
1494 }
1495
1496 /**
1497 * @internal
1498 * @brief Print a description of a device.
1499 */
1500 static int
1501 device_print_child(device_t dev, device_t child)
1502 {
1503 int retval = 0;
1504
1505 if (device_is_alive(child))
1506 retval += BUS_PRINT_CHILD(dev, child);
1507 else
1508 retval += device_printf(child, " not found\n");
1509
1510 return (retval);
1511 }
1512
1513 /**
1514 * @brief Create a new device
1515 *
1516 * This creates a new device and adds it as a child of an existing
1517 * parent device. The new device will be added after the last existing
1518 * child with order zero.
1519 *
1520 * @param dev the device which will be the parent of the
1521 * new child device
1522 * @param name devclass name for new device or @c NULL if not
1523 * specified
1524 * @param unit unit number for new device or @c -1 if not
1525 * specified
1526 *
1527 * @returns the new device
1528 */
1529 device_t
1530 device_add_child(device_t dev, const char *name, int unit)
1531 {
1532 return (device_add_child_ordered(dev, 0, name, unit));
1533 }
1534
1535 /**
1536 * @brief Create a new device
1537 *
1538 * This creates a new device and adds it as a child of an existing
1539 * parent device. The new device will be added after the last existing
1540 * child with the same order.
1541 *
1542 * @param dev the device which will be the parent of the
1543 * new child device
1544 * @param order a value which is used to partially sort the
1545 * children of @p dev - devices created using
1546 * lower values of @p order appear first in @p
1547 * dev's list of children
1548 * @param name devclass name for new device or @c NULL if not
1549 * specified
1550 * @param unit unit number for new device or @c -1 if not
1551 * specified
1552 *
1553 * @returns the new device
1554 */
1555 device_t
1556 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1557 {
1558 device_t child;
1559 device_t place;
1560
1561 PDEBUG(("%s at %s with order %d as unit %d",
1562 name, DEVICENAME(dev), order, unit));
1563
1564 child = make_device(dev, name, unit);
1565 if (child == NULL)
1566 return (child);
1567 child->order = order;
1568
1569 TAILQ_FOREACH(place, &dev->children, link) {
1570 if (place->order > order)
1571 break;
1572 }
1573
1574 if (place) {
1575 /*
1576 * The device 'place' is the first device whose order is
1577 * greater than the new child.
1578 */
1579 TAILQ_INSERT_BEFORE(place, child, link);
1580 } else {
1581 /*
1582 * The new child's order is greater or equal to the order of
1583 * any existing device. Add the child to the tail of the list.
1584 */
1585 TAILQ_INSERT_TAIL(&dev->children, child, link);
1586 }
1587
1588 bus_data_generation_update();
1589 return (child);
1590 }
1591
1592 /**
1593 * @brief Delete a device
1594 *
1595 * This function deletes a device along with all of its children. If
1596 * the device currently has a driver attached to it, the device is
1597 * detached first using device_detach().
1598 *
1599 * @param dev the parent device
1600 * @param child the device to delete
1601 *
1602 * @retval 0 success
1603 * @retval non-zero a unit error code describing the error
1604 */
1605 int
1606 device_delete_child(device_t dev, device_t child)
1607 {
1608 int error;
1609 device_t grandchild;
1610
1611 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1612
1613 /* remove children first */
1614 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1615 error = device_delete_child(child, grandchild);
1616 if (error)
1617 return (error);
1618 }
1619
1620 if ((error = device_detach(child)) != 0)
1621 return (error);
1622 if (child->devclass)
1623 devclass_delete_device(child->devclass, child);
1624 TAILQ_REMOVE(&dev->children, child, link);
1625 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1626 kobj_delete((kobj_t) child, M_BUS);
1627
1628 bus_data_generation_update();
1629 return (0);
1630 }
1631
1632 /**
1633 * @brief Find a device given a unit number
1634 *
1635 * This is similar to devclass_get_devices() but only searches for
1636 * devices which have @p dev as a parent.
1637 *
1638 * @param dev the parent device to search
1639 * @param unit the unit number to search for. If the unit is -1,
1640 * return the first child of @p dev which has name
1641 * @p classname (that is, the one with the lowest unit.)
1642 *
1643 * @returns the device with the given unit number or @c
1644 * NULL if there is no such device
1645 */
1646 device_t
1647 device_find_child(device_t dev, const char *classname, int unit)
1648 {
1649 devclass_t dc;
1650 device_t child;
1651
1652 dc = devclass_find(classname);
1653 if (!dc)
1654 return (NULL);
1655
1656 if (unit != -1) {
1657 child = devclass_get_device(dc, unit);
1658 if (child && child->parent == dev)
1659 return (child);
1660 } else {
1661 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1662 child = devclass_get_device(dc, unit);
1663 if (child && child->parent == dev)
1664 return (child);
1665 }
1666 }
1667 return (NULL);
1668 }
1669
1670 /**
1671 * @internal
1672 */
1673 static driverlink_t
1674 first_matching_driver(devclass_t dc, device_t dev)
1675 {
1676 if (dev->devclass)
1677 return (devclass_find_driver_internal(dc, dev->devclass->name));
1678 return (TAILQ_FIRST(&dc->drivers));
1679 }
1680
1681 /**
1682 * @internal
1683 */
1684 static driverlink_t
1685 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1686 {
1687 if (dev->devclass) {
1688 driverlink_t dl;
1689 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1690 if (!strcmp(dev->devclass->name, dl->driver->name))
1691 return (dl);
1692 return (NULL);
1693 }
1694 return (TAILQ_NEXT(last, link));
1695 }
1696
1697 /**
1698 * @internal
1699 */
1700 int
1701 device_probe_child(device_t dev, device_t child)
1702 {
1703 devclass_t dc;
1704 driverlink_t best = NULL;
1705 driverlink_t dl;
1706 int result, pri = 0;
1707 int hasclass = (child->devclass != 0);
1708
1709 GIANT_REQUIRED;
1710
1711 dc = dev->devclass;
1712 if (!dc)
1713 panic("device_probe_child: parent device has no devclass");
1714
1715 /*
1716 * If the state is already probed, then return. However, don't
1717 * return if we can rebid this object.
1718 */
1719 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
1720 return (0);
1721
1722 for (; dc; dc = dc->parent) {
1723 for (dl = first_matching_driver(dc, child);
1724 dl;
1725 dl = next_matching_driver(dc, child, dl)) {
1726 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1727 device_set_driver(child, dl->driver);
1728 if (!hasclass)
1729 device_set_devclass(child, dl->driver->name);
1730
1731 /* Fetch any flags for the device before probing. */
1732 resource_int_value(dl->driver->name, child->unit,
1733 "flags", &child->devflags);
1734
1735 result = DEVICE_PROBE(child);
1736
1737 /* Reset flags and devclass before the next probe. */
1738 child->devflags = 0;
1739 if (!hasclass)
1740 device_set_devclass(child, NULL);
1741
1742 /*
1743 * If the driver returns SUCCESS, there can be
1744 * no higher match for this device.
1745 */
1746 if (result == 0) {
1747 best = dl;
1748 pri = 0;
1749 break;
1750 }
1751
1752 /*
1753 * The driver returned an error so it
1754 * certainly doesn't match.
1755 */
1756 if (result > 0) {
1757 device_set_driver(child, NULL);
1758 continue;
1759 }
1760
1761 /*
1762 * A priority lower than SUCCESS, remember the
1763 * best matching driver. Initialise the value
1764 * of pri for the first match.
1765 */
1766 if (best == NULL || result > pri) {
1767 best = dl;
1768 pri = result;
1769 continue;
1770 }
1771 }
1772 /*
1773 * If we have an unambiguous match in this devclass,
1774 * don't look in the parent.
1775 */
1776 if (best && pri == 0)
1777 break;
1778 }
1779
1780 /*
1781 * If we found a driver, change state and initialise the devclass.
1782 */
1783 /* XXX What happens if we rebid and got no best? */
1784 if (best) {
1785 /*
1786 * If this device was atached, and we were asked to
1787 * rescan, and it is a different driver, then we have
1788 * to detach the old driver and reattach this new one.
1789 * Note, we don't have to check for DF_REBID here
1790 * because if the state is > DS_ALIVE, we know it must
1791 * be.
1792 *
1793 * This assumes that all DF_REBID drivers can have
1794 * their probe routine called at any time and that
1795 * they are idempotent as well as completely benign in
1796 * normal operations.
1797 *
1798 * We also have to make sure that the detach
1799 * succeeded, otherwise we fail the operation (or
1800 * maybe it should just fail silently? I'm torn).
1801 */
1802 if (child->state > DS_ALIVE && best->driver != child->driver)
1803 if ((result = device_detach(dev)) != 0)
1804 return (result);
1805
1806 /* Set the winning driver, devclass, and flags. */
1807 if (!child->devclass)
1808 device_set_devclass(child, best->driver->name);
1809 device_set_driver(child, best->driver);
1810 resource_int_value(best->driver->name, child->unit,
1811 "flags", &child->devflags);
1812
1813 if (pri < 0) {
1814 /*
1815 * A bit bogus. Call the probe method again to make
1816 * sure that we have the right description.
1817 */
1818 DEVICE_PROBE(child);
1819 #if 0
1820 child->flags |= DF_REBID;
1821 #endif
1822 } else
1823 child->flags &= ~DF_REBID;
1824 child->state = DS_ALIVE;
1825
1826 bus_data_generation_update();
1827 return (0);
1828 }
1829
1830 return (ENXIO);
1831 }
1832
1833 /**
1834 * @brief Return the parent of a device
1835 */
1836 device_t
1837 device_get_parent(device_t dev)
1838 {
1839 return (dev->parent);
1840 }
1841
1842 /**
1843 * @brief Get a list of children of a device
1844 *
1845 * An array containing a list of all the children of the given device
1846 * is allocated and returned in @p *devlistp. The number of devices
1847 * in the array is returned in @p *devcountp. The caller should free
1848 * the array using @c free(p, M_TEMP).
1849 *
1850 * @param dev the device to examine
1851 * @param devlistp points at location for array pointer return
1852 * value
1853 * @param devcountp points at location for array size return value
1854 *
1855 * @retval 0 success
1856 * @retval ENOMEM the array allocation failed
1857 */
1858 int
1859 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1860 {
1861 int count;
1862 device_t child;
1863 device_t *list;
1864
1865 count = 0;
1866 TAILQ_FOREACH(child, &dev->children, link) {
1867 count++;
1868 }
1869
1870 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1871 if (!list)
1872 return (ENOMEM);
1873
1874 count = 0;
1875 TAILQ_FOREACH(child, &dev->children, link) {
1876 list[count] = child;
1877 count++;
1878 }
1879
1880 *devlistp = list;
1881 *devcountp = count;
1882
1883 return (0);
1884 }
1885
1886 /**
1887 * @brief Return the current driver for the device or @c NULL if there
1888 * is no driver currently attached
1889 */
1890 driver_t *
1891 device_get_driver(device_t dev)
1892 {
1893 return (dev->driver);
1894 }
1895
1896 /**
1897 * @brief Return the current devclass for the device or @c NULL if
1898 * there is none.
1899 */
1900 devclass_t
1901 device_get_devclass(device_t dev)
1902 {
1903 return (dev->devclass);
1904 }
1905
1906 /**
1907 * @brief Return the name of the device's devclass or @c NULL if there
1908 * is none.
1909 */
1910 const char *
1911 device_get_name(device_t dev)
1912 {
1913 if (dev != NULL && dev->devclass)
1914 return (devclass_get_name(dev->devclass));
1915 return (NULL);
1916 }
1917
1918 /**
1919 * @brief Return a string containing the device's devclass name
1920 * followed by an ascii representation of the device's unit number
1921 * (e.g. @c "foo2").
1922 */
1923 const char *
1924 device_get_nameunit(device_t dev)
1925 {
1926 return (dev->nameunit);
1927 }
1928
1929 /**
1930 * @brief Return the device's unit number.
1931 */
1932 int
1933 device_get_unit(device_t dev)
1934 {
1935 return (dev->unit);
1936 }
1937
1938 /**
1939 * @brief Return the device's description string
1940 */
1941 const char *
1942 device_get_desc(device_t dev)
1943 {
1944 return (dev->desc);
1945 }
1946
1947 /**
1948 * @brief Return the device's flags
1949 */
1950 u_int32_t
1951 device_get_flags(device_t dev)
1952 {
1953 return (dev->devflags);
1954 }
1955
1956 struct sysctl_ctx_list *
1957 device_get_sysctl_ctx(device_t dev)
1958 {
1959 return (&dev->sysctl_ctx);
1960 }
1961
1962 struct sysctl_oid *
1963 device_get_sysctl_tree(device_t dev)
1964 {
1965 return (dev->sysctl_tree);
1966 }
1967
1968 /**
1969 * @brief Print the name of the device followed by a colon and a space
1970 *
1971 * @returns the number of characters printed
1972 */
1973 int
1974 device_print_prettyname(device_t dev)
1975 {
1976 const char *name = device_get_name(dev);
1977
1978 if (name == 0)
1979 return (printf("unknown: "));
1980 return (printf("%s%d: ", name, device_get_unit(dev)));
1981 }
1982
1983 /**
1984 * @brief Print the name of the device followed by a colon, a space
1985 * and the result of calling vprintf() with the value of @p fmt and
1986 * the following arguments.
1987 *
1988 * @returns the number of characters printed
1989 */
1990 int
1991 device_printf(device_t dev, const char * fmt, ...)
1992 {
1993 va_list ap;
1994 int retval;
1995
1996 retval = device_print_prettyname(dev);
1997 va_start(ap, fmt);
1998 retval += vprintf(fmt, ap);
1999 va_end(ap);
2000 return (retval);
2001 }
2002
2003 /**
2004 * @internal
2005 */
2006 static void
2007 device_set_desc_internal(device_t dev, const char* desc, int copy)
2008 {
2009 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2010 free(dev->desc, M_BUS);
2011 dev->flags &= ~DF_DESCMALLOCED;
2012 dev->desc = NULL;
2013 }
2014
2015 if (copy && desc) {
2016 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2017 if (dev->desc) {
2018 strcpy(dev->desc, desc);
2019 dev->flags |= DF_DESCMALLOCED;
2020 }
2021 } else {
2022 /* Avoid a -Wcast-qual warning */
2023 dev->desc = (char *)(uintptr_t) desc;
2024 }
2025
2026 bus_data_generation_update();
2027 }
2028
2029 /**
2030 * @brief Set the device's description
2031 *
2032 * The value of @c desc should be a string constant that will not
2033 * change (at least until the description is changed in a subsequent
2034 * call to device_set_desc() or device_set_desc_copy()).
2035 */
2036 void
2037 device_set_desc(device_t dev, const char* desc)
2038 {
2039 device_set_desc_internal(dev, desc, FALSE);
2040 }
2041
2042 /**
2043 * @brief Set the device's description
2044 *
2045 * The string pointed to by @c desc is copied. Use this function if
2046 * the device description is generated, (e.g. with sprintf()).
2047 */
2048 void
2049 device_set_desc_copy(device_t dev, const char* desc)
2050 {
2051 device_set_desc_internal(dev, desc, TRUE);
2052 }
2053
2054 /**
2055 * @brief Set the device's flags
2056 */
2057 void
2058 device_set_flags(device_t dev, u_int32_t flags)
2059 {
2060 dev->devflags = flags;
2061 }
2062
2063 /**
2064 * @brief Return the device's softc field
2065 *
2066 * The softc is allocated and zeroed when a driver is attached, based
2067 * on the size field of the driver.
2068 */
2069 void *
2070 device_get_softc(device_t dev)
2071 {
2072 return (dev->softc);
2073 }
2074
2075 /**
2076 * @brief Set the device's softc field
2077 *
2078 * Most drivers do not need to use this since the softc is allocated
2079 * automatically when the driver is attached.
2080 */
2081 void
2082 device_set_softc(device_t dev, void *softc)
2083 {
2084 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2085 free(dev->softc, M_BUS_SC);
2086 dev->softc = softc;
2087 if (dev->softc)
2088 dev->flags |= DF_EXTERNALSOFTC;
2089 else
2090 dev->flags &= ~DF_EXTERNALSOFTC;
2091 }
2092
2093 /**
2094 * @brief Get the device's ivars field
2095 *
2096 * The ivars field is used by the parent device to store per-device
2097 * state (e.g. the physical location of the device or a list of
2098 * resources).
2099 */
2100 void *
2101 device_get_ivars(device_t dev)
2102 {
2103
2104 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2105 return (dev->ivars);
2106 }
2107
2108 /**
2109 * @brief Set the device's ivars field
2110 */
2111 void
2112 device_set_ivars(device_t dev, void * ivars)
2113 {
2114
2115 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2116 dev->ivars = ivars;
2117 }
2118
2119 /**
2120 * @brief Return the device's state
2121 */
2122 device_state_t
2123 device_get_state(device_t dev)
2124 {
2125 return (dev->state);
2126 }
2127
2128 /**
2129 * @brief Set the DF_ENABLED flag for the device
2130 */
2131 void
2132 device_enable(device_t dev)
2133 {
2134 dev->flags |= DF_ENABLED;
2135 }
2136
2137 /**
2138 * @brief Clear the DF_ENABLED flag for the device
2139 */
2140 void
2141 device_disable(device_t dev)
2142 {
2143 dev->flags &= ~DF_ENABLED;
2144 }
2145
2146 /**
2147 * @brief Increment the busy counter for the device
2148 */
2149 void
2150 device_busy(device_t dev)
2151 {
2152 if (dev->state < DS_ATTACHED)
2153 panic("device_busy: called for unattached device");
2154 if (dev->busy == 0 && dev->parent)
2155 device_busy(dev->parent);
2156 dev->busy++;
2157 dev->state = DS_BUSY;
2158 }
2159
2160 /**
2161 * @brief Decrement the busy counter for the device
2162 */
2163 void
2164 device_unbusy(device_t dev)
2165 {
2166 if (dev->state != DS_BUSY)
2167 panic("device_unbusy: called for non-busy device %s",
2168 device_get_nameunit(dev));
2169 dev->busy--;
2170 if (dev->busy == 0) {
2171 if (dev->parent)
2172 device_unbusy(dev->parent);
2173 dev->state = DS_ATTACHED;
2174 }
2175 }
2176
2177 /**
2178 * @brief Set the DF_QUIET flag for the device
2179 */
2180 void
2181 device_quiet(device_t dev)
2182 {
2183 dev->flags |= DF_QUIET;
2184 }
2185
2186 /**
2187 * @brief Clear the DF_QUIET flag for the device
2188 */
2189 void
2190 device_verbose(device_t dev)
2191 {
2192 dev->flags &= ~DF_QUIET;
2193 }
2194
2195 /**
2196 * @brief Return non-zero if the DF_QUIET flag is set on the device
2197 */
2198 int
2199 device_is_quiet(device_t dev)
2200 {
2201 return ((dev->flags & DF_QUIET) != 0);
2202 }
2203
2204 /**
2205 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2206 */
2207 int
2208 device_is_enabled(device_t dev)
2209 {
2210 return ((dev->flags & DF_ENABLED) != 0);
2211 }
2212
2213 /**
2214 * @brief Return non-zero if the device was successfully probed
2215 */
2216 int
2217 device_is_alive(device_t dev)
2218 {
2219 return (dev->state >= DS_ALIVE);
2220 }
2221
2222 /**
2223 * @brief Return non-zero if the device currently has a driver
2224 * attached to it
2225 */
2226 int
2227 device_is_attached(device_t dev)
2228 {
2229 return (dev->state >= DS_ATTACHED);
2230 }
2231
2232 /**
2233 * @brief Set the devclass of a device
2234 * @see devclass_add_device().
2235 */
2236 int
2237 device_set_devclass(device_t dev, const char *classname)
2238 {
2239 devclass_t dc;
2240 int error;
2241
2242 if (!classname) {
2243 if (dev->devclass)
2244 devclass_delete_device(dev->devclass, dev);
2245 return (0);
2246 }
2247
2248 if (dev->devclass) {
2249 printf("device_set_devclass: device class already set\n");
2250 return (EINVAL);
2251 }
2252
2253 dc = devclass_find_internal(classname, NULL, TRUE);
2254 if (!dc)
2255 return (ENOMEM);
2256
2257 error = devclass_add_device(dc, dev);
2258
2259 bus_data_generation_update();
2260 return (error);
2261 }
2262
2263 /**
2264 * @brief Set the driver of a device
2265 *
2266 * @retval 0 success
2267 * @retval EBUSY the device already has a driver attached
2268 * @retval ENOMEM a memory allocation failure occurred
2269 */
2270 int
2271 device_set_driver(device_t dev, driver_t *driver)
2272 {
2273 if (dev->state >= DS_ATTACHED)
2274 return (EBUSY);
2275
2276 if (dev->driver == driver)
2277 return (0);
2278
2279 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2280 free(dev->softc, M_BUS_SC);
2281 dev->softc = NULL;
2282 }
2283 kobj_delete((kobj_t) dev, NULL);
2284 dev->driver = driver;
2285 if (driver) {
2286 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2287 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2288 dev->softc = malloc(driver->size, M_BUS_SC,
2289 M_NOWAIT | M_ZERO);
2290 if (!dev->softc) {
2291 kobj_delete((kobj_t) dev, NULL);
2292 kobj_init((kobj_t) dev, &null_class);
2293 dev->driver = NULL;
2294 return (ENOMEM);
2295 }
2296 }
2297 } else {
2298 kobj_init((kobj_t) dev, &null_class);
2299 }
2300
2301 bus_data_generation_update();
2302 return (0);
2303 }
2304
2305 /**
2306 * @brief Probe a device and attach a driver if possible
2307 *
2308 * This function is the core of the device autoconfiguration
2309 * system. Its purpose is to select a suitable driver for a device and
2310 * then call that driver to initialise the hardware appropriately. The
2311 * driver is selected by calling the DEVICE_PROBE() method of a set of
2312 * candidate drivers and then choosing the driver which returned the
2313 * best value. This driver is then attached to the device using
2314 * device_attach().
2315 *
2316 * The set of suitable drivers is taken from the list of drivers in
2317 * the parent device's devclass. If the device was originally created
2318 * with a specific class name (see device_add_child()), only drivers
2319 * with that name are probed, otherwise all drivers in the devclass
2320 * are probed. If no drivers return successful probe values in the
2321 * parent devclass, the search continues in the parent of that
2322 * devclass (see devclass_get_parent()) if any.
2323 *
2324 * @param dev the device to initialise
2325 *
2326 * @retval 0 success
2327 * @retval ENXIO no driver was found
2328 * @retval ENOMEM memory allocation failure
2329 * @retval non-zero some other unix error code
2330 */
2331 int
2332 device_probe_and_attach(device_t dev)
2333 {
2334 int error;
2335
2336 GIANT_REQUIRED;
2337
2338 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2339 return (0);
2340
2341 if (!(dev->flags & DF_ENABLED)) {
2342 if (bootverbose && device_get_name(dev) != NULL) {
2343 device_print_prettyname(dev);
2344 printf("not probed (disabled)\n");
2345 }
2346 return (0);
2347 }
2348 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2349 if (!(dev->flags & DF_DONENOMATCH)) {
2350 BUS_PROBE_NOMATCH(dev->parent, dev);
2351 devnomatch(dev);
2352 dev->flags |= DF_DONENOMATCH;
2353 }
2354 return (error);
2355 }
2356 error = device_attach(dev);
2357
2358 return (error);
2359 }
2360
2361 /**
2362 * @brief Attach a device driver to a device
2363 *
2364 * This function is a wrapper around the DEVICE_ATTACH() driver
2365 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2366 * device's sysctl tree, optionally prints a description of the device
2367 * and queues a notification event for user-based device management
2368 * services.
2369 *
2370 * Normally this function is only called internally from
2371 * device_probe_and_attach().
2372 *
2373 * @param dev the device to initialise
2374 *
2375 * @retval 0 success
2376 * @retval ENXIO no driver was found
2377 * @retval ENOMEM memory allocation failure
2378 * @retval non-zero some other unix error code
2379 */
2380 int
2381 device_attach(device_t dev)
2382 {
2383 int error;
2384
2385 device_sysctl_init(dev);
2386 if (!device_is_quiet(dev))
2387 device_print_child(dev->parent, dev);
2388 if ((error = DEVICE_ATTACH(dev)) != 0) {
2389 printf("device_attach: %s%d attach returned %d\n",
2390 dev->driver->name, dev->unit, error);
2391 /* Unset the class; set in device_probe_child */
2392 if (dev->devclass == NULL)
2393 device_set_devclass(dev, NULL);
2394 device_set_driver(dev, NULL);
2395 device_sysctl_fini(dev);
2396 dev->state = DS_NOTPRESENT;
2397 return (error);
2398 }
2399 dev->state = DS_ATTACHED;
2400 devadded(dev);
2401 return (0);
2402 }
2403
2404 /**
2405 * @brief Detach a driver from a device
2406 *
2407 * This function is a wrapper around the DEVICE_DETACH() driver
2408 * method. If the call to DEVICE_DETACH() succeeds, it calls
2409 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2410 * notification event for user-based device management services and
2411 * cleans up the device's sysctl tree.
2412 *
2413 * @param dev the device to un-initialise
2414 *
2415 * @retval 0 success
2416 * @retval ENXIO no driver was found
2417 * @retval ENOMEM memory allocation failure
2418 * @retval non-zero some other unix error code
2419 */
2420 int
2421 device_detach(device_t dev)
2422 {
2423 int error;
2424
2425 GIANT_REQUIRED;
2426
2427 PDEBUG(("%s", DEVICENAME(dev)));
2428 if (dev->state == DS_BUSY)
2429 return (EBUSY);
2430 if (dev->state != DS_ATTACHED)
2431 return (0);
2432
2433 if ((error = DEVICE_DETACH(dev)) != 0)
2434 return (error);
2435 devremoved(dev);
2436 device_printf(dev, "detached\n");
2437 if (dev->parent)
2438 BUS_CHILD_DETACHED(dev->parent, dev);
2439
2440 if (!(dev->flags & DF_FIXEDCLASS))
2441 devclass_delete_device(dev->devclass, dev);
2442
2443 dev->state = DS_NOTPRESENT;
2444 device_set_driver(dev, NULL);
2445 device_set_desc(dev, NULL);
2446 device_sysctl_fini(dev);
2447
2448 return (0);
2449 }
2450
2451 /**
2452 * @brief Tells a driver to quiesce itself.
2453 *
2454 * This function is a wrapper around the DEVICE_QUIESCE() driver
2455 * method. If the call to DEVICE_QUIESCE() succeeds.
2456 *
2457 * @param dev the device to quiesce
2458 *
2459 * @retval 0 success
2460 * @retval ENXIO no driver was found
2461 * @retval ENOMEM memory allocation failure
2462 * @retval non-zero some other unix error code
2463 */
2464 int
2465 device_quiesce(device_t dev)
2466 {
2467
2468 PDEBUG(("%s", DEVICENAME(dev)));
2469 if (dev->state == DS_BUSY)
2470 return (EBUSY);
2471 if (dev->state != DS_ATTACHED)
2472 return (0);
2473
2474 return (DEVICE_QUIESCE(dev));
2475 }
2476
2477 /**
2478 * @brief Notify a device of system shutdown
2479 *
2480 * This function calls the DEVICE_SHUTDOWN() driver method if the
2481 * device currently has an attached driver.
2482 *
2483 * @returns the value returned by DEVICE_SHUTDOWN()
2484 */
2485 int
2486 device_shutdown(device_t dev)
2487 {
2488 if (dev->state < DS_ATTACHED)
2489 return (0);
2490 return (DEVICE_SHUTDOWN(dev));
2491 }
2492
2493 /**
2494 * @brief Set the unit number of a device
2495 *
2496 * This function can be used to override the unit number used for a
2497 * device (e.g. to wire a device to a pre-configured unit number).
2498 */
2499 int
2500 device_set_unit(device_t dev, int unit)
2501 {
2502 devclass_t dc;
2503 int err;
2504
2505 dc = device_get_devclass(dev);
2506 if (unit < dc->maxunit && dc->devices[unit])
2507 return (EBUSY);
2508 err = devclass_delete_device(dc, dev);
2509 if (err)
2510 return (err);
2511 dev->unit = unit;
2512 err = devclass_add_device(dc, dev);
2513 if (err)
2514 return (err);
2515
2516 bus_data_generation_update();
2517 return (0);
2518 }
2519
2520 /*======================================*/
2521 /*
2522 * Some useful method implementations to make life easier for bus drivers.
2523 */
2524
2525 /**
2526 * @brief Initialise a resource list.
2527 *
2528 * @param rl the resource list to initialise
2529 */
2530 void
2531 resource_list_init(struct resource_list *rl)
2532 {
2533 STAILQ_INIT(rl);
2534 }
2535
2536 /**
2537 * @brief Reclaim memory used by a resource list.
2538 *
2539 * This function frees the memory for all resource entries on the list
2540 * (if any).
2541 *
2542 * @param rl the resource list to free
2543 */
2544 void
2545 resource_list_free(struct resource_list *rl)
2546 {
2547 struct resource_list_entry *rle;
2548
2549 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2550 if (rle->res)
2551 panic("resource_list_free: resource entry is busy");
2552 STAILQ_REMOVE_HEAD(rl, link);
2553 free(rle, M_BUS);
2554 }
2555 }
2556
2557 /**
2558 * @brief Add a resource entry.
2559 *
2560 * This function adds a resource entry using the given @p type, @p
2561 * start, @p end and @p count values. A rid value is chosen by
2562 * searching sequentially for the first unused rid starting at zero.
2563 *
2564 * @param rl the resource list to edit
2565 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2566 * @param start the start address of the resource
2567 * @param end the end address of the resource
2568 * @param count XXX end-start+1
2569 */
2570 int
2571 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2572 u_long end, u_long count)
2573 {
2574 int rid;
2575
2576 rid = 0;
2577 while (resource_list_find(rl, type, rid) != NULL)
2578 rid++;
2579 resource_list_add(rl, type, rid, start, end, count);
2580 return (rid);
2581 }
2582
2583 /**
2584 * @brief Add or modify a resource entry.
2585 *
2586 * If an existing entry exists with the same type and rid, it will be
2587 * modified using the given values of @p start, @p end and @p
2588 * count. If no entry exists, a new one will be created using the
2589 * given values. The resource list entry that matches is then returned.
2590 *
2591 * @param rl the resource list to edit
2592 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2593 * @param rid the resource identifier
2594 * @param start the start address of the resource
2595 * @param end the end address of the resource
2596 * @param count XXX end-start+1
2597 */
2598 struct resource_list_entry *
2599 resource_list_add(struct resource_list *rl, int type, int rid,
2600 u_long start, u_long end, u_long count)
2601 {
2602 struct resource_list_entry *rle;
2603
2604 rle = resource_list_find(rl, type, rid);
2605 if (!rle) {
2606 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2607 M_NOWAIT);
2608 if (!rle)
2609 panic("resource_list_add: can't record entry");
2610 STAILQ_INSERT_TAIL(rl, rle, link);
2611 rle->type = type;
2612 rle->rid = rid;
2613 rle->res = NULL;
2614 }
2615
2616 if (rle->res)
2617 panic("resource_list_add: resource entry is busy");
2618
2619 rle->start = start;
2620 rle->end = end;
2621 rle->count = count;
2622 return (rle);
2623 }
2624
2625 /**
2626 * @brief Find a resource entry by type and rid.
2627 *
2628 * @param rl the resource list to search
2629 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2630 * @param rid the resource identifier
2631 *
2632 * @returns the resource entry pointer or NULL if there is no such
2633 * entry.
2634 */
2635 struct resource_list_entry *
2636 resource_list_find(struct resource_list *rl, int type, int rid)
2637 {
2638 struct resource_list_entry *rle;
2639
2640 STAILQ_FOREACH(rle, rl, link) {
2641 if (rle->type == type && rle->rid == rid)
2642 return (rle);
2643 }
2644 return (NULL);
2645 }
2646
2647 /**
2648 * @brief Delete a resource entry.
2649 *
2650 * @param rl the resource list to edit
2651 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2652 * @param rid the resource identifier
2653 */
2654 void
2655 resource_list_delete(struct resource_list *rl, int type, int rid)
2656 {
2657 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2658
2659 if (rle) {
2660 if (rle->res != NULL)
2661 panic("resource_list_delete: resource has not been released");
2662 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2663 free(rle, M_BUS);
2664 }
2665 }
2666
2667 /**
2668 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
2669 *
2670 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
2671 * and passing the allocation up to the parent of @p bus. This assumes
2672 * that the first entry of @c device_get_ivars(child) is a struct
2673 * resource_list. This also handles 'passthrough' allocations where a
2674 * child is a remote descendant of bus by passing the allocation up to
2675 * the parent of bus.
2676 *
2677 * Typically, a bus driver would store a list of child resources
2678 * somewhere in the child device's ivars (see device_get_ivars()) and
2679 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
2680 * then call resource_list_alloc() to perform the allocation.
2681 *
2682 * @param rl the resource list to allocate from
2683 * @param bus the parent device of @p child
2684 * @param child the device which is requesting an allocation
2685 * @param type the type of resource to allocate
2686 * @param rid a pointer to the resource identifier
2687 * @param start hint at the start of the resource range - pass
2688 * @c 0UL for any start address
2689 * @param end hint at the end of the resource range - pass
2690 * @c ~0UL for any end address
2691 * @param count hint at the size of range required - pass @c 1
2692 * for any size
2693 * @param flags any extra flags to control the resource
2694 * allocation - see @c RF_XXX flags in
2695 * <sys/rman.h> for details
2696 *
2697 * @returns the resource which was allocated or @c NULL if no
2698 * resource could be allocated
2699 */
2700 struct resource *
2701 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
2702 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
2703 {
2704 struct resource_list_entry *rle = NULL;
2705 int passthrough = (device_get_parent(child) != bus);
2706 int isdefault = (start == 0UL && end == ~0UL);
2707
2708 if (passthrough) {
2709 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2710 type, rid, start, end, count, flags));
2711 }
2712
2713 rle = resource_list_find(rl, type, *rid);
2714
2715 if (!rle)
2716 return (NULL); /* no resource of that type/rid */
2717
2718 if (rle->res)
2719 panic("resource_list_alloc: resource entry is busy");
2720
2721 if (isdefault) {
2722 start = rle->start;
2723 count = ulmax(count, rle->count);
2724 end = ulmax(rle->end, start + count - 1);
2725 }
2726
2727 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2728 type, rid, start, end, count, flags);
2729
2730 /*
2731 * Record the new range.
2732 */
2733 if (rle->res) {
2734 rle->start = rman_get_start(rle->res);
2735 rle->end = rman_get_end(rle->res);
2736 rle->count = count;
2737 }
2738
2739 return (rle->res);
2740 }
2741
2742 /**
2743 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
2744 *
2745 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
2746 * used with resource_list_alloc().
2747 *
2748 * @param rl the resource list which was allocated from
2749 * @param bus the parent device of @p child
2750 * @param child the device which is requesting a release
2751 * @param type the type of resource to allocate
2752 * @param rid the resource identifier
2753 * @param res the resource to release
2754 *
2755 * @retval 0 success
2756 * @retval non-zero a standard unix error code indicating what
2757 * error condition prevented the operation
2758 */
2759 int
2760 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
2761 int type, int rid, struct resource *res)
2762 {
2763 struct resource_list_entry *rle = NULL;
2764 int passthrough = (device_get_parent(child) != bus);
2765 int error;
2766
2767 if (passthrough) {
2768 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2769 type, rid, res));
2770 }
2771
2772 rle = resource_list_find(rl, type, rid);
2773
2774 if (!rle)
2775 panic("resource_list_release: can't find resource");
2776 if (!rle->res)
2777 panic("resource_list_release: resource entry is not busy");
2778
2779 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2780 type, rid, res);
2781 if (error)
2782 return (error);
2783
2784 rle->res = NULL;
2785 return (0);
2786 }
2787
2788 /**
2789 * @brief Print a description of resources in a resource list
2790 *
2791 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
2792 * The name is printed if at least one resource of the given type is available.
2793 * The format is used to print resource start and end.
2794 *
2795 * @param rl the resource list to print
2796 * @param name the name of @p type, e.g. @c "memory"
2797 * @param type type type of resource entry to print
2798 * @param format printf(9) format string to print resource
2799 * start and end values
2800 *
2801 * @returns the number of characters printed
2802 */
2803 int
2804 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2805 const char *format)
2806 {
2807 struct resource_list_entry *rle;
2808 int printed, retval;
2809
2810 printed = 0;
2811 retval = 0;
2812 /* Yes, this is kinda cheating */
2813 STAILQ_FOREACH(rle, rl, link) {
2814 if (rle->type == type) {
2815 if (printed == 0)
2816 retval += printf(" %s ", name);
2817 else
2818 retval += printf(",");
2819 printed++;
2820 retval += printf(format, rle->start);
2821 if (rle->count > 1) {
2822 retval += printf("-");
2823 retval += printf(format, rle->start +
2824 rle->count - 1);
2825 }
2826 }
2827 }
2828 return (retval);
2829 }
2830
2831 /**
2832 * @brief Releases all the resources in a list.
2833 *
2834 * @param rl The resource list to purge.
2835 *
2836 * @returns nothing
2837 */
2838 void
2839 resource_list_purge(struct resource_list *rl)
2840 {
2841 struct resource_list_entry *rle;
2842
2843 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2844 if (rle->res)
2845 bus_release_resource(rman_get_device(rle->res),
2846 rle->type, rle->rid, rle->res);
2847 STAILQ_REMOVE_HEAD(rl, link);
2848 free(rle, M_BUS);
2849 }
2850 }
2851
2852 device_t
2853 bus_generic_add_child(device_t dev, int order, const char *name, int unit)
2854 {
2855
2856 return (device_add_child_ordered(dev, order, name, unit));
2857 }
2858
2859 /**
2860 * @brief Helper function for implementing DEVICE_PROBE()
2861 *
2862 * This function can be used to help implement the DEVICE_PROBE() for
2863 * a bus (i.e. a device which has other devices attached to it). It
2864 * calls the DEVICE_IDENTIFY() method of each driver in the device's
2865 * devclass.
2866 */
2867 int
2868 bus_generic_probe(device_t dev)
2869 {
2870 devclass_t dc = dev->devclass;
2871 driverlink_t dl;
2872
2873 TAILQ_FOREACH(dl, &dc->drivers, link) {
2874 DEVICE_IDENTIFY(dl->driver, dev);
2875 }
2876
2877 return (0);
2878 }
2879
2880 /**
2881 * @brief Helper function for implementing DEVICE_ATTACH()
2882 *
2883 * This function can be used to help implement the DEVICE_ATTACH() for
2884 * a bus. It calls device_probe_and_attach() for each of the device's
2885 * children.
2886 */
2887 int
2888 bus_generic_attach(device_t dev)
2889 {
2890 device_t child;
2891
2892 TAILQ_FOREACH(child, &dev->children, link) {
2893 device_probe_and_attach(child);
2894 }
2895
2896 return (0);
2897 }
2898
2899 /**
2900 * @brief Helper function for implementing DEVICE_DETACH()
2901 *
2902 * This function can be used to help implement the DEVICE_DETACH() for
2903 * a bus. It calls device_detach() for each of the device's
2904 * children.
2905 */
2906 int
2907 bus_generic_detach(device_t dev)
2908 {
2909 device_t child;
2910 int error;
2911
2912 if (dev->state != DS_ATTACHED)
2913 return (EBUSY);
2914
2915 TAILQ_FOREACH(child, &dev->children, link) {
2916 if ((error = device_detach(child)) != 0)
2917 return (error);
2918 }
2919
2920 return (0);
2921 }
2922
2923 /**
2924 * @brief Helper function for implementing DEVICE_SHUTDOWN()
2925 *
2926 * This function can be used to help implement the DEVICE_SHUTDOWN()
2927 * for a bus. It calls device_shutdown() for each of the device's
2928 * children.
2929 */
2930 int
2931 bus_generic_shutdown(device_t dev)
2932 {
2933 device_t child;
2934
2935 TAILQ_FOREACH(child, &dev->children, link) {
2936 device_shutdown(child);
2937 }
2938
2939 return (0);
2940 }
2941
2942 /**
2943 * @brief Helper function for implementing DEVICE_SUSPEND()
2944 *
2945 * This function can be used to help implement the DEVICE_SUSPEND()
2946 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
2947 * children. If any call to DEVICE_SUSPEND() fails, the suspend
2948 * operation is aborted and any devices which were suspended are
2949 * resumed immediately by calling their DEVICE_RESUME() methods.
2950 */
2951 int
2952 bus_generic_suspend(device_t dev)
2953 {
2954 int error;
2955 device_t child, child2;
2956
2957 TAILQ_FOREACH(child, &dev->children, link) {
2958 error = DEVICE_SUSPEND(child);
2959 if (error) {
2960 for (child2 = TAILQ_FIRST(&dev->children);
2961 child2 && child2 != child;
2962 child2 = TAILQ_NEXT(child2, link))
2963 DEVICE_RESUME(child2);
2964 return (error);
2965 }
2966 }
2967 return (0);
2968 }
2969
2970 /**
2971 * @brief Helper function for implementing DEVICE_RESUME()
2972 *
2973 * This function can be used to help implement the DEVICE_RESUME() for
2974 * a bus. It calls DEVICE_RESUME() on each of the device's children.
2975 */
2976 int
2977 bus_generic_resume(device_t dev)
2978 {
2979 device_t child;
2980
2981 TAILQ_FOREACH(child, &dev->children, link) {
2982 DEVICE_RESUME(child);
2983 /* if resume fails, there's nothing we can usefully do... */
2984 }
2985 return (0);
2986 }
2987
2988 /**
2989 * @brief Helper function for implementing BUS_PRINT_CHILD().
2990 *
2991 * This function prints the first part of the ascii representation of
2992 * @p child, including its name, unit and description (if any - see
2993 * device_set_desc()).
2994 *
2995 * @returns the number of characters printed
2996 */
2997 int
2998 bus_print_child_header(device_t dev, device_t child)
2999 {
3000 int retval = 0;
3001
3002 if (device_get_desc(child)) {
3003 retval += device_printf(child, "<%s>", device_get_desc(child));
3004 } else {
3005 retval += printf("%s", device_get_nameunit(child));
3006 }
3007
3008 return (retval);
3009 }
3010
3011 /**
3012 * @brief Helper function for implementing BUS_PRINT_CHILD().
3013 *
3014 * This function prints the last part of the ascii representation of
3015 * @p child, which consists of the string @c " on " followed by the
3016 * name and unit of the @p dev.
3017 *
3018 * @returns the number of characters printed
3019 */
3020 int
3021 bus_print_child_footer(device_t dev, device_t child)
3022 {
3023 return (printf(" on %s\n", device_get_nameunit(dev)));
3024 }
3025
3026 /**
3027 * @brief Helper function for implementing BUS_PRINT_CHILD().
3028 *
3029 * This function simply calls bus_print_child_header() followed by
3030 * bus_print_child_footer().
3031 *
3032 * @returns the number of characters printed
3033 */
3034 int
3035 bus_generic_print_child(device_t dev, device_t child)
3036 {
3037 int retval = 0;
3038
3039 retval += bus_print_child_header(dev, child);
3040 retval += bus_print_child_footer(dev, child);
3041
3042 return (retval);
3043 }
3044
3045 /**
3046 * @brief Stub function for implementing BUS_READ_IVAR().
3047 *
3048 * @returns ENOENT
3049 */
3050 int
3051 bus_generic_read_ivar(device_t dev, device_t child, int index,
3052 uintptr_t * result)
3053 {
3054 return (ENOENT);
3055 }
3056
3057 /**
3058 * @brief Stub function for implementing BUS_WRITE_IVAR().
3059 *
3060 * @returns ENOENT
3061 */
3062 int
3063 bus_generic_write_ivar(device_t dev, device_t child, int index,
3064 uintptr_t value)
3065 {
3066 return (ENOENT);
3067 }
3068
3069 /**
3070 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3071 *
3072 * @returns NULL
3073 */
3074 struct resource_list *
3075 bus_generic_get_resource_list(device_t dev, device_t child)
3076 {
3077 return (NULL);
3078 }
3079
3080 /**
3081 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3082 *
3083 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3084 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3085 * and then calls device_probe_and_attach() for each unattached child.
3086 */
3087 void
3088 bus_generic_driver_added(device_t dev, driver_t *driver)
3089 {
3090 device_t child;
3091
3092 DEVICE_IDENTIFY(driver, dev);
3093 TAILQ_FOREACH(child, &dev->children, link) {
3094 if (child->state == DS_NOTPRESENT ||
3095 (child->flags & DF_REBID))
3096 device_probe_and_attach(child);
3097 }
3098 }
3099
3100 /**
3101 * @brief Helper function for implementing BUS_SETUP_INTR().
3102 *
3103 * This simple implementation of BUS_SETUP_INTR() simply calls the
3104 * BUS_SETUP_INTR() method of the parent of @p dev.
3105 */
3106 int
3107 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3108 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3109 void **cookiep)
3110 {
3111 /* Propagate up the bus hierarchy until someone handles it. */
3112 if (dev->parent)
3113 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3114 filter, intr, arg, cookiep));
3115 return (EINVAL);
3116 }
3117
3118 /**
3119 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3120 *
3121 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3122 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3123 */
3124 int
3125 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3126 void *cookie)
3127 {
3128 /* Propagate up the bus hierarchy until someone handles it. */
3129 if (dev->parent)
3130 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3131 return (EINVAL);
3132 }
3133
3134 /**
3135 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3136 *
3137 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3138 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3139 */
3140 struct resource *
3141 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3142 u_long start, u_long end, u_long count, u_int flags)
3143 {
3144 /* Propagate up the bus hierarchy until someone handles it. */
3145 if (dev->parent)
3146 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3147 start, end, count, flags));
3148 return (NULL);
3149 }
3150
3151 /**
3152 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3153 *
3154 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3155 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3156 */
3157 int
3158 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3159 struct resource *r)
3160 {
3161 /* Propagate up the bus hierarchy until someone handles it. */
3162 if (dev->parent)
3163 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3164 r));
3165 return (EINVAL);
3166 }
3167
3168 /**
3169 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3170 *
3171 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3172 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3173 */
3174 int
3175 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3176 struct resource *r)
3177 {
3178 /* Propagate up the bus hierarchy until someone handles it. */
3179 if (dev->parent)
3180 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3181 r));
3182 return (EINVAL);
3183 }
3184
3185 /**
3186 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3187 *
3188 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3189 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3190 */
3191 int
3192 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3193 int rid, struct resource *r)
3194 {
3195 /* Propagate up the bus hierarchy until someone handles it. */
3196 if (dev->parent)
3197 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3198 r));
3199 return (EINVAL);
3200 }
3201
3202 /**
3203 * @brief Helper function for implementing BUS_BIND_INTR().
3204 *
3205 * This simple implementation of BUS_BIND_INTR() simply calls the
3206 * BUS_BIND_INTR() method of the parent of @p dev.
3207 */
3208 int
3209 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3210 int cpu)
3211 {
3212
3213 /* Propagate up the bus hierarchy until someone handles it. */
3214 if (dev->parent)
3215 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3216 return (EINVAL);
3217 }
3218
3219 /**
3220 * @brief Helper function for implementing BUS_CONFIG_INTR().
3221 *
3222 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3223 * BUS_CONFIG_INTR() method of the parent of @p dev.
3224 */
3225 int
3226 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3227 enum intr_polarity pol)
3228 {
3229
3230 /* Propagate up the bus hierarchy until someone handles it. */
3231 if (dev->parent)
3232 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3233 return (EINVAL);
3234 }
3235
3236 /**
3237 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3238 *
3239 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3240 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3241 */
3242 bus_dma_tag_t
3243 bus_generic_get_dma_tag(device_t dev, device_t child)
3244 {
3245
3246 /* Propagate up the bus hierarchy until someone handles it. */
3247 if (dev->parent != NULL)
3248 return (BUS_GET_DMA_TAG(dev->parent, child));
3249 return (NULL);
3250 }
3251
3252 /**
3253 * @brief Helper function for implementing BUS_GET_RESOURCE().
3254 *
3255 * This implementation of BUS_GET_RESOURCE() uses the
3256 * resource_list_find() function to do most of the work. It calls
3257 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3258 * search.
3259 */
3260 int
3261 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3262 u_long *startp, u_long *countp)
3263 {
3264 struct resource_list * rl = NULL;
3265 struct resource_list_entry * rle = NULL;
3266
3267 rl = BUS_GET_RESOURCE_LIST(dev, child);
3268 if (!rl)
3269 return (EINVAL);
3270
3271 rle = resource_list_find(rl, type, rid);
3272 if (!rle)
3273 return (ENOENT);
3274
3275 if (startp)
3276 *startp = rle->start;
3277 if (countp)
3278 *countp = rle->count;
3279
3280 return (0);
3281 }
3282
3283 /**
3284 * @brief Helper function for implementing BUS_SET_RESOURCE().
3285 *
3286 * This implementation of BUS_SET_RESOURCE() uses the
3287 * resource_list_add() function to do most of the work. It calls
3288 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3289 * edit.
3290 */
3291 int
3292 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3293 u_long start, u_long count)
3294 {
3295 struct resource_list * rl = NULL;
3296
3297 rl = BUS_GET_RESOURCE_LIST(dev, child);
3298 if (!rl)
3299 return (EINVAL);
3300
3301 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3302
3303 return (0);
3304 }
3305
3306 /**
3307 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3308 *
3309 * This implementation of BUS_DELETE_RESOURCE() uses the
3310 * resource_list_delete() function to do most of the work. It calls
3311 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3312 * edit.
3313 */
3314 void
3315 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3316 {
3317 struct resource_list * rl = NULL;
3318
3319 rl = BUS_GET_RESOURCE_LIST(dev, child);
3320 if (!rl)
3321 return;
3322
3323 resource_list_delete(rl, type, rid);
3324
3325 return;
3326 }
3327
3328 /**
3329 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3330 *
3331 * This implementation of BUS_RELEASE_RESOURCE() uses the
3332 * resource_list_release() function to do most of the work. It calls
3333 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3334 */
3335 int
3336 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3337 int rid, struct resource *r)
3338 {
3339 struct resource_list * rl = NULL;
3340
3341 rl = BUS_GET_RESOURCE_LIST(dev, child);
3342 if (!rl)
3343 return (EINVAL);
3344
3345 return (resource_list_release(rl, dev, child, type, rid, r));
3346 }
3347
3348 /**
3349 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3350 *
3351 * This implementation of BUS_ALLOC_RESOURCE() uses the
3352 * resource_list_alloc() function to do most of the work. It calls
3353 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3354 */
3355 struct resource *
3356 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3357 int *rid, u_long start, u_long end, u_long count, u_int flags)
3358 {
3359 struct resource_list * rl = NULL;
3360
3361 rl = BUS_GET_RESOURCE_LIST(dev, child);
3362 if (!rl)
3363 return (NULL);
3364
3365 return (resource_list_alloc(rl, dev, child, type, rid,
3366 start, end, count, flags));
3367 }
3368
3369 /**
3370 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3371 *
3372 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3373 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3374 */
3375 int
3376 bus_generic_child_present(device_t dev, device_t child)
3377 {
3378 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3379 }
3380
3381 /*
3382 * Some convenience functions to make it easier for drivers to use the
3383 * resource-management functions. All these really do is hide the
3384 * indirection through the parent's method table, making for slightly
3385 * less-wordy code. In the future, it might make sense for this code
3386 * to maintain some sort of a list of resources allocated by each device.
3387 */
3388
3389 int
3390 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3391 struct resource **res)
3392 {
3393 int i;
3394
3395 for (i = 0; rs[i].type != -1; i++)
3396 res[i] = NULL;
3397 for (i = 0; rs[i].type != -1; i++) {
3398 res[i] = bus_alloc_resource_any(dev,
3399 rs[i].type, &rs[i].rid, rs[i].flags);
3400 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
3401 bus_release_resources(dev, rs, res);
3402 return (ENXIO);
3403 }
3404 }
3405 return (0);
3406 }
3407
3408 void
3409 bus_release_resources(device_t dev, const struct resource_spec *rs,
3410 struct resource **res)
3411 {
3412 int i;
3413
3414 for (i = 0; rs[i].type != -1; i++)
3415 if (res[i] != NULL) {
3416 bus_release_resource(
3417 dev, rs[i].type, rs[i].rid, res[i]);
3418 res[i] = NULL;
3419 }
3420 }
3421
3422 /**
3423 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3424 *
3425 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3426 * parent of @p dev.
3427 */
3428 struct resource *
3429 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3430 u_long count, u_int flags)
3431 {
3432 if (dev->parent == NULL)
3433 return (NULL);
3434 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3435 count, flags));
3436 }
3437
3438 /**
3439 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
3440 *
3441 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
3442 * parent of @p dev.
3443 */
3444 int
3445 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3446 {
3447 if (dev->parent == NULL)
3448 return (EINVAL);
3449 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3450 }
3451
3452 /**
3453 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
3454 *
3455 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
3456 * parent of @p dev.
3457 */
3458 int
3459 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3460 {
3461 if (dev->parent == NULL)
3462 return (EINVAL);
3463 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3464 }
3465
3466 /**
3467 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
3468 *
3469 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
3470 * parent of @p dev.
3471 */
3472 int
3473 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3474 {
3475 if (dev->parent == NULL)
3476 return (EINVAL);
3477 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3478 }
3479
3480 /**
3481 * @brief Wrapper function for BUS_SETUP_INTR().
3482 *
3483 * This function simply calls the BUS_SETUP_INTR() method of the
3484 * parent of @p dev.
3485 */
3486 int
3487 bus_setup_intr(device_t dev, struct resource *r, int flags,
3488 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
3489 {
3490 int error;
3491
3492 if (dev->parent != NULL) {
3493 error = BUS_SETUP_INTR(dev->parent, dev, r, flags,
3494 filter, handler, arg, cookiep);
3495 if (error == 0) {
3496 if (handler != NULL && !(flags & INTR_MPSAFE))
3497 device_printf(dev, "[GIANT-LOCKED]\n");
3498 if (bootverbose && (flags & INTR_MPSAFE))
3499 device_printf(dev, "[MPSAFE]\n");
3500 if (filter != NULL) {
3501 if (handler == NULL)
3502 device_printf(dev, "[FILTER]\n");
3503 else
3504 device_printf(dev, "[FILTER+ITHREAD]\n");
3505 } else
3506 device_printf(dev, "[ITHREAD]\n");
3507 }
3508 } else
3509 error = EINVAL;
3510 return (error);
3511 }
3512
3513 /**
3514 * @brief Wrapper function for BUS_TEARDOWN_INTR().
3515 *
3516 * This function simply calls the BUS_TEARDOWN_INTR() method of the
3517 * parent of @p dev.
3518 */
3519 int
3520 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3521 {
3522 if (dev->parent == NULL)
3523 return (EINVAL);
3524 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3525 }
3526
3527 /**
3528 * @brief Wrapper function for BUS_BIND_INTR().
3529 *
3530 * This function simply calls the BUS_BIND_INTR() method of the
3531 * parent of @p dev.
3532 */
3533 int
3534 bus_bind_intr(device_t dev, struct resource *r, int cpu)
3535 {
3536 if (dev->parent == NULL)
3537 return (EINVAL);
3538 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
3539 }
3540
3541 /**
3542 * @brief Wrapper function for BUS_SET_RESOURCE().
3543 *
3544 * This function simply calls the BUS_SET_RESOURCE() method of the
3545 * parent of @p dev.
3546 */
3547 int
3548 bus_set_resource(device_t dev, int type, int rid,
3549 u_long start, u_long count)
3550 {
3551 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3552 start, count));
3553 }
3554
3555 /**
3556 * @brief Wrapper function for BUS_GET_RESOURCE().
3557 *
3558 * This function simply calls the BUS_GET_RESOURCE() method of the
3559 * parent of @p dev.
3560 */
3561 int
3562 bus_get_resource(device_t dev, int type, int rid,
3563 u_long *startp, u_long *countp)
3564 {
3565 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3566 startp, countp));
3567 }
3568
3569 /**
3570 * @brief Wrapper function for BUS_GET_RESOURCE().
3571 *
3572 * This function simply calls the BUS_GET_RESOURCE() method of the
3573 * parent of @p dev and returns the start value.
3574 */
3575 u_long
3576 bus_get_resource_start(device_t dev, int type, int rid)
3577 {
3578 u_long start, count;
3579 int error;
3580
3581 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3582 &start, &count);
3583 if (error)
3584 return (0);
3585 return (start);
3586 }
3587
3588 /**
3589 * @brief Wrapper function for BUS_GET_RESOURCE().
3590 *
3591 * This function simply calls the BUS_GET_RESOURCE() method of the
3592 * parent of @p dev and returns the count value.
3593 */
3594 u_long
3595 bus_get_resource_count(device_t dev, int type, int rid)
3596 {
3597 u_long start, count;
3598 int error;
3599
3600 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3601 &start, &count);
3602 if (error)
3603 return (0);
3604 return (count);
3605 }
3606
3607 /**
3608 * @brief Wrapper function for BUS_DELETE_RESOURCE().
3609 *
3610 * This function simply calls the BUS_DELETE_RESOURCE() method of the
3611 * parent of @p dev.
3612 */
3613 void
3614 bus_delete_resource(device_t dev, int type, int rid)
3615 {
3616 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3617 }
3618
3619 /**
3620 * @brief Wrapper function for BUS_CHILD_PRESENT().
3621 *
3622 * This function simply calls the BUS_CHILD_PRESENT() method of the
3623 * parent of @p dev.
3624 */
3625 int
3626 bus_child_present(device_t child)
3627 {
3628 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3629 }
3630
3631 /**
3632 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
3633 *
3634 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
3635 * parent of @p dev.
3636 */
3637 int
3638 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3639 {
3640 device_t parent;
3641
3642 parent = device_get_parent(child);
3643 if (parent == NULL) {
3644 *buf = '\0';
3645 return (0);
3646 }
3647 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3648 }
3649
3650 /**
3651 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
3652 *
3653 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
3654 * parent of @p dev.
3655 */
3656 int
3657 bus_child_location_str(device_t child, char *buf, size_t buflen)
3658 {
3659 device_t parent;
3660
3661 parent = device_get_parent(child);
3662 if (parent == NULL) {
3663 *buf = '\0';
3664 return (0);
3665 }
3666 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3667 }
3668
3669 /**
3670 * @brief Wrapper function for BUS_GET_DMA_TAG().
3671 *
3672 * This function simply calls the BUS_GET_DMA_TAG() method of the
3673 * parent of @p dev.
3674 */
3675 bus_dma_tag_t
3676 bus_get_dma_tag(device_t dev)
3677 {
3678 device_t parent;
3679
3680 parent = device_get_parent(dev);
3681 if (parent == NULL)
3682 return (NULL);
3683 return (BUS_GET_DMA_TAG(parent, dev));
3684 }
3685
3686 /* Resume all devices and then notify userland that we're up again. */
3687 static int
3688 root_resume(device_t dev)
3689 {
3690 int error;
3691
3692 error = bus_generic_resume(dev);
3693 if (error == 0)
3694 devctl_notify("kern", "power", "resume", NULL);
3695 return (error);
3696 }
3697
3698 static int
3699 root_print_child(device_t dev, device_t child)
3700 {
3701 int retval = 0;
3702
3703 retval += bus_print_child_header(dev, child);
3704 retval += printf("\n");
3705
3706 return (retval);
3707 }
3708
3709 static int
3710 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3711 void **cookiep)
3712 {
3713 /*
3714 * If an interrupt mapping gets to here something bad has happened.
3715 */
3716 panic("root_setup_intr");
3717 }
3718
3719 /*
3720 * If we get here, assume that the device is permanant and really is
3721 * present in the system. Removable bus drivers are expected to intercept
3722 * this call long before it gets here. We return -1 so that drivers that
3723 * really care can check vs -1 or some ERRNO returned higher in the food
3724 * chain.
3725 */
3726 static int
3727 root_child_present(device_t dev, device_t child)
3728 {
3729 return (-1);
3730 }
3731
3732 static kobj_method_t root_methods[] = {
3733 /* Device interface */
3734 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3735 KOBJMETHOD(device_suspend, bus_generic_suspend),
3736 KOBJMETHOD(device_resume, root_resume),
3737
3738 /* Bus interface */
3739 KOBJMETHOD(bus_print_child, root_print_child),
3740 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3741 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3742 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3743 KOBJMETHOD(bus_child_present, root_child_present),
3744
3745 { 0, 0 }
3746 };
3747
3748 static driver_t root_driver = {
3749 "root",
3750 root_methods,
3751 1, /* no softc */
3752 };
3753
3754 device_t root_bus;
3755 devclass_t root_devclass;
3756
3757 static int
3758 root_bus_module_handler(module_t mod, int what, void* arg)
3759 {
3760 switch (what) {
3761 case MOD_LOAD:
3762 TAILQ_INIT(&bus_data_devices);
3763 kobj_class_compile((kobj_class_t) &root_driver);
3764 root_bus = make_device(NULL, "root", 0);
3765 root_bus->desc = "System root bus";
3766 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3767 root_bus->driver = &root_driver;
3768 root_bus->state = DS_ATTACHED;
3769 root_devclass = devclass_find_internal("root", NULL, FALSE);
3770 devinit();
3771 return (0);
3772
3773 case MOD_SHUTDOWN:
3774 device_shutdown(root_bus);
3775 return (0);
3776 default:
3777 return (EOPNOTSUPP);
3778 }
3779
3780 return (0);
3781 }
3782
3783 static moduledata_t root_bus_mod = {
3784 "rootbus",
3785 root_bus_module_handler,
3786 0
3787 };
3788 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3789
3790 /**
3791 * @brief Automatically configure devices
3792 *
3793 * This function begins the autoconfiguration process by calling
3794 * device_probe_and_attach() for each child of the @c root0 device.
3795 */
3796 void
3797 root_bus_configure(void)
3798 {
3799 device_t dev;
3800
3801 PDEBUG(("."));
3802
3803 TAILQ_FOREACH(dev, &root_bus->children, link) {
3804 device_probe_and_attach(dev);
3805 }
3806 }
3807
3808 /**
3809 * @brief Module handler for registering device drivers
3810 *
3811 * This module handler is used to automatically register device
3812 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
3813 * devclass_add_driver() for the driver described by the
3814 * driver_module_data structure pointed to by @p arg
3815 */
3816 int
3817 driver_module_handler(module_t mod, int what, void *arg)
3818 {
3819 int error;
3820 struct driver_module_data *dmd;
3821 devclass_t bus_devclass;
3822 kobj_class_t driver;
3823
3824 dmd = (struct driver_module_data *)arg;
3825 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3826 error = 0;
3827
3828 switch (what) {
3829 case MOD_LOAD:
3830 if (dmd->dmd_chainevh)
3831 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3832
3833 driver = dmd->dmd_driver;
3834 PDEBUG(("Loading module: driver %s on bus %s",
3835 DRIVERNAME(driver), dmd->dmd_busname));
3836 error = devclass_add_driver(bus_devclass, driver);
3837 if (error)
3838 break;
3839
3840 /*
3841 * If the driver has any base classes, make the
3842 * devclass inherit from the devclass of the driver's
3843 * first base class. This will allow the system to
3844 * search for drivers in both devclasses for children
3845 * of a device using this driver.
3846 */
3847 if (driver->baseclasses) {
3848 const char *parentname;
3849 parentname = driver->baseclasses[0]->name;
3850 *dmd->dmd_devclass =
3851 devclass_find_internal(driver->name,
3852 parentname, TRUE);
3853 } else {
3854 *dmd->dmd_devclass =
3855 devclass_find_internal(driver->name, NULL, TRUE);
3856 }
3857 break;
3858
3859 case MOD_UNLOAD:
3860 PDEBUG(("Unloading module: driver %s from bus %s",
3861 DRIVERNAME(dmd->dmd_driver),
3862 dmd->dmd_busname));
3863 error = devclass_delete_driver(bus_devclass,
3864 dmd->dmd_driver);
3865
3866 if (!error && dmd->dmd_chainevh)
3867 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3868 break;
3869 case MOD_QUIESCE:
3870 PDEBUG(("Quiesce module: driver %s from bus %s",
3871 DRIVERNAME(dmd->dmd_driver),
3872 dmd->dmd_busname));
3873 error = devclass_quiesce_driver(bus_devclass,
3874 dmd->dmd_driver);
3875
3876 if (!error && dmd->dmd_chainevh)
3877 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3878 break;
3879 default:
3880 error = EOPNOTSUPP;
3881 break;
3882 }
3883
3884 return (error);
3885 }
3886
3887 /**
3888 * @brief Enumerate all hinted devices for this bus.
3889 *
3890 * Walks through the hints for this bus and calls the bus_hinted_child
3891 * routine for each one it fines. It searches first for the specific
3892 * bus that's being probed for hinted children (eg isa0), and then for
3893 * generic children (eg isa).
3894 *
3895 * @param dev bus device to enumerate
3896 */
3897 void
3898 bus_enumerate_hinted_children(device_t bus)
3899 {
3900 int i;
3901 const char *dname, *busname;
3902 int dunit;
3903
3904 /*
3905 * enumerate all devices on the specific bus
3906 */
3907 busname = device_get_nameunit(bus);
3908 i = 0;
3909 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
3910 BUS_HINTED_CHILD(bus, dname, dunit);
3911
3912 /*
3913 * and all the generic ones.
3914 */
3915 busname = device_get_name(bus);
3916 i = 0;
3917 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
3918 BUS_HINTED_CHILD(bus, dname, dunit);
3919 }
3920
3921 #ifdef BUS_DEBUG
3922
3923 /* the _short versions avoid iteration by not calling anything that prints
3924 * more than oneliners. I love oneliners.
3925 */
3926
3927 static void
3928 print_device_short(device_t dev, int indent)
3929 {
3930 if (!dev)
3931 return;
3932
3933 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3934 dev->unit, dev->desc,
3935 (dev->parent? "":"no "),
3936 (TAILQ_EMPTY(&dev->children)? "no ":""),
3937 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3938 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3939 (dev->flags&DF_WILDCARD? "wildcard,":""),
3940 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3941 (dev->flags&DF_REBID? "rebiddable,":""),
3942 (dev->ivars? "":"no "),
3943 (dev->softc? "":"no "),
3944 dev->busy));
3945 }
3946
3947 static void
3948 print_device(device_t dev, int indent)
3949 {
3950 if (!dev)
3951 return;
3952
3953 print_device_short(dev, indent);
3954
3955 indentprintf(("Parent:\n"));
3956 print_device_short(dev->parent, indent+1);
3957 indentprintf(("Driver:\n"));
3958 print_driver_short(dev->driver, indent+1);
3959 indentprintf(("Devclass:\n"));
3960 print_devclass_short(dev->devclass, indent+1);
3961 }
3962
3963 void
3964 print_device_tree_short(device_t dev, int indent)
3965 /* print the device and all its children (indented) */
3966 {
3967 device_t child;
3968
3969 if (!dev)
3970 return;
3971
3972 print_device_short(dev, indent);
3973
3974 TAILQ_FOREACH(child, &dev->children, link) {
3975 print_device_tree_short(child, indent+1);
3976 }
3977 }
3978
3979 void
3980 print_device_tree(device_t dev, int indent)
3981 /* print the device and all its children (indented) */
3982 {
3983 device_t child;
3984
3985 if (!dev)
3986 return;
3987
3988 print_device(dev, indent);
3989
3990 TAILQ_FOREACH(child, &dev->children, link) {
3991 print_device_tree(child, indent+1);
3992 }
3993 }
3994
3995 static void
3996 print_driver_short(driver_t *driver, int indent)
3997 {
3998 if (!driver)
3999 return;
4000
4001 indentprintf(("driver %s: softc size = %zd\n",
4002 driver->name, driver->size));
4003 }
4004
4005 static void
4006 print_driver(driver_t *driver, int indent)
4007 {
4008 if (!driver)
4009 return;
4010
4011 print_driver_short(driver, indent);
4012 }
4013
4014
4015 static void
4016 print_driver_list(driver_list_t drivers, int indent)
4017 {
4018 driverlink_t driver;
4019
4020 TAILQ_FOREACH(driver, &drivers, link) {
4021 print_driver(driver->driver, indent);
4022 }
4023 }
4024
4025 static void
4026 print_devclass_short(devclass_t dc, int indent)
4027 {
4028 if ( !dc )
4029 return;
4030
4031 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4032 }
4033
4034 static void
4035 print_devclass(devclass_t dc, int indent)
4036 {
4037 int i;
4038
4039 if ( !dc )
4040 return;
4041
4042 print_devclass_short(dc, indent);
4043 indentprintf(("Drivers:\n"));
4044 print_driver_list(dc->drivers, indent+1);
4045
4046 indentprintf(("Devices:\n"));
4047 for (i = 0; i < dc->maxunit; i++)
4048 if (dc->devices[i])
4049 print_device(dc->devices[i], indent+1);
4050 }
4051
4052 void
4053 print_devclass_list_short(void)
4054 {
4055 devclass_t dc;
4056
4057 printf("Short listing of devclasses, drivers & devices:\n");
4058 TAILQ_FOREACH(dc, &devclasses, link) {
4059 print_devclass_short(dc, 0);
4060 }
4061 }
4062
4063 void
4064 print_devclass_list(void)
4065 {
4066 devclass_t dc;
4067
4068 printf("Full listing of devclasses, drivers & devices:\n");
4069 TAILQ_FOREACH(dc, &devclasses, link) {
4070 print_devclass(dc, 0);
4071 }
4072 }
4073
4074 #endif
4075
4076 /*
4077 * User-space access to the device tree.
4078 *
4079 * We implement a small set of nodes:
4080 *
4081 * hw.bus Single integer read method to obtain the
4082 * current generation count.
4083 * hw.bus.devices Reads the entire device tree in flat space.
4084 * hw.bus.rman Resource manager interface
4085 *
4086 * We might like to add the ability to scan devclasses and/or drivers to
4087 * determine what else is currently loaded/available.
4088 */
4089
4090 static int
4091 sysctl_bus(SYSCTL_HANDLER_ARGS)
4092 {
4093 struct u_businfo ubus;
4094
4095 ubus.ub_version = BUS_USER_VERSION;
4096 ubus.ub_generation = bus_data_generation;
4097
4098 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4099 }
4100 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4101 "bus-related data");
4102
4103 static int
4104 sysctl_devices(SYSCTL_HANDLER_ARGS)
4105 {
4106 int *name = (int *)arg1;
4107 u_int namelen = arg2;
4108 int index;
4109 struct device *dev;
4110 struct u_device udev; /* XXX this is a bit big */
4111 int error;
4112
4113 if (namelen != 2)
4114 return (EINVAL);
4115
4116 if (bus_data_generation_check(name[0]))
4117 return (EINVAL);
4118
4119 index = name[1];
4120
4121 /*
4122 * Scan the list of devices, looking for the requested index.
4123 */
4124 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4125 if (index-- == 0)
4126 break;
4127 }
4128 if (dev == NULL)
4129 return (ENOENT);
4130
4131 /*
4132 * Populate the return array.
4133 */
4134 bzero(&udev, sizeof(udev));
4135 udev.dv_handle = (uintptr_t)dev;
4136 udev.dv_parent = (uintptr_t)dev->parent;
4137 if (dev->nameunit != NULL)
4138 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4139 if (dev->desc != NULL)
4140 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4141 if (dev->driver != NULL && dev->driver->name != NULL)
4142 strlcpy(udev.dv_drivername, dev->driver->name,
4143 sizeof(udev.dv_drivername));
4144 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4145 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4146 udev.dv_devflags = dev->devflags;
4147 udev.dv_flags = dev->flags;
4148 udev.dv_state = dev->state;
4149 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4150 return (error);
4151 }
4152
4153 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4154 "system device tree");
4155
4156 int
4157 bus_data_generation_check(int generation)
4158 {
4159 if (generation != bus_data_generation)
4160 return (1);
4161
4162 /* XXX generate optimised lists here? */
4163 return (0);
4164 }
4165
4166 void
4167 bus_data_generation_update(void)
4168 {
4169 bus_data_generation++;
4170 }
4171
4172 int
4173 bus_free_resource(device_t dev, int type, struct resource *r)
4174 {
4175 if (r == NULL)
4176 return (0);
4177 return (bus_release_resource(dev, type, rman_get_rid(r), r));
4178 }
Cache object: f3a5645d6c625ad86e843dc250f7becd
|