subr_device.c revision 1.1.12.1 1 1.1.12.1 yamt /* $NetBSD: subr_device.c,v 1.1.12.1 2010/03/11 15:04:18 yamt Exp $ */
2 1.1.12.1 yamt
3 1.1.12.1 yamt /*
4 1.1.12.1 yamt * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1.12.1 yamt * All rights reserved.
6 1.1.12.1 yamt *
7 1.1.12.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.12.1 yamt * modification, are permitted provided that the following conditions
9 1.1.12.1 yamt * are met:
10 1.1.12.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.12.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.12.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.12.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1.12.1 yamt * documentation and/or other materials provided with the distribution.
15 1.1.12.1 yamt *
16 1.1.12.1 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.12.1 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.12.1 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.12.1 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.12.1 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.12.1 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.12.1 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.12.1 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.12.1 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.12.1 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.12.1 yamt * POSSIBILITY OF SUCH DAMAGE.
27 1.1.12.1 yamt */
28 1.1.12.1 yamt
29 1.1.12.1 yamt #include <sys/cdefs.h>
30 1.1.12.1 yamt __KERNEL_RCSID(0, "$NetBSD: subr_device.c,v 1.1.12.1 2010/03/11 15:04:18 yamt Exp $");
31 1.1.12.1 yamt
32 1.1.12.1 yamt #include <sys/param.h>
33 1.1.12.1 yamt #include <sys/device.h>
34 1.1.12.1 yamt #include <sys/systm.h>
35 1.1.12.1 yamt
36 1.1.12.1 yamt /*
37 1.1.12.1 yamt * Accessor functions for the device_t type.
38 1.1.12.1 yamt */
39 1.1.12.1 yamt devclass_t
40 1.1.12.1 yamt device_class(device_t dev)
41 1.1.12.1 yamt {
42 1.1.12.1 yamt
43 1.1.12.1 yamt return dev->dv_class;
44 1.1.12.1 yamt }
45 1.1.12.1 yamt
46 1.1.12.1 yamt cfdata_t
47 1.1.12.1 yamt device_cfdata(device_t dev)
48 1.1.12.1 yamt {
49 1.1.12.1 yamt
50 1.1.12.1 yamt return dev->dv_cfdata;
51 1.1.12.1 yamt }
52 1.1.12.1 yamt
53 1.1.12.1 yamt cfdriver_t
54 1.1.12.1 yamt device_cfdriver(device_t dev)
55 1.1.12.1 yamt {
56 1.1.12.1 yamt
57 1.1.12.1 yamt return dev->dv_cfdriver;
58 1.1.12.1 yamt }
59 1.1.12.1 yamt
60 1.1.12.1 yamt cfattach_t
61 1.1.12.1 yamt device_cfattach(device_t dev)
62 1.1.12.1 yamt {
63 1.1.12.1 yamt
64 1.1.12.1 yamt return dev->dv_cfattach;
65 1.1.12.1 yamt }
66 1.1.12.1 yamt
67 1.1.12.1 yamt int
68 1.1.12.1 yamt device_unit(device_t dev)
69 1.1.12.1 yamt {
70 1.1.12.1 yamt
71 1.1.12.1 yamt return dev->dv_unit;
72 1.1.12.1 yamt }
73 1.1.12.1 yamt
74 1.1.12.1 yamt const char *
75 1.1.12.1 yamt device_xname(device_t dev)
76 1.1.12.1 yamt {
77 1.1.12.1 yamt
78 1.1.12.1 yamt return dev->dv_xname;
79 1.1.12.1 yamt }
80 1.1.12.1 yamt
81 1.1.12.1 yamt device_t
82 1.1.12.1 yamt device_parent(device_t dev)
83 1.1.12.1 yamt {
84 1.1.12.1 yamt
85 1.1.12.1 yamt return dev->dv_parent;
86 1.1.12.1 yamt }
87 1.1.12.1 yamt
88 1.1.12.1 yamt bool
89 1.1.12.1 yamt device_activation(device_t dev, devact_level_t level)
90 1.1.12.1 yamt {
91 1.1.12.1 yamt int active_flags;
92 1.1.12.1 yamt
93 1.1.12.1 yamt active_flags = DVF_ACTIVE;
94 1.1.12.1 yamt switch (level) {
95 1.1.12.1 yamt case DEVACT_LEVEL_FULL:
96 1.1.12.1 yamt active_flags |= DVF_CLASS_SUSPENDED;
97 1.1.12.1 yamt /*FALLTHROUGH*/
98 1.1.12.1 yamt case DEVACT_LEVEL_DRIVER:
99 1.1.12.1 yamt active_flags |= DVF_DRIVER_SUSPENDED;
100 1.1.12.1 yamt /*FALLTHROUGH*/
101 1.1.12.1 yamt case DEVACT_LEVEL_BUS:
102 1.1.12.1 yamt active_flags |= DVF_BUS_SUSPENDED;
103 1.1.12.1 yamt break;
104 1.1.12.1 yamt }
105 1.1.12.1 yamt
106 1.1.12.1 yamt return (dev->dv_flags & active_flags) == DVF_ACTIVE;
107 1.1.12.1 yamt }
108 1.1.12.1 yamt
109 1.1.12.1 yamt bool
110 1.1.12.1 yamt device_is_active(device_t dev)
111 1.1.12.1 yamt {
112 1.1.12.1 yamt int active_flags;
113 1.1.12.1 yamt
114 1.1.12.1 yamt active_flags = DVF_ACTIVE;
115 1.1.12.1 yamt active_flags |= DVF_CLASS_SUSPENDED;
116 1.1.12.1 yamt active_flags |= DVF_DRIVER_SUSPENDED;
117 1.1.12.1 yamt active_flags |= DVF_BUS_SUSPENDED;
118 1.1.12.1 yamt
119 1.1.12.1 yamt return (dev->dv_flags & active_flags) == DVF_ACTIVE;
120 1.1.12.1 yamt }
121 1.1.12.1 yamt
122 1.1.12.1 yamt bool
123 1.1.12.1 yamt device_is_enabled(device_t dev)
124 1.1.12.1 yamt {
125 1.1.12.1 yamt return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
126 1.1.12.1 yamt }
127 1.1.12.1 yamt
128 1.1.12.1 yamt bool
129 1.1.12.1 yamt device_has_power(device_t dev)
130 1.1.12.1 yamt {
131 1.1.12.1 yamt int active_flags;
132 1.1.12.1 yamt
133 1.1.12.1 yamt active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
134 1.1.12.1 yamt
135 1.1.12.1 yamt return (dev->dv_flags & active_flags) == DVF_ACTIVE;
136 1.1.12.1 yamt }
137 1.1.12.1 yamt
138 1.1.12.1 yamt int
139 1.1.12.1 yamt device_locator(device_t dev, u_int locnum)
140 1.1.12.1 yamt {
141 1.1.12.1 yamt
142 1.1.12.1 yamt KASSERT(dev->dv_locators != NULL);
143 1.1.12.1 yamt return dev->dv_locators[locnum];
144 1.1.12.1 yamt }
145 1.1.12.1 yamt
146 1.1.12.1 yamt void *
147 1.1.12.1 yamt device_private(device_t dev)
148 1.1.12.1 yamt {
149 1.1.12.1 yamt
150 1.1.12.1 yamt /*
151 1.1.12.1 yamt * The reason why device_private(NULL) is allowed is to simplify the
152 1.1.12.1 yamt * work of a lot of userspace request handlers (i.e., c/bdev
153 1.1.12.1 yamt * handlers) which grab cfdriver_t->cd_units[n].
154 1.1.12.1 yamt * It avoids having them test for it to be NULL and only then calling
155 1.1.12.1 yamt * device_private.
156 1.1.12.1 yamt */
157 1.1.12.1 yamt return dev == NULL ? NULL : dev->dv_private;
158 1.1.12.1 yamt }
159 1.1.12.1 yamt
160 1.1.12.1 yamt prop_dictionary_t
161 1.1.12.1 yamt device_properties(device_t dev)
162 1.1.12.1 yamt {
163 1.1.12.1 yamt
164 1.1.12.1 yamt return dev->dv_properties;
165 1.1.12.1 yamt }
166 1.1.12.1 yamt
167 1.1.12.1 yamt /*
168 1.1.12.1 yamt * device_is_a:
169 1.1.12.1 yamt *
170 1.1.12.1 yamt * Returns true if the device is an instance of the specified
171 1.1.12.1 yamt * driver.
172 1.1.12.1 yamt */
173 1.1.12.1 yamt bool
174 1.1.12.1 yamt device_is_a(device_t dev, const char *dname)
175 1.1.12.1 yamt {
176 1.1.12.1 yamt
177 1.1.12.1 yamt return strcmp(dev->dv_cfdriver->cd_name, dname) == 0;
178 1.1.12.1 yamt }
179