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/event.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>
43 #include <sys/thread2.h>
45 #include <machine/pc/bios.h>
46 #include <machine/vm86.h>
48 #include <machine_base/apm/apm.h>
50 /* Used by the apm_saver screen saver module */
51 int apm_display (int newstate);
52 struct apm_softc apm_softc;
54 static void apm_resume (void);
55 static int apm_bioscall(void);
56 static int apm_check_function_supported (u_int version, u_int func);
58 static u_long apm_version;
62 #define SCFLAG_ONORMAL 0x0000001
63 #define SCFLAG_OCTL 0x0000002
64 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
66 #define APMDEV(dev) (minor(dev)&0x0f)
67 #define APMDEV_NORMAL 0
70 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
72 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
74 /* Map version number to integer (keeps ordering of version numbers) */
75 #define INTVERSION(major, minor) ((major)*100 + (minor))
77 static struct callout apm_timeout_ch;
79 static timeout_t apm_timeout;
80 static d_open_t apmopen;
81 static d_close_t apmclose;
82 static d_write_t apmwrite;
83 static d_ioctl_t apmioctl;
84 static d_kqfilter_t apmkqfilter;
86 static void apmfilter_detach(struct knote *);
87 static int apmfilter_read(struct knote *, long);
88 static int apmfilter_write(struct knote *, long);
91 static struct dev_ops apm_ops = {
92 { "apm", CDEV_MAJOR, D_KQFILTER },
97 .d_kqfilter = apmkqfilter
100 static int apm_suspend_delay = 1;
101 static int apm_standby_delay = 1;
102 static int apm_debug = 0;
104 #define APM_DPRINT(args...) do { \
110 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
111 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
112 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
115 * return 0 if the function successfull,
116 * return 1 if the function unsuccessfull,
117 * return -1 if the function unsupported.
122 struct apm_softc *sc = &apm_softc;
124 u_int apm_func = sc->bios.r.eax & 0xff;
126 if (!apm_check_function_supported(sc->intversion, apm_func)) {
127 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
128 apm_func, sc->majorversion, sc->minorversion);
133 if (sc->connectmode == APM_PROT32CONNECT) {
134 set_bios_selectors(&sc->bios.seg,
135 BIOSCODE_FLAG | BIOSDATA_FLAG);
136 error = bios32(&sc->bios.r,
137 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
139 error = bios16(&sc->bios, NULL);
145 /* check whether APM function is supported (1) or not (0). */
147 apm_check_function_supported(u_int version, u_int func)
149 /* except driver version */
150 if (func == APM_DRVVERSION) {
155 case INTVERSION(1, 0):
156 if (func > APM_GETPMEVENT) {
157 return (0); /* not supported */
160 case INTVERSION(1, 1):
161 if (func > APM_ENGAGEDISENGAGEPM &&
162 func < APM_OEMFUNC) {
163 return (0); /* not supported */
166 case INTVERSION(1, 2):
170 return (1); /* supported */
173 /* enable/disable power management */
175 apm_enable_disable_pm(int enable)
177 struct apm_softc *sc = &apm_softc;
179 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
181 if (sc->intversion >= INTVERSION(1, 1))
182 sc->bios.r.ebx = PMDV_ALLDEV;
184 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
185 sc->bios.r.ecx = enable;
187 return (apm_bioscall());
190 /* register driver version (APM 1.1 or later) */
192 apm_driver_version(int version)
194 struct apm_softc *sc = &apm_softc;
196 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
197 sc->bios.r.ebx = 0x0;
198 sc->bios.r.ecx = version;
201 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
204 /* Some old BIOSes don't return the connection version in %ax. */
205 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
211 /* engage/disengage power management (APM 1.1 or later) */
213 apm_engage_disengage_pm(int engage)
215 struct apm_softc *sc = &apm_softc;
217 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
218 sc->bios.r.ebx = PMDV_ALLDEV;
219 sc->bios.r.ecx = engage;
221 return (apm_bioscall());
228 struct apm_softc *sc = &apm_softc;
230 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
236 return (PMEV_NOEVENT);
237 return (sc->bios.r.ebx & 0xffff);
240 /* suspend entire system */
242 apm_suspend_system(int state)
244 struct apm_softc *sc = &apm_softc;
246 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
247 sc->bios.r.ebx = PMDV_ALLDEV;
248 sc->bios.r.ecx = state;
251 if (apm_bioscall()) {
252 kprintf("Entire system suspend failure: errcode = %d\n",
253 0xff & (sc->bios.r.eax >> 8));
259 /* Display control */
261 * Experimental implementation: My laptop machine can't handle this function
262 * If your laptop can control the display via APM, please inform me.
263 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
266 apm_display(int newstate)
268 struct apm_softc *sc = &apm_softc;
270 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
271 sc->bios.r.ebx = PMDV_DISP0;
272 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
274 if (apm_bioscall() == 0) {
278 /* If failed, then try to blank all display devices instead. */
279 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
280 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
281 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
283 if (apm_bioscall() == 0) {
286 kprintf("Display off failure: errcode = %d\n",
287 0xff & (sc->bios.r.eax >> 8));
292 * Turn off the entire system.
295 apm_power_off(void *junk, int howto)
297 struct apm_softc *sc = &apm_softc;
299 /* Not halting powering off, or not active */
300 if (!(howto & RB_POWEROFF) || !apm_softc.active)
302 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
303 sc->bios.r.ebx = PMDV_ALLDEV;
304 sc->bios.r.ecx = PMST_OFF;
309 /* APM Battery low handler */
311 apm_battery_low(void)
313 kprintf("\007\007 * * * BATTERY IS LOW * * * \007\007");
316 /* APM hook manager */
317 static struct apmhook *
318 apm_add_hook(struct apmhook **list, struct apmhook *ah)
320 struct apmhook *p, *prev;
322 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
326 panic("illegal apm_hook!");
328 for (p = *list; p != NULL; prev = p, p = p->ah_next)
329 if (p->ah_order > ah->ah_order)
336 ah->ah_next = prev->ah_next;
344 apm_del_hook(struct apmhook **list, struct apmhook *ah)
346 struct apmhook *p, *prev;
350 for (p = *list; p != NULL; prev = p, p = p->ah_next)
353 panic("Tried to delete unregistered apm_hook.");
357 prev->ah_next = p->ah_next;
365 /* APM driver calls some functions automatically */
367 apm_execute_hook(struct apmhook *list)
371 for (p = list; p != NULL; p = p->ah_next) {
372 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
373 if ((*(p->ah_fun))(p->ah_arg))
374 kprintf("Warning: APM hook \"%s\" failed", p->ah_name);
379 /* establish an apm hook */
381 apm_hook_establish(int apmh, struct apmhook *ah)
383 if (apmh < 0 || apmh >= NAPM_HOOK)
386 return apm_add_hook(&hook[apmh], ah);
389 /* disestablish an apm hook */
391 apm_hook_disestablish(int apmh, struct apmhook *ah)
393 if (apmh < 0 || apmh >= NAPM_HOOK)
396 apm_del_hook(&hook[apmh], ah);
400 static struct timeval suspend_time;
401 static struct timeval diff_time;
404 apm_default_resume(void *arg)
406 u_int second, minute, hour;
407 struct timeval resume_time, tmp_time;
409 /* modified for adjkerntz */
411 timer_restore(); /* restore the all timers */
412 inittodr(0); /* adjust time to RTC */
413 microtime(&resume_time);
414 getmicrotime(&tmp_time);
415 timevaladd(&tmp_time, &diff_time);
418 /* XXX THIS DOESN'T WORK!!! */
422 #ifdef APM_FIXUP_CALLTODO
423 /* Calculate the delta time suspended */
424 timevalsub(&resume_time, &suspend_time);
425 /* Fixup the calltodo list with the delta time. */
426 adjust_timeout_calltodo(&resume_time);
427 #endif /* APM_FIXUP_CALLTODOK */
429 #ifndef APM_FIXUP_CALLTODO
430 second = resume_time.tv_sec - suspend_time.tv_sec;
431 #else /* APM_FIXUP_CALLTODO */
433 * We've already calculated resume_time to be the delta between
434 * the suspend and the resume.
436 second = resume_time.tv_sec;
437 #endif /* APM_FIXUP_CALLTODO */
438 hour = second / 3600;
440 minute = second / 60;
442 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
443 hour, minute, second);
448 apm_default_suspend(void *arg)
451 microtime(&diff_time);
453 microtime(&suspend_time);
454 timevalsub(&diff_time, &suspend_time);
459 static int apm_record_event (struct apm_softc *, u_int);
460 static void apm_processevent(void);
462 static u_int apm_op_inprog = 0;
467 struct apm_softc *sc = &apm_softc;
474 sc->suspends = sc->suspend_countdown = 0;
476 if (sc->initialized) {
477 error = DEVICE_SUSPEND(root_bus);
479 DEVICE_RESUME(root_bus);
481 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
482 if (apm_suspend_system(PMST_SUSPEND) == 0) {
485 /* Failure, 'resume' the system again */
486 apm_execute_hook(hook[APM_HOOK_RESUME]);
487 DEVICE_RESUME(root_bus);
496 struct apm_softc *sc = &apm_softc;
502 sc->standbys = sc->standby_countdown = 0;
504 if (sc->initialized) {
506 * As far as standby, we don't need to execute
507 * all of suspend hooks.
509 apm_default_suspend(&apm_softc);
510 if (apm_suspend_system(PMST_STANDBY) == 0)
516 apm_lastreq_notify(void)
518 struct apm_softc *sc = &apm_softc;
520 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
521 sc->bios.r.ebx = PMDV_ALLDEV;
522 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
528 apm_lastreq_rejected(void)
530 struct apm_softc *sc = &apm_softc;
532 if (apm_op_inprog == 0) {
533 return 1; /* no operation in progress */
536 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
537 sc->bios.r.ebx = PMDV_ALLDEV;
538 sc->bios.r.ecx = PMST_LASTREQREJECT;
541 if (apm_bioscall()) {
542 APM_DPRINT("apm_lastreq_rejected: failed\n");
550 * Public interface to the suspend/resume:
552 * Execute suspend and resume hook before and after sleep, respectively.
557 apm_suspend(int state)
559 struct apm_softc *sc = &apm_softc;
561 if (!sc->initialized)
569 sc->suspend_countdown = apm_suspend_delay;
575 sc->standby_countdown = apm_standby_delay;
578 kprintf("apm_suspend: Unknown Suspend state 0x%x\n", state);
583 apm_lastreq_notify();
589 struct apm_softc *sc = &apm_softc;
594 if (sc->initialized) {
595 apm_execute_hook(hook[APM_HOOK_RESUME]);
596 DEVICE_RESUME(root_bus);
601 /* get power status per battery */
603 apm_get_pwstatus(apm_pwstatus_t app)
605 struct apm_softc *sc = &apm_softc;
607 if (app->ap_device != PMDV_ALLDEV &&
608 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
611 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
612 sc->bios.r.ebx = app->ap_device;
614 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
619 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
620 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
621 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
622 app->ap_batt_life = sc->bios.r.ecx & 0xff;
623 sc->bios.r.edx &= 0xffff;
624 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
625 app->ap_batt_time = -1;
626 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
627 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
628 else /* Time is in seconds */
629 app->ap_batt_time = sc->bios.r.edx;
635 /* get APM information */
637 apm_get_info(apm_info_t aip)
639 struct apm_softc *sc = &apm_softc;
640 struct apm_pwstatus aps;
642 bzero(&aps, sizeof(aps));
643 aps.ap_device = PMDV_ALLDEV;
644 if (apm_get_pwstatus(&aps))
647 aip->ai_infoversion = 1;
648 aip->ai_acline = aps.ap_acline;
649 aip->ai_batt_stat = aps.ap_batt_stat;
650 aip->ai_batt_life = aps.ap_batt_life;
651 aip->ai_batt_time = aps.ap_batt_time;
652 aip->ai_major = (u_int)sc->majorversion;
653 aip->ai_minor = (u_int)sc->minorversion;
654 aip->ai_status = (u_int)sc->active;
656 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
660 if (apm_bioscall()) {
661 aip->ai_batteries = -1; /* Unknown */
662 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
664 aip->ai_batteries = sc->bios.r.ebx & 0xff;
665 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
668 bzero(aip->ai_spare, sizeof aip->ai_spare);
674 /* inform APM BIOS that CPU is idle */
678 struct apm_softc *sc = &apm_softc;
682 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
683 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
687 * Some APM implementation halts CPU in BIOS, whenever
688 * "CPU-idle" function are invoked, but swtch() of
689 * FreeBSD halts CPU, therefore, CPU is halted twice
690 * in the sched loop. It makes the interrupt latency
691 * terribly long and be able to cause a serious problem
692 * in interrupt processing. We prevent it by removing
693 * "hlt" operation from swtch() and managed it under
696 if (!sc->active || sc->always_halt_cpu)
697 __asm("hlt"); /* wait for interrupt */
700 /* inform APM BIOS that CPU is busy */
704 struct apm_softc *sc = &apm_softc;
707 * The APM specification says this is only necessary if your BIOS
708 * slows down the processor in the idle task, otherwise it's not
711 if (sc->slow_idle_cpu && sc->active) {
713 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
714 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
721 * APM timeout routine:
723 * This routine is automatically called by timer once per second.
727 apm_timeout(void *dummy)
729 struct apm_softc *sc = &apm_softc;
732 apm_lastreq_notify();
734 if (sc->standbys && sc->standby_countdown-- <= 0)
737 if (sc->suspends && sc->suspend_countdown-- <= 0)
743 if (sc->active == 1) {
744 /* Run slightly more oftan than 1 Hz */
745 callout_reset(&apm_timeout_ch, hz - 1, apm_timeout, NULL);
749 /* enable APM BIOS */
751 apm_event_enable(void)
753 struct apm_softc *sc = &apm_softc;
755 APM_DPRINT("called apm_event_enable()\n");
756 if (sc->initialized) {
758 callout_init(&apm_timeout_ch);
763 /* disable APM BIOS */
765 apm_event_disable(void)
767 struct apm_softc *sc = &apm_softc;
769 APM_DPRINT("called apm_event_disable()\n");
770 if (sc->initialized) {
771 callout_stop(&apm_timeout_ch);
776 /* halt CPU in scheduling loop */
780 struct apm_softc *sc = &apm_softc;
783 sc->always_halt_cpu = 1;
786 /* don't halt CPU in scheduling loop */
788 apm_not_halt_cpu(void)
790 struct apm_softc *sc = &apm_softc;
793 sc->always_halt_cpu = 0;
796 /* device driver definitions */
802 apm_probe(device_t dev)
804 #define APM_KERNBASE KERNBASE
805 struct vm86frame vmf;
806 struct apm_softc *sc = &apm_softc;
809 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
813 device_set_desc(dev, "APM BIOS");
815 if ( device_get_unit(dev) > 0 ) {
816 kprintf("apm: Only one APM driver supported.\n");
820 if (resource_int_value("apm", 0, "flags", &flags) != 0)
823 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
824 bzero(&apm_softc, sizeof(apm_softc));
825 vmf.vmf_ah = APM_BIOS;
826 vmf.vmf_al = APM_INSTCHECK;
828 if (vm86_intcall(APM_INT, &vmf))
829 return ENXIO; /* APM not found */
830 if (vmf.vmf_bx != 0x504d) {
831 kprintf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
834 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
835 kprintf("apm: protected mode connections are not supported\n");
839 apm_version = vmf.vmf_ax;
840 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
841 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
842 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
844 vmf.vmf_ah = APM_BIOS;
845 vmf.vmf_al = APM_DISCONNECT;
847 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
849 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
850 vmf.vmf_ah = APM_BIOS;
851 vmf.vmf_al = APM_PROT32CONNECT;
853 if (vm86_intcall(APM_INT, &vmf)) {
854 kprintf("apm: 32-bit connection error.\n");
857 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
858 sc->bios.seg.code32.limit = 0xffff;
859 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
860 sc->bios.seg.code16.limit = 0xffff;
861 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
862 sc->bios.seg.data.limit = 0xffff;
863 sc->bios.entry = vmf.vmf_ebx;
864 sc->connectmode = APM_PROT32CONNECT;
866 /* use 16-bit connection */
867 vmf.vmf_ah = APM_BIOS;
868 vmf.vmf_al = APM_PROT16CONNECT;
870 if (vm86_intcall(APM_INT, &vmf)) {
871 kprintf("apm: 16-bit connection error.\n");
874 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
875 sc->bios.seg.code16.limit = 0xffff;
876 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
877 sc->bios.seg.data.limit = 0xffff;
878 sc->bios.entry = vmf.vmf_bx;
879 sc->connectmode = APM_PROT16CONNECT;
886 * return 0 if the user will notice and handle the event,
887 * return 1 if the kernel driver should do so.
890 apm_record_event(struct apm_softc *sc, u_int event_type)
892 struct apm_event_info *evp;
894 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
895 return 1; /* no user waiting */
896 if (sc->event_count == APM_NEVENTS)
897 return 1; /* overflow */
898 if (sc->event_filter[event_type] == 0)
899 return 1; /* not registered */
900 evp = &sc->event_list[sc->event_ptr];
903 sc->event_ptr %= APM_NEVENTS;
904 evp->type = event_type;
905 evp->index = ++apm_evindex;
906 KNOTE(&sc->sc_rkq.ki_note, 0);
907 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
910 /* Process APM event */
912 apm_processevent(void)
915 struct apm_softc *sc = &apm_softc;
917 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
918 APM_DPRINT("Received APM Event: " #symbol "\n");
921 apm_event = apm_getevent();
923 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
924 if (apm_op_inprog == 0) {
926 if (apm_record_event(sc, apm_event)) {
927 apm_suspend(PMST_STANDBY);
931 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
932 if (apm_op_inprog == 0) {
934 if (apm_record_event(sc, apm_event)) {
935 apm_suspend(PMST_STANDBY);
939 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
940 apm_lastreq_notify();
941 if (apm_op_inprog == 0) {
943 if (apm_record_event(sc, apm_event)) {
947 return; /* XXX skip the rest */
948 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
949 apm_lastreq_notify();
950 if (apm_op_inprog == 0) {
952 if (apm_record_event(sc, apm_event)) {
956 return; /* XXX skip the rest */
957 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
960 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
961 apm_record_event(sc, apm_event);
964 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
965 apm_record_event(sc, apm_event);
968 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
969 apm_record_event(sc, apm_event);
972 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
973 if (apm_record_event(sc, apm_event)) {
975 apm_suspend(PMST_SUSPEND);
978 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
979 apm_record_event(sc, apm_event);
981 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
982 apm_record_event(sc, apm_event);
983 inittodr(0); /* adjust time to RTC */
985 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
986 apm_record_event(sc, apm_event);
991 kprintf("Unknown Original APM Event 0x%x\n", apm_event);
994 } while (apm_event != PMEV_NOEVENT);
1000 * Initialize APM driver
1004 apm_attach(device_t dev)
1006 struct apm_softc *sc = &apm_softc;
1010 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1013 sc->initialized = 0;
1015 /* Must be externally enabled */
1018 /* Always call HLT in idle loop */
1019 sc->always_halt_cpu = 1;
1021 kgetenv_int("debug.apm_debug", &apm_debug);
1023 /* print bootstrap messages */
1024 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1025 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1026 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1027 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1028 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1029 is_enabled(!sc->disabled));
1030 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1031 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1034 * In one test, apm bios version was 1.02; an attempt to register
1035 * a 1.04 driver resulted in a 1.00 connection! Registering a
1036 * 1.02 driver resulted in a 1.02 connection.
1038 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1039 for (; drv_version > 0x100; drv_version--)
1040 if (apm_driver_version(drv_version) == 0)
1042 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1043 ((drv_version & 0x000f) >> 0);
1044 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1045 ((apm_version & 0x0f00) >> 8);
1047 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1049 if (sc->intversion >= INTVERSION(1, 1))
1050 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1051 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1052 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1053 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1054 sc->majorversion, sc->minorversion);
1057 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1058 /* enable power management */
1060 if (apm_enable_disable_pm(1)) {
1061 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1062 (sc->bios.r.eax >> 8) & 0xff);
1066 /* engage power managment (APM 1.1 or later) */
1067 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1068 if (apm_engage_disengage_pm(1)) {
1069 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1070 (sc->bios.r.eax >> 8) & 0xff);
1071 APM_DPRINT(" (Docked or using external power?).\n");
1075 /* default suspend hook */
1076 sc->sc_suspend.ah_fun = apm_default_suspend;
1077 sc->sc_suspend.ah_arg = sc;
1078 sc->sc_suspend.ah_name = "default suspend";
1079 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1081 /* default resume hook */
1082 sc->sc_resume.ah_fun = apm_default_resume;
1083 sc->sc_resume.ah_arg = sc;
1084 sc->sc_resume.ah_name = "default resume";
1085 sc->sc_resume.ah_order = APM_MIN_ORDER;
1087 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1088 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1090 /* Power the system off using APM */
1091 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1094 sc->initialized = 1;
1096 make_dev(&apm_ops, 0, UID_ROOT, GID_OPERATOR, 0660, "apm");
1097 make_dev(&apm_ops, 8, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
1102 apmopen(struct dev_open_args *ap)
1104 cdev_t dev = ap->a_head.a_dev;
1105 struct apm_softc *sc = &apm_softc;
1106 int ctl = APMDEV(dev);
1108 if (!sc->initialized)
1113 if (!(ap->a_oflags & FWRITE))
1115 if (sc->sc_flags & SCFLAG_OCTL)
1117 sc->sc_flags |= SCFLAG_OCTL;
1118 bzero(sc->event_filter, sizeof sc->event_filter);
1121 sc->sc_flags |= SCFLAG_ONORMAL;
1131 apmclose(struct dev_close_args *ap)
1133 cdev_t dev = ap->a_head.a_dev;
1134 struct apm_softc *sc = &apm_softc;
1135 int ctl = APMDEV(dev);
1139 apm_lastreq_rejected();
1140 sc->sc_flags &= ~SCFLAG_OCTL;
1141 bzero(sc->event_filter, sizeof sc->event_filter);
1144 sc->sc_flags &= ~SCFLAG_ONORMAL;
1147 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1148 sc->event_count = 0;
1155 apmioctl(struct dev_ioctl_args *ap)
1157 cdev_t dev = ap->a_head.a_dev;
1158 struct apm_softc *sc = &apm_softc;
1159 struct apm_bios_arg *args;
1164 if (!sc->initialized)
1166 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", ap->a_cmd);
1168 switch (ap->a_cmd) {
1170 if (!(ap->a_fflag & FWRITE))
1173 apm_suspend(PMST_SUSPEND);
1179 if (!(ap->a_fflag & FWRITE))
1182 apm_suspend(PMST_STANDBY);
1187 case APMIO_GETINFO_OLD:
1189 struct apm_info info;
1190 apm_info_old_t aiop;
1192 if (apm_get_info(&info))
1194 aiop = (apm_info_old_t)ap->a_data;
1195 aiop->ai_major = info.ai_major;
1196 aiop->ai_minor = info.ai_minor;
1197 aiop->ai_acline = info.ai_acline;
1198 aiop->ai_batt_stat = info.ai_batt_stat;
1199 aiop->ai_batt_life = info.ai_batt_life;
1200 aiop->ai_status = info.ai_status;
1204 if (apm_get_info((apm_info_t)ap->a_data))
1207 case APMIO_GETPWSTATUS:
1208 if (apm_get_pwstatus((apm_pwstatus_t)ap->a_data))
1212 if (!(ap->a_fflag & FWRITE))
1217 if (!(ap->a_fflag & FWRITE))
1219 apm_event_disable();
1222 if (!(ap->a_fflag & FWRITE))
1226 case APMIO_NOTHALTCPU:
1227 if (!(ap->a_fflag & FWRITE))
1232 if (!(ap->a_fflag & FWRITE))
1234 newstate = *(int *)ap->a_data;
1235 if (apm_display(newstate))
1239 if (!(ap->a_fflag & FWRITE))
1241 /* XXX compatibility with the old interface */
1242 args = (struct apm_bios_arg *)ap->a_data;
1243 sc->bios.r.eax = args->eax;
1244 sc->bios.r.ebx = args->ebx;
1245 sc->bios.r.ecx = args->ecx;
1246 sc->bios.r.edx = args->edx;
1247 sc->bios.r.esi = args->esi;
1248 sc->bios.r.edi = args->edi;
1249 if ((ret = apm_bioscall())) {
1251 * Return code 1 means bios call was unsuccessful.
1252 * Error code is stored in %ah.
1253 * Return code -1 means bios call was unsupported
1254 * in the APM BIOS version.
1261 * Return code 0 means bios call was successful.
1262 * We need only %al and can discard %ah.
1264 sc->bios.r.eax &= 0xff;
1266 args->eax = sc->bios.r.eax;
1267 args->ebx = sc->bios.r.ebx;
1268 args->ecx = sc->bios.r.ecx;
1269 args->edx = sc->bios.r.edx;
1270 args->esi = sc->bios.r.esi;
1271 args->edi = sc->bios.r.edi;
1278 /* for /dev/apmctl */
1279 if (APMDEV(dev) == APMDEV_CTL) {
1280 struct apm_event_info *evp;
1284 switch (ap->a_cmd) {
1285 case APMIO_NEXTEVENT:
1286 if (!sc->event_count) {
1289 evp = (struct apm_event_info *)ap->a_data;
1290 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1292 *evp = sc->event_list[i];
1296 case APMIO_REJECTLASTREQ:
1297 if (apm_lastreq_rejected()) {
1311 apmwrite(struct dev_write_args *ap)
1313 cdev_t dev = ap->a_head.a_dev;
1314 struct uio *uio = ap->a_uio;
1315 struct apm_softc *sc = &apm_softc;
1320 if (APMDEV(dev) != APMDEV_CTL)
1322 if (uio->uio_resid != sizeof(u_int))
1325 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1328 if (event_type < 0 || event_type >= APM_NPMEV)
1331 if (sc->event_filter[event_type] == 0) {
1336 sc->event_filter[event_type] = enabled;
1337 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1339 return uio->uio_resid;
1342 static struct filterops apmfiltops_read =
1343 { 1, NULL, apmfilter_detach, apmfilter_read };
1344 static struct filterops apmfiltops_write =
1345 { 1, NULL, apmfilter_detach, apmfilter_write };
1348 apmkqfilter(struct dev_kqfilter_args *ap)
1350 struct apm_softc *sc = &apm_softc;
1351 struct knote *kn = ap->a_kn;
1352 struct klist *klist;
1356 switch (kn->kn_filter) {
1358 kn->kn_fop = &apmfiltops_read;
1359 kn->kn_hook = (caddr_t)sc;
1362 kn->kn_fop = &apmfiltops_write;
1363 kn->kn_hook = (caddr_t)sc;
1366 ap->a_result = EOPNOTSUPP;
1370 klist = &sc->sc_rkq.ki_note;
1371 knote_insert(klist, kn);
1377 apmfilter_detach(struct knote *kn)
1379 struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
1380 struct klist *klist;
1382 klist = &sc->sc_rkq.ki_note;
1383 knote_remove(klist, kn);
1387 apmfilter_read(struct knote *kn, long hint)
1389 struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
1392 if (sc->event_count)
1399 apmfilter_write(struct knote *kn, long hint)
1401 /* write()'s are always OK */
1406 * Because apm is a static device that always exists under any attached
1407 * isa device, and not scanned by the isa device, we need an identify
1408 * function to install the device so we can probe for it.
1410 static device_method_t apm_methods[] = {
1411 /* Device interface */
1412 DEVMETHOD(device_identify, bus_generic_identify),
1413 DEVMETHOD(device_probe, apm_probe),
1414 DEVMETHOD(device_attach, apm_attach),
1419 static driver_t apm_driver = {
1422 1, /* no softc (XXX) */
1425 static devclass_t apm_devclass;
1427 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);