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