Home | History | Annotate | Line # | Download | only in dev
apm.c revision 1.9
      1 /*	$NetBSD: apm.c,v 1.9 2003/07/15 02:43:28 lukem 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 <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: apm.c,v 1.9 2003/07/15 02:43:28 lukem Exp $");
     41 
     42 #include "apm.h"
     43 
     44 #if NAPM > 1
     45 #error only one APM emulation device may be configured
     46 #endif
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/kernel.h>
     51 #include <sys/proc.h>
     52 #include <sys/device.h>
     53 #include <sys/fcntl.h>
     54 #include <sys/ioctl.h>
     55 #ifdef __OpenBSD__
     56 #include <sys/event.h>
     57 #endif
     58 #ifdef __NetBSD__
     59 #include <sys/select.h>
     60 #include <sys/poll.h>
     61 #include <sys/conf.h>
     62 #endif
     63 
     64 #ifdef __OpenBSD__
     65 #include <machine/conf.h>
     66 #endif
     67 #include <machine/cpu.h>
     68 #include <machine/apmvar.h>
     69 
     70 #include <macppc/dev/adbvar.h>
     71 #include <macppc/dev/adb_direct.h>
     72 #include <macppc/dev/pm_direct.h>
     73 
     74 #if defined(APMDEBUG)
     75 #define DPRINTF(x)	printf x
     76 #else
     77 #define	DPRINTF(x)	/**/
     78 #endif
     79 
     80 #define APM_NEVENTS 16
     81 
     82 struct apm_softc {
     83 	struct device sc_dev;
     84 	struct selinfo sc_rsel;
     85 #ifdef __OpenBSD__
     86 	struct klist sc_note;
     87 #endif
     88 	int    sc_flags;
     89 	int	event_count;
     90 	int	event_ptr;
     91 	struct lock sc_lock;
     92 	struct	apm_event_info event_list[APM_NEVENTS];
     93 };
     94 
     95 /*
     96  * A brief note on the locking protocol: it's very simple; we
     97  * assert an exclusive lock any time thread context enters the
     98  * APM module.  This is both the APM thread itself, as well as
     99  * user context.
    100  */
    101 #ifdef __NetBSD__
    102 #define	APM_LOCK(apmsc)							\
    103 	(void) lockmgr(&(apmsc)->sc_lock, LK_EXCLUSIVE, NULL)
    104 #define	APM_UNLOCK(apmsc)						\
    105 	(void) lockmgr(&(apmsc)->sc_lock, LK_RELEASE, NULL)
    106 #else
    107 #define APM_LOCK(apmsc)
    108 #define APM_UNLOCK(apmsc)
    109 #endif
    110 
    111 int apmmatch(struct device *, struct cfdata *, void *);
    112 void apmattach(struct device *, struct device *, void *);
    113 
    114 #ifdef __NetBSD__
    115 #if 0
    116 static int	apm_record_event __P((struct apm_softc *, u_int));
    117 #endif
    118 #endif
    119 
    120 CFATTACH_DECL(apm, sizeof(struct apm_softc),
    121     apmmatch, apmattach, NULL, NULL);
    122 
    123 #ifdef __OpenBSD__
    124 struct cfdriver apm_cd = {
    125 	NULL, "apm", DV_DULL
    126 };
    127 #else
    128 extern struct cfdriver apm_cd;
    129 
    130 dev_type_open(apmopen);
    131 dev_type_close(apmclose);
    132 dev_type_ioctl(apmioctl);
    133 dev_type_poll(apmpoll);
    134 dev_type_kqfilter(apmkqfilter);
    135 
    136 const struct cdevsw apm_cdevsw = {
    137 	apmopen, apmclose, noread, nowrite, apmioctl,
    138 	nostop, notty, apmpoll, nommap, apmkqfilter,
    139 };
    140 #endif
    141 
    142 int	apm_evindex;
    143 
    144 #define	APMUNIT(dev)	(minor(dev)&0xf0)
    145 #define	APMDEV(dev)	(minor(dev)&0x0f)
    146 #define APMDEV_NORMAL	0
    147 #define APMDEV_CTL	8
    148 
    149 /*
    150  * Flags to control kernel display
    151  *	SCFLAG_NOPRINT:		do not output APM power messages due to
    152  *				a power change event.
    153  *
    154  *	SCFLAG_PCTPRINT:	do not output APM power messages due to
    155  *				to a power change event unless the battery
    156  *				percentage changes.
    157  */
    158 
    159 #define SCFLAG_NOPRINT	0x0008000
    160 #define SCFLAG_PCTPRINT	0x0004000
    161 #define SCFLAG_PRINT	(SCFLAG_NOPRINT|SCFLAG_PCTPRINT)
    162 
    163 #define	SCFLAG_OREAD 	(1 << 0)
    164 #define	SCFLAG_OWRITE	(1 << 1)
    165 #define	SCFLAG_OPEN	(SCFLAG_OREAD|SCFLAG_OWRITE)
    166 
    167 
    168 int
    169 apmmatch(parent, match, aux)
    170 	struct device *parent;
    171 	struct cfdata *match;
    172 	void *aux;
    173 {
    174 	struct adb_attach_args *aa = (void *)aux;
    175 	if (aa->origaddr != ADBADDR_APM ||
    176 	    aa->handler_id != ADBADDR_APM ||
    177 	    aa->adbaddr != ADBADDR_APM)
    178 		return 0;
    179 
    180 	if (adbHardware != ADB_HW_PB)
    181 		return 0;
    182 
    183 	return 1;
    184 }
    185 
    186 void
    187 apmattach(parent, self, aux)
    188 	struct device *parent, *self;
    189 	void *aux;
    190 {
    191 	struct pmu_battery_info info;
    192 
    193 	pm_battery_info(0, &info);
    194 
    195 	printf(": battery flags 0x%X, ", info.flags);
    196 	printf("%d%% charged\n", ((info.cur_charge * 100) / info.max_charge));
    197 }
    198 
    199 int
    200 apmopen(dev, flag, mode, p)
    201 	dev_t dev;
    202 	int flag, mode;
    203 	struct proc *p;
    204 {
    205 	struct apm_softc *sc;
    206 	int error = 0;
    207 
    208 	/* apm0 only */
    209 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    210 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    211 		return ENXIO;
    212 
    213 	DPRINTF(("apmopen: dev %d pid %d flag %x mode %x\n",
    214 	    APMDEV(dev), p->p_pid, flag, mode));
    215 
    216 	APM_LOCK(sc);
    217 	switch (APMDEV(dev)) {
    218 	case APMDEV_CTL:
    219 		if (!(flag & FWRITE)) {
    220 			error = EINVAL;
    221 			break;
    222 		}
    223 		if (sc->sc_flags & SCFLAG_OWRITE) {
    224 			error = EBUSY;
    225 			break;
    226 		}
    227 		sc->sc_flags |= SCFLAG_OWRITE;
    228 		break;
    229 	case APMDEV_NORMAL:
    230 		if (!(flag & FREAD) || (flag & FWRITE)) {
    231 			error = EINVAL;
    232 			break;
    233 		}
    234 		sc->sc_flags |= SCFLAG_OREAD;
    235 		break;
    236 	default:
    237 		error = ENXIO;
    238 		break;
    239 	}
    240 	APM_UNLOCK(sc);
    241 	return error;
    242 }
    243 
    244 int
    245 apmclose(dev, flag, mode, p)
    246 	dev_t dev;
    247 	int flag, mode;
    248 	struct proc *p;
    249 {
    250 	struct apm_softc *sc;
    251 
    252 	/* apm0 only */
    253 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    254 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    255 		return ENXIO;
    256 
    257 	DPRINTF(("apmclose: pid %d flag %x mode %x\n", p->p_pid, flag, mode));
    258 
    259 	APM_LOCK(sc);
    260 	switch (APMDEV(dev)) {
    261 	case APMDEV_CTL:
    262 		sc->sc_flags &= ~SCFLAG_OWRITE;
    263 		break;
    264 	case APMDEV_NORMAL:
    265 		sc->sc_flags &= ~SCFLAG_OREAD;
    266 		break;
    267 	}
    268 	APM_UNLOCK(sc);
    269 	return 0;
    270 }
    271 
    272 int
    273 apmioctl(dev, cmd, data, flag, p)
    274 	dev_t dev;
    275 	u_long cmd;
    276 	caddr_t data;
    277 	int flag;
    278 	struct proc *p;
    279 {
    280 	struct apm_softc *sc;
    281 	struct pmu_battery_info batt;
    282 	struct apm_power_info *power;
    283 	int error = 0;
    284 
    285 	/* apm0 only */
    286 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    287 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    288 		return ENXIO;
    289 
    290 	APM_LOCK(sc);
    291 	switch (cmd) {
    292 		/* some ioctl names from linux */
    293 	case APM_IOC_STANDBY:
    294 		if ((flag & FWRITE) == 0)
    295 			error = EBADF;
    296 	case APM_IOC_SUSPEND:
    297 		if ((flag & FWRITE) == 0)
    298 			error = EBADF;
    299 		break;
    300 	case APM_IOC_PRN_CTL:
    301 		if ((flag & FWRITE) == 0)
    302 			error = EBADF;
    303 		else {
    304 			int flag = *(int *)data;
    305 			DPRINTF(( "APM_IOC_PRN_CTL: %d\n", flag ));
    306 			switch (flag) {
    307 			case APM_PRINT_ON:	/* enable printing */
    308 				sc->sc_flags &= ~SCFLAG_PRINT;
    309 				break;
    310 			case APM_PRINT_OFF: /* disable printing */
    311 				sc->sc_flags &= ~SCFLAG_PRINT;
    312 				sc->sc_flags |= SCFLAG_NOPRINT;
    313 				break;
    314 			case APM_PRINT_PCT: /* disable some printing */
    315 				sc->sc_flags &= ~SCFLAG_PRINT;
    316 				sc->sc_flags |= SCFLAG_PCTPRINT;
    317 				break;
    318 			default:
    319 				error = EINVAL;
    320 				break;
    321 			}
    322 		}
    323 		break;
    324 	case APM_IOC_DEV_CTL:
    325 		if ((flag & FWRITE) == 0)
    326 			error = EBADF;
    327 		break;
    328 	case APM_IOC_GETPOWER:
    329 	        power = (struct apm_power_info *)data;
    330 
    331 		pm_battery_info(0, &batt);
    332 
    333 		power->ac_state = ((batt.flags & PMU_PWR_AC_PRESENT) ?
    334 		    APM_AC_ON : APM_AC_OFF);
    335 		power->battery_life =
    336 		    ((batt.cur_charge * 100) / batt.max_charge);
    337 
    338 		/*
    339 		 * If the battery is charging, return the minutes left until
    340 		 * charging is complete. apmd knows this.
    341 		 */
    342 
    343 		if (!(batt.flags & PMU_PWR_BATT_PRESENT)) {
    344 			power->battery_state = APM_BATT_UNKNOWN;
    345 			power->minutes_left = 0;
    346 			power->battery_life = 0;
    347 		} else if ((power->ac_state == APM_AC_ON) &&
    348 			   (batt.draw > 0)) {
    349 			power->minutes_left =
    350 			    (((batt.max_charge - batt.cur_charge) * 3600) /
    351 			    batt.draw) / 60;
    352 			power->battery_state = APM_BATT_CHARGING;
    353 		} else {
    354 			power->minutes_left =
    355 			    ((batt.cur_charge * 3600) / (-batt.draw)) / 60;
    356 
    357 			/* XXX - Arbitrary */
    358 			if (power->battery_life > 60) {
    359 				power->battery_state = APM_BATT_HIGH;
    360 			} else if (power->battery_life < 10) {
    361 				power->battery_state = APM_BATT_CRITICAL;
    362 			} else {
    363 				power->battery_state = APM_BATT_LOW;
    364 			}
    365 		}
    366 
    367 		break;
    368 
    369 	default:
    370 		error = ENOTTY;
    371 	}
    372 	APM_UNLOCK(sc);
    373 
    374 	return error;
    375 }
    376 
    377 #ifdef __NetBSD__
    378 #if 0
    379 /*
    380  * return 0 if the user will notice and handle the event,
    381  * return 1 if the kernel driver should do so.
    382  */
    383 static int
    384 apm_record_event(sc, event_type)
    385 	struct apm_softc *sc;
    386 	u_int event_type;
    387 {
    388 	struct apm_event_info *evp;
    389 
    390 	if ((sc->sc_flags & SCFLAG_OPEN) == 0)
    391 		return 1;		/* no user waiting */
    392 	if (sc->event_count == APM_NEVENTS) {
    393 		DPRINTF(("apm_record_event: queue full!\n"));
    394 		return 1;			/* overflow */
    395 	}
    396 	evp = &sc->event_list[sc->event_ptr];
    397 	sc->event_count++;
    398 	sc->event_ptr++;
    399 	sc->event_ptr %= APM_NEVENTS;
    400 	evp->type = event_type;
    401 	evp->index = ++apm_evindex;
    402 	selwakeup(&sc->sc_rsel);
    403 	return (sc->sc_flags & SCFLAG_OWRITE) ? 0 : 1; /* user may handle */
    404 }
    405 #endif
    406 
    407 int
    408 apmpoll(dev, events, p)
    409 	dev_t dev;
    410 	int events;
    411 	struct proc *p;
    412 {
    413 	struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)];
    414 	int revents = 0;
    415 
    416 	APM_LOCK(sc);
    417 	if (events & (POLLIN | POLLRDNORM)) {
    418 		if (sc->event_count)
    419 			revents |= events & (POLLIN | POLLRDNORM);
    420 		else
    421 			selrecord(p, &sc->sc_rsel);
    422 	}
    423 	APM_UNLOCK(sc);
    424 
    425 	return (revents);
    426 }
    427 #endif
    428 
    429 static void
    430 filt_apmrdetach(struct knote *kn)
    431 {
    432 	struct apm_softc *sc = (struct apm_softc *)kn->kn_hook;
    433 
    434 	APM_LOCK(sc);
    435 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    436 	APM_UNLOCK(sc);
    437 }
    438 
    439 static int
    440 filt_apmread(struct knote *kn, long hint)
    441 {
    442 	struct apm_softc *sc = kn->kn_hook;
    443 
    444 	kn->kn_data = sc->event_count;
    445 	return (kn->kn_data > 0);
    446 }
    447 
    448 static struct filterops apmread_filtops =
    449 	{ 1, NULL, filt_apmrdetach, filt_apmread};
    450 
    451 int
    452 apmkqfilter(dev, kn)
    453 	dev_t dev;
    454 	struct knote *kn;
    455 {
    456 	struct apm_softc *sc = apm_cd.cd_devs[APMUNIT(dev)];
    457 	struct klist *klist;
    458 
    459 	switch (kn->kn_filter) {
    460 	case EVFILT_READ:
    461 		klist = &sc->sc_rsel.sel_klist;
    462 		kn->kn_fop = &apmread_filtops;
    463 		break;
    464 	default:
    465 		return (1);
    466 	}
    467 
    468 	kn->kn_hook = sc;
    469 
    470 	APM_LOCK(sc);
    471 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    472 	APM_UNLOCK(sc);
    473 
    474 	return (0);
    475 }
    476