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