Cleanup some of the newbus infrastructure.
[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 $
39b5d600 19 * $DragonFly: src/sys/platform/pc32/apm/apm.c,v 1.12 2005/10/28 03:25: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>
9acd5bbb 43#include <sys/thread2.h>
984263bc
MD
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 */
3ae0cd58 51int apm_display (int newstate);
984263bc
MD
52struct apm_softc apm_softc;
53
3ae0cd58 54static void apm_resume (void);
984263bc 55static int apm_bioscall(void);
3ae0cd58 56static int apm_check_function_supported (u_int version, u_int func);
984263bc
MD
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
b02bbab0 77static struct callout apm_timeout_ch;
984263bc
MD
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{
984263bc
MD
326 struct apmhook *p, *prev;
327
328 APM_DPRINT("Add hook \"%s\"\n", ah->ah_name);
329
9acd5bbb 330 crit_enter();
984263bc
MD
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 }
9acd5bbb 345 crit_exit();
984263bc
MD
346 return ah;
347}
348
349static void
350apm_del_hook(struct apmhook **list, struct apmhook *ah)
351{
984263bc
MD
352 struct apmhook *p, *prev;
353
9acd5bbb 354 crit_enter();
984263bc
MD
355 prev = NULL;
356 for (p = *list; p != NULL; prev = p, p = p->ah_next)
357 if (p == ah)
358 goto deleteit;
359 panic("Tried to delete unregistered apm_hook.");
360 goto nosuchnode;
361deleteit:
362 if (prev != NULL)
363 prev->ah_next = p->ah_next;
364 else
365 *list = p->ah_next;
366nosuchnode:
9acd5bbb 367 crit_exit();
984263bc
MD
368}
369
370
371/* APM driver calls some functions automatically */
372static void
373apm_execute_hook(struct apmhook *list)
374{
375 struct apmhook *p;
376
377 for (p = list; p != NULL; p = p->ah_next) {
378 APM_DPRINT("Execute APM hook \"%s.\"\n", p->ah_name);
379 if ((*(p->ah_fun))(p->ah_arg))
380 printf("Warning: APM hook \"%s\" failed", p->ah_name);
381 }
382}
383
384
385/* establish an apm hook */
386struct apmhook *
387apm_hook_establish(int apmh, struct apmhook *ah)
388{
389 if (apmh < 0 || apmh >= NAPM_HOOK)
390 return NULL;
391
392 return apm_add_hook(&hook[apmh], ah);
393}
394
395/* disestablish an apm hook */
396void
397apm_hook_disestablish(int apmh, struct apmhook *ah)
398{
399 if (apmh < 0 || apmh >= NAPM_HOOK)
400 return;
401
402 apm_del_hook(&hook[apmh], ah);
403}
404
405
406static struct timeval suspend_time;
407static struct timeval diff_time;
408
409static int
410apm_default_resume(void *arg)
411{
984263bc
MD
412 u_int second, minute, hour;
413 struct timeval resume_time, tmp_time;
414
415 /* modified for adjkerntz */
9acd5bbb 416 crit_enter();
984263bc
MD
417 timer_restore(); /* restore the all timers */
418 inittodr(0); /* adjust time to RTC */
419 microtime(&resume_time);
420 getmicrotime(&tmp_time);
421 timevaladd(&tmp_time, &diff_time);
422
423#ifdef FIXME
424 /* XXX THIS DOESN'T WORK!!! */
425 time = tmp_time;
426#endif
427
428#ifdef APM_FIXUP_CALLTODO
429 /* Calculate the delta time suspended */
430 timevalsub(&resume_time, &suspend_time);
431 /* Fixup the calltodo list with the delta time. */
432 adjust_timeout_calltodo(&resume_time);
433#endif /* APM_FIXUP_CALLTODOK */
9acd5bbb 434 crit_exit();
984263bc
MD
435#ifndef APM_FIXUP_CALLTODO
436 second = resume_time.tv_sec - suspend_time.tv_sec;
437#else /* APM_FIXUP_CALLTODO */
438 /*
439 * We've already calculated resume_time to be the delta between
440 * the suspend and the resume.
441 */
442 second = resume_time.tv_sec;
443#endif /* APM_FIXUP_CALLTODO */
444 hour = second / 3600;
445 second %= 3600;
446 minute = second / 60;
447 second %= 60;
448 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n",
449 hour, minute, second);
450 return 0;
451}
452
453static int
454apm_default_suspend(void *arg)
455{
9acd5bbb 456 crit_enter();
984263bc
MD
457 microtime(&diff_time);
458 inittodr(0);
459 microtime(&suspend_time);
460 timevalsub(&diff_time, &suspend_time);
9acd5bbb 461 crit_exit();
984263bc
MD
462 return 0;
463}
464
3ae0cd58 465static int apm_record_event (struct apm_softc *, u_int);
984263bc
MD
466static void apm_processevent(void);
467
468static u_int apm_op_inprog = 0;
469
470static void
471apm_do_suspend(void)
472{
473 struct apm_softc *sc = &apm_softc;
474 int error;
475
476 if (!sc)
477 return;
478
479 apm_op_inprog = 0;
480 sc->suspends = sc->suspend_countdown = 0;
481
482 if (sc->initialized) {
483 error = DEVICE_SUSPEND(root_bus);
484 if (error) {
485 DEVICE_RESUME(root_bus);
486 } else {
487 apm_execute_hook(hook[APM_HOOK_SUSPEND]);
488 if (apm_suspend_system(PMST_SUSPEND) == 0) {
489 apm_processevent();
490 } else {
491 /* Failure, 'resume' the system again */
492 apm_execute_hook(hook[APM_HOOK_RESUME]);
493 DEVICE_RESUME(root_bus);
494 }
495 }
496 }
497}
498
499static void
500apm_do_standby(void)
501{
502 struct apm_softc *sc = &apm_softc;
503
504 if (!sc)
505 return;
506
507 apm_op_inprog = 0;
508 sc->standbys = sc->standby_countdown = 0;
509
510 if (sc->initialized) {
511 /*
512 * As far as standby, we don't need to execute
513 * all of suspend hooks.
514 */
515 apm_default_suspend(&apm_softc);
516 if (apm_suspend_system(PMST_STANDBY) == 0)
517 apm_processevent();
518 }
519}
520
521static void
522apm_lastreq_notify(void)
523{
524 struct apm_softc *sc = &apm_softc;
525
526 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
527 sc->bios.r.ebx = PMDV_ALLDEV;
528 sc->bios.r.ecx = PMST_LASTREQNOTIFY;
529 sc->bios.r.edx = 0;
530 apm_bioscall();
531}
532
533static int
534apm_lastreq_rejected(void)
535{
536 struct apm_softc *sc = &apm_softc;
537
538 if (apm_op_inprog == 0) {
539 return 1; /* no operation in progress */
540 }
541
542 sc->bios.r.eax = (APM_BIOS << 8) | APM_SETPWSTATE;
543 sc->bios.r.ebx = PMDV_ALLDEV;
544 sc->bios.r.ecx = PMST_LASTREQREJECT;
545 sc->bios.r.edx = 0;
546
547 if (apm_bioscall()) {
548 APM_DPRINT("apm_lastreq_rejected: failed\n");
549 return 1;
550 }
551 apm_op_inprog = 0;
552 return 0;
553}
554
555/*
556 * Public interface to the suspend/resume:
557 *
558 * Execute suspend and resume hook before and after sleep, respectively.
559 *
560 */
561
562void
563apm_suspend(int state)
564{
565 struct apm_softc *sc = &apm_softc;
566
567 if (!sc->initialized)
568 return;
569
570 switch (state) {
571 case PMST_SUSPEND:
572 if (sc->suspends)
573 return;
574 sc->suspends++;
575 sc->suspend_countdown = apm_suspend_delay;
576 break;
577 case PMST_STANDBY:
578 if (sc->standbys)
579 return;
580 sc->standbys++;
581 sc->standby_countdown = apm_standby_delay;
582 break;
583 default:
584 printf("apm_suspend: Unknown Suspend state 0x%x\n", state);
585 return;
586 }
587
588 apm_op_inprog++;
589 apm_lastreq_notify();
590}
591
592void
593apm_resume(void)
594{
595 struct apm_softc *sc = &apm_softc;
596
597 if (!sc)
598 return;
599
600 if (sc->initialized) {
601 apm_execute_hook(hook[APM_HOOK_RESUME]);
602 DEVICE_RESUME(root_bus);
603 }
604}
605
606
607/* get power status per battery */
608static int
609apm_get_pwstatus(apm_pwstatus_t app)
610{
611 struct apm_softc *sc = &apm_softc;
612
613 if (app->ap_device != PMDV_ALLDEV &&
614 (app->ap_device < PMDV_BATT0 || app->ap_device > PMDV_BATT_ALL))
615 return 1;
616
617 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETPWSTATUS;
618 sc->bios.r.ebx = app->ap_device;
619 sc->bios.r.ecx = 0;
620 sc->bios.r.edx = 0xffff; /* default to unknown battery time */
621
622 if (apm_bioscall())
623 return 1;
624
625 app->ap_acline = (sc->bios.r.ebx >> 8) & 0xff;
626 app->ap_batt_stat = sc->bios.r.ebx & 0xff;
627 app->ap_batt_flag = (sc->bios.r.ecx >> 8) & 0xff;
628 app->ap_batt_life = sc->bios.r.ecx & 0xff;
629 sc->bios.r.edx &= 0xffff;
630 if (sc->bios.r.edx == 0xffff) /* Time is unknown */
631 app->ap_batt_time = -1;
632 else if (sc->bios.r.edx & 0x8000) /* Time is in minutes */
633 app->ap_batt_time = (sc->bios.r.edx & 0x7fff) * 60;
634 else /* Time is in seconds */
635 app->ap_batt_time = sc->bios.r.edx;
636
637 return 0;
638}
639
640
641/* get APM information */
642static int
643apm_get_info(apm_info_t aip)
644{
645 struct apm_softc *sc = &apm_softc;
646 struct apm_pwstatus aps;
647
648 bzero(&aps, sizeof(aps));
649 aps.ap_device = PMDV_ALLDEV;
650 if (apm_get_pwstatus(&aps))
651 return 1;
652
653 aip->ai_infoversion = 1;
654 aip->ai_acline = aps.ap_acline;
655 aip->ai_batt_stat = aps.ap_batt_stat;
656 aip->ai_batt_life = aps.ap_batt_life;
657 aip->ai_batt_time = aps.ap_batt_time;
658 aip->ai_major = (u_int)sc->majorversion;
659 aip->ai_minor = (u_int)sc->minorversion;
660 aip->ai_status = (u_int)sc->active;
661
662 sc->bios.r.eax = (APM_BIOS << 8) | APM_GETCAPABILITIES;
663 sc->bios.r.ebx = 0;
664 sc->bios.r.ecx = 0;
665 sc->bios.r.edx = 0;
666 if (apm_bioscall()) {
667 aip->ai_batteries = -1; /* Unknown */
668 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */
669 } else {
670 aip->ai_batteries = sc->bios.r.ebx & 0xff;
671 aip->ai_capabilities = sc->bios.r.ecx & 0xf;
672 }
673
674 bzero(aip->ai_spare, sizeof aip->ai_spare);
675
676 return 0;
677}
678
679
680/* inform APM BIOS that CPU is idle */
681void
682apm_cpu_idle(void)
683{
684 struct apm_softc *sc = &apm_softc;
685
686 if (sc->active) {
687
688 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUIDLE;
689 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
690 (void) apm_bioscall();
691 }
692 /*
693 * Some APM implementation halts CPU in BIOS, whenever
694 * "CPU-idle" function are invoked, but swtch() of
695 * FreeBSD halts CPU, therefore, CPU is halted twice
696 * in the sched loop. It makes the interrupt latency
697 * terribly long and be able to cause a serious problem
698 * in interrupt processing. We prevent it by removing
699 * "hlt" operation from swtch() and managed it under
700 * APM driver.
701 */
702 if (!sc->active || sc->always_halt_cpu)
703 __asm("hlt"); /* wait for interrupt */
704}
705
706/* inform APM BIOS that CPU is busy */
707void
708apm_cpu_busy(void)
709{
710 struct apm_softc *sc = &apm_softc;
711
712 /*
713 * The APM specification says this is only necessary if your BIOS
714 * slows down the processor in the idle task, otherwise it's not
715 * necessary.
716 */
717 if (sc->slow_idle_cpu && sc->active) {
718
719 sc->bios.r.eax = (APM_BIOS <<8) | APM_CPUBUSY;
720 sc->bios.r.edx = sc->bios.r.ecx = sc->bios.r.ebx = 0;
721 apm_bioscall();
722 }
723}
724
725
726/*
727 * APM timeout routine:
728 *
729 * This routine is automatically called by timer once per second.
730 */
731
732static void
733apm_timeout(void *dummy)
734{
735 struct apm_softc *sc = &apm_softc;
736
737 if (apm_op_inprog)
738 apm_lastreq_notify();
739
740 if (sc->standbys && sc->standby_countdown-- <= 0)
741 apm_do_standby();
742
743 if (sc->suspends && sc->suspend_countdown-- <= 0)
744 apm_do_suspend();
745
746 if (!sc->bios_busy)
747 apm_processevent();
748
b02bbab0 749 if (sc->active == 1) {
984263bc 750 /* Run slightly more oftan than 1 Hz */
b02bbab0
JS
751 callout_reset(&apm_timeout_ch, hz - 1, apm_timeout, NULL);
752 }
984263bc
MD
753}
754
755/* enable APM BIOS */
756static void
757apm_event_enable(void)
758{
759 struct apm_softc *sc = &apm_softc;
760
761 APM_DPRINT("called apm_event_enable()\n");
762 if (sc->initialized) {
763 sc->active = 1;
b02bbab0 764 callout_init(&apm_timeout_ch);
984263bc
MD
765 apm_timeout(sc);
766 }
767}
768
769/* disable APM BIOS */
770static void
771apm_event_disable(void)
772{
773 struct apm_softc *sc = &apm_softc;
774
775 APM_DPRINT("called apm_event_disable()\n");
776 if (sc->initialized) {
b02bbab0 777 callout_stop(&apm_timeout_ch);
984263bc
MD
778 sc->active = 0;
779 }
780}
781
782/* halt CPU in scheduling loop */
783static void
784apm_halt_cpu(void)
785{
786 struct apm_softc *sc = &apm_softc;
787
788 if (sc->initialized)
789 sc->always_halt_cpu = 1;
790}
791
792/* don't halt CPU in scheduling loop */
793static void
794apm_not_halt_cpu(void)
795{
796 struct apm_softc *sc = &apm_softc;
797
798 if (sc->initialized)
799 sc->always_halt_cpu = 0;
800}
801
802/* device driver definitions */
803
984263bc
MD
804/*
805 * probe for APM BIOS
806 */
807static int
808apm_probe(device_t dev)
809{
810#define APM_KERNBASE KERNBASE
811 struct vm86frame vmf;
812 struct apm_softc *sc = &apm_softc;
813 int disabled, flags;
814
815 if (resource_int_value("apm", 0, "disabled", &disabled) == 0
816 && disabled != 0)
817 return ENXIO;
818
819 device_set_desc(dev, "APM BIOS");
820
821 if ( device_get_unit(dev) > 0 ) {
822 printf("apm: Only one APM driver supported.\n");
823 return ENXIO;
824 }
825
826 if (resource_int_value("apm", 0, "flags", &flags) != 0)
827 flags = 0;
828
829 bzero(&vmf, sizeof(struct vm86frame)); /* safety */
830 bzero(&apm_softc, sizeof(apm_softc));
831 vmf.vmf_ah = APM_BIOS;
832 vmf.vmf_al = APM_INSTCHECK;
833 vmf.vmf_bx = 0;
834 if (vm86_intcall(APM_INT, &vmf))
835 return ENXIO; /* APM not found */
836 if (vmf.vmf_bx != 0x504d) {
837 printf("apm: incorrect signature (0x%x)\n", vmf.vmf_bx);
838 return ENXIO;
839 }
840 if ((vmf.vmf_cx & (APM_32BIT_SUPPORT | APM_16BIT_SUPPORT)) == 0) {
841 printf("apm: protected mode connections are not supported\n");
842 return ENXIO;
843 }
844
845 apm_version = vmf.vmf_ax;
846 sc->slow_idle_cpu = ((vmf.vmf_cx & APM_CPUIDLE_SLOW) != 0);
847 sc->disabled = ((vmf.vmf_cx & APM_DISABLED) != 0);
848 sc->disengaged = ((vmf.vmf_cx & APM_DISENGAGED) != 0);
849
850 vmf.vmf_ah = APM_BIOS;
851 vmf.vmf_al = APM_DISCONNECT;
852 vmf.vmf_bx = 0;
853 vm86_intcall(APM_INT, &vmf); /* disconnect, just in case */
854
855 if ((vmf.vmf_cx & APM_32BIT_SUPPORT) != 0) {
856 vmf.vmf_ah = APM_BIOS;
857 vmf.vmf_al = APM_PROT32CONNECT;
858 vmf.vmf_bx = 0;
859 if (vm86_intcall(APM_INT, &vmf)) {
860 printf("apm: 32-bit connection error.\n");
861 return (ENXIO);
862 }
863 sc->bios.seg.code32.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
864 sc->bios.seg.code32.limit = 0xffff;
865 sc->bios.seg.code16.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
866 sc->bios.seg.code16.limit = 0xffff;
867 sc->bios.seg.data.base = (vmf.vmf_dx << 4) + APM_KERNBASE;
868 sc->bios.seg.data.limit = 0xffff;
869 sc->bios.entry = vmf.vmf_ebx;
870 sc->connectmode = APM_PROT32CONNECT;
871 } else {
872 /* use 16-bit connection */
873 vmf.vmf_ah = APM_BIOS;
874 vmf.vmf_al = APM_PROT16CONNECT;
875 vmf.vmf_bx = 0;
876 if (vm86_intcall(APM_INT, &vmf)) {
877 printf("apm: 16-bit connection error.\n");
878 return (ENXIO);
879 }
880 sc->bios.seg.code16.base = (vmf.vmf_ax << 4) + APM_KERNBASE;
881 sc->bios.seg.code16.limit = 0xffff;
882 sc->bios.seg.data.base = (vmf.vmf_cx << 4) + APM_KERNBASE;
883 sc->bios.seg.data.limit = 0xffff;
884 sc->bios.entry = vmf.vmf_bx;
885 sc->connectmode = APM_PROT16CONNECT;
886 }
887 return(0);
888}
889
890
891/*
892 * return 0 if the user will notice and handle the event,
893 * return 1 if the kernel driver should do so.
894 */
895static int
896apm_record_event(struct apm_softc *sc, u_int event_type)
897{
898 struct apm_event_info *evp;
899
900 if ((sc->sc_flags & SCFLAG_OPEN) == 0)
901 return 1; /* no user waiting */
902 if (sc->event_count == APM_NEVENTS)
903 return 1; /* overflow */
904 if (sc->event_filter[event_type] == 0)
905 return 1; /* not registered */
906 evp = &sc->event_list[sc->event_ptr];
907 sc->event_count++;
908 sc->event_ptr++;
909 sc->event_ptr %= APM_NEVENTS;
910 evp->type = event_type;
911 evp->index = ++apm_evindex;
912 selwakeup(&sc->sc_rsel);
913 return (sc->sc_flags & SCFLAG_OCTL) ? 0 : 1; /* user may handle */
914}
915
916/* Process APM event */
917static void
918apm_processevent(void)
919{
920 int apm_event;
921 struct apm_softc *sc = &apm_softc;
922
923#define OPMEV_DEBUGMESSAGE(symbol) case symbol: \
924 APM_DPRINT("Received APM Event: " #symbol "\n");
925
926 do {
927 apm_event = apm_getevent();
928 switch (apm_event) {
929 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ);
930 if (apm_op_inprog == 0) {
931 apm_op_inprog++;
932 if (apm_record_event(sc, apm_event)) {
933 apm_suspend(PMST_STANDBY);
934 }
935 }
936 break;
937 OPMEV_DEBUGMESSAGE(PMEV_USERSTANDBYREQ);
938 if (apm_op_inprog == 0) {
939 apm_op_inprog++;
940 if (apm_record_event(sc, apm_event)) {
941 apm_suspend(PMST_STANDBY);
942 }
943 }
944 break;
945 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ);
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();
951 }
952 }
953 return; /* XXX skip the rest */
954 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ);
955 apm_lastreq_notify();
956 if (apm_op_inprog == 0) {
957 apm_op_inprog++;
958 if (apm_record_event(sc, apm_event)) {
959 apm_do_suspend();
960 }
961 }
962 return; /* XXX skip the rest */
963 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND);
964 apm_do_suspend();
965 break;
966 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME);
967 apm_record_event(sc, apm_event);
968 apm_resume();
969 break;
970 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME);
971 apm_record_event(sc, apm_event);
972 apm_resume();
973 break;
974 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME);
975 apm_record_event(sc, apm_event);
976 apm_resume();
977 break;
978 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW);
979 if (apm_record_event(sc, apm_event)) {
980 apm_battery_low();
981 apm_suspend(PMST_SUSPEND);
982 }
983 break;
984 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE);
985 apm_record_event(sc, apm_event);
986 break;
987 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME);
988 apm_record_event(sc, apm_event);
989 inittodr(0); /* adjust time to RTC */
990 break;
991 OPMEV_DEBUGMESSAGE(PMEV_CAPABILITIESCHANGE);
992 apm_record_event(sc, apm_event);
993 break;
994 case PMEV_NOEVENT:
995 break;
996 default:
997 printf("Unknown Original APM Event 0x%x\n", apm_event);
998 break;
999 }
1000 } while (apm_event != PMEV_NOEVENT);
1001}
1002
1003/*
1004 * Attach APM:
1005 *
1006 * Initialize APM driver
1007 */
1008
1009static int
1010apm_attach(device_t dev)
1011{
1012 struct apm_softc *sc = &apm_softc;
1013 int flags;
1014 int drv_version;
1015
1016 if (resource_int_value("apm", 0, "flags", &flags) != 0)
1017 flags = 0;
1018
984263bc
MD
1019 sc->initialized = 0;
1020
1021 /* Must be externally enabled */
1022 sc->active = 0;
1023
1024 /* Always call HLT in idle loop */
1025 sc->always_halt_cpu = 1;
1026
1027 getenv_int("debug.apm_debug", &apm_debug);
1028
1029 /* print bootstrap messages */
1030 APM_DPRINT("apm: APM BIOS version %04lx\n", apm_version);
1031 APM_DPRINT("apm: Code16 0x%08x, Data 0x%08x\n",
1032 sc->bios.seg.code16.base, sc->bios.seg.data.base);
1033 APM_DPRINT("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n",
1034 sc->bios.entry, is_enabled(sc->slow_idle_cpu),
1035 is_enabled(!sc->disabled));
1036 APM_DPRINT("apm: CS_limit=0x%x, DS_limit=0x%x\n",
1037 sc->bios.seg.code16.limit, sc->bios.seg.data.limit);
1038
1039 /*
1040 * In one test, apm bios version was 1.02; an attempt to register
1041 * a 1.04 driver resulted in a 1.00 connection! Registering a
1042 * 1.02 driver resulted in a 1.02 connection.
1043 */
1044 drv_version = apm_version > 0x102 ? 0x102 : apm_version;
1045 for (; drv_version > 0x100; drv_version--)
1046 if (apm_driver_version(drv_version) == 0)
1047 break;
1048 sc->minorversion = ((drv_version & 0x00f0) >> 4) * 10 +
1049 ((drv_version & 0x000f) >> 0);
1050 sc->majorversion = ((drv_version & 0xf000) >> 12) * 10 +
1051 ((apm_version & 0x0f00) >> 8);
1052
1053 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion);
1054
1055 if (sc->intversion >= INTVERSION(1, 1))
1056 APM_DPRINT("apm: Engaged control %s\n", is_enabled(!sc->disengaged));
1057 device_printf(dev, "found APM BIOS v%ld.%ld, connected at v%d.%d\n",
1058 ((apm_version & 0xf000) >> 12) * 10 + ((apm_version & 0x0f00) >> 8),
1059 ((apm_version & 0x00f0) >> 4) * 10 + ((apm_version & 0x000f) >> 0),
1060 sc->majorversion, sc->minorversion);
1061
1062
1063 APM_DPRINT("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu));
1064 /* enable power management */
1065 if (sc->disabled) {
1066 if (apm_enable_disable_pm(1)) {
1067 APM_DPRINT("apm: *Warning* enable function failed! [%x]\n",
1068 (sc->bios.r.eax >> 8) & 0xff);
1069 }
1070 }
1071
1072 /* engage power managment (APM 1.1 or later) */
1073 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) {
1074 if (apm_engage_disengage_pm(1)) {
1075 APM_DPRINT("apm: *Warning* engage function failed err=[%x]",
1076 (sc->bios.r.eax >> 8) & 0xff);
1077 APM_DPRINT(" (Docked or using external power?).\n");
1078 }
1079 }
1080
1081 /* default suspend hook */
1082 sc->sc_suspend.ah_fun = apm_default_suspend;
1083 sc->sc_suspend.ah_arg = sc;
1084 sc->sc_suspend.ah_name = "default suspend";
1085 sc->sc_suspend.ah_order = APM_MAX_ORDER;
1086
1087 /* default resume hook */
1088 sc->sc_resume.ah_fun = apm_default_resume;
1089 sc->sc_resume.ah_arg = sc;
1090 sc->sc_resume.ah_name = "default resume";
1091 sc->sc_resume.ah_order = APM_MIN_ORDER;
1092
1093 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend);
1094 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume);
1095
1096 /* Power the system off using APM */
1097 EVENTHANDLER_REGISTER(shutdown_final, apm_power_off, NULL,
1098 SHUTDOWN_PRI_LAST);
1099
1100 sc->initialized = 1;
1101
e4c9c0c8 1102 cdevsw_add(&apm_cdevsw, 0, 0);
ba859f8f
MD
1103 make_dev(&apm_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0660, "apm");
1104 make_dev(&apm_cdevsw, 8, UID_ROOT, GID_OPERATOR, 0660, "apmctl");
984263bc
MD
1105 return 0;
1106}
1107
1108static int
41c20dac 1109apmopen(dev_t dev, int flag, int fmt, d_thread_t *td)
984263bc
MD
1110{
1111 struct apm_softc *sc = &apm_softc;
1112 int ctl = APMDEV(dev);
1113
1114 if (!sc->initialized)
1115 return (ENXIO);
1116
1117 switch (ctl) {
1118 case APMDEV_CTL:
1119 if (!(flag & FWRITE))
1120 return EINVAL;
1121 if (sc->sc_flags & SCFLAG_OCTL)
1122 return EBUSY;
1123 sc->sc_flags |= SCFLAG_OCTL;
1124 bzero(sc->event_filter, sizeof sc->event_filter);
1125 break;
1126 case APMDEV_NORMAL:
1127 sc->sc_flags |= SCFLAG_ONORMAL;
1128 break;
1129 default:
1130 return ENXIO;
1131 break;
1132 }
1133 return 0;
1134}
1135
1136static int
41c20dac 1137apmclose(dev_t dev, int flag, int fmt, d_thread_t *td)
984263bc
MD
1138{
1139 struct apm_softc *sc = &apm_softc;
1140 int ctl = APMDEV(dev);
1141
1142 switch (ctl) {
1143 case APMDEV_CTL:
1144 apm_lastreq_rejected();
1145 sc->sc_flags &= ~SCFLAG_OCTL;
1146 bzero(sc->event_filter, sizeof sc->event_filter);
1147 break;
1148 case APMDEV_NORMAL:
1149 sc->sc_flags &= ~SCFLAG_ONORMAL;
1150 break;
1151 }
1152 if ((sc->sc_flags & SCFLAG_OPEN) == 0) {
1153 sc->event_count = 0;
1154 sc->event_ptr = 0;
1155 }
1156 return 0;
1157}
1158
1159static int
41c20dac 1160apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td)
984263bc
MD
1161{
1162 struct apm_softc *sc = &apm_softc;
1163 struct apm_bios_arg *args;
1164 int error = 0;
1165 int ret;
1166 int newstate;
1167
1168 if (!sc->initialized)
1169 return (ENXIO);
1170 APM_DPRINT("APM ioctl: cmd = 0x%lx\n", cmd);
1171 switch (cmd) {
1172 case APMIO_SUSPEND:
1173 if (!(flag & FWRITE))
1174 return (EPERM);
1175 if (sc->active)
1176 apm_suspend(PMST_SUSPEND);
1177 else
1178 error = EINVAL;
1179 break;
1180
1181 case APMIO_STANDBY:
1182 if (!(flag & FWRITE))
1183 return (EPERM);
1184 if (sc->active)
1185 apm_suspend(PMST_STANDBY);
1186 else
1187 error = EINVAL;
1188 break;
1189
1190 case APMIO_GETINFO_OLD:
1191 {
1192 struct apm_info info;
1193 apm_info_old_t aiop;
1194
1195 if (apm_get_info(&info))
1196 error = ENXIO;
1197 aiop = (apm_info_old_t)addr;
1198 aiop->ai_major = info.ai_major;
1199 aiop->ai_minor = info.ai_minor;
1200 aiop->ai_acline = info.ai_acline;
1201 aiop->ai_batt_stat = info.ai_batt_stat;
1202 aiop->ai_batt_life = info.ai_batt_life;
1203 aiop->ai_status = info.ai_status;
1204 }
1205 break;
1206 case APMIO_GETINFO:
1207 if (apm_get_info((apm_info_t)addr))
1208 error = ENXIO;
1209 break;
1210 case APMIO_GETPWSTATUS:
1211 if (apm_get_pwstatus((apm_pwstatus_t)addr))
1212 error = ENXIO;
1213 break;
1214 case APMIO_ENABLE:
1215 if (!(flag & FWRITE))
1216 return (EPERM);
1217 apm_event_enable();
1218 break;
1219 case APMIO_DISABLE:
1220 if (!(flag & FWRITE))
1221 return (EPERM);
1222 apm_event_disable();
1223 break;
1224 case APMIO_HALTCPU:
1225 if (!(flag & FWRITE))
1226 return (EPERM);
1227 apm_halt_cpu();
1228 break;
1229 case APMIO_NOTHALTCPU:
1230 if (!(flag & FWRITE))
1231 return (EPERM);
1232 apm_not_halt_cpu();
1233 break;
1234 case APMIO_DISPLAY:
1235 if (!(flag & FWRITE))
1236 return (EPERM);
1237 newstate = *(int *)addr;
1238 if (apm_display(newstate))
1239 error = ENXIO;
1240 break;
1241 case APMIO_BIOS:
1242 if (!(flag & FWRITE))
1243 return (EPERM);
1244 /* XXX compatibility with the old interface */
1245 args = (struct apm_bios_arg *)addr;
1246 sc->bios.r.eax = args->eax;
1247 sc->bios.r.ebx = args->ebx;
1248 sc->bios.r.ecx = args->ecx;
1249 sc->bios.r.edx = args->edx;
1250 sc->bios.r.esi = args->esi;
1251 sc->bios.r.edi = args->edi;
1252 if ((ret = apm_bioscall())) {
1253 /*
1254 * Return code 1 means bios call was unsuccessful.
1255 * Error code is stored in %ah.
1256 * Return code -1 means bios call was unsupported
1257 * in the APM BIOS version.
1258 */
1259 if (ret == -1) {
1260 error = EINVAL;
1261 }
1262 } else {
1263 /*
1264 * Return code 0 means bios call was successful.
1265 * We need only %al and can discard %ah.
1266 */
1267 sc->bios.r.eax &= 0xff;
1268 }
1269 args->eax = sc->bios.r.eax;
1270 args->ebx = sc->bios.r.ebx;
1271 args->ecx = sc->bios.r.ecx;
1272 args->edx = sc->bios.r.edx;
1273 args->esi = sc->bios.r.esi;
1274 args->edi = sc->bios.r.edi;
1275 break;
1276 default:
1277 error = EINVAL;
1278 break;
1279 }
1280
1281 /* for /dev/apmctl */
1282 if (APMDEV(dev) == APMDEV_CTL) {
1283 struct apm_event_info *evp;
1284 int i;
1285
1286 error = 0;
1287 switch (cmd) {
1288 case APMIO_NEXTEVENT:
1289 if (!sc->event_count) {
1290 error = EAGAIN;
1291 } else {
1292 evp = (struct apm_event_info *)addr;
1293 i = sc->event_ptr + APM_NEVENTS - sc->event_count;
1294 i %= APM_NEVENTS;
1295 *evp = sc->event_list[i];
1296 sc->event_count--;
1297 }
1298 break;
1299 case APMIO_REJECTLASTREQ:
1300 if (apm_lastreq_rejected()) {
1301 error = EINVAL;
1302 }
1303 break;
1304 default:
1305 error = EINVAL;
1306 break;
1307 }
1308 }
1309
1310 return error;
1311}
1312
1313static int
1314apmwrite(dev_t dev, struct uio *uio, int ioflag)
1315{
1316 struct apm_softc *sc = &apm_softc;
1317 u_int event_type;
1318 int error;
1319 u_char enabled;
1320
1321 if (APMDEV(dev) != APMDEV_CTL)
1322 return(ENODEV);
1323 if (uio->uio_resid != sizeof(u_int))
1324 return(E2BIG);
1325
1326 if ((error = uiomove((caddr_t)&event_type, sizeof(u_int), uio)))
1327 return(error);
1328
1329 if (event_type < 0 || event_type >= APM_NPMEV)
1330 return(EINVAL);
1331
1332 if (sc->event_filter[event_type] == 0) {
1333 enabled = 1;
1334 } else {
1335 enabled = 0;
1336 }
1337 sc->event_filter[event_type] = enabled;
1338 APM_DPRINT("apmwrite: event 0x%x %s\n", event_type, is_enabled(enabled));
1339
1340 return uio->uio_resid;
1341}
1342
1343static int
41c20dac 1344apmpoll(dev_t dev, int events, d_thread_t *td)
984263bc
MD
1345{
1346 struct apm_softc *sc = &apm_softc;
1347 int revents = 0;
1348
1349 if (events & (POLLIN | POLLRDNORM)) {
1350 if (sc->event_count) {
1351 revents |= events & (POLLIN | POLLRDNORM);
1352 } else {
41c20dac 1353 selrecord(td, &sc->sc_rsel);
984263bc
MD
1354 }
1355 }
1356
1357 return (revents);
1358}
1359
39b5d600
MD
1360/*
1361 * Because apm is a static device that always exists under any attached
1362 * isa device, and not scanned by the isa device, we need an identify
1363 * function to install the device so we can probe for it.
1364 */
984263bc
MD
1365static device_method_t apm_methods[] = {
1366 /* Device interface */
39b5d600 1367 DEVMETHOD(device_identify, bus_generic_identify),
984263bc
MD
1368 DEVMETHOD(device_probe, apm_probe),
1369 DEVMETHOD(device_attach, apm_attach),
1370
1371 { 0, 0 }
1372};
1373
1374static driver_t apm_driver = {
1375 "apm",
1376 apm_methods,
1377 1, /* no softc (XXX) */
1378};
1379
1380static devclass_t apm_devclass;
1381
1382DRIVER_MODULE(apm, nexus, apm_driver, apm_devclass, 0, 0);
39b5d600 1383