apm.c revision 34961
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.69 1998/02/09 06:08:06 eivind 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. Do not staticize - used from apm_setup.s */ 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 getmicrotime(&tmp_time); 368 timevaladd(&tmp_time, &diff_time); 369 370#ifdef FIXME 371 /* XXX THIS DOESN'T WORK!!! */ 372 time = tmp_time; 373#endif 374 375#ifdef APM_FIXUP_CALLTODO 376 /* Calculate the delta time suspended */ 377 timevalsub(&resume_time, &suspend_time); 378 /* Fixup the calltodo list with the delta time. */ 379 adjust_timeout_calltodo(&resume_time); 380#endif /* APM_FIXUP_CALLTODOK */ 381 splx(pl); 382#ifndef APM_FIXUP_CALLTODO 383 second = resume_time.tv_sec - suspend_time.tv_sec; 384#else /* APM_FIXUP_CALLTODO */ 385 /* 386 * We've already calculated resume_time to be the delta between 387 * the suspend and the resume. 388 */ 389 second = resume_time.tv_sec; 390#endif /* APM_FIXUP_CALLTODO */ 391 hour = second / 3600; 392 second %= 3600; 393 minute = second / 60; 394 second %= 60; 395 log(LOG_NOTICE, "resumed from suspended mode (slept %02d:%02d:%02d)\n", 396 hour, minute, second); 397 return 0; 398} 399 400static int 401apm_default_suspend(void *arg) 402{ 403 int pl; 404 405 pl = splsoftclock(); 406 microtime(&diff_time); 407 inittodr(0); 408 microtime(&suspend_time); 409 timevalsub(&diff_time, &suspend_time); 410 splx(pl); 411 return 0; 412} 413 414static void apm_processevent(void); 415 416/* 417 * Public interface to the suspend/resume: 418 * 419 * Execute suspend and resume hook before and after sleep, respectively. 420 * 421 */ 422 423void 424apm_suspend(void) 425{ 426 struct apm_softc *sc = &apm_softc; 427 428 if (!sc) 429 return; 430 431 if (sc->initialized) { 432 apm_execute_hook(hook[APM_HOOK_SUSPEND]); 433 if (apm_suspend_system() == 0) 434 apm_processevent(); 435 else 436 /* Failure, 'resume' the system again */ 437 apm_execute_hook(hook[APM_HOOK_RESUME]); 438 } 439} 440 441void 442apm_resume(void) 443{ 444 struct apm_softc *sc = &apm_softc; 445 446 if (!sc) 447 return; 448 449 if (sc->initialized) 450 apm_execute_hook(hook[APM_HOOK_RESUME]); 451} 452 453 454/* get APM information */ 455static int 456apm_get_info(apm_info_t aip) 457{ 458 struct apm_softc *sc = &apm_softc; 459 u_long eax, ebx, ecx, edx; 460 461 eax = (APM_BIOS << 8) | APM_GETPWSTATUS; 462 ebx = PMDV_ALLDEV; 463 ecx = 0; 464 edx = 0xffff; /* default to unknown battery time */ 465 466 if (apm_int(&eax, &ebx, &ecx, &edx)) 467 return 1; 468 469 aip->ai_infoversion = 0; 470 aip->ai_acline = (ebx >> 8) & 0xff; 471 aip->ai_batt_stat = ebx & 0xff; 472 aip->ai_batt_life = ecx & 0xff; 473 aip->ai_major = (u_int)sc->majorversion; 474 aip->ai_minor = (u_int)sc->minorversion; 475 aip->ai_status = (u_int)sc->active; 476 edx &= 0xffff; 477 if (edx == 0xffff) /* Time is unknown */ 478 aip->ai_batt_time = -1; 479 else if (edx & 0x8000) /* Time is in minutes */ 480 aip->ai_batt_time = (edx & 0x7fff) * 60; 481 else /* Time is in seconds */ 482 aip->ai_batt_time = edx; 483 bzero(aip->ai_spare, sizeof aip->ai_spare); 484 485 return 0; 486} 487 488 489/* inform APM BIOS that CPU is idle */ 490void 491apm_cpu_idle(void) 492{ 493 struct apm_softc *sc = &apm_softc; 494 495 if (sc->active) { 496 u_long eax, ebx, ecx, edx; 497 498 eax = (APM_BIOS <<8) | APM_CPUIDLE; 499 edx = ecx = ebx = 0; 500 apm_int(&eax, &ebx, &ecx, &edx); 501 } 502 /* 503 * Some APM implementation halts CPU in BIOS, whenever 504 * "CPU-idle" function are invoked, but swtch() of 505 * FreeBSD halts CPU, therefore, CPU is halted twice 506 * in the sched loop. It makes the interrupt latency 507 * terribly long and be able to cause a serious problem 508 * in interrupt processing. We prevent it by removing 509 * "hlt" operation from swtch() and managed it under 510 * APM driver. 511 */ 512 if (!sc->active || sc->always_halt_cpu) 513 __asm("hlt"); /* wait for interrupt */ 514} 515 516/* inform APM BIOS that CPU is busy */ 517void 518apm_cpu_busy(void) 519{ 520 struct apm_softc *sc = &apm_softc; 521 522 /* 523 * The APM specification says this is only necessary if your BIOS 524 * slows down the processor in the idle task, otherwise it's not 525 * necessary. 526 */ 527 if (sc->slow_idle_cpu && sc->active) { 528 u_long eax, ebx, ecx, edx; 529 530 eax = (APM_BIOS <<8) | APM_CPUBUSY; 531 edx = ecx = ebx = 0; 532 apm_int(&eax, &ebx, &ecx, &edx); 533 } 534} 535 536 537/* 538 * APM timeout routine: 539 * 540 * This routine is automatically called by timer once per second. 541 */ 542 543static void 544apm_timeout(void *dummy) 545{ 546 struct apm_softc *sc = &apm_softc; 547 548 apm_processevent(); 549 if (sc->active == 1) 550 /* Run slightly more oftan than 1 Hz */ 551 apm_timeout_ch = timeout(apm_timeout, NULL, hz - 1 ); 552} 553 554/* enable APM BIOS */ 555static void 556apm_event_enable(void) 557{ 558 struct apm_softc *sc = &apm_softc; 559 560#ifdef APM_DEBUG 561 printf("called apm_event_enable()\n"); 562#endif 563 if (sc->initialized) { 564 sc->active = 1; 565 apm_timeout(sc); 566 } 567} 568 569/* disable APM BIOS */ 570static void 571apm_event_disable(void) 572{ 573 struct apm_softc *sc = &apm_softc; 574 575#ifdef APM_DEBUG 576 printf("called apm_event_disable()\n"); 577#endif 578 if (sc->initialized) { 579 untimeout(apm_timeout, NULL, apm_timeout_ch); 580 sc->active = 0; 581 } 582} 583 584/* halt CPU in scheduling loop */ 585static void 586apm_halt_cpu(void) 587{ 588 struct apm_softc *sc = &apm_softc; 589 590 if (sc->initialized) 591 sc->always_halt_cpu = 1; 592} 593 594/* don't halt CPU in scheduling loop */ 595static void 596apm_not_halt_cpu(void) 597{ 598 struct apm_softc *sc = &apm_softc; 599 600 if (sc->initialized) 601 sc->always_halt_cpu = 0; 602} 603 604/* device driver definitions */ 605static int apmprobe (struct isa_device *); 606static int apmattach(struct isa_device *); 607struct isa_driver apmdriver = { apmprobe, apmattach, "apm" }; 608 609/* 610 * probe APM (dummy): 611 * 612 * APM probing routine is placed on locore.s and apm_init.S because 613 * this process forces the CPU to turn to real mode or V86 mode. 614 * Current version uses real mode, but in a future version, we want 615 * to use V86 mode in APM initialization. 616 */ 617 618static int 619apmprobe(struct isa_device *dvp) 620{ 621 bzero(&apm_softc, sizeof(apm_softc)); 622 623 if ( dvp->id_unit > 0 ) { 624 printf("apm: Only one APM driver supported.\n"); 625 return 0; 626 } 627 switch (apm_version) { 628 case APMINI_CANTFIND: 629 /* silent */ 630 return 0; 631 case APMINI_NOT32BIT: 632 printf("apm: 32bit connection is not supported.\n"); 633 return 0; 634 case APMINI_CONNECTERR: 635 printf("apm: 32-bit connection error.\n"); 636 return 0; 637 } 638 if (dvp->id_flags & 0x20) 639 statclock_disable = 1; 640 return -1; 641} 642 643 644/* Process APM event */ 645static void 646apm_processevent(void) 647{ 648 int apm_event; 649 650#ifdef APM_DEBUG 651# define OPMEV_DEBUGMESSAGE(symbol) case symbol: \ 652 printf("Received APM Event: " #symbol "\n"); 653#else 654# define OPMEV_DEBUGMESSAGE(symbol) case symbol: 655#endif 656 do { 657 apm_event = apm_getevent(); 658 switch (apm_event) { 659 OPMEV_DEBUGMESSAGE(PMEV_STANDBYREQ); 660 apm_suspend(); 661 break; 662 OPMEV_DEBUGMESSAGE(PMEV_SUSPENDREQ); 663 apm_suspend(); 664 break; 665 OPMEV_DEBUGMESSAGE(PMEV_USERSUSPENDREQ); 666 apm_suspend(); 667 break; 668 OPMEV_DEBUGMESSAGE(PMEV_CRITSUSPEND); 669 apm_suspend(); 670 break; 671 OPMEV_DEBUGMESSAGE(PMEV_NORMRESUME); 672 apm_resume(); 673 break; 674 OPMEV_DEBUGMESSAGE(PMEV_CRITRESUME); 675 apm_resume(); 676 break; 677 OPMEV_DEBUGMESSAGE(PMEV_STANDBYRESUME); 678 apm_resume(); 679 break; 680 OPMEV_DEBUGMESSAGE(PMEV_BATTERYLOW); 681 apm_battery_low(); 682 apm_suspend(); 683 break; 684 OPMEV_DEBUGMESSAGE(PMEV_POWERSTATECHANGE); 685 break; 686 OPMEV_DEBUGMESSAGE(PMEV_UPDATETIME); 687 inittodr(0); /* adjust time to RTC */ 688 break; 689 case PMEV_NOEVENT: 690 break; 691 default: 692 printf("Unknown Original APM Event 0x%x\n", apm_event); 693 break; 694 } 695 } while (apm_event != PMEV_NOEVENT); 696} 697 698/* 699 * Attach APM: 700 * 701 * Initialize APM driver (APM BIOS itself has been initialized in locore.s) 702 */ 703 704static int 705apmattach(struct isa_device *dvp) 706{ 707#define APM_KERNBASE KERNBASE 708 struct apm_softc *sc = &apm_softc; 709 710 sc->initialized = 0; 711 712 /* Must be externally enabled */ 713 sc->active = 0; 714 715 /* setup APM parameters */ 716 sc->cs16_base = (apm_cs16_base << 4) + APM_KERNBASE; 717 sc->cs32_base = (apm_cs32_base << 4) + APM_KERNBASE; 718 sc->ds_base = (apm_ds_base << 4) + APM_KERNBASE; 719 sc->cs_limit = apm_cs_limit; 720 sc->ds_limit = apm_ds_limit; 721 sc->cs_entry = apm_cs_entry; 722 723 /* Always call HLT in idle loop */ 724 sc->always_halt_cpu = 1; 725 726 sc->slow_idle_cpu = ((apm_flags & APM_CPUIDLE_SLOW) != 0); 727 sc->disabled = ((apm_flags & APM_DISABLED) != 0); 728 sc->disengaged = ((apm_flags & APM_DISENGAGED) != 0); 729 730 /* print bootstrap messages */ 731#ifdef APM_DEBUG 732 printf("apm: APM BIOS version %04x\n", apm_version); 733 printf("apm: Code32 0x%08x, Code16 0x%08x, Data 0x%08x\n", 734 sc->cs32_base, sc->cs16_base, sc->ds_base); 735 printf("apm: Code entry 0x%08x, Idling CPU %s, Management %s\n", 736 sc->cs_entry, is_enabled(sc->slow_idle_cpu), 737 is_enabled(!sc->disabled)); 738 printf("apm: CS_limit=0x%x, DS_limit=0x%x\n", 739 sc->cs_limit, sc->ds_limit); 740#endif /* APM_DEBUG */ 741 742#if 0 743 /* Workaround for some buggy APM BIOS implementations */ 744 sc->cs_limit = 0xffff; 745 sc->ds_limit = 0xffff; 746#endif 747 748 /* setup GDT */ 749 setup_apm_gdt(sc->cs32_base, sc->cs16_base, sc->ds_base, 750 sc->cs_limit, sc->ds_limit); 751 752 /* setup entry point 48bit pointer */ 753 apm_addr.segment = GSEL(GAPMCODE32_SEL, SEL_KPL); 754 apm_addr.offset = sc->cs_entry; 755 756 if ((dvp->id_flags & 0x10)) { 757 if ((dvp->id_flags & 0xf) >= 0x2) { 758 apm_driver_version(0x102); 759 } 760 if (!apm_version && (dvp->id_flags & 0xf) >= 0x1) { 761 apm_driver_version(0x101); 762 } 763 } else { 764 apm_driver_version(0x102); 765 if (!apm_version) 766 apm_driver_version(0x101); 767 } 768 if (!apm_version) 769 apm_version = 0x100; 770 771 sc->minorversion = ((apm_version & 0x00f0) >> 4) * 10 + 772 ((apm_version & 0x000f) >> 0); 773 sc->majorversion = ((apm_version & 0xf000) >> 12) * 10 + 774 ((apm_version & 0x0f00) >> 8); 775 776 sc->intversion = INTVERSION(sc->majorversion, sc->minorversion); 777 778#ifdef APM_DEBUG 779 if (sc->intversion >= INTVERSION(1, 1)) 780 printf("apm: Engaged control %s\n", is_enabled(!sc->disengaged)); 781#endif 782 783 printf("apm: found APM BIOS version %d.%d\n", 784 sc->majorversion, sc->minorversion); 785 786#ifdef APM_DEBUG 787 printf("apm: Slow Idling CPU %s\n", is_enabled(sc->slow_idle_cpu)); 788#endif 789 790 /* enable power management */ 791 if (sc->disabled) { 792 if (apm_enable_disable_pm(1)) { 793#ifdef APM_DEBUG 794 printf("apm: *Warning* enable function failed! [%x]\n", 795 apm_errno); 796#endif 797 } 798 } 799 800 /* engage power managment (APM 1.1 or later) */ 801 if (sc->intversion >= INTVERSION(1, 1) && sc->disengaged) { 802 if (apm_engage_disengage_pm(1)) { 803#ifdef APM_DEBUG 804 printf("apm: *Warning* engage function failed err=[%x]", 805 apm_errno); 806 printf(" (Docked or using external power?).\n"); 807#endif 808 } 809 } 810 811 /* default suspend hook */ 812 sc->sc_suspend.ah_fun = apm_default_suspend; 813 sc->sc_suspend.ah_arg = sc; 814 sc->sc_suspend.ah_name = "default suspend"; 815 sc->sc_suspend.ah_order = APM_MAX_ORDER; 816 817 /* default resume hook */ 818 sc->sc_resume.ah_fun = apm_default_resume; 819 sc->sc_resume.ah_arg = sc; 820 sc->sc_resume.ah_name = "default resume"; 821 sc->sc_resume.ah_order = APM_MIN_ORDER; 822 823 apm_hook_establish(APM_HOOK_SUSPEND, &sc->sc_suspend); 824 apm_hook_establish(APM_HOOK_RESUME , &sc->sc_resume); 825 826 apm_event_enable(); 827 828 sc->initialized = 1; 829 830#ifdef DEVFS 831 sc->sc_devfs_token = 832 devfs_add_devswf(&apm_cdevsw, 0, DV_CHR, 0, 0, 0600, "apm"); 833#endif 834 return 0; 835} 836 837static int 838apmopen(dev_t dev, int flag, int fmt, struct proc *p) 839{ 840 struct apm_softc *sc = &apm_softc; 841 842 if (minor(dev) != 0 || !sc->initialized) 843 return (ENXIO); 844 845 return 0; 846} 847 848static int 849apmclose(dev_t dev, int flag, int fmt, struct proc *p) 850{ 851 return 0; 852} 853 854static int 855apmioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p) 856{ 857 struct apm_softc *sc = &apm_softc; 858 int error = 0; 859 int newstate; 860 861 if (minor(dev) != 0 || !sc->initialized) 862 return (ENXIO); 863#ifdef APM_DEBUG 864 printf("APM ioctl: cmd = 0x%x\n", cmd); 865#endif 866 switch (cmd) { 867 case APMIO_SUSPEND: 868 if ( sc->active) 869 apm_suspend(); 870 else 871 error = EINVAL; 872 break; 873 case APMIO_GETINFO_OLD: 874 { 875 struct apm_info info; 876 apm_info_old_t aiop; 877 878 if (apm_get_info(&info)) 879 error = ENXIO; 880 aiop = (apm_info_old_t)addr; 881 aiop->ai_major = info.ai_major; 882 aiop->ai_minor = info.ai_minor; 883 aiop->ai_acline = info.ai_acline; 884 aiop->ai_batt_stat = info.ai_batt_stat; 885 aiop->ai_batt_life = info.ai_batt_life; 886 aiop->ai_status = info.ai_status; 887 } 888 break; 889 case APMIO_GETINFO: 890 if (apm_get_info((apm_info_t)addr)) 891 error = ENXIO; 892 break; 893 case APMIO_ENABLE: 894 apm_event_enable(); 895 break; 896 case APMIO_DISABLE: 897 apm_event_disable(); 898 break; 899 case APMIO_HALTCPU: 900 apm_halt_cpu(); 901 break; 902 case APMIO_NOTHALTCPU: 903 apm_not_halt_cpu(); 904 break; 905 case APMIO_DISPLAY: 906 newstate = *(int *)addr; 907 if (apm_display(newstate)) 908 error = ENXIO; 909 break; 910 case APMIO_BIOS: 911 if (apm_bios_call((struct apm_bios_arg*)addr)) 912 error = EIO; 913 break; 914 default: 915 error = EINVAL; 916 break; 917 } 918 return error; 919} 920 921 922static apm_devsw_installed = 0; 923 924static void 925apm_drvinit(void *unused) 926{ 927 dev_t dev; 928 929 if( ! apm_devsw_installed ) { 930 dev = makedev(CDEV_MAJOR,0); 931 cdevsw_add(&dev,&apm_cdevsw,NULL); 932 apm_devsw_installed = 1; 933 } 934} 935 936SYSINIT(apmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,apm_drvinit,NULL) 937