apm.c revision 16109
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@mt.cs.keio.ac.jp>
6 * Copyright (c) 1996 Nate Williams <nate@FreeBSD.org>
7 *
8 * This software may be used, modified, copied, and distributed, in
9 * both source and binary form provided that the above copyright and
10 * these terms are retained. Under no circumstances is the author
11 * responsible for the proper functioning of this software, nor does
12 * the author assume any responsibility for damages incurred with its
13 * use.
14 *
15 * Sep, 1994	Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
16 *
17 *	$Id: apm.c,v 1.42 1996/06/04 17:37:46 nate Exp $
18 */
19
20#include "apm.h"
21
22#if NAPM > 1
23#error only one APM device may be configured
24#endif
25
26#include <sys/param.h>
27#include <sys/conf.h>
28#include <sys/kernel.h>
29#ifdef DEVFS
30#include <sys/devfsext.h>
31#endif /*DEVFS*/
32#include <sys/systm.h>
33#include <sys/malloc.h>
34#include <sys/ioctl.h>
35#include <sys/file.h>
36#include <sys/proc.h>
37#include <sys/vnode.h>
38#include "i386/isa/isa.h"
39#include "i386/isa/isa_device.h"
40#include <machine/apm_bios.h>
41#include <machine/segments.h>
42#include <machine/clock.h>
43#include <vm/vm.h>
44#include <vm/vm_param.h>
45#include <vm/pmap.h>
46#include <sys/syslog.h>
47#include <sys/devconf.h>
48#include "apm_setup.h"
49
50static int apm_display_off __P((void));
51static int apm_int __P((u_long *eax, u_long *ebx, u_long *ecx));
52static void apm_resume __P((void));
53
54/* static data */
55struct apm_softc {
56	int	initialized, active;
57	int	always_halt_cpu, slow_idle_cpu;
58	int	disabled, disengaged;
59	u_int	minorversion, majorversion;
60	u_int	cs32_base, cs16_base, ds_base;
61	u_int	cs_limit, ds_limit;
62	u_int	cs_entry;
63	u_int	intversion;
64	struct apmhook sc_suspend;
65	struct apmhook sc_resume;
66#ifdef DEVFS
67	void 	*sc_devfs_token;
68#endif
69};
70
71static struct kern_devconf kdc_apm = {
72	0, 0, 0,		/* filled in by dev_attach */
73	"apm", 0, { MDDT_ISA, 0 },
74	isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
75	&kdc_isa0,		/* parent */
76	0,			/* parentdata */
77	DC_UNCONFIGURED,	/* state */
78	"Advanced Power Management BIOS",
79	DC_CLS_MISC		/* class */
80};
81
82
83static struct apm_softc apm_softc;
84static struct apmhook	*hook[NAPM_HOOK];		/* XXX */
85
86#define is_enabled(foo) ((foo) ? "enabled" : "disabled")
87
88/* Map version number to integer (keeps ordering of version numbers) */
89#define INTVERSION(major, minor)	((major)*100 + (minor))
90
91static timeout_t apm_timeout;
92static d_open_t apmopen;
93static d_close_t apmclose;
94static d_ioctl_t apmioctl;
95
96#define CDEV_MAJOR 39
97static struct cdevsw apm_cdevsw =
98	{ apmopen,	apmclose,	noread,		nowrite,	/*39*/
99	  apmioctl,	nostop,		nullreset,	nodevtotty,/* APM */
100	  seltrue,	nommap,		NULL ,	"apm"	,NULL,	-1};
101
102static void
103apm_registerdev(struct isa_device *id)
104{
105	if (kdc_apm.kdc_isa)
106		return;
107	kdc_apm.kdc_state = DC_UNCONFIGURED;
108	kdc_apm.kdc_unit = 0;
109	kdc_apm.kdc_isa = id;
110	dev_attach(&kdc_apm);
111}
112
113/* setup APM GDT discriptors */
114static void
115setup_apm_gdt(u_int code32_base, u_int code16_base, u_int data_base, u_int code_limit, u_int data_limit)
116{
117	/* setup 32bit code segment */
118	gdt_segs[GAPMCODE32_SEL].ssd_base  = code32_base;
119	gdt_segs[GAPMCODE32_SEL].ssd_limit = code_limit;
120
121	/* setup 16bit code segment */
122	gdt_segs[GAPMCODE16_SEL].ssd_base  = code16_base;
123	gdt_segs[GAPMCODE16_SEL].ssd_limit = code_limit;
124
125	/* setup data segment */
126	gdt_segs[GAPMDATA_SEL  ].ssd_base  = data_base;
127	gdt_segs[GAPMDATA_SEL  ].ssd_limit = data_limit;
128
129	/* reflect these changes on physical GDT */
130	ssdtosd(gdt_segs + GAPMCODE32_SEL, &gdt[GAPMCODE32_SEL].sd);
131	ssdtosd(gdt_segs + GAPMCODE16_SEL, &gdt[GAPMCODE16_SEL].sd);
132	ssdtosd(gdt_segs + GAPMDATA_SEL  , &gdt[GAPMDATA_SEL  ].sd);
133}
134
135/* 48bit far pointer */
136static struct addr48 {
137	u_long		offset;
138	u_short		segment;
139} apm_addr;
140
141static int apm_errno;
142
143inline
144int
145apm_int(u_long *eax, u_long *ebx, u_long *ecx)
146{
147	u_long cf;
148	__asm __volatile("
149		pushfl
150		cli
151		lcall	_apm_addr
152		movl	$0, %3
153		jnc	1f
154		incl	%3
155	1:
156		popfl
157		"
158		: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=D" (cf)
159		: "0" (*eax),  "1" (*ebx),  "2" (*ecx)
160		: "dx", "si", "memory"
161		);
162	apm_errno = ((*eax) >> 8) & 0xff;
163	return cf;
164}
165
166
167/* enable/disable power management */
168static int
169apm_enable_disable_pm(struct apm_softc *sc, int enable)
170{
171	u_long eax, ebx, ecx;
172
173	eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
174
175	if (sc->intversion >= INTVERSION(1, 1)) {
176		ebx  = PMDV_ALLDEV;
177	} else {
178		ebx  = 0xffff;	/* APM version 1.0 only */
179	}
180	ecx  = enable;
181	return apm_int(&eax, &ebx, &ecx);
182}
183
184/* Tell APM-BIOS that WE will do 1.1 and see what they say... */
185static void
186apm_driver_version(void)
187{
188	u_long eax, ebx, ecx;
189
190	eax = (APM_BIOS << 8) | APM_DRVVERSION;
191	ebx  = 0x0;
192	ecx  = 0x0101;
193	if(!apm_int(&eax, &ebx, &ecx))
194		apm_version = eax & 0xffff;
195}
196
197/* engage/disengage power management (APM 1.1 or later) */
198static int
199apm_engage_disengage_pm(struct apm_softc *sc, int engage)
200{
201	u_long eax, ebx, ecx;
202
203	eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
204	ebx = PMDV_ALLDEV;
205	ecx = engage;
206	return(apm_int(&eax, &ebx, &ecx));
207}
208
209/* get PM event */
210static u_int
211apm_getevent(struct apm_softc *sc)
212{
213	u_long eax, ebx, ecx;
214
215	eax = (APM_BIOS << 8) | APM_GETPMEVENT;
216
217	ebx = 0;
218	ecx = 0;
219	if (apm_int(&eax, &ebx, &ecx))
220		return PMEV_NOEVENT;
221
222	return ebx & 0xffff;
223}
224
225/* suspend entire system */
226static int
227apm_suspend_system(struct apm_softc *sc)
228{
229	u_long eax, ebx, ecx;
230
231	eax = (APM_BIOS << 8) | APM_SETPWSTATE;
232	ebx = PMDV_ALLDEV;
233	ecx = PMST_SUSPEND;
234
235	if (apm_int(&eax, &ebx, &ecx)) {
236		printf("Entire system suspend failure: errcode = %ld\n",
237			0xff & (eax >> 8));
238		return 1;
239	}
240	return 0;
241}
242
243/* Display control */
244/*
245 * Experimental implementation: My laptop machine can't handle this function
246 * If your laptop can control the display via APM, please inform me.
247 *                            HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp>
248 */
249static int
250apm_display_off(void)
251{
252	u_long eax, ebx, ecx;
253
254	eax = (APM_BIOS << 8) | APM_SETPWSTATE;
255	ebx = PMDV_2NDSTORAGE0;
256	ecx = PMST_STANDBY;
257	if (apm_int(&eax, &ebx, &ecx)) {
258		printf("Display off failure: errcode = %ld\n",
259			0xff & (eax >> 8));
260		return 1;
261	}
262
263	return 0;
264}
265
266/* APM Battery low handler */
267static void
268apm_battery_low(struct apm_softc *sc)
269{
270	printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
271}
272
273/* APM hook manager */
274static struct apmhook *
275apm_add_hook(struct apmhook **list, struct apmhook *ah)
276{
277	int s;
278	struct apmhook *p, *prev;
279
280#ifdef APM_DEBUG
281	printf("Add hook \"%s\"\n", ah->ah_name);
282#endif
283
284	s = splhigh();
285	if (ah == NULL) {
286		panic("illegal apm_hook!");
287	}
288	prev = NULL;
289	for (p = *list; p != NULL; prev = p, p = p->ah_next) {
290		if (p->ah_order > ah->ah_order) {
291			break;
292		}
293	}
294
295	if (prev == NULL) {
296		ah->ah_next = *list;
297		*list = ah;
298	} else {
299		ah->ah_next = prev->ah_next;
300		prev->ah_next = ah;
301	}
302	splx(s);
303	return ah;
304}
305
306static void
307apm_del_hook(struct apmhook **list, struct apmhook *ah)
308{
309	int s;
310	struct apmhook *p, *prev;
311
312	s = splhigh();
313	prev = NULL;
314	for (p = *list; p != NULL; prev = p, p = p->ah_next) {
315		if (p == ah) {
316			goto deleteit;
317		}
318	}
319	panic("Tried to delete unregistered apm_hook.");
320	goto nosuchnode;
321deleteit:
322	if (prev != NULL) {
323		prev->ah_next = p->ah_next;
324	} else {
325		*list = p->ah_next;
326	}
327nosuchnode:
328	splx(s);
329}
330
331
332/* APM driver calls some functions automatically */
333static void
334apm_execute_hook(struct apmhook *list)
335{
336	struct apmhook *p;
337
338	for (p = list; p != NULL; p = p->ah_next) {
339#ifdef APM_DEBUG
340		printf("Execute APM hook \"%s.\"\n", p->ah_name);
341#endif
342		if ((*(p->ah_fun))(p->ah_arg)) {
343			printf("Warning: APM hook \"%s\" failed", p->ah_name);
344		}
345	}
346}
347
348
349/* establish an apm hook */
350struct apmhook *
351apm_hook_establish(int apmh, struct apmhook *ah)
352{
353	if (apmh < 0 || apmh >= NAPM_HOOK)
354		return NULL;
355
356	return apm_add_hook(&hook[apmh], ah);
357}
358
359/* disestablish an apm hook */
360void
361apm_hook_disestablish(int apmh, struct apmhook *ah)
362{
363	if (apmh < 0 || apmh >= NAPM_HOOK)
364		return;
365
366	apm_del_hook(&hook[apmh], ah);
367}
368
369
370static struct timeval suspend_time;
371static struct timeval diff_time;
372
373static int
374apm_default_resume(void *arg)
375{
376	int pl;
377	u_int second, minute, hour;
378	struct timeval resume_time, tmp_time;
379
380	/* modified for adjkerntz */
381	pl = splsoftclock();
382	inittodr(0);			/* adjust time to RTC */
383	microtime(&resume_time);
384	tmp_time = time;		/* because 'time' is volatile */
385	timevaladd(&tmp_time, &diff_time);
386	time = tmp_time;
387	splx(pl);
388	second = resume_time.tv_sec - suspend_time.tv_sec;
389	hour = second / 3600;
390	second %= 3600;
391	minute = second / 60;
392	second %= 60;
393	log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
394		hour, minute, second);
395	return 0;
396}
397
398static int
399apm_default_suspend(void *arg)
400{
401	int	pl;
402
403	pl = splsoftclock();
404	microtime(&diff_time);
405	inittodr(0);
406	microtime(&suspend_time);
407	timevalsub(&diff_time, &suspend_time);
408	splx(pl);
409	return 0;
410}
411
412static void apm_processevent(struct apm_softc *);
413
414/*
415 * Public interface to the suspend/resume:
416 *
417 * Execute suspend and resume hook before and after sleep, respectively.
418 *
419 */
420
421void
422apm_suspend(void)
423{
424	struct apm_softc *sc = &apm_softc;
425
426	if (!sc)
427		return;
428
429	if (sc->initialized) {
430		apm_execute_hook(hook[APM_HOOK_SUSPEND]);
431		apm_suspend_system(sc);
432		apm_processevent(sc);
433	}
434}
435
436void
437apm_resume(void)
438{
439	struct apm_softc *sc = &apm_softc;
440
441	if (!sc)
442		return;
443
444	if (sc->initialized) {
445		apm_execute_hook(hook[APM_HOOK_RESUME]);
446	}
447}
448
449
450/* get APM information */
451static int
452apm_get_info(struct apm_softc *sc, apm_info_t aip)
453{
454	u_long eax, ebx, ecx;
455
456	eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
457	ebx = PMDV_ALLDEV;
458	ecx = 0;
459
460	if (apm_int(&eax, &ebx, &ecx))
461		return 1;
462
463	aip->ai_acline    = (ebx >> 8) & 0xff;
464	aip->ai_batt_stat = ebx & 0xff;
465	aip->ai_batt_life = ecx & 0xff;
466	aip->ai_major     = (u_int)sc->majorversion;
467	aip->ai_minor     = (u_int)sc->minorversion;
468	aip->ai_status    = (u_int)sc->active;
469
470	return 0;
471}
472
473
474/* inform APM BIOS that CPU is idle */
475void
476apm_cpu_idle(void)
477{
478	struct apm_softc *sc = &apm_softc;
479
480	if (sc->active) {
481		u_long eax, ebx, ecx;
482
483		eax = (APM_BIOS <<8) | APM_CPUIDLE;
484		ecx = ebx = 0;
485		apm_int(&eax, &ebx, &ecx);
486	}
487	/*
488	 * Some APM implementation halts CPU in BIOS, whenever
489	 * "CPU-idle" function are invoked, but swtch() of
490	 * FreeBSD halts CPU, therefore, CPU is halted twice
491	 * in the sched loop. It makes the interrupt latency
492	 * terribly long and be able to cause a serious problem
493	 * in interrupt processing. We prevent it by removing
494	 * "hlt" operation from swtch() and managed it under
495	 * APM driver.
496	 */
497	if (!sc->active || sc->always_halt_cpu) {
498		__asm("hlt");	/* wait for interrupt */
499	}
500}
501
502/* inform APM BIOS that CPU is busy */
503void
504apm_cpu_busy(void)
505{
506	struct apm_softc *sc = &apm_softc;
507
508	/*
509	 * The APM specification says this is only necessary if your BIOS
510	 * slows down the processor in the idle task, otherwise it's not
511	 * necessary.
512	 */
513	if (sc->slow_idle_cpu && sc->active) {
514		u_long eax, ebx, ecx;
515
516		eax = (APM_BIOS <<8) | APM_CPUBUSY;
517		ecx = ebx = 0;
518		apm_int(&eax, &ebx, &ecx);
519	}
520}
521
522
523/*
524 * APM timeout routine:
525 *
526 * This routine is automatically called by timer once per second.
527 */
528
529static void
530apm_timeout(void *arg)
531{
532	struct apm_softc *sc = arg;
533
534	apm_processevent(sc);
535	if (sc->active == 1) {
536		timeout(apm_timeout, (void *)sc, hz - 1 );  /* More than 1 Hz */
537	}
538}
539
540/* enable APM BIOS */
541static void
542apm_event_enable(struct apm_softc *sc)
543{
544#ifdef APM_DEBUG
545	printf("called apm_event_enable()\n");
546#endif
547	if (sc->initialized) {
548		sc->active = 1;
549		apm_timeout(sc);
550	}
551}
552
553/* disable APM BIOS */
554static void
555apm_event_disable(struct apm_softc *sc)
556{
557#ifdef APM_DEBUG
558	printf("called apm_event_disable()\n");
559#endif
560	if (sc->initialized) {
561		untimeout(apm_timeout, NULL);
562		sc->active = 0;
563	}
564}
565
566/* halt CPU in scheduling loop */
567static void
568apm_halt_cpu(struct apm_softc *sc)
569{
570	if (sc->initialized) {
571		sc->always_halt_cpu = 1;
572	}
573}
574
575/* don't halt CPU in scheduling loop */
576static void
577apm_not_halt_cpu(struct apm_softc *sc)
578{
579	if (sc->initialized) {
580		sc->always_halt_cpu = 0;
581	}
582}
583
584/* device driver definitions */
585static int apmprobe (struct isa_device *);
586static int apmattach(struct isa_device *);
587struct isa_driver apmdriver = {
588	apmprobe, apmattach, "apm" };
589
590/*
591 * probe APM (dummy):
592 *
593 * APM probing routine is placed on locore.s and apm_init.S because
594 * this process forces the CPU to turn to real mode or V86 mode.
595 * Current version uses real mode, but on future version, we want
596 * to use V86 mode in APM initialization.
597 */
598
599static int
600apmprobe(struct isa_device *dvp)
601{
602	if ( dvp->id_unit > 0 ) {
603		printf("apm: Only one APM driver supported.\n");
604		return 0;
605	}
606	apm_registerdev(dvp);
607	switch (apm_version) {
608	case APMINI_CANTFIND:
609		/* silent */
610		return 0;
611	case APMINI_NOT32BIT:
612		printf("apm: 32bit connection is not supported.\n");
613		return 0;
614	case APMINI_CONNECTERR:
615		printf("apm: 32-bit connection error.\n");
616		return 0;
617	}
618#ifdef APM_BROKEN_STATCLOCK
619	statclock_disable = 1;
620#endif
621
622	return -1;
623}
624
625
626/* Process APM event */
627static void
628apm_processevent(struct apm_softc *sc)
629{
630	int apm_event;
631
632#ifdef APM_DEBUG
633#  define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
634	printf("Received APM Event: " #symbol "\n");
635#else
636#  define OPMEV_DEBUGMESSAGE(symbol) case symbol:
637#endif
638	do {
639		apm_event = apm_getevent(sc);
640		switch (apm_event) {
641		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
642			apm_suspend();
643			break;
644		    OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
645			apm_suspend();
646			break;
647		    OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
648			apm_suspend();
649			break;
650		    OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
651			apm_suspend();
652			break;
653		    OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
654			apm_resume();
655			break;
656		    OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
657			apm_resume();
658			break;
659		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
660			apm_resume();
661			break;
662		    OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
663			apm_battery_low(sc);
664			apm_suspend();
665			break;
666		    OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
667			break;
668		    OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
669			inittodr(0);	/* adjust time to RTC */
670			break;
671		    OPMEV_DEBUGMESSAGE(PMEV_NOEVENT);
672			break;
673		    default:
674			printf("Unknown Original APM Event 0x%x\n", apm_event);
675			    break;
676		}
677	} while (apm_event != PMEV_NOEVENT);
678}
679
680/*
681 * Attach APM:
682 *
683 * Initialize APM driver (APM BIOS itself has been initialized in locore.s)
684 */
685
686static int
687apmattach(struct isa_device *dvp)
688{
689#define APM_KERNBASE	KERNBASE
690	struct apm_softc	*sc = &apm_softc;
691#ifdef APM_DSVALUE_BUG
692	caddr_t apm_bios_work;
693
694	apm_bios_work = (caddr_t)malloc(apm_ds_limit, M_DEVBUF, M_NOWAIT);
695	bcopy((caddr_t)((apm_ds_base << 4) + APM_KERNBASE), apm_bios_work,
696		apm_ds_limit);
697#endif /* APM_DSVALUE_BUG */
698
699	sc->initialized = 0;
700
701	/* Must be externally enabled */
702	sc->active = 0;
703
704	/* setup APM parameters */
705	sc->cs16_base = (apm_cs32_base << 4) + APM_KERNBASE;
706	sc->cs32_base = (apm_cs16_base << 4) + APM_KERNBASE;
707	sc->ds_base = (apm_ds_base << 4) + APM_KERNBASE;
708	sc->cs_limit = apm_cs_limit;
709	sc->ds_limit = apm_ds_limit;
710	sc->cs_entry = apm_cs_entry;
711
712#ifdef APM_DSVALUE_BUG
713	sc->ds_base = (u_int)apm_bios_work;
714#endif /* APM_DSVALUE_BUG */
715
716	/* Always call HLT in idle loop */
717	sc->always_halt_cpu = 1;
718
719	sc->slow_idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0);
720	sc->disabled = ((apm_flags & APM_DISABLED) != 0);
721	sc->disengaged = ((apm_flags & APM_DISENGAGED) != 0);
722
723	/* print bootstrap messages */
724#ifdef APM_DEBUG
725	printf("apm: APM BIOS version %04x\n",  apm_version);
726	printf("apm: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n",
727		sc->cs32_base, sc->cs16_base, sc->ds_base);
728	printf("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
729		sc->cs_entry, is_enabled(sc->slow_idle_cpu),
730		is_enabled(!sc->disabled));
731	printf("apm: CS_limit=%x, DS_limit=%x\n", sc->cs_limit, sc->ds_limit);
732#endif /* APM_DEBUG */
733
734#ifdef APM_DEBUG
735	/* Workaround for some buggy APM BIOS implementations */
736	sc->cs_limit = 0xffff;
737	sc->ds_limit = 0xffff;
738#endif
739
740	/* setup GDT */
741	setup_apm_gdt(sc->cs32_base, sc->cs16_base, sc->ds_base,
742			sc->cs_limit, sc->ds_limit);
743
744	/* setup entry point 48bit pointer */
745	apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL);
746	apm_addr.offset  = sc->cs_entry;
747
748#ifdef FORCE_APM10
749	apm_version = 0x100;
750	sc->majorversion = 1;
751	sc->minorversion = 0;
752	sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
753	printf("apm: running in APM 1.0 compatible mode\n");
754	kcd_apm.kdc_description =
755		"Advanced Power Management BIOS (1.0 compatability mode)",
756#else
757	/* Try to kick bios into 1.1 or greater mode */
758	apm_driver_version();
759	sc->minorversion = ((apm_version & 0x00f0) >>  4) * 10 +
760			((apm_version & 0x000f) >> 0);
761	sc->majorversion = ((apm_version & 0xf000) >> 12) * 10 +
762			((apm_version & 0x0f00) >> 8);
763
764	sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
765
766	if (sc->intversion >= INTVERSION(1, 1)) {
767#ifdef APM_DEBUG
768		printf("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
769#endif
770	}
771
772	printf("apm: found APM BIOS version %d.%d\n",
773		sc->majorversion, sc->minorversion);
774#endif /* FORCE_APM10 */
775
776#ifdef APM_DEBUG
777	printf("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
778#endif
779
780	/* enable power management */
781	if (sc->disabled) {
782		if (apm_enable_disable_pm(sc, 1)) {
783#ifdef APM_DEBUG
784			printf("apm: *Warning* enable function failed! [%x]\n",
785				apm_errno);
786#endif
787		}
788	}
789
790	/* engage power managment (APM 1.1 or later) */
791	if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
792		if (apm_engage_disengage_pm(sc, 1)) {
793#ifdef APM_DEBUG
794			printf("apm: *Warning* engage function failed err=[%x]",
795				apm_errno);
796			printf(" (Docked or using external power?).\n");
797#endif
798		}
799	}
800
801        /* default suspend hook */
802        sc->sc_suspend.ah_fun = apm_default_suspend;
803        sc->sc_suspend.ah_arg = sc;
804        sc->sc_suspend.ah_name = "default suspend";
805        sc->sc_suspend.ah_order = APM_MAX_ORDER;
806
807        /* default resume hook */
808        sc->sc_resume.ah_fun = apm_default_resume;
809        sc->sc_resume.ah_arg = sc;
810        sc->sc_resume.ah_name = "default resume";
811        sc->sc_resume.ah_order = APM_MIN_ORDER;
812
813        apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
814        apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
815
816	apm_event_enable(sc);
817	kdc_apm.kdc_state = DC_IDLE;
818
819	sc->initialized = 1;
820
821#ifdef DEVFS
822	sc->sc_devfs_token =
823		devfs_add_devswf(&apm_cdevsw, 0, DV_CHR, 0, 0, 0600, "apm");
824#endif
825	return 0;
826}
827
828static int
829apmopen(dev_t dev, int flag, int fmt, struct proc *p)
830{
831	struct apm_softc *sc = &apm_softc;
832
833	if (minor(dev) != 0 || !sc->initialized)
834		return (ENXIO);
835
836	return 0;
837}
838
839static int
840apmclose(dev_t dev, int flag, int fmt, struct proc *p)
841{
842	return 0;
843}
844
845static int
846apmioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
847{
848	struct apm_softc *sc = &apm_softc;
849	int error = 0;
850
851	if (minor(dev) != 0 || !sc->initialized)
852		return (ENXIO);
853#ifdef APM_DEBUG
854	printf("APM ioctl: cmd = 0x%x\n", cmd);
855#endif
856	switch (cmd) {
857	case APMIO_SUSPEND:
858		if ( sc->active) {
859			apm_suspend();
860		} else {
861			error = EINVAL;
862		}
863		break;
864	case APMIO_GETINFO:
865		if (apm_get_info(sc, (apm_info_t)addr)) {
866			error = ENXIO;
867		}
868		break;
869	case APMIO_ENABLE:
870		kdc_apm.kdc_state = DC_BUSY;
871		apm_event_enable(sc);
872		break;
873	case APMIO_DISABLE:
874		kdc_apm.kdc_state = DC_IDLE;
875		apm_event_disable(sc);
876		break;
877	case APMIO_HALTCPU:
878		apm_halt_cpu(sc);
879		break;
880	case APMIO_NOTHALTCPU:
881		apm_not_halt_cpu(sc);
882		break;
883	case APMIO_DISPLAYOFF:
884		if (apm_display_off()) {
885			error = ENXIO;
886		}
887		break;
888	default:
889		error = EINVAL;
890		break;
891	}
892	return error;
893}
894
895
896static apm_devsw_installed = 0;
897
898static void
899apm_drvinit(void *unused)
900{
901	dev_t dev;
902
903	if( ! apm_devsw_installed ) {
904		dev = makedev(CDEV_MAJOR,0);
905		cdevsw_add(&dev,&apm_cdevsw,NULL);
906		apm_devsw_installed = 1;
907    	}
908}
909
910SYSINIT(apmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,apm_drvinit,NULL)
911