1 /* $NetBSD: acpi_button.c,v 1.21 2006/10/12 01:30:54 christos 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.21 2006/10/12 01:30:54 christos 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 acpibut_softc { 56 struct device sc_dev; /* base device glue */ 57 struct acpi_devnode *sc_node; /* our ACPI devnode */ 58 struct sysmon_pswitch sc_smpsw; /* our sysmon glue */ 59 int sc_flags; /* see below */ 60 }; 61 62 static const char * const power_button_hid[] = { 63 "PNP0C0C", 64 NULL 65 }; 66 67 static const char * const sleep_button_hid[] = { 68 "PNP0C0E", 69 NULL 70 }; 71 72 #define ACPIBUT_F_VERBOSE 0x01 /* verbose events */ 73 74 static int acpibut_match(struct device *, struct cfdata *, void *); 75 static void acpibut_attach(struct device *, struct device *, void *); 76 77 CFATTACH_DECL(acpibut, sizeof(struct acpibut_softc), 78 acpibut_match, acpibut_attach, NULL, NULL); 79 80 static void acpibut_pressed_event(void *); 81 static void acpibut_notify_handler(ACPI_HANDLE, UINT32, void *); 82 83 /* 84 * acpibut_match: 85 * 86 * Autoconfiguration `match' routine. 87 */ 88 static int 89 acpibut_match(struct device *parent __unused, struct cfdata *match __unused, 90 void *aux) 91 { 92 struct acpi_attach_args *aa = aux; 93 94 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 95 return 0; 96 97 if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) 98 return 1; 99 100 if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) 101 return 1; 102 103 return 0; 104 } 105 106 /* 107 * acpibut_attach: 108 * 109 * Autoconfiguration `attach' routine. 110 */ 111 static void 112 acpibut_attach(struct device *parent __unused, struct device *self, void *aux) 113 { 114 struct acpibut_softc *sc = (void *) self; 115 struct acpi_attach_args *aa = aux; 116 const char *desc; 117 ACPI_STATUS rv; 118 119 sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname; 120 121 if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) { 122 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER; 123 desc = "Power"; 124 } else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) { 125 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP; 126 desc = "Sleep"; 127 } else { 128 printf("\n"); 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("%s: unable to register with sysmon\n", 139 sc->sc_dev.dv_xname); 140 return; 141 } 142 143 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, 144 ACPI_DEVICE_NOTIFY, acpibut_notify_handler, sc); 145 if (ACPI_FAILURE(rv)) { 146 aprint_error("%s: unable to register DEVICE NOTIFY handler: %s\n", 147 sc->sc_dev.dv_xname, AcpiFormatException(rv)); 148 return; 149 } 150 151 acpi_set_wake_gpe(sc->sc_node->ad_handle); 152 153 #ifdef ACPI_BUT_DEBUG 154 /* Display the current state when it changes. */ 155 sc->sc_flags = ACPIBUT_F_VERBOSE; 156 #endif 157 } 158 159 /* 160 * acpibut_pressed_event: 161 * 162 * Deal with a button being pressed. 163 */ 164 static void 165 acpibut_pressed_event(void *arg) 166 { 167 struct acpibut_softc *sc = arg; 168 169 if (sc->sc_flags & ACPIBUT_F_VERBOSE) 170 printf("%s: button pressed\n", sc->sc_dev.dv_xname); 171 172 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED); 173 } 174 175 /* 176 * acpibut_notify_handler: 177 * 178 * Callback from ACPI interrupt handler to notify us of an event. 179 */ 180 static void 181 acpibut_notify_handler(ACPI_HANDLE handle __unused, UINT32 notify, 182 void *context) 183 { 184 struct acpibut_softc *sc = context; 185 int rv; 186 187 switch (notify) { 188 case ACPI_NOTIFY_S0PowerButtonPressed: 189 #if 0 190 case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */ 191 #endif 192 #ifdef ACPI_BUT_DEBUG 193 printf("%s: received ButtonPressed message\n", 194 sc->sc_dev.dv_xname); 195 #endif 196 rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO, 197 acpibut_pressed_event, sc); 198 if (ACPI_FAILURE(rv)) 199 printf("%s: WARNING: unable to queue button pressed " 200 "callback: %s\n", sc->sc_dev.dv_xname, 201 AcpiFormatException(rv)); 202 break; 203 204 /* XXX ACPI_NOTIFY_DeviceWake?? */ 205 206 default: 207 printf("%s: received unknown notify message: 0x%x\n", 208 sc->sc_dev.dv_xname, notify); 209 } 210 } 211