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