acpi_acad.c revision 146373
1/*- 2 * Copyright (c) 2000 Takanori Watanabe 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/dev/acpica/acpi_acad.c 146373 2005-05-19 09:13:26Z takawata $ 27 */ 28 29#include "opt_acpi.h" 30#include <sys/param.h> 31#include <sys/kernel.h> 32#include <sys/bus.h> 33 34#include <machine/bus.h> 35#include <sys/rman.h> 36#include <sys/ioccom.h> 37#include <sys/malloc.h> 38#include <sys/module.h> 39#include <sys/conf.h> 40#include <sys/power.h> 41 42#include "acpi.h" 43#include <dev/acpica/acpivar.h> 44#include <dev/acpica/acpiio.h> 45#include <isa/isavar.h> 46#include <isa/pnpvar.h> 47 48/* Hooks for the ACPI CA debugging infrastructure */ 49#define _COMPONENT ACPI_AC_ADAPTER 50ACPI_MODULE_NAME("AC_ADAPTER") 51 52/* Number of times to retry initialization before giving up. */ 53#define ACPI_ACAD_RETRY_MAX 6 54 55#define ACPI_POWERSOURCE_STAT_CHANGE 0x80 56 57struct acpi_acad_softc { 58 int status; 59}; 60 61static void acpi_acad_get_status(void *); 62static void acpi_acad_notify_handler(ACPI_HANDLE, UINT32, void *); 63static int acpi_acad_probe(device_t); 64static int acpi_acad_attach(device_t); 65static int acpi_acad_ioctl(u_long, caddr_t, void *); 66static int acpi_acad_sysctl(SYSCTL_HANDLER_ARGS); 67static void acpi_acad_init_acline(void *arg); 68 69static device_method_t acpi_acad_methods[] = { 70 /* Device interface */ 71 DEVMETHOD(device_probe, acpi_acad_probe), 72 DEVMETHOD(device_attach, acpi_acad_attach), 73 74 {0, 0} 75}; 76 77static driver_t acpi_acad_driver = { 78 "acpi_acad", 79 acpi_acad_methods, 80 sizeof(struct acpi_acad_softc), 81}; 82 83static devclass_t acpi_acad_devclass; 84DRIVER_MODULE(acpi_acad, acpi, acpi_acad_driver, acpi_acad_devclass, 0, 0); 85MODULE_DEPEND(acpi_acad, acpi, 1, 1, 1); 86 87ACPI_SERIAL_DECL(acad, "ACPI AC adapter"); 88 89static void 90acpi_acad_get_status(void *context) 91{ 92 struct acpi_acad_softc *sc; 93 device_t dev; 94 ACPI_HANDLE h; 95 int newstatus; 96 97 dev = context; 98 sc = device_get_softc(dev); 99 h = acpi_get_handle(dev); 100 newstatus = -1; 101 acpi_GetInteger(h, "_PSR", &newstatus); 102 103 /* If status is valid and has changed, notify the system. */ 104 ACPI_SERIAL_BEGIN(acad); 105 if (newstatus != -1 && sc->status != newstatus) { 106 sc->status = newstatus; 107 power_profile_set_state(newstatus ? POWER_PROFILE_PERFORMANCE : 108 POWER_PROFILE_ECONOMY); 109 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 110 "%s Line\n", newstatus ? "On" : "Off"); 111 acpi_UserNotify("ACAD", h, newstatus); 112 } 113 ACPI_SERIAL_END(acad); 114} 115 116static void 117acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 118{ 119 device_t dev; 120 121 dev = (device_t)context; 122 switch (notify) { 123 case ACPI_NOTIFY_BUS_CHECK: 124 case ACPI_NOTIFY_DEVICE_CHECK: 125 case ACPI_POWERSOURCE_STAT_CHANGE: 126 /* Temporarily. It is better to notify policy manager */ 127 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_get_status, context); 128 break; 129 default: 130 device_printf(dev, "unknown notify %#x\n", notify); 131 break; 132 } 133} 134 135static int 136acpi_acad_probe(device_t dev) 137{ 138 static char *acad_ids[] = { "ACPI0003", NULL }; 139 140 if (acpi_disabled("acad") || 141 ACPI_ID_PROBE(device_get_parent(dev), dev, acad_ids) == NULL) 142 return (ENXIO); 143 144 device_set_desc(dev, "AC Adapter"); 145 return (0); 146} 147 148static int 149acpi_acad_attach(device_t dev) 150{ 151 struct acpi_acad_softc *sc; 152 struct acpi_softc *acpi_sc; 153 ACPI_HANDLE handle; 154 int error; 155 156 sc = device_get_softc(dev); 157 handle = acpi_get_handle(dev); 158 159 error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev); 160 if (error != 0) 161 return (error); 162 163 if (device_get_unit(dev) == 0) { 164 acpi_sc = acpi_device_get_parent_softc(dev); 165 SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx, 166 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), 167 OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD, 168 &sc->status, 0, acpi_acad_sysctl, "I", ""); 169 } 170 171 /* Get initial status after whole system is up. */ 172 sc->status = -1; 173 174 /* 175 * Install both system and device notify handlers since the Casio 176 * FIVA needs them. 177 */ 178 AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY, 179 acpi_acad_notify_handler, dev); 180 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev); 181 182 return (0); 183} 184 185static int 186acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg) 187{ 188 struct acpi_acad_softc *sc; 189 device_t dev; 190 191 dev = (device_t)arg; 192 sc = device_get_softc(dev); 193 194 /* 195 * No security check required: information retrieval only. If 196 * new functions are added here, a check might be required. 197 */ 198 switch (cmd) { 199 case ACPIIO_ACAD_GET_STATUS: 200 acpi_acad_get_status(dev); 201 *(int *)addr = sc->status; 202 break; 203 default: 204 break; 205 } 206 207 return (0); 208} 209 210static int 211acpi_acad_sysctl(SYSCTL_HANDLER_ARGS) 212{ 213 int val, error; 214 215 if (acpi_acad_get_acline(&val) != 0) 216 return (ENXIO); 217 218 val = *(u_int *)oidp->oid_arg1; 219 error = sysctl_handle_int(oidp, &val, 0, req); 220 return (error); 221} 222 223static void 224acpi_acad_init_acline(void *arg) 225{ 226 struct acpi_acad_softc *sc; 227 device_t dev; 228 int retry; 229 230 dev = (device_t)arg; 231 sc = device_get_softc(dev); 232 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 233 "acline initialization start\n"); 234 235 for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) { 236 acpi_acad_get_status(dev); 237 if (sc->status != -1) 238 break; 239 AcpiOsSleep(10000); 240 } 241 242 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 243 "acline initialization done, tried %d times\n", retry + 1); 244} 245 246/* 247 * Public interfaces. 248 */ 249int 250acpi_acad_get_acline(int *status) 251{ 252 struct acpi_acad_softc *sc; 253 device_t dev; 254 255 dev = devclass_get_device(acpi_acad_devclass, 0); 256 if (dev == NULL) 257 return (ENXIO); 258 sc = device_get_softc(dev); 259 260 acpi_acad_get_status(dev); 261 *status = sc->status; 262 263 return (0); 264} 265