kern_pmf.c revision 1.9.4.2 1 1.9.4.2 ad /* $NetBSD: kern_pmf.c,v 1.9.4.2 2007/12/26 19:17:22 ad Exp $ */
2 1.9.4.2 ad
3 1.9.4.2 ad /*-
4 1.9.4.2 ad * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.9.4.2 ad * All rights reserved.
6 1.9.4.2 ad *
7 1.9.4.2 ad * Redistribution and use in source and binary forms, with or without
8 1.9.4.2 ad * modification, are permitted provided that the following conditions
9 1.9.4.2 ad * are met:
10 1.9.4.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.9.4.2 ad * notice, this list of conditions and the following disclaimer.
12 1.9.4.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.9.4.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.9.4.2 ad * documentation and/or other materials provided with the distribution.
15 1.9.4.2 ad * 3. All advertising materials mentioning features or use of this software
16 1.9.4.2 ad * must display the following acknowledgement:
17 1.9.4.2 ad * This product includes software developed by Jared D. McNeill.
18 1.9.4.2 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
19 1.9.4.2 ad * contributors may be used to endorse or promote products derived
20 1.9.4.2 ad * from this software without specific prior written permission.
21 1.9.4.2 ad *
22 1.9.4.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.9.4.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.9.4.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.9.4.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.9.4.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.9.4.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.9.4.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.9.4.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.9.4.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.9.4.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.9.4.2 ad * POSSIBILITY OF SUCH DAMAGE.
33 1.9.4.2 ad */
34 1.9.4.2 ad
35 1.9.4.2 ad #include <sys/cdefs.h>
36 1.9.4.2 ad __KERNEL_RCSID(0, "$NetBSD: kern_pmf.c,v 1.9.4.2 2007/12/26 19:17:22 ad Exp $");
37 1.9.4.2 ad
38 1.9.4.2 ad #include <sys/types.h>
39 1.9.4.2 ad #include <sys/param.h>
40 1.9.4.2 ad #include <sys/malloc.h>
41 1.9.4.2 ad #include <sys/buf.h>
42 1.9.4.2 ad #include <sys/callout.h>
43 1.9.4.2 ad #include <sys/kernel.h>
44 1.9.4.2 ad #include <sys/device.h>
45 1.9.4.2 ad #include <sys/pmf.h>
46 1.9.4.2 ad #include <sys/queue.h>
47 1.9.4.2 ad #include <sys/syscallargs.h> /* for sys_sync */
48 1.9.4.2 ad #include <sys/workqueue.h>
49 1.9.4.2 ad #include <prop/proplib.h>
50 1.9.4.2 ad
51 1.9.4.2 ad #ifdef PMF_DEBUG
52 1.9.4.2 ad int pmf_debug_event;
53 1.9.4.2 ad int pmf_debug_idle;
54 1.9.4.2 ad int pmf_debug_transition;
55 1.9.4.2 ad
56 1.9.4.2 ad #define PMF_EVENT_PRINTF(x) if (pmf_debug_event) printf x
57 1.9.4.2 ad #define PMF_IDLE_PRINTF(x) if (pmf_debug_idle) printf x
58 1.9.4.2 ad #define PMF_TRANSITION_PRINTF(x) if (pmf_debug_transition) printf x
59 1.9.4.2 ad #define PMF_TRANSITION_PRINTF2(y,x) if (pmf_debug_transition>y) printf x
60 1.9.4.2 ad #else
61 1.9.4.2 ad #define PMF_EVENT_PRINTF(x) do { } while (0)
62 1.9.4.2 ad #define PMF_IDLE_PRINTF(x) do { } while (0)
63 1.9.4.2 ad #define PMF_TRANSITION_PRINTF(x) do { } while (0)
64 1.9.4.2 ad #define PMF_TRANSITION_PRINTF2(y,x) do { } while (0)
65 1.9.4.2 ad #endif
66 1.9.4.2 ad
67 1.9.4.2 ad /* #define PMF_DEBUG */
68 1.9.4.2 ad
69 1.9.4.2 ad MALLOC_DEFINE(M_PMF, "pmf", "device pmf messaging memory");
70 1.9.4.2 ad
71 1.9.4.2 ad static prop_dictionary_t pmf_platform = NULL;
72 1.9.4.2 ad static struct workqueue *pmf_event_workqueue;
73 1.9.4.2 ad
74 1.9.4.2 ad typedef struct pmf_event_handler {
75 1.9.4.2 ad TAILQ_ENTRY(pmf_event_handler) pmf_link;
76 1.9.4.2 ad pmf_generic_event_t pmf_event;
77 1.9.4.2 ad void (*pmf_handler)(device_t);
78 1.9.4.2 ad device_t pmf_device;
79 1.9.4.2 ad bool pmf_global;
80 1.9.4.2 ad } pmf_event_handler_t;
81 1.9.4.2 ad
82 1.9.4.2 ad static TAILQ_HEAD(, pmf_event_handler) pmf_all_events =
83 1.9.4.2 ad TAILQ_HEAD_INITIALIZER(pmf_all_events);
84 1.9.4.2 ad
85 1.9.4.2 ad typedef struct pmf_event_workitem {
86 1.9.4.2 ad struct work pew_work;
87 1.9.4.2 ad pmf_generic_event_t pew_event;
88 1.9.4.2 ad device_t pew_device;
89 1.9.4.2 ad } pmf_event_workitem_t;
90 1.9.4.2 ad
91 1.9.4.2 ad static void
92 1.9.4.2 ad pmf_event_worker(struct work *wk, void *dummy)
93 1.9.4.2 ad {
94 1.9.4.2 ad pmf_event_workitem_t *pew;
95 1.9.4.2 ad pmf_event_handler_t *event;
96 1.9.4.2 ad
97 1.9.4.2 ad pew = (void *)wk;
98 1.9.4.2 ad KASSERT(wk == &pew->pew_work);
99 1.9.4.2 ad KASSERT(pew != NULL);
100 1.9.4.2 ad
101 1.9.4.2 ad TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
102 1.9.4.2 ad if (event->pmf_event != pew->pew_event)
103 1.9.4.2 ad continue;
104 1.9.4.2 ad if (event->pmf_device == pew->pew_device || event->pmf_global)
105 1.9.4.2 ad (*event->pmf_handler)(event->pmf_device);
106 1.9.4.2 ad }
107 1.9.4.2 ad
108 1.9.4.2 ad free(pew, M_TEMP);
109 1.9.4.2 ad
110 1.9.4.2 ad return;
111 1.9.4.2 ad }
112 1.9.4.2 ad
113 1.9.4.2 ad static bool
114 1.9.4.2 ad pmf_check_system_drivers(void)
115 1.9.4.2 ad {
116 1.9.4.2 ad device_t curdev;
117 1.9.4.2 ad bool unsupported_devs;
118 1.9.4.2 ad
119 1.9.4.2 ad unsupported_devs = false;
120 1.9.4.2 ad TAILQ_FOREACH(curdev, &alldevs, dv_list) {
121 1.9.4.2 ad if (device_pmf_is_registered(curdev))
122 1.9.4.2 ad continue;
123 1.9.4.2 ad if (!unsupported_devs)
124 1.9.4.2 ad printf("Devices without power management support:");
125 1.9.4.2 ad printf(" %s", device_xname(curdev));
126 1.9.4.2 ad unsupported_devs = true;
127 1.9.4.2 ad }
128 1.9.4.2 ad if (unsupported_devs) {
129 1.9.4.2 ad printf("\n");
130 1.9.4.2 ad return false;
131 1.9.4.2 ad }
132 1.9.4.2 ad return true;
133 1.9.4.2 ad }
134 1.9.4.2 ad
135 1.9.4.2 ad bool
136 1.9.4.2 ad pmf_system_bus_resume(void)
137 1.9.4.2 ad {
138 1.9.4.2 ad int depth, maxdepth;
139 1.9.4.2 ad bool rv;
140 1.9.4.2 ad device_t curdev;
141 1.9.4.2 ad
142 1.9.4.2 ad maxdepth = 0;
143 1.9.4.2 ad TAILQ_FOREACH(curdev, &alldevs, dv_list) {
144 1.9.4.2 ad if (curdev->dv_depth > maxdepth)
145 1.9.4.2 ad maxdepth = curdev->dv_depth;
146 1.9.4.2 ad }
147 1.9.4.2 ad ++maxdepth;
148 1.9.4.2 ad
149 1.9.4.2 ad aprint_debug("Powering devices:");
150 1.9.4.2 ad /* D0 handlers are run in order */
151 1.9.4.2 ad depth = 0;
152 1.9.4.2 ad rv = true;
153 1.9.4.2 ad for (depth = 0; depth < maxdepth; ++depth) {
154 1.9.4.2 ad TAILQ_FOREACH(curdev, &alldevs, dv_list) {
155 1.9.4.2 ad if (!device_pmf_is_registered(curdev))
156 1.9.4.2 ad continue;
157 1.9.4.2 ad if (device_is_active(curdev) ||
158 1.9.4.2 ad !device_is_enabled(curdev))
159 1.9.4.2 ad continue;
160 1.9.4.2 ad if (curdev->dv_depth != depth)
161 1.9.4.2 ad continue;
162 1.9.4.2 ad
163 1.9.4.2 ad aprint_debug(" %s", device_xname(curdev));
164 1.9.4.2 ad
165 1.9.4.2 ad if (!device_pmf_bus_resume(curdev))
166 1.9.4.2 ad aprint_debug("(failed)");
167 1.9.4.2 ad }
168 1.9.4.2 ad }
169 1.9.4.2 ad aprint_debug("\n");
170 1.9.4.2 ad
171 1.9.4.2 ad return rv;
172 1.9.4.2 ad }
173 1.9.4.2 ad
174 1.9.4.2 ad bool
175 1.9.4.2 ad pmf_system_resume(void)
176 1.9.4.2 ad {
177 1.9.4.2 ad int depth, maxdepth;
178 1.9.4.2 ad bool rv;
179 1.9.4.2 ad device_t curdev, parent;
180 1.9.4.2 ad
181 1.9.4.2 ad if (!pmf_check_system_drivers())
182 1.9.4.2 ad return false;
183 1.9.4.2 ad
184 1.9.4.2 ad maxdepth = 0;
185 1.9.4.2 ad TAILQ_FOREACH(curdev, &alldevs, dv_list) {
186 1.9.4.2 ad if (curdev->dv_depth > maxdepth)
187 1.9.4.2 ad maxdepth = curdev->dv_depth;
188 1.9.4.2 ad }
189 1.9.4.2 ad ++maxdepth;
190 1.9.4.2 ad
191 1.9.4.2 ad aprint_debug("Resuming devices:");
192 1.9.4.2 ad /* D0 handlers are run in order */
193 1.9.4.2 ad depth = 0;
194 1.9.4.2 ad rv = true;
195 1.9.4.2 ad for (depth = 0; depth < maxdepth; ++depth) {
196 1.9.4.2 ad TAILQ_FOREACH(curdev, &alldevs, dv_list) {
197 1.9.4.2 ad if (device_is_active(curdev) ||
198 1.9.4.2 ad !device_is_enabled(curdev))
199 1.9.4.2 ad continue;
200 1.9.4.2 ad if (curdev->dv_depth != depth)
201 1.9.4.2 ad continue;
202 1.9.4.2 ad parent = device_parent(curdev);
203 1.9.4.2 ad if (parent != NULL &&
204 1.9.4.2 ad !device_is_active(parent))
205 1.9.4.2 ad continue;
206 1.9.4.2 ad
207 1.9.4.2 ad aprint_debug(" %s", device_xname(curdev));
208 1.9.4.2 ad
209 1.9.4.2 ad if (!pmf_device_resume(curdev)) {
210 1.9.4.2 ad rv = false;
211 1.9.4.2 ad aprint_debug("(failed)");
212 1.9.4.2 ad }
213 1.9.4.2 ad }
214 1.9.4.2 ad }
215 1.9.4.2 ad aprint_debug(".\n");
216 1.9.4.2 ad
217 1.9.4.2 ad return rv;
218 1.9.4.2 ad }
219 1.9.4.2 ad
220 1.9.4.2 ad bool
221 1.9.4.2 ad pmf_system_suspend(void)
222 1.9.4.2 ad {
223 1.9.4.2 ad int depth, maxdepth;
224 1.9.4.2 ad device_t curdev;
225 1.9.4.2 ad
226 1.9.4.2 ad if (!pmf_check_system_drivers())
227 1.9.4.2 ad return false;
228 1.9.4.2 ad
229 1.9.4.2 ad /*
230 1.9.4.2 ad * Flush buffers only if the shutdown didn't do so
231 1.9.4.2 ad * already and if there was no panic.
232 1.9.4.2 ad */
233 1.9.4.2 ad if (doing_shutdown == 0 && panicstr == NULL) {
234 1.9.4.2 ad printf("Flushing disk caches: ");
235 1.9.4.2 ad sys_sync(NULL, NULL, NULL);
236 1.9.4.2 ad if (buf_syncwait() != 0)
237 1.9.4.2 ad printf("giving up\n");
238 1.9.4.2 ad else
239 1.9.4.2 ad printf("done\n");
240 1.9.4.2 ad }
241 1.9.4.2 ad
242 1.9.4.2 ad aprint_debug("Suspending devices:");
243 1.9.4.2 ad
244 1.9.4.2 ad maxdepth = 0;
245 1.9.4.2 ad TAILQ_FOREACH(curdev, &alldevs, dv_list) {
246 1.9.4.2 ad if (curdev->dv_depth > maxdepth)
247 1.9.4.2 ad maxdepth = curdev->dv_depth;
248 1.9.4.2 ad }
249 1.9.4.2 ad
250 1.9.4.2 ad for (depth = maxdepth; depth >= 0; --depth) {
251 1.9.4.2 ad TAILQ_FOREACH_REVERSE(curdev, &alldevs, devicelist, dv_list) {
252 1.9.4.2 ad if (curdev->dv_depth != depth)
253 1.9.4.2 ad continue;
254 1.9.4.2 ad if (!device_is_active(curdev))
255 1.9.4.2 ad continue;
256 1.9.4.2 ad
257 1.9.4.2 ad aprint_debug(" %s", device_xname(curdev));
258 1.9.4.2 ad
259 1.9.4.2 ad /* XXX joerg check return value and abort suspend */
260 1.9.4.2 ad if (!pmf_device_suspend(curdev))
261 1.9.4.2 ad aprint_debug("(failed)");
262 1.9.4.2 ad }
263 1.9.4.2 ad }
264 1.9.4.2 ad
265 1.9.4.2 ad aprint_debug(".\n");
266 1.9.4.2 ad
267 1.9.4.2 ad return true;
268 1.9.4.2 ad }
269 1.9.4.2 ad
270 1.9.4.2 ad void
271 1.9.4.2 ad pmf_system_shutdown(void)
272 1.9.4.2 ad {
273 1.9.4.2 ad int depth, maxdepth;
274 1.9.4.2 ad device_t curdev;
275 1.9.4.2 ad
276 1.9.4.2 ad if (!pmf_check_system_drivers())
277 1.9.4.2 ad delay(2000000);
278 1.9.4.2 ad
279 1.9.4.2 ad aprint_debug("Shutting down devices:");
280 1.9.4.2 ad
281 1.9.4.2 ad maxdepth = 0;
282 1.9.4.2 ad TAILQ_FOREACH(curdev, &alldevs, dv_list) {
283 1.9.4.2 ad if (curdev->dv_depth > maxdepth)
284 1.9.4.2 ad maxdepth = curdev->dv_depth;
285 1.9.4.2 ad }
286 1.9.4.2 ad
287 1.9.4.2 ad for (depth = maxdepth; depth >= 0; --depth) {
288 1.9.4.2 ad TAILQ_FOREACH_REVERSE(curdev, &alldevs, devicelist, dv_list) {
289 1.9.4.2 ad if (curdev->dv_depth != depth)
290 1.9.4.2 ad continue;
291 1.9.4.2 ad if (!device_is_active(curdev))
292 1.9.4.2 ad continue;
293 1.9.4.2 ad
294 1.9.4.2 ad aprint_debug(" %s", device_xname(curdev));
295 1.9.4.2 ad
296 1.9.4.2 ad if (!device_pmf_is_registered(curdev))
297 1.9.4.2 ad continue;
298 1.9.4.2 ad if (!device_pmf_class_suspend(curdev)) {
299 1.9.4.2 ad aprint_debug("(failed)");
300 1.9.4.2 ad continue;
301 1.9.4.2 ad }
302 1.9.4.2 ad if (!device_pmf_driver_suspend(curdev)) {
303 1.9.4.2 ad aprint_debug("(failed)");
304 1.9.4.2 ad continue;
305 1.9.4.2 ad }
306 1.9.4.2 ad }
307 1.9.4.2 ad }
308 1.9.4.2 ad
309 1.9.4.2 ad aprint_debug(".\n");
310 1.9.4.2 ad }
311 1.9.4.2 ad
312 1.9.4.2 ad bool
313 1.9.4.2 ad pmf_set_platform(const char *key, const char *value)
314 1.9.4.2 ad {
315 1.9.4.2 ad if (pmf_platform == NULL)
316 1.9.4.2 ad pmf_platform = prop_dictionary_create();
317 1.9.4.2 ad if (pmf_platform == NULL)
318 1.9.4.2 ad return false;
319 1.9.4.2 ad
320 1.9.4.2 ad return prop_dictionary_set_cstring(pmf_platform, key, value);
321 1.9.4.2 ad }
322 1.9.4.2 ad
323 1.9.4.2 ad const char *
324 1.9.4.2 ad pmf_get_platform(const char *key)
325 1.9.4.2 ad {
326 1.9.4.2 ad const char *value;
327 1.9.4.2 ad
328 1.9.4.2 ad if (pmf_platform == NULL)
329 1.9.4.2 ad return NULL;
330 1.9.4.2 ad
331 1.9.4.2 ad if (!prop_dictionary_get_cstring_nocopy(pmf_platform, key, &value))
332 1.9.4.2 ad return NULL;
333 1.9.4.2 ad
334 1.9.4.2 ad return value;
335 1.9.4.2 ad }
336 1.9.4.2 ad
337 1.9.4.2 ad bool
338 1.9.4.2 ad pmf_device_register(device_t dev,
339 1.9.4.2 ad bool (*suspend)(device_t), bool (*resume)(device_t))
340 1.9.4.2 ad {
341 1.9.4.2 ad device_pmf_driver_register(dev, suspend, resume);
342 1.9.4.2 ad
343 1.9.4.2 ad if (!device_pmf_driver_child_register(dev)) {
344 1.9.4.2 ad device_pmf_driver_deregister(dev);
345 1.9.4.2 ad return false;
346 1.9.4.2 ad }
347 1.9.4.2 ad
348 1.9.4.2 ad return true;
349 1.9.4.2 ad }
350 1.9.4.2 ad
351 1.9.4.2 ad void
352 1.9.4.2 ad pmf_device_deregister(device_t dev)
353 1.9.4.2 ad {
354 1.9.4.2 ad device_pmf_class_deregister(dev);
355 1.9.4.2 ad device_pmf_bus_deregister(dev);
356 1.9.4.2 ad device_pmf_driver_deregister(dev);
357 1.9.4.2 ad }
358 1.9.4.2 ad
359 1.9.4.2 ad bool
360 1.9.4.2 ad pmf_device_suspend(device_t dev)
361 1.9.4.2 ad {
362 1.9.4.2 ad PMF_TRANSITION_PRINTF(("%s: suspend enter\n", device_xname(dev)));
363 1.9.4.2 ad if (!device_pmf_is_registered(dev))
364 1.9.4.2 ad return false;
365 1.9.4.2 ad PMF_TRANSITION_PRINTF2(1, ("%s: class suspend\n", device_xname(dev)));
366 1.9.4.2 ad if (!device_pmf_class_suspend(dev))
367 1.9.4.2 ad return false;
368 1.9.4.2 ad PMF_TRANSITION_PRINTF2(1, ("%s: driver suspend\n", device_xname(dev)));
369 1.9.4.2 ad if (!device_pmf_driver_suspend(dev))
370 1.9.4.2 ad return false;
371 1.9.4.2 ad PMF_TRANSITION_PRINTF2(1, ("%s: bus suspend\n", device_xname(dev)));
372 1.9.4.2 ad if (!device_pmf_bus_suspend(dev))
373 1.9.4.2 ad return false;
374 1.9.4.2 ad PMF_TRANSITION_PRINTF(("%s: suspend exit\n", device_xname(dev)));
375 1.9.4.2 ad return true;
376 1.9.4.2 ad }
377 1.9.4.2 ad
378 1.9.4.2 ad bool
379 1.9.4.2 ad pmf_device_resume(device_t dev)
380 1.9.4.2 ad {
381 1.9.4.2 ad PMF_TRANSITION_PRINTF(("%s: resume enter\n", device_xname(dev)));
382 1.9.4.2 ad if (!device_pmf_is_registered(dev))
383 1.9.4.2 ad return false;
384 1.9.4.2 ad PMF_TRANSITION_PRINTF2(1, ("%s: bus resume\n", device_xname(dev)));
385 1.9.4.2 ad if (!device_pmf_bus_resume(dev))
386 1.9.4.2 ad return false;
387 1.9.4.2 ad PMF_TRANSITION_PRINTF2(1, ("%s: driver resume\n", device_xname(dev)));
388 1.9.4.2 ad if (!device_pmf_driver_resume(dev))
389 1.9.4.2 ad return false;
390 1.9.4.2 ad PMF_TRANSITION_PRINTF2(1, ("%s: class resume\n", device_xname(dev)));
391 1.9.4.2 ad if (!device_pmf_class_resume(dev))
392 1.9.4.2 ad return false;
393 1.9.4.2 ad PMF_TRANSITION_PRINTF(("%s: resume exit\n", device_xname(dev)));
394 1.9.4.2 ad return true;
395 1.9.4.2 ad }
396 1.9.4.2 ad
397 1.9.4.2 ad bool
398 1.9.4.2 ad pmf_device_recursive_suspend(device_t dv)
399 1.9.4.2 ad {
400 1.9.4.2 ad device_t curdev;
401 1.9.4.2 ad
402 1.9.4.2 ad if (!device_is_active(dv))
403 1.9.4.2 ad return true;
404 1.9.4.2 ad
405 1.9.4.2 ad TAILQ_FOREACH(curdev, &alldevs, dv_list) {
406 1.9.4.2 ad if (device_parent(curdev) != dv)
407 1.9.4.2 ad continue;
408 1.9.4.2 ad if (!pmf_device_recursive_suspend(curdev))
409 1.9.4.2 ad return false;
410 1.9.4.2 ad }
411 1.9.4.2 ad
412 1.9.4.2 ad return pmf_device_suspend(dv);
413 1.9.4.2 ad }
414 1.9.4.2 ad
415 1.9.4.2 ad bool
416 1.9.4.2 ad pmf_device_recursive_resume(device_t dv)
417 1.9.4.2 ad {
418 1.9.4.2 ad device_t parent;
419 1.9.4.2 ad
420 1.9.4.2 ad if (device_is_active(dv))
421 1.9.4.2 ad return true;
422 1.9.4.2 ad
423 1.9.4.2 ad parent = device_parent(dv);
424 1.9.4.2 ad if (parent != NULL) {
425 1.9.4.2 ad if (!pmf_device_recursive_resume(parent))
426 1.9.4.2 ad return false;
427 1.9.4.2 ad }
428 1.9.4.2 ad
429 1.9.4.2 ad return pmf_device_resume(dv);
430 1.9.4.2 ad }
431 1.9.4.2 ad
432 1.9.4.2 ad bool
433 1.9.4.2 ad pmf_device_resume_subtree(device_t dv)
434 1.9.4.2 ad {
435 1.9.4.2 ad device_t curdev;
436 1.9.4.2 ad
437 1.9.4.2 ad if (!pmf_device_recursive_resume(dv))
438 1.9.4.2 ad return false;
439 1.9.4.2 ad
440 1.9.4.2 ad TAILQ_FOREACH(curdev, &alldevs, dv_list) {
441 1.9.4.2 ad if (device_parent(curdev) != dv)
442 1.9.4.2 ad continue;
443 1.9.4.2 ad if (!pmf_device_resume_subtree(curdev))
444 1.9.4.2 ad return false;
445 1.9.4.2 ad }
446 1.9.4.2 ad return true;
447 1.9.4.2 ad }
448 1.9.4.2 ad
449 1.9.4.2 ad #include <net/if.h>
450 1.9.4.2 ad
451 1.9.4.2 ad static bool
452 1.9.4.2 ad pmf_class_network_suspend(device_t dev)
453 1.9.4.2 ad {
454 1.9.4.2 ad struct ifnet *ifp = device_pmf_class_private(dev);
455 1.9.4.2 ad int s;
456 1.9.4.2 ad
457 1.9.4.2 ad s = splnet();
458 1.9.4.2 ad (*ifp->if_stop)(ifp, 1);
459 1.9.4.2 ad splx(s);
460 1.9.4.2 ad
461 1.9.4.2 ad return true;
462 1.9.4.2 ad }
463 1.9.4.2 ad
464 1.9.4.2 ad static bool
465 1.9.4.2 ad pmf_class_network_resume(device_t dev)
466 1.9.4.2 ad {
467 1.9.4.2 ad struct ifnet *ifp = device_pmf_class_private(dev);
468 1.9.4.2 ad int s;
469 1.9.4.2 ad
470 1.9.4.2 ad s = splnet();
471 1.9.4.2 ad if (ifp->if_flags & IFF_UP) {
472 1.9.4.2 ad ifp->if_flags &= ~IFF_RUNNING;
473 1.9.4.2 ad (*ifp->if_init)(ifp);
474 1.9.4.2 ad (*ifp->if_start)(ifp);
475 1.9.4.2 ad }
476 1.9.4.2 ad splx(s);
477 1.9.4.2 ad
478 1.9.4.2 ad return true;
479 1.9.4.2 ad }
480 1.9.4.2 ad
481 1.9.4.2 ad void
482 1.9.4.2 ad pmf_class_network_register(device_t dev, struct ifnet *ifp)
483 1.9.4.2 ad {
484 1.9.4.2 ad device_pmf_class_register(dev, ifp, pmf_class_network_suspend,
485 1.9.4.2 ad pmf_class_network_resume, NULL);
486 1.9.4.2 ad }
487 1.9.4.2 ad
488 1.9.4.2 ad bool
489 1.9.4.2 ad pmf_event_inject(device_t dv, pmf_generic_event_t ev)
490 1.9.4.2 ad {
491 1.9.4.2 ad pmf_event_workitem_t *pew;
492 1.9.4.2 ad
493 1.9.4.2 ad pew = malloc(sizeof(pmf_event_workitem_t), M_TEMP, M_NOWAIT);
494 1.9.4.2 ad if (pew == NULL) {
495 1.9.4.2 ad PMF_EVENT_PRINTF(("%s: PMF event %d dropped (no memory)\n",
496 1.9.4.2 ad dv ? device_xname(dv) : "<anonymous>", ev));
497 1.9.4.2 ad return false;
498 1.9.4.2 ad }
499 1.9.4.2 ad
500 1.9.4.2 ad pew->pew_event = ev;
501 1.9.4.2 ad pew->pew_device = dv;
502 1.9.4.2 ad
503 1.9.4.2 ad workqueue_enqueue(pmf_event_workqueue, (void *)pew, NULL);
504 1.9.4.2 ad PMF_EVENT_PRINTF(("%s: PMF event %d injected\n",
505 1.9.4.2 ad dv ? device_xname(dv) : "<anonymous>", ev));
506 1.9.4.2 ad
507 1.9.4.2 ad return true;
508 1.9.4.2 ad }
509 1.9.4.2 ad
510 1.9.4.2 ad bool
511 1.9.4.2 ad pmf_event_register(device_t dv, pmf_generic_event_t ev,
512 1.9.4.2 ad void (*handler)(device_t), bool global)
513 1.9.4.2 ad {
514 1.9.4.2 ad pmf_event_handler_t *event;
515 1.9.4.2 ad
516 1.9.4.2 ad event = malloc(sizeof(*event), M_DEVBUF, M_WAITOK);
517 1.9.4.2 ad event->pmf_event = ev;
518 1.9.4.2 ad event->pmf_handler = handler;
519 1.9.4.2 ad event->pmf_device = dv;
520 1.9.4.2 ad event->pmf_global = global;
521 1.9.4.2 ad TAILQ_INSERT_TAIL(&pmf_all_events, event, pmf_link);
522 1.9.4.2 ad
523 1.9.4.2 ad return true;
524 1.9.4.2 ad }
525 1.9.4.2 ad
526 1.9.4.2 ad void
527 1.9.4.2 ad pmf_event_deregister(device_t dv, pmf_generic_event_t ev,
528 1.9.4.2 ad void (*handler)(device_t), bool global)
529 1.9.4.2 ad {
530 1.9.4.2 ad pmf_event_handler_t *event;
531 1.9.4.2 ad
532 1.9.4.2 ad TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
533 1.9.4.2 ad if (event->pmf_event != ev)
534 1.9.4.2 ad continue;
535 1.9.4.2 ad if (event->pmf_device != dv)
536 1.9.4.2 ad continue;
537 1.9.4.2 ad if (event->pmf_global != global)
538 1.9.4.2 ad continue;
539 1.9.4.2 ad if (event->pmf_handler != handler)
540 1.9.4.2 ad continue;
541 1.9.4.2 ad TAILQ_REMOVE(&pmf_all_events, event, pmf_link);
542 1.9.4.2 ad free(event, M_WAITOK);
543 1.9.4.2 ad return;
544 1.9.4.2 ad }
545 1.9.4.2 ad }
546 1.9.4.2 ad
547 1.9.4.2 ad struct display_class_softc {
548 1.9.4.2 ad TAILQ_ENTRY(display_class_softc) dc_link;
549 1.9.4.2 ad device_t dc_dev;
550 1.9.4.2 ad };
551 1.9.4.2 ad
552 1.9.4.2 ad static TAILQ_HEAD(, display_class_softc) all_displays;
553 1.9.4.2 ad static callout_t global_idle_counter;
554 1.9.4.2 ad static int idle_timeout = 30;
555 1.9.4.2 ad
556 1.9.4.2 ad static void
557 1.9.4.2 ad input_idle(void *dummy)
558 1.9.4.2 ad {
559 1.9.4.2 ad PMF_IDLE_PRINTF(("Input idle handler called\n"));
560 1.9.4.2 ad pmf_event_inject(NULL, PMFE_DISPLAY_OFF);
561 1.9.4.2 ad }
562 1.9.4.2 ad
563 1.9.4.2 ad static void
564 1.9.4.2 ad input_activity_handler(device_t dv, devactive_t type)
565 1.9.4.2 ad {
566 1.9.4.2 ad if (!TAILQ_EMPTY(&all_displays))
567 1.9.4.2 ad callout_schedule(&global_idle_counter, idle_timeout * hz);
568 1.9.4.2 ad }
569 1.9.4.2 ad
570 1.9.4.2 ad static void
571 1.9.4.2 ad pmf_class_input_deregister(device_t dv)
572 1.9.4.2 ad {
573 1.9.4.2 ad device_active_deregister(dv, input_activity_handler);
574 1.9.4.2 ad }
575 1.9.4.2 ad
576 1.9.4.2 ad bool
577 1.9.4.2 ad pmf_class_input_register(device_t dv)
578 1.9.4.2 ad {
579 1.9.4.2 ad if (!device_active_register(dv, input_activity_handler))
580 1.9.4.2 ad return false;
581 1.9.4.2 ad
582 1.9.4.2 ad device_pmf_class_register(dv, NULL, NULL, NULL,
583 1.9.4.2 ad pmf_class_input_deregister);
584 1.9.4.2 ad
585 1.9.4.2 ad return true;
586 1.9.4.2 ad }
587 1.9.4.2 ad
588 1.9.4.2 ad static void
589 1.9.4.2 ad pmf_class_display_deregister(device_t dv)
590 1.9.4.2 ad {
591 1.9.4.2 ad struct display_class_softc *sc = device_pmf_class_private(dv);
592 1.9.4.2 ad int s;
593 1.9.4.2 ad
594 1.9.4.2 ad s = splsoftclock();
595 1.9.4.2 ad TAILQ_REMOVE(&all_displays, sc, dc_link);
596 1.9.4.2 ad if (TAILQ_EMPTY(&all_displays))
597 1.9.4.2 ad callout_stop(&global_idle_counter);
598 1.9.4.2 ad splx(s);
599 1.9.4.2 ad
600 1.9.4.2 ad free(sc, M_DEVBUF);
601 1.9.4.2 ad }
602 1.9.4.2 ad
603 1.9.4.2 ad bool
604 1.9.4.2 ad pmf_class_display_register(device_t dv)
605 1.9.4.2 ad {
606 1.9.4.2 ad struct display_class_softc *sc;
607 1.9.4.2 ad int s;
608 1.9.4.2 ad
609 1.9.4.2 ad sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK);
610 1.9.4.2 ad
611 1.9.4.2 ad s = splsoftclock();
612 1.9.4.2 ad if (TAILQ_EMPTY(&all_displays))
613 1.9.4.2 ad callout_schedule(&global_idle_counter, idle_timeout * hz);
614 1.9.4.2 ad
615 1.9.4.2 ad TAILQ_INSERT_HEAD(&all_displays, sc, dc_link);
616 1.9.4.2 ad splx(s);
617 1.9.4.2 ad
618 1.9.4.2 ad device_pmf_class_register(dv, sc, NULL, NULL,
619 1.9.4.2 ad pmf_class_display_deregister);
620 1.9.4.2 ad
621 1.9.4.2 ad return true;
622 1.9.4.2 ad }
623 1.9.4.2 ad
624 1.9.4.2 ad void
625 1.9.4.2 ad pmf_init(void)
626 1.9.4.2 ad {
627 1.9.4.2 ad int err;
628 1.9.4.2 ad
629 1.9.4.2 ad KASSERT(pmf_event_workqueue == NULL);
630 1.9.4.2 ad err = workqueue_create(&pmf_event_workqueue, "pmfevent",
631 1.9.4.2 ad pmf_event_worker, NULL, PRI_NONE, IPL_VM, 0);
632 1.9.4.2 ad if (err)
633 1.9.4.2 ad panic("couldn't create pmfevent workqueue");
634 1.9.4.2 ad
635 1.9.4.2 ad callout_init(&global_idle_counter, 0);
636 1.9.4.2 ad callout_setfunc(&global_idle_counter, input_idle, NULL);
637 1.9.4.2 ad }
638