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/selinfo.h>
31 #include <sys/fcntl.h>
33 #include <sys/signalvar.h>
34 #include <sys/sysctl.h>
35 #include <sys/power.h>
36 #include <machine/apm_bios.h>
37 #include <machine/segments.h>
38 #include <machine/clock.h>
39 #include <machine/stdarg.h>
41 #include <vm/vm_param.h>
43 #include <sys/syslog.h>
45 #include <machine/pc/bios.h>
46 #include <machine/vm86.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
53 #include <pc98/apm/apm.h>
55 /* Used by the apm_saver screen saver module */
56 int apm_display(int newstate);
57 struct apm_softc apm_softc;
59 static void apm_resume(void);
60 static int apm_bioscall(void);
61 static int apm_check_function_supported(u_int version, u_int func);
63 static int apm_pm_func(u_long, void*, ...);
65 static u_long apm_version;
69 #define SCFLAG_ONORMAL 0x0000001
70 #define SCFLAG_OCTL 0x0000002
71 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
73 #define APMDEV(dev) (minor(dev)&0x0f)
74 #define APMDEV_NORMAL 0
78 extern int bios32_apm98(struct bios_regs *, u_int, u_short);
80 /* PC98's SMM definition */
81 #define APM_NECSMM_PORT 0x6b8e
82 #define APM_NECSMM_PORTSZ 1
83 #define APM_NECSMM_EN 0x10
84 static __inline void apm_enable_smm(struct apm_softc *);
85 static __inline void apm_disable_smm(struct apm_softc *);
87 u_int32_t apm_necsmm_mask;
90 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
92 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
94 /* Map version number to integer (keeps ordering of version numbers) */
95 #define INTVERSION(major, minor) ((major)*100 + (minor))
97 static struct callout_handle apm_timeout_ch =
98 CALLOUT_HANDLE_INITIALIZER(&apm_timeout_ch);
100 static timeout_t apm_timeout;
101 static d_open_t apmopen;
102 static d_close_t apmclose;
103 static d_write_t apmwrite;
104 static d_ioctl_t apmioctl;
105 static d_poll_t apmpoll;
107 #define CDEV_MAJOR 39
108 static struct cdevsw apm_cdevsw = {
110 /* close */ apmclose,
112 /* write */ apmwrite,
113 /* ioctl */ apmioctl,
116 /* strategy */ nostrategy,
118 /* maj */ CDEV_MAJOR,
124 static int apm_suspend_delay = 1;
125 static int apm_standby_delay = 1;
126 static int apm_debug = 0;
128 #define APM_DPRINT(args...) do { \
134 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
135 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
136 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
141 struct apm_softc *sc;
143 bus_space_tag_t iot = sc->sc_iot;
144 bus_space_handle_t ioh = sc->sc_ioh;
145 if (apm_necsmm_addr != 0)
146 bus_space_write_1(iot, ioh, 0,
147 (bus_space_read_1(iot, ioh, 0) | ~apm_necsmm_mask));
152 struct apm_softc *sc;
154 bus_space_tag_t iot = sc->sc_iot;
155 bus_space_handle_t ioh = sc->sc_ioh;
156 if (apm_necsmm_addr != 0)
157 bus_space_write_1(iot, ioh, 0,
158 (bus_space_read_1(iot, ioh, 0) & apm_necsmm_mask));
163 * return 0 if the function successfull,
164 * return 1 if the function unsuccessfull,
165 * return -1 if the function unsupported.
170 struct apm_softc *sc = &apm_softc;
172 u_int apm_func = sc->bios.r.eax & 0xff;
174 if (!apm_check_function_supported(sc->intversion, apm_func)) {
175 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
176 apm_func, sc->majorversion, sc->minorversion);
182 set_bios_selectors(&sc->bios.seg, BIOSCODE_FLAG | BIOSDATA_FLAG);
183 if (bios32_apm98(&sc->bios.r, sc->bios.entry,
184 GSEL(GBIOSCODE32_SEL, SEL_KPL)) != 0)
187 if (sc->connectmode == APM_PROT32CONNECT) {
188 set_bios_selectors(&sc->bios.seg,
189 BIOSCODE_FLAG | BIOSDATA_FLAG);
190 errno = bios32(&sc->bios.r,
191 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
193 errno = bios16(&sc->bios, NULL);
200 /* check whether APM function is supported (1) or not (0). */
202 apm_check_function_supported(u_int version, u_int func)
204 /* except driver version */
205 if (func == APM_DRVVERSION) {
209 if (func == APM_GETPWSTATUS) {
215 case INTVERSION(1, 0):
216 if (func > APM_GETPMEVENT) {
217 return (0); /* not supported */
220 case INTVERSION(1, 1):
221 if (func > APM_ENGAGEDISENGAGEPM &&
222 func < APM_OEMFUNC) {
223 return (0); /* not supported */
226 case INTVERSION(1, 2):
230 return (1); /* supported */
233 /* enable/disable power management */
235 apm_enable_disable_pm(int enable)
237 struct apm_softc *sc = &apm_softc;
239 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
241 if (sc->intversion >= INTVERSION(1, 1))
242 sc->bios.r.ebx = PMDV_ALLDEV;
244 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
245 sc->bios.r.ecx = enable;
247 return (apm_bioscall());
250 /* register driver version (APM 1.1 or later) */
252 apm_driver_version(int version)
254 struct apm_softc *sc = &apm_softc;
256 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
257 sc->bios.r.ebx = 0x0;
258 sc->bios.r.ecx = version;
261 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
264 /* Some old BIOSes don't return the connection version in %ax. */
265 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
271 /* engage/disengage power management (APM 1.1 or later) */
273 apm_engage_disengage_pm(int engage)
275 struct apm_softc *sc = &apm_softc;
277 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
278 sc->bios.r.ebx = PMDV_ALLDEV;
279 sc->bios.r.ecx = engage;
281 return (apm_bioscall());
288 struct apm_softc *sc = &apm_softc;
290 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
296 return (PMEV_NOEVENT);
297 return (sc->bios.r.ebx & 0xffff);
300 /* suspend entire system */
302 apm_suspend_system(int state)
304 struct apm_softc *sc = &apm_softc;
306 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
307 sc->bios.r.ebx = PMDV_ALLDEV;
308 sc->bios.r.ecx = state;
314 if (apm_bioscall()) {
315 printf("Entire system suspend failure: errcode = %d\n",
316 0xff & (sc->bios.r.eax >> 8));
325 /* Display control */
327 * Experimental implementation: My laptop machine can't handle this function
328 * If your laptop can control the display via APM, please inform me.
329 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
332 apm_display(int newstate)
334 struct apm_softc *sc = &apm_softc;
336 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
337 sc->bios.r.ebx = PMDV_DISP0;
338 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
340 if (apm_bioscall() == 0) {
344 /* If failed, then try to blank all display devices instead. */
345 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
346 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
347 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
349 if (apm_bioscall() == 0) {
352 printf("Display off failure: errcode = %d\n",
353 0xff & (sc->bios.r.eax >> 8));
358 * Turn off the entire system.
361 apm_power_off(void *junk, int howto)
363 struct apm_softc *sc = &apm_softc;
365 /* Not halting powering off, or not active */
366 if (!(howto & RB_POWEROFF) || !apm_softc.active)
368 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
369 sc->bios.r.ebx = PMDV_ALLDEV;
370 sc->bios.r.ecx = PMST_OFF;
372 (void) apm_bioscall();
375 /* APM Battery low handler */
377 apm_battery_low(void)
379 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
382 /* APM hook manager */
383 static struct apmhook *
384 apm_add_hook(struct apmhook **list, struct apmhook *ah)
387 struct apmhook *p, *prev;
389 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
393 panic("illegal apm_hook!");
395 for (p = *list; p != NULL; prev = p, p = p->ah_next)
396 if (p->ah_order > ah->ah_order)
403 ah->ah_next = prev->ah_next;
411 apm_del_hook(struct apmhook **list, struct apmhook *ah)
414 struct apmhook *p, *prev;
418 for (p = *list; p != NULL; prev = p, p = p->ah_next)
421 panic("Tried to delete unregistered apm_hook.");
425 prev->ah_next = p->ah_next;
433 /* APM driver calls some functions automatically */
435 apm_execute_hook(struct apmhook *list)
439 for (p = list; p != NULL; p = p->ah_next) {
440 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
441 if ((*(p->ah_fun))(p->ah_arg))
442 printf("Warning: APM hook \"%s\" failed", p->ah_name);
447 /* establish an apm hook */
449 apm_hook_establish(int apmh, struct apmhook *ah)
451 if (apmh < 0 || apmh >= NAPM_HOOK)
454 return apm_add_hook(&hook[apmh], ah);
457 /* disestablish an apm hook */
459 apm_hook_disestablish(int apmh, struct apmhook *ah)
461 if (apmh < 0 || apmh >= NAPM_HOOK)
464 apm_del_hook(&hook[apmh], ah);
467 static int apm_record_event(struct apm_softc *, u_int);
468 static void apm_processevent(void);
470 static u_int apm_op_inprog = 0;
475 struct apm_softc *sc = &apm_softc;
482 sc->suspends = sc->suspend_countdown = 0;
484 if (sc->initialized) {
485 error = DEVICE_SUSPEND(root_bus);
487 DEVICE_RESUME(root_bus);
489 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
490 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 if (apm_suspend_system(PMST_STANDBY) == 0)
524 apm_lastreq_notify(void)
526 struct apm_softc *sc = &apm_softc;
528 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
529 sc->bios.r.ebx = PMDV_ALLDEV;
530 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
536 apm_lastreq_rejected(void)
538 struct apm_softc *sc = &apm_softc;
540 if (apm_op_inprog == 0) {
541 return 1; /* no operation in progress */
544 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
545 sc->bios.r.ebx = PMDV_ALLDEV;
546 sc->bios.r.ecx = PMST_LASTREQREJECT;
549 if (apm_bioscall()) {
550 APM_DPRINT("apm_lastreq_rejected: failed\n");
558 * Public interface to the suspend/resume:
560 * Execute suspend and resume hook before and after sleep, respectively.
565 apm_suspend(int state)
567 struct apm_softc *sc = &apm_softc;
569 if (!sc->initialized)
577 sc->suspend_countdown = apm_suspend_delay;
583 sc->standby_countdown = apm_standby_delay;
586 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
591 apm_lastreq_notify();
597 struct apm_softc *sc = &apm_softc;
602 if (sc->suspending == 0)
606 if (sc->initialized) {
607 apm_execute_hook(hook[APM_HOOK_RESUME]);
608 DEVICE_RESUME(root_bus);
613 /* get power status per battery */
615 apm_get_pwstatus(apm_pwstatus_t app)
617 struct apm_softc *sc = &apm_softc;
619 if (app->ap_device != PMDV_ALLDEV &&
620 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
623 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
624 sc->bios.r.ebx = app->ap_device;
626 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
631 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
632 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
633 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
634 app->ap_batt_life = sc->bios.r.ecx & 0xff;
635 sc->bios.r.edx &= 0xffff;
636 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
637 app->ap_batt_time = -1;
638 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
639 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
640 else /* Time is in seconds */
641 app->ap_batt_time = sc->bios.r.edx;
647 /* get APM information */
649 apm_get_info(apm_info_t aip)
651 struct apm_softc *sc = &apm_softc;
652 struct apm_pwstatus aps;
654 bzero(&aps, sizeof(aps));
655 aps.ap_device = PMDV_ALLDEV;
656 if (apm_get_pwstatus(&aps))
659 aip->ai_infoversion = 1;
660 aip->ai_acline = aps.ap_acline;
661 aip->ai_batt_stat = aps.ap_batt_stat;
662 aip->ai_batt_life = aps.ap_batt_life;
663 aip->ai_batt_time = aps.ap_batt_time;
664 aip->ai_major = (u_int)sc->majorversion;
665 aip->ai_minor = (u_int)sc->minorversion;
666 aip->ai_status = (u_int)sc->active;
668 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
672 if (apm_bioscall()) {
673 aip->ai_batteries = -1; /* Unknown */
674 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
676 aip->ai_batteries = sc->bios.r.ebx & 0xff;
677 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
680 bzero(aip->ai_spare, sizeof aip->ai_spare);
686 /* inform APM BIOS that CPU is idle */
690 struct apm_softc *sc = &apm_softc;
694 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
695 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
696 (void) apm_bioscall();
699 * Some APM implementation halts CPU in BIOS, whenever
700 * "CPU-idle" function are invoked, but swtch() of
701 * FreeBSD halts CPU, therefore, CPU is halted twice
702 * in the sched loop. It makes the interrupt latency
703 * terribly long and be able to cause a serious problem
704 * in interrupt processing. We prevent it by removing
705 * "hlt" operation from swtch() and managed it under
708 if (!sc->active || sc->always_halt_cpu)
709 __asm("hlt"); /* wait for interrupt */
712 /* inform APM BIOS that CPU is busy */
716 struct apm_softc *sc = &apm_softc;
719 * The APM specification says this is only necessary if your BIOS
720 * slows down the processor in the idle task, otherwise it's not
723 if (sc->slow_idle_cpu && sc->active) {
725 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
726 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
733 * APM timeout routine:
735 * This routine is automatically called by timer once per second.
739 apm_timeout(void *dummy)
741 struct apm_softc *sc = &apm_softc;
744 apm_lastreq_notify();
746 if (sc->standbys && sc->standby_countdown-- <= 0)
749 if (sc->suspends && sc->suspend_countdown-- <= 0)
756 /* Run slightly more oftan than 1 Hz */
757 apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1);
760 /* enable APM BIOS */
762 apm_event_enable(void)
764 struct apm_softc *sc = &apm_softc;
766 APM_DPRINT("called apm_event_enable()\n");
767 if (sc->initialized) {
773 /* disable APM BIOS */
775 apm_event_disable(void)
777 struct apm_softc *sc = &apm_softc;
779 APM_DPRINT("called apm_event_disable()\n");
780 if (sc->initialized) {
781 untimeout(apm_timeout, NULL, apm_timeout_ch);
786 /* halt CPU in scheduling loop */
790 struct apm_softc *sc = &apm_softc;
793 sc->always_halt_cpu = 1;
796 /* don't halt CPU in scheduling loop */
798 apm_not_halt_cpu(void)
800 struct apm_softc *sc = &apm_softc;
803 sc->always_halt_cpu = 0;
806 /* device driver definitions */
813 apm_modevent(struct module *mod, int event, void *junk)
822 if (!cold && power_pm_get_type() == POWER_PM_TYPE_APM)
833 * Create "connection point"
836 apm_identify(driver_t *driver, device_t parent)
841 printf("Don't load this driver from userland!!\n");
845 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
847 panic("apm_identify");
854 apm_probe(device_t dev)
856 #define APM_KERNBASE KERNBASE
857 struct vm86frame vmf;
858 struct apm_softc *sc = &apm_softc;
864 device_set_desc(dev, "APM BIOS");
866 if (resource_int_value("apm", 0, "disabled", &disabled) != 0)
871 if (device_get_unit(dev) > 0) {
872 printf("apm: Only one APM driver supported.\n");
876 if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
877 power_pm_get_type() != POWER_PM_TYPE_APM) {
878 printf("apm: Other PM system enabled.\n");
882 if (resource_int_value("apm", 0, "flags", &flags) != 0)
885 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
886 bzero(&apm_softc, sizeof(apm_softc));
887 vmf.vmf_ah = APM_BIOS;
888 vmf.vmf_al = APM_INSTCHECK;
890 if (vm86_intcall(APM_INT, &vmf))
891 return ENXIO; /* APM not found */
892 if (vmf.vmf_bx != 0x504d) {
893 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
896 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
897 printf("apm: protected mode connections are not supported\n");
901 apm_version = vmf.vmf_ax;
902 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
903 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
904 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
906 vmf.vmf_ah = APM_BIOS;
907 vmf.vmf_al = APM_DISCONNECT;
909 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
912 /* PC98 have bogos APM 32bit BIOS */
913 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) == 0)
916 sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
917 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
918 if (sc->sc_res == NULL) {
919 printf("apm: cannot open NEC smm device\n");
922 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_res);
924 vmf.vmf_ah = APM_BIOS;
925 vmf.vmf_al = APM_PROT32CONNECT;
927 if (vm86_intcall(APM_INT, &vmf)) {
928 printf("apm: 32-bit connection error.\n");
932 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
933 sc->bios.seg.code32.limit = 0xffff;
934 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
935 sc->bios.seg.code16.limit = 0xffff;
936 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
937 sc->bios.seg.data.limit = 0xffff;
938 sc->bios.entry = vmf.vmf_ebx;
939 sc->connectmode = APM_PROT32CONNECT;
941 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
942 vmf.vmf_ah = APM_BIOS;
943 vmf.vmf_al = APM_PROT32CONNECT;
945 if (vm86_intcall(APM_INT, &vmf)) {
946 printf("apm: 32-bit connection error.\n");
949 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
950 sc->bios.seg.code32.limit = 0xffff;
951 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
952 sc->bios.seg.code16.limit = 0xffff;
953 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
954 sc->bios.seg.data.limit = 0xffff;
955 sc->bios.entry = vmf.vmf_ebx;
956 sc->connectmode = APM_PROT32CONNECT;
958 /* use 16-bit connection */
959 vmf.vmf_ah = APM_BIOS;
960 vmf.vmf_al = APM_PROT16CONNECT;
962 if (vm86_intcall(APM_INT, &vmf)) {
963 printf("apm: 16-bit connection error.\n");
966 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
967 sc->bios.seg.code16.limit = 0xffff;
968 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
969 sc->bios.seg.data.limit = 0xffff;
970 sc->bios.entry = vmf.vmf_bx;
971 sc->connectmode = APM_PROT16CONNECT;
979 * return 0 if the user will notice and handle the event,
980 * return 1 if the kernel driver should do so.
983 apm_record_event(struct apm_softc *sc, u_int event_type)
985 struct apm_event_info *evp;
987 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
988 return 1; /* no user waiting */
989 if (sc->event_count == APM_NEVENTS)
990 return 1; /* overflow */
991 if (sc->event_filter[event_type] == 0)
992 return 1; /* not registered */
993 evp = &sc->event_list[sc->event_ptr];
996 sc->event_ptr %= APM_NEVENTS;
997 evp->type = event_type;
998 evp->index = ++apm_evindex;
999 selwakeup(&sc->sc_rsel);
1000 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
1005 apm_power_profile(struct apm_softc *sc)
1008 struct apm_info info;
1009 static int apm_acline = 0;
1011 if (apm_get_info(&info))
1014 if (apm_acline != info.ai_acline) {
1015 apm_acline = info.ai_acline;
1016 state = apm_acline ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY;
1017 power_profile_set_state(state);
1021 /* Process APM event */
1023 apm_processevent(void)
1026 struct apm_softc *sc = &apm_softc;
1028 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
1029 APM_DPRINT("Received APM Event: " #symbol "\n");
1032 apm_event = apm_getevent();
1033 switch (apm_event) {
1034 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
1035 if (apm_op_inprog == 0) {
1037 if (apm_record_event(sc, apm_event)) {
1038 apm_suspend(PMST_STANDBY);
1042 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
1043 if (apm_op_inprog == 0) {
1045 if (apm_record_event(sc, apm_event)) {
1046 apm_suspend(PMST_STANDBY);
1050 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
1051 apm_lastreq_notify();
1052 if (apm_op_inprog == 0) {
1054 if (apm_record_event(sc, apm_event)) {
1058 return; /* XXX skip the rest */
1059 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
1060 apm_lastreq_notify();
1061 if (apm_op_inprog == 0) {
1063 if (apm_record_event(sc, apm_event)) {
1067 return; /* XXX skip the rest */
1068 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
1071 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
1072 apm_record_event(sc, apm_event);
1075 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
1076 apm_record_event(sc, apm_event);
1079 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
1080 apm_record_event(sc, apm_event);
1082 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
1083 if (apm_record_event(sc, apm_event)) {
1085 apm_suspend(PMST_SUSPEND);
1088 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
1089 apm_record_event(sc, apm_event);
1090 apm_power_profile(sc);
1092 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1093 apm_record_event(sc, apm_event);
1094 inittodr(0); /* adjust time to RTC */
1096 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1097 apm_record_event(sc, apm_event);
1098 apm_power_profile(sc);
1103 printf("Unknown Original APM Event 0x%x\n", apm_event);
1106 } while (apm_event != PMEV_NOEVENT);
1108 apm_disable_smm(sc);
1115 * Initialize APM driver
1119 apm_attach(device_t dev)
1121 struct apm_softc *sc = &apm_softc;
1128 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1132 statclock_disable = 1;
1134 sc->initialized = 0;
1136 /* Must be externally enabled */
1139 /* Always call HLT in idle loop */
1140 sc->always_halt_cpu = 1;
1142 getenv_int("debug.apm_debug", &apm_debug);
1144 /* print bootstrap messages */
1145 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1146 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1147 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1148 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1149 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1150 is_enabled(!sc->disabled));
1151 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1152 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1156 sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
1157 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
1158 if (sc->sc_res == NULL)
1159 panic("%s: counldn't map I/O ports", device_get_name(dev));
1160 sc->sc_iot = rman_get_bustag(sc->sc_res);
1161 sc->sc_ioh = rman_get_bushandle(sc->sc_res);
1163 if (apm_version==0x112 || apm_version==0x111 || apm_version==0x110)
1164 apm_necsmm_addr = APM_NECSMM_PORT;
1166 apm_necsmm_addr = 0;
1167 apm_necsmm_mask = ~APM_NECSMM_EN;
1171 * In one test, apm bios version was 1.02; an attempt to register
1172 * a 1.04 driver resulted in a 1.00 connection! Registering a
1173 * 1.02 driver resulted in a 1.02 connection.
1175 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1176 for (; drv_version > 0x100; drv_version--)
1177 if (apm_driver_version(drv_version) == 0)
1179 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1180 ((drv_version & 0x000f) >> 0);
1181 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1182 ((apm_version & 0x0f00) >> 8);
1184 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1186 if (sc->intversion >= INTVERSION(1, 1))
1187 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1188 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1189 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1190 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1191 sc->majorversion, sc->minorversion);
1194 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1195 /* enable power management */
1197 if (apm_enable_disable_pm(1)) {
1198 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1199 (sc->bios.r.eax >> 8) & 0xff);
1203 /* engage power managment (APM 1.1 or later) */
1204 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1205 if (apm_engage_disengage_pm(1)) {
1206 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1207 (sc->bios.r.eax >> 8) & 0xff);
1208 APM_DPRINT(" (Docked or using external power?).\n");
1212 /* Power the system off using APM */
1213 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1216 /* Register APM again to pass the correct argument of pm_func. */
1217 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc);
1219 sc->initialized = 1;
1222 make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm");
1223 make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl");
1228 apmopen(dev_t dev, int flag, int fmt, struct thread *td)
1230 struct apm_softc *sc = &apm_softc;
1231 int ctl = APMDEV(dev);
1233 if (!sc->initialized)
1238 if (!(flag & FWRITE))
1240 if (sc->sc_flags & SCFLAG_OCTL)
1242 sc->sc_flags |= SCFLAG_OCTL;
1243 bzero(sc->event_filter, sizeof sc->event_filter);
1246 sc->sc_flags |= SCFLAG_ONORMAL;
1256 apmclose(dev_t dev, int flag, int fmt, struct thread *td)
1258 struct apm_softc *sc = &apm_softc;
1259 int ctl = APMDEV(dev);
1263 apm_lastreq_rejected();
1264 sc->sc_flags &= ~SCFLAG_OCTL;
1265 bzero(sc->event_filter, sizeof sc->event_filter);
1268 sc->sc_flags &= ~SCFLAG_ONORMAL;
1271 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1272 sc->event_count = 0;
1279 apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1281 struct apm_softc *sc = &apm_softc;
1282 struct apm_bios_arg *args;
1287 if (!sc->initialized)
1289 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1292 if (!(flag & FWRITE))
1295 apm_suspend(PMST_SUSPEND);
1301 if (!(flag & FWRITE))
1304 apm_suspend(PMST_STANDBY);
1309 case APMIO_GETINFO_OLD:
1311 struct apm_info info;
1312 apm_info_old_t aiop;
1314 if (apm_get_info(&info))
1316 aiop = (apm_info_old_t)addr;
1317 aiop->ai_major = info.ai_major;
1318 aiop->ai_minor = info.ai_minor;
1319 aiop->ai_acline = info.ai_acline;
1320 aiop->ai_batt_stat = info.ai_batt_stat;
1321 aiop->ai_batt_life = info.ai_batt_life;
1322 aiop->ai_status = info.ai_status;
1326 if (apm_get_info((apm_info_t)addr))
1329 case APMIO_GETPWSTATUS:
1330 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1334 if (!(flag & FWRITE))
1339 if (!(flag & FWRITE))
1341 apm_event_disable();
1344 if (!(flag & FWRITE))
1348 case APMIO_NOTHALTCPU:
1349 if (!(flag & FWRITE))
1354 if (!(flag & FWRITE))
1356 newstate = *(int *)addr;
1357 if (apm_display(newstate))
1361 if (!(flag & FWRITE))
1363 /* XXX compatibility with the old interface */
1364 args = (struct apm_bios_arg *)addr;
1365 sc->bios.r.eax = args->eax;
1366 sc->bios.r.ebx = args->ebx;
1367 sc->bios.r.ecx = args->ecx;
1368 sc->bios.r.edx = args->edx;
1369 sc->bios.r.esi = args->esi;
1370 sc->bios.r.edi = args->edi;
1371 if ((ret = apm_bioscall())) {
1373 * Return code 1 means bios call was unsuccessful.
1374 * Error code is stored in %ah.
1375 * Return code -1 means bios call was unsupported
1376 * in the APM BIOS version.
1383 * Return code 0 means bios call was successful.
1384 * We need only %al and can discard %ah.
1386 sc->bios.r.eax &= 0xff;
1388 args->eax = sc->bios.r.eax;
1389 args->ebx = sc->bios.r.ebx;
1390 args->ecx = sc->bios.r.ecx;
1391 args->edx = sc->bios.r.edx;
1392 args->esi = sc->bios.r.esi;
1393 args->edi = sc->bios.r.edi;
1400 /* for /dev/apmctl */
1401 if (APMDEV(dev) == APMDEV_CTL) {
1402 struct apm_event_info *evp;
1407 case APMIO_NEXTEVENT:
1408 if (!sc->event_count) {
1411 evp = (struct apm_event_info *)addr;
1412 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1414 *evp = sc->event_list[i];
1418 case APMIO_REJECTLASTREQ:
1419 if (apm_lastreq_rejected()) {
1433 apmwrite(dev_t dev, struct uio *uio, int ioflag)
1435 struct apm_softc *sc = &apm_softc;
1440 if (APMDEV(dev) != APMDEV_CTL)
1442 if (uio->uio_resid != sizeof(u_int))
1445 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1448 if (event_type < 0 || event_type >= APM_NPMEV)
1451 if (sc->event_filter[event_type] == 0) {
1456 sc->event_filter[event_type] = enabled;
1457 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1459 return uio->uio_resid;
1463 apmpoll(dev_t dev, int events, struct thread *td)
1465 struct apm_softc *sc = &apm_softc;
1468 if (events & (POLLIN | POLLRDNORM)) {
1469 if (sc->event_count) {
1470 revents |= events & (POLLIN | POLLRDNORM);
1472 selrecord(td, &sc->sc_rsel);
1479 static device_method_t apm_methods[] = {
1480 /* Device interface */
1481 DEVMETHOD(device_identify, apm_identify),
1482 DEVMETHOD(device_probe, apm_probe),
1483 DEVMETHOD(device_attach, apm_attach),
1488 static driver_t apm_driver = {
1491 1, /* no softc (XXX) */
1494 static devclass_t apm_devclass;
1496 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, apm_modevent, 0);
1497 MODULE_VERSION(apm, 1);
1500 apm_pm_func(u_long cmd, void *arg, ...)
1502 int state, apm_state;
1508 case POWER_CMD_SUSPEND:
1510 state = va_arg(ap, int);
1514 case POWER_SLEEP_STATE_STANDBY:
1515 apm_state = PMST_STANDBY;
1517 case POWER_SLEEP_STATE_SUSPEND:
1518 case POWER_SLEEP_STATE_HIBERNATE:
1519 apm_state = PMST_SUSPEND;
1526 apm_suspend(apm_state);
1539 apm_pm_register(void *arg)
1543 resource_int_value("apm", 0, "disabled", &disabled);
1545 power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL);
1548 SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, 0);