1 /* $NetBSD: acpi_wakedev.c,v 1.8 2010/03/16 07:18:55 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.8 2010/03/16 07:18:55 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 int acpi_wakedev_node = -1; 45 46 static const char * const acpi_wakedev_default[] = { 47 "PNP0C0C", /* power button */ 48 "PNP0C0E", /* sleep button */ 49 "PNP0C0D", /* lid switch */ 50 "PNP03??", /* PC KBD port */ 51 NULL, 52 }; 53 54 static void acpi_wakedev_prepare(struct acpi_devnode *, int, int); 55 56 SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.wake subtree setup") 57 { 58 const struct sysctlnode *rnode; 59 int err; 60 61 err = sysctl_createv(NULL, 0, NULL, NULL, 62 CTLFLAG_PERMANENT, 63 CTLTYPE_NODE, "hw", NULL, 64 NULL, 0, NULL, 0, CTL_HW, CTL_EOL); 65 if (err) 66 return; 67 err = sysctl_createv(NULL, 0, NULL, &rnode, 68 CTLFLAG_PERMANENT, 69 CTLTYPE_NODE, "wake", NULL, 70 NULL, 0, NULL, 0, 71 CTL_HW, CTL_CREATE, CTL_EOL); 72 if (err) 73 return; 74 acpi_wakedev_node = rnode->sysctl_num; 75 } 76 77 void 78 acpi_wakedev_add(struct acpi_devnode *ad) 79 { 80 int err; 81 82 KASSERT(ad != NULL && ad->ad_parent != NULL); 83 KASSERT((ad->ad_flags & ACPI_DEVICE_WAKEUP) != 0); 84 85 ad->ad_wake.wake_enabled = 0; 86 ad->ad_wake.wake_sysctllog = NULL; 87 88 if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default)) 89 ad->ad_wake.wake_enabled = 1; 90 91 if (acpi_wakedev_node == -1) 92 return; 93 94 err = sysctl_createv(&ad->ad_wake.wake_sysctllog, 0, NULL, NULL, 95 CTLFLAG_READWRITE, CTLTYPE_INT, ad->ad_name, 96 NULL, NULL, 0, &ad->ad_wake.wake_enabled, 0, 97 CTL_HW, acpi_wakedev_node, CTL_CREATE, CTL_EOL); 98 99 if (err != 0) 100 aprint_error_dev(ad->ad_parent, "sysctl_createv(hw.wake.%s) " 101 "failed (err %d)\n", ad->ad_name, err); 102 } 103 104 void 105 acpi_wakedev_commit(struct acpi_softc *sc, int state) 106 { 107 struct acpi_devnode *ad; 108 109 /* 110 * As noted in ACPI 3.0 (p. 243), preparing 111 * a device for wakeup is a two-step process: 112 * 113 * 1. Enable all power resources in _PRW. 114 * 115 * 2. If present, execute _DSW/_PSW method. 116 * 117 * XXX: The first one is yet to be implemented. 118 */ 119 SIMPLEQ_FOREACH(ad, &sc->sc_devnodes, ad_list) { 120 121 if ((ad->ad_flags & ACPI_DEVICE_WAKEUP) == 0) 122 continue; 123 124 if (ad->ad_wake.wake_enabled == 0) 125 acpi_clear_wake_gpe(ad->ad_handle); 126 else { 127 aprint_debug_dev(ad->ad_parent, 128 "set wake GPE for %s\n", ad->ad_name); 129 acpi_set_wake_gpe(ad->ad_handle); 130 } 131 132 acpi_wakedev_prepare(ad, ad->ad_wake.wake_enabled, state); 133 } 134 } 135 136 static void 137 acpi_wakedev_prepare(struct acpi_devnode *ad, int enable, int state) 138 { 139 ACPI_OBJECT_LIST arg; 140 ACPI_OBJECT obj[3]; 141 ACPI_STATUS rv; 142 143 /* 144 * First try to call the Device Sleep Wake control method, _DSW. 145 * Only if this is not available, resort to to the Power State 146 * Wake control method, _PSW, which was deprecated in ACPI 3.0. 147 * 148 * The arguments to these methods are as follows: 149 * 150 * arg0 arg1 arg2 151 * ---- ---- ---- 152 * _PSW 0: disable 153 * 1: enable 154 * 155 * _DSW 0: disable 0: S0 0: D0 156 * 1: enable 1: S1 1: D0 or D1 157 * 2: D0, D1, or D2 158 * x: Sx 3: D0, D1, D2 or D3 159 */ 160 arg.Count = 3; 161 arg.Pointer = obj; 162 163 obj[0].Integer.Value = enable; 164 obj[1].Integer.Value = state; 165 obj[2].Integer.Value = 3; 166 167 obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER; 168 169 rv = AcpiEvaluateObject(ad->ad_handle, "_DSW", &arg, NULL); 170 171 if (ACPI_SUCCESS(rv)) 172 return; 173 174 if (rv != AE_NOT_FOUND) 175 goto fail; 176 177 rv = acpi_eval_set_integer(ad->ad_handle, "_PSW", enable); 178 179 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND) 180 goto fail; 181 182 return; 183 184 fail: 185 aprint_error_dev(ad->ad_parent, "failed to evaluate wake " 186 "control method: %s\n", AcpiFormatException(rv)); 187 } 188