acpi_apm.c revision 119530
1/*- 2 * Copyright (c) 2001 Mitsuru IWASAKI 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: head/sys/i386/acpica/acpi_machdep.c 119530 2003-08-28 16:26:24Z njl $"); 29 30#include <sys/param.h> 31#include <sys/bus.h> 32#include <sys/conf.h> 33#include <sys/fcntl.h> 34#include <sys/uio.h> 35 36#include "acpi.h" 37 38#include <dev/acpica/acpivar.h> 39#include <dev/acpica/acpiio.h> 40 41static device_t acpi_dev; 42 43/* 44 * APM driver emulation 45 */ 46 47#if __FreeBSD_version < 500000 48#include <sys/select.h> 49#else 50#include <sys/selinfo.h> 51#endif 52 53#include <machine/apm_bios.h> 54#include <machine/pc/bios.h> 55 56#if __FreeBSD_version < 500000 57#include <i386/apm/apm.h> 58#else 59#include <i386/bios/apm.h> 60#endif 61 62static struct apm_softc apm_softc; 63 64static d_open_t apmopen; 65static d_close_t apmclose; 66static d_write_t apmwrite; 67static d_ioctl_t apmioctl; 68static d_poll_t apmpoll; 69 70#define CDEV_MAJOR 39 71static struct cdevsw apm_cdevsw = { 72 .d_open = apmopen, 73 .d_close = apmclose, 74 .d_write = apmwrite, 75 .d_ioctl = apmioctl, 76 .d_poll = apmpoll, 77 .d_name = "apm", 78 .d_maj = CDEV_MAJOR, 79}; 80 81static int 82acpi_capm_convert_battstate(struct acpi_battinfo *battp) 83{ 84 int state; 85 86 state = 0xff; /* XXX unknown */ 87 88 if (battp->state & ACPI_BATT_STAT_DISCHARG) { 89 if (battp->cap >= 50) 90 state = 0; /* high */ 91 else 92 state = 1; /* low */ 93 } 94 if (battp->state & ACPI_BATT_STAT_CRITICAL) 95 state = 2; /* critical */ 96 if (battp->state & ACPI_BATT_STAT_CHARGING) 97 state = 3; /* charging */ 98 99 return (state); 100} 101 102static int 103acpi_capm_convert_battflags(struct acpi_battinfo *battp) 104{ 105 int flags; 106 107 flags = 0; 108 109 if (battp->cap >= 50) { 110 flags |= APM_BATT_HIGH; 111 } else { 112 if (battp->state & ACPI_BATT_STAT_CRITICAL) 113 flags |= APM_BATT_CRITICAL; 114 else 115 flags |= APM_BATT_LOW; 116 } 117 if (battp->state & ACPI_BATT_STAT_CHARGING) 118 flags |= APM_BATT_CHARGING; 119 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT) 120 flags = APM_BATT_NOT_PRESENT; 121 122 return (flags); 123} 124 125static int 126acpi_capm_get_info(apm_info_t aip) 127{ 128 int acline; 129 struct acpi_battinfo batt; 130 131 aip->ai_infoversion = 1; 132 aip->ai_major = 1; 133 aip->ai_minor = 2; 134 aip->ai_status = apm_softc.active; 135 aip->ai_capabilities= 0xff00; /* XXX unknown */ 136 137 if (acpi_acad_get_acline(&acline)) 138 aip->ai_acline = 0xff; /* unknown */ 139 else 140 aip->ai_acline = acline; /* on/off */ 141 142 if (acpi_battery_get_battinfo(-1, &batt)) { 143 aip->ai_batt_stat = 0xff; /* unknown */ 144 aip->ai_batt_life = 0xff; /* unknown */ 145 aip->ai_batt_time = -1; /* unknown */ 146 aip->ai_batteries = 0; 147 } else { 148 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt); 149 aip->ai_batt_life = batt.cap; 150 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 151 aip->ai_batteries = acpi_battery_get_units(); 152 } 153 154 return (0); 155} 156 157static int 158acpi_capm_get_pwstatus(apm_pwstatus_t app) 159{ 160 int batt_unit; 161 int acline; 162 struct acpi_battinfo batt; 163 164 if (app->ap_device != PMDV_ALLDEV && 165 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) { 166 return (1); 167 } 168 169 if (app->ap_device == PMDV_ALLDEV) 170 batt_unit = -1; /* all units */ 171 else 172 batt_unit = app->ap_device - PMDV_BATT0; 173 174 if (acpi_battery_get_battinfo(batt_unit, &batt)) 175 return (1); 176 177 app->ap_batt_stat = acpi_capm_convert_battstate(&batt); 178 app->ap_batt_flag = acpi_capm_convert_battflags(&batt); 179 app->ap_batt_life = batt.cap; 180 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 181 182 if (acpi_acad_get_acline(&acline)) 183 app->ap_acline = 0xff; /* unknown */ 184 else 185 app->ap_acline = acline; /* on/off */ 186 187 return (0); 188} 189 190static int 191apmopen(dev_t dev, int flag, int fmt, d_thread_t *td) 192{ 193 return (0); 194} 195 196static int 197apmclose(dev_t dev, int flag, int fmt, d_thread_t *td) 198{ 199 return (0); 200} 201 202static int 203apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 204{ 205 int error = 0; 206 struct acpi_softc *acpi_sc; 207 struct apm_info info; 208 apm_info_old_t aiop; 209 210 if ((acpi_sc = device_get_softc(acpi_dev)) == NULL) 211 return (ENXIO); 212 213 switch (cmd) { 214 case APMIO_SUSPEND: 215 if ((flag & FWRITE) == 0) 216 return (EPERM); 217 if (apm_softc.active) 218 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_suspend_sx); 219 else 220 error = EINVAL; 221 break; 222 case APMIO_STANDBY: 223 if ((flag & FWRITE) == 0) 224 return (EPERM); 225 if (apm_softc.active) 226 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_standby_sx); 227 else 228 error = EINVAL; 229 break; 230 case APMIO_GETINFO_OLD: 231 if (acpi_capm_get_info(&info)) 232 error = ENXIO; 233 aiop = (apm_info_old_t)addr; 234 aiop->ai_major = info.ai_major; 235 aiop->ai_minor = info.ai_minor; 236 aiop->ai_acline = info.ai_acline; 237 aiop->ai_batt_stat = info.ai_batt_stat; 238 aiop->ai_batt_life = info.ai_batt_life; 239 aiop->ai_status = info.ai_status; 240 break; 241 case APMIO_GETINFO: 242 if (acpi_capm_get_info((apm_info_t)addr)) 243 error = ENXIO; 244 break; 245 case APMIO_GETPWSTATUS: 246 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr)) 247 error = ENXIO; 248 break; 249 case APMIO_ENABLE: 250 if ((flag & FWRITE) == 0) 251 return (EPERM); 252 apm_softc.active = 1; 253 break; 254 case APMIO_DISABLE: 255 if ((flag & FWRITE) == 0) 256 return (EPERM); 257 apm_softc.active = 0; 258 break; 259 case APMIO_HALTCPU: 260 break; 261 case APMIO_NOTHALTCPU: 262 break; 263 case APMIO_DISPLAY: 264 if ((flag & FWRITE) == 0) 265 return (EPERM); 266 break; 267 case APMIO_BIOS: 268 if ((flag & FWRITE) == 0) 269 return (EPERM); 270 bzero(addr, sizeof(struct apm_bios_arg)); 271 break; 272 default: 273 error = EINVAL; 274 break; 275 } 276 277 return (error); 278} 279 280static int 281apmwrite(dev_t dev, struct uio *uio, int ioflag) 282{ 283 return (uio->uio_resid); 284} 285 286static int 287apmpoll(dev_t dev, int events, d_thread_t *td) 288{ 289 return (0); 290} 291 292static void 293acpi_capm_init(struct acpi_softc *sc) 294{ 295 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm"); 296} 297 298int 299acpi_machdep_init(device_t dev) 300{ 301 struct acpi_softc *sc; 302 303 acpi_dev = dev; 304 if ((sc = device_get_softc(acpi_dev)) == NULL) 305 return (ENXIO); 306 307 /* 308 * XXX: Prevent the PnP BIOS code from interfering with 309 * our own scan of ISA devices. 310 */ 311 PnPBIOStable = NULL; 312 313 acpi_capm_init(sc); 314 315 acpi_install_wakeup_handler(sc); 316 317#ifdef SMP 318 acpi_SetIntrModel(ACPI_INTR_APIC); 319#endif 320 return (0); 321} 322