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