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