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