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