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