1 /* $NetBSD: acpi_lid.c,v 1.37 2010/03/05 21:01:44 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 Lid Switch driver. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: acpi_lid.c,v 1.37 2010/03/05 21:01:44 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_LID_COMPONENT 54 ACPI_MODULE_NAME ("acpi_lid") 55 56 struct acpilid_softc { 57 struct acpi_devnode *sc_node; 58 struct sysmon_pswitch sc_smpsw; 59 uint64_t sc_status; 60 }; 61 62 static const char * const lid_hid[] = { 63 "PNP0C0D", 64 NULL 65 }; 66 67 static int acpilid_match(device_t, cfdata_t, void *); 68 static void acpilid_attach(device_t, device_t, void *); 69 static int acpilid_detach(device_t, int); 70 static void acpilid_status_changed(void *); 71 static void acpilid_notify_handler(ACPI_HANDLE, UINT32, void *); 72 73 CFATTACH_DECL_NEW(acpilid, sizeof(struct acpilid_softc), 74 acpilid_match, acpilid_attach, acpilid_detach, NULL); 75 76 /* 77 * acpilid_match: 78 * 79 * Autoconfiguration `match' routine. 80 */ 81 static int 82 acpilid_match(device_t parent, cfdata_t match, void *aux) 83 { 84 struct acpi_attach_args *aa = aux; 85 86 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 87 return 0; 88 89 return acpi_match_hid(aa->aa_node->ad_devinfo, lid_hid); 90 } 91 92 /* 93 * acpilid_attach: 94 * 95 * Autoconfiguration `attach' routine. 96 */ 97 static void 98 acpilid_attach(device_t parent, device_t self, void *aux) 99 { 100 struct acpilid_softc *sc = device_private(self); 101 struct acpi_attach_args *aa = aux; 102 ACPI_STATUS rv; 103 104 aprint_naive(": ACPI Lid Switch\n"); 105 aprint_normal(": ACPI Lid Switch\n"); 106 107 sc->sc_node = aa->aa_node; 108 109 sc->sc_smpsw.smpsw_name = device_xname(self); 110 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_LID; 111 112 (void)pmf_device_register(self, NULL, NULL); 113 (void)sysmon_pswitch_register(&sc->sc_smpsw); 114 115 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, 116 ACPI_DEVICE_NOTIFY, acpilid_notify_handler, self); 117 118 if (ACPI_FAILURE(rv)) 119 aprint_error_dev(self, "failed to register notify handler\n"); 120 } 121 122 static int 123 acpilid_detach(device_t self, int flags) 124 { 125 struct acpilid_softc *sc = device_private(self); 126 ACPI_STATUS rv; 127 128 rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle, 129 ACPI_DEVICE_NOTIFY, acpilid_notify_handler); 130 131 if (ACPI_FAILURE(rv)) 132 return EBUSY; 133 134 pmf_device_deregister(self); 135 sysmon_pswitch_unregister(&sc->sc_smpsw); 136 137 return 0; 138 } 139 140 /* 141 * acpilid_status_changed: 142 * 143 * Get, and possibly display, the lid status, and take action. 144 */ 145 static void 146 acpilid_status_changed(void *arg) 147 { 148 device_t dv = arg; 149 struct acpilid_softc *sc = device_private(dv); 150 ACPI_STATUS rv; 151 152 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &sc->sc_status); 153 154 if (ACPI_FAILURE(rv)) 155 return; 156 157 sysmon_pswitch_event(&sc->sc_smpsw, (sc->sc_status == 0) ? 158 PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED); 159 } 160 161 /* 162 * acpilid_notify_handler: 163 * 164 * Callback from ACPI interrupt handler to notify us of an event. 165 */ 166 static void 167 acpilid_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context) 168 { 169 static const int handler = OSL_NOTIFY_HANDLER; 170 device_t dv = context; 171 172 switch (notify) { 173 174 case ACPI_NOTIFY_LidStatusChanged: 175 (void)AcpiOsExecute(handler, acpilid_status_changed, dv); 176 break; 177 178 default: 179 aprint_error_dev(dv, "unknown notify 0x%02X\n", notify); 180 } 181 } 182 183 #ifdef _MODULE 184 185 MODULE(MODULE_CLASS_DRIVER, acpilid, NULL); 186 CFDRIVER_DECL(acpilid, DV_DULL, NULL); 187 188 static int acpilidloc[] = { -1 }; 189 extern struct cfattach acpilid_ca; 190 191 static struct cfparent acpiparent = { 192 "acpinodebus", NULL, DVUNIT_ANY 193 }; 194 195 static struct cfdata acpilid_cfdata[] = { 196 { 197 .cf_name = "acpilid", 198 .cf_atname = "acpilid", 199 .cf_unit = 0, 200 .cf_fstate = FSTATE_STAR, 201 .cf_loc = acpilidloc, 202 .cf_flags = 0, 203 .cf_pspec = &acpiparent, 204 }, 205 206 { NULL } 207 }; 208 209 static int 210 acpilid_modcmd(modcmd_t cmd, void *context) 211 { 212 int err; 213 214 switch (cmd) { 215 216 case MODULE_CMD_INIT: 217 218 err = config_cfdriver_attach(&acpilid_cd); 219 220 if (err != 0) 221 return err; 222 223 err = config_cfattach_attach("acpilid", &acpilid_ca); 224 225 if (err != 0) { 226 config_cfdriver_detach(&acpilid_cd); 227 return err; 228 } 229 230 err = config_cfdata_attach(acpilid_cfdata, 1); 231 232 if (err != 0) { 233 config_cfattach_detach("acpilid", &acpilid_ca); 234 config_cfdriver_detach(&acpilid_cd); 235 return err; 236 } 237 238 return 0; 239 240 case MODULE_CMD_FINI: 241 242 err = config_cfdata_detach(acpilid_cfdata); 243 244 if (err != 0) 245 return err; 246 247 config_cfattach_detach("acpilid", &acpilid_ca); 248 config_cfdriver_detach(&acpilid_cd); 249 250 return 0; 251 252 default: 253 return ENOTTY; 254 } 255 } 256 257 #endif /* _MODULE */ 258