2001-10-26 17:43:05 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2001 Mitsuru IWASAKI
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-02 06:16:45 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-10-26 17:43:05 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include "acpi.h"
|
|
|
|
|
|
|
|
#include <dev/acpica/acpivar.h>
|
|
|
|
#include <dev/acpica/acpiio.h>
|
|
|
|
|
|
|
|
static device_t acpi_dev;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* APM driver emulation
|
|
|
|
*/
|
|
|
|
|
2002-10-16 17:20:43 +00:00
|
|
|
#if __FreeBSD_version < 500000
|
|
|
|
#include <sys/select.h>
|
|
|
|
#else
|
2001-10-26 17:43:05 +00:00
|
|
|
#include <sys/selinfo.h>
|
2002-10-16 17:20:43 +00:00
|
|
|
#endif
|
2001-10-26 17:43:05 +00:00
|
|
|
|
|
|
|
#include <machine/apm_bios.h>
|
|
|
|
#include <machine/pc/bios.h>
|
|
|
|
|
2003-05-13 16:59:46 +00:00
|
|
|
#if __FreeBSD_version < 500000
|
|
|
|
#include <i386/apm/apm.h>
|
|
|
|
#else
|
2003-03-24 19:14:46 +00:00
|
|
|
#include <i386/bios/apm.h>
|
2003-05-13 16:59:46 +00:00
|
|
|
#endif
|
2001-10-26 17:43:05 +00:00
|
|
|
|
|
|
|
static struct apm_softc apm_softc;
|
|
|
|
|
|
|
|
static d_open_t apmopen;
|
|
|
|
static d_close_t apmclose;
|
|
|
|
static d_write_t apmwrite;
|
|
|
|
static d_ioctl_t apmioctl;
|
|
|
|
static d_poll_t apmpoll;
|
|
|
|
|
|
|
|
#define CDEV_MAJOR 39
|
|
|
|
static struct cdevsw apm_cdevsw = {
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = apmopen,
|
|
|
|
.d_close = apmclose,
|
|
|
|
.d_write = apmwrite,
|
|
|
|
.d_ioctl = apmioctl,
|
|
|
|
.d_poll = apmpoll,
|
|
|
|
.d_name = "apm",
|
|
|
|
.d_maj = CDEV_MAJOR,
|
2001-10-26 17:43:05 +00:00
|
|
|
};
|
|
|
|
|
2003-09-10 05:29:30 +00:00
|
|
|
static int intr_model = ACPI_INTR_PIC;
|
|
|
|
|
2001-10-26 17:43:05 +00:00
|
|
|
static int
|
|
|
|
acpi_capm_convert_battstate(struct acpi_battinfo *battp)
|
|
|
|
{
|
|
|
|
int state;
|
|
|
|
|
|
|
|
state = 0xff; /* XXX unknown */
|
|
|
|
|
|
|
|
if (battp->state & ACPI_BATT_STAT_DISCHARG) {
|
2003-08-28 16:26:24 +00:00
|
|
|
if (battp->cap >= 50)
|
2001-10-26 17:43:05 +00:00
|
|
|
state = 0; /* high */
|
2003-08-28 16:26:24 +00:00
|
|
|
else
|
2001-10-26 17:43:05 +00:00
|
|
|
state = 1; /* low */
|
|
|
|
}
|
2003-08-28 16:26:24 +00:00
|
|
|
if (battp->state & ACPI_BATT_STAT_CRITICAL)
|
2001-10-26 17:43:05 +00:00
|
|
|
state = 2; /* critical */
|
2003-08-28 16:26:24 +00:00
|
|
|
if (battp->state & ACPI_BATT_STAT_CHARGING)
|
2001-10-26 17:43:05 +00:00
|
|
|
state = 3; /* charging */
|
2003-08-28 16:26:24 +00:00
|
|
|
|
2003-09-17 08:47:39 +00:00
|
|
|
/* If still unknown, determine it based on the battery capacity. */
|
|
|
|
if (state == 0xff) {
|
|
|
|
if (battp->cap >= 50) {
|
|
|
|
state = 0; /* high */
|
|
|
|
} else {
|
|
|
|
state = 1; /* low */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-26 17:43:05 +00:00
|
|
|
return (state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
acpi_capm_convert_battflags(struct acpi_battinfo *battp)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
|
|
|
|
if (battp->cap >= 50) {
|
|
|
|
flags |= APM_BATT_HIGH;
|
|
|
|
} else {
|
2003-08-28 16:26:24 +00:00
|
|
|
if (battp->state & ACPI_BATT_STAT_CRITICAL)
|
2001-10-26 17:43:05 +00:00
|
|
|
flags |= APM_BATT_CRITICAL;
|
2003-08-28 16:26:24 +00:00
|
|
|
else
|
2001-10-26 17:43:05 +00:00
|
|
|
flags |= APM_BATT_LOW;
|
|
|
|
}
|
2003-08-28 16:26:24 +00:00
|
|
|
if (battp->state & ACPI_BATT_STAT_CHARGING)
|
2001-10-26 17:43:05 +00:00
|
|
|
flags |= APM_BATT_CHARGING;
|
2003-08-28 16:26:24 +00:00
|
|
|
if (battp->state == ACPI_BATT_STAT_NOT_PRESENT)
|
2001-10-26 17:43:05 +00:00
|
|
|
flags = APM_BATT_NOT_PRESENT;
|
|
|
|
|
|
|
|
return (flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
acpi_capm_get_info(apm_info_t aip)
|
|
|
|
{
|
|
|
|
int acline;
|
|
|
|
struct acpi_battinfo batt;
|
|
|
|
|
|
|
|
aip->ai_infoversion = 1;
|
|
|
|
aip->ai_major = 1;
|
|
|
|
aip->ai_minor = 2;
|
|
|
|
aip->ai_status = apm_softc.active;
|
|
|
|
aip->ai_capabilities= 0xff00; /* XXX unknown */
|
|
|
|
|
2003-08-28 16:26:24 +00:00
|
|
|
if (acpi_acad_get_acline(&acline))
|
2001-10-26 17:43:05 +00:00
|
|
|
aip->ai_acline = 0xff; /* unknown */
|
2003-08-28 16:26:24 +00:00
|
|
|
else
|
2001-10-26 17:43:05 +00:00
|
|
|
aip->ai_acline = acline; /* on/off */
|
|
|
|
|
|
|
|
if (acpi_battery_get_battinfo(-1, &batt)) {
|
|
|
|
aip->ai_batt_stat = 0xff; /* unknown */
|
|
|
|
aip->ai_batt_life = 0xff; /* unknown */
|
|
|
|
aip->ai_batt_time = -1; /* unknown */
|
|
|
|
aip->ai_batteries = 0;
|
|
|
|
} else {
|
|
|
|
aip->ai_batt_stat = acpi_capm_convert_battstate(&batt);
|
|
|
|
aip->ai_batt_life = batt.cap;
|
|
|
|
aip->ai_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
|
|
|
|
aip->ai_batteries = acpi_battery_get_units();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
acpi_capm_get_pwstatus(apm_pwstatus_t app)
|
|
|
|
{
|
|
|
|
int batt_unit;
|
|
|
|
int acline;
|
|
|
|
struct acpi_battinfo batt;
|
|
|
|
|
|
|
|
if (app->ap_device != PMDV_ALLDEV &&
|
|
|
|
(app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL)) {
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2003-08-28 16:26:24 +00:00
|
|
|
if (app->ap_device == PMDV_ALLDEV)
|
2001-10-26 17:43:05 +00:00
|
|
|
batt_unit = -1; /* all units */
|
2003-08-28 16:26:24 +00:00
|
|
|
else
|
2001-10-26 17:43:05 +00:00
|
|
|
batt_unit = app->ap_device - PMDV_BATT0;
|
|
|
|
|
2003-08-28 16:26:24 +00:00
|
|
|
if (acpi_battery_get_battinfo(batt_unit, &batt))
|
2001-10-26 17:43:05 +00:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
app->ap_batt_stat = acpi_capm_convert_battstate(&batt);
|
|
|
|
app->ap_batt_flag = acpi_capm_convert_battflags(&batt);
|
|
|
|
app->ap_batt_life = batt.cap;
|
|
|
|
app->ap_batt_time = (batt.min == -1) ? -1 : batt.min * 60;
|
|
|
|
|
2003-08-28 16:26:24 +00:00
|
|
|
if (acpi_acad_get_acline(&acline))
|
2001-10-26 17:43:05 +00:00
|
|
|
app->ap_acline = 0xff; /* unknown */
|
2003-08-28 16:26:24 +00:00
|
|
|
else
|
2001-10-26 17:43:05 +00:00
|
|
|
app->ap_acline = acline; /* on/off */
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-10-09 20:39:26 +00:00
|
|
|
apmopen(dev_t dev, int flag, int fmt, d_thread_t *td)
|
2001-10-26 17:43:05 +00:00
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-10-09 20:39:26 +00:00
|
|
|
apmclose(dev_t dev, int flag, int fmt, d_thread_t *td)
|
2001-10-26 17:43:05 +00:00
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-10-09 20:39:26 +00:00
|
|
|
apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
|
2001-10-26 17:43:05 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
struct acpi_softc *acpi_sc;
|
|
|
|
struct apm_info info;
|
|
|
|
apm_info_old_t aiop;
|
|
|
|
|
2003-08-28 16:26:24 +00:00
|
|
|
if ((acpi_sc = device_get_softc(acpi_dev)) == NULL)
|
2001-10-26 17:43:05 +00:00
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case APMIO_SUSPEND:
|
2003-08-28 16:26:24 +00:00
|
|
|
if ((flag & FWRITE) == 0)
|
2001-10-26 17:43:05 +00:00
|
|
|
return (EPERM);
|
|
|
|
if (apm_softc.active)
|
|
|
|
acpi_SetSleepState(acpi_sc, acpi_sc->acpi_suspend_sx);
|
|
|
|
else
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
case APMIO_STANDBY:
|
2003-08-28 16:26:24 +00:00
|
|
|
if ((flag & FWRITE) == 0)
|
2001-10-26 17:43:05 +00:00
|
|
|
return (EPERM);
|
|
|
|
if (apm_softc.active)
|
|
|
|
acpi_SetSleepState(acpi_sc, acpi_sc->acpi_standby_sx);
|
|
|
|
else
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
case APMIO_GETINFO_OLD:
|
|
|
|
if (acpi_capm_get_info(&info))
|
|
|
|
error = ENXIO;
|
|
|
|
aiop = (apm_info_old_t)addr;
|
|
|
|
aiop->ai_major = info.ai_major;
|
|
|
|
aiop->ai_minor = info.ai_minor;
|
|
|
|
aiop->ai_acline = info.ai_acline;
|
|
|
|
aiop->ai_batt_stat = info.ai_batt_stat;
|
|
|
|
aiop->ai_batt_life = info.ai_batt_life;
|
|
|
|
aiop->ai_status = info.ai_status;
|
|
|
|
break;
|
|
|
|
case APMIO_GETINFO:
|
|
|
|
if (acpi_capm_get_info((apm_info_t)addr))
|
|
|
|
error = ENXIO;
|
|
|
|
break;
|
|
|
|
case APMIO_GETPWSTATUS:
|
|
|
|
if (acpi_capm_get_pwstatus((apm_pwstatus_t)addr))
|
|
|
|
error = ENXIO;
|
|
|
|
break;
|
|
|
|
case APMIO_ENABLE:
|
2003-08-28 16:26:24 +00:00
|
|
|
if ((flag & FWRITE) == 0)
|
2001-10-26 17:43:05 +00:00
|
|
|
return (EPERM);
|
|
|
|
apm_softc.active = 1;
|
|
|
|
break;
|
|
|
|
case APMIO_DISABLE:
|
2003-08-28 16:26:24 +00:00
|
|
|
if ((flag & FWRITE) == 0)
|
2001-10-26 17:43:05 +00:00
|
|
|
return (EPERM);
|
|
|
|
apm_softc.active = 0;
|
|
|
|
break;
|
|
|
|
case APMIO_HALTCPU:
|
|
|
|
break;
|
|
|
|
case APMIO_NOTHALTCPU:
|
|
|
|
break;
|
|
|
|
case APMIO_DISPLAY:
|
2003-08-28 16:26:24 +00:00
|
|
|
if ((flag & FWRITE) == 0)
|
2001-10-26 17:43:05 +00:00
|
|
|
return (EPERM);
|
|
|
|
break;
|
|
|
|
case APMIO_BIOS:
|
2003-08-28 16:26:24 +00:00
|
|
|
if ((flag & FWRITE) == 0)
|
2001-10-26 17:43:05 +00:00
|
|
|
return (EPERM);
|
|
|
|
bzero(addr, sizeof(struct apm_bios_arg));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
apmwrite(dev_t dev, struct uio *uio, int ioflag)
|
|
|
|
{
|
|
|
|
return (uio->uio_resid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-10-09 20:39:26 +00:00
|
|
|
apmpoll(dev_t dev, int events, d_thread_t *td)
|
2001-10-26 17:43:05 +00:00
|
|
|
{
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
acpi_capm_init(struct acpi_softc *sc)
|
|
|
|
{
|
|
|
|
make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
acpi_machdep_init(device_t dev)
|
|
|
|
{
|
|
|
|
struct acpi_softc *sc;
|
|
|
|
|
|
|
|
acpi_dev = dev;
|
2003-08-28 16:26:24 +00:00
|
|
|
if ((sc = device_get_softc(acpi_dev)) == NULL)
|
2001-10-26 17:43:05 +00:00
|
|
|
return (ENXIO);
|
|
|
|
|
2002-09-30 18:45:20 +00:00
|
|
|
/*
|
|
|
|
* XXX: Prevent the PnP BIOS code from interfering with
|
|
|
|
* our own scan of ISA devices.
|
|
|
|
*/
|
|
|
|
PnPBIOStable = NULL;
|
|
|
|
|
2001-10-26 17:43:05 +00:00
|
|
|
acpi_capm_init(sc);
|
|
|
|
|
|
|
|
acpi_install_wakeup_handler(sc);
|
|
|
|
|
2003-09-10 05:29:30 +00:00
|
|
|
if (intr_model != ACPI_INTR_PIC)
|
|
|
|
acpi_SetIntrModel(intr_model);
|
2001-10-26 17:43:05 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2003-09-10 05:29:30 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
acpi_SetDefaultIntrModel(int model)
|
|
|
|
{
|
|
|
|
|
|
|
|
intr_model = model;
|
|
|
|
}
|