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)
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
22 #include <sys/param.h>
23 #include <sys/systm.h>
26 #include <sys/condvar.h>
27 #include <sys/eventhandler.h>
28 #include <sys/fcntl.h>
29 #include <sys/kernel.h>
30 #include <sys/kthread.h>
32 #include <sys/module.h>
33 #include <sys/mutex.h>
35 #include <sys/power.h>
36 #include <sys/reboot.h>
37 #include <sys/selinfo.h>
38 #include <sys/signalvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/syslog.h>
44 #include <machine/apm_bios.h>
45 #include <machine/clock.h>
46 #include <machine/endian.h>
47 #include <machine/pc/bios.h>
48 #include <machine/cpufunc.h>
49 #include <machine/segments.h>
50 #include <machine/stdarg.h>
51 #include <machine/vm86.h>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
59 #include <vm/vm_param.h>
61 #include <i386/bios/apm.h>
64 /* Used by the apm_saver screen saver module */
65 int apm_display(int newstate);
66 struct apm_softc apm_softc;
68 static void apm_resume(void);
69 static int apm_bioscall(void);
70 static int apm_check_function_supported(u_int version, u_int func);
72 static int apm_pm_func(u_long, void*, ...);
74 static u_long apm_version;
78 #define SCFLAG_ONORMAL 0x0000001
79 #define SCFLAG_OCTL 0x0000002
80 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
82 #define APMDEV_NORMAL 0
85 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
87 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
89 /* Map version number to integer (keeps ordering of version numbers) */
90 #define INTVERSION(major, minor) ((major)*100 + (minor))
92 static d_open_t apmopen;
93 static d_close_t apmclose;
94 static d_write_t apmwrite;
95 static d_ioctl_t apmioctl;
96 static d_poll_t apmpoll;
98 static struct cdevsw apm_cdevsw = {
99 .d_version = D_VERSION,
100 .d_flags = D_NEEDGIANT,
109 static int apm_suspend_delay = 1;
110 static int apm_standby_delay = 1;
111 static int apm_swab_batt_minutes = 0;
112 static int apm_debug = 0;
114 #define APM_DPRINT(args...) do { \
120 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
121 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
122 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
124 SYSCTL_INT(_machdep, OID_AUTO, apm_swab_batt_minutes, CTLFLAG_RWTUN,
125 &apm_swab_batt_minutes, 0, "Byte swap battery time value.");
128 * return 0 if the function successfull,
129 * return 1 if the function unsuccessfull,
130 * return -1 if the function unsupported.
135 struct apm_softc *sc = &apm_softc;
137 u_int apm_func = sc->bios.r.eax & 0xff;
139 if (!apm_check_function_supported(sc->intversion, apm_func)) {
140 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
141 apm_func, sc->majorversion, sc->minorversion);
146 if (sc->connectmode == APM_PROT32CONNECT) {
147 set_bios_selectors(&sc->bios.seg,
148 BIOSCODE_FLAG | BIOSDATA_FLAG);
149 errno = bios32(&sc->bios.r,
150 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
152 errno = bios16(&sc->bios, NULL);
158 /* check whether APM function is supported (1) or not (0). */
160 apm_check_function_supported(u_int version, u_int func)
162 /* except driver version */
163 if (func == APM_DRVVERSION) {
168 case INTVERSION(1, 0):
169 if (func > APM_GETPMEVENT) {
170 return (0); /* not supported */
173 case INTVERSION(1, 1):
174 if (func > APM_ENGAGEDISENGAGEPM &&
175 func < APM_OEMFUNC) {
176 return (0); /* not supported */
179 case INTVERSION(1, 2):
183 return (1); /* supported */
186 /* enable/disable power management */
188 apm_enable_disable_pm(int enable)
190 struct apm_softc *sc = &apm_softc;
192 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
194 if (sc->intversion >= INTVERSION(1, 1))
195 sc->bios.r.ebx = PMDV_ALLDEV;
197 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
198 sc->bios.r.ecx = enable;
200 return (apm_bioscall());
203 /* register driver version (APM 1.1 or later) */
205 apm_driver_version(int version)
207 struct apm_softc *sc = &apm_softc;
209 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
210 sc->bios.r.ebx = 0x0;
211 sc->bios.r.ecx = version;
214 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
217 /* Some old BIOSes don't return the connection version in %ax. */
218 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
224 /* engage/disengage power management (APM 1.1 or later) */
226 apm_engage_disengage_pm(int engage)
228 struct apm_softc *sc = &apm_softc;
230 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
231 sc->bios.r.ebx = PMDV_ALLDEV;
232 sc->bios.r.ecx = engage;
234 return (apm_bioscall());
241 struct apm_softc *sc = &apm_softc;
243 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
249 return (PMEV_NOEVENT);
250 return (sc->bios.r.ebx & 0xffff);
253 /* suspend entire system */
255 apm_suspend_system(int state)
257 struct apm_softc *sc = &apm_softc;
259 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
260 sc->bios.r.ebx = PMDV_ALLDEV;
261 sc->bios.r.ecx = state;
264 if (apm_bioscall()) {
265 printf("Entire system suspend failure: errcode = %d\n",
266 0xff & (sc->bios.r.eax >> 8));
273 /* Display control */
275 * Experimental implementation: My laptop machine can't handle this function
276 * If your laptop can control the display via APM, please inform me.
277 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
280 apm_display(int newstate)
282 struct apm_softc *sc = &apm_softc;
284 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
285 sc->bios.r.ebx = PMDV_DISP0;
286 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
288 if (apm_bioscall() == 0) {
292 /* If failed, then try to blank all display devices instead. */
293 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
294 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
295 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
297 if (apm_bioscall() == 0) {
300 printf("Display off failure: errcode = %d\n",
301 0xff & (sc->bios.r.eax >> 8));
306 * Turn off the entire system.
309 apm_power_off(void *junk, int howto)
311 struct apm_softc *sc = &apm_softc;
313 /* Not halting powering off, or not active */
314 if (!(howto & RB_POWEROFF) || !apm_softc.active)
316 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
317 sc->bios.r.ebx = PMDV_ALLDEV;
318 sc->bios.r.ecx = PMST_OFF;
320 (void) apm_bioscall();
323 /* APM Battery low handler */
325 apm_battery_low(void)
327 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
330 /* APM hook manager */
331 static struct apmhook *
332 apm_add_hook(struct apmhook **list, struct apmhook *ah)
335 struct apmhook *p, *prev;
337 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
341 panic("illegal apm_hook!");
343 for (p = *list; p != NULL; prev = p, p = p->ah_next)
344 if (p->ah_order > ah->ah_order)
351 ah->ah_next = prev->ah_next;
359 apm_del_hook(struct apmhook **list, struct apmhook *ah)
362 struct apmhook *p, *prev;
366 for (p = *list; p != NULL; prev = p, p = p->ah_next)
369 panic("Tried to delete unregistered apm_hook.");
373 prev->ah_next = p->ah_next;
381 /* APM driver calls some functions automatically */
383 apm_execute_hook(struct apmhook *list)
387 for (p = list; p != NULL; p = p->ah_next) {
388 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
389 if ((*(p->ah_fun))(p->ah_arg))
390 printf("Warning: APM hook \"%s\" failed", p->ah_name);
395 /* establish an apm hook */
397 apm_hook_establish(int apmh, struct apmhook *ah)
399 if (apmh < 0 || apmh >= NAPM_HOOK)
402 return apm_add_hook(&hook[apmh], ah);
405 /* disestablish an apm hook */
407 apm_hook_disestablish(int apmh, struct apmhook *ah)
409 if (apmh < 0 || apmh >= NAPM_HOOK)
412 apm_del_hook(&hook[apmh], ah);
415 static int apm_record_event(struct apm_softc *, u_int);
416 static void apm_processevent(void);
418 static u_int apm_op_inprog = 0;
423 struct apm_softc *sc = &apm_softc;
426 if (sc == NULL || sc->initialized == 0)
430 sc->suspends = sc->suspend_countdown = 0;
432 EVENTHANDLER_INVOKE(power_suspend);
435 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since
436 * non-MPSAFE drivers need this.
439 error = DEVICE_SUSPEND(root_bus);
443 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
444 if (apm_suspend_system(PMST_SUSPEND) == 0) {
451 /* Failure, 'resume' the system again */
452 apm_execute_hook(hook[APM_HOOK_RESUME]);
453 DEVICE_RESUME(root_bus);
456 EVENTHANDLER_INVOKE(power_resume);
462 struct apm_softc *sc = &apm_softc;
464 if (sc == NULL || sc->initialized == 0)
468 sc->standbys = sc->standby_countdown = 0;
471 * As far as standby, we don't need to execute
472 * all of suspend hooks.
474 if (apm_suspend_system(PMST_STANDBY) == 0)
480 apm_lastreq_notify(void)
482 struct apm_softc *sc = &apm_softc;
484 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
485 sc->bios.r.ebx = PMDV_ALLDEV;
486 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
492 apm_lastreq_rejected(void)
494 struct apm_softc *sc = &apm_softc;
496 if (apm_op_inprog == 0) {
497 return 1; /* no operation in progress */
500 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
501 sc->bios.r.ebx = PMDV_ALLDEV;
502 sc->bios.r.ecx = PMST_LASTREQREJECT;
505 if (apm_bioscall()) {
506 APM_DPRINT("apm_lastreq_rejected: failed\n");
514 * Public interface to the suspend/resume:
516 * Execute suspend and resume hook before and after sleep, respectively.
521 apm_suspend(int state)
523 struct apm_softc *sc = &apm_softc;
525 if (sc == NULL || sc->initialized == 0)
533 sc->suspend_countdown = apm_suspend_delay;
539 sc->standby_countdown = apm_standby_delay;
542 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
547 apm_lastreq_notify();
553 struct apm_softc *sc = &apm_softc;
555 if (sc == NULL || sc->initialized == 0 || sc->suspending == 0)
559 apm_execute_hook(hook[APM_HOOK_RESUME]);
561 DEVICE_RESUME(root_bus);
563 EVENTHANDLER_INVOKE(power_resume);
567 /* get power status per battery */
569 apm_get_pwstatus(apm_pwstatus_t app)
571 struct apm_softc *sc = &apm_softc;
573 if (app->ap_device != PMDV_ALLDEV &&
574 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
577 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
578 sc->bios.r.ebx = app->ap_device;
580 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
585 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
586 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
587 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
588 app->ap_batt_life = sc->bios.r.ecx & 0xff;
589 sc->bios.r.edx &= 0xffff;
590 if (apm_swab_batt_minutes)
591 sc->bios.r.edx = __bswap16(sc->bios.r.edx) | 0x8000;
592 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
593 app->ap_batt_time = -1;
594 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
595 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
596 else /* Time is in seconds */
597 app->ap_batt_time = sc->bios.r.edx;
603 /* get APM information */
605 apm_get_info(apm_info_t aip)
607 struct apm_softc *sc = &apm_softc;
608 struct apm_pwstatus aps;
610 bzero(&aps, sizeof(aps));
611 aps.ap_device = PMDV_ALLDEV;
612 if (apm_get_pwstatus(&aps))
615 aip->ai_infoversion = 1;
616 aip->ai_acline = aps.ap_acline;
617 aip->ai_batt_stat = aps.ap_batt_stat;
618 aip->ai_batt_life = aps.ap_batt_life;
619 aip->ai_batt_time = aps.ap_batt_time;
620 aip->ai_major = (u_int)sc->majorversion;
621 aip->ai_minor = (u_int)sc->minorversion;
622 aip->ai_status = (u_int)sc->active;
624 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
628 if (apm_bioscall()) {
629 aip->ai_batteries = 0xffffffff; /* Unknown */
630 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
632 aip->ai_batteries = sc->bios.r.ebx & 0xff;
633 aip->ai_capabilities = sc->bios.r.ecx & 0xff;
636 bzero(aip->ai_spare, sizeof aip->ai_spare);
642 /* inform APM BIOS that CPU is idle */
646 struct apm_softc *sc = &apm_softc;
650 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
651 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
652 (void) apm_bioscall();
655 * Some APM implementation halts CPU in BIOS, whenever
656 * "CPU-idle" function are invoked, but swtch() of
657 * FreeBSD halts CPU, therefore, CPU is halted twice
658 * in the sched loop. It makes the interrupt latency
659 * terribly long and be able to cause a serious problem
660 * in interrupt processing. We prevent it by removing
661 * "hlt" operation from swtch() and managed it under
664 if (!sc->active || sc->always_halt_cpu)
665 halt(); /* wait for interrupt */
668 /* inform APM BIOS that CPU is busy */
672 struct apm_softc *sc = &apm_softc;
675 * The APM specification says this is only necessary if your BIOS
676 * slows down the processor in the idle task, otherwise it's not
679 if (sc->slow_idle_cpu && sc->active) {
681 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
682 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
691 * This routine wakes up from time to time to deal with delaying the
692 * suspend of the system, or other events.
695 apm_event_thread(void *arg)
697 struct apm_softc *sc = &apm_softc;
702 apm_lastreq_notify();
703 if (sc->standbys && sc->standby_countdown-- <= 0)
705 if (sc->suspends && sc->suspend_countdown-- <= 0)
710 cv_timedwait(&sc->cv, &sc->mtx, 10 * hz / 9);
711 mtx_unlock(&sc->mtx);
717 /* enable APM BIOS */
719 apm_event_enable(void)
721 struct apm_softc *sc = &apm_softc;
723 APM_DPRINT("called apm_event_enable()\n");
725 if (sc == NULL || sc->initialized == 0)
728 /* Start the thread */
730 if (kproc_create(apm_event_thread, sc, &sc->event_thread, 0, 0,
732 panic("Cannot create apm worker thread");
737 /* disable APM BIOS */
739 apm_event_disable(void)
741 struct apm_softc *sc = &apm_softc;
743 APM_DPRINT("called apm_event_disable()\n");
745 if (sc == NULL || sc->initialized == 0)
750 while (sc->running) {
751 cv_broadcast(&sc->cv);
752 msleep(sc->event_thread, &sc->mtx, PWAIT, "apmdie", 0);
754 mtx_unlock(&sc->mtx);
755 sc->event_thread = NULL;
759 /* halt CPU in scheduling loop */
763 struct apm_softc *sc = &apm_softc;
765 if (sc == NULL || sc->initialized == 0)
768 sc->always_halt_cpu = 1;
773 /* don't halt CPU in scheduling loop */
775 apm_not_halt_cpu(void)
777 struct apm_softc *sc = &apm_softc;
779 if (sc == NULL || sc->initialized == 0)
782 sc->always_halt_cpu = 0;
787 /* device driver definitions */
794 apm_modevent(struct module *mod, int event, void *junk)
803 if (!cold && power_pm_get_type() == POWER_PM_TYPE_APM)
814 * Create "connection point"
817 apm_identify(driver_t *driver, device_t parent)
822 printf("Don't load this driver from userland!!\n");
826 if (resource_disabled("apm", 0))
829 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
831 panic("apm_identify");
838 apm_probe(device_t dev)
840 #define APM_KERNBASE KERNBASE
841 struct vm86frame vmf;
842 struct apm_softc *sc = &apm_softc;
844 device_set_desc(dev, "APM BIOS");
845 if (device_get_unit(dev) > 0) {
846 printf("apm: Only one APM driver supported.\n");
850 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
851 power_pm_get_type() != POWER_PM_TYPE_APM) {
852 printf("apm: Other PM system enabled.\n");
856 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
857 bzero(&apm_softc, sizeof(apm_softc));
858 vmf.vmf_ah = APM_BIOS;
859 vmf.vmf_al = APM_INSTCHECK;
861 if (vm86_intcall(APM_INT, &vmf))
862 return ENXIO; /* APM not found */
863 if (vmf.vmf_bx != 0x504d) {
864 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
867 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
868 printf("apm: protected mode connections are not supported\n");
872 apm_version = vmf.vmf_ax;
873 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
874 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
875 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
877 vmf.vmf_ah = APM_BIOS;
878 vmf.vmf_al = APM_DISCONNECT;
880 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
882 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
883 vmf.vmf_ah = APM_BIOS;
884 vmf.vmf_al = APM_PROT32CONNECT;
886 if (vm86_intcall(APM_INT, &vmf)) {
887 printf("apm: 32-bit connection error.\n");
890 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
891 sc->bios.seg.code32.limit = 0xffff;
892 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
893 sc->bios.seg.code16.limit = 0xffff;
894 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
895 sc->bios.seg.data.limit = 0xffff;
896 sc->bios.entry = vmf.vmf_ebx;
897 sc->connectmode = APM_PROT32CONNECT;
899 /* use 16-bit connection */
900 vmf.vmf_ah = APM_BIOS;
901 vmf.vmf_al = APM_PROT16CONNECT;
903 if (vm86_intcall(APM_INT, &vmf)) {
904 printf("apm: 16-bit connection error.\n");
907 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
908 sc->bios.seg.code16.limit = 0xffff;
909 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
910 sc->bios.seg.data.limit = 0xffff;
911 sc->bios.entry = vmf.vmf_bx;
912 sc->connectmode = APM_PROT16CONNECT;
920 * return 0 if the user will notice and handle the event,
921 * return 1 if the kernel driver should do so.
924 apm_record_event(struct apm_softc *sc, u_int event_type)
926 struct apm_event_info *evp;
928 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
929 return 1; /* no user waiting */
930 if (sc->event_count == APM_NEVENTS)
931 return 1; /* overflow */
932 if (sc->event_filter[event_type] == 0)
933 return 1; /* not registered */
934 evp = &sc->event_list[sc->event_ptr];
937 sc->event_ptr %= APM_NEVENTS;
938 evp->type = event_type;
939 evp->index = ++apm_evindex;
940 selwakeuppri(&sc->sc_rsel, PZERO);
941 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
946 apm_power_profile(struct apm_softc *sc)
949 struct apm_info info;
950 static int apm_acline = 0;
952 if (apm_get_info(&info))
955 if (apm_acline != info.ai_acline) {
956 apm_acline = info.ai_acline;
957 state = apm_acline ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY;
958 power_profile_set_state(state);
962 /* Process APM event */
964 apm_processevent(void)
967 struct apm_softc *sc = &apm_softc;
969 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
970 APM_DPRINT("Received APM Event: " #symbol "\n");
973 apm_event = apm_getevent();
975 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
976 if (apm_op_inprog == 0) {
978 if (apm_record_event(sc, apm_event)) {
979 apm_suspend(PMST_STANDBY);
983 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
984 if (apm_op_inprog == 0) {
986 if (apm_record_event(sc, apm_event)) {
987 apm_suspend(PMST_STANDBY);
991 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
992 apm_lastreq_notify();
993 if (apm_op_inprog == 0) {
995 if (apm_record_event(sc, apm_event)) {
999 return; /* XXX skip the rest */
1000 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
1001 apm_lastreq_notify();
1002 if (apm_op_inprog == 0) {
1004 if (apm_record_event(sc, apm_event)) {
1008 return; /* XXX skip the rest */
1009 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
1012 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
1013 apm_record_event(sc, apm_event);
1016 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
1017 apm_record_event(sc, apm_event);
1020 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
1021 apm_record_event(sc, apm_event);
1023 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
1024 if (apm_record_event(sc, apm_event)) {
1026 apm_suspend(PMST_SUSPEND);
1029 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
1030 apm_record_event(sc, apm_event);
1031 apm_power_profile(sc);
1033 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1034 apm_record_event(sc, apm_event);
1035 inittodr(0); /* adjust time to RTC */
1037 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1038 apm_record_event(sc, apm_event);
1039 apm_power_profile(sc);
1044 printf("Unknown Original APM Event 0x%x\n", apm_event);
1047 } while (apm_event != PMEV_NOEVENT);
1053 * Initialize APM driver
1057 apm_attach(device_t dev)
1059 struct apm_softc *sc = &apm_softc;
1062 mtx_init(&sc->mtx, device_get_nameunit(dev), "apm", MTX_DEF);
1063 cv_init(&sc->cv, "cbb cv");
1065 if (device_get_flags(dev) & 0x20)
1066 atrtcclock_disable = 1;
1068 sc->initialized = 0;
1070 /* Must be externally enabled */
1073 /* Always call HLT in idle loop */
1074 sc->always_halt_cpu = 1;
1076 getenv_int("debug.apm_debug", &apm_debug);
1078 /* print bootstrap messages */
1079 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1080 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1081 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1082 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1083 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1084 is_enabled(!sc->disabled));
1085 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1086 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1089 * In one test, apm bios version was 1.02; an attempt to register
1090 * a 1.04 driver resulted in a 1.00 connection! Registering a
1091 * 1.02 driver resulted in a 1.02 connection.
1093 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1094 for (; drv_version > 0x100; drv_version--)
1095 if (apm_driver_version(drv_version) == 0)
1097 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1098 ((drv_version & 0x000f) >> 0);
1099 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1100 ((apm_version & 0x0f00) >> 8);
1102 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1104 if (sc->intversion >= INTVERSION(1, 1))
1105 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1106 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1107 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1108 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1109 sc->majorversion, sc->minorversion);
1112 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1113 /* enable power management */
1115 if (apm_enable_disable_pm(1)) {
1116 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1117 (sc->bios.r.eax >> 8) & 0xff);
1121 /* engage power managment (APM 1.1 or later) */
1122 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1123 if (apm_engage_disengage_pm(1)) {
1124 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1125 (sc->bios.r.eax >> 8) & 0xff);
1126 APM_DPRINT(" (Docked or using external power?).\n");
1130 /* Power the system off using APM */
1131 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1134 /* Register APM again to pass the correct argument of pm_func. */
1135 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc);
1137 sc->initialized = 1;
1141 make_dev(&apm_cdevsw, APMDEV_NORMAL,
1142 UID_ROOT, GID_OPERATOR, 0664, "apm");
1143 make_dev(&apm_cdevsw, APMDEV_CTL,
1144 UID_ROOT, GID_OPERATOR, 0660, "apmctl");
1149 apmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
1151 struct apm_softc *sc = &apm_softc;
1153 if (sc == NULL || sc->initialized == 0)
1156 switch (dev2unit(dev)) {
1158 if (!(flag & FWRITE))
1160 if (sc->sc_flags & SCFLAG_OCTL)
1162 sc->sc_flags |= SCFLAG_OCTL;
1163 bzero(sc->event_filter, sizeof sc->event_filter);
1166 sc->sc_flags |= SCFLAG_ONORMAL;
1173 apmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1175 struct apm_softc *sc = &apm_softc;
1177 switch (dev2unit(dev)) {
1179 apm_lastreq_rejected();
1180 sc->sc_flags &= ~SCFLAG_OCTL;
1181 bzero(sc->event_filter, sizeof sc->event_filter);
1184 sc->sc_flags &= ~SCFLAG_ONORMAL;
1187 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1188 sc->event_count = 0;
1195 apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1197 struct apm_softc *sc = &apm_softc;
1198 struct apm_bios_arg *args;
1203 if (sc == NULL || sc->initialized == 0)
1206 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1209 if (!(flag & FWRITE))
1212 apm_suspend(PMST_SUSPEND);
1218 if (!(flag & FWRITE))
1221 apm_suspend(PMST_STANDBY);
1226 case APMIO_GETINFO_OLD:
1228 struct apm_info info;
1229 apm_info_old_t aiop;
1231 if (apm_get_info(&info))
1233 aiop = (apm_info_old_t)addr;
1234 aiop->ai_major = info.ai_major;
1235 aiop->ai_minor = info.ai_minor;
1236 aiop->ai_acline = info.ai_acline;
1237 aiop->ai_batt_stat = info.ai_batt_stat;
1238 aiop->ai_batt_life = info.ai_batt_life;
1239 aiop->ai_status = info.ai_status;
1243 if (apm_get_info((apm_info_t)addr))
1246 case APMIO_GETPWSTATUS:
1247 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1251 if (!(flag & FWRITE))
1256 if (!(flag & FWRITE))
1258 apm_event_disable();
1261 if (!(flag & FWRITE))
1265 case APMIO_NOTHALTCPU:
1266 if (!(flag & FWRITE))
1271 if (!(flag & FWRITE))
1273 newstate = *(int *)addr;
1274 if (apm_display(newstate))
1278 if (!(flag & FWRITE))
1280 /* XXX compatibility with the old interface */
1281 args = (struct apm_bios_arg *)addr;
1282 sc->bios.r.eax = args->eax;
1283 sc->bios.r.ebx = args->ebx;
1284 sc->bios.r.ecx = args->ecx;
1285 sc->bios.r.edx = args->edx;
1286 sc->bios.r.esi = args->esi;
1287 sc->bios.r.edi = args->edi;
1288 if ((ret = apm_bioscall())) {
1290 * Return code 1 means bios call was unsuccessful.
1291 * Error code is stored in %ah.
1292 * Return code -1 means bios call was unsupported
1293 * in the APM BIOS version.
1300 * Return code 0 means bios call was successful.
1301 * We need only %al and can discard %ah.
1303 sc->bios.r.eax &= 0xff;
1305 args->eax = sc->bios.r.eax;
1306 args->ebx = sc->bios.r.ebx;
1307 args->ecx = sc->bios.r.ecx;
1308 args->edx = sc->bios.r.edx;
1309 args->esi = sc->bios.r.esi;
1310 args->edi = sc->bios.r.edi;
1317 /* for /dev/apmctl */
1318 if (dev2unit(dev) == APMDEV_CTL) {
1319 struct apm_event_info *evp;
1324 case APMIO_NEXTEVENT:
1325 if (!sc->event_count) {
1328 evp = (struct apm_event_info *)addr;
1329 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1331 *evp = sc->event_list[i];
1335 case APMIO_REJECTLASTREQ:
1336 if (apm_lastreq_rejected()) {
1350 apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
1352 struct apm_softc *sc = &apm_softc;
1357 if (dev2unit(dev) != APMDEV_CTL)
1359 if (uio->uio_resid != sizeof(u_int))
1362 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1365 if (event_type < 0 || event_type >= APM_NPMEV)
1368 if (sc->event_filter[event_type] == 0) {
1373 sc->event_filter[event_type] = enabled;
1374 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1376 return uio->uio_resid;
1380 apmpoll(struct cdev *dev, int events, struct thread *td)
1382 struct apm_softc *sc = &apm_softc;
1385 if (events & (POLLIN | POLLRDNORM)) {
1386 if (sc->event_count) {
1387 revents |= events & (POLLIN | POLLRDNORM);
1389 selrecord(td, &sc->sc_rsel);
1396 static device_method_t apm_methods[] = {
1397 /* Device interface */
1398 DEVMETHOD(device_identify, apm_identify),
1399 DEVMETHOD(device_probe, apm_probe),
1400 DEVMETHOD(device_attach, apm_attach),
1405 static driver_t apm_driver = {
1408 1, /* no softc (XXX) */
1411 static devclass_t apm_devclass;
1413 DRIVER_MODULE(apm, legacy, apm_driver, apm_devclass, apm_modevent, 0);
1414 MODULE_VERSION(apm, 1);
1417 apm_pm_func(u_long cmd, void *arg, ...)
1419 int state, apm_state;
1425 case POWER_CMD_SUSPEND:
1427 state = va_arg(ap, int);
1431 case POWER_SLEEP_STATE_STANDBY:
1432 apm_state = PMST_STANDBY;
1434 case POWER_SLEEP_STATE_SUSPEND:
1435 case POWER_SLEEP_STATE_HIBERNATE:
1436 apm_state = PMST_SUSPEND;
1443 apm_suspend(apm_state);
1456 apm_pm_register(void *arg)
1459 if (!resource_disabled("apm", 0))
1460 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL);
1463 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, 0);