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