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