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