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