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