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$ 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 50 ACPI_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_DEVICE_CHECK_PNP 0x00 56 #define ACPI_DEVICE_CHECK_EXISTENCE 0x01 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 71 static device_method_t acpi_acad_methods[] = { 72 /* Device interface */ 73 DEVMETHOD(device_probe, acpi_acad_probe), 74 DEVMETHOD(device_attach, acpi_acad_attach), 75 76 {0, 0} 77 }; 78 79 static driver_t acpi_acad_driver = { 80 "acpi_acad", 81 acpi_acad_methods, 82 sizeof(struct acpi_acad_softc), 83 }; 84 85 static devclass_t acpi_acad_devclass; 86 DRIVER_MODULE(acpi_acad, acpi, acpi_acad_driver, acpi_acad_devclass, 0, 0); 87 MODULE_DEPEND(acpi_acad, acpi, 1, 1, 1); 88 89 ACPI_SERIAL_DECL(acad, "ACPI AC adapter"); 90 91 static void 92 acpi_acad_get_status(void *context) 93 { 94 struct acpi_acad_softc *sc; 95 device_t dev; 96 ACPI_HANDLE h; 97 int newstatus; 98 99 dev = context; 100 sc = device_get_softc(dev); 101 h = acpi_get_handle(dev); 102 newstatus = -1; 103 acpi_GetInteger(h, "_PSR", &newstatus); 104 105 /* If status is valid and has changed, notify the system. */ 106 ACPI_SERIAL_BEGIN(acad); 107 if (newstatus != -1 && sc->status != newstatus) { 108 sc->status = newstatus; 109 power_profile_set_state(newstatus ? POWER_PROFILE_PERFORMANCE : 110 POWER_PROFILE_ECONOMY); 111 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 112 "%s Line\n", newstatus ? "On" : "Off"); 113 acpi_UserNotify("ACAD", h, newstatus); 114 } 115 ACPI_SERIAL_END(acad); 116 } 117 118 static void 119 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 120 { 121 device_t dev; 122 123 dev = (device_t)context; 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 device_printf(dev, "unknown notify %#x\n", notify); 133 break; 134 } 135 } 136 137 static int 138 acpi_acad_probe(device_t dev) 139 { 140 static char *acad_ids[] = { "ACPI0003", NULL }; 141 142 if (acpi_disabled("acad") || 143 ACPI_ID_PROBE(device_get_parent(dev), dev, acad_ids) == NULL) 144 return (ENXIO); 145 146 device_set_desc(dev, "AC Adapter"); 147 return (0); 148 } 149 150 static int 151 acpi_acad_attach(device_t dev) 152 { 153 struct acpi_acad_softc *sc; 154 struct acpi_softc *acpi_sc; 155 ACPI_HANDLE handle; 156 int error; 157 158 sc = device_get_softc(dev); 159 handle = acpi_get_handle(dev); 160 161 error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev); 162 if (error != 0) 163 return (error); 164 165 if (device_get_unit(dev) == 0) { 166 acpi_sc = acpi_device_get_parent_softc(dev); 167 SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx, 168 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), 169 OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD, 170 &sc->status, 0, acpi_acad_sysctl, "I", ""); 171 } 172 173 /* Get initial status after whole system is up. */ 174 sc->status = -1; 175 176 /* 177 * Install both system and device notify handlers since the Casio 178 * FIVA needs them. 179 */ 180 AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY, 181 acpi_acad_notify_handler, dev); 182 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev); 183 184 return (0); 185 } 186 187 static int 188 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg) 189 { 190 struct acpi_acad_softc *sc; 191 device_t dev; 192 193 dev = (device_t)arg; 194 sc = device_get_softc(dev); 195 196 /* 197 * No security check required: information retrieval only. If 198 * new functions are added here, a check might be required. 199 */ 200 switch (cmd) { 201 case ACPIIO_ACAD_GET_STATUS: 202 acpi_acad_get_status(dev); 203 *(int *)addr = sc->status; 204 break; 205 default: 206 break; 207 } 208 209 return (0); 210 } 211 212 static int 213 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS) 214 { 215 int val, error; 216 217 if (acpi_acad_get_acline(&val) != 0) 218 return (ENXIO); 219 220 val = *(u_int *)oidp->oid_arg1; 221 error = sysctl_handle_int(oidp, &val, 0, req); 222 return (error); 223 } 224 225 static void 226 acpi_acad_init_acline(void *arg) 227 { 228 struct acpi_acad_softc *sc; 229 device_t dev; 230 int retry; 231 232 dev = (device_t)arg; 233 sc = device_get_softc(dev); 234 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 235 "acline initialization start\n"); 236 237 for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) { 238 acpi_acad_get_status(dev); 239 if (sc->status != -1) 240 break; 241 AcpiOsSleep(10000); 242 } 243 244 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 245 "acline initialization done, tried %d times\n", retry + 1); 246 } 247 248 /* 249 * Public interfaces. 250 */ 251 int 252 acpi_acad_get_acline(int *status) 253 { 254 struct acpi_acad_softc *sc; 255 device_t dev; 256 257 dev = devclass_get_device(acpi_acad_devclass, 0); 258 if (dev == NULL) 259 return (ENXIO); 260 sc = device_get_softc(dev); 261 262 acpi_acad_get_status(dev); 263 *status = sc->status; 264 265 return (0); 266 } 267