Home | History | Annotate | Line # | Download | only in acpi
acpi_wakedev.c revision 1.4
      1 /* $NetBSD: acpi_wakedev.c,v 1.4 2010/03/05 21:01:44 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.4 2010/03/05 21:01:44 jruoho Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/device.h>
     34 #include <sys/kmem.h>
     35 #include <sys/queue.h>
     36 #include <sys/sysctl.h>
     37 #include <sys/systm.h>
     38 
     39 #include <dev/acpi/acpireg.h>
     40 #include <dev/acpi/acpivar.h>
     41 #include <dev/acpi/acpi_wakedev.h>
     42 
     43 static void	acpi_wakedev_prepare(struct acpi_devnode *, int, int);
     44 
     45 struct acpi_wakedev;
     46 
     47 static TAILQ_HEAD(, acpi_wakedev) acpi_wakedevlist =
     48     TAILQ_HEAD_INITIALIZER(acpi_wakedevlist);
     49 static int acpi_wakedev_node = -1;
     50 
     51 struct acpi_wakedev {
     52 	struct acpi_devnode	*aw_node;
     53 	int			aw_enabled;
     54 	struct sysctllog	*aw_sysctllog;
     55 	TAILQ_ENTRY(acpi_wakedev) aw_list;
     56 };
     57 
     58 static const char * const acpi_wakedev_default[] = {
     59 	"PNP0C0C",	/* power button */
     60 	"PNP0C0E",	/* sleep button */
     61 	"PNP0C0D",	/* lid switch */
     62 	"PNP03??",	/* PC KBD port */
     63 	NULL,
     64 };
     65 
     66 SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.wake subtree setup")
     67 {
     68 	const struct sysctlnode *rnode;
     69 	int err;
     70 
     71 	err = sysctl_createv(NULL, 0, NULL, NULL,
     72 	    CTLFLAG_PERMANENT,
     73 	    CTLTYPE_NODE, "hw", NULL,
     74 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
     75 	if (err)
     76 		return;
     77 	err = sysctl_createv(NULL, 0, NULL, &rnode,
     78 	    CTLFLAG_PERMANENT,
     79 	    CTLTYPE_NODE, "wake", NULL,
     80 	    NULL, 0, NULL, 0,
     81 	    CTL_HW, CTL_CREATE, CTL_EOL);
     82 	if (err)
     83 		return;
     84 	acpi_wakedev_node = rnode->sysctl_num;
     85 }
     86 
     87 static void
     88 acpi_wakedev_sysctl_add(struct acpi_wakedev *aw)
     89 {
     90 	int err;
     91 
     92 	if (acpi_wakedev_node == -1)
     93 		return;
     94 
     95 	err = sysctl_createv(&aw->aw_sysctllog, 0, NULL, NULL,
     96 	    CTLFLAG_READWRITE, CTLTYPE_INT, aw->aw_node->ad_name,
     97 	    NULL, NULL, 0, &aw->aw_enabled, 0,
     98 	    CTL_HW, acpi_wakedev_node, CTL_CREATE, CTL_EOL);
     99 	if (err)
    100 		printf("acpi_wakedev_sysctl_add: "
    101 		    "sysctl_createv(hw.wake.%s) failed. (%d)\n",
    102 		    aw->aw_node->ad_name, err);
    103 }
    104 
    105 static bool
    106 acpi_wakedev_add(struct acpi_softc *sc, struct acpi_devnode *ad)
    107 {
    108 	struct acpi_wakedev *aw;
    109 	ACPI_HANDLE hdl;
    110 
    111 	if (ACPI_FAILURE(AcpiGetHandle(ad->ad_handle, "_PRW", &hdl)))
    112 		return false;
    113 
    114 	aw = kmem_alloc(sizeof(*aw), KM_SLEEP);
    115 	if (aw == NULL) {
    116 		aprint_error("acpi_wakedev_add: kmem_alloc failed\n");
    117 		return false;
    118 	}
    119 	aw->aw_node = ad;
    120 	aw->aw_sysctllog = NULL;
    121 	if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default))
    122 		aw->aw_enabled = 1;
    123 	else
    124 		aw->aw_enabled = 0;
    125 
    126 	TAILQ_INSERT_TAIL(&acpi_wakedevlist, aw, aw_list);
    127 
    128 	acpi_wakedev_sysctl_add(aw);
    129 
    130 	return true;
    131 }
    132 
    133 static void
    134 acpi_wakedev_print(struct acpi_wakedev *aw)
    135 {
    136 	aprint_debug(" %s", aw->aw_node->ad_name);
    137 }
    138 
    139 int
    140 acpi_wakedev_scan(struct acpi_softc *sc)
    141 {
    142 	struct acpi_scope *as;
    143 	struct acpi_devnode *ad;
    144 	struct acpi_wakedev *aw;
    145 	ACPI_DEVICE_INFO *di;
    146 	int count = 0;
    147 
    148 #define ACPI_STA_DEV_VALID	\
    149 	(ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|ACPI_STA_DEV_OK)
    150 
    151 	TAILQ_FOREACH(as, &sc->sc_scopes, as_list)
    152 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    153 			di = ad->ad_devinfo;
    154 			if (di->Type != ACPI_TYPE_DEVICE)
    155 				continue;
    156 			if ((di->Valid & ACPI_VALID_STA) != 0 &&
    157 			    (di->CurrentStatus & ACPI_STA_DEV_VALID) !=
    158 			     ACPI_STA_DEV_VALID)
    159 				continue;
    160 			if (acpi_wakedev_add(sc, ad) == true)
    161 				++count;
    162 		}
    163 
    164 #undef ACPI_STA_DEV_VALID
    165 
    166 	if (count == 0)
    167 		return 0;
    168 
    169 	aprint_debug_dev(sc->sc_dev, "wakeup devices:");
    170 	TAILQ_FOREACH(aw, &acpi_wakedevlist, aw_list)
    171 		acpi_wakedev_print(aw);
    172 	aprint_debug("\n");
    173 
    174 	return count;
    175 }
    176 
    177 void
    178 acpi_wakedev_commit(struct acpi_softc *sc, int state)
    179 {
    180 	struct acpi_wakedev *aw;
    181 
    182 	/*
    183 	 * As noted in ACPI 3.0 (p. 243), preparing
    184 	 * a device for wakeup is a two-step process:
    185 	 *
    186 	 *  1.	Enable all power resources in _PRW.
    187 	 *
    188 	 *  2.	If present, execute _DSW/_PSW method.
    189 	 *
    190 	 * XXX: The first one is yet to be implemented.
    191 	 */
    192 	TAILQ_FOREACH(aw, &acpi_wakedevlist, aw_list) {
    193 
    194 		if (aw->aw_enabled) {
    195 			aprint_debug_dev(sc->sc_dev, "set wake GPE (%s)\n",
    196 			    aw->aw_node->ad_name);
    197 			acpi_set_wake_gpe(aw->aw_node->ad_handle);
    198 		} else
    199 			acpi_clear_wake_gpe(aw->aw_node->ad_handle);
    200 
    201 		acpi_wakedev_prepare(aw->aw_node, aw->aw_enabled, state);
    202 	}
    203 }
    204 
    205 static void
    206 acpi_wakedev_prepare(struct acpi_devnode *ad, int enable, int state)
    207 {
    208 	ACPI_OBJECT_LIST arg;
    209 	ACPI_OBJECT obj[3];
    210 	ACPI_STATUS rv;
    211 
    212 	/*
    213 	 * First try to call the Device Sleep Wake control method, _DSW.
    214 	 * Only if this is not available, resort to to the Power State
    215 	 * Wake control method, _PSW, which was deprecated in ACPI 3.0.
    216 	 *
    217 	 * The arguments to these methods are as follows:
    218 	 *
    219 	 *		arg0		arg1		arg2
    220 	 *		----		----		----
    221 	 *	 _PSW	0: disable
    222 	 *		1: enable
    223 	 *
    224 	 *	 _DSW	0: disable	0: S0		0: D0
    225 	 *		1: enable	1: S1		1: D0 or D1
    226 	 *						2: D0, D1, or D2
    227 	 *				x: Sx		3: D0, D1, D2 or D3
    228 	 */
    229 	arg.Count = 3;
    230 	arg.Pointer = obj;
    231 
    232 	obj[0].Integer.Value = enable;
    233 	obj[1].Integer.Value = state;
    234 	obj[2].Integer.Value = 3;
    235 
    236 	obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
    237 
    238 	rv = AcpiEvaluateObject(ad->ad_handle, "_DSW", &arg, NULL);
    239 
    240 	if (ACPI_SUCCESS(rv))
    241 		return;
    242 
    243 	if (rv != AE_NOT_FOUND)
    244 		goto fail;
    245 
    246 	rv = acpi_eval_set_integer(ad->ad_handle, "_PSW", enable);
    247 
    248 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
    249 		goto fail;
    250 
    251 	return;
    252 
    253 fail:
    254 	aprint_error_dev(ad->ad_device, "failed to evaluate wake "
    255 	    "control method: %s\n", AcpiFormatException(rv));
    256 }
    257 
    258