apm.c revision 14603
165556Sjasone/* 265556Sjasone * APM (Advanced Power Management) BIOS Device Driver 365556Sjasone * 465556Sjasone * Copyright (c) 1994 UKAI, Fumitoshi. 565556Sjasone * Copyright (c) 1994-1995 by HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp> 665556Sjasone * 765556Sjasone * This software may be used, modified, copied, and distributed, in 865556Sjasone * both source and binary form provided that the above copyright and 965556Sjasone * these terms are retained. Under no circumstances is the author 1065556Sjasone * responsible for the proper functioning of this software, nor does 1165556Sjasone * the author assume any responsibility for damages incurred with its 1265556Sjasone * use. 1365556Sjasone * 1465556Sjasone * Sep, 1994 Implemented on FreeBSD 1.1.5.1R (Toshiba AVS001WD) 1565556Sjasone * 1665556Sjasone * $Id: apm.c,v 1.25 1996/03/12 06:09:34 nate Exp $ 1769012Sjhb */ 1865556Sjasone 1965556Sjasone#include "apm.h" 2069012Sjhb 2165556Sjasone#include <sys/param.h> 2265556Sjasone#include <sys/conf.h> 2365556Sjasone#include <sys/kernel.h> 2465556Sjasone#ifdef DEVFS 2565556Sjasone#include <sys/devfsext.h> 2665556Sjasone#endif /*DEVFS*/ 2765556Sjasone#include <sys/systm.h> 2865556Sjasone#include <sys/malloc.h> 2965556Sjasone#include <sys/ioctl.h> 3065556Sjasone#include <sys/file.h> 3168420Sjhb#include <sys/proc.h> 3268420Sjhb#include <sys/vnode.h> 3365556Sjasone#include "i386/isa/isa.h" 3465556Sjasone#include "i386/isa/isa_device.h" 35116182Sobrien#include <machine/apm_bios.h> 36116182Sobrien#include <machine/segments.h> 37116182Sobrien#include <machine/clock.h> 3870035Sjhb#include <vm/vm.h> 3968420Sjhb#include <vm/vm_param.h> 40103787Sjeff#include <vm/pmap.h> 4168420Sjhb#include <sys/syslog.h> 4270035Sjhb#include "apm_setup.h" 43103787Sjeff 4470035Sjhbstatic int apm_display_off __P((void)); 4570705Sjhbstatic int apm_int __P((u_long *eax, u_long *ebx, u_long *ecx)); 4665556Sjasonestatic void apm_resume __P((void)); 4768420Sjhb 4887793Sjhb/* static data */ 4965556Sjasonestruct apm_softc { 5068420Sjhb int initialized, active, halt_cpu; 5178784Sjhb u_int minorversion, majorversion; 5265556Sjasone u_int cs32_base, cs16_base, ds_base; 5393503Sjake u_int cs_limit, ds_limit; 5493950Sjake u_int cs_entry; 5593950Sjake u_int intversion; 5693950Sjake int idle_cpu, disabled, disengaged; 5793503Sjake struct apmhook sc_suspend; 58103787Sjeff struct apmhook sc_resume; 5970035Sjhb void *sc_devfs_token; 6070035Sjhb}; 6178784Sjhb 6278784Sjhbstatic struct apm_softc apm_softc[NAPM]; 6378784Sjhbstatic struct apm_softc *master_softc = NULL; /* XXX */ 6478784Sjhbstatic struct apmhook *hook[NAPM_HOOK]; /* XXX */ 6568420Sjhb 6668420Sjhb#define is_enabled(foo) ((foo) ? "enabled" : "disabled") 6768420Sjhb 6868420Sjhb/* Map version number to integer (keeps ordering of version numbers) */ 6968420Sjhb#define INTVERSION(major, minor) ((major)*100 + (minor)) 7068420Sjhb 7168420Sjhbstatic timeout_t apm_timeout; 7268420Sjhbstatic d_open_t apmopen; 7393503Sjakestatic d_close_t apmclose; 7493503Sjakestatic d_ioctl_t apmioctl; 7568420Sjhb 7668420Sjhb#define CDEV_MAJOR 39 7793503Sjakestatic struct cdevsw apm_cdevsw = 7893503Sjake { apmopen, apmclose, noread, nowrite, /*39*/ 7970705Sjhb apmioctl, nostop, nullreset, nodevtotty,/* APM */ 8070705Sjhb seltrue, nommap, NULL , "apm" ,NULL, -1}; 8170705Sjhb 8270705Sjhb/* setup APM GDT discriptors */ 8377843Speterstatic void 8477900Spetersetup_apm_gdt(u_int code32_base, u_int code16_base, u_int data_base, u_int code_limit, u_int data_limit) 8570705Sjhb{ 8665556Sjasone /* setup 32bit code segment */ 8777843Speter gdt_segs[GAPMCODE32_SEL].ssd_base = code32_base; 8877900Speter gdt_segs[GAPMCODE32_SEL].ssd_limit = code_limit; 8970705Sjhb 9065556Sjasone /* setup 16bit code segment */ 9165556Sjasone gdt_segs[GAPMCODE16_SEL].ssd_base = code16_base; 9270705Sjhb gdt_segs[GAPMCODE16_SEL].ssd_limit = code_limit; 9365556Sjasone 9493503Sjake /* setup data segment */ 9593503Sjake gdt_segs[GAPMDATA_SEL ].ssd_base = data_base; 9693503Sjake gdt_segs[GAPMDATA_SEL ].ssd_limit = data_limit; 9765556Sjasone 9865556Sjasone /* reflect these changes on physical GDT */ 9968420Sjhb ssdtosd(gdt_segs + GAPMCODE32_SEL, &gdt[GAPMCODE32_SEL].sd); 10093503Sjake ssdtosd(gdt_segs + GAPMCODE16_SEL, &gdt[GAPMCODE16_SEL].sd); 10193503Sjake ssdtosd(gdt_segs + GAPMDATA_SEL , &gdt[GAPMDATA_SEL ].sd); 10277900Speter} 10370705Sjhb 10493503Sjake/* 48bit far pointer */ 10568420Sjhbstatic struct addr48 { 106103787Sjeff u_long offset; 107103787Sjeff u_short segment; 108103787Sjeff} apm_addr; 109103787Sjeff 110103787Sjeffstatic int apm_errno; 111103787Sjeff 112103787Sjeffinline 113103787Sjeffint 114103787Sjeffapm_int(u_long *eax, u_long *ebx, u_long *ecx) 115103787Sjeff{ 116103787Sjeff u_long cf; 117103787Sjeff __asm ("pushl %%ebp 118103787Sjeff pushl %%edx 119103787Sjeff pushl %%esi 120103787Sjeff xorl %3,%3 121103787Sjeff movl %3,%%esi 122103787Sjeff lcall _apm_addr 123103787Sjeff jnc 1f 124103787Sjeff incl %3 125103787Sjeff 1: 126103787Sjeff popl %%esi 127103787Sjeff popl %%edx 128103787Sjeff popl %%ebp" 129103787Sjeff : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=D" (cf) 130103787Sjeff : "0" (*eax), "1" (*ebx), "2" (*ecx) 131103787Sjeff ); 132103787Sjeff apm_errno = ((*eax) >> 8) & 0xff; 133103787Sjeff return cf; 134103787Sjeff} 135103787Sjeff 136103787Sjeff 137103787Sjeff/* enable/disable power management */ 138103787Sjeffstatic int 139103787Sjeffapm_enable_disable_pm(struct apm_softc *sc, int enable) 140103787Sjeff{ 141103787Sjeff u_long eax, ebx, ecx; 142103787Sjeff 143103787Sjeff eax = (APM_BIOS << 8) | APM_ENABLEDISABLEPM; 144116697Srwatson 145116697Srwatson if (sc->intversion >= INTVERSION(1, 1)) { 146116697Srwatson ebx = PMDV_ALLDEV; 147103787Sjeff } else { 148103787Sjeff ebx = 0xffff; /* APM version 1.0 only */ 149103787Sjeff } 150103787Sjeff ecx = enable; 151103787Sjeff return apm_int(&eax, &ebx, &ecx); 152103787Sjeff} 153103787Sjeff 154103787Sjeff/* Tell APM-BIOS that WE will do 1.1 and see what they say... */ 155103787Sjeffstatic void 156103787Sjeffapm_driver_version(void) 157103787Sjeff{ 158103787Sjeff u_long eax, ebx, ecx; 159103787Sjeff 160103787Sjeff#ifdef APM_DEBUG 161103787Sjeff eax = (APM_BIOS<<8) | APM_INSTCHECK; 162103787Sjeff ebx = 0x0; 163103787Sjeff ecx = 0x0101; 164103787Sjeff i = apm_int(&eax, &ebx, &ecx); 165103787Sjeff printf("[%04lx %04lx %04lx %ld %02x]\n", 166103787Sjeff eax, ebx, ecx, i, apm_errno); 167103787Sjeff#endif 16868420Sjhb 16993503Sjake eax = (APM_BIOS << 8) | APM_DRVVERSION; 17093503Sjake ebx = 0x0; 17193503Sjake ecx = 0x0101; 17268420Sjhb if(!apm_int(&eax, &ebx, &ecx)) 17368420Sjhb apm_version = eax & 0xffff; 174103787Sjeff 175103787Sjeff#ifdef APM_DEBUG 176103787Sjeff eax = (APM_BIOS << 8) | APM_INSTCHECK; 17774903Sjhb ebx = 0x0; 178103787Sjeff ecx = 0x0101; 179103787Sjeff i = apm_int(&eax, &ebx, &ecx); 18087793Sjhb printf("[%04lx %04lx %04lx %ld %02x]\n", 18193503Sjake eax, ebx, ecx, i, apm_errno); 18291904Sjhb#endif 18368420Sjhb} 18469880Sjhb 18569880Sjhb/* engage/disengage power management (APM 1.1 or later) */ 18668420Sjhbstatic int 18768420Sjhbapm_engage_disengage_pm(struct apm_softc *sc, int engage) 18893503Sjake{ 18993503Sjake u_long eax, ebx, ecx, i; 19093503Sjake 191103787Sjeff eax = (APM_BIOS << 8) | APM_ENGAGEDISENGAGEPM; 19287793Sjhb ebx = PMDV_ALLDEV; 193116101Sjhb ecx = engage; 19487793Sjhb i = apm_int(&eax, &ebx, &ecx); 195116101Sjhb return i; 19693503Sjake} 197103787Sjeff 198103787Sjeff/* get PM event */ 199103787Sjeffstatic u_int 200114471Sjulianapm_getevent(struct apm_softc *sc) 201103995Sjeff{ 202103787Sjeff u_long eax, ebx, ecx; 203103787Sjeff 204103787Sjeff eax = (APM_BIOS << 8) | APM_GETPMEVENT; 205103787Sjeff 206103787Sjeff ebx = 0; 207103787Sjeff ecx = 0; 208103787Sjeff if (apm_int(&eax, &ebx, &ecx)) 209103787Sjeff return PMEV_NOEVENT; 210103787Sjeff 211103787Sjeff return ebx & 0xffff; 212103787Sjeff} 21368420Sjhb 21468420Sjhb/* suspend entire system */ 21568420Sjhbstatic int 21668420Sjhbapm_suspend_system(struct apm_softc *sc) 21768420Sjhb{ 218103787Sjeff u_long eax, ebx, ecx; 21993503Sjake 22091904Sjhb eax = (APM_BIOS << 8) | APM_SETPWSTATE; 221112105Sjhb ebx = PMDV_ALLDEV; 222112105Sjhb ecx = PMST_SUSPEND; 223112105Sjhb 22493503Sjake __asm("cli"); 22568420Sjhb if (apm_int(&eax, &ebx, &ecx)) { 22693503Sjake __asm("sti"); 22768420Sjhb printf("Entire system suspend failure: errcode = %ld\n", 22868782Sjhb 0xff & (eax >> 8)); 22993503Sjake return 1; 23068782Sjhb } 23193503Sjake __asm("sti"); 23293503Sjake return 0; 23393503Sjake} 23493503Sjake 23593503Sjake/* Display control */ 23668420Sjhb/* 23768420Sjhb * Experimental implementation: My laptop machine can't handle this function 23893503Sjake * If your laptop can control the display via APM, please inform me. 23968420Sjhb * HOSOKAWA, Tatsumi <hosokawa@mt.cs.keio.ac.jp> 24093503Sjake */ 24193503Sjakestatic int 24293503Sjakeapm_display_off(void) 24393503Sjake{ 24493503Sjake u_long eax, ebx, ecx; 24593503Sjake 246103787Sjeff eax = (APM_BIOS << 8) | APM_SETPWSTATE; 247103787Sjeff ebx = PMDV_2NDSTORAGE0; 248103787Sjeff ecx = PMST_STANDBY; 249103787Sjeff if (apm_int(&eax, &ebx, &ecx)) { 250103787Sjeff printf("Display off failure: errcode = %ld\n", 251103787Sjeff 0xff & (eax >> 8)); 252116101Sjhb return 1; 25368420Sjhb } 25468420Sjhb 25570035Sjhb return 0; 25670035Sjhb} 25770035Sjhb 25870035Sjhb/* APM Battery low handler */ 25970035Sjhbstatic void 26070035Sjhbapm_battery_low(struct apm_softc *sc) 26170035Sjhb{ 26270035Sjhb printf("\007\007 * * * BATTERY IS LOW * * * \007\007"); 26370035Sjhb} 26470035Sjhb 26570035Sjhb/* APM hook manager */ 26672755Sjhbstatic struct apmhook * 26772755Sjhbapm_add_hook(struct apmhook **list, struct apmhook *ah) 26872755Sjhb{ 26970035Sjhb int s; 27072755Sjhb struct apmhook *p, *prev; 271111270Sjake 27270035Sjhb#if 0 27372755Sjhb printf("Add hook \"%s\"\n", ah->ah_name); 27470035Sjhb#endif 27570035Sjhb 27670035Sjhb s = splhigh(); 27770035Sjhb if (ah == NULL) { 27870035Sjhb panic("illegal apm_hook!"); 27970035Sjhb } 280111270Sjake prev = NULL; 281111270Sjake for (p = *list; p != NULL; prev = p, p = p->ah_next) { 28272755Sjhb if (p->ah_order > ah->ah_order) { 283111270Sjake break; 284111270Sjake } 285111270Sjake } 286111270Sjake 28772755Sjhb if (prev == NULL) { 28872755Sjhb ah->ah_next = *list; 28972755Sjhb *list = ah; 29072755Sjhb } else { 29172755Sjhb ah->ah_next = prev->ah_next; 29272755Sjhb prev->ah_next = ah; 29372755Sjhb } 29472755Sjhb splx(s); 29572755Sjhb return ah; 29672755Sjhb} 29772755Sjhb 29872755Sjhbstatic void 29972755Sjhbapm_del_hook(struct apmhook **list, struct apmhook *ah) 30072755Sjhb{ 30172755Sjhb int s; 30270035Sjhb struct apmhook *p, *prev; 30370035Sjhb 30470035Sjhb s = splhigh(); 30570035Sjhb prev = NULL; 30670035Sjhb for (p = *list; p != NULL; prev = p, p = p->ah_next) { 30770035Sjhb if (p == ah) { 30870035Sjhb goto deleteit; 30970035Sjhb } 31070035Sjhb } 31170035Sjhb panic("Tried to delete unregistered apm_hook."); 31270035Sjhb goto nosuchnode; 31370035Sjhbdeleteit: 31470035Sjhb if (prev != NULL) { 31570035Sjhb prev->ah_next = p->ah_next; 31672755Sjhb } else { 31772755Sjhb *list = p->ah_next; 31872755Sjhb } 31972755Sjhbnosuchnode: 32072755Sjhb splx(s); 32170035Sjhb} 32272755Sjhb 32370035Sjhb 32493503Sjake/* APM driver calls some functions automatically */ 32593503Sjakestatic void 32693503Sjakeapm_execute_hook(struct apmhook *list) 32793503Sjake{ 32893503Sjake struct apmhook *p; 32993503Sjake 33093503Sjake for (p = list; p != NULL; p = p->ah_next) { 33172755Sjhb#if 0 33270035Sjhb printf("Execute APM hook \"%s.\"\n", p->ah_name); 33370035Sjhb#endif 33470035Sjhb if ((*(p->ah_fun))(p->ah_arg)) { 33570035Sjhb printf("Warning: APM hook \"%s\" failed", p->ah_name); 33670035Sjhb } 33770035Sjhb } 33870035Sjhb} 33970035Sjhb 34070035Sjhb 34170035Sjhb/* establish an apm hook */ 34270035Sjhbstruct apmhook * 343apm_hook_establish(int apmh, struct apmhook *ah) 344{ 345 if (apmh < 0 || apmh >= NAPM_HOOK) 346 return NULL; 347 348 return apm_add_hook(&hook[apmh], ah); 349} 350 351#ifdef notused 352/* disestablish an apm hook */ 353void 354apm_hook_disestablish(int apmh, struct apmhook *ah) 355{ 356 if (apmh < 0 || apmh >= NAPM_HOOK) 357 return; 358 359 apm_del_hook(&hook[apmh], ah); 360} 361#endif /* notused */ 362 363 364static struct timeval suspend_time; 365static struct timeval diff_time; 366 367static int 368apm_default_resume(void *arg) 369{ 370 int pl; 371 u_int second, minute, hour; 372 struct timeval resume_time, tmp_time; 373 374 /* modified for adjkerntz */ 375 pl = splsoftclock(); 376 inittodr(0); /* adjust time to RTC */ 377 microtime(&resume_time); 378 tmp_time = time; /* because 'time' is volatile */ 379 timevaladd(&tmp_time, &diff_time); 380 time = tmp_time; 381 splx(pl); 382 second = resume_time.tv_sec - suspend_time.tv_sec; 383 hour = second / 3600; 384 second %= 3600; 385 minute = second / 60; 386 second %= 60; 387 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n", 388 hour, minute, second); 389 return 0; 390} 391 392static int 393apm_default_suspend(void *arg) 394{ 395 int pl; 396 397 pl = splsoftclock(); 398 microtime(&diff_time); 399 inittodr(0); 400 microtime(&suspend_time); 401 timevalsub(&diff_time, &suspend_time); 402 splx(pl); 403 return 0; 404} 405 406static void apm_processevent(struct apm_softc *); 407 408/* 409 * Public interface to the suspend/resume: 410 * 411 * Execute suspend and resume hook before and after sleep, respectively. 412 * 413 */ 414 415void 416apm_suspend(void) 417{ 418 struct apm_softc *sc; 419 420 sc = master_softc; /* XXX */ 421 if (!sc) 422 return; 423 424 if (sc->initialized) { 425 apm_execute_hook(hook[APM_HOOK_SUSPEND]); 426 apm_suspend_system(sc); 427 apm_processevent(sc); 428 } 429} 430 431void 432apm_resume(void) 433{ 434 struct apm_softc *sc; 435 436 sc = master_softc; /* XXX */ 437 if (!sc) 438 return; 439 440 if (sc->initialized) { 441 apm_execute_hook(hook[APM_HOOK_RESUME]); 442 } 443} 444 445 446/* get APM information */ 447static int 448apm_get_info(struct apm_softc *sc, apm_info_t aip) 449{ 450 u_long eax, ebx, ecx; 451 452 eax = (APM_BIOS << 8) | APM_GETPWSTATUS; 453 ebx = PMDV_ALLDEV; 454 ecx = 0; 455 456 if (apm_int(&eax, &ebx, &ecx)) 457 return 1; 458 459 aip->ai_acline = (ebx >> 8) & 0xff; 460 aip->ai_batt_stat = ebx & 0xff; 461 aip->ai_batt_life = ecx & 0xff; 462 aip->ai_major = (u_int)sc->majorversion; 463 aip->ai_minor = (u_int)sc->minorversion; 464 return 0; 465} 466 467 468/* inform APM BIOS that CPU is idle */ 469void 470apm_cpu_idle(void) 471{ 472 struct apm_softc *sc = master_softc; /* XXX */ 473 474 if (sc->idle_cpu) { 475 if (sc->active) { 476 __asm ("movw $0x5305, %ax; lcall _apm_addr"); 477 } 478 } 479 /* 480 * Some APM implementation halts CPU in BIOS, whenever 481 * "CPU-idle" function are invoked, but swtch() of 482 * FreeBSD halts CPU, therefore, CPU is halted twice 483 * in the sched loop. It makes the interrupt latency 484 * terribly long and be able to cause a serious problem 485 * in interrupt processing. We prevent it by removing 486 * "hlt" operation from swtch() and managed it under 487 * APM driver. 488 */ 489 if (!sc->active || sc->halt_cpu) { 490 __asm("sti ; hlt"); /* wait for interrupt */ 491 } 492} 493 494/* inform APM BIOS that CPU is busy */ 495void 496apm_cpu_busy(void) 497{ 498 struct apm_softc *sc = master_softc; /* XXX */ 499 500 if (sc->idle_cpu && sc->active) { 501 __asm("movw $0x5306, %ax; lcall _apm_addr"); 502 } 503} 504 505 506/* 507 * APM timeout routine: 508 * 509 * This routine is automatically called by timer once per second. 510 */ 511 512static void 513apm_timeout(void *arg) 514{ 515 struct apm_softc *sc = arg; 516 517 apm_processevent(sc); 518 timeout(apm_timeout, (void *)sc, hz - 1 ); /* More than 1 Hz */ 519} 520 521/* enable APM BIOS */ 522static void 523apm_event_enable(struct apm_softc *sc) 524{ 525#ifdef APM_DEBUG 526 printf("called apm_event_enable()\n"); 527#endif 528 if (sc->initialized) { 529 sc->active = 1; 530 apm_timeout(sc); 531 } 532} 533 534/* disable APM BIOS */ 535static void 536apm_event_disable(struct apm_softc *sc) 537{ 538#ifdef APM_DEBUG 539 printf("called apm_event_disable()\n"); 540#endif 541 if (sc->initialized) { 542 untimeout(apm_timeout, NULL); 543 sc->active = 0; 544 } 545} 546 547/* halt CPU in scheduling loop */ 548static void 549apm_halt_cpu(struct apm_softc *sc) 550{ 551 if (sc->initialized) { 552 sc->halt_cpu = 1; 553 } 554} 555 556/* don't halt CPU in scheduling loop */ 557static void 558apm_not_halt_cpu(struct apm_softc *sc) 559{ 560 if (sc->initialized) { 561 sc->halt_cpu = 0; 562 } 563} 564 565/* device driver definitions */ 566static int apmprobe (struct isa_device *); 567static int apmattach(struct isa_device *); 568struct isa_driver apmdriver = { 569 apmprobe, apmattach, "apm" }; 570 571/* 572 * probe APM (dummy): 573 * 574 * APM probing routine is placed on locore.s and apm_init.S because 575 * this process forces the CPU to turn to real mode or V86 mode. 576 * Current version uses real mode, but on future version, we want 577 * to use V86 mode in APM initialization. 578 */ 579 580static int 581apmprobe(struct isa_device *dvp) 582{ 583 int unit = dvp->id_unit; 584 585 /* 586 * XXX - This is necessary here so that we don't panic in the idle 587 * loop because master_softc is unitialized. 588 */ 589 master_softc = &apm_softc[unit]; 590 591 switch (apm_version) { 592 case APMINI_CANTFIND: 593 /* silent */ 594 return 0; 595 case APMINI_NOT32BIT: 596 printf("apm%d: 32bit connection is not supported.\n", unit); 597 return 0; 598 case APMINI_CONNECTERR: 599 printf("apm%d: 32-bit connection error.\n", unit); 600 return 0; 601 } 602 603 return -1; 604} 605 606 607/* Process APM event */ 608static void 609apm_processevent(struct apm_softc *sc) 610{ 611 int apm_event; 612 613#ifdef APM_DEBUG 614# define OPMEV_DEBUGMESSAGE(symbol) case symbol: \ 615 printf("Original APM Event: " #symbol "\n"); 616#else 617# define OPMEV_DEBUGMESSAGE(symbol) case symbol: 618#endif 619 while (1) { 620 apm_event = apm_getevent(sc); 621 if (apm_event == PMEV_NOEVENT) 622 break; 623 switch (apm_event) { 624 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ); 625 apm_suspend(); 626 break; 627 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ); 628 apm_suspend(); 629 break; 630 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ); 631 apm_suspend(); 632 break; 633 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND); 634 apm_suspend(); 635 break; 636 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME); 637 apm_resume(); 638 break; 639 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME); 640 apm_resume(); 641 break; 642 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME); 643 apm_resume(); 644 break; 645 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW); 646 apm_battery_low(sc); 647 apm_suspend(); 648 break; 649 650 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE); 651 break; 652 653 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME); 654 inittodr(0); /* adjust time to RTC */ 655 break; 656 657 default: 658 printf("Unknown Original APM Event 0x%x\n", apm_event); 659 break; 660 } 661 } 662} 663 664/* 665 * Attach APM: 666 * 667 * Initialize APM driver (APM BIOS itself has been initialized in locore.s) 668 * 669 * Now, unless I'm mad, (not quite ruled out yet), the APM-1.1 spec is bogus: 670 * 671 * Appendix C says under the header "APM 1.0/APM 1.1 Modal BIOS Behavior" 672 * that "When an APM Driver connects with an APM 1.1 BIOS, the APM 1.1 BIOS 673 * will default to an APM 1.0 connection. After an APM Driver calls the APM 674 * Driver Version function, specifying that it supports APM 1.1, and [sic!] 675 * APM BIOS will change its behavior to an APM 1.1 connection. If the APM 676 * BIOS is an APM 1.0 BIOS, the APM Driver Version function call will fail, 677 * and the connection will remain an APM 1.0 connection." 678 * 679 * OK so I can establish a 1.0 connection, and then tell that I'm a 1.1 680 * and maybe then the BIOS will tell that it too is a 1.1. 681 * Fine. 682 * Now how will I ever get the segment-limits for instance ? There is no 683 * way I can see that I can get a 1.1 response back from an "APM Protected 684 * Mode 32-bit Interface Connect" function ??? 685 * 686 * Who made this, Intel and Microsoft ? -- How did you guess ! 687 * 688 * /phk 689 */ 690 691static int 692apmattach(struct isa_device *dvp) 693{ 694 int unit = dvp->id_unit; 695 char name[32]; 696#define APM_KERNBASE KERNBASE 697 struct apm_softc *sc = &apm_softc[unit]; 698 699 sc->initialized = 0; 700 sc->active = 0; 701 sc->halt_cpu = 1; 702 703 /* setup APM parameters */ 704 sc->cs16_base = (apm_cs32_base << 4) + APM_KERNBASE; 705 sc->cs32_base = (apm_cs16_base << 4) + APM_KERNBASE; 706 sc->ds_base = (apm_ds_base << 4) + APM_KERNBASE; 707 sc->cs_limit = apm_cs_limit; 708 sc->ds_limit = apm_ds_limit; 709 sc->cs_entry = apm_cs_entry; 710 711 sc->idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0); 712 sc->disabled = ((apm_flags & APM_DISABLED) != 0); 713 sc->disengaged = ((apm_flags & APM_DISENGAGED) != 0); 714 715 /* print bootstrap messages */ 716#ifdef APM_DEBUG 717 printf(" found APM BIOS version %04x\n", apm_version); 718 printf("apm%d: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n", 719 unit, sc->cs32_base, sc->cs16_base, sc->ds_base); 720 printf("apm%d: Code entry 0x%08x, Idling CPU %s, Management %s\n", 721 unit, sc->cs_entry, is_enabled(sc->idle_cpu), 722 is_enabled(!sc->disabled)); 723 printf("apm%d: CS_limit=%x, DS_limit=%x\n", 724 unit, sc->cs_limit, sc->ds_limit); 725#endif /* APM_DEBUG */ 726 727 sc->cs_limit = 0xffff; 728 sc->ds_limit = 0xffff; 729 730 /* setup GDT */ 731 setup_apm_gdt(sc->cs32_base, sc->cs16_base, sc->ds_base, 732 sc->cs_limit, sc->ds_limit); 733 734 /* setup entry point 48bit pointer */ 735 apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL); 736 apm_addr.offset = sc->cs_entry; 737 738 /* Try to kick bios into 1.1 mode */ 739 apm_driver_version(); 740 sc->minorversion = ((apm_version & 0x00f0) >> 4) * 10 + 741 ((apm_version & 0x000f) >> 0); 742 sc->majorversion = ((apm_version & 0xf000) >> 12) * 10 + 743 ((apm_version & 0x0f00) >> 8); 744 745 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion); 746 747 if (sc->intversion >= INTVERSION(1, 1)) { 748 printf("apm%d: Engaged control %s\n", 749 unit, is_enabled(!sc->disengaged)); 750 } 751 752 printf(" found APM BIOS version %d.%d\n", 753 sc->majorversion, sc->minorversion); 754 printf("apm%d: Idling CPU %s\n", unit, is_enabled(sc->idle_cpu)); 755 756 /* enable power management */ 757 if (sc->disabled) { 758 if (apm_enable_disable_pm(sc, 1)) { 759 printf("Warning: APM enable function failed! [%x]\n", 760 apm_errno); 761 } 762 } 763 764 /* engage power managment (APM 1.1 or later) */ 765 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) { 766 if (apm_engage_disengage_pm(sc, 1)) { 767 printf("Warning: APM engage function failed [%x]\n", 768 apm_errno); 769 } 770 } 771 772 /* default suspend hook */ 773 sc->sc_suspend.ah_fun = apm_default_suspend; 774 sc->sc_suspend.ah_arg = sc; 775 sc->sc_suspend.ah_name = "default suspend"; 776 sc->sc_suspend.ah_order = APM_MAX_ORDER; 777 778 /* default resume hook */ 779 sc->sc_resume.ah_fun = apm_default_resume; 780 sc->sc_resume.ah_arg = sc; 781 sc->sc_resume.ah_name = "default resume"; 782 sc->sc_resume.ah_order = APM_MIN_ORDER; 783 784 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend); 785 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume); 786 787 apm_event_enable(sc); 788 789 sc->initialized = 1; 790 791#ifdef DEVFS 792 sprintf(name,"apm%d",unit); 793 sc->sc_devfs_token = devfs_add_devsw( 794 "/", name, &apm_cdevsw, unit, DV_CHR, 0, 0, 0600); 795#endif 796 return 0; 797} 798 799static int 800apmopen(dev_t dev, int flag, int fmt, struct proc *p) 801{ 802 struct apm_softc *sc = &apm_softc[minor(dev)]; 803 804 if (minor(dev) >= NAPM) { 805 return (ENXIO); 806 } 807 if (!sc->initialized) { 808 return ENXIO; 809 } 810 return 0; 811} 812 813static int 814apmclose(dev_t dev, int flag, int fmt, struct proc *p) 815{ 816 return 0; 817} 818 819static int 820apmioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p) 821{ 822 struct apm_softc *sc = &apm_softc[minor(dev)]; 823 int error = 0; 824 825#ifdef APM_DEBUG 826 printf("APM ioctl: minor = %d, cmd = 0x%x\n", minor(dev), cmd); 827#endif 828 829 if (minor(dev) >= NAPM) { 830 return ENXIO; 831 } 832 if (!sc->initialized) { 833 return ENXIO; 834 } 835 switch (cmd) { 836 case APMIO_SUSPEND: 837 apm_suspend(); 838 break; 839 case APMIO_GETINFO: 840 if (apm_get_info(sc, (apm_info_t)addr)) { 841 error = ENXIO; 842 } 843 break; 844 case APMIO_ENABLE: 845 apm_event_enable(sc); 846 break; 847 case APMIO_DISABLE: 848 apm_event_disable(sc); 849 break; 850 case APMIO_HALTCPU: 851 apm_halt_cpu(sc); 852 break; 853 case APMIO_NOTHALTCPU: 854 apm_not_halt_cpu(sc); 855 break; 856 case APMIO_DISPLAYOFF: 857 if (apm_display_off()) { 858 error = ENXIO; 859 } 860 break; 861 default: 862 error = EINVAL; 863 break; 864 } 865 return error; 866} 867 868 869static apm_devsw_installed = 0; 870 871static void 872apm_drvinit(void *unused) 873{ 874 dev_t dev; 875 876 if( ! apm_devsw_installed ) { 877 dev = makedev(CDEV_MAJOR,0); 878 cdevsw_add(&dev,&apm_cdevsw,NULL); 879 apm_devsw_installed = 1; 880 } 881} 882 883SYSINIT(apmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,apm_drvinit,NULL) 884