Home | History | Annotate | Line # | Download | only in kern
kern_pmf.c revision 1.5
      1  1.5  jmcneill /* $NetBSD: kern_pmf.c,v 1.5 2007/12/11 01:20:06 jmcneill 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.5  jmcneill __KERNEL_RCSID(0, "$NetBSD: kern_pmf.c,v 1.5 2007/12/11 01:20:06 jmcneill 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.2  jmcneill 
     51  1.2  jmcneill #ifdef PMF_DEBUG
     52  1.2  jmcneill int pmf_debug_event;
     53  1.2  jmcneill int pmf_debug_idle;
     54  1.2  jmcneill int pmf_debug_transition;
     55  1.2  jmcneill 
     56  1.2  jmcneill #define	PMF_EVENT_PRINTF(x)		if (pmf_debug_event) printf x
     57  1.2  jmcneill #define	PMF_IDLE_PRINTF(x)		if (pmf_debug_idle) printf x
     58  1.2  jmcneill #define	PMF_TRANSITION_PRINTF(x)	if (pmf_debug_transition) printf x
     59  1.2  jmcneill #define	PMF_TRANSITION_PRINTF2(y,x)	if (pmf_debug_transition>y) printf x
     60  1.2  jmcneill #else
     61  1.2  jmcneill #define	PMF_EVENT_PRINTF(x)		do { } while (0)
     62  1.2  jmcneill #define	PMF_IDLE_PRINTF(x)		do { } while (0)
     63  1.2  jmcneill #define	PMF_TRANSITION_PRINTF(x)	do { } while (0)
     64  1.2  jmcneill #define	PMF_TRANSITION_PRINTF2(y,x)	do { } while (0)
     65  1.2  jmcneill #endif
     66  1.2  jmcneill 
     67  1.2  jmcneill /* #define PMF_DEBUG */
     68  1.2  jmcneill 
     69  1.2  jmcneill MALLOC_DEFINE(M_PMF, "pmf", "device pmf messaging memory");
     70  1.2  jmcneill 
     71  1.2  jmcneill static prop_dictionary_t pmf_platform = NULL;
     72  1.2  jmcneill static struct workqueue *pmf_event_workqueue;
     73  1.2  jmcneill 
     74  1.2  jmcneill typedef struct pmf_event_handler {
     75  1.2  jmcneill 	TAILQ_ENTRY(pmf_event_handler) pmf_link;
     76  1.2  jmcneill 	pmf_generic_event_t pmf_event;
     77  1.2  jmcneill 	void (*pmf_handler)(device_t);
     78  1.2  jmcneill 	device_t pmf_device;
     79  1.2  jmcneill 	bool pmf_global;
     80  1.2  jmcneill } pmf_event_handler_t;
     81  1.2  jmcneill 
     82  1.2  jmcneill static TAILQ_HEAD(, pmf_event_handler) pmf_all_events =
     83  1.2  jmcneill     TAILQ_HEAD_INITIALIZER(pmf_all_events);
     84  1.2  jmcneill 
     85  1.2  jmcneill typedef struct pmf_event_workitem {
     86  1.2  jmcneill 	struct work		pew_work;
     87  1.2  jmcneill 	pmf_generic_event_t	pew_event;
     88  1.2  jmcneill 	device_t		pew_device;
     89  1.2  jmcneill } pmf_event_workitem_t;
     90  1.2  jmcneill 
     91  1.2  jmcneill static void
     92  1.2  jmcneill pmf_event_worker(struct work *wk, void *dummy)
     93  1.2  jmcneill {
     94  1.2  jmcneill 	pmf_event_workitem_t *pew;
     95  1.2  jmcneill 	pmf_event_handler_t *event;
     96  1.2  jmcneill 
     97  1.2  jmcneill 	pew = (void *)wk;
     98  1.2  jmcneill 	KASSERT(wk == &pew->pew_work);
     99  1.2  jmcneill 	KASSERT(pew != NULL);
    100  1.2  jmcneill 
    101  1.2  jmcneill 	TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
    102  1.2  jmcneill 		if (event->pmf_event != pew->pew_event)
    103  1.2  jmcneill 			continue;
    104  1.2  jmcneill 		if (event->pmf_device != pew->pew_device || event->pmf_global)
    105  1.2  jmcneill 			(*event->pmf_handler)(event->pmf_device);
    106  1.2  jmcneill 	}
    107  1.2  jmcneill 
    108  1.4  jmcneill 	free(pew, M_TEMP);
    109  1.4  jmcneill 
    110  1.4  jmcneill 	return;
    111  1.2  jmcneill }
    112  1.2  jmcneill 
    113  1.2  jmcneill static bool
    114  1.2  jmcneill pmf_check_system_drivers(void)
    115  1.2  jmcneill {
    116  1.2  jmcneill 	device_t curdev;
    117  1.2  jmcneill 	bool unsupported_devs;
    118  1.2  jmcneill 
    119  1.2  jmcneill 	unsupported_devs = false;
    120  1.2  jmcneill 	TAILQ_FOREACH(curdev, &alldevs, dv_list) {
    121  1.2  jmcneill 		if (device_pmf_is_registered(curdev))
    122  1.2  jmcneill 			continue;
    123  1.2  jmcneill 		if (!unsupported_devs)
    124  1.2  jmcneill 			printf("Devices without power management support:");
    125  1.2  jmcneill 		printf(" %s", device_xname(curdev));
    126  1.2  jmcneill 		unsupported_devs = true;
    127  1.2  jmcneill 	}
    128  1.2  jmcneill 	if (unsupported_devs) {
    129  1.2  jmcneill 		printf("\n");
    130  1.2  jmcneill 		return false;
    131  1.2  jmcneill 	}
    132  1.2  jmcneill 	return true;
    133  1.2  jmcneill }
    134  1.2  jmcneill 
    135  1.2  jmcneill bool
    136  1.2  jmcneill pmf_system_resume(void)
    137  1.2  jmcneill {
    138  1.2  jmcneill 	int depth, maxdepth;
    139  1.2  jmcneill 	bool rv;
    140  1.2  jmcneill 	device_t curdev, parent;
    141  1.2  jmcneill 
    142  1.2  jmcneill 	if (!pmf_check_system_drivers())
    143  1.2  jmcneill 		return false;
    144  1.2  jmcneill 
    145  1.2  jmcneill 	maxdepth = 0;
    146  1.2  jmcneill 	TAILQ_FOREACH(curdev, &alldevs, dv_list) {
    147  1.2  jmcneill 		if (curdev->dv_depth > maxdepth)
    148  1.2  jmcneill 			maxdepth = curdev->dv_depth;
    149  1.2  jmcneill 	}
    150  1.2  jmcneill 	++maxdepth;
    151  1.2  jmcneill 
    152  1.2  jmcneill 	aprint_debug("Resuming devices:");
    153  1.2  jmcneill 	/* D0 handlers are run in order */
    154  1.2  jmcneill 	depth = 0;
    155  1.2  jmcneill 	rv = true;
    156  1.2  jmcneill 	for (depth = 0; depth < maxdepth; ++depth) {
    157  1.2  jmcneill 		TAILQ_FOREACH(curdev, &alldevs, dv_list) {
    158  1.2  jmcneill 			if (device_is_active(curdev) ||
    159  1.2  jmcneill 			    !device_is_enabled(curdev))
    160  1.2  jmcneill 				continue;
    161  1.2  jmcneill 			if (curdev->dv_depth != depth)
    162  1.2  jmcneill 				continue;
    163  1.2  jmcneill 			parent = device_parent(curdev);
    164  1.2  jmcneill 			if (parent != NULL &&
    165  1.2  jmcneill 			    !device_is_active(parent))
    166  1.2  jmcneill 				continue;
    167  1.2  jmcneill 
    168  1.2  jmcneill 			aprint_debug(" %s", device_xname(curdev));
    169  1.2  jmcneill 
    170  1.2  jmcneill 			if (!pmf_device_resume(curdev)) {
    171  1.2  jmcneill 				rv = false;
    172  1.2  jmcneill 				aprint_debug("(failed)");
    173  1.2  jmcneill 			}
    174  1.2  jmcneill 		}
    175  1.2  jmcneill 	}
    176  1.2  jmcneill 	aprint_debug(".\n");
    177  1.2  jmcneill 
    178  1.2  jmcneill 	return rv;
    179  1.2  jmcneill }
    180  1.2  jmcneill 
    181  1.2  jmcneill bool
    182  1.2  jmcneill pmf_system_suspend(void)
    183  1.2  jmcneill {
    184  1.2  jmcneill 	int depth, maxdepth;
    185  1.2  jmcneill 	device_t curdev;
    186  1.2  jmcneill 
    187  1.2  jmcneill 	if (!pmf_check_system_drivers())
    188  1.2  jmcneill 		return false;
    189  1.2  jmcneill 
    190  1.2  jmcneill 	/*
    191  1.2  jmcneill 	 * Flush buffers only if the shutdown didn't do so
    192  1.2  jmcneill 	 * already and if there was no panic.
    193  1.2  jmcneill 	 */
    194  1.2  jmcneill 	if (doing_shutdown == 0 && panicstr == NULL) {
    195  1.2  jmcneill 		printf("Flushing disk caches: ");
    196  1.2  jmcneill 		sys_sync(NULL, NULL, NULL);
    197  1.2  jmcneill 		if (buf_syncwait() != 0)
    198  1.2  jmcneill 			printf("giving up\n");
    199  1.2  jmcneill 		else
    200  1.2  jmcneill 			printf("done\n");
    201  1.2  jmcneill 	}
    202  1.2  jmcneill 
    203  1.2  jmcneill 	aprint_debug("Suspending devices:");
    204  1.2  jmcneill 
    205  1.2  jmcneill 	maxdepth = 0;
    206  1.2  jmcneill 	TAILQ_FOREACH(curdev, &alldevs, dv_list) {
    207  1.2  jmcneill 		if (curdev->dv_depth > maxdepth)
    208  1.2  jmcneill 			maxdepth = curdev->dv_depth;
    209  1.2  jmcneill 	}
    210  1.2  jmcneill 
    211  1.2  jmcneill 	for (depth = maxdepth; depth >= 0; --depth) {
    212  1.2  jmcneill 		TAILQ_FOREACH_REVERSE(curdev, &alldevs, devicelist, dv_list) {
    213  1.2  jmcneill 			if (curdev->dv_depth != depth)
    214  1.2  jmcneill 				continue;
    215  1.2  jmcneill 			if (!device_is_active(curdev))
    216  1.2  jmcneill 				continue;
    217  1.2  jmcneill 
    218  1.2  jmcneill 			aprint_debug(" %s", device_xname(curdev));
    219  1.2  jmcneill 
    220  1.2  jmcneill 			/* XXX joerg check return value and abort suspend */
    221  1.2  jmcneill 			if (!pmf_device_suspend(curdev))
    222  1.2  jmcneill 				aprint_debug("(failed)");
    223  1.2  jmcneill 		}
    224  1.2  jmcneill 	}
    225  1.2  jmcneill 
    226  1.2  jmcneill 	aprint_debug(".\n");
    227  1.2  jmcneill 
    228  1.2  jmcneill 	return true;
    229  1.2  jmcneill }
    230  1.2  jmcneill 
    231  1.2  jmcneill void
    232  1.2  jmcneill pmf_system_shutdown(void)
    233  1.2  jmcneill {
    234  1.2  jmcneill 	int depth, maxdepth;
    235  1.2  jmcneill 	device_t curdev;
    236  1.2  jmcneill 
    237  1.2  jmcneill 	if (!pmf_check_system_drivers())
    238  1.2  jmcneill 		delay(2000000);
    239  1.2  jmcneill 
    240  1.2  jmcneill 	aprint_debug("Shutting down devices:");
    241  1.2  jmcneill 
    242  1.2  jmcneill 	maxdepth = 0;
    243  1.2  jmcneill 	TAILQ_FOREACH(curdev, &alldevs, dv_list) {
    244  1.2  jmcneill 		if (curdev->dv_depth > maxdepth)
    245  1.2  jmcneill 			maxdepth = curdev->dv_depth;
    246  1.2  jmcneill 	}
    247  1.2  jmcneill 
    248  1.2  jmcneill 	for (depth = maxdepth; depth >= 0; --depth) {
    249  1.2  jmcneill 		TAILQ_FOREACH_REVERSE(curdev, &alldevs, devicelist, dv_list) {
    250  1.2  jmcneill 			if (curdev->dv_depth != depth)
    251  1.2  jmcneill 				continue;
    252  1.2  jmcneill 			if (!device_is_active(curdev))
    253  1.2  jmcneill 				continue;
    254  1.2  jmcneill 
    255  1.2  jmcneill 			aprint_debug(" %s", device_xname(curdev));
    256  1.2  jmcneill 
    257  1.2  jmcneill 			if (!device_pmf_is_registered(curdev))
    258  1.2  jmcneill 				continue;
    259  1.2  jmcneill 			if (!device_pmf_class_suspend(curdev)) {
    260  1.2  jmcneill 				aprint_debug("(failed)");
    261  1.2  jmcneill 				continue;
    262  1.2  jmcneill 			}
    263  1.2  jmcneill 			if (!device_pmf_driver_suspend(curdev)) {
    264  1.2  jmcneill 				aprint_debug("(failed)");
    265  1.2  jmcneill 				continue;
    266  1.2  jmcneill 			}
    267  1.2  jmcneill 		}
    268  1.2  jmcneill 	}
    269  1.2  jmcneill 
    270  1.2  jmcneill 	aprint_debug(".\n");
    271  1.2  jmcneill }
    272  1.2  jmcneill 
    273  1.2  jmcneill bool
    274  1.2  jmcneill pmf_set_platform(const char *key, const char *value)
    275  1.2  jmcneill {
    276  1.2  jmcneill 	if (pmf_platform == NULL)
    277  1.2  jmcneill 		pmf_platform = prop_dictionary_create();
    278  1.2  jmcneill 	if (pmf_platform == NULL)
    279  1.2  jmcneill 		return false;
    280  1.2  jmcneill 
    281  1.2  jmcneill 	return prop_dictionary_set_cstring(pmf_platform, key, value);
    282  1.2  jmcneill }
    283  1.2  jmcneill 
    284  1.2  jmcneill const char *
    285  1.2  jmcneill pmf_get_platform(const char *key)
    286  1.2  jmcneill {
    287  1.2  jmcneill 	const char *value;
    288  1.2  jmcneill 
    289  1.2  jmcneill 	if (pmf_platform == NULL)
    290  1.2  jmcneill 		return NULL;
    291  1.2  jmcneill 
    292  1.2  jmcneill 	if (!prop_dictionary_get_cstring_nocopy(pmf_platform, key, &value))
    293  1.2  jmcneill 		return NULL;
    294  1.2  jmcneill 
    295  1.2  jmcneill 	return value;
    296  1.2  jmcneill }
    297  1.2  jmcneill 
    298  1.2  jmcneill bool
    299  1.2  jmcneill pmf_device_register(device_t dev,
    300  1.2  jmcneill     bool (*suspend)(device_t), bool (*resume)(device_t))
    301  1.2  jmcneill {
    302  1.2  jmcneill 	device_pmf_driver_register(dev, suspend, resume);
    303  1.2  jmcneill 
    304  1.2  jmcneill 	if (!device_pmf_driver_child_register(dev)) {
    305  1.2  jmcneill 		device_pmf_driver_deregister(dev);
    306  1.2  jmcneill 		return false;
    307  1.2  jmcneill 	}
    308  1.2  jmcneill 
    309  1.2  jmcneill 	return true;
    310  1.2  jmcneill }
    311  1.2  jmcneill 
    312  1.2  jmcneill void
    313  1.2  jmcneill pmf_device_deregister(device_t dev)
    314  1.2  jmcneill {
    315  1.2  jmcneill 	device_pmf_class_deregister(dev);
    316  1.2  jmcneill 	device_pmf_bus_deregister(dev);
    317  1.2  jmcneill 	device_pmf_driver_deregister(dev);
    318  1.2  jmcneill }
    319  1.2  jmcneill 
    320  1.2  jmcneill bool
    321  1.2  jmcneill pmf_device_suspend(device_t dev)
    322  1.2  jmcneill {
    323  1.2  jmcneill 	PMF_TRANSITION_PRINTF(("%s: suspend enter\n", device_xname(dev)));
    324  1.2  jmcneill 	if (!device_pmf_is_registered(dev))
    325  1.2  jmcneill 		return false;
    326  1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: class suspend\n", device_xname(dev)));
    327  1.2  jmcneill 	if (!device_pmf_class_suspend(dev))
    328  1.2  jmcneill 		return false;
    329  1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: driver suspend\n", device_xname(dev)));
    330  1.2  jmcneill 	if (!device_pmf_driver_suspend(dev))
    331  1.2  jmcneill 		return false;
    332  1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: bus suspend\n", device_xname(dev)));
    333  1.2  jmcneill 	if (!device_pmf_bus_suspend(dev))
    334  1.2  jmcneill 		return false;
    335  1.2  jmcneill 	PMF_TRANSITION_PRINTF(("%s: suspend exit\n", device_xname(dev)));
    336  1.2  jmcneill 	return true;
    337  1.2  jmcneill }
    338  1.2  jmcneill 
    339  1.2  jmcneill bool
    340  1.2  jmcneill pmf_device_resume(device_t dev)
    341  1.2  jmcneill {
    342  1.2  jmcneill 	PMF_TRANSITION_PRINTF(("%s: resume enter\n", device_xname(dev)));
    343  1.2  jmcneill 	if (!device_pmf_is_registered(dev))
    344  1.2  jmcneill 		return false;
    345  1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: bus resume\n", device_xname(dev)));
    346  1.2  jmcneill 	if (!device_pmf_bus_resume(dev))
    347  1.2  jmcneill 		return false;
    348  1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: driver resume\n", device_xname(dev)));
    349  1.2  jmcneill 	if (!device_pmf_driver_resume(dev))
    350  1.2  jmcneill 		return false;
    351  1.2  jmcneill 	PMF_TRANSITION_PRINTF2(1, ("%s: class resume\n", device_xname(dev)));
    352  1.2  jmcneill 	if (!device_pmf_class_resume(dev))
    353  1.2  jmcneill 		return false;
    354  1.2  jmcneill 	PMF_TRANSITION_PRINTF(("%s: resume exit\n", device_xname(dev)));
    355  1.2  jmcneill 	return true;
    356  1.2  jmcneill }
    357  1.2  jmcneill 
    358  1.2  jmcneill bool
    359  1.2  jmcneill pmf_device_recursive_suspend(device_t dv)
    360  1.2  jmcneill {
    361  1.2  jmcneill 	device_t curdev;
    362  1.2  jmcneill 
    363  1.2  jmcneill 	if (!device_is_active(dv))
    364  1.2  jmcneill 		return true;
    365  1.2  jmcneill 
    366  1.2  jmcneill 	TAILQ_FOREACH(curdev, &alldevs, dv_list) {
    367  1.2  jmcneill 		if (device_parent(curdev) != dv)
    368  1.2  jmcneill 			continue;
    369  1.2  jmcneill 		if (!pmf_device_recursive_suspend(curdev))
    370  1.2  jmcneill 			return false;
    371  1.2  jmcneill 	}
    372  1.2  jmcneill 
    373  1.2  jmcneill 	return pmf_device_suspend(dv);
    374  1.2  jmcneill }
    375  1.2  jmcneill 
    376  1.2  jmcneill bool
    377  1.2  jmcneill pmf_device_recursive_resume(device_t dv)
    378  1.2  jmcneill {
    379  1.2  jmcneill 	device_t parent;
    380  1.2  jmcneill 
    381  1.2  jmcneill 	if (device_is_active(dv))
    382  1.2  jmcneill 		return true;
    383  1.2  jmcneill 
    384  1.2  jmcneill 	parent = device_parent(dv);
    385  1.2  jmcneill 	if (parent != NULL) {
    386  1.2  jmcneill 		if (!pmf_device_recursive_resume(parent))
    387  1.2  jmcneill 			return false;
    388  1.2  jmcneill 	}
    389  1.2  jmcneill 
    390  1.2  jmcneill 	return pmf_device_resume(dv);
    391  1.2  jmcneill }
    392  1.2  jmcneill 
    393  1.2  jmcneill bool
    394  1.2  jmcneill pmf_device_resume_subtree(device_t dv)
    395  1.2  jmcneill {
    396  1.2  jmcneill 	device_t curdev;
    397  1.2  jmcneill 
    398  1.2  jmcneill 	if (!pmf_device_recursive_resume(dv))
    399  1.2  jmcneill 		return false;
    400  1.2  jmcneill 
    401  1.2  jmcneill 	TAILQ_FOREACH(curdev, &alldevs, dv_list) {
    402  1.2  jmcneill 		if (device_parent(curdev) != dv)
    403  1.2  jmcneill 			continue;
    404  1.2  jmcneill 		if (!pmf_device_resume_subtree(curdev))
    405  1.2  jmcneill 			return false;
    406  1.2  jmcneill 	}
    407  1.2  jmcneill 	return true;
    408  1.2  jmcneill }
    409  1.2  jmcneill 
    410  1.2  jmcneill #include <net/if.h>
    411  1.2  jmcneill 
    412  1.2  jmcneill static bool
    413  1.2  jmcneill pmf_class_network_suspend(device_t dev)
    414  1.2  jmcneill {
    415  1.2  jmcneill 	struct ifnet *ifp = device_pmf_class_private(dev);
    416  1.2  jmcneill 	int s;
    417  1.2  jmcneill 
    418  1.2  jmcneill 	s = splnet();
    419  1.2  jmcneill 	(*ifp->if_stop)(ifp, 1);
    420  1.2  jmcneill 	splx(s);
    421  1.2  jmcneill 
    422  1.2  jmcneill 	return true;
    423  1.2  jmcneill }
    424  1.2  jmcneill 
    425  1.2  jmcneill static bool
    426  1.2  jmcneill pmf_class_network_resume(device_t dev)
    427  1.2  jmcneill {
    428  1.2  jmcneill 	struct ifnet *ifp = device_pmf_class_private(dev);
    429  1.2  jmcneill 	int s;
    430  1.2  jmcneill 
    431  1.2  jmcneill 	s = splnet();
    432  1.2  jmcneill 	if (ifp->if_flags & IFF_UP) {
    433  1.2  jmcneill 		ifp->if_flags &= ~IFF_RUNNING;
    434  1.2  jmcneill 		(*ifp->if_init)(ifp);
    435  1.2  jmcneill 		(*ifp->if_start)(ifp);
    436  1.2  jmcneill 	}
    437  1.2  jmcneill 	splx(s);
    438  1.2  jmcneill 
    439  1.2  jmcneill 	return true;
    440  1.2  jmcneill }
    441  1.2  jmcneill 
    442  1.2  jmcneill void
    443  1.2  jmcneill pmf_class_network_register(device_t dev, struct ifnet *ifp)
    444  1.2  jmcneill {
    445  1.2  jmcneill 	device_pmf_class_register(dev, ifp, pmf_class_network_suspend,
    446  1.2  jmcneill 	    pmf_class_network_resume, NULL);
    447  1.2  jmcneill }
    448  1.2  jmcneill 
    449  1.2  jmcneill bool
    450  1.2  jmcneill pmf_event_inject(device_t dv, pmf_generic_event_t ev)
    451  1.2  jmcneill {
    452  1.2  jmcneill 	pmf_event_workitem_t *pew;
    453  1.2  jmcneill 
    454  1.4  jmcneill 	pew = malloc(sizeof(pmf_event_workitem_t), M_TEMP, M_NOWAIT);
    455  1.2  jmcneill 	if (pew == NULL) {
    456  1.2  jmcneill 		PMF_EVENT_PRINTF(("%s: PMF event %d dropped (no memory)\n",
    457  1.2  jmcneill 		    dv ? device_xname(dv) : "<anonymous>", ev));
    458  1.2  jmcneill 		return false;
    459  1.2  jmcneill 	}
    460  1.2  jmcneill 
    461  1.2  jmcneill 	pew->pew_event = ev;
    462  1.2  jmcneill 	pew->pew_device = dv;
    463  1.2  jmcneill 
    464  1.2  jmcneill 	workqueue_enqueue(pmf_event_workqueue, (void *)pew, NULL);
    465  1.2  jmcneill 	PMF_EVENT_PRINTF(("%s: PMF event %d injected\n",
    466  1.2  jmcneill 	    dv ? device_xname(dv) : "<anonymous>", ev));
    467  1.2  jmcneill 
    468  1.2  jmcneill 	return true;
    469  1.2  jmcneill }
    470  1.2  jmcneill 
    471  1.2  jmcneill bool
    472  1.2  jmcneill pmf_event_register(device_t dv, pmf_generic_event_t ev,
    473  1.2  jmcneill     void (*handler)(device_t), bool global)
    474  1.2  jmcneill {
    475  1.2  jmcneill 	pmf_event_handler_t *event;
    476  1.2  jmcneill 
    477  1.2  jmcneill 	event = malloc(sizeof(*event), M_DEVBUF, M_WAITOK);
    478  1.2  jmcneill 	event->pmf_event = ev;
    479  1.2  jmcneill 	event->pmf_handler = handler;
    480  1.2  jmcneill 	event->pmf_device = dv;
    481  1.2  jmcneill 	event->pmf_global = global;
    482  1.2  jmcneill 	TAILQ_INSERT_TAIL(&pmf_all_events, event, pmf_link);
    483  1.2  jmcneill 
    484  1.2  jmcneill 	return true;
    485  1.2  jmcneill }
    486  1.2  jmcneill 
    487  1.2  jmcneill void
    488  1.2  jmcneill pmf_event_deregister(device_t dv, pmf_generic_event_t ev,
    489  1.2  jmcneill     void (*handler)(device_t), bool global)
    490  1.2  jmcneill {
    491  1.2  jmcneill 	pmf_event_handler_t *event;
    492  1.2  jmcneill 
    493  1.2  jmcneill 	TAILQ_FOREACH(event, &pmf_all_events, pmf_link) {
    494  1.2  jmcneill 		if (event->pmf_event != ev)
    495  1.2  jmcneill 			continue;
    496  1.2  jmcneill 		if (event->pmf_device != dv)
    497  1.2  jmcneill 			continue;
    498  1.2  jmcneill 		if (event->pmf_global != global)
    499  1.2  jmcneill 			continue;
    500  1.2  jmcneill 		if (event->pmf_handler != handler)
    501  1.2  jmcneill 			continue;
    502  1.2  jmcneill 		TAILQ_REMOVE(&pmf_all_events, event, pmf_link);
    503  1.2  jmcneill 		free(event, M_WAITOK);
    504  1.2  jmcneill 	}
    505  1.2  jmcneill }
    506  1.2  jmcneill 
    507  1.2  jmcneill struct display_class_softc {
    508  1.2  jmcneill 	TAILQ_ENTRY(display_class_softc) dc_link;
    509  1.2  jmcneill 	device_t dc_dev;
    510  1.2  jmcneill };
    511  1.2  jmcneill 
    512  1.2  jmcneill static TAILQ_HEAD(, display_class_softc) all_displays;
    513  1.2  jmcneill static callout_t global_idle_counter;
    514  1.2  jmcneill static int idle_timeout = 30;
    515  1.2  jmcneill 
    516  1.2  jmcneill static void
    517  1.2  jmcneill input_idle(void *dummy)
    518  1.2  jmcneill {
    519  1.2  jmcneill 	PMF_IDLE_PRINTF(("Input idle handler called\n"));
    520  1.2  jmcneill 	pmf_event_inject(NULL, PMFE_DISPLAY_OFF);
    521  1.2  jmcneill }
    522  1.2  jmcneill 
    523  1.2  jmcneill static void
    524  1.2  jmcneill input_activity_handler(device_t dv, devactive_t type)
    525  1.2  jmcneill {
    526  1.2  jmcneill 	if (!TAILQ_EMPTY(&all_displays))
    527  1.2  jmcneill 		callout_schedule(&global_idle_counter, idle_timeout * hz);
    528  1.2  jmcneill }
    529  1.2  jmcneill 
    530  1.2  jmcneill static void
    531  1.2  jmcneill pmf_class_input_deregister(device_t dv)
    532  1.2  jmcneill {
    533  1.2  jmcneill 	device_active_deregister(dv, input_activity_handler);
    534  1.2  jmcneill }
    535  1.2  jmcneill 
    536  1.2  jmcneill bool
    537  1.2  jmcneill pmf_class_input_register(device_t dv)
    538  1.2  jmcneill {
    539  1.2  jmcneill 	if (!device_active_register(dv, input_activity_handler))
    540  1.2  jmcneill 		return false;
    541  1.2  jmcneill 
    542  1.2  jmcneill 	device_pmf_class_register(dv, NULL, NULL, NULL,
    543  1.2  jmcneill 	    pmf_class_input_deregister);
    544  1.2  jmcneill 
    545  1.2  jmcneill 	return true;
    546  1.2  jmcneill }
    547  1.2  jmcneill 
    548  1.2  jmcneill static void
    549  1.2  jmcneill pmf_class_display_deregister(device_t dv)
    550  1.2  jmcneill {
    551  1.2  jmcneill 	struct display_class_softc *sc = device_pmf_class_private(dv);
    552  1.2  jmcneill 	int s;
    553  1.2  jmcneill 
    554  1.2  jmcneill 	s = splsoftclock();
    555  1.2  jmcneill 	TAILQ_REMOVE(&all_displays, sc, dc_link);
    556  1.2  jmcneill 	if (TAILQ_EMPTY(&all_displays))
    557  1.2  jmcneill 		callout_stop(&global_idle_counter);
    558  1.2  jmcneill 	splx(s);
    559  1.2  jmcneill 
    560  1.2  jmcneill 	free(sc, M_DEVBUF);
    561  1.2  jmcneill }
    562  1.2  jmcneill 
    563  1.2  jmcneill bool
    564  1.2  jmcneill pmf_class_display_register(device_t dv)
    565  1.2  jmcneill {
    566  1.2  jmcneill 	struct display_class_softc *sc;
    567  1.2  jmcneill 	int s;
    568  1.2  jmcneill 
    569  1.2  jmcneill 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK);
    570  1.2  jmcneill 
    571  1.2  jmcneill 	s = splsoftclock();
    572  1.2  jmcneill 	if (TAILQ_EMPTY(&all_displays))
    573  1.2  jmcneill 		callout_schedule(&global_idle_counter, idle_timeout * hz);
    574  1.2  jmcneill 
    575  1.2  jmcneill 	TAILQ_INSERT_HEAD(&all_displays, sc, dc_link);
    576  1.2  jmcneill 	splx(s);
    577  1.2  jmcneill 
    578  1.2  jmcneill 	device_pmf_class_register(dv, sc, NULL, NULL,
    579  1.2  jmcneill 	    pmf_class_display_deregister);
    580  1.2  jmcneill 
    581  1.2  jmcneill 	return true;
    582  1.2  jmcneill }
    583  1.2  jmcneill 
    584  1.2  jmcneill void
    585  1.2  jmcneill pmf_init(void)
    586  1.2  jmcneill {
    587  1.2  jmcneill 	int err;
    588  1.2  jmcneill 
    589  1.2  jmcneill 	KASSERT(pmf_event_workqueue == NULL);
    590  1.2  jmcneill 	err = workqueue_create(&pmf_event_workqueue, "pmfevent",
    591  1.5  jmcneill 	    pmf_event_worker, NULL, PRI_NONE, IPL_VM, 0);
    592  1.2  jmcneill 	if (err)
    593  1.2  jmcneill 		panic("couldn't create pmfevent workqueue");
    594  1.2  jmcneill 
    595  1.2  jmcneill 	callout_init(&global_idle_counter, 0);
    596  1.2  jmcneill 	callout_setfunc(&global_idle_counter, input_idle, NULL);
    597  1.2  jmcneill }
    598