1 /* $NetBSD: acpi_button.c,v 1.40 2011/02/16 08:35:51 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.40 2011/02/16 08:35:51 jruoho Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/device.h> 47 #include <sys/module.h> 48 #include <sys/systm.h> 49 50 #include <dev/acpi/acpireg.h> 51 #include <dev/acpi/acpivar.h> 52 53 #define _COMPONENT ACPI_BUTTON_COMPONENT 54 ACPI_MODULE_NAME ("acpi_button") 55 56 #define ACPI_NOTIFY_BUTTON 0x80 57 58 struct acpibut_softc { 59 struct acpi_devnode *sc_node; 60 struct sysmon_pswitch sc_smpsw; 61 }; 62 63 static const char * const power_button_hid[] = { 64 "PNP0C0C", 65 NULL 66 }; 67 68 static const char * const sleep_button_hid[] = { 69 "PNP0C0E", 70 NULL 71 }; 72 73 static int acpibut_match(device_t, cfdata_t, void *); 74 static void acpibut_attach(device_t, device_t, void *); 75 static int acpibut_detach(device_t, int); 76 static void acpibut_pressed_event(void *); 77 static void acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *); 78 79 CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc), 80 acpibut_match, acpibut_attach, acpibut_detach, NULL); 81 82 /* 83 * acpibut_match: 84 * 85 * Autoconfiguration `match' routine. 86 */ 87 static int 88 acpibut_match(device_t parent, cfdata_t match, void *aux) 89 { 90 struct acpi_attach_args *aa = aux; 91 92 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 93 return 0; 94 95 if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) 96 return 1; 97 98 if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) 99 return 1; 100 101 return 0; 102 } 103 104 /* 105 * acpibut_attach: 106 * 107 * Autoconfiguration `attach' routine. 108 */ 109 static void 110 acpibut_attach(device_t parent, device_t self, void *aux) 111 { 112 struct acpibut_softc *sc = device_private(self); 113 struct acpi_attach_args *aa = aux; 114 const char *desc; 115 116 sc->sc_smpsw.smpsw_name = device_xname(self); 117 118 if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) { 119 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER; 120 desc = "Power"; 121 } else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) { 122 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP; 123 desc = "Sleep"; 124 } else 125 panic("%s: impossible", __func__); 126 127 aprint_naive(": ACPI %s Button\n", desc); 128 aprint_normal(": ACPI %s Button\n", desc); 129 130 sc->sc_node = aa->aa_node; 131 132 (void)pmf_device_register(self, NULL, NULL); 133 (void)sysmon_pswitch_register(&sc->sc_smpsw); 134 (void)acpi_register_notify(sc->sc_node, acpibut_notify_handler); 135 } 136 137 /* 138 * acpibut_detach: 139 * 140 * Autoconfiguration `detach' routine. 141 */ 142 static int 143 acpibut_detach(device_t self, int flags) 144 { 145 struct acpibut_softc *sc = device_private(self); 146 147 pmf_device_deregister(self); 148 acpi_deregister_notify(sc->sc_node); 149 sysmon_pswitch_unregister(&sc->sc_smpsw); 150 151 return 0; 152 } 153 154 /* 155 * acpibut_pressed_event: 156 * 157 * Deal with a button being pressed. 158 */ 159 static void 160 acpibut_pressed_event(void *arg) 161 { 162 device_t dv = arg; 163 struct acpibut_softc *sc = device_private(dv); 164 165 aprint_debug_dev(dv, "button pressed\n"); 166 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED); 167 } 168 169 /* 170 * acpibut_notify_handler: 171 * 172 * Callback from ACPI interrupt handler to notify us of an event. 173 */ 174 static void 175 acpibut_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context) 176 { 177 static const int handler = OSL_NOTIFY_HANDLER; 178 device_t dv = context; 179 180 switch (notify) { 181 182 case ACPI_NOTIFY_BUTTON: 183 (void)AcpiOsExecute(handler, acpibut_pressed_event, dv); 184 break; 185 186 case ACPI_NOTIFY_DEVICE_WAKE: 187 break; 188 189 default: 190 aprint_debug_dev(dv, "unknown notify 0x%02X\n", notify); 191 } 192 } 193 194 MODULE(MODULE_CLASS_DRIVER, acpibut, NULL); 195 196 #ifdef _MODULE 197 #include "ioconf.c" 198 #endif 199 200 static int 201 acpibut_modcmd(modcmd_t cmd, void *aux) 202 { 203 int rv = 0; 204 205 switch (cmd) { 206 207 case MODULE_CMD_INIT: 208 209 #ifdef _MODULE 210 rv = config_init_component(cfdriver_ioconf_acpibut, 211 cfattach_ioconf_acpibut, cfdata_ioconf_acpibut); 212 #endif 213 break; 214 215 case MODULE_CMD_FINI: 216 217 #ifdef _MODULE 218 rv = config_fini_component(cfdriver_ioconf_acpibut, 219 cfattach_ioconf_acpibut, cfdata_ioconf_acpibut); 220 #endif 221 break; 222 223 default: 224 rv = ENOTTY; 225 } 226 227 return rv; 228 } 229