apm.c revision 36735
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.71 1998/06/03 01:59:32 msmith 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(void) 206{ 207 u_long eax, ebx, ecx, edx; 208 209 eax = (APM_BIOS << 8) | APM_SETPWSTATE; 210 ebx = PMDV_ALLDEV; 211 ecx = PMST_SUSPEND; 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(void) 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() == 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 = 0; 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 bzero(aip->ai_spare, sizeof aip->ai_spare); 490 491 return 0; 492} 493 494 495/* inform APM BIOS that CPU is idle */ 496void 497apm_cpu_idle(void) 498{ 499 struct apm_softc *sc = &apm_softc; 500 501 if (sc->active) { 502 u_long eax, ebx, ecx, edx; 503 504 eax = (APM_BIOS <<8) | APM_CPUIDLE; 505 edx = ecx = ebx = 0; 506 apm_int(&eax, &ebx, &ecx, &edx); 507 } 508 /* 509 * Some APM implementation halts CPU in BIOS, whenever 510 * "CPU-idle" function are invoked, but swtch() of 511 * FreeBSD halts CPU, therefore, CPU is halted twice 512 * in the sched loop. It makes the interrupt latency 513 * terribly long and be able to cause a serious problem 514 * in interrupt processing. We prevent it by removing 515 * "hlt" operation from swtch() and managed it under 516 * APM driver. 517 */ 518 if (!sc->active || sc->always_halt_cpu) 519 __asm("hlt"); /* wait for interrupt */ 520} 521 522/* inform APM BIOS that CPU is busy */ 523void 524apm_cpu_busy(void) 525{ 526 struct apm_softc *sc = &apm_softc; 527 528 /* 529 * The APM specification says this is only necessary if your BIOS 530 * slows down the processor in the idle task, otherwise it's not 531 * necessary. 532 */ 533 if (sc->slow_idle_cpu && sc->active) { 534 u_long eax, ebx, ecx, edx; 535 536 eax = (APM_BIOS <<8) | APM_CPUBUSY; 537 edx = ecx = ebx = 0; 538 apm_int(&eax, &ebx, &ecx, &edx); 539 } 540} 541 542 543/* 544 * APM timeout routine: 545 * 546 * This routine is automatically called by timer once per second. 547 */ 548 549static void 550apm_timeout(void *dummy) 551{ 552 struct apm_softc *sc = &apm_softc; 553 554 apm_processevent(); 555 if (sc->active == 1) 556 /* Run slightly more oftan than 1 Hz */ 557 apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1 ); 558} 559 560/* enable APM BIOS */ 561static void 562apm_event_enable(void) 563{ 564 struct apm_softc *sc = &apm_softc; 565 566#ifdef APM_DEBUG 567 printf("called apm_event_enable()\n"); 568#endif 569 if (sc->initialized) { 570 sc->active = 1; 571 apm_timeout(sc); 572 } 573} 574 575/* disable APM BIOS */ 576static void 577apm_event_disable(void) 578{ 579 struct apm_softc *sc = &apm_softc; 580 581#ifdef APM_DEBUG 582 printf("called apm_event_disable()\n"); 583#endif 584 if (sc->initialized) { 585 untimeout(apm_timeout, NULL, apm_timeout_ch); 586 sc->active = 0; 587 } 588} 589 590/* halt CPU in scheduling loop */ 591static void 592apm_halt_cpu(void) 593{ 594 struct apm_softc *sc = &apm_softc; 595 596 if (sc->initialized) 597 sc->always_halt_cpu = 1; 598} 599 600/* don't halt CPU in scheduling loop */ 601static void 602apm_not_halt_cpu(void) 603{ 604 struct apm_softc *sc = &apm_softc; 605 606 if (sc->initialized) 607 sc->always_halt_cpu = 0; 608} 609 610/* device driver definitions */ 611static int apmprobe (struct isa_device *); 612static int apmattach(struct isa_device *); 613struct isa_driver apmdriver = { apmprobe, apmattach, "apm" }; 614 615/* 616 * probe APM (dummy): 617 * 618 * APM probing routine is placed on locore.s and apm_init.S because 619 * this process forces the CPU to turn to real mode or V86 mode. 620 * Current version uses real mode, but in a future version, we want 621 * to use V86 mode in APM initialization. 622 * 623 * XXX If VM86 is defined, we do. 624 */ 625 626static int 627apmprobe(struct isa_device *dvp) 628{ 629#ifdef VM86 630 struct vm86frame vmf; 631 int i; 632#endif 633 634 if ( dvp->id_unit > 0 ) { 635 printf("apm: Only one APM driver supported.\n"); 636 return 0; 637 } 638 639#ifdef VM86 640 bzero(&vmf, sizeof(struct vm86frame)); /* safety */ 641 vmf.vmf_ax = 0x5300; 642 vmf.vmf_bx = 0; 643 if (((i = vm86_intcall(0x15, &vmf)) == 0) && 644 !(vmf.vmf_eflags & PSL_C) && 645 (vmf.vmf_bx == 0x504d)) { 646 647 apm_version = vmf.vmf_ax; 648 apm_flags = vmf.vmf_cx; 649 650 vmf.vmf_ax = 0x5303; 651 vmf.vmf_bx = 0; 652 if (((i = vm86_intcall(0x15, &vmf)) == 0) && 653 !(vmf.vmf_eflags & PSL_C)) { 654 655 apm_cs32_base = vmf.vmf_ax; 656 apm_cs_entry = vmf.vmf_ebx; 657 apm_cs16_base = vmf.vmf_cx; 658 apm_ds_base = vmf.vmf_dx; 659 apm_cs32_limit = vmf.vmf_si; 660 if (apm_version >= 0x0102) 661 apm_cs16_limit = (vmf.esi.r_ex >> 16); 662 apm_ds_limit = vmf.vmf_di; 663#ifdef APM_DEBUG 664 printf("apm: BIOS probe/32-bit connect successful\n"); 665#endif 666 } else { 667 /* XXX constant typo! */ 668 if (vmf.vmf_ah == APME_PROT32NOTDUPPORTED) { 669 apm_version = APMINI_NOT32BIT; 670 } else { 671 apm_version = APMINI_CONNECTERR; 672 } 673#ifdef APM_DEBUG 674 printf("apm: BIOS 32-bit connect failed: error 0x%x carry %d ah 0x%x\n", 675 i, (vmf.vmf_eflags & PSL_C) ? 1 : 0, vmf.vmf_ah); 676#endif 677 } 678 } else { 679 apm_version = APMINI_CANTFIND; 680#ifdef APM_DEBUG 681 printf("apm: BIOS probe failed: error 0x%x carry %d bx 0x%x\n", 682 i, (vmf.vmf_eflags & PSL_C) ? 1 : 0, vmf.vmf_bx); 683#endif 684 } 685#endif 686 687 bzero(&apm_softc, sizeof(apm_softc)); 688 689 switch (apm_version) { 690 case APMINI_CANTFIND: 691 /* silent */ 692 return 0; 693 case APMINI_NOT32BIT: 694 printf("apm: 32bit connection is not supported.\n"); 695 return 0; 696 case APMINI_CONNECTERR: 697 printf("apm: 32-bit connection error.\n"); 698 return 0; 699 } 700 if (dvp->id_flags & 0x20) 701 statclock_disable = 1; 702 return -1; 703} 704 705 706/* Process APM event */ 707static void 708apm_processevent(void) 709{ 710 int apm_event; 711 712#ifdef APM_DEBUG 713# define OPMEV_DEBUGMESSAGE(symbol) case symbol: \ 714 printf("Received APM Event: " #symbol "\n"); 715#else 716# define OPMEV_DEBUGMESSAGE(symbol) case symbol: 717#endif 718 do { 719 apm_event = apm_getevent(); 720 switch (apm_event) { 721 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ); 722 apm_suspend(); 723 break; 724 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ); 725 apm_suspend(); 726 break; 727 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ); 728 apm_suspend(); 729 break; 730 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND); 731 apm_suspend(); 732 break; 733 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME); 734 apm_resume(); 735 break; 736 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME); 737 apm_resume(); 738 break; 739 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME); 740 apm_resume(); 741 break; 742 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW); 743 apm_battery_low(); 744 apm_suspend(); 745 break; 746 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE); 747 break; 748 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME); 749 inittodr(0); /* adjust time to RTC */ 750 break; 751 case PMEV_NOEVENT: 752 break; 753 default: 754 printf("Unknown Original APM Event 0x%x\n", apm_event); 755 break; 756 } 757 } while (apm_event != PMEV_NOEVENT); 758} 759 760/* 761 * Attach APM: 762 * 763 * Initialize APM driver (APM BIOS itself has been initialized in locore.s) 764 */ 765 766static int 767apmattach(struct isa_device *dvp) 768{ 769#define APM_KERNBASE KERNBASE 770 struct apm_softc *sc = &apm_softc; 771 772 sc->initialized = 0; 773 774 /* Must be externally enabled */ 775 sc->active = 0; 776 777 /* setup APM parameters */ 778 sc->cs16_base = (apm_cs16_base << 4) + APM_KERNBASE; 779 sc->cs32_base = (apm_cs32_base << 4) + APM_KERNBASE; 780 sc->ds_base = (apm_ds_base << 4) + APM_KERNBASE; 781 sc->cs32_limit = apm_cs32_limit - 1; 782 if (apm_cs16_limit == 0) 783 apm_cs16_limit == apm_cs32_limit; 784 sc->cs16_limit = apm_cs16_limit - 1; 785 sc->ds_limit = apm_ds_limit - 1; 786 sc->cs_entry = apm_cs_entry; 787 788 /* Always call HLT in idle loop */ 789 sc->always_halt_cpu = 1; 790 791 sc->slow_idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0); 792 sc->disabled = ((apm_flags & APM_DISABLED) != 0); 793 sc->disengaged = ((apm_flags & APM_DISENGAGED) != 0); 794 795 /* print bootstrap messages */ 796#ifdef APM_DEBUG 797 printf("apm: APM BIOS version %04x\n", apm_version); 798 printf("apm: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n", 799 sc->cs32_base, sc->cs16_base, sc->ds_base); 800 printf("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n", 801 sc->cs_entry, is_enabled(sc->slow_idle_cpu), 802 is_enabled(!sc->disabled)); 803 printf("apm: CS32_limit=0x%x, CS16_limit=0x%x, DS_limit=0x%x\n", 804 (u_short)sc->cs32_limit, (u_short)sc->cs16_limit, (u_short)sc->ds_limit); 805#endif /* APM_DEBUG */ 806 807#if 0 808 /* Workaround for some buggy APM BIOS implementations */ 809 sc->cs_limit = 0xffff; 810 sc->ds_limit = 0xffff; 811#endif 812 813 /* setup GDT */ 814 setup_apm_gdt(sc->cs32_base, sc->cs16_base, sc->ds_base, 815 sc->cs32_limit, sc->cs16_limit, sc->ds_limit); 816 817 /* setup entry point 48bit pointer */ 818 apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL); 819 apm_addr.offset = sc->cs_entry; 820 821 if ((dvp->id_flags & 0x10)) { 822 if ((dvp->id_flags & 0xf) >= 0x2) { 823 apm_driver_version(0x102); 824 } 825 if (!apm_version && (dvp->id_flags & 0xf) >= 0x1) { 826 apm_driver_version(0x101); 827 } 828 } else { 829 apm_driver_version(0x102); 830 if (!apm_version) 831 apm_driver_version(0x101); 832 } 833 if (!apm_version) 834 apm_version = 0x100; 835 836 sc->minorversion = ((apm_version & 0x00f0) >> 4) * 10 + 837 ((apm_version & 0x000f) >> 0); 838 sc->majorversion = ((apm_version & 0xf000) >> 12) * 10 + 839 ((apm_version & 0x0f00) >> 8); 840 841 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion); 842 843#ifdef APM_DEBUG 844 if (sc->intversion >= INTVERSION(1, 1)) 845 printf("apm: Engaged control %s\n", is_enabled(!sc->disengaged)); 846#endif 847 848 printf("apm: found APM BIOS version %d.%d\n", 849 sc->majorversion, sc->minorversion); 850 851#ifdef APM_DEBUG 852 printf("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu)); 853#endif 854 855 /* enable power management */ 856 if (sc->disabled) { 857 if (apm_enable_disable_pm(1)) { 858#ifdef APM_DEBUG 859 printf("apm: *Warning* enable function failed! [%x]\n", 860 apm_errno); 861#endif 862 } 863 } 864 865 /* engage power managment (APM 1.1 or later) */ 866 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) { 867 if (apm_engage_disengage_pm(1)) { 868#ifdef APM_DEBUG 869 printf("apm: *Warning* engage function failed err=[%x]", 870 apm_errno); 871 printf(" (Docked or using external power?).\n"); 872#endif 873 } 874 } 875 876 /* default suspend hook */ 877 sc->sc_suspend.ah_fun = apm_default_suspend; 878 sc->sc_suspend.ah_arg = sc; 879 sc->sc_suspend.ah_name = "default suspend"; 880 sc->sc_suspend.ah_order = APM_MAX_ORDER; 881 882 /* default resume hook */ 883 sc->sc_resume.ah_fun = apm_default_resume; 884 sc->sc_resume.ah_arg = sc; 885 sc->sc_resume.ah_name = "default resume"; 886 sc->sc_resume.ah_order = APM_MIN_ORDER; 887 888 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend); 889 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume); 890 891 apm_event_enable(); 892 893 sc->initialized = 1; 894 895#ifdef DEVFS 896 sc->sc_devfs_token = 897 devfs_add_devswf(&apm_cdevsw, 0, DV_CHR, 0, 0, 0600, "apm"); 898#endif 899 return 0; 900} 901 902static int 903apmopen(dev_t dev, int flag, int fmt, struct proc *p) 904{ 905 struct apm_softc *sc = &apm_softc; 906 907 if (minor(dev) != 0 || !sc->initialized) 908 return (ENXIO); 909 910 return 0; 911} 912 913static int 914apmclose(dev_t dev, int flag, int fmt, struct proc *p) 915{ 916 return 0; 917} 918 919static int 920apmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 921{ 922 struct apm_softc *sc = &apm_softc; 923 int error = 0; 924 int newstate; 925 926 if (minor(dev) != 0 || !sc->initialized) 927 return (ENXIO); 928#ifdef APM_DEBUG 929 printf("APM ioctl: cmd = 0x%x\n", cmd); 930#endif 931 switch (cmd) { 932 case APMIO_SUSPEND: 933 if ( sc->active) 934 apm_suspend(); 935 else 936 error = EINVAL; 937 break; 938 case APMIO_GETINFO_OLD: 939 { 940 struct apm_info info; 941 apm_info_old_t aiop; 942 943 if (apm_get_info(&info)) 944 error = ENXIO; 945 aiop = (apm_info_old_t)addr; 946 aiop->ai_major = info.ai_major; 947 aiop->ai_minor = info.ai_minor; 948 aiop->ai_acline = info.ai_acline; 949 aiop->ai_batt_stat = info.ai_batt_stat; 950 aiop->ai_batt_life = info.ai_batt_life; 951 aiop->ai_status = info.ai_status; 952 } 953 break; 954 case APMIO_GETINFO: 955 if (apm_get_info((apm_info_t)addr)) 956 error = ENXIO; 957 break; 958 case APMIO_ENABLE: 959 apm_event_enable(); 960 break; 961 case APMIO_DISABLE: 962 apm_event_disable(); 963 break; 964 case APMIO_HALTCPU: 965 apm_halt_cpu(); 966 break; 967 case APMIO_NOTHALTCPU: 968 apm_not_halt_cpu(); 969 break; 970 case APMIO_DISPLAY: 971 newstate = *(int *)addr; 972 if (apm_display(newstate)) 973 error = ENXIO; 974 break; 975 case APMIO_BIOS: 976 if (apm_bios_call((struct apm_bios_arg*)addr)) 977 error = EIO; 978 break; 979 default: 980 error = EINVAL; 981 break; 982 } 983 return error; 984} 985 986 987static apm_devsw_installed = 0; 988 989static void 990apm_drvinit(void *unused) 991{ 992 dev_t dev; 993 994 if( ! apm_devsw_installed ) { 995 dev = makedev(CDEV_MAJOR,0); 996 cdevsw_add(&dev,&apm_cdevsw,NULL); 997 apm_devsw_installed = 1; 998 } 999} 1000 1001SYSINIT(apmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,apm_drvinit,NULL) 1002