Home | History | Annotate | Line # | Download | only in dev
apm.c revision 1.1.6.3
      1 /*	$NetBSD: apm.c,v 1.1.6.3 2002/10/02 22:02:24 jdolecek 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 #ifdef __NetBSD__
    118 cdev_decl(apm);
    119 #endif
    120 
    121 struct cfattach apm_ca = {
    122 	sizeof(struct apm_softc), apmmatch, apmattach
    123 };
    124 
    125 #ifdef __OpenBSD__
    126 struct cfdriver apm_cd = {
    127 	NULL, "apm", DV_DULL
    128 };
    129 #else
    130 extern struct cfdriver apm_cd;
    131 #endif
    132 
    133 int	apm_evindex;
    134 
    135 #define	APMUNIT(dev)	(minor(dev)&0xf0)
    136 #define	APMDEV(dev)	(minor(dev)&0x0f)
    137 #define APMDEV_NORMAL	0
    138 #define APMDEV_CTL	8
    139 
    140 /*
    141  * Flags to control kernel display
    142  *	SCFLAG_NOPRINT:		do not output APM power messages due to
    143  *				a power change event.
    144  *
    145  *	SCFLAG_PCTPRINT:	do not output APM power messages due to
    146  *				to a power change event unless the battery
    147  *				percentage changes.
    148  */
    149 
    150 #define SCFLAG_NOPRINT	0x0008000
    151 #define SCFLAG_PCTPRINT	0x0004000
    152 #define SCFLAG_PRINT	(SCFLAG_NOPRINT|SCFLAG_PCTPRINT)
    153 
    154 #define	SCFLAG_OREAD 	(1 << 0)
    155 #define	SCFLAG_OWRITE	(1 << 1)
    156 #define	SCFLAG_OPEN	(SCFLAG_OREAD|SCFLAG_OWRITE)
    157 
    158 
    159 int
    160 apmmatch(parent, match, aux)
    161 	struct device *parent;
    162 	struct cfdata *match;
    163 	void *aux;
    164 {
    165 	struct adb_attach_args *aa = (void *)aux;
    166 	if (aa->origaddr != ADBADDR_APM ||
    167 	    aa->handler_id != ADBADDR_APM ||
    168 	    aa->adbaddr != ADBADDR_APM)
    169 		return 0;
    170 
    171 	if (adbHardware != ADB_HW_PB)
    172 		return 0;
    173 
    174 	return 1;
    175 }
    176 
    177 void
    178 apmattach(parent, self, aux)
    179 	struct device *parent, *self;
    180 	void *aux;
    181 {
    182 	struct pmu_battery_info info;
    183 
    184 	pm_battery_info(0, &info);
    185 
    186 	printf(": battery flags 0x%X, ", info.flags);
    187 	printf("%d%% charged\n", ((info.cur_charge * 100) / info.max_charge));
    188 }
    189 
    190 int
    191 apmopen(dev, flag, mode, p)
    192 	dev_t dev;
    193 	int flag, mode;
    194 	struct proc *p;
    195 {
    196 	struct apm_softc *sc;
    197 	int error = 0;
    198 
    199 	/* apm0 only */
    200 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    201 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    202 		return ENXIO;
    203 
    204 	DPRINTF(("apmopen: dev %d pid %d flag %x mode %x\n",
    205 	    APMDEV(dev), p->p_pid, flag, mode));
    206 
    207 	APM_LOCK(sc);
    208 	switch (APMDEV(dev)) {
    209 	case APMDEV_CTL:
    210 		if (!(flag & FWRITE)) {
    211 			error = EINVAL;
    212 			break;
    213 		}
    214 		if (sc->sc_flags & SCFLAG_OWRITE) {
    215 			error = EBUSY;
    216 			break;
    217 		}
    218 		sc->sc_flags |= SCFLAG_OWRITE;
    219 		break;
    220 	case APMDEV_NORMAL:
    221 		if (!(flag & FREAD) || (flag & FWRITE)) {
    222 			error = EINVAL;
    223 			break;
    224 		}
    225 		sc->sc_flags |= SCFLAG_OREAD;
    226 		break;
    227 	default:
    228 		error = ENXIO;
    229 		break;
    230 	}
    231 	APM_UNLOCK(sc);
    232 	return error;
    233 }
    234 
    235 int
    236 apmclose(dev, flag, mode, p)
    237 	dev_t dev;
    238 	int flag, mode;
    239 	struct proc *p;
    240 {
    241 	struct apm_softc *sc;
    242 
    243 	/* apm0 only */
    244 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    245 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    246 		return ENXIO;
    247 
    248 	DPRINTF(("apmclose: pid %d flag %x mode %x\n", p->p_pid, flag, mode));
    249 
    250 	APM_LOCK(sc);
    251 	switch (APMDEV(dev)) {
    252 	case APMDEV_CTL:
    253 		sc->sc_flags &= ~SCFLAG_OWRITE;
    254 		break;
    255 	case APMDEV_NORMAL:
    256 		sc->sc_flags &= ~SCFLAG_OREAD;
    257 		break;
    258 	}
    259 	APM_UNLOCK(sc);
    260 	return 0;
    261 }
    262 
    263 int
    264 apmioctl(dev, cmd, data, flag, p)
    265 	dev_t dev;
    266 	u_long cmd;
    267 	caddr_t data;
    268 	int flag;
    269 	struct proc *p;
    270 {
    271 	struct apm_softc *sc;
    272 	struct pmu_battery_info batt;
    273 	struct apm_power_info *power;
    274 	int error = 0;
    275 
    276 	/* apm0 only */
    277 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    278 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    279 		return ENXIO;
    280 
    281 	APM_LOCK(sc);
    282 	switch (cmd) {
    283 		/* some ioctl names from linux */
    284 	case APM_IOC_STANDBY:
    285 		if ((flag & FWRITE) == 0)
    286 			error = EBADF;
    287 	case APM_IOC_SUSPEND:
    288 		if ((flag & FWRITE) == 0)
    289 			error = EBADF;
    290 		break;
    291 	case APM_IOC_PRN_CTL:
    292 		if ((flag & FWRITE) == 0)
    293 			error = EBADF;
    294 		else {
    295 			int flag = *(int *)data;
    296 			DPRINTF(( "APM_IOC_PRN_CTL: %d\n", flag ));
    297 			switch (flag) {
    298 			case APM_PRINT_ON:	/* enable printing */
    299 				sc->sc_flags &= ~SCFLAG_PRINT;
    300 				break;
    301 			case APM_PRINT_OFF: /* disable printing */
    302 				sc->sc_flags &= ~SCFLAG_PRINT;
    303 				sc->sc_flags |= SCFLAG_NOPRINT;
    304 				break;
    305 			case APM_PRINT_PCT: /* disable some printing */
    306 				sc->sc_flags &= ~SCFLAG_PRINT;
    307 				sc->sc_flags |= SCFLAG_PCTPRINT;
    308 				break;
    309 			default:
    310 				error = EINVAL;
    311 				break;
    312 			}
    313 		}
    314 		break;
    315 	case APM_IOC_DEV_CTL:
    316 		if ((flag & FWRITE) == 0)
    317 			error = EBADF;
    318 		break;
    319 	case APM_IOC_GETPOWER:
    320 	        power = (struct apm_power_info *)data;
    321 
    322 		pm_battery_info(0, &batt);
    323 
    324 		power->ac_state = ((batt.flags & PMU_PWR_AC_PRESENT) ?
    325 		    APM_AC_ON : APM_AC_OFF);
    326 		power->battery_life =
    327 		    ((batt.cur_charge * 100) / batt.max_charge);
    328 
    329 		/*
    330 		 * If the battery is charging, return the minutes left until
    331 		 * charging is complete. apmd knows this.
    332 		 */
    333 
    334 		if (!(batt.flags & PMU_PWR_BATT_PRESENT)) {
    335 			power->battery_state = APM_BATT_UNKNOWN;
    336 			power->minutes_left = 0;
    337 			power->battery_life = 0;
    338 		} else if ((power->ac_state == APM_AC_ON) &&
    339 			   (batt.draw > 0)) {
    340 			power->minutes_left =
    341 			    (((batt.max_charge - batt.cur_charge) * 3600) /
    342 			    batt.draw) / 60;
    343 			power->battery_state = APM_BATT_CHARGING;
    344 		} else {
    345 			power->minutes_left =
    346 			    ((batt.cur_charge * 3600) / (-batt.draw)) / 60;
    347 
    348 			/* XXX - Arbitrary */
    349 			if (power->battery_life > 60) {
    350 				power->battery_state = APM_BATT_HIGH;
    351 			} else if (power->battery_life < 10) {
    352 				power->battery_state = APM_BATT_CRITICAL;
    353 			} else {
    354 				power->battery_state = APM_BATT_LOW;
    355 			}
    356 		}
    357 
    358 		break;
    359 
    360 	default:
    361 		error = ENOTTY;
    362 	}
    363 	APM_UNLOCK(sc);
    364 
    365 	return error;
    366 }
    367 
    368 #ifdef __NetBSD__
    369 #if 0
    370 /*
    371  * return 0 if the user will notice and handle the event,
    372  * return 1 if the kernel driver should do so.
    373  */
    374 static int
    375 apm_record_event(sc, event_type)
    376 	struct apm_softc *sc;
    377 	u_int event_type;
    378 {
    379 	struct apm_event_info *evp;
    380 
    381 	if ((sc->sc_flags & SCFLAG_OPEN) == 0)
    382 		return 1;		/* no user waiting */
    383 	if (sc->event_count == APM_NEVENTS) {
    384 		DPRINTF(("apm_record_event: queue full!\n"));
    385 		return 1;			/* overflow */
    386 	}
    387 	evp = &sc->event_list[sc->event_ptr];
    388 	sc->event_count++;
    389 	sc->event_ptr++;
    390 	sc->event_ptr %= APM_NEVENTS;
    391 	evp->type = event_type;
    392 	evp->index = ++apm_evindex;
    393 	selwakeup(&sc->sc_rsel);
    394 	return (sc->sc_flags & SCFLAG_OWRITE) ? 0 : 1; /* user may handle */
    395 }
    396 #endif
    397 
    398 int
    399 apmpoll(dev, events, p)
    400 	dev_t dev;
    401 	int events;
    402 	struct proc *p;
    403 {
    404 	struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)];
    405 	int revents = 0;
    406 
    407 	APM_LOCK(sc);
    408 	if (events & (POLLIN | POLLRDNORM)) {
    409 		if (sc->event_count)
    410 			revents |= events & (POLLIN | POLLRDNORM);
    411 		else
    412 			selrecord(p, &sc->sc_rsel);
    413 	}
    414 	APM_UNLOCK(sc);
    415 
    416 	return (revents);
    417 }
    418 #endif
    419 
    420 static void
    421 filt_apmrdetach(struct knote *kn)
    422 {
    423 	struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
    424 
    425 	APM_LOCK(sc);
    426 	SLIST_REMOVE(&sc->sc_rsel.si_klist, kn, knote, kn_selnext);
    427 	APM_UNLOCK(sc);
    428 }
    429 
    430 static int
    431 filt_apmread(struct knote *kn, long hint)
    432 {
    433 	struct apm_softc *sc = kn->kn_hook;
    434 
    435 	kn->kn_data = sc->event_count;
    436 	return (kn->kn_data > 0);
    437 }
    438 
    439 static struct filterops apmread_filtops =
    440 	{ 1, NULL, filt_apmrdetach, filt_apmread};
    441 
    442 int
    443 apmkqfilter(dev, kn)
    444 	dev_t dev;
    445 	struct knote *kn;
    446 {
    447 	struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)];
    448 	struct klist *klist;
    449 
    450 	switch (kn->kn_filter) {
    451 	case EVFILT_READ:
    452 		klist = &sc->sc_rsel.si_klist;
    453 		kn->kn_fop = &apmread_filtops;
    454 		break;
    455 	default:
    456 		return (1);
    457 	}
    458 
    459 	kn->kn_hook = sc;
    460 
    461 	APM_LOCK(sc);
    462 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    463 	APM_UNLOCK(sc);
    464 
    465 	return (0);
    466 }
    467