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