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