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