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)
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/eventhandler.h>
25 #include <sys/kernel.h>
27 #include <sys/reboot.h>
29 #include <sys/select.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>
43 #include <machine/pc/bios.h>
44 #include <machine/vm86.h>
46 #include <i386/apm/apm.h>
48 /* Used by the apm_saver screen saver module */
49 int apm_display __P((int newstate));
50 struct apm_softc apm_softc;
52 static void apm_resume __P((void));
53 static int apm_bioscall(void);
54 static int apm_check_function_supported __P((u_int version, u_int func));
56 static u_long apm_version;
60 #define SCFLAG_ONORMAL 0x0000001
61 #define SCFLAG_OCTL 0x0000002
62 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
64 #define APMDEV(dev) (minor(dev)&0x0f)
65 #define APMDEV_NORMAL 0
68 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
70 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
72 /* Map version number to integer (keeps ordering of version numbers) */
73 #define INTVERSION(major, minor) ((major)*100 + (minor))
75 static struct callout_handle apm_timeout_ch =
76 CALLOUT_HANDLE_INITIALIZER(&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_poll_t apmpoll;
86 static struct cdevsw apm_cdevsw = {
94 /* strategy */ nostrategy,
103 static int apm_suspend_delay = 1;
104 static int apm_standby_delay = 1;
105 static int apm_debug = 0;
107 #define APM_DPRINT(args...) do { \
113 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
114 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
115 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
118 * return 0 if the function successfull,
119 * return 1 if the function unsuccessfull,
120 * return -1 if the function unsupported.
125 struct apm_softc *sc = &apm_softc;
127 u_int apm_func = sc->bios.r.eax & 0xff;
129 if (!apm_check_function_supported(sc->intversion, apm_func)) {
130 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
131 apm_func, sc->majorversion, sc->minorversion);
136 if (sc->connectmode == APM_PROT32CONNECT) {
137 set_bios_selectors(&sc->bios.seg,
138 BIOSCODE_FLAG | BIOSDATA_FLAG);
139 errno = bios32(&sc->bios.r,
140 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
142 errno = bios16(&sc->bios, NULL);
148 /* check whether APM function is supported (1) or not (0). */
150 apm_check_function_supported(u_int version, u_int func)
152 /* except driver version */
153 if (func == APM_DRVVERSION) {
158 case INTVERSION(1, 0):
159 if (func > APM_GETPMEVENT) {
160 return (0); /* not supported */
163 case INTVERSION(1, 1):
164 if (func > APM_ENGAGEDISENGAGEPM &&
165 func < APM_OEMFUNC) {
166 return (0); /* not supported */
169 case INTVERSION(1, 2):
173 return (1); /* supported */
176 /* enable/disable power management */
178 apm_enable_disable_pm(int enable)
180 struct apm_softc *sc = &apm_softc;
182 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
184 if (sc->intversion >= INTVERSION(1, 1))
185 sc->bios.r.ebx = PMDV_ALLDEV;
187 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
188 sc->bios.r.ecx = enable;
190 return (apm_bioscall());
193 /* register driver version (APM 1.1 or later) */
195 apm_driver_version(int version)
197 struct apm_softc *sc = &apm_softc;
199 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
200 sc->bios.r.ebx = 0x0;
201 sc->bios.r.ecx = version;
204 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
207 /* Some old BIOSes don't return the connection version in %ax. */
208 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
214 /* engage/disengage power management (APM 1.1 or later) */
216 apm_engage_disengage_pm(int engage)
218 struct apm_softc *sc = &apm_softc;
220 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
221 sc->bios.r.ebx = PMDV_ALLDEV;
222 sc->bios.r.ecx = engage;
224 return (apm_bioscall());
231 struct apm_softc *sc = &apm_softc;
233 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
239 return (PMEV_NOEVENT);
240 return (sc->bios.r.ebx & 0xffff);
243 /* suspend entire system */
245 apm_suspend_system(int state)
247 struct apm_softc *sc = &apm_softc;
249 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
250 sc->bios.r.ebx = PMDV_ALLDEV;
251 sc->bios.r.ecx = state;
254 if (apm_bioscall()) {
255 printf("Entire system suspend failure: errcode = %d\n",
256 0xff & (sc->bios.r.eax >> 8));
262 /* Display control */
264 * Experimental implementation: My laptop machine can't handle this function
265 * If your laptop can control the display via APM, please inform me.
266 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
269 apm_display(int newstate)
271 struct apm_softc *sc = &apm_softc;
273 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
274 sc->bios.r.ebx = PMDV_DISP0;
275 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
277 if (apm_bioscall() == 0) {
281 /* If failed, then try to blank all display devices instead. */
282 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
283 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
284 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
286 if (apm_bioscall() == 0) {
289 printf("Display off failure: errcode = %d\n",
290 0xff & (sc->bios.r.eax >> 8));
295 * Turn off the entire system.
298 apm_power_off(void *junk, int howto)
300 struct apm_softc *sc = &apm_softc;
302 /* Not halting powering off, or not active */
303 if (!(howto & RB_POWEROFF) || !apm_softc.active)
305 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
306 sc->bios.r.ebx = PMDV_ALLDEV;
307 sc->bios.r.ecx = PMST_OFF;
309 (void) apm_bioscall();
312 /* APM Battery low handler */
314 apm_battery_low(void)
316 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
319 /* APM hook manager */
320 static struct apmhook *
321 apm_add_hook(struct apmhook **list, struct apmhook *ah)
324 struct apmhook *p, *prev;
326 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
330 panic("illegal apm_hook!");
332 for (p = *list; p != NULL; prev = p, p = p->ah_next)
333 if (p->ah_order > ah->ah_order)
340 ah->ah_next = prev->ah_next;
348 apm_del_hook(struct apmhook **list, struct apmhook *ah)
351 struct apmhook *p, *prev;
355 for (p = *list; p != NULL; prev = p, p = p->ah_next)
358 panic("Tried to delete unregistered apm_hook.");
362 prev->ah_next = p->ah_next;
370 /* APM driver calls some functions automatically */
372 apm_execute_hook(struct apmhook *list)
376 for (p = list; p != NULL; p = p->ah_next) {
377 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
378 if ((*(p->ah_fun))(p->ah_arg))
379 printf("Warning: APM hook \"%s\" failed", p->ah_name);
384 /* establish an apm hook */
386 apm_hook_establish(int apmh, struct apmhook *ah)
388 if (apmh < 0 || apmh >= NAPM_HOOK)
391 return apm_add_hook(&hook[apmh], ah);
394 /* disestablish an apm hook */
396 apm_hook_disestablish(int apmh, struct apmhook *ah)
398 if (apmh < 0 || apmh >= NAPM_HOOK)
401 apm_del_hook(&hook[apmh], ah);
404 static int apm_record_event __P((struct apm_softc *, u_int));
405 static void apm_processevent(void);
407 static u_int apm_op_inprog = 0;
412 struct apm_softc *sc = &apm_softc;
419 sc->suspends = sc->suspend_countdown = 0;
421 if (sc->initialized) {
422 error = DEVICE_SUSPEND(root_bus);
424 DEVICE_RESUME(root_bus);
426 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
427 if (apm_suspend_system(PMST_SUSPEND) == 0) {
431 /* Failure, 'resume' the system again */
432 apm_execute_hook(hook[APM_HOOK_RESUME]);
433 DEVICE_RESUME(root_bus);
442 struct apm_softc *sc = &apm_softc;
448 sc->standbys = sc->standby_countdown = 0;
450 if (sc->initialized) {
452 * As far as standby, we don't need to execute
453 * all of suspend hooks.
455 if (apm_suspend_system(PMST_STANDBY) == 0)
461 apm_lastreq_notify(void)
463 struct apm_softc *sc = &apm_softc;
465 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
466 sc->bios.r.ebx = PMDV_ALLDEV;
467 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
473 apm_lastreq_rejected(void)
475 struct apm_softc *sc = &apm_softc;
477 if (apm_op_inprog == 0) {
478 return 1; /* no operation in progress */
481 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
482 sc->bios.r.ebx = PMDV_ALLDEV;
483 sc->bios.r.ecx = PMST_LASTREQREJECT;
486 if (apm_bioscall()) {
487 APM_DPRINT("apm_lastreq_rejected: failed\n");
495 * Public interface to the suspend/resume:
497 * Execute suspend and resume hook before and after sleep, respectively.
502 apm_suspend(int state)
504 struct apm_softc *sc = &apm_softc;
506 if (!sc->initialized)
514 sc->suspend_countdown = apm_suspend_delay;
520 sc->standby_countdown = apm_standby_delay;
523 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
528 apm_lastreq_notify();
534 struct apm_softc *sc = &apm_softc;
539 if (sc->suspending == 0)
543 if (sc->initialized) {
544 apm_execute_hook(hook[APM_HOOK_RESUME]);
545 DEVICE_RESUME(root_bus);
550 /* get power status per battery */
552 apm_get_pwstatus(apm_pwstatus_t app)
554 struct apm_softc *sc = &apm_softc;
556 if (app->ap_device != PMDV_ALLDEV &&
557 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
560 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
561 sc->bios.r.ebx = app->ap_device;
563 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
568 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
569 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
570 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
571 app->ap_batt_life = sc->bios.r.ecx & 0xff;
572 sc->bios.r.edx &= 0xffff;
573 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
574 app->ap_batt_time = -1;
575 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
576 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
577 else /* Time is in seconds */
578 app->ap_batt_time = sc->bios.r.edx;
584 /* get APM information */
586 apm_get_info(apm_info_t aip)
588 struct apm_softc *sc = &apm_softc;
589 struct apm_pwstatus aps;
591 bzero(&aps, sizeof(aps));
592 aps.ap_device = PMDV_ALLDEV;
593 if (apm_get_pwstatus(&aps))
596 aip->ai_infoversion = 1;
597 aip->ai_acline = aps.ap_acline;
598 aip->ai_batt_stat = aps.ap_batt_stat;
599 aip->ai_batt_life = aps.ap_batt_life;
600 aip->ai_batt_time = aps.ap_batt_time;
601 aip->ai_major = (u_int)sc->majorversion;
602 aip->ai_minor = (u_int)sc->minorversion;
603 aip->ai_status = (u_int)sc->active;
605 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
609 if (apm_bioscall()) {
610 aip->ai_batteries = -1; /* Unknown */
611 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
613 aip->ai_batteries = sc->bios.r.ebx & 0xff;
614 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
617 bzero(aip->ai_spare, sizeof aip->ai_spare);
623 /* inform APM BIOS that CPU is idle */
627 struct apm_softc *sc = &apm_softc;
631 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
632 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
633 (void) apm_bioscall();
636 * Some APM implementation halts CPU in BIOS, whenever
637 * "CPU-idle" function are invoked, but swtch() of
638 * FreeBSD halts CPU, therefore, CPU is halted twice
639 * in the sched loop. It makes the interrupt latency
640 * terribly long and be able to cause a serious problem
641 * in interrupt processing. We prevent it by removing
642 * "hlt" operation from swtch() and managed it under
645 if (!sc->active || sc->always_halt_cpu)
646 __asm("hlt"); /* wait for interrupt */
649 /* inform APM BIOS that CPU is busy */
653 struct apm_softc *sc = &apm_softc;
656 * The APM specification says this is only necessary if your BIOS
657 * slows down the processor in the idle task, otherwise it's not
660 if (sc->slow_idle_cpu && sc->active) {
662 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
663 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
670 * APM timeout routine:
672 * This routine is automatically called by timer once per second.
676 apm_timeout(void *dummy)
678 struct apm_softc *sc = &apm_softc;
681 apm_lastreq_notify();
683 if (sc->standbys && sc->standby_countdown-- <= 0)
686 if (sc->suspends && sc->suspend_countdown-- <= 0)
693 /* Run slightly more oftan than 1 Hz */
694 apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1 );
697 /* enable APM BIOS */
699 apm_event_enable(void)
701 struct apm_softc *sc = &apm_softc;
703 APM_DPRINT("called apm_event_enable()\n");
704 if (sc->initialized) {
710 /* disable APM BIOS */
712 apm_event_disable(void)
714 struct apm_softc *sc = &apm_softc;
716 APM_DPRINT("called apm_event_disable()\n");
717 if (sc->initialized) {
718 untimeout(apm_timeout, NULL, apm_timeout_ch);
723 /* halt CPU in scheduling loop */
727 struct apm_softc *sc = &apm_softc;
730 sc->always_halt_cpu = 1;
733 /* don't halt CPU in scheduling loop */
735 apm_not_halt_cpu(void)
737 struct apm_softc *sc = &apm_softc;
740 sc->always_halt_cpu = 0;
743 /* device driver definitions */
746 * Create "connection point"
749 apm_identify(driver_t *driver, device_t parent)
753 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
755 panic("apm_identify");
762 apm_probe(device_t dev)
764 #define APM_KERNBASE KERNBASE
765 struct vm86frame vmf;
766 struct apm_softc *sc = &apm_softc;
769 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
773 device_set_desc(dev, "APM BIOS");
775 if ( device_get_unit(dev) > 0 ) {
776 printf("apm: Only one APM driver supported.\n");
780 if (resource_int_value("apm", 0, "flags", &flags) != 0)
783 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
784 bzero(&apm_softc, sizeof(apm_softc));
785 vmf.vmf_ah = APM_BIOS;
786 vmf.vmf_al = APM_INSTCHECK;
788 if (vm86_intcall(APM_INT, &vmf))
789 return ENXIO; /* APM not found */
790 if (vmf.vmf_bx != 0x504d) {
791 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
794 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
795 printf("apm: protected mode connections are not supported\n");
799 apm_version = vmf.vmf_ax;
800 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
801 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
802 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
804 vmf.vmf_ah = APM_BIOS;
805 vmf.vmf_al = APM_DISCONNECT;
807 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
809 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
810 vmf.vmf_ah = APM_BIOS;
811 vmf.vmf_al = APM_PROT32CONNECT;
813 if (vm86_intcall(APM_INT, &vmf)) {
814 printf("apm: 32-bit connection error.\n");
817 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
818 sc->bios.seg.code32.limit = 0xffff;
819 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
820 sc->bios.seg.code16.limit = 0xffff;
821 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
822 sc->bios.seg.data.limit = 0xffff;
823 sc->bios.entry = vmf.vmf_ebx;
824 sc->connectmode = APM_PROT32CONNECT;
826 /* use 16-bit connection */
827 vmf.vmf_ah = APM_BIOS;
828 vmf.vmf_al = APM_PROT16CONNECT;
830 if (vm86_intcall(APM_INT, &vmf)) {
831 printf("apm: 16-bit connection error.\n");
834 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
835 sc->bios.seg.code16.limit = 0xffff;
836 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
837 sc->bios.seg.data.limit = 0xffff;
838 sc->bios.entry = vmf.vmf_bx;
839 sc->connectmode = APM_PROT16CONNECT;
846 * return 0 if the user will notice and handle the event,
847 * return 1 if the kernel driver should do so.
850 apm_record_event(struct apm_softc *sc, u_int event_type)
852 struct apm_event_info *evp;
854 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
855 return 1; /* no user waiting */
856 if (sc->event_count == APM_NEVENTS)
857 return 1; /* overflow */
858 if (sc->event_filter[event_type] == 0)
859 return 1; /* not registered */
860 evp = &sc->event_list[sc->event_ptr];
863 sc->event_ptr %= APM_NEVENTS;
864 evp->type = event_type;
865 evp->index = ++apm_evindex;
866 selwakeup(&sc->sc_rsel);
867 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
870 /* Process APM event */
872 apm_processevent(void)
875 struct apm_softc *sc = &apm_softc;
877 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
878 APM_DPRINT("Received APM Event: " #symbol "\n");
881 apm_event = apm_getevent();
883 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
884 if (apm_op_inprog == 0) {
886 if (apm_record_event(sc, apm_event)) {
887 apm_suspend(PMST_STANDBY);
891 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
892 if (apm_op_inprog == 0) {
894 if (apm_record_event(sc, apm_event)) {
895 apm_suspend(PMST_STANDBY);
899 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
900 apm_lastreq_notify();
901 if (apm_op_inprog == 0) {
903 if (apm_record_event(sc, apm_event)) {
907 return; /* XXX skip the rest */
908 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
909 apm_lastreq_notify();
910 if (apm_op_inprog == 0) {
912 if (apm_record_event(sc, apm_event)) {
916 return; /* XXX skip the rest */
917 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
920 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
921 apm_record_event(sc, apm_event);
924 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
925 apm_record_event(sc, apm_event);
928 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
929 apm_record_event(sc, apm_event);
931 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
932 if (apm_record_event(sc, apm_event)) {
934 apm_suspend(PMST_SUSPEND);
937 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
938 apm_record_event(sc, apm_event);
940 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
941 apm_record_event(sc, apm_event);
942 inittodr(0); /* adjust time to RTC */
944 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
945 apm_record_event(sc, apm_event);
950 printf("Unknown Original APM Event 0x%x\n", apm_event);
953 } while (apm_event != PMEV_NOEVENT);
959 * Initialize APM driver
963 apm_attach(device_t dev)
965 struct apm_softc *sc = &apm_softc;
969 if (resource_int_value("apm", 0, "flags", &flags) != 0)
973 statclock_disable = 1;
977 /* Must be externally enabled */
980 /* Always call HLT in idle loop */
981 sc->always_halt_cpu = 1;
983 getenv_int("debug.apm_debug", &apm_debug);
985 /* print bootstrap messages */
986 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
987 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
988 sc->bios.seg.code16.base, sc->bios.seg.data.base);
989 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
990 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
991 is_enabled(!sc->disabled));
992 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
993 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
996 * In one test, apm bios version was 1.02; an attempt to register
997 * a 1.04 driver resulted in a 1.00 connection! Registering a
998 * 1.02 driver resulted in a 1.02 connection.
1000 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1001 for (; drv_version > 0x100; drv_version--)
1002 if (apm_driver_version(drv_version) == 0)
1004 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1005 ((drv_version & 0x000f) >> 0);
1006 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1007 ((apm_version & 0x0f00) >> 8);
1009 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1011 if (sc->intversion >= INTVERSION(1, 1))
1012 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1013 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1014 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1015 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1016 sc->majorversion, sc->minorversion);
1019 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1020 /* enable power management */
1022 if (apm_enable_disable_pm(1)) {
1023 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1024 (sc->bios.r.eax >> 8) & 0xff);
1028 /* engage power managment (APM 1.1 or later) */
1029 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1030 if (apm_engage_disengage_pm(1)) {
1031 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1032 (sc->bios.r.eax >> 8) & 0xff);
1033 APM_DPRINT(" (Docked or using external power?).\n");
1037 /* Power the system off using APM */
1038 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1041 sc->initialized = 1;
1044 make_dev(&apm_cdevsw, 0, 0, 5, 0660, "apm");
1045 make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl");
1050 apmopen(dev_t dev, int flag, int fmt, struct proc *p)
1052 struct apm_softc *sc = &apm_softc;
1053 int ctl = APMDEV(dev);
1055 if (!sc->initialized)
1060 if (!(flag & FWRITE))
1062 if (sc->sc_flags & SCFLAG_OCTL)
1064 sc->sc_flags |= SCFLAG_OCTL;
1065 bzero(sc->event_filter, sizeof sc->event_filter);
1068 sc->sc_flags |= SCFLAG_ONORMAL;
1078 apmclose(dev_t dev, int flag, int fmt, struct proc *p)
1080 struct apm_softc *sc = &apm_softc;
1081 int ctl = APMDEV(dev);
1085 apm_lastreq_rejected();
1086 sc->sc_flags &= ~SCFLAG_OCTL;
1087 bzero(sc->event_filter, sizeof sc->event_filter);
1090 sc->sc_flags &= ~SCFLAG_ONORMAL;
1093 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1094 sc->event_count = 0;
1101 apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1103 struct apm_softc *sc = &apm_softc;
1104 struct apm_bios_arg *args;
1109 if (!sc->initialized)
1111 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1114 if (!(flag & FWRITE))
1117 apm_suspend(PMST_SUSPEND);
1123 if (!(flag & FWRITE))
1126 apm_suspend(PMST_STANDBY);
1131 case APMIO_GETINFO_OLD:
1133 struct apm_info info;
1134 apm_info_old_t aiop;
1136 if (apm_get_info(&info))
1138 aiop = (apm_info_old_t)addr;
1139 aiop->ai_major = info.ai_major;
1140 aiop->ai_minor = info.ai_minor;
1141 aiop->ai_acline = info.ai_acline;
1142 aiop->ai_batt_stat = info.ai_batt_stat;
1143 aiop->ai_batt_life = info.ai_batt_life;
1144 aiop->ai_status = info.ai_status;
1148 if (apm_get_info((apm_info_t)addr))
1151 case APMIO_GETPWSTATUS:
1152 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1156 if (!(flag & FWRITE))
1161 if (!(flag & FWRITE))
1163 apm_event_disable();
1166 if (!(flag & FWRITE))
1170 case APMIO_NOTHALTCPU:
1171 if (!(flag & FWRITE))
1176 if (!(flag & FWRITE))
1178 newstate = *(int *)addr;
1179 if (apm_display(newstate))
1183 if (!(flag & FWRITE))
1185 /* XXX compatibility with the old interface */
1186 args = (struct apm_bios_arg *)addr;
1187 sc->bios.r.eax = args->eax;
1188 sc->bios.r.ebx = args->ebx;
1189 sc->bios.r.ecx = args->ecx;
1190 sc->bios.r.edx = args->edx;
1191 sc->bios.r.esi = args->esi;
1192 sc->bios.r.edi = args->edi;
1193 if ((ret = apm_bioscall())) {
1195 * Return code 1 means bios call was unsuccessful.
1196 * Error code is stored in %ah.
1197 * Return code -1 means bios call was unsupported
1198 * in the APM BIOS version.
1205 * Return code 0 means bios call was successful.
1206 * We need only %al and can discard %ah.
1208 sc->bios.r.eax &= 0xff;
1210 args->eax = sc->bios.r.eax;
1211 args->ebx = sc->bios.r.ebx;
1212 args->ecx = sc->bios.r.ecx;
1213 args->edx = sc->bios.r.edx;
1214 args->esi = sc->bios.r.esi;
1215 args->edi = sc->bios.r.edi;
1222 /* for /dev/apmctl */
1223 if (APMDEV(dev) == APMDEV_CTL) {
1224 struct apm_event_info *evp;
1229 case APMIO_NEXTEVENT:
1230 if (!sc->event_count) {
1233 evp = (struct apm_event_info *)addr;
1234 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1236 *evp = sc->event_list[i];
1240 case APMIO_REJECTLASTREQ:
1241 if (apm_lastreq_rejected()) {
1255 apmwrite(dev_t dev, struct uio *uio, int ioflag)
1257 struct apm_softc *sc = &apm_softc;
1262 if (APMDEV(dev) != APMDEV_CTL)
1264 if (uio->uio_resid != sizeof(u_int))
1267 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1270 if (event_type < 0 || event_type >= APM_NPMEV)
1273 if (sc->event_filter[event_type] == 0) {
1278 sc->event_filter[event_type] = enabled;
1279 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1281 return uio->uio_resid;
1285 apmpoll(dev_t dev, int events, struct proc *p)
1287 struct apm_softc *sc = &apm_softc;
1290 if (events & (POLLIN | POLLRDNORM)) {
1291 if (sc->event_count) {
1292 revents |= events & (POLLIN | POLLRDNORM);
1294 selrecord(p, &sc->sc_rsel);
1301 static device_method_t apm_methods[] = {
1302 /* Device interface */
1303 DEVMETHOD(device_identify, apm_identify),
1304 DEVMETHOD(device_probe, apm_probe),
1305 DEVMETHOD(device_attach, apm_attach),
1310 static driver_t apm_driver = {
1313 1, /* no softc (XXX) */
1316 static devclass_t apm_devclass;
1318 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);