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