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 63 struct acpi_acad_softc { 64 int status; 65 }; 66 67 static void 68 acpi_acad_get_status(void *context) 69 { 70 int newstatus; 71 device_t dev = context; 72 struct acpi_acad_softc *sc = device_get_softc(dev); 73 ACPI_HANDLE h = acpi_get_handle(dev); 74 75 if (ACPI_FAILURE(acpi_EvaluateInteger(h, "_PSR", &newstatus))) { 76 sc->status = -1; 77 return; 78 } 79 80 if (sc->status != newstatus) { 81 sc->status = newstatus; 82 /* set system power profile based on AC adapter status */ 83 power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY); 84 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 85 "%s Line\n",(sc->status) ? "On" : "Off"); 86 } 87 } 88 89 static void 90 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 91 { 92 device_t dev = context; 93 94 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 95 "Notify %d\n", notify); 96 97 switch (notify) { 98 case ACPI_DEVICE_CHECK_PNP: 99 case ACPI_DEVICE_CHECK_EXISTENCE: 100 case ACPI_POWERSOURCE_STAT_CHANGE: 101 /*Temporally. It is better to notify policy manager*/ 102 AcpiOsQueueForExecution(OSD_PRIORITY_LO, 103 acpi_acad_get_status,context); 104 break; 105 default: 106 break; 107 } 108 } 109 110 static int 111 acpi_acad_probe(device_t dev) 112 { 113 114 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) && 115 acpi_MatchHid(dev, "ACPI0003")) { 116 117 /* 118 * Set device description 119 */ 120 device_set_desc(dev, "AC adapter"); 121 return(0); 122 } 123 return(ENXIO); 124 } 125 126 static int 127 acpi_acad_attach(device_t dev) 128 { 129 int error; 130 struct acpi_acad_softc *sc; 131 struct acpi_softc *acpi_sc; 132 133 ACPI_HANDLE handle = acpi_get_handle(dev); 134 AcpiInstallNotifyHandler(handle, 135 ACPI_DEVICE_NOTIFY, 136 acpi_acad_notify_handler, dev); 137 /*Installing system notify is not so good*/ 138 AcpiInstallNotifyHandler(handle, 139 ACPI_SYSTEM_NOTIFY, 140 acpi_acad_notify_handler, dev); 141 142 if ((sc = device_get_softc(dev)) == NULL) { 143 return (ENXIO); 144 } 145 if ((error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, 146 acpi_acad_ioctl, dev)) != 0) { 147 return (error); 148 } 149 150 if (device_get_unit(dev) == 0) { 151 acpi_sc = acpi_device_get_parent_softc(dev); 152 SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx, 153 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), 154 OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD, 155 &sc->status, 0, acpi_acad_sysctl, "I", ""); 156 } 157 158 /* Get initial status after whole system is up. */ 159 sc->status = -1; 160 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_get_status, dev); 161 162 return(0); 163 } 164 165 static device_method_t acpi_acad_methods[] = { 166 /* Device interface */ 167 DEVMETHOD(device_probe, acpi_acad_probe), 168 DEVMETHOD(device_attach, acpi_acad_attach), 169 170 {0, 0} 171 }; 172 173 static driver_t acpi_acad_driver = { 174 "acpi_acad", 175 acpi_acad_methods, 176 sizeof(struct acpi_acad_softc), 177 }; 178 179 static devclass_t acpi_acad_devclass; 180 DRIVER_MODULE(acpi_acad,acpi,acpi_acad_driver,acpi_acad_devclass,0,0); 181 182 static int 183 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg) 184 { 185 device_t dev; 186 struct acpi_acad_softc *sc; 187 188 dev = (device_t)arg; 189 if ((sc = device_get_softc(dev)) == NULL) { 190 return(ENXIO); 191 } 192 193 switch (cmd) { 194 case ACPIIO_ACAD_GET_STATUS: 195 acpi_acad_get_status(dev); 196 *(int *)addr = sc->status; 197 break; 198 } 199 200 return(0); 201 } 202 203 static int 204 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS) 205 { 206 int val; 207 int error; 208 209 if (acpi_acad_get_acline(&val)) { 210 return (ENXIO); 211 } 212 213 val = *(u_int *)oidp->oid_arg1; 214 error = sysctl_handle_int(oidp, &val, 0, req); 215 return (error); 216 } 217 218 /* 219 * Public interfaces. 220 */ 221 222 int 223 acpi_acad_get_acline(int *status) 224 { 225 device_t dev; 226 struct acpi_acad_softc *sc; 227 228 if ((dev = devclass_get_device(acpi_acad_devclass, 0)) == NULL) { 229 return (ENXIO); 230 } 231 232 if ((sc = device_get_softc(dev)) == NULL) { 233 return (ENXIO); 234 } 235 236 acpi_acad_get_status(dev); 237 *status = sc->status; 238 239 return (0); 240 } 241 242