2 * APM (Advanced Power Management) BIOS Device Driver
4 * Copyright (c) 1994 UKAI, Fumitoshi.
5 * Copyright (c) 1994-1995 by HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
6 * Copyright (c) 1996 Nate Williams <nate@FreeBSD.org>
7 * Copyright (c) 1997 Poul-Henning Kamp <phk@FreeBSD.org>
9 * This software may be used, modified, copied, and distributed, in
10 * both source and binary form provided that the above copyright and
11 * these terms are retained. Under no circumstances is the author
12 * responsible for the proper functioning of this software, nor does
13 * the author assume any responsibility for damages incurred with its
16 * Sep, 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
20 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
21 * All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/condvar.h>
53 #include <sys/eventhandler.h>
54 #include <sys/fcntl.h>
55 #include <sys/kernel.h>
56 #include <sys/kthread.h>
58 #include <sys/module.h>
59 #include <sys/mutex.h>
61 #include <sys/power.h>
62 #include <sys/reboot.h>
63 #include <sys/selinfo.h>
64 #include <sys/signalvar.h>
65 #include <sys/sysctl.h>
66 #include <sys/syslog.h>
67 #include <sys/timetc.h>
71 #include <machine/apm_bios.h>
72 #include <machine/clock.h>
73 #include <machine/endian.h>
74 #include <machine/pc/bios.h>
75 #include <machine/cpufunc.h>
76 #include <machine/segments.h>
77 #include <machine/stdarg.h>
78 #include <machine/vm86.h>
80 #include <machine/bus.h>
81 #include <machine/resource.h>
86 #include <vm/vm_param.h>
88 #include <i386/bios/apm.h>
91 /* Used by the apm_saver screen saver module */
92 int apm_display(int newstate);
93 struct apm_softc apm_softc;
95 static void apm_resume(void);
96 static int apm_bioscall(void);
97 static int apm_check_function_supported(u_int version, u_int func);
99 static int apm_pm_func(u_long, void*, ...);
101 static u_long apm_version;
105 #define SCFLAG_ONORMAL 0x0000001
106 #define SCFLAG_OCTL 0x0000002
107 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
109 #define APMDEV_NORMAL 0
112 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
114 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
116 /* Map version number to integer (keeps ordering of version numbers) */
117 #define INTVERSION(major, minor) ((major)*100 + (minor))
119 static d_open_t apmopen;
120 static d_close_t apmclose;
121 static d_write_t apmwrite;
122 static d_ioctl_t apmioctl;
123 static d_poll_t apmpoll;
125 static struct cdevsw apm_cdevsw = {
126 .d_version = D_VERSION,
127 .d_flags = D_NEEDGIANT,
136 static int apm_suspend_delay = 1;
137 static int apm_standby_delay = 1;
138 static int apm_swab_batt_minutes = 0;
139 static int apm_debug = 0;
141 #define APM_DPRINT(args...) do { \
147 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
148 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
149 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
151 SYSCTL_INT(_machdep, OID_AUTO, apm_swab_batt_minutes, CTLFLAG_RWTUN,
152 &apm_swab_batt_minutes, 0, "Byte swap battery time value.");
155 * return 0 if the function successfull,
156 * return 1 if the function unsuccessfull,
157 * return -1 if the function unsupported.
162 struct apm_softc *sc = &apm_softc;
164 u_int apm_func = sc->bios.r.eax & 0xff;
166 if (!apm_check_function_supported(sc->intversion, apm_func)) {
167 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
168 apm_func, sc->majorversion, sc->minorversion);
173 if (sc->connectmode == APM_PROT32CONNECT) {
174 set_bios_selectors(&sc->bios.seg,
175 BIOSCODE_FLAG | BIOSDATA_FLAG);
176 errno = bios32(&sc->bios.r,
177 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
179 errno = bios16(&sc->bios, NULL);
185 /* check whether APM function is supported (1) or not (0). */
187 apm_check_function_supported(u_int version, u_int func)
189 /* except driver version */
190 if (func == APM_DRVVERSION) {
195 case INTVERSION(1, 0):
196 if (func > APM_GETPMEVENT) {
197 return (0); /* not supported */
200 case INTVERSION(1, 1):
201 if (func > APM_ENGAGEDISENGAGEPM &&
202 func < APM_OEMFUNC) {
203 return (0); /* not supported */
206 case INTVERSION(1, 2):
210 return (1); /* supported */
213 /* enable/disable power management */
215 apm_enable_disable_pm(int enable)
217 struct apm_softc *sc = &apm_softc;
219 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
221 if (sc->intversion >= INTVERSION(1, 1))
222 sc->bios.r.ebx = PMDV_ALLDEV;
224 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
225 sc->bios.r.ecx = enable;
227 return (apm_bioscall());
230 /* register driver version (APM 1.1 or later) */
232 apm_driver_version(int version)
234 struct apm_softc *sc = &apm_softc;
236 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
237 sc->bios.r.ebx = 0x0;
238 sc->bios.r.ecx = version;
241 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
244 /* Some old BIOSes don't return the connection version in %ax. */
245 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
251 /* engage/disengage power management (APM 1.1 or later) */
253 apm_engage_disengage_pm(int engage)
255 struct apm_softc *sc = &apm_softc;
257 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
258 sc->bios.r.ebx = PMDV_ALLDEV;
259 sc->bios.r.ecx = engage;
261 return (apm_bioscall());
268 struct apm_softc *sc = &apm_softc;
270 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
276 return (PMEV_NOEVENT);
277 return (sc->bios.r.ebx & 0xffff);
280 /* suspend entire system */
282 apm_suspend_system(int state)
284 struct apm_softc *sc = &apm_softc;
286 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
287 sc->bios.r.ebx = PMDV_ALLDEV;
288 sc->bios.r.ecx = state;
291 if (apm_bioscall()) {
292 printf("Entire system suspend failure: errcode = %d\n",
293 0xff & (sc->bios.r.eax >> 8));
300 /* Display control */
302 * Experimental implementation: My laptop machine can't handle this function
303 * If your laptop can control the display via APM, please inform me.
304 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
307 apm_display(int newstate)
309 struct apm_softc *sc = &apm_softc;
311 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
312 sc->bios.r.ebx = PMDV_DISP0;
313 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
315 if (apm_bioscall() == 0) {
319 /* If failed, then try to blank all display devices instead. */
320 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
321 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
322 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
324 if (apm_bioscall() == 0) {
327 printf("Display off failure: errcode = %d\n",
328 0xff & (sc->bios.r.eax >> 8));
333 * Turn off the entire system.
336 apm_power_off(void *junk, int howto)
338 struct apm_softc *sc = &apm_softc;
340 /* Not halting powering off, or not active */
341 if (!(howto & RB_POWEROFF) || !apm_softc.active)
343 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
344 sc->bios.r.ebx = PMDV_ALLDEV;
345 sc->bios.r.ecx = PMST_OFF;
347 (void) apm_bioscall();
350 /* APM Battery low handler */
352 apm_battery_low(void)
354 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
357 /* APM hook manager */
358 static struct apmhook *
359 apm_add_hook(struct apmhook **list, struct apmhook *ah)
361 struct apmhook *p, *prev;
363 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
366 panic("illegal apm_hook!");
368 for (p = *list; p != NULL; prev = p, p = p->ah_next)
369 if (p->ah_order > ah->ah_order)
376 ah->ah_next = prev->ah_next;
383 apm_del_hook(struct apmhook **list, struct apmhook *ah)
385 struct apmhook *p, *prev;
388 for (p = *list; p != NULL; prev = p, p = p->ah_next)
391 panic("Tried to delete unregistered apm_hook.");
395 prev->ah_next = p->ah_next;
401 /* APM driver calls some functions automatically */
403 apm_execute_hook(struct apmhook *list)
407 for (p = list; p != NULL; p = p->ah_next) {
408 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
409 if ((*(p->ah_fun))(p->ah_arg))
410 printf("Warning: APM hook \"%s\" failed", p->ah_name);
415 /* establish an apm hook */
417 apm_hook_establish(int apmh, struct apmhook *ah)
419 if (apmh < 0 || apmh >= NAPM_HOOK)
422 return apm_add_hook(&hook[apmh], ah);
425 /* disestablish an apm hook */
427 apm_hook_disestablish(int apmh, struct apmhook *ah)
429 if (apmh < 0 || apmh >= NAPM_HOOK)
432 apm_del_hook(&hook[apmh], ah);
435 static int apm_record_event(struct apm_softc *, u_int);
436 static void apm_processevent(void);
438 static u_int apm_op_inprog = 0;
443 struct apm_softc *sc = &apm_softc;
446 if (sc == NULL || sc->initialized == 0)
450 sc->suspends = sc->suspend_countdown = 0;
452 EVENTHANDLER_INVOKE(power_suspend);
455 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since
456 * non-MPSAFE drivers need this.
459 error = DEVICE_SUSPEND(root_bus);
463 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
464 if (apm_suspend_system(PMST_SUSPEND) == 0) {
471 /* Failure, 'resume' the system again */
472 apm_execute_hook(hook[APM_HOOK_RESUME]);
473 DEVICE_RESUME(root_bus);
476 EVENTHANDLER_INVOKE(power_resume);
482 struct apm_softc *sc = &apm_softc;
484 if (sc == NULL || sc->initialized == 0)
488 sc->standbys = sc->standby_countdown = 0;
491 * As far as standby, we don't need to execute
492 * all of suspend hooks.
494 if (apm_suspend_system(PMST_STANDBY) == 0)
500 apm_lastreq_notify(void)
502 struct apm_softc *sc = &apm_softc;
504 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
505 sc->bios.r.ebx = PMDV_ALLDEV;
506 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
512 apm_lastreq_rejected(void)
514 struct apm_softc *sc = &apm_softc;
516 if (apm_op_inprog == 0) {
517 return 1; /* no operation in progress */
520 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
521 sc->bios.r.ebx = PMDV_ALLDEV;
522 sc->bios.r.ecx = PMST_LASTREQREJECT;
525 if (apm_bioscall()) {
526 APM_DPRINT("apm_lastreq_rejected: failed\n");
534 * Public interface to the suspend/resume:
536 * Execute suspend and resume hook before and after sleep, respectively.
541 apm_suspend(int state)
543 struct apm_softc *sc = &apm_softc;
545 if (sc == NULL || sc->initialized == 0)
553 sc->suspend_countdown = apm_suspend_delay;
559 sc->standby_countdown = apm_standby_delay;
562 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
567 apm_lastreq_notify();
573 struct apm_softc *sc = &apm_softc;
575 if (sc == NULL || sc->initialized == 0 || sc->suspending == 0)
579 apm_execute_hook(hook[APM_HOOK_RESUME]);
581 DEVICE_RESUME(root_bus);
583 EVENTHANDLER_INVOKE(power_resume);
587 /* get power status per battery */
589 apm_get_pwstatus(apm_pwstatus_t app)
591 struct apm_softc *sc = &apm_softc;
593 if (app->ap_device != PMDV_ALLDEV &&
594 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
597 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
598 sc->bios.r.ebx = app->ap_device;
600 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
605 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
606 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
607 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
608 app->ap_batt_life = sc->bios.r.ecx & 0xff;
609 sc->bios.r.edx &= 0xffff;
610 if (apm_swab_batt_minutes)
611 sc->bios.r.edx = __bswap16(sc->bios.r.edx) | 0x8000;
612 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
613 app->ap_batt_time = -1;
614 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
615 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
616 else /* Time is in seconds */
617 app->ap_batt_time = sc->bios.r.edx;
623 /* get APM information */
625 apm_get_info(apm_info_t aip)
627 struct apm_softc *sc = &apm_softc;
628 struct apm_pwstatus aps;
630 bzero(&aps, sizeof(aps));
631 aps.ap_device = PMDV_ALLDEV;
632 if (apm_get_pwstatus(&aps))
635 aip->ai_infoversion = 1;
636 aip->ai_acline = aps.ap_acline;
637 aip->ai_batt_stat = aps.ap_batt_stat;
638 aip->ai_batt_life = aps.ap_batt_life;
639 aip->ai_batt_time = aps.ap_batt_time;
640 aip->ai_major = (u_int)sc->majorversion;
641 aip->ai_minor = (u_int)sc->minorversion;
642 aip->ai_status = (u_int)sc->active;
644 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
648 if (apm_bioscall()) {
649 aip->ai_batteries = 0xffffffff; /* Unknown */
650 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
652 aip->ai_batteries = sc->bios.r.ebx & 0xff;
653 aip->ai_capabilities = sc->bios.r.ecx & 0xff;
656 bzero(aip->ai_spare, sizeof aip->ai_spare);
662 /* inform APM BIOS that CPU is idle */
666 struct apm_softc *sc = &apm_softc;
670 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
671 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
672 (void) apm_bioscall();
675 * Some APM implementation halts CPU in BIOS, whenever
676 * "CPU-idle" function are invoked, but swtch() of
677 * FreeBSD halts CPU, therefore, CPU is halted twice
678 * in the sched loop. It makes the interrupt latency
679 * terribly long and be able to cause a serious problem
680 * in interrupt processing. We prevent it by removing
681 * "hlt" operation from swtch() and managed it under
684 if (!sc->active || sc->always_halt_cpu)
685 halt(); /* wait for interrupt */
688 /* inform APM BIOS that CPU is busy */
692 struct apm_softc *sc = &apm_softc;
695 * The APM specification says this is only necessary if your BIOS
696 * slows down the processor in the idle task, otherwise it's not
699 if (sc->slow_idle_cpu && sc->active) {
701 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
702 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
711 * This routine wakes up from time to time to deal with delaying the
712 * suspend of the system, or other events.
715 apm_event_thread(void *arg)
717 struct apm_softc *sc = &apm_softc;
722 apm_lastreq_notify();
723 if (sc->standbys && sc->standby_countdown-- <= 0)
725 if (sc->suspends && sc->suspend_countdown-- <= 0)
730 cv_timedwait(&sc->cv, &sc->mtx, 10 * hz / 9);
731 mtx_unlock(&sc->mtx);
737 /* enable APM BIOS */
739 apm_event_enable(void)
741 struct apm_softc *sc = &apm_softc;
743 APM_DPRINT("called apm_event_enable()\n");
745 if (sc == NULL || sc->initialized == 0)
748 /* Start the thread */
750 if (kproc_create(apm_event_thread, sc, &sc->event_thread, 0, 0,
752 panic("Cannot create apm worker thread");
757 /* disable APM BIOS */
759 apm_event_disable(void)
761 struct apm_softc *sc = &apm_softc;
763 APM_DPRINT("called apm_event_disable()\n");
765 if (sc == NULL || sc->initialized == 0)
770 while (sc->running) {
771 cv_broadcast(&sc->cv);
772 msleep(sc->event_thread, &sc->mtx, PWAIT, "apmdie", 0);
774 mtx_unlock(&sc->mtx);
775 sc->event_thread = NULL;
779 /* halt CPU in scheduling loop */
783 struct apm_softc *sc = &apm_softc;
785 if (sc == NULL || sc->initialized == 0)
788 sc->always_halt_cpu = 1;
793 /* don't halt CPU in scheduling loop */
795 apm_not_halt_cpu(void)
797 struct apm_softc *sc = &apm_softc;
799 if (sc == NULL || sc->initialized == 0)
802 sc->always_halt_cpu = 0;
807 /* device driver definitions */
814 apm_modevent(struct module *mod, int event, void *junk)
823 if (!cold && power_pm_get_type() == POWER_PM_TYPE_APM)
834 * Create "connection point"
837 apm_identify(driver_t *driver, device_t parent)
842 printf("Don't load this driver from userland!!\n");
846 if (resource_disabled("apm", 0))
849 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
851 panic("apm_identify");
858 apm_probe(device_t dev)
860 #define APM_KERNBASE KERNBASE
861 struct vm86frame vmf;
862 struct apm_softc *sc = &apm_softc;
864 device_set_desc(dev, "APM BIOS");
865 if (device_get_unit(dev) > 0) {
866 printf("apm: Only one APM driver supported.\n");
870 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
871 power_pm_get_type() != POWER_PM_TYPE_APM) {
872 printf("apm: Other PM system enabled.\n");
876 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
877 bzero(&apm_softc, sizeof(apm_softc));
878 vmf.vmf_ah = APM_BIOS;
879 vmf.vmf_al = APM_INSTCHECK;
881 if (vm86_intcall(APM_INT, &vmf))
882 return ENXIO; /* APM not found */
883 if (vmf.vmf_bx != 0x504d) {
884 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
887 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
888 printf("apm: protected mode connections are not supported\n");
892 apm_version = vmf.vmf_ax;
893 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
894 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
895 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
897 vmf.vmf_ah = APM_BIOS;
898 vmf.vmf_al = APM_DISCONNECT;
900 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
902 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
903 vmf.vmf_ah = APM_BIOS;
904 vmf.vmf_al = APM_PROT32CONNECT;
906 if (vm86_intcall(APM_INT, &vmf)) {
907 printf("apm: 32-bit connection error.\n");
910 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
911 sc->bios.seg.code32.limit = 0xffff;
912 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
913 sc->bios.seg.code16.limit = 0xffff;
914 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
915 sc->bios.seg.data.limit = 0xffff;
916 sc->bios.entry = vmf.vmf_ebx;
917 sc->connectmode = APM_PROT32CONNECT;
919 /* use 16-bit connection */
920 vmf.vmf_ah = APM_BIOS;
921 vmf.vmf_al = APM_PROT16CONNECT;
923 if (vm86_intcall(APM_INT, &vmf)) {
924 printf("apm: 16-bit connection error.\n");
927 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
928 sc->bios.seg.code16.limit = 0xffff;
929 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
930 sc->bios.seg.data.limit = 0xffff;
931 sc->bios.entry = vmf.vmf_bx;
932 sc->connectmode = APM_PROT16CONNECT;
940 * return 0 if the user will notice and handle the event,
941 * return 1 if the kernel driver should do so.
944 apm_record_event(struct apm_softc *sc, u_int event_type)
946 struct apm_event_info *evp;
948 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
949 return 1; /* no user waiting */
950 if (sc->event_count == APM_NEVENTS)
951 return 1; /* overflow */
952 if (sc->event_filter[event_type] == 0)
953 return 1; /* not registered */
954 evp = &sc->event_list[sc->event_ptr];
957 sc->event_ptr %= APM_NEVENTS;
958 evp->type = event_type;
959 evp->index = ++apm_evindex;
960 selwakeuppri(&sc->sc_rsel, PZERO);
961 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
966 apm_power_profile(struct apm_softc *sc)
969 struct apm_info info;
970 static int apm_acline = 0;
972 if (apm_get_info(&info))
975 if (apm_acline != info.ai_acline) {
976 apm_acline = info.ai_acline;
977 state = apm_acline ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY;
978 power_profile_set_state(state);
982 /* Process APM event */
984 apm_processevent(void)
987 struct apm_softc *sc = &apm_softc;
989 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
990 APM_DPRINT("Received APM Event: " #symbol "\n");
993 apm_event = apm_getevent();
995 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
996 if (apm_op_inprog == 0) {
998 if (apm_record_event(sc, apm_event)) {
999 apm_suspend(PMST_STANDBY);
1003 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
1004 if (apm_op_inprog == 0) {
1006 if (apm_record_event(sc, apm_event)) {
1007 apm_suspend(PMST_STANDBY);
1011 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
1012 apm_lastreq_notify();
1013 if (apm_op_inprog == 0) {
1015 if (apm_record_event(sc, apm_event)) {
1019 return; /* XXX skip the rest */
1020 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
1021 apm_lastreq_notify();
1022 if (apm_op_inprog == 0) {
1024 if (apm_record_event(sc, apm_event)) {
1028 return; /* XXX skip the rest */
1029 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
1032 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
1033 apm_record_event(sc, apm_event);
1036 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
1037 apm_record_event(sc, apm_event);
1040 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
1041 apm_record_event(sc, apm_event);
1043 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
1044 if (apm_record_event(sc, apm_event)) {
1046 apm_suspend(PMST_SUSPEND);
1049 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
1050 apm_record_event(sc, apm_event);
1051 apm_power_profile(sc);
1053 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1054 apm_record_event(sc, apm_event);
1055 inittodr(0); /* adjust time to RTC */
1057 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1058 apm_record_event(sc, apm_event);
1059 apm_power_profile(sc);
1064 printf("Unknown Original APM Event 0x%x\n", apm_event);
1067 } while (apm_event != PMEV_NOEVENT);
1070 static struct timeval suspend_time;
1071 static struct timeval diff_time;
1074 apm_rtc_suspend(void *arg __unused)
1077 microtime(&diff_time);
1079 microtime(&suspend_time);
1080 timevalsub(&diff_time, &suspend_time);
1085 apm_rtc_resume(void *arg __unused)
1087 u_int second, minute, hour;
1088 struct timeval resume_time, tmp_time;
1090 /* modified for adjkerntz */
1091 timer_restore(); /* restore the all timers */
1092 inittodr(0); /* adjust time to RTC */
1093 microtime(&resume_time);
1094 getmicrotime(&tmp_time);
1095 timevaladd(&tmp_time, &diff_time);
1096 /* Calculate the delta time suspended */
1097 timevalsub(&resume_time, &suspend_time);
1099 #ifdef PMTIMER_FIXUP_CALLTODO
1100 /* Fixup the calltodo list with the delta time. */
1101 adjust_timeout_calltodo(&resume_time);
1102 #endif /* PMTIMER_FIXUP_CALLTODO */
1103 second = resume_time.tv_sec;
1104 hour = second / 3600;
1106 minute = second / 60;
1108 log(LOG_NOTICE, "wakeup from sleeping state (slept %02d:%02d:%02d)\n",
1109 hour, minute, second);
1116 * Initialize APM driver
1120 apm_attach(device_t dev)
1122 struct apm_softc *sc = &apm_softc;
1125 mtx_init(&sc->mtx, device_get_nameunit(dev), "apm", MTX_DEF);
1126 cv_init(&sc->cv, "cbb cv");
1128 if (device_get_flags(dev) & 0x20)
1129 atrtcclock_disable = 1;
1131 sc->initialized = 0;
1133 /* Must be externally enabled */
1136 /* Always call HLT in idle loop */
1137 sc->always_halt_cpu = 1;
1139 getenv_int("debug.apm_debug", &apm_debug);
1141 /* print bootstrap messages */
1142 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1143 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1144 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1145 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1146 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1147 is_enabled(!sc->disabled));
1148 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1149 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1152 * In one test, apm bios version was 1.02; an attempt to register
1153 * a 1.04 driver resulted in a 1.00 connection! Registering a
1154 * 1.02 driver resulted in a 1.02 connection.
1156 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1157 for (; drv_version > 0x100; drv_version--)
1158 if (apm_driver_version(drv_version) == 0)
1160 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1161 ((drv_version & 0x000f) >> 0);
1162 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1163 ((apm_version & 0x0f00) >> 8);
1165 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1167 if (sc->intversion >= INTVERSION(1, 1))
1168 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1169 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1170 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1171 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1172 sc->majorversion, sc->minorversion);
1175 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1176 /* enable power management */
1178 if (apm_enable_disable_pm(1)) {
1179 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1180 (sc->bios.r.eax >> 8) & 0xff);
1184 /* engage power managment (APM 1.1 or later) */
1185 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1186 if (apm_engage_disengage_pm(1)) {
1187 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1188 (sc->bios.r.eax >> 8) & 0xff);
1189 APM_DPRINT(" (Docked or using external power?).\n");
1193 /* Power the system off using APM */
1194 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1197 /* Register APM again to pass the correct argument of pm_func. */
1198 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc);
1200 sc->initialized = 1;
1204 make_dev(&apm_cdevsw, APMDEV_NORMAL,
1205 UID_ROOT, GID_OPERATOR, 0664, "apm");
1206 make_dev(&apm_cdevsw, APMDEV_CTL,
1207 UID_ROOT, GID_OPERATOR, 0660, "apmctl");
1209 sc->sc_suspend.ah_fun = apm_rtc_suspend;
1210 sc->sc_suspend.ah_arg = sc;
1211 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1213 sc->sc_resume.ah_fun = apm_rtc_resume;
1214 sc->sc_resume.ah_arg = sc;
1215 apm_hook_establish(APM_HOOK_RESUME, &sc->sc_resume);
1221 apmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
1223 struct apm_softc *sc = &apm_softc;
1225 if (sc == NULL || sc->initialized == 0)
1228 switch (dev2unit(dev)) {
1230 if (!(flag & FWRITE))
1232 if (sc->sc_flags & SCFLAG_OCTL)
1234 sc->sc_flags |= SCFLAG_OCTL;
1235 bzero(sc->event_filter, sizeof sc->event_filter);
1238 sc->sc_flags |= SCFLAG_ONORMAL;
1245 apmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1247 struct apm_softc *sc = &apm_softc;
1249 switch (dev2unit(dev)) {
1251 apm_lastreq_rejected();
1252 sc->sc_flags &= ~SCFLAG_OCTL;
1253 bzero(sc->event_filter, sizeof sc->event_filter);
1256 sc->sc_flags &= ~SCFLAG_ONORMAL;
1259 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1260 sc->event_count = 0;
1267 apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1269 struct apm_softc *sc = &apm_softc;
1270 struct apm_bios_arg *args;
1275 if (sc == NULL || sc->initialized == 0)
1278 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1281 if (!(flag & FWRITE))
1284 apm_suspend(PMST_SUSPEND);
1290 if (!(flag & FWRITE))
1293 apm_suspend(PMST_STANDBY);
1298 case APMIO_GETINFO_OLD:
1300 struct apm_info info;
1301 apm_info_old_t aiop;
1303 if (apm_get_info(&info))
1305 aiop = (apm_info_old_t)addr;
1306 aiop->ai_major = info.ai_major;
1307 aiop->ai_minor = info.ai_minor;
1308 aiop->ai_acline = info.ai_acline;
1309 aiop->ai_batt_stat = info.ai_batt_stat;
1310 aiop->ai_batt_life = info.ai_batt_life;
1311 aiop->ai_status = info.ai_status;
1315 if (apm_get_info((apm_info_t)addr))
1318 case APMIO_GETPWSTATUS:
1319 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1323 if (!(flag & FWRITE))
1328 if (!(flag & FWRITE))
1330 apm_event_disable();
1333 if (!(flag & FWRITE))
1337 case APMIO_NOTHALTCPU:
1338 if (!(flag & FWRITE))
1343 if (!(flag & FWRITE))
1345 newstate = *(int *)addr;
1346 if (apm_display(newstate))
1350 if (!(flag & FWRITE))
1352 /* XXX compatibility with the old interface */
1353 args = (struct apm_bios_arg *)addr;
1354 sc->bios.r.eax = args->eax;
1355 sc->bios.r.ebx = args->ebx;
1356 sc->bios.r.ecx = args->ecx;
1357 sc->bios.r.edx = args->edx;
1358 sc->bios.r.esi = args->esi;
1359 sc->bios.r.edi = args->edi;
1360 if ((ret = apm_bioscall())) {
1362 * Return code 1 means bios call was unsuccessful.
1363 * Error code is stored in %ah.
1364 * Return code -1 means bios call was unsupported
1365 * in the APM BIOS version.
1372 * Return code 0 means bios call was successful.
1373 * We need only %al and can discard %ah.
1375 sc->bios.r.eax &= 0xff;
1377 args->eax = sc->bios.r.eax;
1378 args->ebx = sc->bios.r.ebx;
1379 args->ecx = sc->bios.r.ecx;
1380 args->edx = sc->bios.r.edx;
1381 args->esi = sc->bios.r.esi;
1382 args->edi = sc->bios.r.edi;
1389 /* for /dev/apmctl */
1390 if (dev2unit(dev) == APMDEV_CTL) {
1391 struct apm_event_info *evp;
1396 case APMIO_NEXTEVENT:
1397 if (!sc->event_count) {
1400 evp = (struct apm_event_info *)addr;
1401 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1403 *evp = sc->event_list[i];
1407 case APMIO_REJECTLASTREQ:
1408 if (apm_lastreq_rejected()) {
1422 apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
1424 struct apm_softc *sc = &apm_softc;
1429 if (dev2unit(dev) != APMDEV_CTL)
1431 if (uio->uio_resid != sizeof(u_int))
1434 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1437 if (event_type >= APM_NPMEV)
1440 if (sc->event_filter[event_type] == 0) {
1445 sc->event_filter[event_type] = enabled;
1446 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1448 return uio->uio_resid;
1452 apmpoll(struct cdev *dev, int events, struct thread *td)
1454 struct apm_softc *sc = &apm_softc;
1457 if (events & (POLLIN | POLLRDNORM)) {
1458 if (sc->event_count) {
1459 revents |= events & (POLLIN | POLLRDNORM);
1461 selrecord(td, &sc->sc_rsel);
1468 static device_method_t apm_methods[] = {
1469 /* Device interface */
1470 DEVMETHOD(device_identify, apm_identify),
1471 DEVMETHOD(device_probe, apm_probe),
1472 DEVMETHOD(device_attach, apm_attach),
1477 static driver_t apm_driver = {
1480 1, /* no softc (XXX) */
1483 static devclass_t apm_devclass;
1485 DRIVER_MODULE(apm, legacy, apm_driver, apm_devclass, apm_modevent, 0);
1486 MODULE_VERSION(apm, 1);
1489 apm_pm_func(u_long cmd, void *arg, ...)
1491 int state, apm_state;
1497 case POWER_CMD_SUSPEND:
1499 state = va_arg(ap, int);
1503 case POWER_SLEEP_STATE_STANDBY:
1504 apm_state = PMST_STANDBY;
1506 case POWER_SLEEP_STATE_SUSPEND:
1507 case POWER_SLEEP_STATE_HIBERNATE:
1508 apm_state = PMST_SUSPEND;
1515 apm_suspend(apm_state);
1528 apm_pm_register(void *arg)
1531 if (!resource_disabled("apm", 0))
1532 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL);
1535 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, NULL);