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