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 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_acpi.h" 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/bus.h> 34 35 #include <machine/bus.h> 36 #include <sys/rman.h> 37 #include <sys/ioccom.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/conf.h> 41 #include <sys/power.h> 42 43 #include <contrib/dev/acpica/include/acpi.h> 44 45 #include <dev/acpica/acpivar.h> 46 #include <dev/acpica/acpiio.h> 47 #include <isa/isavar.h> 48 #include <isa/pnpvar.h> 49 50 /* Hooks for the ACPI CA debugging infrastructure */ 51 #define _COMPONENT ACPI_AC_ADAPTER 52 ACPI_MODULE_NAME("AC_ADAPTER") 53 54 /* Number of times to retry initialization before giving up. */ 55 #define ACPI_ACAD_RETRY_MAX 6 56 57 #define ACPI_POWERSOURCE_STAT_CHANGE 0x80 58 59 struct acpi_acad_softc { 60 int status; 61 }; 62 63 static void acpi_acad_get_status(void *); 64 static void acpi_acad_notify_handler(ACPI_HANDLE, UINT32, void *); 65 static int acpi_acad_probe(device_t); 66 static int acpi_acad_attach(device_t); 67 static int acpi_acad_ioctl(u_long, caddr_t, void *); 68 static int acpi_acad_sysctl(SYSCTL_HANDLER_ARGS); 69 static void acpi_acad_init_acline(void *arg); 70 static void acpi_acad_ac_only(void *arg); 71 72 static device_method_t acpi_acad_methods[] = { 73 /* Device interface */ 74 DEVMETHOD(device_probe, acpi_acad_probe), 75 DEVMETHOD(device_attach, acpi_acad_attach), 76 77 DEVMETHOD_END 78 }; 79 80 static driver_t acpi_acad_driver = { 81 "acpi_acad", 82 acpi_acad_methods, 83 sizeof(struct acpi_acad_softc), 84 }; 85 86 static devclass_t acpi_acad_devclass; 87 DRIVER_MODULE(acpi_acad, acpi, acpi_acad_driver, acpi_acad_devclass, 0, 0); 88 MODULE_DEPEND(acpi_acad, acpi, 1, 1, 1); 89 90 ACPI_SERIAL_DECL(acad, "ACPI AC adapter"); 91 92 SYSINIT(acad, SI_SUB_KTHREAD_IDLE, SI_ORDER_FIRST, acpi_acad_ac_only, NULL); 93 94 static void 95 acpi_acad_get_status(void *context) 96 { 97 struct acpi_acad_softc *sc; 98 device_t dev; 99 ACPI_HANDLE h; 100 int newstatus; 101 102 dev = context; 103 sc = device_get_softc(dev); 104 h = acpi_get_handle(dev); 105 newstatus = -1; 106 acpi_GetInteger(h, "_PSR", &newstatus); 107 108 /* If status is valid and has changed, notify the system. */ 109 ACPI_SERIAL_BEGIN(acad); 110 if (newstatus != -1 && sc->status != newstatus) { 111 sc->status = newstatus; 112 ACPI_SERIAL_END(acad); 113 power_profile_set_state(newstatus ? POWER_PROFILE_PERFORMANCE : 114 POWER_PROFILE_ECONOMY); 115 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 116 "%s Line\n", newstatus ? "On" : "Off"); 117 acpi_UserNotify("ACAD", h, newstatus); 118 } else 119 ACPI_SERIAL_END(acad); 120 } 121 122 static void 123 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 124 { 125 device_t dev; 126 127 dev = (device_t)context; 128 switch (notify) { 129 case ACPI_NOTIFY_BUS_CHECK: 130 case ACPI_NOTIFY_DEVICE_CHECK: 131 case ACPI_POWERSOURCE_STAT_CHANGE: 132 /* Temporarily. It is better to notify policy manager */ 133 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_acad_get_status, context); 134 break; 135 default: 136 device_printf(dev, "unknown notify %#x\n", notify); 137 break; 138 } 139 } 140 141 static int 142 acpi_acad_probe(device_t dev) 143 { 144 static char *acad_ids[] = { "ACPI0003", NULL }; 145 int rv; 146 147 if (acpi_disabled("acad")) 148 return (ENXIO); 149 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, acad_ids, NULL); 150 if (rv <= 0) 151 device_set_desc(dev, "AC Adapter"); 152 return (rv); 153 } 154 155 static int 156 acpi_acad_attach(device_t dev) 157 { 158 struct acpi_acad_softc *sc; 159 struct acpi_softc *acpi_sc; 160 ACPI_HANDLE handle; 161 int error; 162 163 sc = device_get_softc(dev); 164 handle = acpi_get_handle(dev); 165 166 error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev); 167 if (error != 0) 168 return (error); 169 170 if (device_get_unit(dev) == 0) { 171 acpi_sc = acpi_device_get_parent_softc(dev); 172 SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx, 173 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO, "acline", 174 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, &sc->status, 0, 175 acpi_acad_sysctl, "I", ""); 176 } 177 178 /* Get initial status after whole system is up. */ 179 sc->status = -1; 180 181 /* 182 * Install both system and device notify handlers since the Casio 183 * FIVA needs them. 184 */ 185 AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY, 186 acpi_acad_notify_handler, dev); 187 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_acad_init_acline, dev); 188 189 return (0); 190 } 191 192 static int 193 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg) 194 { 195 struct acpi_acad_softc *sc; 196 device_t dev; 197 198 dev = (device_t)arg; 199 sc = device_get_softc(dev); 200 201 /* 202 * No security check required: information retrieval only. If 203 * new functions are added here, a check might be required. 204 */ 205 switch (cmd) { 206 case ACPIIO_ACAD_GET_STATUS: 207 acpi_acad_get_status(dev); 208 *(int *)addr = sc->status; 209 break; 210 default: 211 break; 212 } 213 214 return (0); 215 } 216 217 static int 218 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS) 219 { 220 int val, error; 221 222 if (acpi_acad_get_acline(&val) != 0) 223 return (ENXIO); 224 225 val = *(u_int *)oidp->oid_arg1; 226 error = sysctl_handle_int(oidp, &val, 0, req); 227 return (error); 228 } 229 230 static void 231 acpi_acad_init_acline(void *arg) 232 { 233 struct acpi_acad_softc *sc; 234 device_t dev; 235 int retry; 236 237 dev = (device_t)arg; 238 sc = device_get_softc(dev); 239 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 240 "acline initialization start\n"); 241 242 for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) { 243 acpi_acad_get_status(dev); 244 if (sc->status != -1) 245 break; 246 AcpiOsSleep(10000); 247 } 248 249 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 250 "acline initialization done, tried %d times\n", retry + 1); 251 } 252 253 /* 254 * If no AC line devices detected after boot, create an "online" event 255 * so that userland code can adjust power settings accordingly. The default 256 * power profile is "performance" so we don't need to repeat that here. 257 */ 258 static void 259 acpi_acad_ac_only(void __unused *arg) 260 { 261 262 if (devclass_get_count(acpi_acad_devclass) == 0) 263 acpi_UserNotify("ACAD", ACPI_ROOT_OBJECT, 1); 264 } 265 266 /* 267 * Public interfaces. 268 */ 269 int 270 acpi_acad_get_acline(int *status) 271 { 272 struct acpi_acad_softc *sc; 273 device_t dev; 274 275 dev = devclass_get_device(acpi_acad_devclass, 0); 276 if (dev == NULL) 277 return (ENXIO); 278 sc = device_get_softc(dev); 279 280 acpi_acad_get_status(dev); 281 *status = sc->status; 282 283 return (0); 284 } 285