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