acpi_apm.c revision 131218
1262639Sdelphij/*- 2262639Sdelphij * Copyright (c) 2001 Mitsuru IWASAKI 3262639Sdelphij * All rights reserved. 4262639Sdelphij * 5262639Sdelphij * Redistribution and use in source and binary forms, with or without 6262639Sdelphij * modification, are permitted provided that the following conditions 7262639Sdelphij * are met: 8262639Sdelphij * 1. Redistributions of source code must retain the above copyright 9262639Sdelphij * notice, this list of conditions and the following disclaimer. 10262639Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 11262639Sdelphij * notice, this list of conditions and the following disclaimer in the 12262639Sdelphij * documentation and/or other materials provided with the distribution. 13262639Sdelphij * 14262639Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15262639Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16262639Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17262639Sdelphij * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18262639Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19262639Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20262639Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21262639Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22262639Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23262639Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24262639Sdelphij * SUCH DAMAGE. 25262639Sdelphij */ 26262639Sdelphij 27262639Sdelphij#include <sys/cdefs.h> 28262639Sdelphij__FBSDID("$FreeBSD: head/sys/i386/acpica/acpi_machdep.c 131218 2004-06-28 02:38:20Z imp $"); 29262639Sdelphij 30262639Sdelphij#include <sys/param.h> 31262639Sdelphij#include <sys/bus.h> 32262639Sdelphij#include <sys/conf.h> 33262639Sdelphij#include <sys/fcntl.h> 34262639Sdelphij#include <sys/kernel.h> 35262639Sdelphij#include <sys/sysctl.h> 36262639Sdelphij#include <sys/uio.h> 37262639Sdelphij 38262639Sdelphij#include "acpi.h" 39262639Sdelphij#include <dev/acpica/acpivar.h> 40262639Sdelphij#include <dev/acpica/acpiio.h> 41262639Sdelphij 42262639Sdelphijstatic device_t acpi_dev; 43262639Sdelphij 44262639Sdelphij/* 45262639Sdelphij * APM driver emulation 46262639Sdelphij */ 47262639Sdelphij 48262639Sdelphij#include <sys/selinfo.h> 49262639Sdelphij 50262639Sdelphij#include <machine/apm_bios.h> 51262639Sdelphij#include <machine/pc/bios.h> 52262639Sdelphij 53262639Sdelphij#include <i386/bios/apm.h> 54262639Sdelphij 55262639Sdelphijuint32_t acpi_reset_video = 1; 56262639SdelphijTUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video); 57262639Sdelphij 58262639Sdelphijstatic int intr_model = ACPI_INTR_PIC; 59262639Sdelphijstatic struct apm_softc apm_softc; 60262639Sdelphij 61262639Sdelphijstatic d_open_t apmopen; 62262639Sdelphijstatic d_close_t apmclose; 63262639Sdelphijstatic d_write_t apmwrite; 64262639Sdelphijstatic d_ioctl_t apmioctl; 65262639Sdelphijstatic d_poll_t apmpoll; 66262639Sdelphij 67262639Sdelphijstatic struct cdevsw apm_cdevsw = { 68262639Sdelphij .d_version = D_VERSION, 69262639Sdelphij .d_flags = D_NEEDGIANT, 70262639Sdelphij .d_open = apmopen, 71262639Sdelphij .d_close = apmclose, 72262639Sdelphij .d_write = apmwrite, 73262639Sdelphij .d_ioctl = apmioctl, 74262639Sdelphij .d_poll = apmpoll, 75262639Sdelphij .d_name = "apm", 76262639Sdelphij}; 77262639Sdelphij 78262639Sdelphijstatic int 79262639Sdelphijacpi_capm_convert_battstate(struct acpi_battinfo *battp) 80262639Sdelphij{ 81262639Sdelphij int state; 82262639Sdelphij 83262639Sdelphij state = APM_UNKNOWN; 84262639Sdelphij 85262639Sdelphij if (battp->state & ACPI_BATT_STAT_DISCHARG) { 86262639Sdelphij if (battp->cap >= 50) 87262639Sdelphij state = 0; /* high */ 88262639Sdelphij else 89262639Sdelphij state = 1; /* low */ 90262639Sdelphij } 91262639Sdelphij if (battp->state & ACPI_BATT_STAT_CRITICAL) 92262639Sdelphij state = 2; /* critical */ 93262639Sdelphij if (battp->state & ACPI_BATT_STAT_CHARGING) 94262639Sdelphij state = 3; /* charging */ 95262639Sdelphij 96262639Sdelphij /* If still unknown, determine it based on the battery capacity. */ 97262639Sdelphij if (state == APM_UNKNOWN) { 98262639Sdelphij if (battp->cap >= 50) 99262639Sdelphij state = 0; /* high */ 100262639Sdelphij else 101262639Sdelphij state = 1; /* low */ 102262639Sdelphij } 103262639Sdelphij 104262639Sdelphij return (state); 105262639Sdelphij} 106262639Sdelphij 107262639Sdelphijstatic int 108262639Sdelphijacpi_capm_convert_battflags(struct acpi_battinfo *battp) 109262639Sdelphij{ 110262639Sdelphij int flags; 111262639Sdelphij 112262639Sdelphij flags = 0; 113262639Sdelphij 114262639Sdelphij if (battp->cap >= 50) 115262639Sdelphij flags |= APM_BATT_HIGH; 116262639Sdelphij else { 117262639Sdelphij if (battp->state & ACPI_BATT_STAT_CRITICAL) 118262639Sdelphij flags |= APM_BATT_CRITICAL; 119262639Sdelphij else 120262639Sdelphij flags |= APM_BATT_LOW; 121262639Sdelphij } 122262639Sdelphij if (battp->state & ACPI_BATT_STAT_CHARGING) 123262639Sdelphij flags |= APM_BATT_CHARGING; 124262639Sdelphij if (battp->state == ACPI_BATT_STAT_NOT_PRESENT) 125262639Sdelphij flags = APM_BATT_NOT_PRESENT; 126262639Sdelphij 127262639Sdelphij return (flags); 128262639Sdelphij} 129262639Sdelphij 130262639Sdelphijstatic int 131262639Sdelphijacpi_capm_get_info(apm_info_t aip) 132262639Sdelphij{ 133262639Sdelphij int acline; 134262639Sdelphij struct acpi_battinfo batt; 135262639Sdelphij 136262639Sdelphij aip->ai_infoversion = 1; 137262639Sdelphij aip->ai_major = 1; 138262639Sdelphij aip->ai_minor = 2; 139262639Sdelphij aip->ai_status = apm_softc.active; 140262639Sdelphij aip->ai_capabilities= 0xff00; /* unknown */ 141262639Sdelphij 142262639Sdelphij if (acpi_acad_get_acline(&acline)) 143262639Sdelphij aip->ai_acline = APM_UNKNOWN; /* unknown */ 144262639Sdelphij else 145262639Sdelphij aip->ai_acline = acline; /* on/off */ 146262639Sdelphij 147262639Sdelphij if (acpi_battery_get_battinfo(-1, &batt)) { 148262639Sdelphij aip->ai_batt_stat = APM_UNKNOWN; 149262639Sdelphij aip->ai_batt_life = APM_UNKNOWN; 150262639Sdelphij aip->ai_batt_time = -1; /* unknown */ 151262639Sdelphij aip->ai_batteries = ~0U; /* unknown */ 152262639Sdelphij } else { 153262639Sdelphij aip->ai_batt_stat = acpi_capm_convert_battstate(&batt); 154262639Sdelphij aip->ai_batt_life = batt.cap; 155262639Sdelphij aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 156262639Sdelphij aip->ai_batteries = acpi_battery_get_units(); 157262639Sdelphij } 158262639Sdelphij 159262639Sdelphij return (0); 160262639Sdelphij} 161262639Sdelphij 162262639Sdelphijstatic int 163262639Sdelphijacpi_capm_get_pwstatus(apm_pwstatus_t app) 164262639Sdelphij{ 165262639Sdelphij int batt_unit; 166262639Sdelphij int acline; 167262639Sdelphij struct acpi_battinfo batt; 168262639Sdelphij 169262639Sdelphij if (app->ap_device != PMDV_ALLDEV && 170262639Sdelphij (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) 171262639Sdelphij return (1); 172262639Sdelphij 173262639Sdelphij if (app->ap_device == PMDV_ALLDEV) 174262639Sdelphij batt_unit = -1; /* all units */ 175262639Sdelphij else 176262639Sdelphij batt_unit = app->ap_device - PMDV_BATT0; 177262639Sdelphij 178262639Sdelphij if (acpi_battery_get_battinfo(batt_unit, &batt)) 179262639Sdelphij return (1); 180262639Sdelphij 181262639Sdelphij app->ap_batt_stat = acpi_capm_convert_battstate(&batt); 182262639Sdelphij app->ap_batt_flag = acpi_capm_convert_battflags(&batt); 183262639Sdelphij app->ap_batt_life = batt.cap; 184262639Sdelphij app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 185262639Sdelphij 186262639Sdelphij if (acpi_acad_get_acline(&acline)) 187262639Sdelphij app->ap_acline = APM_UNKNOWN; 188262639Sdelphij else 189262639Sdelphij app->ap_acline = acline; /* on/off */ 190262639Sdelphij 191262639Sdelphij return (0); 192262639Sdelphij} 193262639Sdelphij 194262639Sdelphijstatic int 195262639Sdelphijapmopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 196262639Sdelphij{ 197262639Sdelphij return (0); 198262639Sdelphij} 199262639Sdelphij 200262639Sdelphijstatic int 201262639Sdelphijapmclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 202262639Sdelphij{ 203262639Sdelphij return (0); 204262639Sdelphij} 205262639Sdelphij 206262639Sdelphijstatic int 207262639Sdelphijapmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 208262639Sdelphij{ 209262639Sdelphij int error = 0; 210262639Sdelphij struct acpi_softc *acpi_sc; 211262639Sdelphij struct apm_info info; 212262639Sdelphij apm_info_old_t aiop; 213 214 acpi_sc = device_get_softc(acpi_dev); 215 216 switch (cmd) { 217 case APMIO_SUSPEND: 218 if ((flag & FWRITE) == 0) 219 return (EPERM); 220 if (apm_softc.active) 221 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_suspend_sx); 222 else 223 error = EINVAL; 224 break; 225 case APMIO_STANDBY: 226 if ((flag & FWRITE) == 0) 227 return (EPERM); 228 if (apm_softc.active) 229 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_standby_sx); 230 else 231 error = EINVAL; 232 break; 233 case APMIO_GETINFO_OLD: 234 if (acpi_capm_get_info(&info)) 235 error = ENXIO; 236 aiop = (apm_info_old_t)addr; 237 aiop->ai_major = info.ai_major; 238 aiop->ai_minor = info.ai_minor; 239 aiop->ai_acline = info.ai_acline; 240 aiop->ai_batt_stat = info.ai_batt_stat; 241 aiop->ai_batt_life = info.ai_batt_life; 242 aiop->ai_status = info.ai_status; 243 break; 244 case APMIO_GETINFO: 245 if (acpi_capm_get_info((apm_info_t)addr)) 246 error = ENXIO; 247 break; 248 case APMIO_GETPWSTATUS: 249 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr)) 250 error = ENXIO; 251 break; 252 case APMIO_ENABLE: 253 if ((flag & FWRITE) == 0) 254 return (EPERM); 255 apm_softc.active = 1; 256 break; 257 case APMIO_DISABLE: 258 if ((flag & FWRITE) == 0) 259 return (EPERM); 260 apm_softc.active = 0; 261 break; 262 case APMIO_HALTCPU: 263 break; 264 case APMIO_NOTHALTCPU: 265 break; 266 case APMIO_DISPLAY: 267 if ((flag & FWRITE) == 0) 268 return (EPERM); 269 break; 270 case APMIO_BIOS: 271 if ((flag & FWRITE) == 0) 272 return (EPERM); 273 bzero(addr, sizeof(struct apm_bios_arg)); 274 break; 275 default: 276 error = EINVAL; 277 break; 278 } 279 280 return (error); 281} 282 283static int 284apmwrite(struct cdev *dev, struct uio *uio, int ioflag) 285{ 286 return (uio->uio_resid); 287} 288 289static int 290apmpoll(struct cdev *dev, int events, d_thread_t *td) 291{ 292 return (0); 293} 294 295static void 296acpi_capm_init(struct acpi_softc *sc) 297{ 298 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm"); 299} 300 301int 302acpi_machdep_init(device_t dev) 303{ 304 struct acpi_softc *sc; 305 306 acpi_dev = dev; 307 sc = device_get_softc(acpi_dev); 308 309 acpi_capm_init(sc); 310 311 acpi_install_wakeup_handler(sc); 312 313 if (intr_model == ACPI_INTR_PIC) 314 BUS_CONFIG_INTR(dev, AcpiGbl_FADT->SciInt, INTR_TRIGGER_LEVEL, 315 INTR_POLARITY_LOW); 316 else 317 acpi_SetIntrModel(intr_model); 318 319 SYSCTL_ADD_UINT(&sc->acpi_sysctl_ctx, 320 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, 321 "reset_video", CTLFLAG_RD | CTLFLAG_RW, &acpi_reset_video, 0, 322 "Call the VESA reset BIOS vector on the resume path"); 323 324 return (0); 325} 326 327void 328acpi_SetDefaultIntrModel(int model) 329{ 330 331 intr_model = model; 332} 333