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