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