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.21 2006/12/23 00:27:03 swildner Exp $
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/eventhandler.h>
26 #include <sys/device.h>
27 #include <sys/kernel.h>
29 #include <sys/reboot.h>
31 #include <sys/selinfo.h>
33 #include <sys/event.h>
34 #include <sys/fcntl.h>
36 #include <sys/signalvar.h>
37 #include <sys/sysctl.h>
38 #include <machine/apm_bios.h>
39 #include <machine/segments.h>
40 #include <machine/clock.h>
42 #include <vm/vm_param.h>
44 #include <sys/syslog.h>
45 #include <sys/thread2.h>
47 #include <machine/pc/bios.h>
48 #include <machine/vm86.h>
50 #include <machine_base/apm/apm.h>
52 /* Used by the apm_saver screen saver module */
53 int apm_display (int newstate);
54 struct apm_softc apm_softc;
56 static void apm_resume (void);
57 static int apm_bioscall(void);
58 static int apm_check_function_supported (u_int version, u_int func);
60 static u_long apm_version;
64 #define SCFLAG_ONORMAL 0x0000001
65 #define SCFLAG_OCTL 0x0000002
66 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
68 #define APMDEV(dev) (minor(dev)&0x0f)
69 #define APMDEV_NORMAL 0
72 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
74 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
76 /* Map version number to integer (keeps ordering of version numbers) */
77 #define INTVERSION(major, minor) ((major)*100 + (minor))
79 static struct callout apm_timeout_ch;
81 static timeout_t apm_timeout;
82 static d_open_t apmopen;
83 static d_close_t apmclose;
84 static d_write_t apmwrite;
85 static d_ioctl_t apmioctl;
86 static d_poll_t apmpoll;
87 static d_kqfilter_t apmkqfilter;
89 static void apmfilter_detach(struct knote *);
90 static int apmfilter(struct knote *, long);
93 static struct dev_ops apm_ops = {
94 { "apm", CDEV_MAJOR, D_KQFILTER },
100 .d_kqfilter = apmkqfilter
103 static int apm_suspend_delay = 1;
104 static int apm_standby_delay = 1;
105 static int apm_debug = 0;
107 #define APM_DPRINT(args...) do { \
113 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
114 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
115 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
118 * return 0 if the function successfull,
119 * return 1 if the function unsuccessfull,
120 * return -1 if the function unsupported.
125 struct apm_softc *sc = &apm_softc;
127 u_int apm_func = sc->bios.r.eax & 0xff;
129 if (!apm_check_function_supported(sc->intversion, apm_func)) {
130 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
131 apm_func, sc->majorversion, sc->minorversion);
136 if (sc->connectmode == APM_PROT32CONNECT) {
137 set_bios_selectors(&sc->bios.seg,
138 BIOSCODE_FLAG | BIOSDATA_FLAG);
139 error = bios32(&sc->bios.r,
140 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
142 error = bios16(&sc->bios, NULL);
148 /* check whether APM function is supported (1) or not (0). */
150 apm_check_function_supported(u_int version, u_int func)
152 /* except driver version */
153 if (func == APM_DRVVERSION) {
158 case INTVERSION(1, 0):
159 if (func > APM_GETPMEVENT) {
160 return (0); /* not supported */
163 case INTVERSION(1, 1):
164 if (func > APM_ENGAGEDISENGAGEPM &&
165 func < APM_OEMFUNC) {
166 return (0); /* not supported */
169 case INTVERSION(1, 2):
173 return (1); /* supported */
176 /* enable/disable power management */
178 apm_enable_disable_pm(int enable)
180 struct apm_softc *sc = &apm_softc;
182 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
184 if (sc->intversion >= INTVERSION(1, 1))
185 sc->bios.r.ebx = PMDV_ALLDEV;
187 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
188 sc->bios.r.ecx = enable;
190 return (apm_bioscall());
193 /* register driver version (APM 1.1 or later) */
195 apm_driver_version(int version)
197 struct apm_softc *sc = &apm_softc;
199 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
200 sc->bios.r.ebx = 0x0;
201 sc->bios.r.ecx = version;
204 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
207 /* Some old BIOSes don't return the connection version in %ax. */
208 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
214 /* engage/disengage power management (APM 1.1 or later) */
216 apm_engage_disengage_pm(int engage)
218 struct apm_softc *sc = &apm_softc;
220 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
221 sc->bios.r.ebx = PMDV_ALLDEV;
222 sc->bios.r.ecx = engage;
224 return (apm_bioscall());
231 struct apm_softc *sc = &apm_softc;
233 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
239 return (PMEV_NOEVENT);
240 return (sc->bios.r.ebx & 0xffff);
243 /* suspend entire system */
245 apm_suspend_system(int state)
247 struct apm_softc *sc = &apm_softc;
249 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
250 sc->bios.r.ebx = PMDV_ALLDEV;
251 sc->bios.r.ecx = state;
254 if (apm_bioscall()) {
255 kprintf("Entire system suspend failure: errcode = %d\n",
256 0xff & (sc->bios.r.eax >> 8));
262 /* Display control */
264 * Experimental implementation: My laptop machine can't handle this function
265 * If your laptop can control the display via APM, please inform me.
266 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
269 apm_display(int newstate)
271 struct apm_softc *sc = &apm_softc;
273 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
274 sc->bios.r.ebx = PMDV_DISP0;
275 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
277 if (apm_bioscall() == 0) {
281 /* If failed, then try to blank all display devices instead. */
282 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
283 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
284 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
286 if (apm_bioscall() == 0) {
289 kprintf("Display off failure: errcode = %d\n",
290 0xff & (sc->bios.r.eax >> 8));
295 * Turn off the entire system.
298 apm_power_off(void *junk, int howto)
300 struct apm_softc *sc = &apm_softc;
302 /* Not halting powering off, or not active */
303 if (!(howto & RB_POWEROFF) || !apm_softc.active)
305 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
306 sc->bios.r.ebx = PMDV_ALLDEV;
307 sc->bios.r.ecx = PMST_OFF;
312 /* APM Battery low handler */
314 apm_battery_low(void)
316 kprintf("\007\007 * * * BATTERY IS LOW * * * \007\007");
319 /* APM hook manager */
320 static struct apmhook *
321 apm_add_hook(struct apmhook **list, struct apmhook *ah)
323 struct apmhook *p, *prev;
325 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
329 panic("illegal apm_hook!");
331 for (p = *list; p != NULL; prev = p, p = p->ah_next)
332 if (p->ah_order > ah->ah_order)
339 ah->ah_next = prev->ah_next;
347 apm_del_hook(struct apmhook **list, struct apmhook *ah)
349 struct apmhook *p, *prev;
353 for (p = *list; p != NULL; prev = p, p = p->ah_next)
356 panic("Tried to delete unregistered apm_hook.");
360 prev->ah_next = p->ah_next;
368 /* APM driver calls some functions automatically */
370 apm_execute_hook(struct apmhook *list)
374 for (p = list; p != NULL; p = p->ah_next) {
375 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
376 if ((*(p->ah_fun))(p->ah_arg))
377 kprintf("Warning: APM hook \"%s\" failed", p->ah_name);
382 /* establish an apm hook */
384 apm_hook_establish(int apmh, struct apmhook *ah)
386 if (apmh < 0 || apmh >= NAPM_HOOK)
389 return apm_add_hook(&hook[apmh], ah);
392 /* disestablish an apm hook */
394 apm_hook_disestablish(int apmh, struct apmhook *ah)
396 if (apmh < 0 || apmh >= NAPM_HOOK)
399 apm_del_hook(&hook[apmh], ah);
403 static struct timeval suspend_time;
404 static struct timeval diff_time;
407 apm_default_resume(void *arg)
409 u_int second, minute, hour;
410 struct timeval resume_time, tmp_time;
412 /* modified for adjkerntz */
414 timer_restore(); /* restore the all timers */
415 inittodr(0); /* adjust time to RTC */
416 microtime(&resume_time);
417 getmicrotime(&tmp_time);
418 timevaladd(&tmp_time, &diff_time);
421 /* XXX THIS DOESN'T WORK!!! */
425 #ifdef APM_FIXUP_CALLTODO
426 /* Calculate the delta time suspended */
427 timevalsub(&resume_time, &suspend_time);
428 /* Fixup the calltodo list with the delta time. */
429 adjust_timeout_calltodo(&resume_time);
430 #endif /* APM_FIXUP_CALLTODOK */
432 #ifndef APM_FIXUP_CALLTODO
433 second = resume_time.tv_sec - suspend_time.tv_sec;
434 #else /* APM_FIXUP_CALLTODO */
436 * We've already calculated resume_time to be the delta between
437 * the suspend and the resume.
439 second = resume_time.tv_sec;
440 #endif /* APM_FIXUP_CALLTODO */
441 hour = second / 3600;
443 minute = second / 60;
445 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
446 hour, minute, second);
451 apm_default_suspend(void *arg)
454 microtime(&diff_time);
456 microtime(&suspend_time);
457 timevalsub(&diff_time, &suspend_time);
462 static int apm_record_event (struct apm_softc *, u_int);
463 static void apm_processevent(void);
465 static u_int apm_op_inprog = 0;
470 struct apm_softc *sc = &apm_softc;
477 sc->suspends = sc->suspend_countdown = 0;
479 if (sc->initialized) {
480 error = DEVICE_SUSPEND(root_bus);
482 DEVICE_RESUME(root_bus);
484 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
485 if (apm_suspend_system(PMST_SUSPEND) == 0) {
488 /* Failure, 'resume' the system again */
489 apm_execute_hook(hook[APM_HOOK_RESUME]);
490 DEVICE_RESUME(root_bus);
499 struct apm_softc *sc = &apm_softc;
505 sc->standbys = sc->standby_countdown = 0;
507 if (sc->initialized) {
509 * As far as standby, we don't need to execute
510 * all of suspend hooks.
512 apm_default_suspend(&apm_softc);
513 if (apm_suspend_system(PMST_STANDBY) == 0)
519 apm_lastreq_notify(void)
521 struct apm_softc *sc = &apm_softc;
523 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
524 sc->bios.r.ebx = PMDV_ALLDEV;
525 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
531 apm_lastreq_rejected(void)
533 struct apm_softc *sc = &apm_softc;
535 if (apm_op_inprog == 0) {
536 return 1; /* no operation in progress */
539 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
540 sc->bios.r.ebx = PMDV_ALLDEV;
541 sc->bios.r.ecx = PMST_LASTREQREJECT;
544 if (apm_bioscall()) {
545 APM_DPRINT("apm_lastreq_rejected: failed\n");
553 * Public interface to the suspend/resume:
555 * Execute suspend and resume hook before and after sleep, respectively.
560 apm_suspend(int state)
562 struct apm_softc *sc = &apm_softc;
564 if (!sc->initialized)
572 sc->suspend_countdown = apm_suspend_delay;
578 sc->standby_countdown = apm_standby_delay;
581 kprintf("apm_suspend: Unknown Suspend state 0x%x\n", state);
586 apm_lastreq_notify();
592 struct apm_softc *sc = &apm_softc;
597 if (sc->initialized) {
598 apm_execute_hook(hook[APM_HOOK_RESUME]);
599 DEVICE_RESUME(root_bus);
604 /* get power status per battery */
606 apm_get_pwstatus(apm_pwstatus_t app)
608 struct apm_softc *sc = &apm_softc;
610 if (app->ap_device != PMDV_ALLDEV &&
611 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
614 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
615 sc->bios.r.ebx = app->ap_device;
617 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
622 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
623 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
624 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
625 app->ap_batt_life = sc->bios.r.ecx & 0xff;
626 sc->bios.r.edx &= 0xffff;
627 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
628 app->ap_batt_time = -1;
629 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
630 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
631 else /* Time is in seconds */
632 app->ap_batt_time = sc->bios.r.edx;
638 /* get APM information */
640 apm_get_info(apm_info_t aip)
642 struct apm_softc *sc = &apm_softc;
643 struct apm_pwstatus aps;
645 bzero(&aps, sizeof(aps));
646 aps.ap_device = PMDV_ALLDEV;
647 if (apm_get_pwstatus(&aps))
650 aip->ai_infoversion = 1;
651 aip->ai_acline = aps.ap_acline;
652 aip->ai_batt_stat = aps.ap_batt_stat;
653 aip->ai_batt_life = aps.ap_batt_life;
654 aip->ai_batt_time = aps.ap_batt_time;
655 aip->ai_major = (u_int)sc->majorversion;
656 aip->ai_minor = (u_int)sc->minorversion;
657 aip->ai_status = (u_int)sc->active;
659 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
663 if (apm_bioscall()) {
664 aip->ai_batteries = -1; /* Unknown */
665 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
667 aip->ai_batteries = sc->bios.r.ebx & 0xff;
668 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
671 bzero(aip->ai_spare, sizeof aip->ai_spare);
677 /* inform APM BIOS that CPU is idle */
681 struct apm_softc *sc = &apm_softc;
685 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
686 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
690 * Some APM implementation halts CPU in BIOS, whenever
691 * "CPU-idle" function are invoked, but swtch() of
692 * FreeBSD halts CPU, therefore, CPU is halted twice
693 * in the sched loop. It makes the interrupt latency
694 * terribly long and be able to cause a serious problem
695 * in interrupt processing. We prevent it by removing
696 * "hlt" operation from swtch() and managed it under
699 if (!sc->active || sc->always_halt_cpu)
700 __asm("hlt"); /* wait for interrupt */
703 /* inform APM BIOS that CPU is busy */
707 struct apm_softc *sc = &apm_softc;
710 * The APM specification says this is only necessary if your BIOS
711 * slows down the processor in the idle task, otherwise it's not
714 if (sc->slow_idle_cpu && sc->active) {
716 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
717 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
724 * APM timeout routine:
726 * This routine is automatically called by timer once per second.
730 apm_timeout(void *dummy)
732 struct apm_softc *sc = &apm_softc;
735 apm_lastreq_notify();
737 if (sc->standbys && sc->standby_countdown-- <= 0)
740 if (sc->suspends && sc->suspend_countdown-- <= 0)
746 if (sc->active == 1) {
747 /* Run slightly more oftan than 1 Hz */
748 callout_reset(&apm_timeout_ch, hz - 1, apm_timeout, NULL);
752 /* enable APM BIOS */
754 apm_event_enable(void)
756 struct apm_softc *sc = &apm_softc;
758 APM_DPRINT("called apm_event_enable()\n");
759 if (sc->initialized) {
761 callout_init(&apm_timeout_ch);
766 /* disable APM BIOS */
768 apm_event_disable(void)
770 struct apm_softc *sc = &apm_softc;
772 APM_DPRINT("called apm_event_disable()\n");
773 if (sc->initialized) {
774 callout_stop(&apm_timeout_ch);
779 /* halt CPU in scheduling loop */
783 struct apm_softc *sc = &apm_softc;
786 sc->always_halt_cpu = 1;
789 /* don't halt CPU in scheduling loop */
791 apm_not_halt_cpu(void)
793 struct apm_softc *sc = &apm_softc;
796 sc->always_halt_cpu = 0;
799 /* device driver definitions */
805 apm_probe(device_t dev)
807 #define APM_KERNBASE KERNBASE
808 struct vm86frame vmf;
809 struct apm_softc *sc = &apm_softc;
812 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
816 device_set_desc(dev, "APM BIOS");
818 if ( device_get_unit(dev) > 0 ) {
819 kprintf("apm: Only one APM driver supported.\n");
823 if (resource_int_value("apm", 0, "flags", &flags) != 0)
826 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
827 bzero(&apm_softc, sizeof(apm_softc));
828 vmf.vmf_ah = APM_BIOS;
829 vmf.vmf_al = APM_INSTCHECK;
831 if (vm86_intcall(APM_INT, &vmf))
832 return ENXIO; /* APM not found */
833 if (vmf.vmf_bx != 0x504d) {
834 kprintf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
837 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
838 kprintf("apm: protected mode connections are not supported\n");
842 apm_version = vmf.vmf_ax;
843 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
844 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
845 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
847 vmf.vmf_ah = APM_BIOS;
848 vmf.vmf_al = APM_DISCONNECT;
850 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
852 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
853 vmf.vmf_ah = APM_BIOS;
854 vmf.vmf_al = APM_PROT32CONNECT;
856 if (vm86_intcall(APM_INT, &vmf)) {
857 kprintf("apm: 32-bit connection error.\n");
860 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
861 sc->bios.seg.code32.limit = 0xffff;
862 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
863 sc->bios.seg.code16.limit = 0xffff;
864 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
865 sc->bios.seg.data.limit = 0xffff;
866 sc->bios.entry = vmf.vmf_ebx;
867 sc->connectmode = APM_PROT32CONNECT;
869 /* use 16-bit connection */
870 vmf.vmf_ah = APM_BIOS;
871 vmf.vmf_al = APM_PROT16CONNECT;
873 if (vm86_intcall(APM_INT, &vmf)) {
874 kprintf("apm: 16-bit connection error.\n");
877 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
878 sc->bios.seg.code16.limit = 0xffff;
879 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
880 sc->bios.seg.data.limit = 0xffff;
881 sc->bios.entry = vmf.vmf_bx;
882 sc->connectmode = APM_PROT16CONNECT;
889 * return 0 if the user will notice and handle the event,
890 * return 1 if the kernel driver should do so.
893 apm_record_event(struct apm_softc *sc, u_int event_type)
895 struct apm_event_info *evp;
897 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
898 return 1; /* no user waiting */
899 if (sc->event_count == APM_NEVENTS)
900 return 1; /* overflow */
901 if (sc->event_filter[event_type] == 0)
902 return 1; /* not registered */
903 evp = &sc->event_list[sc->event_ptr];
906 sc->event_ptr %= APM_NEVENTS;
907 evp->type = event_type;
908 evp->index = ++apm_evindex;
909 selwakeup(&sc->sc_rsel);
910 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
913 /* Process APM event */
915 apm_processevent(void)
918 struct apm_softc *sc = &apm_softc;
920 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
921 APM_DPRINT("Received APM Event: " #symbol "\n");
924 apm_event = apm_getevent();
926 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
927 if (apm_op_inprog == 0) {
929 if (apm_record_event(sc, apm_event)) {
930 apm_suspend(PMST_STANDBY);
934 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
935 if (apm_op_inprog == 0) {
937 if (apm_record_event(sc, apm_event)) {
938 apm_suspend(PMST_STANDBY);
942 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
943 apm_lastreq_notify();
944 if (apm_op_inprog == 0) {
946 if (apm_record_event(sc, apm_event)) {
950 return; /* XXX skip the rest */
951 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
952 apm_lastreq_notify();
953 if (apm_op_inprog == 0) {
955 if (apm_record_event(sc, apm_event)) {
959 return; /* XXX skip the rest */
960 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
963 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
964 apm_record_event(sc, apm_event);
967 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
968 apm_record_event(sc, apm_event);
971 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
972 apm_record_event(sc, apm_event);
975 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
976 if (apm_record_event(sc, apm_event)) {
978 apm_suspend(PMST_SUSPEND);
981 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
982 apm_record_event(sc, apm_event);
984 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
985 apm_record_event(sc, apm_event);
986 inittodr(0); /* adjust time to RTC */
988 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
989 apm_record_event(sc, apm_event);
994 kprintf("Unknown Original APM Event 0x%x\n", apm_event);
997 } while (apm_event != PMEV_NOEVENT);
1003 * Initialize APM driver
1007 apm_attach(device_t dev)
1009 struct apm_softc *sc = &apm_softc;
1013 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1016 sc->initialized = 0;
1018 /* Must be externally enabled */
1021 /* Always call HLT in idle loop */
1022 sc->always_halt_cpu = 1;
1024 kgetenv_int("debug.apm_debug", &apm_debug);
1026 /* print bootstrap messages */
1027 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1028 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1029 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1030 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1031 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1032 is_enabled(!sc->disabled));
1033 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1034 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1037 * In one test, apm bios version was 1.02; an attempt to register
1038 * a 1.04 driver resulted in a 1.00 connection! Registering a
1039 * 1.02 driver resulted in a 1.02 connection.
1041 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1042 for (; drv_version > 0x100; drv_version--)
1043 if (apm_driver_version(drv_version) == 0)
1045 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1046 ((drv_version & 0x000f) >> 0);
1047 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1048 ((apm_version & 0x0f00) >> 8);
1050 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1052 if (sc->intversion >= INTVERSION(1, 1))
1053 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1054 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1055 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1056 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1057 sc->majorversion, sc->minorversion);
1060 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1061 /* enable power management */
1063 if (apm_enable_disable_pm(1)) {
1064 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1065 (sc->bios.r.eax >> 8) & 0xff);
1069 /* engage power managment (APM 1.1 or later) */
1070 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1071 if (apm_engage_disengage_pm(1)) {
1072 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1073 (sc->bios.r.eax >> 8) & 0xff);
1074 APM_DPRINT(" (Docked or using external power?).\n");
1078 /* default suspend hook */
1079 sc->sc_suspend.ah_fun = apm_default_suspend;
1080 sc->sc_suspend.ah_arg = sc;
1081 sc->sc_suspend.ah_name = "default suspend";
1082 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1084 /* default resume hook */
1085 sc->sc_resume.ah_fun = apm_default_resume;
1086 sc->sc_resume.ah_arg = sc;
1087 sc->sc_resume.ah_name = "default resume";
1088 sc->sc_resume.ah_order = APM_MIN_ORDER;
1090 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1091 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1093 /* Power the system off using APM */
1094 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1097 sc->initialized = 1;
1099 make_dev(&apm_ops, 0, UID_ROOT, GID_OPERATOR, 0660, "apm");
1100 make_dev(&apm_ops, 8, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
1105 apmopen(struct dev_open_args *ap)
1107 cdev_t dev = ap->a_head.a_dev;
1108 struct apm_softc *sc = &apm_softc;
1109 int ctl = APMDEV(dev);
1111 if (!sc->initialized)
1116 if (!(ap->a_oflags & FWRITE))
1118 if (sc->sc_flags & SCFLAG_OCTL)
1120 sc->sc_flags |= SCFLAG_OCTL;
1121 bzero(sc->event_filter, sizeof sc->event_filter);
1124 sc->sc_flags |= SCFLAG_ONORMAL;
1134 apmclose(struct dev_close_args *ap)
1136 cdev_t dev = ap->a_head.a_dev;
1137 struct apm_softc *sc = &apm_softc;
1138 int ctl = APMDEV(dev);
1142 apm_lastreq_rejected();
1143 sc->sc_flags &= ~SCFLAG_OCTL;
1144 bzero(sc->event_filter, sizeof sc->event_filter);
1147 sc->sc_flags &= ~SCFLAG_ONORMAL;
1150 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1151 sc->event_count = 0;
1158 apmioctl(struct dev_ioctl_args *ap)
1160 cdev_t dev = ap->a_head.a_dev;
1161 struct apm_softc *sc = &apm_softc;
1162 struct apm_bios_arg *args;
1167 if (!sc->initialized)
1169 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", ap->a_cmd);
1171 switch (ap->a_cmd) {
1173 if (!(ap->a_fflag & FWRITE))
1176 apm_suspend(PMST_SUSPEND);
1182 if (!(ap->a_fflag & FWRITE))
1185 apm_suspend(PMST_STANDBY);
1190 case APMIO_GETINFO_OLD:
1192 struct apm_info info;
1193 apm_info_old_t aiop;
1195 if (apm_get_info(&info))
1197 aiop = (apm_info_old_t)ap->a_data;
1198 aiop->ai_major = info.ai_major;
1199 aiop->ai_minor = info.ai_minor;
1200 aiop->ai_acline = info.ai_acline;
1201 aiop->ai_batt_stat = info.ai_batt_stat;
1202 aiop->ai_batt_life = info.ai_batt_life;
1203 aiop->ai_status = info.ai_status;
1207 if (apm_get_info((apm_info_t)ap->a_data))
1210 case APMIO_GETPWSTATUS:
1211 if (apm_get_pwstatus((apm_pwstatus_t)ap->a_data))
1215 if (!(ap->a_fflag & FWRITE))
1220 if (!(ap->a_fflag & FWRITE))
1222 apm_event_disable();
1225 if (!(ap->a_fflag & FWRITE))
1229 case APMIO_NOTHALTCPU:
1230 if (!(ap->a_fflag & FWRITE))
1235 if (!(ap->a_fflag & FWRITE))
1237 newstate = *(int *)ap->a_data;
1238 if (apm_display(newstate))
1242 if (!(ap->a_fflag & FWRITE))
1244 /* XXX compatibility with the old interface */
1245 args = (struct apm_bios_arg *)ap->a_data;
1246 sc->bios.r.eax = args->eax;
1247 sc->bios.r.ebx = args->ebx;
1248 sc->bios.r.ecx = args->ecx;
1249 sc->bios.r.edx = args->edx;
1250 sc->bios.r.esi = args->esi;
1251 sc->bios.r.edi = args->edi;
1252 if ((ret = apm_bioscall())) {
1254 * Return code 1 means bios call was unsuccessful.
1255 * Error code is stored in %ah.
1256 * Return code -1 means bios call was unsupported
1257 * in the APM BIOS version.
1264 * Return code 0 means bios call was successful.
1265 * We need only %al and can discard %ah.
1267 sc->bios.r.eax &= 0xff;
1269 args->eax = sc->bios.r.eax;
1270 args->ebx = sc->bios.r.ebx;
1271 args->ecx = sc->bios.r.ecx;
1272 args->edx = sc->bios.r.edx;
1273 args->esi = sc->bios.r.esi;
1274 args->edi = sc->bios.r.edi;
1281 /* for /dev/apmctl */
1282 if (APMDEV(dev) == APMDEV_CTL) {
1283 struct apm_event_info *evp;
1287 switch (ap->a_cmd) {
1288 case APMIO_NEXTEVENT:
1289 if (!sc->event_count) {
1292 evp = (struct apm_event_info *)ap->a_data;
1293 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1295 *evp = sc->event_list[i];
1299 case APMIO_REJECTLASTREQ:
1300 if (apm_lastreq_rejected()) {
1314 apmwrite(struct dev_write_args *ap)
1316 cdev_t dev = ap->a_head.a_dev;
1317 struct uio *uio = ap->a_uio;
1318 struct apm_softc *sc = &apm_softc;
1323 if (APMDEV(dev) != APMDEV_CTL)
1325 if (uio->uio_resid != sizeof(u_int))
1328 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1331 if (event_type < 0 || event_type >= APM_NPMEV)
1334 if (sc->event_filter[event_type] == 0) {
1339 sc->event_filter[event_type] = enabled;
1340 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1342 return uio->uio_resid;
1346 apmpoll(struct dev_poll_args *ap)
1348 struct apm_softc *sc = &apm_softc;
1351 if (ap->a_events & (POLLIN | POLLRDNORM)) {
1352 if (sc->event_count) {
1353 revents |= ap->a_events & (POLLIN | POLLRDNORM);
1355 selrecord(curthread, &sc->sc_rsel);
1358 ap->a_events = revents;
1362 static struct filterops apmfiltops =
1363 { 1, NULL, apmfilter_detach, apmfilter };
1366 apmkqfilter(struct dev_kqfilter_args *ap)
1368 struct apm_softc *sc = &apm_softc;
1369 struct knote *kn = ap->a_kn;
1370 struct klist *klist;
1374 switch (kn->kn_filter) {
1376 kn->kn_fop = &apmfiltops;
1377 kn->kn_hook = (caddr_t)sc;
1380 ap->a_result = EOPNOTSUPP;
1385 klist = &sc->sc_rsel.si_note;
1386 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1393 apmfilter_detach(struct knote *kn)
1395 struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
1396 struct klist *klist;
1399 klist = &sc->sc_rsel.si_note;
1400 SLIST_REMOVE(klist, kn, knote, kn_selnext);
1405 apmfilter(struct knote *kn, long hint)
1407 struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
1410 if (sc->event_count)
1417 * Because apm is a static device that always exists under any attached
1418 * isa device, and not scanned by the isa device, we need an identify
1419 * function to install the device so we can probe for it.
1421 static device_method_t apm_methods[] = {
1422 /* Device interface */
1423 DEVMETHOD(device_identify, bus_generic_identify),
1424 DEVMETHOD(device_probe, apm_probe),
1425 DEVMETHOD(device_attach, apm_attach),
1430 static driver_t apm_driver = {
1433 1, /* no softc (XXX) */
1436 static devclass_t apm_devclass;
1438 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);