Home | History | Annotate | Line # | Download | only in acpi
acpi_wakedev.c revision 1.9
      1 /* $NetBSD: acpi_wakedev.c,v 1.9 2010/04/12 12:14:26 jruoho Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: acpi_wakedev.c,v 1.9 2010/04/12 12:14:26 jruoho Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/device.h>
     34 #include <sys/sysctl.h>
     35 #include <sys/systm.h>
     36 
     37 #include <dev/acpi/acpireg.h>
     38 #include <dev/acpi/acpivar.h>
     39 #include <dev/acpi/acpi_wakedev.h>
     40 
     41 #define _COMPONENT		   ACPI_BUS_COMPONENT
     42 ACPI_MODULE_NAME		   ("acpi_wakedev")
     43 
     44 static const char * const acpi_wakedev_default[] = {
     45 	"PNP0C0C",	/* power button */
     46 	"PNP0C0E",	/* sleep button */
     47 	"PNP0C0D",	/* lid switch */
     48 	"PNP03??",	/* PC KBD port */
     49 	NULL,
     50 };
     51 
     52 static const struct sysctlnode *rnode = NULL;
     53 
     54 static void	acpi_wakedev_prepare(struct acpi_devnode *, int, int);
     55 
     56 SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.acpi.wake subtree setup")
     57 {
     58 	int err;
     59 
     60 	err = sysctl_createv(NULL, 0, NULL, &rnode,
     61 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
     62 	    NULL, NULL, 0, NULL, 0,
     63 	    CTL_HW, CTL_EOL);
     64 
     65 	if (err != 0)
     66 		goto fail;
     67 
     68 	err = sysctl_createv(NULL, 0, &rnode, &rnode,
     69 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi",
     70 	    NULL, NULL, 0, NULL, 0,
     71 	    CTL_CREATE, CTL_EOL);
     72 
     73 	if (err != 0)
     74 		goto fail;
     75 
     76 	err = sysctl_createv(NULL, 0, &rnode, &rnode,
     77 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
     78 	    "wake", SYSCTL_DESCR("ACPI device wake-up"),
     79 	    NULL, 0, NULL, 0,
     80 	    CTL_CREATE, CTL_EOL);
     81 
     82 	if (err != 0)
     83 		goto fail;
     84 
     85 	return;
     86 
     87 fail:
     88 	rnode = NULL;
     89 }
     90 
     91 void
     92 acpi_wakedev_add(struct acpi_devnode *ad)
     93 {
     94 	int err;
     95 
     96 	KASSERT(ad != NULL && ad->ad_parent != NULL);
     97 	KASSERT((ad->ad_flags & ACPI_DEVICE_WAKEUP) != 0);
     98 
     99 	ad->ad_wake = 0;
    100 
    101 	if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default))
    102 		ad->ad_wake = 1;
    103 
    104 	if (rnode == NULL)
    105 		return;
    106 
    107 	err = sysctl_createv(NULL, 0, &rnode, NULL,
    108 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, ad->ad_name,
    109 	    NULL, NULL, 0, &ad->ad_wake, 0,
    110 	    CTL_CREATE, CTL_EOL);
    111 
    112 	if (err != 0)
    113 		aprint_error_dev(ad->ad_parent, "sysctl_createv"
    114 		    "(hw.acpi.wake.%s) failed (err %d)\n", ad->ad_name, err);
    115 }
    116 
    117 void
    118 acpi_wakedev_commit(struct acpi_softc *sc, int state)
    119 {
    120 	struct acpi_devnode *ad;
    121 
    122 	/*
    123 	 * As noted in ACPI 3.0 (p. 243), preparing
    124 	 * a device for wakeup is a two-step process:
    125 	 *
    126 	 *  1.	Enable all power resources in _PRW.
    127 	 *
    128 	 *  2.	If present, execute _DSW/_PSW method.
    129 	 *
    130 	 * XXX: The first one is yet to be implemented.
    131 	 */
    132 	SIMPLEQ_FOREACH(ad, &sc->sc_devnodes, ad_list) {
    133 
    134 		if ((ad->ad_flags & ACPI_DEVICE_WAKEUP) == 0)
    135 			continue;
    136 
    137 		if (ad->ad_wake == 0)
    138 			acpi_clear_wake_gpe(ad->ad_handle);
    139 		else {
    140 			aprint_debug_dev(ad->ad_parent,
    141 			    "set wake GPE for %s\n", ad->ad_name);
    142 			acpi_set_wake_gpe(ad->ad_handle);
    143 		}
    144 
    145 		acpi_wakedev_prepare(ad, ad->ad_wake, state);
    146 	}
    147 }
    148 
    149 static void
    150 acpi_wakedev_prepare(struct acpi_devnode *ad, int enable, int state)
    151 {
    152 	ACPI_OBJECT_LIST arg;
    153 	ACPI_OBJECT obj[3];
    154 	ACPI_STATUS rv;
    155 
    156 	/*
    157 	 * First try to call the Device Sleep Wake control method, _DSW.
    158 	 * Only if this is not available, resort to to the Power State
    159 	 * Wake control method, _PSW, which was deprecated in ACPI 3.0.
    160 	 *
    161 	 * The arguments to these methods are as follows:
    162 	 *
    163 	 *		arg0		arg1		arg2
    164 	 *		----		----		----
    165 	 *	 _PSW	0: disable
    166 	 *		1: enable
    167 	 *
    168 	 *	 _DSW	0: disable	0: S0		0: D0
    169 	 *		1: enable	1: S1		1: D0 or D1
    170 	 *						2: D0, D1, or D2
    171 	 *				x: Sx		3: D0, D1, D2 or D3
    172 	 */
    173 	arg.Count = 3;
    174 	arg.Pointer = obj;
    175 
    176 	obj[0].Integer.Value = enable;
    177 	obj[1].Integer.Value = state;
    178 	obj[2].Integer.Value = 3;
    179 
    180 	obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
    181 
    182 	rv = AcpiEvaluateObject(ad->ad_handle, "_DSW", &arg, NULL);
    183 
    184 	if (ACPI_SUCCESS(rv))
    185 		return;
    186 
    187 	if (rv != AE_NOT_FOUND)
    188 		goto fail;
    189 
    190 	rv = acpi_eval_set_integer(ad->ad_handle, "_PSW", enable);
    191 
    192 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
    193 		goto fail;
    194 
    195 	return;
    196 
    197 fail:
    198 	aprint_error_dev(ad->ad_parent, "failed to evaluate wake "
    199 	    "control method: %s\n", AcpiFormatException(rv));
    200 }
    201