1 /* $NetBSD: dalb_acpi.c,v 1.6 2010/01/31 19:49:29 jruoho Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 Christoph Egger <cegger@netbsd.org> 5 * Copyright (c) 2008 Jared D. McNeill <jmcneill@netbsd.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: dalb_acpi.c,v 1.6 2010/01/31 19:49:29 jruoho Exp $"); 31 32 /* 33 * Direct Application Launch Button: 34 * http://www.microsoft.com/whdc/system/vista/DirAppLaunch.mspx 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 #include <sys/proc.h> 41 #include <sys/kernel.h> 42 #include <sys/callout.h> 43 #include <sys/sysctl.h> 44 45 #include <machine/bus.h> 46 47 #include <dev/acpi/acpica.h> 48 #include <dev/acpi/acpivar.h> 49 #include <dev/acpi/acpireg.h> 50 51 #define _COMPONENT ACPI_RESOURCE_COMPONENT 52 ACPI_MODULE_NAME ("dalb_acpi") 53 54 #define DALB_ID_INVALID -1 55 56 struct acpi_dalb_softc { 57 device_t sc_dev; 58 struct acpi_devnode *sc_node; 59 struct sysctllog *sc_log; 60 61 ACPI_INTEGER sc_usageid; 62 63 /* There's one PNP0C32 ACPI device for each button. 64 * Therefore, one instance is enough. */ 65 struct sysmon_pswitch sc_smpsw; 66 bool sc_smpsw_valid; 67 }; 68 69 static int acpi_dalb_match(device_t, cfdata_t, void *); 70 static void acpi_dalb_attach(device_t, device_t, void *); 71 static int acpi_dalb_detach(device_t, int); 72 static void acpi_dalb_notify_handler(ACPI_HANDLE, UINT32, void *); 73 static bool acpi_dalb_resume(device_t, pmf_qual_t); 74 75 static void acpi_dalb_get_wakeup_hotkeys(void *opaque); 76 static void acpi_dalb_get_runtime_hotkeys(void *opaque); 77 78 CFATTACH_DECL_NEW(acpidalb, sizeof(struct acpi_dalb_softc), 79 acpi_dalb_match, acpi_dalb_attach, acpi_dalb_detach, NULL); 80 81 static const char * const acpi_dalb_ids[] = { 82 "PNP0C32", /* Direct Application Launch Button */ 83 NULL 84 }; 85 86 #ifdef DEBUG 87 #define DPRINTF(x) printf x 88 #else 89 #define DPRINTF(x) 90 #endif 91 92 #define DALB_SYSTEM_WAKEUP 0x02 93 #define DALB_SYSTEM_RUNTIME 0x80 94 95 static int 96 acpi_dalb_match(device_t parent, cfdata_t match, void *aux) 97 { 98 struct acpi_attach_args *aa = aux; 99 100 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 101 return 0; 102 103 return acpi_match_hid(aa->aa_node->ad_devinfo, acpi_dalb_ids); 104 } 105 106 static void 107 acpi_dalb_sysmon_init(struct acpi_dalb_softc *sc) 108 { 109 sc->sc_smpsw_valid = true; 110 sc->sc_smpsw.smpsw_name = device_xname(sc->sc_dev); 111 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_HOTKEY; 112 if (sysmon_pswitch_register(&sc->sc_smpsw)) { 113 aprint_error_dev(sc->sc_dev, 114 "couldn't register sleep with sysmon\n"); 115 sc->sc_smpsw_valid = false; 116 } 117 } 118 119 120 static void 121 acpi_dalb_init(device_t dev) 122 { 123 struct acpi_dalb_softc *sc = device_private(dev); 124 ACPI_OBJECT *obj; 125 ACPI_STATUS rv; 126 ACPI_BUFFER ret; 127 128 rv = acpi_eval_struct(sc->sc_node->ad_handle, "GHID", &ret); 129 if (ACPI_FAILURE(rv) || ret.Pointer == NULL) { 130 aprint_error_dev(dev, 131 "couldn't enable notify handler: (%s)\n", 132 AcpiFormatException(rv)); 133 return; 134 } 135 136 obj = (ACPI_OBJECT *)ret.Pointer; 137 if (obj->Type != ACPI_TYPE_BUFFER) { 138 sc->sc_usageid = DALB_ID_INVALID; 139 aprint_debug_dev(dev, "invalid ACPI type: %d\n", obj->Type); 140 goto out; 141 } 142 143 switch (obj->Buffer.Length) { 144 case 1: 145 sc->sc_usageid = *(uint8_t *)obj->Buffer.Pointer; 146 break; 147 case 2: 148 sc->sc_usageid = le16toh(*(uint16_t *)obj->Buffer.Pointer); 149 break; 150 case 4: 151 sc->sc_usageid = le32toh(*(uint32_t *)obj->Buffer.Pointer); 152 break; 153 default: 154 aprint_debug_dev(dev, "unhandled ret.Length: 0x%lx\n", 155 (unsigned long)obj->Buffer.Length); 156 sc->sc_usageid = DALB_ID_INVALID; 157 break; 158 } 159 160 out: 161 ACPI_FREE(ret.Pointer); 162 } 163 164 static void 165 acpi_dalb_attach(device_t parent, device_t self, void *aux) 166 { 167 struct acpi_dalb_softc *sc = device_private(self); 168 struct acpi_attach_args *aa = aux; 169 ACPI_STATUS rv; 170 171 aprint_naive("\n"); 172 aprint_normal(": Direct Application Launch Button\n"); 173 174 sc->sc_node = aa->aa_node; 175 sc->sc_dev = self; 176 177 config_interrupts(self, acpi_dalb_init); 178 179 /* Install notify handler */ 180 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, 181 ACPI_ALL_NOTIFY, acpi_dalb_notify_handler, self); 182 if (ACPI_FAILURE(rv)) 183 aprint_error_dev(self, 184 "couldn't install notify handler: (%s)\n", 185 AcpiFormatException(rv)); 186 187 sc->sc_smpsw_valid = false; 188 acpi_dalb_sysmon_init(sc); 189 190 if (!pmf_device_register(self, NULL, acpi_dalb_resume)) 191 aprint_error_dev(self, "couldn't establish power handler\n"); 192 } 193 194 static int 195 acpi_dalb_detach(device_t self, int flags) 196 { 197 struct acpi_dalb_softc *sc = device_private(self); 198 ACPI_STATUS rv; 199 200 rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle, 201 ACPI_ALL_NOTIFY, acpi_dalb_notify_handler); 202 203 if (ACPI_FAILURE(rv)) 204 return EBUSY; 205 206 pmf_device_deregister(self); 207 sysmon_pswitch_unregister(&sc->sc_smpsw); 208 209 return 0; 210 } 211 212 static void 213 acpi_dalb_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque) 214 { 215 device_t dev = opaque; 216 struct acpi_dalb_softc *sc = device_private(dev); 217 ACPI_STATUS rv; 218 219 switch (notify) { 220 case DALB_SYSTEM_WAKEUP: 221 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, 222 acpi_dalb_get_wakeup_hotkeys, dev); 223 break; 224 case DALB_SYSTEM_RUNTIME: 225 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, 226 acpi_dalb_get_runtime_hotkeys, dev); 227 break; 228 229 default: 230 aprint_error_dev(dev, 231 "unknown notification event 0x%x from button 0x%x\n", 232 notify, (uint32_t)sc->sc_usageid); 233 return; 234 } 235 236 if (ACPI_FAILURE(rv)) 237 aprint_error_dev(dev, "couldn't queue hotkey handler: %s\n", 238 AcpiFormatException(rv)); 239 } 240 241 static void 242 acpi_dalb_get_wakeup_hotkeys(void *opaque) 243 { 244 device_t dev = opaque; 245 struct acpi_dalb_softc *sc = device_private(dev); 246 247 if (!sc->sc_smpsw_valid) 248 return; 249 DPRINTF(("%s: %s: invoking sysmon_pswitch_event\n", 250 sc->sc_smpsw.smpsw_name, __func__)); 251 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED); 252 } 253 254 static void 255 acpi_dalb_get_runtime_hotkeys(void *opaque) 256 { 257 device_t dev = opaque; 258 struct acpi_dalb_softc *sc = device_private(dev); 259 260 if (!sc->sc_smpsw_valid) 261 return; 262 DPRINTF(("%s: %s: invoking sysmon_pswitch_event\n", 263 sc->sc_smpsw.smpsw_name, __func__)); 264 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED); 265 } 266 267 static bool 268 acpi_dalb_resume(device_t dev, pmf_qual_t qual) 269 { 270 struct acpi_dalb_softc *sc = device_private(dev); 271 ACPI_STATUS rv; 272 ACPI_BUFFER ret; 273 274 rv = acpi_eval_struct(sc->sc_node->ad_handle, "GHID", &ret); 275 if (ACPI_FAILURE(rv)) { 276 aprint_error_dev(dev, "couldn't evaluate GHID: %s\n", 277 AcpiFormatException(rv)); 278 return false; 279 } 280 if (ret.Pointer) 281 ACPI_FREE(ret.Pointer); 282 283 return true; 284 } 285