1 /* $NetBSD: acpi_button.c,v 1.30 2010/01/31 06:10:53 jruoho 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 Button driver. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.30 2010/01/31 06:10:53 jruoho 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 #define _COMPONENT ACPI_BUTTON_COMPONENT 56 ACPI_MODULE_NAME ("acpi_button") 57 58 struct acpibut_softc { 59 struct acpi_devnode *sc_node; /* our ACPI devnode */ 60 struct sysmon_pswitch sc_smpsw; /* our sysmon glue */ 61 int sc_flags; /* see below */ 62 }; 63 64 static const char * const power_button_hid[] = { 65 "PNP0C0C", 66 NULL 67 }; 68 69 static const char * const sleep_button_hid[] = { 70 "PNP0C0E", 71 NULL 72 }; 73 74 #define ACPIBUT_F_VERBOSE 0x01 /* verbose events */ 75 76 static int acpibut_match(device_t, cfdata_t, void *); 77 static void acpibut_attach(device_t, device_t, void *); 78 static int acpibut_detach(device_t, int); 79 static void acpibut_pressed_event(void *); 80 static void acpibut_notify_handler(ACPI_HANDLE, UINT32, void *); 81 82 CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc), 83 acpibut_match, acpibut_attach, acpibut_detach, NULL); 84 85 /* 86 * acpibut_match: 87 * 88 * Autoconfiguration `match' routine. 89 */ 90 static int 91 acpibut_match(device_t parent, cfdata_t match, void *aux) 92 { 93 struct acpi_attach_args *aa = aux; 94 95 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 96 return 0; 97 98 if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) 99 return 1; 100 101 if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) 102 return 1; 103 104 return 0; 105 } 106 107 /* 108 * acpibut_attach: 109 * 110 * Autoconfiguration `attach' routine. 111 */ 112 static void 113 acpibut_attach(device_t parent, device_t self, void *aux) 114 { 115 struct acpibut_softc *sc = device_private(self); 116 struct acpi_attach_args *aa = aux; 117 const char *desc; 118 ACPI_STATUS rv; 119 120 sc->sc_smpsw.smpsw_name = device_xname(self); 121 122 if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) { 123 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER; 124 desc = "Power"; 125 } else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) { 126 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP; 127 desc = "Sleep"; 128 } else { 129 panic("acpibut_attach: impossible"); 130 } 131 132 aprint_naive(": ACPI %s Button\n", desc); 133 aprint_normal(": ACPI %s Button\n", desc); 134 135 sc->sc_node = aa->aa_node; 136 137 if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) { 138 aprint_error_dev(self, "unable to register with sysmon\n"); 139 return; 140 } 141 142 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, 143 ACPI_DEVICE_NOTIFY, acpibut_notify_handler, self); 144 if (ACPI_FAILURE(rv)) { 145 aprint_error_dev(self, 146 "unable to register DEVICE NOTIFY handler: %s\n", 147 AcpiFormatException(rv)); 148 return; 149 } 150 151 #ifdef ACPI_BUT_DEBUG 152 /* Display the current state when it changes. */ 153 sc->sc_flags = ACPIBUT_F_VERBOSE; 154 #endif 155 156 if (!pmf_device_register(self, NULL, NULL)) 157 aprint_error_dev(self, "couldn't establish power handler\n"); 158 } 159 160 /* 161 * acpibut_detach: 162 * 163 * Autoconfiguration `detach' routine. 164 */ 165 static int 166 acpibut_detach(device_t self, int flags) 167 { 168 struct acpibut_softc *sc = device_private(self); 169 ACPI_STATUS rv; 170 171 rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle, 172 ACPI_DEVICE_NOTIFY, acpibut_notify_handler); 173 174 if (ACPI_FAILURE(rv)) 175 return EBUSY; 176 177 pmf_device_deregister(self); 178 sysmon_pswitch_unregister(&sc->sc_smpsw); 179 180 return 0; 181 } 182 183 /* 184 * acpibut_pressed_event: 185 * 186 * Deal with a button being pressed. 187 */ 188 static void 189 acpibut_pressed_event(void *arg) 190 { 191 device_t dv = arg; 192 struct acpibut_softc *sc = device_private(dv); 193 194 if (sc->sc_flags & ACPIBUT_F_VERBOSE) 195 aprint_verbose_dev(dv, "button pressed\n"); 196 197 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED); 198 } 199 200 /* 201 * acpibut_notify_handler: 202 * 203 * Callback from ACPI interrupt handler to notify us of an event. 204 */ 205 static void 206 acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context) 207 { 208 device_t dv = context; 209 ACPI_STATUS rv; 210 211 switch (notify) { 212 case ACPI_NOTIFY_S0PowerButtonPressed: 213 #if 0 214 case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */ 215 #endif 216 #ifdef ACPI_BUT_DEBUG 217 aprint_debug_dev(dv, "received ButtonPressed message\n"); 218 #endif 219 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, 220 acpibut_pressed_event, dv); 221 if (ACPI_FAILURE(rv)) 222 aprint_error_dev(dv, 223 "WARNING: unable to queue button pressed callback: %s\n", 224 AcpiFormatException(rv)); 225 break; 226 227 /* XXX ACPI_NOTIFY_DeviceWake?? */ 228 229 default: 230 aprint_error_dev(dv, "received unknown notify message: 0x%x\n", 231 notify); 232 } 233 } 234