acpi_apm.c revision 131312
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 131312 2004-06-30 04:42:29Z 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/kernel.h> 35#include <sys/sysctl.h> 36#include <sys/uio.h> 37#include <vm/vm.h> 38#include <vm/pmap.h> 39 40#include "acpi.h" 41#include <dev/acpica/acpivar.h> 42#include <dev/acpica/acpiio.h> 43 44static device_t acpi_dev; 45 46/* 47 * APM driver emulation 48 */ 49 50#include <sys/selinfo.h> 51 52#include <machine/apm_bios.h> 53#include <machine/pc/bios.h> 54 55#include <i386/bios/apm.h> 56 57uint32_t acpi_reset_video = 1; 58TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video); 59 60static int intr_model = ACPI_INTR_PIC; 61static struct apm_softc apm_softc; 62 63static d_open_t apmopen; 64static d_close_t apmclose; 65static d_write_t apmwrite; 66static d_ioctl_t apmioctl; 67static d_poll_t apmpoll; 68 69static struct cdevsw apm_cdevsw = { 70 .d_version = D_VERSION, 71 .d_flags = D_NEEDGIANT, 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}; 79 80static int 81acpi_capm_convert_battstate(struct acpi_battinfo *battp) 82{ 83 int state; 84 85 state = APM_UNKNOWN; 86 87 if (battp->state & ACPI_BATT_STAT_DISCHARG) { 88 if (battp->cap >= 50) 89 state = 0; /* high */ 90 else 91 state = 1; /* low */ 92 } 93 if (battp->state & ACPI_BATT_STAT_CRITICAL) 94 state = 2; /* critical */ 95 if (battp->state & ACPI_BATT_STAT_CHARGING) 96 state = 3; /* charging */ 97 98 /* If still unknown, determine it based on the battery capacity. */ 99 if (state == APM_UNKNOWN) { 100 if (battp->cap >= 50) 101 state = 0; /* high */ 102 else 103 state = 1; /* low */ 104 } 105 106 return (state); 107} 108 109static int 110acpi_capm_convert_battflags(struct acpi_battinfo *battp) 111{ 112 int flags; 113 114 flags = 0; 115 116 if (battp->cap >= 50) 117 flags |= APM_BATT_HIGH; 118 else { 119 if (battp->state & ACPI_BATT_STAT_CRITICAL) 120 flags |= APM_BATT_CRITICAL; 121 else 122 flags |= APM_BATT_LOW; 123 } 124 if (battp->state & ACPI_BATT_STAT_CHARGING) 125 flags |= APM_BATT_CHARGING; 126 if (battp->state == ACPI_BATT_STAT_NOT_PRESENT) 127 flags = APM_BATT_NOT_PRESENT; 128 129 return (flags); 130} 131 132static int 133acpi_capm_get_info(apm_info_t aip) 134{ 135 int acline; 136 struct acpi_battinfo batt; 137 138 aip->ai_infoversion = 1; 139 aip->ai_major = 1; 140 aip->ai_minor = 2; 141 aip->ai_status = apm_softc.active; 142 aip->ai_capabilities= 0xff00; /* unknown */ 143 144 if (acpi_acad_get_acline(&acline)) 145 aip->ai_acline = APM_UNKNOWN; /* unknown */ 146 else 147 aip->ai_acline = acline; /* on/off */ 148 149 if (acpi_battery_get_battinfo(-1, &batt)) { 150 aip->ai_batt_stat = APM_UNKNOWN; 151 aip->ai_batt_life = APM_UNKNOWN; 152 aip->ai_batt_time = -1; /* unknown */ 153 aip->ai_batteries = ~0U; /* unknown */ 154 } else { 155 aip->ai_batt_stat = acpi_capm_convert_battstate(&batt); 156 aip->ai_batt_life = batt.cap; 157 aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 158 aip->ai_batteries = acpi_battery_get_units(); 159 } 160 161 return (0); 162} 163 164static int 165acpi_capm_get_pwstatus(apm_pwstatus_t app) 166{ 167 int batt_unit; 168 int acline; 169 struct acpi_battinfo batt; 170 171 if (app->ap_device != PMDV_ALLDEV && 172 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) 173 return (1); 174 175 if (app->ap_device == PMDV_ALLDEV) 176 batt_unit = -1; /* all units */ 177 else 178 batt_unit = app->ap_device - PMDV_BATT0; 179 180 if (acpi_battery_get_battinfo(batt_unit, &batt)) 181 return (1); 182 183 app->ap_batt_stat = acpi_capm_convert_battstate(&batt); 184 app->ap_batt_flag = acpi_capm_convert_battflags(&batt); 185 app->ap_batt_life = batt.cap; 186 app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60; 187 188 if (acpi_acad_get_acline(&acline)) 189 app->ap_acline = APM_UNKNOWN; 190 else 191 app->ap_acline = acline; /* on/off */ 192 193 return (0); 194} 195 196static int 197apmopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 198{ 199 return (0); 200} 201 202static int 203apmclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 204{ 205 return (0); 206} 207 208static int 209apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 210{ 211 int error = 0; 212 struct acpi_softc *acpi_sc; 213 struct apm_info info; 214 apm_info_old_t aiop; 215 216 acpi_sc = device_get_softc(acpi_dev); 217 218 switch (cmd) { 219 case APMIO_SUSPEND: 220 if ((flag & FWRITE) == 0) 221 return (EPERM); 222 if (apm_softc.active) 223 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_suspend_sx); 224 else 225 error = EINVAL; 226 break; 227 case APMIO_STANDBY: 228 if ((flag & FWRITE) == 0) 229 return (EPERM); 230 if (apm_softc.active) 231 acpi_SetSleepState(acpi_sc, acpi_sc->acpi_standby_sx); 232 else 233 error = EINVAL; 234 break; 235 case APMIO_GETINFO_OLD: 236 if (acpi_capm_get_info(&info)) 237 error = ENXIO; 238 aiop = (apm_info_old_t)addr; 239 aiop->ai_major = info.ai_major; 240 aiop->ai_minor = info.ai_minor; 241 aiop->ai_acline = info.ai_acline; 242 aiop->ai_batt_stat = info.ai_batt_stat; 243 aiop->ai_batt_life = info.ai_batt_life; 244 aiop->ai_status = info.ai_status; 245 break; 246 case APMIO_GETINFO: 247 if (acpi_capm_get_info((apm_info_t)addr)) 248 error = ENXIO; 249 break; 250 case APMIO_GETPWSTATUS: 251 if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr)) 252 error = ENXIO; 253 break; 254 case APMIO_ENABLE: 255 if ((flag & FWRITE) == 0) 256 return (EPERM); 257 apm_softc.active = 1; 258 break; 259 case APMIO_DISABLE: 260 if ((flag & FWRITE) == 0) 261 return (EPERM); 262 apm_softc.active = 0; 263 break; 264 case APMIO_HALTCPU: 265 break; 266 case APMIO_NOTHALTCPU: 267 break; 268 case APMIO_DISPLAY: 269 if ((flag & FWRITE) == 0) 270 return (EPERM); 271 break; 272 case APMIO_BIOS: 273 if ((flag & FWRITE) == 0) 274 return (EPERM); 275 bzero(addr, sizeof(struct apm_bios_arg)); 276 break; 277 default: 278 error = EINVAL; 279 break; 280 } 281 282 return (error); 283} 284 285static int 286apmwrite(struct cdev *dev, struct uio *uio, int ioflag) 287{ 288 return (uio->uio_resid); 289} 290 291static int 292apmpoll(struct cdev *dev, int events, d_thread_t *td) 293{ 294 return (0); 295} 296 297static void 298acpi_capm_init(struct acpi_softc *sc) 299{ 300 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm"); 301} 302 303int 304acpi_machdep_init(device_t dev) 305{ 306 struct acpi_softc *sc; 307 308 acpi_dev = dev; 309 sc = device_get_softc(acpi_dev); 310 311 acpi_capm_init(sc); 312 313 acpi_install_wakeup_handler(sc); 314 315 if (intr_model == ACPI_INTR_PIC) 316 BUS_CONFIG_INTR(dev, AcpiGbl_FADT->SciInt, INTR_TRIGGER_LEVEL, 317 INTR_POLARITY_LOW); 318 else 319 acpi_SetIntrModel(intr_model); 320 321 SYSCTL_ADD_UINT(&sc->acpi_sysctl_ctx, 322 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, 323 "reset_video", CTLFLAG_RD | CTLFLAG_RW, &acpi_reset_video, 0, 324 "Call the VESA reset BIOS vector on the resume path"); 325 326 return (0); 327} 328 329void 330acpi_SetDefaultIntrModel(int model) 331{ 332 333 intr_model = model; 334} 335 336/* Check BIOS date. If 1998 or older, disable ACPI. */ 337int 338acpi_machdep_quirks(int *quirks) 339{ 340 char *va; 341 int year; 342 343 /* BIOS address 0xffff5 contains the date in the format mm/dd/yy. */ 344 va = pmap_mapdev(0xffff0, 16); 345 sscanf(va + 11, "%2d", &year); 346 pmap_unmapdev((vm_offset_t)va, 16); 347 348 /* 349 * Date must be >= 1/1/1999 or we don't trust ACPI. Note that this 350 * check must be changed by my 114th birthday. 351 */ 352 if (year > 90 && year < 99) 353 *quirks = ACPI_Q_BROKEN; 354 355 return (0); 356} 357