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