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