acpi_acad.c revision 128990
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 128990 2004-05-06 02:18:58Z njl $ 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/conf.h> 39#include <sys/power.h> 40 41#include "acpi.h" 42#include <dev/acpica/acpivar.h> 43#include <dev/acpica/acpiio.h> 44 45/* Hooks for the ACPI CA debugging infrastructure */ 46#define _COMPONENT ACPI_AC_ADAPTER 47ACPI_MODULE_NAME("AC_ADAPTER") 48 49/* Number of times to retry initialization before giving up. */ 50#define ACPI_ACAD_RETRY_MAX 6 51 52#define ACPI_DEVICE_CHECK_PNP 0x00 53#define ACPI_DEVICE_CHECK_EXISTENCE 0x01 54#define ACPI_POWERSOURCE_STAT_CHANGE 0x80 55 56struct acpi_acad_softc { 57 int status; 58 int initializing; 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 87static void 88acpi_acad_get_status(void *context) 89{ 90 struct acpi_acad_softc *sc; 91 device_t dev; 92 ACPI_HANDLE h; 93 int newstatus; 94 95 dev = context; 96 sc = device_get_softc(dev); 97 h = acpi_get_handle(dev); 98 if (ACPI_FAILURE(acpi_GetInteger(h, "_PSR", &newstatus))) { 99 sc->status = -1; 100 return; 101 } 102 103 if (sc->status != newstatus) { 104 sc->status = newstatus; 105 106 /* Set system power profile based on AC adapter status */ 107 power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE : 108 POWER_PROFILE_ECONOMY); 109 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 110 "%s Line\n", sc->status ? "On" : "Off"); 111 112 acpi_UserNotify("ACAD", h, sc->status); 113 } 114} 115 116static void 117acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 118{ 119 device_t dev = context; 120 121 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 122 "Notify 0x%x\n", notify); 123 124 switch (notify) { 125 case ACPI_DEVICE_CHECK_PNP: 126 case ACPI_DEVICE_CHECK_EXISTENCE: 127 case ACPI_POWERSOURCE_STAT_CHANGE: 128 /* Temporarily. It is better to notify policy manager */ 129 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_get_status, context); 130 break; 131 default: 132 break; 133 } 134} 135 136static int 137acpi_acad_probe(device_t dev) 138{ 139 if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("acad") && 140 acpi_MatchHid(dev, "ACPI0003")) { 141 device_set_desc(dev, "AC Adapter"); 142 return (0); 143 } 144 return (ENXIO); 145} 146 147static int 148acpi_acad_attach(device_t dev) 149{ 150 struct acpi_acad_softc *sc; 151 struct acpi_softc *acpi_sc; 152 ACPI_HANDLE handle; 153 int error; 154 155 sc = device_get_softc(dev); 156 if (sc == NULL) 157 return (ENXIO); 158 handle = acpi_get_handle(dev); 159 160 error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev); 161 if (error != 0) 162 return (error); 163 164 if (device_get_unit(dev) == 0) { 165 acpi_sc = acpi_device_get_parent_softc(dev); 166 SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx, 167 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), 168 OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD, 169 &sc->status, 0, acpi_acad_sysctl, "I", ""); 170 } 171 172 /* Get initial status after whole system is up. */ 173 sc->status = -1; 174 sc->initializing = 0; 175 176 /* 177 * Also install a system notify handler even though this is not 178 * required by the specification. The Casio FIVA needs this. 179 */ 180 AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY, 181 acpi_acad_notify_handler, dev); 182 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY, 183 acpi_acad_notify_handler, dev); 184 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev); 185 186 return (0); 187} 188 189static int 190acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg) 191{ 192 struct acpi_acad_softc *sc; 193 device_t dev; 194 195 dev = (device_t)arg; 196 sc = device_get_softc(dev); 197 if (sc == NULL) 198 return (ENXIO); 199 200 /* 201 * No security check required: information retrieval only. If 202 * new functions are added here, a check might be required. 203 */ 204 switch (cmd) { 205 case ACPIIO_ACAD_GET_STATUS: 206 acpi_acad_get_status(dev); 207 *(int *)addr = sc->status; 208 break; 209 default: 210 break; 211 } 212 213 return (0); 214} 215 216static int 217acpi_acad_sysctl(SYSCTL_HANDLER_ARGS) 218{ 219 int val, error; 220 221 if (acpi_acad_get_acline(&val) != 0) 222 return (ENXIO); 223 224 val = *(u_int *)oidp->oid_arg1; 225 error = sysctl_handle_int(oidp, &val, 0, req); 226 return (error); 227} 228 229static void 230acpi_acad_init_acline(void *arg) 231{ 232 struct acpi_acad_softc *sc; 233 device_t dev; 234 int retry, status; 235 236 dev = (device_t)arg; 237 sc = device_get_softc(dev); 238 if (sc->initializing) 239 return; 240 241 sc->initializing = 1; 242 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 243 "acline initialization start\n"); 244 245 status = 0; 246 for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) { 247 acpi_acad_get_status(dev); 248 if (status != sc->status) 249 break; 250 AcpiOsSleep(10, 0); 251 } 252 253 sc->initializing = 0; 254 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 255 "acline initialization done, tried %d times\n", retry + 1); 256} 257 258/* 259 * Public interfaces. 260 */ 261int 262acpi_acad_get_acline(int *status) 263{ 264 struct acpi_acad_softc *sc; 265 device_t dev; 266 267 dev = devclass_get_device(acpi_acad_devclass, 0); 268 if (dev == NULL) 269 return (ENXIO); 270 sc = device_get_softc(dev); 271 if (sc == NULL) 272 return (ENXIO); 273 274 acpi_acad_get_status(dev); 275 *status = sc->status; 276 277 return (0); 278} 279