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