apm.c revision 40751
1/*
2 * APM (Advanced Power Management) BIOS Device Driver
3 *
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>
8 *
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
14 * use.
15 *
16 * Sep, 1994	Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
17 *
18 *	$Id: apm.c,v 1.74 1998/09/28 03:41:12 jlemon Exp $
19 */
20
21#include "opt_devfs.h"
22#include "opt_vm86.h"
23
24#include <sys/param.h>
25#include <sys/conf.h>
26#include <sys/kernel.h>
27#ifdef DEVFS
28#include <sys/devfsext.h>
29#endif /*DEVFS*/
30#include <sys/systm.h>
31#include <sys/time.h>
32#include <sys/reboot.h>
33#include <i386/isa/isa_device.h>
34#include <machine/apm_bios.h>
35#include <machine/segments.h>
36#include <machine/clock.h>
37#include <vm/vm.h>
38#include <vm/vm_param.h>
39#include <vm/pmap.h>
40#include <sys/syslog.h>
41#include <i386/apm/apm_setup.h>
42
43#ifdef VM86
44#include <machine/psl.h>
45#include <machine/vm86.h>
46#endif
47
48static int apm_display __P((int newstate));
49static int apm_int __P((u_long *eax, u_long *ebx, u_long *ecx, u_long *edx));
50static void apm_resume __P((void));
51
52/* static data */
53struct apm_softc {
54	int	initialized, active;
55	int	always_halt_cpu, slow_idle_cpu;
56	int	disabled, disengaged;
57	u_int	minorversion, majorversion;
58	u_int	cs32_base, cs16_base, ds_base;
59	u_int	cs16_limit, cs32_limit, ds_limit;
60	u_int	cs_entry;
61	u_int	intversion;
62	struct apmhook sc_suspend;
63	struct apmhook sc_resume;
64#ifdef DEVFS
65	void 	*sc_devfs_token;
66#endif
67};
68
69static struct apm_softc apm_softc;
70static struct apmhook	*hook[NAPM_HOOK];		/* XXX */
71
72#define is_enabled(foo) ((foo) ? "enabled" : "disabled")
73
74/* Map version number to integer (keeps ordering of version numbers) */
75#define INTVERSION(major, minor)	((major)*100 + (minor))
76
77static struct callout_handle apm_timeout_ch =
78    CALLOUT_HANDLE_INITIALIZER(&apm_timeout_ch);
79
80static timeout_t apm_timeout;
81static d_open_t apmopen;
82static d_close_t apmclose;
83static d_ioctl_t apmioctl;
84
85#define CDEV_MAJOR 39
86static struct cdevsw apm_cdevsw =
87	{ apmopen,	apmclose,	noread,		nowrite,	/*39*/
88	  apmioctl,	nostop,		nullreset,	nodevtotty,/* APM */
89	  seltrue,	nommap,		NULL ,	"apm"	,NULL,	-1};
90
91/* setup APM GDT discriptors */
92static void
93setup_apm_gdt(u_int code32_base, u_int code16_base, u_int data_base, u_int code32_limit, u_int code16_limit, u_int data_limit)
94{
95	/* setup 32bit code segment */
96	gdt_segs[GAPMCODE32_SEL].ssd_base  = code32_base;
97	gdt_segs[GAPMCODE32_SEL].ssd_limit = code32_limit;
98
99	/* setup 16bit code segment */
100	gdt_segs[GAPMCODE16_SEL].ssd_base  = code16_base;
101	gdt_segs[GAPMCODE16_SEL].ssd_limit = code16_limit;
102
103	/* setup data segment */
104	gdt_segs[GAPMDATA_SEL  ].ssd_base  = data_base;
105	gdt_segs[GAPMDATA_SEL  ].ssd_limit = data_limit;
106
107	/* reflect these changes on physical GDT */
108	ssdtosd(gdt_segs + GAPMCODE32_SEL, &gdt[GAPMCODE32_SEL].sd);
109	ssdtosd(gdt_segs + GAPMCODE16_SEL, &gdt[GAPMCODE16_SEL].sd);
110	ssdtosd(gdt_segs + GAPMDATA_SEL  , &gdt[GAPMDATA_SEL  ].sd);
111}
112
113/* 48bit far pointer. Do not staticize - used from apm_setup.s */
114struct addr48 {
115	u_long		offset;
116	u_short		segment;
117} apm_addr;
118
119static int apm_errno;
120
121static int
122apm_int(u_long *eax, u_long *ebx, u_long *ecx, u_long *edx)
123{
124	struct apm_bios_arg apa;
125	int cf;
126
127	apa.eax = *eax;
128	apa.ebx = *ebx;
129	apa.ecx = *ecx;
130	apa.edx = *edx;
131	cf = apm_bios_call(&apa);
132	*eax = apa.eax;
133	*ebx = apa.ebx;
134	*ecx = apa.ecx;
135	*edx = apa.edx;
136	apm_errno = ((*eax) >> 8) & 0xff;
137	return cf;
138}
139
140
141/* enable/disable power management */
142static int
143apm_enable_disable_pm(int enable)
144{
145	struct apm_softc *sc = &apm_softc;
146
147	u_long eax, ebx, ecx, edx;
148
149	eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
150
151	if (sc->intversion >= INTVERSION(1, 1))
152		ebx  = PMDV_ALLDEV;
153	else
154		ebx  = 0xffff;	/* APM version 1.0 only */
155	ecx  = enable;
156	edx = 0;
157	return apm_int(&eax, &ebx, &ecx, &edx);
158}
159
160static void
161apm_driver_version(int version)
162{
163	u_long eax, ebx, ecx, edx;
164
165	/* First try APM 1.2 */
166	eax = (APM_BIOS << 8) | APM_DRVVERSION;
167	ebx  = 0x0;
168	ecx  = version;
169	edx = 0;
170	if(!apm_int(&eax, &ebx, &ecx, &edx))
171		apm_version = eax & 0xffff;
172}
173
174/* engage/disengage power management (APM 1.1 or later) */
175static int
176apm_engage_disengage_pm(int engage)
177{
178	u_long eax, ebx, ecx, edx;
179
180	eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
181	ebx = PMDV_ALLDEV;
182	ecx = engage;
183	edx = 0;
184	return(apm_int(&eax, &ebx, &ecx, &edx));
185}
186
187/* get PM event */
188static u_int
189apm_getevent(void)
190{
191	u_long eax, ebx, ecx, edx;
192
193	eax = (APM_BIOS << 8) | APM_GETPMEVENT;
194
195	ebx = 0;
196	ecx = 0;
197	edx = 0;
198	if (apm_int(&eax, &ebx, &ecx, &edx))
199		return PMEV_NOEVENT;
200
201	return ebx & 0xffff;
202}
203
204/* suspend entire system */
205static int
206apm_suspend_system(int state)
207{
208	u_long eax, ebx, ecx, edx;
209
210	eax = (APM_BIOS << 8) | APM_SETPWSTATE;
211	ebx = PMDV_ALLDEV;
212	ecx = state;
213	edx = 0;
214
215	if (apm_int(&eax, &ebx, &ecx, &edx)) {
216		printf("Entire system suspend failure: errcode = %ld\n",
217			0xff & (eax >> 8));
218		return 1;
219	}
220	return 0;
221}
222
223/* Display control */
224/*
225 * Experimental implementation: My laptop machine can't handle this function
226 * If your laptop can control the display via APM, please inform me.
227 *                            HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
228 */
229static int
230apm_display(int newstate)
231{
232	u_long eax, ebx, ecx, edx;
233
234	eax = (APM_BIOS << 8) | APM_SETPWSTATE;
235	ebx = PMDV_DISP0;
236	ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
237	edx = 0;
238	if (apm_int(&eax, &ebx, &ecx, &edx)) {
239		printf("Display off failure: errcode = %ld\n",
240			0xff & (eax >> 8));
241		return 1;
242	}
243	return 0;
244}
245
246/*
247 * Turn off the entire system.
248 */
249static void
250apm_power_off(int howto, void *junk)
251{
252	u_long eax, ebx, ecx, edx;
253
254	/* Not halting, or not active */
255	if (!(howto & RB_HALT) || !apm_softc.active)
256		return;
257	eax = (APM_BIOS << 8) | APM_SETPWSTATE;
258	ebx = PMDV_ALLDEV;
259	ecx = PMST_OFF;
260	edx = 0;
261	apm_int(&eax, &ebx, &ecx, &edx);
262}
263
264/* APM Battery low handler */
265static void
266apm_battery_low(void)
267{
268	printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
269}
270
271/* APM hook manager */
272static struct apmhook *
273apm_add_hook(struct apmhook **list, struct apmhook *ah)
274{
275	int s;
276	struct apmhook *p, *prev;
277
278#ifdef APM_DEBUG
279	printf("Add hook \"%s\"\n", ah->ah_name);
280#endif
281
282	s = splhigh();
283	if (ah == NULL)
284		panic("illegal apm_hook!");
285	prev = NULL;
286	for (p = *list; p != NULL; prev = p, p = p->ah_next)
287		if (p->ah_order > ah->ah_order)
288			break;
289
290	if (prev == NULL) {
291		ah->ah_next = *list;
292		*list = ah;
293	} else {
294		ah->ah_next = prev->ah_next;
295		prev->ah_next = ah;
296	}
297	splx(s);
298	return ah;
299}
300
301static void
302apm_del_hook(struct apmhook **list, struct apmhook *ah)
303{
304	int s;
305	struct apmhook *p, *prev;
306
307	s = splhigh();
308	prev = NULL;
309	for (p = *list; p != NULL; prev = p, p = p->ah_next)
310		if (p == ah)
311			goto deleteit;
312	panic("Tried to delete unregistered apm_hook.");
313	goto nosuchnode;
314deleteit:
315	if (prev != NULL)
316		prev->ah_next = p->ah_next;
317	else
318		*list = p->ah_next;
319nosuchnode:
320	splx(s);
321}
322
323
324/* APM driver calls some functions automatically */
325static void
326apm_execute_hook(struct apmhook *list)
327{
328	struct apmhook *p;
329
330	for (p = list; p != NULL; p = p->ah_next) {
331#ifdef APM_DEBUG
332		printf("Execute APM hook \"%s.\"\n", p->ah_name);
333#endif
334		if ((*(p->ah_fun))(p->ah_arg))
335			printf("Warning: APM hook \"%s\" failed", p->ah_name);
336	}
337}
338
339
340/* establish an apm hook */
341struct apmhook *
342apm_hook_establish(int apmh, struct apmhook *ah)
343{
344	if (apmh < 0 || apmh >= NAPM_HOOK)
345		return NULL;
346
347	return apm_add_hook(&hook[apmh], ah);
348}
349
350/* disestablish an apm hook */
351void
352apm_hook_disestablish(int apmh, struct apmhook *ah)
353{
354	if (apmh < 0 || apmh >= NAPM_HOOK)
355		return;
356
357	apm_del_hook(&hook[apmh], ah);
358}
359
360
361static struct timeval suspend_time;
362static struct timeval diff_time;
363
364static int
365apm_default_resume(void *arg)
366{
367	int pl;
368	u_int second, minute, hour;
369	struct timeval resume_time, tmp_time;
370
371	/* modified for adjkerntz */
372	pl = splsoftclock();
373	inittodr(0);			/* adjust time to RTC */
374	microtime(&resume_time);
375	getmicrotime(&tmp_time);
376	timevaladd(&tmp_time, &diff_time);
377
378#ifdef FIXME
379	/* XXX THIS DOESN'T WORK!!! */
380	time = tmp_time;
381#endif
382
383#ifdef APM_FIXUP_CALLTODO
384	/* Calculate the delta time suspended */
385	timevalsub(&resume_time, &suspend_time);
386	/* Fixup the calltodo list with the delta time. */
387	adjust_timeout_calltodo(&resume_time);
388#endif /* APM_FIXUP_CALLTODOK */
389	splx(pl);
390#ifndef APM_FIXUP_CALLTODO
391	second = resume_time.tv_sec - suspend_time.tv_sec;
392#else /* APM_FIXUP_CALLTODO */
393	/*
394	 * We've already calculated resume_time to be the delta between
395	 * the suspend and the resume.
396	 */
397	second = resume_time.tv_sec;
398#endif /* APM_FIXUP_CALLTODO */
399	hour = second / 3600;
400	second %= 3600;
401	minute = second / 60;
402	second %= 60;
403	log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
404		hour, minute, second);
405	return 0;
406}
407
408static int
409apm_default_suspend(void *arg)
410{
411	int	pl;
412
413	pl = splsoftclock();
414	microtime(&diff_time);
415	inittodr(0);
416	microtime(&suspend_time);
417	timevalsub(&diff_time, &suspend_time);
418	splx(pl);
419	return 0;
420}
421
422static void apm_processevent(void);
423
424/*
425 * Public interface to the suspend/resume:
426 *
427 * Execute suspend and resume hook before and after sleep, respectively.
428 *
429 */
430
431void
432apm_suspend(int state)
433{
434	struct apm_softc *sc = &apm_softc;
435
436	if (!sc)
437		return;
438
439	if (sc->initialized) {
440		apm_execute_hook(hook[APM_HOOK_SUSPEND]);
441		if (apm_suspend_system(state) == 0)
442			apm_processevent();
443		else
444			/* Failure, 'resume' the system again */
445			apm_execute_hook(hook[APM_HOOK_RESUME]);
446	}
447}
448
449void
450apm_resume(void)
451{
452	struct apm_softc *sc = &apm_softc;
453
454	if (!sc)
455		return;
456
457	if (sc->initialized)
458		apm_execute_hook(hook[APM_HOOK_RESUME]);
459}
460
461
462/* get APM information */
463static int
464apm_get_info(apm_info_t aip)
465{
466	struct apm_softc *sc = &apm_softc;
467	u_long eax, ebx, ecx, edx;
468
469	eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
470	ebx = PMDV_ALLDEV;
471	ecx = 0;
472	edx = 0xffff;			/* default to unknown battery time */
473
474	if (apm_int(&eax, &ebx, &ecx, &edx))
475		return 1;
476
477	aip->ai_infoversion = 1;
478	aip->ai_acline      = (ebx >> 8) & 0xff;
479	aip->ai_batt_stat   = ebx & 0xff;
480	aip->ai_batt_life   = ecx & 0xff;
481	aip->ai_major       = (u_int)sc->majorversion;
482	aip->ai_minor       = (u_int)sc->minorversion;
483	aip->ai_status      = (u_int)sc->active;
484	edx &= 0xffff;
485	if (edx == 0xffff)	/* Time is unknown */
486		aip->ai_batt_time = -1;
487	else if (edx & 0x8000)	/* Time is in minutes */
488		aip->ai_batt_time = (edx & 0x7fff) * 60;
489	else			/* Time is in seconds */
490		aip->ai_batt_time = edx;
491
492	eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
493	ebx = 0;
494	ecx = 0;
495	edx = 0;
496	if (apm_int(&eax, &ebx, &ecx, &edx)) {
497		aip->ai_batteries = -1;	/* Unknown */
498		aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
499	} else {
500		aip->ai_batteries = ebx & 0xff;
501		aip->ai_capabilities = ecx & 0xf;
502	}
503
504	bzero(aip->ai_spare, sizeof aip->ai_spare);
505
506	return 0;
507}
508
509
510/* inform APM BIOS that CPU is idle */
511void
512apm_cpu_idle(void)
513{
514	struct apm_softc *sc = &apm_softc;
515
516	if (sc->active) {
517		u_long eax, ebx, ecx, edx;
518
519		eax = (APM_BIOS <<8) | APM_CPUIDLE;
520		edx = ecx = ebx = 0;
521		apm_int(&eax, &ebx, &ecx, &edx);
522	}
523	/*
524	 * Some APM implementation halts CPU in BIOS, whenever
525	 * "CPU-idle" function are invoked, but swtch() of
526	 * FreeBSD halts CPU, therefore, CPU is halted twice
527	 * in the sched loop. It makes the interrupt latency
528	 * terribly long and be able to cause a serious problem
529	 * in interrupt processing. We prevent it by removing
530	 * "hlt" operation from swtch() and managed it under
531	 * APM driver.
532	 */
533	if (!sc->active || sc->always_halt_cpu)
534		__asm("hlt");	/* wait for interrupt */
535}
536
537/* inform APM BIOS that CPU is busy */
538void
539apm_cpu_busy(void)
540{
541	struct apm_softc *sc = &apm_softc;
542
543	/*
544	 * The APM specification says this is only necessary if your BIOS
545	 * slows down the processor in the idle task, otherwise it's not
546	 * necessary.
547	 */
548	if (sc->slow_idle_cpu && sc->active) {
549		u_long eax, ebx, ecx, edx;
550
551		eax = (APM_BIOS <<8) | APM_CPUBUSY;
552		edx = ecx = ebx = 0;
553		apm_int(&eax, &ebx, &ecx, &edx);
554	}
555}
556
557
558/*
559 * APM timeout routine:
560 *
561 * This routine is automatically called by timer once per second.
562 */
563
564static void
565apm_timeout(void *dummy)
566{
567	struct apm_softc *sc = &apm_softc;
568
569	apm_processevent();
570	if (sc->active == 1)
571		/* Run slightly more oftan than 1 Hz */
572		apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1 );
573}
574
575/* enable APM BIOS */
576static void
577apm_event_enable(void)
578{
579	struct apm_softc *sc = &apm_softc;
580
581#ifdef APM_DEBUG
582	printf("called apm_event_enable()\n");
583#endif
584	if (sc->initialized) {
585		sc->active = 1;
586		apm_timeout(sc);
587	}
588}
589
590/* disable APM BIOS */
591static void
592apm_event_disable(void)
593{
594	struct apm_softc *sc = &apm_softc;
595
596#ifdef APM_DEBUG
597	printf("called apm_event_disable()\n");
598#endif
599	if (sc->initialized) {
600		untimeout(apm_timeout, NULL, apm_timeout_ch);
601		sc->active = 0;
602	}
603}
604
605/* halt CPU in scheduling loop */
606static void
607apm_halt_cpu(void)
608{
609	struct apm_softc *sc = &apm_softc;
610
611	if (sc->initialized)
612		sc->always_halt_cpu = 1;
613}
614
615/* don't halt CPU in scheduling loop */
616static void
617apm_not_halt_cpu(void)
618{
619	struct apm_softc *sc = &apm_softc;
620
621	if (sc->initialized)
622		sc->always_halt_cpu = 0;
623}
624
625/* device driver definitions */
626static int apmprobe (struct isa_device *);
627static int apmattach(struct isa_device *);
628struct isa_driver apmdriver = { apmprobe, apmattach, "apm" };
629
630/*
631 * probe APM (dummy):
632 *
633 * APM probing routine is placed on locore.s and apm_init.S because
634 * this process forces the CPU to turn to real mode or V86 mode.
635 * Current version uses real mode, but in a future version, we want
636 * to use V86 mode in APM initialization.
637 *
638 * XXX If VM86 is defined, we do.
639 */
640
641static int
642apmprobe(struct isa_device *dvp)
643{
644#ifdef VM86
645	struct vm86frame	vmf;
646	int			i;
647#endif
648
649	if ( dvp->id_unit > 0 ) {
650		printf("apm: Only one APM driver supported.\n");
651		return 0;
652	}
653
654#ifdef VM86
655	bzero(&vmf, sizeof(struct vm86frame));		/* safety */
656	vmf.vmf_ax = (APM_BIOS << 8) | APM_INSTCHECK;
657	vmf.vmf_bx = 0;
658	if (((i = vm86_intcall(SYSTEM_BIOS, &vmf)) == 0) &&
659	    !(vmf.vmf_eflags & PSL_C) &&
660	    (vmf.vmf_bx == 0x504d)) {
661
662		apm_version   = vmf.vmf_ax;
663		apm_flags     = vmf.vmf_cx;
664
665		vmf.vmf_ax = (APM_BIOS << 8) | APM_PROT32CONNECT;
666		vmf.vmf_bx = 0;
667		if (((i = vm86_intcall(SYSTEM_BIOS, &vmf)) == 0) &&
668		    !(vmf.vmf_eflags & PSL_C)) {
669
670			apm_cs32_base = vmf.vmf_ax;
671			apm_cs_entry  = vmf.vmf_ebx;
672			apm_cs16_base = vmf.vmf_cx;
673			apm_ds_base   = vmf.vmf_dx;
674			apm_cs32_limit  = vmf.vmf_si;
675			if (apm_version >= 0x0102)
676				apm_cs16_limit = (vmf.esi.r_ex >> 16);
677			apm_ds_limit  = vmf.vmf_di;
678#ifdef APM_DEBUG
679			printf("apm: BIOS probe/32-bit connect successful\n");
680#endif
681		} else {
682			/* XXX constant typo! */
683			if (vmf.vmf_ah == APME_PROT32NOTDUPPORTED) {
684				apm_version = APMINI_NOT32BIT;
685			} else {
686				apm_version = APMINI_CONNECTERR;
687			}
688#ifdef APM_DEBUG
689			printf("apm: BIOS 32-bit connect failed: error 0x%x  carry %d  ah 0x%x\n",
690			       i, (vmf.vmf_eflags & PSL_C) ? 1 : 0, vmf.vmf_ah);
691#endif
692		}
693	} else {
694		apm_version = APMINI_CANTFIND;
695#ifdef APM_DEBUG
696		printf("apm: BIOS probe failed: error 0x%x  carry %d  bx 0x%x\n",
697		       i, (vmf.vmf_eflags & PSL_C) ? 1 : 0, vmf.vmf_bx);
698#endif
699	}
700#endif
701
702	bzero(&apm_softc, sizeof(apm_softc));
703
704	switch (apm_version) {
705	case APMINI_CANTFIND:
706		/* silent */
707		return 0;
708	case APMINI_NOT32BIT:
709		printf("apm: 32bit connection is not supported.\n");
710		return 0;
711	case APMINI_CONNECTERR:
712		printf("apm: 32-bit connection error.\n");
713		return 0;
714	}
715	if (dvp->id_flags & 0x20)
716		statclock_disable = 1;
717	return -1;
718}
719
720
721/* Process APM event */
722static void
723apm_processevent(void)
724{
725	int apm_event;
726
727#ifdef APM_DEBUG
728#  define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
729	printf("Received APM Event: " #symbol "\n");
730#else
731#  define OPMEV_DEBUGMESSAGE(symbol) case symbol:
732#endif
733	do {
734		apm_event = apm_getevent();
735		switch (apm_event) {
736		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
737			apm_suspend(PMST_STANDBY);
738			break;
739		    OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
740			apm_suspend(PMST_SUSPEND);
741			break;
742		    OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
743			apm_suspend(PMST_SUSPEND);
744			break;
745		    OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
746			apm_suspend(PMST_SUSPEND);
747			break;
748		    OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
749			apm_resume();
750			break;
751		    OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
752			apm_resume();
753			break;
754		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
755			apm_resume();
756			break;
757		    OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
758			apm_battery_low();
759			apm_suspend(PMST_SUSPEND);
760			break;
761		    OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
762			break;
763		    OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
764			inittodr(0);	/* adjust time to RTC */
765			break;
766		    case PMEV_NOEVENT:
767			break;
768		    default:
769			printf("Unknown Original APM Event 0x%x\n", apm_event);
770			    break;
771		}
772	} while (apm_event != PMEV_NOEVENT);
773}
774
775/*
776 * Attach APM:
777 *
778 * Initialize APM driver (APM BIOS itself has been initialized in locore.s)
779 */
780
781static int
782apmattach(struct isa_device *dvp)
783{
784#define APM_KERNBASE	KERNBASE
785	struct apm_softc	*sc = &apm_softc;
786
787	sc->initialized = 0;
788
789	/* Must be externally enabled */
790	sc->active = 0;
791
792	/* setup APM parameters */
793	sc->cs16_base = (apm_cs16_base << 4) + APM_KERNBASE;
794	sc->cs32_base = (apm_cs32_base << 4) + APM_KERNBASE;
795	sc->ds_base = (apm_ds_base << 4) + APM_KERNBASE;
796	sc->cs32_limit = apm_cs32_limit - 1;
797	if (apm_cs16_limit == 0)
798	    apm_cs16_limit == apm_cs32_limit;
799	sc->cs16_limit = apm_cs16_limit - 1;
800	sc->ds_limit = apm_ds_limit - 1;
801	sc->cs_entry = apm_cs_entry;
802
803	/* Always call HLT in idle loop */
804	sc->always_halt_cpu = 1;
805
806	sc->slow_idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0);
807	sc->disabled = ((apm_flags & APM_DISABLED) != 0);
808	sc->disengaged = ((apm_flags & APM_DISENGAGED) != 0);
809
810	/* print bootstrap messages */
811#ifdef APM_DEBUG
812	printf("apm: APM BIOS version %04x\n",  apm_version);
813	printf("apm: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n",
814		sc->cs32_base, sc->cs16_base, sc->ds_base);
815	printf("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
816		sc->cs_entry, is_enabled(sc->slow_idle_cpu),
817		is_enabled(!sc->disabled));
818	printf("apm: CS32_limit=0x%x, CS16_limit=0x%x, DS_limit=0x%x\n",
819		(u_short)sc->cs32_limit, (u_short)sc->cs16_limit, (u_short)sc->ds_limit);
820#endif /* APM_DEBUG */
821
822#if 0
823	/* Workaround for some buggy APM BIOS implementations */
824	sc->cs_limit = 0xffff;
825	sc->ds_limit = 0xffff;
826#endif
827
828	/* setup GDT */
829	setup_apm_gdt(sc->cs32_base, sc->cs16_base, sc->ds_base,
830			sc->cs32_limit, sc->cs16_limit, sc->ds_limit);
831
832	/* setup entry point 48bit pointer */
833	apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL);
834	apm_addr.offset  = sc->cs_entry;
835
836	if ((dvp->id_flags & 0x10)) {
837		if ((dvp->id_flags & 0xf) >= 0x2) {
838			apm_driver_version(0x102);
839		}
840		if (!apm_version && (dvp->id_flags & 0xf) >= 0x1) {
841			apm_driver_version(0x101);
842		}
843	} else {
844		apm_driver_version(0x102);
845		if (!apm_version)
846			apm_driver_version(0x101);
847	}
848	if (!apm_version)
849		apm_version = 0x100;
850
851	sc->minorversion = ((apm_version & 0x00f0) >>  4) * 10 +
852			((apm_version & 0x000f) >> 0);
853	sc->majorversion = ((apm_version & 0xf000) >> 12) * 10 +
854			((apm_version & 0x0f00) >> 8);
855
856	sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
857
858#ifdef APM_DEBUG
859	if (sc->intversion >= INTVERSION(1, 1))
860		printf("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
861#endif
862
863	printf("apm: found APM BIOS version %d.%d\n",
864		sc->majorversion, sc->minorversion);
865
866#ifdef APM_DEBUG
867	printf("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
868#endif
869
870	/* enable power management */
871	if (sc->disabled) {
872		if (apm_enable_disable_pm(1)) {
873#ifdef APM_DEBUG
874			printf("apm: *Warning* enable function failed! [%x]\n",
875				apm_errno);
876#endif
877		}
878	}
879
880	/* engage power managment (APM 1.1 or later) */
881	if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
882		if (apm_engage_disengage_pm(1)) {
883#ifdef APM_DEBUG
884			printf("apm: *Warning* engage function failed err=[%x]",
885				apm_errno);
886			printf(" (Docked or using external power?).\n");
887#endif
888		}
889	}
890
891        /* default suspend hook */
892        sc->sc_suspend.ah_fun = apm_default_suspend;
893        sc->sc_suspend.ah_arg = sc;
894        sc->sc_suspend.ah_name = "default suspend";
895        sc->sc_suspend.ah_order = APM_MAX_ORDER;
896
897        /* default resume hook */
898        sc->sc_resume.ah_fun = apm_default_resume;
899        sc->sc_resume.ah_arg = sc;
900        sc->sc_resume.ah_name = "default resume";
901        sc->sc_resume.ah_order = APM_MIN_ORDER;
902
903        apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
904        apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
905
906	apm_event_enable();
907
908	/* Power the system off using APM */
909	at_shutdown_pri(apm_power_off, NULL, SHUTDOWN_FINAL, SHUTDOWN_PRI_LAST);
910
911	sc->initialized = 1;
912
913#ifdef DEVFS
914	sc->sc_devfs_token =
915		devfs_add_devswf(&apm_cdevsw, 0, DV_CHR, 0, 0, 0600, "apm");
916#endif
917	return 0;
918}
919
920static int
921apmopen(dev_t dev, int flag, int fmt, struct proc *p)
922{
923	struct apm_softc *sc = &apm_softc;
924
925	if (minor(dev) != 0 || !sc->initialized)
926		return (ENXIO);
927
928	return 0;
929}
930
931static int
932apmclose(dev_t dev, int flag, int fmt, struct proc *p)
933{
934	return 0;
935}
936
937static int
938apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
939{
940	struct apm_softc *sc = &apm_softc;
941	int error = 0;
942	int newstate;
943
944	if (minor(dev) != 0 || !sc->initialized)
945		return (ENXIO);
946#ifdef APM_DEBUG
947	printf("APM ioctl: cmd = 0x%x\n", cmd);
948#endif
949	switch (cmd) {
950	case APMIO_SUSPEND:
951		if (sc->active)
952			apm_suspend(PMST_SUSPEND);
953		else
954			error = EINVAL;
955		break;
956
957	case APMIO_STANDBY:
958		if (sc->active)
959			apm_suspend(PMST_STANDBY);
960		else
961			error = EINVAL;
962		break;
963
964	case APMIO_GETINFO_OLD:
965		{
966			struct apm_info info;
967			apm_info_old_t aiop;
968
969			if (apm_get_info(&info))
970				error = ENXIO;
971			aiop = (apm_info_old_t)addr;
972			aiop->ai_major = info.ai_major;
973			aiop->ai_minor = info.ai_minor;
974			aiop->ai_acline = info.ai_acline;
975			aiop->ai_batt_stat = info.ai_batt_stat;
976			aiop->ai_batt_life = info.ai_batt_life;
977			aiop->ai_status = info.ai_status;
978		}
979		break;
980	case APMIO_GETINFO:
981		if (apm_get_info((apm_info_t)addr))
982			error = ENXIO;
983		break;
984	case APMIO_ENABLE:
985		apm_event_enable();
986		break;
987	case APMIO_DISABLE:
988		apm_event_disable();
989		break;
990	case APMIO_HALTCPU:
991		apm_halt_cpu();
992		break;
993	case APMIO_NOTHALTCPU:
994		apm_not_halt_cpu();
995		break;
996	case APMIO_DISPLAY:
997		newstate = *(int *)addr;
998		if (apm_display(newstate))
999			error = ENXIO;
1000		break;
1001	case APMIO_BIOS:
1002		if (apm_bios_call((struct apm_bios_arg*)addr) == 0)
1003			((struct apm_bios_arg*)addr)->eax &= 0xff;
1004		break;
1005	default:
1006		error = EINVAL;
1007		break;
1008	}
1009	return error;
1010}
1011
1012
1013static apm_devsw_installed = 0;
1014
1015static void
1016apm_drvinit(void *unused)
1017{
1018	dev_t dev;
1019
1020	if( ! apm_devsw_installed ) {
1021		dev = makedev(CDEV_MAJOR,0);
1022		cdevsw_add(&dev,&apm_cdevsw,NULL);
1023		apm_devsw_installed = 1;
1024    	}
1025}
1026
1027SYSINIT(apmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,apm_drvinit,NULL)
1028