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.4 2003/06/28 04:15:59 dillon Exp $
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/eventhandler.h>
26 #include <sys/kernel.h>
28 #include <sys/reboot.h>
30 #include <sys/select.h>
32 #include <sys/fcntl.h>
34 #include <sys/signalvar.h>
35 #include <sys/sysctl.h>
36 #include <machine/apm_bios.h>
37 #include <machine/segments.h>
38 #include <machine/clock.h>
40 #include <vm/vm_param.h>
42 #include <sys/syslog.h>
44 #include <machine/pc/bios.h>
45 #include <machine/vm86.h>
47 #include <i386/apm/apm.h>
49 /* Used by the apm_saver screen saver module */
50 int apm_display __P((int newstate));
51 struct apm_softc apm_softc;
53 static void apm_resume __P((void));
54 static int apm_bioscall(void);
55 static int apm_check_function_supported __P((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_handle apm_timeout_ch =
77 CALLOUT_HANDLE_INITIALIZER(&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_poll_t apmpoll;
87 static struct cdevsw apm_cdevsw = {
95 /* strategy */ nostrategy,
104 static int apm_suspend_delay = 1;
105 static int apm_standby_delay = 1;
106 static int apm_debug = 0;
108 #define APM_DPRINT(args...) do { \
114 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
115 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
116 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
119 * return 0 if the function successfull,
120 * return 1 if the function unsuccessfull,
121 * return -1 if the function unsupported.
126 struct apm_softc *sc = &apm_softc;
128 u_int apm_func = sc->bios.r.eax & 0xff;
130 if (!apm_check_function_supported(sc->intversion, apm_func)) {
131 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
132 apm_func, sc->majorversion, sc->minorversion);
137 if (sc->connectmode == APM_PROT32CONNECT) {
138 set_bios_selectors(&sc->bios.seg,
139 BIOSCODE_FLAG | BIOSDATA_FLAG);
140 errno = bios32(&sc->bios.r,
141 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
143 errno = bios16(&sc->bios, NULL);
149 /* check whether APM function is supported (1) or not (0). */
151 apm_check_function_supported(u_int version, u_int func)
153 /* except driver version */
154 if (func == APM_DRVVERSION) {
159 case INTVERSION(1, 0):
160 if (func > APM_GETPMEVENT) {
161 return (0); /* not supported */
164 case INTVERSION(1, 1):
165 if (func > APM_ENGAGEDISENGAGEPM &&
166 func < APM_OEMFUNC) {
167 return (0); /* not supported */
170 case INTVERSION(1, 2):
174 return (1); /* supported */
177 /* enable/disable power management */
179 apm_enable_disable_pm(int enable)
181 struct apm_softc *sc = &apm_softc;
183 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
185 if (sc->intversion >= INTVERSION(1, 1))
186 sc->bios.r.ebx = PMDV_ALLDEV;
188 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
189 sc->bios.r.ecx = enable;
191 return (apm_bioscall());
194 /* register driver version (APM 1.1 or later) */
196 apm_driver_version(int version)
198 struct apm_softc *sc = &apm_softc;
200 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
201 sc->bios.r.ebx = 0x0;
202 sc->bios.r.ecx = version;
205 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
208 /* Some old BIOSes don't return the connection version in %ax. */
209 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
215 /* engage/disengage power management (APM 1.1 or later) */
217 apm_engage_disengage_pm(int engage)
219 struct apm_softc *sc = &apm_softc;
221 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
222 sc->bios.r.ebx = PMDV_ALLDEV;
223 sc->bios.r.ecx = engage;
225 return (apm_bioscall());
232 struct apm_softc *sc = &apm_softc;
234 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
240 return (PMEV_NOEVENT);
241 return (sc->bios.r.ebx & 0xffff);
244 /* suspend entire system */
246 apm_suspend_system(int state)
248 struct apm_softc *sc = &apm_softc;
250 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
251 sc->bios.r.ebx = PMDV_ALLDEV;
252 sc->bios.r.ecx = state;
255 if (apm_bioscall()) {
256 printf("Entire system suspend failure: errcode = %d\n",
257 0xff & (sc->bios.r.eax >> 8));
263 /* Display control */
265 * Experimental implementation: My laptop machine can't handle this function
266 * If your laptop can control the display via APM, please inform me.
267 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
270 apm_display(int newstate)
272 struct apm_softc *sc = &apm_softc;
274 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
275 sc->bios.r.ebx = PMDV_DISP0;
276 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
278 if (apm_bioscall() == 0) {
282 /* If failed, then try to blank all display devices instead. */
283 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
284 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
285 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
287 if (apm_bioscall() == 0) {
290 printf("Display off failure: errcode = %d\n",
291 0xff & (sc->bios.r.eax >> 8));
296 * Turn off the entire system.
299 apm_power_off(void *junk, int howto)
301 struct apm_softc *sc = &apm_softc;
303 /* Not halting powering off, or not active */
304 if (!(howto & RB_POWEROFF) || !apm_softc.active)
306 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
307 sc->bios.r.ebx = PMDV_ALLDEV;
308 sc->bios.r.ecx = PMST_OFF;
310 (void) apm_bioscall();
313 /* APM Battery low handler */
315 apm_battery_low(void)
317 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
320 /* APM hook manager */
321 static struct apmhook *
322 apm_add_hook(struct apmhook **list, struct apmhook *ah)
325 struct apmhook *p, *prev;
327 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
331 panic("illegal apm_hook!");
333 for (p = *list; p != NULL; prev = p, p = p->ah_next)
334 if (p->ah_order > ah->ah_order)
341 ah->ah_next = prev->ah_next;
349 apm_del_hook(struct apmhook **list, struct apmhook *ah)
352 struct apmhook *p, *prev;
356 for (p = *list; p != NULL; prev = p, p = p->ah_next)
359 panic("Tried to delete unregistered apm_hook.");
363 prev->ah_next = p->ah_next;
371 /* APM driver calls some functions automatically */
373 apm_execute_hook(struct apmhook *list)
377 for (p = list; p != NULL; p = p->ah_next) {
378 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
379 if ((*(p->ah_fun))(p->ah_arg))
380 printf("Warning: APM hook \"%s\" failed", p->ah_name);
385 /* establish an apm hook */
387 apm_hook_establish(int apmh, struct apmhook *ah)
389 if (apmh < 0 || apmh >= NAPM_HOOK)
392 return apm_add_hook(&hook[apmh], ah);
395 /* disestablish an apm hook */
397 apm_hook_disestablish(int apmh, struct apmhook *ah)
399 if (apmh < 0 || apmh >= NAPM_HOOK)
402 apm_del_hook(&hook[apmh], ah);
406 static struct timeval suspend_time;
407 static struct timeval diff_time;
410 apm_default_resume(void *arg)
413 u_int second, minute, hour;
414 struct timeval resume_time, tmp_time;
416 /* modified for adjkerntz */
418 timer_restore(); /* restore the all timers */
419 inittodr(0); /* adjust time to RTC */
420 microtime(&resume_time);
421 getmicrotime(&tmp_time);
422 timevaladd(&tmp_time, &diff_time);
425 /* XXX THIS DOESN'T WORK!!! */
429 #ifdef APM_FIXUP_CALLTODO
430 /* Calculate the delta time suspended */
431 timevalsub(&resume_time, &suspend_time);
432 /* Fixup the calltodo list with the delta time. */
433 adjust_timeout_calltodo(&resume_time);
434 #endif /* APM_FIXUP_CALLTODOK */
436 #ifndef APM_FIXUP_CALLTODO
437 second = resume_time.tv_sec - suspend_time.tv_sec;
438 #else /* APM_FIXUP_CALLTODO */
440 * We've already calculated resume_time to be the delta between
441 * the suspend and the resume.
443 second = resume_time.tv_sec;
444 #endif /* APM_FIXUP_CALLTODO */
445 hour = second / 3600;
447 minute = second / 60;
449 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
450 hour, minute, second);
455 apm_default_suspend(void *arg)
460 microtime(&diff_time);
462 microtime(&suspend_time);
463 timevalsub(&diff_time, &suspend_time);
468 static int apm_record_event __P((struct apm_softc *, u_int));
469 static void apm_processevent(void);
471 static u_int apm_op_inprog = 0;
476 struct apm_softc *sc = &apm_softc;
483 sc->suspends = sc->suspend_countdown = 0;
485 if (sc->initialized) {
486 error = DEVICE_SUSPEND(root_bus);
488 DEVICE_RESUME(root_bus);
490 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
491 if (apm_suspend_system(PMST_SUSPEND) == 0) {
494 /* Failure, 'resume' the system again */
495 apm_execute_hook(hook[APM_HOOK_RESUME]);
496 DEVICE_RESUME(root_bus);
505 struct apm_softc *sc = &apm_softc;
511 sc->standbys = sc->standby_countdown = 0;
513 if (sc->initialized) {
515 * As far as standby, we don't need to execute
516 * all of suspend hooks.
518 apm_default_suspend(&apm_softc);
519 if (apm_suspend_system(PMST_STANDBY) == 0)
525 apm_lastreq_notify(void)
527 struct apm_softc *sc = &apm_softc;
529 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
530 sc->bios.r.ebx = PMDV_ALLDEV;
531 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
537 apm_lastreq_rejected(void)
539 struct apm_softc *sc = &apm_softc;
541 if (apm_op_inprog == 0) {
542 return 1; /* no operation in progress */
545 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
546 sc->bios.r.ebx = PMDV_ALLDEV;
547 sc->bios.r.ecx = PMST_LASTREQREJECT;
550 if (apm_bioscall()) {
551 APM_DPRINT("apm_lastreq_rejected: failed\n");
559 * Public interface to the suspend/resume:
561 * Execute suspend and resume hook before and after sleep, respectively.
566 apm_suspend(int state)
568 struct apm_softc *sc = &apm_softc;
570 if (!sc->initialized)
578 sc->suspend_countdown = apm_suspend_delay;
584 sc->standby_countdown = apm_standby_delay;
587 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
592 apm_lastreq_notify();
598 struct apm_softc *sc = &apm_softc;
603 if (sc->initialized) {
604 apm_execute_hook(hook[APM_HOOK_RESUME]);
605 DEVICE_RESUME(root_bus);
610 /* get power status per battery */
612 apm_get_pwstatus(apm_pwstatus_t app)
614 struct apm_softc *sc = &apm_softc;
616 if (app->ap_device != PMDV_ALLDEV &&
617 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
620 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
621 sc->bios.r.ebx = app->ap_device;
623 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
628 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
629 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
630 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
631 app->ap_batt_life = sc->bios.r.ecx & 0xff;
632 sc->bios.r.edx &= 0xffff;
633 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
634 app->ap_batt_time = -1;
635 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
636 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
637 else /* Time is in seconds */
638 app->ap_batt_time = sc->bios.r.edx;
644 /* get APM information */
646 apm_get_info(apm_info_t aip)
648 struct apm_softc *sc = &apm_softc;
649 struct apm_pwstatus aps;
651 bzero(&aps, sizeof(aps));
652 aps.ap_device = PMDV_ALLDEV;
653 if (apm_get_pwstatus(&aps))
656 aip->ai_infoversion = 1;
657 aip->ai_acline = aps.ap_acline;
658 aip->ai_batt_stat = aps.ap_batt_stat;
659 aip->ai_batt_life = aps.ap_batt_life;
660 aip->ai_batt_time = aps.ap_batt_time;
661 aip->ai_major = (u_int)sc->majorversion;
662 aip->ai_minor = (u_int)sc->minorversion;
663 aip->ai_status = (u_int)sc->active;
665 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
669 if (apm_bioscall()) {
670 aip->ai_batteries = -1; /* Unknown */
671 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
673 aip->ai_batteries = sc->bios.r.ebx & 0xff;
674 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
677 bzero(aip->ai_spare, sizeof aip->ai_spare);
683 /* inform APM BIOS that CPU is idle */
687 struct apm_softc *sc = &apm_softc;
691 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
692 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
693 (void) apm_bioscall();
696 * Some APM implementation halts CPU in BIOS, whenever
697 * "CPU-idle" function are invoked, but swtch() of
698 * FreeBSD halts CPU, therefore, CPU is halted twice
699 * in the sched loop. It makes the interrupt latency
700 * terribly long and be able to cause a serious problem
701 * in interrupt processing. We prevent it by removing
702 * "hlt" operation from swtch() and managed it under
705 if (!sc->active || sc->always_halt_cpu)
706 __asm("hlt"); /* wait for interrupt */
709 /* inform APM BIOS that CPU is busy */
713 struct apm_softc *sc = &apm_softc;
716 * The APM specification says this is only necessary if your BIOS
717 * slows down the processor in the idle task, otherwise it's not
720 if (sc->slow_idle_cpu && sc->active) {
722 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
723 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
730 * APM timeout routine:
732 * This routine is automatically called by timer once per second.
736 apm_timeout(void *dummy)
738 struct apm_softc *sc = &apm_softc;
741 apm_lastreq_notify();
743 if (sc->standbys && sc->standby_countdown-- <= 0)
746 if (sc->suspends && sc->suspend_countdown-- <= 0)
753 /* Run slightly more oftan than 1 Hz */
754 apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1 );
757 /* enable APM BIOS */
759 apm_event_enable(void)
761 struct apm_softc *sc = &apm_softc;
763 APM_DPRINT("called apm_event_enable()\n");
764 if (sc->initialized) {
770 /* disable APM BIOS */
772 apm_event_disable(void)
774 struct apm_softc *sc = &apm_softc;
776 APM_DPRINT("called apm_event_disable()\n");
777 if (sc->initialized) {
778 untimeout(apm_timeout, NULL, apm_timeout_ch);
783 /* halt CPU in scheduling loop */
787 struct apm_softc *sc = &apm_softc;
790 sc->always_halt_cpu = 1;
793 /* don't halt CPU in scheduling loop */
795 apm_not_halt_cpu(void)
797 struct apm_softc *sc = &apm_softc;
800 sc->always_halt_cpu = 0;
803 /* device driver definitions */
806 * Create "connection point"
809 apm_identify(driver_t *driver, device_t parent)
813 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
815 panic("apm_identify");
822 apm_probe(device_t dev)
824 #define APM_KERNBASE KERNBASE
825 struct vm86frame vmf;
826 struct apm_softc *sc = &apm_softc;
829 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
833 device_set_desc(dev, "APM BIOS");
835 if ( device_get_unit(dev) > 0 ) {
836 printf("apm: Only one APM driver supported.\n");
840 if (resource_int_value("apm", 0, "flags", &flags) != 0)
843 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
844 bzero(&apm_softc, sizeof(apm_softc));
845 vmf.vmf_ah = APM_BIOS;
846 vmf.vmf_al = APM_INSTCHECK;
848 if (vm86_intcall(APM_INT, &vmf))
849 return ENXIO; /* APM not found */
850 if (vmf.vmf_bx != 0x504d) {
851 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
854 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
855 printf("apm: protected mode connections are not supported\n");
859 apm_version = vmf.vmf_ax;
860 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
861 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
862 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
864 vmf.vmf_ah = APM_BIOS;
865 vmf.vmf_al = APM_DISCONNECT;
867 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
869 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
870 vmf.vmf_ah = APM_BIOS;
871 vmf.vmf_al = APM_PROT32CONNECT;
873 if (vm86_intcall(APM_INT, &vmf)) {
874 printf("apm: 32-bit connection error.\n");
877 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
878 sc->bios.seg.code32.limit = 0xffff;
879 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
880 sc->bios.seg.code16.limit = 0xffff;
881 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
882 sc->bios.seg.data.limit = 0xffff;
883 sc->bios.entry = vmf.vmf_ebx;
884 sc->connectmode = APM_PROT32CONNECT;
886 /* use 16-bit connection */
887 vmf.vmf_ah = APM_BIOS;
888 vmf.vmf_al = APM_PROT16CONNECT;
890 if (vm86_intcall(APM_INT, &vmf)) {
891 printf("apm: 16-bit connection error.\n");
894 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
895 sc->bios.seg.code16.limit = 0xffff;
896 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
897 sc->bios.seg.data.limit = 0xffff;
898 sc->bios.entry = vmf.vmf_bx;
899 sc->connectmode = APM_PROT16CONNECT;
906 * return 0 if the user will notice and handle the event,
907 * return 1 if the kernel driver should do so.
910 apm_record_event(struct apm_softc *sc, u_int event_type)
912 struct apm_event_info *evp;
914 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
915 return 1; /* no user waiting */
916 if (sc->event_count == APM_NEVENTS)
917 return 1; /* overflow */
918 if (sc->event_filter[event_type] == 0)
919 return 1; /* not registered */
920 evp = &sc->event_list[sc->event_ptr];
923 sc->event_ptr %= APM_NEVENTS;
924 evp->type = event_type;
925 evp->index = ++apm_evindex;
926 selwakeup(&sc->sc_rsel);
927 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
930 /* Process APM event */
932 apm_processevent(void)
935 struct apm_softc *sc = &apm_softc;
937 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
938 APM_DPRINT("Received APM Event: " #symbol "\n");
941 apm_event = apm_getevent();
943 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
944 if (apm_op_inprog == 0) {
946 if (apm_record_event(sc, apm_event)) {
947 apm_suspend(PMST_STANDBY);
951 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
952 if (apm_op_inprog == 0) {
954 if (apm_record_event(sc, apm_event)) {
955 apm_suspend(PMST_STANDBY);
959 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
960 apm_lastreq_notify();
961 if (apm_op_inprog == 0) {
963 if (apm_record_event(sc, apm_event)) {
967 return; /* XXX skip the rest */
968 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
969 apm_lastreq_notify();
970 if (apm_op_inprog == 0) {
972 if (apm_record_event(sc, apm_event)) {
976 return; /* XXX skip the rest */
977 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
980 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
981 apm_record_event(sc, apm_event);
984 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
985 apm_record_event(sc, apm_event);
988 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
989 apm_record_event(sc, apm_event);
992 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
993 if (apm_record_event(sc, apm_event)) {
995 apm_suspend(PMST_SUSPEND);
998 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
999 apm_record_event(sc, apm_event);
1001 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1002 apm_record_event(sc, apm_event);
1003 inittodr(0); /* adjust time to RTC */
1005 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1006 apm_record_event(sc, apm_event);
1011 printf("Unknown Original APM Event 0x%x\n", apm_event);
1014 } while (apm_event != PMEV_NOEVENT);
1020 * Initialize APM driver
1024 apm_attach(device_t dev)
1026 struct apm_softc *sc = &apm_softc;
1030 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1034 statclock_disable = 1;
1036 sc->initialized = 0;
1038 /* Must be externally enabled */
1041 /* Always call HLT in idle loop */
1042 sc->always_halt_cpu = 1;
1044 getenv_int("debug.apm_debug", &apm_debug);
1046 /* print bootstrap messages */
1047 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1048 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1049 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1050 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1051 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1052 is_enabled(!sc->disabled));
1053 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1054 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1057 * In one test, apm bios version was 1.02; an attempt to register
1058 * a 1.04 driver resulted in a 1.00 connection! Registering a
1059 * 1.02 driver resulted in a 1.02 connection.
1061 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1062 for (; drv_version > 0x100; drv_version--)
1063 if (apm_driver_version(drv_version) == 0)
1065 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1066 ((drv_version & 0x000f) >> 0);
1067 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1068 ((apm_version & 0x0f00) >> 8);
1070 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1072 if (sc->intversion >= INTVERSION(1, 1))
1073 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1074 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1075 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1076 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1077 sc->majorversion, sc->minorversion);
1080 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1081 /* enable power management */
1083 if (apm_enable_disable_pm(1)) {
1084 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1085 (sc->bios.r.eax >> 8) & 0xff);
1089 /* engage power managment (APM 1.1 or later) */
1090 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1091 if (apm_engage_disengage_pm(1)) {
1092 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1093 (sc->bios.r.eax >> 8) & 0xff);
1094 APM_DPRINT(" (Docked or using external power?).\n");
1098 /* default suspend hook */
1099 sc->sc_suspend.ah_fun = apm_default_suspend;
1100 sc->sc_suspend.ah_arg = sc;
1101 sc->sc_suspend.ah_name = "default suspend";
1102 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1104 /* default resume hook */
1105 sc->sc_resume.ah_fun = apm_default_resume;
1106 sc->sc_resume.ah_arg = sc;
1107 sc->sc_resume.ah_name = "default resume";
1108 sc->sc_resume.ah_order = APM_MIN_ORDER;
1110 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1111 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1113 /* Power the system off using APM */
1114 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1117 sc->initialized = 1;
1119 make_dev(&apm_cdevsw, 0, 0, 5, 0660, "apm");
1120 make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl");
1125 apmopen(dev_t dev, int flag, int fmt, d_thread_t *td)
1127 struct apm_softc *sc = &apm_softc;
1128 int ctl = APMDEV(dev);
1130 if (!sc->initialized)
1135 if (!(flag & FWRITE))
1137 if (sc->sc_flags & SCFLAG_OCTL)
1139 sc->sc_flags |= SCFLAG_OCTL;
1140 bzero(sc->event_filter, sizeof sc->event_filter);
1143 sc->sc_flags |= SCFLAG_ONORMAL;
1153 apmclose(dev_t dev, int flag, int fmt, d_thread_t *td)
1155 struct apm_softc *sc = &apm_softc;
1156 int ctl = APMDEV(dev);
1160 apm_lastreq_rejected();
1161 sc->sc_flags &= ~SCFLAG_OCTL;
1162 bzero(sc->event_filter, sizeof sc->event_filter);
1165 sc->sc_flags &= ~SCFLAG_ONORMAL;
1168 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1169 sc->event_count = 0;
1176 apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
1178 struct apm_softc *sc = &apm_softc;
1179 struct apm_bios_arg *args;
1184 if (!sc->initialized)
1186 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1189 if (!(flag & FWRITE))
1192 apm_suspend(PMST_SUSPEND);
1198 if (!(flag & FWRITE))
1201 apm_suspend(PMST_STANDBY);
1206 case APMIO_GETINFO_OLD:
1208 struct apm_info info;
1209 apm_info_old_t aiop;
1211 if (apm_get_info(&info))
1213 aiop = (apm_info_old_t)addr;
1214 aiop->ai_major = info.ai_major;
1215 aiop->ai_minor = info.ai_minor;
1216 aiop->ai_acline = info.ai_acline;
1217 aiop->ai_batt_stat = info.ai_batt_stat;
1218 aiop->ai_batt_life = info.ai_batt_life;
1219 aiop->ai_status = info.ai_status;
1223 if (apm_get_info((apm_info_t)addr))
1226 case APMIO_GETPWSTATUS:
1227 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1231 if (!(flag & FWRITE))
1236 if (!(flag & FWRITE))
1238 apm_event_disable();
1241 if (!(flag & FWRITE))
1245 case APMIO_NOTHALTCPU:
1246 if (!(flag & FWRITE))
1251 if (!(flag & FWRITE))
1253 newstate = *(int *)addr;
1254 if (apm_display(newstate))
1258 if (!(flag & FWRITE))
1260 /* XXX compatibility with the old interface */
1261 args = (struct apm_bios_arg *)addr;
1262 sc->bios.r.eax = args->eax;
1263 sc->bios.r.ebx = args->ebx;
1264 sc->bios.r.ecx = args->ecx;
1265 sc->bios.r.edx = args->edx;
1266 sc->bios.r.esi = args->esi;
1267 sc->bios.r.edi = args->edi;
1268 if ((ret = apm_bioscall())) {
1270 * Return code 1 means bios call was unsuccessful.
1271 * Error code is stored in %ah.
1272 * Return code -1 means bios call was unsupported
1273 * in the APM BIOS version.
1280 * Return code 0 means bios call was successful.
1281 * We need only %al and can discard %ah.
1283 sc->bios.r.eax &= 0xff;
1285 args->eax = sc->bios.r.eax;
1286 args->ebx = sc->bios.r.ebx;
1287 args->ecx = sc->bios.r.ecx;
1288 args->edx = sc->bios.r.edx;
1289 args->esi = sc->bios.r.esi;
1290 args->edi = sc->bios.r.edi;
1297 /* for /dev/apmctl */
1298 if (APMDEV(dev) == APMDEV_CTL) {
1299 struct apm_event_info *evp;
1304 case APMIO_NEXTEVENT:
1305 if (!sc->event_count) {
1308 evp = (struct apm_event_info *)addr;
1309 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1311 *evp = sc->event_list[i];
1315 case APMIO_REJECTLASTREQ:
1316 if (apm_lastreq_rejected()) {
1330 apmwrite(dev_t dev, struct uio *uio, int ioflag)
1332 struct apm_softc *sc = &apm_softc;
1337 if (APMDEV(dev) != APMDEV_CTL)
1339 if (uio->uio_resid != sizeof(u_int))
1342 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1345 if (event_type < 0 || event_type >= APM_NPMEV)
1348 if (sc->event_filter[event_type] == 0) {
1353 sc->event_filter[event_type] = enabled;
1354 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1356 return uio->uio_resid;
1360 apmpoll(dev_t dev, int events, d_thread_t *td)
1362 struct apm_softc *sc = &apm_softc;
1365 if (events & (POLLIN | POLLRDNORM)) {
1366 if (sc->event_count) {
1367 revents |= events & (POLLIN | POLLRDNORM);
1369 selrecord(td, &sc->sc_rsel);
1376 static device_method_t apm_methods[] = {
1377 /* Device interface */
1378 DEVMETHOD(device_identify, apm_identify),
1379 DEVMETHOD(device_probe, apm_probe),
1380 DEVMETHOD(device_attach, apm_attach),
1385 static driver_t apm_driver = {
1388 1, /* no softc (XXX) */
1391 static devclass_t apm_devclass;
1393 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);