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