1 /* $NetBSD: acpi_acad.c,v 1.48 2011/01/04 05:48:48 jruoho Exp $ */ 2 3 /* 4 * Copyright 2001 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 AC Adapter driver. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.48 2011/01/04 05:48:48 jruoho Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/device.h> 47 #include <sys/module.h> 48 #include <sys/mutex.h> 49 #include <sys/systm.h> 50 51 #include <dev/acpi/acpireg.h> 52 #include <dev/acpi/acpivar.h> 53 54 #define _COMPONENT ACPI_ACAD_COMPONENT 55 ACPI_MODULE_NAME ("acpi_acad") 56 57 #define ACPI_NOTIFY_ACAD 0x80 58 #define ACPI_NOTIFY_ACAD_2 0x81 /* XXX. */ 59 60 struct acpiacad_softc { 61 struct acpi_devnode *sc_node; 62 struct sysmon_envsys *sc_sme; 63 struct sysmon_pswitch sc_smpsw; 64 envsys_data_t sc_sensor; 65 kmutex_t sc_mutex; 66 int sc_status; 67 }; 68 69 static const char * const acad_hid[] = { 70 "ACPI0003", 71 NULL 72 }; 73 74 static int acpiacad_match(device_t, cfdata_t, void *); 75 static void acpiacad_attach(device_t, device_t, void *); 76 static int acpiacad_detach(device_t, int); 77 static bool acpiacad_resume(device_t, const pmf_qual_t *); 78 static void acpiacad_get_status(void *); 79 static void acpiacad_notify_handler(ACPI_HANDLE, uint32_t, void *); 80 static void acpiacad_init_envsys(device_t); 81 82 CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc), 83 acpiacad_match, acpiacad_attach, acpiacad_detach, NULL); 84 85 /* 86 * acpiacad_match: 87 * 88 * Autoconfiguration `match' routine. 89 */ 90 static int 91 acpiacad_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 return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid); 99 } 100 101 /* 102 * acpiacad_attach: 103 * 104 * Autoconfiguration `attach' routine. 105 */ 106 static void 107 acpiacad_attach(device_t parent, device_t self, void *aux) 108 { 109 struct acpiacad_softc *sc = device_private(self); 110 struct acpi_attach_args *aa = aux; 111 112 aprint_naive(": ACPI AC Adapter\n"); 113 aprint_normal(": ACPI AC Adapter\n"); 114 115 sc->sc_sme = NULL; 116 sc->sc_status = -1; 117 sc->sc_node = aa->aa_node; 118 119 acpiacad_init_envsys(self); 120 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE); 121 122 sc->sc_smpsw.smpsw_name = device_xname(self); 123 sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER; 124 125 (void)sysmon_pswitch_register(&sc->sc_smpsw); 126 (void)pmf_device_register(self, NULL, acpiacad_resume); 127 (void)acpi_register_notify(sc->sc_node, acpiacad_notify_handler); 128 } 129 130 /* 131 * acpiacad_detach: 132 * 133 * Autoconfiguration `detach' routine. 134 */ 135 static int 136 acpiacad_detach(device_t self, int flags) 137 { 138 struct acpiacad_softc *sc = device_private(self); 139 140 acpi_deregister_notify(sc->sc_node); 141 142 mutex_destroy(&sc->sc_mutex); 143 144 if (sc->sc_sme != NULL) 145 sysmon_envsys_unregister(sc->sc_sme); 146 147 pmf_device_deregister(self); 148 sysmon_pswitch_unregister(&sc->sc_smpsw); 149 150 return 0; 151 } 152 153 /* 154 * acpiacad_resume: 155 * 156 * Queue a new status check. 157 */ 158 static bool 159 acpiacad_resume(device_t dv, const pmf_qual_t *qual) 160 { 161 162 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv); 163 164 return true; 165 } 166 167 /* 168 * acpiacad_get_status: 169 * 170 * Get, and possibly display, the current AC line status. 171 */ 172 static void 173 acpiacad_get_status(void *arg) 174 { 175 device_t dv = arg; 176 struct acpiacad_softc *sc = device_private(dv); 177 ACPI_INTEGER status; 178 ACPI_STATUS rv; 179 180 mutex_enter(&sc->sc_mutex); 181 182 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status); 183 184 if (ACPI_FAILURE(rv)) 185 goto fail; 186 187 if (status != 0 && status != 1) { 188 rv = AE_BAD_VALUE; 189 goto fail; 190 } 191 192 if (sc->sc_status != (int)status) { 193 194 /* 195 * If status has changed, send the event: 196 * 197 * PSWITCH_EVENT_PRESSED : _PSR = 1 : AC online. 198 * PSWITCH_EVENT_RELEASED : _PSR = 0 : AC offline. 199 */ 200 sysmon_pswitch_event(&sc->sc_smpsw, (status != 0) ? 201 PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED); 202 203 aprint_debug_dev(dv, "AC adapter %sconnected\n", 204 status == 0 ? "not " : ""); 205 } 206 207 sc->sc_status = status; 208 sc->sc_sensor.state = ENVSYS_SVALID; 209 sc->sc_sensor.value_cur = sc->sc_status; 210 211 mutex_exit(&sc->sc_mutex); 212 213 return; 214 215 fail: 216 sc->sc_status = -1; 217 sc->sc_sensor.state = ENVSYS_SINVALID; 218 219 aprint_debug_dev(dv, "failed to evaluate _PSR: %s\n", 220 AcpiFormatException(rv)); 221 222 mutex_exit(&sc->sc_mutex); 223 } 224 225 /* 226 * acpiacad_notify_handler: 227 * 228 * Callback from ACPI interrupt handler to notify us of an event. 229 */ 230 static void 231 acpiacad_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context) 232 { 233 static const int handler = OSL_NOTIFY_HANDLER; 234 device_t dv = context; 235 236 switch (notify) { 237 /* 238 * XXX So, BusCheck is not exactly what I would expect, 239 * but at least my IBM T21 sends it on AC adapter status 240 * change. --thorpej@wasabisystems.com 241 */ 242 /* 243 * XXX My Acer TravelMate 291 sends DeviceCheck on AC 244 * adapter status change. 245 * --rpaulo@NetBSD.org 246 */ 247 /* 248 * XXX Sony VAIO VGN-N250E sends 0x81 on AC adapter status change. 249 * --jmcneill@NetBSD.org 250 */ 251 case ACPI_NOTIFY_ACAD: 252 case ACPI_NOTIFY_ACAD_2: 253 case ACPI_NOTIFY_BUS_CHECK: 254 case ACPI_NOTIFY_DEVICE_CHECK: 255 (void)AcpiOsExecute(handler, acpiacad_get_status, dv); 256 break; 257 258 case ACPI_NOTIFY_DEVICE_WAKE: 259 break; 260 261 default: 262 aprint_debug_dev(dv, "unknown notify 0x%02X\n", notify); 263 } 264 } 265 266 static void 267 acpiacad_init_envsys(device_t dv) 268 { 269 struct acpiacad_softc *sc = device_private(dv); 270 271 sc->sc_sme = sysmon_envsys_create(); 272 273 sc->sc_sensor.state = ENVSYS_SINVALID; 274 sc->sc_sensor.units = ENVSYS_INDICATOR; 275 276 (void)strlcpy(sc->sc_sensor.desc, "connected", ENVSYS_DESCLEN); 277 278 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor) != 0) 279 goto fail; 280 281 sc->sc_sme->sme_name = device_xname(dv); 282 sc->sc_sme->sme_class = SME_CLASS_ACADAPTER; 283 sc->sc_sme->sme_flags = SME_DISABLE_REFRESH; 284 285 if (sysmon_envsys_register(sc->sc_sme) != 0) 286 goto fail; 287 288 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv); 289 290 return; 291 292 fail: 293 aprint_error_dev(dv, "failed to initialize sysmon\n"); 294 sysmon_envsys_destroy(sc->sc_sme); 295 sc->sc_sme = NULL; 296 } 297 298 #ifdef _MODULE 299 300 MODULE(MODULE_CLASS_DRIVER, acpiacad, NULL); 301 CFDRIVER_DECL(acpiacad, DV_DULL, NULL); 302 303 static int acpiacadloc[] = { -1 }; 304 extern struct cfattach acpiacad_ca; 305 306 static struct cfparent acpiparent = { 307 "acpinodebus", NULL, DVUNIT_ANY 308 }; 309 310 static struct cfdata acpiacad_cfdata[] = { 311 { 312 .cf_name = "acpiacad", 313 .cf_atname = "acpiacad", 314 .cf_unit = 0, 315 .cf_fstate = FSTATE_STAR, 316 .cf_loc = acpiacadloc, 317 .cf_flags = 0, 318 .cf_pspec = &acpiparent, 319 }, 320 321 { NULL, NULL, 0, 0, NULL, 0, NULL } 322 }; 323 324 static int 325 acpiacad_modcmd(modcmd_t cmd, void *context) 326 { 327 int err; 328 329 switch (cmd) { 330 331 case MODULE_CMD_INIT: 332 333 err = config_cfdriver_attach(&acpiacad_cd); 334 335 if (err != 0) 336 return err; 337 338 err = config_cfattach_attach("acpiacad", &acpiacad_ca); 339 340 if (err != 0) { 341 config_cfdriver_detach(&acpiacad_cd); 342 return err; 343 } 344 345 err = config_cfdata_attach(acpiacad_cfdata, 1); 346 347 if (err != 0) { 348 config_cfattach_detach("acpiacad", &acpiacad_ca); 349 config_cfdriver_detach(&acpiacad_cd); 350 return err; 351 } 352 353 return 0; 354 355 case MODULE_CMD_FINI: 356 357 err = config_cfdata_detach(acpiacad_cfdata); 358 359 if (err != 0) 360 return err; 361 362 config_cfattach_detach("acpiacad", &acpiacad_ca); 363 config_cfdriver_detach(&acpiacad_cd); 364 365 return 0; 366 367 default: 368 return ENOTTY; 369 } 370 } 371 372 #endif /* _MODULE */ 373