1 /* $NetBSD: acpi_lid.c,v 1.28 2009/08/25 10:34:08 jmcneill Exp $ */ 2 3 /* 4 * Copyright 2001, 2003 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * ACPI Lid Switch driver. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: acpi_lid.c,v 1.28 2009/08/25 10:34:08 jmcneill Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/device.h> 48 49 #include <dev/acpi/acpica.h> 50 #include <dev/acpi/acpireg.h> 51 #include <dev/acpi/acpivar.h> 52 53 #include <dev/sysmon/sysmonvar.h> 54 55 struct acpilid_softc { 56 struct acpi_devnode *sc_node; /* our ACPI devnode */ 57 struct sysmon_pswitch sc_smpsw; /* our sysmon glue */ 58 }; 59 60 static const char * const lid_hid[] = { 61 "PNP0C0D", 62 NULL 63 }; 64 65 static int acpilid_match(device_t, cfdata_t, void *); 66 static void acpilid_attach(device_t, device_t, void *); 67 static int acpilid_detach(device_t, int); 68 69 CFATTACH_DECL_NEW(acpilid, sizeof(struct acpilid_softc), 70 acpilid_match, acpilid_attach, acpilid_detach, NULL); 71 72 static void acpilid_status_changed(void *); 73 static void acpilid_notify_handler(ACPI_HANDLE, UINT32, void *); 74 75 static void acpilid_wake_event(device_t, bool); 76 static bool acpilid_suspend(device_t PMF_FN_PROTO); 77 78 /* 79 * acpilid_match: 80 * 81 * Autoconfiguration `match' routine. 82 */ 83 static int 84 acpilid_match(device_t parent, cfdata_t match, void *aux) 85 { 86 struct acpi_attach_args *aa = aux; 87 88 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 89 return 0; 90 91 return acpi_match_hid(aa->aa_node->ad_devinfo, lid_hid); 92 } 93 94 /* 95 * acpilid_attach: 96 * 97 * Autoconfiguration `attach' routine. 98 */ 99 static void 100 acpilid_attach(device_t parent, device_t self, void *aux) 101 { 102 struct acpilid_softc *sc = device_private(self); 103 struct acpi_attach_args *aa = aux; 104 ACPI_STATUS rv; 105 106 aprint_naive(": ACPI Lid Switch\n"); 107 aprint_normal(": ACPI Lid Switch\n"); 108 109 sc->sc_node = aa->aa_node; 110 111 sc->sc_smpsw.smpsw_name = device_xname(self); 112 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_LID; 113 if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) { 114 aprint_error_dev(self, "unable to register with sysmon\n"); 115 return; 116 } 117 118 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, 119 ACPI_DEVICE_NOTIFY, acpilid_notify_handler, self); 120 if (ACPI_FAILURE(rv)) { 121 aprint_error_dev(self, 122 "unable to register DEVICE NOTIFY handler: %s\n", 123 AcpiFormatException(rv)); 124 return; 125 } 126 127 if (!pmf_device_register(self, acpilid_suspend, NULL)) 128 aprint_error_dev(self, "couldn't establish power handler\n"); 129 } 130 131 static int 132 acpilid_detach(device_t self, int flags) 133 { 134 struct acpilid_softc *sc = device_private(self); 135 ACPI_STATUS rv; 136 137 rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle, 138 ACPI_DEVICE_NOTIFY, acpilid_notify_handler); 139 if (ACPI_FAILURE(rv)) { 140 aprint_error_dev(self, 141 "unable to deregister DEVICE NOTIFY handler: %s\n", 142 AcpiFormatException(rv)); 143 return EBUSY; 144 } 145 146 pmf_device_deregister(self); 147 sysmon_pswitch_unregister(&sc->sc_smpsw); 148 149 return 0; 150 } 151 152 static void 153 acpilid_wake_event(device_t dv, bool enable) 154 { 155 struct acpilid_softc *sc = device_private(dv); 156 157 ACPI_STATUS rv; 158 ACPI_OBJECT_LIST ArgList; 159 ACPI_OBJECT Arg; 160 161 ArgList.Count = 1; 162 ArgList.Pointer = &Arg; 163 164 Arg.Type = ACPI_TYPE_INTEGER; 165 Arg.Integer.Value = enable ? 1 : 0; 166 167 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_PSW", 168 &ArgList, NULL); 169 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND) 170 aprint_error_dev(dv, 171 "unable to evaluate _PSW handler: %s\n", 172 AcpiFormatException(rv)); 173 } 174 175 /* 176 * acpilid_status_changed: 177 * 178 * Get, and possibly display, the lid status, and take action. 179 */ 180 static void 181 acpilid_status_changed(void *arg) 182 { 183 struct acpilid_softc *sc = arg; 184 ACPI_INTEGER status; 185 ACPI_STATUS rv; 186 187 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &status); 188 if (ACPI_FAILURE(rv)) 189 return; 190 191 sysmon_pswitch_event(&sc->sc_smpsw, status == 0 ? 192 PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED); 193 } 194 195 /* 196 * acpilid_notify_handler: 197 * 198 * Callback from ACPI interrupt handler to notify us of an event. 199 */ 200 static void 201 acpilid_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context) 202 { 203 device_t dv = context; 204 struct acpilid_softc *sc = device_private(dv); 205 ACPI_STATUS rv; 206 207 switch (notify) { 208 case ACPI_NOTIFY_LidStatusChanged: 209 #ifdef ACPI_LID_DEBUG 210 printf("%s: received LidStatusChanged message\n", 211 device_xname(dv)); 212 #endif 213 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, 214 acpilid_status_changed, sc); 215 if (ACPI_FAILURE(rv)) 216 aprint_error_dev(dv, 217 "WARNING: unable to queue lid change " 218 "callback: %s\n", AcpiFormatException(rv)); 219 break; 220 221 default: 222 aprint_debug_dev(dv, 223 "received unknown notify message: 0x%x\n", notify); 224 } 225 } 226 227 static bool 228 acpilid_suspend(device_t dv PMF_FN_ARGS) 229 { 230 struct acpilid_softc *sc = device_private(dv); 231 ACPI_INTEGER status; 232 ACPI_STATUS rv; 233 234 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &status); 235 if (ACPI_FAILURE(rv)) 236 return true; 237 238 acpilid_wake_event(dv, status == 0); 239 return true; 240 } 241