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