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