HAMMER Utilities: Sync with 59A
[dragonfly.git] / sys / platform / pc32 / apm / apm.c
blobd2ee2f0d49ca6ad59745ab34b468e1876748fced
1 /*
2 * APM (Advanced Power Management) BIOS Device Driver
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>
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.
16 * Sep, 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD)
18 * $FreeBSD: src/sys/i386/apm/apm.c,v 1.114.2.5 2002/11/02 04:41:50 iwasaki Exp $
19 * $DragonFly: src/sys/platform/pc32/apm/apm.c,v 1.21 2006/12/23 00:27:03 swildner Exp $
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/eventhandler.h>
25 #include <sys/conf.h>
26 #include <sys/device.h>
27 #include <sys/kernel.h>
28 #include <sys/time.h>
29 #include <sys/reboot.h>
30 #include <sys/bus.h>
31 #include <sys/selinfo.h>
32 #include <sys/poll.h>
33 #include <sys/fcntl.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 #include <sys/thread2.h>
46 #include <machine/pc/bios.h>
47 #include <machine/vm86.h>
49 #include <machine_base/apm/apm.h>
51 /* Used by the apm_saver screen saver module */
52 int apm_display (int newstate);
53 struct apm_softc apm_softc;
55 static void apm_resume (void);
56 static int apm_bioscall(void);
57 static int apm_check_function_supported (u_int version, u_int func);
59 static u_long apm_version;
61 int apm_evindex;
63 #define SCFLAG_ONORMAL 0x0000001
64 #define SCFLAG_OCTL 0x0000002
65 #define SCFLAG_OPEN (SCFLAG_ONORMAL|SCFLAG_OCTL)
67 #define APMDEV(dev) (minor(dev)&0x0f)
68 #define APMDEV_NORMAL 0
69 #define APMDEV_CTL 8
71 static struct apmhook *hook[NAPM_HOOK]; /* XXX */
73 #define is_enabled(foo) ((foo) ? "enabled" : "disabled")
75 /* Map version number to integer (keeps ordering of version numbers) */
76 #define INTVERSION(major, minor) ((major)*100 + (minor))
78 static struct callout apm_timeout_ch;
80 static timeout_t apm_timeout;
81 static d_open_t apmopen;
82 static d_close_t apmclose;
83 static d_write_t apmwrite;
84 static d_ioctl_t apmioctl;
85 static d_poll_t apmpoll;
87 #define CDEV_MAJOR 39
88 static struct dev_ops apm_ops = {
89 { "apm", CDEV_MAJOR, 0 },
90 .d_open = apmopen,
91 .d_close = apmclose,
92 .d_write = apmwrite,
93 .d_ioctl = apmioctl,
94 .d_poll = apmpoll,
97 static int apm_suspend_delay = 1;
98 static int apm_standby_delay = 1;
99 static int apm_debug = 0;
101 #define APM_DPRINT(args...) do { \
102 if (apm_debug) { \
103 kprintf(args); \
105 } while (0)
107 SYSCTL_INT(_machdep, OID_AUTO, apm_suspend_delay, CTLFLAG_RW, &apm_suspend_delay, 1, "");
108 SYSCTL_INT(_machdep, OID_AUTO, apm_standby_delay, CTLFLAG_RW, &apm_standby_delay, 1, "");
109 SYSCTL_INT(_debug, OID_AUTO, apm_debug, CTLFLAG_RW, &apm_debug, 0, "");
112 * return 0 if the function successfull,
113 * return 1 if the function unsuccessfull,
114 * return -1 if the function unsupported.
116 static int
117 apm_bioscall(void)
119 struct apm_softc *sc = &apm_softc;
120 int error = 0;
121 u_int apm_func = sc->bios.r.eax & 0xff;
123 if (!apm_check_function_supported(sc->intversion, apm_func)) {
124 APM_DPRINT("apm_bioscall: function 0x%x is not supported in v%d.%d\n",
125 apm_func, sc->majorversion, sc->minorversion);
126 return (-1);
129 sc->bios_busy = 1;
130 if (sc->connectmode == APM_PROT32CONNECT) {
131 set_bios_selectors(&sc->bios.seg,
132 BIOSCODE_FLAG | BIOSDATA_FLAG);
133 error = bios32(&sc->bios.r,
134 sc->bios.entry, GSEL(GBIOSCODE32_SEL, SEL_KPL));
135 } else {
136 error = bios16(&sc->bios, NULL);
138 sc->bios_busy = 0;
139 return (error);
142 /* check whether APM function is supported (1) or not (0). */
143 static int
144 apm_check_function_supported(u_int version, u_int func)
146 /* except driver version */
147 if (func == APM_DRVVERSION) {
148 return (1);
151 switch (version) {
152 case INTVERSION(1, 0):
153 if (func > APM_GETPMEVENT) {
154 return (0); /* not supported */
156 break;
157 case INTVERSION(1, 1):
158 if (func > APM_ENGAGEDISENGAGEPM &&
159 func < APM_OEMFUNC) {
160 return (0); /* not supported */
162 break;
163 case INTVERSION(1, 2):
164 break;
167 return (1); /* supported */
170 /* enable/disable power management */
171 static int
172 apm_enable_disable_pm(int enable)
174 struct apm_softc *sc = &apm_softc;
176 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM;
178 if (sc->intversion >= INTVERSION(1, 1))
179 sc->bios.r.ebx = PMDV_ALLDEV;
180 else
181 sc->bios.r.ebx = 0xffff; /* APM version 1.0 only */
182 sc->bios.r.ecx = enable;
183 sc->bios.r.edx = 0;
184 return (apm_bioscall());
187 /* register driver version (APM 1.1 or later) */
188 static int
189 apm_driver_version(int version)
191 struct apm_softc *sc = &apm_softc;
193 sc->bios.r.eax = (APM_BIOS << 8) | APM_DRVVERSION;
194 sc->bios.r.ebx = 0x0;
195 sc->bios.r.ecx = version;
196 sc->bios.r.edx = 0;
198 if (apm_bioscall() == 0 && sc->bios.r.eax == version)
199 return (0);
201 /* Some old BIOSes don't return the connection version in %ax. */
202 if (sc->bios.r.eax == ((APM_BIOS << 8) | APM_DRVVERSION))
203 return (0);
205 return (1);
208 /* engage/disengage power management (APM 1.1 or later) */
209 static int
210 apm_engage_disengage_pm(int engage)
212 struct apm_softc *sc = &apm_softc;
214 sc->bios.r.eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM;
215 sc->bios.r.ebx = PMDV_ALLDEV;
216 sc->bios.r.ecx = engage;
217 sc->bios.r.edx = 0;
218 return (apm_bioscall());
221 /* get PM event */
222 static u_int
223 apm_getevent(void)
225 struct apm_softc *sc = &apm_softc;
227 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPMEVENT;
229 sc->bios.r.ebx = 0;
230 sc->bios.r.ecx = 0;
231 sc->bios.r.edx = 0;
232 if (apm_bioscall())
233 return (PMEV_NOEVENT);
234 return (sc->bios.r.ebx & 0xffff);
237 /* suspend entire system */
238 static int
239 apm_suspend_system(int state)
241 struct apm_softc *sc = &apm_softc;
243 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
244 sc->bios.r.ebx = PMDV_ALLDEV;
245 sc->bios.r.ecx = state;
246 sc->bios.r.edx = 0;
248 if (apm_bioscall()) {
249 kprintf("Entire system suspend failure: errcode = %d\n",
250 0xff & (sc->bios.r.eax >> 8));
251 return 1;
253 return 0;
256 /* Display control */
258 * Experimental implementation: My laptop machine can't handle this function
259 * If your laptop can control the display via APM, please inform me.
260 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org>
263 apm_display(int newstate)
265 struct apm_softc *sc = &apm_softc;
267 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
268 sc->bios.r.ebx = PMDV_DISP0;
269 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
270 sc->bios.r.edx = 0;
271 if (apm_bioscall() == 0) {
272 return 0;
275 /* If failed, then try to blank all display devices instead. */
276 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
277 sc->bios.r.ebx = PMDV_DISPALL; /* all display devices */
278 sc->bios.r.ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND;
279 sc->bios.r.edx = 0;
280 if (apm_bioscall() == 0) {
281 return 0;
283 kprintf("Display off failure: errcode = %d\n",
284 0xff & (sc->bios.r.eax >> 8));
285 return 1;
289 * Turn off the entire system.
291 static void
292 apm_power_off(void *junk, int howto)
294 struct apm_softc *sc = &apm_softc;
296 /* Not halting powering off, or not active */
297 if (!(howto & RB_POWEROFF) || !apm_softc.active)
298 return;
299 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
300 sc->bios.r.ebx = PMDV_ALLDEV;
301 sc->bios.r.ecx = PMST_OFF;
302 sc->bios.r.edx = 0;
303 apm_bioscall();
306 /* APM Battery low handler */
307 static void
308 apm_battery_low(void)
310 kprintf("\007\007 * * * BATTERY IS LOW * * * \007\007");
313 /* APM hook manager */
314 static struct apmhook *
315 apm_add_hook(struct apmhook **list, struct apmhook *ah)
317 struct apmhook *p, *prev;
319 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
321 crit_enter();
322 if (ah == NULL)
323 panic("illegal apm_hook!");
324 prev = NULL;
325 for (p = *list; p != NULL; prev = p, p = p->ah_next)
326 if (p->ah_order > ah->ah_order)
327 break;
329 if (prev == NULL) {
330 ah->ah_next = *list;
331 *list = ah;
332 } else {
333 ah->ah_next = prev->ah_next;
334 prev->ah_next = ah;
336 crit_exit();
337 return ah;
340 static void
341 apm_del_hook(struct apmhook **list, struct apmhook *ah)
343 struct apmhook *p, *prev;
345 crit_enter();
346 prev = NULL;
347 for (p = *list; p != NULL; prev = p, p = p->ah_next)
348 if (p == ah)
349 goto deleteit;
350 panic("Tried to delete unregistered apm_hook.");
351 goto nosuchnode;
352 deleteit:
353 if (prev != NULL)
354 prev->ah_next = p->ah_next;
355 else
356 *list = p->ah_next;
357 nosuchnode:
358 crit_exit();
362 /* APM driver calls some functions automatically */
363 static void
364 apm_execute_hook(struct apmhook *list)
366 struct apmhook *p;
368 for (p = list; p != NULL; p = p->ah_next) {
369 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
370 if ((*(p->ah_fun))(p->ah_arg))
371 kprintf("Warning: APM hook \"%s\" failed", p->ah_name);
376 /* establish an apm hook */
377 struct apmhook *
378 apm_hook_establish(int apmh, struct apmhook *ah)
380 if (apmh < 0 || apmh >= NAPM_HOOK)
381 return NULL;
383 return apm_add_hook(&hook[apmh], ah);
386 /* disestablish an apm hook */
387 void
388 apm_hook_disestablish(int apmh, struct apmhook *ah)
390 if (apmh < 0 || apmh >= NAPM_HOOK)
391 return;
393 apm_del_hook(&hook[apmh], ah);
397 static struct timeval suspend_time;
398 static struct timeval diff_time;
400 static int
401 apm_default_resume(void *arg)
403 u_int second, minute, hour;
404 struct timeval resume_time, tmp_time;
406 /* modified for adjkerntz */
407 crit_enter();
408 timer_restore(); /* restore the all timers */
409 inittodr(0); /* adjust time to RTC */
410 microtime(&resume_time);
411 getmicrotime(&tmp_time);
412 timevaladd(&tmp_time, &diff_time);
414 #ifdef FIXME
415 /* XXX THIS DOESN'T WORK!!! */
416 time = tmp_time;
417 #endif
419 #ifdef APM_FIXUP_CALLTODO
420 /* Calculate the delta time suspended */
421 timevalsub(&resume_time, &suspend_time);
422 /* Fixup the calltodo list with the delta time. */
423 adjust_timeout_calltodo(&resume_time);
424 #endif /* APM_FIXUP_CALLTODOK */
425 crit_exit();
426 #ifndef APM_FIXUP_CALLTODO
427 second = resume_time.tv_sec - suspend_time.tv_sec;
428 #else /* APM_FIXUP_CALLTODO */
430 * We've already calculated resume_time to be the delta between
431 * the suspend and the resume.
433 second = resume_time.tv_sec;
434 #endif /* APM_FIXUP_CALLTODO */
435 hour = second / 3600;
436 second %= 3600;
437 minute = second / 60;
438 second %= 60;
439 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
440 hour, minute, second);
441 return 0;
444 static int
445 apm_default_suspend(void *arg)
447 crit_enter();
448 microtime(&diff_time);
449 inittodr(0);
450 microtime(&suspend_time);
451 timevalsub(&diff_time, &suspend_time);
452 crit_exit();
453 return 0;
456 static int apm_record_event (struct apm_softc *, u_int);
457 static void apm_processevent(void);
459 static u_int apm_op_inprog = 0;
461 static void
462 apm_do_suspend(void)
464 struct apm_softc *sc = &apm_softc;
465 int error;
467 if (!sc)
468 return;
470 apm_op_inprog = 0;
471 sc->suspends = sc->suspend_countdown = 0;
473 if (sc->initialized) {
474 error = DEVICE_SUSPEND(root_bus);
475 if (error) {
476 DEVICE_RESUME(root_bus);
477 } else {
478 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
479 if (apm_suspend_system(PMST_SUSPEND) == 0) {
480 apm_processevent();
481 } else {
482 /* Failure, 'resume' the system again */
483 apm_execute_hook(hook[APM_HOOK_RESUME]);
484 DEVICE_RESUME(root_bus);
490 static void
491 apm_do_standby(void)
493 struct apm_softc *sc = &apm_softc;
495 if (!sc)
496 return;
498 apm_op_inprog = 0;
499 sc->standbys = sc->standby_countdown = 0;
501 if (sc->initialized) {
503 * As far as standby, we don't need to execute
504 * all of suspend hooks.
506 apm_default_suspend(&apm_softc);
507 if (apm_suspend_system(PMST_STANDBY) == 0)
508 apm_processevent();
512 static void
513 apm_lastreq_notify(void)
515 struct apm_softc *sc = &apm_softc;
517 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
518 sc->bios.r.ebx = PMDV_ALLDEV;
519 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
520 sc->bios.r.edx = 0;
521 apm_bioscall();
524 static int
525 apm_lastreq_rejected(void)
527 struct apm_softc *sc = &apm_softc;
529 if (apm_op_inprog == 0) {
530 return 1; /* no operation in progress */
533 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
534 sc->bios.r.ebx = PMDV_ALLDEV;
535 sc->bios.r.ecx = PMST_LASTREQREJECT;
536 sc->bios.r.edx = 0;
538 if (apm_bioscall()) {
539 APM_DPRINT("apm_lastreq_rejected: failed\n");
540 return 1;
542 apm_op_inprog = 0;
543 return 0;
547 * Public interface to the suspend/resume:
549 * Execute suspend and resume hook before and after sleep, respectively.
553 void
554 apm_suspend(int state)
556 struct apm_softc *sc = &apm_softc;
558 if (!sc->initialized)
559 return;
561 switch (state) {
562 case PMST_SUSPEND:
563 if (sc->suspends)
564 return;
565 sc->suspends++;
566 sc->suspend_countdown = apm_suspend_delay;
567 break;
568 case PMST_STANDBY:
569 if (sc->standbys)
570 return;
571 sc->standbys++;
572 sc->standby_countdown = apm_standby_delay;
573 break;
574 default:
575 kprintf("apm_suspend: Unknown Suspend state 0x%x\n", state);
576 return;
579 apm_op_inprog++;
580 apm_lastreq_notify();
583 void
584 apm_resume(void)
586 struct apm_softc *sc = &apm_softc;
588 if (!sc)
589 return;
591 if (sc->initialized) {
592 apm_execute_hook(hook[APM_HOOK_RESUME]);
593 DEVICE_RESUME(root_bus);
598 /* get power status per battery */
599 static int
600 apm_get_pwstatus(apm_pwstatus_t app)
602 struct apm_softc *sc = &apm_softc;
604 if (app->ap_device != PMDV_ALLDEV &&
605 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
606 return 1;
608 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
609 sc->bios.r.ebx = app->ap_device;
610 sc->bios.r.ecx = 0;
611 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
613 if (apm_bioscall())
614 return 1;
616 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
617 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
618 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
619 app->ap_batt_life = sc->bios.r.ecx & 0xff;
620 sc->bios.r.edx &= 0xffff;
621 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
622 app->ap_batt_time = -1;
623 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
624 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
625 else /* Time is in seconds */
626 app->ap_batt_time = sc->bios.r.edx;
628 return 0;
632 /* get APM information */
633 static int
634 apm_get_info(apm_info_t aip)
636 struct apm_softc *sc = &apm_softc;
637 struct apm_pwstatus aps;
639 bzero(&aps, sizeof(aps));
640 aps.ap_device = PMDV_ALLDEV;
641 if (apm_get_pwstatus(&aps))
642 return 1;
644 aip->ai_infoversion = 1;
645 aip->ai_acline = aps.ap_acline;
646 aip->ai_batt_stat = aps.ap_batt_stat;
647 aip->ai_batt_life = aps.ap_batt_life;
648 aip->ai_batt_time = aps.ap_batt_time;
649 aip->ai_major = (u_int)sc->majorversion;
650 aip->ai_minor = (u_int)sc->minorversion;
651 aip->ai_status = (u_int)sc->active;
653 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
654 sc->bios.r.ebx = 0;
655 sc->bios.r.ecx = 0;
656 sc->bios.r.edx = 0;
657 if (apm_bioscall()) {
658 aip->ai_batteries = -1; /* Unknown */
659 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
660 } else {
661 aip->ai_batteries = sc->bios.r.ebx & 0xff;
662 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
665 bzero(aip->ai_spare, sizeof aip->ai_spare);
667 return 0;
671 /* inform APM BIOS that CPU is idle */
672 void
673 apm_cpu_idle(void)
675 struct apm_softc *sc = &apm_softc;
677 if (sc->active) {
679 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
680 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
681 apm_bioscall();
684 * Some APM implementation halts CPU in BIOS, whenever
685 * "CPU-idle" function are invoked, but swtch() of
686 * FreeBSD halts CPU, therefore, CPU is halted twice
687 * in the sched loop. It makes the interrupt latency
688 * terribly long and be able to cause a serious problem
689 * in interrupt processing. We prevent it by removing
690 * "hlt" operation from swtch() and managed it under
691 * APM driver.
693 if (!sc->active || sc->always_halt_cpu)
694 __asm("hlt"); /* wait for interrupt */
697 /* inform APM BIOS that CPU is busy */
698 void
699 apm_cpu_busy(void)
701 struct apm_softc *sc = &apm_softc;
704 * The APM specification says this is only necessary if your BIOS
705 * slows down the processor in the idle task, otherwise it's not
706 * necessary.
708 if (sc->slow_idle_cpu && sc->active) {
710 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
711 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
712 apm_bioscall();
718 * APM timeout routine:
720 * This routine is automatically called by timer once per second.
723 static void
724 apm_timeout(void *dummy)
726 struct apm_softc *sc = &apm_softc;
728 if (apm_op_inprog)
729 apm_lastreq_notify();
731 if (sc->standbys && sc->standby_countdown-- <= 0)
732 apm_do_standby();
734 if (sc->suspends && sc->suspend_countdown-- <= 0)
735 apm_do_suspend();
737 if (!sc->bios_busy)
738 apm_processevent();
740 if (sc->active == 1) {
741 /* Run slightly more oftan than 1 Hz */
742 callout_reset(&apm_timeout_ch, hz - 1, apm_timeout, NULL);
746 /* enable APM BIOS */
747 static void
748 apm_event_enable(void)
750 struct apm_softc *sc = &apm_softc;
752 APM_DPRINT("called apm_event_enable()\n");
753 if (sc->initialized) {
754 sc->active = 1;
755 callout_init(&apm_timeout_ch);
756 apm_timeout(sc);
760 /* disable APM BIOS */
761 static void
762 apm_event_disable(void)
764 struct apm_softc *sc = &apm_softc;
766 APM_DPRINT("called apm_event_disable()\n");
767 if (sc->initialized) {
768 callout_stop(&apm_timeout_ch);
769 sc->active = 0;
773 /* halt CPU in scheduling loop */
774 static void
775 apm_halt_cpu(void)
777 struct apm_softc *sc = &apm_softc;
779 if (sc->initialized)
780 sc->always_halt_cpu = 1;
783 /* don't halt CPU in scheduling loop */
784 static void
785 apm_not_halt_cpu(void)
787 struct apm_softc *sc = &apm_softc;
789 if (sc->initialized)
790 sc->always_halt_cpu = 0;
793 /* device driver definitions */
796 * probe for APM BIOS
798 static int
799 apm_probe(device_t dev)
801 #define APM_KERNBASE KERNBASE
802 struct vm86frame vmf;
803 struct apm_softc *sc = &apm_softc;
804 int disabled, flags;
806 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
807 && disabled != 0)
808 return ENXIO;
810 device_set_desc(dev, "APM BIOS");
812 if ( device_get_unit(dev) > 0 ) {
813 kprintf("apm: Only one APM driver supported.\n");
814 return ENXIO;
817 if (resource_int_value("apm", 0, "flags", &flags) != 0)
818 flags = 0;
820 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
821 bzero(&apm_softc, sizeof(apm_softc));
822 vmf.vmf_ah = APM_BIOS;
823 vmf.vmf_al = APM_INSTCHECK;
824 vmf.vmf_bx = 0;
825 if (vm86_intcall(APM_INT, &vmf))
826 return ENXIO; /* APM not found */
827 if (vmf.vmf_bx != 0x504d) {
828 kprintf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
829 return ENXIO;
831 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
832 kprintf("apm: protected mode connections are not supported\n");
833 return ENXIO;
836 apm_version = vmf.vmf_ax;
837 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
838 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
839 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
841 vmf.vmf_ah = APM_BIOS;
842 vmf.vmf_al = APM_DISCONNECT;
843 vmf.vmf_bx = 0;
844 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
846 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
847 vmf.vmf_ah = APM_BIOS;
848 vmf.vmf_al = APM_PROT32CONNECT;
849 vmf.vmf_bx = 0;
850 if (vm86_intcall(APM_INT, &vmf)) {
851 kprintf("apm: 32-bit connection error.\n");
852 return (ENXIO);
854 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
855 sc->bios.seg.code32.limit = 0xffff;
856 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
857 sc->bios.seg.code16.limit = 0xffff;
858 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
859 sc->bios.seg.data.limit = 0xffff;
860 sc->bios.entry = vmf.vmf_ebx;
861 sc->connectmode = APM_PROT32CONNECT;
862 } else {
863 /* use 16-bit connection */
864 vmf.vmf_ah = APM_BIOS;
865 vmf.vmf_al = APM_PROT16CONNECT;
866 vmf.vmf_bx = 0;
867 if (vm86_intcall(APM_INT, &vmf)) {
868 kprintf("apm: 16-bit connection error.\n");
869 return (ENXIO);
871 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
872 sc->bios.seg.code16.limit = 0xffff;
873 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
874 sc->bios.seg.data.limit = 0xffff;
875 sc->bios.entry = vmf.vmf_bx;
876 sc->connectmode = APM_PROT16CONNECT;
878 return(0);
883 * return 0 if the user will notice and handle the event,
884 * return 1 if the kernel driver should do so.
886 static int
887 apm_record_event(struct apm_softc *sc, u_int event_type)
889 struct apm_event_info *evp;
891 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
892 return 1; /* no user waiting */
893 if (sc->event_count == APM_NEVENTS)
894 return 1; /* overflow */
895 if (sc->event_filter[event_type] == 0)
896 return 1; /* not registered */
897 evp = &sc->event_list[sc->event_ptr];
898 sc->event_count++;
899 sc->event_ptr++;
900 sc->event_ptr %= APM_NEVENTS;
901 evp->type = event_type;
902 evp->index = ++apm_evindex;
903 selwakeup(&sc->sc_rsel);
904 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
907 /* Process APM event */
908 static void
909 apm_processevent(void)
911 int apm_event;
912 struct apm_softc *sc = &apm_softc;
914 #define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
915 APM_DPRINT("Received APM Event: " #symbol "\n");
917 do {
918 apm_event = apm_getevent();
919 switch (apm_event) {
920 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
921 if (apm_op_inprog == 0) {
922 apm_op_inprog++;
923 if (apm_record_event(sc, apm_event)) {
924 apm_suspend(PMST_STANDBY);
927 break;
928 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
929 if (apm_op_inprog == 0) {
930 apm_op_inprog++;
931 if (apm_record_event(sc, apm_event)) {
932 apm_suspend(PMST_STANDBY);
935 break;
936 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
937 apm_lastreq_notify();
938 if (apm_op_inprog == 0) {
939 apm_op_inprog++;
940 if (apm_record_event(sc, apm_event)) {
941 apm_do_suspend();
944 return; /* XXX skip the rest */
945 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
946 apm_lastreq_notify();
947 if (apm_op_inprog == 0) {
948 apm_op_inprog++;
949 if (apm_record_event(sc, apm_event)) {
950 apm_do_suspend();
953 return; /* XXX skip the rest */
954 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
955 apm_do_suspend();
956 break;
957 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
958 apm_record_event(sc, apm_event);
959 apm_resume();
960 break;
961 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
962 apm_record_event(sc, apm_event);
963 apm_resume();
964 break;
965 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
966 apm_record_event(sc, apm_event);
967 apm_resume();
968 break;
969 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
970 if (apm_record_event(sc, apm_event)) {
971 apm_battery_low();
972 apm_suspend(PMST_SUSPEND);
974 break;
975 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
976 apm_record_event(sc, apm_event);
977 break;
978 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
979 apm_record_event(sc, apm_event);
980 inittodr(0); /* adjust time to RTC */
981 break;
982 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
983 apm_record_event(sc, apm_event);
984 break;
985 case PMEV_NOEVENT:
986 break;
987 default:
988 kprintf("Unknown Original APM Event 0x%x\n", apm_event);
989 break;
991 } while (apm_event != PMEV_NOEVENT);
995 * Attach APM:
997 * Initialize APM driver
1000 static int
1001 apm_attach(device_t dev)
1003 struct apm_softc *sc = &apm_softc;
1004 int flags;
1005 int drv_version;
1007 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1008 flags = 0;
1010 sc->initialized = 0;
1012 /* Must be externally enabled */
1013 sc->active = 0;
1015 /* Always call HLT in idle loop */
1016 sc->always_halt_cpu = 1;
1018 kgetenv_int("debug.apm_debug", &apm_debug);
1020 /* print bootstrap messages */
1021 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1022 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1023 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1024 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1025 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1026 is_enabled(!sc->disabled));
1027 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1028 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1031 * In one test, apm bios version was 1.02; an attempt to register
1032 * a 1.04 driver resulted in a 1.00 connection! Registering a
1033 * 1.02 driver resulted in a 1.02 connection.
1035 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1036 for (; drv_version > 0x100; drv_version--)
1037 if (apm_driver_version(drv_version) == 0)
1038 break;
1039 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1040 ((drv_version & 0x000f) >> 0);
1041 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1042 ((apm_version & 0x0f00) >> 8);
1044 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1046 if (sc->intversion >= INTVERSION(1, 1))
1047 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1048 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1049 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1050 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1051 sc->majorversion, sc->minorversion);
1054 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1055 /* enable power management */
1056 if (sc->disabled) {
1057 if (apm_enable_disable_pm(1)) {
1058 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1059 (sc->bios.r.eax >> 8) & 0xff);
1063 /* engage power managment (APM 1.1 or later) */
1064 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1065 if (apm_engage_disengage_pm(1)) {
1066 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1067 (sc->bios.r.eax >> 8) & 0xff);
1068 APM_DPRINT(" (Docked or using external power?).\n");
1072 /* default suspend hook */
1073 sc->sc_suspend.ah_fun = apm_default_suspend;
1074 sc->sc_suspend.ah_arg = sc;
1075 sc->sc_suspend.ah_name = "default suspend";
1076 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1078 /* default resume hook */
1079 sc->sc_resume.ah_fun = apm_default_resume;
1080 sc->sc_resume.ah_arg = sc;
1081 sc->sc_resume.ah_name = "default resume";
1082 sc->sc_resume.ah_order = APM_MIN_ORDER;
1084 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1085 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1087 /* Power the system off using APM */
1088 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1089 SHUTDOWN_PRI_LAST);
1091 sc->initialized = 1;
1093 dev_ops_add(&apm_ops, 0, 0);
1094 make_dev(&apm_ops, 0, UID_ROOT, GID_OPERATOR, 0660, "apm");
1095 make_dev(&apm_ops, 8, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
1096 return 0;
1099 static int
1100 apmopen(struct dev_open_args *ap)
1102 cdev_t dev = ap->a_head.a_dev;
1103 struct apm_softc *sc = &apm_softc;
1104 int ctl = APMDEV(dev);
1106 if (!sc->initialized)
1107 return (ENXIO);
1109 switch (ctl) {
1110 case APMDEV_CTL:
1111 if (!(ap->a_oflags & FWRITE))
1112 return EINVAL;
1113 if (sc->sc_flags & SCFLAG_OCTL)
1114 return EBUSY;
1115 sc->sc_flags |= SCFLAG_OCTL;
1116 bzero(sc->event_filter, sizeof sc->event_filter);
1117 break;
1118 case APMDEV_NORMAL:
1119 sc->sc_flags |= SCFLAG_ONORMAL;
1120 break;
1121 default:
1122 return ENXIO;
1123 break;
1125 return 0;
1128 static int
1129 apmclose(struct dev_close_args *ap)
1131 cdev_t dev = ap->a_head.a_dev;
1132 struct apm_softc *sc = &apm_softc;
1133 int ctl = APMDEV(dev);
1135 switch (ctl) {
1136 case APMDEV_CTL:
1137 apm_lastreq_rejected();
1138 sc->sc_flags &= ~SCFLAG_OCTL;
1139 bzero(sc->event_filter, sizeof sc->event_filter);
1140 break;
1141 case APMDEV_NORMAL:
1142 sc->sc_flags &= ~SCFLAG_ONORMAL;
1143 break;
1145 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1146 sc->event_count = 0;
1147 sc->event_ptr = 0;
1149 return 0;
1152 static int
1153 apmioctl(struct dev_ioctl_args *ap)
1155 cdev_t dev = ap->a_head.a_dev;
1156 struct apm_softc *sc = &apm_softc;
1157 struct apm_bios_arg *args;
1158 int error = 0;
1159 int ret;
1160 int newstate;
1162 if (!sc->initialized)
1163 return (ENXIO);
1164 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", ap->a_cmd);
1166 switch (ap->a_cmd) {
1167 case APMIO_SUSPEND:
1168 if (!(ap->a_fflag & FWRITE))
1169 return (EPERM);
1170 if (sc->active)
1171 apm_suspend(PMST_SUSPEND);
1172 else
1173 error = EINVAL;
1174 break;
1176 case APMIO_STANDBY:
1177 if (!(ap->a_fflag & FWRITE))
1178 return (EPERM);
1179 if (sc->active)
1180 apm_suspend(PMST_STANDBY);
1181 else
1182 error = EINVAL;
1183 break;
1185 case APMIO_GETINFO_OLD:
1187 struct apm_info info;
1188 apm_info_old_t aiop;
1190 if (apm_get_info(&info))
1191 error = ENXIO;
1192 aiop = (apm_info_old_t)ap->a_data;
1193 aiop->ai_major = info.ai_major;
1194 aiop->ai_minor = info.ai_minor;
1195 aiop->ai_acline = info.ai_acline;
1196 aiop->ai_batt_stat = info.ai_batt_stat;
1197 aiop->ai_batt_life = info.ai_batt_life;
1198 aiop->ai_status = info.ai_status;
1200 break;
1201 case APMIO_GETINFO:
1202 if (apm_get_info((apm_info_t)ap->a_data))
1203 error = ENXIO;
1204 break;
1205 case APMIO_GETPWSTATUS:
1206 if (apm_get_pwstatus((apm_pwstatus_t)ap->a_data))
1207 error = ENXIO;
1208 break;
1209 case APMIO_ENABLE:
1210 if (!(ap->a_fflag & FWRITE))
1211 return (EPERM);
1212 apm_event_enable();
1213 break;
1214 case APMIO_DISABLE:
1215 if (!(ap->a_fflag & FWRITE))
1216 return (EPERM);
1217 apm_event_disable();
1218 break;
1219 case APMIO_HALTCPU:
1220 if (!(ap->a_fflag & FWRITE))
1221 return (EPERM);
1222 apm_halt_cpu();
1223 break;
1224 case APMIO_NOTHALTCPU:
1225 if (!(ap->a_fflag & FWRITE))
1226 return (EPERM);
1227 apm_not_halt_cpu();
1228 break;
1229 case APMIO_DISPLAY:
1230 if (!(ap->a_fflag & FWRITE))
1231 return (EPERM);
1232 newstate = *(int *)ap->a_data;
1233 if (apm_display(newstate))
1234 error = ENXIO;
1235 break;
1236 case APMIO_BIOS:
1237 if (!(ap->a_fflag & FWRITE))
1238 return (EPERM);
1239 /* XXX compatibility with the old interface */
1240 args = (struct apm_bios_arg *)ap->a_data;
1241 sc->bios.r.eax = args->eax;
1242 sc->bios.r.ebx = args->ebx;
1243 sc->bios.r.ecx = args->ecx;
1244 sc->bios.r.edx = args->edx;
1245 sc->bios.r.esi = args->esi;
1246 sc->bios.r.edi = args->edi;
1247 if ((ret = apm_bioscall())) {
1249 * Return code 1 means bios call was unsuccessful.
1250 * Error code is stored in %ah.
1251 * Return code -1 means bios call was unsupported
1252 * in the APM BIOS version.
1254 if (ret == -1) {
1255 error = EINVAL;
1257 } else {
1259 * Return code 0 means bios call was successful.
1260 * We need only %al and can discard %ah.
1262 sc->bios.r.eax &= 0xff;
1264 args->eax = sc->bios.r.eax;
1265 args->ebx = sc->bios.r.ebx;
1266 args->ecx = sc->bios.r.ecx;
1267 args->edx = sc->bios.r.edx;
1268 args->esi = sc->bios.r.esi;
1269 args->edi = sc->bios.r.edi;
1270 break;
1271 default:
1272 error = EINVAL;
1273 break;
1276 /* for /dev/apmctl */
1277 if (APMDEV(dev) == APMDEV_CTL) {
1278 struct apm_event_info *evp;
1279 int i;
1281 error = 0;
1282 switch (ap->a_cmd) {
1283 case APMIO_NEXTEVENT:
1284 if (!sc->event_count) {
1285 error = EAGAIN;
1286 } else {
1287 evp = (struct apm_event_info *)ap->a_data;
1288 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1289 i %= APM_NEVENTS;
1290 *evp = sc->event_list[i];
1291 sc->event_count--;
1293 break;
1294 case APMIO_REJECTLASTREQ:
1295 if (apm_lastreq_rejected()) {
1296 error = EINVAL;
1298 break;
1299 default:
1300 error = EINVAL;
1301 break;
1305 return error;
1308 static int
1309 apmwrite(struct dev_write_args *ap)
1311 cdev_t dev = ap->a_head.a_dev;
1312 struct uio *uio = ap->a_uio;
1313 struct apm_softc *sc = &apm_softc;
1314 u_int event_type;
1315 int error;
1316 u_char enabled;
1318 if (APMDEV(dev) != APMDEV_CTL)
1319 return(ENODEV);
1320 if (uio->uio_resid != sizeof(u_int))
1321 return(E2BIG);
1323 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1324 return(error);
1326 if (event_type < 0 || event_type >= APM_NPMEV)
1327 return(EINVAL);
1329 if (sc->event_filter[event_type] == 0) {
1330 enabled = 1;
1331 } else {
1332 enabled = 0;
1334 sc->event_filter[event_type] = enabled;
1335 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1337 return uio->uio_resid;
1340 static int
1341 apmpoll(struct dev_poll_args *ap)
1343 struct apm_softc *sc = &apm_softc;
1344 int revents = 0;
1346 if (ap->a_events & (POLLIN | POLLRDNORM)) {
1347 if (sc->event_count) {
1348 revents |= ap->a_events & (POLLIN | POLLRDNORM);
1349 } else {
1350 selrecord(curthread, &sc->sc_rsel);
1353 ap->a_events = revents;
1354 return (0);
1358 * Because apm is a static device that always exists under any attached
1359 * isa device, and not scanned by the isa device, we need an identify
1360 * function to install the device so we can probe for it.
1362 static device_method_t apm_methods[] = {
1363 /* Device interface */
1364 DEVMETHOD(device_identify, bus_generic_identify),
1365 DEVMETHOD(device_probe, apm_probe),
1366 DEVMETHOD(device_attach, apm_attach),
1368 { 0, 0 }
1371 static driver_t apm_driver = {
1372 "apm",
1373 apm_methods,
1374 1, /* no softc (XXX) */
1377 static devclass_t apm_devclass;
1379 DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);