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