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