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