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