apm.c revision 116665
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
19#include <sys/cdefs.h>
20__FBSDID("$FreeBSD: head/sys/i386/bios/apm.c 116665 2003-06-22 05:08:10Z mdodd $");
21
22#include <sys/param.h>
23#include <sys/systm.h>
24#include <sys/bus.h>
25#include <sys/conf.h>
26#include <sys/eventhandler.h>
27#include <sys/fcntl.h>
28#include <sys/kernel.h>
29#include <sys/poll.h>
30#include <sys/power.h>
31#include <sys/reboot.h>
32#include <sys/selinfo.h>
33#include <sys/signalvar.h>
34#include <sys/sysctl.h>
35#include <sys/syslog.h>
36#include <sys/time.h>
37#include <sys/uio.h>
38
39#include <machine/apm_bios.h>
40#include <machine/clock.h>
41#include <machine/endian.h>
42#include <machine/pc/bios.h>
43#include <machine/cpufunc.h>
44#include <machine/segments.h>
45#include <machine/stdarg.h>
46#include <machine/vm86.h>
47
48#include <machine/bus.h>
49#include <machine/resource.h>
50#include <sys/rman.h>
51
52#include <vm/vm.h>
53#include <vm/pmap.h>
54#include <vm/vm_param.h>
55
56#include <i386/bios/apm.h>
57
58/* Used by the apm_saver screen saver module */
59int apm_display(int newstate);
60struct apm_softc apm_softc;
61
62static void apm_resume(void);
63static int apm_bioscall(void);
64static int apm_check_function_supported(u_int version, u_int func);
65
66static int apm_pm_func(u_long, void*, ...);
67
68static u_long	apm_version;
69
70int	apm_evindex;
71
72#define	SCFLAG_ONORMAL	0x0000001
73#define	SCFLAG_OCTL	0x0000002
74#define	SCFLAG_OPEN	(SCFLAG_ONORMAL|SCFLAG_OCTL)
75
76#define APMDEV(dev)	(minor(dev)&0x0f)
77#define APMDEV_NORMAL	0
78#define APMDEV_CTL	8
79
80#ifdef PC98
81extern int bios32_apm98(struct bios_regs *, u_int, u_short);
82
83/* PC98's SMM definition */
84#define	APM_NECSMM_PORT		0x6b8e
85#define	APM_NECSMM_PORTSZ	1
86#define	APM_NECSMM_EN		0x10
87static __inline void apm_enable_smm(struct apm_softc *);
88static __inline void apm_disable_smm(struct apm_softc *);
89int apm_necsmm_addr;
90u_int32_t apm_necsmm_mask;
91#endif
92
93static struct apmhook	*hook[NAPM_HOOK];		/* XXX */
94
95#define is_enabled(foo) ((foo) ? "enabled" : "disabled")
96
97/* Map version number to integer (keeps ordering of version numbers) */
98#define INTVERSION(major, minor)	((major)*100 + (minor))
99
100static struct callout_handle apm_timeout_ch =
101    CALLOUT_HANDLE_INITIALIZER(&apm_timeout_ch);
102
103static timeout_t apm_timeout;
104static d_open_t apmopen;
105static d_close_t apmclose;
106static d_write_t apmwrite;
107static d_ioctl_t apmioctl;
108static d_poll_t apmpoll;
109
110#define CDEV_MAJOR 39
111static struct cdevsw apm_cdevsw = {
112	.d_open =	apmopen,
113	.d_close =	apmclose,
114	.d_write =	apmwrite,
115	.d_ioctl =	apmioctl,
116	.d_poll =	apmpoll,
117	.d_name =	"apm",
118	.d_maj =	CDEV_MAJOR,
119};
120
121static int apm_suspend_delay = 1;
122static int apm_standby_delay = 1;
123static int apm_swab_batt_minutes = 0;
124static int apm_debug = 0;
125
126#define APM_DPRINT(args...) do	{					\
127	if (apm_debug) {						\
128		printf(args);						\
129	}								\
130} while (0)
131
132SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
133SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
134SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
135
136TUNABLE_INT("machdep.apm_swab_batt_minutes", &apm_swab_batt_minutes);
137SYSCTL_INT(_machdep, OID_AUTO, apm_swab_batt_minutes, CTLFLAG_RW,
138	   &apm_swab_batt_minutes, 0, "Byte swap battery time value.");
139
140#ifdef PC98
141static __inline void
142apm_enable_smm(sc)
143	struct apm_softc *sc;
144{
145	bus_space_tag_t iot = sc->sc_iot;
146	bus_space_handle_t ioh = sc->sc_ioh;
147	if (apm_necsmm_addr != 0)
148		bus_space_write_1(iot, ioh, 0,
149			  (bus_space_read_1(iot, ioh, 0) | ~apm_necsmm_mask));
150}
151
152static __inline void
153apm_disable_smm(sc)
154	struct apm_softc *sc;
155{
156	bus_space_tag_t iot = sc->sc_iot;
157	bus_space_handle_t ioh = sc->sc_ioh;
158	if (apm_necsmm_addr != 0)
159		bus_space_write_1(iot, ioh, 0,
160			  (bus_space_read_1(iot, ioh, 0) & apm_necsmm_mask));
161}
162#endif
163
164/*
165 * return  0 if the function successfull,
166 * return  1 if the function unsuccessfull,
167 * return -1 if the function unsupported.
168 */
169static int
170apm_bioscall(void)
171{
172	struct apm_softc *sc = &apm_softc;
173	int errno = 0;
174	u_int apm_func = sc->bios.r.eax & 0xff;
175
176	if (!apm_check_function_supported(sc->intversion, apm_func)) {
177		APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
178		    apm_func, sc->majorversion, sc->minorversion);
179		return (-1);
180	}
181
182	sc->bios_busy = 1;
183#ifdef	PC98
184	set_bios_selectors(&sc->bios.seg, BIOSCODE_FLAG | BIOSDATA_FLAG);
185	if (bios32_apm98(&sc->bios.r, sc->bios.entry,
186	    GSEL(GBIOSCODE32_SEL, SEL_KPL)) != 0)
187		return 1;
188#else
189	if (sc->connectmode == APM_PROT32CONNECT) {
190		set_bios_selectors(&sc->bios.seg,
191				   BIOSCODE_FLAG | BIOSDATA_FLAG);
192		errno = bios32(&sc->bios.r,
193			       sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
194	} else {
195		errno = bios16(&sc->bios, NULL);
196	}
197#endif
198	sc->bios_busy = 0;
199	return (errno);
200}
201
202/* check whether APM function is supported (1)  or not (0). */
203static int
204apm_check_function_supported(u_int version, u_int func)
205{
206	/* except driver version */
207	if (func == APM_DRVVERSION) {
208		return (1);
209	}
210#ifdef PC98
211	if (func == APM_GETPWSTATUS) {
212		return (1);
213	}
214#endif
215
216	switch (version) {
217	case INTVERSION(1, 0):
218		if (func > APM_GETPMEVENT) {
219			return (0); /* not supported */
220		}
221		break;
222	case INTVERSION(1, 1):
223		if (func > APM_ENGAGEDISENGAGEPM &&
224		    func < APM_OEMFUNC) {
225			return (0); /* not supported */
226		}
227		break;
228	case INTVERSION(1, 2):
229		break;
230	}
231
232	return (1); /* supported */
233}
234
235/* enable/disable power management */
236static int
237apm_enable_disable_pm(int enable)
238{
239	struct apm_softc *sc = &apm_softc;
240
241	sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
242
243	if (sc->intversion >= INTVERSION(1, 1))
244		sc->bios.r.ebx  = PMDV_ALLDEV;
245	else
246		sc->bios.r.ebx  = 0xffff;	/* APM version 1.0 only */
247	sc->bios.r.ecx  = enable;
248	sc->bios.r.edx = 0;
249	return (apm_bioscall());
250}
251
252/* register driver version (APM 1.1 or later) */
253static int
254apm_driver_version(int version)
255{
256	struct apm_softc *sc = &apm_softc;
257
258	sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
259	sc->bios.r.ebx  = 0x0;
260	sc->bios.r.ecx  = version;
261	sc->bios.r.edx = 0;
262
263	if (apm_bioscall() == 0 && sc->bios.r.eax == version)
264		return (0);
265
266	/* Some old BIOSes don't return the connection version in %ax. */
267	if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
268		return (0);
269
270	return (1);
271}
272
273/* engage/disengage power management (APM 1.1 or later) */
274static int
275apm_engage_disengage_pm(int engage)
276{
277	struct apm_softc *sc = &apm_softc;
278
279	sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
280	sc->bios.r.ebx = PMDV_ALLDEV;
281	sc->bios.r.ecx = engage;
282	sc->bios.r.edx = 0;
283	return (apm_bioscall());
284}
285
286/* get PM event */
287static u_int
288apm_getevent(void)
289{
290	struct apm_softc *sc = &apm_softc;
291
292	sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
293
294	sc->bios.r.ebx = 0;
295	sc->bios.r.ecx = 0;
296	sc->bios.r.edx = 0;
297	if (apm_bioscall())
298		return (PMEV_NOEVENT);
299	return (sc->bios.r.ebx & 0xffff);
300}
301
302/* suspend entire system */
303static int
304apm_suspend_system(int state)
305{
306	struct apm_softc *sc = &apm_softc;
307
308	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
309	sc->bios.r.ebx = PMDV_ALLDEV;
310	sc->bios.r.ecx = state;
311	sc->bios.r.edx = 0;
312
313#ifdef PC98
314	apm_disable_smm(sc);
315#endif
316	if (apm_bioscall()) {
317 		printf("Entire system suspend failure: errcode = %d\n",
318		       0xff & (sc->bios.r.eax >> 8));
319 		return 1;
320 	}
321#ifdef PC98
322	apm_enable_smm(sc);
323#endif
324 	return 0;
325}
326
327/* Display control */
328/*
329 * Experimental implementation: My laptop machine can't handle this function
330 * If your laptop can control the display via APM, please inform me.
331 *                            HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
332 */
333int
334apm_display(int newstate)
335{
336	struct apm_softc *sc = &apm_softc;
337
338	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
339	sc->bios.r.ebx = PMDV_DISP0;
340	sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
341	sc->bios.r.edx = 0;
342	if (apm_bioscall() == 0) {
343		return 0;
344 	}
345
346	/* If failed, then try to blank all display devices instead. */
347	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
348	sc->bios.r.ebx = PMDV_DISPALL;	/* all display devices */
349	sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
350	sc->bios.r.edx = 0;
351	if (apm_bioscall() == 0) {
352		return 0;
353 	}
354 	printf("Display off failure: errcode = %d\n",
355	       0xff & (sc->bios.r.eax >> 8));
356 	return 1;
357}
358
359/*
360 * Turn off the entire system.
361 */
362static void
363apm_power_off(void *junk, int howto)
364{
365	struct apm_softc *sc = &apm_softc;
366
367	/* Not halting powering off, or not active */
368	if (!(howto & RB_POWEROFF) || !apm_softc.active)
369		return;
370	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
371	sc->bios.r.ebx = PMDV_ALLDEV;
372	sc->bios.r.ecx = PMST_OFF;
373	sc->bios.r.edx = 0;
374	(void) apm_bioscall();
375}
376
377/* APM Battery low handler */
378static void
379apm_battery_low(void)
380{
381	printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
382}
383
384/* APM hook manager */
385static struct apmhook *
386apm_add_hook(struct apmhook **list, struct apmhook *ah)
387{
388	int s;
389	struct apmhook *p, *prev;
390
391	APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
392
393	s = splhigh();
394	if (ah == NULL)
395		panic("illegal apm_hook!");
396	prev = NULL;
397	for (p = *list; p != NULL; prev = p, p = p->ah_next)
398		if (p->ah_order > ah->ah_order)
399			break;
400
401	if (prev == NULL) {
402		ah->ah_next = *list;
403		*list = ah;
404	} else {
405		ah->ah_next = prev->ah_next;
406		prev->ah_next = ah;
407	}
408	splx(s);
409	return ah;
410}
411
412static void
413apm_del_hook(struct apmhook **list, struct apmhook *ah)
414{
415	int s;
416	struct apmhook *p, *prev;
417
418	s = splhigh();
419	prev = NULL;
420	for (p = *list; p != NULL; prev = p, p = p->ah_next)
421		if (p == ah)
422			goto deleteit;
423	panic("Tried to delete unregistered apm_hook.");
424	goto nosuchnode;
425deleteit:
426	if (prev != NULL)
427		prev->ah_next = p->ah_next;
428	else
429		*list = p->ah_next;
430nosuchnode:
431	splx(s);
432}
433
434
435/* APM driver calls some functions automatically */
436static void
437apm_execute_hook(struct apmhook *list)
438{
439	struct apmhook *p;
440
441	for (p = list; p != NULL; p = p->ah_next) {
442		APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
443		if ((*(p->ah_fun))(p->ah_arg))
444			printf("Warning: APM hook \"%s\" failed", p->ah_name);
445	}
446}
447
448
449/* establish an apm hook */
450struct apmhook *
451apm_hook_establish(int apmh, struct apmhook *ah)
452{
453	if (apmh < 0 || apmh >= NAPM_HOOK)
454		return NULL;
455
456	return apm_add_hook(&hook[apmh], ah);
457}
458
459/* disestablish an apm hook */
460void
461apm_hook_disestablish(int apmh, struct apmhook *ah)
462{
463	if (apmh < 0 || apmh >= NAPM_HOOK)
464		return;
465
466	apm_del_hook(&hook[apmh], ah);
467}
468
469static int apm_record_event(struct apm_softc *, u_int);
470static void apm_processevent(void);
471
472static u_int apm_op_inprog = 0;
473
474static void
475apm_do_suspend(void)
476{
477	struct apm_softc *sc = &apm_softc;
478	int error;
479
480	if (!sc)
481		return;
482
483	apm_op_inprog = 0;
484	sc->suspends = sc->suspend_countdown = 0;
485
486	if (sc->initialized) {
487		error = DEVICE_SUSPEND(root_bus);
488		if (error) {
489			DEVICE_RESUME(root_bus);
490		} else {
491			apm_execute_hook(hook[APM_HOOK_SUSPEND]);
492			if (apm_suspend_system(PMST_SUSPEND) == 0) {
493				sc->suspending = 1;
494				apm_processevent();
495			} else {
496				/* Failure, 'resume' the system again */
497				apm_execute_hook(hook[APM_HOOK_RESUME]);
498				DEVICE_RESUME(root_bus);
499			}
500		}
501	}
502}
503
504static void
505apm_do_standby(void)
506{
507	struct apm_softc *sc = &apm_softc;
508
509	if (!sc)
510		return;
511
512	apm_op_inprog = 0;
513	sc->standbys = sc->standby_countdown = 0;
514
515	if (sc->initialized) {
516		/*
517		 * As far as standby, we don't need to execute
518		 * all of suspend hooks.
519		 */
520		if (apm_suspend_system(PMST_STANDBY) == 0)
521			apm_processevent();
522	}
523}
524
525static void
526apm_lastreq_notify(void)
527{
528	struct apm_softc *sc = &apm_softc;
529
530	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
531	sc->bios.r.ebx = PMDV_ALLDEV;
532	sc->bios.r.ecx = PMST_LASTREQNOTIFY;
533	sc->bios.r.edx = 0;
534	apm_bioscall();
535}
536
537static int
538apm_lastreq_rejected(void)
539{
540	struct apm_softc *sc = &apm_softc;
541
542	if (apm_op_inprog == 0) {
543		return 1;	/* no operation in progress */
544	}
545
546	sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
547	sc->bios.r.ebx = PMDV_ALLDEV;
548	sc->bios.r.ecx = PMST_LASTREQREJECT;
549	sc->bios.r.edx = 0;
550
551	if (apm_bioscall()) {
552		APM_DPRINT("apm_lastreq_rejected: failed\n");
553		return 1;
554	}
555	apm_op_inprog = 0;
556	return 0;
557}
558
559/*
560 * Public interface to the suspend/resume:
561 *
562 * Execute suspend and resume hook before and after sleep, respectively.
563 *
564 */
565
566void
567apm_suspend(int state)
568{
569	struct apm_softc *sc = &apm_softc;
570
571	if (!sc->initialized)
572		return;
573
574	switch (state) {
575	case PMST_SUSPEND:
576		if (sc->suspends)
577			return;
578		sc->suspends++;
579		sc->suspend_countdown = apm_suspend_delay;
580		break;
581	case PMST_STANDBY:
582		if (sc->standbys)
583			return;
584		sc->standbys++;
585		sc->standby_countdown = apm_standby_delay;
586		break;
587	default:
588		printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
589		return;
590	}
591
592	apm_op_inprog++;
593	apm_lastreq_notify();
594}
595
596static void
597apm_resume(void)
598{
599	struct apm_softc *sc = &apm_softc;
600
601	if (!sc)
602		return;
603
604	if (sc->suspending == 0)
605		return;
606
607	sc->suspending = 0;
608	if (sc->initialized) {
609		apm_execute_hook(hook[APM_HOOK_RESUME]);
610		DEVICE_RESUME(root_bus);
611	}
612}
613
614
615/* get power status per battery */
616static int
617apm_get_pwstatus(apm_pwstatus_t app)
618{
619	struct apm_softc *sc = &apm_softc;
620
621	if (app->ap_device != PMDV_ALLDEV &&
622	    (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
623		return 1;
624
625	sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
626	sc->bios.r.ebx = app->ap_device;
627	sc->bios.r.ecx = 0;
628	sc->bios.r.edx = 0xffff;	/* default to unknown battery time */
629
630	if (apm_bioscall())
631		return 1;
632
633	app->ap_acline    = (sc->bios.r.ebx >> 8) & 0xff;
634	app->ap_batt_stat = sc->bios.r.ebx & 0xff;
635	app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
636	app->ap_batt_life = sc->bios.r.ecx & 0xff;
637	sc->bios.r.edx &= 0xffff;
638	if (apm_swab_batt_minutes)
639		sc->bios.r.edx = __bswap16(sc->bios.r.edx) | 0x8000;
640	if (sc->bios.r.edx == 0xffff)	/* Time is unknown */
641		app->ap_batt_time = -1;
642	else if (sc->bios.r.edx & 0x8000)	/* Time is in minutes */
643		app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
644	else				/* Time is in seconds */
645		app->ap_batt_time = sc->bios.r.edx;
646
647	return 0;
648}
649
650
651/* get APM information */
652static int
653apm_get_info(apm_info_t aip)
654{
655	struct apm_softc *sc = &apm_softc;
656	struct apm_pwstatus aps;
657
658	bzero(&aps, sizeof(aps));
659	aps.ap_device = PMDV_ALLDEV;
660	if (apm_get_pwstatus(&aps))
661		return 1;
662
663	aip->ai_infoversion = 1;
664	aip->ai_acline      = aps.ap_acline;
665	aip->ai_batt_stat   = aps.ap_batt_stat;
666	aip->ai_batt_life   = aps.ap_batt_life;
667	aip->ai_batt_time   = aps.ap_batt_time;
668	aip->ai_major       = (u_int)sc->majorversion;
669	aip->ai_minor       = (u_int)sc->minorversion;
670	aip->ai_status      = (u_int)sc->active;
671
672	sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
673	sc->bios.r.ebx = 0;
674	sc->bios.r.ecx = 0;
675	sc->bios.r.edx = 0;
676	if (apm_bioscall()) {
677		aip->ai_batteries = -1;	/* Unknown */
678		aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
679	} else {
680		aip->ai_batteries = sc->bios.r.ebx & 0xff;
681		aip->ai_capabilities = sc->bios.r.ecx & 0xf;
682	}
683
684	bzero(aip->ai_spare, sizeof aip->ai_spare);
685
686	return 0;
687}
688
689
690/* inform APM BIOS that CPU is idle */
691void
692apm_cpu_idle(void)
693{
694	struct apm_softc *sc = &apm_softc;
695
696	if (sc->active) {
697
698		sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
699		sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
700		(void) apm_bioscall();
701	}
702	/*
703	 * Some APM implementation halts CPU in BIOS, whenever
704	 * "CPU-idle" function are invoked, but swtch() of
705	 * FreeBSD halts CPU, therefore, CPU is halted twice
706	 * in the sched loop. It makes the interrupt latency
707	 * terribly long and be able to cause a serious problem
708	 * in interrupt processing. We prevent it by removing
709	 * "hlt" operation from swtch() and managed it under
710	 * APM driver.
711	 */
712	if (!sc->active || sc->always_halt_cpu)
713		halt();	/* wait for interrupt */
714}
715
716/* inform APM BIOS that CPU is busy */
717void
718apm_cpu_busy(void)
719{
720	struct apm_softc *sc = &apm_softc;
721
722	/*
723	 * The APM specification says this is only necessary if your BIOS
724	 * slows down the processor in the idle task, otherwise it's not
725	 * necessary.
726	 */
727	if (sc->slow_idle_cpu && sc->active) {
728
729		sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
730		sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
731		apm_bioscall();
732	}
733}
734
735
736/*
737 * APM timeout routine:
738 *
739 * This routine is automatically called by timer once per second.
740 */
741
742static void
743apm_timeout(void *dummy)
744{
745	struct apm_softc *sc = &apm_softc;
746
747	if (apm_op_inprog)
748		apm_lastreq_notify();
749
750	if (sc->standbys && sc->standby_countdown-- <= 0)
751		apm_do_standby();
752
753	if (sc->suspends && sc->suspend_countdown-- <= 0)
754		apm_do_suspend();
755
756	if (!sc->bios_busy)
757		apm_processevent();
758
759	if (sc->active == 1)
760		/* Run slightly more oftan than 1 Hz */
761		apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1);
762}
763
764/* enable APM BIOS */
765static void
766apm_event_enable(void)
767{
768	struct apm_softc *sc = &apm_softc;
769
770	APM_DPRINT("called apm_event_enable()\n");
771	if (sc->initialized) {
772		sc->active = 1;
773		apm_timeout(sc);
774	}
775}
776
777/* disable APM BIOS */
778static void
779apm_event_disable(void)
780{
781	struct apm_softc *sc = &apm_softc;
782
783	APM_DPRINT("called apm_event_disable()\n");
784	if (sc->initialized) {
785		untimeout(apm_timeout, NULL, apm_timeout_ch);
786		sc->active = 0;
787	}
788}
789
790/* halt CPU in scheduling loop */
791static void
792apm_halt_cpu(void)
793{
794	struct apm_softc *sc = &apm_softc;
795
796	if (sc->initialized)
797		sc->always_halt_cpu = 1;
798}
799
800/* don't halt CPU in scheduling loop */
801static void
802apm_not_halt_cpu(void)
803{
804	struct apm_softc *sc = &apm_softc;
805
806	if (sc->initialized)
807		sc->always_halt_cpu = 0;
808}
809
810/* device driver definitions */
811
812/*
813 * Module event
814 */
815
816static int
817apm_modevent(struct module *mod, int event, void *junk)
818{
819
820	switch (event) {
821	case MOD_LOAD:
822		if (!cold)
823			return (EPERM);
824		break;
825	case MOD_UNLOAD:
826		if (!cold && power_pm_get_type() == POWER_PM_TYPE_APM)
827			return (EBUSY);
828		break;
829	default:
830		break;
831	}
832
833	return (0);
834}
835
836/*
837 * Create "connection point"
838 */
839static void
840apm_identify(driver_t *driver, device_t parent)
841{
842	device_t child;
843
844	if (!cold) {
845		printf("Don't load this driver from userland!!\n");
846		return;
847	}
848
849	child = BUS_ADD_CHILD(parent, 0, "apm", 0);
850	if (child == NULL)
851		panic("apm_identify");
852}
853
854/*
855 * probe for APM BIOS
856 */
857static int
858apm_probe(device_t dev)
859{
860#define APM_KERNBASE	KERNBASE
861	struct vm86frame	vmf;
862	struct apm_softc	*sc = &apm_softc;
863	int			disabled, flags;
864#ifdef PC98
865	int			rid;
866#endif
867
868	device_set_desc(dev, "APM BIOS");
869
870	if (resource_int_value("apm", 0, "disabled", &disabled) != 0)
871		disabled = 0;
872	if (disabled)
873		return ENXIO;
874
875	if (device_get_unit(dev) > 0) {
876		printf("apm: Only one APM driver supported.\n");
877		return ENXIO;
878	}
879
880	if (power_pm_get_type() != POWER_PM_TYPE_NONE &&
881	    power_pm_get_type() != POWER_PM_TYPE_APM) {
882		printf("apm: Other PM system enabled.\n");
883		return ENXIO;
884	}
885
886	if (resource_int_value("apm", 0, "flags", &flags) != 0)
887		flags = 0;
888
889	bzero(&vmf, sizeof(struct vm86frame));		/* safety */
890	bzero(&apm_softc, sizeof(apm_softc));
891	vmf.vmf_ah = APM_BIOS;
892	vmf.vmf_al = APM_INSTCHECK;
893	vmf.vmf_bx = 0;
894	if (vm86_intcall(APM_INT, &vmf))
895		return ENXIO;			/* APM not found */
896	if (vmf.vmf_bx != 0x504d) {
897		printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
898		return ENXIO;
899	}
900	if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
901		printf("apm: protected mode connections are not supported\n");
902		return ENXIO;
903	}
904
905	apm_version = vmf.vmf_ax;
906	sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
907	sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
908	sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
909
910	vmf.vmf_ah = APM_BIOS;
911	vmf.vmf_al = APM_DISCONNECT;
912	vmf.vmf_bx = 0;
913        vm86_intcall(APM_INT, &vmf);		/* disconnect, just in case */
914
915#ifdef PC98
916	/* PC98 have bogos APM 32bit BIOS */
917	if ((vmf.vmf_cx & APM_32BIT_SUPPORT) == 0)
918		return ENXIO;
919	rid = 0;
920	bus_set_resource(dev, SYS_RES_IOPORT, rid,
921			 APM_NECSMM_PORT, APM_NECSMM_PORTSZ);
922	sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
923			 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
924	if (sc->sc_res == NULL) {
925		printf("apm: cannot open NEC smm device\n");
926		return ENXIO;
927	}
928	bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_res);
929
930	vmf.vmf_ah = APM_BIOS;
931	vmf.vmf_al = APM_PROT32CONNECT;
932	vmf.vmf_bx = 0;
933	if (vm86_intcall(APM_INT, &vmf)) {
934		printf("apm: 32-bit connection error.\n");
935		return (ENXIO);
936 	}
937
938	sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
939	sc->bios.seg.code32.limit = 0xffff;
940	sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
941	sc->bios.seg.code16.limit = 0xffff;
942	sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
943	sc->bios.seg.data.limit = 0xffff;
944	sc->bios.entry = vmf.vmf_ebx;
945	sc->connectmode = APM_PROT32CONNECT;
946#else
947	if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
948		vmf.vmf_ah = APM_BIOS;
949		vmf.vmf_al = APM_PROT32CONNECT;
950		vmf.vmf_bx = 0;
951		if (vm86_intcall(APM_INT, &vmf)) {
952			printf("apm: 32-bit connection error.\n");
953			return (ENXIO);
954 		}
955		sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
956		sc->bios.seg.code32.limit = 0xffff;
957		sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
958		sc->bios.seg.code16.limit = 0xffff;
959		sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
960		sc->bios.seg.data.limit = 0xffff;
961		sc->bios.entry = vmf.vmf_ebx;
962		sc->connectmode = APM_PROT32CONNECT;
963 	} else {
964		/* use 16-bit connection */
965		vmf.vmf_ah = APM_BIOS;
966		vmf.vmf_al = APM_PROT16CONNECT;
967		vmf.vmf_bx = 0;
968		if (vm86_intcall(APM_INT, &vmf)) {
969			printf("apm: 16-bit connection error.\n");
970			return (ENXIO);
971		}
972		sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
973		sc->bios.seg.code16.limit = 0xffff;
974		sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
975		sc->bios.seg.data.limit = 0xffff;
976		sc->bios.entry = vmf.vmf_bx;
977		sc->connectmode = APM_PROT16CONNECT;
978	}
979#endif
980	return(0);
981}
982
983
984/*
985 * return 0 if the user will notice and handle the event,
986 * return 1 if the kernel driver should do so.
987 */
988static int
989apm_record_event(struct apm_softc *sc, u_int event_type)
990{
991	struct apm_event_info *evp;
992
993	if ((sc->sc_flags & SCFLAG_OPEN) == 0)
994		return 1;		/* no user waiting */
995	if (sc->event_count == APM_NEVENTS)
996		return 1;			/* overflow */
997	if (sc->event_filter[event_type] == 0)
998		return 1;		/* not registered */
999	evp = &sc->event_list[sc->event_ptr];
1000	sc->event_count++;
1001	sc->event_ptr++;
1002	sc->event_ptr %= APM_NEVENTS;
1003	evp->type = event_type;
1004	evp->index = ++apm_evindex;
1005	selwakeup(&sc->sc_rsel);
1006	return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
1007}
1008
1009/* Power profile */
1010static void
1011apm_power_profile(struct apm_softc *sc)
1012{
1013	int state;
1014	struct apm_info info;
1015	static int apm_acline = 0;
1016
1017	if (apm_get_info(&info))
1018		return;
1019
1020	if (apm_acline != info.ai_acline) {
1021		apm_acline = info.ai_acline;
1022		state = apm_acline ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY;
1023		power_profile_set_state(state);
1024	}
1025}
1026
1027/* Process APM event */
1028static void
1029apm_processevent(void)
1030{
1031	int apm_event;
1032	struct apm_softc *sc = &apm_softc;
1033
1034#define OPMEV_DEBUGMESSAGE(symbol) case symbol:				\
1035	APM_DPRINT("Received APM Event: " #symbol "\n");
1036
1037	do {
1038		apm_event = apm_getevent();
1039		switch (apm_event) {
1040		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
1041			if (apm_op_inprog == 0) {
1042			    apm_op_inprog++;
1043			    if (apm_record_event(sc, apm_event)) {
1044				apm_suspend(PMST_STANDBY);
1045			    }
1046			}
1047			break;
1048		    OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
1049			if (apm_op_inprog == 0) {
1050			    apm_op_inprog++;
1051			    if (apm_record_event(sc, apm_event)) {
1052				apm_suspend(PMST_STANDBY);
1053			    }
1054			}
1055			break;
1056		    OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
1057 			apm_lastreq_notify();
1058			if (apm_op_inprog == 0) {
1059			    apm_op_inprog++;
1060			    if (apm_record_event(sc, apm_event)) {
1061				apm_do_suspend();
1062			    }
1063			}
1064			return; /* XXX skip the rest */
1065		    OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
1066 			apm_lastreq_notify();
1067			if (apm_op_inprog == 0) {
1068			    apm_op_inprog++;
1069			    if (apm_record_event(sc, apm_event)) {
1070				apm_do_suspend();
1071			    }
1072			}
1073			return; /* XXX skip the rest */
1074		    OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
1075			apm_do_suspend();
1076			break;
1077		    OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
1078			apm_record_event(sc, apm_event);
1079			apm_resume();
1080			break;
1081		    OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
1082			apm_record_event(sc, apm_event);
1083			apm_resume();
1084			break;
1085		    OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
1086			apm_record_event(sc, apm_event);
1087			break;
1088		    OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
1089			if (apm_record_event(sc, apm_event)) {
1090			    apm_battery_low();
1091			    apm_suspend(PMST_SUSPEND);
1092			}
1093			break;
1094		    OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
1095			apm_record_event(sc, apm_event);
1096			apm_power_profile(sc);
1097			break;
1098		    OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1099			apm_record_event(sc, apm_event);
1100			inittodr(0);	/* adjust time to RTC */
1101			break;
1102		    OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1103			apm_record_event(sc, apm_event);
1104			apm_power_profile(sc);
1105			break;
1106		    case PMEV_NOEVENT:
1107			break;
1108		    default:
1109			printf("Unknown Original APM Event 0x%x\n", apm_event);
1110			    break;
1111		}
1112	} while (apm_event != PMEV_NOEVENT);
1113#ifdef PC98
1114	apm_disable_smm(sc);
1115#endif
1116}
1117
1118/*
1119 * Attach APM:
1120 *
1121 * Initialize APM driver
1122 */
1123
1124static int
1125apm_attach(device_t dev)
1126{
1127	struct apm_softc	*sc = &apm_softc;
1128	int			flags;
1129	int			drv_version;
1130#ifdef PC98
1131	int			rid;
1132#endif
1133
1134	if (resource_int_value("apm", 0, "flags", &flags) != 0)
1135		flags = 0;
1136
1137	if (flags & 0x20)
1138		statclock_disable = 1;
1139
1140	sc->initialized = 0;
1141
1142	/* Must be externally enabled */
1143	sc->active = 0;
1144
1145	/* Always call HLT in idle loop */
1146	sc->always_halt_cpu = 1;
1147
1148	getenv_int("debug.apm_debug", &apm_debug);
1149
1150	/* print bootstrap messages */
1151	APM_DPRINT("apm: APM BIOS version %04lx\n",  apm_version);
1152	APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1153	    sc->bios.seg.code16.base, sc->bios.seg.data.base);
1154	APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1155	    sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1156	    is_enabled(!sc->disabled));
1157	APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1158	    sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1159
1160#ifdef PC98
1161	rid = 0;
1162	sc->sc_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
1163			 APM_NECSMM_PORT, ~0, APM_NECSMM_PORTSZ, RF_ACTIVE);
1164	if (sc->sc_res == NULL)
1165		panic("%s: counldn't map I/O ports", device_get_name(dev));
1166	sc->sc_iot = rman_get_bustag(sc->sc_res);
1167	sc->sc_ioh = rman_get_bushandle(sc->sc_res);
1168
1169	if (apm_version==0x112 || apm_version==0x111 || apm_version==0x110)
1170		apm_necsmm_addr = APM_NECSMM_PORT;
1171	else
1172		apm_necsmm_addr = 0;
1173	apm_necsmm_mask = ~APM_NECSMM_EN;
1174#endif /* PC98 */
1175
1176	/*
1177         * In one test, apm bios version was 1.02; an attempt to register
1178         * a 1.04 driver resulted in a 1.00 connection!  Registering a
1179         * 1.02 driver resulted in a 1.02 connection.
1180         */
1181	drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1182	for (; drv_version > 0x100; drv_version--)
1183		if (apm_driver_version(drv_version) == 0)
1184			break;
1185	sc->minorversion = ((drv_version & 0x00f0) >>  4) * 10 +
1186		((drv_version & 0x000f) >> 0);
1187	sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1188		((apm_version & 0x0f00) >> 8);
1189
1190	sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1191
1192	if (sc->intversion >= INTVERSION(1, 1))
1193		APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1194	device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1195	       ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1196	       ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1197	       sc->majorversion, sc->minorversion);
1198
1199
1200	APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1201	/* enable power management */
1202	if (sc->disabled) {
1203		if (apm_enable_disable_pm(1)) {
1204			APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1205			    (sc->bios.r.eax >> 8) & 0xff);
1206		}
1207	}
1208
1209	/* engage power managment (APM 1.1 or later) */
1210	if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1211		if (apm_engage_disengage_pm(1)) {
1212			APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1213			    (sc->bios.r.eax >> 8) & 0xff);
1214			APM_DPRINT(" (Docked or using external power?).\n");
1215		}
1216	}
1217
1218	/* Power the system off using APM */
1219	EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1220			      SHUTDOWN_PRI_LAST);
1221
1222	/* Register APM again to pass the correct argument of pm_func. */
1223	power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, sc);
1224
1225	sc->initialized = 1;
1226	sc->suspending = 0;
1227
1228	make_dev(&apm_cdevsw, 0, 0, 5, 0664, "apm");
1229	make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl");
1230	return 0;
1231}
1232
1233static int
1234apmopen(dev_t dev, int flag, int fmt, struct thread *td)
1235{
1236	struct apm_softc *sc = &apm_softc;
1237	int ctl = APMDEV(dev);
1238
1239	if (!sc->initialized)
1240		return (ENXIO);
1241
1242	switch (ctl) {
1243	case APMDEV_CTL:
1244		if (!(flag & FWRITE))
1245			return EINVAL;
1246		if (sc->sc_flags & SCFLAG_OCTL)
1247			return EBUSY;
1248		sc->sc_flags |= SCFLAG_OCTL;
1249		bzero(sc->event_filter, sizeof sc->event_filter);
1250		break;
1251	case APMDEV_NORMAL:
1252		sc->sc_flags |= SCFLAG_ONORMAL;
1253		break;
1254	default:
1255		return ENXIO;
1256		break;
1257	}
1258	return 0;
1259}
1260
1261static int
1262apmclose(dev_t dev, int flag, int fmt, struct thread *td)
1263{
1264	struct apm_softc *sc = &apm_softc;
1265	int ctl = APMDEV(dev);
1266
1267	switch (ctl) {
1268	case APMDEV_CTL:
1269		apm_lastreq_rejected();
1270		sc->sc_flags &= ~SCFLAG_OCTL;
1271		bzero(sc->event_filter, sizeof sc->event_filter);
1272		break;
1273	case APMDEV_NORMAL:
1274		sc->sc_flags &= ~SCFLAG_ONORMAL;
1275		break;
1276	}
1277	if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1278		sc->event_count = 0;
1279		sc->event_ptr = 0;
1280	}
1281	return 0;
1282}
1283
1284static int
1285apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1286{
1287	struct apm_softc *sc = &apm_softc;
1288	struct apm_bios_arg *args;
1289	int error = 0;
1290	int ret;
1291	int newstate;
1292
1293	if (!sc->initialized)
1294		return (ENXIO);
1295	APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1296	switch (cmd) {
1297	case APMIO_SUSPEND:
1298		if (!(flag & FWRITE))
1299			return (EPERM);
1300		if (sc->active)
1301			apm_suspend(PMST_SUSPEND);
1302		else
1303			error = EINVAL;
1304		break;
1305
1306	case APMIO_STANDBY:
1307		if (!(flag & FWRITE))
1308			return (EPERM);
1309		if (sc->active)
1310			apm_suspend(PMST_STANDBY);
1311		else
1312			error = EINVAL;
1313		break;
1314
1315	case APMIO_GETINFO_OLD:
1316		{
1317			struct apm_info info;
1318			apm_info_old_t aiop;
1319
1320			if (apm_get_info(&info))
1321				error = ENXIO;
1322			aiop = (apm_info_old_t)addr;
1323			aiop->ai_major = info.ai_major;
1324			aiop->ai_minor = info.ai_minor;
1325			aiop->ai_acline = info.ai_acline;
1326			aiop->ai_batt_stat = info.ai_batt_stat;
1327			aiop->ai_batt_life = info.ai_batt_life;
1328			aiop->ai_status = info.ai_status;
1329		}
1330		break;
1331	case APMIO_GETINFO:
1332		if (apm_get_info((apm_info_t)addr))
1333			error = ENXIO;
1334		break;
1335	case APMIO_GETPWSTATUS:
1336		if (apm_get_pwstatus((apm_pwstatus_t)addr))
1337			error = ENXIO;
1338		break;
1339	case APMIO_ENABLE:
1340		if (!(flag & FWRITE))
1341			return (EPERM);
1342		apm_event_enable();
1343		break;
1344	case APMIO_DISABLE:
1345		if (!(flag & FWRITE))
1346			return (EPERM);
1347		apm_event_disable();
1348		break;
1349	case APMIO_HALTCPU:
1350		if (!(flag & FWRITE))
1351			return (EPERM);
1352		apm_halt_cpu();
1353		break;
1354	case APMIO_NOTHALTCPU:
1355		if (!(flag & FWRITE))
1356			return (EPERM);
1357		apm_not_halt_cpu();
1358		break;
1359	case APMIO_DISPLAY:
1360		if (!(flag & FWRITE))
1361			return (EPERM);
1362		newstate = *(int *)addr;
1363		if (apm_display(newstate))
1364			error = ENXIO;
1365		break;
1366	case APMIO_BIOS:
1367		if (!(flag & FWRITE))
1368			return (EPERM);
1369		/* XXX compatibility with the old interface */
1370		args = (struct apm_bios_arg *)addr;
1371		sc->bios.r.eax = args->eax;
1372		sc->bios.r.ebx = args->ebx;
1373		sc->bios.r.ecx = args->ecx;
1374		sc->bios.r.edx = args->edx;
1375		sc->bios.r.esi = args->esi;
1376		sc->bios.r.edi = args->edi;
1377		if ((ret = apm_bioscall())) {
1378			/*
1379			 * Return code 1 means bios call was unsuccessful.
1380			 * Error code is stored in %ah.
1381			 * Return code -1 means bios call was unsupported
1382			 * in the APM BIOS version.
1383			 */
1384			if (ret == -1) {
1385				error = EINVAL;
1386			}
1387		} else {
1388			/*
1389			 * Return code 0 means bios call was successful.
1390			 * We need only %al and can discard %ah.
1391			 */
1392			sc->bios.r.eax &= 0xff;
1393		}
1394		args->eax = sc->bios.r.eax;
1395		args->ebx = sc->bios.r.ebx;
1396		args->ecx = sc->bios.r.ecx;
1397		args->edx = sc->bios.r.edx;
1398		args->esi = sc->bios.r.esi;
1399		args->edi = sc->bios.r.edi;
1400		break;
1401	default:
1402		error = EINVAL;
1403		break;
1404	}
1405
1406	/* for /dev/apmctl */
1407	if (APMDEV(dev) == APMDEV_CTL) {
1408		struct apm_event_info *evp;
1409		int i;
1410
1411		error = 0;
1412		switch (cmd) {
1413		case APMIO_NEXTEVENT:
1414			if (!sc->event_count) {
1415				error = EAGAIN;
1416			} else {
1417				evp = (struct apm_event_info *)addr;
1418				i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1419				i %= APM_NEVENTS;
1420				*evp = sc->event_list[i];
1421				sc->event_count--;
1422			}
1423			break;
1424		case APMIO_REJECTLASTREQ:
1425			if (apm_lastreq_rejected()) {
1426				error = EINVAL;
1427			}
1428			break;
1429		default:
1430			error = EINVAL;
1431			break;
1432		}
1433	}
1434
1435	return error;
1436}
1437
1438static int
1439apmwrite(dev_t dev, struct uio *uio, int ioflag)
1440{
1441	struct apm_softc *sc = &apm_softc;
1442	u_int event_type;
1443	int error;
1444	u_char enabled;
1445
1446	if (APMDEV(dev) != APMDEV_CTL)
1447		return(ENODEV);
1448	if (uio->uio_resid != sizeof(u_int))
1449		return(E2BIG);
1450
1451	if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1452		return(error);
1453
1454	if (event_type < 0 || event_type >= APM_NPMEV)
1455		return(EINVAL);
1456
1457	if (sc->event_filter[event_type] == 0) {
1458		enabled = 1;
1459	} else {
1460		enabled = 0;
1461	}
1462	sc->event_filter[event_type] = enabled;
1463	APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1464
1465	return uio->uio_resid;
1466}
1467
1468static int
1469apmpoll(dev_t dev, int events, struct thread *td)
1470{
1471	struct apm_softc *sc = &apm_softc;
1472	int revents = 0;
1473
1474	if (events & (POLLIN | POLLRDNORM)) {
1475		if (sc->event_count) {
1476			revents |= events & (POLLIN | POLLRDNORM);
1477		} else {
1478			selrecord(td, &sc->sc_rsel);
1479		}
1480	}
1481
1482	return (revents);
1483}
1484
1485static device_method_t apm_methods[] = {
1486	/* Device interface */
1487	DEVMETHOD(device_identify,	apm_identify),
1488	DEVMETHOD(device_probe,		apm_probe),
1489	DEVMETHOD(device_attach,	apm_attach),
1490
1491	{ 0, 0 }
1492};
1493
1494static driver_t apm_driver = {
1495	"apm",
1496	apm_methods,
1497	1,			/* no softc (XXX) */
1498};
1499
1500static devclass_t apm_devclass;
1501
1502DRIVER_MODULE(apm, legacy, apm_driver, apm_devclass, apm_modevent, 0);
1503MODULE_VERSION(apm, 1);
1504
1505static int
1506apm_pm_func(u_long cmd, void *arg, ...)
1507{
1508	int	state, apm_state;
1509	int	error;
1510	va_list	ap;
1511
1512	error = 0;
1513	switch (cmd) {
1514	case POWER_CMD_SUSPEND:
1515		va_start(ap, arg);
1516		state = va_arg(ap, int);
1517		va_end(ap);
1518
1519		switch (state) {
1520		case POWER_SLEEP_STATE_STANDBY:
1521			apm_state = PMST_STANDBY;
1522			break;
1523		case POWER_SLEEP_STATE_SUSPEND:
1524		case POWER_SLEEP_STATE_HIBERNATE:
1525			apm_state = PMST_SUSPEND;
1526			break;
1527		default:
1528			error = EINVAL;
1529			goto out;
1530		}
1531
1532		apm_suspend(apm_state);
1533		break;
1534
1535	default:
1536		error = EINVAL;
1537		goto out;
1538	}
1539
1540out:
1541	return (error);
1542}
1543
1544static void
1545apm_pm_register(void *arg)
1546{
1547	int	disabled = 0;
1548
1549	resource_int_value("apm", 0, "disabled", &disabled);
1550	if (disabled == 0)
1551		power_pm_register(POWER_PM_TYPE_APM, apm_pm_func, NULL);
1552}
1553
1554SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, apm_pm_register, 0);
1555