Home | History | Annotate | Line # | Download | only in acpi
acpi_wakedev.c revision 1.6.2.2
      1  1.6.2.2     rmind /* $NetBSD: acpi_wakedev.c,v 1.6.2.2 2010/07/03 01:19:34 rmind Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4  1.6.2.1     rmind  * Copyright (c) 2009, 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmcneill  * modification, are permitted provided that the following conditions
      9      1.1  jmcneill  * are met:
     10      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmcneill  *
     16      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill #include <sys/cdefs.h>
     30  1.6.2.2     rmind __KERNEL_RCSID(0, "$NetBSD: acpi_wakedev.c,v 1.6.2.2 2010/07/03 01:19:34 rmind Exp $");
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/param.h>
     33      1.3    jruoho #include <sys/device.h>
     34      1.1  jmcneill #include <sys/sysctl.h>
     35      1.3    jruoho #include <sys/systm.h>
     36      1.1  jmcneill 
     37      1.3    jruoho #include <dev/acpi/acpireg.h>
     38      1.1  jmcneill #include <dev/acpi/acpivar.h>
     39  1.6.2.2     rmind #include <dev/acpi/acpi_power.h>
     40      1.1  jmcneill #include <dev/acpi/acpi_wakedev.h>
     41      1.1  jmcneill 
     42  1.6.2.2     rmind #define _COMPONENT		ACPI_BUS_COMPONENT
     43  1.6.2.2     rmind ACPI_MODULE_NAME		("acpi_wakedev")
     44      1.5    jruoho 
     45      1.1  jmcneill static const char * const acpi_wakedev_default[] = {
     46      1.1  jmcneill 	"PNP0C0C",	/* power button */
     47      1.1  jmcneill 	"PNP0C0E",	/* sleep button */
     48      1.1  jmcneill 	"PNP0C0D",	/* lid switch */
     49      1.1  jmcneill 	"PNP03??",	/* PC KBD port */
     50      1.1  jmcneill 	NULL,
     51      1.1  jmcneill };
     52      1.1  jmcneill 
     53  1.6.2.1     rmind static const struct sysctlnode *rnode = NULL;
     54  1.6.2.1     rmind 
     55  1.6.2.2     rmind static void	acpi_wakedev_method(struct acpi_devnode *, int, int);
     56  1.6.2.2     rmind static void	acpi_wakedev_gpe(struct acpi_devnode *, int, int);
     57  1.6.2.2     rmind static void	acpi_wakedev_power(struct acpi_devnode *, ACPI_OBJECT *);
     58      1.5    jruoho 
     59  1.6.2.1     rmind SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.acpi.wake subtree setup")
     60      1.1  jmcneill {
     61      1.1  jmcneill 	int err;
     62      1.1  jmcneill 
     63      1.1  jmcneill 	err = sysctl_createv(NULL, 0, NULL, &rnode,
     64  1.6.2.1     rmind 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
     65  1.6.2.1     rmind 	    NULL, NULL, 0, NULL, 0,
     66  1.6.2.1     rmind 	    CTL_HW, CTL_EOL);
     67      1.1  jmcneill 
     68  1.6.2.1     rmind 	if (err != 0)
     69  1.6.2.1     rmind 		goto fail;
     70      1.1  jmcneill 
     71  1.6.2.1     rmind 	err = sysctl_createv(NULL, 0, &rnode, &rnode,
     72  1.6.2.1     rmind 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi",
     73  1.6.2.1     rmind 	    NULL, NULL, 0, NULL, 0,
     74  1.6.2.1     rmind 	    CTL_CREATE, CTL_EOL);
     75      1.1  jmcneill 
     76  1.6.2.1     rmind 	if (err != 0)
     77  1.6.2.1     rmind 		goto fail;
     78      1.1  jmcneill 
     79  1.6.2.1     rmind 	err = sysctl_createv(NULL, 0, &rnode, &rnode,
     80  1.6.2.1     rmind 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
     81  1.6.2.1     rmind 	    "wake", SYSCTL_DESCR("ACPI device wake-up"),
     82  1.6.2.1     rmind 	    NULL, 0, NULL, 0,
     83  1.6.2.1     rmind 	    CTL_CREATE, CTL_EOL);
     84      1.1  jmcneill 
     85  1.6.2.1     rmind 	if (err != 0)
     86  1.6.2.1     rmind 		goto fail;
     87      1.1  jmcneill 
     88  1.6.2.1     rmind 	return;
     89      1.1  jmcneill 
     90  1.6.2.1     rmind fail:
     91  1.6.2.1     rmind 	rnode = NULL;
     92      1.1  jmcneill }
     93      1.1  jmcneill 
     94  1.6.2.1     rmind void
     95  1.6.2.1     rmind acpi_wakedev_add(struct acpi_devnode *ad)
     96      1.1  jmcneill {
     97  1.6.2.1     rmind 	int err;
     98      1.6    jruoho 
     99  1.6.2.1     rmind 	KASSERT(ad != NULL && ad->ad_root != NULL);
    100  1.6.2.1     rmind 	KASSERT((ad->ad_flags & ACPI_DEVICE_WAKEUP) != 0);
    101      1.6    jruoho 
    102  1.6.2.1     rmind 	ad->ad_wake = 0;
    103      1.1  jmcneill 
    104  1.6.2.1     rmind 	if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default))
    105  1.6.2.1     rmind 		ad->ad_wake = 1;
    106      1.1  jmcneill 
    107  1.6.2.1     rmind 	if (rnode == NULL)
    108  1.6.2.1     rmind 		return;
    109      1.1  jmcneill 
    110  1.6.2.1     rmind 	err = sysctl_createv(NULL, 0, &rnode, NULL,
    111  1.6.2.1     rmind 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, ad->ad_name,
    112  1.6.2.1     rmind 	    NULL, NULL, 0, &ad->ad_wake, 0,
    113  1.6.2.1     rmind 	    CTL_CREATE, CTL_EOL);
    114  1.6.2.1     rmind 
    115  1.6.2.1     rmind 	if (err != 0)
    116  1.6.2.1     rmind 		aprint_error_dev(ad->ad_root, "sysctl_createv"
    117  1.6.2.1     rmind 		    "(hw.acpi.wake.%s) failed (err %d)\n", ad->ad_name, err);
    118      1.1  jmcneill }
    119      1.1  jmcneill 
    120      1.1  jmcneill void
    121      1.4    jruoho acpi_wakedev_commit(struct acpi_softc *sc, int state)
    122      1.1  jmcneill {
    123  1.6.2.1     rmind 	struct acpi_devnode *ad;
    124      1.1  jmcneill 
    125      1.4    jruoho 	/*
    126  1.6.2.2     rmind 	 * To prepare a device for wakeup:
    127      1.4    jruoho 	 *
    128  1.6.2.2     rmind 	 *  1.	Set appropriate GPEs.
    129      1.4    jruoho 	 *
    130  1.6.2.2     rmind 	 *  2.	Enable all power resources in _PRW.
    131      1.4    jruoho 	 *
    132  1.6.2.2     rmind 	 *  3.	If present, execute _DSW/_PSW method.
    133      1.4    jruoho 	 */
    134  1.6.2.1     rmind 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    135      1.4    jruoho 
    136  1.6.2.1     rmind 		if ((ad->ad_flags & ACPI_DEVICE_WAKEUP) == 0)
    137  1.6.2.1     rmind 			continue;
    138      1.4    jruoho 
    139  1.6.2.2     rmind 		acpi_wakedev_gpe(ad, ad->ad_wake, state);
    140  1.6.2.2     rmind 		acpi_wakedev_method(ad, ad->ad_wake, state);
    141      1.1  jmcneill 	}
    142      1.1  jmcneill }
    143      1.4    jruoho 
    144      1.4    jruoho static void
    145  1.6.2.2     rmind acpi_wakedev_method(struct acpi_devnode *ad, int enable, int state)
    146      1.4    jruoho {
    147      1.4    jruoho 	ACPI_OBJECT_LIST arg;
    148      1.4    jruoho 	ACPI_OBJECT obj[3];
    149      1.4    jruoho 	ACPI_STATUS rv;
    150      1.4    jruoho 
    151      1.4    jruoho 	/*
    152      1.4    jruoho 	 * First try to call the Device Sleep Wake control method, _DSW.
    153      1.4    jruoho 	 * Only if this is not available, resort to to the Power State
    154      1.4    jruoho 	 * Wake control method, _PSW, which was deprecated in ACPI 3.0.
    155      1.4    jruoho 	 *
    156      1.4    jruoho 	 * The arguments to these methods are as follows:
    157      1.4    jruoho 	 *
    158      1.4    jruoho 	 *		arg0		arg1		arg2
    159      1.4    jruoho 	 *		----		----		----
    160      1.4    jruoho 	 *	 _PSW	0: disable
    161      1.4    jruoho 	 *		1: enable
    162      1.4    jruoho 	 *
    163      1.4    jruoho 	 *	 _DSW	0: disable	0: S0		0: D0
    164      1.4    jruoho 	 *		1: enable	1: S1		1: D0 or D1
    165      1.4    jruoho 	 *						2: D0, D1, or D2
    166      1.4    jruoho 	 *				x: Sx		3: D0, D1, D2 or D3
    167      1.4    jruoho 	 */
    168      1.4    jruoho 	arg.Count = 3;
    169      1.4    jruoho 	arg.Pointer = obj;
    170      1.4    jruoho 
    171      1.4    jruoho 	obj[0].Integer.Value = enable;
    172      1.4    jruoho 	obj[1].Integer.Value = state;
    173  1.6.2.2     rmind 	obj[2].Integer.Value = ACPI_STATE_D0;
    174      1.4    jruoho 
    175      1.4    jruoho 	obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
    176      1.4    jruoho 
    177      1.4    jruoho 	rv = AcpiEvaluateObject(ad->ad_handle, "_DSW", &arg, NULL);
    178      1.4    jruoho 
    179      1.4    jruoho 	if (ACPI_SUCCESS(rv))
    180      1.4    jruoho 		return;
    181      1.4    jruoho 
    182      1.4    jruoho 	if (rv != AE_NOT_FOUND)
    183      1.4    jruoho 		goto fail;
    184      1.4    jruoho 
    185      1.4    jruoho 	rv = acpi_eval_set_integer(ad->ad_handle, "_PSW", enable);
    186      1.4    jruoho 
    187      1.4    jruoho 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
    188      1.4    jruoho 		goto fail;
    189      1.4    jruoho 
    190      1.4    jruoho 	return;
    191      1.4    jruoho 
    192      1.4    jruoho fail:
    193  1.6.2.1     rmind 	aprint_error_dev(ad->ad_root, "failed to evaluate wake "
    194      1.4    jruoho 	    "control method: %s\n", AcpiFormatException(rv));
    195      1.4    jruoho }
    196  1.6.2.1     rmind 
    197  1.6.2.1     rmind static void
    198  1.6.2.2     rmind acpi_wakedev_gpe(struct acpi_devnode *ad, int enable, int state)
    199  1.6.2.1     rmind {
    200  1.6.2.1     rmind 	ACPI_OBJECT *elm, *obj;
    201  1.6.2.2     rmind 	ACPI_HANDLE hdl = NULL;
    202  1.6.2.1     rmind 	ACPI_INTEGER val;
    203  1.6.2.1     rmind 	ACPI_BUFFER buf;
    204  1.6.2.1     rmind 	ACPI_STATUS rv;
    205  1.6.2.1     rmind 
    206  1.6.2.2     rmind 	rv = acpi_eval_struct(ad->ad_handle, "_PRW", &buf);
    207  1.6.2.1     rmind 
    208  1.6.2.1     rmind 	if (ACPI_FAILURE(rv))
    209  1.6.2.1     rmind 		return;
    210  1.6.2.1     rmind 
    211  1.6.2.1     rmind 	obj = buf.Pointer;
    212  1.6.2.1     rmind 
    213  1.6.2.1     rmind 	if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count < 2)
    214  1.6.2.1     rmind 		goto out;
    215  1.6.2.1     rmind 
    216  1.6.2.1     rmind 	/*
    217  1.6.2.1     rmind 	 * As noted in ACPI 3.0 (section 7.2.10), the _PRW object is
    218  1.6.2.1     rmind 	 * a package in which the first element is either an integer
    219  1.6.2.1     rmind 	 * or again a package. In the latter case the package inside
    220  1.6.2.1     rmind 	 * the package element has two elements, a reference handle
    221  1.6.2.1     rmind 	 * and the GPE number.
    222  1.6.2.1     rmind 	 */
    223  1.6.2.1     rmind 	elm = &obj->Package.Elements[0];
    224  1.6.2.1     rmind 
    225  1.6.2.1     rmind 	switch (elm->Type) {
    226  1.6.2.1     rmind 
    227  1.6.2.1     rmind 	case ACPI_TYPE_INTEGER:
    228  1.6.2.1     rmind 		val = elm->Integer.Value;
    229  1.6.2.1     rmind 		break;
    230  1.6.2.1     rmind 
    231  1.6.2.1     rmind 	case ACPI_TYPE_PACKAGE:
    232  1.6.2.1     rmind 
    233  1.6.2.1     rmind 		if (elm->Package.Count < 2)
    234  1.6.2.1     rmind 			goto out;
    235  1.6.2.1     rmind 
    236  1.6.2.1     rmind 		if (elm->Package.Elements[0].Type != ACPI_TYPE_LOCAL_REFERENCE)
    237  1.6.2.1     rmind 			goto out;
    238  1.6.2.1     rmind 
    239  1.6.2.1     rmind 		if (elm->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
    240  1.6.2.1     rmind 			goto out;
    241  1.6.2.1     rmind 
    242  1.6.2.2     rmind 		hdl = elm->Package.Elements[0].Reference.Handle;
    243  1.6.2.1     rmind 		val = elm->Package.Elements[1].Integer.Value;
    244  1.6.2.1     rmind 		break;
    245  1.6.2.1     rmind 
    246  1.6.2.1     rmind 	default:
    247  1.6.2.1     rmind 		goto out;
    248  1.6.2.1     rmind 	}
    249  1.6.2.1     rmind 
    250  1.6.2.2     rmind  	/*
    251  1.6.2.2     rmind 	 * The second element is an integer that contains the
    252  1.6.2.2     rmind 	 * lowest sleep state that can be entered while still
    253  1.6.2.2     rmind 	 * providing wake-up functionality. The rest of the
    254  1.6.2.2     rmind 	 * elements are references to power resources.
    255  1.6.2.2     rmind 	 */
    256  1.6.2.2     rmind 	elm = &obj->Package.Elements[1];
    257  1.6.2.2     rmind 
    258  1.6.2.2     rmind 	if (elm->Type != ACPI_TYPE_INTEGER)
    259  1.6.2.2     rmind 		goto out;
    260  1.6.2.2     rmind 
    261  1.6.2.2     rmind 	if (state > elm->Integer.Value)
    262  1.6.2.2     rmind 		aprint_error_dev(ad->ad_root, "sleep state S%d "
    263  1.6.2.2     rmind 		    "loses wake for %s\n", state, ad->ad_name);
    264  1.6.2.2     rmind 
    265  1.6.2.1     rmind 	/*
    266  1.6.2.2     rmind 	 * Turn on power resources.
    267  1.6.2.1     rmind 	 */
    268  1.6.2.2     rmind 	if (enable != 0)
    269  1.6.2.2     rmind 		acpi_wakedev_power(ad, obj);
    270  1.6.2.2     rmind 
    271  1.6.2.2     rmind 	/*
    272  1.6.2.2     rmind 	 * Set both runtime and wake GPEs, but unset only wake GPEs.
    273  1.6.2.2     rmind 	 */
    274  1.6.2.2     rmind 	if (enable != 0)
    275  1.6.2.2     rmind 		(void)AcpiEnableGpe(hdl, val, ACPI_GPE_TYPE_WAKE_RUN);
    276  1.6.2.2     rmind 	else
    277  1.6.2.2     rmind 		(void)AcpiDisableGpe(hdl, val, ACPI_GPE_TYPE_WAKE);
    278  1.6.2.1     rmind 
    279  1.6.2.1     rmind 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "wake GPE %s for %s\n",
    280  1.6.2.1     rmind 		(enable != 0) ? "enabled" : "disabled", ad->ad_name));
    281  1.6.2.1     rmind 
    282  1.6.2.1     rmind out:
    283  1.6.2.1     rmind 	ACPI_FREE(buf.Pointer);
    284  1.6.2.1     rmind }
    285  1.6.2.2     rmind 
    286  1.6.2.2     rmind static void
    287  1.6.2.2     rmind acpi_wakedev_power(struct acpi_devnode *ad, ACPI_OBJECT *obj)
    288  1.6.2.2     rmind {
    289  1.6.2.2     rmind 	ACPI_OBJECT *elm;
    290  1.6.2.2     rmind 	ACPI_HANDLE hdl;
    291  1.6.2.2     rmind 	ACPI_STATUS rv;
    292  1.6.2.2     rmind 	uint32_t i, n;
    293  1.6.2.2     rmind 
    294  1.6.2.2     rmind 	n = obj->Package.Count;
    295  1.6.2.2     rmind 
    296  1.6.2.2     rmind 	if (n < 3)
    297  1.6.2.2     rmind 		return;
    298  1.6.2.2     rmind 
    299  1.6.2.2     rmind 	for (i = 2; i < n; i++) {
    300  1.6.2.2     rmind 
    301  1.6.2.2     rmind 		elm = &obj->Package.Elements[i];
    302  1.6.2.2     rmind 		rv = acpi_eval_reference_handle(elm, &hdl);
    303  1.6.2.2     rmind 
    304  1.6.2.2     rmind 		if (ACPI_FAILURE(rv))
    305  1.6.2.2     rmind 			continue;
    306  1.6.2.2     rmind 
    307  1.6.2.2     rmind 		(void)acpi_power_res(hdl, ad->ad_handle, true);
    308  1.6.2.2     rmind 	}
    309  1.6.2.2     rmind }
    310