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)
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
22 #include <sys/param.h>
23 #include <sys/systm.h>
26 #include <sys/condvar.h>
27 #include <sys/eventhandler.h>
28 #include <sys/fcntl.h>
29 #include <sys/kernel.h>
30 #include <sys/kthread.h>
32 #include <sys/module.h>
33 #include <sys/mutex.h>
35 #include <sys/power.h>
36 #include <sys/reboot.h>
37 #include <sys/selinfo.h>
38 #include <sys/signalvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/syslog.h>
44 #include <machine/apm_bios.h>
45 #include <machine/clock.h>
46 #include <machine/endian.h>
47 #include <machine/pc/bios.h>
48 #include <machine/cpufunc.h>
49 #include <machine/segments.h>
50 #include <machine/stdarg.h>
51 #include <machine/vm86.h>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
59 #include <vm/vm_param.h>
61 #include <i386/bios/apm.h>
63 /* Used by the apm_saver screen saver module */
64 int apm_display(int newstate);
65 struct apm_softc apm_softc;
67 static void apm_resume(void);
68 static int apm_bioscall(void);
69 static int apm_check_function_supported(u_int version, u_int func);
71 static int apm_pm_func(u_long, void*, ...);
73 static u_long apm_version;
77 #define SCFLAG_ONORMAL 0x0000001
78 #define SCFLAG_OCTL 0x0000002
79 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
81 #define APMDEV(dev) (dev2unit(dev)&0x0f)
82 #define APMDEV_NORMAL 0
86 extern int bios32_apm98(struct bios_regs *, u_int, u_short);
88 /* PC98's SMM definition */
89 #define APM_NECSMM_PORT 0x6b8e
90 #define APM_NECSMM_PORTSZ 1
91 #define APM_NECSMM_EN 0x10
92 static __inline void apm_enable_smm(struct apm_softc *);
93 static __inline void apm_disable_smm(struct apm_softc *);
95 u_int32_t apm_necsmm_mask;
98 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
100 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
102 /* Map version number to integer (keeps ordering of version numbers) */
103 #define INTVERSION(major, minor) ((major)*100 + (minor))
105 static d_open_t apmopen;
106 static d_close_t apmclose;
107 static d_write_t apmwrite;
108 static d_ioctl_t apmioctl;
109 static d_poll_t apmpoll;
111 static struct cdevsw apm_cdevsw = {
112 .d_version = D_VERSION,
113 .d_flags = D_NEEDGIANT,
122 static int apm_suspend_delay = 1;
123 static int apm_standby_delay = 1;
124 static int apm_swab_batt_minutes = 0;
125 static int apm_debug = 0;
127 #define APM_DPRINT(args...) do { \
133 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
134 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
135 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
137 TUNABLE_INT("machdep.apm_swab_batt_minutes", &apm_swab_batt_minutes);
138 SYSCTL_INT(_machdep, OID_AUTO, apm_swab_batt_minutes, CTLFLAG_RW,
139 &apm_swab_batt_minutes, 0, "Byte swap battery time value.");
144 struct apm_softc *sc;
146 bus_space_tag_t iot = sc->sc_iot;
147 bus_space_handle_t ioh = sc->sc_ioh;
148 if (apm_necsmm_addr != 0)
149 bus_space_write_1(iot, ioh, 0,
150 (bus_space_read_1(iot, ioh, 0) | ~apm_necsmm_mask));
155 struct apm_softc *sc;
157 bus_space_tag_t iot = sc->sc_iot;
158 bus_space_handle_t ioh = sc->sc_ioh;
159 if (apm_necsmm_addr != 0)
160 bus_space_write_1(iot, ioh, 0,
161 (bus_space_read_1(iot, ioh, 0) & apm_necsmm_mask));
166 * return 0 if the function successfull,
167 * return 1 if the function unsuccessfull,
168 * return -1 if the function unsupported.
173 struct apm_softc *sc = &apm_softc;
175 u_int apm_func = sc->bios.r.eax & 0xff;
177 if (!apm_check_function_supported(sc->intversion, apm_func)) {
178 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
179 apm_func, sc->majorversion, sc->minorversion);
185 set_bios_selectors(&sc->bios.seg, BIOSCODE_FLAG | BIOSDATA_FLAG);
186 if (bios32_apm98(&sc->bios.r, sc->bios.entry,
187 GSEL(GBIOSCODE32_SEL, SEL_KPL)) != 0)
190 if (sc->connectmode == APM_PROT32CONNECT) {
191 set_bios_selectors(&sc->bios.seg,
192 BIOSCODE_FLAG | BIOSDATA_FLAG);
193 errno = bios32(&sc->bios.r,
194 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
196 errno = bios16(&sc->bios, NULL);
203 /* check whether APM function is supported (1) or not (0). */
205 apm_check_function_supported(u_int version, u_int func)
207 /* except driver version */
208 if (func == APM_DRVVERSION) {
212 if (func == APM_GETPWSTATUS) {
218 case INTVERSION(1, 0):
219 if (func > APM_GETPMEVENT) {
220 return (0); /* not supported */
223 case INTVERSION(1, 1):
224 if (func > APM_ENGAGEDISENGAGEPM &&
225 func < APM_OEMFUNC) {
226 return (0); /* not supported */
229 case INTVERSION(1, 2):
233 return (1); /* supported */
236 /* enable/disable power management */
238 apm_enable_disable_pm(int enable)
240 struct apm_softc *sc = &apm_softc;
242 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
244 if (sc->intversion >= INTVERSION(1, 1))
245 sc->bios.r.ebx = PMDV_ALLDEV;
247 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
248 sc->bios.r.ecx = enable;
250 return (apm_bioscall());
253 /* register driver version (APM 1.1 or later) */
255 apm_driver_version(int version)
257 struct apm_softc *sc = &apm_softc;
259 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
260 sc->bios.r.ebx = 0x0;
261 sc->bios.r.ecx = version;
264 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
267 /* Some old BIOSes don't return the connection version in %ax. */
268 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
274 /* engage/disengage power management (APM 1.1 or later) */
276 apm_engage_disengage_pm(int engage)
278 struct apm_softc *sc = &apm_softc;
280 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
281 sc->bios.r.ebx = PMDV_ALLDEV;
282 sc->bios.r.ecx = engage;
284 return (apm_bioscall());
291 struct apm_softc *sc = &apm_softc;
293 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
299 return (PMEV_NOEVENT);
300 return (sc->bios.r.ebx & 0xffff);
303 /* suspend entire system */
305 apm_suspend_system(int state)
307 struct apm_softc *sc = &apm_softc;
309 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
310 sc->bios.r.ebx = PMDV_ALLDEV;
311 sc->bios.r.ecx = state;
317 if (apm_bioscall()) {
318 printf("Entire system suspend failure: errcode = %d\n",
319 0xff & (sc->bios.r.eax >> 8));
328 /* Display control */
330 * Experimental implementation: My laptop machine can't handle this function
331 * If your laptop can control the display via APM, please inform me.
332 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
335 apm_display(int newstate)
337 struct apm_softc *sc = &apm_softc;
339 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
340 sc->bios.r.ebx = PMDV_DISP0;
341 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
343 if (apm_bioscall() == 0) {
347 /* If failed, then try to blank all display devices instead. */
348 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
349 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
350 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
352 if (apm_bioscall() == 0) {
355 printf("Display off failure: errcode = %d\n",
356 0xff & (sc->bios.r.eax >> 8));
361 * Turn off the entire system.
364 apm_power_off(void *junk, int howto)
366 struct apm_softc *sc = &apm_softc;
368 /* Not halting powering off, or not active */
369 if (!(howto & RB_POWEROFF) || !apm_softc.active)
371 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
372 sc->bios.r.ebx = PMDV_ALLDEV;
373 sc->bios.r.ecx = PMST_OFF;
375 (void) apm_bioscall();
378 /* APM Battery low handler */
380 apm_battery_low(void)
382 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
385 /* APM hook manager */
386 static struct apmhook *
387 apm_add_hook(struct apmhook **list, struct apmhook *ah)
390 struct apmhook *p, *prev;
392 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
396 panic("illegal apm_hook!");
398 for (p = *list; p != NULL; prev = p, p = p->ah_next)
399 if (p->ah_order > ah->ah_order)
406 ah->ah_next = prev->ah_next;
414 apm_del_hook(struct apmhook **list, struct apmhook *ah)
417 struct apmhook *p, *prev;
421 for (p = *list; p != NULL; prev = p, p = p->ah_next)
424 panic("Tried to delete unregistered apm_hook.");
428 prev->ah_next = p->ah_next;
436 /* APM driver calls some functions automatically */
438 apm_execute_hook(struct apmhook *list)
442 for (p = list; p != NULL; p = p->ah_next) {
443 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
444 if ((*(p->ah_fun))(p->ah_arg))
445 printf("Warning: APM hook \"%s\" failed", p->ah_name);
450 /* establish an apm hook */
452 apm_hook_establish(int apmh, struct apmhook *ah)
454 if (apmh < 0 || apmh >= NAPM_HOOK)
457 return apm_add_hook(&hook[apmh], ah);
460 /* disestablish an apm hook */
462 apm_hook_disestablish(int apmh, struct apmhook *ah)
464 if (apmh < 0 || apmh >= NAPM_HOOK)
467 apm_del_hook(&hook[apmh], ah);
470 static int apm_record_event(struct apm_softc *, u_int);
471 static void apm_processevent(void);
473 static u_int apm_op_inprog = 0;
478 struct apm_softc *sc = &apm_softc;
481 if (sc == NULL || sc->initialized == 0)
485 sc->suspends = sc->suspend_countdown = 0;
488 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since
489 * non-MPSAFE drivers need this.
492 error = DEVICE_SUSPEND(root_bus);
498 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
499 if (apm_suspend_system(PMST_SUSPEND) == 0) {
503 /* Failure, 'resume' the system again */
504 apm_execute_hook(hook[APM_HOOK_RESUME]);
505 DEVICE_RESUME(root_bus);
514 struct apm_softc *sc = &apm_softc;
516 if (sc == NULL || sc->initialized == 0)
520 sc->standbys = sc->standby_countdown = 0;
523 * As far as standby, we don't need to execute
524 * all of suspend hooks.
526 if (apm_suspend_system(PMST_STANDBY) == 0)
532 apm_lastreq_notify(void)
534 struct apm_softc *sc = &apm_softc;
536 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
537 sc->bios.r.ebx = PMDV_ALLDEV;
538 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
544 apm_lastreq_rejected(void)
546 struct apm_softc *sc = &apm_softc;
548 if (apm_op_inprog == 0) {
549 return 1; /* no operation in progress */
552 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
553 sc->bios.r.ebx = PMDV_ALLDEV;
554 sc->bios.r.ecx = PMST_LASTREQREJECT;
557 if (apm_bioscall()) {
558 APM_DPRINT("apm_lastreq_rejected: failed\n");
566 * Public interface to the suspend/resume:
568 * Execute suspend and resume hook before and after sleep, respectively.
573 apm_suspend(int state)
575 struct apm_softc *sc = &apm_softc;
577 if (sc == NULL || sc->initialized == 0)
585 sc->suspend_countdown = apm_suspend_delay;
591 sc->standby_countdown = apm_standby_delay;
594 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
599 apm_lastreq_notify();
605 struct apm_softc *sc = &apm_softc;
607 if (sc == NULL || sc->initialized == 0 || sc->suspending == 0)
611 apm_execute_hook(hook[APM_HOOK_RESUME]);
613 DEVICE_RESUME(root_bus);
619 /* get power status per battery */
621 apm_get_pwstatus(apm_pwstatus_t app)
623 struct apm_softc *sc = &apm_softc;
625 if (app->ap_device != PMDV_ALLDEV &&
626 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
629 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
630 sc->bios.r.ebx = app->ap_device;
632 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
637 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
638 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
639 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
640 app->ap_batt_life = sc->bios.r.ecx & 0xff;
641 sc->bios.r.edx &= 0xffff;
642 if (apm_swab_batt_minutes)
643 sc->bios.r.edx = __bswap16(sc->bios.r.edx) | 0x8000;
644 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
645 app->ap_batt_time = -1;
646 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
647 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
648 else /* Time is in seconds */
649 app->ap_batt_time = sc->bios.r.edx;
655 /* get APM information */
657 apm_get_info(apm_info_t aip)
659 struct apm_softc *sc = &apm_softc;
660 struct apm_pwstatus aps;
662 bzero(&aps, sizeof(aps));
663 aps.ap_device = PMDV_ALLDEV;
664 if (apm_get_pwstatus(&aps))
667 aip->ai_infoversion = 1;
668 aip->ai_acline = aps.ap_acline;
669 aip->ai_batt_stat = aps.ap_batt_stat;
670 aip->ai_batt_life = aps.ap_batt_life;
671 aip->ai_batt_time = aps.ap_batt_time;
672 aip->ai_major = (u_int)sc->majorversion;
673 aip->ai_minor = (u_int)sc->minorversion;
674 aip->ai_status = (u_int)sc->active;
676 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
680 if (apm_bioscall()) {
681 aip->ai_batteries = 0xffffffff; /* Unknown */
682 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
684 aip->ai_batteries = sc->bios.r.ebx & 0xff;
685 aip->ai_capabilities = sc->bios.r.ecx & 0xff;
688 bzero(aip->ai_spare, sizeof aip->ai_spare);
694 /* inform APM BIOS that CPU is idle */
698 struct apm_softc *sc = &apm_softc;
702 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
703 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
704 (void) apm_bioscall();
707 * Some APM implementation halts CPU in BIOS, whenever
708 * "CPU-idle" function are invoked, but swtch() of
709 * FreeBSD halts CPU, therefore, CPU is halted twice
710 * in the sched loop. It makes the interrupt latency
711 * terribly long and be able to cause a serious problem
712 * in interrupt processing. We prevent it by removing
713 * "hlt" operation from swtch() and managed it under
716 if (!sc->active || sc->always_halt_cpu)
717 halt(); /* wait for interrupt */
720 /* inform APM BIOS that CPU is busy */
724 struct apm_softc *sc = &apm_softc;
727 * The APM specification says this is only necessary if your BIOS
728 * slows down the processor in the idle task, otherwise it's not
731 if (sc->slow_idle_cpu && sc->active) {
733 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
734 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
743 * This routine wakes up from time to time to deal with delaying the
744 * suspend of the system, or other events.
747 apm_event_thread(void *arg)
749 struct apm_softc *sc = &apm_softc;
754 apm_lastreq_notify();
755 if (sc->standbys && sc->standby_countdown-- <= 0)
757 if (sc->suspends && sc->suspend_countdown-- <= 0)
762 cv_timedwait(&sc->cv, &sc->mtx, 10 * hz / 9);
763 mtx_unlock(&sc->mtx);
769 /* enable APM BIOS */
771 apm_event_enable(void)
773 struct apm_softc *sc = &apm_softc;
775 APM_DPRINT("called apm_event_enable()\n");
777 if (sc == NULL || sc->initialized == 0)
780 /* Start the thread */
782 if (kproc_create(apm_event_thread, sc, &sc->event_thread, 0, 0,
784 panic("Cannot create apm worker thread");
789 /* disable APM BIOS */
791 apm_event_disable(void)
793 struct apm_softc *sc = &apm_softc;
795 APM_DPRINT("called apm_event_disable()\n");
797 if (sc == NULL || sc->initialized == 0)
802 while (sc->running) {
803 cv_broadcast(&sc->cv);
804 msleep(sc->event_thread, &sc->mtx, PWAIT, "apmdie", 0);
806 mtx_unlock(&sc->mtx);
807 sc->event_thread = NULL;
811 /* halt CPU in scheduling loop */
815 struct apm_softc *sc = &apm_softc;
817 if (sc == NULL || sc->initialized == 0)
820 sc->always_halt_cpu = 1;
825 /* don't halt CPU in scheduling loop */
827 apm_not_halt_cpu(void)
829 struct apm_softc *sc = &apm_softc;
831 if (sc == NULL || sc->initialized == 0)
834 sc->always_halt_cpu = 0;
839 /* device driver definitions */
846 apm_modevent(struct module *mod, int event, void *junk)
855 if (!cold && power_pm_get_type() == POWER_PM_TYPE_APM)
866 * Create "connection point"
869 apm_identify(driver_t *driver, device_t parent)
874 printf("Don't load this driver from userland!!\n");
878 if (resource_disabled("apm", 0))
881 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
883 panic("apm_identify");
890 apm_probe(device_t dev)
892 #define APM_KERNBASE KERNBASE
893 struct vm86frame vmf;
894 struct apm_softc *sc = &apm_softc;
899 device_set_desc(dev, "APM BIOS");
900 if (device_get_unit(dev) > 0) {
901 printf("apm: Only one APM driver supported.\n");
905 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
906 power_pm_get_type() != POWER_PM_TYPE_APM) {
907 printf("apm: Other PM system enabled.\n");
911 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
912 bzero(&apm_softc, sizeof(apm_softc));
913 vmf.vmf_ah = APM_BIOS;
914 vmf.vmf_al = APM_INSTCHECK;
916 if (vm86_intcall(APM_INT, &vmf))
917 return ENXIO; /* APM not found */
918 if (vmf.vmf_bx != 0x504d) {
919 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
922 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
923 printf("apm: protected mode connections are not supported\n");
927 apm_version = vmf.vmf_ax;
928 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
929 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
930 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
932 vmf.vmf_ah = APM_BIOS;
933 vmf.vmf_al = APM_DISCONNECT;
935 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
938 /* PC98 have bogos APM 32bit BIOS */
939 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) == 0)
942 bus_set_resource(dev, SYS_RES_IOPORT, rid,
943 APM_NECSMM_PORT, APM_NECSMM_PORTSZ);
944 sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
945 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
946 if (sc->sc_res == NULL) {
947 printf("apm: cannot open NEC smm device\n");
950 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_res);
952 vmf.vmf_ah = APM_BIOS;
953 vmf.vmf_al = APM_PROT32CONNECT;
955 if (vm86_intcall(APM_INT, &vmf)) {
956 printf("apm: 32-bit connection error.\n");
960 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
961 sc->bios.seg.code32.limit = 0xffff;
962 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
963 sc->bios.seg.code16.limit = 0xffff;
964 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
965 sc->bios.seg.data.limit = 0xffff;
966 sc->bios.entry = vmf.vmf_ebx;
967 sc->connectmode = APM_PROT32CONNECT;
969 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
970 vmf.vmf_ah = APM_BIOS;
971 vmf.vmf_al = APM_PROT32CONNECT;
973 if (vm86_intcall(APM_INT, &vmf)) {
974 printf("apm: 32-bit connection error.\n");
977 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
978 sc->bios.seg.code32.limit = 0xffff;
979 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
980 sc->bios.seg.code16.limit = 0xffff;
981 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
982 sc->bios.seg.data.limit = 0xffff;
983 sc->bios.entry = vmf.vmf_ebx;
984 sc->connectmode = APM_PROT32CONNECT;
986 /* use 16-bit connection */
987 vmf.vmf_ah = APM_BIOS;
988 vmf.vmf_al = APM_PROT16CONNECT;
990 if (vm86_intcall(APM_INT, &vmf)) {
991 printf("apm: 16-bit connection error.\n");
994 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
995 sc->bios.seg.code16.limit = 0xffff;
996 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
997 sc->bios.seg.data.limit = 0xffff;
998 sc->bios.entry = vmf.vmf_bx;
999 sc->connectmode = APM_PROT16CONNECT;
1007 * return 0 if the user will notice and handle the event,
1008 * return 1 if the kernel driver should do so.
1011 apm_record_event(struct apm_softc *sc, u_int event_type)
1013 struct apm_event_info *evp;
1015 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
1016 return 1; /* no user waiting */
1017 if (sc->event_count == APM_NEVENTS)
1018 return 1; /* overflow */
1019 if (sc->event_filter[event_type] == 0)
1020 return 1; /* not registered */
1021 evp = &sc->event_list[sc->event_ptr];
1024 sc->event_ptr %= APM_NEVENTS;
1025 evp->type = event_type;
1026 evp->index = ++apm_evindex;
1027 selwakeuppri(&sc->sc_rsel, PZERO);
1028 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
1033 apm_power_profile(struct apm_softc *sc)
1036 struct apm_info info;
1037 static int apm_acline = 0;
1039 if (apm_get_info(&info))
1042 if (apm_acline != info.ai_acline) {
1043 apm_acline = info.ai_acline;
1044 state = apm_acline ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY;
1045 power_profile_set_state(state);
1049 /* Process APM event */
1051 apm_processevent(void)
1054 struct apm_softc *sc = &apm_softc;
1056 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
1057 APM_DPRINT("Received APM Event: " #symbol "\n");
1060 apm_event = apm_getevent();
1061 switch (apm_event) {
1062 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
1063 if (apm_op_inprog == 0) {
1065 if (apm_record_event(sc, apm_event)) {
1066 apm_suspend(PMST_STANDBY);
1070 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
1071 if (apm_op_inprog == 0) {
1073 if (apm_record_event(sc, apm_event)) {
1074 apm_suspend(PMST_STANDBY);
1078 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
1079 apm_lastreq_notify();
1080 if (apm_op_inprog == 0) {
1082 if (apm_record_event(sc, apm_event)) {
1086 return; /* XXX skip the rest */
1087 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
1088 apm_lastreq_notify();
1089 if (apm_op_inprog == 0) {
1091 if (apm_record_event(sc, apm_event)) {
1095 return; /* XXX skip the rest */
1096 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
1099 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
1100 apm_record_event(sc, apm_event);
1103 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
1104 apm_record_event(sc, apm_event);
1107 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
1108 apm_record_event(sc, apm_event);
1110 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
1111 if (apm_record_event(sc, apm_event)) {
1113 apm_suspend(PMST_SUSPEND);
1116 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
1117 apm_record_event(sc, apm_event);
1118 apm_power_profile(sc);
1120 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1121 apm_record_event(sc, apm_event);
1122 inittodr(0); /* adjust time to RTC */
1124 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1125 apm_record_event(sc, apm_event);
1126 apm_power_profile(sc);
1131 printf("Unknown Original APM Event 0x%x\n", apm_event);
1134 } while (apm_event != PMEV_NOEVENT);
1136 apm_disable_smm(sc);
1143 * Initialize APM driver
1147 apm_attach(device_t dev)
1149 struct apm_softc *sc = &apm_softc;
1154 mtx_init(&sc->mtx, device_get_nameunit(dev), "apm", MTX_DEF);
1155 cv_init(&sc->cv, "cbb cv");
1157 if (device_get_flags(dev) & 0x20)
1158 statclock_disable = 1;
1160 sc->initialized = 0;
1162 /* Must be externally enabled */
1165 /* Always call HLT in idle loop */
1166 sc->always_halt_cpu = 1;
1168 getenv_int("debug.apm_debug", &apm_debug);
1170 /* print bootstrap messages */
1171 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1172 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1173 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1174 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1175 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1176 is_enabled(!sc->disabled));
1177 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1178 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1182 sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
1183 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
1184 if (sc->sc_res == NULL)
1185 panic("%s: counldn't map I/O ports", device_get_name(dev));
1186 sc->sc_iot = rman_get_bustag(sc->sc_res);
1187 sc->sc_ioh = rman_get_bushandle(sc->sc_res);
1189 if (apm_version==0x112 || apm_version==0x111 || apm_version==0x110)
1190 apm_necsmm_addr = APM_NECSMM_PORT;
1192 apm_necsmm_addr = 0;
1193 apm_necsmm_mask = ~APM_NECSMM_EN;
1197 * In one test, apm bios version was 1.02; an attempt to register
1198 * a 1.04 driver resulted in a 1.00 connection! Registering a
1199 * 1.02 driver resulted in a 1.02 connection.
1201 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1202 for (; drv_version > 0x100; drv_version--)
1203 if (apm_driver_version(drv_version) == 0)
1205 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1206 ((drv_version & 0x000f) >> 0);
1207 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1208 ((apm_version & 0x0f00) >> 8);
1210 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1212 if (sc->intversion >= INTVERSION(1, 1))
1213 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1214 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1215 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1216 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1217 sc->majorversion, sc->minorversion);
1220 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1221 /* enable power management */
1223 if (apm_enable_disable_pm(1)) {
1224 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1225 (sc->bios.r.eax >> 8) & 0xff);
1229 /* engage power managment (APM 1.1 or later) */
1230 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1231 if (apm_engage_disengage_pm(1)) {
1232 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1233 (sc->bios.r.eax >> 8) & 0xff);
1234 APM_DPRINT(" (Docked or using external power?).\n");
1238 /* Power the system off using APM */
1239 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1242 /* Register APM again to pass the correct argument of pm_func. */
1243 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc);
1245 sc->initialized = 1;
1249 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm");
1250 make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl");
1255 apmopen(struct cdev *dev, int flag, int fmt, struct thread *td)
1257 struct apm_softc *sc = &apm_softc;
1258 int ctl = APMDEV(dev);
1260 if (sc == NULL || sc->initialized == 0)
1265 if (!(flag & FWRITE))
1267 if (sc->sc_flags & SCFLAG_OCTL)
1269 sc->sc_flags |= SCFLAG_OCTL;
1270 bzero(sc->event_filter, sizeof sc->event_filter);
1273 sc->sc_flags |= SCFLAG_ONORMAL;
1283 apmclose(struct cdev *dev, int flag, int fmt, struct thread *td)
1285 struct apm_softc *sc = &apm_softc;
1286 int ctl = APMDEV(dev);
1290 apm_lastreq_rejected();
1291 sc->sc_flags &= ~SCFLAG_OCTL;
1292 bzero(sc->event_filter, sizeof sc->event_filter);
1295 sc->sc_flags &= ~SCFLAG_ONORMAL;
1298 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1299 sc->event_count = 0;
1306 apmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1308 struct apm_softc *sc = &apm_softc;
1309 struct apm_bios_arg *args;
1314 if (sc == NULL || sc->initialized == 0)
1317 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1320 if (!(flag & FWRITE))
1323 apm_suspend(PMST_SUSPEND);
1329 if (!(flag & FWRITE))
1332 apm_suspend(PMST_STANDBY);
1337 case APMIO_GETINFO_OLD:
1339 struct apm_info info;
1340 apm_info_old_t aiop;
1342 if (apm_get_info(&info))
1344 aiop = (apm_info_old_t)addr;
1345 aiop->ai_major = info.ai_major;
1346 aiop->ai_minor = info.ai_minor;
1347 aiop->ai_acline = info.ai_acline;
1348 aiop->ai_batt_stat = info.ai_batt_stat;
1349 aiop->ai_batt_life = info.ai_batt_life;
1350 aiop->ai_status = info.ai_status;
1354 if (apm_get_info((apm_info_t)addr))
1357 case APMIO_GETPWSTATUS:
1358 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1362 if (!(flag & FWRITE))
1367 if (!(flag & FWRITE))
1369 apm_event_disable();
1372 if (!(flag & FWRITE))
1376 case APMIO_NOTHALTCPU:
1377 if (!(flag & FWRITE))
1382 if (!(flag & FWRITE))
1384 newstate = *(int *)addr;
1385 if (apm_display(newstate))
1389 if (!(flag & FWRITE))
1391 /* XXX compatibility with the old interface */
1392 args = (struct apm_bios_arg *)addr;
1393 sc->bios.r.eax = args->eax;
1394 sc->bios.r.ebx = args->ebx;
1395 sc->bios.r.ecx = args->ecx;
1396 sc->bios.r.edx = args->edx;
1397 sc->bios.r.esi = args->esi;
1398 sc->bios.r.edi = args->edi;
1399 if ((ret = apm_bioscall())) {
1401 * Return code 1 means bios call was unsuccessful.
1402 * Error code is stored in %ah.
1403 * Return code -1 means bios call was unsupported
1404 * in the APM BIOS version.
1411 * Return code 0 means bios call was successful.
1412 * We need only %al and can discard %ah.
1414 sc->bios.r.eax &= 0xff;
1416 args->eax = sc->bios.r.eax;
1417 args->ebx = sc->bios.r.ebx;
1418 args->ecx = sc->bios.r.ecx;
1419 args->edx = sc->bios.r.edx;
1420 args->esi = sc->bios.r.esi;
1421 args->edi = sc->bios.r.edi;
1428 /* for /dev/apmctl */
1429 if (APMDEV(dev) == APMDEV_CTL) {
1430 struct apm_event_info *evp;
1435 case APMIO_NEXTEVENT:
1436 if (!sc->event_count) {
1439 evp = (struct apm_event_info *)addr;
1440 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1442 *evp = sc->event_list[i];
1446 case APMIO_REJECTLASTREQ:
1447 if (apm_lastreq_rejected()) {
1461 apmwrite(struct cdev *dev, struct uio *uio, int ioflag)
1463 struct apm_softc *sc = &apm_softc;
1468 if (APMDEV(dev) != APMDEV_CTL)
1470 if (uio->uio_resid != sizeof(u_int))
1473 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1476 if (event_type < 0 || event_type >= APM_NPMEV)
1479 if (sc->event_filter[event_type] == 0) {
1484 sc->event_filter[event_type] = enabled;
1485 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1487 return uio->uio_resid;
1491 apmpoll(struct cdev *dev, int events, struct thread *td)
1493 struct apm_softc *sc = &apm_softc;
1496 if (events & (POLLIN | POLLRDNORM)) {
1497 if (sc->event_count) {
1498 revents |= events & (POLLIN | POLLRDNORM);
1500 selrecord(td, &sc->sc_rsel);
1507 static device_method_t apm_methods[] = {
1508 /* Device interface */
1509 DEVMETHOD(device_identify, apm_identify),
1510 DEVMETHOD(device_probe, apm_probe),
1511 DEVMETHOD(device_attach, apm_attach),
1516 static driver_t apm_driver = {
1519 1, /* no softc (XXX) */
1522 static devclass_t apm_devclass;
1524 DRIVER_MODULE(apm, legacy, apm_driver, apm_devclass, apm_modevent, 0);
1525 MODULE_VERSION(apm, 1);
1528 apm_pm_func(u_long cmd, void *arg, ...)
1530 int state, apm_state;
1536 case POWER_CMD_SUSPEND:
1538 state = va_arg(ap, int);
1542 case POWER_SLEEP_STATE_STANDBY:
1543 apm_state = PMST_STANDBY;
1545 case POWER_SLEEP_STATE_SUSPEND:
1546 case POWER_SLEEP_STATE_HIBERNATE:
1547 apm_state = PMST_SUSPEND;
1554 apm_suspend(apm_state);
1567 apm_pm_register(void *arg)
1570 if (!resource_disabled("apm", 0))
1571 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL);
1574 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, 0);