FreeBSD kernel kern code
subr_bus.c
Go to the documentation of this file.
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("$BSDSUniX$");
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 <net/vnet.h>
57 
58 #include <machine/stdarg.h>
59 
60 #include <vm/uma.h>
61 
62 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
63 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
64 
65 /*
66  * Used to attach drivers to devclasses.
67  */
68 typedef struct driverlink *driverlink_t;
69 struct driverlink {
70  kobj_class_t driver;
71  TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
72  int pass;
73  TAILQ_ENTRY(driverlink) passlink;
74 };
75 
76 /*
77  * Forward declarations
78  */
79 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
80 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
81 typedef TAILQ_HEAD(device_list, device) device_list_t;
82 
83 struct devclass {
84  TAILQ_ENTRY(devclass) link;
85  devclass_t parent; /* parent in devclass hierarchy */
86  driver_list_t drivers; /* bus devclasses store drivers for bus */
87  char *name;
88  device_t *devices; /* array of devices indexed by unit */
89  int maxunit; /* size of devices array */
90  int flags;
91 #define DC_HAS_CHILDREN 1
92 
93  struct sysctl_ctx_list sysctl_ctx;
94  struct sysctl_oid *sysctl_tree;
95 };
96 
100 struct device {
101  /*
102  * A device is a kernel object. The first field must be the
103  * current ops table for the object.
104  */
106 
107  /*
108  * Device hierarchy.
109  */
110  TAILQ_ENTRY(device) link;
111  TAILQ_ENTRY(device) devlink;
112  device_t parent;
113  device_list_t children;
115  /*
116  * Details of this device.
117  */
118  driver_t *driver;
119  devclass_t devclass;
120  int unit;
121  char* nameunit;
122  char* desc;
123  int busy;
124  device_state_t state;
125  uint32_t devflags;
126  u_int flags;
127 #define DF_ENABLED 0x01 /* device should be probed/attached */
128 #define DF_FIXEDCLASS 0x02 /* devclass specified at create time */
129 #define DF_WILDCARD 0x04 /* unit was originally wildcard */
130 #define DF_DESCMALLOCED 0x08 /* description was malloced */
131 #define DF_QUIET 0x10 /* don't print verbose attach message */
132 #define DF_DONENOMATCH 0x20 /* don't execute DEVICE_NOMATCH again */
133 #define DF_EXTERNALSOFTC 0x40 /* softc not allocated by us */
134 #define DF_REBID 0x80 /* Can rebid after attach */
135  u_int order;
136  void *ivars;
137  void *softc;
139  struct sysctl_ctx_list sysctl_ctx;
140  struct sysctl_oid *sysctl_tree;
141 };
142 
143 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
144 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
145 
146 #ifdef BUS_DEBUG
147 
148 static int bus_debug = 1;
149 TUNABLE_INT("bus.debug", &bus_debug);
150 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
151  "Debug bus code");
152 
153 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
154 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
155 #define DRIVERNAME(d) ((d)? d->name : "no driver")
156 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
157 
162 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
163 
164 static void print_device_short(device_t dev, int indent);
165 static void print_device(device_t dev, int indent);
166 void print_device_tree_short(device_t dev, int indent);
167 void print_device_tree(device_t dev, int indent);
168 static void print_driver_short(driver_t *driver, int indent);
169 static void print_driver(driver_t *driver, int indent);
170 static void print_driver_list(driver_list_t drivers, int indent);
171 static void print_devclass_short(devclass_t dc, int indent);
172 static void print_devclass(devclass_t dc, int indent);
173 void print_devclass_list_short(void);
174 void print_devclass_list(void);
175 
176 #else
177 /* Make the compiler ignore the function calls */
178 #define PDEBUG(a) /* nop */
179 #define DEVICENAME(d) /* nop */
180 #define DRIVERNAME(d) /* nop */
181 #define DEVCLANAME(d) /* nop */
182 
183 #define print_device_short(d,i) /* nop */
184 #define print_device(d,i) /* nop */
185 #define print_device_tree_short(d,i) /* nop */
186 #define print_device_tree(d,i) /* nop */
187 #define print_driver_short(d,i) /* nop */
188 #define print_driver(d,i) /* nop */
189 #define print_driver_list(d,i) /* nop */
190 #define print_devclass_short(d,i) /* nop */
191 #define print_devclass(d,i) /* nop */
192 #define print_devclass_list_short() /* nop */
193 #define print_devclass_list() /* nop */
194 #endif
195 
196 /*
197  * dev sysctl tree
198  */
199 
200 enum {
202 };
203 
204 static int
205 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
206 {
207  devclass_t dc = (devclass_t)arg1;
208  const char *value;
209 
210  switch (arg2) {
212  value = dc->parent ? dc->parent->name : "";
213  break;
214  default:
215  return (EINVAL);
216  }
217  return (SYSCTL_OUT(req, value, strlen(value)));
218 }
219 
220 static void
221 devclass_sysctl_init(devclass_t dc)
222 {
223 
224  if (dc->sysctl_tree != NULL)
225  return;
226  sysctl_ctx_init(&dc->sysctl_ctx);
227  dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
228  SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
229  CTLFLAG_RD, NULL, "");
230  SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
231  OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
233  "parent class");
234 }
235 
236 enum {
242 };
243 
244 static int
245 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
246 {
247  device_t dev = (device_t)arg1;
248  const char *value;
249  char *buf;
250  int error;
251 
252  buf = NULL;
253  switch (arg2) {
254  case DEVICE_SYSCTL_DESC:
255  value = dev->desc ? dev->desc : "";
256  break;
258  value = dev->driver ? dev->driver->name : "";
259  break;
261  value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
262  bus_child_location_str(dev, buf, 1024);
263  break;
265  value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
266  bus_child_pnpinfo_str(dev, buf, 1024);
267  break;
269  value = dev->parent ? dev->parent->nameunit : "";
270  break;
271  default:
272  return (EINVAL);
273  }
274  error = SYSCTL_OUT(req, value, strlen(value));
275  if (buf != NULL)
276  free(buf, M_BUS);
277  return (error);
278 }
279 
280 static void
281 device_sysctl_init(device_t dev)
282 {
283  devclass_t dc = dev->devclass;
284 
285  if (dev->sysctl_tree != NULL)
286  return;
288  sysctl_ctx_init(&dev->sysctl_ctx);
289  dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
290  SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
291  dev->nameunit + strlen(dc->name),
292  CTLFLAG_RD, NULL, "");
293  SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
294  OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
296  "device description");
297  SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
298  OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
300  "device driver name");
301  SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
302  OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
304  "device location relative to parent");
305  SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
306  OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
308  "device identification");
309  SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
310  OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
312  "parent device");
313 }
314 
315 static void
316 device_sysctl_update(device_t dev)
317 {
318  devclass_t dc = dev->devclass;
319 
320  if (dev->sysctl_tree == NULL)
321  return;
322  sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
323 }
324 
325 static void
326 device_sysctl_fini(device_t dev)
327 {
328  if (dev->sysctl_tree == NULL)
329  return;
330  sysctl_ctx_free(&dev->sysctl_ctx);
331  dev->sysctl_tree = NULL;
332 }
333 
334 /*
335  * /dev/devctl implementation
336  */
337 
338 /*
339  * This design allows only one reader for /dev/devctl. This is not desirable
340  * in the long run, but will get a lot of hair out of this implementation.
341  * Maybe we should make this device a clonable device.
342  *
343  * Also note: we specifically do not attach a device to the device_t tree
344  * to avoid potential chicken and egg problems. One could argue that all
345  * of this belongs to the root node. One could also further argue that the
346  * sysctl interface that we have not might more properly be an ioctl
347  * interface, but at this stage of the game, I'm not inclined to rock that
348  * boat.
349  *
350  * I'm also not sure that the SIGIO support is done correctly or not, as
351  * I copied it from a driver that had SIGIO support that likely hasn't been
352  * tested since 3.4 or 2.2.8!
353  */
354 
355 /* Deprecated way to adjust queue length */
356 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
357 /* XXX Need to support old-style tunable hw.bus.devctl_disable" */
358 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, NULL,
359  0, sysctl_devctl_disable, "I", "devctl disable -- deprecated");
360 
361 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
362 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
364 TUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length);
365 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW, NULL,
366  0, sysctl_devctl_queue, "I", "devctl queue length");
367 
368 static d_open_t devopen;
369 static d_close_t devclose;
370 static d_read_t devread;
371 static d_ioctl_t devioctl;
372 static d_poll_t devpoll;
373 
374 static struct cdevsw dev_cdevsw = {
375  .d_version = D_VERSION,
376  .d_flags = D_NEEDGIANT,
377  .d_open = devopen,
378  .d_close = devclose,
379  .d_read = devread,
380  .d_ioctl = devioctl,
381  .d_poll = devpoll,
382  .d_name = "devctl",
383 };
384 
386 {
387  char *dei_data;
388  TAILQ_ENTRY(dev_event_info) dei_link;
389 };
390 
392 
393 static struct dev_softc
394 {
395  int inuse;
396  int nonblock;
397  int queued;
398  struct mtx mtx;
399  struct cv cv;
400  struct selinfo sel;
401  struct devq devq;
402  struct proc *async_proc;
403 } devsoftc;
404 
405 static struct cdev *devctl_dev;
406 
407 static void
408 devinit(void)
409 {
410  devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
411  UID_ROOT, GID_WHEEL, 0600, "devctl");
412  mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
413  cv_init(&devsoftc.cv, "dev cv");
414  TAILQ_INIT(&devsoftc.devq);
415 }
416 
417 static int
418 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
419 {
420  if (devsoftc.inuse)
421  return (EBUSY);
422  /* move to init */
423  devsoftc.inuse = 1;
424  devsoftc.nonblock = 0;
425  devsoftc.async_proc = NULL;
426  return (0);
427 }
428 
429 static int
430 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
431 {
432  devsoftc.inuse = 0;
433  mtx_lock(&devsoftc.mtx);
434  cv_broadcast(&devsoftc.cv);
435  mtx_unlock(&devsoftc.mtx);
436  devsoftc.async_proc = NULL;
437  return (0);
438 }
439 
440 /*
441  * The read channel for this device is used to report changes to
442  * userland in realtime. We are required to free the data as well as
443  * the n1 object because we allocate them separately. Also note that
444  * we return one record at a time. If you try to read this device a
445  * character at a time, you will lose the rest of the data. Listening
446  * programs are expected to cope.
447  */
448 static int
449 devread(struct cdev *dev, struct uio *uio, int ioflag)
450 {
451  struct dev_event_info *n1;
452  int rv;
453 
454  mtx_lock(&devsoftc.mtx);
455  while (TAILQ_EMPTY(&devsoftc.devq)) {
456  if (devsoftc.nonblock) {
457  mtx_unlock(&devsoftc.mtx);
458  return (EAGAIN);
459  }
460  rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
461  if (rv) {
462  /*
463  * Need to translate ERESTART to EINTR here? -- jake
464  */
465  mtx_unlock(&devsoftc.mtx);
466  return (rv);
467  }
468  }
469  n1 = TAILQ_FIRST(&devsoftc.devq);
470  TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
471  devsoftc.queued--;
472  mtx_unlock(&devsoftc.mtx);
473  rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
474  free(n1->dei_data, M_BUS);
475  free(n1, M_BUS);
476  return (rv);
477 }
478 
479 static int
480 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
481 {
482  switch (cmd) {
483 
484  case FIONBIO:
485  if (*(int*)data)
486  devsoftc.nonblock = 1;
487  else
488  devsoftc.nonblock = 0;
489  return (0);
490  case FIOASYNC:
491  if (*(int*)data)
492  devsoftc.async_proc = td->td_proc;
493  else
494  devsoftc.async_proc = NULL;
495  return (0);
496 
497  /* (un)Support for other fcntl() calls. */
498  case FIOCLEX:
499  case FIONCLEX:
500  case FIONREAD:
501  case FIOSETOWN:
502  case FIOGETOWN:
503  default:
504  break;
505  }
506  return (ENOTTY);
507 }
508 
509 static int
510 devpoll(struct cdev *dev, int events, struct thread *td)
511 {
512  int revents = 0;
513 
514  mtx_lock(&devsoftc.mtx);
515  if (events & (POLLIN | POLLRDNORM)) {
516  if (!TAILQ_EMPTY(&devsoftc.devq))
517  revents = events & (POLLIN | POLLRDNORM);
518  else
519  selrecord(td, &devsoftc.sel);
520  }
521  mtx_unlock(&devsoftc.mtx);
522 
523  return (revents);
524 }
525 
529 boolean_t
531 {
532  return (devsoftc.inuse == 1);
533 }
534 
542 void
543 devctl_queue_data_f(char *data, int flags)
544 {
545  struct dev_event_info *n1 = NULL, *n2 = NULL;
546  struct proc *p;
547 
548  if (strlen(data) == 0)
549  goto out;
550  if (devctl_queue_length == 0)
551  goto out;
552  n1 = malloc(sizeof(*n1), M_BUS, flags);
553  if (n1 == NULL)
554  goto out;
555  n1->dei_data = data;
556  mtx_lock(&devsoftc.mtx);
557  if (devctl_queue_length == 0) {
558  mtx_unlock(&devsoftc.mtx);
559  free(n1->dei_data, M_BUS);
560  free(n1, M_BUS);
561  return;
562  }
563  /* Leave at least one spot in the queue... */
564  while (devsoftc.queued > devctl_queue_length - 1) {
565  n2 = TAILQ_FIRST(&devsoftc.devq);
566  TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
567  free(n2->dei_data, M_BUS);
568  free(n2, M_BUS);
569  devsoftc.queued--;
570  }
571  TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
572  devsoftc.queued++;
573  cv_broadcast(&devsoftc.cv);
574  mtx_unlock(&devsoftc.mtx);
576  p = devsoftc.async_proc;
577  if (p != NULL) {
578  PROC_LOCK(p);
579  kern_psignal(p, SIGIO);
580  PROC_UNLOCK(p);
581  }
582  return;
583 out:
584  /*
585  * We have to free data on all error paths since the caller
586  * assumes it will be free'd when this item is dequeued.
587  */
588  free(data, M_BUS);
589  return;
590 }
591 
592 void
593 devctl_queue_data(char *data)
594 {
595 
596  devctl_queue_data_f(data, M_NOWAIT);
597 }
598 
602 void
603 devctl_notify_f(const char *system, const char *subsystem, const char *type,
604  const char *data, int flags)
605 {
606  int len = 0;
607  char *msg;
608 
609  if (system == NULL)
610  return; /* BOGUS! Must specify system. */
611  if (subsystem == NULL)
612  return; /* BOGUS! Must specify subsystem. */
613  if (type == NULL)
614  return; /* BOGUS! Must specify type. */
615  len += strlen(" system=") + strlen(system);
616  len += strlen(" subsystem=") + strlen(subsystem);
617  len += strlen(" type=") + strlen(type);
618  /* add in the data message plus newline. */
619  if (data != NULL)
620  len += strlen(data);
621  len += 3; /* '!', '\n', and NUL */
622  msg = malloc(len, M_BUS, flags);
623  if (msg == NULL)
624  return; /* Drop it on the floor */
625  if (data != NULL)
626  snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
627  system, subsystem, type, data);
628  else
629  snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
630  system, subsystem, type);
631  devctl_queue_data_f(msg, flags);
632 }
633 
634 void
635 devctl_notify(const char *system, const char *subsystem, const char *type,
636  const char *data)
637 {
638 
639  devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
640 }
641 
642 /*
643  * Common routine that tries to make sending messages as easy as possible.
644  * We allocate memory for the data, copy strings into that, but do not
645  * free it unless there's an error. The dequeue part of the driver should
646  * free the data. We don't send data when the device is disabled. We do
647  * send data, even when we have no listeners, because we wish to avoid
648  * races relating to startup and restart of listening applications.
649  *
650  * devaddq is designed to string together the type of event, with the
651  * object of that event, plus the plug and play info and location info
652  * for that event. This is likely most useful for devices, but less
653  * useful for other consumers of this interface. Those should use
654  * the devctl_queue_data() interface instead.
655  */
656 static void
657 devaddq(const char *type, const char *what, device_t dev)
658 {
659  char *data = NULL;
660  char *loc = NULL;
661  char *pnp = NULL;
662  const char *parstr;
663 
664  if (!devctl_queue_length)/* Rare race, but lost races safely discard */
665  return;
666  data = malloc(1024, M_BUS, M_NOWAIT);
667  if (data == NULL)
668  goto bad;
669 
670  /* get the bus specific location of this device */
671  loc = malloc(1024, M_BUS, M_NOWAIT);
672  if (loc == NULL)
673  goto bad;
674  *loc = '\0';
675  bus_child_location_str(dev, loc, 1024);
676 
677  /* Get the bus specific pnp info of this device */
678  pnp = malloc(1024, M_BUS, M_NOWAIT);
679  if (pnp == NULL)
680  goto bad;
681  *pnp = '\0';
682  bus_child_pnpinfo_str(dev, pnp, 1024);
683 
684  /* Get the parent of this device, or / if high enough in the tree. */
685  if (device_get_parent(dev) == NULL)
686  parstr = "."; /* Or '/' ? */
687  else
688  parstr = device_get_nameunit(device_get_parent(dev));
689  /* String it all together. */
690  snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
691  parstr);
692  free(loc, M_BUS);
693  free(pnp, M_BUS);
694  devctl_queue_data(data);
695  return;
696 bad:
697  free(pnp, M_BUS);
698  free(loc, M_BUS);
699  free(data, M_BUS);
700  return;
701 }
702 
703 /*
704  * A device was added to the tree. We are called just after it successfully
705  * attaches (that is, probe and attach success for this device). No call
706  * is made if a device is merely parented into the tree. See devnomatch
707  * if probe fails. If attach fails, no notification is sent (but maybe
708  * we should have a different message for this).
709  */
710 static void
711 devadded(device_t dev)
712 {
713  devaddq("+", device_get_nameunit(dev), dev);
714 }
715 
716 /*
717  * A device was removed from the tree. We are called just before this
718  * happens.
719  */
720 static void
721 devremoved(device_t dev)
722 {
723  devaddq("-", device_get_nameunit(dev), dev);
724 }
725 
726 /*
727  * Called when there's no match for this device. This is only called
728  * the first time that no match happens, so we don't keep getting this
729  * message. Should that prove to be undesirable, we can change it.
730  * This is called when all drivers that can attach to a given bus
731  * decline to accept this device. Other errors may not be detected.
732  */
733 static void
734 devnomatch(device_t dev)
735 {
736  devaddq("?", "", dev);
737 }
738 
739 static int
740 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
741 {
742  struct dev_event_info *n1;
743  int dis, error;
744 
745  dis = devctl_queue_length == 0;
746  error = sysctl_handle_int(oidp, &dis, 0, req);
747  if (error || !req->newptr)
748  return (error);
749  mtx_lock(&devsoftc.mtx);
750  if (dis) {
751  while (!TAILQ_EMPTY(&devsoftc.devq)) {
752  n1 = TAILQ_FIRST(&devsoftc.devq);
753  TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
754  free(n1->dei_data, M_BUS);
755  free(n1, M_BUS);
756  }
757  devsoftc.queued = 0;
758  devctl_queue_length = 0;
759  } else {
760  devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
761  }
762  mtx_unlock(&devsoftc.mtx);
763  return (0);
764 }
765 
766 static int
767 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
768 {
769  struct dev_event_info *n1;
770  int q, error;
771 
773  error = sysctl_handle_int(oidp, &q, 0, req);
774  if (error || !req->newptr)
775  return (error);
776  if (q < 0)
777  return (EINVAL);
778  mtx_lock(&devsoftc.mtx);
779  devctl_queue_length = q;
780  while (devsoftc.queued > devctl_queue_length) {
781  n1 = TAILQ_FIRST(&devsoftc.devq);
782  TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
783  free(n1->dei_data, M_BUS);
784  free(n1, M_BUS);
785  devsoftc.queued--;
786  }
787  mtx_unlock(&devsoftc.mtx);
788  return (0);
789 }
790 
791 /* End of /dev/devctl code */
792 
793 static TAILQ_HEAD(,device) bus_data_devices;
794 static int bus_data_generation = 1;
795 
796 static kobj_method_t null_methods[] = {
797  KOBJMETHOD_END
798 };
799 
800 DEFINE_CLASS(null, null_methods, 0);
801 
802 /*
803  * Bus pass implementation
804  */
805 
806 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
807 int bus_current_pass = BUS_PASS_ROOT;
808 
819 static void
821 {
822  struct driverlink *dl;
823 
824  /* We only consider pass numbers during boot. */
825  if (bus_current_pass == BUS_PASS_DEFAULT)
826  return;
827 
828  /*
829  * Walk the passes list. If we already know about this pass
830  * then there is nothing to do. If we don't, then insert this
831  * driver link into the list.
832  */
833  TAILQ_FOREACH(dl, &passes, passlink) {
834  if (dl->pass < new->pass)
835  continue;
836  if (dl->pass == new->pass)
837  return;
838  TAILQ_INSERT_BEFORE(dl, new, passlink);
839  return;
840  }
841  TAILQ_INSERT_TAIL(&passes, new, passlink);
842 }
843 
851 void
852 bus_set_pass(int pass)
853 {
854  struct driverlink *dl;
855 
856  if (bus_current_pass > pass)
857  panic("Attempt to lower bus pass level");
858 
859  TAILQ_FOREACH(dl, &passes, passlink) {
860  /* Skip pass values below the current pass level. */
861  if (dl->pass <= bus_current_pass)
862  continue;
863 
864  /*
865  * Bail once we hit a driver with a pass level that is
866  * too high.
867  */
868  if (dl->pass > pass)
869  break;
870 
871  /*
872  * Raise the pass level to the next level and rescan
873  * the tree.
874  */
875  bus_current_pass = dl->pass;
876  BUS_NEW_PASS(root_bus);
877  }
878 
879  /*
880  * If there isn't a driver registered for the requested pass,
881  * then bus_current_pass might still be less than 'pass'. Set
882  * it to 'pass' in that case.
883  */
884  if (bus_current_pass < pass)
885  bus_current_pass = pass;
886  KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
887 }
888 
889 /*
890  * Devclass implementation
891  */
892 
893 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
894 
910 static devclass_t
911 devclass_find_internal(const char *classname, const char *parentname,
912  int create)
913 {
914  devclass_t dc;
915 
916  PDEBUG(("looking for %s", classname));
917  if (!classname)
918  return (NULL);
919 
920  TAILQ_FOREACH(dc, &devclasses, link) {
921  if (!strcmp(dc->name, classname))
922  break;
923  }
924 
925  if (create && !dc) {
926  PDEBUG(("creating %s", classname));
927  dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
928  M_BUS, M_NOWAIT | M_ZERO);
929  if (!dc)
930  return (NULL);
931  dc->parent = NULL;
932  dc->name = (char*) (dc + 1);
933  strcpy(dc->name, classname);
934  TAILQ_INIT(&dc->drivers);
935  TAILQ_INSERT_TAIL(&devclasses, dc, link);
936 
938  }
939 
940  /*
941  * If a parent class is specified, then set that as our parent so
942  * that this devclass will support drivers for the parent class as
943  * well. If the parent class has the same name don't do this though
944  * as it creates a cycle that can trigger an infinite loop in
945  * device_probe_child() if a device exists for which there is no
946  * suitable driver.
947  */
948  if (parentname && dc && !dc->parent &&
949  strcmp(classname, parentname) != 0) {
950  dc->parent = devclass_find_internal(parentname, NULL, TRUE);
951  dc->parent->flags |= DC_HAS_CHILDREN;
952  }
953 
954  return (dc);
955 }
956 
965 devclass_t
966 devclass_create(const char *classname)
967 {
968  return (devclass_find_internal(classname, NULL, TRUE));
969 }
970 
979 devclass_t
980 devclass_find(const char *classname)
981 {
982  return (devclass_find_internal(classname, NULL, FALSE));
983 }
984 
1002 static void
1003 devclass_driver_added(devclass_t dc, driver_t *driver)
1004 {
1005  devclass_t parent;
1006  int i;
1007 
1008  /*
1009  * Call BUS_DRIVER_ADDED for any existing busses in this class.
1010  */
1011  for (i = 0; i < dc->maxunit; i++)
1012  if (dc->devices[i] && device_is_attached(dc->devices[i]))
1013  BUS_DRIVER_ADDED(dc->devices[i], driver);
1014 
1015  /*
1016  * Walk through the children classes. Since we only keep a
1017  * single parent pointer around, we walk the entire list of
1018  * devclasses looking for children. We set the
1019  * DC_HAS_CHILDREN flag when a child devclass is created on
1020  * the parent, so we only walk the list for those devclasses
1021  * that have children.
1022  */
1023  if (!(dc->flags & DC_HAS_CHILDREN))
1024  return;
1025  parent = dc;
1026  TAILQ_FOREACH(dc, &devclasses, link) {
1027  if (dc->parent == parent)
1028  devclass_driver_added(dc, driver);
1029  }
1030 }
1031 
1043 int
1044 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1045 {
1046  driverlink_t dl;
1047  const char *parentname;
1048 
1049  PDEBUG(("%s", DRIVERNAME(driver)));
1050 
1051  /* Don't allow invalid pass values. */
1052  if (pass <= BUS_PASS_ROOT)
1053  return (EINVAL);
1054 
1055  dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1056  if (!dl)
1057  return (ENOMEM);
1058 
1059  /*
1060  * Compile the driver's methods. Also increase the reference count
1061  * so that the class doesn't get freed when the last instance
1062  * goes. This means we can safely use static methods and avoids a
1063  * double-free in devclass_delete_driver.
1064  */
1065  kobj_class_compile((kobj_class_t) driver);
1066 
1067  /*
1068  * If the driver has any base classes, make the
1069  * devclass inherit from the devclass of the driver's
1070  * first base class. This will allow the system to
1071  * search for drivers in both devclasses for children
1072  * of a device using this driver.
1073  */
1074  if (driver->baseclasses)
1075  parentname = driver->baseclasses[0]->name;
1076  else
1077  parentname = NULL;
1078  *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1079 
1080  dl->driver = driver;
1081  TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1082  driver->refs++; /* XXX: kobj_mtx */
1083  dl->pass = pass;
1085 
1086  devclass_driver_added(dc, driver);
1088  return (0);
1089 }
1090 
1110 static int
1111 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1112 {
1113  devclass_t parent;
1114  device_t dev;
1115  int error, i;
1116 
1117  /*
1118  * Disassociate from any devices. We iterate through all the
1119  * devices in the devclass of the driver and detach any which are
1120  * using the driver and which have a parent in the devclass which
1121  * we are deleting from.
1122  *
1123  * Note that since a driver can be in multiple devclasses, we
1124  * should not detach devices which are not children of devices in
1125  * the affected devclass.
1126  */
1127  for (i = 0; i < dc->maxunit; i++) {
1128  if (dc->devices[i]) {
1129  dev = dc->devices[i];
1130  if (dev->driver == driver && dev->parent &&
1131  dev->parent->devclass == busclass) {
1132  if ((error = device_detach(dev)) != 0)
1133  return (error);
1134  BUS_PROBE_NOMATCH(dev->parent, dev);
1135  devnomatch(dev);
1136  dev->flags |= DF_DONENOMATCH;
1137  }
1138  }
1139  }
1140 
1141  /*
1142  * Walk through the children classes. Since we only keep a
1143  * single parent pointer around, we walk the entire list of
1144  * devclasses looking for children. We set the
1145  * DC_HAS_CHILDREN flag when a child devclass is created on
1146  * the parent, so we only walk the list for those devclasses
1147  * that have children.
1148  */
1149  if (!(busclass->flags & DC_HAS_CHILDREN))
1150  return (0);
1151  parent = busclass;
1152  TAILQ_FOREACH(busclass, &devclasses, link) {
1153  if (busclass->parent == parent) {
1154  error = devclass_driver_deleted(busclass, dc, driver);
1155  if (error)
1156  return (error);
1157  }
1158  }
1159  return (0);
1160 }
1161 
1176 int
1177 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1178 {
1179  devclass_t dc = devclass_find(driver->name);
1180  driverlink_t dl;
1181  int error;
1182 
1183  PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1184 
1185  if (!dc)
1186  return (0);
1187 
1188  /*
1189  * Find the link structure in the bus' list of drivers.
1190  */
1191  TAILQ_FOREACH(dl, &busclass->drivers, link) {
1192  if (dl->driver == driver)
1193  break;
1194  }
1195 
1196  if (!dl) {
1197  PDEBUG(("%s not found in %s list", driver->name,
1198  busclass->name));
1199  return (ENOENT);
1200  }
1201 
1202  error = devclass_driver_deleted(busclass, dc, driver);
1203  if (error != 0)
1204  return (error);
1205 
1206  TAILQ_REMOVE(&busclass->drivers, dl, link);
1207  free(dl, M_BUS);
1208 
1209  /* XXX: kobj_mtx */
1210  driver->refs--;
1211  if (driver->refs == 0)
1212  kobj_class_free((kobj_class_t) driver);
1213 
1215  return (0);
1216 }
1217 
1231 static int
1232 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1233 {
1234  devclass_t dc = devclass_find(driver->name);
1235  driverlink_t dl;
1236  device_t dev;
1237  int i;
1238  int error;
1239 
1240  PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1241 
1242  if (!dc)
1243  return (0);
1244 
1245  /*
1246  * Find the link structure in the bus' list of drivers.
1247  */
1248  TAILQ_FOREACH(dl, &busclass->drivers, link) {
1249  if (dl->driver == driver)
1250  break;
1251  }
1252 
1253  if (!dl) {
1254  PDEBUG(("%s not found in %s list", driver->name,
1255  busclass->name));
1256  return (ENOENT);
1257  }
1258 
1259  /*
1260  * Quiesce all devices. We iterate through all the devices in
1261  * the devclass of the driver and quiesce any which are using
1262  * the driver and which have a parent in the devclass which we
1263  * are quiescing.
1264  *
1265  * Note that since a driver can be in multiple devclasses, we
1266  * should not quiesce devices which are not children of
1267  * devices in the affected devclass.
1268  */
1269  for (i = 0; i < dc->maxunit; i++) {
1270  if (dc->devices[i]) {
1271  dev = dc->devices[i];
1272  if (dev->driver == driver && dev->parent &&
1273  dev->parent->devclass == busclass) {
1274  if ((error = device_quiesce(dev)) != 0)
1275  return (error);
1276  }
1277  }
1278  }
1279 
1280  return (0);
1281 }
1282 
1286 static driverlink_t
1287 devclass_find_driver_internal(devclass_t dc, const char *classname)
1288 {
1289  driverlink_t dl;
1290 
1291  PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1292 
1293  TAILQ_FOREACH(dl, &dc->drivers, link) {
1294  if (!strcmp(dl->driver->name, classname))
1295  return (dl);
1296  }
1297 
1298  PDEBUG(("not found"));
1299  return (NULL);
1300 }
1301 
1305 const char *
1306 devclass_get_name(devclass_t dc)
1307 {
1308  return (dc->name);
1309 }
1310 
1320 device_t
1321 devclass_get_device(devclass_t dc, int unit)
1322 {
1323  if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1324  return (NULL);
1325  return (dc->devices[unit]);
1326 }
1327 
1338 void *
1339 devclass_get_softc(devclass_t dc, int unit)
1340 {
1341  device_t dev;
1342 
1343  dev = devclass_get_device(dc, unit);
1344  if (!dev)
1345  return (NULL);
1346 
1347  return (device_get_softc(dev));
1348 }
1349 
1366 int
1367 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1368 {
1369  int count, i;
1370  device_t *list;
1371 
1372  count = devclass_get_count(dc);
1373  list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1374  if (!list)
1375  return (ENOMEM);
1376 
1377  count = 0;
1378  for (i = 0; i < dc->maxunit; i++) {
1379  if (dc->devices[i]) {
1380  list[count] = dc->devices[i];
1381  count++;
1382  }
1383  }
1384 
1385  *devlistp = list;
1386  *devcountp = count;
1387 
1388  return (0);
1389 }
1390 
1407 int
1408 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1409 {
1410  driverlink_t dl;
1411  driver_t **list;
1412  int count;
1413 
1414  count = 0;
1415  TAILQ_FOREACH(dl, &dc->drivers, link)
1416  count++;
1417  list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1418  if (list == NULL)
1419  return (ENOMEM);
1420 
1421  count = 0;
1422  TAILQ_FOREACH(dl, &dc->drivers, link) {
1423  list[count] = dl->driver;
1424  count++;
1425  }
1426  *listp = list;
1427  *countp = count;
1428 
1429  return (0);
1430 }
1431 
1437 int
1438 devclass_get_count(devclass_t dc)
1439 {
1440  int count, i;
1441 
1442  count = 0;
1443  for (i = 0; i < dc->maxunit; i++)
1444  if (dc->devices[i])
1445  count++;
1446  return (count);
1447 }
1448 
1458 int
1459 devclass_get_maxunit(devclass_t dc)
1460 {
1461  if (dc == NULL)
1462  return (-1);
1463  return (dc->maxunit);
1464 }
1465 
1475 int
1476 devclass_find_free_unit(devclass_t dc, int unit)
1477 {
1478  if (dc == NULL)
1479  return (unit);
1480  while (unit < dc->maxunit && dc->devices[unit] != NULL)
1481  unit++;
1482  return (unit);
1483 }
1484 
1494 void
1495 devclass_set_parent(devclass_t dc, devclass_t pdc)
1496 {
1497  dc->parent = pdc;
1498 }
1499 
1505 devclass_t
1506 devclass_get_parent(devclass_t dc)
1507 {
1508  return (dc->parent);
1509 }
1510 
1511 struct sysctl_ctx_list *
1513 {
1514  return (&dc->sysctl_ctx);
1515 }
1516 
1517 struct sysctl_oid *
1519 {
1520  return (dc->sysctl_tree);
1521 }
1522 
1538 static int
1539 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1540 {
1541  const char *s;
1542  int unit = *unitp;
1543 
1544  PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1545 
1546  /* Ask the parent bus if it wants to wire this device. */
1547  if (unit == -1)
1548  BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1549  &unit);
1550 
1551  /* If we were given a wired unit number, check for existing device */
1552  /* XXX imp XXX */
1553  if (unit != -1) {
1554  if (unit >= 0 && unit < dc->maxunit &&
1555  dc->devices[unit] != NULL) {
1556  if (bootverbose)
1557  printf("%s: %s%d already exists; skipping it\n",
1558  dc->name, dc->name, *unitp);
1559  return (EEXIST);
1560  }
1561  } else {
1562  /* Unwired device, find the next available slot for it */
1563  unit = 0;
1564  for (unit = 0;; unit++) {
1565  /* If there is an "at" hint for a unit then skip it. */
1566  if (resource_string_value(dc->name, unit, "at", &s) ==
1567  0)
1568  continue;
1569 
1570  /* If this device slot is already in use, skip it. */
1571  if (unit < dc->maxunit && dc->devices[unit] != NULL)
1572  continue;
1573 
1574  break;
1575  }
1576  }
1577 
1578  /*
1579  * We've selected a unit beyond the length of the table, so let's
1580  * extend the table to make room for all units up to and including
1581  * this one.
1582  */
1583  if (unit >= dc->maxunit) {
1584  device_t *newlist, *oldlist;
1585  int newsize;
1586 
1587  oldlist = dc->devices;
1588  newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1589  newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1590  if (!newlist)
1591  return (ENOMEM);
1592  if (oldlist != NULL)
1593  bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1594  bzero(newlist + dc->maxunit,
1595  sizeof(device_t) * (newsize - dc->maxunit));
1596  dc->devices = newlist;
1597  dc->maxunit = newsize;
1598  if (oldlist != NULL)
1599  free(oldlist, M_BUS);
1600  }
1601  PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1602 
1603  *unitp = unit;
1604  return (0);
1605 }
1606 
1623 static int
1624 devclass_add_device(devclass_t dc, device_t dev)
1625 {
1626  int buflen, error;
1627 
1628  PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1629 
1630  buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1631  if (buflen < 0)
1632  return (ENOMEM);
1633  dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1634  if (!dev->nameunit)
1635  return (ENOMEM);
1636 
1637  if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1638  free(dev->nameunit, M_BUS);
1639  dev->nameunit = NULL;
1640  return (error);
1641  }
1642  dc->devices[dev->unit] = dev;
1643  dev->devclass = dc;
1644  snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1645 
1646  return (0);
1647 }
1648 
1661 static int
1662 devclass_delete_device(devclass_t dc, device_t dev)
1663 {
1664  if (!dc || !dev)
1665  return (0);
1666 
1667  PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1668 
1669  if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1670  panic("devclass_delete_device: inconsistent device class");
1671  dc->devices[dev->unit] = NULL;
1672  if (dev->flags & DF_WILDCARD)
1673  dev->unit = -1;
1674  dev->devclass = NULL;
1675  free(dev->nameunit, M_BUS);
1676  dev->nameunit = NULL;
1677 
1678  return (0);
1679 }
1680 
1693 static device_t
1694 make_device(device_t parent, const char *name, int unit)
1695 {
1696  device_t dev;
1697  devclass_t dc;
1698 
1699  PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1700 
1701  if (name) {
1702  dc = devclass_find_internal(name, NULL, TRUE);
1703  if (!dc) {
1704  printf("make_device: can't find device class %s\n",
1705  name);
1706  return (NULL);
1707  }
1708  } else {
1709  dc = NULL;
1710  }
1711 
1712  dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1713  if (!dev)
1714  return (NULL);
1715 
1716  dev->parent = parent;
1717  TAILQ_INIT(&dev->children);
1718  kobj_init((kobj_t) dev, &null_class);
1719  dev->driver = NULL;
1720  dev->devclass = NULL;
1721  dev->unit = unit;
1722  dev->nameunit = NULL;
1723  dev->desc = NULL;
1724  dev->busy = 0;
1725  dev->devflags = 0;
1726  dev->flags = DF_ENABLED;
1727  dev->order = 0;
1728  if (unit == -1)
1729  dev->flags |= DF_WILDCARD;
1730  if (name) {
1731  dev->flags |= DF_FIXEDCLASS;
1732  if (devclass_add_device(dc, dev)) {
1733  kobj_delete((kobj_t) dev, M_BUS);
1734  return (NULL);
1735  }
1736  }
1737  dev->ivars = NULL;
1738  dev->softc = NULL;
1739 
1740  dev->state = DS_NOTPRESENT;
1741 
1742  TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1744 
1745  return (dev);
1746 }
1747 
1752 static int
1753 device_print_child(device_t dev, device_t child)
1754 {
1755  int retval = 0;
1756 
1757  if (device_is_alive(child))
1758  retval += BUS_PRINT_CHILD(dev, child);
1759  else
1760  retval += device_printf(child, " not found\n");
1761 
1762  return (retval);
1763 }
1764 
1781 device_t
1782 device_add_child(device_t dev, const char *name, int unit)
1783 {
1784  return (device_add_child_ordered(dev, 0, name, unit));
1785 }
1786 
1807 device_t
1808 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1809 {
1810  device_t child;
1811  device_t place;
1812 
1813  PDEBUG(("%s at %s with order %u as unit %d",
1814  name, DEVICENAME(dev), order, unit));
1815  KASSERT(name != NULL || unit == -1,
1816  ("child device with wildcard name and specific unit number"));
1817 
1818  child = make_device(dev, name, unit);
1819  if (child == NULL)
1820  return (child);
1821  child->order = order;
1822 
1823  TAILQ_FOREACH(place, &dev->children, link) {
1824  if (place->order > order)
1825  break;
1826  }
1827 
1828  if (place) {
1829  /*
1830  * The device 'place' is the first device whose order is
1831  * greater than the new child.
1832  */
1833  TAILQ_INSERT_BEFORE(place, child, link);
1834  } else {
1835  /*
1836  * The new child's order is greater or equal to the order of
1837  * any existing device. Add the child to the tail of the list.
1838  */
1839  TAILQ_INSERT_TAIL(&dev->children, child, link);
1840  }
1841 
1843  return (child);
1844 }
1845 
1859 int
1860 device_delete_child(device_t dev, device_t child)
1861 {
1862  int error;
1863  device_t grandchild;
1864 
1865  PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1866 
1867  /* remove children first */
1868  while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1869  error = device_delete_child(child, grandchild);
1870  if (error)
1871  return (error);
1872  }
1873 
1874  if ((error = device_detach(child)) != 0)
1875  return (error);
1876  if (child->devclass)
1877  devclass_delete_device(child->devclass, child);
1878  if (child->parent)
1879  BUS_CHILD_DELETED(dev, child);
1880  TAILQ_REMOVE(&dev->children, child, link);
1881  TAILQ_REMOVE(&bus_data_devices, child, devlink);
1882  kobj_delete((kobj_t) child, M_BUS);
1883 
1885  return (0);
1886 }
1887 
1901 int
1903 {
1904  device_t child;
1905  int error;
1906 
1907  PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1908 
1909  error = 0;
1910 
1911  while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1912  error = device_delete_child(dev, child);
1913  if (error) {
1914  PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1915  break;
1916  }
1917  }
1918  return (error);
1919 }
1920 
1935 device_t
1936 device_find_child(device_t dev, const char *classname, int unit)
1937 {
1938  devclass_t dc;
1939  device_t child;
1940 
1941  dc = devclass_find(classname);
1942  if (!dc)
1943  return (NULL);
1944 
1945  if (unit != -1) {
1946  child = devclass_get_device(dc, unit);
1947  if (child && child->parent == dev)
1948  return (child);
1949  } else {
1950  for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1951  child = devclass_get_device(dc, unit);
1952  if (child && child->parent == dev)
1953  return (child);
1954  }
1955  }
1956  return (NULL);
1957 }
1958 
1962 static driverlink_t
1963 first_matching_driver(devclass_t dc, device_t dev)
1964 {
1965  if (dev->devclass)
1966  return (devclass_find_driver_internal(dc, dev->devclass->name));
1967  return (TAILQ_FIRST(&dc->drivers));
1968 }
1969 
1973 static driverlink_t
1974 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1975 {
1976  if (dev->devclass) {
1977  driverlink_t dl;
1978  for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1979  if (!strcmp(dev->devclass->name, dl->driver->name))
1980  return (dl);
1981  return (NULL);
1982  }
1983  return (TAILQ_NEXT(last, link));
1984 }
1985 
1989 int
1990 device_probe_child(device_t dev, device_t child)
1991 {
1992  devclass_t dc;
1993  driverlink_t best = NULL;
1994  driverlink_t dl;
1995  int result, pri = 0;
1996  int hasclass = (child->devclass != NULL);
1997 
1998  GIANT_REQUIRED;
1999 
2000  dc = dev->devclass;
2001  if (!dc)
2002  panic("device_probe_child: parent device has no devclass");
2003 
2004  /*
2005  * If the state is already probed, then return. However, don't
2006  * return if we can rebid this object.
2007  */
2008  if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2009  return (0);
2010 
2011  for (; dc; dc = dc->parent) {
2012  for (dl = first_matching_driver(dc, child);
2013  dl;
2014  dl = next_matching_driver(dc, child, dl)) {
2015  /* If this driver's pass is too high, then ignore it. */
2016  if (dl->pass > bus_current_pass)
2017  continue;
2018 
2019  PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2020  result = device_set_driver(child, dl->driver);
2021  if (result == ENOMEM)
2022  return (result);
2023  else if (result != 0)
2024  continue;
2025  if (!hasclass) {
2026  if (device_set_devclass(child,
2027  dl->driver->name) != 0) {
2028  printf("driver bug: Unable to set "
2029  "devclass (devname: %s)\n",
2030  device_get_name(child));
2031  (void)device_set_driver(child, NULL);
2032  continue;
2033  }
2034  }
2035 
2036  /* Fetch any flags for the device before probing. */
2037  resource_int_value(dl->driver->name, child->unit,
2038  "flags", &child->devflags);
2039 
2040  result = DEVICE_PROBE(child);
2041 
2042  /* Reset flags and devclass before the next probe. */
2043  child->devflags = 0;
2044  if (!hasclass)
2045  (void)device_set_devclass(child, NULL);
2046 
2047  /*
2048  * If the driver returns SUCCESS, there can be
2049  * no higher match for this device.
2050  */
2051  if (result == 0) {
2052  best = dl;
2053  pri = 0;
2054  break;
2055  }
2056 
2057  /*
2058  * The driver returned an error so it
2059  * certainly doesn't match.
2060  */
2061  if (result > 0) {
2062  (void)device_set_driver(child, NULL);
2063  continue;
2064  }
2065 
2066  /*
2067  * A priority lower than SUCCESS, remember the
2068  * best matching driver. Initialise the value
2069  * of pri for the first match.
2070  */
2071  if (best == NULL || result > pri) {
2072  /*
2073  * Probes that return BUS_PROBE_NOWILDCARD
2074  * or lower only match when they are set
2075  * in stone by the parent bus.
2076  */
2077  if (result <= BUS_PROBE_NOWILDCARD &&
2078  child->flags & DF_WILDCARD)
2079  continue;
2080  best = dl;
2081  pri = result;
2082  continue;
2083  }
2084  }
2085  /*
2086  * If we have an unambiguous match in this devclass,
2087  * don't look in the parent.
2088  */
2089  if (best && pri == 0)
2090  break;
2091  }
2092 
2093  /*
2094  * If we found a driver, change state and initialise the devclass.
2095  */
2096  /* XXX What happens if we rebid and got no best? */
2097  if (best) {
2098  /*
2099  * If this device was attached, and we were asked to
2100  * rescan, and it is a different driver, then we have
2101  * to detach the old driver and reattach this new one.
2102  * Note, we don't have to check for DF_REBID here
2103  * because if the state is > DS_ALIVE, we know it must
2104  * be.
2105  *
2106  * This assumes that all DF_REBID drivers can have
2107  * their probe routine called at any time and that
2108  * they are idempotent as well as completely benign in
2109  * normal operations.
2110  *
2111  * We also have to make sure that the detach
2112  * succeeded, otherwise we fail the operation (or
2113  * maybe it should just fail silently? I'm torn).
2114  */
2115  if (child->state > DS_ALIVE && best->driver != child->driver)
2116  if ((result = device_detach(dev)) != 0)
2117  return (result);
2118 
2119  /* Set the winning driver, devclass, and flags. */
2120  if (!child->devclass) {
2121  result = device_set_devclass(child, best->driver->name);
2122  if (result != 0)
2123  return (result);
2124  }
2125  result = device_set_driver(child, best->driver);
2126  if (result != 0)
2127  return (result);
2128  resource_int_value(best->driver->name, child->unit,
2129  "flags", &child->devflags);
2130 
2131  if (pri < 0) {
2132  /*
2133  * A bit bogus. Call the probe method again to make
2134  * sure that we have the right description.
2135  */
2136  DEVICE_PROBE(child);
2137 #if 0
2138  child->flags |= DF_REBID;
2139 #endif
2140  } else
2141  child->flags &= ~DF_REBID;
2142  child->state = DS_ALIVE;
2143 
2145  return (0);
2146  }
2147 
2148  return (ENXIO);
2149 }
2150 
2154 device_t
2155 device_get_parent(device_t dev)
2156 {
2157  return (dev->parent);
2158 }
2159 
2176 int
2177 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2178 {
2179  int count;
2180  device_t child;
2181  device_t *list;
2182 
2183  count = 0;
2184  TAILQ_FOREACH(child, &dev->children, link) {
2185  count++;
2186  }
2187  if (count == 0) {
2188  *devlistp = NULL;
2189  *devcountp = 0;
2190  return (0);
2191  }
2192 
2193  list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2194  if (!list)
2195  return (ENOMEM);
2196 
2197  count = 0;
2198  TAILQ_FOREACH(child, &dev->children, link) {
2199  list[count] = child;
2200  count++;
2201  }
2202 
2203  *devlistp = list;
2204  *devcountp = count;
2205 
2206  return (0);
2207 }
2208 
2213 driver_t *
2214 device_get_driver(device_t dev)
2215 {
2216  return (dev->driver);
2217 }
2218 
2223 devclass_t
2224 device_get_devclass(device_t dev)
2225 {
2226  return (dev->devclass);
2227 }
2228 
2233 const char *
2234 device_get_name(device_t dev)
2235 {
2236  if (dev != NULL && dev->devclass)
2237  return (devclass_get_name(dev->devclass));
2238  return (NULL);
2239 }
2240 
2246 const char *
2247 device_get_nameunit(device_t dev)
2248 {
2249  return (dev->nameunit);
2250 }
2251 
2255 int
2256 device_get_unit(device_t dev)
2257 {
2258  return (dev->unit);
2259 }
2260 
2264 const char *
2265 device_get_desc(device_t dev)
2266 {
2267  return (dev->desc);
2268 }
2269 
2273 uint32_t
2274 device_get_flags(device_t dev)
2275 {
2276  return (dev->devflags);
2277 }
2278 
2279 struct sysctl_ctx_list *
2281 {
2282  return (&dev->sysctl_ctx);
2283 }
2284 
2285 struct sysctl_oid *
2287 {
2288  return (dev->sysctl_tree);
2289 }
2290 
2296 int
2298 {
2299  const char *name = device_get_name(dev);
2300 
2301  if (name == NULL)
2302  return (printf("unknown: "));
2303  return (printf("%s%d: ", name, device_get_unit(dev)));
2304 }
2305 
2313 int
2314 device_printf(device_t dev, const char * fmt, ...)
2315 {
2316  va_list ap;
2317  int retval;
2318 
2319  retval = device_print_prettyname(dev);
2320  va_start(ap, fmt);
2321  retval += vprintf(fmt, ap);
2322  va_end(ap);
2323  return (retval);
2324 }
2325 
2329 static void
2330 device_set_desc_internal(device_t dev, const char* desc, int copy)
2331 {
2332  if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2333  free(dev->desc, M_BUS);
2334  dev->flags &= ~DF_DESCMALLOCED;
2335  dev->desc = NULL;
2336  }
2337 
2338  if (copy && desc) {
2339  dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2340  if (dev->desc) {
2341  strcpy(dev->desc, desc);
2342  dev->flags |= DF_DESCMALLOCED;
2343  }
2344  } else {
2345  /* Avoid a -Wcast-qual warning */
2346  dev->desc = (char *)(uintptr_t) desc;
2347  }
2348 
2350 }
2351 
2359 void
2360 device_set_desc(device_t dev, const char* desc)
2361 {
2362  device_set_desc_internal(dev, desc, FALSE);
2363 }
2364 
2371 void
2372 device_set_desc_copy(device_t dev, const char* desc)
2373 {
2374  device_set_desc_internal(dev, desc, TRUE);
2375 }
2376 
2380 void
2381 device_set_flags(device_t dev, uint32_t flags)
2382 {
2383  dev->devflags = flags;
2384 }
2385 
2392 void *
2393 device_get_softc(device_t dev)
2394 {
2395  return (dev->softc);
2396 }
2397 
2404 void
2405 device_set_softc(device_t dev, void *softc)
2406 {
2407  if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2408  free(dev->softc, M_BUS_SC);
2409  dev->softc = softc;
2410  if (dev->softc)
2411  dev->flags |= DF_EXTERNALSOFTC;
2412  else
2413  dev->flags &= ~DF_EXTERNALSOFTC;
2414 }
2415 
2422 void
2423 device_free_softc(void *softc)
2424 {
2425  free(softc, M_BUS_SC);
2426 }
2427 
2436 void
2437 device_claim_softc(device_t dev)
2438 {
2439  if (dev->softc)
2440  dev->flags |= DF_EXTERNALSOFTC;
2441  else
2442  dev->flags &= ~DF_EXTERNALSOFTC;
2443 }
2444 
2452 void *
2453 device_get_ivars(device_t dev)
2454 {
2455 
2456  KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2457  return (dev->ivars);
2458 }
2459 
2463 void
2464 device_set_ivars(device_t dev, void * ivars)
2465 {
2466 
2467  KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2468  dev->ivars = ivars;
2469 }
2470 
2474 device_state_t
2475 device_get_state(device_t dev)
2476 {
2477  return (dev->state);
2478 }
2479 
2483 void
2484 device_enable(device_t dev)
2485 {
2486  dev->flags |= DF_ENABLED;
2487 }
2488 
2492 void
2493 device_disable(device_t dev)
2494 {
2495  dev->flags &= ~DF_ENABLED;
2496 }
2497 
2501 void
2502 device_busy(device_t dev)
2503 {
2504  if (dev->state < DS_ATTACHING)
2505  panic("device_busy: called for unattached device");
2506  if (dev->busy == 0 && dev->parent)
2507  device_busy(dev->parent);
2508  dev->busy++;
2509  if (dev->state == DS_ATTACHED)
2510  dev->state = DS_BUSY;
2511 }
2512 
2516 void
2517 device_unbusy(device_t dev)
2518 {
2519  if (dev->busy != 0 && dev->state != DS_BUSY &&
2520  dev->state != DS_ATTACHING)
2521  panic("device_unbusy: called for non-busy device %s",
2522  device_get_nameunit(dev));
2523  dev->busy--;
2524  if (dev->busy == 0) {
2525  if (dev->parent)
2526  device_unbusy(dev->parent);
2527  if (dev->state == DS_BUSY)
2528  dev->state = DS_ATTACHED;
2529  }
2530 }
2531 
2535 void
2536 device_quiet(device_t dev)
2537 {
2538  dev->flags |= DF_QUIET;
2539 }
2540 
2544 void
2545 device_verbose(device_t dev)
2546 {
2547  dev->flags &= ~DF_QUIET;
2548 }
2549 
2553 int
2554 device_is_quiet(device_t dev)
2555 {
2556  return ((dev->flags & DF_QUIET) != 0);
2557 }
2558 
2562 int
2563 device_is_enabled(device_t dev)
2564 {
2565  return ((dev->flags & DF_ENABLED) != 0);
2566 }
2567 
2571 int
2572 device_is_alive(device_t dev)
2573 {
2574  return (dev->state >= DS_ALIVE);
2575 }
2576 
2581 int
2582 device_is_attached(device_t dev)
2583 {
2584  return (dev->state >= DS_ATTACHED);
2585 }
2586 
2591 int
2592 device_set_devclass(device_t dev, const char *classname)
2593 {
2594  devclass_t dc;
2595  int error;
2596 
2597  if (!classname) {
2598  if (dev->devclass)
2599  devclass_delete_device(dev->devclass, dev);
2600  return (0);
2601  }
2602 
2603  if (dev->devclass) {
2604  printf("device_set_devclass: device class already set\n");
2605  return (EINVAL);
2606  }
2607 
2608  dc = devclass_find_internal(classname, NULL, TRUE);
2609  if (!dc)
2610  return (ENOMEM);
2611 
2612  error = devclass_add_device(dc, dev);
2613 
2615  return (error);
2616 }
2617 
2625 int
2626 device_set_driver(device_t dev, driver_t *driver)
2627 {
2628  if (dev->state >= DS_ATTACHED)
2629  return (EBUSY);
2630 
2631  if (dev->driver == driver)
2632  return (0);
2633 
2634  if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2635  free(dev->softc, M_BUS_SC);
2636  dev->softc = NULL;
2637  }
2638  device_set_desc(dev, NULL);
2639  kobj_delete((kobj_t) dev, NULL);
2640  dev->driver = driver;
2641  if (driver) {
2642  kobj_init((kobj_t) dev, (kobj_class_t) driver);
2643  if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2644  dev->softc = malloc(driver->size, M_BUS_SC,
2645  M_NOWAIT | M_ZERO);
2646  if (!dev->softc) {
2647  kobj_delete((kobj_t) dev, NULL);
2648  kobj_init((kobj_t) dev, &null_class);
2649  dev->driver = NULL;
2650  return (ENOMEM);
2651  }
2652  }
2653  } else {
2654  kobj_init((kobj_t) dev, &null_class);
2655  }
2656 
2658  return (0);
2659 }
2660 
2688 int
2689 device_probe(device_t dev)
2690 {
2691  int error;
2692 
2693  GIANT_REQUIRED;
2694 
2695  if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2696  return (-1);
2697 
2698  if (!(dev->flags & DF_ENABLED)) {
2699  if (bootverbose && device_get_name(dev) != NULL) {
2701  printf("not probed (disabled)\n");
2702  }
2703  return (-1);
2704  }
2705  if ((error = device_probe_child(dev->parent, dev)) != 0) {
2706  if (bus_current_pass == BUS_PASS_DEFAULT &&
2707  !(dev->flags & DF_DONENOMATCH)) {
2708  BUS_PROBE_NOMATCH(dev->parent, dev);
2709  devnomatch(dev);
2710  dev->flags |= DF_DONENOMATCH;
2711  }
2712  return (error);
2713  }
2714  return (0);
2715 }
2716 
2722 int
2724 {
2725  int error;
2726 
2727  GIANT_REQUIRED;
2728 
2729  error = device_probe(dev);
2730  if (error == -1)
2731  return (0);
2732  else if (error != 0)
2733  return (error);
2734 
2735  CURVNET_SET_QUIET(vnet0);
2736  error = device_attach(dev);
2737  CURVNET_RESTORE();
2738  return error;
2739 }
2740 
2760 int
2761 device_attach(device_t dev)
2762 {
2763  int error;
2764 
2765  if (resource_disabled(dev->driver->name, dev->unit)) {
2766  device_disable(dev);
2767  if (bootverbose)
2768  device_printf(dev, "disabled via hints entry\n");
2769  return (ENXIO);
2770  }
2771 
2772  device_sysctl_init(dev);
2773  if (!device_is_quiet(dev))
2774  device_print_child(dev->parent, dev);
2775  dev->state = DS_ATTACHING;
2776  if ((error = DEVICE_ATTACH(dev)) != 0) {
2777  printf("device_attach: %s%d attach returned %d\n",
2778  dev->driver->name, dev->unit, error);
2779  if (!(dev->flags & DF_FIXEDCLASS))
2780  devclass_delete_device(dev->devclass, dev);
2781  (void)device_set_driver(dev, NULL);
2782  device_sysctl_fini(dev);
2783  KASSERT(dev->busy == 0, ("attach failed but busy"));
2784  dev->state = DS_NOTPRESENT;
2785  return (error);
2786  }
2787  device_sysctl_update(dev);
2788  if (dev->busy)
2789  dev->state = DS_BUSY;
2790  else
2791  dev->state = DS_ATTACHED;
2792  dev->flags &= ~DF_DONENOMATCH;
2793  devadded(dev);
2794  return (0);
2795 }
2796 
2813 int
2814 device_detach(device_t dev)
2815 {
2816  int error;
2817 
2818  GIANT_REQUIRED;
2819 
2820  PDEBUG(("%s", DEVICENAME(dev)));
2821  if (dev->state == DS_BUSY)
2822  return (EBUSY);
2823  if (dev->state != DS_ATTACHED)
2824  return (0);
2825 
2826  if ((error = DEVICE_DETACH(dev)) != 0)
2827  return (error);
2828  devremoved(dev);
2829  if (!device_is_quiet(dev))
2830  device_printf(dev, "detached\n");
2831  if (dev->parent)
2832  BUS_CHILD_DETACHED(dev->parent, dev);
2833 
2834  if (!(dev->flags & DF_FIXEDCLASS))
2835  devclass_delete_device(dev->devclass, dev);
2836 
2837  dev->state = DS_NOTPRESENT;
2838  (void)device_set_driver(dev, NULL);
2839  device_sysctl_fini(dev);
2840 
2841  return (0);
2842 }
2843 
2857 int
2858 device_quiesce(device_t dev)
2859 {
2860 
2861  PDEBUG(("%s", DEVICENAME(dev)));
2862  if (dev->state == DS_BUSY)
2863  return (EBUSY);
2864  if (dev->state != DS_ATTACHED)
2865  return (0);
2866 
2867  return (DEVICE_QUIESCE(dev));
2868 }
2869 
2878 int
2879 device_shutdown(device_t dev)
2880 {
2881  if (dev->state < DS_ATTACHED)
2882  return (0);
2883  return (DEVICE_SHUTDOWN(dev));
2884 }
2885 
2892 int
2893 device_set_unit(device_t dev, int unit)
2894 {
2895  devclass_t dc;
2896  int err;
2897 
2898  dc = device_get_devclass(dev);
2899  if (unit < dc->maxunit && dc->devices[unit])
2900  return (EBUSY);
2901  err = devclass_delete_device(dc, dev);
2902  if (err)
2903  return (err);
2904  dev->unit = unit;
2905  err = devclass_add_device(dc, dev);
2906  if (err)
2907  return (err);
2908 
2910  return (0);
2911 }
2912 
2913 /*======================================*/
2914 /*
2915  * Some useful method implementations to make life easier for bus drivers.
2916  */
2917 
2923 void
2924 resource_list_init(struct resource_list *rl)
2925 {
2926  STAILQ_INIT(rl);
2927 }
2928 
2937 void
2938 resource_list_free(struct resource_list *rl)
2939 {
2940  struct resource_list_entry *rle;
2941 
2942  while ((rle = STAILQ_FIRST(rl)) != NULL) {
2943  if (rle->res)
2944  panic("resource_list_free: resource entry is busy");
2945  STAILQ_REMOVE_HEAD(rl, link);
2946  free(rle, M_BUS);
2947  }
2948 }
2949 
2963 int
2964 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2965  u_long end, u_long count)
2966 {
2967  int rid;
2968 
2969  rid = 0;
2970  while (resource_list_find(rl, type, rid) != NULL)
2971  rid++;
2972  resource_list_add(rl, type, rid, start, end, count);
2973  return (rid);
2974 }
2975 
2991 struct resource_list_entry *
2992 resource_list_add(struct resource_list *rl, int type, int rid,
2993  u_long start, u_long end, u_long count)
2994 {
2995  struct resource_list_entry *rle;
2996 
2997  rle = resource_list_find(rl, type, rid);
2998  if (!rle) {
2999  rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3000  M_NOWAIT);
3001  if (!rle)
3002  panic("resource_list_add: can't record entry");
3003  STAILQ_INSERT_TAIL(rl, rle, link);
3004  rle->type = type;
3005  rle->rid = rid;
3006  rle->res = NULL;
3007  rle->flags = 0;
3008  }
3009 
3010  if (rle->res)
3011  panic("resource_list_add: resource entry is busy");
3012 
3013  rle->start = start;
3014  rle->end = end;
3015  rle->count = count;
3016  return (rle);
3017 }
3018 
3031 int
3032 resource_list_busy(struct resource_list *rl, int type, int rid)
3033 {
3034  struct resource_list_entry *rle;
3035 
3036  rle = resource_list_find(rl, type, rid);
3037  if (rle == NULL || rle->res == NULL)
3038  return (0);
3039  if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3040  KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3041  ("reserved resource is active"));
3042  return (0);
3043  }
3044  return (1);
3045 }
3046 
3060 int
3061 resource_list_reserved(struct resource_list *rl, int type, int rid)
3062 {
3063  struct resource_list_entry *rle;
3064 
3065  rle = resource_list_find(rl, type, rid);
3066  if (rle != NULL && rle->flags & RLE_RESERVED)
3067  return (1);
3068  return (0);
3069 }
3070 
3081 struct resource_list_entry *
3082 resource_list_find(struct resource_list *rl, int type, int rid)
3083 {
3084  struct resource_list_entry *rle;
3085 
3086  STAILQ_FOREACH(rle, rl, link) {
3087  if (rle->type == type && rle->rid == rid)
3088  return (rle);
3089  }
3090  return (NULL);
3091 }
3092 
3100 void
3101 resource_list_delete(struct resource_list *rl, int type, int rid)
3102 {
3103  struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3104 
3105  if (rle) {
3106  if (rle->res != NULL)
3107  panic("resource_list_delete: resource has not been released");
3108  STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3109  free(rle, M_BUS);
3110  }
3111 }
3112 
3149 struct resource *
3150 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3151  int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3152 {
3153  struct resource_list_entry *rle = NULL;
3154  int passthrough = (device_get_parent(child) != bus);
3155  struct resource *r;
3156 
3157  if (passthrough)
3158  panic(
3159  "resource_list_reserve() should only be called for direct children");
3160  if (flags & RF_ACTIVE)
3161  panic(
3162  "resource_list_reserve() should only reserve inactive resources");
3163 
3164  r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3165  flags);
3166  if (r != NULL) {
3167  rle = resource_list_find(rl, type, *rid);
3168  rle->flags |= RLE_RESERVED;
3169  }
3170  return (r);
3171 }
3172 
3206 struct resource *
3207 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3208  int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3209 {
3210  struct resource_list_entry *rle = NULL;
3211  int passthrough = (device_get_parent(child) != bus);
3212  int isdefault = (start == 0UL && end == ~0UL);
3213 
3214  if (passthrough) {
3215  return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3216  type, rid, start, end, count, flags));
3217  }
3218 
3219  rle = resource_list_find(rl, type, *rid);
3220 
3221  if (!rle)
3222  return (NULL); /* no resource of that type/rid */
3223 
3224  if (rle->res) {
3225  if (rle->flags & RLE_RESERVED) {
3226  if (rle->flags & RLE_ALLOCATED)
3227  return (NULL);
3228  if ((flags & RF_ACTIVE) &&
3229  bus_activate_resource(child, type, *rid,
3230  rle->res) != 0)
3231  return (NULL);
3232  rle->flags |= RLE_ALLOCATED;
3233  return (rle->res);
3234  }
3235  device_printf(bus,
3236  "resource entry %#x type %d for child %s is busy\n", *rid,
3237  type, device_get_nameunit(child));
3238  return (NULL);
3239  }
3240 
3241  if (isdefault) {
3242  start = rle->start;
3243  count = ulmax(count, rle->count);
3244  end = ulmax(rle->end, start + count - 1);
3245  }
3246 
3247  rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3248  type, rid, start, end, count, flags);
3249 
3250  /*
3251  * Record the new range.
3252  */
3253  if (rle->res) {
3254  rle->start = rman_get_start(rle->res);
3255  rle->end = rman_get_end(rle->res);
3256  rle->count = count;
3257  }
3258 
3259  return (rle->res);
3260 }
3261 
3279 int
3280 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3281  int type, int rid, struct resource *res)
3282 {
3283  struct resource_list_entry *rle = NULL;
3284  int passthrough = (device_get_parent(child) != bus);
3285  int error;
3286 
3287  if (passthrough) {
3288  return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3289  type, rid, res));
3290  }
3291 
3292  rle = resource_list_find(rl, type, rid);
3293 
3294  if (!rle)
3295  panic("resource_list_release: can't find resource");
3296  if (!rle->res)
3297  panic("resource_list_release: resource entry is not busy");
3298  if (rle->flags & RLE_RESERVED) {
3299  if (rle->flags & RLE_ALLOCATED) {
3300  if (rman_get_flags(res) & RF_ACTIVE) {
3301  error = bus_deactivate_resource(child, type,
3302  rid, res);
3303  if (error)
3304  return (error);
3305  }
3306  rle->flags &= ~RLE_ALLOCATED;
3307  return (0);
3308  }
3309  return (EINVAL);
3310  }
3311 
3312  error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3313  type, rid, res);
3314  if (error)
3315  return (error);
3316 
3317  rle->res = NULL;
3318  return (0);
3319 }
3320 
3337 int
3338 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3339  int type, int rid)
3340 {
3341  struct resource_list_entry *rle = NULL;
3342  int passthrough = (device_get_parent(child) != bus);
3343 
3344  if (passthrough)
3345  panic(
3346  "resource_list_unreserve() should only be called for direct children");
3347 
3348  rle = resource_list_find(rl, type, rid);
3349 
3350  if (!rle)
3351  panic("resource_list_unreserve: can't find resource");
3352  if (!(rle->flags & RLE_RESERVED))
3353  return (EINVAL);
3354  if (rle->flags & RLE_ALLOCATED)
3355  return (EBUSY);
3356  rle->flags &= ~RLE_RESERVED;
3357  return (resource_list_release(rl, bus, child, type, rid, rle->res));
3358 }
3359 
3375 int
3376 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3377  const char *format)
3378 {
3379  struct resource_list_entry *rle;
3380  int printed, retval;
3381 
3382  printed = 0;
3383  retval = 0;
3384  /* Yes, this is kinda cheating */
3385  STAILQ_FOREACH(rle, rl, link) {
3386  if (rle->type == type) {
3387  if (printed == 0)
3388  retval += printf(" %s ", name);
3389  else
3390  retval += printf(",");
3391  printed++;
3392  retval += printf(format, rle->start);
3393  if (rle->count > 1) {
3394  retval += printf("-");
3395  retval += printf(format, rle->start +
3396  rle->count - 1);
3397  }
3398  }
3399  }
3400  return (retval);
3401 }
3402 
3410 void
3411 resource_list_purge(struct resource_list *rl)
3412 {
3413  struct resource_list_entry *rle;
3414 
3415  while ((rle = STAILQ_FIRST(rl)) != NULL) {
3416  if (rle->res)
3418  rle->type, rle->rid, rle->res);
3419  STAILQ_REMOVE_HEAD(rl, link);
3420  free(rle, M_BUS);
3421  }
3422 }
3423 
3424 device_t
3425 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3426 {
3427 
3428  return (device_add_child_ordered(dev, order, name, unit));
3429 }
3430 
3439 int
3440 bus_generic_probe(device_t dev)
3441 {
3442  devclass_t dc = dev->devclass;
3443  driverlink_t dl;
3444 
3445  TAILQ_FOREACH(dl, &dc->drivers, link) {
3446  /*
3447  * If this driver's pass is too high, then ignore it.
3448  * For most drivers in the default pass, this will
3449  * never be true. For early-pass drivers they will
3450  * only call the identify routines of eligible drivers
3451  * when this routine is called. Drivers for later
3452  * passes should have their identify routines called
3453  * on early-pass busses during BUS_NEW_PASS().
3454  */
3455  if (dl->pass > bus_current_pass)
3456  continue;
3457  DEVICE_IDENTIFY(dl->driver, dev);
3458  }
3459 
3460  return (0);
3461 }
3462 
3470 int
3471 bus_generic_attach(device_t dev)
3472 {
3473  device_t child;
3474 
3475  TAILQ_FOREACH(child, &dev->children, link) {
3476  device_probe_and_attach(child);
3477  }
3478 
3479  return (0);
3480 }
3481 
3489 int
3490 bus_generic_detach(device_t dev)
3491 {
3492  device_t child;
3493  int error;
3494 
3495  if (dev->state != DS_ATTACHED)
3496  return (EBUSY);
3497 
3498  TAILQ_FOREACH(child, &dev->children, link) {
3499  if ((error = device_detach(child)) != 0)
3500  return (error);
3501  }
3502 
3503  return (0);
3504 }
3505 
3513 int
3515 {
3516  device_t child;
3517 
3518  TAILQ_FOREACH(child, &dev->children, link) {
3519  device_shutdown(child);
3520  }
3521 
3522  return (0);
3523 }
3524 
3534 int
3535 bus_generic_suspend(device_t dev)
3536 {
3537  int error;
3538  device_t child, child2;
3539 
3540  TAILQ_FOREACH(child, &dev->children, link) {
3541  error = DEVICE_SUSPEND(child);
3542  if (error) {
3543  for (child2 = TAILQ_FIRST(&dev->children);
3544  child2 && child2 != child;
3545  child2 = TAILQ_NEXT(child2, link))
3546  DEVICE_RESUME(child2);
3547  return (error);
3548  }
3549  }
3550  return (0);
3551 }
3552 
3559 int
3560 bus_generic_resume(device_t dev)
3561 {
3562  device_t child;
3563 
3564  TAILQ_FOREACH(child, &dev->children, link) {
3565  DEVICE_RESUME(child);
3566  /* if resume fails, there's nothing we can usefully do... */
3567  }
3568  return (0);
3569 }
3570 
3580 int
3581 bus_print_child_header(device_t dev, device_t child)
3582 {
3583  int retval = 0;
3584 
3585  if (device_get_desc(child)) {
3586  retval += device_printf(child, "<%s>", device_get_desc(child));
3587  } else {
3588  retval += printf("%s", device_get_nameunit(child));
3589  }
3590 
3591  return (retval);
3592 }
3593 
3603 int
3604 bus_print_child_footer(device_t dev, device_t child)
3605 {
3606  return (printf(" on %s\n", device_get_nameunit(dev)));
3607 }
3608 
3617 int
3618 bus_generic_print_child(device_t dev, device_t child)
3619 {
3620  int retval = 0;
3621 
3622  retval += bus_print_child_header(dev, child);
3623  retval += bus_print_child_footer(dev, child);
3624 
3625  return (retval);
3626 }
3627 
3633 int
3634 bus_generic_read_ivar(device_t dev, device_t child, int index,
3635  uintptr_t * result)
3636 {
3637  return (ENOENT);
3638 }
3639 
3645 int
3646 bus_generic_write_ivar(device_t dev, device_t child, int index,
3647  uintptr_t value)
3648 {
3649  return (ENOENT);
3650 }
3651 
3657 struct resource_list *
3658 bus_generic_get_resource_list(device_t dev, device_t child)
3659 {
3660  return (NULL);
3661 }
3662 
3670 void
3671 bus_generic_driver_added(device_t dev, driver_t *driver)
3672 {
3673  device_t child;
3674 
3675  DEVICE_IDENTIFY(driver, dev);
3676  TAILQ_FOREACH(child, &dev->children, link) {
3677  if (child->state == DS_NOTPRESENT ||
3678  (child->flags & DF_REBID))
3679  device_probe_and_attach(child);
3680  }
3681 }
3682 
3693 void
3695 {
3696  driverlink_t dl;
3697  devclass_t dc;
3698  device_t child;
3699 
3700  dc = dev->devclass;
3701  TAILQ_FOREACH(dl, &dc->drivers, link) {
3702  if (dl->pass == bus_current_pass)
3703  DEVICE_IDENTIFY(dl->driver, dev);
3704  }
3705  TAILQ_FOREACH(child, &dev->children, link) {
3706  if (child->state >= DS_ATTACHED)
3707  BUS_NEW_PASS(child);
3708  else if (child->state == DS_NOTPRESENT)
3709  device_probe_and_attach(child);
3710  }
3711 }
3712 
3719 int
3720 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3721  int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3722  void **cookiep)
3723 {
3724  /* Propagate up the bus hierarchy until someone handles it. */
3725  if (dev->parent)
3726  return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3727  filter, intr, arg, cookiep));
3728  return (EINVAL);
3729 }
3730 
3737 int
3738 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3739  void *cookie)
3740 {
3741  /* Propagate up the bus hierarchy until someone handles it. */
3742  if (dev->parent)
3743  return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3744  return (EINVAL);
3745 }
3746 
3753 int
3754 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3755  struct resource *r, u_long start, u_long end)
3756 {
3757  /* Propagate up the bus hierarchy until someone handles it. */
3758  if (dev->parent)
3759  return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3760  end));
3761  return (EINVAL);
3762 }
3763 
3770 struct resource *
3771 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3772  u_long start, u_long end, u_long count, u_int flags)
3773 {
3774  /* Propagate up the bus hierarchy until someone handles it. */
3775  if (dev->parent)
3776  return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3777  start, end, count, flags));
3778  return (NULL);
3779 }
3780 
3787 int
3788 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3789  struct resource *r)
3790 {
3791  /* Propagate up the bus hierarchy until someone handles it. */
3792  if (dev->parent)
3793  return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3794  r));
3795  return (EINVAL);
3796 }
3797 
3804 int
3805 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3806  struct resource *r)
3807 {
3808  /* Propagate up the bus hierarchy until someone handles it. */
3809  if (dev->parent)
3810  return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3811  r));
3812  return (EINVAL);
3813 }
3814 
3821 int
3822 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3823  int rid, struct resource *r)
3824 {
3825  /* Propagate up the bus hierarchy until someone handles it. */
3826  if (dev->parent)
3827  return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3828  r));
3829  return (EINVAL);
3830 }
3831 
3838 int
3839 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3840  int cpu)
3841 {
3842 
3843  /* Propagate up the bus hierarchy until someone handles it. */
3844  if (dev->parent)
3845  return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3846  return (EINVAL);
3847 }
3848 
3855 int
3856 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3857  enum intr_polarity pol)
3858 {
3859 
3860  /* Propagate up the bus hierarchy until someone handles it. */
3861  if (dev->parent)
3862  return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3863  return (EINVAL);
3864 }
3865 
3872 int
3873 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3874  void *cookie, const char *descr)
3875 {
3876 
3877  /* Propagate up the bus hierarchy until someone handles it. */
3878  if (dev->parent)
3879  return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3880  descr));
3881  return (EINVAL);
3882 }
3883 
3890 bus_dma_tag_t
3891 bus_generic_get_dma_tag(device_t dev, device_t child)
3892 {
3893 
3894  /* Propagate up the bus hierarchy until someone handles it. */
3895  if (dev->parent != NULL)
3896  return (BUS_GET_DMA_TAG(dev->parent, child));
3897  return (NULL);
3898 }
3899 
3908 int
3909 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3910  u_long *startp, u_long *countp)
3911 {
3912  struct resource_list * rl = NULL;
3913  struct resource_list_entry * rle = NULL;
3914 
3915  rl = BUS_GET_RESOURCE_LIST(dev, child);
3916  if (!rl)
3917  return (EINVAL);
3918 
3919  rle = resource_list_find(rl, type, rid);
3920  if (!rle)
3921  return (ENOENT);
3922 
3923  if (startp)
3924  *startp = rle->start;
3925  if (countp)
3926  *countp = rle->count;
3927 
3928  return (0);
3929 }
3930 
3939 int
3940 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3941  u_long start, u_long count)
3942 {
3943  struct resource_list * rl = NULL;
3944 
3945  rl = BUS_GET_RESOURCE_LIST(dev, child);
3946  if (!rl)
3947  return (EINVAL);
3948 
3949  resource_list_add(rl, type, rid, start, (start + count - 1), count);
3950 
3951  return (0);
3952 }
3953 
3962 void
3963 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3964 {
3965  struct resource_list * rl = NULL;
3966 
3967  rl = BUS_GET_RESOURCE_LIST(dev, child);
3968  if (!rl)
3969  return;
3970 
3971  resource_list_delete(rl, type, rid);
3972 
3973  return;
3974 }
3975 
3983 int
3984 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3985  int rid, struct resource *r)
3986 {
3987  struct resource_list * rl = NULL;
3988 
3989  if (device_get_parent(child) != dev)
3990  return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
3991  type, rid, r));
3992 
3993  rl = BUS_GET_RESOURCE_LIST(dev, child);
3994  if (!rl)
3995  return (EINVAL);
3996 
3997  return (resource_list_release(rl, dev, child, type, rid, r));
3998 }
3999 
4007 struct resource *
4008 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4009  int *rid, u_long start, u_long end, u_long count, u_int flags)
4010 {
4011  struct resource_list * rl = NULL;
4012 
4013  if (device_get_parent(child) != dev)
4014  return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4015  type, rid, start, end, count, flags));
4016 
4017  rl = BUS_GET_RESOURCE_LIST(dev, child);
4018  if (!rl)
4019  return (NULL);
4020 
4021  return (resource_list_alloc(rl, dev, child, type, rid,
4022  start, end, count, flags));
4023 }
4024 
4031 int
4032 bus_generic_child_present(device_t dev, device_t child)
4033 {
4034  return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4035 }
4036 
4037 /*
4038  * Some convenience functions to make it easier for drivers to use the
4039  * resource-management functions. All these really do is hide the
4040  * indirection through the parent's method table, making for slightly
4041  * less-wordy code. In the future, it might make sense for this code
4042  * to maintain some sort of a list of resources allocated by each device.
4043  */
4044 
4045 int
4046 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4047  struct resource **res)
4048 {
4049  int i;
4050 
4051  for (i = 0; rs[i].type != -1; i++)
4052  res[i] = NULL;
4053  for (i = 0; rs[i].type != -1; i++) {
4054  res[i] = bus_alloc_resource_any(dev,
4055  rs[i].type, &rs[i].rid, rs[i].flags);
4056  if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4057  bus_release_resources(dev, rs, res);
4058  return (ENXIO);
4059  }
4060  }
4061  return (0);
4062 }
4063 
4064 void
4065 bus_release_resources(device_t dev, const struct resource_spec *rs,
4066  struct resource **res)
4067 {
4068  int i;
4069 
4070  for (i = 0; rs[i].type != -1; i++)
4071  if (res[i] != NULL) {
4073  dev, rs[i].type, rs[i].rid, res[i]);
4074  res[i] = NULL;
4075  }
4076 }
4077 
4084 struct resource *
4085 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
4086  u_long count, u_int flags)
4087 {
4088  if (dev->parent == NULL)
4089  return (NULL);
4090  return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4091  count, flags));
4092 }
4093 
4100 int
4101 bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start,
4102  u_long end)
4103 {
4104  if (dev->parent == NULL)
4105  return (EINVAL);
4106  return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4107 }
4108 
4115 int
4116 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4117 {
4118  if (dev->parent == NULL)
4119  return (EINVAL);
4120  return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4121 }
4122 
4129 int
4130 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4131 {
4132  if (dev->parent == NULL)
4133  return (EINVAL);
4134  return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4135 }
4136 
4143 int
4144 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4145 {
4146  if (dev->parent == NULL)
4147  return (EINVAL);
4148  return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4149 }
4150 
4157 int
4158 bus_setup_intr(device_t dev, struct resource *r, int flags,
4159  driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4160 {
4161  int error;
4162 
4163  if (dev->parent == NULL)
4164  return (EINVAL);
4165  error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4166  arg, cookiep);
4167  if (error != 0)
4168  return (error);
4169  if (handler != NULL && !(flags & INTR_MPSAFE))
4170  device_printf(dev, "[GIANT-LOCKED]\n");
4171  return (0);
4172 }
4173 
4180 int
4181 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4182 {
4183  if (dev->parent == NULL)
4184  return (EINVAL);
4185  return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4186 }
4187 
4194 int
4195 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4196 {
4197  if (dev->parent == NULL)
4198  return (EINVAL);
4199  return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4200 }
4201 
4209 int
4210 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4211  const char *fmt, ...)
4212 {
4213  va_list ap;
4214  char descr[MAXCOMLEN + 1];
4215 
4216  if (dev->parent == NULL)
4217  return (EINVAL);
4218  va_start(ap, fmt);
4219  vsnprintf(descr, sizeof(descr), fmt, ap);
4220  va_end(ap);
4221  return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4222 }
4223 
4230 int
4231 bus_set_resource(device_t dev, int type, int rid,
4232  u_long start, u_long count)
4233 {
4234  return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4235  start, count));
4236 }
4237 
4244 int
4245 bus_get_resource(device_t dev, int type, int rid,
4246  u_long *startp, u_long *countp)
4247 {
4248  return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4249  startp, countp));
4250 }
4251 
4258 u_long
4259 bus_get_resource_start(device_t dev, int type, int rid)
4260 {
4261  u_long start, count;
4262  int error;
4263 
4264  error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4265  &start, &count);
4266  if (error)
4267  return (0);
4268  return (start);
4269 }
4270 
4277 u_long
4278 bus_get_resource_count(device_t dev, int type, int rid)
4279 {
4280  u_long start, count;
4281  int error;
4282 
4283  error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4284  &start, &count);
4285  if (error)
4286  return (0);
4287  return (count);
4288 }
4289 
4296 void
4297 bus_delete_resource(device_t dev, int type, int rid)
4298 {
4299  BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4300 }
4301 
4308 int
4309 bus_child_present(device_t child)
4310 {
4311  return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4312 }
4313 
4320 int
4321 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4322 {
4323  device_t parent;
4324 
4325  parent = device_get_parent(child);
4326  if (parent == NULL) {
4327  *buf = '\0';
4328  return (0);
4329  }
4330  return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4331 }
4332 
4339 int
4340 bus_child_location_str(device_t child, char *buf, size_t buflen)
4341 {
4342  device_t parent;
4343 
4344  parent = device_get_parent(child);
4345  if (parent == NULL) {
4346  *buf = '\0';
4347  return (0);
4348  }
4349  return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4350 }
4351 
4358 bus_dma_tag_t
4359 bus_get_dma_tag(device_t dev)
4360 {
4361  device_t parent;
4362 
4363  parent = device_get_parent(dev);
4364  if (parent == NULL)
4365  return (NULL);
4366  return (BUS_GET_DMA_TAG(parent, dev));
4367 }
4368 
4369 /* Resume all devices and then notify userland that we're up again. */
4370 static int
4371 root_resume(device_t dev)
4372 {
4373  int error;
4374 
4375  error = bus_generic_resume(dev);
4376  if (error == 0)
4377  devctl_notify("kern", "power", "resume", NULL);
4378  return (error);
4379 }
4380 
4381 static int
4382 root_print_child(device_t dev, device_t child)
4383 {
4384  int retval = 0;
4385 
4386  retval += bus_print_child_header(dev, child);
4387  retval += printf("\n");
4388 
4389  return (retval);
4390 }
4391 
4392 static int
4393 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4394  driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4395 {
4396  /*
4397  * If an interrupt mapping gets to here something bad has happened.
4398  */
4399  panic("root_setup_intr");
4400 }
4401 
4402 /*
4403  * If we get here, assume that the device is permanant and really is
4404  * present in the system. Removable bus drivers are expected to intercept
4405  * this call long before it gets here. We return -1 so that drivers that
4406  * really care can check vs -1 or some ERRNO returned higher in the food
4407  * chain.
4408  */
4409 static int
4410 root_child_present(device_t dev, device_t child)
4411 {
4412  return (-1);
4413 }
4414 
4415 static kobj_method_t root_methods[] = {
4416  /* Device interface */
4418  KOBJMETHOD(device_suspend, bus_generic_suspend),
4419  KOBJMETHOD(device_resume, root_resume),
4420 
4421  /* Bus interface */
4422  KOBJMETHOD(bus_print_child, root_print_child),
4423  KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4424  KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4425  KOBJMETHOD(bus_setup_intr, root_setup_intr),
4427 
4428  KOBJMETHOD_END
4429 };
4430 
4431 static driver_t root_driver = {
4432  "root",
4433  root_methods,
4434  1, /* no softc */
4435 };
4436 
4437 device_t root_bus;
4438 devclass_t root_devclass;
4439 
4440 static int
4441 root_bus_module_handler(module_t mod, int what, void* arg)
4442 {
4443  switch (what) {
4444  case MOD_LOAD:
4445  TAILQ_INIT(&bus_data_devices);
4446  kobj_class_compile((kobj_class_t) &root_driver);
4447  root_bus = make_device(NULL, "root", 0);
4448  root_bus->desc = "System root bus";
4449  kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4450  root_bus->driver = &root_driver;
4451  root_bus->state = DS_ATTACHED;
4452  root_devclass = devclass_find_internal("root", NULL, FALSE);
4453  devinit();
4454  return (0);
4455 
4456  case MOD_SHUTDOWN:
4457  device_shutdown(root_bus);
4458  return (0);
4459  default:
4460  return (EOPNOTSUPP);
4461  }
4462 
4463  return (0);
4464 }
4465 
4466 static moduledata_t root_bus_mod = {
4467  "rootbus",
4469  NULL
4470 };
4471 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4472 
4479 void
4481 {
4482 
4483  PDEBUG(("."));
4484 
4485  /* Eventually this will be split up, but this is sufficient for now. */
4486  bus_set_pass(BUS_PASS_DEFAULT);
4487 }
4488 
4497 int
4498 driver_module_handler(module_t mod, int what, void *arg)
4499 {
4500  struct driver_module_data *dmd;
4501  devclass_t bus_devclass;
4502  kobj_class_t driver;
4503  int error, pass;
4504 
4505  dmd = (struct driver_module_data *)arg;
4506  bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4507  error = 0;
4508 
4509  switch (what) {
4510  case MOD_LOAD:
4511  if (dmd->dmd_chainevh)
4512  error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4513 
4514  pass = dmd->dmd_pass;
4515  driver = dmd->dmd_driver;
4516  PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4517  DRIVERNAME(driver), dmd->dmd_busname, pass));
4518  error = devclass_add_driver(bus_devclass, driver, pass,
4519  dmd->dmd_devclass);
4520  break;
4521 
4522  case MOD_UNLOAD:
4523  PDEBUG(("Unloading module: driver %s from bus %s",
4524  DRIVERNAME(dmd->dmd_driver),
4525  dmd->dmd_busname));
4526  error = devclass_delete_driver(bus_devclass,
4527  dmd->dmd_driver);
4528 
4529  if (!error && dmd->dmd_chainevh)
4530  error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4531  break;
4532  case MOD_QUIESCE:
4533  PDEBUG(("Quiesce module: driver %s from bus %s",
4534  DRIVERNAME(dmd->dmd_driver),
4535  dmd->dmd_busname));
4536  error = devclass_quiesce_driver(bus_devclass,
4537  dmd->dmd_driver);
4538 
4539  if (!error && dmd->dmd_chainevh)
4540  error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4541  break;
4542  default:
4543  error = EOPNOTSUPP;
4544  break;
4545  }
4546 
4547  return (error);
4548 }
4549 
4560 void
4562 {
4563  int i;
4564  const char *dname, *busname;
4565  int dunit;
4566 
4567  /*
4568  * enumerate all devices on the specific bus
4569  */
4570  busname = device_get_nameunit(bus);
4571  i = 0;
4572  while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4573  BUS_HINTED_CHILD(bus, dname, dunit);
4574 
4575  /*
4576  * and all the generic ones.
4577  */
4578  busname = device_get_name(bus);
4579  i = 0;
4580  while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4581  BUS_HINTED_CHILD(bus, dname, dunit);
4582 }
4583 
4584 #ifdef BUS_DEBUG
4585 
4586 /* the _short versions avoid iteration by not calling anything that prints
4587  * more than oneliners. I love oneliners.
4588  */
4589 
4590 static void
4591 print_device_short(device_t dev, int indent)
4592 {
4593  if (!dev)
4594  return;
4595 
4596  indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4597  dev->unit, dev->desc,
4598  (dev->parent? "":"no "),
4599  (TAILQ_EMPTY(&dev->children)? "no ":""),
4600  (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4601  (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4602  (dev->flags&DF_WILDCARD? "wildcard,":""),
4603  (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4604  (dev->flags&DF_REBID? "rebiddable,":""),
4605  (dev->ivars? "":"no "),
4606  (dev->softc? "":"no "),
4607  dev->busy));
4608 }
4609 
4610 static void
4611 print_device(device_t dev, int indent)
4612 {
4613  if (!dev)
4614  return;
4615 
4616  print_device_short(dev, indent);
4617 
4618  indentprintf(("Parent:\n"));
4619  print_device_short(dev->parent, indent+1);
4620  indentprintf(("Driver:\n"));
4621  print_driver_short(dev->driver, indent+1);
4622  indentprintf(("Devclass:\n"));
4623  print_devclass_short(dev->devclass, indent+1);
4624 }
4625 
4626 void
4627 print_device_tree_short(device_t dev, int indent)
4628 /* print the device and all its children (indented) */
4629 {
4630  device_t child;
4631 
4632  if (!dev)
4633  return;
4634 
4635  print_device_short(dev, indent);
4636 
4637  TAILQ_FOREACH(child, &dev->children, link) {
4638  print_device_tree_short(child, indent+1);
4639  }
4640 }
4641 
4642 void
4643 print_device_tree(device_t dev, int indent)
4644 /* print the device and all its children (indented) */
4645 {
4646  device_t child;
4647 
4648  if (!dev)
4649  return;
4650 
4651  print_device(dev, indent);
4652 
4653  TAILQ_FOREACH(child, &dev->children, link) {
4654  print_device_tree(child, indent+1);
4655  }
4656 }
4657 
4658 static void
4659 print_driver_short(driver_t *driver, int indent)
4660 {
4661  if (!driver)
4662  return;
4663 
4664  indentprintf(("driver %s: softc size = %zd\n",
4665  driver->name, driver->size));
4666 }
4667 
4668 static void
4669 print_driver(driver_t *driver, int indent)
4670 {
4671  if (!driver)
4672  return;
4673 
4674  print_driver_short(driver, indent);
4675 }
4676 
4677 static void
4678 print_driver_list(driver_list_t drivers, int indent)
4679 {
4680  driverlink_t driver;
4681 
4682  TAILQ_FOREACH(driver, &drivers, link) {
4683  print_driver(driver->driver, indent);
4684  }
4685 }
4686 
4687 static void
4688 print_devclass_short(devclass_t dc, int indent)
4689 {
4690  if ( !dc )
4691  return;
4692 
4693  indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4694 }
4695 
4696 static void
4697 print_devclass(devclass_t dc, int indent)
4698 {
4699  int i;
4700 
4701  if ( !dc )
4702  return;
4703 
4704  print_devclass_short(dc, indent);
4705  indentprintf(("Drivers:\n"));
4706  print_driver_list(dc->drivers, indent+1);
4707 
4708  indentprintf(("Devices:\n"));
4709  for (i = 0; i < dc->maxunit; i++)
4710  if (dc->devices[i])
4711  print_device(dc->devices[i], indent+1);
4712 }
4713 
4714 void
4716 {
4717  devclass_t dc;
4718 
4719  printf("Short listing of devclasses, drivers & devices:\n");
4720  TAILQ_FOREACH(dc, &devclasses, link) {
4721  print_devclass_short(dc, 0);
4722  }
4723 }
4724 
4725 void
4726 print_devclass_list(void)
4727 {
4728  devclass_t dc;
4729 
4730  printf("Full listing of devclasses, drivers & devices:\n");
4731  TAILQ_FOREACH(dc, &devclasses, link) {
4732  print_devclass(dc, 0);
4733  }
4734 }
4735 
4736 #endif
4737 
4738 /*
4739  * User-space access to the device tree.
4740  *
4741  * We implement a small set of nodes:
4742  *
4743  * hw.bus Single integer read method to obtain the
4744  * current generation count.
4745  * hw.bus.devices Reads the entire device tree in flat space.
4746  * hw.bus.rman Resource manager interface
4747  *
4748  * We might like to add the ability to scan devclasses and/or drivers to
4749  * determine what else is currently loaded/available.
4750  */
4751 
4752 static int
4753 sysctl_bus(SYSCTL_HANDLER_ARGS)
4754 {
4755  struct u_businfo ubus;
4756 
4757  ubus.ub_version = BUS_USER_VERSION;
4758  ubus.ub_generation = bus_data_generation;
4759 
4760  return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4761 }
4762 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4763  "bus-related data");
4764 
4765 static int
4766 sysctl_devices(SYSCTL_HANDLER_ARGS)
4767 {
4768  int *name = (int *)arg1;
4769  u_int namelen = arg2;
4770  int index;
4771  struct device *dev;
4772  struct u_device udev; /* XXX this is a bit big */
4773  int error;
4774 
4775  if (namelen != 2)
4776  return (EINVAL);
4777 
4778  if (bus_data_generation_check(name[0]))
4779  return (EINVAL);
4780 
4781  index = name[1];
4782 
4783  /*
4784  * Scan the list of devices, looking for the requested index.
4785  */
4786  TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4787  if (index-- == 0)
4788  break;
4789  }
4790  if (dev == NULL)
4791  return (ENOENT);
4792 
4793  /*
4794  * Populate the return array.
4795  */
4796  bzero(&udev, sizeof(udev));
4797  udev.dv_handle = (uintptr_t)dev;
4798  udev.dv_parent = (uintptr_t)dev->parent;
4799  if (dev->nameunit != NULL)
4800  strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4801  if (dev->desc != NULL)
4802  strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4803  if (dev->driver != NULL && dev->driver->name != NULL)
4804  strlcpy(udev.dv_drivername, dev->driver->name,
4805  sizeof(udev.dv_drivername));
4806  bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4807  bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4808  udev.dv_devflags = dev->devflags;
4809  udev.dv_flags = dev->flags;
4810  udev.dv_state = dev->state;
4811  error = SYSCTL_OUT(req, &udev, sizeof(udev));
4812  return (error);
4813 }
4814 
4815 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4816  "system device tree");
4817 
4818 int
4820 {
4821  if (generation != bus_data_generation)
4822  return (1);
4823 
4824  /* XXX generate optimised lists here? */
4825  return (0);
4826 }
4827 
4828 void
4830 {
4831  bus_data_generation++;
4832 }
4833 
4834 int
4835 bus_free_resource(device_t dev, int type, struct resource *r)
4836 {
4837  if (r == NULL)
4838  return (0);
4839  return (bus_release_resource(dev, type, rman_get_rid(r), r));
4840 }
int device_is_quiet(device_t dev)
Return non-zero if the DF_QUIET flag is set on the device.
Definition: subr_bus.c:2554
char * dei_data
Definition: subr_bus.c:387
#define print_devclass_list()
Definition: subr_bus.c:193
static void devremoved(device_t dev)
Definition: subr_bus.c:721
void device_disable(device_t dev)
Clear the DF_ENABLED flag for the device.
Definition: subr_bus.c:2493
u_int rman_get_flags(struct resource *r)
Definition: subr_rman.c:837
struct resource * bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
Wrapper function for BUS_ALLOC_RESOURCE().
Definition: subr_bus.c:4085
static void devnomatch(device_t dev)
Definition: subr_bus.c:734
void bus_generic_driver_added(device_t dev, driver_t *driver)
Helper function for implementing BUS_DRIVER_ADDED().
Definition: subr_bus.c:3671
device_t device_find_child(device_t dev, const char *classname, int unit)
Find a device given a unit number.
Definition: subr_bus.c:1936
static int device_print_child(device_t dev, device_t child)
Print a description of a device.
Definition: subr_bus.c:1753
int device_quiesce(device_t dev)
Tells a driver to quiesce itself.
Definition: subr_bus.c:2858
const char * device_get_desc(device_t dev)
Return the device's description string.
Definition: subr_bus.c:2265
devclass_t root_devclass
Definition: subr_bus.c:4438
int inuse
Definition: subr_bus.c:395
int device_delete_children(device_t dev)
Delete all children devices of the given device, if any.
Definition: subr_bus.c:1902
static kobj_method_t root_methods[]
Definition: subr_bus.c:4415
static driverlink_t devclass_find_driver_internal(devclass_t dc, const char *classname)
Definition: subr_bus.c:1287
int bus_generic_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
Stub function for implementing BUS_WRITE_IVAR().
Definition: subr_bus.c:3646
struct buf * buf
Definition: vfs_bio.c:97
int bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, void *cookie)
Helper function for implementing BUS_TEARDOWN_INTR().
Definition: subr_bus.c:3738
int bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
Wrapper function for BUS_CHILD_PNPINFO_STR().
Definition: subr_bus.c:4321
device_t device_add_child(device_t dev, const char *name, int unit)
Create a new device.
Definition: subr_bus.c:1782
void * devclass_get_softc(devclass_t dc, int unit)
Find the softc field of a device given a unit number.
Definition: subr_bus.c:1339
int device_is_attached(device_t dev)
Return non-zero if the device currently has a driver attached to it.
Definition: subr_bus.c:2582
int bus_generic_adjust_resource(device_t dev, device_t child, int type, struct resource *r, u_long start, u_long end)
Helper function for implementing BUS_ADJUST_RESOURCE().
Definition: subr_bus.c:3754
#define print_device_short(d, i)
Definition: subr_bus.c:183
static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
Definition: subr_bus.c:740
int device_printf(device_t dev, const char *fmt,...)
Print the name of the device followed by a colon, a space and the result of calling vprintf() with th...
Definition: subr_bus.c:2314
void selwakeup(struct selinfo *sip)
Definition: sys_generic.c:1656
caddr_t value
Definition: linker_if.m:51
#define DF_WILDCARD
int snprintf(char *str, size_t size, const char *format,...)
Definition: subr_prf.c:509
int device_is_enabled(device_t dev)
Return non-zero if the DF_ENABLED flag is set on the device.
Definition: subr_bus.c:2563
int rman_get_rid(struct resource *r)
Definition: subr_rman.c:893
void resource_list_delete(struct resource_list *rl, int type, int rid)
Delete a resource entry.
Definition: subr_bus.c:3101
INTERFACE bus
Definition: bus_if.m:39
int bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
Helper function for implementing BUS_SET_RESOURCE().
Definition: subr_bus.c:3940
int bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
Helper function for implementing BUS_SETUP_INTR().
Definition: subr_bus.c:3720
int bootverbose
Definition: init_main.c:107
void *** start
Definition: linker_if.m:86
int nonblock
Definition: subr_bus.c:396
static void device_set_desc_internal(device_t dev, const char *desc, int copy)
Definition: subr_bus.c:2330
void selrecord(struct thread *selector, struct selinfo *sip)
Definition: sys_generic.c:1606
int bus_generic_child_present(device_t dev, device_t child)
Helper function for implementing BUS_CHILD_PRESENT().
Definition: subr_bus.c:4032
int bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
Helper function for implementing BUS_GET_RESOURCE().
Definition: subr_bus.c:3909
struct resource * resource_list_alloc(struct resource_list *rl, device_t bus, device_t child, int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
Helper function for implementing BUS_ALLOC_RESOURCE()
Definition: subr_bus.c:3207
int devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
Add a device driver to a device class.
Definition: subr_bus.c:1044
static int sysctl_devices(SYSCTL_HANDLER_ARGS)
Definition: subr_bus.c:4766
struct selinfo sel
Definition: subr_bus.c:400
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
Definition: kern_malloc.c:454
void device_busy(device_t dev)
Increment the busy counter for the device.
Definition: subr_bus.c:2502
void bus_delete_resource(device_t dev, int type, int rid)
Wrapper function for BUS_DELETE_RESOURCE().
Definition: subr_bus.c:4297
int resource_find_match(int *anchor, const char **name, int *unit, const char *resname, const char *value)
Definition: subr_hints.c:325
int bus_setup_intr(device_t dev, struct resource *r, int flags, driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
Wrapper function for BUS_SETUP_INTR().
Definition: subr_bus.c:4158
int resource_list_busy(struct resource_list *rl, int type, int rid)
Determine if a resource entry is busy.
Definition: subr_bus.c:3032
static int devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
Definition: subr_bus.c:205
int devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
Get a list of drivers in the devclass.
Definition: subr_bus.c:1408
static devclass_t devclass_find_internal(const char *classname, const char *parentname, int create)
Find or create a device class.
Definition: subr_bus.c:911
void panic(const char *fmt,...)
void device_quiet(device_t dev)
Set the DF_QUIET flag for the device.
Definition: subr_bus.c:2536
void device_enable(device_t dev)
Set the DF_ENABLED flag for the device.
Definition: subr_bus.c:2484
static struct dev_softc devsoftc
device_t bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
Definition: subr_bus.c:3425
static void driver_register_pass(struct driverlink *new)
Register the pass level of a new driver attachment.
Definition: subr_bus.c:820
device_t parent
Definition: device_if.m:171
int device_get_unit(device_t dev)
Return the device's unit number.
Definition: subr_bus.c:2256
u_long bus_get_resource_start(device_t dev, int type, int rid)
Wrapper function for BUS_GET_RESOURCE().
Definition: subr_bus.c:4259
static driver_t root_driver
Definition: subr_bus.c:4431
static void device_sysctl_update(device_t dev)
Definition: subr_bus.c:316
SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT|CTLFLAG_RW, NULL, 0, sysctl_devctl_disable,"I","devctl disable -- deprecated")
#define print_devclass(d, i)
Definition: subr_bus.c:191
static moduledata_t root_bus_mod
Definition: subr_bus.c:4466
void resource_list_init(struct resource_list *rl)
Initialise a resource list.
Definition: subr_bus.c:2924
struct resource * bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
Helper function for implementing BUS_ALLOC_RESOURCE().
Definition: subr_bus.c:3771
const char * devclass_get_name(devclass_t dc)
Return the name of the devclass.
Definition: subr_bus.c:1306
void bus_enumerate_hinted_children(device_t bus)
Enumerate all hinted devices for this bus.
Definition: subr_bus.c:4561
void device_set_flags(device_t dev, uint32_t flags)
Set the device's flags.
Definition: subr_bus.c:2381
static int root_print_child(device_t dev, device_t child)
Definition: subr_bus.c:4382
const char * name
Definition: kern_fail.c:97
static void devclass_sysctl_init(devclass_t dc)
Definition: subr_bus.c:221
int bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
Wrapper function for BUS_DEACTIVATE_RESOURCE().
Definition: subr_bus.c:4130
device_state_t device_get_state(device_t dev)
Return the device's state.
Definition: subr_bus.c:2475
int resource_disabled(const char *name, int unit)
Definition: subr_hints.c:375
void * device_get_ivars(device_t dev)
Get the device's ivars field.
Definition: subr_bus.c:2453
#define DF_DONENOMATCH
boolean_t devctl_process_running(void)
Return whether the userland process is running.
Definition: subr_bus.c:530
void resource_list_purge(struct resource_list *rl)
Releases all the resources in a list.
Definition: subr_bus.c:3411
SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD,&boothowto, 0,"Boot control flags, passed from loader")
#define print_device_tree_short(d, i)
Definition: subr_bus.c:185
#define print_driver_short(d, i)
Definition: subr_bus.c:187
int devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
Get a list of devices in the devclass.
Definition: subr_bus.c:1367
struct cdev * make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,...)
Definition: kern_conf.c:843
u_long bus_get_resource_count(device_t dev, int type, int rid)
Wrapper function for BUS_GET_RESOURCE().
Definition: subr_bus.c:4278
void kern_psignal(struct proc *p, int sig)
Definition: kern_sig.c:1975
int bus_generic_activate_resource(device_t dev, device_t child, int type, int rid, struct resource *r)
Helper function for implementing BUS_ACTIVATE_RESOURCE().
Definition: subr_bus.c:3805
int bus_generic_resume(device_t dev)
Helper function for implementing DEVICE_RESUME()
Definition: subr_bus.c:3560
int resource_string_value(const char *name, int unit, const char *resname, const char **result)
Definition: subr_hints.c:277
int resource_list_print_type(struct resource_list *rl, const char *name, int type, const char *format)
Print a description of resources in a resource list.
Definition: subr_bus.c:3376
int devclass_get_count(devclass_t dc)
Get the number of devices in a devclass.
Definition: subr_bus.c:1438
static struct cdevsw dev_cdevsw
Definition: subr_bus.c:374
int * type
Definition: cpufreq_if.m:98
int vsnprintf(char *str, size_t size, const char *format, va_list ap)
Definition: subr_prf.c:524
int bus_get_resource(device_t dev, int type, int rid, u_long *startp, u_long *countp)
Wrapper function for BUS_GET_RESOURCE().
Definition: subr_bus.c:4245
void device_claim_softc(device_t dev)
Claim softc.
Definition: subr_bus.c:2437
int resource_list_reserved(struct resource_list *rl, int type, int rid)
Determine if a resource entry is reserved.
Definition: subr_bus.c:3061
u_long rman_get_start(struct resource *r)
Definition: subr_rman.c:809
int device_probe(device_t dev)
Probe a device, and return this status.
Definition: subr_bus.c:2689
struct sysctl_ctx_list * device_get_sysctl_ctx(device_t dev)
Definition: subr_bus.c:2280
static int sysctl_bus(SYSCTL_HANDLER_ARGS)
Definition: subr_bus.c:4753
int resource_list_add_next(struct resource_list *rl, int type, u_long start, u_long end, u_long count)
Add a resource entry.
Definition: subr_bus.c:2964
Implementation of device.
Definition: subr_bus.c:100
void bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
Helper function for implementing BUS_DELETE_RESOURCE().
Definition: subr_bus.c:3963
void devclass_set_parent(devclass_t dc, devclass_t pdc)
Set the parent of a devclass.
Definition: subr_bus.c:1495
static int root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
Definition: subr_bus.c:4393
void device_unbusy(device_t dev)
Decrement the busy counter for the device.
Definition: subr_bus.c:2517
static d_read_t devread
Definition: subr_bus.c:370
#define DF_EXTERNALSOFTC
int device_probe_and_attach(device_t dev)
Probe a device and attach a driver if possible.
Definition: subr_bus.c:2723
static u_int busy
int bus_set_resource(device_t dev, int type, int rid, u_long start, u_long count)
Wrapper function for BUS_SET_RESOURCE().
Definition: subr_bus.c:4231
#define print_devclass_short(d, i)
Definition: subr_bus.c:190
static void devclass_driver_added(devclass_t dc, driver_t *driver)
Register that a device driver has been added to a devclass.
Definition: subr_bus.c:1003
#define DF_QUIET
static int root_bus_module_handler(module_t mod, int what, void *arg)
Definition: subr_bus.c:4441
void bus_generic_new_pass(device_t dev)
Helper function for implementing BUS_NEW_PASS().
Definition: subr_bus.c:3694
void bus_set_pass(int pass)
Raise the current bus pass.
Definition: subr_bus.c:852
TUNABLE_INT("hw.bus.devctl_queue",&devctl_queue_length)
static void device_sysctl_fini(device_t dev)
Definition: subr_bus.c:326
void sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
Definition: kern_sysctl.c:551
int bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq, void *cookie, const char *descr)
Helper function for implementing BUS_DESCRIBE_INTR().
Definition: subr_bus.c:3873
struct cv cv
Definition: subr_bus.c:399
int devclass_find_free_unit(devclass_t dc, int unit)
Find a free unit number in a devclass.
Definition: subr_bus.c:1476
#define DF_DESCMALLOCED
int bus_generic_suspend(device_t dev)
Helper function for implementing DEVICE_SUSPEND()
Definition: subr_bus.c:3535
devclass_t devclass_get_parent(devclass_t dc)
Get the parent of a devclass.
Definition: subr_bus.c:1506
int bus_describe_intr(device_t dev, struct resource *irq, void *cookie, const char *fmt,...)
Wrapper function for BUS_DESCRIBE_INTR().
Definition: subr_bus.c:4210
typedef TAILQ_HEAD(devclass_list, devclass)
Definition: subr_bus.c:79
void resource_list_free(struct resource_list *rl)
Reclaim memory used by a resource list.
Definition: subr_bus.c:2938
device_t root_bus
Definition: subr_bus.c:4437
int bus_generic_detach(device_t dev)
Helper function for implementing DEVICE_DETACH()
Definition: subr_bus.c:3490
int resource_int_value(const char *name, int unit, const char *resname, int *result)
Definition: subr_hints.c:230
bus_dma_tag_t bus_get_dma_tag(device_t dev)
Wrapper function for BUS_GET_DMA_TAG().
Definition: subr_bus.c:4359
int bus_child_present(device_t child)
Wrapper function for BUS_CHILD_PRESENT().
Definition: subr_bus.c:4309
int device_is_alive(device_t dev)
Return non-zero if the device was successfully probed.
Definition: subr_bus.c:2572
static void devinit(void)
Definition: subr_bus.c:408
static d_open_t devopen
Definition: subr_bus.c:368
static int devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
Register that a device driver has been deleted from a devclass.
Definition: subr_bus.c:1111
struct resource_list * bus_generic_get_resource_list(device_t dev, device_t child)
Stub function for implementing BUS_GET_RESOURCE_LIST().
Definition: subr_bus.c:3658
device_t device_get_parent(device_t dev)
Return the parent of a device.
Definition: subr_bus.c:2155
int device_probe_child(device_t dev, device_t child)
Definition: subr_bus.c:1990
int bus_data_generation_check(int generation)
Definition: subr_bus.c:4819
int bus_generic_probe(device_t dev)
Helper function for implementing DEVICE_PROBE()
Definition: subr_bus.c:3440
void bus_release_resources(device_t dev, const struct resource_spec *rs, struct resource **res)
Definition: subr_bus.c:4065
int bus_current_pass
Definition: subr_bus.c:807
struct sysctl_ctx_list * devclass_get_sysctl_ctx(devclass_t dc)
Definition: subr_bus.c:1512
static driverlink_t next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
Definition: subr_bus.c:1974
int device_print_prettyname(device_t dev)
Print the name of the device followed by a colon and a space.
Definition: subr_bus.c:2297
struct device * rman_get_device(struct resource *r)
Definition: subr_rman.c:907
struct sysctl_oid * device_get_sysctl_tree(device_t dev)
Definition: subr_bus.c:2286
#define print_devclass_list_short()
Definition: subr_bus.c:192
int bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
Helper function for implementing BUS_BIND_INTR().
Definition: subr_bus.c:3839
devclass_t device_get_devclass(device_t dev)
Return the current devclass for the device or NULL if there is none.
Definition: subr_bus.c:2224
#define DEVCLANAME(d)
Definition: subr_bus.c:181
static MALLOC_DEFINE(M_BUS,"bus","Bus data structures")
void kobj_delete(kobj_t obj, struct malloc_type *mtype)
Definition: subr_kobj.c:326
int bus_print_child_header(device_t dev, device_t child)
Helper function for implementing BUS_PRINT_CHILD().
Definition: subr_bus.c:3581
static int devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
Allocate a unit number.
Definition: subr_bus.c:1539
static int devctl_queue_length
Definition: subr_bus.c:363
int bus_print_child_footer(device_t dev, device_t child)
Helper function for implementing BUS_PRINT_CHILD().
Definition: subr_bus.c:3604
int bus_child_location_str(device_t child, char *buf, size_t buflen)
Wrapper function for BUS_CHILD_LOCATION_STR().
Definition: subr_bus.c:4340
static struct cdev * devctl_dev
Definition: subr_bus.c:405
__FBSDID("$BSDSUniX$")
int device_detach(device_t dev)
Detach a driver from a device.
Definition: subr_bus.c:2814
void devctl_queue_data(char *data)
Definition: subr_bus.c:593
int bus_generic_attach(device_t dev)
Helper function for implementing DEVICE_ATTACH()
Definition: subr_bus.c:3471
int bus_generic_release_resource(device_t dev, device_t child, int type, int rid, struct resource *r)
Helper function for implementing BUS_RELEASE_RESOURCE().
Definition: subr_bus.c:3788
int bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
Wrapper function for BUS_ACTIVATE_RESOURCE().
Definition: subr_bus.c:4116
SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL)
int device_set_driver(device_t dev, driver_t *driver)
Set the driver of a device.
Definition: subr_bus.c:2626
#define print_driver(d, i)
Definition: subr_bus.c:188
int device_set_devclass(device_t dev, const char *classname)
Set the devclass of a device.
Definition: subr_bus.c:2592
void device_verbose(device_t dev)
Clear the DF_QUIET flag for the device.
Definition: subr_bus.c:2545
int sysctl_handle_int(SYSCTL_HANDLER_ARGS)
Definition: kern_sysctl.c:986
static int root_resume(device_t dev)
Definition: subr_bus.c:4371
void devctl_notify(const char *system, const char *subsystem, const char *type, const char *data)
Definition: subr_bus.c:635
#define DEVCTL_DEFAULT_QUEUE_LEN
Definition: subr_bus.c:361
int bus_free_resource(device_t dev, int type, struct resource *r)
Definition: subr_bus.c:4835
struct sysctl_oid * devclass_get_sysctl_tree(devclass_t dc)
Definition: subr_bus.c:1518
static driver_list_t passes
Definition: subr_bus.c:806
int bus_generic_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
Stub function for implementing BUS_READ_IVAR().
Definition: subr_bus.c:3634
void devctl_queue_data_f(char *data, int flags)
Queue data to be read from the devctl device.
Definition: subr_bus.c:543
static driverlink_t first_matching_driver(devclass_t dc, device_t dev)
Definition: subr_bus.c:1963
#define DEVICENAME(d)
Definition: subr_bus.c:179
static int devclass_delete_device(devclass_t dc, device_t dev)
Delete a device from a devclass.
Definition: subr_bus.c:1662
int bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
Wrapper function for BUS_TEARDOWN_INTR().
Definition: subr_bus.c:4181
int resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child, int type, int rid)
Fully release a reserved resource.
Definition: subr_bus.c:3338
static int devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
Quiesces a set of device drivers from a device class.
Definition: subr_bus.c:1232
void kobj_class_free(kobj_class_t cls)
Definition: subr_kobj.c:233
struct devq devq
Definition: subr_bus.c:401
void device_set_desc_copy(device_t dev, const char *desc)
Set the device's description.
Definition: subr_bus.c:2372
device_t devclass_get_device(devclass_t dc, int unit)
Find a device given a unit number.
Definition: subr_bus.c:1321
void device_set_softc(device_t dev, void *softc)
Set the device's softc field.
Definition: subr_bus.c:2405
int device_shutdown(device_t dev)
Notify a device of system shutdown.
Definition: subr_bus.c:2879
#define DRIVERNAME(d)
Definition: subr_bus.c:180
#define DF_REBID
void cv_init(struct cv *cvp, const char *desc)
Definition: kern_condvar.c:63
#define print_device_tree(d, i)
Definition: subr_bus.c:186
int bus_generic_shutdown(device_t dev)
Helper function for implementing DEVICE_SHUTDOWN()
Definition: subr_bus.c:3514
int resource_list_release(struct resource_list *rl, device_t bus, device_t child, int type, int rid, struct resource *res)
Helper function for implementing BUS_RELEASE_RESOURCE()
Definition: subr_bus.c:3280
#define DF_ENABLED
int uiomove(void *cp, int n, struct uio *uio)
Definition: subr_uio.c:202
void root_bus_configure(void)
Automatically configure devices.
Definition: subr_bus.c:4480
void devctl_notify_f(const char *system, const char *subsystem, const char *type, const char *data, int flags)
Send a 'notification' to userland, using standard ways.
Definition: subr_bus.c:603
#define DF_FIXEDCLASS
int queued
Definition: subr_bus.c:397
int devclass_get_maxunit(devclass_t dc)
Get the maximum unit number used in a devclass.
Definition: subr_bus.c:1459
void free(void *addr, struct malloc_type *mtp)
Definition: kern_malloc.c:554
#define print_driver_list(d, i)
Definition: subr_bus.c:189
int printf(const char *fmt,...)
Definition: subr_prf.c:367
struct resource * bus_generic_rl_alloc_resource(device_t dev, device_t child, int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
Helper function for implementing BUS_ALLOC_RESOURCE().
Definition: subr_bus.c:4008
static int root_child_present(device_t dev, device_t child)
Definition: subr_bus.c:4410
DEFINE_CLASS(null, null_methods, 0)
int devclass_delete_driver(devclass_t busclass, driver_t *driver)
Delete a device driver from a device class.
Definition: subr_bus.c:1177
struct resource_list_entry * resource_list_find(struct resource_list *rl, int type, int rid)
Find a resource entry by type and rid.
Definition: subr_bus.c:3082
linker_file_t * result
Definition: linker_if.m:136
static devclass_list_t devclasses
Definition: subr_bus.c:893
void * device_get_softc(device_t dev)
Return the device's softc field.
Definition: subr_bus.c:2393
static void device_sysctl_init(device_t dev)
Definition: subr_bus.c:281
struct resource * resource_list_reserve(struct resource_list *rl, device_t bus, device_t child, int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
Allocate a reserved resource.
Definition: subr_bus.c:3150
void mtx_init(struct mtx *m, const char *name, const char *type, int opts)
Definition: kern_mutex.c:837
static d_close_t devclose
Definition: subr_bus.c:369
int bus_generic_print_child(device_t dev, device_t child)
Helper function for implementing BUS_PRINT_CHILD().
Definition: subr_bus.c:3618
int bus_alloc_resources(device_t dev, struct resource_spec *rs, struct resource **res)
Definition: subr_bus.c:4046
int bus_release_resource(device_t dev, int type, int rid, struct resource *r)
Wrapper function for BUS_RELEASE_RESOURCE().
Definition: subr_bus.c:4144
#define PDEBUG(a)
Definition: subr_bus.c:178
struct mtx mtx
Definition: subr_bus.c:398
u_long rman_get_end(struct resource *r)
Definition: subr_rman.c:823
DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST)
#define print_device(d, i)
Definition: subr_bus.c:184
void kobj_class_compile(kobj_class_t cls)
Definition: subr_kobj.c:127
const char * device_get_nameunit(device_t dev)
Return a string containing the device's devclass name followed by an ascii representation of the devi...
Definition: subr_bus.c:2247
int device_attach(device_t dev)
Attach a device driver to a device.
Definition: subr_bus.c:2761
int bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start, u_long end)
Wrapper function for BUS_ADJUST_RESOURCE().
Definition: subr_bus.c:4101
int device_delete_child(device_t dev, device_t child)
Delete a device.
Definition: subr_bus.c:1860
const char * device_get_name(device_t dev)
Return the name of the device's devclass or NULL if there is none.
Definition: subr_bus.c:2234
int vprintf(const char *fmt, va_list ap)
Definition: subr_prf.c:380
int bus_generic_rl_release_resource(device_t dev, device_t child, int type, int rid, struct resource *r)
Helper function for implementing BUS_RELEASE_RESOURCE().
Definition: subr_bus.c:3984
struct driverlink * driverlink_t
Definition: subr_bus.c:68
static d_ioctl_t devioctl
Definition: subr_bus.c:371
driver_t * device_get_driver(device_t dev)
Return the current driver for the device or NULL if there is no driver currently attached.
Definition: subr_bus.c:2214
void device_set_desc(device_t dev, const char *desc)
Set the device's description.
Definition: subr_bus.c:2360
int sysctl_ctx_free(struct sysctl_ctx_list *clist)
Definition: kern_sysctl.c:278
int bus_bind_intr(device_t dev, struct resource *r, int cpu)
Wrapper function for BUS_BIND_INTR().
Definition: subr_bus.c:4195
int bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig, enum intr_polarity pol)
Helper function for implementing BUS_CONFIG_INTR().
Definition: subr_bus.c:3856
int device_get_children(device_t dev, device_t **devlistp, int *devcountp)
Get a list of children of a device.
Definition: subr_bus.c:2177
void kobj_init(kobj_t obj, kobj_class_t cls)
Definition: subr_kobj.c:290
struct resource_list_entry * resource_list_add(struct resource_list *rl, int type, int rid, u_long start, u_long end, u_long count)
Add or modify a resource entry.
Definition: subr_bus.c:2992
int bus_generic_deactivate_resource(device_t dev, device_t child, int type, int rid, struct resource *r)
Helper function for implementing BUS_DEACTIVATE_RESOURCE().
Definition: subr_bus.c:3822
int device_set_unit(device_t dev, int unit)
Set the unit number of a device.
Definition: subr_bus.c:2893
void device_free_softc(void *softc)
Free claimed softc.
Definition: subr_bus.c:2423
devclass_t devclass_find(const char *classname)
Find a device class.
Definition: subr_bus.c:980
uint32_t device_get_flags(device_t dev)
Return the device's flags.
Definition: subr_bus.c:2274
static d_poll_t devpoll
Definition: subr_bus.c:372
void bus_data_generation_update(void)
Definition: subr_bus.c:4829
static int device_sysctl_handler(SYSCTL_HANDLER_ARGS)
Definition: subr_bus.c:245
static device_t make_device(device_t parent, const char *name, int unit)
Make a new device and add it as a child of parent.
Definition: subr_bus.c:1694
void device_set_ivars(device_t dev, void *ivars)
Set the device's ivars field.
Definition: subr_bus.c:2464
int driver_module_handler(module_t mod, int what, void *arg)
Module handler for registering device drivers.
Definition: subr_bus.c:4498
static void devaddq(const char *type, const char *what, device_t dev)
Definition: subr_bus.c:657
static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
Definition: subr_bus.c:767
bus_dma_tag_t bus_generic_get_dma_tag(device_t dev, device_t child)
Helper function for implementing BUS_GET_DMA_TAG().
Definition: subr_bus.c:3891
devclass_t devclass_create(const char *classname)
Create a device class.
Definition: subr_bus.c:966
KOBJ_FIELDS
Definition: subr_bus.c:105
static void devadded(device_t dev)
Definition: subr_bus.c:711
#define DC_HAS_CHILDREN
static int devclass_add_device(devclass_t dc, device_t dev)
Add a device to a devclass.
Definition: subr_bus.c:1624
device_t device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
Create a new device.
Definition: subr_bus.c:1808
int * count
Definition: cpufreq_if.m:63
struct proc * async_proc
Definition: subr_bus.c:402
int sysctl_ctx_init(struct sysctl_ctx_list *c)
Definition: kern_sysctl.c:260