2005-01-06 22:18:23 +00:00
|
|
|
/*-
|
1995-02-17 02:22:57 +00:00
|
|
|
* APM (Advanced Power Management) BIOS Device Driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 1994 UKAI, Fumitoshi.
|
1997-01-06 06:02:03 +00:00
|
|
|
* Copyright (c) 1994-1995 by HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
|
1996-04-18 19:21:47 +00:00
|
|
|
* Copyright (c) 1996 Nate Williams <nate@FreeBSD.org>
|
1997-03-29 11:07:12 +00:00
|
|
|
* Copyright (c) 1997 Poul-Henning Kamp <phk@FreeBSD.org>
|
1994-10-01 02:56:21 +00:00
|
|
|
*
|
|
|
|
* This software may be used, modified, copied, and distributed, in
|
|
|
|
* both source and binary form provided that the above copyright and
|
1995-05-30 08:16:23 +00:00
|
|
|
* these terms are retained. Under no circumstances is the author
|
|
|
|
* responsible for the proper functioning of this software, nor does
|
|
|
|
* the author assume any responsibility for damages incurred with its
|
1994-10-01 02:56:21 +00:00
|
|
|
* use.
|
|
|
|
*
|
|
|
|
* Sep, 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
|
|
|
|
*/
|
|
|
|
|
2003-06-02 06:02:49 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
#include <sys/param.h>
|
Divorce "dev_t" from the "major|minor" bitmap, which is now called
udev_t in the kernel but still called dev_t in userland.
Provide functions to manipulate both types:
major() umajor()
minor() uminor()
makedev() umakedev()
dev2udev() udev2dev()
For now they're functions, they will become in-line functions
after one of the next two steps in this process.
Return major/minor/makedev to macro-hood for userland.
Register a name in cdevsw[] for the "filedescriptor" driver.
In the kernel the udev_t appears in places where we have the
major/minor number combination, (ie: a potential device: we
may not have the driver nor the device), like in inodes, vattr,
cdevsw registration and so on, whereas the dev_t appears where
we carry around a reference to a actual device.
In the future the cdevsw and the aliased-from vnode will be hung
directly from the dev_t, along with up to two softc pointers for
the device driver and a few houskeeping bits. This will essentially
replace the current "alias" check code (same buck, bigger bang).
A little stunt has been provided to try to catch places where the
wrong type is being used (dev_t vs udev_t), if you see something
not working, #undef DEVT_FASCIST in kern/kern_conf.c and see if
it makes a difference. If it does, please try to track it down
(many hands make light work) or at least try to reproduce it
as simply as possible, and describe how to do that.
Without DEVT_FASCIST I belive this patch is a no-op.
Stylistic/posixoid comments about the userland view of the <sys/*.h>
files welcome now, from userland they now contain the end result.
Next planned step: make all dev_t's refer to the same devsw[] which
means convert BLK's to CHR's at the perimeter of the vnodes and
other places where they enter the game (bootdev, mknod, sysctl).
1999-05-11 19:55:07 +00:00
|
|
|
#include <sys/systm.h>
|
2002-09-21 18:30:12 +00:00
|
|
|
#include <sys/bus.h>
|
1995-11-28 09:44:42 +00:00
|
|
|
#include <sys/conf.h>
|
2006-05-25 23:06:38 +00:00
|
|
|
#include <sys/condvar.h>
|
2002-09-21 18:30:12 +00:00
|
|
|
#include <sys/eventhandler.h>
|
|
|
|
#include <sys/fcntl.h>
|
1995-11-29 10:49:16 +00:00
|
|
|
#include <sys/kernel.h>
|
2006-05-25 23:06:38 +00:00
|
|
|
#include <sys/kthread.h>
|
|
|
|
#include <sys/lock.h>
|
2004-05-30 20:34:58 +00:00
|
|
|
#include <sys/module.h>
|
2006-05-25 23:06:38 +00:00
|
|
|
#include <sys/mutex.h>
|
2002-09-21 18:30:12 +00:00
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/power.h>
|
1998-10-30 05:41:15 +00:00
|
|
|
#include <sys/reboot.h>
|
2001-01-09 04:33:49 +00:00
|
|
|
#include <sys/selinfo.h>
|
1999-07-10 18:08:57 +00:00
|
|
|
#include <sys/signalvar.h>
|
1999-08-22 14:48:00 +00:00
|
|
|
#include <sys/sysctl.h>
|
2002-09-21 18:30:12 +00:00
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
#include <machine/apm_bios.h>
|
1994-10-02 17:40:38 +00:00
|
|
|
#include <machine/clock.h>
|
2003-06-22 05:08:10 +00:00
|
|
|
#include <machine/endian.h>
|
2002-09-21 18:30:12 +00:00
|
|
|
#include <machine/pc/bios.h>
|
2002-09-21 18:51:19 +00:00
|
|
|
#include <machine/cpufunc.h>
|
2002-09-21 18:30:12 +00:00
|
|
|
#include <machine/segments.h>
|
2001-11-01 16:34:07 +00:00
|
|
|
#include <machine/stdarg.h>
|
2002-09-21 18:30:12 +00:00
|
|
|
#include <machine/vm86.h>
|
|
|
|
|
2003-03-25 05:19:18 +00:00
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
#include <sys/rman.h>
|
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
#include <vm/vm.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <vm/pmap.h>
|
2002-09-21 18:30:12 +00:00
|
|
|
#include <vm/vm_param.h>
|
1998-06-03 01:59:42 +00:00
|
|
|
|
2003-03-24 19:14:46 +00:00
|
|
|
#include <i386/bios/apm.h>
|
2009-05-03 17:47:21 +00:00
|
|
|
#include <isa/rtc.h>
|
1999-10-02 03:34:15 +00:00
|
|
|
|
|
|
|
/* Used by the apm_saver screen saver module */
|
2002-03-20 05:48:58 +00:00
|
|
|
int apm_display(int newstate);
|
1999-10-02 03:34:15 +00:00
|
|
|
struct apm_softc apm_softc;
|
|
|
|
|
2002-03-20 05:48:58 +00:00
|
|
|
static void apm_resume(void);
|
1999-07-29 01:49:19 +00:00
|
|
|
static int apm_bioscall(void);
|
2002-03-20 05:48:58 +00:00
|
|
|
static int apm_check_function_supported(u_int version, u_int func);
|
1999-07-28 20:20:31 +00:00
|
|
|
|
2001-11-01 16:34:07 +00:00
|
|
|
static int apm_pm_func(u_long, void*, ...);
|
|
|
|
|
1999-07-28 20:20:31 +00:00
|
|
|
static u_long apm_version;
|
|
|
|
|
1999-07-10 18:08:57 +00:00
|
|
|
int apm_evindex;
|
|
|
|
|
|
|
|
#define SCFLAG_ONORMAL 0x0000001
|
|
|
|
#define SCFLAG_OCTL 0x0000002
|
|
|
|
#define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
|
|
|
|
|
|
|
|
#define APMDEV_NORMAL 0
|
2009-10-31 10:38:30 +00:00
|
|
|
#define APMDEV_CTL 1
|
1995-02-17 02:22:57 +00:00
|
|
|
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
extern int bios32_apm98(struct bios_regs *, u_int, u_short);
|
|
|
|
|
|
|
|
/* PC98's SMM definition */
|
|
|
|
#define APM_NECSMM_PORT 0x6b8e
|
|
|
|
#define APM_NECSMM_PORTSZ 1
|
|
|
|
#define APM_NECSMM_EN 0x10
|
|
|
|
static __inline void apm_enable_smm(struct apm_softc *);
|
|
|
|
static __inline void apm_disable_smm(struct apm_softc *);
|
|
|
|
int apm_necsmm_addr;
|
|
|
|
u_int32_t apm_necsmm_mask;
|
|
|
|
#endif
|
|
|
|
|
1995-12-22 13:08:27 +00:00
|
|
|
static struct apmhook *hook[NAPM_HOOK]; /* XXX */
|
1995-02-17 02:22:57 +00:00
|
|
|
|
1994-10-02 17:40:38 +00:00
|
|
|
#define is_enabled(foo) ((foo) ? "enabled" : "disabled")
|
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
/* Map version number to integer (keeps ordering of version numbers) */
|
|
|
|
#define INTVERSION(major, minor) ((major)*100 + (minor))
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static d_open_t apmopen;
|
|
|
|
static d_close_t apmclose;
|
1999-07-10 18:08:57 +00:00
|
|
|
static d_write_t apmwrite;
|
1995-12-08 11:19:42 +00:00
|
|
|
static d_ioctl_t apmioctl;
|
1999-07-10 18:08:57 +00:00
|
|
|
static d_poll_t apmpoll;
|
1995-12-08 11:19:42 +00:00
|
|
|
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw apm_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_flags = D_NEEDGIANT,
|
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",
|
1999-05-30 16:53:49 +00:00
|
|
|
};
|
1995-12-08 11:19:42 +00:00
|
|
|
|
1999-08-22 14:48:00 +00:00
|
|
|
static int apm_suspend_delay = 1;
|
|
|
|
static int apm_standby_delay = 1;
|
2003-06-22 05:08:10 +00:00
|
|
|
static int apm_swab_batt_minutes = 0;
|
2000-08-04 20:28:53 +00:00
|
|
|
static int apm_debug = 0;
|
|
|
|
|
|
|
|
#define APM_DPRINT(args...) do { \
|
|
|
|
if (apm_debug) { \
|
|
|
|
printf(args); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
1999-08-22 14:48:00 +00:00
|
|
|
|
|
|
|
SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
|
|
|
|
SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
|
2000-08-04 20:28:53 +00:00
|
|
|
SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
|
1999-08-22 14:48:00 +00:00
|
|
|
|
2003-06-22 05:08:10 +00:00
|
|
|
TUNABLE_INT("machdep.apm_swab_batt_minutes", &apm_swab_batt_minutes);
|
|
|
|
SYSCTL_INT(_machdep, OID_AUTO, apm_swab_batt_minutes, CTLFLAG_RW,
|
|
|
|
&apm_swab_batt_minutes, 0, "Byte swap battery time value.");
|
|
|
|
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
static __inline void
|
|
|
|
apm_enable_smm(sc)
|
|
|
|
struct apm_softc *sc;
|
|
|
|
{
|
|
|
|
bus_space_tag_t iot = sc->sc_iot;
|
|
|
|
bus_space_handle_t ioh = sc->sc_ioh;
|
|
|
|
if (apm_necsmm_addr != 0)
|
|
|
|
bus_space_write_1(iot, ioh, 0,
|
|
|
|
(bus_space_read_1(iot, ioh, 0) | ~apm_necsmm_mask));
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
apm_disable_smm(sc)
|
|
|
|
struct apm_softc *sc;
|
|
|
|
{
|
|
|
|
bus_space_tag_t iot = sc->sc_iot;
|
|
|
|
bus_space_handle_t ioh = sc->sc_ioh;
|
|
|
|
if (apm_necsmm_addr != 0)
|
|
|
|
bus_space_write_1(iot, ioh, 0,
|
|
|
|
(bus_space_read_1(iot, ioh, 0) & apm_necsmm_mask));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-08-14 18:39:40 +00:00
|
|
|
/*
|
|
|
|
* return 0 if the function successfull,
|
|
|
|
* return 1 if the function unsuccessfull,
|
|
|
|
* return -1 if the function unsupported.
|
|
|
|
*/
|
1997-11-12 04:12:51 +00:00
|
|
|
static int
|
1999-07-29 01:49:19 +00:00
|
|
|
apm_bioscall(void)
|
1994-10-02 01:45:42 +00:00
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
int errno = 0;
|
1999-08-14 18:39:40 +00:00
|
|
|
u_int apm_func = sc->bios.r.eax & 0xff;
|
|
|
|
|
|
|
|
if (!apm_check_function_supported(sc->intversion, apm_func)) {
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
|
|
|
|
apm_func, sc->majorversion, sc->minorversion);
|
1999-08-14 18:39:40 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
1999-07-29 01:49:19 +00:00
|
|
|
|
|
|
|
sc->bios_busy = 1;
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
set_bios_selectors(&sc->bios.seg, BIOSCODE_FLAG | BIOSDATA_FLAG);
|
|
|
|
if (bios32_apm98(&sc->bios.r, sc->bios.entry,
|
|
|
|
GSEL(GBIOSCODE32_SEL, SEL_KPL)) != 0)
|
|
|
|
return 1;
|
|
|
|
#else
|
1999-07-29 01:49:19 +00:00
|
|
|
if (sc->connectmode == APM_PROT32CONNECT) {
|
|
|
|
set_bios_selectors(&sc->bios.seg,
|
|
|
|
BIOSCODE_FLAG | BIOSDATA_FLAG);
|
|
|
|
errno = bios32(&sc->bios.r,
|
|
|
|
sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
|
|
|
|
} else {
|
|
|
|
errno = bios16(&sc->bios, NULL);
|
|
|
|
}
|
2003-03-25 05:19:18 +00:00
|
|
|
#endif
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios_busy = 0;
|
|
|
|
return (errno);
|
1994-10-02 01:45:42 +00:00
|
|
|
}
|
1994-10-01 02:56:21 +00:00
|
|
|
|
1999-08-14 18:39:40 +00:00
|
|
|
/* check whether APM function is supported (1) or not (0). */
|
|
|
|
static int
|
|
|
|
apm_check_function_supported(u_int version, u_int func)
|
|
|
|
{
|
|
|
|
/* except driver version */
|
|
|
|
if (func == APM_DRVVERSION) {
|
|
|
|
return (1);
|
|
|
|
}
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
if (func == APM_GETPWSTATUS) {
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
#endif
|
1999-08-14 18:39:40 +00:00
|
|
|
|
|
|
|
switch (version) {
|
|
|
|
case INTVERSION(1, 0):
|
|
|
|
if (func > APM_GETPMEVENT) {
|
|
|
|
return (0); /* not supported */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INTVERSION(1, 1):
|
|
|
|
if (func > APM_ENGAGEDISENGAGEPM &&
|
|
|
|
func < APM_OEMFUNC) {
|
|
|
|
return (0); /* not supported */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INTVERSION(1, 2):
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1); /* supported */
|
|
|
|
}
|
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
/* enable/disable power management */
|
1995-05-30 08:16:23 +00:00
|
|
|
static int
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_enable_disable_pm(int enable)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1997-01-06 06:02:03 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
|
1994-10-01 02:56:21 +00:00
|
|
|
|
1997-01-06 06:02:03 +00:00
|
|
|
if (sc->intversion >= INTVERSION(1, 1))
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.ebx = PMDV_ALLDEV;
|
1997-01-06 06:02:03 +00:00
|
|
|
else
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
|
|
|
|
sc->bios.r.ecx = enable;
|
|
|
|
sc->bios.r.edx = 0;
|
|
|
|
return (apm_bioscall());
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
/* register driver version (APM 1.1 or later) */
|
|
|
|
static int
|
1997-03-29 11:07:12 +00:00
|
|
|
apm_driver_version(int version)
|
1994-10-02 17:40:38 +00:00
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
|
|
|
|
sc->bios.r.ebx = 0x0;
|
|
|
|
sc->bios.r.ecx = version;
|
|
|
|
sc->bios.r.edx = 0;
|
|
|
|
|
|
|
|
if (apm_bioscall() == 0 && sc->bios.r.eax == version)
|
|
|
|
return (0);
|
1999-08-14 18:39:40 +00:00
|
|
|
|
|
|
|
/* Some old BIOSes don't return the connection version in %ax. */
|
|
|
|
if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
|
|
|
|
return (0);
|
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
return (1);
|
1994-10-02 17:40:38 +00:00
|
|
|
}
|
1999-07-29 01:49:19 +00:00
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
/* engage/disengage power management (APM 1.1 or later) */
|
1995-05-30 08:16:23 +00:00
|
|
|
static int
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_engage_disengage_pm(int engage)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
|
|
|
|
sc->bios.r.ebx = PMDV_ALLDEV;
|
|
|
|
sc->bios.r.ecx = engage;
|
|
|
|
sc->bios.r.edx = 0;
|
|
|
|
return (apm_bioscall());
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
1999-07-29 01:49:19 +00:00
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
/* get PM event */
|
1995-05-30 08:16:23 +00:00
|
|
|
static u_int
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_getevent(void)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
|
|
|
|
|
|
|
|
sc->bios.r.ebx = 0;
|
|
|
|
sc->bios.r.ecx = 0;
|
|
|
|
sc->bios.r.edx = 0;
|
|
|
|
if (apm_bioscall())
|
|
|
|
return (PMEV_NOEVENT);
|
|
|
|
return (sc->bios.r.ebx & 0xffff);
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
1999-07-29 01:49:19 +00:00
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
/* suspend entire system */
|
1995-05-30 08:16:23 +00:00
|
|
|
static int
|
1998-07-06 06:29:07 +00:00
|
|
|
apm_suspend_system(int state)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
|
|
|
|
sc->bios.r.ebx = PMDV_ALLDEV;
|
|
|
|
sc->bios.r.ecx = state;
|
|
|
|
sc->bios.r.edx = 0;
|
|
|
|
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
apm_disable_smm(sc);
|
|
|
|
#endif
|
1999-07-29 01:49:19 +00:00
|
|
|
if (apm_bioscall()) {
|
|
|
|
printf("Entire system suspend failure: errcode = %d\n",
|
|
|
|
0xff & (sc->bios.r.eax >> 8));
|
|
|
|
return 1;
|
|
|
|
}
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
apm_enable_smm(sc);
|
|
|
|
#endif
|
1999-07-29 01:49:19 +00:00
|
|
|
return 0;
|
1995-02-17 02:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Display control */
|
|
|
|
/*
|
|
|
|
* Experimental implementation: My laptop machine can't handle this function
|
|
|
|
* If your laptop can control the display via APM, please inform me.
|
1997-01-06 06:02:03 +00:00
|
|
|
* HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
|
1995-02-17 02:22:57 +00:00
|
|
|
*/
|
1999-10-02 03:34:15 +00:00
|
|
|
int
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_display(int newstate)
|
1995-02-17 02:22:57 +00:00
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
|
|
|
|
sc->bios.r.ebx = PMDV_DISP0;
|
|
|
|
sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
|
|
|
|
sc->bios.r.edx = 0;
|
2000-08-04 20:28:53 +00:00
|
|
|
if (apm_bioscall() == 0) {
|
|
|
|
return 0;
|
1999-07-29 01:49:19 +00:00
|
|
|
}
|
2000-08-04 20:28:53 +00:00
|
|
|
|
|
|
|
/* If failed, then try to blank all display devices instead. */
|
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
|
|
|
|
sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
|
|
|
|
sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
|
|
|
|
sc->bios.r.edx = 0;
|
|
|
|
if (apm_bioscall() == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
printf("Display off failure: errcode = %d\n",
|
|
|
|
0xff & (sc->bios.r.eax >> 8));
|
|
|
|
return 1;
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
1997-06-15 02:03:03 +00:00
|
|
|
/*
|
|
|
|
* Turn off the entire system.
|
|
|
|
*/
|
1998-10-30 05:41:15 +00:00
|
|
|
static void
|
1999-08-21 06:24:40 +00:00
|
|
|
apm_power_off(void *junk, int howto)
|
1997-06-15 02:03:03 +00:00
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1997-06-15 02:03:03 +00:00
|
|
|
|
1998-12-10 23:36:14 +00:00
|
|
|
/* Not halting powering off, or not active */
|
|
|
|
if (!(howto & RB_POWEROFF) || !apm_softc.active)
|
1997-06-19 00:25:03 +00:00
|
|
|
return;
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
|
|
|
|
sc->bios.r.ebx = PMDV_ALLDEV;
|
|
|
|
sc->bios.r.ecx = PMST_OFF;
|
|
|
|
sc->bios.r.edx = 0;
|
|
|
|
(void) apm_bioscall();
|
1997-06-15 02:03:03 +00:00
|
|
|
}
|
1997-01-06 06:02:03 +00:00
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
/* APM Battery low handler */
|
1995-05-30 08:16:23 +00:00
|
|
|
static void
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_battery_low(void)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1994-10-02 17:40:38 +00:00
|
|
|
printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
1995-02-17 02:22:57 +00:00
|
|
|
/* APM hook manager */
|
|
|
|
static struct apmhook *
|
|
|
|
apm_add_hook(struct apmhook **list, struct apmhook *ah)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct apmhook *p, *prev;
|
|
|
|
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
|
1995-02-17 02:22:57 +00:00
|
|
|
|
|
|
|
s = splhigh();
|
1997-01-06 06:02:03 +00:00
|
|
|
if (ah == NULL)
|
1995-02-17 02:22:57 +00:00
|
|
|
panic("illegal apm_hook!");
|
|
|
|
prev = NULL;
|
1997-01-06 06:02:03 +00:00
|
|
|
for (p = *list; p != NULL; prev = p, p = p->ah_next)
|
|
|
|
if (p->ah_order > ah->ah_order)
|
1995-02-17 02:22:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (prev == NULL) {
|
|
|
|
ah->ah_next = *list;
|
|
|
|
*list = ah;
|
|
|
|
} else {
|
|
|
|
ah->ah_next = prev->ah_next;
|
|
|
|
prev->ah_next = ah;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
return ah;
|
|
|
|
}
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
static void
|
1995-02-17 02:22:57 +00:00
|
|
|
apm_del_hook(struct apmhook **list, struct apmhook *ah)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct apmhook *p, *prev;
|
|
|
|
|
|
|
|
s = splhigh();
|
|
|
|
prev = NULL;
|
1997-01-06 06:02:03 +00:00
|
|
|
for (p = *list; p != NULL; prev = p, p = p->ah_next)
|
|
|
|
if (p == ah)
|
1995-02-17 02:22:57 +00:00
|
|
|
goto deleteit;
|
|
|
|
panic("Tried to delete unregistered apm_hook.");
|
|
|
|
goto nosuchnode;
|
|
|
|
deleteit:
|
1997-01-06 06:02:03 +00:00
|
|
|
if (prev != NULL)
|
1995-02-17 02:22:57 +00:00
|
|
|
prev->ah_next = p->ah_next;
|
1997-01-06 06:02:03 +00:00
|
|
|
else
|
1995-02-17 02:22:57 +00:00
|
|
|
*list = p->ah_next;
|
|
|
|
nosuchnode:
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* APM driver calls some functions automatically */
|
1995-05-30 08:16:23 +00:00
|
|
|
static void
|
1995-02-17 02:22:57 +00:00
|
|
|
apm_execute_hook(struct apmhook *list)
|
|
|
|
{
|
|
|
|
struct apmhook *p;
|
|
|
|
|
|
|
|
for (p = list; p != NULL; p = p->ah_next) {
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
|
1997-01-06 06:02:03 +00:00
|
|
|
if ((*(p->ah_fun))(p->ah_arg))
|
1995-02-17 02:22:57 +00:00
|
|
|
printf("Warning: APM hook \"%s\" failed", p->ah_name);
|
1995-05-30 08:16:23 +00:00
|
|
|
}
|
1995-02-17 02:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* establish an apm hook */
|
|
|
|
struct apmhook *
|
|
|
|
apm_hook_establish(int apmh, struct apmhook *ah)
|
|
|
|
{
|
|
|
|
if (apmh < 0 || apmh >= NAPM_HOOK)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return apm_add_hook(&hook[apmh], ah);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* disestablish an apm hook */
|
1995-12-25 07:38:47 +00:00
|
|
|
void
|
1995-02-17 02:22:57 +00:00
|
|
|
apm_hook_disestablish(int apmh, struct apmhook *ah)
|
|
|
|
{
|
|
|
|
if (apmh < 0 || apmh >= NAPM_HOOK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
apm_del_hook(&hook[apmh], ah);
|
|
|
|
}
|
|
|
|
|
2002-03-20 05:48:58 +00:00
|
|
|
static int apm_record_event(struct apm_softc *, u_int);
|
1997-01-06 06:02:03 +00:00
|
|
|
static void apm_processevent(void);
|
1995-02-17 02:22:57 +00:00
|
|
|
|
1999-07-10 18:08:57 +00:00
|
|
|
static u_int apm_op_inprog = 0;
|
|
|
|
|
1999-08-22 14:48:00 +00:00
|
|
|
static void
|
|
|
|
apm_do_suspend(void)
|
|
|
|
{
|
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
int error;
|
|
|
|
|
2005-04-12 20:14:56 +00:00
|
|
|
if (sc == NULL || sc->initialized == 0)
|
1999-08-22 14:48:00 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
apm_op_inprog = 0;
|
|
|
|
sc->suspends = sc->suspend_countdown = 0;
|
|
|
|
|
2011-04-19 16:20:55 +00:00
|
|
|
EVENTHANDLER_INVOKE(power_suspend);
|
|
|
|
|
2007-11-14 05:43:55 +00:00
|
|
|
/*
|
|
|
|
* Be sure to hold Giant across DEVICE_SUSPEND/RESUME since
|
|
|
|
* non-MPSAFE drivers need this.
|
|
|
|
*/
|
|
|
|
mtx_lock(&Giant);
|
2005-04-12 20:14:56 +00:00
|
|
|
error = DEVICE_SUSPEND(root_bus);
|
2011-04-19 16:20:55 +00:00
|
|
|
if (error)
|
|
|
|
goto backout;
|
2005-04-12 20:14:56 +00:00
|
|
|
|
|
|
|
apm_execute_hook(hook[APM_HOOK_SUSPEND]);
|
|
|
|
if (apm_suspend_system(PMST_SUSPEND) == 0) {
|
|
|
|
sc->suspending = 1;
|
|
|
|
apm_processevent();
|
2011-04-19 16:30:17 +00:00
|
|
|
mtx_unlock(&Giant);
|
|
|
|
return;
|
1999-08-22 14:48:00 +00:00
|
|
|
}
|
2011-04-19 16:30:17 +00:00
|
|
|
|
|
|
|
/* Failure, 'resume' the system again */
|
|
|
|
apm_execute_hook(hook[APM_HOOK_RESUME]);
|
|
|
|
DEVICE_RESUME(root_bus);
|
2011-04-19 16:20:55 +00:00
|
|
|
backout:
|
2007-12-06 01:39:23 +00:00
|
|
|
mtx_unlock(&Giant);
|
2011-04-19 16:20:55 +00:00
|
|
|
EVENTHANDLER_INVOKE(power_resume);
|
1999-08-22 14:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
apm_do_standby(void)
|
|
|
|
{
|
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
2005-04-12 20:14:56 +00:00
|
|
|
if (sc == NULL || sc->initialized == 0)
|
1999-08-22 14:48:00 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
apm_op_inprog = 0;
|
|
|
|
sc->standbys = sc->standby_countdown = 0;
|
|
|
|
|
2005-04-12 20:14:56 +00:00
|
|
|
/*
|
|
|
|
* As far as standby, we don't need to execute
|
|
|
|
* all of suspend hooks.
|
|
|
|
*/
|
|
|
|
if (apm_suspend_system(PMST_STANDBY) == 0)
|
|
|
|
apm_processevent();
|
|
|
|
return;
|
1999-08-22 14:48:00 +00:00
|
|
|
}
|
|
|
|
|
1999-07-10 18:08:57 +00:00
|
|
|
static void
|
|
|
|
apm_lastreq_notify(void)
|
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1999-07-10 18:08:57 +00:00
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
|
|
|
|
sc->bios.r.ebx = PMDV_ALLDEV;
|
|
|
|
sc->bios.r.ecx = PMST_LASTREQNOTIFY;
|
|
|
|
sc->bios.r.edx = 0;
|
|
|
|
apm_bioscall();
|
1999-07-10 18:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
apm_lastreq_rejected(void)
|
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1999-07-10 18:08:57 +00:00
|
|
|
|
|
|
|
if (apm_op_inprog == 0) {
|
|
|
|
return 1; /* no operation in progress */
|
|
|
|
}
|
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
|
|
|
|
sc->bios.r.ebx = PMDV_ALLDEV;
|
|
|
|
sc->bios.r.ecx = PMST_LASTREQREJECT;
|
|
|
|
sc->bios.r.edx = 0;
|
1999-07-10 18:08:57 +00:00
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
if (apm_bioscall()) {
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("apm_lastreq_rejected: failed\n");
|
1999-07-10 18:08:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
apm_op_inprog = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-02-17 02:22:57 +00:00
|
|
|
/*
|
|
|
|
* Public interface to the suspend/resume:
|
|
|
|
*
|
|
|
|
* Execute suspend and resume hook before and after sleep, respectively.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
void
|
1998-07-06 06:29:07 +00:00
|
|
|
apm_suspend(int state)
|
1995-02-17 02:22:57 +00:00
|
|
|
{
|
1996-03-18 21:58:22 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1995-02-17 02:22:57 +00:00
|
|
|
|
2005-04-12 20:14:56 +00:00
|
|
|
if (sc == NULL || sc->initialized == 0)
|
1999-09-20 15:29:23 +00:00
|
|
|
return;
|
|
|
|
|
1999-08-22 14:48:00 +00:00
|
|
|
switch (state) {
|
|
|
|
case PMST_SUSPEND:
|
|
|
|
if (sc->suspends)
|
|
|
|
return;
|
|
|
|
sc->suspends++;
|
|
|
|
sc->suspend_countdown = apm_suspend_delay;
|
|
|
|
break;
|
|
|
|
case PMST_STANDBY:
|
|
|
|
if (sc->standbys)
|
|
|
|
return;
|
|
|
|
sc->standbys++;
|
|
|
|
sc->standby_countdown = apm_standby_delay;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
|
1995-02-17 02:22:57 +00:00
|
|
|
return;
|
|
|
|
}
|
1999-08-22 14:48:00 +00:00
|
|
|
|
|
|
|
apm_op_inprog++;
|
|
|
|
apm_lastreq_notify();
|
1995-02-17 02:22:57 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 08:57:14 +00:00
|
|
|
static void
|
1995-02-17 02:22:57 +00:00
|
|
|
apm_resume(void)
|
|
|
|
{
|
1996-03-18 21:58:22 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1995-02-17 02:22:57 +00:00
|
|
|
|
2005-04-12 20:14:56 +00:00
|
|
|
if (sc == NULL || sc->initialized == 0 || sc->suspending == 0)
|
2000-09-14 22:34:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
sc->suspending = 0;
|
2005-04-12 20:14:56 +00:00
|
|
|
apm_execute_hook(hook[APM_HOOK_RESUME]);
|
2007-11-14 05:43:55 +00:00
|
|
|
mtx_lock(&Giant);
|
2005-04-12 20:14:56 +00:00
|
|
|
DEVICE_RESUME(root_bus);
|
2007-11-14 05:43:55 +00:00
|
|
|
mtx_unlock(&Giant);
|
2011-04-19 16:20:55 +00:00
|
|
|
EVENTHANDLER_INVOKE(power_resume);
|
1995-02-17 02:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-13 17:05:27 +00:00
|
|
|
/* get power status per battery */
|
1994-10-01 02:56:21 +00:00
|
|
|
static int
|
2000-08-13 17:05:27 +00:00
|
|
|
apm_get_pwstatus(apm_pwstatus_t app)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1997-01-06 06:02:03 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1994-10-01 02:56:21 +00:00
|
|
|
|
2000-08-13 17:05:27 +00:00
|
|
|
if (app->ap_device != PMDV_ALLDEV &&
|
|
|
|
(app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
|
|
|
|
return 1;
|
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
|
2000-08-13 17:05:27 +00:00
|
|
|
sc->bios.r.ebx = app->ap_device;
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.ecx = 0;
|
2000-08-13 17:05:27 +00:00
|
|
|
sc->bios.r.edx = 0xffff; /* default to unknown battery time */
|
1994-10-02 01:45:42 +00:00
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
if (apm_bioscall())
|
1994-10-01 02:56:21 +00:00
|
|
|
return 1;
|
1994-10-02 01:45:42 +00:00
|
|
|
|
2000-08-13 17:05:27 +00:00
|
|
|
app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
|
|
|
|
app->ap_batt_stat = sc->bios.r.ebx & 0xff;
|
|
|
|
app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
|
|
|
|
app->ap_batt_life = sc->bios.r.ecx & 0xff;
|
|
|
|
sc->bios.r.edx &= 0xffff;
|
2003-06-22 05:08:10 +00:00
|
|
|
if (apm_swab_batt_minutes)
|
|
|
|
sc->bios.r.edx = __bswap16(sc->bios.r.edx) | 0x8000;
|
2000-08-13 17:05:27 +00:00
|
|
|
if (sc->bios.r.edx == 0xffff) /* Time is unknown */
|
|
|
|
app->ap_batt_time = -1;
|
|
|
|
else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
|
|
|
|
app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
|
|
|
|
else /* Time is in seconds */
|
|
|
|
app->ap_batt_time = sc->bios.r.edx;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* get APM information */
|
|
|
|
static int
|
|
|
|
apm_get_info(apm_info_t aip)
|
|
|
|
{
|
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
struct apm_pwstatus aps;
|
|
|
|
|
|
|
|
bzero(&aps, sizeof(aps));
|
|
|
|
aps.ap_device = PMDV_ALLDEV;
|
|
|
|
if (apm_get_pwstatus(&aps))
|
|
|
|
return 1;
|
|
|
|
|
1998-07-06 06:29:07 +00:00
|
|
|
aip->ai_infoversion = 1;
|
2000-08-13 17:05:27 +00:00
|
|
|
aip->ai_acline = aps.ap_acline;
|
|
|
|
aip->ai_batt_stat = aps.ap_batt_stat;
|
|
|
|
aip->ai_batt_life = aps.ap_batt_life;
|
|
|
|
aip->ai_batt_time = aps.ap_batt_time;
|
1997-11-12 04:12:51 +00:00
|
|
|
aip->ai_major = (u_int)sc->majorversion;
|
|
|
|
aip->ai_minor = (u_int)sc->minorversion;
|
|
|
|
aip->ai_status = (u_int)sc->active;
|
1998-07-06 06:29:07 +00:00
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
|
|
|
|
sc->bios.r.ebx = 0;
|
|
|
|
sc->bios.r.ecx = 0;
|
|
|
|
sc->bios.r.edx = 0;
|
|
|
|
if (apm_bioscall()) {
|
2004-06-16 20:27:50 +00:00
|
|
|
aip->ai_batteries = 0xffffffff; /* Unknown */
|
1998-07-06 06:29:07 +00:00
|
|
|
aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
|
|
|
|
} else {
|
1999-07-29 01:49:19 +00:00
|
|
|
aip->ai_batteries = sc->bios.r.ebx & 0xff;
|
2004-06-16 20:27:50 +00:00
|
|
|
aip->ai_capabilities = sc->bios.r.ecx & 0xff;
|
1998-07-06 06:29:07 +00:00
|
|
|
}
|
|
|
|
|
1997-11-12 04:12:51 +00:00
|
|
|
bzero(aip->ai_spare, sizeof aip->ai_spare);
|
1996-03-13 00:42:56 +00:00
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* inform APM BIOS that CPU is idle */
|
1995-12-25 07:38:47 +00:00
|
|
|
void
|
1994-10-01 02:56:21 +00:00
|
|
|
apm_cpu_idle(void)
|
|
|
|
{
|
1996-03-18 21:58:22 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1995-02-17 02:22:57 +00:00
|
|
|
|
1996-03-19 16:56:56 +00:00
|
|
|
if (sc->active) {
|
1996-03-19 16:48:38 +00:00
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
|
|
|
|
sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
|
|
|
|
(void) apm_bioscall();
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
/*
|
1995-05-30 08:16:23 +00:00
|
|
|
* Some APM implementation halts CPU in BIOS, whenever
|
1994-10-01 02:56:21 +00:00
|
|
|
* "CPU-idle" function are invoked, but swtch() of
|
|
|
|
* FreeBSD halts CPU, therefore, CPU is halted twice
|
|
|
|
* in the sched loop. It makes the interrupt latency
|
|
|
|
* terribly long and be able to cause a serious problem
|
|
|
|
* in interrupt processing. We prevent it by removing
|
|
|
|
* "hlt" operation from swtch() and managed it under
|
|
|
|
* APM driver.
|
|
|
|
*/
|
1997-01-06 06:02:03 +00:00
|
|
|
if (!sc->active || sc->always_halt_cpu)
|
2002-09-21 18:51:19 +00:00
|
|
|
halt(); /* wait for interrupt */
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* inform APM BIOS that CPU is busy */
|
1995-12-25 07:38:47 +00:00
|
|
|
void
|
1994-10-01 02:56:21 +00:00
|
|
|
apm_cpu_busy(void)
|
|
|
|
{
|
1996-03-18 21:58:22 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1995-02-17 02:22:57 +00:00
|
|
|
|
1996-03-19 16:56:56 +00:00
|
|
|
/*
|
|
|
|
* The APM specification says this is only necessary if your BIOS
|
|
|
|
* slows down the processor in the idle task, otherwise it's not
|
|
|
|
* necessary.
|
|
|
|
*/
|
|
|
|
if (sc->slow_idle_cpu && sc->active) {
|
1996-03-19 16:48:38 +00:00
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
|
|
|
|
sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
|
|
|
|
apm_bioscall();
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-05-25 23:06:38 +00:00
|
|
|
* APM thread loop.
|
1994-10-01 02:56:21 +00:00
|
|
|
*
|
2006-05-25 23:06:38 +00:00
|
|
|
* This routine wakes up from time to time to deal with delaying the
|
|
|
|
* suspend of the system, or other events.
|
1994-10-01 02:56:21 +00:00
|
|
|
*/
|
1995-05-30 08:16:23 +00:00
|
|
|
static void
|
2006-05-25 23:06:38 +00:00
|
|
|
apm_event_thread(void *arg)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1997-01-06 06:02:03 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1995-02-17 02:22:57 +00:00
|
|
|
|
2006-05-25 23:06:38 +00:00
|
|
|
sc->running = 1;
|
|
|
|
while (sc->active) {
|
|
|
|
if (apm_op_inprog)
|
|
|
|
apm_lastreq_notify();
|
|
|
|
if (sc->standbys && sc->standby_countdown-- <= 0)
|
|
|
|
apm_do_standby();
|
|
|
|
if (sc->suspends && sc->suspend_countdown-- <= 0)
|
|
|
|
apm_do_suspend();
|
|
|
|
if (!sc->bios_busy)
|
|
|
|
apm_processevent();
|
|
|
|
mtx_lock(&sc->mtx);
|
|
|
|
cv_timedwait(&sc->cv, &sc->mtx, 10 * hz / 9);
|
|
|
|
mtx_unlock(&sc->mtx);
|
|
|
|
}
|
|
|
|
sc->running = 0;
|
2007-10-20 23:23:23 +00:00
|
|
|
kproc_exit(0);
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enable APM BIOS */
|
1995-05-30 08:16:23 +00:00
|
|
|
static void
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_event_enable(void)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1997-01-06 06:02:03 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("called apm_event_enable()\n");
|
2005-04-12 20:14:56 +00:00
|
|
|
|
|
|
|
if (sc == NULL || sc->initialized == 0)
|
|
|
|
return;
|
|
|
|
|
2006-05-25 23:06:38 +00:00
|
|
|
/* Start the thread */
|
2005-04-12 20:14:56 +00:00
|
|
|
sc->active = 1;
|
2007-10-20 23:23:23 +00:00
|
|
|
if (kproc_create(apm_event_thread, sc, &sc->event_thread, 0, 0,
|
2006-05-25 23:06:38 +00:00
|
|
|
"apm worker"))
|
|
|
|
panic("Cannot create apm worker thread");
|
2005-04-12 20:14:56 +00:00
|
|
|
|
|
|
|
return;
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* disable APM BIOS */
|
1995-05-30 08:16:23 +00:00
|
|
|
static void
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_event_disable(void)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1997-01-06 06:02:03 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("called apm_event_disable()\n");
|
2005-04-12 20:14:56 +00:00
|
|
|
|
|
|
|
if (sc == NULL || sc->initialized == 0)
|
|
|
|
return;
|
|
|
|
|
2006-05-25 23:06:38 +00:00
|
|
|
mtx_lock(&sc->mtx);
|
2005-04-12 20:14:56 +00:00
|
|
|
sc->active = 0;
|
2006-05-25 23:06:38 +00:00
|
|
|
while (sc->running) {
|
|
|
|
cv_broadcast(&sc->cv);
|
|
|
|
msleep(sc->event_thread, &sc->mtx, PWAIT, "apmdie", 0);
|
|
|
|
}
|
|
|
|
mtx_unlock(&sc->mtx);
|
|
|
|
sc->event_thread = NULL;
|
2005-04-12 20:14:56 +00:00
|
|
|
return;
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* halt CPU in scheduling loop */
|
1995-05-30 08:16:23 +00:00
|
|
|
static void
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_halt_cpu(void)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1997-01-06 06:02:03 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
2005-04-12 20:14:56 +00:00
|
|
|
if (sc == NULL || sc->initialized == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sc->always_halt_cpu = 1;
|
|
|
|
|
|
|
|
return;
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* don't halt CPU in scheduling loop */
|
1995-05-30 08:16:23 +00:00
|
|
|
static void
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_not_halt_cpu(void)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1997-01-06 06:02:03 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
2005-04-12 20:14:56 +00:00
|
|
|
if (sc == NULL || sc->initialized == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sc->always_halt_cpu = 0;
|
|
|
|
|
|
|
|
return;
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* device driver definitions */
|
|
|
|
|
2001-11-01 16:34:07 +00:00
|
|
|
/*
|
|
|
|
* Module event
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
apm_modevent(struct module *mod, int event, void *junk)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
if (!cold)
|
|
|
|
return (EPERM);
|
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
if (!cold && power_pm_get_type() == POWER_PM_TYPE_APM)
|
|
|
|
return (EBUSY);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-08-22 19:51:33 +00:00
|
|
|
/*
|
|
|
|
* Create "connection point"
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
apm_identify(driver_t *driver, device_t parent)
|
|
|
|
{
|
|
|
|
device_t child;
|
|
|
|
|
2001-11-01 16:34:07 +00:00
|
|
|
if (!cold) {
|
|
|
|
printf("Don't load this driver from userland!!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-22 13:06:09 +00:00
|
|
|
if (resource_disabled("apm", 0))
|
|
|
|
return;
|
|
|
|
|
1999-08-22 19:51:33 +00:00
|
|
|
child = BUS_ADD_CHILD(parent, 0, "apm", 0);
|
|
|
|
if (child == NULL)
|
|
|
|
panic("apm_identify");
|
|
|
|
}
|
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
/*
|
1999-07-29 01:49:19 +00:00
|
|
|
* probe for APM BIOS
|
1994-10-01 02:56:21 +00:00
|
|
|
*/
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1999-04-16 21:22:55 +00:00
|
|
|
apm_probe(device_t dev)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1999-07-29 01:49:19 +00:00
|
|
|
#define APM_KERNBASE KERNBASE
|
1998-06-03 01:59:42 +00:00
|
|
|
struct vm86frame vmf;
|
1999-07-29 01:49:19 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
int rid;
|
|
|
|
#endif
|
1999-04-18 15:10:58 +00:00
|
|
|
|
1999-04-16 21:22:55 +00:00
|
|
|
device_set_desc(dev, "APM BIOS");
|
2001-03-13 05:56:48 +00:00
|
|
|
if (device_get_unit(dev) > 0) {
|
1996-03-18 21:58:22 +00:00
|
|
|
printf("apm: Only one APM driver supported.\n");
|
1999-04-16 21:22:55 +00:00
|
|
|
return ENXIO;
|
1996-03-18 21:58:22 +00:00
|
|
|
}
|
1998-06-03 01:59:42 +00:00
|
|
|
|
2001-11-01 16:34:07 +00:00
|
|
|
if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
|
|
|
|
power_pm_get_type() != POWER_PM_TYPE_APM) {
|
|
|
|
printf("apm: Other PM system enabled.\n");
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
1998-06-03 01:59:42 +00:00
|
|
|
bzero(&vmf, sizeof(struct vm86frame)); /* safety */
|
1999-07-29 01:49:19 +00:00
|
|
|
bzero(&apm_softc, sizeof(apm_softc));
|
|
|
|
vmf.vmf_ah = APM_BIOS;
|
|
|
|
vmf.vmf_al = APM_INSTCHECK;
|
1998-06-03 01:59:42 +00:00
|
|
|
vmf.vmf_bx = 0;
|
1999-07-29 01:49:19 +00:00
|
|
|
if (vm86_intcall(APM_INT, &vmf))
|
|
|
|
return ENXIO; /* APM not found */
|
|
|
|
if (vmf.vmf_bx != 0x504d) {
|
|
|
|
printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
|
|
|
|
printf("apm: protected mode connections are not supported\n");
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
apm_version = vmf.vmf_ax;
|
|
|
|
sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
|
|
|
|
sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
|
|
|
|
sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
|
1998-06-03 01:59:42 +00:00
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
vmf.vmf_ah = APM_BIOS;
|
|
|
|
vmf.vmf_al = APM_DISCONNECT;
|
|
|
|
vmf.vmf_bx = 0;
|
|
|
|
vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
|
1998-06-03 01:59:42 +00:00
|
|
|
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
/* PC98 have bogos APM 32bit BIOS */
|
|
|
|
if ((vmf.vmf_cx & APM_32BIT_SUPPORT) == 0)
|
|
|
|
return ENXIO;
|
|
|
|
rid = 0;
|
|
|
|
bus_set_resource(dev, SYS_RES_IOPORT, rid,
|
|
|
|
APM_NECSMM_PORT, APM_NECSMM_PORTSZ);
|
|
|
|
sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
|
|
|
|
APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
|
|
|
|
if (sc->sc_res == NULL) {
|
|
|
|
printf("apm: cannot open NEC smm device\n");
|
|
|
|
return ENXIO;
|
|
|
|
}
|
|
|
|
bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_res);
|
|
|
|
|
|
|
|
vmf.vmf_ah = APM_BIOS;
|
|
|
|
vmf.vmf_al = APM_PROT32CONNECT;
|
|
|
|
vmf.vmf_bx = 0;
|
|
|
|
if (vm86_intcall(APM_INT, &vmf)) {
|
|
|
|
printf("apm: 32-bit connection error.\n");
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
|
|
|
|
sc->bios.seg.code32.limit = 0xffff;
|
|
|
|
sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
|
|
|
|
sc->bios.seg.code16.limit = 0xffff;
|
|
|
|
sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
|
|
|
|
sc->bios.seg.data.limit = 0xffff;
|
|
|
|
sc->bios.entry = vmf.vmf_ebx;
|
|
|
|
sc->connectmode = APM_PROT32CONNECT;
|
|
|
|
#else
|
1999-07-29 01:49:19 +00:00
|
|
|
if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
|
|
|
|
vmf.vmf_ah = APM_BIOS;
|
|
|
|
vmf.vmf_al = APM_PROT32CONNECT;
|
1998-06-03 01:59:42 +00:00
|
|
|
vmf.vmf_bx = 0;
|
1999-07-29 01:49:19 +00:00
|
|
|
if (vm86_intcall(APM_INT, &vmf)) {
|
|
|
|
printf("apm: 32-bit connection error.\n");
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
|
|
|
|
sc->bios.seg.code32.limit = 0xffff;
|
|
|
|
sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
|
1999-08-02 18:46:34 +00:00
|
|
|
sc->bios.seg.code16.limit = 0xffff;
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
|
1999-08-02 18:46:34 +00:00
|
|
|
sc->bios.seg.data.limit = 0xffff;
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.entry = vmf.vmf_ebx;
|
|
|
|
sc->connectmode = APM_PROT32CONNECT;
|
|
|
|
} else {
|
|
|
|
/* use 16-bit connection */
|
|
|
|
vmf.vmf_ah = APM_BIOS;
|
|
|
|
vmf.vmf_al = APM_PROT16CONNECT;
|
|
|
|
vmf.vmf_bx = 0;
|
|
|
|
if (vm86_intcall(APM_INT, &vmf)) {
|
|
|
|
printf("apm: 16-bit connection error.\n");
|
|
|
|
return (ENXIO);
|
1998-06-03 01:59:42 +00:00
|
|
|
}
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
|
1999-08-02 18:46:34 +00:00
|
|
|
sc->bios.seg.code16.limit = 0xffff;
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
|
1999-08-02 18:46:34 +00:00
|
|
|
sc->bios.seg.data.limit = 0xffff;
|
1999-07-29 01:49:19 +00:00
|
|
|
sc->bios.entry = vmf.vmf_bx;
|
|
|
|
sc->connectmode = APM_PROT16CONNECT;
|
1998-06-03 01:59:42 +00:00
|
|
|
}
|
2003-03-25 05:19:18 +00:00
|
|
|
#endif
|
1999-07-29 01:49:19 +00:00
|
|
|
return(0);
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
|
1999-07-10 18:08:57 +00:00
|
|
|
/*
|
|
|
|
* return 0 if the user will notice and handle the event,
|
|
|
|
* return 1 if the kernel driver should do so.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
apm_record_event(struct apm_softc *sc, u_int event_type)
|
|
|
|
{
|
|
|
|
struct apm_event_info *evp;
|
|
|
|
|
|
|
|
if ((sc->sc_flags & SCFLAG_OPEN) == 0)
|
|
|
|
return 1; /* no user waiting */
|
|
|
|
if (sc->event_count == APM_NEVENTS)
|
|
|
|
return 1; /* overflow */
|
|
|
|
if (sc->event_filter[event_type] == 0)
|
|
|
|
return 1; /* not registered */
|
|
|
|
evp = &sc->event_list[sc->event_ptr];
|
|
|
|
sc->event_count++;
|
|
|
|
sc->event_ptr++;
|
|
|
|
sc->event_ptr %= APM_NEVENTS;
|
|
|
|
evp->type = event_type;
|
|
|
|
evp->index = ++apm_evindex;
|
2003-11-09 09:17:26 +00:00
|
|
|
selwakeuppri(&sc->sc_rsel, PZERO);
|
1999-07-10 18:08:57 +00:00
|
|
|
return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
|
|
|
|
}
|
1994-10-01 02:56:21 +00:00
|
|
|
|
2002-03-04 18:46:13 +00:00
|
|
|
/* Power profile */
|
|
|
|
static void
|
|
|
|
apm_power_profile(struct apm_softc *sc)
|
|
|
|
{
|
|
|
|
int state;
|
|
|
|
struct apm_info info;
|
|
|
|
static int apm_acline = 0;
|
|
|
|
|
|
|
|
if (apm_get_info(&info))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (apm_acline != info.ai_acline) {
|
|
|
|
apm_acline = info.ai_acline;
|
|
|
|
state = apm_acline ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY;
|
|
|
|
power_profile_set_state(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
/* Process APM event */
|
|
|
|
static void
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_processevent(void)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1994-10-02 17:40:38 +00:00
|
|
|
int apm_event;
|
1999-07-10 18:08:57 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1994-10-01 02:56:21 +00:00
|
|
|
|
2000-08-04 20:28:53 +00:00
|
|
|
#define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
|
|
|
|
APM_DPRINT("Received APM Event: " #symbol "\n");
|
|
|
|
|
1996-04-18 19:21:47 +00:00
|
|
|
do {
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_event = apm_getevent();
|
1994-10-01 02:56:21 +00:00
|
|
|
switch (apm_event) {
|
1994-10-02 17:40:38 +00:00
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
|
1999-07-10 18:08:57 +00:00
|
|
|
if (apm_op_inprog == 0) {
|
|
|
|
apm_op_inprog++;
|
|
|
|
if (apm_record_event(sc, apm_event)) {
|
|
|
|
apm_suspend(PMST_STANDBY);
|
1999-09-11 15:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
|
|
|
|
if (apm_op_inprog == 0) {
|
|
|
|
apm_op_inprog++;
|
|
|
|
if (apm_record_event(sc, apm_event)) {
|
|
|
|
apm_suspend(PMST_STANDBY);
|
1999-07-10 18:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
1994-10-02 17:40:38 +00:00
|
|
|
break;
|
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
|
1999-07-10 18:08:57 +00:00
|
|
|
apm_lastreq_notify();
|
|
|
|
if (apm_op_inprog == 0) {
|
|
|
|
apm_op_inprog++;
|
|
|
|
if (apm_record_event(sc, apm_event)) {
|
1999-08-22 14:48:00 +00:00
|
|
|
apm_do_suspend();
|
1999-07-10 18:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return; /* XXX skip the rest */
|
1994-10-02 17:40:38 +00:00
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
|
1999-07-10 18:08:57 +00:00
|
|
|
apm_lastreq_notify();
|
|
|
|
if (apm_op_inprog == 0) {
|
|
|
|
apm_op_inprog++;
|
|
|
|
if (apm_record_event(sc, apm_event)) {
|
1999-08-22 14:48:00 +00:00
|
|
|
apm_do_suspend();
|
1999-07-10 18:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return; /* XXX skip the rest */
|
1994-10-02 17:40:38 +00:00
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
|
1999-08-22 14:48:00 +00:00
|
|
|
apm_do_suspend();
|
1994-10-01 02:56:21 +00:00
|
|
|
break;
|
1994-10-02 17:40:38 +00:00
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
|
1999-07-10 18:08:57 +00:00
|
|
|
apm_record_event(sc, apm_event);
|
1995-02-17 02:22:57 +00:00
|
|
|
apm_resume();
|
1994-10-02 17:40:38 +00:00
|
|
|
break;
|
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
|
1999-07-10 18:08:57 +00:00
|
|
|
apm_record_event(sc, apm_event);
|
1995-02-17 02:22:57 +00:00
|
|
|
apm_resume();
|
1994-10-02 17:40:38 +00:00
|
|
|
break;
|
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
|
1999-07-10 18:08:57 +00:00
|
|
|
apm_record_event(sc, apm_event);
|
1994-10-02 17:40:38 +00:00
|
|
|
break;
|
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
|
1999-07-10 18:08:57 +00:00
|
|
|
if (apm_record_event(sc, apm_event)) {
|
|
|
|
apm_battery_low();
|
|
|
|
apm_suspend(PMST_SUSPEND);
|
|
|
|
}
|
1994-10-01 02:56:21 +00:00
|
|
|
break;
|
1994-10-02 17:40:38 +00:00
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
|
1999-07-10 18:08:57 +00:00
|
|
|
apm_record_event(sc, apm_event);
|
2002-03-04 18:46:13 +00:00
|
|
|
apm_power_profile(sc);
|
1994-10-01 02:56:21 +00:00
|
|
|
break;
|
1994-10-02 17:40:38 +00:00
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
|
1999-07-10 18:08:57 +00:00
|
|
|
apm_record_event(sc, apm_event);
|
1994-10-01 02:56:21 +00:00
|
|
|
inittodr(0); /* adjust time to RTC */
|
|
|
|
break;
|
1999-09-11 16:00:28 +00:00
|
|
|
OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
|
|
|
|
apm_record_event(sc, apm_event);
|
2002-03-04 18:46:13 +00:00
|
|
|
apm_power_profile(sc);
|
1999-09-11 16:00:28 +00:00
|
|
|
break;
|
1997-03-29 11:07:12 +00:00
|
|
|
case PMEV_NOEVENT:
|
1996-04-18 19:21:47 +00:00
|
|
|
break;
|
1994-10-02 17:40:38 +00:00
|
|
|
default:
|
|
|
|
printf("Unknown Original APM Event 0x%x\n", apm_event);
|
|
|
|
break;
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
1996-04-18 19:21:47 +00:00
|
|
|
} while (apm_event != PMEV_NOEVENT);
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
apm_disable_smm(sc);
|
|
|
|
#endif
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach APM:
|
|
|
|
*
|
1999-07-28 19:37:32 +00:00
|
|
|
* Initialize APM driver
|
1994-10-01 02:56:21 +00:00
|
|
|
*/
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
1999-04-16 21:22:55 +00:00
|
|
|
apm_attach(device_t dev)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1996-03-18 21:58:22 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1999-07-29 01:49:19 +00:00
|
|
|
int drv_version;
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
int rid;
|
|
|
|
#endif
|
2006-05-25 23:06:38 +00:00
|
|
|
mtx_init(&sc->mtx, device_get_nameunit(dev), "apm", MTX_DEF);
|
|
|
|
cv_init(&sc->cv, "cbb cv");
|
1999-04-16 21:22:55 +00:00
|
|
|
|
2009-05-04 08:41:54 +00:00
|
|
|
#ifndef PC98
|
2004-10-14 22:21:59 +00:00
|
|
|
if (device_get_flags(dev) & 0x20)
|
2009-05-03 17:47:21 +00:00
|
|
|
atrtcclock_disable = 1;
|
2009-05-04 08:36:47 +00:00
|
|
|
#endif
|
1999-07-29 01:49:19 +00:00
|
|
|
|
1995-02-17 02:22:57 +00:00
|
|
|
sc->initialized = 0;
|
1996-03-18 22:29:48 +00:00
|
|
|
|
|
|
|
/* Must be externally enabled */
|
1995-02-17 02:22:57 +00:00
|
|
|
sc->active = 0;
|
1994-10-01 02:56:21 +00:00
|
|
|
|
1996-03-19 16:56:56 +00:00
|
|
|
/* Always call HLT in idle loop */
|
|
|
|
sc->always_halt_cpu = 1;
|
|
|
|
|
2000-08-04 20:28:53 +00:00
|
|
|
getenv_int("debug.apm_debug", &apm_debug);
|
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
/* print bootstrap messages */
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
|
|
|
|
APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
|
|
|
|
sc->bios.seg.code16.base, sc->bios.seg.data.base);
|
|
|
|
APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
|
|
|
|
sc->bios.entry, is_enabled(sc->slow_idle_cpu),
|
|
|
|
is_enabled(!sc->disabled));
|
|
|
|
APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
|
|
|
|
sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
|
1994-10-01 02:56:21 +00:00
|
|
|
|
2003-03-25 05:19:18 +00:00
|
|
|
#ifdef PC98
|
|
|
|
rid = 0;
|
|
|
|
sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
|
|
|
|
APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
|
|
|
|
if (sc->sc_res == NULL)
|
|
|
|
panic("%s: counldn't map I/O ports", device_get_name(dev));
|
|
|
|
sc->sc_iot = rman_get_bustag(sc->sc_res);
|
|
|
|
sc->sc_ioh = rman_get_bushandle(sc->sc_res);
|
|
|
|
|
|
|
|
if (apm_version==0x112 || apm_version==0x111 || apm_version==0x110)
|
|
|
|
apm_necsmm_addr = APM_NECSMM_PORT;
|
|
|
|
else
|
|
|
|
apm_necsmm_addr = 0;
|
|
|
|
apm_necsmm_mask = ~APM_NECSMM_EN;
|
|
|
|
#endif /* PC98 */
|
|
|
|
|
1999-07-29 01:49:19 +00:00
|
|
|
/*
|
1999-12-02 03:13:11 +00:00
|
|
|
* In one test, apm bios version was 1.02; an attempt to register
|
|
|
|
* a 1.04 driver resulted in a 1.00 connection! Registering a
|
|
|
|
* 1.02 driver resulted in a 1.02 connection.
|
|
|
|
*/
|
1999-07-29 01:49:19 +00:00
|
|
|
drv_version = apm_version > 0x102 ? 0x102 : apm_version;
|
|
|
|
for (; drv_version > 0x100; drv_version--)
|
|
|
|
if (apm_driver_version(drv_version) == 0)
|
|
|
|
break;
|
|
|
|
sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
|
|
|
|
((drv_version & 0x000f) >> 0);
|
|
|
|
sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
|
|
|
|
((apm_version & 0x0f00) >> 8);
|
1994-10-02 17:40:38 +00:00
|
|
|
|
1995-02-17 02:22:57 +00:00
|
|
|
sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
|
1994-10-02 17:40:38 +00:00
|
|
|
|
1997-01-06 06:02:03 +00:00
|
|
|
if (sc->intversion >= INTVERSION(1, 1))
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
|
|
|
|
device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
|
1999-07-29 01:49:19 +00:00
|
|
|
((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
|
|
|
|
((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
|
|
|
|
sc->majorversion, sc->minorversion);
|
1996-04-23 19:59:14 +00:00
|
|
|
|
1994-10-02 17:40:38 +00:00
|
|
|
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
|
1994-10-01 02:56:21 +00:00
|
|
|
/* enable power management */
|
1995-02-17 02:22:57 +00:00
|
|
|
if (sc->disabled) {
|
1997-01-06 06:02:03 +00:00
|
|
|
if (apm_enable_disable_pm(1)) {
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
|
|
|
|
(sc->bios.r.eax >> 8) & 0xff);
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* engage power managment (APM 1.1 or later) */
|
1995-02-17 02:22:57 +00:00
|
|
|
if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
|
1997-01-06 06:02:03 +00:00
|
|
|
if (apm_engage_disengage_pm(1)) {
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
|
|
|
|
(sc->bios.r.eax >> 8) & 0xff);
|
|
|
|
APM_DPRINT(" (Docked or using external power?).\n");
|
1994-10-01 02:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-30 05:41:15 +00:00
|
|
|
/* Power the system off using APM */
|
1999-08-21 06:24:40 +00:00
|
|
|
EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
|
|
|
|
SHUTDOWN_PRI_LAST);
|
1998-10-30 05:41:15 +00:00
|
|
|
|
2001-11-01 16:34:07 +00:00
|
|
|
/* Register APM again to pass the correct argument of pm_func. */
|
|
|
|
power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc);
|
|
|
|
|
1995-02-17 02:22:57 +00:00
|
|
|
sc->initialized = 1;
|
2000-09-14 22:34:57 +00:00
|
|
|
sc->suspending = 0;
|
2006-05-25 23:06:38 +00:00
|
|
|
sc->running = 0;
|
1995-02-17 02:22:57 +00:00
|
|
|
|
2009-10-31 10:38:30 +00:00
|
|
|
make_dev(&apm_cdevsw, APMDEV_NORMAL,
|
|
|
|
UID_ROOT, GID_OPERATOR, 0664, "apm");
|
|
|
|
make_dev(&apm_cdevsw, APMDEV_CTL,
|
|
|
|
UID_ROOT, GID_OPERATOR, 0660, "apmctl");
|
1994-10-01 02:56:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
apmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1996-03-18 21:58:22 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1995-02-17 02:22:57 +00:00
|
|
|
|
2005-04-12 20:14:56 +00:00
|
|
|
if (sc == NULL || sc->initialized == 0)
|
1996-03-18 21:58:22 +00:00
|
|
|
return (ENXIO);
|
|
|
|
|
2009-10-31 10:38:30 +00:00
|
|
|
switch (dev2unit(dev)) {
|
1999-07-10 18:08:57 +00:00
|
|
|
case APMDEV_CTL:
|
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return EINVAL;
|
|
|
|
if (sc->sc_flags & SCFLAG_OCTL)
|
|
|
|
return EBUSY;
|
|
|
|
sc->sc_flags |= SCFLAG_OCTL;
|
|
|
|
bzero(sc->event_filter, sizeof sc->event_filter);
|
|
|
|
break;
|
|
|
|
case APMDEV_NORMAL:
|
|
|
|
sc->sc_flags |= SCFLAG_ONORMAL;
|
|
|
|
break;
|
|
|
|
}
|
1994-10-01 02:56:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
apmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1999-07-10 18:08:57 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
|
2009-10-31 10:38:30 +00:00
|
|
|
switch (dev2unit(dev)) {
|
1999-07-10 18:08:57 +00:00
|
|
|
case APMDEV_CTL:
|
|
|
|
apm_lastreq_rejected();
|
|
|
|
sc->sc_flags &= ~SCFLAG_OCTL;
|
|
|
|
bzero(sc->event_filter, sizeof sc->event_filter);
|
|
|
|
break;
|
|
|
|
case APMDEV_NORMAL:
|
|
|
|
sc->sc_flags &= ~SCFLAG_ONORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
|
|
|
|
sc->event_count = 0;
|
|
|
|
sc->event_ptr = 0;
|
|
|
|
}
|
1994-10-01 02:56:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
|
1994-10-01 02:56:21 +00:00
|
|
|
{
|
1996-03-18 21:58:22 +00:00
|
|
|
struct apm_softc *sc = &apm_softc;
|
1999-07-30 19:35:03 +00:00
|
|
|
struct apm_bios_arg *args;
|
1994-10-01 02:56:21 +00:00
|
|
|
int error = 0;
|
1999-08-14 18:39:40 +00:00
|
|
|
int ret;
|
1997-01-06 06:02:03 +00:00
|
|
|
int newstate;
|
1994-10-01 02:56:21 +00:00
|
|
|
|
2005-04-12 20:14:56 +00:00
|
|
|
if (sc == NULL || sc->initialized == 0)
|
1996-03-18 22:29:48 +00:00
|
|
|
return (ENXIO);
|
2005-04-12 20:14:56 +00:00
|
|
|
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
|
1994-10-01 02:56:21 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case APMIO_SUSPEND:
|
2000-07-19 06:32:00 +00:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1998-07-06 06:29:07 +00:00
|
|
|
if (sc->active)
|
|
|
|
apm_suspend(PMST_SUSPEND);
|
|
|
|
else
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case APMIO_STANDBY:
|
2000-07-19 06:32:00 +00:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1998-07-06 06:29:07 +00:00
|
|
|
if (sc->active)
|
|
|
|
apm_suspend(PMST_STANDBY);
|
1997-01-06 06:02:03 +00:00
|
|
|
else
|
1996-03-18 23:30:14 +00:00
|
|
|
error = EINVAL;
|
1994-10-01 02:56:21 +00:00
|
|
|
break;
|
1998-07-06 06:29:07 +00:00
|
|
|
|
1997-11-12 04:12:51 +00:00
|
|
|
case APMIO_GETINFO_OLD:
|
|
|
|
{
|
|
|
|
struct apm_info info;
|
|
|
|
apm_info_old_t aiop;
|
|
|
|
|
|
|
|
if (apm_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;
|
1994-10-01 02:56:21 +00:00
|
|
|
case APMIO_GETINFO:
|
1997-01-06 06:02:03 +00:00
|
|
|
if (apm_get_info((apm_info_t)addr))
|
1994-10-01 02:56:21 +00:00
|
|
|
error = ENXIO;
|
|
|
|
break;
|
2000-08-13 17:05:27 +00:00
|
|
|
case APMIO_GETPWSTATUS:
|
|
|
|
if (apm_get_pwstatus((apm_pwstatus_t)addr))
|
|
|
|
error = ENXIO;
|
|
|
|
break;
|
1994-10-01 02:56:21 +00:00
|
|
|
case APMIO_ENABLE:
|
2000-07-19 06:32:00 +00:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_event_enable();
|
1994-10-01 02:56:21 +00:00
|
|
|
break;
|
|
|
|
case APMIO_DISABLE:
|
2000-07-19 06:32:00 +00:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_event_disable();
|
1994-10-01 02:56:21 +00:00
|
|
|
break;
|
|
|
|
case APMIO_HALTCPU:
|
2000-07-19 06:32:00 +00:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_halt_cpu();
|
1994-10-01 02:56:21 +00:00
|
|
|
break;
|
|
|
|
case APMIO_NOTHALTCPU:
|
2000-07-19 06:32:00 +00:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1997-01-06 06:02:03 +00:00
|
|
|
apm_not_halt_cpu();
|
1995-02-17 02:22:57 +00:00
|
|
|
break;
|
1997-01-06 06:02:03 +00:00
|
|
|
case APMIO_DISPLAY:
|
2000-07-19 06:32:00 +00:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1997-01-06 06:02:03 +00:00
|
|
|
newstate = *(int *)addr;
|
|
|
|
if (apm_display(newstate))
|
1995-02-17 02:22:57 +00:00
|
|
|
error = ENXIO;
|
1994-10-01 02:56:21 +00:00
|
|
|
break;
|
1999-07-30 19:35:03 +00:00
|
|
|
case APMIO_BIOS:
|
2000-07-19 06:32:00 +00:00
|
|
|
if (!(flag & FWRITE))
|
|
|
|
return (EPERM);
|
1999-07-30 19:35:03 +00:00
|
|
|
/* XXX compatibility with the old interface */
|
|
|
|
args = (struct apm_bios_arg *)addr;
|
2010-11-11 19:20:33 +00:00
|
|
|
#ifdef PC98
|
|
|
|
if (((args->eax >> 8) & 0xff) == 0x53) {
|
|
|
|
sc->bios.r.eax = args->eax & ~0xffff;
|
|
|
|
sc->bios.r.eax |= APM_BIOS << 8;
|
|
|
|
switch (args->eax & 0xff) {
|
|
|
|
case 0x0a:
|
|
|
|
sc->bios.r.eax |= APM_GETPWSTATUS;
|
|
|
|
break;
|
|
|
|
case 0x0e:
|
|
|
|
sc->bios.r.eax |= APM_DRVVERSION;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sc->bios.r.eax |= args->eax & 0xff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
1999-07-30 19:35:03 +00:00
|
|
|
sc->bios.r.eax = args->eax;
|
|
|
|
sc->bios.r.ebx = args->ebx;
|
|
|
|
sc->bios.r.ecx = args->ecx;
|
|
|
|
sc->bios.r.edx = args->edx;
|
|
|
|
sc->bios.r.esi = args->esi;
|
|
|
|
sc->bios.r.edi = args->edi;
|
1999-08-14 18:39:40 +00:00
|
|
|
if ((ret = apm_bioscall())) {
|
|
|
|
/*
|
|
|
|
* Return code 1 means bios call was unsuccessful.
|
|
|
|
* Error code is stored in %ah.
|
|
|
|
* Return code -1 means bios call was unsupported
|
|
|
|
* in the APM BIOS version.
|
|
|
|
*/
|
|
|
|
if (ret == -1) {
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Return code 0 means bios call was successful.
|
|
|
|
* We need only %al and can discard %ah.
|
|
|
|
*/
|
1999-07-30 19:35:03 +00:00
|
|
|
sc->bios.r.eax &= 0xff;
|
1999-08-14 18:39:40 +00:00
|
|
|
}
|
1999-07-30 19:35:03 +00:00
|
|
|
args->eax = sc->bios.r.eax;
|
|
|
|
args->ebx = sc->bios.r.ebx;
|
|
|
|
args->ecx = sc->bios.r.ecx;
|
|
|
|
args->edx = sc->bios.r.edx;
|
|
|
|
args->esi = sc->bios.r.esi;
|
|
|
|
args->edi = sc->bios.r.edi;
|
|
|
|
break;
|
1994-10-01 02:56:21 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
1999-07-10 18:08:57 +00:00
|
|
|
|
|
|
|
/* for /dev/apmctl */
|
2009-10-31 10:38:30 +00:00
|
|
|
if (dev2unit(dev) == APMDEV_CTL) {
|
1999-07-10 18:08:57 +00:00
|
|
|
struct apm_event_info *evp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
switch (cmd) {
|
|
|
|
case APMIO_NEXTEVENT:
|
|
|
|
if (!sc->event_count) {
|
|
|
|
error = EAGAIN;
|
|
|
|
} else {
|
|
|
|
evp = (struct apm_event_info *)addr;
|
|
|
|
i = sc->event_ptr + APM_NEVENTS - sc->event_count;
|
|
|
|
i %= APM_NEVENTS;
|
|
|
|
*evp = sc->event_list[i];
|
|
|
|
sc->event_count--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case APMIO_REJECTLASTREQ:
|
|
|
|
if (apm_lastreq_rejected()) {
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-10-01 02:56:21 +00:00
|
|
|
return error;
|
|
|
|
}
|
1995-11-29 10:49:16 +00:00
|
|
|
|
1999-07-10 18:08:57 +00:00
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
|
1999-07-10 18:08:57 +00:00
|
|
|
{
|
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
u_int event_type;
|
|
|
|
int error;
|
|
|
|
u_char enabled;
|
|
|
|
|
2009-10-31 10:38:30 +00:00
|
|
|
if (dev2unit(dev) != APMDEV_CTL)
|
1999-07-10 18:08:57 +00:00
|
|
|
return(ENODEV);
|
|
|
|
if (uio->uio_resid != sizeof(u_int))
|
|
|
|
return(E2BIG);
|
|
|
|
|
|
|
|
if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
|
|
|
|
return(error);
|
|
|
|
|
|
|
|
if (event_type < 0 || event_type >= APM_NPMEV)
|
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
if (sc->event_filter[event_type] == 0) {
|
|
|
|
enabled = 1;
|
|
|
|
} else {
|
|
|
|
enabled = 0;
|
|
|
|
}
|
|
|
|
sc->event_filter[event_type] = enabled;
|
2000-08-04 20:28:53 +00:00
|
|
|
APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
|
1999-07-10 18:08:57 +00:00
|
|
|
|
|
|
|
return uio->uio_resid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-16 09:47:26 +00:00
|
|
|
apmpoll(struct cdev *dev, int events, struct thread *td)
|
1999-07-10 18:08:57 +00:00
|
|
|
{
|
|
|
|
struct apm_softc *sc = &apm_softc;
|
|
|
|
int revents = 0;
|
|
|
|
|
1999-07-22 14:45:22 +00:00
|
|
|
if (events & (POLLIN | POLLRDNORM)) {
|
|
|
|
if (sc->event_count) {
|
1999-07-10 18:08:57 +00:00
|
|
|
revents |= events & (POLLIN | POLLRDNORM);
|
1999-07-22 14:45:22 +00:00
|
|
|
} else {
|
2001-09-12 08:38:13 +00:00
|
|
|
selrecord(td, &sc->sc_rsel);
|
1999-07-22 14:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
1999-07-10 18:08:57 +00:00
|
|
|
|
|
|
|
return (revents);
|
|
|
|
}
|
|
|
|
|
1999-04-16 21:22:55 +00:00
|
|
|
static device_method_t apm_methods[] = {
|
|
|
|
/* Device interface */
|
1999-08-22 19:51:33 +00:00
|
|
|
DEVMETHOD(device_identify, apm_identify),
|
1999-04-16 21:22:55 +00:00
|
|
|
DEVMETHOD(device_probe, apm_probe),
|
|
|
|
DEVMETHOD(device_attach, apm_attach),
|
1995-11-29 10:49:16 +00:00
|
|
|
|
1999-04-16 21:22:55 +00:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
1995-11-28 09:44:42 +00:00
|
|
|
|
1999-04-16 21:22:55 +00:00
|
|
|
static driver_t apm_driver = {
|
|
|
|
"apm",
|
|
|
|
apm_methods,
|
|
|
|
1, /* no softc (XXX) */
|
|
|
|
};
|
1995-11-29 10:49:16 +00:00
|
|
|
|
1999-04-16 21:22:55 +00:00
|
|
|
static devclass_t apm_devclass;
|
1995-11-29 10:49:16 +00:00
|
|
|
|
2002-09-23 15:56:09 +00:00
|
|
|
DRIVER_MODULE(apm, legacy, apm_driver, apm_devclass, apm_modevent, 0);
|
2001-11-01 16:34:07 +00:00
|
|
|
MODULE_VERSION(apm, 1);
|
|
|
|
|
|
|
|
static int
|
|
|
|
apm_pm_func(u_long cmd, void *arg, ...)
|
|
|
|
{
|
|
|
|
int state, apm_state;
|
|
|
|
int error;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
switch (cmd) {
|
|
|
|
case POWER_CMD_SUSPEND:
|
|
|
|
va_start(ap, arg);
|
|
|
|
state = va_arg(ap, int);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case POWER_SLEEP_STATE_STANDBY:
|
|
|
|
apm_state = PMST_STANDBY;
|
|
|
|
break;
|
|
|
|
case POWER_SLEEP_STATE_SUSPEND:
|
|
|
|
case POWER_SLEEP_STATE_HIBERNATE:
|
|
|
|
apm_state = PMST_SUSPEND;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
apm_suspend(apm_state);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
apm_pm_register(void *arg)
|
|
|
|
{
|
|
|
|
|
2003-07-02 16:09:02 +00:00
|
|
|
if (!resource_disabled("apm", 0))
|
2001-11-01 16:34:07 +00:00
|
|
|
power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, 0);
|