apm.c revision 37414
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 * $Id: apm.c,v 1.72 1998/06/07 17:09:55 dfr Exp $ 19 */ 20 21#include "opt_devfs.h" 22#include "opt_vm86.h" 23 24#include <sys/param.h> 25#include <sys/conf.h> 26#include <sys/kernel.h> 27#ifdef DEVFS 28#include <sys/devfsext.h> 29#endif /*DEVFS*/ 30#include <sys/systm.h> 31#include <sys/time.h> 32#include <i386/isa/isa_device.h> 33#include <machine/apm_bios.h> 34#include <machine/segments.h> 35#include <machine/clock.h> 36#include <vm/vm.h> 37#include <vm/vm_param.h> 38#include <vm/pmap.h> 39#include <sys/syslog.h> 40#include <i386/apm/apm_setup.h> 41 42#ifdef VM86 43#include <machine/psl.h> 44#include <machine/vm86.h> 45#endif 46 47static int apm_display __P((int newstate)); 48static int apm_int __P((u_long *eax, u_long *ebx, u_long *ecx, u_long *edx)); 49static void apm_resume __P((void)); 50 51/* static data */ 52struct apm_softc { 53 int initialized, active; 54 int always_halt_cpu, slow_idle_cpu; 55 int disabled, disengaged; 56 u_int minorversion, majorversion; 57 u_int cs32_base, cs16_base, ds_base; 58 u_int cs16_limit, cs32_limit, ds_limit; 59 u_int cs_entry; 60 u_int intversion; 61 struct apmhook sc_suspend; 62 struct apmhook sc_resume; 63#ifdef DEVFS 64 void *sc_devfs_token; 65#endif 66}; 67 68static struct apm_softc apm_softc; 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_ioctl_t apmioctl; 83 84#define CDEV_MAJOR 39 85static struct cdevsw apm_cdevsw = 86 { apmopen, apmclose, noread, nowrite, /*39*/ 87 apmioctl, nostop, nullreset, nodevtotty,/* APM */ 88 seltrue, nommap, NULL , "apm" ,NULL, -1}; 89 90/* setup APM GDT discriptors */ 91static void 92setup_apm_gdt(u_int code32_base, u_int code16_base, u_int data_base, u_int code32_limit, u_int code16_limit, u_int data_limit) 93{ 94 /* setup 32bit code segment */ 95 gdt_segs[GAPMCODE32_SEL].ssd_base = code32_base; 96 gdt_segs[GAPMCODE32_SEL].ssd_limit = code32_limit; 97 98 /* setup 16bit code segment */ 99 gdt_segs[GAPMCODE16_SEL].ssd_base = code16_base; 100 gdt_segs[GAPMCODE16_SEL].ssd_limit = code16_limit; 101 102 /* setup data segment */ 103 gdt_segs[GAPMDATA_SEL ].ssd_base = data_base; 104 gdt_segs[GAPMDATA_SEL ].ssd_limit = data_limit; 105 106 /* reflect these changes on physical GDT */ 107 ssdtosd(gdt_segs + GAPMCODE32_SEL, &gdt[GAPMCODE32_SEL].sd); 108 ssdtosd(gdt_segs + GAPMCODE16_SEL, &gdt[GAPMCODE16_SEL].sd); 109 ssdtosd(gdt_segs + GAPMDATA_SEL , &gdt[GAPMDATA_SEL ].sd); 110} 111 112/* 48bit far pointer. Do not staticize - used from apm_setup.s */ 113struct addr48 { 114 u_long offset; 115 u_short segment; 116} apm_addr; 117 118static int apm_errno; 119 120static int 121apm_int(u_long *eax, u_long *ebx, u_long *ecx, u_long *edx) 122{ 123 struct apm_bios_arg apa; 124 int cf; 125 126 apa.eax = *eax; 127 apa.ebx = *ebx; 128 apa.ecx = *ecx; 129 apa.edx = *edx; 130 cf = apm_bios_call(&apa); 131 *eax = apa.eax; 132 *ebx = apa.ebx; 133 *ecx = apa.ecx; 134 *edx = apa.edx; 135 apm_errno = ((*eax) >> 8) & 0xff; 136 return cf; 137} 138 139 140/* enable/disable power management */ 141static int 142apm_enable_disable_pm(int enable) 143{ 144 struct apm_softc *sc = &apm_softc; 145 146 u_long eax, ebx, ecx, edx; 147 148 eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM; 149 150 if (sc->intversion >= INTVERSION(1, 1)) 151 ebx = PMDV_ALLDEV; 152 else 153 ebx = 0xffff; /* APM version 1.0 only */ 154 ecx = enable; 155 edx = 0; 156 return apm_int(&eax, &ebx, &ecx, &edx); 157} 158 159static void 160apm_driver_version(int version) 161{ 162 u_long eax, ebx, ecx, edx; 163 164 /* First try APM 1.2 */ 165 eax = (APM_BIOS << 8) | APM_DRVVERSION; 166 ebx = 0x0; 167 ecx = version; 168 edx = 0; 169 if(!apm_int(&eax, &ebx, &ecx, &edx)) 170 apm_version = eax & 0xffff; 171} 172 173/* engage/disengage power management (APM 1.1 or later) */ 174static int 175apm_engage_disengage_pm(int engage) 176{ 177 u_long eax, ebx, ecx, edx; 178 179 eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM; 180 ebx = PMDV_ALLDEV; 181 ecx = engage; 182 edx = 0; 183 return(apm_int(&eax, &ebx, &ecx, &edx)); 184} 185 186/* get PM event */ 187static u_int 188apm_getevent(void) 189{ 190 u_long eax, ebx, ecx, edx; 191 192 eax = (APM_BIOS << 8) | APM_GETPMEVENT; 193 194 ebx = 0; 195 ecx = 0; 196 edx = 0; 197 if (apm_int(&eax, &ebx, &ecx, &edx)) 198 return PMEV_NOEVENT; 199 200 return ebx & 0xffff; 201} 202 203/* suspend entire system */ 204static int 205apm_suspend_system(int state) 206{ 207 u_long eax, ebx, ecx, edx; 208 209 eax = (APM_BIOS << 8) | APM_SETPWSTATE; 210 ebx = PMDV_ALLDEV; 211 ecx = state; 212 edx = 0; 213 214 if (apm_int(&eax, &ebx, &ecx, &edx)) { 215 printf("Entire system suspend failure: errcode = %ld\n", 216 0xff & (eax >> 8)); 217 return 1; 218 } 219 return 0; 220} 221 222/* Display control */ 223/* 224 * Experimental implementation: My laptop machine can't handle this function 225 * If your laptop can control the display via APM, please inform me. 226 * HOSOKAWA, Tatsumi <hosokawa@jp.FreeBSD.org> 227 */ 228static int 229apm_display(int newstate) 230{ 231 u_long eax, ebx, ecx, edx; 232 233 eax = (APM_BIOS << 8) | APM_SETPWSTATE; 234 ebx = PMDV_DISP0; 235 ecx = newstate ? PMST_APMENABLED:PMST_SUSPEND; 236 edx = 0; 237 if (apm_int(&eax, &ebx, &ecx, &edx)) { 238 printf("Display off failure: errcode = %ld\n", 239 0xff & (eax >> 8)); 240 return 1; 241 } 242 return 0; 243} 244 245/* 246 * Turn off the entire system. 247 */ 248void 249apm_power_off(void) 250{ 251 u_long eax, ebx, ecx, edx; 252 253 if (!apm_softc.active) 254 return; 255 eax = (APM_BIOS << 8) | APM_SETPWSTATE; 256 ebx = PMDV_ALLDEV; 257 ecx = PMST_OFF; 258 edx = 0; 259 apm_int(&eax, &ebx, &ecx, &edx); 260} 261 262/* APM Battery low handler */ 263static void 264apm_battery_low(void) 265{ 266 printf("\007\007 * * * BATTERY IS LOW * * * \007\007"); 267} 268 269/* APM hook manager */ 270static struct apmhook * 271apm_add_hook(struct apmhook **list, struct apmhook *ah) 272{ 273 int s; 274 struct apmhook *p, *prev; 275 276#ifdef APM_DEBUG 277 printf("Add hook \"%s\"\n", ah->ah_name); 278#endif 279 280 s = splhigh(); 281 if (ah == NULL) 282 panic("illegal apm_hook!"); 283 prev = NULL; 284 for (p = *list; p != NULL; prev = p, p = p->ah_next) 285 if (p->ah_order > ah->ah_order) 286 break; 287 288 if (prev == NULL) { 289 ah->ah_next = *list; 290 *list = ah; 291 } else { 292 ah->ah_next = prev->ah_next; 293 prev->ah_next = ah; 294 } 295 splx(s); 296 return ah; 297} 298 299static void 300apm_del_hook(struct apmhook **list, struct apmhook *ah) 301{ 302 int s; 303 struct apmhook *p, *prev; 304 305 s = splhigh(); 306 prev = NULL; 307 for (p = *list; p != NULL; prev = p, p = p->ah_next) 308 if (p == ah) 309 goto deleteit; 310 panic("Tried to delete unregistered apm_hook."); 311 goto nosuchnode; 312deleteit: 313 if (prev != NULL) 314 prev->ah_next = p->ah_next; 315 else 316 *list = p->ah_next; 317nosuchnode: 318 splx(s); 319} 320 321 322/* APM driver calls some functions automatically */ 323static void 324apm_execute_hook(struct apmhook *list) 325{ 326 struct apmhook *p; 327 328 for (p = list; p != NULL; p = p->ah_next) { 329#ifdef APM_DEBUG 330 printf("Execute APM hook \"%s.\"\n", p->ah_name); 331#endif 332 if ((*(p->ah_fun))(p->ah_arg)) 333 printf("Warning: APM hook \"%s\" failed", p->ah_name); 334 } 335} 336 337 338/* establish an apm hook */ 339struct apmhook * 340apm_hook_establish(int apmh, struct apmhook *ah) 341{ 342 if (apmh < 0 || apmh >= NAPM_HOOK) 343 return NULL; 344 345 return apm_add_hook(&hook[apmh], ah); 346} 347 348/* disestablish an apm hook */ 349void 350apm_hook_disestablish(int apmh, struct apmhook *ah) 351{ 352 if (apmh < 0 || apmh >= NAPM_HOOK) 353 return; 354 355 apm_del_hook(&hook[apmh], ah); 356} 357 358 359static struct timeval suspend_time; 360static struct timeval diff_time; 361 362static int 363apm_default_resume(void *arg) 364{ 365 int pl; 366 u_int second, minute, hour; 367 struct timeval resume_time, tmp_time; 368 369 /* modified for adjkerntz */ 370 pl = splsoftclock(); 371 inittodr(0); /* adjust time to RTC */ 372 microtime(&resume_time); 373 getmicrotime(&tmp_time); 374 timevaladd(&tmp_time, &diff_time); 375 376#ifdef FIXME 377 /* XXX THIS DOESN'T WORK!!! */ 378 time = tmp_time; 379#endif 380 381#ifdef APM_FIXUP_CALLTODO 382 /* Calculate the delta time suspended */ 383 timevalsub(&resume_time, &suspend_time); 384 /* Fixup the calltodo list with the delta time. */ 385 adjust_timeout_calltodo(&resume_time); 386#endif /* APM_FIXUP_CALLTODOK */ 387 splx(pl); 388#ifndef APM_FIXUP_CALLTODO 389 second = resume_time.tv_sec - suspend_time.tv_sec; 390#else /* APM_FIXUP_CALLTODO */ 391 /* 392 * We've already calculated resume_time to be the delta between 393 * the suspend and the resume. 394 */ 395 second = resume_time.tv_sec; 396#endif /* APM_FIXUP_CALLTODO */ 397 hour = second / 3600; 398 second %= 3600; 399 minute = second / 60; 400 second %= 60; 401 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n", 402 hour, minute, second); 403 return 0; 404} 405 406static int 407apm_default_suspend(void *arg) 408{ 409 int pl; 410 411 pl = splsoftclock(); 412 microtime(&diff_time); 413 inittodr(0); 414 microtime(&suspend_time); 415 timevalsub(&diff_time, &suspend_time); 416 splx(pl); 417 return 0; 418} 419 420static void apm_processevent(void); 421 422/* 423 * Public interface to the suspend/resume: 424 * 425 * Execute suspend and resume hook before and after sleep, respectively. 426 * 427 */ 428 429void 430apm_suspend(int state) 431{ 432 struct apm_softc *sc = &apm_softc; 433 434 if (!sc) 435 return; 436 437 if (sc->initialized) { 438 apm_execute_hook(hook[APM_HOOK_SUSPEND]); 439 if (apm_suspend_system(state) == 0) 440 apm_processevent(); 441 else 442 /* Failure, 'resume' the system again */ 443 apm_execute_hook(hook[APM_HOOK_RESUME]); 444 } 445} 446 447void 448apm_resume(void) 449{ 450 struct apm_softc *sc = &apm_softc; 451 452 if (!sc) 453 return; 454 455 if (sc->initialized) 456 apm_execute_hook(hook[APM_HOOK_RESUME]); 457} 458 459 460/* get APM information */ 461static int 462apm_get_info(apm_info_t aip) 463{ 464 struct apm_softc *sc = &apm_softc; 465 u_long eax, ebx, ecx, edx; 466 467 eax = (APM_BIOS << 8) | APM_GETPWSTATUS; 468 ebx = PMDV_ALLDEV; 469 ecx = 0; 470 edx = 0xffff; /* default to unknown battery time */ 471 472 if (apm_int(&eax, &ebx, &ecx, &edx)) 473 return 1; 474 475 aip->ai_infoversion = 1; 476 aip->ai_acline = (ebx >> 8) & 0xff; 477 aip->ai_batt_stat = ebx & 0xff; 478 aip->ai_batt_life = ecx & 0xff; 479 aip->ai_major = (u_int)sc->majorversion; 480 aip->ai_minor = (u_int)sc->minorversion; 481 aip->ai_status = (u_int)sc->active; 482 edx &= 0xffff; 483 if (edx == 0xffff) /* Time is unknown */ 484 aip->ai_batt_time = -1; 485 else if (edx & 0x8000) /* Time is in minutes */ 486 aip->ai_batt_time = (edx & 0x7fff) * 60; 487 else /* Time is in seconds */ 488 aip->ai_batt_time = edx; 489 490 eax = (APM_BIOS << 8) | APM_GETCAPABILITIES; 491 ebx = 0; 492 ecx = 0; 493 edx = 0; 494 if (apm_int(&eax, &ebx, &ecx, &edx)) { 495 aip->ai_batteries = -1; /* Unknown */ 496 aip->ai_capabilities = 0xff00; /* Unknown, with no bits set */ 497 } else { 498 aip->ai_batteries = ebx & 0xff; 499 aip->ai_capabilities = ecx & 0xf; 500 } 501 502 bzero(aip->ai_spare, sizeof aip->ai_spare); 503 504 return 0; 505} 506 507 508/* inform APM BIOS that CPU is idle */ 509void 510apm_cpu_idle(void) 511{ 512 struct apm_softc *sc = &apm_softc; 513 514 if (sc->active) { 515 u_long eax, ebx, ecx, edx; 516 517 eax = (APM_BIOS <<8) | APM_CPUIDLE; 518 edx = ecx = ebx = 0; 519 apm_int(&eax, &ebx, &ecx, &edx); 520 } 521 /* 522 * Some APM implementation halts CPU in BIOS, whenever 523 * "CPU-idle" function are invoked, but swtch() of 524 * FreeBSD halts CPU, therefore, CPU is halted twice 525 * in the sched loop. It makes the interrupt latency 526 * terribly long and be able to cause a serious problem 527 * in interrupt processing. We prevent it by removing 528 * "hlt" operation from swtch() and managed it under 529 * APM driver. 530 */ 531 if (!sc->active || sc->always_halt_cpu) 532 __asm("hlt"); /* wait for interrupt */ 533} 534 535/* inform APM BIOS that CPU is busy */ 536void 537apm_cpu_busy(void) 538{ 539 struct apm_softc *sc = &apm_softc; 540 541 /* 542 * The APM specification says this is only necessary if your BIOS 543 * slows down the processor in the idle task, otherwise it's not 544 * necessary. 545 */ 546 if (sc->slow_idle_cpu && sc->active) { 547 u_long eax, ebx, ecx, edx; 548 549 eax = (APM_BIOS <<8) | APM_CPUBUSY; 550 edx = ecx = ebx = 0; 551 apm_int(&eax, &ebx, &ecx, &edx); 552 } 553} 554 555 556/* 557 * APM timeout routine: 558 * 559 * This routine is automatically called by timer once per second. 560 */ 561 562static void 563apm_timeout(void *dummy) 564{ 565 struct apm_softc *sc = &apm_softc; 566 567 apm_processevent(); 568 if (sc->active == 1) 569 /* Run slightly more oftan than 1 Hz */ 570 apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1 ); 571} 572 573/* enable APM BIOS */ 574static void 575apm_event_enable(void) 576{ 577 struct apm_softc *sc = &apm_softc; 578 579#ifdef APM_DEBUG 580 printf("called apm_event_enable()\n"); 581#endif 582 if (sc->initialized) { 583 sc->active = 1; 584 apm_timeout(sc); 585 } 586} 587 588/* disable APM BIOS */ 589static void 590apm_event_disable(void) 591{ 592 struct apm_softc *sc = &apm_softc; 593 594#ifdef APM_DEBUG 595 printf("called apm_event_disable()\n"); 596#endif 597 if (sc->initialized) { 598 untimeout(apm_timeout, NULL, apm_timeout_ch); 599 sc->active = 0; 600 } 601} 602 603/* halt CPU in scheduling loop */ 604static void 605apm_halt_cpu(void) 606{ 607 struct apm_softc *sc = &apm_softc; 608 609 if (sc->initialized) 610 sc->always_halt_cpu = 1; 611} 612 613/* don't halt CPU in scheduling loop */ 614static void 615apm_not_halt_cpu(void) 616{ 617 struct apm_softc *sc = &apm_softc; 618 619 if (sc->initialized) 620 sc->always_halt_cpu = 0; 621} 622 623/* device driver definitions */ 624static int apmprobe (struct isa_device *); 625static int apmattach(struct isa_device *); 626struct isa_driver apmdriver = { apmprobe, apmattach, "apm" }; 627 628/* 629 * probe APM (dummy): 630 * 631 * APM probing routine is placed on locore.s and apm_init.S because 632 * this process forces the CPU to turn to real mode or V86 mode. 633 * Current version uses real mode, but in a future version, we want 634 * to use V86 mode in APM initialization. 635 * 636 * XXX If VM86 is defined, we do. 637 */ 638 639static int 640apmprobe(struct isa_device *dvp) 641{ 642#ifdef VM86 643 struct vm86frame vmf; 644 int i; 645#endif 646 647 if ( dvp->id_unit > 0 ) { 648 printf("apm: Only one APM driver supported.\n"); 649 return 0; 650 } 651 652#ifdef VM86 653 bzero(&vmf, sizeof(struct vm86frame)); /* safety */ 654 vmf.vmf_ax = 0x5300; 655 vmf.vmf_bx = 0; 656 if (((i = vm86_intcall(0x15, &vmf)) == 0) && 657 !(vmf.vmf_eflags & PSL_C) && 658 (vmf.vmf_bx == 0x504d)) { 659 660 apm_version = vmf.vmf_ax; 661 apm_flags = vmf.vmf_cx; 662 663 vmf.vmf_ax = 0x5303; 664 vmf.vmf_bx = 0; 665 if (((i = vm86_intcall(0x15, &vmf)) == 0) && 666 !(vmf.vmf_eflags & PSL_C)) { 667 668 apm_cs32_base = vmf.vmf_ax; 669 apm_cs_entry = vmf.vmf_ebx; 670 apm_cs16_base = vmf.vmf_cx; 671 apm_ds_base = vmf.vmf_dx; 672 apm_cs32_limit = vmf.vmf_si; 673 if (apm_version >= 0x0102) 674 apm_cs16_limit = (vmf.esi.r_ex >> 16); 675 apm_ds_limit = vmf.vmf_di; 676#ifdef APM_DEBUG 677 printf("apm: BIOS probe/32-bit connect successful\n"); 678#endif 679 } else { 680 /* XXX constant typo! */ 681 if (vmf.vmf_ah == APME_PROT32NOTDUPPORTED) { 682 apm_version = APMINI_NOT32BIT; 683 } else { 684 apm_version = APMINI_CONNECTERR; 685 } 686#ifdef APM_DEBUG 687 printf("apm: BIOS 32-bit connect failed: error 0x%x carry %d ah 0x%x\n", 688 i, (vmf.vmf_eflags & PSL_C) ? 1 : 0, vmf.vmf_ah); 689#endif 690 } 691 } else { 692 apm_version = APMINI_CANTFIND; 693#ifdef APM_DEBUG 694 printf("apm: BIOS probe failed: error 0x%x carry %d bx 0x%x\n", 695 i, (vmf.vmf_eflags & PSL_C) ? 1 : 0, vmf.vmf_bx); 696#endif 697 } 698#endif 699 700 bzero(&apm_softc, sizeof(apm_softc)); 701 702 switch (apm_version) { 703 case APMINI_CANTFIND: 704 /* silent */ 705 return 0; 706 case APMINI_NOT32BIT: 707 printf("apm: 32bit connection is not supported.\n"); 708 return 0; 709 case APMINI_CONNECTERR: 710 printf("apm: 32-bit connection error.\n"); 711 return 0; 712 } 713 if (dvp->id_flags & 0x20) 714 statclock_disable = 1; 715 return -1; 716} 717 718 719/* Process APM event */ 720static void 721apm_processevent(void) 722{ 723 int apm_event; 724 725#ifdef APM_DEBUG 726# define OPMEV_DEBUGMESSAGE(symbol) case symbol: \ 727 printf("Received APM Event: " #symbol "\n"); 728#else 729# define OPMEV_DEBUGMESSAGE(symbol) case symbol: 730#endif 731 do { 732 apm_event = apm_getevent(); 733 switch (apm_event) { 734 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ); 735 apm_suspend(PMST_STANDBY); 736 break; 737 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ); 738 apm_suspend(PMST_SUSPEND); 739 break; 740 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ); 741 apm_suspend(PMST_SUSPEND); 742 break; 743 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND); 744 apm_suspend(PMST_SUSPEND); 745 break; 746 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME); 747 apm_resume(); 748 break; 749 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME); 750 apm_resume(); 751 break; 752 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME); 753 apm_resume(); 754 break; 755 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW); 756 apm_battery_low(); 757 apm_suspend(PMST_SUSPEND); 758 break; 759 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE); 760 break; 761 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME); 762 inittodr(0); /* adjust time to RTC */ 763 break; 764 case PMEV_NOEVENT: 765 break; 766 default: 767 printf("Unknown Original APM Event 0x%x\n", apm_event); 768 break; 769 } 770 } while (apm_event != PMEV_NOEVENT); 771} 772 773/* 774 * Attach APM: 775 * 776 * Initialize APM driver (APM BIOS itself has been initialized in locore.s) 777 */ 778 779static int 780apmattach(struct isa_device *dvp) 781{ 782#define APM_KERNBASE KERNBASE 783 struct apm_softc *sc = &apm_softc; 784 785 sc->initialized = 0; 786 787 /* Must be externally enabled */ 788 sc->active = 0; 789 790 /* setup APM parameters */ 791 sc->cs16_base = (apm_cs16_base << 4) + APM_KERNBASE; 792 sc->cs32_base = (apm_cs32_base << 4) + APM_KERNBASE; 793 sc->ds_base = (apm_ds_base << 4) + APM_KERNBASE; 794 sc->cs32_limit = apm_cs32_limit - 1; 795 if (apm_cs16_limit == 0) 796 apm_cs16_limit == apm_cs32_limit; 797 sc->cs16_limit = apm_cs16_limit - 1; 798 sc->ds_limit = apm_ds_limit - 1; 799 sc->cs_entry = apm_cs_entry; 800 801 /* Always call HLT in idle loop */ 802 sc->always_halt_cpu = 1; 803 804 sc->slow_idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0); 805 sc->disabled = ((apm_flags & APM_DISABLED) != 0); 806 sc->disengaged = ((apm_flags & APM_DISENGAGED) != 0); 807 808 /* print bootstrap messages */ 809#ifdef APM_DEBUG 810 printf("apm: APM BIOS version %04x\n", apm_version); 811 printf("apm: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n", 812 sc->cs32_base, sc->cs16_base, sc->ds_base); 813 printf("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n", 814 sc->cs_entry, is_enabled(sc->slow_idle_cpu), 815 is_enabled(!sc->disabled)); 816 printf("apm: CS32_limit=0x%x, CS16_limit=0x%x, DS_limit=0x%x\n", 817 (u_short)sc->cs32_limit, (u_short)sc->cs16_limit, (u_short)sc->ds_limit); 818#endif /* APM_DEBUG */ 819 820#if 0 821 /* Workaround for some buggy APM BIOS implementations */ 822 sc->cs_limit = 0xffff; 823 sc->ds_limit = 0xffff; 824#endif 825 826 /* setup GDT */ 827 setup_apm_gdt(sc->cs32_base, sc->cs16_base, sc->ds_base, 828 sc->cs32_limit, sc->cs16_limit, sc->ds_limit); 829 830 /* setup entry point 48bit pointer */ 831 apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL); 832 apm_addr.offset = sc->cs_entry; 833 834 if ((dvp->id_flags & 0x10)) { 835 if ((dvp->id_flags & 0xf) >= 0x2) { 836 apm_driver_version(0x102); 837 } 838 if (!apm_version && (dvp->id_flags & 0xf) >= 0x1) { 839 apm_driver_version(0x101); 840 } 841 } else { 842 apm_driver_version(0x102); 843 if (!apm_version) 844 apm_driver_version(0x101); 845 } 846 if (!apm_version) 847 apm_version = 0x100; 848 849 sc->minorversion = ((apm_version & 0x00f0) >> 4) * 10 + 850 ((apm_version & 0x000f) >> 0); 851 sc->majorversion = ((apm_version & 0xf000) >> 12) * 10 + 852 ((apm_version & 0x0f00) >> 8); 853 854 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion); 855 856#ifdef APM_DEBUG 857 if (sc->intversion >= INTVERSION(1, 1)) 858 printf("apm: Engaged control %s\n", is_enabled(!sc->disengaged)); 859#endif 860 861 printf("apm: found APM BIOS version %d.%d\n", 862 sc->majorversion, sc->minorversion); 863 864#ifdef APM_DEBUG 865 printf("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu)); 866#endif 867 868 /* enable power management */ 869 if (sc->disabled) { 870 if (apm_enable_disable_pm(1)) { 871#ifdef APM_DEBUG 872 printf("apm: *Warning* enable function failed! [%x]\n", 873 apm_errno); 874#endif 875 } 876 } 877 878 /* engage power managment (APM 1.1 or later) */ 879 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) { 880 if (apm_engage_disengage_pm(1)) { 881#ifdef APM_DEBUG 882 printf("apm: *Warning* engage function failed err=[%x]", 883 apm_errno); 884 printf(" (Docked or using external power?).\n"); 885#endif 886 } 887 } 888 889 /* default suspend hook */ 890 sc->sc_suspend.ah_fun = apm_default_suspend; 891 sc->sc_suspend.ah_arg = sc; 892 sc->sc_suspend.ah_name = "default suspend"; 893 sc->sc_suspend.ah_order = APM_MAX_ORDER; 894 895 /* default resume hook */ 896 sc->sc_resume.ah_fun = apm_default_resume; 897 sc->sc_resume.ah_arg = sc; 898 sc->sc_resume.ah_name = "default resume"; 899 sc->sc_resume.ah_order = APM_MIN_ORDER; 900 901 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend); 902 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume); 903 904 apm_event_enable(); 905 906 sc->initialized = 1; 907 908#ifdef DEVFS 909 sc->sc_devfs_token = 910 devfs_add_devswf(&apm_cdevsw, 0, DV_CHR, 0, 0, 0600, "apm"); 911#endif 912 return 0; 913} 914 915static int 916apmopen(dev_t dev, int flag, int fmt, struct proc *p) 917{ 918 struct apm_softc *sc = &apm_softc; 919 920 if (minor(dev) != 0 || !sc->initialized) 921 return (ENXIO); 922 923 return 0; 924} 925 926static int 927apmclose(dev_t dev, int flag, int fmt, struct proc *p) 928{ 929 return 0; 930} 931 932static int 933apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 934{ 935 struct apm_softc *sc = &apm_softc; 936 int error = 0; 937 int newstate; 938 939 if (minor(dev) != 0 || !sc->initialized) 940 return (ENXIO); 941#ifdef APM_DEBUG 942 printf("APM ioctl: cmd = 0x%x\n", cmd); 943#endif 944 switch (cmd) { 945 case APMIO_SUSPEND: 946 if (sc->active) 947 apm_suspend(PMST_SUSPEND); 948 else 949 error = EINVAL; 950 break; 951 952 case APMIO_STANDBY: 953 if (sc->active) 954 apm_suspend(PMST_STANDBY); 955 else 956 error = EINVAL; 957 break; 958 959 case APMIO_GETINFO_OLD: 960 { 961 struct apm_info info; 962 apm_info_old_t aiop; 963 964 if (apm_get_info(&info)) 965 error = ENXIO; 966 aiop = (apm_info_old_t)addr; 967 aiop->ai_major = info.ai_major; 968 aiop->ai_minor = info.ai_minor; 969 aiop->ai_acline = info.ai_acline; 970 aiop->ai_batt_stat = info.ai_batt_stat; 971 aiop->ai_batt_life = info.ai_batt_life; 972 aiop->ai_status = info.ai_status; 973 } 974 break; 975 case APMIO_GETINFO: 976 if (apm_get_info((apm_info_t)addr)) 977 error = ENXIO; 978 break; 979 case APMIO_ENABLE: 980 apm_event_enable(); 981 break; 982 case APMIO_DISABLE: 983 apm_event_disable(); 984 break; 985 case APMIO_HALTCPU: 986 apm_halt_cpu(); 987 break; 988 case APMIO_NOTHALTCPU: 989 apm_not_halt_cpu(); 990 break; 991 case APMIO_DISPLAY: 992 newstate = *(int *)addr; 993 if (apm_display(newstate)) 994 error = ENXIO; 995 break; 996 case APMIO_BIOS: 997 if (apm_bios_call((struct apm_bios_arg*)addr) == 0) 998 ((struct apm_bios_arg*)addr)->eax &= 0xff; 999 break; 1000 default: 1001 error = EINVAL; 1002 break; 1003 } 1004 return error; 1005} 1006 1007 1008static apm_devsw_installed = 0; 1009 1010static void 1011apm_drvinit(void *unused) 1012{ 1013 dev_t dev; 1014 1015 if( ! apm_devsw_installed ) { 1016 dev = makedev(CDEV_MAJOR,0); 1017 cdevsw_add(&dev,&apm_cdevsw,NULL); 1018 apm_devsw_installed = 1; 1019 } 1020} 1021 1022SYSINIT(apmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,apm_drvinit,NULL) 1023