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,
111 "Enable debug output");
114 * return 0 if the function successfull,
115 * return 1 if the function unsuccessfull,
116 * return -1 if the function unsupported.
121 struct apm_softc *sc = &apm_softc;
123 u_int apm_func = sc->bios.r.eax & 0xff;
125 if (!apm_check_function_supported(sc->intversion, apm_func)) {
126 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
127 apm_func, sc->majorversion, sc->minorversion);
132 if (sc->connectmode == APM_PROT32CONNECT) {
133 set_bios_selectors(&sc->bios.seg,
134 BIOSCODE_FLAG | BIOSDATA_FLAG);
135 error = bios32(&sc->bios.r,
136 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
138 error = bios16(&sc->bios, NULL);
144 /* check whether APM function is supported (1) or not (0). */
146 apm_check_function_supported(u_int version, u_int func)
148 /* except driver version */
149 if (func == APM_DRVVERSION) {
154 case INTVERSION(1, 0):
155 if (func > APM_GETPMEVENT) {
156 return (0); /* not supported */
159 case INTVERSION(1, 1):
160 if (func > APM_ENGAGEDISENGAGEPM &&
161 func < APM_OEMFUNC) {
162 return (0); /* not supported */
165 case INTVERSION(1, 2):
169 return (1); /* supported */
172 /* enable/disable power management */
174 apm_enable_disable_pm(int enable)
176 struct apm_softc *sc = &apm_softc;
178 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
180 if (sc->intversion >= INTVERSION(1, 1))
181 sc->bios.r.ebx = PMDV_ALLDEV;
183 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
184 sc->bios.r.ecx = enable;
186 return (apm_bioscall());
189 /* register driver version (APM 1.1 or later) */
191 apm_driver_version(int version)
193 struct apm_softc *sc = &apm_softc;
195 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
196 sc->bios.r.ebx = 0x0;
197 sc->bios.r.ecx = version;
200 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
203 /* Some old BIOSes don't return the connection version in %ax. */
204 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
210 /* engage/disengage power management (APM 1.1 or later) */
212 apm_engage_disengage_pm(int engage)
214 struct apm_softc *sc = &apm_softc;
216 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
217 sc->bios.r.ebx = PMDV_ALLDEV;
218 sc->bios.r.ecx = engage;
220 return (apm_bioscall());
227 struct apm_softc *sc = &apm_softc;
229 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
235 return (PMEV_NOEVENT);
236 return (sc->bios.r.ebx & 0xffff);
239 /* suspend entire system */
241 apm_suspend_system(int state)
243 struct apm_softc *sc = &apm_softc;
245 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
246 sc->bios.r.ebx = PMDV_ALLDEV;
247 sc->bios.r.ecx = state;
250 if (apm_bioscall()) {
251 kprintf("Entire system suspend failure: errcode = %d\n",
252 0xff & (sc->bios.r.eax >> 8));
258 /* Display control */
260 * Experimental implementation: My laptop machine can't handle this function
261 * If your laptop can control the display via APM, please inform me.
262 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
265 apm_display(int newstate)
267 struct apm_softc *sc = &apm_softc;
269 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
270 sc->bios.r.ebx = PMDV_DISP0;
271 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
273 if (apm_bioscall() == 0) {
277 /* If failed, then try to blank all display devices instead. */
278 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
279 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
280 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
282 if (apm_bioscall() == 0) {
285 kprintf("Display off failure: errcode = %d\n",
286 0xff & (sc->bios.r.eax >> 8));
291 * Turn off the entire system.
294 apm_power_off(void *junk, int howto)
296 struct apm_softc *sc = &apm_softc;
298 /* Not halting powering off, or not active */
299 if (!(howto & RB_POWEROFF) || !apm_softc.active)
301 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
302 sc->bios.r.ebx = PMDV_ALLDEV;
303 sc->bios.r.ecx = PMST_OFF;
308 /* APM Battery low handler */
310 apm_battery_low(void)
312 kprintf("\007\007 * * * BATTERY IS LOW * * * \007\007");
315 /* APM hook manager */
316 static struct apmhook *
317 apm_add_hook(struct apmhook **list, struct apmhook *ah)
319 struct apmhook *p, *prev;
321 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
325 panic("illegal apm_hook!");
327 for (p = *list; p != NULL; prev = p, p = p->ah_next)
328 if (p->ah_order > ah->ah_order)
335 ah->ah_next = prev->ah_next;
343 apm_del_hook(struct apmhook **list, struct apmhook *ah)
345 struct apmhook *p, *prev;
349 for (p = *list; p != NULL; prev = p, p = p->ah_next)
352 panic("Tried to delete unregistered apm_hook.");
356 prev->ah_next = p->ah_next;
364 /* APM driver calls some functions automatically */
366 apm_execute_hook(struct apmhook *list)
370 for (p = list; p != NULL; p = p->ah_next) {
371 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
372 if ((*(p->ah_fun))(p->ah_arg))
373 kprintf("Warning: APM hook \"%s\" failed", p->ah_name);
378 /* establish an apm hook */
380 apm_hook_establish(int apmh, struct apmhook *ah)
382 if (apmh < 0 || apmh >= NAPM_HOOK)
385 return apm_add_hook(&hook[apmh], ah);
388 /* disestablish an apm hook */
390 apm_hook_disestablish(int apmh, struct apmhook *ah)
392 if (apmh < 0 || apmh >= NAPM_HOOK)
395 apm_del_hook(&hook[apmh], ah);
399 static struct timeval suspend_time;
400 static struct timeval diff_time;
403 apm_default_resume(void *arg)
405 u_int second, minute, hour;
406 struct timeval resume_time, tmp_time;
408 /* modified for adjkerntz */
410 timer_restore(); /* restore the all timers */
411 inittodr(0); /* adjust time to RTC */
412 microtime(&resume_time);
413 getmicrotime(&tmp_time);
414 timevaladd(&tmp_time, &diff_time);
417 /* XXX THIS DOESN'T WORK!!! */
421 #ifdef APM_FIXUP_CALLTODO
422 /* Calculate the delta time suspended */
423 timevalsub(&resume_time, &suspend_time);
424 /* Fixup the calltodo list with the delta time. */
425 adjust_timeout_calltodo(&resume_time);
426 #endif /* APM_FIXUP_CALLTODOK */
428 #ifndef APM_FIXUP_CALLTODO
429 second = resume_time.tv_sec - suspend_time.tv_sec;
430 #else /* APM_FIXUP_CALLTODO */
432 * We've already calculated resume_time to be the delta between
433 * the suspend and the resume.
435 second = resume_time.tv_sec;
436 #endif /* APM_FIXUP_CALLTODO */
437 hour = second / 3600;
439 minute = second / 60;
441 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
442 hour, minute, second);
447 apm_default_suspend(void *arg)
450 microtime(&diff_time);
452 microtime(&suspend_time);
453 timevalsub(&diff_time, &suspend_time);
458 static int apm_record_event (struct apm_softc *, u_int);
459 static void apm_processevent(void);
461 static u_int apm_op_inprog = 0;
466 struct apm_softc *sc = &apm_softc;
473 sc->suspends = sc->suspend_countdown = 0;
475 if (sc->initialized) {
476 error = DEVICE_SUSPEND(root_bus);
478 DEVICE_RESUME(root_bus);
480 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
481 if (apm_suspend_system(PMST_SUSPEND) == 0) {
484 /* Failure, 'resume' the system again */
485 apm_execute_hook(hook[APM_HOOK_RESUME]);
486 DEVICE_RESUME(root_bus);
495 struct apm_softc *sc = &apm_softc;
501 sc->standbys = sc->standby_countdown = 0;
503 if (sc->initialized) {
505 * As far as standby, we don't need to execute
506 * all of suspend hooks.
508 apm_default_suspend(&apm_softc);
509 if (apm_suspend_system(PMST_STANDBY) == 0)
515 apm_lastreq_notify(void)
517 struct apm_softc *sc = &apm_softc;
519 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
520 sc->bios.r.ebx = PMDV_ALLDEV;
521 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
527 apm_lastreq_rejected(void)
529 struct apm_softc *sc = &apm_softc;
531 if (apm_op_inprog == 0) {
532 return 1; /* no operation in progress */
535 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
536 sc->bios.r.ebx = PMDV_ALLDEV;
537 sc->bios.r.ecx = PMST_LASTREQREJECT;
540 if (apm_bioscall()) {
541 APM_DPRINT("apm_lastreq_rejected: failed\n");
549 * Public interface to the suspend/resume:
551 * Execute suspend and resume hook before and after sleep, respectively.
556 apm_suspend(int state)
558 struct apm_softc *sc = &apm_softc;
560 if (!sc->initialized)
568 sc->suspend_countdown = apm_suspend_delay;
574 sc->standby_countdown = apm_standby_delay;
577 kprintf("apm_suspend: Unknown Suspend state 0x%x\n", state);
582 apm_lastreq_notify();
588 struct apm_softc *sc = &apm_softc;
593 if (sc->initialized) {
594 apm_execute_hook(hook[APM_HOOK_RESUME]);
595 DEVICE_RESUME(root_bus);
600 /* get power status per battery */
602 apm_get_pwstatus(apm_pwstatus_t app)
604 struct apm_softc *sc = &apm_softc;
606 if (app->ap_device != PMDV_ALLDEV &&
607 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
610 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
611 sc->bios.r.ebx = app->ap_device;
613 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
618 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
619 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
620 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
621 app->ap_batt_life = sc->bios.r.ecx & 0xff;
622 sc->bios.r.edx &= 0xffff;
623 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
624 app->ap_batt_time = -1;
625 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
626 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
627 else /* Time is in seconds */
628 app->ap_batt_time = sc->bios.r.edx;
634 /* get APM information */
636 apm_get_info(apm_info_t aip)
638 struct apm_softc *sc = &apm_softc;
639 struct apm_pwstatus aps;
641 bzero(&aps, sizeof(aps));
642 aps.ap_device = PMDV_ALLDEV;
643 if (apm_get_pwstatus(&aps))
646 aip->ai_infoversion = 1;
647 aip->ai_acline = aps.ap_acline;
648 aip->ai_batt_stat = aps.ap_batt_stat;
649 aip->ai_batt_life = aps.ap_batt_life;
650 aip->ai_batt_time = aps.ap_batt_time;
651 aip->ai_major = (u_int)sc->majorversion;
652 aip->ai_minor = (u_int)sc->minorversion;
653 aip->ai_status = (u_int)sc->active;
655 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
659 if (apm_bioscall()) {
660 aip->ai_batteries = -1; /* Unknown */
661 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
663 aip->ai_batteries = sc->bios.r.ebx & 0xff;
664 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
667 bzero(aip->ai_spare, sizeof aip->ai_spare);
673 /* inform APM BIOS that CPU is idle */
677 struct apm_softc *sc = &apm_softc;
681 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
682 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
686 * Some APM implementation halts CPU in BIOS, whenever
687 * "CPU-idle" function are invoked, but swtch() of
688 * FreeBSD halts CPU, therefore, CPU is halted twice
689 * in the sched loop. It makes the interrupt latency
690 * terribly long and be able to cause a serious problem
691 * in interrupt processing. We prevent it by removing
692 * "hlt" operation from swtch() and managed it under
695 if (!sc->active || sc->always_halt_cpu)
696 __asm("hlt"); /* wait for interrupt */
699 /* inform APM BIOS that CPU is busy */
703 struct apm_softc *sc = &apm_softc;
706 * The APM specification says this is only necessary if your BIOS
707 * slows down the processor in the idle task, otherwise it's not
710 if (sc->slow_idle_cpu && sc->active) {
712 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
713 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
720 * APM timeout routine:
722 * This routine is automatically called by timer once per second.
726 apm_timeout(void *dummy)
728 struct apm_softc *sc = &apm_softc;
731 apm_lastreq_notify();
733 if (sc->standbys && sc->standby_countdown-- <= 0)
736 if (sc->suspends && sc->suspend_countdown-- <= 0)
742 if (sc->active == 1) {
743 /* Run slightly more oftan than 1 Hz */
744 callout_reset(&apm_timeout_ch, hz - 1, apm_timeout, NULL);
748 /* enable APM BIOS */
750 apm_event_enable(void)
752 struct apm_softc *sc = &apm_softc;
754 APM_DPRINT("called apm_event_enable()\n");
755 if (sc->initialized) {
757 callout_init(&apm_timeout_ch);
762 /* disable APM BIOS */
764 apm_event_disable(void)
766 struct apm_softc *sc = &apm_softc;
768 APM_DPRINT("called apm_event_disable()\n");
769 if (sc->initialized) {
770 callout_stop(&apm_timeout_ch);
775 /* halt CPU in scheduling loop */
779 struct apm_softc *sc = &apm_softc;
782 sc->always_halt_cpu = 1;
785 /* don't halt CPU in scheduling loop */
787 apm_not_halt_cpu(void)
789 struct apm_softc *sc = &apm_softc;
792 sc->always_halt_cpu = 0;
795 /* device driver definitions */
801 apm_probe(device_t dev)
803 #define APM_KERNBASE KERNBASE
804 struct vm86frame vmf;
805 struct apm_softc *sc = &apm_softc;
808 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
812 device_set_desc(dev, "APM BIOS");
814 if ( device_get_unit(dev) > 0 ) {
815 kprintf("apm: Only one APM driver supported.\n");
819 if (resource_int_value("apm", 0, "flags", &flags) != 0)
822 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
823 bzero(&apm_softc, sizeof(apm_softc));
824 vmf.vmf_ah = APM_BIOS;
825 vmf.vmf_al = APM_INSTCHECK;
827 if (vm86_intcall(APM_INT, &vmf))
828 return ENXIO; /* APM not found */
829 if (vmf.vmf_bx != 0x504d) {
830 kprintf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
833 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
834 kprintf("apm: protected mode connections are not supported\n");
838 apm_version = vmf.vmf_ax;
839 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
840 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
841 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
843 vmf.vmf_ah = APM_BIOS;
844 vmf.vmf_al = APM_DISCONNECT;
846 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
848 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
849 vmf.vmf_ah = APM_BIOS;
850 vmf.vmf_al = APM_PROT32CONNECT;
852 if (vm86_intcall(APM_INT, &vmf)) {
853 kprintf("apm: 32-bit connection error.\n");
856 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
857 sc->bios.seg.code32.limit = 0xffff;
858 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
859 sc->bios.seg.code16.limit = 0xffff;
860 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
861 sc->bios.seg.data.limit = 0xffff;
862 sc->bios.entry = vmf.vmf_ebx;
863 sc->connectmode = APM_PROT32CONNECT;
865 /* use 16-bit connection */
866 vmf.vmf_ah = APM_BIOS;
867 vmf.vmf_al = APM_PROT16CONNECT;
869 if (vm86_intcall(APM_INT, &vmf)) {
870 kprintf("apm: 16-bit connection error.\n");
873 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
874 sc->bios.seg.code16.limit = 0xffff;
875 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
876 sc->bios.seg.data.limit = 0xffff;
877 sc->bios.entry = vmf.vmf_bx;
878 sc->connectmode = APM_PROT16CONNECT;
885 * return 0 if the user will notice and handle the event,
886 * return 1 if the kernel driver should do so.
889 apm_record_event(struct apm_softc *sc, u_int event_type)
891 struct apm_event_info *evp;
893 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
894 return 1; /* no user waiting */
895 if (sc->event_count == APM_NEVENTS)
896 return 1; /* overflow */
897 if (sc->event_filter[event_type] == 0)
898 return 1; /* not registered */
899 evp = &sc->event_list[sc->event_ptr];
902 sc->event_ptr %= APM_NEVENTS;
903 evp->type = event_type;
904 evp->index = ++apm_evindex;
905 KNOTE(&sc->sc_rkq.ki_note, 0);
906 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
909 /* Process APM event */
911 apm_processevent(void)
914 struct apm_softc *sc = &apm_softc;
916 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
917 APM_DPRINT("Received APM Event: " #symbol "\n");
920 apm_event = apm_getevent();
922 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
923 if (apm_op_inprog == 0) {
925 if (apm_record_event(sc, apm_event)) {
926 apm_suspend(PMST_STANDBY);
930 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
931 if (apm_op_inprog == 0) {
933 if (apm_record_event(sc, apm_event)) {
934 apm_suspend(PMST_STANDBY);
938 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
939 apm_lastreq_notify();
940 if (apm_op_inprog == 0) {
942 if (apm_record_event(sc, apm_event)) {
946 return; /* XXX skip the rest */
947 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
948 apm_lastreq_notify();
949 if (apm_op_inprog == 0) {
951 if (apm_record_event(sc, apm_event)) {
955 return; /* XXX skip the rest */
956 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
959 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
960 apm_record_event(sc, apm_event);
963 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
964 apm_record_event(sc, apm_event);
967 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
968 apm_record_event(sc, apm_event);
971 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
972 if (apm_record_event(sc, apm_event)) {
974 apm_suspend(PMST_SUSPEND);
977 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
978 apm_record_event(sc, apm_event);
980 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
981 apm_record_event(sc, apm_event);
982 inittodr(0); /* adjust time to RTC */
984 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
985 apm_record_event(sc, apm_event);
990 kprintf("Unknown Original APM Event 0x%x\n", apm_event);
993 } while (apm_event != PMEV_NOEVENT);
999 * Initialize APM driver
1003 apm_attach(device_t dev)
1005 struct apm_softc *sc = &apm_softc;
1009 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1012 sc->initialized = 0;
1014 /* Must be externally enabled */
1017 /* Always call HLT in idle loop */
1018 sc->always_halt_cpu = 1;
1020 kgetenv_int("debug.apm_debug", &apm_debug);
1022 /* print bootstrap messages */
1023 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1024 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1025 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1026 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1027 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1028 is_enabled(!sc->disabled));
1029 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1030 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1033 * In one test, apm bios version was 1.02; an attempt to register
1034 * a 1.04 driver resulted in a 1.00 connection! Registering a
1035 * 1.02 driver resulted in a 1.02 connection.
1037 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1038 for (; drv_version > 0x100; drv_version--)
1039 if (apm_driver_version(drv_version) == 0)
1041 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1042 ((drv_version & 0x000f) >> 0);
1043 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1044 ((apm_version & 0x0f00) >> 8);
1046 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1048 if (sc->intversion >= INTVERSION(1, 1))
1049 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1050 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1051 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1052 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1053 sc->majorversion, sc->minorversion);
1056 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1057 /* enable power management */
1059 if (apm_enable_disable_pm(1)) {
1060 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1061 (sc->bios.r.eax >> 8) & 0xff);
1065 /* engage power managment (APM 1.1 or later) */
1066 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1067 if (apm_engage_disengage_pm(1)) {
1068 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1069 (sc->bios.r.eax >> 8) & 0xff);
1070 APM_DPRINT(" (Docked or using external power?).\n");
1074 /* default suspend hook */
1075 sc->sc_suspend.ah_fun = apm_default_suspend;
1076 sc->sc_suspend.ah_arg = sc;
1077 sc->sc_suspend.ah_name = "default suspend";
1078 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1080 /* default resume hook */
1081 sc->sc_resume.ah_fun = apm_default_resume;
1082 sc->sc_resume.ah_arg = sc;
1083 sc->sc_resume.ah_name = "default resume";
1084 sc->sc_resume.ah_order = APM_MIN_ORDER;
1086 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1087 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1089 /* Power the system off using APM */
1090 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1093 sc->initialized = 1;
1095 make_dev(&apm_ops, 0, UID_ROOT, GID_OPERATOR, 0660, "apm");
1096 make_dev(&apm_ops, 8, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
1101 apmopen(struct dev_open_args *ap)
1103 cdev_t dev = ap->a_head.a_dev;
1104 struct apm_softc *sc = &apm_softc;
1105 int ctl = APMDEV(dev);
1107 if (!sc->initialized)
1112 if (!(ap->a_oflags & FWRITE))
1114 if (sc->sc_flags & SCFLAG_OCTL)
1116 sc->sc_flags |= SCFLAG_OCTL;
1117 bzero(sc->event_filter, sizeof sc->event_filter);
1120 sc->sc_flags |= SCFLAG_ONORMAL;
1130 apmclose(struct dev_close_args *ap)
1132 cdev_t dev = ap->a_head.a_dev;
1133 struct apm_softc *sc = &apm_softc;
1134 int ctl = APMDEV(dev);
1138 apm_lastreq_rejected();
1139 sc->sc_flags &= ~SCFLAG_OCTL;
1140 bzero(sc->event_filter, sizeof sc->event_filter);
1143 sc->sc_flags &= ~SCFLAG_ONORMAL;
1146 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1147 sc->event_count = 0;
1154 apmioctl(struct dev_ioctl_args *ap)
1156 cdev_t dev = ap->a_head.a_dev;
1157 struct apm_softc *sc = &apm_softc;
1158 struct apm_bios_arg *args;
1163 if (!sc->initialized)
1165 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", ap->a_cmd);
1167 switch (ap->a_cmd) {
1169 if (!(ap->a_fflag & FWRITE))
1172 apm_suspend(PMST_SUSPEND);
1178 if (!(ap->a_fflag & FWRITE))
1181 apm_suspend(PMST_STANDBY);
1186 case APMIO_GETINFO_OLD:
1188 struct apm_info info;
1189 apm_info_old_t aiop;
1191 if (apm_get_info(&info))
1193 aiop = (apm_info_old_t)ap->a_data;
1194 aiop->ai_major = info.ai_major;
1195 aiop->ai_minor = info.ai_minor;
1196 aiop->ai_acline = info.ai_acline;
1197 aiop->ai_batt_stat = info.ai_batt_stat;
1198 aiop->ai_batt_life = info.ai_batt_life;
1199 aiop->ai_status = info.ai_status;
1203 if (apm_get_info((apm_info_t)ap->a_data))
1206 case APMIO_GETPWSTATUS:
1207 if (apm_get_pwstatus((apm_pwstatus_t)ap->a_data))
1211 if (!(ap->a_fflag & FWRITE))
1216 if (!(ap->a_fflag & FWRITE))
1218 apm_event_disable();
1221 if (!(ap->a_fflag & FWRITE))
1225 case APMIO_NOTHALTCPU:
1226 if (!(ap->a_fflag & FWRITE))
1231 if (!(ap->a_fflag & FWRITE))
1233 newstate = *(int *)ap->a_data;
1234 if (apm_display(newstate))
1238 if (!(ap->a_fflag & FWRITE))
1240 /* XXX compatibility with the old interface */
1241 args = (struct apm_bios_arg *)ap->a_data;
1242 sc->bios.r.eax = args->eax;
1243 sc->bios.r.ebx = args->ebx;
1244 sc->bios.r.ecx = args->ecx;
1245 sc->bios.r.edx = args->edx;
1246 sc->bios.r.esi = args->esi;
1247 sc->bios.r.edi = args->edi;
1248 if ((ret = apm_bioscall())) {
1250 * Return code 1 means bios call was unsuccessful.
1251 * Error code is stored in %ah.
1252 * Return code -1 means bios call was unsupported
1253 * in the APM BIOS version.
1260 * Return code 0 means bios call was successful.
1261 * We need only %al and can discard %ah.
1263 sc->bios.r.eax &= 0xff;
1265 args->eax = sc->bios.r.eax;
1266 args->ebx = sc->bios.r.ebx;
1267 args->ecx = sc->bios.r.ecx;
1268 args->edx = sc->bios.r.edx;
1269 args->esi = sc->bios.r.esi;
1270 args->edi = sc->bios.r.edi;
1277 /* for /dev/apmctl */
1278 if (APMDEV(dev) == APMDEV_CTL) {
1279 struct apm_event_info *evp;
1283 switch (ap->a_cmd) {
1284 case APMIO_NEXTEVENT:
1285 if (!sc->event_count) {
1288 evp = (struct apm_event_info *)ap->a_data;
1289 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1291 *evp = sc->event_list[i];
1295 case APMIO_REJECTLASTREQ:
1296 if (apm_lastreq_rejected()) {
1310 apmwrite(struct dev_write_args *ap)
1312 cdev_t dev = ap->a_head.a_dev;
1313 struct uio *uio = ap->a_uio;
1314 struct apm_softc *sc = &apm_softc;
1319 if (APMDEV(dev) != APMDEV_CTL)
1321 if (uio->uio_resid != sizeof(u_int))
1324 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1327 if (event_type < 0 || event_type >= APM_NPMEV)
1330 if (sc->event_filter[event_type] == 0) {
1335 sc->event_filter[event_type] = enabled;
1336 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1338 return uio->uio_resid;
1341 static struct filterops apmfiltops_read =
1342 { FILTEROP_ISFD, NULL, apmfilter_detach, apmfilter_read };
1343 static struct filterops apmfiltops_write =
1344 { FILTEROP_ISFD, NULL, apmfilter_detach, apmfilter_write };
1347 apmkqfilter(struct dev_kqfilter_args *ap)
1349 struct apm_softc *sc = &apm_softc;
1350 struct knote *kn = ap->a_kn;
1351 struct klist *klist;
1355 switch (kn->kn_filter) {
1357 kn->kn_fop = &apmfiltops_read;
1358 kn->kn_hook = (caddr_t)sc;
1361 kn->kn_fop = &apmfiltops_write;
1362 kn->kn_hook = (caddr_t)sc;
1365 ap->a_result = EOPNOTSUPP;
1369 klist = &sc->sc_rkq.ki_note;
1370 knote_insert(klist, kn);
1376 apmfilter_detach(struct knote *kn)
1378 struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
1379 struct klist *klist;
1381 klist = &sc->sc_rkq.ki_note;
1382 knote_remove(klist, kn);
1386 apmfilter_read(struct knote *kn, long hint)
1388 struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
1391 if (sc->event_count)
1398 apmfilter_write(struct knote *kn, long hint)
1400 /* write()'s are always OK */
1405 * Because apm is a static device that always exists under any attached
1406 * isa device, and not scanned by the isa device, we need an identify
1407 * function to install the device so we can probe for it.
1409 static device_method_t apm_methods[] = {
1410 /* Device interface */
1411 DEVMETHOD(device_identify, bus_generic_identify),
1412 DEVMETHOD(device_probe, apm_probe),
1413 DEVMETHOD(device_attach, apm_attach),
1418 static driver_t apm_driver = {
1421 1, /* no softc (XXX) */
1424 static devclass_t apm_devclass;
1426 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);