Home | History | Annotate | Line # | Download | only in acpi
acpi_wakedev.c revision 1.2.6.1
      1  1.2.6.1  uebayasi /* $NetBSD: acpi_wakedev.c,v 1.2.6.1 2010/04/30 14:43:06 uebayasi Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4  1.2.6.1  uebayasi  * 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.2.6.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: acpi_wakedev.c,v 1.2.6.1 2010/04/30 14:43:06 uebayasi Exp $");
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/param.h>
     33      1.1  jmcneill #include <sys/device.h>
     34      1.1  jmcneill #include <sys/sysctl.h>
     35  1.2.6.1  uebayasi #include <sys/systm.h>
     36      1.1  jmcneill 
     37      1.1  jmcneill #include <dev/acpi/acpireg.h>
     38  1.2.6.1  uebayasi #include <dev/acpi/acpivar.h>
     39      1.1  jmcneill #include <dev/acpi/acpi_wakedev.h>
     40      1.1  jmcneill 
     41  1.2.6.1  uebayasi #define _COMPONENT		   ACPI_BUS_COMPONENT
     42  1.2.6.1  uebayasi ACPI_MODULE_NAME		   ("acpi_wakedev")
     43      1.1  jmcneill 
     44      1.1  jmcneill static const char * const acpi_wakedev_default[] = {
     45      1.1  jmcneill 	"PNP0C0C",	/* power button */
     46      1.1  jmcneill 	"PNP0C0E",	/* sleep button */
     47      1.1  jmcneill 	"PNP0C0D",	/* lid switch */
     48      1.1  jmcneill 	"PNP03??",	/* PC KBD port */
     49      1.1  jmcneill 	NULL,
     50      1.1  jmcneill };
     51      1.1  jmcneill 
     52  1.2.6.1  uebayasi static const struct sysctlnode *rnode = NULL;
     53  1.2.6.1  uebayasi 
     54  1.2.6.1  uebayasi static void	acpi_wakedev_prepare(struct acpi_devnode *, int, int);
     55  1.2.6.1  uebayasi static void	acpi_wakedev_gpe(struct acpi_devnode *, int);
     56  1.2.6.1  uebayasi 
     57  1.2.6.1  uebayasi SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.acpi.wake subtree setup")
     58      1.1  jmcneill {
     59      1.1  jmcneill 	int err;
     60      1.1  jmcneill 
     61      1.1  jmcneill 	err = sysctl_createv(NULL, 0, NULL, &rnode,
     62  1.2.6.1  uebayasi 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
     63  1.2.6.1  uebayasi 	    NULL, NULL, 0, NULL, 0,
     64  1.2.6.1  uebayasi 	    CTL_HW, CTL_EOL);
     65  1.2.6.1  uebayasi 
     66  1.2.6.1  uebayasi 	if (err != 0)
     67  1.2.6.1  uebayasi 		goto fail;
     68  1.2.6.1  uebayasi 
     69  1.2.6.1  uebayasi 	err = sysctl_createv(NULL, 0, &rnode, &rnode,
     70  1.2.6.1  uebayasi 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi",
     71  1.2.6.1  uebayasi 	    NULL, NULL, 0, NULL, 0,
     72  1.2.6.1  uebayasi 	    CTL_CREATE, CTL_EOL);
     73  1.2.6.1  uebayasi 
     74  1.2.6.1  uebayasi 	if (err != 0)
     75  1.2.6.1  uebayasi 		goto fail;
     76  1.2.6.1  uebayasi 
     77  1.2.6.1  uebayasi 	err = sysctl_createv(NULL, 0, &rnode, &rnode,
     78  1.2.6.1  uebayasi 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
     79  1.2.6.1  uebayasi 	    "wake", SYSCTL_DESCR("ACPI device wake-up"),
     80      1.1  jmcneill 	    NULL, 0, NULL, 0,
     81  1.2.6.1  uebayasi 	    CTL_CREATE, CTL_EOL);
     82  1.2.6.1  uebayasi 
     83  1.2.6.1  uebayasi 	if (err != 0)
     84  1.2.6.1  uebayasi 		goto fail;
     85  1.2.6.1  uebayasi 
     86  1.2.6.1  uebayasi 	return;
     87  1.2.6.1  uebayasi 
     88  1.2.6.1  uebayasi fail:
     89  1.2.6.1  uebayasi 	rnode = NULL;
     90      1.1  jmcneill }
     91      1.1  jmcneill 
     92  1.2.6.1  uebayasi void
     93  1.2.6.1  uebayasi acpi_wakedev_add(struct acpi_devnode *ad)
     94      1.1  jmcneill {
     95      1.1  jmcneill 	int err;
     96      1.1  jmcneill 
     97  1.2.6.1  uebayasi 	KASSERT(ad != NULL && ad->ad_root != NULL);
     98  1.2.6.1  uebayasi 	KASSERT((ad->ad_flags & ACPI_DEVICE_WAKEUP) != 0);
     99  1.2.6.1  uebayasi 
    100  1.2.6.1  uebayasi 	ad->ad_wake = 0;
    101  1.2.6.1  uebayasi 
    102  1.2.6.1  uebayasi 	if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default))
    103  1.2.6.1  uebayasi 		ad->ad_wake = 1;
    104  1.2.6.1  uebayasi 
    105  1.2.6.1  uebayasi 	if (rnode == NULL)
    106      1.1  jmcneill 		return;
    107      1.1  jmcneill 
    108  1.2.6.1  uebayasi 	err = sysctl_createv(NULL, 0, &rnode, NULL,
    109  1.2.6.1  uebayasi 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, ad->ad_name,
    110  1.2.6.1  uebayasi 	    NULL, NULL, 0, &ad->ad_wake, 0,
    111  1.2.6.1  uebayasi 	    CTL_CREATE, CTL_EOL);
    112  1.2.6.1  uebayasi 
    113  1.2.6.1  uebayasi 	if (err != 0)
    114  1.2.6.1  uebayasi 		aprint_error_dev(ad->ad_root, "sysctl_createv"
    115  1.2.6.1  uebayasi 		    "(hw.acpi.wake.%s) failed (err %d)\n", ad->ad_name, err);
    116      1.1  jmcneill }
    117      1.1  jmcneill 
    118  1.2.6.1  uebayasi void
    119  1.2.6.1  uebayasi acpi_wakedev_commit(struct acpi_softc *sc, int state)
    120      1.1  jmcneill {
    121  1.2.6.1  uebayasi 	struct acpi_devnode *ad;
    122  1.2.6.1  uebayasi 
    123  1.2.6.1  uebayasi 	/*
    124  1.2.6.1  uebayasi 	 * As noted in ACPI 3.0 (p. 243), preparing
    125  1.2.6.1  uebayasi 	 * a device for wakeup is a two-step process:
    126  1.2.6.1  uebayasi 	 *
    127  1.2.6.1  uebayasi 	 *  1.	Enable all power resources in _PRW.
    128  1.2.6.1  uebayasi 	 *
    129  1.2.6.1  uebayasi 	 *  2.	If present, execute _DSW/_PSW method.
    130  1.2.6.1  uebayasi 	 *
    131  1.2.6.1  uebayasi 	 * XXX: The first one is yet to be implemented.
    132  1.2.6.1  uebayasi 	 */
    133  1.2.6.1  uebayasi 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    134      1.1  jmcneill 
    135  1.2.6.1  uebayasi 		if ((ad->ad_flags & ACPI_DEVICE_WAKEUP) == 0)
    136  1.2.6.1  uebayasi 			continue;
    137      1.1  jmcneill 
    138  1.2.6.1  uebayasi 		acpi_wakedev_gpe(ad, ad->ad_wake);
    139  1.2.6.1  uebayasi 		acpi_wakedev_prepare(ad, ad->ad_wake, state);
    140      1.1  jmcneill 	}
    141  1.2.6.1  uebayasi }
    142  1.2.6.1  uebayasi 
    143  1.2.6.1  uebayasi static void
    144  1.2.6.1  uebayasi acpi_wakedev_prepare(struct acpi_devnode *ad, int enable, int state)
    145  1.2.6.1  uebayasi {
    146  1.2.6.1  uebayasi 	ACPI_OBJECT_LIST arg;
    147  1.2.6.1  uebayasi 	ACPI_OBJECT obj[3];
    148  1.2.6.1  uebayasi 	ACPI_STATUS rv;
    149  1.2.6.1  uebayasi 
    150  1.2.6.1  uebayasi 	/*
    151  1.2.6.1  uebayasi 	 * First try to call the Device Sleep Wake control method, _DSW.
    152  1.2.6.1  uebayasi 	 * Only if this is not available, resort to to the Power State
    153  1.2.6.1  uebayasi 	 * Wake control method, _PSW, which was deprecated in ACPI 3.0.
    154  1.2.6.1  uebayasi 	 *
    155  1.2.6.1  uebayasi 	 * The arguments to these methods are as follows:
    156  1.2.6.1  uebayasi 	 *
    157  1.2.6.1  uebayasi 	 *		arg0		arg1		arg2
    158  1.2.6.1  uebayasi 	 *		----		----		----
    159  1.2.6.1  uebayasi 	 *	 _PSW	0: disable
    160  1.2.6.1  uebayasi 	 *		1: enable
    161  1.2.6.1  uebayasi 	 *
    162  1.2.6.1  uebayasi 	 *	 _DSW	0: disable	0: S0		0: D0
    163  1.2.6.1  uebayasi 	 *		1: enable	1: S1		1: D0 or D1
    164  1.2.6.1  uebayasi 	 *						2: D0, D1, or D2
    165  1.2.6.1  uebayasi 	 *				x: Sx		3: D0, D1, D2 or D3
    166  1.2.6.1  uebayasi 	 */
    167  1.2.6.1  uebayasi 	arg.Count = 3;
    168  1.2.6.1  uebayasi 	arg.Pointer = obj;
    169  1.2.6.1  uebayasi 
    170  1.2.6.1  uebayasi 	obj[0].Integer.Value = enable;
    171  1.2.6.1  uebayasi 	obj[1].Integer.Value = state;
    172  1.2.6.1  uebayasi 	obj[2].Integer.Value = 3;
    173      1.1  jmcneill 
    174  1.2.6.1  uebayasi 	obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
    175      1.1  jmcneill 
    176  1.2.6.1  uebayasi 	rv = AcpiEvaluateObject(ad->ad_handle, "_DSW", &arg, NULL);
    177      1.1  jmcneill 
    178  1.2.6.1  uebayasi 	if (ACPI_SUCCESS(rv))
    179  1.2.6.1  uebayasi 		return;
    180  1.2.6.1  uebayasi 
    181  1.2.6.1  uebayasi 	if (rv != AE_NOT_FOUND)
    182  1.2.6.1  uebayasi 		goto fail;
    183  1.2.6.1  uebayasi 
    184  1.2.6.1  uebayasi 	rv = acpi_eval_set_integer(ad->ad_handle, "_PSW", enable);
    185  1.2.6.1  uebayasi 
    186  1.2.6.1  uebayasi 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
    187  1.2.6.1  uebayasi 		goto fail;
    188  1.2.6.1  uebayasi 
    189  1.2.6.1  uebayasi 	return;
    190  1.2.6.1  uebayasi 
    191  1.2.6.1  uebayasi fail:
    192  1.2.6.1  uebayasi 	aprint_error_dev(ad->ad_root, "failed to evaluate wake "
    193  1.2.6.1  uebayasi 	    "control method: %s\n", AcpiFormatException(rv));
    194      1.1  jmcneill }
    195      1.1  jmcneill 
    196      1.1  jmcneill static void
    197  1.2.6.1  uebayasi acpi_wakedev_gpe(struct acpi_devnode *ad, int enable)
    198      1.1  jmcneill {
    199  1.2.6.1  uebayasi 	ACPI_OBJECT *elm, *obj;
    200  1.2.6.1  uebayasi 	ACPI_INTEGER val;
    201  1.2.6.1  uebayasi 	ACPI_BUFFER buf;
    202  1.2.6.1  uebayasi 	ACPI_STATUS rv;
    203      1.1  jmcneill 
    204  1.2.6.1  uebayasi 	rv = acpi_eval_struct(ad->ad_handle, METHOD_NAME__PRW, &buf);
    205      1.1  jmcneill 
    206  1.2.6.1  uebayasi 	if (ACPI_FAILURE(rv))
    207  1.2.6.1  uebayasi 		return;
    208      1.1  jmcneill 
    209  1.2.6.1  uebayasi 	obj = buf.Pointer;
    210  1.2.6.1  uebayasi 
    211  1.2.6.1  uebayasi 	if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count < 2)
    212  1.2.6.1  uebayasi 		goto out;
    213      1.1  jmcneill 
    214  1.2.6.1  uebayasi 	/*
    215  1.2.6.1  uebayasi 	 * As noted in ACPI 3.0 (section 7.2.10), the _PRW object is
    216  1.2.6.1  uebayasi 	 * a package in which the first element is either an integer
    217  1.2.6.1  uebayasi 	 * or again a package. In the latter case the package inside
    218  1.2.6.1  uebayasi 	 * the package element has two elements, a reference handle
    219  1.2.6.1  uebayasi 	 * and the GPE number.
    220  1.2.6.1  uebayasi 	 */
    221  1.2.6.1  uebayasi 	elm = &obj->Package.Elements[0];
    222  1.2.6.1  uebayasi 
    223  1.2.6.1  uebayasi 	switch (elm->Type) {
    224  1.2.6.1  uebayasi 
    225  1.2.6.1  uebayasi 	case ACPI_TYPE_INTEGER:
    226  1.2.6.1  uebayasi 		val = elm->Integer.Value;
    227  1.2.6.1  uebayasi 		break;
    228  1.2.6.1  uebayasi 
    229  1.2.6.1  uebayasi 	case ACPI_TYPE_PACKAGE:
    230  1.2.6.1  uebayasi 
    231  1.2.6.1  uebayasi 		if (elm->Package.Count < 2)
    232  1.2.6.1  uebayasi 			goto out;
    233  1.2.6.1  uebayasi 
    234  1.2.6.1  uebayasi 		if (elm->Package.Elements[0].Type != ACPI_TYPE_LOCAL_REFERENCE)
    235  1.2.6.1  uebayasi 			goto out;
    236  1.2.6.1  uebayasi 
    237  1.2.6.1  uebayasi 		if (elm->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
    238  1.2.6.1  uebayasi 			goto out;
    239  1.2.6.1  uebayasi 
    240  1.2.6.1  uebayasi 		val = elm->Package.Elements[1].Integer.Value;
    241  1.2.6.1  uebayasi 		break;
    242  1.2.6.1  uebayasi 
    243  1.2.6.1  uebayasi 	default:
    244  1.2.6.1  uebayasi 		goto out;
    245  1.2.6.1  uebayasi 	}
    246  1.2.6.1  uebayasi 
    247  1.2.6.1  uebayasi 	/*
    248  1.2.6.1  uebayasi 	 * Set or unset a GPE as both runtime and wake.
    249  1.2.6.1  uebayasi 	 */
    250  1.2.6.1  uebayasi 	if (enable == 0)
    251  1.2.6.1  uebayasi 		(void)AcpiDisableGpe(NULL, val, ACPI_NOT_ISR);
    252  1.2.6.1  uebayasi 	else {
    253  1.2.6.1  uebayasi 		(void)AcpiSetGpeType(NULL, val, ACPI_GPE_TYPE_WAKE_RUN);
    254  1.2.6.1  uebayasi 		(void)AcpiEnableGpe(NULL, val, ACPI_NOT_ISR);
    255      1.1  jmcneill 	}
    256  1.2.6.1  uebayasi 
    257  1.2.6.1  uebayasi 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "wake GPE %s for %s\n",
    258  1.2.6.1  uebayasi 		(enable != 0) ? "enabled" : "disabled", ad->ad_name));
    259  1.2.6.1  uebayasi 
    260  1.2.6.1  uebayasi out:
    261  1.2.6.1  uebayasi 	ACPI_FREE(buf.Pointer);
    262      1.1  jmcneill }
    263