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 $
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/eventhandler.h>
25 #include <sys/device.h>
26 #include <sys/kernel.h>
28 #include <sys/reboot.h>
30 #include <sys/event.h>
31 #include <sys/fcntl.h>
33 #include <sys/signalvar.h>
34 #include <sys/sysctl.h>
35 #include <machine/apm_bios.h>
36 #include <machine/segments.h>
37 #include <machine/clock.h>
39 #include <vm/vm_param.h>
41 #include <sys/syslog.h>
42 #include <sys/thread2.h>
44 #include <machine/pc/bios.h>
45 #include <machine/vm86.h>
47 #include <machine_base/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_kqfilter_t apmkqfilter;
85 static void apmfilter_detach(struct knote *);
86 static int apmfilter_read(struct knote *, long);
87 static int apmfilter_write(struct knote *, long);
89 static struct dev_ops apm_ops = {
95 .d_kqfilter = apmkqfilter
98 static int apm_suspend_delay = 1;
99 static int apm_standby_delay = 1;
100 static int apm_debug = 0;
102 #define APM_DPRINT(args...) do { \
108 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
109 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
110 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
113 * return 0 if the function successfull,
114 * return 1 if the function unsuccessfull,
115 * return -1 if the function unsupported.
120 struct apm_softc *sc = &apm_softc;
122 u_int apm_func = sc->bios.r.eax & 0xff;
124 if (!apm_check_function_supported(sc->intversion, apm_func)) {
125 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
126 apm_func, sc->majorversion, sc->minorversion);
131 if (sc->connectmode == APM_PROT32CONNECT) {
132 set_bios_selectors(&sc->bios.seg,
133 BIOSCODE_FLAG | BIOSDATA_FLAG);
134 error = bios32(&sc->bios.r,
135 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
137 error = bios16(&sc->bios, NULL);
143 /* check whether APM function is supported (1) or not (0). */
145 apm_check_function_supported(u_int version, u_int func)
147 /* except driver version */
148 if (func == APM_DRVVERSION) {
153 case INTVERSION(1, 0):
154 if (func > APM_GETPMEVENT) {
155 return (0); /* not supported */
158 case INTVERSION(1, 1):
159 if (func > APM_ENGAGEDISENGAGEPM &&
160 func < APM_OEMFUNC) {
161 return (0); /* not supported */
164 case INTVERSION(1, 2):
168 return (1); /* supported */
171 /* enable/disable power management */
173 apm_enable_disable_pm(int enable)
175 struct apm_softc *sc = &apm_softc;
177 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
179 if (sc->intversion >= INTVERSION(1, 1))
180 sc->bios.r.ebx = PMDV_ALLDEV;
182 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
183 sc->bios.r.ecx = enable;
185 return (apm_bioscall());
188 /* register driver version (APM 1.1 or later) */
190 apm_driver_version(int version)
192 struct apm_softc *sc = &apm_softc;
194 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
195 sc->bios.r.ebx = 0x0;
196 sc->bios.r.ecx = version;
199 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
202 /* Some old BIOSes don't return the connection version in %ax. */
203 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
209 /* engage/disengage power management (APM 1.1 or later) */
211 apm_engage_disengage_pm(int engage)
213 struct apm_softc *sc = &apm_softc;
215 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
216 sc->bios.r.ebx = PMDV_ALLDEV;
217 sc->bios.r.ecx = engage;
219 return (apm_bioscall());
226 struct apm_softc *sc = &apm_softc;
228 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
234 return (PMEV_NOEVENT);
235 return (sc->bios.r.ebx & 0xffff);
238 /* suspend entire system */
240 apm_suspend_system(int state)
242 struct apm_softc *sc = &apm_softc;
244 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
245 sc->bios.r.ebx = PMDV_ALLDEV;
246 sc->bios.r.ecx = state;
249 if (apm_bioscall()) {
250 kprintf("Entire system suspend failure: errcode = %d\n",
251 0xff & (sc->bios.r.eax >> 8));
257 /* Display control */
259 * Experimental implementation: My laptop machine can't handle this function
260 * If your laptop can control the display via APM, please inform me.
261 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
264 apm_display(int newstate)
266 struct apm_softc *sc = &apm_softc;
268 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
269 sc->bios.r.ebx = PMDV_DISP0;
270 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
272 if (apm_bioscall() == 0) {
276 /* If failed, then try to blank all display devices instead. */
277 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
278 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
279 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
281 if (apm_bioscall() == 0) {
284 kprintf("Display off failure: errcode = %d\n",
285 0xff & (sc->bios.r.eax >> 8));
290 * Turn off the entire system.
293 apm_power_off(void *junk, int howto)
295 struct apm_softc *sc = &apm_softc;
297 /* Not halting powering off, or not active */
298 if (!(howto & RB_POWEROFF) || !apm_softc.active)
300 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
301 sc->bios.r.ebx = PMDV_ALLDEV;
302 sc->bios.r.ecx = PMST_OFF;
307 /* APM Battery low handler */
309 apm_battery_low(void)
311 kprintf("\007\007 * * * BATTERY IS LOW * * * \007\007");
314 /* APM hook manager */
315 static struct apmhook *
316 apm_add_hook(struct apmhook **list, struct apmhook *ah)
318 struct apmhook *p, *prev;
320 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
324 panic("illegal apm_hook!");
326 for (p = *list; p != NULL; prev = p, p = p->ah_next)
327 if (p->ah_order > ah->ah_order)
334 ah->ah_next = prev->ah_next;
342 apm_del_hook(struct apmhook **list, struct apmhook *ah)
344 struct apmhook *p, *prev;
348 for (p = *list; p != NULL; prev = p, p = p->ah_next)
351 panic("Tried to delete unregistered apm_hook.");
355 prev->ah_next = p->ah_next;
363 /* APM driver calls some functions automatically */
365 apm_execute_hook(struct apmhook *list)
369 for (p = list; p != NULL; p = p->ah_next) {
370 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
371 if ((*(p->ah_fun))(p->ah_arg))
372 kprintf("Warning: APM hook \"%s\" failed", p->ah_name);
377 /* establish an apm hook */
379 apm_hook_establish(int apmh, struct apmhook *ah)
381 if (apmh < 0 || apmh >= NAPM_HOOK)
384 return apm_add_hook(&hook[apmh], ah);
387 /* disestablish an apm hook */
389 apm_hook_disestablish(int apmh, struct apmhook *ah)
391 if (apmh < 0 || apmh >= NAPM_HOOK)
394 apm_del_hook(&hook[apmh], ah);
398 static struct timeval suspend_time;
399 static struct timeval diff_time;
402 apm_default_resume(void *arg)
404 u_int second, minute, hour;
405 struct timeval resume_time, tmp_time;
407 /* modified for adjkerntz */
409 timer_restore(); /* restore the all timers */
410 inittodr(0); /* adjust time to RTC */
411 microtime(&resume_time);
412 getmicrotime(&tmp_time);
413 timevaladd(&tmp_time, &diff_time);
416 /* XXX THIS DOESN'T WORK!!! */
420 #ifdef APM_FIXUP_CALLTODO
421 /* Calculate the delta time suspended */
422 timevalsub(&resume_time, &suspend_time);
423 /* Fixup the calltodo list with the delta time. */
424 adjust_timeout_calltodo(&resume_time);
425 #endif /* APM_FIXUP_CALLTODOK */
427 #ifndef APM_FIXUP_CALLTODO
428 second = resume_time.tv_sec - suspend_time.tv_sec;
429 #else /* APM_FIXUP_CALLTODO */
431 * We've already calculated resume_time to be the delta between
432 * the suspend and the resume.
434 second = resume_time.tv_sec;
435 #endif /* APM_FIXUP_CALLTODO */
436 hour = second / 3600;
438 minute = second / 60;
440 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
441 hour, minute, second);
446 apm_default_suspend(void *arg)
449 microtime(&diff_time);
451 microtime(&suspend_time);
452 timevalsub(&diff_time, &suspend_time);
457 static int apm_record_event (struct apm_softc *, u_int);
458 static void apm_processevent(void);
460 static u_int apm_op_inprog = 0;
465 struct apm_softc *sc = &apm_softc;
472 sc->suspends = sc->suspend_countdown = 0;
474 if (sc->initialized) {
475 error = DEVICE_SUSPEND(root_bus);
477 DEVICE_RESUME(root_bus);
479 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
480 if (apm_suspend_system(PMST_SUSPEND) == 0) {
483 /* Failure, 'resume' the system again */
484 apm_execute_hook(hook[APM_HOOK_RESUME]);
485 DEVICE_RESUME(root_bus);
494 struct apm_softc *sc = &apm_softc;
500 sc->standbys = sc->standby_countdown = 0;
502 if (sc->initialized) {
504 * As far as standby, we don't need to execute
505 * all of suspend hooks.
507 apm_default_suspend(&apm_softc);
508 if (apm_suspend_system(PMST_STANDBY) == 0)
514 apm_lastreq_notify(void)
516 struct apm_softc *sc = &apm_softc;
518 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
519 sc->bios.r.ebx = PMDV_ALLDEV;
520 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
526 apm_lastreq_rejected(void)
528 struct apm_softc *sc = &apm_softc;
530 if (apm_op_inprog == 0) {
531 return 1; /* no operation in progress */
534 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
535 sc->bios.r.ebx = PMDV_ALLDEV;
536 sc->bios.r.ecx = PMST_LASTREQREJECT;
539 if (apm_bioscall()) {
540 APM_DPRINT("apm_lastreq_rejected: failed\n");
548 * Public interface to the suspend/resume:
550 * Execute suspend and resume hook before and after sleep, respectively.
555 apm_suspend(int state)
557 struct apm_softc *sc = &apm_softc;
559 if (!sc->initialized)
567 sc->suspend_countdown = apm_suspend_delay;
573 sc->standby_countdown = apm_standby_delay;
576 kprintf("apm_suspend: Unknown Suspend state 0x%x\n", state);
581 apm_lastreq_notify();
587 struct apm_softc *sc = &apm_softc;
592 if (sc->initialized) {
593 apm_execute_hook(hook[APM_HOOK_RESUME]);
594 DEVICE_RESUME(root_bus);
599 /* get power status per battery */
601 apm_get_pwstatus(apm_pwstatus_t app)
603 struct apm_softc *sc = &apm_softc;
605 if (app->ap_device != PMDV_ALLDEV &&
606 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
609 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
610 sc->bios.r.ebx = app->ap_device;
612 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
617 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
618 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
619 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
620 app->ap_batt_life = sc->bios.r.ecx & 0xff;
621 sc->bios.r.edx &= 0xffff;
622 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
623 app->ap_batt_time = -1;
624 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
625 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
626 else /* Time is in seconds */
627 app->ap_batt_time = sc->bios.r.edx;
633 /* get APM information */
635 apm_get_info(apm_info_t aip)
637 struct apm_softc *sc = &apm_softc;
638 struct apm_pwstatus aps;
640 bzero(&aps, sizeof(aps));
641 aps.ap_device = PMDV_ALLDEV;
642 if (apm_get_pwstatus(&aps))
645 aip->ai_infoversion = 1;
646 aip->ai_acline = aps.ap_acline;
647 aip->ai_batt_stat = aps.ap_batt_stat;
648 aip->ai_batt_life = aps.ap_batt_life;
649 aip->ai_batt_time = aps.ap_batt_time;
650 aip->ai_major = (u_int)sc->majorversion;
651 aip->ai_minor = (u_int)sc->minorversion;
652 aip->ai_status = (u_int)sc->active;
654 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
658 if (apm_bioscall()) {
659 aip->ai_batteries = -1; /* Unknown */
660 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
662 aip->ai_batteries = sc->bios.r.ebx & 0xff;
663 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
666 bzero(aip->ai_spare, sizeof aip->ai_spare);
672 /* inform APM BIOS that CPU is idle */
676 struct apm_softc *sc = &apm_softc;
680 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
681 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
685 * Some APM implementation halts CPU in BIOS, whenever
686 * "CPU-idle" function are invoked, but swtch() of
687 * FreeBSD halts CPU, therefore, CPU is halted twice
688 * in the sched loop. It makes the interrupt latency
689 * terribly long and be able to cause a serious problem
690 * in interrupt processing. We prevent it by removing
691 * "hlt" operation from swtch() and managed it under
694 if (!sc->active || sc->always_halt_cpu)
695 __asm("hlt"); /* wait for interrupt */
698 /* inform APM BIOS that CPU is busy */
702 struct apm_softc *sc = &apm_softc;
705 * The APM specification says this is only necessary if your BIOS
706 * slows down the processor in the idle task, otherwise it's not
709 if (sc->slow_idle_cpu && sc->active) {
711 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
712 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
719 * APM timeout routine:
721 * This routine is automatically called by timer once per second.
725 apm_timeout(void *dummy)
727 struct apm_softc *sc = &apm_softc;
730 apm_lastreq_notify();
732 if (sc->standbys && sc->standby_countdown-- <= 0)
735 if (sc->suspends && sc->suspend_countdown-- <= 0)
741 if (sc->active == 1) {
742 /* Run slightly more oftan than 1 Hz */
743 callout_reset(&apm_timeout_ch, hz - 1, apm_timeout, NULL);
747 /* enable APM BIOS */
749 apm_event_enable(void)
751 struct apm_softc *sc = &apm_softc;
753 APM_DPRINT("called apm_event_enable()\n");
754 if (sc->initialized) {
756 callout_init(&apm_timeout_ch);
761 /* disable APM BIOS */
763 apm_event_disable(void)
765 struct apm_softc *sc = &apm_softc;
767 APM_DPRINT("called apm_event_disable()\n");
768 if (sc->initialized) {
769 callout_stop(&apm_timeout_ch);
774 /* halt CPU in scheduling loop */
778 struct apm_softc *sc = &apm_softc;
781 sc->always_halt_cpu = 1;
784 /* don't halt CPU in scheduling loop */
786 apm_not_halt_cpu(void)
788 struct apm_softc *sc = &apm_softc;
791 sc->always_halt_cpu = 0;
794 /* device driver definitions */
800 apm_probe(device_t dev)
802 #define APM_KERNBASE KERNBASE
803 struct vm86frame vmf;
804 struct apm_softc *sc = &apm_softc;
807 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
811 device_set_desc(dev, "APM BIOS");
813 if ( device_get_unit(dev) > 0 ) {
814 kprintf("apm: Only one APM driver supported.\n");
818 if (resource_int_value("apm", 0, "flags", &flags) != 0)
821 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
822 bzero(&apm_softc, sizeof(apm_softc));
823 vmf.vmf_ah = APM_BIOS;
824 vmf.vmf_al = APM_INSTCHECK;
826 if (vm86_intcall(APM_INT, &vmf))
827 return ENXIO; /* APM not found */
828 if (vmf.vmf_bx != 0x504d) {
829 kprintf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
832 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
833 kprintf("apm: protected mode connections are not supported\n");
837 apm_version = vmf.vmf_ax;
838 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
839 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
840 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
842 vmf.vmf_ah = APM_BIOS;
843 vmf.vmf_al = APM_DISCONNECT;
845 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
847 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
848 vmf.vmf_ah = APM_BIOS;
849 vmf.vmf_al = APM_PROT32CONNECT;
851 if (vm86_intcall(APM_INT, &vmf)) {
852 kprintf("apm: 32-bit connection error.\n");
855 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
856 sc->bios.seg.code32.limit = 0xffff;
857 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
858 sc->bios.seg.code16.limit = 0xffff;
859 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
860 sc->bios.seg.data.limit = 0xffff;
861 sc->bios.entry = vmf.vmf_ebx;
862 sc->connectmode = APM_PROT32CONNECT;
864 /* use 16-bit connection */
865 vmf.vmf_ah = APM_BIOS;
866 vmf.vmf_al = APM_PROT16CONNECT;
868 if (vm86_intcall(APM_INT, &vmf)) {
869 kprintf("apm: 16-bit connection error.\n");
872 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
873 sc->bios.seg.code16.limit = 0xffff;
874 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
875 sc->bios.seg.data.limit = 0xffff;
876 sc->bios.entry = vmf.vmf_bx;
877 sc->connectmode = APM_PROT16CONNECT;
884 * return 0 if the user will notice and handle the event,
885 * return 1 if the kernel driver should do so.
888 apm_record_event(struct apm_softc *sc, u_int event_type)
890 struct apm_event_info *evp;
892 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
893 return 1; /* no user waiting */
894 if (sc->event_count == APM_NEVENTS)
895 return 1; /* overflow */
896 if (sc->event_filter[event_type] == 0)
897 return 1; /* not registered */
898 evp = &sc->event_list[sc->event_ptr];
901 sc->event_ptr %= APM_NEVENTS;
902 evp->type = event_type;
903 evp->index = ++apm_evindex;
904 KNOTE(&sc->sc_rkq.ki_note, 0);
905 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
908 /* Process APM event */
910 apm_processevent(void)
913 struct apm_softc *sc = &apm_softc;
915 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
916 APM_DPRINT("Received APM Event: " #symbol "\n");
919 apm_event = apm_getevent();
921 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
922 if (apm_op_inprog == 0) {
924 if (apm_record_event(sc, apm_event)) {
925 apm_suspend(PMST_STANDBY);
929 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
930 if (apm_op_inprog == 0) {
932 if (apm_record_event(sc, apm_event)) {
933 apm_suspend(PMST_STANDBY);
937 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
938 apm_lastreq_notify();
939 if (apm_op_inprog == 0) {
941 if (apm_record_event(sc, apm_event)) {
945 return; /* XXX skip the rest */
946 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
947 apm_lastreq_notify();
948 if (apm_op_inprog == 0) {
950 if (apm_record_event(sc, apm_event)) {
954 return; /* XXX skip the rest */
955 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
958 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
959 apm_record_event(sc, apm_event);
962 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
963 apm_record_event(sc, apm_event);
966 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
967 apm_record_event(sc, apm_event);
970 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
971 if (apm_record_event(sc, apm_event)) {
973 apm_suspend(PMST_SUSPEND);
976 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
977 apm_record_event(sc, apm_event);
979 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
980 apm_record_event(sc, apm_event);
981 inittodr(0); /* adjust time to RTC */
983 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
984 apm_record_event(sc, apm_event);
989 kprintf("Unknown Original APM Event 0x%x\n", apm_event);
992 } while (apm_event != PMEV_NOEVENT);
998 * Initialize APM driver
1002 apm_attach(device_t dev)
1004 struct apm_softc *sc = &apm_softc;
1008 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1011 sc->initialized = 0;
1013 /* Must be externally enabled */
1016 /* Always call HLT in idle loop */
1017 sc->always_halt_cpu = 1;
1019 kgetenv_int("debug.apm_debug", &apm_debug);
1021 /* print bootstrap messages */
1022 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1023 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1024 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1025 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1026 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1027 is_enabled(!sc->disabled));
1028 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1029 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1032 * In one test, apm bios version was 1.02; an attempt to register
1033 * a 1.04 driver resulted in a 1.00 connection! Registering a
1034 * 1.02 driver resulted in a 1.02 connection.
1036 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1037 for (; drv_version > 0x100; drv_version--)
1038 if (apm_driver_version(drv_version) == 0)
1040 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1041 ((drv_version & 0x000f) >> 0);
1042 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1043 ((apm_version & 0x0f00) >> 8);
1045 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1047 if (sc->intversion >= INTVERSION(1, 1))
1048 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1049 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1050 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1051 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1052 sc->majorversion, sc->minorversion);
1055 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1056 /* enable power management */
1058 if (apm_enable_disable_pm(1)) {
1059 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1060 (sc->bios.r.eax >> 8) & 0xff);
1064 /* engage power managment (APM 1.1 or later) */
1065 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1066 if (apm_engage_disengage_pm(1)) {
1067 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1068 (sc->bios.r.eax >> 8) & 0xff);
1069 APM_DPRINT(" (Docked or using external power?).\n");
1073 /* default suspend hook */
1074 sc->sc_suspend.ah_fun = apm_default_suspend;
1075 sc->sc_suspend.ah_arg = sc;
1076 sc->sc_suspend.ah_name = "default suspend";
1077 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1079 /* default resume hook */
1080 sc->sc_resume.ah_fun = apm_default_resume;
1081 sc->sc_resume.ah_arg = sc;
1082 sc->sc_resume.ah_name = "default resume";
1083 sc->sc_resume.ah_order = APM_MIN_ORDER;
1085 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1086 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1088 /* Power the system off using APM */
1089 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1092 sc->initialized = 1;
1094 make_dev(&apm_ops, 0, UID_ROOT, GID_OPERATOR, 0660, "apm");
1095 make_dev(&apm_ops, 8, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
1100 apmopen(struct dev_open_args *ap)
1102 cdev_t dev = ap->a_head.a_dev;
1103 struct apm_softc *sc = &apm_softc;
1104 int ctl = APMDEV(dev);
1106 if (!sc->initialized)
1111 if (!(ap->a_oflags & FWRITE))
1113 if (sc->sc_flags & SCFLAG_OCTL)
1115 sc->sc_flags |= SCFLAG_OCTL;
1116 bzero(sc->event_filter, sizeof sc->event_filter);
1119 sc->sc_flags |= SCFLAG_ONORMAL;
1129 apmclose(struct dev_close_args *ap)
1131 cdev_t dev = ap->a_head.a_dev;
1132 struct apm_softc *sc = &apm_softc;
1133 int ctl = APMDEV(dev);
1137 apm_lastreq_rejected();
1138 sc->sc_flags &= ~SCFLAG_OCTL;
1139 bzero(sc->event_filter, sizeof sc->event_filter);
1142 sc->sc_flags &= ~SCFLAG_ONORMAL;
1145 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1146 sc->event_count = 0;
1153 apmioctl(struct dev_ioctl_args *ap)
1155 cdev_t dev = ap->a_head.a_dev;
1156 struct apm_softc *sc = &apm_softc;
1157 struct apm_bios_arg *args;
1162 if (!sc->initialized)
1164 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", ap->a_cmd);
1166 switch (ap->a_cmd) {
1168 if (!(ap->a_fflag & FWRITE))
1171 apm_suspend(PMST_SUSPEND);
1177 if (!(ap->a_fflag & FWRITE))
1180 apm_suspend(PMST_STANDBY);
1185 case APMIO_GETINFO_OLD:
1187 struct apm_info info;
1188 apm_info_old_t aiop;
1190 if (apm_get_info(&info))
1192 aiop = (apm_info_old_t)ap->a_data;
1193 aiop->ai_major = info.ai_major;
1194 aiop->ai_minor = info.ai_minor;
1195 aiop->ai_acline = info.ai_acline;
1196 aiop->ai_batt_stat = info.ai_batt_stat;
1197 aiop->ai_batt_life = info.ai_batt_life;
1198 aiop->ai_status = info.ai_status;
1202 if (apm_get_info((apm_info_t)ap->a_data))
1205 case APMIO_GETPWSTATUS:
1206 if (apm_get_pwstatus((apm_pwstatus_t)ap->a_data))
1210 if (!(ap->a_fflag & FWRITE))
1215 if (!(ap->a_fflag & FWRITE))
1217 apm_event_disable();
1220 if (!(ap->a_fflag & FWRITE))
1224 case APMIO_NOTHALTCPU:
1225 if (!(ap->a_fflag & FWRITE))
1230 if (!(ap->a_fflag & FWRITE))
1232 newstate = *(int *)ap->a_data;
1233 if (apm_display(newstate))
1237 if (!(ap->a_fflag & FWRITE))
1239 /* XXX compatibility with the old interface */
1240 args = (struct apm_bios_arg *)ap->a_data;
1241 sc->bios.r.eax = args->eax;
1242 sc->bios.r.ebx = args->ebx;
1243 sc->bios.r.ecx = args->ecx;
1244 sc->bios.r.edx = args->edx;
1245 sc->bios.r.esi = args->esi;
1246 sc->bios.r.edi = args->edi;
1247 if ((ret = apm_bioscall())) {
1249 * Return code 1 means bios call was unsuccessful.
1250 * Error code is stored in %ah.
1251 * Return code -1 means bios call was unsupported
1252 * in the APM BIOS version.
1259 * Return code 0 means bios call was successful.
1260 * We need only %al and can discard %ah.
1262 sc->bios.r.eax &= 0xff;
1264 args->eax = sc->bios.r.eax;
1265 args->ebx = sc->bios.r.ebx;
1266 args->ecx = sc->bios.r.ecx;
1267 args->edx = sc->bios.r.edx;
1268 args->esi = sc->bios.r.esi;
1269 args->edi = sc->bios.r.edi;
1276 /* for /dev/apmctl */
1277 if (APMDEV(dev) == APMDEV_CTL) {
1278 struct apm_event_info *evp;
1282 switch (ap->a_cmd) {
1283 case APMIO_NEXTEVENT:
1284 if (!sc->event_count) {
1287 evp = (struct apm_event_info *)ap->a_data;
1288 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1290 *evp = sc->event_list[i];
1294 case APMIO_REJECTLASTREQ:
1295 if (apm_lastreq_rejected()) {
1309 apmwrite(struct dev_write_args *ap)
1311 cdev_t dev = ap->a_head.a_dev;
1312 struct uio *uio = ap->a_uio;
1313 struct apm_softc *sc = &apm_softc;
1318 if (APMDEV(dev) != APMDEV_CTL)
1320 if (uio->uio_resid != sizeof(u_int))
1323 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1326 if (event_type < 0 || event_type >= APM_NPMEV)
1329 if (sc->event_filter[event_type] == 0) {
1334 sc->event_filter[event_type] = enabled;
1335 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1337 return uio->uio_resid;
1340 static struct filterops apmfiltops_read =
1341 { FILTEROP_ISFD, NULL, apmfilter_detach, apmfilter_read };
1342 static struct filterops apmfiltops_write =
1343 { FILTEROP_ISFD, NULL, apmfilter_detach, apmfilter_write };
1346 apmkqfilter(struct dev_kqfilter_args *ap)
1348 struct apm_softc *sc = &apm_softc;
1349 struct knote *kn = ap->a_kn;
1350 struct klist *klist;
1354 switch (kn->kn_filter) {
1356 kn->kn_fop = &apmfiltops_read;
1357 kn->kn_hook = (caddr_t)sc;
1360 kn->kn_fop = &apmfiltops_write;
1361 kn->kn_hook = (caddr_t)sc;
1364 ap->a_result = EOPNOTSUPP;
1368 klist = &sc->sc_rkq.ki_note;
1369 knote_insert(klist, kn);
1375 apmfilter_detach(struct knote *kn)
1377 struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
1378 struct klist *klist;
1380 klist = &sc->sc_rkq.ki_note;
1381 knote_remove(klist, kn);
1385 apmfilter_read(struct knote *kn, long hint)
1387 struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
1390 if (sc->event_count)
1397 apmfilter_write(struct knote *kn, long hint)
1399 /* write()'s are always OK */
1404 * Because apm is a static device that always exists under any attached
1405 * isa device, and not scanned by the isa device, we need an identify
1406 * function to install the device so we can probe for it.
1408 static device_method_t apm_methods[] = {
1409 /* Device interface */
1410 DEVMETHOD(device_identify, bus_generic_identify),
1411 DEVMETHOD(device_probe, apm_probe),
1412 DEVMETHOD(device_attach, apm_attach),
1417 static driver_t apm_driver = {
1420 1, /* no softc (XXX) */
1423 static devclass_t apm_devclass;
1425 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);