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 <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 /* 47 * Hooks for the ACPI CA debugging infrastructure 48 */ 49 #define _COMPONENT ACPI_AC_ADAPTER 50 ACPI_MODULE_NAME("AC_ADAPTER") 51 52 #define ACPI_DEVICE_CHECK_PNP 0x00 53 #define ACPI_DEVICE_CHECK_EXISTENCE 0x01 54 #define ACPI_POWERSOURCE_STAT_CHANGE 0x80 55 56 static void acpi_acad_get_status(void * ); 57 static void acpi_acad_notify_handler(ACPI_HANDLE , UINT32 ,void *); 58 static int acpi_acad_probe(device_t); 59 static int acpi_acad_attach(device_t); 60 static int acpi_acad_ioctl(u_long, caddr_t, void *); 61 static int acpi_acad_sysctl(SYSCTL_HANDLER_ARGS); 62 static void acpi_acad_init_acline(void *arg); 63 64 struct acpi_acad_softc { 65 int status; 66 67 int initializing; 68 }; 69 70 static void 71 acpi_acad_get_status(void *context) 72 { 73 int newstatus; 74 device_t dev = context; 75 struct acpi_acad_softc *sc = device_get_softc(dev); 76 ACPI_HANDLE h = acpi_get_handle(dev); 77 78 if (ACPI_FAILURE(acpi_EvaluateInteger(h, "_PSR", &newstatus))) { 79 sc->status = -1; 80 return; 81 } 82 83 if (sc->status != newstatus) { 84 sc->status = newstatus; 85 /* set system power profile based on AC adapter status */ 86 power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY); 87 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 88 "%s Line\n",(sc->status) ? "On" : "Off"); 89 } 90 } 91 92 static void 93 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 94 { 95 device_t dev = context; 96 97 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 98 "Notify %d\n", notify); 99 100 switch (notify) { 101 case ACPI_DEVICE_CHECK_PNP: 102 case ACPI_DEVICE_CHECK_EXISTENCE: 103 case ACPI_POWERSOURCE_STAT_CHANGE: 104 /*Temporally. It is better to notify policy manager*/ 105 AcpiOsQueueForExecution(OSD_PRIORITY_LO, 106 acpi_acad_get_status,context); 107 break; 108 default: 109 break; 110 } 111 } 112 113 static int 114 acpi_acad_probe(device_t dev) 115 { 116 117 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) && 118 acpi_MatchHid(dev, "ACPI0003")) { 119 120 /* 121 * Set device description 122 */ 123 device_set_desc(dev, "AC adapter"); 124 return(0); 125 } 126 return(ENXIO); 127 } 128 129 static int 130 acpi_acad_attach(device_t dev) 131 { 132 int error; 133 struct acpi_acad_softc *sc; 134 struct acpi_softc *acpi_sc; 135 136 ACPI_HANDLE handle = acpi_get_handle(dev); 137 AcpiInstallNotifyHandler(handle, 138 ACPI_DEVICE_NOTIFY, 139 acpi_acad_notify_handler, dev); 140 /*Installing system notify is not so good*/ 141 AcpiInstallNotifyHandler(handle, 142 ACPI_SYSTEM_NOTIFY, 143 acpi_acad_notify_handler, dev); 144 145 if ((sc = device_get_softc(dev)) == NULL) { 146 return (ENXIO); 147 } 148 if ((error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, 149 acpi_acad_ioctl, dev)) != 0) { 150 return (error); 151 } 152 153 if (device_get_unit(dev) == 0) { 154 acpi_sc = acpi_device_get_parent_softc(dev); 155 SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx, 156 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), 157 OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD, 158 &sc->status, 0, acpi_acad_sysctl, "I", ""); 159 } 160 161 /* Get initial status after whole system is up. */ 162 sc->status = -1; 163 sc->initializing = 0; 164 165 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev); 166 167 return(0); 168 } 169 170 static device_method_t acpi_acad_methods[] = { 171 /* Device interface */ 172 DEVMETHOD(device_probe, acpi_acad_probe), 173 DEVMETHOD(device_attach, acpi_acad_attach), 174 175 {0, 0} 176 }; 177 178 static driver_t acpi_acad_driver = { 179 "acpi_acad", 180 acpi_acad_methods, 181 sizeof(struct acpi_acad_softc), 182 }; 183 184 static devclass_t acpi_acad_devclass; 185 DRIVER_MODULE(acpi_acad,acpi,acpi_acad_driver,acpi_acad_devclass,0,0); 186 187 static int 188 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg) 189 { 190 device_t dev; 191 struct acpi_acad_softc *sc; 192 193 dev = (device_t)arg; 194 if ((sc = device_get_softc(dev)) == NULL) { 195 return(ENXIO); 196 } 197 198 /* 199 * No security check required: information retrieval only. If 200 * new functions are added here, a check might be required. 201 */ 202 203 switch (cmd) { 204 case ACPIIO_ACAD_GET_STATUS: 205 acpi_acad_get_status(dev); 206 *(int *)addr = sc->status; 207 break; 208 } 209 210 return(0); 211 } 212 213 static int 214 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS) 215 { 216 int val; 217 int error; 218 219 if (acpi_acad_get_acline(&val)) { 220 return (ENXIO); 221 } 222 223 val = *(u_int *)oidp->oid_arg1; 224 error = sysctl_handle_int(oidp, &val, 0, req); 225 return (error); 226 } 227 228 static void 229 acpi_acad_init_acline(void *arg) 230 { 231 int retry; 232 int status; 233 device_t dev = (device_t)arg; 234 struct acpi_acad_softc *sc = device_get_softc(dev); 235 #define ACPI_ACAD_RETRY_MAX 6 236 237 if (sc->initializing) { 238 return; 239 } 240 241 sc->initializing = 1; 242 243 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 244 "acline initialization start\n"); 245 246 status = 0; 247 for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++, AcpiOsSleep(10, 0)) { 248 acpi_acad_get_status(dev); 249 if (status != sc->status) 250 break; 251 } 252 253 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 254 "acline initialization done, tried %d times\n", retry+1); 255 256 sc->initializing = 0; 257 } 258 259 /* 260 * Public interfaces. 261 */ 262 263 int 264 acpi_acad_get_acline(int *status) 265 { 266 device_t dev; 267 struct acpi_acad_softc *sc; 268 269 if ((dev = devclass_get_device(acpi_acad_devclass, 0)) == NULL) { 270 return (ENXIO); 271 } 272 273 if ((sc = device_get_softc(dev)) == NULL) { 274 return (ENXIO); 275 } 276 277 acpi_acad_get_status(dev); 278 *status = sc->status; 279 280 return (0); 281 } 282 283