acpi_acad.c revision 118925
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 118925 2003-08-15 02:17:23Z 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 <machine/resource.h> 36#include <sys/rman.h> 37#include <sys/ioccom.h> 38#include <sys/malloc.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 46/* Hooks for the ACPI CA debugging infrastructure */ 47#define _COMPONENT ACPI_AC_ADAPTER 48ACPI_MODULE_NAME("AC_ADAPTER") 49 50/* Number of times to retry initialization before giving up. */ 51#define ACPI_ACAD_RETRY_MAX 6 52 53#define ACPI_DEVICE_CHECK_PNP 0x00 54#define ACPI_DEVICE_CHECK_EXISTENCE 0x01 55#define ACPI_POWERSOURCE_STAT_CHANGE 0x80 56 57struct acpi_acad_softc { 58 int status; 59 int initializing; 60}; 61 62static void acpi_acad_get_status(void *); 63static void acpi_acad_notify_handler(ACPI_HANDLE, UINT32, void *); 64static int acpi_acad_probe(device_t); 65static int acpi_acad_attach(device_t); 66static int acpi_acad_ioctl(u_long, caddr_t, void *); 67static int acpi_acad_sysctl(SYSCTL_HANDLER_ARGS); 68static void acpi_acad_init_acline(void *arg); 69 70static device_method_t acpi_acad_methods[] = { 71 /* Device interface */ 72 DEVMETHOD(device_probe, acpi_acad_probe), 73 DEVMETHOD(device_attach, acpi_acad_attach), 74 75 {0, 0} 76}; 77 78static driver_t acpi_acad_driver = { 79 "acpi_acad", 80 acpi_acad_methods, 81 sizeof(struct acpi_acad_softc), 82}; 83 84static devclass_t acpi_acad_devclass; 85DRIVER_MODULE(acpi_acad, acpi, acpi_acad_driver, acpi_acad_devclass, 0, 0); 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_EvaluateInteger(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} 113 114static void 115acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 116{ 117 device_t dev = context; 118 119 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 120 "Notify %d\n", notify); 121 122 switch (notify) { 123 case ACPI_DEVICE_CHECK_PNP: 124 case ACPI_DEVICE_CHECK_EXISTENCE: 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 break; 131 } 132} 133 134static int 135acpi_acad_probe(device_t dev) 136{ 137 if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && 138 acpi_MatchHid(dev, "ACPI0003")) { 139 140 device_set_desc(dev, "AC adapter"); 141 return (0); 142 } 143 return (ENXIO); 144} 145 146static int 147acpi_acad_attach(device_t dev) 148{ 149 struct acpi_acad_softc *sc; 150 struct acpi_softc *acpi_sc; 151 ACPI_HANDLE handle; 152 int error; 153 154 sc = device_get_softc(dev); 155 if (sc == NULL) 156 return (ENXIO); 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 sc->initializing = 0; 174 175 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY, 176 acpi_acad_notify_handler, dev); 177 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev); 178 179 return (0); 180} 181 182static int 183acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg) 184{ 185 struct acpi_acad_softc *sc; 186 device_t dev; 187 188 dev = (device_t)arg; 189 sc = device_get_softc(dev); 190 if (sc == NULL) 191 return (ENXIO); 192 193 /* 194 * No security check required: information retrieval only. If 195 * new functions are added here, a check might be required. 196 */ 197 switch (cmd) { 198 case ACPIIO_ACAD_GET_STATUS: 199 acpi_acad_get_status(dev); 200 *(int *)addr = sc->status; 201 break; 202 default: 203 break; 204 } 205 206 return (0); 207} 208 209static int 210acpi_acad_sysctl(SYSCTL_HANDLER_ARGS) 211{ 212 int val, error; 213 214 if (acpi_acad_get_acline(&val) != 0) 215 return (ENXIO); 216 217 val = *(u_int *)oidp->oid_arg1; 218 error = sysctl_handle_int(oidp, &val, 0, req); 219 return (error); 220} 221 222static void 223acpi_acad_init_acline(void *arg) 224{ 225 struct acpi_acad_softc *sc; 226 device_t dev; 227 int retry, status; 228 229 dev = (device_t)arg; 230 sc = device_get_softc(dev); 231 if (sc->initializing) 232 return; 233 234 sc->initializing = 1; 235 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 236 "acline initialization start\n"); 237 238 status = 0; 239 for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) { 240 acpi_acad_get_status(dev); 241 if (status != sc->status) 242 break; 243 AcpiOsSleep(10, 0); 244 } 245 246 sc->initializing = 0; 247 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 248 "acline initialization done, tried %d times\n", retry + 1); 249} 250 251/* 252 * Public interfaces. 253 */ 254int 255acpi_acad_get_acline(int *status) 256{ 257 struct acpi_acad_softc *sc; 258 device_t dev; 259 260 dev = devclass_get_device(acpi_acad_devclass, 0); 261 if (dev == NULL) 262 return (ENXIO); 263 sc = device_get_softc(dev); 264 if (sc == NULL) 265 return (ENXIO); 266 267 acpi_acad_get_status(dev); 268 *status = sc->status; 269 270 return (0); 271} 272