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)
18 * $FreeBSD: src/sys/i386/apm/apm.c,v 1.114.2.5 2002/11/02 04:41:50 iwasaki Exp $
19 * $DragonFly: src/sys/platform/pc32/apm/apm.c,v 1.10 2004/09/17 00:21:08 joerg Exp $
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/eventhandler.h>
26 #include <sys/kernel.h>
28 #include <sys/reboot.h>
30 #include <sys/select.h>
32 #include <sys/fcntl.h>
34 #include <sys/signalvar.h>
35 #include <sys/sysctl.h>
36 #include <machine/apm_bios.h>
37 #include <machine/segments.h>
38 #include <machine/clock.h>
40 #include <vm/vm_param.h>
42 #include <sys/syslog.h>
44 #include <machine/pc/bios.h>
45 #include <machine/vm86.h>
47 #include <i386/apm/apm.h>
49 /* Used by the apm_saver screen saver module */
50 int apm_display (int newstate);
51 struct apm_softc apm_softc;
53 static void apm_resume (void);
54 static int apm_bioscall(void);
55 static int apm_check_function_supported (u_int version, u_int func);
57 static u_long apm_version;
61 #define SCFLAG_ONORMAL 0x0000001
62 #define SCFLAG_OCTL 0x0000002
63 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
65 #define APMDEV(dev) (minor(dev)&0x0f)
66 #define APMDEV_NORMAL 0
69 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
71 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
73 /* Map version number to integer (keeps ordering of version numbers) */
74 #define INTVERSION(major, minor) ((major)*100 + (minor))
76 static struct callout apm_timeout_ch;
78 static timeout_t apm_timeout;
79 static d_open_t apmopen;
80 static d_close_t apmclose;
81 static d_write_t apmwrite;
82 static d_ioctl_t apmioctl;
83 static d_poll_t apmpoll;
86 static struct cdevsw apm_cdevsw = {
100 /* strategy */ nostrategy,
105 static int apm_suspend_delay = 1;
106 static int apm_standby_delay = 1;
107 static int apm_debug = 0;
109 #define APM_DPRINT(args...) do { \
115 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
116 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
117 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
120 * return 0 if the function successfull,
121 * return 1 if the function unsuccessfull,
122 * return -1 if the function unsupported.
127 struct apm_softc *sc = &apm_softc;
129 u_int apm_func = sc->bios.r.eax & 0xff;
131 if (!apm_check_function_supported(sc->intversion, apm_func)) {
132 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
133 apm_func, sc->majorversion, sc->minorversion);
138 if (sc->connectmode == APM_PROT32CONNECT) {
139 set_bios_selectors(&sc->bios.seg,
140 BIOSCODE_FLAG | BIOSDATA_FLAG);
141 errno = bios32(&sc->bios.r,
142 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
144 errno = bios16(&sc->bios, NULL);
150 /* check whether APM function is supported (1) or not (0). */
152 apm_check_function_supported(u_int version, u_int func)
154 /* except driver version */
155 if (func == APM_DRVVERSION) {
160 case INTVERSION(1, 0):
161 if (func > APM_GETPMEVENT) {
162 return (0); /* not supported */
165 case INTVERSION(1, 1):
166 if (func > APM_ENGAGEDISENGAGEPM &&
167 func < APM_OEMFUNC) {
168 return (0); /* not supported */
171 case INTVERSION(1, 2):
175 return (1); /* supported */
178 /* enable/disable power management */
180 apm_enable_disable_pm(int enable)
182 struct apm_softc *sc = &apm_softc;
184 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
186 if (sc->intversion >= INTVERSION(1, 1))
187 sc->bios.r.ebx = PMDV_ALLDEV;
189 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
190 sc->bios.r.ecx = enable;
192 return (apm_bioscall());
195 /* register driver version (APM 1.1 or later) */
197 apm_driver_version(int version)
199 struct apm_softc *sc = &apm_softc;
201 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
202 sc->bios.r.ebx = 0x0;
203 sc->bios.r.ecx = version;
206 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
209 /* Some old BIOSes don't return the connection version in %ax. */
210 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
216 /* engage/disengage power management (APM 1.1 or later) */
218 apm_engage_disengage_pm(int engage)
220 struct apm_softc *sc = &apm_softc;
222 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
223 sc->bios.r.ebx = PMDV_ALLDEV;
224 sc->bios.r.ecx = engage;
226 return (apm_bioscall());
233 struct apm_softc *sc = &apm_softc;
235 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
241 return (PMEV_NOEVENT);
242 return (sc->bios.r.ebx & 0xffff);
245 /* suspend entire system */
247 apm_suspend_system(int state)
249 struct apm_softc *sc = &apm_softc;
251 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
252 sc->bios.r.ebx = PMDV_ALLDEV;
253 sc->bios.r.ecx = state;
256 if (apm_bioscall()) {
257 printf("Entire system suspend failure: errcode = %d\n",
258 0xff & (sc->bios.r.eax >> 8));
264 /* Display control */
266 * Experimental implementation: My laptop machine can't handle this function
267 * If your laptop can control the display via APM, please inform me.
268 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
271 apm_display(int newstate)
273 struct apm_softc *sc = &apm_softc;
275 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
276 sc->bios.r.ebx = PMDV_DISP0;
277 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
279 if (apm_bioscall() == 0) {
283 /* If failed, then try to blank all display devices instead. */
284 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
285 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
286 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
288 if (apm_bioscall() == 0) {
291 printf("Display off failure: errcode = %d\n",
292 0xff & (sc->bios.r.eax >> 8));
297 * Turn off the entire system.
300 apm_power_off(void *junk, int howto)
302 struct apm_softc *sc = &apm_softc;
304 /* Not halting powering off, or not active */
305 if (!(howto & RB_POWEROFF) || !apm_softc.active)
307 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
308 sc->bios.r.ebx = PMDV_ALLDEV;
309 sc->bios.r.ecx = PMST_OFF;
311 (void) apm_bioscall();
314 /* APM Battery low handler */
316 apm_battery_low(void)
318 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
321 /* APM hook manager */
322 static struct apmhook *
323 apm_add_hook(struct apmhook **list, struct apmhook *ah)
326 struct apmhook *p, *prev;
328 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
332 panic("illegal apm_hook!");
334 for (p = *list; p != NULL; prev = p, p = p->ah_next)
335 if (p->ah_order > ah->ah_order)
342 ah->ah_next = prev->ah_next;
350 apm_del_hook(struct apmhook **list, struct apmhook *ah)
353 struct apmhook *p, *prev;
357 for (p = *list; p != NULL; prev = p, p = p->ah_next)
360 panic("Tried to delete unregistered apm_hook.");
364 prev->ah_next = p->ah_next;
372 /* APM driver calls some functions automatically */
374 apm_execute_hook(struct apmhook *list)
378 for (p = list; p != NULL; p = p->ah_next) {
379 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
380 if ((*(p->ah_fun))(p->ah_arg))
381 printf("Warning: APM hook \"%s\" failed", p->ah_name);
386 /* establish an apm hook */
388 apm_hook_establish(int apmh, struct apmhook *ah)
390 if (apmh < 0 || apmh >= NAPM_HOOK)
393 return apm_add_hook(&hook[apmh], ah);
396 /* disestablish an apm hook */
398 apm_hook_disestablish(int apmh, struct apmhook *ah)
400 if (apmh < 0 || apmh >= NAPM_HOOK)
403 apm_del_hook(&hook[apmh], ah);
407 static struct timeval suspend_time;
408 static struct timeval diff_time;
411 apm_default_resume(void *arg)
414 u_int second, minute, hour;
415 struct timeval resume_time, tmp_time;
417 /* modified for adjkerntz */
419 timer_restore(); /* restore the all timers */
420 inittodr(0); /* adjust time to RTC */
421 microtime(&resume_time);
422 getmicrotime(&tmp_time);
423 timevaladd(&tmp_time, &diff_time);
426 /* XXX THIS DOESN'T WORK!!! */
430 #ifdef APM_FIXUP_CALLTODO
431 /* Calculate the delta time suspended */
432 timevalsub(&resume_time, &suspend_time);
433 /* Fixup the calltodo list with the delta time. */
434 adjust_timeout_calltodo(&resume_time);
435 #endif /* APM_FIXUP_CALLTODOK */
437 #ifndef APM_FIXUP_CALLTODO
438 second = resume_time.tv_sec - suspend_time.tv_sec;
439 #else /* APM_FIXUP_CALLTODO */
441 * We've already calculated resume_time to be the delta between
442 * the suspend and the resume.
444 second = resume_time.tv_sec;
445 #endif /* APM_FIXUP_CALLTODO */
446 hour = second / 3600;
448 minute = second / 60;
450 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
451 hour, minute, second);
456 apm_default_suspend(void *arg)
461 microtime(&diff_time);
463 microtime(&suspend_time);
464 timevalsub(&diff_time, &suspend_time);
469 static int apm_record_event (struct apm_softc *, u_int);
470 static void apm_processevent(void);
472 static u_int apm_op_inprog = 0;
477 struct apm_softc *sc = &apm_softc;
484 sc->suspends = sc->suspend_countdown = 0;
486 if (sc->initialized) {
487 error = DEVICE_SUSPEND(root_bus);
489 DEVICE_RESUME(root_bus);
491 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
492 if (apm_suspend_system(PMST_SUSPEND) == 0) {
495 /* Failure, 'resume' the system again */
496 apm_execute_hook(hook[APM_HOOK_RESUME]);
497 DEVICE_RESUME(root_bus);
506 struct apm_softc *sc = &apm_softc;
512 sc->standbys = sc->standby_countdown = 0;
514 if (sc->initialized) {
516 * As far as standby, we don't need to execute
517 * all of suspend hooks.
519 apm_default_suspend(&apm_softc);
520 if (apm_suspend_system(PMST_STANDBY) == 0)
526 apm_lastreq_notify(void)
528 struct apm_softc *sc = &apm_softc;
530 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
531 sc->bios.r.ebx = PMDV_ALLDEV;
532 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
538 apm_lastreq_rejected(void)
540 struct apm_softc *sc = &apm_softc;
542 if (apm_op_inprog == 0) {
543 return 1; /* no operation in progress */
546 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
547 sc->bios.r.ebx = PMDV_ALLDEV;
548 sc->bios.r.ecx = PMST_LASTREQREJECT;
551 if (apm_bioscall()) {
552 APM_DPRINT("apm_lastreq_rejected: failed\n");
560 * Public interface to the suspend/resume:
562 * Execute suspend and resume hook before and after sleep, respectively.
567 apm_suspend(int state)
569 struct apm_softc *sc = &apm_softc;
571 if (!sc->initialized)
579 sc->suspend_countdown = apm_suspend_delay;
585 sc->standby_countdown = apm_standby_delay;
588 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
593 apm_lastreq_notify();
599 struct apm_softc *sc = &apm_softc;
604 if (sc->initialized) {
605 apm_execute_hook(hook[APM_HOOK_RESUME]);
606 DEVICE_RESUME(root_bus);
611 /* get power status per battery */
613 apm_get_pwstatus(apm_pwstatus_t app)
615 struct apm_softc *sc = &apm_softc;
617 if (app->ap_device != PMDV_ALLDEV &&
618 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
621 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
622 sc->bios.r.ebx = app->ap_device;
624 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
629 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
630 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
631 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
632 app->ap_batt_life = sc->bios.r.ecx & 0xff;
633 sc->bios.r.edx &= 0xffff;
634 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
635 app->ap_batt_time = -1;
636 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
637 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
638 else /* Time is in seconds */
639 app->ap_batt_time = sc->bios.r.edx;
645 /* get APM information */
647 apm_get_info(apm_info_t aip)
649 struct apm_softc *sc = &apm_softc;
650 struct apm_pwstatus aps;
652 bzero(&aps, sizeof(aps));
653 aps.ap_device = PMDV_ALLDEV;
654 if (apm_get_pwstatus(&aps))
657 aip->ai_infoversion = 1;
658 aip->ai_acline = aps.ap_acline;
659 aip->ai_batt_stat = aps.ap_batt_stat;
660 aip->ai_batt_life = aps.ap_batt_life;
661 aip->ai_batt_time = aps.ap_batt_time;
662 aip->ai_major = (u_int)sc->majorversion;
663 aip->ai_minor = (u_int)sc->minorversion;
664 aip->ai_status = (u_int)sc->active;
666 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
670 if (apm_bioscall()) {
671 aip->ai_batteries = -1; /* Unknown */
672 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
674 aip->ai_batteries = sc->bios.r.ebx & 0xff;
675 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
678 bzero(aip->ai_spare, sizeof aip->ai_spare);
684 /* inform APM BIOS that CPU is idle */
688 struct apm_softc *sc = &apm_softc;
692 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
693 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
694 (void) apm_bioscall();
697 * Some APM implementation halts CPU in BIOS, whenever
698 * "CPU-idle" function are invoked, but swtch() of
699 * FreeBSD halts CPU, therefore, CPU is halted twice
700 * in the sched loop. It makes the interrupt latency
701 * terribly long and be able to cause a serious problem
702 * in interrupt processing. We prevent it by removing
703 * "hlt" operation from swtch() and managed it under
706 if (!sc->active || sc->always_halt_cpu)
707 __asm("hlt"); /* wait for interrupt */
710 /* inform APM BIOS that CPU is busy */
714 struct apm_softc *sc = &apm_softc;
717 * The APM specification says this is only necessary if your BIOS
718 * slows down the processor in the idle task, otherwise it's not
721 if (sc->slow_idle_cpu && sc->active) {
723 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
724 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
731 * APM timeout routine:
733 * This routine is automatically called by timer once per second.
737 apm_timeout(void *dummy)
739 struct apm_softc *sc = &apm_softc;
742 apm_lastreq_notify();
744 if (sc->standbys && sc->standby_countdown-- <= 0)
747 if (sc->suspends && sc->suspend_countdown-- <= 0)
753 if (sc->active == 1) {
754 /* Run slightly more oftan than 1 Hz */
755 callout_reset(&apm_timeout_ch, hz - 1, apm_timeout, NULL);
759 /* enable APM BIOS */
761 apm_event_enable(void)
763 struct apm_softc *sc = &apm_softc;
765 APM_DPRINT("called apm_event_enable()\n");
766 if (sc->initialized) {
768 callout_init(&apm_timeout_ch);
773 /* disable APM BIOS */
775 apm_event_disable(void)
777 struct apm_softc *sc = &apm_softc;
779 APM_DPRINT("called apm_event_disable()\n");
780 if (sc->initialized) {
781 callout_stop(&apm_timeout_ch);
786 /* halt CPU in scheduling loop */
790 struct apm_softc *sc = &apm_softc;
793 sc->always_halt_cpu = 1;
796 /* don't halt CPU in scheduling loop */
798 apm_not_halt_cpu(void)
800 struct apm_softc *sc = &apm_softc;
803 sc->always_halt_cpu = 0;
806 /* device driver definitions */
809 * Create "connection point"
812 apm_identify(driver_t *driver, device_t parent)
816 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
818 panic("apm_identify");
825 apm_probe(device_t dev)
827 #define APM_KERNBASE KERNBASE
828 struct vm86frame vmf;
829 struct apm_softc *sc = &apm_softc;
832 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
836 device_set_desc(dev, "APM BIOS");
838 if ( device_get_unit(dev) > 0 ) {
839 printf("apm: Only one APM driver supported.\n");
843 if (resource_int_value("apm", 0, "flags", &flags) != 0)
846 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
847 bzero(&apm_softc, sizeof(apm_softc));
848 vmf.vmf_ah = APM_BIOS;
849 vmf.vmf_al = APM_INSTCHECK;
851 if (vm86_intcall(APM_INT, &vmf))
852 return ENXIO; /* APM not found */
853 if (vmf.vmf_bx != 0x504d) {
854 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
857 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
858 printf("apm: protected mode connections are not supported\n");
862 apm_version = vmf.vmf_ax;
863 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
864 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
865 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
867 vmf.vmf_ah = APM_BIOS;
868 vmf.vmf_al = APM_DISCONNECT;
870 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
872 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
873 vmf.vmf_ah = APM_BIOS;
874 vmf.vmf_al = APM_PROT32CONNECT;
876 if (vm86_intcall(APM_INT, &vmf)) {
877 printf("apm: 32-bit connection error.\n");
880 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
881 sc->bios.seg.code32.limit = 0xffff;
882 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
883 sc->bios.seg.code16.limit = 0xffff;
884 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
885 sc->bios.seg.data.limit = 0xffff;
886 sc->bios.entry = vmf.vmf_ebx;
887 sc->connectmode = APM_PROT32CONNECT;
889 /* use 16-bit connection */
890 vmf.vmf_ah = APM_BIOS;
891 vmf.vmf_al = APM_PROT16CONNECT;
893 if (vm86_intcall(APM_INT, &vmf)) {
894 printf("apm: 16-bit connection error.\n");
897 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
898 sc->bios.seg.code16.limit = 0xffff;
899 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
900 sc->bios.seg.data.limit = 0xffff;
901 sc->bios.entry = vmf.vmf_bx;
902 sc->connectmode = APM_PROT16CONNECT;
909 * return 0 if the user will notice and handle the event,
910 * return 1 if the kernel driver should do so.
913 apm_record_event(struct apm_softc *sc, u_int event_type)
915 struct apm_event_info *evp;
917 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
918 return 1; /* no user waiting */
919 if (sc->event_count == APM_NEVENTS)
920 return 1; /* overflow */
921 if (sc->event_filter[event_type] == 0)
922 return 1; /* not registered */
923 evp = &sc->event_list[sc->event_ptr];
926 sc->event_ptr %= APM_NEVENTS;
927 evp->type = event_type;
928 evp->index = ++apm_evindex;
929 selwakeup(&sc->sc_rsel);
930 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
933 /* Process APM event */
935 apm_processevent(void)
938 struct apm_softc *sc = &apm_softc;
940 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
941 APM_DPRINT("Received APM Event: " #symbol "\n");
944 apm_event = apm_getevent();
946 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
947 if (apm_op_inprog == 0) {
949 if (apm_record_event(sc, apm_event)) {
950 apm_suspend(PMST_STANDBY);
954 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
955 if (apm_op_inprog == 0) {
957 if (apm_record_event(sc, apm_event)) {
958 apm_suspend(PMST_STANDBY);
962 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
963 apm_lastreq_notify();
964 if (apm_op_inprog == 0) {
966 if (apm_record_event(sc, apm_event)) {
970 return; /* XXX skip the rest */
971 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
972 apm_lastreq_notify();
973 if (apm_op_inprog == 0) {
975 if (apm_record_event(sc, apm_event)) {
979 return; /* XXX skip the rest */
980 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
983 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
984 apm_record_event(sc, apm_event);
987 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
988 apm_record_event(sc, apm_event);
991 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
992 apm_record_event(sc, apm_event);
995 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
996 if (apm_record_event(sc, apm_event)) {
998 apm_suspend(PMST_SUSPEND);
1001 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
1002 apm_record_event(sc, apm_event);
1004 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1005 apm_record_event(sc, apm_event);
1006 inittodr(0); /* adjust time to RTC */
1008 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1009 apm_record_event(sc, apm_event);
1014 printf("Unknown Original APM Event 0x%x\n", apm_event);
1017 } while (apm_event != PMEV_NOEVENT);
1023 * Initialize APM driver
1027 apm_attach(device_t dev)
1029 struct apm_softc *sc = &apm_softc;
1033 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1036 sc->initialized = 0;
1038 /* Must be externally enabled */
1041 /* Always call HLT in idle loop */
1042 sc->always_halt_cpu = 1;
1044 getenv_int("debug.apm_debug", &apm_debug);
1046 /* print bootstrap messages */
1047 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1048 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1049 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1050 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1051 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1052 is_enabled(!sc->disabled));
1053 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1054 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1057 * In one test, apm bios version was 1.02; an attempt to register
1058 * a 1.04 driver resulted in a 1.00 connection! Registering a
1059 * 1.02 driver resulted in a 1.02 connection.
1061 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1062 for (; drv_version > 0x100; drv_version--)
1063 if (apm_driver_version(drv_version) == 0)
1065 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1066 ((drv_version & 0x000f) >> 0);
1067 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1068 ((apm_version & 0x0f00) >> 8);
1070 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1072 if (sc->intversion >= INTVERSION(1, 1))
1073 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1074 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1075 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1076 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1077 sc->majorversion, sc->minorversion);
1080 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1081 /* enable power management */
1083 if (apm_enable_disable_pm(1)) {
1084 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1085 (sc->bios.r.eax >> 8) & 0xff);
1089 /* engage power managment (APM 1.1 or later) */
1090 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1091 if (apm_engage_disengage_pm(1)) {
1092 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1093 (sc->bios.r.eax >> 8) & 0xff);
1094 APM_DPRINT(" (Docked or using external power?).\n");
1098 /* default suspend hook */
1099 sc->sc_suspend.ah_fun = apm_default_suspend;
1100 sc->sc_suspend.ah_arg = sc;
1101 sc->sc_suspend.ah_name = "default suspend";
1102 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1104 /* default resume hook */
1105 sc->sc_resume.ah_fun = apm_default_resume;
1106 sc->sc_resume.ah_arg = sc;
1107 sc->sc_resume.ah_name = "default resume";
1108 sc->sc_resume.ah_order = APM_MIN_ORDER;
1110 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1111 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1113 /* Power the system off using APM */
1114 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1117 sc->initialized = 1;
1119 cdevsw_add(&apm_cdevsw, 0, 0);
1120 make_dev(&apm_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0660, "apm");
1121 make_dev(&apm_cdevsw, 8, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
1126 apmopen(dev_t dev, int flag, int fmt, d_thread_t *td)
1128 struct apm_softc *sc = &apm_softc;
1129 int ctl = APMDEV(dev);
1131 if (!sc->initialized)
1136 if (!(flag & FWRITE))
1138 if (sc->sc_flags & SCFLAG_OCTL)
1140 sc->sc_flags |= SCFLAG_OCTL;
1141 bzero(sc->event_filter, sizeof sc->event_filter);
1144 sc->sc_flags |= SCFLAG_ONORMAL;
1154 apmclose(dev_t dev, int flag, int fmt, d_thread_t *td)
1156 struct apm_softc *sc = &apm_softc;
1157 int ctl = APMDEV(dev);
1161 apm_lastreq_rejected();
1162 sc->sc_flags &= ~SCFLAG_OCTL;
1163 bzero(sc->event_filter, sizeof sc->event_filter);
1166 sc->sc_flags &= ~SCFLAG_ONORMAL;
1169 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1170 sc->event_count = 0;
1177 apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
1179 struct apm_softc *sc = &apm_softc;
1180 struct apm_bios_arg *args;
1185 if (!sc->initialized)
1187 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1190 if (!(flag & FWRITE))
1193 apm_suspend(PMST_SUSPEND);
1199 if (!(flag & FWRITE))
1202 apm_suspend(PMST_STANDBY);
1207 case APMIO_GETINFO_OLD:
1209 struct apm_info info;
1210 apm_info_old_t aiop;
1212 if (apm_get_info(&info))
1214 aiop = (apm_info_old_t)addr;
1215 aiop->ai_major = info.ai_major;
1216 aiop->ai_minor = info.ai_minor;
1217 aiop->ai_acline = info.ai_acline;
1218 aiop->ai_batt_stat = info.ai_batt_stat;
1219 aiop->ai_batt_life = info.ai_batt_life;
1220 aiop->ai_status = info.ai_status;
1224 if (apm_get_info((apm_info_t)addr))
1227 case APMIO_GETPWSTATUS:
1228 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1232 if (!(flag & FWRITE))
1237 if (!(flag & FWRITE))
1239 apm_event_disable();
1242 if (!(flag & FWRITE))
1246 case APMIO_NOTHALTCPU:
1247 if (!(flag & FWRITE))
1252 if (!(flag & FWRITE))
1254 newstate = *(int *)addr;
1255 if (apm_display(newstate))
1259 if (!(flag & FWRITE))
1261 /* XXX compatibility with the old interface */
1262 args = (struct apm_bios_arg *)addr;
1263 sc->bios.r.eax = args->eax;
1264 sc->bios.r.ebx = args->ebx;
1265 sc->bios.r.ecx = args->ecx;
1266 sc->bios.r.edx = args->edx;
1267 sc->bios.r.esi = args->esi;
1268 sc->bios.r.edi = args->edi;
1269 if ((ret = apm_bioscall())) {
1271 * Return code 1 means bios call was unsuccessful.
1272 * Error code is stored in %ah.
1273 * Return code -1 means bios call was unsupported
1274 * in the APM BIOS version.
1281 * Return code 0 means bios call was successful.
1282 * We need only %al and can discard %ah.
1284 sc->bios.r.eax &= 0xff;
1286 args->eax = sc->bios.r.eax;
1287 args->ebx = sc->bios.r.ebx;
1288 args->ecx = sc->bios.r.ecx;
1289 args->edx = sc->bios.r.edx;
1290 args->esi = sc->bios.r.esi;
1291 args->edi = sc->bios.r.edi;
1298 /* for /dev/apmctl */
1299 if (APMDEV(dev) == APMDEV_CTL) {
1300 struct apm_event_info *evp;
1305 case APMIO_NEXTEVENT:
1306 if (!sc->event_count) {
1309 evp = (struct apm_event_info *)addr;
1310 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1312 *evp = sc->event_list[i];
1316 case APMIO_REJECTLASTREQ:
1317 if (apm_lastreq_rejected()) {
1331 apmwrite(dev_t dev, struct uio *uio, int ioflag)
1333 struct apm_softc *sc = &apm_softc;
1338 if (APMDEV(dev) != APMDEV_CTL)
1340 if (uio->uio_resid != sizeof(u_int))
1343 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1346 if (event_type < 0 || event_type >= APM_NPMEV)
1349 if (sc->event_filter[event_type] == 0) {
1354 sc->event_filter[event_type] = enabled;
1355 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1357 return uio->uio_resid;
1361 apmpoll(dev_t dev, int events, d_thread_t *td)
1363 struct apm_softc *sc = &apm_softc;
1366 if (events & (POLLIN | POLLRDNORM)) {
1367 if (sc->event_count) {
1368 revents |= events & (POLLIN | POLLRDNORM);
1370 selrecord(td, &sc->sc_rsel);
1377 static device_method_t apm_methods[] = {
1378 /* Device interface */
1379 DEVMETHOD(device_identify, apm_identify),
1380 DEVMETHOD(device_probe, apm_probe),
1381 DEVMETHOD(device_attach, apm_attach),
1386 static driver_t apm_driver = {
1389 1, /* no softc (XXX) */
1392 static devclass_t apm_devclass;
1394 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);