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.11 2005/06/03 17:12:17 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>
43 #include <sys/thread2.h>
45 #include <machine/pc/bios.h>
46 #include <machine/vm86.h>
48 #include <i386/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_poll_t apmpoll;
87 static struct cdevsw apm_cdevsw = {
101 /* strategy */ nostrategy,
106 static int apm_suspend_delay = 1;
107 static int apm_standby_delay = 1;
108 static int apm_debug = 0;
110 #define APM_DPRINT(args...) do { \
116 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
117 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
118 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
121 * return 0 if the function successfull,
122 * return 1 if the function unsuccessfull,
123 * return -1 if the function unsupported.
128 struct apm_softc *sc = &apm_softc;
130 u_int apm_func = sc->bios.r.eax & 0xff;
132 if (!apm_check_function_supported(sc->intversion, apm_func)) {
133 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
134 apm_func, sc->majorversion, sc->minorversion);
139 if (sc->connectmode == APM_PROT32CONNECT) {
140 set_bios_selectors(&sc->bios.seg,
141 BIOSCODE_FLAG | BIOSDATA_FLAG);
142 errno = bios32(&sc->bios.r,
143 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
145 errno = bios16(&sc->bios, NULL);
151 /* check whether APM function is supported (1) or not (0). */
153 apm_check_function_supported(u_int version, u_int func)
155 /* except driver version */
156 if (func == APM_DRVVERSION) {
161 case INTVERSION(1, 0):
162 if (func > APM_GETPMEVENT) {
163 return (0); /* not supported */
166 case INTVERSION(1, 1):
167 if (func > APM_ENGAGEDISENGAGEPM &&
168 func < APM_OEMFUNC) {
169 return (0); /* not supported */
172 case INTVERSION(1, 2):
176 return (1); /* supported */
179 /* enable/disable power management */
181 apm_enable_disable_pm(int enable)
183 struct apm_softc *sc = &apm_softc;
185 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
187 if (sc->intversion >= INTVERSION(1, 1))
188 sc->bios.r.ebx = PMDV_ALLDEV;
190 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
191 sc->bios.r.ecx = enable;
193 return (apm_bioscall());
196 /* register driver version (APM 1.1 or later) */
198 apm_driver_version(int version)
200 struct apm_softc *sc = &apm_softc;
202 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
203 sc->bios.r.ebx = 0x0;
204 sc->bios.r.ecx = version;
207 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
210 /* Some old BIOSes don't return the connection version in %ax. */
211 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
217 /* engage/disengage power management (APM 1.1 or later) */
219 apm_engage_disengage_pm(int engage)
221 struct apm_softc *sc = &apm_softc;
223 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
224 sc->bios.r.ebx = PMDV_ALLDEV;
225 sc->bios.r.ecx = engage;
227 return (apm_bioscall());
234 struct apm_softc *sc = &apm_softc;
236 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
242 return (PMEV_NOEVENT);
243 return (sc->bios.r.ebx & 0xffff);
246 /* suspend entire system */
248 apm_suspend_system(int state)
250 struct apm_softc *sc = &apm_softc;
252 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
253 sc->bios.r.ebx = PMDV_ALLDEV;
254 sc->bios.r.ecx = state;
257 if (apm_bioscall()) {
258 printf("Entire system suspend failure: errcode = %d\n",
259 0xff & (sc->bios.r.eax >> 8));
265 /* Display control */
267 * Experimental implementation: My laptop machine can't handle this function
268 * If your laptop can control the display via APM, please inform me.
269 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
272 apm_display(int newstate)
274 struct apm_softc *sc = &apm_softc;
276 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
277 sc->bios.r.ebx = PMDV_DISP0;
278 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
280 if (apm_bioscall() == 0) {
284 /* If failed, then try to blank all display devices instead. */
285 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
286 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
287 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
289 if (apm_bioscall() == 0) {
292 printf("Display off failure: errcode = %d\n",
293 0xff & (sc->bios.r.eax >> 8));
298 * Turn off the entire system.
301 apm_power_off(void *junk, int howto)
303 struct apm_softc *sc = &apm_softc;
305 /* Not halting powering off, or not active */
306 if (!(howto & RB_POWEROFF) || !apm_softc.active)
308 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
309 sc->bios.r.ebx = PMDV_ALLDEV;
310 sc->bios.r.ecx = PMST_OFF;
312 (void) apm_bioscall();
315 /* APM Battery low handler */
317 apm_battery_low(void)
319 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
322 /* APM hook manager */
323 static struct apmhook *
324 apm_add_hook(struct apmhook **list, struct apmhook *ah)
326 struct apmhook *p, *prev;
328 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
332 panic("illegal apm_hook!");
334 for (p = *list; p != NULL; prev = p, p = p->ah_next)
335 if (p->ah_order > ah->ah_order)
342 ah->ah_next = prev->ah_next;
350 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)
412 u_int second, minute, hour;
413 struct timeval resume_time, tmp_time;
415 /* modified for adjkerntz */
417 timer_restore(); /* restore the all timers */
418 inittodr(0); /* adjust time to RTC */
419 microtime(&resume_time);
420 getmicrotime(&tmp_time);
421 timevaladd(&tmp_time, &diff_time);
424 /* XXX THIS DOESN'T WORK!!! */
428 #ifdef APM_FIXUP_CALLTODO
429 /* Calculate the delta time suspended */
430 timevalsub(&resume_time, &suspend_time);
431 /* Fixup the calltodo list with the delta time. */
432 adjust_timeout_calltodo(&resume_time);
433 #endif /* APM_FIXUP_CALLTODOK */
435 #ifndef APM_FIXUP_CALLTODO
436 second = resume_time.tv_sec - suspend_time.tv_sec;
437 #else /* APM_FIXUP_CALLTODO */
439 * We've already calculated resume_time to be the delta between
440 * the suspend and the resume.
442 second = resume_time.tv_sec;
443 #endif /* APM_FIXUP_CALLTODO */
444 hour = second / 3600;
446 minute = second / 60;
448 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
449 hour, minute, second);
454 apm_default_suspend(void *arg)
457 microtime(&diff_time);
459 microtime(&suspend_time);
460 timevalsub(&diff_time, &suspend_time);
465 static int apm_record_event (struct apm_softc *, u_int);
466 static void apm_processevent(void);
468 static u_int apm_op_inprog = 0;
473 struct apm_softc *sc = &apm_softc;
480 sc->suspends = sc->suspend_countdown = 0;
482 if (sc->initialized) {
483 error = DEVICE_SUSPEND(root_bus);
485 DEVICE_RESUME(root_bus);
487 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
488 if (apm_suspend_system(PMST_SUSPEND) == 0) {
491 /* Failure, 'resume' the system again */
492 apm_execute_hook(hook[APM_HOOK_RESUME]);
493 DEVICE_RESUME(root_bus);
502 struct apm_softc *sc = &apm_softc;
508 sc->standbys = sc->standby_countdown = 0;
510 if (sc->initialized) {
512 * As far as standby, we don't need to execute
513 * all of suspend hooks.
515 apm_default_suspend(&apm_softc);
516 if (apm_suspend_system(PMST_STANDBY) == 0)
522 apm_lastreq_notify(void)
524 struct apm_softc *sc = &apm_softc;
526 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
527 sc->bios.r.ebx = PMDV_ALLDEV;
528 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
534 apm_lastreq_rejected(void)
536 struct apm_softc *sc = &apm_softc;
538 if (apm_op_inprog == 0) {
539 return 1; /* no operation in progress */
542 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
543 sc->bios.r.ebx = PMDV_ALLDEV;
544 sc->bios.r.ecx = PMST_LASTREQREJECT;
547 if (apm_bioscall()) {
548 APM_DPRINT("apm_lastreq_rejected: failed\n");
556 * Public interface to the suspend/resume:
558 * Execute suspend and resume hook before and after sleep, respectively.
563 apm_suspend(int state)
565 struct apm_softc *sc = &apm_softc;
567 if (!sc->initialized)
575 sc->suspend_countdown = apm_suspend_delay;
581 sc->standby_countdown = apm_standby_delay;
584 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
589 apm_lastreq_notify();
595 struct apm_softc *sc = &apm_softc;
600 if (sc->initialized) {
601 apm_execute_hook(hook[APM_HOOK_RESUME]);
602 DEVICE_RESUME(root_bus);
607 /* get power status per battery */
609 apm_get_pwstatus(apm_pwstatus_t app)
611 struct apm_softc *sc = &apm_softc;
613 if (app->ap_device != PMDV_ALLDEV &&
614 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
617 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
618 sc->bios.r.ebx = app->ap_device;
620 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
625 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
626 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
627 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
628 app->ap_batt_life = sc->bios.r.ecx & 0xff;
629 sc->bios.r.edx &= 0xffff;
630 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
631 app->ap_batt_time = -1;
632 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
633 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
634 else /* Time is in seconds */
635 app->ap_batt_time = sc->bios.r.edx;
641 /* get APM information */
643 apm_get_info(apm_info_t aip)
645 struct apm_softc *sc = &apm_softc;
646 struct apm_pwstatus aps;
648 bzero(&aps, sizeof(aps));
649 aps.ap_device = PMDV_ALLDEV;
650 if (apm_get_pwstatus(&aps))
653 aip->ai_infoversion = 1;
654 aip->ai_acline = aps.ap_acline;
655 aip->ai_batt_stat = aps.ap_batt_stat;
656 aip->ai_batt_life = aps.ap_batt_life;
657 aip->ai_batt_time = aps.ap_batt_time;
658 aip->ai_major = (u_int)sc->majorversion;
659 aip->ai_minor = (u_int)sc->minorversion;
660 aip->ai_status = (u_int)sc->active;
662 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
666 if (apm_bioscall()) {
667 aip->ai_batteries = -1; /* Unknown */
668 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
670 aip->ai_batteries = sc->bios.r.ebx & 0xff;
671 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
674 bzero(aip->ai_spare, sizeof aip->ai_spare);
680 /* inform APM BIOS that CPU is idle */
684 struct apm_softc *sc = &apm_softc;
688 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
689 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
690 (void) apm_bioscall();
693 * Some APM implementation halts CPU in BIOS, whenever
694 * "CPU-idle" function are invoked, but swtch() of
695 * FreeBSD halts CPU, therefore, CPU is halted twice
696 * in the sched loop. It makes the interrupt latency
697 * terribly long and be able to cause a serious problem
698 * in interrupt processing. We prevent it by removing
699 * "hlt" operation from swtch() and managed it under
702 if (!sc->active || sc->always_halt_cpu)
703 __asm("hlt"); /* wait for interrupt */
706 /* inform APM BIOS that CPU is busy */
710 struct apm_softc *sc = &apm_softc;
713 * The APM specification says this is only necessary if your BIOS
714 * slows down the processor in the idle task, otherwise it's not
717 if (sc->slow_idle_cpu && sc->active) {
719 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
720 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
727 * APM timeout routine:
729 * This routine is automatically called by timer once per second.
733 apm_timeout(void *dummy)
735 struct apm_softc *sc = &apm_softc;
738 apm_lastreq_notify();
740 if (sc->standbys && sc->standby_countdown-- <= 0)
743 if (sc->suspends && sc->suspend_countdown-- <= 0)
749 if (sc->active == 1) {
750 /* Run slightly more oftan than 1 Hz */
751 callout_reset(&apm_timeout_ch, hz - 1, apm_timeout, NULL);
755 /* enable APM BIOS */
757 apm_event_enable(void)
759 struct apm_softc *sc = &apm_softc;
761 APM_DPRINT("called apm_event_enable()\n");
762 if (sc->initialized) {
764 callout_init(&apm_timeout_ch);
769 /* disable APM BIOS */
771 apm_event_disable(void)
773 struct apm_softc *sc = &apm_softc;
775 APM_DPRINT("called apm_event_disable()\n");
776 if (sc->initialized) {
777 callout_stop(&apm_timeout_ch);
782 /* halt CPU in scheduling loop */
786 struct apm_softc *sc = &apm_softc;
789 sc->always_halt_cpu = 1;
792 /* don't halt CPU in scheduling loop */
794 apm_not_halt_cpu(void)
796 struct apm_softc *sc = &apm_softc;
799 sc->always_halt_cpu = 0;
802 /* device driver definitions */
805 * Create "connection point"
808 apm_identify(driver_t *driver, device_t parent)
812 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
814 panic("apm_identify");
821 apm_probe(device_t dev)
823 #define APM_KERNBASE KERNBASE
824 struct vm86frame vmf;
825 struct apm_softc *sc = &apm_softc;
828 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
832 device_set_desc(dev, "APM BIOS");
834 if ( device_get_unit(dev) > 0 ) {
835 printf("apm: Only one APM driver supported.\n");
839 if (resource_int_value("apm", 0, "flags", &flags) != 0)
842 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
843 bzero(&apm_softc, sizeof(apm_softc));
844 vmf.vmf_ah = APM_BIOS;
845 vmf.vmf_al = APM_INSTCHECK;
847 if (vm86_intcall(APM_INT, &vmf))
848 return ENXIO; /* APM not found */
849 if (vmf.vmf_bx != 0x504d) {
850 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
853 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
854 printf("apm: protected mode connections are not supported\n");
858 apm_version = vmf.vmf_ax;
859 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
860 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
861 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
863 vmf.vmf_ah = APM_BIOS;
864 vmf.vmf_al = APM_DISCONNECT;
866 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
868 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
869 vmf.vmf_ah = APM_BIOS;
870 vmf.vmf_al = APM_PROT32CONNECT;
872 if (vm86_intcall(APM_INT, &vmf)) {
873 printf("apm: 32-bit connection error.\n");
876 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
877 sc->bios.seg.code32.limit = 0xffff;
878 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
879 sc->bios.seg.code16.limit = 0xffff;
880 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
881 sc->bios.seg.data.limit = 0xffff;
882 sc->bios.entry = vmf.vmf_ebx;
883 sc->connectmode = APM_PROT32CONNECT;
885 /* use 16-bit connection */
886 vmf.vmf_ah = APM_BIOS;
887 vmf.vmf_al = APM_PROT16CONNECT;
889 if (vm86_intcall(APM_INT, &vmf)) {
890 printf("apm: 16-bit connection error.\n");
893 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
894 sc->bios.seg.code16.limit = 0xffff;
895 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
896 sc->bios.seg.data.limit = 0xffff;
897 sc->bios.entry = vmf.vmf_bx;
898 sc->connectmode = APM_PROT16CONNECT;
905 * return 0 if the user will notice and handle the event,
906 * return 1 if the kernel driver should do so.
909 apm_record_event(struct apm_softc *sc, u_int event_type)
911 struct apm_event_info *evp;
913 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
914 return 1; /* no user waiting */
915 if (sc->event_count == APM_NEVENTS)
916 return 1; /* overflow */
917 if (sc->event_filter[event_type] == 0)
918 return 1; /* not registered */
919 evp = &sc->event_list[sc->event_ptr];
922 sc->event_ptr %= APM_NEVENTS;
923 evp->type = event_type;
924 evp->index = ++apm_evindex;
925 selwakeup(&sc->sc_rsel);
926 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
929 /* Process APM event */
931 apm_processevent(void)
934 struct apm_softc *sc = &apm_softc;
936 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
937 APM_DPRINT("Received APM Event: " #symbol "\n");
940 apm_event = apm_getevent();
942 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
943 if (apm_op_inprog == 0) {
945 if (apm_record_event(sc, apm_event)) {
946 apm_suspend(PMST_STANDBY);
950 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
951 if (apm_op_inprog == 0) {
953 if (apm_record_event(sc, apm_event)) {
954 apm_suspend(PMST_STANDBY);
958 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
959 apm_lastreq_notify();
960 if (apm_op_inprog == 0) {
962 if (apm_record_event(sc, apm_event)) {
966 return; /* XXX skip the rest */
967 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
968 apm_lastreq_notify();
969 if (apm_op_inprog == 0) {
971 if (apm_record_event(sc, apm_event)) {
975 return; /* XXX skip the rest */
976 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
979 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
980 apm_record_event(sc, apm_event);
983 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
984 apm_record_event(sc, apm_event);
987 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
988 apm_record_event(sc, apm_event);
991 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
992 if (apm_record_event(sc, apm_event)) {
994 apm_suspend(PMST_SUSPEND);
997 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
998 apm_record_event(sc, apm_event);
1000 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1001 apm_record_event(sc, apm_event);
1002 inittodr(0); /* adjust time to RTC */
1004 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1005 apm_record_event(sc, apm_event);
1010 printf("Unknown Original APM Event 0x%x\n", apm_event);
1013 } while (apm_event != PMEV_NOEVENT);
1019 * Initialize APM driver
1023 apm_attach(device_t dev)
1025 struct apm_softc *sc = &apm_softc;
1029 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1032 sc->initialized = 0;
1034 /* Must be externally enabled */
1037 /* Always call HLT in idle loop */
1038 sc->always_halt_cpu = 1;
1040 getenv_int("debug.apm_debug", &apm_debug);
1042 /* print bootstrap messages */
1043 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1044 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1045 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1046 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1047 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1048 is_enabled(!sc->disabled));
1049 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1050 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1053 * In one test, apm bios version was 1.02; an attempt to register
1054 * a 1.04 driver resulted in a 1.00 connection! Registering a
1055 * 1.02 driver resulted in a 1.02 connection.
1057 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1058 for (; drv_version > 0x100; drv_version--)
1059 if (apm_driver_version(drv_version) == 0)
1061 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1062 ((drv_version & 0x000f) >> 0);
1063 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1064 ((apm_version & 0x0f00) >> 8);
1066 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1068 if (sc->intversion >= INTVERSION(1, 1))
1069 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1070 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1071 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1072 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1073 sc->majorversion, sc->minorversion);
1076 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1077 /* enable power management */
1079 if (apm_enable_disable_pm(1)) {
1080 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1081 (sc->bios.r.eax >> 8) & 0xff);
1085 /* engage power managment (APM 1.1 or later) */
1086 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1087 if (apm_engage_disengage_pm(1)) {
1088 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1089 (sc->bios.r.eax >> 8) & 0xff);
1090 APM_DPRINT(" (Docked or using external power?).\n");
1094 /* default suspend hook */
1095 sc->sc_suspend.ah_fun = apm_default_suspend;
1096 sc->sc_suspend.ah_arg = sc;
1097 sc->sc_suspend.ah_name = "default suspend";
1098 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1100 /* default resume hook */
1101 sc->sc_resume.ah_fun = apm_default_resume;
1102 sc->sc_resume.ah_arg = sc;
1103 sc->sc_resume.ah_name = "default resume";
1104 sc->sc_resume.ah_order = APM_MIN_ORDER;
1106 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1107 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1109 /* Power the system off using APM */
1110 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1113 sc->initialized = 1;
1115 cdevsw_add(&apm_cdevsw, 0, 0);
1116 make_dev(&apm_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0660, "apm");
1117 make_dev(&apm_cdevsw, 8, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
1122 apmopen(dev_t dev, int flag, int fmt, d_thread_t *td)
1124 struct apm_softc *sc = &apm_softc;
1125 int ctl = APMDEV(dev);
1127 if (!sc->initialized)
1132 if (!(flag & FWRITE))
1134 if (sc->sc_flags & SCFLAG_OCTL)
1136 sc->sc_flags |= SCFLAG_OCTL;
1137 bzero(sc->event_filter, sizeof sc->event_filter);
1140 sc->sc_flags |= SCFLAG_ONORMAL;
1150 apmclose(dev_t dev, int flag, int fmt, d_thread_t *td)
1152 struct apm_softc *sc = &apm_softc;
1153 int ctl = APMDEV(dev);
1157 apm_lastreq_rejected();
1158 sc->sc_flags &= ~SCFLAG_OCTL;
1159 bzero(sc->event_filter, sizeof sc->event_filter);
1162 sc->sc_flags &= ~SCFLAG_ONORMAL;
1165 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1166 sc->event_count = 0;
1173 apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
1175 struct apm_softc *sc = &apm_softc;
1176 struct apm_bios_arg *args;
1181 if (!sc->initialized)
1183 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1186 if (!(flag & FWRITE))
1189 apm_suspend(PMST_SUSPEND);
1195 if (!(flag & FWRITE))
1198 apm_suspend(PMST_STANDBY);
1203 case APMIO_GETINFO_OLD:
1205 struct apm_info info;
1206 apm_info_old_t aiop;
1208 if (apm_get_info(&info))
1210 aiop = (apm_info_old_t)addr;
1211 aiop->ai_major = info.ai_major;
1212 aiop->ai_minor = info.ai_minor;
1213 aiop->ai_acline = info.ai_acline;
1214 aiop->ai_batt_stat = info.ai_batt_stat;
1215 aiop->ai_batt_life = info.ai_batt_life;
1216 aiop->ai_status = info.ai_status;
1220 if (apm_get_info((apm_info_t)addr))
1223 case APMIO_GETPWSTATUS:
1224 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1228 if (!(flag & FWRITE))
1233 if (!(flag & FWRITE))
1235 apm_event_disable();
1238 if (!(flag & FWRITE))
1242 case APMIO_NOTHALTCPU:
1243 if (!(flag & FWRITE))
1248 if (!(flag & FWRITE))
1250 newstate = *(int *)addr;
1251 if (apm_display(newstate))
1255 if (!(flag & FWRITE))
1257 /* XXX compatibility with the old interface */
1258 args = (struct apm_bios_arg *)addr;
1259 sc->bios.r.eax = args->eax;
1260 sc->bios.r.ebx = args->ebx;
1261 sc->bios.r.ecx = args->ecx;
1262 sc->bios.r.edx = args->edx;
1263 sc->bios.r.esi = args->esi;
1264 sc->bios.r.edi = args->edi;
1265 if ((ret = apm_bioscall())) {
1267 * Return code 1 means bios call was unsuccessful.
1268 * Error code is stored in %ah.
1269 * Return code -1 means bios call was unsupported
1270 * in the APM BIOS version.
1277 * Return code 0 means bios call was successful.
1278 * We need only %al and can discard %ah.
1280 sc->bios.r.eax &= 0xff;
1282 args->eax = sc->bios.r.eax;
1283 args->ebx = sc->bios.r.ebx;
1284 args->ecx = sc->bios.r.ecx;
1285 args->edx = sc->bios.r.edx;
1286 args->esi = sc->bios.r.esi;
1287 args->edi = sc->bios.r.edi;
1294 /* for /dev/apmctl */
1295 if (APMDEV(dev) == APMDEV_CTL) {
1296 struct apm_event_info *evp;
1301 case APMIO_NEXTEVENT:
1302 if (!sc->event_count) {
1305 evp = (struct apm_event_info *)addr;
1306 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1308 *evp = sc->event_list[i];
1312 case APMIO_REJECTLASTREQ:
1313 if (apm_lastreq_rejected()) {
1327 apmwrite(dev_t dev, struct uio *uio, int ioflag)
1329 struct apm_softc *sc = &apm_softc;
1334 if (APMDEV(dev) != APMDEV_CTL)
1336 if (uio->uio_resid != sizeof(u_int))
1339 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1342 if (event_type < 0 || event_type >= APM_NPMEV)
1345 if (sc->event_filter[event_type] == 0) {
1350 sc->event_filter[event_type] = enabled;
1351 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1353 return uio->uio_resid;
1357 apmpoll(dev_t dev, int events, d_thread_t *td)
1359 struct apm_softc *sc = &apm_softc;
1362 if (events & (POLLIN | POLLRDNORM)) {
1363 if (sc->event_count) {
1364 revents |= events & (POLLIN | POLLRDNORM);
1366 selrecord(td, &sc->sc_rsel);
1373 static device_method_t apm_methods[] = {
1374 /* Device interface */
1375 DEVMETHOD(device_identify, apm_identify),
1376 DEVMETHOD(device_probe, apm_probe),
1377 DEVMETHOD(device_attach, apm_attach),
1382 static driver_t apm_driver = {
1385 1, /* no softc (XXX) */
1388 static devclass_t apm_devclass;
1390 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);