Home | History | Annotate | Line # | Download | only in apm
apm.c revision 1.1
      1 /*	$NetBSD: apm.c,v 1.1 2000/07/02 09:48:12 takemura Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by John Kohl and Christopher G. Demetriou.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * from: sys/arch/i386/i386/apm.c,v 1.49 2000/05/08
     40  */
     41 
     42 #include "opt_apm.h"
     43 
     44 #ifdef APM_NOIDLE
     45 #error APM_NOIDLE option deprecated; use APM_NO_IDLE instead
     46 #endif
     47 
     48 #if defined(DEBUG) && !defined(APMDEBUG)
     49 #define	APMDEBUG
     50 #endif
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/kernel.h>
     56 #include <sys/map.h>
     57 #include <sys/proc.h>
     58 #include <sys/kthread.h>
     59 #include <sys/lock.h>
     60 #include <sys/user.h>
     61 #include <sys/malloc.h>
     62 #include <sys/device.h>
     63 #include <sys/fcntl.h>
     64 #include <sys/ioctl.h>
     65 #include <sys/select.h>
     66 #include <sys/poll.h>
     67 #include <sys/conf.h>
     68 
     69 #include <dev/apm/apmvar.h>
     70 
     71 #include <machine/stdarg.h>
     72 
     73 #define	APMDEBUG
     74 #if defined(APMDEBUG)
     75 #define	DPRINTF(f, x)		do { if (apmdebug & (f)) printf x; } while (0)
     76 
     77 #define	APMDEBUG_INFO		0x01
     78 #define	APMDEBUG_APMCALLS	0x02
     79 #define	APMDEBUG_EVENTS		0x04
     80 #define	APMDEBUG_PROBE		0x10
     81 #define	APMDEBUG_ATTACH		0x40
     82 #define	APMDEBUG_DEVICE		0x20
     83 #define	APMDEBUG_ANOM		0x40
     84 
     85 #ifdef APMDEBUG_VALUE
     86 int	apmdebug = APMDEBUG_VALUE;
     87 #else
     88 int	apmdebug = 0;
     89 #endif
     90 #else
     91 #define	DPRINTF(f, x)		/**/
     92 #endif
     93 
     94 #define APM_NEVENTS 16
     95 
     96 struct apm_softc {
     97 	struct device sc_dev;
     98 	struct selinfo sc_rsel;
     99 	struct selinfo sc_xsel;
    100 	int	sc_flags;
    101 	int	event_count;
    102 	int	event_ptr;
    103 	int	sc_power_state;
    104 	struct proc *sc_thread;
    105 	struct lock sc_lock;
    106 	struct apm_event_info event_list[APM_NEVENTS];
    107 	struct apm_accessops *ops;
    108 	void *cookie;
    109 };
    110 #define	SCFLAG_OREAD	0x0000001
    111 #define	SCFLAG_OWRITE	0x0000002
    112 #define	SCFLAG_OPEN	(SCFLAG_OREAD|SCFLAG_OWRITE)
    113 
    114 #define	APMUNIT(dev)	(minor(dev)&0xf0)
    115 #define	APMDEV(dev)	(minor(dev)&0x0f)
    116 #define APMDEV_NORMAL	0
    117 #define APMDEV_CTL	8
    118 
    119 /*
    120  * A brief note on the locking protocol: it's very simple; we
    121  * assert an exclusive lock any time thread context enters the
    122  * APM module.  This is both the APM thread itself, as well as
    123  * user context.
    124  */
    125 #define	APM_LOCK(apmsc)							\
    126 	(void) lockmgr(&(apmsc)->sc_lock, LK_EXCLUSIVE, NULL)
    127 #define	APM_UNLOCK(apmsc)						\
    128 	(void) lockmgr(&(apmsc)->sc_lock, LK_RELEASE, NULL)
    129 
    130 static void	apmattach __P((struct device *, struct device *, void *));
    131 static int	apmmatch __P((struct device *, struct cfdata *, void *));
    132 
    133 static void	apm_event_handle __P((struct apm_softc *, u_int, u_int));
    134 static void	apm_periodic_check __P((struct apm_softc *));
    135 static void	apm_create_thread __P((void *));
    136 static void	apm_thread __P((void *));
    137 static void	apm_perror __P((const char *, int, ...))
    138 		    __kprintf_attribute__((__format__(__printf__,1,3)));
    139 #ifdef APM_POWER_PRINT
    140 static void	apm_power_print __P((struct apm_softc *, struct apm_power_info *));
    141 #endif
    142 static int	apm_record_event __P((struct apm_softc *, u_int));
    143 static void	apm_set_ver __P((struct apm_softc *, u_long));
    144 static void	apm_standby __P((struct apm_softc *));
    145 static const char *apm_strerror __P((int));
    146 static void	apm_suspend __P((struct apm_softc *));
    147 static void	apm_resume __P((struct apm_softc *, u_int, u_int));
    148 
    149 cdev_decl(apm);
    150 
    151 struct cfattach apm_ca = {
    152 	sizeof(struct apm_softc), apmmatch, apmattach
    153 };
    154 
    155 extern struct cfdriver apm_cd;
    156 
    157 /* configurable variables */
    158 int	apm_bogus_bios = 0;
    159 #ifdef APM_DISABLE
    160 int	apm_enabled = 0;
    161 #else
    162 int	apm_enabled = 1;
    163 #endif
    164 #ifdef APM_NO_IDLE
    165 int	apm_do_idle = 0;
    166 #else
    167 int	apm_do_idle = 1;
    168 #endif
    169 #ifdef APM_NO_STANDBY
    170 int	apm_do_standby = 0;
    171 #else
    172 int	apm_do_standby = 1;
    173 #endif
    174 #ifdef APM_V10_ONLY
    175 int	apm_v11_enabled = 0;
    176 #else
    177 int	apm_v11_enabled = 1;
    178 #endif
    179 #ifdef APM_NO_V12
    180 int	apm_v12_enabled = 0;
    181 #else
    182 int	apm_v12_enabled = 1;
    183 #endif
    184 
    185 /* variables used during operation (XXX cgd) */
    186 u_char	apm_majver, apm_minver;
    187 int	apm_inited;
    188 int	apm_standbys, apm_userstandbys, apm_suspends, apm_battlow;
    189 int	apm_damn_fool_bios, apm_op_inprog;
    190 int	apm_evindex;
    191 
    192 static const char *
    193 apm_strerror(code)
    194 	int code;
    195 {
    196 	switch (code) {
    197 	case APM_ERR_PM_DISABLED:
    198 		return ("power management disabled");
    199 	case APM_ERR_REALALREADY:
    200 		return ("real mode interface already connected");
    201 	case APM_ERR_NOTCONN:
    202 		return ("interface not connected");
    203 	case APM_ERR_16ALREADY:
    204 		return ("16-bit interface already connected");
    205 	case APM_ERR_16NOTSUPP:
    206 		return ("16-bit interface not supported");
    207 	case APM_ERR_32ALREADY:
    208 		return ("32-bit interface already connected");
    209 	case APM_ERR_32NOTSUPP:
    210 		return ("32-bit interface not supported");
    211 	case APM_ERR_UNRECOG_DEV:
    212 		return ("unrecognized device ID");
    213 	case APM_ERR_ERANGE:
    214 		return ("parameter out of range");
    215 	case APM_ERR_NOTENGAGED:
    216 		return ("interface not engaged");
    217 	case APM_ERR_UNABLE:
    218 		return ("unable to enter requested state");
    219 	case APM_ERR_NOEVENTS:
    220 		return ("no pending events");
    221 	case APM_ERR_NOT_PRESENT:
    222 		return ("no APM present");
    223 	default:
    224 		return ("unknown error code");
    225 	}
    226 }
    227 
    228 static void
    229 apm_perror(const char *str, int errinfo, ...) /* XXX cgd */
    230 {
    231 	va_list ap;
    232 
    233 	printf("APM ");
    234 
    235 	va_start(ap, errinfo);
    236 	vprintf(str, ap);			/* XXX cgd */
    237 	va_end(ap);
    238 
    239 	printf(": %s\n", apm_strerror(errinfo));
    240 }
    241 
    242 #ifdef APM_POWER_PRINT
    243 static void
    244 apm_power_print(sc, pi)
    245 	struct apm_softc *sc;
    246 	struct apm_power_info *pi;
    247 {
    248 
    249 	if (APM_BATT_LIFE(regs) != APM_BATT_LIFE_UNKNOWN) {
    250 		printf("%s: battery life expectancy: %d%%\n",
    251 		    sc->sc_dev.dv_xname, APM_BATT_LIFE(regs));
    252 	}
    253 	printf("%s: A/C state: ", sc->sc_dev.dv_xname);
    254 	switch (pi->ac_state) {
    255 	case APM_AC_OFF:
    256 		printf("off\n");
    257 		break;
    258 	case APM_AC_ON:
    259 		printf("on\n");
    260 		break;
    261 	case APM_AC_BACKUP:
    262 		printf("backup power\n");
    263 		break;
    264 	default:
    265 	case APM_AC_UNKNOWN:
    266 		printf("unknown\n");
    267 		break;
    268 	}
    269 	printf("%s: battery charge state:", sc->sc_dev.dv_xname);
    270 	switch (pi->battery_state) {
    271 	case APM_BATT_HIGH:
    272 		printf("high\n");
    273 		break;
    274 	case APM_BATT_LOW:
    275 		printf("low\n");
    276 		break;
    277 	case APM_BATT_CRITICAL:
    278 		printf("critical\n");
    279 		break;
    280 	case APM_BATT_CHARGING:
    281 		printf("charging\n");
    282 		break;
    283 	case APM_BATT_UNKNOWN:
    284 		printf("unknown\n");
    285 		break;
    286 	default:
    287 		printf("undecoded state %x\n", pi->battery_state);
    288 		break;
    289 	}
    290 	if (pi->minutes_left != 0) {
    291 		printf("%s: estimated ", sc->sc_dev.dv_xname);
    292 		printf("%dh ", pi->minutes_left / 60);
    293 	}
    294 	return;
    295 }
    296 #endif
    297 
    298 static void
    299 apm_suspend(sc)
    300 	struct apm_softc *sc;
    301 {
    302 
    303 	if (sc->sc_power_state == PWR_SUSPEND) {
    304 #ifdef APMDEBUG
    305 		printf("%s: apm_suspend: already suspended?\n",
    306 		    sc->sc_dev.dv_xname);
    307 #endif
    308 		return;
    309 	}
    310 	sc->sc_power_state = PWR_SUSPEND;
    311 
    312 	dopowerhooks(PWR_SUSPEND);
    313 
    314 	/* XXX cgd */
    315 	(void)sc->ops->set_powstate(sc->cookie, APM_DEV_ALLDEVS, APM_SYS_SUSPEND);
    316 }
    317 
    318 static void
    319 apm_standby(sc)
    320 	struct apm_softc *sc;
    321 {
    322 
    323 	if (sc->sc_power_state == PWR_STANDBY) {
    324 #ifdef APMDEBUG
    325 		printf("%s: apm_standby: already standing by?\n",
    326 		    sc->sc_dev.dv_xname);
    327 #endif
    328 		return;
    329 	}
    330 	sc->sc_power_state = PWR_STANDBY;
    331 
    332 	dopowerhooks(PWR_STANDBY);
    333 
    334 	/* XXX cgd */
    335 	(void)sc->ops->set_powstate(sc->cookie, APM_DEV_ALLDEVS, APM_SYS_STANDBY);
    336 }
    337 
    338 static void
    339 apm_resume(sc, event_type, event_info)
    340 	struct apm_softc *sc;
    341 	u_int event_type, event_info;
    342 {
    343 
    344 	if (sc->sc_power_state == PWR_RESUME) {
    345 #ifdef APMDEBUG
    346 		printf("%s: apm_resume: already running?\n",
    347 		    sc->sc_dev.dv_xname);
    348 #endif
    349 		return;
    350 	}
    351 	sc->sc_power_state = PWR_RESUME;
    352 
    353 	/*
    354 	 * Some system requires its clock to be initialized after hybernation.
    355 	 */
    356 /* XXX
    357 	initrtclock();
    358 */
    359 
    360 	inittodr(time.tv_sec);
    361 	dopowerhooks(PWR_RESUME);
    362 	apm_record_event(sc, event_type);
    363 }
    364 
    365 /*
    366  * return 0 if the user will notice and handle the event,
    367  * return 1 if the kernel driver should do so.
    368  */
    369 static int
    370 apm_record_event(sc, event_type)
    371 	struct apm_softc *sc;
    372 	u_int event_type;
    373 {
    374 	struct apm_event_info *evp;
    375 
    376 	if ((sc->sc_flags & SCFLAG_OPEN) == 0)
    377 		return 1;		/* no user waiting */
    378 	if (sc->event_count == APM_NEVENTS)
    379 		return 1;			/* overflow */
    380 	evp = &sc->event_list[sc->event_ptr];
    381 	sc->event_count++;
    382 	sc->event_ptr++;
    383 	sc->event_ptr %= APM_NEVENTS;
    384 	evp->type = event_type;
    385 	evp->index = ++apm_evindex;
    386 	selwakeup(&sc->sc_rsel);
    387 	return (sc->sc_flags & SCFLAG_OWRITE) ? 0 : 1; /* user may handle */
    388 }
    389 
    390 static void
    391 apm_event_handle(sc, event_code, event_info)
    392 	struct apm_softc *sc;
    393 	u_int event_code, event_info;
    394 {
    395 	int error;
    396 	char *code;
    397 	struct apm_power_info pi;
    398 
    399 	switch (event_code) {
    400 	case APM_USER_STANDBY_REQ:
    401 		DPRINTF(APMDEBUG_EVENTS, ("apmev: user standby request\n"));
    402 		if (apm_do_standby) {
    403 			if (apm_record_event(sc, event_code))
    404 				apm_userstandbys++;
    405 			apm_op_inprog++;
    406 			(void)sc->ops->set_powstate(sc->cookie,
    407 						    APM_DEV_ALLDEVS,
    408 						    APM_LASTREQ_INPROG);
    409 		} else {
    410 			(void)sc->ops->set_powstate(sc->cookie,
    411 						    APM_DEV_ALLDEVS,
    412 						    APM_LASTREQ_REJECTED);
    413 			/* in case BIOS hates being spurned */
    414 			sc->ops->enable(sc->cookie, 1);
    415 		}
    416 		break;
    417 
    418 	case APM_STANDBY_REQ:
    419 		DPRINTF(APMDEBUG_EVENTS, ("apmev: system standby request\n"));
    420 		if (apm_standbys || apm_suspends) {
    421 			DPRINTF(APMDEBUG_EVENTS | APMDEBUG_ANOM,
    422 			    ("damn fool BIOS did not wait for answer\n"));
    423 			/* just give up the fight */
    424 			apm_damn_fool_bios = 1;
    425 		}
    426 		if (apm_do_standby) {
    427 			if (apm_record_event(sc, event_code))
    428 				apm_standbys++;
    429 			apm_op_inprog++;
    430 			(void)sc->ops->set_powstate(sc->cookie,
    431 						    APM_DEV_ALLDEVS,
    432 						    APM_LASTREQ_INPROG);
    433 		} else {
    434 			(void)sc->ops->set_powstate(sc->cookie,
    435 						    APM_DEV_ALLDEVS,
    436 						    APM_LASTREQ_REJECTED);
    437 			/* in case BIOS hates being spurned */
    438 			sc->ops->enable(sc->cookie, 1);
    439 		}
    440 		break;
    441 
    442 	case APM_USER_SUSPEND_REQ:
    443 		DPRINTF(APMDEBUG_EVENTS, ("apmev: user suspend request\n"));
    444 		if (apm_record_event(sc, event_code))
    445 			apm_suspends++;
    446 		apm_op_inprog++;
    447 		(void)sc->ops->set_powstate(sc->cookie,
    448 					    APM_DEV_ALLDEVS,
    449 					    APM_LASTREQ_INPROG);
    450 		break;
    451 
    452 	case APM_SUSPEND_REQ:
    453 		DPRINTF(APMDEBUG_EVENTS, ("apmev: system suspend request\n"));
    454 		if (apm_standbys || apm_suspends) {
    455 			DPRINTF(APMDEBUG_EVENTS | APMDEBUG_ANOM,
    456 			    ("damn fool BIOS did not wait for answer\n"));
    457 			/* just give up the fight */
    458 			apm_damn_fool_bios = 1;
    459 		}
    460 		if (apm_record_event(sc, event_code))
    461 			apm_suspends++;
    462 		apm_op_inprog++;
    463 		(void)sc->ops->set_powstate(sc->cookie,
    464 					    APM_DEV_ALLDEVS,
    465 					    APM_LASTREQ_INPROG);
    466 		break;
    467 
    468 	case APM_POWER_CHANGE:
    469 		DPRINTF(APMDEBUG_EVENTS, ("apmev: power status change\n"));
    470 		error = sc->ops->get_powstat(sc->cookie, &pi);
    471 #ifdef APM_POWER_PRINT
    472 		/* only print if nobody is catching events. */
    473 		if (error == 0 &&
    474 		    (sc->sc_flags & (SCFLAG_OREAD|SCFLAG_OWRITE)) == 0)
    475 			apm_power_print(sc, &pi);
    476 #endif
    477 		apm_record_event(sc, event_code);
    478 		break;
    479 
    480 	case APM_NORMAL_RESUME:
    481 		DPRINTF(APMDEBUG_EVENTS, ("apmev: resume system\n"));
    482 		apm_resume(sc, event_code, event_info);
    483 		break;
    484 
    485 	case APM_CRIT_RESUME:
    486 		DPRINTF(APMDEBUG_EVENTS, ("apmev: critical resume system"));
    487 		apm_resume(sc, event_code, event_info);
    488 		break;
    489 
    490 	case APM_SYS_STANDBY_RESUME:
    491 		DPRINTF(APMDEBUG_EVENTS, ("apmev: system standby resume\n"));
    492 		apm_resume(sc, event_code, event_info);
    493 		break;
    494 
    495 	case APM_UPDATE_TIME:
    496 		DPRINTF(APMDEBUG_EVENTS, ("apmev: update time\n"));
    497 		apm_resume(sc, event_code, event_info);
    498 		break;
    499 
    500 	case APM_CRIT_SUSPEND_REQ:
    501 		DPRINTF(APMDEBUG_EVENTS, ("apmev: critical system suspend\n"));
    502 		apm_record_event(sc, event_code);
    503 		apm_suspend(sc);
    504 		break;
    505 
    506 	case APM_BATTERY_LOW:
    507 		DPRINTF(APMDEBUG_EVENTS, ("apmev: battery low\n"));
    508 		apm_battlow++;
    509 		apm_record_event(sc, event_code);
    510 		break;
    511 
    512 	case APM_CAP_CHANGE:
    513 		DPRINTF(APMDEBUG_EVENTS, ("apmev: capability change\n"));
    514 		if (apm_minver < 2) {
    515 			DPRINTF(APMDEBUG_EVENTS, ("apm: unexpected event\n"));
    516 		} else {
    517 			u_int numbatts, capflags;
    518 			sc->ops->get_capabilities(sc->cookie,
    519 						  &numbatts, &capflags);
    520 			sc->ops->get_powstat(sc->cookie, &pi); /* XXX */
    521 		}
    522 		break;
    523 
    524 	default:
    525 		switch (event_code >> 8) {
    526 			case 0:
    527 				code = "reserved system";
    528 				break;
    529 			case 1:
    530 				code = "reserved device";
    531 				break;
    532 			case 2:
    533 				code = "OEM defined";
    534 				break;
    535 			default:
    536 				code = "reserved";
    537 				break;
    538 		}
    539 		printf("APM: %s event code %x\n", code, event_code);
    540 	}
    541 }
    542 
    543 static void
    544 apm_periodic_check(sc)
    545 	struct apm_softc *sc;
    546 {
    547 	int error;
    548 	u_int event_code, event_info;
    549 
    550 
    551 	/*
    552 	 * tell the BIOS we're working on it, if asked to do a
    553 	 * suspend/standby
    554 	 */
    555 	if (apm_op_inprog)
    556 		sc->ops->set_powstate(sc->cookie, APM_DEV_ALLDEVS,
    557 				      APM_LASTREQ_INPROG);
    558 
    559 	while ((error = sc->ops->get_event(sc->cookie, &event_code,
    560 					   &event_info)) == 0
    561 	       && !apm_damn_fool_bios)
    562 		apm_event_handle(sc, event_code, event_info);
    563 
    564 	if (error != APM_ERR_NOEVENTS)
    565 		apm_perror("get event", error);
    566 	if (apm_suspends) {
    567 		apm_op_inprog = 0;
    568 		apm_suspend(sc);
    569 	} else if (apm_standbys || apm_userstandbys) {
    570 		apm_op_inprog = 0;
    571 		apm_standby(sc);
    572 	}
    573 	apm_suspends = apm_standbys = apm_battlow = apm_userstandbys = 0;
    574 	apm_damn_fool_bios = 0;
    575 }
    576 
    577 static void
    578 apm_set_ver(self, detail)
    579 	struct apm_softc *self;
    580 	u_long detail;
    581 {
    582 
    583 	if (apm_v12_enabled &&
    584 	    APM_MAJOR_VERS(detail) == 1 &&
    585 	    APM_MINOR_VERS(detail) == 2) {
    586 		apm_majver = 1;
    587 		apm_minver = 2;
    588 		goto ok;
    589 	}
    590 
    591 	if (apm_v11_enabled &&
    592 	    APM_MAJOR_VERS(detail) == 1 &&
    593 	    APM_MINOR_VERS(detail) == 1) {
    594 		apm_majver = 1;
    595 		apm_minver = 1;
    596 	} else {
    597 		apm_majver = 1;
    598 		apm_minver = 0;
    599 	}
    600 ok:
    601 	printf("Power Management spec V%d.%d", apm_majver, apm_minver);
    602 	apm_inited = 1;
    603 	if (detail & APM_IDLE_SLOWS) {
    604 #ifdef DIAGNOSTIC
    605 		/* not relevant often */
    606 		printf(" (slowidle)");
    607 #endif
    608 		/* leave apm_do_idle at its user-configured setting */
    609 	} else
    610 		apm_do_idle = 0;
    611 #ifdef DIAGNOSTIC
    612 	if (detail & APM_BIOS_PM_DISABLED)
    613 		printf(" (BIOS mgmt disabled)");
    614 	if (detail & APM_BIOS_PM_DISENGAGED)
    615 		printf(" (BIOS managing devices)");
    616 #endif
    617 }
    618 
    619 static int
    620 apmmatch(parent, match, aux)
    621 	struct device *parent;
    622 	struct cfdata *match;
    623 	void *aux;
    624 {
    625 
    626 	/* There can be only one! */
    627 	if (apm_inited)
    628 		return 0;
    629 
    630 	return (1);
    631 }
    632 
    633 static void
    634 apmattach(parent, self, aux)
    635 	struct device *parent, *self;
    636 	void *aux;
    637 {
    638 	struct apm_softc *sc = (void *)self;
    639 	struct apmdev_attach_args *aaa = aux;
    640 	struct apm_power_info pinfo;
    641 	u_int numbatts, capflags;
    642 	int error;
    643 
    644 	printf(": ");
    645 
    646 	sc->ops = aaa->accessops;
    647 	sc->cookie = aaa->accesscookie;
    648 
    649 	switch ((APM_MAJOR_VERS(aaa->apm_detail) << 8) +
    650 		APM_MINOR_VERS(aaa->apm_detail)) {
    651 	case 0x0100:
    652 		apm_v11_enabled = 0;
    653 		apm_v12_enabled = 0;
    654 		break;
    655 	case 0x0101:
    656 		apm_v12_enabled = 0;
    657 		/* fall through */
    658 	case 0x0102:
    659 	default:
    660 		break;
    661 	}
    662 
    663 	apm_set_ver(sc, aaa->apm_detail);	/* prints version info */
    664 	printf("\n");
    665 	if (apm_minver >= 2)
    666 		sc->ops->get_capabilities(sc->cookie, &numbatts, &capflags);
    667 
    668 	/*
    669 	 * enable power management
    670 	 */
    671 	sc->ops->enable(sc->cookie, 1);
    672 
    673 	error = sc->ops->get_powstat(sc->cookie, &pinfo);
    674 	if (error == 0) {
    675 #ifdef APM_POWER_PRINT
    676 		apm_power_print(apmsc, &pinfo);
    677 #endif
    678 	} else
    679 		apm_perror("get power status", error);
    680 	sc->ops->cpu_busy(sc->cookie);
    681 
    682 	lockinit(&sc->sc_lock, PWAIT, "apmlk", 0, 0);
    683 
    684 	/* Initial state is `resumed'. */
    685 	sc->sc_power_state = PWR_RESUME;
    686 
    687 	/* Do an initial check. */
    688 	apm_periodic_check(sc);
    689 
    690 	/*
    691 	 * Create a kernel thread to periodically check for APM events,
    692 	 * and notify other subsystems when they occur.
    693 	 */
    694 	kthread_create(apm_create_thread, sc);
    695 
    696 	return;
    697 }
    698 
    699 /*
    700  * Print function (for parent devices).
    701  */
    702 int
    703 apmprint(aux, pnp)
    704 	void *aux;
    705 	const char *pnp;
    706 {
    707 	if (pnp)
    708 		printf("apm at %s", pnp);
    709 
    710 	return (UNCONF);
    711 }
    712 
    713 void
    714 apm_create_thread(arg)
    715 	void *arg;
    716 {
    717 	struct apm_softc *sc = arg;
    718 
    719 	if (kthread_create1(apm_thread, sc, &sc->sc_thread,
    720 			    "%s", sc->sc_dev.dv_xname) == 0)
    721 		return;
    722 
    723 	/*
    724 	 * We were unable to create the APM thread; bail out.
    725 	 */
    726 	sc->ops->disconnect(sc->cookie);
    727 	printf("%s: unable to create thread, kernel APM support disabled\n",
    728 	       sc->sc_dev.dv_xname);
    729 }
    730 
    731 void
    732 apm_thread(arg)
    733 	void *arg;
    734 {
    735 	struct apm_softc *apmsc = arg;
    736 
    737 	/*
    738 	 * Loop forever, doing a periodic check for APM events.
    739 	 */
    740 	for (;;) {
    741 		APM_LOCK(apmsc);
    742 		apm_periodic_check(apmsc);
    743 		APM_UNLOCK(apmsc);
    744 		(void) tsleep(apmsc, PWAIT, "apmev",  (8 * hz) / 7);
    745 	}
    746 }
    747 
    748 int
    749 apmopen(dev, flag, mode, p)
    750 	dev_t dev;
    751 	int flag, mode;
    752 	struct proc *p;
    753 {
    754 	int unit = APMUNIT(dev);
    755 	int ctl = APMDEV(dev);
    756 	int error = 0;
    757 	struct apm_softc *sc;
    758 
    759 	if (unit >= apm_cd.cd_ndevs)
    760 		return ENXIO;
    761 	sc = apm_cd.cd_devs[unit];
    762 	if (!sc)
    763 		return ENXIO;
    764 
    765 	if (!apm_inited)
    766 		return ENXIO;
    767 
    768 	DPRINTF(APMDEBUG_DEVICE,
    769 	    ("apmopen: pid %d flag %x mode %x\n", p->p_pid, flag, mode));
    770 
    771 	APM_LOCK(sc);
    772 	switch (ctl) {
    773 	case APMDEV_CTL:
    774 		if (!(flag & FWRITE)) {
    775 			error = EINVAL;
    776 			break;
    777 		}
    778 		if (sc->sc_flags & SCFLAG_OWRITE) {
    779 			error = EBUSY;
    780 			break;
    781 		}
    782 		sc->sc_flags |= SCFLAG_OWRITE;
    783 		break;
    784 	case APMDEV_NORMAL:
    785 		if (!(flag & FREAD) || (flag & FWRITE)) {
    786 			error = EINVAL;
    787 			break;
    788 		}
    789 		sc->sc_flags |= SCFLAG_OREAD;
    790 		break;
    791 	default:
    792 		error = ENXIO;
    793 		break;
    794 	}
    795 	APM_UNLOCK(sc);
    796 
    797 	return (error);
    798 }
    799 
    800 int
    801 apmclose(dev, flag, mode, p)
    802 	dev_t dev;
    803 	int flag, mode;
    804 	struct proc *p;
    805 {
    806 	struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)];
    807 	int ctl = APMDEV(dev);
    808 
    809 	DPRINTF(APMDEBUG_DEVICE,
    810 	    ("apmclose: pid %d flag %x mode %x\n", p->p_pid, flag, mode));
    811 
    812 	APM_LOCK(sc);
    813 	switch (ctl) {
    814 	case APMDEV_CTL:
    815 		sc->sc_flags &= ~SCFLAG_OWRITE;
    816 		break;
    817 	case APMDEV_NORMAL:
    818 		sc->sc_flags &= ~SCFLAG_OREAD;
    819 		break;
    820 	}
    821 	if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
    822 		sc->event_count = 0;
    823 		sc->event_ptr = 0;
    824 	}
    825 	APM_UNLOCK(sc);
    826 	return 0;
    827 }
    828 
    829 int
    830 apmioctl(dev, cmd, data, flag, p)
    831 	dev_t dev;
    832 	u_long cmd;
    833 	caddr_t data;
    834 	int flag;
    835 	struct proc *p;
    836 {
    837 	struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)];
    838 	struct apm_power_info *powerp;
    839 	struct apm_event_info *evp;
    840 #if 0
    841 	struct apm_ctl *actl;
    842 #endif
    843 	int i, error = 0;
    844 
    845 	APM_LOCK(sc);
    846 	switch (cmd) {
    847 	case APM_IOC_STANDBY:
    848 		if (!apm_do_standby) {
    849 			error = EOPNOTSUPP;
    850 			break;
    851 		}
    852 
    853 		if ((flag & FWRITE) == 0) {
    854 			error = EBADF;
    855 			break;
    856 		}
    857 		apm_userstandbys++;
    858 		break;
    859 
    860 	case APM_IOC_SUSPEND:
    861 		if ((flag & FWRITE) == 0) {
    862 			error = EBADF;
    863 			break;
    864 		}
    865 		apm_suspends++;
    866 		break;
    867 
    868 	case APM_IOC_NEXTEVENT:
    869 		if (!sc->event_count)
    870 			error = EAGAIN;
    871 		else {
    872 			evp = (struct apm_event_info *)data;
    873 			i = sc->event_ptr + APM_NEVENTS - sc->event_count;
    874 			i %= APM_NEVENTS;
    875 			*evp = sc->event_list[i];
    876 			sc->event_count--;
    877 		}
    878 		break;
    879 
    880 	case APM_IOC_GETPOWER:
    881 		powerp = (struct apm_power_info *)data;
    882 		if ((error = sc->ops->get_powstat(sc->cookie, powerp)) != 0) {
    883 			apm_perror("ioctl get power status", error);
    884 			error = EIO;
    885 		}
    886 		break;
    887 
    888 	default:
    889 		error = ENOTTY;
    890 	}
    891 	APM_UNLOCK(sc);
    892 
    893 	return (error);
    894 }
    895 
    896 int
    897 apmpoll(dev, events, p)
    898 	dev_t dev;
    899 	int events;
    900 	struct proc *p;
    901 {
    902 	struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)];
    903 	int revents = 0;
    904 
    905 	APM_LOCK(sc);
    906 	if (events & (POLLIN | POLLRDNORM)) {
    907 		if (sc->event_count)
    908 			revents |= events & (POLLIN | POLLRDNORM);
    909 		else
    910 			selrecord(p, &sc->sc_rsel);
    911 	}
    912 	APM_UNLOCK(sc);
    913 
    914 	return (revents);
    915 }
    916