Home | History | Annotate | Line # | Download | only in dev
apm.c revision 1.8.2.1
      1  1.8.2.1     skrll /*	$NetBSD: apm.c,v 1.8.2.1 2004/08/03 10:37:20 skrll Exp $	*/
      2      1.1    itojun /*	$OpenBSD: apm.c,v 1.5 2002/06/07 07:13:59 miod Exp $	*/
      3      1.1    itojun 
      4      1.1    itojun /*-
      5      1.1    itojun  * Copyright (c) 2001 Alexander Guy.  All rights reserved.
      6      1.1    itojun  * Copyright (c) 1998-2001 Michael Shalayeff. All rights reserved.
      7      1.1    itojun  * Copyright (c) 1995 John T. Kohl.  All rights reserved.
      8      1.1    itojun  *
      9      1.1    itojun  * Redistribution and use in source and binary forms, with or without
     10      1.1    itojun  * modification, are permitted provided that the following conditions
     11      1.1    itojun  * are met:
     12      1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     13      1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     14      1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     16      1.1    itojun  *    documentation and/or other materials provided with the distribution.
     17      1.1    itojun  * 3. All advertising materials mentioning features or use of this software
     18      1.1    itojun  *    must display the following acknowledgement:
     19      1.1    itojun  *	This product includes software developed by the University of
     20      1.1    itojun  *	California, Berkeley and its contributors.
     21      1.1    itojun  * 4. Neither the name of the University nor the names of its contributors
     22      1.1    itojun  *    may be used to endorse or promote products derived from this software
     23      1.1    itojun  *    without specific prior written permission.
     24      1.1    itojun  *
     25      1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26      1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27      1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28      1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29      1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30      1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31      1.1    itojun  * OR SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32      1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33      1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34      1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35      1.1    itojun  * SUCH DAMAGE.
     36      1.1    itojun  *
     37      1.1    itojun  */
     38      1.1    itojun 
     39  1.8.2.1     skrll #include <sys/cdefs.h>
     40  1.8.2.1     skrll __KERNEL_RCSID(0, "$NetBSD: apm.c,v 1.8.2.1 2004/08/03 10:37:20 skrll Exp $");
     41  1.8.2.1     skrll 
     42      1.1    itojun #include "apm.h"
     43      1.1    itojun 
     44      1.1    itojun #if NAPM > 1
     45      1.1    itojun #error only one APM emulation device may be configured
     46      1.1    itojun #endif
     47      1.1    itojun 
     48      1.1    itojun #include <sys/param.h>
     49      1.1    itojun #include <sys/systm.h>
     50      1.1    itojun #include <sys/kernel.h>
     51      1.1    itojun #include <sys/proc.h>
     52      1.1    itojun #include <sys/device.h>
     53      1.1    itojun #include <sys/fcntl.h>
     54      1.1    itojun #include <sys/ioctl.h>
     55      1.1    itojun #ifdef __OpenBSD__
     56      1.1    itojun #include <sys/event.h>
     57      1.1    itojun #endif
     58      1.1    itojun #ifdef __NetBSD__
     59      1.1    itojun #include <sys/select.h>
     60      1.1    itojun #include <sys/poll.h>
     61      1.1    itojun #include <sys/conf.h>
     62      1.1    itojun #endif
     63      1.1    itojun 
     64      1.1    itojun #ifdef __OpenBSD__
     65      1.1    itojun #include <machine/conf.h>
     66      1.1    itojun #endif
     67      1.1    itojun #include <machine/cpu.h>
     68      1.1    itojun #include <machine/apmvar.h>
     69      1.1    itojun 
     70      1.1    itojun #include <macppc/dev/adbvar.h>
     71      1.1    itojun #include <macppc/dev/adb_direct.h>
     72      1.1    itojun #include <macppc/dev/pm_direct.h>
     73      1.1    itojun 
     74      1.1    itojun #if defined(APMDEBUG)
     75      1.1    itojun #define DPRINTF(x)	printf x
     76      1.1    itojun #else
     77      1.1    itojun #define	DPRINTF(x)	/**/
     78      1.1    itojun #endif
     79      1.1    itojun 
     80      1.1    itojun #define APM_NEVENTS 16
     81      1.1    itojun 
     82      1.1    itojun struct apm_softc {
     83      1.1    itojun 	struct device sc_dev;
     84      1.1    itojun 	struct selinfo sc_rsel;
     85      1.1    itojun #ifdef __OpenBSD__
     86      1.1    itojun 	struct klist sc_note;
     87      1.1    itojun #endif
     88      1.1    itojun 	int    sc_flags;
     89      1.1    itojun 	int	event_count;
     90      1.1    itojun 	int	event_ptr;
     91      1.1    itojun 	struct lock sc_lock;
     92      1.1    itojun 	struct	apm_event_info event_list[APM_NEVENTS];
     93      1.1    itojun };
     94      1.1    itojun 
     95      1.1    itojun /*
     96      1.1    itojun  * A brief note on the locking protocol: it's very simple; we
     97      1.1    itojun  * assert an exclusive lock any time thread context enters the
     98      1.1    itojun  * APM module.  This is both the APM thread itself, as well as
     99      1.1    itojun  * user context.
    100      1.1    itojun  */
    101      1.1    itojun #ifdef __NetBSD__
    102      1.1    itojun #define	APM_LOCK(apmsc)							\
    103      1.1    itojun 	(void) lockmgr(&(apmsc)->sc_lock, LK_EXCLUSIVE, NULL)
    104      1.1    itojun #define	APM_UNLOCK(apmsc)						\
    105      1.1    itojun 	(void) lockmgr(&(apmsc)->sc_lock, LK_RELEASE, NULL)
    106      1.1    itojun #else
    107      1.1    itojun #define APM_LOCK(apmsc)
    108      1.1    itojun #define APM_UNLOCK(apmsc)
    109      1.1    itojun #endif
    110      1.1    itojun 
    111      1.1    itojun int apmmatch(struct device *, struct cfdata *, void *);
    112      1.1    itojun void apmattach(struct device *, struct device *, void *);
    113      1.1    itojun 
    114      1.1    itojun #ifdef __NetBSD__
    115      1.1    itojun #if 0
    116      1.1    itojun static int	apm_record_event __P((struct apm_softc *, u_int));
    117      1.1    itojun #endif
    118      1.1    itojun #endif
    119      1.1    itojun 
    120      1.4   thorpej CFATTACH_DECL(apm, sizeof(struct apm_softc),
    121      1.4   thorpej     apmmatch, apmattach, NULL, NULL);
    122      1.1    itojun 
    123      1.1    itojun #ifdef __OpenBSD__
    124      1.1    itojun struct cfdriver apm_cd = {
    125      1.1    itojun 	NULL, "apm", DV_DULL
    126      1.1    itojun };
    127      1.1    itojun #else
    128      1.1    itojun extern struct cfdriver apm_cd;
    129      1.2   gehenna 
    130      1.2   gehenna dev_type_open(apmopen);
    131      1.2   gehenna dev_type_close(apmclose);
    132      1.2   gehenna dev_type_ioctl(apmioctl);
    133      1.2   gehenna dev_type_poll(apmpoll);
    134      1.5  jdolecek dev_type_kqfilter(apmkqfilter);
    135      1.2   gehenna 
    136      1.2   gehenna const struct cdevsw apm_cdevsw = {
    137      1.2   gehenna 	apmopen, apmclose, noread, nowrite, apmioctl,
    138      1.5  jdolecek 	nostop, notty, apmpoll, nommap, apmkqfilter,
    139      1.2   gehenna };
    140      1.1    itojun #endif
    141      1.1    itojun 
    142      1.1    itojun int	apm_evindex;
    143      1.1    itojun 
    144      1.1    itojun #define	APMUNIT(dev)	(minor(dev)&0xf0)
    145      1.1    itojun #define	APMDEV(dev)	(minor(dev)&0x0f)
    146      1.1    itojun #define APMDEV_NORMAL	0
    147      1.1    itojun #define APMDEV_CTL	8
    148      1.1    itojun 
    149      1.1    itojun /*
    150      1.1    itojun  * Flags to control kernel display
    151      1.1    itojun  *	SCFLAG_NOPRINT:		do not output APM power messages due to
    152      1.1    itojun  *				a power change event.
    153      1.1    itojun  *
    154      1.1    itojun  *	SCFLAG_PCTPRINT:	do not output APM power messages due to
    155      1.1    itojun  *				to a power change event unless the battery
    156      1.1    itojun  *				percentage changes.
    157      1.1    itojun  */
    158      1.1    itojun 
    159      1.1    itojun #define SCFLAG_NOPRINT	0x0008000
    160      1.1    itojun #define SCFLAG_PCTPRINT	0x0004000
    161      1.1    itojun #define SCFLAG_PRINT	(SCFLAG_NOPRINT|SCFLAG_PCTPRINT)
    162      1.1    itojun 
    163      1.1    itojun #define	SCFLAG_OREAD 	(1 << 0)
    164      1.1    itojun #define	SCFLAG_OWRITE	(1 << 1)
    165      1.1    itojun #define	SCFLAG_OPEN	(SCFLAG_OREAD|SCFLAG_OWRITE)
    166      1.1    itojun 
    167      1.1    itojun 
    168      1.1    itojun int
    169      1.1    itojun apmmatch(parent, match, aux)
    170      1.1    itojun 	struct device *parent;
    171      1.1    itojun 	struct cfdata *match;
    172      1.1    itojun 	void *aux;
    173      1.1    itojun {
    174      1.1    itojun 	struct adb_attach_args *aa = (void *)aux;
    175      1.1    itojun 	if (aa->origaddr != ADBADDR_APM ||
    176      1.1    itojun 	    aa->handler_id != ADBADDR_APM ||
    177      1.1    itojun 	    aa->adbaddr != ADBADDR_APM)
    178      1.1    itojun 		return 0;
    179      1.1    itojun 
    180      1.1    itojun 	if (adbHardware != ADB_HW_PB)
    181      1.1    itojun 		return 0;
    182      1.1    itojun 
    183      1.1    itojun 	return 1;
    184      1.1    itojun }
    185      1.1    itojun 
    186      1.1    itojun void
    187      1.1    itojun apmattach(parent, self, aux)
    188      1.1    itojun 	struct device *parent, *self;
    189      1.1    itojun 	void *aux;
    190      1.1    itojun {
    191  1.8.2.1     skrll 	struct apm_softc *sc = (struct apm_softc *) self;
    192      1.1    itojun 	struct pmu_battery_info info;
    193      1.1    itojun 
    194      1.1    itojun 	pm_battery_info(0, &info);
    195      1.1    itojun 
    196      1.1    itojun 	printf(": battery flags 0x%X, ", info.flags);
    197      1.1    itojun 	printf("%d%% charged\n", ((info.cur_charge * 100) / info.max_charge));
    198  1.8.2.1     skrll 
    199  1.8.2.1     skrll 	sc->sc_flags = 0;
    200  1.8.2.1     skrll 	sc->event_ptr = 0;
    201  1.8.2.1     skrll 	sc->event_count = 0;
    202  1.8.2.1     skrll 	lockinit(&sc->sc_lock, PWAIT, "apmlk", 0, 0);
    203      1.1    itojun }
    204      1.1    itojun 
    205      1.1    itojun int
    206      1.8      fvdl apmopen(dev, flag, mode, p)
    207      1.1    itojun 	dev_t dev;
    208      1.1    itojun 	int flag, mode;
    209      1.8      fvdl 	struct proc *p;
    210      1.1    itojun {
    211      1.1    itojun 	struct apm_softc *sc;
    212      1.1    itojun 	int error = 0;
    213      1.1    itojun 
    214      1.1    itojun 	/* apm0 only */
    215      1.1    itojun 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    216      1.1    itojun 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    217      1.1    itojun 		return ENXIO;
    218      1.1    itojun 
    219      1.1    itojun 	DPRINTF(("apmopen: dev %d pid %d flag %x mode %x\n",
    220      1.8      fvdl 	    APMDEV(dev), p->p_pid, flag, mode));
    221      1.1    itojun 
    222      1.1    itojun 	APM_LOCK(sc);
    223      1.1    itojun 	switch (APMDEV(dev)) {
    224      1.1    itojun 	case APMDEV_CTL:
    225      1.1    itojun 		if (!(flag & FWRITE)) {
    226      1.1    itojun 			error = EINVAL;
    227      1.1    itojun 			break;
    228      1.1    itojun 		}
    229      1.1    itojun 		if (sc->sc_flags & SCFLAG_OWRITE) {
    230      1.1    itojun 			error = EBUSY;
    231      1.1    itojun 			break;
    232      1.1    itojun 		}
    233      1.1    itojun 		sc->sc_flags |= SCFLAG_OWRITE;
    234      1.1    itojun 		break;
    235      1.1    itojun 	case APMDEV_NORMAL:
    236      1.1    itojun 		if (!(flag & FREAD) || (flag & FWRITE)) {
    237      1.1    itojun 			error = EINVAL;
    238      1.1    itojun 			break;
    239      1.1    itojun 		}
    240      1.1    itojun 		sc->sc_flags |= SCFLAG_OREAD;
    241      1.1    itojun 		break;
    242      1.1    itojun 	default:
    243      1.1    itojun 		error = ENXIO;
    244      1.1    itojun 		break;
    245      1.1    itojun 	}
    246      1.1    itojun 	APM_UNLOCK(sc);
    247      1.1    itojun 	return error;
    248      1.1    itojun }
    249      1.1    itojun 
    250      1.1    itojun int
    251      1.8      fvdl apmclose(dev, flag, mode, p)
    252      1.1    itojun 	dev_t dev;
    253      1.1    itojun 	int flag, mode;
    254      1.8      fvdl 	struct proc *p;
    255      1.1    itojun {
    256      1.1    itojun 	struct apm_softc *sc;
    257      1.1    itojun 
    258      1.1    itojun 	/* apm0 only */
    259      1.1    itojun 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    260      1.1    itojun 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    261      1.1    itojun 		return ENXIO;
    262      1.1    itojun 
    263      1.8      fvdl 	DPRINTF(("apmclose: pid %d flag %x mode %x\n", p->p_pid, flag, mode));
    264      1.1    itojun 
    265      1.1    itojun 	APM_LOCK(sc);
    266      1.1    itojun 	switch (APMDEV(dev)) {
    267      1.1    itojun 	case APMDEV_CTL:
    268      1.1    itojun 		sc->sc_flags &= ~SCFLAG_OWRITE;
    269      1.1    itojun 		break;
    270      1.1    itojun 	case APMDEV_NORMAL:
    271      1.1    itojun 		sc->sc_flags &= ~SCFLAG_OREAD;
    272      1.1    itojun 		break;
    273      1.1    itojun 	}
    274      1.1    itojun 	APM_UNLOCK(sc);
    275      1.1    itojun 	return 0;
    276      1.1    itojun }
    277      1.1    itojun 
    278      1.1    itojun int
    279      1.8      fvdl apmioctl(dev, cmd, data, flag, p)
    280      1.1    itojun 	dev_t dev;
    281      1.1    itojun 	u_long cmd;
    282      1.1    itojun 	caddr_t data;
    283      1.1    itojun 	int flag;
    284      1.8      fvdl 	struct proc *p;
    285      1.1    itojun {
    286      1.1    itojun 	struct apm_softc *sc;
    287      1.1    itojun 	struct pmu_battery_info batt;
    288      1.1    itojun 	struct apm_power_info *power;
    289      1.1    itojun 	int error = 0;
    290      1.1    itojun 
    291      1.1    itojun 	/* apm0 only */
    292      1.1    itojun 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    293      1.1    itojun 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    294      1.1    itojun 		return ENXIO;
    295      1.1    itojun 
    296      1.1    itojun 	APM_LOCK(sc);
    297      1.1    itojun 	switch (cmd) {
    298      1.1    itojun 		/* some ioctl names from linux */
    299      1.1    itojun 	case APM_IOC_STANDBY:
    300      1.1    itojun 		if ((flag & FWRITE) == 0)
    301      1.1    itojun 			error = EBADF;
    302      1.1    itojun 	case APM_IOC_SUSPEND:
    303      1.1    itojun 		if ((flag & FWRITE) == 0)
    304      1.1    itojun 			error = EBADF;
    305      1.1    itojun 		break;
    306      1.1    itojun 	case APM_IOC_PRN_CTL:
    307      1.1    itojun 		if ((flag & FWRITE) == 0)
    308      1.1    itojun 			error = EBADF;
    309      1.1    itojun 		else {
    310      1.1    itojun 			int flag = *(int *)data;
    311      1.1    itojun 			DPRINTF(( "APM_IOC_PRN_CTL: %d\n", flag ));
    312      1.1    itojun 			switch (flag) {
    313      1.1    itojun 			case APM_PRINT_ON:	/* enable printing */
    314      1.1    itojun 				sc->sc_flags &= ~SCFLAG_PRINT;
    315      1.1    itojun 				break;
    316      1.1    itojun 			case APM_PRINT_OFF: /* disable printing */
    317      1.1    itojun 				sc->sc_flags &= ~SCFLAG_PRINT;
    318      1.1    itojun 				sc->sc_flags |= SCFLAG_NOPRINT;
    319      1.1    itojun 				break;
    320      1.1    itojun 			case APM_PRINT_PCT: /* disable some printing */
    321      1.1    itojun 				sc->sc_flags &= ~SCFLAG_PRINT;
    322      1.1    itojun 				sc->sc_flags |= SCFLAG_PCTPRINT;
    323      1.1    itojun 				break;
    324      1.1    itojun 			default:
    325      1.1    itojun 				error = EINVAL;
    326      1.1    itojun 				break;
    327      1.1    itojun 			}
    328      1.1    itojun 		}
    329      1.1    itojun 		break;
    330      1.1    itojun 	case APM_IOC_DEV_CTL:
    331      1.1    itojun 		if ((flag & FWRITE) == 0)
    332      1.1    itojun 			error = EBADF;
    333      1.1    itojun 		break;
    334      1.1    itojun 	case APM_IOC_GETPOWER:
    335      1.1    itojun 	        power = (struct apm_power_info *)data;
    336      1.1    itojun 
    337      1.1    itojun 		pm_battery_info(0, &batt);
    338      1.1    itojun 
    339      1.1    itojun 		power->ac_state = ((batt.flags & PMU_PWR_AC_PRESENT) ?
    340      1.1    itojun 		    APM_AC_ON : APM_AC_OFF);
    341      1.1    itojun 		power->battery_life =
    342      1.1    itojun 		    ((batt.cur_charge * 100) / batt.max_charge);
    343      1.1    itojun 
    344      1.1    itojun 		/*
    345      1.1    itojun 		 * If the battery is charging, return the minutes left until
    346      1.1    itojun 		 * charging is complete. apmd knows this.
    347      1.1    itojun 		 */
    348      1.1    itojun 
    349      1.1    itojun 		if (!(batt.flags & PMU_PWR_BATT_PRESENT)) {
    350      1.1    itojun 			power->battery_state = APM_BATT_UNKNOWN;
    351      1.1    itojun 			power->minutes_left = 0;
    352      1.1    itojun 			power->battery_life = 0;
    353      1.1    itojun 		} else if ((power->ac_state == APM_AC_ON) &&
    354      1.1    itojun 			   (batt.draw > 0)) {
    355      1.1    itojun 			power->minutes_left =
    356      1.1    itojun 			    (((batt.max_charge - batt.cur_charge) * 3600) /
    357      1.1    itojun 			    batt.draw) / 60;
    358      1.1    itojun 			power->battery_state = APM_BATT_CHARGING;
    359      1.1    itojun 		} else {
    360      1.1    itojun 			power->minutes_left =
    361      1.1    itojun 			    ((batt.cur_charge * 3600) / (-batt.draw)) / 60;
    362      1.1    itojun 
    363      1.1    itojun 			/* XXX - Arbitrary */
    364      1.1    itojun 			if (power->battery_life > 60) {
    365      1.1    itojun 				power->battery_state = APM_BATT_HIGH;
    366      1.1    itojun 			} else if (power->battery_life < 10) {
    367      1.1    itojun 				power->battery_state = APM_BATT_CRITICAL;
    368      1.1    itojun 			} else {
    369      1.1    itojun 				power->battery_state = APM_BATT_LOW;
    370      1.1    itojun 			}
    371      1.1    itojun 		}
    372      1.1    itojun 
    373      1.1    itojun 		break;
    374      1.1    itojun 
    375      1.1    itojun 	default:
    376      1.1    itojun 		error = ENOTTY;
    377      1.1    itojun 	}
    378      1.1    itojun 	APM_UNLOCK(sc);
    379      1.1    itojun 
    380      1.1    itojun 	return error;
    381      1.1    itojun }
    382      1.1    itojun 
    383      1.1    itojun #ifdef __NetBSD__
    384      1.1    itojun #if 0
    385      1.1    itojun /*
    386      1.1    itojun  * return 0 if the user will notice and handle the event,
    387      1.1    itojun  * return 1 if the kernel driver should do so.
    388      1.1    itojun  */
    389      1.1    itojun static int
    390      1.1    itojun apm_record_event(sc, event_type)
    391      1.1    itojun 	struct apm_softc *sc;
    392      1.1    itojun 	u_int event_type;
    393      1.1    itojun {
    394      1.1    itojun 	struct apm_event_info *evp;
    395      1.1    itojun 
    396      1.1    itojun 	if ((sc->sc_flags & SCFLAG_OPEN) == 0)
    397      1.1    itojun 		return 1;		/* no user waiting */
    398      1.1    itojun 	if (sc->event_count == APM_NEVENTS) {
    399      1.1    itojun 		DPRINTF(("apm_record_event: queue full!\n"));
    400      1.1    itojun 		return 1;			/* overflow */
    401      1.1    itojun 	}
    402      1.1    itojun 	evp = &sc->event_list[sc->event_ptr];
    403      1.1    itojun 	sc->event_count++;
    404      1.1    itojun 	sc->event_ptr++;
    405      1.1    itojun 	sc->event_ptr %= APM_NEVENTS;
    406      1.1    itojun 	evp->type = event_type;
    407      1.1    itojun 	evp->index = ++apm_evindex;
    408      1.1    itojun 	selwakeup(&sc->sc_rsel);
    409      1.1    itojun 	return (sc->sc_flags & SCFLAG_OWRITE) ? 0 : 1; /* user may handle */
    410      1.1    itojun }
    411      1.1    itojun #endif
    412      1.1    itojun 
    413      1.1    itojun int
    414      1.8      fvdl apmpoll(dev, events, p)
    415      1.1    itojun 	dev_t dev;
    416      1.1    itojun 	int events;
    417      1.8      fvdl 	struct proc *p;
    418      1.1    itojun {
    419      1.1    itojun 	struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)];
    420      1.1    itojun 	int revents = 0;
    421      1.1    itojun 
    422      1.1    itojun 	APM_LOCK(sc);
    423      1.1    itojun 	if (events & (POLLIN | POLLRDNORM)) {
    424      1.1    itojun 		if (sc->event_count)
    425      1.1    itojun 			revents |= events & (POLLIN | POLLRDNORM);
    426      1.1    itojun 		else
    427      1.8      fvdl 			selrecord(p, &sc->sc_rsel);
    428      1.1    itojun 	}
    429      1.1    itojun 	APM_UNLOCK(sc);
    430      1.1    itojun 
    431      1.1    itojun 	return (revents);
    432      1.1    itojun }
    433      1.1    itojun #endif
    434      1.1    itojun 
    435      1.5  jdolecek static void
    436      1.5  jdolecek filt_apmrdetach(struct knote *kn)
    437      1.1    itojun {
    438      1.1    itojun 	struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
    439      1.1    itojun 
    440      1.5  jdolecek 	APM_LOCK(sc);
    441      1.6  christos 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    442      1.5  jdolecek 	APM_UNLOCK(sc);
    443      1.1    itojun }
    444      1.1    itojun 
    445      1.5  jdolecek static int
    446      1.5  jdolecek filt_apmread(struct knote *kn, long hint)
    447      1.1    itojun {
    448      1.5  jdolecek 	struct apm_softc *sc = kn->kn_hook;
    449      1.1    itojun 
    450      1.5  jdolecek 	kn->kn_data = sc->event_count;
    451      1.5  jdolecek 	return (kn->kn_data > 0);
    452      1.1    itojun }
    453      1.1    itojun 
    454      1.5  jdolecek static struct filterops apmread_filtops =
    455      1.5  jdolecek 	{ 1, NULL, filt_apmrdetach, filt_apmread};
    456      1.5  jdolecek 
    457      1.1    itojun int
    458      1.1    itojun apmkqfilter(dev, kn)
    459      1.1    itojun 	dev_t dev;
    460      1.1    itojun 	struct knote *kn;
    461      1.1    itojun {
    462      1.5  jdolecek 	struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)];
    463      1.5  jdolecek 	struct klist *klist;
    464      1.1    itojun 
    465      1.1    itojun 	switch (kn->kn_filter) {
    466      1.1    itojun 	case EVFILT_READ:
    467      1.6  christos 		klist = &sc->sc_rsel.sel_klist;
    468      1.1    itojun 		kn->kn_fop = &apmread_filtops;
    469      1.1    itojun 		break;
    470      1.1    itojun 	default:
    471      1.1    itojun 		return (1);
    472      1.1    itojun 	}
    473      1.1    itojun 
    474      1.5  jdolecek 	kn->kn_hook = sc;
    475      1.5  jdolecek 
    476      1.5  jdolecek 	APM_LOCK(sc);
    477      1.5  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    478      1.5  jdolecek 	APM_UNLOCK(sc);
    479      1.1    itojun 
    480      1.1    itojun 	return (0);
    481      1.1    itojun }
    482