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