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