acpi_apm.c revision 114977
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2001 Mitsuru IWASAKI 3219820Sjeff * All rights reserved. 4219820Sjeff * 5219820Sjeff * Redistribution and use in source and binary forms, with or without 6219820Sjeff * modification, are permitted provided that the following conditions 7219820Sjeff * are met: 8219820Sjeff * 1. Redistributions of source code must retain the above copyright 9219820Sjeff * notice, this list of conditions and the following disclaimer. 10219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 11219820Sjeff * notice, this list of conditions and the following disclaimer in the 12219820Sjeff * documentation and/or other materials provided with the distribution. 13219820Sjeff * 14219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17219820Sjeff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24219820Sjeff * SUCH DAMAGE. 25219820Sjeff * 26219820Sjeff * $FreeBSD: head/sys/i386/acpica/acpi_machdep.c 114977 2003-05-13 16:59:46Z jhb $ 27219820Sjeff */ 28219820Sjeff 29219820Sjeff#include <sys/param.h> 30219820Sjeff#include <sys/bus.h> 31219820Sjeff#include <sys/conf.h> 32219820Sjeff#include <sys/fcntl.h> 33219820Sjeff#include <sys/uio.h> 34219820Sjeff 35219820Sjeff#include "acpi.h" 36219820Sjeff 37219820Sjeff#include <dev/acpica/acpivar.h> 38219820Sjeff#include <dev/acpica/acpiio.h> 39219820Sjeff 40219820Sjeffstatic device_t acpi_dev; 41219820Sjeff 42219820Sjeff/* 43219820Sjeff * APM driver emulation 44219820Sjeff */ 45219820Sjeff 46219820Sjeff#if __FreeBSD_version < 500000 47219820Sjeff#include <sys/select.h> 48219820Sjeff#else 49219820Sjeff#include <sys/selinfo.h> 50219820Sjeff#endif 51219820Sjeff 52219820Sjeff#include <machine/apm_bios.h> 53219820Sjeff#include <machine/pc/bios.h> 54219820Sjeff 55219820Sjeff#if __FreeBSD_version < 500000 56219820Sjeff#include <i386/apm/apm.h> 57219820Sjeff#else 58219820Sjeff#include <i386/bios/apm.h> 59219820Sjeff#endif 60219820Sjeff 61219820Sjeffstatic struct apm_softc apm_softc; 62219820Sjeff 63219820Sjeffstatic d_open_t apmopen; 64219820Sjeffstatic d_close_t apmclose; 65219820Sjeffstatic d_write_t apmwrite; 66219820Sjeffstatic d_ioctl_t apmioctl; 67219820Sjeffstatic d_poll_t apmpoll; 68219820Sjeff 69219820Sjeff#define CDEV_MAJOR 39 70219820Sjeffstatic struct cdevsw apm_cdevsw = { 71219820Sjeff .d_open = apmopen, 72219820Sjeff .d_close = apmclose, 73219820Sjeff .d_write = apmwrite, 74219820Sjeff .d_ioctl = apmioctl, 75219820Sjeff .d_poll = apmpoll, 76219820Sjeff .d_name = "apm", 77219820Sjeff .d_maj = CDEV_MAJOR, 78219820Sjeff}; 79219820Sjeff 80219820Sjeffstatic int 81219820Sjeffacpi_capm_convert_battstate(struct acpi_battinfo *battp) 82219820Sjeff{ 83219820Sjeff int state; 84219820Sjeff 85219820Sjeff state = 0xff; /* XXX unknown */ 86219820Sjeff 87219820Sjeff if (battp->state & ACPI_BATT_STAT_DISCHARG) { 88219820Sjeff if (battp->cap >= 50) { 89219820Sjeff state = 0; /* high */ 90219820Sjeff } else { 91219820Sjeff state = 1; /* low */ 92219820Sjeff } 93219820Sjeff } 94219820Sjeff if (battp->state & ACPI_BATT_STAT_CRITICAL) { 95219820Sjeff state = 2; /* critical */ 96 } 97 if (battp->state & ACPI_BATT_STAT_CHARGING) { 98 state = 3; /* charging */ 99 } 100 return (state); 101} 102 103static int 104acpi_capm_convert_battflags(struct acpi_battinfo *battp) 105{ 106 int flags; 107 108 flags = 0; 109 110 if (battp->cap >= 50) { 111 flags |= APM_BATT_HIGH; 112 } else { 113 if (battp->state & ACPI_BATT_STAT_CRITICAL) { 114 flags |= APM_BATT_CRITICAL; 115 } else { 116 flags |= APM_BATT_LOW; 117 } 118 } 119 if (battp->state & ACPI_BATT_STAT_CHARGING) { 120 flags |= APM_BATT_CHARGING; 121 } 122 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT) { 123 flags = APM_BATT_NOT_PRESENT; 124 } 125 126 return (flags); 127} 128 129static int 130acpi_capm_get_info(apm_info_t aip) 131{ 132 int acline; 133 struct acpi_battinfo batt; 134 135 aip->ai_infoversion = 1; 136 aip->ai_major = 1; 137 aip->ai_minor = 2; 138 aip->ai_status = apm_softc.active; 139 aip->ai_capabilities= 0xff00; /* XXX unknown */ 140 141 if (acpi_acad_get_acline(&acline)) { 142 aip->ai_acline = 0xff; /* unknown */ 143 } else { 144 aip->ai_acline = acline; /* on/off */ 145 } 146 147 if (acpi_battery_get_battinfo(-1, &batt)) { 148 aip->ai_batt_stat = 0xff; /* unknown */ 149 aip->ai_batt_life = 0xff; /* unknown */ 150 aip->ai_batt_time = -1; /* unknown */ 151 aip->ai_batteries = 0; 152 } else { 153 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt); 154 aip->ai_batt_life = batt.cap; 155 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 156 aip->ai_batteries = acpi_battery_get_units(); 157 } 158 159 return (0); 160} 161 162static int 163acpi_capm_get_pwstatus(apm_pwstatus_t app) 164{ 165 int batt_unit; 166 int acline; 167 struct acpi_battinfo batt; 168 169 if (app->ap_device != PMDV_ALLDEV && 170 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) { 171 return (1); 172 } 173 174 if (app->ap_device == PMDV_ALLDEV) { 175 batt_unit = -1; /* all units */ 176 } else { 177 batt_unit = app->ap_device - PMDV_BATT0; 178 } 179 180 if (acpi_battery_get_battinfo(batt_unit, &batt)) { 181 return (1); 182 } 183 184 app->ap_batt_stat = acpi_capm_convert_battstate(&batt); 185 app->ap_batt_flag = acpi_capm_convert_battflags(&batt); 186 app->ap_batt_life = batt.cap; 187 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 188 189 if (acpi_acad_get_acline(&acline)) { 190 app->ap_acline = 0xff; /* unknown */ 191 } else { 192 app->ap_acline = acline; /* on/off */ 193 } 194 195 return (0); 196} 197 198static int 199apmopen(dev_t dev, int flag, int fmt, d_thread_t *td) 200{ 201 return (0); 202} 203 204static int 205apmclose(dev_t dev, int flag, int fmt, d_thread_t *td) 206{ 207 return (0); 208} 209 210static int 211apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 212{ 213 int error = 0; 214 struct acpi_softc *acpi_sc; 215 struct apm_info info; 216 apm_info_old_t aiop; 217 218 if ((acpi_sc = device_get_softc(acpi_dev)) == NULL) { 219 return (ENXIO); 220 } 221 222 switch (cmd) { 223 case APMIO_SUSPEND: 224 if (!(flag & FWRITE)) 225 return (EPERM); 226 if (apm_softc.active) 227 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_suspend_sx); 228 else 229 error = EINVAL; 230 break; 231 232 case APMIO_STANDBY: 233 if (!(flag & FWRITE)) 234 return (EPERM); 235 if (apm_softc.active) 236 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_standby_sx); 237 else 238 error = EINVAL; 239 break; 240 241 case APMIO_GETINFO_OLD: 242 if (acpi_capm_get_info(&info)) 243 error = ENXIO; 244 aiop = (apm_info_old_t)addr; 245 aiop->ai_major = info.ai_major; 246 aiop->ai_minor = info.ai_minor; 247 aiop->ai_acline = info.ai_acline; 248 aiop->ai_batt_stat = info.ai_batt_stat; 249 aiop->ai_batt_life = info.ai_batt_life; 250 aiop->ai_status = info.ai_status; 251 break; 252 253 case APMIO_GETINFO: 254 if (acpi_capm_get_info((apm_info_t)addr)) 255 error = ENXIO; 256 257 break; 258 259 case APMIO_GETPWSTATUS: 260 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr)) 261 error = ENXIO; 262 break; 263 264 case APMIO_ENABLE: 265 if (!(flag & FWRITE)) 266 return (EPERM); 267 apm_softc.active = 1; 268 break; 269 270 case APMIO_DISABLE: 271 if (!(flag & FWRITE)) 272 return (EPERM); 273 apm_softc.active = 0; 274 break; 275 276 case APMIO_HALTCPU: 277 break; 278 279 case APMIO_NOTHALTCPU: 280 break; 281 282 case APMIO_DISPLAY: 283 if (!(flag & FWRITE)) 284 return (EPERM); 285 break; 286 287 case APMIO_BIOS: 288 if (!(flag & FWRITE)) 289 return (EPERM); 290 bzero(addr, sizeof(struct apm_bios_arg)); 291 break; 292 293 default: 294 error = EINVAL; 295 break; 296 } 297 298 return (error); 299} 300 301static int 302apmwrite(dev_t dev, struct uio *uio, int ioflag) 303{ 304 305 return (uio->uio_resid); 306} 307 308static int 309apmpoll(dev_t dev, int events, d_thread_t *td) 310{ 311 return (0); 312} 313 314static void 315acpi_capm_init(struct acpi_softc *sc) 316{ 317 318 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm"); 319} 320 321int 322acpi_machdep_init(device_t dev) 323{ 324 struct acpi_softc *sc; 325 326 acpi_dev = dev; 327 if ((sc = device_get_softc(acpi_dev)) == NULL) { 328 return (ENXIO); 329 } 330 331 /* 332 * XXX: Prevent the PnP BIOS code from interfering with 333 * our own scan of ISA devices. 334 */ 335 PnPBIOStable = NULL; 336 337 acpi_capm_init(sc); 338 339 acpi_install_wakeup_handler(sc); 340 341#ifdef SMP 342 acpi_SetIntrModel(ACPI_INTR_APIC); 343#endif 344 return (0); 345} 346 347