proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / platform / pc32 / apm / apm.c
CommitLineData
984263bc
MD
1/*
2 * APM (Advanced Power Management) BIOS Device Driver
3 *
4 * Copyright (c) 1994 UKAI, Fumitoshi.
5 * Copyright (c) 1994-1995 by HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
6 * Copyright (c) 1996 Nate Williams <nate@FreeBSD.org>
7 * Copyright (c) 1997 Poul-Henning Kamp <phk@FreeBSD.org>
8 *
9 * This software may be used, modified, copied, and distributed, in
10 * both source and binary form provided that the above copyright and
11 * these terms are retained. Under no circumstances is the author
12 * responsible for the proper functioning of this software, nor does
13 * the author assume any responsibility for damages incurred with its
14 * use.
15 *
16 * Sep, 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
17 *
18 * $FreeBSD: src/sys/i386/apm/apm.c,v 1.114.2.5 2002/11/02 04:41:50 iwasaki Exp $
41c20dac 19 * $DragonFly: src/sys/platform/pc32/apm/apm.c,v 1.3 2003/06/23 17:55:37 dillon Exp $
984263bc
MD
20 */
21
22#include <sys/param.h>
23#include <sys/systm.h>
24#include <sys/eventhandler.h>
25#include <sys/conf.h>
26#include <sys/kernel.h>
27#include <sys/time.h>
28#include <sys/reboot.h>
29#include <sys/bus.h>
30#include <sys/select.h>
31#include <sys/poll.h>
32#include <sys/fcntl.h>
33#include <sys/proc.h>
34#include <sys/uio.h>
35#include <sys/signalvar.h>
36#include <sys/sysctl.h>
37#include <machine/apm_bios.h>
38#include <machine/segments.h>
39#include <machine/clock.h>
40#include <vm/vm.h>
41#include <vm/vm_param.h>
42#include <vm/pmap.h>
43#include <sys/syslog.h>
44
45#include <machine/pc/bios.h>
46#include <machine/vm86.h>
47
48#include <i386/apm/apm.h>
49
50/* Used by the apm_saver screen saver module */
51int apm_display __P((int newstate));
52struct apm_softc apm_softc;
53
54static void apm_resume __P((void));
55static int apm_bioscall(void);
56static int apm_check_function_supported __P((u_int version, u_int func));
57
58static u_long apm_version;
59
60int apm_evindex;
61
62#define SCFLAG_ONORMAL 0x0000001
63#define SCFLAG_OCTL 0x0000002
64#define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
65
66#define APMDEV(dev) (minor(dev)&0x0f)
67#define APMDEV_NORMAL 0
68#define APMDEV_CTL 8
69
70static struct apmhook *hook[NAPM_HOOK]; /* XXX */
71
72#define is_enabled(foo) ((foo) ? "enabled" : "disabled")
73
74/* Map version number to integer (keeps ordering of version numbers) */
75#define INTVERSION(major, minor) ((major)*100 + (minor))
76
77static struct callout_handle apm_timeout_ch =
78 CALLOUT_HANDLE_INITIALIZER(&apm_timeout_ch);
79
80static timeout_t apm_timeout;
81static d_open_t apmopen;
82static d_close_t apmclose;
83static d_write_t apmwrite;
84static d_ioctl_t apmioctl;
85static d_poll_t apmpoll;
86
87#define CDEV_MAJOR 39
88static struct cdevsw apm_cdevsw = {
89 /* open */ apmopen,
90 /* close */ apmclose,
91 /* read */ noread,
92 /* write */ apmwrite,
93 /* ioctl */ apmioctl,
94 /* poll */ apmpoll,
95 /* mmap */ nommap,
96 /* strategy */ nostrategy,
97 /* name */ "apm",
98 /* maj */ CDEV_MAJOR,
99 /* dump */ nodump,
100 /* psize */ nopsize,
101 /* flags */ 0,
102 /* bmaj */ -1
103};
104
105static int apm_suspend_delay = 1;
106static int apm_standby_delay = 1;
107static int apm_debug = 0;
108
109#define APM_DPRINT(args...) do { \
110 if (apm_debug) { \
111 printf(args); \
112 } \
113} while (0)
114
115SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
116SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
117SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
118
119/*
120 * return 0 if the function successfull,
121 * return 1 if the function unsuccessfull,
122 * return -1 if the function unsupported.
123 */
124static int
125apm_bioscall(void)
126{
127 struct apm_softc *sc = &apm_softc;
128 int errno = 0;
129 u_int apm_func = sc->bios.r.eax & 0xff;
130
131 if (!apm_check_function_supported(sc->intversion, apm_func)) {
132 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
133 apm_func, sc->majorversion, sc->minorversion);
134 return (-1);
135 }
136
137 sc->bios_busy = 1;
138 if (sc->connectmode == APM_PROT32CONNECT) {
139 set_bios_selectors(&sc->bios.seg,
140 BIOSCODE_FLAG | BIOSDATA_FLAG);
141 errno = bios32(&sc->bios.r,
142 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
143 } else {
144 errno = bios16(&sc->bios, NULL);
145 }
146 sc->bios_busy = 0;
147 return (errno);
148}
149
150/* check whether APM function is supported (1) or not (0). */
151static int
152apm_check_function_supported(u_int version, u_int func)
153{
154 /* except driver version */
155 if (func == APM_DRVVERSION) {
156 return (1);
157 }
158
159 switch (version) {
160 case INTVERSION(1, 0):
161 if (func > APM_GETPMEVENT) {
162 return (0); /* not supported */
163 }
164 break;
165 case INTVERSION(1, 1):
166 if (func > APM_ENGAGEDISENGAGEPM &&
167 func < APM_OEMFUNC) {
168 return (0); /* not supported */
169 }
170 break;
171 case INTVERSION(1, 2):
172 break;
173 }
174
175 return (1); /* supported */
176}
177
178/* enable/disable power management */
179static int
180apm_enable_disable_pm(int enable)
181{
182 struct apm_softc *sc = &apm_softc;
183
184 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
185
186 if (sc->intversion >= INTVERSION(1, 1))
187 sc->bios.r.ebx = PMDV_ALLDEV;
188 else
189 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
190 sc->bios.r.ecx = enable;
191 sc->bios.r.edx = 0;
192 return (apm_bioscall());
193}
194
195/* register driver version (APM 1.1 or later) */
196static int
197apm_driver_version(int version)
198{
199 struct apm_softc *sc = &apm_softc;
200
201 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
202 sc->bios.r.ebx = 0x0;
203 sc->bios.r.ecx = version;
204 sc->bios.r.edx = 0;
205
206 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
207 return (0);
208
209 /* Some old BIOSes don't return the connection version in %ax. */
210 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
211 return (0);
212
213 return (1);
214}
215
216/* engage/disengage power management (APM 1.1 or later) */
217static int
218apm_engage_disengage_pm(int engage)
219{
220 struct apm_softc *sc = &apm_softc;
221
222 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
223 sc->bios.r.ebx = PMDV_ALLDEV;
224 sc->bios.r.ecx = engage;
225 sc->bios.r.edx = 0;
226 return (apm_bioscall());
227}
228
229/* get PM event */
230static u_int
231apm_getevent(void)
232{
233 struct apm_softc *sc = &apm_softc;
234
235 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
236
237 sc->bios.r.ebx = 0;
238 sc->bios.r.ecx = 0;
239 sc->bios.r.edx = 0;
240 if (apm_bioscall())
241 return (PMEV_NOEVENT);
242 return (sc->bios.r.ebx & 0xffff);
243}
244
245/* suspend entire system */
246static int
247apm_suspend_system(int state)
248{
249 struct apm_softc *sc = &apm_softc;
250
251 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
252 sc->bios.r.ebx = PMDV_ALLDEV;
253 sc->bios.r.ecx = state;
254 sc->bios.r.edx = 0;
255
256 if (apm_bioscall()) {
257 printf("Entire system suspend failure: errcode = %d\n",
258 0xff & (sc->bios.r.eax >> 8));
259 return 1;
260 }
261 return 0;
262}
263
264/* Display control */
265/*
266 * Experimental implementation: My laptop machine can't handle this function
267 * If your laptop can control the display via APM, please inform me.
268 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
269 */
270int
271apm_display(int newstate)
272{
273 struct apm_softc *sc = &apm_softc;
274
275 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
276 sc->bios.r.ebx = PMDV_DISP0;
277 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
278 sc->bios.r.edx = 0;
279 if (apm_bioscall() == 0) {
280 return 0;
281 }
282
283 /* If failed, then try to blank all display devices instead. */
284 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
285 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
286 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
287 sc->bios.r.edx = 0;
288 if (apm_bioscall() == 0) {
289 return 0;
290 }
291 printf("Display off failure: errcode = %d\n",
292 0xff & (sc->bios.r.eax >> 8));
293 return 1;
294}
295
296/*
297 * Turn off the entire system.
298 */
299static void
300apm_power_off(void *junk, int howto)
301{
302 struct apm_softc *sc = &apm_softc;
303
304 /* Not halting powering off, or not active */
305 if (!(howto & RB_POWEROFF) || !apm_softc.active)
306 return;
307 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
308 sc->bios.r.ebx = PMDV_ALLDEV;
309 sc->bios.r.ecx = PMST_OFF;
310 sc->bios.r.edx = 0;
311 (void) apm_bioscall();
312}
313
314/* APM Battery low handler */
315static void
316apm_battery_low(void)
317{
318 printf("\007\007 * * * BATTERY IS LOW * * * \007\007");
319}
320
321/* APM hook manager */
322static struct apmhook *
323apm_add_hook(struct apmhook **list, struct apmhook *ah)
324{
325 int s;
326 struct apmhook *p, *prev;
327
328 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
329
330 s = splhigh();
331 if (ah == NULL)
332 panic("illegal apm_hook!");
333 prev = NULL;
334 for (p = *list; p != NULL; prev = p, p = p->ah_next)
335 if (p->ah_order > ah->ah_order)
336 break;
337
338 if (prev == NULL) {
339 ah->ah_next = *list;
340 *list = ah;
341 } else {
342 ah->ah_next = prev->ah_next;
343 prev->ah_next = ah;
344 }
345 splx(s);
346 return ah;
347}
348
349static void
350apm_del_hook(struct apmhook **list, struct apmhook *ah)
351{
352 int s;
353 struct apmhook *p, *prev;
354
355 s = splhigh();
356 prev = NULL;
357 for (p = *list; p != NULL; prev = p, p = p->ah_next)
358 if (p == ah)
359 goto deleteit;
360 panic("Tried to delete unregistered apm_hook.");
361 goto nosuchnode;
362deleteit:
363 if (prev != NULL)
364 prev->ah_next = p->ah_next;
365 else
366 *list = p->ah_next;
367nosuchnode:
368 splx(s);
369}
370
371
372/* APM driver calls some functions automatically */
373static void
374apm_execute_hook(struct apmhook *list)
375{
376 struct apmhook *p;
377
378 for (p = list; p != NULL; p = p->ah_next) {
379 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
380 if ((*(p->ah_fun))(p->ah_arg))
381 printf("Warning: APM hook \"%s\" failed", p->ah_name);
382 }
383}
384
385
386/* establish an apm hook */
387struct apmhook *
388apm_hook_establish(int apmh, struct apmhook *ah)
389{
390 if (apmh < 0 || apmh >= NAPM_HOOK)
391 return NULL;
392
393 return apm_add_hook(&hook[apmh], ah);
394}
395
396/* disestablish an apm hook */
397void
398apm_hook_disestablish(int apmh, struct apmhook *ah)
399{
400 if (apmh < 0 || apmh >= NAPM_HOOK)
401 return;
402
403 apm_del_hook(&hook[apmh], ah);
404}
405
406
407static struct timeval suspend_time;
408static struct timeval diff_time;
409
410static int
411apm_default_resume(void *arg)
412{
413 int pl;
414 u_int second, minute, hour;
415 struct timeval resume_time, tmp_time;
416
417 /* modified for adjkerntz */
418 pl = splsoftclock();
419 timer_restore(); /* restore the all timers */
420 inittodr(0); /* adjust time to RTC */
421 microtime(&resume_time);
422 getmicrotime(&tmp_time);
423 timevaladd(&tmp_time, &diff_time);
424
425#ifdef FIXME
426 /* XXX THIS DOESN'T WORK!!! */
427 time = tmp_time;
428#endif
429
430#ifdef APM_FIXUP_CALLTODO
431 /* Calculate the delta time suspended */
432 timevalsub(&resume_time, &suspend_time);
433 /* Fixup the calltodo list with the delta time. */
434 adjust_timeout_calltodo(&resume_time);
435#endif /* APM_FIXUP_CALLTODOK */
436 splx(pl);
437#ifndef APM_FIXUP_CALLTODO
438 second = resume_time.tv_sec - suspend_time.tv_sec;
439#else /* APM_FIXUP_CALLTODO */
440 /*
441 * We've already calculated resume_time to be the delta between
442 * the suspend and the resume.
443 */
444 second = resume_time.tv_sec;
445#endif /* APM_FIXUP_CALLTODO */
446 hour = second / 3600;
447 second %= 3600;
448 minute = second / 60;
449 second %= 60;
450 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
451 hour, minute, second);
452 return 0;
453}
454
455static int
456apm_default_suspend(void *arg)
457{
458 int pl;
459
460 pl = splsoftclock();
461 microtime(&diff_time);
462 inittodr(0);
463 microtime(&suspend_time);
464 timevalsub(&diff_time, &suspend_time);
465 splx(pl);
466 return 0;
467}
468
469static int apm_record_event __P((struct apm_softc *, u_int));
470static void apm_processevent(void);
471
472static u_int apm_op_inprog = 0;
473
474static void
475apm_do_suspend(void)
476{
477 struct apm_softc *sc = &apm_softc;
478 int error;
479
480 if (!sc)
481 return;
482
483 apm_op_inprog = 0;
484 sc->suspends = sc->suspend_countdown = 0;
485
486 if (sc->initialized) {
487 error = DEVICE_SUSPEND(root_bus);
488 if (error) {
489 DEVICE_RESUME(root_bus);
490 } else {
491 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
492 if (apm_suspend_system(PMST_SUSPEND) == 0) {
493 apm_processevent();
494 } else {
495 /* Failure, 'resume' the system again */
496 apm_execute_hook(hook[APM_HOOK_RESUME]);
497 DEVICE_RESUME(root_bus);
498 }
499 }
500 }
501}
502
503static void
504apm_do_standby(void)
505{
506 struct apm_softc *sc = &apm_softc;
507
508 if (!sc)
509 return;
510
511 apm_op_inprog = 0;
512 sc->standbys = sc->standby_countdown = 0;
513
514 if (sc->initialized) {
515 /*
516 * As far as standby, we don't need to execute
517 * all of suspend hooks.
518 */
519 apm_default_suspend(&apm_softc);
520 if (apm_suspend_system(PMST_STANDBY) == 0)
521 apm_processevent();
522 }
523}
524
525static void
526apm_lastreq_notify(void)
527{
528 struct apm_softc *sc = &apm_softc;
529
530 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
531 sc->bios.r.ebx = PMDV_ALLDEV;
532 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
533 sc->bios.r.edx = 0;
534 apm_bioscall();
535}
536
537static int
538apm_lastreq_rejected(void)
539{
540 struct apm_softc *sc = &apm_softc;
541
542 if (apm_op_inprog == 0) {
543 return 1; /* no operation in progress */
544 }
545
546 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
547 sc->bios.r.ebx = PMDV_ALLDEV;
548 sc->bios.r.ecx = PMST_LASTREQREJECT;
549 sc->bios.r.edx = 0;
550
551 if (apm_bioscall()) {
552 APM_DPRINT("apm_lastreq_rejected: failed\n");
553 return 1;
554 }
555 apm_op_inprog = 0;
556 return 0;
557}
558
559/*
560 * Public interface to the suspend/resume:
561 *
562 * Execute suspend and resume hook before and after sleep, respectively.
563 *
564 */
565
566void
567apm_suspend(int state)
568{
569 struct apm_softc *sc = &apm_softc;
570
571 if (!sc->initialized)
572 return;
573
574 switch (state) {
575 case PMST_SUSPEND:
576 if (sc->suspends)
577 return;
578 sc->suspends++;
579 sc->suspend_countdown = apm_suspend_delay;
580 break;
581 case PMST_STANDBY:
582 if (sc->standbys)
583 return;
584 sc->standbys++;
585 sc->standby_countdown = apm_standby_delay;
586 break;
587 default:
588 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
589 return;
590 }
591
592 apm_op_inprog++;
593 apm_lastreq_notify();
594}
595
596void
597apm_resume(void)
598{
599 struct apm_softc *sc = &apm_softc;
600
601 if (!sc)
602 return;
603
604 if (sc->initialized) {
605 apm_execute_hook(hook[APM_HOOK_RESUME]);
606 DEVICE_RESUME(root_bus);
607 }
608}
609
610
611/* get power status per battery */
612static int
613apm_get_pwstatus(apm_pwstatus_t app)
614{
615 struct apm_softc *sc = &apm_softc;
616
617 if (app->ap_device != PMDV_ALLDEV &&
618 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
619 return 1;
620
621 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
622 sc->bios.r.ebx = app->ap_device;
623 sc->bios.r.ecx = 0;
624 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
625
626 if (apm_bioscall())
627 return 1;
628
629 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
630 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
631 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
632 app->ap_batt_life = sc->bios.r.ecx & 0xff;
633 sc->bios.r.edx &= 0xffff;
634 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
635 app->ap_batt_time = -1;
636 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
637 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
638 else /* Time is in seconds */
639 app->ap_batt_time = sc->bios.r.edx;
640
641 return 0;
642}
643
644
645/* get APM information */
646static int
647apm_get_info(apm_info_t aip)
648{
649 struct apm_softc *sc = &apm_softc;
650 struct apm_pwstatus aps;
651
652 bzero(&aps, sizeof(aps));
653 aps.ap_device = PMDV_ALLDEV;
654 if (apm_get_pwstatus(&aps))
655 return 1;
656
657 aip->ai_infoversion = 1;
658 aip->ai_acline = aps.ap_acline;
659 aip->ai_batt_stat = aps.ap_batt_stat;
660 aip->ai_batt_life = aps.ap_batt_life;
661 aip->ai_batt_time = aps.ap_batt_time;
662 aip->ai_major = (u_int)sc->majorversion;
663 aip->ai_minor = (u_int)sc->minorversion;
664 aip->ai_status = (u_int)sc->active;
665
666 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
667 sc->bios.r.ebx = 0;
668 sc->bios.r.ecx = 0;
669 sc->bios.r.edx = 0;
670 if (apm_bioscall()) {
671 aip->ai_batteries = -1; /* Unknown */
672 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
673 } else {
674 aip->ai_batteries = sc->bios.r.ebx & 0xff;
675 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
676 }
677
678 bzero(aip->ai_spare, sizeof aip->ai_spare);
679
680 return 0;
681}
682
683
684/* inform APM BIOS that CPU is idle */
685void
686apm_cpu_idle(void)
687{
688 struct apm_softc *sc = &apm_softc;
689
690 if (sc->active) {
691
692 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
693 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
694 (void) apm_bioscall();
695 }
696 /*
697 * Some APM implementation halts CPU in BIOS, whenever
698 * "CPU-idle" function are invoked, but swtch() of
699 * FreeBSD halts CPU, therefore, CPU is halted twice
700 * in the sched loop. It makes the interrupt latency
701 * terribly long and be able to cause a serious problem
702 * in interrupt processing. We prevent it by removing
703 * "hlt" operation from swtch() and managed it under
704 * APM driver.
705 */
706 if (!sc->active || sc->always_halt_cpu)
707 __asm("hlt"); /* wait for interrupt */
708}
709
710/* inform APM BIOS that CPU is busy */
711void
712apm_cpu_busy(void)
713{
714 struct apm_softc *sc = &apm_softc;
715
716 /*
717 * The APM specification says this is only necessary if your BIOS
718 * slows down the processor in the idle task, otherwise it's not
719 * necessary.
720 */
721 if (sc->slow_idle_cpu && sc->active) {
722
723 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
724 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
725 apm_bioscall();
726 }
727}
728
729
730/*
731 * APM timeout routine:
732 *
733 * This routine is automatically called by timer once per second.
734 */
735
736static void
737apm_timeout(void *dummy)
738{
739 struct apm_softc *sc = &apm_softc;
740
741 if (apm_op_inprog)
742 apm_lastreq_notify();
743
744 if (sc->standbys && sc->standby_countdown-- <= 0)
745 apm_do_standby();
746
747 if (sc->suspends && sc->suspend_countdown-- <= 0)
748 apm_do_suspend();
749
750 if (!sc->bios_busy)
751 apm_processevent();
752
753 if (sc->active == 1)
754 /* Run slightly more oftan than 1 Hz */
755 apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1 );
756}
757
758/* enable APM BIOS */
759static void
760apm_event_enable(void)
761{
762 struct apm_softc *sc = &apm_softc;
763
764 APM_DPRINT("called apm_event_enable()\n");
765 if (sc->initialized) {
766 sc->active = 1;
767 apm_timeout(sc);
768 }
769}
770
771/* disable APM BIOS */
772static void
773apm_event_disable(void)
774{
775 struct apm_softc *sc = &apm_softc;
776
777 APM_DPRINT("called apm_event_disable()\n");
778 if (sc->initialized) {
779 untimeout(apm_timeout, NULL, apm_timeout_ch);
780 sc->active = 0;
781 }
782}
783
784/* halt CPU in scheduling loop */
785static void
786apm_halt_cpu(void)
787{
788 struct apm_softc *sc = &apm_softc;
789
790 if (sc->initialized)
791 sc->always_halt_cpu = 1;
792}
793
794/* don't halt CPU in scheduling loop */
795static void
796apm_not_halt_cpu(void)
797{
798 struct apm_softc *sc = &apm_softc;
799
800 if (sc->initialized)
801 sc->always_halt_cpu = 0;
802}
803
804/* device driver definitions */
805
806/*
807 * Create "connection point"
808 */
809static void
810apm_identify(driver_t *driver, device_t parent)
811{
812 device_t child;
813
814 child = BUS_ADD_CHILD(parent, 0, "apm", 0);
815 if (child == NULL)
816 panic("apm_identify");
817}
818
819/*
820 * probe for APM BIOS
821 */
822static int
823apm_probe(device_t dev)
824{
825#define APM_KERNBASE KERNBASE
826 struct vm86frame vmf;
827 struct apm_softc *sc = &apm_softc;
828 int disabled, flags;
829
830 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
831 && disabled != 0)
832 return ENXIO;
833
834 device_set_desc(dev, "APM BIOS");
835
836 if ( device_get_unit(dev) > 0 ) {
837 printf("apm: Only one APM driver supported.\n");
838 return ENXIO;
839 }
840
841 if (resource_int_value("apm", 0, "flags", &flags) != 0)
842 flags = 0;
843
844 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
845 bzero(&apm_softc, sizeof(apm_softc));
846 vmf.vmf_ah = APM_BIOS;
847 vmf.vmf_al = APM_INSTCHECK;
848 vmf.vmf_bx = 0;
849 if (vm86_intcall(APM_INT, &vmf))
850 return ENXIO; /* APM not found */
851 if (vmf.vmf_bx != 0x504d) {
852 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
853 return ENXIO;
854 }
855 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
856 printf("apm: protected mode connections are not supported\n");
857 return ENXIO;
858 }
859
860 apm_version = vmf.vmf_ax;
861 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
862 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
863 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
864
865 vmf.vmf_ah = APM_BIOS;
866 vmf.vmf_al = APM_DISCONNECT;
867 vmf.vmf_bx = 0;
868 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
869
870 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
871 vmf.vmf_ah = APM_BIOS;
872 vmf.vmf_al = APM_PROT32CONNECT;
873 vmf.vmf_bx = 0;
874 if (vm86_intcall(APM_INT, &vmf)) {
875 printf("apm: 32-bit connection error.\n");
876 return (ENXIO);
877 }
878 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
879 sc->bios.seg.code32.limit = 0xffff;
880 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
881 sc->bios.seg.code16.limit = 0xffff;
882 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
883 sc->bios.seg.data.limit = 0xffff;
884 sc->bios.entry = vmf.vmf_ebx;
885 sc->connectmode = APM_PROT32CONNECT;
886 } else {
887 /* use 16-bit connection */
888 vmf.vmf_ah = APM_BIOS;
889 vmf.vmf_al = APM_PROT16CONNECT;
890 vmf.vmf_bx = 0;
891 if (vm86_intcall(APM_INT, &vmf)) {
892 printf("apm: 16-bit connection error.\n");
893 return (ENXIO);
894 }
895 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
896 sc->bios.seg.code16.limit = 0xffff;
897 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
898 sc->bios.seg.data.limit = 0xffff;
899 sc->bios.entry = vmf.vmf_bx;
900 sc->connectmode = APM_PROT16CONNECT;
901 }
902 return(0);
903}
904
905
906/*
907 * return 0 if the user will notice and handle the event,
908 * return 1 if the kernel driver should do so.
909 */
910static int
911apm_record_event(struct apm_softc *sc, u_int event_type)
912{
913 struct apm_event_info *evp;
914
915 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
916 return 1; /* no user waiting */
917 if (sc->event_count == APM_NEVENTS)
918 return 1; /* overflow */
919 if (sc->event_filter[event_type] == 0)
920 return 1; /* not registered */
921 evp = &sc->event_list[sc->event_ptr];
922 sc->event_count++;
923 sc->event_ptr++;
924 sc->event_ptr %= APM_NEVENTS;
925 evp->type = event_type;
926 evp->index = ++apm_evindex;
927 selwakeup(&sc->sc_rsel);
928 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
929}
930
931/* Process APM event */
932static void
933apm_processevent(void)
934{
935 int apm_event;
936 struct apm_softc *sc = &apm_softc;
937
938#define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
939 APM_DPRINT("Received APM Event: " #symbol "\n");
940
941 do {
942 apm_event = apm_getevent();
943 switch (apm_event) {
944 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
945 if (apm_op_inprog == 0) {
946 apm_op_inprog++;
947 if (apm_record_event(sc, apm_event)) {
948 apm_suspend(PMST_STANDBY);
949 }
950 }
951 break;
952 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
953 if (apm_op_inprog == 0) {
954 apm_op_inprog++;
955 if (apm_record_event(sc, apm_event)) {
956 apm_suspend(PMST_STANDBY);
957 }
958 }
959 break;
960 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
961 apm_lastreq_notify();
962 if (apm_op_inprog == 0) {
963 apm_op_inprog++;
964 if (apm_record_event(sc, apm_event)) {
965 apm_do_suspend();
966 }
967 }
968 return; /* XXX skip the rest */
969 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
970 apm_lastreq_notify();
971 if (apm_op_inprog == 0) {
972 apm_op_inprog++;
973 if (apm_record_event(sc, apm_event)) {
974 apm_do_suspend();
975 }
976 }
977 return; /* XXX skip the rest */
978 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
979 apm_do_suspend();
980 break;
981 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
982 apm_record_event(sc, apm_event);
983 apm_resume();
984 break;
985 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
986 apm_record_event(sc, apm_event);
987 apm_resume();
988 break;
989 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
990 apm_record_event(sc, apm_event);
991 apm_resume();
992 break;
993 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
994 if (apm_record_event(sc, apm_event)) {
995 apm_battery_low();
996 apm_suspend(PMST_SUSPEND);
997 }
998 break;
999 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
1000 apm_record_event(sc, apm_event);
1001 break;
1002 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
1003 apm_record_event(sc, apm_event);
1004 inittodr(0); /* adjust time to RTC */
1005 break;
1006 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
1007 apm_record_event(sc, apm_event);
1008 break;
1009 case PMEV_NOEVENT:
1010 break;
1011 default:
1012 printf("Unknown Original APM Event 0x%x\n", apm_event);
1013 break;
1014 }
1015 } while (apm_event != PMEV_NOEVENT);
1016}
1017
1018/*
1019 * Attach APM:
1020 *
1021 * Initialize APM driver
1022 */
1023
1024static int
1025apm_attach(device_t dev)
1026{
1027 struct apm_softc *sc = &apm_softc;
1028 int flags;
1029 int drv_version;
1030
1031 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1032 flags = 0;
1033
1034 if (flags & 0x20)
1035 statclock_disable = 1;
1036
1037 sc->initialized = 0;
1038
1039 /* Must be externally enabled */
1040 sc->active = 0;
1041
1042 /* Always call HLT in idle loop */
1043 sc->always_halt_cpu = 1;
1044
1045 getenv_int("debug.apm_debug", &apm_debug);
1046
1047 /* print bootstrap messages */
1048 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1049 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1050 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1051 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1052 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1053 is_enabled(!sc->disabled));
1054 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1055 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1056
1057 /*
1058 * In one test, apm bios version was 1.02; an attempt to register
1059 * a 1.04 driver resulted in a 1.00 connection! Registering a
1060 * 1.02 driver resulted in a 1.02 connection.
1061 */
1062 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1063 for (; drv_version > 0x100; drv_version--)
1064 if (apm_driver_version(drv_version) == 0)
1065 break;
1066 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1067 ((drv_version & 0x000f) >> 0);
1068 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1069 ((apm_version & 0x0f00) >> 8);
1070
1071 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1072
1073 if (sc->intversion >= INTVERSION(1, 1))
1074 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1075 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1076 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1077 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1078 sc->majorversion, sc->minorversion);
1079
1080
1081 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1082 /* enable power management */
1083 if (sc->disabled) {
1084 if (apm_enable_disable_pm(1)) {
1085 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1086 (sc->bios.r.eax >> 8) & 0xff);
1087 }
1088 }
1089
1090 /* engage power managment (APM 1.1 or later) */
1091 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1092 if (apm_engage_disengage_pm(1)) {
1093 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1094 (sc->bios.r.eax >> 8) & 0xff);
1095 APM_DPRINT(" (Docked or using external power?).\n");
1096 }
1097 }
1098
1099 /* default suspend hook */
1100 sc->sc_suspend.ah_fun = apm_default_suspend;
1101 sc->sc_suspend.ah_arg = sc;
1102 sc->sc_suspend.ah_name = "default suspend";
1103 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1104
1105 /* default resume hook */
1106 sc->sc_resume.ah_fun = apm_default_resume;
1107 sc->sc_resume.ah_arg = sc;
1108 sc->sc_resume.ah_name = "default resume";
1109 sc->sc_resume.ah_order = APM_MIN_ORDER;
1110
1111 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1112 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1113
1114 /* Power the system off using APM */
1115 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1116 SHUTDOWN_PRI_LAST);
1117
1118 sc->initialized = 1;
1119
1120 make_dev(&apm_cdevsw, 0, 0, 5, 0660, "apm");
1121 make_dev(&apm_cdevsw, 8, 0, 5, 0660, "apmctl");
1122 return 0;
1123}
1124
1125static int
41c20dac 1126apmopen(dev_t dev, int flag, int fmt, d_thread_t *td)
984263bc
MD
1127{
1128 struct apm_softc *sc = &apm_softc;
1129 int ctl = APMDEV(dev);
1130
1131 if (!sc->initialized)
1132 return (ENXIO);
1133
1134 switch (ctl) {
1135 case APMDEV_CTL:
1136 if (!(flag & FWRITE))
1137 return EINVAL;
1138 if (sc->sc_flags & SCFLAG_OCTL)
1139 return EBUSY;
1140 sc->sc_flags |= SCFLAG_OCTL;
1141 bzero(sc->event_filter, sizeof sc->event_filter);
1142 break;
1143 case APMDEV_NORMAL:
1144 sc->sc_flags |= SCFLAG_ONORMAL;
1145 break;
1146 default:
1147 return ENXIO;
1148 break;
1149 }
1150 return 0;
1151}
1152
1153static int
41c20dac 1154apmclose(dev_t dev, int flag, int fmt, d_thread_t *td)
984263bc
MD
1155{
1156 struct apm_softc *sc = &apm_softc;
1157 int ctl = APMDEV(dev);
1158
1159 switch (ctl) {
1160 case APMDEV_CTL:
1161 apm_lastreq_rejected();
1162 sc->sc_flags &= ~SCFLAG_OCTL;
1163 bzero(sc->event_filter, sizeof sc->event_filter);
1164 break;
1165 case APMDEV_NORMAL:
1166 sc->sc_flags &= ~SCFLAG_ONORMAL;
1167 break;
1168 }
1169 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1170 sc->event_count = 0;
1171 sc->event_ptr = 0;
1172 }
1173 return 0;
1174}
1175
1176static int
41c20dac 1177apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
984263bc
MD
1178{
1179 struct apm_softc *sc = &apm_softc;
1180 struct apm_bios_arg *args;
1181 int error = 0;
1182 int ret;
1183 int newstate;
1184
1185 if (!sc->initialized)
1186 return (ENXIO);
1187 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1188 switch (cmd) {
1189 case APMIO_SUSPEND:
1190 if (!(flag & FWRITE))
1191 return (EPERM);
1192 if (sc->active)
1193 apm_suspend(PMST_SUSPEND);
1194 else
1195 error = EINVAL;
1196 break;
1197
1198 case APMIO_STANDBY:
1199 if (!(flag & FWRITE))
1200 return (EPERM);
1201 if (sc->active)
1202 apm_suspend(PMST_STANDBY);
1203 else
1204 error = EINVAL;
1205 break;
1206
1207 case APMIO_GETINFO_OLD:
1208 {
1209 struct apm_info info;
1210 apm_info_old_t aiop;
1211
1212 if (apm_get_info(&info))
1213 error = ENXIO;
1214 aiop = (apm_info_old_t)addr;
1215 aiop->ai_major = info.ai_major;
1216 aiop->ai_minor = info.ai_minor;
1217 aiop->ai_acline = info.ai_acline;
1218 aiop->ai_batt_stat = info.ai_batt_stat;
1219 aiop->ai_batt_life = info.ai_batt_life;
1220 aiop->ai_status = info.ai_status;
1221 }
1222 break;
1223 case APMIO_GETINFO:
1224 if (apm_get_info((apm_info_t)addr))
1225 error = ENXIO;
1226 break;
1227 case APMIO_GETPWSTATUS:
1228 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1229 error = ENXIO;
1230 break;
1231 case APMIO_ENABLE:
1232 if (!(flag & FWRITE))
1233 return (EPERM);
1234 apm_event_enable();
1235 break;
1236 case APMIO_DISABLE:
1237 if (!(flag & FWRITE))
1238 return (EPERM);
1239 apm_event_disable();
1240 break;
1241 case APMIO_HALTCPU:
1242 if (!(flag & FWRITE))
1243 return (EPERM);
1244 apm_halt_cpu();
1245 break;
1246 case APMIO_NOTHALTCPU:
1247 if (!(flag & FWRITE))
1248 return (EPERM);
1249 apm_not_halt_cpu();
1250 break;
1251 case APMIO_DISPLAY:
1252 if (!(flag & FWRITE))
1253 return (EPERM);
1254 newstate = *(int *)addr;
1255 if (apm_display(newstate))
1256 error = ENXIO;
1257 break;
1258 case APMIO_BIOS:
1259 if (!(flag & FWRITE))
1260 return (EPERM);
1261 /* XXX compatibility with the old interface */
1262 args = (struct apm_bios_arg *)addr;
1263 sc->bios.r.eax = args->eax;
1264 sc->bios.r.ebx = args->ebx;
1265 sc->bios.r.ecx = args->ecx;
1266 sc->bios.r.edx = args->edx;
1267 sc->bios.r.esi = args->esi;
1268 sc->bios.r.edi = args->edi;
1269 if ((ret = apm_bioscall())) {
1270 /*
1271 * Return code 1 means bios call was unsuccessful.
1272 * Error code is stored in %ah.
1273 * Return code -1 means bios call was unsupported
1274 * in the APM BIOS version.
1275 */
1276 if (ret == -1) {
1277 error = EINVAL;
1278 }
1279 } else {
1280 /*
1281 * Return code 0 means bios call was successful.
1282 * We need only %al and can discard %ah.
1283 */
1284 sc->bios.r.eax &= 0xff;
1285 }
1286 args->eax = sc->bios.r.eax;
1287 args->ebx = sc->bios.r.ebx;
1288 args->ecx = sc->bios.r.ecx;
1289 args->edx = sc->bios.r.edx;
1290 args->esi = sc->bios.r.esi;
1291 args->edi = sc->bios.r.edi;
1292 break;
1293 default:
1294 error = EINVAL;
1295 break;
1296 }
1297
1298 /* for /dev/apmctl */
1299 if (APMDEV(dev) == APMDEV_CTL) {
1300 struct apm_event_info *evp;
1301 int i;
1302
1303 error = 0;
1304 switch (cmd) {
1305 case APMIO_NEXTEVENT:
1306 if (!sc->event_count) {
1307 error = EAGAIN;
1308 } else {
1309 evp = (struct apm_event_info *)addr;
1310 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1311 i %= APM_NEVENTS;
1312 *evp = sc->event_list[i];
1313 sc->event_count--;
1314 }
1315 break;
1316 case APMIO_REJECTLASTREQ:
1317 if (apm_lastreq_rejected()) {
1318 error = EINVAL;
1319 }
1320 break;
1321 default:
1322 error = EINVAL;
1323 break;
1324 }
1325 }
1326
1327 return error;
1328}
1329
1330static int
1331apmwrite(dev_t dev, struct uio *uio, int ioflag)
1332{
1333 struct apm_softc *sc = &apm_softc;
1334 u_int event_type;
1335 int error;
1336 u_char enabled;
1337
1338 if (APMDEV(dev) != APMDEV_CTL)
1339 return(ENODEV);
1340 if (uio->uio_resid != sizeof(u_int))
1341 return(E2BIG);
1342
1343 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1344 return(error);
1345
1346 if (event_type < 0 || event_type >= APM_NPMEV)
1347 return(EINVAL);
1348
1349 if (sc->event_filter[event_type] == 0) {
1350 enabled = 1;
1351 } else {
1352 enabled = 0;
1353 }
1354 sc->event_filter[event_type] = enabled;
1355 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1356
1357 return uio->uio_resid;
1358}
1359
1360static int
41c20dac 1361apmpoll(dev_t dev, int events, d_thread_t *td)
984263bc
MD
1362{
1363 struct apm_softc *sc = &apm_softc;
1364 int revents = 0;
1365
1366 if (events & (POLLIN | POLLRDNORM)) {
1367 if (sc->event_count) {
1368 revents |= events & (POLLIN | POLLRDNORM);
1369 } else {
41c20dac 1370 selrecord(td, &sc->sc_rsel);
984263bc
MD
1371 }
1372 }
1373
1374 return (revents);
1375}
1376
1377static device_method_t apm_methods[] = {
1378 /* Device interface */
1379 DEVMETHOD(device_identify, apm_identify),
1380 DEVMETHOD(device_probe, apm_probe),
1381 DEVMETHOD(device_attach, apm_attach),
1382
1383 { 0, 0 }
1384};
1385
1386static driver_t apm_driver = {
1387 "apm",
1388 apm_methods,
1389 1, /* no softc (XXX) */
1390};
1391
1392static devclass_t apm_devclass;
1393
1394DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);