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.21 2006/12/23 00:27:03 swildner Exp $
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/eventhandler.h>
26 #include <sys/device.h>
27 #include <sys/kernel.h>
29 #include <sys/reboot.h>
31 #include <sys/selinfo.h>
33 #include <sys/fcntl.h>
35 #include <sys/signalvar.h>
36 #include <sys/sysctl.h>
37 #include <machine/apm_bios.h>
38 #include <machine/segments.h>
39 #include <machine/clock.h>
41 #include <vm/vm_param.h>
43 #include <sys/syslog.h>
44 #include <sys/thread2.h>
46 #include <machine/pc/bios.h>
47 #include <machine/vm86.h>
49 #include <machine_base/apm/apm.h>
51 /* Used by the apm_saver screen saver module */
52 int apm_display (int newstate
);
53 struct apm_softc apm_softc
;
55 static void apm_resume (void);
56 static int apm_bioscall(void);
57 static int apm_check_function_supported (u_int version
, u_int func
);
59 static u_long apm_version
;
63 #define SCFLAG_ONORMAL 0x0000001
64 #define SCFLAG_OCTL 0x0000002
65 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
67 #define APMDEV(dev) (minor(dev)&0x0f)
68 #define APMDEV_NORMAL 0
71 static struct apmhook
*hook
[NAPM_HOOK
]; /* XXX */
73 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
75 /* Map version number to integer (keeps ordering of version numbers) */
76 #define INTVERSION(major, minor) ((major)*100 + (minor))
78 static struct callout apm_timeout_ch
;
80 static timeout_t apm_timeout
;
81 static d_open_t apmopen
;
82 static d_close_t apmclose
;
83 static d_write_t apmwrite
;
84 static d_ioctl_t apmioctl
;
85 static d_poll_t apmpoll
;
88 static struct dev_ops apm_ops
= {
89 { "apm", CDEV_MAJOR
, 0 },
97 static int apm_suspend_delay
= 1;
98 static int apm_standby_delay
= 1;
99 static int apm_debug
= 0;
101 #define APM_DPRINT(args...) do { \
107 SYSCTL_INT(_machdep
, OID_AUTO
, apm_suspend_delay
, CTLFLAG_RW
, &apm_suspend_delay
, 1, "");
108 SYSCTL_INT(_machdep
, OID_AUTO
, apm_standby_delay
, CTLFLAG_RW
, &apm_standby_delay
, 1, "");
109 SYSCTL_INT(_debug
, OID_AUTO
, apm_debug
, CTLFLAG_RW
, &apm_debug
, 0, "");
112 * return 0 if the function successfull,
113 * return 1 if the function unsuccessfull,
114 * return -1 if the function unsupported.
119 struct apm_softc
*sc
= &apm_softc
;
121 u_int apm_func
= sc
->bios
.r
.eax
& 0xff;
123 if (!apm_check_function_supported(sc
->intversion
, apm_func
)) {
124 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
125 apm_func
, sc
->majorversion
, sc
->minorversion
);
130 if (sc
->connectmode
== APM_PROT32CONNECT
) {
131 set_bios_selectors(&sc
->bios
.seg
,
132 BIOSCODE_FLAG
| BIOSDATA_FLAG
);
133 error
= bios32(&sc
->bios
.r
,
134 sc
->bios
.entry
, GSEL(GBIOSCODE32_SEL
, SEL_KPL
));
136 error
= bios16(&sc
->bios
, NULL
);
142 /* check whether APM function is supported (1) or not (0). */
144 apm_check_function_supported(u_int version
, u_int func
)
146 /* except driver version */
147 if (func
== APM_DRVVERSION
) {
152 case INTVERSION(1, 0):
153 if (func
> APM_GETPMEVENT
) {
154 return (0); /* not supported */
157 case INTVERSION(1, 1):
158 if (func
> APM_ENGAGEDISENGAGEPM
&&
159 func
< APM_OEMFUNC
) {
160 return (0); /* not supported */
163 case INTVERSION(1, 2):
167 return (1); /* supported */
170 /* enable/disable power management */
172 apm_enable_disable_pm(int enable
)
174 struct apm_softc
*sc
= &apm_softc
;
176 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_ENABLEDISABLEPM
;
178 if (sc
->intversion
>= INTVERSION(1, 1))
179 sc
->bios
.r
.ebx
= PMDV_ALLDEV
;
181 sc
->bios
.r
.ebx
= 0xffff; /* APM version 1.0 only */
182 sc
->bios
.r
.ecx
= enable
;
184 return (apm_bioscall());
187 /* register driver version (APM 1.1 or later) */
189 apm_driver_version(int version
)
191 struct apm_softc
*sc
= &apm_softc
;
193 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_DRVVERSION
;
194 sc
->bios
.r
.ebx
= 0x0;
195 sc
->bios
.r
.ecx
= version
;
198 if (apm_bioscall() == 0 && sc
->bios
.r
.eax
== version
)
201 /* Some old BIOSes don't return the connection version in %ax. */
202 if (sc
->bios
.r
.eax
== ((APM_BIOS
<< 8) | APM_DRVVERSION
))
208 /* engage/disengage power management (APM 1.1 or later) */
210 apm_engage_disengage_pm(int engage
)
212 struct apm_softc
*sc
= &apm_softc
;
214 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_ENGAGEDISENGAGEPM
;
215 sc
->bios
.r
.ebx
= PMDV_ALLDEV
;
216 sc
->bios
.r
.ecx
= engage
;
218 return (apm_bioscall());
225 struct apm_softc
*sc
= &apm_softc
;
227 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_GETPMEVENT
;
233 return (PMEV_NOEVENT
);
234 return (sc
->bios
.r
.ebx
& 0xffff);
237 /* suspend entire system */
239 apm_suspend_system(int state
)
241 struct apm_softc
*sc
= &apm_softc
;
243 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_SETPWSTATE
;
244 sc
->bios
.r
.ebx
= PMDV_ALLDEV
;
245 sc
->bios
.r
.ecx
= state
;
248 if (apm_bioscall()) {
249 kprintf("Entire system suspend failure: errcode = %d\n",
250 0xff & (sc
->bios
.r
.eax
>> 8));
256 /* Display control */
258 * Experimental implementation: My laptop machine can't handle this function
259 * If your laptop can control the display via APM, please inform me.
260 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
263 apm_display(int newstate
)
265 struct apm_softc
*sc
= &apm_softc
;
267 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_SETPWSTATE
;
268 sc
->bios
.r
.ebx
= PMDV_DISP0
;
269 sc
->bios
.r
.ecx
= newstate
? PMST_APMENABLED
:PMST_SUSPEND
;
271 if (apm_bioscall() == 0) {
275 /* If failed, then try to blank all display devices instead. */
276 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_SETPWSTATE
;
277 sc
->bios
.r
.ebx
= PMDV_DISPALL
; /* all display devices */
278 sc
->bios
.r
.ecx
= newstate
? PMST_APMENABLED
:PMST_SUSPEND
;
280 if (apm_bioscall() == 0) {
283 kprintf("Display off failure: errcode = %d\n",
284 0xff & (sc
->bios
.r
.eax
>> 8));
289 * Turn off the entire system.
292 apm_power_off(void *junk
, int howto
)
294 struct apm_softc
*sc
= &apm_softc
;
296 /* Not halting powering off, or not active */
297 if (!(howto
& RB_POWEROFF
) || !apm_softc
.active
)
299 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_SETPWSTATE
;
300 sc
->bios
.r
.ebx
= PMDV_ALLDEV
;
301 sc
->bios
.r
.ecx
= PMST_OFF
;
306 /* APM Battery low handler */
308 apm_battery_low(void)
310 kprintf("\007\007 * * * BATTERY IS LOW * * * \007\007");
313 /* APM hook manager */
314 static struct apmhook
*
315 apm_add_hook(struct apmhook
**list
, struct apmhook
*ah
)
317 struct apmhook
*p
, *prev
;
319 APM_DPRINT("Add hook \"%s\"\n", ah
->ah_name
);
323 panic("illegal apm_hook!");
325 for (p
= *list
; p
!= NULL
; prev
= p
, p
= p
->ah_next
)
326 if (p
->ah_order
> ah
->ah_order
)
333 ah
->ah_next
= prev
->ah_next
;
341 apm_del_hook(struct apmhook
**list
, struct apmhook
*ah
)
343 struct apmhook
*p
, *prev
;
347 for (p
= *list
; p
!= NULL
; prev
= p
, p
= p
->ah_next
)
350 panic("Tried to delete unregistered apm_hook.");
354 prev
->ah_next
= p
->ah_next
;
362 /* APM driver calls some functions automatically */
364 apm_execute_hook(struct apmhook
*list
)
368 for (p
= list
; p
!= NULL
; p
= p
->ah_next
) {
369 APM_DPRINT("Execute APM hook \"%s.\"\n", p
->ah_name
);
370 if ((*(p
->ah_fun
))(p
->ah_arg
))
371 kprintf("Warning: APM hook \"%s\" failed", p
->ah_name
);
376 /* establish an apm hook */
378 apm_hook_establish(int apmh
, struct apmhook
*ah
)
380 if (apmh
< 0 || apmh
>= NAPM_HOOK
)
383 return apm_add_hook(&hook
[apmh
], ah
);
386 /* disestablish an apm hook */
388 apm_hook_disestablish(int apmh
, struct apmhook
*ah
)
390 if (apmh
< 0 || apmh
>= NAPM_HOOK
)
393 apm_del_hook(&hook
[apmh
], ah
);
397 static struct timeval suspend_time
;
398 static struct timeval diff_time
;
401 apm_default_resume(void *arg
)
403 u_int second
, minute
, hour
;
404 struct timeval resume_time
, tmp_time
;
406 /* modified for adjkerntz */
408 timer_restore(); /* restore the all timers */
409 inittodr(0); /* adjust time to RTC */
410 microtime(&resume_time
);
411 getmicrotime(&tmp_time
);
412 timevaladd(&tmp_time
, &diff_time
);
415 /* XXX THIS DOESN'T WORK!!! */
419 #ifdef APM_FIXUP_CALLTODO
420 /* Calculate the delta time suspended */
421 timevalsub(&resume_time
, &suspend_time
);
422 /* Fixup the calltodo list with the delta time. */
423 adjust_timeout_calltodo(&resume_time
);
424 #endif /* APM_FIXUP_CALLTODOK */
426 #ifndef APM_FIXUP_CALLTODO
427 second
= resume_time
.tv_sec
- suspend_time
.tv_sec
;
428 #else /* APM_FIXUP_CALLTODO */
430 * We've already calculated resume_time to be the delta between
431 * the suspend and the resume.
433 second
= resume_time
.tv_sec
;
434 #endif /* APM_FIXUP_CALLTODO */
435 hour
= second
/ 3600;
437 minute
= second
/ 60;
439 log(LOG_NOTICE
, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
440 hour
, minute
, second
);
445 apm_default_suspend(void *arg
)
448 microtime(&diff_time
);
450 microtime(&suspend_time
);
451 timevalsub(&diff_time
, &suspend_time
);
456 static int apm_record_event (struct apm_softc
*, u_int
);
457 static void apm_processevent(void);
459 static u_int apm_op_inprog
= 0;
464 struct apm_softc
*sc
= &apm_softc
;
471 sc
->suspends
= sc
->suspend_countdown
= 0;
473 if (sc
->initialized
) {
474 error
= DEVICE_SUSPEND(root_bus
);
476 DEVICE_RESUME(root_bus
);
478 apm_execute_hook(hook
[APM_HOOK_SUSPEND
]);
479 if (apm_suspend_system(PMST_SUSPEND
) == 0) {
482 /* Failure, 'resume' the system again */
483 apm_execute_hook(hook
[APM_HOOK_RESUME
]);
484 DEVICE_RESUME(root_bus
);
493 struct apm_softc
*sc
= &apm_softc
;
499 sc
->standbys
= sc
->standby_countdown
= 0;
501 if (sc
->initialized
) {
503 * As far as standby, we don't need to execute
504 * all of suspend hooks.
506 apm_default_suspend(&apm_softc
);
507 if (apm_suspend_system(PMST_STANDBY
) == 0)
513 apm_lastreq_notify(void)
515 struct apm_softc
*sc
= &apm_softc
;
517 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_SETPWSTATE
;
518 sc
->bios
.r
.ebx
= PMDV_ALLDEV
;
519 sc
->bios
.r
.ecx
= PMST_LASTREQNOTIFY
;
525 apm_lastreq_rejected(void)
527 struct apm_softc
*sc
= &apm_softc
;
529 if (apm_op_inprog
== 0) {
530 return 1; /* no operation in progress */
533 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_SETPWSTATE
;
534 sc
->bios
.r
.ebx
= PMDV_ALLDEV
;
535 sc
->bios
.r
.ecx
= PMST_LASTREQREJECT
;
538 if (apm_bioscall()) {
539 APM_DPRINT("apm_lastreq_rejected: failed\n");
547 * Public interface to the suspend/resume:
549 * Execute suspend and resume hook before and after sleep, respectively.
554 apm_suspend(int state
)
556 struct apm_softc
*sc
= &apm_softc
;
558 if (!sc
->initialized
)
566 sc
->suspend_countdown
= apm_suspend_delay
;
572 sc
->standby_countdown
= apm_standby_delay
;
575 kprintf("apm_suspend: Unknown Suspend state 0x%x\n", state
);
580 apm_lastreq_notify();
586 struct apm_softc
*sc
= &apm_softc
;
591 if (sc
->initialized
) {
592 apm_execute_hook(hook
[APM_HOOK_RESUME
]);
593 DEVICE_RESUME(root_bus
);
598 /* get power status per battery */
600 apm_get_pwstatus(apm_pwstatus_t app
)
602 struct apm_softc
*sc
= &apm_softc
;
604 if (app
->ap_device
!= PMDV_ALLDEV
&&
605 (app
->ap_device
< PMDV_BATT0
|| app
->ap_device
> PMDV_BATT_ALL
))
608 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_GETPWSTATUS
;
609 sc
->bios
.r
.ebx
= app
->ap_device
;
611 sc
->bios
.r
.edx
= 0xffff; /* default to unknown battery time */
616 app
->ap_acline
= (sc
->bios
.r
.ebx
>> 8) & 0xff;
617 app
->ap_batt_stat
= sc
->bios
.r
.ebx
& 0xff;
618 app
->ap_batt_flag
= (sc
->bios
.r
.ecx
>> 8) & 0xff;
619 app
->ap_batt_life
= sc
->bios
.r
.ecx
& 0xff;
620 sc
->bios
.r
.edx
&= 0xffff;
621 if (sc
->bios
.r
.edx
== 0xffff) /* Time is unknown */
622 app
->ap_batt_time
= -1;
623 else if (sc
->bios
.r
.edx
& 0x8000) /* Time is in minutes */
624 app
->ap_batt_time
= (sc
->bios
.r
.edx
& 0x7fff) * 60;
625 else /* Time is in seconds */
626 app
->ap_batt_time
= sc
->bios
.r
.edx
;
632 /* get APM information */
634 apm_get_info(apm_info_t aip
)
636 struct apm_softc
*sc
= &apm_softc
;
637 struct apm_pwstatus aps
;
639 bzero(&aps
, sizeof(aps
));
640 aps
.ap_device
= PMDV_ALLDEV
;
641 if (apm_get_pwstatus(&aps
))
644 aip
->ai_infoversion
= 1;
645 aip
->ai_acline
= aps
.ap_acline
;
646 aip
->ai_batt_stat
= aps
.ap_batt_stat
;
647 aip
->ai_batt_life
= aps
.ap_batt_life
;
648 aip
->ai_batt_time
= aps
.ap_batt_time
;
649 aip
->ai_major
= (u_int
)sc
->majorversion
;
650 aip
->ai_minor
= (u_int
)sc
->minorversion
;
651 aip
->ai_status
= (u_int
)sc
->active
;
653 sc
->bios
.r
.eax
= (APM_BIOS
<< 8) | APM_GETCAPABILITIES
;
657 if (apm_bioscall()) {
658 aip
->ai_batteries
= -1; /* Unknown */
659 aip
->ai_capabilities
= 0xff00; /* Unknown, with no bits set */
661 aip
->ai_batteries
= sc
->bios
.r
.ebx
& 0xff;
662 aip
->ai_capabilities
= sc
->bios
.r
.ecx
& 0xf;
665 bzero(aip
->ai_spare
, sizeof aip
->ai_spare
);
671 /* inform APM BIOS that CPU is idle */
675 struct apm_softc
*sc
= &apm_softc
;
679 sc
->bios
.r
.eax
= (APM_BIOS
<<8) | APM_CPUIDLE
;
680 sc
->bios
.r
.edx
= sc
->bios
.r
.ecx
= sc
->bios
.r
.ebx
= 0;
684 * Some APM implementation halts CPU in BIOS, whenever
685 * "CPU-idle" function are invoked, but swtch() of
686 * FreeBSD halts CPU, therefore, CPU is halted twice
687 * in the sched loop. It makes the interrupt latency
688 * terribly long and be able to cause a serious problem
689 * in interrupt processing. We prevent it by removing
690 * "hlt" operation from swtch() and managed it under
693 if (!sc
->active
|| sc
->always_halt_cpu
)
694 __asm("hlt"); /* wait for interrupt */
697 /* inform APM BIOS that CPU is busy */
701 struct apm_softc
*sc
= &apm_softc
;
704 * The APM specification says this is only necessary if your BIOS
705 * slows down the processor in the idle task, otherwise it's not
708 if (sc
->slow_idle_cpu
&& sc
->active
) {
710 sc
->bios
.r
.eax
= (APM_BIOS
<<8) | APM_CPUBUSY
;
711 sc
->bios
.r
.edx
= sc
->bios
.r
.ecx
= sc
->bios
.r
.ebx
= 0;
718 * APM timeout routine:
720 * This routine is automatically called by timer once per second.
724 apm_timeout(void *dummy
)
726 struct apm_softc
*sc
= &apm_softc
;
729 apm_lastreq_notify();
731 if (sc
->standbys
&& sc
->standby_countdown
-- <= 0)
734 if (sc
->suspends
&& sc
->suspend_countdown
-- <= 0)
740 if (sc
->active
== 1) {
741 /* Run slightly more oftan than 1 Hz */
742 callout_reset(&apm_timeout_ch
, hz
- 1, apm_timeout
, NULL
);
746 /* enable APM BIOS */
748 apm_event_enable(void)
750 struct apm_softc
*sc
= &apm_softc
;
752 APM_DPRINT("called apm_event_enable()\n");
753 if (sc
->initialized
) {
755 callout_init(&apm_timeout_ch
);
760 /* disable APM BIOS */
762 apm_event_disable(void)
764 struct apm_softc
*sc
= &apm_softc
;
766 APM_DPRINT("called apm_event_disable()\n");
767 if (sc
->initialized
) {
768 callout_stop(&apm_timeout_ch
);
773 /* halt CPU in scheduling loop */
777 struct apm_softc
*sc
= &apm_softc
;
780 sc
->always_halt_cpu
= 1;
783 /* don't halt CPU in scheduling loop */
785 apm_not_halt_cpu(void)
787 struct apm_softc
*sc
= &apm_softc
;
790 sc
->always_halt_cpu
= 0;
793 /* device driver definitions */
799 apm_probe(device_t dev
)
801 #define APM_KERNBASE KERNBASE
802 struct vm86frame vmf
;
803 struct apm_softc
*sc
= &apm_softc
;
806 if (resource_int_value("apm", 0, "disabled", &disabled
) == 0
810 device_set_desc(dev
, "APM BIOS");
812 if ( device_get_unit(dev
) > 0 ) {
813 kprintf("apm: Only one APM driver supported.\n");
817 if (resource_int_value("apm", 0, "flags", &flags
) != 0)
820 bzero(&vmf
, sizeof(struct vm86frame
)); /* safety */
821 bzero(&apm_softc
, sizeof(apm_softc
));
822 vmf
.vmf_ah
= APM_BIOS
;
823 vmf
.vmf_al
= APM_INSTCHECK
;
825 if (vm86_intcall(APM_INT
, &vmf
))
826 return ENXIO
; /* APM not found */
827 if (vmf
.vmf_bx
!= 0x504d) {
828 kprintf("apm: incorrect signature (0x%x)\n", vmf
.vmf_bx
);
831 if ((vmf
.vmf_cx
& (APM_32BIT_SUPPORT
| APM_16BIT_SUPPORT
)) == 0) {
832 kprintf("apm: protected mode connections are not supported\n");
836 apm_version
= vmf
.vmf_ax
;
837 sc
->slow_idle_cpu
= ((vmf
.vmf_cx
& APM_CPUIDLE_SLOW
) != 0);
838 sc
->disabled
= ((vmf
.vmf_cx
& APM_DISABLED
) != 0);
839 sc
->disengaged
= ((vmf
.vmf_cx
& APM_DISENGAGED
) != 0);
841 vmf
.vmf_ah
= APM_BIOS
;
842 vmf
.vmf_al
= APM_DISCONNECT
;
844 vm86_intcall(APM_INT
, &vmf
); /* disconnect, just in case */
846 if ((vmf
.vmf_cx
& APM_32BIT_SUPPORT
) != 0) {
847 vmf
.vmf_ah
= APM_BIOS
;
848 vmf
.vmf_al
= APM_PROT32CONNECT
;
850 if (vm86_intcall(APM_INT
, &vmf
)) {
851 kprintf("apm: 32-bit connection error.\n");
854 sc
->bios
.seg
.code32
.base
= (vmf
.vmf_ax
<< 4) + APM_KERNBASE
;
855 sc
->bios
.seg
.code32
.limit
= 0xffff;
856 sc
->bios
.seg
.code16
.base
= (vmf
.vmf_cx
<< 4) + APM_KERNBASE
;
857 sc
->bios
.seg
.code16
.limit
= 0xffff;
858 sc
->bios
.seg
.data
.base
= (vmf
.vmf_dx
<< 4) + APM_KERNBASE
;
859 sc
->bios
.seg
.data
.limit
= 0xffff;
860 sc
->bios
.entry
= vmf
.vmf_ebx
;
861 sc
->connectmode
= APM_PROT32CONNECT
;
863 /* use 16-bit connection */
864 vmf
.vmf_ah
= APM_BIOS
;
865 vmf
.vmf_al
= APM_PROT16CONNECT
;
867 if (vm86_intcall(APM_INT
, &vmf
)) {
868 kprintf("apm: 16-bit connection error.\n");
871 sc
->bios
.seg
.code16
.base
= (vmf
.vmf_ax
<< 4) + APM_KERNBASE
;
872 sc
->bios
.seg
.code16
.limit
= 0xffff;
873 sc
->bios
.seg
.data
.base
= (vmf
.vmf_cx
<< 4) + APM_KERNBASE
;
874 sc
->bios
.seg
.data
.limit
= 0xffff;
875 sc
->bios
.entry
= vmf
.vmf_bx
;
876 sc
->connectmode
= APM_PROT16CONNECT
;
883 * return 0 if the user will notice and handle the event,
884 * return 1 if the kernel driver should do so.
887 apm_record_event(struct apm_softc
*sc
, u_int event_type
)
889 struct apm_event_info
*evp
;
891 if ((sc
->sc_flags
& SCFLAG_OPEN
) == 0)
892 return 1; /* no user waiting */
893 if (sc
->event_count
== APM_NEVENTS
)
894 return 1; /* overflow */
895 if (sc
->event_filter
[event_type
] == 0)
896 return 1; /* not registered */
897 evp
= &sc
->event_list
[sc
->event_ptr
];
900 sc
->event_ptr
%= APM_NEVENTS
;
901 evp
->type
= event_type
;
902 evp
->index
= ++apm_evindex
;
903 selwakeup(&sc
->sc_rsel
);
904 return (sc
->sc_flags
& SCFLAG_OCTL
) ? 0 : 1; /* user may handle */
907 /* Process APM event */
909 apm_processevent(void)
912 struct apm_softc
*sc
= &apm_softc
;
914 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
915 APM_DPRINT("Received APM Event: " #symbol "\n");
918 apm_event
= apm_getevent();
920 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ
);
921 if (apm_op_inprog
== 0) {
923 if (apm_record_event(sc
, apm_event
)) {
924 apm_suspend(PMST_STANDBY
);
928 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ
);
929 if (apm_op_inprog
== 0) {
931 if (apm_record_event(sc
, apm_event
)) {
932 apm_suspend(PMST_STANDBY
);
936 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ
);
937 apm_lastreq_notify();
938 if (apm_op_inprog
== 0) {
940 if (apm_record_event(sc
, apm_event
)) {
944 return; /* XXX skip the rest */
945 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ
);
946 apm_lastreq_notify();
947 if (apm_op_inprog
== 0) {
949 if (apm_record_event(sc
, apm_event
)) {
953 return; /* XXX skip the rest */
954 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND
);
957 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME
);
958 apm_record_event(sc
, apm_event
);
961 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME
);
962 apm_record_event(sc
, apm_event
);
965 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME
);
966 apm_record_event(sc
, apm_event
);
969 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW
);
970 if (apm_record_event(sc
, apm_event
)) {
972 apm_suspend(PMST_SUSPEND
);
975 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE
);
976 apm_record_event(sc
, apm_event
);
978 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME
);
979 apm_record_event(sc
, apm_event
);
980 inittodr(0); /* adjust time to RTC */
982 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE
);
983 apm_record_event(sc
, apm_event
);
988 kprintf("Unknown Original APM Event 0x%x\n", apm_event
);
991 } while (apm_event
!= PMEV_NOEVENT
);
997 * Initialize APM driver
1001 apm_attach(device_t dev
)
1003 struct apm_softc
*sc
= &apm_softc
;
1007 if (resource_int_value("apm", 0, "flags", &flags
) != 0)
1010 sc
->initialized
= 0;
1012 /* Must be externally enabled */
1015 /* Always call HLT in idle loop */
1016 sc
->always_halt_cpu
= 1;
1018 kgetenv_int("debug.apm_debug", &apm_debug
);
1020 /* print bootstrap messages */
1021 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version
);
1022 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1023 sc
->bios
.seg
.code16
.base
, sc
->bios
.seg
.data
.base
);
1024 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1025 sc
->bios
.entry
, is_enabled(sc
->slow_idle_cpu
),
1026 is_enabled(!sc
->disabled
));
1027 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1028 sc
->bios
.seg
.code16
.limit
, sc
->bios
.seg
.data
.limit
);
1031 * In one test, apm bios version was 1.02; an attempt to register
1032 * a 1.04 driver resulted in a 1.00 connection! Registering a
1033 * 1.02 driver resulted in a 1.02 connection.
1035 drv_version
= apm_version
> 0x102 ? 0x102 : apm_version
;
1036 for (; drv_version
> 0x100; drv_version
--)
1037 if (apm_driver_version(drv_version
) == 0)
1039 sc
->minorversion
= ((drv_version
& 0x00f0) >> 4) * 10 +
1040 ((drv_version
& 0x000f) >> 0);
1041 sc
->majorversion
= ((drv_version
& 0xf000) >> 12) * 10 +
1042 ((apm_version
& 0x0f00) >> 8);
1044 sc
->intversion
= INTVERSION(sc
->majorversion
, sc
->minorversion
);
1046 if (sc
->intversion
>= INTVERSION(1, 1))
1047 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc
->disengaged
));
1048 device_printf(dev
, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1049 ((apm_version
& 0xf000) >> 12) * 10 + ((apm_version
& 0x0f00) >> 8),
1050 ((apm_version
& 0x00f0) >> 4) * 10 + ((apm_version
& 0x000f) >> 0),
1051 sc
->majorversion
, sc
->minorversion
);
1054 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc
->slow_idle_cpu
));
1055 /* enable power management */
1057 if (apm_enable_disable_pm(1)) {
1058 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1059 (sc
->bios
.r
.eax
>> 8) & 0xff);
1063 /* engage power managment (APM 1.1 or later) */
1064 if (sc
->intversion
>= INTVERSION(1, 1) && sc
->disengaged
) {
1065 if (apm_engage_disengage_pm(1)) {
1066 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1067 (sc
->bios
.r
.eax
>> 8) & 0xff);
1068 APM_DPRINT(" (Docked or using external power?).\n");
1072 /* default suspend hook */
1073 sc
->sc_suspend
.ah_fun
= apm_default_suspend
;
1074 sc
->sc_suspend
.ah_arg
= sc
;
1075 sc
->sc_suspend
.ah_name
= "default suspend";
1076 sc
->sc_suspend
.ah_order
= APM_MAX_ORDER
;
1078 /* default resume hook */
1079 sc
->sc_resume
.ah_fun
= apm_default_resume
;
1080 sc
->sc_resume
.ah_arg
= sc
;
1081 sc
->sc_resume
.ah_name
= "default resume";
1082 sc
->sc_resume
.ah_order
= APM_MIN_ORDER
;
1084 apm_hook_establish(APM_HOOK_SUSPEND
, &sc
->sc_suspend
);
1085 apm_hook_establish(APM_HOOK_RESUME
, &sc
->sc_resume
);
1087 /* Power the system off using APM */
1088 EVENTHANDLER_REGISTER(shutdown_final
, apm_power_off
, NULL
,
1091 sc
->initialized
= 1;
1093 dev_ops_add(&apm_ops
, 0, 0);
1094 make_dev(&apm_ops
, 0, UID_ROOT
, GID_OPERATOR
, 0660, "apm");
1095 make_dev(&apm_ops
, 8, UID_ROOT
, GID_OPERATOR
, 0660, "apmctl");
1100 apmopen(struct dev_open_args
*ap
)
1102 cdev_t dev
= ap
->a_head
.a_dev
;
1103 struct apm_softc
*sc
= &apm_softc
;
1104 int ctl
= APMDEV(dev
);
1106 if (!sc
->initialized
)
1111 if (!(ap
->a_oflags
& FWRITE
))
1113 if (sc
->sc_flags
& SCFLAG_OCTL
)
1115 sc
->sc_flags
|= SCFLAG_OCTL
;
1116 bzero(sc
->event_filter
, sizeof sc
->event_filter
);
1119 sc
->sc_flags
|= SCFLAG_ONORMAL
;
1129 apmclose(struct dev_close_args
*ap
)
1131 cdev_t dev
= ap
->a_head
.a_dev
;
1132 struct apm_softc
*sc
= &apm_softc
;
1133 int ctl
= APMDEV(dev
);
1137 apm_lastreq_rejected();
1138 sc
->sc_flags
&= ~SCFLAG_OCTL
;
1139 bzero(sc
->event_filter
, sizeof sc
->event_filter
);
1142 sc
->sc_flags
&= ~SCFLAG_ONORMAL
;
1145 if ((sc
->sc_flags
& SCFLAG_OPEN
) == 0) {
1146 sc
->event_count
= 0;
1153 apmioctl(struct dev_ioctl_args
*ap
)
1155 cdev_t dev
= ap
->a_head
.a_dev
;
1156 struct apm_softc
*sc
= &apm_softc
;
1157 struct apm_bios_arg
*args
;
1162 if (!sc
->initialized
)
1164 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", ap
->a_cmd
);
1166 switch (ap
->a_cmd
) {
1168 if (!(ap
->a_fflag
& FWRITE
))
1171 apm_suspend(PMST_SUSPEND
);
1177 if (!(ap
->a_fflag
& FWRITE
))
1180 apm_suspend(PMST_STANDBY
);
1185 case APMIO_GETINFO_OLD
:
1187 struct apm_info info
;
1188 apm_info_old_t aiop
;
1190 if (apm_get_info(&info
))
1192 aiop
= (apm_info_old_t
)ap
->a_data
;
1193 aiop
->ai_major
= info
.ai_major
;
1194 aiop
->ai_minor
= info
.ai_minor
;
1195 aiop
->ai_acline
= info
.ai_acline
;
1196 aiop
->ai_batt_stat
= info
.ai_batt_stat
;
1197 aiop
->ai_batt_life
= info
.ai_batt_life
;
1198 aiop
->ai_status
= info
.ai_status
;
1202 if (apm_get_info((apm_info_t
)ap
->a_data
))
1205 case APMIO_GETPWSTATUS
:
1206 if (apm_get_pwstatus((apm_pwstatus_t
)ap
->a_data
))
1210 if (!(ap
->a_fflag
& FWRITE
))
1215 if (!(ap
->a_fflag
& FWRITE
))
1217 apm_event_disable();
1220 if (!(ap
->a_fflag
& FWRITE
))
1224 case APMIO_NOTHALTCPU
:
1225 if (!(ap
->a_fflag
& FWRITE
))
1230 if (!(ap
->a_fflag
& FWRITE
))
1232 newstate
= *(int *)ap
->a_data
;
1233 if (apm_display(newstate
))
1237 if (!(ap
->a_fflag
& FWRITE
))
1239 /* XXX compatibility with the old interface */
1240 args
= (struct apm_bios_arg
*)ap
->a_data
;
1241 sc
->bios
.r
.eax
= args
->eax
;
1242 sc
->bios
.r
.ebx
= args
->ebx
;
1243 sc
->bios
.r
.ecx
= args
->ecx
;
1244 sc
->bios
.r
.edx
= args
->edx
;
1245 sc
->bios
.r
.esi
= args
->esi
;
1246 sc
->bios
.r
.edi
= args
->edi
;
1247 if ((ret
= apm_bioscall())) {
1249 * Return code 1 means bios call was unsuccessful.
1250 * Error code is stored in %ah.
1251 * Return code -1 means bios call was unsupported
1252 * in the APM BIOS version.
1259 * Return code 0 means bios call was successful.
1260 * We need only %al and can discard %ah.
1262 sc
->bios
.r
.eax
&= 0xff;
1264 args
->eax
= sc
->bios
.r
.eax
;
1265 args
->ebx
= sc
->bios
.r
.ebx
;
1266 args
->ecx
= sc
->bios
.r
.ecx
;
1267 args
->edx
= sc
->bios
.r
.edx
;
1268 args
->esi
= sc
->bios
.r
.esi
;
1269 args
->edi
= sc
->bios
.r
.edi
;
1276 /* for /dev/apmctl */
1277 if (APMDEV(dev
) == APMDEV_CTL
) {
1278 struct apm_event_info
*evp
;
1282 switch (ap
->a_cmd
) {
1283 case APMIO_NEXTEVENT
:
1284 if (!sc
->event_count
) {
1287 evp
= (struct apm_event_info
*)ap
->a_data
;
1288 i
= sc
->event_ptr
+ APM_NEVENTS
- sc
->event_count
;
1290 *evp
= sc
->event_list
[i
];
1294 case APMIO_REJECTLASTREQ
:
1295 if (apm_lastreq_rejected()) {
1309 apmwrite(struct dev_write_args
*ap
)
1311 cdev_t dev
= ap
->a_head
.a_dev
;
1312 struct uio
*uio
= ap
->a_uio
;
1313 struct apm_softc
*sc
= &apm_softc
;
1318 if (APMDEV(dev
) != APMDEV_CTL
)
1320 if (uio
->uio_resid
!= sizeof(u_int
))
1323 if ((error
= uiomove((caddr_t
)&event_type
, sizeof(u_int
), uio
)))
1326 if (event_type
< 0 || event_type
>= APM_NPMEV
)
1329 if (sc
->event_filter
[event_type
] == 0) {
1334 sc
->event_filter
[event_type
] = enabled
;
1335 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type
, is_enabled(enabled
));
1337 return uio
->uio_resid
;
1341 apmpoll(struct dev_poll_args
*ap
)
1343 struct apm_softc
*sc
= &apm_softc
;
1346 if (ap
->a_events
& (POLLIN
| POLLRDNORM
)) {
1347 if (sc
->event_count
) {
1348 revents
|= ap
->a_events
& (POLLIN
| POLLRDNORM
);
1350 selrecord(curthread
, &sc
->sc_rsel
);
1353 ap
->a_events
= revents
;
1358 * Because apm is a static device that always exists under any attached
1359 * isa device, and not scanned by the isa device, we need an identify
1360 * function to install the device so we can probe for it.
1362 static device_method_t apm_methods
[] = {
1363 /* Device interface */
1364 DEVMETHOD(device_identify
, bus_generic_identify
),
1365 DEVMETHOD(device_probe
, apm_probe
),
1366 DEVMETHOD(device_attach
, apm_attach
),
1371 static driver_t apm_driver
= {
1374 1, /* no softc (XXX) */
1377 static devclass_t apm_devclass
;
1379 DRIVER_MODULE(apm
, nexus
, apm_driver
, apm_devclass
, 0, 0);