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